From 07c382914cf118ced219fa1d2d2fa43c7a6d4bfb Mon Sep 17 00:00:00 2001 From: Vitalii Date: Wed, 18 Oct 2023 20:54:28 +0300 Subject: [PATCH] scripts/wasm, worker: fixed wasm module caching issue Included hash in wasm file. Added a manifest file that contains the wasm file name for worker access. Reworked worker setup to query the manifest file without caching, ensuring the correct wasm file name is always retrieved. The worker will first attempt to retrieve the cached wasm file, but will refetch it with a cache reload if an error occurs. Change-Id: Ic4ef68e502b318a29243bf275c041863ec1275ee --- scripts/build-wasm.sh | 14 +++++++- web/satellite/.gitignore | 1 + web/satellite/scripts/build-wasm-dev.sh | 18 +++++++++- web/satellite/scripts/build-wasm.sh | 18 +++++++++- web/satellite/src/utils/accessGrant.worker.js | 34 +++++++++++++------ 5 files changed, 71 insertions(+), 14 deletions(-) diff --git a/scripts/build-wasm.sh b/scripts/build-wasm.sh index c238c50e1..ccb82ef15 100755 --- a/scripts/build-wasm.sh +++ b/scripts/build-wasm.sh @@ -15,5 +15,17 @@ brotli -k release/$TAG/wasm/wasm_exec.js # Build wasm code GOOS=js GOARCH=wasm go build -o release/$TAG/wasm/access.wasm storj.io/storj/satellite/console/wasm +# Take a hash of generated wasm code +hash=$(sha256sum release/$TAG/wasm/access.wasm | awk '{print $1}') + +# Define new file name +filename="access.${hash:0:8}.wasm" + +# Rename the file to include the hash +mv release/$TAG/wasm/access.wasm release/$TAG/wasm/$filename + # Compress wasm code using brotli -brotli -k release/$TAG/wasm/access.wasm +brotli -k release/$TAG/wasm/$filename + +# Generate the manifest which would contain our new file name +echo "{\"fileName\": \"$filename\"}" > release/$TAG/wasm/wasm-manifest.json diff --git a/web/satellite/.gitignore b/web/satellite/.gitignore index 5fd9d5b45..990668ec9 100644 --- a/web/satellite/.gitignore +++ b/web/satellite/.gitignore @@ -10,6 +10,7 @@ wasm_exec.js wasm_exec.js.br *.wasm *.wasm.br +wasm-manifest.json # local env files .env.local diff --git a/web/satellite/scripts/build-wasm-dev.sh b/web/satellite/scripts/build-wasm-dev.sh index ba90d25a4..c51c17e50 100755 --- a/web/satellite/scripts/build-wasm-dev.sh +++ b/web/satellite/scripts/build-wasm-dev.sh @@ -4,4 +4,20 @@ cp "$(go env GOROOT)/misc/wasm/wasm_exec.js" ./static/wasm # Build wasm code -GOOS=js GOARCH=wasm go build -o ./static/wasm/access.wasm storj.io/storj/satellite/console/wasm \ No newline at end of file +GOOS=js GOARCH=wasm go build -o ./static/wasm/access.wasm storj.io/storj/satellite/console/wasm + +# Take a hash of generated wasm code +if command -v sha256sum > /dev/null; then + hash=$(sha256sum ./static/wasm/access.wasm | awk '{print $1}') +else + hash=$(shasum -a 256 ./static/wasm/access.wasm | awk '{print $1}') +fi + +# Define new file name +filename="access.${hash:0:8}.wasm" + +# Rename the file to include the hash +mv ./static/wasm/access.wasm ./static/wasm/$filename + +# Generate the manifest which would contain our new file name +echo "{\"fileName\": \"$filename\"}" > ./static/wasm/wasm-manifest.json diff --git a/web/satellite/scripts/build-wasm.sh b/web/satellite/scripts/build-wasm.sh index a2d9255bd..3388b0331 100755 --- a/web/satellite/scripts/build-wasm.sh +++ b/web/satellite/scripts/build-wasm.sh @@ -9,5 +9,21 @@ brotli -k -f ./static/wasm/wasm_exec.js # Build wasm code GOOS=js GOARCH=wasm go build -o ./static/wasm/access.wasm storj.io/storj/satellite/console/wasm +# Take a hash of generated wasm code +if command -v sha256sum > /dev/null; then + hash=$(sha256sum ./static/wasm/access.wasm | awk '{print $1}') +else + hash=$(shasum -a 256 ./static/wasm/access.wasm | awk '{print $1}') +fi + +# Define new file name +filename="access.${hash:0:8}.wasm" + +# Rename the file to include the hash +mv ./static/wasm/access.wasm ./static/wasm/$filename + # Compress wasm code using brotli -brotli -k -f ./static/wasm/access.wasm +brotli -k -f ./static/wasm/$filename + +# Generate the manifest which would contain our new file name +echo "{\"fileName\": \"$filename\"}" > ./static/wasm/wasm-manifest.json diff --git a/web/satellite/src/utils/accessGrant.worker.js b/web/satellite/src/utils/accessGrant.worker.js index e804c8b38..d20581565 100644 --- a/web/satellite/src/utils/accessGrant.worker.js +++ b/web/satellite/src/utils/accessGrant.worker.js @@ -8,6 +8,22 @@ if (!WebAssembly.instantiate) { self.postMessage(new Error('web assembly is not supported')); } +async function setupWithCacheControl(mode) { + const go = new self.Go(); + + const manifestResp = await fetch('/static/static/wasm/wasm-manifest.json', { cache: 'no-cache' }); + const manifest = await manifestResp.json(); + + const response = await fetch(`/static/static/wasm/${manifest.fileName}`, { cache: mode }); + const buffer = await response.arrayBuffer(); + const module = await WebAssembly.compile(buffer); + const instance = await WebAssembly.instantiate(module, go.importObject); + + go.run(instance); + + self.postMessage('configured'); +} + self.onmessage = async function (event) { const data = event.data; let result; @@ -15,17 +31,13 @@ self.onmessage = async function (event) { switch (data.type) { case 'Setup': try { - const go = new self.Go(); - const response = await fetch('/static/static/wasm/access.wasm'); - const buffer = await response.arrayBuffer(); - const module = await WebAssembly.compile(buffer); - const instance = await WebAssembly.instantiate(module, go.importObject); - - go.run(instance); - - self.postMessage('configured'); - } catch (e) { - self.postMessage(new Error(e.message)); + await setupWithCacheControl('default'); + } catch { + try { + await setupWithCacheControl('reload'); + } catch (e) { + self.postMessage(new Error(e.message)); + } } break;