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