filerjs / filer

Node-like file system for browsers
BSD 2-Clause "Simplified" License
613 stars 153 forks source link

BUG: unexpected failures when calling fs functions (e.g. writeFile) with empty options object #773

Closed bcheidemann closed 3 years ago

bcheidemann commented 3 years ago

The following code:

fs.writeFile('/a', 'trololol', {}, (err) => {
    // Do something
 });

Throws the error: Error: Uncaught TypeError: buffer.copy is not a function (http://localhost:1234/tests.e31bb0bc.js:17358) at commonjsGlobal.onerror (mocha.947ba722.js:32655:12)

There are some more examples of issues like this but I don't have them to hand at present. I will add them to this bug report when I am able to.

EDIT: I found one similar(ish) issue with fs.readdir. I've opened a separate issue (#775) for this as it appears to be an unrelated issue on initial inspection.

bcheidemann commented 3 years ago

I've created a PR with a failing regression test to illustrate the issue ( PR #774 )

humphd commented 3 years ago

Nice find. I think this is a bug in validate_file_options(), or how we deal with the encoding being missing on the options object:

https://github.com/filerjs/filer/blob/75f2a70c7b8111987f0df6c9105c1b6c65bef3c4/src/filesystem/implementation.js#L1624-L1633

We are returning back the options object untouched, which is probably OK (node does the same), but we fail to then use 'utf8' as our default, like node does in https://github.com/nodejs/node/blob/24fd791184ad0ba27f92ac49b820aec78e491296/lib/fs.js#L1512. I'll comment more in the PR.

bcheidemann commented 3 years ago

Thanks for the pointer - saved me a lot of time!

humphd commented 3 years ago

Fixed by #774.