Nuxt/examples/experimental/wasm/server/api/sum.ts
2022-07-11 10:41:56 +02:00

20 lines
493 B
TypeScript

import { defineLazyHandler } from 'h3'
export default defineLazyHandler(async () => {
const { exports: { sum } } = await loadWasmInstance(
// @ts-ignore
() => import('~/server/wasm/sum.wasm')
)
return (event) => {
const { a = 0, b = 0 } = useQuery(event)
return { sum: sum(a, b) }
}
})
async function loadWasmInstance (importFn, imports = {}) {
const init = await importFn().then(m => m.default || m)
const { instance } = await init(imports)
return instance
}