protobufjs 8.6.2 → 8.6.3

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 (105) hide show
  1. package/LICENSE +39 -39
  2. package/README.md +441 -441
  3. package/dist/light/protobuf.js +8575 -8454
  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 +3064 -2999
  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 +10479 -10358
  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 -87
  22. package/ext/descriptor.js +1354 -1354
  23. package/ext/protojson.LICENSE +201 -201
  24. package/ext/protojson.d.ts +20 -20
  25. package/ext/protojson.js +925 -925
  26. package/ext/textformat.d.ts +19 -19
  27. package/ext/textformat.js +1256 -1256
  28. package/google/LICENSE +27 -27
  29. package/google/README.md +1 -1
  30. package/google/api/annotations.json +82 -82
  31. package/google/api/annotations.proto +10 -10
  32. package/google/api/http.json +85 -85
  33. package/google/api/http.proto +30 -30
  34. package/google/protobuf/api.json +117 -117
  35. package/google/protobuf/api.proto +33 -33
  36. package/google/protobuf/compiler/plugin.json +126 -126
  37. package/google/protobuf/compiler/plugin.proto +47 -47
  38. package/google/protobuf/descriptor.json +1381 -1381
  39. package/google/protobuf/descriptor.proto +534 -534
  40. package/google/protobuf/source_context.json +19 -19
  41. package/google/protobuf/source_context.proto +7 -7
  42. package/google/protobuf/type.json +201 -201
  43. package/google/protobuf/type.proto +89 -89
  44. package/index.d.ts +30 -0
  45. package/index.js +4 -4
  46. package/light.d.ts +2 -2
  47. package/light.js +3 -3
  48. package/minimal.d.ts +2 -2
  49. package/minimal.js +4 -4
  50. package/package.json +93 -93
  51. package/src/common.js +399 -399
  52. package/src/converter.js +344 -344
  53. package/src/decoder.js +214 -208
  54. package/src/encoder.js +111 -111
  55. package/src/enum.js +231 -231
  56. package/src/field.js +497 -497
  57. package/src/index-light.js +104 -104
  58. package/src/index-minimal.js +35 -36
  59. package/src/index.js +12 -12
  60. package/src/mapfield.js +136 -136
  61. package/src/message.js +137 -137
  62. package/src/method.js +175 -175
  63. package/src/namespace.js +566 -565
  64. package/src/object.js +382 -382
  65. package/src/oneof.js +225 -225
  66. package/src/parse.js +1068 -1068
  67. package/src/reader.js +558 -543
  68. package/src/reader_buffer.js +72 -72
  69. package/src/root.js +416 -416
  70. package/src/roots.js +18 -18
  71. package/src/rpc/service.js +148 -148
  72. package/src/rpc.js +36 -36
  73. package/src/service.js +198 -198
  74. package/src/tokenize.js +421 -421
  75. package/src/type.js +654 -655
  76. package/src/types.js +196 -196
  77. package/src/typescript.js +25 -25
  78. package/src/util/aspromise.d.ts +13 -13
  79. package/src/util/aspromise.js +52 -52
  80. package/src/util/base64.d.ts +32 -32
  81. package/src/util/base64.js +146 -146
  82. package/src/util/codegen.d.ts +31 -31
  83. package/src/util/codegen.js +113 -113
  84. package/src/util/eventemitter.d.ts +45 -45
  85. package/src/util/eventemitter.js +86 -86
  86. package/src/util/fetch.d.ts +56 -56
  87. package/src/util/fetch.js +112 -112
  88. package/src/util/float.d.ts +83 -83
  89. package/src/util/float.js +335 -335
  90. package/src/util/fs.js +11 -11
  91. package/src/util/longbits.js +200 -200
  92. package/src/util/minimal.js +515 -515
  93. package/src/util/path.d.ts +22 -22
  94. package/src/util/path.js +100 -72
  95. package/src/util/patterns.js +7 -7
  96. package/src/util/pool.d.ts +32 -32
  97. package/src/util/pool.js +48 -48
  98. package/src/util/utf8.d.ts +24 -24
  99. package/src/util/utf8.js +181 -130
  100. package/src/util.js +264 -242
  101. package/src/verifier.js +180 -180
  102. package/src/wrappers.js +106 -106
  103. package/src/writer.js +495 -495
  104. package/src/writer_buffer.js +102 -102
  105. package/tsconfig.json +6 -6
@@ -1,515 +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
- // 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
- };
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
+ };