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-05-23 17:26:23 -04:00
|
|
|
use crate::args::create_default_npmrc;
|
2023-04-26 13:07:15 -04:00
|
|
|
use crate::args::get_root_cert_store;
|
2023-05-10 20:06:59 -04:00
|
|
|
use crate::args::npm_pkg_req_ref_to_binary_command;
|
2023-01-17 19:18:24 -05:00
|
|
|
use crate::args::CaData;
|
2023-04-26 13:07:15 -04:00
|
|
|
use crate::args::CacheSetting;
|
2023-05-10 20:06:59 -04:00
|
|
|
use crate::args::PackageJsonDepsProvider;
|
2023-05-01 08:59:38 -04:00
|
|
|
use crate::args::StorageKeyResolver;
|
2023-05-10 20:06:59 -04:00
|
|
|
use crate::cache::Caches;
|
2023-05-25 14:27:45 -04:00
|
|
|
use crate::cache::DenoDirProvider;
|
2023-05-10 20:06:59 -04:00
|
|
|
use crate::cache::NodeAnalysisCache;
|
2024-06-03 17:17:08 -04:00
|
|
|
use crate::http_util::HttpClientProvider;
|
2023-07-19 04:30:04 -04:00
|
|
|
use crate::node::CliCjsCodeAnalyzer;
|
2023-10-02 17:53:55 -04:00
|
|
|
use crate::npm::create_cli_npm_resolver;
|
2023-11-29 09:32:23 -05:00
|
|
|
use crate::npm::CliNpmResolverByonmCreateOptions;
|
2023-10-02 17:53:55 -04:00
|
|
|
use crate::npm::CliNpmResolverCreateOptions;
|
|
|
|
use crate::npm::CliNpmResolverManagedCreateOptions;
|
|
|
|
use crate::npm::CliNpmResolverManagedPackageJsonInstallerOption;
|
|
|
|
use crate::npm::CliNpmResolverManagedSnapshotOption;
|
2023-06-08 11:48:29 -04:00
|
|
|
use crate::npm::NpmCacheDir;
|
2024-01-27 12:10:36 -05:00
|
|
|
use crate::resolver::CjsResolutionStore;
|
|
|
|
use crate::resolver::CliNodeResolver;
|
2023-05-10 20:06:59 -04:00
|
|
|
use crate::resolver::MappedSpecifierResolver;
|
2024-01-27 12:10:36 -05:00
|
|
|
use crate::resolver::NpmModuleLoader;
|
2023-04-26 13:07:15 -04:00
|
|
|
use crate::util::progress_bar::ProgressBar;
|
|
|
|
use crate::util::progress_bar::ProgressBarStyle;
|
2023-01-24 23:03:03 -05:00
|
|
|
use crate::util::v8::construct_v8_flags;
|
2023-05-01 08:59:38 -04:00
|
|
|
use crate::worker::CliMainWorkerFactory;
|
|
|
|
use crate::worker::CliMainWorkerOptions;
|
2024-05-16 03:09:35 -04:00
|
|
|
use crate::worker::ModuleLoaderAndSourceMapGetter;
|
2023-05-01 08:59:38 -04:00
|
|
|
use crate::worker::ModuleLoaderFactory;
|
2023-05-10 20:06:59 -04:00
|
|
|
use deno_ast::MediaType;
|
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;
|
|
|
|
use deno_core::futures::FutureExt;
|
2021-01-04 18:15:52 -05:00
|
|
|
use deno_core::v8_set_flags;
|
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;
|
2023-05-04 14:28:42 -04:00
|
|
|
use deno_runtime::deno_fs;
|
2023-05-10 20:06:59 -04:00
|
|
|
use deno_runtime::deno_node::analyze::NodeCodeTranslator;
|
2024-03-05 19:23:51 -05:00
|
|
|
use deno_runtime::deno_node::NodeResolutionMode;
|
2023-05-01 08:59:38 -04:00
|
|
|
use deno_runtime::deno_node::NodeResolver;
|
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-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;
|
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-02-13 11:22:30 -05:00
|
|
|
pub mod binary;
|
2023-05-10 20:06:59 -04:00
|
|
|
mod file_system;
|
|
|
|
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-05-10 20:06:59 -04:00
|
|
|
use self::binary::load_npm_vfs;
|
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;
|
|
|
|
|
|
|
|
struct SharedModuleLoaderState {
|
|
|
|
eszip: eszip::EszipV2,
|
|
|
|
mapped_specifier_resolver: MappedSpecifierResolver,
|
2023-10-03 19:05:06 -04:00
|
|
|
node_resolver: Arc<CliNodeResolver>,
|
2023-05-10 20:06:59 -04:00
|
|
|
npm_module_loader: Arc<NpmModuleLoader>,
|
|
|
|
}
|
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>,
|
|
|
|
root_permissions: PermissionsContainer,
|
|
|
|
dynamic_permissions: PermissionsContainer,
|
2022-02-24 08:28:00 -05:00
|
|
|
}
|
2020-11-30 14:35:12 -05:00
|
|
|
|
|
|
|
impl ModuleLoader for EmbeddedModuleLoader {
|
|
|
|
fn resolve(
|
|
|
|
&self,
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2023-10-03 19:05:06 -04: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,
|
2023-10-03 19:05:06 -04:00
|
|
|
) {
|
2024-03-05 19:23:51 -05:00
|
|
|
return match result? {
|
|
|
|
Some(res) => Ok(res.into_url()),
|
|
|
|
None => Err(generic_error("not found")),
|
|
|
|
};
|
2023-05-10 20:06:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
let maybe_mapped = self
|
|
|
|
.shared
|
|
|
|
.mapped_specifier_resolver
|
|
|
|
.resolve(specifier, &referrer)?
|
|
|
|
.into_specifier();
|
|
|
|
|
|
|
|
// npm specifier
|
|
|
|
let specifier_text = maybe_mapped
|
2023-03-15 17:46:36 -04:00
|
|
|
.as_ref()
|
2023-05-10 20:06:59 -04:00
|
|
|
.map(|r| r.as_str())
|
|
|
|
.unwrap_or(specifier);
|
|
|
|
if let Ok(reference) = NpmPackageReqReference::from_str(specifier_text) {
|
2024-03-05 19:23:51 -05:00
|
|
|
return self
|
|
|
|
.shared
|
|
|
|
.node_resolver
|
|
|
|
.resolve_req_reference(
|
|
|
|
&reference,
|
|
|
|
&referrer,
|
|
|
|
NodeResolutionMode::Execution,
|
|
|
|
)
|
|
|
|
.map(|res| res.into_url());
|
2023-05-10 20:06:59 -04:00
|
|
|
}
|
|
|
|
|
2024-03-05 19:23:51 -05:00
|
|
|
let specifier = match maybe_mapped {
|
|
|
|
Some(resolved) => resolved,
|
|
|
|
None => deno_core::resolve_import(specifier, referrer.as_str())?,
|
|
|
|
};
|
2024-04-27 17:11:57 -04:00
|
|
|
|
|
|
|
if specifier.scheme() == "jsr" {
|
|
|
|
if let Some(module) = self.shared.eszip.get_module(specifier.as_str()) {
|
|
|
|
return Ok(ModuleSpecifier::parse(&module.specifier).unwrap());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 19:23:51 -05:00
|
|
|
self
|
|
|
|
.shared
|
|
|
|
.node_resolver
|
|
|
|
.handle_if_in_node_modules(specifier)
|
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) {
|
|
|
|
let npm_module_loader = self.shared.npm_module_loader.clone();
|
|
|
|
let original_specifier = original_specifier.clone();
|
|
|
|
let maybe_referrer = maybe_referrer.cloned();
|
|
|
|
return deno_core::ModuleLoadResponse::Async(
|
|
|
|
async move {
|
|
|
|
let code_source = 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?;
|
|
|
|
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-03-21 16:57:32 -04:00
|
|
|
None,
|
2024-05-18 11:42:03 -04:00
|
|
|
))
|
|
|
|
}
|
|
|
|
.boxed_local(),
|
|
|
|
);
|
2023-05-10 20:06:59 -04:00
|
|
|
}
|
|
|
|
|
2023-08-27 00:04:12 -04:00
|
|
|
let Some(module) =
|
|
|
|
self.shared.eszip.get_module(original_specifier.as_str())
|
|
|
|
else {
|
2024-01-27 01:57:41 -05:00
|
|
|
return deno_core::ModuleLoadResponse::Sync(Err(type_error(format!(
|
|
|
|
"Module not found: {}",
|
|
|
|
original_specifier
|
2023-08-27 00:04:12 -04:00
|
|
|
))));
|
2023-07-27 12:38:32 -04:00
|
|
|
};
|
|
|
|
let original_specifier = original_specifier.clone();
|
|
|
|
let found_specifier =
|
|
|
|
ModuleSpecifier::parse(&module.specifier).expect("invalid url in eszip");
|
2023-04-04 08:46:31 -04:00
|
|
|
|
2024-01-27 01:57:41 -05:00
|
|
|
deno_core::ModuleLoadResponse::Async(
|
|
|
|
async move {
|
|
|
|
let code = module.source().await.ok_or_else(|| {
|
|
|
|
type_error(format!("Module not found: {}", original_specifier))
|
|
|
|
})?;
|
|
|
|
let code = arc_u8_to_arc_str(code)
|
|
|
|
.map_err(|_| type_error("Module source is not utf-8"))?;
|
|
|
|
Ok(deno_core::ModuleSource::new_with_redirect(
|
|
|
|
match module.kind {
|
|
|
|
eszip::ModuleKind::JavaScript => ModuleType::JavaScript,
|
|
|
|
eszip::ModuleKind::Json => ModuleType::Json,
|
|
|
|
eszip::ModuleKind::Jsonc => {
|
|
|
|
return Err(type_error("jsonc modules not supported"))
|
|
|
|
}
|
|
|
|
eszip::ModuleKind::OpaqueData => {
|
|
|
|
unreachable!();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ModuleSourceCode::String(code.into()),
|
|
|
|
&original_specifier,
|
|
|
|
&found_specifier,
|
2024-03-21 16:57:32 -04:00
|
|
|
None,
|
2024-01-27 01:57:41 -05:00
|
|
|
))
|
|
|
|
}
|
|
|
|
.boxed_local(),
|
|
|
|
)
|
2020-11-30 14:35:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-27 12:38:32 -04:00
|
|
|
fn arc_u8_to_arc_str(
|
|
|
|
arc_u8: Arc<[u8]>,
|
|
|
|
) -> Result<Arc<str>, std::str::Utf8Error> {
|
|
|
|
// Check that the string is valid UTF-8.
|
|
|
|
std::str::from_utf8(&arc_u8)?;
|
|
|
|
// SAFETY: the string is valid UTF-8, and the layout Arc<[u8]> is the same as
|
|
|
|
// Arc<str>. This is proven by the From<Arc<str>> impl for Arc<[u8]> from the
|
|
|
|
// standard library.
|
|
|
|
Ok(unsafe { std::mem::transmute(arc_u8) })
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-05-01 08:59:38 -04:00
|
|
|
impl ModuleLoaderFactory for StandaloneModuleLoaderFactory {
|
|
|
|
fn create_for_main(
|
|
|
|
&self,
|
2023-05-10 20:06:59 -04:00
|
|
|
root_permissions: PermissionsContainer,
|
|
|
|
dynamic_permissions: PermissionsContainer,
|
2024-05-16 03:09:35 -04:00
|
|
|
) -> ModuleLoaderAndSourceMapGetter {
|
|
|
|
ModuleLoaderAndSourceMapGetter {
|
|
|
|
module_loader: Rc::new(EmbeddedModuleLoader {
|
|
|
|
shared: self.shared.clone(),
|
|
|
|
root_permissions,
|
|
|
|
dynamic_permissions,
|
|
|
|
}),
|
|
|
|
source_map_getter: None,
|
|
|
|
}
|
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,
|
2023-05-10 20:06:59 -04:00
|
|
|
root_permissions: PermissionsContainer,
|
|
|
|
dynamic_permissions: PermissionsContainer,
|
2024-05-16 03:09:35 -04:00
|
|
|
) -> ModuleLoaderAndSourceMapGetter {
|
|
|
|
ModuleLoaderAndSourceMapGetter {
|
|
|
|
module_loader: Rc::new(EmbeddedModuleLoader {
|
|
|
|
shared: self.shared.clone(),
|
|
|
|
root_permissions,
|
|
|
|
dynamic_permissions,
|
|
|
|
}),
|
|
|
|
source_map_getter: None,
|
|
|
|
}
|
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())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-07 21:08:51 -05:00
|
|
|
pub async fn run(
|
2023-06-08 11:48:29 -04:00
|
|
|
mut eszip: eszip::EszipV2,
|
2021-01-07 21:08:51 -05:00
|
|
|
metadata: Metadata,
|
2024-03-31 10:58:19 -04:00
|
|
|
) -> Result<i32, AnyError> {
|
2022-02-15 07:33:46 -05:00
|
|
|
let main_module = &metadata.entrypoint;
|
2023-05-10 20:06:59 -04:00
|
|
|
let current_exe_path = std::env::current_exe().unwrap();
|
|
|
|
let current_exe_name =
|
|
|
|
current_exe_path.file_name().unwrap().to_string_lossy();
|
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();
|
|
|
|
let root_path = std::env::temp_dir()
|
|
|
|
.join(format!("deno-compile-{}", current_exe_name))
|
|
|
|
.join("node_modules");
|
2024-05-23 17:26:23 -04:00
|
|
|
let npm_cache_dir =
|
|
|
|
NpmCacheDir::new(root_path.clone(), vec![npm_registry_url.clone()]);
|
2023-10-02 17:53:55 -04:00
|
|
|
let npm_global_cache_dir = npm_cache_dir.get_cache_location();
|
2023-11-29 09:32:23 -05:00
|
|
|
let cache_setting = CacheSetting::Only;
|
|
|
|
let (package_json_deps_provider, fs, npm_resolver, maybe_vfs_root) =
|
|
|
|
match metadata.node_modules {
|
|
|
|
Some(binary::NodeModules::Managed {
|
|
|
|
node_modules_dir,
|
|
|
|
package_json_deps,
|
|
|
|
}) => {
|
|
|
|
// this will always have a snapshot
|
|
|
|
let snapshot = eszip.take_npm_snapshot().unwrap();
|
|
|
|
let vfs_root_dir_path = if node_modules_dir {
|
|
|
|
root_path
|
|
|
|
} else {
|
2024-05-23 17:26:23 -04:00
|
|
|
npm_cache_dir.root_dir().to_owned()
|
2023-11-29 09:32:23 -05:00
|
|
|
};
|
|
|
|
let vfs = load_npm_vfs(vfs_root_dir_path.clone())
|
|
|
|
.context("Failed to load npm vfs.")?;
|
|
|
|
let maybe_node_modules_path = if node_modules_dir {
|
|
|
|
Some(vfs.root().to_path_buf())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
let package_json_deps_provider =
|
|
|
|
Arc::new(PackageJsonDepsProvider::new(
|
|
|
|
package_json_deps.map(|serialized| serialized.into_deps()),
|
|
|
|
));
|
|
|
|
let fs = Arc::new(DenoCompileFileSystem::new(vfs))
|
|
|
|
as Arc<dyn deno_fs::FileSystem>;
|
|
|
|
let npm_resolver = create_cli_npm_resolver(
|
|
|
|
CliNpmResolverCreateOptions::Managed(CliNpmResolverManagedCreateOptions {
|
|
|
|
snapshot: CliNpmResolverManagedSnapshotOption::Specified(Some(snapshot)),
|
|
|
|
maybe_lockfile: None,
|
|
|
|
fs: fs.clone(),
|
2024-06-03 17:17:08 -04:00
|
|
|
http_client_provider: http_client_provider.clone(),
|
2023-11-29 09:32:23 -05:00
|
|
|
npm_global_cache_dir,
|
|
|
|
cache_setting,
|
|
|
|
text_only_progress_bar: progress_bar,
|
|
|
|
maybe_node_modules_path,
|
|
|
|
package_json_installer:
|
|
|
|
CliNpmResolverManagedPackageJsonInstallerOption::ConditionalInstall(
|
|
|
|
package_json_deps_provider.clone(),
|
|
|
|
),
|
|
|
|
npm_system_info: Default::default(),
|
2024-05-23 17:26:23 -04:00
|
|
|
// Packages from different registries are already inlined in the ESZip,
|
|
|
|
// so no need to create actual `.npmrc` configuration.
|
|
|
|
npmrc: create_default_npmrc(),
|
2023-11-29 09:32:23 -05:00
|
|
|
}),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
(
|
|
|
|
package_json_deps_provider,
|
|
|
|
fs,
|
|
|
|
npm_resolver,
|
|
|
|
Some(vfs_root_dir_path),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Some(binary::NodeModules::Byonm { package_json_deps }) => {
|
|
|
|
let vfs_root_dir_path = root_path;
|
|
|
|
let vfs = load_npm_vfs(vfs_root_dir_path.clone())
|
|
|
|
.context("Failed to load npm vfs.")?;
|
|
|
|
let node_modules_path = vfs.root().join("node_modules");
|
|
|
|
let package_json_deps_provider =
|
|
|
|
Arc::new(PackageJsonDepsProvider::new(
|
|
|
|
package_json_deps.map(|serialized| serialized.into_deps()),
|
|
|
|
));
|
|
|
|
let fs = Arc::new(DenoCompileFileSystem::new(vfs))
|
|
|
|
as Arc<dyn deno_fs::FileSystem>;
|
|
|
|
let npm_resolver =
|
|
|
|
create_cli_npm_resolver(CliNpmResolverCreateOptions::Byonm(
|
|
|
|
CliNpmResolverByonmCreateOptions {
|
|
|
|
fs: fs.clone(),
|
|
|
|
root_node_modules_dir: node_modules_path,
|
|
|
|
},
|
|
|
|
))
|
|
|
|
.await?;
|
|
|
|
(
|
|
|
|
package_json_deps_provider,
|
|
|
|
fs,
|
|
|
|
npm_resolver,
|
|
|
|
Some(vfs_root_dir_path),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
let package_json_deps_provider =
|
|
|
|
Arc::new(PackageJsonDepsProvider::new(None));
|
|
|
|
let fs = Arc::new(deno_fs::RealFs) as Arc<dyn deno_fs::FileSystem>;
|
|
|
|
let npm_resolver = create_cli_npm_resolver(
|
|
|
|
CliNpmResolverCreateOptions::Managed(CliNpmResolverManagedCreateOptions {
|
|
|
|
snapshot: CliNpmResolverManagedSnapshotOption::Specified(None),
|
|
|
|
maybe_lockfile: None,
|
|
|
|
fs: fs.clone(),
|
2024-06-03 17:17:08 -04:00
|
|
|
http_client_provider: http_client_provider.clone(),
|
2023-11-29 09:32:23 -05:00
|
|
|
npm_global_cache_dir,
|
|
|
|
cache_setting,
|
|
|
|
text_only_progress_bar: progress_bar,
|
|
|
|
maybe_node_modules_path: None,
|
|
|
|
package_json_installer:
|
|
|
|
CliNpmResolverManagedPackageJsonInstallerOption::ConditionalInstall(
|
|
|
|
package_json_deps_provider.clone(),
|
|
|
|
),
|
|
|
|
npm_system_info: Default::default(),
|
2024-05-23 17:26:23 -04:00
|
|
|
// Packages from different registries are already inlined in the ESZip,
|
|
|
|
// so no need to create actual `.npmrc` configuration.
|
|
|
|
npmrc: create_default_npmrc(),
|
2023-11-29 09:32:23 -05:00
|
|
|
}),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
(package_json_deps_provider, fs, npm_resolver, None)
|
|
|
|
}
|
2023-05-10 20:06:59 -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(
|
|
|
|
fs.clone(),
|
|
|
|
npm_resolver.clone().into_npm_resolver(),
|
2023-04-26 13:07:15 -04:00
|
|
|
));
|
2023-05-10 20:06:59 -04:00
|
|
|
let cjs_resolutions = Arc::new(CjsResolutionStore::default());
|
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());
|
2023-07-24 15:35:13 -04:00
|
|
|
let cjs_esm_code_analyzer =
|
|
|
|
CliCjsCodeAnalyzer::new(node_analysis_cache, fs.clone());
|
2023-05-10 20:06:59 -04:00
|
|
|
let node_code_translator = Arc::new(NodeCodeTranslator::new(
|
|
|
|
cjs_esm_code_analyzer,
|
|
|
|
fs.clone(),
|
|
|
|
node_resolver.clone(),
|
2023-09-29 09:26:25 -04:00
|
|
|
npm_resolver.clone().into_npm_resolver(),
|
2023-05-10 20:06:59 -04:00
|
|
|
));
|
|
|
|
let maybe_import_map = metadata.maybe_import_map.map(|(base, source)| {
|
|
|
|
Arc::new(parse_from_json(&base, &source).unwrap().import_map)
|
|
|
|
});
|
2023-10-03 19:05:06 -04:00
|
|
|
let cli_node_resolver = Arc::new(CliNodeResolver::new(
|
2024-03-05 19:23:51 -05:00
|
|
|
Some(cjs_resolutions.clone()),
|
|
|
|
fs.clone(),
|
2023-10-03 19:05:06 -04:00
|
|
|
node_resolver.clone(),
|
|
|
|
npm_resolver.clone(),
|
|
|
|
));
|
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 {
|
|
|
|
eszip,
|
|
|
|
mapped_specifier_resolver: MappedSpecifierResolver::new(
|
|
|
|
maybe_import_map.clone(),
|
|
|
|
package_json_deps_provider.clone(),
|
2023-05-01 08:59:38 -04:00
|
|
|
),
|
2023-10-03 19:05:06 -04:00
|
|
|
node_resolver: cli_node_resolver.clone(),
|
2023-05-10 20:06:59 -04:00
|
|
|
npm_module_loader: Arc::new(NpmModuleLoader::new(
|
|
|
|
cjs_resolutions,
|
|
|
|
node_code_translator,
|
|
|
|
fs.clone(),
|
2023-10-03 19:05:06 -04:00
|
|
|
cli_node_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-05-06 19:21:58 -04:00
|
|
|
let maybe_cwd = std::env::current_dir().ok();
|
|
|
|
let mut permissions =
|
|
|
|
metadata.permissions.to_options(maybe_cwd.as_deref())?;
|
2023-05-26 13:33:38 -04:00
|
|
|
// if running with an npm vfs, grant read access to it
|
2023-11-29 09:32:23 -05:00
|
|
|
if let Some(vfs_root) = maybe_vfs_root {
|
2023-05-26 13:33:38 -04:00
|
|
|
match &mut permissions.allow_read {
|
|
|
|
Some(vec) if vec.is_empty() => {
|
|
|
|
// do nothing, already granted
|
|
|
|
}
|
|
|
|
Some(vec) => {
|
|
|
|
vec.push(vfs_root);
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
permissions.allow_read = Some(vec![vfs_root]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PermissionsContainer::new(Permissions::from_options(&permissions)?)
|
|
|
|
};
|
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));
|
|
|
|
// TODO(bartlomieju): enable, once we deprecate `--unstable` in favor
|
|
|
|
// of granular --unstable-* flags.
|
|
|
|
// feature_checker.set_warn_cb(Box::new(crate::unstable_warn_cb));
|
2024-01-22 12:37:28 -05:00
|
|
|
if metadata.unstable_config.legacy_flag_enabled {
|
2023-10-12 11:55:50 -04:00
|
|
|
checker.enable_legacy_unstable();
|
|
|
|
}
|
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(
|
|
|
|
StorageKeyResolver::empty(),
|
2023-11-14 16:06:00 -05:00
|
|
|
crate::args::DenoSubcommand::Run(Default::default()),
|
2023-09-28 16:43:45 -04:00
|
|
|
npm_resolver,
|
2023-05-01 08:59:38 -04:00
|
|
|
node_resolver,
|
2023-07-01 18:52:30 -04:00
|
|
|
Default::default(),
|
2023-05-01 08:59:38 -04:00
|
|
|
Box::new(module_loader_factory),
|
2023-05-01 16:42:05 -04:00
|
|
|
root_cert_store_provider,
|
2023-05-05 12:44:24 -04:00
|
|
|
fs,
|
2023-05-01 08:59:38 -04:00
|
|
|
None,
|
2023-05-22 16:55:04 -04:00
|
|
|
None,
|
2023-10-30 20:25:58 -04:00
|
|
|
None,
|
2023-10-12 11:55:50 -04:00
|
|
|
feature_checker,
|
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-05-01 08:59:38 -04:00
|
|
|
coverage_dir: None,
|
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-05-10 20:06:59 -04:00
|
|
|
is_npm_main: main_module.scheme() == "npm",
|
2023-11-11 12:01:48 -05:00
|
|
|
skip_op_registration: true,
|
2021-10-05 16:41:14 -04:00
|
|
|
location: metadata.location,
|
2024-02-23 11:30:29 -05:00
|
|
|
argv0: NpmPackageReqReference::from_specifier(main_module)
|
|
|
|
.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-01-22 12:37:28 -05:00
|
|
|
unstable: metadata.unstable_config.legacy_flag_enabled,
|
2023-10-03 19:05:06 -04:00
|
|
|
maybe_root_package_json_deps: package_json_deps_provider.deps().cloned(),
|
2024-02-13 11:22:30 -05:00
|
|
|
create_hmr_runner: None,
|
|
|
|
create_coverage_collector: None,
|
2021-10-05 16:41:14 -04:00
|
|
|
},
|
2023-12-13 05:14:16 -05:00
|
|
|
None,
|
2024-04-24 15:45:49 -04:00
|
|
|
None,
|
|
|
|
None,
|
2024-02-14 23:50:17 -05:00
|
|
|
false,
|
2024-01-31 17:18:14 -05:00
|
|
|
// TODO(bartlomieju): temporarily disabled
|
|
|
|
// metadata.disable_deprecated_api_warning,
|
|
|
|
true,
|
2024-01-26 10:41:16 -05:00
|
|
|
false,
|
2024-04-17 10:19:55 -04:00
|
|
|
// Code cache is not supported for standalone binary yet.
|
|
|
|
None,
|
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-03-31 10:58:19 -04:00
|
|
|
deno_core::JsRuntime::init_platform(None);
|
2022-06-28 10:49:30 -04:00
|
|
|
|
2023-05-01 08:59:38 -04:00
|
|
|
let mut worker = worker_factory
|
2024-04-24 15:45:49 -04:00
|
|
|
.create_main_worker(
|
|
|
|
WorkerExecutionMode::Run,
|
|
|
|
main_module.clone(),
|
|
|
|
permissions,
|
|
|
|
)
|
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
|
|
|
}
|