-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: load bytecode if possible to support Cloudflare Workers as wasm …
…import (#31)
1 parent
95cb375
commit dc15e37
Showing
1 changed file
with
24 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |