2022-01-08 03:09:52 +00:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2020-09-06 00:34:02 +00:00
|
|
|
|
2022-07-19 15:58:18 +00:00
|
|
|
use crate::emit::emit_parsed_source;
|
2022-06-28 20:45:55 +00:00
|
|
|
use crate::emit::TsTypeLib;
|
2022-07-19 15:58:18 +00:00
|
|
|
use crate::graph_util::ModuleEntry;
|
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-07-19 15:58:18 +00:00
|
|
|
use crate::text_encoding::code_without_source_map;
|
|
|
|
use crate::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;
|
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;
|
2022-07-19 15:58:18 +00:00
|
|
|
use deno_core::SourceMapGetter;
|
2020-12-13 18:45:53 +00:00
|
|
|
use deno_runtime::permissions::Permissions;
|
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 {
|
|
|
|
pub code: String,
|
|
|
|
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
|
|
|
|
/// 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 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 {
|
2021-09-24 15:10:42 +00:00
|
|
|
pub fn new(ps: ProcState) -> 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(),
|
2021-05-17 07:44:38 +00:00
|
|
|
root_permissions: Permissions::allow_all(),
|
2021-09-24 15:10:42 +00:00
|
|
|
ps,
|
2020-09-19 23:17:35 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-24 15:10:42 +00:00
|
|
|
pub fn new_for_worker(ps: ProcState, permissions: Permissions) -> 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(),
|
2021-05-17 07:44:38 +00:00
|
|
|
root_permissions: 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> {
|
2022-08-24 12:54:20 +00: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 15:58:18 +00: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 16:14:59 +00:00
|
|
|
code, media_type, ..
|
2022-07-19 15:58:18 +00: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 16:14:59 +00:00
|
|
|
&self.ps.parsed_source_cache,
|
2022-07-19 15:58:18 +00:00
|
|
|
&found_url,
|
2022-08-22 16:14:59 +00:00
|
|
|
*media_type,
|
|
|
|
code,
|
2022-07-19 15:58:18 +00: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 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,
|
|
|
|
found_url,
|
|
|
|
media_type: *media_type,
|
|
|
|
})
|
|
|
|
}
|
2022-09-12 22:11:32 +00: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 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>,
|
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) {
|
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,
|
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 {
|
|
|
|
code,
|
|
|
|
found_url: specifier.clone(),
|
|
|
|
media_type: MediaType::from(specifier),
|
|
|
|
}
|
|
|
|
} 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 {
|
|
|
|
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 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,
|
2021-10-10 21:26:22 +00:00
|
|
|
_is_main: bool,
|
2020-09-14 16:48:57 +00:00
|
|
|
) -> Result<ModuleSpecifier, AnyError> {
|
2021-10-10 21:26:22 +00:00
|
|
|
self.ps.resolve(specifier, referrer)
|
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>,
|
2022-07-19 15:58:18 +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.
|
2022-09-03 13:43:35 +00:00
|
|
|
Box::pin(deno_core::futures::future::ready(
|
|
|
|
self.load_sync(specifier, maybe_referrer),
|
|
|
|
))
|
2019-06-05 20:35:38 +00:00
|
|
|
}
|
2020-04-30 12:37:06 +00:00
|
|
|
|
|
|
|
fn prepare_load(
|
|
|
|
&self,
|
2020-09-19 23:17:35 +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
|
|
|
let state = op_state.borrow();
|
|
|
|
|
2021-05-17 07:44:38 +00:00
|
|
|
let dynamic_permissions = state.borrow::<Permissions>().clone();
|
2021-10-10 21:26:22 +00:00
|
|
|
let root_permissions = if is_dynamic {
|
|
|
|
dynamic_permissions.clone()
|
|
|
|
} 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-09-19 23:17:35 +00:00
|
|
|
drop(state);
|
|
|
|
|
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,
|
2021-11-15 23:25:52 +00:00
|
|
|
false,
|
2021-09-24 15:10:42 +00:00
|
|
|
)
|
|
|
|
.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> {
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|