Skip to content

Instantly share code, notes, and snippets.

@voluntadpear
Last active April 29, 2023 21:11
Show Gist options
  • Save voluntadpear/23e0aa8c1dac1d1f1dbd7756e592efea to your computer and use it in GitHub Desktop.
Save voluntadpear/23e0aa8c1dac1d1f1dbd7756e592efea to your computer and use it in GitHub Desktop.
Script to inline a WebAssembly module to make it compatible with Netlify edge functions
import { readFileSync, writeFileSync } from 'fs';
function inlineWasm() {
const wasmFile = readFileSync("./module.wasm");
const uInt8Array = new Uint8Array(wasmFile);
const strRep = uInt8Array.join(",");
const output = `export const wasmCode = new Uint8Array([${strRep}])`;
writeFileSync("./wasm.ts", output);
}
inlineWasm();
// Then, to use it from a Netlify edge function
import { wasmCode } from "./wasm.ts";
const wasmModule = new WebAssembly.Module(wasmCode);
const wasmInstance = new WebAssembly.Instance(wasmModule);
const main = wasmInstance.exports.main as CallableFunction;
export default async () => {
return new Response(`The answer is: ${main().toString()}`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment