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.07k stars 218 forks source link

Mounted IndexedDB file system doesn't support `writeFileSync` #315

Open soapdog opened 3 years ago

soapdog commented 3 years ago

I have a filesystem initialized with

BrowserFS.configure({
    fs: "MountableFileSystem",
    options: {
        "/templates": {
            fs: "ZipFS",
            options: {
                // Wrap as Buffer object.
                zipData: Buffer.from(zipData)
            }
        },
        "/tmp": { fs: "InMemory" },
        "/books": { fs: "IndexedDB", options: { storeName: "books" } },
        "/etc": { fs: "IndexedDB", options: { storeName: "etc"} },
        "/integration": { fs: "IndexedDB", options: { storeName: "integration" } }

    }
}

I can read and write normally to /tmp and read from /templates but attempts to write using either writeFileSync or writeFile to any of the IndexedDB paths fail with error:

errno: 95, code: "ENOTSUP"

I'm building with rollup, and I didn't expected this to fail. Trying to use "LocalStorage" as a backend works but since I'm writing large files, I'd rather use IndexedDB.

soapdog commented 3 years ago

I've created a jsfiddle with a minimal example: https://jsfiddle.net/24vfryqs/ showing that the IndexedDB backend is not working as expected.

The source is:

try {
BrowserFS.install(window);

BrowserFS.configure(
  {
    fs: "MountableFileSystem",
    options: {
      "/tmp": { fs: "InMemory" },
      "/home": { fs: "IndexedDB", options: { storeName: "home"} },
    },
  },
  function (e) {
    if (e) {
      // An error occurred.
      console.log("error has happened", e)
      throw e
    }
    // Otherwise, BrowserFS is ready to use!
    var fs = require("fs")
    fs.writeFile(
      "/tmp/test.txt",
      "Cool, I can do this in the browser!",
      function (err) {
        fs.readFile("/tmp/test.txt", function (err, contents) {
          console.log(contents.toString())
        })
      }
    )

    fs.writeFile(
      "/home/test.txt",
      "Cool, I can do this in the indexeddb!",
      function (err) {
        if (err) {
            throw err
        }
        fs.readFile("/home/test.txt", function (err, contents) {
          console.log(contents.toString())
        })
      }
    )
  }
)
} catch(e) {
    console.log(e.message)
}
cgc commented 3 years ago

Wonder if the issue might be because you're using version 2.0.0? 1.4.3 is the last non-beta release on NPM, and changing the fiddle's cdnjs link from 2.0.0 to 1.4.3 seems to result in correct behavior.

mathiscode commented 2 years ago

@soapdog Did you ever get this fixed? I have the same problem using 1.4.3 from npm.

soapdog commented 2 years ago

@mathiscode no, I never got this to work. In the end, I realised my app could be stateless and removed all the persistence code from it. I still use BrowserFS in it, but it is all in-memory storage.

james-pre commented 1 year ago

@soapdog

Does it work when using the latest dev build of BrowserFS? (you will need to build from source)

james-pre commented 11 months ago

Please use https://github.com/zen-fs/core/issues/20