diff options
author | Aaron O'Mullan <aaron.omullan@gmail.com> | 2021-04-28 18:41:50 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-28 18:41:50 +0200 |
commit | 0260b488fbba9a43c64641428d3603b8761067a4 (patch) | |
tree | 66ce487f9241a3b91942dd048c7e43cb192bf9e8 /op_crates/fetch/lib.rs | |
parent | b28f9445aae85dbf86033300cfcb55e404529a23 (diff) |
core: introduce extensions (#9800)
Extensions allow declarative extensions to "JsRuntime" (ops, state, JS or middleware).
This allows for:
- `op_crates` to be plug-and-play & self-contained, reducing complexity leaked to consumers
- op middleware (like metrics_op) to be opt-in and for new middleware (unstable, tracing,...)
- `MainWorker` and `WebWorker` to be composable, allowing users to extend workers with their ops whilst benefiting from the other infrastructure (inspector, etc...)
In short extensions improve deno's modularity, reducing complexity and leaky abstractions for embedders and the internal codebase.
Diffstat (limited to 'op_crates/fetch/lib.rs')
-rw-r--r-- | op_crates/fetch/lib.rs | 81 |
1 files changed, 38 insertions, 43 deletions
diff --git a/op_crates/fetch/lib.rs b/op_crates/fetch/lib.rs index ad96dea46..7033a416c 100644 --- a/op_crates/fetch/lib.rs +++ b/op_crates/fetch/lib.rs @@ -8,12 +8,15 @@ use deno_core::error::AnyError; 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_async; +use deno_core::op_sync; use deno_core::url::Url; use deno_core::AsyncRefCell; use deno_core::CancelFuture; use deno_core::CancelHandle; use deno_core::CancelTryFuture; -use deno_core::JsRuntime; +use deno_core::Extension; use deno_core::OpState; use deno_core::RcRef; use deno_core::Resource; @@ -49,49 +52,41 @@ use tokio_util::io::StreamReader; pub use reqwest; // Re-export reqwest -/// Execute this crates' JS source files. -pub fn init(isolate: &mut JsRuntime) { - let files = vec![ - ( - "deno:op_crates/fetch/01_fetch_util.js", - include_str!("01_fetch_util.js"), - ), - ( - "deno:op_crates/fetch/11_streams.js", - include_str!("11_streams.js"), - ), - ( - "deno:op_crates/fetch/20_headers.js", - include_str!("20_headers.js"), - ), - ( - "deno:op_crates/fetch/21_formdata.js", - include_str!("21_formdata.js"), - ), - ( - "deno:op_crates/fetch/22_body.js", - include_str!("22_body.js"), - ), - ( - "deno:op_crates/fetch/22_http_client.js", - include_str!("22_http_client.js"), - ), - ( - "deno:op_crates/fetch/23_request.js", - include_str!("23_request.js"), - ), - ( - "deno:op_crates/fetch/23_response.js", - include_str!("23_response.js"), - ), - ( - "deno:op_crates/fetch/26_fetch.js", - include_str!("26_fetch.js"), +pub fn init<P: FetchPermissions + 'static>( + user_agent: String, + ca_data: Option<Vec<u8>>, +) -> Extension { + Extension::with_ops( + include_js_files!( + prefix "deno:op_crates/fetch", + "01_fetch_util.js", + "11_streams.js", + "20_headers.js", + "21_formdata.js", + "22_body.js", + "22_http_client.js", + "23_request.js", + "23_response.js", + "26_fetch.js", ), - ]; - for (url, source_code) in files { - isolate.execute(url, source_code).expect(url); - } + vec![ + ("op_fetch", op_sync(op_fetch::<P>)), + ("op_fetch_send", op_async(op_fetch_send)), + ("op_fetch_request_write", op_async(op_fetch_request_write)), + ("op_fetch_response_read", op_async(op_fetch_response_read)), + ("op_create_http_client", op_sync(op_create_http_client::<P>)), + ], + Some(Box::new(move |state| { + state.put::<reqwest::Client>({ + create_http_client(user_agent.clone(), ca_data.clone()).unwrap() + }); + state.put::<HttpClientDefaults>(HttpClientDefaults { + ca_data: ca_data.clone(), + user_agent: user_agent.clone(), + }); + Ok(()) + })), + ) } pub struct HttpClientDefaults { |