1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-28 16:20:57 -05:00

refactor: add deno_fetch::Options for init (#12904)

deno_fetch::init has a lot of parameters and generic on two types 
that keeps expanding over time. This refactor adds deno_fetch::Options 
struct for more clearly defining the various parameters.
This commit is contained in:
Ryan Dahl 2021-11-28 13:07:03 -05:00 committed by GitHub
parent 96d02deed6
commit cc83ad39ce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 76 additions and 54 deletions

7
Cargo.lock generated
View file

@ -805,6 +805,7 @@ dependencies = [
"data-url", "data-url",
"deno_core", "deno_core",
"deno_tls", "deno_tls",
"dyn-clone",
"http", "http",
"reqwest", "reqwest",
"serde", "serde",
@ -1158,6 +1159,12 @@ dependencies = [
"text_lines", "text_lines",
] ]
[[package]]
name = "dyn-clone"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee2626afccd7561a06cf1367e2950c4718ea04565e20fb5029b6c7d8ad09abcf"
[[package]] [[package]]
name = "ecdsa" name = "ecdsa"
version = "0.12.4" version = "0.12.4"

View file

@ -18,6 +18,7 @@ bytes = "1.1.0"
data-url = "0.1.0" data-url = "0.1.0"
deno_core = { version = "0.109.0", path = "../../core" } deno_core = { version = "0.109.0", path = "../../core" }
deno_tls = { version = "0.14.0", path = "../tls" } deno_tls = { version = "0.14.0", path = "../tls" }
dyn-clone = "1"
http = "0.2.4" http = "0.2.4"
reqwest = { version = "0.11.4", default-features = false, features = ["rustls-tls", "stream", "gzip", "brotli"] } reqwest = { version = "0.11.4", default-features = false, features = ["rustls-tls", "stream", "gzip", "brotli"] }
serde = { version = "1.0.129", features = ["derive"] } serde = { version = "1.0.129", features = ["derive"] }

View file

@ -57,18 +57,35 @@ pub use reqwest;
pub use fs_fetch_handler::FsFetchHandler; pub use fs_fetch_handler::FsFetchHandler;
pub fn init<FP, FH>( pub struct Options {
user_agent: String, pub user_agent: String,
root_cert_store: Option<RootCertStore>, pub root_cert_store: Option<RootCertStore>,
proxy: Option<Proxy>, pub proxy: Option<Proxy>,
request_builder_hook: Option<fn(RequestBuilder) -> RequestBuilder>, pub request_builder_hook: Option<fn(RequestBuilder) -> RequestBuilder>,
unsafely_ignore_certificate_errors: Option<Vec<String>>, pub unsafely_ignore_certificate_errors: Option<Vec<String>>,
client_cert_chain_and_key: Option<(String, String)>, pub client_cert_chain_and_key: Option<(String, String)>,
file_fetch_handler: FH, pub file_fetch_handler: Box<dyn FetchHandler>,
) -> Extension }
struct BoxFetchHandler(Box<dyn FetchHandler>);
impl Default for Options {
fn default() -> Self {
Self {
user_agent: "".to_string(),
root_cert_store: None,
proxy: None,
request_builder_hook: None,
unsafely_ignore_certificate_errors: None,
client_cert_chain_and_key: None,
file_fetch_handler: Box::new(DefaultFileFetchHandler),
}
}
}
pub fn init<FP>(options: Options) -> Extension
where where
FP: FetchPermissions + 'static, FP: FetchPermissions + 'static,
FH: FetchHandler + 'static,
{ {
Extension::builder() Extension::builder()
.js(include_js_files!( .js(include_js_files!(
@ -83,7 +100,7 @@ where
"26_fetch.js", "26_fetch.js",
)) ))
.ops(vec![ .ops(vec![
("op_fetch", op_sync(op_fetch::<FP, FH>)), ("op_fetch", op_sync(op_fetch::<FP>)),
("op_fetch_send", op_async(op_fetch_send)), ("op_fetch_send", op_async(op_fetch_send)),
( (
"op_fetch_custom_client", "op_fetch_custom_client",
@ -93,25 +110,28 @@ where
.state(move |state| { .state(move |state| {
state.put::<reqwest::Client>({ state.put::<reqwest::Client>({
create_http_client( create_http_client(
user_agent.clone(), options.user_agent.clone(),
root_cert_store.clone(), options.root_cert_store.clone(),
vec![], vec![],
proxy.clone(), options.proxy.clone(),
unsafely_ignore_certificate_errors.clone(), options.unsafely_ignore_certificate_errors.clone(),
client_cert_chain_and_key.clone(), options.client_cert_chain_and_key.clone(),
) )
.unwrap() .unwrap()
}); });
state.put::<HttpClientDefaults>(HttpClientDefaults { state.put::<HttpClientDefaults>(HttpClientDefaults {
user_agent: user_agent.clone(), user_agent: options.user_agent.clone(),
root_cert_store: root_cert_store.clone(), root_cert_store: options.root_cert_store.clone(),
proxy: proxy.clone(), proxy: options.proxy.clone(),
request_builder_hook, request_builder_hook: options.request_builder_hook,
unsafely_ignore_certificate_errors: unsafely_ignore_certificate_errors unsafely_ignore_certificate_errors: options
.unsafely_ignore_certificate_errors
.clone(), .clone(),
client_cert_chain_and_key: client_cert_chain_and_key.clone(), client_cert_chain_and_key: options.client_cert_chain_and_key.clone(),
}); });
state.put::<FH>(file_fetch_handler.clone()); state.put(BoxFetchHandler(dyn_clone::clone_box(
&*options.file_fetch_handler,
)));
Ok(()) Ok(())
}) })
.build() .build()
@ -129,7 +149,7 @@ pub struct HttpClientDefaults {
pub type CancelableResponseFuture = pub type CancelableResponseFuture =
Pin<Box<dyn Future<Output = CancelableResponseResult>>>; Pin<Box<dyn Future<Output = CancelableResponseResult>>>;
pub trait FetchHandler: Clone { pub trait FetchHandler: dyn_clone::DynClone {
// Return the result of the fetch request consisting of a tuple of the // Return the result of the fetch request consisting of a tuple of the
// cancelable response result, the optional fetch body resource and the // cancelable response result, the optional fetch body resource and the
// optional cancel handle. // optional cancel handle.
@ -143,6 +163,8 @@ pub trait FetchHandler: Clone {
); );
} }
dyn_clone::clone_trait_object!(FetchHandler);
/// A default implementation which will error for every request. /// A default implementation which will error for every request.
#[derive(Clone)] #[derive(Clone)]
pub struct DefaultFileFetchHandler; pub struct DefaultFileFetchHandler;
@ -193,14 +215,13 @@ pub struct FetchReturn {
cancel_handle_rid: Option<ResourceId>, cancel_handle_rid: Option<ResourceId>,
} }
pub fn op_fetch<FP, FH>( pub fn op_fetch<FP>(
state: &mut OpState, state: &mut OpState,
args: FetchArgs, args: FetchArgs,
data: Option<ZeroCopyBuf>, data: Option<ZeroCopyBuf>,
) -> Result<FetchReturn, AnyError> ) -> Result<FetchReturn, AnyError>
where where
FP: FetchPermissions + 'static, FP: FetchPermissions + 'static,
FH: FetchHandler + 'static,
{ {
let client = if let Some(rid) = args.client_rid { let client = if let Some(rid) = args.client_rid {
let r = state.resource_table.get::<HttpClientResource>(rid)?; let r = state.resource_table.get::<HttpClientResource>(rid)?;
@ -230,7 +251,8 @@ where
))); )));
} }
let file_fetch_handler = state.borrow_mut::<FH>(); let BoxFetchHandler(file_fetch_handler) =
state.borrow_mut::<BoxFetchHandler>();
let (request, maybe_request_body, maybe_cancel_handle) = let (request, maybe_request_body, maybe_cancel_handle) =
file_fetch_handler.fetch_file(url); file_fetch_handler.fetch_file(url);
let request_rid = state.resource_table.add(FetchRequestResource(request)); let request_rid = state.resource_table.add(FetchRequestResource(request));

View file

@ -121,15 +121,7 @@ mod not_docs {
deno_url::init(), deno_url::init(),
deno_tls::init(), deno_tls::init(),
deno_web::init(deno_web::BlobStore::default(), Default::default()), deno_web::init(deno_web::BlobStore::default(), Default::default()),
deno_fetch::init::<Permissions, deno_fetch::DefaultFileFetchHandler>( deno_fetch::init::<Permissions>(Default::default()),
"".to_owned(),
None,
None,
None,
None,
None,
deno_fetch::DefaultFileFetchHandler, // No enable_file_fetch
),
deno_websocket::init::<Permissions>("".to_owned(), None, None), deno_websocket::init::<Permissions>("".to_owned(), None, None),
deno_webstorage::init(None), deno_webstorage::init(None),
deno_crypto::init(None), deno_crypto::init(None),

View file

@ -318,15 +318,15 @@ impl WebWorker {
deno_console::init(), deno_console::init(),
deno_url::init(), deno_url::init(),
deno_web::init(options.blob_store.clone(), Some(main_module.clone())), deno_web::init(options.blob_store.clone(), Some(main_module.clone())),
deno_fetch::init::<Permissions, deno_fetch::FsFetchHandler>( deno_fetch::init::<Permissions>(deno_fetch::Options {
options.user_agent.clone(), user_agent: options.user_agent.clone(),
options.root_cert_store.clone(), root_cert_store: options.root_cert_store.clone(),
None, unsafely_ignore_certificate_errors: options
None, .unsafely_ignore_certificate_errors
options.unsafely_ignore_certificate_errors.clone(), .clone(),
None, file_fetch_handler: Box::new(deno_fetch::FsFetchHandler),
deno_fetch::FsFetchHandler, ..Default::default()
), }),
deno_websocket::init::<Permissions>( deno_websocket::init::<Permissions>(
options.user_agent.clone(), options.user_agent.clone(),
options.root_cert_store.clone(), options.root_cert_store.clone(),

View file

@ -101,15 +101,15 @@ impl MainWorker {
options.blob_store.clone(), options.blob_store.clone(),
options.bootstrap.location.clone(), options.bootstrap.location.clone(),
), ),
deno_fetch::init::<Permissions, deno_fetch::FsFetchHandler>( deno_fetch::init::<Permissions>(deno_fetch::Options {
options.user_agent.clone(), user_agent: options.user_agent.clone(),
options.root_cert_store.clone(), root_cert_store: options.root_cert_store.clone(),
None, unsafely_ignore_certificate_errors: options
None, .unsafely_ignore_certificate_errors
options.unsafely_ignore_certificate_errors.clone(), .clone(),
None, file_fetch_handler: Box::new(deno_fetch::FsFetchHandler),
deno_fetch::FsFetchHandler, ..Default::default()
), }),
deno_websocket::init::<Permissions>( deno_websocket::init::<Permissions>(
options.user_agent.clone(), options.user_agent.clone(),
options.root_cert_store.clone(), options.root_cert_store.clone(),