Skip to content

Commit bf77daa

Browse files
committed
fix: invalid paratemeter error cb if path is invalid
1 parent 2afceff commit bf77daa

File tree

7 files changed

+42
-7
lines changed

7 files changed

+42
-7
lines changed

dist/virtualfs.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/virtualfs.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@phcode/fs",
33
"description": "Phoenix virtual file system over filer/ browser fs access/ tauri / phoenix web socket APIs",
4-
"version": "2.0.1",
4+
"version": "2.0.2",
55
"keywords": [
66
"phoenix",
77
"browser",

src/fslib.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ const fileSystemLib = {
121121
},
122122
stat: function (...args) { // (path, callback)
123123
let path = args[0];
124+
if(typeof path !== 'string') {
125+
let callback = args[_getFirstFunctionIndex(args)];
126+
return callback(new Errors.EINVAL(`Error Invalid path for stat: ${path}`));
127+
}
124128
if(TauriFS.isTauriSubPath(path)) {
125129
return TauriFS.stat(...args);
126130
}

src/fslib_filer.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ function _processContents(contentsBuffer, encoding, callback, path) {
5959

6060
function readFile(path, options, callback) {
6161
try{
62+
if(typeof path !== 'string') {
63+
return callback(new Errors.EINVAL(`Error Invalid path for readFile: ${path}`));
64+
}
65+
6266
path = globalObject.path.normalize(path);
6367

6468
callback = arguments[arguments.length - 1];
@@ -87,6 +91,9 @@ function writeFile (path, data, options, callback) {
8791
try{
8892
callback = arguments[arguments.length - 1];
8993
options = Utils.validateFileOptions(options, Constants.BINARY_ENCODING, 'w');
94+
if(typeof path !== 'string') {
95+
return callback(new Errors.EINVAL(`Error Invalid path for writeFile: ${path}`));
96+
}
9097

9198
let bufferData;
9299
if(data instanceof ArrayBuffer){

test/test-file.browser.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,30 @@ function _setupTests(testType) {
129129
await _writeTestFile();
130130
});
131131

132+
it(`should phoenix ${testType} writeFile return an error if called with invalid parameters`, async function () {
133+
let resolveP;
134+
const promise = new Promise((resolve) => {resolveP = resolve;});
135+
fs.writeFile(42, _fileContent(), `utf8`, (err)=>{
136+
resolveP(err);
137+
});
138+
const error = await promise;
139+
140+
expect(error.code).to.equal(fs.ERR_CODES.EINVAL);
141+
});
142+
143+
it(`should phoenix ${testType} unlink return an error if called with invalid parameters`, async function () {
144+
let resolveP;
145+
const promise = new Promise((resolve) => {resolveP = resolve;});
146+
147+
fs.unlink(42, (err)=>{
148+
resolveP(err);
149+
});
150+
151+
const error = await promise;
152+
153+
expect(error.code).to.equal(fs.ERR_CODES.EINVAL);
154+
});
155+
132156
it(`Should phoenix ${testType} read in browser`, async function () {
133157
await _writeTestFile();
134158
let resolveP,rejectP;

0 commit comments

Comments
 (0)