2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-10-25 14:39:00 -04:00
|
|
|
|
2024-10-04 15:55:41 -04:00
|
|
|
use std::borrow::Cow;
|
2023-10-25 14:39:00 -04:00
|
|
|
use std::path::Path;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::serde_json;
|
2024-09-30 09:33:32 -04:00
|
|
|
use deno_resolver::npm::ByonmNpmResolver;
|
|
|
|
use deno_resolver::npm::ByonmNpmResolverCreateOptions;
|
2024-11-14 15:24:25 -05:00
|
|
|
use deno_resolver::npm::CliNpmReqResolver;
|
2024-11-01 12:27:00 -04:00
|
|
|
use deno_runtime::deno_node::DenoFsNodeResolverEnv;
|
2023-10-25 14:39:00 -04:00
|
|
|
use deno_runtime::deno_node::NodePermissions;
|
2024-09-27 15:35:37 -04:00
|
|
|
use deno_runtime::ops::process::NpmProcessStateProvider;
|
2024-11-14 15:24:25 -05:00
|
|
|
use node_resolver::NpmPackageFolderResolver;
|
2023-10-25 14:39:00 -04:00
|
|
|
|
|
|
|
use crate::args::NpmProcessState;
|
|
|
|
use crate::args::NpmProcessStateKind;
|
2024-09-30 09:33:32 -04:00
|
|
|
use crate::resolver::CliDenoResolverFs;
|
2023-10-25 14:39:00 -04:00
|
|
|
|
|
|
|
use super::CliNpmResolver;
|
|
|
|
use super::InnerCliNpmResolverRef;
|
|
|
|
|
2024-09-30 09:33:32 -04:00
|
|
|
pub type CliByonmNpmResolverCreateOptions =
|
2024-11-01 12:27:00 -04:00
|
|
|
ByonmNpmResolverCreateOptions<CliDenoResolverFs, DenoFsNodeResolverEnv>;
|
|
|
|
pub type CliByonmNpmResolver =
|
|
|
|
ByonmNpmResolver<CliDenoResolverFs, DenoFsNodeResolverEnv>;
|
2023-10-25 14:39:00 -04:00
|
|
|
|
2024-09-30 09:33:32 -04:00
|
|
|
// todo(dsherret): the services hanging off `CliNpmResolver` doesn't seem ideal. We should probably decouple.
|
2023-10-25 14:39:00 -04:00
|
|
|
#[derive(Debug)]
|
2024-09-30 09:33:32 -04:00
|
|
|
struct CliByonmWrapper(Arc<CliByonmNpmResolver>);
|
2024-07-09 12:15:03 -04:00
|
|
|
|
2024-09-30 09:33:32 -04:00
|
|
|
impl NpmProcessStateProvider for CliByonmWrapper {
|
2024-07-25 19:08:14 -04:00
|
|
|
fn get_npm_process_state(&self) -> String {
|
|
|
|
serde_json::to_string(&NpmProcessState {
|
|
|
|
kind: NpmProcessStateKind::Byonm,
|
|
|
|
local_node_modules_path: self
|
2024-09-30 09:33:32 -04:00
|
|
|
.0
|
|
|
|
.root_node_modules_dir()
|
2024-07-25 19:08:14 -04:00
|
|
|
.map(|p| p.to_string_lossy().to_string()),
|
|
|
|
})
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-30 09:33:32 -04:00
|
|
|
impl CliNpmResolver for CliByonmNpmResolver {
|
2024-11-14 15:24:25 -05:00
|
|
|
fn into_npm_pkg_folder_resolver(
|
|
|
|
self: Arc<Self>,
|
|
|
|
) -> Arc<dyn NpmPackageFolderResolver> {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn into_npm_req_resolver(self: Arc<Self>) -> Arc<dyn CliNpmReqResolver> {
|
2023-10-25 14:39:00 -04:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-07-25 19:08:14 -04:00
|
|
|
fn into_process_state_provider(
|
|
|
|
self: Arc<Self>,
|
|
|
|
) -> Arc<dyn NpmProcessStateProvider> {
|
2024-09-30 09:33:32 -04:00
|
|
|
Arc::new(CliByonmWrapper(self))
|
2024-07-25 19:08:14 -04:00
|
|
|
}
|
|
|
|
|
2024-11-14 15:24:25 -05:00
|
|
|
fn into_maybe_byonm(self: Arc<Self>) -> Option<Arc<CliByonmNpmResolver>> {
|
|
|
|
Some(self)
|
|
|
|
}
|
|
|
|
|
2023-10-25 14:39:00 -04:00
|
|
|
fn clone_snapshotted(&self) -> Arc<dyn CliNpmResolver> {
|
2024-09-30 09:33:32 -04:00
|
|
|
Arc::new(self.clone())
|
2023-10-25 14:39:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn as_inner(&self) -> InnerCliNpmResolverRef {
|
|
|
|
InnerCliNpmResolverRef::Byonm(self)
|
|
|
|
}
|
|
|
|
|
2024-09-30 09:33:32 -04:00
|
|
|
fn root_node_modules_path(&self) -> Option<&Path> {
|
|
|
|
self.root_node_modules_dir()
|
2023-10-25 14:39:00 -04:00
|
|
|
}
|
|
|
|
|
2024-11-01 12:27:00 -04:00
|
|
|
fn ensure_read_permission<'a>(
|
|
|
|
&self,
|
|
|
|
permissions: &mut dyn NodePermissions,
|
|
|
|
path: &'a Path,
|
|
|
|
) -> Result<Cow<'a, Path>, AnyError> {
|
|
|
|
if !path
|
|
|
|
.components()
|
|
|
|
.any(|c| c.as_os_str().to_ascii_lowercase() == "node_modules")
|
|
|
|
{
|
2024-11-04 12:17:21 -05:00
|
|
|
permissions.check_read_path(path).map_err(Into::into)
|
2024-11-01 12:27:00 -04:00
|
|
|
} else {
|
|
|
|
Ok(Cow::Borrowed(path))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-25 14:39:00 -04:00
|
|
|
fn check_state_hash(&self) -> Option<u64> {
|
|
|
|
// it is very difficult to determine the check state hash for byonm
|
|
|
|
// so we just return None to signify check caching is not supported
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|