filerjs / filer

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

Regression test and fix for issue #773 #774

Closed bcheidemann closed 3 years ago

bcheidemann commented 3 years ago

Adds a test which currently fails, demonstrating issue #773

bcheidemann commented 3 years ago

Let's fix this while you're adding this test.

I think the easiest thing would be to mimic what node does here:

diff --git a/src/filesystem/implementation.js b/src/filesystem/implementation.js
index 3027688..67bc8c0 100644
--- a/src/filesystem/implementation.js
+++ b/src/filesystem/implementation.js
@@ -1873,8 +1873,8 @@ function writeFile(context, path, data, options, callback) {
   if(typeof data === 'number') {
     data = '' + data;
   }
-  if(typeof data === 'string' && options.encoding === 'utf8') {
-    data = Buffer.from(data);
+  if(typeof data === 'string') {
+    data = Buffer.from(data, options.encoding || 'utf8');
   }

   open_file(context, path, flags, function(err, fileNode) {

Node does a bit more work to assert the encoding, but I don't know if we care beyond making sure we set the default encoding to 'utf8' if it's missing.

I've implemented the equivalent of what node is doing, should be good to review now =)

bcheidemann commented 3 years ago

Closes #773