Skip to content

Commit 4cc7645

Browse files
Trotttargos
authored andcommitted
stream: move duplicated code to an internal module
Create a utils module for isIterable(), isReadable(), and isStream(). PR-URL: #37508 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 46af567 commit 4cc7645

File tree

5 files changed

+45
-43
lines changed

5 files changed

+45
-43
lines changed

lib/internal/streams/pipeline.js

+6-21
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const {
77
ArrayIsArray,
88
ReflectApply,
99
SymbolAsyncIterator,
10-
SymbolIterator,
1110
} = primordials;
1211

1312
let eos;
@@ -23,6 +22,12 @@ const {
2322

2423
const { validateCallback } = require('internal/validators');
2524

25+
const {
26+
isIterable,
27+
isReadable,
28+
isStream,
29+
} = require('internal/streams/utils');
30+
2631
let EE;
2732
let PassThrough;
2833
let Readable;
@@ -78,26 +83,6 @@ function popCallback(streams) {
7883
return streams.pop();
7984
}
8085

81-
function isReadable(obj) {
82-
return !!(obj && typeof obj.pipe === 'function');
83-
}
84-
85-
function isWritable(obj) {
86-
return !!(obj && typeof obj.write === 'function');
87-
}
88-
89-
function isStream(obj) {
90-
return isReadable(obj) || isWritable(obj);
91-
}
92-
93-
function isIterable(obj, isAsync) {
94-
if (!obj) return false;
95-
if (isAsync === true) return typeof obj[SymbolAsyncIterator] === 'function';
96-
if (isAsync === false) return typeof obj[SymbolIterator] === 'function';
97-
return typeof obj[SymbolAsyncIterator] === 'function' ||
98-
typeof obj[SymbolIterator] === 'function';
99-
}
100-
10186
function makeAsyncIterable(val) {
10287
if (isIterable(val)) {
10388
return val;

lib/internal/streams/utils.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
const {
4+
SymbolAsyncIterator,
5+
SymbolIterator,
6+
} = primordials;
7+
8+
function isReadable(obj) {
9+
return !!(obj && typeof obj.pipe === 'function');
10+
}
11+
12+
function isWritable(obj) {
13+
return !!(obj && typeof obj.write === 'function');
14+
}
15+
16+
function isStream(obj) {
17+
return isReadable(obj) || isWritable(obj);
18+
}
19+
20+
function isIterable(obj, isAsync) {
21+
if (!obj) return false;
22+
if (isAsync === true) return typeof obj[SymbolAsyncIterator] === 'function';
23+
if (isAsync === false) return typeof obj[SymbolIterator] === 'function';
24+
return typeof obj[SymbolAsyncIterator] === 'function' ||
25+
typeof obj[SymbolIterator] === 'function';
26+
}
27+
28+
module.exports = {
29+
isIterable,
30+
isReadable,
31+
isStream,
32+
};

lib/stream/promises.js

+5-22
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
const {
44
ArrayPrototypePop,
55
Promise,
6-
SymbolAsyncIterator,
7-
SymbolIterator,
86
} = primordials;
97

108
const {
@@ -15,29 +13,14 @@ const {
1513
validateAbortSignal,
1614
} = require('internal/validators');
1715

16+
const {
17+
isIterable,
18+
isStream,
19+
} = require('internal/streams/utils');
20+
1821
let pl;
1922
let eos;
2023

21-
function isReadable(obj) {
22-
return !!(obj && typeof obj.pipe === 'function');
23-
}
24-
25-
function isWritable(obj) {
26-
return !!(obj && typeof obj.write === 'function');
27-
}
28-
29-
function isStream(obj) {
30-
return isReadable(obj) || isWritable(obj);
31-
}
32-
33-
function isIterable(obj, isAsync) {
34-
if (!obj) return false;
35-
if (isAsync === true) return typeof obj[SymbolAsyncIterator] === 'function';
36-
if (isAsync === false) return typeof obj[SymbolIterator] === 'function';
37-
return typeof obj[SymbolAsyncIterator] === 'function' ||
38-
typeof obj[SymbolIterator] === 'function';
39-
}
40-
4124
function pipeline(...streams) {
4225
if (!pl) pl = require('internal/streams/pipeline');
4326
return new Promise((resolve, reject) => {

node.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@
256256
'lib/internal/streams/state.js',
257257
'lib/internal/streams/pipeline.js',
258258
'lib/internal/streams/end-of-stream.js',
259+
'lib/internal/streams/utils.js',
259260
'deps/v8/tools/splaytree.js',
260261
'deps/v8/tools/codemap.js',
261262
'deps/v8/tools/consarray.js',

test/parallel/test-bootstrap-modules.js

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ const expectedModules = new Set([
9898
'NativeModule internal/streams/readable',
9999
'NativeModule internal/streams/state',
100100
'NativeModule internal/streams/transform',
101+
'NativeModule internal/streams/utils',
101102
'NativeModule internal/streams/writable',
102103
'NativeModule internal/timers',
103104
'NativeModule internal/url',

0 commit comments

Comments
 (0)