2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-01-13 02:51:32 -05:00
|
|
|
|
2023-10-03 19:05:06 -04:00
|
|
|
use std::path::Path;
|
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;
|
2022-08-11 16:59:12 -04:00
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::futures::FutureExt;
|
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;
|
2023-10-12 11:55:50 -04:00
|
|
|
use deno_core::FeatureChecker;
|
2022-08-11 16:59:12 -04:00
|
|
|
use deno_core::ModuleId;
|
2023-05-01 08:59:38 -04:00
|
|
|
use deno_core::ModuleLoader;
|
2023-11-21 21:45:34 -05:00
|
|
|
use deno_core::PollEventLoopOptions;
|
2023-04-27 10:05:20 -04:00
|
|
|
use deno_core::SharedArrayBufferStore;
|
2024-04-17 10:19:55 -04:00
|
|
|
use deno_runtime::code_cache;
|
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;
|
2024-07-25 19:08:14 -04:00
|
|
|
use deno_runtime::deno_node::NodeExtInitServices;
|
2023-04-27 10:05:20 -04:00
|
|
|
use deno_runtime::deno_node::NodeResolver;
|
2024-06-06 23:37:53 -04:00
|
|
|
use deno_runtime::deno_permissions::PermissionsContainer;
|
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;
|
|
|
|
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;
|
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-04-06 18:46:44 -04:00
|
|
|
use deno_semver::npm::NpmPackageReqReference;
|
2024-02-07 11:25:14 -05:00
|
|
|
use deno_terminal::colors;
|
2024-07-25 19:08:14 -04:00
|
|
|
use node_resolver::NodeResolution;
|
|
|
|
use node_resolver::NodeResolutionMode;
|
2023-10-30 20:25:58 -04:00
|
|
|
use tokio::select;
|
2022-08-11 16:59:12 -04:00
|
|
|
|
2024-06-28 20:18:21 -04:00
|
|
|
use crate::args::CliLockfile;
|
2023-11-14 16:06:00 -05:00
|
|
|
use crate::args::DenoSubcommand;
|
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-11-28 17:28:54 -05:00
|
|
|
use crate::util::checksum;
|
2023-10-30 20:25:58 -04:00
|
|
|
use crate::util::file_watcher::WatcherCommunicator;
|
|
|
|
use crate::util::file_watcher::WatcherRestartMode;
|
2022-08-11 16:59:12 -04:00
|
|
|
use crate::version;
|
|
|
|
|
2024-05-16 03:09:35 -04:00
|
|
|
pub struct ModuleLoaderAndSourceMapGetter {
|
|
|
|
pub module_loader: Rc<dyn ModuleLoader>,
|
|
|
|
}
|
|
|
|
|
2023-05-01 08:59:38 -04:00
|
|
|
pub trait ModuleLoaderFactory: Send + Sync {
|
|
|
|
fn create_for_main(
|
|
|
|
&self,
|
|
|
|
root_permissions: PermissionsContainer,
|
|
|
|
dynamic_permissions: PermissionsContainer,
|
2024-05-16 03:09:35 -04:00
|
|
|
) -> ModuleLoaderAndSourceMapGetter;
|
2023-05-01 08:59:38 -04:00
|
|
|
|
|
|
|
fn create_for_worker(
|
|
|
|
&self,
|
|
|
|
root_permissions: PermissionsContainer,
|
|
|
|
dynamic_permissions: PermissionsContainer,
|
2024-05-16 03:09:35 -04:00
|
|
|
) -> ModuleLoaderAndSourceMapGetter;
|
2023-05-01 08:59:38 -04:00
|
|
|
}
|
|
|
|
|
2024-02-13 11:22:30 -05:00
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
pub trait HmrRunner: Send + Sync {
|
|
|
|
async fn start(&mut self) -> Result<(), AnyError>;
|
|
|
|
async fn stop(&mut self) -> Result<(), AnyError>;
|
|
|
|
async fn run(&mut self) -> Result<(), AnyError>;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
pub trait CoverageCollector: Send + Sync {
|
|
|
|
async fn start_collecting(&mut self) -> Result<(), AnyError>;
|
|
|
|
async fn stop_collecting(&mut self) -> Result<(), AnyError>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type CreateHmrRunnerCb = Box<
|
|
|
|
dyn Fn(deno_core::LocalInspectorSession) -> Box<dyn HmrRunner> + Send + Sync,
|
|
|
|
>;
|
|
|
|
|
|
|
|
pub type CreateCoverageCollectorCb = Box<
|
|
|
|
dyn Fn(deno_core::LocalInspectorSession) -> Box<dyn CoverageCollector>
|
|
|
|
+ Send
|
|
|
|
+ Sync,
|
|
|
|
>;
|
|
|
|
|
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-11-05 16:27:36 -05:00
|
|
|
pub enable_op_summary_metrics: bool,
|
2023-04-27 10:05:20 -04:00
|
|
|
pub enable_testing_features: bool,
|
|
|
|
pub has_node_modules_dir: bool,
|
2023-10-30 20:25:58 -04:00
|
|
|
pub hmr: bool,
|
2023-04-27 10:05:20 -04:00
|
|
|
pub inspect_brk: bool,
|
|
|
|
pub inspect_wait: bool,
|
2023-11-10 12:41:24 -05:00
|
|
|
pub strace_ops: Option<Vec<String>>,
|
2023-04-27 10:05:20 -04:00
|
|
|
pub is_inspecting: bool,
|
|
|
|
pub is_npm_main: bool,
|
|
|
|
pub location: Option<Url>,
|
2024-02-23 11:30:29 -05:00
|
|
|
pub argv0: Option<String>,
|
2024-05-05 10:16:02 -04:00
|
|
|
pub node_debug: 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-11-11 12:01:48 -05:00
|
|
|
pub skip_op_registration: bool,
|
2024-02-13 11:22:30 -05:00
|
|
|
pub create_hmr_runner: Option<CreateHmrRunnerCb>,
|
|
|
|
pub create_coverage_collector: Option<CreateCoverageCollectorCb>,
|
2023-04-27 10:05:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct SharedWorkerState {
|
2023-05-01 08:59:38 -04:00
|
|
|
options: CliMainWorkerOptions,
|
2023-11-14 16:06:00 -05:00
|
|
|
subcommand: DenoSubcommand,
|
2023-05-01 08:59:38 -04:00
|
|
|
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-10-30 20:25:58 -04:00
|
|
|
maybe_file_watcher_communicator: Option<Arc<WatcherCommunicator>>,
|
2023-05-01 08:59:38 -04:00
|
|
|
maybe_inspector_server: Option<Arc<InspectorServer>>,
|
2024-06-28 20:18:21 -04:00
|
|
|
maybe_lockfile: Option<Arc<CliLockfile>>,
|
2023-10-12 11:55:50 -04:00
|
|
|
feature_checker: Arc<FeatureChecker>,
|
2023-12-19 07:37:22 -05:00
|
|
|
node_ipc: Option<i64>,
|
2024-02-14 23:50:17 -05:00
|
|
|
enable_future_features: bool,
|
2024-01-18 18:30:49 -05:00
|
|
|
disable_deprecated_api_warning: bool,
|
2024-01-26 10:41:16 -05:00
|
|
|
verbose_deprecated_api_warning: bool,
|
2024-04-17 10:19:55 -04:00
|
|
|
code_cache: Option<Arc<dyn code_cache::CodeCache>>,
|
2024-05-17 01:38:50 -04:00
|
|
|
serve_port: Option<u16>,
|
2024-04-24 15:45:49 -04:00
|
|
|
serve_host: Option<String>,
|
2023-05-01 08:59:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SharedWorkerState {
|
2024-07-25 19:08:14 -04:00
|
|
|
pub fn create_node_init_services(&self) -> NodeExtInitServices {
|
|
|
|
NodeExtInitServices {
|
|
|
|
node_require_resolver: self.npm_resolver.clone().into_require_resolver(),
|
|
|
|
node_resolver: self.node_resolver.clone(),
|
|
|
|
npm_process_state_provider: self
|
|
|
|
.npm_resolver
|
|
|
|
.clone()
|
|
|
|
.into_process_state_provider(),
|
|
|
|
npm_resolver: self.npm_resolver.clone().into_npm_resolver(),
|
|
|
|
}
|
|
|
|
}
|
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?;
|
2023-10-30 20:25:58 -04:00
|
|
|
let mut maybe_hmr_runner = self.maybe_setup_hmr_runner().await?;
|
|
|
|
|
2022-08-11 16:59:12 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-04-15 14:08:33 -04:00
|
|
|
self.worker.dispatch_load_event()?;
|
2022-08-11 16:59:12 -04:00
|
|
|
|
|
|
|
loop {
|
2023-10-30 20:25:58 -04:00
|
|
|
if let Some(hmr_runner) = maybe_hmr_runner.as_mut() {
|
|
|
|
let watcher_communicator =
|
|
|
|
self.shared.maybe_file_watcher_communicator.clone().unwrap();
|
|
|
|
|
|
|
|
let hmr_future = hmr_runner.run().boxed_local();
|
|
|
|
let event_loop_future = self.worker.run_event_loop(false).boxed_local();
|
|
|
|
|
|
|
|
let result;
|
|
|
|
select! {
|
|
|
|
hmr_result = hmr_future => {
|
|
|
|
result = hmr_result;
|
|
|
|
},
|
|
|
|
event_loop_result = event_loop_future => {
|
|
|
|
result = event_loop_result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Err(e) = result {
|
|
|
|
watcher_communicator
|
|
|
|
.change_restart_mode(WatcherRestartMode::Automatic);
|
|
|
|
return Err(e);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self
|
|
|
|
.worker
|
|
|
|
.run_event_loop(maybe_coverage_collector.is_none())
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
|
2024-04-16 09:45:41 -04:00
|
|
|
let web_continue = self.worker.dispatch_beforeunload_event()?;
|
|
|
|
if !web_continue {
|
|
|
|
let node_continue = self.worker.dispatch_process_beforeexit_event()?;
|
|
|
|
if !node_continue {
|
|
|
|
break;
|
|
|
|
}
|
2022-08-11 16:59:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-15 14:08:33 -04:00
|
|
|
self.worker.dispatch_unload_event()?;
|
2024-04-16 09:45:41 -04:00
|
|
|
self.worker.dispatch_process_exit_event()?;
|
2022-08-11 16:59:12 -04:00
|
|
|
|
|
|
|
if let Some(coverage_collector) = maybe_coverage_collector.as_mut() {
|
|
|
|
self
|
|
|
|
.worker
|
2023-11-21 21:45:34 -05:00
|
|
|
.js_runtime
|
2023-12-13 10:07:26 -05:00
|
|
|
.with_event_loop_future(
|
2023-11-21 21:45:34 -05:00
|
|
|
coverage_collector.stop_collecting().boxed_local(),
|
2023-12-13 10:07:26 -05:00
|
|
|
PollEventLoopOptions::default(),
|
2023-11-21 21:45:34 -05:00
|
|
|
)
|
2022-08-11 16:59:12 -04:00
|
|
|
.await?;
|
|
|
|
}
|
2023-10-30 20:25:58 -04:00
|
|
|
if let Some(hmr_runner) = maybe_hmr_runner.as_mut() {
|
|
|
|
self
|
|
|
|
.worker
|
2023-11-21 21:45:34 -05:00
|
|
|
.js_runtime
|
2023-12-13 10:07:26 -05:00
|
|
|
.with_event_loop_future(
|
2023-11-21 21:45:34 -05:00
|
|
|
hmr_runner.stop().boxed_local(),
|
2023-12-13 10:07:26 -05:00
|
|
|
PollEventLoopOptions::default(),
|
2023-11-21 21:45:34 -05:00
|
|
|
)
|
2023-10-30 20:25:58 -04:00
|
|
|
.await?;
|
|
|
|
}
|
2022-08-11 16:59:12 -04:00
|
|
|
|
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> {
|
2024-08-14 09:21:11 -04:00
|
|
|
if self.inner.is_main_cjs {
|
|
|
|
deno_node::load_cjs_module(
|
|
|
|
&mut self.inner.worker.js_runtime,
|
|
|
|
&self
|
|
|
|
.inner
|
|
|
|
.main_module
|
|
|
|
.to_file_path()
|
|
|
|
.unwrap()
|
|
|
|
.to_string_lossy(),
|
|
|
|
true,
|
|
|
|
self.inner.shared.options.inspect_brk,
|
|
|
|
)?;
|
|
|
|
} else {
|
|
|
|
self.inner.execute_main_module_possibly_with_npm().await?;
|
|
|
|
}
|
|
|
|
|
2024-04-15 14:08:33 -04:00
|
|
|
self.inner.worker.dispatch_load_event()?;
|
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),
|
|
|
|
}
|
2024-04-16 09:45:41 -04:00
|
|
|
let web_continue = self.inner.worker.dispatch_beforeunload_event()?;
|
|
|
|
if !web_continue {
|
|
|
|
let node_continue =
|
|
|
|
self.inner.worker.dispatch_process_beforeexit_event()?;
|
|
|
|
if !node_continue {
|
|
|
|
break Ok(());
|
|
|
|
}
|
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
|
|
|
|
2024-04-15 14:08:33 -04:00
|
|
|
self.inner.worker.dispatch_unload_event()?;
|
2024-04-16 09:45:41 -04:00
|
|
|
self.inner.worker.dispatch_process_exit_event()?;
|
2022-08-11 16:59:12 -04:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for FileWatcherModuleExecutor {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if self.pending_unload {
|
2024-04-15 14:08:33 -04:00
|
|
|
let _ = self.inner.worker.dispatch_unload_event();
|
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-10-30 20:25:58 -04:00
|
|
|
pub async fn maybe_setup_hmr_runner(
|
|
|
|
&mut self,
|
2024-02-13 11:22:30 -05:00
|
|
|
) -> Result<Option<Box<dyn HmrRunner>>, AnyError> {
|
2023-10-30 20:25:58 -04:00
|
|
|
if !self.shared.options.hmr {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
2024-02-13 11:22:30 -05:00
|
|
|
let Some(setup_hmr_runner) = self.shared.options.create_hmr_runner.as_ref()
|
|
|
|
else {
|
|
|
|
return Ok(None);
|
|
|
|
};
|
2023-10-30 20:25:58 -04:00
|
|
|
|
2024-03-11 23:48:00 -04:00
|
|
|
let session = self.worker.create_inspector_session();
|
2024-02-13 11:22:30 -05:00
|
|
|
|
|
|
|
let mut hmr_runner = setup_hmr_runner(session);
|
2023-10-30 20:25:58 -04:00
|
|
|
|
|
|
|
self
|
|
|
|
.worker
|
2023-11-21 21:45:34 -05:00
|
|
|
.js_runtime
|
2023-12-13 10:07:26 -05:00
|
|
|
.with_event_loop_future(
|
2023-11-21 21:45:34 -05:00
|
|
|
hmr_runner.start().boxed_local(),
|
2023-12-13 10:07:26 -05:00
|
|
|
PollEventLoopOptions::default(),
|
2023-11-21 21:45:34 -05:00
|
|
|
)
|
2023-10-30 20:25:58 -04:00
|
|
|
.await?;
|
|
|
|
Ok(Some(hmr_runner))
|
|
|
|
}
|
|
|
|
|
2024-02-13 11:22:30 -05:00
|
|
|
pub async fn maybe_setup_coverage_collector(
|
|
|
|
&mut self,
|
|
|
|
) -> Result<Option<Box<dyn CoverageCollector>>, AnyError> {
|
|
|
|
let Some(create_coverage_collector) =
|
|
|
|
self.shared.options.create_coverage_collector.as_ref()
|
|
|
|
else {
|
|
|
|
return Ok(None);
|
|
|
|
};
|
|
|
|
|
2024-03-11 23:48:00 -04:00
|
|
|
let session = self.worker.create_inspector_session();
|
2024-02-13 11:22:30 -05:00
|
|
|
let mut coverage_collector = create_coverage_collector(session);
|
|
|
|
self
|
|
|
|
.worker
|
|
|
|
.js_runtime
|
|
|
|
.with_event_loop_future(
|
|
|
|
coverage_collector.start_collecting().boxed_local(),
|
|
|
|
PollEventLoopOptions::default(),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
Ok(Some(coverage_collector))
|
|
|
|
}
|
|
|
|
|
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> {
|
2024-03-04 20:17:39 -05:00
|
|
|
self.worker.js_runtime.execute_script(name, source_code)
|
2023-09-14 10:38:15 -04:00
|
|
|
}
|
2022-08-11 16:59:12 -04:00
|
|
|
}
|
|
|
|
|
2024-08-14 18:26:21 -04:00
|
|
|
#[derive(Clone)]
|
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-11-14 16:06:00 -05:00
|
|
|
subcommand: DenoSubcommand,
|
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-10-30 20:25:58 -04:00
|
|
|
maybe_file_watcher_communicator: Option<Arc<WatcherCommunicator>>,
|
2023-04-27 10:05:20 -04:00
|
|
|
maybe_inspector_server: Option<Arc<InspectorServer>>,
|
2024-06-28 20:18:21 -04:00
|
|
|
maybe_lockfile: Option<Arc<CliLockfile>>,
|
2023-10-12 11:55:50 -04:00
|
|
|
feature_checker: Arc<FeatureChecker>,
|
2023-04-27 10:05:20 -04:00
|
|
|
options: CliMainWorkerOptions,
|
2023-12-19 07:37:22 -05:00
|
|
|
node_ipc: Option<i64>,
|
2024-05-17 01:38:50 -04:00
|
|
|
serve_port: Option<u16>,
|
2024-04-24 15:45:49 -04:00
|
|
|
serve_host: Option<String>,
|
2024-02-14 23:50:17 -05:00
|
|
|
enable_future_features: bool,
|
2024-01-18 18:30:49 -05:00
|
|
|
disable_deprecated_api_warning: bool,
|
2024-01-26 10:41:16 -05:00
|
|
|
verbose_deprecated_api_warning: bool,
|
2024-04-17 10:19:55 -04:00
|
|
|
code_cache: Option<Arc<dyn code_cache::CodeCache>>,
|
2023-04-27 10:05:20 -04:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
shared: Arc::new(SharedWorkerState {
|
|
|
|
options,
|
2023-11-14 16:06:00 -05:00
|
|
|
subcommand,
|
2023-04-27 10:05:20 -04:00
|
|
|
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-10-30 20:25:58 -04:00
|
|
|
maybe_file_watcher_communicator,
|
2023-04-27 10:05:20 -04:00
|
|
|
maybe_inspector_server,
|
2023-05-22 16:55:04 -04:00
|
|
|
maybe_lockfile,
|
2023-10-12 11:55:50 -04:00
|
|
|
feature_checker,
|
2023-12-13 05:14:16 -05:00
|
|
|
node_ipc,
|
2024-04-24 15:45:49 -04:00
|
|
|
serve_port,
|
|
|
|
serve_host,
|
2024-02-14 23:50:17 -05:00
|
|
|
enable_future_features,
|
2024-01-18 18:30:49 -05:00
|
|
|
disable_deprecated_api_warning,
|
2024-01-26 10:41:16 -05:00
|
|
|
verbose_deprecated_api_warning,
|
2024-04-17 10:19:55 -04:00
|
|
|
code_cache,
|
2023-04-27 10:05:20 -04:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn create_main_worker(
|
|
|
|
&self,
|
2024-04-24 15:45:49 -04:00
|
|
|
mode: WorkerExecutionMode,
|
2023-04-27 10:05:20 -04:00
|
|
|
main_module: ModuleSpecifier,
|
|
|
|
permissions: PermissionsContainer,
|
|
|
|
) -> Result<CliMainWorker, AnyError> {
|
|
|
|
self
|
|
|
|
.create_custom_worker(
|
2024-04-24 15:45:49 -04:00
|
|
|
mode,
|
2023-04-27 10:05:20 -04:00
|
|
|
main_module,
|
|
|
|
permissions,
|
|
|
|
vec![],
|
|
|
|
Default::default(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn create_custom_worker(
|
|
|
|
&self,
|
2024-04-24 15:45:49 -04:00
|
|
|
mode: WorkerExecutionMode,
|
2023-04-27 10:05:20 -04:00
|
|
|
main_module: ModuleSpecifier,
|
|
|
|
permissions: PermissionsContainer,
|
2023-12-02 18:40:27 -05:00
|
|
|
custom_extensions: Vec<Extension>,
|
2023-04-27 10:05:20 -04:00
|
|
|
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-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-10-03 19:05:06 -04:00
|
|
|
|
|
|
|
// use a fake referrer that can be used to discover the package.json if necessary
|
|
|
|
let referrer =
|
|
|
|
ModuleSpecifier::from_directory_path(self.shared.fs.cwd()?)
|
|
|
|
.unwrap()
|
|
|
|
.join("package.json")?;
|
|
|
|
let package_folder = shared
|
2023-09-28 16:43:45 -04:00
|
|
|
.npm_resolver
|
2023-10-03 19:05:06 -04:00
|
|
|
.resolve_pkg_folder_from_deno_module_req(
|
|
|
|
package_ref.req(),
|
|
|
|
&referrer,
|
|
|
|
)?;
|
2024-06-06 23:37:53 -04:00
|
|
|
let node_resolution = self
|
|
|
|
.resolve_binary_entrypoint(&package_folder, package_ref.sub_path())?;
|
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
|
2024-06-28 20:18:21 -04:00
|
|
|
lockfile.write_if_changed()?;
|
2023-05-22 16:55:04 -04:00
|
|
|
}
|
|
|
|
|
2023-04-27 10:05:20 -04:00
|
|
|
(node_resolution.into_url(), is_main_cjs)
|
2023-11-16 17:29:35 -05:00
|
|
|
} else if shared.options.is_npm_main
|
|
|
|
|| shared.node_resolver.in_npm_package(&main_module)
|
|
|
|
{
|
2023-04-27 10:05:20 -04:00
|
|
|
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)
|
|
|
|
};
|
|
|
|
|
2024-07-23 20:11:38 -04:00
|
|
|
let ModuleLoaderAndSourceMapGetter { module_loader } = shared
|
2023-05-01 08:59:38 -04:00
|
|
|
.module_loader_factory
|
|
|
|
.create_for_main(PermissionsContainer::allow_all(), permissions.clone());
|
2023-04-27 10:05:20 -04:00
|
|
|
let maybe_inspector_server = shared.maybe_inspector_server.clone();
|
|
|
|
|
|
|
|
let create_web_worker_cb =
|
2024-08-14 18:26:21 -04:00
|
|
|
create_web_worker_callback(shared.clone(), stdio.clone());
|
2023-04-27 10:05:20 -04:00
|
|
|
|
|
|
|
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()]))
|
|
|
|
});
|
|
|
|
|
2023-11-01 18:15:08 -04:00
|
|
|
// TODO(bartlomieju): this is cruft, update FeatureChecker to spit out
|
|
|
|
// list of enabled features.
|
|
|
|
let feature_checker = shared.feature_checker.clone();
|
2023-11-25 11:41:21 -05:00
|
|
|
let mut unstable_features =
|
|
|
|
Vec::with_capacity(crate::UNSTABLE_GRANULAR_FLAGS.len());
|
2023-11-01 18:15:08 -04:00
|
|
|
for (feature_name, _, id) in crate::UNSTABLE_GRANULAR_FLAGS {
|
|
|
|
if feature_checker.check(feature_name) {
|
|
|
|
unstable_features.push(*id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-27 10:05:20 -04:00
|
|
|
let options = WorkerOptions {
|
|
|
|
bootstrap: BootstrapOptions {
|
2024-08-15 17:47:16 -04:00
|
|
|
deno_version: crate::version::DENO_VERSION_INFO.deno.to_string(),
|
2023-04-27 10:05:20 -04:00
|
|
|
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-11-05 16:27:36 -05:00
|
|
|
enable_op_summary_metrics: shared.options.enable_op_summary_metrics,
|
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(),
|
2024-05-14 17:32:09 -04:00
|
|
|
is_stdout_tty: deno_terminal::is_stdout_tty(),
|
|
|
|
is_stderr_tty: deno_terminal::is_stderr_tty(),
|
2024-07-19 06:39:05 -04:00
|
|
|
color_level: colors::get_color_level(),
|
2023-04-27 10:05:20 -04:00
|
|
|
unstable: shared.options.unstable,
|
2023-11-01 18:15:08 -04:00
|
|
|
unstable_features,
|
2024-08-15 17:47:16 -04:00
|
|
|
user_agent: version::DENO_VERSION_INFO.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,
|
2024-02-23 11:30:29 -05:00
|
|
|
argv0: shared.options.argv0.clone(),
|
2024-05-05 10:16:02 -04:00
|
|
|
node_debug: shared.options.node_debug.clone(),
|
2023-12-13 05:14:16 -05:00
|
|
|
node_ipc_fd: shared.node_ipc,
|
2024-01-18 18:30:49 -05:00
|
|
|
disable_deprecated_api_warning: shared.disable_deprecated_api_warning,
|
2024-01-26 10:41:16 -05:00
|
|
|
verbose_deprecated_api_warning: shared.verbose_deprecated_api_warning,
|
2024-02-14 23:50:17 -05:00
|
|
|
future: shared.enable_future_features,
|
2024-04-24 15:45:49 -04:00
|
|
|
mode,
|
|
|
|
serve_port: shared.serve_port,
|
|
|
|
serve_host: shared.serve_host.clone(),
|
2023-04-27 10:05:20 -04:00
|
|
|
},
|
2023-12-02 18:40:27 -05:00
|
|
|
extensions: custom_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,
|
|
|
|
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,
|
2023-11-10 12:41:24 -05:00
|
|
|
strace_ops: shared.options.strace_ops.clone(),
|
2023-04-27 10:05:20 -04:00
|
|
|
module_loader,
|
2023-05-04 14:28:42 -04:00
|
|
|
fs: shared.fs.clone(),
|
2024-07-25 19:08:14 -04:00
|
|
|
node_services: Some(shared.create_node_init_services()),
|
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,
|
2023-11-01 18:15:08 -04:00
|
|
|
feature_checker,
|
2023-11-11 12:01:48 -05:00
|
|
|
skip_op_registration: shared.options.skip_op_registration,
|
2024-04-17 10:19:55 -04:00
|
|
|
v8_code_cache: shared.code_cache.clone(),
|
2023-04-27 10:05:20 -04:00
|
|
|
};
|
|
|
|
|
2023-11-14 16:06:00 -05:00
|
|
|
let mut worker = MainWorker::bootstrap_from_options(
|
2023-04-27 10:05:20 -04:00
|
|
|
main_module.clone(),
|
|
|
|
permissions,
|
|
|
|
options,
|
|
|
|
);
|
|
|
|
|
2023-11-24 22:46:16 -05:00
|
|
|
if self.shared.subcommand.needs_test() {
|
2024-01-25 14:54:35 -05:00
|
|
|
macro_rules! test_file {
|
|
|
|
($($file:literal),*) => {
|
2024-03-04 20:17:39 -05:00
|
|
|
$(worker.js_runtime.lazy_load_es_module_with_code(
|
2024-01-25 14:54:35 -05:00
|
|
|
concat!("ext:cli/", $file),
|
2024-03-04 20:17:39 -05:00
|
|
|
deno_core::ascii_str_include!(concat!("js/", $file)),
|
2024-01-25 14:54:35 -05:00
|
|
|
)?;)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
test_file!(
|
|
|
|
"40_test_common.js",
|
|
|
|
"40_test.js",
|
|
|
|
"40_bench.js",
|
|
|
|
"40_jupyter.js"
|
|
|
|
);
|
2023-11-14 16:06:00 -05:00
|
|
|
}
|
|
|
|
|
2023-04-27 10:05:20 -04:00
|
|
|
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-10-03 19:05:06 -04:00
|
|
|
package_folder: &Path,
|
|
|
|
sub_path: Option<&str>,
|
2023-08-01 18:47:57 -04:00
|
|
|
) -> Result<NodeResolution, AnyError> {
|
2023-09-28 16:43:45 -04:00
|
|
|
match self
|
|
|
|
.shared
|
|
|
|
.node_resolver
|
2023-10-03 19:05:06 -04:00
|
|
|
.resolve_binary_export(package_folder, sub_path)
|
2023-09-28 16:43:45 -04:00
|
|
|
{
|
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
|
2024-06-06 23:37:53 -04:00
|
|
|
let result =
|
|
|
|
self.resolve_binary_entrypoint_fallback(package_folder, sub_path);
|
2023-08-01 18:47:57 -04:00
|
|
|
match result {
|
|
|
|
Ok(Some(resolution)) => Ok(resolution),
|
2024-07-09 12:15:03 -04:00
|
|
|
Ok(None) => Err(original_err.into()),
|
2023-08-01 18:47:57 -04:00
|
|
|
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-10-03 19:05:06 -04:00
|
|
|
package_folder: &Path,
|
|
|
|
sub_path: Option<&str>,
|
2023-08-01 18:47:57 -04:00
|
|
|
) -> Result<Option<NodeResolution>, AnyError> {
|
|
|
|
// only fallback if the user specified a sub path
|
2023-10-03 19:05:06 -04:00
|
|
|
if 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);
|
|
|
|
}
|
|
|
|
|
2024-07-23 20:22:24 -04:00
|
|
|
let resolution = self
|
2023-10-05 16:18:29 -04:00
|
|
|
.shared
|
|
|
|
.node_resolver
|
|
|
|
.resolve_package_subpath_from_deno_module(
|
|
|
|
package_folder,
|
|
|
|
sub_path,
|
2024-07-09 12:15:03 -04:00
|
|
|
/* referrer */ None,
|
2023-10-05 16:18:29 -04:00
|
|
|
NodeResolutionMode::Execution,
|
2024-07-23 20:22:24 -04:00
|
|
|
)?;
|
2023-08-01 18:47:57 -04:00
|
|
|
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
|
|
|
|
2024-07-23 20:11:38 -04:00
|
|
|
let ModuleLoaderAndSourceMapGetter { module_loader } =
|
|
|
|
shared.module_loader_factory.create_for_worker(
|
|
|
|
args.parent_permissions.clone(),
|
|
|
|
args.permissions.clone(),
|
|
|
|
);
|
2022-08-11 16:59:12 -04:00
|
|
|
let create_web_worker_cb =
|
2024-08-14 18:26:21 -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 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()]))
|
|
|
|
});
|
|
|
|
|
2023-11-01 18:15:08 -04:00
|
|
|
// TODO(bartlomieju): this is cruft, update FeatureChecker to spit out
|
|
|
|
// list of enabled features.
|
|
|
|
let feature_checker = shared.feature_checker.clone();
|
2023-11-25 11:41:21 -05:00
|
|
|
let mut unstable_features =
|
|
|
|
Vec::with_capacity(crate::UNSTABLE_GRANULAR_FLAGS.len());
|
2023-11-01 18:15:08 -04:00
|
|
|
for (feature_name, _, id) in crate::UNSTABLE_GRANULAR_FLAGS {
|
|
|
|
if feature_checker.check(feature_name) {
|
|
|
|
unstable_features.push(*id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-11 16:59:12 -04:00
|
|
|
let options = WebWorkerOptions {
|
|
|
|
bootstrap: BootstrapOptions {
|
2024-08-15 17:47:16 -04:00
|
|
|
deno_version: crate::version::DENO_VERSION_INFO.deno.to_string(),
|
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-11-05 16:27:36 -05:00
|
|
|
enable_op_summary_metrics: shared.options.enable_op_summary_metrics,
|
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(),
|
2024-07-19 06:39:05 -04:00
|
|
|
color_level: colors::get_color_level(),
|
2024-05-14 17:32:09 -04:00
|
|
|
is_stdout_tty: deno_terminal::is_stdout_tty(),
|
|
|
|
is_stderr_tty: deno_terminal::is_stderr_tty(),
|
2023-04-27 10:05:20 -04:00
|
|
|
unstable: shared.options.unstable,
|
2023-11-01 18:15:08 -04:00
|
|
|
unstable_features,
|
2024-08-15 17:47:16 -04:00
|
|
|
user_agent: version::DENO_VERSION_INFO.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,
|
2024-02-23 11:30:29 -05:00
|
|
|
argv0: shared.options.argv0.clone(),
|
2024-05-05 10:16:02 -04:00
|
|
|
node_debug: shared.options.node_debug.clone(),
|
2023-12-13 05:14:16 -05:00
|
|
|
node_ipc_fd: None,
|
2024-01-18 18:30:49 -05:00
|
|
|
disable_deprecated_api_warning: shared.disable_deprecated_api_warning,
|
2024-01-26 10:41:16 -05:00
|
|
|
verbose_deprecated_api_warning: shared.verbose_deprecated_api_warning,
|
2024-04-04 12:27:18 -04:00
|
|
|
future: shared.enable_future_features,
|
2024-08-14 18:26:21 -04:00
|
|
|
mode: WorkerExecutionMode::Worker,
|
2024-04-24 15:45:49 -04:00
|
|
|
serve_port: shared.serve_port,
|
|
|
|
serve_host: shared.serve_host.clone(),
|
2022-08-11 16:59:12 -04:00
|
|
|
},
|
2023-12-02 18:40:27 -05:00
|
|
|
extensions: vec![],
|
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)),
|
|
|
|
module_loader,
|
2023-05-04 14:28:42 -04:00
|
|
|
fs: shared.fs.clone(),
|
2024-07-25 19:08:14 -04:00
|
|
|
node_services: Some(shared.create_node_init_services()),
|
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,
|
2023-11-01 18:15:08 -04:00
|
|
|
feature_checker,
|
2024-03-13 13:22:25 -04:00
|
|
|
strace_ops: shared.options.strace_ops.clone(),
|
|
|
|
close_on_idle: args.close_on_idle,
|
2024-03-11 18:18:03 -04:00
|
|
|
maybe_worker_metadata: args.maybe_worker_metadata,
|
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
|
|
|
|
2024-05-08 22:45:06 -04:00
|
|
|
#[allow(clippy::print_stdout)]
|
|
|
|
#[allow(clippy::print_stderr)]
|
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;
|
2024-06-06 23:37:53 -04:00
|
|
|
use deno_runtime::deno_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();
|
2024-03-13 16:30:48 -04:00
|
|
|
let permissions =
|
|
|
|
PermissionsContainer::new(Permissions::none_without_prompt());
|
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());
|
|
|
|
}
|
|
|
|
}
|