Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PM-13650] Use transpiled JS build #9

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>;
Loading