summaryrefslogtreecommitdiff
path: root/ext/fetch/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/fetch/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/fetch/lib.rs')
-rw-r--r--ext/fetch/lib.rs97
1 files changed, 35 insertions, 62 deletions
diff --git a/ext/fetch/lib.rs b/ext/fetch/lib.rs
index 20885dda5..4cd5d68c8 100644
--- a/ext/fetch/lib.rs
+++ b/ext/fetch/lib.rs
@@ -10,10 +10,8 @@ use deno_core::futures::stream::Peekable;
use deno_core::futures::Future;
use deno_core::futures::Stream;
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;
@@ -24,7 +22,6 @@ use deno_core::CancelFuture;
use deno_core::CancelHandle;
use deno_core::CancelTryFuture;
use deno_core::Canceled;
-use deno_core::Extension;
use deno_core::OpState;
use deno_core::RcRef;
use deno_core::Resource;
@@ -93,65 +90,41 @@ impl Default for Options {
}
}
-fn ext() -> ExtensionBuilder {
- Extension::builder_with_deps(
- env!("CARGO_PKG_NAME"),
- &["deno_webidl", "deno_web", "deno_url", "deno_console"],
- )
-}
-
-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).build()
-}
+deno_core::extension!(deno_fetch,
+ deps = [ deno_webidl, deno_web, deno_url, deno_console ],
+ parameters = [FP: FetchPermissions],
+ ops = [
+ op_fetch<FP>,
+ op_fetch_send,
+ op_fetch_custom_client<FP>,
+ ],
+ esm = [
+ "20_headers.js",
+ "21_formdata.js",
+ "22_body.js",
+ "22_http_client.js",
+ "23_request.js",
+ "23_response.js",
+ "26_fetch.js"
+ ],
+ config = {
+ options: Options,
+ },
+ state = |state, options| {
+ state.put::<Options>(options.clone());
+ state.put::<reqwest::Client>({
+ create_http_client(
+ options.user_agent,
+ options.root_cert_store,
+ vec![],
+ options.proxy,
+ options.unsafely_ignore_certificate_errors,
+ options.client_cert_chain_and_key
+ )
+ .unwrap()
+ });
+ },
+);
pub type CancelableResponseFuture =
Pin<Box<dyn Future<Output = CancelableResponseResult>>>;