@waku/message-encryption 0.0.24 → 0.0.26-434be7b.0
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.
- package/CHANGELOG.md +8 -0
- package/bundle/crypto.js +1 -1
- package/bundle/ecies.js +2 -3
- package/bundle/{index-alzPvot7.js → index-Caa7SScj.js} +1 -1
- package/bundle/index.js +127 -5
- package/bundle/{encryption-zFGfcjHZ.js → symmetric-BtVudDdW.js} +1320 -2479
- package/bundle/{symmetric-CXVjdTdV.js → symmetric-TylJB-2X.js} +450 -607
- package/bundle/symmetric.js +2 -3
- package/dist/.tsbuildinfo +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -112
- package/src/index.ts +1 -1
- package/bundle/ecies-R65pUoo3.js +0 -124
- package/bundle/symmetric-7aAyizy_.js +0 -124
@@ -1,2144 +1,1190 @@
|
|
1
|
-
import {
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
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 { c as allocUnsafe, f as fromString, d as concat, u as utf8ToBytes$1, e as getDefaultExportFromCjs, h as encrypt, i as hexToBytes, j as decrypt, k as generateIv, l as encrypt$1, S as Symmetric, m as decrypt$1, r as randomBytes, n as keccak256, s as sign, o as Signature, p as recoverPublicKey, V as Version$1, O as OneMillion$1, a as generateSymmetricKey } from './symmetric-TylJB-2X.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
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
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
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
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
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
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
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
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
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
function
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
|
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
|
-
|
784
|
-
|
785
|
-
|
786
|
-
|
787
|
-
|
788
|
-
|
789
|
-
|
790
|
-
|
791
|
-
|
792
|
-
|
793
|
-
|
794
|
-
|
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
|
-
|
840
|
-
|
841
|
-
|
842
|
-
|
843
|
-
|
844
|
-
|
845
|
-
|
846
|
-
|
847
|
-
|
848
|
-
|
849
|
-
|
850
|
-
|
851
|
-
|
852
|
-
|
853
|
-
|
854
|
-
|
855
|
-
|
856
|
-
|
857
|
-
|
858
|
-
|
859
|
-
|
860
|
-
|
861
|
-
|
862
|
-
|
863
|
-
|
864
|
-
|
865
|
-
|
866
|
-
|
867
|
-
|
868
|
-
|
869
|
-
|
870
|
-
|
871
|
-
|
872
|
-
|
873
|
-
|
874
|
-
|
875
|
-
|
876
|
-
|
877
|
-
|
878
|
-
|
879
|
-
|
880
|
-
|
881
|
-
|
882
|
-
|
883
|
-
|
884
|
-
|
885
|
-
|
886
|
-
|
887
|
-
|
888
|
-
|
889
|
-
|
890
|
-
|
891
|
-
|
892
|
-
|
893
|
-
|
894
|
-
|
895
|
-
|
896
|
-
|
897
|
-
|
898
|
-
|
899
|
-
|
900
|
-
|
901
|
-
|
902
|
-
|
903
|
-
|
904
|
-
|
905
|
-
|
906
|
-
|
907
|
-
|
908
|
-
|
909
|
-
|
910
|
-
|
911
|
-
|
912
|
-
|
913
|
-
|
914
|
-
|
915
|
-
|
916
|
-
|
917
|
-
|
918
|
-
|
919
|
-
|
920
|
-
|
921
|
-
|
922
|
-
|
923
|
-
|
924
|
-
|
925
|
-
|
926
|
-
|
927
|
-
|
928
|
-
|
929
|
-
|
930
|
-
|
931
|
-
|
932
|
-
|
933
|
-
|
934
|
-
|
935
|
-
|
936
|
-
|
937
|
-
|
938
|
-
|
939
|
-
|
940
|
-
|
941
|
-
|
942
|
-
|
943
|
-
|
944
|
-
|
945
|
-
|
946
|
-
|
947
|
-
|
948
|
-
|
949
|
-
|
950
|
-
|
951
|
-
|
952
|
-
|
953
|
-
|
954
|
-
|
955
|
-
|
956
|
-
|
957
|
-
|
958
|
-
|
959
|
-
|
960
|
-
|
961
|
-
|
962
|
-
|
963
|
-
|
964
|
-
|
965
|
-
|
966
|
-
|
967
|
-
|
968
|
-
|
969
|
-
|
970
|
-
|
971
|
-
|
972
|
-
|
973
|
-
|
974
|
-
|
975
|
-
|
976
|
-
|
977
|
-
|
978
|
-
|
979
|
-
|
980
|
-
|
981
|
-
|
982
|
-
|
983
|
-
|
984
|
-
|
985
|
-
|
986
|
-
|
987
|
-
|
988
|
-
|
989
|
-
|
990
|
-
|
991
|
-
|
992
|
-
|
993
|
-
|
994
|
-
|
995
|
-
|
996
|
-
|
997
|
-
|
998
|
-
|
999
|
-
|
1000
|
-
|
1001
|
-
|
1002
|
-
|
1003
|
-
|
1004
|
-
|
1005
|
-
|
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
|
-
|
1048
|
-
|
1049
|
-
|
1050
|
-
|
1051
|
-
|
1052
|
-
|
1053
|
-
|
1054
|
-
|
1055
|
-
|
1056
|
-
|
1057
|
-
|
1058
|
-
|
1059
|
-
|
1060
|
-
|
1061
|
-
|
1062
|
-
|
1063
|
-
|
1064
|
-
|
1065
|
-
|
1066
|
-
|
1067
|
-
|
1068
|
-
|
1069
|
-
|
1070
|
-
|
1071
|
-
|
1072
|
-
|
1073
|
-
|
1074
|
-
|
1075
|
-
|
1076
|
-
|
1077
|
-
|
1078
|
-
|
1079
|
-
|
1080
|
-
|
1081
|
-
|
1082
|
-
|
1083
|
-
|
1084
|
-
|
1085
|
-
|
1086
|
-
|
1087
|
-
|
1088
|
-
|
1089
|
-
|
1090
|
-
|
1091
|
-
|
1092
|
-
|
1093
|
-
|
1094
|
-
|
1095
|
-
|
1096
|
-
|
1097
|
-
|
1098
|
-
|
1099
|
-
|
1100
|
-
|
1101
|
-
|
1102
|
-
|
1103
|
-
|
1104
|
-
|
1105
|
-
|
1106
|
-
|
1107
|
-
|
1108
|
-
|
1109
|
-
|
1110
|
-
|
1111
|
-
|
1112
|
-
|
1113
|
-
|
1114
|
-
|
1115
|
-
|
1116
|
-
|
1117
|
-
|
1118
|
-
|
1119
|
-
|
1120
|
-
|
1121
|
-
|
1122
|
-
|
1123
|
-
|
1124
|
-
|
1125
|
-
|
1126
|
-
|
1127
|
-
|
1128
|
-
|
1129
|
-
|
1130
|
-
|
1131
|
-
|
1132
|
-
|
1133
|
-
|
1134
|
-
|
1135
|
-
|
1136
|
-
|
1137
|
-
|
1138
|
-
|
1139
|
-
|
1140
|
-
|
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(
|
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
|
-
|
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
|
+
}
|
483
|
+
/**
|
484
|
+
* Reads a varint as an unsigned 32 bit value
|
485
|
+
*/
|
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
|
+
}
|
509
|
+
/**
|
510
|
+
* Reads a varint as a signed 32 bit value
|
511
|
+
*/
|
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
|
+
}
|
615
|
+
/**
|
616
|
+
* Skips the next element of the specified wire type
|
617
|
+
*/
|
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}`);
|
640
|
+
}
|
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) {
|
659
|
+
return bits;
|
660
|
+
}
|
661
|
+
i = 0;
|
662
|
+
}
|
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;
|
677
|
+
return bits;
|
678
|
+
}
|
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
|
+
}
|
687
|
+
}
|
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);
|
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
|
+
}
|
1516
750
|
/**
|
1517
|
-
*
|
1518
|
-
* @type {Uint8Array}
|
751
|
+
* Reads a zig-zag encoded varint as a signed 64 bit value
|
1519
752
|
*/
|
1520
|
-
|
1521
|
-
|
753
|
+
sint64() {
|
754
|
+
return this.readLongVarint().zzDecode().toBigInt();
|
755
|
+
}
|
1522
756
|
/**
|
1523
|
-
*
|
1524
|
-
*
|
757
|
+
* Reads a zig-zag encoded varint as a signed 64 bit value returned as a
|
758
|
+
* possibly unsafe JavaScript number
|
1525
759
|
*/
|
1526
|
-
|
1527
|
-
|
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
|
+
}
|
1528
794
|
/**
|
1529
|
-
*
|
1530
|
-
*
|
795
|
+
* Reads zig-zag encoded fixed 64 bits returned as a possibly unsafe
|
796
|
+
* JavaScript number
|
1531
797
|
*/
|
1532
|
-
|
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();
|
806
|
+
}
|
807
|
+
}
|
808
|
+
function createReader(buf) {
|
809
|
+
return new Uint8ArrayReader(buf instanceof Uint8Array ? buf : buf.subarray());
|
1533
810
|
}
|
1534
811
|
|
1535
|
-
|
1536
|
-
|
1537
|
-
|
1538
|
-
|
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);
|
1591
|
-
}
|
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
|
-
};
|
812
|
+
function decodeMessage(buf, codec, opts) {
|
813
|
+
const reader = createReader(buf);
|
814
|
+
return codec.decode(reader, undefined, opts);
|
815
|
+
}
|
1603
816
|
|
1604
817
|
/**
|
1605
|
-
*
|
1606
|
-
* @returns {number} Value read
|
818
|
+
* A general purpose buffer pool
|
1607
819
|
*/
|
1608
|
-
|
1609
|
-
|
1610
|
-
|
1611
|
-
|
1612
|
-
|
1613
|
-
|
1614
|
-
|
1615
|
-
|
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)
|
1624
|
-
return bits;
|
1625
|
-
}
|
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)
|
1630
|
-
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;
|
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);
|
1641
828
|
}
|
1642
|
-
|
1643
|
-
|
1644
|
-
|
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;
|
829
|
+
if (offset + size > SIZE) {
|
830
|
+
slab = allocUnsafe(SIZE);
|
831
|
+
offset = 0;
|
1652
832
|
}
|
1653
|
-
|
1654
|
-
|
1655
|
-
|
1656
|
-
|
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;
|
833
|
+
const buf = slab.subarray(offset, offset += size);
|
834
|
+
if ((offset & 7) !== 0) {
|
835
|
+
// align to 32 bit
|
836
|
+
offset = (offset | 7) + 1;
|
1662
837
|
}
|
1663
|
-
|
1664
|
-
|
1665
|
-
throw Error("invalid varint encoding");
|
838
|
+
return buf;
|
839
|
+
};
|
1666
840
|
}
|
1667
841
|
|
1668
|
-
/* eslint-enable no-invalid-this */
|
1669
|
-
|
1670
842
|
/**
|
1671
|
-
*
|
1672
|
-
*
|
1673
|
-
* @
|
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
|
843
|
+
* Constructs a new writer operation instance.
|
844
|
+
*
|
845
|
+
* @classdesc Scheduled writer operation
|
1694
846
|
*/
|
1695
|
-
|
1696
|
-
|
1697
|
-
|
1698
|
-
|
1699
|
-
|
1700
|
-
|
1701
|
-
|
1702
|
-
|
1703
|
-
|
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
|
869
|
+
}
|
1704
870
|
}
|
1705
|
-
|
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
|
-
|
871
|
+
/* istanbul ignore next */
|
872
|
+
function noop() { } // eslint-disable-line no-empty-function
|
1719
873
|
/**
|
1720
|
-
*
|
1721
|
-
* @returns {number} Value read
|
874
|
+
* Constructs a new writer state instance
|
1722
875
|
*/
|
1723
|
-
|
1724
|
-
|
1725
|
-
|
1726
|
-
|
1727
|
-
|
1728
|
-
|
1729
|
-
|
1730
|
-
|
1731
|
-
|
1732
|
-
|
1733
|
-
|
1734
|
-
|
1735
|
-
|
1736
|
-
|
1737
|
-
|
1738
|
-
|
1739
|
-
|
1740
|
-
|
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
|
+
}
|
1741
899
|
}
|
1742
|
-
|
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
|
-
/**
|
1776
|
-
* Reads a double (64 bit float) as a number.
|
1777
|
-
* @function
|
1778
|
-
* @returns {number} Value read
|
1779
|
-
*/
|
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
|
-
};
|
1790
|
-
|
900
|
+
const bufferPool = pool();
|
1791
901
|
/**
|
1792
|
-
*
|
1793
|
-
* @returns {Uint8Array} Value read
|
902
|
+
* Allocates a buffer of the specified size
|
1794
903
|
*/
|
1795
|
-
|
1796
|
-
|
1797
|
-
|
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);
|
904
|
+
function alloc(size) {
|
905
|
+
if (globalThis.Buffer != null) {
|
906
|
+
return allocUnsafe(size);
|
1813
907
|
}
|
1814
|
-
return
|
1815
|
-
}
|
1816
|
-
|
1817
|
-
/**
|
1818
|
-
* Reads a string preceeded by its byte length as a varint.
|
1819
|
-
* @returns {string} Value read
|
1820
|
-
*/
|
1821
|
-
Reader$1.prototype.string = function read_string() {
|
1822
|
-
var bytes = this.bytes();
|
1823
|
-
return utf8$1.read(bytes, 0, bytes.length);
|
1824
|
-
};
|
1825
|
-
|
908
|
+
return bufferPool(size);
|
909
|
+
}
|
1826
910
|
/**
|
1827
|
-
*
|
1828
|
-
*
|
1829
|
-
*
|
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.
|
1830
916
|
*/
|
1831
|
-
|
1832
|
-
|
1833
|
-
|
1834
|
-
|
1835
|
-
|
1836
|
-
|
1837
|
-
|
1838
|
-
|
1839
|
-
|
1840
|
-
|
1841
|
-
|
1842
|
-
|
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;
|
1843
939
|
}
|
1844
|
-
|
1845
|
-
|
1846
|
-
|
1847
|
-
|
1848
|
-
|
1849
|
-
|
1850
|
-
|
1851
|
-
*/
|
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);
|
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;
|
1875
947
|
}
|
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
948
|
/**
|
1931
|
-
*
|
1932
|
-
* @name BufferReader#buf
|
1933
|
-
* @type {Buffer}
|
949
|
+
* Writes an unsigned 32 bit value as a varint
|
1934
950
|
*/
|
1935
|
-
|
1936
|
-
|
1937
|
-
|
1938
|
-
|
1939
|
-
|
1940
|
-
|
1941
|
-
|
1942
|
-
|
1943
|
-
|
1944
|
-
|
1945
|
-
|
1946
|
-
|
1947
|
-
|
1948
|
-
|
1949
|
-
|
1950
|
-
|
1951
|
-
|
1952
|
-
|
1953
|
-
|
1954
|
-
|
1955
|
-
|
1956
|
-
|
1957
|
-
|
1958
|
-
|
1959
|
-
|
1960
|
-
|
1961
|
-
|
1962
|
-
|
1963
|
-
|
1964
|
-
|
1965
|
-
|
1966
|
-
|
1967
|
-
|
1968
|
-
|
1969
|
-
|
1970
|
-
|
1971
|
-
|
1972
|
-
|
1973
|
-
|
1974
|
-
|
1975
|
-
|
1976
|
-
|
1977
|
-
|
1978
|
-
|
1979
|
-
|
1980
|
-
|
1981
|
-
|
1982
|
-
|
1983
|
-
|
1984
|
-
|
1985
|
-
|
1986
|
-
|
1987
|
-
|
1988
|
-
|
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);
|
1054
|
+
}
|
1989
1055
|
/**
|
1990
|
-
*
|
1991
|
-
* @type {function(Uint8Array, number, *)}
|
1056
|
+
* Writes an unsigned 64 bit value as fixed 64 bits
|
1992
1057
|
*/
|
1993
|
-
|
1994
|
-
|
1058
|
+
fixed64(value) {
|
1059
|
+
const bits = LongBits.fromBigInt(value);
|
1060
|
+
return this._push(writeFixed32, 4, bits.lo)._push(writeFixed32, 4, bits.hi);
|
1061
|
+
}
|
1995
1062
|
/**
|
1996
|
-
*
|
1997
|
-
* @type {number}
|
1063
|
+
* Writes an unsigned 64 bit value as fixed 64 bits
|
1998
1064
|
*/
|
1999
|
-
|
2000
|
-
|
1065
|
+
fixed64Number(value) {
|
1066
|
+
const bits = LongBits.fromNumber(value);
|
1067
|
+
return this._push(writeFixed32, 4, bits.lo)._push(writeFixed32, 4, bits.hi);
|
1068
|
+
}
|
2001
1069
|
/**
|
2002
|
-
*
|
2003
|
-
* @type {Writer.Op|undefined}
|
1070
|
+
* Writes an unsigned 64 bit value as fixed 64 bits
|
2004
1071
|
*/
|
2005
|
-
|
2006
|
-
|
1072
|
+
fixed64String(value) {
|
1073
|
+
return this.fixed64(BigInt(value));
|
1074
|
+
}
|
2007
1075
|
/**
|
2008
|
-
*
|
2009
|
-
* @type {*}
|
1076
|
+
* Writes a signed 64 bit value as fixed 64 bits
|
2010
1077
|
*/
|
2011
|
-
|
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
|
-
|
1078
|
+
sfixed64(value) {
|
1079
|
+
return this.fixed64(value);
|
1080
|
+
}
|
2027
1081
|
/**
|
2028
|
-
*
|
2029
|
-
* @type {Writer.Op}
|
1082
|
+
* Writes a signed 64 bit value as fixed 64 bits
|
2030
1083
|
*/
|
2031
|
-
|
2032
|
-
|
1084
|
+
sfixed64Number(value) {
|
1085
|
+
return this.fixed64Number(value);
|
1086
|
+
}
|
2033
1087
|
/**
|
2034
|
-
*
|
2035
|
-
* @type {Writer.Op}
|
1088
|
+
* Writes a signed 64 bit value as fixed 64 bits
|
2036
1089
|
*/
|
2037
|
-
|
2038
|
-
|
1090
|
+
sfixed64String(value) {
|
1091
|
+
return this.fixed64String(value);
|
1092
|
+
}
|
2039
1093
|
/**
|
2040
|
-
*
|
2041
|
-
* @type {number}
|
1094
|
+
* Writes a float (32 bit)
|
2042
1095
|
*/
|
2043
|
-
|
2044
|
-
|
1096
|
+
float(value) {
|
1097
|
+
return this._push(writeFloatLE, 4, value);
|
1098
|
+
}
|
2045
1099
|
/**
|
2046
|
-
*
|
2047
|
-
*
|
1100
|
+
* Writes a double (64 bit float).
|
1101
|
+
*
|
1102
|
+
* @function
|
1103
|
+
* @param {number} value - Value to write
|
1104
|
+
* @returns {Writer} `this`
|
2048
1105
|
*/
|
2049
|
-
|
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
|
-
|
1106
|
+
double(value) {
|
1107
|
+
return this._push(writeDoubleLE, 8, value);
|
1108
|
+
}
|
2059
1109
|
/**
|
2060
|
-
*
|
2061
|
-
* @type {number}
|
1110
|
+
* Writes a sequence of bytes
|
2062
1111
|
*/
|
2063
|
-
|
2064
|
-
|
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
|
+
}
|
2065
1119
|
/**
|
2066
|
-
*
|
2067
|
-
* @type {Object}
|
1120
|
+
* Writes a string
|
2068
1121
|
*/
|
2069
|
-
|
2070
|
-
|
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
|
+
}
|
2071
1128
|
/**
|
2072
|
-
*
|
2073
|
-
* @
|
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.
|
2074
1131
|
*/
|
2075
|
-
|
2076
|
-
|
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
|
+
}
|
2077
1138
|
/**
|
2078
|
-
*
|
2079
|
-
* @type {Object|null}
|
1139
|
+
* Resets this instance to the last state
|
2080
1140
|
*/
|
2081
|
-
|
2082
|
-
|
2083
|
-
|
2084
|
-
|
2085
|
-
|
2086
|
-
|
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
|
-
})();
|
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;
|
2096
1147
|
}
|
2097
|
-
|
2098
|
-
|
2099
|
-
|
2100
|
-
}
|
2101
|
-
|
2102
|
-
|
2103
|
-
/**
|
2104
|
-
|
2105
|
-
|
2106
|
-
|
2107
|
-
|
2108
|
-
|
2109
|
-
|
2110
|
-
|
2111
|
-
|
2112
|
-
|
2113
|
-
|
2114
|
-
|
2115
|
-
|
2116
|
-
|
2117
|
-
}
|
2118
|
-
|
2119
|
-
|
2120
|
-
|
2121
|
-
|
2122
|
-
|
2123
|
-
|
2124
|
-
|
2125
|
-
|
2126
|
-
|
2127
|
-
|
2128
|
-
|
2129
|
-
|
2130
|
-
|
2131
|
-
|
2132
|
-
|
2133
|
-
|
2134
|
-
this.len += len;
|
2135
|
-
return this;
|
2136
|
-
};
|
2137
|
-
|
1148
|
+
else {
|
1149
|
+
this.head = this.tail = new Op(noop, 0, 0);
|
1150
|
+
this.len = 0;
|
1151
|
+
}
|
1152
|
+
return this;
|
1153
|
+
}
|
1154
|
+
/**
|
1155
|
+
* Resets to the last state and appends the fork state's current write length as a varint followed by its operations.
|
1156
|
+
*/
|
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
|
+
}
|
1169
|
+
/**
|
1170
|
+
* Finishes the write operation
|
1171
|
+
*/
|
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;
|
1180
|
+
}
|
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
|
-
*
|
2153
|
-
* @
|
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
|
-
|
2160
|
-
|
2161
|
-
|
2162
|
-
|
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
|
2263
|
-
buf[pos + 1] =
|
2264
|
-
buf[pos + 2] =
|
2265
|
-
buf[pos + 3] =
|
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
|
-
|
2454
|
-
|
2455
|
-
|
2456
|
-
|
2457
|
-
|
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
|
-
|
2468
|
-
|
2469
|
-
|
2470
|
-
|
2471
|
-
|
2472
|
-
|
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
|
-
|
2540
|
-
|
2541
|
-
return BigInt(original.call(this).toString());
|
2542
|
-
};
|
2543
|
-
}
|
2544
|
-
return obj;
|
1243
|
+
return this;
|
1244
|
+
};
|
2545
1245
|
}
|
2546
|
-
function
|
2547
|
-
|
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
|
2550
|
-
|
2551
|
-
|
2552
|
-
|
2553
|
-
|
2554
|
-
|
2555
|
-
|
2556
|
-
|
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
|
-
|
2566
|
-
|
2567
|
-
|
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 =
|
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
|
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
|
-
|
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 (!
|
3613
|
+
if (!isBytes(data))
|
4903
3614
|
throw new Error(`expected Uint8Array, got ${typeof data}`);
|
4904
3615
|
return data;
|
4905
3616
|
}
|
@@ -5146,54 +3857,59 @@ var Protocols;
|
|
5146
3857
|
Protocols["LightPush"] = "lightpush";
|
5147
3858
|
Protocols["Filter"] = "filter";
|
5148
3859
|
})(Protocols || (Protocols = {}));
|
5149
|
-
var
|
5150
|
-
(function (
|
3860
|
+
var ProtocolError;
|
3861
|
+
(function (ProtocolError) {
|
5151
3862
|
/** Could not determine the origin of the fault. Best to check connectivity and try again */
|
5152
|
-
|
3863
|
+
ProtocolError["GENERIC_FAIL"] = "Generic error";
|
5153
3864
|
/**
|
5154
3865
|
* Failure to protobuf encode the message. This is not recoverable and needs
|
5155
3866
|
* further investigation.
|
5156
3867
|
*/
|
5157
|
-
|
3868
|
+
ProtocolError["ENCODE_FAILED"] = "Failed to encode";
|
5158
3869
|
/**
|
5159
3870
|
* Failure to protobuf decode the message. May be due to a remote peer issue,
|
5160
3871
|
* ensuring that messages are sent via several peer enable mitigation of this error.
|
5161
3872
|
*/
|
5162
|
-
|
3873
|
+
ProtocolError["DECODE_FAILED"] = "Failed to decode";
|
5163
3874
|
/**
|
5164
3875
|
* The message payload is empty, making the message invalid. Ensure that a non-empty
|
5165
3876
|
* payload is set on the outgoing message.
|
5166
3877
|
*/
|
5167
|
-
|
3878
|
+
ProtocolError["EMPTY_PAYLOAD"] = "Payload is empty";
|
5168
3879
|
/**
|
5169
3880
|
* The message size is above the maximum message size allowed on the Waku Network.
|
5170
3881
|
* Compressing the message or using an alternative strategy for large messages is recommended.
|
5171
3882
|
*/
|
5172
|
-
|
3883
|
+
ProtocolError["SIZE_TOO_BIG"] = "Size is too big";
|
5173
3884
|
/**
|
5174
3885
|
* The PubsubTopic passed to the send function is not configured on the Waku node.
|
5175
3886
|
* Please ensure that the PubsubTopic is used when initializing the Waku node.
|
5176
3887
|
*/
|
5177
|
-
|
3888
|
+
ProtocolError["TOPIC_NOT_CONFIGURED"] = "Topic not configured";
|
5178
3889
|
/**
|
5179
3890
|
* Failure to find a peer with suitable protocols. This may due to a connection issue.
|
5180
3891
|
* Mitigation can be: retrying after a given time period, display connectivity issue
|
5181
3892
|
* to user or listening for `peer:connected:bootstrap` or `peer:connected:peer-exchange`
|
5182
3893
|
* on the connection manager before retrying.
|
5183
3894
|
*/
|
5184
|
-
|
3895
|
+
ProtocolError["NO_PEER_AVAILABLE"] = "No peer available";
|
5185
3896
|
/**
|
5186
3897
|
* The remote peer did not behave as expected. Mitigation for `NO_PEER_AVAILABLE`
|
5187
3898
|
* or `DECODE_FAILED` can be used.
|
5188
3899
|
*/
|
5189
|
-
|
3900
|
+
ProtocolError["REMOTE_PEER_FAULT"] = "Remote peer fault";
|
5190
3901
|
/**
|
5191
3902
|
* The remote peer rejected the message. Information provided by the remote peer
|
5192
3903
|
* is logged. Review message validity, or mitigation for `NO_PEER_AVAILABLE`
|
5193
3904
|
* or `DECODE_FAILED` can be used.
|
5194
3905
|
*/
|
5195
|
-
|
5196
|
-
|
3906
|
+
ProtocolError["REMOTE_PEER_REJECTED"] = "Remote peer rejected";
|
3907
|
+
/**
|
3908
|
+
* The protocol request timed out without a response. This may be due to a connection issue.
|
3909
|
+
* Mitigation can be: retrying after a given time period
|
3910
|
+
*/
|
3911
|
+
ProtocolError["REQUEST_TIMEOUT"] = "Request timeout";
|
3912
|
+
})(ProtocolError || (ProtocolError = {}));
|
5197
3913
|
|
5198
3914
|
var PageDirection;
|
5199
3915
|
(function (PageDirection) {
|
@@ -5205,6 +3921,7 @@ var Tags;
|
|
5205
3921
|
(function (Tags) {
|
5206
3922
|
Tags["BOOTSTRAP"] = "bootstrap";
|
5207
3923
|
Tags["PEER_EXCHANGE"] = "peer-exchange";
|
3924
|
+
Tags["LOCAL"] = "local-peer-cache";
|
5208
3925
|
})(Tags || (Tags = {}));
|
5209
3926
|
var EPeersByDiscoveryEvents;
|
5210
3927
|
(function (EPeersByDiscoveryEvents) {
|
@@ -5222,6 +3939,10 @@ var EConnectionStateEvents;
|
|
5222
3939
|
* DefaultPubsubTopic is the default gossipsub topic to use for Waku.
|
5223
3940
|
*/
|
5224
3941
|
const DefaultPubsubTopic = "/waku/2/default-waku/proto";
|
3942
|
+
/**
|
3943
|
+
* The default cluster ID for The Waku Network
|
3944
|
+
*/
|
3945
|
+
const DEFAULT_CLUSTER_ID = 1;
|
5225
3946
|
|
5226
3947
|
const singleShardInfoToPubsubTopic = (shardInfo) => {
|
5227
3948
|
if (shardInfo.clusterId === undefined || shardInfo.shard === undefined)
|
@@ -5285,7 +4006,7 @@ function contentTopicToShardIndex(contentTopic, networkShards = 8) {
|
|
5285
4006
|
const dataview = new DataView(digest.buffer.slice(-8));
|
5286
4007
|
return Number(dataview.getBigUint64(0, false) % BigInt(networkShards));
|
5287
4008
|
}
|
5288
|
-
function contentTopicToPubsubTopic(contentTopic, clusterId =
|
4009
|
+
function contentTopicToPubsubTopic(contentTopic, clusterId = DEFAULT_CLUSTER_ID, networkShards = 8) {
|
5289
4010
|
const shardIndex = contentTopicToShardIndex(contentTopic, networkShards);
|
5290
4011
|
return `/waku/2/rs/${clusterId}/${shardIndex}`;
|
5291
4012
|
}
|
@@ -6055,7 +4776,7 @@ class Logger {
|
|
6055
4776
|
}
|
6056
4777
|
}
|
6057
4778
|
|
6058
|
-
const log = new Logger("message:version-0");
|
4779
|
+
const log$1 = new Logger("message:version-0");
|
6059
4780
|
const OneMillion = BigInt(1000000);
|
6060
4781
|
const Version = 0;
|
6061
4782
|
let DecodedMessage$1 = class DecodedMessage {
|
@@ -6130,7 +4851,7 @@ let Decoder$1 = class Decoder {
|
|
6130
4851
|
// https://rfc.vac.dev/spec/14/
|
6131
4852
|
// > If omitted, the value SHOULD be interpreted as version 0.
|
6132
4853
|
if (proto.version ?? 0 !== Version) {
|
6133
|
-
log.error("Failed to decode due to incorrect version, expected:", Version, ", actual:", proto.version);
|
4854
|
+
log$1.error("Failed to decode due to incorrect version, expected:", Version, ", actual:", proto.version);
|
6134
4855
|
return Promise.resolve(undefined);
|
6135
4856
|
}
|
6136
4857
|
return new DecodedMessage$1(pubsubTopic, proto);
|
@@ -6353,4 +5074,124 @@ function postCipher(message) {
|
|
6353
5074
|
return { payload, sig };
|
6354
5075
|
}
|
6355
5076
|
|
6356
|
-
|
5077
|
+
const log = new Logger("message-encryption:symmetric");
|
5078
|
+
class Encoder {
|
5079
|
+
pubsubTopic;
|
5080
|
+
contentTopic;
|
5081
|
+
symKey;
|
5082
|
+
sigPrivKey;
|
5083
|
+
ephemeral;
|
5084
|
+
metaSetter;
|
5085
|
+
constructor(pubsubTopic, contentTopic, symKey, sigPrivKey, ephemeral = false, metaSetter) {
|
5086
|
+
this.pubsubTopic = pubsubTopic;
|
5087
|
+
this.contentTopic = contentTopic;
|
5088
|
+
this.symKey = symKey;
|
5089
|
+
this.sigPrivKey = sigPrivKey;
|
5090
|
+
this.ephemeral = ephemeral;
|
5091
|
+
this.metaSetter = metaSetter;
|
5092
|
+
if (!contentTopic || contentTopic === "") {
|
5093
|
+
throw new Error("Content topic must be specified");
|
5094
|
+
}
|
5095
|
+
}
|
5096
|
+
async toWire(message) {
|
5097
|
+
const protoMessage = await this.toProtoObj(message);
|
5098
|
+
if (!protoMessage)
|
5099
|
+
return;
|
5100
|
+
return WakuMessage$3.encode(protoMessage);
|
5101
|
+
}
|
5102
|
+
async toProtoObj(message) {
|
5103
|
+
const timestamp = message.timestamp ?? new Date();
|
5104
|
+
const preparedPayload = await preCipher(message.payload, this.sigPrivKey);
|
5105
|
+
const payload = await encryptSymmetric(preparedPayload, this.symKey);
|
5106
|
+
const protoMessage = {
|
5107
|
+
payload,
|
5108
|
+
version: Version$1,
|
5109
|
+
contentTopic: this.contentTopic,
|
5110
|
+
timestamp: BigInt(timestamp.valueOf()) * OneMillion$1,
|
5111
|
+
meta: undefined,
|
5112
|
+
rateLimitProof: message.rateLimitProof,
|
5113
|
+
ephemeral: this.ephemeral
|
5114
|
+
};
|
5115
|
+
if (this.metaSetter) {
|
5116
|
+
const meta = this.metaSetter(protoMessage);
|
5117
|
+
return { ...protoMessage, meta };
|
5118
|
+
}
|
5119
|
+
return protoMessage;
|
5120
|
+
}
|
5121
|
+
}
|
5122
|
+
/**
|
5123
|
+
* Creates an encoder that encrypts messages using symmetric encryption for the
|
5124
|
+
* given key, as defined in [26/WAKU2-PAYLOAD](https://rfc.vac.dev/spec/26/).
|
5125
|
+
*
|
5126
|
+
* An encoder is used to encode messages in the [`14/WAKU2-MESSAGE](https://rfc.vac.dev/spec/14/)
|
5127
|
+
* format to be sent over the Waku network. The resulting encoder can then be
|
5128
|
+
* pass to { @link @waku/interfaces!ISender.send } to automatically encrypt
|
5129
|
+
* and encode outgoing messages.
|
5130
|
+
*
|
5131
|
+
* The payload can optionally be signed with the given private key as defined
|
5132
|
+
* in [26/WAKU2-PAYLOAD](https://rfc.vac.dev/spec/26/).
|
5133
|
+
*/
|
5134
|
+
function createEncoder({ pubsubTopic = DefaultPubsubTopic, pubsubTopicShardInfo, contentTopic, symKey, sigPrivKey, ephemeral = false, metaSetter }) {
|
5135
|
+
return new Encoder(determinePubsubTopic(contentTopic, pubsubTopic ?? pubsubTopicShardInfo), contentTopic, symKey, sigPrivKey, ephemeral, metaSetter);
|
5136
|
+
}
|
5137
|
+
class Decoder extends Decoder$1 {
|
5138
|
+
symKey;
|
5139
|
+
constructor(pubsubTopic, contentTopic, symKey) {
|
5140
|
+
super(pubsubTopic, contentTopic);
|
5141
|
+
this.symKey = symKey;
|
5142
|
+
}
|
5143
|
+
async fromProtoObj(pubsubTopic, protoMessage) {
|
5144
|
+
const cipherPayload = protoMessage.payload;
|
5145
|
+
if (protoMessage.version !== Version$1) {
|
5146
|
+
log.error("Failed to decrypt due to incorrect version, expected:", Version$1, ", actual:", protoMessage.version);
|
5147
|
+
return;
|
5148
|
+
}
|
5149
|
+
let payload;
|
5150
|
+
try {
|
5151
|
+
payload = await decryptSymmetric(cipherPayload, this.symKey);
|
5152
|
+
}
|
5153
|
+
catch (e) {
|
5154
|
+
log.error(`Failed to decrypt message using asymmetric decryption for contentTopic: ${this.contentTopic}`, e);
|
5155
|
+
return;
|
5156
|
+
}
|
5157
|
+
if (!payload) {
|
5158
|
+
log.error(`Failed to decrypt payload for contentTopic ${this.contentTopic}`);
|
5159
|
+
return;
|
5160
|
+
}
|
5161
|
+
const res = postCipher(payload);
|
5162
|
+
if (!res) {
|
5163
|
+
log.error(`Failed to decode payload for contentTopic ${this.contentTopic}`);
|
5164
|
+
return;
|
5165
|
+
}
|
5166
|
+
log.info("Message decrypted", protoMessage);
|
5167
|
+
return new DecodedMessage(pubsubTopic, protoMessage, res.payload, res.sig?.signature, res.sig?.publicKey);
|
5168
|
+
}
|
5169
|
+
}
|
5170
|
+
/**
|
5171
|
+
* Creates a decoder that decrypts messages using symmetric encryption, using
|
5172
|
+
* the given key as defined in [26/WAKU2-PAYLOAD](https://rfc.vac.dev/spec/26/).
|
5173
|
+
*
|
5174
|
+
* A decoder is used to decode messages from the [14/WAKU2-MESSAGE](https://rfc.vac.dev/spec/14/)
|
5175
|
+
* format when received from the Waku network. The resulting decoder can then be
|
5176
|
+
* pass to { @link @waku/interfaces!IReceiver.subscribe } to automatically decrypt and
|
5177
|
+
* decode incoming messages.
|
5178
|
+
*
|
5179
|
+
* @param contentTopic The resulting decoder will only decode messages with this content topic.
|
5180
|
+
* @param symKey The symmetric key used to decrypt the message.
|
5181
|
+
*/
|
5182
|
+
function createDecoder(contentTopic, symKey, pubsubTopicShardInfo = DefaultPubsubTopic) {
|
5183
|
+
return new Decoder(determinePubsubTopic(contentTopic, pubsubTopicShardInfo), contentTopic, symKey);
|
5184
|
+
}
|
5185
|
+
|
5186
|
+
var symmetric = /*#__PURE__*/Object.freeze({
|
5187
|
+
__proto__: null,
|
5188
|
+
createDecoder: createDecoder,
|
5189
|
+
createEncoder: createEncoder,
|
5190
|
+
decryptSymmetric: decryptSymmetric,
|
5191
|
+
encryptSymmetric: encryptSymmetric,
|
5192
|
+
generateSymmetricKey: generateSymmetricKey,
|
5193
|
+
postCipher: postCipher,
|
5194
|
+
preCipher: preCipher
|
5195
|
+
});
|
5196
|
+
|
5197
|
+
export { DefaultPubsubTopic as D, Logger as L, WakuMessage$3 as W, Decoder$1 as a, decryptAsymmetric as b, postCipher as c, determinePubsubTopic as d, encryptAsymmetric as e, DecodedMessage as f, decryptSymmetric as g, encryptSymmetric as h, createEncoder as i, createDecoder as j, preCipher as p, symmetric as s };
|