File size: 1,032 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* eslint-disable @typescript-eslint/no-unused-vars */

/// <reference path="../shared/runtime-utils.ts" />

function readWebAssemblyAsResponse(path: string) {
  const { createReadStream } = require('fs') as typeof import('fs')
  const { Readable } = require('stream') as typeof import('stream')

  const stream = createReadStream(path)

  // @ts-ignore unfortunately there's a slight type mismatch with the stream.
  return new Response(Readable.toWeb(stream), {
    headers: {
      'content-type': 'application/wasm',
    },
  })
}

async function compileWebAssemblyFromPath(
  path: string
): Promise<WebAssembly.Module> {
  const response = readWebAssemblyAsResponse(path)

  return await WebAssembly.compileStreaming(response)
}

async function instantiateWebAssemblyFromPath(
  path: string,
  importsObj: WebAssembly.Imports
): Promise<Exports> {
  const response = readWebAssemblyAsResponse(path)

  const { instance } = await WebAssembly.instantiateStreaming(
    response,
    importsObj
  )

  return instance.exports
}