Skip to content

Commit

Permalink
build: inject wasm base64 into build_index.sh script
Browse files Browse the repository at this point in the history
  • Loading branch information
Dustin-Ray committed Nov 22, 2024
1 parent 840d6f4 commit 51d38a7
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions node/src/build_index.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,78 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

cat $1/sxt_proof_of_sql_sdk_wasm.js $SCRIPT_DIR/index_tail.js > $SCRIPT_DIR/index.js
cp $1/sxt_proof_of_sql_sdk_wasm_bg.wasm $SCRIPT_DIR

# Enable error handling
set -euo pipefail

# Define an error handler to gracefully handle errors
error_handler() {
echo "Error: Script encountered an issue. Exiting gracefully."
exit 1
}

# Trap errors and call the error handler
trap error_handler ERR

# Truncate the JavaScript file at the specified line
truncate_js_file() {
local input_js_file=$1
local output_js_file=$2
if ! sed '/^const wasm_url = new URL('\''sxt_proof_of_sql_sdk_wasm_bg.wasm'\'', import.meta.url);/,$d' "$input_js_file" > "$output_js_file"; then
echo "Error: Failed to truncate the JavaScript file."
return 1
fi
}

# Base64-encode the WASM file and append it as a string to the JS file
append_base64_wasm_to_js() {
local wasm_file=$1
local js_file=$2
if ! base64 "$wasm_file" > /dev/null 2>&1; then
echo "Error: Failed to Base64-encode the WASM file."
return 1
fi
local base64_wasm=$(base64 "$wasm_file")
echo -e "\n// Embedded WASM (Base64 Encoded)" >> "$js_file"
echo "let wasmCode = \`$base64_wasm\`;" >> "$js_file"
echo "const wasmBytes = Uint8Array.from(atob(wasmCode), c => c.charCodeAt(0));" >> "$js_file"
}

# Append WebAssembly instantiation code
append_wasm_instantiation_code() {
local js_file=$1
echo -e "\n// Instantiate the WebAssembly module" >> "$js_file"
echo "const wasmInstance = (await WebAssembly.instantiate(wasmBytes, imports)).instance;" >> "$js_file"
echo "const wasm = wasmInstance.exports;" >> "$js_file"
echo "export const __wasm = wasm;" >> "$js_file"
}

# Main logic
main() {
local input_js_file="sxt_proof_of_sql_sdk_wasm.js"
local wasm_file="sxt_proof_of_sql_sdk_wasm_bg.wasm"
local output_js_file="sxt_proof_of_sql_sdk_wasm_modified.js"

if [[ ! -f "$input_js_file" ]]; then
echo "Error: Input JavaScript file '$input_js_file' not found."
return 1
fi

if [[ ! -f "$wasm_file" ]]; then
echo "Error: WASM file '$wasm_file' not found."
return 1
fi

echo "Truncating the JavaScript file..."
truncate_js_file "$input_js_file" "$output_js_file" || return 1

echo "Embedding the Base64-encoded WASM file..."
append_base64_wasm_to_js "$wasm_file" "$output_js_file" || return 1

echo "Appending WebAssembly instantiation code..."
append_wasm_instantiation_code "$output_js_file" || return 1

echo "Modified JavaScript file created: $output_js_file"
}

main "$@"

0 comments on commit 51d38a7

Please sign in to comment.