# shellcheck shell=bash # Part of JoeyLib, Copyright (c) 2026 Scott Duensing, under the MIT License. # See LICENSE.md for license information. # SPDX-License-Identifier: MIT # # Shared by the web gates (scripts/bench-web.sh, scripts/verify-web-*.sh) and # joey-run: serve a web program's folder (make web: bin/NAME/NAME.html and # its files) over HTTP on this machine and drive it in headless Chromium with # scripts/lib/webDriver.mjs. Source after joeyEnv.sh. # # webNeed exit 2 unless Chromium and Node (emsdk's) are here # webServe DIR serve DIR; sets WEB_PORT and WEB_SERVER_PID # webSoundSink a PulseAudio null sink for Chromium to play to, so # a gate that times the page's sound has a real clock # pacing it (muted, Chromium's audio runs on a fake # one that drifts off real time); false without a # sound server # webDrive PAGE ARGS... run webDriver.mjs on http://127.0.0.1:WEB_PORT/PAGE # (playing to the sink, when there is one) # webStop stop the server, and remove the sink WEB_PORT="" WEB_SERVER_PID="" WEB_SINK_NAME="" WEB_SINK_MODULE="" WEB_PORT_MIN=20000 WEB_PORT_SPAN=20000 webNeed() { WEB_CHROME="$(command -v chromium || command -v chromium-browser || command -v google-chrome || true)" if [ -z "$WEB_CHROME" ]; then echo "$(basename "$0"): no Chromium or Chrome to run web programs in" >&2 exit 2 fi WEB_NODE="$(ls -d "${EMSDK_DIR:-}"/node/*/bin/node 2>/dev/null | head -1)" if [ -z "$WEB_NODE" ]; then echo "$(basename "$0"): emsdk's node is not installed; run: ./install.sh install toolchain:web" >&2 exit 2 fi } webServe() { local tries local server server=$(joeyTool httpServe) || exit 1 for ((tries = 0; tries < 10; tries++)); do WEB_PORT=$((WEB_PORT_MIN + RANDOM % WEB_PORT_SPAN)) "$server" "$WEB_PORT" "$1" > /dev/null 2>&1 & WEB_SERVER_PID=$! sleep 0.5 if joeyAlive "$WEB_SERVER_PID"; then return 0 fi done echo "$(basename "$0"): could not start a web server" >&2 exit 1 } webSoundSink() { # shellcheck source=scripts/pulseRecordLib.sh source "$JOEY_REPO/scripts/pulseRecordLib.sh" pulseEnsureServer || return 1 WEB_SINK_NAME="joeyWeb$$" WEB_SINK_MODULE=$(pulseCreateSink "$WEB_SINK_NAME") || WEB_SINK_MODULE="" [ -n "$WEB_SINK_MODULE" ] } webDrive() { local page="$1" shift if [ -n "$WEB_SINK_MODULE" ]; then PULSE_SINK="$WEB_SINK_NAME" "$WEB_NODE" "$JOEY_REPO/scripts/lib/webDriver.mjs" --chrome "$WEB_CHROME" --url "http://127.0.0.1:$WEB_PORT/$page" --sound "$@" else "$WEB_NODE" "$JOEY_REPO/scripts/lib/webDriver.mjs" --chrome "$WEB_CHROME" --url "http://127.0.0.1:$WEB_PORT/$page" "$@" fi } webStop() { joeyStop "$WEB_SERVER_PID" WEB_SERVER_PID="" if [ -n "$WEB_SINK_MODULE" ]; then pulseUnloadSink "$WEB_SINK_MODULE" WEB_SINK_MODULE="" fi }