Repository navigation
test(lambda): behavioral guard for handler CJS banner shims (follow-up to #1932) - #1977
Merged
Merged
Conversation
Miguel's fix (f999b40) added the require/__filename/__dirname shims to the Lambda handler bundle after #1932 crashed every render at import with "__dirname is not defined in ES module scope" (wawoff2's emscripten build reads __dirname at module scope; it's inlined via producer -> fontCompression). The accompanying test only grepped build-zip.ts for the banner literals, so it passes even if the shim is renamed, reordered into a broken form, or if a new inlined CJS dep needs a global the banner doesn't provide. Replace it with a behavioral test: extract the banner to _handlerBanner.ts (build-zip.ts self-executes on import, so it can't be imported directly), bundle a fixture that touches __dirname/__filename/require with the real banner, and import the output under real Node -- not the bun test runtime, which defines __dirname in ESM and would mask a missing shim. The import faithfully reproduces Lambda's Node ESM environment and fails with the exact #1932 error when any shim is dropped. Handler bundle output is unchanged (identical banner string). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
miguel-heygen
approved these changes
Jul 6, 2026
dahans-msft2
pushed a commit
to dahans-msft2/hyperframes
that referenced
this pull request
Aug 6, 2026
…om#1977) Miguel's fix (f999b40) added the require/__filename/__dirname shims to the Lambda handler bundle after heygen-com#1932 crashed every render at import with "__dirname is not defined in ES module scope" (wawoff2's emscripten build reads __dirname at module scope; it's inlined via producer -> fontCompression). The accompanying test only grepped build-zip.ts for the banner literals, so it passes even if the shim is renamed, reordered into a broken form, or if a new inlined CJS dep needs a global the banner doesn't provide. Replace it with a behavioral test: extract the banner to _handlerBanner.ts (build-zip.ts self-executes on import, so it can't be imported directly), bundle a fixture that touches __dirname/__filename/require with the real banner, and import the output under real Node -- not the bun test runtime, which defines __dirname in ESM and would mask a missing shim. The import faithfully reproduces Lambda's Node ESM environment and fails with the exact heygen-com#1932 error when any shim is dropped. Handler bundle output is unchanged (identical banner string). Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
hanzo-dev
pushed a commit
to hanzoai/frames
that referenced
this pull request
Sep 11, 2026
…om#1977) Miguel's fix (751fdd6) added the require/__filename/__dirname shims to the Lambda handler bundle after heygen-com#1932 crashed every render at import with "__dirname is not defined in ES module scope" (wawoff2's emscripten build reads __dirname at module scope; it's inlined via producer -> fontCompression). The accompanying test only grepped build-zip.ts for the banner literals, so it passes even if the shim is renamed, reordered into a broken form, or if a new inlined CJS dep needs a global the banner doesn't provide. Replace it with a behavioral test: extract the banner to _handlerBanner.ts (build-zip.ts self-executes on import, so it can't be imported directly), bundle a fixture that touches __dirname/__filename/require with the real banner, and import the output under real Node -- not the bun test runtime, which defines __dirname in ESM and would mask a missing shim. The import faithfully reproduces Lambda's Node ESM environment and fails with the exact heygen-com#1932 error when any shim is dropped. Handler bundle output is unchanged (identical banner string). Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #1932
The root cause of #1932 — the Lambda handler crashing at import with
__dirname is not defined in ES module scope— is already fixed and released (Miguel'sf999b40d, in v0.7.35). This PR is a follow-up that hardens the regression test so the fix can't silently rot.Why the existing test doesn't guard the bug
build-zip.test.tstoday just readsbuild-zip.tsas text and asserts the four banner literals appear in the source:That's tautological — it never bundles or runs anything. It stays green if the shim is renamed, reordered into a non-working form, or if a new inlined CJS dep needs a global the banner doesn't provide. It would not have caught the original break in a refactor.
What this changes
scripts/_handlerBanner.tsasHANDLER_BANNER.build-zip.tsself-executesmain()on import, so a test can't import a constant from it directly; a sibling_-module matches the existing_formatBytes.tsconvention. The handler bundle output is byte-identical (same banner string).__dirname/__filename/requireat module top level with the realHANDLER_BANNER, then import the output. It fails with the exact lambda: handler.mjs crashes at import — CJS deps bundled into ESM without a __dirname shim (wawoff2) #1932 error if any shim is dropped.bun test. Bun defines__dirnameeven in ESM, which would mask a missing shim and green-light a broken bundle. Lambda runs Node, so the test spawnsnode(present in the CI test job alongside bun) to reproduce the deploy target faithfully. Verified: removing the__dirnameshim makes the test fail withReferenceError: __dirname is not defined in ES module scope.Scope notes (from investigating "does this affect other packages?")
@hyperframes/producerand@hyperframes/clialready carry the fullrequire/__filename/__dirnamebanner — not affected.@hyperframes/gcp-cloud-runkeeps@hyperframes/producerexternal in its esbuild config, sowawoff2is never inlined into it (it loads producer's own shimmeddistat runtime) — not affected.aws-lambdais the only bundle that inlines producer source, which is why it was uniquely exposed.build.mjs, cli/tsup.config.ts, aws-lambda/build-zip.ts). This class of drift is exactly what caused lambda: handler.mjs crashes at import — CJS deps bundled into ESM without a __dirname shim (wawoff2) #1932. A single shared banner constant across all three would remove the drift risk — happy to do that as a separate change if we want it.Test plan
bun test scripts/build-zip.test.ts→ pass__dirnameshim from_handlerBanner.ts→ test fails with the lambda: handler.mjs crashes at import — CJS deps bundled into ESM without a __dirname shim (wawoff2) #1932ReferenceError🤖 Generated with Claude Code