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
This commit is contained in:
Vitalii 2023-10-18 20:54:28 +03:00
parent 4e0ffd1a11
commit 07c382914c
5 changed files with 71 additions and 14 deletions

View File

@ -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

View File

@ -10,6 +10,7 @@ wasm_exec.js
wasm_exec.js.br
*.wasm
*.wasm.br
wasm-manifest.json
# local env files
.env.local

View File

@ -5,3 +5,19 @@ 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
# 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

View File

@ -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

View File

@ -8,15 +8,13 @@ if (!WebAssembly.instantiate) {
self.postMessage(new Error('web assembly is not supported'));
}
self.onmessage = async function (event) {
const data = event.data;
let result;
let apiKey;
switch (data.type) {
case 'Setup':
try {
async function setupWithCacheControl(mode) {
const go = new self.Go();
const response = await fetch('/static/static/wasm/access.wasm');
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);
@ -24,9 +22,23 @@ self.onmessage = async function (event) {
go.run(instance);
self.postMessage('configured');
}
self.onmessage = async function (event) {
const data = event.data;
let result;
let apiKey;
switch (data.type) {
case 'Setup':
try {
await setupWithCacheControl('default');
} catch {
try {
await setupWithCacheControl('reload');
} catch (e) {
self.postMessage(new Error(e.message));
}
}
break;
case 'DeriveAndEncryptRootKey':