-
Notifications
You must be signed in to change notification settings - Fork 37
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-12967] Create bitwarden-wasm-internal #1089
base: main
Are you sure you want to change the base?
Changes from 6 commits
22ef11f
570c96a
468c461
6c6e7f6
8214f82
0e5f326
48536e7
bb20e46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
--- | ||
name: Build @bitwarden/sdk-internal | ||
|
||
on: | ||
pull_request: | ||
push: | ||
branches: | ||
- "main" | ||
- "rc" | ||
- "hotfix-rc" | ||
workflow_dispatch: | ||
|
||
defaults: | ||
run: | ||
shell: bash | ||
working-directory: crates/bitwarden-wasm-internal | ||
|
||
jobs: | ||
build: | ||
name: Building @bitwarden/sdk-wasm-internal | ||
runs-on: ubuntu-22.04 | ||
|
||
steps: | ||
- name: Checkout repo | ||
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 | ||
|
||
- name: Setup Node | ||
uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4.0.4 | ||
with: | ||
node-version: 20 | ||
registry-url: "https://npm.pkg.github.com" | ||
cache: "npm" | ||
|
||
- name: Install dependencies | ||
run: npm i -g binaryen | ||
|
||
- name: Install rust | ||
uses: dtolnay/rust-toolchain@7b1c307e0dcbda6122208f10795a713336a9b35a # stable | ||
with: | ||
toolchain: stable | ||
targets: wasm32-unknown-unknown | ||
|
||
- name: Cache cargo registry | ||
uses: Swatinem/rust-cache@23bce251a8cd2ffc3c1075eaa2367cf899916d84 # v2.7.3 | ||
with: | ||
key: wasm-cargo-cache | ||
|
||
- name: Install wasm-bindgen-cli | ||
run: cargo install wasm-bindgen-cli | ||
|
||
- name: Build | ||
run: ./build.sh -r | ||
|
||
- name: Upload artifact | ||
uses: actions/upload-artifact@50769540e7f4bd5e21e526ee35c689e35e0d6874 # v4.4.0 | ||
with: | ||
name: sdk-bitwarden-wasm-internal | ||
path: ${{ github.workspace }}/languages/js/sdk-internal/* | ||
if-no-files-found: error | ||
|
||
- name: Set version | ||
if: ${{ github.ref == 'refs/heads/main' }} | ||
# Fetches current version from registry and uses prerelease to bump it | ||
run: | | ||
npm version --no-git-tag-version $(npm view @bitwarden/sdk-internal@latest version) | ||
npm version --no-git-tag-version prerelease | ||
env: | ||
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
working-directory: languages/js/sdk-internal | ||
|
||
- name: Publish NPM | ||
if: ${{ github.ref == 'refs/heads/main' }} | ||
run: npm publish --access public | ||
env: | ||
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
working-directory: languages/js/sdk-internal | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
[package] | ||
name = "bitwarden-wasm-internal" | ||
version = "0.1.0" | ||
publish = false | ||
|
||
authors.workspace = true | ||
edition.workspace = true | ||
rust-version.workspace = true | ||
homepage.workspace = true | ||
repository.workspace = true | ||
license-file.workspace = true | ||
keywords.workspace = true | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
bitwarden = { workspace = true, features = ["internal", "wasm"] } | ||
console_error_panic_hook = "0.1.7" | ||
console_log = { version = "1.0.0", features = ["color"] } | ||
js-sys = "0.3.68" | ||
log = "0.4.20" | ||
serde_json = ">=1.0.96, <2.0" | ||
wasm-bindgen = { version = "0.2.91", features = ["serde-serialize"] } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: should this use the workspace version?
I haven't seen this syntax before, but I saw that you did this in |
||
wasm-bindgen-futures = "0.4.41" | ||
|
||
[lints] | ||
workspace = true |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# bitwarden-wasm-internal | ||
|
||
**Note:** This is only for internal use. Bitwarden will not provide any support for this crate. | ||
|
||
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 | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Move to the root of the repository | ||
cd "$(dirname "$0")" | ||
cd ../../ | ||
|
||
if [ "$1" != "-r" ]; then | ||
# Dev | ||
cargo build -p bitwarden-wasm-internal --target wasm32-unknown-unknown | ||
wasm-bindgen --target bundler --out-dir languages/js/sdk-internal ./target/wasm32-unknown-unknown/debug/bitwarden_wasm_internal.wasm | ||
wasm-bindgen --target nodejs --out-dir languages/js/sdk-internal/node ./target/wasm32-unknown-unknown/debug/bitwarden_wasm_internal.wasm | ||
else | ||
# Release | ||
cargo build -p bitwarden-wasm-internal --target wasm32-unknown-unknown --release | ||
wasm-bindgen --target bundler --out-dir languages/js/sdk-internal ./target/wasm32-unknown-unknown/release/bitwarden_wasm_internal.wasm | ||
wasm-bindgen --target nodejs --out-dir languages/js/sdk-internal/node ./target/wasm32-unknown-unknown/release/bitwarden_wasm_internal.wasm | ||
fi | ||
|
||
# Format | ||
npx prettier --write ./languages/js/sdk-internal | ||
|
||
# Optimize size | ||
wasm-opt -Os ./languages/js/sdk-internal/bitwarden_wasm_internal_bg.wasm -o ./languages/js/sdk-internal/bitwarden_wasm_internal_bg.wasm | ||
wasm-opt -Os ./languages/js/sdk-internal/node/bitwarden_wasm_internal_bg.wasm -o ./languages/js/sdk-internal/node/bitwarden_wasm_internal_bg.wasm | ||
|
||
# Transpile to JS | ||
wasm2js ./languages/js/sdk-internal/bitwarden_wasm_internal_bg.wasm -o ./languages/js/sdk-internal/bitwarden_wasm_internal_bg.wasm.js | ||
npx terser ./languages/js/sdk-internal/bitwarden_wasm_internal_bg.wasm.js -o ./languages/js/sdk-internal/bitwarden_wasm_internal_bg.wasm.js |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
extern crate console_error_panic_hook; | ||
use std::rc::Rc; | ||
|
||
use bitwarden::{Client, ClientSettings}; | ||
use log::{set_max_level, Level}; | ||
use wasm_bindgen::prelude::*; | ||
|
||
#[wasm_bindgen] | ||
pub enum LogLevel { | ||
Trace, | ||
Debug, | ||
Info, | ||
Warn, | ||
Error, | ||
} | ||
|
||
fn convert_level(level: LogLevel) -> Level { | ||
match level { | ||
LogLevel::Trace => Level::Trace, | ||
LogLevel::Debug => Level::Debug, | ||
LogLevel::Info => Level::Info, | ||
LogLevel::Warn => Level::Warn, | ||
LogLevel::Error => Level::Error, | ||
} | ||
} | ||
|
||
// Rc<...> is to avoid needing to take ownership of the Client during our async run_command | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question/issue?: We don't have a |
||
// function https://github.com/rustwasm/wasm-bindgen/issues/2195#issuecomment-799588401 | ||
#[wasm_bindgen] | ||
pub struct BitwardenClient(Rc<Client>); | ||
|
||
#[wasm_bindgen] | ||
impl BitwardenClient { | ||
#[wasm_bindgen(constructor)] | ||
pub fn new(settings: Option<ClientSettings>, log_level: Option<LogLevel>) -> Self { | ||
console_error_panic_hook::set_once(); | ||
let log_level = convert_level(log_level.unwrap_or(LogLevel::Info)); | ||
if let Err(_e) = console_log::init_with_level(log_level) { | ||
set_max_level(log_level.to_level_filter()) | ||
} | ||
|
||
Self(Rc::new(Client::new(settings))) | ||
} | ||
|
||
/// Test method, echoes back the input | ||
pub fn echo(&self, msg: String) -> String { | ||
msg | ||
} | ||
|
||
/// Test method, calls http endpoint | ||
pub async fn http_get(&self, url: String) -> Result<String, String> { | ||
let client = self.0.internal.get_http_client(); | ||
let res = client.get(&url).send().await.map_err(|e| e.to_string())?; | ||
|
||
res.text().await.map_err(|e| e.to_string()) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod client; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
bitwarden_wasm_internal_bg.js | ||
bitwarden_wasm_internal_bg.wasm | ||
bitwarden_wasm_internal_bg.wasm.d.ts | ||
bitwarden_wasm_internal_bg.wasm.js | ||
bitwarden_wasm_internal.d.ts | ||
bitwarden_wasm_internal.js |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
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); | ||
} | ||
|
||
export * from "./bitwarden_wasm_internal_bg.js"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should split the build and publish workflows. If it's needed to publish it on every push to
main
, I suggest triggering the publishing workflow here. An example of that can be found in the build-swift.yml workflow:sdk/.github/workflows/build-swift.yml
Lines 101 to 115 in 90f8a54
BTW is it released to npmjs.com or to github packages?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently gh packages, but we will probably change it to npmjs.