2022-07-11 08:41:56 +00:00
|
|
|
import { defineLazyHandler } from 'h3'
|
2021-11-21 15:58:47 +00:00
|
|
|
|
2022-07-11 08:41:56 +00:00
|
|
|
export default defineLazyHandler(async () => {
|
2021-11-21 15:58:47 +00:00
|
|
|
const { exports: { sum } } = await loadWasmInstance(
|
|
|
|
// @ts-ignore
|
|
|
|
() => import('~/server/wasm/sum.wasm')
|
|
|
|
)
|
|
|
|
|
2022-07-11 08:41:56 +00:00
|
|
|
return (event) => {
|
|
|
|
const { a = 0, b = 0 } = useQuery(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
|
|
|
|
}
|