Code Monkey home page Code Monkey logo

wasabio's Introduction

wasabio

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, // in-memory key-value store for cross worker state storage
	EventEmitter, // lets you make named emitter for cross worker communication
	readFile,
	writeFile,
	appendFile,
	readFileSync,
	writeFileSync,
	appendFileSync,
	// ... Node FS api
} from "wasabio";

Some utility functions are also provided:

// return true if library is initialized
available(): boolean;
// serializes memory to a buffer, sets the thread counter to 0
serialize(memory: WebAssembly.Memory): Uint8Array;
// deserializes buffer to memory, sets the correct buffer size
deserialize(buffer: Uint8Array): WebAssembly.Memory;
// compresses serialized wasabio memory buffer into a zip buffer
compress(buffer: Uint8Array): Promise<Uint8Array>
// decompresses zip buffer into serialized wasabio memory buffer
decompress(buffer: Uint8Array): Promise<Uint8Array>

Initialization

From New Memory

Initialize wasabio on the first thread (usually the main thread) like so:

// main.ts
import { initialize } from "wasabio";
const mem = await initialize();
// some time later
worker.postMessage(mem);

On other threads (workers, frames, etc.) initialize wasabio like so:

// worker.ts
import { initialize } from "wasabio";
addEventListener("message", async ({ data }) => {
	initialize(data, { sync: true }); // sync as an option
});

From Existing Memory

Initialize wasabio on the first thread (usually the main thread) like so:

// main.ts
import { initialize } from "wasabio";
const cold = getSharedArrayBufferFromSomewhere();
const hot = await initialize(cold, { reboot: true });
// some time later
worker.postMessage(hot);

On other threads (workers, frames, etc.) initialize wasabio like so:

// worker.ts
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.

wasabio's People

Contributors

3p3r avatar

Watchers

 avatar  avatar

Forkers

zorse-code

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.