diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2022-11-10 22:03:28 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-10 22:03:28 +0100 |
commit | 8d5c0112fbbed188f97218ace2357feba3a8746f (patch) | |
tree | cf08da990c8bf90da116e8db3ac6332cd37578a1 /ext/net | |
parent | 53e974b276b095faf52918c4c6e988e9d2788cef (diff) |
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
Diffstat (limited to 'ext/net')
-rw-r--r-- | ext/net/01_net.js | 50 | ||||
-rw-r--r-- | ext/net/ops.rs | 33 | ||||
-rw-r--r-- | ext/net/ops_unix.rs | 27 |
3 files changed, 80 insertions, 30 deletions
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<P: NetPermissions + 'static>() -> Vec<OpDecl> { crate::ops_unix::op_net_connect_unix::decl::<P>(), op_net_listen_tcp::decl::<P>(), op_net_listen_udp::decl::<P>(), + op_node_unstable_net_listen_udp::decl::<P>(), #[cfg(unix)] crate::ops_unix::op_net_listen_unix::decl::<P>(), #[cfg(unix)] crate::ops_unix::op_net_listen_unixpacket::decl::<P>(), + #[cfg(unix)] + crate::ops_unix::op_node_unstable_net_listen_unixpacket::decl::<P>(), 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<NP>( +fn net_listen_udp<NP>( state: &mut OpState, addr: IpAddr, reuse_address: bool, @@ -297,7 +299,6 @@ fn op_net_listen_udp<NP>( where NP: NetPermissions + 'static, { - super::check_unstable(state, "Deno.listenDatagram"); state .borrow_mut::<NP>() .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<NP>( + 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::<NP>(state, addr, reuse_address) +} + +#[op] +fn op_node_unstable_net_listen_udp<NP>( + 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::<NP>(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<NP>( +pub fn net_listen_unixpacket<NP>( state: &mut OpState, path: String, ) -> Result<(ResourceId, Option<String>), 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::<NP>(); 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<NP>( + state: &mut OpState, + path: String, +) -> Result<(ResourceId, Option<String>), AnyError> +where + NP: NetPermissions + 'static, +{ + super::check_unstable(state, "Deno.listenDatagram"); + net_listen_unixpacket::<NP>(state, path) +} + +#[op] +pub fn op_node_unstable_net_listen_unixpacket<NP>( + state: &mut OpState, + path: String, +) -> Result<(ResourceId, Option<String>), AnyError> +where + NP: NetPermissions + 'static, +{ + net_listen_unixpacket::<NP>(state, path) +} + pub fn pathstring(pathname: &Path) -> Result<String, AnyError> { into_string(pathname.into()) } |