forked from denoland/wasmbuild
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.ts
121 lines (110 loc) · 3.5 KB
/
loader.ts
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
export type DecompressCallback = (bytes: Uint8Array) => Uint8Array;
export interface LoaderOptions {
/** The Wasm module's imports. */
imports: WebAssembly.Imports | undefined;
/** A function that caches the Wasm module to a local path so that
* so that a network request isn't required on every load.
*
* Returns an ArrayBuffer with the bytes on download success, but
* cache save failure.
*/
cache?: (
url: URL,
decompress: DecompressCallback | undefined,
) => Promise<URL | Uint8Array>;
}
export class Loader {
#options: LoaderOptions;
#lastLoadPromise:
| Promise<WebAssembly.WebAssemblyInstantiatedSource>
| undefined;
#instantiated: WebAssembly.WebAssemblyInstantiatedSource | undefined;
constructor(options: LoaderOptions) {
this.#options = options;
}
get instance() {
return this.#instantiated?.instance;
}
get module() {
return this.#instantiated?.module;
}
load(
url: URL,
decompress: DecompressCallback | undefined,
): Promise<WebAssembly.WebAssemblyInstantiatedSource> {
if (this.#instantiated) {
return Promise.resolve(this.#instantiated);
} else if (this.#lastLoadPromise == null) {
this.#lastLoadPromise = (async () => {
try {
this.#instantiated = await this.#instantiate(url, decompress);
return this.#instantiated;
} finally {
this.#lastLoadPromise = undefined;
}
})();
}
return this.#lastLoadPromise;
}
async #instantiate(url: URL, decompress: DecompressCallback | undefined) {
const imports = this.#options.imports;
if (this.#options.cache != null && url.protocol !== "file:") {
try {
const result = await this.#options.cache(
url,
decompress ?? ((bytes) => bytes),
);
if (result instanceof URL) {
url = result;
decompress = undefined; // already decompressed
} else if (result != null) {
return WebAssembly.instantiate(result, imports);
}
} catch {
// ignore if caching ever fails (ex. when on deploy)
}
}
const isFile = url.protocol === "file:";
// make file urls work in Node via dnt
const isNode = (globalThis as any).process?.versions?.node != null;
if (isFile && typeof Deno !== "object") {
throw new Error(
"Loading local files are not supported in this environment",
);
}
if (isNode && isFile) {
// the deno global will be shimmed by dnt
const wasmCode = await Deno.readFile(url);
return WebAssembly.instantiate(
decompress ? decompress(wasmCode) : wasmCode,
imports,
);
}
switch (url.protocol) {
case "file:":
case "https:":
case "http:": {
const wasmResponse = await fetch(url);
if (decompress) {
const wasmCode = new Uint8Array(await wasmResponse.arrayBuffer());
return WebAssembly.instantiate(decompress(wasmCode), imports);
}
if (
isFile ||
wasmResponse.headers.get("content-type")?.toLowerCase()
.startsWith("application/wasm")
) {
return WebAssembly.instantiateStreaming(wasmResponse, imports);
} else {
return WebAssembly.instantiate(
await wasmResponse.arrayBuffer(),
imports,
);
}
}
default:
throw new Error(`Unsupported protocol: ${url.protocol}`);
}
}
}