diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2024-07-18 00:37:31 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-18 01:37:31 +0200 |
commit | 7b33623b1d70803b43e511a58666a73dd0b2ed67 (patch) | |
tree | 2d900f2be67caebf6a886d6e06a340b095e636cc /runtime/ops | |
parent | f4b9d8586215fc07c28998e5d896fefa876139b7 (diff) |
Reland "refactor(fetch): reimplement fetch with hyper instead of reqwest" (#24593)
Originally landed in
https://github.com/denoland/deno/commit/f6fd6619e708a515831f707438368d81b0c9aa56.
Reverted in https://github.com/denoland/deno/pull/24574.
This reland contains a fix that sends "Accept: */*" header for calls made
from "FileFetcher". Absence of this header made downloading source code
from JSR broken. This is tested by ensuring this header is present in the
test server that servers JSR packages.
---------
Co-authored-by: Sean McArthur <sean@seanmonstar.com>
Diffstat (limited to 'runtime/ops')
-rw-r--r-- | runtime/ops/web_worker/sync_fetch.rs | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/runtime/ops/web_worker/sync_fetch.rs b/runtime/ops/web_worker/sync_fetch.rs index 37286ca62..cdb151a86 100644 --- a/runtime/ops/web_worker/sync_fetch.rs +++ b/runtime/ops/web_worker/sync_fetch.rs @@ -13,6 +13,7 @@ use deno_core::OpState; use deno_fetch::data_url::DataUrl; use deno_web::BlobStore; use deno_websocket::DomExceptionNetworkError; +use http_body_util::BodyExt; use hyper::body::Bytes; use serde::Deserialize; use serde::Serialize; @@ -78,10 +79,23 @@ pub fn op_worker_sync_fetch( let (body, mime_type, res_url) = match script_url.scheme() { "http" | "https" => { - let resp = - client.get(script_url).send().await?.error_for_status()?; - - let res_url = resp.url().to_string(); + let mut req = http::Request::new( + http_body_util::Empty::new() + .map_err(|never| match never {}) + .boxed(), + ); + *req.uri_mut() = script_url.as_str().parse()?; + + let resp = client.send(req).await?; + + if resp.status().is_client_error() + || resp.status().is_server_error() + { + return Err(type_error(format!( + "http status error: {}", + resp.status() + ))); + } // TODO(andreubotella) Properly run fetch's "extract a MIME type". let mime_type = resp @@ -93,9 +107,9 @@ pub fn op_worker_sync_fetch( // Always check the MIME type with HTTP(S). loose_mime_checks = false; - let body = resp.bytes().await?; + let body = resp.collect().await?.to_bytes(); - (body, mime_type, res_url) + (body, mime_type, script) } "data" => { let data_url = DataUrl::process(&script) |