Last active
April 29, 2023 21:11
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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