2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2024-05-16 03:09:35 -04:00
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::pin::Pin;
|
|
|
|
use std::rc::Rc;
|
|
|
|
use std::str;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2024-02-28 16:30:45 -05:00
|
|
|
use crate::args::jsr_url;
|
2023-04-14 16:22:33 -04:00
|
|
|
use crate::args::CliOptions;
|
|
|
|
use crate::args::DenoSubcommand;
|
2022-11-25 18:29:48 -05:00
|
|
|
use crate::args::TsTypeLib;
|
2024-04-17 10:19:55 -04:00
|
|
|
use crate::cache::CodeCache;
|
|
|
|
use crate::cache::ModuleInfoCache;
|
2023-04-14 16:22:33 -04:00
|
|
|
use crate::cache::ParsedSourceCache;
|
|
|
|
use crate::emit::Emitter;
|
2024-05-08 15:34:46 -04:00
|
|
|
use crate::factory::CliFactory;
|
2024-05-16 03:09:35 -04:00
|
|
|
use crate::graph_container::MainModuleGraphContainer;
|
|
|
|
use crate::graph_container::ModuleGraphContainer;
|
|
|
|
use crate::graph_container::ModuleGraphUpdatePermit;
|
2023-04-14 16:22:33 -04:00
|
|
|
use crate::graph_util::graph_lock_or_exit;
|
2024-02-16 17:10:06 -05:00
|
|
|
use crate::graph_util::CreateGraphOptions;
|
2023-04-14 16:22:33 -04:00
|
|
|
use crate::graph_util::ModuleGraphBuilder;
|
2022-08-20 11:31:33 -04:00
|
|
|
use crate::node;
|
2023-04-14 16:22:33 -04:00
|
|
|
use crate::resolver::CliGraphResolver;
|
2024-01-27 12:10:36 -05:00
|
|
|
use crate::resolver::CliNodeResolver;
|
|
|
|
use crate::resolver::ModuleCodeStringSource;
|
|
|
|
use crate::resolver::NpmModuleLoader;
|
2023-04-14 16:22:33 -04:00
|
|
|
use crate::tools::check;
|
2023-04-14 18:05:46 -04:00
|
|
|
use crate::tools::check::TypeChecker;
|
2023-04-14 16:22:33 -04:00
|
|
|
use crate::util::progress_bar::ProgressBar;
|
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;
|
2024-05-16 03:09:35 -04:00
|
|
|
use crate::worker::ModuleLoaderAndSourceMapGetter;
|
2023-05-01 08:59:38 -04:00
|
|
|
use crate::worker::ModuleLoaderFactory;
|
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;
|
2024-02-28 16:30:45 -05:00
|
|
|
use deno_core::anyhow::bail;
|
2022-09-03 09:43:35 -04:00
|
|
|
use deno_core::anyhow::Context;
|
2023-04-14 16:22:33 -04:00
|
|
|
use deno_core::error::custom_error;
|
|
|
|
use deno_core::error::generic_error;
|
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;
|
2023-04-14 16:22:33 -04:00
|
|
|
use deno_core::parking_lot::Mutex;
|
2022-07-19 11:58:18 -04:00
|
|
|
use deno_core::resolve_url;
|
2024-01-09 23:18:40 -05:00
|
|
|
use deno_core::ModuleCodeString;
|
2020-03-02 13:12:49 -05:00
|
|
|
use deno_core::ModuleLoader;
|
2022-07-19 11:58:18 -04:00
|
|
|
use deno_core::ModuleSource;
|
2024-01-02 20:34:41 -05:00
|
|
|
use deno_core::ModuleSourceCode;
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::ModuleSpecifier;
|
2022-07-19 11:58:18 -04:00
|
|
|
use deno_core::ModuleType;
|
2024-01-14 20:28:46 -05:00
|
|
|
use deno_core::RequestedModuleType;
|
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-10-24 09:37:02 -04:00
|
|
|
use deno_graph::source::ResolutionMode;
|
2023-04-14 16:22:33 -04:00
|
|
|
use deno_graph::source::Resolver;
|
2024-05-16 03:09:35 -04:00
|
|
|
use deno_graph::GraphKind;
|
2024-01-31 22:15:22 -05:00
|
|
|
use deno_graph::JsModule;
|
2023-02-22 14:15:25 -05:00
|
|
|
use deno_graph::JsonModule;
|
2023-04-14 16:22:33 -04:00
|
|
|
use deno_graph::Module;
|
2024-05-16 03:09:35 -04:00
|
|
|
use deno_graph::ModuleGraph;
|
2023-04-14 16:22:33 -04:00
|
|
|
use deno_graph::Resolution;
|
|
|
|
use deno_lockfile::Lockfile;
|
2024-04-17 10:19:55 -04:00
|
|
|
use deno_runtime::code_cache;
|
2024-03-05 19:23:51 -05:00
|
|
|
use deno_runtime::deno_node::NodeResolutionMode;
|
2024-04-17 10:19:55 -04:00
|
|
|
use deno_runtime::fs_util::code_timestamp;
|
2023-01-07 11:25:34 -05:00
|
|
|
use deno_runtime::permissions::PermissionsContainer;
|
2023-04-14 16:22:33 -04:00
|
|
|
use deno_semver::npm::NpmPackageReqReference;
|
|
|
|
|
2024-05-08 15:34:46 -04:00
|
|
|
pub async fn load_top_level_deps(factory: &CliFactory) -> Result<(), AnyError> {
|
|
|
|
let npm_resolver = factory.npm_resolver().await?;
|
|
|
|
if let Some(npm_resolver) = npm_resolver.as_managed() {
|
|
|
|
npm_resolver.ensure_top_level_package_json_install().await?;
|
|
|
|
npm_resolver.resolve_pending().await?;
|
|
|
|
}
|
|
|
|
// cache as many entries in the import map as we can
|
|
|
|
if let Some(import_map) = factory.maybe_import_map().await? {
|
|
|
|
let roots = import_map
|
|
|
|
.imports()
|
|
|
|
.entries()
|
|
|
|
.filter_map(|entry| {
|
|
|
|
if entry.key.ends_with('/') {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
entry.value.cloned()
|
|
|
|
}
|
|
|
|
})
|
2024-05-16 03:09:35 -04:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
let mut graph_permit = factory
|
|
|
|
.main_module_graph_container()
|
|
|
|
.await?
|
|
|
|
.acquire_update_permit()
|
|
|
|
.await;
|
|
|
|
let graph = graph_permit.graph_mut();
|
2024-05-08 15:34:46 -04:00
|
|
|
factory
|
|
|
|
.module_load_preparer()
|
|
|
|
.await?
|
|
|
|
.prepare_module_load(
|
2024-05-16 03:09:35 -04:00
|
|
|
graph,
|
|
|
|
&roots,
|
2024-05-08 15:34:46 -04:00
|
|
|
false,
|
|
|
|
factory.cli_options().ts_type_lib_window(),
|
|
|
|
deno_runtime::permissions::PermissionsContainer::allow_all(),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-04-14 16:22:33 -04:00
|
|
|
pub struct ModuleLoadPreparer {
|
|
|
|
options: Arc<CliOptions>,
|
|
|
|
lockfile: Option<Arc<Mutex<Lockfile>>>,
|
|
|
|
module_graph_builder: Arc<ModuleGraphBuilder>,
|
|
|
|
progress_bar: ProgressBar,
|
2023-04-14 18:05:46 -04:00
|
|
|
type_checker: Arc<TypeChecker>,
|
2023-04-14 16:22:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ModuleLoadPreparer {
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
|
|
pub fn new(
|
|
|
|
options: Arc<CliOptions>,
|
|
|
|
lockfile: Option<Arc<Mutex<Lockfile>>>,
|
|
|
|
module_graph_builder: Arc<ModuleGraphBuilder>,
|
|
|
|
progress_bar: ProgressBar,
|
2023-04-14 18:05:46 -04:00
|
|
|
type_checker: Arc<TypeChecker>,
|
2023-04-14 16:22:33 -04:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
options,
|
|
|
|
lockfile,
|
|
|
|
module_graph_builder,
|
|
|
|
progress_bar,
|
2023-04-14 18:05:46 -04:00
|
|
|
type_checker,
|
2023-04-14 16:22:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This method must be called for a module or a static importer of that
|
|
|
|
/// module before attempting to `load()` it from a `JsRuntime`. It will
|
|
|
|
/// populate the graph data in memory with the necessary source code, write
|
|
|
|
/// emits where necessary or report any module graph / type checking errors.
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
|
|
pub async fn prepare_module_load(
|
|
|
|
&self,
|
2024-05-16 03:09:35 -04:00
|
|
|
graph: &mut ModuleGraph,
|
|
|
|
roots: &[ModuleSpecifier],
|
2023-04-14 16:22:33 -04:00
|
|
|
is_dynamic: bool,
|
|
|
|
lib: TsTypeLib,
|
2023-04-26 16:23:28 -04:00
|
|
|
permissions: PermissionsContainer,
|
2023-04-14 16:22:33 -04:00
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
log::debug!("Preparing module load.");
|
|
|
|
let _pb_clear_guard = self.progress_bar.clear_guard();
|
|
|
|
|
2023-04-26 16:23:28 -04:00
|
|
|
let mut cache = self.module_graph_builder.create_fetch_cacher(permissions);
|
2024-05-16 03:09:35 -04:00
|
|
|
log::debug!("Building module graph.");
|
2024-03-13 16:38:01 -04:00
|
|
|
let has_type_checked = !graph.roots.is_empty();
|
2023-04-14 16:22:33 -04:00
|
|
|
|
|
|
|
self
|
|
|
|
.module_graph_builder
|
|
|
|
.build_graph_with_npm_resolution(
|
|
|
|
graph,
|
2024-02-16 17:10:06 -05:00
|
|
|
CreateGraphOptions {
|
2023-04-14 16:22:33 -04:00
|
|
|
is_dynamic,
|
2024-02-16 17:10:06 -05:00
|
|
|
graph_kind: graph.graph_kind(),
|
2024-05-16 03:09:35 -04:00
|
|
|
roots: roots.to_vec(),
|
2024-02-16 17:10:06 -05:00
|
|
|
loader: Some(&mut cache),
|
2023-04-14 16:22:33 -04:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
2024-05-16 03:09:35 -04:00
|
|
|
self.module_graph_builder.graph_roots_valid(graph, roots)?;
|
2023-04-14 18:05:46 -04:00
|
|
|
|
|
|
|
// If there is a lockfile...
|
2023-04-14 16:22:33 -04:00
|
|
|
if let Some(lockfile) = &self.lockfile {
|
2023-04-14 18:05:46 -04:00
|
|
|
let mut lockfile = lockfile.lock();
|
|
|
|
// validate the integrity of all the modules
|
|
|
|
graph_lock_or_exit(graph, &mut lockfile);
|
|
|
|
// update it with anything new
|
2023-05-22 16:55:04 -04:00
|
|
|
lockfile.write().context("Failed writing lockfile.")?;
|
2023-04-14 16:22:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
drop(_pb_clear_guard);
|
|
|
|
|
|
|
|
// type check if necessary
|
2024-03-13 16:38:01 -04:00
|
|
|
if self.options.type_check_mode().is_true() && !has_type_checked {
|
2023-04-14 18:05:46 -04:00
|
|
|
self
|
|
|
|
.type_checker
|
|
|
|
.check(
|
2024-03-13 16:38:01 -04:00
|
|
|
// todo(perf): since this is only done the first time the graph is
|
|
|
|
// created, we could avoid the clone of the graph here by providing
|
|
|
|
// the actual graph on the first run and then getting the Arc<ModuleGraph>
|
|
|
|
// back from the return value.
|
2024-05-16 03:09:35 -04:00
|
|
|
graph.clone(),
|
2023-04-14 18:05:46 -04:00
|
|
|
check::CheckOptions {
|
2024-02-20 16:29:57 -05:00
|
|
|
build_fast_check_graph: true,
|
2023-04-14 18:05:46 -04:00
|
|
|
lib,
|
|
|
|
log_ignored_options: false,
|
2024-03-13 16:38:01 -04:00
|
|
|
reload: self.options.reload_flag(),
|
2024-02-21 08:35:25 -05:00
|
|
|
type_check_mode: self.options.type_check_mode(),
|
2023-04-14 18:05:46 -04:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await?;
|
2023-04-14 16:22:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
log::debug!("Prepared module load.");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-05-01 08:59:38 -04:00
|
|
|
}
|
|
|
|
|
2023-04-27 10:05:20 -04:00
|
|
|
struct SharedCliModuleLoaderState {
|
2024-05-16 03:09:35 -04:00
|
|
|
graph_kind: GraphKind,
|
2023-04-27 10:05:20 -04:00
|
|
|
lib_window: TsTypeLib,
|
|
|
|
lib_worker: TsTypeLib,
|
|
|
|
is_inspecting: bool,
|
|
|
|
is_repl: bool,
|
2024-05-16 03:09:35 -04:00
|
|
|
code_cache: Option<Arc<CodeCache>>,
|
|
|
|
emitter: Arc<Emitter>,
|
|
|
|
main_module_graph_container: Arc<MainModuleGraphContainer>,
|
|
|
|
module_info_cache: Arc<ModuleInfoCache>,
|
2023-04-14 16:22:33 -04:00
|
|
|
module_load_preparer: Arc<ModuleLoadPreparer>,
|
2023-10-03 19:05:06 -04:00
|
|
|
node_resolver: Arc<CliNodeResolver>,
|
2023-04-25 18:36:31 -04:00
|
|
|
npm_module_loader: NpmModuleLoader,
|
2024-05-16 03:09:35 -04:00
|
|
|
parsed_source_cache: Arc<ParsedSourceCache>,
|
|
|
|
resolver: Arc<CliGraphResolver>,
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
2023-04-27 10:05:20 -04:00
|
|
|
pub struct CliModuleLoaderFactory {
|
2023-05-01 08:59:38 -04:00
|
|
|
shared: Arc<SharedCliModuleLoaderState>,
|
2023-04-27 10:05:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CliModuleLoaderFactory {
|
2023-10-03 19:05:06 -04:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2023-01-10 08:35:44 -05:00
|
|
|
pub fn new(
|
2023-04-27 10:05:20 -04:00
|
|
|
options: &CliOptions,
|
2024-05-16 03:09:35 -04:00
|
|
|
code_cache: Option<Arc<CodeCache>>,
|
2023-04-27 10:05:20 -04:00
|
|
|
emitter: Arc<Emitter>,
|
2024-05-16 03:09:35 -04:00
|
|
|
main_module_graph_container: Arc<MainModuleGraphContainer>,
|
|
|
|
module_info_cache: Arc<ModuleInfoCache>,
|
2023-04-27 10:05:20 -04:00
|
|
|
module_load_preparer: Arc<ModuleLoadPreparer>,
|
2023-10-03 19:05:06 -04:00
|
|
|
node_resolver: Arc<CliNodeResolver>,
|
2023-04-27 10:05:20 -04:00
|
|
|
npm_module_loader: NpmModuleLoader,
|
2024-05-16 03:09:35 -04:00
|
|
|
parsed_source_cache: Arc<ParsedSourceCache>,
|
|
|
|
resolver: Arc<CliGraphResolver>,
|
2023-04-27 10:05:20 -04:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
2023-05-01 08:59:38 -04:00
|
|
|
shared: Arc::new(SharedCliModuleLoaderState {
|
2024-05-16 03:09:35 -04:00
|
|
|
graph_kind: options.graph_kind(),
|
2023-04-27 10:05:20 -04:00
|
|
|
lib_window: options.ts_type_lib_window(),
|
|
|
|
lib_worker: options.ts_type_lib_worker(),
|
|
|
|
is_inspecting: options.is_inspecting(),
|
2023-09-15 20:42:09 -04:00
|
|
|
is_repl: matches!(
|
|
|
|
options.sub_command(),
|
|
|
|
DenoSubcommand::Repl(_) | DenoSubcommand::Jupyter(_)
|
|
|
|
),
|
2024-05-16 03:09:35 -04:00
|
|
|
code_cache,
|
|
|
|
emitter,
|
|
|
|
main_module_graph_container,
|
|
|
|
module_info_cache,
|
2023-04-27 10:05:20 -04:00
|
|
|
module_load_preparer,
|
2023-10-03 19:05:06 -04:00
|
|
|
node_resolver,
|
2023-04-27 10:05:20 -04:00
|
|
|
npm_module_loader,
|
2024-05-16 03:09:35 -04:00
|
|
|
parsed_source_cache,
|
|
|
|
resolver,
|
2023-04-27 10:05:20 -04:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-16 03:09:35 -04:00
|
|
|
fn create_with_lib<TGraphContainer: ModuleGraphContainer>(
|
2023-04-27 10:05:20 -04:00
|
|
|
&self,
|
2024-05-16 03:09:35 -04:00
|
|
|
graph_container: TGraphContainer,
|
2023-05-01 08:59:38 -04:00
|
|
|
lib: TsTypeLib,
|
2023-01-10 08:35:44 -05:00
|
|
|
root_permissions: PermissionsContainer,
|
|
|
|
dynamic_permissions: PermissionsContainer,
|
2024-05-16 03:09:35 -04:00
|
|
|
) -> ModuleLoaderAndSourceMapGetter {
|
|
|
|
let loader = Rc::new(CliModuleLoader {
|
2023-05-01 08:59:38 -04:00
|
|
|
lib,
|
2023-01-10 08:35:44 -05:00
|
|
|
root_permissions,
|
|
|
|
dynamic_permissions,
|
2024-05-16 03:09:35 -04:00
|
|
|
graph_container,
|
|
|
|
emitter: self.shared.emitter.clone(),
|
|
|
|
parsed_source_cache: self.shared.parsed_source_cache.clone(),
|
2023-05-01 08:59:38 -04:00
|
|
|
shared: self.shared.clone(),
|
2024-05-16 03:09:35 -04:00
|
|
|
});
|
|
|
|
ModuleLoaderAndSourceMapGetter {
|
|
|
|
module_loader: loader.clone(),
|
|
|
|
source_map_getter: Some(loader),
|
|
|
|
}
|
2020-09-19 19:17:35 -04:00
|
|
|
}
|
2023-05-01 08:59:38 -04:00
|
|
|
}
|
2020-09-19 19:17:35 -04:00
|
|
|
|
2023-05-01 08:59:38 -04:00
|
|
|
impl ModuleLoaderFactory for CliModuleLoaderFactory {
|
|
|
|
fn create_for_main(
|
2023-04-27 10:05:20 -04:00
|
|
|
&self,
|
2023-01-10 08:35:44 -05:00
|
|
|
root_permissions: PermissionsContainer,
|
|
|
|
dynamic_permissions: PermissionsContainer,
|
2024-05-16 03:09:35 -04:00
|
|
|
) -> ModuleLoaderAndSourceMapGetter {
|
2023-04-27 10:05:20 -04:00
|
|
|
self.create_with_lib(
|
2024-05-16 03:09:35 -04:00
|
|
|
(*self.shared.main_module_graph_container).clone(),
|
2023-05-01 08:59:38 -04:00
|
|
|
self.shared.lib_window,
|
2023-01-10 08:35:44 -05:00
|
|
|
root_permissions,
|
|
|
|
dynamic_permissions,
|
2023-04-27 10:05:20 -04:00
|
|
|
)
|
2020-09-19 19:17:35 -04:00
|
|
|
}
|
2022-07-19 11:58:18 -04:00
|
|
|
|
2023-05-01 08:59:38 -04:00
|
|
|
fn create_for_worker(
|
2023-04-27 10:05:20 -04:00
|
|
|
&self,
|
|
|
|
root_permissions: PermissionsContainer,
|
|
|
|
dynamic_permissions: PermissionsContainer,
|
2024-05-16 03:09:35 -04:00
|
|
|
) -> ModuleLoaderAndSourceMapGetter {
|
2023-05-01 08:59:38 -04:00
|
|
|
self.create_with_lib(
|
2024-05-16 03:09:35 -04:00
|
|
|
// create a fresh module graph for the worker
|
|
|
|
WorkerModuleGraphContainer::new(Arc::new(ModuleGraph::new(
|
|
|
|
self.shared.graph_kind,
|
|
|
|
))),
|
2023-05-01 08:59:38 -04:00
|
|
|
self.shared.lib_worker,
|
2023-04-27 10:05:20 -04:00
|
|
|
root_permissions,
|
|
|
|
dynamic_permissions,
|
2023-05-01 08:59:38 -04:00
|
|
|
)
|
|
|
|
}
|
2023-04-27 10:05:20 -04:00
|
|
|
}
|
|
|
|
|
2024-05-16 03:09:35 -04:00
|
|
|
struct CliModuleLoader<TGraphContainer: ModuleGraphContainer> {
|
2023-04-27 10:05:20 -04:00
|
|
|
lib: TsTypeLib,
|
|
|
|
/// The initial set of permissions used to resolve the static imports in the
|
|
|
|
/// worker. These are "allow all" for main worker, and parent thread
|
|
|
|
/// permissions for Web Worker.
|
|
|
|
root_permissions: PermissionsContainer,
|
|
|
|
/// Permissions used to resolve dynamic imports, these get passed as
|
|
|
|
/// "root permissions" for Web Worker.
|
|
|
|
dynamic_permissions: PermissionsContainer,
|
|
|
|
shared: Arc<SharedCliModuleLoaderState>,
|
2024-05-16 03:09:35 -04:00
|
|
|
emitter: Arc<Emitter>,
|
|
|
|
parsed_source_cache: Arc<ParsedSourceCache>,
|
|
|
|
graph_container: TGraphContainer,
|
2023-04-27 10:05:20 -04:00
|
|
|
}
|
|
|
|
|
2024-05-16 03:09:35 -04:00
|
|
|
impl<TGraphContainer: ModuleGraphContainer> CliModuleLoader<TGraphContainer> {
|
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,
|
2024-01-14 20:28:46 -05:00
|
|
|
requested_module_type: RequestedModuleType,
|
2022-08-20 11:31:33 -04:00
|
|
|
) -> Result<ModuleSource, AnyError> {
|
2023-04-25 18:36:31 -04:00
|
|
|
let permissions = if is_dynamic {
|
|
|
|
&self.dynamic_permissions
|
|
|
|
} else {
|
|
|
|
&self.root_permissions
|
|
|
|
};
|
2023-05-10 20:06:59 -04:00
|
|
|
let code_source = if let Some(result) = self
|
|
|
|
.shared
|
|
|
|
.npm_module_loader
|
|
|
|
.load_sync_if_in_npm_package(specifier, maybe_referrer, permissions)
|
|
|
|
{
|
|
|
|
result?
|
2022-08-20 11:31:33 -04:00
|
|
|
} else {
|
2024-05-16 03:09:35 -04:00
|
|
|
self.load_prepared_module(specifier, maybe_referrer)?
|
2022-08-20 11:31:33 -04:00
|
|
|
};
|
2023-04-27 10:05:20 -04:00
|
|
|
let code = if self.shared.is_inspecting {
|
2022-08-20 11:31:33 -04:00
|
|
|
// 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)
|
|
|
|
};
|
2024-01-14 20:28:46 -05:00
|
|
|
let module_type = match code_source.media_type {
|
|
|
|
MediaType::Json => ModuleType::Json,
|
|
|
|
_ => ModuleType::JavaScript,
|
|
|
|
};
|
|
|
|
|
|
|
|
// If we loaded a JSON file, but the "requested_module_type" (that is computed from
|
|
|
|
// import attributes) is not JSON we need to fail.
|
|
|
|
if module_type == ModuleType::Json
|
|
|
|
&& requested_module_type != RequestedModuleType::Json
|
|
|
|
{
|
|
|
|
return Err(generic_error("Attempted to load JSON module without specifying \"type\": \"json\" attribute in the import statement."));
|
|
|
|
}
|
|
|
|
|
2024-04-17 10:19:55 -04:00
|
|
|
let code_cache = if module_type == ModuleType::JavaScript {
|
|
|
|
self.shared.code_cache.as_ref().and_then(|cache| {
|
|
|
|
let code_hash = self
|
|
|
|
.get_code_hash_or_timestamp(specifier, code_source.media_type)
|
|
|
|
.ok()
|
|
|
|
.flatten();
|
|
|
|
if let Some(code_hash) = code_hash {
|
|
|
|
cache
|
|
|
|
.get_sync(
|
|
|
|
specifier.as_str(),
|
|
|
|
code_cache::CodeCacheType::EsModule,
|
|
|
|
&code_hash,
|
|
|
|
)
|
|
|
|
.map(Cow::from)
|
|
|
|
.inspect(|_| {
|
|
|
|
// This log line is also used by tests.
|
|
|
|
log::debug!(
|
|
|
|
"V8 code cache hit for ES module: {specifier}, [{code_hash:?}]"
|
|
|
|
);
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2023-04-04 08:46:31 -04:00
|
|
|
Ok(ModuleSource::new_with_redirect(
|
2024-01-14 20:28:46 -05:00
|
|
|
module_type,
|
2024-01-02 20:34:41 -05:00
|
|
|
ModuleSourceCode::String(code),
|
2023-04-04 08:46:31 -04:00
|
|
|
specifier,
|
|
|
|
&code_source.found_url,
|
2024-04-17 10:19:55 -04:00
|
|
|
code_cache,
|
2023-04-04 08:46:31 -04:00
|
|
|
))
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
2020-04-27 19:12:38 -04:00
|
|
|
|
2024-02-28 16:30:45 -05:00
|
|
|
fn resolve_referrer(
|
2019-06-09 09:08:20 -04:00
|
|
|
&self,
|
|
|
|
referrer: &str,
|
2024-02-28 16:30:45 -05:00
|
|
|
) -> Result<ModuleSpecifier, AnyError> {
|
|
|
|
// TODO(bartlomieju): ideally we shouldn't need to call `current_dir()` on each
|
|
|
|
// call - maybe it should be caller's responsibility to pass it as an arg?
|
|
|
|
let cwd = std::env::current_dir().context("Unable to get CWD")?;
|
|
|
|
if referrer.is_empty() && self.shared.is_repl {
|
|
|
|
// FIXME(bartlomieju): this is a hacky way to provide compatibility with REPL
|
|
|
|
// and `Deno.core.evalContext` API. Ideally we should always have a referrer filled
|
|
|
|
// but sadly that's not the case due to missing APIs in V8.
|
|
|
|
deno_core::resolve_path("./$deno$repl.ts", &cwd).map_err(|e| e.into())
|
|
|
|
} else {
|
|
|
|
deno_core::resolve_url_or_path(referrer, &cwd).map_err(|e| e.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inner_resolve(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &ModuleSpecifier,
|
2023-01-10 08:35:44 -05:00
|
|
|
kind: ResolutionKind,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<ModuleSpecifier, AnyError> {
|
2023-04-24 21:07:48 -04:00
|
|
|
let permissions = if matches!(kind, ResolutionKind::DynamicImport) {
|
|
|
|
&self.dynamic_permissions
|
2023-01-10 08:35:44 -05:00
|
|
|
} else {
|
2023-04-24 21:07:48 -04:00
|
|
|
&self.root_permissions
|
2023-01-10 08:35:44 -05:00
|
|
|
};
|
2023-04-14 16:22:33 -04:00
|
|
|
|
2024-02-28 16:30:45 -05:00
|
|
|
if let Some(result) = self.shared.node_resolver.resolve_if_in_npm_package(
|
|
|
|
specifier,
|
|
|
|
referrer,
|
2024-03-05 19:23:51 -05:00
|
|
|
NodeResolutionMode::Execution,
|
2024-02-28 16:30:45 -05:00
|
|
|
permissions,
|
|
|
|
) {
|
2024-03-05 19:23:51 -05:00
|
|
|
return match result? {
|
|
|
|
Some(res) => Ok(res.into_url()),
|
|
|
|
None => Err(generic_error("not found")),
|
|
|
|
};
|
2024-02-28 16:30:45 -05:00
|
|
|
}
|
2023-04-14 16:22:33 -04:00
|
|
|
|
2024-05-16 03:09:35 -04:00
|
|
|
let graph = self.graph_container.graph();
|
2024-02-28 16:30:45 -05:00
|
|
|
let maybe_resolved = match graph.get(referrer) {
|
|
|
|
Some(Module::Js(module)) => {
|
|
|
|
module.dependencies.get(specifier).map(|d| &d.maybe_code)
|
2023-04-14 16:22:33 -04:00
|
|
|
}
|
2024-02-28 16:30:45 -05:00
|
|
|
_ => None,
|
|
|
|
};
|
2023-04-14 16:22:33 -04:00
|
|
|
|
2024-02-28 16:30:45 -05:00
|
|
|
match maybe_resolved {
|
|
|
|
Some(Resolution::Ok(resolved)) => {
|
|
|
|
let specifier = &resolved.specifier;
|
|
|
|
let specifier = match graph.get(specifier) {
|
|
|
|
Some(Module::Npm(module)) => {
|
|
|
|
let package_folder = self
|
|
|
|
.shared
|
|
|
|
.node_resolver
|
|
|
|
.npm_resolver
|
|
|
|
.as_managed()
|
|
|
|
.unwrap() // byonm won't create a Module::Npm
|
|
|
|
.resolve_pkg_folder_from_deno_module(module.nv_reference.nv())?;
|
2024-03-05 19:23:51 -05:00
|
|
|
let maybe_resolution = self
|
2024-02-28 16:30:45 -05:00
|
|
|
.shared
|
|
|
|
.node_resolver
|
2024-03-05 19:23:51 -05:00
|
|
|
.resolve_package_sub_path_from_deno_module(
|
2024-02-28 16:30:45 -05:00
|
|
|
&package_folder,
|
|
|
|
module.nv_reference.sub_path(),
|
|
|
|
referrer,
|
2024-03-05 19:23:51 -05:00
|
|
|
NodeResolutionMode::Execution,
|
2024-02-28 16:30:45 -05:00
|
|
|
permissions,
|
|
|
|
)
|
|
|
|
.with_context(|| {
|
|
|
|
format!("Could not resolve '{}'.", module.nv_reference)
|
2024-03-05 19:23:51 -05:00
|
|
|
})?;
|
|
|
|
match maybe_resolution {
|
|
|
|
Some(res) => res.into_url(),
|
|
|
|
None => return Err(generic_error("not found")),
|
|
|
|
}
|
2024-02-28 16:30:45 -05:00
|
|
|
}
|
|
|
|
Some(Module::Node(module)) => module.specifier.clone(),
|
|
|
|
Some(Module::Js(module)) => module.specifier.clone(),
|
|
|
|
Some(Module::Json(module)) => module.specifier.clone(),
|
|
|
|
Some(Module::External(module)) => {
|
|
|
|
node::resolve_specifier_into_node_modules(&module.specifier)
|
|
|
|
}
|
|
|
|
None => specifier.clone(),
|
|
|
|
};
|
|
|
|
return Ok(specifier);
|
|
|
|
}
|
|
|
|
Some(Resolution::Err(err)) => {
|
|
|
|
return Err(custom_error(
|
|
|
|
"TypeError",
|
|
|
|
format!("{}\n", err.to_string_with_range()),
|
|
|
|
))
|
2023-04-14 16:22:33 -04:00
|
|
|
}
|
2024-02-28 16:30:45 -05:00
|
|
|
Some(Resolution::None) | None => {}
|
2023-04-14 16:22:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME(bartlomieju): this is another hack way to provide NPM specifier
|
|
|
|
// support in REPL. This should be fixed.
|
2023-10-24 09:37:02 -04:00
|
|
|
let resolution = self.shared.resolver.resolve(
|
|
|
|
specifier,
|
2023-12-06 19:03:18 -05:00
|
|
|
&deno_graph::Range {
|
|
|
|
specifier: referrer.clone(),
|
|
|
|
start: deno_graph::Position::zeroed(),
|
|
|
|
end: deno_graph::Position::zeroed(),
|
|
|
|
},
|
2023-10-24 09:37:02 -04:00
|
|
|
ResolutionMode::Execution,
|
|
|
|
);
|
2023-04-14 16:22:33 -04:00
|
|
|
|
2023-04-27 10:05:20 -04:00
|
|
|
if self.shared.is_repl {
|
2023-04-14 16:22:33 -04:00
|
|
|
let specifier = resolution
|
|
|
|
.as_ref()
|
|
|
|
.ok()
|
|
|
|
.map(Cow::Borrowed)
|
|
|
|
.or_else(|| ModuleSpecifier::parse(specifier).ok().map(Cow::Owned));
|
|
|
|
if let Some(specifier) = specifier {
|
|
|
|
if let Ok(reference) =
|
|
|
|
NpmPackageReqReference::from_specifier(&specifier)
|
|
|
|
{
|
2024-03-05 19:23:51 -05:00
|
|
|
return self
|
|
|
|
.shared
|
|
|
|
.node_resolver
|
|
|
|
.resolve_req_reference(
|
|
|
|
&reference,
|
|
|
|
permissions,
|
|
|
|
referrer,
|
|
|
|
NodeResolutionMode::Execution,
|
|
|
|
)
|
|
|
|
.map(|res| res.into_url());
|
2023-04-14 16:22:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-20 00:02:08 -04:00
|
|
|
resolution.map_err(|err| err.into())
|
2019-06-05 16:35:38 -04:00
|
|
|
}
|
2024-04-17 10:19:55 -04:00
|
|
|
|
|
|
|
fn get_code_hash_or_timestamp(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
media_type: MediaType,
|
|
|
|
) -> Result<Option<String>, AnyError> {
|
|
|
|
let hash = self
|
|
|
|
.shared
|
|
|
|
.module_info_cache
|
|
|
|
.get_module_source_hash(specifier, media_type)?;
|
|
|
|
if let Some(hash) = hash {
|
|
|
|
return Ok(Some(hash.into()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the modified timestamp from the local file system if we don't have a hash.
|
|
|
|
let timestamp = code_timestamp(specifier.as_str())
|
|
|
|
.map(|timestamp| timestamp.to_string())?;
|
|
|
|
Ok(Some(timestamp))
|
|
|
|
}
|
2024-05-16 03:09:35 -04:00
|
|
|
|
|
|
|
fn load_prepared_module(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
maybe_referrer: Option<&ModuleSpecifier>,
|
|
|
|
) -> Result<ModuleCodeStringSource, AnyError> {
|
|
|
|
if specifier.scheme() == "node" {
|
|
|
|
unreachable!(); // Node built-in modules should be handled internally.
|
|
|
|
}
|
|
|
|
|
|
|
|
let graph = self.graph_container.graph();
|
|
|
|
match graph.get(specifier) {
|
|
|
|
Some(deno_graph::Module::Json(JsonModule {
|
|
|
|
source,
|
|
|
|
media_type,
|
|
|
|
specifier,
|
|
|
|
..
|
|
|
|
})) => Ok(ModuleCodeStringSource {
|
|
|
|
code: source.clone().into(),
|
|
|
|
found_url: specifier.clone(),
|
|
|
|
media_type: *media_type,
|
|
|
|
}),
|
|
|
|
Some(deno_graph::Module::Js(JsModule {
|
|
|
|
source,
|
|
|
|
media_type,
|
|
|
|
specifier,
|
|
|
|
..
|
|
|
|
})) => {
|
|
|
|
let code: ModuleCodeString = match media_type {
|
|
|
|
MediaType::JavaScript
|
|
|
|
| MediaType::Unknown
|
|
|
|
| MediaType::Cjs
|
|
|
|
| MediaType::Mjs
|
|
|
|
| MediaType::Json => source.clone().into(),
|
|
|
|
MediaType::Dts | MediaType::Dcts | MediaType::Dmts => {
|
|
|
|
Default::default()
|
|
|
|
}
|
|
|
|
MediaType::TypeScript
|
|
|
|
| MediaType::Mts
|
|
|
|
| MediaType::Cts
|
|
|
|
| MediaType::Jsx
|
|
|
|
| MediaType::Tsx => {
|
|
|
|
// get emit text
|
|
|
|
self
|
|
|
|
.emitter
|
|
|
|
.emit_parsed_source(specifier, *media_type, source)?
|
|
|
|
}
|
|
|
|
MediaType::TsBuildInfo | MediaType::Wasm | MediaType::SourceMap => {
|
|
|
|
panic!("Unexpected media type {media_type} for {specifier}")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// at this point, we no longer need the parsed source in memory, so free it
|
|
|
|
self.parsed_source_cache.free(specifier);
|
|
|
|
|
|
|
|
Ok(ModuleCodeStringSource {
|
|
|
|
code,
|
|
|
|
found_url: specifier.clone(),
|
|
|
|
media_type: *media_type,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
Some(
|
|
|
|
deno_graph::Module::External(_)
|
|
|
|
| deno_graph::Module::Node(_)
|
|
|
|
| deno_graph::Module::Npm(_),
|
|
|
|
)
|
|
|
|
| None => {
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-02-28 16:30:45 -05:00
|
|
|
}
|
|
|
|
|
2024-05-16 03:09:35 -04:00
|
|
|
impl<TGraphContainer: ModuleGraphContainer> ModuleLoader
|
|
|
|
for CliModuleLoader<TGraphContainer>
|
|
|
|
{
|
2024-02-28 16:30:45 -05:00
|
|
|
fn resolve(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &str,
|
|
|
|
kind: ResolutionKind,
|
|
|
|
) -> Result<ModuleSpecifier, AnyError> {
|
|
|
|
fn ensure_not_jsr_non_jsr_remote_import(
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
referrer: &ModuleSpecifier,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
if referrer.as_str().starts_with(jsr_url().as_str())
|
|
|
|
&& !specifier.as_str().starts_with(jsr_url().as_str())
|
|
|
|
&& matches!(specifier.scheme(), "http" | "https")
|
|
|
|
{
|
|
|
|
bail!("Importing {} blocked. JSR packages cannot import non-JSR remote modules for security reasons.", specifier);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
let referrer = self.resolve_referrer(referrer)?;
|
|
|
|
let specifier = self.inner_resolve(specifier, &referrer, kind)?;
|
|
|
|
ensure_not_jsr_non_jsr_remote_import(&specifier, &referrer)?;
|
|
|
|
Ok(specifier)
|
|
|
|
}
|
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,
|
2024-01-14 20:28:46 -05:00
|
|
|
requested_module_type: RequestedModuleType,
|
2024-01-27 01:57:41 -05:00
|
|
|
) -> deno_core::ModuleLoadResponse {
|
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.
|
2024-01-27 01:57:41 -05:00
|
|
|
deno_core::ModuleLoadResponse::Sync(self.load_sync(
|
2023-01-10 08:35:44 -05:00
|
|
|
specifier,
|
|
|
|
maybe_referrer,
|
|
|
|
is_dynamic,
|
2024-01-14 20:28:46 -05:00
|
|
|
requested_module_type,
|
2024-01-27 01:57:41 -05:00
|
|
|
))
|
2019-06-05 16:35:38 -04:00
|
|
|
}
|
2020-04-30 08:37:06 -04:00
|
|
|
|
|
|
|
fn prepare_load(
|
|
|
|
&self,
|
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>>>> {
|
2024-05-16 03:09:35 -04:00
|
|
|
if self.shared.node_resolver.in_npm_package(specifier) {
|
|
|
|
return Box::pin(deno_core::futures::future::ready(Ok(())));
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
|
|
|
|
2020-10-22 20:50:15 -04:00
|
|
|
let specifier = specifier.clone();
|
2024-05-16 03:09:35 -04:00
|
|
|
let graph_container = self.graph_container.clone();
|
2023-04-27 10:05:20 -04:00
|
|
|
let module_load_preparer = self.shared.module_load_preparer.clone();
|
2020-09-19 19:17:35 -04:00
|
|
|
|
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 {
|
2024-05-16 03:09:35 -04:00
|
|
|
let mut update_permit = graph_container.acquire_update_permit().await;
|
|
|
|
let graph = update_permit.graph_mut();
|
2023-04-14 16:22:33 -04:00
|
|
|
module_load_preparer
|
2024-05-16 03:09:35 -04:00
|
|
|
.prepare_module_load(
|
|
|
|
graph,
|
|
|
|
&[specifier],
|
|
|
|
is_dynamic,
|
|
|
|
lib,
|
|
|
|
root_permissions,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
update_permit.commit();
|
|
|
|
Ok(())
|
2020-05-29 10:32:15 -04:00
|
|
|
}
|
|
|
|
.boxed_local()
|
2020-04-30 08:37:06 -04:00
|
|
|
}
|
2024-04-17 10:19:55 -04:00
|
|
|
|
|
|
|
fn code_cache_ready(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
code_cache: &[u8],
|
|
|
|
) -> Pin<Box<dyn Future<Output = ()>>> {
|
|
|
|
if let Some(cache) = self.shared.code_cache.as_ref() {
|
|
|
|
let media_type = MediaType::from_specifier(specifier);
|
|
|
|
let code_hash = self
|
|
|
|
.get_code_hash_or_timestamp(specifier, media_type)
|
|
|
|
.ok()
|
|
|
|
.flatten();
|
|
|
|
if let Some(code_hash) = code_hash {
|
|
|
|
// This log line is also used by tests.
|
|
|
|
log::debug!(
|
|
|
|
"Updating V8 code cache for ES module: {specifier}, [{code_hash:?}]"
|
|
|
|
);
|
|
|
|
cache.set_sync(
|
|
|
|
specifier.as_str(),
|
|
|
|
code_cache::CodeCacheType::EsModule,
|
|
|
|
&code_hash,
|
|
|
|
code_cache,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2024-05-16 03:09:35 -04:00
|
|
|
std::future::ready(()).boxed_local()
|
2024-04-17 10:19:55 -04:00
|
|
|
}
|
2019-06-05 16:35:38 -04:00
|
|
|
}
|
2022-07-19 11:58:18 -04:00
|
|
|
|
2024-05-16 03:09:35 -04:00
|
|
|
impl<TGraphContainer: ModuleGraphContainer> SourceMapGetter
|
|
|
|
for CliModuleLoader<TGraphContainer>
|
|
|
|
{
|
2022-07-19 11:58:18 -04:00
|
|
|
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
|
|
|
}
|
2024-05-16 03:09:35 -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> {
|
2024-05-16 03:09:35 -04:00
|
|
|
let graph = self.graph_container.graph();
|
2023-02-09 22:00:23 -05:00
|
|
|
let code = match graph.get(&resolve_url(file_name).ok()?) {
|
2024-01-31 22:15:22 -05:00
|
|
|
Some(deno_graph::Module::Js(module)) => &module.source,
|
2023-02-22 14:15:25 -05:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-05-16 03:09:35 -04:00
|
|
|
|
|
|
|
/// Holds the `ModuleGraph` in workers.
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct WorkerModuleGraphContainer {
|
|
|
|
// Allow only one request to update the graph data at a time,
|
|
|
|
// but allow other requests to read from it at any time even
|
|
|
|
// while another request is updating the data.
|
|
|
|
update_queue: Rc<deno_core::unsync::TaskQueue>,
|
|
|
|
inner: Rc<RefCell<Arc<ModuleGraph>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WorkerModuleGraphContainer {
|
|
|
|
pub fn new(module_graph: Arc<ModuleGraph>) -> Self {
|
|
|
|
Self {
|
|
|
|
update_queue: Default::default(),
|
|
|
|
inner: Rc::new(RefCell::new(module_graph)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ModuleGraphContainer for WorkerModuleGraphContainer {
|
|
|
|
async fn acquire_update_permit(&self) -> impl ModuleGraphUpdatePermit {
|
|
|
|
let permit = self.update_queue.acquire().await;
|
|
|
|
WorkerModuleGraphUpdatePermit {
|
|
|
|
permit,
|
|
|
|
inner: self.inner.clone(),
|
|
|
|
graph: (**self.inner.borrow()).clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn graph(&self) -> Arc<ModuleGraph> {
|
|
|
|
self.inner.borrow().clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct WorkerModuleGraphUpdatePermit {
|
|
|
|
permit: deno_core::unsync::TaskQueuePermit,
|
|
|
|
inner: Rc<RefCell<Arc<ModuleGraph>>>,
|
|
|
|
graph: ModuleGraph,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ModuleGraphUpdatePermit for WorkerModuleGraphUpdatePermit {
|
|
|
|
fn graph_mut(&mut self) -> &mut ModuleGraph {
|
|
|
|
&mut self.graph
|
|
|
|
}
|
|
|
|
|
|
|
|
fn commit(self) {
|
|
|
|
*self.inner.borrow_mut() = Arc::new(self.graph);
|
|
|
|
drop(self.permit); // explicit drop for clarity
|
|
|
|
}
|
|
|
|
}
|