-
-
Notifications
You must be signed in to change notification settings - Fork 14
fix(sourcemap): crc32 is not a function on Node.js < 20.15 (CLI-34F) #1643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /** | ||
| * CRC-32 checksum that works on every Node.js version the npm package runs on. | ||
| * | ||
| * `zlib.crc32` only exists on Node.js >= 20.15 / >= 22.2, but the npm package | ||
| * accepts Node.js 18+ (see the version gate in `script/bundle.ts`). Older | ||
| * runtimes fall back to a table-driven implementation of the same IEEE 802.3 | ||
| * polynomial, so the checksums are identical either way. | ||
| */ | ||
|
|
||
| import zlib from "node:zlib"; | ||
|
|
||
| /** Reflected CRC-32 (IEEE 802.3) polynomial. */ | ||
| const POLYNOMIAL = 0xed_b8_83_20; | ||
|
|
||
| let table: Uint32Array | undefined; | ||
|
|
||
| // biome-ignore-start lint/suspicious/noBitwiseOperators: CRC-32 is defined in terms of bitwise operations | ||
| function getTable(): Uint32Array { | ||
| if (!table) { | ||
| table = new Uint32Array(256); | ||
| for (let n = 0; n < 256; n++) { | ||
| let c = n; | ||
| for (let k = 0; k < 8; k++) { | ||
| c = c & 1 ? POLYNOMIAL ^ (c >>> 1) : c >>> 1; | ||
| } | ||
| table[n] = c; | ||
| } | ||
| } | ||
| return table; | ||
| } | ||
|
|
||
| /** | ||
| * Portable CRC-32, used when `zlib.crc32` is unavailable. | ||
| * | ||
| * @internal Exported for testing | ||
| */ | ||
| export function crc32Fallback(data: Uint8Array, value = 0): number { | ||
| const lookup = getTable(); | ||
| let crc = ~value; | ||
| for (const byte of data) { | ||
| crc = (lookup[(crc ^ byte) & 0xff] as number) ^ (crc >>> 8); | ||
| } | ||
| return ~crc >>> 0; | ||
| } | ||
| // biome-ignore-end lint/suspicious/noBitwiseOperators: CRC-32 is defined in terms of bitwise operations | ||
|
|
||
| /** | ||
| * Compute the CRC-32 of `data`, optionally continuing from a previous | ||
| * checksum `value`. Returns an unsigned 32-bit integer, like `zlib.crc32`. | ||
| */ | ||
| export const crc32: (data: Uint8Array, value?: number) => number = | ||
| typeof zlib.crc32 === "function" ? zlib.crc32 : crc32Fallback; |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /** | ||
| * DeterministicZipWriter compatibility tests for Node.js versions without | ||
| * `zlib.crc32`. | ||
| */ | ||
|
|
||
| import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import { unzipSync } from "fflate"; | ||
| import { expect, test, vi } from "vitest"; | ||
|
|
||
| vi.mock("node:zlib", async (importOriginal) => { | ||
| const actual = await importOriginal<Record<string, unknown>>(); | ||
| const withoutCrc32 = { ...actual, crc32: undefined }; | ||
| return { ...withoutCrc32, default: withoutCrc32 }; | ||
| }); | ||
|
|
||
| // Import after the mock so crc32.ts selects its portable implementation. | ||
| import { DeterministicZipWriter } from "../../../src/lib/build/zip-writer.js"; | ||
|
|
||
| test("writes valid CRCs when zlib.crc32 is unavailable", async () => { | ||
| const tmpDir = await mkdtemp(join(tmpdir(), "zip-writer-no-crc32-")); | ||
| try { | ||
| const outputPath = join(tmpDir, "bundle.zip"); | ||
| const sourcePath = join(tmpDir, "source.bin"); | ||
| await writeFile(sourcePath, Buffer.from("streamed contents")); | ||
|
|
||
| const zip = await DeterministicZipWriter.create(outputPath); | ||
| await zip.addData("memory.txt", Buffer.from("in-memory contents")); | ||
| await zip.addFile("streamed.txt", sourcePath); | ||
| await zip.finalize(); | ||
|
|
||
| const entries = unzipSync(await readFile(outputPath)); | ||
| expect(Buffer.from(entries["memory.txt"])).toEqual( | ||
| Buffer.from("in-memory contents") | ||
| ); | ||
| expect(Buffer.from(entries["streamed.txt"])).toEqual( | ||
| Buffer.from("streamed contents") | ||
| ); | ||
| } finally { | ||
| await rm(tmpDir, { recursive: true, force: true }); | ||
| } | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /** | ||
| * Property-Based Tests for the portable CRC-32 fallback. | ||
| * | ||
| * The fallback must be byte-for-byte compatible with `zlib.crc32` (the | ||
| * native implementation on Node.js >= 20.15 / >= 22.2), including when a | ||
| * checksum is continued across chunks. | ||
| */ | ||
|
|
||
| import { crc32 as nativeCrc32 } from "node:zlib"; | ||
| import { assert as fcAssert, property, uint8Array } from "fast-check"; | ||
| import { describe, expect, test } from "vitest"; | ||
| import { crc32Fallback } from "../../src/lib/crc32.js"; | ||
| import { DEFAULT_NUM_RUNS } from "../model-based/helpers.js"; | ||
|
|
||
| describe("property: crc32Fallback", () => { | ||
| test("matches zlib.crc32 for arbitrary bytes", () => { | ||
| fcAssert( | ||
| property(uint8Array({ maxLength: 4096 }), (data) => { | ||
| expect(crc32Fallback(data)).toBe(nativeCrc32(data)); | ||
| }), | ||
| { numRuns: DEFAULT_NUM_RUNS } | ||
| ); | ||
| }); | ||
|
|
||
| test("continuing from a previous checksum equals hashing the concatenation", () => { | ||
| fcAssert( | ||
| property(uint8Array(), uint8Array(), (a, b) => { | ||
| const joined = Buffer.concat([a, b]); | ||
| expect(crc32Fallback(b, crc32Fallback(a))).toBe(crc32Fallback(joined)); | ||
| expect(crc32Fallback(b, nativeCrc32(a))).toBe(nativeCrc32(joined)); | ||
| }), | ||
| { numRuns: DEFAULT_NUM_RUNS } | ||
| ); | ||
| }); | ||
|
|
||
| test("produces the standard CRC-32 check value", () => { | ||
| expect(crc32Fallback(Buffer.from("123456789"))).toBe(0xcb_f4_39_26); | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /** | ||
| * ZipWriter on Node.js versions without `zlib.crc32` (< 20.15 / < 22.2). | ||
| * Kept in a separate file so the node:zlib mock doesn't leak into zip.test.ts. | ||
| */ | ||
|
|
||
| import { spawnSync } from "node:child_process"; | ||
| import { mkdtemp, rm } from "node:fs/promises"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import { afterEach, beforeEach, expect, test, vi } from "vitest"; | ||
|
|
||
| vi.mock("node:zlib", async (importOriginal) => { | ||
| const actual = await importOriginal<Record<string, unknown>>(); | ||
| const withoutCrc32 = { ...actual, crc32: undefined }; | ||
| return { ...withoutCrc32, default: withoutCrc32 }; | ||
| }); | ||
|
|
||
| // Import AFTER the mock so zip.ts sees a node:zlib without crc32. | ||
| import { ZipWriter } from "../../../src/lib/sourcemap/zip.js"; | ||
|
|
||
| let tmpDir: string; | ||
|
|
||
| beforeEach(async () => { | ||
| tmpDir = await mkdtemp(join(tmpdir(), "zip-no-crc32-")); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await rm(tmpDir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| test("writes archives with valid CRCs when zlib.crc32 is unavailable", async () => { | ||
| const zipPath = join(tmpDir, "bundle.zip"); | ||
| const zip = await ZipWriter.create(zipPath); | ||
| await zip.addEntry("bundle.js", Buffer.from("console.log(1);\n")); | ||
| await zip.addEntry("bundle.js.map", Buffer.from('{"version":3}')); | ||
| await zip.finalize(); | ||
|
|
||
| // `unzip -t` recomputes each entry's CRC-32 and fails on a mismatch. | ||
| const proc = spawnSync("unzip", ["-t", zipPath], { | ||
| stdio: ["pipe", "pipe", "pipe"], | ||
| }); | ||
| expect(proc.stderr.toString()).toBe(""); | ||
| expect(proc.status).toBe(0); | ||
| }); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.