From 8d5c0112fbbed188f97218ace2357feba3a8746f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 10 Nov 2022 22:03:28 +0100 Subject: feat: don't require --unstable flag for npm programs (#16520) This PR adds copies of several unstable APIs that are available in "Deno[Deno.internal].nodeUnstable" namespace. These copies do not perform unstable check (ie. don't require "--unstable" flag to be present). Otherwise they work exactly the same, including permission checks. These APIs are not meant to be used by users directly and can change at any time. Copies of following APIs are available in that namespace: - Deno.spawnChild - Deno.spawn - Deno.spawnSync - Deno.serve - Deno.upgradeHttpRaw - Deno.listenDatagram --- ext/net/01_net.js | 50 ++++++++++++++++++++++++++------------------------ ext/net/ops.rs | 33 ++++++++++++++++++++++++++++++--- ext/net/ops_unix.rs | 27 ++++++++++++++++++++++++--- 3 files changed, 80 insertions(+), 30 deletions(-) (limited to 'ext/net') diff --git a/ext/net/01_net.js b/ext/net/01_net.js index 765b94035..971ec2e8b 100644 --- a/ext/net/01_net.js +++ b/ext/net/01_net.js @@ -319,30 +319,32 @@ } } - function listenDatagram(args) { - switch (args.transport) { - case "udp": { - const [rid, addr] = ops.op_net_listen_udp( - { - hostname: args.hostname ?? "127.0.0.1", - port: args.port, - }, - args.reuseAddress ?? false, - ); - addr.transport = "udp"; - return new Datagram(rid, addr); - } - case "unixpacket": { - const [rid, path] = ops.op_net_listen_unixpacket(args.path); - const addr = { - transport: "unixpacket", - path, - }; - return new Datagram(rid, addr); + function createListenDatagram(udpOpFn, unixOpFn) { + return function listenDatagram(args) { + switch (args.transport) { + case "udp": { + const [rid, addr] = udpOpFn( + { + hostname: args.hostname ?? "127.0.0.1", + port: args.port, + }, + args.reuseAddress ?? false, + ); + addr.transport = "udp"; + return new Datagram(rid, addr); + } + case "unixpacket": { + const [rid, path] = unixOpFn(args.path); + const addr = { + transport: "unixpacket", + path, + }; + return new Datagram(rid, addr); + } + default: + throw new TypeError(`Unsupported transport: '${transport}'`); } - default: - throw new TypeError(`Unsupported transport: '${transport}'`); - } + }; } async function connect(args) { @@ -389,7 +391,7 @@ TcpConn, UnixConn, listen, - listenDatagram, + createListenDatagram, Listener, shutdown, Datagram, diff --git a/ext/net/ops.rs b/ext/net/ops.rs index e6420bf9e..96de8cff1 100644 --- a/ext/net/ops.rs +++ b/ext/net/ops.rs @@ -53,10 +53,13 @@ pub fn init() -> Vec { crate::ops_unix::op_net_connect_unix::decl::

(), op_net_listen_tcp::decl::

(), op_net_listen_udp::decl::

(), + op_node_unstable_net_listen_udp::decl::

(), #[cfg(unix)] crate::ops_unix::op_net_listen_unix::decl::

(), #[cfg(unix)] crate::ops_unix::op_net_listen_unixpacket::decl::

(), + #[cfg(unix)] + crate::ops_unix::op_node_unstable_net_listen_unixpacket::decl::

(), op_net_recv_udp::decl(), #[cfg(unix)] crate::ops_unix::op_net_recv_unixpacket::decl(), @@ -288,8 +291,7 @@ where Ok((rid, IpAddr::from(local_addr))) } -#[op] -fn op_net_listen_udp( +fn net_listen_udp( state: &mut OpState, addr: IpAddr, reuse_address: bool, @@ -297,7 +299,6 @@ fn op_net_listen_udp( where NP: NetPermissions + 'static, { - super::check_unstable(state, "Deno.listenDatagram"); state .borrow_mut::() .check_net(&(&addr.hostname, Some(addr.port)), "Deno.listenDatagram()")?; @@ -343,6 +344,32 @@ where Ok((rid, IpAddr::from(local_addr))) } +#[op] +fn op_net_listen_udp( + state: &mut OpState, + addr: IpAddr, + reuse_address: bool, +) -> Result<(ResourceId, IpAddr), AnyError> +where + NP: NetPermissions + 'static, +{ + super::check_unstable(state, "Deno.listenDatagram"); + net_listen_udp::(state, addr, reuse_address) +} + +#[op] +fn op_node_unstable_net_listen_udp( + state: &mut OpState, + addr: IpAddr, + reuse_address: bool, +) -> Result<(ResourceId, IpAddr), AnyError> +where + NP: NetPermissions + 'static, +{ + super::check_unstable(state, "Deno.listenDatagram"); + net_listen_udp::(state, addr, reuse_address) +} + #[derive(Serialize, Eq, PartialEq, Debug)] #[serde(untagged)] pub enum DnsReturnRecord { diff --git a/ext/net/ops_unix.rs b/ext/net/ops_unix.rs index b45b02343..bf03f4015 100644 --- a/ext/net/ops_unix.rs +++ b/ext/net/ops_unix.rs @@ -209,8 +209,7 @@ where Ok((rid, pathname)) } -#[op] -pub fn op_net_listen_unixpacket( +pub fn net_listen_unixpacket( state: &mut OpState, path: String, ) -> Result<(ResourceId, Option), AnyError> @@ -218,7 +217,6 @@ where NP: NetPermissions + 'static, { let address_path = Path::new(&path); - super::check_unstable(state, "Deno.listenDatagram"); let permissions = state.borrow_mut::(); permissions.check_read(address_path, "Deno.listenDatagram()")?; permissions.check_write(address_path, "Deno.listenDatagram()")?; @@ -233,6 +231,29 @@ where Ok((rid, pathname)) } +#[op] +pub fn op_net_listen_unixpacket( + state: &mut OpState, + path: String, +) -> Result<(ResourceId, Option), AnyError> +where + NP: NetPermissions + 'static, +{ + super::check_unstable(state, "Deno.listenDatagram"); + net_listen_unixpacket::(state, path) +} + +#[op] +pub fn op_node_unstable_net_listen_unixpacket( + state: &mut OpState, + path: String, +) -> Result<(ResourceId, Option), AnyError> +where + NP: NetPermissions + 'static, +{ + net_listen_unixpacket::(state, path) +} + pub fn pathstring(pathname: &Path) -> Result { into_string(pathname.into()) } -- cgit v1.2.3