Files
hyperframes/packages/aws-lambda/scripts/build-zip.test.ts
T
James RussoandClaude Opus 5.5 81d73d2278 fix(aws-lambda): chrome-headless-shell 158 in the arm64 video image, sparticuz 153 in the x86 ZIP (#8155)
* fix(aws-lambda): run chrome-headless-shell 158 in the arm64 video image

The arm64 video image now installs Chrome for Testing's linux-arm64 chrome-headless-shell
158.0.8127.36, pinned by sha256 and fetched with curl -f, as the whole shell folder. The Lambda
Node 22 base has none of the shared libraries the shell links and no fonts, so the image installs
them from the base's repository at the same pinned release. The image sets
HYPERFRAMES_LAMBDA_CHROME_SOURCE and HYPERFRAMES_LAMBDA_CHROME_PATH itself, and the nested video
function no longer passes ChromeSource, which would override them. stage-arm64-image.ts stops
fetching the sparticuz arm64 pack and drops the ZIP's x86 sparticuz Chrome instead.

The x86 handler ZIP stays on @sparticuz/chromium, bumped from 148.0.0 to 153.0.0. It builds at
258,502,222 bytes unzipped, under Lambda's 262,144,000-byte limit and the 248 MiB in-house gate.

Two fixes for --source=chrome-headless-shell:
- build-zip.ts staged only the executable, which exits at launch without its ICU data and .pak
  files. It now stages the whole shell folder at chrome-headless-shell-linux64/.
- Nothing set HYPERFRAMES_LAMBDA_CHROME_PATH, which src/chromium.ts requires for that source.
  The SAM template and the CDK stack now set it to the bundled shell when ChromeSource asks for it.

Drops the unused @types/aws-lambda devDependency, which the dead-code audit reports whenever this
package's manifest changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* ci(publish): build the Lambda arm64 video image on an ARM runner

The video stage now installs Chrome's system libraries with microdnf, so it runs code for arm64.
The pack job built that image on ubuntu-latest with no emulation, which it could do only while the
stage copied files. A new lambda-video-image job builds it natively on ubuntu-24.04-arm, from the
handler zip whose sha256 the zip job reported, and reports the OCI tarball's sha256. Pack downloads
that tarball, checks the sha256 against the job's, and pins it as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore(aws-lambda): fit the comment ratchet and the package size budget

chromium.ts loses the two doc lines on BUNDLED_SHELL_DIR and BUNDLED_SHELL_PATH; the names and the
SAM template's comment already say what they are.

@hyperframes/aws-lambda's unpacked budget goes from 290,508 to 298,000 bytes. The package unpacks to
295,324 bytes, up from the shared chrome-headless-shell staging module the ZIP and image scripts now
import and from the README and docs that describe the two Chrome sources.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 09:07:46 +00:00

135 lines
5.7 KiB
TypeScript

import { describe, expect, it } from "bun:test";
import { spawnSync } from "node:child_process";
import { mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { pathToFileURL } from "node:url";
import * as esbuild from "esbuild";
import { BUNDLED_SHELL_DIR, BUNDLED_SHELL_PATH } from "../src/chromium.js";
import { stageChromeHeadlessShell } from "./_chromeHeadlessShell.js";
import { HANDLER_BANNER } from "./_handlerBanner.js";
// The handler ships as ESM (.mjs) but inlines CJS deps that assume Node's CJS
// globals exist at module scope: postcss et al. call top-level `require(...)`,
// and wawoff2's emscripten build reads `__dirname`. A freshly deployed stack
// crashed on every render (#1932) with "__dirname is not defined in ES module
// scope"; the fix is the require/__filename/__dirname shim in HANDLER_BANNER.
//
// This bundles a fixture touching all three globals with the REAL banner and
// imports the output, so it catches a dropped/renamed shim behaviourally
// rather than by grepping for literals (which survives a broken refactor).
//
// The import MUST run under Node, not the `bun test` runtime: Bun defines
// `__dirname`/`__filename` even in ESM, which would mask a missing shim and
// green-light a broken bundle. Lambda runs Node, so we spawn `node` (guaranteed
// present in CI alongside bun) to reproduce the deploy target faithfully.
describe("build-zip handler banner", () => {
it("shims require/__filename/__dirname so inlined CJS deps import under Node", () => {
const dir = mkdtempSync(join(tmpdir(), "hf-banner-test-"));
try {
const entry = join(dir, "fixture.ts");
const outfile = join(dir, "out.mjs");
// Reference each CJS global at module top level, the way inlined deps do.
// If any shim is missing, importing `out.mjs` throws at eval time.
writeFileSync(
entry,
[
"const cjsDir = __dirname;",
"const cjsFile = __filename;",
"const path = require('node:path');",
"if (typeof cjsDir !== 'string') throw new Error('__dirname missing');",
"if (typeof cjsFile !== 'string') throw new Error('__filename missing');",
"if (typeof path.join !== 'function') throw new Error('require missing');",
"console.log('BANNER_OK');",
].join("\n"),
);
esbuild.buildSync({
bundle: true,
platform: "node",
target: "node22",
format: "esm",
entryPoints: [entry],
outfile,
banner: { js: HANDLER_BANNER },
});
// Import under real Node — Lambda's runtime — not the bun test runtime.
const res = spawnSync(
"node",
[
"--input-type=module",
"-e",
`await import(${JSON.stringify(pathToFileURL(outfile).href)});`,
],
{ encoding: "utf8" },
);
// A missing shim surfaces as a non-zero exit + ReferenceError on stderr.
// Guard against a silent skip if `node` isn't on PATH (it is in CI).
expect(res.error).toBeUndefined();
expect(res.stderr).not.toContain("is not defined in ES module scope");
expect(res.stderr).not.toContain("Dynamic require");
expect(res.status).toBe(0);
expect(res.stdout).toContain("BANNER_OK");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
});
describe("build-zip chrome-headless-shell staging", () => {
function puppeteerCache(versions: Record<string, Record<string, string>>): string {
const home = mkdtempSync(join(tmpdir(), "hf-shell-cache-"));
for (const [version, files] of Object.entries(versions)) {
const folder = join(home, ".cache/puppeteer/chrome-headless-shell", version);
for (const [path, body] of Object.entries(files)) {
const file = join(folder, "chrome-headless-shell-linux64", path);
mkdirSync(dirname(file), { recursive: true });
writeFileSync(file, body, { mode: path === "chrome-headless-shell" ? 0o755 : 0o644 });
}
}
return home;
}
it("stages the whole newest shell folder where the template and the CDK stack point", () => {
const home = puppeteerCache({
"99.0.0.1": { "chrome-headless-shell": "old" },
"158.0.8127.36": {
"chrome-headless-shell": "exe",
"icudtl.dat": "icu",
"headless_lib_data.pak": "pak",
"locales/en-US.pak": "locale",
},
});
const staging = mkdtempSync(join(tmpdir(), "hf-shell-staging-"));
try {
expect(stageChromeHeadlessShell(staging, home)).toBe("158.0.8127.36");
const shell = join(staging, BUNDLED_SHELL_DIR);
// Without icudtl.dat beside it the shell exits at launch, so the folder ships whole.
expect(readFileSync(join(shell, "icudtl.dat"), "utf-8")).toBe("icu");
expect(readFileSync(join(shell, "headless_lib_data.pak"), "utf-8")).toBe("pak");
expect(readFileSync(join(shell, "locales/en-US.pak"), "utf-8")).toBe("locale");
expect(readFileSync(join(shell, "chrome-headless-shell"), "utf-8")).toBe("exe");
expect(statSync(join(shell, "chrome-headless-shell")).mode & 0o111).not.toBe(0);
expect(join("/var/task", BUNDLED_SHELL_DIR, "chrome-headless-shell")).toBe(
BUNDLED_SHELL_PATH,
);
} finally {
rmSync(home, { recursive: true, force: true });
rmSync(staging, { recursive: true, force: true });
}
});
it("names the install command when the cache is missing", () => {
const home = mkdtempSync(join(tmpdir(), "hf-shell-nocache-"));
try {
expect(() => stageChromeHeadlessShell(home, home)).toThrow(
/@puppeteer\/browsers install chrome-headless-shell@/,
);
} finally {
rmSync(home, { recursive: true, force: true });
}
});
});