2023-01-13 07:51:32 +00:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
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;
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::futures::task::LocalFutureObj;
|
|
|
|
use deno_core::futures::FutureExt;
|
|
|
|
use deno_core::located_script_name;
|
2023-04-27 14:05:20 +00:00
|
|
|
use deno_core::url::Url;
|
|
|
|
use deno_core::CompiledWasmModuleStore;
|
2022-08-11 20:59:12 +00:00
|
|
|
use deno_core::Extension;
|
|
|
|
use deno_core::ModuleId;
|
2023-05-01 12:59:38 +00:00
|
|
|
use deno_core::ModuleLoader;
|
2023-04-27 14:05:20 +00:00
|
|
|
use deno_core::SharedArrayBufferStore;
|
2023-05-01 12:59:38 +00:00
|
|
|
use deno_core::SourceMapGetter;
|
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-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-04-27 14:05:20 +00:00
|
|
|
use deno_runtime::deno_node::NodeResolver;
|
|
|
|
use deno_runtime::deno_tls::rustls::RootCertStore;
|
|
|
|
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;
|
2022-08-17 01:00:35 +00:00
|
|
|
use deno_runtime::ops::worker_host::WorkerEventCb;
|
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-04-06 22:46:44 +00:00
|
|
|
use deno_semver::npm::NpmPackageReqReference;
|
2022-08-11 20:59:12 +00:00
|
|
|
|
2023-04-27 14:05:20 +00:00
|
|
|
use crate::args::StorageKeyResolver;
|
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::ops;
|
|
|
|
use crate::tools;
|
|
|
|
use crate::tools::coverage::CoverageCollector;
|
2022-11-28 22:28:54 +00:00
|
|
|
use crate::util::checksum;
|
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-04-27 14:05:20 +00:00
|
|
|
pub struct CliMainWorkerOptions {
|
|
|
|
pub argv: Vec<String>,
|
|
|
|
pub debug: bool,
|
|
|
|
pub coverage_dir: Option<String>,
|
|
|
|
pub enable_testing_features: bool,
|
|
|
|
pub has_node_modules_dir: bool,
|
|
|
|
pub inspect_brk: bool,
|
|
|
|
pub inspect_wait: bool,
|
|
|
|
pub is_inspecting: bool,
|
|
|
|
pub is_npm_main: bool,
|
|
|
|
pub location: Option<Url>,
|
|
|
|
pub maybe_binary_npm_command_name: Option<String>,
|
2023-05-01 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,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SharedWorkerState {
|
2023-05-01 12:59:38 +00:00
|
|
|
options: CliMainWorkerOptions,
|
|
|
|
storage_key_resolver: StorageKeyResolver,
|
|
|
|
npm_resolver: Arc<CliNpmResolver>,
|
|
|
|
node_resolver: Arc<NodeResolver>,
|
|
|
|
has_node_specifier_checker: Box<dyn HasNodeSpecifierChecker>,
|
|
|
|
blob_store: BlobStore,
|
|
|
|
broadcast_channel: InMemoryBroadcastChannel,
|
|
|
|
shared_array_buffer_store: SharedArrayBufferStore,
|
|
|
|
compiled_wasm_module_store: CompiledWasmModuleStore,
|
|
|
|
module_loader_factory: Box<dyn ModuleLoaderFactory>,
|
|
|
|
root_cert_store: RootCertStore,
|
|
|
|
node_fs: Arc<dyn deno_node::NodeFs>,
|
|
|
|
maybe_inspector_server: Option<Arc<InspectorServer>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SharedWorkerState {
|
|
|
|
pub fn should_initialize_node_runtime(&self) -> bool {
|
|
|
|
self.npm_resolver.has_packages()
|
|
|
|
|| self.has_node_specifier_checker.has_node_specifier()
|
|
|
|
|| self.options.is_npm_main
|
|
|
|
}
|
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?;
|
|
|
|
log::debug!("main_module {}", self.main_module);
|
|
|
|
|
2022-09-03 16:19:30 +00:00
|
|
|
if self.is_main_cjs {
|
2023-03-20 04:40:21 +00:00
|
|
|
self.initialize_main_module_for_node()?;
|
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 {
|
|
|
|
self
|
|
|
|
.worker
|
|
|
|
.run_event_loop(maybe_coverage_collector.is_none())
|
|
|
|
.await?;
|
|
|
|
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
|
|
|
|
.with_event_loop(coverage_collector.stop_collecting().boxed_local())
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
|
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> {
|
2023-05-01 12:59:38 +00:00
|
|
|
if self.shared.should_initialize_node_runtime() {
|
2023-03-20 04:40:21 +00:00
|
|
|
self.initialize_main_module_for_node()?;
|
2022-08-20 15:31:33 +00:00
|
|
|
}
|
|
|
|
self.worker.evaluate_module(id).await
|
|
|
|
}
|
|
|
|
|
2023-03-20 04:40:21 +00:00
|
|
|
fn initialize_main_module_for_node(&mut self) -> Result<(), AnyError> {
|
2023-03-20 18:05:13 +00:00
|
|
|
deno_node::initialize_runtime(
|
|
|
|
&mut self.worker.js_runtime,
|
2023-04-27 14:05:20 +00:00
|
|
|
self.shared.options.has_node_modules_dir,
|
|
|
|
self.shared.options.maybe_binary_npm_command_name.as_deref(),
|
2023-03-20 18:05:13 +00:00
|
|
|
)?;
|
|
|
|
|
2022-08-24 02:01:21 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
.with_event_loop(coverage_collector.start_collecting().boxed_local())
|
|
|
|
.await?;
|
|
|
|
Ok(Some(coverage_collector))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
npm_resolver: Arc<CliNpmResolver>,
|
|
|
|
node_resolver: Arc<NodeResolver>,
|
2023-05-01 12:59:38 +00:00
|
|
|
has_node_specifier_checker: Box<dyn HasNodeSpecifierChecker>,
|
2023-04-27 14:05:20 +00:00
|
|
|
blob_store: BlobStore,
|
2023-05-01 12:59:38 +00:00
|
|
|
module_loader_factory: Box<dyn ModuleLoaderFactory>,
|
2023-04-27 14:05:20 +00:00
|
|
|
root_cert_store: RootCertStore,
|
|
|
|
node_fs: Arc<dyn deno_node::NodeFs>,
|
|
|
|
maybe_inspector_server: Option<Arc<InspectorServer>>,
|
|
|
|
options: CliMainWorkerOptions,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
shared: Arc::new(SharedWorkerState {
|
|
|
|
options,
|
|
|
|
storage_key_resolver,
|
|
|
|
npm_resolver,
|
|
|
|
node_resolver,
|
2023-05-01 12:59:38 +00:00
|
|
|
has_node_specifier_checker,
|
2023-04-27 14:05:20 +00:00
|
|
|
blob_store,
|
|
|
|
broadcast_channel: Default::default(),
|
|
|
|
shared_array_buffer_store: Default::default(),
|
|
|
|
compiled_wasm_module_store: Default::default(),
|
|
|
|
module_loader_factory,
|
|
|
|
root_cert_store,
|
|
|
|
node_fs,
|
|
|
|
maybe_inspector_server,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn create_main_worker(
|
|
|
|
&self,
|
|
|
|
main_module: ModuleSpecifier,
|
|
|
|
permissions: PermissionsContainer,
|
|
|
|
) -> Result<CliMainWorker, AnyError> {
|
|
|
|
self
|
|
|
|
.create_custom_worker(
|
|
|
|
main_module,
|
|
|
|
permissions,
|
|
|
|
vec![],
|
|
|
|
Default::default(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn create_custom_worker(
|
|
|
|
&self,
|
|
|
|
main_module: ModuleSpecifier,
|
|
|
|
permissions: PermissionsContainer,
|
|
|
|
mut custom_extensions: Vec<Extension>,
|
|
|
|
stdio: deno_runtime::deno_io::Stdio,
|
|
|
|
) -> Result<CliMainWorker, AnyError> {
|
|
|
|
let shared = &self.shared;
|
|
|
|
let (main_module, is_main_cjs) = if let Ok(package_ref) =
|
|
|
|
NpmPackageReqReference::from_specifier(&main_module)
|
|
|
|
{
|
|
|
|
shared
|
|
|
|
.npm_resolver
|
|
|
|
.add_package_reqs(vec![package_ref.req.clone()])
|
|
|
|
.await?;
|
|
|
|
let node_resolution =
|
|
|
|
shared.node_resolver.resolve_binary_export(&package_ref)?;
|
|
|
|
let is_main_cjs = matches!(node_resolution, NodeResolution::CommonJs(_));
|
|
|
|
(node_resolution.into_url(), is_main_cjs)
|
|
|
|
} else if shared.options.is_npm_main {
|
|
|
|
let node_resolution =
|
|
|
|
shared.node_resolver.url_to_node_resolution(main_module)?;
|
|
|
|
let is_main_cjs = matches!(node_resolution, NodeResolution::CommonJs(_));
|
|
|
|
(node_resolution.into_url(), is_main_cjs)
|
|
|
|
} else {
|
|
|
|
(main_module, false)
|
|
|
|
};
|
|
|
|
|
2023-05-01 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 web_worker_preload_module_cb =
|
|
|
|
create_web_worker_preload_module_callback(shared);
|
|
|
|
let web_worker_pre_execute_module_cb =
|
|
|
|
create_web_worker_pre_execute_module_callback(shared.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()]))
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut extensions = ops::cli_exts(shared.npm_resolver.clone());
|
|
|
|
extensions.append(&mut custom_extensions);
|
|
|
|
|
|
|
|
let options = WorkerOptions {
|
|
|
|
bootstrap: BootstrapOptions {
|
|
|
|
args: shared.options.argv.clone(),
|
|
|
|
cpu_count: std::thread::available_parallelism()
|
|
|
|
.map(|p| p.get())
|
|
|
|
.unwrap_or(1),
|
|
|
|
debug_flag: shared.options.debug,
|
|
|
|
enable_testing_features: shared.options.enable_testing_features,
|
|
|
|
locale: deno_core::v8::icu::get_language_tag(),
|
|
|
|
location: shared.options.location.clone(),
|
|
|
|
no_color: !colors::use_color(),
|
|
|
|
is_tty: colors::is_tty(),
|
|
|
|
runtime_version: version::deno().to_string(),
|
|
|
|
ts_version: version::TYPESCRIPT.to_string(),
|
|
|
|
unstable: shared.options.unstable,
|
|
|
|
user_agent: version::get_user_agent().to_string(),
|
|
|
|
inspect: shared.options.is_inspecting,
|
|
|
|
},
|
|
|
|
extensions,
|
|
|
|
startup_snapshot: Some(crate::js::deno_isolate_init()),
|
|
|
|
unsafely_ignore_certificate_errors: shared
|
|
|
|
.options
|
|
|
|
.unsafely_ignore_certificate_errors
|
|
|
|
.clone(),
|
|
|
|
root_cert_store: Some(shared.root_cert_store.clone()),
|
|
|
|
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,
|
|
|
|
web_worker_preload_module_cb,
|
|
|
|
web_worker_pre_execute_module_cb,
|
|
|
|
maybe_inspector_server,
|
|
|
|
should_break_on_first_statement: shared.options.inspect_brk,
|
|
|
|
should_wait_for_inspector_session: shared.options.inspect_wait,
|
|
|
|
module_loader,
|
|
|
|
node_fs: Some(shared.node_fs.clone()),
|
|
|
|
npm_resolver: Some(shared.npm_resolver.clone()),
|
|
|
|
get_error_class_fn: Some(&errors::get_error_class_name),
|
|
|
|
cache_storage_dir,
|
|
|
|
origin_storage_dir,
|
|
|
|
blob_store: shared.blob_store.clone(),
|
|
|
|
broadcast_channel: shared.broadcast_channel.clone(),
|
|
|
|
shared_array_buffer_store: Some(shared.shared_array_buffer_store.clone()),
|
|
|
|
compiled_wasm_module_store: Some(
|
|
|
|
shared.compiled_wasm_module_store.clone(),
|
|
|
|
),
|
|
|
|
stdio,
|
|
|
|
};
|
|
|
|
|
|
|
|
let worker = MainWorker::bootstrap_from_options(
|
|
|
|
main_module.clone(),
|
|
|
|
permissions,
|
|
|
|
options,
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(CliMainWorker {
|
|
|
|
main_module,
|
|
|
|
is_main_cjs,
|
|
|
|
worker,
|
|
|
|
shared: shared.clone(),
|
|
|
|
})
|
|
|
|
}
|
2022-08-11 20:59:12 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 16:19:30 +00:00
|
|
|
// TODO(bartlomieju): this callback could have default value
|
|
|
|
// and not be required
|
2022-08-11 20:59:12 +00:00
|
|
|
fn create_web_worker_preload_module_callback(
|
2023-04-27 14:05:20 +00:00
|
|
|
_shared: &Arc<SharedWorkerState>,
|
2022-08-17 01:00:35 +00:00
|
|
|
) -> Arc<WorkerEventCb> {
|
2022-09-03 16:19:30 +00:00
|
|
|
Arc::new(move |worker| {
|
|
|
|
let fut = async move { Ok(worker) };
|
2022-08-11 20:59:12 +00:00
|
|
|
LocalFutureObj::new(Box::new(fut))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-20 15:31:33 +00:00
|
|
|
fn create_web_worker_pre_execute_module_callback(
|
2023-04-27 14:05:20 +00:00
|
|
|
shared: Arc<SharedWorkerState>,
|
2022-08-20 15:31:33 +00:00
|
|
|
) -> Arc<WorkerEventCb> {
|
|
|
|
Arc::new(move |mut worker| {
|
2023-04-27 14:05:20 +00:00
|
|
|
let shared = shared.clone();
|
2022-08-20 15:31:33 +00:00
|
|
|
let fut = async move {
|
|
|
|
// this will be up to date after pre-load
|
2023-05-01 12:59:38 +00:00
|
|
|
if shared.should_initialize_node_runtime() {
|
2023-02-10 15:26:39 +00:00
|
|
|
deno_node::initialize_runtime(
|
2022-12-16 22:41:51 +00:00
|
|
|
&mut worker.js_runtime,
|
2023-04-27 14:05:20 +00:00
|
|
|
shared.options.has_node_modules_dir,
|
2023-03-20 18:05:13 +00:00
|
|
|
None,
|
2023-03-20 04:40:21 +00:00
|
|
|
)?;
|
2022-08-20 15:31:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(worker)
|
|
|
|
};
|
|
|
|
LocalFutureObj::new(Box::new(fut))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
let preload_module_cb = create_web_worker_preload_module_callback(&shared);
|
2022-08-20 15:31:33 +00:00
|
|
|
let pre_execute_module_cb =
|
2023-04-27 14:05:20 +00:00
|
|
|
create_web_worker_pre_execute_module_callback(shared.clone());
|
2022-08-11 20:59:12 +00:00
|
|
|
|
2023-04-27 14:05:20 +00:00
|
|
|
let extensions = ops::cli_exts(shared.npm_resolver.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()]))
|
|
|
|
});
|
|
|
|
|
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-04-27 14:05:20 +00:00
|
|
|
debug_flag: shared.options.debug,
|
|
|
|
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-03-23 22:27:58 +00:00
|
|
|
runtime_version: version::deno().to_string(),
|
2022-08-11 20:59:12 +00:00
|
|
|
ts_version: version::TYPESCRIPT.to_string(),
|
2023-04-27 14:05:20 +00:00
|
|
|
unstable: shared.options.unstable,
|
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,
|
2022-08-11 20:59:12 +00:00
|
|
|
},
|
|
|
|
extensions,
|
2022-11-21 13:36:26 +00:00
|
|
|
startup_snapshot: Some(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-04-27 14:05:20 +00:00
|
|
|
root_cert_store: Some(shared.root_cert_store.clone()),
|
|
|
|
seed: shared.options.seed,
|
2022-08-11 20:59:12 +00:00
|
|
|
create_web_worker_cb,
|
|
|
|
preload_module_cb,
|
2022-08-20 15:31:33 +00:00
|
|
|
pre_execute_module_cb,
|
2022-08-11 20:59:12 +00:00
|
|
|
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-04-27 14:05:20 +00:00
|
|
|
node_fs: Some(shared.node_fs.clone()),
|
|
|
|
npm_resolver: Some(shared.npm_resolver.clone()),
|
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,
|
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 {
|
2023-04-24 23:44:35 +00:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
2022-11-21 13:36:26 +00:00
|
|
|
use super::*;
|
2023-03-13 23:31:03 +00:00
|
|
|
use deno_core::resolve_path;
|
2023-01-15 04:18:58 +00:00
|
|
|
use deno_core::FsModuleLoader;
|
2022-11-21 13:36:26 +00:00
|
|
|
use deno_runtime::deno_broadcast_channel::InMemoryBroadcastChannel;
|
|
|
|
use deno_runtime::deno_web::BlobStore;
|
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-03-26 04:38:45 +00:00
|
|
|
bootstrap: BootstrapOptions::default(),
|
2022-11-21 13:36:26 +00:00
|
|
|
extensions: vec![],
|
|
|
|
startup_snapshot: Some(crate::js::deno_isolate_init()),
|
|
|
|
unsafely_ignore_certificate_errors: None,
|
|
|
|
root_cert_store: None,
|
|
|
|
seed: None,
|
|
|
|
format_js_error_fn: None,
|
|
|
|
source_map_getter: None,
|
|
|
|
web_worker_preload_module_cb: Arc::new(|_| unreachable!()),
|
|
|
|
web_worker_pre_execute_module_cb: Arc::new(|_| unreachable!()),
|
|
|
|
create_web_worker_cb: Arc::new(|_| unreachable!()),
|
|
|
|
maybe_inspector_server: None,
|
|
|
|
should_break_on_first_statement: false,
|
2022-12-12 14:33:30 +00:00
|
|
|
should_wait_for_inspector_session: false,
|
2022-11-21 13:36:26 +00:00
|
|
|
module_loader: Rc::new(FsModuleLoader),
|
2023-04-24 23:44:35 +00:00
|
|
|
node_fs: Some(Arc::new(deno_node::RealFs)),
|
2022-11-21 13:36:26 +00:00
|
|
|
npm_resolver: None,
|
|
|
|
get_error_class_fn: None,
|
|
|
|
cache_storage_dir: None,
|
|
|
|
origin_storage_dir: None,
|
|
|
|
blob_store: BlobStore::default(),
|
|
|
|
broadcast_channel: InMemoryBroadcastChannel::default(),
|
|
|
|
shared_array_buffer_store: None,
|
|
|
|
compiled_wasm_module_store: None,
|
|
|
|
stdio: Default::default(),
|
|
|
|
};
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|