Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: move duplicated code to an internal module #37508

Merged
merged 1 commit into from Feb 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 6 additions & 21 deletions lib/internal/streams/pipeline.js
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
@@ -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
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
1 change: 1 addition & 0 deletions node.gyp
Expand Up @@ -263,6 +263,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
Expand Up @@ -98,6 +98,7 @@ const expectedModules = new Set([
'NativeModule internal/streams/readable',
'NativeModule internal/streams/state',
'NativeModule internal/streams/transform',
'NativeModule internal/streams/utils',
'NativeModule internal/streams/writable',
'NativeModule internal/timers',
'NativeModule internal/url',
Expand Down