@depup/apollo__protobufjs 1.2.7-depup.0

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 (113) hide show
  1. package/LICENSE +39 -0
  2. package/README.md +32 -0
  3. package/bin/pbjs +6 -0
  4. package/bin/pbts +6 -0
  5. package/changes.json +14 -0
  6. package/cli/LICENSE +33 -0
  7. package/cli/README.md +11 -0
  8. package/cli/bin/pbjs +6 -0
  9. package/cli/bin/pbts +6 -0
  10. package/cli/index.d.ts +3 -0
  11. package/cli/index.js +3 -0
  12. package/cli/lib/tsd-jsdoc/LICENSE +21 -0
  13. package/cli/lib/tsd-jsdoc/README.md +23 -0
  14. package/cli/lib/tsd-jsdoc/plugin.js +21 -0
  15. package/cli/lib/tsd-jsdoc/publish.js +693 -0
  16. package/cli/lib/tsd-jsdoc.json +18 -0
  17. package/cli/package.json +7 -0
  18. package/cli/package.standalone.json +32 -0
  19. package/cli/pbjs.d.ts +9 -0
  20. package/cli/pbjs.js +331 -0
  21. package/cli/pbts.d.ts +9 -0
  22. package/cli/pbts.js +198 -0
  23. package/cli/targets/json-module.js +38 -0
  24. package/cli/targets/json.js +8 -0
  25. package/cli/targets/proto.js +326 -0
  26. package/cli/targets/proto2.js +10 -0
  27. package/cli/targets/proto3.js +10 -0
  28. package/cli/targets/static-module.js +29 -0
  29. package/cli/targets/static.js +709 -0
  30. package/cli/util.js +183 -0
  31. package/cli/wrappers/amd.js +7 -0
  32. package/cli/wrappers/closure.js +7 -0
  33. package/cli/wrappers/commonjs.js +7 -0
  34. package/cli/wrappers/default.js +15 -0
  35. package/cli/wrappers/es6.js +5 -0
  36. package/dist/README.md +31 -0
  37. package/dist/light/README.md +31 -0
  38. package/dist/light/protobuf.js +7198 -0
  39. package/dist/light/protobuf.js.map +1 -0
  40. package/dist/light/protobuf.min.js +7 -0
  41. package/dist/light/protobuf.min.js.map +1 -0
  42. package/dist/minimal/README.md +31 -0
  43. package/dist/minimal/protobuf.js +2675 -0
  44. package/dist/minimal/protobuf.js.map +1 -0
  45. package/dist/minimal/protobuf.min.js +7 -0
  46. package/dist/minimal/protobuf.min.js.map +1 -0
  47. package/dist/protobuf.js +8775 -0
  48. package/dist/protobuf.js.map +1 -0
  49. package/dist/protobuf.min.js +7 -0
  50. package/dist/protobuf.min.js.map +1 -0
  51. package/ext/debug/README.md +4 -0
  52. package/ext/debug/index.js +71 -0
  53. package/ext/descriptor/README.md +72 -0
  54. package/ext/descriptor/index.d.ts +191 -0
  55. package/ext/descriptor/index.js +1052 -0
  56. package/ext/descriptor/test.js +54 -0
  57. package/google/LICENSE +27 -0
  58. package/google/README.md +1 -0
  59. package/google/api/annotations.json +83 -0
  60. package/google/api/annotations.proto +11 -0
  61. package/google/api/http.json +86 -0
  62. package/google/api/http.proto +31 -0
  63. package/google/protobuf/api.json +118 -0
  64. package/google/protobuf/api.proto +34 -0
  65. package/google/protobuf/descriptor.json +739 -0
  66. package/google/protobuf/descriptor.proto +286 -0
  67. package/google/protobuf/source_context.json +20 -0
  68. package/google/protobuf/source_context.proto +7 -0
  69. package/google/protobuf/type.json +202 -0
  70. package/google/protobuf/type.proto +89 -0
  71. package/index.d.ts +2628 -0
  72. package/index.js +4 -0
  73. package/light.d.ts +2 -0
  74. package/light.js +4 -0
  75. package/minimal.d.ts +2 -0
  76. package/minimal.js +4 -0
  77. package/package.json +147 -0
  78. package/scripts/postinstall.js +35 -0
  79. package/src/common.js +399 -0
  80. package/src/converter.js +304 -0
  81. package/src/decoder.js +106 -0
  82. package/src/encoder.js +119 -0
  83. package/src/enum.js +181 -0
  84. package/src/field.js +379 -0
  85. package/src/index-light.js +104 -0
  86. package/src/index-minimal.js +36 -0
  87. package/src/index.js +12 -0
  88. package/src/mapfield.js +126 -0
  89. package/src/message.js +139 -0
  90. package/src/method.js +151 -0
  91. package/src/namespace.js +433 -0
  92. package/src/object.js +200 -0
  93. package/src/oneof.js +203 -0
  94. package/src/parse.js +761 -0
  95. package/src/reader.js +405 -0
  96. package/src/reader_buffer.js +44 -0
  97. package/src/root.js +353 -0
  98. package/src/roots.js +18 -0
  99. package/src/rpc/service.js +142 -0
  100. package/src/rpc.js +36 -0
  101. package/src/service.js +167 -0
  102. package/src/tokenize.js +397 -0
  103. package/src/type.js +589 -0
  104. package/src/types.js +196 -0
  105. package/src/typescript.jsdoc +22 -0
  106. package/src/util/longbits.js +200 -0
  107. package/src/util/minimal.js +406 -0
  108. package/src/util.js +178 -0
  109. package/src/verifier.js +191 -0
  110. package/src/wrappers.js +83 -0
  111. package/src/writer.js +459 -0
  112. package/src/writer_buffer.js +81 -0
  113. package/tsconfig.json +7 -0
@@ -0,0 +1,406 @@
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
+ // global object reference
29
+ util.global = typeof window !== "undefined" && window
30
+ || typeof global !== "undefined" && global
31
+ || typeof self !== "undefined" && self
32
+ || this; // eslint-disable-line no-invalid-this
33
+
34
+ /**
35
+ * An immuable empty array.
36
+ * @memberof util
37
+ * @type {Array.<*>}
38
+ * @const
39
+ */
40
+ util.emptyArray = Object.freeze ? Object.freeze([]) : /* istanbul ignore next */ []; // used on prototypes
41
+
42
+ /**
43
+ * An immutable empty object.
44
+ * @type {Object}
45
+ * @const
46
+ */
47
+ util.emptyObject = Object.freeze ? Object.freeze({}) : /* istanbul ignore next */ {}; // used on prototypes
48
+
49
+ /**
50
+ * Whether running within node or not.
51
+ * @memberof util
52
+ * @type {boolean}
53
+ * @const
54
+ */
55
+ util.isNode = Boolean(util.global.process && util.global.process.versions && util.global.process.versions.node);
56
+
57
+ /**
58
+ * Tests if the specified value is an integer.
59
+ * @function
60
+ * @param {*} value Value to test
61
+ * @returns {boolean} `true` if the value is an integer
62
+ */
63
+ util.isInteger = Number.isInteger || /* istanbul ignore next */ function isInteger(value) {
64
+ return typeof value === "number" && isFinite(value) && Math.floor(value) === value;
65
+ };
66
+
67
+ /**
68
+ * Tests if the specified value is a string.
69
+ * @param {*} value Value to test
70
+ * @returns {boolean} `true` if the value is a string
71
+ */
72
+ util.isString = function isString(value) {
73
+ return typeof value === "string" || value instanceof String;
74
+ };
75
+
76
+ /**
77
+ * Tests if the specified value is a non-null object.
78
+ * @param {*} value Value to test
79
+ * @returns {boolean} `true` if the value is a non-null object
80
+ */
81
+ util.isObject = function isObject(value) {
82
+ return value && typeof value === "object";
83
+ };
84
+
85
+ /**
86
+ * Checks if a property on a message is considered to be present.
87
+ * This is an alias of {@link util.isSet}.
88
+ * @function
89
+ * @param {Object} obj Plain object or message instance
90
+ * @param {string} prop Property name
91
+ * @returns {boolean} `true` if considered to be present, otherwise `false`
92
+ */
93
+ util.isset =
94
+
95
+ /**
96
+ * Checks if a property on a message is considered to be present.
97
+ * @param {Object} obj Plain object or message instance
98
+ * @param {string} prop Property name
99
+ * @returns {boolean} `true` if considered to be present, otherwise `false`
100
+ */
101
+ util.isSet = function isSet(obj, prop) {
102
+ var value = obj[prop];
103
+ if (value != null && obj.hasOwnProperty(prop)) // eslint-disable-line eqeqeq, no-prototype-builtins
104
+ return typeof value !== "object" || (Array.isArray(value) ? value.length : Object.keys(value).length) > 0;
105
+ return false;
106
+ };
107
+
108
+ /**
109
+ * Any compatible Buffer instance.
110
+ * This is a minimal stand-alone definition of a Buffer instance. The actual type is that exported by node's typings.
111
+ * @interface Buffer
112
+ * @extends Uint8Array
113
+ */
114
+
115
+ /**
116
+ * Node's Buffer class if available.
117
+ * @type {Constructor<Buffer>}
118
+ */
119
+ util.Buffer = (function() {
120
+ try {
121
+ var Buffer = util.inquire("buffer").Buffer;
122
+ // refuse to use non-node buffers if not explicitly assigned (perf reasons):
123
+ return Buffer.prototype.utf8Write ? Buffer : /* istanbul ignore next */ null;
124
+ } catch (e) {
125
+ /* istanbul ignore next */
126
+ return null;
127
+ }
128
+ })();
129
+
130
+ // Internal alias of or polyfull for Buffer.from.
131
+ util._Buffer_from = null;
132
+
133
+ // Internal alias of or polyfill for Buffer.allocUnsafe.
134
+ util._Buffer_allocUnsafe = null;
135
+
136
+ /**
137
+ * Creates a new buffer of whatever type supported by the environment.
138
+ * @param {number|number[]} [sizeOrArray=0] Buffer size or number array
139
+ * @returns {Uint8Array|Buffer} Buffer
140
+ */
141
+ util.newBuffer = function newBuffer(sizeOrArray) {
142
+ /* istanbul ignore next */
143
+ return typeof sizeOrArray === "number"
144
+ ? util.Buffer
145
+ ? util._Buffer_allocUnsafe(sizeOrArray)
146
+ : new util.Array(sizeOrArray)
147
+ : util.Buffer
148
+ ? util._Buffer_from(sizeOrArray)
149
+ : typeof Uint8Array === "undefined"
150
+ ? sizeOrArray
151
+ : new Uint8Array(sizeOrArray);
152
+ };
153
+
154
+ /**
155
+ * Array implementation used in the browser. `Uint8Array` if supported, otherwise `Array`.
156
+ * @type {Constructor<Uint8Array>}
157
+ */
158
+ util.Array = typeof Uint8Array !== "undefined" ? Uint8Array /* istanbul ignore next */ : Array;
159
+
160
+ /*
161
+ * Long.js's Long class if available and $ENABLE_LONG is set. This lets us leave it on
162
+ * for this package's tests but have it be off in actual usage-reporting-protobuf use.
163
+ * (We leave it on for some mode where there is no `process` that is used by tests.)
164
+ */
165
+ util.Long = (typeof process === 'undefined' || process.env.ENABLE_LONG) ? (/* istanbul ignore next */ util.global.dcodeIO && /* istanbul ignore next */ util.global.dcodeIO.Long
166
+ || /* istanbul ignore next */ util.global.Long
167
+ || util.inquire("long")) : undefined;
168
+
169
+ /**
170
+ * Regular expression used to verify 2 bit (`bool`) map keys.
171
+ * @type {RegExp}
172
+ * @const
173
+ */
174
+ util.key2Re = /^true|false|0|1$/;
175
+
176
+ /**
177
+ * Regular expression used to verify 32 bit (`int32` etc.) map keys.
178
+ * @type {RegExp}
179
+ * @const
180
+ */
181
+ util.key32Re = /^-?(?:0|[1-9][0-9]*)$/;
182
+
183
+ /**
184
+ * Regular expression used to verify 64 bit (`int64` etc.) map keys.
185
+ * @type {RegExp}
186
+ * @const
187
+ */
188
+ util.key64Re = /^(?:[\\x00-\\xff]{8}|-?(?:0|[1-9][0-9]*))$/;
189
+
190
+ /*
191
+ * Converts a number or long to an 8 characters long hash string.
192
+ * @param {Long|number} value Value to convert
193
+ * @returns {string} Hash
194
+ */
195
+ util.longToHash = function longToHash(value) {
196
+ return value
197
+ ? util.LongBits.from(value).toHash()
198
+ : util.LongBits.zeroHash;
199
+ };
200
+
201
+ /*
202
+ * Converts an 8 characters long hash string to a long or number.
203
+ * @param {string} hash Hash
204
+ * @param {boolean} [unsigned=false] Whether unsigned or not
205
+ * @returns {Long|number} Original value
206
+ */
207
+ util.longFromHash = function longFromHash(hash, unsigned) {
208
+ var bits = util.LongBits.fromHash(hash);
209
+ if (util.Long)
210
+ return util.Long.fromBits(bits.lo, bits.hi, unsigned);
211
+ return bits.toNumber(Boolean(unsigned));
212
+ };
213
+
214
+ /**
215
+ * Merges the properties of the source object into the destination object.
216
+ * @memberof util
217
+ * @param {Object.<string,*>} dst Destination object
218
+ * @param {Object.<string,*>} src Source object
219
+ * @param {boolean} [ifNotSet=false] Merges only if the key is not already set
220
+ * @returns {Object.<string,*>} Destination object
221
+ */
222
+ function merge(dst, src, ifNotSet) { // used by converters
223
+ for (var keys = Object.keys(src), i = 0; i < keys.length; ++i)
224
+ if (dst[keys[i]] === undefined || !ifNotSet)
225
+ dst[keys[i]] = src[keys[i]];
226
+ return dst;
227
+ }
228
+
229
+ util.merge = merge;
230
+
231
+ /**
232
+ * Converts the first character of a string to lower case.
233
+ * @param {string} str String to convert
234
+ * @returns {string} Converted string
235
+ */
236
+ util.lcFirst = function lcFirst(str) {
237
+ return str.charAt(0).toLowerCase() + str.substring(1);
238
+ };
239
+
240
+ /**
241
+ * Creates a custom error constructor.
242
+ * @memberof util
243
+ * @param {string} name Error name
244
+ * @returns {Constructor<Error>} Custom error constructor
245
+ */
246
+ function newError(name) {
247
+
248
+ function CustomError(message, properties) {
249
+
250
+ if (!(this instanceof CustomError))
251
+ return new CustomError(message, properties);
252
+
253
+ // Error.call(this, message);
254
+ // ^ just returns a new error instance because the ctor can be called as a function
255
+
256
+ Object.defineProperty(this, "message", { get: function() { return message; } });
257
+
258
+ /* istanbul ignore next */
259
+ if (Error.captureStackTrace) // node
260
+ Error.captureStackTrace(this, CustomError);
261
+ else
262
+ Object.defineProperty(this, "stack", { value: (new Error()).stack || "" });
263
+
264
+ if (properties)
265
+ merge(this, properties);
266
+ }
267
+
268
+ (CustomError.prototype = Object.create(Error.prototype)).constructor = CustomError;
269
+
270
+ Object.defineProperty(CustomError.prototype, "name", { get: function() { return name; } });
271
+
272
+ CustomError.prototype.toString = function toString() {
273
+ return this.name + ": " + this.message;
274
+ };
275
+
276
+ return CustomError;
277
+ }
278
+
279
+ util.newError = newError;
280
+
281
+ /**
282
+ * Constructs a new protocol error.
283
+ * @classdesc Error subclass indicating a protocol specifc error.
284
+ * @memberof util
285
+ * @extends Error
286
+ * @template T extends Message<T>
287
+ * @constructor
288
+ * @param {string} message Error message
289
+ * @param {Object.<string,*>} [properties] Additional properties
290
+ * @example
291
+ * try {
292
+ * MyMessage.decode(someBuffer); // throws if required fields are missing
293
+ * } catch (e) {
294
+ * if (e instanceof ProtocolError && e.instance)
295
+ * console.log("decoded so far: " + JSON.stringify(e.instance));
296
+ * }
297
+ */
298
+ util.ProtocolError = newError("ProtocolError");
299
+
300
+ /**
301
+ * So far decoded message instance.
302
+ * @name util.ProtocolError#instance
303
+ * @type {Message<T>}
304
+ */
305
+
306
+ /**
307
+ * A OneOf getter as returned by {@link util.oneOfGetter}.
308
+ * @typedef OneOfGetter
309
+ * @type {function}
310
+ * @returns {string|undefined} Set field name, if any
311
+ */
312
+
313
+ /**
314
+ * Builds a getter for a oneof's present field name.
315
+ * @param {string[]} fieldNames Field names
316
+ * @returns {OneOfGetter} Unbound getter
317
+ */
318
+ util.oneOfGetter = function getOneOf(fieldNames) {
319
+ var fieldMap = {};
320
+ for (var i = 0; i < fieldNames.length; ++i)
321
+ fieldMap[fieldNames[i]] = 1;
322
+
323
+ /**
324
+ * @returns {string|undefined} Set field name, if any
325
+ * @this Object
326
+ * @ignore
327
+ */
328
+ return function() { // eslint-disable-line consistent-return
329
+ for (var keys = Object.keys(this), i = keys.length - 1; i > -1; --i)
330
+ if (fieldMap[keys[i]] === 1 && this[keys[i]] !== undefined && this[keys[i]] !== null)
331
+ return keys[i];
332
+ };
333
+ };
334
+
335
+ /**
336
+ * A OneOf setter as returned by {@link util.oneOfSetter}.
337
+ * @typedef OneOfSetter
338
+ * @type {function}
339
+ * @param {string|undefined} value Field name
340
+ * @returns {undefined}
341
+ */
342
+
343
+ /**
344
+ * Builds a setter for a oneof's present field name.
345
+ * @param {string[]} fieldNames Field names
346
+ * @returns {OneOfSetter} Unbound setter
347
+ */
348
+ util.oneOfSetter = function setOneOf(fieldNames) {
349
+
350
+ /**
351
+ * @param {string} name Field name
352
+ * @returns {undefined}
353
+ * @this Object
354
+ * @ignore
355
+ */
356
+ return function(name) {
357
+ for (var i = 0; i < fieldNames.length; ++i)
358
+ if (fieldNames[i] !== name)
359
+ delete this[fieldNames[i]];
360
+ };
361
+ };
362
+
363
+ /**
364
+ * Default conversion options used for {@link Message#toJSON} implementations.
365
+ *
366
+ * These options are close to proto3's JSON mapping with the exception that internal types like Any are handled just like messages. More precisely:
367
+ *
368
+ * - Longs become strings
369
+ * - Enums become string keys
370
+ * - Bytes become base64 encoded strings
371
+ * - (Sub-)Messages become plain objects
372
+ * - Maps become plain objects with all string keys
373
+ * - Repeated fields become arrays
374
+ * - NaN and Infinity for float and double fields become strings
375
+ *
376
+ * @type {IConversionOptions}
377
+ * @see https://developers.google.com/protocol-buffers/docs/proto3?hl=en#json
378
+ */
379
+ util.toJSONOptions = {
380
+ longs: String,
381
+ enums: String,
382
+ bytes: String,
383
+ json: true
384
+ };
385
+
386
+ // Sets up buffer utility according to the environment (called in index-minimal)
387
+ util._configure = function() {
388
+ var Buffer = util.Buffer;
389
+ /* istanbul ignore if */
390
+ if (!Buffer) {
391
+ util._Buffer_from = util._Buffer_allocUnsafe = null;
392
+ return;
393
+ }
394
+ // because node 4.x buffers are incompatible & immutable
395
+ // see: https://github.com/dcodeIO/protobuf.js/pull/665
396
+ util._Buffer_from = Buffer.from !== Uint8Array.from && Buffer.from ||
397
+ /* istanbul ignore next */
398
+ function Buffer_from(value, encoding) {
399
+ return new Buffer(value, encoding);
400
+ };
401
+ util._Buffer_allocUnsafe = Buffer.allocUnsafe ||
402
+ /* istanbul ignore next */
403
+ function Buffer_allocUnsafe(size) {
404
+ return new Buffer(size);
405
+ };
406
+ };
package/src/util.js ADDED
@@ -0,0 +1,178 @@
1
+ "use strict";
2
+
3
+ /**
4
+ * Various utility functions.
5
+ * @namespace
6
+ */
7
+ var util = module.exports = require("./util/minimal");
8
+
9
+ var roots = require("./roots");
10
+
11
+ var Type, // cyclic
12
+ Enum;
13
+
14
+ util.codegen = require("@protobufjs/codegen");
15
+ util.fetch = require("@protobufjs/fetch");
16
+ util.path = require("@protobufjs/path");
17
+
18
+ /**
19
+ * Node's fs module if available.
20
+ * @type {Object.<string,*>}
21
+ */
22
+ util.fs = util.inquire("fs");
23
+
24
+ /**
25
+ * Converts an object's values to an array.
26
+ * @param {Object.<string,*>} object Object to convert
27
+ * @returns {Array.<*>} Converted array
28
+ */
29
+ util.toArray = function toArray(object) {
30
+ if (object) {
31
+ var keys = Object.keys(object),
32
+ array = new Array(keys.length),
33
+ index = 0;
34
+ while (index < keys.length)
35
+ array[index] = object[keys[index++]];
36
+ return array;
37
+ }
38
+ return [];
39
+ };
40
+
41
+ /**
42
+ * Converts an array of keys immediately followed by their respective value to an object, omitting undefined values.
43
+ * @param {Array.<*>} array Array to convert
44
+ * @returns {Object.<string,*>} Converted object
45
+ */
46
+ util.toObject = function toObject(array) {
47
+ var object = {},
48
+ index = 0;
49
+ while (index < array.length) {
50
+ var key = array[index++],
51
+ val = array[index++];
52
+ if (val !== undefined)
53
+ object[key] = val;
54
+ }
55
+ return object;
56
+ };
57
+
58
+ var safePropBackslashRe = /\\/g,
59
+ safePropQuoteRe = /"/g;
60
+
61
+ /**
62
+ * Tests whether the specified name is a reserved word in JS.
63
+ * @param {string} name Name to test
64
+ * @returns {boolean} `true` if reserved, otherwise `false`
65
+ */
66
+ util.isReserved = function isReserved(name) {
67
+ return /^(?:do|if|in|for|let|new|try|var|case|else|enum|eval|false|null|this|true|void|with|break|catch|class|const|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|instanceof)$/.test(name);
68
+ };
69
+
70
+ /**
71
+ * Returns a safe property accessor for the specified property name.
72
+ * @param {string} prop Property name
73
+ * @returns {string} Safe accessor
74
+ */
75
+ util.safeProp = function safeProp(prop) {
76
+ if (!/^[$\w_]+$/.test(prop) || util.isReserved(prop))
77
+ return "[\"" + prop.replace(safePropBackslashRe, "\\\\").replace(safePropQuoteRe, "\\\"") + "\"]";
78
+ return "." + prop;
79
+ };
80
+
81
+ /**
82
+ * Converts the first character of a string to upper case.
83
+ * @param {string} str String to convert
84
+ * @returns {string} Converted string
85
+ */
86
+ util.ucFirst = function ucFirst(str) {
87
+ return str.charAt(0).toUpperCase() + str.substring(1);
88
+ };
89
+
90
+ var camelCaseRe = /_([a-z])/g;
91
+
92
+ /**
93
+ * Converts a string to camel case.
94
+ * @param {string} str String to convert
95
+ * @returns {string} Converted string
96
+ */
97
+ util.camelCase = function camelCase(str) {
98
+ return str.substring(0, 1)
99
+ + str.substring(1)
100
+ .replace(camelCaseRe, function($0, $1) { return $1.toUpperCase(); });
101
+ };
102
+
103
+ /**
104
+ * Compares reflected fields by id.
105
+ * @param {Field} a First field
106
+ * @param {Field} b Second field
107
+ * @returns {number} Comparison value
108
+ */
109
+ util.compareFieldsById = function compareFieldsById(a, b) {
110
+ return a.id - b.id;
111
+ };
112
+
113
+ /**
114
+ * Decorator helper for types (TypeScript).
115
+ * @param {Constructor<T>} ctor Constructor function
116
+ * @param {string} [typeName] Type name, defaults to the constructor's name
117
+ * @returns {Type} Reflected type
118
+ * @template T extends Message<T>
119
+ * @property {Root} root Decorators root
120
+ */
121
+ util.decorateType = function decorateType(ctor, typeName) {
122
+
123
+ /* istanbul ignore if */
124
+ if (ctor.$type) {
125
+ if (typeName && ctor.$type.name !== typeName) {
126
+ util.decorateRoot.remove(ctor.$type);
127
+ ctor.$type.name = typeName;
128
+ util.decorateRoot.add(ctor.$type);
129
+ }
130
+ return ctor.$type;
131
+ }
132
+
133
+ /* istanbul ignore next */
134
+ if (!Type)
135
+ Type = require("./type");
136
+
137
+ var type = new Type(typeName || ctor.name);
138
+ util.decorateRoot.add(type);
139
+ type.ctor = ctor; // sets up .encode, .decode etc.
140
+ Object.defineProperty(ctor, "$type", { value: type, enumerable: false });
141
+ Object.defineProperty(ctor.prototype, "$type", { value: type, enumerable: false });
142
+ return type;
143
+ };
144
+
145
+ var decorateEnumIndex = 0;
146
+
147
+ /**
148
+ * Decorator helper for enums (TypeScript).
149
+ * @param {Object} object Enum object
150
+ * @returns {Enum} Reflected enum
151
+ */
152
+ util.decorateEnum = function decorateEnum(object) {
153
+
154
+ /* istanbul ignore if */
155
+ if (object.$type)
156
+ return object.$type;
157
+
158
+ /* istanbul ignore next */
159
+ if (!Enum)
160
+ Enum = require("./enum");
161
+
162
+ var enm = new Enum("Enum" + decorateEnumIndex++, object);
163
+ util.decorateRoot.add(enm);
164
+ Object.defineProperty(object, "$type", { value: enm, enumerable: false });
165
+ return enm;
166
+ };
167
+
168
+ /**
169
+ * Decorator root (TypeScript).
170
+ * @name util.decorateRoot
171
+ * @type {Root}
172
+ * @readonly
173
+ */
174
+ Object.defineProperty(util, "decorateRoot", {
175
+ get: function() {
176
+ return roots["decorated"] || (roots["decorated"] = new (require("./root"))());
177
+ }
178
+ });