| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent d19316d commit 322912a
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1641,6 +1641,10 @@ readable.on('data', (chunk) => { | |||
| 1641 | 1641 | }); | |
| 1642 | 1642 | ``` | |
| 1643 | 1643 | ||
| 1644 | + Calling `Readable.from(string)` or `Readable.from(buffer)` will not have | ||
| 1645 | + the strings or buffers be iterated to match the other streams semantics | ||
| 1646 | + for performance reasons. | ||
| 1647 | + | ||
| 1644 | 1648 | ## API for Stream Implementers | |
| 1645 | 1649 | ||
| 1646 | 1650 | <!--type=misc--> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,13 +4,25 @@ const { | |||
| 4 | 4 | SymbolAsyncIterator, | |
| 5 | 5 | SymbolIterator | |
| 6 | 6 | } = primordials; | |
| 7 | + const { Buffer } = require('buffer'); | ||
| 7 | 8 | ||
| 8 | 9 | const { | |
| 9 | 10 | ERR_INVALID_ARG_TYPE | |
| 10 | 11 | } = require('internal/errors').codes; | |
| 11 | 12 | ||
| 12 | 13 | function from(Readable, iterable, opts) { | |
| 13 | 14 | let iterator; | |
| 15 | + if (typeof iterable === 'string' || iterable instanceof Buffer) { | ||
| 16 | + return new Readable({ | ||
| 17 | + objectMode: true, | ||
| 18 | + ...opts, | ||
| 19 | + read() { | ||
| 20 | + this.push(iterable); | ||
| 21 | + this.push(null); | ||
| 22 | + } | ||
| 23 | + }); | ||
| 24 | + } | ||
| 25 | + | ||
| 14 | 26 | if (iterable && iterable[SymbolAsyncIterator]) | |
| 15 | 27 | iterator = iterable[SymbolAsyncIterator](); | |
| 16 | 28 | else if (iterable && iterable[SymbolIterator]) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -56,13 +56,23 @@ async function toReadablePromises() { | |||
| 56 | 56 | async function toReadableString() { | |
| 57 | 57 | const stream = Readable.from('abc'); | |
| 58 | 58 | ||
| 59 | - const expected = ['a', 'b', 'c']; | ||
| 59 | + const expected = ['abc']; | ||
| 60 | 60 | ||
| 61 | 61 | for await (const chunk of stream) { | |
| 62 | 62 | strictEqual(chunk, expected.shift()); | |
| 63 | 63 | } | |
| 64 | 64 | } | |
| 65 | 65 | ||
| 66 | + async function toReadableBuffer() { | ||
| 67 | + const stream = Readable.from(Buffer.from('abc')); | ||
| 68 | + | ||
| 69 | + const expected = ['abc']; | ||
| 70 | + | ||
| 71 | + for await (const chunk of stream) { | ||
| 72 | + strictEqual(chunk.toString(), expected.shift()); | ||
| 73 | + } | ||
| 74 | + } | ||
| 75 | + | ||
| 66 | 76 | async function toReadableOnData() { | |
| 67 | 77 | async function* generate() { | |
| 68 | 78 | yield 'a'; | |
@@ -154,6 +164,7 @@ Promise.all([ | |||
| 154 | 164 | toReadableSyncIterator(), | |
| 155 | 165 | toReadablePromises(), | |
| 156 | 166 | toReadableString(), | |
| 167 | + toReadableBuffer(), | ||
| 157 | 168 | toReadableOnData(), | |
| 158 | 169 | toReadableOnDataNonObject(), | |
| 159 | 170 | destroysTheStreamWhenThrowing(), | |
| Back | FazBrowse Home | New Git URL |
0 commit comments