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

what is the FS reference in the Emscripten example ? #299

Closed kevzettler closed 4 years ago

kevzettler commented 4 years ago

Hello I am importing BrowserFS into a another typescript project that also targets Emscripten. I am confused on the Emscripten example from the README:

/**
 * Mounts a localStorage-backed file system into the /data folder of Emscripten's file system.
 */
function setupBFS() {
  // Grab the BrowserFS Emscripten FS plugin.
  var BFS = new BrowserFS.EmscriptenFS();
  // Create the folder that we'll turn into a mount point.
  FS.createFolder(FS.root, 'data', true, true);
  // Mount BFS's root folder into the '/data' folder.
  FS.mount(BFS, {root: '/'}, '/data');
}

Where exactly does the FS refrence come from? in:

  FS.createFolder(FS.root, 'data', true, true);
  // Mount BFS's root folder into the '/data' folder.
  FS.mount(BFS, {root: '/'}, '/data');

That implies its some global or polly fill or something. I have thought it might be from BFSRequire and tried this in my code:

    const BFS = new BrowserFS.EmscriptenFS();
    // Mount the file system into Emscripten.
    const FS = BrowserFS.BFSRequire('fs');
    FS.mkdir('/emulator');
    FS.mount(BFS, {root: '/'}, '/emulator');

That fails because mount is not defined on FS

Furthermore this approach fails as well:

    const BFS = new BrowserFS.EmscriptenFS();
    // Mount the file system into Emscripten.
    BFS.mkdir('/emulator');
    BFS.mount(BFS, {root: '/'}, '/emulator');

because mkdir does not exist on BFS. So how do I get this magic FS into my typescript project?

kevzettler commented 4 years ago

I have learned this is a magic global injected by emscripten