sliftutils 0.1.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.
Files changed (47) hide show
  1. package/.cursorrules +161 -0
  2. package/.eslintrc.js +38 -0
  3. package/.vscode/settings.json +39 -0
  4. package/bundler/buffer.js +2370 -0
  5. package/bundler/bundleEntry.ts +32 -0
  6. package/bundler/bundleEntryCaller.ts +8 -0
  7. package/bundler/bundleRequire.ts +244 -0
  8. package/bundler/bundleWrapper.ts +115 -0
  9. package/bundler/bundler.ts +72 -0
  10. package/bundler/flattenSourceMaps.ts +0 -0
  11. package/bundler/sourceMaps.ts +261 -0
  12. package/misc/environment.ts +11 -0
  13. package/misc/types.ts +3 -0
  14. package/misc/zip.ts +37 -0
  15. package/package.json +24 -0
  16. package/spec.txt +33 -0
  17. package/storage/CachedStorage.ts +32 -0
  18. package/storage/DelayedStorage.ts +30 -0
  19. package/storage/DiskCollection.ts +272 -0
  20. package/storage/FileFolderAPI.tsx +427 -0
  21. package/storage/IStorage.ts +40 -0
  22. package/storage/IndexedDBFileFolderAPI.ts +170 -0
  23. package/storage/JSONStorage.ts +35 -0
  24. package/storage/PendingManager.tsx +63 -0
  25. package/storage/PendingStorage.ts +47 -0
  26. package/storage/PrivateFileSystemStorage.ts +192 -0
  27. package/storage/StorageObservable.ts +122 -0
  28. package/storage/TransactionStorage.ts +485 -0
  29. package/storage/fileSystemPointer.ts +81 -0
  30. package/storage/storage.d.ts +41 -0
  31. package/tsconfig.json +31 -0
  32. package/web/DropdownCustom.tsx +150 -0
  33. package/web/FullscreenModal.tsx +75 -0
  34. package/web/GenericFormat.tsx +186 -0
  35. package/web/Input.tsx +350 -0
  36. package/web/InputLabel.tsx +288 -0
  37. package/web/InputPicker.tsx +158 -0
  38. package/web/LocalStorageParam.ts +56 -0
  39. package/web/SyncedController.ts +405 -0
  40. package/web/SyncedLoadingIndicator.tsx +37 -0
  41. package/web/Table.tsx +188 -0
  42. package/web/URLParam.ts +84 -0
  43. package/web/asyncObservable.ts +40 -0
  44. package/web/colors.tsx +14 -0
  45. package/web/mobxTyped.ts +29 -0
  46. package/web/modal.tsx +18 -0
  47. package/web/observer.tsx +35 -0
@@ -0,0 +1,2370 @@
1
+ /*!
2
+ * The buffer module from node.js, for the browser.
3
+ *
4
+ * @author Feross Aboukhadijeh <https://feross.org>
5
+ * @license MIT
6
+ */
7
+ /* eslint-disable no-proto */
8
+ (function main() {
9
+ 'use strict'
10
+
11
+ const base64 = (function () {
12
+ let exports = {};
13
+
14
+ exports.byteLength = byteLength
15
+ exports.toByteArray = toByteArray
16
+ exports.fromByteArray = fromByteArray
17
+
18
+ var lookup = []
19
+ var revLookup = []
20
+ var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array
21
+
22
+ var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
23
+ for (var i = 0, len = code.length; i < len; ++i) {
24
+ lookup[i] = code[i]
25
+ revLookup[code.charCodeAt(i)] = i
26
+ }
27
+
28
+ // Support decoding URL-safe base64 strings, as Node.js does.
29
+ // See: https://en.wikipedia.org/wiki/Base64#URL_applications
30
+ revLookup['-'.charCodeAt(0)] = 62
31
+ revLookup['_'.charCodeAt(0)] = 63
32
+
33
+ function getLens(b64) {
34
+ var len = b64.length
35
+
36
+ if (len % 4 > 0) {
37
+ throw new Error('Invalid string. Length must be a multiple of 4')
38
+ }
39
+
40
+ // Trim off extra bytes after placeholder bytes are found
41
+ // See: https://github.com/beatgammit/base64-js/issues/42
42
+ var validLen = b64.indexOf('=')
43
+ if (validLen === -1) validLen = len
44
+
45
+ var placeHoldersLen = validLen === len
46
+ ? 0
47
+ : 4 - (validLen % 4)
48
+
49
+ return [validLen, placeHoldersLen]
50
+ }
51
+
52
+ // base64 is 4/3 + up to two characters of the original data
53
+ function byteLength(b64) {
54
+ var lens = getLens(b64)
55
+ var validLen = lens[0]
56
+ var placeHoldersLen = lens[1]
57
+ return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen
58
+ }
59
+
60
+ function _byteLength(b64, validLen, placeHoldersLen) {
61
+ return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen
62
+ }
63
+
64
+ function toByteArray(b64) {
65
+ var tmp
66
+ var lens = getLens(b64)
67
+ var validLen = lens[0]
68
+ var placeHoldersLen = lens[1]
69
+
70
+ var arr = new Arr(_byteLength(b64, validLen, placeHoldersLen))
71
+
72
+ var curByte = 0
73
+
74
+ // if there are placeholders, only get up to the last complete 4 chars
75
+ var len = placeHoldersLen > 0
76
+ ? validLen - 4
77
+ : validLen
78
+
79
+ var i
80
+ for (i = 0; i < len; i += 4) {
81
+ tmp =
82
+ (revLookup[b64.charCodeAt(i)] << 18) |
83
+ (revLookup[b64.charCodeAt(i + 1)] << 12) |
84
+ (revLookup[b64.charCodeAt(i + 2)] << 6) |
85
+ revLookup[b64.charCodeAt(i + 3)]
86
+ arr[curByte++] = (tmp >> 16) & 0xFF
87
+ arr[curByte++] = (tmp >> 8) & 0xFF
88
+ arr[curByte++] = tmp & 0xFF
89
+ }
90
+
91
+ if (placeHoldersLen === 2) {
92
+ tmp =
93
+ (revLookup[b64.charCodeAt(i)] << 2) |
94
+ (revLookup[b64.charCodeAt(i + 1)] >> 4)
95
+ arr[curByte++] = tmp & 0xFF
96
+ }
97
+
98
+ if (placeHoldersLen === 1) {
99
+ tmp =
100
+ (revLookup[b64.charCodeAt(i)] << 10) |
101
+ (revLookup[b64.charCodeAt(i + 1)] << 4) |
102
+ (revLookup[b64.charCodeAt(i + 2)] >> 2)
103
+ arr[curByte++] = (tmp >> 8) & 0xFF
104
+ arr[curByte++] = tmp & 0xFF
105
+ }
106
+
107
+ return arr
108
+ }
109
+
110
+ function tripletToBase64(num) {
111
+ return lookup[num >> 18 & 0x3F] +
112
+ lookup[num >> 12 & 0x3F] +
113
+ lookup[num >> 6 & 0x3F] +
114
+ lookup[num & 0x3F]
115
+ }
116
+
117
+ function encodeChunk(uint8, start, end) {
118
+ var tmp
119
+ var output = []
120
+ for (var i = start; i < end; i += 3) {
121
+ tmp =
122
+ ((uint8[i] << 16) & 0xFF0000) +
123
+ ((uint8[i + 1] << 8) & 0xFF00) +
124
+ (uint8[i + 2] & 0xFF)
125
+ output.push(tripletToBase64(tmp))
126
+ }
127
+ return output.join('')
128
+ }
129
+
130
+ function fromByteArray(uint8) {
131
+ var tmp
132
+ var len = uint8.length
133
+ var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes
134
+ var parts = []
135
+ var maxChunkLength = 16383 // must be multiple of 3
136
+
137
+ // go through the array every three bytes, we'll deal with trailing stuff later
138
+ for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
139
+ parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength)))
140
+ }
141
+
142
+ // pad the end with zeros, but make sure to not forget the extra bytes
143
+ if (extraBytes === 1) {
144
+ tmp = uint8[len - 1]
145
+ parts.push(
146
+ lookup[tmp >> 2] +
147
+ lookup[(tmp << 4) & 0x3F] +
148
+ '=='
149
+ )
150
+ } else if (extraBytes === 2) {
151
+ tmp = (uint8[len - 2] << 8) + uint8[len - 1]
152
+ parts.push(
153
+ lookup[tmp >> 10] +
154
+ lookup[(tmp >> 4) & 0x3F] +
155
+ lookup[(tmp << 2) & 0x3F] +
156
+ '='
157
+ )
158
+ }
159
+
160
+ return parts.join('')
161
+ }
162
+
163
+ return exports;
164
+ })();
165
+ const ieee754 = (function () {
166
+ let exports = {};
167
+ /*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */
168
+ exports.read = function (buffer, offset, isLE, mLen, nBytes) {
169
+ let e, m
170
+ const eLen = (nBytes * 8) - mLen - 1
171
+ const eMax = (1 << eLen) - 1
172
+ const eBias = eMax >> 1
173
+ let nBits = -7
174
+ let i = isLE ? (nBytes - 1) : 0
175
+ const d = isLE ? -1 : 1
176
+ let s = buffer[offset + i]
177
+
178
+ i += d
179
+
180
+ e = s & ((1 << (-nBits)) - 1)
181
+ s >>= (-nBits)
182
+ nBits += eLen
183
+ while (nBits > 0) {
184
+ e = (e * 256) + buffer[offset + i]
185
+ i += d
186
+ nBits -= 8
187
+ }
188
+
189
+ m = e & ((1 << (-nBits)) - 1)
190
+ e >>= (-nBits)
191
+ nBits += mLen
192
+ while (nBits > 0) {
193
+ m = (m * 256) + buffer[offset + i]
194
+ i += d
195
+ nBits -= 8
196
+ }
197
+
198
+ if (e === 0) {
199
+ e = 1 - eBias
200
+ } else if (e === eMax) {
201
+ return m ? NaN : ((s ? -1 : 1) * Infinity)
202
+ } else {
203
+ m = m + Math.pow(2, mLen)
204
+ e = e - eBias
205
+ }
206
+ return (s ? -1 : 1) * m * Math.pow(2, e - mLen)
207
+ }
208
+
209
+ exports.write = function (buffer, value, offset, isLE, mLen, nBytes) {
210
+ let e, m, c
211
+ let eLen = (nBytes * 8) - mLen - 1
212
+ const eMax = (1 << eLen) - 1
213
+ const eBias = eMax >> 1
214
+ const rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0)
215
+ let i = isLE ? 0 : (nBytes - 1)
216
+ const d = isLE ? 1 : -1
217
+ const s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0
218
+
219
+ value = Math.abs(value)
220
+
221
+ if (isNaN(value) || value === Infinity) {
222
+ m = isNaN(value) ? 1 : 0
223
+ e = eMax
224
+ } else {
225
+ e = Math.floor(Math.log(value) / Math.LN2)
226
+ if (value * (c = Math.pow(2, -e)) < 1) {
227
+ e--
228
+ c *= 2
229
+ }
230
+ if (e + eBias >= 1) {
231
+ value += rt / c
232
+ } else {
233
+ value += rt * Math.pow(2, 1 - eBias)
234
+ }
235
+ if (value * c >= 2) {
236
+ e++
237
+ c /= 2
238
+ }
239
+
240
+ if (e + eBias >= eMax) {
241
+ m = 0
242
+ e = eMax
243
+ } else if (e + eBias >= 1) {
244
+ m = ((value * c) - 1) * Math.pow(2, mLen)
245
+ e = e + eBias
246
+ } else {
247
+ m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen)
248
+ e = 0
249
+ }
250
+ }
251
+
252
+ while (mLen >= 8) {
253
+ buffer[offset + i] = m & 0xff
254
+ i += d
255
+ m /= 256
256
+ mLen -= 8
257
+ }
258
+
259
+ e = (e << mLen) | m
260
+ eLen += mLen
261
+ while (eLen > 0) {
262
+ buffer[offset + i] = e & 0xff
263
+ i += d
264
+ e /= 256
265
+ eLen -= 8
266
+ }
267
+
268
+ buffer[offset + i - d] |= s * 128
269
+ }
270
+ return exports;
271
+ })();
272
+ const customInspectSymbol =
273
+ (typeof Symbol === 'function' && typeof Symbol['for'] === 'function') // eslint-disable-line dot-notation
274
+ ? Symbol['for']('nodejs.util.inspect.custom') // eslint-disable-line dot-notation
275
+ : null
276
+
277
+ let exports = globalThis;
278
+
279
+ exports.Buffer = Buffer
280
+ exports.SlowBuffer = SlowBuffer
281
+ exports.INSPECT_MAX_BYTES = 50
282
+
283
+ const K_MAX_LENGTH = 0x7fffffff
284
+ exports.kMaxLength = K_MAX_LENGTH
285
+
286
+ /**
287
+ * If `Buffer.TYPED_ARRAY_SUPPORT`:
288
+ * === true Use Uint8Array implementation (fastest)
289
+ * === false Print warning and recommend using `buffer` v4.x which has an Object
290
+ * implementation (most compatible, even IE6)
291
+ *
292
+ * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+,
293
+ * Opera 11.6+, iOS 4.2+.
294
+ *
295
+ * We report that the browser does not support typed arrays if the are not subclassable
296
+ * using __proto__. Firefox 4-29 lacks support for adding new properties to `Uint8Array`
297
+ * (See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438). IE 10 lacks support
298
+ * for __proto__ and has a buggy typed array implementation.
299
+ */
300
+ Buffer.TYPED_ARRAY_SUPPORT = typedArraySupport()
301
+
302
+ if (!Buffer.TYPED_ARRAY_SUPPORT && typeof console !== 'undefined' &&
303
+ typeof console.error === 'function') {
304
+ console.error(
305
+ 'This browser lacks typed array (Uint8Array) support which is required by ' +
306
+ '`buffer` v5.x. Use `buffer` v4.x if you require old browser support.'
307
+ )
308
+ }
309
+
310
+ function typedArraySupport() {
311
+ // Can typed array instances can be augmented?
312
+ try {
313
+ const arr = new Uint8Array(1)
314
+ const proto = { foo: function () { return 42 } }
315
+ Object.setPrototypeOf(proto, Uint8Array.prototype)
316
+ Object.setPrototypeOf(arr, proto)
317
+ return arr.foo() === 42
318
+ } catch (e) {
319
+ return false
320
+ }
321
+ }
322
+
323
+ Object.defineProperty(Buffer.prototype, 'parent', {
324
+ enumerable: true,
325
+ get: function () {
326
+ if (!Buffer.isBuffer(this)) return undefined
327
+ return this.buffer
328
+ }
329
+ })
330
+
331
+ Object.defineProperty(Buffer.prototype, 'offset', {
332
+ enumerable: true,
333
+ get: function () {
334
+ if (!Buffer.isBuffer(this)) return undefined
335
+ return this.byteOffset
336
+ }
337
+ })
338
+
339
+ function createBuffer(length) {
340
+ if (length > K_MAX_LENGTH) {
341
+ throw new RangeError('The value "' + length + '" is invalid for option "size"')
342
+ }
343
+ // Return an augmented `Uint8Array` instance
344
+ const buf = new Uint8Array(length)
345
+ Object.setPrototypeOf(buf, Buffer.prototype)
346
+ return buf
347
+ }
348
+
349
+ /**
350
+ * The Buffer constructor returns instances of `Uint8Array` that have their
351
+ * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of
352
+ * `Uint8Array`, so the returned instances will have all the node `Buffer` methods
353
+ * and the `Uint8Array` methods. Square bracket notation works as expected -- it
354
+ * returns a single octet.
355
+ *
356
+ * The `Uint8Array` prototype remains unmodified.
357
+ */
358
+
359
+ function Buffer(arg, encodingOrOffset, length) {
360
+ // Common case.
361
+ if (typeof arg === 'number') {
362
+ if (typeof encodingOrOffset === 'string') {
363
+ throw new TypeError(
364
+ 'The "string" argument must be of type string. Received type number'
365
+ )
366
+ }
367
+ return allocUnsafe(arg)
368
+ }
369
+ return from(arg, encodingOrOffset, length)
370
+ }
371
+
372
+ Buffer.poolSize = 8192 // not used by this implementation
373
+
374
+ function from(value, encodingOrOffset, length) {
375
+ if (typeof value === 'string') {
376
+ return fromString(value, encodingOrOffset)
377
+ }
378
+
379
+ if (ArrayBuffer.isView(value)) {
380
+ return fromArrayView(value)
381
+ }
382
+
383
+ if (value == null) {
384
+ throw new TypeError(
385
+ 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' +
386
+ 'or Array-like Object. Received type ' + (typeof value)
387
+ )
388
+ }
389
+
390
+ if (isInstance(value, ArrayBuffer) ||
391
+ (value && isInstance(value.buffer, ArrayBuffer))) {
392
+ return fromArrayBuffer(value, encodingOrOffset, length)
393
+ }
394
+
395
+ if (typeof SharedArrayBuffer !== 'undefined' &&
396
+ (isInstance(value, SharedArrayBuffer) ||
397
+ (value && isInstance(value.buffer, SharedArrayBuffer)))) {
398
+ return fromArrayBuffer(value, encodingOrOffset, length)
399
+ }
400
+
401
+ if (typeof value === 'number') {
402
+ throw new TypeError(
403
+ 'The "value" argument must not be of type number. Received type number'
404
+ )
405
+ }
406
+
407
+ const valueOf = value.valueOf && value.valueOf()
408
+ if (valueOf != null && valueOf !== value) {
409
+ return Buffer.from(valueOf, encodingOrOffset, length)
410
+ }
411
+
412
+ const b = fromObject(value)
413
+ if (b) return b
414
+
415
+ if (typeof Symbol !== 'undefined' && Symbol.toPrimitive != null &&
416
+ typeof value[Symbol.toPrimitive] === 'function') {
417
+ return Buffer.from(value[Symbol.toPrimitive]('string'), encodingOrOffset, length)
418
+ }
419
+
420
+ throw new TypeError(
421
+ 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' +
422
+ 'or Array-like Object. Received type ' + (typeof value)
423
+ )
424
+ }
425
+
426
+ /**
427
+ * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError
428
+ * if value is a number.
429
+ * Buffer.from(str[, encoding])
430
+ * Buffer.from(array)
431
+ * Buffer.from(buffer)
432
+ * Buffer.from(arrayBuffer[, byteOffset[, length]])
433
+ **/
434
+ Buffer.from = function (value, encodingOrOffset, length) {
435
+ return from(value, encodingOrOffset, length)
436
+ }
437
+
438
+ // Note: Change prototype *after* Buffer.from is defined to workaround Chrome bug:
439
+ // https://github.com/feross/buffer/pull/148
440
+ Object.setPrototypeOf(Buffer.prototype, Uint8Array.prototype)
441
+ Object.setPrototypeOf(Buffer, Uint8Array)
442
+
443
+ function assertSize(size) {
444
+ if (typeof size !== 'number') {
445
+ throw new TypeError('"size" argument must be of type number')
446
+ } else if (size < 0) {
447
+ throw new RangeError('The value "' + size + '" is invalid for option "size"')
448
+ }
449
+ }
450
+
451
+ function alloc(size, fill, encoding) {
452
+ assertSize(size)
453
+ if (size <= 0) {
454
+ return createBuffer(size)
455
+ }
456
+ if (fill !== undefined) {
457
+ // Only pay attention to encoding if it's a string. This
458
+ // prevents accidentally sending in a number that would
459
+ // be interpreted as a start offset.
460
+ return typeof encoding === 'string'
461
+ ? createBuffer(size).fill(fill, encoding)
462
+ : createBuffer(size).fill(fill)
463
+ }
464
+ return createBuffer(size)
465
+ }
466
+
467
+ /**
468
+ * Creates a new filled Buffer instance.
469
+ * alloc(size[, fill[, encoding]])
470
+ **/
471
+ Buffer.alloc = function (size, fill, encoding) {
472
+ return alloc(size, fill, encoding)
473
+ }
474
+
475
+ function allocUnsafe(size) {
476
+ assertSize(size)
477
+ return createBuffer(size < 0 ? 0 : checked(size) | 0)
478
+ }
479
+
480
+ /**
481
+ * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance.
482
+ * */
483
+ Buffer.allocUnsafe = function (size) {
484
+ return allocUnsafe(size)
485
+ }
486
+ /**
487
+ * Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance.
488
+ */
489
+ Buffer.allocUnsafeSlow = function (size) {
490
+ return allocUnsafe(size)
491
+ }
492
+
493
+ function fromString(string, encoding) {
494
+ if (typeof encoding !== 'string' || encoding === '') {
495
+ encoding = 'utf8'
496
+ }
497
+
498
+ if (!Buffer.isEncoding(encoding)) {
499
+ throw new TypeError('Unknown encoding: ' + encoding)
500
+ }
501
+
502
+ const length = byteLength(string, encoding) | 0
503
+ let buf = createBuffer(length)
504
+
505
+ const actual = buf.write(string, encoding)
506
+
507
+ if (actual !== length) {
508
+ // Writing a hex string, for example, that contains invalid characters will
509
+ // cause everything after the first invalid character to be ignored. (e.g.
510
+ // 'abxxcd' will be treated as 'ab')
511
+ buf = buf.slice(0, actual)
512
+ }
513
+
514
+ return buf
515
+ }
516
+
517
+ function fromArrayLike(array) {
518
+ const length = array.length < 0 ? 0 : checked(array.length) | 0
519
+ const buf = createBuffer(length)
520
+ for (let i = 0; i < length; i += 1) {
521
+ buf[i] = array[i] & 255
522
+ }
523
+ return buf
524
+ }
525
+
526
+ function fromArrayView(arrayView) {
527
+ if (isInstance(arrayView, Uint8Array)) {
528
+ const copy = new Uint8Array(arrayView)
529
+ return fromArrayBuffer(copy.buffer, copy.byteOffset, copy.byteLength)
530
+ }
531
+ return fromArrayLike(arrayView)
532
+ }
533
+
534
+ function fromArrayBuffer(array, byteOffset, length) {
535
+ if (byteOffset < 0 || array.byteLength < byteOffset) {
536
+ throw new RangeError('"offset" is outside of buffer bounds')
537
+ }
538
+
539
+ if (array.byteLength < byteOffset + (length || 0)) {
540
+ throw new RangeError('"length" is outside of buffer bounds')
541
+ }
542
+
543
+ let buf
544
+ if (byteOffset === undefined && length === undefined) {
545
+ buf = new Uint8Array(array)
546
+ } else if (length === undefined) {
547
+ buf = new Uint8Array(array, byteOffset)
548
+ } else {
549
+ buf = new Uint8Array(array, byteOffset, length)
550
+ }
551
+
552
+ // Return an augmented `Uint8Array` instance
553
+ Object.setPrototypeOf(buf, Buffer.prototype)
554
+
555
+ return buf
556
+ }
557
+
558
+ function fromObject(obj) {
559
+ if (Buffer.isBuffer(obj)) {
560
+ const len = checked(obj.length) | 0
561
+ const buf = createBuffer(len)
562
+
563
+ if (buf.length === 0) {
564
+ return buf
565
+ }
566
+
567
+ obj.copy(buf, 0, 0, len)
568
+ return buf
569
+ }
570
+
571
+ if (obj.length !== undefined) {
572
+ if (typeof obj.length !== 'number' || numberIsNaN(obj.length)) {
573
+ return createBuffer(0)
574
+ }
575
+ return fromArrayLike(obj)
576
+ }
577
+
578
+ if (obj.type === 'Buffer' && Array.isArray(obj.data)) {
579
+ return fromArrayLike(obj.data)
580
+ }
581
+ }
582
+
583
+ function checked(length) {
584
+ // Note: cannot use `length < K_MAX_LENGTH` here because that fails when
585
+ // length is NaN (which is otherwise coerced to zero.)
586
+ if (length >= K_MAX_LENGTH) {
587
+ throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
588
+ 'size: 0x' + K_MAX_LENGTH.toString(16) + ' bytes')
589
+ }
590
+ return length | 0
591
+ }
592
+
593
+ function SlowBuffer(length) {
594
+ if (+length != length) { // eslint-disable-line eqeqeq
595
+ length = 0
596
+ }
597
+ return Buffer.alloc(+length)
598
+ }
599
+
600
+ Buffer.isBuffer = function isBuffer(b) {
601
+ return b != null && b._isBuffer === true &&
602
+ b !== Buffer.prototype // so Buffer.isBuffer(Buffer.prototype) will be false
603
+ }
604
+
605
+ Buffer.compare = function compare(a, b) {
606
+ if (isInstance(a, Uint8Array)) a = Buffer.from(a, a.offset, a.byteLength)
607
+ if (isInstance(b, Uint8Array)) b = Buffer.from(b, b.offset, b.byteLength)
608
+ if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) {
609
+ throw new TypeError(
610
+ 'The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array'
611
+ )
612
+ }
613
+
614
+ if (a === b) return 0
615
+
616
+ let x = a.length
617
+ let y = b.length
618
+
619
+ for (let i = 0, len = Math.min(x, y); i < len; ++i) {
620
+ if (a[i] !== b[i]) {
621
+ x = a[i]
622
+ y = b[i]
623
+ break
624
+ }
625
+ }
626
+
627
+ if (x < y) return -1
628
+ if (y < x) return 1
629
+ return 0
630
+ }
631
+
632
+ Buffer.isEncoding = function isEncoding(encoding) {
633
+ switch (String(encoding).toLowerCase()) {
634
+ case 'hex':
635
+ case 'utf8':
636
+ case 'utf-8':
637
+ case 'ascii':
638
+ case 'latin1':
639
+ case 'binary':
640
+ case 'base64':
641
+ case 'ucs2':
642
+ case 'ucs-2':
643
+ case 'utf16le':
644
+ case 'utf-16le':
645
+ return true
646
+ default:
647
+ return false
648
+ }
649
+ }
650
+
651
+ Buffer.concat = function concat(list, length) {
652
+ if (!Array.isArray(list)) {
653
+ throw new TypeError('"list" argument must be an Array of Buffers')
654
+ }
655
+
656
+ if (list.length === 0) {
657
+ return Buffer.alloc(0)
658
+ }
659
+
660
+ let i
661
+ if (length === undefined) {
662
+ length = 0
663
+ for (i = 0; i < list.length; ++i) {
664
+ length += list[i].length
665
+ }
666
+ }
667
+
668
+ const buffer = Buffer.allocUnsafe(length)
669
+ let pos = 0
670
+ for (i = 0; i < list.length; ++i) {
671
+ let buf = list[i]
672
+ if (isInstance(buf, Uint8Array)) {
673
+ if (pos + buf.length > buffer.length) {
674
+ if (!Buffer.isBuffer(buf)) {
675
+ buf = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
676
+ }
677
+ buf.copy(buffer, pos)
678
+ } else {
679
+ Uint8Array.prototype.set.call(
680
+ buffer,
681
+ buf,
682
+ pos
683
+ )
684
+ }
685
+ } else if (!Buffer.isBuffer(buf)) {
686
+ throw new TypeError('"list" argument must be an Array of Buffers')
687
+ } else {
688
+ buf.copy(buffer, pos)
689
+ }
690
+ pos += buf.length
691
+ }
692
+ return buffer
693
+ }
694
+
695
+ function byteLength(string, encoding) {
696
+ if (Buffer.isBuffer(string)) {
697
+ return string.length
698
+ }
699
+ if (ArrayBuffer.isView(string) || isInstance(string, ArrayBuffer)) {
700
+ return string.byteLength
701
+ }
702
+ if (typeof string !== 'string') {
703
+ throw new TypeError(
704
+ 'The "string" argument must be one of type string, Buffer, or ArrayBuffer. ' +
705
+ 'Received type ' + typeof string
706
+ )
707
+ }
708
+
709
+ const len = string.length
710
+ const mustMatch = (arguments.length > 2 && arguments[2] === true)
711
+ if (!mustMatch && len === 0) return 0
712
+
713
+ // Use a for loop to avoid recursion
714
+ let loweredCase = false
715
+ for (; ;) {
716
+ switch (encoding) {
717
+ case 'ascii':
718
+ case 'latin1':
719
+ case 'binary':
720
+ return len
721
+ case 'utf8':
722
+ case 'utf-8':
723
+ return utf8ToBytes(string).length
724
+ case 'ucs2':
725
+ case 'ucs-2':
726
+ case 'utf16le':
727
+ case 'utf-16le':
728
+ return len * 2
729
+ case 'hex':
730
+ return len >>> 1
731
+ case 'base64':
732
+ return base64ToBytes(string).length
733
+ default:
734
+ if (loweredCase) {
735
+ return mustMatch ? -1 : utf8ToBytes(string).length // assume utf8
736
+ }
737
+ encoding = ('' + encoding).toLowerCase()
738
+ loweredCase = true
739
+ }
740
+ }
741
+ }
742
+ Buffer.byteLength = byteLength
743
+
744
+ function slowToString(encoding, start, end) {
745
+ let loweredCase = false
746
+
747
+ // No need to verify that "this.length <= MAX_UINT32" since it's a read-only
748
+ // property of a typed array.
749
+
750
+ // This behaves neither like String nor Uint8Array in that we set start/end
751
+ // to their upper/lower bounds if the value passed is out of range.
752
+ // undefined is handled specially as per ECMA-262 6th Edition,
753
+ // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization.
754
+ if (start === undefined || start < 0) {
755
+ start = 0
756
+ }
757
+ // Return early if start > this.length. Done here to prevent potential uint32
758
+ // coercion fail below.
759
+ if (start > this.length) {
760
+ return ''
761
+ }
762
+
763
+ if (end === undefined || end > this.length) {
764
+ end = this.length
765
+ }
766
+
767
+ if (end <= 0) {
768
+ return ''
769
+ }
770
+
771
+ // Force coercion to uint32. This will also coerce falsey/NaN values to 0.
772
+ end >>>= 0
773
+ start >>>= 0
774
+
775
+ if (end <= start) {
776
+ return ''
777
+ }
778
+
779
+ if (!encoding) encoding = 'utf8'
780
+
781
+ while (true) {
782
+ switch (encoding) {
783
+ case 'hex':
784
+ return hexSlice(this, start, end)
785
+
786
+ case 'utf8':
787
+ case 'utf-8':
788
+ return utf8Slice(this, start, end)
789
+
790
+ case 'ascii':
791
+ return asciiSlice(this, start, end)
792
+
793
+ case 'latin1':
794
+ case 'binary':
795
+ return latin1Slice(this, start, end)
796
+
797
+ case 'base64':
798
+ return base64Slice(this, start, end)
799
+
800
+ case 'ucs2':
801
+ case 'ucs-2':
802
+ case 'utf16le':
803
+ case 'utf-16le':
804
+ return utf16leSlice(this, start, end)
805
+
806
+ default:
807
+ if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
808
+ encoding = (encoding + '').toLowerCase()
809
+ loweredCase = true
810
+ }
811
+ }
812
+ }
813
+
814
+ // This property is used by `Buffer.isBuffer` (and the `is-buffer` npm package)
815
+ // to detect a Buffer instance. It's not possible to use `instanceof Buffer`
816
+ // reliably in a browserify context because there could be multiple different
817
+ // copies of the 'buffer' package in use. This method works even for Buffer
818
+ // instances that were created from another copy of the `buffer` package.
819
+ // See: https://github.com/feross/buffer/issues/154
820
+ Buffer.prototype._isBuffer = true
821
+
822
+ function swap(b, n, m) {
823
+ const i = b[n]
824
+ b[n] = b[m]
825
+ b[m] = i
826
+ }
827
+
828
+ Buffer.prototype.swap16 = function swap16() {
829
+ const len = this.length
830
+ if (len % 2 !== 0) {
831
+ throw new RangeError('Buffer size must be a multiple of 16-bits')
832
+ }
833
+ for (let i = 0; i < len; i += 2) {
834
+ swap(this, i, i + 1)
835
+ }
836
+ return this
837
+ }
838
+
839
+ Buffer.prototype.swap32 = function swap32() {
840
+ const len = this.length
841
+ if (len % 4 !== 0) {
842
+ throw new RangeError('Buffer size must be a multiple of 32-bits')
843
+ }
844
+ for (let i = 0; i < len; i += 4) {
845
+ swap(this, i, i + 3)
846
+ swap(this, i + 1, i + 2)
847
+ }
848
+ return this
849
+ }
850
+
851
+ Buffer.prototype.swap64 = function swap64() {
852
+ const len = this.length
853
+ if (len % 8 !== 0) {
854
+ throw new RangeError('Buffer size must be a multiple of 64-bits')
855
+ }
856
+ for (let i = 0; i < len; i += 8) {
857
+ swap(this, i, i + 7)
858
+ swap(this, i + 1, i + 6)
859
+ swap(this, i + 2, i + 5)
860
+ swap(this, i + 3, i + 4)
861
+ }
862
+ return this
863
+ }
864
+
865
+ Buffer.prototype.toString = function toString() {
866
+ const length = this.length
867
+ if (length === 0) return ''
868
+ if (arguments.length === 0) return utf8Slice(this, 0, length)
869
+ return slowToString.apply(this, arguments)
870
+ }
871
+
872
+ Buffer.prototype.toLocaleString = Buffer.prototype.toString
873
+
874
+ Buffer.prototype.equals = function equals(b) {
875
+ if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer')
876
+ if (this === b) return true
877
+ return Buffer.compare(this, b) === 0
878
+ }
879
+
880
+ Buffer.prototype.inspect = function inspect() {
881
+ let str = ''
882
+ const max = exports.INSPECT_MAX_BYTES
883
+ str = this.toString('hex', 0, max).replace(/(.{2})/g, '$1 ').trim()
884
+ if (this.length > max) str += ' ... '
885
+ return '<Buffer ' + str + '>'
886
+ }
887
+ if (customInspectSymbol) {
888
+ Buffer.prototype[customInspectSymbol] = Buffer.prototype.inspect
889
+ }
890
+
891
+ Buffer.prototype.compare = function compare(target, start, end, thisStart, thisEnd) {
892
+ if (isInstance(target, Uint8Array)) {
893
+ target = Buffer.from(target, target.offset, target.byteLength)
894
+ }
895
+ if (!Buffer.isBuffer(target)) {
896
+ throw new TypeError(
897
+ 'The "target" argument must be one of type Buffer or Uint8Array. ' +
898
+ 'Received type ' + (typeof target)
899
+ )
900
+ }
901
+
902
+ if (start === undefined) {
903
+ start = 0
904
+ }
905
+ if (end === undefined) {
906
+ end = target ? target.length : 0
907
+ }
908
+ if (thisStart === undefined) {
909
+ thisStart = 0
910
+ }
911
+ if (thisEnd === undefined) {
912
+ thisEnd = this.length
913
+ }
914
+
915
+ if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) {
916
+ throw new RangeError('out of range index')
917
+ }
918
+
919
+ if (thisStart >= thisEnd && start >= end) {
920
+ return 0
921
+ }
922
+ if (thisStart >= thisEnd) {
923
+ return -1
924
+ }
925
+ if (start >= end) {
926
+ return 1
927
+ }
928
+
929
+ start >>>= 0
930
+ end >>>= 0
931
+ thisStart >>>= 0
932
+ thisEnd >>>= 0
933
+
934
+ if (this === target) return 0
935
+
936
+ let x = thisEnd - thisStart
937
+ let y = end - start
938
+ const len = Math.min(x, y)
939
+
940
+ const thisCopy = this.slice(thisStart, thisEnd)
941
+ const targetCopy = target.slice(start, end)
942
+
943
+ for (let i = 0; i < len; ++i) {
944
+ if (thisCopy[i] !== targetCopy[i]) {
945
+ x = thisCopy[i]
946
+ y = targetCopy[i]
947
+ break
948
+ }
949
+ }
950
+
951
+ if (x < y) return -1
952
+ if (y < x) return 1
953
+ return 0
954
+ }
955
+
956
+ // Finds either the first index of `val` in `buffer` at offset >= `byteOffset`,
957
+ // OR the last index of `val` in `buffer` at offset <= `byteOffset`.
958
+ //
959
+ // Arguments:
960
+ // - buffer - a Buffer to search
961
+ // - val - a string, Buffer, or number
962
+ // - byteOffset - an index into `buffer`; will be clamped to an int32
963
+ // - encoding - an optional encoding, relevant is val is a string
964
+ // - dir - true for indexOf, false for lastIndexOf
965
+ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
966
+ // Empty buffer means no match
967
+ if (buffer.length === 0) return -1
968
+
969
+ // Normalize byteOffset
970
+ if (typeof byteOffset === 'string') {
971
+ encoding = byteOffset
972
+ byteOffset = 0
973
+ } else if (byteOffset > 0x7fffffff) {
974
+ byteOffset = 0x7fffffff
975
+ } else if (byteOffset < -0x80000000) {
976
+ byteOffset = -0x80000000
977
+ }
978
+ byteOffset = +byteOffset // Coerce to Number.
979
+ if (numberIsNaN(byteOffset)) {
980
+ // byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer
981
+ byteOffset = dir ? 0 : (buffer.length - 1)
982
+ }
983
+
984
+ // Normalize byteOffset: negative offsets start from the end of the buffer
985
+ if (byteOffset < 0) byteOffset = buffer.length + byteOffset
986
+ if (byteOffset >= buffer.length) {
987
+ if (dir) return -1
988
+ else byteOffset = buffer.length - 1
989
+ } else if (byteOffset < 0) {
990
+ if (dir) byteOffset = 0
991
+ else return -1
992
+ }
993
+
994
+ // Normalize val
995
+ if (typeof val === 'string') {
996
+ val = Buffer.from(val, encoding)
997
+ }
998
+
999
+ // Finally, search either indexOf (if dir is true) or lastIndexOf
1000
+ if (Buffer.isBuffer(val)) {
1001
+ // Special case: looking for empty string/buffer always fails
1002
+ if (val.length === 0) {
1003
+ return -1
1004
+ }
1005
+ return arrayIndexOf(buffer, val, byteOffset, encoding, dir)
1006
+ } else if (typeof val === 'number') {
1007
+ val = val & 0xFF // Search for a byte value [0-255]
1008
+ if (typeof Uint8Array.prototype.indexOf === 'function') {
1009
+ if (dir) {
1010
+ return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset)
1011
+ } else {
1012
+ return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset)
1013
+ }
1014
+ }
1015
+ return arrayIndexOf(buffer, [val], byteOffset, encoding, dir)
1016
+ }
1017
+
1018
+ throw new TypeError('val must be string, number or Buffer')
1019
+ }
1020
+
1021
+ function arrayIndexOf(arr, val, byteOffset, encoding, dir) {
1022
+ let indexSize = 1
1023
+ let arrLength = arr.length
1024
+ let valLength = val.length
1025
+
1026
+ if (encoding !== undefined) {
1027
+ encoding = String(encoding).toLowerCase()
1028
+ if (encoding === 'ucs2' || encoding === 'ucs-2' ||
1029
+ encoding === 'utf16le' || encoding === 'utf-16le') {
1030
+ if (arr.length < 2 || val.length < 2) {
1031
+ return -1
1032
+ }
1033
+ indexSize = 2
1034
+ arrLength /= 2
1035
+ valLength /= 2
1036
+ byteOffset /= 2
1037
+ }
1038
+ }
1039
+
1040
+ function read(buf, i) {
1041
+ if (indexSize === 1) {
1042
+ return buf[i]
1043
+ } else {
1044
+ return buf.readUInt16BE(i * indexSize)
1045
+ }
1046
+ }
1047
+
1048
+ let i
1049
+ if (dir) {
1050
+ let foundIndex = -1
1051
+ for (i = byteOffset; i < arrLength; i++) {
1052
+ if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) {
1053
+ if (foundIndex === -1) foundIndex = i
1054
+ if (i - foundIndex + 1 === valLength) return foundIndex * indexSize
1055
+ } else {
1056
+ if (foundIndex !== -1) i -= i - foundIndex
1057
+ foundIndex = -1
1058
+ }
1059
+ }
1060
+ } else {
1061
+ if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength
1062
+ for (i = byteOffset; i >= 0; i--) {
1063
+ let found = true
1064
+ for (let j = 0; j < valLength; j++) {
1065
+ if (read(arr, i + j) !== read(val, j)) {
1066
+ found = false
1067
+ break
1068
+ }
1069
+ }
1070
+ if (found) return i
1071
+ }
1072
+ }
1073
+
1074
+ return -1
1075
+ }
1076
+
1077
+ Buffer.prototype.includes = function includes(val, byteOffset, encoding) {
1078
+ return this.indexOf(val, byteOffset, encoding) !== -1
1079
+ }
1080
+
1081
+ Buffer.prototype.indexOf = function indexOf(val, byteOffset, encoding) {
1082
+ return bidirectionalIndexOf(this, val, byteOffset, encoding, true)
1083
+ }
1084
+
1085
+ Buffer.prototype.lastIndexOf = function lastIndexOf(val, byteOffset, encoding) {
1086
+ return bidirectionalIndexOf(this, val, byteOffset, encoding, false)
1087
+ }
1088
+
1089
+ function hexWrite(buf, string, offset, length) {
1090
+ offset = Number(offset) || 0
1091
+ const remaining = buf.length - offset
1092
+ if (!length) {
1093
+ length = remaining
1094
+ } else {
1095
+ length = Number(length)
1096
+ if (length > remaining) {
1097
+ length = remaining
1098
+ }
1099
+ }
1100
+
1101
+ const strLen = string.length
1102
+
1103
+ if (length > strLen / 2) {
1104
+ length = strLen / 2
1105
+ }
1106
+ let i
1107
+ for (i = 0; i < length; ++i) {
1108
+ const parsed = parseInt(string.substr(i * 2, 2), 16)
1109
+ if (numberIsNaN(parsed)) return i
1110
+ buf[offset + i] = parsed
1111
+ }
1112
+ return i
1113
+ }
1114
+
1115
+ function utf8Write(buf, string, offset, length) {
1116
+ return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length)
1117
+ }
1118
+
1119
+ function asciiWrite(buf, string, offset, length) {
1120
+ return blitBuffer(asciiToBytes(string), buf, offset, length)
1121
+ }
1122
+
1123
+ function base64Write(buf, string, offset, length) {
1124
+ return blitBuffer(base64ToBytes(string), buf, offset, length)
1125
+ }
1126
+
1127
+ function ucs2Write(buf, string, offset, length) {
1128
+ return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length)
1129
+ }
1130
+
1131
+ Buffer.prototype.write = function write(string, offset, length, encoding) {
1132
+ // Buffer#write(string)
1133
+ if (offset === undefined) {
1134
+ encoding = 'utf8'
1135
+ length = this.length
1136
+ offset = 0
1137
+ // Buffer#write(string, encoding)
1138
+ } else if (length === undefined && typeof offset === 'string') {
1139
+ encoding = offset
1140
+ length = this.length
1141
+ offset = 0
1142
+ // Buffer#write(string, offset[, length][, encoding])
1143
+ } else if (isFinite(offset)) {
1144
+ offset = offset >>> 0
1145
+ if (isFinite(length)) {
1146
+ length = length >>> 0
1147
+ if (encoding === undefined) encoding = 'utf8'
1148
+ } else {
1149
+ encoding = length
1150
+ length = undefined
1151
+ }
1152
+ } else {
1153
+ throw new Error(
1154
+ 'Buffer.write(string, encoding, offset[, length]) is no longer supported'
1155
+ )
1156
+ }
1157
+
1158
+ const remaining = this.length - offset
1159
+ if (length === undefined || length > remaining) length = remaining
1160
+
1161
+ if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) {
1162
+ throw new RangeError('Attempt to write outside buffer bounds')
1163
+ }
1164
+
1165
+ if (!encoding) encoding = 'utf8'
1166
+
1167
+ let loweredCase = false
1168
+ for (; ;) {
1169
+ switch (encoding) {
1170
+ case 'hex':
1171
+ return hexWrite(this, string, offset, length)
1172
+
1173
+ case 'utf8':
1174
+ case 'utf-8':
1175
+ return utf8Write(this, string, offset, length)
1176
+
1177
+ case 'ascii':
1178
+ case 'latin1':
1179
+ case 'binary':
1180
+ return asciiWrite(this, string, offset, length)
1181
+
1182
+ case 'base64':
1183
+ // Warning: maxLength not taken into account in base64Write
1184
+ return base64Write(this, string, offset, length)
1185
+
1186
+ case 'ucs2':
1187
+ case 'ucs-2':
1188
+ case 'utf16le':
1189
+ case 'utf-16le':
1190
+ return ucs2Write(this, string, offset, length)
1191
+
1192
+ default:
1193
+ if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
1194
+ encoding = ('' + encoding).toLowerCase()
1195
+ loweredCase = true
1196
+ }
1197
+ }
1198
+ }
1199
+
1200
+ Buffer.prototype.toJSON = function toJSON() {
1201
+ return {
1202
+ type: 'Buffer',
1203
+ data: Array.prototype.slice.call(this._arr || this, 0)
1204
+ }
1205
+ }
1206
+
1207
+ function base64Slice(buf, start, end) {
1208
+ if (start === 0 && end === buf.length) {
1209
+ return base64.fromByteArray(buf)
1210
+ } else {
1211
+ return base64.fromByteArray(buf.slice(start, end))
1212
+ }
1213
+ }
1214
+
1215
+ function utf8Slice(buf, start, end) {
1216
+ end = Math.min(buf.length, end)
1217
+ const res = []
1218
+
1219
+ let i = start
1220
+ while (i < end) {
1221
+ const firstByte = buf[i]
1222
+ let codePoint = null
1223
+ let bytesPerSequence = (firstByte > 0xEF)
1224
+ ? 4
1225
+ : (firstByte > 0xDF)
1226
+ ? 3
1227
+ : (firstByte > 0xBF)
1228
+ ? 2
1229
+ : 1
1230
+
1231
+ if (i + bytesPerSequence <= end) {
1232
+ let secondByte, thirdByte, fourthByte, tempCodePoint
1233
+
1234
+ switch (bytesPerSequence) {
1235
+ case 1:
1236
+ if (firstByte < 0x80) {
1237
+ codePoint = firstByte
1238
+ }
1239
+ break
1240
+ case 2:
1241
+ secondByte = buf[i + 1]
1242
+ if ((secondByte & 0xC0) === 0x80) {
1243
+ tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F)
1244
+ if (tempCodePoint > 0x7F) {
1245
+ codePoint = tempCodePoint
1246
+ }
1247
+ }
1248
+ break
1249
+ case 3:
1250
+ secondByte = buf[i + 1]
1251
+ thirdByte = buf[i + 2]
1252
+ if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) {
1253
+ tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F)
1254
+ if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) {
1255
+ codePoint = tempCodePoint
1256
+ }
1257
+ }
1258
+ break
1259
+ case 4:
1260
+ secondByte = buf[i + 1]
1261
+ thirdByte = buf[i + 2]
1262
+ fourthByte = buf[i + 3]
1263
+ if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) {
1264
+ tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F)
1265
+ if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) {
1266
+ codePoint = tempCodePoint
1267
+ }
1268
+ }
1269
+ }
1270
+ }
1271
+
1272
+ if (codePoint === null) {
1273
+ // we did not generate a valid codePoint so insert a
1274
+ // replacement char (U+FFFD) and advance only 1 byte
1275
+ codePoint = 0xFFFD
1276
+ bytesPerSequence = 1
1277
+ } else if (codePoint > 0xFFFF) {
1278
+ // encode to utf16 (surrogate pair dance)
1279
+ codePoint -= 0x10000
1280
+ res.push(codePoint >>> 10 & 0x3FF | 0xD800)
1281
+ codePoint = 0xDC00 | codePoint & 0x3FF
1282
+ }
1283
+
1284
+ res.push(codePoint)
1285
+ i += bytesPerSequence
1286
+ }
1287
+
1288
+ return decodeCodePointsArray(res)
1289
+ }
1290
+
1291
+ // Based on http://stackoverflow.com/a/22747272/680742, the browser with
1292
+ // the lowest limit is Chrome, with 0x10000 args.
1293
+ // We go 1 magnitude less, for safety
1294
+ const MAX_ARGUMENTS_LENGTH = 0x1000
1295
+
1296
+ function decodeCodePointsArray(codePoints) {
1297
+ const len = codePoints.length
1298
+ if (len <= MAX_ARGUMENTS_LENGTH) {
1299
+ return String.fromCharCode.apply(String, codePoints) // avoid extra slice()
1300
+ }
1301
+
1302
+ // Decode in chunks to avoid "call stack size exceeded".
1303
+ let res = ''
1304
+ let i = 0
1305
+ while (i < len) {
1306
+ res += String.fromCharCode.apply(
1307
+ String,
1308
+ codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH)
1309
+ )
1310
+ }
1311
+ return res
1312
+ }
1313
+
1314
+ function asciiSlice(buf, start, end) {
1315
+ let ret = ''
1316
+ end = Math.min(buf.length, end)
1317
+
1318
+ for (let i = start; i < end; ++i) {
1319
+ ret += String.fromCharCode(buf[i] & 0x7F)
1320
+ }
1321
+ return ret
1322
+ }
1323
+
1324
+ function latin1Slice(buf, start, end) {
1325
+ let ret = ''
1326
+ end = Math.min(buf.length, end)
1327
+
1328
+ for (let i = start; i < end; ++i) {
1329
+ ret += String.fromCharCode(buf[i])
1330
+ }
1331
+ return ret
1332
+ }
1333
+
1334
+ function hexSlice(buf, start, end) {
1335
+ const len = buf.length
1336
+
1337
+ if (!start || start < 0) start = 0
1338
+ if (!end || end < 0 || end > len) end = len
1339
+
1340
+ let out = ''
1341
+ for (let i = start; i < end; ++i) {
1342
+ out += hexSliceLookupTable[buf[i]]
1343
+ }
1344
+ return out
1345
+ }
1346
+
1347
+ function utf16leSlice(buf, start, end) {
1348
+ const bytes = buf.slice(start, end)
1349
+ let res = ''
1350
+ // If bytes.length is odd, the last 8 bits must be ignored (same as node.js)
1351
+ for (let i = 0; i < bytes.length - 1; i += 2) {
1352
+ res += String.fromCharCode(bytes[i] + (bytes[i + 1] * 256))
1353
+ }
1354
+ return res
1355
+ }
1356
+
1357
+ Buffer.prototype.slice = function slice(start, end) {
1358
+ const len = this.length
1359
+ start = ~~start
1360
+ end = end === undefined ? len : ~~end
1361
+
1362
+ if (start < 0) {
1363
+ start += len
1364
+ if (start < 0) start = 0
1365
+ } else if (start > len) {
1366
+ start = len
1367
+ }
1368
+
1369
+ if (end < 0) {
1370
+ end += len
1371
+ if (end < 0) end = 0
1372
+ } else if (end > len) {
1373
+ end = len
1374
+ }
1375
+
1376
+ if (end < start) end = start
1377
+
1378
+ const newBuf = this.subarray(start, end)
1379
+ // Return an augmented `Uint8Array` instance
1380
+ Object.setPrototypeOf(newBuf, Buffer.prototype)
1381
+
1382
+ return newBuf
1383
+ }
1384
+
1385
+ /*
1386
+ * Need to make sure that buffer isn't trying to write out of bounds.
1387
+ */
1388
+ function checkOffset(offset, ext, length) {
1389
+ if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint')
1390
+ if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length')
1391
+ }
1392
+
1393
+ Buffer.prototype.readUintLE =
1394
+ Buffer.prototype.readUIntLE = function readUIntLE(offset, byteLength, noAssert) {
1395
+ offset = offset >>> 0
1396
+ byteLength = byteLength >>> 0
1397
+ if (!noAssert) checkOffset(offset, byteLength, this.length)
1398
+
1399
+ let val = this[offset]
1400
+ let mul = 1
1401
+ let i = 0
1402
+ while (++i < byteLength && (mul *= 0x100)) {
1403
+ val += this[offset + i] * mul
1404
+ }
1405
+
1406
+ return val
1407
+ }
1408
+
1409
+ Buffer.prototype.readUintBE =
1410
+ Buffer.prototype.readUIntBE = function readUIntBE(offset, byteLength, noAssert) {
1411
+ offset = offset >>> 0
1412
+ byteLength = byteLength >>> 0
1413
+ if (!noAssert) {
1414
+ checkOffset(offset, byteLength, this.length)
1415
+ }
1416
+
1417
+ let val = this[offset + --byteLength]
1418
+ let mul = 1
1419
+ while (byteLength > 0 && (mul *= 0x100)) {
1420
+ val += this[offset + --byteLength] * mul
1421
+ }
1422
+
1423
+ return val
1424
+ }
1425
+
1426
+ Buffer.prototype.readUint8 =
1427
+ Buffer.prototype.readUInt8 = function readUInt8(offset, noAssert) {
1428
+ offset = offset >>> 0
1429
+ if (!noAssert) checkOffset(offset, 1, this.length)
1430
+ return this[offset]
1431
+ }
1432
+
1433
+ Buffer.prototype.readUint16LE =
1434
+ Buffer.prototype.readUInt16LE = function readUInt16LE(offset, noAssert) {
1435
+ offset = offset >>> 0
1436
+ if (!noAssert) checkOffset(offset, 2, this.length)
1437
+ return this[offset] | (this[offset + 1] << 8)
1438
+ }
1439
+
1440
+ Buffer.prototype.readUint16BE =
1441
+ Buffer.prototype.readUInt16BE = function readUInt16BE(offset, noAssert) {
1442
+ offset = offset >>> 0
1443
+ if (!noAssert) checkOffset(offset, 2, this.length)
1444
+ return (this[offset] << 8) | this[offset + 1]
1445
+ }
1446
+
1447
+ Buffer.prototype.readUint32LE =
1448
+ Buffer.prototype.readUInt32LE = function readUInt32LE(offset, noAssert) {
1449
+ offset = offset >>> 0
1450
+ if (!noAssert) checkOffset(offset, 4, this.length)
1451
+
1452
+ return ((this[offset]) |
1453
+ (this[offset + 1] << 8) |
1454
+ (this[offset + 2] << 16)) +
1455
+ (this[offset + 3] * 0x1000000)
1456
+ }
1457
+
1458
+ Buffer.prototype.readUint32BE =
1459
+ Buffer.prototype.readUInt32BE = function readUInt32BE(offset, noAssert) {
1460
+ offset = offset >>> 0
1461
+ if (!noAssert) checkOffset(offset, 4, this.length)
1462
+
1463
+ return (this[offset] * 0x1000000) +
1464
+ ((this[offset + 1] << 16) |
1465
+ (this[offset + 2] << 8) |
1466
+ this[offset + 3])
1467
+ }
1468
+
1469
+ Buffer.prototype.readBigUInt64LE = defineBigIntMethod(function readBigUInt64LE(offset) {
1470
+ offset = offset >>> 0
1471
+ validateNumber(offset, 'offset')
1472
+ const first = this[offset]
1473
+ const last = this[offset + 7]
1474
+ if (first === undefined || last === undefined) {
1475
+ boundsError(offset, this.length - 8)
1476
+ }
1477
+
1478
+ const lo = first +
1479
+ this[++offset] * 2 ** 8 +
1480
+ this[++offset] * 2 ** 16 +
1481
+ this[++offset] * 2 ** 24
1482
+
1483
+ const hi = this[++offset] +
1484
+ this[++offset] * 2 ** 8 +
1485
+ this[++offset] * 2 ** 16 +
1486
+ last * 2 ** 24
1487
+
1488
+ return BigInt(lo) + (BigInt(hi) << BigInt(32))
1489
+ })
1490
+
1491
+ Buffer.prototype.readBigUInt64BE = defineBigIntMethod(function readBigUInt64BE(offset) {
1492
+ offset = offset >>> 0
1493
+ validateNumber(offset, 'offset')
1494
+ const first = this[offset]
1495
+ const last = this[offset + 7]
1496
+ if (first === undefined || last === undefined) {
1497
+ boundsError(offset, this.length - 8)
1498
+ }
1499
+
1500
+ const hi = first * 2 ** 24 +
1501
+ this[++offset] * 2 ** 16 +
1502
+ this[++offset] * 2 ** 8 +
1503
+ this[++offset]
1504
+
1505
+ const lo = this[++offset] * 2 ** 24 +
1506
+ this[++offset] * 2 ** 16 +
1507
+ this[++offset] * 2 ** 8 +
1508
+ last
1509
+
1510
+ return (BigInt(hi) << BigInt(32)) + BigInt(lo)
1511
+ })
1512
+
1513
+ Buffer.prototype.readIntLE = function readIntLE(offset, byteLength, noAssert) {
1514
+ offset = offset >>> 0
1515
+ byteLength = byteLength >>> 0
1516
+ if (!noAssert) checkOffset(offset, byteLength, this.length)
1517
+
1518
+ let val = this[offset]
1519
+ let mul = 1
1520
+ let i = 0
1521
+ while (++i < byteLength && (mul *= 0x100)) {
1522
+ val += this[offset + i] * mul
1523
+ }
1524
+ mul *= 0x80
1525
+
1526
+ if (val >= mul) val -= Math.pow(2, 8 * byteLength)
1527
+
1528
+ return val
1529
+ }
1530
+
1531
+ Buffer.prototype.readIntBE = function readIntBE(offset, byteLength, noAssert) {
1532
+ offset = offset >>> 0
1533
+ byteLength = byteLength >>> 0
1534
+ if (!noAssert) checkOffset(offset, byteLength, this.length)
1535
+
1536
+ let i = byteLength
1537
+ let mul = 1
1538
+ let val = this[offset + --i]
1539
+ while (i > 0 && (mul *= 0x100)) {
1540
+ val += this[offset + --i] * mul
1541
+ }
1542
+ mul *= 0x80
1543
+
1544
+ if (val >= mul) val -= Math.pow(2, 8 * byteLength)
1545
+
1546
+ return val
1547
+ }
1548
+
1549
+ Buffer.prototype.readInt8 = function readInt8(offset, noAssert) {
1550
+ offset = offset >>> 0
1551
+ if (!noAssert) checkOffset(offset, 1, this.length)
1552
+ if (!(this[offset] & 0x80)) return (this[offset])
1553
+ return ((0xff - this[offset] + 1) * -1)
1554
+ }
1555
+
1556
+ Buffer.prototype.readInt16LE = function readInt16LE(offset, noAssert) {
1557
+ offset = offset >>> 0
1558
+ if (!noAssert) checkOffset(offset, 2, this.length)
1559
+ const val = this[offset] | (this[offset + 1] << 8)
1560
+ return (val & 0x8000) ? val | 0xFFFF0000 : val
1561
+ }
1562
+
1563
+ Buffer.prototype.readInt16BE = function readInt16BE(offset, noAssert) {
1564
+ offset = offset >>> 0
1565
+ if (!noAssert) checkOffset(offset, 2, this.length)
1566
+ const val = this[offset + 1] | (this[offset] << 8)
1567
+ return (val & 0x8000) ? val | 0xFFFF0000 : val
1568
+ }
1569
+
1570
+ Buffer.prototype.readInt32LE = function readInt32LE(offset, noAssert) {
1571
+ offset = offset >>> 0
1572
+ if (!noAssert) checkOffset(offset, 4, this.length)
1573
+
1574
+ return (this[offset]) |
1575
+ (this[offset + 1] << 8) |
1576
+ (this[offset + 2] << 16) |
1577
+ (this[offset + 3] << 24)
1578
+ }
1579
+
1580
+ Buffer.prototype.readInt32BE = function readInt32BE(offset, noAssert) {
1581
+ offset = offset >>> 0
1582
+ if (!noAssert) checkOffset(offset, 4, this.length)
1583
+
1584
+ return (this[offset] << 24) |
1585
+ (this[offset + 1] << 16) |
1586
+ (this[offset + 2] << 8) |
1587
+ (this[offset + 3])
1588
+ }
1589
+
1590
+ Buffer.prototype.readBigInt64LE = defineBigIntMethod(function readBigInt64LE(offset) {
1591
+ offset = offset >>> 0
1592
+ validateNumber(offset, 'offset')
1593
+ const first = this[offset]
1594
+ const last = this[offset + 7]
1595
+ if (first === undefined || last === undefined) {
1596
+ boundsError(offset, this.length - 8)
1597
+ }
1598
+
1599
+ const val = this[offset + 4] +
1600
+ this[offset + 5] * 2 ** 8 +
1601
+ this[offset + 6] * 2 ** 16 +
1602
+ (last << 24) // Overflow
1603
+
1604
+ return (BigInt(val) << BigInt(32)) +
1605
+ BigInt(first +
1606
+ this[++offset] * 2 ** 8 +
1607
+ this[++offset] * 2 ** 16 +
1608
+ this[++offset] * 2 ** 24)
1609
+ })
1610
+
1611
+ Buffer.prototype.readBigInt64BE = defineBigIntMethod(function readBigInt64BE(offset) {
1612
+ offset = offset >>> 0
1613
+ validateNumber(offset, 'offset')
1614
+ const first = this[offset]
1615
+ const last = this[offset + 7]
1616
+ if (first === undefined || last === undefined) {
1617
+ boundsError(offset, this.length - 8)
1618
+ }
1619
+
1620
+ const val = (first << 24) + // Overflow
1621
+ this[++offset] * 2 ** 16 +
1622
+ this[++offset] * 2 ** 8 +
1623
+ this[++offset]
1624
+
1625
+ return (BigInt(val) << BigInt(32)) +
1626
+ BigInt(this[++offset] * 2 ** 24 +
1627
+ this[++offset] * 2 ** 16 +
1628
+ this[++offset] * 2 ** 8 +
1629
+ last)
1630
+ })
1631
+
1632
+ Buffer.prototype.readFloatLE = function readFloatLE(offset, noAssert) {
1633
+ offset = offset >>> 0
1634
+ if (!noAssert) checkOffset(offset, 4, this.length)
1635
+ return ieee754.read(this, offset, true, 23, 4)
1636
+ }
1637
+
1638
+ Buffer.prototype.readFloatBE = function readFloatBE(offset, noAssert) {
1639
+ offset = offset >>> 0
1640
+ if (!noAssert) checkOffset(offset, 4, this.length)
1641
+ return ieee754.read(this, offset, false, 23, 4)
1642
+ }
1643
+
1644
+ Buffer.prototype.readDoubleLE = function readDoubleLE(offset, noAssert) {
1645
+ offset = offset >>> 0
1646
+ if (!noAssert) checkOffset(offset, 8, this.length)
1647
+ return ieee754.read(this, offset, true, 52, 8)
1648
+ }
1649
+
1650
+ Buffer.prototype.readDoubleBE = function readDoubleBE(offset, noAssert) {
1651
+ offset = offset >>> 0
1652
+ if (!noAssert) checkOffset(offset, 8, this.length)
1653
+ return ieee754.read(this, offset, false, 52, 8)
1654
+ }
1655
+
1656
+ function checkInt(buf, value, offset, ext, max, min) {
1657
+ if (!Buffer.isBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance')
1658
+ if (value > max || value < min) throw new RangeError('"value" argument is out of bounds')
1659
+ if (offset + ext > buf.length) throw new RangeError('Index out of range')
1660
+ }
1661
+
1662
+ Buffer.prototype.writeUintLE =
1663
+ Buffer.prototype.writeUIntLE = function writeUIntLE(value, offset, byteLength, noAssert) {
1664
+ value = +value
1665
+ offset = offset >>> 0
1666
+ byteLength = byteLength >>> 0
1667
+ if (!noAssert) {
1668
+ const maxBytes = Math.pow(2, 8 * byteLength) - 1
1669
+ checkInt(this, value, offset, byteLength, maxBytes, 0)
1670
+ }
1671
+
1672
+ let mul = 1
1673
+ let i = 0
1674
+ this[offset] = value & 0xFF
1675
+ while (++i < byteLength && (mul *= 0x100)) {
1676
+ this[offset + i] = (value / mul) & 0xFF
1677
+ }
1678
+
1679
+ return offset + byteLength
1680
+ }
1681
+
1682
+ Buffer.prototype.writeUintBE =
1683
+ Buffer.prototype.writeUIntBE = function writeUIntBE(value, offset, byteLength, noAssert) {
1684
+ value = +value
1685
+ offset = offset >>> 0
1686
+ byteLength = byteLength >>> 0
1687
+ if (!noAssert) {
1688
+ const maxBytes = Math.pow(2, 8 * byteLength) - 1
1689
+ checkInt(this, value, offset, byteLength, maxBytes, 0)
1690
+ }
1691
+
1692
+ let i = byteLength - 1
1693
+ let mul = 1
1694
+ this[offset + i] = value & 0xFF
1695
+ while (--i >= 0 && (mul *= 0x100)) {
1696
+ this[offset + i] = (value / mul) & 0xFF
1697
+ }
1698
+
1699
+ return offset + byteLength
1700
+ }
1701
+
1702
+ Buffer.prototype.writeUint8 =
1703
+ Buffer.prototype.writeUInt8 = function writeUInt8(value, offset, noAssert) {
1704
+ value = +value
1705
+ offset = offset >>> 0
1706
+ if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0)
1707
+ this[offset] = (value & 0xff)
1708
+ return offset + 1
1709
+ }
1710
+
1711
+ Buffer.prototype.writeUint16LE =
1712
+ Buffer.prototype.writeUInt16LE = function writeUInt16LE(value, offset, noAssert) {
1713
+ value = +value
1714
+ offset = offset >>> 0
1715
+ if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)
1716
+ this[offset] = (value & 0xff)
1717
+ this[offset + 1] = (value >>> 8)
1718
+ return offset + 2
1719
+ }
1720
+
1721
+ Buffer.prototype.writeUint16BE =
1722
+ Buffer.prototype.writeUInt16BE = function writeUInt16BE(value, offset, noAssert) {
1723
+ value = +value
1724
+ offset = offset >>> 0
1725
+ if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)
1726
+ this[offset] = (value >>> 8)
1727
+ this[offset + 1] = (value & 0xff)
1728
+ return offset + 2
1729
+ }
1730
+
1731
+ Buffer.prototype.writeUint32LE =
1732
+ Buffer.prototype.writeUInt32LE = function writeUInt32LE(value, offset, noAssert) {
1733
+ value = +value
1734
+ offset = offset >>> 0
1735
+ if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)
1736
+ this[offset + 3] = (value >>> 24)
1737
+ this[offset + 2] = (value >>> 16)
1738
+ this[offset + 1] = (value >>> 8)
1739
+ this[offset] = (value & 0xff)
1740
+ return offset + 4
1741
+ }
1742
+
1743
+ Buffer.prototype.writeUint32BE =
1744
+ Buffer.prototype.writeUInt32BE = function writeUInt32BE(value, offset, noAssert) {
1745
+ value = +value
1746
+ offset = offset >>> 0
1747
+ if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)
1748
+ this[offset] = (value >>> 24)
1749
+ this[offset + 1] = (value >>> 16)
1750
+ this[offset + 2] = (value >>> 8)
1751
+ this[offset + 3] = (value & 0xff)
1752
+ return offset + 4
1753
+ }
1754
+
1755
+ function wrtBigUInt64LE(buf, value, offset, min, max) {
1756
+ checkIntBI(value, min, max, buf, offset, 7)
1757
+
1758
+ let lo = Number(value & BigInt(0xffffffff))
1759
+ buf[offset++] = lo
1760
+ lo = lo >> 8
1761
+ buf[offset++] = lo
1762
+ lo = lo >> 8
1763
+ buf[offset++] = lo
1764
+ lo = lo >> 8
1765
+ buf[offset++] = lo
1766
+ let hi = Number(value >> BigInt(32) & BigInt(0xffffffff))
1767
+ buf[offset++] = hi
1768
+ hi = hi >> 8
1769
+ buf[offset++] = hi
1770
+ hi = hi >> 8
1771
+ buf[offset++] = hi
1772
+ hi = hi >> 8
1773
+ buf[offset++] = hi
1774
+ return offset
1775
+ }
1776
+
1777
+ function wrtBigUInt64BE(buf, value, offset, min, max) {
1778
+ checkIntBI(value, min, max, buf, offset, 7)
1779
+
1780
+ let lo = Number(value & BigInt(0xffffffff))
1781
+ buf[offset + 7] = lo
1782
+ lo = lo >> 8
1783
+ buf[offset + 6] = lo
1784
+ lo = lo >> 8
1785
+ buf[offset + 5] = lo
1786
+ lo = lo >> 8
1787
+ buf[offset + 4] = lo
1788
+ let hi = Number(value >> BigInt(32) & BigInt(0xffffffff))
1789
+ buf[offset + 3] = hi
1790
+ hi = hi >> 8
1791
+ buf[offset + 2] = hi
1792
+ hi = hi >> 8
1793
+ buf[offset + 1] = hi
1794
+ hi = hi >> 8
1795
+ buf[offset] = hi
1796
+ return offset + 8
1797
+ }
1798
+
1799
+ Buffer.prototype.writeBigUInt64LE = defineBigIntMethod(function writeBigUInt64LE(value, offset = 0) {
1800
+ return wrtBigUInt64LE(this, value, offset, BigInt(0), BigInt('0xffffffffffffffff'))
1801
+ })
1802
+
1803
+ Buffer.prototype.writeBigUInt64BE = defineBigIntMethod(function writeBigUInt64BE(value, offset = 0) {
1804
+ return wrtBigUInt64BE(this, value, offset, BigInt(0), BigInt('0xffffffffffffffff'))
1805
+ })
1806
+
1807
+ Buffer.prototype.writeIntLE = function writeIntLE(value, offset, byteLength, noAssert) {
1808
+ value = +value
1809
+ offset = offset >>> 0
1810
+ if (!noAssert) {
1811
+ const limit = Math.pow(2, (8 * byteLength) - 1)
1812
+
1813
+ checkInt(this, value, offset, byteLength, limit - 1, -limit)
1814
+ }
1815
+
1816
+ let i = 0
1817
+ let mul = 1
1818
+ let sub = 0
1819
+ this[offset] = value & 0xFF
1820
+ while (++i < byteLength && (mul *= 0x100)) {
1821
+ if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) {
1822
+ sub = 1
1823
+ }
1824
+ this[offset + i] = ((value / mul) >> 0) - sub & 0xFF
1825
+ }
1826
+
1827
+ return offset + byteLength
1828
+ }
1829
+
1830
+ Buffer.prototype.writeIntBE = function writeIntBE(value, offset, byteLength, noAssert) {
1831
+ value = +value
1832
+ offset = offset >>> 0
1833
+ if (!noAssert) {
1834
+ const limit = Math.pow(2, (8 * byteLength) - 1)
1835
+
1836
+ checkInt(this, value, offset, byteLength, limit - 1, -limit)
1837
+ }
1838
+
1839
+ let i = byteLength - 1
1840
+ let mul = 1
1841
+ let sub = 0
1842
+ this[offset + i] = value & 0xFF
1843
+ while (--i >= 0 && (mul *= 0x100)) {
1844
+ if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) {
1845
+ sub = 1
1846
+ }
1847
+ this[offset + i] = ((value / mul) >> 0) - sub & 0xFF
1848
+ }
1849
+
1850
+ return offset + byteLength
1851
+ }
1852
+
1853
+ Buffer.prototype.writeInt8 = function writeInt8(value, offset, noAssert) {
1854
+ value = +value
1855
+ offset = offset >>> 0
1856
+ if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80)
1857
+ if (value < 0) value = 0xff + value + 1
1858
+ this[offset] = (value & 0xff)
1859
+ return offset + 1
1860
+ }
1861
+
1862
+ Buffer.prototype.writeInt16LE = function writeInt16LE(value, offset, noAssert) {
1863
+ value = +value
1864
+ offset = offset >>> 0
1865
+ if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)
1866
+ this[offset] = (value & 0xff)
1867
+ this[offset + 1] = (value >>> 8)
1868
+ return offset + 2
1869
+ }
1870
+
1871
+ Buffer.prototype.writeInt16BE = function writeInt16BE(value, offset, noAssert) {
1872
+ value = +value
1873
+ offset = offset >>> 0
1874
+ if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)
1875
+ this[offset] = (value >>> 8)
1876
+ this[offset + 1] = (value & 0xff)
1877
+ return offset + 2
1878
+ }
1879
+
1880
+ Buffer.prototype.writeInt32LE = function writeInt32LE(value, offset, noAssert) {
1881
+ value = +value
1882
+ offset = offset >>> 0
1883
+ if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
1884
+ this[offset] = (value & 0xff)
1885
+ this[offset + 1] = (value >>> 8)
1886
+ this[offset + 2] = (value >>> 16)
1887
+ this[offset + 3] = (value >>> 24)
1888
+ return offset + 4
1889
+ }
1890
+
1891
+ Buffer.prototype.writeInt32BE = function writeInt32BE(value, offset, noAssert) {
1892
+ value = +value
1893
+ offset = offset >>> 0
1894
+ if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
1895
+ if (value < 0) value = 0xffffffff + value + 1
1896
+ this[offset] = (value >>> 24)
1897
+ this[offset + 1] = (value >>> 16)
1898
+ this[offset + 2] = (value >>> 8)
1899
+ this[offset + 3] = (value & 0xff)
1900
+ return offset + 4
1901
+ }
1902
+
1903
+ Buffer.prototype.writeBigInt64LE = defineBigIntMethod(function writeBigInt64LE(value, offset = 0) {
1904
+ return wrtBigUInt64LE(this, value, offset, -BigInt('0x8000000000000000'), BigInt('0x7fffffffffffffff'))
1905
+ })
1906
+
1907
+ Buffer.prototype.writeBigInt64BE = defineBigIntMethod(function writeBigInt64BE(value, offset = 0) {
1908
+ return wrtBigUInt64BE(this, value, offset, -BigInt('0x8000000000000000'), BigInt('0x7fffffffffffffff'))
1909
+ })
1910
+
1911
+ function checkIEEE754(buf, value, offset, ext, max, min) {
1912
+ if (offset + ext > buf.length) throw new RangeError('Index out of range')
1913
+ if (offset < 0) throw new RangeError('Index out of range')
1914
+ }
1915
+
1916
+ function writeFloat(buf, value, offset, littleEndian, noAssert) {
1917
+ value = +value
1918
+ offset = offset >>> 0
1919
+ if (!noAssert) {
1920
+ checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38)
1921
+ }
1922
+ ieee754.write(buf, value, offset, littleEndian, 23, 4)
1923
+ return offset + 4
1924
+ }
1925
+
1926
+ Buffer.prototype.writeFloatLE = function writeFloatLE(value, offset, noAssert) {
1927
+ return writeFloat(this, value, offset, true, noAssert)
1928
+ }
1929
+
1930
+ Buffer.prototype.writeFloatBE = function writeFloatBE(value, offset, noAssert) {
1931
+ return writeFloat(this, value, offset, false, noAssert)
1932
+ }
1933
+
1934
+ function writeDouble(buf, value, offset, littleEndian, noAssert) {
1935
+ value = +value
1936
+ offset = offset >>> 0
1937
+ if (!noAssert) {
1938
+ checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308)
1939
+ }
1940
+ ieee754.write(buf, value, offset, littleEndian, 52, 8)
1941
+ return offset + 8
1942
+ }
1943
+
1944
+ Buffer.prototype.writeDoubleLE = function writeDoubleLE(value, offset, noAssert) {
1945
+ return writeDouble(this, value, offset, true, noAssert)
1946
+ }
1947
+
1948
+ Buffer.prototype.writeDoubleBE = function writeDoubleBE(value, offset, noAssert) {
1949
+ return writeDouble(this, value, offset, false, noAssert)
1950
+ }
1951
+
1952
+ // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
1953
+ Buffer.prototype.copy = function copy(target, targetStart, start, end) {
1954
+ if (!Buffer.isBuffer(target)) throw new TypeError('argument should be a Buffer')
1955
+ if (!start) start = 0
1956
+ if (!end && end !== 0) end = this.length
1957
+ if (targetStart >= target.length) targetStart = target.length
1958
+ if (!targetStart) targetStart = 0
1959
+ if (end > 0 && end < start) end = start
1960
+
1961
+ // Copy 0 bytes; we're done
1962
+ if (end === start) return 0
1963
+ if (target.length === 0 || this.length === 0) return 0
1964
+
1965
+ // Fatal error conditions
1966
+ if (targetStart < 0) {
1967
+ throw new RangeError('targetStart out of bounds')
1968
+ }
1969
+ if (start < 0 || start >= this.length) throw new RangeError('Index out of range')
1970
+ if (end < 0) throw new RangeError('sourceEnd out of bounds')
1971
+
1972
+ // Are we oob?
1973
+ if (end > this.length) end = this.length
1974
+ if (target.length - targetStart < end - start) {
1975
+ end = target.length - targetStart + start
1976
+ }
1977
+
1978
+ const len = end - start
1979
+
1980
+ if (this === target && typeof Uint8Array.prototype.copyWithin === 'function') {
1981
+ // Use built-in when available, missing from IE11
1982
+ this.copyWithin(targetStart, start, end)
1983
+ } else {
1984
+ Uint8Array.prototype.set.call(
1985
+ target,
1986
+ this.subarray(start, end),
1987
+ targetStart
1988
+ )
1989
+ }
1990
+
1991
+ return len
1992
+ }
1993
+
1994
+ // Usage:
1995
+ // buffer.fill(number[, offset[, end]])
1996
+ // buffer.fill(buffer[, offset[, end]])
1997
+ // buffer.fill(string[, offset[, end]][, encoding])
1998
+ Buffer.prototype.fill = function fill(val, start, end, encoding) {
1999
+ // Handle string cases:
2000
+ if (typeof val === 'string') {
2001
+ if (typeof start === 'string') {
2002
+ encoding = start
2003
+ start = 0
2004
+ end = this.length
2005
+ } else if (typeof end === 'string') {
2006
+ encoding = end
2007
+ end = this.length
2008
+ }
2009
+ if (encoding !== undefined && typeof encoding !== 'string') {
2010
+ throw new TypeError('encoding must be a string')
2011
+ }
2012
+ if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) {
2013
+ throw new TypeError('Unknown encoding: ' + encoding)
2014
+ }
2015
+ if (val.length === 1) {
2016
+ const code = val.charCodeAt(0)
2017
+ if ((encoding === 'utf8' && code < 128) ||
2018
+ encoding === 'latin1') {
2019
+ // Fast path: If `val` fits into a single byte, use that numeric value.
2020
+ val = code
2021
+ }
2022
+ }
2023
+ } else if (typeof val === 'number') {
2024
+ val = val & 255
2025
+ } else if (typeof val === 'boolean') {
2026
+ val = Number(val)
2027
+ }
2028
+
2029
+ // Invalid ranges are not set to a default, so can range check early.
2030
+ if (start < 0 || this.length < start || this.length < end) {
2031
+ throw new RangeError('Out of range index')
2032
+ }
2033
+
2034
+ if (end <= start) {
2035
+ return this
2036
+ }
2037
+
2038
+ start = start >>> 0
2039
+ end = end === undefined ? this.length : end >>> 0
2040
+
2041
+ if (!val) val = 0
2042
+
2043
+ let i
2044
+ if (typeof val === 'number') {
2045
+ for (i = start; i < end; ++i) {
2046
+ this[i] = val
2047
+ }
2048
+ } else {
2049
+ const bytes = Buffer.isBuffer(val)
2050
+ ? val
2051
+ : Buffer.from(val, encoding)
2052
+ const len = bytes.length
2053
+ if (len === 0) {
2054
+ throw new TypeError('The value "' + val +
2055
+ '" is invalid for argument "value"')
2056
+ }
2057
+ for (i = 0; i < end - start; ++i) {
2058
+ this[i + start] = bytes[i % len]
2059
+ }
2060
+ }
2061
+
2062
+ return this
2063
+ }
2064
+
2065
+ // CUSTOM ERRORS
2066
+ // =============
2067
+
2068
+ // Simplified versions from Node, changed for Buffer-only usage
2069
+ const errors = {}
2070
+ function E(sym, getMessage, Base) {
2071
+ errors[sym] = class NodeError extends Base {
2072
+ constructor() {
2073
+ super()
2074
+
2075
+ Object.defineProperty(this, 'message', {
2076
+ value: getMessage.apply(this, arguments),
2077
+ writable: true,
2078
+ configurable: true
2079
+ })
2080
+
2081
+ // Add the error code to the name to include it in the stack trace.
2082
+ this.name = `${this.name} [${sym}]`
2083
+ // Access the stack to generate the error message including the error code
2084
+ // from the name.
2085
+ this.stack // eslint-disable-line no-unused-expressions
2086
+ // Reset the name to the actual name.
2087
+ delete this.name
2088
+ }
2089
+
2090
+ get code() {
2091
+ return sym
2092
+ }
2093
+
2094
+ set code(value) {
2095
+ Object.defineProperty(this, 'code', {
2096
+ configurable: true,
2097
+ enumerable: true,
2098
+ value,
2099
+ writable: true
2100
+ })
2101
+ }
2102
+
2103
+ toString() {
2104
+ return `${this.name} [${sym}]: ${this.message}`
2105
+ }
2106
+ }
2107
+ }
2108
+
2109
+ E('ERR_BUFFER_OUT_OF_BOUNDS',
2110
+ function (name) {
2111
+ if (name) {
2112
+ return `${name} is outside of buffer bounds`
2113
+ }
2114
+
2115
+ return 'Attempt to access memory outside buffer bounds'
2116
+ }, RangeError)
2117
+ E('ERR_INVALID_ARG_TYPE',
2118
+ function (name, actual) {
2119
+ return `The "${name}" argument must be of type number. Received type ${typeof actual}`
2120
+ }, TypeError)
2121
+ E('ERR_OUT_OF_RANGE',
2122
+ function (str, range, input) {
2123
+ let msg = `The value of "${str}" is out of range.`
2124
+ let received = input
2125
+ if (Number.isInteger(input) && Math.abs(input) > 2 ** 32) {
2126
+ received = addNumericalSeparator(String(input))
2127
+ } else if (typeof input === 'bigint') {
2128
+ received = String(input)
2129
+ if (input > BigInt(2) ** BigInt(32) || input < -(BigInt(2) ** BigInt(32))) {
2130
+ received = addNumericalSeparator(received)
2131
+ }
2132
+ received += 'n'
2133
+ }
2134
+ msg += ` It must be ${range}. Received ${received}`
2135
+ return msg
2136
+ }, RangeError)
2137
+
2138
+ function addNumericalSeparator(val) {
2139
+ let res = ''
2140
+ let i = val.length
2141
+ const start = val[0] === '-' ? 1 : 0
2142
+ for (; i >= start + 4; i -= 3) {
2143
+ res = `_${val.slice(i - 3, i)}${res}`
2144
+ }
2145
+ return `${val.slice(0, i)}${res}`
2146
+ }
2147
+
2148
+ // CHECK FUNCTIONS
2149
+ // ===============
2150
+
2151
+ function checkBounds(buf, offset, byteLength) {
2152
+ validateNumber(offset, 'offset')
2153
+ if (buf[offset] === undefined || buf[offset + byteLength] === undefined) {
2154
+ boundsError(offset, buf.length - (byteLength + 1))
2155
+ }
2156
+ }
2157
+
2158
+ function checkIntBI(value, min, max, buf, offset, byteLength) {
2159
+ if (value > max || value < min) {
2160
+ const n = typeof min === 'bigint' ? 'n' : ''
2161
+ let range
2162
+ if (byteLength > 3) {
2163
+ if (min === 0 || min === BigInt(0)) {
2164
+ range = `>= 0${n} and < 2${n} ** ${(byteLength + 1) * 8}${n}`
2165
+ } else {
2166
+ range = `>= -(2${n} ** ${(byteLength + 1) * 8 - 1}${n}) and < 2 ** ` +
2167
+ `${(byteLength + 1) * 8 - 1}${n}`
2168
+ }
2169
+ } else {
2170
+ range = `>= ${min}${n} and <= ${max}${n}`
2171
+ }
2172
+ throw new errors.ERR_OUT_OF_RANGE('value', range, value)
2173
+ }
2174
+ checkBounds(buf, offset, byteLength)
2175
+ }
2176
+
2177
+ function validateNumber(value, name) {
2178
+ if (typeof value !== 'number') {
2179
+ throw new errors.ERR_INVALID_ARG_TYPE(name, 'number', value)
2180
+ }
2181
+ }
2182
+
2183
+ function boundsError(value, length, type) {
2184
+ if (Math.floor(value) !== value) {
2185
+ validateNumber(value, type)
2186
+ throw new errors.ERR_OUT_OF_RANGE(type || 'offset', 'an integer', value)
2187
+ }
2188
+
2189
+ if (length < 0) {
2190
+ throw new errors.ERR_BUFFER_OUT_OF_BOUNDS()
2191
+ }
2192
+
2193
+ throw new errors.ERR_OUT_OF_RANGE(type || 'offset',
2194
+ `>= ${type ? 1 : 0} and <= ${length}`,
2195
+ value)
2196
+ }
2197
+
2198
+ // HELPER FUNCTIONS
2199
+ // ================
2200
+
2201
+ const INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g
2202
+
2203
+ function base64clean(str) {
2204
+ // Node takes equal signs as end of the Base64 encoding
2205
+ str = str.split('=')[0]
2206
+ // Node strips out invalid characters like \n and \t from the string, base64-js does not
2207
+ str = str.trim().replace(INVALID_BASE64_RE, '')
2208
+ // Node converts strings with length < 2 to ''
2209
+ if (str.length < 2) return ''
2210
+ // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not
2211
+ while (str.length % 4 !== 0) {
2212
+ str = str + '='
2213
+ }
2214
+ return str
2215
+ }
2216
+
2217
+ function utf8ToBytes(string, units) {
2218
+ units = units || Infinity
2219
+ let codePoint
2220
+ const length = string.length
2221
+ let leadSurrogate = null
2222
+ const bytes = []
2223
+
2224
+ for (let i = 0; i < length; ++i) {
2225
+ codePoint = string.charCodeAt(i)
2226
+
2227
+ // is surrogate component
2228
+ if (codePoint > 0xD7FF && codePoint < 0xE000) {
2229
+ // last char was a lead
2230
+ if (!leadSurrogate) {
2231
+ // no lead yet
2232
+ if (codePoint > 0xDBFF) {
2233
+ // unexpected trail
2234
+ if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
2235
+ continue
2236
+ } else if (i + 1 === length) {
2237
+ // unpaired lead
2238
+ if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
2239
+ continue
2240
+ }
2241
+
2242
+ // valid lead
2243
+ leadSurrogate = codePoint
2244
+
2245
+ continue
2246
+ }
2247
+
2248
+ // 2 leads in a row
2249
+ if (codePoint < 0xDC00) {
2250
+ if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
2251
+ leadSurrogate = codePoint
2252
+ continue
2253
+ }
2254
+
2255
+ // valid surrogate pair
2256
+ codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000
2257
+ } else if (leadSurrogate) {
2258
+ // valid bmp char, but last char was a lead
2259
+ if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
2260
+ }
2261
+
2262
+ leadSurrogate = null
2263
+
2264
+ // encode utf8
2265
+ if (codePoint < 0x80) {
2266
+ if ((units -= 1) < 0) break
2267
+ bytes.push(codePoint)
2268
+ } else if (codePoint < 0x800) {
2269
+ if ((units -= 2) < 0) break
2270
+ bytes.push(
2271
+ codePoint >> 0x6 | 0xC0,
2272
+ codePoint & 0x3F | 0x80
2273
+ )
2274
+ } else if (codePoint < 0x10000) {
2275
+ if ((units -= 3) < 0) break
2276
+ bytes.push(
2277
+ codePoint >> 0xC | 0xE0,
2278
+ codePoint >> 0x6 & 0x3F | 0x80,
2279
+ codePoint & 0x3F | 0x80
2280
+ )
2281
+ } else if (codePoint < 0x110000) {
2282
+ if ((units -= 4) < 0) break
2283
+ bytes.push(
2284
+ codePoint >> 0x12 | 0xF0,
2285
+ codePoint >> 0xC & 0x3F | 0x80,
2286
+ codePoint >> 0x6 & 0x3F | 0x80,
2287
+ codePoint & 0x3F | 0x80
2288
+ )
2289
+ } else {
2290
+ throw new Error('Invalid code point')
2291
+ }
2292
+ }
2293
+
2294
+ return bytes
2295
+ }
2296
+
2297
+ function asciiToBytes(str) {
2298
+ const byteArray = []
2299
+ for (let i = 0; i < str.length; ++i) {
2300
+ // Node's code seems to be doing this and not & 0x7F..
2301
+ byteArray.push(str.charCodeAt(i) & 0xFF)
2302
+ }
2303
+ return byteArray
2304
+ }
2305
+
2306
+ function utf16leToBytes(str, units) {
2307
+ let c, hi, lo
2308
+ const byteArray = []
2309
+ for (let i = 0; i < str.length; ++i) {
2310
+ if ((units -= 2) < 0) break
2311
+
2312
+ c = str.charCodeAt(i)
2313
+ hi = c >> 8
2314
+ lo = c % 256
2315
+ byteArray.push(lo)
2316
+ byteArray.push(hi)
2317
+ }
2318
+
2319
+ return byteArray
2320
+ }
2321
+
2322
+ function base64ToBytes(str) {
2323
+ return base64.toByteArray(base64clean(str))
2324
+ }
2325
+
2326
+ function blitBuffer(src, dst, offset, length) {
2327
+ let i
2328
+ for (i = 0; i < length; ++i) {
2329
+ if ((i + offset >= dst.length) || (i >= src.length)) break
2330
+ dst[i + offset] = src[i]
2331
+ }
2332
+ return i
2333
+ }
2334
+
2335
+ // ArrayBuffer or Uint8Array objects from other contexts (i.e. iframes) do not pass
2336
+ // the `instanceof` check but they should be treated as of that type.
2337
+ // See: https://github.com/feross/buffer/issues/166
2338
+ function isInstance(obj, type) {
2339
+ return obj instanceof type ||
2340
+ (obj != null && obj.constructor != null && obj.constructor.name != null &&
2341
+ obj.constructor.name === type.name)
2342
+ }
2343
+ function numberIsNaN(obj) {
2344
+ // For IE11 support
2345
+ return obj !== obj // eslint-disable-line no-self-compare
2346
+ }
2347
+
2348
+ // Create lookup table for `toString('hex')`
2349
+ // See: https://github.com/feross/buffer/issues/219
2350
+ const hexSliceLookupTable = (function () {
2351
+ const alphabet = '0123456789abcdef'
2352
+ const table = new Array(256)
2353
+ for (let i = 0; i < 16; ++i) {
2354
+ const i16 = i * 16
2355
+ for (let j = 0; j < 16; ++j) {
2356
+ table[i16 + j] = alphabet[i] + alphabet[j]
2357
+ }
2358
+ }
2359
+ return table
2360
+ })()
2361
+
2362
+ // Return not function with Error if BigInt not supported
2363
+ function defineBigIntMethod(fn) {
2364
+ return typeof BigInt === 'undefined' ? BufferBigIntNotDefined : fn
2365
+ }
2366
+
2367
+ function BufferBigIntNotDefined() {
2368
+ throw new Error('BigInt not supported')
2369
+ }
2370
+ })();