robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
40ab941f79
master
1--- 2--- Nvim msgpack-RPC protocol session. Manages requests/notifications/responses. 3--- 4 5local uv =vim . uv 6local RpcStream =require ( 'test.client.rpc_stream' ) 7 8--- Nvim msgpack-RPC protocol session. Manages requests/notifications/responses. 9--- 10--- @class test.Session 11--- @field private _pending_messages string[] Requests/notifications received from the remote end. 12--- @field private _rpc_stream test.RpcStream 13--- @field private _prepare uv.uv_prepare_t 14--- @field private _timer uv.uv_timer_t 15--- @field private _is_running boolean true during `Session:run()` scope. 16--- @field data table Arbitrary user data. 17local Session ={ 18eof_err_msg ='Nvim EOF (crash?)' , 19} 20Session . __index =Session 21if package . loaded [ 'jit' ] then 22-- luajit pcall is already coroutine safe 23Session . safe_pcall =pcall 24else 25Session . safe_pcall =require 'coxpcall' . pcall 26end 27 28local function resume ( co , ... ) 29local status , result =coroutine . resume ( co , ... ) 30 31if coroutine . status ( co ) == 'dead' then 32if not status then 33error ( result ) 34end 35return 36end 37 38assert ( coroutine . status ( co ) == 'suspended' ) 39result ( co ) 40end 41 42local function coroutine_exec ( func , ... ) 43local args ={ ... } 44local on_complete --- @type function? 45 46if # args > 0 and type ( args [ # args ]) == 'function' then 47-- completion callback 48on_complete =table . remove ( args ) 49end 50 51resume ( coroutine . create ( function () 52local status , result , flag =Session . safe_pcall ( func , unpack ( args )) 53if on_complete then 54coroutine . yield ( function () 55-- run the completion callback on the main thread 56on_complete ( status , result , flag ) 57end ) 58end 59end )) 60end 61 62--- Creates a new msgpack-RPC session. 63function Session . new ( stream ) 64return setmetatable ( { 65_rpc_stream =RpcStream . new ( stream ), 66_pending_messages ={} , 67_prepare =uv . new_prepare (), 68_timer =uv . new_timer (), 69_is_running =false , 70} , Session ) 71end 72 73--- @param timeout integer? 74--- @return string? 75function Session : next_message ( timeout ) 76local function on_request ( method , args , response ) 77table . insert ( self . _pending_messages , { 'request' , method , args , response } ) 78uv . stop () 79end 80 81local function on_notification ( method , args ) 82table . insert ( self . _pending_messages , { 'notification' , method , args } ) 83uv . stop () 84end 85 86if self . _is_running then 87error ( 'Event loop already running' ) 88end 89 90if # self . _pending_messages > 0 then 91return table . remove ( self . _pending_messages , 1 ) 92end 93 94-- if closed, only return pending messages 95if self . closed then 96return nil 97end 98 99self : _run ( on_request , on_notification , timeout ) 100return table . remove ( self . _pending_messages , 1 ) 101end 102 103--- Sends a notification to the RPC endpoint. 104function Session : notify ( method , ... ) 105self . _rpc_stream : write ( method , { ... } ) 106end 107 108--- Sends a request to the RPC endpoint. 109--- 110--- @param method string 111--- @param ... any 112--- @return boolean, table 113function Session : request ( method , ... ) 114local args ={ ... } 115local err , result 116if self . _is_running then 117err , result =self : _yielding_request ( method , args ) 118else 119err , result =self : _blocking_request ( method , args ) 120end 121 122if err then 123return false , err 124end 125 126return true , result 127end 128 129--- Processes incoming RPC requests/notifications until exhausted. 130--- 131--- TODO(justinmk): luaclient2 avoids this via uvutil.cb_wait() + uvutil.add_idle_call()? 132--- 133--- @param request_cb function Handles requests from the sever to the local end. 134--- @param notification_cb function Handles notifications from the sever to the local end. 135--- @param setup_cb function 136--- @param timeout number 137function Session : run ( request_cb , notification_cb , setup_cb , timeout ) 138--- Handles an incoming request. 139local function on_request ( method , args , response ) 140coroutine_exec ( request_cb , method , args , function ( status , result , flag ) 141if status then 142response : send ( result , flag ) 143else 144response : send ( result , true ) 145end 146end ) 147end 148 149--- Handles an incoming notification. 150local function on_notification ( method , args ) 151coroutine_exec ( notification_cb , method , args ) 152end 153 154self . _is_running =true 155 156if setup_cb then 157coroutine_exec ( setup_cb ) 158end 159 160while # self . _pending_messages > 0 do 161local msg =table . remove ( self . _pending_messages , 1 ) 162if msg [ 1 ] == 'request' then 163on_request ( msg [ 2 ], msg [ 3 ], msg [ 4 ]) 164else 165on_notification ( msg [ 2 ], msg [ 3 ]) 166end 167end 168 169self : _run ( on_request , on_notification , timeout ) 170self . _is_running =false 171end 172 173function Session : stop () 174uv . stop () 175end 176 177function Session : close ( signal , noblock ) 178if not self . _timer : is_closing () then 179self . _timer : close () 180end 181if not self . _prepare : is_closing () then 182self . _prepare : close () 183end 184self . _rpc_stream : close ( signal , noblock ) 185self . closed =true 186end 187 188--- Sends a request to the RPC endpoint, without blocking (schedules a coroutine). 189function Session : _yielding_request ( method , args ) 190return coroutine . yield ( function ( co ) 191self . _rpc_stream : write ( method , args , function ( err , result ) 192resume ( co , err , result ) 193end ) 194end ) 195end 196 197--- Sends a request to the RPC endpoint, and blocks (polls event loop) until a response is received. 198function Session : _blocking_request ( method , args ) 199local err , result 200 201-- Invoked when a request is received from the remote end. 202local function on_request ( method_ , args_ , response ) 203table . insert ( self . _pending_messages , { 'request' , method_ , args_ , response } ) 204end 205 206-- Invoked when a notification is received from the remote end. 207local function on_notification ( method_ , args_ ) 208table . insert ( self . _pending_messages , { 'notification' , method_ , args_ } ) 209end 210 211self . _rpc_stream : write ( method , args , function ( e , r ) 212err =e 213result =r 214uv . stop () 215end ) 216 217-- Poll for incoming requests/notifications received from the remote end. 218self : _run ( on_request , on_notification ) 219return ( err or self . eof_err ), result 220end 221 222--- Polls for incoming requests/notifications received from the remote end. 223function Session : _run ( request_cb , notification_cb , timeout ) 224if type ( timeout ) == 'number' then 225self . _prepare : start ( function () 226self . _timer : start ( timeout , 0 , function () 227uv . stop () 228end ) 229self . _prepare : stop () 230end ) 231end 232local got_eof =false 233self . _rpc_stream : read_start ( request_cb , notification_cb , function () 234uv . stop () 235got_eof =true 236end ) 237local ret , err , _ =uv . run () 238if ret == nil then 239error ( err ) 240end 241self . _prepare : stop () 242self . _timer : stop () 243self . _rpc_stream : read_stop () 244if got_eof then 245self . eof_err ={ 1 , self : _eof_msg () } 246end 247end 248 249--- Message shown on unexpected stdout EOF. 250--- @private 251--- @return string 252function Session : _eof_msg () 253local msg =self . eof_err_msg 254--- @diagnostic disable-next-line: access-invisible 255local proc =self . _rpc_stream . _stream --[[@as test.ProcStream]] 256 257-- ProcStream (spawned child) has stderr + exit-code; SocketStream (a socket peer) does not. 258if proc . stderr ~= nil then 259-- stdout EOF and process-exit are separate libuv events. Wait for up to 1s. 260vim . wait ( 1000 , function () 261return proc . status ~= nil or proc . signal ~= nil 262end ) 263if proc . status ~= nil then 264msg =( '%s exit code: %d (0x%08X)' ): format ( msg , proc . status , proc . status % 2 ^ 32 ) 265end 266if proc . signal ~= nil and proc . signal ~= 0 then 267msg =( '%s signal: %d' ): format ( msg , proc . signal ) 268end 269end 270 271local stderr =proc . stderr --[[@as string?]] 272if '' ~= (( stderr or '' ): match ( '^%s*(.*%S)' ) or '' ) then 273msg =msg .. ' stderr:\n' .. stderr 274end 275return msg 276end 277 278--- Nvim msgpack-RPC session. 279return Session