From 6497d6d67660fc383410f81262d7e3432e44bd2e Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 2 Nov 2022 16:19:17 +0100 Subject: [PATCH] feat(crypto-js): Make `scripts/build.sh` compatible with macOS. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch also improves the “phrasing”, simplifies the code a little bit etc. Small stuff. --- .../matrix-sdk-crypto-js/scripts/build.sh | 30 +++++++++---------- .../{ => scripts}/unbase64.js | 8 ++--- 2 files changed, 18 insertions(+), 20 deletions(-) rename bindings/matrix-sdk-crypto-js/{ => scripts}/unbase64.js (89%) diff --git a/bindings/matrix-sdk-crypto-js/scripts/build.sh b/bindings/matrix-sdk-crypto-js/scripts/build.sh index 835b0ad8d..3f828b47d 100755 --- a/bindings/matrix-sdk-crypto-js/scripts/build.sh +++ b/bindings/matrix-sdk-crypto-js/scripts/build.sh @@ -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 diff --git a/bindings/matrix-sdk-crypto-js/unbase64.js b/bindings/matrix-sdk-crypto-js/scripts/unbase64.js similarity index 89% rename from bindings/matrix-sdk-crypto-js/unbase64.js rename to bindings/matrix-sdk-crypto-js/scripts/unbase64.js index 445d69097..fa82e2dde 100644 --- a/bindings/matrix-sdk-crypto-js/unbase64.js +++ b/bindings/matrix-sdk-crypto-js/scripts/unbase64.js @@ -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; +};