diff options
Diffstat (limited to 'ext/net')
-rw-r--r-- | ext/net/lib.rs | 86 | ||||
-rw-r--r-- | ext/net/ops.rs | 43 | ||||
-rw-r--r-- | ext/net/ops_tls.rs | 11 |
3 files changed, 45 insertions, 95 deletions
diff --git a/ext/net/lib.rs b/ext/net/lib.rs index 4703b05cc..76ed02706 100644 --- a/ext/net/lib.rs +++ b/ext/net/lib.rs @@ -8,9 +8,6 @@ pub mod ops_unix; pub mod resolve_addr; use deno_core::error::AnyError; -use deno_core::include_js_files; -use deno_core::Extension; -use deno_core::ExtensionBuilder; use deno_core::OpState; use deno_tls::rustls::RootCertStore; use std::cell::RefCell; @@ -78,55 +75,48 @@ pub struct DefaultTlsOptions { /// would override previously used alias. pub struct UnsafelyIgnoreCertificateErrors(pub Option<Vec<String>>); -fn ext() -> ExtensionBuilder { - Extension::builder_with_deps(env!("CARGO_PKG_NAME"), &["deno_web"]) -} +deno_core::extension!(deno_net, + deps = [ deno_web ], + parameters = [ P: NetPermissions ], + ops = [ + ops::op_net_accept_tcp, + ops::op_net_connect_tcp<P>, + ops::op_net_listen_tcp<P>, + ops::op_net_listen_udp<P>, + ops::op_node_unstable_net_listen_udp<P>, + ops::op_net_recv_udp, + ops::op_net_send_udp<P>, + ops::op_dns_resolve<P>, + ops::op_set_nodelay, + ops::op_set_keepalive, -fn ops<P: NetPermissions + 'static>( - ext: &mut ExtensionBuilder, - root_cert_store: Option<RootCertStore>, - unstable: bool, - unsafely_ignore_certificate_errors: Option<Vec<String>>, -) -> &mut ExtensionBuilder { - let mut ops = ops::init::<P>(); - ops.extend(ops_tls::init::<P>()); + ops_tls::op_tls_start<P>, + ops_tls::op_net_connect_tls<P>, + ops_tls::op_net_listen_tls<P>, + ops_tls::op_net_accept_tls, + ops_tls::op_tls_handshake, - ext.ops(ops).state(move |state| { + #[cfg(unix)] ops_unix::op_net_accept_unix, + #[cfg(unix)] ops_unix::op_net_connect_unix<P>, + #[cfg(unix)] ops_unix::op_net_listen_unix<P>, + #[cfg(unix)] ops_unix::op_net_listen_unixpacket<P>, + #[cfg(unix)] ops_unix::op_node_unstable_net_listen_unixpacket<P>, + #[cfg(unix)] ops_unix::op_net_recv_unixpacket, + #[cfg(unix)] ops_unix::op_net_send_unixpacket<P>, + ], + esm = [ "01_net.js", "02_tls.js" ], + config = { + root_cert_store: Option<RootCertStore>, + unstable: bool, + unsafely_ignore_certificate_errors: Option<Vec<String>>, + }, + state = |state, root_cert_store, unstable, unsafely_ignore_certificate_errors| { state.put(DefaultTlsOptions { - root_cert_store: root_cert_store.clone(), + root_cert_store, }); state.put(UnstableChecker { unstable }); state.put(UnsafelyIgnoreCertificateErrors( - unsafely_ignore_certificate_errors.clone(), + unsafely_ignore_certificate_errors, )); - }) -} - -pub fn init_ops_and_esm<P: NetPermissions + 'static>( - root_cert_store: Option<RootCertStore>, - unstable: bool, - unsafely_ignore_certificate_errors: Option<Vec<String>>, -) -> Extension { - ops::<P>( - &mut ext(), - root_cert_store, - unstable, - unsafely_ignore_certificate_errors, - ) - .esm(include_js_files!("01_net.js", "02_tls.js",)) - .build() -} - -pub fn init_ops<P: NetPermissions + 'static>( - root_cert_store: Option<RootCertStore>, - unstable: bool, - unsafely_ignore_certificate_errors: Option<Vec<String>>, -) -> Extension { - ops::<P>( - &mut ext(), - root_cert_store, - unstable, - unsafely_ignore_certificate_errors, - ) - .build() -} + }, +); diff --git a/ext/net/ops.rs b/ext/net/ops.rs index 8ac08119a..c094ddac2 100644 --- a/ext/net/ops.rs +++ b/ext/net/ops.rs @@ -15,7 +15,6 @@ use deno_core::AsyncRefCell; use deno_core::ByteString; use deno_core::CancelHandle; use deno_core::CancelTryFuture; -use deno_core::OpDecl; use deno_core::OpState; use deno_core::RcRef; use deno_core::Resource; @@ -44,35 +43,6 @@ use trust_dns_resolver::error::ResolveErrorKind; use trust_dns_resolver::system_conf; use trust_dns_resolver::AsyncResolver; -pub fn init<P: NetPermissions + 'static>() -> Vec<OpDecl> { - vec![ - op_net_accept_tcp::decl(), - #[cfg(unix)] - crate::ops_unix::op_net_accept_unix::decl(), - op_net_connect_tcp::decl::<P>(), - #[cfg(unix)] - 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(), - op_net_send_udp::decl::<P>(), - #[cfg(unix)] - crate::ops_unix::op_net_send_unixpacket::decl::<P>(), - op_dns_resolve::decl::<P>(), - op_set_nodelay::decl(), - op_set_keepalive::decl(), - ] -} - #[derive(Serialize, Clone, Debug)] #[serde(rename_all = "camelCase")] pub struct TlsHandshakeInfo { @@ -650,7 +620,6 @@ fn rdata_to_return_record( mod tests { use super::*; use crate::UnstableChecker; - use deno_core::Extension; use deno_core::JsRuntime; use deno_core::RuntimeOptions; use socket2::SockRef; @@ -906,15 +875,17 @@ mod tests { let listener = TcpListener::bind(addr).await.unwrap(); let _ = listener.accept().await; }); - let my_ext = Extension::builder("test_ext") - .state(move |state| { + + deno_core::extension!( + test_ext, + state = |state| { state.put(TestPermission {}); state.put(UnstableChecker { unstable: true }); - }) - .build(); + } + ); let mut runtime = JsRuntime::new(RuntimeOptions { - extensions: vec![my_ext], + extensions: vec![test_ext::init_ops()], ..Default::default() }); diff --git a/ext/net/ops_tls.rs b/ext/net/ops_tls.rs index f550569a1..c0cfb8674 100644 --- a/ext/net/ops_tls.rs +++ b/ext/net/ops_tls.rs @@ -31,7 +31,6 @@ use deno_core::AsyncResult; use deno_core::ByteString; use deno_core::CancelHandle; use deno_core::CancelTryFuture; -use deno_core::OpDecl; use deno_core::OpState; use deno_core::RcRef; use deno_core::Resource; @@ -653,16 +652,6 @@ impl Write for ImplementWriteTrait<'_, TcpStream> { } } -pub fn init<P: NetPermissions + 'static>() -> Vec<OpDecl> { - vec![ - op_tls_start::decl::<P>(), - op_net_connect_tls::decl::<P>(), - op_net_listen_tls::decl::<P>(), - op_net_accept_tls::decl(), - op_tls_handshake::decl(), - ] -} - #[derive(Debug)] pub struct TlsStreamResource { rd: AsyncRefCell<ReadHalf>, |