2022-11-07 11:47:38 +00:00
|
|
|
import { defineLazyEventHandler } from 'h3'
|
2021-11-21 15:58:47 +00:00
|
|
|
|
2022-11-07 11:47:38 +00:00
|
|
|
export default defineLazyEventHandler(async () => {
|
2021-11-21 15:58:47 +00:00
|
|
|
const { exports: { sum } } = await loadWasmInstance(
|
2023-04-14 12:53:21 +00:00
|
|
|
// @ts-expect-error TODO: https://github.com/nuxt/nuxt/issues/14131
|
2021-11-21 15:58:47 +00:00
|
|
|
() => import('~/server/wasm/sum.wasm')
|
|
|
|
)
|
|
|
|
|
2022-07-11 08:41:56 +00:00
|
|
|
return (event) => {
|
2022-10-08 09:32:20 +00:00
|
|
|
const { a = 0, b = 0 } = getQuery(event)
|
2021-11-21 15:58:47 +00:00
|
|
|
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
|
|
|
|
}
|