2023-01-02 21:00:42 +00:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-09-06 00:34:02 +00:00
|
|
|
|
2022-11-25 23:29:48 +00:00
|
|
|
use crate::args::TsTypeLib;
|
2022-07-19 15:58:18 +00:00
|
|
|
use crate::emit::emit_parsed_source;
|
2022-08-20 15:31:33 +00:00
|
|
|
use crate::node;
|
2021-09-24 15:10:42 +00:00
|
|
|
use crate::proc_state::ProcState;
|
2022-11-28 22:28:54 +00:00
|
|
|
use crate::util::text_encoding::code_without_source_map;
|
|
|
|
use crate::util::text_encoding::source_map_from_code;
|
2021-10-10 21:26:22 +00:00
|
|
|
|
2022-07-19 15:58:18 +00:00
|
|
|
use deno_ast::MediaType;
|
|
|
|
use deno_core::anyhow::anyhow;
|
2022-09-03 13:43:35 +00:00
|
|
|
use deno_core::anyhow::Context;
|
2020-09-14 16:48:57 +00:00
|
|
|
use deno_core::error::AnyError;
|
2020-09-21 16:36:37 +00:00
|
|
|
use deno_core::futures::future::FutureExt;
|
|
|
|
use deno_core::futures::Future;
|
2022-07-19 15:58:18 +00:00
|
|
|
use deno_core::resolve_url;
|
2023-03-21 22:33:12 +00:00
|
|
|
use deno_core::ModuleCode;
|
2020-03-02 18:12:49 +00:00
|
|
|
use deno_core::ModuleLoader;
|
2022-07-19 15:58:18 +00:00
|
|
|
use deno_core::ModuleSource;
|
2020-01-05 16:56:18 +00:00
|
|
|
use deno_core::ModuleSpecifier;
|
2022-07-19 15:58:18 +00:00
|
|
|
use deno_core::ModuleType;
|
2020-09-19 23:17:35 +00:00
|
|
|
use deno_core::OpState;
|
2023-01-10 13:35:44 +00:00
|
|
|
use deno_core::ResolutionKind;
|
2022-07-19 15:58:18 +00:00
|
|
|
use deno_core::SourceMapGetter;
|
2023-02-22 19:15:25 +00:00
|
|
|
use deno_graph::EsmModule;
|
|
|
|
use deno_graph::JsonModule;
|
2023-01-07 16:25:34 +00:00
|
|
|
use deno_runtime::permissions::PermissionsContainer;
|
2020-02-08 19:34:31 +00:00
|
|
|
use std::cell::RefCell;
|
2019-11-17 00:17:47 +00:00
|
|
|
use std::pin::Pin;
|
2020-02-08 19:34:31 +00:00
|
|
|
use std::rc::Rc;
|
2019-07-31 17:16:03 +00:00
|
|
|
use std::str;
|
2020-04-03 17:40:11 +00:00
|
|
|
|
2022-07-19 15:58:18 +00:00
|
|
|
struct ModuleCodeSource {
|
2023-03-21 22:33:12 +00:00
|
|
|
pub code: ModuleCode,
|
2022-07-19 15:58:18 +00:00
|
|
|
pub found_url: ModuleSpecifier,
|
|
|
|
pub media_type: MediaType,
|
|
|
|
}
|
|
|
|
|
2022-03-23 13:54:22 +00:00
|
|
|
pub struct CliModuleLoader {
|
2022-06-28 20:45:55 +00:00
|
|
|
pub lib: TsTypeLib,
|
2021-05-17 07:44:38 +00:00
|
|
|
/// The initial set of permissions used to resolve the static imports in the
|
2023-01-10 13:35:44 +00:00
|
|
|
/// worker. These are "allow all" for main worker, and parent thread
|
|
|
|
/// permissions for Web Worker.
|
2023-01-07 16:25:34 +00:00
|
|
|
pub root_permissions: PermissionsContainer,
|
2023-01-10 13:35:44 +00:00
|
|
|
/// Permissions used to resolve dynamic imports, these get passed as
|
|
|
|
/// "root permissions" for Web Worker.
|
|
|
|
dynamic_permissions: PermissionsContainer,
|
2021-09-24 15:10:42 +00:00
|
|
|
pub ps: ProcState,
|
2019-03-14 23:17:52 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 23:17:35 +00:00
|
|
|
impl CliModuleLoader {
|
2023-01-10 13:35:44 +00:00
|
|
|
pub fn new(
|
|
|
|
ps: ProcState,
|
|
|
|
root_permissions: PermissionsContainer,
|
|
|
|
dynamic_permissions: PermissionsContainer,
|
|
|
|
) -> Rc<Self> {
|
2020-09-19 23:17:35 +00:00
|
|
|
Rc::new(CliModuleLoader {
|
2022-06-29 15:51:11 +00:00
|
|
|
lib: ps.options.ts_type_lib_window(),
|
2023-01-10 13:35:44 +00:00
|
|
|
root_permissions,
|
|
|
|
dynamic_permissions,
|
2021-09-24 15:10:42 +00:00
|
|
|
ps,
|
2020-09-19 23:17:35 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-07 16:25:34 +00:00
|
|
|
pub fn new_for_worker(
|
|
|
|
ps: ProcState,
|
2023-01-10 13:35:44 +00:00
|
|
|
root_permissions: PermissionsContainer,
|
|
|
|
dynamic_permissions: PermissionsContainer,
|
2023-01-07 16:25:34 +00:00
|
|
|
) -> Rc<Self> {
|
2020-09-19 23:17:35 +00:00
|
|
|
Rc::new(CliModuleLoader {
|
2022-06-29 15:51:11 +00:00
|
|
|
lib: ps.options.ts_type_lib_worker(),
|
2023-01-10 13:35:44 +00:00
|
|
|
root_permissions,
|
|
|
|
dynamic_permissions,
|
2021-09-24 15:10:42 +00:00
|
|
|
ps,
|
2020-09-19 23:17:35 +00:00
|
|
|
})
|
|
|
|
}
|
2022-07-19 15:58:18 +00:00
|
|
|
|
|
|
|
fn load_prepared_module(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
2022-09-12 22:11:32 +00:00
|
|
|
maybe_referrer: Option<ModuleSpecifier>,
|
2022-07-19 15:58:18 +00:00
|
|
|
) -> Result<ModuleCodeSource, AnyError> {
|
2023-02-15 18:44:52 +00:00
|
|
|
if specifier.scheme() == "node" {
|
2023-02-22 19:15:25 +00:00
|
|
|
unreachable!(); // Node built-in modules should be handled internally.
|
2023-02-15 18:44:52 +00:00
|
|
|
}
|
|
|
|
|
2023-02-10 03:00:23 +00:00
|
|
|
let graph = self.ps.graph();
|
|
|
|
match graph.get(specifier) {
|
2023-02-22 19:15:25 +00:00
|
|
|
Some(deno_graph::Module::Json(JsonModule {
|
|
|
|
source,
|
2023-02-10 03:00:23 +00:00
|
|
|
media_type,
|
|
|
|
specifier,
|
|
|
|
..
|
2023-02-22 19:15:25 +00:00
|
|
|
})) => Ok(ModuleCodeSource {
|
2023-03-21 22:33:12 +00:00
|
|
|
code: source.into(),
|
2023-02-22 19:15:25 +00:00
|
|
|
found_url: specifier.clone(),
|
|
|
|
media_type: *media_type,
|
|
|
|
}),
|
|
|
|
Some(deno_graph::Module::Esm(EsmModule {
|
|
|
|
source,
|
|
|
|
media_type,
|
|
|
|
specifier,
|
|
|
|
..
|
|
|
|
})) => {
|
2023-03-21 22:33:12 +00:00
|
|
|
let code: ModuleCode = match media_type {
|
2022-07-19 15:58:18 +00:00
|
|
|
MediaType::JavaScript
|
|
|
|
| MediaType::Unknown
|
|
|
|
| MediaType::Cjs
|
|
|
|
| MediaType::Mjs
|
2023-03-21 22:33:12 +00:00
|
|
|
| MediaType::Json => source.into(),
|
|
|
|
MediaType::Dts | MediaType::Dcts | MediaType::Dmts => {
|
|
|
|
Default::default()
|
|
|
|
}
|
2022-07-19 15:58:18 +00:00
|
|
|
MediaType::TypeScript
|
|
|
|
| MediaType::Mts
|
|
|
|
| MediaType::Cts
|
|
|
|
| MediaType::Jsx
|
|
|
|
| MediaType::Tsx => {
|
|
|
|
// get emit text
|
|
|
|
emit_parsed_source(
|
|
|
|
&self.ps.emit_cache,
|
2022-08-22 16:14:59 +00:00
|
|
|
&self.ps.parsed_source_cache,
|
2023-02-10 03:00:23 +00:00
|
|
|
specifier,
|
2022-08-22 16:14:59 +00:00
|
|
|
*media_type,
|
2023-02-22 19:15:25 +00:00
|
|
|
source,
|
2022-07-19 15:58:18 +00:00
|
|
|
&self.ps.emit_options,
|
|
|
|
self.ps.emit_options_hash,
|
|
|
|
)?
|
|
|
|
}
|
|
|
|
MediaType::TsBuildInfo | MediaType::Wasm | MediaType::SourceMap => {
|
2023-02-10 03:00:23 +00:00
|
|
|
panic!("Unexpected media type {media_type} for {specifier}")
|
2022-07-19 15:58:18 +00:00
|
|
|
}
|
|
|
|
};
|
2022-08-22 16:14:59 +00:00
|
|
|
|
|
|
|
// at this point, we no longer need the parsed source in memory, so free it
|
|
|
|
self.ps.parsed_source_cache.free(specifier);
|
|
|
|
|
2022-07-19 15:58:18 +00:00
|
|
|
Ok(ModuleCodeSource {
|
|
|
|
code,
|
2023-02-10 03:00:23 +00:00
|
|
|
found_url: specifier.clone(),
|
2022-07-19 15:58:18 +00:00
|
|
|
media_type: *media_type,
|
|
|
|
})
|
|
|
|
}
|
2022-09-12 22:11:32 +00:00
|
|
|
_ => {
|
2023-01-27 15:43:16 +00:00
|
|
|
let mut msg = format!("Loading unprepared module: {specifier}");
|
2022-09-12 22:11:32 +00:00
|
|
|
if let Some(referrer) = maybe_referrer {
|
|
|
|
msg = format!("{}, imported from: {}", msg, referrer.as_str());
|
|
|
|
}
|
|
|
|
Err(anyhow!(msg))
|
|
|
|
}
|
2022-07-19 15:58:18 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-20 15:31:33 +00:00
|
|
|
|
|
|
|
fn load_sync(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
2022-09-03 13:43:35 +00:00
|
|
|
maybe_referrer: Option<ModuleSpecifier>,
|
2023-01-10 13:35:44 +00:00
|
|
|
is_dynamic: bool,
|
2022-08-20 15:31:33 +00:00
|
|
|
) -> Result<ModuleSource, AnyError> {
|
|
|
|
let code_source = if self.ps.npm_resolver.in_npm_package(specifier) {
|
2022-09-12 18:28:51 +00:00
|
|
|
let file_path = specifier.to_file_path().unwrap();
|
2022-09-03 13:43:35 +00:00
|
|
|
let code = std::fs::read_to_string(&file_path).with_context(|| {
|
|
|
|
let mut msg = "Unable to load ".to_string();
|
2022-11-18 01:59:10 +00:00
|
|
|
msg.push_str(&file_path.to_string_lossy());
|
2022-09-10 19:00:45 +00:00
|
|
|
if let Some(referrer) = &maybe_referrer {
|
2022-09-03 13:43:35 +00:00
|
|
|
msg.push_str(" imported from ");
|
|
|
|
msg.push_str(referrer.as_str());
|
|
|
|
}
|
|
|
|
msg
|
|
|
|
})?;
|
2022-08-20 15:31:33 +00:00
|
|
|
|
2022-09-12 18:28:51 +00:00
|
|
|
let code = if self.ps.cjs_resolutions.lock().contains(specifier) {
|
2023-01-10 13:35:44 +00:00
|
|
|
let mut permissions = if is_dynamic {
|
|
|
|
self.dynamic_permissions.clone()
|
|
|
|
} else {
|
|
|
|
self.root_permissions.clone()
|
|
|
|
};
|
2022-08-20 15:31:33 +00:00
|
|
|
// translate cjs to esm if it's cjs and inject node globals
|
|
|
|
node::translate_cjs_to_esm(
|
|
|
|
&self.ps.file_fetcher,
|
2022-09-12 18:28:51 +00:00
|
|
|
specifier,
|
2022-08-20 15:31:33 +00:00
|
|
|
code,
|
|
|
|
MediaType::Cjs,
|
|
|
|
&self.ps.npm_resolver,
|
2022-10-01 10:15:56 +00:00
|
|
|
&self.ps.node_analysis_cache,
|
2023-01-10 13:35:44 +00:00
|
|
|
&mut permissions,
|
2022-08-20 15:31:33 +00:00
|
|
|
)?
|
|
|
|
} else {
|
|
|
|
// only inject node globals for esm
|
2022-10-01 10:15:56 +00:00
|
|
|
node::esm_code_with_node_globals(
|
|
|
|
&self.ps.node_analysis_cache,
|
|
|
|
specifier,
|
|
|
|
code,
|
|
|
|
)?
|
2022-08-20 15:31:33 +00:00
|
|
|
};
|
|
|
|
ModuleCodeSource {
|
2023-03-21 22:33:12 +00:00
|
|
|
code: code.into(),
|
2022-08-20 15:31:33 +00:00
|
|
|
found_url: specifier.clone(),
|
2023-03-21 15:46:40 +00:00
|
|
|
media_type: MediaType::from_specifier(specifier),
|
2022-08-20 15:31:33 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-09-12 22:11:32 +00:00
|
|
|
self.load_prepared_module(specifier, maybe_referrer)?
|
2022-08-20 15:31:33 +00:00
|
|
|
};
|
|
|
|
let code = if self.ps.options.is_inspecting() {
|
|
|
|
// we need the code with the source map in order for
|
|
|
|
// it to work with --inspect or --inspect-brk
|
|
|
|
code_source.code
|
|
|
|
} else {
|
|
|
|
// reduce memory and throw away the source map
|
|
|
|
// because we don't need it
|
|
|
|
code_without_source_map(code_source.code)
|
|
|
|
};
|
|
|
|
Ok(ModuleSource {
|
2023-03-21 22:33:12 +00:00
|
|
|
code,
|
2022-08-20 15:31:33 +00:00
|
|
|
module_url_specified: specifier.to_string(),
|
|
|
|
module_url_found: code_source.found_url.to_string(),
|
|
|
|
module_type: match code_source.media_type {
|
|
|
|
MediaType::Json => ModuleType::Json,
|
|
|
|
_ => ModuleType::JavaScript,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2020-04-27 23:12:38 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 23:17:35 +00:00
|
|
|
impl ModuleLoader for CliModuleLoader {
|
2019-06-09 13:08:20 +00:00
|
|
|
fn resolve(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &str,
|
2023-01-10 13:35:44 +00:00
|
|
|
kind: ResolutionKind,
|
2020-09-14 16:48:57 +00:00
|
|
|
) -> Result<ModuleSpecifier, AnyError> {
|
2023-01-10 13:35:44 +00:00
|
|
|
let mut permissions = if matches!(kind, ResolutionKind::DynamicImport) {
|
|
|
|
self.dynamic_permissions.clone()
|
|
|
|
} else {
|
|
|
|
self.root_permissions.clone()
|
|
|
|
};
|
|
|
|
self.ps.resolve(specifier, referrer, &mut permissions)
|
2019-06-05 20:35:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-12 23:55:59 +00:00
|
|
|
fn load(
|
|
|
|
&self,
|
2022-07-19 15:58:18 +00:00
|
|
|
specifier: &ModuleSpecifier,
|
2022-09-03 13:43:35 +00:00
|
|
|
maybe_referrer: Option<ModuleSpecifier>,
|
2023-01-10 13:35:44 +00:00
|
|
|
is_dynamic: bool,
|
2020-03-02 18:12:49 +00:00
|
|
|
) -> Pin<Box<deno_core::ModuleSourceFuture>> {
|
2021-10-10 21:26:22 +00:00
|
|
|
// NOTE: this block is async only because of `deno_core` interface
|
|
|
|
// requirements; module was already loaded when constructing module graph
|
2022-07-19 15:58:18 +00:00
|
|
|
// during call to `prepare_load` so we can load it synchronously.
|
2023-01-10 13:35:44 +00:00
|
|
|
Box::pin(deno_core::futures::future::ready(self.load_sync(
|
|
|
|
specifier,
|
|
|
|
maybe_referrer,
|
|
|
|
is_dynamic,
|
|
|
|
)))
|
2019-06-05 20:35:38 +00:00
|
|
|
}
|
2020-04-30 12:37:06 +00:00
|
|
|
|
|
|
|
fn prepare_load(
|
|
|
|
&self,
|
2023-01-10 13:35:44 +00:00
|
|
|
_op_state: Rc<RefCell<OpState>>,
|
2020-10-23 00:50:15 +00:00
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
_maybe_referrer: Option<String>,
|
|
|
|
is_dynamic: bool,
|
2020-09-14 16:48:57 +00:00
|
|
|
) -> Pin<Box<dyn Future<Output = Result<(), AnyError>>>> {
|
2022-08-20 15:31:33 +00:00
|
|
|
if self.ps.npm_resolver.in_npm_package(specifier) {
|
|
|
|
// nothing to prepare
|
|
|
|
return Box::pin(deno_core::futures::future::ready(Ok(())));
|
|
|
|
}
|
|
|
|
|
2020-10-23 00:50:15 +00:00
|
|
|
let specifier = specifier.clone();
|
2021-09-24 15:10:42 +00:00
|
|
|
let ps = self.ps.clone();
|
2020-09-19 23:17:35 +00:00
|
|
|
|
2023-01-10 13:35:44 +00:00
|
|
|
let dynamic_permissions = self.dynamic_permissions.clone();
|
2021-10-10 21:26:22 +00:00
|
|
|
let root_permissions = if is_dynamic {
|
2023-01-10 13:35:44 +00:00
|
|
|
self.dynamic_permissions.clone()
|
2021-10-10 21:26:22 +00:00
|
|
|
} else {
|
|
|
|
self.root_permissions.clone()
|
|
|
|
};
|
2022-06-28 20:45:55 +00:00
|
|
|
let lib = self.lib;
|
2021-01-06 20:31:16 +00:00
|
|
|
|
2020-05-29 14:32:15 +00:00
|
|
|
async move {
|
2021-09-24 15:10:42 +00:00
|
|
|
ps.prepare_module_load(
|
2022-03-11 01:33:02 +00:00
|
|
|
vec![specifier],
|
2021-10-10 21:26:22 +00:00
|
|
|
is_dynamic,
|
2021-09-24 15:10:42 +00:00
|
|
|
lib,
|
|
|
|
root_permissions,
|
|
|
|
dynamic_permissions,
|
|
|
|
)
|
|
|
|
.await
|
2020-05-29 14:32:15 +00:00
|
|
|
}
|
|
|
|
.boxed_local()
|
2020-04-30 12:37:06 +00:00
|
|
|
}
|
2019-06-05 20:35:38 +00:00
|
|
|
}
|
2022-07-19 15:58:18 +00:00
|
|
|
|
|
|
|
impl SourceMapGetter for CliModuleLoader {
|
|
|
|
fn get_source_map(&self, file_name: &str) -> Option<Vec<u8>> {
|
2022-08-20 15:31:33 +00:00
|
|
|
let specifier = resolve_url(file_name).ok()?;
|
|
|
|
match specifier.scheme() {
|
|
|
|
// we should only be looking for emits for schemes that denote external
|
|
|
|
// modules, which the disk_cache supports
|
|
|
|
"wasm" | "file" | "http" | "https" | "data" | "blob" => (),
|
|
|
|
_ => return None,
|
2022-07-19 15:58:18 +00:00
|
|
|
}
|
2022-09-12 22:11:32 +00:00
|
|
|
let source = self.load_prepared_module(&specifier, None).ok()?;
|
2022-08-20 15:31:33 +00:00
|
|
|
source_map_from_code(&source.code)
|
2022-07-19 15:58:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_source_line(
|
|
|
|
&self,
|
|
|
|
file_name: &str,
|
|
|
|
line_number: usize,
|
|
|
|
) -> Option<String> {
|
2023-02-10 03:00:23 +00:00
|
|
|
let graph = self.ps.graph();
|
|
|
|
let code = match graph.get(&resolve_url(file_name).ok()?) {
|
2023-02-22 19:15:25 +00:00
|
|
|
Some(deno_graph::Module::Esm(module)) => &module.source,
|
|
|
|
Some(deno_graph::Module::Json(module)) => &module.source,
|
2022-07-19 15:58:18 +00:00
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
// Do NOT use .lines(): it skips the terminating empty line.
|
|
|
|
// (due to internally using_terminator() instead of .split())
|
|
|
|
let lines: Vec<&str> = code.split('\n').collect();
|
|
|
|
if line_number >= lines.len() {
|
|
|
|
Some(format!(
|
|
|
|
"{} Couldn't format source line: Line {} is out of bounds (source may have changed at runtime)",
|
|
|
|
crate::colors::yellow("Warning"), line_number + 1,
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
Some(lines[line_number].to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|