compiler.rs 1.9 KB
Newer Older
B
Bartek Iwańczuk 已提交
1
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
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::*;
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,
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

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,
27 28
    &args.extension,
    &args.contents,
B
Bartek Iwańczuk 已提交
29 30
  )?;

31 32 33 34 35 36 37
  Ok(JsonOp::Sync(json!({})))
}

#[derive(Deserialize)]
struct FetchSourceFileArgs {
  specifier: String,
  referrer: String,
B
Bartek Iwańczuk 已提交
38 39 40 41
}

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

  // 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 =
52
    state.resolve(&args.specifier, &args.referrer, false, is_dyn_import)?;
B
Bartek Iwańczuk 已提交
53 54 55

  let fut = state
    .file_fetcher
56
    .fetch_source_file_async(&resolved_specifier);
B
Bartek Iwańczuk 已提交
57 58 59 60

  // 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.
61 62 63 64 65 66 67
  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(),
  })))
B
Bartek Iwańczuk 已提交
68
}