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().
  • Loading branch information
Trott committed Feb 27, 2021
1 parent 5968c54 commit e2034b4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 43 deletions.
27 changes: 6 additions & 21 deletions lib/internal/streams/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const {
FunctionPrototypeCall,
ReflectApply,
SymbolAsyncIterator,
SymbolIterator,
} = primordials;

let eos;
Expand All @@ -27,6 +26,12 @@ const {

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

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

let EE;
let PassThrough;
let Readable;
Expand Down Expand Up @@ -82,26 +87,6 @@ function popCallback(streams) {
return ArrayPrototypePop(streams);
}

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,
};
27 changes: 5 additions & 22 deletions lib/stream/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
const {
ArrayPrototypePop,
Promise,
SymbolAsyncIterator,
SymbolIterator,
} = primordials;

const {
Expand All @@ -15,29 +13,14 @@ const {
validateAbortSignal,
} = require('internal/validators');

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

let pl;
let eos;

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 pipeline(...streams) {
if (!pl) pl = require('internal/streams/pipeline');
return new Promise((resolve, reject) => {
Expand Down

0 comments on commit e2034b4

Please sign in to comment.