@powersync/common 0.0.0-dev-20251106124255 → 0.0.0-dev-20251201150812

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 (31) hide show
  1. package/dist/bundle.cjs +4560 -184
  2. package/dist/bundle.cjs.map +1 -1
  3. package/dist/bundle.mjs +4560 -184
  4. package/dist/bundle.mjs.map +1 -1
  5. package/dist/bundle.node.cjs +42 -17
  6. package/dist/bundle.node.cjs.map +1 -1
  7. package/dist/bundle.node.mjs +42 -17
  8. package/dist/bundle.node.mjs.map +1 -1
  9. package/dist/index.d.cts +226 -162
  10. package/lib/client/ConnectionManager.d.ts +1 -1
  11. package/lib/client/ConnectionManager.js.map +1 -1
  12. package/lib/client/sync/bucket/SqliteBucketStorage.js +2 -2
  13. package/lib/client/sync/bucket/SqliteBucketStorage.js.map +1 -1
  14. package/lib/client/sync/stream/AbstractRemote.js +10 -4
  15. package/lib/client/sync/stream/AbstractRemote.js.map +1 -1
  16. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +4 -4
  17. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +1 -1
  18. package/lib/client/triggers/TriggerManager.d.ts +71 -12
  19. package/lib/client/triggers/TriggerManagerImpl.js +10 -5
  20. package/lib/client/triggers/TriggerManagerImpl.js.map +1 -1
  21. package/lib/db/crud/SyncStatus.d.ts +7 -2
  22. package/lib/db/crud/SyncStatus.js +15 -1
  23. package/lib/db/crud/SyncStatus.js.map +1 -1
  24. package/package.json +2 -2
  25. package/src/client/ConnectionManager.ts +1 -1
  26. package/src/client/sync/bucket/SqliteBucketStorage.ts +2 -2
  27. package/src/client/sync/stream/AbstractRemote.ts +15 -5
  28. package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +5 -6
  29. package/src/client/triggers/TriggerManager.ts +79 -12
  30. package/src/client/triggers/TriggerManagerImpl.ts +12 -6
  31. package/src/db/crud/SyncStatus.ts +18 -3
package/dist/bundle.cjs CHANGED
@@ -1,13 +1,164 @@
1
1
  'use strict';
2
2
 
3
3
  var asyncMutex = require('async-mutex');
4
- var eventIterator = require('event-iterator');
5
- var _ = require('buffer/');
6
4
 
7
5
  function getDefaultExportFromCjs (x) {
8
6
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
9
7
  }
10
8
 
9
+ var dom = {};
10
+
11
+ var eventIterator = {};
12
+
13
+ var hasRequiredEventIterator;
14
+
15
+ function requireEventIterator () {
16
+ if (hasRequiredEventIterator) return eventIterator;
17
+ hasRequiredEventIterator = 1;
18
+ Object.defineProperty(eventIterator, "__esModule", { value: true });
19
+ class EventQueue {
20
+ constructor() {
21
+ this.pullQueue = [];
22
+ this.pushQueue = [];
23
+ this.eventHandlers = {};
24
+ this.isPaused = false;
25
+ this.isStopped = false;
26
+ }
27
+ push(value) {
28
+ if (this.isStopped)
29
+ return;
30
+ const resolution = { value, done: false };
31
+ if (this.pullQueue.length) {
32
+ const placeholder = this.pullQueue.shift();
33
+ if (placeholder)
34
+ placeholder.resolve(resolution);
35
+ }
36
+ else {
37
+ this.pushQueue.push(Promise.resolve(resolution));
38
+ if (this.highWaterMark !== undefined &&
39
+ this.pushQueue.length >= this.highWaterMark &&
40
+ !this.isPaused) {
41
+ this.isPaused = true;
42
+ if (this.eventHandlers.highWater) {
43
+ this.eventHandlers.highWater();
44
+ }
45
+ else if (console) {
46
+ console.warn(`EventIterator queue reached ${this.pushQueue.length} items`);
47
+ }
48
+ }
49
+ }
50
+ }
51
+ stop() {
52
+ if (this.isStopped)
53
+ return;
54
+ this.isStopped = true;
55
+ this.remove();
56
+ for (const placeholder of this.pullQueue) {
57
+ placeholder.resolve({ value: undefined, done: true });
58
+ }
59
+ this.pullQueue.length = 0;
60
+ }
61
+ fail(error) {
62
+ if (this.isStopped)
63
+ return;
64
+ this.isStopped = true;
65
+ this.remove();
66
+ if (this.pullQueue.length) {
67
+ for (const placeholder of this.pullQueue) {
68
+ placeholder.reject(error);
69
+ }
70
+ this.pullQueue.length = 0;
71
+ }
72
+ else {
73
+ const rejection = Promise.reject(error);
74
+ /* Attach error handler to avoid leaking an unhandled promise rejection. */
75
+ rejection.catch(() => { });
76
+ this.pushQueue.push(rejection);
77
+ }
78
+ }
79
+ remove() {
80
+ Promise.resolve().then(() => {
81
+ if (this.removeCallback)
82
+ this.removeCallback();
83
+ });
84
+ }
85
+ [Symbol.asyncIterator]() {
86
+ return {
87
+ next: (value) => {
88
+ const result = this.pushQueue.shift();
89
+ if (result) {
90
+ if (this.lowWaterMark !== undefined &&
91
+ this.pushQueue.length <= this.lowWaterMark &&
92
+ this.isPaused) {
93
+ this.isPaused = false;
94
+ if (this.eventHandlers.lowWater) {
95
+ this.eventHandlers.lowWater();
96
+ }
97
+ }
98
+ return result;
99
+ }
100
+ else if (this.isStopped) {
101
+ return Promise.resolve({ value: undefined, done: true });
102
+ }
103
+ else {
104
+ return new Promise((resolve, reject) => {
105
+ this.pullQueue.push({ resolve, reject });
106
+ });
107
+ }
108
+ },
109
+ return: () => {
110
+ this.isStopped = true;
111
+ this.pushQueue.length = 0;
112
+ this.remove();
113
+ return Promise.resolve({ value: undefined, done: true });
114
+ },
115
+ };
116
+ }
117
+ }
118
+ class EventIterator {
119
+ constructor(listen, { highWaterMark = 100, lowWaterMark = 1 } = {}) {
120
+ const queue = new EventQueue();
121
+ queue.highWaterMark = highWaterMark;
122
+ queue.lowWaterMark = lowWaterMark;
123
+ queue.removeCallback =
124
+ listen({
125
+ push: value => queue.push(value),
126
+ stop: () => queue.stop(),
127
+ fail: error => queue.fail(error),
128
+ on: (event, fn) => {
129
+ queue.eventHandlers[event] = fn;
130
+ },
131
+ }) || (() => { });
132
+ this[Symbol.asyncIterator] = () => queue[Symbol.asyncIterator]();
133
+ Object.freeze(this);
134
+ }
135
+ }
136
+ eventIterator.EventIterator = EventIterator;
137
+ eventIterator.default = EventIterator;
138
+ return eventIterator;
139
+ }
140
+
141
+ var hasRequiredDom;
142
+
143
+ function requireDom () {
144
+ if (hasRequiredDom) return dom;
145
+ hasRequiredDom = 1;
146
+ Object.defineProperty(dom, "__esModule", { value: true });
147
+ const event_iterator_1 = requireEventIterator();
148
+ dom.EventIterator = event_iterator_1.EventIterator;
149
+ function subscribe(event, options, evOptions) {
150
+ return new event_iterator_1.EventIterator(({ push }) => {
151
+ this.addEventListener(event, push, options);
152
+ return () => this.removeEventListener(event, push, options);
153
+ }, evOptions);
154
+ }
155
+ dom.subscribe = subscribe;
156
+ dom.default = event_iterator_1.EventIterator;
157
+ return dom;
158
+ }
159
+
160
+ var domExports = requireDom();
161
+
11
162
  var logger$1 = {exports: {}};
12
163
 
13
164
  /*!
@@ -571,12 +722,26 @@ class SyncStatus {
571
722
  return {
572
723
  connected: this.connected,
573
724
  connecting: this.connecting,
574
- dataFlow: this.dataFlowStatus,
725
+ dataFlow: {
726
+ ...this.dataFlowStatus,
727
+ uploadError: this.serializeError(this.dataFlowStatus.uploadError),
728
+ downloadError: this.serializeError(this.dataFlowStatus.downloadError)
729
+ },
575
730
  lastSyncedAt: this.lastSyncedAt,
576
731
  hasSynced: this.hasSynced,
577
732
  priorityStatusEntries: this.priorityStatusEntries
578
733
  };
579
734
  }
735
+ serializeError(error) {
736
+ if (typeof error == 'undefined') {
737
+ return undefined;
738
+ }
739
+ return {
740
+ name: error.name,
741
+ message: error.message,
742
+ stack: error.stack
743
+ };
744
+ }
580
745
  static comparePriorities(a, b) {
581
746
  return b.priority - a.priority; // Reverse because higher priorities have lower numbers
582
747
  }
@@ -1780,147 +1945,4347 @@ class CrudEntry {
1780
1945
  }
1781
1946
  }
1782
1947
 
1783
- class CrudTransaction extends CrudBatch {
1784
- crud;
1785
- complete;
1786
- transactionId;
1787
- constructor(
1788
- /**
1789
- * List of client-side changes.
1790
- */
1791
- crud,
1792
- /**
1793
- * Call to remove the changes from the local queue, once successfully uploaded.
1794
- */
1795
- complete,
1796
- /**
1797
- * If null, this contains a list of changes recorded without an explicit transaction associated.
1798
- */
1799
- transactionId) {
1800
- super(crud, false, complete);
1801
- this.crud = crud;
1802
- this.complete = complete;
1803
- this.transactionId = transactionId;
1804
- }
1805
- }
1948
+ class CrudTransaction extends CrudBatch {
1949
+ crud;
1950
+ complete;
1951
+ transactionId;
1952
+ constructor(
1953
+ /**
1954
+ * List of client-side changes.
1955
+ */
1956
+ crud,
1957
+ /**
1958
+ * Call to remove the changes from the local queue, once successfully uploaded.
1959
+ */
1960
+ complete,
1961
+ /**
1962
+ * If null, this contains a list of changes recorded without an explicit transaction associated.
1963
+ */
1964
+ transactionId) {
1965
+ super(crud, false, complete);
1966
+ this.crud = crud;
1967
+ this.complete = complete;
1968
+ this.transactionId = transactionId;
1969
+ }
1970
+ }
1971
+
1972
+ /**
1973
+ * Calls to Abortcontroller.abort(reason: any) will result in the
1974
+ * `reason` being thrown. This is not necessarily an error,
1975
+ * but extends error for better logging purposes.
1976
+ */
1977
+ class AbortOperation extends Error {
1978
+ reason;
1979
+ constructor(reason) {
1980
+ super(reason);
1981
+ this.reason = reason;
1982
+ // Set the prototype explicitly
1983
+ Object.setPrototypeOf(this, AbortOperation.prototype);
1984
+ // Capture stack trace
1985
+ if (Error.captureStackTrace) {
1986
+ Error.captureStackTrace(this, AbortOperation);
1987
+ }
1988
+ }
1989
+ }
1990
+
1991
+ exports.OpTypeEnum = void 0;
1992
+ (function (OpTypeEnum) {
1993
+ OpTypeEnum[OpTypeEnum["CLEAR"] = 1] = "CLEAR";
1994
+ OpTypeEnum[OpTypeEnum["MOVE"] = 2] = "MOVE";
1995
+ OpTypeEnum[OpTypeEnum["PUT"] = 3] = "PUT";
1996
+ OpTypeEnum[OpTypeEnum["REMOVE"] = 4] = "REMOVE";
1997
+ })(exports.OpTypeEnum || (exports.OpTypeEnum = {}));
1998
+ /**
1999
+ * Used internally for sync buckets.
2000
+ */
2001
+ class OpType {
2002
+ value;
2003
+ static fromJSON(jsonValue) {
2004
+ return new OpType(exports.OpTypeEnum[jsonValue]);
2005
+ }
2006
+ constructor(value) {
2007
+ this.value = value;
2008
+ }
2009
+ toJSON() {
2010
+ return Object.entries(exports.OpTypeEnum).find(([, value]) => value === this.value)[0];
2011
+ }
2012
+ }
2013
+
2014
+ class OplogEntry {
2015
+ op_id;
2016
+ op;
2017
+ checksum;
2018
+ subkey;
2019
+ object_type;
2020
+ object_id;
2021
+ data;
2022
+ static fromRow(row) {
2023
+ return new OplogEntry(row.op_id, OpType.fromJSON(row.op), row.checksum, row.subkey, row.object_type, row.object_id, row.data);
2024
+ }
2025
+ constructor(op_id, op, checksum, subkey, object_type, object_id, data) {
2026
+ this.op_id = op_id;
2027
+ this.op = op;
2028
+ this.checksum = checksum;
2029
+ this.subkey = subkey;
2030
+ this.object_type = object_type;
2031
+ this.object_id = object_id;
2032
+ this.data = data;
2033
+ }
2034
+ toJSON(fixedKeyEncoding = false) {
2035
+ return {
2036
+ op_id: this.op_id,
2037
+ op: this.op.toJSON(),
2038
+ object_type: this.object_type,
2039
+ object_id: this.object_id,
2040
+ checksum: this.checksum,
2041
+ data: this.data,
2042
+ // Older versions of the JS SDK used to always JSON.stringify here. That has always been wrong,
2043
+ // but we need to migrate gradually to not break existing databases.
2044
+ subkey: fixedKeyEncoding ? this.subkey : JSON.stringify(this.subkey)
2045
+ };
2046
+ }
2047
+ }
2048
+
2049
+ class SyncDataBucket {
2050
+ bucket;
2051
+ data;
2052
+ has_more;
2053
+ after;
2054
+ next_after;
2055
+ static fromRow(row) {
2056
+ return new SyncDataBucket(row.bucket, row.data.map((entry) => OplogEntry.fromRow(entry)), row.has_more ?? false, row.after, row.next_after);
2057
+ }
2058
+ constructor(bucket, data,
2059
+ /**
2060
+ * True if the response does not contain all the data for this bucket, and another request must be made.
2061
+ */
2062
+ has_more,
2063
+ /**
2064
+ * The `after` specified in the request.
2065
+ */
2066
+ after,
2067
+ /**
2068
+ * Use this for the next request.
2069
+ */
2070
+ next_after) {
2071
+ this.bucket = bucket;
2072
+ this.data = data;
2073
+ this.has_more = has_more;
2074
+ this.after = after;
2075
+ this.next_after = next_after;
2076
+ }
2077
+ toJSON(fixedKeyEncoding = false) {
2078
+ return {
2079
+ bucket: this.bucket,
2080
+ has_more: this.has_more,
2081
+ after: this.after,
2082
+ next_after: this.next_after,
2083
+ data: this.data.map((entry) => entry.toJSON(fixedKeyEncoding))
2084
+ };
2085
+ }
2086
+ }
2087
+
2088
+ var buffer$1 = {};
2089
+
2090
+ var base64Js = {};
2091
+
2092
+ var hasRequiredBase64Js;
2093
+
2094
+ function requireBase64Js () {
2095
+ if (hasRequiredBase64Js) return base64Js;
2096
+ hasRequiredBase64Js = 1;
2097
+
2098
+ base64Js.byteLength = byteLength;
2099
+ base64Js.toByteArray = toByteArray;
2100
+ base64Js.fromByteArray = fromByteArray;
2101
+
2102
+ var lookup = [];
2103
+ var revLookup = [];
2104
+ var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array;
2105
+
2106
+ var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
2107
+ for (var i = 0, len = code.length; i < len; ++i) {
2108
+ lookup[i] = code[i];
2109
+ revLookup[code.charCodeAt(i)] = i;
2110
+ }
2111
+
2112
+ // Support decoding URL-safe base64 strings, as Node.js does.
2113
+ // See: https://en.wikipedia.org/wiki/Base64#URL_applications
2114
+ revLookup['-'.charCodeAt(0)] = 62;
2115
+ revLookup['_'.charCodeAt(0)] = 63;
2116
+
2117
+ function getLens (b64) {
2118
+ var len = b64.length;
2119
+
2120
+ if (len % 4 > 0) {
2121
+ throw new Error('Invalid string. Length must be a multiple of 4')
2122
+ }
2123
+
2124
+ // Trim off extra bytes after placeholder bytes are found
2125
+ // See: https://github.com/beatgammit/base64-js/issues/42
2126
+ var validLen = b64.indexOf('=');
2127
+ if (validLen === -1) validLen = len;
2128
+
2129
+ var placeHoldersLen = validLen === len
2130
+ ? 0
2131
+ : 4 - (validLen % 4);
2132
+
2133
+ return [validLen, placeHoldersLen]
2134
+ }
2135
+
2136
+ // base64 is 4/3 + up to two characters of the original data
2137
+ function byteLength (b64) {
2138
+ var lens = getLens(b64);
2139
+ var validLen = lens[0];
2140
+ var placeHoldersLen = lens[1];
2141
+ return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen
2142
+ }
2143
+
2144
+ function _byteLength (b64, validLen, placeHoldersLen) {
2145
+ return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen
2146
+ }
2147
+
2148
+ function toByteArray (b64) {
2149
+ var tmp;
2150
+ var lens = getLens(b64);
2151
+ var validLen = lens[0];
2152
+ var placeHoldersLen = lens[1];
2153
+
2154
+ var arr = new Arr(_byteLength(b64, validLen, placeHoldersLen));
2155
+
2156
+ var curByte = 0;
2157
+
2158
+ // if there are placeholders, only get up to the last complete 4 chars
2159
+ var len = placeHoldersLen > 0
2160
+ ? validLen - 4
2161
+ : validLen;
2162
+
2163
+ var i;
2164
+ for (i = 0; i < len; i += 4) {
2165
+ tmp =
2166
+ (revLookup[b64.charCodeAt(i)] << 18) |
2167
+ (revLookup[b64.charCodeAt(i + 1)] << 12) |
2168
+ (revLookup[b64.charCodeAt(i + 2)] << 6) |
2169
+ revLookup[b64.charCodeAt(i + 3)];
2170
+ arr[curByte++] = (tmp >> 16) & 0xFF;
2171
+ arr[curByte++] = (tmp >> 8) & 0xFF;
2172
+ arr[curByte++] = tmp & 0xFF;
2173
+ }
2174
+
2175
+ if (placeHoldersLen === 2) {
2176
+ tmp =
2177
+ (revLookup[b64.charCodeAt(i)] << 2) |
2178
+ (revLookup[b64.charCodeAt(i + 1)] >> 4);
2179
+ arr[curByte++] = tmp & 0xFF;
2180
+ }
2181
+
2182
+ if (placeHoldersLen === 1) {
2183
+ tmp =
2184
+ (revLookup[b64.charCodeAt(i)] << 10) |
2185
+ (revLookup[b64.charCodeAt(i + 1)] << 4) |
2186
+ (revLookup[b64.charCodeAt(i + 2)] >> 2);
2187
+ arr[curByte++] = (tmp >> 8) & 0xFF;
2188
+ arr[curByte++] = tmp & 0xFF;
2189
+ }
2190
+
2191
+ return arr
2192
+ }
2193
+
2194
+ function tripletToBase64 (num) {
2195
+ return lookup[num >> 18 & 0x3F] +
2196
+ lookup[num >> 12 & 0x3F] +
2197
+ lookup[num >> 6 & 0x3F] +
2198
+ lookup[num & 0x3F]
2199
+ }
2200
+
2201
+ function encodeChunk (uint8, start, end) {
2202
+ var tmp;
2203
+ var output = [];
2204
+ for (var i = start; i < end; i += 3) {
2205
+ tmp =
2206
+ ((uint8[i] << 16) & 0xFF0000) +
2207
+ ((uint8[i + 1] << 8) & 0xFF00) +
2208
+ (uint8[i + 2] & 0xFF);
2209
+ output.push(tripletToBase64(tmp));
2210
+ }
2211
+ return output.join('')
2212
+ }
2213
+
2214
+ function fromByteArray (uint8) {
2215
+ var tmp;
2216
+ var len = uint8.length;
2217
+ var extraBytes = len % 3; // if we have 1 byte left, pad 2 bytes
2218
+ var parts = [];
2219
+ var maxChunkLength = 16383; // must be multiple of 3
2220
+
2221
+ // go through the array every three bytes, we'll deal with trailing stuff later
2222
+ for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
2223
+ parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength)));
2224
+ }
2225
+
2226
+ // pad the end with zeros, but make sure to not forget the extra bytes
2227
+ if (extraBytes === 1) {
2228
+ tmp = uint8[len - 1];
2229
+ parts.push(
2230
+ lookup[tmp >> 2] +
2231
+ lookup[(tmp << 4) & 0x3F] +
2232
+ '=='
2233
+ );
2234
+ } else if (extraBytes === 2) {
2235
+ tmp = (uint8[len - 2] << 8) + uint8[len - 1];
2236
+ parts.push(
2237
+ lookup[tmp >> 10] +
2238
+ lookup[(tmp >> 4) & 0x3F] +
2239
+ lookup[(tmp << 2) & 0x3F] +
2240
+ '='
2241
+ );
2242
+ }
2243
+
2244
+ return parts.join('')
2245
+ }
2246
+ return base64Js;
2247
+ }
2248
+
2249
+ var ieee754 = {};
2250
+
2251
+ /*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */
2252
+
2253
+ var hasRequiredIeee754;
2254
+
2255
+ function requireIeee754 () {
2256
+ if (hasRequiredIeee754) return ieee754;
2257
+ hasRequiredIeee754 = 1;
2258
+ ieee754.read = function (buffer, offset, isLE, mLen, nBytes) {
2259
+ var e, m;
2260
+ var eLen = (nBytes * 8) - mLen - 1;
2261
+ var eMax = (1 << eLen) - 1;
2262
+ var eBias = eMax >> 1;
2263
+ var nBits = -7;
2264
+ var i = isLE ? (nBytes - 1) : 0;
2265
+ var d = isLE ? -1 : 1;
2266
+ var s = buffer[offset + i];
2267
+
2268
+ i += d;
2269
+
2270
+ e = s & ((1 << (-nBits)) - 1);
2271
+ s >>= (-nBits);
2272
+ nBits += eLen;
2273
+ for (; nBits > 0; e = (e * 256) + buffer[offset + i], i += d, nBits -= 8) {}
2274
+
2275
+ m = e & ((1 << (-nBits)) - 1);
2276
+ e >>= (-nBits);
2277
+ nBits += mLen;
2278
+ for (; nBits > 0; m = (m * 256) + buffer[offset + i], i += d, nBits -= 8) {}
2279
+
2280
+ if (e === 0) {
2281
+ e = 1 - eBias;
2282
+ } else if (e === eMax) {
2283
+ return m ? NaN : ((s ? -1 : 1) * Infinity)
2284
+ } else {
2285
+ m = m + Math.pow(2, mLen);
2286
+ e = e - eBias;
2287
+ }
2288
+ return (s ? -1 : 1) * m * Math.pow(2, e - mLen)
2289
+ };
2290
+
2291
+ ieee754.write = function (buffer, value, offset, isLE, mLen, nBytes) {
2292
+ var e, m, c;
2293
+ var eLen = (nBytes * 8) - mLen - 1;
2294
+ var eMax = (1 << eLen) - 1;
2295
+ var eBias = eMax >> 1;
2296
+ var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0);
2297
+ var i = isLE ? 0 : (nBytes - 1);
2298
+ var d = isLE ? 1 : -1;
2299
+ var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0;
2300
+
2301
+ value = Math.abs(value);
2302
+
2303
+ if (isNaN(value) || value === Infinity) {
2304
+ m = isNaN(value) ? 1 : 0;
2305
+ e = eMax;
2306
+ } else {
2307
+ e = Math.floor(Math.log(value) / Math.LN2);
2308
+ if (value * (c = Math.pow(2, -e)) < 1) {
2309
+ e--;
2310
+ c *= 2;
2311
+ }
2312
+ if (e + eBias >= 1) {
2313
+ value += rt / c;
2314
+ } else {
2315
+ value += rt * Math.pow(2, 1 - eBias);
2316
+ }
2317
+ if (value * c >= 2) {
2318
+ e++;
2319
+ c /= 2;
2320
+ }
2321
+
2322
+ if (e + eBias >= eMax) {
2323
+ m = 0;
2324
+ e = eMax;
2325
+ } else if (e + eBias >= 1) {
2326
+ m = ((value * c) - 1) * Math.pow(2, mLen);
2327
+ e = e + eBias;
2328
+ } else {
2329
+ m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
2330
+ e = 0;
2331
+ }
2332
+ }
2333
+
2334
+ for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {}
2335
+
2336
+ e = (e << mLen) | m;
2337
+ eLen += mLen;
2338
+ for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {}
2339
+
2340
+ buffer[offset + i - d] |= s * 128;
2341
+ };
2342
+ return ieee754;
2343
+ }
2344
+
2345
+ /*!
2346
+ * The buffer module from node.js, for the browser.
2347
+ *
2348
+ * @author Feross Aboukhadijeh <https://feross.org>
2349
+ * @license MIT
2350
+ */
2351
+
2352
+ var hasRequiredBuffer$1;
2353
+
2354
+ function requireBuffer$1 () {
2355
+ if (hasRequiredBuffer$1) return buffer$1;
2356
+ hasRequiredBuffer$1 = 1;
2357
+ (function (exports) {
2358
+
2359
+ const base64 = requireBase64Js();
2360
+ const ieee754 = requireIeee754();
2361
+ const customInspectSymbol =
2362
+ (typeof Symbol === 'function' && typeof Symbol['for'] === 'function') // eslint-disable-line dot-notation
2363
+ ? Symbol['for']('nodejs.util.inspect.custom') // eslint-disable-line dot-notation
2364
+ : null;
2365
+
2366
+ exports.Buffer = Buffer;
2367
+ exports.SlowBuffer = SlowBuffer;
2368
+ exports.INSPECT_MAX_BYTES = 50;
2369
+
2370
+ const K_MAX_LENGTH = 0x7fffffff;
2371
+ exports.kMaxLength = K_MAX_LENGTH;
2372
+
2373
+ /**
2374
+ * If `Buffer.TYPED_ARRAY_SUPPORT`:
2375
+ * === true Use Uint8Array implementation (fastest)
2376
+ * === false Print warning and recommend using `buffer` v4.x which has an Object
2377
+ * implementation (most compatible, even IE6)
2378
+ *
2379
+ * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+,
2380
+ * Opera 11.6+, iOS 4.2+.
2381
+ *
2382
+ * We report that the browser does not support typed arrays if the are not subclassable
2383
+ * using __proto__. Firefox 4-29 lacks support for adding new properties to `Uint8Array`
2384
+ * (See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438). IE 10 lacks support
2385
+ * for __proto__ and has a buggy typed array implementation.
2386
+ */
2387
+ Buffer.TYPED_ARRAY_SUPPORT = typedArraySupport();
2388
+
2389
+ if (!Buffer.TYPED_ARRAY_SUPPORT && typeof console !== 'undefined' &&
2390
+ typeof console.error === 'function') {
2391
+ console.error(
2392
+ 'This browser lacks typed array (Uint8Array) support which is required by ' +
2393
+ '`buffer` v5.x. Use `buffer` v4.x if you require old browser support.'
2394
+ );
2395
+ }
2396
+
2397
+ function typedArraySupport () {
2398
+ // Can typed array instances can be augmented?
2399
+ try {
2400
+ const arr = new Uint8Array(1);
2401
+ const proto = { foo: function () { return 42 } };
2402
+ Object.setPrototypeOf(proto, Uint8Array.prototype);
2403
+ Object.setPrototypeOf(arr, proto);
2404
+ return arr.foo() === 42
2405
+ } catch (e) {
2406
+ return false
2407
+ }
2408
+ }
2409
+
2410
+ Object.defineProperty(Buffer.prototype, 'parent', {
2411
+ enumerable: true,
2412
+ get: function () {
2413
+ if (!Buffer.isBuffer(this)) return undefined
2414
+ return this.buffer
2415
+ }
2416
+ });
2417
+
2418
+ Object.defineProperty(Buffer.prototype, 'offset', {
2419
+ enumerable: true,
2420
+ get: function () {
2421
+ if (!Buffer.isBuffer(this)) return undefined
2422
+ return this.byteOffset
2423
+ }
2424
+ });
2425
+
2426
+ function createBuffer (length) {
2427
+ if (length > K_MAX_LENGTH) {
2428
+ throw new RangeError('The value "' + length + '" is invalid for option "size"')
2429
+ }
2430
+ // Return an augmented `Uint8Array` instance
2431
+ const buf = new Uint8Array(length);
2432
+ Object.setPrototypeOf(buf, Buffer.prototype);
2433
+ return buf
2434
+ }
2435
+
2436
+ /**
2437
+ * The Buffer constructor returns instances of `Uint8Array` that have their
2438
+ * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of
2439
+ * `Uint8Array`, so the returned instances will have all the node `Buffer` methods
2440
+ * and the `Uint8Array` methods. Square bracket notation works as expected -- it
2441
+ * returns a single octet.
2442
+ *
2443
+ * The `Uint8Array` prototype remains unmodified.
2444
+ */
2445
+
2446
+ function Buffer (arg, encodingOrOffset, length) {
2447
+ // Common case.
2448
+ if (typeof arg === 'number') {
2449
+ if (typeof encodingOrOffset === 'string') {
2450
+ throw new TypeError(
2451
+ 'The "string" argument must be of type string. Received type number'
2452
+ )
2453
+ }
2454
+ return allocUnsafe(arg)
2455
+ }
2456
+ return from(arg, encodingOrOffset, length)
2457
+ }
2458
+
2459
+ Buffer.poolSize = 8192; // not used by this implementation
2460
+
2461
+ function from (value, encodingOrOffset, length) {
2462
+ if (typeof value === 'string') {
2463
+ return fromString(value, encodingOrOffset)
2464
+ }
2465
+
2466
+ if (ArrayBuffer.isView(value)) {
2467
+ return fromArrayView(value)
2468
+ }
2469
+
2470
+ if (value == null) {
2471
+ throw new TypeError(
2472
+ 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' +
2473
+ 'or Array-like Object. Received type ' + (typeof value)
2474
+ )
2475
+ }
2476
+
2477
+ if (isInstance(value, ArrayBuffer) ||
2478
+ (value && isInstance(value.buffer, ArrayBuffer))) {
2479
+ return fromArrayBuffer(value, encodingOrOffset, length)
2480
+ }
2481
+
2482
+ if (typeof SharedArrayBuffer !== 'undefined' &&
2483
+ (isInstance(value, SharedArrayBuffer) ||
2484
+ (value && isInstance(value.buffer, SharedArrayBuffer)))) {
2485
+ return fromArrayBuffer(value, encodingOrOffset, length)
2486
+ }
2487
+
2488
+ if (typeof value === 'number') {
2489
+ throw new TypeError(
2490
+ 'The "value" argument must not be of type number. Received type number'
2491
+ )
2492
+ }
2493
+
2494
+ const valueOf = value.valueOf && value.valueOf();
2495
+ if (valueOf != null && valueOf !== value) {
2496
+ return Buffer.from(valueOf, encodingOrOffset, length)
2497
+ }
2498
+
2499
+ const b = fromObject(value);
2500
+ if (b) return b
2501
+
2502
+ if (typeof Symbol !== 'undefined' && Symbol.toPrimitive != null &&
2503
+ typeof value[Symbol.toPrimitive] === 'function') {
2504
+ return Buffer.from(value[Symbol.toPrimitive]('string'), encodingOrOffset, length)
2505
+ }
2506
+
2507
+ throw new TypeError(
2508
+ 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' +
2509
+ 'or Array-like Object. Received type ' + (typeof value)
2510
+ )
2511
+ }
2512
+
2513
+ /**
2514
+ * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError
2515
+ * if value is a number.
2516
+ * Buffer.from(str[, encoding])
2517
+ * Buffer.from(array)
2518
+ * Buffer.from(buffer)
2519
+ * Buffer.from(arrayBuffer[, byteOffset[, length]])
2520
+ **/
2521
+ Buffer.from = function (value, encodingOrOffset, length) {
2522
+ return from(value, encodingOrOffset, length)
2523
+ };
2524
+
2525
+ // Note: Change prototype *after* Buffer.from is defined to workaround Chrome bug:
2526
+ // https://github.com/feross/buffer/pull/148
2527
+ Object.setPrototypeOf(Buffer.prototype, Uint8Array.prototype);
2528
+ Object.setPrototypeOf(Buffer, Uint8Array);
2529
+
2530
+ function assertSize (size) {
2531
+ if (typeof size !== 'number') {
2532
+ throw new TypeError('"size" argument must be of type number')
2533
+ } else if (size < 0) {
2534
+ throw new RangeError('The value "' + size + '" is invalid for option "size"')
2535
+ }
2536
+ }
2537
+
2538
+ function alloc (size, fill, encoding) {
2539
+ assertSize(size);
2540
+ if (size <= 0) {
2541
+ return createBuffer(size)
2542
+ }
2543
+ if (fill !== undefined) {
2544
+ // Only pay attention to encoding if it's a string. This
2545
+ // prevents accidentally sending in a number that would
2546
+ // be interpreted as a start offset.
2547
+ return typeof encoding === 'string'
2548
+ ? createBuffer(size).fill(fill, encoding)
2549
+ : createBuffer(size).fill(fill)
2550
+ }
2551
+ return createBuffer(size)
2552
+ }
2553
+
2554
+ /**
2555
+ * Creates a new filled Buffer instance.
2556
+ * alloc(size[, fill[, encoding]])
2557
+ **/
2558
+ Buffer.alloc = function (size, fill, encoding) {
2559
+ return alloc(size, fill, encoding)
2560
+ };
2561
+
2562
+ function allocUnsafe (size) {
2563
+ assertSize(size);
2564
+ return createBuffer(size < 0 ? 0 : checked(size) | 0)
2565
+ }
2566
+
2567
+ /**
2568
+ * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance.
2569
+ * */
2570
+ Buffer.allocUnsafe = function (size) {
2571
+ return allocUnsafe(size)
2572
+ };
2573
+ /**
2574
+ * Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance.
2575
+ */
2576
+ Buffer.allocUnsafeSlow = function (size) {
2577
+ return allocUnsafe(size)
2578
+ };
2579
+
2580
+ function fromString (string, encoding) {
2581
+ if (typeof encoding !== 'string' || encoding === '') {
2582
+ encoding = 'utf8';
2583
+ }
2584
+
2585
+ if (!Buffer.isEncoding(encoding)) {
2586
+ throw new TypeError('Unknown encoding: ' + encoding)
2587
+ }
2588
+
2589
+ const length = byteLength(string, encoding) | 0;
2590
+ let buf = createBuffer(length);
2591
+
2592
+ const actual = buf.write(string, encoding);
2593
+
2594
+ if (actual !== length) {
2595
+ // Writing a hex string, for example, that contains invalid characters will
2596
+ // cause everything after the first invalid character to be ignored. (e.g.
2597
+ // 'abxxcd' will be treated as 'ab')
2598
+ buf = buf.slice(0, actual);
2599
+ }
2600
+
2601
+ return buf
2602
+ }
2603
+
2604
+ function fromArrayLike (array) {
2605
+ const length = array.length < 0 ? 0 : checked(array.length) | 0;
2606
+ const buf = createBuffer(length);
2607
+ for (let i = 0; i < length; i += 1) {
2608
+ buf[i] = array[i] & 255;
2609
+ }
2610
+ return buf
2611
+ }
2612
+
2613
+ function fromArrayView (arrayView) {
2614
+ if (isInstance(arrayView, Uint8Array)) {
2615
+ const copy = new Uint8Array(arrayView);
2616
+ return fromArrayBuffer(copy.buffer, copy.byteOffset, copy.byteLength)
2617
+ }
2618
+ return fromArrayLike(arrayView)
2619
+ }
2620
+
2621
+ function fromArrayBuffer (array, byteOffset, length) {
2622
+ if (byteOffset < 0 || array.byteLength < byteOffset) {
2623
+ throw new RangeError('"offset" is outside of buffer bounds')
2624
+ }
2625
+
2626
+ if (array.byteLength < byteOffset + (length || 0)) {
2627
+ throw new RangeError('"length" is outside of buffer bounds')
2628
+ }
2629
+
2630
+ let buf;
2631
+ if (byteOffset === undefined && length === undefined) {
2632
+ buf = new Uint8Array(array);
2633
+ } else if (length === undefined) {
2634
+ buf = new Uint8Array(array, byteOffset);
2635
+ } else {
2636
+ buf = new Uint8Array(array, byteOffset, length);
2637
+ }
2638
+
2639
+ // Return an augmented `Uint8Array` instance
2640
+ Object.setPrototypeOf(buf, Buffer.prototype);
2641
+
2642
+ return buf
2643
+ }
2644
+
2645
+ function fromObject (obj) {
2646
+ if (Buffer.isBuffer(obj)) {
2647
+ const len = checked(obj.length) | 0;
2648
+ const buf = createBuffer(len);
2649
+
2650
+ if (buf.length === 0) {
2651
+ return buf
2652
+ }
2653
+
2654
+ obj.copy(buf, 0, 0, len);
2655
+ return buf
2656
+ }
2657
+
2658
+ if (obj.length !== undefined) {
2659
+ if (typeof obj.length !== 'number' || numberIsNaN(obj.length)) {
2660
+ return createBuffer(0)
2661
+ }
2662
+ return fromArrayLike(obj)
2663
+ }
2664
+
2665
+ if (obj.type === 'Buffer' && Array.isArray(obj.data)) {
2666
+ return fromArrayLike(obj.data)
2667
+ }
2668
+ }
2669
+
2670
+ function checked (length) {
2671
+ // Note: cannot use `length < K_MAX_LENGTH` here because that fails when
2672
+ // length is NaN (which is otherwise coerced to zero.)
2673
+ if (length >= K_MAX_LENGTH) {
2674
+ throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
2675
+ 'size: 0x' + K_MAX_LENGTH.toString(16) + ' bytes')
2676
+ }
2677
+ return length | 0
2678
+ }
2679
+
2680
+ function SlowBuffer (length) {
2681
+ if (+length != length) { // eslint-disable-line eqeqeq
2682
+ length = 0;
2683
+ }
2684
+ return Buffer.alloc(+length)
2685
+ }
2686
+
2687
+ Buffer.isBuffer = function isBuffer (b) {
2688
+ return b != null && b._isBuffer === true &&
2689
+ b !== Buffer.prototype // so Buffer.isBuffer(Buffer.prototype) will be false
2690
+ };
2691
+
2692
+ Buffer.compare = function compare (a, b) {
2693
+ if (isInstance(a, Uint8Array)) a = Buffer.from(a, a.offset, a.byteLength);
2694
+ if (isInstance(b, Uint8Array)) b = Buffer.from(b, b.offset, b.byteLength);
2695
+ if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) {
2696
+ throw new TypeError(
2697
+ 'The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array'
2698
+ )
2699
+ }
2700
+
2701
+ if (a === b) return 0
2702
+
2703
+ let x = a.length;
2704
+ let y = b.length;
2705
+
2706
+ for (let i = 0, len = Math.min(x, y); i < len; ++i) {
2707
+ if (a[i] !== b[i]) {
2708
+ x = a[i];
2709
+ y = b[i];
2710
+ break
2711
+ }
2712
+ }
2713
+
2714
+ if (x < y) return -1
2715
+ if (y < x) return 1
2716
+ return 0
2717
+ };
2718
+
2719
+ Buffer.isEncoding = function isEncoding (encoding) {
2720
+ switch (String(encoding).toLowerCase()) {
2721
+ case 'hex':
2722
+ case 'utf8':
2723
+ case 'utf-8':
2724
+ case 'ascii':
2725
+ case 'latin1':
2726
+ case 'binary':
2727
+ case 'base64':
2728
+ case 'ucs2':
2729
+ case 'ucs-2':
2730
+ case 'utf16le':
2731
+ case 'utf-16le':
2732
+ return true
2733
+ default:
2734
+ return false
2735
+ }
2736
+ };
2737
+
2738
+ Buffer.concat = function concat (list, length) {
2739
+ if (!Array.isArray(list)) {
2740
+ throw new TypeError('"list" argument must be an Array of Buffers')
2741
+ }
2742
+
2743
+ if (list.length === 0) {
2744
+ return Buffer.alloc(0)
2745
+ }
2746
+
2747
+ let i;
2748
+ if (length === undefined) {
2749
+ length = 0;
2750
+ for (i = 0; i < list.length; ++i) {
2751
+ length += list[i].length;
2752
+ }
2753
+ }
2754
+
2755
+ const buffer = Buffer.allocUnsafe(length);
2756
+ let pos = 0;
2757
+ for (i = 0; i < list.length; ++i) {
2758
+ let buf = list[i];
2759
+ if (isInstance(buf, Uint8Array)) {
2760
+ if (pos + buf.length > buffer.length) {
2761
+ if (!Buffer.isBuffer(buf)) buf = Buffer.from(buf);
2762
+ buf.copy(buffer, pos);
2763
+ } else {
2764
+ Uint8Array.prototype.set.call(
2765
+ buffer,
2766
+ buf,
2767
+ pos
2768
+ );
2769
+ }
2770
+ } else if (!Buffer.isBuffer(buf)) {
2771
+ throw new TypeError('"list" argument must be an Array of Buffers')
2772
+ } else {
2773
+ buf.copy(buffer, pos);
2774
+ }
2775
+ pos += buf.length;
2776
+ }
2777
+ return buffer
2778
+ };
2779
+
2780
+ function byteLength (string, encoding) {
2781
+ if (Buffer.isBuffer(string)) {
2782
+ return string.length
2783
+ }
2784
+ if (ArrayBuffer.isView(string) || isInstance(string, ArrayBuffer)) {
2785
+ return string.byteLength
2786
+ }
2787
+ if (typeof string !== 'string') {
2788
+ throw new TypeError(
2789
+ 'The "string" argument must be one of type string, Buffer, or ArrayBuffer. ' +
2790
+ 'Received type ' + typeof string
2791
+ )
2792
+ }
2793
+
2794
+ const len = string.length;
2795
+ const mustMatch = (arguments.length > 2 && arguments[2] === true);
2796
+ if (!mustMatch && len === 0) return 0
2797
+
2798
+ // Use a for loop to avoid recursion
2799
+ let loweredCase = false;
2800
+ for (;;) {
2801
+ switch (encoding) {
2802
+ case 'ascii':
2803
+ case 'latin1':
2804
+ case 'binary':
2805
+ return len
2806
+ case 'utf8':
2807
+ case 'utf-8':
2808
+ return utf8ToBytes(string).length
2809
+ case 'ucs2':
2810
+ case 'ucs-2':
2811
+ case 'utf16le':
2812
+ case 'utf-16le':
2813
+ return len * 2
2814
+ case 'hex':
2815
+ return len >>> 1
2816
+ case 'base64':
2817
+ return base64ToBytes(string).length
2818
+ default:
2819
+ if (loweredCase) {
2820
+ return mustMatch ? -1 : utf8ToBytes(string).length // assume utf8
2821
+ }
2822
+ encoding = ('' + encoding).toLowerCase();
2823
+ loweredCase = true;
2824
+ }
2825
+ }
2826
+ }
2827
+ Buffer.byteLength = byteLength;
2828
+
2829
+ function slowToString (encoding, start, end) {
2830
+ let loweredCase = false;
2831
+
2832
+ // No need to verify that "this.length <= MAX_UINT32" since it's a read-only
2833
+ // property of a typed array.
2834
+
2835
+ // This behaves neither like String nor Uint8Array in that we set start/end
2836
+ // to their upper/lower bounds if the value passed is out of range.
2837
+ // undefined is handled specially as per ECMA-262 6th Edition,
2838
+ // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization.
2839
+ if (start === undefined || start < 0) {
2840
+ start = 0;
2841
+ }
2842
+ // Return early if start > this.length. Done here to prevent potential uint32
2843
+ // coercion fail below.
2844
+ if (start > this.length) {
2845
+ return ''
2846
+ }
2847
+
2848
+ if (end === undefined || end > this.length) {
2849
+ end = this.length;
2850
+ }
2851
+
2852
+ if (end <= 0) {
2853
+ return ''
2854
+ }
2855
+
2856
+ // Force coercion to uint32. This will also coerce falsey/NaN values to 0.
2857
+ end >>>= 0;
2858
+ start >>>= 0;
2859
+
2860
+ if (end <= start) {
2861
+ return ''
2862
+ }
2863
+
2864
+ if (!encoding) encoding = 'utf8';
2865
+
2866
+ while (true) {
2867
+ switch (encoding) {
2868
+ case 'hex':
2869
+ return hexSlice(this, start, end)
2870
+
2871
+ case 'utf8':
2872
+ case 'utf-8':
2873
+ return utf8Slice(this, start, end)
2874
+
2875
+ case 'ascii':
2876
+ return asciiSlice(this, start, end)
2877
+
2878
+ case 'latin1':
2879
+ case 'binary':
2880
+ return latin1Slice(this, start, end)
2881
+
2882
+ case 'base64':
2883
+ return base64Slice(this, start, end)
2884
+
2885
+ case 'ucs2':
2886
+ case 'ucs-2':
2887
+ case 'utf16le':
2888
+ case 'utf-16le':
2889
+ return utf16leSlice(this, start, end)
2890
+
2891
+ default:
2892
+ if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
2893
+ encoding = (encoding + '').toLowerCase();
2894
+ loweredCase = true;
2895
+ }
2896
+ }
2897
+ }
2898
+
2899
+ // This property is used by `Buffer.isBuffer` (and the `is-buffer` npm package)
2900
+ // to detect a Buffer instance. It's not possible to use `instanceof Buffer`
2901
+ // reliably in a browserify context because there could be multiple different
2902
+ // copies of the 'buffer' package in use. This method works even for Buffer
2903
+ // instances that were created from another copy of the `buffer` package.
2904
+ // See: https://github.com/feross/buffer/issues/154
2905
+ Buffer.prototype._isBuffer = true;
2906
+
2907
+ function swap (b, n, m) {
2908
+ const i = b[n];
2909
+ b[n] = b[m];
2910
+ b[m] = i;
2911
+ }
2912
+
2913
+ Buffer.prototype.swap16 = function swap16 () {
2914
+ const len = this.length;
2915
+ if (len % 2 !== 0) {
2916
+ throw new RangeError('Buffer size must be a multiple of 16-bits')
2917
+ }
2918
+ for (let i = 0; i < len; i += 2) {
2919
+ swap(this, i, i + 1);
2920
+ }
2921
+ return this
2922
+ };
2923
+
2924
+ Buffer.prototype.swap32 = function swap32 () {
2925
+ const len = this.length;
2926
+ if (len % 4 !== 0) {
2927
+ throw new RangeError('Buffer size must be a multiple of 32-bits')
2928
+ }
2929
+ for (let i = 0; i < len; i += 4) {
2930
+ swap(this, i, i + 3);
2931
+ swap(this, i + 1, i + 2);
2932
+ }
2933
+ return this
2934
+ };
2935
+
2936
+ Buffer.prototype.swap64 = function swap64 () {
2937
+ const len = this.length;
2938
+ if (len % 8 !== 0) {
2939
+ throw new RangeError('Buffer size must be a multiple of 64-bits')
2940
+ }
2941
+ for (let i = 0; i < len; i += 8) {
2942
+ swap(this, i, i + 7);
2943
+ swap(this, i + 1, i + 6);
2944
+ swap(this, i + 2, i + 5);
2945
+ swap(this, i + 3, i + 4);
2946
+ }
2947
+ return this
2948
+ };
2949
+
2950
+ Buffer.prototype.toString = function toString () {
2951
+ const length = this.length;
2952
+ if (length === 0) return ''
2953
+ if (arguments.length === 0) return utf8Slice(this, 0, length)
2954
+ return slowToString.apply(this, arguments)
2955
+ };
2956
+
2957
+ Buffer.prototype.toLocaleString = Buffer.prototype.toString;
2958
+
2959
+ Buffer.prototype.equals = function equals (b) {
2960
+ if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer')
2961
+ if (this === b) return true
2962
+ return Buffer.compare(this, b) === 0
2963
+ };
2964
+
2965
+ Buffer.prototype.inspect = function inspect () {
2966
+ let str = '';
2967
+ const max = exports.INSPECT_MAX_BYTES;
2968
+ str = this.toString('hex', 0, max).replace(/(.{2})/g, '$1 ').trim();
2969
+ if (this.length > max) str += ' ... ';
2970
+ return '<Buffer ' + str + '>'
2971
+ };
2972
+ if (customInspectSymbol) {
2973
+ Buffer.prototype[customInspectSymbol] = Buffer.prototype.inspect;
2974
+ }
2975
+
2976
+ Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) {
2977
+ if (isInstance(target, Uint8Array)) {
2978
+ target = Buffer.from(target, target.offset, target.byteLength);
2979
+ }
2980
+ if (!Buffer.isBuffer(target)) {
2981
+ throw new TypeError(
2982
+ 'The "target" argument must be one of type Buffer or Uint8Array. ' +
2983
+ 'Received type ' + (typeof target)
2984
+ )
2985
+ }
2986
+
2987
+ if (start === undefined) {
2988
+ start = 0;
2989
+ }
2990
+ if (end === undefined) {
2991
+ end = target ? target.length : 0;
2992
+ }
2993
+ if (thisStart === undefined) {
2994
+ thisStart = 0;
2995
+ }
2996
+ if (thisEnd === undefined) {
2997
+ thisEnd = this.length;
2998
+ }
2999
+
3000
+ if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) {
3001
+ throw new RangeError('out of range index')
3002
+ }
3003
+
3004
+ if (thisStart >= thisEnd && start >= end) {
3005
+ return 0
3006
+ }
3007
+ if (thisStart >= thisEnd) {
3008
+ return -1
3009
+ }
3010
+ if (start >= end) {
3011
+ return 1
3012
+ }
3013
+
3014
+ start >>>= 0;
3015
+ end >>>= 0;
3016
+ thisStart >>>= 0;
3017
+ thisEnd >>>= 0;
3018
+
3019
+ if (this === target) return 0
3020
+
3021
+ let x = thisEnd - thisStart;
3022
+ let y = end - start;
3023
+ const len = Math.min(x, y);
3024
+
3025
+ const thisCopy = this.slice(thisStart, thisEnd);
3026
+ const targetCopy = target.slice(start, end);
3027
+
3028
+ for (let i = 0; i < len; ++i) {
3029
+ if (thisCopy[i] !== targetCopy[i]) {
3030
+ x = thisCopy[i];
3031
+ y = targetCopy[i];
3032
+ break
3033
+ }
3034
+ }
3035
+
3036
+ if (x < y) return -1
3037
+ if (y < x) return 1
3038
+ return 0
3039
+ };
3040
+
3041
+ // Finds either the first index of `val` in `buffer` at offset >= `byteOffset`,
3042
+ // OR the last index of `val` in `buffer` at offset <= `byteOffset`.
3043
+ //
3044
+ // Arguments:
3045
+ // - buffer - a Buffer to search
3046
+ // - val - a string, Buffer, or number
3047
+ // - byteOffset - an index into `buffer`; will be clamped to an int32
3048
+ // - encoding - an optional encoding, relevant is val is a string
3049
+ // - dir - true for indexOf, false for lastIndexOf
3050
+ function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) {
3051
+ // Empty buffer means no match
3052
+ if (buffer.length === 0) return -1
3053
+
3054
+ // Normalize byteOffset
3055
+ if (typeof byteOffset === 'string') {
3056
+ encoding = byteOffset;
3057
+ byteOffset = 0;
3058
+ } else if (byteOffset > 0x7fffffff) {
3059
+ byteOffset = 0x7fffffff;
3060
+ } else if (byteOffset < -2147483648) {
3061
+ byteOffset = -2147483648;
3062
+ }
3063
+ byteOffset = +byteOffset; // Coerce to Number.
3064
+ if (numberIsNaN(byteOffset)) {
3065
+ // byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer
3066
+ byteOffset = dir ? 0 : (buffer.length - 1);
3067
+ }
3068
+
3069
+ // Normalize byteOffset: negative offsets start from the end of the buffer
3070
+ if (byteOffset < 0) byteOffset = buffer.length + byteOffset;
3071
+ if (byteOffset >= buffer.length) {
3072
+ if (dir) return -1
3073
+ else byteOffset = buffer.length - 1;
3074
+ } else if (byteOffset < 0) {
3075
+ if (dir) byteOffset = 0;
3076
+ else return -1
3077
+ }
3078
+
3079
+ // Normalize val
3080
+ if (typeof val === 'string') {
3081
+ val = Buffer.from(val, encoding);
3082
+ }
3083
+
3084
+ // Finally, search either indexOf (if dir is true) or lastIndexOf
3085
+ if (Buffer.isBuffer(val)) {
3086
+ // Special case: looking for empty string/buffer always fails
3087
+ if (val.length === 0) {
3088
+ return -1
3089
+ }
3090
+ return arrayIndexOf(buffer, val, byteOffset, encoding, dir)
3091
+ } else if (typeof val === 'number') {
3092
+ val = val & 0xFF; // Search for a byte value [0-255]
3093
+ if (typeof Uint8Array.prototype.indexOf === 'function') {
3094
+ if (dir) {
3095
+ return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset)
3096
+ } else {
3097
+ return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset)
3098
+ }
3099
+ }
3100
+ return arrayIndexOf(buffer, [val], byteOffset, encoding, dir)
3101
+ }
3102
+
3103
+ throw new TypeError('val must be string, number or Buffer')
3104
+ }
3105
+
3106
+ function arrayIndexOf (arr, val, byteOffset, encoding, dir) {
3107
+ let indexSize = 1;
3108
+ let arrLength = arr.length;
3109
+ let valLength = val.length;
3110
+
3111
+ if (encoding !== undefined) {
3112
+ encoding = String(encoding).toLowerCase();
3113
+ if (encoding === 'ucs2' || encoding === 'ucs-2' ||
3114
+ encoding === 'utf16le' || encoding === 'utf-16le') {
3115
+ if (arr.length < 2 || val.length < 2) {
3116
+ return -1
3117
+ }
3118
+ indexSize = 2;
3119
+ arrLength /= 2;
3120
+ valLength /= 2;
3121
+ byteOffset /= 2;
3122
+ }
3123
+ }
3124
+
3125
+ function read (buf, i) {
3126
+ if (indexSize === 1) {
3127
+ return buf[i]
3128
+ } else {
3129
+ return buf.readUInt16BE(i * indexSize)
3130
+ }
3131
+ }
3132
+
3133
+ let i;
3134
+ if (dir) {
3135
+ let foundIndex = -1;
3136
+ for (i = byteOffset; i < arrLength; i++) {
3137
+ if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) {
3138
+ if (foundIndex === -1) foundIndex = i;
3139
+ if (i - foundIndex + 1 === valLength) return foundIndex * indexSize
3140
+ } else {
3141
+ if (foundIndex !== -1) i -= i - foundIndex;
3142
+ foundIndex = -1;
3143
+ }
3144
+ }
3145
+ } else {
3146
+ if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength;
3147
+ for (i = byteOffset; i >= 0; i--) {
3148
+ let found = true;
3149
+ for (let j = 0; j < valLength; j++) {
3150
+ if (read(arr, i + j) !== read(val, j)) {
3151
+ found = false;
3152
+ break
3153
+ }
3154
+ }
3155
+ if (found) return i
3156
+ }
3157
+ }
3158
+
3159
+ return -1
3160
+ }
3161
+
3162
+ Buffer.prototype.includes = function includes (val, byteOffset, encoding) {
3163
+ return this.indexOf(val, byteOffset, encoding) !== -1
3164
+ };
3165
+
3166
+ Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) {
3167
+ return bidirectionalIndexOf(this, val, byteOffset, encoding, true)
3168
+ };
3169
+
3170
+ Buffer.prototype.lastIndexOf = function lastIndexOf (val, byteOffset, encoding) {
3171
+ return bidirectionalIndexOf(this, val, byteOffset, encoding, false)
3172
+ };
3173
+
3174
+ function hexWrite (buf, string, offset, length) {
3175
+ offset = Number(offset) || 0;
3176
+ const remaining = buf.length - offset;
3177
+ if (!length) {
3178
+ length = remaining;
3179
+ } else {
3180
+ length = Number(length);
3181
+ if (length > remaining) {
3182
+ length = remaining;
3183
+ }
3184
+ }
3185
+
3186
+ const strLen = string.length;
3187
+
3188
+ if (length > strLen / 2) {
3189
+ length = strLen / 2;
3190
+ }
3191
+ let i;
3192
+ for (i = 0; i < length; ++i) {
3193
+ const parsed = parseInt(string.substr(i * 2, 2), 16);
3194
+ if (numberIsNaN(parsed)) return i
3195
+ buf[offset + i] = parsed;
3196
+ }
3197
+ return i
3198
+ }
3199
+
3200
+ function utf8Write (buf, string, offset, length) {
3201
+ return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length)
3202
+ }
3203
+
3204
+ function asciiWrite (buf, string, offset, length) {
3205
+ return blitBuffer(asciiToBytes(string), buf, offset, length)
3206
+ }
3207
+
3208
+ function base64Write (buf, string, offset, length) {
3209
+ return blitBuffer(base64ToBytes(string), buf, offset, length)
3210
+ }
3211
+
3212
+ function ucs2Write (buf, string, offset, length) {
3213
+ return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length)
3214
+ }
3215
+
3216
+ Buffer.prototype.write = function write (string, offset, length, encoding) {
3217
+ // Buffer#write(string)
3218
+ if (offset === undefined) {
3219
+ encoding = 'utf8';
3220
+ length = this.length;
3221
+ offset = 0;
3222
+ // Buffer#write(string, encoding)
3223
+ } else if (length === undefined && typeof offset === 'string') {
3224
+ encoding = offset;
3225
+ length = this.length;
3226
+ offset = 0;
3227
+ // Buffer#write(string, offset[, length][, encoding])
3228
+ } else if (isFinite(offset)) {
3229
+ offset = offset >>> 0;
3230
+ if (isFinite(length)) {
3231
+ length = length >>> 0;
3232
+ if (encoding === undefined) encoding = 'utf8';
3233
+ } else {
3234
+ encoding = length;
3235
+ length = undefined;
3236
+ }
3237
+ } else {
3238
+ throw new Error(
3239
+ 'Buffer.write(string, encoding, offset[, length]) is no longer supported'
3240
+ )
3241
+ }
3242
+
3243
+ const remaining = this.length - offset;
3244
+ if (length === undefined || length > remaining) length = remaining;
3245
+
3246
+ if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) {
3247
+ throw new RangeError('Attempt to write outside buffer bounds')
3248
+ }
3249
+
3250
+ if (!encoding) encoding = 'utf8';
3251
+
3252
+ let loweredCase = false;
3253
+ for (;;) {
3254
+ switch (encoding) {
3255
+ case 'hex':
3256
+ return hexWrite(this, string, offset, length)
3257
+
3258
+ case 'utf8':
3259
+ case 'utf-8':
3260
+ return utf8Write(this, string, offset, length)
3261
+
3262
+ case 'ascii':
3263
+ case 'latin1':
3264
+ case 'binary':
3265
+ return asciiWrite(this, string, offset, length)
3266
+
3267
+ case 'base64':
3268
+ // Warning: maxLength not taken into account in base64Write
3269
+ return base64Write(this, string, offset, length)
3270
+
3271
+ case 'ucs2':
3272
+ case 'ucs-2':
3273
+ case 'utf16le':
3274
+ case 'utf-16le':
3275
+ return ucs2Write(this, string, offset, length)
3276
+
3277
+ default:
3278
+ if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
3279
+ encoding = ('' + encoding).toLowerCase();
3280
+ loweredCase = true;
3281
+ }
3282
+ }
3283
+ };
3284
+
3285
+ Buffer.prototype.toJSON = function toJSON () {
3286
+ return {
3287
+ type: 'Buffer',
3288
+ data: Array.prototype.slice.call(this._arr || this, 0)
3289
+ }
3290
+ };
3291
+
3292
+ function base64Slice (buf, start, end) {
3293
+ if (start === 0 && end === buf.length) {
3294
+ return base64.fromByteArray(buf)
3295
+ } else {
3296
+ return base64.fromByteArray(buf.slice(start, end))
3297
+ }
3298
+ }
3299
+
3300
+ function utf8Slice (buf, start, end) {
3301
+ end = Math.min(buf.length, end);
3302
+ const res = [];
3303
+
3304
+ let i = start;
3305
+ while (i < end) {
3306
+ const firstByte = buf[i];
3307
+ let codePoint = null;
3308
+ let bytesPerSequence = (firstByte > 0xEF)
3309
+ ? 4
3310
+ : (firstByte > 0xDF)
3311
+ ? 3
3312
+ : (firstByte > 0xBF)
3313
+ ? 2
3314
+ : 1;
3315
+
3316
+ if (i + bytesPerSequence <= end) {
3317
+ let secondByte, thirdByte, fourthByte, tempCodePoint;
3318
+
3319
+ switch (bytesPerSequence) {
3320
+ case 1:
3321
+ if (firstByte < 0x80) {
3322
+ codePoint = firstByte;
3323
+ }
3324
+ break
3325
+ case 2:
3326
+ secondByte = buf[i + 1];
3327
+ if ((secondByte & 0xC0) === 0x80) {
3328
+ tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F);
3329
+ if (tempCodePoint > 0x7F) {
3330
+ codePoint = tempCodePoint;
3331
+ }
3332
+ }
3333
+ break
3334
+ case 3:
3335
+ secondByte = buf[i + 1];
3336
+ thirdByte = buf[i + 2];
3337
+ if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) {
3338
+ tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F);
3339
+ if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) {
3340
+ codePoint = tempCodePoint;
3341
+ }
3342
+ }
3343
+ break
3344
+ case 4:
3345
+ secondByte = buf[i + 1];
3346
+ thirdByte = buf[i + 2];
3347
+ fourthByte = buf[i + 3];
3348
+ if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) {
3349
+ tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F);
3350
+ if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) {
3351
+ codePoint = tempCodePoint;
3352
+ }
3353
+ }
3354
+ }
3355
+ }
3356
+
3357
+ if (codePoint === null) {
3358
+ // we did not generate a valid codePoint so insert a
3359
+ // replacement char (U+FFFD) and advance only 1 byte
3360
+ codePoint = 0xFFFD;
3361
+ bytesPerSequence = 1;
3362
+ } else if (codePoint > 0xFFFF) {
3363
+ // encode to utf16 (surrogate pair dance)
3364
+ codePoint -= 0x10000;
3365
+ res.push(codePoint >>> 10 & 0x3FF | 0xD800);
3366
+ codePoint = 0xDC00 | codePoint & 0x3FF;
3367
+ }
3368
+
3369
+ res.push(codePoint);
3370
+ i += bytesPerSequence;
3371
+ }
3372
+
3373
+ return decodeCodePointsArray(res)
3374
+ }
3375
+
3376
+ // Based on http://stackoverflow.com/a/22747272/680742, the browser with
3377
+ // the lowest limit is Chrome, with 0x10000 args.
3378
+ // We go 1 magnitude less, for safety
3379
+ const MAX_ARGUMENTS_LENGTH = 0x1000;
3380
+
3381
+ function decodeCodePointsArray (codePoints) {
3382
+ const len = codePoints.length;
3383
+ if (len <= MAX_ARGUMENTS_LENGTH) {
3384
+ return String.fromCharCode.apply(String, codePoints) // avoid extra slice()
3385
+ }
3386
+
3387
+ // Decode in chunks to avoid "call stack size exceeded".
3388
+ let res = '';
3389
+ let i = 0;
3390
+ while (i < len) {
3391
+ res += String.fromCharCode.apply(
3392
+ String,
3393
+ codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH)
3394
+ );
3395
+ }
3396
+ return res
3397
+ }
3398
+
3399
+ function asciiSlice (buf, start, end) {
3400
+ let ret = '';
3401
+ end = Math.min(buf.length, end);
3402
+
3403
+ for (let i = start; i < end; ++i) {
3404
+ ret += String.fromCharCode(buf[i] & 0x7F);
3405
+ }
3406
+ return ret
3407
+ }
3408
+
3409
+ function latin1Slice (buf, start, end) {
3410
+ let ret = '';
3411
+ end = Math.min(buf.length, end);
3412
+
3413
+ for (let i = start; i < end; ++i) {
3414
+ ret += String.fromCharCode(buf[i]);
3415
+ }
3416
+ return ret
3417
+ }
3418
+
3419
+ function hexSlice (buf, start, end) {
3420
+ const len = buf.length;
3421
+
3422
+ if (!start || start < 0) start = 0;
3423
+ if (!end || end < 0 || end > len) end = len;
3424
+
3425
+ let out = '';
3426
+ for (let i = start; i < end; ++i) {
3427
+ out += hexSliceLookupTable[buf[i]];
3428
+ }
3429
+ return out
3430
+ }
3431
+
3432
+ function utf16leSlice (buf, start, end) {
3433
+ const bytes = buf.slice(start, end);
3434
+ let res = '';
3435
+ // If bytes.length is odd, the last 8 bits must be ignored (same as node.js)
3436
+ for (let i = 0; i < bytes.length - 1; i += 2) {
3437
+ res += String.fromCharCode(bytes[i] + (bytes[i + 1] * 256));
3438
+ }
3439
+ return res
3440
+ }
3441
+
3442
+ Buffer.prototype.slice = function slice (start, end) {
3443
+ const len = this.length;
3444
+ start = ~~start;
3445
+ end = end === undefined ? len : ~~end;
3446
+
3447
+ if (start < 0) {
3448
+ start += len;
3449
+ if (start < 0) start = 0;
3450
+ } else if (start > len) {
3451
+ start = len;
3452
+ }
3453
+
3454
+ if (end < 0) {
3455
+ end += len;
3456
+ if (end < 0) end = 0;
3457
+ } else if (end > len) {
3458
+ end = len;
3459
+ }
3460
+
3461
+ if (end < start) end = start;
3462
+
3463
+ const newBuf = this.subarray(start, end);
3464
+ // Return an augmented `Uint8Array` instance
3465
+ Object.setPrototypeOf(newBuf, Buffer.prototype);
3466
+
3467
+ return newBuf
3468
+ };
3469
+
3470
+ /*
3471
+ * Need to make sure that buffer isn't trying to write out of bounds.
3472
+ */
3473
+ function checkOffset (offset, ext, length) {
3474
+ if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint')
3475
+ if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length')
3476
+ }
3477
+
3478
+ Buffer.prototype.readUintLE =
3479
+ Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) {
3480
+ offset = offset >>> 0;
3481
+ byteLength = byteLength >>> 0;
3482
+ if (!noAssert) checkOffset(offset, byteLength, this.length);
3483
+
3484
+ let val = this[offset];
3485
+ let mul = 1;
3486
+ let i = 0;
3487
+ while (++i < byteLength && (mul *= 0x100)) {
3488
+ val += this[offset + i] * mul;
3489
+ }
3490
+
3491
+ return val
3492
+ };
3493
+
3494
+ Buffer.prototype.readUintBE =
3495
+ Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) {
3496
+ offset = offset >>> 0;
3497
+ byteLength = byteLength >>> 0;
3498
+ if (!noAssert) {
3499
+ checkOffset(offset, byteLength, this.length);
3500
+ }
3501
+
3502
+ let val = this[offset + --byteLength];
3503
+ let mul = 1;
3504
+ while (byteLength > 0 && (mul *= 0x100)) {
3505
+ val += this[offset + --byteLength] * mul;
3506
+ }
3507
+
3508
+ return val
3509
+ };
3510
+
3511
+ Buffer.prototype.readUint8 =
3512
+ Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) {
3513
+ offset = offset >>> 0;
3514
+ if (!noAssert) checkOffset(offset, 1, this.length);
3515
+ return this[offset]
3516
+ };
3517
+
3518
+ Buffer.prototype.readUint16LE =
3519
+ Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) {
3520
+ offset = offset >>> 0;
3521
+ if (!noAssert) checkOffset(offset, 2, this.length);
3522
+ return this[offset] | (this[offset + 1] << 8)
3523
+ };
3524
+
3525
+ Buffer.prototype.readUint16BE =
3526
+ Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) {
3527
+ offset = offset >>> 0;
3528
+ if (!noAssert) checkOffset(offset, 2, this.length);
3529
+ return (this[offset] << 8) | this[offset + 1]
3530
+ };
3531
+
3532
+ Buffer.prototype.readUint32LE =
3533
+ Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) {
3534
+ offset = offset >>> 0;
3535
+ if (!noAssert) checkOffset(offset, 4, this.length);
3536
+
3537
+ return ((this[offset]) |
3538
+ (this[offset + 1] << 8) |
3539
+ (this[offset + 2] << 16)) +
3540
+ (this[offset + 3] * 0x1000000)
3541
+ };
3542
+
3543
+ Buffer.prototype.readUint32BE =
3544
+ Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) {
3545
+ offset = offset >>> 0;
3546
+ if (!noAssert) checkOffset(offset, 4, this.length);
3547
+
3548
+ return (this[offset] * 0x1000000) +
3549
+ ((this[offset + 1] << 16) |
3550
+ (this[offset + 2] << 8) |
3551
+ this[offset + 3])
3552
+ };
3553
+
3554
+ Buffer.prototype.readBigUInt64LE = defineBigIntMethod(function readBigUInt64LE (offset) {
3555
+ offset = offset >>> 0;
3556
+ validateNumber(offset, 'offset');
3557
+ const first = this[offset];
3558
+ const last = this[offset + 7];
3559
+ if (first === undefined || last === undefined) {
3560
+ boundsError(offset, this.length - 8);
3561
+ }
3562
+
3563
+ const lo = first +
3564
+ this[++offset] * 2 ** 8 +
3565
+ this[++offset] * 2 ** 16 +
3566
+ this[++offset] * 2 ** 24;
3567
+
3568
+ const hi = this[++offset] +
3569
+ this[++offset] * 2 ** 8 +
3570
+ this[++offset] * 2 ** 16 +
3571
+ last * 2 ** 24;
3572
+
3573
+ return BigInt(lo) + (BigInt(hi) << BigInt(32))
3574
+ });
3575
+
3576
+ Buffer.prototype.readBigUInt64BE = defineBigIntMethod(function readBigUInt64BE (offset) {
3577
+ offset = offset >>> 0;
3578
+ validateNumber(offset, 'offset');
3579
+ const first = this[offset];
3580
+ const last = this[offset + 7];
3581
+ if (first === undefined || last === undefined) {
3582
+ boundsError(offset, this.length - 8);
3583
+ }
3584
+
3585
+ const hi = first * 2 ** 24 +
3586
+ this[++offset] * 2 ** 16 +
3587
+ this[++offset] * 2 ** 8 +
3588
+ this[++offset];
3589
+
3590
+ const lo = this[++offset] * 2 ** 24 +
3591
+ this[++offset] * 2 ** 16 +
3592
+ this[++offset] * 2 ** 8 +
3593
+ last;
3594
+
3595
+ return (BigInt(hi) << BigInt(32)) + BigInt(lo)
3596
+ });
3597
+
3598
+ Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) {
3599
+ offset = offset >>> 0;
3600
+ byteLength = byteLength >>> 0;
3601
+ if (!noAssert) checkOffset(offset, byteLength, this.length);
3602
+
3603
+ let val = this[offset];
3604
+ let mul = 1;
3605
+ let i = 0;
3606
+ while (++i < byteLength && (mul *= 0x100)) {
3607
+ val += this[offset + i] * mul;
3608
+ }
3609
+ mul *= 0x80;
3610
+
3611
+ if (val >= mul) val -= Math.pow(2, 8 * byteLength);
3612
+
3613
+ return val
3614
+ };
3615
+
3616
+ Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) {
3617
+ offset = offset >>> 0;
3618
+ byteLength = byteLength >>> 0;
3619
+ if (!noAssert) checkOffset(offset, byteLength, this.length);
3620
+
3621
+ let i = byteLength;
3622
+ let mul = 1;
3623
+ let val = this[offset + --i];
3624
+ while (i > 0 && (mul *= 0x100)) {
3625
+ val += this[offset + --i] * mul;
3626
+ }
3627
+ mul *= 0x80;
3628
+
3629
+ if (val >= mul) val -= Math.pow(2, 8 * byteLength);
3630
+
3631
+ return val
3632
+ };
3633
+
3634
+ Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) {
3635
+ offset = offset >>> 0;
3636
+ if (!noAssert) checkOffset(offset, 1, this.length);
3637
+ if (!(this[offset] & 0x80)) return (this[offset])
3638
+ return ((0xff - this[offset] + 1) * -1)
3639
+ };
3640
+
3641
+ Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) {
3642
+ offset = offset >>> 0;
3643
+ if (!noAssert) checkOffset(offset, 2, this.length);
3644
+ const val = this[offset] | (this[offset + 1] << 8);
3645
+ return (val & 0x8000) ? val | 0xFFFF0000 : val
3646
+ };
3647
+
3648
+ Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) {
3649
+ offset = offset >>> 0;
3650
+ if (!noAssert) checkOffset(offset, 2, this.length);
3651
+ const val = this[offset + 1] | (this[offset] << 8);
3652
+ return (val & 0x8000) ? val | 0xFFFF0000 : val
3653
+ };
3654
+
3655
+ Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) {
3656
+ offset = offset >>> 0;
3657
+ if (!noAssert) checkOffset(offset, 4, this.length);
3658
+
3659
+ return (this[offset]) |
3660
+ (this[offset + 1] << 8) |
3661
+ (this[offset + 2] << 16) |
3662
+ (this[offset + 3] << 24)
3663
+ };
3664
+
3665
+ Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) {
3666
+ offset = offset >>> 0;
3667
+ if (!noAssert) checkOffset(offset, 4, this.length);
3668
+
3669
+ return (this[offset] << 24) |
3670
+ (this[offset + 1] << 16) |
3671
+ (this[offset + 2] << 8) |
3672
+ (this[offset + 3])
3673
+ };
3674
+
3675
+ Buffer.prototype.readBigInt64LE = defineBigIntMethod(function readBigInt64LE (offset) {
3676
+ offset = offset >>> 0;
3677
+ validateNumber(offset, 'offset');
3678
+ const first = this[offset];
3679
+ const last = this[offset + 7];
3680
+ if (first === undefined || last === undefined) {
3681
+ boundsError(offset, this.length - 8);
3682
+ }
3683
+
3684
+ const val = this[offset + 4] +
3685
+ this[offset + 5] * 2 ** 8 +
3686
+ this[offset + 6] * 2 ** 16 +
3687
+ (last << 24); // Overflow
3688
+
3689
+ return (BigInt(val) << BigInt(32)) +
3690
+ BigInt(first +
3691
+ this[++offset] * 2 ** 8 +
3692
+ this[++offset] * 2 ** 16 +
3693
+ this[++offset] * 2 ** 24)
3694
+ });
3695
+
3696
+ Buffer.prototype.readBigInt64BE = defineBigIntMethod(function readBigInt64BE (offset) {
3697
+ offset = offset >>> 0;
3698
+ validateNumber(offset, 'offset');
3699
+ const first = this[offset];
3700
+ const last = this[offset + 7];
3701
+ if (first === undefined || last === undefined) {
3702
+ boundsError(offset, this.length - 8);
3703
+ }
3704
+
3705
+ const val = (first << 24) + // Overflow
3706
+ this[++offset] * 2 ** 16 +
3707
+ this[++offset] * 2 ** 8 +
3708
+ this[++offset];
3709
+
3710
+ return (BigInt(val) << BigInt(32)) +
3711
+ BigInt(this[++offset] * 2 ** 24 +
3712
+ this[++offset] * 2 ** 16 +
3713
+ this[++offset] * 2 ** 8 +
3714
+ last)
3715
+ });
3716
+
3717
+ Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) {
3718
+ offset = offset >>> 0;
3719
+ if (!noAssert) checkOffset(offset, 4, this.length);
3720
+ return ieee754.read(this, offset, true, 23, 4)
3721
+ };
3722
+
3723
+ Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) {
3724
+ offset = offset >>> 0;
3725
+ if (!noAssert) checkOffset(offset, 4, this.length);
3726
+ return ieee754.read(this, offset, false, 23, 4)
3727
+ };
3728
+
3729
+ Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) {
3730
+ offset = offset >>> 0;
3731
+ if (!noAssert) checkOffset(offset, 8, this.length);
3732
+ return ieee754.read(this, offset, true, 52, 8)
3733
+ };
3734
+
3735
+ Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) {
3736
+ offset = offset >>> 0;
3737
+ if (!noAssert) checkOffset(offset, 8, this.length);
3738
+ return ieee754.read(this, offset, false, 52, 8)
3739
+ };
3740
+
3741
+ function checkInt (buf, value, offset, ext, max, min) {
3742
+ if (!Buffer.isBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance')
3743
+ if (value > max || value < min) throw new RangeError('"value" argument is out of bounds')
3744
+ if (offset + ext > buf.length) throw new RangeError('Index out of range')
3745
+ }
3746
+
3747
+ Buffer.prototype.writeUintLE =
3748
+ Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) {
3749
+ value = +value;
3750
+ offset = offset >>> 0;
3751
+ byteLength = byteLength >>> 0;
3752
+ if (!noAssert) {
3753
+ const maxBytes = Math.pow(2, 8 * byteLength) - 1;
3754
+ checkInt(this, value, offset, byteLength, maxBytes, 0);
3755
+ }
3756
+
3757
+ let mul = 1;
3758
+ let i = 0;
3759
+ this[offset] = value & 0xFF;
3760
+ while (++i < byteLength && (mul *= 0x100)) {
3761
+ this[offset + i] = (value / mul) & 0xFF;
3762
+ }
3763
+
3764
+ return offset + byteLength
3765
+ };
3766
+
3767
+ Buffer.prototype.writeUintBE =
3768
+ Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) {
3769
+ value = +value;
3770
+ offset = offset >>> 0;
3771
+ byteLength = byteLength >>> 0;
3772
+ if (!noAssert) {
3773
+ const maxBytes = Math.pow(2, 8 * byteLength) - 1;
3774
+ checkInt(this, value, offset, byteLength, maxBytes, 0);
3775
+ }
3776
+
3777
+ let i = byteLength - 1;
3778
+ let mul = 1;
3779
+ this[offset + i] = value & 0xFF;
3780
+ while (--i >= 0 && (mul *= 0x100)) {
3781
+ this[offset + i] = (value / mul) & 0xFF;
3782
+ }
3783
+
3784
+ return offset + byteLength
3785
+ };
3786
+
3787
+ Buffer.prototype.writeUint8 =
3788
+ Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) {
3789
+ value = +value;
3790
+ offset = offset >>> 0;
3791
+ if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0);
3792
+ this[offset] = (value & 0xff);
3793
+ return offset + 1
3794
+ };
3795
+
3796
+ Buffer.prototype.writeUint16LE =
3797
+ Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) {
3798
+ value = +value;
3799
+ offset = offset >>> 0;
3800
+ if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0);
3801
+ this[offset] = (value & 0xff);
3802
+ this[offset + 1] = (value >>> 8);
3803
+ return offset + 2
3804
+ };
3805
+
3806
+ Buffer.prototype.writeUint16BE =
3807
+ Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) {
3808
+ value = +value;
3809
+ offset = offset >>> 0;
3810
+ if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0);
3811
+ this[offset] = (value >>> 8);
3812
+ this[offset + 1] = (value & 0xff);
3813
+ return offset + 2
3814
+ };
3815
+
3816
+ Buffer.prototype.writeUint32LE =
3817
+ Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) {
3818
+ value = +value;
3819
+ offset = offset >>> 0;
3820
+ if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0);
3821
+ this[offset + 3] = (value >>> 24);
3822
+ this[offset + 2] = (value >>> 16);
3823
+ this[offset + 1] = (value >>> 8);
3824
+ this[offset] = (value & 0xff);
3825
+ return offset + 4
3826
+ };
3827
+
3828
+ Buffer.prototype.writeUint32BE =
3829
+ Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) {
3830
+ value = +value;
3831
+ offset = offset >>> 0;
3832
+ if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0);
3833
+ this[offset] = (value >>> 24);
3834
+ this[offset + 1] = (value >>> 16);
3835
+ this[offset + 2] = (value >>> 8);
3836
+ this[offset + 3] = (value & 0xff);
3837
+ return offset + 4
3838
+ };
3839
+
3840
+ function wrtBigUInt64LE (buf, value, offset, min, max) {
3841
+ checkIntBI(value, min, max, buf, offset, 7);
3842
+
3843
+ let lo = Number(value & BigInt(0xffffffff));
3844
+ buf[offset++] = lo;
3845
+ lo = lo >> 8;
3846
+ buf[offset++] = lo;
3847
+ lo = lo >> 8;
3848
+ buf[offset++] = lo;
3849
+ lo = lo >> 8;
3850
+ buf[offset++] = lo;
3851
+ let hi = Number(value >> BigInt(32) & BigInt(0xffffffff));
3852
+ buf[offset++] = hi;
3853
+ hi = hi >> 8;
3854
+ buf[offset++] = hi;
3855
+ hi = hi >> 8;
3856
+ buf[offset++] = hi;
3857
+ hi = hi >> 8;
3858
+ buf[offset++] = hi;
3859
+ return offset
3860
+ }
3861
+
3862
+ function wrtBigUInt64BE (buf, value, offset, min, max) {
3863
+ checkIntBI(value, min, max, buf, offset, 7);
3864
+
3865
+ let lo = Number(value & BigInt(0xffffffff));
3866
+ buf[offset + 7] = lo;
3867
+ lo = lo >> 8;
3868
+ buf[offset + 6] = lo;
3869
+ lo = lo >> 8;
3870
+ buf[offset + 5] = lo;
3871
+ lo = lo >> 8;
3872
+ buf[offset + 4] = lo;
3873
+ let hi = Number(value >> BigInt(32) & BigInt(0xffffffff));
3874
+ buf[offset + 3] = hi;
3875
+ hi = hi >> 8;
3876
+ buf[offset + 2] = hi;
3877
+ hi = hi >> 8;
3878
+ buf[offset + 1] = hi;
3879
+ hi = hi >> 8;
3880
+ buf[offset] = hi;
3881
+ return offset + 8
3882
+ }
3883
+
3884
+ Buffer.prototype.writeBigUInt64LE = defineBigIntMethod(function writeBigUInt64LE (value, offset = 0) {
3885
+ return wrtBigUInt64LE(this, value, offset, BigInt(0), BigInt('0xffffffffffffffff'))
3886
+ });
3887
+
3888
+ Buffer.prototype.writeBigUInt64BE = defineBigIntMethod(function writeBigUInt64BE (value, offset = 0) {
3889
+ return wrtBigUInt64BE(this, value, offset, BigInt(0), BigInt('0xffffffffffffffff'))
3890
+ });
3891
+
3892
+ Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) {
3893
+ value = +value;
3894
+ offset = offset >>> 0;
3895
+ if (!noAssert) {
3896
+ const limit = Math.pow(2, (8 * byteLength) - 1);
3897
+
3898
+ checkInt(this, value, offset, byteLength, limit - 1, -limit);
3899
+ }
3900
+
3901
+ let i = 0;
3902
+ let mul = 1;
3903
+ let sub = 0;
3904
+ this[offset] = value & 0xFF;
3905
+ while (++i < byteLength && (mul *= 0x100)) {
3906
+ if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) {
3907
+ sub = 1;
3908
+ }
3909
+ this[offset + i] = ((value / mul) >> 0) - sub & 0xFF;
3910
+ }
3911
+
3912
+ return offset + byteLength
3913
+ };
3914
+
3915
+ Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) {
3916
+ value = +value;
3917
+ offset = offset >>> 0;
3918
+ if (!noAssert) {
3919
+ const limit = Math.pow(2, (8 * byteLength) - 1);
3920
+
3921
+ checkInt(this, value, offset, byteLength, limit - 1, -limit);
3922
+ }
3923
+
3924
+ let i = byteLength - 1;
3925
+ let mul = 1;
3926
+ let sub = 0;
3927
+ this[offset + i] = value & 0xFF;
3928
+ while (--i >= 0 && (mul *= 0x100)) {
3929
+ if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) {
3930
+ sub = 1;
3931
+ }
3932
+ this[offset + i] = ((value / mul) >> 0) - sub & 0xFF;
3933
+ }
3934
+
3935
+ return offset + byteLength
3936
+ };
3937
+
3938
+ Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) {
3939
+ value = +value;
3940
+ offset = offset >>> 0;
3941
+ if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -128);
3942
+ if (value < 0) value = 0xff + value + 1;
3943
+ this[offset] = (value & 0xff);
3944
+ return offset + 1
3945
+ };
3946
+
3947
+ Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) {
3948
+ value = +value;
3949
+ offset = offset >>> 0;
3950
+ if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -32768);
3951
+ this[offset] = (value & 0xff);
3952
+ this[offset + 1] = (value >>> 8);
3953
+ return offset + 2
3954
+ };
3955
+
3956
+ Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) {
3957
+ value = +value;
3958
+ offset = offset >>> 0;
3959
+ if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -32768);
3960
+ this[offset] = (value >>> 8);
3961
+ this[offset + 1] = (value & 0xff);
3962
+ return offset + 2
3963
+ };
3964
+
3965
+ Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) {
3966
+ value = +value;
3967
+ offset = offset >>> 0;
3968
+ if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -2147483648);
3969
+ this[offset] = (value & 0xff);
3970
+ this[offset + 1] = (value >>> 8);
3971
+ this[offset + 2] = (value >>> 16);
3972
+ this[offset + 3] = (value >>> 24);
3973
+ return offset + 4
3974
+ };
3975
+
3976
+ Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) {
3977
+ value = +value;
3978
+ offset = offset >>> 0;
3979
+ if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -2147483648);
3980
+ if (value < 0) value = 0xffffffff + value + 1;
3981
+ this[offset] = (value >>> 24);
3982
+ this[offset + 1] = (value >>> 16);
3983
+ this[offset + 2] = (value >>> 8);
3984
+ this[offset + 3] = (value & 0xff);
3985
+ return offset + 4
3986
+ };
3987
+
3988
+ Buffer.prototype.writeBigInt64LE = defineBigIntMethod(function writeBigInt64LE (value, offset = 0) {
3989
+ return wrtBigUInt64LE(this, value, offset, -BigInt('0x8000000000000000'), BigInt('0x7fffffffffffffff'))
3990
+ });
3991
+
3992
+ Buffer.prototype.writeBigInt64BE = defineBigIntMethod(function writeBigInt64BE (value, offset = 0) {
3993
+ return wrtBigUInt64BE(this, value, offset, -BigInt('0x8000000000000000'), BigInt('0x7fffffffffffffff'))
3994
+ });
3995
+
3996
+ function checkIEEE754 (buf, value, offset, ext, max, min) {
3997
+ if (offset + ext > buf.length) throw new RangeError('Index out of range')
3998
+ if (offset < 0) throw new RangeError('Index out of range')
3999
+ }
4000
+
4001
+ function writeFloat (buf, value, offset, littleEndian, noAssert) {
4002
+ value = +value;
4003
+ offset = offset >>> 0;
4004
+ if (!noAssert) {
4005
+ checkIEEE754(buf, value, offset, 4);
4006
+ }
4007
+ ieee754.write(buf, value, offset, littleEndian, 23, 4);
4008
+ return offset + 4
4009
+ }
4010
+
4011
+ Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) {
4012
+ return writeFloat(this, value, offset, true, noAssert)
4013
+ };
4014
+
4015
+ Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) {
4016
+ return writeFloat(this, value, offset, false, noAssert)
4017
+ };
4018
+
4019
+ function writeDouble (buf, value, offset, littleEndian, noAssert) {
4020
+ value = +value;
4021
+ offset = offset >>> 0;
4022
+ if (!noAssert) {
4023
+ checkIEEE754(buf, value, offset, 8);
4024
+ }
4025
+ ieee754.write(buf, value, offset, littleEndian, 52, 8);
4026
+ return offset + 8
4027
+ }
4028
+
4029
+ Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) {
4030
+ return writeDouble(this, value, offset, true, noAssert)
4031
+ };
4032
+
4033
+ Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) {
4034
+ return writeDouble(this, value, offset, false, noAssert)
4035
+ };
4036
+
4037
+ // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
4038
+ Buffer.prototype.copy = function copy (target, targetStart, start, end) {
4039
+ if (!Buffer.isBuffer(target)) throw new TypeError('argument should be a Buffer')
4040
+ if (!start) start = 0;
4041
+ if (!end && end !== 0) end = this.length;
4042
+ if (targetStart >= target.length) targetStart = target.length;
4043
+ if (!targetStart) targetStart = 0;
4044
+ if (end > 0 && end < start) end = start;
4045
+
4046
+ // Copy 0 bytes; we're done
4047
+ if (end === start) return 0
4048
+ if (target.length === 0 || this.length === 0) return 0
4049
+
4050
+ // Fatal error conditions
4051
+ if (targetStart < 0) {
4052
+ throw new RangeError('targetStart out of bounds')
4053
+ }
4054
+ if (start < 0 || start >= this.length) throw new RangeError('Index out of range')
4055
+ if (end < 0) throw new RangeError('sourceEnd out of bounds')
4056
+
4057
+ // Are we oob?
4058
+ if (end > this.length) end = this.length;
4059
+ if (target.length - targetStart < end - start) {
4060
+ end = target.length - targetStart + start;
4061
+ }
4062
+
4063
+ const len = end - start;
4064
+
4065
+ if (this === target && typeof Uint8Array.prototype.copyWithin === 'function') {
4066
+ // Use built-in when available, missing from IE11
4067
+ this.copyWithin(targetStart, start, end);
4068
+ } else {
4069
+ Uint8Array.prototype.set.call(
4070
+ target,
4071
+ this.subarray(start, end),
4072
+ targetStart
4073
+ );
4074
+ }
4075
+
4076
+ return len
4077
+ };
4078
+
4079
+ // Usage:
4080
+ // buffer.fill(number[, offset[, end]])
4081
+ // buffer.fill(buffer[, offset[, end]])
4082
+ // buffer.fill(string[, offset[, end]][, encoding])
4083
+ Buffer.prototype.fill = function fill (val, start, end, encoding) {
4084
+ // Handle string cases:
4085
+ if (typeof val === 'string') {
4086
+ if (typeof start === 'string') {
4087
+ encoding = start;
4088
+ start = 0;
4089
+ end = this.length;
4090
+ } else if (typeof end === 'string') {
4091
+ encoding = end;
4092
+ end = this.length;
4093
+ }
4094
+ if (encoding !== undefined && typeof encoding !== 'string') {
4095
+ throw new TypeError('encoding must be a string')
4096
+ }
4097
+ if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) {
4098
+ throw new TypeError('Unknown encoding: ' + encoding)
4099
+ }
4100
+ if (val.length === 1) {
4101
+ const code = val.charCodeAt(0);
4102
+ if ((encoding === 'utf8' && code < 128) ||
4103
+ encoding === 'latin1') {
4104
+ // Fast path: If `val` fits into a single byte, use that numeric value.
4105
+ val = code;
4106
+ }
4107
+ }
4108
+ } else if (typeof val === 'number') {
4109
+ val = val & 255;
4110
+ } else if (typeof val === 'boolean') {
4111
+ val = Number(val);
4112
+ }
4113
+
4114
+ // Invalid ranges are not set to a default, so can range check early.
4115
+ if (start < 0 || this.length < start || this.length < end) {
4116
+ throw new RangeError('Out of range index')
4117
+ }
4118
+
4119
+ if (end <= start) {
4120
+ return this
4121
+ }
4122
+
4123
+ start = start >>> 0;
4124
+ end = end === undefined ? this.length : end >>> 0;
4125
+
4126
+ if (!val) val = 0;
4127
+
4128
+ let i;
4129
+ if (typeof val === 'number') {
4130
+ for (i = start; i < end; ++i) {
4131
+ this[i] = val;
4132
+ }
4133
+ } else {
4134
+ const bytes = Buffer.isBuffer(val)
4135
+ ? val
4136
+ : Buffer.from(val, encoding);
4137
+ const len = bytes.length;
4138
+ if (len === 0) {
4139
+ throw new TypeError('The value "' + val +
4140
+ '" is invalid for argument "value"')
4141
+ }
4142
+ for (i = 0; i < end - start; ++i) {
4143
+ this[i + start] = bytes[i % len];
4144
+ }
4145
+ }
4146
+
4147
+ return this
4148
+ };
4149
+
4150
+ // CUSTOM ERRORS
4151
+ // =============
4152
+
4153
+ // Simplified versions from Node, changed for Buffer-only usage
4154
+ const errors = {};
4155
+ function E (sym, getMessage, Base) {
4156
+ errors[sym] = class NodeError extends Base {
4157
+ constructor () {
4158
+ super();
4159
+
4160
+ Object.defineProperty(this, 'message', {
4161
+ value: getMessage.apply(this, arguments),
4162
+ writable: true,
4163
+ configurable: true
4164
+ });
4165
+
4166
+ // Add the error code to the name to include it in the stack trace.
4167
+ this.name = `${this.name} [${sym}]`;
4168
+ // Access the stack to generate the error message including the error code
4169
+ // from the name.
4170
+ this.stack; // eslint-disable-line no-unused-expressions
4171
+ // Reset the name to the actual name.
4172
+ delete this.name;
4173
+ }
4174
+
4175
+ get code () {
4176
+ return sym
4177
+ }
4178
+
4179
+ set code (value) {
4180
+ Object.defineProperty(this, 'code', {
4181
+ configurable: true,
4182
+ enumerable: true,
4183
+ value,
4184
+ writable: true
4185
+ });
4186
+ }
4187
+
4188
+ toString () {
4189
+ return `${this.name} [${sym}]: ${this.message}`
4190
+ }
4191
+ };
4192
+ }
4193
+
4194
+ E('ERR_BUFFER_OUT_OF_BOUNDS',
4195
+ function (name) {
4196
+ if (name) {
4197
+ return `${name} is outside of buffer bounds`
4198
+ }
4199
+
4200
+ return 'Attempt to access memory outside buffer bounds'
4201
+ }, RangeError);
4202
+ E('ERR_INVALID_ARG_TYPE',
4203
+ function (name, actual) {
4204
+ return `The "${name}" argument must be of type number. Received type ${typeof actual}`
4205
+ }, TypeError);
4206
+ E('ERR_OUT_OF_RANGE',
4207
+ function (str, range, input) {
4208
+ let msg = `The value of "${str}" is out of range.`;
4209
+ let received = input;
4210
+ if (Number.isInteger(input) && Math.abs(input) > 2 ** 32) {
4211
+ received = addNumericalSeparator(String(input));
4212
+ } else if (typeof input === 'bigint') {
4213
+ received = String(input);
4214
+ if (input > BigInt(2) ** BigInt(32) || input < -(BigInt(2) ** BigInt(32))) {
4215
+ received = addNumericalSeparator(received);
4216
+ }
4217
+ received += 'n';
4218
+ }
4219
+ msg += ` It must be ${range}. Received ${received}`;
4220
+ return msg
4221
+ }, RangeError);
4222
+
4223
+ function addNumericalSeparator (val) {
4224
+ let res = '';
4225
+ let i = val.length;
4226
+ const start = val[0] === '-' ? 1 : 0;
4227
+ for (; i >= start + 4; i -= 3) {
4228
+ res = `_${val.slice(i - 3, i)}${res}`;
4229
+ }
4230
+ return `${val.slice(0, i)}${res}`
4231
+ }
4232
+
4233
+ // CHECK FUNCTIONS
4234
+ // ===============
4235
+
4236
+ function checkBounds (buf, offset, byteLength) {
4237
+ validateNumber(offset, 'offset');
4238
+ if (buf[offset] === undefined || buf[offset + byteLength] === undefined) {
4239
+ boundsError(offset, buf.length - (byteLength + 1));
4240
+ }
4241
+ }
4242
+
4243
+ function checkIntBI (value, min, max, buf, offset, byteLength) {
4244
+ if (value > max || value < min) {
4245
+ const n = typeof min === 'bigint' ? 'n' : '';
4246
+ let range;
4247
+ {
4248
+ if (min === 0 || min === BigInt(0)) {
4249
+ range = `>= 0${n} and < 2${n} ** ${(byteLength + 1) * 8}${n}`;
4250
+ } else {
4251
+ range = `>= -(2${n} ** ${(byteLength + 1) * 8 - 1}${n}) and < 2 ** ` +
4252
+ `${(byteLength + 1) * 8 - 1}${n}`;
4253
+ }
4254
+ }
4255
+ throw new errors.ERR_OUT_OF_RANGE('value', range, value)
4256
+ }
4257
+ checkBounds(buf, offset, byteLength);
4258
+ }
4259
+
4260
+ function validateNumber (value, name) {
4261
+ if (typeof value !== 'number') {
4262
+ throw new errors.ERR_INVALID_ARG_TYPE(name, 'number', value)
4263
+ }
4264
+ }
4265
+
4266
+ function boundsError (value, length, type) {
4267
+ if (Math.floor(value) !== value) {
4268
+ validateNumber(value, type);
4269
+ throw new errors.ERR_OUT_OF_RANGE('offset', 'an integer', value)
4270
+ }
4271
+
4272
+ if (length < 0) {
4273
+ throw new errors.ERR_BUFFER_OUT_OF_BOUNDS()
4274
+ }
4275
+
4276
+ throw new errors.ERR_OUT_OF_RANGE('offset',
4277
+ `>= ${0} and <= ${length}`,
4278
+ value)
4279
+ }
4280
+
4281
+ // HELPER FUNCTIONS
4282
+ // ================
4283
+
4284
+ const INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g;
4285
+
4286
+ function base64clean (str) {
4287
+ // Node takes equal signs as end of the Base64 encoding
4288
+ str = str.split('=')[0];
4289
+ // Node strips out invalid characters like \n and \t from the string, base64-js does not
4290
+ str = str.trim().replace(INVALID_BASE64_RE, '');
4291
+ // Node converts strings with length < 2 to ''
4292
+ if (str.length < 2) return ''
4293
+ // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not
4294
+ while (str.length % 4 !== 0) {
4295
+ str = str + '=';
4296
+ }
4297
+ return str
4298
+ }
4299
+
4300
+ function utf8ToBytes (string, units) {
4301
+ units = units || Infinity;
4302
+ let codePoint;
4303
+ const length = string.length;
4304
+ let leadSurrogate = null;
4305
+ const bytes = [];
4306
+
4307
+ for (let i = 0; i < length; ++i) {
4308
+ codePoint = string.charCodeAt(i);
4309
+
4310
+ // is surrogate component
4311
+ if (codePoint > 0xD7FF && codePoint < 0xE000) {
4312
+ // last char was a lead
4313
+ if (!leadSurrogate) {
4314
+ // no lead yet
4315
+ if (codePoint > 0xDBFF) {
4316
+ // unexpected trail
4317
+ if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
4318
+ continue
4319
+ } else if (i + 1 === length) {
4320
+ // unpaired lead
4321
+ if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
4322
+ continue
4323
+ }
4324
+
4325
+ // valid lead
4326
+ leadSurrogate = codePoint;
4327
+
4328
+ continue
4329
+ }
4330
+
4331
+ // 2 leads in a row
4332
+ if (codePoint < 0xDC00) {
4333
+ if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
4334
+ leadSurrogate = codePoint;
4335
+ continue
4336
+ }
4337
+
4338
+ // valid surrogate pair
4339
+ codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000;
4340
+ } else if (leadSurrogate) {
4341
+ // valid bmp char, but last char was a lead
4342
+ if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
4343
+ }
4344
+
4345
+ leadSurrogate = null;
4346
+
4347
+ // encode utf8
4348
+ if (codePoint < 0x80) {
4349
+ if ((units -= 1) < 0) break
4350
+ bytes.push(codePoint);
4351
+ } else if (codePoint < 0x800) {
4352
+ if ((units -= 2) < 0) break
4353
+ bytes.push(
4354
+ codePoint >> 0x6 | 0xC0,
4355
+ codePoint & 0x3F | 0x80
4356
+ );
4357
+ } else if (codePoint < 0x10000) {
4358
+ if ((units -= 3) < 0) break
4359
+ bytes.push(
4360
+ codePoint >> 0xC | 0xE0,
4361
+ codePoint >> 0x6 & 0x3F | 0x80,
4362
+ codePoint & 0x3F | 0x80
4363
+ );
4364
+ } else if (codePoint < 0x110000) {
4365
+ if ((units -= 4) < 0) break
4366
+ bytes.push(
4367
+ codePoint >> 0x12 | 0xF0,
4368
+ codePoint >> 0xC & 0x3F | 0x80,
4369
+ codePoint >> 0x6 & 0x3F | 0x80,
4370
+ codePoint & 0x3F | 0x80
4371
+ );
4372
+ } else {
4373
+ throw new Error('Invalid code point')
4374
+ }
4375
+ }
4376
+
4377
+ return bytes
4378
+ }
4379
+
4380
+ function asciiToBytes (str) {
4381
+ const byteArray = [];
4382
+ for (let i = 0; i < str.length; ++i) {
4383
+ // Node's code seems to be doing this and not & 0x7F..
4384
+ byteArray.push(str.charCodeAt(i) & 0xFF);
4385
+ }
4386
+ return byteArray
4387
+ }
4388
+
4389
+ function utf16leToBytes (str, units) {
4390
+ let c, hi, lo;
4391
+ const byteArray = [];
4392
+ for (let i = 0; i < str.length; ++i) {
4393
+ if ((units -= 2) < 0) break
4394
+
4395
+ c = str.charCodeAt(i);
4396
+ hi = c >> 8;
4397
+ lo = c % 256;
4398
+ byteArray.push(lo);
4399
+ byteArray.push(hi);
4400
+ }
4401
+
4402
+ return byteArray
4403
+ }
4404
+
4405
+ function base64ToBytes (str) {
4406
+ return base64.toByteArray(base64clean(str))
4407
+ }
4408
+
4409
+ function blitBuffer (src, dst, offset, length) {
4410
+ let i;
4411
+ for (i = 0; i < length; ++i) {
4412
+ if ((i + offset >= dst.length) || (i >= src.length)) break
4413
+ dst[i + offset] = src[i];
4414
+ }
4415
+ return i
4416
+ }
4417
+
4418
+ // ArrayBuffer or Uint8Array objects from other contexts (i.e. iframes) do not pass
4419
+ // the `instanceof` check but they should be treated as of that type.
4420
+ // See: https://github.com/feross/buffer/issues/166
4421
+ function isInstance (obj, type) {
4422
+ return obj instanceof type ||
4423
+ (obj != null && obj.constructor != null && obj.constructor.name != null &&
4424
+ obj.constructor.name === type.name)
4425
+ }
4426
+ function numberIsNaN (obj) {
4427
+ // For IE11 support
4428
+ return obj !== obj // eslint-disable-line no-self-compare
4429
+ }
4430
+
4431
+ // Create lookup table for `toString('hex')`
4432
+ // See: https://github.com/feross/buffer/issues/219
4433
+ const hexSliceLookupTable = (function () {
4434
+ const alphabet = '0123456789abcdef';
4435
+ const table = new Array(256);
4436
+ for (let i = 0; i < 16; ++i) {
4437
+ const i16 = i * 16;
4438
+ for (let j = 0; j < 16; ++j) {
4439
+ table[i16 + j] = alphabet[i] + alphabet[j];
4440
+ }
4441
+ }
4442
+ return table
4443
+ })();
4444
+
4445
+ // Return not function with Error if BigInt not supported
4446
+ function defineBigIntMethod (fn) {
4447
+ return typeof BigInt === 'undefined' ? BufferBigIntNotDefined : fn
4448
+ }
4449
+
4450
+ function BufferBigIntNotDefined () {
4451
+ throw new Error('BigInt not supported')
4452
+ }
4453
+ } (buffer$1));
4454
+ return buffer$1;
4455
+ }
4456
+
4457
+ var bufferExports$1 = requireBuffer$1();
4458
+
4459
+ var dist = {};
4460
+
4461
+ var buffer = {};
4462
+
4463
+ /*!
4464
+ * The buffer module from node.js, for the browser.
4465
+ *
4466
+ * @author Feross Aboukhadijeh <https://feross.org>
4467
+ * @license MIT
4468
+ */
4469
+
4470
+ var hasRequiredBuffer;
4471
+
4472
+ function requireBuffer () {
4473
+ if (hasRequiredBuffer) return buffer;
4474
+ hasRequiredBuffer = 1;
4475
+ (function (exports) {
4476
+
4477
+ var base64 = requireBase64Js();
4478
+ var ieee754 = requireIeee754();
4479
+ var customInspectSymbol =
4480
+ (typeof Symbol === 'function' && typeof Symbol['for'] === 'function') // eslint-disable-line dot-notation
4481
+ ? Symbol['for']('nodejs.util.inspect.custom') // eslint-disable-line dot-notation
4482
+ : null;
4483
+
4484
+ exports.Buffer = Buffer;
4485
+ exports.SlowBuffer = SlowBuffer;
4486
+ exports.INSPECT_MAX_BYTES = 50;
4487
+
4488
+ var K_MAX_LENGTH = 0x7fffffff;
4489
+ exports.kMaxLength = K_MAX_LENGTH;
4490
+
4491
+ /**
4492
+ * If `Buffer.TYPED_ARRAY_SUPPORT`:
4493
+ * === true Use Uint8Array implementation (fastest)
4494
+ * === false Print warning and recommend using `buffer` v4.x which has an Object
4495
+ * implementation (most compatible, even IE6)
4496
+ *
4497
+ * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+,
4498
+ * Opera 11.6+, iOS 4.2+.
4499
+ *
4500
+ * We report that the browser does not support typed arrays if the are not subclassable
4501
+ * using __proto__. Firefox 4-29 lacks support for adding new properties to `Uint8Array`
4502
+ * (See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438). IE 10 lacks support
4503
+ * for __proto__ and has a buggy typed array implementation.
4504
+ */
4505
+ Buffer.TYPED_ARRAY_SUPPORT = typedArraySupport();
4506
+
4507
+ if (!Buffer.TYPED_ARRAY_SUPPORT && typeof console !== 'undefined' &&
4508
+ typeof console.error === 'function') {
4509
+ console.error(
4510
+ 'This browser lacks typed array (Uint8Array) support which is required by ' +
4511
+ '`buffer` v5.x. Use `buffer` v4.x if you require old browser support.'
4512
+ );
4513
+ }
4514
+
4515
+ function typedArraySupport () {
4516
+ // Can typed array instances can be augmented?
4517
+ try {
4518
+ var arr = new Uint8Array(1);
4519
+ var proto = { foo: function () { return 42 } };
4520
+ Object.setPrototypeOf(proto, Uint8Array.prototype);
4521
+ Object.setPrototypeOf(arr, proto);
4522
+ return arr.foo() === 42
4523
+ } catch (e) {
4524
+ return false
4525
+ }
4526
+ }
4527
+
4528
+ Object.defineProperty(Buffer.prototype, 'parent', {
4529
+ enumerable: true,
4530
+ get: function () {
4531
+ if (!Buffer.isBuffer(this)) return undefined
4532
+ return this.buffer
4533
+ }
4534
+ });
4535
+
4536
+ Object.defineProperty(Buffer.prototype, 'offset', {
4537
+ enumerable: true,
4538
+ get: function () {
4539
+ if (!Buffer.isBuffer(this)) return undefined
4540
+ return this.byteOffset
4541
+ }
4542
+ });
4543
+
4544
+ function createBuffer (length) {
4545
+ if (length > K_MAX_LENGTH) {
4546
+ throw new RangeError('The value "' + length + '" is invalid for option "size"')
4547
+ }
4548
+ // Return an augmented `Uint8Array` instance
4549
+ var buf = new Uint8Array(length);
4550
+ Object.setPrototypeOf(buf, Buffer.prototype);
4551
+ return buf
4552
+ }
4553
+
4554
+ /**
4555
+ * The Buffer constructor returns instances of `Uint8Array` that have their
4556
+ * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of
4557
+ * `Uint8Array`, so the returned instances will have all the node `Buffer` methods
4558
+ * and the `Uint8Array` methods. Square bracket notation works as expected -- it
4559
+ * returns a single octet.
4560
+ *
4561
+ * The `Uint8Array` prototype remains unmodified.
4562
+ */
4563
+
4564
+ function Buffer (arg, encodingOrOffset, length) {
4565
+ // Common case.
4566
+ if (typeof arg === 'number') {
4567
+ if (typeof encodingOrOffset === 'string') {
4568
+ throw new TypeError(
4569
+ 'The "string" argument must be of type string. Received type number'
4570
+ )
4571
+ }
4572
+ return allocUnsafe(arg)
4573
+ }
4574
+ return from(arg, encodingOrOffset, length)
4575
+ }
4576
+
4577
+ Buffer.poolSize = 8192; // not used by this implementation
4578
+
4579
+ function from (value, encodingOrOffset, length) {
4580
+ if (typeof value === 'string') {
4581
+ return fromString(value, encodingOrOffset)
4582
+ }
4583
+
4584
+ if (ArrayBuffer.isView(value)) {
4585
+ return fromArrayView(value)
4586
+ }
4587
+
4588
+ if (value == null) {
4589
+ throw new TypeError(
4590
+ 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' +
4591
+ 'or Array-like Object. Received type ' + (typeof value)
4592
+ )
4593
+ }
4594
+
4595
+ if (isInstance(value, ArrayBuffer) ||
4596
+ (value && isInstance(value.buffer, ArrayBuffer))) {
4597
+ return fromArrayBuffer(value, encodingOrOffset, length)
4598
+ }
4599
+
4600
+ if (typeof SharedArrayBuffer !== 'undefined' &&
4601
+ (isInstance(value, SharedArrayBuffer) ||
4602
+ (value && isInstance(value.buffer, SharedArrayBuffer)))) {
4603
+ return fromArrayBuffer(value, encodingOrOffset, length)
4604
+ }
4605
+
4606
+ if (typeof value === 'number') {
4607
+ throw new TypeError(
4608
+ 'The "value" argument must not be of type number. Received type number'
4609
+ )
4610
+ }
4611
+
4612
+ var valueOf = value.valueOf && value.valueOf();
4613
+ if (valueOf != null && valueOf !== value) {
4614
+ return Buffer.from(valueOf, encodingOrOffset, length)
4615
+ }
4616
+
4617
+ var b = fromObject(value);
4618
+ if (b) return b
4619
+
4620
+ if (typeof Symbol !== 'undefined' && Symbol.toPrimitive != null &&
4621
+ typeof value[Symbol.toPrimitive] === 'function') {
4622
+ return Buffer.from(
4623
+ value[Symbol.toPrimitive]('string'), encodingOrOffset, length
4624
+ )
4625
+ }
4626
+
4627
+ throw new TypeError(
4628
+ 'The first argument must be one of type string, Buffer, ArrayBuffer, Array, ' +
4629
+ 'or Array-like Object. Received type ' + (typeof value)
4630
+ )
4631
+ }
4632
+
4633
+ /**
4634
+ * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError
4635
+ * if value is a number.
4636
+ * Buffer.from(str[, encoding])
4637
+ * Buffer.from(array)
4638
+ * Buffer.from(buffer)
4639
+ * Buffer.from(arrayBuffer[, byteOffset[, length]])
4640
+ **/
4641
+ Buffer.from = function (value, encodingOrOffset, length) {
4642
+ return from(value, encodingOrOffset, length)
4643
+ };
4644
+
4645
+ // Note: Change prototype *after* Buffer.from is defined to workaround Chrome bug:
4646
+ // https://github.com/feross/buffer/pull/148
4647
+ Object.setPrototypeOf(Buffer.prototype, Uint8Array.prototype);
4648
+ Object.setPrototypeOf(Buffer, Uint8Array);
4649
+
4650
+ function assertSize (size) {
4651
+ if (typeof size !== 'number') {
4652
+ throw new TypeError('"size" argument must be of type number')
4653
+ } else if (size < 0) {
4654
+ throw new RangeError('The value "' + size + '" is invalid for option "size"')
4655
+ }
4656
+ }
4657
+
4658
+ function alloc (size, fill, encoding) {
4659
+ assertSize(size);
4660
+ if (size <= 0) {
4661
+ return createBuffer(size)
4662
+ }
4663
+ if (fill !== undefined) {
4664
+ // Only pay attention to encoding if it's a string. This
4665
+ // prevents accidentally sending in a number that would
4666
+ // be interpreted as a start offset.
4667
+ return typeof encoding === 'string'
4668
+ ? createBuffer(size).fill(fill, encoding)
4669
+ : createBuffer(size).fill(fill)
4670
+ }
4671
+ return createBuffer(size)
4672
+ }
4673
+
4674
+ /**
4675
+ * Creates a new filled Buffer instance.
4676
+ * alloc(size[, fill[, encoding]])
4677
+ **/
4678
+ Buffer.alloc = function (size, fill, encoding) {
4679
+ return alloc(size, fill, encoding)
4680
+ };
4681
+
4682
+ function allocUnsafe (size) {
4683
+ assertSize(size);
4684
+ return createBuffer(size < 0 ? 0 : checked(size) | 0)
4685
+ }
4686
+
4687
+ /**
4688
+ * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance.
4689
+ * */
4690
+ Buffer.allocUnsafe = function (size) {
4691
+ return allocUnsafe(size)
4692
+ };
4693
+ /**
4694
+ * Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance.
4695
+ */
4696
+ Buffer.allocUnsafeSlow = function (size) {
4697
+ return allocUnsafe(size)
4698
+ };
4699
+
4700
+ function fromString (string, encoding) {
4701
+ if (typeof encoding !== 'string' || encoding === '') {
4702
+ encoding = 'utf8';
4703
+ }
4704
+
4705
+ if (!Buffer.isEncoding(encoding)) {
4706
+ throw new TypeError('Unknown encoding: ' + encoding)
4707
+ }
4708
+
4709
+ var length = byteLength(string, encoding) | 0;
4710
+ var buf = createBuffer(length);
4711
+
4712
+ var actual = buf.write(string, encoding);
4713
+
4714
+ if (actual !== length) {
4715
+ // Writing a hex string, for example, that contains invalid characters will
4716
+ // cause everything after the first invalid character to be ignored. (e.g.
4717
+ // 'abxxcd' will be treated as 'ab')
4718
+ buf = buf.slice(0, actual);
4719
+ }
4720
+
4721
+ return buf
4722
+ }
4723
+
4724
+ function fromArrayLike (array) {
4725
+ var length = array.length < 0 ? 0 : checked(array.length) | 0;
4726
+ var buf = createBuffer(length);
4727
+ for (var i = 0; i < length; i += 1) {
4728
+ buf[i] = array[i] & 255;
4729
+ }
4730
+ return buf
4731
+ }
4732
+
4733
+ function fromArrayView (arrayView) {
4734
+ if (isInstance(arrayView, Uint8Array)) {
4735
+ var copy = new Uint8Array(arrayView);
4736
+ return fromArrayBuffer(copy.buffer, copy.byteOffset, copy.byteLength)
4737
+ }
4738
+ return fromArrayLike(arrayView)
4739
+ }
4740
+
4741
+ function fromArrayBuffer (array, byteOffset, length) {
4742
+ if (byteOffset < 0 || array.byteLength < byteOffset) {
4743
+ throw new RangeError('"offset" is outside of buffer bounds')
4744
+ }
4745
+
4746
+ if (array.byteLength < byteOffset + (length || 0)) {
4747
+ throw new RangeError('"length" is outside of buffer bounds')
4748
+ }
4749
+
4750
+ var buf;
4751
+ if (byteOffset === undefined && length === undefined) {
4752
+ buf = new Uint8Array(array);
4753
+ } else if (length === undefined) {
4754
+ buf = new Uint8Array(array, byteOffset);
4755
+ } else {
4756
+ buf = new Uint8Array(array, byteOffset, length);
4757
+ }
4758
+
4759
+ // Return an augmented `Uint8Array` instance
4760
+ Object.setPrototypeOf(buf, Buffer.prototype);
4761
+
4762
+ return buf
4763
+ }
4764
+
4765
+ function fromObject (obj) {
4766
+ if (Buffer.isBuffer(obj)) {
4767
+ var len = checked(obj.length) | 0;
4768
+ var buf = createBuffer(len);
4769
+
4770
+ if (buf.length === 0) {
4771
+ return buf
4772
+ }
4773
+
4774
+ obj.copy(buf, 0, 0, len);
4775
+ return buf
4776
+ }
4777
+
4778
+ if (obj.length !== undefined) {
4779
+ if (typeof obj.length !== 'number' || numberIsNaN(obj.length)) {
4780
+ return createBuffer(0)
4781
+ }
4782
+ return fromArrayLike(obj)
4783
+ }
4784
+
4785
+ if (obj.type === 'Buffer' && Array.isArray(obj.data)) {
4786
+ return fromArrayLike(obj.data)
4787
+ }
4788
+ }
4789
+
4790
+ function checked (length) {
4791
+ // Note: cannot use `length < K_MAX_LENGTH` here because that fails when
4792
+ // length is NaN (which is otherwise coerced to zero.)
4793
+ if (length >= K_MAX_LENGTH) {
4794
+ throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
4795
+ 'size: 0x' + K_MAX_LENGTH.toString(16) + ' bytes')
4796
+ }
4797
+ return length | 0
4798
+ }
4799
+
4800
+ function SlowBuffer (length) {
4801
+ if (+length != length) { // eslint-disable-line eqeqeq
4802
+ length = 0;
4803
+ }
4804
+ return Buffer.alloc(+length)
4805
+ }
4806
+
4807
+ Buffer.isBuffer = function isBuffer (b) {
4808
+ return b != null && b._isBuffer === true &&
4809
+ b !== Buffer.prototype // so Buffer.isBuffer(Buffer.prototype) will be false
4810
+ };
4811
+
4812
+ Buffer.compare = function compare (a, b) {
4813
+ if (isInstance(a, Uint8Array)) a = Buffer.from(a, a.offset, a.byteLength);
4814
+ if (isInstance(b, Uint8Array)) b = Buffer.from(b, b.offset, b.byteLength);
4815
+ if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) {
4816
+ throw new TypeError(
4817
+ 'The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array'
4818
+ )
4819
+ }
4820
+
4821
+ if (a === b) return 0
4822
+
4823
+ var x = a.length;
4824
+ var y = b.length;
4825
+
4826
+ for (var i = 0, len = Math.min(x, y); i < len; ++i) {
4827
+ if (a[i] !== b[i]) {
4828
+ x = a[i];
4829
+ y = b[i];
4830
+ break
4831
+ }
4832
+ }
4833
+
4834
+ if (x < y) return -1
4835
+ if (y < x) return 1
4836
+ return 0
4837
+ };
4838
+
4839
+ Buffer.isEncoding = function isEncoding (encoding) {
4840
+ switch (String(encoding).toLowerCase()) {
4841
+ case 'hex':
4842
+ case 'utf8':
4843
+ case 'utf-8':
4844
+ case 'ascii':
4845
+ case 'latin1':
4846
+ case 'binary':
4847
+ case 'base64':
4848
+ case 'ucs2':
4849
+ case 'ucs-2':
4850
+ case 'utf16le':
4851
+ case 'utf-16le':
4852
+ return true
4853
+ default:
4854
+ return false
4855
+ }
4856
+ };
4857
+
4858
+ Buffer.concat = function concat (list, length) {
4859
+ if (!Array.isArray(list)) {
4860
+ throw new TypeError('"list" argument must be an Array of Buffers')
4861
+ }
4862
+
4863
+ if (list.length === 0) {
4864
+ return Buffer.alloc(0)
4865
+ }
4866
+
4867
+ var i;
4868
+ if (length === undefined) {
4869
+ length = 0;
4870
+ for (i = 0; i < list.length; ++i) {
4871
+ length += list[i].length;
4872
+ }
4873
+ }
4874
+
4875
+ var buffer = Buffer.allocUnsafe(length);
4876
+ var pos = 0;
4877
+ for (i = 0; i < list.length; ++i) {
4878
+ var buf = list[i];
4879
+ if (isInstance(buf, Uint8Array)) {
4880
+ if (pos + buf.length > buffer.length) {
4881
+ Buffer.from(buf).copy(buffer, pos);
4882
+ } else {
4883
+ Uint8Array.prototype.set.call(
4884
+ buffer,
4885
+ buf,
4886
+ pos
4887
+ );
4888
+ }
4889
+ } else if (!Buffer.isBuffer(buf)) {
4890
+ throw new TypeError('"list" argument must be an Array of Buffers')
4891
+ } else {
4892
+ buf.copy(buffer, pos);
4893
+ }
4894
+ pos += buf.length;
4895
+ }
4896
+ return buffer
4897
+ };
4898
+
4899
+ function byteLength (string, encoding) {
4900
+ if (Buffer.isBuffer(string)) {
4901
+ return string.length
4902
+ }
4903
+ if (ArrayBuffer.isView(string) || isInstance(string, ArrayBuffer)) {
4904
+ return string.byteLength
4905
+ }
4906
+ if (typeof string !== 'string') {
4907
+ throw new TypeError(
4908
+ 'The "string" argument must be one of type string, Buffer, or ArrayBuffer. ' +
4909
+ 'Received type ' + typeof string
4910
+ )
4911
+ }
4912
+
4913
+ var len = string.length;
4914
+ var mustMatch = (arguments.length > 2 && arguments[2] === true);
4915
+ if (!mustMatch && len === 0) return 0
4916
+
4917
+ // Use a for loop to avoid recursion
4918
+ var loweredCase = false;
4919
+ for (;;) {
4920
+ switch (encoding) {
4921
+ case 'ascii':
4922
+ case 'latin1':
4923
+ case 'binary':
4924
+ return len
4925
+ case 'utf8':
4926
+ case 'utf-8':
4927
+ return utf8ToBytes(string).length
4928
+ case 'ucs2':
4929
+ case 'ucs-2':
4930
+ case 'utf16le':
4931
+ case 'utf-16le':
4932
+ return len * 2
4933
+ case 'hex':
4934
+ return len >>> 1
4935
+ case 'base64':
4936
+ return base64ToBytes(string).length
4937
+ default:
4938
+ if (loweredCase) {
4939
+ return mustMatch ? -1 : utf8ToBytes(string).length // assume utf8
4940
+ }
4941
+ encoding = ('' + encoding).toLowerCase();
4942
+ loweredCase = true;
4943
+ }
4944
+ }
4945
+ }
4946
+ Buffer.byteLength = byteLength;
4947
+
4948
+ function slowToString (encoding, start, end) {
4949
+ var loweredCase = false;
4950
+
4951
+ // No need to verify that "this.length <= MAX_UINT32" since it's a read-only
4952
+ // property of a typed array.
4953
+
4954
+ // This behaves neither like String nor Uint8Array in that we set start/end
4955
+ // to their upper/lower bounds if the value passed is out of range.
4956
+ // undefined is handled specially as per ECMA-262 6th Edition,
4957
+ // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization.
4958
+ if (start === undefined || start < 0) {
4959
+ start = 0;
4960
+ }
4961
+ // Return early if start > this.length. Done here to prevent potential uint32
4962
+ // coercion fail below.
4963
+ if (start > this.length) {
4964
+ return ''
4965
+ }
4966
+
4967
+ if (end === undefined || end > this.length) {
4968
+ end = this.length;
4969
+ }
4970
+
4971
+ if (end <= 0) {
4972
+ return ''
4973
+ }
4974
+
4975
+ // Force coercion to uint32. This will also coerce falsey/NaN values to 0.
4976
+ end >>>= 0;
4977
+ start >>>= 0;
4978
+
4979
+ if (end <= start) {
4980
+ return ''
4981
+ }
4982
+
4983
+ if (!encoding) encoding = 'utf8';
4984
+
4985
+ while (true) {
4986
+ switch (encoding) {
4987
+ case 'hex':
4988
+ return hexSlice(this, start, end)
4989
+
4990
+ case 'utf8':
4991
+ case 'utf-8':
4992
+ return utf8Slice(this, start, end)
4993
+
4994
+ case 'ascii':
4995
+ return asciiSlice(this, start, end)
4996
+
4997
+ case 'latin1':
4998
+ case 'binary':
4999
+ return latin1Slice(this, start, end)
5000
+
5001
+ case 'base64':
5002
+ return base64Slice(this, start, end)
5003
+
5004
+ case 'ucs2':
5005
+ case 'ucs-2':
5006
+ case 'utf16le':
5007
+ case 'utf-16le':
5008
+ return utf16leSlice(this, start, end)
5009
+
5010
+ default:
5011
+ if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
5012
+ encoding = (encoding + '').toLowerCase();
5013
+ loweredCase = true;
5014
+ }
5015
+ }
5016
+ }
5017
+
5018
+ // This property is used by `Buffer.isBuffer` (and the `is-buffer` npm package)
5019
+ // to detect a Buffer instance. It's not possible to use `instanceof Buffer`
5020
+ // reliably in a browserify context because there could be multiple different
5021
+ // copies of the 'buffer' package in use. This method works even for Buffer
5022
+ // instances that were created from another copy of the `buffer` package.
5023
+ // See: https://github.com/feross/buffer/issues/154
5024
+ Buffer.prototype._isBuffer = true;
5025
+
5026
+ function swap (b, n, m) {
5027
+ var i = b[n];
5028
+ b[n] = b[m];
5029
+ b[m] = i;
5030
+ }
5031
+
5032
+ Buffer.prototype.swap16 = function swap16 () {
5033
+ var len = this.length;
5034
+ if (len % 2 !== 0) {
5035
+ throw new RangeError('Buffer size must be a multiple of 16-bits')
5036
+ }
5037
+ for (var i = 0; i < len; i += 2) {
5038
+ swap(this, i, i + 1);
5039
+ }
5040
+ return this
5041
+ };
5042
+
5043
+ Buffer.prototype.swap32 = function swap32 () {
5044
+ var len = this.length;
5045
+ if (len % 4 !== 0) {
5046
+ throw new RangeError('Buffer size must be a multiple of 32-bits')
5047
+ }
5048
+ for (var i = 0; i < len; i += 4) {
5049
+ swap(this, i, i + 3);
5050
+ swap(this, i + 1, i + 2);
5051
+ }
5052
+ return this
5053
+ };
5054
+
5055
+ Buffer.prototype.swap64 = function swap64 () {
5056
+ var len = this.length;
5057
+ if (len % 8 !== 0) {
5058
+ throw new RangeError('Buffer size must be a multiple of 64-bits')
5059
+ }
5060
+ for (var i = 0; i < len; i += 8) {
5061
+ swap(this, i, i + 7);
5062
+ swap(this, i + 1, i + 6);
5063
+ swap(this, i + 2, i + 5);
5064
+ swap(this, i + 3, i + 4);
5065
+ }
5066
+ return this
5067
+ };
5068
+
5069
+ Buffer.prototype.toString = function toString () {
5070
+ var length = this.length;
5071
+ if (length === 0) return ''
5072
+ if (arguments.length === 0) return utf8Slice(this, 0, length)
5073
+ return slowToString.apply(this, arguments)
5074
+ };
5075
+
5076
+ Buffer.prototype.toLocaleString = Buffer.prototype.toString;
5077
+
5078
+ Buffer.prototype.equals = function equals (b) {
5079
+ if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer')
5080
+ if (this === b) return true
5081
+ return Buffer.compare(this, b) === 0
5082
+ };
5083
+
5084
+ Buffer.prototype.inspect = function inspect () {
5085
+ var str = '';
5086
+ var max = exports.INSPECT_MAX_BYTES;
5087
+ str = this.toString('hex', 0, max).replace(/(.{2})/g, '$1 ').trim();
5088
+ if (this.length > max) str += ' ... ';
5089
+ return '<Buffer ' + str + '>'
5090
+ };
5091
+ if (customInspectSymbol) {
5092
+ Buffer.prototype[customInspectSymbol] = Buffer.prototype.inspect;
5093
+ }
5094
+
5095
+ Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) {
5096
+ if (isInstance(target, Uint8Array)) {
5097
+ target = Buffer.from(target, target.offset, target.byteLength);
5098
+ }
5099
+ if (!Buffer.isBuffer(target)) {
5100
+ throw new TypeError(
5101
+ 'The "target" argument must be one of type Buffer or Uint8Array. ' +
5102
+ 'Received type ' + (typeof target)
5103
+ )
5104
+ }
5105
+
5106
+ if (start === undefined) {
5107
+ start = 0;
5108
+ }
5109
+ if (end === undefined) {
5110
+ end = target ? target.length : 0;
5111
+ }
5112
+ if (thisStart === undefined) {
5113
+ thisStart = 0;
5114
+ }
5115
+ if (thisEnd === undefined) {
5116
+ thisEnd = this.length;
5117
+ }
5118
+
5119
+ if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) {
5120
+ throw new RangeError('out of range index')
5121
+ }
5122
+
5123
+ if (thisStart >= thisEnd && start >= end) {
5124
+ return 0
5125
+ }
5126
+ if (thisStart >= thisEnd) {
5127
+ return -1
5128
+ }
5129
+ if (start >= end) {
5130
+ return 1
5131
+ }
5132
+
5133
+ start >>>= 0;
5134
+ end >>>= 0;
5135
+ thisStart >>>= 0;
5136
+ thisEnd >>>= 0;
5137
+
5138
+ if (this === target) return 0
5139
+
5140
+ var x = thisEnd - thisStart;
5141
+ var y = end - start;
5142
+ var len = Math.min(x, y);
5143
+
5144
+ var thisCopy = this.slice(thisStart, thisEnd);
5145
+ var targetCopy = target.slice(start, end);
5146
+
5147
+ for (var i = 0; i < len; ++i) {
5148
+ if (thisCopy[i] !== targetCopy[i]) {
5149
+ x = thisCopy[i];
5150
+ y = targetCopy[i];
5151
+ break
5152
+ }
5153
+ }
5154
+
5155
+ if (x < y) return -1
5156
+ if (y < x) return 1
5157
+ return 0
5158
+ };
5159
+
5160
+ // Finds either the first index of `val` in `buffer` at offset >= `byteOffset`,
5161
+ // OR the last index of `val` in `buffer` at offset <= `byteOffset`.
5162
+ //
5163
+ // Arguments:
5164
+ // - buffer - a Buffer to search
5165
+ // - val - a string, Buffer, or number
5166
+ // - byteOffset - an index into `buffer`; will be clamped to an int32
5167
+ // - encoding - an optional encoding, relevant is val is a string
5168
+ // - dir - true for indexOf, false for lastIndexOf
5169
+ function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) {
5170
+ // Empty buffer means no match
5171
+ if (buffer.length === 0) return -1
5172
+
5173
+ // Normalize byteOffset
5174
+ if (typeof byteOffset === 'string') {
5175
+ encoding = byteOffset;
5176
+ byteOffset = 0;
5177
+ } else if (byteOffset > 0x7fffffff) {
5178
+ byteOffset = 0x7fffffff;
5179
+ } else if (byteOffset < -2147483648) {
5180
+ byteOffset = -2147483648;
5181
+ }
5182
+ byteOffset = +byteOffset; // Coerce to Number.
5183
+ if (numberIsNaN(byteOffset)) {
5184
+ // byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer
5185
+ byteOffset = dir ? 0 : (buffer.length - 1);
5186
+ }
5187
+
5188
+ // Normalize byteOffset: negative offsets start from the end of the buffer
5189
+ if (byteOffset < 0) byteOffset = buffer.length + byteOffset;
5190
+ if (byteOffset >= buffer.length) {
5191
+ if (dir) return -1
5192
+ else byteOffset = buffer.length - 1;
5193
+ } else if (byteOffset < 0) {
5194
+ if (dir) byteOffset = 0;
5195
+ else return -1
5196
+ }
5197
+
5198
+ // Normalize val
5199
+ if (typeof val === 'string') {
5200
+ val = Buffer.from(val, encoding);
5201
+ }
5202
+
5203
+ // Finally, search either indexOf (if dir is true) or lastIndexOf
5204
+ if (Buffer.isBuffer(val)) {
5205
+ // Special case: looking for empty string/buffer always fails
5206
+ if (val.length === 0) {
5207
+ return -1
5208
+ }
5209
+ return arrayIndexOf(buffer, val, byteOffset, encoding, dir)
5210
+ } else if (typeof val === 'number') {
5211
+ val = val & 0xFF; // Search for a byte value [0-255]
5212
+ if (typeof Uint8Array.prototype.indexOf === 'function') {
5213
+ if (dir) {
5214
+ return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset)
5215
+ } else {
5216
+ return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset)
5217
+ }
5218
+ }
5219
+ return arrayIndexOf(buffer, [val], byteOffset, encoding, dir)
5220
+ }
5221
+
5222
+ throw new TypeError('val must be string, number or Buffer')
5223
+ }
5224
+
5225
+ function arrayIndexOf (arr, val, byteOffset, encoding, dir) {
5226
+ var indexSize = 1;
5227
+ var arrLength = arr.length;
5228
+ var valLength = val.length;
5229
+
5230
+ if (encoding !== undefined) {
5231
+ encoding = String(encoding).toLowerCase();
5232
+ if (encoding === 'ucs2' || encoding === 'ucs-2' ||
5233
+ encoding === 'utf16le' || encoding === 'utf-16le') {
5234
+ if (arr.length < 2 || val.length < 2) {
5235
+ return -1
5236
+ }
5237
+ indexSize = 2;
5238
+ arrLength /= 2;
5239
+ valLength /= 2;
5240
+ byteOffset /= 2;
5241
+ }
5242
+ }
5243
+
5244
+ function read (buf, i) {
5245
+ if (indexSize === 1) {
5246
+ return buf[i]
5247
+ } else {
5248
+ return buf.readUInt16BE(i * indexSize)
5249
+ }
5250
+ }
5251
+
5252
+ var i;
5253
+ if (dir) {
5254
+ var foundIndex = -1;
5255
+ for (i = byteOffset; i < arrLength; i++) {
5256
+ if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) {
5257
+ if (foundIndex === -1) foundIndex = i;
5258
+ if (i - foundIndex + 1 === valLength) return foundIndex * indexSize
5259
+ } else {
5260
+ if (foundIndex !== -1) i -= i - foundIndex;
5261
+ foundIndex = -1;
5262
+ }
5263
+ }
5264
+ } else {
5265
+ if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength;
5266
+ for (i = byteOffset; i >= 0; i--) {
5267
+ var found = true;
5268
+ for (var j = 0; j < valLength; j++) {
5269
+ if (read(arr, i + j) !== read(val, j)) {
5270
+ found = false;
5271
+ break
5272
+ }
5273
+ }
5274
+ if (found) return i
5275
+ }
5276
+ }
5277
+
5278
+ return -1
5279
+ }
5280
+
5281
+ Buffer.prototype.includes = function includes (val, byteOffset, encoding) {
5282
+ return this.indexOf(val, byteOffset, encoding) !== -1
5283
+ };
5284
+
5285
+ Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) {
5286
+ return bidirectionalIndexOf(this, val, byteOffset, encoding, true)
5287
+ };
5288
+
5289
+ Buffer.prototype.lastIndexOf = function lastIndexOf (val, byteOffset, encoding) {
5290
+ return bidirectionalIndexOf(this, val, byteOffset, encoding, false)
5291
+ };
5292
+
5293
+ function hexWrite (buf, string, offset, length) {
5294
+ offset = Number(offset) || 0;
5295
+ var remaining = buf.length - offset;
5296
+ if (!length) {
5297
+ length = remaining;
5298
+ } else {
5299
+ length = Number(length);
5300
+ if (length > remaining) {
5301
+ length = remaining;
5302
+ }
5303
+ }
5304
+
5305
+ var strLen = string.length;
5306
+
5307
+ if (length > strLen / 2) {
5308
+ length = strLen / 2;
5309
+ }
5310
+ for (var i = 0; i < length; ++i) {
5311
+ var parsed = parseInt(string.substr(i * 2, 2), 16);
5312
+ if (numberIsNaN(parsed)) return i
5313
+ buf[offset + i] = parsed;
5314
+ }
5315
+ return i
5316
+ }
5317
+
5318
+ function utf8Write (buf, string, offset, length) {
5319
+ return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length)
5320
+ }
5321
+
5322
+ function asciiWrite (buf, string, offset, length) {
5323
+ return blitBuffer(asciiToBytes(string), buf, offset, length)
5324
+ }
5325
+
5326
+ function base64Write (buf, string, offset, length) {
5327
+ return blitBuffer(base64ToBytes(string), buf, offset, length)
5328
+ }
5329
+
5330
+ function ucs2Write (buf, string, offset, length) {
5331
+ return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length)
5332
+ }
5333
+
5334
+ Buffer.prototype.write = function write (string, offset, length, encoding) {
5335
+ // Buffer#write(string)
5336
+ if (offset === undefined) {
5337
+ encoding = 'utf8';
5338
+ length = this.length;
5339
+ offset = 0;
5340
+ // Buffer#write(string, encoding)
5341
+ } else if (length === undefined && typeof offset === 'string') {
5342
+ encoding = offset;
5343
+ length = this.length;
5344
+ offset = 0;
5345
+ // Buffer#write(string, offset[, length][, encoding])
5346
+ } else if (isFinite(offset)) {
5347
+ offset = offset >>> 0;
5348
+ if (isFinite(length)) {
5349
+ length = length >>> 0;
5350
+ if (encoding === undefined) encoding = 'utf8';
5351
+ } else {
5352
+ encoding = length;
5353
+ length = undefined;
5354
+ }
5355
+ } else {
5356
+ throw new Error(
5357
+ 'Buffer.write(string, encoding, offset[, length]) is no longer supported'
5358
+ )
5359
+ }
5360
+
5361
+ var remaining = this.length - offset;
5362
+ if (length === undefined || length > remaining) length = remaining;
5363
+
5364
+ if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) {
5365
+ throw new RangeError('Attempt to write outside buffer bounds')
5366
+ }
5367
+
5368
+ if (!encoding) encoding = 'utf8';
5369
+
5370
+ var loweredCase = false;
5371
+ for (;;) {
5372
+ switch (encoding) {
5373
+ case 'hex':
5374
+ return hexWrite(this, string, offset, length)
5375
+
5376
+ case 'utf8':
5377
+ case 'utf-8':
5378
+ return utf8Write(this, string, offset, length)
5379
+
5380
+ case 'ascii':
5381
+ case 'latin1':
5382
+ case 'binary':
5383
+ return asciiWrite(this, string, offset, length)
5384
+
5385
+ case 'base64':
5386
+ // Warning: maxLength not taken into account in base64Write
5387
+ return base64Write(this, string, offset, length)
5388
+
5389
+ case 'ucs2':
5390
+ case 'ucs-2':
5391
+ case 'utf16le':
5392
+ case 'utf-16le':
5393
+ return ucs2Write(this, string, offset, length)
5394
+
5395
+ default:
5396
+ if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
5397
+ encoding = ('' + encoding).toLowerCase();
5398
+ loweredCase = true;
5399
+ }
5400
+ }
5401
+ };
5402
+
5403
+ Buffer.prototype.toJSON = function toJSON () {
5404
+ return {
5405
+ type: 'Buffer',
5406
+ data: Array.prototype.slice.call(this._arr || this, 0)
5407
+ }
5408
+ };
5409
+
5410
+ function base64Slice (buf, start, end) {
5411
+ if (start === 0 && end === buf.length) {
5412
+ return base64.fromByteArray(buf)
5413
+ } else {
5414
+ return base64.fromByteArray(buf.slice(start, end))
5415
+ }
5416
+ }
5417
+
5418
+ function utf8Slice (buf, start, end) {
5419
+ end = Math.min(buf.length, end);
5420
+ var res = [];
5421
+
5422
+ var i = start;
5423
+ while (i < end) {
5424
+ var firstByte = buf[i];
5425
+ var codePoint = null;
5426
+ var bytesPerSequence = (firstByte > 0xEF)
5427
+ ? 4
5428
+ : (firstByte > 0xDF)
5429
+ ? 3
5430
+ : (firstByte > 0xBF)
5431
+ ? 2
5432
+ : 1;
5433
+
5434
+ if (i + bytesPerSequence <= end) {
5435
+ var secondByte, thirdByte, fourthByte, tempCodePoint;
5436
+
5437
+ switch (bytesPerSequence) {
5438
+ case 1:
5439
+ if (firstByte < 0x80) {
5440
+ codePoint = firstByte;
5441
+ }
5442
+ break
5443
+ case 2:
5444
+ secondByte = buf[i + 1];
5445
+ if ((secondByte & 0xC0) === 0x80) {
5446
+ tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F);
5447
+ if (tempCodePoint > 0x7F) {
5448
+ codePoint = tempCodePoint;
5449
+ }
5450
+ }
5451
+ break
5452
+ case 3:
5453
+ secondByte = buf[i + 1];
5454
+ thirdByte = buf[i + 2];
5455
+ if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) {
5456
+ tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F);
5457
+ if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) {
5458
+ codePoint = tempCodePoint;
5459
+ }
5460
+ }
5461
+ break
5462
+ case 4:
5463
+ secondByte = buf[i + 1];
5464
+ thirdByte = buf[i + 2];
5465
+ fourthByte = buf[i + 3];
5466
+ if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) {
5467
+ tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F);
5468
+ if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) {
5469
+ codePoint = tempCodePoint;
5470
+ }
5471
+ }
5472
+ }
5473
+ }
5474
+
5475
+ if (codePoint === null) {
5476
+ // we did not generate a valid codePoint so insert a
5477
+ // replacement char (U+FFFD) and advance only 1 byte
5478
+ codePoint = 0xFFFD;
5479
+ bytesPerSequence = 1;
5480
+ } else if (codePoint > 0xFFFF) {
5481
+ // encode to utf16 (surrogate pair dance)
5482
+ codePoint -= 0x10000;
5483
+ res.push(codePoint >>> 10 & 0x3FF | 0xD800);
5484
+ codePoint = 0xDC00 | codePoint & 0x3FF;
5485
+ }
5486
+
5487
+ res.push(codePoint);
5488
+ i += bytesPerSequence;
5489
+ }
5490
+
5491
+ return decodeCodePointsArray(res)
5492
+ }
5493
+
5494
+ // Based on http://stackoverflow.com/a/22747272/680742, the browser with
5495
+ // the lowest limit is Chrome, with 0x10000 args.
5496
+ // We go 1 magnitude less, for safety
5497
+ var MAX_ARGUMENTS_LENGTH = 0x1000;
5498
+
5499
+ function decodeCodePointsArray (codePoints) {
5500
+ var len = codePoints.length;
5501
+ if (len <= MAX_ARGUMENTS_LENGTH) {
5502
+ return String.fromCharCode.apply(String, codePoints) // avoid extra slice()
5503
+ }
5504
+
5505
+ // Decode in chunks to avoid "call stack size exceeded".
5506
+ var res = '';
5507
+ var i = 0;
5508
+ while (i < len) {
5509
+ res += String.fromCharCode.apply(
5510
+ String,
5511
+ codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH)
5512
+ );
5513
+ }
5514
+ return res
5515
+ }
5516
+
5517
+ function asciiSlice (buf, start, end) {
5518
+ var ret = '';
5519
+ end = Math.min(buf.length, end);
5520
+
5521
+ for (var i = start; i < end; ++i) {
5522
+ ret += String.fromCharCode(buf[i] & 0x7F);
5523
+ }
5524
+ return ret
5525
+ }
5526
+
5527
+ function latin1Slice (buf, start, end) {
5528
+ var ret = '';
5529
+ end = Math.min(buf.length, end);
5530
+
5531
+ for (var i = start; i < end; ++i) {
5532
+ ret += String.fromCharCode(buf[i]);
5533
+ }
5534
+ return ret
5535
+ }
5536
+
5537
+ function hexSlice (buf, start, end) {
5538
+ var len = buf.length;
5539
+
5540
+ if (!start || start < 0) start = 0;
5541
+ if (!end || end < 0 || end > len) end = len;
5542
+
5543
+ var out = '';
5544
+ for (var i = start; i < end; ++i) {
5545
+ out += hexSliceLookupTable[buf[i]];
5546
+ }
5547
+ return out
5548
+ }
5549
+
5550
+ function utf16leSlice (buf, start, end) {
5551
+ var bytes = buf.slice(start, end);
5552
+ var res = '';
5553
+ // If bytes.length is odd, the last 8 bits must be ignored (same as node.js)
5554
+ for (var i = 0; i < bytes.length - 1; i += 2) {
5555
+ res += String.fromCharCode(bytes[i] + (bytes[i + 1] * 256));
5556
+ }
5557
+ return res
5558
+ }
5559
+
5560
+ Buffer.prototype.slice = function slice (start, end) {
5561
+ var len = this.length;
5562
+ start = ~~start;
5563
+ end = end === undefined ? len : ~~end;
5564
+
5565
+ if (start < 0) {
5566
+ start += len;
5567
+ if (start < 0) start = 0;
5568
+ } else if (start > len) {
5569
+ start = len;
5570
+ }
5571
+
5572
+ if (end < 0) {
5573
+ end += len;
5574
+ if (end < 0) end = 0;
5575
+ } else if (end > len) {
5576
+ end = len;
5577
+ }
5578
+
5579
+ if (end < start) end = start;
5580
+
5581
+ var newBuf = this.subarray(start, end);
5582
+ // Return an augmented `Uint8Array` instance
5583
+ Object.setPrototypeOf(newBuf, Buffer.prototype);
5584
+
5585
+ return newBuf
5586
+ };
5587
+
5588
+ /*
5589
+ * Need to make sure that buffer isn't trying to write out of bounds.
5590
+ */
5591
+ function checkOffset (offset, ext, length) {
5592
+ if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint')
5593
+ if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length')
5594
+ }
5595
+
5596
+ Buffer.prototype.readUintLE =
5597
+ Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) {
5598
+ offset = offset >>> 0;
5599
+ byteLength = byteLength >>> 0;
5600
+ if (!noAssert) checkOffset(offset, byteLength, this.length);
5601
+
5602
+ var val = this[offset];
5603
+ var mul = 1;
5604
+ var i = 0;
5605
+ while (++i < byteLength && (mul *= 0x100)) {
5606
+ val += this[offset + i] * mul;
5607
+ }
5608
+
5609
+ return val
5610
+ };
5611
+
5612
+ Buffer.prototype.readUintBE =
5613
+ Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) {
5614
+ offset = offset >>> 0;
5615
+ byteLength = byteLength >>> 0;
5616
+ if (!noAssert) {
5617
+ checkOffset(offset, byteLength, this.length);
5618
+ }
5619
+
5620
+ var val = this[offset + --byteLength];
5621
+ var mul = 1;
5622
+ while (byteLength > 0 && (mul *= 0x100)) {
5623
+ val += this[offset + --byteLength] * mul;
5624
+ }
5625
+
5626
+ return val
5627
+ };
5628
+
5629
+ Buffer.prototype.readUint8 =
5630
+ Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) {
5631
+ offset = offset >>> 0;
5632
+ if (!noAssert) checkOffset(offset, 1, this.length);
5633
+ return this[offset]
5634
+ };
5635
+
5636
+ Buffer.prototype.readUint16LE =
5637
+ Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) {
5638
+ offset = offset >>> 0;
5639
+ if (!noAssert) checkOffset(offset, 2, this.length);
5640
+ return this[offset] | (this[offset + 1] << 8)
5641
+ };
5642
+
5643
+ Buffer.prototype.readUint16BE =
5644
+ Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) {
5645
+ offset = offset >>> 0;
5646
+ if (!noAssert) checkOffset(offset, 2, this.length);
5647
+ return (this[offset] << 8) | this[offset + 1]
5648
+ };
5649
+
5650
+ Buffer.prototype.readUint32LE =
5651
+ Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) {
5652
+ offset = offset >>> 0;
5653
+ if (!noAssert) checkOffset(offset, 4, this.length);
5654
+
5655
+ return ((this[offset]) |
5656
+ (this[offset + 1] << 8) |
5657
+ (this[offset + 2] << 16)) +
5658
+ (this[offset + 3] * 0x1000000)
5659
+ };
5660
+
5661
+ Buffer.prototype.readUint32BE =
5662
+ Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) {
5663
+ offset = offset >>> 0;
5664
+ if (!noAssert) checkOffset(offset, 4, this.length);
5665
+
5666
+ return (this[offset] * 0x1000000) +
5667
+ ((this[offset + 1] << 16) |
5668
+ (this[offset + 2] << 8) |
5669
+ this[offset + 3])
5670
+ };
5671
+
5672
+ Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) {
5673
+ offset = offset >>> 0;
5674
+ byteLength = byteLength >>> 0;
5675
+ if (!noAssert) checkOffset(offset, byteLength, this.length);
5676
+
5677
+ var val = this[offset];
5678
+ var mul = 1;
5679
+ var i = 0;
5680
+ while (++i < byteLength && (mul *= 0x100)) {
5681
+ val += this[offset + i] * mul;
5682
+ }
5683
+ mul *= 0x80;
5684
+
5685
+ if (val >= mul) val -= Math.pow(2, 8 * byteLength);
5686
+
5687
+ return val
5688
+ };
5689
+
5690
+ Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) {
5691
+ offset = offset >>> 0;
5692
+ byteLength = byteLength >>> 0;
5693
+ if (!noAssert) checkOffset(offset, byteLength, this.length);
5694
+
5695
+ var i = byteLength;
5696
+ var mul = 1;
5697
+ var val = this[offset + --i];
5698
+ while (i > 0 && (mul *= 0x100)) {
5699
+ val += this[offset + --i] * mul;
5700
+ }
5701
+ mul *= 0x80;
5702
+
5703
+ if (val >= mul) val -= Math.pow(2, 8 * byteLength);
5704
+
5705
+ return val
5706
+ };
5707
+
5708
+ Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) {
5709
+ offset = offset >>> 0;
5710
+ if (!noAssert) checkOffset(offset, 1, this.length);
5711
+ if (!(this[offset] & 0x80)) return (this[offset])
5712
+ return ((0xff - this[offset] + 1) * -1)
5713
+ };
5714
+
5715
+ Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) {
5716
+ offset = offset >>> 0;
5717
+ if (!noAssert) checkOffset(offset, 2, this.length);
5718
+ var val = this[offset] | (this[offset + 1] << 8);
5719
+ return (val & 0x8000) ? val | 0xFFFF0000 : val
5720
+ };
5721
+
5722
+ Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) {
5723
+ offset = offset >>> 0;
5724
+ if (!noAssert) checkOffset(offset, 2, this.length);
5725
+ var val = this[offset + 1] | (this[offset] << 8);
5726
+ return (val & 0x8000) ? val | 0xFFFF0000 : val
5727
+ };
5728
+
5729
+ Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) {
5730
+ offset = offset >>> 0;
5731
+ if (!noAssert) checkOffset(offset, 4, this.length);
5732
+
5733
+ return (this[offset]) |
5734
+ (this[offset + 1] << 8) |
5735
+ (this[offset + 2] << 16) |
5736
+ (this[offset + 3] << 24)
5737
+ };
5738
+
5739
+ Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) {
5740
+ offset = offset >>> 0;
5741
+ if (!noAssert) checkOffset(offset, 4, this.length);
5742
+
5743
+ return (this[offset] << 24) |
5744
+ (this[offset + 1] << 16) |
5745
+ (this[offset + 2] << 8) |
5746
+ (this[offset + 3])
5747
+ };
5748
+
5749
+ Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) {
5750
+ offset = offset >>> 0;
5751
+ if (!noAssert) checkOffset(offset, 4, this.length);
5752
+ return ieee754.read(this, offset, true, 23, 4)
5753
+ };
5754
+
5755
+ Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) {
5756
+ offset = offset >>> 0;
5757
+ if (!noAssert) checkOffset(offset, 4, this.length);
5758
+ return ieee754.read(this, offset, false, 23, 4)
5759
+ };
5760
+
5761
+ Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) {
5762
+ offset = offset >>> 0;
5763
+ if (!noAssert) checkOffset(offset, 8, this.length);
5764
+ return ieee754.read(this, offset, true, 52, 8)
5765
+ };
1806
5766
 
1807
- /**
1808
- * Calls to Abortcontroller.abort(reason: any) will result in the
1809
- * `reason` being thrown. This is not necessarily an error,
1810
- * but extends error for better logging purposes.
1811
- */
1812
- class AbortOperation extends Error {
1813
- reason;
1814
- constructor(reason) {
1815
- super(reason);
1816
- this.reason = reason;
1817
- // Set the prototype explicitly
1818
- Object.setPrototypeOf(this, AbortOperation.prototype);
1819
- // Capture stack trace
1820
- if (Error.captureStackTrace) {
1821
- Error.captureStackTrace(this, AbortOperation);
1822
- }
1823
- }
1824
- }
5767
+ Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) {
5768
+ offset = offset >>> 0;
5769
+ if (!noAssert) checkOffset(offset, 8, this.length);
5770
+ return ieee754.read(this, offset, false, 52, 8)
5771
+ };
1825
5772
 
1826
- exports.OpTypeEnum = void 0;
1827
- (function (OpTypeEnum) {
1828
- OpTypeEnum[OpTypeEnum["CLEAR"] = 1] = "CLEAR";
1829
- OpTypeEnum[OpTypeEnum["MOVE"] = 2] = "MOVE";
1830
- OpTypeEnum[OpTypeEnum["PUT"] = 3] = "PUT";
1831
- OpTypeEnum[OpTypeEnum["REMOVE"] = 4] = "REMOVE";
1832
- })(exports.OpTypeEnum || (exports.OpTypeEnum = {}));
1833
- /**
1834
- * Used internally for sync buckets.
1835
- */
1836
- class OpType {
1837
- value;
1838
- static fromJSON(jsonValue) {
1839
- return new OpType(exports.OpTypeEnum[jsonValue]);
1840
- }
1841
- constructor(value) {
1842
- this.value = value;
1843
- }
1844
- toJSON() {
1845
- return Object.entries(exports.OpTypeEnum).find(([, value]) => value === this.value)[0];
1846
- }
1847
- }
5773
+ function checkInt (buf, value, offset, ext, max, min) {
5774
+ if (!Buffer.isBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance')
5775
+ if (value > max || value < min) throw new RangeError('"value" argument is out of bounds')
5776
+ if (offset + ext > buf.length) throw new RangeError('Index out of range')
5777
+ }
1848
5778
 
1849
- class OplogEntry {
1850
- op_id;
1851
- op;
1852
- checksum;
1853
- subkey;
1854
- object_type;
1855
- object_id;
1856
- data;
1857
- static fromRow(row) {
1858
- return new OplogEntry(row.op_id, OpType.fromJSON(row.op), row.checksum, row.subkey, row.object_type, row.object_id, row.data);
1859
- }
1860
- constructor(op_id, op, checksum, subkey, object_type, object_id, data) {
1861
- this.op_id = op_id;
1862
- this.op = op;
1863
- this.checksum = checksum;
1864
- this.subkey = subkey;
1865
- this.object_type = object_type;
1866
- this.object_id = object_id;
1867
- this.data = data;
1868
- }
1869
- toJSON(fixedKeyEncoding = false) {
1870
- return {
1871
- op_id: this.op_id,
1872
- op: this.op.toJSON(),
1873
- object_type: this.object_type,
1874
- object_id: this.object_id,
1875
- checksum: this.checksum,
1876
- data: this.data,
1877
- // Older versions of the JS SDK used to always JSON.stringify here. That has always been wrong,
1878
- // but we need to migrate gradually to not break existing databases.
1879
- subkey: fixedKeyEncoding ? this.subkey : JSON.stringify(this.subkey)
1880
- };
1881
- }
1882
- }
5779
+ Buffer.prototype.writeUintLE =
5780
+ Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) {
5781
+ value = +value;
5782
+ offset = offset >>> 0;
5783
+ byteLength = byteLength >>> 0;
5784
+ if (!noAssert) {
5785
+ var maxBytes = Math.pow(2, 8 * byteLength) - 1;
5786
+ checkInt(this, value, offset, byteLength, maxBytes, 0);
5787
+ }
1883
5788
 
1884
- class SyncDataBucket {
1885
- bucket;
1886
- data;
1887
- has_more;
1888
- after;
1889
- next_after;
1890
- static fromRow(row) {
1891
- return new SyncDataBucket(row.bucket, row.data.map((entry) => OplogEntry.fromRow(entry)), row.has_more ?? false, row.after, row.next_after);
1892
- }
1893
- constructor(bucket, data,
1894
- /**
1895
- * True if the response does not contain all the data for this bucket, and another request must be made.
1896
- */
1897
- has_more,
1898
- /**
1899
- * The `after` specified in the request.
1900
- */
1901
- after,
1902
- /**
1903
- * Use this for the next request.
1904
- */
1905
- next_after) {
1906
- this.bucket = bucket;
1907
- this.data = data;
1908
- this.has_more = has_more;
1909
- this.after = after;
1910
- this.next_after = next_after;
1911
- }
1912
- toJSON(fixedKeyEncoding = false) {
1913
- return {
1914
- bucket: this.bucket,
1915
- has_more: this.has_more,
1916
- after: this.after,
1917
- next_after: this.next_after,
1918
- data: this.data.map((entry) => entry.toJSON(fixedKeyEncoding))
1919
- };
1920
- }
5789
+ var mul = 1;
5790
+ var i = 0;
5791
+ this[offset] = value & 0xFF;
5792
+ while (++i < byteLength && (mul *= 0x100)) {
5793
+ this[offset + i] = (value / mul) & 0xFF;
5794
+ }
5795
+
5796
+ return offset + byteLength
5797
+ };
5798
+
5799
+ Buffer.prototype.writeUintBE =
5800
+ Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) {
5801
+ value = +value;
5802
+ offset = offset >>> 0;
5803
+ byteLength = byteLength >>> 0;
5804
+ if (!noAssert) {
5805
+ var maxBytes = Math.pow(2, 8 * byteLength) - 1;
5806
+ checkInt(this, value, offset, byteLength, maxBytes, 0);
5807
+ }
5808
+
5809
+ var i = byteLength - 1;
5810
+ var mul = 1;
5811
+ this[offset + i] = value & 0xFF;
5812
+ while (--i >= 0 && (mul *= 0x100)) {
5813
+ this[offset + i] = (value / mul) & 0xFF;
5814
+ }
5815
+
5816
+ return offset + byteLength
5817
+ };
5818
+
5819
+ Buffer.prototype.writeUint8 =
5820
+ Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) {
5821
+ value = +value;
5822
+ offset = offset >>> 0;
5823
+ if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0);
5824
+ this[offset] = (value & 0xff);
5825
+ return offset + 1
5826
+ };
5827
+
5828
+ Buffer.prototype.writeUint16LE =
5829
+ Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) {
5830
+ value = +value;
5831
+ offset = offset >>> 0;
5832
+ if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0);
5833
+ this[offset] = (value & 0xff);
5834
+ this[offset + 1] = (value >>> 8);
5835
+ return offset + 2
5836
+ };
5837
+
5838
+ Buffer.prototype.writeUint16BE =
5839
+ Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) {
5840
+ value = +value;
5841
+ offset = offset >>> 0;
5842
+ if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0);
5843
+ this[offset] = (value >>> 8);
5844
+ this[offset + 1] = (value & 0xff);
5845
+ return offset + 2
5846
+ };
5847
+
5848
+ Buffer.prototype.writeUint32LE =
5849
+ Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) {
5850
+ value = +value;
5851
+ offset = offset >>> 0;
5852
+ if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0);
5853
+ this[offset + 3] = (value >>> 24);
5854
+ this[offset + 2] = (value >>> 16);
5855
+ this[offset + 1] = (value >>> 8);
5856
+ this[offset] = (value & 0xff);
5857
+ return offset + 4
5858
+ };
5859
+
5860
+ Buffer.prototype.writeUint32BE =
5861
+ Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) {
5862
+ value = +value;
5863
+ offset = offset >>> 0;
5864
+ if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0);
5865
+ this[offset] = (value >>> 24);
5866
+ this[offset + 1] = (value >>> 16);
5867
+ this[offset + 2] = (value >>> 8);
5868
+ this[offset + 3] = (value & 0xff);
5869
+ return offset + 4
5870
+ };
5871
+
5872
+ Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) {
5873
+ value = +value;
5874
+ offset = offset >>> 0;
5875
+ if (!noAssert) {
5876
+ var limit = Math.pow(2, (8 * byteLength) - 1);
5877
+
5878
+ checkInt(this, value, offset, byteLength, limit - 1, -limit);
5879
+ }
5880
+
5881
+ var i = 0;
5882
+ var mul = 1;
5883
+ var sub = 0;
5884
+ this[offset] = value & 0xFF;
5885
+ while (++i < byteLength && (mul *= 0x100)) {
5886
+ if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) {
5887
+ sub = 1;
5888
+ }
5889
+ this[offset + i] = ((value / mul) >> 0) - sub & 0xFF;
5890
+ }
5891
+
5892
+ return offset + byteLength
5893
+ };
5894
+
5895
+ Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) {
5896
+ value = +value;
5897
+ offset = offset >>> 0;
5898
+ if (!noAssert) {
5899
+ var limit = Math.pow(2, (8 * byteLength) - 1);
5900
+
5901
+ checkInt(this, value, offset, byteLength, limit - 1, -limit);
5902
+ }
5903
+
5904
+ var i = byteLength - 1;
5905
+ var mul = 1;
5906
+ var sub = 0;
5907
+ this[offset + i] = value & 0xFF;
5908
+ while (--i >= 0 && (mul *= 0x100)) {
5909
+ if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) {
5910
+ sub = 1;
5911
+ }
5912
+ this[offset + i] = ((value / mul) >> 0) - sub & 0xFF;
5913
+ }
5914
+
5915
+ return offset + byteLength
5916
+ };
5917
+
5918
+ Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) {
5919
+ value = +value;
5920
+ offset = offset >>> 0;
5921
+ if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -128);
5922
+ if (value < 0) value = 0xff + value + 1;
5923
+ this[offset] = (value & 0xff);
5924
+ return offset + 1
5925
+ };
5926
+
5927
+ Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) {
5928
+ value = +value;
5929
+ offset = offset >>> 0;
5930
+ if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -32768);
5931
+ this[offset] = (value & 0xff);
5932
+ this[offset + 1] = (value >>> 8);
5933
+ return offset + 2
5934
+ };
5935
+
5936
+ Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) {
5937
+ value = +value;
5938
+ offset = offset >>> 0;
5939
+ if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -32768);
5940
+ this[offset] = (value >>> 8);
5941
+ this[offset + 1] = (value & 0xff);
5942
+ return offset + 2
5943
+ };
5944
+
5945
+ Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) {
5946
+ value = +value;
5947
+ offset = offset >>> 0;
5948
+ if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -2147483648);
5949
+ this[offset] = (value & 0xff);
5950
+ this[offset + 1] = (value >>> 8);
5951
+ this[offset + 2] = (value >>> 16);
5952
+ this[offset + 3] = (value >>> 24);
5953
+ return offset + 4
5954
+ };
5955
+
5956
+ Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) {
5957
+ value = +value;
5958
+ offset = offset >>> 0;
5959
+ if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -2147483648);
5960
+ if (value < 0) value = 0xffffffff + value + 1;
5961
+ this[offset] = (value >>> 24);
5962
+ this[offset + 1] = (value >>> 16);
5963
+ this[offset + 2] = (value >>> 8);
5964
+ this[offset + 3] = (value & 0xff);
5965
+ return offset + 4
5966
+ };
5967
+
5968
+ function checkIEEE754 (buf, value, offset, ext, max, min) {
5969
+ if (offset + ext > buf.length) throw new RangeError('Index out of range')
5970
+ if (offset < 0) throw new RangeError('Index out of range')
5971
+ }
5972
+
5973
+ function writeFloat (buf, value, offset, littleEndian, noAssert) {
5974
+ value = +value;
5975
+ offset = offset >>> 0;
5976
+ if (!noAssert) {
5977
+ checkIEEE754(buf, value, offset, 4);
5978
+ }
5979
+ ieee754.write(buf, value, offset, littleEndian, 23, 4);
5980
+ return offset + 4
5981
+ }
5982
+
5983
+ Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) {
5984
+ return writeFloat(this, value, offset, true, noAssert)
5985
+ };
5986
+
5987
+ Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) {
5988
+ return writeFloat(this, value, offset, false, noAssert)
5989
+ };
5990
+
5991
+ function writeDouble (buf, value, offset, littleEndian, noAssert) {
5992
+ value = +value;
5993
+ offset = offset >>> 0;
5994
+ if (!noAssert) {
5995
+ checkIEEE754(buf, value, offset, 8);
5996
+ }
5997
+ ieee754.write(buf, value, offset, littleEndian, 52, 8);
5998
+ return offset + 8
5999
+ }
6000
+
6001
+ Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) {
6002
+ return writeDouble(this, value, offset, true, noAssert)
6003
+ };
6004
+
6005
+ Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) {
6006
+ return writeDouble(this, value, offset, false, noAssert)
6007
+ };
6008
+
6009
+ // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
6010
+ Buffer.prototype.copy = function copy (target, targetStart, start, end) {
6011
+ if (!Buffer.isBuffer(target)) throw new TypeError('argument should be a Buffer')
6012
+ if (!start) start = 0;
6013
+ if (!end && end !== 0) end = this.length;
6014
+ if (targetStart >= target.length) targetStart = target.length;
6015
+ if (!targetStart) targetStart = 0;
6016
+ if (end > 0 && end < start) end = start;
6017
+
6018
+ // Copy 0 bytes; we're done
6019
+ if (end === start) return 0
6020
+ if (target.length === 0 || this.length === 0) return 0
6021
+
6022
+ // Fatal error conditions
6023
+ if (targetStart < 0) {
6024
+ throw new RangeError('targetStart out of bounds')
6025
+ }
6026
+ if (start < 0 || start >= this.length) throw new RangeError('Index out of range')
6027
+ if (end < 0) throw new RangeError('sourceEnd out of bounds')
6028
+
6029
+ // Are we oob?
6030
+ if (end > this.length) end = this.length;
6031
+ if (target.length - targetStart < end - start) {
6032
+ end = target.length - targetStart + start;
6033
+ }
6034
+
6035
+ var len = end - start;
6036
+
6037
+ if (this === target && typeof Uint8Array.prototype.copyWithin === 'function') {
6038
+ // Use built-in when available, missing from IE11
6039
+ this.copyWithin(targetStart, start, end);
6040
+ } else {
6041
+ Uint8Array.prototype.set.call(
6042
+ target,
6043
+ this.subarray(start, end),
6044
+ targetStart
6045
+ );
6046
+ }
6047
+
6048
+ return len
6049
+ };
6050
+
6051
+ // Usage:
6052
+ // buffer.fill(number[, offset[, end]])
6053
+ // buffer.fill(buffer[, offset[, end]])
6054
+ // buffer.fill(string[, offset[, end]][, encoding])
6055
+ Buffer.prototype.fill = function fill (val, start, end, encoding) {
6056
+ // Handle string cases:
6057
+ if (typeof val === 'string') {
6058
+ if (typeof start === 'string') {
6059
+ encoding = start;
6060
+ start = 0;
6061
+ end = this.length;
6062
+ } else if (typeof end === 'string') {
6063
+ encoding = end;
6064
+ end = this.length;
6065
+ }
6066
+ if (encoding !== undefined && typeof encoding !== 'string') {
6067
+ throw new TypeError('encoding must be a string')
6068
+ }
6069
+ if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) {
6070
+ throw new TypeError('Unknown encoding: ' + encoding)
6071
+ }
6072
+ if (val.length === 1) {
6073
+ var code = val.charCodeAt(0);
6074
+ if ((encoding === 'utf8' && code < 128) ||
6075
+ encoding === 'latin1') {
6076
+ // Fast path: If `val` fits into a single byte, use that numeric value.
6077
+ val = code;
6078
+ }
6079
+ }
6080
+ } else if (typeof val === 'number') {
6081
+ val = val & 255;
6082
+ } else if (typeof val === 'boolean') {
6083
+ val = Number(val);
6084
+ }
6085
+
6086
+ // Invalid ranges are not set to a default, so can range check early.
6087
+ if (start < 0 || this.length < start || this.length < end) {
6088
+ throw new RangeError('Out of range index')
6089
+ }
6090
+
6091
+ if (end <= start) {
6092
+ return this
6093
+ }
6094
+
6095
+ start = start >>> 0;
6096
+ end = end === undefined ? this.length : end >>> 0;
6097
+
6098
+ if (!val) val = 0;
6099
+
6100
+ var i;
6101
+ if (typeof val === 'number') {
6102
+ for (i = start; i < end; ++i) {
6103
+ this[i] = val;
6104
+ }
6105
+ } else {
6106
+ var bytes = Buffer.isBuffer(val)
6107
+ ? val
6108
+ : Buffer.from(val, encoding);
6109
+ var len = bytes.length;
6110
+ if (len === 0) {
6111
+ throw new TypeError('The value "' + val +
6112
+ '" is invalid for argument "value"')
6113
+ }
6114
+ for (i = 0; i < end - start; ++i) {
6115
+ this[i + start] = bytes[i % len];
6116
+ }
6117
+ }
6118
+
6119
+ return this
6120
+ };
6121
+
6122
+ // HELPER FUNCTIONS
6123
+ // ================
6124
+
6125
+ var INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g;
6126
+
6127
+ function base64clean (str) {
6128
+ // Node takes equal signs as end of the Base64 encoding
6129
+ str = str.split('=')[0];
6130
+ // Node strips out invalid characters like \n and \t from the string, base64-js does not
6131
+ str = str.trim().replace(INVALID_BASE64_RE, '');
6132
+ // Node converts strings with length < 2 to ''
6133
+ if (str.length < 2) return ''
6134
+ // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not
6135
+ while (str.length % 4 !== 0) {
6136
+ str = str + '=';
6137
+ }
6138
+ return str
6139
+ }
6140
+
6141
+ function utf8ToBytes (string, units) {
6142
+ units = units || Infinity;
6143
+ var codePoint;
6144
+ var length = string.length;
6145
+ var leadSurrogate = null;
6146
+ var bytes = [];
6147
+
6148
+ for (var i = 0; i < length; ++i) {
6149
+ codePoint = string.charCodeAt(i);
6150
+
6151
+ // is surrogate component
6152
+ if (codePoint > 0xD7FF && codePoint < 0xE000) {
6153
+ // last char was a lead
6154
+ if (!leadSurrogate) {
6155
+ // no lead yet
6156
+ if (codePoint > 0xDBFF) {
6157
+ // unexpected trail
6158
+ if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
6159
+ continue
6160
+ } else if (i + 1 === length) {
6161
+ // unpaired lead
6162
+ if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
6163
+ continue
6164
+ }
6165
+
6166
+ // valid lead
6167
+ leadSurrogate = codePoint;
6168
+
6169
+ continue
6170
+ }
6171
+
6172
+ // 2 leads in a row
6173
+ if (codePoint < 0xDC00) {
6174
+ if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
6175
+ leadSurrogate = codePoint;
6176
+ continue
6177
+ }
6178
+
6179
+ // valid surrogate pair
6180
+ codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000;
6181
+ } else if (leadSurrogate) {
6182
+ // valid bmp char, but last char was a lead
6183
+ if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
6184
+ }
6185
+
6186
+ leadSurrogate = null;
6187
+
6188
+ // encode utf8
6189
+ if (codePoint < 0x80) {
6190
+ if ((units -= 1) < 0) break
6191
+ bytes.push(codePoint);
6192
+ } else if (codePoint < 0x800) {
6193
+ if ((units -= 2) < 0) break
6194
+ bytes.push(
6195
+ codePoint >> 0x6 | 0xC0,
6196
+ codePoint & 0x3F | 0x80
6197
+ );
6198
+ } else if (codePoint < 0x10000) {
6199
+ if ((units -= 3) < 0) break
6200
+ bytes.push(
6201
+ codePoint >> 0xC | 0xE0,
6202
+ codePoint >> 0x6 & 0x3F | 0x80,
6203
+ codePoint & 0x3F | 0x80
6204
+ );
6205
+ } else if (codePoint < 0x110000) {
6206
+ if ((units -= 4) < 0) break
6207
+ bytes.push(
6208
+ codePoint >> 0x12 | 0xF0,
6209
+ codePoint >> 0xC & 0x3F | 0x80,
6210
+ codePoint >> 0x6 & 0x3F | 0x80,
6211
+ codePoint & 0x3F | 0x80
6212
+ );
6213
+ } else {
6214
+ throw new Error('Invalid code point')
6215
+ }
6216
+ }
6217
+
6218
+ return bytes
6219
+ }
6220
+
6221
+ function asciiToBytes (str) {
6222
+ var byteArray = [];
6223
+ for (var i = 0; i < str.length; ++i) {
6224
+ // Node's code seems to be doing this and not & 0x7F..
6225
+ byteArray.push(str.charCodeAt(i) & 0xFF);
6226
+ }
6227
+ return byteArray
6228
+ }
6229
+
6230
+ function utf16leToBytes (str, units) {
6231
+ var c, hi, lo;
6232
+ var byteArray = [];
6233
+ for (var i = 0; i < str.length; ++i) {
6234
+ if ((units -= 2) < 0) break
6235
+
6236
+ c = str.charCodeAt(i);
6237
+ hi = c >> 8;
6238
+ lo = c % 256;
6239
+ byteArray.push(lo);
6240
+ byteArray.push(hi);
6241
+ }
6242
+
6243
+ return byteArray
6244
+ }
6245
+
6246
+ function base64ToBytes (str) {
6247
+ return base64.toByteArray(base64clean(str))
6248
+ }
6249
+
6250
+ function blitBuffer (src, dst, offset, length) {
6251
+ for (var i = 0; i < length; ++i) {
6252
+ if ((i + offset >= dst.length) || (i >= src.length)) break
6253
+ dst[i + offset] = src[i];
6254
+ }
6255
+ return i
6256
+ }
6257
+
6258
+ // ArrayBuffer or Uint8Array objects from other contexts (i.e. iframes) do not pass
6259
+ // the `instanceof` check but they should be treated as of that type.
6260
+ // See: https://github.com/feross/buffer/issues/166
6261
+ function isInstance (obj, type) {
6262
+ return obj instanceof type ||
6263
+ (obj != null && obj.constructor != null && obj.constructor.name != null &&
6264
+ obj.constructor.name === type.name)
6265
+ }
6266
+ function numberIsNaN (obj) {
6267
+ // For IE11 support
6268
+ return obj !== obj // eslint-disable-line no-self-compare
6269
+ }
6270
+
6271
+ // Create lookup table for `toString('hex')`
6272
+ // See: https://github.com/feross/buffer/issues/219
6273
+ var hexSliceLookupTable = (function () {
6274
+ var alphabet = '0123456789abcdef';
6275
+ var table = new Array(256);
6276
+ for (var i = 0; i < 16; ++i) {
6277
+ var i16 = i * 16;
6278
+ for (var j = 0; j < 16; ++j) {
6279
+ table[i16 + j] = alphabet[i] + alphabet[j];
6280
+ }
6281
+ }
6282
+ return table
6283
+ })();
6284
+ } (buffer));
6285
+ return buffer;
1921
6286
  }
1922
6287
 
1923
- var dist = {};
6288
+ var bufferExports = requireBuffer();
1924
6289
 
1925
6290
  var Codecs = {};
1926
6291
 
@@ -2185,7 +6550,7 @@ function requireCodecs () {
2185
6550
  */
2186
6551
  function serializeFrameWithLength(frame) {
2187
6552
  var buffer = serializeFrame(frame);
2188
- var lengthPrefixed = _.Buffer.allocUnsafe(buffer.length + UINT24_SIZE);
6553
+ var lengthPrefixed = bufferExports.Buffer.allocUnsafe(buffer.length + UINT24_SIZE);
2189
6554
  writeUInt24BE(lengthPrefixed, buffer.length, 0);
2190
6555
  buffer.copy(lengthPrefixed, UINT24_SIZE);
2191
6556
  return lengthPrefixed;
@@ -2336,13 +6701,13 @@ function requireCodecs () {
2336
6701
  function serializeSetupFrame(frame) {
2337
6702
  var resumeTokenLength = frame.resumeToken != null ? frame.resumeToken.byteLength : 0;
2338
6703
  var metadataMimeTypeLength = frame.metadataMimeType != null
2339
- ? _.Buffer.byteLength(frame.metadataMimeType, "ascii")
6704
+ ? bufferExports.Buffer.byteLength(frame.metadataMimeType, "ascii")
2340
6705
  : 0;
2341
6706
  var dataMimeTypeLength = frame.dataMimeType != null
2342
- ? _.Buffer.byteLength(frame.dataMimeType, "ascii")
6707
+ ? bufferExports.Buffer.byteLength(frame.dataMimeType, "ascii")
2343
6708
  : 0;
2344
6709
  var payloadLength = getPayloadLength(frame);
2345
- var buffer = _.Buffer.allocUnsafe(FRAME_HEADER_SIZE +
6710
+ var buffer = bufferExports.Buffer.allocUnsafe(FRAME_HEADER_SIZE +
2346
6711
  SETUP_FIXED_SIZE + //
2347
6712
  (resumeTokenLength ? RESUME_TOKEN_LENGTH_SIZE + resumeTokenLength : 0) +
2348
6713
  metadataMimeTypeLength +
@@ -2373,10 +6738,10 @@ function requireCodecs () {
2373
6738
  function sizeOfSetupFrame(frame) {
2374
6739
  var resumeTokenLength = frame.resumeToken != null ? frame.resumeToken.byteLength : 0;
2375
6740
  var metadataMimeTypeLength = frame.metadataMimeType != null
2376
- ? _.Buffer.byteLength(frame.metadataMimeType, "ascii")
6741
+ ? bufferExports.Buffer.byteLength(frame.metadataMimeType, "ascii")
2377
6742
  : 0;
2378
6743
  var dataMimeTypeLength = frame.dataMimeType != null
2379
- ? _.Buffer.byteLength(frame.dataMimeType, "ascii")
6744
+ ? bufferExports.Buffer.byteLength(frame.dataMimeType, "ascii")
2380
6745
  : 0;
2381
6746
  var payloadLength = getPayloadLength(frame);
2382
6747
  return (FRAME_HEADER_SIZE +
@@ -2465,8 +6830,8 @@ function requireCodecs () {
2465
6830
  */
2466
6831
  var ERROR_FIXED_SIZE = 4;
2467
6832
  function serializeErrorFrame(frame) {
2468
- var messageLength = frame.message != null ? _.Buffer.byteLength(frame.message, "utf8") : 0;
2469
- var buffer = _.Buffer.allocUnsafe(FRAME_HEADER_SIZE + ERROR_FIXED_SIZE + messageLength);
6833
+ var messageLength = frame.message != null ? bufferExports.Buffer.byteLength(frame.message, "utf8") : 0;
6834
+ var buffer = bufferExports.Buffer.allocUnsafe(FRAME_HEADER_SIZE + ERROR_FIXED_SIZE + messageLength);
2470
6835
  var offset = writeHeader(frame, buffer);
2471
6836
  offset = buffer.writeUInt32BE(frame.code, offset);
2472
6837
  if (frame.message != null) {
@@ -2475,7 +6840,7 @@ function requireCodecs () {
2475
6840
  return buffer;
2476
6841
  }
2477
6842
  function sizeOfErrorFrame(frame) {
2478
- var messageLength = frame.message != null ? _.Buffer.byteLength(frame.message, "utf8") : 0;
6843
+ var messageLength = frame.message != null ? bufferExports.Buffer.byteLength(frame.message, "utf8") : 0;
2479
6844
  return FRAME_HEADER_SIZE + ERROR_FIXED_SIZE + messageLength;
2480
6845
  }
2481
6846
  /**
@@ -2514,7 +6879,7 @@ function requireCodecs () {
2514
6879
  var KEEPALIVE_FIXED_SIZE = 8;
2515
6880
  function serializeKeepAliveFrame(frame) {
2516
6881
  var dataLength = frame.data != null ? frame.data.byteLength : 0;
2517
- var buffer = _.Buffer.allocUnsafe(FRAME_HEADER_SIZE + KEEPALIVE_FIXED_SIZE + dataLength);
6882
+ var buffer = bufferExports.Buffer.allocUnsafe(FRAME_HEADER_SIZE + KEEPALIVE_FIXED_SIZE + dataLength);
2518
6883
  var offset = writeHeader(frame, buffer);
2519
6884
  offset = writeUInt64BE(buffer, frame.lastReceivedPosition, offset);
2520
6885
  if (frame.data != null) {
@@ -2559,7 +6924,7 @@ function requireCodecs () {
2559
6924
  var LEASE_FIXED_SIZE = 8;
2560
6925
  function serializeLeaseFrame(frame) {
2561
6926
  var metaLength = frame.metadata != null ? frame.metadata.byteLength : 0;
2562
- var buffer = _.Buffer.allocUnsafe(FRAME_HEADER_SIZE + LEASE_FIXED_SIZE + metaLength);
6927
+ var buffer = bufferExports.Buffer.allocUnsafe(FRAME_HEADER_SIZE + LEASE_FIXED_SIZE + metaLength);
2563
6928
  var offset = writeHeader(frame, buffer);
2564
6929
  offset = buffer.writeUInt32BE(frame.ttl, offset);
2565
6930
  offset = buffer.writeUInt32BE(frame.requestCount, offset);
@@ -2608,7 +6973,7 @@ function requireCodecs () {
2608
6973
  */
2609
6974
  function serializeRequestFrame(frame) {
2610
6975
  var payloadLength = getPayloadLength(frame);
2611
- var buffer = _.Buffer.allocUnsafe(FRAME_HEADER_SIZE + payloadLength);
6976
+ var buffer = bufferExports.Buffer.allocUnsafe(FRAME_HEADER_SIZE + payloadLength);
2612
6977
  var offset = writeHeader(frame, buffer);
2613
6978
  writePayload(frame, buffer, offset);
2614
6979
  return buffer;
@@ -2624,13 +6989,13 @@ function requireCodecs () {
2624
6989
  function serializeMetadataPushFrame(frame) {
2625
6990
  var metadata = frame.metadata;
2626
6991
  if (metadata != null) {
2627
- var buffer = _.Buffer.allocUnsafe(FRAME_HEADER_SIZE + metadata.byteLength);
6992
+ var buffer = bufferExports.Buffer.allocUnsafe(FRAME_HEADER_SIZE + metadata.byteLength);
2628
6993
  var offset = writeHeader(frame, buffer);
2629
6994
  metadata.copy(buffer, offset);
2630
6995
  return buffer;
2631
6996
  }
2632
6997
  else {
2633
- var buffer = _.Buffer.allocUnsafe(FRAME_HEADER_SIZE);
6998
+ var buffer = bufferExports.Buffer.allocUnsafe(FRAME_HEADER_SIZE);
2634
6999
  writeHeader(frame, buffer);
2635
7000
  return buffer;
2636
7001
  }
@@ -2700,7 +7065,7 @@ function requireCodecs () {
2700
7065
  var REQUEST_MANY_HEADER = 4;
2701
7066
  function serializeRequestManyFrame(frame) {
2702
7067
  var payloadLength = getPayloadLength(frame);
2703
- var buffer = _.Buffer.allocUnsafe(FRAME_HEADER_SIZE + REQUEST_MANY_HEADER + payloadLength);
7068
+ var buffer = bufferExports.Buffer.allocUnsafe(FRAME_HEADER_SIZE + REQUEST_MANY_HEADER + payloadLength);
2704
7069
  var offset = writeHeader(frame, buffer);
2705
7070
  offset = buffer.writeUInt32BE(frame.requestN, offset);
2706
7071
  writePayload(frame, buffer, offset);
@@ -2769,7 +7134,7 @@ function requireCodecs () {
2769
7134
  */
2770
7135
  var REQUEST_N_HEADER = 4;
2771
7136
  function serializeRequestNFrame(frame) {
2772
- var buffer = _.Buffer.allocUnsafe(FRAME_HEADER_SIZE + REQUEST_N_HEADER);
7137
+ var buffer = bufferExports.Buffer.allocUnsafe(FRAME_HEADER_SIZE + REQUEST_N_HEADER);
2773
7138
  var offset = writeHeader(frame, buffer);
2774
7139
  buffer.writeUInt32BE(frame.requestN, offset);
2775
7140
  return buffer;
@@ -2801,7 +7166,7 @@ function requireCodecs () {
2801
7166
  * Writes a CANCEL frame to a new buffer and returns it.
2802
7167
  */
2803
7168
  function serializeCancelFrame(frame) {
2804
- var buffer = _.Buffer.allocUnsafe(FRAME_HEADER_SIZE);
7169
+ var buffer = bufferExports.Buffer.allocUnsafe(FRAME_HEADER_SIZE);
2805
7170
  writeHeader(frame, buffer);
2806
7171
  return buffer;
2807
7172
  }
@@ -2826,7 +7191,7 @@ function requireCodecs () {
2826
7191
  */
2827
7192
  function serializePayloadFrame(frame) {
2828
7193
  var payloadLength = getPayloadLength(frame);
2829
- var buffer = _.Buffer.allocUnsafe(FRAME_HEADER_SIZE + payloadLength);
7194
+ var buffer = bufferExports.Buffer.allocUnsafe(FRAME_HEADER_SIZE + payloadLength);
2830
7195
  var offset = writeHeader(frame, buffer);
2831
7196
  writePayload(frame, buffer, offset);
2832
7197
  return buffer;
@@ -2865,7 +7230,7 @@ function requireCodecs () {
2865
7230
  var RESUME_FIXED_SIZE = 22;
2866
7231
  function serializeResumeFrame(frame) {
2867
7232
  var resumeTokenLength = frame.resumeToken.byteLength;
2868
- var buffer = _.Buffer.allocUnsafe(FRAME_HEADER_SIZE + RESUME_FIXED_SIZE + resumeTokenLength);
7233
+ var buffer = bufferExports.Buffer.allocUnsafe(FRAME_HEADER_SIZE + RESUME_FIXED_SIZE + resumeTokenLength);
2869
7234
  var offset = writeHeader(frame, buffer);
2870
7235
  offset = buffer.writeUInt16BE(frame.majorVersion, offset);
2871
7236
  offset = buffer.writeUInt16BE(frame.minorVersion, offset);
@@ -2926,7 +7291,7 @@ function requireCodecs () {
2926
7291
  */
2927
7292
  var RESUME_OK_FIXED_SIZE = 8;
2928
7293
  function serializeResumeOkFrame(frame) {
2929
- var buffer = _.Buffer.allocUnsafe(FRAME_HEADER_SIZE + RESUME_OK_FIXED_SIZE);
7294
+ var buffer = bufferExports.Buffer.allocUnsafe(FRAME_HEADER_SIZE + RESUME_OK_FIXED_SIZE);
2930
7295
  var offset = writeHeader(frame, buffer);
2931
7296
  writeUInt64BE(buffer, frame.clientPosition, offset);
2932
7297
  return buffer;
@@ -3764,7 +8129,7 @@ function requireFragmenter () {
3764
8129
  metadataLength = payload.metadata.byteLength;
3765
8130
  if (!(metadataLength === 0)) return [3 /*break*/, 1];
3766
8131
  remaining -= Frames_1.Lengths.METADATA;
3767
- metadata = _.Buffer.allocUnsafe(0);
8132
+ metadata = bufferExports.Buffer.allocUnsafe(0);
3768
8133
  return [3 /*break*/, 6];
3769
8134
  case 1:
3770
8135
  metadataPosition = 0;
@@ -3877,7 +8242,7 @@ function requireFragmenter () {
3877
8242
  metadataLength = payload.metadata.byteLength;
3878
8243
  if (!(metadataLength === 0)) return [3 /*break*/, 1];
3879
8244
  remaining -= Frames_1.Lengths.METADATA;
3880
- metadata = _.Buffer.allocUnsafe(0);
8245
+ metadata = bufferExports.Buffer.allocUnsafe(0);
3881
8246
  return [3 /*break*/, 6];
3882
8247
  case 1:
3883
8248
  metadataPosition = 0;
@@ -4018,10 +8383,10 @@ function requireReassembler () {
4018
8383
  }
4019
8384
  // TODO: add validation
4020
8385
  holder.data = holder.data
4021
- ? _.Buffer.concat([holder.data, dataFragment])
8386
+ ? bufferExports.Buffer.concat([holder.data, dataFragment])
4022
8387
  : dataFragment;
4023
8388
  if (holder.metadata && metadataFragment) {
4024
- holder.metadata = _.Buffer.concat([holder.metadata, metadataFragment]);
8389
+ holder.metadata = bufferExports.Buffer.concat([holder.metadata, metadataFragment]);
4025
8390
  }
4026
8391
  return true;
4027
8392
  }
@@ -4030,12 +8395,12 @@ function requireReassembler () {
4030
8395
  // TODO: add validation
4031
8396
  holder.hasFragments = false;
4032
8397
  var data = holder.data
4033
- ? _.Buffer.concat([holder.data, dataFragment])
8398
+ ? bufferExports.Buffer.concat([holder.data, dataFragment])
4034
8399
  : dataFragment;
4035
8400
  holder.data = undefined;
4036
8401
  if (holder.metadata) {
4037
8402
  var metadata = metadataFragment
4038
- ? _.Buffer.concat([holder.metadata, metadataFragment])
8403
+ ? bufferExports.Buffer.concat([holder.metadata, metadataFragment])
4039
8404
  : holder.metadata;
4040
8405
  holder.metadata = undefined;
4041
8406
  return {
@@ -6704,7 +11069,7 @@ function requireDist () {
6704
11069
 
6705
11070
  var distExports = requireDist();
6706
11071
 
6707
- var version = "1.41.0";
11072
+ var version = "1.43.1";
6708
11073
  var PACKAGE = {
6709
11074
  version: version};
6710
11075
 
@@ -6920,7 +11285,7 @@ function requireWebsocketDuplexConnection () {
6920
11285
  };
6921
11286
  _this.handleMessage = function (message) {
6922
11287
  try {
6923
- var buffer = _.Buffer.from(message.data);
11288
+ var buffer = bufferExports.Buffer.from(message.data);
6924
11289
  var frame = _this.deserializer.deserializeFrame(buffer);
6925
11290
  _this.multiplexerDemultiplexer.handle(frame);
6926
11291
  }
@@ -7226,7 +11591,7 @@ class AbstractRemote {
7226
11591
  else {
7227
11592
  contents = JSON.stringify(js);
7228
11593
  }
7229
- return _.Buffer.from(contents);
11594
+ return bufferExports$1.Buffer.from(contents);
7230
11595
  }
7231
11596
  const syncQueueRequestSize = fetchStrategy == exports.FetchStrategy.Buffered ? 10 : 1;
7232
11597
  const request = await this.buildRequest(path);
@@ -7452,9 +11817,11 @@ class AbstractRemote {
7452
11817
  // Create a new stream splitting the response at line endings while also handling cancellations
7453
11818
  // by closing the reader.
7454
11819
  const reader = res.body.getReader();
11820
+ let readerReleased = false;
7455
11821
  // This will close the network request and read stream
7456
11822
  const closeReader = async () => {
7457
11823
  try {
11824
+ readerReleased = true;
7458
11825
  await reader.cancel();
7459
11826
  }
7460
11827
  catch (ex) {
@@ -7462,17 +11829,21 @@ class AbstractRemote {
7462
11829
  }
7463
11830
  reader.releaseLock();
7464
11831
  };
11832
+ const stream = new DataStream({
11833
+ logger: this.logger,
11834
+ mapLine: mapLine
11835
+ });
7465
11836
  abortSignal?.addEventListener('abort', () => {
7466
11837
  closeReader();
11838
+ stream.close();
7467
11839
  });
7468
11840
  const decoder = this.createTextDecoder();
7469
11841
  let buffer = '';
7470
- const stream = new DataStream({
7471
- logger: this.logger,
7472
- mapLine: mapLine
7473
- });
7474
11842
  const l = stream.registerListener({
7475
11843
  lowWater: async () => {
11844
+ if (stream.closed || abortSignal?.aborted || readerReleased) {
11845
+ return;
11846
+ }
7476
11847
  try {
7477
11848
  let didCompleteLine = false;
7478
11849
  while (!didCompleteLine) {
@@ -7796,7 +12167,7 @@ The next upload iteration will be delayed.`);
7796
12167
  uploadError: ex
7797
12168
  }
7798
12169
  });
7799
- await this.delayRetry(controller.signal);
12170
+ await this.delayRetry(controller.signal, this.options.crudUploadThrottleMs);
7800
12171
  if (!this.isConnected) {
7801
12172
  // Exit the upload loop if the sync stream is no longer connected
7802
12173
  break;
@@ -8507,14 +12878,14 @@ The next upload iteration will be delayed.`);
8507
12878
  // trigger this for all updates
8508
12879
  this.iterateListeners((cb) => cb.statusUpdated?.(options));
8509
12880
  }
8510
- async delayRetry(signal) {
12881
+ async delayRetry(signal, delayMs) {
8511
12882
  return new Promise((resolve) => {
8512
12883
  if (signal?.aborted) {
8513
12884
  // If the signal is already aborted, resolve immediately
8514
12885
  resolve();
8515
12886
  return;
8516
12887
  }
8517
- const { retryDelayMs } = this.options;
12888
+ const delay = delayMs ?? this.options.retryDelayMs;
8518
12889
  let timeoutId;
8519
12890
  const endDelay = () => {
8520
12891
  resolve();
@@ -8525,7 +12896,7 @@ The next upload iteration will be delayed.`);
8525
12896
  signal?.removeEventListener('abort', endDelay);
8526
12897
  };
8527
12898
  signal?.addEventListener('abort', endDelay, { once: true });
8528
- timeoutId = setTimeout(endDelay, retryDelayMs);
12899
+ timeoutId = setTimeout(endDelay, delay);
8529
12900
  });
8530
12901
  }
8531
12902
  updateSubscriptions(subscriptions) {
@@ -8632,6 +13003,7 @@ class TriggerManagerImpl {
8632
13003
  await hooks?.beforeCreate?.(tx);
8633
13004
  await tx.execute(/* sql */ `
8634
13005
  CREATE TEMP TABLE ${destination} (
13006
+ operation_id INTEGER PRIMARY KEY AUTOINCREMENT,
8635
13007
  id TEXT,
8636
13008
  operation TEXT,
8637
13009
  timestamp TEXT,
@@ -8742,17 +13114,20 @@ class TriggerManagerImpl {
8742
13114
  const callbackResult = await options.onChange({
8743
13115
  ...tx,
8744
13116
  destinationTable: destination,
8745
- withDiff: async (query, params) => {
13117
+ withDiff: async (query, params, options) => {
8746
13118
  // Wrap the query to expose the destination table
13119
+ const operationIdSelect = options?.castOperationIdAsText
13120
+ ? 'id, operation, CAST(operation_id AS TEXT) as operation_id, timestamp, value, previous_value'
13121
+ : '*';
8747
13122
  const wrappedQuery = /* sql */ `
8748
13123
  WITH
8749
13124
  DIFF AS (
8750
13125
  SELECT
8751
- *
13126
+ ${operationIdSelect}
8752
13127
  FROM
8753
13128
  ${destination}
8754
13129
  ORDER BY
8755
- timestamp ASC
13130
+ operation_id ASC
8756
13131
  ) ${query}
8757
13132
  `;
8758
13133
  return tx.getAll(wrappedQuery, params);
@@ -8766,13 +13141,14 @@ class TriggerManagerImpl {
8766
13141
  id,
8767
13142
  ${contextColumns.length > 0
8768
13143
  ? `${contextColumns.map((col) => `json_extract(value, '$.${col}') as ${col}`).join(', ')},`
8769
- : ''} operation as __operation,
13144
+ : ''} operation_id as __operation_id,
13145
+ operation as __operation,
8770
13146
  timestamp as __timestamp,
8771
13147
  previous_value as __previous_value
8772
13148
  FROM
8773
13149
  ${destination}
8774
13150
  ORDER BY
8775
- __timestamp ASC
13151
+ __operation_id ASC
8776
13152
  ) ${query}
8777
13153
  `;
8778
13154
  return tx.getAll(wrappedQuery, params);
@@ -9606,7 +13982,7 @@ SELECT * FROM crud_entries;
9606
13982
  * @returns An AsyncIterable that yields QueryResults whenever the data changes
9607
13983
  */
9608
13984
  watchWithAsyncGenerator(sql, parameters, options) {
9609
- return new eventIterator.EventIterator((eventOptions) => {
13985
+ return new domExports.EventIterator((eventOptions) => {
9610
13986
  const handler = {
9611
13987
  onResult: (result) => {
9612
13988
  eventOptions.push(result);
@@ -9716,7 +14092,7 @@ SELECT * FROM crud_entries;
9716
14092
  */
9717
14093
  onChangeWithAsyncGenerator(options) {
9718
14094
  const resolvedOptions = options ?? {};
9719
- return new eventIterator.EventIterator((eventOptions) => {
14095
+ return new domExports.EventIterator((eventOptions) => {
9720
14096
  const dispose = this.onChangeWithCallback({
9721
14097
  onChange: (event) => {
9722
14098
  eventOptions.push(event);
@@ -10046,7 +14422,7 @@ class SqliteBucketStorage extends BaseObserver {
10046
14422
  // Nothing to update
10047
14423
  return false;
10048
14424
  }
10049
- const rs = await this.db.getAll("SELECT seq FROM sqlite_sequence WHERE name = 'ps_crud'");
14425
+ const rs = await this.db.getAll("SELECT seq FROM main.sqlite_sequence WHERE name = 'ps_crud'");
10050
14426
  if (!rs.length) {
10051
14427
  // Nothing to update
10052
14428
  return false;
@@ -10060,7 +14436,7 @@ class SqliteBucketStorage extends BaseObserver {
10060
14436
  this.logger.debug(`New data uploaded since write checkpoint ${opId} - need new write checkpoint`);
10061
14437
  return false;
10062
14438
  }
10063
- const rs = await tx.execute("SELECT seq FROM sqlite_sequence WHERE name = 'ps_crud'");
14439
+ const rs = await tx.execute("SELECT seq FROM main.sqlite_sequence WHERE name = 'ps_crud'");
10064
14440
  if (!rs.rows?.length) {
10065
14441
  // assert isNotEmpty
10066
14442
  throw new Error('SQLite Sequence should not be empty');