From 0260b488fbba9a43c64641428d3603b8761067a4 Mon Sep 17 00:00:00 2001 From: Aaron O'Mullan Date: Wed, 28 Apr 2021 18:41:50 +0200 Subject: 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. --- op_crates/fetch/lib.rs | 81 +++++++++++++++++++++++--------------------------- 1 file changed, 38 insertions(+), 43 deletions(-) (limited to 'op_crates/fetch/lib.rs') 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( + user_agent: String, + ca_data: Option>, +) -> 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::

)), + ("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::

)), + ], + Some(Box::new(move |state| { + state.put::({ + create_http_client(user_agent.clone(), ca_data.clone()).unwrap() + }); + state.put::(HttpClientDefaults { + ca_data: ca_data.clone(), + user_agent: user_agent.clone(), + }); + Ok(()) + })), + ) } pub struct HttpClientDefaults { -- cgit v1.2.3