diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2021-11-28 13:07:03 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-28 13:07:03 -0500 |
commit | cc83ad39ce3e8186c8785d47fa4801317c8fac4a (patch) | |
tree | 7910684a15a0c33992c311b4fb94f9b323f3a7ee /ext/fetch/lib.rs | |
parent | 96d02deed626f391be0df30b9b08b7ba141db388 (diff) |
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.
Diffstat (limited to 'ext/fetch/lib.rs')
-rw-r--r-- | ext/fetch/lib.rs | 76 |
1 files changed, 49 insertions, 27 deletions
diff --git a/ext/fetch/lib.rs b/ext/fetch/lib.rs index 5bae92c8e..3d4c51f8f 100644 --- a/ext/fetch/lib.rs +++ b/ext/fetch/lib.rs @@ -57,18 +57,35 @@ pub use reqwest; pub use fs_fetch_handler::FsFetchHandler; -pub fn init<FP, FH>( - user_agent: String, - root_cert_store: Option<RootCertStore>, - proxy: Option<Proxy>, - request_builder_hook: Option<fn(RequestBuilder) -> RequestBuilder>, - unsafely_ignore_certificate_errors: Option<Vec<String>>, - client_cert_chain_and_key: Option<(String, String)>, - file_fetch_handler: FH, -) -> Extension +pub struct Options { + pub user_agent: String, + pub root_cert_store: Option<RootCertStore>, + pub proxy: Option<Proxy>, + pub request_builder_hook: Option<fn(RequestBuilder) -> RequestBuilder>, + pub unsafely_ignore_certificate_errors: Option<Vec<String>>, + pub client_cert_chain_and_key: Option<(String, String)>, + pub file_fetch_handler: Box<dyn FetchHandler>, +} + +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 FP: FetchPermissions + 'static, - FH: FetchHandler + 'static, { Extension::builder() .js(include_js_files!( @@ -83,7 +100,7 @@ where "26_fetch.js", )) .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_custom_client", @@ -93,25 +110,28 @@ where .state(move |state| { state.put::<reqwest::Client>({ create_http_client( - user_agent.clone(), - root_cert_store.clone(), + options.user_agent.clone(), + options.root_cert_store.clone(), vec![], - proxy.clone(), - unsafely_ignore_certificate_errors.clone(), - client_cert_chain_and_key.clone(), + options.proxy.clone(), + options.unsafely_ignore_certificate_errors.clone(), + options.client_cert_chain_and_key.clone(), ) .unwrap() }); state.put::<HttpClientDefaults>(HttpClientDefaults { - user_agent: user_agent.clone(), - root_cert_store: root_cert_store.clone(), - proxy: proxy.clone(), - request_builder_hook, - unsafely_ignore_certificate_errors: unsafely_ignore_certificate_errors + user_agent: options.user_agent.clone(), + root_cert_store: options.root_cert_store.clone(), + proxy: options.proxy.clone(), + request_builder_hook: options.request_builder_hook, + unsafely_ignore_certificate_errors: options + .unsafely_ignore_certificate_errors .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(()) }) .build() @@ -129,7 +149,7 @@ pub struct HttpClientDefaults { pub type CancelableResponseFuture = 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 // cancelable response result, the optional fetch body resource and the // 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. #[derive(Clone)] pub struct DefaultFileFetchHandler; @@ -193,14 +215,13 @@ pub struct FetchReturn { cancel_handle_rid: Option<ResourceId>, } -pub fn op_fetch<FP, FH>( +pub fn op_fetch<FP>( state: &mut OpState, args: FetchArgs, data: Option<ZeroCopyBuf>, ) -> Result<FetchReturn, AnyError> where FP: FetchPermissions + 'static, - FH: FetchHandler + 'static, { let client = if let Some(rid) = args.client_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) = file_fetch_handler.fetch_file(url); let request_rid = state.resource_table.add(FetchRequestResource(request)); |