Skip to content

Commit 3725bd2

Browse files
panvanodejs-github-bot
authored andcommitted
stream: fix brotli error handling in web compression streams
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: #62107 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Mattias Buelens <mattias@buelens.com> Reviewed-By: René <contact.9a5d6388@renegade334.me.uk>
1 parent 7991cd2 commit 3725bd2

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

lib/internal/webstreams/adapters.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,14 @@ function handleKnownInternalErrors(cause) {
120120
case cause?.code === 'ERR_STREAM_PREMATURE_CLOSE': {
121121
return new AbortError(undefined, { cause });
122122
}
123-
case ZLIB_FAILURES.has(cause?.code): {
123+
case ZLIB_FAILURES.has(cause?.code):
124+
// Brotli decoder error codes are formatted as 'ERR_' +
125+
// BrotliDecoderErrorString(), where the latter returns strings like
126+
// '_ERROR_FORMAT_...', '_ERROR_ALLOC_...', '_ERROR_UNREACHABLE', etc.
127+
// The resulting JS error codes all start with 'ERR__ERROR_'.
128+
// Falls through
129+
case cause?.code != null &&
130+
StringPrototypeStartsWith(cause.code, 'ERR__ERROR_'): {
124131
// eslint-disable-next-line no-restricted-syntax
125132
const error = new TypeError(undefined, { cause });
126133
error.code = cause.code;

test/parallel/test-webstreams-compression-bad-chunks.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,21 @@ for (const format of ['deflate', 'deflate-raw', 'gzip', 'brotli']) {
5555
});
5656
}
5757
}
58+
59+
// Verify that decompression errors (e.g. corrupt data) are surfaced as
60+
// TypeError, not plain Error, per the Compression Streams spec.
61+
for (const format of ['deflate', 'deflate-raw', 'gzip', 'brotli']) {
62+
test(`DecompressionStream surfaces corrupt data as TypeError for ${format}`, async () => {
63+
const ds = new DecompressionStream(format);
64+
const writer = ds.writable.getWriter();
65+
const reader = ds.readable.getReader();
66+
67+
const corruptData = new Uint8Array([0, 1, 2, 3, 4, 5]);
68+
69+
writer.write(corruptData).catch(() => {});
70+
reader.read().catch(() => {});
71+
72+
await assert.rejects(writer.close(), { name: 'TypeError' });
73+
await assert.rejects(reader.closed, { name: 'TypeError' });
74+
});
75+
}

test/wpt/status/compression.json

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
11
{
2-
"decompression-bad-chunks.any.js": {
3-
"fail": {
4-
"expected": [
5-
"chunk of type invalid deflate bytes should error the stream for brotli",
6-
"chunk of type invalid gzip bytes should error the stream for brotli"
7-
]
8-
}
9-
},
102
"compression-with-detach.window.js": {
113
"requires": ["crypto"]
124
},

0 commit comments

Comments
 (0)