2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2021-01-07 21:08:51 -05:00
|
|
|
|
2023-04-26 13:07:15 -04:00
|
|
|
use crate::args::get_root_cert_store;
|
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-01 08:59:38 -04:00
|
|
|
use crate::args::StorageKeyResolver;
|
2023-04-26 13:07:15 -04:00
|
|
|
use crate::cache::DenoDir;
|
2021-09-13 00:19:23 -04:00
|
|
|
use crate::file_fetcher::get_source_from_data_url;
|
2023-04-26 13:07:15 -04:00
|
|
|
use crate::http_util::HttpClient;
|
|
|
|
use crate::npm::create_npm_fs_resolver;
|
|
|
|
use crate::npm::CliNpmRegistryApi;
|
|
|
|
use crate::npm::CliNpmResolver;
|
|
|
|
use crate::npm::NpmCache;
|
|
|
|
use crate::npm::NpmResolution;
|
|
|
|
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;
|
|
|
|
use crate::worker::HasNodeSpecifierChecker;
|
|
|
|
use crate::worker::ModuleLoaderFactory;
|
2023-02-15 11:30:54 -05:00
|
|
|
use crate::CliGraphResolver;
|
2021-11-16 09:02:28 -05:00
|
|
|
use deno_core::anyhow::Context;
|
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;
|
2020-11-30 14:35:12 -05:00
|
|
|
use deno_core::ModuleLoader;
|
|
|
|
use deno_core::ModuleSpecifier;
|
2023-04-04 08:46:31 -04:00
|
|
|
use deno_core::ModuleType;
|
2023-01-10 08:35:44 -05:00
|
|
|
use deno_core::ResolutionKind;
|
2022-02-24 08:28:00 -05:00
|
|
|
use deno_graph::source::Resolver;
|
2023-04-26 13:07:15 -04:00
|
|
|
use deno_runtime::deno_node;
|
2023-05-01 08:59:38 -04:00
|
|
|
use deno_runtime::deno_node::NodeResolver;
|
2023-05-01 16:42:05 -04:00
|
|
|
use deno_runtime::deno_tls::rustls::RootCertStore;
|
|
|
|
use deno_runtime::deno_tls::RootCertStoreProvider;
|
2023-04-26 13:07:15 -04:00
|
|
|
use deno_runtime::deno_web::BlobStore;
|
2020-12-13 13:45:53 -05:00
|
|
|
use deno_runtime::permissions::Permissions;
|
2023-01-07 11:25:34 -05:00
|
|
|
use deno_runtime::permissions::PermissionsContainer;
|
2022-02-24 08:28:00 -05:00
|
|
|
use import_map::parse_from_json;
|
2020-11-30 14:35:12 -05:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::rc::Rc;
|
2020-12-11 12:49:26 -05:00
|
|
|
use std::sync::Arc;
|
2020-11-30 14:35:12 -05:00
|
|
|
|
2023-04-19 17:50:56 -04:00
|
|
|
mod binary;
|
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;
|
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-03-19 18:32:54 -04:00
|
|
|
eszip: Arc<eszip::EszipV2>,
|
|
|
|
maybe_import_map_resolver: Option<Arc<CliGraphResolver>>,
|
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-01-10 08:35:44 -05:00
|
|
|
_kind: ResolutionKind,
|
2020-11-30 14:35:12 -05:00
|
|
|
) -> Result<ModuleSpecifier, AnyError> {
|
2022-03-31 04:41:30 -04:00
|
|
|
// Try to follow redirects when resolving.
|
|
|
|
let referrer = match self.eszip.get_module(referrer) {
|
|
|
|
Some(eszip::Module { ref specifier, .. }) => {
|
2023-03-14 13:18:01 -04:00
|
|
|
ModuleSpecifier::parse(specifier)?
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
let cwd = std::env::current_dir().context("Unable to get CWD")?;
|
|
|
|
deno_core::resolve_url_or_path(referrer, &cwd)?
|
2022-03-31 04:41:30 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-03-15 17:46:36 -04:00
|
|
|
self
|
|
|
|
.maybe_import_map_resolver
|
|
|
|
.as_ref()
|
|
|
|
.map(|r| r.resolve(specifier, &referrer))
|
|
|
|
.unwrap_or_else(|| {
|
2022-02-24 08:28:00 -05:00
|
|
|
deno_core::resolve_import(specifier, referrer.as_str())
|
|
|
|
.map_err(|err| err.into())
|
2023-03-15 17:46:36 -04:00
|
|
|
})
|
2020-11-30 14:35:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn load(
|
|
|
|
&self,
|
|
|
|
module_specifier: &ModuleSpecifier,
|
2023-04-04 08:46:31 -04:00
|
|
|
_maybe_referrer: Option<&ModuleSpecifier>,
|
2020-11-30 14:35:12 -05:00
|
|
|
_is_dynamic: bool,
|
|
|
|
) -> Pin<Box<deno_core::ModuleSourceFuture>> {
|
2023-01-05 14:29:50 -05:00
|
|
|
let is_data_uri = get_source_from_data_url(module_specifier).ok();
|
2022-02-15 07:33:46 -05:00
|
|
|
let module = self
|
2022-02-24 08:28:00 -05:00
|
|
|
.eszip
|
2022-02-15 07:33:46 -05:00
|
|
|
.get_module(module_specifier.as_str())
|
|
|
|
.ok_or_else(|| type_error("Module not found"));
|
2023-04-04 08:46:31 -04:00
|
|
|
// TODO(mmastrac): This clone can probably be removed in the future if ModuleSpecifier is no longer a full-fledged URL
|
2023-01-05 14:29:50 -05:00
|
|
|
let module_specifier = module_specifier.clone();
|
2023-04-04 08:46:31 -04:00
|
|
|
|
2020-11-30 14:35:12 -05:00
|
|
|
async move {
|
2022-05-05 07:16:25 -04:00
|
|
|
if let Some((source, _)) = is_data_uri {
|
2023-04-04 08:46:31 -04:00
|
|
|
return Ok(deno_core::ModuleSource::new(
|
|
|
|
deno_core::ModuleType::JavaScript,
|
|
|
|
source.into(),
|
|
|
|
&module_specifier,
|
|
|
|
));
|
2020-11-30 14:35:12 -05:00
|
|
|
}
|
2021-06-05 10:24:17 -04:00
|
|
|
|
2022-02-15 07:33:46 -05:00
|
|
|
let module = module?;
|
2023-04-06 18:46:44 -04:00
|
|
|
let code = module.source().await.unwrap_or_default();
|
2022-02-15 07:33:46 -05:00
|
|
|
let code = std::str::from_utf8(&code)
|
|
|
|
.map_err(|_| type_error("Module source is not utf-8"))?
|
2023-04-04 08:46:31 -04:00
|
|
|
.to_owned()
|
|
|
|
.into();
|
2022-02-15 07:33:46 -05:00
|
|
|
|
2023-04-04 08:46:31 -04:00
|
|
|
Ok(deno_core::ModuleSource::new(
|
|
|
|
match module.kind {
|
|
|
|
eszip::ModuleKind::JavaScript => ModuleType::JavaScript,
|
|
|
|
eszip::ModuleKind::Json => ModuleType::Json,
|
2022-02-15 07:33:46 -05:00
|
|
|
},
|
2023-04-04 08:46:31 -04:00
|
|
|
code,
|
|
|
|
&module_specifier,
|
|
|
|
))
|
2020-11-30 14:35:12 -05:00
|
|
|
}
|
|
|
|
.boxed_local()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-01 08:59:38 -04:00
|
|
|
struct StandaloneModuleLoaderFactory {
|
|
|
|
loader: EmbeddedModuleLoader,
|
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,
|
|
|
|
_root_permissions: PermissionsContainer,
|
|
|
|
_dynamic_permissions: PermissionsContainer,
|
|
|
|
) -> Rc<dyn ModuleLoader> {
|
|
|
|
Rc::new(self.loader.clone())
|
|
|
|
}
|
2023-04-26 13:07:15 -04:00
|
|
|
|
2023-05-01 08:59:38 -04:00
|
|
|
fn create_for_worker(
|
|
|
|
&self,
|
|
|
|
_root_permissions: PermissionsContainer,
|
|
|
|
_dynamic_permissions: PermissionsContainer,
|
|
|
|
) -> Rc<dyn ModuleLoader> {
|
|
|
|
Rc::new(self.loader.clone())
|
|
|
|
}
|
2023-03-19 18:32:54 -04:00
|
|
|
|
2023-05-01 08:59:38 -04:00
|
|
|
fn create_source_map_getter(
|
|
|
|
&self,
|
|
|
|
) -> Option<Box<dyn deno_core::SourceMapGetter>> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2023-03-19 18:32:54 -04:00
|
|
|
|
2023-05-01 08:59:38 -04:00
|
|
|
struct StandaloneHasNodeSpecifierChecker;
|
2023-03-19 18:32:54 -04:00
|
|
|
|
2023-05-01 08:59:38 -04:00
|
|
|
impl HasNodeSpecifierChecker for StandaloneHasNodeSpecifierChecker {
|
|
|
|
fn has_node_specifier(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
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(
|
2022-02-15 07:33:46 -05:00
|
|
|
eszip: eszip::EszipV2,
|
2021-01-07 21:08:51 -05:00
|
|
|
metadata: Metadata,
|
|
|
|
) -> Result<(), AnyError> {
|
2022-02-15 07:33:46 -05:00
|
|
|
let main_module = &metadata.entrypoint;
|
2023-04-26 13:07:15 -04:00
|
|
|
let dir = DenoDir::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);
|
2023-05-01 16:42:05 -04:00
|
|
|
let http_client = Arc::new(HttpClient::new(
|
|
|
|
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-04-26 13:07:15 -04:00
|
|
|
let npm_registry_url = CliNpmRegistryApi::default_url().to_owned();
|
|
|
|
let npm_cache = Arc::new(NpmCache::new(
|
|
|
|
dir.npm_folder_path(),
|
|
|
|
CacheSetting::Use,
|
|
|
|
http_client.clone(),
|
|
|
|
progress_bar.clone(),
|
|
|
|
));
|
|
|
|
let npm_api = Arc::new(CliNpmRegistryApi::new(
|
|
|
|
npm_registry_url.clone(),
|
|
|
|
npm_cache.clone(),
|
|
|
|
http_client.clone(),
|
|
|
|
progress_bar.clone(),
|
|
|
|
));
|
|
|
|
let node_fs = Arc::new(deno_node::RealFs);
|
|
|
|
let npm_resolution =
|
|
|
|
Arc::new(NpmResolution::from_serialized(npm_api.clone(), None, None));
|
|
|
|
let npm_fs_resolver = create_npm_fs_resolver(
|
|
|
|
node_fs.clone(),
|
|
|
|
npm_cache,
|
|
|
|
&progress_bar,
|
|
|
|
npm_registry_url,
|
|
|
|
npm_resolution.clone(),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
let npm_resolver = Arc::new(CliNpmResolver::new(
|
|
|
|
npm_resolution.clone(),
|
|
|
|
npm_fs_resolver,
|
|
|
|
None,
|
|
|
|
));
|
2023-05-01 08:59:38 -04:00
|
|
|
let node_resolver =
|
|
|
|
Arc::new(NodeResolver::new(node_fs.clone(), npm_resolver.clone()));
|
|
|
|
let module_loader_factory = StandaloneModuleLoaderFactory {
|
|
|
|
loader: EmbeddedModuleLoader {
|
|
|
|
eszip: Arc::new(eszip),
|
|
|
|
maybe_import_map_resolver: metadata.maybe_import_map.map(
|
|
|
|
|(base, source)| {
|
|
|
|
Arc::new(CliGraphResolver::new(
|
|
|
|
None,
|
|
|
|
Some(Arc::new(
|
|
|
|
parse_from_json(&base, &source).unwrap().import_map,
|
|
|
|
)),
|
|
|
|
false,
|
|
|
|
npm_api.clone(),
|
|
|
|
npm_resolution.clone(),
|
|
|
|
Default::default(),
|
|
|
|
))
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
};
|
2023-04-26 13:07:15 -04:00
|
|
|
|
2023-05-01 08:59:38 -04:00
|
|
|
let worker_factory = CliMainWorkerFactory::new(
|
|
|
|
StorageKeyResolver::empty(),
|
|
|
|
npm_resolver.clone(),
|
|
|
|
node_resolver,
|
|
|
|
Box::new(StandaloneHasNodeSpecifierChecker),
|
|
|
|
BlobStore::default(),
|
|
|
|
Box::new(module_loader_factory),
|
2023-05-01 16:42:05 -04:00
|
|
|
root_cert_store_provider,
|
2023-04-26 13:07:15 -04:00
|
|
|
node_fs,
|
2023-05-01 08:59:38 -04:00
|
|
|
None,
|
|
|
|
CliMainWorkerOptions {
|
|
|
|
argv: metadata.argv,
|
|
|
|
debug: false,
|
|
|
|
coverage_dir: None,
|
2021-10-05 16:41:14 -04:00
|
|
|
enable_testing_features: false,
|
2023-05-01 08:59:38 -04:00
|
|
|
has_node_modules_dir: false,
|
|
|
|
inspect_brk: false,
|
|
|
|
inspect_wait: false,
|
|
|
|
is_inspecting: false,
|
|
|
|
is_npm_main: false,
|
2021-10-05 16:41:14 -04:00
|
|
|
location: metadata.location,
|
2023-05-01 08:59:38 -04:00
|
|
|
// todo(dsherret): support a binary command being compiled
|
|
|
|
maybe_binary_npm_command_name: None,
|
|
|
|
origin_data_folder_path: None,
|
|
|
|
seed: metadata.seed,
|
|
|
|
unsafely_ignore_certificate_errors: metadata
|
|
|
|
.unsafely_ignore_certificate_errors,
|
2021-10-05 16:41:14 -04:00
|
|
|
unstable: metadata.unstable,
|
|
|
|
},
|
|
|
|
);
|
2022-06-28 10:49:30 -04:00
|
|
|
|
2023-05-01 08:59:38 -04:00
|
|
|
v8_set_flags(construct_v8_flags(&metadata.v8_flags, vec![]));
|
2022-06-28 10:49:30 -04:00
|
|
|
|
2023-05-01 08:59:38 -04:00
|
|
|
let permissions = PermissionsContainer::new(Permissions::from_options(
|
|
|
|
&metadata.permissions,
|
|
|
|
)?);
|
|
|
|
let mut worker = worker_factory
|
|
|
|
.create_main_worker(main_module.clone(), permissions)
|
|
|
|
.await?;
|
2021-01-04 18:15:52 -05:00
|
|
|
|
2023-05-01 08:59:38 -04:00
|
|
|
let exit_code = worker.run().await?;
|
|
|
|
std::process::exit(exit_code)
|
2021-01-04 18:15:52 -05:00
|
|
|
}
|