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
@@ -1,518 +1,515 @@
1
- "use strict";
2
- var util = exports;
3
-
4
- // used to return a Promise where callback is omitted
5
- util.asPromise = require("./aspromise");
6
-
7
- // converts to / from base64 encoded strings
8
- util.base64 = require("./base64");
9
-
10
- // base class of rpc.Service
11
- util.EventEmitter = require("./eventemitter");
12
-
13
- // float handling accross browsers
14
- util.float = require("./float");
15
-
16
- // requires modules optionally and hides the call from bundlers
17
- util.inquire = require("./inquire");
18
-
19
- // converts to / from utf8 encoded strings
20
- util.utf8 = require("./utf8");
21
-
22
- // provides a node-like buffer pool in the browser
23
- util.pool = require("./pool");
24
-
25
- // utility to work with the low and high bits of a 64 bit value
26
- util.LongBits = require("./longbits");
27
-
28
- /**
29
- * Tests if the specified key can affect object prototypes.
30
- * @memberof util
31
- * @param {string} key Key to test
32
- * @returns {boolean} `true` if the key is unsafe
33
- */
34
- function isUnsafeProperty(key) {
35
- return key === "__proto__" || key === "prototype" || key === "constructor";
36
- }
37
-
38
- util.isUnsafeProperty = isUnsafeProperty;
39
-
40
- /**
41
- * Whether running within node or not.
42
- * @memberof util
43
- * @type {boolean}
44
- */
45
- util.isNode = Boolean(typeof global !== "undefined"
46
- && global
47
- && global.process
48
- && global.process.versions
49
- && global.process.versions.node);
50
-
51
- /**
52
- * Global object reference.
53
- * @memberof util
54
- * @type {Object}
55
- */
56
- util.global = util.isNode && global
57
- || typeof window !== "undefined" && window
58
- || typeof self !== "undefined" && self
59
- || this; // eslint-disable-line no-invalid-this
60
-
61
- /**
62
- * An immuable empty array.
63
- * @memberof util
64
- * @type {Array.<*>}
65
- * @const
66
- */
67
- util.emptyArray = Object.freeze ? Object.freeze([]) : /* istanbul ignore next */ []; // used on prototypes
68
-
69
- /**
70
- * An immutable empty object.
71
- * @type {Object}
72
- * @const
73
- */
74
- util.emptyObject = Object.freeze ? Object.freeze({}) : /* istanbul ignore next */ {}; // used on prototypes
75
-
76
- /**
77
- * Tests if the specified value is an integer.
78
- * @function
79
- * @param {*} value Value to test
80
- * @returns {boolean} `true` if the value is an integer
81
- */
82
- util.isInteger = Number.isInteger || /* istanbul ignore next */ function isInteger(value) {
83
- return typeof value === "number" && isFinite(value) && Math.floor(value) === value;
84
- };
85
-
86
- /**
87
- * Tests if the specified value is a string.
88
- * @param {*} value Value to test
89
- * @returns {boolean} `true` if the value is a string
90
- */
91
- util.isString = function isString(value) {
92
- return typeof value === "string" || value instanceof String;
93
- };
94
-
95
- /**
96
- * Tests if the specified value is a non-null object.
97
- * @param {*} value Value to test
98
- * @returns {boolean} `true` if the value is a non-null object
99
- */
100
- util.isObject = function isObject(value) {
101
- return value && typeof value === "object";
102
- };
103
-
104
- /**
105
- * Checks if a property on a message is considered to be present.
106
- * This is an alias of {@link util.isSet}.
107
- * @function
108
- * @param {Object} obj Plain object or message instance
109
- * @param {string} prop Property name
110
- * @returns {boolean} `true` if considered to be present, otherwise `false`
111
- */
112
- util.isset =
113
-
114
- /**
115
- * Checks if a property on a message is considered to be present.
116
- * @param {Object} obj Plain object or message instance
117
- * @param {string} prop Property name
118
- * @returns {boolean} `true` if considered to be present, otherwise `false`
119
- */
120
- util.isSet = function isSet(obj, prop) {
121
- var value = obj[prop];
122
- if (value != null && Object.hasOwnProperty.call(obj, prop)) // eslint-disable-line eqeqeq
123
- return typeof value !== "object" || (Array.isArray(value) ? value.length : Object.keys(value).length) > 0;
124
- return false;
125
- };
126
-
127
- /**
128
- * Any compatible Buffer instance.
129
- * This is a minimal stand-alone definition of a Buffer instance. The actual type is that exported by node's typings.
130
- * @interface Buffer
131
- * @extends Uint8Array
132
- */
133
-
134
- /**
135
- * Node's Buffer class if available.
136
- * @type {Constructor<Buffer>}
137
- */
138
- util.Buffer = (function() {
139
- try {
140
- var Buffer = util.global.Buffer;
141
- // refuse to use non-node buffers if not explicitly assigned (perf reasons):
142
- return Buffer.prototype.utf8Write ? Buffer : /* istanbul ignore next */ null;
143
- } catch (e) {
144
- /* istanbul ignore next */
145
- return null;
146
- }
147
- })();
148
-
149
- // Internal alias of or polyfull for Buffer.from.
150
- util._Buffer_from = null;
151
-
152
- // Internal alias of or polyfill for Buffer.allocUnsafe.
153
- util._Buffer_allocUnsafe = null;
154
-
155
- /**
156
- * Creates a new buffer of whatever type supported by the environment.
157
- * @param {number|number[]} [sizeOrArray=0] Buffer size or number array
158
- * @returns {Uint8Array|Buffer} Buffer
159
- */
160
- util.newBuffer = function newBuffer(sizeOrArray) {
161
- /* istanbul ignore next */
162
- return typeof sizeOrArray === "number"
163
- ? util.Buffer
164
- ? util._Buffer_allocUnsafe(sizeOrArray)
165
- : new util.Array(sizeOrArray)
166
- : util.Buffer
167
- ? util._Buffer_from(sizeOrArray)
168
- : typeof Uint8Array === "undefined"
169
- ? sizeOrArray
170
- : new Uint8Array(sizeOrArray);
171
- };
172
-
173
- /**
174
- * Array implementation used in the browser. `Uint8Array` if supported, otherwise `Array`.
175
- * @type {Constructor<Uint8Array>}
176
- */
177
- util.Array = typeof Uint8Array !== "undefined" ? Uint8Array /* istanbul ignore next */ : Array;
178
-
179
- /**
180
- * Any compatible Long instance.
181
- * This is a minimal stand-alone definition of a Long instance. The actual type is that exported by long.js.
182
- * @interface Long
183
- * @property {number} low Low bits
184
- * @property {number} high High bits
185
- * @property {boolean} unsigned Whether unsigned or not
186
- */
187
-
188
- /**
189
- * Long.js's Long class if available.
190
- * @type {Constructor<Long>}
191
- */
192
- util.Long = /* istanbul ignore next */ util.global.dcodeIO && /* istanbul ignore next */ util.global.dcodeIO.Long
193
- || /* istanbul ignore next */ util.global.Long
194
- || (function() {
195
- try {
196
- var Long = require("long");
197
- return Long && Long.isLong ? Long : null;
198
- } catch (e) {
199
- /* istanbul ignore next */
200
- return null;
201
- }
202
- })();
203
-
204
- /**
205
- * Regular expression used to verify 2 bit (`bool`) map keys.
206
- * @type {RegExp}
207
- * @const
208
- */
209
- util.key2Re = /^(?:true|false|0|1)$/;
210
-
211
- /**
212
- * Regular expression used to verify 32 bit (`int32` etc.) map keys.
213
- * @type {RegExp}
214
- * @const
215
- */
216
- util.key32Re = /^-?(?:0|[1-9][0-9]*)$/;
217
-
218
- /**
219
- * Regular expression used to verify 64 bit (`int64` etc.) map keys.
220
- * @type {RegExp}
221
- * @const
222
- */
223
- util.key64Re = /^(?:[\x00-\xff]{8}|-?(?:0|[1-9][0-9]*))$/; // eslint-disable-line no-control-regex
224
-
225
- /**
226
- * Converts a number or long to an 8 characters long hash string.
227
- * @param {Long|number} value Value to convert
228
- * @returns {string} Hash
229
- */
230
- util.longToHash = function longToHash(value) {
231
- return value
232
- ? util.LongBits.from(value).toHash()
233
- : util.LongBits.zeroHash;
234
- };
235
-
236
- /**
237
- * Converts an 8 characters long hash string to a long or number.
238
- * @param {string} hash Hash
239
- * @param {boolean} [unsigned=false] Whether unsigned or not
240
- * @returns {Long|number} Original value
241
- */
242
- util.longFromHash = function longFromHash(hash, unsigned) {
243
- var bits = util.LongBits.fromHash(hash);
244
- if (util.Long)
245
- return util.Long.fromBits(bits.lo, bits.hi, unsigned);
246
- return bits.toNumber(Boolean(unsigned));
247
- };
248
-
249
- /**
250
- * Converts a 64 bit key to a long or number if it is an 8 characters long hash string.
251
- * @param {string} key Map key
252
- * @param {boolean} [unsigned=false] Whether unsigned or not
253
- * @returns {Long|number|string} Original value
254
- */
255
- util.longFromKey = function longFromKey(key, unsigned) {
256
- return util.key64Re.test(key) && !util.key32Re.test(key)
257
- ? util.longFromHash(key, unsigned)
258
- : key;
259
- };
260
-
261
- /**
262
- * Converts a boolean key to a boolean value.
263
- * @param {string} key Map key
264
- * @returns {boolean} Boolean value
265
- */
266
- util.boolFromKey = function boolFromKey(key) {
267
- return key === "true" || key === "1";
268
- };
269
-
270
- /**
271
- * Merges the properties of the source object into the destination object.
272
- * @memberof util
273
- * @param {Object.<string,*>} dst Destination object
274
- * @param {...(Object.<string,*>|boolean)} src Source objects, optionally followed by an `ifNotSet` flag
275
- * @returns {Object.<string,*>} Destination object
276
- */
277
- function merge(dst) { // used by converters
278
- var ifNotSet = typeof arguments[arguments.length - 1] === "boolean",
279
- limit = ifNotSet ? arguments.length - 1 : arguments.length;
280
- ifNotSet = ifNotSet && arguments[arguments.length - 1];
281
- for (var a = 1; a < limit; ++a) {
282
- var src = arguments[a];
283
- if (!src)
284
- continue;
285
- for (var keys = Object.keys(src), i = 0; i < keys.length; ++i)
286
- if (!isUnsafeProperty(keys[i]) && (dst[keys[i]] === undefined || !ifNotSet))
287
- dst[keys[i]] = src[keys[i]];
288
- }
289
- return dst;
290
- }
291
-
292
- util.merge = merge;
293
-
294
- /**
295
- * Schema declaration nesting limit.
296
- * @memberof util
297
- * @type {number}
298
- */
299
- util.nestingLimit = 32; // protoc: MaxMessageDeclarationNestingDepth
300
-
301
- /**
302
- * Recursion limit.
303
- * @memberof util
304
- * @type {number}
305
- */
306
- util.recursionLimit = 100; // protoc: CodedInputStream::default_recursion_limit_
307
-
308
- /**
309
- * Makes a property safe for assignment as an own property.
310
- * @memberof util
311
- * @param {Object.<string,*>} obj Object
312
- * @param {string} key Property key
313
- * @param {boolean} [enumerable=true] Whether the property should be enumerable
314
- * @returns {undefined}
315
- */
316
- util.makeProp = function makeProp(obj, key, enumerable) {
317
- if (Object.prototype.hasOwnProperty.call(obj, key))
318
- return;
319
- Object.defineProperty(obj, key, {
320
- enumerable: enumerable === undefined ? true : enumerable,
321
- configurable: true,
322
- writable: true
323
- });
324
- };
325
-
326
- /**
327
- * Converts the first character of a string to lower case.
328
- * @param {string} str String to convert
329
- * @returns {string} Converted string
330
- */
331
- util.lcFirst = function lcFirst(str) {
332
- return str.charAt(0).toLowerCase() + str.substring(1);
333
- };
334
-
335
- /**
336
- * Creates a custom error constructor.
337
- * @memberof util
338
- * @param {string} name Error name
339
- * @returns {Constructor<Error>} Custom error constructor
340
- */
341
- function newError(name) {
342
-
343
- function CustomError(message, properties) {
344
-
345
- if (!(this instanceof CustomError))
346
- return new CustomError(message, properties);
347
-
348
- // Error.call(this, message);
349
- // ^ just returns a new error instance because the ctor can be called as a function
350
-
351
- Object.defineProperty(this, "message", { get: function() { return message; } });
352
-
353
- /* istanbul ignore next */
354
- if (Error.captureStackTrace) // node
355
- Error.captureStackTrace(this, CustomError);
356
- else
357
- Object.defineProperty(this, "stack", { value: new Error().stack || "" });
358
-
359
- if (properties)
360
- merge(this, properties);
361
- }
362
-
363
- CustomError.prototype = Object.create(Error.prototype, {
364
- constructor: {
365
- value: CustomError,
366
- writable: true,
367
- enumerable: false,
368
- configurable: true,
369
- },
370
- name: {
371
- get: function get() { return name; },
372
- set: undefined,
373
- enumerable: false,
374
- // configurable: false would accurately preserve the behavior of
375
- // the original, but I'm guessing that was not intentional.
376
- // For an actual error subclass, this property would
377
- // be configurable.
378
- configurable: true,
379
- },
380
- toString: {
381
- value: function value() { return this.name + ": " + this.message; },
382
- writable: true,
383
- enumerable: false,
384
- configurable: true,
385
- },
386
- });
387
-
388
- return CustomError;
389
- }
390
-
391
- util.newError = newError;
392
-
393
- /**
394
- * Constructs a new protocol error.
395
- * @classdesc Error subclass indicating a protocol specifc error.
396
- * @memberof util
397
- * @extends Error
398
- * @template T extends Message<T>
399
- * @constructor
400
- * @param {string} message Error message
401
- * @param {Object.<string,*>} [properties] Additional properties
402
- * @example
403
- * try {
404
- * MyMessage.decode(someBuffer); // throws if required fields are missing
405
- * } catch (e) {
406
- * if (e instanceof ProtocolError && e.instance)
407
- * console.log("decoded so far: " + JSON.stringify(e.instance));
408
- * }
409
- */
410
- util.ProtocolError = newError("ProtocolError");
411
-
412
- /**
413
- * So far decoded message instance.
414
- * @name util.ProtocolError#instance
415
- * @type {Message<T>}
416
- */
417
-
418
- /**
419
- * A OneOf getter as returned by {@link util.oneOfGetter}.
420
- * @typedef OneOfGetter
421
- * @type {function}
422
- * @returns {string|undefined} Set field name, if any
423
- */
424
-
425
- /**
426
- * Builds a getter for a oneof's present field name.
427
- * @param {string[]} fieldNames Field names
428
- * @returns {OneOfGetter} Unbound getter
429
- */
430
- util.oneOfGetter = function getOneOf(fieldNames) {
431
- var fieldMap = {};
432
- for (var i = 0; i < fieldNames.length; ++i)
433
- fieldMap[fieldNames[i]] = 1;
434
-
435
- /**
436
- * @returns {string|undefined} Set field name, if any
437
- * @this Object
438
- * @ignore
439
- */
440
- return function() { // eslint-disable-line consistent-return
441
- for (var keys = Object.keys(this), i = keys.length - 1; i > -1; --i)
442
- if (fieldMap[keys[i]] === 1 && this[keys[i]] !== undefined && this[keys[i]] !== null)
443
- return keys[i];
444
- };
445
- };
446
-
447
- /**
448
- * A OneOf setter as returned by {@link util.oneOfSetter}.
449
- * @typedef OneOfSetter
450
- * @type {function}
451
- * @param {string|undefined} value Field name
452
- * @returns {undefined}
453
- */
454
-
455
- /**
456
- * Builds a setter for a oneof's present field name.
457
- * @param {string[]} fieldNames Field names
458
- * @returns {OneOfSetter} Unbound setter
459
- */
460
- util.oneOfSetter = function setOneOf(fieldNames) {
461
-
462
- /**
463
- * @param {string} name Field name
464
- * @returns {undefined}
465
- * @this Object
466
- * @ignore
467
- */
468
- return function(name) {
469
- for (var i = 0; i < fieldNames.length; ++i)
470
- if (fieldNames[i] !== name)
471
- delete this[fieldNames[i]];
472
- };
473
- };
474
-
475
- /**
476
- * Default conversion options used for {@link Message#toJSON} implementations.
477
- *
478
- * These options are close to proto3's JSON mapping with the exception that internal types like Any are handled just like messages. More precisely:
479
- *
480
- * - Longs become strings
481
- * - Enums become string keys
482
- * - Bytes become base64 encoded strings
483
- * - (Sub-)Messages become plain objects
484
- * - Maps become plain objects with all string keys
485
- * - Repeated fields become arrays
486
- * - NaN and Infinity for float and double fields become strings
487
- *
488
- * @type {IConversionOptions}
489
- * @see https://developers.google.com/protocol-buffers/docs/proto3?hl=en#json
490
- */
491
- util.toJSONOptions = {
492
- longs: String,
493
- enums: String,
494
- bytes: String,
495
- json: true
496
- };
497
-
498
- // Sets up buffer utility according to the environment (called in index-minimal)
499
- util._configure = function() {
500
- var Buffer = util.Buffer;
501
- /* istanbul ignore if */
502
- if (!Buffer) {
503
- util._Buffer_from = util._Buffer_allocUnsafe = null;
504
- return;
505
- }
506
- // because node 4.x buffers are incompatible & immutable
507
- // see: https://github.com/dcodeIO/protobuf.js/pull/665
508
- util._Buffer_from = Buffer.from !== Uint8Array.from && Buffer.from ||
509
- /* istanbul ignore next */
510
- function Buffer_from(value, encoding) {
511
- return new Buffer(value, encoding);
512
- };
513
- util._Buffer_allocUnsafe = Buffer.allocUnsafe ||
514
- /* istanbul ignore next */
515
- function Buffer_allocUnsafe(size) {
516
- return new Buffer(size);
517
- };
518
- };
1
+ "use strict";
2
+ var util = exports;
3
+
4
+ // used to return a Promise where callback is omitted
5
+ util.asPromise = require("./aspromise");
6
+
7
+ // converts to / from base64 encoded strings
8
+ util.base64 = require("./base64");
9
+
10
+ // base class of rpc.Service
11
+ util.EventEmitter = require("./eventemitter");
12
+
13
+ // float handling accross browsers
14
+ util.float = require("./float");
15
+
16
+ // converts to / from utf8 encoded strings
17
+ util.utf8 = require("./utf8");
18
+
19
+ // provides a node-like buffer pool in the browser
20
+ util.pool = require("./pool");
21
+
22
+ // utility to work with the low and high bits of a 64 bit value
23
+ util.LongBits = require("./longbits");
24
+
25
+ /**
26
+ * Tests if the specified key can affect object prototypes.
27
+ * @memberof util
28
+ * @param {string} key Key to test
29
+ * @returns {boolean} `true` if the key is unsafe
30
+ */
31
+ function isUnsafeProperty(key) {
32
+ return key === "__proto__" || key === "prototype" || key === "constructor";
33
+ }
34
+
35
+ util.isUnsafeProperty = isUnsafeProperty;
36
+
37
+ /**
38
+ * Whether running within node or not.
39
+ * @memberof util
40
+ * @type {boolean}
41
+ */
42
+ util.isNode = Boolean(typeof global !== "undefined"
43
+ && global
44
+ && global.process
45
+ && global.process.versions
46
+ && global.process.versions.node);
47
+
48
+ /**
49
+ * Global object reference.
50
+ * @memberof util
51
+ * @type {Object}
52
+ */
53
+ util.global = util.isNode && global
54
+ || typeof window !== "undefined" && window
55
+ || typeof self !== "undefined" && self
56
+ || this; // eslint-disable-line no-invalid-this
57
+
58
+ /**
59
+ * An immuable empty array.
60
+ * @memberof util
61
+ * @type {Array.<*>}
62
+ * @const
63
+ */
64
+ util.emptyArray = Object.freeze ? Object.freeze([]) : /* istanbul ignore next */ []; // used on prototypes
65
+
66
+ /**
67
+ * An immutable empty object.
68
+ * @type {Object}
69
+ * @const
70
+ */
71
+ util.emptyObject = Object.freeze ? Object.freeze({}) : /* istanbul ignore next */ {}; // used on prototypes
72
+
73
+ /**
74
+ * Tests if the specified value is an integer.
75
+ * @function
76
+ * @param {*} value Value to test
77
+ * @returns {boolean} `true` if the value is an integer
78
+ */
79
+ util.isInteger = Number.isInteger || /* istanbul ignore next */ function isInteger(value) {
80
+ return typeof value === "number" && isFinite(value) && Math.floor(value) === value;
81
+ };
82
+
83
+ /**
84
+ * Tests if the specified value is a string.
85
+ * @param {*} value Value to test
86
+ * @returns {boolean} `true` if the value is a string
87
+ */
88
+ util.isString = function isString(value) {
89
+ return typeof value === "string" || value instanceof String;
90
+ };
91
+
92
+ /**
93
+ * Tests if the specified value is a non-null object.
94
+ * @param {*} value Value to test
95
+ * @returns {boolean} `true` if the value is a non-null object
96
+ */
97
+ util.isObject = function isObject(value) {
98
+ return value && typeof value === "object";
99
+ };
100
+
101
+ /**
102
+ * Checks if a property on a message is considered to be present.
103
+ * This is an alias of {@link util.isSet}.
104
+ * @function
105
+ * @param {Object} obj Plain object or message instance
106
+ * @param {string} prop Property name
107
+ * @returns {boolean} `true` if considered to be present, otherwise `false`
108
+ */
109
+ util.isset =
110
+
111
+ /**
112
+ * Checks if a property on a message is considered to be present.
113
+ * @param {Object} obj Plain object or message instance
114
+ * @param {string} prop Property name
115
+ * @returns {boolean} `true` if considered to be present, otherwise `false`
116
+ */
117
+ util.isSet = function isSet(obj, prop) {
118
+ var value = obj[prop];
119
+ if (value != null && Object.hasOwnProperty.call(obj, prop)) // eslint-disable-line eqeqeq
120
+ return typeof value !== "object" || (Array.isArray(value) ? value.length : Object.keys(value).length) > 0;
121
+ return false;
122
+ };
123
+
124
+ /**
125
+ * Any compatible Buffer instance.
126
+ * This is a minimal stand-alone definition of a Buffer instance. The actual type is that exported by node's typings.
127
+ * @interface Buffer
128
+ * @extends Uint8Array
129
+ */
130
+
131
+ /**
132
+ * Node's Buffer class if available.
133
+ * @type {Constructor<Buffer>}
134
+ */
135
+ util.Buffer = (function() {
136
+ try {
137
+ var Buffer = util.global.Buffer;
138
+ // refuse to use non-node buffers if not explicitly assigned (perf reasons):
139
+ return Buffer.prototype.utf8Write ? Buffer : /* istanbul ignore next */ null;
140
+ } catch (e) {
141
+ /* istanbul ignore next */
142
+ return null;
143
+ }
144
+ })();
145
+
146
+ // Internal alias of or polyfull for Buffer.from.
147
+ util._Buffer_from = null;
148
+
149
+ // Internal alias of or polyfill for Buffer.allocUnsafe.
150
+ util._Buffer_allocUnsafe = null;
151
+
152
+ /**
153
+ * Creates a new buffer of whatever type supported by the environment.
154
+ * @param {number|number[]} [sizeOrArray=0] Buffer size or number array
155
+ * @returns {Uint8Array|Buffer} Buffer
156
+ */
157
+ util.newBuffer = function newBuffer(sizeOrArray) {
158
+ /* istanbul ignore next */
159
+ return typeof sizeOrArray === "number"
160
+ ? util.Buffer
161
+ ? util._Buffer_allocUnsafe(sizeOrArray)
162
+ : new util.Array(sizeOrArray)
163
+ : util.Buffer
164
+ ? util._Buffer_from(sizeOrArray)
165
+ : typeof Uint8Array === "undefined"
166
+ ? sizeOrArray
167
+ : new Uint8Array(sizeOrArray);
168
+ };
169
+
170
+ /**
171
+ * Array implementation used in the browser. `Uint8Array` if supported, otherwise `Array`.
172
+ * @type {Constructor<Uint8Array>}
173
+ */
174
+ util.Array = typeof Uint8Array !== "undefined" ? Uint8Array /* istanbul ignore next */ : Array;
175
+
176
+ /**
177
+ * Any compatible Long instance.
178
+ * This is a minimal stand-alone definition of a Long instance. The actual type is that exported by long.js.
179
+ * @interface Long
180
+ * @property {number} low Low bits
181
+ * @property {number} high High bits
182
+ * @property {boolean} unsigned Whether unsigned or not
183
+ */
184
+
185
+ /**
186
+ * Long.js's Long class if available.
187
+ * @type {Constructor<Long>}
188
+ */
189
+ util.Long = /* istanbul ignore next */ util.global.dcodeIO && /* istanbul ignore next */ util.global.dcodeIO.Long
190
+ || /* istanbul ignore next */ util.global.Long
191
+ || (function() {
192
+ try {
193
+ var Long = require("long");
194
+ return Long && Long.isLong ? Long : null;
195
+ } catch (e) {
196
+ /* istanbul ignore next */
197
+ return null;
198
+ }
199
+ })();
200
+
201
+ /**
202
+ * Regular expression used to verify 2 bit (`bool`) map keys.
203
+ * @type {RegExp}
204
+ * @const
205
+ */
206
+ util.key2Re = /^(?:true|false|0|1)$/;
207
+
208
+ /**
209
+ * Regular expression used to verify 32 bit (`int32` etc.) map keys.
210
+ * @type {RegExp}
211
+ * @const
212
+ */
213
+ util.key32Re = /^-?(?:0|[1-9][0-9]*)$/;
214
+
215
+ /**
216
+ * Regular expression used to verify 64 bit (`int64` etc.) map keys.
217
+ * @type {RegExp}
218
+ * @const
219
+ */
220
+ util.key64Re = /^(?:[\x00-\xff]{8}|-?(?:0|[1-9][0-9]*))$/; // eslint-disable-line no-control-regex
221
+
222
+ /**
223
+ * Converts a number or long to an 8 characters long hash string.
224
+ * @param {Long|number} value Value to convert
225
+ * @returns {string} Hash
226
+ */
227
+ util.longToHash = function longToHash(value) {
228
+ return value
229
+ ? util.LongBits.from(value).toHash()
230
+ : util.LongBits.zeroHash;
231
+ };
232
+
233
+ /**
234
+ * Converts an 8 characters long hash string to a long or number.
235
+ * @param {string} hash Hash
236
+ * @param {boolean} [unsigned=false] Whether unsigned or not
237
+ * @returns {Long|number} Original value
238
+ */
239
+ util.longFromHash = function longFromHash(hash, unsigned) {
240
+ var bits = util.LongBits.fromHash(hash);
241
+ if (util.Long)
242
+ return util.Long.fromBits(bits.lo, bits.hi, unsigned);
243
+ return bits.toNumber(Boolean(unsigned));
244
+ };
245
+
246
+ /**
247
+ * Converts a 64 bit key to a long or number if it is an 8 characters long hash string.
248
+ * @param {string} key Map key
249
+ * @param {boolean} [unsigned=false] Whether unsigned or not
250
+ * @returns {Long|number|string} Original value
251
+ */
252
+ util.longFromKey = function longFromKey(key, unsigned) {
253
+ return util.key64Re.test(key) && !util.key32Re.test(key)
254
+ ? util.longFromHash(key, unsigned)
255
+ : key;
256
+ };
257
+
258
+ /**
259
+ * Converts a boolean key to a boolean value.
260
+ * @param {string} key Map key
261
+ * @returns {boolean} Boolean value
262
+ */
263
+ util.boolFromKey = function boolFromKey(key) {
264
+ return key === "true" || key === "1";
265
+ };
266
+
267
+ /**
268
+ * Merges the properties of the source object into the destination object.
269
+ * @memberof util
270
+ * @param {Object.<string,*>} dst Destination object
271
+ * @param {...(Object.<string,*>|boolean)} src Source objects, optionally followed by an `ifNotSet` flag
272
+ * @returns {Object.<string,*>} Destination object
273
+ */
274
+ function merge(dst) { // used by converters
275
+ var ifNotSet = typeof arguments[arguments.length - 1] === "boolean",
276
+ limit = ifNotSet ? arguments.length - 1 : arguments.length;
277
+ ifNotSet = ifNotSet && arguments[arguments.length - 1];
278
+ for (var a = 1; a < limit; ++a) {
279
+ var src = arguments[a];
280
+ if (!src)
281
+ continue;
282
+ for (var keys = Object.keys(src), i = 0; i < keys.length; ++i)
283
+ if (!isUnsafeProperty(keys[i]) && (dst[keys[i]] === undefined || !ifNotSet))
284
+ dst[keys[i]] = src[keys[i]];
285
+ }
286
+ return dst;
287
+ }
288
+
289
+ util.merge = merge;
290
+
291
+ /**
292
+ * Schema declaration nesting limit.
293
+ * @memberof util
294
+ * @type {number}
295
+ */
296
+ util.nestingLimit = 32; // protoc: MaxMessageDeclarationNestingDepth
297
+
298
+ /**
299
+ * Recursion limit.
300
+ * @memberof util
301
+ * @type {number}
302
+ */
303
+ util.recursionLimit = 100; // protoc: CodedInputStream::default_recursion_limit_
304
+
305
+ /**
306
+ * Makes a property safe for assignment as an own property.
307
+ * @memberof util
308
+ * @param {Object.<string,*>} obj Object
309
+ * @param {string} key Property key
310
+ * @param {boolean} [enumerable=true] Whether the property should be enumerable
311
+ * @returns {undefined}
312
+ */
313
+ util.makeProp = function makeProp(obj, key, enumerable) {
314
+ if (Object.prototype.hasOwnProperty.call(obj, key))
315
+ return;
316
+ Object.defineProperty(obj, key, {
317
+ enumerable: enumerable === undefined ? true : enumerable,
318
+ configurable: true,
319
+ writable: true
320
+ });
321
+ };
322
+
323
+ /**
324
+ * Converts the first character of a string to lower case.
325
+ * @param {string} str String to convert
326
+ * @returns {string} Converted string
327
+ */
328
+ util.lcFirst = function lcFirst(str) {
329
+ return str.charAt(0).toLowerCase() + str.substring(1);
330
+ };
331
+
332
+ /**
333
+ * Creates a custom error constructor.
334
+ * @memberof util
335
+ * @param {string} name Error name
336
+ * @returns {Constructor<Error>} Custom error constructor
337
+ */
338
+ function newError(name) {
339
+
340
+ function CustomError(message, properties) {
341
+
342
+ if (!(this instanceof CustomError))
343
+ return new CustomError(message, properties);
344
+
345
+ // Error.call(this, message);
346
+ // ^ just returns a new error instance because the ctor can be called as a function
347
+
348
+ Object.defineProperty(this, "message", { get: function() { return message; } });
349
+
350
+ /* istanbul ignore next */
351
+ if (Error.captureStackTrace) // node
352
+ Error.captureStackTrace(this, CustomError);
353
+ else
354
+ Object.defineProperty(this, "stack", { value: new Error().stack || "" });
355
+
356
+ if (properties)
357
+ merge(this, properties);
358
+ }
359
+
360
+ CustomError.prototype = Object.create(Error.prototype, {
361
+ constructor: {
362
+ value: CustomError,
363
+ writable: true,
364
+ enumerable: false,
365
+ configurable: true,
366
+ },
367
+ name: {
368
+ get: function get() { return name; },
369
+ set: undefined,
370
+ enumerable: false,
371
+ // configurable: false would accurately preserve the behavior of
372
+ // the original, but I'm guessing that was not intentional.
373
+ // For an actual error subclass, this property would
374
+ // be configurable.
375
+ configurable: true,
376
+ },
377
+ toString: {
378
+ value: function value() { return this.name + ": " + this.message; },
379
+ writable: true,
380
+ enumerable: false,
381
+ configurable: true,
382
+ },
383
+ });
384
+
385
+ return CustomError;
386
+ }
387
+
388
+ util.newError = newError;
389
+
390
+ /**
391
+ * Constructs a new protocol error.
392
+ * @classdesc Error subclass indicating a protocol specifc error.
393
+ * @memberof util
394
+ * @extends Error
395
+ * @template T extends Message<T>
396
+ * @constructor
397
+ * @param {string} message Error message
398
+ * @param {Object.<string,*>} [properties] Additional properties
399
+ * @example
400
+ * try {
401
+ * MyMessage.decode(someBuffer); // throws if required fields are missing
402
+ * } catch (e) {
403
+ * if (e instanceof ProtocolError && e.instance)
404
+ * console.log("decoded so far: " + JSON.stringify(e.instance));
405
+ * }
406
+ */
407
+ util.ProtocolError = newError("ProtocolError");
408
+
409
+ /**
410
+ * So far decoded message instance.
411
+ * @name util.ProtocolError#instance
412
+ * @type {Message<T>}
413
+ */
414
+
415
+ /**
416
+ * A OneOf getter as returned by {@link util.oneOfGetter}.
417
+ * @typedef OneOfGetter
418
+ * @type {function}
419
+ * @returns {string|undefined} Set field name, if any
420
+ */
421
+
422
+ /**
423
+ * Builds a getter for a oneof's present field name.
424
+ * @param {string[]} fieldNames Field names
425
+ * @returns {OneOfGetter} Unbound getter
426
+ */
427
+ util.oneOfGetter = function getOneOf(fieldNames) {
428
+ var fieldMap = {};
429
+ for (var i = 0; i < fieldNames.length; ++i)
430
+ fieldMap[fieldNames[i]] = 1;
431
+
432
+ /**
433
+ * @returns {string|undefined} Set field name, if any
434
+ * @this Object
435
+ * @ignore
436
+ */
437
+ return function() { // eslint-disable-line consistent-return
438
+ for (var keys = Object.keys(this), i = keys.length - 1; i > -1; --i)
439
+ if (fieldMap[keys[i]] === 1 && this[keys[i]] !== undefined && this[keys[i]] !== null)
440
+ return keys[i];
441
+ };
442
+ };
443
+
444
+ /**
445
+ * A OneOf setter as returned by {@link util.oneOfSetter}.
446
+ * @typedef OneOfSetter
447
+ * @type {function}
448
+ * @param {string|undefined} value Field name
449
+ * @returns {undefined}
450
+ */
451
+
452
+ /**
453
+ * Builds a setter for a oneof's present field name.
454
+ * @param {string[]} fieldNames Field names
455
+ * @returns {OneOfSetter} Unbound setter
456
+ */
457
+ util.oneOfSetter = function setOneOf(fieldNames) {
458
+
459
+ /**
460
+ * @param {string} name Field name
461
+ * @returns {undefined}
462
+ * @this Object
463
+ * @ignore
464
+ */
465
+ return function(name) {
466
+ for (var i = 0; i < fieldNames.length; ++i)
467
+ if (fieldNames[i] !== name)
468
+ delete this[fieldNames[i]];
469
+ };
470
+ };
471
+
472
+ /**
473
+ * Default conversion options used for {@link Message#toJSON} implementations.
474
+ *
475
+ * These options are close to proto3's JSON mapping with the exception that internal types like Any are handled just like messages. More precisely:
476
+ *
477
+ * - Longs become strings
478
+ * - Enums become string keys
479
+ * - Bytes become base64 encoded strings
480
+ * - (Sub-)Messages become plain objects
481
+ * - Maps become plain objects with all string keys
482
+ * - Repeated fields become arrays
483
+ * - NaN and Infinity for float and double fields become strings
484
+ *
485
+ * @type {IConversionOptions}
486
+ * @see https://developers.google.com/protocol-buffers/docs/proto3?hl=en#json
487
+ */
488
+ util.toJSONOptions = {
489
+ longs: String,
490
+ enums: String,
491
+ bytes: String,
492
+ json: true
493
+ };
494
+
495
+ // Sets up buffer utility according to the environment (called in index-minimal)
496
+ util._configure = function() {
497
+ var Buffer = util.Buffer;
498
+ /* istanbul ignore if */
499
+ if (!Buffer) {
500
+ util._Buffer_from = util._Buffer_allocUnsafe = null;
501
+ return;
502
+ }
503
+ // because node 4.x buffers are incompatible & immutable
504
+ // see: https://github.com/dcodeIO/protobuf.js/pull/665
505
+ util._Buffer_from = Buffer.from !== Uint8Array.from && Buffer.from ||
506
+ /* istanbul ignore next */
507
+ function Buffer_from(value, encoding) {
508
+ return new Buffer(value, encoding);
509
+ };
510
+ util._Buffer_allocUnsafe = Buffer.allocUnsafe ||
511
+ /* istanbul ignore next */
512
+ function Buffer_allocUnsafe(size) {
513
+ return new Buffer(size);
514
+ };
515
+ };