Skip to content

Commit

Permalink
fix: cache wasm file locally after first run (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Jun 1, 2023
1 parent c41900b commit fc8a606
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 114 deletions.
54 changes: 22 additions & 32 deletions deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,33 @@
"lock": false,
"tasks": {
"test": "deno test -A",
"build": "deno run -A --no-check --unstable https://deno.land/x/wasmbuild@0.10.4/main.ts --out lib/pkg"
},
"test": {
"exclude": [
"./tests/declaration_import_project/npm",
"./tests/import_map_project/npm",
"./tests/json_module_project/npm",
"./tests/module_mappings_project/npm",
"./tests/node_types_project/npm",
"./tests/package_mappings_project/npm",
"./tests/polyfill_array_find_last_project/npm",
"./tests/polyfill_project/npm",
"./tests/shim_project/npm",
"./tests/test_project/npm",
"./tests/tla_project/npm",
"./tests/undici_project/npm",
"./tests/web_socket_project/npm"
]
},
"fmt": {
"exclude": [
"target/",
"wasm/target/"
]
"build": "deno run -A --no-check --unstable https://deno.land/x/wasmbuild@0.13.0/main.ts --out lib/pkg"
},
"lint": {
"rules": {
"exclude": [
"no-explicit-any",
"camelcase"
]
},
"exclude": [
"target/",
"wasm/target/",
"lib/pkg/",
"rs-lib/src/polyfills/scripts/"
]
}
}
},
"exclude": [
"target/",
"wasm/target/",
"lib/pkg/",
"rs-lib/src/polyfills/scripts/",
"tests/declaration_import_project/npm",
"tests/import_map_project/npm",
"tests/json_module_project/npm",
"tests/module_mappings_project/npm",
"tests/node_types_project/npm",
"tests/package_mappings_project/npm",
"tests/polyfill_array_find_last_project/npm",
"tests/polyfill_project/npm",
"tests/shim_project/npm",
"tests/test_project/npm",
"tests/tla_project/npm",
"tests/undici_project/npm",
"tests/web_socket_project/npm"
]
}
102 changes: 20 additions & 82 deletions lib/pkg/dnt_wasm.generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,13 @@ const imports = {
},
};

import { Loader } from "https://deno.land/x/wasmbuild@0.13.0/loader.ts";
import { cacheToLocalDir } from "https://deno.land/x/wasmbuild@0.13.0/cache.ts";

const loader = new Loader({
imports,
cache: cacheToLocalDir,
});
/**
* Decompression callback
*
Expand All @@ -554,9 +561,6 @@ export async function instantiate(opts) {
return (await instantiateWithInstance(opts)).exports;
}

let instanceWithExports;
let lastLoadPromise;

/** Instantiates an instance of the Wasm module along with its exports.
* @remarks It is safe to call this multiple times and once successfully
* loaded it will always return a reference to the same object.
Expand All @@ -566,28 +570,18 @@ let lastLoadPromise;
* exports: { transform: typeof transform }
* }>}
*/
export function instantiateWithInstance(opts) {
if (instanceWithExports != null) {
return Promise.resolve(instanceWithExports);
}
if (lastLoadPromise == null) {
lastLoadPromise = (async () => {
try {
const instance = (await instantiateModule(opts ?? {})).instance;
wasm = instance.exports;
cachedInt32Memory0 = new Int32Array(wasm.memory.buffer);
cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
instanceWithExports = {
instance,
exports: getWasmInstanceExports(),
};
return instanceWithExports;
} finally {
lastLoadPromise = null;
}
})();
}
return lastLoadPromise;
export async function instantiateWithInstance(opts) {
const { instance } = await loader.load(
opts?.url ?? new URL("dnt_wasm_bg.wasm", import.meta.url),
opts?.decompress,
);
wasm = wasm ?? instance.exports;
cachedInt32Memory0 = cachedInt32Memory0 ?? new Int32Array(wasm.memory.buffer);
cachedUint8Memory0 = cachedUint8Memory0 ?? new Uint8Array(wasm.memory.buffer);
return {
instance,
exports: getWasmInstanceExports(),
};
}

function getWasmInstanceExports() {
Expand All @@ -596,61 +590,5 @@ function getWasmInstanceExports() {

/** Gets if the Wasm module has been instantiated. */
export function isInstantiated() {
return instanceWithExports != null;
}

/**
* @param {InstantiateOptions} opts
*/
async function instantiateModule(opts) {
const wasmUrl = opts.url ?? new URL("dnt_wasm_bg.wasm", import.meta.url);
const decompress = opts.decompress;
const isFile = wasmUrl.protocol === "file:";

// make file urls work in Node via dnt
const isNode = globalThis.process?.versions?.node != null;
if (isNode && isFile) {
// the deno global will be shimmed by dnt
const wasmCode = await Deno.readFile(wasmUrl);
return WebAssembly.instantiate(
decompress ? decompress(wasmCode) : wasmCode,
imports,
);
}

switch (wasmUrl.protocol) {
case "file:":
case "https:":
case "http:": {
if (isFile) {
if (typeof Deno !== "object") {
throw new Error("file urls are not supported in this environment");
}
if ("permissions" in Deno) {
await Deno.permissions.request({ name: "read", path: wasmUrl });
}
} else if (typeof Deno === "object" && "permissions" in Deno) {
await Deno.permissions.request({ name: "net", host: wasmUrl.host });
}
const wasmResponse = await fetch(wasmUrl);
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: ${wasmUrl.protocol}`);
}
return loader.instance != null;
}

0 comments on commit fc8a606

Please sign in to comment.