11'use strict'
22
33const { StringPrototypeSlice, SymbolIterator, TypedArrayPrototypeSet, Uint8Array } = require ( '../../ours/primordials' )
4-
54const { Buffer } = require ( 'buffer' )
6-
75const { inspect } = require ( '../../ours/util' )
8-
96module . exports = class BufferList {
107 constructor ( ) {
118 this . head = null
129 this . tail = null
1310 this . length = 0
1411 }
15-
1612 push ( v ) {
1713 const entry = {
1814 data : v ,
@@ -23,7 +19,6 @@ module.exports = class BufferList {
2319 this . tail = entry
2420 ++ this . length
2521 }
26-
2722 unshift ( v ) {
2823 const entry = {
2924 data : v ,
@@ -33,7 +28,6 @@ module.exports = class BufferList {
3328 this . head = entry
3429 ++ this . length
3530 }
36-
3731 shift ( ) {
3832 if ( this . length === 0 ) return
3933 const ret = this . head . data
@@ -42,73 +36,62 @@ module.exports = class BufferList {
4236 -- this . length
4337 return ret
4438 }
45-
4639 clear ( ) {
4740 this . head = this . tail = null
4841 this . length = 0
4942 }
50-
5143 join ( s ) {
5244 if ( this . length === 0 ) return ''
5345 let p = this . head
5446 let ret = '' + p . data
55-
5647 while ( ( p = p . next ) !== null ) ret += s + p . data
57-
5848 return ret
5949 }
60-
6150 concat ( n ) {
6251 if ( this . length === 0 ) return Buffer . alloc ( 0 )
6352 const ret = Buffer . allocUnsafe ( n >>> 0 )
6453 let p = this . head
6554 let i = 0
66-
6755 while ( p ) {
6856 TypedArrayPrototypeSet ( ret , p . data , i )
6957 i += p . data . length
7058 p = p . next
7159 }
72-
7360 return ret
74- } // Consumes a specified amount of bytes or characters from the buffered data.
61+ }
7562
63+ // Consumes a specified amount of bytes or characters from the buffered data.
7664 consume ( n , hasStrings ) {
7765 const data = this . head . data
78-
7966 if ( n < data . length ) {
8067 // `slice` is the same for buffers and strings.
8168 const slice = data . slice ( 0 , n )
8269 this . head . data = data . slice ( n )
8370 return slice
8471 }
85-
8672 if ( n === data . length ) {
8773 // First chunk is a perfect match.
8874 return this . shift ( )
89- } // Result spans more than one buffer.
90-
75+ }
76+ // Result spans more than one buffer.
9177 return hasStrings ? this . _getString ( n ) : this . _getBuffer ( n )
9278 }
93-
9479 first ( ) {
9580 return this . head . data
9681 }
97-
9882 * [ SymbolIterator ] ( ) {
9983 for ( let p = this . head ; p ; p = p . next ) {
10084 yield p . data
10185 }
102- } // Consumes a specified amount of characters from the buffered data.
86+ }
10387
88+ // Consumes a specified amount of characters from the buffered data.
10489 _getString ( n ) {
10590 let ret = ''
10691 let p = this . head
10792 let c = 0
108-
10993 do {
11094 const str = p . data
111-
11295 if ( n > str . length ) {
11396 ret += str
11497 n -= str . length
@@ -123,26 +106,22 @@ module.exports = class BufferList {
123106 this . head = p
124107 p . data = StringPrototypeSlice ( str , n )
125108 }
126-
127109 break
128110 }
129-
130111 ++ c
131112 } while ( ( p = p . next ) !== null )
132-
133113 this . length -= c
134114 return ret
135- } // Consumes a specified amount of bytes from the buffered data.
115+ }
136116
117+ // Consumes a specified amount of bytes from the buffered data.
137118 _getBuffer ( n ) {
138119 const ret = Buffer . allocUnsafe ( n )
139120 const retLen = n
140121 let p = this . head
141122 let c = 0
142-
143123 do {
144124 const buf = p . data
145-
146125 if ( n > buf . length ) {
147126 TypedArrayPrototypeSet ( ret , buf , retLen - n )
148127 n -= buf . length
@@ -157,17 +136,15 @@ module.exports = class BufferList {
157136 this . head = p
158137 p . data = buf . slice ( n )
159138 }
160-
161139 break
162140 }
163-
164141 ++ c
165142 } while ( ( p = p . next ) !== null )
166-
167143 this . length -= c
168144 return ret
169- } // Make sure the linked list only shows the minimal necessary information.
145+ }
170146
147+ // Make sure the linked list only shows the minimal necessary information.
171148 [ Symbol . for ( 'nodejs.util.inspect.custom' ) ] ( _ , options ) {
172149 return inspect ( this , {
173150 ...options ,
0 commit comments