From 093856671aaa461f14c11b341d5dcc6e52ba8828 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Mon, 31 Oct 2022 22:29:38 +0000 Subject: [PATCH 01/17] Encode the WASM as base64 Some nasty hackery to get around the nastiness of the JS ecosystem --- bindings/matrix-sdk-crypto-js/package.json | 7 +-- .../matrix-sdk-crypto-js/scripts/build.sh | 32 +++++++++++++ bindings/matrix-sdk-crypto-js/unbase64.js | 48 +++++++++++++++++++ 3 files changed, 82 insertions(+), 5 deletions(-) create mode 100755 bindings/matrix-sdk-crypto-js/scripts/build.sh create mode 100644 bindings/matrix-sdk-crypto-js/unbase64.js diff --git a/bindings/matrix-sdk-crypto-js/package.json b/bindings/matrix-sdk-crypto-js/package.json index 70c2a428a..2d8d0a524 100644 --- a/bindings/matrix-sdk-crypto-js/package.json +++ b/bindings/matrix-sdk-crypto-js/package.json @@ -37,12 +37,9 @@ "node": ">= 10" }, "scripts": { - "build": "cross-env RUSTFLAGS='-C opt-level=z' WASM_BINDGEN_WEAKREF=1 wasm-pack build --release --target nodejs --scope matrix-org --out-dir ./pkg", + "build": "./scripts/build.sh", "test": "jest --verbose", "doc": "typedoc --tsconfig .", - "prepack": "npm run build && npm run test", - "pack": "wasm-pack pack", - "prepublish": "npm run pack", - "publish": "wasm-pack publish" + "prepack": "npm run build && npm run test" } } diff --git a/bindings/matrix-sdk-crypto-js/scripts/build.sh b/bindings/matrix-sdk-crypto-js/scripts/build.sh new file mode 100755 index 000000000..fc6c57a08 --- /dev/null +++ b/bindings/matrix-sdk-crypto-js/scripts/build.sh @@ -0,0 +1,32 @@ +#!/bin/bash +# +# 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 +# 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. + +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 + +# 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' \ + pkg/matrix_sdk_crypto_js.js diff --git a/bindings/matrix-sdk-crypto-js/unbase64.js b/bindings/matrix-sdk-crypto-js/unbase64.js new file mode 100644 index 000000000..28818efa8 --- /dev/null +++ b/bindings/matrix-sdk-crypto-js/unbase64.js @@ -0,0 +1,48 @@ +// Javascript module which exports a function which will un-base64 a string +// +// From 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 + +function b64ToUint6(nChr) { + return nChr > 64 && nChr < 91 + ? nChr - 65 + : nChr > 96 && nChr < 123 + ? nChr - 71 + : nChr > 47 && nChr < 58 + ? nChr + 4 + : nChr === 43 + ? 62 + : nChr === 47 + ? 63 + : 0; +} + +function base64DecToArr(sBase64, nBlocksSize) { + const sB64Enc = sBase64.replace(/[^A-Za-z0-9+/]/g, ""); + const nInLen = sB64Enc.length; + const nOutLen = nBlocksSize + ? Math.ceil(((nInLen * 3 + 1) >> 2) / nBlocksSize) * nBlocksSize + : (nInLen * 3 + 1) >> 2; + const taBytes = new Uint8Array(nOutLen); + + let nMod3; + let nMod4; + let nUint24 = 0; + let nOutIdx = 0; + for (let nInIdx = 0; nInIdx < nInLen; nInIdx++) { + nMod4 = nInIdx & 3; + nUint24 |= b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << (6 * (3 - nMod4)); + if (nMod4 === 3 || nInLen - nInIdx === 1) { + nMod3 = 0; + while (nMod3 < 3 && nOutIdx < nOutLen) { + taBytes[nOutIdx] = (nUint24 >>> ((16 >>> nMod3) & 24)) & 255; + nMod3++; + nOutIdx++; + } + nUint24 = 0; + } + } + + return taBytes; +} + +module.exports = base64DecToArr; From 4557494da627075d683b5eb38d6cf7db402fd0b2 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Tue, 1 Nov 2022 10:18:44 +0000 Subject: [PATCH 02/17] Optimise unbase64 Use a lookup table instead of a function with if statements --- bindings/matrix-sdk-crypto-js/unbase64.js | 24 +++++------------------ 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/bindings/matrix-sdk-crypto-js/unbase64.js b/bindings/matrix-sdk-crypto-js/unbase64.js index 28818efa8..c1eb154ef 100644 --- a/bindings/matrix-sdk-crypto-js/unbase64.js +++ b/bindings/matrix-sdk-crypto-js/unbase64.js @@ -1,27 +1,13 @@ // Javascript module which exports a function which will un-base64 a string // -// From 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 +// 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 -function b64ToUint6(nChr) { - return nChr > 64 && nChr < 91 - ? nChr - 65 - : nChr > 96 && nChr < 123 - ? nChr - 71 - : nChr > 47 && nChr < 58 - ? nChr + 4 - : nChr === 43 - ? 62 - : nChr === 47 - ? 63 - : 0; -} +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, nBlocksSize) { +function base64DecToArr(sBase64) { const sB64Enc = sBase64.replace(/[^A-Za-z0-9+/]/g, ""); const nInLen = sB64Enc.length; - const nOutLen = nBlocksSize - ? Math.ceil(((nInLen * 3 + 1) >> 2) / nBlocksSize) * nBlocksSize - : (nInLen * 3 + 1) >> 2; + const nOutLen = (nInLen / 3 + 1) >> 2; const taBytes = new Uint8Array(nOutLen); let nMod3; @@ -30,7 +16,7 @@ function base64DecToArr(sBase64, nBlocksSize) { let nOutIdx = 0; for (let nInIdx = 0; nInIdx < nInLen; nInIdx++) { nMod4 = nInIdx & 3; - nUint24 |= b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << (6 * (3 - nMod4)); + nUint24 |= lookup[sB64Enc.charCodeAt(nInIdx)] << (6 * (3 - nMod4)); if (nMod4 === 3 || nInLen - nInIdx === 1) { nMod3 = 0; while (nMod3 < 3 && nOutIdx < nOutLen) { From ce03f016b901bdee8bbb9c4a0b9f3c8a71a8f346 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Tue, 1 Nov 2022 10:19:13 +0000 Subject: [PATCH 03/17] Copy unbase64.js into the right place --- bindings/matrix-sdk-crypto-js/scripts/build.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bindings/matrix-sdk-crypto-js/scripts/build.sh b/bindings/matrix-sdk-crypto-js/scripts/build.sh index fc6c57a08..835b0ad8d 100755 --- a/bindings/matrix-sdk-crypto-js/scripts/build.sh +++ b/bindings/matrix-sdk-crypto-js/scripts/build.sh @@ -23,10 +23,13 @@ RUSTFLAGS='-C opt-level=z' WASM_BINDGEN_WEAKREF=1 wasm-pack build --release --ta echo '`;' } > pkg/matrix_sdk_crypto_js_bg.wasm.js +# copy in the unbase64 module +cp unbase64.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. -loadwasm='const bytes = require("../unbase64.js")(require("./matrix_sdk_crypto_js_bg.wasm.js"));' +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' \ pkg/matrix_sdk_crypto_js.js From 0f104c7433f25c92ed96540a4e5bc0247b8e0775 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Tue, 1 Nov 2022 13:47:44 +0000 Subject: [PATCH 04/17] Fix unbase64 loading --- bindings/matrix-sdk-crypto-js/unbase64.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bindings/matrix-sdk-crypto-js/unbase64.js b/bindings/matrix-sdk-crypto-js/unbase64.js index c1eb154ef..445d69097 100644 --- a/bindings/matrix-sdk-crypto-js/unbase64.js +++ b/bindings/matrix-sdk-crypto-js/unbase64.js @@ -7,7 +7,7 @@ const lookup = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 function base64DecToArr(sBase64) { const sB64Enc = sBase64.replace(/[^A-Za-z0-9+/]/g, ""); const nInLen = sB64Enc.length; - const nOutLen = (nInLen / 3 + 1) >> 2; + const nOutLen = (nInLen * 3 + 1) >> 2; const taBytes = new Uint8Array(nOutLen); let nMod3; From 6497d6d67660fc383410f81262d7e3432e44bd2e Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 2 Nov 2022 16:19:17 +0100 Subject: [PATCH 05/17] 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; +}; From 28d4a69552d9f31698d1378e231336857046cfe1 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 2 Nov 2022 16:32:20 +0100 Subject: [PATCH 06/17] !fixup --- bindings/matrix-sdk-crypto-js/scripts/build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bindings/matrix-sdk-crypto-js/scripts/build.sh b/bindings/matrix-sdk-crypto-js/scripts/build.sh index 3f828b47d..cd918517b 100755 --- a/bindings/matrix-sdk-crypto-js/scripts/build.sh +++ b/bindings/matrix-sdk-crypto-js/scripts/build.sh @@ -22,7 +22,7 @@ RUSTFLAGS='-C opt-level=z' WASM_BINDGEN_WEAKREF=1 wasm-pack build --release --ta 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 scripts/wasm.js pkg/ +cp scripts/unbase64.js pkg/ # In the JavaScript: # 1. Replace the lines that load the Wasm, From bb96ab89f803257c52839e375c5e5cc20722fbdf Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 2 Nov 2022 16:57:26 +0100 Subject: [PATCH 07/17] fix(crypto-js): Make `build.sh` cross-platform-ish. --- bindings/matrix-sdk-crypto-js/scripts/build.sh | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/bindings/matrix-sdk-crypto-js/scripts/build.sh b/bindings/matrix-sdk-crypto-js/scripts/build.sh index cd918517b..8adb387c9 100755 --- a/bindings/matrix-sdk-crypto-js/scripts/build.sh +++ b/bindings/matrix-sdk-crypto-js/scripts/build.sh @@ -16,7 +16,7 @@ set -e -RUSTFLAGS='-C opt-level=z' WASM_BINDGEN_WEAKREF=1 wasm-pack build --release --target nodejs --scope matrix-org --out-dir ./pkg +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 @@ -24,11 +24,17 @@ echo "module.exports = '$(base64 pkg/matrix_sdk_crypto_js_bg.wasm)';" > pkg/matr # Copy in the unbase64 module cp scripts/unbase64.js pkg/ +if test "$(uname)" = "Darwin"; then + SEDI="-i ''" +else + SEDI="-i" +fi + # 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 '' \ +sed "${SEDI}" \ -e "/^const path = /d" \ -e "s@^const bytes =.*@${loadwasm}@" \ -e '/Text..coder.*= require(.util.)/d' \ From a4ca6dbf38c4b302d0ca657a807c8472572f267f Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 2 Nov 2022 17:25:13 +0100 Subject: [PATCH 08/17] !debug --- .../matrix-sdk-crypto-js/scripts/build.sh | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/bindings/matrix-sdk-crypto-js/scripts/build.sh b/bindings/matrix-sdk-crypto-js/scripts/build.sh index 8adb387c9..401e58851 100755 --- a/bindings/matrix-sdk-crypto-js/scripts/build.sh +++ b/bindings/matrix-sdk-crypto-js/scripts/build.sh @@ -21,21 +21,30 @@ RUSTFLAGS='-C opt-level=z' WASM_BINDGEN_WEAKREF=1 wasm-pack build --release --ta # 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 +echo 'HEAD:'; +head -c 30 pkg/matrix_sdk_crypto_js_bg.wasm.js + +echo -e '\n\nTAIL:'; +tail -c 20 pkg/matrix_sdk_crypto_js_bg.wasm.js + # Copy in the unbase64 module cp scripts/unbase64.js pkg/ -if test "$(uname)" = "Darwin"; then - SEDI="-i ''" -else - SEDI="-i" -fi - # 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 "${SEDI}" \ - -e "/^const path = /d" \ - -e "s@^const bytes =.*@${loadwasm}@" \ - -e '/Text..coder.*= require(.util.)/d' \ - pkg/matrix_sdk_crypto_js.js + +if test "$(uname)" = "Darwin"; then + sed -i '' \ + -e "/^const path = /d" \ + -e "s@^const bytes =.*@${loadwasm}@" \ + -e '/Text..coder.*= require(.util.)/d' \ + pkg/matrix_sdk_crypto_js.js +else + sed -i \ + -e "/^const path = /d" \ + -e "s@^const bytes =.*@${loadwasm}@" \ + -e '/Text..coder.*= require(.util.)/d' \ + pkg/matrix_sdk_crypto_js.js +fi From 9d400a7494d8d17ceb1e5e0d839d0e0f5428e8f9 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Wed, 2 Nov 2022 17:13:58 +0000 Subject: [PATCH 09/17] fix JS syntax --- bindings/matrix-sdk-crypto-js/scripts/build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bindings/matrix-sdk-crypto-js/scripts/build.sh b/bindings/matrix-sdk-crypto-js/scripts/build.sh index 401e58851..15e8fce6a 100755 --- a/bindings/matrix-sdk-crypto-js/scripts/build.sh +++ b/bindings/matrix-sdk-crypto-js/scripts/build.sh @@ -19,7 +19,7 @@ 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 base64'ed Wasm. -echo "module.exports = '$(base64 pkg/matrix_sdk_crypto_js_bg.wasm)';" > pkg/matrix_sdk_crypto_js_bg.wasm.js +echo "module.exports = \`$(base64 pkg/matrix_sdk_crypto_js_bg.wasm)\`;" > pkg/matrix_sdk_crypto_js_bg.wasm.js echo 'HEAD:'; head -c 30 pkg/matrix_sdk_crypto_js_bg.wasm.js From 898265b2579eef603b2a322adaea9ac636aaef6c Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Wed, 2 Nov 2022 17:22:00 +0000 Subject: [PATCH 10/17] Clean up build script --- .../matrix-sdk-crypto-js/scripts/build.sh | 29 +++++-------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/bindings/matrix-sdk-crypto-js/scripts/build.sh b/bindings/matrix-sdk-crypto-js/scripts/build.sh index 15e8fce6a..f62d9ef1d 100755 --- a/bindings/matrix-sdk-crypto-js/scripts/build.sh +++ b/bindings/matrix-sdk-crypto-js/scripts/build.sh @@ -11,22 +11,16 @@ # # 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 +cd $(dirname "$0")/.. + 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 -echo 'HEAD:'; -head -c 30 pkg/matrix_sdk_crypto_js_bg.wasm.js - -echo -e '\n\nTAIL:'; -tail -c 20 pkg/matrix_sdk_crypto_js_bg.wasm.js - # Copy in the unbase64 module cp scripts/unbase64.js pkg/ @@ -35,16 +29,9 @@ cp scripts/unbase64.js pkg/ # 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"));' -if test "$(uname)" = "Darwin"; then - sed -i '' \ - -e "/^const path = /d" \ - -e "s@^const bytes =.*@${loadwasm}@" \ - -e '/Text..coder.*= require(.util.)/d' \ - pkg/matrix_sdk_crypto_js.js -else - sed -i \ - -e "/^const path = /d" \ - -e "s@^const bytes =.*@${loadwasm}@" \ - -e '/Text..coder.*= require(.util.)/d' \ - pkg/matrix_sdk_crypto_js.js -fi +# sed on OSX uses different syntax for sed -i, so let's just avoid it. +sed -e "/^const path = /d" \ + -e "s@^const bytes =.*@${loadwasm}@" \ + -e '/Text..coder.*= require(.util.)/d' \ + pkg/matrix_sdk_crypto_js.js >pkg/matrix_sdk_crypto_js.js.new +mv pkg/matrix_sdk_crypto_js.js.new pkg/matrix_sdk_crypto_js.js From 223e65fc260fe8de962cec84cae633ab6e55062a Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Thu, 3 Nov 2022 11:08:52 +0100 Subject: [PATCH 11/17] !debug --- .github/workflows/bindings_ci.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/bindings_ci.yml b/.github/workflows/bindings_ci.yml index 128eeebac..d3c1c9eb4 100644 --- a/.github/workflows/bindings_ci.yml +++ b/.github/workflows/bindings_ci.yml @@ -181,6 +181,9 @@ jobs: uses: actions/setup-node@v3 with: node-version: 18.0 + + - name: Setup tmate session + uses: mxschmitt/action-tmate@v3 - name: Install NPM dependencies working-directory: ${{ env.MATRIX_SDK_CRYPTO_JS_PATH }} From aa7d2258676d38fa36c074b4d4a62fe1c7d9e294 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Thu, 3 Nov 2022 13:45:14 +0100 Subject: [PATCH 12/17] feat(crypto-js): `Qr.to_bytes` returns a `Uint8ClampedArray`. --- bindings/matrix-sdk-crypto-js/src/verification.rs | 11 ++++++++--- bindings/matrix-sdk-crypto-js/tests/device.test.js | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/bindings/matrix-sdk-crypto-js/src/verification.rs b/bindings/matrix-sdk-crypto-js/src/verification.rs index f51a0b66f..c99b3ff43 100644 --- a/bindings/matrix-sdk-crypto-js/src/verification.rs +++ b/bindings/matrix-sdk-crypto-js/src/verification.rs @@ -484,8 +484,13 @@ impl Qr { /// The `to_qr_code` method can be used to instead output a QrCode /// object that can be rendered. #[wasm_bindgen(js_name = "toBytes")] - pub fn to_bytes(&self) -> Result { - Ok(self.inner.to_bytes()?.into_iter().map(JsValue::from).collect()) + pub fn to_bytes(&self) -> Result { + let bytes = self.inner.to_bytes()?; + let output = Uint8ClampedArray::new_with_length(bytes.len() as _); + + output.copy_from(&bytes); + + Ok(output) } /// Notify the other side that we have successfully scanned the QR @@ -754,7 +759,7 @@ impl QrCodeScan { /// This method is useful if you would like to do your own custom QR code /// decoding. #[wasm_bindgen(js_name = "fromBytes")] - pub fn from_bytes(buffer: Uint8ClampedArray) -> Result { + pub fn from_bytes(buffer: &Uint8ClampedArray) -> Result { let bytes = buffer.to_vec(); Ok(Self { inner: matrix_sdk_qrcode::QrVerificationData::from_bytes(&bytes)? }) diff --git a/bindings/matrix-sdk-crypto-js/tests/device.test.js b/bindings/matrix-sdk-crypto-js/tests/device.test.js index 7f8d8fe6a..63e240455 100644 --- a/bindings/matrix-sdk-crypto-js/tests/device.test.js +++ b/bindings/matrix-sdk-crypto-js/tests/device.test.js @@ -716,7 +716,7 @@ describe('Key Verification', () => { qrCodeBytes = qr2.toBytes(); expect(qrCodeBytes).toHaveLength(122); - expect(qrCodeBytes.slice(0, 7)).toStrictEqual([...qrCodeHeader, ...qrCodeVersion].map(char => char.charCodeAt(0))); + // expect(qrCodeBytes.slice(0, 7)).toStrictEqual([...qrCodeHeader, ...qrCodeVersion].map(char => char.charCodeAt(0))); }); test('can render QR code', async () => { From d044565caac81e88ebf1a8fe502b951b5b70344b Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Thu, 3 Nov 2022 14:57:07 +0100 Subject: [PATCH 13/17] test(crypto-js): Keep `qr.toBytes()` local, to avoid weird GC collection. --- bindings/matrix-sdk-crypto-js/tests/device.test.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/bindings/matrix-sdk-crypto-js/tests/device.test.js b/bindings/matrix-sdk-crypto-js/tests/device.test.js index 63e240455..a3fa80742 100644 --- a/bindings/matrix-sdk-crypto-js/tests/device.test.js +++ b/bindings/matrix-sdk-crypto-js/tests/device.test.js @@ -707,16 +707,14 @@ describe('Key Verification', () => { expect(qr2.roomId).toBeUndefined(); }); - let qrCodeBytes; - test('can read QR code\'s bytes', async () => { const qrCodeHeader = 'MATRIX'; const qrCodeVersion = '\x02'; - qrCodeBytes = qr2.toBytes(); + const qrCodeBytes = qr2.toBytes(); expect(qrCodeBytes).toHaveLength(122); - // expect(qrCodeBytes.slice(0, 7)).toStrictEqual([...qrCodeHeader, ...qrCodeVersion].map(char => char.charCodeAt(0))); + expect(Array.from(qrCodeBytes.slice(0, 7))).toEqual([...qrCodeHeader, ...qrCodeVersion].map(char => char.charCodeAt(0))); }); test('can render QR code', async () => { @@ -793,7 +791,7 @@ describe('Key Verification', () => { let qr1; test('can scan a QR code from bytes', async () => { - const scan = QrCodeScan.fromBytes(qrCodeBytes); + const scan = QrCodeScan.fromBytes(qr2.toBytes()); expect(scan).toBeInstanceOf(QrCodeScan); From 265ac1f97b3d3c79c54bad0618b6c71c4b294bf6 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Thu, 3 Nov 2022 14:59:22 +0100 Subject: [PATCH 14/17] chore(crypto-js): Configure a profiling profile for `wasm-pack`. --- bindings/matrix-sdk-crypto-js/Cargo.toml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/bindings/matrix-sdk-crypto-js/Cargo.toml b/bindings/matrix-sdk-crypto-js/Cargo.toml index 17b62cd99..9407729b5 100644 --- a/bindings/matrix-sdk-crypto-js/Cargo.toml +++ b/bindings/matrix-sdk-crypto-js/Cargo.toml @@ -15,6 +15,14 @@ publish = false [package.metadata.docs.rs] rustdoc-args = ["--cfg", "docsrs"] +[package.metadata.wasm-pack.profile.profiling] +wasm-opt = false + +[package.metadata.wasm-pack.profile.profiling.wasm-bindgen] +debug-js-glue = false +demangle-name-section = true +dwarf-debug-info = true + [package.metadata.wasm-pack.profile.release] wasm-opt = ['-Oz'] From e989bc2377e063484454c6e35dc9b38f571f2874 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Thu, 3 Nov 2022 15:37:21 +0100 Subject: [PATCH 15/17] fix(crypto-js): `scan_qr_code` takes a reference to `QrCodeScan`. --- bindings/matrix-sdk-crypto-js/src/verification.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bindings/matrix-sdk-crypto-js/src/verification.rs b/bindings/matrix-sdk-crypto-js/src/verification.rs index c99b3ff43..811311e42 100644 --- a/bindings/matrix-sdk-crypto-js/src/verification.rs +++ b/bindings/matrix-sdk-crypto-js/src/verification.rs @@ -1034,9 +1034,9 @@ impl VerificationRequest { /// for this verification flow. #[cfg(feature = "qrcode")] #[wasm_bindgen(js_name = "scanQrCode")] - pub fn scan_qr_code(&self, data: QrCodeScan) -> Promise { + pub fn scan_qr_code(&self, data: &QrCodeScan) -> Promise { let me = self.inner.clone(); - let qr_verification_data = data.inner; + let qr_verification_data = data.inner.clone(); future_to_promise( async move { Ok(me.scan_qr_code(qr_verification_data).await?.map(Qr::from)) }, From 6d21df6d290ad086f6f0f26d72e40c7be6c2991f Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Thu, 3 Nov 2022 15:37:54 +0100 Subject: [PATCH 16/17] !debug off --- .github/workflows/bindings_ci.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/bindings_ci.yml b/.github/workflows/bindings_ci.yml index d3c1c9eb4..6c88df478 100644 --- a/.github/workflows/bindings_ci.yml +++ b/.github/workflows/bindings_ci.yml @@ -182,9 +182,6 @@ jobs: with: node-version: 18.0 - - name: Setup tmate session - uses: mxschmitt/action-tmate@v3 - - name: Install NPM dependencies working-directory: ${{ env.MATRIX_SDK_CRYPTO_JS_PATH }} run: npm install From ee27c19bf7a6cec0303fc7cf56ee232a393037c6 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Thu, 3 Nov 2022 15:57:20 +0100 Subject: [PATCH 17/17] chore: Removing trailing spaces. --- .github/workflows/bindings_ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/bindings_ci.yml b/.github/workflows/bindings_ci.yml index 6c88df478..128eeebac 100644 --- a/.github/workflows/bindings_ci.yml +++ b/.github/workflows/bindings_ci.yml @@ -181,7 +181,7 @@ jobs: uses: actions/setup-node@v3 with: node-version: 18.0 - + - name: Install NPM dependencies working-directory: ${{ env.MATRIX_SDK_CRYPTO_JS_PATH }} run: npm install