2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-08-16 08:29:54 -04:00
|
|
|
|
2023-07-01 18:52:30 -04:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2021-08-16 08:29:54 -04:00
|
|
|
use crate::web_worker::WebWorkerInternalHandle;
|
|
|
|
use crate::web_worker::WebWorkerType;
|
2024-05-05 10:07:21 -04:00
|
|
|
use deno_core::futures::StreamExt;
|
2023-09-21 10:08:23 -04:00
|
|
|
use deno_core::op2;
|
2021-08-16 08:29:54 -04:00
|
|
|
use deno_core::url::Url;
|
|
|
|
use deno_core::OpState;
|
|
|
|
use deno_fetch::data_url::DataUrl;
|
2024-10-22 04:41:08 -04:00
|
|
|
use deno_fetch::FetchError;
|
2021-08-16 08:29:54 -04:00
|
|
|
use deno_web::BlobStore;
|
2024-07-17 19:37:31 -04:00
|
|
|
use http_body_util::BodyExt;
|
2021-08-16 08:29:54 -04:00
|
|
|
use hyper::body::Bytes;
|
2023-01-14 23:18:58 -05:00
|
|
|
use serde::Deserialize;
|
|
|
|
use serde::Serialize;
|
2021-08-16 08:29:54 -04:00
|
|
|
|
|
|
|
// TODO(andreubotella) Properly parse the MIME type
|
|
|
|
fn mime_type_essence(mime_type: &str) -> String {
|
2022-02-24 20:03:12 -05:00
|
|
|
let essence = match mime_type.split_once(';') {
|
2021-08-16 08:29:54 -04:00
|
|
|
Some((essence, _)) => essence,
|
|
|
|
None => mime_type,
|
|
|
|
};
|
|
|
|
essence.trim().to_ascii_lowercase()
|
|
|
|
}
|
|
|
|
|
2024-10-22 04:41:08 -04:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
pub enum SyncFetchError {
|
|
|
|
#[error("Blob URLs are not supported in this context.")]
|
|
|
|
BlobUrlsNotSupportedInContext,
|
|
|
|
#[error("{0}")]
|
|
|
|
Io(#[from] std::io::Error),
|
|
|
|
#[error("Invalid script URL")]
|
|
|
|
InvalidScriptUrl,
|
|
|
|
#[error("http status error: {0}")]
|
|
|
|
InvalidStatusCode(http::StatusCode),
|
|
|
|
#[error("Classic scripts with scheme {0}: are not supported in workers")]
|
|
|
|
ClassicScriptSchemeUnsupportedInWorkers(String),
|
|
|
|
#[error("{0}")]
|
|
|
|
InvalidUri(#[from] http::uri::InvalidUri),
|
|
|
|
#[error("Invalid MIME type {0:?}.")]
|
|
|
|
InvalidMimeType(String),
|
|
|
|
#[error("Missing MIME type.")]
|
|
|
|
MissingMimeType,
|
|
|
|
#[error(transparent)]
|
|
|
|
Fetch(#[from] FetchError),
|
|
|
|
#[error(transparent)]
|
|
|
|
Join(#[from] tokio::task::JoinError),
|
|
|
|
#[error(transparent)]
|
|
|
|
Other(deno_core::error::AnyError),
|
|
|
|
}
|
|
|
|
|
2021-08-16 08:29:54 -04:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct SyncFetchScript {
|
|
|
|
url: String,
|
|
|
|
script: String,
|
|
|
|
}
|
|
|
|
|
2023-09-21 10:08:23 -04:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2021-08-16 08:29:54 -04:00
|
|
|
pub fn op_worker_sync_fetch(
|
|
|
|
state: &mut OpState,
|
2023-09-21 10:08:23 -04:00
|
|
|
#[serde] scripts: Vec<String>,
|
2024-05-05 10:07:21 -04:00
|
|
|
loose_mime_checks: bool,
|
2024-10-22 04:41:08 -04:00
|
|
|
) -> Result<Vec<SyncFetchScript>, SyncFetchError> {
|
2021-08-16 08:29:54 -04:00
|
|
|
let handle = state.borrow::<WebWorkerInternalHandle>().clone();
|
|
|
|
assert_eq!(handle.worker_type, WebWorkerType::Classic);
|
|
|
|
|
2024-05-05 10:07:21 -04:00
|
|
|
// it's not safe to share a client across tokio runtimes, so create a fresh one
|
|
|
|
// https://github.com/seanmonstar/reqwest/issues/1148#issuecomment-910868788
|
|
|
|
let options = state.borrow::<deno_fetch::Options>().clone();
|
2024-10-22 04:41:08 -04:00
|
|
|
let client = deno_fetch::create_client_from_options(&options)
|
|
|
|
.map_err(FetchError::ClientCreate)?;
|
2021-10-27 09:09:58 -04:00
|
|
|
|
2021-08-16 08:29:54 -04:00
|
|
|
// TODO(andreubotella) It's not good to throw an exception related to blob
|
|
|
|
// URLs when none of the script URLs use the blob scheme.
|
|
|
|
// Also, in which contexts are blob URLs not supported?
|
2021-08-18 09:19:22 -04:00
|
|
|
let blob_store = state
|
2023-07-01 18:52:30 -04:00
|
|
|
.try_borrow::<Arc<BlobStore>>()
|
2024-10-22 04:41:08 -04:00
|
|
|
.ok_or(SyncFetchError::BlobUrlsNotSupportedInContext)?
|
2021-08-18 09:19:22 -04:00
|
|
|
.clone();
|
|
|
|
|
|
|
|
// TODO(andreubotella): make the below thread into a resource that can be
|
2023-06-26 09:10:27 -04:00
|
|
|
// re-used. This would allow parallel fetching of multiple scripts.
|
2021-08-18 09:19:22 -04:00
|
|
|
|
|
|
|
let thread = std::thread::spawn(move || {
|
|
|
|
let runtime = tokio::runtime::Builder::new_current_thread()
|
|
|
|
.enable_io()
|
|
|
|
.enable_time()
|
2023-01-14 23:06:46 -05:00
|
|
|
.build()?;
|
2021-08-18 09:19:22 -04:00
|
|
|
|
2024-05-05 10:07:21 -04:00
|
|
|
runtime.block_on(async move {
|
|
|
|
let mut futures = scripts
|
|
|
|
.into_iter()
|
|
|
|
.map(|script| {
|
|
|
|
let client = client.clone();
|
|
|
|
let blob_store = blob_store.clone();
|
|
|
|
deno_core::unsync::spawn(async move {
|
|
|
|
let script_url = Url::parse(&script)
|
2024-10-22 04:41:08 -04:00
|
|
|
.map_err(|_| SyncFetchError::InvalidScriptUrl)?;
|
2024-05-05 10:07:21 -04:00
|
|
|
let mut loose_mime_checks = loose_mime_checks;
|
|
|
|
|
|
|
|
let (body, mime_type, res_url) = match script_url.scheme() {
|
|
|
|
"http" | "https" => {
|
2024-07-17 19:37:31 -04:00
|
|
|
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()?;
|
|
|
|
|
2024-10-22 04:41:08 -04:00
|
|
|
let resp =
|
|
|
|
client.send(req).await.map_err(FetchError::ClientSend)?;
|
2024-07-17 19:37:31 -04:00
|
|
|
|
|
|
|
if resp.status().is_client_error()
|
|
|
|
|| resp.status().is_server_error()
|
|
|
|
{
|
2024-10-22 04:41:08 -04:00
|
|
|
return Err(SyncFetchError::InvalidStatusCode(resp.status()));
|
2024-07-17 19:37:31 -04:00
|
|
|
}
|
2024-05-05 10:07:21 -04:00
|
|
|
|
|
|
|
// TODO(andreubotella) Properly run fetch's "extract a MIME type".
|
|
|
|
let mime_type = resp
|
|
|
|
.headers()
|
|
|
|
.get("Content-Type")
|
|
|
|
.and_then(|v| v.to_str().ok())
|
|
|
|
.map(mime_type_essence);
|
|
|
|
|
|
|
|
// Always check the MIME type with HTTP(S).
|
|
|
|
loose_mime_checks = false;
|
|
|
|
|
2024-10-22 04:41:08 -04:00
|
|
|
let body = resp
|
|
|
|
.collect()
|
|
|
|
.await
|
|
|
|
.map_err(SyncFetchError::Other)?
|
|
|
|
.to_bytes();
|
2024-05-05 10:07:21 -04:00
|
|
|
|
2024-07-17 19:37:31 -04:00
|
|
|
(body, mime_type, script)
|
2024-05-05 10:07:21 -04:00
|
|
|
}
|
|
|
|
"data" => {
|
2024-10-22 04:41:08 -04:00
|
|
|
let data_url =
|
|
|
|
DataUrl::process(&script).map_err(FetchError::DataUrl)?;
|
2021-08-16 08:29:54 -04:00
|
|
|
|
2024-05-05 10:07:21 -04:00
|
|
|
let mime_type = {
|
|
|
|
let mime = data_url.mime_type();
|
|
|
|
format!("{}/{}", mime.type_, mime.subtype)
|
|
|
|
};
|
2021-08-16 08:29:54 -04:00
|
|
|
|
2024-10-22 04:41:08 -04:00
|
|
|
let (body, _) =
|
|
|
|
data_url.decode_to_vec().map_err(FetchError::Base64)?;
|
2021-08-16 08:29:54 -04:00
|
|
|
|
2024-05-05 10:07:21 -04:00
|
|
|
(Bytes::from(body), Some(mime_type), script)
|
|
|
|
}
|
|
|
|
"blob" => {
|
2024-10-22 04:41:08 -04:00
|
|
|
let blob = blob_store
|
|
|
|
.get_object_url(script_url)
|
|
|
|
.ok_or(FetchError::BlobNotFound)?;
|
2021-08-16 08:29:54 -04:00
|
|
|
|
2024-05-05 10:07:21 -04:00
|
|
|
let mime_type = mime_type_essence(&blob.media_type);
|
2021-08-16 08:29:54 -04:00
|
|
|
|
2024-10-17 15:05:38 -04:00
|
|
|
let body = blob.read_all().await;
|
2021-08-16 08:29:54 -04:00
|
|
|
|
2024-05-05 10:07:21 -04:00
|
|
|
(Bytes::from(body), Some(mime_type), script)
|
|
|
|
}
|
|
|
|
_ => {
|
2024-10-22 04:41:08 -04:00
|
|
|
return Err(
|
|
|
|
SyncFetchError::ClassicScriptSchemeUnsupportedInWorkers(
|
|
|
|
script_url.scheme().to_string(),
|
|
|
|
),
|
|
|
|
)
|
2021-08-18 09:19:22 -04:00
|
|
|
}
|
2024-05-05 10:07:21 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
if !loose_mime_checks {
|
|
|
|
// TODO(andreubotella) Check properly for a Javascript MIME type.
|
|
|
|
match mime_type.as_deref() {
|
|
|
|
Some("application/javascript" | "text/javascript") => {}
|
|
|
|
Some(mime_type) => {
|
2024-10-22 04:41:08 -04:00
|
|
|
return Err(SyncFetchError::InvalidMimeType(
|
|
|
|
mime_type.to_string(),
|
2024-10-18 15:30:46 -04:00
|
|
|
))
|
2024-05-05 10:07:21 -04:00
|
|
|
}
|
2024-10-22 04:41:08 -04:00
|
|
|
None => return Err(SyncFetchError::MissingMimeType),
|
2021-08-18 09:19:22 -04:00
|
|
|
}
|
2021-08-16 08:29:54 -04:00
|
|
|
}
|
|
|
|
|
2024-05-05 10:07:21 -04:00
|
|
|
let (text, _) = encoding_rs::UTF_8.decode_with_bom_removal(&body);
|
2021-08-16 08:29:54 -04:00
|
|
|
|
2024-05-05 10:07:21 -04:00
|
|
|
Ok(SyncFetchScript {
|
|
|
|
url: res_url,
|
|
|
|
script: text.into_owned(),
|
|
|
|
})
|
2021-08-18 09:19:22 -04:00
|
|
|
})
|
2021-08-16 08:29:54 -04:00
|
|
|
})
|
2024-05-05 10:07:21 -04:00
|
|
|
.collect::<deno_core::futures::stream::FuturesUnordered<_>>();
|
|
|
|
let mut ret = Vec::with_capacity(futures.len());
|
|
|
|
while let Some(result) = futures.next().await {
|
|
|
|
let script = result??;
|
|
|
|
ret.push(script);
|
|
|
|
}
|
|
|
|
Ok(ret)
|
|
|
|
})
|
2021-08-18 09:19:22 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
thread.join().unwrap()
|
2021-08-16 08:29:54 -04:00
|
|
|
}
|