BrowserFS
BrowserFS copied to clipboard
Some clarity on setup, WorkerFS and in general
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 :-)
- 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
- 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?
- 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. :-)
OK. I think I got it.
- Create any filesystem in Browser or Worker, ie.
BrowserFS.configure({
fs:'InMemory',
}, callbackFn )
it will be "the source", where files are actually kept
- 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
- Attach filesystem
var worker = new Worker('path/to/worker.js')` // in browser or `self` in WebWorker
BrowserFS.FileSystem.WorkerFS.attachRemoteListener(worker)
- 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 ;-)
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.
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.
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.
- 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?
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. ;-)
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
Closing this question since it is stale.
好的。我想我明白了。
- 在浏览器或工作器中创建任何文件系统,即。
BrowserFS.configure({ fs:'InMemory', }, callbackFn )
它将是“源”,文件实际保存的地方
- 在其他浏览上下文(浏览器/Worker)中创建 WorkerFS 文件系统
BrowserFS.configure({ fs: 'WorkerFS', options: { worker: self // `self` in WebWorker or `new Worker('path/to/worker.js')` in browser } }
这将是一个在您附加它时与另一个保持同步的文件系统
- 附加文件系统
var worker = new Worker('path/to/worker.js')` // in browser or `self` in WebWorker BrowserFS.FileSystem.WorkerFS.attachRemoteListener(worker)
- 利润! 将文件保存在一个上下文中(即 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.