Skip to content

Commit 076f396

Browse files
committed
feat(net): Make missing path for the unixpacket transport mean using an unbound socket
Useful if the socket is only used to send messages, but not receive them. (ie: syslog, readiness notify socket, …)
1 parent 944edc4 commit 076f396

File tree

2 files changed

+52
-14
lines changed

2 files changed

+52
-14
lines changed

ext/net/ops_unix.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -198,17 +198,25 @@ pub fn op_net_listen_unix(
198198

199199
pub fn net_listen_unixpacket(
200200
state: &mut OpState,
201-
address_path: &str,
201+
address_path: Option<&str>,
202202
) -> Result<(ResourceId, Option<String>), NetError> {
203-
let permissions = state.borrow_mut::<PermissionsContainer>();
204-
let address_path = permissions
205-
.check_open(
206-
Cow::Borrowed(Path::new(address_path)),
207-
OpenAccessKind::ReadWriteNoFollow,
208-
Some("Deno.listenDatagram()"),
209-
)
210-
.map_err(NetError::Permission)?;
211-
let socket = UnixDatagram::bind(address_path)?;
203+
let socket = match address_path {
204+
// Bind to given path
205+
Some(address_path) => {
206+
let permissions = state.borrow_mut::<PermissionsContainer>();
207+
let address_path = permissions
208+
.check_open(
209+
Cow::Borrowed(Path::new(address_path)),
210+
OpenAccessKind::ReadWriteNoFollow,
211+
Some("Deno.listenDatagram()"),
212+
)
213+
.map_err(NetError::Permission)?;
214+
UnixDatagram::bind(address_path)?
215+
}
216+
217+
// Leave socket unbound
218+
None => UnixDatagram::unbound()?,
219+
};
212220
let local_addr = socket.local_addr()?;
213221
let pathname = local_addr.as_pathname().map(pathstring).transpose()?;
214222
let datagram_resource = UnixDatagramResource {
@@ -223,19 +231,19 @@ pub fn net_listen_unixpacket(
223231
#[serde]
224232
pub fn op_net_listen_unixpacket(
225233
state: &mut OpState,
226-
#[string] path: &str,
234+
#[string] path: Option<String>, // todo: Option<&str> not supported in ops yet
227235
) -> Result<(ResourceId, Option<String>), NetError> {
228236
super::check_unstable(state, "Deno.listenDatagram");
229-
net_listen_unixpacket(state, path)
237+
net_listen_unixpacket(state, path.map(|s| s.as_deref()))
230238
}
231239

232240
#[op2(stack_trace)]
233241
#[serde]
234242
pub fn op_node_unstable_net_listen_unixpacket(
235243
state: &mut OpState,
236-
#[string] path: &str,
244+
#[string] path: Option<String>, // todo: Option<&str> not supported in ops yet
237245
) -> Result<(ResourceId, Option<String>), NetError> {
238-
net_listen_unixpacket(state, path)
246+
net_listen_unixpacket(state, path.map(|s| s.as_deref()))
239247
}
240248

241249
pub fn pathstring(pathname: &Path) -> Result<String, NetError> {

tests/unit/net_test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@ Deno.test(
9999
},
100100
);
101101

102+
Deno.test(
103+
{
104+
ignore: Deno.build.os === "windows",
105+
permissions: { read: true, write: true },
106+
},
107+
function netUnixPacketAnonListenClose() {
108+
const socket = Deno.listenDatagram({
109+
transport: "unixpacket",
110+
});
111+
assert(socket.addr.transport === "unixpacket");
112+
assertEquals(socket.addr.path, "");
113+
socket.close();
114+
},
115+
);
116+
102117
Deno.test(
103118
{
104119
ignore: Deno.build.os === "windows",
@@ -137,6 +152,21 @@ Deno.test(
137152
},
138153
);
139154

155+
Deno.test(
156+
{
157+
ignore: Deno.build.os === "windows",
158+
permissions: { read: true, write: false },
159+
},
160+
function netUnixPacketAnonListenWritePermission() {
161+
const socket = Deno.listenDatagram({
162+
transport: "unixpacket",
163+
});
164+
assert(socket.addr.transport === "unixpacket");
165+
assertEquals(socket.addr.path, "");
166+
socket.close();
167+
},
168+
);
169+
140170
Deno.test(
141171
{
142172
permissions: { net: true },

0 commit comments

Comments
 (0)