Skip to content

Commit 7792f1a

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 <duhamelantoine1995@gmail.com> PR-URL: #63034 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 717476a commit 7792f1a

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,
@@ -73,6 +71,7 @@ const {
7371
const {
7472
kEmptyObject,
7573
normalizeEncoding,
74+
setOwnProperty,
7675
} = require('internal/util');
7776

7877
const {
@@ -92,6 +91,7 @@ const {
9291

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

94+
const { zlib } = internalBinding('constants');
9595
const { UV_EOF } = internalBinding('uv');
9696

9797
const encoder = new TextEncoder();
@@ -100,37 +100,34 @@ const kValidateChunk = Symbol('kValidateChunk');
100100
const kDestroyOnSyncError = Symbol('kDestroyOnSyncError');
101101

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

114110
/**
115111
* @param {Error|null} cause
116112
* @returns {Error|null}
117113
*/
118114
function handleKnownInternalErrors(cause) {
115+
const causeCode = cause?.code;
119116
switch (true) {
120-
case cause?.code === 'ERR_STREAM_PREMATURE_CLOSE': {
117+
case causeCode === 'ERR_STREAM_PREMATURE_CLOSE': {
121118
return new AbortError(undefined, { cause });
122119
}
123-
case ZLIB_FAILURES.has(cause?.code):
120+
case ZLIB_FAILURES.has(causeCode):
124121
// Brotli decoder error codes are formatted as 'ERR_' +
125122
// BrotliDecoderErrorString(), where the latter returns strings like
126123
// '_ERROR_FORMAT_...', '_ERROR_ALLOC_...', '_ERROR_UNREACHABLE', etc.
127124
// The resulting JS error codes all start with 'ERR__ERROR_'.
128125
// Falls through
129-
case cause?.code != null &&
130-
StringPrototypeStartsWith(cause.code, 'ERR__ERROR_'): {
126+
case causeCode != null &&
127+
StringPrototypeStartsWith(causeCode, 'ERR__ERROR_'): {
131128
// eslint-disable-next-line no-restricted-syntax
132129
const error = new TypeError(undefined, { cause });
133-
error.code = cause.code;
130+
setOwnProperty(error, 'code', causeCode);
134131
return error;
135132
}
136133
default:
@@ -189,8 +186,7 @@ function newWritableStreamFromStreamWritable(streamWritable, options = kEmptyObj
189186
let closed;
190187

191188
function onDrain() {
192-
if (backpressurePromise !== undefined)
193-
backpressurePromise.resolve();
189+
backpressurePromise?.resolve();
194190
}
195191

196192
const cleanup = eos(streamWritable, (error) => {
@@ -201,8 +197,7 @@ function newWritableStreamFromStreamWritable(streamWritable, options = kEmptyObj
201197
// that happen to emit an error event again after finished is called.
202198
streamWritable.on('error', () => {});
203199
if (error != null) {
204-
if (backpressurePromise !== undefined)
205-
backpressurePromise.reject(error);
200+
backpressurePromise?.reject(error);
206201
// If closed is not undefined, the error is happening
207202
// after the WritableStream close has already started.
208203
// We need to reject it here.
@@ -329,10 +324,10 @@ function newStreamWritableFromWritableStream(writableStream, options = kEmptyObj
329324
writer.ready,
330325
() => {
331326
return PromisePrototypeThen(
332-
SafePromiseAll(
327+
SafePromiseAllReturnVoid(
333328
chunks,
334329
(data) => writer.write(data.chunk)),
335-
() => done(),
330+
done,
336331
done);
337332
},
338333
done);
@@ -790,10 +785,10 @@ function newStreamDuplexFromReadableWritablePair(pair = kEmptyObject, options =
790785
writer.ready,
791786
() => {
792787
return PromisePrototypeThen(
793-
SafePromiseAll(
788+
SafePromiseAllReturnVoid(
794789
chunks,
795790
(data) => writer.write(data.chunk)),
796-
() => done(),
791+
done,
797792
done);
798793
},
799794
done);
@@ -895,7 +890,7 @@ function newStreamDuplexFromReadableWritablePair(pair = kEmptyObject, options =
895890

896891
if (!writableClosed || !readableClosed) {
897892
PromisePrototypeThen(
898-
SafePromiseAll([
893+
SafePromiseAllReturnVoid([
899894
closeWriter(),
900895
closeReader(),
901896
]),

0 commit comments

Comments
 (0)