1
1
'use strict'
2
2
3
- var symbol = Symbol . for ( 'BufferList' )
3
+ const symbol = Symbol . for ( 'BufferList' )
4
4
5
5
function BufferList ( buf ) {
6
- if ( ! ( this instanceof BufferList ) )
6
+ if ( ! ( this instanceof BufferList ) ) {
7
7
return new BufferList ( buf )
8
+ }
8
9
9
10
BufferList . _init . call ( this , buf )
10
11
}
@@ -14,80 +15,100 @@ BufferList._init = function _init (buf) {
14
15
15
16
this . _bufs = [ ]
16
17
this . length = 0
17
- if ( buf ) this . append ( buf )
18
+
19
+ if ( buf ) {
20
+ this . append ( buf )
21
+ }
18
22
}
19
23
20
24
BufferList . prototype . _new = function _new ( buf ) {
21
25
return new BufferList ( buf )
22
26
}
23
27
24
28
BufferList . prototype . _offset = function _offset ( offset ) {
25
- var tot = 0 , i = 0 , _t
26
- if ( offset === 0 ) return [ 0 , 0 ]
27
- for ( ; i < this . _bufs . length ; i ++ ) {
28
- _t = tot + this . _bufs [ i ] . length
29
- if ( offset < _t || i == this . _bufs . length - 1 ) {
30
- return [ i , offset - tot ]
29
+ if ( offset === 0 ) {
30
+ return [ 0 , 0 ]
31
+ }
32
+
33
+ let tot = 0
34
+
35
+ for ( let i = 0 ; i < this . _bufs . length ; i ++ ) {
36
+ const _t = tot + this . _bufs [ i ] . length
37
+ if ( offset < _t || i === this . _bufs . length - 1 ) {
38
+ return [ i , offset - tot ]
31
39
}
32
40
tot = _t
33
41
}
34
42
}
35
43
36
44
BufferList . prototype . _reverseOffset = function ( blOffset ) {
37
- var bufferId = blOffset [ 0 ]
38
- var offset = blOffset [ 1 ]
39
- for ( var i = 0 ; i < bufferId ; i ++ ) {
45
+ const bufferId = blOffset [ 0 ]
46
+ let offset = blOffset [ 1 ]
47
+
48
+ for ( let i = 0 ; i < bufferId ; i ++ ) {
40
49
offset += this . _bufs [ i ] . length
41
50
}
51
+
42
52
return offset
43
53
}
44
54
45
55
BufferList . prototype . get = function get ( index ) {
46
56
if ( index > this . length || index < 0 ) {
47
57
return undefined
48
58
}
49
- var offset = this . _offset ( index )
59
+
60
+ const offset = this . _offset ( index )
61
+
50
62
return this . _bufs [ offset [ 0 ] ] [ offset [ 1 ] ]
51
63
}
52
64
53
65
BufferList . prototype . slice = function slice ( start , end ) {
54
- if ( typeof start == 'number' && start < 0 )
66
+ if ( typeof start === 'number' && start < 0 ) {
55
67
start += this . length
56
- if ( typeof end == 'number' && end < 0 )
68
+ }
69
+
70
+ if ( typeof end === 'number' && end < 0 ) {
57
71
end += this . length
72
+ }
73
+
58
74
return this . copy ( null , 0 , start , end )
59
75
}
60
76
61
-
62
77
BufferList . prototype . copy = function copy ( dst , dstStart , srcStart , srcEnd ) {
63
- if ( typeof srcStart != 'number' || srcStart < 0 )
78
+ if ( typeof srcStart !== 'number' || srcStart < 0 ) {
64
79
srcStart = 0
65
- if ( typeof srcEnd != 'number' || srcEnd > this . length )
80
+ }
81
+
82
+ if ( typeof srcEnd !== 'number' || srcEnd > this . length ) {
66
83
srcEnd = this . length
67
- if ( srcStart >= this . length )
84
+ }
85
+
86
+ if ( srcStart >= this . length ) {
68
87
return dst || Buffer . alloc ( 0 )
69
- if ( srcEnd <= 0 )
88
+ }
89
+
90
+ if ( srcEnd <= 0 ) {
70
91
return dst || Buffer . alloc ( 0 )
92
+ }
71
93
72
- var copy = ! ! dst
73
- , off = this . _offset ( srcStart )
74
- , len = srcEnd - srcStart
75
- , bytes = len
76
- , bufoff = ( copy && dstStart ) || 0
77
- , start = off [ 1 ]
78
- , l
79
- , i
94
+ const copy = ! ! dst
95
+ const off = this . _offset ( srcStart )
96
+ const len = srcEnd - srcStart
97
+ let bytes = len
98
+ let bufoff = ( copy && dstStart ) || 0
99
+ let start = off [ 1 ]
80
100
81
101
// copy/slice everything
82
- if ( srcStart === 0 && srcEnd == this . length ) {
83
- if ( ! copy ) { // slice, but full concat if multiple buffers
102
+ if ( srcStart === 0 && srcEnd === this . length ) {
103
+ if ( ! copy ) {
104
+ // slice, but full concat if multiple buffers
84
105
return this . _bufs . length === 1
85
106
? this . _bufs [ 0 ]
86
107
: Buffer . concat ( this . _bufs , this . length )
87
108
}
88
109
89
110
// copy, need to copy individual buffers
90
- for ( i = 0 ; i < this . _bufs . length ; i ++ ) {
111
+ for ( let i = 0 ; i < this . _bufs . length ; i ++ ) {
91
112
this . _bufs [ i ] . copy ( dst , bufoff )
92
113
bufoff += this . _bufs [ i ] . length
93
114
}
@@ -102,11 +123,13 @@ BufferList.prototype.copy = function copy (dst, dstStart, srcStart, srcEnd) {
102
123
: this . _bufs [ off [ 0 ] ] . slice ( start , start + bytes )
103
124
}
104
125
105
- if ( ! copy ) // a slice, we need something to copy in to
126
+ if ( ! copy ) {
127
+ // a slice, we need something to copy in to
106
128
dst = Buffer . allocUnsafe ( len )
129
+ }
107
130
108
- for ( i = off [ 0 ] ; i < this . _bufs . length ; i ++ ) {
109
- l = this . _bufs [ i ] . length - start
131
+ for ( let i = off [ 0 ] ; i < this . _bufs . length ; i ++ ) {
132
+ const l = this . _bufs [ i ] . length - start
110
133
111
134
if ( bytes > l ) {
112
135
this . _bufs [ i ] . copy ( dst , bufoff , start )
@@ -118,8 +141,9 @@ BufferList.prototype.copy = function copy (dst, dstStart, srcStart, srcEnd) {
118
141
bufoff += l
119
142
bytes -= l
120
143
121
- if ( start )
144
+ if ( start ) {
122
145
start = 0
146
+ }
123
147
}
124
148
125
149
return dst
@@ -129,25 +153,31 @@ BufferList.prototype.shallowSlice = function shallowSlice (start, end) {
129
153
start = start || 0
130
154
end = typeof end !== 'number' ? this . length : end
131
155
132
- if ( start < 0 )
156
+ if ( start < 0 ) {
133
157
start += this . length
134
- if ( end < 0 )
158
+ }
159
+
160
+ if ( end < 0 ) {
135
161
end += this . length
162
+ }
136
163
137
164
if ( start === end ) {
138
165
return this . _new ( )
139
166
}
140
- var startOffset = this . _offset ( start )
141
- , endOffset = this . _offset ( end )
142
- , buffers = this . _bufs . slice ( startOffset [ 0 ] , endOffset [ 0 ] + 1 )
143
167
144
- if ( endOffset [ 1 ] == 0 )
168
+ const startOffset = this . _offset ( start )
169
+ const endOffset = this . _offset ( end )
170
+ const buffers = this . _bufs . slice ( startOffset [ 0 ] , endOffset [ 0 ] + 1 )
171
+
172
+ if ( endOffset [ 1 ] === 0 ) {
145
173
buffers . pop ( )
146
- else
147
- buffers [ buffers . length - 1 ] = buffers [ buffers . length - 1 ] . slice ( 0 , endOffset [ 1 ] )
174
+ } else {
175
+ buffers [ buffers . length - 1 ] = buffers [ buffers . length - 1 ] . slice ( 0 , endOffset [ 1 ] )
176
+ }
148
177
149
- if ( startOffset [ 1 ] != 0 )
178
+ if ( startOffset [ 1 ] !== 0 ) {
150
179
buffers [ 0 ] = buffers [ 0 ] . slice ( startOffset [ 1 ] )
180
+ }
151
181
152
182
return this . _new ( buffers )
153
183
}
@@ -168,23 +198,21 @@ BufferList.prototype.consume = function consume (bytes) {
168
198
break
169
199
}
170
200
}
201
+
171
202
return this
172
203
}
173
204
174
-
175
205
BufferList . prototype . duplicate = function duplicate ( ) {
176
- var i = 0
177
- , copy = this . _new ( )
206
+ const copy = this . _new ( )
178
207
179
- for ( ; i < this . _bufs . length ; i ++ )
208
+ for ( let i = 0 ; i < this . _bufs . length ; i ++ ) {
180
209
copy . append ( this . _bufs [ i ] )
210
+ }
181
211
182
212
return copy
183
213
}
184
214
185
215
BufferList . prototype . append = function append ( buf ) {
186
- var i = 0
187
-
188
216
if ( buf == null ) {
189
217
return this
190
218
}
@@ -193,17 +221,20 @@ BufferList.prototype.append = function append (buf) {
193
221
// append a view of the underlying ArrayBuffer
194
222
this . _appendBuffer ( Buffer . from ( buf . buffer , buf . byteOffset , buf . byteLength ) )
195
223
} else if ( Array . isArray ( buf ) ) {
196
- for ( ; i < buf . length ; i ++ )
224
+ for ( let i = 0 ; i < buf . length ; i ++ ) {
197
225
this . append ( buf [ i ] )
226
+ }
198
227
} else if ( this . _isBufferList ( buf ) ) {
199
228
// unwrap argument into individual BufferLists
200
- for ( ; i < buf . _bufs . length ; i ++ )
229
+ for ( let i = 0 ; i < buf . _bufs . length ; i ++ ) {
201
230
this . append ( buf . _bufs [ i ] )
231
+ }
202
232
} else {
203
233
// coerce number arguments to strings, since Buffer(number) does
204
234
// uninitialized memory allocation
205
- if ( typeof buf == 'number' )
235
+ if ( typeof buf === 'number' ) {
206
236
buf = buf . toString ( )
237
+ }
207
238
208
239
this . _appendBuffer ( Buffer . from ( buf ) )
209
240
}
@@ -221,6 +252,7 @@ BufferList.prototype.indexOf = function (search, offset, encoding) {
221
252
encoding = offset
222
253
offset = undefined
223
254
}
255
+
224
256
if ( typeof search === 'function' || Array . isArray ( search ) ) {
225
257
throw new TypeError ( 'The "value" argument must be one of type string, Buffer, BufferList, or Uint8Array.' )
226
258
} else if ( typeof search === 'number' ) {
@@ -236,6 +268,7 @@ BufferList.prototype.indexOf = function (search, offset, encoding) {
236
268
}
237
269
238
270
offset = Number ( offset || 0 )
271
+
239
272
if ( isNaN ( offset ) ) {
240
273
offset = 0
241
274
}
@@ -252,39 +285,48 @@ BufferList.prototype.indexOf = function (search, offset, encoding) {
252
285
return offset > this . length ? this . length : offset
253
286
}
254
287
255
- var blOffset = this . _offset ( offset )
256
- var blIndex = blOffset [ 0 ] // index of which internal buffer we're working on
257
- var buffOffset = blOffset [ 1 ] // offset of the internal buffer we're working on
288
+ const blOffset = this . _offset ( offset )
289
+ let blIndex = blOffset [ 0 ] // index of which internal buffer we're working on
290
+ let buffOffset = blOffset [ 1 ] // offset of the internal buffer we're working on
258
291
259
292
// scan over each buffer
260
- for ( blIndex ; blIndex < this . _bufs . length ; blIndex ++ ) {
261
- var buff = this . _bufs [ blIndex ]
293
+ for ( ; blIndex < this . _bufs . length ; blIndex ++ ) {
294
+ const buff = this . _bufs [ blIndex ]
295
+
262
296
while ( buffOffset < buff . length ) {
263
- var availableWindow = buff . length - buffOffset
297
+ const availableWindow = buff . length - buffOffset
298
+
264
299
if ( availableWindow >= search . length ) {
265
- var nativeSearchResult = buff . indexOf ( search , buffOffset )
300
+ const nativeSearchResult = buff . indexOf ( search , buffOffset )
301
+
266
302
if ( nativeSearchResult !== - 1 ) {
267
303
return this . _reverseOffset ( [ blIndex , nativeSearchResult ] )
268
304
}
305
+
269
306
buffOffset = buff . length - search . length + 1 // end of native search window
270
307
} else {
271
- var revOffset = this . _reverseOffset ( [ blIndex , buffOffset ] )
308
+ const revOffset = this . _reverseOffset ( [ blIndex , buffOffset ] )
309
+
272
310
if ( this . _match ( revOffset , search ) ) {
273
311
return revOffset
274
312
}
313
+
275
314
buffOffset ++
276
315
}
277
316
}
317
+
278
318
buffOffset = 0
279
319
}
320
+
280
321
return - 1
281
322
}
282
323
283
324
BufferList . prototype . _match = function ( offset , search ) {
284
325
if ( this . length - offset < search . length ) {
285
326
return false
286
327
}
287
- for ( var searchOffset = 0 ; searchOffset < search . length ; searchOffset ++ ) {
328
+
329
+ for ( let searchOffset = 0 ; searchOffset < search . length ; searchOffset ++ ) {
288
330
if ( this . get ( offset + searchOffset ) !== search [ searchOffset ] ) {
289
331
return false
290
332
}
@@ -293,28 +335,28 @@ BufferList.prototype._match = function (offset, search) {
293
335
}
294
336
295
337
; ( function ( ) {
296
- var methods = {
297
- ' readDoubleBE' : 8
298
- , ' readDoubleLE' : 8
299
- , ' readFloatBE' : 4
300
- , ' readFloatLE' : 4
301
- , ' readInt32BE' : 4
302
- , ' readInt32LE' : 4
303
- , ' readUInt32BE' : 4
304
- , ' readUInt32LE' : 4
305
- , ' readInt16BE' : 2
306
- , ' readInt16LE' : 2
307
- , ' readUInt16BE' : 2
308
- , ' readUInt16LE' : 2
309
- , ' readInt8' : 1
310
- , ' readUInt8' : 1
311
- , ' readIntBE' : null
312
- , ' readIntLE' : null
313
- , ' readUIntBE' : null
314
- , ' readUIntLE' : null
315
- }
316
-
317
- for ( var m in methods ) {
338
+ const methods = {
339
+ readDoubleBE : 8 ,
340
+ readDoubleLE : 8 ,
341
+ readFloatBE : 4 ,
342
+ readFloatLE : 4 ,
343
+ readInt32BE : 4 ,
344
+ readInt32LE : 4 ,
345
+ readUInt32BE : 4 ,
346
+ readUInt32LE : 4 ,
347
+ readInt16BE : 2 ,
348
+ readInt16LE : 2 ,
349
+ readUInt16BE : 2 ,
350
+ readUInt16LE : 2 ,
351
+ readInt8 : 1 ,
352
+ readUInt8 : 1 ,
353
+ readIntBE : null ,
354
+ readIntLE : null ,
355
+ readUIntBE : null ,
356
+ readUIntLE : null
357
+ }
358
+
359
+ for ( const m in methods ) {
318
360
( function ( m ) {
319
361
if ( methods [ m ] === null ) {
320
362
BufferList . prototype [ m ] = function ( offset , byteLength ) {
0 commit comments