Skip to content

Commit

Permalink
fix: Avoid top-level await to support next.js 13
Browse files Browse the repository at this point in the history
  • Loading branch information
blaine-arcjet committed Dec 11, 2023
1 parent 69de42b commit 828ba81
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
28 changes: 14 additions & 14 deletions analyze/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,21 @@ async function init(): Promise<WasmAPI | undefined> {
}

if (state === "uninitialized") {
let wasmModule: WebAssembly.Module;
// We use `NEXT_RUNTIME` env var to DCE the Node/Browser code in the `else` block
// possible values: "edge" | "nodejs" | undefined
if (process.env["NEXT_RUNTIME"] === "edge") {
const mod = await import(
// @ts-expect-error
"./wasm/arcjet_analyze_js_req_bg.wasm?module"
);
wasmModule = mod.default;
} else {
const { wasm } = await import("./wasm/arcjet.wasm.js");
wasmModule = await WebAssembly.compile(wasm);
}

try {
let wasmModule: WebAssembly.Module;
// We use `NEXT_RUNTIME` env var to DCE the Node/Browser code in the `else` block
// possible values: "edge" | "nodejs" | undefined
if (process.env["NEXT_RUNTIME"] === "edge") {
const mod = await import(
// @ts-expect-error
"./wasm/arcjet_analyze_js_req_bg.wasm?module"
);
wasmModule = mod.default;
} else {
const { wasm } = await import("./wasm/arcjet.wasm.js");
wasmModule = await WebAssembly.compile(await wasm());
}

await initWasm(wasmModule);
state = "initialized";
} catch (err) {
Expand Down
6 changes: 5 additions & 1 deletion analyze/wasm/arcjet.wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@
* properly support consistent asset bundling techniques.
*/

export const wasm: ArrayBuffer;
/**
* Returns an ArrayBuffer for the Arcjet Wasm binary, decoded from a base64 Data
* URL.
*/
export function wasm(): Promise<ArrayBuffer>;
15 changes: 11 additions & 4 deletions analyze/wasm/arcjet.wasm.js

Large diffs are not rendered by default.

0 comments on commit 828ba81

Please sign in to comment.