2024-01-01 19:58:21 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-01-13 07:51:32 +00:00
|
|
|
|
2023-10-03 23:05:06 +00:00
|
|
|
use std::path::Path;
|
2022-08-11 20:59:12 +00:00
|
|
|
use std::path::PathBuf;
|
2023-04-27 14:05:20 +00:00
|
|
|
use std::rc::Rc;
|
2022-08-11 20:59:12 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use deno_ast::ModuleSpecifier;
|
2023-08-01 22:47:57 +00:00
|
|
|
use deno_core::anyhow::bail;
|
2023-05-22 20:55:04 +00:00
|
|
|
use deno_core::anyhow::Context;
|
2022-08-11 20:59:12 +00:00
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::futures::FutureExt;
|
|
|
|
use deno_core::located_script_name;
|
2023-05-22 20:55:04 +00:00
|
|
|
use deno_core::parking_lot::Mutex;
|
2023-04-27 14:05:20 +00:00
|
|
|
use deno_core::url::Url;
|
2023-09-14 14:38:15 +00:00
|
|
|
use deno_core::v8;
|
2023-04-27 14:05:20 +00:00
|
|
|
use deno_core::CompiledWasmModuleStore;
|
2022-08-11 20:59:12 +00:00
|
|
|
use deno_core::Extension;
|
2023-10-12 15:55:50 +00:00
|
|
|
use deno_core::FeatureChecker;
|
2022-08-11 20:59:12 +00:00
|
|
|
use deno_core::ModuleId;
|
2023-05-01 12:59:38 +00:00
|
|
|
use deno_core::ModuleLoader;
|
2023-11-22 02:45:34 +00:00
|
|
|
use deno_core::PollEventLoopOptions;
|
2023-04-27 14:05:20 +00:00
|
|
|
use deno_core::SharedArrayBufferStore;
|
2023-05-01 12:59:38 +00:00
|
|
|
use deno_core::SourceMapGetter;
|
2023-05-22 20:55:04 +00:00
|
|
|
use deno_lockfile::Lockfile;
|
2022-08-11 20:59:12 +00:00
|
|
|
use deno_runtime::colors;
|
2023-04-27 14:05:20 +00:00
|
|
|
use deno_runtime::deno_broadcast_channel::InMemoryBroadcastChannel;
|
2023-05-04 18:28:42 +00:00
|
|
|
use deno_runtime::deno_fs;
|
2023-02-10 15:26:39 +00:00
|
|
|
use deno_runtime::deno_node;
|
2023-04-22 01:02:46 +00:00
|
|
|
use deno_runtime::deno_node::NodeResolution;
|
2023-08-01 22:47:57 +00:00
|
|
|
use deno_runtime::deno_node::NodeResolutionMode;
|
2023-04-27 14:05:20 +00:00
|
|
|
use deno_runtime::deno_node::NodeResolver;
|
2023-05-01 20:42:05 +00:00
|
|
|
use deno_runtime::deno_tls::RootCertStoreProvider;
|
2023-04-27 14:05:20 +00:00
|
|
|
use deno_runtime::deno_web::BlobStore;
|
2022-09-02 20:53:23 +00:00
|
|
|
use deno_runtime::fmt_errors::format_js_error;
|
2023-04-27 14:05:20 +00:00
|
|
|
use deno_runtime::inspector_server::InspectorServer;
|
2022-08-11 20:59:12 +00:00
|
|
|
use deno_runtime::ops::worker_host::CreateWebWorkerCb;
|
2023-01-07 16:25:34 +00:00
|
|
|
use deno_runtime::permissions::PermissionsContainer;
|
2022-08-11 20:59:12 +00: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 15:34:50 +00:00
|
|
|
use deno_runtime::WorkerLogLevel;
|
2023-04-06 22:46:44 +00:00
|
|
|
use deno_semver::npm::NpmPackageReqReference;
|
2023-09-18 20:02:58 +00:00
|
|
|
use deno_semver::package::PackageReqReference;
|
2023-10-31 00:25:58 +00:00
|
|
|
use tokio::select;
|
2022-08-11 20:59:12 +00:00
|
|
|
|
2023-09-18 20:02:58 +00:00
|
|
|
use crate::args::package_json::PackageJsonDeps;
|
2023-11-14 21:06:00 +00:00
|
|
|
use crate::args::DenoSubcommand;
|
2023-04-27 14:05:20 +00:00
|
|
|
use crate::args::StorageKeyResolver;
|
2023-10-31 00:25:58 +00:00
|
|
|
use crate::emit::Emitter;
|
2022-08-11 20:59:12 +00:00
|
|
|
use crate::errors;
|
2023-04-27 14:05:20 +00:00
|
|
|
use crate::npm::CliNpmResolver;
|
2022-08-11 20:59:12 +00:00
|
|
|
use crate::tools;
|
|
|
|
use crate::tools::coverage::CoverageCollector;
|
2023-10-31 00:25:58 +00:00
|
|
|
use crate::tools::run::hmr::HmrRunner;
|
2022-11-28 22:28:54 +00:00
|
|
|
use crate::util::checksum;
|
2023-10-31 00:25:58 +00:00
|
|
|
use crate::util::file_watcher::WatcherCommunicator;
|
|
|
|
use crate::util::file_watcher::WatcherRestartMode;
|
2022-08-11 20:59:12 +00:00
|
|
|
use crate::version;
|
|
|
|
|
2023-05-01 12:59:38 +00: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 18:35:23 +00:00
|
|
|
#[derive(Clone)]
|
2023-04-27 14:05:20 +00:00
|
|
|
pub struct CliMainWorkerOptions {
|
|
|
|
pub argv: Vec<String>,
|
2023-05-30 15:34:50 +00:00
|
|
|
pub log_level: WorkerLogLevel,
|
2023-04-27 14:05:20 +00:00
|
|
|
pub coverage_dir: Option<String>,
|
2023-11-05 21:27:36 +00:00
|
|
|
pub enable_op_summary_metrics: bool,
|
2023-04-27 14:05:20 +00:00
|
|
|
pub enable_testing_features: bool,
|
|
|
|
pub has_node_modules_dir: bool,
|
2023-10-31 00:25:58 +00:00
|
|
|
pub hmr: bool,
|
2023-04-27 14:05:20 +00:00
|
|
|
pub inspect_brk: bool,
|
|
|
|
pub inspect_wait: bool,
|
2023-11-10 17:41:24 +00:00
|
|
|
pub strace_ops: Option<Vec<String>>,
|
2023-04-27 14:05:20 +00:00
|
|
|
pub is_inspecting: bool,
|
|
|
|
pub is_npm_main: bool,
|
|
|
|
pub location: Option<Url>,
|
|
|
|
pub maybe_binary_npm_command_name: Option<String>,
|
2023-05-01 12:59:38 +00:00
|
|
|
pub origin_data_folder_path: Option<PathBuf>,
|
2023-04-27 14:05:20 +00:00
|
|
|
pub seed: Option<u64>,
|
|
|
|
pub unsafely_ignore_certificate_errors: Option<Vec<String>>,
|
|
|
|
pub unstable: bool,
|
2023-11-11 17:01:48 +00:00
|
|
|
pub skip_op_registration: bool,
|
2023-10-03 23:05:06 +00:00
|
|
|
pub maybe_root_package_json_deps: Option<PackageJsonDeps>,
|
2023-04-27 14:05:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct SharedWorkerState {
|
2023-05-01 12:59:38 +00:00
|
|
|
options: CliMainWorkerOptions,
|
2023-11-14 21:06:00 +00:00
|
|
|
subcommand: DenoSubcommand,
|
2023-05-01 12:59:38 +00:00
|
|
|
storage_key_resolver: StorageKeyResolver,
|
2023-09-29 13:26:25 +00:00
|
|
|
npm_resolver: Arc<dyn CliNpmResolver>,
|
2023-05-01 12:59:38 +00:00
|
|
|
node_resolver: Arc<NodeResolver>,
|
2023-07-01 22:52:30 +00:00
|
|
|
blob_store: Arc<BlobStore>,
|
2023-05-01 12:59:38 +00:00
|
|
|
broadcast_channel: InMemoryBroadcastChannel,
|
|
|
|
shared_array_buffer_store: SharedArrayBufferStore,
|
|
|
|
compiled_wasm_module_store: CompiledWasmModuleStore,
|
|
|
|
module_loader_factory: Box<dyn ModuleLoaderFactory>,
|
2023-05-01 20:42:05 +00:00
|
|
|
root_cert_store_provider: Arc<dyn RootCertStoreProvider>,
|
2023-05-04 18:28:42 +00:00
|
|
|
fs: Arc<dyn deno_fs::FileSystem>,
|
2023-10-31 00:25:58 +00:00
|
|
|
emitter: Option<Arc<Emitter>>,
|
|
|
|
maybe_file_watcher_communicator: Option<Arc<WatcherCommunicator>>,
|
2023-05-01 12:59:38 +00:00
|
|
|
maybe_inspector_server: Option<Arc<InspectorServer>>,
|
2023-05-22 20:55:04 +00:00
|
|
|
maybe_lockfile: Option<Arc<Mutex<Lockfile>>>,
|
2023-10-12 15:55:50 +00:00
|
|
|
feature_checker: Arc<FeatureChecker>,
|
2023-12-19 12:37:22 +00:00
|
|
|
node_ipc: Option<i64>,
|
2024-01-18 23:30:49 +00:00
|
|
|
disable_deprecated_api_warning: bool,
|
2024-01-26 15:41:16 +00:00
|
|
|
verbose_deprecated_api_warning: bool,
|
2023-05-01 12:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SharedWorkerState {
|
2023-08-15 19:36:36 +00:00
|
|
|
// Currently empty
|
2023-04-27 14:05:20 +00:00
|
|
|
}
|
|
|
|
|
2022-08-11 20:59:12 +00:00
|
|
|
pub struct CliMainWorker {
|
|
|
|
main_module: ModuleSpecifier,
|
2022-08-23 14:39:19 +00:00
|
|
|
is_main_cjs: bool,
|
2022-08-11 20:59:12 +00:00
|
|
|
worker: MainWorker,
|
2023-04-27 14:05:20 +00:00
|
|
|
shared: Arc<SharedWorkerState>,
|
2022-08-11 20:59:12 +00: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-31 00:25:58 +00:00
|
|
|
let mut maybe_hmr_runner = self.maybe_setup_hmr_runner().await?;
|
|
|
|
|
2022-08-11 20:59:12 +00:00
|
|
|
log::debug!("main_module {}", self.main_module);
|
|
|
|
|
2022-09-03 16:19:30 +00:00
|
|
|
if self.is_main_cjs {
|
2023-02-10 15:26:39 +00:00
|
|
|
deno_node::load_cjs_module(
|
2022-08-23 14:39:19 +00:00
|
|
|
&mut self.worker.js_runtime,
|
|
|
|
&self.main_module.to_file_path().unwrap().to_string_lossy(),
|
|
|
|
true,
|
2023-04-27 14:05:20 +00:00
|
|
|
self.shared.options.inspect_brk,
|
2022-08-23 14:39:19 +00:00
|
|
|
)?;
|
2022-08-11 20:59:12 +00:00
|
|
|
} else {
|
2022-08-20 15:31:33 +00:00
|
|
|
self.execute_main_module_possibly_with_npm().await?;
|
2022-08-11 20:59:12 +00:00
|
|
|
}
|
|
|
|
|
2023-03-21 22:33:12 +00:00
|
|
|
self.worker.dispatch_load_event(located_script_name!())?;
|
2022-08-11 20:59:12 +00:00
|
|
|
|
|
|
|
loop {
|
2023-10-31 00:25:58 +00: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?;
|
|
|
|
}
|
|
|
|
|
2022-08-11 20:59:12 +00:00
|
|
|
if !self
|
|
|
|
.worker
|
2023-03-21 22:33:12 +00:00
|
|
|
.dispatch_beforeunload_event(located_script_name!())?
|
2022-08-11 20:59:12 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 22:33:12 +00:00
|
|
|
self.worker.dispatch_unload_event(located_script_name!())?;
|
2022-08-11 20:59:12 +00:00
|
|
|
|
|
|
|
if let Some(coverage_collector) = maybe_coverage_collector.as_mut() {
|
|
|
|
self
|
|
|
|
.worker
|
2023-11-22 02:45:34 +00:00
|
|
|
.js_runtime
|
2023-12-13 15:07:26 +00:00
|
|
|
.with_event_loop_future(
|
2023-11-22 02:45:34 +00:00
|
|
|
coverage_collector.stop_collecting().boxed_local(),
|
2023-12-13 15:07:26 +00:00
|
|
|
PollEventLoopOptions::default(),
|
2023-11-22 02:45:34 +00:00
|
|
|
)
|
2022-08-11 20:59:12 +00:00
|
|
|
.await?;
|
|
|
|
}
|
2023-10-31 00:25:58 +00:00
|
|
|
if let Some(hmr_runner) = maybe_hmr_runner.as_mut() {
|
|
|
|
self
|
|
|
|
.worker
|
2023-11-22 02:45:34 +00:00
|
|
|
.js_runtime
|
2023-12-13 15:07:26 +00:00
|
|
|
.with_event_loop_future(
|
2023-11-22 02:45:34 +00:00
|
|
|
hmr_runner.stop().boxed_local(),
|
2023-12-13 15:07:26 +00:00
|
|
|
PollEventLoopOptions::default(),
|
2023-11-22 02:45:34 +00:00
|
|
|
)
|
2023-10-31 00:25:58 +00:00
|
|
|
.await?;
|
|
|
|
}
|
2022-08-11 20:59:12 +00:00
|
|
|
|
2023-01-05 19:29:50 +00:00
|
|
|
Ok(self.worker.exit_code())
|
2022-08-11 20:59:12 +00: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 15:31:33 +00:00
|
|
|
inner: CliMainWorker,
|
2022-08-11 20:59:12 +00:00
|
|
|
pending_unload: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FileWatcherModuleExecutor {
|
2022-08-20 15:31:33 +00:00
|
|
|
pub fn new(worker: CliMainWorker) -> FileWatcherModuleExecutor {
|
2022-08-11 20:59:12 +00:00
|
|
|
FileWatcherModuleExecutor {
|
2022-08-20 15:31:33 +00:00
|
|
|
inner: worker,
|
2022-08-11 20:59:12 +00:00
|
|
|
pending_unload: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Execute the given main module emitting load and unload events before and after execution
|
|
|
|
/// respectively.
|
2022-08-20 15:31:33 +00:00
|
|
|
pub async fn execute(&mut self) -> Result<(), AnyError> {
|
|
|
|
self.inner.execute_main_module_possibly_with_npm().await?;
|
|
|
|
self
|
|
|
|
.inner
|
|
|
|
.worker
|
2023-03-21 22:33:12 +00:00
|
|
|
.dispatch_load_event(located_script_name!())?;
|
2022-08-11 20:59:12 +00:00
|
|
|
self.pending_unload = true;
|
|
|
|
|
|
|
|
let result = loop {
|
2022-09-06 11:18:23 +00:00
|
|
|
match self.inner.worker.run_event_loop(false).await {
|
|
|
|
Ok(()) => {}
|
|
|
|
Err(error) => break Err(error),
|
|
|
|
}
|
|
|
|
match self
|
2022-08-20 15:31:33 +00:00
|
|
|
.inner
|
2022-08-11 20:59:12 +00:00
|
|
|
.worker
|
2023-03-21 22:33:12 +00:00
|
|
|
.dispatch_beforeunload_event(located_script_name!())
|
2022-08-11 20:59:12 +00:00
|
|
|
{
|
2022-09-06 11:18:23 +00:00
|
|
|
Ok(default_prevented) if default_prevented => {} // continue loop
|
|
|
|
Ok(_) => break Ok(()),
|
|
|
|
Err(error) => break Err(error),
|
2022-08-11 20:59:12 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
self.pending_unload = false;
|
|
|
|
|
2022-09-26 19:52:16 +00:00
|
|
|
result?;
|
2022-08-11 20:59:12 +00:00
|
|
|
|
2022-08-20 15:31:33 +00:00
|
|
|
self
|
|
|
|
.inner
|
|
|
|
.worker
|
2023-03-21 22:33:12 +00:00
|
|
|
.dispatch_unload_event(located_script_name!())?;
|
2022-08-11 20:59:12 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for FileWatcherModuleExecutor {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if self.pending_unload {
|
2022-09-06 11:18:23 +00:00
|
|
|
let _ = self
|
2022-08-20 15:31:33 +00:00
|
|
|
.inner
|
2022-08-11 20:59:12 +00:00
|
|
|
.worker
|
2023-03-21 22:33:12 +00:00
|
|
|
.dispatch_unload_event(located_script_name!());
|
2022-08-11 20:59:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-20 15:31:33 +00:00
|
|
|
let mut executor = FileWatcherModuleExecutor::new(self);
|
|
|
|
executor.execute().await
|
2022-08-11 20:59:12 +00:00
|
|
|
}
|
|
|
|
|
2023-04-13 17:43:23 +00:00
|
|
|
pub async fn execute_main_module_possibly_with_npm(
|
2022-08-20 15:31:33 +00: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 17:43:23 +00:00
|
|
|
pub async fn execute_side_module_possibly_with_npm(
|
2022-08-20 15:31:33 +00: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 17:43:23 +00:00
|
|
|
pub async fn maybe_setup_coverage_collector(
|
2022-08-11 20:59:12 +00:00
|
|
|
&mut self,
|
|
|
|
) -> Result<Option<CoverageCollector>, AnyError> {
|
2023-04-27 14:05:20 +00:00
|
|
|
if let Some(coverage_dir) = &self.shared.options.coverage_dir {
|
2022-08-11 20:59:12 +00: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
|
2023-11-22 02:45:34 +00:00
|
|
|
.js_runtime
|
2023-12-13 15:07:26 +00:00
|
|
|
.with_event_loop_future(
|
2023-11-22 02:45:34 +00:00
|
|
|
coverage_collector.start_collecting().boxed_local(),
|
2023-12-13 15:07:26 +00:00
|
|
|
PollEventLoopOptions::default(),
|
2023-11-22 02:45:34 +00:00
|
|
|
)
|
2022-08-11 20:59:12 +00:00
|
|
|
.await?;
|
|
|
|
Ok(Some(coverage_collector))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
2023-09-14 14:38:15 +00:00
|
|
|
|
2023-10-31 00:25:58 +00:00
|
|
|
pub async fn maybe_setup_hmr_runner(
|
|
|
|
&mut self,
|
|
|
|
) -> Result<Option<HmrRunner>, AnyError> {
|
|
|
|
if !self.shared.options.hmr {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
|
|
|
let watcher_communicator =
|
|
|
|
self.shared.maybe_file_watcher_communicator.clone().unwrap();
|
|
|
|
let emitter = self.shared.emitter.clone().unwrap();
|
|
|
|
|
|
|
|
let session = self.worker.create_inspector_session().await;
|
|
|
|
let mut hmr_runner = HmrRunner::new(emitter, session, watcher_communicator);
|
|
|
|
|
|
|
|
self
|
|
|
|
.worker
|
2023-11-22 02:45:34 +00:00
|
|
|
.js_runtime
|
2023-12-13 15:07:26 +00:00
|
|
|
.with_event_loop_future(
|
2023-11-22 02:45:34 +00:00
|
|
|
hmr_runner.start().boxed_local(),
|
2023-12-13 15:07:26 +00:00
|
|
|
PollEventLoopOptions::default(),
|
2023-11-22 02:45:34 +00:00
|
|
|
)
|
2023-10-31 00:25:58 +00:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(Some(hmr_runner))
|
|
|
|
}
|
|
|
|
|
2023-09-14 14:38:15 +00: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 20:59:12 +00:00
|
|
|
}
|
|
|
|
|
2023-04-27 14:05:20 +00:00
|
|
|
pub struct CliMainWorkerFactory {
|
|
|
|
shared: Arc<SharedWorkerState>,
|
2022-11-21 13:36:26 +00:00
|
|
|
}
|
|
|
|
|
2023-04-27 14:05:20 +00:00
|
|
|
impl CliMainWorkerFactory {
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
|
|
pub fn new(
|
|
|
|
storage_key_resolver: StorageKeyResolver,
|
2023-11-14 21:06:00 +00:00
|
|
|
subcommand: DenoSubcommand,
|
2023-09-29 13:26:25 +00:00
|
|
|
npm_resolver: Arc<dyn CliNpmResolver>,
|
2023-04-27 14:05:20 +00:00
|
|
|
node_resolver: Arc<NodeResolver>,
|
2023-07-01 22:52:30 +00:00
|
|
|
blob_store: Arc<BlobStore>,
|
2023-05-01 12:59:38 +00:00
|
|
|
module_loader_factory: Box<dyn ModuleLoaderFactory>,
|
2023-05-01 20:42:05 +00:00
|
|
|
root_cert_store_provider: Arc<dyn RootCertStoreProvider>,
|
2023-05-04 18:28:42 +00:00
|
|
|
fs: Arc<dyn deno_fs::FileSystem>,
|
2023-10-31 00:25:58 +00:00
|
|
|
emitter: Option<Arc<Emitter>>,
|
|
|
|
maybe_file_watcher_communicator: Option<Arc<WatcherCommunicator>>,
|
2023-04-27 14:05:20 +00:00
|
|
|
maybe_inspector_server: Option<Arc<InspectorServer>>,
|
2023-05-22 20:55:04 +00:00
|
|
|
maybe_lockfile: Option<Arc<Mutex<Lockfile>>>,
|
2023-10-12 15:55:50 +00:00
|
|
|
feature_checker: Arc<FeatureChecker>,
|
2023-04-27 14:05:20 +00:00
|
|
|
options: CliMainWorkerOptions,
|
2023-12-19 12:37:22 +00:00
|
|
|
node_ipc: Option<i64>,
|
2024-01-18 23:30:49 +00:00
|
|
|
disable_deprecated_api_warning: bool,
|
2024-01-26 15:41:16 +00:00
|
|
|
verbose_deprecated_api_warning: bool,
|
2023-04-27 14:05:20 +00:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
shared: Arc::new(SharedWorkerState {
|
|
|
|
options,
|
2023-11-14 21:06:00 +00:00
|
|
|
subcommand,
|
2023-04-27 14:05:20 +00: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 20:42:05 +00:00
|
|
|
root_cert_store_provider,
|
2023-10-31 00:25:58 +00:00
|
|
|
emitter,
|
2023-05-04 18:28:42 +00:00
|
|
|
fs,
|
2023-10-31 00:25:58 +00:00
|
|
|
maybe_file_watcher_communicator,
|
2023-04-27 14:05:20 +00:00
|
|
|
maybe_inspector_server,
|
2023-05-22 20:55:04 +00:00
|
|
|
maybe_lockfile,
|
2023-10-12 15:55:50 +00:00
|
|
|
feature_checker,
|
2023-12-13 10:14:16 +00:00
|
|
|
node_ipc,
|
2024-01-18 23:30:49 +00:00
|
|
|
disable_deprecated_api_warning,
|
2024-01-26 15:41:16 +00:00
|
|
|
verbose_deprecated_api_warning,
|
2023-04-27 14:05:20 +00: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,
|
2023-12-02 23:40:27 +00:00
|
|
|
custom_extensions: Vec<Extension>,
|
2023-04-27 14:05:20 +00: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-18 20:02:58 +00: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
|
2023-10-03 23:05:06 +00:00
|
|
|
.maybe_root_package_json_deps
|
2023-09-18 20:02:58 +00:00
|
|
|
.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 13:26:25 +00:00
|
|
|
if let Some(npm_resolver) = shared.npm_resolver.as_managed() {
|
|
|
|
npm_resolver
|
|
|
|
.add_package_reqs(&[package_ref.req().clone()])
|
|
|
|
.await?;
|
|
|
|
}
|
2023-10-03 23:05:06 +00: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 20:43:45 +00:00
|
|
|
.npm_resolver
|
2023-10-03 23:05:06 +00:00
|
|
|
.resolve_pkg_folder_from_deno_module_req(
|
|
|
|
package_ref.req(),
|
|
|
|
&referrer,
|
|
|
|
)?;
|
|
|
|
let node_resolution = self.resolve_binary_entrypoint(
|
|
|
|
&package_folder,
|
|
|
|
package_ref.sub_path(),
|
|
|
|
&permissions,
|
|
|
|
)?;
|
2023-04-27 14:05:20 +00:00
|
|
|
let is_main_cjs = matches!(node_resolution, NodeResolution::CommonJs(_));
|
2023-05-22 20:55:04 +00: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 14:05:20 +00:00
|
|
|
(node_resolution.into_url(), is_main_cjs)
|
2023-11-16 22:29:35 +00:00
|
|
|
} else if shared.options.is_npm_main
|
|
|
|
|| shared.node_resolver.in_npm_package(&main_module)
|
|
|
|
{
|
2023-04-27 14:05:20 +00: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)
|
|
|
|
};
|
|
|
|
|
2023-05-01 12:59:38 +00: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 14:05:20 +00: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 20:59:12 +00:00
|
|
|
.options
|
2023-04-27 14:05:20 +00:00
|
|
|
.origin_data_folder_path
|
2023-05-01 12:59:38 +00:00
|
|
|
.as_ref()
|
|
|
|
.unwrap() // must be set if storage key resolver returns a value
|
2023-04-27 14:05:20 +00: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 22:15:08 +00: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 16:41:21 +00:00
|
|
|
let mut unstable_features =
|
|
|
|
Vec::with_capacity(crate::UNSTABLE_GRANULAR_FLAGS.len());
|
2023-11-01 22:15:08 +00:00
|
|
|
for (feature_name, _, id) in crate::UNSTABLE_GRANULAR_FLAGS {
|
|
|
|
if feature_checker.check(feature_name) {
|
|
|
|
unstable_features.push(*id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-27 14:05:20 +00:00
|
|
|
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 15:34:50 +00:00
|
|
|
log_level: shared.options.log_level,
|
2023-11-05 21:27:36 +00:00
|
|
|
enable_op_summary_metrics: shared.options.enable_op_summary_metrics,
|
2023-04-27 14:05:20 +00: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(),
|
|
|
|
unstable: shared.options.unstable,
|
2023-11-01 22:15:08 +00:00
|
|
|
unstable_features,
|
2023-04-27 14:05:20 +00:00
|
|
|
user_agent: version::get_user_agent().to_string(),
|
|
|
|
inspect: shared.options.is_inspecting,
|
2023-08-15 19:36:36 +00: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-12-13 10:14:16 +00:00
|
|
|
node_ipc_fd: shared.node_ipc,
|
2024-01-18 23:30:49 +00:00
|
|
|
disable_deprecated_api_warning: shared.disable_deprecated_api_warning,
|
2024-01-26 15:41:16 +00:00
|
|
|
verbose_deprecated_api_warning: shared.verbose_deprecated_api_warning,
|
2023-04-27 14:05:20 +00:00
|
|
|
},
|
2023-12-02 23:40:27 +00:00
|
|
|
extensions: custom_extensions,
|
2023-08-05 23:47:15 +00:00
|
|
|
startup_snapshot: crate::js::deno_isolate_init(),
|
2023-06-05 09:22:32 +00:00
|
|
|
create_params: None,
|
2023-04-27 14:05:20 +00:00
|
|
|
unsafely_ignore_certificate_errors: shared
|
|
|
|
.options
|
|
|
|
.unsafely_ignore_certificate_errors
|
|
|
|
.clone(),
|
2023-05-01 20:42:05 +00:00
|
|
|
root_cert_store_provider: Some(shared.root_cert_store_provider.clone()),
|
2023-04-27 14:05:20 +00:00
|
|
|
seed: shared.options.seed,
|
2023-05-01 12:59:38 +00:00
|
|
|
source_map_getter: maybe_source_map_getter,
|
2023-04-27 14:05:20 +00: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,
|
2023-11-10 17:41:24 +00:00
|
|
|
strace_ops: shared.options.strace_ops.clone(),
|
2023-04-27 14:05:20 +00:00
|
|
|
module_loader,
|
2023-05-04 18:28:42 +00:00
|
|
|
fs: shared.fs.clone(),
|
2023-09-29 13:26:25 +00:00
|
|
|
npm_resolver: Some(shared.npm_resolver.clone().into_npm_resolver()),
|
2023-04-27 14:05:20 +00: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 22:15:08 +00:00
|
|
|
feature_checker,
|
2023-11-11 17:01:48 +00:00
|
|
|
skip_op_registration: shared.options.skip_op_registration,
|
2023-04-27 14:05:20 +00:00
|
|
|
};
|
|
|
|
|
2023-11-14 21:06:00 +00:00
|
|
|
let mut worker = MainWorker::bootstrap_from_options(
|
2023-04-27 14:05:20 +00:00
|
|
|
main_module.clone(),
|
|
|
|
permissions,
|
|
|
|
options,
|
|
|
|
);
|
|
|
|
|
2023-11-25 03:46:16 +00:00
|
|
|
if self.shared.subcommand.needs_test() {
|
2024-01-25 19:54:35 +00:00
|
|
|
macro_rules! test_file {
|
|
|
|
($($file:literal),*) => {
|
|
|
|
$(worker.js_runtime.lazy_load_es_module_from_code(
|
|
|
|
concat!("ext:cli/", $file),
|
|
|
|
deno_core::FastString::StaticAscii(include_str!(concat!("js/", $file))),
|
|
|
|
)?;)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
test_file!(
|
|
|
|
"40_test_common.js",
|
|
|
|
"40_test.js",
|
|
|
|
"40_bench.js",
|
|
|
|
"40_jupyter.js"
|
|
|
|
);
|
2023-11-14 21:06:00 +00:00
|
|
|
}
|
|
|
|
|
2023-04-27 14:05:20 +00:00
|
|
|
Ok(CliMainWorker {
|
|
|
|
main_module,
|
|
|
|
is_main_cjs,
|
|
|
|
worker,
|
|
|
|
shared: shared.clone(),
|
|
|
|
})
|
|
|
|
}
|
2023-08-01 22:47:57 +00:00
|
|
|
|
|
|
|
fn resolve_binary_entrypoint(
|
|
|
|
&self,
|
2023-10-03 23:05:06 +00:00
|
|
|
package_folder: &Path,
|
|
|
|
sub_path: Option<&str>,
|
2023-08-01 22:47:57 +00:00
|
|
|
permissions: &PermissionsContainer,
|
|
|
|
) -> Result<NodeResolution, AnyError> {
|
2023-09-28 20:43:45 +00:00
|
|
|
match self
|
|
|
|
.shared
|
|
|
|
.node_resolver
|
2023-10-03 23:05:06 +00:00
|
|
|
.resolve_binary_export(package_folder, sub_path)
|
2023-09-28 20:43:45 +00:00
|
|
|
{
|
2023-08-01 22:47:57 +00:00
|
|
|
Ok(node_resolution) => Ok(node_resolution),
|
|
|
|
Err(original_err) => {
|
|
|
|
// if the binary entrypoint was not found, fallback to regular node resolution
|
2023-10-03 23:05:06 +00:00
|
|
|
let result = self.resolve_binary_entrypoint_fallback(
|
|
|
|
package_folder,
|
|
|
|
sub_path,
|
|
|
|
permissions,
|
|
|
|
);
|
2023-08-01 22:47:57 +00:00
|
|
|
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-10-03 23:05:06 +00:00
|
|
|
package_folder: &Path,
|
|
|
|
sub_path: Option<&str>,
|
2023-08-01 22:47:57 +00:00
|
|
|
permissions: &PermissionsContainer,
|
|
|
|
) -> Result<Option<NodeResolution>, AnyError> {
|
|
|
|
// only fallback if the user specified a sub path
|
2023-10-03 23:05:06 +00:00
|
|
|
if sub_path.is_none() {
|
2023-08-01 22:47:57 +00: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-10-05 20:18:29 +00:00
|
|
|
// use a fake referrer since a real one doesn't exist
|
|
|
|
let referrer =
|
|
|
|
ModuleSpecifier::from_directory_path(package_folder).unwrap();
|
|
|
|
let Some(resolution) = self
|
|
|
|
.shared
|
|
|
|
.node_resolver
|
|
|
|
.resolve_package_subpath_from_deno_module(
|
|
|
|
package_folder,
|
|
|
|
sub_path,
|
|
|
|
&referrer,
|
|
|
|
NodeResolutionMode::Execution,
|
|
|
|
permissions,
|
|
|
|
)?
|
2023-08-27 04:04:12 +00:00
|
|
|
else {
|
2023-08-01 22:47:57 +00: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 20:59:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn create_web_worker_callback(
|
2023-04-27 14:05:20 +00:00
|
|
|
shared: Arc<SharedWorkerState>,
|
2023-03-05 00:39:48 +00:00
|
|
|
stdio: deno_runtime::deno_io::Stdio,
|
2022-08-11 20:59:12 +00:00
|
|
|
) -> Arc<CreateWebWorkerCb> {
|
|
|
|
Arc::new(move |args| {
|
2023-04-27 14:05:20 +00:00
|
|
|
let maybe_inspector_server = shared.maybe_inspector_server.clone();
|
2022-08-11 20:59:12 +00:00
|
|
|
|
2023-05-01 12:59:38 +00: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 20:59:12 +00:00
|
|
|
let create_web_worker_cb =
|
2023-04-27 14:05:20 +00:00
|
|
|
create_web_worker_callback(shared.clone(), stdio.clone());
|
2022-08-11 20:59:12 +00:00
|
|
|
|
2023-04-27 14:05:20 +00:00
|
|
|
let maybe_storage_key = shared
|
|
|
|
.storage_key_resolver
|
|
|
|
.resolve_storage_key(&args.main_module);
|
2022-09-28 12:11:12 +00: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 22:15:08 +00: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 16:41:21 +00:00
|
|
|
let mut unstable_features =
|
|
|
|
Vec::with_capacity(crate::UNSTABLE_GRANULAR_FLAGS.len());
|
2023-11-01 22:15:08 +00:00
|
|
|
for (feature_name, _, id) in crate::UNSTABLE_GRANULAR_FLAGS {
|
|
|
|
if feature_checker.check(feature_name) {
|
|
|
|
unstable_features.push(*id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-11 20:59:12 +00:00
|
|
|
let options = WebWorkerOptions {
|
|
|
|
bootstrap: BootstrapOptions {
|
2023-04-27 14:05:20 +00:00
|
|
|
args: shared.options.argv.clone(),
|
2022-08-11 20:59:12 +00:00
|
|
|
cpu_count: std::thread::available_parallelism()
|
|
|
|
.map(|p| p.get())
|
|
|
|
.unwrap_or(1),
|
2023-05-30 15:34:50 +00:00
|
|
|
log_level: shared.options.log_level,
|
2023-11-05 21:27:36 +00:00
|
|
|
enable_op_summary_metrics: shared.options.enable_op_summary_metrics,
|
2023-04-27 14:05:20 +00:00
|
|
|
enable_testing_features: shared.options.enable_testing_features,
|
2022-10-18 13:33:35 +00:00
|
|
|
locale: deno_core::v8::icu::get_language_tag(),
|
2022-08-11 20:59:12 +00:00
|
|
|
location: Some(args.main_module.clone()),
|
|
|
|
no_color: !colors::use_color(),
|
|
|
|
is_tty: colors::is_tty(),
|
2023-04-27 14:05:20 +00:00
|
|
|
unstable: shared.options.unstable,
|
2023-11-01 22:15:08 +00:00
|
|
|
unstable_features,
|
2023-03-23 22:27:58 +00:00
|
|
|
user_agent: version::get_user_agent().to_string(),
|
2023-04-27 14:05:20 +00:00
|
|
|
inspect: shared.options.is_inspecting,
|
2023-08-15 19:36:36 +00: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-12-13 10:14:16 +00:00
|
|
|
node_ipc_fd: None,
|
2024-01-18 23:30:49 +00:00
|
|
|
disable_deprecated_api_warning: shared.disable_deprecated_api_warning,
|
2024-01-26 15:41:16 +00:00
|
|
|
verbose_deprecated_api_warning: shared.verbose_deprecated_api_warning,
|
2022-08-11 20:59:12 +00:00
|
|
|
},
|
2023-12-02 23:40:27 +00:00
|
|
|
extensions: vec![],
|
2023-08-05 23:47:15 +00:00
|
|
|
startup_snapshot: crate::js::deno_isolate_init(),
|
2023-04-27 14:05:20 +00:00
|
|
|
unsafely_ignore_certificate_errors: shared
|
2022-08-11 20:59:12 +00:00
|
|
|
.options
|
2023-04-27 14:05:20 +00:00
|
|
|
.unsafely_ignore_certificate_errors
|
2022-12-08 16:50:09 +00:00
|
|
|
.clone(),
|
2023-05-01 20:42:05 +00:00
|
|
|
root_cert_store_provider: Some(shared.root_cert_store_provider.clone()),
|
2023-04-27 14:05:20 +00:00
|
|
|
seed: shared.options.seed,
|
2022-08-11 20:59:12 +00:00
|
|
|
create_web_worker_cb,
|
|
|
|
format_js_error_fn: Some(Arc::new(format_js_error)),
|
2023-05-01 12:59:38 +00:00
|
|
|
source_map_getter: maybe_source_map_getter,
|
2022-08-11 20:59:12 +00:00
|
|
|
module_loader,
|
2023-05-04 18:28:42 +00:00
|
|
|
fs: shared.fs.clone(),
|
2023-09-29 13:26:25 +00:00
|
|
|
npm_resolver: Some(shared.npm_resolver.clone().into_npm_resolver()),
|
2022-08-11 20:59:12 +00:00
|
|
|
worker_type: args.worker_type,
|
|
|
|
maybe_inspector_server,
|
|
|
|
get_error_class_fn: Some(&errors::get_error_class_name),
|
2023-04-27 14:05:20 +00: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 20:59:12 +00:00
|
|
|
stdio: stdio.clone(),
|
2022-09-28 12:11:12 +00:00
|
|
|
cache_storage_dir,
|
2023-11-01 22:15:08 +00:00
|
|
|
feature_checker,
|
2022-08-11 20:59:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
WebWorker::bootstrap_from_options(
|
|
|
|
args.name,
|
|
|
|
args.permissions,
|
|
|
|
args.main_module,
|
|
|
|
args.worker_id,
|
|
|
|
options,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
2022-11-21 13:36:26 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2023-03-13 23:31:03 +00:00
|
|
|
use deno_core::resolve_path;
|
2023-01-07 16:25:34 +00:00
|
|
|
use deno_runtime::permissions::Permissions;
|
2022-11-21 13:36:26 +00:00
|
|
|
|
|
|
|
fn create_test_worker() -> MainWorker {
|
2023-03-13 23:31:03 +00:00
|
|
|
let main_module =
|
|
|
|
resolve_path("./hello.js", &std::env::current_dir().unwrap()).unwrap();
|
2023-01-07 16:25:34 +00:00
|
|
|
let permissions = PermissionsContainer::new(Permissions::default());
|
2022-11-21 13:36:26 +00:00
|
|
|
|
|
|
|
let options = WorkerOptions {
|
2023-08-05 23:47:15 +00:00
|
|
|
startup_snapshot: crate::js::deno_isolate_init(),
|
2023-05-04 18:28:42 +00:00
|
|
|
..Default::default()
|
2022-11-21 13:36:26 +00: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-14 01:12:09 +00:00
|
|
|
let module_specifier = ModuleSpecifier::from_file_path(&p).unwrap();
|
2022-11-21 13:36:26 +00:00
|
|
|
let mut worker = create_test_worker();
|
|
|
|
let result = worker.execute_main_module(&module_specifier).await;
|
|
|
|
if let Err(err) = result {
|
2023-01-27 15:43:16 +00:00
|
|
|
eprintln!("execute_mod err {err:?}");
|
2022-11-21 13:36:26 +00:00
|
|
|
}
|
|
|
|
if let Err(e) = worker.run_event_loop(false).await {
|
2023-01-27 15:43:16 +00:00
|
|
|
panic!("Future got unexpected error: {e:?}");
|
2022-11-21 13:36:26 +00: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 23:31:03 +00:00
|
|
|
let module_specifier = ModuleSpecifier::from_file_path(&p).unwrap();
|
2022-11-21 13:36:26 +00:00
|
|
|
let mut worker = create_test_worker();
|
|
|
|
let result = worker.execute_main_module(&module_specifier).await;
|
|
|
|
if let Err(err) = result {
|
2023-01-27 15:43:16 +00:00
|
|
|
eprintln!("execute_mod err {err:?}");
|
2022-11-21 13:36:26 +00:00
|
|
|
}
|
|
|
|
if let Err(e) = worker.run_event_loop(false).await {
|
2023-01-27 15:43:16 +00:00
|
|
|
panic!("Future got unexpected error: {e:?}");
|
2022-11-21 13:36:26 +00: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 23:31:03 +00:00
|
|
|
let module_specifier =
|
|
|
|
resolve_path("./does-not-exist", &std::env::current_dir().unwrap())
|
|
|
|
.unwrap();
|
2022-11-21 13:36:26 +00: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 23:31:03 +00:00
|
|
|
let module_specifier = ModuleSpecifier::from_file_path(&p).unwrap();
|
2022-11-21 13:36:26 +00:00
|
|
|
let result = worker.execute_main_module(&module_specifier).await;
|
|
|
|
assert!(result.is_ok());
|
|
|
|
}
|
|
|
|
}
|