Skip to content

Commit f8018f2

Browse files
mcollinaTrott
authored andcommitted
stream: do not chunk strings and Buffer in Readable.from
PR-URL: nodejs#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 e13a37e commit f8018f2

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

doc/api/stream.md

+4
Original file line numberDiff line numberDiff line change
@@ -1642,6 +1642,10 @@ readable.on('data', (chunk) => {
16421642
});
16431643
```
16441644

1645+
Calling `Readable.from(string)` or `Readable.from(buffer)` will not have
1646+
the strings or buffers be iterated to match the other streams semantics
1647+
for performance reasons.
1648+
16451649
## API for Stream Implementers
16461650

16471651
<!--type=misc-->

lib/internal/streams/from.js

+12
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,25 @@ const {
44
Symbol,
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[Symbol.asyncIterator])
1527
iterator = iterable[Symbol.asyncIterator]();
1628
else if (iterable && iterable[SymbolIterator])

test/parallel/test-readable-from.js

+12-1
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)