Skip to content

Commit

Permalink
Load WASM in parallel without blocking the main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
Schniz committed Feb 28, 2022
1 parent f926334 commit 666a13a
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions packages/next/server/web/sandbox/context.ts
@@ -1,6 +1,6 @@
import type { Context } from 'vm'
import { Blob, File, FormData } from 'next/dist/compiled/formdata-node'
import { readFileSync } from 'fs'
import { readFileSync, promises as fs } from 'fs'
import { requireDependencies } from './require'
import { TransformStream } from 'next/dist/compiled/web-streams-polyfill'
import cookie from 'next/dist/compiled/cookie'
Expand Down Expand Up @@ -270,9 +270,15 @@ async function loadWasmBindings(
wasmBindings: WasmBinding[]
): Promise<Record<string, WebAssembly.Module>> {
const modules: Record<string, WebAssembly.Module> = {}
for (const binding of wasmBindings) {
const module = await WebAssembly.compile(readFileSync(binding.filePath))
modules[binding.name] = module
}

await Promise.all(
wasmBindings.map(async (binding) => {
const module = await WebAssembly.compile(
await fs.readFile(binding.filePath)
)
modules[binding.name] = module
})
)

return modules
}

0 comments on commit 666a13a

Please sign in to comment.