Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions js/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ build: \
(build-inner "seasons") \
(build-inner "transforms")
just fix-package-json
just fix-wasm-streaming-fallback
just copy-readme

build-inner target args='':
Expand All @@ -32,6 +33,80 @@ fix-package-json:
jq < augurs/package.json ". | .version = \"$VERSION\"" > augurs/package.json.tmp
mv augurs/package.json.tmp augurs/package.json

# Patch wasm-bindgen's generated __wbg_load to always fall back to the
# non-streaming WebAssembly.instantiate path. The generated code rethrows
# when instantiateStreaming fails and Content-Type is application/wasm,
# but this breaks in environments where fetch() returns a Proxy-wrapped
# Response (e.g. OpenTelemetry instrumentation-fetch) that passes JS
# `instanceof Response` but fails the browser's internal C++ slot check.
# Related: https://github.com/open-telemetry/opentelemetry-js/issues/5477
fix-wasm-streaming-fallback:
#!/usr/bin/env python3
import re, glob, sys
# wasm-bindgen 0.2.101-0.2.106: multi-line else block
CATCH_V1 = re.compile(
r'(\s*)\} catch \(e\) \{\s*\n'
r'\s*const validResponse.*?\n'
r'\s*\n'
r'\s*if \(validResponse.*?\n'
r'\s*console\.warn\(.*?\n'
r'\s*\n'
r'\s*\} else \{\s*\n'
r'\s*throw e;\s*\n'
r'\s*\}'
)
# wasm-bindgen 0.2.107+: single-line else block
CATCH_V2 = re.compile(
r'(\s*)\} catch \(e\) \{\s*\n'
r'\s*const validResponse.*?\n'
r'\s*\n'
r'\s*if \(validResponse.*?\n'
r'\s*console\.warn\(.*?\n'
r'\s*\n'
r'\s*\} else \{ throw e; \}'
)
CATCH_REPL = (
r'\1} catch (e) {\1 console.warn('
r'"`WebAssembly.instantiateStreaming` failed. Falling back to '
r'`WebAssembly.instantiate` which is slower. Original error:\\n", e);'
)
# Module-level Set (wasm-bindgen 0.2.101-0.2.106)
EXPECTED_TYPES_SET_RE = re.compile(r"const EXPECTED_RESPONSE_TYPES[^\n]*\n\n")
# Local function inside __wbg_load (wasm-bindgen 0.2.107+)
EXPECTED_TYPE_FN_RE = re.compile(
r"\n function expectedResponseType\(type\) \{"
r".*?return false;\n \}\n",
re.DOTALL,
)
errors = []
for f in sorted(glob.glob("augurs/*.js")):
text = open(f).read()
if "instantiateStreaming" not in text:
continue
# Replace the conditional rethrow with an unconditional warn + fallback.
# Try both patterns; only one will match depending on wasm-bindgen version.
text, n = CATCH_V2.subn(CATCH_REPL, text)
if n == 0:
text, n = CATCH_V1.subn(CATCH_REPL, text)
if n == 0:
errors.append(f"{f}: expected to patch __wbg_load but regex didn't match")
continue
Comment on lines +88 to +93
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Idempotency gap: running the recipe standalone on already-patched files produces a misleading error.

When both CATCH_V2.subn and CATCH_V1.subn return n == 0 on a previously-patched file, the error appended is "expected to patch __wbg_load but regex didn't match", and the final message printed is "The wasm-bindgen output format may have changed. Update the regex in js/justfile." A developer retrying without a clean rebuild (e.g., after a partial build failure) would chase a phantom wasm-bindgen format change. A simple content-based already-patched check avoids the false alarm:

♻️ Proposed fix — distinguish already-patched from genuinely unrecognised format
      if n == 0:
-         errors.append(f"{f}: expected to patch __wbg_load but regex didn't match")
-         continue
+         # If neither pattern is present either, the file may already be patched.
+         if not (CATCH_V1.search(text) or CATCH_V2.search(text)):
+             print(f"  {f}: already patched or no matching pattern — skipping")
+             continue
+         errors.append(f"{f}: instantiateStreaming present but regex didn't match; "
+                        f"wasm-bindgen output format may have changed")
+         continue
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@js/justfile` around lines 88 - 93, The current logic appends an error when
both CATCH_V2.subn and CATCH_V1.subn return n == 0 even if the file is already
patched; modify the branch after those two attempts to detect an already-patched
file (e.g., check if CATCH_REPL or a unique marker is present in text) and if so
skip adding the error and continue, otherwise keep appending the existing error;
adjust the code around the variables CATCH_V2, CATCH_V1, CATCH_REPL, text,
errors and f so the recipe is idempotent when run against already-patched files.

# Remove the now-unused response type check helper.
text = EXPECTED_TYPES_SET_RE.sub("", text)
text = EXPECTED_TYPE_FN_RE.sub("\n", text)
# Verify the original rethrow pattern is fully gone after patching.
if CATCH_V1.search(text) or CATCH_V2.search(text):
errors.append(f"{f}: patched but original rethrow pattern still present")
continue
open(f, "w").write(text)
print(f" patched {f} ({n} replacement(s))")
if errors:
print("ERROR: wasm-streaming-fallback patch failed:", file=sys.stderr)
for e in errors:
print(f" {e}", file=sys.stderr)
print("The wasm-bindgen output format may have changed. Update the regex in js/justfile.", file=sys.stderr)
sys.exit(1)

copy-readme:
cp README.md augurs/README.md

Expand Down