refactor: remove deno_fetch::HttpClientDefaults
More clean up that should have been in
@@ -57,6 +57,7 @@ pub use reqwest;
pub use fs_fetch_handler::FsFetchHandler;
+#[derive(Clone)]
pub struct Options {
pub user_agent: String,
pub root_cert_store: Option<RootCertStore>,
@@ -67,8 +68,6 @@ pub struct Options {
pub file_fetch_handler: Box<dyn FetchHandler>,
}
-struct BoxFetchHandler(Box<dyn FetchHandler>);
-
impl Default for Options {
fn default() -> Self {
Self {
@@ -108,6 +107,7 @@ where
),
])
.state(move |state| {
+ state.put::<Options>(options.clone());
state.put::<reqwest::Client>({
create_http_client(
options.user_agent.clone(),
@@ -119,33 +119,11 @@ where
)
.unwrap()
});
- state.put::<HttpClientDefaults>(HttpClientDefaults {
- 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: options.client_cert_chain_and_key.clone(),
- });
- state.put(BoxFetchHandler(dyn_clone::clone_box(
- &*options.file_fetch_handler,
- )));
Ok(())
})
.build()
}
-pub struct HttpClientDefaults {
- 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 type CancelableResponseFuture =
Pin<Box<dyn Future<Output = CancelableResponseResult>>>;
@@ -251,8 +229,9 @@ where
)));
}
- let BoxFetchHandler(file_fetch_handler) =
- state.borrow_mut::<BoxFetchHandler>();
+ let Options {
+ file_fetch_handler, ..
+ } = state.borrow_mut::<Options>();
let (request, maybe_request_body, maybe_cancel_handle) =
file_fetch_handler.fetch_file(url);
let request_rid = state.resource_table.add(FetchRequestResource(request));
@@ -317,8 +296,8 @@ where
}
}
- let defaults = state.borrow::<HttpClientDefaults>();
- if let Some(request_builder_hook) = defaults.request_builder_hook {
+ let options = state.borrow::<Options>();
+ if let Some(request_builder_hook) = options.request_builder_hook {
request = request_builder_hook(request);
}
@@ -575,7 +554,7 @@ where
}
};
- let defaults = state.borrow::<HttpClientDefaults>();
+ let options = state.borrow::<Options>();
let ca_certs = args
.ca_certs
.into_iter()
@@ -583,11 +562,11 @@ where
.collect::<Vec<_>>();
let client = create_http_client(
- defaults.user_agent.clone(),
- defaults.root_cert_store.clone(),
+ options.user_agent.clone(),
+ options.root_cert_store.clone(),
ca_certs,
args.proxy,
- defaults.unsafely_ignore_certificate_errors.clone(),
+ options.unsafely_ignore_certificate_errors.clone(),
client_cert_chain_and_key,
)?;
