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;
|
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;
|
2024-07-25 19:08:14 -04:00
|
|
|
use deno_runtime::deno_node::NodeExtInitServices;
|
2024-11-01 12:27:00 -04:00
|
|
|
use deno_runtime::deno_node::NodeRequireLoader;
|
|
|
|
use deno_runtime::deno_node::NodeRequireLoaderRc;
|
2023-04-27 10:05:20 -04:00
|
|
|
use deno_runtime::deno_node::NodeResolver;
|
2024-11-01 12:27:00 -04:00
|
|
|
use deno_runtime::deno_node::PackageJsonResolver;
|
2024-06-06 23:37:53 -04:00
|
|
|
use deno_runtime::deno_permissions::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;
|
2024-09-27 15:35:37 -04:00
|
|
|
use deno_runtime::ops::process::NpmProcessStateProviderRc;
|
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;
|
2024-09-29 20:07:50 -04:00
|
|
|
use deno_runtime::web_worker::WebWorkerServiceOptions;
|
2022-08-11 16:59:12 -04:00
|
|
|
use deno_runtime::worker::MainWorker;
|
|
|
|
use deno_runtime::worker::WorkerOptions;
|
2024-09-29 20:07:50 -04:00
|
|
|
use deno_runtime::worker::WorkerServiceOptions;
|
2022-08-11 16:59:12 -04:00
|
|
|
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-11-26 06:22:18 -05:00
|
|
|
use deno_telemetry::OtelConfig;
|
2024-02-07 11:25:14 -05:00
|
|
|
use deno_terminal::colors;
|
2024-11-13 10:10:09 -05:00
|
|
|
use node_resolver::NodeModuleKind;
|
2024-07-25 19:08:14 -04:00
|
|
|
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-11-01 12:27:00 -04:00
|
|
|
pub struct CreateModuleLoaderResult {
|
2024-05-16 03:09:35 -04:00
|
|
|
pub module_loader: Rc<dyn ModuleLoader>,
|
2024-11-01 12:27:00 -04:00
|
|
|
pub node_require_loader: Rc<dyn NodeRequireLoader>,
|
2024-05-16 03:09:35 -04:00
|
|
|
}
|
|
|
|
|
2023-05-01 08:59:38 -04:00
|
|
|
pub trait ModuleLoaderFactory: Send + Sync {
|
|
|
|
fn create_for_main(
|
|
|
|
&self,
|
|
|
|
root_permissions: PermissionsContainer,
|
2024-11-01 12:27:00 -04:00
|
|
|
) -> CreateModuleLoaderResult;
|
2023-05-01 08:59:38 -04:00
|
|
|
|
|
|
|
fn create_for_worker(
|
|
|
|
&self,
|
2024-09-25 21:50:54 -04:00
|
|
|
parent_permissions: PermissionsContainer,
|
|
|
|
permissions: PermissionsContainer,
|
2024-11-01 12:27:00 -04:00
|
|
|
) -> CreateModuleLoaderResult;
|
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>;
|
|
|
|
}
|
|
|
|
|
2024-11-18 15:09:28 -05:00
|
|
|
pub trait CliCodeCache: code_cache::CodeCache {
|
|
|
|
/// Gets if the code cache is still enabled.
|
|
|
|
fn enabled(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_code_cache(self: Arc<Self>) -> Arc<dyn code_cache::CodeCache>;
|
|
|
|
}
|
|
|
|
|
2024-02-13 11:22:30 -05:00
|
|
|
#[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 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>>,
|
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>,
|
2024-08-30 17:58:24 -04:00
|
|
|
pub node_ipc: Option<i64>,
|
|
|
|
pub serve_port: Option<u16>,
|
|
|
|
pub serve_host: Option<String>,
|
2023-04-27 10:05:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct SharedWorkerState {
|
2023-07-01 18:52:30 -04:00
|
|
|
blob_store: Arc<BlobStore>,
|
2023-05-01 08:59:38 -04:00
|
|
|
broadcast_channel: InMemoryBroadcastChannel,
|
2024-11-18 15:09:28 -05:00
|
|
|
code_cache: Option<Arc<dyn CliCodeCache>>,
|
2023-05-01 08:59:38 -04:00
|
|
|
compiled_wasm_module_store: CompiledWasmModuleStore,
|
2024-09-16 16:39:37 -04:00
|
|
|
feature_checker: Arc<FeatureChecker>,
|
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>>,
|
2024-09-16 16:39:37 -04:00
|
|
|
module_loader_factory: Box<dyn ModuleLoaderFactory>,
|
|
|
|
node_resolver: Arc<NodeResolver>,
|
|
|
|
npm_resolver: Arc<dyn CliNpmResolver>,
|
2024-11-01 12:27:00 -04:00
|
|
|
pkg_json_resolver: Arc<PackageJsonResolver>,
|
2024-09-16 16:39:37 -04:00
|
|
|
root_cert_store_provider: Arc<dyn RootCertStoreProvider>,
|
2024-09-25 21:50:54 -04:00
|
|
|
root_permissions: PermissionsContainer,
|
2024-09-16 16:39:37 -04:00
|
|
|
shared_array_buffer_store: SharedArrayBufferStore,
|
|
|
|
storage_key_resolver: StorageKeyResolver,
|
|
|
|
options: CliMainWorkerOptions,
|
|
|
|
subcommand: DenoSubcommand,
|
2024-11-13 05:38:46 -05:00
|
|
|
otel_config: Option<OtelConfig>, // `None` means OpenTelemetry is disabled.
|
2023-05-01 08:59:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SharedWorkerState {
|
2024-11-01 12:27:00 -04:00
|
|
|
pub fn create_node_init_services(
|
|
|
|
&self,
|
|
|
|
node_require_loader: NodeRequireLoaderRc,
|
|
|
|
) -> NodeExtInitServices {
|
2024-07-25 19:08:14 -04:00
|
|
|
NodeExtInitServices {
|
2024-11-01 12:27:00 -04:00
|
|
|
node_require_loader,
|
2024-07-25 19:08:14 -04:00
|
|
|
node_resolver: self.node_resolver.clone(),
|
2024-11-14 15:24:25 -05:00
|
|
|
npm_resolver: self.npm_resolver.clone().into_npm_pkg_folder_resolver(),
|
2024-11-01 12:27:00 -04:00
|
|
|
pkg_json_resolver: self.pkg_json_resolver.clone(),
|
2024-07-25 19:08:14 -04:00
|
|
|
}
|
|
|
|
}
|
2024-09-27 15:35:37 -04:00
|
|
|
|
|
|
|
pub fn npm_process_state_provider(&self) -> NpmProcessStateProviderRc {
|
|
|
|
self.npm_resolver.clone().into_process_state_provider()
|
|
|
|
}
|
2023-04-27 10:05:20 -04:00
|
|
|
}
|
|
|
|
|
2022-08-11 16:59:12 -04:00
|
|
|
pub struct CliMainWorker {
|
|
|
|
main_module: ModuleSpecifier,
|
|
|
|
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);
|
|
|
|
|
2024-11-01 12:27:00 -04:00
|
|
|
self.execute_main_module().await?;
|
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-11-01 12:27:00 -04:00
|
|
|
self.inner.execute_main_module().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
|
|
|
}
|
|
|
|
|
2024-11-01 12:27:00 -04:00
|
|
|
pub async fn execute_main_module(&mut self) -> Result<(), AnyError> {
|
2022-08-20 11:31:33 -04:00
|
|
|
let id = self.worker.preload_main_module(&self.main_module).await?;
|
2024-11-01 12:27:00 -04:00
|
|
|
self.worker.evaluate_module(id).await
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
|
|
|
|
2024-11-01 12:27:00 -04:00
|
|
|
pub async fn execute_side_module(&mut self) -> Result<(), AnyError> {
|
2022-08-20 11:31:33 -04:00
|
|
|
let id = self.worker.preload_side_module(&self.main_module).await?;
|
|
|
|
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-11-19 20:04:48 -05:00
|
|
|
// TODO(bartlomieju): this should be moved to some other place, added to avoid string
|
|
|
|
// duplication between worker setups and `deno info` output.
|
|
|
|
pub fn get_cache_storage_dir() -> PathBuf {
|
|
|
|
// Note: we currently use temp_dir() to avoid managing storage size.
|
|
|
|
std::env::temp_dir().join("deno_cache")
|
|
|
|
}
|
|
|
|
|
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(
|
2023-07-01 18:52:30 -04:00
|
|
|
blob_store: Arc<BlobStore>,
|
2024-11-18 15:09:28 -05:00
|
|
|
code_cache: Option<Arc<dyn CliCodeCache>>,
|
2024-09-16 16:39:37 -04:00
|
|
|
feature_checker: Arc<FeatureChecker>,
|
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>>,
|
2024-09-16 16:39:37 -04:00
|
|
|
module_loader_factory: Box<dyn ModuleLoaderFactory>,
|
|
|
|
node_resolver: Arc<NodeResolver>,
|
|
|
|
npm_resolver: Arc<dyn CliNpmResolver>,
|
2024-11-01 12:27:00 -04:00
|
|
|
pkg_json_resolver: Arc<PackageJsonResolver>,
|
2024-09-16 16:39:37 -04:00
|
|
|
root_cert_store_provider: Arc<dyn RootCertStoreProvider>,
|
2024-09-25 21:50:54 -04:00
|
|
|
root_permissions: PermissionsContainer,
|
2024-09-16 16:39:37 -04:00
|
|
|
storage_key_resolver: StorageKeyResolver,
|
|
|
|
subcommand: DenoSubcommand,
|
2024-08-30 17:58:24 -04:00
|
|
|
options: CliMainWorkerOptions,
|
2024-11-13 05:38:46 -05:00
|
|
|
otel_config: Option<OtelConfig>,
|
2023-04-27 10:05:20 -04:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
shared: Arc::new(SharedWorkerState {
|
|
|
|
blob_store,
|
|
|
|
broadcast_channel: Default::default(),
|
2024-09-16 16:39:37 -04:00
|
|
|
code_cache,
|
2023-04-27 10:05:20 -04:00
|
|
|
compiled_wasm_module_store: Default::default(),
|
2024-09-16 16:39:37 -04:00
|
|
|
feature_checker,
|
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,
|
2024-09-16 16:39:37 -04:00
|
|
|
module_loader_factory,
|
|
|
|
node_resolver,
|
|
|
|
npm_resolver,
|
2024-11-01 12:27:00 -04:00
|
|
|
pkg_json_resolver,
|
2024-09-16 16:39:37 -04:00
|
|
|
root_cert_store_provider,
|
2024-09-25 21:50:54 -04:00
|
|
|
root_permissions,
|
2024-09-16 16:39:37 -04:00
|
|
|
shared_array_buffer_store: Default::default(),
|
|
|
|
storage_key_resolver,
|
|
|
|
options,
|
|
|
|
subcommand,
|
2024-11-13 05:38:46 -05:00
|
|
|
otel_config,
|
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,
|
|
|
|
) -> 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,
|
2024-09-25 21:50:54 -04:00
|
|
|
self.shared.root_permissions.clone(),
|
2023-04-27 10:05:20 -04:00
|
|
|
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;
|
2024-11-01 12:27:00 -04:00
|
|
|
let CreateModuleLoaderResult {
|
|
|
|
module_loader,
|
|
|
|
node_require_loader,
|
|
|
|
} = shared
|
2024-10-14 20:48:39 -04:00
|
|
|
.module_loader_factory
|
|
|
|
.create_for_main(permissions.clone());
|
2024-11-01 12:27:00 -04:00
|
|
|
let main_module = if let Ok(package_ref) =
|
2023-04-27 10:05:20 -04:00
|
|
|
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-11-01 12:27:00 -04:00
|
|
|
let main_module = self
|
2024-06-06 23:37:53 -04:00
|
|
|
.resolve_binary_entrypoint(&package_folder, package_ref.sub_path())?;
|
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
|
|
|
}
|
|
|
|
|
2024-11-01 12:27:00 -04:00
|
|
|
main_module
|
2023-04-27 10:05:20 -04:00
|
|
|
} else {
|
2024-11-01 12:27:00 -04:00
|
|
|
main_module
|
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
|
2024-11-19 20:04:48 -05:00
|
|
|
get_cache_storage_dir().join(checksum::gen(&[key.as_bytes()]))
|
2023-04-27 10:05:20 -04:00
|
|
|
});
|
|
|
|
|
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());
|
2024-08-27 07:45:27 -04:00
|
|
|
for granular_flag in crate::UNSTABLE_GRANULAR_FLAGS {
|
|
|
|
if feature_checker.check(granular_flag.name) {
|
|
|
|
unstable_features.push(granular_flag.id);
|
2023-11-01 18:15:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-29 20:07:50 -04:00
|
|
|
let services = WorkerServiceOptions {
|
|
|
|
root_cert_store_provider: Some(shared.root_cert_store_provider.clone()),
|
|
|
|
module_loader,
|
|
|
|
fs: shared.fs.clone(),
|
2024-11-01 12:27:00 -04:00
|
|
|
node_services: Some(
|
|
|
|
shared.create_node_init_services(node_require_loader),
|
|
|
|
),
|
2024-09-29 20:07:50 -04:00
|
|
|
npm_process_state_provider: Some(shared.npm_process_state_provider()),
|
|
|
|
blob_store: shared.blob_store.clone(),
|
|
|
|
broadcast_channel: shared.broadcast_channel.clone(),
|
2024-11-15 05:44:11 -05:00
|
|
|
fetch_dns_resolver: Default::default(),
|
2024-09-29 20:07:50 -04:00
|
|
|
shared_array_buffer_store: Some(shared.shared_array_buffer_store.clone()),
|
|
|
|
compiled_wasm_module_store: Some(
|
|
|
|
shared.compiled_wasm_module_store.clone(),
|
|
|
|
),
|
|
|
|
feature_checker,
|
|
|
|
permissions,
|
2024-11-18 15:09:28 -05:00
|
|
|
v8_code_cache: shared.code_cache.clone().map(|c| c.as_code_cache()),
|
2024-09-29 20:07:50 -04:00
|
|
|
};
|
2024-11-15 04:33:03 -05:00
|
|
|
|
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-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(),
|
2024-08-30 17:58:24 -04:00
|
|
|
node_ipc_fd: shared.options.node_ipc,
|
2024-04-24 15:45:49 -04:00
|
|
|
mode,
|
2024-08-30 17:58:24 -04:00
|
|
|
serve_port: shared.options.serve_port,
|
|
|
|
serve_host: shared.options.serve_host.clone(),
|
2024-11-13 05:38:46 -05:00
|
|
|
otel_config: shared.otel_config.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(),
|
2024-11-15 04:33:03 -05:00
|
|
|
create_params: create_isolate_create_params(),
|
2023-04-27 10:05:20 -04:00
|
|
|
unsafely_ignore_certificate_errors: shared
|
|
|
|
.options
|
|
|
|
.unsafely_ignore_certificate_errors
|
|
|
|
.clone(),
|
|
|
|
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
|
|
|
get_error_class_fn: Some(&errors::get_error_class_name),
|
|
|
|
cache_storage_dir,
|
|
|
|
origin_storage_dir,
|
|
|
|
stdio,
|
2023-11-11 12:01:48 -05:00
|
|
|
skip_op_registration: shared.options.skip_op_registration,
|
2024-11-20 16:24:04 -05:00
|
|
|
enable_stack_trace_arg_in_ops: crate::args::has_trace_permissions_enabled(
|
|
|
|
),
|
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(),
|
2024-09-29 20:07:50 -04:00
|
|
|
services,
|
2023-04-27 10:05:20 -04:00
|
|
|
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,
|
|
|
|
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>,
|
2024-11-01 12:27:00 -04:00
|
|
|
) -> Result<ModuleSpecifier, 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
|
|
|
{
|
2024-11-01 12:27:00 -04:00
|
|
|
Ok(specifier) => Ok(specifier),
|
2023-08-01 18:47:57 -04:00
|
|
|
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 {
|
2024-11-01 12:27:00 -04:00
|
|
|
Ok(Some(specifier)) => Ok(specifier),
|
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>,
|
2024-11-01 12:27:00 -04:00
|
|
|
) -> Result<Option<ModuleSpecifier>, AnyError> {
|
2023-08-01 18:47:57 -04:00
|
|
|
// 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-11-01 12:27:00 -04:00
|
|
|
let specifier = 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,
|
2024-11-13 10:10:09 -05:00
|
|
|
NodeModuleKind::Esm,
|
2023-10-05 16:18:29 -04:00
|
|
|
NodeResolutionMode::Execution,
|
2024-07-23 20:22:24 -04:00
|
|
|
)?;
|
2024-11-01 12:27:00 -04:00
|
|
|
if specifier
|
|
|
|
.to_file_path()
|
|
|
|
.map(|p| p.exists())
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
|
|
|
Ok(Some(specifier))
|
|
|
|
} else {
|
|
|
|
bail!("Cannot find module '{}'", specifier)
|
2023-08-01 18:47:57 -04:00
|
|
|
}
|
|
|
|
}
|
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-11-01 12:27:00 -04:00
|
|
|
let CreateModuleLoaderResult {
|
|
|
|
module_loader,
|
|
|
|
node_require_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
|
2024-11-19 20:04:48 -05:00
|
|
|
get_cache_storage_dir().join(checksum::gen(&[key.as_bytes()]))
|
2022-09-28 08:11:12 -04:00
|
|
|
});
|
|
|
|
|
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());
|
2024-08-27 07:45:27 -04:00
|
|
|
for granular_flag in crate::UNSTABLE_GRANULAR_FLAGS {
|
|
|
|
if feature_checker.check(granular_flag.name) {
|
|
|
|
unstable_features.push(granular_flag.id);
|
2023-11-01 18:15:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-29 20:07:50 -04:00
|
|
|
let services = WebWorkerServiceOptions {
|
|
|
|
root_cert_store_provider: Some(shared.root_cert_store_provider.clone()),
|
|
|
|
module_loader,
|
|
|
|
fs: shared.fs.clone(),
|
2024-11-01 12:27:00 -04:00
|
|
|
node_services: Some(
|
|
|
|
shared.create_node_init_services(node_require_loader),
|
|
|
|
),
|
2024-09-29 20:07:50 -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(),
|
|
|
|
),
|
|
|
|
maybe_inspector_server,
|
|
|
|
feature_checker,
|
|
|
|
npm_process_state_provider: Some(shared.npm_process_state_provider()),
|
|
|
|
permissions: args.permissions,
|
|
|
|
};
|
2022-08-11 16:59:12 -04:00
|
|
|
let options = WebWorkerOptions {
|
2024-09-29 20:07:50 -04:00
|
|
|
name: args.name,
|
|
|
|
main_module: args.main_module.clone(),
|
|
|
|
worker_id: args.worker_id,
|
2022-08-11 16:59:12 -04:00
|
|
|
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(),
|
2024-09-29 20:07:50 -04:00
|
|
|
location: Some(args.main_module),
|
2022-08-11 16:59:12 -04:00
|
|
|
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-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-08-14 18:26:21 -04:00
|
|
|
mode: WorkerExecutionMode::Worker,
|
2024-08-30 17:58:24 -04:00
|
|
|
serve_port: shared.options.serve_port,
|
|
|
|
serve_host: shared.options.serve_host.clone(),
|
2024-11-13 05:38:46 -05:00
|
|
|
otel_config: shared.otel_config.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(),
|
2024-11-15 04:33:03 -05:00
|
|
|
create_params: create_isolate_create_params(),
|
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-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)),
|
|
|
|
worker_type: args.worker_type,
|
|
|
|
get_error_class_fn: Some(&errors::get_error_class_name),
|
|
|
|
stdio: stdio.clone(),
|
2022-09-28 08:11:12 -04:00
|
|
|
cache_storage_dir,
|
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,
|
2024-11-20 16:24:04 -05:00
|
|
|
enable_stack_trace_arg_in_ops: crate::args::has_trace_permissions_enabled(
|
|
|
|
),
|
2022-08-11 16:59:12 -04:00
|
|
|
};
|
|
|
|
|
2024-09-29 20:07:50 -04:00
|
|
|
WebWorker::bootstrap_from_options(services, options)
|
2022-08-11 16:59:12 -04:00
|
|
|
})
|
|
|
|
}
|
2022-11-21 08:36:26 -05:00
|
|
|
|
2024-11-15 04:33:03 -05:00
|
|
|
/// By default V8 uses 1.4Gb heap limit which is meant for browser tabs.
|
|
|
|
/// Instead probe for the total memory on the system and use it instead
|
|
|
|
/// as a default.
|
|
|
|
pub fn create_isolate_create_params() -> Option<v8::CreateParams> {
|
|
|
|
let maybe_mem_info = deno_runtime::sys_info::mem_info();
|
|
|
|
maybe_mem_info.map(|mem_info| {
|
|
|
|
v8::CreateParams::default()
|
|
|
|
.heap_limits_from_system_memory(mem_info.total, 0)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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-09-29 20:07:50 -04:00
|
|
|
use deno_core::FsModuleLoader;
|
2024-09-16 16:39:37 -04:00
|
|
|
use deno_fs::RealFs;
|
2024-06-06 23:37:53 -04:00
|
|
|
use deno_runtime::deno_permissions::Permissions;
|
2024-09-30 09:19:24 -04:00
|
|
|
use deno_runtime::permissions::RuntimePermissionDescriptorParser;
|
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-09-29 20:07:50 -04:00
|
|
|
let fs = Arc::new(RealFs);
|
|
|
|
let permission_desc_parser =
|
|
|
|
Arc::new(RuntimePermissionDescriptorParser::new(fs.clone()));
|
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
|
|
|
};
|
|
|
|
|
2024-09-29 20:07:50 -04:00
|
|
|
MainWorker::bootstrap_from_options(
|
|
|
|
main_module,
|
|
|
|
WorkerServiceOptions {
|
|
|
|
module_loader: Rc::new(FsModuleLoader),
|
|
|
|
permissions: PermissionsContainer::new(
|
2024-09-30 09:19:24 -04:00
|
|
|
permission_desc_parser,
|
2024-09-29 20:07:50 -04:00
|
|
|
Permissions::none_without_prompt(),
|
|
|
|
),
|
|
|
|
blob_store: Default::default(),
|
|
|
|
broadcast_channel: Default::default(),
|
|
|
|
feature_checker: Default::default(),
|
|
|
|
node_services: Default::default(),
|
|
|
|
npm_process_state_provider: Default::default(),
|
|
|
|
root_cert_store_provider: Default::default(),
|
2024-11-15 05:44:11 -05:00
|
|
|
fetch_dns_resolver: Default::default(),
|
2024-09-29 20:07:50 -04:00
|
|
|
shared_array_buffer_store: Default::default(),
|
|
|
|
compiled_wasm_module_store: Default::default(),
|
|
|
|
v8_code_cache: Default::default(),
|
|
|
|
fs,
|
|
|
|
},
|
|
|
|
options,
|
|
|
|
)
|
2022-11-21 08:36:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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());
|
|
|
|
}
|
|
|
|
}
|