2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2022-11-25 18:29:48 -05:00
|
|
|
use crate::args::TsTypeLib;
|
2022-07-19 11:58:18 -04:00
|
|
|
use crate::emit::emit_parsed_source;
|
|
|
|
use crate::graph_util::ModuleEntry;
|
2022-08-20 11:31:33 -04:00
|
|
|
use crate::node;
|
2021-09-24 11:10:42 -04:00
|
|
|
use crate::proc_state::ProcState;
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::util::text_encoding::code_without_source_map;
|
|
|
|
use crate::util::text_encoding::source_map_from_code;
|
2021-10-10 17:26:22 -04:00
|
|
|
|
2022-07-19 11:58:18 -04:00
|
|
|
use deno_ast::MediaType;
|
|
|
|
use deno_core::anyhow::anyhow;
|
2022-09-03 09:43:35 -04:00
|
|
|
use deno_core::anyhow::Context;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::futures::future::FutureExt;
|
|
|
|
use deno_core::futures::Future;
|
2022-07-19 11:58:18 -04:00
|
|
|
use deno_core::resolve_url;
|
2020-03-02 13:12:49 -05:00
|
|
|
use deno_core::ModuleLoader;
|
2022-07-19 11:58:18 -04:00
|
|
|
use deno_core::ModuleSource;
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::ModuleSpecifier;
|
2022-07-19 11:58:18 -04:00
|
|
|
use deno_core::ModuleType;
|
2020-09-19 19:17:35 -04:00
|
|
|
use deno_core::OpState;
|
2022-07-19 11:58:18 -04:00
|
|
|
use deno_core::SourceMapGetter;
|
2020-12-13 13:45:53 -05:00
|
|
|
use deno_runtime::permissions::Permissions;
|
2020-02-08 14:34:31 -05:00
|
|
|
use std::cell::RefCell;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::pin::Pin;
|
2020-02-08 14:34:31 -05:00
|
|
|
use std::rc::Rc;
|
2019-07-31 13:16:03 -04:00
|
|
|
use std::str;
|
2020-04-03 13:40:11 -04:00
|
|
|
|
2022-07-19 11:58:18 -04:00
|
|
|
struct ModuleCodeSource {
|
|
|
|
pub code: String,
|
|
|
|
pub found_url: ModuleSpecifier,
|
|
|
|
pub media_type: MediaType,
|
|
|
|
}
|
|
|
|
|
2022-03-23 09:54:22 -04:00
|
|
|
pub struct CliModuleLoader {
|
2022-06-28 16:45:55 -04:00
|
|
|
pub lib: TsTypeLib,
|
2021-05-17 03:44:38 -04:00
|
|
|
/// The initial set of permissions used to resolve the static imports in the
|
|
|
|
/// worker. They are decoupled from the worker (dynamic) permissions since
|
|
|
|
/// read access errors must be raised based on the parent thread permissions.
|
|
|
|
pub root_permissions: Permissions,
|
2021-09-24 11:10:42 -04:00
|
|
|
pub ps: ProcState,
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
impl CliModuleLoader {
|
2021-09-24 11:10:42 -04:00
|
|
|
pub fn new(ps: ProcState) -> Rc<Self> {
|
2020-09-19 19:17:35 -04:00
|
|
|
Rc::new(CliModuleLoader {
|
2022-06-29 11:51:11 -04:00
|
|
|
lib: ps.options.ts_type_lib_window(),
|
2021-05-17 03:44:38 -04:00
|
|
|
root_permissions: Permissions::allow_all(),
|
2021-09-24 11:10:42 -04:00
|
|
|
ps,
|
2020-09-19 19:17:35 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-24 11:10:42 -04:00
|
|
|
pub fn new_for_worker(ps: ProcState, permissions: Permissions) -> Rc<Self> {
|
2020-09-19 19:17:35 -04:00
|
|
|
Rc::new(CliModuleLoader {
|
2022-06-29 11:51:11 -04:00
|
|
|
lib: ps.options.ts_type_lib_worker(),
|
2021-05-17 03:44:38 -04:00
|
|
|
root_permissions: permissions,
|
2021-09-24 11:10:42 -04:00
|
|
|
ps,
|
2020-09-19 19:17:35 -04:00
|
|
|
})
|
|
|
|
}
|
2022-07-19 11:58:18 -04:00
|
|
|
|
|
|
|
fn load_prepared_module(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
2022-09-12 18:11:32 -04:00
|
|
|
maybe_referrer: Option<ModuleSpecifier>,
|
2022-07-19 11:58:18 -04:00
|
|
|
) -> Result<ModuleCodeSource, AnyError> {
|
2022-08-24 08:54:20 -04:00
|
|
|
if specifier.as_str() == "node:module" {
|
|
|
|
return Ok(ModuleCodeSource {
|
|
|
|
code: deno_runtime::deno_node::MODULE_ES_SHIM.to_string(),
|
|
|
|
found_url: specifier.to_owned(),
|
|
|
|
media_type: MediaType::JavaScript,
|
|
|
|
});
|
|
|
|
}
|
2022-07-19 11:58:18 -04:00
|
|
|
let graph_data = self.ps.graph_data.read();
|
|
|
|
let found_url = graph_data.follow_redirect(specifier);
|
|
|
|
match graph_data.get(&found_url) {
|
|
|
|
Some(ModuleEntry::Module {
|
2022-08-22 12:14:59 -04:00
|
|
|
code, media_type, ..
|
2022-07-19 11:58:18 -04:00
|
|
|
}) => {
|
|
|
|
let code = match media_type {
|
|
|
|
MediaType::JavaScript
|
|
|
|
| MediaType::Unknown
|
|
|
|
| MediaType::Cjs
|
|
|
|
| MediaType::Mjs
|
|
|
|
| MediaType::Json => {
|
|
|
|
if let Some(source) = graph_data.get_cjs_esm_translation(specifier)
|
|
|
|
{
|
|
|
|
source.to_owned()
|
|
|
|
} else {
|
|
|
|
code.to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MediaType::Dts | MediaType::Dcts | MediaType::Dmts => "".to_string(),
|
|
|
|
MediaType::TypeScript
|
|
|
|
| MediaType::Mts
|
|
|
|
| MediaType::Cts
|
|
|
|
| MediaType::Jsx
|
|
|
|
| MediaType::Tsx => {
|
|
|
|
// get emit text
|
|
|
|
emit_parsed_source(
|
|
|
|
&self.ps.emit_cache,
|
2022-08-22 12:14:59 -04:00
|
|
|
&self.ps.parsed_source_cache,
|
2022-07-19 11:58:18 -04:00
|
|
|
&found_url,
|
2022-08-22 12:14:59 -04:00
|
|
|
*media_type,
|
|
|
|
code,
|
2022-07-19 11:58:18 -04:00
|
|
|
&self.ps.emit_options,
|
|
|
|
self.ps.emit_options_hash,
|
|
|
|
)?
|
|
|
|
}
|
|
|
|
MediaType::TsBuildInfo | MediaType::Wasm | MediaType::SourceMap => {
|
|
|
|
panic!("Unexpected media type {} for {}", media_type, found_url)
|
|
|
|
}
|
|
|
|
};
|
2022-08-22 12:14:59 -04: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 11:58:18 -04:00
|
|
|
Ok(ModuleCodeSource {
|
|
|
|
code,
|
|
|
|
found_url,
|
|
|
|
media_type: *media_type,
|
|
|
|
})
|
|
|
|
}
|
2022-09-12 18:11:32 -04:00
|
|
|
_ => {
|
|
|
|
let mut msg = format!("Loading unprepared module: {}", specifier);
|
|
|
|
if let Some(referrer) = maybe_referrer {
|
|
|
|
msg = format!("{}, imported from: {}", msg, referrer.as_str());
|
|
|
|
}
|
|
|
|
Err(anyhow!(msg))
|
|
|
|
}
|
2022-07-19 11:58:18 -04:00
|
|
|
}
|
|
|
|
}
|
2022-08-20 11:31:33 -04:00
|
|
|
|
|
|
|
fn load_sync(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
2022-09-03 09:43:35 -04:00
|
|
|
maybe_referrer: Option<ModuleSpecifier>,
|
2022-08-20 11:31:33 -04:00
|
|
|
) -> Result<ModuleSource, AnyError> {
|
|
|
|
let code_source = if self.ps.npm_resolver.in_npm_package(specifier) {
|
2022-09-12 14:28:51 -04:00
|
|
|
let file_path = specifier.to_file_path().unwrap();
|
2022-09-03 09:43:35 -04:00
|
|
|
let code = std::fs::read_to_string(&file_path).with_context(|| {
|
|
|
|
let mut msg = "Unable to load ".to_string();
|
2022-11-17 20:59:10 -05:00
|
|
|
msg.push_str(&file_path.to_string_lossy());
|
2022-09-10 15:00:45 -04:00
|
|
|
if let Some(referrer) = &maybe_referrer {
|
2022-09-03 09:43:35 -04:00
|
|
|
msg.push_str(" imported from ");
|
|
|
|
msg.push_str(referrer.as_str());
|
|
|
|
}
|
|
|
|
msg
|
|
|
|
})?;
|
2022-08-20 11:31:33 -04:00
|
|
|
|
2022-09-12 14:28:51 -04:00
|
|
|
let code = if self.ps.cjs_resolutions.lock().contains(specifier) {
|
2022-08-20 11:31:33 -04: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 14:28:51 -04:00
|
|
|
specifier,
|
2022-08-20 11:31:33 -04:00
|
|
|
code,
|
|
|
|
MediaType::Cjs,
|
|
|
|
&self.ps.npm_resolver,
|
2022-10-01 06:15:56 -04:00
|
|
|
&self.ps.node_analysis_cache,
|
2022-08-20 11:31:33 -04:00
|
|
|
)?
|
|
|
|
} else {
|
|
|
|
// only inject node globals for esm
|
2022-10-01 06:15:56 -04:00
|
|
|
node::esm_code_with_node_globals(
|
|
|
|
&self.ps.node_analysis_cache,
|
|
|
|
specifier,
|
|
|
|
code,
|
|
|
|
)?
|
2022-08-20 11:31:33 -04:00
|
|
|
};
|
|
|
|
ModuleCodeSource {
|
|
|
|
code,
|
|
|
|
found_url: specifier.clone(),
|
|
|
|
media_type: MediaType::from(specifier),
|
|
|
|
}
|
|
|
|
} else {
|
2022-09-12 18:11:32 -04:00
|
|
|
self.load_prepared_module(specifier, maybe_referrer)?
|
2022-08-20 11:31:33 -04: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 {
|
|
|
|
code: code.into_bytes().into_boxed_slice(),
|
|
|
|
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 19:12:38 -04:00
|
|
|
}
|
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
impl ModuleLoader for CliModuleLoader {
|
2019-06-09 09:08:20 -04:00
|
|
|
fn resolve(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &str,
|
2021-10-10 17:26:22 -04:00
|
|
|
_is_main: bool,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<ModuleSpecifier, AnyError> {
|
2021-10-10 17:26:22 -04:00
|
|
|
self.ps.resolve(specifier, referrer)
|
2019-06-05 16:35:38 -04:00
|
|
|
}
|
|
|
|
|
2019-06-12 19:55:59 -04:00
|
|
|
fn load(
|
|
|
|
&self,
|
2022-07-19 11:58:18 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
2022-09-03 09:43:35 -04:00
|
|
|
maybe_referrer: Option<ModuleSpecifier>,
|
2022-07-19 11:58:18 -04:00
|
|
|
_is_dynamic: bool,
|
2020-03-02 13:12:49 -05:00
|
|
|
) -> Pin<Box<deno_core::ModuleSourceFuture>> {
|
2021-10-10 17:26:22 -04:00
|
|
|
// NOTE: this block is async only because of `deno_core` interface
|
|
|
|
// requirements; module was already loaded when constructing module graph
|
2022-07-19 11:58:18 -04:00
|
|
|
// during call to `prepare_load` so we can load it synchronously.
|
2022-09-03 09:43:35 -04:00
|
|
|
Box::pin(deno_core::futures::future::ready(
|
|
|
|
self.load_sync(specifier, maybe_referrer),
|
|
|
|
))
|
2019-06-05 16:35:38 -04:00
|
|
|
}
|
2020-04-30 08:37:06 -04:00
|
|
|
|
|
|
|
fn prepare_load(
|
|
|
|
&self,
|
2020-09-19 19:17:35 -04:00
|
|
|
op_state: Rc<RefCell<OpState>>,
|
2020-10-22 20:50:15 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
_maybe_referrer: Option<String>,
|
|
|
|
is_dynamic: bool,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Pin<Box<dyn Future<Output = Result<(), AnyError>>>> {
|
2022-08-20 11:31:33 -04:00
|
|
|
if self.ps.npm_resolver.in_npm_package(specifier) {
|
|
|
|
// nothing to prepare
|
|
|
|
return Box::pin(deno_core::futures::future::ready(Ok(())));
|
|
|
|
}
|
|
|
|
|
2020-10-22 20:50:15 -04:00
|
|
|
let specifier = specifier.clone();
|
2021-09-24 11:10:42 -04:00
|
|
|
let ps = self.ps.clone();
|
2020-09-19 19:17:35 -04:00
|
|
|
let state = op_state.borrow();
|
|
|
|
|
2021-05-17 03:44:38 -04:00
|
|
|
let dynamic_permissions = state.borrow::<Permissions>().clone();
|
2021-10-10 17:26:22 -04:00
|
|
|
let root_permissions = if is_dynamic {
|
|
|
|
dynamic_permissions.clone()
|
|
|
|
} else {
|
|
|
|
self.root_permissions.clone()
|
|
|
|
};
|
2022-06-28 16:45:55 -04:00
|
|
|
let lib = self.lib;
|
2021-01-06 15:31:16 -05:00
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
drop(state);
|
|
|
|
|
2020-05-29 10:32:15 -04:00
|
|
|
async move {
|
2021-09-24 11:10:42 -04:00
|
|
|
ps.prepare_module_load(
|
2022-03-10 20:33:02 -05:00
|
|
|
vec![specifier],
|
2021-10-10 17:26:22 -04:00
|
|
|
is_dynamic,
|
2021-09-24 11:10:42 -04:00
|
|
|
lib,
|
|
|
|
root_permissions,
|
|
|
|
dynamic_permissions,
|
2021-11-15 18:25:52 -05:00
|
|
|
false,
|
2021-09-24 11:10:42 -04:00
|
|
|
)
|
|
|
|
.await
|
2020-05-29 10:32:15 -04:00
|
|
|
}
|
|
|
|
.boxed_local()
|
2020-04-30 08:37:06 -04:00
|
|
|
}
|
2019-06-05 16:35:38 -04:00
|
|
|
}
|
2022-07-19 11:58:18 -04:00
|
|
|
|
|
|
|
impl SourceMapGetter for CliModuleLoader {
|
|
|
|
fn get_source_map(&self, file_name: &str) -> Option<Vec<u8>> {
|
2022-08-20 11:31:33 -04: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 11:58:18 -04:00
|
|
|
}
|
2022-09-12 18:11:32 -04:00
|
|
|
let source = self.load_prepared_module(&specifier, None).ok()?;
|
2022-08-20 11:31:33 -04:00
|
|
|
source_map_from_code(&source.code)
|
2022-07-19 11:58:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_source_line(
|
|
|
|
&self,
|
|
|
|
file_name: &str,
|
|
|
|
line_number: usize,
|
|
|
|
) -> Option<String> {
|
|
|
|
let graph_data = self.ps.graph_data.read();
|
|
|
|
let specifier = graph_data.follow_redirect(&resolve_url(file_name).ok()?);
|
|
|
|
let code = match graph_data.get(&specifier) {
|
|
|
|
Some(ModuleEntry::Module { code, .. }) => code,
|
|
|
|
_ => 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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|