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

promise-based init #366

Closed konsumer closed 10 months ago

konsumer commented 10 months ago

Hi, I love BrowserFS! It's been really useful for WASM/WASI.

In projects where I use it, I always end up adding this, since I work with mostly ES6 modules, and async/await:

import * as BrowserFS from 'browserfs'

const { Buffer } = BrowserFS.BFSRequire('buffer')

const getFs = options => new Promise((resolve, reject) => {
  BrowserFS.configure(options, err => {
    if (err) {
      return reject(err)
    }
    resolve(BrowserFS.BFSRequire('fs'))
  })
})

so I can do things like this:

const fs = await getFs({
  fs: 'OverlayFS',
  options: {
    readable: {
      fs: 'ZipFS',
      options: {
        zipData: Buffer.from(await fetch('myzip.zip').then(r => r.arrayBuffer()))
      }
    },
    writable: {
      fs: 'LocalStorage'
    }
  }
})

would you consider adding this to BrowserFS, so it's built-in? I'd be happy to PR for it.

james-pre commented 10 months ago

@konsumer Thanks for opening an issue! I already added this so you should be able to use it like so:

import { configure, BFSRequire } from 'browserfs'
import { Buffer } from 'buffer';

await configure({
  fs: 'OverlayFS',
  options: {
    readable: {
      fs: 'ZipFS',
      options: {
        zipData: Buffer.from(await fetch('myzip.zip').then(r => r.arrayBuffer()))
      }
    },
    writable: {
      fs: 'LocalStorage'
    }
  }
});

const fs = BFSRequire('fs');

Please read Releases and compatibility going forward.