Skip to content

Commit e235705

Browse files
committed
add support to connect on socket.unix
1 parent 776095b commit e235705

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

lib/socket/unix.lua

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
--
22
-- SPDX-FileCopyrightText: (c) 2025 Ring Zero Desenvolvimento de Software LTDA
3-
-- SPDX-License-Identifier: MIT OR GPL-2.0-only
3+
-- SPDX-License-Identifier: MIT OR GPL-2.0-only
44
--
55

66
---
@@ -17,6 +17,15 @@ local sock = socket.sock
1717

1818
local unix = {}
1919

20+
---
21+
-- Connects a new UNIX domain socket to a specific path.
22+
-- @param path (string) The UNIX domain socket path.
23+
-- @param type (string) [optional] "STREAM" or "DGRAM".
24+
-- @return A new UNIX domain socket object (stream by default) connected to path.
25+
-- @raise Error if socket.new() or socket.connect() fail.
26+
-- @see socket.new
27+
-- @see socket.connect
28+
2029
---
2130
-- Binds a new UNIX domain socket to a specific path.
2231
-- @param path (string) The UNIX domain socket path.
@@ -25,12 +34,17 @@ local unix = {}
2534
-- @raise Error if socket.new() or socket.bind() fail.
2635
-- @see socket.new
2736
-- @see socket.bind
28-
function unix.bind(path, _type)
29-
local t = _type and string.upper(_type) or "STREAM"
30-
local s = socket.new(af.UNIX, sock[t], 0)
31-
s:bind(path)
32-
return s
37+
local methods = {connect = true, bind = true}
38+
39+
local function __index(_, method)
40+
assert(methods[method], "undefined method")
41+
return function (path, type)
42+
local type = type and string.upper(type) or "STREAM"
43+
local sock = socket.new(af.UNIX, sock[type], 0)
44+
sock[method](sock, path)
45+
return sock
46+
end
3347
end
3448

35-
return unix
49+
return setmetatable(unix, {__index = __index})
3650

0 commit comments

Comments
 (0)