2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-08-26 08:50:21 -04:00
|
|
|
use super::dispatch_json::{Deserialize, JsonOp, Value};
|
2019-11-16 19:17:47 -05:00
|
|
|
use crate::futures::future::try_join_all;
|
2019-11-14 08:31:39 -05:00
|
|
|
use crate::msg;
|
2019-10-11 14:41:54 -04:00
|
|
|
use crate::ops::json_op;
|
2019-08-14 11:03:02 -04:00
|
|
|
use crate::state::ThreadSafeState;
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::Loader;
|
|
|
|
use deno_core::*;
|
2019-08-26 08:50:21 -04:00
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
pub fn init(i: &mut Isolate, s: &ThreadSafeState) {
|
|
|
|
i.register_op("cache", s.core_op(json_op(s.stateful_op(op_cache))));
|
2020-01-08 09:17:44 -05:00
|
|
|
i.register_op(
|
|
|
|
"resolve_modules",
|
|
|
|
s.core_op(json_op(s.stateful_op(op_resolve_modules))),
|
|
|
|
);
|
2019-10-11 14:41:54 -04:00
|
|
|
i.register_op(
|
|
|
|
"fetch_source_files",
|
|
|
|
s.core_op(json_op(s.stateful_op(op_fetch_source_files))),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct CacheArgs {
|
|
|
|
module_id: String,
|
|
|
|
contents: String,
|
|
|
|
extension: String,
|
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_cache(
|
2019-08-14 11:03:02 -04:00
|
|
|
state: &ThreadSafeState,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: CacheArgs = serde_json::from_value(args)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
let module_specifier = ModuleSpecifier::resolve_url(&args.module_id)
|
2019-08-14 11:03:02 -04:00
|
|
|
.expect("Should be valid module specifier");
|
|
|
|
|
2019-11-04 10:38:52 -05:00
|
|
|
state.global_state.ts_compiler.cache_compiler_output(
|
2019-08-14 11:03:02 -04:00
|
|
|
&module_specifier,
|
2019-08-26 08:50:21 -04:00
|
|
|
&args.extension,
|
|
|
|
&args.contents,
|
2019-08-14 11:03:02 -04:00
|
|
|
)?;
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
Ok(JsonOp::Sync(json!({})))
|
|
|
|
}
|
|
|
|
|
2020-01-08 09:17:44 -05:00
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
struct SpecifiersReferrerArgs {
|
2019-09-14 12:05:00 -04:00
|
|
|
specifiers: Vec<String>,
|
2019-11-25 09:33:23 -05:00
|
|
|
referrer: Option<String>,
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2020-01-08 09:17:44 -05:00
|
|
|
fn op_resolve_modules(
|
2019-08-14 11:03:02 -04:00
|
|
|
state: &ThreadSafeState,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
2019-10-03 07:23:29 -04:00
|
|
|
_data: Option<PinnedBuf>,
|
2019-08-26 08:50:21 -04:00
|
|
|
) -> Result<JsonOp, ErrBox> {
|
2020-01-08 09:17:44 -05:00
|
|
|
let args: SpecifiersReferrerArgs = serde_json::from_value(args)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
|
|
|
// TODO(ry) Maybe a security hole. Only the compiler worker should have access
|
|
|
|
// to this. Need a test to demonstrate the hole.
|
|
|
|
let is_dyn_import = false;
|
|
|
|
|
2020-01-08 09:17:44 -05:00
|
|
|
let (referrer, is_main) = if let Some(referrer) = args.referrer {
|
|
|
|
(referrer, false)
|
|
|
|
} else {
|
|
|
|
("<unknown>".to_owned(), true)
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut specifiers = vec![];
|
|
|
|
|
|
|
|
for specifier in &args.specifiers {
|
|
|
|
let resolved_specifier =
|
|
|
|
state.resolve(specifier, &referrer, is_main, is_dyn_import);
|
|
|
|
match resolved_specifier {
|
|
|
|
Ok(ms) => specifiers.push(ms.as_str().to_owned()),
|
|
|
|
Err(err) => return Err(err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(JsonOp::Sync(json!(specifiers)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn op_fetch_source_files(
|
|
|
|
state: &ThreadSafeState,
|
|
|
|
args: Value,
|
|
|
|
_data: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: SpecifiersReferrerArgs = serde_json::from_value(args)?;
|
|
|
|
|
|
|
|
let ref_specifier = if let Some(referrer) = args.referrer {
|
2019-11-25 09:33:23 -05:00
|
|
|
let specifier = ModuleSpecifier::resolve_url(&referrer)
|
|
|
|
.expect("Referrer is not a valid specifier");
|
2020-01-08 09:17:44 -05:00
|
|
|
Some(specifier)
|
2019-11-25 09:33:23 -05:00
|
|
|
} else {
|
2020-01-08 09:17:44 -05:00
|
|
|
None
|
2019-11-25 09:33:23 -05:00
|
|
|
};
|
|
|
|
|
2019-09-14 12:05:00 -04:00
|
|
|
let mut futures = vec![];
|
|
|
|
for specifier in &args.specifiers {
|
|
|
|
let resolved_specifier =
|
2020-01-08 09:17:44 -05:00
|
|
|
ModuleSpecifier::resolve_url(&specifier).expect("Invalid specifier");
|
2019-09-14 12:05:00 -04:00
|
|
|
let fut = state
|
2019-11-04 10:38:52 -05:00
|
|
|
.global_state
|
2019-09-14 12:05:00 -04:00
|
|
|
.file_fetcher
|
2019-11-25 09:33:23 -05:00
|
|
|
.fetch_source_file_async(&resolved_specifier, ref_specifier.clone());
|
2019-09-14 12:05:00 -04:00
|
|
|
futures.push(fut);
|
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-11-14 08:31:39 -05:00
|
|
|
let global_state = state.global_state.clone();
|
|
|
|
|
2020-01-04 05:20:52 -05:00
|
|
|
let future = Box::pin(async move {
|
|
|
|
let files = try_join_all(futures).await?;
|
|
|
|
|
|
|
|
// We want to get an array of futures that resolves to
|
|
|
|
let v = files.into_iter().map(|file| {
|
|
|
|
async {
|
|
|
|
// Special handling of Wasm files:
|
|
|
|
// compile them into JS first!
|
|
|
|
// This allows TS to do correct export types.
|
|
|
|
let source_code = match file.media_type {
|
|
|
|
msg::MediaType::Wasm => {
|
|
|
|
global_state
|
|
|
|
.wasm_compiler
|
|
|
|
.compile_async(global_state.clone(), &file)
|
|
|
|
.await?
|
|
|
|
.code
|
2019-11-14 08:31:39 -05:00
|
|
|
}
|
2020-01-04 05:20:52 -05:00
|
|
|
_ => String::from_utf8(file.source_code).unwrap(),
|
|
|
|
};
|
|
|
|
Ok::<_, ErrBox>(json!({
|
|
|
|
"url": file.url.to_string(),
|
|
|
|
"filename": file.filename.to_str().unwrap(),
|
|
|
|
"mediaType": file.media_type as i32,
|
|
|
|
"sourceCode": source_code,
|
|
|
|
}))
|
|
|
|
}
|
2019-10-03 07:23:29 -04:00
|
|
|
});
|
2019-09-14 12:05:00 -04:00
|
|
|
|
2020-01-04 05:20:52 -05:00
|
|
|
let v = try_join_all(v).await?;
|
|
|
|
Ok(v.into())
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(JsonOp::Async(future))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|