jvilk / BrowserFS

BrowserFS is an in-browser filesystem that emulates the Node JS filesystem API and supports storing and retrieving files from various backends.
Other
3.06k stars 216 forks source link

Unexpected Error in writing File #286

Closed elliott-wen closed 4 years ago

elliott-wen commented 4 years ago

Hi, there.

When I tried to execute the following simple script, it just threw an error.

<script type="text/javascript" src="browserfs.js"></script>
<script type="text/javascript">
  BrowserFS.install(window);
  // Configures BrowserFS to use the LocalStorage file system.
  BrowserFS.configure({
    fs: "LocalStorage"
  }, function(e) {
    if (e) {
      // An error happened!
      throw e;
    } else {
        console.log("Hello world");
        var fs = BrowserFS.BFSRequire('fs');
        var datain = new ArrayBuffer(32);
        fs.writeFile("test.txt", datain, function(e, data){
          console.log(e);
          console.log(data);
         });
    }
  });
</script>

TypeError: e.copy is not a function at e.n.writeSync (browserfs.rollup.js:3339) at e.n.write (browserfs.rollup.js:3282) at browserfs.rollup.js:2853 at e.open (browserfs.rollup.js:2975) at et.writeFile (browserfs.rollup.js:2837) at x.writeFile (browserfs.rollup.js:951) at Object.D..A. [as writeFile] (browserfs.rollup.js:1787) at index.html:32 at passthrough.js:1 at Function.e.Create (passthrough.js:1)

Could you kindly enlighten me what's going on? Have tried 1.4.3. and 2.0.

Only happened when writting typearrays or arraybuffer, works fine for string

elliott-wen commented 4 years ago

It turns out it is not a bug, I needed to wrap the typearray with buffer.from, but I just wonder whether it makes sense to automate this step, since strings are already auto wrapped.

jvilk commented 4 years ago

BrowserFS emulates the Node API, so I thought that it should be clear that you need to use Buffer, not ArrayBuffer. I don't believe the Node API accepts bare ArrayBuffers (does it?). It accepts strings for legacy reasons.

As a result, I am marking this as WONTFIX.