protobufjs 7.3.0 → 7.3.2

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