Skip to content

docs(engine): document __name polyfill and add regression test#385

Open
vanceingalls wants to merge 1 commit intovance/hdr-image-transfer-cachefrom
vance/hdr-name-polyfill-investigation
Open

docs(engine): document __name polyfill and add regression test#385
vanceingalls wants to merge 1 commit intovance/hdr-image-transfer-cachefrom
vance/hdr-name-polyfill-investigation

Conversation

@vanceingalls
Copy link
Copy Markdown
Collaborator

@vanceingalls vanceingalls commented Apr 21, 2026

Summary

Document why the window.__name polyfill in frameCapture.ts is necessary, expand the inline comment with the full per-runtime matrix, and add a regression test that surfaces transpiler behavior on the next failure.

Outcome of the Chunk 12 investigation: keep the polyfill.

Why

Chunk 12 of plans/hdr-followups.md. The polyfill had a vague comment and no test, so it was unclear whether it was still needed or could be deleted.

Empirical findings

Probe in /tmp/hf-name-probe:

Runtime / build Injects __name(fn, "name") wrappers in Function.prototype.toString()?
bun (TS loader) No — verified for top-level and nested named functions / arrow expressions.
tsx (esbuild loader, keepNames=true) Yes for nested named functions / arrows; observed crash mode in dev/test.
tsc (noEmit and emit) No — does not inject the helper.
tsup for @hyperframes/cli (noExternal: ["@hyperframes/engine"]) Polyfill definition is bundled, but __name(...) call sites are absent in packages/cli/dist/cli.js (grepped).

Root cause. @hyperframes/engine's package.json exports raw TypeScript (main/exports./src/index.ts), so every consumer's transpiler decides whether to inject __name. Anything that runs through tsx (producer parity-harness, ad-hoc dev scripts, bun run --filter @hyperframes/engine test via Vitest's loader) will serialize wrapped function bodies into page.evaluate(...) and crash with ReferenceError: __name is not defined.

Decision. Keep the no-op window.__name shim. Cost is one evaluateOnNewDocument call. The alternative (rewriting every page.evaluate(fn) site to page.addScriptTag({ content: "..." }), like packages/cli/src/commands/contrast-audit.browser.js already does) is far more invasive and easy to regress.

What changed

  • Expanded the inline comment in packages/engine/src/services/frameCapture.ts to explain the per-runtime matrix above and point to the script-tag alternative.
  • New packages/engine/src/services/frameCapture-namePolyfill.test.ts — a pure unit test (matches the rest of the engine package's no-browser-launch convention) that:
    1. Asserts the polyfill is wired up via evaluateOnNewDocument and runs before the first awaited browser.version() call.
    2. Probes the active Vitest transpiler for __name(...) injection so the next maintainer can see at a glance whether the upstream behavior has shifted.

Test plan

  • bun run --filter @hyperframes/engine test → 408/408 pass (3 new tests in this file).
  • bunx tsc --noEmit -p packages/engine clean.
  • bunx oxlint and bunx oxfmt --check clean on edited files.

Stack

Chunk 12 of plans/hdr-followups.md. Independent of all other chunks; closes out the investigation item.

Copy link
Copy Markdown
Collaborator Author

vanceingalls commented Apr 21, 2026

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@vanceingalls vanceingalls force-pushed the vance/hdr-name-polyfill-investigation branch from 9da66c8 to 3058347 Compare April 21, 2026 20:48
@vanceingalls vanceingalls force-pushed the vance/hdr-image-transfer-cache branch 2 times, most recently from 21c0314 to 5fe390c Compare April 21, 2026 20:54
@vanceingalls vanceingalls force-pushed the vance/hdr-name-polyfill-investigation branch from 3058347 to 3ebfb55 Compare April 21, 2026 20:54
@vanceingalls vanceingalls marked this pull request as ready for review April 21, 2026 20:57
@vanceingalls vanceingalls force-pushed the vance/hdr-name-polyfill-investigation branch from 3ebfb55 to 574ba0e Compare April 21, 2026 22:37
@vanceingalls vanceingalls force-pushed the vance/hdr-image-transfer-cache branch from 5fe390c to 42ab080 Compare April 21, 2026 22:37
@vanceingalls vanceingalls force-pushed the vance/hdr-name-polyfill-investigation branch from 574ba0e to dec187d Compare April 22, 2026 01:16
@vanceingalls vanceingalls force-pushed the vance/hdr-image-transfer-cache branch from 42ab080 to 6d73ff8 Compare April 22, 2026 01:16
@vanceingalls vanceingalls force-pushed the vance/hdr-image-transfer-cache branch from 54c8e1f to b71cf6f Compare April 22, 2026 20:48
@vanceingalls vanceingalls force-pushed the vance/hdr-name-polyfill-investigation branch 2 times, most recently from 1178aee to fa5f89f Compare April 22, 2026 22:13
@vanceingalls vanceingalls force-pushed the vance/hdr-image-transfer-cache branch from 3c4754f to e49a01f Compare April 22, 2026 22:55
@vanceingalls vanceingalls force-pushed the vance/hdr-name-polyfill-investigation branch from fa5f89f to 39c89eb Compare April 22, 2026 22:55
@vanceingalls vanceingalls force-pushed the vance/hdr-image-transfer-cache branch from e49a01f to a801330 Compare April 22, 2026 23:26
@vanceingalls vanceingalls force-pushed the vance/hdr-name-polyfill-investigation branch from 39c89eb to 42ba7b2 Compare April 22, 2026 23:26
@vanceingalls vanceingalls force-pushed the vance/hdr-image-transfer-cache branch from a801330 to 2af81de Compare April 23, 2026 00:08
@vanceingalls vanceingalls force-pushed the vance/hdr-name-polyfill-investigation branch from 42ba7b2 to 1391c08 Compare April 23, 2026 00:09
@vanceingalls vanceingalls force-pushed the vance/hdr-image-transfer-cache branch from 2af81de to a3d4984 Compare April 23, 2026 00:12
@vanceingalls vanceingalls force-pushed the vance/hdr-name-polyfill-investigation branch 2 times, most recently from f5e524b to d42983e Compare April 23, 2026 00:45
@vanceingalls vanceingalls force-pushed the vance/hdr-image-transfer-cache branch from a3d4984 to b3241ce Compare April 23, 2026 00:45
@vanceingalls vanceingalls force-pushed the vance/hdr-name-polyfill-investigation branch from d42983e to cb904e6 Compare April 23, 2026 01:58
@vanceingalls vanceingalls force-pushed the vance/hdr-image-transfer-cache branch 2 times, most recently from 8b636ec to 6bc1dd6 Compare April 23, 2026 03:00
@vanceingalls vanceingalls force-pushed the vance/hdr-name-polyfill-investigation branch from cb904e6 to 85ef7aa Compare April 23, 2026 03:01
@vanceingalls vanceingalls force-pushed the vance/hdr-image-transfer-cache branch from 6bc1dd6 to b03a62f Compare April 23, 2026 03:23
@vanceingalls vanceingalls force-pushed the vance/hdr-name-polyfill-investigation branch from 85ef7aa to 3e5824c Compare April 23, 2026 03:24
@vanceingalls vanceingalls force-pushed the vance/hdr-image-transfer-cache branch from b03a62f to 31915d0 Compare April 23, 2026 03:45
@vanceingalls vanceingalls force-pushed the vance/hdr-name-polyfill-investigation branch from 3e5824c to d3c40de Compare April 23, 2026 03:45
@vanceingalls vanceingalls force-pushed the vance/hdr-image-transfer-cache branch from 31915d0 to 90bd43b Compare April 23, 2026 04:53
@vanceingalls vanceingalls force-pushed the vance/hdr-name-polyfill-investigation branch from d3c40de to cd24066 Compare April 23, 2026 04:53
@vanceingalls vanceingalls force-pushed the vance/hdr-image-transfer-cache branch from 90bd43b to 4555013 Compare April 23, 2026 05:13
@vanceingalls vanceingalls force-pushed the vance/hdr-name-polyfill-investigation branch from cd24066 to ed9217d Compare April 23, 2026 05:13
@vanceingalls vanceingalls force-pushed the vance/hdr-image-transfer-cache branch from 4555013 to 1f61054 Compare April 23, 2026 05:48
@vanceingalls vanceingalls force-pushed the vance/hdr-name-polyfill-investigation branch from ed9217d to 8821cd8 Compare April 23, 2026 05:49
Investigation under HDR follow-ups Chunk 12. The window.__name shim in
frameCapture.ts is necessary because @hyperframes/engine ships raw
TypeScript and consumers' transpilers may inject __name(fn,"name")
wrappers around named functions. When Puppeteer serializes a
page.evaluate(callback) via Function.prototype.toString(), those
wrappers travel into the browser and crash with
ReferenceError: __name is not defined.

Empirical findings (probe in /tmp/hf-name-probe):
- bun TS loader: does not inject __name.
- tsx (esbuild loader, keepNames=true): injects __name; this is the
  observed crash mode in dev/test (parity-harness, ad-hoc scripts,
  Vitest under bun run --filter @hyperframes/engine test).
- tsc: does not inject.
- tsup for @hyperframes/cli with noExternal: [@hyperframes/engine]:
  bundles the polyfill definition but emits no __name(...) call
  sites in dist/cli.js (verified by grep).

Root cause: engine package.json points main/exports at
./src/index.ts, so each consumer's transpiler decides whether
__name appears. Anything routed through tsx triggers the crash;
the polyfill makes it a no-op.

Decision: keep the shim. It's a single evaluateOnNewDocument call.
The alternative (rewriting every page.evaluate(fn) site to
page.addScriptTag with raw text, like contrast-audit.browser.js)
is significantly more invasive.

Changes:
- Expand the inline comment in frameCapture.ts so future maintainers
  see the full per-runtime matrix and the script-tag alternative.
- Add frameCapture-namePolyfill.test.ts: a pure unit test (matches
  the rest of the engine package's no-browser-launch convention)
  that asserts the polyfill is wired up, runs before the first
  awaited browser.version(), and probes the active transpiler so
  the next maintainer can see at a glance whether the upstream
  behavior has shifted.

Verified:
- bun run --filter @hyperframes/engine test -> 408/408 pass.
- bunx tsc --noEmit -p packages/engine clean.
- bunx oxlint and bunx oxfmt --check clean.
@vanceingalls vanceingalls force-pushed the vance/hdr-name-polyfill-investigation branch from 8821cd8 to 90225e4 Compare April 23, 2026 06:07
@vanceingalls vanceingalls force-pushed the vance/hdr-image-transfer-cache branch from 1f61054 to 0b54ff2 Compare April 23, 2026 06:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants