Skip to content

Commit

Permalink
feat: standard buffer api compat + callback and streaming apis
Browse files Browse the repository at this point in the history
  • Loading branch information
3p3r committed Apr 8, 2024
1 parent b61be32 commit 1424cca
Show file tree
Hide file tree
Showing 7 changed files with 266 additions and 170 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ dist
.pnp.*

# generated files
src/index.js
target
deps
pkg
Expand Down
105 changes: 105 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@types/karma": "^6.3.8",
"@types/mocha": "^10.0.6",
"@types/node": "^20.11.30",
"@types/typedarray-to-buffer": "^4.0.4",
"@types/webpack": "^5.28.5",
"assert": "^2.1.0",
"atob-lite": "^2.0.0",
Expand All @@ -53,18 +54,22 @@
"karma-jasmine": "^5.1.0",
"karma-mocha": "^2.0.1",
"karma-webpack": "^5.0.1",
"memfs": "^4.8.1",
"mocha": "^10.3.0",
"path-browserify": "^1.0.1",
"prettier": "^3.2.5",
"process": "^0.11.10",
"puppeteer": "^22.6.1",
"stream-browserify": "^3.0.0",
"string-replace-loader": "^3.1.0",
"terser-webpack-plugin": "^5.3.10",
"ts-loader": "^9.5.1",
"ts-mocha": "^10.0.0",
"ts-node": "^10.9.2",
"tsx": "^4.7.1",
"typedarray-to-buffer": "^4.0.0",
"typescript": "^5.4.3",
"url": "^0.11.3",
"url-loader": "^4.1.1",
"web-worker": "^1.3.0",
"webpack": "^5.91.0",
Expand Down
13 changes: 12 additions & 1 deletion src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ pub unsafe fn openSync(
return Ok(fd);
} else {
let err: JsValue = JsError::new("ENOENT: no such file or directory").into();
Reflect::set(&err, &"path".into(), &path.into()).unwrap();
Reflect::set(&err, &"code".into(), &"ENOENT".into()).unwrap();
Reflect::set(&err, &"syscall".into(), &"open".into()).unwrap();
return Err(err);
Expand Down Expand Up @@ -378,6 +379,7 @@ pub unsafe fn readdirSync(
) -> Result<JsValue, JsValue> {
if !existsSync(path.clone()) {
let err: JsValue = JsError::new("ENOENT: no such file or directory").into();
Reflect::set(&err, &"path".into(), &path.into()).unwrap();
Reflect::set(&err, &"code".into(), &"ENOENT".into()).unwrap();
Reflect::set(&err, &"syscall".into(), &"readdir".into()).unwrap();
return Err(err);
Expand Down Expand Up @@ -501,6 +503,7 @@ pub unsafe fn readFileSync(
.to_lowercase();
if !existsSync(path.clone()) {
let err: JsValue = JsError::new("ENOENT: no such file or directory").into();
Reflect::set(&err, &"path".into(), &path.into()).unwrap();
Reflect::set(&err, &"code".into(), &"ENOENT".into()).unwrap();
Reflect::set(&err, &"syscall".into(), &"open".into()).unwrap();
return Err(err);
Expand All @@ -514,7 +517,13 @@ pub unsafe fn readFileSync(
broadcast!(name_of!(readFileSync), path);
let data = lfs::read_file_sync(path.as_str()).unwrap();
let out = match encoding.as_str() {
"utf8" | "utf-8" => JsValue::from(String::from_utf8(data).unwrap()),
"utf8" | "utf-8" => {
if String::from_utf8(data.clone()).is_ok() {
JsValue::from(String::from_utf8(data).unwrap())
} else {
JsValue::from(js_sys::Uint8Array::from(data.as_slice()))
}
}
"buffer" => JsValue::from(js_sys::Uint8Array::from(data.as_slice())),
_ => return Err(JsError::new("unsupported encoding").into()),
};
Expand Down Expand Up @@ -733,6 +742,7 @@ pub unsafe fn statSync(
if !existsSync(path.clone()) {
if throw_if_no_entry {
let err: JsValue = JsError::new("ENOENT: no such file or directory").into();
Reflect::set(&err, &"path".into(), &path.into()).unwrap();
Reflect::set(&err, &"code".into(), &"ENOENT".into()).unwrap();
Reflect::set(&err, &"syscall".into(), &"stat".into()).unwrap();
return Err(err);
Expand Down Expand Up @@ -801,6 +811,7 @@ pub unsafe fn lstatSync(
if !existsSync(path.clone()) {
if throw_if_no_entry {
let err: JsValue = JsError::new("ENOENT: no such file or directory").into();
Reflect::set(&err, &"path".into(), &path.into()).unwrap();
Reflect::set(&err, &"code".into(), &"ENOENT".into()).unwrap();
Reflect::set(&err, &"syscall".into(), &"lstat".into()).unwrap();
return Err(err);
Expand Down
Loading

0 comments on commit 1424cca

Please sign in to comment.