|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 | 3 | const {
|
| 4 | + Boolean, |
| 5 | + ObjectEntries, |
4 | 6 | PromisePrototypeThen,
|
5 | 7 | PromiseResolve,
|
6 | 8 | SafePromiseAll,
|
7 | 9 | SafePromisePrototypeFinally,
|
| 10 | + SafeSet, |
8 | 11 | TypedArrayPrototypeGetBuffer,
|
9 |
| - TypedArrayPrototypeGetByteOffset, |
10 | 12 | TypedArrayPrototypeGetByteLength,
|
| 13 | + TypedArrayPrototypeGetByteOffset, |
| 14 | + TypeError, |
11 | 15 | Uint8Array,
|
12 | 16 | } = primordials;
|
13 | 17 |
|
@@ -82,6 +86,28 @@ const { UV_EOF } = internalBinding('uv');
|
82 | 86 |
|
83 | 87 | const encoder = new TextEncoder();
|
84 | 88 |
|
| 89 | +const ZLIB_FAILURES = new SafeSet([ |
| 90 | + ...ObjectEntries(internalBinding('constants').zlib) |
| 91 | + .map(({ 0: code, 1: value }) => (value < 0 ? code : null)).filter(Boolean), |
| 92 | + 'Z_NEED_DICT', |
| 93 | +]); |
| 94 | + |
| 95 | +function handleKnownInternalErrors(cause) { |
| 96 | + switch (true) { |
| 97 | + case cause?.code === 'ERR_STREAM_PREMATURE_CLOSE': { |
| 98 | + return new AbortError(undefined, { cause }); |
| 99 | + } |
| 100 | + case ZLIB_FAILURES.has(cause?.code): { |
| 101 | + // eslint-disable-next-line no-restricted-syntax |
| 102 | + const error = new TypeError(undefined, { cause }); |
| 103 | + error.code = cause.code; |
| 104 | + return error; |
| 105 | + } |
| 106 | + default: |
| 107 | + return cause; |
| 108 | + } |
| 109 | +} |
| 110 | + |
85 | 111 | /**
|
86 | 112 | * @typedef {import('../../stream').Writable} Writable
|
87 | 113 | * @typedef {import('../../stream').Readable} Readable
|
@@ -137,10 +163,7 @@ function newWritableStreamFromStreamWritable(streamWritable) {
|
137 | 163 | }
|
138 | 164 |
|
139 | 165 | const cleanup = finished(streamWritable, (error) => {
|
140 |
| - if (error?.code === 'ERR_STREAM_PREMATURE_CLOSE') { |
141 |
| - const err = new AbortError(undefined, { cause: error }); |
142 |
| - error = err; |
143 |
| - } |
| 166 | + error = handleKnownInternalErrors(error); |
144 | 167 |
|
145 | 168 | cleanup();
|
146 | 169 | // This is a protection against non-standard, legacy streams
|
@@ -440,10 +463,7 @@ function newReadableStreamFromStreamReadable(streamReadable, options = kEmptyObj
|
440 | 463 | streamReadable.pause();
|
441 | 464 |
|
442 | 465 | const cleanup = finished(streamReadable, (error) => {
|
443 |
| - if (error?.code === 'ERR_STREAM_PREMATURE_CLOSE') { |
444 |
| - const err = new AbortError(undefined, { cause: error }); |
445 |
| - error = err; |
446 |
| - } |
| 466 | + error = handleKnownInternalErrors(error); |
447 | 467 |
|
448 | 468 | cleanup();
|
449 | 469 | // This is a protection against non-standard, legacy streams
|
|
0 commit comments