Repository navigation
node:zlib: accept zstd pledgedSrcSize values of 4 GiB and larger #33442
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
Open
robobun
wants to merge
6
commits into
main
Choose a base branch
from
farm/145e3845/zstd-pledged-src-size
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6a09d54
node:zlib: accept zstd pledgedSrcSize values of 4 GiB and larger
robobun 570f788
[autofix.ci] apply automated fixes
autofix-ci[bot] 28ce99c
Harden the streaming pledgedSrcSize test and cover NaN
robobun 82dbeaf
Run the zstd pledgedSrcSize test on Bun and on Node.js
robobun 6977076
Remove the comment above the pledgedSrcSize validator
robobun 2ad1e02
Merge remote-tracking branch 'origin/main' into farm/145e3845/zstd-pl…
robobun 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,115 @@ | ||
| /** | ||
| * All tests in this file run in both Bun and Node.js: `bun test` runs them | ||
| * here, and the last test runs this same file under Node.js. | ||
| * | ||
| * zstd writes `pledgedSrcSize` into the frame header as the Frame_Content_Size, | ||
| * a field of up to 8 bytes, so a size of 4 GiB or more is valid. The expected | ||
| * values below were recorded from Node v26.3.0 and v26.10.0. | ||
| */ | ||
| import assert from "node:assert"; | ||
| import { describe, test } from "node:test"; | ||
| import { fileURLToPath } from "node:url"; | ||
| import { promisify } from "node:util"; | ||
| import zlib from "node:zlib"; | ||
|
|
||
| // Node validates the option as a non-negative safe integer since nodejs/node | ||
| // de6eeece8f (v26.7.0). An older Node truncates or clamps such a value, so the | ||
| // reject cases only describe it from that version on. Bun always runs them. | ||
| const runtimeValidates = (() => { | ||
| if (process.versions.bun) return true; | ||
| const [major, minor] = process.versions.node.split(".").map(Number); | ||
| return major > 26 || (major === 26 && minor >= 7); | ||
| })(); | ||
| const rejectTest = runtimeValidates ? test : test.skip; | ||
|
|
||
| // https://github.com/facebook/zstd/blob/dev/doc/zstd_compression_format.md#frame_header | ||
| // magic(4) | frame header descriptor(1) | [window descriptor(1)] | [dictionary id] | [frame content size] | ||
| function frameContentSize(frame: Buffer): bigint | null { | ||
| assert.strictEqual(frame.readUInt32LE(0), 0xfd2fb528); | ||
| const descriptor = frame[4]; | ||
| const singleSegment = (descriptor >> 5) & 1; | ||
| const offset = 5 + (singleSegment ? 0 : 1) + [0, 1, 2, 4][descriptor & 0b11]; | ||
| switch (descriptor >> 6) { | ||
| case 0: | ||
| return singleSegment ? BigInt(frame[offset]) : null; | ||
| case 1: | ||
| return BigInt(frame.readUInt16LE(offset) + 256); | ||
| case 2: | ||
| return BigInt(frame.readUInt32LE(offset)); | ||
| default: | ||
| return frame.readBigUInt64LE(offset); | ||
| } | ||
| } | ||
|
|
||
| // Finishing with ZSTD_e_flush instead of ZSTD_e_end means zstd never checks the pledged size | ||
| // against what was actually written, so these can pledge 4 GiB without feeding it 4 GiB. | ||
| // The pledge still goes into the frame header. | ||
| const input = Buffer.alloc(64, 0x61); | ||
| const flushOnly = (pledgedSrcSize: number) => ({ pledgedSrcSize, finishFlush: zlib.constants.ZSTD_e_flush }); | ||
|
|
||
| describe("zstd pledgedSrcSize", () => { | ||
| test("is written to the frame header unmodified when below 4 GiB", () => { | ||
| assert.strictEqual(frameContentSize(zlib.zstdCompressSync(input, flushOnly(1000))), 1000n); | ||
| assert.strictEqual(frameContentSize(zlib.zstdCompressSync(input, flushOnly(2 ** 32 - 1))), 4294967295n); | ||
| }); | ||
|
|
||
| test("accepts 4 GiB and larger", () => { | ||
| assert.strictEqual(frameContentSize(zlib.zstdCompressSync(input, flushOnly(2 ** 32))), 4294967296n); | ||
| assert.strictEqual(frameContentSize(zlib.zstdCompressSync(input, flushOnly(2 ** 33 + 1))), 8589934593n); | ||
| assert.strictEqual( | ||
| frameContentSize(zlib.zstdCompressSync(input, flushOnly(Number.MAX_SAFE_INTEGER))), | ||
| 9007199254740991n, | ||
| ); | ||
| }); | ||
|
|
||
| test("accepts 4 GiB and larger when compressing asynchronously", async () => { | ||
| const compressed = await promisify(zlib.zstdCompress)(input, flushOnly(2 ** 32)); | ||
| assert.strictEqual(frameContentSize(compressed), 4294967296n); | ||
| }); | ||
|
|
||
| test("accepts 4 GiB and larger when streaming", async () => { | ||
| const encoder = zlib.createZstdCompress({ pledgedSrcSize: 2 ** 32 }); | ||
| try { | ||
| const chunks: Buffer[] = []; | ||
| const { promise, resolve, reject } = Promise.withResolvers<void>(); | ||
| encoder.on("data", chunk => chunks.push(chunk)); | ||
| encoder.on("error", reject); | ||
| encoder.on("close", () => reject(new Error("encoder closed before the flush completed"))); | ||
| encoder.write(input, err => err && reject(err)); | ||
| encoder.flush(() => resolve()); | ||
| await promise; | ||
| assert.strictEqual(frameContentSize(Buffer.concat(chunks)), 4294967296n); | ||
| } finally { | ||
| encoder.destroy(); | ||
| } | ||
| }); | ||
|
|
||
| for (const pledgedSrcSize of [Number.MAX_SAFE_INTEGER + 1, -1, 1.9, Infinity, -Infinity, NaN]) { | ||
| rejectTest(`rejects ${pledgedSrcSize}`, () => { | ||
| const expected = { name: "RangeError", code: "ERR_OUT_OF_RANGE" }; | ||
| assert.throws(() => zlib.createZstdCompress({ pledgedSrcSize }), expected); | ||
| assert.throws(() => zlib.zstdCompressSync(input, { pledgedSrcSize }), expected); | ||
|
robobun marked this conversation as resolved.
Resolved
|
||
| }); | ||
| } | ||
| }); | ||
|
|
||
| // Only in Bun: when Node.js runs this file it must not spawn itself again. | ||
| if (typeof Bun !== "undefined") { | ||
| const { bunEnv, nodeExe } = await import("harness"); | ||
| const node = nodeExe(); | ||
|
|
||
| describe("Node.js compatibility", () => { | ||
| (node ? test : test.skip)("all tests pass in Node.js", async () => { | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| // A direct run, not `node --test`: the runner mode forks a second node | ||
| // process per file. node:test still exits non-zero on any failure. | ||
| await using proc = Bun.spawn({ | ||
| cmd: [node!, "--v8-pool-size=1", fileURLToPath(import.meta.url)], | ||
|
robobun marked this conversation as resolved.
Resolved
|
||
| env: { ...bunEnv, UV_THREADPOOL_SIZE: "2" }, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| assert.deepStrictEqual({ exitCode, output: exitCode === 0 ? "" : stdout + stderr }, { exitCode: 0, output: "" }); | ||
| }); | ||
| }); | ||
| } | ||
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.