A Fastify plugin for loading and executing WebAssembly modules.
npm i fastify-wasm
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;
});
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;
});
Licensed under MIT.