From e55b448730160a6e4df9815a268d4049ac89deab Mon Sep 17 00:00:00 2001 From: Matt Mastracci Date: Fri, 17 Mar 2023 12:22:15 -0600 Subject: feat(core) deno_core::extension! macro to simplify extension registration (#18210) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- ext/net/lib.rs | 86 ++++++++++++++++++++++++++-------------------------------- 1 file changed, 38 insertions(+), 48 deletions(-) (limited to 'ext/net/lib.rs') 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>); -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

, + ops::op_net_listen_tcp

, + ops::op_net_listen_udp

, + ops::op_node_unstable_net_listen_udp

, + ops::op_net_recv_udp, + ops::op_net_send_udp

, + ops::op_dns_resolve

, + ops::op_set_nodelay, + ops::op_set_keepalive, -fn ops( - ext: &mut ExtensionBuilder, - root_cert_store: Option, - unstable: bool, - unsafely_ignore_certificate_errors: Option>, -) -> &mut ExtensionBuilder { - let mut ops = ops::init::

(); - ops.extend(ops_tls::init::

()); + ops_tls::op_tls_start

, + ops_tls::op_net_connect_tls

, + ops_tls::op_net_listen_tls

, + 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

, + #[cfg(unix)] ops_unix::op_net_listen_unix

, + #[cfg(unix)] ops_unix::op_net_listen_unixpacket

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

, + #[cfg(unix)] ops_unix::op_net_recv_unixpacket, + #[cfg(unix)] ops_unix::op_net_send_unixpacket

, + ], + esm = [ "01_net.js", "02_tls.js" ], + config = { + root_cert_store: Option, + unstable: bool, + unsafely_ignore_certificate_errors: Option>, + }, + 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( - root_cert_store: Option, - unstable: bool, - unsafely_ignore_certificate_errors: Option>, -) -> Extension { - ops::

( - &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( - root_cert_store: Option, - unstable: bool, - unsafely_ignore_certificate_errors: Option>, -) -> Extension { - ops::

( - &mut ext(), - root_cert_store, - unstable, - unsafely_ignore_certificate_errors, - ) - .build() -} + }, +); -- cgit v1.2.3