robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local uv =vim . uv 2local strbuffer =require ( 'vim._core.stringbuffer' ) 3 4--- Interface for transport implementations. 5--- 6--- @class (internal, exact) vim.net.Transport 7--- @field listen fun(self: vim.net.Transport, on_read: fun(err: string?, data: string?), on_exit: fun(code: integer, signal: integer)) 8--- @field write fun(self: vim.net.Transport, msg: string) 9--- @field is_closing fun(self: vim.net.Transport): boolean 10--- @field terminate fun(self: vim.net.Transport) 11 12--- Transport backed by newly spawned process using `vim.system()`. 13--- 14--- @class (private, exact) vim.net.TransportRun : vim.net.Transport 15--- @field private cmd string[] Command to start the process. 16--- @field private extra_spawn_params? vim.net.transport.ExtraSpawnParams 17--- @field private log vim.Log 18--- @field private sysobj? vim.SystemObj 19--- @field new fun(cmd: string[], extra_spawn_params?: vim.net.transport.ExtraSpawnParams, log: vim.Log): vim.net.TransportRun 20local TransportRun ={} 21 22function TransportRun . new ( cmd , extra_spawn_params , log ) 23return setmetatable ( { 24cmd =cmd , 25extra_spawn_params =extra_spawn_params , 26log =log , 27} , { __index =TransportRun } ) 28end 29 30--- @param on_read fun(err: string?, data: string?) 31--- @param on_exit fun(code: integer, signal: integer) 32function TransportRun : listen ( on_read , on_exit ) 33--- @param chunk string? 34local function on_stderr ( _ , chunk ) 35if chunk then 36self . log . error ( 'transport' , self . cmd [ 1 ], 'stderr' , chunk ) 37end 38end 39 40self . extra_spawn_params =self . extra_spawn_params or {} 41 42if self . extra_spawn_params . cwd then 43local stat =uv . fs_stat ( self . extra_spawn_params . cwd ) 44assert ( stat and stat . type == 'directory' or false , 'cwd must be a directory' ) 45end 46 47-- Default to non-detached on Windows. 48local detached =vim . fn . has ( 'win32' ) ~= 1 49if self . extra_spawn_params . detached ~= nil then 50detached =self . extra_spawn_params . detached 51end 52 53---@type boolean, vim.SystemObj|string 54local ok , sysobj_or_err =pcall ( vim . system , self . cmd , { 55stdin =true , 56stdout =on_read , 57stderr =on_stderr , 58cwd =self . extra_spawn_params . cwd , 59env =self . extra_spawn_params . env , 60detach =detached , 61} , function ( obj ) 62on_exit ( obj . code , obj . signal ) 63end ) 64 65if not ok then ---@cast sysobj_or_err string 66local err =sysobj_or_err 67local sfx =err : match ( 'ENOENT' ) 68and '. The command is either not installed, missing from PATH, or not executable.' 69or string . format ( ' with error message: %s' , err ) 70 71error (( 'Spawning process with cmd: `%s` failed%s' ): format ( vim . inspect ( self . cmd ), sfx )) 72end ---@cast sysobj_or_err vim.SystemObj 73 74self . sysobj =sysobj_or_err 75end 76 77function TransportRun : write ( msg ) 78assert ( self . sysobj ): write ( msg ) 79end 80 81function TransportRun : is_closing () 82return self . sysobj == nil or self . sysobj : is_closing () 83end 84 85function TransportRun : terminate () 86local sysobj =assert ( self . sysobj ) 87if sysobj : is_closing () then 88return 89end 90sysobj : kill ( 15 ) 91end 92 93--- Transport backed by an existing `uv.uv_pipe_t` or `uv.uv_tcp_t` connection. 94--- 95--- @class (private, exact) vim.net.TransportConnect : vim.net.Transport 96--- @field private host_or_path string 97--- @field private port? integer 98--- @field private log vim.Log 99--- @field private handle? uv.uv_pipe_t|uv.uv_tcp_t 100--- Connect returns a PublicClient synchronously so the caller 101--- can immediately send messages before the connection is established. 102--- These messages are buffered in `msgbuf`. 103--- @field private connected boolean 104--- @field private closing boolean 105--- @field private msgbuf vim.Ringbuf<string> 106--- @field private on_exit? fun(code: integer, signal: integer) 107--- @field new fun(host_or_path: string, port?: integer, log: vim.Log): vim.net.TransportConnect 108local TransportConnect ={} 109 110function TransportConnect . new ( host_or_path , port , log ) 111return setmetatable ( { 112host_or_path =host_or_path , 113port =port , 114log =log , 115connected =false , 116-- size should be enough because the client can't really do anything until initialization is done 117-- which required a response from the process - implying the connection got established 118msgbuf =vim . ringbuf ( 10 ), 119closing =false , 120} , { __index =TransportConnect } ) 121end 122 123--- @param on_read fun(err: string?, data: string?) 124--- @param on_exit? fun(code: integer, signal: integer) 125function TransportConnect : listen ( on_read , on_exit ) 126self . on_exit =on_exit 127self . handle =( 128self . port and assert ( uv . new_tcp (), 'Could not create new TCP socket' ) 129or assert ( uv . new_pipe ( false ), 'Pipe could not be opened.' ) 130) 131 132--- @param err string? 133local function on_connect ( err ) 134if err then 135local address =not self . port and self . host_or_path or ( self . host_or_path .. ':' .. self . port ) 136vim . schedule ( function () 137vim . notify ( 138string . format ( 'Could not connect to %s, reason: %s' , address , vim . inspect ( err )), 139vim . log . levels . WARN 140) 141end ) 142-- Since the actual connection establishment is done asynchronously, we cannot throw an error on failure, 143-- therefore we treat the failure of the actual connection as a disconnection of our abstracted connection. 144-- Furthermore, because we also allow writes before the actual connection is established, 145-- this makes the underlying actual connection remain transparent to the user. 146on_read ( nil , nil ) 147return 148end 149self . handle : read_start ( on_read ) 150self . connected =true 151for msg in self . msgbuf do 152self . handle : write ( msg ) 153end 154end 155 156if not self . port then 157self . handle : connect ( self . host_or_path , on_connect ) 158return 159end 160 161local info =uv . getaddrinfo ( self . host_or_path , nil ) 162local resolved_host =info and info [ 1 ] and info [ 1 ]. addr or self . host_or_path 163self . handle : connect ( resolved_host , self . port , on_connect ) 164end 165 166function TransportConnect : write ( msg ) 167if self . connected then 168local _ , err =assert ( self . handle ): write ( msg ) 169if err and not self . closing then 170self . log . error ( 'Error on handle:write: %q' , err ) 171end 172return 173end 174 175self . msgbuf : push ( msg ) 176end 177 178function TransportConnect : is_closing () 179return self . closing 180end 181 182function TransportConnect : terminate () 183if self . closing then 184return 185end 186self . closing =true 187if self . handle then 188if self . connected then 189self . handle : shutdown () 190end 191self . handle : close () 192end 193if self . on_exit then 194self . on_exit ( 0 , 0 ) 195end 196end 197 198--- Create a message stream from a decoder. 199--- 200--- The decoder consumes from the given string buffer 201--- and returns a message body when a full message is available. 202--- `nil` means it needs more transport data. 203--- decoder errors are reported through `on_error`. 204--- 205---@class (internal, exact) vim.net.MessageStream 206---@field private strbuf vim._core.stringbuffer 207---@field private decode fun(strbuf: vim._core.stringbuffer): string? 208---@field private on_read fun(err: string?, data: string?) 209---@field private on_error fun(err: any) 210---@field feed fun(self: vim.net.MessageStream, err: string?, data: string?) 211---@field encode fun(msg: string): string 212---@field new fun(decode: (fun(strbuf: vim._core.stringbuffer): string?), encode: (fun(msg: string): string), on_read: fun(err: string?, data: string?), on_error: fun(err: any)): vim.net.MessageStream 213local MessageStream ={} 214 215---@param decode fun(strbuf: vim._core.stringbuffer): string? 216---@param encode fun(msg: string): string 217---@param on_read fun(err: string?, data: string?) 218---@param on_error fun(err: any) 219---@return vim.net.MessageStream 220function MessageStream . new ( decode , encode , on_read , on_error ) 221return setmetatable ( { 222strbuf =strbuffer . new (), 223decode =decode , 224on_read =on_read , 225on_error =on_error , 226encode =encode , 227} , { __index =MessageStream } ) 228end 229 230---@param err string? 231---@param data string? 232function MessageStream : feed ( err , data ) 233if err then 234self . on_read ( err , nil ) 235return 236elseif data == nil then 237self . on_read ( nil , nil ) 238return 239end 240 241self . strbuf : put ( data ) 242 243while true do 244local ok , body =pcall ( self . decode , self . strbuf ) 245if not ok then 246self . on_error ( body ) 247return 248elseif body == nil then 249break 250end 251self . on_read ( nil , body ) 252end 253end 254 255return { 256TransportRun =TransportRun , 257TransportConnect =TransportConnect , 258MessageStream =MessageStream , 259}