robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
9432e6c1e2
master
1--- 2--- Basic stream types. 3--- See `rpc_stream.lua` for the msgpack layer. 4--- 5 6local uv =vim . uv 7 8local function close_handle ( handle ) 9if handle and not handle : is_closing () then 10handle : close () 11end 12end 13 14local function read_stop ( handle ) 15if handle and not handle : is_closing () then 16handle : read_stop () 17end 18end 19 20--- @class test.Stream 21--- @field write fun(self, data: string|string[]) 22--- @field read_start fun(self, cb: fun(chunk: string)) 23--- @field read_stop fun(self) 24--- @field close fun(self, signal?: string, noblock?: boolean) 25 26--- Stream over given pipes. 27--- 28--- @class vim.StdioStream : test.Stream 29--- @field private _in uv.uv_pipe_t 30--- @field private _out uv.uv_pipe_t 31local StdioStream ={} 32StdioStream . __index =StdioStream 33 34function StdioStream . open () 35local self =setmetatable ( { 36_in =assert ( uv . new_pipe ( false )), 37_out =assert ( uv . new_pipe ( false )), 38} , StdioStream ) 39self . _in : open ( 0 ) 40self . _out : open ( 1 ) 41return self 42end 43 44--- @param data string|string[] 45function StdioStream : write ( data ) 46self . _out : write ( data ) 47end 48 49function StdioStream : read_start ( cb ) 50self . _in : read_start ( function ( err , chunk ) 51if err then 52error ( err ) 53end 54cb ( chunk ) 55end ) 56end 57 58function StdioStream : read_stop () 59read_stop ( self . _in ) 60end 61 62function StdioStream : close () 63close_handle ( self . _in ) 64close_handle ( self . _out ) 65end 66 67--- Stream over a named pipe or TCP socket. 68--- 69--- @class test.SocketStream : test.Stream 70--- @field package _stream_error? string 71--- @field package _socket uv.uv_pipe_t 72local SocketStream ={} 73SocketStream . __index =SocketStream 74 75function SocketStream . open ( file ) 76local socket =assert ( uv . new_pipe ( false )) 77local self =setmetatable ( { 78_socket =socket , 79_stream_error =nil , 80} , SocketStream ) 81uv . pipe_connect ( socket , file , function ( err ) 82uv . stop () 83self . _stream_error =self . _stream_error or err 84end ) 85-- On Windows, writing to the pipe doesn't work if it's not connected yet, 86-- so wait for the connect callback to be called. 87uv . run () 88if self . _stream_error then 89close_handle ( socket ) 90error ( self . _stream_error ) 91end 92return self 93end 94 95function SocketStream . connect ( host , port ) 96local socket =assert ( uv . new_tcp ()) 97local self =setmetatable ( { 98_socket =socket , 99_stream_error =nil , 100} , SocketStream ) 101uv . tcp_connect ( socket , host , port , function ( err ) 102self . _stream_error =self . _stream_error or err 103end ) 104return self 105end 106 107function SocketStream : write ( data ) 108if self . _stream_error then 109error ( self . _stream_error ) 110end 111uv . write ( self . _socket , data , function ( err ) 112if err then 113self . _stream_error =self . _stream_error or err 114end 115end ) 116end 117 118function SocketStream : read_start ( cb ) 119if self . _stream_error then 120error ( self . _stream_error ) 121end 122uv . read_start ( self . _socket , function ( err , chunk ) 123if err then 124self . _stream_error =self . _stream_error or err 125cb ( nil ) -- Signal EOF so the session layer stops. 126return 127end 128cb ( chunk ) 129end ) 130end 131 132function SocketStream : read_stop () 133if self . _stream_error then 134error ( self . _stream_error ) 135end 136read_stop ( self . _socket ) 137end 138 139function SocketStream : close () 140close_handle ( self . _socket ) 141end 142 143--- Stream over child process stdio. 144--- 145--- @class test.ProcStream : test.Stream 146--- @field private _proc uv.uv_process_t 147--- @field private _pid integer 148--- @field private _child_stdin uv.uv_pipe_t 149--- @field private _child_stdout uv.uv_pipe_t 150--- @field private _child_stderr uv.uv_pipe_t 151--- @field package _closed integer 152--- @field package _on_exit fun(closed: integer?) 153--- Collects stdout (if `collect_text=true`). Treats data as text (CRLF converted to LF). 154--- @field stdout string 155--- Collects stderr as raw data. 156--- @field stderr string 157--- Gets stderr+stdout as text (CRLF converted to LF). 158--- @field output fun(): string 159--- @field stdout_eof boolean 160--- @field stderr_eof boolean 161--- Collects text into the `stdout` field. 162--- @field collect_text boolean 163--- Exit code 164--- @field status integer 165--- @field signal integer 166local ProcStream ={} 167ProcStream . __index =ProcStream 168 169--- Starts child process specified by `argv`. 170--- 171--- @param argv string[] 172--- @param env string[]? 173--- @param io_extra uv.uv_pipe_t? 174--- @param on_exit fun(closed: integer?)? Called after the child process exits. 175--- `closed` is the timestamp (uv.now()) when close() was called, or nil if it wasn't. 176--- @param forward_stderr boolean? Forward child process stderr, otherwise collect it. 177--- @return test.ProcStream 178function ProcStream . spawn ( argv , env , io_extra , on_exit , forward_stderr ) 179local self =setmetatable ( { 180collect_text =false , 181output =function ( self ) 182if not self . collect_text then 183error ( 'set collect_text=true' ) 184end 185return ( self . stderr .. self . stdout ): gsub ( '\r\n' , '\n' ) 186end , 187stdout ='' , 188stderr ='' , 189stdout_eof =false , 190stderr_eof =false , 191_child_stdin =assert ( uv . new_pipe ( false )), 192_child_stdout =assert ( uv . new_pipe ( false )), 193_child_stderr =assert ( uv . new_pipe ( false )), 194_exiting =false , 195_on_exit =on_exit , 196} , ProcStream ) 197local prog =argv [ 1 ] 198local args ={} --- @type string[] 199for i =2 , # argv do 200args [ # args + 1 ] =argv [ i ] 201end 202local stderr =forward_stderr and 1 or self . _child_stderr 203--- @diagnostic disable-next-line:missing-fields 204self . _proc , self . _pid =uv . spawn ( prog , { 205stdio ={ self . _child_stdin , self . _child_stdout , stderr , io_extra } , 206args =args , 207--- @diagnostic disable-next-line:assign-type-mismatch 208env =env , 209} , function ( status , signal ) 210self . signal =signal 211-- "Abort" exit may not set status; force to nonzero in that case. 212self . status =( 0 ~= ( status or 0 ) or 0 == ( signal or 0 )) and status or ( 128 + ( signal or 0 )) 213close_handle ( self . _child_stdin ) 214close_handle ( self . _proc ) 215if self . _on_exit then 216self . _on_exit ( self . _closed ) 217end 218end ) 219 220if not self . _proc then 221local err =self . _pid 222error ( err ) 223end 224 225return self 226end 227 228function ProcStream : write ( data ) 229self . _child_stdin : write ( data ) 230end 231 232function ProcStream : on_read ( stream , cb , err , chunk ) 233if err then 234error ( err ) -- stream read failed? 235elseif chunk then 236-- Always collect stderr, in case it gives useful info on failure. 237if stream == 'stderr' then 238self . stderr =self . stderr .. chunk --[[@as string]] 239elseif stream == 'stdout' and self . collect_text then 240-- Set `stdout` and convert CRLF => LF. 241self . stdout =( self . stdout .. chunk ): gsub ( '\r\n' , '\n' ) 242end 243else 244-- stderr_eof/stdout_eof 245self [ stream .. '_eof' ] =true ---@type boolean 246-- EOF is the stream's lifecycle end even if the caller never closes it. 247close_handle ( stream == 'stdout' and self . _child_stdout or self . _child_stderr ) 248end 249 250-- Handler provided by the caller. 251if cb then 252cb ( chunk ) 253end 254end 255 256--- Collects output until the process exits. 257function ProcStream : wait () 258while not ( self . stdout_eof and self . stderr_eof and ( self . status or self . signal )) do 259uv . run ( 'once' ) 260end 261end 262 263function ProcStream : read_start ( on_stdout , on_stderr ) 264self . _child_stdout : read_start ( function ( err , chunk ) 265self : on_read ( 'stdout' , on_stdout , err , chunk ) 266end ) 267self . _child_stderr : read_start ( function ( err , chunk ) 268self : on_read ( 'stderr' , on_stderr , err , chunk ) 269end ) 270end 271 272function ProcStream : read_stop () 273read_stop ( self . _child_stdout ) 274read_stop ( self . _child_stderr ) 275end 276 277function ProcStream : close ( signal , noblock ) 278if self . _closed then 279return 280end 281self . _closed =uv . now () 282self : read_stop () 283close_handle ( self . _child_stdin ) 284close_handle ( self . _child_stdout ) 285close_handle ( self . _child_stderr ) 286if type ( signal ) == 'string' and self . _proc and not self . _proc : is_closing () then 287self . _proc : kill ( 'sig' .. signal ) 288end 289if not noblock then 290while self . status == nil do 291uv . run 'once' 292end 293return self . status , self . signal 294end 295end 296 297return { 298StdioStream =StdioStream , 299ProcStream =ProcStream , 300SocketStream =SocketStream , 301}