From 1e9e4a975ecf4e2174bc60f9e1892edfcba6cd78 Mon Sep 17 00:00:00 2001 From: Chance Russell Date: Fri, 16 Feb 2024 14:04:40 -0600 Subject: [PATCH 1/2] transport#connect: pass nrepl+unix: strings unchanged We need a way to let the user indicate that they want a AF_UNIX socket connection. I don't have the Vimscript skills to plumb in an alternate connection command and function, so here's a simple, dumb proof of concept: if the user passes a string starting with nrepl+unix:, we'll pass that string unmodified as the host value when we call the Python script. --- autoload/fireplace/transport.vim | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/autoload/fireplace/transport.vim b/autoload/fireplace/transport.vim index 4c4d91d..e5ec4e0 100644 --- a/autoload/fireplace/transport.vim +++ b/autoload/fireplace/transport.vim @@ -169,16 +169,20 @@ augroup fireplace_transport augroup END function! fireplace#transport#connect(arg) abort - let url = substitute(a:arg, '#.*', '', '') - if url =~# '^\d\+$' - let url = 'nrepl://localhost:' . url - elseif url =~# '^[^:/@]\+\(:\d\+\)\=$' - let url = 'nrepl://' . url - elseif url !~# '^\a\+://' - throw "Fireplace: invalid connection string " . string(a:arg) + if a:arg =~# '^nrepl+unix:.*' + let url = a:arg + else + let url = substitute(a:arg, '#.*', '', '') + if url =~# '^\d\+$' + let url = 'nrepl://localhost:' . url + elseif url =~# '^[^:/@]\+\(:\d\+\)\=$' + let url = 'nrepl://' . url + elseif url !~# '^\a\+://' + throw "Fireplace: invalid connection string " . string(a:arg) + endif + let url = substitute(url, '^\a\+://[^/]*\zs$', '/', '') + let url = substitute(url, '^nrepl://[^/:]*\zs/', ':7888/', '') endif - let url = substitute(url, '^\a\+://[^/]*\zs$', '/', '') - let url = substitute(url, '^nrepl://[^/:]*\zs/', ':7888/', '') if has_key(s:urls, url) return s:urls[url].transport endif From 836a6186bba078c36b25aae7308c72d3b3dcba93 Mon Sep 17 00:00:00 2001 From: Chance Russell Date: Fri, 16 Feb 2024 14:09:43 -0600 Subject: [PATCH 2/2] fireplace.py: add basic support for AF_UNIX sockets Proof of concept. Punting on adding a cleaner path for signaling that the user wants to connect to such a server in favor of the dumbest thing that could work: if the input host string starts with `nrepl+unix:`, use the rest of the string as the socket path. --- pythonx/fireplace.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/pythonx/fireplace.py b/pythonx/fireplace.py index a5a5549..d846022 100644 --- a/pythonx/fireplace.py +++ b/pythonx/fireplace.py @@ -93,17 +93,22 @@ def quickfix(t, e, tb): return {'title': str(e), 'items': items} class Connection: - def __init__(self, host, port, keepalive_file=None): + def __init__(self, host, port, keepalive_file=None, socket_type=socket.AF_INET, socket_path=None): self.keepalive_file = keepalive_file self.connected = False self.host = host - self.port = int(port) + self.port = int(port) if port else None + self.socket_path = socket_path + self.socket_type = socket_type def socket(self): if not self.connected: - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s = socket.socket(self.socket_type, socket.SOCK_STREAM) s.settimeout(8) - s.connect((self.host, self.port)) + if self.socket_type == socket.AF_UNIX: + s.connect(self.socket_path) + else: + s.connect((self.host, self.port)) s.setblocking(1) self._socket = s self.connected = True @@ -178,11 +183,15 @@ def tunnel(self): def main(host = None, port = None, *args): try: - match = re.search('//([^:/@]+)(?::(\d+))?', host) - if match: - host = match.groups()[0] - port = match.groups()[1] - conn = Connection(host, int(port or 7888)) + if re.fullmatch('nrepl\+unix:.*', host): + socket_path = re.sub('^nrepl\+unix:', '', host) + conn = Connection(None, None, socket_path=socket_path, socket_type=socket.AF_UNIX) + else: + match = re.search('//([^:/@]+)(?::(\d+))?', host) + if match: + host = match.groups()[0] + port = match.groups()[1] + conn = Connection(host, int(port or 7888)) try: conn.tunnel() finally: