@kobakazu0429/wasmer-wasi
TypeScript icon, indicating that this package has built-in type declarations

0.12.0-internalΒ β€’Β PublicΒ β€’Β Published

@wasmer/wasi

Isomorphic Javascript library for interacting with WASI Modules in Node.js and the Browser. πŸ“š

Documentation for Wasmer-JS Stack can be found on the Wasmer Docs.

Table of Contents

Features

@wasmer/wasi 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 { lowerI64Imports } from "@wasmer/wasm-transformer"

import { WasmFs } from "@wasmer/wasmfs";

// Instantiate a new WASI Instance
const wasmFs = new WasmFs();
let wasi = new WASI({
  args: [],
  env: {},
  bindings: {
    // uses browser APIs in the browser, node APIs in node
    ...WASI.defaultBindings,
    fs: wasmFs.fs
  }
});

const startWasiTask = async () => {
  // Fetch our Wasm File
  const response = await fetch("./my-wasi-module.wasm");
  const responseArrayBuffer = await response.arrayBuffer();

  // Instantiate the WebAssembly file
  const wasm_bytes = new Uint8Array(responseArrayBuffer).buffer;
  const lowered_wasm = await lowerI64Imports(wasm_bytes);
  let module = await WebAssembly.compile(lowered_wasm);
  let instance = await WebAssembly.instantiate(module, {
    ...wasi.getImports(module)
  });

  // Start the WebAssembly WASI instance!
  wasi.start(instance);

  // Output what's inside of /dev/stdout!
  const stdout = await wasmFs.getStdOut();
  console.log(stdout);
};
startWasiTask();

Reference API

The Reference API Documentation can be found on the @wasmer/wasi Reference API Wasmer Docs.

Contributing

This project follows the all-contributors specification.

Contributions of any kind are welcome! πŸ‘

Package Sidebar

Install

npm i @kobakazu0429/wasmer-wasi

Weekly Downloads

3

Version

0.12.0-internal

License

MIT

Unpacked Size

299 kB

Total Files

40

Last publish

Collaborators

  • kobakazu0429