WebAssembly and SharedArrayBuffer IO. Pronounced "wassabee-yo".
Purpose
wasabio
offers several utility APIs with familiar interfaces, and implemented
to work over a single SharedArrayBuffer and inside a TS-wrapped WASM module.
wasabio
provides infrastructure for multi threaded applications running in web
browsers, enabling safe cross-worker communication and shared state.
wasabio
can be used in bundlers like Webpack to allow for an exchange between
node core modules and its own implementations to seamlessly run in the browser.
Currently wasabio
provides:
- Node's FileSystem API (implemented on top of an in-memory POSIX filesystem)
- LocalStorage API (implemented on top of an in-memory key-value store)
- Node's EventEmitter API (implemented on top of a SharedArrayBuffer)
- Low level Locks, Buses, Channels, and various other utilities
Usage
API
All APIs are spec-compatible with their original counterparts.
note: library needs to be initialized before use, see below.
import {
localStorage,
EventEmitter,
readFile,
writeFile,
appendFile,
readFileSync,
writeFileSync,
appendFileSync,
} from "wasabio";
Some utility functions are also provided:
available(): boolean;
serialize(memory: WebAssembly.Memory): Uint8Array;
deserialize(buffer: Uint8Array): WebAssembly.Memory;
compress(buffer: Uint8Array): Promise<Uint8Array>
decompress(buffer: Uint8Array): Promise<Uint8Array>
Initialization
From New Memory
Initialize wasabio
on the first thread (usually the main thread) like so:
import { initialize } from "wasabio";
const mem = await initialize();
worker.postMessage(mem);
On other threads (workers, frames, etc.) initialize wasabio
like so:
import { initialize } from "wasabio";
addEventListener("message", async ({ data }) => {
initialize(data, { sync: true });
});
From Existing Memory
Initialize wasabio
on the first thread (usually the main thread) like so:
import { initialize } from "wasabio";
const cold = getSharedArrayBufferFromSomewhere();
const hot = await initialize(cold, { reboot: true });
worker.postMessage(hot);
On other threads (workers, frames, etc.) initialize wasabio
like so:
import { initialize } from "wasabio";
addEventListener("message", async ({ data }) => {
initialize(data, { reboot: false });
});
In this case, reboot
signifies that the library is being initialized from cold
storage and thread-local state should be reset.
Webpack Plugin
A Webpack plugin is provided to allow for seamless integration of wasabio
into
any project. The plugin currently supports generating a WASM memory that can be
used to boot the library on the main thread with.
If you are not interested in using the Webpack plugin, you may opt out of its
dependencies being installed by using npm install wasabio --omit=optional
.