2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-01-07 21:08:51 -05:00
|
|
|
|
2024-05-26 00:13:20 -04:00
|
|
|
// Allow unused code warnings because we share
|
|
|
|
// code between the two bin targets.
|
|
|
|
#![allow(dead_code)]
|
|
|
|
#![allow(unused_imports)]
|
|
|
|
|
2024-10-24 15:48:48 -04:00
|
|
|
use binary::StandaloneData;
|
|
|
|
use binary::StandaloneModules;
|
2024-11-18 15:09:28 -05:00
|
|
|
use code_cache::DenoCompileCodeCache;
|
2023-05-10 20:06:59 -04:00
|
|
|
use deno_ast::MediaType;
|
2024-09-28 08:50:16 -04:00
|
|
|
use deno_cache_dir::npm::NpmCacheDir;
|
2024-07-03 20:54:33 -04:00
|
|
|
use deno_config::workspace::MappedResolution;
|
|
|
|
use deno_config::workspace::MappedResolutionError;
|
2024-08-07 03:43:05 -04:00
|
|
|
use deno_config::workspace::ResolverWorkspaceJsrPackage;
|
2024-07-03 20:54:33 -04:00
|
|
|
use deno_config::workspace::WorkspaceResolver;
|
2021-11-16 09:02:28 -05:00
|
|
|
use deno_core::anyhow::Context;
|
2023-07-27 12:38:32 -04:00
|
|
|
use deno_core::error::generic_error;
|
2020-11-30 14:35:12 -05:00
|
|
|
use deno_core::error::type_error;
|
|
|
|
use deno_core::error::AnyError;
|
2024-11-18 15:09:28 -05:00
|
|
|
use deno_core::futures::future::LocalBoxFuture;
|
2020-11-30 14:35:12 -05:00
|
|
|
use deno_core::futures::FutureExt;
|
2021-01-04 18:15:52 -05:00
|
|
|
use deno_core::v8_set_flags;
|
2024-11-01 12:27:00 -04:00
|
|
|
use deno_core::FastString;
|
2023-10-12 11:55:50 -04:00
|
|
|
use deno_core::FeatureChecker;
|
2020-11-30 14:35:12 -05:00
|
|
|
use deno_core::ModuleLoader;
|
2024-01-02 20:34:41 -05:00
|
|
|
use deno_core::ModuleSourceCode;
|
2020-11-30 14:35:12 -05:00
|
|
|
use deno_core::ModuleSpecifier;
|
2023-04-04 08:46:31 -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;
|
2024-11-18 15:09:28 -05:00
|
|
|
use deno_core::SourceCodeCacheInfo;
|
2024-07-03 20:54:33 -04:00
|
|
|
use deno_npm::npm_rc::ResolvedNpmRc;
|
2024-07-23 17:34:46 -04:00
|
|
|
use deno_package_json::PackageJsonDepValue;
|
2024-11-14 15:24:25 -05:00
|
|
|
use deno_resolver::npm::NpmReqResolverOptions;
|
2023-05-04 14:28:42 -04:00
|
|
|
use deno_runtime::deno_fs;
|
2024-07-31 19:22:34 -04:00
|
|
|
use deno_runtime::deno_node::create_host_defined_options;
|
2024-11-01 12:27:00 -04:00
|
|
|
use deno_runtime::deno_node::NodeRequireLoader;
|
2023-05-01 08:59:38 -04:00
|
|
|
use deno_runtime::deno_node::NodeResolver;
|
2024-11-01 12:27:00 -04:00
|
|
|
use deno_runtime::deno_node::PackageJsonResolver;
|
2024-06-06 23:37:53 -04:00
|
|
|
use deno_runtime::deno_permissions::Permissions;
|
|
|
|
use deno_runtime::deno_permissions::PermissionsContainer;
|
2023-05-01 16:42:05 -04:00
|
|
|
use deno_runtime::deno_tls::rustls::RootCertStore;
|
|
|
|
use deno_runtime::deno_tls::RootCertStoreProvider;
|
2024-09-16 16:39:37 -04:00
|
|
|
use deno_runtime::deno_web::BlobStore;
|
|
|
|
use deno_runtime::permissions::RuntimePermissionDescriptorParser;
|
2024-04-24 15:45:49 -04:00
|
|
|
use deno_runtime::WorkerExecutionMode;
|
2023-05-30 11:34:50 -04:00
|
|
|
use deno_runtime::WorkerLogLevel;
|
2023-05-10 20:06:59 -04:00
|
|
|
use deno_semver::npm::NpmPackageReqReference;
|
2022-02-24 08:28:00 -05:00
|
|
|
use import_map::parse_from_json;
|
2024-07-25 19:08:14 -04:00
|
|
|
use node_resolver::analyze::NodeCodeTranslator;
|
2024-11-13 10:10:09 -05:00
|
|
|
use node_resolver::errors::ClosestPkgJsonError;
|
|
|
|
use node_resolver::NodeModuleKind;
|
2024-07-25 19:08:14 -04:00
|
|
|
use node_resolver::NodeResolutionMode;
|
2024-11-01 12:27:00 -04:00
|
|
|
use serialization::DenoCompileModuleSource;
|
2024-07-03 20:54:33 -04:00
|
|
|
use std::borrow::Cow;
|
2020-11-30 14:35:12 -05:00
|
|
|
use std::rc::Rc;
|
2020-12-11 12:49:26 -05:00
|
|
|
use std::sync::Arc;
|
2020-11-30 14:35:12 -05:00
|
|
|
|
2024-07-25 19:08:14 -04:00
|
|
|
use crate::args::create_default_npmrc;
|
|
|
|
use crate::args::get_root_cert_store;
|
|
|
|
use crate::args::npm_pkg_req_ref_to_binary_command;
|
|
|
|
use crate::args::CaData;
|
|
|
|
use crate::args::CacheSetting;
|
2024-09-04 10:00:44 -04:00
|
|
|
use crate::args::NpmInstallDepsProvider;
|
2024-07-25 19:08:14 -04:00
|
|
|
use crate::args::StorageKeyResolver;
|
|
|
|
use crate::cache::Caches;
|
2024-10-24 15:48:48 -04:00
|
|
|
use crate::cache::DenoCacheEnvFsAdapter;
|
2024-07-25 19:08:14 -04:00
|
|
|
use crate::cache::DenoDirProvider;
|
2024-11-18 15:09:28 -05:00
|
|
|
use crate::cache::FastInsecureHasher;
|
2024-07-25 19:08:14 -04:00
|
|
|
use crate::cache::NodeAnalysisCache;
|
2024-09-28 08:50:16 -04:00
|
|
|
use crate::cache::RealDenoCacheEnv;
|
2024-07-25 19:08:14 -04:00
|
|
|
use crate::http_util::HttpClientProvider;
|
|
|
|
use crate::node::CliCjsCodeAnalyzer;
|
2024-11-01 12:27:00 -04:00
|
|
|
use crate::node::CliNodeCodeTranslator;
|
2024-07-25 19:08:14 -04:00
|
|
|
use crate::npm::create_cli_npm_resolver;
|
2024-11-01 12:27:00 -04:00
|
|
|
use crate::npm::create_in_npm_pkg_checker;
|
2024-09-30 09:33:32 -04:00
|
|
|
use crate::npm::CliByonmNpmResolverCreateOptions;
|
2024-11-01 12:27:00 -04:00
|
|
|
use crate::npm::CliManagedInNpmPkgCheckerCreateOptions;
|
|
|
|
use crate::npm::CliManagedNpmResolverCreateOptions;
|
|
|
|
use crate::npm::CliNpmResolver;
|
2024-07-25 19:08:14 -04:00
|
|
|
use crate::npm::CliNpmResolverCreateOptions;
|
|
|
|
use crate::npm::CliNpmResolverManagedSnapshotOption;
|
2024-11-01 12:27:00 -04:00
|
|
|
use crate::npm::CreateInNpmPkgCheckerOptions;
|
|
|
|
use crate::resolver::CjsTracker;
|
2024-09-30 09:33:32 -04:00
|
|
|
use crate::resolver::CliDenoResolverFs;
|
2024-11-14 15:24:25 -05:00
|
|
|
use crate::resolver::CliNpmReqResolver;
|
2024-11-13 10:10:09 -05:00
|
|
|
use crate::resolver::IsCjsResolverOptions;
|
2024-07-25 19:08:14 -04:00
|
|
|
use crate::resolver::NpmModuleLoader;
|
|
|
|
use crate::util::progress_bar::ProgressBar;
|
|
|
|
use crate::util::progress_bar::ProgressBarStyle;
|
|
|
|
use crate::util::v8::construct_v8_flags;
|
2024-11-18 15:09:28 -05:00
|
|
|
use crate::worker::CliCodeCache;
|
2024-07-25 19:08:14 -04:00
|
|
|
use crate::worker::CliMainWorkerFactory;
|
|
|
|
use crate::worker::CliMainWorkerOptions;
|
2024-11-01 12:27:00 -04:00
|
|
|
use crate::worker::CreateModuleLoaderResult;
|
2024-07-25 19:08:14 -04:00
|
|
|
use crate::worker::ModuleLoaderFactory;
|
|
|
|
|
2024-02-13 11:22:30 -05:00
|
|
|
pub mod binary;
|
2024-11-18 15:09:28 -05:00
|
|
|
mod code_cache;
|
2023-05-10 20:06:59 -04:00
|
|
|
mod file_system;
|
2024-10-24 15:48:48 -04:00
|
|
|
mod serialization;
|
2023-05-10 20:06:59 -04:00
|
|
|
mod virtual_fs;
|
2022-02-15 07:33:46 -05:00
|
|
|
|
2023-04-19 17:50:56 -04:00
|
|
|
pub use binary::extract_standalone;
|
|
|
|
pub use binary::is_standalone_binary;
|
|
|
|
pub use binary::DenoCompileBinaryWriter;
|
2022-02-15 07:33:46 -05:00
|
|
|
|
2023-04-19 17:50:56 -04:00
|
|
|
use self::binary::Metadata;
|
2023-05-10 20:06:59 -04:00
|
|
|
use self::file_system::DenoCompileFileSystem;
|
|
|
|
|
2024-07-03 20:54:33 -04:00
|
|
|
struct SharedModuleLoaderState {
|
2024-11-01 12:27:00 -04:00
|
|
|
cjs_tracker: Arc<CjsTracker>,
|
|
|
|
fs: Arc<dyn deno_fs::FileSystem>,
|
2024-10-24 15:48:48 -04:00
|
|
|
modules: StandaloneModules,
|
2024-11-01 12:27:00 -04:00
|
|
|
node_code_translator: Arc<CliNodeCodeTranslator>,
|
2024-11-14 15:24:25 -05:00
|
|
|
node_resolver: Arc<NodeResolver>,
|
2023-05-10 20:06:59 -04:00
|
|
|
npm_module_loader: Arc<NpmModuleLoader>,
|
2024-11-14 15:24:25 -05:00
|
|
|
npm_req_resolver: Arc<CliNpmReqResolver>,
|
2024-11-01 12:27:00 -04:00
|
|
|
npm_resolver: Arc<dyn CliNpmResolver>,
|
|
|
|
workspace_resolver: WorkspaceResolver,
|
2024-11-18 15:09:28 -05:00
|
|
|
code_cache: Option<Arc<dyn CliCodeCache>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SharedModuleLoaderState {
|
|
|
|
fn get_code_cache(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
source: &[u8],
|
|
|
|
) -> Option<SourceCodeCacheInfo> {
|
|
|
|
let Some(code_cache) = &self.code_cache else {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
if !code_cache.enabled() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
// deno version is already included in the root cache key
|
|
|
|
let hash = FastInsecureHasher::new_without_deno_version()
|
|
|
|
.write_hashable(source)
|
|
|
|
.finish();
|
|
|
|
let data = code_cache.get_sync(
|
|
|
|
specifier,
|
|
|
|
deno_runtime::code_cache::CodeCacheType::EsModule,
|
|
|
|
hash,
|
|
|
|
);
|
|
|
|
Some(SourceCodeCacheInfo {
|
|
|
|
hash,
|
|
|
|
data: data.map(Cow::Owned),
|
|
|
|
})
|
|
|
|
}
|
2023-05-10 20:06:59 -04:00
|
|
|
}
|
2021-01-04 18:15:52 -05:00
|
|
|
|
2023-03-19 18:32:54 -04:00
|
|
|
#[derive(Clone)]
|
2022-02-24 08:28:00 -05:00
|
|
|
struct EmbeddedModuleLoader {
|
2023-05-10 20:06:59 -04:00
|
|
|
shared: Arc<SharedModuleLoaderState>,
|
2022-02-24 08:28:00 -05:00
|
|
|
}
|
2020-11-30 14:35:12 -05:00
|
|
|
|
2024-11-01 12:27:00 -04:00
|
|
|
impl std::fmt::Debug for EmbeddedModuleLoader {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
f.debug_struct("EmbeddedModuleLoader").finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-06 11:05:30 -04:00
|
|
|
pub const MODULE_NOT_FOUND: &str = "Module not found";
|
2024-08-27 05:27:10 -04:00
|
|
|
pub const UNSUPPORTED_SCHEME: &str = "Unsupported scheme";
|
2024-08-06 11:05:30 -04:00
|
|
|
|
2020-11-30 14:35:12 -05:00
|
|
|
impl ModuleLoader for EmbeddedModuleLoader {
|
|
|
|
fn resolve(
|
|
|
|
&self,
|
2024-09-04 10:00:44 -04:00
|
|
|
raw_specifier: &str,
|
2022-02-24 08:28:00 -05:00
|
|
|
referrer: &str,
|
2023-05-10 20:06:59 -04:00
|
|
|
kind: ResolutionKind,
|
2020-11-30 14:35:12 -05:00
|
|
|
) -> Result<ModuleSpecifier, AnyError> {
|
2023-07-27 12:38:32 -04:00
|
|
|
let referrer = if referrer == "." {
|
|
|
|
if kind != ResolutionKind::MainModule {
|
|
|
|
return Err(generic_error(format!(
|
|
|
|
"Expected to resolve main module, got {:?} instead.",
|
|
|
|
kind
|
|
|
|
)));
|
2022-03-31 04:41:30 -04:00
|
|
|
}
|
2023-07-27 12:38:32 -04:00
|
|
|
let current_dir = std::env::current_dir().unwrap();
|
|
|
|
deno_core::resolve_path(".", ¤t_dir)?
|
|
|
|
} else {
|
|
|
|
ModuleSpecifier::parse(referrer).map_err(|err| {
|
|
|
|
type_error(format!("Referrer uses invalid specifier: {}", err))
|
|
|
|
})?
|
2022-03-31 04:41:30 -04:00
|
|
|
};
|
2024-11-13 10:10:09 -05:00
|
|
|
let referrer_kind = if self
|
|
|
|
.shared
|
|
|
|
.cjs_tracker
|
|
|
|
.is_maybe_cjs(&referrer, MediaType::from_specifier(&referrer))?
|
|
|
|
{
|
|
|
|
NodeModuleKind::Cjs
|
|
|
|
} else {
|
|
|
|
NodeModuleKind::Esm
|
|
|
|
};
|
2022-03-31 04:41:30 -04:00
|
|
|
|
2024-07-23 20:22:24 -04:00
|
|
|
if self.shared.node_resolver.in_npm_package(&referrer) {
|
|
|
|
return Ok(
|
|
|
|
self
|
|
|
|
.shared
|
|
|
|
.node_resolver
|
2024-11-13 10:10:09 -05:00
|
|
|
.resolve(
|
|
|
|
raw_specifier,
|
|
|
|
&referrer,
|
|
|
|
referrer_kind,
|
|
|
|
NodeResolutionMode::Execution,
|
|
|
|
)?
|
2024-07-23 20:22:24 -04:00
|
|
|
.into_url(),
|
|
|
|
);
|
2023-05-10 20:06:59 -04:00
|
|
|
}
|
|
|
|
|
2024-09-04 10:00:44 -04:00
|
|
|
let mapped_resolution = self
|
|
|
|
.shared
|
|
|
|
.workspace_resolver
|
|
|
|
.resolve(raw_specifier, &referrer);
|
2023-05-10 20:06:59 -04:00
|
|
|
|
2024-07-03 20:54:33 -04:00
|
|
|
match mapped_resolution {
|
2024-08-07 03:43:05 -04:00
|
|
|
Ok(MappedResolution::WorkspaceJsrPackage { specifier, .. }) => {
|
|
|
|
Ok(specifier)
|
|
|
|
}
|
2024-07-17 09:13:22 -04:00
|
|
|
Ok(MappedResolution::WorkspaceNpmPackage {
|
|
|
|
target_pkg_json: pkg_json,
|
|
|
|
sub_path,
|
|
|
|
..
|
|
|
|
}) => Ok(
|
|
|
|
self
|
|
|
|
.shared
|
|
|
|
.node_resolver
|
2024-11-14 15:24:25 -05:00
|
|
|
.resolve_package_subpath_from_deno_module(
|
2024-07-17 09:13:22 -04:00
|
|
|
pkg_json.dir_path(),
|
|
|
|
sub_path.as_deref(),
|
|
|
|
Some(&referrer),
|
2024-11-13 10:10:09 -05:00
|
|
|
referrer_kind,
|
2024-07-17 09:13:22 -04:00
|
|
|
NodeResolutionMode::Execution,
|
2024-11-01 12:27:00 -04:00
|
|
|
)?,
|
2024-07-17 09:13:22 -04:00
|
|
|
),
|
2024-07-03 20:54:33 -04:00
|
|
|
Ok(MappedResolution::PackageJson {
|
|
|
|
dep_result,
|
|
|
|
sub_path,
|
|
|
|
alias,
|
|
|
|
..
|
|
|
|
}) => match dep_result.as_ref().map_err(|e| AnyError::from(e.clone()))? {
|
2024-11-14 15:24:25 -05:00
|
|
|
PackageJsonDepValue::Req(req) => self
|
|
|
|
.shared
|
|
|
|
.npm_req_resolver
|
|
|
|
.resolve_req_with_sub_path(
|
2024-07-03 20:54:33 -04:00
|
|
|
req,
|
|
|
|
sub_path.as_deref(),
|
|
|
|
&referrer,
|
2024-11-13 10:10:09 -05:00
|
|
|
referrer_kind,
|
2024-07-03 20:54:33 -04:00
|
|
|
NodeResolutionMode::Execution,
|
|
|
|
)
|
2024-11-14 15:24:25 -05:00
|
|
|
.map_err(AnyError::from),
|
2024-07-03 20:54:33 -04:00
|
|
|
PackageJsonDepValue::Workspace(version_req) => {
|
|
|
|
let pkg_folder = self
|
|
|
|
.shared
|
|
|
|
.workspace_resolver
|
|
|
|
.resolve_workspace_pkg_json_folder_for_pkg_json_dep(
|
|
|
|
alias,
|
|
|
|
version_req,
|
|
|
|
)?;
|
|
|
|
Ok(
|
|
|
|
self
|
|
|
|
.shared
|
|
|
|
.node_resolver
|
2024-11-14 15:24:25 -05:00
|
|
|
.resolve_package_subpath_from_deno_module(
|
2024-07-03 20:54:33 -04:00
|
|
|
pkg_folder,
|
|
|
|
sub_path.as_deref(),
|
2024-07-09 12:15:03 -04:00
|
|
|
Some(&referrer),
|
2024-11-13 10:10:09 -05:00
|
|
|
referrer_kind,
|
2024-07-03 20:54:33 -04:00
|
|
|
NodeResolutionMode::Execution,
|
2024-11-01 12:27:00 -04:00
|
|
|
)?,
|
2024-07-03 20:54:33 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
},
|
2024-08-20 10:11:43 -04:00
|
|
|
Ok(MappedResolution::Normal { specifier, .. })
|
|
|
|
| Ok(MappedResolution::ImportMap { specifier, .. }) => {
|
2024-07-03 20:54:33 -04:00
|
|
|
if let Ok(reference) =
|
|
|
|
NpmPackageReqReference::from_specifier(&specifier)
|
|
|
|
{
|
2024-11-14 15:24:25 -05:00
|
|
|
return Ok(self.shared.npm_req_resolver.resolve_req_reference(
|
2024-11-01 12:27:00 -04:00
|
|
|
&reference,
|
|
|
|
&referrer,
|
2024-11-13 10:10:09 -05:00
|
|
|
referrer_kind,
|
2024-11-01 12:27:00 -04:00
|
|
|
NodeResolutionMode::Execution,
|
2024-11-14 15:24:25 -05:00
|
|
|
)?);
|
2024-07-03 20:54:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if specifier.scheme() == "jsr" {
|
2024-10-24 15:48:48 -04:00
|
|
|
if let Some(specifier) =
|
|
|
|
self.shared.modules.resolve_specifier(&specifier)?
|
|
|
|
{
|
|
|
|
return Ok(specifier.clone());
|
2024-07-03 20:54:33 -04:00
|
|
|
}
|
|
|
|
}
|
2024-04-27 17:11:57 -04:00
|
|
|
|
2024-07-25 09:07:59 -04:00
|
|
|
Ok(
|
|
|
|
self
|
|
|
|
.shared
|
|
|
|
.node_resolver
|
2024-11-14 15:24:25 -05:00
|
|
|
.handle_if_in_node_modules(&specifier)
|
2024-07-25 09:07:59 -04:00
|
|
|
.unwrap_or(specifier),
|
|
|
|
)
|
2024-04-27 17:11:57 -04:00
|
|
|
}
|
2024-07-03 20:54:33 -04:00
|
|
|
Err(err)
|
|
|
|
if err.is_unmapped_bare_specifier() && referrer.scheme() == "file" =>
|
|
|
|
{
|
2024-11-14 15:24:25 -05:00
|
|
|
let maybe_res = self.shared.npm_req_resolver.resolve_if_for_npm_pkg(
|
2024-09-04 10:00:44 -04:00
|
|
|
raw_specifier,
|
2024-07-03 20:54:33 -04:00
|
|
|
&referrer,
|
2024-11-13 10:10:09 -05:00
|
|
|
referrer_kind,
|
2024-07-03 20:54:33 -04:00
|
|
|
NodeResolutionMode::Execution,
|
2024-07-23 20:22:24 -04:00
|
|
|
)?;
|
|
|
|
if let Some(res) = maybe_res {
|
2024-07-03 20:54:33 -04:00
|
|
|
return Ok(res.into_url());
|
|
|
|
}
|
|
|
|
Err(err.into())
|
|
|
|
}
|
|
|
|
Err(err) => Err(err.into()),
|
2024-04-27 17:11:57 -04:00
|
|
|
}
|
2020-11-30 14:35:12 -05:00
|
|
|
}
|
|
|
|
|
2024-07-31 19:22:34 -04:00
|
|
|
fn get_host_defined_options<'s>(
|
|
|
|
&self,
|
|
|
|
scope: &mut deno_core::v8::HandleScope<'s>,
|
|
|
|
name: &str,
|
|
|
|
) -> Option<deno_core::v8::Local<'s, deno_core::v8::Data>> {
|
|
|
|
let name = deno_core::ModuleSpecifier::parse(name).ok()?;
|
|
|
|
if self.shared.node_resolver.in_npm_package(&name) {
|
|
|
|
Some(create_host_defined_options(scope))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-30 14:35:12 -05:00
|
|
|
fn load(
|
|
|
|
&self,
|
2023-07-27 12:38:32 -04:00
|
|
|
original_specifier: &ModuleSpecifier,
|
2023-05-10 20:06:59 -04:00
|
|
|
maybe_referrer: Option<&ModuleSpecifier>,
|
2024-06-06 23:37:53 -04: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 {
|
2024-01-31 22:15:22 -05:00
|
|
|
if original_specifier.scheme() == "data" {
|
|
|
|
let data_url_text =
|
|
|
|
match deno_graph::source::RawDataUrl::parse(original_specifier)
|
2024-05-28 14:58:43 -04:00
|
|
|
.and_then(|url| url.decode())
|
2024-01-31 22:15:22 -05:00
|
|
|
{
|
|
|
|
Ok(response) => response,
|
|
|
|
Err(err) => {
|
|
|
|
return deno_core::ModuleLoadResponse::Sync(Err(type_error(
|
|
|
|
format!("{:#}", err),
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
};
|
2024-01-27 01:57:41 -05:00
|
|
|
return deno_core::ModuleLoadResponse::Sync(Ok(
|
2023-07-27 12:38:32 -04:00
|
|
|
deno_core::ModuleSource::new(
|
|
|
|
deno_core::ModuleType::JavaScript,
|
2024-01-31 22:15:22 -05:00
|
|
|
ModuleSourceCode::String(data_url_text.into()),
|
2023-07-27 12:38:32 -04:00
|
|
|
original_specifier,
|
2024-03-21 16:57:32 -04:00
|
|
|
None,
|
2023-07-27 12:38:32 -04:00
|
|
|
),
|
2024-01-27 01:57:41 -05:00
|
|
|
));
|
2023-07-27 12:38:32 -04:00
|
|
|
}
|
|
|
|
|
2024-05-18 11:42:03 -04:00
|
|
|
if self.shared.node_resolver.in_npm_package(original_specifier) {
|
2024-11-18 15:09:28 -05:00
|
|
|
let shared = self.shared.clone();
|
2024-05-18 11:42:03 -04:00
|
|
|
let original_specifier = original_specifier.clone();
|
|
|
|
let maybe_referrer = maybe_referrer.cloned();
|
|
|
|
return deno_core::ModuleLoadResponse::Async(
|
|
|
|
async move {
|
2024-11-18 15:09:28 -05:00
|
|
|
let code_source = shared
|
|
|
|
.npm_module_loader
|
2024-06-06 23:37:53 -04:00
|
|
|
.load(&original_specifier, maybe_referrer.as_ref())
|
2024-05-18 11:42:03 -04:00
|
|
|
.await?;
|
2024-11-18 15:09:28 -05:00
|
|
|
let code_cache_entry = shared.get_code_cache(
|
|
|
|
&code_source.found_url,
|
|
|
|
code_source.code.as_bytes(),
|
|
|
|
);
|
2024-05-18 11:42:03 -04:00
|
|
|
Ok(deno_core::ModuleSource::new_with_redirect(
|
2023-05-10 20:06:59 -04:00
|
|
|
match code_source.media_type {
|
|
|
|
MediaType::Json => ModuleType::Json,
|
|
|
|
_ => ModuleType::JavaScript,
|
|
|
|
},
|
2024-06-05 11:04:16 -04:00
|
|
|
code_source.code,
|
2024-05-18 11:42:03 -04:00
|
|
|
&original_specifier,
|
2023-05-10 20:06:59 -04:00
|
|
|
&code_source.found_url,
|
2024-11-18 15:09:28 -05:00
|
|
|
code_cache_entry,
|
2024-05-18 11:42:03 -04:00
|
|
|
))
|
|
|
|
}
|
|
|
|
.boxed_local(),
|
|
|
|
);
|
2023-05-10 20:06:59 -04:00
|
|
|
}
|
|
|
|
|
2024-10-24 15:48:48 -04:00
|
|
|
match self.shared.modules.read(original_specifier) {
|
|
|
|
Ok(Some(module)) => {
|
2024-11-01 12:27:00 -04:00
|
|
|
let media_type = module.media_type;
|
2024-10-24 15:48:48 -04:00
|
|
|
let (module_specifier, module_type, module_source) =
|
2024-11-01 12:27:00 -04:00
|
|
|
module.into_parts();
|
|
|
|
let is_maybe_cjs = match self
|
|
|
|
.shared
|
|
|
|
.cjs_tracker
|
|
|
|
.is_maybe_cjs(original_specifier, media_type)
|
|
|
|
{
|
|
|
|
Ok(is_maybe_cjs) => is_maybe_cjs,
|
|
|
|
Err(err) => {
|
|
|
|
return deno_core::ModuleLoadResponse::Sync(Err(type_error(
|
|
|
|
format!("{:?}", err),
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if is_maybe_cjs {
|
|
|
|
let original_specifier = original_specifier.clone();
|
|
|
|
let module_specifier = module_specifier.clone();
|
|
|
|
let shared = self.shared.clone();
|
|
|
|
deno_core::ModuleLoadResponse::Async(
|
|
|
|
async move {
|
|
|
|
let source = match module_source {
|
|
|
|
DenoCompileModuleSource::String(string) => {
|
|
|
|
Cow::Borrowed(string)
|
|
|
|
}
|
|
|
|
DenoCompileModuleSource::Bytes(module_code_bytes) => {
|
|
|
|
match module_code_bytes {
|
|
|
|
Cow::Owned(bytes) => Cow::Owned(
|
|
|
|
crate::util::text_encoding::from_utf8_lossy_owned(bytes),
|
|
|
|
),
|
|
|
|
Cow::Borrowed(bytes) => String::from_utf8_lossy(bytes),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let source = shared
|
|
|
|
.node_code_translator
|
|
|
|
.translate_cjs_to_esm(&module_specifier, Some(source))
|
|
|
|
.await?;
|
|
|
|
let module_source = match source {
|
|
|
|
Cow::Owned(source) => ModuleSourceCode::String(source.into()),
|
|
|
|
Cow::Borrowed(source) => {
|
|
|
|
ModuleSourceCode::String(FastString::from_static(source))
|
|
|
|
}
|
|
|
|
};
|
2024-11-18 15:09:28 -05:00
|
|
|
let code_cache_entry = shared
|
|
|
|
.get_code_cache(&module_specifier, module_source.as_bytes());
|
2024-11-01 12:27:00 -04:00
|
|
|
Ok(deno_core::ModuleSource::new_with_redirect(
|
|
|
|
module_type,
|
|
|
|
module_source,
|
|
|
|
&original_specifier,
|
|
|
|
&module_specifier,
|
2024-11-18 15:09:28 -05:00
|
|
|
code_cache_entry,
|
2024-11-01 12:27:00 -04:00
|
|
|
))
|
|
|
|
}
|
|
|
|
.boxed_local(),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
let module_source = module_source.into_for_v8();
|
2024-11-18 15:09:28 -05:00
|
|
|
let code_cache_entry = self
|
|
|
|
.shared
|
|
|
|
.get_code_cache(module_specifier, module_source.as_bytes());
|
2024-11-01 12:27:00 -04:00
|
|
|
deno_core::ModuleLoadResponse::Sync(Ok(
|
|
|
|
deno_core::ModuleSource::new_with_redirect(
|
|
|
|
module_type,
|
|
|
|
module_source,
|
|
|
|
original_specifier,
|
|
|
|
module_specifier,
|
2024-11-18 15:09:28 -05:00
|
|
|
code_cache_entry,
|
2024-11-01 12:27:00 -04:00
|
|
|
),
|
|
|
|
))
|
|
|
|
}
|
2024-01-27 01:57:41 -05:00
|
|
|
}
|
2024-10-24 15:48:48 -04:00
|
|
|
Ok(None) => deno_core::ModuleLoadResponse::Sync(Err(type_error(
|
|
|
|
format!("{MODULE_NOT_FOUND}: {}", original_specifier),
|
|
|
|
))),
|
|
|
|
Err(err) => deno_core::ModuleLoadResponse::Sync(Err(type_error(
|
|
|
|
format!("{:?}", err),
|
|
|
|
))),
|
|
|
|
}
|
2020-11-30 14:35:12 -05:00
|
|
|
}
|
2024-11-18 15:09:28 -05:00
|
|
|
|
|
|
|
fn code_cache_ready(
|
|
|
|
&self,
|
|
|
|
specifier: ModuleSpecifier,
|
|
|
|
source_hash: u64,
|
|
|
|
code_cache_data: &[u8],
|
|
|
|
) -> LocalBoxFuture<'static, ()> {
|
|
|
|
if let Some(code_cache) = &self.shared.code_cache {
|
|
|
|
code_cache.set_sync(
|
|
|
|
specifier,
|
|
|
|
deno_runtime::code_cache::CodeCacheType::EsModule,
|
|
|
|
source_hash,
|
|
|
|
code_cache_data,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
std::future::ready(()).boxed_local()
|
|
|
|
}
|
2020-11-30 14:35:12 -05:00
|
|
|
}
|
|
|
|
|
2024-11-01 12:27:00 -04:00
|
|
|
impl NodeRequireLoader for EmbeddedModuleLoader {
|
|
|
|
fn ensure_read_permission<'a>(
|
|
|
|
&self,
|
|
|
|
permissions: &mut dyn deno_runtime::deno_node::NodePermissions,
|
|
|
|
path: &'a std::path::Path,
|
|
|
|
) -> Result<Cow<'a, std::path::Path>, AnyError> {
|
|
|
|
if self.shared.modules.has_file(path) {
|
|
|
|
// allow reading if the file is in the snapshot
|
|
|
|
return Ok(Cow::Borrowed(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
self
|
|
|
|
.shared
|
|
|
|
.npm_resolver
|
|
|
|
.ensure_read_permission(permissions, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn load_text_file_lossy(
|
|
|
|
&self,
|
|
|
|
path: &std::path::Path,
|
|
|
|
) -> Result<String, AnyError> {
|
|
|
|
Ok(self.shared.fs.read_text_file_lossy_sync(path, None)?)
|
|
|
|
}
|
2024-11-13 10:10:09 -05:00
|
|
|
|
|
|
|
fn is_maybe_cjs(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<bool, ClosestPkgJsonError> {
|
|
|
|
let media_type = MediaType::from_specifier(specifier);
|
|
|
|
self.shared.cjs_tracker.is_maybe_cjs(specifier, media_type)
|
|
|
|
}
|
2024-11-01 12:27:00 -04:00
|
|
|
}
|
|
|
|
|
2023-05-01 08:59:38 -04:00
|
|
|
struct StandaloneModuleLoaderFactory {
|
2023-05-10 20:06:59 -04:00
|
|
|
shared: Arc<SharedModuleLoaderState>,
|
2023-03-19 18:32:54 -04:00
|
|
|
}
|
|
|
|
|
2024-11-01 12:27:00 -04:00
|
|
|
impl StandaloneModuleLoaderFactory {
|
|
|
|
pub fn create_result(&self) -> CreateModuleLoaderResult {
|
|
|
|
let loader = Rc::new(EmbeddedModuleLoader {
|
|
|
|
shared: self.shared.clone(),
|
|
|
|
});
|
|
|
|
CreateModuleLoaderResult {
|
|
|
|
module_loader: loader.clone(),
|
|
|
|
node_require_loader: loader,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-01 08:59:38 -04:00
|
|
|
impl ModuleLoaderFactory for StandaloneModuleLoaderFactory {
|
|
|
|
fn create_for_main(
|
|
|
|
&self,
|
2024-09-25 21:50:54 -04:00
|
|
|
_root_permissions: PermissionsContainer,
|
2024-11-01 12:27:00 -04:00
|
|
|
) -> CreateModuleLoaderResult {
|
|
|
|
self.create_result()
|
2023-05-01 08:59:38 -04:00
|
|
|
}
|
2023-04-26 13:07:15 -04:00
|
|
|
|
2023-05-01 08:59:38 -04:00
|
|
|
fn create_for_worker(
|
|
|
|
&self,
|
2024-09-25 21:50:54 -04:00
|
|
|
_parent_permissions: PermissionsContainer,
|
|
|
|
_permissions: PermissionsContainer,
|
2024-11-01 12:27:00 -04:00
|
|
|
) -> CreateModuleLoaderResult {
|
|
|
|
self.create_result()
|
2023-05-01 08:59:38 -04:00
|
|
|
}
|
|
|
|
}
|
2023-03-19 18:32:54 -04:00
|
|
|
|
2023-05-01 16:42:05 -04:00
|
|
|
struct StandaloneRootCertStoreProvider {
|
|
|
|
ca_stores: Option<Vec<String>>,
|
|
|
|
ca_data: Option<CaData>,
|
|
|
|
cell: once_cell::sync::OnceCell<RootCertStore>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RootCertStoreProvider for StandaloneRootCertStoreProvider {
|
|
|
|
fn get_or_try_init(&self) -> Result<&RootCertStore, AnyError> {
|
|
|
|
self.cell.get_or_try_init(|| {
|
|
|
|
get_root_cert_store(None, self.ca_stores.clone(), self.ca_data.clone())
|
|
|
|
.map_err(|err| err.into())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-24 15:48:48 -04:00
|
|
|
pub async fn run(data: StandaloneData) -> Result<i32, AnyError> {
|
|
|
|
let StandaloneData {
|
|
|
|
fs,
|
|
|
|
metadata,
|
|
|
|
modules,
|
|
|
|
npm_snapshot,
|
|
|
|
root_path,
|
|
|
|
vfs,
|
|
|
|
} = data;
|
2023-05-25 14:27:45 -04:00
|
|
|
let deno_dir_provider = Arc::new(DenoDirProvider::new(None));
|
2023-05-01 16:42:05 -04:00
|
|
|
let root_cert_store_provider = Arc::new(StandaloneRootCertStoreProvider {
|
|
|
|
ca_stores: metadata.ca_stores,
|
|
|
|
ca_data: metadata.ca_data.map(CaData::Bytes),
|
|
|
|
cell: Default::default(),
|
|
|
|
});
|
2023-04-26 13:07:15 -04:00
|
|
|
let progress_bar = ProgressBar::new(ProgressBarStyle::TextOnly);
|
2024-06-03 17:17:08 -04:00
|
|
|
let http_client_provider = Arc::new(HttpClientProvider::new(
|
2023-05-01 16:42:05 -04:00
|
|
|
Some(root_cert_store_provider.clone()),
|
2023-04-26 13:07:15 -04:00
|
|
|
metadata.unsafely_ignore_certificate_errors.clone(),
|
2023-05-01 16:42:05 -04:00
|
|
|
));
|
2023-05-10 20:06:59 -04:00
|
|
|
// use a dummy npm registry url
|
|
|
|
let npm_registry_url = ModuleSpecifier::parse("https://localhost/").unwrap();
|
2024-07-17 09:13:22 -04:00
|
|
|
let root_dir_url =
|
|
|
|
Arc::new(ModuleSpecifier::from_directory_path(&root_path).unwrap());
|
2024-07-03 20:54:33 -04:00
|
|
|
let main_module = root_dir_url.join(&metadata.entrypoint_key).unwrap();
|
2024-10-24 15:48:48 -04:00
|
|
|
let npm_global_cache_dir = root_path.join(".deno_compile_node_modules");
|
2023-11-29 09:32:23 -05:00
|
|
|
let cache_setting = CacheSetting::Only;
|
2024-11-01 12:27:00 -04:00
|
|
|
let pkg_json_resolver = Arc::new(PackageJsonResolver::new(
|
|
|
|
deno_runtime::deno_node::DenoFsNodeResolverEnv::new(fs.clone()),
|
|
|
|
));
|
|
|
|
let (in_npm_pkg_checker, npm_resolver) = match metadata.node_modules {
|
2024-07-03 20:54:33 -04:00
|
|
|
Some(binary::NodeModules::Managed { node_modules_dir }) => {
|
2024-11-01 12:27:00 -04:00
|
|
|
// create an npmrc that uses the fake npm_registry_url to resolve packages
|
|
|
|
let npmrc = Arc::new(ResolvedNpmRc {
|
|
|
|
default_config: deno_npm::npm_rc::RegistryConfigWithUrl {
|
|
|
|
registry_url: npm_registry_url.clone(),
|
|
|
|
config: Default::default(),
|
|
|
|
},
|
|
|
|
scopes: Default::default(),
|
|
|
|
registry_configs: Default::default(),
|
|
|
|
});
|
|
|
|
let npm_cache_dir = Arc::new(NpmCacheDir::new(
|
|
|
|
&DenoCacheEnvFsAdapter(fs.as_ref()),
|
|
|
|
npm_global_cache_dir,
|
|
|
|
npmrc.get_all_known_registries_urls(),
|
|
|
|
));
|
2024-10-24 15:48:48 -04:00
|
|
|
let snapshot = npm_snapshot.unwrap();
|
2024-07-03 20:54:33 -04:00
|
|
|
let maybe_node_modules_path = node_modules_dir
|
2024-10-24 15:48:48 -04:00
|
|
|
.map(|node_modules_dir| root_path.join(node_modules_dir));
|
2024-11-01 12:27:00 -04:00
|
|
|
let in_npm_pkg_checker =
|
|
|
|
create_in_npm_pkg_checker(CreateInNpmPkgCheckerOptions::Managed(
|
|
|
|
CliManagedInNpmPkgCheckerCreateOptions {
|
|
|
|
root_cache_dir_url: npm_cache_dir.root_dir_url(),
|
|
|
|
maybe_node_modules_path: maybe_node_modules_path.as_deref(),
|
|
|
|
},
|
|
|
|
));
|
|
|
|
let npm_resolver =
|
|
|
|
create_cli_npm_resolver(CliNpmResolverCreateOptions::Managed(
|
|
|
|
CliManagedNpmResolverCreateOptions {
|
|
|
|
snapshot: CliNpmResolverManagedSnapshotOption::Specified(Some(
|
|
|
|
snapshot,
|
|
|
|
)),
|
|
|
|
maybe_lockfile: None,
|
|
|
|
fs: fs.clone(),
|
|
|
|
http_client_provider: http_client_provider.clone(),
|
|
|
|
npm_cache_dir,
|
|
|
|
cache_setting,
|
|
|
|
text_only_progress_bar: progress_bar,
|
|
|
|
maybe_node_modules_path,
|
|
|
|
npm_system_info: Default::default(),
|
|
|
|
npm_install_deps_provider: Arc::new(
|
|
|
|
// this is only used for installing packages, which isn't necessary with deno compile
|
|
|
|
NpmInstallDepsProvider::empty(),
|
|
|
|
),
|
|
|
|
npmrc,
|
|
|
|
lifecycle_scripts: Default::default(),
|
|
|
|
},
|
|
|
|
))
|
|
|
|
.await?;
|
|
|
|
(in_npm_pkg_checker, npm_resolver)
|
2024-07-03 20:54:33 -04:00
|
|
|
}
|
|
|
|
Some(binary::NodeModules::Byonm {
|
|
|
|
root_node_modules_dir,
|
|
|
|
}) => {
|
2024-07-10 14:46:25 -04:00
|
|
|
let root_node_modules_dir =
|
|
|
|
root_node_modules_dir.map(|p| vfs.root().join(p));
|
2024-11-01 12:27:00 -04:00
|
|
|
let in_npm_pkg_checker =
|
|
|
|
create_in_npm_pkg_checker(CreateInNpmPkgCheckerOptions::Byonm);
|
|
|
|
let npm_resolver = create_cli_npm_resolver(
|
|
|
|
CliNpmResolverCreateOptions::Byonm(CliByonmNpmResolverCreateOptions {
|
2024-09-30 09:33:32 -04:00
|
|
|
fs: CliDenoResolverFs(fs.clone()),
|
2024-11-01 12:27:00 -04:00
|
|
|
pkg_json_resolver: pkg_json_resolver.clone(),
|
2024-07-03 20:54:33 -04:00
|
|
|
root_node_modules_dir,
|
2024-11-01 12:27:00 -04:00
|
|
|
}),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
(in_npm_pkg_checker, npm_resolver)
|
2024-07-03 20:54:33 -04:00
|
|
|
}
|
|
|
|
None => {
|
2024-11-01 12:27:00 -04:00
|
|
|
// Packages from different registries are already inlined in the binary,
|
|
|
|
// so no need to create actual `.npmrc` configuration.
|
|
|
|
let npmrc = create_default_npmrc();
|
|
|
|
let npm_cache_dir = Arc::new(NpmCacheDir::new(
|
|
|
|
&DenoCacheEnvFsAdapter(fs.as_ref()),
|
|
|
|
npm_global_cache_dir,
|
|
|
|
npmrc.get_all_known_registries_urls(),
|
|
|
|
));
|
|
|
|
let in_npm_pkg_checker =
|
|
|
|
create_in_npm_pkg_checker(CreateInNpmPkgCheckerOptions::Managed(
|
|
|
|
CliManagedInNpmPkgCheckerCreateOptions {
|
|
|
|
root_cache_dir_url: npm_cache_dir.root_dir_url(),
|
|
|
|
maybe_node_modules_path: None,
|
|
|
|
},
|
|
|
|
));
|
|
|
|
let npm_resolver =
|
|
|
|
create_cli_npm_resolver(CliNpmResolverCreateOptions::Managed(
|
|
|
|
CliManagedNpmResolverCreateOptions {
|
|
|
|
snapshot: CliNpmResolverManagedSnapshotOption::Specified(None),
|
|
|
|
maybe_lockfile: None,
|
|
|
|
fs: fs.clone(),
|
|
|
|
http_client_provider: http_client_provider.clone(),
|
|
|
|
npm_cache_dir,
|
|
|
|
cache_setting,
|
|
|
|
text_only_progress_bar: progress_bar,
|
|
|
|
maybe_node_modules_path: None,
|
|
|
|
npm_system_info: Default::default(),
|
|
|
|
npm_install_deps_provider: Arc::new(
|
|
|
|
// this is only used for installing packages, which isn't necessary with deno compile
|
|
|
|
NpmInstallDepsProvider::empty(),
|
|
|
|
),
|
|
|
|
npmrc: create_default_npmrc(),
|
|
|
|
lifecycle_scripts: Default::default(),
|
|
|
|
},
|
|
|
|
))
|
|
|
|
.await?;
|
|
|
|
(in_npm_pkg_checker, npm_resolver)
|
2024-07-03 20:54:33 -04:00
|
|
|
}
|
|
|
|
};
|
2023-06-08 11:48:29 -04:00
|
|
|
|
2023-11-29 09:32:23 -05:00
|
|
|
let has_node_modules_dir = npm_resolver.root_node_modules_path().is_some();
|
2023-09-29 09:26:25 -04:00
|
|
|
let node_resolver = Arc::new(NodeResolver::new(
|
2024-07-25 19:08:14 -04:00
|
|
|
deno_runtime::deno_node::DenoFsNodeResolverEnv::new(fs.clone()),
|
2024-11-01 12:27:00 -04:00
|
|
|
in_npm_pkg_checker.clone(),
|
2024-11-14 15:24:25 -05:00
|
|
|
npm_resolver.clone().into_npm_pkg_folder_resolver(),
|
2024-11-01 12:27:00 -04:00
|
|
|
pkg_json_resolver.clone(),
|
|
|
|
));
|
|
|
|
let cjs_tracker = Arc::new(CjsTracker::new(
|
|
|
|
in_npm_pkg_checker.clone(),
|
|
|
|
pkg_json_resolver.clone(),
|
2024-11-13 10:10:09 -05:00
|
|
|
IsCjsResolverOptions {
|
|
|
|
detect_cjs: !metadata.workspace_resolver.package_jsons.is_empty(),
|
|
|
|
is_node_main: false,
|
2024-11-01 12:27:00 -04:00
|
|
|
},
|
2023-04-26 13:07:15 -04:00
|
|
|
));
|
2023-05-25 14:27:45 -04:00
|
|
|
let cache_db = Caches::new(deno_dir_provider.clone());
|
2023-05-10 20:06:59 -04:00
|
|
|
let node_analysis_cache = NodeAnalysisCache::new(cache_db.node_analysis_db());
|
2024-11-14 15:24:25 -05:00
|
|
|
let npm_req_resolver =
|
|
|
|
Arc::new(CliNpmReqResolver::new(NpmReqResolverOptions {
|
|
|
|
byonm_resolver: (npm_resolver.clone()).into_maybe_byonm(),
|
|
|
|
fs: CliDenoResolverFs(fs.clone()),
|
|
|
|
in_npm_pkg_checker: in_npm_pkg_checker.clone(),
|
|
|
|
node_resolver: node_resolver.clone(),
|
|
|
|
npm_req_resolver: npm_resolver.clone().into_npm_req_resolver(),
|
|
|
|
}));
|
2024-09-20 00:10:34 -04:00
|
|
|
let cjs_esm_code_analyzer = CliCjsCodeAnalyzer::new(
|
|
|
|
node_analysis_cache,
|
2024-11-01 12:27:00 -04:00
|
|
|
cjs_tracker.clone(),
|
2024-09-20 00:10:34 -04:00
|
|
|
fs.clone(),
|
2024-10-14 20:48:39 -04:00
|
|
|
None,
|
2024-09-20 00:10:34 -04:00
|
|
|
);
|
2023-05-10 20:06:59 -04:00
|
|
|
let node_code_translator = Arc::new(NodeCodeTranslator::new(
|
|
|
|
cjs_esm_code_analyzer,
|
2024-07-25 19:08:14 -04:00
|
|
|
deno_runtime::deno_node::DenoFsNodeResolverEnv::new(fs.clone()),
|
2024-11-01 12:27:00 -04:00
|
|
|
in_npm_pkg_checker,
|
2023-05-10 20:06:59 -04:00
|
|
|
node_resolver.clone(),
|
2024-11-14 15:24:25 -05:00
|
|
|
npm_resolver.clone().into_npm_pkg_folder_resolver(),
|
2024-11-01 12:27:00 -04:00
|
|
|
pkg_json_resolver.clone(),
|
2023-05-10 20:06:59 -04:00
|
|
|
));
|
2024-07-03 20:54:33 -04:00
|
|
|
let workspace_resolver = {
|
|
|
|
let import_map = match metadata.workspace_resolver.import_map {
|
|
|
|
Some(import_map) => Some(
|
|
|
|
import_map::parse_from_json_with_options(
|
|
|
|
root_dir_url.join(&import_map.specifier).unwrap(),
|
|
|
|
&import_map.json,
|
|
|
|
import_map::ImportMapOptions {
|
|
|
|
address_hook: None,
|
|
|
|
expand_imports: true,
|
|
|
|
},
|
|
|
|
)?
|
|
|
|
.import_map,
|
|
|
|
),
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
let pkg_jsons = metadata
|
|
|
|
.workspace_resolver
|
|
|
|
.package_jsons
|
|
|
|
.into_iter()
|
|
|
|
.map(|(relative_path, json)| {
|
|
|
|
let path = root_dir_url
|
|
|
|
.join(&relative_path)
|
|
|
|
.unwrap()
|
|
|
|
.to_file_path()
|
|
|
|
.unwrap();
|
|
|
|
let pkg_json =
|
2024-07-23 17:34:46 -04:00
|
|
|
deno_package_json::PackageJson::load_from_value(path, json);
|
2024-07-03 20:54:33 -04:00
|
|
|
Arc::new(pkg_json)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
WorkspaceResolver::new_raw(
|
2024-07-17 09:13:22 -04:00
|
|
|
root_dir_url.clone(),
|
2024-07-03 20:54:33 -04:00
|
|
|
import_map,
|
2024-08-07 03:43:05 -04:00
|
|
|
metadata
|
|
|
|
.workspace_resolver
|
|
|
|
.jsr_pkgs
|
|
|
|
.iter()
|
|
|
|
.map(|pkg| ResolverWorkspaceJsrPackage {
|
2024-08-20 10:11:43 -04:00
|
|
|
is_patch: false, // only used for enhancing the diagnostic, which isn't shown in deno compile
|
2024-08-07 03:43:05 -04:00
|
|
|
base: root_dir_url.join(&pkg.relative_base).unwrap(),
|
|
|
|
name: pkg.name.clone(),
|
|
|
|
version: pkg.version.clone(),
|
|
|
|
exports: pkg.exports.clone(),
|
|
|
|
})
|
|
|
|
.collect(),
|
2024-07-03 20:54:33 -04:00
|
|
|
pkg_jsons,
|
|
|
|
metadata.workspace_resolver.pkg_json_resolution,
|
|
|
|
)
|
|
|
|
};
|
2024-11-18 15:09:28 -05:00
|
|
|
let code_cache = match metadata.code_cache_key {
|
|
|
|
Some(code_cache_key) => Some(Arc::new(DenoCompileCodeCache::new(
|
|
|
|
root_path.with_file_name(format!(
|
|
|
|
"{}.cache",
|
|
|
|
root_path.file_name().unwrap().to_string_lossy()
|
|
|
|
)),
|
|
|
|
code_cache_key,
|
|
|
|
)) as Arc<dyn CliCodeCache>),
|
|
|
|
None => {
|
|
|
|
log::debug!("Code cache disabled.");
|
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
2023-05-01 08:59:38 -04:00
|
|
|
let module_loader_factory = StandaloneModuleLoaderFactory {
|
2023-05-10 20:06:59 -04:00
|
|
|
shared: Arc::new(SharedModuleLoaderState {
|
2024-11-01 12:27:00 -04:00
|
|
|
cjs_tracker: cjs_tracker.clone(),
|
|
|
|
fs: fs.clone(),
|
2024-10-24 15:48:48 -04:00
|
|
|
modules,
|
2024-11-01 12:27:00 -04:00
|
|
|
node_code_translator: node_code_translator.clone(),
|
2024-11-14 15:24:25 -05:00
|
|
|
node_resolver: node_resolver.clone(),
|
2023-05-10 20:06:59 -04:00
|
|
|
npm_module_loader: Arc::new(NpmModuleLoader::new(
|
2024-11-01 12:27:00 -04:00
|
|
|
cjs_tracker.clone(),
|
2023-05-10 20:06:59 -04:00
|
|
|
fs.clone(),
|
2024-11-01 12:27:00 -04:00
|
|
|
node_code_translator,
|
2023-05-10 20:06:59 -04:00
|
|
|
)),
|
2024-11-18 15:09:28 -05:00
|
|
|
code_cache: code_cache.clone(),
|
2024-11-01 12:27:00 -04:00
|
|
|
npm_resolver: npm_resolver.clone(),
|
|
|
|
workspace_resolver,
|
2024-11-14 15:24:25 -05:00
|
|
|
npm_req_resolver,
|
2023-05-10 20:06:59 -04:00
|
|
|
}),
|
2023-05-01 08:59:38 -04:00
|
|
|
};
|
2023-04-26 13:07:15 -04:00
|
|
|
|
2023-05-26 13:33:38 -04:00
|
|
|
let permissions = {
|
2024-09-25 21:50:54 -04:00
|
|
|
let mut permissions =
|
|
|
|
metadata.permissions.to_options(/* cli_arg_urls */ &[]);
|
2024-10-24 15:48:48 -04:00
|
|
|
// grant read access to the vfs
|
|
|
|
match &mut permissions.allow_read {
|
|
|
|
Some(vec) if vec.is_empty() => {
|
|
|
|
// do nothing, already granted
|
|
|
|
}
|
|
|
|
Some(vec) => {
|
|
|
|
vec.push(root_path.to_string_lossy().to_string());
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
permissions.allow_read =
|
|
|
|
Some(vec![root_path.to_string_lossy().to_string()]);
|
2023-05-26 13:33:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
let desc_parser =
|
|
|
|
Arc::new(RuntimePermissionDescriptorParser::new(fs.clone()));
|
|
|
|
let permissions =
|
|
|
|
Permissions::from_options(desc_parser.as_ref(), &permissions)?;
|
|
|
|
PermissionsContainer::new(desc_parser, permissions)
|
2023-05-26 13:33:38 -04:00
|
|
|
};
|
2023-10-12 11:55:50 -04:00
|
|
|
let feature_checker = Arc::new({
|
|
|
|
let mut checker = FeatureChecker::default();
|
|
|
|
checker.set_exit_cb(Box::new(crate::unstable_exit_cb));
|
2024-01-22 12:37:28 -05:00
|
|
|
for feature in metadata.unstable_config.features {
|
2024-01-06 09:01:09 -05:00
|
|
|
// `metadata` is valid for the whole lifetime of the program, so we
|
|
|
|
// can leak the string here.
|
|
|
|
checker.enable_feature(feature.leak());
|
|
|
|
}
|
2023-10-12 11:55:50 -04:00
|
|
|
checker
|
|
|
|
});
|
2023-05-01 08:59:38 -04:00
|
|
|
let worker_factory = CliMainWorkerFactory::new(
|
2024-09-16 16:39:37 -04:00
|
|
|
Arc::new(BlobStore::default()),
|
2024-11-18 15:09:28 -05:00
|
|
|
code_cache,
|
2024-09-16 16:39:37 -04:00
|
|
|
feature_checker,
|
|
|
|
fs,
|
2023-05-22 16:55:04 -04:00
|
|
|
None,
|
2023-10-30 20:25:58 -04:00
|
|
|
None,
|
2024-08-30 17:58:24 -04:00
|
|
|
None,
|
2024-09-16 16:39:37 -04:00
|
|
|
Box::new(module_loader_factory),
|
|
|
|
node_resolver,
|
|
|
|
npm_resolver,
|
2024-11-01 12:27:00 -04:00
|
|
|
pkg_json_resolver,
|
2024-09-16 16:39:37 -04:00
|
|
|
root_cert_store_provider,
|
2024-09-25 21:50:54 -04:00
|
|
|
permissions,
|
2024-09-16 16:39:37 -04:00
|
|
|
StorageKeyResolver::empty(),
|
|
|
|
crate::args::DenoSubcommand::Run(Default::default()),
|
2023-05-01 08:59:38 -04:00
|
|
|
CliMainWorkerOptions {
|
|
|
|
argv: metadata.argv,
|
2023-05-30 11:34:50 -04:00
|
|
|
log_level: WorkerLogLevel::Info,
|
2023-11-05 16:27:36 -05:00
|
|
|
enable_op_summary_metrics: false,
|
2021-10-05 16:41:14 -04:00
|
|
|
enable_testing_features: false,
|
2023-05-10 20:06:59 -04:00
|
|
|
has_node_modules_dir,
|
2023-10-30 20:25:58 -04:00
|
|
|
hmr: false,
|
2023-05-01 08:59:38 -04:00
|
|
|
inspect_brk: false,
|
|
|
|
inspect_wait: false,
|
2023-11-10 12:41:24 -05:00
|
|
|
strace_ops: None,
|
2023-05-01 08:59:38 -04:00
|
|
|
is_inspecting: false,
|
2023-11-11 12:01:48 -05:00
|
|
|
skip_op_registration: true,
|
2021-10-05 16:41:14 -04:00
|
|
|
location: metadata.location,
|
2024-07-03 20:54:33 -04:00
|
|
|
argv0: NpmPackageReqReference::from_specifier(&main_module)
|
2024-02-23 11:30:29 -05:00
|
|
|
.ok()
|
|
|
|
.map(|req_ref| npm_pkg_req_ref_to_binary_command(&req_ref))
|
|
|
|
.or(std::env::args().next()),
|
2024-05-05 10:16:02 -04:00
|
|
|
node_debug: std::env::var("NODE_DEBUG").ok(),
|
2023-05-01 08:59:38 -04:00
|
|
|
origin_data_folder_path: None,
|
|
|
|
seed: metadata.seed,
|
|
|
|
unsafely_ignore_certificate_errors: metadata
|
|
|
|
.unsafely_ignore_certificate_errors,
|
2024-02-13 11:22:30 -05:00
|
|
|
create_hmr_runner: None,
|
|
|
|
create_coverage_collector: None,
|
2024-08-30 17:58:24 -04:00
|
|
|
node_ipc: None,
|
|
|
|
serve_port: None,
|
|
|
|
serve_host: None,
|
2021-10-05 16:41:14 -04:00
|
|
|
},
|
2024-11-13 05:38:46 -05:00
|
|
|
metadata.otel_config,
|
2021-10-05 16:41:14 -04:00
|
|
|
);
|
2022-06-28 10:49:30 -04:00
|
|
|
|
2024-03-31 10:58:19 -04:00
|
|
|
// Initialize v8 once from the main thread.
|
2023-05-13 15:42:34 -04:00
|
|
|
v8_set_flags(construct_v8_flags(&[], &metadata.v8_flags, vec![]));
|
2024-09-16 19:43:36 -04:00
|
|
|
// TODO(bartlomieju): remove last argument once Deploy no longer needs it
|
2024-08-01 18:15:20 -04:00
|
|
|
deno_core::JsRuntime::init_platform(None, true);
|
2022-06-28 10:49:30 -04:00
|
|
|
|
2023-05-01 08:59:38 -04:00
|
|
|
let mut worker = worker_factory
|
2024-09-25 21:50:54 -04:00
|
|
|
.create_main_worker(WorkerExecutionMode::Run, main_module)
|
2023-05-01 08:59:38 -04:00
|
|
|
.await?;
|
2021-01-04 18:15:52 -05:00
|
|
|
|
2023-05-01 08:59:38 -04:00
|
|
|
let exit_code = worker.run().await?;
|
2024-03-31 10:58:19 -04:00
|
|
|
Ok(exit_code)
|
2021-01-04 18:15:52 -05:00
|
|
|
}
|