Skip to content

Commit

Permalink
stream: extract Readable.from in its own file
Browse files Browse the repository at this point in the history
See: nodejs/readable-stream#420

PR-URL: #30140
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: Gus Caplan <[email protected]>
Reviewed-By: Beth Griggs <[email protected]>
  • Loading branch information
mcollina authored and MylesBorins committed Dec 17, 2019
1 parent 20f64a9 commit 894aaa2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 35 deletions.
39 changes: 4 additions & 35 deletions lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const {
// Lazy loaded to improve the startup performance.
let StringDecoder;
let createReadableStreamAsyncIterator;
let from;

util.inherits(Readable, Stream);

Expand Down Expand Up @@ -1154,40 +1155,8 @@ function endReadableNT(state, stream) {
}

Readable.from = function(iterable, opts) {
let iterator;
if (iterable && iterable[Symbol.asyncIterator])
iterator = iterable[Symbol.asyncIterator]();
else if (iterable && iterable[Symbol.iterator])
iterator = iterable[Symbol.iterator]();
else
throw new ERR_INVALID_ARG_TYPE('iterable', ['Iterable'], iterable);

const readable = new Readable({
objectMode: true,
...opts
});
// Reading boolean to protect against _read
// being called before last iteration completion.
let reading = false;
readable._read = function() {
if (!reading) {
reading = true;
next();
}
};
async function next() {
try {
const { value, done } = await iterator.next();
if (done) {
readable.push(null);
} else if (readable.push(await value)) {
next();
} else {
reading = false;
}
} catch (err) {
readable.destroy(err);
}
if (from === undefined) {
from = require('internal/streams/from');
}
return readable;
return from(Readable, iterable, opts);
};
46 changes: 46 additions & 0 deletions lib/internal/streams/from.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

const {
ERR_INVALID_ARG_TYPE
} = require('internal/errors').codes;

function from(Readable, iterable, opts) {
let iterator;
if (iterable && iterable[Symbol.asyncIterator])
iterator = iterable[Symbol.asyncIterator]();
else if (iterable && iterable[Symbol.iterator])
iterator = iterable[Symbol.iterator]();
else
throw new ERR_INVALID_ARG_TYPE('iterable', ['Iterable'], iterable);

const readable = new Readable({
objectMode: true,
...opts
});
// Reading boolean to protect against _read
// being called before last iteration completion.
let reading = false;
readable._read = function() {
if (!reading) {
reading = true;
next();
}
};
async function next() {
try {
const { value, done } = await iterator.next();
if (done) {
readable.push(null);
} else if (readable.push(await value)) {
next();
} else {
reading = false;
}
} catch (err) {
readable.destroy(err);
}
}
return readable;
}

module.exports = from;
1 change: 1 addition & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@
'lib/internal/streams/async_iterator.js',
'lib/internal/streams/buffer_list.js',
'lib/internal/streams/duplexpair.js',
'lib/internal/streams/from.js',
'lib/internal/streams/legacy.js',
'lib/internal/streams/destroy.js',
'lib/internal/streams/state.js',
Expand Down

0 comments on commit 894aaa2

Please sign in to comment.