@waku/message-encryption 0.0.24 → 0.0.25

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.
@@ -1,2144 +1,1190 @@
1
- import { h as commonjsGlobal, i as getDefaultExportFromCjs, j as concat, u as utf8ToBytes$1, l as encrypt, m as hexToBytes, n as decrypt, o as generateIv, p as encrypt$1, S as Symmetric, q as decrypt$1, r as randomBytes, k as keccak256, d as sign, t as Signature, v as recoverPublicKey } from './symmetric-CXVjdTdV.js';
2
-
3
- var minimal = {};
4
-
5
- var aspromise;
6
- var hasRequiredAspromise;
7
-
8
- function requireAspromise () {
9
- if (hasRequiredAspromise) return aspromise;
10
- hasRequiredAspromise = 1;
11
- aspromise = asPromise;
12
-
13
- /**
14
- * Callback as used by {@link util.asPromise}.
15
- * @typedef asPromiseCallback
16
- * @type {function}
17
- * @param {Error|null} error Error, if any
18
- * @param {...*} params Additional arguments
19
- * @returns {undefined}
20
- */
21
-
22
- /**
23
- * Returns a promise from a node-style callback function.
24
- * @memberof util
25
- * @param {asPromiseCallback} fn Function to call
26
- * @param {*} ctx Function context
27
- * @param {...*} params Function arguments
28
- * @returns {Promise<*>} Promisified function
29
- */
30
- function asPromise(fn, ctx/*, varargs */) {
31
- var params = new Array(arguments.length - 1),
32
- offset = 0,
33
- index = 2,
34
- pending = true;
35
- while (index < arguments.length)
36
- params[offset++] = arguments[index++];
37
- return new Promise(function executor(resolve, reject) {
38
- params[offset] = function callback(err/*, varargs */) {
39
- if (pending) {
40
- pending = false;
41
- if (err)
42
- reject(err);
43
- else {
44
- var params = new Array(arguments.length - 1),
45
- offset = 0;
46
- while (offset < params.length)
47
- params[offset++] = arguments[offset];
48
- resolve.apply(null, params);
49
- }
50
- }
51
- };
52
- try {
53
- fn.apply(ctx || null, params);
54
- } catch (err) {
55
- if (pending) {
56
- pending = false;
57
- reject(err);
58
- }
59
- }
60
- });
61
- }
62
- return aspromise;
1
+ import { h as allocUnsafe, i as fromString, j as concat, u as utf8ToBytes$1, l as getDefaultExportFromCjs, m as encrypt, n as hexToBytes, o as decrypt, p as generateIv, q as encrypt$1, S as Symmetric, t as decrypt$1, r as randomBytes, k as keccak256, d as sign, v as Signature, w as recoverPublicKey } from './symmetric-Cd3XbaRu.js';
2
+
3
+ /* eslint-disable no-fallthrough */
4
+ const N1 = Math.pow(2, 7);
5
+ const N2 = Math.pow(2, 14);
6
+ const N3 = Math.pow(2, 21);
7
+ const N4 = Math.pow(2, 28);
8
+ const N5 = Math.pow(2, 35);
9
+ const N6 = Math.pow(2, 42);
10
+ const N7 = Math.pow(2, 49);
11
+ /** Most significant bit of a byte */
12
+ const MSB = 0x80;
13
+ /** Rest of the bits in a byte */
14
+ const REST = 0x7f;
15
+ function encodingLength(value) {
16
+ if (value < N1) {
17
+ return 1;
18
+ }
19
+ if (value < N2) {
20
+ return 2;
21
+ }
22
+ if (value < N3) {
23
+ return 3;
24
+ }
25
+ if (value < N4) {
26
+ return 4;
27
+ }
28
+ if (value < N5) {
29
+ return 5;
30
+ }
31
+ if (value < N6) {
32
+ return 6;
33
+ }
34
+ if (value < N7) {
35
+ return 7;
36
+ }
37
+ if (Number.MAX_SAFE_INTEGER != null && value > Number.MAX_SAFE_INTEGER) {
38
+ throw new RangeError('Could not encode varint');
39
+ }
40
+ return 8;
63
41
  }
64
-
65
- var base64$1 = {};
66
-
67
- var hasRequiredBase64;
68
-
69
- function requireBase64 () {
70
- if (hasRequiredBase64) return base64$1;
71
- hasRequiredBase64 = 1;
72
- (function (exports) {
73
-
74
- /**
75
- * A minimal base64 implementation for number arrays.
76
- * @memberof util
77
- * @namespace
78
- */
79
- var base64 = exports;
80
-
81
- /**
82
- * Calculates the byte length of a base64 encoded string.
83
- * @param {string} string Base64 encoded string
84
- * @returns {number} Byte length
85
- */
86
- base64.length = function length(string) {
87
- var p = string.length;
88
- if (!p)
89
- return 0;
90
- var n = 0;
91
- while (--p % 4 > 1 && string.charAt(p) === "=")
92
- ++n;
93
- return Math.ceil(string.length * 3) / 4 - n;
94
- };
95
-
96
- // Base64 encoding table
97
- var b64 = new Array(64);
98
-
99
- // Base64 decoding table
100
- var s64 = new Array(123);
101
-
102
- // 65..90, 97..122, 48..57, 43, 47
103
- for (var i = 0; i < 64;)
104
- s64[b64[i] = i < 26 ? i + 65 : i < 52 ? i + 71 : i < 62 ? i - 4 : i - 59 | 43] = i++;
105
-
106
- /**
107
- * Encodes a buffer to a base64 encoded string.
108
- * @param {Uint8Array} buffer Source buffer
109
- * @param {number} start Source start
110
- * @param {number} end Source end
111
- * @returns {string} Base64 encoded string
112
- */
113
- base64.encode = function encode(buffer, start, end) {
114
- var parts = null,
115
- chunk = [];
116
- var i = 0, // output index
117
- j = 0, // goto index
118
- t; // temporary
119
- while (start < end) {
120
- var b = buffer[start++];
121
- switch (j) {
122
- case 0:
123
- chunk[i++] = b64[b >> 2];
124
- t = (b & 3) << 4;
125
- j = 1;
126
- break;
127
- case 1:
128
- chunk[i++] = b64[t | b >> 4];
129
- t = (b & 15) << 2;
130
- j = 2;
131
- break;
132
- case 2:
133
- chunk[i++] = b64[t | b >> 6];
134
- chunk[i++] = b64[b & 63];
135
- j = 0;
136
- break;
137
- }
138
- if (i > 8191) {
139
- (parts || (parts = [])).push(String.fromCharCode.apply(String, chunk));
140
- i = 0;
141
- }
142
- }
143
- if (j) {
144
- chunk[i++] = b64[t];
145
- chunk[i++] = 61;
146
- if (j === 1)
147
- chunk[i++] = 61;
148
- }
149
- if (parts) {
150
- if (i)
151
- parts.push(String.fromCharCode.apply(String, chunk.slice(0, i)));
152
- return parts.join("");
153
- }
154
- return String.fromCharCode.apply(String, chunk.slice(0, i));
155
- };
156
-
157
- var invalidEncoding = "invalid encoding";
158
-
159
- /**
160
- * Decodes a base64 encoded string to a buffer.
161
- * @param {string} string Source string
162
- * @param {Uint8Array} buffer Destination buffer
163
- * @param {number} offset Destination offset
164
- * @returns {number} Number of bytes written
165
- * @throws {Error} If encoding is invalid
166
- */
167
- base64.decode = function decode(string, buffer, offset) {
168
- var start = offset;
169
- var j = 0, // goto index
170
- t; // temporary
171
- for (var i = 0; i < string.length;) {
172
- var c = string.charCodeAt(i++);
173
- if (c === 61 && j > 1)
174
- break;
175
- if ((c = s64[c]) === undefined)
176
- throw Error(invalidEncoding);
177
- switch (j) {
178
- case 0:
179
- t = c;
180
- j = 1;
181
- break;
182
- case 1:
183
- buffer[offset++] = t << 2 | (c & 48) >> 4;
184
- t = c;
185
- j = 2;
186
- break;
187
- case 2:
188
- buffer[offset++] = (t & 15) << 4 | (c & 60) >> 2;
189
- t = c;
190
- j = 3;
191
- break;
192
- case 3:
193
- buffer[offset++] = (t & 3) << 6 | c;
194
- j = 0;
195
- break;
196
- }
197
- }
198
- if (j === 1)
199
- throw Error(invalidEncoding);
200
- return offset - start;
201
- };
202
-
203
- /**
204
- * Tests if the specified string appears to be base64 encoded.
205
- * @param {string} string String to test
206
- * @returns {boolean} `true` if probably base64 encoded, otherwise false
207
- */
208
- base64.test = function test(string) {
209
- return /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(string);
210
- };
211
- } (base64$1));
212
- return base64$1;
42
+ function encodeUint8Array(value, buf, offset = 0) {
43
+ switch (encodingLength(value)) {
44
+ case 8: {
45
+ buf[offset++] = (value & 0xFF) | MSB;
46
+ value /= 128;
47
+ }
48
+ case 7: {
49
+ buf[offset++] = (value & 0xFF) | MSB;
50
+ value /= 128;
51
+ }
52
+ case 6: {
53
+ buf[offset++] = (value & 0xFF) | MSB;
54
+ value /= 128;
55
+ }
56
+ case 5: {
57
+ buf[offset++] = (value & 0xFF) | MSB;
58
+ value /= 128;
59
+ }
60
+ case 4: {
61
+ buf[offset++] = (value & 0xFF) | MSB;
62
+ value >>>= 7;
63
+ }
64
+ case 3: {
65
+ buf[offset++] = (value & 0xFF) | MSB;
66
+ value >>>= 7;
67
+ }
68
+ case 2: {
69
+ buf[offset++] = (value & 0xFF) | MSB;
70
+ value >>>= 7;
71
+ }
72
+ case 1: {
73
+ buf[offset++] = (value & 0xFF);
74
+ value >>>= 7;
75
+ break;
76
+ }
77
+ default: throw new Error('unreachable');
78
+ }
79
+ return buf;
213
80
  }
214
-
215
- var eventemitter;
216
- var hasRequiredEventemitter;
217
-
218
- function requireEventemitter () {
219
- if (hasRequiredEventemitter) return eventemitter;
220
- hasRequiredEventemitter = 1;
221
- eventemitter = EventEmitter;
222
-
223
- /**
224
- * Constructs a new event emitter instance.
225
- * @classdesc A minimal event emitter.
226
- * @memberof util
227
- * @constructor
228
- */
229
- function EventEmitter() {
230
-
231
- /**
232
- * Registered listeners.
233
- * @type {Object.<string,*>}
234
- * @private
235
- */
236
- this._listeners = {};
237
- }
238
-
239
- /**
240
- * Registers an event listener.
241
- * @param {string} evt Event name
242
- * @param {function} fn Listener
243
- * @param {*} [ctx] Listener context
244
- * @returns {util.EventEmitter} `this`
245
- */
246
- EventEmitter.prototype.on = function on(evt, fn, ctx) {
247
- (this._listeners[evt] || (this._listeners[evt] = [])).push({
248
- fn : fn,
249
- ctx : ctx || this
250
- });
251
- return this;
252
- };
253
-
254
- /**
255
- * Removes an event listener or any matching listeners if arguments are omitted.
256
- * @param {string} [evt] Event name. Removes all listeners if omitted.
257
- * @param {function} [fn] Listener to remove. Removes all listeners of `evt` if omitted.
258
- * @returns {util.EventEmitter} `this`
259
- */
260
- EventEmitter.prototype.off = function off(evt, fn) {
261
- if (evt === undefined)
262
- this._listeners = {};
263
- else {
264
- if (fn === undefined)
265
- this._listeners[evt] = [];
266
- else {
267
- var listeners = this._listeners[evt];
268
- for (var i = 0; i < listeners.length;)
269
- if (listeners[i].fn === fn)
270
- listeners.splice(i, 1);
271
- else
272
- ++i;
273
- }
274
- }
275
- return this;
276
- };
277
-
278
- /**
279
- * Emits an event by calling its listeners with the specified arguments.
280
- * @param {string} evt Event name
281
- * @param {...*} args Arguments
282
- * @returns {util.EventEmitter} `this`
283
- */
284
- EventEmitter.prototype.emit = function emit(evt) {
285
- var listeners = this._listeners[evt];
286
- if (listeners) {
287
- var args = [],
288
- i = 1;
289
- for (; i < arguments.length;)
290
- args.push(arguments[i++]);
291
- for (i = 0; i < listeners.length;)
292
- listeners[i].fn.apply(listeners[i++].ctx, args);
293
- }
294
- return this;
295
- };
296
- return eventemitter;
81
+ function decodeUint8Array(buf, offset) {
82
+ let b = buf[offset];
83
+ let res = 0;
84
+ res += b & REST;
85
+ if (b < MSB) {
86
+ return res;
87
+ }
88
+ b = buf[offset + 1];
89
+ res += (b & REST) << 7;
90
+ if (b < MSB) {
91
+ return res;
92
+ }
93
+ b = buf[offset + 2];
94
+ res += (b & REST) << 14;
95
+ if (b < MSB) {
96
+ return res;
97
+ }
98
+ b = buf[offset + 3];
99
+ res += (b & REST) << 21;
100
+ if (b < MSB) {
101
+ return res;
102
+ }
103
+ b = buf[offset + 4];
104
+ res += (b & REST) * N4;
105
+ if (b < MSB) {
106
+ return res;
107
+ }
108
+ b = buf[offset + 5];
109
+ res += (b & REST) * N5;
110
+ if (b < MSB) {
111
+ return res;
112
+ }
113
+ b = buf[offset + 6];
114
+ res += (b & REST) * N6;
115
+ if (b < MSB) {
116
+ return res;
117
+ }
118
+ b = buf[offset + 7];
119
+ res += (b & REST) * N7;
120
+ if (b < MSB) {
121
+ return res;
122
+ }
123
+ throw new RangeError('Could not decode varint');
297
124
  }
298
125
 
299
- var float;
300
- var hasRequiredFloat;
301
-
302
- function requireFloat () {
303
- if (hasRequiredFloat) return float;
304
- hasRequiredFloat = 1;
305
-
306
- float = factory(factory);
307
-
308
- /**
309
- * Reads / writes floats / doubles from / to buffers.
310
- * @name util.float
311
- * @namespace
312
- */
313
-
314
- /**
315
- * Writes a 32 bit float to a buffer using little endian byte order.
316
- * @name util.float.writeFloatLE
317
- * @function
318
- * @param {number} val Value to write
319
- * @param {Uint8Array} buf Target buffer
320
- * @param {number} pos Target buffer offset
321
- * @returns {undefined}
322
- */
323
-
324
- /**
325
- * Writes a 32 bit float to a buffer using big endian byte order.
326
- * @name util.float.writeFloatBE
327
- * @function
328
- * @param {number} val Value to write
329
- * @param {Uint8Array} buf Target buffer
330
- * @param {number} pos Target buffer offset
331
- * @returns {undefined}
332
- */
333
-
334
- /**
335
- * Reads a 32 bit float from a buffer using little endian byte order.
336
- * @name util.float.readFloatLE
337
- * @function
338
- * @param {Uint8Array} buf Source buffer
339
- * @param {number} pos Source buffer offset
340
- * @returns {number} Value read
341
- */
342
-
343
- /**
344
- * Reads a 32 bit float from a buffer using big endian byte order.
345
- * @name util.float.readFloatBE
346
- * @function
347
- * @param {Uint8Array} buf Source buffer
348
- * @param {number} pos Source buffer offset
349
- * @returns {number} Value read
350
- */
351
-
352
- /**
353
- * Writes a 64 bit double to a buffer using little endian byte order.
354
- * @name util.float.writeDoubleLE
355
- * @function
356
- * @param {number} val Value to write
357
- * @param {Uint8Array} buf Target buffer
358
- * @param {number} pos Target buffer offset
359
- * @returns {undefined}
360
- */
361
-
362
- /**
363
- * Writes a 64 bit double to a buffer using big endian byte order.
364
- * @name util.float.writeDoubleBE
365
- * @function
366
- * @param {number} val Value to write
367
- * @param {Uint8Array} buf Target buffer
368
- * @param {number} pos Target buffer offset
369
- * @returns {undefined}
370
- */
371
-
372
- /**
373
- * Reads a 64 bit double from a buffer using little endian byte order.
374
- * @name util.float.readDoubleLE
375
- * @function
376
- * @param {Uint8Array} buf Source buffer
377
- * @param {number} pos Source buffer offset
378
- * @returns {number} Value read
379
- */
380
-
381
- /**
382
- * Reads a 64 bit double from a buffer using big endian byte order.
383
- * @name util.float.readDoubleBE
384
- * @function
385
- * @param {Uint8Array} buf Source buffer
386
- * @param {number} pos Source buffer offset
387
- * @returns {number} Value read
388
- */
389
-
390
- // Factory function for the purpose of node-based testing in modified global environments
391
- function factory(exports) {
392
-
393
- // float: typed array
394
- if (typeof Float32Array !== "undefined") (function() {
395
-
396
- var f32 = new Float32Array([ -0 ]),
397
- f8b = new Uint8Array(f32.buffer),
398
- le = f8b[3] === 128;
399
-
400
- function writeFloat_f32_cpy(val, buf, pos) {
401
- f32[0] = val;
402
- buf[pos ] = f8b[0];
403
- buf[pos + 1] = f8b[1];
404
- buf[pos + 2] = f8b[2];
405
- buf[pos + 3] = f8b[3];
406
- }
407
-
408
- function writeFloat_f32_rev(val, buf, pos) {
409
- f32[0] = val;
410
- buf[pos ] = f8b[3];
411
- buf[pos + 1] = f8b[2];
412
- buf[pos + 2] = f8b[1];
413
- buf[pos + 3] = f8b[0];
414
- }
415
-
416
- /* istanbul ignore next */
417
- exports.writeFloatLE = le ? writeFloat_f32_cpy : writeFloat_f32_rev;
418
- /* istanbul ignore next */
419
- exports.writeFloatBE = le ? writeFloat_f32_rev : writeFloat_f32_cpy;
420
-
421
- function readFloat_f32_cpy(buf, pos) {
422
- f8b[0] = buf[pos ];
423
- f8b[1] = buf[pos + 1];
424
- f8b[2] = buf[pos + 2];
425
- f8b[3] = buf[pos + 3];
426
- return f32[0];
427
- }
428
-
429
- function readFloat_f32_rev(buf, pos) {
430
- f8b[3] = buf[pos ];
431
- f8b[2] = buf[pos + 1];
432
- f8b[1] = buf[pos + 2];
433
- f8b[0] = buf[pos + 3];
434
- return f32[0];
435
- }
436
-
437
- /* istanbul ignore next */
438
- exports.readFloatLE = le ? readFloat_f32_cpy : readFloat_f32_rev;
439
- /* istanbul ignore next */
440
- exports.readFloatBE = le ? readFloat_f32_rev : readFloat_f32_cpy;
441
-
442
- // float: ieee754
443
- })(); else (function() {
444
-
445
- function writeFloat_ieee754(writeUint, val, buf, pos) {
446
- var sign = val < 0 ? 1 : 0;
447
- if (sign)
448
- val = -val;
449
- if (val === 0)
450
- writeUint(1 / val > 0 ? /* positive */ 0 : /* negative 0 */ 2147483648, buf, pos);
451
- else if (isNaN(val))
452
- writeUint(2143289344, buf, pos);
453
- else if (val > 3.4028234663852886e+38) // +-Infinity
454
- writeUint((sign << 31 | 2139095040) >>> 0, buf, pos);
455
- else if (val < 1.1754943508222875e-38) // denormal
456
- writeUint((sign << 31 | Math.round(val / 1.401298464324817e-45)) >>> 0, buf, pos);
457
- else {
458
- var exponent = Math.floor(Math.log(val) / Math.LN2),
459
- mantissa = Math.round(val * Math.pow(2, -exponent) * 8388608) & 8388607;
460
- writeUint((sign << 31 | exponent + 127 << 23 | mantissa) >>> 0, buf, pos);
461
- }
462
- }
463
-
464
- exports.writeFloatLE = writeFloat_ieee754.bind(null, writeUintLE);
465
- exports.writeFloatBE = writeFloat_ieee754.bind(null, writeUintBE);
466
-
467
- function readFloat_ieee754(readUint, buf, pos) {
468
- var uint = readUint(buf, pos),
469
- sign = (uint >> 31) * 2 + 1,
470
- exponent = uint >>> 23 & 255,
471
- mantissa = uint & 8388607;
472
- return exponent === 255
473
- ? mantissa
474
- ? NaN
475
- : sign * Infinity
476
- : exponent === 0 // denormal
477
- ? sign * 1.401298464324817e-45 * mantissa
478
- : sign * Math.pow(2, exponent - 150) * (mantissa + 8388608);
479
- }
480
-
481
- exports.readFloatLE = readFloat_ieee754.bind(null, readUintLE);
482
- exports.readFloatBE = readFloat_ieee754.bind(null, readUintBE);
483
-
484
- })();
485
-
486
- // double: typed array
487
- if (typeof Float64Array !== "undefined") (function() {
488
-
489
- var f64 = new Float64Array([-0]),
490
- f8b = new Uint8Array(f64.buffer),
491
- le = f8b[7] === 128;
492
-
493
- function writeDouble_f64_cpy(val, buf, pos) {
494
- f64[0] = val;
495
- buf[pos ] = f8b[0];
496
- buf[pos + 1] = f8b[1];
497
- buf[pos + 2] = f8b[2];
498
- buf[pos + 3] = f8b[3];
499
- buf[pos + 4] = f8b[4];
500
- buf[pos + 5] = f8b[5];
501
- buf[pos + 6] = f8b[6];
502
- buf[pos + 7] = f8b[7];
503
- }
504
-
505
- function writeDouble_f64_rev(val, buf, pos) {
506
- f64[0] = val;
507
- buf[pos ] = f8b[7];
508
- buf[pos + 1] = f8b[6];
509
- buf[pos + 2] = f8b[5];
510
- buf[pos + 3] = f8b[4];
511
- buf[pos + 4] = f8b[3];
512
- buf[pos + 5] = f8b[2];
513
- buf[pos + 6] = f8b[1];
514
- buf[pos + 7] = f8b[0];
515
- }
516
-
517
- /* istanbul ignore next */
518
- exports.writeDoubleLE = le ? writeDouble_f64_cpy : writeDouble_f64_rev;
519
- /* istanbul ignore next */
520
- exports.writeDoubleBE = le ? writeDouble_f64_rev : writeDouble_f64_cpy;
521
-
522
- function readDouble_f64_cpy(buf, pos) {
523
- f8b[0] = buf[pos ];
524
- f8b[1] = buf[pos + 1];
525
- f8b[2] = buf[pos + 2];
526
- f8b[3] = buf[pos + 3];
527
- f8b[4] = buf[pos + 4];
528
- f8b[5] = buf[pos + 5];
529
- f8b[6] = buf[pos + 6];
530
- f8b[7] = buf[pos + 7];
531
- return f64[0];
532
- }
533
-
534
- function readDouble_f64_rev(buf, pos) {
535
- f8b[7] = buf[pos ];
536
- f8b[6] = buf[pos + 1];
537
- f8b[5] = buf[pos + 2];
538
- f8b[4] = buf[pos + 3];
539
- f8b[3] = buf[pos + 4];
540
- f8b[2] = buf[pos + 5];
541
- f8b[1] = buf[pos + 6];
542
- f8b[0] = buf[pos + 7];
543
- return f64[0];
544
- }
545
-
546
- /* istanbul ignore next */
547
- exports.readDoubleLE = le ? readDouble_f64_cpy : readDouble_f64_rev;
548
- /* istanbul ignore next */
549
- exports.readDoubleBE = le ? readDouble_f64_rev : readDouble_f64_cpy;
550
-
551
- // double: ieee754
552
- })(); else (function() {
553
-
554
- function writeDouble_ieee754(writeUint, off0, off1, val, buf, pos) {
555
- var sign = val < 0 ? 1 : 0;
556
- if (sign)
557
- val = -val;
558
- if (val === 0) {
559
- writeUint(0, buf, pos + off0);
560
- writeUint(1 / val > 0 ? /* positive */ 0 : /* negative 0 */ 2147483648, buf, pos + off1);
561
- } else if (isNaN(val)) {
562
- writeUint(0, buf, pos + off0);
563
- writeUint(2146959360, buf, pos + off1);
564
- } else if (val > 1.7976931348623157e+308) { // +-Infinity
565
- writeUint(0, buf, pos + off0);
566
- writeUint((sign << 31 | 2146435072) >>> 0, buf, pos + off1);
567
- } else {
568
- var mantissa;
569
- if (val < 2.2250738585072014e-308) { // denormal
570
- mantissa = val / 5e-324;
571
- writeUint(mantissa >>> 0, buf, pos + off0);
572
- writeUint((sign << 31 | mantissa / 4294967296) >>> 0, buf, pos + off1);
573
- } else {
574
- var exponent = Math.floor(Math.log(val) / Math.LN2);
575
- if (exponent === 1024)
576
- exponent = 1023;
577
- mantissa = val * Math.pow(2, -exponent);
578
- writeUint(mantissa * 4503599627370496 >>> 0, buf, pos + off0);
579
- writeUint((sign << 31 | exponent + 1023 << 20 | mantissa * 1048576 & 1048575) >>> 0, buf, pos + off1);
580
- }
581
- }
582
- }
583
-
584
- exports.writeDoubleLE = writeDouble_ieee754.bind(null, writeUintLE, 0, 4);
585
- exports.writeDoubleBE = writeDouble_ieee754.bind(null, writeUintBE, 4, 0);
586
-
587
- function readDouble_ieee754(readUint, off0, off1, buf, pos) {
588
- var lo = readUint(buf, pos + off0),
589
- hi = readUint(buf, pos + off1);
590
- var sign = (hi >> 31) * 2 + 1,
591
- exponent = hi >>> 20 & 2047,
592
- mantissa = 4294967296 * (hi & 1048575) + lo;
593
- return exponent === 2047
594
- ? mantissa
595
- ? NaN
596
- : sign * Infinity
597
- : exponent === 0 // denormal
598
- ? sign * 5e-324 * mantissa
599
- : sign * Math.pow(2, exponent - 1075) * (mantissa + 4503599627370496);
600
- }
601
-
602
- exports.readDoubleLE = readDouble_ieee754.bind(null, readUintLE, 0, 4);
603
- exports.readDoubleBE = readDouble_ieee754.bind(null, readUintBE, 4, 0);
604
-
605
- })();
606
-
607
- return exports;
608
- }
609
-
610
- // uint helpers
611
-
612
- function writeUintLE(val, buf, pos) {
613
- buf[pos ] = val & 255;
614
- buf[pos + 1] = val >>> 8 & 255;
615
- buf[pos + 2] = val >>> 16 & 255;
616
- buf[pos + 3] = val >>> 24;
617
- }
618
-
619
- function writeUintBE(val, buf, pos) {
620
- buf[pos ] = val >>> 24;
621
- buf[pos + 1] = val >>> 16 & 255;
622
- buf[pos + 2] = val >>> 8 & 255;
623
- buf[pos + 3] = val & 255;
624
- }
625
-
626
- function readUintLE(buf, pos) {
627
- return (buf[pos ]
628
- | buf[pos + 1] << 8
629
- | buf[pos + 2] << 16
630
- | buf[pos + 3] << 24) >>> 0;
631
- }
632
-
633
- function readUintBE(buf, pos) {
634
- return (buf[pos ] << 24
635
- | buf[pos + 1] << 16
636
- | buf[pos + 2] << 8
637
- | buf[pos + 3]) >>> 0;
638
- }
639
- return float;
126
+ const f32 = new Float32Array([-0]);
127
+ const f8b = new Uint8Array(f32.buffer);
128
+ /**
129
+ * Writes a 32 bit float to a buffer using little endian byte order
130
+ */
131
+ function writeFloatLE(val, buf, pos) {
132
+ f32[0] = val;
133
+ buf[pos] = f8b[0];
134
+ buf[pos + 1] = f8b[1];
135
+ buf[pos + 2] = f8b[2];
136
+ buf[pos + 3] = f8b[3];
640
137
  }
641
-
642
- var inquire_1;
643
- var hasRequiredInquire;
644
-
645
- function requireInquire () {
646
- if (hasRequiredInquire) return inquire_1;
647
- hasRequiredInquire = 1;
648
- inquire_1 = inquire;
649
-
650
- /**
651
- * Requires a module only if available.
652
- * @memberof util
653
- * @param {string} moduleName Module to require
654
- * @returns {?Object} Required module if available and not empty, otherwise `null`
655
- */
656
- function inquire(moduleName) {
657
- try {
658
- var mod = eval("quire".replace(/^/,"re"))(moduleName); // eslint-disable-line no-eval
659
- if (mod && (mod.length || Object.keys(mod).length))
660
- return mod;
661
- } catch (e) {} // eslint-disable-line no-empty
662
- return null;
663
- }
664
- return inquire_1;
138
+ /**
139
+ * Reads a 32 bit float from a buffer using little endian byte order
140
+ */
141
+ function readFloatLE(buf, pos) {
142
+ f8b[0] = buf[pos];
143
+ f8b[1] = buf[pos + 1];
144
+ f8b[2] = buf[pos + 2];
145
+ f8b[3] = buf[pos + 3];
146
+ return f32[0];
665
147
  }
666
-
667
- var utf8$2 = {};
668
-
669
- var hasRequiredUtf8;
670
-
671
- function requireUtf8 () {
672
- if (hasRequiredUtf8) return utf8$2;
673
- hasRequiredUtf8 = 1;
674
- (function (exports) {
675
-
676
- /**
677
- * A minimal UTF8 implementation for number arrays.
678
- * @memberof util
679
- * @namespace
680
- */
681
- var utf8 = exports;
682
-
683
- /**
684
- * Calculates the UTF8 byte length of a string.
685
- * @param {string} string String
686
- * @returns {number} Byte length
687
- */
688
- utf8.length = function utf8_length(string) {
689
- var len = 0,
690
- c = 0;
691
- for (var i = 0; i < string.length; ++i) {
692
- c = string.charCodeAt(i);
693
- if (c < 128)
694
- len += 1;
695
- else if (c < 2048)
696
- len += 2;
697
- else if ((c & 0xFC00) === 0xD800 && (string.charCodeAt(i + 1) & 0xFC00) === 0xDC00) {
698
- ++i;
699
- len += 4;
700
- } else
701
- len += 3;
702
- }
703
- return len;
704
- };
705
-
706
- /**
707
- * Reads UTF8 bytes as a string.
708
- * @param {Uint8Array} buffer Source buffer
709
- * @param {number} start Source start
710
- * @param {number} end Source end
711
- * @returns {string} String read
712
- */
713
- utf8.read = function utf8_read(buffer, start, end) {
714
- var len = end - start;
715
- if (len < 1)
716
- return "";
717
- var parts = null,
718
- chunk = [],
719
- i = 0, // char offset
720
- t; // temporary
721
- while (start < end) {
722
- t = buffer[start++];
723
- if (t < 128)
724
- chunk[i++] = t;
725
- else if (t > 191 && t < 224)
726
- chunk[i++] = (t & 31) << 6 | buffer[start++] & 63;
727
- else if (t > 239 && t < 365) {
728
- t = ((t & 7) << 18 | (buffer[start++] & 63) << 12 | (buffer[start++] & 63) << 6 | buffer[start++] & 63) - 0x10000;
729
- chunk[i++] = 0xD800 + (t >> 10);
730
- chunk[i++] = 0xDC00 + (t & 1023);
731
- } else
732
- chunk[i++] = (t & 15) << 12 | (buffer[start++] & 63) << 6 | buffer[start++] & 63;
733
- if (i > 8191) {
734
- (parts || (parts = [])).push(String.fromCharCode.apply(String, chunk));
735
- i = 0;
736
- }
737
- }
738
- if (parts) {
739
- if (i)
740
- parts.push(String.fromCharCode.apply(String, chunk.slice(0, i)));
741
- return parts.join("");
742
- }
743
- return String.fromCharCode.apply(String, chunk.slice(0, i));
744
- };
745
-
746
- /**
747
- * Writes a string as UTF8 bytes.
748
- * @param {string} string Source string
749
- * @param {Uint8Array} buffer Destination buffer
750
- * @param {number} offset Destination offset
751
- * @returns {number} Bytes written
752
- */
753
- utf8.write = function utf8_write(string, buffer, offset) {
754
- var start = offset,
755
- c1, // character 1
756
- c2; // character 2
757
- for (var i = 0; i < string.length; ++i) {
758
- c1 = string.charCodeAt(i);
759
- if (c1 < 128) {
760
- buffer[offset++] = c1;
761
- } else if (c1 < 2048) {
762
- buffer[offset++] = c1 >> 6 | 192;
763
- buffer[offset++] = c1 & 63 | 128;
764
- } else if ((c1 & 0xFC00) === 0xD800 && ((c2 = string.charCodeAt(i + 1)) & 0xFC00) === 0xDC00) {
765
- c1 = 0x10000 + ((c1 & 0x03FF) << 10) + (c2 & 0x03FF);
766
- ++i;
767
- buffer[offset++] = c1 >> 18 | 240;
768
- buffer[offset++] = c1 >> 12 & 63 | 128;
769
- buffer[offset++] = c1 >> 6 & 63 | 128;
770
- buffer[offset++] = c1 & 63 | 128;
771
- } else {
772
- buffer[offset++] = c1 >> 12 | 224;
773
- buffer[offset++] = c1 >> 6 & 63 | 128;
774
- buffer[offset++] = c1 & 63 | 128;
775
- }
776
- }
777
- return offset - start;
778
- };
779
- } (utf8$2));
780
- return utf8$2;
148
+ const f64 = new Float64Array([-0]);
149
+ const d8b = new Uint8Array(f64.buffer);
150
+ /**
151
+ * Writes a 64 bit double to a buffer using little endian byte order
152
+ */
153
+ function writeDoubleLE(val, buf, pos) {
154
+ f64[0] = val;
155
+ buf[pos] = d8b[0];
156
+ buf[pos + 1] = d8b[1];
157
+ buf[pos + 2] = d8b[2];
158
+ buf[pos + 3] = d8b[3];
159
+ buf[pos + 4] = d8b[4];
160
+ buf[pos + 5] = d8b[5];
161
+ buf[pos + 6] = d8b[6];
162
+ buf[pos + 7] = d8b[7];
781
163
  }
782
-
783
- var pool_1;
784
- var hasRequiredPool;
785
-
786
- function requirePool () {
787
- if (hasRequiredPool) return pool_1;
788
- hasRequiredPool = 1;
789
- pool_1 = pool;
790
-
791
- /**
792
- * An allocator as used by {@link util.pool}.
793
- * @typedef PoolAllocator
794
- * @type {function}
795
- * @param {number} size Buffer size
796
- * @returns {Uint8Array} Buffer
797
- */
798
-
799
- /**
800
- * A slicer as used by {@link util.pool}.
801
- * @typedef PoolSlicer
802
- * @type {function}
803
- * @param {number} start Start offset
804
- * @param {number} end End offset
805
- * @returns {Uint8Array} Buffer slice
806
- * @this {Uint8Array}
807
- */
808
-
809
- /**
810
- * A general purpose buffer pool.
811
- * @memberof util
812
- * @function
813
- * @param {PoolAllocator} alloc Allocator
814
- * @param {PoolSlicer} slice Slicer
815
- * @param {number} [size=8192] Slab size
816
- * @returns {PoolAllocator} Pooled allocator
817
- */
818
- function pool(alloc, slice, size) {
819
- var SIZE = size || 8192;
820
- var MAX = SIZE >>> 1;
821
- var slab = null;
822
- var offset = SIZE;
823
- return function pool_alloc(size) {
824
- if (size < 1 || size > MAX)
825
- return alloc(size);
826
- if (offset + size > SIZE) {
827
- slab = alloc(SIZE);
828
- offset = 0;
829
- }
830
- var buf = slice.call(slab, offset, offset += size);
831
- if (offset & 7) // align to 32 bit
832
- offset = (offset | 7) + 1;
833
- return buf;
834
- };
835
- }
836
- return pool_1;
164
+ /**
165
+ * Reads a 64 bit double from a buffer using little endian byte order
166
+ */
167
+ function readDoubleLE(buf, pos) {
168
+ d8b[0] = buf[pos];
169
+ d8b[1] = buf[pos + 1];
170
+ d8b[2] = buf[pos + 2];
171
+ d8b[3] = buf[pos + 3];
172
+ d8b[4] = buf[pos + 4];
173
+ d8b[5] = buf[pos + 5];
174
+ d8b[6] = buf[pos + 6];
175
+ d8b[7] = buf[pos + 7];
176
+ return f64[0];
837
177
  }
838
178
 
839
- var longbits;
840
- var hasRequiredLongbits;
841
-
842
- function requireLongbits () {
843
- if (hasRequiredLongbits) return longbits;
844
- hasRequiredLongbits = 1;
845
- longbits = LongBits;
846
-
847
- var util = requireMinimal();
848
-
849
- /**
850
- * Constructs new long bits.
851
- * @classdesc Helper class for working with the low and high bits of a 64 bit value.
852
- * @memberof util
853
- * @constructor
854
- * @param {number} lo Low 32 bits, unsigned
855
- * @param {number} hi High 32 bits, unsigned
856
- */
857
- function LongBits(lo, hi) {
858
-
859
- // note that the casts below are theoretically unnecessary as of today, but older statically
860
- // generated converter code might still call the ctor with signed 32bits. kept for compat.
861
-
862
- /**
863
- * Low bits.
864
- * @type {number}
865
- */
866
- this.lo = lo >>> 0;
867
-
868
- /**
869
- * High bits.
870
- * @type {number}
871
- */
872
- this.hi = hi >>> 0;
873
- }
874
-
875
- /**
876
- * Zero bits.
877
- * @memberof util.LongBits
878
- * @type {util.LongBits}
879
- */
880
- var zero = LongBits.zero = new LongBits(0, 0);
881
-
882
- zero.toNumber = function() { return 0; };
883
- zero.zzEncode = zero.zzDecode = function() { return this; };
884
- zero.length = function() { return 1; };
885
-
886
- /**
887
- * Zero hash.
888
- * @memberof util.LongBits
889
- * @type {string}
890
- */
891
- var zeroHash = LongBits.zeroHash = "\0\0\0\0\0\0\0\0";
892
-
893
- /**
894
- * Constructs new long bits from the specified number.
895
- * @param {number} value Value
896
- * @returns {util.LongBits} Instance
897
- */
898
- LongBits.fromNumber = function fromNumber(value) {
899
- if (value === 0)
900
- return zero;
901
- var sign = value < 0;
902
- if (sign)
903
- value = -value;
904
- var lo = value >>> 0,
905
- hi = (value - lo) / 4294967296 >>> 0;
906
- if (sign) {
907
- hi = ~hi >>> 0;
908
- lo = ~lo >>> 0;
909
- if (++lo > 4294967295) {
910
- lo = 0;
911
- if (++hi > 4294967295)
912
- hi = 0;
913
- }
914
- }
915
- return new LongBits(lo, hi);
916
- };
917
-
918
- /**
919
- * Constructs new long bits from a number, long or string.
920
- * @param {Long|number|string} value Value
921
- * @returns {util.LongBits} Instance
922
- */
923
- LongBits.from = function from(value) {
924
- if (typeof value === "number")
925
- return LongBits.fromNumber(value);
926
- if (util.isString(value)) {
927
- /* istanbul ignore else */
928
- if (util.Long)
929
- value = util.Long.fromString(value);
930
- else
931
- return LongBits.fromNumber(parseInt(value, 10));
932
- }
933
- return value.low || value.high ? new LongBits(value.low >>> 0, value.high >>> 0) : zero;
934
- };
935
-
936
- /**
937
- * Converts this long bits to a possibly unsafe JavaScript number.
938
- * @param {boolean} [unsigned=false] Whether unsigned or not
939
- * @returns {number} Possibly unsafe number
940
- */
941
- LongBits.prototype.toNumber = function toNumber(unsigned) {
942
- if (!unsigned && this.hi >>> 31) {
943
- var lo = ~this.lo + 1 >>> 0,
944
- hi = ~this.hi >>> 0;
945
- if (!lo)
946
- hi = hi + 1 >>> 0;
947
- return -(lo + hi * 4294967296);
948
- }
949
- return this.lo + this.hi * 4294967296;
950
- };
951
-
952
- /**
953
- * Converts this long bits to a long.
954
- * @param {boolean} [unsigned=false] Whether unsigned or not
955
- * @returns {Long} Long
956
- */
957
- LongBits.prototype.toLong = function toLong(unsigned) {
958
- return util.Long
959
- ? new util.Long(this.lo | 0, this.hi | 0, Boolean(unsigned))
960
- /* istanbul ignore next */
961
- : { low: this.lo | 0, high: this.hi | 0, unsigned: Boolean(unsigned) };
962
- };
963
-
964
- var charCodeAt = String.prototype.charCodeAt;
965
-
966
- /**
967
- * Constructs new long bits from the specified 8 characters long hash.
968
- * @param {string} hash Hash
969
- * @returns {util.LongBits} Bits
970
- */
971
- LongBits.fromHash = function fromHash(hash) {
972
- if (hash === zeroHash)
973
- return zero;
974
- return new LongBits(
975
- ( charCodeAt.call(hash, 0)
976
- | charCodeAt.call(hash, 1) << 8
977
- | charCodeAt.call(hash, 2) << 16
978
- | charCodeAt.call(hash, 3) << 24) >>> 0
979
- ,
980
- ( charCodeAt.call(hash, 4)
981
- | charCodeAt.call(hash, 5) << 8
982
- | charCodeAt.call(hash, 6) << 16
983
- | charCodeAt.call(hash, 7) << 24) >>> 0
984
- );
985
- };
986
-
987
- /**
988
- * Converts this long bits to a 8 characters long hash.
989
- * @returns {string} Hash
990
- */
991
- LongBits.prototype.toHash = function toHash() {
992
- return String.fromCharCode(
993
- this.lo & 255,
994
- this.lo >>> 8 & 255,
995
- this.lo >>> 16 & 255,
996
- this.lo >>> 24 ,
997
- this.hi & 255,
998
- this.hi >>> 8 & 255,
999
- this.hi >>> 16 & 255,
1000
- this.hi >>> 24
1001
- );
1002
- };
1003
-
1004
- /**
1005
- * Zig-zag encodes this long bits.
1006
- * @returns {util.LongBits} `this`
1007
- */
1008
- LongBits.prototype.zzEncode = function zzEncode() {
1009
- var mask = this.hi >> 31;
1010
- this.hi = ((this.hi << 1 | this.lo >>> 31) ^ mask) >>> 0;
1011
- this.lo = ( this.lo << 1 ^ mask) >>> 0;
1012
- return this;
1013
- };
1014
-
1015
- /**
1016
- * Zig-zag decodes this long bits.
1017
- * @returns {util.LongBits} `this`
1018
- */
1019
- LongBits.prototype.zzDecode = function zzDecode() {
1020
- var mask = -(this.lo & 1);
1021
- this.lo = ((this.lo >>> 1 | this.hi << 31) ^ mask) >>> 0;
1022
- this.hi = ( this.hi >>> 1 ^ mask) >>> 0;
1023
- return this;
1024
- };
1025
-
1026
- /**
1027
- * Calculates the length of this longbits when encoded as a varint.
1028
- * @returns {number} Length
1029
- */
1030
- LongBits.prototype.length = function length() {
1031
- var part0 = this.lo,
1032
- part1 = (this.lo >>> 28 | this.hi << 4) >>> 0,
1033
- part2 = this.hi >>> 24;
1034
- return part2 === 0
1035
- ? part1 === 0
1036
- ? part0 < 16384
1037
- ? part0 < 128 ? 1 : 2
1038
- : part0 < 2097152 ? 3 : 4
1039
- : part1 < 16384
1040
- ? part1 < 128 ? 5 : 6
1041
- : part1 < 2097152 ? 7 : 8
1042
- : part2 < 128 ? 9 : 10;
1043
- };
1044
- return longbits;
179
+ // the largest BigInt we can safely downcast to a Number
180
+ const MAX_SAFE_NUMBER_INTEGER = BigInt(Number.MAX_SAFE_INTEGER);
181
+ const MIN_SAFE_NUMBER_INTEGER = BigInt(Number.MIN_SAFE_INTEGER);
182
+ /**
183
+ * Constructs new long bits.
184
+ *
185
+ * @classdesc Helper class for working with the low and high bits of a 64 bit value.
186
+ * @memberof util
187
+ * @function Object() { [native code] }
188
+ * @param {number} lo - Low 32 bits, unsigned
189
+ * @param {number} hi - High 32 bits, unsigned
190
+ */
191
+ class LongBits {
192
+ lo;
193
+ hi;
194
+ constructor(lo, hi) {
195
+ // note that the casts below are theoretically unnecessary as of today, but older statically
196
+ // generated converter code might still call the ctor with signed 32bits. kept for compat.
197
+ /**
198
+ * Low bits
199
+ */
200
+ this.lo = lo | 0;
201
+ /**
202
+ * High bits
203
+ */
204
+ this.hi = hi | 0;
205
+ }
206
+ /**
207
+ * Converts this long bits to a possibly unsafe JavaScript number
208
+ */
209
+ toNumber(unsigned = false) {
210
+ if (!unsigned && (this.hi >>> 31) > 0) {
211
+ const lo = ~this.lo + 1 >>> 0;
212
+ let hi = ~this.hi >>> 0;
213
+ if (lo === 0) {
214
+ hi = hi + 1 >>> 0;
215
+ }
216
+ return -(lo + hi * 4294967296);
217
+ }
218
+ return this.lo + this.hi * 4294967296;
219
+ }
220
+ /**
221
+ * Converts this long bits to a bigint
222
+ */
223
+ toBigInt(unsigned = false) {
224
+ if (unsigned) {
225
+ return BigInt(this.lo >>> 0) + (BigInt(this.hi >>> 0) << 32n);
226
+ }
227
+ if ((this.hi >>> 31) !== 0) {
228
+ const lo = ~this.lo + 1 >>> 0;
229
+ let hi = ~this.hi >>> 0;
230
+ if (lo === 0) {
231
+ hi = hi + 1 >>> 0;
232
+ }
233
+ return -(BigInt(lo) + (BigInt(hi) << 32n));
234
+ }
235
+ return BigInt(this.lo >>> 0) + (BigInt(this.hi >>> 0) << 32n);
236
+ }
237
+ /**
238
+ * Converts this long bits to a string
239
+ */
240
+ toString(unsigned = false) {
241
+ return this.toBigInt(unsigned).toString();
242
+ }
243
+ /**
244
+ * Zig-zag encodes this long bits
245
+ */
246
+ zzEncode() {
247
+ const mask = this.hi >> 31;
248
+ this.hi = ((this.hi << 1 | this.lo >>> 31) ^ mask) >>> 0;
249
+ this.lo = (this.lo << 1 ^ mask) >>> 0;
250
+ return this;
251
+ }
252
+ /**
253
+ * Zig-zag decodes this long bits
254
+ */
255
+ zzDecode() {
256
+ const mask = -(this.lo & 1);
257
+ this.lo = ((this.lo >>> 1 | this.hi << 31) ^ mask) >>> 0;
258
+ this.hi = (this.hi >>> 1 ^ mask) >>> 0;
259
+ return this;
260
+ }
261
+ /**
262
+ * Calculates the length of this longbits when encoded as a varint.
263
+ */
264
+ length() {
265
+ const part0 = this.lo;
266
+ const part1 = (this.lo >>> 28 | this.hi << 4) >>> 0;
267
+ const part2 = this.hi >>> 24;
268
+ return part2 === 0
269
+ ? part1 === 0
270
+ ? part0 < 16384
271
+ ? part0 < 128 ? 1 : 2
272
+ : part0 < 2097152 ? 3 : 4
273
+ : part1 < 16384
274
+ ? part1 < 128 ? 5 : 6
275
+ : part1 < 2097152 ? 7 : 8
276
+ : part2 < 128 ? 9 : 10;
277
+ }
278
+ /**
279
+ * Constructs new long bits from the specified number
280
+ */
281
+ static fromBigInt(value) {
282
+ if (value === 0n) {
283
+ return zero;
284
+ }
285
+ if (value < MAX_SAFE_NUMBER_INTEGER && value > MIN_SAFE_NUMBER_INTEGER) {
286
+ return this.fromNumber(Number(value));
287
+ }
288
+ const negative = value < 0n;
289
+ if (negative) {
290
+ value = -value;
291
+ }
292
+ let hi = value >> 32n;
293
+ let lo = value - (hi << 32n);
294
+ if (negative) {
295
+ hi = ~hi | 0n;
296
+ lo = ~lo | 0n;
297
+ if (++lo > TWO_32) {
298
+ lo = 0n;
299
+ if (++hi > TWO_32) {
300
+ hi = 0n;
301
+ }
302
+ }
303
+ }
304
+ return new LongBits(Number(lo), Number(hi));
305
+ }
306
+ /**
307
+ * Constructs new long bits from the specified number
308
+ */
309
+ static fromNumber(value) {
310
+ if (value === 0) {
311
+ return zero;
312
+ }
313
+ const sign = value < 0;
314
+ if (sign) {
315
+ value = -value;
316
+ }
317
+ let lo = value >>> 0;
318
+ let hi = (value - lo) / 4294967296 >>> 0;
319
+ if (sign) {
320
+ hi = ~hi >>> 0;
321
+ lo = ~lo >>> 0;
322
+ if (++lo > 4294967295) {
323
+ lo = 0;
324
+ if (++hi > 4294967295) {
325
+ hi = 0;
326
+ }
327
+ }
328
+ }
329
+ return new LongBits(lo, hi);
330
+ }
331
+ /**
332
+ * Constructs new long bits from a number, long or string
333
+ */
334
+ static from(value) {
335
+ if (typeof value === 'number') {
336
+ return LongBits.fromNumber(value);
337
+ }
338
+ if (typeof value === 'bigint') {
339
+ return LongBits.fromBigInt(value);
340
+ }
341
+ if (typeof value === 'string') {
342
+ return LongBits.fromBigInt(BigInt(value));
343
+ }
344
+ return value.low != null || value.high != null ? new LongBits(value.low >>> 0, value.high >>> 0) : zero;
345
+ }
1045
346
  }
347
+ const zero = new LongBits(0, 0);
348
+ zero.toBigInt = function () { return 0n; };
349
+ zero.zzEncode = zero.zzDecode = function () { return this; };
350
+ zero.length = function () { return 1; };
351
+ const TWO_32 = 4294967296n;
1046
352
 
1047
- var hasRequiredMinimal;
1048
-
1049
- function requireMinimal () {
1050
- if (hasRequiredMinimal) return minimal;
1051
- hasRequiredMinimal = 1;
1052
- (function (exports) {
1053
- var util = exports;
1054
-
1055
- // used to return a Promise where callback is omitted
1056
- util.asPromise = requireAspromise();
1057
-
1058
- // converts to / from base64 encoded strings
1059
- util.base64 = requireBase64();
1060
-
1061
- // base class of rpc.Service
1062
- util.EventEmitter = requireEventemitter();
1063
-
1064
- // float handling accross browsers
1065
- util.float = requireFloat();
1066
-
1067
- // requires modules optionally and hides the call from bundlers
1068
- util.inquire = requireInquire();
1069
-
1070
- // converts to / from utf8 encoded strings
1071
- util.utf8 = requireUtf8();
1072
-
1073
- // provides a node-like buffer pool in the browser
1074
- util.pool = requirePool();
1075
-
1076
- // utility to work with the low and high bits of a 64 bit value
1077
- util.LongBits = requireLongbits();
1078
-
1079
- /**
1080
- * Whether running within node or not.
1081
- * @memberof util
1082
- * @type {boolean}
1083
- */
1084
- util.isNode = Boolean(typeof commonjsGlobal !== "undefined"
1085
- && commonjsGlobal
1086
- && commonjsGlobal.process
1087
- && commonjsGlobal.process.versions
1088
- && commonjsGlobal.process.versions.node);
1089
-
1090
- /**
1091
- * Global object reference.
1092
- * @memberof util
1093
- * @type {Object}
1094
- */
1095
- util.global = util.isNode && commonjsGlobal
1096
- || typeof window !== "undefined" && window
1097
- || typeof self !== "undefined" && self
1098
- || commonjsGlobal; // eslint-disable-line no-invalid-this
1099
-
1100
- /**
1101
- * An immuable empty array.
1102
- * @memberof util
1103
- * @type {Array.<*>}
1104
- * @const
1105
- */
1106
- util.emptyArray = Object.freeze ? Object.freeze([]) : /* istanbul ignore next */ []; // used on prototypes
1107
-
1108
- /**
1109
- * An immutable empty object.
1110
- * @type {Object}
1111
- * @const
1112
- */
1113
- util.emptyObject = Object.freeze ? Object.freeze({}) : /* istanbul ignore next */ {}; // used on prototypes
1114
-
1115
- /**
1116
- * Tests if the specified value is an integer.
1117
- * @function
1118
- * @param {*} value Value to test
1119
- * @returns {boolean} `true` if the value is an integer
1120
- */
1121
- util.isInteger = Number.isInteger || /* istanbul ignore next */ function isInteger(value) {
1122
- return typeof value === "number" && isFinite(value) && Math.floor(value) === value;
1123
- };
1124
-
1125
- /**
1126
- * Tests if the specified value is a string.
1127
- * @param {*} value Value to test
1128
- * @returns {boolean} `true` if the value is a string
1129
- */
1130
- util.isString = function isString(value) {
1131
- return typeof value === "string" || value instanceof String;
1132
- };
1133
-
1134
- /**
1135
- * Tests if the specified value is a non-null object.
1136
- * @param {*} value Value to test
1137
- * @returns {boolean} `true` if the value is a non-null object
1138
- */
1139
- util.isObject = function isObject(value) {
1140
- return value && typeof value === "object";
1141
- };
1142
-
1143
- /**
1144
- * Checks if a property on a message is considered to be present.
1145
- * This is an alias of {@link util.isSet}.
1146
- * @function
1147
- * @param {Object} obj Plain object or message instance
1148
- * @param {string} prop Property name
1149
- * @returns {boolean} `true` if considered to be present, otherwise `false`
1150
- */
1151
- util.isset =
1152
-
1153
- /**
1154
- * Checks if a property on a message is considered to be present.
1155
- * @param {Object} obj Plain object or message instance
1156
- * @param {string} prop Property name
1157
- * @returns {boolean} `true` if considered to be present, otherwise `false`
1158
- */
1159
- util.isSet = function isSet(obj, prop) {
1160
- var value = obj[prop];
1161
- if (value != null && obj.hasOwnProperty(prop)) // eslint-disable-line eqeqeq, no-prototype-builtins
1162
- return typeof value !== "object" || (Array.isArray(value) ? value.length : Object.keys(value).length) > 0;
1163
- return false;
1164
- };
1165
-
1166
- /**
1167
- * Any compatible Buffer instance.
1168
- * This is a minimal stand-alone definition of a Buffer instance. The actual type is that exported by node's typings.
1169
- * @interface Buffer
1170
- * @extends Uint8Array
1171
- */
1172
-
1173
- /**
1174
- * Node's Buffer class if available.
1175
- * @type {Constructor<Buffer>}
1176
- */
1177
- util.Buffer = (function() {
1178
- try {
1179
- var Buffer = util.inquire("buffer").Buffer;
1180
- // refuse to use non-node buffers if not explicitly assigned (perf reasons):
1181
- return Buffer.prototype.utf8Write ? Buffer : /* istanbul ignore next */ null;
1182
- } catch (e) {
1183
- /* istanbul ignore next */
1184
- return null;
1185
- }
1186
- })();
1187
-
1188
- // Internal alias of or polyfull for Buffer.from.
1189
- util._Buffer_from = null;
1190
-
1191
- // Internal alias of or polyfill for Buffer.allocUnsafe.
1192
- util._Buffer_allocUnsafe = null;
1193
-
1194
- /**
1195
- * Creates a new buffer of whatever type supported by the environment.
1196
- * @param {number|number[]} [sizeOrArray=0] Buffer size or number array
1197
- * @returns {Uint8Array|Buffer} Buffer
1198
- */
1199
- util.newBuffer = function newBuffer(sizeOrArray) {
1200
- /* istanbul ignore next */
1201
- return typeof sizeOrArray === "number"
1202
- ? util.Buffer
1203
- ? util._Buffer_allocUnsafe(sizeOrArray)
1204
- : new util.Array(sizeOrArray)
1205
- : util.Buffer
1206
- ? util._Buffer_from(sizeOrArray)
1207
- : typeof Uint8Array === "undefined"
1208
- ? sizeOrArray
1209
- : new Uint8Array(sizeOrArray);
1210
- };
1211
-
1212
- /**
1213
- * Array implementation used in the browser. `Uint8Array` if supported, otherwise `Array`.
1214
- * @type {Constructor<Uint8Array>}
1215
- */
1216
- util.Array = typeof Uint8Array !== "undefined" ? Uint8Array /* istanbul ignore next */ : Array;
1217
-
1218
- /**
1219
- * Any compatible Long instance.
1220
- * This is a minimal stand-alone definition of a Long instance. The actual type is that exported by long.js.
1221
- * @interface Long
1222
- * @property {number} low Low bits
1223
- * @property {number} high High bits
1224
- * @property {boolean} unsigned Whether unsigned or not
1225
- */
1226
-
1227
- /**
1228
- * Long.js's Long class if available.
1229
- * @type {Constructor<Long>}
1230
- */
1231
- util.Long = /* istanbul ignore next */ util.global.dcodeIO && /* istanbul ignore next */ util.global.dcodeIO.Long
1232
- || /* istanbul ignore next */ util.global.Long
1233
- || util.inquire("long");
1234
-
1235
- /**
1236
- * Regular expression used to verify 2 bit (`bool`) map keys.
1237
- * @type {RegExp}
1238
- * @const
1239
- */
1240
- util.key2Re = /^true|false|0|1$/;
1241
-
1242
- /**
1243
- * Regular expression used to verify 32 bit (`int32` etc.) map keys.
1244
- * @type {RegExp}
1245
- * @const
1246
- */
1247
- util.key32Re = /^-?(?:0|[1-9][0-9]*)$/;
1248
-
1249
- /**
1250
- * Regular expression used to verify 64 bit (`int64` etc.) map keys.
1251
- * @type {RegExp}
1252
- * @const
1253
- */
1254
- util.key64Re = /^(?:[\\x00-\\xff]{8}|-?(?:0|[1-9][0-9]*))$/;
1255
-
1256
- /**
1257
- * Converts a number or long to an 8 characters long hash string.
1258
- * @param {Long|number} value Value to convert
1259
- * @returns {string} Hash
1260
- */
1261
- util.longToHash = function longToHash(value) {
1262
- return value
1263
- ? util.LongBits.from(value).toHash()
1264
- : util.LongBits.zeroHash;
1265
- };
1266
-
1267
- /**
1268
- * Converts an 8 characters long hash string to a long or number.
1269
- * @param {string} hash Hash
1270
- * @param {boolean} [unsigned=false] Whether unsigned or not
1271
- * @returns {Long|number} Original value
1272
- */
1273
- util.longFromHash = function longFromHash(hash, unsigned) {
1274
- var bits = util.LongBits.fromHash(hash);
1275
- if (util.Long)
1276
- return util.Long.fromBits(bits.lo, bits.hi, unsigned);
1277
- return bits.toNumber(Boolean(unsigned));
1278
- };
1279
-
1280
- /**
1281
- * Merges the properties of the source object into the destination object.
1282
- * @memberof util
1283
- * @param {Object.<string,*>} dst Destination object
1284
- * @param {Object.<string,*>} src Source object
1285
- * @param {boolean} [ifNotSet=false] Merges only if the key is not already set
1286
- * @returns {Object.<string,*>} Destination object
1287
- */
1288
- function merge(dst, src, ifNotSet) { // used by converters
1289
- for (var keys = Object.keys(src), i = 0; i < keys.length; ++i)
1290
- if (dst[keys[i]] === undefined || !ifNotSet)
1291
- dst[keys[i]] = src[keys[i]];
1292
- return dst;
1293
- }
1294
-
1295
- util.merge = merge;
1296
-
1297
- /**
1298
- * Converts the first character of a string to lower case.
1299
- * @param {string} str String to convert
1300
- * @returns {string} Converted string
1301
- */
1302
- util.lcFirst = function lcFirst(str) {
1303
- return str.charAt(0).toLowerCase() + str.substring(1);
1304
- };
1305
-
1306
- /**
1307
- * Creates a custom error constructor.
1308
- * @memberof util
1309
- * @param {string} name Error name
1310
- * @returns {Constructor<Error>} Custom error constructor
1311
- */
1312
- function newError(name) {
1313
-
1314
- function CustomError(message, properties) {
1315
-
1316
- if (!(this instanceof CustomError))
1317
- return new CustomError(message, properties);
1318
-
1319
- // Error.call(this, message);
1320
- // ^ just returns a new error instance because the ctor can be called as a function
1321
-
1322
- Object.defineProperty(this, "message", { get: function() { return message; } });
1323
-
1324
- /* istanbul ignore next */
1325
- if (Error.captureStackTrace) // node
1326
- Error.captureStackTrace(this, CustomError);
1327
- else
1328
- Object.defineProperty(this, "stack", { value: new Error().stack || "" });
1329
-
1330
- if (properties)
1331
- merge(this, properties);
1332
- }
1333
-
1334
- CustomError.prototype = Object.create(Error.prototype, {
1335
- constructor: {
1336
- value: CustomError,
1337
- writable: true,
1338
- enumerable: false,
1339
- configurable: true,
1340
- },
1341
- name: {
1342
- get: function get() { return name; },
1343
- set: undefined,
1344
- enumerable: false,
1345
- // configurable: false would accurately preserve the behavior of
1346
- // the original, but I'm guessing that was not intentional.
1347
- // For an actual error subclass, this property would
1348
- // be configurable.
1349
- configurable: true,
1350
- },
1351
- toString: {
1352
- value: function value() { return this.name + ": " + this.message; },
1353
- writable: true,
1354
- enumerable: false,
1355
- configurable: true,
1356
- },
1357
- });
1358
-
1359
- return CustomError;
1360
- }
1361
-
1362
- util.newError = newError;
1363
-
1364
- /**
1365
- * Constructs a new protocol error.
1366
- * @classdesc Error subclass indicating a protocol specifc error.
1367
- * @memberof util
1368
- * @extends Error
1369
- * @template T extends Message<T>
1370
- * @constructor
1371
- * @param {string} message Error message
1372
- * @param {Object.<string,*>} [properties] Additional properties
1373
- * @example
1374
- * try {
1375
- * MyMessage.decode(someBuffer); // throws if required fields are missing
1376
- * } catch (e) {
1377
- * if (e instanceof ProtocolError && e.instance)
1378
- * console.log("decoded so far: " + JSON.stringify(e.instance));
1379
- * }
1380
- */
1381
- util.ProtocolError = newError("ProtocolError");
1382
-
1383
- /**
1384
- * So far decoded message instance.
1385
- * @name util.ProtocolError#instance
1386
- * @type {Message<T>}
1387
- */
1388
-
1389
- /**
1390
- * A OneOf getter as returned by {@link util.oneOfGetter}.
1391
- * @typedef OneOfGetter
1392
- * @type {function}
1393
- * @returns {string|undefined} Set field name, if any
1394
- */
1395
-
1396
- /**
1397
- * Builds a getter for a oneof's present field name.
1398
- * @param {string[]} fieldNames Field names
1399
- * @returns {OneOfGetter} Unbound getter
1400
- */
1401
- util.oneOfGetter = function getOneOf(fieldNames) {
1402
- var fieldMap = {};
1403
- for (var i = 0; i < fieldNames.length; ++i)
1404
- fieldMap[fieldNames[i]] = 1;
1405
-
1406
- /**
1407
- * @returns {string|undefined} Set field name, if any
1408
- * @this Object
1409
- * @ignore
1410
- */
1411
- return function() { // eslint-disable-line consistent-return
1412
- for (var keys = Object.keys(this), i = keys.length - 1; i > -1; --i)
1413
- if (fieldMap[keys[i]] === 1 && this[keys[i]] !== undefined && this[keys[i]] !== null)
1414
- return keys[i];
1415
- };
1416
- };
1417
-
1418
- /**
1419
- * A OneOf setter as returned by {@link util.oneOfSetter}.
1420
- * @typedef OneOfSetter
1421
- * @type {function}
1422
- * @param {string|undefined} value Field name
1423
- * @returns {undefined}
1424
- */
1425
-
1426
- /**
1427
- * Builds a setter for a oneof's present field name.
1428
- * @param {string[]} fieldNames Field names
1429
- * @returns {OneOfSetter} Unbound setter
1430
- */
1431
- util.oneOfSetter = function setOneOf(fieldNames) {
1432
-
1433
- /**
1434
- * @param {string} name Field name
1435
- * @returns {undefined}
1436
- * @this Object
1437
- * @ignore
1438
- */
1439
- return function(name) {
1440
- for (var i = 0; i < fieldNames.length; ++i)
1441
- if (fieldNames[i] !== name)
1442
- delete this[fieldNames[i]];
1443
- };
1444
- };
1445
-
1446
- /**
1447
- * Default conversion options used for {@link Message#toJSON} implementations.
1448
- *
1449
- * These options are close to proto3's JSON mapping with the exception that internal types like Any are handled just like messages. More precisely:
1450
- *
1451
- * - Longs become strings
1452
- * - Enums become string keys
1453
- * - Bytes become base64 encoded strings
1454
- * - (Sub-)Messages become plain objects
1455
- * - Maps become plain objects with all string keys
1456
- * - Repeated fields become arrays
1457
- * - NaN and Infinity for float and double fields become strings
1458
- *
1459
- * @type {IConversionOptions}
1460
- * @see https://developers.google.com/protocol-buffers/docs/proto3?hl=en#json
1461
- */
1462
- util.toJSONOptions = {
1463
- longs: String,
1464
- enums: String,
1465
- bytes: String,
1466
- json: true
1467
- };
1468
-
1469
- // Sets up buffer utility according to the environment (called in index-minimal)
1470
- util._configure = function() {
1471
- var Buffer = util.Buffer;
1472
- /* istanbul ignore if */
1473
- if (!Buffer) {
1474
- util._Buffer_from = util._Buffer_allocUnsafe = null;
1475
- return;
1476
- }
1477
- // because node 4.x buffers are incompatible & immutable
1478
- // see: https://github.com/dcodeIO/protobuf.js/pull/665
1479
- util._Buffer_from = Buffer.from !== Uint8Array.from && Buffer.from ||
1480
- /* istanbul ignore next */
1481
- function Buffer_from(value, encoding) {
1482
- return new Buffer(value, encoding);
1483
- };
1484
- util._Buffer_allocUnsafe = Buffer.allocUnsafe ||
1485
- /* istanbul ignore next */
1486
- function Buffer_allocUnsafe(size) {
1487
- return new Buffer(size);
1488
- };
1489
- };
1490
- } (minimal));
1491
- return minimal;
353
+ /**
354
+ * Calculates the UTF8 byte length of a string
355
+ */
356
+ function length(string) {
357
+ let len = 0;
358
+ let c = 0;
359
+ for (let i = 0; i < string.length; ++i) {
360
+ c = string.charCodeAt(i);
361
+ if (c < 128) {
362
+ len += 1;
363
+ }
364
+ else if (c < 2048) {
365
+ len += 2;
366
+ }
367
+ else if ((c & 0xFC00) === 0xD800 && (string.charCodeAt(i + 1) & 0xFC00) === 0xDC00) {
368
+ ++i;
369
+ len += 4;
370
+ }
371
+ else {
372
+ len += 3;
373
+ }
374
+ }
375
+ return len;
376
+ }
377
+ /**
378
+ * Reads UTF8 bytes as a string
379
+ */
380
+ function read(buffer, start, end) {
381
+ const len = end - start;
382
+ if (len < 1) {
383
+ return '';
384
+ }
385
+ let parts;
386
+ const chunk = [];
387
+ let i = 0; // char offset
388
+ let t; // temporary
389
+ while (start < end) {
390
+ t = buffer[start++];
391
+ if (t < 128) {
392
+ chunk[i++] = t;
393
+ }
394
+ else if (t > 191 && t < 224) {
395
+ chunk[i++] = (t & 31) << 6 | buffer[start++] & 63;
396
+ }
397
+ else if (t > 239 && t < 365) {
398
+ t = ((t & 7) << 18 | (buffer[start++] & 63) << 12 | (buffer[start++] & 63) << 6 | buffer[start++] & 63) - 0x10000;
399
+ chunk[i++] = 0xD800 + (t >> 10);
400
+ chunk[i++] = 0xDC00 + (t & 1023);
401
+ }
402
+ else {
403
+ chunk[i++] = (t & 15) << 12 | (buffer[start++] & 63) << 6 | buffer[start++] & 63;
404
+ }
405
+ if (i > 8191) {
406
+ (parts ?? (parts = [])).push(String.fromCharCode.apply(String, chunk));
407
+ i = 0;
408
+ }
409
+ }
410
+ if (parts != null) {
411
+ if (i > 0) {
412
+ parts.push(String.fromCharCode.apply(String, chunk.slice(0, i)));
413
+ }
414
+ return parts.join('');
415
+ }
416
+ return String.fromCharCode.apply(String, chunk.slice(0, i));
417
+ }
418
+ /**
419
+ * Writes a string as UTF8 bytes
420
+ */
421
+ function write(string, buffer, offset) {
422
+ const start = offset;
423
+ let c1; // character 1
424
+ let c2; // character 2
425
+ for (let i = 0; i < string.length; ++i) {
426
+ c1 = string.charCodeAt(i);
427
+ if (c1 < 128) {
428
+ buffer[offset++] = c1;
429
+ }
430
+ else if (c1 < 2048) {
431
+ buffer[offset++] = c1 >> 6 | 192;
432
+ buffer[offset++] = c1 & 63 | 128;
433
+ }
434
+ else if ((c1 & 0xFC00) === 0xD800 && ((c2 = string.charCodeAt(i + 1)) & 0xFC00) === 0xDC00) {
435
+ c1 = 0x10000 + ((c1 & 0x03FF) << 10) + (c2 & 0x03FF);
436
+ ++i;
437
+ buffer[offset++] = c1 >> 18 | 240;
438
+ buffer[offset++] = c1 >> 12 & 63 | 128;
439
+ buffer[offset++] = c1 >> 6 & 63 | 128;
440
+ buffer[offset++] = c1 & 63 | 128;
441
+ }
442
+ else {
443
+ buffer[offset++] = c1 >> 12 | 224;
444
+ buffer[offset++] = c1 >> 6 & 63 | 128;
445
+ buffer[offset++] = c1 & 63 | 128;
446
+ }
447
+ }
448
+ return offset - start;
1492
449
  }
1493
-
1494
- var reader$1 = Reader$1;
1495
-
1496
- var util$4 = requireMinimal();
1497
-
1498
- var BufferReader$1; // cyclic
1499
-
1500
- var LongBits$1 = util$4.LongBits,
1501
- utf8$1 = util$4.utf8;
1502
450
 
1503
451
  /* istanbul ignore next */
1504
452
  function indexOutOfRange(reader, writeLength) {
1505
- return RangeError("index out of range: " + reader.pos + " + " + (writeLength || 1) + " > " + reader.len);
453
+ return RangeError(`index out of range: ${reader.pos} + ${writeLength ?? 1} > ${reader.len}`);
454
+ }
455
+ function readFixed32End(buf, end) {
456
+ return (buf[end - 4] |
457
+ buf[end - 3] << 8 |
458
+ buf[end - 2] << 16 |
459
+ buf[end - 1] << 24) >>> 0;
1506
460
  }
1507
-
1508
461
  /**
1509
462
  * Constructs a new reader instance using the specified buffer.
1510
- * @classdesc Wire format reader using `Uint8Array` if available, otherwise `Array`.
1511
- * @constructor
1512
- * @param {Uint8Array} buffer Buffer to read from
1513
463
  */
1514
- function Reader$1(buffer) {
1515
-
464
+ class Uint8ArrayReader {
465
+ buf;
466
+ pos;
467
+ len;
468
+ _slice = Uint8Array.prototype.subarray;
469
+ constructor(buffer) {
470
+ /**
471
+ * Read buffer
472
+ */
473
+ this.buf = buffer;
474
+ /**
475
+ * Read buffer position
476
+ */
477
+ this.pos = 0;
478
+ /**
479
+ * Read buffer length
480
+ */
481
+ this.len = buffer.length;
482
+ }
1516
483
  /**
1517
- * Read buffer.
1518
- * @type {Uint8Array}
484
+ * Reads a varint as an unsigned 32 bit value
1519
485
  */
1520
- this.buf = buffer;
1521
-
486
+ uint32() {
487
+ let value = 4294967295;
488
+ value = (this.buf[this.pos] & 127) >>> 0;
489
+ if (this.buf[this.pos++] < 128)
490
+ return value;
491
+ value = (value | (this.buf[this.pos] & 127) << 7) >>> 0;
492
+ if (this.buf[this.pos++] < 128)
493
+ return value;
494
+ value = (value | (this.buf[this.pos] & 127) << 14) >>> 0;
495
+ if (this.buf[this.pos++] < 128)
496
+ return value;
497
+ value = (value | (this.buf[this.pos] & 127) << 21) >>> 0;
498
+ if (this.buf[this.pos++] < 128)
499
+ return value;
500
+ value = (value | (this.buf[this.pos] & 15) << 28) >>> 0;
501
+ if (this.buf[this.pos++] < 128)
502
+ return value;
503
+ if ((this.pos += 5) > this.len) {
504
+ this.pos = this.len;
505
+ throw indexOutOfRange(this, 10);
506
+ }
507
+ return value;
508
+ }
1522
509
  /**
1523
- * Read buffer position.
1524
- * @type {number}
510
+ * Reads a varint as a signed 32 bit value
1525
511
  */
1526
- this.pos = 0;
1527
-
512
+ int32() {
513
+ return this.uint32() | 0;
514
+ }
515
+ /**
516
+ * Reads a zig-zag encoded varint as a signed 32 bit value
517
+ */
518
+ sint32() {
519
+ const value = this.uint32();
520
+ return value >>> 1 ^ -(value & 1) | 0;
521
+ }
522
+ /**
523
+ * Reads a varint as a boolean
524
+ */
525
+ bool() {
526
+ return this.uint32() !== 0;
527
+ }
528
+ /**
529
+ * Reads fixed 32 bits as an unsigned 32 bit integer
530
+ */
531
+ fixed32() {
532
+ if (this.pos + 4 > this.len) {
533
+ throw indexOutOfRange(this, 4);
534
+ }
535
+ const res = readFixed32End(this.buf, this.pos += 4);
536
+ return res;
537
+ }
538
+ /**
539
+ * Reads fixed 32 bits as a signed 32 bit integer
540
+ */
541
+ sfixed32() {
542
+ if (this.pos + 4 > this.len) {
543
+ throw indexOutOfRange(this, 4);
544
+ }
545
+ const res = readFixed32End(this.buf, this.pos += 4) | 0;
546
+ return res;
547
+ }
548
+ /**
549
+ * Reads a float (32 bit) as a number
550
+ */
551
+ float() {
552
+ if (this.pos + 4 > this.len) {
553
+ throw indexOutOfRange(this, 4);
554
+ }
555
+ const value = readFloatLE(this.buf, this.pos);
556
+ this.pos += 4;
557
+ return value;
558
+ }
559
+ /**
560
+ * Reads a double (64 bit float) as a number
561
+ */
562
+ double() {
563
+ /* istanbul ignore if */
564
+ if (this.pos + 8 > this.len) {
565
+ throw indexOutOfRange(this, 4);
566
+ }
567
+ const value = readDoubleLE(this.buf, this.pos);
568
+ this.pos += 8;
569
+ return value;
570
+ }
571
+ /**
572
+ * Reads a sequence of bytes preceded by its length as a varint
573
+ */
574
+ bytes() {
575
+ const length = this.uint32();
576
+ const start = this.pos;
577
+ const end = this.pos + length;
578
+ /* istanbul ignore if */
579
+ if (end > this.len) {
580
+ throw indexOutOfRange(this, length);
581
+ }
582
+ this.pos += length;
583
+ return start === end // fix for IE 10/Win8 and others' subarray returning array of size 1
584
+ ? new Uint8Array(0)
585
+ : this.buf.subarray(start, end);
586
+ }
587
+ /**
588
+ * Reads a string preceded by its byte length as a varint
589
+ */
590
+ string() {
591
+ const bytes = this.bytes();
592
+ return read(bytes, 0, bytes.length);
593
+ }
594
+ /**
595
+ * Skips the specified number of bytes if specified, otherwise skips a varint
596
+ */
597
+ skip(length) {
598
+ if (typeof length === 'number') {
599
+ /* istanbul ignore if */
600
+ if (this.pos + length > this.len) {
601
+ throw indexOutOfRange(this, length);
602
+ }
603
+ this.pos += length;
604
+ }
605
+ else {
606
+ do {
607
+ /* istanbul ignore if */
608
+ if (this.pos >= this.len) {
609
+ throw indexOutOfRange(this);
610
+ }
611
+ } while ((this.buf[this.pos++] & 128) !== 0);
612
+ }
613
+ return this;
614
+ }
1528
615
  /**
1529
- * Read buffer length.
1530
- * @type {number}
616
+ * Skips the next element of the specified wire type
1531
617
  */
1532
- this.len = buffer.length;
1533
- }
1534
-
1535
- var create_array = typeof Uint8Array !== "undefined"
1536
- ? function create_typed_array(buffer) {
1537
- if (buffer instanceof Uint8Array || Array.isArray(buffer))
1538
- return new Reader$1(buffer);
1539
- throw Error("illegal buffer");
1540
- }
1541
- /* istanbul ignore next */
1542
- : function create_array(buffer) {
1543
- if (Array.isArray(buffer))
1544
- return new Reader$1(buffer);
1545
- throw Error("illegal buffer");
1546
- };
1547
-
1548
- var create$1 = function create() {
1549
- return util$4.Buffer
1550
- ? function create_buffer_setup(buffer) {
1551
- return (Reader$1.create = function create_buffer(buffer) {
1552
- return util$4.Buffer.isBuffer(buffer)
1553
- ? new BufferReader$1(buffer)
1554
- /* istanbul ignore next */
1555
- : create_array(buffer);
1556
- })(buffer);
1557
- }
1558
- /* istanbul ignore next */
1559
- : create_array;
1560
- };
1561
-
1562
- /**
1563
- * Creates a new reader using the specified buffer.
1564
- * @function
1565
- * @param {Uint8Array|Buffer} buffer Buffer to read from
1566
- * @returns {Reader|BufferReader} A {@link BufferReader} if `buffer` is a Buffer, otherwise a {@link Reader}
1567
- * @throws {Error} If `buffer` is not a valid buffer
1568
- */
1569
- Reader$1.create = create$1();
1570
-
1571
- Reader$1.prototype._slice = util$4.Array.prototype.subarray || /* istanbul ignore next */ util$4.Array.prototype.slice;
1572
-
1573
- /**
1574
- * Reads a varint as an unsigned 32 bit value.
1575
- * @function
1576
- * @returns {number} Value read
1577
- */
1578
- Reader$1.prototype.uint32 = (function read_uint32_setup() {
1579
- var value = 4294967295; // optimizer type-hint, tends to deopt otherwise (?!)
1580
- return function read_uint32() {
1581
- value = ( this.buf[this.pos] & 127 ) >>> 0; if (this.buf[this.pos++] < 128) return value;
1582
- value = (value | (this.buf[this.pos] & 127) << 7) >>> 0; if (this.buf[this.pos++] < 128) return value;
1583
- value = (value | (this.buf[this.pos] & 127) << 14) >>> 0; if (this.buf[this.pos++] < 128) return value;
1584
- value = (value | (this.buf[this.pos] & 127) << 21) >>> 0; if (this.buf[this.pos++] < 128) return value;
1585
- value = (value | (this.buf[this.pos] & 15) << 28) >>> 0; if (this.buf[this.pos++] < 128) return value;
1586
-
1587
- /* istanbul ignore if */
1588
- if ((this.pos += 5) > this.len) {
1589
- this.pos = this.len;
1590
- throw indexOutOfRange(this, 10);
618
+ skipType(wireType) {
619
+ switch (wireType) {
620
+ case 0:
621
+ this.skip();
622
+ break;
623
+ case 1:
624
+ this.skip(8);
625
+ break;
626
+ case 2:
627
+ this.skip(this.uint32());
628
+ break;
629
+ case 3:
630
+ while ((wireType = this.uint32() & 7) !== 4) {
631
+ this.skipType(wireType);
632
+ }
633
+ break;
634
+ case 5:
635
+ this.skip(4);
636
+ break;
637
+ /* istanbul ignore next */
638
+ default:
639
+ throw Error(`invalid wire type ${wireType} at offset ${this.pos}`);
1591
640
  }
1592
- return value;
1593
- };
1594
- })();
1595
-
1596
- /**
1597
- * Reads a varint as a signed 32 bit value.
1598
- * @returns {number} Value read
1599
- */
1600
- Reader$1.prototype.int32 = function read_int32() {
1601
- return this.uint32() | 0;
1602
- };
1603
-
1604
- /**
1605
- * Reads a zig-zag encoded varint as a signed 32 bit value.
1606
- * @returns {number} Value read
1607
- */
1608
- Reader$1.prototype.sint32 = function read_sint32() {
1609
- var value = this.uint32();
1610
- return value >>> 1 ^ -(value & 1) | 0;
1611
- };
1612
-
1613
- /* eslint-disable no-invalid-this */
1614
-
1615
- function readLongVarint() {
1616
- // tends to deopt with local vars for octet etc.
1617
- var bits = new LongBits$1(0, 0);
1618
- var i = 0;
1619
- if (this.len - this.pos > 4) { // fast route (lo)
1620
- for (; i < 4; ++i) {
1621
- // 1st..4th
1622
- bits.lo = (bits.lo | (this.buf[this.pos] & 127) << i * 7) >>> 0;
1623
- if (this.buf[this.pos++] < 128)
641
+ return this;
642
+ }
643
+ readLongVarint() {
644
+ // tends to deopt with local vars for octet etc.
645
+ const bits = new LongBits(0, 0);
646
+ let i = 0;
647
+ if (this.len - this.pos > 4) { // fast route (lo)
648
+ for (; i < 4; ++i) {
649
+ // 1st..4th
650
+ bits.lo = (bits.lo | (this.buf[this.pos] & 127) << i * 7) >>> 0;
651
+ if (this.buf[this.pos++] < 128) {
652
+ return bits;
653
+ }
654
+ }
655
+ // 5th
656
+ bits.lo = (bits.lo | (this.buf[this.pos] & 127) << 28) >>> 0;
657
+ bits.hi = (bits.hi | (this.buf[this.pos] & 127) >> 4) >>> 0;
658
+ if (this.buf[this.pos++] < 128) {
1624
659
  return bits;
660
+ }
661
+ i = 0;
1625
662
  }
1626
- // 5th
1627
- bits.lo = (bits.lo | (this.buf[this.pos] & 127) << 28) >>> 0;
1628
- bits.hi = (bits.hi | (this.buf[this.pos] & 127) >> 4) >>> 0;
1629
- if (this.buf[this.pos++] < 128)
663
+ else {
664
+ for (; i < 3; ++i) {
665
+ /* istanbul ignore if */
666
+ if (this.pos >= this.len) {
667
+ throw indexOutOfRange(this);
668
+ }
669
+ // 1st..3th
670
+ bits.lo = (bits.lo | (this.buf[this.pos] & 127) << i * 7) >>> 0;
671
+ if (this.buf[this.pos++] < 128) {
672
+ return bits;
673
+ }
674
+ }
675
+ // 4th
676
+ bits.lo = (bits.lo | (this.buf[this.pos++] & 127) << i * 7) >>> 0;
1630
677
  return bits;
1631
- i = 0;
1632
- } else {
1633
- for (; i < 3; ++i) {
1634
- /* istanbul ignore if */
1635
- if (this.pos >= this.len)
1636
- throw indexOutOfRange(this);
1637
- // 1st..3th
1638
- bits.lo = (bits.lo | (this.buf[this.pos] & 127) << i * 7) >>> 0;
1639
- if (this.buf[this.pos++] < 128)
1640
- return bits;
1641
678
  }
1642
- // 4th
1643
- bits.lo = (bits.lo | (this.buf[this.pos++] & 127) << i * 7) >>> 0;
1644
- return bits;
1645
- }
1646
- if (this.len - this.pos > 4) { // fast route (hi)
1647
- for (; i < 5; ++i) {
1648
- // 6th..10th
1649
- bits.hi = (bits.hi | (this.buf[this.pos] & 127) << i * 7 + 3) >>> 0;
1650
- if (this.buf[this.pos++] < 128)
1651
- return bits;
679
+ if (this.len - this.pos > 4) { // fast route (hi)
680
+ for (; i < 5; ++i) {
681
+ // 6th..10th
682
+ bits.hi = (bits.hi | (this.buf[this.pos] & 127) << i * 7 + 3) >>> 0;
683
+ if (this.buf[this.pos++] < 128) {
684
+ return bits;
685
+ }
686
+ }
1652
687
  }
1653
- } else {
1654
- for (; i < 5; ++i) {
1655
- /* istanbul ignore if */
1656
- if (this.pos >= this.len)
1657
- throw indexOutOfRange(this);
1658
- // 6th..10th
1659
- bits.hi = (bits.hi | (this.buf[this.pos] & 127) << i * 7 + 3) >>> 0;
1660
- if (this.buf[this.pos++] < 128)
1661
- return bits;
688
+ else {
689
+ for (; i < 5; ++i) {
690
+ if (this.pos >= this.len) {
691
+ throw indexOutOfRange(this);
692
+ }
693
+ // 6th..10th
694
+ bits.hi = (bits.hi | (this.buf[this.pos] & 127) << i * 7 + 3) >>> 0;
695
+ if (this.buf[this.pos++] < 128) {
696
+ return bits;
697
+ }
698
+ }
699
+ }
700
+ throw Error('invalid varint encoding');
701
+ }
702
+ readFixed64() {
703
+ if (this.pos + 8 > this.len) {
704
+ throw indexOutOfRange(this, 8);
1662
705
  }
706
+ const lo = readFixed32End(this.buf, this.pos += 4);
707
+ const hi = readFixed32End(this.buf, this.pos += 4);
708
+ return new LongBits(lo, hi);
709
+ }
710
+ /**
711
+ * Reads a varint as a signed 64 bit value
712
+ */
713
+ int64() {
714
+ return this.readLongVarint().toBigInt();
715
+ }
716
+ /**
717
+ * Reads a varint as a signed 64 bit value returned as a possibly unsafe
718
+ * JavaScript number
719
+ */
720
+ int64Number() {
721
+ return this.readLongVarint().toNumber();
722
+ }
723
+ /**
724
+ * Reads a varint as a signed 64 bit value returned as a string
725
+ */
726
+ int64String() {
727
+ return this.readLongVarint().toString();
728
+ }
729
+ /**
730
+ * Reads a varint as an unsigned 64 bit value
731
+ */
732
+ uint64() {
733
+ return this.readLongVarint().toBigInt(true);
734
+ }
735
+ /**
736
+ * Reads a varint as an unsigned 64 bit value returned as a possibly unsafe
737
+ * JavaScript number
738
+ */
739
+ uint64Number() {
740
+ const value = decodeUint8Array(this.buf, this.pos);
741
+ this.pos += encodingLength(value);
742
+ return value;
743
+ }
744
+ /**
745
+ * Reads a varint as an unsigned 64 bit value returned as a string
746
+ */
747
+ uint64String() {
748
+ return this.readLongVarint().toString(true);
749
+ }
750
+ /**
751
+ * Reads a zig-zag encoded varint as a signed 64 bit value
752
+ */
753
+ sint64() {
754
+ return this.readLongVarint().zzDecode().toBigInt();
755
+ }
756
+ /**
757
+ * Reads a zig-zag encoded varint as a signed 64 bit value returned as a
758
+ * possibly unsafe JavaScript number
759
+ */
760
+ sint64Number() {
761
+ return this.readLongVarint().zzDecode().toNumber();
762
+ }
763
+ /**
764
+ * Reads a zig-zag encoded varint as a signed 64 bit value returned as a
765
+ * string
766
+ */
767
+ sint64String() {
768
+ return this.readLongVarint().zzDecode().toString();
769
+ }
770
+ /**
771
+ * Reads fixed 64 bits
772
+ */
773
+ fixed64() {
774
+ return this.readFixed64().toBigInt();
775
+ }
776
+ /**
777
+ * Reads fixed 64 bits returned as a possibly unsafe JavaScript number
778
+ */
779
+ fixed64Number() {
780
+ return this.readFixed64().toNumber();
781
+ }
782
+ /**
783
+ * Reads fixed 64 bits returned as a string
784
+ */
785
+ fixed64String() {
786
+ return this.readFixed64().toString();
787
+ }
788
+ /**
789
+ * Reads zig-zag encoded fixed 64 bits
790
+ */
791
+ sfixed64() {
792
+ return this.readFixed64().toBigInt();
793
+ }
794
+ /**
795
+ * Reads zig-zag encoded fixed 64 bits returned as a possibly unsafe
796
+ * JavaScript number
797
+ */
798
+ sfixed64Number() {
799
+ return this.readFixed64().toNumber();
800
+ }
801
+ /**
802
+ * Reads zig-zag encoded fixed 64 bits returned as a string
803
+ */
804
+ sfixed64String() {
805
+ return this.readFixed64().toString();
1663
806
  }
1664
- /* istanbul ignore next */
1665
- throw Error("invalid varint encoding");
1666
807
  }
1667
-
1668
- /* eslint-enable no-invalid-this */
1669
-
1670
- /**
1671
- * Reads a varint as a signed 64 bit value.
1672
- * @name Reader#int64
1673
- * @function
1674
- * @returns {Long} Value read
1675
- */
1676
-
1677
- /**
1678
- * Reads a varint as an unsigned 64 bit value.
1679
- * @name Reader#uint64
1680
- * @function
1681
- * @returns {Long} Value read
1682
- */
1683
-
1684
- /**
1685
- * Reads a zig-zag encoded varint as a signed 64 bit value.
1686
- * @name Reader#sint64
1687
- * @function
1688
- * @returns {Long} Value read
1689
- */
1690
-
1691
- /**
1692
- * Reads a varint as a boolean.
1693
- * @returns {boolean} Value read
1694
- */
1695
- Reader$1.prototype.bool = function read_bool() {
1696
- return this.uint32() !== 0;
1697
- };
1698
-
1699
- function readFixed32_end(buf, end) { // note that this uses `end`, not `pos`
1700
- return (buf[end - 4]
1701
- | buf[end - 3] << 8
1702
- | buf[end - 2] << 16
1703
- | buf[end - 1] << 24) >>> 0;
808
+ function createReader(buf) {
809
+ return new Uint8ArrayReader(buf instanceof Uint8Array ? buf : buf.subarray());
1704
810
  }
1705
811
 
1706
- /**
1707
- * Reads fixed 32 bits as an unsigned 32 bit integer.
1708
- * @returns {number} Value read
1709
- */
1710
- Reader$1.prototype.fixed32 = function read_fixed32() {
1711
-
1712
- /* istanbul ignore if */
1713
- if (this.pos + 4 > this.len)
1714
- throw indexOutOfRange(this, 4);
1715
-
1716
- return readFixed32_end(this.buf, this.pos += 4);
1717
- };
1718
-
1719
- /**
1720
- * Reads fixed 32 bits as a signed 32 bit integer.
1721
- * @returns {number} Value read
1722
- */
1723
- Reader$1.prototype.sfixed32 = function read_sfixed32() {
1724
-
1725
- /* istanbul ignore if */
1726
- if (this.pos + 4 > this.len)
1727
- throw indexOutOfRange(this, 4);
1728
-
1729
- return readFixed32_end(this.buf, this.pos += 4) | 0;
1730
- };
1731
-
1732
- /* eslint-disable no-invalid-this */
1733
-
1734
- function readFixed64(/* this: Reader */) {
1735
-
1736
- /* istanbul ignore if */
1737
- if (this.pos + 8 > this.len)
1738
- throw indexOutOfRange(this, 8);
1739
-
1740
- return new LongBits$1(readFixed32_end(this.buf, this.pos += 4), readFixed32_end(this.buf, this.pos += 4));
812
+ function decodeMessage(buf, codec, opts) {
813
+ const reader = createReader(buf);
814
+ return codec.decode(reader, undefined, opts);
1741
815
  }
1742
816
 
1743
- /* eslint-enable no-invalid-this */
1744
-
1745
- /**
1746
- * Reads fixed 64 bits.
1747
- * @name Reader#fixed64
1748
- * @function
1749
- * @returns {Long} Value read
1750
- */
1751
-
1752
- /**
1753
- * Reads zig-zag encoded fixed 64 bits.
1754
- * @name Reader#sfixed64
1755
- * @function
1756
- * @returns {Long} Value read
1757
- */
1758
-
1759
- /**
1760
- * Reads a float (32 bit) as a number.
1761
- * @function
1762
- * @returns {number} Value read
1763
- */
1764
- Reader$1.prototype.float = function read_float() {
1765
-
1766
- /* istanbul ignore if */
1767
- if (this.pos + 4 > this.len)
1768
- throw indexOutOfRange(this, 4);
1769
-
1770
- var value = util$4.float.readFloatLE(this.buf, this.pos);
1771
- this.pos += 4;
1772
- return value;
1773
- };
1774
-
1775
817
  /**
1776
- * Reads a double (64 bit float) as a number.
1777
- * @function
1778
- * @returns {number} Value read
818
+ * A general purpose buffer pool
1779
819
  */
1780
- Reader$1.prototype.double = function read_double() {
1781
-
1782
- /* istanbul ignore if */
1783
- if (this.pos + 8 > this.len)
1784
- throw indexOutOfRange(this, 4);
1785
-
1786
- var value = util$4.float.readDoubleLE(this.buf, this.pos);
1787
- this.pos += 8;
1788
- return value;
1789
- };
820
+ function pool(size) {
821
+ const SIZE = size ?? 8192;
822
+ const MAX = SIZE >>> 1;
823
+ let slab;
824
+ let offset = SIZE;
825
+ return function poolAlloc(size) {
826
+ if (size < 1 || size > MAX) {
827
+ return allocUnsafe(size);
828
+ }
829
+ if (offset + size > SIZE) {
830
+ slab = allocUnsafe(SIZE);
831
+ offset = 0;
832
+ }
833
+ const buf = slab.subarray(offset, offset += size);
834
+ if ((offset & 7) !== 0) {
835
+ // align to 32 bit
836
+ offset = (offset | 7) + 1;
837
+ }
838
+ return buf;
839
+ };
840
+ }
1790
841
 
1791
842
  /**
1792
- * Reads a sequence of bytes preceeded by its length as a varint.
1793
- * @returns {Uint8Array} Value read
843
+ * Constructs a new writer operation instance.
844
+ *
845
+ * @classdesc Scheduled writer operation
1794
846
  */
1795
- Reader$1.prototype.bytes = function read_bytes() {
1796
- var length = this.uint32(),
1797
- start = this.pos,
1798
- end = this.pos + length;
1799
-
1800
- /* istanbul ignore if */
1801
- if (end > this.len)
1802
- throw indexOutOfRange(this, length);
1803
-
1804
- this.pos += length;
1805
- if (Array.isArray(this.buf)) // plain array
1806
- return this.buf.slice(start, end);
1807
-
1808
- if (start === end) { // fix for IE 10/Win8 and others' subarray returning array of size 1
1809
- var nativeBuffer = util$4.Buffer;
1810
- return nativeBuffer
1811
- ? nativeBuffer.alloc(0)
1812
- : new this.buf.constructor(0);
847
+ class Op {
848
+ /**
849
+ * Function to call
850
+ */
851
+ fn;
852
+ /**
853
+ * Value byte length
854
+ */
855
+ len;
856
+ /**
857
+ * Next operation
858
+ */
859
+ next;
860
+ /**
861
+ * Value to write
862
+ */
863
+ val;
864
+ constructor(fn, len, val) {
865
+ this.fn = fn;
866
+ this.len = len;
867
+ this.next = undefined;
868
+ this.val = val; // type varies
1813
869
  }
1814
- return this._slice.call(this.buf, start, end);
1815
- };
1816
-
870
+ }
871
+ /* istanbul ignore next */
872
+ function noop() { } // eslint-disable-line no-empty-function
1817
873
  /**
1818
- * Reads a string preceeded by its byte length as a varint.
1819
- * @returns {string} Value read
874
+ * Constructs a new writer state instance
1820
875
  */
1821
- Reader$1.prototype.string = function read_string() {
1822
- var bytes = this.bytes();
1823
- return utf8$1.read(bytes, 0, bytes.length);
1824
- };
1825
-
876
+ class State {
877
+ /**
878
+ * Current head
879
+ */
880
+ head;
881
+ /**
882
+ * Current tail
883
+ */
884
+ tail;
885
+ /**
886
+ * Current buffer length
887
+ */
888
+ len;
889
+ /**
890
+ * Next state
891
+ */
892
+ next;
893
+ constructor(writer) {
894
+ this.head = writer.head;
895
+ this.tail = writer.tail;
896
+ this.len = writer.len;
897
+ this.next = writer.states;
898
+ }
899
+ }
900
+ const bufferPool = pool();
1826
901
  /**
1827
- * Skips the specified number of bytes if specified, otherwise skips a varint.
1828
- * @param {number} [length] Length if known, otherwise a varint is assumed
1829
- * @returns {Reader} `this`
902
+ * Allocates a buffer of the specified size
1830
903
  */
1831
- Reader$1.prototype.skip = function skip(length) {
1832
- if (typeof length === "number") {
1833
- /* istanbul ignore if */
1834
- if (this.pos + length > this.len)
1835
- throw indexOutOfRange(this, length);
1836
- this.pos += length;
1837
- } else {
1838
- do {
1839
- /* istanbul ignore if */
1840
- if (this.pos >= this.len)
1841
- throw indexOutOfRange(this);
1842
- } while (this.buf[this.pos++] & 128);
904
+ function alloc(size) {
905
+ if (globalThis.Buffer != null) {
906
+ return allocUnsafe(size);
1843
907
  }
1844
- return this;
1845
- };
1846
-
908
+ return bufferPool(size);
909
+ }
1847
910
  /**
1848
- * Skips the next element of the specified wire type.
1849
- * @param {number} wireType Wire type received
1850
- * @returns {Reader} `this`
911
+ * When a value is written, the writer calculates its byte length and puts it into a linked
912
+ * list of operations to perform when finish() is called. This both allows us to allocate
913
+ * buffers of the exact required size and reduces the amount of work we have to do compared
914
+ * to first calculating over objects and then encoding over objects. In our case, the encoding
915
+ * part is just a linked list walk calling operations with already prepared values.
1851
916
  */
1852
- Reader$1.prototype.skipType = function(wireType) {
1853
- switch (wireType) {
1854
- case 0:
1855
- this.skip();
1856
- break;
1857
- case 1:
1858
- this.skip(8);
1859
- break;
1860
- case 2:
1861
- this.skip(this.uint32());
1862
- break;
1863
- case 3:
1864
- while ((wireType = this.uint32() & 7) !== 4) {
1865
- this.skipType(wireType);
1866
- }
1867
- break;
1868
- case 5:
1869
- this.skip(4);
1870
- break;
1871
-
1872
- /* istanbul ignore next */
1873
- default:
1874
- throw Error("invalid wire type " + wireType + " at offset " + this.pos);
917
+ class Uint8ArrayWriter {
918
+ /**
919
+ * Current length
920
+ */
921
+ len;
922
+ /**
923
+ * Operations head
924
+ */
925
+ head;
926
+ /**
927
+ * Operations tail
928
+ */
929
+ tail;
930
+ /**
931
+ * Linked forked states
932
+ */
933
+ states;
934
+ constructor() {
935
+ this.len = 0;
936
+ this.head = new Op(noop, 0, 0);
937
+ this.tail = this.head;
938
+ this.states = null;
939
+ }
940
+ /**
941
+ * Pushes a new operation to the queue
942
+ */
943
+ _push(fn, len, val) {
944
+ this.tail = this.tail.next = new Op(fn, len, val);
945
+ this.len += len;
946
+ return this;
947
+ }
948
+ /**
949
+ * Writes an unsigned 32 bit value as a varint
950
+ */
951
+ uint32(value) {
952
+ // here, the call to this.push has been inlined and a varint specific Op subclass is used.
953
+ // uint32 is by far the most frequently used operation and benefits significantly from this.
954
+ this.len += (this.tail = this.tail.next = new VarintOp((value = value >>> 0) <
955
+ 128
956
+ ? 1
957
+ : value < 16384
958
+ ? 2
959
+ : value < 2097152
960
+ ? 3
961
+ : value < 268435456
962
+ ? 4
963
+ : 5, value)).len;
964
+ return this;
965
+ }
966
+ /**
967
+ * Writes a signed 32 bit value as a varint`
968
+ */
969
+ int32(value) {
970
+ return value < 0
971
+ ? this._push(writeVarint64, 10, LongBits.fromNumber(value)) // 10 bytes per spec
972
+ : this.uint32(value);
973
+ }
974
+ /**
975
+ * Writes a 32 bit value as a varint, zig-zag encoded
976
+ */
977
+ sint32(value) {
978
+ return this.uint32((value << 1 ^ value >> 31) >>> 0);
979
+ }
980
+ /**
981
+ * Writes an unsigned 64 bit value as a varint
982
+ */
983
+ uint64(value) {
984
+ const bits = LongBits.fromBigInt(value);
985
+ return this._push(writeVarint64, bits.length(), bits);
986
+ }
987
+ /**
988
+ * Writes an unsigned 64 bit value as a varint
989
+ */
990
+ uint64Number(value) {
991
+ return this._push(encodeUint8Array, encodingLength(value), value);
992
+ }
993
+ /**
994
+ * Writes an unsigned 64 bit value as a varint
995
+ */
996
+ uint64String(value) {
997
+ return this.uint64(BigInt(value));
998
+ }
999
+ /**
1000
+ * Writes a signed 64 bit value as a varint
1001
+ */
1002
+ int64(value) {
1003
+ return this.uint64(value);
1004
+ }
1005
+ /**
1006
+ * Writes a signed 64 bit value as a varint
1007
+ */
1008
+ int64Number(value) {
1009
+ return this.uint64Number(value);
1010
+ }
1011
+ /**
1012
+ * Writes a signed 64 bit value as a varint
1013
+ */
1014
+ int64String(value) {
1015
+ return this.uint64String(value);
1016
+ }
1017
+ /**
1018
+ * Writes a signed 64 bit value as a varint, zig-zag encoded
1019
+ */
1020
+ sint64(value) {
1021
+ const bits = LongBits.fromBigInt(value).zzEncode();
1022
+ return this._push(writeVarint64, bits.length(), bits);
1023
+ }
1024
+ /**
1025
+ * Writes a signed 64 bit value as a varint, zig-zag encoded
1026
+ */
1027
+ sint64Number(value) {
1028
+ const bits = LongBits.fromNumber(value).zzEncode();
1029
+ return this._push(writeVarint64, bits.length(), bits);
1030
+ }
1031
+ /**
1032
+ * Writes a signed 64 bit value as a varint, zig-zag encoded
1033
+ */
1034
+ sint64String(value) {
1035
+ return this.sint64(BigInt(value));
1036
+ }
1037
+ /**
1038
+ * Writes a boolish value as a varint
1039
+ */
1040
+ bool(value) {
1041
+ return this._push(writeByte, 1, value ? 1 : 0);
1042
+ }
1043
+ /**
1044
+ * Writes an unsigned 32 bit value as fixed 32 bits
1045
+ */
1046
+ fixed32(value) {
1047
+ return this._push(writeFixed32, 4, value >>> 0);
1048
+ }
1049
+ /**
1050
+ * Writes a signed 32 bit value as fixed 32 bits
1051
+ */
1052
+ sfixed32(value) {
1053
+ return this.fixed32(value);
1875
1054
  }
1876
- return this;
1877
- };
1878
-
1879
- Reader$1._configure = function(BufferReader_) {
1880
- BufferReader$1 = BufferReader_;
1881
- Reader$1.create = create$1();
1882
- BufferReader$1._configure();
1883
-
1884
- var fn = util$4.Long ? "toLong" : /* istanbul ignore next */ "toNumber";
1885
- util$4.merge(Reader$1.prototype, {
1886
-
1887
- int64: function read_int64() {
1888
- return readLongVarint.call(this)[fn](false);
1889
- },
1890
-
1891
- uint64: function read_uint64() {
1892
- return readLongVarint.call(this)[fn](true);
1893
- },
1894
-
1895
- sint64: function read_sint64() {
1896
- return readLongVarint.call(this).zzDecode()[fn](false);
1897
- },
1898
-
1899
- fixed64: function read_fixed64() {
1900
- return readFixed64.call(this)[fn](true);
1901
- },
1902
-
1903
- sfixed64: function read_sfixed64() {
1904
- return readFixed64.call(this)[fn](false);
1905
- }
1906
-
1907
- });
1908
- };
1909
-
1910
- var ReaderClass = /*@__PURE__*/getDefaultExportFromCjs(reader$1);
1911
-
1912
- var reader_buffer = BufferReader;
1913
-
1914
- // extends Reader
1915
- var Reader = reader$1;
1916
- (BufferReader.prototype = Object.create(Reader.prototype)).constructor = BufferReader;
1917
-
1918
- var util$3 = requireMinimal();
1919
-
1920
- /**
1921
- * Constructs a new buffer reader instance.
1922
- * @classdesc Wire format reader using node buffers.
1923
- * @extends Reader
1924
- * @constructor
1925
- * @param {Buffer} buffer Buffer to read from
1926
- */
1927
- function BufferReader(buffer) {
1928
- Reader.call(this, buffer);
1929
-
1930
1055
  /**
1931
- * Read buffer.
1932
- * @name BufferReader#buf
1933
- * @type {Buffer}
1056
+ * Writes an unsigned 64 bit value as fixed 64 bits
1934
1057
  */
1935
- }
1936
-
1937
- BufferReader._configure = function () {
1938
- /* istanbul ignore else */
1939
- if (util$3.Buffer)
1940
- BufferReader.prototype._slice = util$3.Buffer.prototype.slice;
1941
- };
1942
-
1943
-
1944
- /**
1945
- * @override
1946
- */
1947
- BufferReader.prototype.string = function read_string_buffer() {
1948
- var len = this.uint32(); // modifies pos
1949
- return this.buf.utf8Slice
1950
- ? this.buf.utf8Slice(this.pos, this.pos = Math.min(this.pos + len, this.len))
1951
- : this.buf.toString("utf-8", this.pos, this.pos = Math.min(this.pos + len, this.len));
1952
- };
1953
-
1954
- /**
1955
- * Reads a sequence of bytes preceeded by its length as a varint.
1956
- * @name BufferReader#bytes
1957
- * @function
1958
- * @returns {Buffer} Value read
1959
- */
1960
-
1961
- BufferReader._configure();
1962
-
1963
- var ReaderBufferClass = /*@__PURE__*/getDefaultExportFromCjs(reader_buffer);
1964
-
1965
- var minimalExports = requireMinimal();
1966
- var util$2 = /*@__PURE__*/getDefaultExportFromCjs(minimalExports);
1967
-
1968
- var writer$1 = Writer$1;
1969
-
1970
- var util$1 = requireMinimal();
1971
-
1972
- var BufferWriter$1; // cyclic
1973
-
1974
- var LongBits = util$1.LongBits,
1975
- base64 = util$1.base64,
1976
- utf8 = util$1.utf8;
1977
-
1978
- /**
1979
- * Constructs a new writer operation instance.
1980
- * @classdesc Scheduled writer operation.
1981
- * @constructor
1982
- * @param {function(*, Uint8Array, number)} fn Function to call
1983
- * @param {number} len Value byte length
1984
- * @param {*} val Value to write
1985
- * @ignore
1986
- */
1987
- function Op(fn, len, val) {
1988
-
1058
+ fixed64(value) {
1059
+ const bits = LongBits.fromBigInt(value);
1060
+ return this._push(writeFixed32, 4, bits.lo)._push(writeFixed32, 4, bits.hi);
1061
+ }
1989
1062
  /**
1990
- * Function to call.
1991
- * @type {function(Uint8Array, number, *)}
1063
+ * Writes an unsigned 64 bit value as fixed 64 bits
1992
1064
  */
1993
- this.fn = fn;
1994
-
1065
+ fixed64Number(value) {
1066
+ const bits = LongBits.fromNumber(value);
1067
+ return this._push(writeFixed32, 4, bits.lo)._push(writeFixed32, 4, bits.hi);
1068
+ }
1995
1069
  /**
1996
- * Value byte length.
1997
- * @type {number}
1070
+ * Writes an unsigned 64 bit value as fixed 64 bits
1998
1071
  */
1999
- this.len = len;
2000
-
1072
+ fixed64String(value) {
1073
+ return this.fixed64(BigInt(value));
1074
+ }
2001
1075
  /**
2002
- * Next operation.
2003
- * @type {Writer.Op|undefined}
1076
+ * Writes a signed 64 bit value as fixed 64 bits
2004
1077
  */
2005
- this.next = undefined;
2006
-
1078
+ sfixed64(value) {
1079
+ return this.fixed64(value);
1080
+ }
2007
1081
  /**
2008
- * Value to write.
2009
- * @type {*}
1082
+ * Writes a signed 64 bit value as fixed 64 bits
2010
1083
  */
2011
- this.val = val; // type varies
2012
- }
2013
-
2014
- /* istanbul ignore next */
2015
- function noop() {} // eslint-disable-line no-empty-function
2016
-
2017
- /**
2018
- * Constructs a new writer state instance.
2019
- * @classdesc Copied writer state.
2020
- * @memberof Writer
2021
- * @constructor
2022
- * @param {Writer} writer Writer to copy state from
2023
- * @ignore
2024
- */
2025
- function State(writer) {
2026
-
1084
+ sfixed64Number(value) {
1085
+ return this.fixed64Number(value);
1086
+ }
2027
1087
  /**
2028
- * Current head.
2029
- * @type {Writer.Op}
1088
+ * Writes a signed 64 bit value as fixed 64 bits
2030
1089
  */
2031
- this.head = writer.head;
2032
-
1090
+ sfixed64String(value) {
1091
+ return this.fixed64String(value);
1092
+ }
2033
1093
  /**
2034
- * Current tail.
2035
- * @type {Writer.Op}
1094
+ * Writes a float (32 bit)
2036
1095
  */
2037
- this.tail = writer.tail;
2038
-
1096
+ float(value) {
1097
+ return this._push(writeFloatLE, 4, value);
1098
+ }
1099
+ /**
1100
+ * Writes a double (64 bit float).
1101
+ *
1102
+ * @function
1103
+ * @param {number} value - Value to write
1104
+ * @returns {Writer} `this`
1105
+ */
1106
+ double(value) {
1107
+ return this._push(writeDoubleLE, 8, value);
1108
+ }
2039
1109
  /**
2040
- * Current buffer length.
2041
- * @type {number}
1110
+ * Writes a sequence of bytes
2042
1111
  */
2043
- this.len = writer.len;
2044
-
1112
+ bytes(value) {
1113
+ const len = value.length >>> 0;
1114
+ if (len === 0) {
1115
+ return this._push(writeByte, 1, 0);
1116
+ }
1117
+ return this.uint32(len)._push(writeBytes, len, value);
1118
+ }
2045
1119
  /**
2046
- * Next state.
2047
- * @type {State|null}
1120
+ * Writes a string
2048
1121
  */
2049
- this.next = writer.states;
2050
- }
2051
-
2052
- /**
2053
- * Constructs a new writer instance.
2054
- * @classdesc Wire format writer using `Uint8Array` if available, otherwise `Array`.
2055
- * @constructor
2056
- */
2057
- function Writer$1() {
2058
-
1122
+ string(value) {
1123
+ const len = length(value);
1124
+ return len !== 0
1125
+ ? this.uint32(len)._push(write, len, value)
1126
+ : this._push(writeByte, 1, 0);
1127
+ }
2059
1128
  /**
2060
- * Current length.
2061
- * @type {number}
1129
+ * Forks this writer's state by pushing it to a stack.
1130
+ * Calling {@link Writer#reset|reset} or {@link Writer#ldelim|ldelim} resets the writer to the previous state.
2062
1131
  */
2063
- this.len = 0;
2064
-
1132
+ fork() {
1133
+ this.states = new State(this);
1134
+ this.head = this.tail = new Op(noop, 0, 0);
1135
+ this.len = 0;
1136
+ return this;
1137
+ }
2065
1138
  /**
2066
- * Operations head.
2067
- * @type {Object}
1139
+ * Resets this instance to the last state
2068
1140
  */
2069
- this.head = new Op(noop, 0, 0);
2070
-
1141
+ reset() {
1142
+ if (this.states != null) {
1143
+ this.head = this.states.head;
1144
+ this.tail = this.states.tail;
1145
+ this.len = this.states.len;
1146
+ this.states = this.states.next;
1147
+ }
1148
+ else {
1149
+ this.head = this.tail = new Op(noop, 0, 0);
1150
+ this.len = 0;
1151
+ }
1152
+ return this;
1153
+ }
2071
1154
  /**
2072
- * Operations tail
2073
- * @type {Object}
1155
+ * Resets to the last state and appends the fork state's current write length as a varint followed by its operations.
2074
1156
  */
2075
- this.tail = this.head;
2076
-
1157
+ ldelim() {
1158
+ const head = this.head;
1159
+ const tail = this.tail;
1160
+ const len = this.len;
1161
+ this.reset().uint32(len);
1162
+ if (len !== 0) {
1163
+ this.tail.next = head.next; // skip noop
1164
+ this.tail = tail;
1165
+ this.len += len;
1166
+ }
1167
+ return this;
1168
+ }
2077
1169
  /**
2078
- * Linked forked states.
2079
- * @type {Object|null}
1170
+ * Finishes the write operation
2080
1171
  */
2081
- this.states = null;
2082
-
2083
- // When a value is written, the writer calculates its byte length and puts it into a linked
2084
- // list of operations to perform when finish() is called. This both allows us to allocate
2085
- // buffers of the exact required size and reduces the amount of work we have to do compared
2086
- // to first calculating over objects and then encoding over objects. In our case, the encoding
2087
- // part is just a linked list walk calling operations with already prepared values.
2088
- }
2089
-
2090
- var create = function create() {
2091
- return util$1.Buffer
2092
- ? function create_buffer_setup() {
2093
- return (Writer$1.create = function create_buffer() {
2094
- return new BufferWriter$1();
2095
- })();
1172
+ finish() {
1173
+ let head = this.head.next; // skip noop
1174
+ const buf = alloc(this.len);
1175
+ let pos = 0;
1176
+ while (head != null) {
1177
+ head.fn(head.val, buf, pos);
1178
+ pos += head.len;
1179
+ head = head.next;
2096
1180
  }
2097
- /* istanbul ignore next */
2098
- : function create_array() {
2099
- return new Writer$1();
2100
- };
2101
- };
2102
-
2103
- /**
2104
- * Creates a new writer.
2105
- * @function
2106
- * @returns {BufferWriter|Writer} A {@link BufferWriter} when Buffers are supported, otherwise a {@link Writer}
2107
- */
2108
- Writer$1.create = create();
2109
-
2110
- /**
2111
- * Allocates a buffer of the specified size.
2112
- * @param {number} size Buffer size
2113
- * @returns {Uint8Array} Buffer
2114
- */
2115
- Writer$1.alloc = function alloc(size) {
2116
- return new util$1.Array(size);
2117
- };
2118
-
2119
- // Use Uint8Array buffer pool in the browser, just like node does with buffers
2120
- /* istanbul ignore else */
2121
- if (util$1.Array !== Array)
2122
- Writer$1.alloc = util$1.pool(Writer$1.alloc, util$1.Array.prototype.subarray);
2123
-
2124
- /**
2125
- * Pushes a new operation to the queue.
2126
- * @param {function(Uint8Array, number, *)} fn Function to call
2127
- * @param {number} len Value byte length
2128
- * @param {number} val Value to write
2129
- * @returns {Writer} `this`
2130
- * @private
2131
- */
2132
- Writer$1.prototype._push = function push(fn, len, val) {
2133
- this.tail = this.tail.next = new Op(fn, len, val);
2134
- this.len += len;
2135
- return this;
2136
- };
2137
-
1181
+ // this.head = this.tail = null;
1182
+ return buf;
1183
+ }
1184
+ }
2138
1185
  function writeByte(val, buf, pos) {
2139
1186
  buf[pos] = val & 255;
2140
1187
  }
2141
-
2142
1188
  function writeVarint32(val, buf, pos) {
2143
1189
  while (val > 127) {
2144
1190
  buf[pos++] = val & 127 | 128;
@@ -2146,67 +1192,20 @@ function writeVarint32(val, buf, pos) {
2146
1192
  }
2147
1193
  buf[pos] = val;
2148
1194
  }
2149
-
2150
1195
  /**
2151
1196
  * Constructs a new varint writer operation instance.
2152
- * @classdesc Scheduled varint writer operation.
2153
- * @extends Op
2154
- * @constructor
2155
- * @param {number} len Value byte length
2156
- * @param {number} val Value to write
2157
- * @ignore
1197
+ *
1198
+ * @classdesc Scheduled varint writer operation
2158
1199
  */
2159
- function VarintOp(len, val) {
2160
- this.len = len;
2161
- this.next = undefined;
2162
- this.val = val;
1200
+ class VarintOp extends Op {
1201
+ next;
1202
+ constructor(len, val) {
1203
+ super(writeVarint32, len, val);
1204
+ this.next = undefined;
1205
+ }
2163
1206
  }
2164
-
2165
- VarintOp.prototype = Object.create(Op.prototype);
2166
- VarintOp.prototype.fn = writeVarint32;
2167
-
2168
- /**
2169
- * Writes an unsigned 32 bit value as a varint.
2170
- * @param {number} value Value to write
2171
- * @returns {Writer} `this`
2172
- */
2173
- Writer$1.prototype.uint32 = function write_uint32(value) {
2174
- // here, the call to this.push has been inlined and a varint specific Op subclass is used.
2175
- // uint32 is by far the most frequently used operation and benefits significantly from this.
2176
- this.len += (this.tail = this.tail.next = new VarintOp(
2177
- (value = value >>> 0)
2178
- < 128 ? 1
2179
- : value < 16384 ? 2
2180
- : value < 2097152 ? 3
2181
- : value < 268435456 ? 4
2182
- : 5,
2183
- value)).len;
2184
- return this;
2185
- };
2186
-
2187
- /**
2188
- * Writes a signed 32 bit value as a varint.
2189
- * @function
2190
- * @param {number} value Value to write
2191
- * @returns {Writer} `this`
2192
- */
2193
- Writer$1.prototype.int32 = function write_int32(value) {
2194
- return value < 0
2195
- ? this._push(writeVarint64, 10, LongBits.fromNumber(value)) // 10 bytes per spec
2196
- : this.uint32(value);
2197
- };
2198
-
2199
- /**
2200
- * Writes a 32 bit value as a varint, zig-zag encoded.
2201
- * @param {number} value Value to write
2202
- * @returns {Writer} `this`
2203
- */
2204
- Writer$1.prototype.sint32 = function write_sint32(value) {
2205
- return this.uint32((value << 1 ^ value >> 31) >>> 0);
2206
- };
2207
-
2208
1207
  function writeVarint64(val, buf, pos) {
2209
- while (val.hi) {
1208
+ while (val.hi !== 0) {
2210
1209
  buf[pos++] = val.lo & 127 | 128;
2211
1210
  val.lo = (val.lo >>> 7 | val.hi << 25) >>> 0;
2212
1211
  val.hi >>>= 7;
@@ -2217,358 +1216,60 @@ function writeVarint64(val, buf, pos) {
2217
1216
  }
2218
1217
  buf[pos++] = val.lo;
2219
1218
  }
2220
-
2221
- /**
2222
- * Writes an unsigned 64 bit value as a varint.
2223
- * @param {Long|number|string} value Value to write
2224
- * @returns {Writer} `this`
2225
- * @throws {TypeError} If `value` is a string and no long library is present.
2226
- */
2227
- Writer$1.prototype.uint64 = function write_uint64(value) {
2228
- var bits = LongBits.from(value);
2229
- return this._push(writeVarint64, bits.length(), bits);
2230
- };
2231
-
2232
- /**
2233
- * Writes a signed 64 bit value as a varint.
2234
- * @function
2235
- * @param {Long|number|string} value Value to write
2236
- * @returns {Writer} `this`
2237
- * @throws {TypeError} If `value` is a string and no long library is present.
2238
- */
2239
- Writer$1.prototype.int64 = Writer$1.prototype.uint64;
2240
-
2241
- /**
2242
- * Writes a signed 64 bit value as a varint, zig-zag encoded.
2243
- * @param {Long|number|string} value Value to write
2244
- * @returns {Writer} `this`
2245
- * @throws {TypeError} If `value` is a string and no long library is present.
2246
- */
2247
- Writer$1.prototype.sint64 = function write_sint64(value) {
2248
- var bits = LongBits.from(value).zzEncode();
2249
- return this._push(writeVarint64, bits.length(), bits);
2250
- };
2251
-
2252
- /**
2253
- * Writes a boolish value as a varint.
2254
- * @param {boolean} value Value to write
2255
- * @returns {Writer} `this`
2256
- */
2257
- Writer$1.prototype.bool = function write_bool(value) {
2258
- return this._push(writeByte, 1, value ? 1 : 0);
2259
- };
2260
-
2261
1219
  function writeFixed32(val, buf, pos) {
2262
- buf[pos ] = val & 255;
2263
- buf[pos + 1] = val >>> 8 & 255;
2264
- buf[pos + 2] = val >>> 16 & 255;
2265
- buf[pos + 3] = val >>> 24;
1220
+ buf[pos] = val & 255;
1221
+ buf[pos + 1] = val >>> 8 & 255;
1222
+ buf[pos + 2] = val >>> 16 & 255;
1223
+ buf[pos + 3] = val >>> 24;
2266
1224
  }
2267
-
2268
- /**
2269
- * Writes an unsigned 32 bit value as fixed 32 bits.
2270
- * @param {number} value Value to write
2271
- * @returns {Writer} `this`
2272
- */
2273
- Writer$1.prototype.fixed32 = function write_fixed32(value) {
2274
- return this._push(writeFixed32, 4, value >>> 0);
2275
- };
2276
-
2277
- /**
2278
- * Writes a signed 32 bit value as fixed 32 bits.
2279
- * @function
2280
- * @param {number} value Value to write
2281
- * @returns {Writer} `this`
2282
- */
2283
- Writer$1.prototype.sfixed32 = Writer$1.prototype.fixed32;
2284
-
2285
- /**
2286
- * Writes an unsigned 64 bit value as fixed 64 bits.
2287
- * @param {Long|number|string} value Value to write
2288
- * @returns {Writer} `this`
2289
- * @throws {TypeError} If `value` is a string and no long library is present.
2290
- */
2291
- Writer$1.prototype.fixed64 = function write_fixed64(value) {
2292
- var bits = LongBits.from(value);
2293
- return this._push(writeFixed32, 4, bits.lo)._push(writeFixed32, 4, bits.hi);
2294
- };
2295
-
2296
- /**
2297
- * Writes a signed 64 bit value as fixed 64 bits.
2298
- * @function
2299
- * @param {Long|number|string} value Value to write
2300
- * @returns {Writer} `this`
2301
- * @throws {TypeError} If `value` is a string and no long library is present.
2302
- */
2303
- Writer$1.prototype.sfixed64 = Writer$1.prototype.fixed64;
2304
-
2305
- /**
2306
- * Writes a float (32 bit).
2307
- * @function
2308
- * @param {number} value Value to write
2309
- * @returns {Writer} `this`
2310
- */
2311
- Writer$1.prototype.float = function write_float(value) {
2312
- return this._push(util$1.float.writeFloatLE, 4, value);
2313
- };
2314
-
2315
- /**
2316
- * Writes a double (64 bit float).
2317
- * @function
2318
- * @param {number} value Value to write
2319
- * @returns {Writer} `this`
2320
- */
2321
- Writer$1.prototype.double = function write_double(value) {
2322
- return this._push(util$1.float.writeDoubleLE, 8, value);
2323
- };
2324
-
2325
- var writeBytes = util$1.Array.prototype.set
2326
- ? function writeBytes_set(val, buf, pos) {
2327
- buf.set(val, pos); // also works for plain array values
2328
- }
2329
- /* istanbul ignore next */
2330
- : function writeBytes_for(val, buf, pos) {
2331
- for (var i = 0; i < val.length; ++i)
2332
- buf[pos + i] = val[i];
2333
- };
2334
-
2335
- /**
2336
- * Writes a sequence of bytes.
2337
- * @param {Uint8Array|string} value Buffer or base64 encoded string to write
2338
- * @returns {Writer} `this`
2339
- */
2340
- Writer$1.prototype.bytes = function write_bytes(value) {
2341
- var len = value.length >>> 0;
2342
- if (!len)
2343
- return this._push(writeByte, 1, 0);
2344
- if (util$1.isString(value)) {
2345
- var buf = Writer$1.alloc(len = base64.length(value));
2346
- base64.decode(value, buf, 0);
2347
- value = buf;
2348
- }
2349
- return this.uint32(len)._push(writeBytes, len, value);
2350
- };
2351
-
2352
- /**
2353
- * Writes a string.
2354
- * @param {string} value Value to write
2355
- * @returns {Writer} `this`
2356
- */
2357
- Writer$1.prototype.string = function write_string(value) {
2358
- var len = utf8.length(value);
2359
- return len
2360
- ? this.uint32(len)._push(utf8.write, len, value)
2361
- : this._push(writeByte, 1, 0);
2362
- };
2363
-
2364
- /**
2365
- * Forks this writer's state by pushing it to a stack.
2366
- * Calling {@link Writer#reset|reset} or {@link Writer#ldelim|ldelim} resets the writer to the previous state.
2367
- * @returns {Writer} `this`
2368
- */
2369
- Writer$1.prototype.fork = function fork() {
2370
- this.states = new State(this);
2371
- this.head = this.tail = new Op(noop, 0, 0);
2372
- this.len = 0;
2373
- return this;
2374
- };
2375
-
2376
- /**
2377
- * Resets this instance to the last state.
2378
- * @returns {Writer} `this`
2379
- */
2380
- Writer$1.prototype.reset = function reset() {
2381
- if (this.states) {
2382
- this.head = this.states.head;
2383
- this.tail = this.states.tail;
2384
- this.len = this.states.len;
2385
- this.states = this.states.next;
2386
- } else {
2387
- this.head = this.tail = new Op(noop, 0, 0);
2388
- this.len = 0;
2389
- }
2390
- return this;
2391
- };
2392
-
2393
- /**
2394
- * Resets to the last state and appends the fork state's current write length as a varint followed by its operations.
2395
- * @returns {Writer} `this`
2396
- */
2397
- Writer$1.prototype.ldelim = function ldelim() {
2398
- var head = this.head,
2399
- tail = this.tail,
2400
- len = this.len;
2401
- this.reset().uint32(len);
2402
- if (len) {
2403
- this.tail.next = head.next; // skip noop
2404
- this.tail = tail;
2405
- this.len += len;
2406
- }
2407
- return this;
2408
- };
2409
-
2410
- /**
2411
- * Finishes the write operation.
2412
- * @returns {Uint8Array} Finished buffer
2413
- */
2414
- Writer$1.prototype.finish = function finish() {
2415
- var head = this.head.next, // skip noop
2416
- buf = this.constructor.alloc(this.len),
2417
- pos = 0;
2418
- while (head) {
2419
- head.fn(head.val, buf, pos);
2420
- pos += head.len;
2421
- head = head.next;
2422
- }
2423
- // this.head = this.tail = null;
2424
- return buf;
2425
- };
2426
-
2427
- Writer$1._configure = function(BufferWriter_) {
2428
- BufferWriter$1 = BufferWriter_;
2429
- Writer$1.create = create();
2430
- BufferWriter$1._configure();
2431
- };
2432
-
2433
- var WriterClass = /*@__PURE__*/getDefaultExportFromCjs(writer$1);
2434
-
2435
- var writer_buffer = BufferWriter;
2436
-
2437
- // extends Writer
2438
- var Writer = writer$1;
2439
- (BufferWriter.prototype = Object.create(Writer.prototype)).constructor = BufferWriter;
2440
-
2441
- var util = requireMinimal();
2442
-
2443
- /**
2444
- * Constructs a new buffer writer instance.
2445
- * @classdesc Wire format writer using node buffers.
2446
- * @extends Writer
2447
- * @constructor
2448
- */
2449
- function BufferWriter() {
2450
- Writer.call(this);
1225
+ function writeBytes(val, buf, pos) {
1226
+ buf.set(val, pos);
2451
1227
  }
2452
-
2453
- BufferWriter._configure = function () {
2454
- /**
2455
- * Allocates a buffer of the specified size.
2456
- * @function
2457
- * @param {number} size Buffer size
2458
- * @returns {Buffer} Buffer
2459
- */
2460
- BufferWriter.alloc = util._Buffer_allocUnsafe;
2461
-
2462
- BufferWriter.writeBytesBuffer = util.Buffer && util.Buffer.prototype instanceof Uint8Array && util.Buffer.prototype.set.name === "set"
2463
- ? function writeBytesBuffer_set(val, buf, pos) {
2464
- buf.set(val, pos); // faster than copy (requires node >= 4 where Buffers extend Uint8Array and set is properly inherited)
2465
- // also works for plain array values
1228
+ if (globalThis.Buffer != null) {
1229
+ Uint8ArrayWriter.prototype.bytes = function (value) {
1230
+ const len = value.length >>> 0;
1231
+ this.uint32(len);
1232
+ if (len > 0) {
1233
+ this._push(writeBytesBuffer, len, value);
2466
1234
  }
2467
- /* istanbul ignore next */
2468
- : function writeBytesBuffer_copy(val, buf, pos) {
2469
- if (val.copy) // Buffer values
2470
- val.copy(buf, pos, 0, val.length);
2471
- else for (var i = 0; i < val.length;) // plain array values
2472
- buf[pos++] = val[i++];
2473
- };
2474
- };
2475
-
2476
-
2477
- /**
2478
- * @override
2479
- */
2480
- BufferWriter.prototype.bytes = function write_bytes_buffer(value) {
2481
- if (util.isString(value))
2482
- value = util._Buffer_from(value, "base64");
2483
- var len = value.length >>> 0;
2484
- this.uint32(len);
2485
- if (len)
2486
- this._push(BufferWriter.writeBytesBuffer, len, value);
2487
- return this;
2488
- };
2489
-
2490
- function writeStringBuffer(val, buf, pos) {
2491
- if (val.length < 40) // plain js is faster for short strings (probably due to redundant assertions)
2492
- util.utf8.write(val, buf, pos);
2493
- else if (buf.utf8Write)
2494
- buf.utf8Write(val, pos);
2495
- else
2496
- buf.write(val, pos);
2497
- }
2498
-
2499
- /**
2500
- * @override
2501
- */
2502
- BufferWriter.prototype.string = function write_string_buffer(value) {
2503
- var len = util.Buffer.byteLength(value);
2504
- this.uint32(len);
2505
- if (len)
2506
- this._push(writeStringBuffer, len, value);
2507
- return this;
2508
- };
2509
-
2510
-
2511
- /**
2512
- * Finishes the write operation.
2513
- * @name BufferWriter#finish
2514
- * @function
2515
- * @returns {Buffer} Finished buffer
2516
- */
2517
-
2518
- BufferWriter._configure();
2519
-
2520
- var WriterBufferClass = /*@__PURE__*/getDefaultExportFromCjs(writer_buffer);
2521
-
2522
- // @ts-expect-error no types
2523
- function configure() {
2524
- util$2._configure();
2525
- ReaderClass._configure(ReaderBufferClass);
2526
- WriterClass._configure(WriterBufferClass);
2527
- }
2528
- // Set up buffer utility according to the environment
2529
- configure();
2530
- // monkey patch the reader to add native bigint support
2531
- const methods = [
2532
- 'uint64', 'int64', 'sint64', 'fixed64', 'sfixed64'
2533
- ];
2534
- function patchReader(obj) {
2535
- for (const method of methods) {
2536
- if (obj[method] == null) {
2537
- continue;
1235
+ return this;
1236
+ };
1237
+ Uint8ArrayWriter.prototype.string = function (value) {
1238
+ const len = globalThis.Buffer.byteLength(value);
1239
+ this.uint32(len);
1240
+ if (len > 0) {
1241
+ this._push(writeStringBuffer, len, value);
2538
1242
  }
2539
- const original = obj[method];
2540
- obj[method] = function () {
2541
- return BigInt(original.call(this).toString());
2542
- };
2543
- }
2544
- return obj;
1243
+ return this;
1244
+ };
2545
1245
  }
2546
- function reader(buf) {
2547
- return patchReader(new ReaderClass(buf));
1246
+ function writeBytesBuffer(val, buf, pos) {
1247
+ buf.set(val, pos); // faster than copy (requires node >= 4 where Buffers extend Uint8Array and set is properly inherited)
1248
+ // also works for plain array values
2548
1249
  }
2549
- function patchWriter(obj) {
2550
- for (const method of methods) {
2551
- if (obj[method] == null) {
2552
- continue;
2553
- }
2554
- const original = obj[method];
2555
- obj[method] = function (val) {
2556
- return original.call(this, val.toString());
2557
- };
1250
+ function writeStringBuffer(val, buf, pos) {
1251
+ if (val.length < 40) {
1252
+ // plain js is faster for short strings (probably due to redundant assertions)
1253
+ write(val, buf, pos);
1254
+ // @ts-expect-error buf isn't a Uint8Array?
1255
+ }
1256
+ else if (buf.utf8Write != null) {
1257
+ // @ts-expect-error buf isn't a Uint8Array?
1258
+ buf.utf8Write(val, pos);
1259
+ }
1260
+ else {
1261
+ buf.set(fromString(val), pos);
2558
1262
  }
2559
- return obj;
2560
- }
2561
- function writer() {
2562
- return patchWriter(WriterClass.create());
2563
1263
  }
2564
-
2565
- function decodeMessage(buf, codec) {
2566
- const r = reader(buf instanceof Uint8Array ? buf : buf.subarray());
2567
- return codec.decode(r);
1264
+ /**
1265
+ * Creates a new writer
1266
+ */
1267
+ function createWriter() {
1268
+ return new Uint8ArrayWriter();
2568
1269
  }
2569
1270
 
2570
1271
  function encodeMessage(message, codec) {
2571
- const w = writer();
1272
+ const w = createWriter();
2572
1273
  codec.encode(message, w, {
2573
1274
  lengthDelimited: false
2574
1275
  });
@@ -4846,8 +3547,13 @@ var WakuMetadataResponse;
4846
3547
  };
4847
3548
  })(WakuMetadataResponse || (WakuMetadataResponse = {}));
4848
3549
 
3550
+ // copied from utils
3551
+ function isBytes$1(a) {
3552
+ return (a instanceof Uint8Array ||
3553
+ (a != null && typeof a === 'object' && a.constructor.name === 'Uint8Array'));
3554
+ }
4849
3555
  function bytes(b, ...lengths) {
4850
- if (!(b instanceof Uint8Array))
3556
+ if (!isBytes$1(b))
4851
3557
  throw new Error('Expected Uint8Array');
4852
3558
  if (lengths.length > 0 && !lengths.includes(b.length))
4853
3559
  throw new Error(`Expected Uint8Array of length ${lengths}, not of length=${b.length}`);
@@ -4872,14 +3578,19 @@ function output(out, instance) {
4872
3578
  // For node.js, package.json#exports field mapping rewrites import
4873
3579
  // from `crypto` to `cryptoNode`, which imports native module.
4874
3580
  // Makes the utils un-importable in browsers without a bundler.
4875
- // Once node.js 18 is deprecated, we can just drop the import.
4876
- const u8a = (a) => a instanceof Uint8Array;
3581
+ // Once node.js 18 is deprecated (2025-04-30), we can just drop the import.
3582
+ function isBytes(a) {
3583
+ return (a instanceof Uint8Array ||
3584
+ (a != null && typeof a === 'object' && a.constructor.name === 'Uint8Array'));
3585
+ }
4877
3586
  // Cast array to view
4878
3587
  const createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
4879
3588
  // The rotate right (circular right shift) operation for uint32
4880
3589
  const rotr = (word, shift) => (word << (32 - shift)) | (word >>> shift);
4881
3590
  // big-endian hardware is rare. Just in case someone still decides to run hashes:
4882
3591
  // early-throw an error because we don't support BE yet.
3592
+ // Other libraries would silently corrupt the data instead of throwing an error,
3593
+ // when they don't support it.
4883
3594
  const isLE = new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44;
4884
3595
  if (!isLE)
4885
3596
  throw new Error('Non little-endian hardware is not supported');
@@ -4899,7 +3610,7 @@ function utf8ToBytes(str) {
4899
3610
  function toBytes(data) {
4900
3611
  if (typeof data === 'string')
4901
3612
  data = utf8ToBytes(data);
4902
- if (!u8a(data))
3613
+ if (!isBytes(data))
4903
3614
  throw new Error(`expected Uint8Array, got ${typeof data}`);
4904
3615
  return data;
4905
3616
  }
@@ -5205,6 +3916,7 @@ var Tags;
5205
3916
  (function (Tags) {
5206
3917
  Tags["BOOTSTRAP"] = "bootstrap";
5207
3918
  Tags["PEER_EXCHANGE"] = "peer-exchange";
3919
+ Tags["LOCAL"] = "local-peer-cache";
5208
3920
  })(Tags || (Tags = {}));
5209
3921
  var EPeersByDiscoveryEvents;
5210
3922
  (function (EPeersByDiscoveryEvents) {
@@ -5222,6 +3934,10 @@ var EConnectionStateEvents;
5222
3934
  * DefaultPubsubTopic is the default gossipsub topic to use for Waku.
5223
3935
  */
5224
3936
  const DefaultPubsubTopic = "/waku/2/default-waku/proto";
3937
+ /**
3938
+ * The default cluster ID for The Waku Network
3939
+ */
3940
+ const DEFAULT_CLUSTER_ID = 1;
5225
3941
 
5226
3942
  const singleShardInfoToPubsubTopic = (shardInfo) => {
5227
3943
  if (shardInfo.clusterId === undefined || shardInfo.shard === undefined)
@@ -5285,7 +4001,7 @@ function contentTopicToShardIndex(contentTopic, networkShards = 8) {
5285
4001
  const dataview = new DataView(digest.buffer.slice(-8));
5286
4002
  return Number(dataview.getBigUint64(0, false) % BigInt(networkShards));
5287
4003
  }
5288
- function contentTopicToPubsubTopic(contentTopic, clusterId = 1, networkShards = 8) {
4004
+ function contentTopicToPubsubTopic(contentTopic, clusterId = DEFAULT_CLUSTER_ID, networkShards = 8) {
5289
4005
  const shardIndex = contentTopicToShardIndex(contentTopic, networkShards);
5290
4006
  return `/waku/2/rs/${clusterId}/${shardIndex}`;
5291
4007
  }