summaryrefslogtreecommitdiff
path: root/ext/websocket
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2023-03-09 10:56:19 -0400
committerGitHub <noreply@github.com>2023-03-09 14:56:19 +0000
commit8f207c0f3f3a43d77e0c88cfdc840b4b742b9708 (patch)
treea8699bca5ffffff03a9d72f1bed9208a349338ba /ext/websocket
parent99da8a69e7260b72e55d7214ec96f6ac5e759f35 (diff)
refactor: Split extension registration for runtime and snapshotting (#18095)
This commit splits "<ext_name>::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
Diffstat (limited to 'ext/websocket')
-rw-r--r--ext/websocket/lib.rs69
1 files changed, 51 insertions, 18 deletions
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<P: WebSocketPermissions + 'static>(
+fn ext() -> ExtensionBuilder {
+ Extension::builder_with_deps(
+ env!("CARGO_PKG_NAME"),
+ &["deno_url", "deno_webidl"],
+ )
+}
+
+fn ops<P: WebSocketPermissions + 'static>(
+ ext: &mut ExtensionBuilder,
+ user_agent: String,
+ root_cert_store: Option<RootCertStore>,
+ unsafely_ignore_certificate_errors: Option<Vec<String>>,
+) -> &mut ExtensionBuilder {
+ ext
+ .ops(vec![
+ op_ws_check_permission_and_cancel_handle::decl::<P>(),
+ op_ws_create::decl::<P>(),
+ op_ws_send::decl(),
+ op_ws_close::decl(),
+ op_ws_next_event::decl(),
+ ])
+ .state(move |state| {
+ state.put::<WsUserAgent>(WsUserAgent(user_agent.clone()));
+ state.put(UnsafelyIgnoreCertificateErrors(
+ unsafely_ignore_certificate_errors.clone(),
+ ));
+ state.put::<WsRootStore>(WsRootStore(root_cert_store.clone()));
+ })
+}
+
+pub fn init_ops_and_esm<P: WebSocketPermissions + 'static>(
user_agent: String,
root_cert_store: Option<RootCertStore>,
unsafely_ignore_certificate_errors: Option<Vec<String>>,
) -> Extension {
- Extension::builder_with_deps(
- env!("CARGO_PKG_NAME"),
- &["deno_url", "deno_webidl"],
+ ops::<P>(
+ &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::<P>(),
- op_ws_create::decl::<P>(),
- op_ws_send::decl(),
- op_ws_close::decl(),
- op_ws_next_event::decl(),
- ])
- .state(move |state| {
- state.put::<WsUserAgent>(WsUserAgent(user_agent.clone()));
- state.put(UnsafelyIgnoreCertificateErrors(
- unsafely_ignore_certificate_errors.clone(),
- ));
- state.put::<WsRootStore>(WsRootStore(root_cert_store.clone()));
- })
+ .build()
+}
+
+pub fn init_ops<P: WebSocketPermissions + 'static>(
+ user_agent: String,
+ root_cert_store: Option<RootCertStore>,
+ unsafely_ignore_certificate_errors: Option<Vec<String>>,
+) -> Extension {
+ ops::<P>(
+ &mut ext(),
+ user_agent,
+ root_cert_store,
+ unsafely_ignore_certificate_errors,
+ )
.build()
}