@@ -12,6 +12,7 @@ const {
1212const { concat, toArrayBuffer, unmask } = require ( './buffer-util' ) ;
1313const { isValidStatusCode, isValidUTF8 } = require ( './validation' ) ;
1414
15+ const FastBuffer = Buffer [ Symbol . species ] ;
1516const GET_INFO = 0 ;
1617const GET_PAYLOAD_LENGTH_16 = 1 ;
1718const GET_PAYLOAD_LENGTH_64 = 2 ;
@@ -97,8 +98,13 @@ class Receiver extends Writable {
9798
9899 if ( n < this . _buffers [ 0 ] . length ) {
99100 const buf = this . _buffers [ 0 ] ;
100- this . _buffers [ 0 ] = buf . subarray ( n ) ;
101- return buf . subarray ( 0 , n ) ;
101+ this . _buffers [ 0 ] = new FastBuffer (
102+ buf . buffer ,
103+ buf . byteOffset + n ,
104+ buf . length - n
105+ ) ;
106+
107+ return new FastBuffer ( buf . buffer , buf . byteOffset , n ) ;
102108 }
103109
104110 const dst = Buffer . allocUnsafe ( n ) ;
@@ -111,7 +117,11 @@ class Receiver extends Writable {
111117 dst . set ( this . _buffers . shift ( ) , offset ) ;
112118 } else {
113119 dst . set ( new Uint8Array ( buf . buffer , buf . byteOffset , n ) , offset ) ;
114- this . _buffers [ 0 ] = buf . subarray ( n ) ;
120+ this . _buffers [ 0 ] = new FastBuffer (
121+ buf . buffer ,
122+ buf . byteOffset + n ,
123+ buf . length - n
124+ ) ;
115125 }
116126
117127 n -= buf . length ;
@@ -562,7 +572,11 @@ class Receiver extends Writable {
562572 ) ;
563573 }
564574
565- const buf = data . subarray ( 2 ) ;
575+ const buf = new FastBuffer (
576+ data . buffer ,
577+ data . byteOffset + 2 ,
578+ data . length - 2
579+ ) ;
566580
567581 if ( ! this . _skipUTF8Validation && ! isValidUTF8 ( buf ) ) {
568582 return error (
0 commit comments