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