bl 6.1.6 → 7.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/BufferList.js CHANGED
@@ -1,421 +1,503 @@
1
- 'use strict'
1
+ import { Buffer } from 'node:buffer'
2
+
3
+ /**
4
+ * @typedef {Buffer | BufferList | Uint8Array | Array<Buffer | BufferList | Uint8Array | string | number> | string | number} BufferListAcceptedTypes
5
+ */
2
6
 
3
- const { Buffer } = require('buffer')
4
7
  const symbol = Symbol.for('BufferList')
5
8
 
6
- function BufferList (buf) {
7
- if (!(this instanceof BufferList)) {
8
- return new BufferList(buf)
9
+ export class BufferList {
10
+ /** @param {BufferListAcceptedTypes} [buf] */
11
+ constructor (buf) {
12
+ /** @type {Buffer[]} */
13
+ this._bufs = []
14
+ /** @type {number} */
15
+ this.length = 0
16
+ Object.defineProperty(this, symbol, { value: true })
17
+ if (buf) {
18
+ this.append(buf)
19
+ }
9
20
  }
10
21
 
11
- BufferList._init.call(this, buf)
12
- }
13
-
14
- BufferList._init = function _init (buf) {
15
- Object.defineProperty(this, symbol, { value: true })
16
-
17
- this._bufs = []
18
- this.length = 0
19
-
20
- if (buf) {
21
- this.append(buf)
22
+ /** @param {BufferListAcceptedTypes} [buf] */
23
+ _new (buf) {
24
+ return new BufferList(buf)
22
25
  }
23
- }
24
26
 
25
- BufferList.prototype._new = function _new (buf) {
26
- return new BufferList(buf)
27
- }
28
-
29
- BufferList.prototype._offset = function _offset (offset) {
30
- if (offset === 0) {
31
- return [0, 0]
32
- }
27
+ /**
28
+ * @param {number} offset
29
+ * @returns {[number, number]}
30
+ */
31
+ _offset (offset) {
32
+ if (offset === 0 || this._bufs.length === 0) {
33
+ return [0, 0]
34
+ }
33
35
 
34
- let tot = 0
36
+ let tot = 0
35
37
 
36
- for (let i = 0; i < this._bufs.length; i++) {
37
- const _t = tot + this._bufs[i].length
38
- if (offset < _t || i === this._bufs.length - 1) {
39
- return [i, offset - tot]
38
+ for (let i = 0; i < this._bufs.length; i++) {
39
+ const _t = tot + this._bufs[i].length
40
+ if (offset < _t || i === this._bufs.length - 1) {
41
+ return [i, offset - tot]
42
+ }
43
+ tot = _t
40
44
  }
41
- tot = _t
42
- }
43
- }
44
-
45
- BufferList.prototype._reverseOffset = function (blOffset) {
46
- const bufferId = blOffset[0]
47
- let offset = blOffset[1]
48
45
 
49
- for (let i = 0; i < bufferId; i++) {
50
- offset += this._bufs[i].length
46
+ return [0, 0]
51
47
  }
52
48
 
53
- return offset
54
- }
49
+ /**
50
+ * @param {[number, number]} blOffset
51
+ * @returns {number}
52
+ */
53
+ _reverseOffset (blOffset) {
54
+ const bufferId = blOffset[0]
55
+ let offset = blOffset[1]
55
56
 
56
- BufferList.prototype.getBuffers = function getBuffers () {
57
- return this._bufs
58
- }
57
+ for (let i = 0; i < bufferId; i++) {
58
+ offset += this._bufs[i].length
59
+ }
59
60
 
60
- BufferList.prototype.get = function get (index) {
61
- if (index > this.length || index < 0) {
62
- return undefined
61
+ return offset
63
62
  }
64
63
 
65
- const offset = this._offset(index)
66
-
67
- return this._bufs[offset[0]][offset[1]]
68
- }
69
-
70
- BufferList.prototype.slice = function slice (start, end) {
71
- if (typeof start === 'number' && start < 0) {
72
- start += this.length
64
+ /** @returns {Buffer[]} */
65
+ getBuffers () {
66
+ return this._bufs
73
67
  }
74
68
 
75
- if (typeof end === 'number' && end < 0) {
76
- end += this.length
77
- }
69
+ /**
70
+ * @param {number} index
71
+ * @returns {number | undefined}
72
+ */
73
+ get (index) {
74
+ if (index > this.length || index < 0) {
75
+ return undefined
76
+ }
78
77
 
79
- return this.copy(null, 0, start, end)
80
- }
78
+ const offset = this._offset(index)
81
79
 
82
- BufferList.prototype.copy = function copy (dst, dstStart, srcStart, srcEnd) {
83
- if (typeof srcStart !== 'number' || srcStart < 0) {
84
- srcStart = 0
80
+ return this._bufs[offset[0]][offset[1]]
85
81
  }
86
82
 
87
- if (typeof srcEnd !== 'number' || srcEnd > this.length) {
88
- srcEnd = this.length
89
- }
83
+ /**
84
+ * @param {number} [start]
85
+ * @param {number} [end]
86
+ * @returns {Buffer}
87
+ */
88
+ slice (start, end) {
89
+ if (typeof start === 'number' && start < 0) {
90
+ start += this.length
91
+ }
90
92
 
91
- if (srcStart >= this.length) {
92
- return dst || Buffer.alloc(0)
93
- }
93
+ if (typeof end === 'number' && end < 0) {
94
+ end += this.length
95
+ }
94
96
 
95
- if (srcEnd <= 0) {
96
- return dst || Buffer.alloc(0)
97
+ return this.copy(null, 0, start, end)
97
98
  }
98
99
 
99
- const copy = !!dst
100
- const off = this._offset(srcStart)
101
- const len = srcEnd - srcStart
102
- let bytes = len
103
- let bufoff = (copy && dstStart) || 0
104
- let start = off[1]
105
-
106
- // copy/slice everything
107
- if (srcStart === 0 && srcEnd === this.length) {
108
- if (!copy) {
109
- // slice, but full concat if multiple buffers
110
- return this._bufs.length === 1
111
- ? this._bufs[0]
112
- : Buffer.concat(this._bufs, this.length)
100
+ /**
101
+ * @param {Buffer | null} dst
102
+ * @param {number} [dstStart]
103
+ * @param {number} [srcStart]
104
+ * @param {number} [srcEnd]
105
+ * @returns {Buffer}
106
+ */
107
+ copy (dst, dstStart, srcStart, srcEnd) {
108
+ if (typeof srcStart !== 'number' || srcStart < 0) {
109
+ srcStart = 0
113
110
  }
114
111
 
115
- // copy, need to copy individual buffers
116
- for (let i = 0; i < this._bufs.length; i++) {
117
- this._bufs[i].copy(dst, bufoff)
118
- bufoff += this._bufs[i].length
112
+ if (typeof srcEnd !== 'number' || srcEnd > this.length) {
113
+ srcEnd = this.length
119
114
  }
120
115
 
121
- return dst
122
- }
116
+ if (srcStart >= this.length) {
117
+ return dst || Buffer.alloc(0)
118
+ }
123
119
 
124
- // easy, cheap case where it's a subset of one of the buffers
125
- if (bytes <= this._bufs[off[0]].length - start) {
126
- return copy
127
- ? this._bufs[off[0]].copy(dst, dstStart, start, start + bytes)
128
- : this._bufs[off[0]].slice(start, start + bytes)
129
- }
120
+ if (srcEnd <= 0) {
121
+ return dst || Buffer.alloc(0)
122
+ }
130
123
 
131
- if (!copy) {
132
- // a slice, we need something to copy in to
133
- dst = Buffer.allocUnsafe(len)
134
- }
124
+ const copy = !!dst
125
+ const off = this._offset(srcStart)
126
+ const len = srcEnd - srcStart
127
+ let bytes = len
128
+ let bufoff = (copy && dstStart) || 0
129
+ let start = off[1]
130
+
131
+ // copy/slice everything
132
+ if (srcStart === 0 && srcEnd === this.length) {
133
+ if (!copy) {
134
+ // slice, but full concat if multiple buffers
135
+ return this._bufs.length === 1
136
+ ? this._bufs[0]
137
+ : Buffer.concat(/** @type {any} */ (this._bufs), this.length)
138
+ }
135
139
 
136
- for (let i = off[0]; i < this._bufs.length; i++) {
137
- const l = this._bufs[i].length - start
140
+ // copy, need to copy individual buffers
141
+ const d = /** @type {any} */ (dst)
142
+ for (let i = 0; i < this._bufs.length; i++) {
143
+ this._bufs[i].copy(d, bufoff)
144
+ bufoff += this._bufs[i].length
145
+ }
138
146
 
139
- if (bytes > l) {
140
- this._bufs[i].copy(dst, bufoff, start)
141
- bufoff += l
142
- } else {
143
- this._bufs[i].copy(dst, bufoff, start, start + bytes)
144
- bufoff += l
145
- break
147
+ return d
146
148
  }
147
149
 
148
- bytes -= l
150
+ // easy, cheap case where it's a subset of one of the buffers
151
+ if (bytes <= this._bufs[off[0]].length - start) {
152
+ if (!copy) {
153
+ return this._bufs[off[0]].slice(start, start + bytes)
154
+ }
155
+ this._bufs[off[0]].copy(/** @type {any} */ (dst), dstStart, start, start + bytes)
156
+ return /** @type {Buffer} */ (dst)
157
+ }
149
158
 
150
- if (start) {
151
- start = 0
159
+ if (!copy) {
160
+ // a slice, we need something to copy in to
161
+ dst = Buffer.allocUnsafe(len)
152
162
  }
153
- }
154
163
 
155
- // safeguard so that we don't return uninitialized memory
156
- if (dst.length > bufoff) return dst.slice(0, bufoff)
164
+ const target = /** @type {any} */ (dst)
157
165
 
158
- return dst
159
- }
166
+ for (let i = off[0]; i < this._bufs.length; i++) {
167
+ const l = this._bufs[i].length - start
160
168
 
161
- BufferList.prototype.shallowSlice = function shallowSlice (start, end) {
162
- start = start || 0
163
- end = typeof end !== 'number' ? this.length : end
169
+ if (bytes > l) {
170
+ this._bufs[i].copy(target, bufoff, start)
171
+ bufoff += l
172
+ } else {
173
+ this._bufs[i].copy(target, bufoff, start, start + bytes)
174
+ bufoff += l
175
+ break
176
+ }
164
177
 
165
- if (start < 0) {
166
- start += this.length
167
- }
178
+ bytes -= l
168
179
 
169
- if (end < 0) {
170
- end += this.length
171
- }
180
+ if (start) {
181
+ start = 0
182
+ }
183
+ }
184
+
185
+ // safeguard so that we don't return uninitialized memory
186
+ if (target.length > bufoff) return target.slice(0, bufoff)
172
187
 
173
- if (start === end) {
174
- return this._new()
188
+ return target
175
189
  }
176
190
 
177
- const startOffset = this._offset(start)
178
- const endOffset = this._offset(end)
179
- const buffers = this._bufs.slice(startOffset[0], endOffset[0] + 1)
191
+ /**
192
+ * @param {number} [start]
193
+ * @param {number} [end]
194
+ * @returns {BufferList}
195
+ */
196
+ shallowSlice (start, end) {
197
+ start = start || 0
198
+ end = typeof end !== 'number' ? this.length : end
180
199
 
181
- if (endOffset[1] === 0) {
182
- buffers.pop()
183
- } else {
184
- buffers[buffers.length - 1] = buffers[buffers.length - 1].slice(0, endOffset[1])
185
- }
200
+ if (start < 0) {
201
+ start += this.length
202
+ }
186
203
 
187
- if (startOffset[1] !== 0) {
188
- buffers[0] = buffers[0].slice(startOffset[1])
189
- }
204
+ if (end < 0) {
205
+ end += this.length
206
+ }
190
207
 
191
- return this._new(buffers)
192
- }
208
+ if (start === end) {
209
+ return this._new()
210
+ }
193
211
 
194
- BufferList.prototype.toString = function toString (encoding, start, end) {
195
- return this.slice(start, end).toString(encoding)
196
- }
212
+ const startOffset = this._offset(start)
213
+ const endOffset = this._offset(end)
214
+ const buffers = this._bufs.slice(startOffset[0], endOffset[0] + 1)
197
215
 
198
- BufferList.prototype.consume = function consume (bytes) {
199
- // first, normalize the argument, in accordance with how Buffer does it
200
- bytes = Math.trunc(bytes)
201
- // do nothing if not a positive number
202
- if (Number.isNaN(bytes) || bytes <= 0) return this
203
-
204
- while (this._bufs.length) {
205
- if (bytes >= this._bufs[0].length) {
206
- bytes -= this._bufs[0].length
207
- this.length -= this._bufs[0].length
208
- this._bufs.shift()
216
+ if (endOffset[1] === 0) {
217
+ buffers.pop()
209
218
  } else {
210
- this._bufs[0] = this._bufs[0].slice(bytes)
211
- this.length -= bytes
212
- break
219
+ buffers[buffers.length - 1] = buffers[buffers.length - 1].slice(0, endOffset[1])
213
220
  }
214
- }
215
221
 
216
- return this
217
- }
222
+ if (startOffset[1] !== 0) {
223
+ buffers[0] = buffers[0].slice(startOffset[1])
224
+ }
218
225
 
219
- BufferList.prototype.duplicate = function duplicate () {
220
- const copy = this._new()
226
+ return this._new(buffers)
227
+ }
228
+
229
+ /**
230
+ * @param {BufferEncoding} [encoding]
231
+ * @param {number} [start]
232
+ * @param {number} [end]
233
+ * @returns {string}
234
+ */
235
+ toString (encoding, start, end) {
236
+ return this.slice(start, end).toString(encoding)
237
+ }
238
+
239
+ /**
240
+ * @param {number} bytes
241
+ * @returns {this}
242
+ */
243
+ consume (bytes) {
244
+ // first, normalize the argument, in accordance with how Buffer does it
245
+ bytes = Math.trunc(bytes)
246
+ // do nothing if not a positive number
247
+ if (Number.isNaN(bytes) || bytes <= 0) return this
248
+
249
+ while (this._bufs.length) {
250
+ if (bytes >= this._bufs[0].length) {
251
+ bytes -= this._bufs[0].length
252
+ this.length -= this._bufs[0].length
253
+ this._bufs.shift()
254
+ } else {
255
+ this._bufs[0] = this._bufs[0].slice(bytes)
256
+ this.length -= bytes
257
+ break
258
+ }
259
+ }
221
260
 
222
- for (let i = 0; i < this._bufs.length; i++) {
223
- copy.append(this._bufs[i])
261
+ return this
224
262
  }
225
263
 
226
- return copy
227
- }
264
+ /** @returns {BufferList} */
265
+ duplicate () {
266
+ const copy = this._new()
228
267
 
229
- BufferList.prototype.append = function append (buf) {
230
- return this._attach(buf, BufferList.prototype._appendBuffer)
231
- }
268
+ for (let i = 0; i < this._bufs.length; i++) {
269
+ copy.append(this._bufs[i])
270
+ }
232
271
 
233
- BufferList.prototype.prepend = function prepend (buf) {
234
- return this._attach(buf, BufferList.prototype._prependBuffer, true)
235
- }
272
+ return copy
273
+ }
236
274
 
237
- BufferList.prototype._attach = function _attach (buf, attacher, prepend) {
238
- if (buf == null) {
239
- return this
275
+ /**
276
+ * @param {BufferListAcceptedTypes} buf
277
+ * @returns {this}
278
+ */
279
+ append (buf) {
280
+ return this._attach(buf, BufferList.prototype._appendBuffer)
240
281
  }
241
282
 
242
- if (buf.buffer) {
243
- // append/prepend a view of the underlying ArrayBuffer
244
- attacher.call(this, Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength))
245
- } else if (Array.isArray(buf)) {
246
- const [starting, modifier] = prepend ? [buf.length - 1, -1] : [0, 1]
283
+ /**
284
+ * @param {BufferListAcceptedTypes} buf
285
+ * @returns {this}
286
+ */
287
+ prepend (buf) {
288
+ return this._attach(buf, BufferList.prototype._prependBuffer, true)
289
+ }
247
290
 
248
- for (let i = starting; i >= 0 && i < buf.length; i += modifier) {
249
- this._attach(buf[i], attacher, prepend)
291
+ /**
292
+ * @param {BufferListAcceptedTypes} buf
293
+ * @param {(this: BufferList, buf: Buffer) => void} attacher
294
+ * @param {boolean} [prepend]
295
+ * @returns {this}
296
+ */
297
+ _attach (buf, attacher, prepend) {
298
+ if (buf == null) {
299
+ return this
250
300
  }
251
- } else if (this._isBufferList(buf)) {
252
- // unwrap argument into individual BufferLists
253
- const [starting, modifier] = prepend ? [buf._bufs.length - 1, -1] : [0, 1]
254
301
 
255
- for (let i = starting; i >= 0 && i < buf._bufs.length; i += modifier) {
256
- this._attach(buf._bufs[i], attacher, prepend)
257
- }
258
- } else {
259
- // coerce number arguments to strings, since Buffer(number) does
260
- // uninitialized memory allocation
261
- if (typeof buf === 'number') {
262
- buf = buf.toString()
263
- }
302
+ if (typeof buf === 'object' && 'buffer' in buf) {
303
+ // append/prepend a view of the underlying ArrayBuffer
304
+ const view = /** @type {Uint8Array} */ (buf)
305
+ attacher.call(this, Buffer.from(view.buffer, view.byteOffset, view.byteLength))
306
+ } else if (Array.isArray(buf)) {
307
+ const [starting, modifier] = prepend ? [buf.length - 1, -1] : [0, 1]
264
308
 
265
- attacher.call(this, Buffer.from(buf))
266
- }
309
+ for (let i = starting; i >= 0 && i < buf.length; i += modifier) {
310
+ this._attach(buf[i], attacher, prepend)
311
+ }
312
+ } else if (this._isBufferList(buf)) {
313
+ // unwrap argument into individual BufferLists
314
+ const [starting, modifier] = prepend ? [buf._bufs.length - 1, -1] : [0, 1]
267
315
 
268
- return this
269
- }
316
+ for (let i = starting; i >= 0 && i < buf._bufs.length; i += modifier) {
317
+ this._attach(buf._bufs[i], attacher, prepend)
318
+ }
319
+ } else {
320
+ // coerce number arguments to strings, since Buffer(number) does
321
+ // uninitialized memory allocation
322
+ if (typeof buf === 'number') {
323
+ buf = buf.toString()
324
+ }
270
325
 
271
- BufferList.prototype._appendBuffer = function appendBuffer (buf) {
272
- this._bufs.push(buf)
273
- this.length += buf.length
274
- }
326
+ attacher.call(this, Buffer.from(/** @type {string} */ (buf)))
327
+ }
275
328
 
276
- BufferList.prototype._prependBuffer = function prependBuffer (buf) {
277
- this._bufs.unshift(buf)
278
- this.length += buf.length
279
- }
329
+ return this
330
+ }
280
331
 
281
- BufferList.prototype.indexOf = function (search, offset, encoding) {
282
- if (encoding === undefined && typeof offset === 'string') {
283
- encoding = offset
284
- offset = undefined
332
+ /** @param {Buffer} buf */
333
+ _appendBuffer (buf) {
334
+ this._bufs.push(buf)
335
+ this.length += buf.length
285
336
  }
286
337
 
287
- if (typeof search === 'function' || Array.isArray(search)) {
288
- throw new TypeError('The "value" argument must be one of type string, Buffer, BufferList, or Uint8Array.')
289
- } else if (typeof search === 'number') {
290
- search = Buffer.from([search])
291
- } else if (typeof search === 'string') {
292
- search = Buffer.from(search, encoding)
293
- } else if (this._isBufferList(search)) {
294
- search = search.slice()
295
- } else if (Array.isArray(search.buffer)) {
296
- search = Buffer.from(search.buffer, search.byteOffset, search.byteLength)
297
- } else if (!Buffer.isBuffer(search)) {
298
- search = Buffer.from(search)
338
+ /** @param {Buffer} buf */
339
+ _prependBuffer (buf) {
340
+ this._bufs.unshift(buf)
341
+ this.length += buf.length
299
342
  }
300
343
 
301
- offset = Number(offset || 0)
344
+ /**
345
+ * @param {string | number | Buffer | BufferList | Uint8Array} search
346
+ * @param {number | string} [offset]
347
+ * @param {BufferEncoding} [encoding]
348
+ * @returns {number}
349
+ */
350
+ indexOf (search, offset, encoding) {
351
+ if (encoding === undefined && typeof offset === 'string') {
352
+ encoding = /** @type {BufferEncoding} */ (offset)
353
+ offset = undefined
354
+ }
302
355
 
303
- if (isNaN(offset)) {
304
- offset = 0
305
- }
356
+ if (typeof search === 'function' || Array.isArray(search)) {
357
+ throw new TypeError('The "value" argument must be one of type string, Buffer, BufferList, or Uint8Array.')
358
+ } else if (typeof search === 'number') {
359
+ search = Buffer.from([search])
360
+ } else if (typeof search === 'string') {
361
+ search = Buffer.from(search, encoding)
362
+ } else if (this._isBufferList(search)) {
363
+ search = search.slice()
364
+ } else if (Array.isArray(/** @type {any} */ (search).buffer)) {
365
+ search = Buffer.from(/** @type {any} */ (search).buffer, /** @type {any} */ (search).byteOffset, /** @type {any} */ (search).byteLength)
366
+ } else if (!Buffer.isBuffer(search)) {
367
+ search = Buffer.from(/** @type {any} */ (search))
368
+ }
306
369
 
307
- if (offset < 0) {
308
- offset = this.length + offset
309
- }
370
+ offset = Number(offset || 0)
310
371
 
311
- if (offset < 0) {
312
- offset = 0
313
- }
372
+ if (isNaN(offset)) {
373
+ offset = 0
374
+ }
314
375
 
315
- if (search.length === 0) {
316
- return offset > this.length ? this.length : offset
317
- }
376
+ if (offset < 0) {
377
+ offset = this.length + offset
378
+ }
318
379
 
319
- const blOffset = this._offset(offset)
320
- let blIndex = blOffset[0] // index of which internal buffer we're working on
321
- let buffOffset = blOffset[1] // offset of the internal buffer we're working on
380
+ if (offset < 0) {
381
+ offset = 0
382
+ }
322
383
 
323
- // scan over each buffer
324
- for (; blIndex < this._bufs.length; blIndex++) {
325
- const buff = this._bufs[blIndex]
384
+ if (search.length === 0) {
385
+ return offset > this.length ? this.length : offset
386
+ }
326
387
 
327
- while (buffOffset < buff.length) {
328
- const availableWindow = buff.length - buffOffset
388
+ const blOffset = this._offset(offset)
389
+ let blIndex = blOffset[0] // index of which internal buffer we're working on
390
+ let buffOffset = blOffset[1] // offset of the internal buffer we're working on
329
391
 
330
- if (availableWindow >= search.length) {
331
- const nativeSearchResult = buff.indexOf(search, buffOffset)
392
+ // scan over each buffer
393
+ for (; blIndex < this._bufs.length; blIndex++) {
394
+ const buff = this._bufs[blIndex]
332
395
 
333
- if (nativeSearchResult !== -1) {
334
- return this._reverseOffset([blIndex, nativeSearchResult])
335
- }
396
+ while (buffOffset < buff.length) {
397
+ const availableWindow = buff.length - buffOffset
336
398
 
337
- buffOffset = buff.length - search.length + 1 // end of native search window
338
- } else {
339
- const revOffset = this._reverseOffset([blIndex, buffOffset])
399
+ if (availableWindow >= search.length) {
400
+ const nativeSearchResult = buff.indexOf(/** @type {any} */ (search), buffOffset)
340
401
 
341
- if (this._match(revOffset, search)) {
342
- return revOffset
343
- }
402
+ if (nativeSearchResult !== -1) {
403
+ return this._reverseOffset([blIndex, nativeSearchResult])
404
+ }
344
405
 
345
- buffOffset++
346
- }
347
- }
406
+ buffOffset = buff.length - search.length + 1 // end of native search window
407
+ } else {
408
+ const revOffset = this._reverseOffset([blIndex, buffOffset])
348
409
 
349
- buffOffset = 0
350
- }
410
+ if (this._match(revOffset, search)) {
411
+ return revOffset
412
+ }
351
413
 
352
- return -1
353
- }
414
+ buffOffset++
415
+ }
416
+ }
354
417
 
355
- BufferList.prototype._match = function (offset, search) {
356
- if (this.length - offset < search.length) {
357
- return false
418
+ buffOffset = 0
419
+ }
420
+
421
+ return -1
358
422
  }
359
423
 
360
- for (let searchOffset = 0; searchOffset < search.length; searchOffset++) {
361
- if (this.get(offset + searchOffset) !== search[searchOffset]) {
424
+ /**
425
+ * @param {number} offset
426
+ * @param {Buffer | Uint8Array} search
427
+ * @returns {boolean}
428
+ */
429
+ _match (offset, search) {
430
+ if (this.length - offset < search.length) {
362
431
  return false
363
432
  }
433
+
434
+ for (let searchOffset = 0; searchOffset < search.length; searchOffset++) {
435
+ if (this.get(offset + searchOffset) !== search[searchOffset]) {
436
+ return false
437
+ }
438
+ }
439
+ return true
364
440
  }
365
- return true
366
- }
367
441
 
368
- ;(function () {
369
- const methods = {
370
- readDoubleBE: 8,
371
- readDoubleLE: 8,
372
- readFloatBE: 4,
373
- readFloatLE: 4,
374
- readBigInt64BE: 8,
375
- readBigInt64LE: 8,
376
- readBigUInt64BE: 8,
377
- readBigUInt64LE: 8,
378
- readInt32BE: 4,
379
- readInt32LE: 4,
380
- readUInt32BE: 4,
381
- readUInt32LE: 4,
382
- readInt16BE: 2,
383
- readInt16LE: 2,
384
- readUInt16BE: 2,
385
- readUInt16LE: 2,
386
- readInt8: 1,
387
- readUInt8: 1,
388
- readIntBE: null,
389
- readIntLE: null,
390
- readUIntBE: null,
391
- readUIntLE: null
442
+ // Used internally by the class and also as an indicator of this object being
443
+ // a `BufferList`. It's not possible to use `instanceof BufferList` in a browser
444
+ // environment because there could be multiple different copies of the
445
+ // BufferList class and some `BufferList`s might be `BufferList`s.
446
+ /**
447
+ * @param {any} b
448
+ * @returns {b is BufferList}
449
+ */
450
+ _isBufferList (b) {
451
+ return b instanceof BufferList || BufferList.isBufferList(b)
392
452
  }
393
453
 
394
- for (const m in methods) {
395
- (function (m) {
396
- if (methods[m] === null) {
397
- BufferList.prototype[m] = function (offset, byteLength) {
398
- return this.slice(offset, offset + byteLength)[m](0, byteLength)
399
- }
400
- } else {
401
- BufferList.prototype[m] = function (offset = 0) {
402
- return this.slice(offset, offset + methods[m])[m](0)
403
- }
404
- }
405
- }(m))
454
+ /**
455
+ * @param {any} b
456
+ * @returns {boolean}
457
+ */
458
+ static isBufferList (b) {
459
+ return b != null && b[symbol]
406
460
  }
407
- }())
408
-
409
- // Used internally by the class and also as an indicator of this object being
410
- // a `BufferList`. It's not possible to use `instanceof BufferList` in a browser
411
- // environment because there could be multiple different copies of the
412
- // BufferList class and some `BufferList`s might be `BufferList`s.
413
- BufferList.prototype._isBufferList = function _isBufferList (b) {
414
- return b instanceof BufferList || BufferList.isBufferList(b)
415
461
  }
416
462
 
417
- BufferList.isBufferList = function isBufferList (b) {
418
- return b != null && b[symbol]
463
+ // Add all the Buffer read methods
464
+ /** @type {Record<string, number | null>} */
465
+ const methods = {
466
+ readDoubleBE: 8,
467
+ readDoubleLE: 8,
468
+ readFloatBE: 4,
469
+ readFloatLE: 4,
470
+ readBigInt64BE: 8,
471
+ readBigInt64LE: 8,
472
+ readBigUInt64BE: 8,
473
+ readBigUInt64LE: 8,
474
+ readInt32BE: 4,
475
+ readInt32LE: 4,
476
+ readUInt32BE: 4,
477
+ readUInt32LE: 4,
478
+ readInt16BE: 2,
479
+ readInt16LE: 2,
480
+ readUInt16BE: 2,
481
+ readUInt16LE: 2,
482
+ readInt8: 1,
483
+ readUInt8: 1,
484
+ readIntBE: null,
485
+ readIntLE: null,
486
+ readUIntBE: null,
487
+ readUIntLE: null
488
+ }
489
+
490
+ for (const m in methods) {
491
+ const size = methods[m]
492
+ if (size === null) {
493
+ /** @type {any} */ (BufferList.prototype)[m] = function (/** @type {number} */ offset, /** @type {number} */ byteLength) {
494
+ return /** @type {any} */ (this.slice(offset, offset + byteLength))[m](0, byteLength)
495
+ }
496
+ } else {
497
+ /** @type {any} */ (BufferList.prototype)[m] = function (/** @type {number} */ offset = 0) {
498
+ return /** @type {any} */ (this.slice(offset, offset + size))[m](0)
499
+ }
500
+ }
419
501
  }
420
502
 
421
- module.exports = BufferList
503
+ export default BufferList