stream: copyedit webstreams/adapter.js

- Simplify `ZLIB_FAILURES` creation.
- Cache `cause.code` in `handleKnownInternalErrors` in case of a getter.
- Replace `SafePromiseAll` with `SafePromiseAllReturnVoid` to reduce the
  number of allocated promises.

Signed-off-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/63034
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Antoine du Hamel committed 2026-05-05 16:19:53 +02:00
1 parent 3191d2936a
commit a95830b72a
1 file changed
+22 -27
+22 -27
View File
@@ -2,13 +2,11 @@
const {
ArrayPrototypeFilter,
ArrayPrototypeMap,
Boolean,
ObjectEntries,
ObjectKeys,
PromisePrototypeThen,
PromiseResolve,
PromiseWithResolvers,
SafePromiseAll,
SafePromiseAllReturnVoid,
SafePromisePrototypeFinally,
SafeSet,
StringPrototypeStartsWith,
@@ -74,6 +72,7 @@ const {
getDeprecationWarningEmitter,
kEmptyObject,
normalizeEncoding,
setOwnProperty,
} = require('internal/util');
const {
@@ -93,6 +92,7 @@ const {
const { eos } = require('internal/streams/end-of-stream');
const { zlib } = internalBinding('constants');
const { UV_EOF } = internalBinding('uv');
const encoder = new TextEncoder();
@@ -101,37 +101,34 @@ const kValidateChunk = Symbol('kValidateChunk');
const kDestroyOnSyncError = Symbol('kDestroyOnSyncError');
// Collect all negative (error) ZLIB codes and Z_NEED_DICT
const ZLIB_FAILURES = new SafeSet([
...ArrayPrototypeFilter(
ArrayPrototypeMap(
ObjectEntries(internalBinding('constants').zlib),
({ 0: code, 1: value }) => (value < 0 ? code : null),
),
Boolean,
const ZLIB_FAILURES = new SafeSet(
ArrayPrototypeFilter(
ObjectKeys(zlib),
(code) => code === 'Z_NEED_DICT' || zlib[code] < 0,
),
'Z_NEED_DICT',
]);
);
/**
* @param {Error|null} cause
* @returns {Error|null}
*/
function handleKnownInternalErrors(cause) {
const causeCode = cause?.code;
switch (true) {
case cause?.code === 'ERR_STREAM_PREMATURE_CLOSE': {
case causeCode === 'ERR_STREAM_PREMATURE_CLOSE': {
return new AbortError(undefined, { cause });
}
case ZLIB_FAILURES.has(cause?.code):
case ZLIB_FAILURES.has(causeCode):
// 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_'): {
case causeCode != null &&
StringPrototypeStartsWith(causeCode, 'ERR__ERROR_'): {
// eslint-disable-next-line no-restricted-syntax
const error = new TypeError(undefined, { cause });
error.code = cause.code;
setOwnProperty(error, 'code', causeCode);
return error;
}
default:
@@ -190,8 +187,7 @@ function newWritableStreamFromStreamWritable(streamWritable, options = kEmptyObj
let closed;
function onDrain() {
if (backpressurePromise !== undefined)
backpressurePromise.resolve();
backpressurePromise?.resolve();
}
const cleanup = eos(streamWritable, (error) => {
@@ -202,8 +198,7 @@ function newWritableStreamFromStreamWritable(streamWritable, options = kEmptyObj
// that happen to emit an error event again after finished is called.
streamWritable.on('error', () => {});
if (error != null) {
if (backpressurePromise !== undefined)
backpressurePromise.reject(error);
backpressurePromise?.reject(error);
// If closed is not undefined, the error is happening
// after the WritableStream close has already started.
// We need to reject it here.
@@ -330,10 +325,10 @@ function newStreamWritableFromWritableStream(writableStream, options = kEmptyObj
writer.ready,
() => {
return PromisePrototypeThen(
SafePromiseAll(
SafePromiseAllReturnVoid(
chunks,
(data) => writer.write(data.chunk)),
() => done(),
done,
done);
},
done);
@@ -802,10 +797,10 @@ function newStreamDuplexFromReadableWritablePair(pair = kEmptyObject, options =
writer.ready,
() => {
return PromisePrototypeThen(
SafePromiseAll(
SafePromiseAllReturnVoid(
chunks,
(data) => writer.write(data.chunk)),
() => done(),
done,
done);
},
done);
@@ -907,7 +902,7 @@ function newStreamDuplexFromReadableWritablePair(pair = kEmptyObject, options =
if (!writableClosed || !readableClosed) {
PromisePrototypeThen(
SafePromiseAll([
SafePromiseAllReturnVoid([
closeWriter(),
closeReader(),
]),