Skip to content

Commit 8876ff6

Browse files
committed
stream: move duplicated code to an internal module
Create a utils module for isIterable(), isReadable(), and isStream().
1 parent 5968c54 commit 8876ff6

File tree

4 files changed

+44
-43
lines changed

4 files changed

+44
-43
lines changed

lib/internal/streams/pipeline.js

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const {
1111
FunctionPrototypeCall,
1212
ReflectApply,
1313
SymbolAsyncIterator,
14-
SymbolIterator,
1514
} = primordials;
1615

1716
let eos;
@@ -27,6 +26,12 @@ const {
2726

2827
const { validateCallback } = require('internal/validators');
2928

29+
const {
30+
isIterable,
31+
isReadable,
32+
isStream,
33+
} = require('internal/streams/utils');
34+
3035
let EE;
3136
let PassThrough;
3237
let Readable;
@@ -82,26 +87,6 @@ function popCallback(streams) {
8287
return ArrayPrototypePop(streams);
8388
}
8489

85-
function isReadable(obj) {
86-
return !!(obj && typeof obj.pipe === 'function');
87-
}
88-
89-
function isWritable(obj) {
90-
return !!(obj && typeof obj.write === 'function');
91-
}
92-
93-
function isStream(obj) {
94-
return isReadable(obj) || isWritable(obj);
95-
}
96-
97-
function isIterable(obj, isAsync) {
98-
if (!obj) return false;
99-
if (isAsync === true) return typeof obj[SymbolAsyncIterator] === 'function';
100-
if (isAsync === false) return typeof obj[SymbolIterator] === 'function';
101-
return typeof obj[SymbolAsyncIterator] === 'function' ||
102-
typeof obj[SymbolIterator] === 'function';
103-
}
104-
10590
function makeAsyncIterable(val) {
10691
if (isIterable(val)) {
10792
return val;

lib/internal/streams/utils.js

Lines changed: 32 additions & 0 deletions
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

Lines changed: 5 additions & 22 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@
263263
'lib/internal/streams/state.js',
264264
'lib/internal/streams/pipeline.js',
265265
'lib/internal/streams/end-of-stream.js',
266+
'lib/internal/streams/utils.js',
266267
'deps/v8/tools/splaytree.js',
267268
'deps/v8/tools/codemap.js',
268269
'deps/v8/tools/consarray.js',

0 commit comments

Comments
 (0)