Skip to content

Commit

Permalink
refactor(ext/fetch): pass opstate in FetchHandler::fetch_file
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats committed Dec 3, 2021
1 parent 1947f89 commit 46794d4
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
4 changes: 3 additions & 1 deletion ext/fetch/fs_fetch_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use deno_core::futures::FutureExt;
use deno_core::futures::TryFutureExt;
use deno_core::url::Url;
use deno_core::CancelFuture;
use deno_core::OpState;
use reqwest::StatusCode;
use std::rc::Rc;
use tokio_util::io::ReaderStream;
Expand All @@ -21,7 +22,8 @@ pub struct FsFetchHandler;

impl FetchHandler for FsFetchHandler {
fn fetch_file(
&mut self,
&self,
_state: &mut OpState,
url: Url,
) -> (
CancelableResponseFuture,
Expand Down
13 changes: 8 additions & 5 deletions ext/fetch/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub struct Options {
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>,
pub file_fetch_handler: Rc<dyn FetchHandler>,
}

impl Default for Options {
Expand All @@ -79,7 +79,7 @@ impl Default for Options {
request_builder_hook: None,
unsafely_ignore_certificate_errors: None,
client_cert_chain_and_key: None,
file_fetch_handler: Box::new(DefaultFileFetchHandler),
file_fetch_handler: Rc::new(DefaultFileFetchHandler),
}
}
}
Expand Down Expand Up @@ -134,7 +134,8 @@ pub trait FetchHandler: dyn_clone::DynClone {
// cancelable response result, the optional fetch body resource and the
// optional cancel handle.
fn fetch_file(
&mut self,
&self,
state: &mut OpState,
url: Url,
) -> (
CancelableResponseFuture,
Expand All @@ -151,7 +152,8 @@ pub struct DefaultFileFetchHandler;

impl FetchHandler for DefaultFileFetchHandler {
fn fetch_file(
&mut self,
&self,
_state: &mut OpState,
_url: Url,
) -> (
CancelableResponseFuture,
Expand Down Expand Up @@ -234,8 +236,9 @@ where
let Options {
file_fetch_handler, ..
} = state.borrow_mut::<Options>();
let file_fetch_handler = file_fetch_handler.clone();
let (request, maybe_request_body, maybe_cancel_handle) =
file_fetch_handler.fetch_file(url);
file_fetch_handler.fetch_file(state, url);
let request_rid = state.resource_table.add(FetchRequestResource(request));
let maybe_request_body_rid =
maybe_request_body.map(|r| state.resource_table.add(r));
Expand Down
2 changes: 1 addition & 1 deletion runtime/web_worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ impl WebWorker {
unsafely_ignore_certificate_errors: options
.unsafely_ignore_certificate_errors
.clone(),
file_fetch_handler: Box::new(deno_fetch::FsFetchHandler),
file_fetch_handler: Rc::new(deno_fetch::FsFetchHandler),
..Default::default()
}),
deno_websocket::init::<Permissions>(
Expand Down
2 changes: 1 addition & 1 deletion runtime/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl MainWorker {
unsafely_ignore_certificate_errors: options
.unsafely_ignore_certificate_errors
.clone(),
file_fetch_handler: Box::new(deno_fetch::FsFetchHandler),
file_fetch_handler: Rc::new(deno_fetch::FsFetchHandler),
..Default::default()
}),
deno_websocket::init::<Permissions>(
Expand Down

0 comments on commit 46794d4

Please sign in to comment.