From 8f207c0f3f3a43d77e0c88cfdc840b4b742b9708 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 9 Mar 2023 10:56:19 -0400 Subject: refactor: Split extension registration for runtime and snapshotting (#18095) This commit splits "::init" functions into "init_ops" and "init_ops_and_esm". That way we don't have to construct list of ESM sources on each startup if we're running with a snapshot. In a follow up commit "deno_core" will be changed to not have a split between "extensions" and "extensions_with_js" - it will be embedders' responsibility to pass appropriately configured extensions. Prerequisite for https://github.com/denoland/deno/pull/18080 --- ext/websocket/lib.rs | 69 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 18 deletions(-) (limited to 'ext/websocket/lib.rs') diff --git a/ext/websocket/lib.rs b/ext/websocket/lib.rs index bf6d51914..dbbeae21f 100644 --- a/ext/websocket/lib.rs +++ b/ext/websocket/lib.rs @@ -9,6 +9,7 @@ use deno_core::futures::SinkExt; use deno_core::futures::StreamExt; use deno_core::include_js_files; use deno_core::op; +use deno_core::ExtensionBuilder; use deno_core::url; use deno_core::AsyncRefCell; @@ -497,33 +498,65 @@ pub async fn op_ws_next_event( Ok(res) } -pub fn init( +fn ext() -> ExtensionBuilder { + Extension::builder_with_deps( + env!("CARGO_PKG_NAME"), + &["deno_url", "deno_webidl"], + ) +} + +fn ops( + ext: &mut ExtensionBuilder, + user_agent: String, + root_cert_store: Option, + unsafely_ignore_certificate_errors: Option>, +) -> &mut ExtensionBuilder { + ext + .ops(vec![ + op_ws_check_permission_and_cancel_handle::decl::

(), + op_ws_create::decl::

(), + op_ws_send::decl(), + op_ws_close::decl(), + op_ws_next_event::decl(), + ]) + .state(move |state| { + state.put::(WsUserAgent(user_agent.clone())); + state.put(UnsafelyIgnoreCertificateErrors( + unsafely_ignore_certificate_errors.clone(), + )); + state.put::(WsRootStore(root_cert_store.clone())); + }) +} + +pub fn init_ops_and_esm( user_agent: String, root_cert_store: Option, unsafely_ignore_certificate_errors: Option>, ) -> Extension { - Extension::builder_with_deps( - env!("CARGO_PKG_NAME"), - &["deno_url", "deno_webidl"], + ops::

( + &mut ext(), + user_agent, + root_cert_store, + unsafely_ignore_certificate_errors, ) .esm(include_js_files!( "01_websocket.js", "02_websocketstream.js", )) - .ops(vec![ - op_ws_check_permission_and_cancel_handle::decl::

(), - op_ws_create::decl::

(), - op_ws_send::decl(), - op_ws_close::decl(), - op_ws_next_event::decl(), - ]) - .state(move |state| { - state.put::(WsUserAgent(user_agent.clone())); - state.put(UnsafelyIgnoreCertificateErrors( - unsafely_ignore_certificate_errors.clone(), - )); - state.put::(WsRootStore(root_cert_store.clone())); - }) + .build() +} + +pub fn init_ops( + user_agent: String, + root_cert_store: Option, + unsafely_ignore_certificate_errors: Option>, +) -> Extension { + ops::

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