File tree Expand file tree Collapse file tree 3 files changed +28
-1
lines changed Expand file tree Collapse file tree 3 files changed +28
-1
lines changed Original file line number Diff line number Diff 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-->
Original file line number Diff line number Diff line change @@ -4,13 +4,25 @@ const {
44 SymbolAsyncIterator,
55 SymbolIterator
66} = primordials ;
7+ const { Buffer } = require ( 'buffer' ) ;
78
89const {
910 ERR_INVALID_ARG_TYPE
1011} = require ( 'internal/errors' ) . codes ;
1112
1213function 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 ] )
Original file line number Diff line number Diff line change @@ -56,13 +56,23 @@ async function toReadablePromises() {
5656async 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+
6676async 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 ( ) ,
You can’t perform that action at this time.
0 commit comments