protobufjs 7.3.1 → 7.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/LICENSE +39 -39
  2. package/README.md +727 -727
  3. package/dist/light/protobuf.js +7381 -0
  4. package/dist/light/protobuf.js.map +1 -0
  5. package/dist/light/protobuf.min.js +8 -0
  6. package/dist/light/protobuf.min.js.map +1 -0
  7. package/dist/minimal/protobuf.js +2736 -0
  8. package/dist/minimal/protobuf.js.map +1 -0
  9. package/dist/minimal/protobuf.min.js +8 -0
  10. package/dist/minimal/protobuf.min.js.map +1 -0
  11. package/dist/protobuf.js +9105 -0
  12. package/dist/protobuf.js.map +1 -0
  13. package/dist/protobuf.min.js +8 -0
  14. package/dist/protobuf.min.js.map +1 -0
  15. package/ext/debug/README.md +4 -4
  16. package/ext/debug/index.js +71 -71
  17. package/ext/descriptor/README.md +72 -72
  18. package/ext/descriptor/index.d.ts +191 -191
  19. package/ext/descriptor/index.js +1052 -1052
  20. package/ext/descriptor/test.js +54 -54
  21. package/google/LICENSE +27 -27
  22. package/google/README.md +1 -1
  23. package/google/api/annotations.json +82 -82
  24. package/google/api/annotations.proto +10 -10
  25. package/google/api/http.json +85 -85
  26. package/google/api/http.proto +30 -30
  27. package/google/protobuf/api.json +117 -117
  28. package/google/protobuf/api.proto +33 -33
  29. package/google/protobuf/descriptor.json +738 -738
  30. package/google/protobuf/descriptor.proto +286 -286
  31. package/google/protobuf/source_context.json +19 -19
  32. package/google/protobuf/source_context.proto +7 -7
  33. package/google/protobuf/type.json +201 -201
  34. package/google/protobuf/type.proto +89 -89
  35. package/index.d.ts +2741 -2741
  36. package/index.js +4 -4
  37. package/light.d.ts +2 -2
  38. package/light.js +3 -3
  39. package/minimal.d.ts +2 -2
  40. package/minimal.js +4 -4
  41. package/package.json +111 -112
  42. package/scripts/postinstall.js +32 -32
  43. package/src/common.js +399 -399
  44. package/src/converter.js +301 -301
  45. package/src/decoder.js +129 -129
  46. package/src/encoder.js +100 -100
  47. package/src/enum.js +198 -198
  48. package/src/field.js +377 -377
  49. package/src/index-light.js +104 -104
  50. package/src/index-minimal.js +36 -36
  51. package/src/index.js +12 -12
  52. package/src/mapfield.js +126 -126
  53. package/src/message.js +138 -138
  54. package/src/method.js +160 -160
  55. package/src/namespace.js +433 -433
  56. package/src/object.js +243 -243
  57. package/src/oneof.js +203 -203
  58. package/src/parse.js +889 -889
  59. package/src/reader.js +416 -416
  60. package/src/reader_buffer.js +51 -51
  61. package/src/root.js +368 -368
  62. package/src/roots.js +18 -18
  63. package/src/rpc/service.js +142 -142
  64. package/src/rpc.js +36 -36
  65. package/src/service.js +167 -167
  66. package/src/tokenize.js +416 -416
  67. package/src/type.js +589 -589
  68. package/src/types.js +196 -196
  69. package/src/typescript.jsdoc +15 -15
  70. package/src/util/longbits.js +200 -200
  71. package/src/util/minimal.js +438 -438
  72. package/src/util.js +212 -212
  73. package/src/verifier.js +176 -176
  74. package/src/wrappers.js +102 -102
  75. package/src/writer.js +465 -465
  76. package/src/writer_buffer.js +85 -85
  77. package/tsconfig.json +7 -7
package/src/reader.js CHANGED
@@ -1,416 +1,416 @@
1
- "use strict";
2
- module.exports = Reader;
3
-
4
- var util = require("./util/minimal");
5
-
6
- var BufferReader; // cyclic
7
-
8
- var LongBits = util.LongBits,
9
- utf8 = util.utf8;
10
-
11
- /* istanbul ignore next */
12
- function indexOutOfRange(reader, writeLength) {
13
- return RangeError("index out of range: " + reader.pos + " + " + (writeLength || 1) + " > " + reader.len);
14
- }
15
-
16
- /**
17
- * Constructs a new reader instance using the specified buffer.
18
- * @classdesc Wire format reader using `Uint8Array` if available, otherwise `Array`.
19
- * @constructor
20
- * @param {Uint8Array} buffer Buffer to read from
21
- */
22
- function Reader(buffer) {
23
-
24
- /**
25
- * Read buffer.
26
- * @type {Uint8Array}
27
- */
28
- this.buf = buffer;
29
-
30
- /**
31
- * Read buffer position.
32
- * @type {number}
33
- */
34
- this.pos = 0;
35
-
36
- /**
37
- * Read buffer length.
38
- * @type {number}
39
- */
40
- this.len = buffer.length;
41
- }
42
-
43
- var create_array = typeof Uint8Array !== "undefined"
44
- ? function create_typed_array(buffer) {
45
- if (buffer instanceof Uint8Array || Array.isArray(buffer))
46
- return new Reader(buffer);
47
- throw Error("illegal buffer");
48
- }
49
- /* istanbul ignore next */
50
- : function create_array(buffer) {
51
- if (Array.isArray(buffer))
52
- return new Reader(buffer);
53
- throw Error("illegal buffer");
54
- };
55
-
56
- var create = function create() {
57
- return util.Buffer
58
- ? function create_buffer_setup(buffer) {
59
- return (Reader.create = function create_buffer(buffer) {
60
- return util.Buffer.isBuffer(buffer)
61
- ? new BufferReader(buffer)
62
- /* istanbul ignore next */
63
- : create_array(buffer);
64
- })(buffer);
65
- }
66
- /* istanbul ignore next */
67
- : create_array;
68
- };
69
-
70
- /**
71
- * Creates a new reader using the specified buffer.
72
- * @function
73
- * @param {Uint8Array|Buffer} buffer Buffer to read from
74
- * @returns {Reader|BufferReader} A {@link BufferReader} if `buffer` is a Buffer, otherwise a {@link Reader}
75
- * @throws {Error} If `buffer` is not a valid buffer
76
- */
77
- Reader.create = create();
78
-
79
- Reader.prototype._slice = util.Array.prototype.subarray || /* istanbul ignore next */ util.Array.prototype.slice;
80
-
81
- /**
82
- * Reads a varint as an unsigned 32 bit value.
83
- * @function
84
- * @returns {number} Value read
85
- */
86
- Reader.prototype.uint32 = (function read_uint32_setup() {
87
- var value = 4294967295; // optimizer type-hint, tends to deopt otherwise (?!)
88
- return function read_uint32() {
89
- value = ( this.buf[this.pos] & 127 ) >>> 0; if (this.buf[this.pos++] < 128) return value;
90
- value = (value | (this.buf[this.pos] & 127) << 7) >>> 0; if (this.buf[this.pos++] < 128) return value;
91
- value = (value | (this.buf[this.pos] & 127) << 14) >>> 0; if (this.buf[this.pos++] < 128) return value;
92
- value = (value | (this.buf[this.pos] & 127) << 21) >>> 0; if (this.buf[this.pos++] < 128) return value;
93
- value = (value | (this.buf[this.pos] & 15) << 28) >>> 0; if (this.buf[this.pos++] < 128) return value;
94
-
95
- /* istanbul ignore if */
96
- if ((this.pos += 5) > this.len) {
97
- this.pos = this.len;
98
- throw indexOutOfRange(this, 10);
99
- }
100
- return value;
101
- };
102
- })();
103
-
104
- /**
105
- * Reads a varint as a signed 32 bit value.
106
- * @returns {number} Value read
107
- */
108
- Reader.prototype.int32 = function read_int32() {
109
- return this.uint32() | 0;
110
- };
111
-
112
- /**
113
- * Reads a zig-zag encoded varint as a signed 32 bit value.
114
- * @returns {number} Value read
115
- */
116
- Reader.prototype.sint32 = function read_sint32() {
117
- var value = this.uint32();
118
- return value >>> 1 ^ -(value & 1) | 0;
119
- };
120
-
121
- /* eslint-disable no-invalid-this */
122
-
123
- function readLongVarint() {
124
- // tends to deopt with local vars for octet etc.
125
- var bits = new LongBits(0, 0);
126
- var i = 0;
127
- if (this.len - this.pos > 4) { // fast route (lo)
128
- for (; i < 4; ++i) {
129
- // 1st..4th
130
- bits.lo = (bits.lo | (this.buf[this.pos] & 127) << i * 7) >>> 0;
131
- if (this.buf[this.pos++] < 128)
132
- return bits;
133
- }
134
- // 5th
135
- bits.lo = (bits.lo | (this.buf[this.pos] & 127) << 28) >>> 0;
136
- bits.hi = (bits.hi | (this.buf[this.pos] & 127) >> 4) >>> 0;
137
- if (this.buf[this.pos++] < 128)
138
- return bits;
139
- i = 0;
140
- } else {
141
- for (; i < 3; ++i) {
142
- /* istanbul ignore if */
143
- if (this.pos >= this.len)
144
- throw indexOutOfRange(this);
145
- // 1st..3th
146
- bits.lo = (bits.lo | (this.buf[this.pos] & 127) << i * 7) >>> 0;
147
- if (this.buf[this.pos++] < 128)
148
- return bits;
149
- }
150
- // 4th
151
- bits.lo = (bits.lo | (this.buf[this.pos++] & 127) << i * 7) >>> 0;
152
- return bits;
153
- }
154
- if (this.len - this.pos > 4) { // fast route (hi)
155
- for (; i < 5; ++i) {
156
- // 6th..10th
157
- bits.hi = (bits.hi | (this.buf[this.pos] & 127) << i * 7 + 3) >>> 0;
158
- if (this.buf[this.pos++] < 128)
159
- return bits;
160
- }
161
- } else {
162
- for (; i < 5; ++i) {
163
- /* istanbul ignore if */
164
- if (this.pos >= this.len)
165
- throw indexOutOfRange(this);
166
- // 6th..10th
167
- bits.hi = (bits.hi | (this.buf[this.pos] & 127) << i * 7 + 3) >>> 0;
168
- if (this.buf[this.pos++] < 128)
169
- return bits;
170
- }
171
- }
172
- /* istanbul ignore next */
173
- throw Error("invalid varint encoding");
174
- }
175
-
176
- /* eslint-enable no-invalid-this */
177
-
178
- /**
179
- * Reads a varint as a signed 64 bit value.
180
- * @name Reader#int64
181
- * @function
182
- * @returns {Long} Value read
183
- */
184
-
185
- /**
186
- * Reads a varint as an unsigned 64 bit value.
187
- * @name Reader#uint64
188
- * @function
189
- * @returns {Long} Value read
190
- */
191
-
192
- /**
193
- * Reads a zig-zag encoded varint as a signed 64 bit value.
194
- * @name Reader#sint64
195
- * @function
196
- * @returns {Long} Value read
197
- */
198
-
199
- /**
200
- * Reads a varint as a boolean.
201
- * @returns {boolean} Value read
202
- */
203
- Reader.prototype.bool = function read_bool() {
204
- return this.uint32() !== 0;
205
- };
206
-
207
- function readFixed32_end(buf, end) { // note that this uses `end`, not `pos`
208
- return (buf[end - 4]
209
- | buf[end - 3] << 8
210
- | buf[end - 2] << 16
211
- | buf[end - 1] << 24) >>> 0;
212
- }
213
-
214
- /**
215
- * Reads fixed 32 bits as an unsigned 32 bit integer.
216
- * @returns {number} Value read
217
- */
218
- Reader.prototype.fixed32 = function read_fixed32() {
219
-
220
- /* istanbul ignore if */
221
- if (this.pos + 4 > this.len)
222
- throw indexOutOfRange(this, 4);
223
-
224
- return readFixed32_end(this.buf, this.pos += 4);
225
- };
226
-
227
- /**
228
- * Reads fixed 32 bits as a signed 32 bit integer.
229
- * @returns {number} Value read
230
- */
231
- Reader.prototype.sfixed32 = function read_sfixed32() {
232
-
233
- /* istanbul ignore if */
234
- if (this.pos + 4 > this.len)
235
- throw indexOutOfRange(this, 4);
236
-
237
- return readFixed32_end(this.buf, this.pos += 4) | 0;
238
- };
239
-
240
- /* eslint-disable no-invalid-this */
241
-
242
- function readFixed64(/* this: Reader */) {
243
-
244
- /* istanbul ignore if */
245
- if (this.pos + 8 > this.len)
246
- throw indexOutOfRange(this, 8);
247
-
248
- return new LongBits(readFixed32_end(this.buf, this.pos += 4), readFixed32_end(this.buf, this.pos += 4));
249
- }
250
-
251
- /* eslint-enable no-invalid-this */
252
-
253
- /**
254
- * Reads fixed 64 bits.
255
- * @name Reader#fixed64
256
- * @function
257
- * @returns {Long} Value read
258
- */
259
-
260
- /**
261
- * Reads zig-zag encoded fixed 64 bits.
262
- * @name Reader#sfixed64
263
- * @function
264
- * @returns {Long} Value read
265
- */
266
-
267
- /**
268
- * Reads a float (32 bit) as a number.
269
- * @function
270
- * @returns {number} Value read
271
- */
272
- Reader.prototype.float = function read_float() {
273
-
274
- /* istanbul ignore if */
275
- if (this.pos + 4 > this.len)
276
- throw indexOutOfRange(this, 4);
277
-
278
- var value = util.float.readFloatLE(this.buf, this.pos);
279
- this.pos += 4;
280
- return value;
281
- };
282
-
283
- /**
284
- * Reads a double (64 bit float) as a number.
285
- * @function
286
- * @returns {number} Value read
287
- */
288
- Reader.prototype.double = function read_double() {
289
-
290
- /* istanbul ignore if */
291
- if (this.pos + 8 > this.len)
292
- throw indexOutOfRange(this, 4);
293
-
294
- var value = util.float.readDoubleLE(this.buf, this.pos);
295
- this.pos += 8;
296
- return value;
297
- };
298
-
299
- /**
300
- * Reads a sequence of bytes preceeded by its length as a varint.
301
- * @returns {Uint8Array} Value read
302
- */
303
- Reader.prototype.bytes = function read_bytes() {
304
- var length = this.uint32(),
305
- start = this.pos,
306
- end = this.pos + length;
307
-
308
- /* istanbul ignore if */
309
- if (end > this.len)
310
- throw indexOutOfRange(this, length);
311
-
312
- this.pos += length;
313
- if (Array.isArray(this.buf)) // plain array
314
- return this.buf.slice(start, end);
315
-
316
- if (start === end) { // fix for IE 10/Win8 and others' subarray returning array of size 1
317
- var nativeBuffer = util.Buffer;
318
- return nativeBuffer
319
- ? nativeBuffer.alloc(0)
320
- : new this.buf.constructor(0);
321
- }
322
- return this._slice.call(this.buf, start, end);
323
- };
324
-
325
- /**
326
- * Reads a string preceeded by its byte length as a varint.
327
- * @returns {string} Value read
328
- */
329
- Reader.prototype.string = function read_string() {
330
- var bytes = this.bytes();
331
- return utf8.read(bytes, 0, bytes.length);
332
- };
333
-
334
- /**
335
- * Skips the specified number of bytes if specified, otherwise skips a varint.
336
- * @param {number} [length] Length if known, otherwise a varint is assumed
337
- * @returns {Reader} `this`
338
- */
339
- Reader.prototype.skip = function skip(length) {
340
- if (typeof length === "number") {
341
- /* istanbul ignore if */
342
- if (this.pos + length > this.len)
343
- throw indexOutOfRange(this, length);
344
- this.pos += length;
345
- } else {
346
- do {
347
- /* istanbul ignore if */
348
- if (this.pos >= this.len)
349
- throw indexOutOfRange(this);
350
- } while (this.buf[this.pos++] & 128);
351
- }
352
- return this;
353
- };
354
-
355
- /**
356
- * Skips the next element of the specified wire type.
357
- * @param {number} wireType Wire type received
358
- * @returns {Reader} `this`
359
- */
360
- Reader.prototype.skipType = function(wireType) {
361
- switch (wireType) {
362
- case 0:
363
- this.skip();
364
- break;
365
- case 1:
366
- this.skip(8);
367
- break;
368
- case 2:
369
- this.skip(this.uint32());
370
- break;
371
- case 3:
372
- while ((wireType = this.uint32() & 7) !== 4) {
373
- this.skipType(wireType);
374
- }
375
- break;
376
- case 5:
377
- this.skip(4);
378
- break;
379
-
380
- /* istanbul ignore next */
381
- default:
382
- throw Error("invalid wire type " + wireType + " at offset " + this.pos);
383
- }
384
- return this;
385
- };
386
-
387
- Reader._configure = function(BufferReader_) {
388
- BufferReader = BufferReader_;
389
- Reader.create = create();
390
- BufferReader._configure();
391
-
392
- var fn = util.Long ? "toLong" : /* istanbul ignore next */ "toNumber";
393
- util.merge(Reader.prototype, {
394
-
395
- int64: function read_int64() {
396
- return readLongVarint.call(this)[fn](false);
397
- },
398
-
399
- uint64: function read_uint64() {
400
- return readLongVarint.call(this)[fn](true);
401
- },
402
-
403
- sint64: function read_sint64() {
404
- return readLongVarint.call(this).zzDecode()[fn](false);
405
- },
406
-
407
- fixed64: function read_fixed64() {
408
- return readFixed64.call(this)[fn](true);
409
- },
410
-
411
- sfixed64: function read_sfixed64() {
412
- return readFixed64.call(this)[fn](false);
413
- }
414
-
415
- });
416
- };
1
+ "use strict";
2
+ module.exports = Reader;
3
+
4
+ var util = require("./util/minimal");
5
+
6
+ var BufferReader; // cyclic
7
+
8
+ var LongBits = util.LongBits,
9
+ utf8 = util.utf8;
10
+
11
+ /* istanbul ignore next */
12
+ function indexOutOfRange(reader, writeLength) {
13
+ return RangeError("index out of range: " + reader.pos + " + " + (writeLength || 1) + " > " + reader.len);
14
+ }
15
+
16
+ /**
17
+ * Constructs a new reader instance using the specified buffer.
18
+ * @classdesc Wire format reader using `Uint8Array` if available, otherwise `Array`.
19
+ * @constructor
20
+ * @param {Uint8Array} buffer Buffer to read from
21
+ */
22
+ function Reader(buffer) {
23
+
24
+ /**
25
+ * Read buffer.
26
+ * @type {Uint8Array}
27
+ */
28
+ this.buf = buffer;
29
+
30
+ /**
31
+ * Read buffer position.
32
+ * @type {number}
33
+ */
34
+ this.pos = 0;
35
+
36
+ /**
37
+ * Read buffer length.
38
+ * @type {number}
39
+ */
40
+ this.len = buffer.length;
41
+ }
42
+
43
+ var create_array = typeof Uint8Array !== "undefined"
44
+ ? function create_typed_array(buffer) {
45
+ if (buffer instanceof Uint8Array || Array.isArray(buffer))
46
+ return new Reader(buffer);
47
+ throw Error("illegal buffer");
48
+ }
49
+ /* istanbul ignore next */
50
+ : function create_array(buffer) {
51
+ if (Array.isArray(buffer))
52
+ return new Reader(buffer);
53
+ throw Error("illegal buffer");
54
+ };
55
+
56
+ var create = function create() {
57
+ return util.Buffer
58
+ ? function create_buffer_setup(buffer) {
59
+ return (Reader.create = function create_buffer(buffer) {
60
+ return util.Buffer.isBuffer(buffer)
61
+ ? new BufferReader(buffer)
62
+ /* istanbul ignore next */
63
+ : create_array(buffer);
64
+ })(buffer);
65
+ }
66
+ /* istanbul ignore next */
67
+ : create_array;
68
+ };
69
+
70
+ /**
71
+ * Creates a new reader using the specified buffer.
72
+ * @function
73
+ * @param {Uint8Array|Buffer} buffer Buffer to read from
74
+ * @returns {Reader|BufferReader} A {@link BufferReader} if `buffer` is a Buffer, otherwise a {@link Reader}
75
+ * @throws {Error} If `buffer` is not a valid buffer
76
+ */
77
+ Reader.create = create();
78
+
79
+ Reader.prototype._slice = util.Array.prototype.subarray || /* istanbul ignore next */ util.Array.prototype.slice;
80
+
81
+ /**
82
+ * Reads a varint as an unsigned 32 bit value.
83
+ * @function
84
+ * @returns {number} Value read
85
+ */
86
+ Reader.prototype.uint32 = (function read_uint32_setup() {
87
+ var value = 4294967295; // optimizer type-hint, tends to deopt otherwise (?!)
88
+ return function read_uint32() {
89
+ value = ( this.buf[this.pos] & 127 ) >>> 0; if (this.buf[this.pos++] < 128) return value;
90
+ value = (value | (this.buf[this.pos] & 127) << 7) >>> 0; if (this.buf[this.pos++] < 128) return value;
91
+ value = (value | (this.buf[this.pos] & 127) << 14) >>> 0; if (this.buf[this.pos++] < 128) return value;
92
+ value = (value | (this.buf[this.pos] & 127) << 21) >>> 0; if (this.buf[this.pos++] < 128) return value;
93
+ value = (value | (this.buf[this.pos] & 15) << 28) >>> 0; if (this.buf[this.pos++] < 128) return value;
94
+
95
+ /* istanbul ignore if */
96
+ if ((this.pos += 5) > this.len) {
97
+ this.pos = this.len;
98
+ throw indexOutOfRange(this, 10);
99
+ }
100
+ return value;
101
+ };
102
+ })();
103
+
104
+ /**
105
+ * Reads a varint as a signed 32 bit value.
106
+ * @returns {number} Value read
107
+ */
108
+ Reader.prototype.int32 = function read_int32() {
109
+ return this.uint32() | 0;
110
+ };
111
+
112
+ /**
113
+ * Reads a zig-zag encoded varint as a signed 32 bit value.
114
+ * @returns {number} Value read
115
+ */
116
+ Reader.prototype.sint32 = function read_sint32() {
117
+ var value = this.uint32();
118
+ return value >>> 1 ^ -(value & 1) | 0;
119
+ };
120
+
121
+ /* eslint-disable no-invalid-this */
122
+
123
+ function readLongVarint() {
124
+ // tends to deopt with local vars for octet etc.
125
+ var bits = new LongBits(0, 0);
126
+ var i = 0;
127
+ if (this.len - this.pos > 4) { // fast route (lo)
128
+ for (; i < 4; ++i) {
129
+ // 1st..4th
130
+ bits.lo = (bits.lo | (this.buf[this.pos] & 127) << i * 7) >>> 0;
131
+ if (this.buf[this.pos++] < 128)
132
+ return bits;
133
+ }
134
+ // 5th
135
+ bits.lo = (bits.lo | (this.buf[this.pos] & 127) << 28) >>> 0;
136
+ bits.hi = (bits.hi | (this.buf[this.pos] & 127) >> 4) >>> 0;
137
+ if (this.buf[this.pos++] < 128)
138
+ return bits;
139
+ i = 0;
140
+ } else {
141
+ for (; i < 3; ++i) {
142
+ /* istanbul ignore if */
143
+ if (this.pos >= this.len)
144
+ throw indexOutOfRange(this);
145
+ // 1st..3th
146
+ bits.lo = (bits.lo | (this.buf[this.pos] & 127) << i * 7) >>> 0;
147
+ if (this.buf[this.pos++] < 128)
148
+ return bits;
149
+ }
150
+ // 4th
151
+ bits.lo = (bits.lo | (this.buf[this.pos++] & 127) << i * 7) >>> 0;
152
+ return bits;
153
+ }
154
+ if (this.len - this.pos > 4) { // fast route (hi)
155
+ for (; i < 5; ++i) {
156
+ // 6th..10th
157
+ bits.hi = (bits.hi | (this.buf[this.pos] & 127) << i * 7 + 3) >>> 0;
158
+ if (this.buf[this.pos++] < 128)
159
+ return bits;
160
+ }
161
+ } else {
162
+ for (; i < 5; ++i) {
163
+ /* istanbul ignore if */
164
+ if (this.pos >= this.len)
165
+ throw indexOutOfRange(this);
166
+ // 6th..10th
167
+ bits.hi = (bits.hi | (this.buf[this.pos] & 127) << i * 7 + 3) >>> 0;
168
+ if (this.buf[this.pos++] < 128)
169
+ return bits;
170
+ }
171
+ }
172
+ /* istanbul ignore next */
173
+ throw Error("invalid varint encoding");
174
+ }
175
+
176
+ /* eslint-enable no-invalid-this */
177
+
178
+ /**
179
+ * Reads a varint as a signed 64 bit value.
180
+ * @name Reader#int64
181
+ * @function
182
+ * @returns {Long} Value read
183
+ */
184
+
185
+ /**
186
+ * Reads a varint as an unsigned 64 bit value.
187
+ * @name Reader#uint64
188
+ * @function
189
+ * @returns {Long} Value read
190
+ */
191
+
192
+ /**
193
+ * Reads a zig-zag encoded varint as a signed 64 bit value.
194
+ * @name Reader#sint64
195
+ * @function
196
+ * @returns {Long} Value read
197
+ */
198
+
199
+ /**
200
+ * Reads a varint as a boolean.
201
+ * @returns {boolean} Value read
202
+ */
203
+ Reader.prototype.bool = function read_bool() {
204
+ return this.uint32() !== 0;
205
+ };
206
+
207
+ function readFixed32_end(buf, end) { // note that this uses `end`, not `pos`
208
+ return (buf[end - 4]
209
+ | buf[end - 3] << 8
210
+ | buf[end - 2] << 16
211
+ | buf[end - 1] << 24) >>> 0;
212
+ }
213
+
214
+ /**
215
+ * Reads fixed 32 bits as an unsigned 32 bit integer.
216
+ * @returns {number} Value read
217
+ */
218
+ Reader.prototype.fixed32 = function read_fixed32() {
219
+
220
+ /* istanbul ignore if */
221
+ if (this.pos + 4 > this.len)
222
+ throw indexOutOfRange(this, 4);
223
+
224
+ return readFixed32_end(this.buf, this.pos += 4);
225
+ };
226
+
227
+ /**
228
+ * Reads fixed 32 bits as a signed 32 bit integer.
229
+ * @returns {number} Value read
230
+ */
231
+ Reader.prototype.sfixed32 = function read_sfixed32() {
232
+
233
+ /* istanbul ignore if */
234
+ if (this.pos + 4 > this.len)
235
+ throw indexOutOfRange(this, 4);
236
+
237
+ return readFixed32_end(this.buf, this.pos += 4) | 0;
238
+ };
239
+
240
+ /* eslint-disable no-invalid-this */
241
+
242
+ function readFixed64(/* this: Reader */) {
243
+
244
+ /* istanbul ignore if */
245
+ if (this.pos + 8 > this.len)
246
+ throw indexOutOfRange(this, 8);
247
+
248
+ return new LongBits(readFixed32_end(this.buf, this.pos += 4), readFixed32_end(this.buf, this.pos += 4));
249
+ }
250
+
251
+ /* eslint-enable no-invalid-this */
252
+
253
+ /**
254
+ * Reads fixed 64 bits.
255
+ * @name Reader#fixed64
256
+ * @function
257
+ * @returns {Long} Value read
258
+ */
259
+
260
+ /**
261
+ * Reads zig-zag encoded fixed 64 bits.
262
+ * @name Reader#sfixed64
263
+ * @function
264
+ * @returns {Long} Value read
265
+ */
266
+
267
+ /**
268
+ * Reads a float (32 bit) as a number.
269
+ * @function
270
+ * @returns {number} Value read
271
+ */
272
+ Reader.prototype.float = function read_float() {
273
+
274
+ /* istanbul ignore if */
275
+ if (this.pos + 4 > this.len)
276
+ throw indexOutOfRange(this, 4);
277
+
278
+ var value = util.float.readFloatLE(this.buf, this.pos);
279
+ this.pos += 4;
280
+ return value;
281
+ };
282
+
283
+ /**
284
+ * Reads a double (64 bit float) as a number.
285
+ * @function
286
+ * @returns {number} Value read
287
+ */
288
+ Reader.prototype.double = function read_double() {
289
+
290
+ /* istanbul ignore if */
291
+ if (this.pos + 8 > this.len)
292
+ throw indexOutOfRange(this, 4);
293
+
294
+ var value = util.float.readDoubleLE(this.buf, this.pos);
295
+ this.pos += 8;
296
+ return value;
297
+ };
298
+
299
+ /**
300
+ * Reads a sequence of bytes preceeded by its length as a varint.
301
+ * @returns {Uint8Array} Value read
302
+ */
303
+ Reader.prototype.bytes = function read_bytes() {
304
+ var length = this.uint32(),
305
+ start = this.pos,
306
+ end = this.pos + length;
307
+
308
+ /* istanbul ignore if */
309
+ if (end > this.len)
310
+ throw indexOutOfRange(this, length);
311
+
312
+ this.pos += length;
313
+ if (Array.isArray(this.buf)) // plain array
314
+ return this.buf.slice(start, end);
315
+
316
+ if (start === end) { // fix for IE 10/Win8 and others' subarray returning array of size 1
317
+ var nativeBuffer = util.Buffer;
318
+ return nativeBuffer
319
+ ? nativeBuffer.alloc(0)
320
+ : new this.buf.constructor(0);
321
+ }
322
+ return this._slice.call(this.buf, start, end);
323
+ };
324
+
325
+ /**
326
+ * Reads a string preceeded by its byte length as a varint.
327
+ * @returns {string} Value read
328
+ */
329
+ Reader.prototype.string = function read_string() {
330
+ var bytes = this.bytes();
331
+ return utf8.read(bytes, 0, bytes.length);
332
+ };
333
+
334
+ /**
335
+ * Skips the specified number of bytes if specified, otherwise skips a varint.
336
+ * @param {number} [length] Length if known, otherwise a varint is assumed
337
+ * @returns {Reader} `this`
338
+ */
339
+ Reader.prototype.skip = function skip(length) {
340
+ if (typeof length === "number") {
341
+ /* istanbul ignore if */
342
+ if (this.pos + length > this.len)
343
+ throw indexOutOfRange(this, length);
344
+ this.pos += length;
345
+ } else {
346
+ do {
347
+ /* istanbul ignore if */
348
+ if (this.pos >= this.len)
349
+ throw indexOutOfRange(this);
350
+ } while (this.buf[this.pos++] & 128);
351
+ }
352
+ return this;
353
+ };
354
+
355
+ /**
356
+ * Skips the next element of the specified wire type.
357
+ * @param {number} wireType Wire type received
358
+ * @returns {Reader} `this`
359
+ */
360
+ Reader.prototype.skipType = function(wireType) {
361
+ switch (wireType) {
362
+ case 0:
363
+ this.skip();
364
+ break;
365
+ case 1:
366
+ this.skip(8);
367
+ break;
368
+ case 2:
369
+ this.skip(this.uint32());
370
+ break;
371
+ case 3:
372
+ while ((wireType = this.uint32() & 7) !== 4) {
373
+ this.skipType(wireType);
374
+ }
375
+ break;
376
+ case 5:
377
+ this.skip(4);
378
+ break;
379
+
380
+ /* istanbul ignore next */
381
+ default:
382
+ throw Error("invalid wire type " + wireType + " at offset " + this.pos);
383
+ }
384
+ return this;
385
+ };
386
+
387
+ Reader._configure = function(BufferReader_) {
388
+ BufferReader = BufferReader_;
389
+ Reader.create = create();
390
+ BufferReader._configure();
391
+
392
+ var fn = util.Long ? "toLong" : /* istanbul ignore next */ "toNumber";
393
+ util.merge(Reader.prototype, {
394
+
395
+ int64: function read_int64() {
396
+ return readLongVarint.call(this)[fn](false);
397
+ },
398
+
399
+ uint64: function read_uint64() {
400
+ return readLongVarint.call(this)[fn](true);
401
+ },
402
+
403
+ sint64: function read_sint64() {
404
+ return readLongVarint.call(this).zzDecode()[fn](false);
405
+ },
406
+
407
+ fixed64: function read_fixed64() {
408
+ return readFixed64.call(this)[fn](true);
409
+ },
410
+
411
+ sfixed64: function read_sfixed64() {
412
+ return readFixed64.call(this)[fn](false);
413
+ }
414
+
415
+ });
416
+ };