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

feat(crypto-js): Encode the WASM as base64 for portability #1167

Merged
merged 17 commits into from
Nov 3, 2022
Merged

Conversation

richvdh
Copy link
Member

@richvdh richvdh commented Oct 31, 2022

I'm sorry about the filthiness of this hack.

It seems like the only way to get the WASM loaded correctly everywhere is to base64 it into a regular JS module. So, let's do that...

The --target nodejs seems to be closest to what we want; I also tried --target bundler, but orchestrating the different modules was rather more complicated.

@richvdh richvdh requested a review from Hywan October 31, 2022 22:35
Comment on lines -44 to -46
"pack": "wasm-pack pack",
"prepublish": "npm run pack",
"publish": "wasm-pack publish"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed these for now as I don't think they'll do the right thing, and they seemed to be a footgun.

Some nasty hackery to get around the nastiness of the JS ecosystem
@richvdh
Copy link
Member Author

richvdh commented Nov 1, 2022

hold off on review for a minute, have realised a thinko

@richvdh richvdh removed the request for review from Hywan November 1, 2022 09:50
Use a lookup table instead of a function with if statements
@codecov
Copy link

codecov bot commented Nov 1, 2022

Codecov Report

Base: 73.07% // Head: 73.62% // Increases project coverage by +0.55% 🎉

Coverage data is based on head (ee27c19) compared to base (471ac07).
Patch has no changes to coverable lines.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1167      +/-   ##
==========================================
+ Coverage   73.07%   73.62%   +0.55%     
==========================================
  Files         110      110              
  Lines       12304    12384      +80     
==========================================
+ Hits         8991     9118     +127     
+ Misses       3313     3266      -47     
Impacted Files Coverage Δ
...s/matrix-sdk-crypto/src/store/integration_tests.rs 99.17% <0.00%> (-0.83%) ⬇️
.../matrix-sdk-crypto/src/session_manager/sessions.rs 78.12% <0.00%> (-0.62%) ⬇️
...x-sdk-crypto/src/session_manager/group_sessions.rs 82.29% <0.00%> (-0.32%) ⬇️
crates/matrix-sdk-sled/src/crypto_store.rs 88.55% <0.00%> (ø)
...ates/matrix-sdk-crypto/src/verification/sas/mod.rs 79.29% <0.00%> (+0.09%) ⬆️
crates/matrix-sdk-crypto/src/identities/device.rs 75.00% <0.00%> (+0.12%) ⬆️
crates/matrix-sdk/src/http_client.rs 86.41% <0.00%> (+0.16%) ⬆️
crates/matrix-sdk/src/client/builder.rs 70.96% <0.00%> (+0.23%) ⬆️
crates/matrix-sdk-crypto/src/gossiping/machine.rs 60.69% <0.00%> (+0.28%) ⬆️
crates/matrix-sdk-crypto/src/store/mod.rs 58.69% <0.00%> (+0.30%) ⬆️
... and 21 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@richvdh richvdh requested a review from Hywan November 1, 2022 10:38
@richvdh
Copy link
Member Author

richvdh commented Nov 1, 2022

@Hywan these tests pass locally, and I don't really understand what they are complaining about. Any ideas?

@Hywan Hywan self-assigned this Nov 2, 2022
RUSTFLAGS='-C opt-level=z' WASM_BINDGEN_WEAKREF=1 wasm-pack build --release --target nodejs --scope matrix-org --out-dir pkg

# Convert the Wasm into a JS file that exports the base64'ed Wasm.
echo "module.exports = \`$(base64 pkg/matrix_sdk_crypto_js_bg.wasm)\`;" > pkg/matrix_sdk_crypto_js_bg.wasm.js
Copy link
Member Author

@richvdh richvdh Nov 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Hywan I don't love that this means that we'll slurp the whole 4.3MB of base64ed-WASM into bash and pass it as an argument to echo. Did my previous solution of writing it straight to the output file not work for you?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry, I was just trying to debug something. Your previous solution was good too!

Copy link
Member

@Hywan Hywan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, finally! :-)

# 2. Remove the imports of `TextDecoder` and `TextEncoder`. We rely on the global defaults.
loadwasm='const bytes = require("./unbase64.js")(require("./matrix_sdk_crypto_js_bg.wasm.js"));'

# sed on OSX uses different syntax for sed -i, so let's just avoid it.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@Hywan Hywan enabled auto-merge November 3, 2022 14:57
@Hywan Hywan merged commit 5b25b89 into main Nov 3, 2022
@Hywan Hywan deleted the rav/base64-wasm branch November 3, 2022 15:44
richvdh added a commit that referenced this pull request Dec 12, 2022
Mostly because, as of #1167, we no longer have a `publish` script.
richvdh added a commit to element-hq/element-web that referenced this pull request Jun 26, 2024
Now that we use the Rust crypto stack
(matrix-org/matrix-react-sdk#12630), the legacy olm
library is unneeded. Remove all the dedicated cruft for loading it.

(All this crap is why matrix-sdk-crypto-wasm has to base64 its wasm artifact,
cf matrix-org/matrix-rust-sdk#1167.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants