Skip to content

Commit 11e2a51

Browse files
committed
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 <[email protected]>
1 parent 1f9636a commit 11e2a51

1 file changed

Lines changed: 22 additions & 27 deletions

File tree

lib/internal/webstreams/adapters.js

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
const {
44
ArrayPrototypeFilter,
5-
ArrayPrototypeMap,
6-
Boolean,
7-
ObjectEntries,
5+
ObjectKeys,
86
PromisePrototypeThen,
97
PromiseResolve,
108
PromiseWithResolvers,
11-
SafePromiseAll,
9+
SafePromiseAllReturnVoid,
1210
SafePromisePrototypeFinally,
1311
SafeSet,
1412
StringPrototypeStartsWith,
@@ -74,6 +72,7 @@ const {
7472
getDeprecationWarningEmitter,
7573
kEmptyObject,
7674
normalizeEncoding,
75+
setOwnProperty,
7776
} = require('internal/util');
7877

7978
const {
@@ -93,6 +92,7 @@ const {
9392

9493
const { eos } = require('internal/streams/end-of-stream');
9594

95+
const { zlib } = internalBinding('constants');
9696
const { UV_EOF } = internalBinding('uv');
9797

9898
const encoder = new TextEncoder();
@@ -101,37 +101,34 @@ const kValidateChunk = Symbol('kValidateChunk');
101101
const kDestroyOnSyncError = Symbol('kDestroyOnSyncError');
102102

103103
// Collect all negative (error) ZLIB codes and Z_NEED_DICT
104-
const ZLIB_FAILURES = new SafeSet([
105-
...ArrayPrototypeFilter(
106-
ArrayPrototypeMap(
107-
ObjectEntries(internalBinding('constants').zlib),
108-
({ 0: code, 1: value }) => (value < 0 ? code : null),
109-
),
110-
Boolean,
104+
const ZLIB_FAILURES = new SafeSet(
105+
ArrayPrototypeFilter(
106+
ObjectKeys(zlib),
107+
(code) => code === 'Z_NEED_DICT' || zlib[code] < 0,
111108
),
112-
'Z_NEED_DICT',
113-
]);
109+
);
114110

115111
/**
116112
* @param {Error|null} cause
117113
* @returns {Error|null}
118114
*/
119115
function handleKnownInternalErrors(cause) {
116+
const causeCode = cause?.code;
120117
switch (true) {
121-
case cause?.code === 'ERR_STREAM_PREMATURE_CLOSE': {
118+
case causeCode === 'ERR_STREAM_PREMATURE_CLOSE': {
122119
return new AbortError(undefined, { cause });
123120
}
124-
case ZLIB_FAILURES.has(cause?.code):
121+
case ZLIB_FAILURES.has(causeCode):
125122
// Brotli decoder error codes are formatted as 'ERR_' +
126123
// BrotliDecoderErrorString(), where the latter returns strings like
127124
// '_ERROR_FORMAT_...', '_ERROR_ALLOC_...', '_ERROR_UNREACHABLE', etc.
128125
// The resulting JS error codes all start with 'ERR__ERROR_'.
129126
// Falls through
130-
case cause?.code != null &&
131-
StringPrototypeStartsWith(cause.code, 'ERR__ERROR_'): {
127+
case causeCode != null &&
128+
StringPrototypeStartsWith(causeCode, 'ERR__ERROR_'): {
132129
// eslint-disable-next-line no-restricted-syntax
133130
const error = new TypeError(undefined, { cause });
134-
error.code = cause.code;
131+
setOwnProperty(error, 'code', causeCode);
135132
return error;
136133
}
137134
default:
@@ -190,8 +187,7 @@ function newWritableStreamFromStreamWritable(streamWritable, options = kEmptyObj
190187
let closed;
191188

192189
function onDrain() {
193-
if (backpressurePromise !== undefined)
194-
backpressurePromise.resolve();
190+
backpressurePromise?.resolve();
195191
}
196192

197193
const cleanup = eos(streamWritable, (error) => {
@@ -202,8 +198,7 @@ function newWritableStreamFromStreamWritable(streamWritable, options = kEmptyObj
202198
// that happen to emit an error event again after finished is called.
203199
streamWritable.on('error', () => {});
204200
if (error != null) {
205-
if (backpressurePromise !== undefined)
206-
backpressurePromise.reject(error);
201+
backpressurePromise?.reject(error);
207202
// If closed is not undefined, the error is happening
208203
// after the WritableStream close has already started.
209204
// We need to reject it here.
@@ -330,10 +325,10 @@ function newStreamWritableFromWritableStream(writableStream, options = kEmptyObj
330325
writer.ready,
331326
() => {
332327
return PromisePrototypeThen(
333-
SafePromiseAll(
328+
SafePromiseAllReturnVoid(
334329
chunks,
335330
(data) => writer.write(data.chunk)),
336-
() => done(),
331+
done,
337332
done);
338333
},
339334
done);
@@ -802,10 +797,10 @@ function newStreamDuplexFromReadableWritablePair(pair = kEmptyObject, options =
802797
writer.ready,
803798
() => {
804799
return PromisePrototypeThen(
805-
SafePromiseAll(
800+
SafePromiseAllReturnVoid(
806801
chunks,
807802
(data) => writer.write(data.chunk)),
808-
() => done(),
803+
done,
809804
done);
810805
},
811806
done);
@@ -907,7 +902,7 @@ function newStreamDuplexFromReadableWritablePair(pair = kEmptyObject, options =
907902

908903
if (!writableClosed || !readableClosed) {
909904
PromisePrototypeThen(
910-
SafePromiseAll([
905+
SafePromiseAllReturnVoid([
911906
closeWriter(),
912907
closeReader(),
913908
]),

0 commit comments

Comments
 (0)