forked from web-infra-dev/rspack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add rspack fs node (web-infra-dev#1918)
* feat: init fs node * test: finish node test * chore: update workflow * chore: clippy * chore: tweak feature list
- Loading branch information
Showing
15 changed files
with
315 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
napi_build::setup(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* tslint:disable */ | ||
/* eslint-disable */ | ||
|
||
/* auto-generated by NAPI-RS */ | ||
|
||
export interface NodeFs { | ||
writeFile: (...args: any[]) => any | ||
mkdir: (...args: any[]) => any | ||
mkdirp: (...args: any[]) => any | ||
} | ||
export type TestFS = TestFs | ||
export class TestFs { | ||
constructor(fs: NodeFs) | ||
writeSync(file: string, data: Buffer): void | ||
mkdirSync(file: string): void | ||
mkdirpSync(file: string): void | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "@rspack/fs", | ||
"version": "0.1.0", | ||
"description": "File system utilities for rspack", | ||
"private": "true", | ||
"license": "MIT", | ||
"main": "index.node", | ||
"scripts": { | ||
"build:test": "RUSTFLAGS=\"--cfg node\" napi build", | ||
"test": "jest --verbose" | ||
}, | ||
"devDependencies": { | ||
"@napi-rs/cli": "2.14.2", | ||
"@types/jest": "29.0.2", | ||
"memfs": "3.4.12" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,146 @@ | ||
#![allow(clippy::unwrap_in_result)] | ||
|
||
mod node; | ||
|
||
use std::{marker::PhantomData, path::Path}; | ||
|
||
use napi::Env; | ||
use node::{NodeFS, NodeFSRef, TryIntoNodeFSRef}; | ||
use rspack_fs::{sync::WritableFileSystem, Error, Result}; | ||
|
||
pub struct NodeWritableFileSystem { | ||
env: Env, | ||
fs_ref: NodeFSRef, | ||
_data: PhantomData<*mut ()>, | ||
} | ||
|
||
impl NodeWritableFileSystem { | ||
pub fn new(env: Env, fs: NodeFS) -> napi::Result<Self> { | ||
Ok(Self { | ||
env, | ||
fs_ref: fs.try_into_node_fs_ref(&env)?, | ||
_data: PhantomData, | ||
}) | ||
} | ||
} | ||
|
||
impl WritableFileSystem for NodeWritableFileSystem { | ||
fn create_dir<P: AsRef<Path>>(&self, dir: P) -> Result<()> { | ||
let dir = dir.as_ref().to_string_lossy(); | ||
let mkdir = self.fs_ref.mkdir.get().expect("Failed to get mkdir"); | ||
mkdir | ||
.call( | ||
None, | ||
&[self | ||
.env | ||
.create_string(&dir) | ||
.expect("Failed to create string")], | ||
) | ||
.map_err(|err| { | ||
Error::Io(std::io::Error::new( | ||
std::io::ErrorKind::Other, | ||
err.to_string(), | ||
)) | ||
})?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn create_dir_all<P: AsRef<Path>>(&self, dir: P) -> Result<()> { | ||
let dir = dir.as_ref().to_string_lossy(); | ||
let mkdirp = self.fs_ref.mkdirp.get().expect("Failed to get mkdirp"); | ||
mkdirp | ||
.call( | ||
None, | ||
&[self | ||
.env | ||
.create_string(&dir) | ||
.expect("Failed to create string")], | ||
) | ||
.map_err(|err| { | ||
Error::Io(std::io::Error::new( | ||
std::io::ErrorKind::Other, | ||
err.to_string(), | ||
)) | ||
})?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn write<P: AsRef<Path>, D: AsRef<[u8]>>(&self, file: P, data: D) -> Result<()> { | ||
let file = file.as_ref().to_string_lossy(); | ||
let buf = data.as_ref().to_vec(); | ||
let write_file = self | ||
.fs_ref | ||
.write_file | ||
.get() | ||
.expect("Failed to get write_file"); | ||
|
||
write_file | ||
.call( | ||
None, | ||
&[ | ||
self | ||
.env | ||
.create_string(&file) | ||
.expect("Failed to create string") | ||
.into_unknown(), | ||
self | ||
.env | ||
.create_buffer_with_data(buf) | ||
.expect("Failed to create buffer") | ||
.into_unknown(), | ||
], | ||
) | ||
.map_err(|err| { | ||
Error::Io(std::io::Error::new( | ||
std::io::ErrorKind::Other, | ||
err.to_string(), | ||
)) | ||
})?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(node)] | ||
mod node_test { | ||
use napi::{bindgen_prelude::Buffer, Env}; | ||
use napi_derive::napi; | ||
|
||
use super::*; | ||
|
||
#[napi] | ||
pub struct TestFS { | ||
writable_fs: NodeWritableFileSystem, | ||
} | ||
|
||
#[napi] | ||
impl TestFS { | ||
#[napi(constructor)] | ||
pub fn new(env: Env, fs: NodeFS) -> Self { | ||
Self { | ||
writable_fs: NodeWritableFileSystem::new(env, fs).unwrap(), | ||
} | ||
} | ||
|
||
#[napi] | ||
pub fn write_sync(&self, file: String, data: Buffer) { | ||
self.writable_fs.write(file, data).unwrap(); | ||
} | ||
|
||
#[napi] | ||
pub fn mkdir_sync(&self, file: String) { | ||
self.writable_fs.create_dir(file).unwrap(); | ||
} | ||
|
||
#[napi] | ||
pub fn mkdirp_sync(&self, file: String) { | ||
self.writable_fs.create_dir_all(file).unwrap(); | ||
} | ||
} | ||
} | ||
|
||
#[cfg(node)] | ||
#[doc(hidden)] | ||
pub use node_test::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use napi::{Env, JsFunction, Ref}; | ||
use napi_derive::napi; | ||
|
||
pub(crate) struct JsFunctionRef { | ||
env: Env, | ||
reference: Ref<()>, | ||
} | ||
|
||
impl JsFunctionRef { | ||
fn new(env: Env, f: JsFunction) -> napi::Result<Self> { | ||
Ok(Self { | ||
env, | ||
reference: env.create_reference(f)?, | ||
}) | ||
} | ||
|
||
pub(crate) fn get(&self) -> napi::Result<JsFunction> { | ||
self.env.get_reference_value(&self.reference) | ||
} | ||
} | ||
|
||
impl Drop for JsFunctionRef { | ||
fn drop(&mut self) { | ||
let result = self.reference.unref(self.env); | ||
debug_assert!(result.is_ok()); | ||
} | ||
} | ||
|
||
#[napi(object)] | ||
pub struct NodeFS { | ||
pub write_file: JsFunction, | ||
pub mkdir: JsFunction, | ||
pub mkdirp: JsFunction, | ||
} | ||
|
||
pub(crate) trait TryIntoNodeFSRef { | ||
fn try_into_node_fs_ref(self, env: &Env) -> napi::Result<NodeFSRef>; | ||
} | ||
|
||
impl TryIntoNodeFSRef for NodeFS { | ||
fn try_into_node_fs_ref(self, env: &Env) -> napi::Result<NodeFSRef> { | ||
Ok(NodeFSRef { | ||
write_file: JsFunctionRef::new(*env, self.write_file)?, | ||
mkdir: JsFunctionRef::new(*env, self.mkdir)?, | ||
mkdirp: JsFunctionRef::new(*env, self.mkdirp)?, | ||
}) | ||
} | ||
} | ||
|
||
pub(crate) struct NodeFSRef { | ||
pub(crate) write_file: JsFunctionRef, | ||
pub(crate) mkdir: JsFunctionRef, | ||
pub(crate) mkdirp: JsFunctionRef, | ||
} |
Oops, something went wrong.