compiler.rs 2.6 KB
Newer Older
B
Bartek Iwańczuk 已提交
1
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
B
Bartek Iwańczuk 已提交
2
use super::dispatch_json::{Deserialize, JsonOp, Value};
B
Bartek Iwańczuk 已提交
3 4 5
use crate::state::ThreadSafeState;
use crate::tokio_util;
use deno::*;
B
Bartek Iwańczuk 已提交
6 7 8 9 10 11 12 13

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct CacheArgs {
  module_id: String,
  contents: String,
  extension: String,
}
B
Bartek Iwańczuk 已提交
14 15 16

pub fn op_cache(
  state: &ThreadSafeState,
B
Bartek Iwańczuk 已提交
17 18 19 20
  args: Value,
  _zero_copy: Option<PinnedBuf>,
) -> Result<JsonOp, ErrBox> {
  let args: CacheArgs = serde_json::from_value(args)?;
B
Bartek Iwańczuk 已提交
21

B
Bartek Iwańczuk 已提交
22
  let module_specifier = ModuleSpecifier::resolve_url(&args.module_id)
B
Bartek Iwańczuk 已提交
23 24 25 26
    .expect("Should be valid module specifier");

  state.ts_compiler.cache_compiler_output(
    &module_specifier,
B
Bartek Iwańczuk 已提交
27 28
    &args.extension,
    &args.contents,
B
Bartek Iwańczuk 已提交
29 30
  )?;

B
Bartek Iwańczuk 已提交
31 32 33 34
  Ok(JsonOp::Sync(json!({})))
}

#[derive(Deserialize)]
35 36
struct FetchSourceFilesArgs {
  specifiers: Vec<String>,
B
Bartek Iwańczuk 已提交
37
  referrer: String,
B
Bartek Iwańczuk 已提交
38 39
}

40
pub fn op_fetch_source_files(
B
Bartek Iwańczuk 已提交
41
  state: &ThreadSafeState,
B
Bartek Iwańczuk 已提交
42 43 44
  args: Value,
  _zero_copy: Option<PinnedBuf>,
) -> Result<JsonOp, ErrBox> {
45
  let args: FetchSourceFilesArgs = serde_json::from_value(args)?;
B
Bartek Iwańczuk 已提交
46 47 48 49 50

  // 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;

51 52 53 54 55 56 57 58 59
  let mut futures = vec![];
  for specifier in &args.specifiers {
    let resolved_specifier =
      state.resolve(specifier, &args.referrer, false, is_dyn_import)?;
    let fut = state
      .file_fetcher
      .fetch_source_file_async(&resolved_specifier);
    futures.push(fut);
  }
B
Bartek Iwańczuk 已提交
60 61

  // WARNING: Here we use tokio_util::block_on() which starts a new Tokio
62
  // runtime for executing the future. This is so we don't inadvertently run
B
Bartek Iwańczuk 已提交
63
  // out of threads in the main runtime.
64 65 66 67 68 69 70 71 72 73 74 75 76 77
  let files = tokio_util::block_on(futures::future::join_all(futures))?;
  let res: Vec<serde_json::value::Value> = files
    .into_iter()
    .map(|file| {
      json!({
        "moduleName": file.url.to_string(),
        "filename": file.filename.to_str().unwrap(),
        "mediaType": file.media_type as i32,
        "sourceCode": String::from_utf8(file.source_code).unwrap(),
      })
    })
    .collect();

  Ok(JsonOp::Sync(json!(res)))
B
Bartek Iwańczuk 已提交
78
}
79 80 81 82 83 84 85 86 87 88 89 90

#[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)?;
91
  if let Some(source_code) = deno_cli_snapshots::get_asset(&args.name) {
92 93 94 95 96
    Ok(JsonOp::Sync(json!(source_code)))
  } else {
    panic!("op_fetch_asset bad asset {}", args.name)
  }
}