mirror of
https://github.com/nodejs/node.git
synced 2026-11-25 23:01:06 -05:00
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: https://github.com/nodejs/node/pull/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>
This commit is contained in:
1 parent
4bdcaf2865
commit
4d62e95bfa
3 files changed
+26
-9
No files matched your search
@@ -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;
|
||||
|
||||
@@ -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' });
|
||||
});
|
||||
}
|
||||
@@ -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"]
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user