Skip to content

Add Node's error codes to the one-shot node:zlib APIs - #8610

Merged
littledivy merged 1 commit into
mainfrom
littledivy/2026-11-18-node-zlib-error-codes
Nov 18, 2026
Merged

littledivy merged 1 commit into
mainfrom
littledivy/2026-11-18-node-zlib-error-codes

Conversation

@littledivy

Copy link
Copy Markdown
Member

The one-shot node:zlib functions (deflateSync, gunzip(cb), brotliCompressSync, zstdDecompress(cb) and the rest) go straight into C++. Their errors have the same types as Node's, but they don't carry Node's code. A non-buffer input threw a JSG TypeError, a corrupt stream threw a plain Error without code or errno, and an out-of-range option or an oversized output threw a bare RangeError. The streaming classes already set these, so the same failure looked different depending on which API you used.

In this patch:

  • the one-shot functions check their input in TypeScript and throw ERR_INVALID_ARG_TYPE with Node's message. Strings, ArrayBufferViews and ArrayBuffers are accepted as before.
  • a zlib, brotli or zstd error from a one-shot call carries the engine's code (e.g. Z_DATA_ERROR) and errno, as the stream path does.
  • the chunkSize, maxOutputLength, flush and finishFlush range checks throw with code: 'ERR_OUT_OF_RANGE', and the zlib finishFlush message now reports the finishFlush value instead of flush.
  • exceeding maxOutputLength sets code: 'ERR_BUFFER_TOO_LARGE'. The message stays "Memory limit exceeded".

No thrown type changes, and apart from the finishFlush value no message changes. Errors only gain properties, so this ships without a compatibility flag. The TODOs in zlib-nodejs-test now assert the codes, and a new oneShotErrorCodes case covers the accepted inputs and the range checks.

The one-shot node:zlib functions (deflateSync, gunzip(cb), brotliCompressSync, zstdDecompress(cb) and the rest) go straight into C++. Their errors have the same types as Node's, but they don't carry Node's `code`. A non-buffer input threw a JSG TypeError, a corrupt stream threw a plain Error without `code` or `errno`, and an out-of-range option or an oversized output threw a bare RangeError. The streaming classes already set these, so the same failure looked different depending on which API you used.

In this patch:

- the one-shot functions check their input in TypeScript and throw `ERR_INVALID_ARG_TYPE` with Node's message. Strings, ArrayBufferViews and ArrayBuffers are accepted as before.
- a zlib, brotli or zstd error from a one-shot call carries the engine's `code` (e.g. `Z_DATA_ERROR`) and `errno`, as the stream path does.
- the `chunkSize`, `maxOutputLength`, `flush` and `finishFlush` range checks throw with `code: 'ERR_OUT_OF_RANGE'`, and the zlib `finishFlush` message now reports the `finishFlush` value instead of `flush`.
- exceeding `maxOutputLength` sets `code: 'ERR_BUFFER_TOO_LARGE'`. The message stays "Memory limit exceeded".

No thrown type changes, and apart from the `finishFlush` value no message changes. Errors only gain properties, so this ships without a compatibility flag. The TODOs in zlib-nodejs-test now assert the codes, and a new `oneShotErrorCodes` case covers the accepted inputs and the range checks.
@littledivy
littledivy requested review from a team as code owners November 18, 2026 13:36

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved — No problems found. The error codes, errno and the finishFlush message fix are applied the same way across the zlib, brotli and zstd one-shot paths, and the tests cover them.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security concerns.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md concerns.


📋 Code Quality: ✅ LGTM

The changes are applied the same way across all one-shot entry points. Errors raised inside the callback variants reach the callback with code and errno set. The finishFlush message now reports the right value.


⚡ Performance: ✅ LGTM

The new code runs only on error paths, plus one constant-time type check per call. Buffer handling is unchanged.


📝 Documentation: ✅ LGTM

No documentation concerns.


Reviewed commit: 40c7777a

@littledivy
littledivy merged commit 26c6357 into main Nov 18, 2026
35 of 37 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-18-node-zlib-error-codes branch November 18, 2026 15:24
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