2019-10-27 01:09:03 +02:00
|
|
|
'use strict';
|
|
|
|
|
2019-11-30 16:55:29 +01:00
|
|
|
const {
|
|
|
|
Symbol,
|
2019-12-08 22:35:19 +01:00
|
|
|
SymbolIterator
|
2019-11-30 16:55:29 +01:00
|
|
|
} = primordials;
|
|
|
|
|
2019-10-27 01:09:03 +02:00
|
|
|
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]();
|
2019-12-08 22:35:19 +01:00
|
|
|
else if (iterable && iterable[SymbolIterator])
|
|
|
|
iterator = iterable[SymbolIterator]();
|
2019-10-27 01:09:03 +02:00
|
|
|
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;
|