Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / protobufjs / src / util / minimal.js
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)).constructor = CustomError;
284
285     Object.defineProperty(CustomError.prototype, "name", { get: function() { return name; } });
286
287     CustomError.prototype.toString = function toString() {
288         return this.name + ": " + this.message;
289     };
290
291     return CustomError;
292 }
293
294 util.newError = newError;
295
296 /**
297  * Constructs a new protocol error.
298  * @classdesc Error subclass indicating a protocol specifc error.
299  * @memberof util
300  * @extends Error
301  * @template T extends Message<T>
302  * @constructor
303  * @param {string} message Error message
304  * @param {Object.<string,*>} [properties] Additional properties
305  * @example
306  * try {
307  *     MyMessage.decode(someBuffer); // throws if required fields are missing
308  * } catch (e) {
309  *     if (e instanceof ProtocolError && e.instance)
310  *         console.log("decoded so far: " + JSON.stringify(e.instance));
311  * }
312  */
313 util.ProtocolError = newError("ProtocolError");
314
315 /**
316  * So far decoded message instance.
317  * @name util.ProtocolError#instance
318  * @type {Message<T>}
319  */
320
321 /**
322  * A OneOf getter as returned by {@link util.oneOfGetter}.
323  * @typedef OneOfGetter
324  * @type {function}
325  * @returns {string|undefined} Set field name, if any
326  */
327
328 /**
329  * Builds a getter for a oneof's present field name.
330  * @param {string[]} fieldNames Field names
331  * @returns {OneOfGetter} Unbound getter
332  */
333 util.oneOfGetter = function getOneOf(fieldNames) {
334     var fieldMap = {};
335     for (var i = 0; i < fieldNames.length; ++i)
336         fieldMap[fieldNames[i]] = 1;
337
338     /**
339      * @returns {string|undefined} Set field name, if any
340      * @this Object
341      * @ignore
342      */
343     return function() { // eslint-disable-line consistent-return
344         for (var keys = Object.keys(this), i = keys.length - 1; i > -1; --i)
345             if (fieldMap[keys[i]] === 1 && this[keys[i]] !== undefined && this[keys[i]] !== null)
346                 return keys[i];
347     };
348 };
349
350 /**
351  * A OneOf setter as returned by {@link util.oneOfSetter}.
352  * @typedef OneOfSetter
353  * @type {function}
354  * @param {string|undefined} value Field name
355  * @returns {undefined}
356  */
357
358 /**
359  * Builds a setter for a oneof's present field name.
360  * @param {string[]} fieldNames Field names
361  * @returns {OneOfSetter} Unbound setter
362  */
363 util.oneOfSetter = function setOneOf(fieldNames) {
364
365     /**
366      * @param {string} name Field name
367      * @returns {undefined}
368      * @this Object
369      * @ignore
370      */
371     return function(name) {
372         for (var i = 0; i < fieldNames.length; ++i)
373             if (fieldNames[i] !== name)
374                 delete this[fieldNames[i]];
375     };
376 };
377
378 /**
379  * Default conversion options used for {@link Message#toJSON} implementations.
380  *
381  * These options are close to proto3's JSON mapping with the exception that internal types like Any are handled just like messages. More precisely:
382  *
383  * - Longs become strings
384  * - Enums become string keys
385  * - Bytes become base64 encoded strings
386  * - (Sub-)Messages become plain objects
387  * - Maps become plain objects with all string keys
388  * - Repeated fields become arrays
389  * - NaN and Infinity for float and double fields become strings
390  *
391  * @type {IConversionOptions}
392  * @see https://developers.google.com/protocol-buffers/docs/proto3?hl=en#json
393  */
394 util.toJSONOptions = {
395     longs: String,
396     enums: String,
397     bytes: String,
398     json: true
399 };
400
401 // Sets up buffer utility according to the environment (called in index-minimal)
402 util._configure = function() {
403     var Buffer = util.Buffer;
404     /* istanbul ignore if */
405     if (!Buffer) {
406         util._Buffer_from = util._Buffer_allocUnsafe = null;
407         return;
408     }
409     // because node 4.x buffers are incompatible & immutable
410     // see: https://github.com/dcodeIO/protobuf.js/pull/665
411     util._Buffer_from = Buffer.from !== Uint8Array.from && Buffer.from ||
412         /* istanbul ignore next */
413         function Buffer_from(value, encoding) {
414             return new Buffer(value, encoding);
415         };
416     util._Buffer_allocUnsafe = Buffer.allocUnsafe ||
417         /* istanbul ignore next */
418         function Buffer_allocUnsafe(size) {
419             return new Buffer(size);
420         };
421 };