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;
|
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;
|
2023-03-21 18:33:12 -04:00
|
|
|
use deno_core::ModuleCode;
|
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;
|
2023-01-10 08:35:44 -05:00
|
|
|
use deno_core::ResolutionKind;
|
2022-07-19 11:58:18 -04:00
|
|
|
use deno_core::SourceMapGetter;
|
2023-02-22 14:15:25 -05:00
|
|
|
use deno_graph::EsmModule;
|
|
|
|
use deno_graph::JsonModule;
|
2023-01-07 11:25:34 -05:00
|
|
|
use deno_runtime::permissions::PermissionsContainer;
|
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 {
|
2023-03-21 18:33:12 -04:00
|
|
|
pub code: ModuleCode,
|
2022-07-19 11:58:18 -04:00
|
|
|
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
|
2023-01-10 08:35:44 -05:00
|
|
|
/// worker. These are "allow all" for main worker, and parent thread
|
|
|
|
/// permissions for Web Worker.
|
2023-01-07 11:25:34 -05:00
|
|
|
pub root_permissions: PermissionsContainer,
|
2023-01-10 08:35:44 -05:00
|
|
|
/// Permissions used to resolve dynamic imports, these get passed as
|
|
|
|
/// "root permissions" for Web Worker.
|
|
|
|
dynamic_permissions: PermissionsContainer,
|
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 {
|
2023-01-10 08:35:44 -05:00
|
|
|
pub fn new(
|
|
|
|
ps: ProcState,
|
|
|
|
root_permissions: PermissionsContainer,
|
|
|
|
dynamic_permissions: PermissionsContainer,
|
|
|
|
) -> 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(),
|
2023-01-10 08:35:44 -05:00
|
|
|
root_permissions,
|
|
|
|
dynamic_permissions,
|
2021-09-24 11:10:42 -04:00
|
|
|
ps,
|
2020-09-19 19:17:35 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-07 11:25:34 -05:00
|
|
|
pub fn new_for_worker(
|
|
|
|
ps: ProcState,
|
2023-01-10 08:35:44 -05:00
|
|
|
root_permissions: PermissionsContainer,
|
|
|
|
dynamic_permissions: PermissionsContainer,
|
2023-01-07 11:25:34 -05:00
|
|
|
) -> 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(),
|
2023-01-10 08:35:44 -05:00
|
|
|
root_permissions,
|
|
|
|
dynamic_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,
|
2023-04-04 08:46:31 -04:00
|
|
|
maybe_referrer: Option<&ModuleSpecifier>,
|
2022-07-19 11:58:18 -04:00
|
|
|
) -> Result<ModuleCodeSource, AnyError> {
|
2023-02-15 13:44:52 -05:00
|
|
|
if specifier.scheme() == "node" {
|
2023-02-22 14:15:25 -05:00
|
|
|
unreachable!(); // Node built-in modules should be handled internally.
|
2023-02-15 13:44:52 -05:00
|
|
|
}
|
|
|
|
|
2023-02-09 22:00:23 -05:00
|
|
|
let graph = self.ps.graph();
|
|
|
|
match graph.get(specifier) {
|
2023-02-22 14:15:25 -05:00
|
|
|
Some(deno_graph::Module::Json(JsonModule {
|
|
|
|
source,
|
2023-02-09 22:00:23 -05:00
|
|
|
media_type,
|
|
|
|
specifier,
|
|
|
|
..
|
2023-02-22 14:15:25 -05:00
|
|
|
})) => Ok(ModuleCodeSource {
|
2023-04-04 08:46:31 -04:00
|
|
|
code: source.clone().into(),
|
2023-02-22 14:15:25 -05:00
|
|
|
found_url: specifier.clone(),
|
|
|
|
media_type: *media_type,
|
|
|
|
}),
|
|
|
|
Some(deno_graph::Module::Esm(EsmModule {
|
|
|
|
source,
|
|
|
|
media_type,
|
|
|
|
specifier,
|
|
|
|
..
|
|
|
|
})) => {
|
2023-03-21 18:33:12 -04:00
|
|
|
let code: ModuleCode = match media_type {
|
2022-07-19 11:58:18 -04:00
|
|
|
MediaType::JavaScript
|
|
|
|
| MediaType::Unknown
|
|
|
|
| MediaType::Cjs
|
|
|
|
| MediaType::Mjs
|
2023-04-04 08:46:31 -04:00
|
|
|
| MediaType::Json => source.clone().into(),
|
2023-03-21 18:33:12 -04:00
|
|
|
MediaType::Dts | MediaType::Dcts | MediaType::Dmts => {
|
|
|
|
Default::default()
|
|
|
|
}
|
2022-07-19 11:58:18 -04:00
|
|
|
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,
|
2023-02-09 22:00:23 -05:00
|
|
|
specifier,
|
2022-08-22 12:14:59 -04:00
|
|
|
*media_type,
|
2023-02-22 14:15:25 -05:00
|
|
|
source,
|
2022-07-19 11:58:18 -04:00
|
|
|
&self.ps.emit_options,
|
|
|
|
self.ps.emit_options_hash,
|
|
|
|
)?
|
|
|
|
}
|
|
|
|
MediaType::TsBuildInfo | MediaType::Wasm | MediaType::SourceMap => {
|
2023-02-09 22:00:23 -05:00
|
|
|
panic!("Unexpected media type {media_type} for {specifier}")
|
2022-07-19 11:58:18 -04:00
|
|
|
}
|
|
|
|
};
|
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,
|
2023-02-09 22:00:23 -05:00
|
|
|
found_url: specifier.clone(),
|
2022-07-19 11:58:18 -04:00
|
|
|
media_type: *media_type,
|
|
|
|
})
|
|
|
|
}
|
2022-09-12 18:11:32 -04:00
|
|
|
_ => {
|
2023-01-27 10:43:16 -05:00
|
|
|
let mut msg = format!("Loading unprepared module: {specifier}");
|
2022-09-12 18:11:32 -04:00
|
|
|
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,
|
2023-04-04 08:46:31 -04:00
|
|
|
maybe_referrer: Option<&ModuleSpecifier>,
|
2023-01-10 08:35:44 -05:00
|
|
|
is_dynamic: bool,
|
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) {
|
2023-01-10 08:35:44 -05:00
|
|
|
let mut permissions = if is_dynamic {
|
|
|
|
self.dynamic_permissions.clone()
|
|
|
|
} else {
|
|
|
|
self.root_permissions.clone()
|
|
|
|
};
|
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,
|
2023-01-10 08:35:44 -05:00
|
|
|
&mut permissions,
|
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 {
|
2023-03-21 18:33:12 -04:00
|
|
|
code: code.into(),
|
2022-08-20 11:31:33 -04:00
|
|
|
found_url: specifier.clone(),
|
2023-03-21 11:46:40 -04:00
|
|
|
media_type: MediaType::from_specifier(specifier),
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
|
|
|
} 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)
|
|
|
|
};
|
2023-04-04 08:46:31 -04:00
|
|
|
Ok(ModuleSource::new_with_redirect(
|
|
|
|
match code_source.media_type {
|
2022-08-20 11:31:33 -04:00
|
|
|
MediaType::Json => ModuleType::Json,
|
|
|
|
_ => ModuleType::JavaScript,
|
|
|
|
},
|
2023-04-04 08:46:31 -04:00
|
|
|
code,
|
|
|
|
specifier,
|
|
|
|
&code_source.found_url,
|
|
|
|
))
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
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,
|
2023-01-10 08:35:44 -05:00
|
|
|
kind: ResolutionKind,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<ModuleSpecifier, AnyError> {
|
2023-01-10 08:35:44 -05: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 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,
|
2023-04-04 08:46:31 -04:00
|
|
|
maybe_referrer: Option<&ModuleSpecifier>,
|
2023-01-10 08:35:44 -05: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.
|
2023-01-10 08:35:44 -05:00
|
|
|
Box::pin(deno_core::futures::future::ready(self.load_sync(
|
|
|
|
specifier,
|
|
|
|
maybe_referrer,
|
|
|
|
is_dynamic,
|
|
|
|
)))
|
2019-06-05 16:35:38 -04:00
|
|
|
}
|
2020-04-30 08:37:06 -04:00
|
|
|
|
|
|
|
fn prepare_load(
|
|
|
|
&self,
|
2023-01-10 08:35:44 -05: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
|
|
|
|
2023-01-10 08:35:44 -05:00
|
|
|
let dynamic_permissions = self.dynamic_permissions.clone();
|
2021-10-10 17:26:22 -04:00
|
|
|
let root_permissions = if is_dynamic {
|
2023-01-10 08:35:44 -05:00
|
|
|
self.dynamic_permissions.clone()
|
2021-10-10 17:26:22 -04:00
|
|
|
} 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-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,
|
|
|
|
)
|
|
|
|
.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> {
|
2023-02-09 22:00:23 -05:00
|
|
|
let graph = self.ps.graph();
|
|
|
|
let code = match graph.get(&resolve_url(file_name).ok()?) {
|
2023-02-22 14:15:25 -05:00
|
|
|
Some(deno_graph::Module::Esm(module)) => &module.source,
|
|
|
|
Some(deno_graph::Module::Json(module)) => &module.source,
|
2022-07-19 11:58:18 -04: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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|