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