Skip to content

Commit 2b55f7c

Browse files
committed
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.
1 parent e7298b0 commit 2b55f7c

3 files changed

Lines changed: 26 additions & 9 deletions

File tree

lib/internal/webstreams/adapters.js

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