Skip to content

Commit 322912a

Browse files
mcollinaBethGriggs
authored andcommitted
stream: do not chunk strings and Buffer in Readable.from
PR-URL: #30912 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent d19316d commit 322912a

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

‎doc/api/stream.md‎

Lines changed: 4 additions & 0 deletions
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-->

‎lib/internal/streams/from.js‎

Lines 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
functionfrom(Readable,iterable,opts){
1314
letiterator;
15+
if(typeofiterable==='string'||iterableinstanceofBuffer){
16+
returnnewReadable({
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
elseif(iterable&&iterable[SymbolIterator])

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,23 @@ async function toReadablePromises(){
5656
asyncfunctiontoReadableString(){
5757
conststream=Readable.from('abc');
5858

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

6161
forawait(constchunkofstream){
6262
strictEqual(chunk,expected.shift());
6363
}
6464
}
6565

66+
asyncfunctiontoReadableBuffer(){
67+
conststream=Readable.from(Buffer.from('abc'));
68+
69+
constexpected=['abc'];
70+
71+
forawait(constchunkofstream){
72+
strictEqual(chunk.toString(),expected.shift());
73+
}
74+
}
75+
6676
asyncfunctiontoReadableOnData(){
6777
asyncfunction*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)