@@ -102,6 +102,8 @@ const kErroredValue = Symbol('kErroredValue');
102
102
const kDefaultEncodingValue = Symbol ( 'kDefaultEncodingValue' ) ;
103
103
const kDecoderValue = Symbol ( 'kDecoderValue' ) ;
104
104
const kEncodingValue = Symbol ( 'kEncodingValue' ) ;
105
+ const kBuffer = Symbol ( 'kBuffer' ) ;
106
+ const kBufferIndex = Symbol ( 'kBufferIndex' ) ;
105
107
106
108
const kEnded = 1 << 9 ;
107
109
const kEndEmitted = 1 << 10 ;
@@ -276,8 +278,8 @@ function ReadableState(options, stream, isDuplex) {
276
278
getHighWaterMark ( this , options , 'readableHighWaterMark' , isDuplex ) :
277
279
getDefaultHighWaterMark ( false ) ;
278
280
279
- this . buffer = [ ] ;
280
- this . bufferIndex = 0 ;
281
+ this [ kBuffer ] = [ ] ;
282
+ this [ kBufferIndex ] = 0 ;
281
283
this . length = 0 ;
282
284
this . pipes = [ ] ;
283
285
@@ -561,13 +563,13 @@ function addChunk(stream, state, chunk, addToFront) {
561
563
// Update the buffer info.
562
564
state . length += ( state [ kState ] & kObjectMode ) !== 0 ? 1 : chunk . length ;
563
565
if ( addToFront ) {
564
- if ( state . bufferIndex > 0 ) {
565
- state . buffer [ -- state . bufferIndex ] = chunk ;
566
+ if ( state [ kBufferIndex ] > 0 ) {
567
+ state [ kBuffer ] [ -- state [ kBufferIndex ] ] = chunk ;
566
568
} else {
567
- state . buffer . unshift ( chunk ) ; // Slow path
569
+ state [ kBuffer ] . unshift ( chunk ) ; // Slow path
568
570
}
569
571
} else {
570
- state . buffer . push ( chunk ) ;
572
+ state [ kBuffer ] . push ( chunk ) ;
571
573
}
572
574
573
575
if ( ( state [ kState ] & kNeedReadable ) !== 0 )
@@ -592,14 +594,14 @@ Readable.prototype.setEncoding = function(enc) {
592
594
593
595
// Iterate over current buffer to convert already stored Buffers:
594
596
let content = '' ;
595
- for ( const data of state . buffer . slice ( state . bufferIndex ) ) {
597
+ for ( const data of state [ kBuffer ] . slice ( state [ kBufferIndex ] ) ) {
596
598
content += decoder . write ( data ) ;
597
599
}
598
- state . buffer . length = 0 ;
599
- state . bufferIndex = 0 ;
600
+ state [ kBuffer ] . length = 0 ;
601
+ state [ kBufferIndex ] = 0 ;
600
602
601
603
if ( content !== '' )
602
- state . buffer . push ( content ) ;
604
+ state [ kBuffer ] . push ( content ) ;
603
605
state . length = content . length ;
604
606
return this ;
605
607
} ;
@@ -633,7 +635,7 @@ function howMuchToRead(n, state) {
633
635
if ( NumberIsNaN ( n ) ) {
634
636
// Only flow one buffer at a time.
635
637
if ( ( state [ kState ] & kFlowing ) !== 0 && state . length )
636
- return state . buffer [ state . bufferIndex ] . length ;
638
+ return state [ kBuffer ] [ state [ kBufferIndex ] ] . length ;
637
639
return state . length ;
638
640
}
639
641
if ( n <= state . length )
@@ -790,7 +792,7 @@ function onEofChunk(stream, state) {
790
792
if ( decoder ) {
791
793
const chunk = decoder . end ( ) ;
792
794
if ( chunk && chunk . length ) {
793
- state . buffer . push ( chunk ) ;
795
+ state [ kBuffer ] . push ( chunk ) ;
794
796
state . length += ( state [ kState ] & kObjectMode ) !== 0 ? 1 : chunk . length ;
795
797
}
796
798
}
@@ -1459,7 +1461,7 @@ ObjectDefineProperties(Readable.prototype, {
1459
1461
__proto__ : null ,
1460
1462
enumerable : false ,
1461
1463
get : function ( ) {
1462
- return this . _readableState && this . _readableState . buffer ;
1464
+ return this . _readableState && this . _readableState [ kBuffer ] ;
1463
1465
} ,
1464
1466
} ,
1465
1467
@@ -1541,9 +1543,15 @@ ObjectDefineProperties(Readable.prototype, {
1541
1543
return this . _readableState ? this . _readableState . endEmitted : false ;
1542
1544
} ,
1543
1545
} ,
1544
-
1545
1546
} ) ;
1546
1547
1548
+ function normalizeBuffer ( state ) {
1549
+ if ( state [ kBufferIndex ] > 0 ) {
1550
+ state . splice ( state [ kBufferIndex ] ) ;
1551
+ state [ kBufferIndex ] = 0 ;
1552
+ }
1553
+ }
1554
+
1547
1555
ObjectDefineProperties ( ReadableState . prototype , {
1548
1556
// Legacy getter for `pipesCount`.
1549
1557
pipesCount : {
@@ -1568,6 +1576,31 @@ ObjectDefineProperties(ReadableState.prototype, {
1568
1576
}
1569
1577
} ,
1570
1578
} ,
1579
+
1580
+ // Legacy compat
1581
+ buffer : {
1582
+ __proto__ : null ,
1583
+ get ( ) {
1584
+ const r = this . _readableState ;
1585
+ return new Proxy ( r [ kBuffer ] , {
1586
+ get ( target , name ) {
1587
+ if ( name === 'length' ) {
1588
+ return target [ kBuffer ] . length - target [ kBufferIndex ] ;
1589
+ }
1590
+ if ( name === '0' ) {
1591
+ return target [ kBuffer ] [ target [ kBufferIndex ] ] ;
1592
+ }
1593
+ normalizeBuffer ( target ) ;
1594
+ return target [ name ] ;
1595
+ } ,
1596
+ set ( target , property , value , receiver ) {
1597
+ normalizeBuffer ( target ) ;
1598
+ target [ property ] = value ;
1599
+ return true ;
1600
+ }
1601
+ } )
1602
+ } ,
1603
+ } ,
1571
1604
} ) ;
1572
1605
1573
1606
// Exposed for testing purposes only.
@@ -1582,10 +1615,10 @@ function fromList(n, state) {
1582
1615
if ( state . length === 0 )
1583
1616
return null ;
1584
1617
1585
- let idx = state . bufferIndex ;
1618
+ let idx = state [ kBufferIndex ] ;
1586
1619
let ret ;
1587
1620
1588
- const buf = state . buffer ;
1621
+ const buf = state [ kBuffer ] ;
1589
1622
const len = buf . length ;
1590
1623
1591
1624
if ( ( state [ kState ] & kObjectMode ) !== 0 ) {
@@ -1656,22 +1689,22 @@ function fromList(n, state) {
1656
1689
TypedArrayPrototypeSet ( ret , data , retLen - n ) ;
1657
1690
buf [ idx ++ ] = null ;
1658
1691
} else {
1659
- TypedArrayPrototypeSet ( ret , new FastBuffer ( data . buffer , data . byteOffset , n ) , retLen - n ) ;
1660
- buf [ idx ] = new FastBuffer ( data . buffer , data . byteOffset + n , data . length - n ) ;
1692
+ TypedArrayPrototypeSet ( ret , new FastBuffer ( data [ kBuffer ] , data . byteOffset , n ) , retLen - n ) ;
1693
+ buf [ idx ] = new FastBuffer ( data [ kBuffer ] , data . byteOffset + n , data . length - n ) ;
1661
1694
}
1662
1695
break ;
1663
1696
}
1664
1697
}
1665
1698
}
1666
1699
1667
1700
if ( idx === len ) {
1668
- state . buffer . length = 0 ;
1669
- state . bufferIndex = 0 ;
1701
+ state [ kBuffer ] . length = 0 ;
1702
+ state [ kBufferIndex ] = 0 ;
1670
1703
} else if ( idx > 1024 ) {
1671
- state . buffer . splice ( 0 , idx ) ;
1672
- state . bufferIndex = 0 ;
1704
+ state [ kBuffer ] . splice ( 0 , idx ) ;
1705
+ state [ kBufferIndex ] = 0 ;
1673
1706
} else {
1674
- state . bufferIndex = idx ;
1707
+ state [ kBufferIndex ] = idx ;
1675
1708
}
1676
1709
1677
1710
return ret ;
0 commit comments