protobufjs 8.6.0 → 8.6.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 (108) hide show
  1. package/LICENSE +39 -39
  2. package/README.md +441 -441
  3. package/dist/light/protobuf.js +8473 -8516
  4. package/dist/light/protobuf.js.map +1 -1
  5. package/dist/light/protobuf.min.js +3 -3
  6. package/dist/light/protobuf.min.js.map +1 -1
  7. package/dist/minimal/protobuf.js +3018 -3061
  8. package/dist/minimal/protobuf.js.map +1 -1
  9. package/dist/minimal/protobuf.min.js +3 -3
  10. package/dist/minimal/protobuf.min.js.map +1 -1
  11. package/dist/protobuf.js +10376 -10419
  12. package/dist/protobuf.js.map +1 -1
  13. package/dist/protobuf.min.js +3 -3
  14. package/dist/protobuf.min.js.map +1 -1
  15. package/ext/README.md +70 -70
  16. package/ext/debug/README.md +4 -4
  17. package/ext/debug/index.js +71 -71
  18. package/ext/descriptor/README.md +5 -5
  19. package/ext/descriptor/index.d.ts +2 -2
  20. package/ext/descriptor/index.js +2 -2
  21. package/ext/descriptor.d.ts +87 -86
  22. package/ext/descriptor.generated.d.ts +14 -6
  23. package/ext/descriptor.js +1354 -1303
  24. package/ext/protojson.LICENSE +201 -201
  25. package/ext/protojson.d.ts +20 -20
  26. package/ext/protojson.js +925 -903
  27. package/ext/textformat.d.ts +19 -19
  28. package/ext/textformat.js +1256 -1256
  29. package/google/LICENSE +27 -27
  30. package/google/README.md +1 -1
  31. package/google/api/annotations.json +82 -82
  32. package/google/api/annotations.proto +10 -10
  33. package/google/api/http.json +85 -85
  34. package/google/api/http.proto +30 -30
  35. package/google/protobuf/api.json +117 -117
  36. package/google/protobuf/api.proto +33 -33
  37. package/google/protobuf/compiler/plugin.json +126 -126
  38. package/google/protobuf/compiler/plugin.proto +47 -47
  39. package/google/protobuf/descriptor.json +1381 -1381
  40. package/google/protobuf/descriptor.proto +534 -534
  41. package/google/protobuf/source_context.json +19 -19
  42. package/google/protobuf/source_context.proto +7 -7
  43. package/google/protobuf/type.json +201 -201
  44. package/google/protobuf/type.proto +89 -89
  45. package/index.d.ts +10 -18
  46. package/index.js +4 -4
  47. package/light.d.ts +2 -2
  48. package/light.js +3 -3
  49. package/minimal.d.ts +2 -2
  50. package/minimal.js +4 -4
  51. package/package.json +93 -93
  52. package/src/common.js +399 -399
  53. package/src/converter.js +344 -344
  54. package/src/decoder.js +208 -208
  55. package/src/encoder.js +111 -111
  56. package/src/enum.js +231 -231
  57. package/src/field.js +497 -497
  58. package/src/index-light.js +104 -104
  59. package/src/index-minimal.js +36 -36
  60. package/src/index.js +12 -12
  61. package/src/mapfield.js +136 -136
  62. package/src/message.js +137 -137
  63. package/src/method.js +175 -175
  64. package/src/namespace.js +565 -565
  65. package/src/object.js +382 -382
  66. package/src/oneof.js +225 -225
  67. package/src/parse.js +1068 -1068
  68. package/src/reader.js +543 -543
  69. package/src/reader_buffer.js +72 -72
  70. package/src/root.js +416 -416
  71. package/src/roots.js +18 -18
  72. package/src/rpc/service.js +148 -148
  73. package/src/rpc.js +36 -36
  74. package/src/service.js +198 -198
  75. package/src/tokenize.js +421 -421
  76. package/src/type.js +655 -655
  77. package/src/types.js +196 -196
  78. package/src/typescript.js +25 -25
  79. package/src/util/aspromise.d.ts +13 -13
  80. package/src/util/aspromise.js +52 -52
  81. package/src/util/base64.d.ts +32 -32
  82. package/src/util/base64.js +146 -146
  83. package/src/util/codegen.d.ts +31 -31
  84. package/src/util/codegen.js +113 -113
  85. package/src/util/eventemitter.d.ts +45 -45
  86. package/src/util/eventemitter.js +86 -86
  87. package/src/util/fetch.d.ts +56 -56
  88. package/src/util/fetch.js +112 -112
  89. package/src/util/float.d.ts +83 -83
  90. package/src/util/float.js +335 -335
  91. package/src/util/fs.js +11 -11
  92. package/src/util/longbits.js +200 -200
  93. package/src/util/minimal.js +515 -518
  94. package/src/util/path.d.ts +22 -22
  95. package/src/util/path.js +72 -72
  96. package/src/util/patterns.js +7 -7
  97. package/src/util/pool.d.ts +32 -32
  98. package/src/util/pool.js +48 -48
  99. package/src/util/utf8.d.ts +24 -24
  100. package/src/util/utf8.js +130 -130
  101. package/src/util.js +242 -242
  102. package/src/verifier.js +180 -180
  103. package/src/wrappers.js +106 -106
  104. package/src/writer.js +495 -495
  105. package/src/writer_buffer.js +102 -102
  106. package/tsconfig.json +6 -6
  107. package/src/util/inquire.d.ts +0 -10
  108. package/src/util/inquire.js +0 -38
package/src/writer.js CHANGED
@@ -1,495 +1,495 @@
1
- "use strict";
2
- module.exports = Writer;
3
-
4
- var util = require("./util/minimal");
5
-
6
- var BufferWriter; // cyclic
7
-
8
- var LongBits = util.LongBits,
9
- base64 = util.base64,
10
- utf8 = util.utf8;
11
-
12
- /**
13
- * Constructs a new writer operation instance.
14
- * @classdesc Scheduled writer operation.
15
- * @constructor
16
- * @param {function(*, Uint8Array, number)} fn Function to call
17
- * @param {number} len Value byte length
18
- * @param {*} val Value to write
19
- * @ignore
20
- */
21
- function Op(fn, len, val) {
22
-
23
- /**
24
- * Function to call.
25
- * @type {function(Uint8Array, number, *)}
26
- */
27
- this.fn = fn;
28
-
29
- /**
30
- * Value byte length.
31
- * @type {number}
32
- */
33
- this.len = len;
34
-
35
- /**
36
- * Next operation.
37
- * @type {Writer.Op|undefined}
38
- */
39
- this.next = undefined;
40
-
41
- /**
42
- * Value to write.
43
- * @type {*}
44
- */
45
- this.val = val; // type varies
46
- }
47
-
48
- /* istanbul ignore next */
49
- function noop() {} // eslint-disable-line no-empty-function
50
-
51
- /**
52
- * Constructs a new writer state instance.
53
- * @classdesc Copied writer state.
54
- * @memberof Writer
55
- * @constructor
56
- * @param {Writer} writer Writer to copy state from
57
- * @ignore
58
- */
59
- function State(writer) {
60
-
61
- /**
62
- * Current head.
63
- * @type {Writer.Op}
64
- */
65
- this.head = writer.head;
66
-
67
- /**
68
- * Current tail.
69
- * @type {Writer.Op}
70
- */
71
- this.tail = writer.tail;
72
-
73
- /**
74
- * Current buffer length.
75
- * @type {number}
76
- */
77
- this.len = writer.len;
78
-
79
- /**
80
- * Next state.
81
- * @type {State|null}
82
- */
83
- this.next = writer.states;
84
- }
85
-
86
- /**
87
- * Constructs a new writer instance.
88
- * @classdesc Wire format writer using `Uint8Array` if available, otherwise `Array`.
89
- * @constructor
90
- */
91
- function Writer() {
92
-
93
- /**
94
- * Current length.
95
- * @type {number}
96
- */
97
- this.len = 0;
98
-
99
- /**
100
- * Operations head.
101
- * @type {Object}
102
- */
103
- this.head = new Op(noop, 0, 0);
104
-
105
- /**
106
- * Operations tail
107
- * @type {Object}
108
- */
109
- this.tail = this.head;
110
-
111
- /**
112
- * Linked forked states.
113
- * @type {Object|null}
114
- */
115
- this.states = null;
116
-
117
- // When a value is written, the writer calculates its byte length and puts it into a linked
118
- // list of operations to perform when finish() is called. This both allows us to allocate
119
- // buffers of the exact required size and reduces the amount of work we have to do compared
120
- // to first calculating over objects and then encoding over objects. In our case, the encoding
121
- // part is just a linked list walk calling operations with already prepared values.
122
- }
123
-
124
- var create = function create() {
125
- return util.Buffer
126
- ? function create_buffer_setup() {
127
- return (Writer.create = function create_buffer() {
128
- return new BufferWriter();
129
- })();
130
- }
131
- /* istanbul ignore next */
132
- : function create_array() {
133
- return new Writer();
134
- };
135
- };
136
-
137
- /**
138
- * Creates a new writer.
139
- * @function
140
- * @returns {BufferWriter|Writer} A {@link BufferWriter} when Buffers are supported, otherwise a {@link Writer}
141
- */
142
- Writer.create = create();
143
-
144
- /**
145
- * Allocates a buffer of the specified size.
146
- * @param {number} size Buffer size
147
- * @returns {Uint8Array} Buffer
148
- */
149
- Writer.alloc = function alloc(size) {
150
- return new util.Array(size);
151
- };
152
-
153
- // Use Uint8Array buffer pool in the browser, just like node does with buffers
154
- /* istanbul ignore else */
155
- if (util.Array !== Array)
156
- Writer.alloc = util.pool(Writer.alloc, util.Array.prototype.subarray);
157
-
158
- /**
159
- * Pushes a new operation to the queue.
160
- * @param {function(Uint8Array, number, *)} fn Function to call
161
- * @param {number} len Value byte length
162
- * @param {number} val Value to write
163
- * @returns {Writer} `this`
164
- * @private
165
- */
166
- Writer.prototype._push = function push(fn, len, val) {
167
- this.tail = this.tail.next = new Op(fn, len, val);
168
- this.len += len;
169
- return this;
170
- };
171
-
172
- function writeByte(val, buf, pos) {
173
- buf[pos] = val & 255;
174
- }
175
-
176
- function writeStringAscii(val, buf, pos) {
177
- for (var i = 0; i < val.length;)
178
- buf[pos++] = val.charCodeAt(i++);
179
- }
180
-
181
- function writeVarint32(val, buf, pos) {
182
- while (val > 127) {
183
- buf[pos++] = val & 127 | 128;
184
- val >>>= 7;
185
- }
186
- buf[pos] = val;
187
- }
188
-
189
- /**
190
- * Constructs a new varint writer operation instance.
191
- * @classdesc Scheduled varint writer operation.
192
- * @extends Op
193
- * @constructor
194
- * @param {number} len Value byte length
195
- * @param {number} val Value to write
196
- * @ignore
197
- */
198
- function VarintOp(len, val) {
199
- this.len = len;
200
- this.next = undefined;
201
- this.val = val;
202
- }
203
-
204
- VarintOp.prototype = Object.create(Op.prototype);
205
- VarintOp.prototype.fn = writeVarint32;
206
-
207
- /**
208
- * Writes an unsigned 32 bit value as a varint.
209
- * @param {number} value Value to write
210
- * @returns {Writer} `this`
211
- */
212
- Writer.prototype.uint32 = function write_uint32(value) {
213
- // here, the call to this.push has been inlined and a varint specific Op subclass is used.
214
- // uint32 is by far the most frequently used operation and benefits significantly from this.
215
- this.len += (this.tail = this.tail.next = new VarintOp(
216
- (value = value >>> 0)
217
- < 128 ? 1
218
- : value < 16384 ? 2
219
- : value < 2097152 ? 3
220
- : value < 268435456 ? 4
221
- : 5,
222
- value)).len;
223
- return this;
224
- };
225
-
226
- /**
227
- * Writes a signed 32 bit value as a varint.
228
- * @function
229
- * @param {number} value Value to write
230
- * @returns {Writer} `this`
231
- */
232
- Writer.prototype.int32 = function write_int32(value) {
233
- return (value |= 0) < 0
234
- ? this._push(writeVarint64, 10, LongBits.fromNumber(value)) // 10 bytes per spec
235
- : this.uint32(value);
236
- };
237
-
238
- /**
239
- * Writes a 32 bit value as a varint, zig-zag encoded.
240
- * @param {number} value Value to write
241
- * @returns {Writer} `this`
242
- */
243
- Writer.prototype.sint32 = function write_sint32(value) {
244
- return this.uint32((value << 1 ^ value >> 31) >>> 0);
245
- };
246
-
247
- function writeVarint64(val, buf, pos) {
248
- var lo = val.lo,
249
- hi = val.hi;
250
- while (hi) {
251
- buf[pos++] = lo & 127 | 128;
252
- lo = (lo >>> 7 | hi << 25) >>> 0;
253
- hi >>>= 7;
254
- }
255
- while (lo > 127) {
256
- buf[pos++] = lo & 127 | 128;
257
- lo = lo >>> 7;
258
- }
259
- buf[pos++] = lo;
260
- }
261
-
262
- /**
263
- * Writes an unsigned 64 bit value as a varint.
264
- * @param {Long|number|string} value Value to write
265
- * @returns {Writer} `this`
266
- * @throws {TypeError} If `value` is a string and no long library is present.
267
- */
268
- Writer.prototype.uint64 = function write_uint64(value) {
269
- var bits = LongBits.from(value);
270
- return this._push(writeVarint64, bits.length(), bits);
271
- };
272
-
273
- /**
274
- * Writes a signed 64 bit value as a varint.
275
- * @function
276
- * @param {Long|number|string} value Value to write
277
- * @returns {Writer} `this`
278
- * @throws {TypeError} If `value` is a string and no long library is present.
279
- */
280
- Writer.prototype.int64 = Writer.prototype.uint64;
281
-
282
- /**
283
- * Writes a signed 64 bit value as a varint, zig-zag encoded.
284
- * @param {Long|number|string} value Value to write
285
- * @returns {Writer} `this`
286
- * @throws {TypeError} If `value` is a string and no long library is present.
287
- */
288
- Writer.prototype.sint64 = function write_sint64(value) {
289
- var bits = LongBits.from(value).zzEncode();
290
- return this._push(writeVarint64, bits.length(), bits);
291
- };
292
-
293
- /**
294
- * Writes a boolish value as a varint.
295
- * @param {boolean} value Value to write
296
- * @returns {Writer} `this`
297
- */
298
- Writer.prototype.bool = function write_bool(value) {
299
- return this._push(writeByte, 1, value ? 1 : 0);
300
- };
301
-
302
- function writeFixed32(val, buf, pos) {
303
- buf[pos ] = val & 255;
304
- buf[pos + 1] = val >>> 8 & 255;
305
- buf[pos + 2] = val >>> 16 & 255;
306
- buf[pos + 3] = val >>> 24;
307
- }
308
-
309
- /**
310
- * Writes an unsigned 32 bit value as fixed 32 bits.
311
- * @param {number} value Value to write
312
- * @returns {Writer} `this`
313
- */
314
- Writer.prototype.fixed32 = function write_fixed32(value) {
315
- return this._push(writeFixed32, 4, value >>> 0);
316
- };
317
-
318
- /**
319
- * Writes a signed 32 bit value as fixed 32 bits.
320
- * @function
321
- * @param {number} value Value to write
322
- * @returns {Writer} `this`
323
- */
324
- Writer.prototype.sfixed32 = Writer.prototype.fixed32;
325
-
326
- /**
327
- * Writes an unsigned 64 bit value as fixed 64 bits.
328
- * @param {Long|number|string} value Value to write
329
- * @returns {Writer} `this`
330
- * @throws {TypeError} If `value` is a string and no long library is present.
331
- */
332
- Writer.prototype.fixed64 = function write_fixed64(value) {
333
- var bits = LongBits.from(value);
334
- return this._push(writeFixed32, 4, bits.lo)._push(writeFixed32, 4, bits.hi);
335
- };
336
-
337
- /**
338
- * Writes a signed 64 bit value as fixed 64 bits.
339
- * @function
340
- * @param {Long|number|string} value Value to write
341
- * @returns {Writer} `this`
342
- * @throws {TypeError} If `value` is a string and no long library is present.
343
- */
344
- Writer.prototype.sfixed64 = Writer.prototype.fixed64;
345
-
346
- /**
347
- * Writes a float (32 bit).
348
- * @function
349
- * @param {number} value Value to write
350
- * @returns {Writer} `this`
351
- */
352
- Writer.prototype.float = function write_float(value) {
353
- return this._push(util.float.writeFloatLE, 4, value);
354
- };
355
-
356
- /**
357
- * Writes a double (64 bit float).
358
- * @function
359
- * @param {number} value Value to write
360
- * @returns {Writer} `this`
361
- */
362
- Writer.prototype.double = function write_double(value) {
363
- return this._push(util.float.writeDoubleLE, 8, value);
364
- };
365
-
366
- var writeBytes = util.Array.prototype.set
367
- ? function writeBytes_set(val, buf, pos) {
368
- buf.set(val, pos); // also works for plain array values
369
- }
370
- /* istanbul ignore next */
371
- : function writeBytes_for(val, buf, pos) {
372
- for (var i = 0; i < val.length; ++i)
373
- buf[pos + i] = val[i];
374
- };
375
-
376
- /**
377
- * Writes a sequence of bytes.
378
- * @param {Uint8Array|string} value Buffer or base64 encoded string to write
379
- * @returns {Writer} `this`
380
- */
381
- Writer.prototype.bytes = function write_bytes(value) {
382
- var len = value.length >>> 0;
383
- if (!len)
384
- return this._push(writeByte, 1, 0);
385
- if (util.isString(value)) {
386
- var buf = Writer.alloc(len = base64.length(value));
387
- base64.decode(value, buf, 0);
388
- value = buf;
389
- }
390
- return this.uint32(len)._push(writeBytes, len, value);
391
- };
392
-
393
- /**
394
- * Writes raw bytes without a tag or length prefix.
395
- * @param {Uint8Array} value Raw bytes
396
- * @returns {Writer} `this`
397
- */
398
- Writer.prototype.raw = function write_raw(value) {
399
- var len = value.length >>> 0;
400
- return len ? this._push(writeBytes, len, value) : this;
401
- };
402
-
403
- /**
404
- * Writes a string.
405
- * @param {string} value Value to write
406
- * @returns {Writer} `this`
407
- */
408
- Writer.prototype.string = function write_string(value) {
409
- var len = utf8.length(value);
410
- return len
411
- ? this.uint32(len)._push(len === value.length ? writeStringAscii : utf8.write, len, value)
412
- : this._push(writeByte, 1, 0);
413
- };
414
-
415
- /**
416
- * Forks this writer's state by pushing it to a stack.
417
- * Calling {@link Writer#reset|reset} or {@link Writer#ldelim|ldelim} resets the writer to the previous state.
418
- * @returns {Writer} `this`
419
- */
420
- Writer.prototype.fork = function fork() {
421
- this.states = new State(this);
422
- this.head = this.tail = new Op(noop, 0, 0);
423
- this.len = 0;
424
- return this;
425
- };
426
-
427
- /**
428
- * Resets this instance to the last state.
429
- * @returns {Writer} `this`
430
- */
431
- Writer.prototype.reset = function reset() {
432
- if (this.states) {
433
- this.head = this.states.head;
434
- this.tail = this.states.tail;
435
- this.len = this.states.len;
436
- this.states = this.states.next;
437
- } else {
438
- this.head = this.tail = new Op(noop, 0, 0);
439
- this.len = 0;
440
- }
441
- return this;
442
- };
443
-
444
- /**
445
- * Resets to the last state and appends the fork state's current write length as a varint followed by its operations.
446
- * @returns {Writer} `this`
447
- */
448
- Writer.prototype.ldelim = function ldelim() {
449
- var head = this.head,
450
- tail = this.tail,
451
- len = this.len;
452
- this.reset().uint32(len);
453
- if (len) {
454
- this.tail.next = head.next; // skip noop
455
- this.tail = tail;
456
- this.len += len;
457
- }
458
- return this;
459
- };
460
-
461
- /**
462
- * Finishes the write operation.
463
- * @returns {Uint8Array} Finished buffer
464
- */
465
- Writer.prototype.finish = function finish() {
466
- return this.finishInto(this.constructor.alloc(this.len), 0);
467
- };
468
-
469
- /**
470
- * Finishes the write operation, writing into the provided buffer.
471
- * The caller must ensure that `buf` has enough space starting at `offset`
472
- * to hold {@link Writer#len} bytes.
473
- * @param {T} buf Target buffer
474
- * @param {number} [offset=0] Offset to start writing at
475
- * @returns {T} The provided buffer
476
- * @template T extends Uint8Array
477
- */
478
- Writer.prototype.finishInto = function finishInto(buf, offset) {
479
- if (offset === undefined)
480
- offset = 0;
481
- var head = this.head.next,
482
- pos = offset;
483
- while (head) {
484
- head.fn(head.val, buf, pos);
485
- pos += head.len;
486
- head = head.next;
487
- }
488
- return buf;
489
- };
490
-
491
- Writer._configure = function(BufferWriter_) {
492
- BufferWriter = BufferWriter_;
493
- Writer.create = create();
494
- BufferWriter._configure();
495
- };
1
+ "use strict";
2
+ module.exports = Writer;
3
+
4
+ var util = require("./util/minimal");
5
+
6
+ var BufferWriter; // cyclic
7
+
8
+ var LongBits = util.LongBits,
9
+ base64 = util.base64,
10
+ utf8 = util.utf8;
11
+
12
+ /**
13
+ * Constructs a new writer operation instance.
14
+ * @classdesc Scheduled writer operation.
15
+ * @constructor
16
+ * @param {function(*, Uint8Array, number)} fn Function to call
17
+ * @param {number} len Value byte length
18
+ * @param {*} val Value to write
19
+ * @ignore
20
+ */
21
+ function Op(fn, len, val) {
22
+
23
+ /**
24
+ * Function to call.
25
+ * @type {function(Uint8Array, number, *)}
26
+ */
27
+ this.fn = fn;
28
+
29
+ /**
30
+ * Value byte length.
31
+ * @type {number}
32
+ */
33
+ this.len = len;
34
+
35
+ /**
36
+ * Next operation.
37
+ * @type {Writer.Op|undefined}
38
+ */
39
+ this.next = undefined;
40
+
41
+ /**
42
+ * Value to write.
43
+ * @type {*}
44
+ */
45
+ this.val = val; // type varies
46
+ }
47
+
48
+ /* istanbul ignore next */
49
+ function noop() {} // eslint-disable-line no-empty-function
50
+
51
+ /**
52
+ * Constructs a new writer state instance.
53
+ * @classdesc Copied writer state.
54
+ * @memberof Writer
55
+ * @constructor
56
+ * @param {Writer} writer Writer to copy state from
57
+ * @ignore
58
+ */
59
+ function State(writer) {
60
+
61
+ /**
62
+ * Current head.
63
+ * @type {Writer.Op}
64
+ */
65
+ this.head = writer.head;
66
+
67
+ /**
68
+ * Current tail.
69
+ * @type {Writer.Op}
70
+ */
71
+ this.tail = writer.tail;
72
+
73
+ /**
74
+ * Current buffer length.
75
+ * @type {number}
76
+ */
77
+ this.len = writer.len;
78
+
79
+ /**
80
+ * Next state.
81
+ * @type {State|null}
82
+ */
83
+ this.next = writer.states;
84
+ }
85
+
86
+ /**
87
+ * Constructs a new writer instance.
88
+ * @classdesc Wire format writer using `Uint8Array` if available, otherwise `Array`.
89
+ * @constructor
90
+ */
91
+ function Writer() {
92
+
93
+ /**
94
+ * Current length.
95
+ * @type {number}
96
+ */
97
+ this.len = 0;
98
+
99
+ /**
100
+ * Operations head.
101
+ * @type {Object}
102
+ */
103
+ this.head = new Op(noop, 0, 0);
104
+
105
+ /**
106
+ * Operations tail
107
+ * @type {Object}
108
+ */
109
+ this.tail = this.head;
110
+
111
+ /**
112
+ * Linked forked states.
113
+ * @type {Object|null}
114
+ */
115
+ this.states = null;
116
+
117
+ // When a value is written, the writer calculates its byte length and puts it into a linked
118
+ // list of operations to perform when finish() is called. This both allows us to allocate
119
+ // buffers of the exact required size and reduces the amount of work we have to do compared
120
+ // to first calculating over objects and then encoding over objects. In our case, the encoding
121
+ // part is just a linked list walk calling operations with already prepared values.
122
+ }
123
+
124
+ var create = function create() {
125
+ return util.Buffer
126
+ ? function create_buffer_setup() {
127
+ return (Writer.create = function create_buffer() {
128
+ return new BufferWriter();
129
+ })();
130
+ }
131
+ /* istanbul ignore next */
132
+ : function create_array() {
133
+ return new Writer();
134
+ };
135
+ };
136
+
137
+ /**
138
+ * Creates a new writer.
139
+ * @function
140
+ * @returns {BufferWriter|Writer} A {@link BufferWriter} when Buffers are supported, otherwise a {@link Writer}
141
+ */
142
+ Writer.create = create();
143
+
144
+ /**
145
+ * Allocates a buffer of the specified size.
146
+ * @param {number} size Buffer size
147
+ * @returns {Uint8Array} Buffer
148
+ */
149
+ Writer.alloc = function alloc(size) {
150
+ return new util.Array(size);
151
+ };
152
+
153
+ // Use Uint8Array buffer pool in the browser, just like node does with buffers
154
+ /* istanbul ignore else */
155
+ if (util.Array !== Array)
156
+ Writer.alloc = util.pool(Writer.alloc, util.Array.prototype.subarray);
157
+
158
+ /**
159
+ * Pushes a new operation to the queue.
160
+ * @param {function(Uint8Array, number, *)} fn Function to call
161
+ * @param {number} len Value byte length
162
+ * @param {number} val Value to write
163
+ * @returns {Writer} `this`
164
+ * @private
165
+ */
166
+ Writer.prototype._push = function push(fn, len, val) {
167
+ this.tail = this.tail.next = new Op(fn, len, val);
168
+ this.len += len;
169
+ return this;
170
+ };
171
+
172
+ function writeByte(val, buf, pos) {
173
+ buf[pos] = val & 255;
174
+ }
175
+
176
+ function writeStringAscii(val, buf, pos) {
177
+ for (var i = 0; i < val.length;)
178
+ buf[pos++] = val.charCodeAt(i++);
179
+ }
180
+
181
+ function writeVarint32(val, buf, pos) {
182
+ while (val > 127) {
183
+ buf[pos++] = val & 127 | 128;
184
+ val >>>= 7;
185
+ }
186
+ buf[pos] = val;
187
+ }
188
+
189
+ /**
190
+ * Constructs a new varint writer operation instance.
191
+ * @classdesc Scheduled varint writer operation.
192
+ * @extends Op
193
+ * @constructor
194
+ * @param {number} len Value byte length
195
+ * @param {number} val Value to write
196
+ * @ignore
197
+ */
198
+ function VarintOp(len, val) {
199
+ this.len = len;
200
+ this.next = undefined;
201
+ this.val = val;
202
+ }
203
+
204
+ VarintOp.prototype = Object.create(Op.prototype);
205
+ VarintOp.prototype.fn = writeVarint32;
206
+
207
+ /**
208
+ * Writes an unsigned 32 bit value as a varint.
209
+ * @param {number} value Value to write
210
+ * @returns {Writer} `this`
211
+ */
212
+ Writer.prototype.uint32 = function write_uint32(value) {
213
+ // here, the call to this.push has been inlined and a varint specific Op subclass is used.
214
+ // uint32 is by far the most frequently used operation and benefits significantly from this.
215
+ this.len += (this.tail = this.tail.next = new VarintOp(
216
+ (value = value >>> 0)
217
+ < 128 ? 1
218
+ : value < 16384 ? 2
219
+ : value < 2097152 ? 3
220
+ : value < 268435456 ? 4
221
+ : 5,
222
+ value)).len;
223
+ return this;
224
+ };
225
+
226
+ /**
227
+ * Writes a signed 32 bit value as a varint.
228
+ * @function
229
+ * @param {number} value Value to write
230
+ * @returns {Writer} `this`
231
+ */
232
+ Writer.prototype.int32 = function write_int32(value) {
233
+ return (value |= 0) < 0
234
+ ? this._push(writeVarint64, 10, LongBits.fromNumber(value)) // 10 bytes per spec
235
+ : this.uint32(value);
236
+ };
237
+
238
+ /**
239
+ * Writes a 32 bit value as a varint, zig-zag encoded.
240
+ * @param {number} value Value to write
241
+ * @returns {Writer} `this`
242
+ */
243
+ Writer.prototype.sint32 = function write_sint32(value) {
244
+ return this.uint32((value << 1 ^ value >> 31) >>> 0);
245
+ };
246
+
247
+ function writeVarint64(val, buf, pos) {
248
+ var lo = val.lo,
249
+ hi = val.hi;
250
+ while (hi) {
251
+ buf[pos++] = lo & 127 | 128;
252
+ lo = (lo >>> 7 | hi << 25) >>> 0;
253
+ hi >>>= 7;
254
+ }
255
+ while (lo > 127) {
256
+ buf[pos++] = lo & 127 | 128;
257
+ lo = lo >>> 7;
258
+ }
259
+ buf[pos++] = lo;
260
+ }
261
+
262
+ /**
263
+ * Writes an unsigned 64 bit value as a varint.
264
+ * @param {Long|number|string} value Value to write
265
+ * @returns {Writer} `this`
266
+ * @throws {TypeError} If `value` is a string and no long library is present.
267
+ */
268
+ Writer.prototype.uint64 = function write_uint64(value) {
269
+ var bits = LongBits.from(value);
270
+ return this._push(writeVarint64, bits.length(), bits);
271
+ };
272
+
273
+ /**
274
+ * Writes a signed 64 bit value as a varint.
275
+ * @function
276
+ * @param {Long|number|string} value Value to write
277
+ * @returns {Writer} `this`
278
+ * @throws {TypeError} If `value` is a string and no long library is present.
279
+ */
280
+ Writer.prototype.int64 = Writer.prototype.uint64;
281
+
282
+ /**
283
+ * Writes a signed 64 bit value as a varint, zig-zag encoded.
284
+ * @param {Long|number|string} value Value to write
285
+ * @returns {Writer} `this`
286
+ * @throws {TypeError} If `value` is a string and no long library is present.
287
+ */
288
+ Writer.prototype.sint64 = function write_sint64(value) {
289
+ var bits = LongBits.from(value).zzEncode();
290
+ return this._push(writeVarint64, bits.length(), bits);
291
+ };
292
+
293
+ /**
294
+ * Writes a boolish value as a varint.
295
+ * @param {boolean} value Value to write
296
+ * @returns {Writer} `this`
297
+ */
298
+ Writer.prototype.bool = function write_bool(value) {
299
+ return this._push(writeByte, 1, value ? 1 : 0);
300
+ };
301
+
302
+ function writeFixed32(val, buf, pos) {
303
+ buf[pos ] = val & 255;
304
+ buf[pos + 1] = val >>> 8 & 255;
305
+ buf[pos + 2] = val >>> 16 & 255;
306
+ buf[pos + 3] = val >>> 24;
307
+ }
308
+
309
+ /**
310
+ * Writes an unsigned 32 bit value as fixed 32 bits.
311
+ * @param {number} value Value to write
312
+ * @returns {Writer} `this`
313
+ */
314
+ Writer.prototype.fixed32 = function write_fixed32(value) {
315
+ return this._push(writeFixed32, 4, value >>> 0);
316
+ };
317
+
318
+ /**
319
+ * Writes a signed 32 bit value as fixed 32 bits.
320
+ * @function
321
+ * @param {number} value Value to write
322
+ * @returns {Writer} `this`
323
+ */
324
+ Writer.prototype.sfixed32 = Writer.prototype.fixed32;
325
+
326
+ /**
327
+ * Writes an unsigned 64 bit value as fixed 64 bits.
328
+ * @param {Long|number|string} value Value to write
329
+ * @returns {Writer} `this`
330
+ * @throws {TypeError} If `value` is a string and no long library is present.
331
+ */
332
+ Writer.prototype.fixed64 = function write_fixed64(value) {
333
+ var bits = LongBits.from(value);
334
+ return this._push(writeFixed32, 4, bits.lo)._push(writeFixed32, 4, bits.hi);
335
+ };
336
+
337
+ /**
338
+ * Writes a signed 64 bit value as fixed 64 bits.
339
+ * @function
340
+ * @param {Long|number|string} value Value to write
341
+ * @returns {Writer} `this`
342
+ * @throws {TypeError} If `value` is a string and no long library is present.
343
+ */
344
+ Writer.prototype.sfixed64 = Writer.prototype.fixed64;
345
+
346
+ /**
347
+ * Writes a float (32 bit).
348
+ * @function
349
+ * @param {number} value Value to write
350
+ * @returns {Writer} `this`
351
+ */
352
+ Writer.prototype.float = function write_float(value) {
353
+ return this._push(util.float.writeFloatLE, 4, value);
354
+ };
355
+
356
+ /**
357
+ * Writes a double (64 bit float).
358
+ * @function
359
+ * @param {number} value Value to write
360
+ * @returns {Writer} `this`
361
+ */
362
+ Writer.prototype.double = function write_double(value) {
363
+ return this._push(util.float.writeDoubleLE, 8, value);
364
+ };
365
+
366
+ var writeBytes = util.Array.prototype.set
367
+ ? function writeBytes_set(val, buf, pos) {
368
+ buf.set(val, pos); // also works for plain array values
369
+ }
370
+ /* istanbul ignore next */
371
+ : function writeBytes_for(val, buf, pos) {
372
+ for (var i = 0; i < val.length; ++i)
373
+ buf[pos + i] = val[i];
374
+ };
375
+
376
+ /**
377
+ * Writes a sequence of bytes.
378
+ * @param {Uint8Array|string} value Buffer or base64 encoded string to write
379
+ * @returns {Writer} `this`
380
+ */
381
+ Writer.prototype.bytes = function write_bytes(value) {
382
+ var len = value.length >>> 0;
383
+ if (!len)
384
+ return this._push(writeByte, 1, 0);
385
+ if (util.isString(value)) {
386
+ var buf = Writer.alloc(len = base64.length(value));
387
+ base64.decode(value, buf, 0);
388
+ value = buf;
389
+ }
390
+ return this.uint32(len)._push(writeBytes, len, value);
391
+ };
392
+
393
+ /**
394
+ * Writes raw bytes without a tag or length prefix.
395
+ * @param {Uint8Array} value Raw bytes
396
+ * @returns {Writer} `this`
397
+ */
398
+ Writer.prototype.raw = function write_raw(value) {
399
+ var len = value.length >>> 0;
400
+ return len ? this._push(writeBytes, len, value) : this;
401
+ };
402
+
403
+ /**
404
+ * Writes a string.
405
+ * @param {string} value Value to write
406
+ * @returns {Writer} `this`
407
+ */
408
+ Writer.prototype.string = function write_string(value) {
409
+ var len = utf8.length(value);
410
+ return len
411
+ ? this.uint32(len)._push(len === value.length ? writeStringAscii : utf8.write, len, value)
412
+ : this._push(writeByte, 1, 0);
413
+ };
414
+
415
+ /**
416
+ * Forks this writer's state by pushing it to a stack.
417
+ * Calling {@link Writer#reset|reset} or {@link Writer#ldelim|ldelim} resets the writer to the previous state.
418
+ * @returns {Writer} `this`
419
+ */
420
+ Writer.prototype.fork = function fork() {
421
+ this.states = new State(this);
422
+ this.head = this.tail = new Op(noop, 0, 0);
423
+ this.len = 0;
424
+ return this;
425
+ };
426
+
427
+ /**
428
+ * Resets this instance to the last state.
429
+ * @returns {Writer} `this`
430
+ */
431
+ Writer.prototype.reset = function reset() {
432
+ if (this.states) {
433
+ this.head = this.states.head;
434
+ this.tail = this.states.tail;
435
+ this.len = this.states.len;
436
+ this.states = this.states.next;
437
+ } else {
438
+ this.head = this.tail = new Op(noop, 0, 0);
439
+ this.len = 0;
440
+ }
441
+ return this;
442
+ };
443
+
444
+ /**
445
+ * Resets to the last state and appends the fork state's current write length as a varint followed by its operations.
446
+ * @returns {Writer} `this`
447
+ */
448
+ Writer.prototype.ldelim = function ldelim() {
449
+ var head = this.head,
450
+ tail = this.tail,
451
+ len = this.len;
452
+ this.reset().uint32(len);
453
+ if (len) {
454
+ this.tail.next = head.next; // skip noop
455
+ this.tail = tail;
456
+ this.len += len;
457
+ }
458
+ return this;
459
+ };
460
+
461
+ /**
462
+ * Finishes the write operation.
463
+ * @returns {Uint8Array} Finished buffer
464
+ */
465
+ Writer.prototype.finish = function finish() {
466
+ return this.finishInto(this.constructor.alloc(this.len), 0);
467
+ };
468
+
469
+ /**
470
+ * Finishes the write operation, writing into the provided buffer.
471
+ * The caller must ensure that `buf` has enough space starting at `offset`
472
+ * to hold {@link Writer#len} bytes.
473
+ * @param {T} buf Target buffer
474
+ * @param {number} [offset=0] Offset to start writing at
475
+ * @returns {T} The provided buffer
476
+ * @template T extends Uint8Array
477
+ */
478
+ Writer.prototype.finishInto = function finishInto(buf, offset) {
479
+ if (offset === undefined)
480
+ offset = 0;
481
+ var head = this.head.next,
482
+ pos = offset;
483
+ while (head) {
484
+ head.fn(head.val, buf, pos);
485
+ pos += head.len;
486
+ head = head.next;
487
+ }
488
+ return buf;
489
+ };
490
+
491
+ Writer._configure = function(BufferWriter_) {
492
+ BufferWriter = BufferWriter_;
493
+ Writer.create = create();
494
+ BufferWriter._configure();
495
+ };