2025-01-01 04:12:39 +09:00
|
|
|
// Copyright 2018-2025 the Deno authors. MIT license.
|
2023-01-13 15:51:32 +08:00
|
|
|
|
2022-08-11 16:59:12 -04:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use deno_ast::ModuleSpecifier;
|
2025-01-08 14:52:32 -08:00
|
|
|
use deno_core::error::CoreError;
|
2022-08-11 16:59:12 -04:00
|
|
|
use deno_core::futures::FutureExt;
|
2023-09-14 16:38:15 +02:00
|
|
|
use deno_core::v8;
|
2022-08-11 16:59:12 -04:00
|
|
|
use deno_core::Extension;
|
2023-11-22 03:45:34 +01:00
|
|
|
use deno_core::PollEventLoopOptions;
|
2025-01-08 14:52:32 -08:00
|
|
|
use deno_error::JsErrorBox;
|
2025-01-15 09:35:46 -05:00
|
|
|
use deno_lib::worker::LibMainWorker;
|
|
|
|
use deno_lib::worker::LibMainWorkerFactory;
|
2025-01-17 15:39:29 -05:00
|
|
|
use deno_lib::worker::ResolveNpmBinaryEntrypointError;
|
2024-06-06 23:37:53 -04:00
|
|
|
use deno_runtime::deno_permissions::PermissionsContainer;
|
2022-08-11 16:59:12 -04:00
|
|
|
use deno_runtime::worker::MainWorker;
|
2024-04-24 15:45:49 -04:00
|
|
|
use deno_runtime::WorkerExecutionMode;
|
2023-04-06 18:46:44 -04:00
|
|
|
use deno_semver::npm::NpmPackageReqReference;
|
2025-01-15 09:35:46 -05:00
|
|
|
use sys_traits::EnvCurrentDir;
|
2023-10-31 01:25:58 +01:00
|
|
|
use tokio::select;
|
2022-08-11 16:59:12 -04:00
|
|
|
|
2024-06-28 17:18:21 -07:00
|
|
|
use crate::args::CliLockfile;
|
2024-12-10 18:24:23 -08:00
|
|
|
use crate::args::NpmCachingStrategy;
|
2025-01-13 17:35:18 -05:00
|
|
|
use crate::npm::installer::NpmInstaller;
|
|
|
|
use crate::npm::installer::PackageCaching;
|
2023-04-27 10:05:20 -04:00
|
|
|
use crate::npm::CliNpmResolver;
|
2024-12-31 11:29:07 -05:00
|
|
|
use crate::sys::CliSys;
|
2023-10-31 01:25:58 +01:00
|
|
|
use crate::util::file_watcher::WatcherCommunicator;
|
|
|
|
use crate::util::file_watcher::WatcherRestartMode;
|
2023-05-01 08:59:38 -04:00
|
|
|
|
2024-02-13 21:52:30 +05:30
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
pub trait HmrRunner: Send + Sync {
|
2025-01-08 14:52:32 -08:00
|
|
|
async fn start(&mut self) -> Result<(), CoreError>;
|
|
|
|
async fn stop(&mut self) -> Result<(), CoreError>;
|
|
|
|
async fn run(&mut self) -> Result<(), CoreError>;
|
2024-02-13 21:52:30 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
pub trait CoverageCollector: Send + Sync {
|
2025-01-17 18:41:52 +01:00
|
|
|
async fn start_collecting(&mut self) -> Result<(), CoreError>;
|
|
|
|
async fn stop_collecting(&mut self) -> Result<(), CoreError>;
|
2024-02-13 21:52:30 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2024-02-13 21:52:30 +05:30
|
|
|
pub create_hmr_runner: Option<CreateHmrRunnerCb>,
|
|
|
|
pub create_coverage_collector: Option<CreateCoverageCollectorCb>,
|
2025-01-15 09:35:46 -05:00
|
|
|
pub default_npm_caching_strategy: NpmCachingStrategy,
|
|
|
|
pub needs_test_modules: bool,
|
2023-05-01 08:59:38 -04:00
|
|
|
}
|
|
|
|
|
2025-01-15 09:35:46 -05:00
|
|
|
/// Data shared between the factory and workers.
|
|
|
|
struct SharedState {
|
|
|
|
pub create_hmr_runner: Option<CreateHmrRunnerCb>,
|
|
|
|
pub create_coverage_collector: Option<CreateCoverageCollectorCb>,
|
|
|
|
pub maybe_file_watcher_communicator: Option<Arc<WatcherCommunicator>>,
|
2023-04-27 10:05:20 -04:00
|
|
|
}
|
|
|
|
|
2022-08-11 16:59:12 -04:00
|
|
|
pub struct CliMainWorker {
|
2025-01-15 09:35:46 -05:00
|
|
|
worker: LibMainWorker,
|
|
|
|
shared: Arc<SharedState>,
|
2022-08-11 16:59:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CliMainWorker {
|
2025-01-15 09:35:46 -05:00
|
|
|
#[inline]
|
2022-08-11 16:59:12 -04:00
|
|
|
pub fn into_main_worker(self) -> MainWorker {
|
2025-01-15 09:35:46 -05:00
|
|
|
self.worker.into_main_worker()
|
2022-08-11 16:59:12 -04:00
|
|
|
}
|
|
|
|
|
2025-01-17 18:41:52 +01:00
|
|
|
pub async fn setup_repl(&mut self) -> Result<(), CoreError> {
|
2022-08-11 16:59:12 -04:00
|
|
|
self.worker.run_event_loop(false).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2025-01-08 14:52:32 -08:00
|
|
|
pub async fn run(&mut self) -> Result<i32, CoreError> {
|
2022-08-11 16:59:12 -04:00
|
|
|
let mut maybe_coverage_collector =
|
|
|
|
self.maybe_setup_coverage_collector().await?;
|
2023-10-31 01:25:58 +01:00
|
|
|
let mut maybe_hmr_runner = self.maybe_setup_hmr_runner().await?;
|
|
|
|
|
2025-01-15 09:35:46 -05:00
|
|
|
log::debug!("main_module {}", self.worker.main_module());
|
2022-08-11 16:59:12 -04:00
|
|
|
|
2025-01-17 15:39:29 -05:00
|
|
|
// WARNING: Remember to update cli/lib/worker.rs to align with
|
|
|
|
// changes made here so that they affect deno_compile as well.
|
2024-11-01 12:27:00 -04:00
|
|
|
self.execute_main_module().await?;
|
2024-04-15 19:08:33 +01:00
|
|
|
self.worker.dispatch_load_event()?;
|
2022-08-11 16:59:12 -04:00
|
|
|
|
|
|
|
loop {
|
2023-10-31 01:25:58 +01:00
|
|
|
if let Some(hmr_runner) = maybe_hmr_runner.as_mut() {
|
|
|
|
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 => {
|
2025-01-08 14:52:32 -08:00
|
|
|
result = hmr_result.map_err(Into::into);
|
2023-10-31 01:25:58 +01:00
|
|
|
},
|
|
|
|
event_loop_result = event_loop_future => {
|
|
|
|
result = event_loop_result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Err(e) = result {
|
2025-01-15 09:35:46 -05:00
|
|
|
self
|
|
|
|
.shared
|
|
|
|
.maybe_file_watcher_communicator
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
2023-10-31 01:25:58 +01:00
|
|
|
.change_restart_mode(WatcherRestartMode::Automatic);
|
|
|
|
return Err(e);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self
|
|
|
|
.worker
|
|
|
|
.run_event_loop(maybe_coverage_collector.is_none())
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
|
2024-04-16 19:15:41 +05:30
|
|
|
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 19:08:33 +01:00
|
|
|
self.worker.dispatch_unload_event()?;
|
2024-04-16 19:15:41 +05:30
|
|
|
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
|
2025-01-15 09:35:46 -05:00
|
|
|
.js_runtime()
|
2023-12-13 08:07:26 -07:00
|
|
|
.with_event_loop_future(
|
2023-11-22 03:45:34 +01:00
|
|
|
coverage_collector.stop_collecting().boxed_local(),
|
2023-12-13 08:07:26 -07:00
|
|
|
PollEventLoopOptions::default(),
|
2023-11-22 03:45:34 +01:00
|
|
|
)
|
2022-08-11 16:59:12 -04:00
|
|
|
.await?;
|
|
|
|
}
|
2023-10-31 01:25:58 +01:00
|
|
|
if let Some(hmr_runner) = maybe_hmr_runner.as_mut() {
|
|
|
|
self
|
|
|
|
.worker
|
2025-01-15 09:35:46 -05:00
|
|
|
.js_runtime()
|
2023-12-13 08:07:26 -07:00
|
|
|
.with_event_loop_future(
|
2023-11-22 03:45:34 +01:00
|
|
|
hmr_runner.stop().boxed_local(),
|
2023-12-13 08:07:26 -07:00
|
|
|
PollEventLoopOptions::default(),
|
2023-11-22 03:45:34 +01:00
|
|
|
)
|
2023-10-31 01:25:58 +01:00
|
|
|
.await?;
|
|
|
|
}
|
2022-08-11 16:59:12 -04:00
|
|
|
|
2023-01-06 03:29:50 +08:00
|
|
|
Ok(self.worker.exit_code())
|
2022-08-11 16:59:12 -04:00
|
|
|
}
|
|
|
|
|
2025-01-17 18:41:52 +01:00
|
|
|
pub async fn run_for_watcher(self) -> Result<(), CoreError> {
|
2022-08-11 16:59:12 -04:00
|
|
|
/// 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.
|
2025-01-17 18:41:52 +01:00
|
|
|
pub async fn execute(&mut self) -> Result<(), CoreError> {
|
2024-11-01 12:27:00 -04:00
|
|
|
self.inner.execute_main_module().await?;
|
2024-04-15 19:08:33 +01: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 12:18:23 +01:00
|
|
|
match self.inner.worker.run_event_loop(false).await {
|
|
|
|
Ok(()) => {}
|
|
|
|
Err(error) => break Err(error),
|
|
|
|
}
|
2024-04-16 19:15:41 +05:30
|
|
|
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 21:52:16 +02:00
|
|
|
result?;
|
2022-08-11 16:59:12 -04:00
|
|
|
|
2024-04-15 19:08:33 +01:00
|
|
|
self.inner.worker.dispatch_unload_event()?;
|
2024-04-16 19:15:41 +05:30
|
|
|
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 19:08:33 +01: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
|
|
|
}
|
|
|
|
|
2025-01-15 09:35:46 -05:00
|
|
|
#[inline]
|
2025-01-08 14:52:32 -08:00
|
|
|
pub async fn execute_main_module(&mut self) -> Result<(), CoreError> {
|
2025-01-15 09:35:46 -05:00
|
|
|
self.worker.execute_main_module().await
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
|
|
|
|
2025-01-15 09:35:46 -05:00
|
|
|
#[inline]
|
2025-01-08 14:52:32 -08:00
|
|
|
pub async fn execute_side_module(&mut self) -> Result<(), CoreError> {
|
2025-01-15 09:35:46 -05:00
|
|
|
self.worker.execute_side_module().await
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
|
|
|
|
2023-10-31 01:25:58 +01:00
|
|
|
pub async fn maybe_setup_hmr_runner(
|
|
|
|
&mut self,
|
2025-01-17 18:41:52 +01:00
|
|
|
) -> Result<Option<Box<dyn HmrRunner>>, CoreError> {
|
2025-01-15 09:35:46 -05:00
|
|
|
let Some(setup_hmr_runner) = self.shared.create_hmr_runner.as_ref() else {
|
2024-02-13 21:52:30 +05:30
|
|
|
return Ok(None);
|
|
|
|
};
|
2023-10-31 01:25:58 +01:00
|
|
|
|
2024-03-11 23:48:00 -04:00
|
|
|
let session = self.worker.create_inspector_session();
|
2024-02-13 21:52:30 +05:30
|
|
|
|
|
|
|
let mut hmr_runner = setup_hmr_runner(session);
|
2023-10-31 01:25:58 +01:00
|
|
|
|
|
|
|
self
|
|
|
|
.worker
|
2025-01-15 09:35:46 -05:00
|
|
|
.js_runtime()
|
2023-12-13 08:07:26 -07:00
|
|
|
.with_event_loop_future(
|
2023-11-22 03:45:34 +01:00
|
|
|
hmr_runner.start().boxed_local(),
|
2023-12-13 08:07:26 -07:00
|
|
|
PollEventLoopOptions::default(),
|
2023-11-22 03:45:34 +01:00
|
|
|
)
|
2023-10-31 01:25:58 +01:00
|
|
|
.await?;
|
|
|
|
Ok(Some(hmr_runner))
|
|
|
|
}
|
|
|
|
|
2024-02-13 21:52:30 +05:30
|
|
|
pub async fn maybe_setup_coverage_collector(
|
|
|
|
&mut self,
|
2025-01-17 18:41:52 +01:00
|
|
|
) -> Result<Option<Box<dyn CoverageCollector>>, CoreError> {
|
2024-02-13 21:52:30 +05:30
|
|
|
let Some(create_coverage_collector) =
|
2025-01-15 09:35:46 -05:00
|
|
|
self.shared.create_coverage_collector.as_ref()
|
2024-02-13 21:52:30 +05:30
|
|
|
else {
|
|
|
|
return Ok(None);
|
|
|
|
};
|
|
|
|
|
2024-03-11 23:48:00 -04:00
|
|
|
let session = self.worker.create_inspector_session();
|
2024-02-13 21:52:30 +05:30
|
|
|
let mut coverage_collector = create_coverage_collector(session);
|
|
|
|
self
|
|
|
|
.worker
|
2025-01-15 09:35:46 -05:00
|
|
|
.js_runtime()
|
2024-02-13 21:52:30 +05:30
|
|
|
.with_event_loop_future(
|
|
|
|
coverage_collector.start_collecting().boxed_local(),
|
|
|
|
PollEventLoopOptions::default(),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
Ok(Some(coverage_collector))
|
|
|
|
}
|
|
|
|
|
2023-09-14 16:38:15 +02:00
|
|
|
pub fn execute_script_static(
|
|
|
|
&mut self,
|
|
|
|
name: &'static str,
|
|
|
|
source_code: &'static str,
|
2025-01-08 14:52:32 -08:00
|
|
|
) -> Result<v8::Global<v8::Value>, CoreError> {
|
2025-01-15 09:35:46 -05:00
|
|
|
self.worker.js_runtime().execute_script(name, source_code)
|
2023-09-14 16:38:15 +02:00
|
|
|
}
|
2022-08-11 16:59:12 -04:00
|
|
|
}
|
|
|
|
|
2025-01-17 18:41:52 +01:00
|
|
|
#[derive(Debug, thiserror::Error, deno_error::JsError)]
|
|
|
|
pub enum CreateCustomWorkerError {
|
|
|
|
#[class(inherit)]
|
|
|
|
#[error(transparent)]
|
|
|
|
Io(#[from] std::io::Error),
|
|
|
|
#[class(inherit)]
|
|
|
|
#[error(transparent)]
|
|
|
|
Core(#[from] CoreError),
|
|
|
|
#[class(inherit)]
|
|
|
|
#[error(transparent)]
|
|
|
|
ResolvePkgFolderFromDenoReq(
|
|
|
|
#[from] deno_resolver::npm::ResolvePkgFolderFromDenoReqError,
|
|
|
|
),
|
|
|
|
#[class(inherit)]
|
|
|
|
#[error(transparent)]
|
|
|
|
UrlParse(#[from] deno_core::url::ParseError),
|
|
|
|
#[class(inherit)]
|
|
|
|
#[error(transparent)]
|
2025-01-17 15:39:29 -05:00
|
|
|
ResolveNpmBinaryEntrypoint(#[from] ResolveNpmBinaryEntrypointError),
|
2025-01-17 18:41:52 +01:00
|
|
|
#[class(inherit)]
|
|
|
|
#[error(transparent)]
|
|
|
|
NpmPackageReq(JsErrorBox),
|
|
|
|
#[class(inherit)]
|
|
|
|
#[error(transparent)]
|
|
|
|
AtomicWriteFileWithRetries(
|
|
|
|
#[from] crate::args::AtomicWriteFileWithRetriesError,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
2023-04-27 10:05:20 -04:00
|
|
|
pub struct CliMainWorkerFactory {
|
2025-01-15 09:35:46 -05:00
|
|
|
lib_main_worker_factory: LibMainWorkerFactory<CliSys>,
|
|
|
|
maybe_lockfile: Option<Arc<CliLockfile>>,
|
|
|
|
npm_installer: Option<Arc<NpmInstaller>>,
|
|
|
|
npm_resolver: CliNpmResolver,
|
|
|
|
root_permissions: PermissionsContainer,
|
|
|
|
shared: Arc<SharedState>,
|
|
|
|
sys: CliSys,
|
|
|
|
default_npm_caching_strategy: NpmCachingStrategy,
|
|
|
|
needs_test_modules: bool,
|
2022-11-21 14:36:26 +01:00
|
|
|
}
|
|
|
|
|
2023-04-27 10:05:20 -04:00
|
|
|
impl CliMainWorkerFactory {
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
|
|
pub fn new(
|
2025-01-15 09:35:46 -05:00
|
|
|
lib_main_worker_factory: LibMainWorkerFactory<CliSys>,
|
2023-10-31 01:25:58 +01:00
|
|
|
maybe_file_watcher_communicator: Option<Arc<WatcherCommunicator>>,
|
2024-06-28 17:18:21 -07:00
|
|
|
maybe_lockfile: Option<Arc<CliLockfile>>,
|
2025-01-13 17:35:18 -05:00
|
|
|
npm_installer: Option<Arc<NpmInstaller>>,
|
2025-01-14 10:01:05 -05:00
|
|
|
npm_resolver: CliNpmResolver,
|
2024-12-31 11:29:07 -05:00
|
|
|
sys: CliSys,
|
2024-08-30 17:58:24 -04:00
|
|
|
options: CliMainWorkerOptions,
|
2025-01-15 09:35:46 -05:00
|
|
|
root_permissions: PermissionsContainer,
|
2023-04-27 10:05:20 -04:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
2025-01-15 09:35:46 -05:00
|
|
|
lib_main_worker_factory,
|
|
|
|
maybe_lockfile,
|
|
|
|
npm_installer,
|
|
|
|
npm_resolver,
|
|
|
|
root_permissions,
|
|
|
|
sys,
|
|
|
|
shared: Arc::new(SharedState {
|
|
|
|
create_hmr_runner: options.create_hmr_runner,
|
|
|
|
create_coverage_collector: options.create_coverage_collector,
|
2023-10-31 01:25:58 +01:00
|
|
|
maybe_file_watcher_communicator,
|
2023-04-27 10:05:20 -04:00
|
|
|
}),
|
2025-01-15 09:35:46 -05:00
|
|
|
default_npm_caching_strategy: options.default_npm_caching_strategy,
|
|
|
|
needs_test_modules: options.needs_test_modules,
|
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,
|
2025-01-17 18:41:52 +01:00
|
|
|
) -> Result<CliMainWorker, CreateCustomWorkerError> {
|
2023-04-27 10:05:20 -04:00
|
|
|
self
|
|
|
|
.create_custom_worker(
|
2024-04-24 15:45:49 -04:00
|
|
|
mode,
|
2023-04-27 10:05:20 -04:00
|
|
|
main_module,
|
2025-01-15 09:35:46 -05:00
|
|
|
self.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-03 00:40:27 +01:00
|
|
|
custom_extensions: Vec<Extension>,
|
2023-04-27 10:05:20 -04:00
|
|
|
stdio: deno_runtime::deno_io::Stdio,
|
2025-01-17 18:41:52 +01:00
|
|
|
) -> Result<CliMainWorker, CreateCustomWorkerError> {
|
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)
|
|
|
|
{
|
2025-01-15 09:35:46 -05:00
|
|
|
if let Some(npm_installer) = &self.npm_installer {
|
2024-12-10 18:24:23 -08:00
|
|
|
let reqs = &[package_ref.req().clone()];
|
2025-01-13 17:35:18 -05:00
|
|
|
npm_installer
|
2024-12-10 18:24:23 -08:00
|
|
|
.add_package_reqs(
|
|
|
|
reqs,
|
|
|
|
if matches!(
|
2025-01-15 09:35:46 -05:00
|
|
|
self.default_npm_caching_strategy,
|
2024-12-10 18:24:23 -08:00
|
|
|
NpmCachingStrategy::Lazy
|
|
|
|
) {
|
2025-01-13 17:35:18 -05:00
|
|
|
PackageCaching::Only(reqs.into())
|
2024-12-10 18:24:23 -08:00
|
|
|
} else {
|
2025-01-13 17:35:18 -05:00
|
|
|
PackageCaching::All
|
2024-12-10 18:24:23 -08:00
|
|
|
},
|
|
|
|
)
|
2025-01-17 18:41:52 +01:00
|
|
|
.await
|
|
|
|
.map_err(CreateCustomWorkerError::NpmPackageReq)?;
|
2023-09-29 09:26:25 -04:00
|
|
|
}
|
2023-10-03 19:05:06 -04:00
|
|
|
|
|
|
|
// use a fake referrer that can be used to discover the package.json if necessary
|
2025-01-17 18:41:52 +01:00
|
|
|
let referrer =
|
|
|
|
ModuleSpecifier::from_directory_path(self.sys.env_current_dir()?)
|
|
|
|
.unwrap()
|
|
|
|
.join("package.json")?;
|
|
|
|
let package_folder =
|
|
|
|
self.npm_resolver.resolve_pkg_folder_from_deno_module_req(
|
|
|
|
package_ref.req(),
|
|
|
|
&referrer,
|
|
|
|
)?;
|
2025-01-17 15:39:29 -05:00
|
|
|
let main_module =
|
|
|
|
self.lib_main_worker_factory.resolve_npm_binary_entrypoint(
|
|
|
|
&package_folder,
|
|
|
|
package_ref.sub_path(),
|
|
|
|
)?;
|
2023-05-22 16:55:04 -04:00
|
|
|
|
2025-01-15 09:35:46 -05:00
|
|
|
if let Some(lockfile) = &self.maybe_lockfile {
|
2023-05-22 16:55:04 -04:00
|
|
|
// 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 17:18:21 -07: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
|
|
|
};
|
|
|
|
|
2025-01-15 09:35:46 -05:00
|
|
|
let mut worker = self.lib_main_worker_factory.create_custom_worker(
|
|
|
|
mode,
|
|
|
|
main_module,
|
2024-09-29 20:07:50 -04:00
|
|
|
permissions,
|
2025-01-15 09:35:46 -05:00
|
|
|
custom_extensions,
|
2023-04-27 10:05:20 -04:00
|
|
|
stdio,
|
2025-01-15 09:35:46 -05:00
|
|
|
)?;
|
2023-04-27 10:05:20 -04:00
|
|
|
|
2025-01-15 09:35:46 -05:00
|
|
|
if self.needs_test_modules {
|
2024-01-25 14:54:35 -05:00
|
|
|
macro_rules! test_file {
|
|
|
|
($($file:literal),*) => {
|
2025-01-15 09:35:46 -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-05 01:17:39 +00: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",
|
2024-12-21 00:58:03 +01:00
|
|
|
"40_jupyter.js",
|
2024-12-23 08:45:47 +01:00
|
|
|
// TODO(bartlomieju): probably shouldn't include these files here?
|
|
|
|
"40_lint_selector.js",
|
2024-12-21 00:58:03 +01:00
|
|
|
"40_lint.js"
|
2024-01-25 14:54:35 -05:00
|
|
|
);
|
2023-11-14 13:06:00 -08:00
|
|
|
}
|
|
|
|
|
2023-04-27 10:05:20 -04:00
|
|
|
Ok(CliMainWorker {
|
|
|
|
worker,
|
2025-01-15 09:35:46 -05:00
|
|
|
shared: self.shared.clone(),
|
2023-04-27 10:05:20 -04:00
|
|
|
})
|
|
|
|
}
|
2022-08-11 16:59:12 -04:00
|
|
|
}
|
|
|
|
|
2024-05-08 22:45:06 -04:00
|
|
|
#[allow(clippy::print_stdout)]
|
|
|
|
#[allow(clippy::print_stderr)]
|
2022-11-21 14:36:26 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2025-01-15 09:35:46 -05:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
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;
|
2025-01-15 09:35:46 -05:00
|
|
|
use deno_resolver::npm::DenoInNpmPackageChecker;
|
|
|
|
use deno_runtime::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;
|
2025-01-15 09:35:46 -05:00
|
|
|
use deno_runtime::worker::WorkerOptions;
|
|
|
|
use deno_runtime::worker::WorkerServiceOptions;
|
2022-11-21 14:36:26 +01:00
|
|
|
|
2024-12-31 12:13:39 -05:00
|
|
|
use super::*;
|
|
|
|
|
2022-11-21 14:36:26 +01: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);
|
2024-12-31 11:29:07 -05:00
|
|
|
let permission_desc_parser = Arc::new(
|
|
|
|
RuntimePermissionDescriptorParser::new(crate::sys::CliSys::default()),
|
|
|
|
);
|
2022-11-21 14:36:26 +01:00
|
|
|
let options = WorkerOptions {
|
2023-08-06 00:47:15 +01:00
|
|
|
startup_snapshot: crate::js::deno_isolate_init(),
|
2023-05-04 14:28:42 -04:00
|
|
|
..Default::default()
|
2022-11-21 14:36:26 +01:00
|
|
|
};
|
|
|
|
|
2025-01-14 10:01:05 -05:00
|
|
|
MainWorker::bootstrap_from_options::<
|
|
|
|
DenoInNpmPackageChecker,
|
|
|
|
CliNpmResolver,
|
|
|
|
CliSys,
|
|
|
|
>(
|
2025-01-15 09:35:46 -05:00
|
|
|
&main_module,
|
2024-09-29 20:07:50 -04:00
|
|
|
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 14:14:11 +03:30
|
|
|
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 14:36:26 +01: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 14:36:26 +01: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 14:36:26 +01: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 14:36:26 +01: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 14:36:26 +01: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 14:36:26 +01: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 14:36:26 +01: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 14:36:26 +01: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 14:36:26 +01:00
|
|
|
let result = worker.execute_main_module(&module_specifier).await;
|
|
|
|
assert!(result.is_ok());
|
|
|
|
}
|
|
|
|
}
|