Skip to content

Commit

Permalink
Rework the WASM code (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hinton authored Dec 12, 2023
1 parent f17bac3 commit ff5bb1f
Show file tree
Hide file tree
Showing 25 changed files with 807 additions and 4,365 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/generate_schemas.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 # v3.1.3
with:
name: schemas.ts
path: ${{ github.workspace }}/languages/js_webassembly/bitwarden_client/schemas.ts
path: ${{ github.workspace }}/languages/js/sdk-client/src/schemas.ts
if-no-files-found: error

- name: Upload c# schemas artifact
Expand Down
10 changes: 6 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ build/
bld/
[Bb]in/
[Oo]bj/
*.wasm

# Binary files
*.dylib
Expand Down Expand Up @@ -42,6 +43,7 @@ crates/bitwarden-napi/sdk-napi.*.node

# Complied TypeScript client
crates/bitwarden-napi/dist
languages/js/sdk-client/dist/

# Uniffi
languages/swift/BitwardenFFI.xcframework
Expand All @@ -52,11 +54,11 @@ languages/kotlin/sdk/src/main/java/com/bitwarden/sdk/bitwarden_uniffi.kt
languages/kotlin/sdk/src/main/java/com/bitwarden/core/bitwarden.kt

# Schemas
support/schemas
crates/bitwarden-napi/src-ts/bitwarden_client/schemas.ts
languages/csharp/Bitwarden.Sdk/schemas.cs
languages/js_webassembly/bitwarden_client/schemas.ts
languages/python/bitwarden_sdk/schemas.py
languages/cpp/include/schemas.hpp
languages/csharp/Bitwarden.Sdk/schemas.cs
languages/go/schema.go
languages/java/src/main/java/com/bitwarden/sdk/schema
languages/js/sdk-client/src/schemas.ts
languages/python/bitwarden_sdk/schemas.py
support/schemas
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions crates/bitwarden-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
rust-version = "1.57"

[lib]
crate-type = ["cdylib", "rlib"]
crate-type = ["cdylib"]

[dependencies]
console_error_panic_hook = "0.1.7"
Expand All @@ -16,7 +16,10 @@ serde = { version = "1.0.193", features = ["derive"] }
wasm-bindgen = { version = "0.2.89", features = ["serde-serialize"] }
wasm-bindgen-futures = "0.4.39"

bitwarden-json = { path = "../bitwarden-json", features = ["secrets"] }
bitwarden-json = { path = "../bitwarden-json", features = [
"secrets",
"internal",
] }

[dev-dependencies]
wasm-bindgen-test = "0.3.39"
23 changes: 23 additions & 0 deletions crates/bitwarden-wasm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Bitwarden-wasm

Requirements:

- `wasm32-unknown-unknown` rust target.
- `wasm-bindgen-cli` installed.
- `binaryen` installed for `wasm-opt` and `wasm2js`.

```bash
rustup target add wasm32-unknown-unknown
cargo install -f wasm-bindgen-cli
brew install binaryen
```

#### Build

```bash
# dev
./build.sh

# release
./build.sh -r
```
23 changes: 23 additions & 0 deletions crates/bitwarden-wasm/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Move to the root of the repository
cd "$(dirname "$0")"
cd ../../

if [ "$1" != "-r" ]; then
# Dev
cargo build -p bitwarden -p bitwarden-wasm --target wasm32-unknown-unknown --features wasm-bindgen
wasm-bindgen --target bundler --out-dir languages/js/wasm ./target/wasm32-unknown-unknown/debug/bitwarden_wasm.wasm
wasm-bindgen --target nodejs --out-dir languages/js/wasm/node ./target/wasm32-unknown-unknown/debug/bitwarden_wasm.wasm
else
# Release
cargo build -p bitwarden -p bitwarden-wasm --target wasm32-unknown-unknown --features wasm-bindgen --release
wasm-bindgen --target bundler --out-dir languages/js/wasm ./target/wasm32-unknown-unknown/release/bitwarden_wasm.wasm
wasm-bindgen --target nodejs --out-dir languages/js/wasm/node ./target/wasm32-unknown-unknown/release/bitwarden_wasm.wasm
fi

# 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
4 changes: 3 additions & 1 deletion crates/bitwarden/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ secrets = [] # Secrets manager API
internal = [] # Internal testing methods
mobile = ["uniffi", "internal"] # Mobile-specific features

wasm-bindgen = ["chrono/wasmbind"]

[dependencies]
aes = ">=0.8.2, <0.9"
argon2 = { version = ">=0.5.0, <0.6", features = [
Expand Down Expand Up @@ -46,7 +48,7 @@ pbkdf2 = { version = ">=0.12.1, <0.13", default-features = false }
rand = ">=0.8.5, <0.9"
reqwest = { version = ">=0.11, <0.12", features = ["json"] }
rsa = ">=0.9.2, <0.10"
schemars = { version = ">=0.8, <0.9", features = ["uuid1", "chrono"] }
schemars = { version = ">=0.8.9, <0.9", features = ["uuid1", "chrono"] }
serde = { version = ">=1.0, <2.0", features = ["derive"] }
serde_json = ">=1.0.96, <2.0"
serde_qs = ">=0.12.0, <0.13"
Expand Down
File renamed without changes.
Loading

0 comments on commit ff5bb1f

Please sign in to comment.