BrowserFS icon indicating copy to clipboard operation
BrowserFS copied to clipboard

Mounted IndexedDB file system doesn't support `writeFileSync`

Open soapdog opened this issue 3 years ago • 6 comments

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 avatar May 16 '21 21:05 soapdog

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)
}

soapdog avatar May 16 '21 21:05 soapdog

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.

cgc avatar Aug 30 '21 18:08 cgc

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

mathiscode avatar Dec 06 '21 16:12 mathiscode

@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.

soapdog avatar Dec 08 '21 16:12 soapdog

@soapdog

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

james-pre avatar Mar 09 '23 23:03 james-pre

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

james-pre avatar Oct 25 '23 00:10 james-pre