The WASI class provides the WASI system call API and additional convenience methods for working with WASI-based applications. Each WASI instance represents a distinct environment.
Node.js module
wasi
The 'node:wasi' module provides an implementation of the WebAssembly System Interface (WASI), allowing WASI-compliant WebAssembly modules to run in Node.js with access to file systems, clocks, and other host functions.
It includes WASI class for instantiating and executing WASI modules with configurable filesystem and environment.
Works in Bun
Partially implemented. Specific missing features are not detailed.
class WASI
- readonly wasiImport: Dict<any>
wasiImportis an object that implements the WASI system call API. This object should be passed as thewasi_snapshot_preview1import during the instantiation of aWebAssembly.Instance. - instance: object,): void;
Set up WASI host bindings to
instancewithout callinginitialize()orstart(). This method is useful when the WASI module is instantiated in child threads for sharing the memory across threads.finalizeBindings()requires that eitherinstanceexports aWebAssembly.Memorynamedmemoryor user specify aWebAssembly.Memoryobject inoptions.memory. If thememoryis invalid an exception is thrown.start()andinitialize()will callfinalizeBindings()internally. IffinalizeBindings()is called more than once, an exception is thrown. Return an import object that can be passed to
WebAssembly.instantiate()if no other WASM imports are needed beyond those provided by WASI.If version
unstablewas passed into the constructor it will return:{ wasi_unstable: wasi.wasiImport }If version
preview1was passed into the constructor or no version was specified it will return:{ wasi_snapshot_preview1: wasi.wasiImport }- instance: object): void;
Attempt to initialize
instanceas a WASI reactor by invoking its_initialize()export, if it is present. Ifinstancecontains a_start()export, then an exception is thrown.initialize()requires thatinstanceexports aWebAssembly.Memorynamedmemory. Ifinstancedoes not have amemoryexport an exception is thrown.If
initialize()is called more than once, an exception is thrown. - instance: object): number;
Attempt to begin execution of
instanceas a WASI command by invoking its_start()export. Ifinstancedoes not contain a_start()export, or ifinstancecontains an_initialize()export, then an exception is thrown.start()requires thatinstanceexports aWebAssembly.Memorynamedmemory. Ifinstancedoes not have amemoryexport an exception is thrown.If
start()is called more than once, an exception is thrown.
Type definitions
interface FinalizeBindingsOptions
interface WASIOptions
- args?: readonly string[]
An array of strings that the WebAssembly application will see as command line arguments. The first argument is the virtual path to the WASI command itself.
- env?: object
An object similar to
process.envthat the WebAssembly application will see as its environment. - preopens?: Dict<string>
This object represents the WebAssembly application's sandbox directory structure. The string keys of
preopensare treated as directories within the sandbox. The corresponding values inpreopensare the real paths to those directories on the host machine. - returnOnExit?: boolean
By default, when WASI applications call
__wasi_proc_exit()wasi.start()will return with the exit code specified rather than terminatng the process. Setting this option tofalsewill cause the Node.js process to exit with the specified exit code instead. - version: 'unstable' | 'preview1'
The version of WASI requested. Currently the only supported versions are
'unstable'and'preview1'. This option is mandatory.