feat(crypto-js): Make scripts/build.sh compatible with macOS.

This patch also improves the “phrasing”, simplifies the code a little bit etc.
Small stuff.
This commit is contained in:
Ivan Enderlin
2022-11-02 16:19:17 +01:00
parent 0f104c7433
commit 6497d6d676
2 changed files with 18 additions and 20 deletions

View File

@@ -1,35 +1,35 @@
#!/bin/bash
#
# Build the javascript modules
# Build the JavaScript modules
#
# This script is really a workaround for https://github.com/rustwasm/wasm-pack/issues/1074.
#
# Currently, the only reliable way to load webassembly in all the JS
# Currently, the only reliable way to load WebAssembly in all the JS
# environments we want to target (web-via-webpack, web-via-browserify, jest)
# seems to be to pack the WASM into base64, and then unpack it and instantiate
# it at runtime.
#
# Hopefully one day, https://github.com/rustwasm/wasm-pack/issues/1074 will be
# fixed and this will be unnecessary.
#
# Run this script from the root of the project, i.e. from the parent directory of this file.
set -e
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 b64'ed wasm
{
echo 'module.exports = `'
base64 pkg/matrix_sdk_crypto_js_bg.wasm
echo '`;'
} > pkg/matrix_sdk_crypto_js_bg.wasm.js
# 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 in the unbase64 module
cp unbase64.js pkg/
# Copy in the unbase64 module
cp scripts/wasm.js pkg/
# In the javascript:
# 1. replace the lines that load the wasm
# 2. remove the imports of TextDecoder and TextEncoder. We rely on the global defaults.
# In the JavaScript:
# 1. Replace the lines that load the Wasm,
# 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 -i -e "/^const path = /,+1 c$loadwasm" \
-e '/= require(`util`)/d' \
sed -i '' \
-e "/^const path = /d" \
-e "s@^const bytes =.*@${loadwasm}@" \
-e '/Text..coder.*= require(.util.)/d' \
pkg/matrix_sdk_crypto_js.js

View File

@@ -1,10 +1,10 @@
// Javascript module which exports a function which will un-base64 a string
// JavaScript module which exports a function which will un-base64 a string.
//
// Based on the code at https://developer.mozilla.org/en-US/docs/Glossary/Base64#solution_2_%E2%80%93_rewriting_atob_and_btoa_using_typedarrays_and_utf-8
const lookup = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 62, 0, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 63, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51]);
function base64DecToArr(sBase64) {
module.exports = (sBase64) => {
const sB64Enc = sBase64.replace(/[^A-Za-z0-9+/]/g, "");
const nInLen = sB64Enc.length;
const nOutLen = (nInLen * 3 + 1) >> 2;
@@ -29,6 +29,4 @@ function base64DecToArr(sBase64) {
}
return taBytes;
}
module.exports = base64DecToArr;
};