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 217 forks source link

Some clarity on setup, WorkerFS and in general #210

Closed ev45ive closed 1 year ago

ev45ive commented 6 years ago

Hello. First - thatnks again for BrowserFS! 👍

Trying to use in webpack. Managed to start it with

// FIX for - https://github.com/jvilk/BrowserFS/issues/201 
  //  process.js:1 Uncaught TypeError: BrowserFS.BFSRequire is not a functio
  module: {
    noParse: /browserfs\.js/
  },

But then reading the docs i got bit confued. I hope getting this cleared for me could help improve the docs for others :-)

  1. When is fs ready
    BrowserFS.configure({
    fs: "LocalStorage"
    }, function(e) {
    if (e) {
      // An error happened!
      throw e;
    }
    // Otherwise, BrowserFS is ready-to-use!
    // <= Should I use filesystem here?
    })
    // <= Or here? Is it synchronous then?? Or I need 
  2. Webpack and configure vs initialize - configuration says configure() in some places, initialize in others. I am using configure like above, and then
    fs.writeFile('/test.txt', 'Cool, I can do this in the browser!', function (err) {
    fs.readFile('/test.txt', function (err, contents) {
    err && console.error(err)
    console.log(contents.toString());
    });

    But contents is null and I get Error code "EIO" errno 5 message "Error: EIO: Initialize BrowserFS with a file system using BrowserFS.initialize(filesystem)"

What is initialize? How to use it. Why i get this error when using configure?

  1. How to use it with WorkerFS. Documentation is confusing here again. https://jvilk.com/browserfs/1.4.1/classes/_backend_workerfs_.workerfs.html

MAIN BROWSER THREAD:

  // Listen for remote file system requests.
  BrowserFS.FileSystem.WorkerFS.attachRemoteListener(webWorkerObject);
``
I was using 

BrowserFS.configure({ fs:'WorkerFS', options{ worker: worker } }) `` How is attachRemoteListener fitting in? Where do I call it? Why do I call it?

WEBWORKER THREAD: `` // Set the remote file system as the root file system. BrowserFS.configure({ fs: "WorkerFS", options: { worker: self }}, function(e) { // Ready! });



If you have a moment to clarify those I would apperciate. Its either I am getting this wrong or documentation could be improved so its  more straightforward. Thanks anyway, will keep trying. :-)
ev45ive commented 6 years ago

OK. I think I got it.

  1. Create any filesystem in Browser or Worker, ie.

    BrowserFS.configure({
      fs:'InMemory',
    }, callbackFn )

    it will be "the source", where files are actually kept

  2. Create WorkerFS filesystem in the other browsing context (Browser / Worker)

    BrowserFS.configure({
    fs: 'WorkerFS',
    options: {
    worker: self   // `self` in WebWorker or `new Worker('path/to/worker.js')` in browser 
    }
    }

    This will be a filesystem that stays in sync with the other one when you Attach it

  3. Attach filesystem

    var worker = new Worker('path/to/worker.js')` // in browser or `self` in WebWorker
    BrowserFS.FileSystem.WorkerFS.attachRemoteListener(worker)
  4. Profit! Save file in one context (ie. Worker)

     var fs = require('fs')
    fs.writeFile('/test.txt', 'Cool, I can do this in the Worker!', callbackFn )

    Read in the other (ie. Browser)

      var fs = require('fs')
      fs.readFile('/test.txt', function (err, contents) {
        err && console.error(err)
        console.log(contents.toString()); // Text from Worker
      });

Trick was to use one filesystem in one BrowserFS, and connect to it from other BrowserFS

Hope It helps someone ;-)

jvilk commented 6 years ago

Yeah! That's the trick. It's hard to explain without a diagram, and as a one-person dev team on this project, it's hard to find time to invest resources into good documentation. I tried my best to make the documentation for WorkerFS useful for folks, but I can see how that isn't sufficient.

amitnovick commented 5 years ago

So I got this error in an unrelated (yet prolific) project and thought it would be useful for other users who come here from a search engine.

My issue was when trying to use CodeSandbox on this URL, when the graphical preview finishes loading it would just display this error with a callback trace:

Error: EIO: Initialize BrowserFS with a file system using BrowserFS.initialize(filesystem)

Referenced here from CodeSandBox issue I created for it.

amitnovick commented 5 years ago

Update: currently the previous error message is gone and instead there is some other error message:

Cannot find module 'emotion/macro' from '/src'

Not sure if this is BrowserFS issue or CodeSandbox issue.

daoxian commented 3 years ago
  1. Profit! Save file in one context (ie. Worker)
     var fs = require('fs')
    fs.writeFile('/test.txt', 'Cool, I can do this in the Worker!', callbackFn )

Are you sure you can use the require keyword in Worker JS file?

ev45ive commented 3 years ago

Are you sure you can use the require keyword in Worker JS file?

I used webpack for both browser and worker bundling. So you can use any module type. require, import, amd, umd.. because webpack. ;-)

milahu commented 1 year ago

Error: EIO: Initialize BrowserFS with a file system using BrowserFS.initialize(filesystem)

i get this error from BrowserFS.configure in production build works in development build

fixed by replacing BrowserFS.configure with BrowserFS.initialize see also https://github.com/jvilk/BrowserFS/issues/308#issuecomment-1345274287

james-pre commented 1 year ago

Closing this question since it is stale.

QingShan-Xu commented 6 months ago

好的。我想我明白了。

  1. 在浏览器或工作器中创建任何文件系统,即。
    BrowserFS.configure({
      fs:'InMemory',
    }, callbackFn )

它将是“源”,文件实际保存的地方

  1. 在其他浏览上下文(浏览器/Worker)中创建 WorkerFS 文件系统
BrowserFS.configure({
  fs: 'WorkerFS',
  options: {
    worker: self   // `self` in WebWorker or `new Worker('path/to/worker.js')` in browser 
  }
}

这将是一个在您附加它时与另一个保持同步的文件系统

  1. 附加文件系统
    var worker = new Worker('path/to/worker.js')` // in browser or `self` in WebWorker
    BrowserFS.FileSystem.WorkerFS.attachRemoteListener(worker)
  1. 利润! 将文件保存在一个上下文中(即 Worker)
     var fs = require('fs')
    fs.writeFile('/test.txt', 'Cool, I can do this in the Worker!', callbackFn )

在其他(即浏览器)中阅读

      var fs = require('fs')
      fs.readFile('/test.txt', function (err, contents) {
        err && console.error(err)
        console.log(contents.toString()); // Text from Worker
      });

技巧是在一个 BrowserFS 中使用一个文件系统,并从其他 BrowserFS 连接到它

希望它对某人有帮助;-)

Hello, do you have any relevant examples? I'm stuck in a bottleneck. After following your instructions, I still get an error and feel distressed.