From 4d62e95bfa8e1cd02bcae4c9264e9ad03e1752b0 Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Wed, 4 Mar 2026 23:37:06 +0100 Subject: [PATCH] stream: fix brotli error handling in web compression streams MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Convert brotli decompression errors to TypeError to match the Compression Streams spec, by extending handleKnownInternalErrors() in the adapters layer to recognize brotli error codes. This replaces the manual error event handler on DecompressionStream which was redundant with the adapter's built-in error propagation. PR-URL: https://github.com/nodejs/node/pull/62107 Reviewed-By: Yagiz Nizipli Reviewed-By: Matteo Collina Reviewed-By: Mattias Buelens Reviewed-By: René --- lib/internal/webstreams/adapters.js | 9 ++++++++- .../test-webstreams-compression-bad-chunks.js | 18 ++++++++++++++++++ test/wpt/status/compression.json | 8 -------- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/lib/internal/webstreams/adapters.js b/lib/internal/webstreams/adapters.js index e4695a83dc4..1ffb19b4f59 100644 --- a/lib/internal/webstreams/adapters.js +++ b/lib/internal/webstreams/adapters.js @@ -120,7 +120,14 @@ function handleKnownInternalErrors(cause) { case cause?.code === 'ERR_STREAM_PREMATURE_CLOSE': { return new AbortError(undefined, { cause }); } - case ZLIB_FAILURES.has(cause?.code): { + case ZLIB_FAILURES.has(cause?.code): + // Brotli decoder error codes are formatted as 'ERR_' + + // BrotliDecoderErrorString(), where the latter returns strings like + // '_ERROR_FORMAT_...', '_ERROR_ALLOC_...', '_ERROR_UNREACHABLE', etc. + // The resulting JS error codes all start with 'ERR__ERROR_'. + // Falls through + case cause?.code != null && + StringPrototypeStartsWith(cause.code, 'ERR__ERROR_'): { // eslint-disable-next-line no-restricted-syntax const error = new TypeError(undefined, { cause }); error.code = cause.code; diff --git a/test/parallel/test-webstreams-compression-bad-chunks.js b/test/parallel/test-webstreams-compression-bad-chunks.js index 442eb8b2ac1..4a8ca3cff8a 100644 --- a/test/parallel/test-webstreams-compression-bad-chunks.js +++ b/test/parallel/test-webstreams-compression-bad-chunks.js @@ -55,3 +55,21 @@ for (const format of ['deflate', 'deflate-raw', 'gzip', 'brotli']) { }); } } + +// Verify that decompression errors (e.g. corrupt data) are surfaced as +// TypeError, not plain Error, per the Compression Streams spec. +for (const format of ['deflate', 'deflate-raw', 'gzip', 'brotli']) { + test(`DecompressionStream surfaces corrupt data as TypeError for ${format}`, async () => { + const ds = new DecompressionStream(format); + const writer = ds.writable.getWriter(); + const reader = ds.readable.getReader(); + + const corruptData = new Uint8Array([0, 1, 2, 3, 4, 5]); + + writer.write(corruptData).catch(() => {}); + reader.read().catch(() => {}); + + await assert.rejects(writer.close(), { name: 'TypeError' }); + await assert.rejects(reader.closed, { name: 'TypeError' }); + }); +} diff --git a/test/wpt/status/compression.json b/test/wpt/status/compression.json index b5c5877db48..5c73f02a587 100644 --- a/test/wpt/status/compression.json +++ b/test/wpt/status/compression.json @@ -1,12 +1,4 @@ { - "decompression-bad-chunks.any.js": { - "fail": { - "expected": [ - "chunk of type invalid deflate bytes should error the stream for brotli", - "chunk of type invalid gzip bytes should error the stream for brotli" - ] - } - }, "compression-with-detach.window.js": { "requires": ["crypto"] },