Skip to content

Commit

Permalink
stream: move duplicated code to an internal module
Browse files Browse the repository at this point in the history
Create a utils module for isIterable(), isReadable(), and isStream().

PR-URL: #37508
Backport-PR-URL: #39973
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>
  • Loading branch information
Trott authored and targos committed Sep 4, 2021
1 parent 2b0e270 commit 32a5b8f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
27 changes: 6 additions & 21 deletions lib/internal/streams/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const {
ArrayIsArray,
ReflectApply,
SymbolAsyncIterator,
SymbolIterator,
} = primordials;

let eos;
Expand All @@ -22,6 +21,12 @@ const {
ERR_STREAM_DESTROYED
} = require('internal/errors').codes;

const {
isIterable,
isReadable,
isStream,
} = require('internal/streams/utils');

let EE;
let PassThrough;
let Readable;
Expand Down Expand Up @@ -78,26 +83,6 @@ function popCallback(streams) {
return streams.pop();
}

function isReadable(obj) {
return !!(obj && typeof obj.pipe === 'function');
}

function isWritable(obj) {
return !!(obj && typeof obj.write === 'function');
}

function isStream(obj) {
return isReadable(obj) || isWritable(obj);
}

function isIterable(obj, isAsync) {
if (!obj) return false;
if (isAsync === true) return typeof obj[SymbolAsyncIterator] === 'function';
if (isAsync === false) return typeof obj[SymbolIterator] === 'function';
return typeof obj[SymbolAsyncIterator] === 'function' ||
typeof obj[SymbolIterator] === 'function';
}

function makeAsyncIterable(val) {
if (isIterable(val)) {
return val;
Expand Down
32 changes: 32 additions & 0 deletions lib/internal/streams/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

const {
SymbolAsyncIterator,
SymbolIterator,
} = primordials;

function isReadable(obj) {
return !!(obj && typeof obj.pipe === 'function');
}

function isWritable(obj) {
return !!(obj && typeof obj.write === 'function');
}

function isStream(obj) {
return isReadable(obj) || isWritable(obj);
}

function isIterable(obj, isAsync) {
if (!obj) return false;
if (isAsync === true) return typeof obj[SymbolAsyncIterator] === 'function';
if (isAsync === false) return typeof obj[SymbolIterator] === 'function';
return typeof obj[SymbolAsyncIterator] === 'function' ||
typeof obj[SymbolIterator] === 'function';
}

module.exports = {
isIterable,
isReadable,
isStream,
};
1 change: 1 addition & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@
'lib/internal/streams/state.js',
'lib/internal/streams/pipeline.js',
'lib/internal/streams/end-of-stream.js',
'lib/internal/streams/utils.js',
'deps/v8/tools/splaytree.js',
'deps/v8/tools/codemap.js',
'deps/v8/tools/consarray.js',
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-bootstrap-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const expectedModules = new Set([
'NativeModule internal/process/warning',
'NativeModule internal/querystring',
'NativeModule internal/source_map/source_map_cache',
'NativeModule internal/streams/utils',
'NativeModule internal/timers',
'NativeModule internal/url',
'NativeModule internal/util',
Expand Down

0 comments on commit 32a5b8f

Please sign in to comment.