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"] },