@dxos/esbuild-plugins 0.1.14

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