Skip to content

Commit 161b9cf

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 d81adf8 commit 161b9cf

File tree

4 files changed

+28
-17
lines changed

4 files changed

+28
-17
lines changed

lib/internal/webstreams/adapters.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const {
1111
SafePromiseAll,
1212
SafePromisePrototypeFinally,
1313
SafeSet,
14+
StringPrototypeStartsWith,
1415
TypeError,
1516
TypedArrayPrototypeGetBuffer,
1617
TypedArrayPrototypeGetByteLength,
@@ -115,7 +116,14 @@ function handleKnownInternalErrors(cause) {
115116
case cause?.code === 'ERR_STREAM_PREMATURE_CLOSE': {
116117
return new AbortError(undefined, { cause });
117118
}
118-
case ZLIB_FAILURES.has(cause?.code): {
119+
case ZLIB_FAILURES.has(cause?.code):
120+
// Brotli decoder error codes are formatted as 'ERR_' +
121+
// BrotliDecoderErrorString(), where the latter returns strings like
122+
// '_ERROR_FORMAT_...', '_ERROR_ALLOC_...', '_ERROR_UNREACHABLE', etc.
123+
// The resulting JS error codes all start with 'ERR__ERROR_'.
124+
// Falls through
125+
case cause?.code != null &&
126+
StringPrototypeStartsWith(cause.code, 'ERR__ERROR_'): {
119127
// eslint-disable-next-line no-restricted-syntax
120128
const error = new TypeError(undefined, { cause });
121129
error.code = cause.code;

lib/internal/webstreams/compression.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,6 @@ class DecompressionStream {
157157
}
158158
addBufferSourceValidation(this.#handle);
159159
this.#transform = newReadableWritablePairFromDuplex(this.#handle);
160-
161-
this.#handle.on('error', (err) => {
162-
if (this.#transform?.writable &&
163-
!this.#transform.writable.locked &&
164-
typeof this.#transform.writable.abort === 'function') {
165-
this.#transform.writable.abort(err);
166-
}
167-
});
168160
}
169161

170162
/**

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,22 @@ 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+
// Write invalid bytes for any format — these are not valid compressed data.
68+
const corruptData = new Uint8Array([0, 1, 2, 3, 4, 5]);
69+
70+
writer.write(corruptData).then(() => {}, () => {});
71+
reader.read().then(() => {}, () => {});
72+
73+
await assert.rejects(writer.close(), { name: 'TypeError' });
74+
await assert.rejects(reader.closed, { name: 'TypeError' });
75+
});
76+
}

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)