2023-01-13 02:51:32 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2022-08-11 16:59:12 -04:00
|
|
|
use std::path::PathBuf;
|
2023-04-27 10:05:20 -04:00
|
|
|
use std::rc::Rc;
|
2022-08-11 16:59:12 -04:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use deno_ast::ModuleSpecifier;
|
2023-08-01 18:47:57 -04:00
|
|
|
use deno_core::anyhow::bail;
|
2023-05-22 16:55:04 -04:00
|
|
|
use deno_core::anyhow::Context;
|
2022-08-11 16:59:12 -04:00
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::futures::FutureExt;
|
|
|
|
use deno_core::located_script_name;
|
2023-05-22 16:55:04 -04:00
|
|
|
use deno_core::parking_lot::Mutex;
|
2023-04-27 10:05:20 -04:00
|
|
|
use deno_core::url::Url;
|
2023-09-14 10:38:15 -04:00
|
|
|
use deno_core::v8;
|
2023-04-27 10:05:20 -04:00
|
|
|
use deno_core::CompiledWasmModuleStore;
|
2022-08-11 16:59:12 -04:00
|
|
|
use deno_core::Extension;
|
|
|
|
use deno_core::ModuleId;
|
2023-05-01 08:59:38 -04:00
|
|
|
use deno_core::ModuleLoader;
|
2023-04-27 10:05:20 -04:00
|
|
|
use deno_core::SharedArrayBufferStore;
|
2023-05-01 08:59:38 -04:00
|
|
|
use deno_core::SourceMapGetter;
|
2023-05-22 16:55:04 -04:00
|
|
|
use deno_lockfile::Lockfile;
|
2022-08-11 16:59:12 -04:00
|
|
|
use deno_runtime::colors;
|
2023-04-27 10:05:20 -04:00
|
|
|
use deno_runtime::deno_broadcast_channel::InMemoryBroadcastChannel;
|
2023-05-04 14:28:42 -04:00
|
|
|
use deno_runtime::deno_fs;
|
2023-02-10 10:26:39 -05:00
|
|
|
use deno_runtime::deno_node;
|
2023-04-21 21:02:46 -04:00
|
|
|
use deno_runtime::deno_node::NodeResolution;
|
2023-08-01 18:47:57 -04:00
|
|
|
use deno_runtime::deno_node::NodeResolutionMode;
|
2023-04-27 10:05:20 -04:00
|
|
|
use deno_runtime::deno_node::NodeResolver;
|
2023-05-01 16:42:05 -04:00
|
|
|
use deno_runtime::deno_tls::RootCertStoreProvider;
|
2023-04-27 10:05:20 -04:00
|
|
|
use deno_runtime::deno_web::BlobStore;
|
2022-09-02 16:53:23 -04:00
|
|
|
use deno_runtime::fmt_errors::format_js_error;
|
2023-04-27 10:05:20 -04:00
|
|
|
use deno_runtime::inspector_server::InspectorServer;
|
2022-08-11 16:59:12 -04:00
|
|
|
use deno_runtime::ops::worker_host::CreateWebWorkerCb;
|
2023-01-07 11:25:34 -05:00
|
|
|
use deno_runtime::permissions::PermissionsContainer;
|
2022-08-11 16:59:12 -04:00
|
|
|
use deno_runtime::web_worker::WebWorker;
|
|
|
|
use deno_runtime::web_worker::WebWorkerOptions;
|
|
|
|
use deno_runtime::worker::MainWorker;
|
|
|
|
use deno_runtime::worker::WorkerOptions;
|
|
|
|
use deno_runtime::BootstrapOptions;
|
2023-05-30 11:34:50 -04:00
|
|
|
use deno_runtime::WorkerLogLevel;
|
2023-09-28 16:43:45 -04:00
|
|
|
use deno_semver::npm::NpmPackageNvReference;
|
2023-04-06 18:46:44 -04:00
|
|
|
use deno_semver::npm::NpmPackageReqReference;
|
2023-09-18 16:02:58 -04:00
|
|
|
use deno_semver::package::PackageReqReference;
|
2022-08-11 16:59:12 -04:00
|
|
|
|
2023-09-18 16:02:58 -04:00
|
|
|
use crate::args::package_json::PackageJsonDeps;
|
2023-04-27 10:05:20 -04:00
|
|
|
use crate::args::StorageKeyResolver;
|
2022-08-11 16:59:12 -04:00
|
|
|
use crate::errors;
|
2023-04-27 10:05:20 -04:00
|
|
|
use crate::npm::CliNpmResolver;
|
2022-08-11 16:59:12 -04:00
|
|
|
use crate::ops;
|
|
|
|
use crate::tools;
|
|
|
|
use crate::tools::coverage::CoverageCollector;
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::util::checksum;
|
2022-08-11 16:59:12 -04:00
|
|
|
use crate::version;
|
|
|
|
|
2023-05-01 08:59:38 -04:00
|
|
|
pub trait ModuleLoaderFactory: Send + Sync {
|
|
|
|
fn create_for_main(
|
|
|
|
&self,
|
|
|
|
root_permissions: PermissionsContainer,
|
|
|
|
dynamic_permissions: PermissionsContainer,
|
|
|
|
) -> Rc<dyn ModuleLoader>;
|
|
|
|
|
|
|
|
fn create_for_worker(
|
|
|
|
&self,
|
|
|
|
root_permissions: PermissionsContainer,
|
|
|
|
dynamic_permissions: PermissionsContainer,
|
|
|
|
) -> Rc<dyn ModuleLoader>;
|
|
|
|
|
|
|
|
fn create_source_map_getter(&self) -> Option<Box<dyn SourceMapGetter>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
// todo(dsherret): this is temporary and we should remove this
|
|
|
|
// once we no longer conditionally initialize the node runtime
|
|
|
|
pub trait HasNodeSpecifierChecker: Send + Sync {
|
|
|
|
fn has_node_specifier(&self) -> bool;
|
|
|
|
}
|
|
|
|
|
2023-05-01 14:35:23 -04:00
|
|
|
#[derive(Clone)]
|
2023-04-27 10:05:20 -04:00
|
|
|
pub struct CliMainWorkerOptions {
|
|
|
|
pub argv: Vec<String>,
|
2023-05-30 11:34:50 -04:00
|
|
|
pub log_level: WorkerLogLevel,
|
2023-04-27 10:05:20 -04:00
|
|
|
pub coverage_dir: Option<String>,
|
|
|
|
pub enable_testing_features: bool,
|
|
|
|
pub has_node_modules_dir: bool,
|
|
|
|
pub inspect_brk: bool,
|
|
|
|
pub inspect_wait: bool,
|
|
|
|
pub is_inspecting: bool,
|
|
|
|
pub is_npm_main: bool,
|
|
|
|
pub location: Option<Url>,
|
|
|
|
pub maybe_binary_npm_command_name: Option<String>,
|
2023-05-01 08:59:38 -04:00
|
|
|
pub origin_data_folder_path: Option<PathBuf>,
|
2023-04-27 10:05:20 -04:00
|
|
|
pub seed: Option<u64>,
|
|
|
|
pub unsafely_ignore_certificate_errors: Option<Vec<String>>,
|
|
|
|
pub unstable: bool,
|
2023-09-18 16:02:58 -04:00
|
|
|
pub maybe_package_json_deps: Option<PackageJsonDeps>,
|
2023-04-27 10:05:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct SharedWorkerState {
|
2023-05-01 08:59:38 -04:00
|
|
|
options: CliMainWorkerOptions,
|
|
|
|
storage_key_resolver: StorageKeyResolver,
|
2023-09-29 09:26:25 -04:00
|
|
|
npm_resolver: Arc<dyn CliNpmResolver>,
|
2023-05-01 08:59:38 -04:00
|
|
|
node_resolver: Arc<NodeResolver>,
|
2023-07-01 18:52:30 -04:00
|
|
|
blob_store: Arc<BlobStore>,
|
2023-05-01 08:59:38 -04:00
|
|
|
broadcast_channel: InMemoryBroadcastChannel,
|
|
|
|
shared_array_buffer_store: SharedArrayBufferStore,
|
|
|
|
compiled_wasm_module_store: CompiledWasmModuleStore,
|
|
|
|
module_loader_factory: Box<dyn ModuleLoaderFactory>,
|
2023-05-01 16:42:05 -04:00
|
|
|
root_cert_store_provider: Arc<dyn RootCertStoreProvider>,
|
2023-05-04 14:28:42 -04:00
|
|
|
fs: Arc<dyn deno_fs::FileSystem>,
|
2023-05-01 08:59:38 -04:00
|
|
|
maybe_inspector_server: Option<Arc<InspectorServer>>,
|
2023-05-22 16:55:04 -04:00
|
|
|
maybe_lockfile: Option<Arc<Mutex<Lockfile>>>,
|
2023-05-01 08:59:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SharedWorkerState {
|
2023-08-15 15:36:36 -04:00
|
|
|
// Currently empty
|
2023-04-27 10:05:20 -04:00
|
|
|
}
|
|
|
|
|
2022-08-11 16:59:12 -04:00
|
|
|
pub struct CliMainWorker {
|
|
|
|
main_module: ModuleSpecifier,
|
2022-08-23 10:39:19 -04:00
|
|
|
is_main_cjs: bool,
|
2022-08-11 16:59:12 -04:00
|
|
|
worker: MainWorker,
|
2023-04-27 10:05:20 -04:00
|
|
|
shared: Arc<SharedWorkerState>,
|
2022-08-11 16:59:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CliMainWorker {
|
|
|
|
pub fn into_main_worker(self) -> MainWorker {
|
|
|
|
self.worker
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn setup_repl(&mut self) -> Result<(), AnyError> {
|
|
|
|
self.worker.run_event_loop(false).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run(&mut self) -> Result<i32, AnyError> {
|
|
|
|
let mut maybe_coverage_collector =
|
|
|
|
self.maybe_setup_coverage_collector().await?;
|
|
|
|
log::debug!("main_module {}", self.main_module);
|
|
|
|
|
2022-09-03 12:19:30 -04:00
|
|
|
if self.is_main_cjs {
|
2023-02-10 10:26:39 -05:00
|
|
|
deno_node::load_cjs_module(
|
2022-08-23 10:39:19 -04:00
|
|
|
&mut self.worker.js_runtime,
|
|
|
|
&self.main_module.to_file_path().unwrap().to_string_lossy(),
|
|
|
|
true,
|
2023-04-27 10:05:20 -04:00
|
|
|
self.shared.options.inspect_brk,
|
2022-08-23 10:39:19 -04:00
|
|
|
)?;
|
2022-08-11 16:59:12 -04:00
|
|
|
} else {
|
2022-08-20 11:31:33 -04:00
|
|
|
self.execute_main_module_possibly_with_npm().await?;
|
2022-08-11 16:59:12 -04:00
|
|
|
}
|
|
|
|
|
2023-03-21 18:33:12 -04:00
|
|
|
self.worker.dispatch_load_event(located_script_name!())?;
|
2022-08-11 16:59:12 -04:00
|
|
|
|
|
|
|
loop {
|
|
|
|
self
|
|
|
|
.worker
|
|
|
|
.run_event_loop(maybe_coverage_collector.is_none())
|
|
|
|
.await?;
|
|
|
|
if !self
|
|
|
|
.worker
|
2023-03-21 18:33:12 -04:00
|
|
|
.dispatch_beforeunload_event(located_script_name!())?
|
2022-08-11 16:59:12 -04:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 18:33:12 -04:00
|
|
|
self.worker.dispatch_unload_event(located_script_name!())?;
|
2022-08-11 16:59:12 -04:00
|
|
|
|
|
|
|
if let Some(coverage_collector) = maybe_coverage_collector.as_mut() {
|
|
|
|
self
|
|
|
|
.worker
|
|
|
|
.with_event_loop(coverage_collector.stop_collecting().boxed_local())
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
|
2023-01-05 14:29:50 -05:00
|
|
|
Ok(self.worker.exit_code())
|
2022-08-11 16:59:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run_for_watcher(self) -> Result<(), AnyError> {
|
|
|
|
/// The FileWatcherModuleExecutor provides module execution with safe dispatching of life-cycle events by tracking the
|
|
|
|
/// state of any pending events and emitting accordingly on drop in the case of a future
|
|
|
|
/// cancellation.
|
|
|
|
struct FileWatcherModuleExecutor {
|
2022-08-20 11:31:33 -04:00
|
|
|
inner: CliMainWorker,
|
2022-08-11 16:59:12 -04:00
|
|
|
pending_unload: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FileWatcherModuleExecutor {
|
2022-08-20 11:31:33 -04:00
|
|
|
pub fn new(worker: CliMainWorker) -> FileWatcherModuleExecutor {
|
2022-08-11 16:59:12 -04:00
|
|
|
FileWatcherModuleExecutor {
|
2022-08-20 11:31:33 -04:00
|
|
|
inner: worker,
|
2022-08-11 16:59:12 -04:00
|
|
|
pending_unload: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Execute the given main module emitting load and unload events before and after execution
|
|
|
|
/// respectively.
|
2022-08-20 11:31:33 -04:00
|
|
|
pub async fn execute(&mut self) -> Result<(), AnyError> {
|
|
|
|
self.inner.execute_main_module_possibly_with_npm().await?;
|
|
|
|
self
|
|
|
|
.inner
|
|
|
|
.worker
|
2023-03-21 18:33:12 -04:00
|
|
|
.dispatch_load_event(located_script_name!())?;
|
2022-08-11 16:59:12 -04:00
|
|
|
self.pending_unload = true;
|
|
|
|
|
|
|
|
let result = loop {
|
2022-09-06 07:18:23 -04:00
|
|
|
match self.inner.worker.run_event_loop(false).await {
|
|
|
|
Ok(()) => {}
|
|
|
|
Err(error) => break Err(error),
|
|
|
|
}
|
|
|
|
match self
|
2022-08-20 11:31:33 -04:00
|
|
|
.inner
|
2022-08-11 16:59:12 -04:00
|
|
|
.worker
|
2023-03-21 18:33:12 -04:00
|
|
|
.dispatch_beforeunload_event(located_script_name!())
|
2022-08-11 16:59:12 -04:00
|
|
|
{
|
2022-09-06 07:18:23 -04:00
|
|
|
Ok(default_prevented) if default_prevented => {} // continue loop
|
|
|
|
Ok(_) => break Ok(()),
|
|
|
|
Err(error) => break Err(error),
|
2022-08-11 16:59:12 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
self.pending_unload = false;
|
|
|
|
|
2022-09-26 15:52:16 -04:00
|
|
|
result?;
|
2022-08-11 16:59:12 -04:00
|
|
|
|
2022-08-20 11:31:33 -04:00
|
|
|
self
|
|
|
|
.inner
|
|
|
|
.worker
|
2023-03-21 18:33:12 -04:00
|
|
|
.dispatch_unload_event(located_script_name!())?;
|
2022-08-11 16:59:12 -04:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for FileWatcherModuleExecutor {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if self.pending_unload {
|
2022-09-06 07:18:23 -04:00
|
|
|
let _ = self
|
2022-08-20 11:31:33 -04:00
|
|
|
.inner
|
2022-08-11 16:59:12 -04:00
|
|
|
.worker
|
2023-03-21 18:33:12 -04:00
|
|
|
.dispatch_unload_event(located_script_name!());
|
2022-08-11 16:59:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-20 11:31:33 -04:00
|
|
|
let mut executor = FileWatcherModuleExecutor::new(self);
|
|
|
|
executor.execute().await
|
2022-08-11 16:59:12 -04:00
|
|
|
}
|
|
|
|
|
2023-04-13 13:43:23 -04:00
|
|
|
pub async fn execute_main_module_possibly_with_npm(
|
2022-08-20 11:31:33 -04:00
|
|
|
&mut self,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let id = self.worker.preload_main_module(&self.main_module).await?;
|
|
|
|
self.evaluate_module_possibly_with_npm(id).await
|
|
|
|
}
|
|
|
|
|
2023-04-13 13:43:23 -04:00
|
|
|
pub async fn execute_side_module_possibly_with_npm(
|
2022-08-20 11:31:33 -04:00
|
|
|
&mut self,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let id = self.worker.preload_side_module(&self.main_module).await?;
|
|
|
|
self.evaluate_module_possibly_with_npm(id).await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn evaluate_module_possibly_with_npm(
|
|
|
|
&mut self,
|
|
|
|
id: ModuleId,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
self.worker.evaluate_module(id).await
|
|
|
|
}
|
|
|
|
|
2023-04-13 13:43:23 -04:00
|
|
|
pub async fn maybe_setup_coverage_collector(
|
2022-08-11 16:59:12 -04:00
|
|
|
&mut self,
|
|
|
|
) -> Result<Option<CoverageCollector>, AnyError> {
|
2023-04-27 10:05:20 -04:00
|
|
|
if let Some(coverage_dir) = &self.shared.options.coverage_dir {
|
2022-08-11 16:59:12 -04:00
|
|
|
let session = self.worker.create_inspector_session().await;
|
|
|
|
|
|
|
|
let coverage_dir = PathBuf::from(coverage_dir);
|
|
|
|
let mut coverage_collector =
|
|
|
|
tools::coverage::CoverageCollector::new(coverage_dir, session);
|
|
|
|
self
|
|
|
|
.worker
|
|
|
|
.with_event_loop(coverage_collector.start_collecting().boxed_local())
|
|
|
|
.await?;
|
|
|
|
Ok(Some(coverage_collector))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
2023-09-14 10:38:15 -04:00
|
|
|
|
|
|
|
pub fn execute_script_static(
|
|
|
|
&mut self,
|
|
|
|
name: &'static str,
|
|
|
|
source_code: &'static str,
|
|
|
|
) -> Result<v8::Global<v8::Value>, AnyError> {
|
|
|
|
self
|
|
|
|
.worker
|
|
|
|
.js_runtime
|
|
|
|
.execute_script_static(name, source_code)
|
|
|
|
}
|
2022-08-11 16:59:12 -04:00
|
|
|
}
|
|
|
|
|
2023-04-27 10:05:20 -04:00
|
|
|
pub struct CliMainWorkerFactory {
|
|
|
|
shared: Arc<SharedWorkerState>,
|
2022-11-21 08:36:26 -05:00
|
|
|
}
|
|
|
|
|
2023-04-27 10:05:20 -04:00
|
|
|
impl CliMainWorkerFactory {
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
|
|
pub fn new(
|
|
|
|
storage_key_resolver: StorageKeyResolver,
|
2023-09-29 09:26:25 -04:00
|
|
|
npm_resolver: Arc<dyn CliNpmResolver>,
|
2023-04-27 10:05:20 -04:00
|
|
|
node_resolver: Arc<NodeResolver>,
|
2023-07-01 18:52:30 -04:00
|
|
|
blob_store: Arc<BlobStore>,
|
2023-05-01 08:59:38 -04:00
|
|
|
module_loader_factory: Box<dyn ModuleLoaderFactory>,
|
2023-05-01 16:42:05 -04:00
|
|
|
root_cert_store_provider: Arc<dyn RootCertStoreProvider>,
|
2023-05-04 14:28:42 -04:00
|
|
|
fs: Arc<dyn deno_fs::FileSystem>,
|
2023-04-27 10:05:20 -04:00
|
|
|
maybe_inspector_server: Option<Arc<InspectorServer>>,
|
2023-05-22 16:55:04 -04:00
|
|
|
maybe_lockfile: Option<Arc<Mutex<Lockfile>>>,
|
2023-04-27 10:05:20 -04:00
|
|
|
options: CliMainWorkerOptions,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
shared: Arc::new(SharedWorkerState {
|
|
|
|
options,
|
|
|
|
storage_key_resolver,
|
|
|
|
npm_resolver,
|
|
|
|
node_resolver,
|
|
|
|
blob_store,
|
|
|
|
broadcast_channel: Default::default(),
|
|
|
|
shared_array_buffer_store: Default::default(),
|
|
|
|
compiled_wasm_module_store: Default::default(),
|
|
|
|
module_loader_factory,
|
2023-05-01 16:42:05 -04:00
|
|
|
root_cert_store_provider,
|
2023-05-04 14:28:42 -04:00
|
|
|
fs,
|
2023-04-27 10:05:20 -04:00
|
|
|
maybe_inspector_server,
|
2023-05-22 16:55:04 -04:00
|
|
|
maybe_lockfile,
|
2023-04-27 10:05:20 -04:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn create_main_worker(
|
|
|
|
&self,
|
|
|
|
main_module: ModuleSpecifier,
|
|
|
|
permissions: PermissionsContainer,
|
|
|
|
) -> Result<CliMainWorker, AnyError> {
|
|
|
|
self
|
|
|
|
.create_custom_worker(
|
|
|
|
main_module,
|
|
|
|
permissions,
|
|
|
|
vec![],
|
|
|
|
Default::default(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn create_custom_worker(
|
|
|
|
&self,
|
|
|
|
main_module: ModuleSpecifier,
|
|
|
|
permissions: PermissionsContainer,
|
|
|
|
mut custom_extensions: Vec<Extension>,
|
|
|
|
stdio: deno_runtime::deno_io::Stdio,
|
|
|
|
) -> Result<CliMainWorker, AnyError> {
|
|
|
|
let shared = &self.shared;
|
|
|
|
let (main_module, is_main_cjs) = if let Ok(package_ref) =
|
|
|
|
NpmPackageReqReference::from_specifier(&main_module)
|
|
|
|
{
|
2023-09-18 16:02:58 -04:00
|
|
|
let package_ref = if package_ref.req().version_req.version_text() == "*" {
|
|
|
|
// When using the wildcard version, select the same version used in the
|
|
|
|
// package.json deps in order to prevent adding new dependency version
|
|
|
|
shared
|
|
|
|
.options
|
|
|
|
.maybe_package_json_deps
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|deps| {
|
|
|
|
deps
|
|
|
|
.values()
|
|
|
|
.filter_map(|v| v.as_ref().ok())
|
|
|
|
.find(|dep| dep.name == package_ref.req().name)
|
|
|
|
.map(|dep| {
|
|
|
|
NpmPackageReqReference::new(PackageReqReference {
|
|
|
|
req: dep.clone(),
|
|
|
|
sub_path: package_ref.sub_path().map(|s| s.to_string()),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.unwrap_or(package_ref)
|
|
|
|
} else {
|
|
|
|
package_ref
|
|
|
|
};
|
2023-09-29 09:26:25 -04:00
|
|
|
if let Some(npm_resolver) = shared.npm_resolver.as_managed() {
|
|
|
|
npm_resolver
|
|
|
|
.add_package_reqs(&[package_ref.req().clone()])
|
|
|
|
.await?;
|
|
|
|
}
|
2023-09-28 16:43:45 -04:00
|
|
|
let package_ref = shared
|
|
|
|
.npm_resolver
|
|
|
|
.resolve_pkg_nv_ref_from_pkg_req_ref(&package_ref)?;
|
2023-04-27 10:05:20 -04:00
|
|
|
let node_resolution =
|
2023-08-01 18:47:57 -04:00
|
|
|
self.resolve_binary_entrypoint(&package_ref, &permissions)?;
|
2023-04-27 10:05:20 -04:00
|
|
|
let is_main_cjs = matches!(node_resolution, NodeResolution::CommonJs(_));
|
2023-05-22 16:55:04 -04:00
|
|
|
|
|
|
|
if let Some(lockfile) = &shared.maybe_lockfile {
|
|
|
|
// For npm binary commands, ensure that the lockfile gets updated
|
|
|
|
// so that we can re-use the npm resolution the next time it runs
|
|
|
|
// for better performance
|
|
|
|
lockfile
|
|
|
|
.lock()
|
|
|
|
.write()
|
|
|
|
.context("Failed writing lockfile.")?;
|
|
|
|
}
|
|
|
|
|
2023-04-27 10:05:20 -04:00
|
|
|
(node_resolution.into_url(), is_main_cjs)
|
|
|
|
} else if shared.options.is_npm_main {
|
|
|
|
let node_resolution =
|
|
|
|
shared.node_resolver.url_to_node_resolution(main_module)?;
|
|
|
|
let is_main_cjs = matches!(node_resolution, NodeResolution::CommonJs(_));
|
|
|
|
(node_resolution.into_url(), is_main_cjs)
|
|
|
|
} else {
|
|
|
|
(main_module, false)
|
|
|
|
};
|
|
|
|
|
2023-05-01 08:59:38 -04:00
|
|
|
let module_loader = shared
|
|
|
|
.module_loader_factory
|
|
|
|
.create_for_main(PermissionsContainer::allow_all(), permissions.clone());
|
|
|
|
let maybe_source_map_getter =
|
|
|
|
shared.module_loader_factory.create_source_map_getter();
|
2023-04-27 10:05:20 -04:00
|
|
|
let maybe_inspector_server = shared.maybe_inspector_server.clone();
|
|
|
|
|
|
|
|
let create_web_worker_cb =
|
|
|
|
create_web_worker_callback(shared.clone(), stdio.clone());
|
|
|
|
|
|
|
|
let maybe_storage_key = shared
|
|
|
|
.storage_key_resolver
|
|
|
|
.resolve_storage_key(&main_module);
|
|
|
|
let origin_storage_dir = maybe_storage_key.as_ref().map(|key| {
|
|
|
|
shared
|
2022-08-11 16:59:12 -04:00
|
|
|
.options
|
2023-04-27 10:05:20 -04:00
|
|
|
.origin_data_folder_path
|
2023-05-01 08:59:38 -04:00
|
|
|
.as_ref()
|
|
|
|
.unwrap() // must be set if storage key resolver returns a value
|
2023-04-27 10:05:20 -04:00
|
|
|
.join(checksum::gen(&[key.as_bytes()]))
|
|
|
|
});
|
|
|
|
let cache_storage_dir = maybe_storage_key.map(|key| {
|
|
|
|
// TODO(@satyarohith): storage quota management
|
|
|
|
// Note: we currently use temp_dir() to avoid managing storage size.
|
|
|
|
std::env::temp_dir()
|
|
|
|
.join("deno_cache")
|
|
|
|
.join(checksum::gen(&[key.as_bytes()]))
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut extensions = ops::cli_exts(shared.npm_resolver.clone());
|
|
|
|
extensions.append(&mut custom_extensions);
|
|
|
|
|
|
|
|
let options = WorkerOptions {
|
|
|
|
bootstrap: BootstrapOptions {
|
|
|
|
args: shared.options.argv.clone(),
|
|
|
|
cpu_count: std::thread::available_parallelism()
|
|
|
|
.map(|p| p.get())
|
|
|
|
.unwrap_or(1),
|
2023-05-30 11:34:50 -04:00
|
|
|
log_level: shared.options.log_level,
|
2023-04-27 10:05:20 -04:00
|
|
|
enable_testing_features: shared.options.enable_testing_features,
|
|
|
|
locale: deno_core::v8::icu::get_language_tag(),
|
|
|
|
location: shared.options.location.clone(),
|
|
|
|
no_color: !colors::use_color(),
|
|
|
|
is_tty: colors::is_tty(),
|
|
|
|
runtime_version: version::deno().to_string(),
|
|
|
|
ts_version: version::TYPESCRIPT.to_string(),
|
|
|
|
unstable: shared.options.unstable,
|
|
|
|
user_agent: version::get_user_agent().to_string(),
|
|
|
|
inspect: shared.options.is_inspecting,
|
2023-08-15 15:36:36 -04:00
|
|
|
has_node_modules_dir: shared.options.has_node_modules_dir,
|
|
|
|
maybe_binary_npm_command_name: shared
|
|
|
|
.options
|
|
|
|
.maybe_binary_npm_command_name
|
|
|
|
.clone(),
|
2023-04-27 10:05:20 -04:00
|
|
|
},
|
|
|
|
extensions,
|
2023-08-05 19:47:15 -04:00
|
|
|
startup_snapshot: crate::js::deno_isolate_init(),
|
2023-06-05 05:22:32 -04:00
|
|
|
create_params: None,
|
2023-04-27 10:05:20 -04:00
|
|
|
unsafely_ignore_certificate_errors: shared
|
|
|
|
.options
|
|
|
|
.unsafely_ignore_certificate_errors
|
|
|
|
.clone(),
|
2023-05-01 16:42:05 -04:00
|
|
|
root_cert_store_provider: Some(shared.root_cert_store_provider.clone()),
|
2023-04-27 10:05:20 -04:00
|
|
|
seed: shared.options.seed,
|
2023-05-01 08:59:38 -04:00
|
|
|
source_map_getter: maybe_source_map_getter,
|
2023-04-27 10:05:20 -04:00
|
|
|
format_js_error_fn: Some(Arc::new(format_js_error)),
|
|
|
|
create_web_worker_cb,
|
|
|
|
maybe_inspector_server,
|
|
|
|
should_break_on_first_statement: shared.options.inspect_brk,
|
|
|
|
should_wait_for_inspector_session: shared.options.inspect_wait,
|
|
|
|
module_loader,
|
2023-05-04 14:28:42 -04:00
|
|
|
fs: shared.fs.clone(),
|
2023-09-29 09:26:25 -04:00
|
|
|
npm_resolver: Some(shared.npm_resolver.clone().into_npm_resolver()),
|
2023-04-27 10:05:20 -04:00
|
|
|
get_error_class_fn: Some(&errors::get_error_class_name),
|
|
|
|
cache_storage_dir,
|
|
|
|
origin_storage_dir,
|
|
|
|
blob_store: shared.blob_store.clone(),
|
|
|
|
broadcast_channel: shared.broadcast_channel.clone(),
|
|
|
|
shared_array_buffer_store: Some(shared.shared_array_buffer_store.clone()),
|
|
|
|
compiled_wasm_module_store: Some(
|
|
|
|
shared.compiled_wasm_module_store.clone(),
|
|
|
|
),
|
|
|
|
stdio,
|
|
|
|
};
|
|
|
|
|
|
|
|
let worker = MainWorker::bootstrap_from_options(
|
|
|
|
main_module.clone(),
|
|
|
|
permissions,
|
|
|
|
options,
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(CliMainWorker {
|
|
|
|
main_module,
|
|
|
|
is_main_cjs,
|
|
|
|
worker,
|
|
|
|
shared: shared.clone(),
|
|
|
|
})
|
|
|
|
}
|
2023-08-01 18:47:57 -04:00
|
|
|
|
|
|
|
fn resolve_binary_entrypoint(
|
|
|
|
&self,
|
2023-09-28 16:43:45 -04:00
|
|
|
package_ref: &NpmPackageNvReference,
|
2023-08-01 18:47:57 -04:00
|
|
|
permissions: &PermissionsContainer,
|
|
|
|
) -> Result<NodeResolution, AnyError> {
|
2023-09-28 16:43:45 -04:00
|
|
|
let package_folder = self
|
|
|
|
.shared
|
|
|
|
.npm_resolver
|
|
|
|
.resolve_pkg_folder_from_deno_module(package_ref.nv())?;
|
|
|
|
match self
|
|
|
|
.shared
|
|
|
|
.node_resolver
|
|
|
|
.resolve_binary_export(&package_folder, package_ref.sub_path())
|
|
|
|
{
|
2023-08-01 18:47:57 -04:00
|
|
|
Ok(node_resolution) => Ok(node_resolution),
|
|
|
|
Err(original_err) => {
|
|
|
|
// if the binary entrypoint was not found, fallback to regular node resolution
|
|
|
|
let result =
|
|
|
|
self.resolve_binary_entrypoint_fallback(package_ref, permissions);
|
|
|
|
match result {
|
|
|
|
Ok(Some(resolution)) => Ok(resolution),
|
|
|
|
Ok(None) => Err(original_err),
|
|
|
|
Err(fallback_err) => {
|
|
|
|
bail!("{:#}\n\nFallback failed: {:#}", original_err, fallback_err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// resolve the binary entrypoint using regular node resolution
|
|
|
|
fn resolve_binary_entrypoint_fallback(
|
|
|
|
&self,
|
2023-09-28 16:43:45 -04:00
|
|
|
package_ref: &NpmPackageNvReference,
|
2023-08-01 18:47:57 -04:00
|
|
|
permissions: &PermissionsContainer,
|
|
|
|
) -> Result<Option<NodeResolution>, AnyError> {
|
|
|
|
// only fallback if the user specified a sub path
|
2023-08-21 05:53:52 -04:00
|
|
|
if package_ref.sub_path().is_none() {
|
2023-08-01 18:47:57 -04:00
|
|
|
// it's confusing to users if the package doesn't have any binary
|
|
|
|
// entrypoint and we just execute the main script which will likely
|
|
|
|
// have blank output, so do not resolve the entrypoint in this case
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
2023-09-28 16:43:45 -04:00
|
|
|
let package_folder = self
|
|
|
|
.shared
|
|
|
|
.npm_resolver
|
|
|
|
.resolve_pkg_folder_from_deno_module(package_ref.nv())?;
|
|
|
|
let Some(resolution) = self.shared.node_resolver.resolve_npm_reference(
|
|
|
|
&package_folder,
|
|
|
|
package_ref.sub_path(),
|
|
|
|
NodeResolutionMode::Execution,
|
|
|
|
permissions,
|
|
|
|
)?
|
2023-08-27 00:04:12 -04:00
|
|
|
else {
|
2023-08-01 18:47:57 -04:00
|
|
|
return Ok(None);
|
|
|
|
};
|
|
|
|
match &resolution {
|
|
|
|
NodeResolution::BuiltIn(_) => Ok(None),
|
|
|
|
NodeResolution::CommonJs(specifier) | NodeResolution::Esm(specifier) => {
|
|
|
|
if specifier
|
|
|
|
.to_file_path()
|
|
|
|
.map(|p| p.exists())
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
|
|
|
Ok(Some(resolution))
|
|
|
|
} else {
|
|
|
|
bail!("Cannot find module '{}'", specifier)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-11 16:59:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn create_web_worker_callback(
|
2023-04-27 10:05:20 -04:00
|
|
|
shared: Arc<SharedWorkerState>,
|
2023-03-04 19:39:48 -05:00
|
|
|
stdio: deno_runtime::deno_io::Stdio,
|
2022-08-11 16:59:12 -04:00
|
|
|
) -> Arc<CreateWebWorkerCb> {
|
|
|
|
Arc::new(move |args| {
|
2023-04-27 10:05:20 -04:00
|
|
|
let maybe_inspector_server = shared.maybe_inspector_server.clone();
|
2022-08-11 16:59:12 -04:00
|
|
|
|
2023-05-01 08:59:38 -04:00
|
|
|
let module_loader = shared.module_loader_factory.create_for_worker(
|
|
|
|
args.parent_permissions.clone(),
|
|
|
|
args.permissions.clone(),
|
|
|
|
);
|
|
|
|
let maybe_source_map_getter =
|
|
|
|
shared.module_loader_factory.create_source_map_getter();
|
2022-08-11 16:59:12 -04:00
|
|
|
let create_web_worker_cb =
|
2023-04-27 10:05:20 -04:00
|
|
|
create_web_worker_callback(shared.clone(), stdio.clone());
|
2022-08-11 16:59:12 -04:00
|
|
|
|
2023-04-27 10:05:20 -04:00
|
|
|
let extensions = ops::cli_exts(shared.npm_resolver.clone());
|
2022-08-11 16:59:12 -04:00
|
|
|
|
2023-04-27 10:05:20 -04:00
|
|
|
let maybe_storage_key = shared
|
|
|
|
.storage_key_resolver
|
|
|
|
.resolve_storage_key(&args.main_module);
|
2022-09-28 08:11:12 -04:00
|
|
|
let cache_storage_dir = maybe_storage_key.map(|key| {
|
|
|
|
// TODO(@satyarohith): storage quota management
|
|
|
|
// Note: we currently use temp_dir() to avoid managing storage size.
|
|
|
|
std::env::temp_dir()
|
|
|
|
.join("deno_cache")
|
|
|
|
.join(checksum::gen(&[key.as_bytes()]))
|
|
|
|
});
|
|
|
|
|
2022-08-11 16:59:12 -04:00
|
|
|
let options = WebWorkerOptions {
|
|
|
|
bootstrap: BootstrapOptions {
|
2023-04-27 10:05:20 -04:00
|
|
|
args: shared.options.argv.clone(),
|
2022-08-11 16:59:12 -04:00
|
|
|
cpu_count: std::thread::available_parallelism()
|
|
|
|
.map(|p| p.get())
|
|
|
|
.unwrap_or(1),
|
2023-05-30 11:34:50 -04:00
|
|
|
log_level: shared.options.log_level,
|
2023-04-27 10:05:20 -04:00
|
|
|
enable_testing_features: shared.options.enable_testing_features,
|
2022-10-18 09:33:35 -04:00
|
|
|
locale: deno_core::v8::icu::get_language_tag(),
|
2022-08-11 16:59:12 -04:00
|
|
|
location: Some(args.main_module.clone()),
|
|
|
|
no_color: !colors::use_color(),
|
|
|
|
is_tty: colors::is_tty(),
|
2023-03-23 18:27:58 -04:00
|
|
|
runtime_version: version::deno().to_string(),
|
2022-08-11 16:59:12 -04:00
|
|
|
ts_version: version::TYPESCRIPT.to_string(),
|
2023-04-27 10:05:20 -04:00
|
|
|
unstable: shared.options.unstable,
|
2023-03-23 18:27:58 -04:00
|
|
|
user_agent: version::get_user_agent().to_string(),
|
2023-04-27 10:05:20 -04:00
|
|
|
inspect: shared.options.is_inspecting,
|
2023-08-15 15:36:36 -04:00
|
|
|
has_node_modules_dir: shared.options.has_node_modules_dir,
|
|
|
|
maybe_binary_npm_command_name: shared
|
|
|
|
.options
|
|
|
|
.maybe_binary_npm_command_name
|
|
|
|
.clone(),
|
2022-08-11 16:59:12 -04:00
|
|
|
},
|
|
|
|
extensions,
|
2023-08-05 19:47:15 -04:00
|
|
|
startup_snapshot: crate::js::deno_isolate_init(),
|
2023-04-27 10:05:20 -04:00
|
|
|
unsafely_ignore_certificate_errors: shared
|
2022-08-11 16:59:12 -04:00
|
|
|
.options
|
2023-04-27 10:05:20 -04:00
|
|
|
.unsafely_ignore_certificate_errors
|
2022-12-08 11:50:09 -05:00
|
|
|
.clone(),
|
2023-05-01 16:42:05 -04:00
|
|
|
root_cert_store_provider: Some(shared.root_cert_store_provider.clone()),
|
2023-04-27 10:05:20 -04:00
|
|
|
seed: shared.options.seed,
|
2022-08-11 16:59:12 -04:00
|
|
|
create_web_worker_cb,
|
|
|
|
format_js_error_fn: Some(Arc::new(format_js_error)),
|
2023-05-01 08:59:38 -04:00
|
|
|
source_map_getter: maybe_source_map_getter,
|
2022-08-11 16:59:12 -04:00
|
|
|
module_loader,
|
2023-05-04 14:28:42 -04:00
|
|
|
fs: shared.fs.clone(),
|
2023-09-29 09:26:25 -04:00
|
|
|
npm_resolver: Some(shared.npm_resolver.clone().into_npm_resolver()),
|
2022-08-11 16:59:12 -04:00
|
|
|
worker_type: args.worker_type,
|
|
|
|
maybe_inspector_server,
|
|
|
|
get_error_class_fn: Some(&errors::get_error_class_name),
|
2023-04-27 10:05:20 -04:00
|
|
|
blob_store: shared.blob_store.clone(),
|
|
|
|
broadcast_channel: shared.broadcast_channel.clone(),
|
|
|
|
shared_array_buffer_store: Some(shared.shared_array_buffer_store.clone()),
|
|
|
|
compiled_wasm_module_store: Some(
|
|
|
|
shared.compiled_wasm_module_store.clone(),
|
|
|
|
),
|
2022-08-11 16:59:12 -04:00
|
|
|
stdio: stdio.clone(),
|
2022-09-28 08:11:12 -04:00
|
|
|
cache_storage_dir,
|
2022-08-11 16:59:12 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
WebWorker::bootstrap_from_options(
|
|
|
|
args.name,
|
|
|
|
args.permissions,
|
|
|
|
args.main_module,
|
|
|
|
args.worker_id,
|
|
|
|
options,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
2022-11-21 08:36:26 -05:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2023-03-13 19:31:03 -04:00
|
|
|
use deno_core::resolve_path;
|
2023-01-07 11:25:34 -05:00
|
|
|
use deno_runtime::permissions::Permissions;
|
2022-11-21 08:36:26 -05:00
|
|
|
|
|
|
|
fn create_test_worker() -> MainWorker {
|
2023-03-13 19:31:03 -04:00
|
|
|
let main_module =
|
|
|
|
resolve_path("./hello.js", &std::env::current_dir().unwrap()).unwrap();
|
2023-01-07 11:25:34 -05:00
|
|
|
let permissions = PermissionsContainer::new(Permissions::default());
|
2022-11-21 08:36:26 -05:00
|
|
|
|
|
|
|
let options = WorkerOptions {
|
2023-08-05 19:47:15 -04:00
|
|
|
startup_snapshot: crate::js::deno_isolate_init(),
|
2023-05-04 14:28:42 -04:00
|
|
|
..Default::default()
|
2022-11-21 08:36:26 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
MainWorker::bootstrap_from_options(main_module, permissions, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn execute_mod_esm_imports_a() {
|
|
|
|
let p = test_util::testdata_path().join("runtime/esm_imports_a.js");
|
2023-03-13 21:12:09 -04:00
|
|
|
let module_specifier = ModuleSpecifier::from_file_path(&p).unwrap();
|
2022-11-21 08:36:26 -05:00
|
|
|
let mut worker = create_test_worker();
|
|
|
|
let result = worker.execute_main_module(&module_specifier).await;
|
|
|
|
if let Err(err) = result {
|
2023-01-27 10:43:16 -05:00
|
|
|
eprintln!("execute_mod err {err:?}");
|
2022-11-21 08:36:26 -05:00
|
|
|
}
|
|
|
|
if let Err(e) = worker.run_event_loop(false).await {
|
2023-01-27 10:43:16 -05:00
|
|
|
panic!("Future got unexpected error: {e:?}");
|
2022-11-21 08:36:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn execute_mod_circular() {
|
|
|
|
let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
|
|
|
.join("tests/circular1.js");
|
2023-03-13 19:31:03 -04:00
|
|
|
let module_specifier = ModuleSpecifier::from_file_path(&p).unwrap();
|
2022-11-21 08:36:26 -05:00
|
|
|
let mut worker = create_test_worker();
|
|
|
|
let result = worker.execute_main_module(&module_specifier).await;
|
|
|
|
if let Err(err) = result {
|
2023-01-27 10:43:16 -05:00
|
|
|
eprintln!("execute_mod err {err:?}");
|
2022-11-21 08:36:26 -05:00
|
|
|
}
|
|
|
|
if let Err(e) = worker.run_event_loop(false).await {
|
2023-01-27 10:43:16 -05:00
|
|
|
panic!("Future got unexpected error: {e:?}");
|
2022-11-21 08:36:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn execute_mod_resolve_error() {
|
|
|
|
// "foo" is not a valid module specifier so this should return an error.
|
|
|
|
let mut worker = create_test_worker();
|
2023-03-13 19:31:03 -04:00
|
|
|
let module_specifier =
|
|
|
|
resolve_path("./does-not-exist", &std::env::current_dir().unwrap())
|
|
|
|
.unwrap();
|
2022-11-21 08:36:26 -05:00
|
|
|
let result = worker.execute_main_module(&module_specifier).await;
|
|
|
|
assert!(result.is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn execute_mod_002_hello() {
|
|
|
|
// This assumes cwd is project root (an assumption made throughout the
|
|
|
|
// tests).
|
|
|
|
let mut worker = create_test_worker();
|
|
|
|
let p = test_util::testdata_path().join("run/001_hello.js");
|
2023-03-13 19:31:03 -04:00
|
|
|
let module_specifier = ModuleSpecifier::from_file_path(&p).unwrap();
|
2022-11-21 08:36:26 -05:00
|
|
|
let result = worker.execute_main_module(&module_specifier).await;
|
|
|
|
assert!(result.is_ok());
|
|
|
|
}
|
|
|
|
}
|