11'use strict' ;
22
33const {
4+ StringPrototypeSlice,
45 SymbolIterator,
56 Uint8Array,
7+ TypedArrayPrototypeSet,
8+ TypedArrayPrototypeSlice,
69} = primordials ;
710
811const { Buffer } = require ( 'buffer' ) ;
@@ -67,7 +70,7 @@ module.exports = class BufferList {
6770 let p = this . head ;
6871 let i = 0 ;
6972 while ( p ) {
70- ret . set ( p . data , i ) ;
73+ TypedArrayPrototypeSet ( ret , p . data , i ) ;
7174 i += p . data . length ;
7275 p = p . next ;
7376 }
@@ -78,10 +81,11 @@ module.exports = class BufferList {
7881 consume ( n , hasStrings ) {
7982 const data = this . head . data ;
8083 if ( n < data . length ) {
81- // `slice` is the same for buffers and strings.
82- const slice = data . slice ( 0 , n ) ;
83- this . head . data = data . slice ( n ) ;
84- return slice ;
84+ const slice = typeof data === 'string' ?
85+ StringPrototypeSlice :
86+ TypedArrayPrototypeSlice ;
87+ this . head . data = slice ( data , n ) ;
88+ return slice ( data , 0 , n ) ;
8589 }
8690 if ( n === data . length ) {
8791 // First chunk is a perfect match.
@@ -120,9 +124,9 @@ module.exports = class BufferList {
120124 else
121125 this . head = this . tail = null ;
122126 } else {
123- ret += str . slice ( 0 , n ) ;
127+ ret += StringPrototypeSlice ( str , 0 , n ) ;
124128 this . head = p ;
125- p . data = str . slice ( n ) ;
129+ p . data = StringPrototypeSlice ( str , n ) ;
126130 }
127131 break ;
128132 }
@@ -141,20 +145,22 @@ module.exports = class BufferList {
141145 do {
142146 const buf = p . data ;
143147 if ( n > buf . length ) {
144- ret . set ( buf , retLen - n ) ;
148+ TypedArrayPrototypeSet ( ret , buf , retLen - n ) ;
145149 n -= buf . length ;
146150 } else {
147151 if ( n === buf . length ) {
148- ret . set ( buf , retLen - n ) ;
152+ TypedArrayPrototypeSet ( ret , buf , retLen - n ) ;
149153 ++ c ;
150154 if ( p . next )
151155 this . head = p . next ;
152156 else
153157 this . head = this . tail = null ;
154158 } else {
155- ret . set ( new Uint8Array ( buf . buffer , buf . byteOffset , n ) , retLen - n ) ;
159+ TypedArrayPrototypeSet ( ret ,
160+ new Uint8Array ( buf . buffer , buf . byteOffset , n ) ,
161+ retLen - n ) ;
156162 this . head = p ;
157- p . data = buf . slice ( n ) ;
163+ p . data = TypedArrayPrototypeSlice ( buf , n ) ;
158164 }
159165 break ;
160166 }
0 commit comments