Skip to content

Commit

Permalink
fix: load bytecode if possible to support Cloudflare Workers as wasm …
Browse files Browse the repository at this point in the history
…import (#31)
vasco-santos authored Jan 30, 2024
1 parent 95cb375 commit dc15e37
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/lib.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
import load, { create } from "../gen/wasm.js"
import { code, CODE_LENGTH, HEIGHT_SIZE, ROOT_SIZE, MAX_SIZE } from './constant.js'
import {
code,
CODE_LENGTH,
HEIGHT_SIZE,
ROOT_SIZE,
MAX_SIZE,
} from "./constant.js"
export * from "./type.js"
export { code, CODE_LENGTH, HEIGHT_SIZE, ROOT_SIZE, MAX_SIZE }

await load()
// load bytecode in Cloudflare Workers as wasm import
// all other paths are disallowed by embedder
let bytecode
// https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent
export const CLOUDFLARE_WORKERS_NAVIGATOR = "Cloudflare-Workers"
/* c8 ignore start */
try {
if (globalThis.navigator?.userAgent === CLOUDFLARE_WORKERS_NAVIGATOR) {
// playwright tries to bundle imported file when it sees import with
// string literal. We workaround by using variable to import.
const wasm = "../gen/wasm_bg.wasm"
bytecode = (await import(wasm)).default
}
} catch {}
/* c8 ignore stop */

await load(bytecode)

export { create }

0 comments on commit dc15e37

Please sign in to comment.