Skip to content

Commit 1fb2ab1

Browse files
authored
Merge pull request #910 from openmina/fix/asset-name-derandomization
fix(web): Don't use random hashes for names, use file contents checksums
2 parents 014dd4f + 99d800d commit 1fb2ab1

File tree

1 file changed

+60
-24
lines changed

1 file changed

+60
-24
lines changed

frontend/docker/startup.sh

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -113,40 +113,76 @@ download_wasm_files() {
113113
rm "/tmp/openmina-$OPENMINA_WASM_VERSION-webnode-wasm.tar.gz"
114114
}
115115

116+
get_short_sha1() {
117+
local file="$1"
118+
if [ -z "$file" ]; then
119+
echo "Usage: get_short_sha1 filename" >&2
120+
return 1
121+
fi
122+
if [ ! -f "$file" ]; then
123+
echo "Error: File not found: $file" >&2
124+
return 1
125+
fi
126+
127+
if command -v sha1sum >/dev/null 2>&1; then
128+
sha1sum "$file" | awk '{ print substr($1,1,8) }'
129+
elif command -v openssl >/dev/null 2>&1; then
130+
openssl sha1 "$file" | awk '{ print substr($2,1,8) }'
131+
else
132+
echo "Error: Neither sha1sum nor openssl is available for hashing." >&2
133+
return 1
134+
fi
135+
}
136+
116137
inject_caching_logic() {
117-
local js_file="$1"
118-
local index_html="$2"
119-
if [ -f "$js_file" ]; then
120-
echo "Injecting caching logic into $js_file"
138+
local js_file="$1"
139+
local index_html="$2"
121140

122-
# Generate a unique hash
123-
local hash=$(openssl rand -hex 8)
141+
# Check if JavaScript file exists
142+
if [ ! -f "$js_file" ]; then
143+
echo "Warning: $js_file not found. Caching logic not injected."
144+
return 0
145+
fi
124146

125-
# replace openmina_node_web_bg.wasm with openmina_node_web_bg.$hash.wasm
126-
sed -i "s/openmina_node_web_bg.wasm/openmina_node_web_bg.$hash.wasm/g" "$js_file"
147+
echo "Injecting caching logic into $js_file"
127148

128-
# add cache headers to fetch calls
129-
sed -i 's/module_or_path = fetch(module_or_path);/module_or_path = fetch(module_or_path, { cache: "force-cache", headers: { "Cache-Control": "max-age=31536000, immutable" } });/' "$js_file"
149+
# Define target files
150+
local js_target_file="${TARGET_DIR}/openmina_node_web.js"
151+
local wasm_target_file="${TARGET_DIR}/openmina_node_web_bg.wasm"
130152

131-
# rename openmina_node_web_bg.wasm to openmina_node_web_bg.$hash.wasm
132-
mv "$TARGET_DIR/openmina_node_web_bg.wasm" "$TARGET_DIR/openmina_node_web_bg.$hash.wasm"
153+
# Generate checksum hashes
154+
local js_file_hash
155+
js_file_hash=$(get_short_sha1 "$js_target_file") || { echo "Failed to get hash for $js_target_file"; return 1; }
133156

134-
# rename openmina_node_web.js to openmina_node_web.$hash.js
135-
mv "$TARGET_DIR/openmina_node_web.js" "$TARGET_DIR/openmina_node_web.$hash.js"
157+
local wasm_file_hash
158+
wasm_file_hash=$(get_short_sha1 "$wasm_target_file") || { echo "Failed to get hash for $wasm_target_file"; return 1; }
136159

137-
# replace openmina_node_web.js with openmina_node_web.$hash.js in index.html
138-
sed -i "s/openmina_node_web.js/openmina_node_web.$hash.js/g" "$index_html"
160+
# Check if hashed files already exist to prevent multiple injections
161+
local js_new_file="${TARGET_DIR}/openmina_node_web.${js_file_hash}.js"
162+
local wasm_new_file="${TARGET_DIR}/openmina_node_web_bg.${wasm_file_hash}.wasm"
139163

140-
if [[ $? -ne 0 ]]; then
141-
echo "Failed to inject caching logic into $js_file"
142-
else
143-
echo "Successfully injected caching logic into $js_file"
164+
if [[ -f "$js_new_file" ]] && [[ -f "$wasm_new_file" ]]; then
165+
echo "Hashed files already exist. Skipping caching logic injection."
166+
return 0
144167
fi
145-
else
146-
echo "Warning: $js_file not found. Caching logic not injected."
147-
fi
148-
}
149168

169+
# Replace openmina_node_web_bg.wasm with openmina_node_web_bg.<hash>.wasm in JS file
170+
sed -i "s/openmina_node_web_bg\.wasm/openmina_node_web_bg.${wasm_file_hash}.wasm/g" "$js_file" || { echo "Failed to update wasm filename in $js_file"; return 1; }
171+
172+
# Add cache headers to fetch calls in JS file
173+
sed -i 's/module_or_path = fetch(module_or_path);/module_or_path = fetch(module_or_path, { cache: "force-cache", headers: { "Cache-Control": "max-age=31536000, immutable" } });/' "$js_file" || { echo "Failed to inject cache headers into $js_file"; return 1; }
174+
175+
# Rename wasm file with hash
176+
mv "$wasm_target_file" "$wasm_new_file" || { echo "Failed to rename $wasm_target_file to $wasm_new_file"; return 1; }
177+
178+
# Rename JS file with hash
179+
mv "$js_target_file" "$js_new_file" || { echo "Failed to rename $js_target_file to $js_new_file"; return 1; }
180+
181+
# Replace JS filename in index.html
182+
sed -i "s/openmina_node_web\.js/openmina_node_web.${js_file_hash}.js/g" "$index_html" || { echo "Failed to update JS filename in $index_html"; return 1; }
183+
184+
echo "Successfully injected caching logic into $js_file"
185+
}
150186

151187
if [ -n "$OPENMINA_FRONTEND_ENVIRONMENT" ]; then
152188
echo "Using environment: $OPENMINA_FRONTEND_ENVIRONMENT"

0 commit comments

Comments
 (0)