2019-08-14 11:03:02 -04:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-08-26 08:50:21 -04:00
|
|
|
use super::dispatch_json::{Deserialize, JsonOp, Value};
|
2019-08-30 11:11:33 -04:00
|
|
|
use crate::assets;
|
2019-08-14 11:03:02 -04:00
|
|
|
use crate::state::ThreadSafeState;
|
|
|
|
use crate::tokio_util;
|
|
|
|
use deno::*;
|
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
|
|
|
|
|
|
|
pub fn op_cache(
|
|
|
|
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");
|
|
|
|
|
|
|
|
state.ts_compiler.cache_compiler_output(
|
|
|
|
&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!({})))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct FetchSourceFileArgs {
|
|
|
|
specifier: String,
|
|
|
|
referrer: String,
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn op_fetch_source_file(
|
|
|
|
state: &ThreadSafeState,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: FetchSourceFileArgs = 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;
|
|
|
|
|
|
|
|
let resolved_specifier =
|
2019-08-26 08:50:21 -04:00
|
|
|
state.resolve(&args.specifier, &args.referrer, false, is_dyn_import)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
|
|
|
let fut = state
|
|
|
|
.file_fetcher
|
2019-08-26 08:50:21 -04:00
|
|
|
.fetch_source_file_async(&resolved_specifier);
|
2019-08-14 11:03:02 -04:00
|
|
|
|
|
|
|
// WARNING: Here we use tokio_util::block_on() which starts a new Tokio
|
|
|
|
// runtime for executing the future. This is so we don't inadvernently run
|
|
|
|
// out of threads in the main runtime.
|
2019-08-26 08:50:21 -04:00
|
|
|
let out = tokio_util::block_on(fut)?;
|
|
|
|
Ok(JsonOp::Sync(json!({
|
|
|
|
"moduleName": out.url.to_string(),
|
|
|
|
"filename": out.filename.to_str().unwrap(),
|
|
|
|
"mediaType": out.media_type as i32,
|
|
|
|
"sourceCode": String::from_utf8(out.source_code).unwrap(),
|
|
|
|
})))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
2019-08-30 11:11:33 -04:00
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct FetchAssetArgs {
|
|
|
|
name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn op_fetch_asset(
|
|
|
|
_state: &ThreadSafeState,
|
|
|
|
args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: FetchAssetArgs = serde_json::from_value(args)?;
|
|
|
|
if let Some(source_code) = assets::get_source_code(&args.name) {
|
|
|
|
Ok(JsonOp::Sync(json!(source_code)))
|
|
|
|
} else {
|
|
|
|
panic!("op_fetch_asset bad asset {}", args.name)
|
|
|
|
}
|
|
|
|
}
|