| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent d05f67c commit 87f14e1
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -47,6 +47,7 @@ const { | |||
| 47 | 47 | // Lazy loaded to improve the startup performance. | |
| 48 | 48 | let StringDecoder; | |
| 49 | 49 | let createReadableStreamAsyncIterator; | |
| 50 | + let from; | ||
| 50 | 51 | ||
| 51 | 52 | Object.setPrototypeOf(Readable.prototype, Stream.prototype); | |
| 52 | 53 | Object.setPrototypeOf(Readable, Stream); | |
@@ -1209,40 +1210,8 @@ function endReadableNT(state, stream) { | |||
| 1209 | 1210 | } | |
| 1210 | 1211 | ||
| 1211 | 1212 | Readable.from = function(iterable, opts) { | |
| 1212 | - let iterator; | ||
| 1213 | - if (iterable && iterable[Symbol.asyncIterator]) | ||
| 1214 | - iterator = iterable[Symbol.asyncIterator](); | ||
| 1215 | - else if (iterable && iterable[Symbol.iterator]) | ||
| 1216 | - iterator = iterable[Symbol.iterator](); | ||
| 1217 | - else | ||
| 1218 | - throw new ERR_INVALID_ARG_TYPE('iterable', ['Iterable'], iterable); | ||
| 1219 | - | ||
| 1220 | - const readable = new Readable({ | ||
| 1221 | - objectMode: true, | ||
| 1222 | - ...opts | ||
| 1223 | - }); | ||
| 1224 | - // Reading boolean to protect against _read | ||
| 1225 | - // being called before last iteration completion. | ||
| 1226 | - let reading = false; | ||
| 1227 | - readable._read = function() { | ||
| 1228 | - if (!reading) { | ||
| 1229 | - reading = true; | ||
| 1230 | - next(); | ||
| 1231 | - } | ||
| 1232 | - }; | ||
| 1233 | - async function next() { | ||
| 1234 | - try { | ||
| 1235 | - const { value, done } = await iterator.next(); | ||
| 1236 | - if (done) { | ||
| 1237 | - readable.push(null); | ||
| 1238 | - } else if (readable.push(await value)) { | ||
| 1239 | - next(); | ||
| 1240 | - } else { | ||
| 1241 | - reading = false; | ||
| 1242 | - } | ||
| 1243 | - } catch (err) { | ||
| 1244 | - readable.destroy(err); | ||
| 1245 | - } | ||
| 1213 | + if (from === undefined) { | ||
| 1214 | + from = require('internal/streams/from'); | ||
| 1246 | 1215 | } | |
| 1247 | - return readable; | ||
| 1216 | + return from(Readable, iterable, opts); | ||
| 1248 | 1217 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,46 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const { | ||
| 4 | + ERR_INVALID_ARG_TYPE | ||
| 5 | + } = require('internal/errors').codes; | ||
| 6 | + | ||
| 7 | + function from(Readable, iterable, opts) { | ||
| 8 | + let iterator; | ||
| 9 | + if (iterable && iterable[Symbol.asyncIterator]) | ||
| 10 | + iterator = iterable[Symbol.asyncIterator](); | ||
| 11 | + else if (iterable && iterable[Symbol.iterator]) | ||
| 12 | + iterator = iterable[Symbol.iterator](); | ||
| 13 | + else | ||
| 14 | + throw new ERR_INVALID_ARG_TYPE('iterable', ['Iterable'], iterable); | ||
| 15 | + | ||
| 16 | + const readable = new Readable({ | ||
| 17 | + objectMode: true, | ||
| 18 | + ...opts | ||
| 19 | + }); | ||
| 20 | + // Reading boolean to protect against _read | ||
| 21 | + // being called before last iteration completion. | ||
| 22 | + let reading = false; | ||
| 23 | + readable._read = function() { | ||
| 24 | + if (!reading) { | ||
| 25 | + reading = true; | ||
| 26 | + next(); | ||
| 27 | + } | ||
| 28 | + }; | ||
| 29 | + async function next() { | ||
| 30 | + try { | ||
| 31 | + const { value, done } = await iterator.next(); | ||
| 32 | + if (done) { | ||
| 33 | + readable.push(null); | ||
| 34 | + } else if (readable.push(await value)) { | ||
| 35 | + next(); | ||
| 36 | + } else { | ||
| 37 | + reading = false; | ||
| 38 | + } | ||
| 39 | + } catch (err) { | ||
| 40 | + readable.destroy(err); | ||
| 41 | + } | ||
| 42 | + } | ||
| 43 | + return readable; | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + module.exports = from; | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -205,6 +205,7 @@ | |||
| 205 | 205 | 'lib/internal/streams/async_iterator.js', | |
| 206 | 206 | 'lib/internal/streams/buffer_list.js', | |
| 207 | 207 | 'lib/internal/streams/duplexpair.js', | |
| 208 | + 'lib/internal/streams/from.js', | ||
| 208 | 209 | 'lib/internal/streams/legacy.js', | |
| 209 | 210 | 'lib/internal/streams/destroy.js', | |
| 210 | 211 | 'lib/internal/streams/state.js', | |
| Back | FazBrowse Home | New Git URL |
0 commit comments