Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8cd8cd7

Browse filesBrowse files
mcollinatargos
authored andcommitted
stream: do not chunk strings and Buffer in Readable.from
PR-URL: #30912 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 0266d7f commit 8cd8cd7
Copy full SHA for 8cd8cd7

File tree

Expand file treeCollapse file tree

3 files changed

+28
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+28
-1
lines changed
Open diff view settings
Collapse file

‎doc/api/stream.md‎

Copy file name to clipboardExpand all lines: doc/api/stream.md
+4Lines changed: 4 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1641,6 +1641,10 @@ readable.on('data', (chunk) => {
16411641
});
16421642
```
16431643

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+
16441648
## API for Stream Implementers
16451649

16461650
<!--type=misc-->
Collapse file

‎lib/internal/streams/from.js‎

Copy file name to clipboardExpand all lines: lib/internal/streams/from.js
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,25 @@ const {
44
SymbolAsyncIterator,
55
SymbolIterator
66
} = primordials;
7+
const { Buffer } = require('buffer');
78

89
const {
910
ERR_INVALID_ARG_TYPE
1011
} = require('internal/errors').codes;
1112

1213
function from(Readable, iterable, opts) {
1314
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+
1426
if (iterable && iterable[SymbolAsyncIterator])
1527
iterator = iterable[SymbolAsyncIterator]();
1628
else if (iterable && iterable[SymbolIterator])
Collapse file

‎test/parallel/test-readable-from.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-readable-from.js
+12-1Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,23 @@ async function toReadablePromises() {
5656
async function toReadableString() {
5757
const stream = Readable.from('abc');
5858

59-
const expected = ['a', 'b', 'c'];
59+
const expected = ['abc'];
6060

6161
for await (const chunk of stream) {
6262
strictEqual(chunk, expected.shift());
6363
}
6464
}
6565

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+
6676
async function toReadableOnData() {
6777
async function* generate() {
6878
yield 'a';
@@ -154,6 +164,7 @@ Promise.all([
154164
toReadableSyncIterator(),
155165
toReadablePromises(),
156166
toReadableString(),
167+
toReadableBuffer(),
157168
toReadableOnData(),
158169
toReadableOnDataNonObject(),
159170
destroysTheStreamWhenThrowing(),

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.