Skip to content

test(lambda): behavioral guard for handler CJS banner shims (follow-up to #1932) - #1977

Merged
jrusso1020 merged 1 commit into
mainfrom
fix/lambda-banner-behavioral-test
Jul 6, 2026
Merged

jrusso1020 merged 1 commit into
mainfrom
fix/lambda-banner-behavioral-test

Conversation

@jrusso1020

Copy link
Copy Markdown
Collaborator

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's f999b40d, 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.ts today just reads build-zip.ts as text and asserts the four banner literals appear in the source:

expect(source).toContain('import { dirname as __hf_dirname } from "path";');
// ...three more string matches

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

  • Extract the banner into scripts/_handlerBanner.ts as HANDLER_BANNER. build-zip.ts self-executes main() on import, so a test can't import a constant from it directly; a sibling _-module matches the existing _formatBytes.ts convention. The handler bundle output is byte-identical (same banner string).
  • Behavioral test: bundle a fixture that touches __dirname / __filename / require at module top level with the real HANDLER_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.
  • Import under real Node, not bun test. Bun defines __dirname even in ESM, which would mask a missing shim and green-light a broken bundle. Lambda runs Node, so the test spawns node (present in the CI test job alongside bun) to reproduce the deploy target faithfully. Verified: removing the __dirname shim makes the test fail with ReferenceError: __dirname is not defined in ES module scope.

Scope notes (from investigating "does this affect other packages?")

  • @hyperframes/producer and @hyperframes/cli already carry the full require/__filename/__dirname banner — not affected.
  • @hyperframes/gcp-cloud-run keeps @hyperframes/producer external in its esbuild config, so wawoff2 is never inlined into it (it loads producer's own shimmed dist at runtime) — not affected. aws-lambda is the only bundle that inlines producer source, which is why it was uniquely exposed.
  • Longer-term option (not in this PR): the same CJS-interop banner is hand-maintained in three build configs (producer/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

🤖 Generated with Claude Code

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>
@jrusso1020
jrusso1020 merged commit 2b1109d into main Jul 6, 2026
39 checks passed
@jrusso1020
jrusso1020 deleted the fix/lambda-banner-behavioral-test branch July 6, 2026 04:51
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>
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