diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2023-03-09 10:56:19 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-09 14:56:19 +0000 |
commit | 8f207c0f3f3a43d77e0c88cfdc840b4b742b9708 (patch) | |
tree | a8699bca5ffffff03a9d72f1bed9208a349338ba /ext/fetch/lib.rs | |
parent | 99da8a69e7260b72e55d7214ec96f6ac5e759f35 (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/fetch/lib.rs')
-rw-r--r-- | ext/fetch/lib.rs | 98 |
1 files changed, 65 insertions, 33 deletions
diff --git a/ext/fetch/lib.rs b/ext/fetch/lib.rs index 8576b3c53..647e0ec7f 100644 --- a/ext/fetch/lib.rs +++ b/ext/fetch/lib.rs @@ -13,6 +13,7 @@ use deno_core::futures::StreamExt; use deno_core::include_js_files; use deno_core::op; use deno_core::BufView; +use deno_core::ExtensionBuilder; use deno_core::WriteOutcome; use deno_core::url::Url; @@ -91,43 +92,74 @@ impl Default for Options { } } -pub fn init<FP>(options: Options) -> Extension -where - FP: FetchPermissions + 'static, -{ +fn ext() -> ExtensionBuilder { Extension::builder_with_deps( env!("CARGO_PKG_NAME"), &["deno_webidl", "deno_web", "deno_url", "deno_console"], ) - .esm(include_js_files!( - "20_headers.js", - "21_formdata.js", - "22_body.js", - "22_http_client.js", - "23_request.js", - "23_response.js", - "26_fetch.js", - )) - .ops(vec![ - op_fetch::decl::<FP>(), - op_fetch_send::decl(), - op_fetch_custom_client::decl::<FP>(), - ]) - .state(move |state| { - state.put::<Options>(options.clone()); - state.put::<reqwest::Client>({ - create_http_client( - options.user_agent.clone(), - options.root_cert_store.clone(), - vec![], - options.proxy.clone(), - options.unsafely_ignore_certificate_errors.clone(), - options.client_cert_chain_and_key.clone(), - ) - .unwrap() - }); - }) - .build() +} + +fn ops<FP>( + ext: &mut ExtensionBuilder, + options: Options, +) -> &mut ExtensionBuilder +where + FP: FetchPermissions + 'static, +{ + ext + .ops(vec![ + op_fetch::decl::<FP>(), + op_fetch_send::decl(), + op_fetch_custom_client::decl::<FP>(), + ]) + .state(move |state| { + state.put::<Options>(options.clone()); + state.put::<reqwest::Client>({ + create_http_client( + options.user_agent.clone(), + options.root_cert_store.clone(), + vec![], + options.proxy.clone(), + options.unsafely_ignore_certificate_errors.clone(), + options.client_cert_chain_and_key.clone(), + ) + .unwrap() + }); + }) +} + +pub fn init_ops_and_esm<FP>(options: Options) -> Extension +where + FP: FetchPermissions + 'static, +{ + ops::<FP>(&mut ext(), options) + .esm(include_js_files!( + "20_headers.js", + "21_formdata.js", + "22_body.js", + "22_http_client.js", + "23_request.js", + "23_response.js", + "26_fetch.js", + )) + .build() +} + +pub fn init_ops<FP>(options: Options) -> Extension +where + FP: FetchPermissions + 'static, +{ + ops::<FP>(&mut ext(), options) + .esm(include_js_files!( + "20_headers.js", + "21_formdata.js", + "22_body.js", + "22_http_client.js", + "23_request.js", + "23_response.js", + "26_fetch.js", + )) + .build() } pub type CancelableResponseFuture = |