Skip to content

Plugin for loading and executing WebAssembly modules in Fastify.

License

Notifications You must be signed in to change notification settings

Dueen/fastify-wasm

Repository files navigation

fastify-wasm

CI NPM version License neostandard javascript style

A Fastify plugin for loading and executing WebAssembly modules.

Install

npm i fastify-wasm

Usage

This plugin enables you to load and execute WebAssembly modules within a Fastify application. Simply register the plugin and provide the path to your WebAssembly file.

"use strict";

const fastify = require("fastify")
const fastifyWasm = require("fastify-wasm")

const app = fastify()

app.register(fastifyWasm, {
  path: 'path/to/your/wasm',
})

app.listen({ port: 3000 }, (err) => {
  if (err) throw err;
});

TypeScript

This plugin includes TypeScript definitions for better type safety and improved developer experience. Exports from the WebAssembly module can be typed on the FastifyWasmExports interface.

import fastify from "fastify";
import fastifyWasm from "fastify-wasm";

interface FastifyWasmExports {
  add(lhs: number, rhs: number): number
}

const app = fastify();

app.register(fastifyWasm, {
  path: 'path/to/your/wasm',
});

app.get('/', (_req, reply) => {
  // Call the exported function `add` from the wasm module
  const { add } = fastify.wasm.instance.exports
  reply.send({ result: add(2, 2) })
})

app.listen({ port: 3000 }, (err) => {
  if (err) throw err;
});

License

Licensed under MIT.