summaryrefslogtreecommitdiff
path: root/ext/net/lib.rs
diff options
context:
space:
mode:
authorMatt Mastracci <matthew@mastracci.com>2023-03-17 12:22:15 -0600
committerGitHub <noreply@github.com>2023-03-17 18:22:15 +0000
commite55b448730160a6e4df9815a268d4049ac89deab (patch)
tree35d80fd60f2f1d1d06903caff256484a7d703d76 /ext/net/lib.rs
parent0bc6bf5d33b8198253954d7f04558270de45c925 (diff)
feat(core) deno_core::extension! macro to simplify extension registration (#18210)
This implements two macros to simplify extension registration and centralize a lot of the boilerplate as a base for future improvements: * `deno_core::ops!` registers a block of `#[op]`s, optionally with type parameters, useful for places where we share lists of ops * `deno_core::extension!` is used to register an extension, and creates two methods that can be used at runtime/snapshot generation time: `init_ops` and `init_ops_and_esm`. --------- Co-authored-by: Bartek IwaƄczuk <biwanczuk@gmail.com>
Diffstat (limited to 'ext/net/lib.rs')
-rw-r--r--ext/net/lib.rs86
1 files changed, 38 insertions, 48 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()
-}
+ },
+);