ts.rs 23.7 KB
Newer Older
R
Ry Dahl 已提交
1
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
2
use super::compiler_worker::CompilerWorker;
3
use crate::colors;
K
Kitson Kelly 已提交
4
use crate::compilers::CompilationResultFuture;
5
use crate::compilers::CompiledModule;
6
use crate::diagnostics::Diagnostic;
B
Bartek Iwańczuk 已提交
7
use crate::disk_cache::DiskCache;
8 9
use crate::file_fetcher::SourceFile;
use crate::file_fetcher::SourceFileFetcher;
10
use crate::global_state::GlobalState;
A
Andy Hayden 已提交
11
use crate::msg;
12
use crate::op_error::OpError;
13
use crate::ops::JsonResult;
B
Bartek Iwańczuk 已提交
14
use crate::source_maps::SourceMapGetter;
15
use crate::startup_data;
16
use crate::state::*;
17
use crate::tokio_util;
B
Bartek Iwańczuk 已提交
18
use crate::version;
19
use crate::web_worker::WebWorkerHandle;
20
use crate::worker::WorkerEvent;
21 22 23
use deno_core::Buf;
use deno_core::ErrBox;
use deno_core::ModuleSpecifier;
B
Bartek Iwańczuk 已提交
24
use futures::future::FutureExt;
25
use log::info;
26
use regex::Regex;
27
use serde_json::json;
K
Kitson Kelly 已提交
28
use std::collections::HashMap;
B
Bartek Iwańczuk 已提交
29 30
use std::collections::HashSet;
use std::fs;
K
Kitson Kelly 已提交
31
use std::hash::BuildHasher;
32
use std::io;
33
use std::ops::Deref;
34
use std::path::PathBuf;
B
Bartek Iwańczuk 已提交
35
use std::pin::Pin;
K
Kitson Kelly 已提交
36
use std::str;
37
use std::sync::atomic::Ordering;
38
use std::sync::Arc;
B
Bartek Iwańczuk 已提交
39 40
use std::sync::Mutex;
use url::Url;
R
Ryan Dahl 已提交
41

42 43 44 45 46
lazy_static! {
  static ref CHECK_JS_RE: Regex =
    Regex::new(r#""checkJs"\s*?:\s*?true"#).unwrap();
}

47
#[derive(Clone)]
48 49 50 51 52
pub enum TargetLib {
  Main,
  Worker,
}

53 54 55 56 57 58 59 60 61
/// Struct which represents the state of the compiler
/// configuration where the first is canonical name for the configuration file,
/// second is a vector of the bytes of the contents of the configuration file,
/// third is bytes of the hash of contents.
#[derive(Clone)]
pub struct CompilerConfig {
  pub path: Option<PathBuf>,
  pub content: Option<Vec<u8>>,
  pub hash: Vec<u8>,
62
  pub compile_js: bool,
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
}

impl CompilerConfig {
  /// Take the passed flag and resolve the file name relative to the cwd.
  pub fn load(config_path: Option<String>) -> Result<Self, ErrBox> {
    let config_file = match &config_path {
      Some(config_file_name) => {
        debug!("Compiler config file: {}", config_file_name);
        let cwd = std::env::current_dir().unwrap();
        Some(cwd.join(config_file_name))
      }
      _ => None,
    };

    // Convert the PathBuf to a canonicalized string.  This is needed by the
    // compiler to properly deal with the configuration.
    let config_path = match &config_file {
A
Axetroy 已提交
80 81 82 83 84 85 86 87 88
      Some(config_file) => Some(config_file.canonicalize().map_err(|_| {
        io::Error::new(
          io::ErrorKind::InvalidInput,
          format!(
            "Could not find the config file: {}",
            config_file.to_string_lossy()
          ),
        )
      })),
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
      _ => None,
    };

    // Load the contents of the configuration file
    let config = match &config_file {
      Some(config_file) => {
        debug!("Attempt to load config: {}", config_file.to_str().unwrap());
        let config = fs::read(&config_file)?;
        Some(config)
      }
      _ => None,
    };

    let config_hash = match &config {
      Some(bytes) => bytes.clone(),
      _ => b"".to_vec(),
    };

107 108 109 110 111 112 113 114 115
    // If `checkJs` is set to true in `compilerOptions` then we're gonna be compiling
    // JavaScript files as well
    let compile_js = if let Some(config_content) = config.clone() {
      let config_str = std::str::from_utf8(&config_content)?;
      CHECK_JS_RE.is_match(config_str)
    } else {
      false
    };

116
    let ts_config = Self {
A
Axetroy 已提交
117
      path: config_path.unwrap_or_else(|| Ok(PathBuf::new())).ok(),
118 119
      content: config,
      hash: config_hash,
120
      compile_js,
121 122 123 124 125
    };

    Ok(ts_config)
  }
}
B
Bartek Iwańczuk 已提交
126 127 128 129 130 131 132 133

/// Information associated with compiled file in cache.
/// Includes source code path and state hash.
/// version_hash is used to validate versions of the file
/// and could be used to remove stale file in cache.
pub struct CompiledFileMetadata {
  pub source_path: PathBuf,
  pub version_hash: String,
R
Ryan Dahl 已提交
134 135
}

R
Ryan Dahl 已提交
136 137
static SOURCE_PATH: &str = "source_path";
static VERSION_HASH: &str = "version_hash";
138

B
Bartek Iwańczuk 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
impl CompiledFileMetadata {
  pub fn from_json_string(metadata_string: String) -> Option<Self> {
    // TODO: use serde for deserialization
    let maybe_metadata_json: serde_json::Result<serde_json::Value> =
      serde_json::from_str(&metadata_string);

    if let Ok(metadata_json) = maybe_metadata_json {
      let source_path = metadata_json[SOURCE_PATH].as_str().map(PathBuf::from);
      let version_hash = metadata_json[VERSION_HASH].as_str().map(String::from);

      if source_path.is_none() || version_hash.is_none() {
        return None;
      }

      return Some(CompiledFileMetadata {
        source_path: source_path.unwrap(),
        version_hash: version_hash.unwrap(),
      });
R
Ryan Dahl 已提交
157
    }
B
Bartek Iwańczuk 已提交
158 159

    None
R
Ryan Dahl 已提交
160 161
  }

162
  pub fn to_json_string(&self) -> Result<String, serde_json::Error> {
B
Bartek Iwańczuk 已提交
163
    let mut value_map = serde_json::map::Map::new();
164

B
Bartek Iwańczuk 已提交
165 166 167 168 169
    value_map.insert(SOURCE_PATH.to_owned(), json!(&self.source_path));
    value_map.insert(VERSION_HASH.to_string(), json!(&self.version_hash));
    serde_json::to_string(&value_map)
  }
}
R
Ryan Dahl 已提交
170
/// Creates the JSON message send to compiler.ts's onmessage.
K
Kitson Kelly 已提交
171
fn req(
172
  request_type: msg::CompilerRequestType,
K
Kitson Kelly 已提交
173 174
  root_names: Vec<String>,
  compiler_config: CompilerConfig,
175
  out_file: Option<PathBuf>,
176
  target: &str,
K
Kitson Kelly 已提交
177
  bundle: bool,
K
Kitson Kelly 已提交
178
) -> Buf {
179 180
  let j = match (compiler_config.path, compiler_config.content) {
    (Some(config_path), Some(config_data)) => json!({
181
      "type": request_type as i32,
182
      "target": target,
183
      "rootNames": root_names,
184
      "outFile": out_file,
K
Kitson Kelly 已提交
185
      "bundle": bundle,
186 187 188
      "configPath": config_path,
      "config": str::from_utf8(&config_data).unwrap(),
    }),
189
    _ => json!({
190
      "type": request_type as i32,
191
      "target": target,
192
      "rootNames": root_names,
193
      "outFile": out_file,
K
Kitson Kelly 已提交
194
      "bundle": bundle,
195
    }),
R
Ryan Dahl 已提交
196
  };
197

R
Ryan Dahl 已提交
198
  j.to_string().into_boxed_str().into_boxed_bytes()
R
Ryan Dahl 已提交
199 200
}

201
/// Emit a SHA256 hash based on source code, deno version and TS config.
B
Bartek Iwańczuk 已提交
202 203 204 205 206 207
/// Used to check if a recompilation for source code is needed.
pub fn source_code_version_hash(
  source_code: &[u8],
  version: &str,
  config_hash: &[u8],
) -> String {
R
Ry Dahl 已提交
208
  crate::checksum::gen(vec![source_code, version.as_bytes(), config_hash])
B
Bartek Iwańczuk 已提交
209 210
}

211
pub struct TsCompilerInner {
212
  pub file_fetcher: SourceFileFetcher,
B
Bartek Iwańczuk 已提交
213 214 215 216 217 218 219 220
  pub config: CompilerConfig,
  pub disk_cache: DiskCache,
  /// Set of all URLs that have been compiled. This prevents double
  /// compilation of module.
  pub compiled: Mutex<HashSet<Url>>,
  /// This setting is controlled by `--reload` flag. Unless the flag
  /// is provided disk cache is used.
  pub use_disk_cache: bool,
221 222
  /// This setting is controlled by `compilerOptions.checkJs`
  pub compile_js: bool,
B
Bartek Iwańczuk 已提交
223 224
}

225 226 227 228 229 230 231 232 233 234
#[derive(Clone)]
pub struct TsCompiler(Arc<TsCompilerInner>);

impl Deref for TsCompiler {
  type Target = TsCompilerInner;
  fn deref(&self) -> &Self::Target {
    &self.0
  }
}

B
Bartek Iwańczuk 已提交
235 236
impl TsCompiler {
  pub fn new(
237 238
    file_fetcher: SourceFileFetcher,
    disk_cache: DiskCache,
B
Bartek Iwańczuk 已提交
239 240
    use_disk_cache: bool,
    config_path: Option<String>,
241 242
  ) -> Result<Self, ErrBox> {
    let config = CompilerConfig::load(config_path)?;
243
    Ok(TsCompiler(Arc::new(TsCompilerInner {
244 245
      file_fetcher,
      disk_cache,
246
      compile_js: config.compile_js,
247
      config,
B
Bartek Iwańczuk 已提交
248 249
      compiled: Mutex::new(HashSet::new()),
      use_disk_cache,
250
    })))
B
Bartek Iwańczuk 已提交
251 252
  }

253 254
  /// Create a new V8 worker with snapshot of TS compiler and setup compiler's
  /// runtime.
255
  fn setup_worker(global_state: GlobalState) -> CompilerWorker {
256 257
    let entry_point =
      ModuleSpecifier::resolve_url_or_path("./__$deno$ts_compiler.ts").unwrap();
258 259 260
    let worker_state =
      State::new(global_state.clone(), None, entry_point, DebugType::Internal)
        .expect("Unable to create worker state");
261

B
Bartek Iwańczuk 已提交
262
    // Count how many times we start the compiler worker.
263
    global_state.compiler_starts.fetch_add(1, Ordering::SeqCst);
B
Bartek Iwańczuk 已提交
264

265
    let mut worker = CompilerWorker::new(
B
Bartek Iwańczuk 已提交
266 267
      "TS".to_string(),
      startup_data::compiler_isolate_init(),
268
      worker_state,
B
Bartek Iwańczuk 已提交
269
    );
270
    worker.execute("bootstrapTsCompilerRuntime()").unwrap();
B
Bartek Iwańczuk 已提交
271 272 273
    worker
  }

274
  pub async fn bundle(
275
    &self,
276
    global_state: GlobalState,
B
Bartek Iwańczuk 已提交
277
    module_name: String,
278
    out_file: Option<PathBuf>,
279
  ) -> Result<(), ErrBox> {
B
Bartek Iwańczuk 已提交
280 281 282 283 284
    debug!(
      "Invoking the compiler to bundle. module_name: {}",
      module_name
    );

A
Axetroy 已提交
285
    let root_names = vec![module_name];
286
    let req_msg = req(
K
Kitson Kelly 已提交
287
      msg::CompilerRequestType::Compile,
288 289 290
      root_names,
      self.config.clone(),
      out_file,
291
      "main",
K
Kitson Kelly 已提交
292
      true,
293
    );
B
Bartek Iwańczuk 已提交
294

295 296 297 298 299
    let msg = execute_in_thread(global_state.clone(), req_msg).await?;
    let json_str = std::str::from_utf8(&msg).unwrap();
    debug!("Message: {}", json_str);
    if let Some(diagnostics) = Diagnostic::from_emit_result(json_str) {
      return Err(ErrBox::from(diagnostics));
300 301
    }
    Ok(())
B
Bartek Iwańczuk 已提交
302 303
  }

304 305
  /// Mark given module URL as compiled to avoid multiple compilations of same
  /// module in single run.
B
Bartek Iwańczuk 已提交
306 307 308 309 310
  fn mark_compiled(&self, url: &Url) {
    let mut c = self.compiled.lock().unwrap();
    c.insert(url.clone());
  }

311 312
  /// Check if given module URL has already been compiled and can be fetched
  /// directly from disk.
B
Bartek Iwańczuk 已提交
313 314 315 316 317 318 319 320 321
  fn has_compiled(&self, url: &Url) -> bool {
    let c = self.compiled.lock().unwrap();
    c.contains(url)
  }

  /// Asynchronously compile module and all it's dependencies.
  ///
  /// This method compiled every module at most once.
  ///
322 323
  /// If `--reload` flag was provided then compiler will not on-disk cache and
  /// force recompilation.
B
Bartek Iwańczuk 已提交
324
  ///
325 326
  /// If compilation is required then new V8 worker is spawned with fresh TS
  /// compiler.
327
  pub async fn compile(
328
    &self,
329
    global_state: GlobalState,
B
Bartek Iwańczuk 已提交
330
    source_file: &SourceFile,
331
    target: TargetLib,
332
  ) -> Result<CompiledModule, ErrBox> {
B
Bartek Iwańczuk 已提交
333
    if self.has_compiled(&source_file.url) {
334
      return self.get_compiled_module(&source_file.url);
B
Bartek Iwańczuk 已提交
335 336 337 338 339 340 341 342 343 344 345
    }

    if self.use_disk_cache {
      // Try to load cached version:
      // 1. check if there's 'meta' file
      if let Some(metadata) = self.get_metadata(&source_file.url) {
        // 2. compare version hashes
        // TODO: it would probably be good idea to make it method implemented on SourceFile
        let version_hash_to_validate = source_code_version_hash(
          &source_file.source_code,
          version::DENO,
346
          &self.config.hash,
B
Bartek Iwańczuk 已提交
347 348 349 350 351
        );

        if metadata.version_hash == version_hash_to_validate {
          debug!("load_cache metadata version hash match");
          if let Ok(compiled_module) =
352
            self.get_compiled_module(&source_file.url)
B
Bartek Iwańczuk 已提交
353
          {
354
            self.mark_compiled(&source_file.url);
355
            return Ok(compiled_module);
B
Bartek Iwańczuk 已提交
356
          }
K
Kitson Kelly 已提交
357 358
        }
      }
B
Bartek Iwańczuk 已提交
359 360 361
    }
    let source_file_ = source_file.clone();
    let module_url = source_file.url.clone();
362 363 364 365
    let target = match target {
      TargetLib::Main => "main",
      TargetLib::Worker => "worker",
    };
B
Bartek Iwańczuk 已提交
366
    let root_names = vec![module_url.to_string()];
367 368 369 370 371
    let req_msg = req(
      msg::CompilerRequestType::Compile,
      root_names,
      self.config.clone(),
      None,
372
      target,
K
Kitson Kelly 已提交
373
      false,
374
    );
B
Bartek Iwańczuk 已提交
375

376
    let ts_compiler = self.clone();
377

378
    info!(
379 380 381 382
      "{} {}",
      colors::green("Compile".to_string()),
      module_url.to_string()
    );
383

384
    let msg = execute_in_thread(global_state.clone(), req_msg).await?;
385

386 387 388
    let json_str = std::str::from_utf8(&msg).unwrap();
    if let Some(diagnostics) = Diagnostic::from_emit_result(json_str) {
      return Err(ErrBox::from(diagnostics));
389
    }
390
    ts_compiler.get_compiled_module(&source_file_.url)
B
Bartek Iwańczuk 已提交
391 392 393
  }

  /// Get associated `CompiledFileMetadata` for given module if it exists.
394
  pub fn get_metadata(&self, url: &Url) -> Option<CompiledFileMetadata> {
B
Bartek Iwańczuk 已提交
395 396 397 398 399 400 401 402 403 404 405
    // Try to load cached version:
    // 1. check if there's 'meta' file
    let cache_key = self
      .disk_cache
      .get_cache_filename_with_extension(url, "meta");
    if let Ok(metadata_bytes) = self.disk_cache.get(&cache_key) {
      if let Ok(metadata) = std::str::from_utf8(&metadata_bytes) {
        if let Some(read_metadata) =
          CompiledFileMetadata::from_json_string(metadata.to_string())
        {
          return Some(read_metadata);
406 407
        }
      }
B
Bartek Iwańczuk 已提交
408 409 410 411
    }

    None
  }
R
Ryan Dahl 已提交
412

413
  pub fn get_compiled_module(
414
    &self,
415 416 417 418 419 420 421 422 423 424 425 426 427 428
    module_url: &Url,
  ) -> Result<CompiledModule, ErrBox> {
    let compiled_source_file = self.get_compiled_source_file(module_url)?;

    let compiled_module = CompiledModule {
      code: str::from_utf8(&compiled_source_file.source_code)
        .unwrap()
        .to_string(),
      name: module_url.to_string(),
    };

    Ok(compiled_module)
  }

B
Bartek Iwańczuk 已提交
429 430 431 432
  /// Return compiled JS file for given TS module.
  // TODO: ideally we shouldn't construct SourceFile by hand, but it should be delegated to
  // SourceFileFetcher
  pub fn get_compiled_source_file(
433
    &self,
434
    module_url: &Url,
B
Bartek Iwańczuk 已提交
435 436 437
  ) -> Result<SourceFile, ErrBox> {
    let cache_key = self
      .disk_cache
438
      .get_cache_filename_with_extension(&module_url, "js");
B
Bartek Iwańczuk 已提交
439 440 441 442 443
    let compiled_code = self.disk_cache.get(&cache_key)?;
    let compiled_code_filename = self.disk_cache.location.join(cache_key);
    debug!("compiled filename: {:?}", compiled_code_filename);

    let compiled_module = SourceFile {
444
      url: module_url.clone(),
B
Bartek Iwańczuk 已提交
445 446 447
      filename: compiled_code_filename,
      media_type: msg::MediaType::JavaScript,
      source_code: compiled_code,
448
      types_url: None,
B
Bartek Iwańczuk 已提交
449 450 451 452 453 454 455 456 457
    };

    Ok(compiled_module)
  }

  /// Save compiled JS file for given TS module to on-disk cache.
  ///
  /// Along compiled file a special metadata file is saved as well containing
  /// hash that can be validated to avoid unnecessary recompilation.
458
  fn cache_compiled_file(
459
    &self,
B
Bartek Iwańczuk 已提交
460 461 462 463 464 465
    module_specifier: &ModuleSpecifier,
    contents: &str,
  ) -> std::io::Result<()> {
    let js_key = self
      .disk_cache
      .get_cache_filename_with_extension(module_specifier.as_url(), "js");
466 467 468 469 470 471 472 473 474 475 476 477
    self.disk_cache.set(&js_key, contents.as_bytes())?;
    self.mark_compiled(module_specifier.as_url());
    let source_file = self
      .file_fetcher
      .fetch_cached_source_file(&module_specifier)
      .expect("Source file not found");

    let version_hash = source_code_version_hash(
      &source_file.source_code,
      version::DENO,
      &self.config.hash,
    );
B
Bartek Iwańczuk 已提交
478

479 480 481 482 483 484 485 486 487 488 489
    let compiled_file_metadata = CompiledFileMetadata {
      source_path: source_file.filename,
      version_hash,
    };
    let meta_key = self
      .disk_cache
      .get_cache_filename_with_extension(module_specifier.as_url(), "meta");
    self.disk_cache.set(
      &meta_key,
      compiled_file_metadata.to_json_string()?.as_bytes(),
    )
B
Bartek Iwańczuk 已提交
490 491 492 493 494 495
  }

  /// Return associated source map file for given TS module.
  // TODO: ideally we shouldn't construct SourceFile by hand, but it should be delegated to
  // SourceFileFetcher
  pub fn get_source_map_file(
496
    &self,
B
Bartek Iwańczuk 已提交
497 498 499 500 501 502 503 504 505 506 507 508 509 510
    module_specifier: &ModuleSpecifier,
  ) -> Result<SourceFile, ErrBox> {
    let cache_key = self
      .disk_cache
      .get_cache_filename_with_extension(module_specifier.as_url(), "js.map");
    let source_code = self.disk_cache.get(&cache_key)?;
    let source_map_filename = self.disk_cache.location.join(cache_key);
    debug!("source map filename: {:?}", source_map_filename);

    let source_map_file = SourceFile {
      url: module_specifier.as_url().to_owned(),
      filename: source_map_filename,
      media_type: msg::MediaType::JavaScript,
      source_code,
511
      types_url: None,
B
Bartek Iwańczuk 已提交
512 513 514 515 516 517 518
    };

    Ok(source_map_file)
  }

  /// Save source map file for given TS module to on-disk cache.
  fn cache_source_map(
519
    &self,
B
Bartek Iwańczuk 已提交
520 521 522 523 524 525 526 527 528 529
    module_specifier: &ModuleSpecifier,
    contents: &str,
  ) -> std::io::Result<()> {
    let source_map_key = self
      .disk_cache
      .get_cache_filename_with_extension(module_specifier.as_url(), "js.map");
    self.disk_cache.set(&source_map_key, contents.as_bytes())
  }

  /// This method is called by TS compiler via an "op".
530
  pub fn cache_compiler_output(
531
    &self,
B
Bartek Iwańczuk 已提交
532 533 534 535 536 537
    module_specifier: &ModuleSpecifier,
    extension: &str,
    contents: &str,
  ) -> std::io::Result<()> {
    match extension {
      ".map" => self.cache_source_map(module_specifier, contents),
538
      ".js" => self.cache_compiled_file(module_specifier, contents),
B
Bartek Iwańczuk 已提交
539 540 541
      _ => unreachable!(),
    }
  }
A
andy finch 已提交
542 543
}

B
Bartek Iwańczuk 已提交
544 545 546 547
impl SourceMapGetter for TsCompiler {
  fn get_source_map(&self, script_name: &str) -> Option<Vec<u8>> {
    self
      .try_to_resolve_and_get_source_map(script_name)
R
Ry Dahl 已提交
548
      .map(|out| out.source_code)
B
Bartek Iwańczuk 已提交
549 550 551 552 553 554 555
  }

  fn get_source_line(&self, script_name: &str, line: usize) -> Option<String> {
    self
      .try_resolve_and_get_source_file(script_name)
      .and_then(|out| {
        str::from_utf8(&out.source_code).ok().and_then(|v| {
556 557 558
          // Do NOT use .lines(): it skips the terminating empty line.
          // (due to internally using .split_terminator() instead of .split())
          let lines: Vec<&str> = v.split('\n').collect();
B
Bartek Iwańczuk 已提交
559 560 561 562 563 564 565 566 567
          assert!(lines.len() > line);
          Some(lines[line].to_string())
        })
      })
  }
}

// `SourceMapGetter` related methods
impl TsCompiler {
568
  fn try_to_resolve(&self, script_name: &str) -> Option<ModuleSpecifier> {
B
Bartek Iwańczuk 已提交
569 570 571 572 573 574 575 576 577 578 579
    // if `script_name` can't be resolved to ModuleSpecifier it's probably internal
    // script (like `gen/cli/bundle/compiler.js`) so we won't be
    // able to get source for it anyway
    ModuleSpecifier::resolve_url(script_name).ok()
  }

  fn try_resolve_and_get_source_file(
    &self,
    script_name: &str,
  ) -> Option<SourceFile> {
    if let Some(module_specifier) = self.try_to_resolve(script_name) {
580
      return self
581 582
        .file_fetcher
        .fetch_cached_source_file(&module_specifier);
B
Bartek Iwańczuk 已提交
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
    }

    None
  }

  fn try_to_resolve_and_get_source_map(
    &self,
    script_name: &str,
  ) -> Option<SourceFile> {
    if let Some(module_specifier) = self.try_to_resolve(script_name) {
      return match self.get_source_map_file(&module_specifier) {
        Ok(out) => Some(out),
        Err(_) => None,
      };
    }

    None
  }
R
Ryan Dahl 已提交
601 602
}

603 604
// TODO(bartlomieju): exactly same function is in `wasm.rs` - only difference
// it created WasmCompiler instead of TsCompiler - deduplicate
605
async fn execute_in_thread(
606
  global_state: GlobalState,
607
  req: Buf,
608 609
) -> Result<Buf, ErrBox> {
  let (handle_sender, handle_receiver) =
610
    std::sync::mpsc::sync_channel::<Result<WebWorkerHandle, ErrBox>>(1);
611 612 613
  let builder =
    std::thread::Builder::new().name("deno-ts-compiler".to_string());
  let join_handle = builder.spawn(move || {
614
    let worker = TsCompiler::setup_worker(global_state.clone());
615 616
    handle_sender.send(Ok(worker.thread_safe_handle())).unwrap();
    drop(handle_sender);
617
    tokio_util::run_basic(worker).expect("Panic in event loop");
618
  })?;
619 620
  let handle = handle_receiver.recv().unwrap()?;
  handle.post_message(req)?;
621 622 623 624
  let event = handle.get_event().await.expect("Compiler didn't respond");
  let buf = match event {
    WorkerEvent::Message(buf) => Ok(buf),
    WorkerEvent::Error(error) => Err(error),
625
    WorkerEvent::TerminalError(error) => Err(error),
626
  }?;
627
  // Shutdown worker and wait for thread to finish
628
  handle.terminate();
629 630
  join_handle.join().unwrap();
  Ok(buf)
631 632 633 634
}

async fn execute_in_thread_json(
  req_msg: Buf,
635
  global_state: GlobalState,
636
) -> JsonResult {
637 638 639
  let msg = execute_in_thread(global_state, req_msg)
    .await
    .map_err(|e| OpError::other(e.to_string()))?;
640 641
  let json_str = std::str::from_utf8(&msg).unwrap();
  Ok(json!(json_str))
642 643
}

644
pub fn runtime_compile<S: BuildHasher>(
645
  global_state: GlobalState,
K
Kitson Kelly 已提交
646 647 648 649 650 651 652
  root_name: &str,
  sources: &Option<HashMap<String, String, S>>,
  bundle: bool,
  options: &Option<String>,
) -> Pin<Box<CompilationResultFuture>> {
  let req_msg = json!({
    "type": msg::CompilerRequestType::RuntimeCompile as i32,
653
    "target": "runtime",
K
Kitson Kelly 已提交
654 655 656 657 658 659 660 661 662
    "rootName": root_name,
    "sources": sources,
    "options": options,
    "bundle": bundle,
  })
  .to_string()
  .into_boxed_str()
  .into_boxed_bytes();

663
  execute_in_thread_json(req_msg, global_state).boxed_local()
K
Kitson Kelly 已提交
664 665
}

666
pub fn runtime_transpile<S: BuildHasher>(
667
  global_state: GlobalState,
K
Kitson Kelly 已提交
668 669 670 671 672 673 674 675 676 677 678 679
  sources: &HashMap<String, String, S>,
  options: &Option<String>,
) -> Pin<Box<CompilationResultFuture>> {
  let req_msg = json!({
    "type": msg::CompilerRequestType::RuntimeTranspile as i32,
    "sources": sources,
    "options": options,
  })
  .to_string()
  .into_boxed_str()
  .into_boxed_bytes();

680
  execute_in_thread_json(req_msg, global_state).boxed_local()
K
Kitson Kelly 已提交
681 682
}

R
Ryan Dahl 已提交
683 684 685
#[cfg(test)]
mod tests {
  use super::*;
686
  use crate::fs as deno_fs;
687
  use deno_core::ModuleSpecifier;
B
Bartek Iwańczuk 已提交
688
  use std::path::PathBuf;
689
  use tempfile::TempDir;
B
Bartek Iwańczuk 已提交
690

691
  #[tokio::test]
692
  async fn test_compile() {
693 694 695
    let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
      .parent()
      .unwrap()
L
Luka Hartwig 已提交
696
      .join("cli/tests/002_hello.ts");
697 698 699 700 701 702 703
    let specifier =
      ModuleSpecifier::resolve_url_or_path(p.to_str().unwrap()).unwrap();
    let out = SourceFile {
      url: specifier.as_url().clone(),
      filename: PathBuf::from(p.to_str().unwrap().to_string()),
      media_type: msg::MediaType::TypeScript,
      source_code: include_bytes!("../tests/002_hello.ts").to_vec(),
704
      types_url: None,
705
    };
706 707
    let mock_state =
      GlobalState::mock(vec![String::from("deno"), String::from("hello.js")]);
708 709
    let result = mock_state
      .ts_compiler
710
      .compile(mock_state.clone(), &out, TargetLib::Main)
711 712 713 714 715 716
      .await;
    assert!(result.is_ok());
    assert!(result
      .unwrap()
      .code
      .as_bytes()
717
      .starts_with(b"\"use strict\";\nconsole.log(\"Hello World\");"));
718 719
  }

720
  #[tokio::test]
721
  async fn test_bundle() {
722 723 724
    let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
      .parent()
      .unwrap()
L
Luka Hartwig 已提交
725
      .join("cli/tests/002_hello.ts");
726
    use deno_core::ModuleSpecifier;
727
    let module_name = ModuleSpecifier::resolve_url_or_path(p.to_str().unwrap())
K
Kitson Kelly 已提交
728 729 730
      .unwrap()
      .to_string();

731
    let state = GlobalState::mock(vec![
732 733
      String::from("deno"),
      p.to_string_lossy().into(),
K
Kitson Kelly 已提交
734 735
      String::from("$deno$/bundle.js"),
    ]);
736

737 738
    let result = state
      .ts_compiler
739
      .bundle(
740 741
        state.clone(),
        module_name,
742
        Some(PathBuf::from("$deno$/bundle.js")),
743 744 745
      )
      .await;
    assert!(result.is_ok());
K
Kitson Kelly 已提交
746
  }
B
Bartek Iwańczuk 已提交
747 748 749 750

  #[test]
  fn test_source_code_version_hash() {
    assert_eq!(
751
      "0185b42de0686b4c93c314daaa8dee159f768a9e9a336c2a5e3d5b8ca6c4208c",
B
Bartek Iwańczuk 已提交
752 753 754 755
      source_code_version_hash(b"1+2", "0.4.0", b"{}")
    );
    // Different source_code should result in different hash.
    assert_eq!(
756
      "e58631f1b6b6ce2b300b133ec2ad16a8a5ba6b7ecf812a8c06e59056638571ac",
B
Bartek Iwańczuk 已提交
757 758 759 760
      source_code_version_hash(b"1", "0.4.0", b"{}")
    );
    // Different version should result in different hash.
    assert_eq!(
761
      "307e6200347a88dbbada453102deb91c12939c65494e987d2d8978f6609b5633",
B
Bartek Iwańczuk 已提交
762 763 764 765
      source_code_version_hash(b"1", "0.1.0", b"{}")
    );
    // Different config should result in different hash.
    assert_eq!(
766
      "195eaf104a591d1d7f69fc169c60a41959c2b7a21373cd23a8f675f877ec385f",
B
Bartek Iwańczuk 已提交
767 768 769
      source_code_version_hash(b"1", "0.4.0", b"{\"compilerOptions\": {}}")
    );
  }
770 771 772 773 774 775 776 777

  #[test]
  fn test_compile_js() {
    let temp_dir = TempDir::new().expect("tempdir fail");
    let temp_dir_path = temp_dir.path();

    let test_cases = vec![
      // valid JSON
A
Axetroy 已提交
778
      (r#"{ "compilerOptions": { "checkJs": true } } "#, true),
779 780 781 782 783 784
      // JSON with comment
      (
        r#"{ "compilerOptions": { // force .js file compilation by Deno "checkJs": true } } "#,
        true,
      ),
      // invalid JSON
A
Axetroy 已提交
785
      (r#"{ "compilerOptions": { "checkJs": true },{ } "#, true),
786
      // without content
A
Axetroy 已提交
787
      ("", false),
788 789 790 791 792 793 794 795 796 797 798
    ];

    let path = temp_dir_path.join("tsconfig.json");
    let path_str = path.to_str().unwrap().to_string();

    for (json_str, expected) in test_cases {
      deno_fs::write_file(&path, json_str.as_bytes(), 0o666).unwrap();
      let config = CompilerConfig::load(Some(path_str.clone())).unwrap();
      assert_eq!(config.compile_js, expected);
    }
  }
799 800 801 802 803 804 805

  #[test]
  fn test_compiler_config_load() {
    let temp_dir = TempDir::new().expect("tempdir fail");
    let temp_dir_path = temp_dir.path();
    let path = temp_dir_path.join("doesnotexist.json");
    let path_str = path.to_str().unwrap().to_string();
A
Axetroy 已提交
806
    let res = CompilerConfig::load(Some(path_str));
807 808
    assert!(res.is_err());
  }
R
Ryan Dahl 已提交
809
}