2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-02-19 00:34:11 -05:00
|
|
|
use super::dispatch_json::Deserialize;
|
|
|
|
use super::dispatch_json::JsonOp;
|
|
|
|
use super::dispatch_json::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;
|
2020-02-23 14:51:29 -05:00
|
|
|
use crate::op_error::OpError;
|
2020-02-08 14:34:31 -05:00
|
|
|
use crate::state::State;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::CoreIsolate;
|
2020-03-02 13:12:49 -05:00
|
|
|
use deno_core::ModuleLoader;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::ModuleSpecifier;
|
|
|
|
use deno_core::ZeroCopyBuf;
|
2020-02-19 17:51:10 -05:00
|
|
|
use futures::future::FutureExt;
|
2019-08-26 08:50:21 -04:00
|
|
|
|
2020-04-23 05:51:07 -04:00
|
|
|
pub fn init(i: &mut CoreIsolate, s: &State) {
|
2020-02-25 09:14:27 -05:00
|
|
|
i.register_op("op_cache", s.stateful_json_op(op_cache));
|
|
|
|
i.register_op("op_resolve_modules", s.stateful_json_op(op_resolve_modules));
|
2020-01-08 09:17:44 -05:00
|
|
|
i.register_op(
|
2020-02-25 09:14:27 -05:00
|
|
|
"op_fetch_source_files",
|
|
|
|
s.stateful_json_op(op_fetch_source_files),
|
2020-01-08 09:17:44 -05:00
|
|
|
);
|
2020-02-25 09:14:27 -05:00
|
|
|
let custom_assets = std::collections::HashMap::new(); // TODO(ry) use None.
|
2019-10-11 14:41:54 -04:00
|
|
|
i.register_op(
|
2020-02-25 09:14:27 -05:00
|
|
|
"op_fetch_asset",
|
|
|
|
deno_typescript::op_fetch_asset(custom_assets),
|
2020-02-19 00:34:11 -05:00
|
|
|
);
|
2019-10-11 14:41:54 -04:00
|
|
|
}
|
|
|
|
|
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(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
2020-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-08-26 08:50:21 -04:00
|
|
|
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");
|
|
|
|
|
2020-02-19 17:51:10 -05:00
|
|
|
let state_ = &state.borrow().global_state;
|
|
|
|
let ts_compiler = state_.ts_compiler.clone();
|
2020-04-15 23:14:28 -04:00
|
|
|
ts_compiler.cache_compiler_output(
|
2020-02-19 17:51:10 -05:00
|
|
|
&module_specifier,
|
|
|
|
&args.extension,
|
|
|
|
&args.contents,
|
2020-04-15 23:14:28 -04:00
|
|
|
)?;
|
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(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
2020-01-24 15:10:49 -05:00
|
|
|
_data: Option<ZeroCopyBuf>,
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2020-01-08 09:17:44 -05:00
|
|
|
let args: SpecifiersReferrerArgs = serde_json::from_value(args)?;
|
|
|
|
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 {
|
2020-02-23 14:51:29 -05:00
|
|
|
let specifier = state
|
|
|
|
.resolve(specifier, &referrer, is_main)
|
|
|
|
.map_err(OpError::from)?;
|
|
|
|
specifiers.push(specifier.as_str().to_owned());
|
2020-01-08 09:17:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(JsonOp::Sync(json!(specifiers)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn op_fetch_source_files(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2020-01-08 09:17:44 -05:00
|
|
|
args: Value,
|
2020-01-24 15:10:49 -05:00
|
|
|
_data: Option<ZeroCopyBuf>,
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2020-01-08 09:17:44 -05:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2020-02-08 14:34:31 -05:00
|
|
|
let global_state = state.borrow().global_state.clone();
|
2020-02-19 17:51:10 -05:00
|
|
|
let file_fetcher = global_state.file_fetcher.clone();
|
|
|
|
let specifiers = args.specifiers.clone();
|
|
|
|
let future = async move {
|
|
|
|
let file_futures: Vec<_> = specifiers
|
|
|
|
.into_iter()
|
|
|
|
.map(|specifier| {
|
|
|
|
let file_fetcher_ = file_fetcher.clone();
|
|
|
|
let ref_specifier_ = ref_specifier.clone();
|
|
|
|
async move {
|
|
|
|
let resolved_specifier = ModuleSpecifier::resolve_url(&specifier)
|
|
|
|
.expect("Invalid specifier");
|
|
|
|
file_fetcher_
|
2020-02-25 14:42:00 -05:00
|
|
|
.fetch_source_file(&resolved_specifier, ref_specifier_)
|
2020-02-19 17:51:10 -05:00
|
|
|
.await
|
|
|
|
}
|
|
|
|
.boxed_local()
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2020-02-23 14:51:29 -05:00
|
|
|
let files = try_join_all(file_futures).await.map_err(OpError::from)?;
|
2020-01-04 05:20:52 -05:00
|
|
|
// We want to get an array of futures that resolves to
|
2020-01-26 13:59:41 -05:00
|
|
|
let v = files.into_iter().map(|f| {
|
2020-01-04 05:20:52 -05:00
|
|
|
async {
|
2020-01-26 13:59:41 -05:00
|
|
|
// if the source file contains a `types_url` we need to replace
|
|
|
|
// the module with the type definition when requested by the compiler
|
|
|
|
let file = match f.types_url {
|
|
|
|
Some(types_url) => {
|
|
|
|
let types_specifier = ModuleSpecifier::from(types_url);
|
|
|
|
global_state
|
|
|
|
.file_fetcher
|
2020-02-25 14:42:00 -05:00
|
|
|
.fetch_source_file(&types_specifier, ref_specifier.clone())
|
2020-02-23 14:51:29 -05:00
|
|
|
.await
|
|
|
|
.map_err(OpError::from)?
|
2020-01-26 13:59:41 -05:00
|
|
|
}
|
|
|
|
_ => f,
|
|
|
|
};
|
2020-03-18 12:39:53 -04:00
|
|
|
// Special handling of WASM and JSON files:
|
2020-01-04 05:20:52 -05:00
|
|
|
// compile them into JS first!
|
2020-03-18 12:39:53 -04:00
|
|
|
// This allows TS to do correct export types as well as bundles.
|
2020-01-04 05:20:52 -05:00
|
|
|
let source_code = match file.media_type {
|
|
|
|
msg::MediaType::Wasm => {
|
|
|
|
global_state
|
|
|
|
.wasm_compiler
|
2020-02-25 14:42:00 -05:00
|
|
|
.compile(global_state.clone(), &file)
|
2020-02-23 14:51:29 -05:00
|
|
|
.await
|
|
|
|
.map_err(|e| OpError::other(e.to_string()))?
|
2020-01-04 05:20:52 -05:00
|
|
|
.code
|
2019-11-14 08:31:39 -05:00
|
|
|
}
|
2020-03-18 12:39:53 -04:00
|
|
|
msg::MediaType::Json => {
|
|
|
|
global_state
|
|
|
|
.json_compiler
|
|
|
|
.compile(&file)
|
|
|
|
.await
|
|
|
|
.map_err(|e| OpError::other(e.to_string()))?
|
|
|
|
.code
|
|
|
|
}
|
2020-04-10 22:14:16 -04:00
|
|
|
_ => String::from_utf8(file.source_code)
|
|
|
|
.map_err(|_| OpError::invalid_utf8())?,
|
2020-01-04 05:20:52 -05:00
|
|
|
};
|
2020-02-23 14:51:29 -05:00
|
|
|
Ok::<_, OpError>(json!({
|
2020-01-04 05:20:52 -05:00
|
|
|
"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())
|
2020-02-19 17:51:10 -05:00
|
|
|
}
|
|
|
|
.boxed_local();
|
2020-01-04 05:20:52 -05:00
|
|
|
|
|
|
|
Ok(JsonOp::Async(future))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|