@@ -641,8 +641,16 @@ function howMuchToRead(n, state) {
641641 return ( state [ kState ] & kEnded ) !== 0 ? state . length : 0 ;
642642}
643643
644+ Readable . prototype . readv = function readv ( ) {
645+ return _read . call ( this , true ) ;
646+ } ;
647+
648+ Readable . prototype . read = function read ( ) {
649+ return _read . call ( this , false ) ;
650+ }
651+
644652// You can override either this method, or the async _read(n) below.
645- Readable . prototype . read = function ( n ) {
653+ function _read ( n , returnArr ) {
646654 debug ( 'read' , n ) ;
647655 // Same as parseInt(undefined, 10), however V8 7.3 performance regressed
648656 // in this scenario, so we are doing it manually.
@@ -748,7 +756,7 @@ Readable.prototype.read = function(n) {
748756
749757 let ret ;
750758 if ( n > 0 )
751- ret = fromList ( n , state ) ;
759+ ret = fromList ( n , state , returnArr ) ;
752760 else
753761 ret = null ;
754762
@@ -777,7 +785,13 @@ Readable.prototype.read = function(n) {
777785
778786 if ( ret !== null && ( state [ kState ] & ( kErrorEmitted | kCloseEmitted ) ) === 0 ) {
779787 state [ kState ] |= kDataEmitted ;
780- this . emit ( 'data' , ret ) ;
788+ if ( returnArr ) {
789+ for ( let i = 0 ; i < ret . length ; ++ i ) {
790+ this . emit ( 'data' , ret [ i ] ) ;
791+ }
792+ } else {
793+ this . emit ( 'data' , ret ) ;
794+ }
781795 }
782796
783797 return ret ;
@@ -1582,7 +1596,7 @@ Readable._fromList = fromList;
15821596// Length is the combined lengths of all the buffers in the list.
15831597// This function is designed to be inlinable, so please take care when making
15841598// changes to the function body.
1585- function fromList ( n , state ) {
1599+ function fromList ( n , state , returnArr ) {
15861600 // nothing buffered.
15871601 if ( state . length === 0 )
15881602 return null ;
@@ -1594,16 +1608,25 @@ function fromList(n, state) {
15941608 const len = buf . length ;
15951609
15961610 if ( ( state [ kState ] & kObjectMode ) !== 0 ) {
1597- ret = buf [ idx ] ;
1598- buf [ idx ++ ] = null ;
1611+ if ( returnArr ) {
1612+ ret = buf . slice ( idx ) ;
1613+ idx += ret . length ;
1614+ } else {
1615+ ret = buf [ idx ] ;
1616+ buf [ idx ++ ] = null ;
1617+ ret = returnArr ? [ ret ] : ret ;
1618+ }
15991619 } else if ( ! n || n >= state . length ) {
16001620 // Read it all, truncate the list.
1601- if ( ( state [ kState ] & kDecoder ) !== 0 ) {
1621+ if ( ( state [ kState ] & kDecoder ) !== 0 ) {
16021622 ret = '' ;
16031623 while ( idx < len ) {
16041624 ret += buf [ idx ] ;
16051625 buf [ idx ++ ] = null ;
16061626 }
1627+ } else if ( returnArr ) {
1628+ ret = buf . slice ( idx ) ;
1629+ idx += ret . length ;
16071630 } else if ( len - idx === 0 ) {
16081631 ret = Buffer . alloc ( 0 ) ;
16091632 } else if ( len - idx === 1 ) {
@@ -1623,10 +1646,12 @@ function fromList(n, state) {
16231646 // `slice` is the same for buffers and strings.
16241647 ret = buf [ idx ] . slice ( 0 , n ) ;
16251648 buf [ idx ] = buf [ idx ] . slice ( n ) ;
1649+ ret = returnArr ? [ ret ] : ret ;
16261650 } else if ( n === buf [ idx ] . length ) {
16271651 // First chunk is a perfect match.
16281652 ret = buf [ idx ] ;
16291653 buf [ idx ++ ] = null ;
1654+ ret = returnArr ? [ ret ] : ret ;
16301655 } else if ( ( state [ kState ] & kDecoder ) !== 0 ) {
16311656 ret = '' ;
16321657 while ( idx < len ) {
@@ -1646,6 +1671,27 @@ function fromList(n, state) {
16461671 break ;
16471672 }
16481673 }
1674+ ret = returnArr ? [ ret ] : ret ;
1675+ } else if ( returnArr ) {
1676+ ret = [ ] ;
1677+ const retLen = n ;
1678+ while ( idx < len ) {
1679+ const data = buf [ idx ] ;
1680+ if ( n > data . length ) {
1681+ ret . push ( data ) ;
1682+ n -= data . length ;
1683+ buf [ idx ++ ] = null ;
1684+ } else {
1685+ if ( n === data . length ) {
1686+ ret . push ( data ) ;
1687+ buf [ idx ++ ] = null ;
1688+ } else {
1689+ ret . push ( new FastBuffer ( data . buffer , data . byteOffset , n ) ) ;
1690+ buf [ idx ] = new FastBuffer ( data . buffer , data . byteOffset + n , data . length - n ) ;
1691+ }
1692+ break ;
1693+ }
1694+ }
16491695 } else {
16501696 ret = Buffer . allocUnsafe ( n ) ;
16511697
0 commit comments