Files
Bose-SoundTouch/scripts/patch-stockholm-bridge.py
Tobias GesellchenandClaude Opus 4.7 0c4a12670b fix(stockholm): patch browser_http_proxy.js so the proxy URL respects basePath
Two patching gaps caused every Stockholm HTTP-proxy call from a
/stockholm/* page to hit /api/http-proxy (404) instead of the
basePath-prefixed /stockholm/api/http-proxy:

1. The proxy URL constant in browser_http_proxy.js is declared as
   `var PROXY_PATH` (uppercase). Our patch script only knew about the
   lowercase `var proxyPath` form used in app_comm.js, so it never
   matched the upstream file.

2. Even if the constant had matched, browser_http_proxy.js's IIFE
   evaluates the URL at script-load time — but the injected bootstrap
   that defines window.__stockholmBase is placed just before </head>,
   i.e. after the <script src=…> tags. The captured value would
   always fall back to the unprefixed "/api/http-proxy".

3. The Makefile never passed browser_http_proxy.js to the patch script
   at all.

Fix:

  - Add an uppercase `PROXY_PATH` replacement entry in
    patch-stockholm-bridge.py (keeps the lowercase one for
    app_comm.js).
  - Add a second replacement that rewrites the **use site** in
    browser_http_proxy.js to inline `(window.__stockholmBase||"") +
    "/api/http-proxy?url=" + ...`. Reading __stockholmBase at
    call-time bypasses the load-order trap; the patched
    `var PROXY_PATH = …` declaration above becomes dead code but
    stays harmless.
  - Pass `$(STOCKHOLM_DIR)/js/browser_http_proxy.js` to the patch
    script in the prepare-stockholm target so it actually gets
    rewritten.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-17 15:05:39 +02:00

59 lines
2.4 KiB
Python

#!/usr/bin/env python3
"""Patch Stockholm bridge JS files to use window.__stockholmBase for API paths.
Usage: patch-stockholm-bridge.py <file> [<file> ...]
Applied once by `make prepare-stockholm`. Idempotent — already-patched files
are left unchanged.
"""
import sys
REPLACEMENTS = [
(
'xhr.open("POST", "/api/native/appSend"',
'xhr.open("POST", (window.__stockholmBase||"") + "/api/native/appSend"',
),
(
'xhr.open("GET", "/api/native/runQueue',
'xhr.open("GET", (window.__stockholmBase||"") + "/api/native/runQueue',
),
(
'var proxyPath = "/api/http-proxy";',
'var proxyPath = (window.__stockholmBase||"") + "/api/http-proxy";',
),
(
# The standalone /api/http-proxy declaration in browser_http_proxy.js
# uses an UPPERCASE constant name. Same shape as above, different
# identifier — keep both replacements; the lowercase one applies to
# app_comm.js, the uppercase one to browser_http_proxy.js.
'var PROXY_PATH = "/api/http-proxy";',
'var PROXY_PATH = (window.__stockholmBase||"") + "/api/http-proxy";',
),
(
# browser_http_proxy.js's IIFE evaluates PROXY_PATH at script-load
# time, but the injected bootstrap that defines window.__stockholmBase
# is placed just before </head> — i.e. after the <script src=…> tags
# for the bridge files. So PROXY_PATH would always fall back to the
# unprefixed "/api/http-proxy", failing under STOCKHOLM_BASE_PATH.
# Inline a lazy expression at the use site so it reads __stockholmBase
# at call time, when bootstrap has finished. The var declaration above
# remains patched but becomes dead code.
'return PROXY_PATH + "?url=" + encodeURIComponent(target.href);',
'return (window.__stockholmBase||"") + "/api/http-proxy?url=" + encodeURIComponent(target.href);',
),
(
'return new URL(url, window.location.origin + "/").href;',
'return new URL(url, window.location.origin + (window.__stockholmBase || "") + "/").href;',
),
]
for path in sys.argv[1:]:
try:
original = open(path).read()
patched = original
for old, new in REPLACEMENTS:
patched = patched.replace(old, new)
if patched != original:
open(path, "w").write(patched)
except FileNotFoundError:
print(f"warning: {path} not found, skipping", file=sys.stderr)