Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support for Generic FS for Node Compatibility #27464

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions ext/node/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use deno_core::url::Url;
#[allow(unused_imports)]
use deno_core::v8;
use deno_core::v8::ExternalReference;
use node_resolver::env::NodeResolverEnv;
use node_resolver::errors::ClosestPkgJsonError;
use node_resolver::NpmPackageFolderResolverRc;
use once_cell::sync::Lazy;
Expand Down Expand Up @@ -183,11 +184,11 @@ fn op_node_build_os() -> String {
}

#[derive(Clone)]
pub struct NodeExtInitServices {
pub struct NodeExtInitServices<T: NodeResolverEnv> {
pub node_require_loader: NodeRequireLoaderRc,
pub node_resolver: NodeResolverRc,
pub node_resolver: NodeResolverRc<T>,
pub npm_resolver: NpmPackageFolderResolverRc,
pub pkg_json_resolver: PackageJsonResolverRc,
pub pkg_json_resolver: PackageJsonResolverRc<T>,
}

deno_core::extension!(deno_node,
Expand Down Expand Up @@ -807,16 +808,15 @@ deno_core::extension!(deno_node,
},
);

pub type NodeResolver = node_resolver::NodeResolver<DenoFsNodeResolverEnv>;
pub type NodeResolver<T: NodeResolverEnv> = node_resolver::NodeResolver<T>;
#[allow(clippy::disallowed_types)]
pub type NodeResolverRc =
deno_fs::sync::MaybeArc<node_resolver::NodeResolver<DenoFsNodeResolverEnv>>;
pub type PackageJsonResolver =
node_resolver::PackageJsonResolver<DenoFsNodeResolverEnv>;
pub type NodeResolverRc<T: NodeResolverEnv> =
deno_fs::sync::MaybeArc<node_resolver::NodeResolver<T>>;
pub type PackageJsonResolver<T: NodeResolverEnv> =
node_resolver::PackageJsonResolver<T>;
#[allow(clippy::disallowed_types)]
pub type PackageJsonResolverRc = deno_fs::sync::MaybeArc<
node_resolver::PackageJsonResolver<DenoFsNodeResolverEnv>,
>;
pub type PackageJsonResolverRc<T: NodeResolverEnv> =
deno_fs::sync::MaybeArc<node_resolver::PackageJsonResolver<T>>;

#[derive(Debug)]
pub struct DenoFsNodeResolverEnv {
Expand Down
16 changes: 8 additions & 8 deletions ext/node/ops/require.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ pub fn op_require_is_deno_dir_package(
state: &mut OpState,
#[string] path: String,
) -> bool {
let resolver = state.borrow::<NodeResolverRc>();
let resolver = state.borrow::<NodeResolverRc<_>>();
match deno_path_util::url_from_file_path(&PathBuf::from(path)) {
Ok(specifier) => resolver.in_npm_package(&specifier),
Err(_) => false,
Expand Down Expand Up @@ -427,7 +427,7 @@ where
return Ok(None);
}

let pkg_json_resolver = state.borrow::<PackageJsonResolverRc>();
let pkg_json_resolver = state.borrow::<PackageJsonResolverRc<_>>();
let pkg = pkg_json_resolver
.get_closest_package_json_from_file_path(&PathBuf::from(
parent_path.unwrap(),
Expand Down Expand Up @@ -459,7 +459,7 @@ where

let referrer = deno_core::url::Url::from_file_path(&pkg.path).unwrap();
if let Some(exports) = &pkg.exports {
let node_resolver = state.borrow::<NodeResolverRc>();
let node_resolver = state.borrow::<NodeResolverRc<_>>();
let r = node_resolver.package_exports_resolve(
&pkg.path,
&expansion,
Expand Down Expand Up @@ -526,8 +526,8 @@ where
P: NodePermissions + 'static,
{
let fs = state.borrow::<FileSystemRc>();
let node_resolver = state.borrow::<NodeResolverRc>();
let pkg_json_resolver = state.borrow::<PackageJsonResolverRc>();
let node_resolver = state.borrow::<NodeResolverRc<_>>();
let pkg_json_resolver = state.borrow::<PackageJsonResolverRc<_>>();

let modules_path = PathBuf::from(&modules_path_str);
let modules_specifier = deno_path_util::url_from_file_path(&modules_path)?;
Expand Down Expand Up @@ -596,7 +596,7 @@ pub fn op_require_read_package_scope<P>(
where
P: NodePermissions + 'static,
{
let pkg_json_resolver = state.borrow::<PackageJsonResolverRc>();
let pkg_json_resolver = state.borrow::<PackageJsonResolverRc<_>>();
let package_json_path = PathBuf::from(package_json_path);
if package_json_path.file_name() != Some("package.json".as_ref()) {
// permissions: do not allow reading a non-package.json file
Expand All @@ -621,15 +621,15 @@ where
let referrer_path = PathBuf::from(&referrer_filename);
let referrer_path = ensure_read_permission::<P>(state, &referrer_path)
.map_err(RequireErrorKind::Permission)?;
let pkg_json_resolver = state.borrow::<PackageJsonResolverRc>();
let pkg_json_resolver = state.borrow::<PackageJsonResolverRc<_>>();
let Some(pkg) = pkg_json_resolver
.get_closest_package_json_from_file_path(&referrer_path)?
else {
return Ok(None);
};

if pkg.imports.is_some() {
let node_resolver = state.borrow::<NodeResolverRc>();
let node_resolver = state.borrow::<NodeResolverRc<_>>();
let referrer_url = Url::from_file_path(&referrer_filename).unwrap();
let url = node_resolver.package_imports_resolve(
&request,
Expand Down
Loading