2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-10-13 19:52:49 -04:00
|
|
|
|
2022-06-27 16:54:09 -04:00
|
|
|
use crate::args::TsConfig;
|
2022-10-21 11:20:18 -04:00
|
|
|
use crate::node;
|
|
|
|
use crate::node::node_resolve_npm_reference;
|
|
|
|
use crate::node::NodeResolution;
|
|
|
|
use crate::npm::NpmPackageResolver;
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::util::checksum;
|
2020-10-13 19:52:49 -04:00
|
|
|
|
2021-09-07 10:39:32 -04:00
|
|
|
use deno_ast::MediaType;
|
2021-11-16 09:02:28 -05:00
|
|
|
use deno_core::anyhow::anyhow;
|
|
|
|
use deno_core::anyhow::Context;
|
2020-10-13 19:52:49 -04:00
|
|
|
use deno_core::error::AnyError;
|
2021-06-21 19:45:41 -04:00
|
|
|
use deno_core::located_script_name;
|
2022-03-14 13:44:15 -04:00
|
|
|
use deno_core::op;
|
2021-02-17 13:47:18 -05:00
|
|
|
use deno_core::resolve_url_or_path;
|
2021-02-25 15:18:35 -05:00
|
|
|
use deno_core::serde::Deserialize;
|
2022-09-02 10:54:40 -04:00
|
|
|
use deno_core::serde::Deserializer;
|
2021-05-11 00:54:10 -04:00
|
|
|
use deno_core::serde::Serialize;
|
2022-09-02 10:54:40 -04:00
|
|
|
use deno_core::serde::Serializer;
|
2020-10-13 19:52:49 -04:00
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_core::serde_json::json;
|
|
|
|
use deno_core::serde_json::Value;
|
2023-01-14 09:36:19 -05:00
|
|
|
use deno_core::serde_v8;
|
2022-03-14 13:44:15 -04:00
|
|
|
use deno_core::Extension;
|
2020-10-13 19:52:49 -04:00
|
|
|
use deno_core::JsRuntime;
|
|
|
|
use deno_core::ModuleSpecifier;
|
2022-03-14 13:44:15 -04:00
|
|
|
use deno_core::OpState;
|
2020-10-13 19:52:49 -04:00
|
|
|
use deno_core::RuntimeOptions;
|
2022-07-04 18:12:41 -04:00
|
|
|
use deno_core::Snapshot;
|
2023-02-22 14:15:25 -05:00
|
|
|
use deno_graph::npm::NpmPackageNvReference;
|
2023-02-21 12:03:48 -05:00
|
|
|
use deno_graph::npm::NpmPackageReqReference;
|
2023-02-22 14:15:25 -05:00
|
|
|
use deno_graph::Module;
|
2023-02-09 22:00:23 -05:00
|
|
|
use deno_graph::ModuleGraph;
|
|
|
|
use deno_graph::ResolutionResolved;
|
2022-11-30 18:07:32 -05:00
|
|
|
use deno_runtime::deno_node::NodeResolutionMode;
|
2023-01-10 08:35:44 -05:00
|
|
|
use deno_runtime::permissions::PermissionsContainer;
|
2021-12-18 16:14:42 -05:00
|
|
|
use once_cell::sync::Lazy;
|
2022-10-21 11:20:18 -04:00
|
|
|
use std::borrow::Cow;
|
2020-10-25 16:17:58 -04:00
|
|
|
use std::collections::HashMap;
|
2022-09-02 10:54:40 -04:00
|
|
|
use std::fmt;
|
2020-11-09 18:10:41 -05:00
|
|
|
use std::path::PathBuf;
|
2020-12-29 23:17:17 -05:00
|
|
|
use std::sync::Arc;
|
2020-10-13 19:52:49 -04:00
|
|
|
|
2022-11-25 18:29:48 -05:00
|
|
|
mod diagnostics;
|
|
|
|
|
|
|
|
pub use self::diagnostics::Diagnostic;
|
|
|
|
pub use self::diagnostics::DiagnosticCategory;
|
|
|
|
pub use self::diagnostics::DiagnosticMessageChain;
|
|
|
|
pub use self::diagnostics::Diagnostics;
|
|
|
|
pub use self::diagnostics::Position;
|
|
|
|
|
2022-07-04 18:12:41 -04:00
|
|
|
pub static COMPILER_SNAPSHOT: Lazy<Box<[u8]>> = Lazy::new(
|
|
|
|
#[cold]
|
|
|
|
#[inline(never)]
|
|
|
|
|| {
|
|
|
|
static COMPRESSED_COMPILER_SNAPSHOT: &[u8] =
|
|
|
|
include_bytes!(concat!(env!("OUT_DIR"), "/COMPILER_SNAPSHOT.bin"));
|
|
|
|
|
2023-02-13 20:46:32 -05:00
|
|
|
// NOTE(bartlomieju): Compressing the TSC snapshot in debug build took
|
|
|
|
// ~45s on M1 MacBook Pro; without compression it took ~1s.
|
|
|
|
// Thus we're not not using compressed snapshot, trading off
|
|
|
|
// a lot of build time for some startup time in debug build.
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
return COMPRESSED_COMPILER_SNAPSHOT.to_vec().into_boxed_slice();
|
|
|
|
|
|
|
|
#[cfg(not(debug_assertions))]
|
2022-07-04 18:12:41 -04:00
|
|
|
zstd::bulk::decompress(
|
|
|
|
&COMPRESSED_COMPILER_SNAPSHOT[4..],
|
|
|
|
u32::from_le_bytes(COMPRESSED_COMPILER_SNAPSHOT[0..4].try_into().unwrap())
|
|
|
|
as usize,
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
.into_boxed_slice()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-12-08 11:50:09 -05:00
|
|
|
pub fn get_types_declaration_file_text(unstable: bool) -> String {
|
2023-01-14 09:36:19 -05:00
|
|
|
let mut assets = get_asset_texts_from_new_runtime()
|
|
|
|
.unwrap()
|
|
|
|
.into_iter()
|
|
|
|
.map(|a| (a.specifier, a.text))
|
|
|
|
.collect::<HashMap<_, _>>();
|
|
|
|
|
|
|
|
let mut lib_names = vec![
|
|
|
|
"deno.ns",
|
|
|
|
"deno.console",
|
|
|
|
"deno.url",
|
|
|
|
"deno.web",
|
|
|
|
"deno.fetch",
|
|
|
|
"deno.webgpu",
|
|
|
|
"deno.websocket",
|
|
|
|
"deno.webstorage",
|
|
|
|
"deno.crypto",
|
|
|
|
"deno.broadcast_channel",
|
|
|
|
"deno.net",
|
|
|
|
"deno.shared_globals",
|
|
|
|
"deno.cache",
|
|
|
|
"deno.window",
|
2022-12-08 11:50:09 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
if unstable {
|
2023-01-14 09:36:19 -05:00
|
|
|
lib_names.push("deno.unstable");
|
2022-12-08 11:50:09 -05:00
|
|
|
}
|
|
|
|
|
2023-01-14 09:36:19 -05:00
|
|
|
lib_names
|
|
|
|
.into_iter()
|
|
|
|
.map(|name| {
|
2023-01-27 10:43:16 -05:00
|
|
|
let asset_url = format!("asset:///lib.{name}.d.ts");
|
2023-01-14 09:36:19 -05:00
|
|
|
assets.remove(&asset_url).unwrap()
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join("\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_asset_texts_from_new_runtime() -> Result<Vec<AssetText>, AnyError> {
|
|
|
|
// the assets are stored within the typescript isolate, so take them out of there
|
|
|
|
let mut runtime = JsRuntime::new(RuntimeOptions {
|
|
|
|
startup_snapshot: Some(compiler_snapshot()),
|
|
|
|
extensions: vec![Extension::builder("deno_cli_tsc")
|
|
|
|
.ops(get_tsc_ops())
|
|
|
|
.build()],
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
let global =
|
|
|
|
runtime.execute_script("get_assets.js", "globalThis.getAssets()")?;
|
|
|
|
let scope = &mut runtime.handle_scope();
|
|
|
|
let local = deno_core::v8::Local::new(scope, global);
|
|
|
|
Ok(serde_v8::from_v8::<Vec<AssetText>>(scope, local)?)
|
2022-12-08 11:50:09 -05:00
|
|
|
}
|
|
|
|
|
2022-07-04 18:12:41 -04:00
|
|
|
pub fn compiler_snapshot() -> Snapshot {
|
2022-11-17 20:59:10 -05:00
|
|
|
Snapshot::Static(&COMPILER_SNAPSHOT)
|
2022-07-04 18:12:41 -04:00
|
|
|
}
|
|
|
|
|
2021-02-12 06:49:42 -05:00
|
|
|
macro_rules! inc {
|
|
|
|
($e:expr) => {
|
2022-11-25 18:29:48 -05:00
|
|
|
include_str!(concat!("./dts/", $e))
|
2021-02-12 06:49:42 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-12-18 16:14:42 -05:00
|
|
|
/// Contains static assets that are not preloaded in the compiler snapshot.
|
2023-01-14 09:36:19 -05:00
|
|
|
///
|
|
|
|
/// We lazily load these because putting them in the compiler snapshot will
|
|
|
|
/// increase memory usage when not used (last time checked by about 0.5MB).
|
|
|
|
pub static LAZILY_LOADED_STATIC_ASSETS: Lazy<
|
|
|
|
HashMap<&'static str, &'static str>,
|
|
|
|
> = Lazy::new(|| {
|
|
|
|
([
|
|
|
|
(
|
|
|
|
"lib.dom.asynciterable.d.ts",
|
|
|
|
inc!("lib.dom.asynciterable.d.ts"),
|
|
|
|
),
|
|
|
|
("lib.dom.d.ts", inc!("lib.dom.d.ts")),
|
|
|
|
("lib.dom.extras.d.ts", inc!("lib.dom.extras.d.ts")),
|
|
|
|
("lib.dom.iterable.d.ts", inc!("lib.dom.iterable.d.ts")),
|
|
|
|
("lib.es6.d.ts", inc!("lib.es6.d.ts")),
|
|
|
|
("lib.es2016.full.d.ts", inc!("lib.es2016.full.d.ts")),
|
|
|
|
("lib.es2017.full.d.ts", inc!("lib.es2017.full.d.ts")),
|
|
|
|
("lib.es2018.full.d.ts", inc!("lib.es2018.full.d.ts")),
|
|
|
|
("lib.es2019.full.d.ts", inc!("lib.es2019.full.d.ts")),
|
|
|
|
("lib.es2020.full.d.ts", inc!("lib.es2020.full.d.ts")),
|
|
|
|
("lib.es2021.full.d.ts", inc!("lib.es2021.full.d.ts")),
|
|
|
|
("lib.es2022.full.d.ts", inc!("lib.es2022.full.d.ts")),
|
|
|
|
("lib.esnext.full.d.ts", inc!("lib.esnext.full.d.ts")),
|
|
|
|
("lib.scripthost.d.ts", inc!("lib.scripthost.d.ts")),
|
|
|
|
("lib.webworker.d.ts", inc!("lib.webworker.d.ts")),
|
|
|
|
(
|
|
|
|
"lib.webworker.importscripts.d.ts",
|
|
|
|
inc!("lib.webworker.importscripts.d.ts"),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"lib.webworker.iterable.d.ts",
|
|
|
|
inc!("lib.webworker.iterable.d.ts"),
|
|
|
|
),
|
2023-01-24 09:05:54 -05:00
|
|
|
(
|
|
|
|
// Special file that can be used to inject the @types/node package.
|
|
|
|
// This is used for `node:` specifiers.
|
|
|
|
"node_types.d.ts",
|
|
|
|
"/// <reference types=\"npm:@types/node\" />\n",
|
|
|
|
),
|
2023-01-14 09:36:19 -05:00
|
|
|
])
|
|
|
|
.iter()
|
|
|
|
.cloned()
|
|
|
|
.collect()
|
|
|
|
});
|
2021-02-12 06:49:42 -05:00
|
|
|
|
2022-09-02 10:54:40 -04:00
|
|
|
/// A structure representing stats from a type check operation for a graph.
|
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq)]
|
|
|
|
pub struct Stats(pub Vec<(String, u32)>);
|
|
|
|
|
|
|
|
impl<'de> Deserialize<'de> for Stats {
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
|
|
|
let items: Vec<(String, u32)> = Deserialize::deserialize(deserializer)?;
|
|
|
|
Ok(Stats(items))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for Stats {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
Serialize::serialize(&self.0, serializer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Stats {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
writeln!(f, "Compilation statistics:")?;
|
|
|
|
for (key, value) in self.0.clone() {
|
2023-01-27 10:43:16 -05:00
|
|
|
writeln!(f, " {key}: {value}")?;
|
2022-09-02 10:54:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-14 09:36:19 -05:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct AssetText {
|
|
|
|
pub specifier: String,
|
|
|
|
pub text: String,
|
|
|
|
}
|
|
|
|
|
2021-02-12 06:49:42 -05:00
|
|
|
/// Retrieve a static asset that are included in the binary.
|
2023-01-14 09:36:19 -05:00
|
|
|
fn get_lazily_loaded_asset(asset: &str) -> Option<&'static str> {
|
|
|
|
LAZILY_LOADED_STATIC_ASSETS.get(asset).map(|s| s.to_owned())
|
2020-11-02 14:41:20 -05:00
|
|
|
}
|
|
|
|
|
2020-10-27 20:52:20 -04:00
|
|
|
fn get_maybe_hash(
|
2022-05-20 16:40:55 -04:00
|
|
|
maybe_source: Option<&str>,
|
2020-10-27 20:52:20 -04:00
|
|
|
hash_data: &[Vec<u8>],
|
|
|
|
) -> Option<String> {
|
|
|
|
if let Some(source) = maybe_source {
|
|
|
|
let mut data = vec![source.as_bytes().to_owned()];
|
|
|
|
data.extend_from_slice(hash_data);
|
2022-11-28 17:28:54 -05:00
|
|
|
Some(checksum::gen(&data))
|
2020-10-27 20:52:20 -04:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 17:26:22 -04:00
|
|
|
/// Hash the URL so it can be sent to `tsc` in a supportable way
|
2022-10-21 11:20:18 -04:00
|
|
|
fn hash_url(specifier: &ModuleSpecifier, media_type: MediaType) -> String {
|
2022-11-28 17:28:54 -05:00
|
|
|
let hash = checksum::gen(&[specifier.path().as_bytes()]);
|
2021-10-10 17:26:22 -04:00
|
|
|
format!(
|
|
|
|
"{}:///{}{}",
|
2021-04-07 09:22:14 -04:00
|
|
|
specifier.scheme(),
|
2021-10-10 17:26:22 -04:00
|
|
|
hash,
|
|
|
|
media_type.as_ts_extension()
|
|
|
|
)
|
2021-04-07 09:22:14 -04:00
|
|
|
}
|
|
|
|
|
2022-01-31 04:32:49 -05:00
|
|
|
/// If the provided URLs derivable tsc media type doesn't match the media type,
|
|
|
|
/// we will add an extension to the output. This is to avoid issues with
|
|
|
|
/// specifiers that don't have extensions, that tsc refuses to emit because they
|
|
|
|
/// think a `.js` version exists, when it doesn't.
|
|
|
|
fn maybe_remap_specifier(
|
|
|
|
specifier: &ModuleSpecifier,
|
2022-10-21 11:20:18 -04:00
|
|
|
media_type: MediaType,
|
2022-01-31 04:32:49 -05:00
|
|
|
) -> Option<String> {
|
|
|
|
let path = if specifier.scheme() == "file" {
|
|
|
|
if let Ok(path) = specifier.to_file_path() {
|
|
|
|
path
|
|
|
|
} else {
|
|
|
|
PathBuf::from(specifier.path())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PathBuf::from(specifier.path())
|
|
|
|
};
|
|
|
|
if path.extension().is_none() {
|
|
|
|
Some(format!("{}{}", specifier, media_type.as_ts_extension()))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 18:10:41 -05:00
|
|
|
/// tsc only supports `.ts`, `.tsx`, `.d.ts`, `.js`, or `.jsx` as root modules
|
|
|
|
/// and so we have to detect the apparent media type based on extensions it
|
|
|
|
/// supports.
|
|
|
|
fn get_tsc_media_type(specifier: &ModuleSpecifier) -> MediaType {
|
2021-02-17 13:47:18 -05:00
|
|
|
let path = if specifier.scheme() == "file" {
|
|
|
|
if let Ok(path) = specifier.to_file_path() {
|
2020-11-09 18:10:41 -05:00
|
|
|
path
|
|
|
|
} else {
|
2021-02-17 13:47:18 -05:00
|
|
|
PathBuf::from(specifier.path())
|
2020-11-09 18:10:41 -05:00
|
|
|
}
|
|
|
|
} else {
|
2021-02-17 13:47:18 -05:00
|
|
|
PathBuf::from(specifier.path())
|
2020-11-09 18:10:41 -05:00
|
|
|
};
|
|
|
|
match path.extension() {
|
|
|
|
None => MediaType::Unknown,
|
|
|
|
Some(os_str) => match os_str.to_str() {
|
|
|
|
Some("ts") => {
|
|
|
|
if let Some(os_str) = path.file_stem() {
|
|
|
|
if let Some(file_name) = os_str.to_str() {
|
|
|
|
if file_name.ends_with(".d") {
|
|
|
|
return MediaType::Dts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MediaType::TypeScript
|
|
|
|
}
|
2021-11-01 16:22:27 -04:00
|
|
|
Some("mts") => {
|
|
|
|
if let Some(os_str) = path.file_stem() {
|
|
|
|
if let Some(file_name) = os_str.to_str() {
|
|
|
|
if file_name.ends_with(".d") {
|
2021-12-09 17:12:21 -05:00
|
|
|
return MediaType::Dmts;
|
2021-11-01 16:22:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-09 17:12:21 -05:00
|
|
|
MediaType::Mts
|
2021-11-01 16:22:27 -04:00
|
|
|
}
|
|
|
|
Some("cts") => {
|
|
|
|
if let Some(os_str) = path.file_stem() {
|
|
|
|
if let Some(file_name) = os_str.to_str() {
|
|
|
|
if file_name.ends_with(".d") {
|
2021-12-09 17:12:21 -05:00
|
|
|
return MediaType::Dcts;
|
2021-11-01 16:22:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-09 17:12:21 -05:00
|
|
|
MediaType::Cts
|
2021-11-01 16:22:27 -04:00
|
|
|
}
|
2021-03-25 14:17:37 -04:00
|
|
|
Some("tsx") => MediaType::Tsx,
|
2020-11-09 18:10:41 -05:00
|
|
|
Some("js") => MediaType::JavaScript,
|
2021-12-09 17:12:21 -05:00
|
|
|
Some("mjs") => MediaType::Mjs,
|
|
|
|
Some("cjs") => MediaType::Cjs,
|
2021-03-25 14:17:37 -04:00
|
|
|
Some("jsx") => MediaType::Jsx,
|
2020-11-09 18:10:41 -05:00
|
|
|
_ => MediaType::Unknown,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 19:52:49 -04:00
|
|
|
#[derive(Debug, Clone, Default, Eq, PartialEq)]
|
|
|
|
pub struct EmittedFile {
|
|
|
|
pub data: String,
|
|
|
|
pub maybe_specifiers: Option<Vec<ModuleSpecifier>>,
|
|
|
|
pub media_type: MediaType,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A structure representing a request to be sent to the tsc runtime.
|
2020-10-25 16:17:58 -04:00
|
|
|
#[derive(Debug)]
|
2020-10-13 19:52:49 -04:00
|
|
|
pub struct Request {
|
|
|
|
/// The TypeScript compiler options which will be serialized and sent to
|
|
|
|
/// tsc.
|
|
|
|
pub config: TsConfig,
|
|
|
|
/// Indicates to the tsc runtime if debug logging should occur.
|
|
|
|
pub debug: bool,
|
2023-02-09 22:00:23 -05:00
|
|
|
pub graph: Arc<ModuleGraph>,
|
2020-10-13 19:52:49 -04:00
|
|
|
pub hash_data: Vec<Vec<u8>>,
|
2021-06-21 17:18:32 -04:00
|
|
|
pub maybe_config_specifier: Option<ModuleSpecifier>,
|
2022-10-21 11:20:18 -04:00
|
|
|
pub maybe_npm_resolver: Option<NpmPackageResolver>,
|
2020-10-13 19:52:49 -04:00
|
|
|
pub maybe_tsbuildinfo: Option<String>,
|
|
|
|
/// A vector of strings that represent the root/entry point modules for the
|
|
|
|
/// program.
|
2020-10-25 16:17:58 -04:00
|
|
|
pub root_names: Vec<(ModuleSpecifier, MediaType)>,
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2022-03-23 09:54:22 -04:00
|
|
|
pub struct Response {
|
2020-10-13 19:52:49 -04:00
|
|
|
/// Any diagnostics that have been returned from the checker.
|
|
|
|
pub diagnostics: Diagnostics,
|
|
|
|
/// If there was any build info associated with the exec request.
|
|
|
|
pub maybe_tsbuildinfo: Option<String>,
|
|
|
|
/// Statistics from the check.
|
2022-09-02 10:54:40 -04:00
|
|
|
pub stats: Stats,
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
|
2022-10-21 11:20:18 -04:00
|
|
|
#[derive(Debug, Default)]
|
2020-10-13 19:52:49 -04:00
|
|
|
struct State {
|
|
|
|
hash_data: Vec<Vec<u8>>,
|
2023-02-09 22:00:23 -05:00
|
|
|
graph: Arc<ModuleGraph>,
|
2021-06-21 17:18:32 -04:00
|
|
|
maybe_config_specifier: Option<ModuleSpecifier>,
|
2020-10-13 19:52:49 -04:00
|
|
|
maybe_tsbuildinfo: Option<String>,
|
|
|
|
maybe_response: Option<RespondArgs>,
|
2022-10-21 11:20:18 -04:00
|
|
|
maybe_npm_resolver: Option<NpmPackageResolver>,
|
2022-01-31 04:32:49 -05:00
|
|
|
remapped_specifiers: HashMap<String, ModuleSpecifier>,
|
2020-10-25 16:17:58 -04:00
|
|
|
root_map: HashMap<String, ModuleSpecifier>,
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl State {
|
|
|
|
pub fn new(
|
2023-02-09 22:00:23 -05:00
|
|
|
graph: Arc<ModuleGraph>,
|
2020-10-13 19:52:49 -04:00
|
|
|
hash_data: Vec<Vec<u8>>,
|
2021-06-21 17:18:32 -04:00
|
|
|
maybe_config_specifier: Option<ModuleSpecifier>,
|
2022-10-21 11:20:18 -04:00
|
|
|
maybe_npm_resolver: Option<NpmPackageResolver>,
|
2020-10-13 19:52:49 -04:00
|
|
|
maybe_tsbuildinfo: Option<String>,
|
2020-10-25 16:17:58 -04:00
|
|
|
root_map: HashMap<String, ModuleSpecifier>,
|
2022-01-31 04:32:49 -05:00
|
|
|
remapped_specifiers: HashMap<String, ModuleSpecifier>,
|
2020-10-13 19:52:49 -04:00
|
|
|
) -> Self {
|
|
|
|
State {
|
|
|
|
hash_data,
|
2023-02-09 22:00:23 -05:00
|
|
|
graph,
|
2021-06-21 17:18:32 -04:00
|
|
|
maybe_config_specifier,
|
2022-10-21 11:20:18 -04:00
|
|
|
maybe_npm_resolver,
|
2020-10-13 19:52:49 -04:00
|
|
|
maybe_tsbuildinfo,
|
|
|
|
maybe_response: None,
|
2022-01-31 04:32:49 -05:00
|
|
|
remapped_specifiers,
|
2020-10-25 16:17:58 -04:00
|
|
|
root_map,
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 17:18:32 -04:00
|
|
|
fn normalize_specifier(specifier: &str) -> Result<ModuleSpecifier, AnyError> {
|
|
|
|
resolve_url_or_path(&specifier.replace(".d.ts.d.ts", ".d.ts"))
|
|
|
|
.map_err(|err| err.into())
|
|
|
|
}
|
|
|
|
|
2020-10-13 19:52:49 -04:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct CreateHashArgs {
|
|
|
|
/// The string data to be used to generate the hash. This will be mixed with
|
|
|
|
/// other state data in Deno to derive the final hash.
|
|
|
|
data: String,
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-03-14 18:38:53 -04:00
|
|
|
fn op_create_hash(s: &mut OpState, args: Value) -> Result<Value, AnyError> {
|
2022-03-14 13:44:15 -04:00
|
|
|
let state = s.borrow_mut::<State>();
|
2020-10-13 19:52:49 -04:00
|
|
|
let v: CreateHashArgs = serde_json::from_value(args)
|
|
|
|
.context("Invalid request from JavaScript for \"op_create_hash\".")?;
|
|
|
|
let mut data = vec![v.data.as_bytes().to_owned()];
|
|
|
|
data.extend_from_slice(&state.hash_data);
|
2022-11-28 17:28:54 -05:00
|
|
|
let hash = checksum::gen(&data);
|
2020-10-13 19:52:49 -04:00
|
|
|
Ok(json!({ "hash": hash }))
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-07-26 13:34:08 -04:00
|
|
|
fn op_cwd(s: &mut OpState) -> Result<String, AnyError> {
|
2022-03-14 13:44:15 -04:00
|
|
|
let state = s.borrow_mut::<State>();
|
2021-06-21 17:18:32 -04:00
|
|
|
if let Some(config_specifier) = &state.maybe_config_specifier {
|
|
|
|
let cwd = config_specifier.join("./")?;
|
|
|
|
Ok(cwd.to_string())
|
|
|
|
} else {
|
|
|
|
Ok("cache:///".to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 19:52:49 -04:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct EmitArgs {
|
|
|
|
/// The text data/contents of the file.
|
|
|
|
data: String,
|
|
|
|
/// The _internal_ filename for the file. This will be used to determine how
|
|
|
|
/// the file is cached and stored.
|
|
|
|
file_name: String,
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-05-13 04:36:31 -04:00
|
|
|
fn op_emit(state: &mut OpState, args: EmitArgs) -> bool {
|
2022-03-14 13:44:15 -04:00
|
|
|
let state = state.borrow_mut::<State>();
|
2022-02-02 14:48:54 -05:00
|
|
|
match args.file_name.as_ref() {
|
2023-02-05 11:49:20 -05:00
|
|
|
"internal:///.tsbuildinfo" => state.maybe_tsbuildinfo = Some(args.data),
|
2022-02-02 14:48:54 -05:00
|
|
|
_ => {
|
2022-07-12 18:58:39 -04:00
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
panic!("Unhandled emit write: {}", args.file_name);
|
|
|
|
}
|
2022-02-02 14:48:54 -05:00
|
|
|
}
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
|
2022-05-13 04:36:31 -04:00
|
|
|
true
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
|
2021-06-21 17:18:32 -04:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
struct ExistsArgs {
|
|
|
|
/// The fully qualified specifier that should be loaded.
|
|
|
|
specifier: String,
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-05-13 04:36:31 -04:00
|
|
|
fn op_exists(state: &mut OpState, args: ExistsArgs) -> bool {
|
2022-03-14 13:44:15 -04:00
|
|
|
let state = state.borrow_mut::<State>();
|
2023-02-09 22:00:23 -05:00
|
|
|
let graph = &state.graph;
|
2021-06-21 17:18:32 -04:00
|
|
|
if let Ok(specifier) = normalize_specifier(&args.specifier) {
|
|
|
|
if specifier.scheme() == "asset" || specifier.scheme() == "data" {
|
2022-05-13 04:36:31 -04:00
|
|
|
true
|
2021-06-21 17:18:32 -04:00
|
|
|
} else {
|
2023-02-09 22:00:23 -05:00
|
|
|
graph.get(&specifier).is_some()
|
2021-06-21 17:18:32 -04:00
|
|
|
}
|
|
|
|
} else {
|
2022-05-13 04:36:31 -04:00
|
|
|
false
|
2021-06-21 17:18:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 19:52:49 -04:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
struct LoadArgs {
|
|
|
|
/// The fully qualified specifier that should be loaded.
|
|
|
|
specifier: String,
|
|
|
|
}
|
|
|
|
|
2022-10-21 11:20:18 -04:00
|
|
|
pub fn as_ts_script_kind(media_type: MediaType) -> i32 {
|
2021-12-15 13:22:36 -05:00
|
|
|
match media_type {
|
|
|
|
MediaType::JavaScript => 1,
|
|
|
|
MediaType::Jsx => 2,
|
|
|
|
MediaType::Mjs => 1,
|
|
|
|
MediaType::Cjs => 1,
|
|
|
|
MediaType::TypeScript => 3,
|
|
|
|
MediaType::Mts => 3,
|
|
|
|
MediaType::Cts => 3,
|
|
|
|
MediaType::Dts => 3,
|
|
|
|
MediaType::Dmts => 3,
|
|
|
|
MediaType::Dcts => 3,
|
|
|
|
MediaType::Tsx => 4,
|
|
|
|
MediaType::Json => 6,
|
2022-10-21 11:20:18 -04:00
|
|
|
MediaType::SourceMap
|
|
|
|
| MediaType::TsBuildInfo
|
|
|
|
| MediaType::Wasm
|
|
|
|
| MediaType::Unknown => 0,
|
2021-12-15 13:22:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-03-14 18:38:53 -04:00
|
|
|
fn op_load(state: &mut OpState, args: Value) -> Result<Value, AnyError> {
|
2022-03-14 13:44:15 -04:00
|
|
|
let state = state.borrow_mut::<State>();
|
2020-10-13 19:52:49 -04:00
|
|
|
let v: LoadArgs = serde_json::from_value(args)
|
|
|
|
.context("Invalid request from JavaScript for \"op_load\".")?;
|
2021-06-21 17:18:32 -04:00
|
|
|
let specifier = normalize_specifier(&v.specifier)
|
2020-10-13 19:52:49 -04:00
|
|
|
.context("Error converting a string module specifier for \"op_load\".")?;
|
|
|
|
let mut hash: Option<String> = None;
|
2020-10-22 20:50:15 -04:00
|
|
|
let mut media_type = MediaType::Unknown;
|
2023-02-09 22:00:23 -05:00
|
|
|
let graph = &state.graph;
|
2023-02-05 11:49:20 -05:00
|
|
|
let data = if &v.specifier == "internal:///.tsbuildinfo" {
|
2022-10-21 11:20:18 -04:00
|
|
|
state.maybe_tsbuildinfo.as_deref().map(Cow::Borrowed)
|
2020-10-22 20:50:15 -04:00
|
|
|
// in certain situations we return a "blank" module to tsc and we need to
|
|
|
|
// handle the request for that module here.
|
2023-02-05 11:49:20 -05:00
|
|
|
} else if &v.specifier == "internal:///missing_dependency.d.ts" {
|
2020-10-22 20:50:15 -04:00
|
|
|
hash = Some("1".to_string());
|
2021-01-27 14:54:20 -05:00
|
|
|
media_type = MediaType::Dts;
|
2022-10-21 11:20:18 -04:00
|
|
|
Some(Cow::Borrowed("declare const __: any;\nexport = __;\n"))
|
2023-01-14 09:36:19 -05:00
|
|
|
} else if let Some(name) = v.specifier.strip_prefix("asset:///") {
|
|
|
|
let maybe_source = get_lazily_loaded_asset(name);
|
2022-05-20 16:40:55 -04:00
|
|
|
hash = get_maybe_hash(maybe_source, &state.hash_data);
|
2020-10-27 20:52:20 -04:00
|
|
|
media_type = MediaType::from(&v.specifier);
|
2022-10-21 11:20:18 -04:00
|
|
|
maybe_source.map(Cow::Borrowed)
|
2020-10-13 19:52:49 -04:00
|
|
|
} else {
|
2022-01-31 04:32:49 -05:00
|
|
|
let specifier = if let Some(remapped_specifier) =
|
|
|
|
state.remapped_specifiers.get(&v.specifier)
|
2021-01-05 21:22:38 -05:00
|
|
|
{
|
2023-01-05 12:33:09 -05:00
|
|
|
remapped_specifier
|
2021-01-05 21:22:38 -05:00
|
|
|
} else if let Some(remapped_specifier) = state.root_map.get(&v.specifier) {
|
2023-01-05 12:33:09 -05:00
|
|
|
remapped_specifier
|
2021-01-05 21:22:38 -05:00
|
|
|
} else {
|
2023-01-05 12:33:09 -05:00
|
|
|
&specifier
|
2021-01-05 21:22:38 -05:00
|
|
|
};
|
2023-02-09 22:00:23 -05:00
|
|
|
let maybe_source = if let Some(module) = graph.get(specifier) {
|
2023-02-22 14:15:25 -05:00
|
|
|
match module {
|
|
|
|
Module::Esm(module) => {
|
|
|
|
media_type = module.media_type;
|
|
|
|
Some(Cow::Borrowed(&*module.source))
|
|
|
|
}
|
|
|
|
Module::Json(module) => {
|
|
|
|
media_type = MediaType::Json;
|
|
|
|
Some(Cow::Borrowed(&*module.source))
|
|
|
|
}
|
2023-02-23 10:58:10 -05:00
|
|
|
Module::Npm(_) | Module::Node(_) => None,
|
|
|
|
Module::External(module) => {
|
|
|
|
// means it's Deno code importing an npm module
|
2023-02-24 19:35:43 -05:00
|
|
|
let specifier =
|
|
|
|
node::resolve_specifier_into_node_modules(&module.specifier);
|
|
|
|
media_type = MediaType::from(&specifier);
|
2023-02-23 10:58:10 -05:00
|
|
|
let file_path = specifier.to_file_path().unwrap();
|
|
|
|
let code =
|
|
|
|
std::fs::read_to_string(&file_path).with_context(|| {
|
|
|
|
format!("Unable to load {}", file_path.display())
|
|
|
|
})?;
|
|
|
|
Some(Cow::Owned(code))
|
|
|
|
}
|
2023-02-22 14:15:25 -05:00
|
|
|
}
|
2022-10-21 11:20:18 -04:00
|
|
|
} else if state
|
|
|
|
.maybe_npm_resolver
|
|
|
|
.as_ref()
|
2023-01-05 12:33:09 -05:00
|
|
|
.map(|resolver| resolver.in_npm_package(specifier))
|
2022-10-21 11:20:18 -04:00
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
2023-01-05 12:33:09 -05:00
|
|
|
media_type = MediaType::from(specifier);
|
2022-10-21 11:20:18 -04:00
|
|
|
let file_path = specifier.to_file_path().unwrap();
|
|
|
|
let code = std::fs::read_to_string(&file_path)
|
|
|
|
.with_context(|| format!("Unable to load {}", file_path.display()))?;
|
|
|
|
Some(Cow::Owned(code))
|
2020-10-22 20:50:15 -04:00
|
|
|
} else {
|
2021-10-10 17:26:22 -04:00
|
|
|
media_type = MediaType::Unknown;
|
|
|
|
None
|
2020-10-22 20:50:15 -04:00
|
|
|
};
|
2022-10-21 11:20:18 -04:00
|
|
|
hash = get_maybe_hash(maybe_source.as_deref(), &state.hash_data);
|
2020-10-13 19:52:49 -04:00
|
|
|
maybe_source
|
|
|
|
};
|
|
|
|
|
2022-10-21 11:20:18 -04:00
|
|
|
Ok(json!({
|
|
|
|
"data": data,
|
|
|
|
"version": hash,
|
|
|
|
"scriptKind": as_ts_script_kind(media_type),
|
|
|
|
}))
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
|
2021-05-11 00:54:10 -04:00
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
2020-10-13 19:52:49 -04:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2020-12-07 05:46:39 -05:00
|
|
|
pub struct ResolveArgs {
|
2020-10-13 19:52:49 -04:00
|
|
|
/// The base specifier that the supplied specifier strings should be resolved
|
|
|
|
/// relative to.
|
2020-12-07 05:46:39 -05:00
|
|
|
pub base: String,
|
2020-10-13 19:52:49 -04:00
|
|
|
/// A list of specifiers that should be resolved.
|
2020-12-07 05:46:39 -05:00
|
|
|
pub specifiers: Vec<String>,
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
|
|
|
fn op_resolve(
|
|
|
|
state: &mut OpState,
|
|
|
|
args: ResolveArgs,
|
2022-05-13 04:36:31 -04:00
|
|
|
) -> Result<Vec<(String, String)>, AnyError> {
|
2022-03-14 13:44:15 -04:00
|
|
|
let state = state.borrow_mut::<State>();
|
2022-12-05 20:09:31 -05:00
|
|
|
let mut resolved: Vec<(String, String)> =
|
|
|
|
Vec::with_capacity(args.specifiers.len());
|
2022-01-31 04:32:49 -05:00
|
|
|
let referrer = if let Some(remapped_specifier) =
|
|
|
|
state.remapped_specifiers.get(&args.base)
|
|
|
|
{
|
|
|
|
remapped_specifier.clone()
|
|
|
|
} else if let Some(remapped_base) = state.root_map.get(&args.base) {
|
2020-10-25 16:17:58 -04:00
|
|
|
remapped_base.clone()
|
|
|
|
} else {
|
2022-01-31 04:32:49 -05:00
|
|
|
normalize_specifier(&args.base).context(
|
2020-10-25 16:17:58 -04:00
|
|
|
"Error converting a string module specifier for \"op_resolve\".",
|
|
|
|
)?
|
|
|
|
};
|
2023-01-24 09:05:54 -05:00
|
|
|
for specifier in args.specifiers {
|
|
|
|
if let Some(module_name) = specifier.strip_prefix("node:") {
|
|
|
|
if crate::node::resolve_builtin_node_module(module_name).is_ok() {
|
|
|
|
// return itself for node: specifiers because during type checking
|
|
|
|
// we resolve to the ambient modules in the @types/node package
|
|
|
|
// rather than deno_std/node
|
|
|
|
resolved.push((specifier, MediaType::Dts.to_string()));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 19:52:49 -04:00
|
|
|
if specifier.starts_with("asset:///") {
|
2023-01-24 09:05:54 -05:00
|
|
|
let media_type =
|
|
|
|
MediaType::from(&specifier).as_ts_extension().to_string();
|
|
|
|
resolved.push((specifier, media_type));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-02-09 22:00:23 -05:00
|
|
|
let graph = &state.graph;
|
2023-02-22 14:15:25 -05:00
|
|
|
let resolved_dep = graph
|
|
|
|
.get(&referrer)
|
|
|
|
.and_then(|m| m.esm())
|
|
|
|
.and_then(|m| m.dependencies.get(&specifier))
|
|
|
|
.and_then(|d| d.maybe_type.ok().or_else(|| d.maybe_code.ok()));
|
2023-02-09 22:00:23 -05:00
|
|
|
|
2023-01-24 09:05:54 -05:00
|
|
|
let maybe_result = match resolved_dep {
|
2023-02-09 22:00:23 -05:00
|
|
|
Some(ResolutionResolved { specifier, .. }) => {
|
2023-02-22 14:15:25 -05:00
|
|
|
resolve_graph_specifier_types(specifier, state)?
|
2023-01-24 09:05:54 -05:00
|
|
|
}
|
2023-02-22 14:15:25 -05:00
|
|
|
_ => resolve_non_graph_specifier_types(&specifier, &referrer, state)?,
|
2023-01-24 09:05:54 -05:00
|
|
|
};
|
|
|
|
let result = match maybe_result {
|
|
|
|
Some((specifier, media_type)) => {
|
|
|
|
let specifier_str = match specifier.scheme() {
|
|
|
|
"data" | "blob" => {
|
|
|
|
let specifier_str = hash_url(&specifier, media_type);
|
|
|
|
state
|
|
|
|
.remapped_specifiers
|
|
|
|
.insert(specifier_str.clone(), specifier);
|
|
|
|
specifier_str
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
if let Some(specifier_str) =
|
|
|
|
maybe_remap_specifier(&specifier, media_type)
|
|
|
|
{
|
2022-01-31 04:32:49 -05:00
|
|
|
state
|
|
|
|
.remapped_specifiers
|
|
|
|
.insert(specifier_str.clone(), specifier);
|
2021-12-16 05:45:41 -05:00
|
|
|
specifier_str
|
2023-01-24 09:05:54 -05:00
|
|
|
} else {
|
|
|
|
specifier.to_string()
|
2021-12-16 05:45:41 -05:00
|
|
|
}
|
2023-01-24 09:05:54 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
(specifier_str, media_type.as_ts_extension().into())
|
|
|
|
}
|
|
|
|
None => (
|
2023-02-05 11:49:20 -05:00
|
|
|
"internal:///missing_dependency.d.ts".to_string(),
|
2023-01-24 09:05:54 -05:00
|
|
|
".d.ts".to_string(),
|
|
|
|
),
|
|
|
|
};
|
|
|
|
log::debug!("Resolved {} to {:?}", specifier, result);
|
|
|
|
resolved.push(result);
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
|
2022-05-13 04:36:31 -04:00
|
|
|
Ok(resolved)
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
|
2023-02-22 14:15:25 -05:00
|
|
|
fn resolve_graph_specifier_types(
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
state: &State,
|
|
|
|
) -> Result<Option<(ModuleSpecifier, MediaType)>, AnyError> {
|
|
|
|
let graph = &state.graph;
|
|
|
|
let maybe_module = graph.get(specifier);
|
|
|
|
// follow the types reference directive, which may be pointing at an npm package
|
|
|
|
let maybe_module = match maybe_module {
|
|
|
|
Some(Module::Esm(module)) => {
|
|
|
|
let maybe_types_dep = module
|
|
|
|
.maybe_types_dependency
|
|
|
|
.as_ref()
|
|
|
|
.map(|d| &d.dependency);
|
|
|
|
match maybe_types_dep.and_then(|d| d.maybe_specifier()) {
|
|
|
|
Some(specifier) => graph.get(specifier),
|
|
|
|
_ => maybe_module,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
maybe_module => maybe_module,
|
|
|
|
};
|
|
|
|
|
|
|
|
// now get the types from the resolved module
|
|
|
|
match maybe_module {
|
|
|
|
Some(Module::Esm(module)) => {
|
|
|
|
Ok(Some((module.specifier.clone(), module.media_type)))
|
|
|
|
}
|
|
|
|
Some(Module::Json(module)) => {
|
|
|
|
Ok(Some((module.specifier.clone(), module.media_type)))
|
|
|
|
}
|
|
|
|
Some(Module::Npm(module)) => {
|
|
|
|
if let Some(npm_resolver) = &state.maybe_npm_resolver {
|
|
|
|
resolve_npm_package_reference_types(&module.nv_reference, npm_resolver)
|
|
|
|
.map(Some)
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
2023-02-23 10:58:10 -05:00
|
|
|
Some(Module::External(module)) => {
|
|
|
|
// we currently only use "External" for when the module is in an npm package
|
|
|
|
Ok(state.maybe_npm_resolver.as_ref().map(|npm_resolver| {
|
2023-02-24 19:35:43 -05:00
|
|
|
let specifier =
|
|
|
|
node::resolve_specifier_into_node_modules(&module.specifier);
|
2023-02-23 10:58:10 -05:00
|
|
|
NodeResolution::into_specifier_and_media_type(
|
2023-02-24 19:35:43 -05:00
|
|
|
node::url_to_node_resolution(specifier, npm_resolver).ok(),
|
2023-02-23 10:58:10 -05:00
|
|
|
)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
Some(Module::Node(_)) | None => Ok(None),
|
2023-02-22 14:15:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_non_graph_specifier_types(
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &ModuleSpecifier,
|
|
|
|
state: &State,
|
|
|
|
) -> Result<Option<(ModuleSpecifier, MediaType)>, AnyError> {
|
|
|
|
let npm_resolver = match state.maybe_npm_resolver.as_ref() {
|
|
|
|
Some(npm_resolver) => npm_resolver,
|
|
|
|
None => return Ok(None), // we only support non-graph types for npm packages
|
|
|
|
};
|
|
|
|
if npm_resolver.in_npm_package(referrer) {
|
|
|
|
// we're in an npm package, so use node resolution
|
|
|
|
Ok(Some(NodeResolution::into_specifier_and_media_type(
|
|
|
|
node::node_resolve(
|
|
|
|
specifier,
|
|
|
|
referrer,
|
|
|
|
NodeResolutionMode::Types,
|
|
|
|
npm_resolver,
|
|
|
|
&mut PermissionsContainer::allow_all(),
|
|
|
|
)
|
|
|
|
.ok()
|
|
|
|
.flatten(),
|
|
|
|
)))
|
|
|
|
} else if let Ok(npm_ref) = NpmPackageReqReference::from_str(specifier) {
|
|
|
|
// todo(dsherret): add support for injecting this in the graph so
|
|
|
|
// we don't need this special code here.
|
|
|
|
// This could occur when resolving npm:@types/node when it is
|
|
|
|
// injected and not part of the graph
|
|
|
|
let node_id = npm_resolver
|
|
|
|
.resolution()
|
|
|
|
.resolve_pkg_id_from_pkg_req(&npm_ref.req)?;
|
|
|
|
let npm_id_ref = NpmPackageNvReference {
|
|
|
|
nv: node_id.nv,
|
|
|
|
sub_path: npm_ref.sub_path,
|
|
|
|
};
|
|
|
|
resolve_npm_package_reference_types(&npm_id_ref, npm_resolver).map(Some)
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-21 11:20:18 -04:00
|
|
|
pub fn resolve_npm_package_reference_types(
|
2023-02-22 14:15:25 -05:00
|
|
|
npm_ref: &NpmPackageNvReference,
|
2022-10-21 11:20:18 -04:00
|
|
|
npm_resolver: &NpmPackageResolver,
|
|
|
|
) -> Result<(ModuleSpecifier, MediaType), AnyError> {
|
|
|
|
let maybe_resolution = node_resolve_npm_reference(
|
|
|
|
npm_ref,
|
|
|
|
NodeResolutionMode::Types,
|
|
|
|
npm_resolver,
|
2023-01-10 08:35:44 -05:00
|
|
|
&mut PermissionsContainer::allow_all(),
|
2022-10-21 11:20:18 -04:00
|
|
|
)?;
|
|
|
|
Ok(NodeResolution::into_specifier_and_media_type(
|
|
|
|
maybe_resolution,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[op]
|
2023-03-03 08:34:10 -05:00
|
|
|
fn op_is_node_file(state: &mut OpState, path: &str) -> bool {
|
2022-10-21 11:20:18 -04:00
|
|
|
let state = state.borrow::<State>();
|
2023-03-03 08:34:10 -05:00
|
|
|
match ModuleSpecifier::parse(path) {
|
2022-10-21 11:20:18 -04:00
|
|
|
Ok(specifier) => state
|
|
|
|
.maybe_npm_resolver
|
|
|
|
.as_ref()
|
|
|
|
.map(|r| r.in_npm_package(&specifier))
|
|
|
|
.unwrap_or(false),
|
|
|
|
Err(_) => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 19:52:49 -04:00
|
|
|
#[derive(Debug, Deserialize, Eq, PartialEq)]
|
2020-10-22 20:50:15 -04:00
|
|
|
struct RespondArgs {
|
2020-10-13 19:52:49 -04:00
|
|
|
pub diagnostics: Diagnostics,
|
2022-09-02 10:54:40 -04:00
|
|
|
pub stats: Stats,
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-03-14 18:38:53 -04:00
|
|
|
fn op_respond(state: &mut OpState, args: Value) -> Result<Value, AnyError> {
|
2022-03-14 13:44:15 -04:00
|
|
|
let state = state.borrow_mut::<State>();
|
2020-10-13 19:52:49 -04:00
|
|
|
let v: RespondArgs = serde_json::from_value(args)
|
|
|
|
.context("Error converting the result for \"op_respond\".")?;
|
|
|
|
state.maybe_response = Some(v);
|
|
|
|
Ok(json!(true))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Execute a request on the supplied snapshot, returning a response which
|
|
|
|
/// contains information, like any emitted files, diagnostics, statistics and
|
|
|
|
/// optionally an updated TypeScript build info.
|
2022-03-23 09:54:22 -04:00
|
|
|
pub fn exec(request: Request) -> Result<Response, AnyError> {
|
2020-10-25 16:17:58 -04:00
|
|
|
// tsc cannot handle root specifiers that don't have one of the "acceptable"
|
|
|
|
// extensions. Therefore, we have to check the root modules against their
|
|
|
|
// extensions and remap any that are unacceptable to tsc and add them to the
|
|
|
|
// op state so when requested, we can remap to the original specifier.
|
|
|
|
let mut root_map = HashMap::new();
|
2022-01-31 04:32:49 -05:00
|
|
|
let mut remapped_specifiers = HashMap::new();
|
2020-10-25 16:17:58 -04:00
|
|
|
let root_names: Vec<String> = request
|
|
|
|
.root_names
|
|
|
|
.iter()
|
2021-04-07 09:22:14 -04:00
|
|
|
.map(|(s, mt)| match s.scheme() {
|
|
|
|
"data" | "blob" => {
|
2022-10-21 11:20:18 -04:00
|
|
|
let specifier_str = hash_url(s, *mt);
|
2022-01-31 04:32:49 -05:00
|
|
|
remapped_specifiers.insert(specifier_str.clone(), s.clone());
|
2021-01-05 21:22:38 -05:00
|
|
|
specifier_str
|
2021-04-07 09:22:14 -04:00
|
|
|
}
|
|
|
|
_ => {
|
2021-01-05 21:22:38 -05:00
|
|
|
let ext_media_type = get_tsc_media_type(s);
|
2022-10-21 11:20:18 -04:00
|
|
|
if *mt != ext_media_type {
|
2021-01-05 21:22:38 -05:00
|
|
|
let new_specifier = format!("{}{}", s, mt.as_ts_extension());
|
|
|
|
root_map.insert(new_specifier.clone(), s.clone());
|
|
|
|
new_specifier
|
|
|
|
} else {
|
|
|
|
s.as_str().to_owned()
|
|
|
|
}
|
2020-10-25 16:17:58 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
2022-03-14 13:44:15 -04:00
|
|
|
let mut runtime = JsRuntime::new(RuntimeOptions {
|
2022-07-04 18:12:41 -04:00
|
|
|
startup_snapshot: Some(compiler_snapshot()),
|
2023-01-08 17:48:46 -05:00
|
|
|
extensions: vec![Extension::builder("deno_cli_tsc")
|
2023-01-14 09:36:19 -05:00
|
|
|
.ops(get_tsc_ops())
|
2022-03-14 13:44:15 -04:00
|
|
|
.state(move |state| {
|
|
|
|
state.put(State::new(
|
2023-02-09 22:00:23 -05:00
|
|
|
request.graph.clone(),
|
2022-03-14 13:44:15 -04:00
|
|
|
request.hash_data.clone(),
|
|
|
|
request.maybe_config_specifier.clone(),
|
2022-10-21 11:20:18 -04:00
|
|
|
request.maybe_npm_resolver.clone(),
|
2022-03-14 13:44:15 -04:00
|
|
|
request.maybe_tsbuildinfo.clone(),
|
|
|
|
root_map.clone(),
|
|
|
|
remapped_specifiers.clone(),
|
|
|
|
));
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.build()],
|
|
|
|
..Default::default()
|
|
|
|
});
|
2020-10-13 19:52:49 -04:00
|
|
|
|
|
|
|
let startup_source = "globalThis.startup({ legacyFlag: false })";
|
2020-10-25 16:17:58 -04:00
|
|
|
let request_value = json!({
|
|
|
|
"config": request.config,
|
|
|
|
"debug": request.debug,
|
|
|
|
"rootNames": root_names,
|
|
|
|
});
|
|
|
|
let request_str = request_value.to_string();
|
2023-01-27 10:43:16 -05:00
|
|
|
let exec_source = format!("globalThis.exec({request_str})");
|
2020-10-13 19:52:49 -04:00
|
|
|
|
|
|
|
runtime
|
2021-06-21 19:45:41 -04:00
|
|
|
.execute_script(&located_script_name!(), startup_source)
|
2020-10-13 19:52:49 -04:00
|
|
|
.context("Could not properly start the compiler runtime.")?;
|
2021-06-21 19:45:41 -04:00
|
|
|
runtime.execute_script(&located_script_name!(), &exec_source)?;
|
2020-10-13 19:52:49 -04:00
|
|
|
|
|
|
|
let op_state = runtime.op_state();
|
|
|
|
let mut op_state = op_state.borrow_mut();
|
|
|
|
let state = op_state.take::<State>();
|
|
|
|
|
|
|
|
if let Some(response) = state.maybe_response {
|
|
|
|
let diagnostics = response.diagnostics;
|
|
|
|
let maybe_tsbuildinfo = state.maybe_tsbuildinfo;
|
|
|
|
let stats = response.stats;
|
|
|
|
|
|
|
|
Ok(Response {
|
|
|
|
diagnostics,
|
|
|
|
maybe_tsbuildinfo,
|
|
|
|
stats,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Err(anyhow!("The response for the exec request was not set."))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-14 09:36:19 -05:00
|
|
|
fn get_tsc_ops() -> Vec<deno_core::OpDecl> {
|
|
|
|
vec![
|
|
|
|
op_cwd::decl(),
|
|
|
|
op_create_hash::decl(),
|
|
|
|
op_emit::decl(),
|
|
|
|
op_exists::decl(),
|
|
|
|
op_is_node_file::decl(),
|
|
|
|
op_load::decl(),
|
|
|
|
op_resolve::decl(),
|
|
|
|
op_respond::decl(),
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2020-10-13 19:52:49 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2022-11-25 18:29:48 -05:00
|
|
|
use super::Diagnostic;
|
|
|
|
use super::DiagnosticCategory;
|
2020-10-13 19:52:49 -04:00
|
|
|
use super::*;
|
2022-06-27 16:54:09 -04:00
|
|
|
use crate::args::TsConfig;
|
2021-10-10 17:26:22 -04:00
|
|
|
use deno_core::futures::future;
|
2022-03-14 13:44:15 -04:00
|
|
|
use deno_core::OpState;
|
2023-02-09 22:00:23 -05:00
|
|
|
use deno_graph::ModuleGraph;
|
2021-10-10 17:26:22 -04:00
|
|
|
use std::fs;
|
|
|
|
|
|
|
|
#[derive(Debug, Default)]
|
2022-03-23 09:54:22 -04:00
|
|
|
pub struct MockLoader {
|
2021-10-10 17:26:22 -04:00
|
|
|
pub fixtures: PathBuf,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl deno_graph::source::Loader for MockLoader {
|
|
|
|
fn load(
|
|
|
|
&mut self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
_is_dynamic: bool,
|
|
|
|
) -> deno_graph::source::LoadFuture {
|
|
|
|
let specifier_text = specifier
|
|
|
|
.to_string()
|
|
|
|
.replace(":///", "_")
|
|
|
|
.replace("://", "_")
|
2022-02-24 20:03:12 -05:00
|
|
|
.replace('/', "-");
|
2021-10-10 17:26:22 -04:00
|
|
|
let source_path = self.fixtures.join(specifier_text);
|
2022-12-17 17:20:15 -05:00
|
|
|
let response = fs::read_to_string(source_path)
|
2021-10-10 17:26:22 -04:00
|
|
|
.map(|c| {
|
2022-02-15 07:33:46 -05:00
|
|
|
Some(deno_graph::source::LoadResponse::Module {
|
2021-10-10 17:26:22 -04:00
|
|
|
specifier: specifier.clone(),
|
|
|
|
maybe_headers: None,
|
2022-05-20 16:40:55 -04:00
|
|
|
content: c.into(),
|
2021-10-10 17:26:22 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
.map_err(|err| err.into());
|
2022-01-13 11:58:00 -05:00
|
|
|
Box::pin(future::ready(response))
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
|
|
|
}
|
2020-10-13 19:52:49 -04:00
|
|
|
|
|
|
|
async fn setup(
|
|
|
|
maybe_specifier: Option<ModuleSpecifier>,
|
|
|
|
maybe_hash_data: Option<Vec<Vec<u8>>>,
|
|
|
|
maybe_tsbuildinfo: Option<String>,
|
2022-03-14 13:44:15 -04:00
|
|
|
) -> OpState {
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = maybe_specifier
|
|
|
|
.unwrap_or_else(|| resolve_url_or_path("file:///main.ts").unwrap());
|
2020-10-13 19:52:49 -04:00
|
|
|
let hash_data = maybe_hash_data.unwrap_or_else(|| vec![b"".to_vec()]);
|
2021-08-11 10:20:47 -04:00
|
|
|
let fixtures = test_util::testdata_path().join("tsc2");
|
2021-10-10 17:26:22 -04:00
|
|
|
let mut loader = MockLoader { fixtures };
|
2023-02-09 22:00:23 -05:00
|
|
|
let mut graph = ModuleGraph::default();
|
|
|
|
graph
|
|
|
|
.build(vec![specifier], &mut loader, Default::default())
|
|
|
|
.await;
|
2022-03-14 13:44:15 -04:00
|
|
|
let state = State::new(
|
2023-02-09 22:00:23 -05:00
|
|
|
Arc::new(graph),
|
2021-01-05 21:22:38 -05:00
|
|
|
hash_data,
|
2021-06-21 17:18:32 -04:00
|
|
|
None,
|
2022-10-21 11:20:18 -04:00
|
|
|
None,
|
2021-01-05 21:22:38 -05:00
|
|
|
maybe_tsbuildinfo,
|
|
|
|
HashMap::new(),
|
|
|
|
HashMap::new(),
|
2022-03-14 13:44:15 -04:00
|
|
|
);
|
|
|
|
let mut op_state = OpState::new(1);
|
|
|
|
op_state.put(state);
|
|
|
|
op_state
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
|
2020-10-27 20:52:20 -04:00
|
|
|
async fn test_exec(
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<Response, AnyError> {
|
|
|
|
let hash_data = vec![b"something".to_vec()];
|
2021-08-11 10:20:47 -04:00
|
|
|
let fixtures = test_util::testdata_path().join("tsc2");
|
2021-10-10 17:26:22 -04:00
|
|
|
let mut loader = MockLoader { fixtures };
|
2023-02-09 22:00:23 -05:00
|
|
|
let mut graph = ModuleGraph::default();
|
|
|
|
graph
|
|
|
|
.build(vec![specifier.clone()], &mut loader, Default::default())
|
|
|
|
.await;
|
2020-10-27 20:52:20 -04:00
|
|
|
let config = TsConfig::new(json!({
|
|
|
|
"allowJs": true,
|
|
|
|
"checkJs": false,
|
|
|
|
"esModuleInterop": true,
|
|
|
|
"emitDecoratorMetadata": false,
|
|
|
|
"incremental": true,
|
|
|
|
"jsx": "react",
|
|
|
|
"jsxFactory": "React.createElement",
|
|
|
|
"jsxFragmentFactory": "React.Fragment",
|
|
|
|
"lib": ["deno.window"],
|
|
|
|
"module": "esnext",
|
|
|
|
"noEmit": true,
|
2023-02-05 11:49:20 -05:00
|
|
|
"outDir": "internal:///",
|
2020-10-27 20:52:20 -04:00
|
|
|
"strict": true,
|
|
|
|
"target": "esnext",
|
2023-02-05 11:49:20 -05:00
|
|
|
"tsBuildInfoFile": "internal:///.tsbuildinfo",
|
2020-10-27 20:52:20 -04:00
|
|
|
}));
|
|
|
|
let request = Request {
|
|
|
|
config,
|
|
|
|
debug: false,
|
2023-02-09 22:00:23 -05:00
|
|
|
graph: Arc::new(graph),
|
2020-10-27 20:52:20 -04:00
|
|
|
hash_data,
|
2021-06-21 17:18:32 -04:00
|
|
|
maybe_config_specifier: None,
|
2022-10-21 11:20:18 -04:00
|
|
|
maybe_npm_resolver: None,
|
2020-10-27 20:52:20 -04:00
|
|
|
maybe_tsbuildinfo: None,
|
|
|
|
root_names: vec![(specifier.clone(), MediaType::TypeScript)],
|
|
|
|
};
|
2021-01-05 20:38:23 -05:00
|
|
|
exec(request)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-07-04 18:12:41 -04:00
|
|
|
fn test_compiler_snapshot() {
|
2022-07-14 17:52:44 -04:00
|
|
|
let mut js_runtime = JsRuntime::new(RuntimeOptions {
|
2022-07-04 18:12:41 -04:00
|
|
|
startup_snapshot: Some(compiler_snapshot()),
|
2021-01-05 20:38:23 -05:00
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
js_runtime
|
2021-06-21 19:45:41 -04:00
|
|
|
.execute_script(
|
2021-01-05 20:38:23 -05:00
|
|
|
"<anon>",
|
|
|
|
r#"
|
|
|
|
if (!(startup)) {
|
|
|
|
throw Error("bad");
|
|
|
|
}
|
|
|
|
console.log(`ts version: ${ts.version}`);
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-10-27 20:52:20 -04:00
|
|
|
}
|
|
|
|
|
2020-10-13 19:52:49 -04:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_create_hash() {
|
|
|
|
let mut state = setup(None, Some(vec![b"something".to_vec()]), None).await;
|
2022-03-14 13:44:15 -04:00
|
|
|
let actual = op_create_hash::call(
|
|
|
|
&mut state,
|
|
|
|
json!({ "data": "some sort of content" }),
|
|
|
|
)
|
|
|
|
.expect("could not invoke op");
|
2020-10-13 19:52:49 -04:00
|
|
|
assert_eq!(
|
|
|
|
actual,
|
|
|
|
json!({"hash": "ae92df8f104748768838916857a1623b6a3c593110131b0a00f81ad9dac16511"})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-05 21:22:38 -05:00
|
|
|
#[test]
|
2021-10-10 17:26:22 -04:00
|
|
|
fn test_hash_url() {
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = deno_core::resolve_url(
|
2021-01-05 21:22:38 -05:00
|
|
|
"data:application/javascript,console.log(\"Hello%20Deno\");",
|
|
|
|
)
|
|
|
|
.unwrap();
|
2022-10-21 11:20:18 -04:00
|
|
|
assert_eq!(hash_url(&specifier, MediaType::JavaScript), "data:///d300ea0796bd72b08df10348e0b70514c021f2e45bfe59cec24e12e97cd79c58.js");
|
2021-01-05 21:22:38 -05:00
|
|
|
}
|
|
|
|
|
2020-11-09 18:10:41 -05:00
|
|
|
#[test]
|
|
|
|
fn test_get_tsc_media_type() {
|
|
|
|
let fixtures = vec![
|
|
|
|
("file:///a.ts", MediaType::TypeScript),
|
2021-12-09 17:12:21 -05:00
|
|
|
("file:///a.cts", MediaType::Cts),
|
|
|
|
("file:///a.mts", MediaType::Mts),
|
2021-03-25 14:17:37 -04:00
|
|
|
("file:///a.tsx", MediaType::Tsx),
|
2020-11-09 18:10:41 -05:00
|
|
|
("file:///a.d.ts", MediaType::Dts),
|
2021-12-09 17:12:21 -05:00
|
|
|
("file:///a.d.cts", MediaType::Dcts),
|
|
|
|
("file:///a.d.mts", MediaType::Dmts),
|
2020-11-09 18:10:41 -05:00
|
|
|
("file:///a.js", MediaType::JavaScript),
|
2021-03-25 14:17:37 -04:00
|
|
|
("file:///a.jsx", MediaType::Jsx),
|
2021-12-09 17:12:21 -05:00
|
|
|
("file:///a.cjs", MediaType::Cjs),
|
|
|
|
("file:///a.mjs", MediaType::Mjs),
|
2020-11-09 18:10:41 -05:00
|
|
|
("file:///a.json", MediaType::Unknown),
|
|
|
|
("file:///a.wasm", MediaType::Unknown),
|
|
|
|
("file:///a.js.map", MediaType::Unknown),
|
|
|
|
("file:///.tsbuildinfo", MediaType::Unknown),
|
|
|
|
];
|
|
|
|
for (specifier, media_type) in fixtures {
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = resolve_url_or_path(specifier).unwrap();
|
2020-11-09 18:10:41 -05:00
|
|
|
assert_eq!(get_tsc_media_type(&specifier), media_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 19:52:49 -04:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_emit_tsbuildinfo() {
|
|
|
|
let mut state = setup(None, None, None).await;
|
2022-03-14 13:44:15 -04:00
|
|
|
let actual = op_emit::call(
|
2020-10-13 19:52:49 -04:00
|
|
|
&mut state,
|
2022-02-02 14:48:54 -05:00
|
|
|
EmitArgs {
|
|
|
|
data: "some file content".to_string(),
|
2023-02-05 11:49:20 -05:00
|
|
|
file_name: "internal:///.tsbuildinfo".to_string(),
|
2022-02-02 14:48:54 -05:00
|
|
|
},
|
2022-05-13 04:36:31 -04:00
|
|
|
);
|
|
|
|
assert!(actual);
|
2022-03-14 13:44:15 -04:00
|
|
|
let state = state.borrow::<State>();
|
2020-10-13 19:52:49 -04:00
|
|
|
assert_eq!(
|
|
|
|
state.maybe_tsbuildinfo,
|
|
|
|
Some("some file content".to_string())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_load() {
|
|
|
|
let mut state = setup(
|
2021-02-17 13:47:18 -05:00
|
|
|
Some(resolve_url_or_path("https://deno.land/x/mod.ts").unwrap()),
|
2020-10-13 19:52:49 -04:00
|
|
|
None,
|
|
|
|
Some("some content".to_string()),
|
|
|
|
)
|
|
|
|
.await;
|
2022-03-14 13:44:15 -04:00
|
|
|
let actual = op_load::call(
|
2020-10-13 19:52:49 -04:00
|
|
|
&mut state,
|
|
|
|
json!({ "specifier": "https://deno.land/x/mod.ts"}),
|
|
|
|
)
|
|
|
|
.expect("should have invoked op");
|
|
|
|
assert_eq!(
|
|
|
|
actual,
|
|
|
|
json!({
|
|
|
|
"data": "console.log(\"hello deno\");\n",
|
2022-05-15 14:41:37 -04:00
|
|
|
"version": "149c777056afcc973d5fcbe11421b6d5ddc57b81786765302030d7fc893bf729",
|
2020-10-22 20:50:15 -04:00
|
|
|
"scriptKind": 3,
|
2020-10-13 19:52:49 -04:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-27 20:52:20 -04:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct LoadResponse {
|
|
|
|
data: String,
|
2022-05-15 14:41:37 -04:00
|
|
|
version: Option<String>,
|
2020-10-27 20:52:20 -04:00
|
|
|
script_kind: i64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_load_asset() {
|
|
|
|
let mut state = setup(
|
2021-02-17 13:47:18 -05:00
|
|
|
Some(resolve_url_or_path("https://deno.land/x/mod.ts").unwrap()),
|
2020-10-27 20:52:20 -04:00
|
|
|
None,
|
|
|
|
Some("some content".to_string()),
|
|
|
|
)
|
|
|
|
.await;
|
2022-03-14 13:44:15 -04:00
|
|
|
let value = op_load::call(
|
|
|
|
&mut state,
|
|
|
|
json!({ "specifier": "asset:///lib.dom.d.ts" }),
|
|
|
|
)
|
|
|
|
.expect("should have invoked op");
|
2020-10-27 20:52:20 -04:00
|
|
|
let actual: LoadResponse =
|
|
|
|
serde_json::from_value(value).expect("failed to deserialize");
|
2023-01-14 09:36:19 -05:00
|
|
|
let expected = get_lazily_loaded_asset("lib.dom.d.ts").unwrap();
|
2020-10-27 20:52:20 -04:00
|
|
|
assert_eq!(actual.data, expected);
|
2022-05-15 14:41:37 -04:00
|
|
|
assert!(actual.version.is_some());
|
2020-10-27 20:52:20 -04:00
|
|
|
assert_eq!(actual.script_kind, 3);
|
|
|
|
}
|
|
|
|
|
2020-10-13 19:52:49 -04:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_load_tsbuildinfo() {
|
|
|
|
let mut state = setup(
|
2021-02-17 13:47:18 -05:00
|
|
|
Some(resolve_url_or_path("https://deno.land/x/mod.ts").unwrap()),
|
2020-10-13 19:52:49 -04:00
|
|
|
None,
|
|
|
|
Some("some content".to_string()),
|
|
|
|
)
|
|
|
|
.await;
|
2023-02-05 11:49:20 -05:00
|
|
|
let actual = op_load::call(
|
|
|
|
&mut state,
|
|
|
|
json!({ "specifier": "internal:///.tsbuildinfo"}),
|
|
|
|
)
|
|
|
|
.expect("should have invoked op");
|
2020-10-13 19:52:49 -04:00
|
|
|
assert_eq!(
|
|
|
|
actual,
|
|
|
|
json!({
|
|
|
|
"data": "some content",
|
2022-05-15 14:41:37 -04:00
|
|
|
"version": null,
|
2020-10-22 20:50:15 -04:00
|
|
|
"scriptKind": 0,
|
2020-10-13 19:52:49 -04:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_load_missing_specifier() {
|
|
|
|
let mut state = setup(None, None, None).await;
|
2022-03-14 13:44:15 -04:00
|
|
|
let actual = op_load::call(
|
2020-10-13 19:52:49 -04:00
|
|
|
&mut state,
|
|
|
|
json!({ "specifier": "https://deno.land/x/mod.ts"}),
|
|
|
|
)
|
|
|
|
.expect("should have invoked op");
|
|
|
|
assert_eq!(
|
|
|
|
actual,
|
|
|
|
json!({
|
|
|
|
"data": null,
|
2022-05-15 14:41:37 -04:00
|
|
|
"version": null,
|
2020-10-22 20:50:15 -04:00
|
|
|
"scriptKind": 0,
|
2020-10-13 19:52:49 -04:00
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_resolve() {
|
|
|
|
let mut state = setup(
|
2021-02-17 13:47:18 -05:00
|
|
|
Some(resolve_url_or_path("https://deno.land/x/a.ts").unwrap()),
|
2020-10-13 19:52:49 -04:00
|
|
|
None,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.await;
|
2022-03-14 13:44:15 -04:00
|
|
|
let actual = op_resolve::call(
|
2020-10-13 19:52:49 -04:00
|
|
|
&mut state,
|
2022-01-31 04:32:49 -05:00
|
|
|
ResolveArgs {
|
|
|
|
base: "https://deno.land/x/a.ts".to_string(),
|
|
|
|
specifiers: vec!["./b.ts".to_string()],
|
|
|
|
},
|
2020-10-13 19:52:49 -04:00
|
|
|
)
|
|
|
|
.expect("should have invoked op");
|
2022-05-13 04:36:31 -04:00
|
|
|
assert_eq!(
|
|
|
|
actual,
|
|
|
|
vec![("https://deno.land/x/b.ts".into(), ".ts".into())]
|
|
|
|
);
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2020-10-22 20:50:15 -04:00
|
|
|
async fn test_resolve_empty() {
|
2020-10-13 19:52:49 -04:00
|
|
|
let mut state = setup(
|
2021-02-17 13:47:18 -05:00
|
|
|
Some(resolve_url_or_path("https://deno.land/x/a.ts").unwrap()),
|
2020-10-13 19:52:49 -04:00
|
|
|
None,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.await;
|
2022-03-14 13:44:15 -04:00
|
|
|
let actual = op_resolve::call(
|
2020-10-13 19:52:49 -04:00
|
|
|
&mut state,
|
2022-01-31 04:32:49 -05:00
|
|
|
ResolveArgs {
|
|
|
|
base: "https://deno.land/x/a.ts".to_string(),
|
|
|
|
specifiers: vec!["./bad.ts".to_string()],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.expect("should have not errored");
|
2021-01-27 14:54:20 -05:00
|
|
|
assert_eq!(
|
|
|
|
actual,
|
2023-02-05 11:49:20 -05:00
|
|
|
vec![("internal:///missing_dependency.d.ts".into(), ".d.ts".into())]
|
2021-01-27 14:54:20 -05:00
|
|
|
);
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_respond() {
|
|
|
|
let mut state = setup(None, None, None).await;
|
2022-03-14 13:44:15 -04:00
|
|
|
let actual = op_respond::call(
|
2020-10-13 19:52:49 -04:00
|
|
|
&mut state,
|
|
|
|
json!({
|
|
|
|
"diagnostics": [
|
|
|
|
{
|
|
|
|
"messageText": "Unknown compiler option 'invalid'.",
|
|
|
|
"category": 1,
|
|
|
|
"code": 5023
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"stats": [["a", 12]]
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.expect("should have invoked op");
|
|
|
|
assert_eq!(actual, json!(true));
|
2022-03-14 13:44:15 -04:00
|
|
|
let state = state.borrow::<State>();
|
2020-10-13 19:52:49 -04:00
|
|
|
assert_eq!(
|
|
|
|
state.maybe_response,
|
|
|
|
Some(RespondArgs {
|
2020-10-27 20:52:20 -04:00
|
|
|
diagnostics: Diagnostics::new(vec![Diagnostic {
|
2020-10-13 19:52:49 -04:00
|
|
|
category: DiagnosticCategory::Error,
|
|
|
|
code: 5023,
|
|
|
|
start: None,
|
|
|
|
end: None,
|
|
|
|
message_text: Some(
|
|
|
|
"Unknown compiler option \'invalid\'.".to_string()
|
|
|
|
),
|
|
|
|
message_chain: None,
|
|
|
|
source: None,
|
|
|
|
source_line: None,
|
|
|
|
file_name: None,
|
|
|
|
related_information: None,
|
|
|
|
}]),
|
|
|
|
stats: Stats(vec![("a".to_string(), 12)])
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2020-10-27 20:52:20 -04:00
|
|
|
async fn test_exec_basic() {
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = resolve_url_or_path("https://deno.land/x/a.ts").unwrap();
|
2020-10-27 20:52:20 -04:00
|
|
|
let actual = test_exec(&specifier)
|
2020-10-13 19:52:49 -04:00
|
|
|
.await
|
2020-10-27 20:52:20 -04:00
|
|
|
.expect("exec should not have errored");
|
|
|
|
assert!(actual.diagnostics.is_empty());
|
2020-10-13 19:52:49 -04:00
|
|
|
assert!(actual.maybe_tsbuildinfo.is_some());
|
|
|
|
assert_eq!(actual.stats.0.len(), 12);
|
|
|
|
}
|
2020-10-24 16:05:33 -04:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_exec_reexport_dts() {
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = resolve_url_or_path("file:///reexports.ts").unwrap();
|
2020-10-27 20:52:20 -04:00
|
|
|
let actual = test_exec(&specifier)
|
2020-10-24 16:05:33 -04:00
|
|
|
.await
|
2020-10-27 20:52:20 -04:00
|
|
|
.expect("exec should not have errored");
|
|
|
|
assert!(actual.diagnostics.is_empty());
|
2020-10-24 16:05:33 -04:00
|
|
|
assert!(actual.maybe_tsbuildinfo.is_some());
|
|
|
|
assert_eq!(actual.stats.0.len(), 12);
|
|
|
|
}
|
2020-10-27 20:52:20 -04:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn fix_lib_ref() {
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = resolve_url_or_path("file:///libref.ts").unwrap();
|
2020-10-27 20:52:20 -04:00
|
|
|
let actual = test_exec(&specifier)
|
|
|
|
.await
|
|
|
|
.expect("exec should not have errored");
|
|
|
|
assert!(actual.diagnostics.is_empty());
|
|
|
|
}
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|