2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-08-16 08:29:54 -04:00
|
|
|
|
|
|
|
use crate::web_worker::WebWorkerInternalHandle;
|
|
|
|
use crate::web_worker::WebWorkerType;
|
|
|
|
use deno_core::error::type_error;
|
|
|
|
use deno_core::error::AnyError;
|
2022-03-14 13:44:15 -04:00
|
|
|
use deno_core::op;
|
2021-08-16 08:29:54 -04:00
|
|
|
use deno_core::url::Url;
|
|
|
|
use deno_core::OpState;
|
|
|
|
use deno_fetch::data_url::DataUrl;
|
|
|
|
use deno_fetch::reqwest;
|
|
|
|
use deno_web::BlobStore;
|
|
|
|
use deno_websocket::DomExceptionNetworkError;
|
|
|
|
use hyper::body::Bytes;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use tokio::task::JoinHandle;
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct SyncFetchScript {
|
|
|
|
url: String,
|
|
|
|
script: String,
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-08-16 08:29:54 -04:00
|
|
|
pub fn op_worker_sync_fetch(
|
|
|
|
state: &mut OpState,
|
|
|
|
scripts: Vec<String>,
|
|
|
|
mut loose_mime_checks: bool,
|
|
|
|
) -> Result<Vec<SyncFetchScript>, AnyError> {
|
|
|
|
let handle = state.borrow::<WebWorkerInternalHandle>().clone();
|
|
|
|
assert_eq!(handle.worker_type, WebWorkerType::Classic);
|
|
|
|
|
2021-10-27 09:09:58 -04:00
|
|
|
let client = state.borrow::<reqwest::Client>().clone();
|
|
|
|
|
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
|
|
|
|
.try_borrow::<BlobStore>()
|
|
|
|
.ok_or_else(|| type_error("Blob URLs are not supported in this context."))?
|
|
|
|
.clone();
|
|
|
|
|
|
|
|
// TODO(andreubotella): make the below thread into a resource that can be
|
|
|
|
// re-used. This would allow parallel fecthing of multiple scripts.
|
|
|
|
|
|
|
|
let thread = std::thread::spawn(move || {
|
|
|
|
let runtime = tokio::runtime::Builder::new_current_thread()
|
|
|
|
.enable_io()
|
|
|
|
.enable_time()
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let handles: Vec<_> = scripts
|
|
|
|
.into_iter()
|
|
|
|
.map(|script| -> JoinHandle<Result<SyncFetchScript, AnyError>> {
|
2021-10-27 09:09:58 -04:00
|
|
|
let client = client.clone();
|
2021-08-18 09:19:22 -04:00
|
|
|
let blob_store = blob_store.clone();
|
|
|
|
runtime.spawn(async move {
|
|
|
|
let script_url = Url::parse(&script)
|
|
|
|
.map_err(|_| type_error("Invalid script URL"))?;
|
|
|
|
|
|
|
|
let (body, mime_type, res_url) = match script_url.scheme() {
|
|
|
|
"http" | "https" => {
|
2021-10-27 09:09:58 -04:00
|
|
|
let resp =
|
|
|
|
client.get(script_url).send().await?.error_for_status()?;
|
2021-08-18 09:19:22 -04:00
|
|
|
|
|
|
|
let res_url = resp.url().to_string();
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
let body = resp.bytes().await?;
|
|
|
|
|
|
|
|
(body, mime_type, res_url)
|
|
|
|
}
|
|
|
|
"data" => {
|
|
|
|
let data_url = DataUrl::process(&script)
|
|
|
|
.map_err(|e| type_error(format!("{:?}", e)))?;
|
2021-08-16 08:29:54 -04:00
|
|
|
|
2021-08-18 09:19:22 -04:00
|
|
|
let mime_type = {
|
|
|
|
let mime = data_url.mime_type();
|
|
|
|
format!("{}/{}", mime.type_, mime.subtype)
|
|
|
|
};
|
2021-08-16 08:29:54 -04:00
|
|
|
|
2021-08-18 09:19:22 -04:00
|
|
|
let (body, _) = data_url
|
|
|
|
.decode_to_vec()
|
|
|
|
.map_err(|e| type_error(format!("{:?}", e)))?;
|
2021-08-16 08:29:54 -04:00
|
|
|
|
2021-08-18 09:19:22 -04:00
|
|
|
(Bytes::from(body), Some(mime_type), script)
|
|
|
|
}
|
|
|
|
"blob" => {
|
|
|
|
let blob =
|
|
|
|
blob_store.get_object_url(script_url)?.ok_or_else(|| {
|
|
|
|
type_error("Blob for the given URL not found.")
|
|
|
|
})?;
|
2021-08-16 08:29:54 -04:00
|
|
|
|
2021-08-18 09:19:22 -04:00
|
|
|
let mime_type = mime_type_essence(&blob.media_type);
|
2021-08-16 08:29:54 -04:00
|
|
|
|
2021-08-18 09:19:22 -04:00
|
|
|
let body = blob.read_all().await?;
|
2021-08-16 08:29:54 -04:00
|
|
|
|
2021-08-18 09:19:22 -04:00
|
|
|
(Bytes::from(body), Some(mime_type), script)
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
return Err(type_error(format!(
|
|
|
|
"Classic scripts with scheme {}: are not supported in workers.",
|
|
|
|
script_url.scheme()
|
|
|
|
)))
|
2021-08-16 08:29:54 -04:00
|
|
|
}
|
2021-08-18 09:19:22 -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) => {
|
|
|
|
return Err(
|
|
|
|
DomExceptionNetworkError {
|
|
|
|
msg: format!("Invalid MIME type {:?}.", mime_type),
|
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
return Err(
|
|
|
|
DomExceptionNetworkError::new("Missing MIME type.").into(),
|
|
|
|
)
|
|
|
|
}
|
2021-08-16 08:29:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-18 09:19:22 -04:00
|
|
|
let (text, _) = encoding_rs::UTF_8.decode_with_bom_removal(&body);
|
2021-08-16 08:29:54 -04:00
|
|
|
|
2021-08-18 09:19:22 -04:00
|
|
|
Ok(SyncFetchScript {
|
|
|
|
url: res_url,
|
|
|
|
script: text.into_owned(),
|
|
|
|
})
|
2021-08-16 08:29:54 -04:00
|
|
|
})
|
|
|
|
})
|
2021-08-18 09:19:22 -04:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
let mut ret = Vec::with_capacity(handles.len());
|
|
|
|
for handle in handles {
|
|
|
|
let script = runtime.block_on(handle)??;
|
|
|
|
ret.push(script);
|
|
|
|
}
|
|
|
|
Ok(ret)
|
|
|
|
});
|
|
|
|
|
|
|
|
thread.join().unwrap()
|
2021-08-16 08:29:54 -04:00
|
|
|
}
|