@wasmer/wasi
Isomorphic Javascript library for interacting with WASI Modules in Node.js and the Browser. 📚
Table of Contents
Features
This project is forked from node-wasi, a Node implementation made by Gus Caplan. 🙏😄
It uses the same API than the future WASI integration in Node, to help transition to it once it becomes available in Node.
However, @wasmer/wasi
is focused on:
- Bringing WASI to an Isomorphic context (Node.js and the Browser) 🖥️
- Make it easy to plug in different filesystems (via wasmfs) 📂
- Make it type-safe using Typescript 👷
- Pure JavaScript implementation (no Native bindings needed) 🚀
- ~ 15KB minified + gzipped 📦
Installation
For instaling @wasmer/wasi
, just run this command in your shell:
npm install --save @wasmer/wasi
Quick Start
This quick start is for browsers. For node, WasmFs is not required
import WASI from "@wasmer/wasi";
import WasmFs from "@wasmer/wasmfs";
const wasmFs = new WasmFs();
let wasi = new WASI({
args: [],
env: {},
bindings: {
...WASI.defaultBindings,
fs: wasmFs.fs
}
});
const startWasiTask = async () => {
const response = await fetch("./my-wasi-module.wasm");
const responseArrayBuffer = await response.arrayBuffer();
const wasm_bytes = new Uint8Array(responseArrayBuffer).buffer;
let { instance } = await WebAssembly.instantiate(wasm_bytes, {
wasi_unstable: wasi.wasiImport
});
wasi.start(instance);
const stdout = await wasmFs.getStdOut();
console.log(stdout);
};
startWasiTask();
For a larger end-to-end example, please see the wasm-terminal package.
Reference API
new WASI(wasiConfigObject)
Constructs a new WASI instance.
The Config object is is as follows:
let myWASIInstance = new WASI({
preopenDirectories: {},
env: {},
args: [],
bindings: {
...WASI.defaultConfig.bindings
}
});
And returns a WASI Instance:
console.log(myWASIInstance);
WASI.defaultBindings
The default bindings for the environment that are set on the bindings
property of the constructor config object. This is useful for use cases like, you want to plugin in your own file system. For example:
const myFs = require("fs");
let wasi = new WASI({
preopenDirectories: {},
env: {},
args: [],
bindings: {
fs: myFs,
...WASI.defaultBindings
}
});
Contributing
This project follows the all-contributors specification.
Contributions of any kind are welcome! 👍