refactor(ext/node): allow injecting NodeFs from CLI (#18829)

This allows providing a `NodeFs` as part of the `WorkerOptions`.
This commit is contained in:
David Sherret 2023-04-24 19:44:35 -04:00 committed by GitHub
parent bb74e75a04
commit aa286fdecb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 1631 additions and 1687 deletions

View file

@ -62,16 +62,18 @@ impl PackageJson {
}
}
pub fn load<Fs: NodeFs>(
pub fn load(
fs: &dyn NodeFs,
resolver: &dyn NpmResolver,
permissions: &mut dyn NodePermissions,
path: PathBuf,
) -> Result<PackageJson, AnyError> {
resolver.ensure_read_permission(permissions, &path)?;
Self::load_skip_read_permission::<Fs>(path)
Self::load_skip_read_permission(fs, path)
}
pub fn load_skip_read_permission<Fs: NodeFs>(
pub fn load_skip_read_permission(
fs: &dyn NodeFs,
path: PathBuf,
) -> Result<PackageJson, AnyError> {
assert!(path.is_absolute());
@ -80,7 +82,7 @@ impl PackageJson {
return Ok(CACHE.with(|cache| cache.borrow()[&path].clone()));
}
let source = match Fs::read_to_string(&path) {
let source = match fs.read_to_string(&path) {
Ok(source) => source,
Err(err) if err.kind() == ErrorKind::NotFound => {
return Ok(PackageJson::empty(path));