Skip to content

Commit 96a6e8b

Browse files
vanceingallsclaude
andcommitted
fix(engine): keep the PNG CRC working on Node 22.0/22.1
zlib.crc32 landed in Node 22.2.0, but engine and cli both declare `"node": ">=22"` and the runtime gate is major-only, so 22.0 and 22.1 are supported. A NAMED import of a missing export throws at module EVALUATION — ffprobe.ts would have failed to load at all on those runtimes, before any PNG was touched, taking every probe with it. Namespace import plus a capability check, with the previous bit-at-a-time implementation retained as the fallback. Modern runtimes keep the 210ms -> 1.3ms win; older ones keep working. Raising the floor to >=22.2.0 was the alternative, but that is a user-facing support change and does not belong in a PNG bug fix. Tests: the same HDR PNG parses identically with the native export absent, and a corrupt chunk still rejects on the fallback path. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 2af3f4d commit 96a6e8b

2 files changed

Lines changed: 69 additions & 5 deletions

File tree

‎packages/engine/src/utils/ffprobe.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,3 +709,44 @@ describe("PNG chunk walk — integrity of the fallback itself", () => {
709709
).toBeNull();
710710
});
711711
});
712+
713+
describe("crc32 works on every runtime the package declares", () => {
714+
afterEach(() => {
715+
vi.resetModules();
716+
vi.doUnmock("node:zlib");
717+
});
718+
719+
/** Load ffprobe.ts as it would evaluate on Node 22.0/22.1. */
720+
async function loadWithoutNativeCrc32() {
721+
const actual = await vi.importActual<typeof import("node:zlib")>("node:zlib");
722+
vi.resetModules();
723+
// zlib.crc32 landed in 22.2.0, but engine and cli both declare
724+
// `"node": ">=22"` behind a major-only gate. A NAMED import of a missing
725+
// export throws at module evaluation, so ffprobe.ts would fail to load
726+
// entirely on those runtimes — before any PNG is touched.
727+
vi.doMock("node:zlib", () => ({ ...actual, crc32: undefined }));
728+
return import("./ffprobe.js");
729+
}
730+
731+
it("parses an HDR PNG identically with the native crc32 unavailable", async () => {
732+
const png = buildPngWithChunks([
733+
pngChunk("IHDR", [0, 0, 0x0f, 0, 0, 0, 0x08, 0x70, 16, 2, 0, 0, 0]),
734+
pngChunk("cICP", [9, 16, 0, 1]),
735+
pngChunk("IEND", []),
736+
]);
737+
const withNative = extractPngMetadataFromBuffer(png);
738+
expect(withNative?.colorSpace?.colorTransfer).toBe("smpte2084");
739+
740+
const fresh = await loadWithoutNativeCrc32();
741+
expect(fresh.extractPngMetadataFromBuffer(png)).toEqual(withNative);
742+
});
743+
744+
it("rejects a corrupt chunk on the fallback path too", async () => {
745+
const bad = pngChunk("IHDR", [0, 0, 0x0f, 0, 0, 0, 0x08, 0x70, 16, 2, 0, 0, 0]);
746+
bad[bad.length - 1] ^= 0xff;
747+
const png = buildPngWithChunks([bad, pngChunk("IEND", [])]);
748+
749+
const fresh = await loadWithoutNativeCrc32();
750+
expect(fresh.extractPngMetadataFromBuffer(png)).toBeNull();
751+
});
752+
});

‎packages/engine/src/utils/ffprobe.ts‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// fallow-ignore-file code-duplication complexity
22
import { spawn } from "child_process";
33
import { readFileSync } from "fs";
4-
import { crc32 } from "node:zlib";
4+
import * as zlib from "node:zlib";
55
import { basename, extname } from "path";
66
import { redactTelemetryString } from "@hyperframes/core";
77
import { FFPROBE_PATH_ENV, getFfprobeBinary } from "./ffmpegBinaries.js";
@@ -170,11 +170,34 @@ interface StillImageMetadata {
170170

171171
// node:zlib's crc32 is native and takes a running seed, so the chunk type and
172172
// the chunk data can be CRC'd in sequence without concatenating them into a
173-
// throwaway buffer. The hand-rolled bit-at-a-time loop this replaces cost
174-
// ~210 ms on a 12 MiB PNG; this is ~1.3 ms. Available on this repo's
175-
// "node": ">=22".
173+
// throwaway buffer: ~210 ms -> ~1.3 ms on a 12 MiB PNG.
174+
//
175+
// It landed in Node 22.2.0, and this repo declares `"node": ">=22"` with a
176+
// major-only runtime gate, so 22.0 and 22.1 are still supported. A NAMED
177+
// import of a missing export throws at module evaluation — i.e. `ffprobe.ts`
178+
// would fail to load at all on those, long before any PNG is parsed — so the
179+
// namespace import plus this capability check is deliberate. Raising the
180+
// floor to 22.2.0 instead would be a user-facing support change, which does
181+
// not belong in a PNG bug fix.
182+
const nativeCrc32 = typeof zlib.crc32 === "function" ? zlib.crc32 : undefined;
183+
184+
/** Bit-at-a-time fallback for Node 22.0/22.1. Correct, just slower. */
185+
function crc32Fallback(data: Buffer, seed: number): number {
186+
let crc = seed ^ 0xffffffff;
187+
for (let i = 0; i < data.length; i++) {
188+
crc ^= data[i] ?? 0;
189+
for (let bit = 0; bit < 8; bit++) {
190+
const mask = -(crc & 1);
191+
crc = (crc >>> 1) ^ (0xedb88320 & mask);
192+
}
193+
}
194+
return (crc ^ 0xffffffff) >>> 0;
195+
}
196+
176197
function chunkCrc32(chunkType: string, chunkData: Buffer): number {
177-
return crc32(chunkData, crc32(Buffer.from(chunkType, "ascii")));
198+
const typeBytes = Buffer.from(chunkType, "ascii");
199+
if (nativeCrc32) return nativeCrc32(chunkData, nativeCrc32(typeBytes));
200+
return crc32Fallback(chunkData, crc32Fallback(typeBytes, 0));
178201
}
179202

180203
export function extractPngMetadataFromBuffer(buf: Buffer): StillImageMetadata | null {

0 commit comments

Comments
 (0)