Skip to content

Commit ecace9d

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 5c9aaea commit ecace9d

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.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.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
@@ -114,6 +114,21 @@ Deno.test(
114114
},
115115
);
116116

117+
Deno.test(
118+
{
119+
ignore: Deno.build.os === "windows",
120+
permissions: { read: true, write: true },
121+
},
122+
function netUnixPacketAnonListenClose() {
123+
const socket = Deno.listenDatagram({
124+
transport: "unixpacket",
125+
});
126+
assert(socket.addr.transport === "unixpacket");
127+
assertEquals(socket.addr.path, null);
128+
socket.close();
129+
},
130+
);
131+
117132
Deno.test(
118133
{
119134
ignore: Deno.build.os === "windows",
@@ -152,6 +167,21 @@ Deno.test(
152167
},
153168
);
154169

170+
Deno.test(
171+
{
172+
ignore: Deno.build.os === "windows",
173+
permissions: { read: true, write: false },
174+
},
175+
function netUnixPacketAnonListenWritePermission() {
176+
const socket = Deno.listenDatagram({
177+
transport: "unixpacket",
178+
});
179+
assert(socket.addr.transport === "unixpacket");
180+
assertEquals(socket.addr.path, null);
181+
socket.close();
182+
},
183+
);
184+
155185
Deno.test(
156186
{
157187
permissions: { net: true },

0 commit comments

Comments
 (0)