Skip to content

Commit

Permalink
[PM-13650] Use transpiled JS build
Browse files Browse the repository at this point in the history
  • Loading branch information
dani-garcia committed Oct 23, 2024
1 parent 3800954 commit 576fdb7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
35 changes: 32 additions & 3 deletions languages/js/sdk-internal/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,37 @@
// https://stackoverflow.com/a/47880734
const supported = (() => {
try {
if (typeof WebAssembly === "object" && typeof WebAssembly.instantiate === "function") {
const module = new WebAssembly.Module(
Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00),
);
if (module instanceof WebAssembly.Module) {
return new WebAssembly.Instance(module) instanceof WebAssembly.Instance;
}
}
} catch (e) {}
return false;
})();

import { __wbg_set_wasm } from "./bitwarden_wasm_internal_bg.js";

// In order to support a fallback strategy for web we need to conditionally load the wasm file
export function init(wasm) {
__wbg_set_wasm(wasm);
let loaded_wasm;

export async function init(wasm) {
if (loaded_wasm) {
return;
}

// If the caller provided a wasm module, use it for backwards compatibility.
if (wasm) {
loaded_wasm = wasm;
} else if (supported) {
loaded_wasm = await import("./bitwarden_wasm_internal_bg.wasm");
} else {
loaded_wasm = await import("./bitwarden_wasm_internal_bg.wasm.js");
}

__wbg_set_wasm(loaded_wasm);
}

export * from "./bitwarden_wasm_internal_bg.js";
5 changes: 3 additions & 2 deletions languages/js/sdk-internal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
"node/bitwarden_wasm_internal_bg.wasm",
"node/bitwarden_wasm_internal_bg.wasm.d.ts",
"node/bitwarden_wasm_internal.d.ts",
"node/bitwarden_wasm_internal.js"
"node/bitwarden_wasm_internal.js",
"types.d.ts"
],
"main": "node/bitwarden_wasm_internal.js",
"module": "index.js",
"types": "bitwarden_wasm_internal.d.ts",
"types": "types.d.ts",
"scripts": {},
"sideEffects": [
"./bitwarden_wasm_internal.js"
Expand Down
3 changes: 3 additions & 0 deletions languages/js/sdk-internal/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./bitwarden_wasm_internal";

export function init(module?: any): Promise<void>;

0 comments on commit 576fdb7

Please sign in to comment.