Skip to content

Latest commit

 

History

History
76 lines (54 loc) · 2.35 KB

fsa-to-fs.md

File metadata and controls

76 lines (54 loc) · 2.35 KB

File System Access API to Node fs API

This adapter implements Node's fs-like filesystem API on top of the web File System Access (FSA) API.

This allows you to run Node.js code in browser, for example, run any Node.js package that uses fs module.

Usage

You need to get hold of FileSystemDirectoryHandle and then use convert it to fs-like filesystem.

import { FsaNodeFs } from 'memfs/lib/fsa-to-node';

const fs = new FsaNodeFs(dir);

Now you can use the fs filesystem API to execute any of the Node's fs methods.

await fs.promises.writeFile('/hello.txt', 'Hello World!');

Out ouf the box most asynchronous API methods are supported, including callbacks API, promises API, write stream, and read stream.

Synchronous API

It is possible to use synchronous API, but it requires some extra setup. You need to setup a synchronous filesystem adapter for that. (See sync demo below.)

import { FsaNodeFs, FsaNodeSyncAdapterWorker } from 'memfs/lib/fsa-to-node';

const adapter = await FsaNodeSyncAdapterWorker.start('https://<path>/worker.js', dir);
const fs = new FsaNodeFs(dir, adapter);

Where 'https://<path>/worker.js' is a path to a worker file, which could look like this:

import { FsaNodeSyncWorker } from '../../src/fsa-to-node/worker/FsaNodeSyncWorker';

if (typeof window === 'undefined') {
  const worker = new FsaNodeSyncWorker();
  worker.start();
}

You will also need to run your app through HTTPS and with COI enabled. Using Webpack dev server you can do it like this:

{
  devServer: {
    // HTTPS is required for Atomics and SharedArrayBuffer to work.
    https: true,
    headers: {
      // These two headers are required for Atomics and SharedArrayBuffer to work.
      "Cross-Origin-Opener-Policy": "same-origin",
      "Cross-Origin-Embedder-Policy": "require-corp",
    },
  },
},

Now most of the synchronous API should work, see the sync demo below.

Demos

  • Async API and WriteStream: yarn demo:fsa-to-node-zipfile - Readme
  • Synchronous API: yarn demo:fsa-to-node-sync-tests - Readme