Skip to content

Commit

Permalink
Handle conditional loading of js wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
Hinton committed Nov 24, 2023
1 parent ba02aa4 commit dc715fb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,8 @@ wasm-bindgen --target nodejs --out-dir languages/js/wasm/node ./target/wasm32-un
# Optimize size
wasm-opt -Os ./languages/js/wasm/bitwarden_wasm_bg.wasm -o ./languages/js/wasm/bitwarden_wasm_bg.wasm
wasm-opt -Os ./languages/js/wasm/node/bitwarden_wasm_bg.wasm -o ./languages/js/wasm/node/bitwarden_wasm_bg.wasm

# Transpile to JS
wasm2js ./languages/js/wasm/bitwarden_wasm_bg.wasm -o ./languages/js/wasm/bitwarden_wasm_bg.wasm.js
npx terser ./languages/js/wasm/bitwarden_wasm_bg.wasm.js -o ./languages/js/wasm/bitwarden_wasm_bg.wasm.js
```
5 changes: 5 additions & 0 deletions languages/js/sdk-client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ export class BitwardenClient {
passwordLogin: {
email: email,
password: password,
kdf: {
pBKDF2: {
iterations: 100_000,
}
},
},
})
);
Expand Down
27 changes: 27 additions & 0 deletions languages/js/wasm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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;
})();

let wasm;

if (supported) {
wasm = await import('./bitwarden_wasm_bg.wasm');
} else {
wasm = await import('./bitwarden_wasm_bg.wasm.js');
}

import { __wbg_set_wasm } from "./bitwarden_wasm_bg.js";
__wbg_set_wasm(wasm);
export * from "./bitwarden_wasm_bg.js";


2 changes: 1 addition & 1 deletion languages/js/wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"node/bitwarden_wasm.js"
],
"main": "node/bitwarden_wasm.js",
"module": "bitwarden_wasm.js",
"module": "index.js",
"types": "bitwarden_wasm.d.ts",
"sideEffects": [
"./bitwarden_wasm.js",
Expand Down

0 comments on commit dc715fb

Please sign in to comment.