@wasmer/wasi
The Package for using WASI easily from Node and the Browser

WASI

import { WASI } from "@wasmer/wasi"

Constructor

new WASI(wasiConfigObject): WASI
Constructs a new WASI instance.
The wasiConfigObject is as follows:
1
const browserBindings = require("@wasmer/wasi/lib/bindings/browser");
2
// for Node:
3
// const nodeBindings = require("@wasmer/wasi/lib/bindings/node");
4
​
5
let myWASIInstance = new WASI({
6
// OPTIONAL: The pre-opened dirctories
7
preopenDirectories: {},
8
​
9
// OPTIONAL: The environment vars
10
env: {},
11
​
12
// OPTIONAL: The arguments provided
13
args: [],
14
​
15
// OPTIONAL: The environment bindings (fs, path),
16
// useful for using WASI in diferent environments
17
// such as Node.js, Browsers, ...
18
bindings: {
19
// hrtime: (time?: [number, number]) -> number
20
// exit: (code?: number) -> void
21
// kill: (pid: number, signal?: string | number) -> void
22
// randomFillSync: (buffer: Buffer, offset?: number, size?: number) -> Buffer
23
// isTTY: () -> bool
24
// fs: Filesystem (with similar API interface as Node 'fs' module)
25
// path: Path (with similar API Interface as Node 'path' module)
26
...browserBindings // Use `nodeBindings` for Node
27
}
28
});
Copied!
This returns a WASI instance. Please see the Instance properties section to learn about the WASI instance.

Class Properties

Instance Properties

memory

wasiInstance.memory: WebAssembly.Memory
​WebAssembly.memory object, that is a view into the Wasm Module's linear memory.

view

wasiInstance.view: DataView
​DataView object, that is a view into the Wasm Module's linear memory.

FD_MAP

wasiInstance.FD_MAP: Map<number, File>
​Javascript Map, where the key is the file descriptor, and the value is a Javascript File.

exports

wasiInstance.exports: Exports
WASI API to be imported in the importObject on instantiation.

bindings

wasiInstance.bindings: WASIBindings
The bindings for common node like objects, such as fs for filesystem, these should work by default, but are applied depending on the platform. You can view the source code for your respective platform's bindings here.

start

wasiInstance.start(wasmInstance: WebAssembly.Instance): void
Function that takes in a WASI WebAssembly Instance and starts it.

getImports

wasiInstance.getImports(wasmModule: WebAssembly.Module): Exports
Function that returns the map of corresponding imports for the WASI module. It will throw an error in case the wasmModule is not a WASI Module, or it have an incompatible version.
Last modified 2mo ago