Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc-cloned / node_modules / protobufjs / src / protobuf.js
1 /**\r
2  * The ProtoBuf namespace.\r
3  * @exports ProtoBuf\r
4  * @namespace\r
5  * @expose\r
6  */\r
7 var ProtoBuf = {};\r
8 \r
9 /**\r
10  * @type {!function(new: ByteBuffer, ...[*])}\r
11  * @expose\r
12  */\r
13 ProtoBuf.ByteBuffer = ByteBuffer;\r
14 \r
15 /**\r
16  * @type {?function(new: Long, ...[*])}\r
17  * @expose\r
18  */\r
19 ProtoBuf.Long = ByteBuffer.Long || null;\r
20 \r
21 /**\r
22  * ProtoBuf.js version.\r
23  * @type {string}\r
24  * @const\r
25  * @expose\r
26  */\r
27 ProtoBuf.VERSION = "/*?= VERSION */";\r
28 \r
29 /**\r
30  * Wire types.\r
31  * @type {Object.<string,number>}\r
32  * @const\r
33  * @expose\r
34  */\r
35 ProtoBuf.WIRE_TYPES = {};\r
36 \r
37 /**\r
38  * Varint wire type.\r
39  * @type {number}\r
40  * @expose\r
41  */\r
42 ProtoBuf.WIRE_TYPES.VARINT = 0;\r
43 \r
44 /**\r
45  * Fixed 64 bits wire type.\r
46  * @type {number}\r
47  * @const\r
48  * @expose\r
49  */\r
50 ProtoBuf.WIRE_TYPES.BITS64 = 1;\r
51 \r
52 /**\r
53  * Length delimited wire type.\r
54  * @type {number}\r
55  * @const\r
56  * @expose\r
57  */\r
58 ProtoBuf.WIRE_TYPES.LDELIM = 2;\r
59 \r
60 /**\r
61  * Start group wire type.\r
62  * @type {number}\r
63  * @const\r
64  * @expose\r
65  */\r
66 ProtoBuf.WIRE_TYPES.STARTGROUP = 3;\r
67 \r
68 /**\r
69  * End group wire type.\r
70  * @type {number}\r
71  * @const\r
72  * @expose\r
73  */\r
74 ProtoBuf.WIRE_TYPES.ENDGROUP = 4;\r
75 \r
76 /**\r
77  * Fixed 32 bits wire type.\r
78  * @type {number}\r
79  * @const\r
80  * @expose\r
81  */\r
82 ProtoBuf.WIRE_TYPES.BITS32 = 5;\r
83 \r
84 /**\r
85  * Packable wire types.\r
86  * @type {!Array.<number>}\r
87  * @const\r
88  * @expose\r
89  */\r
90 ProtoBuf.PACKABLE_WIRE_TYPES = [\r
91     ProtoBuf.WIRE_TYPES.VARINT,\r
92     ProtoBuf.WIRE_TYPES.BITS64,\r
93     ProtoBuf.WIRE_TYPES.BITS32\r
94 ];\r
95 \r
96 /**\r
97  * Types.\r
98  * @dict\r
99  * @type {!Object.<string,{name: string, wireType: number, defaultValue: *}>}\r
100  * @const\r
101  * @expose\r
102  */\r
103 ProtoBuf.TYPES = {\r
104     // According to the protobuf spec.\r
105     "int32": {\r
106         name: "int32",\r
107         wireType: ProtoBuf.WIRE_TYPES.VARINT,\r
108         defaultValue: 0\r
109     },\r
110     "uint32": {\r
111         name: "uint32",\r
112         wireType: ProtoBuf.WIRE_TYPES.VARINT,\r
113         defaultValue: 0\r
114     },\r
115     "sint32": {\r
116         name: "sint32",\r
117         wireType: ProtoBuf.WIRE_TYPES.VARINT,\r
118         defaultValue: 0\r
119     },\r
120     "int64": {\r
121         name: "int64",\r
122         wireType: ProtoBuf.WIRE_TYPES.VARINT,\r
123         defaultValue: ProtoBuf.Long ? ProtoBuf.Long.ZERO : undefined\r
124     },\r
125     "uint64": {\r
126         name: "uint64",\r
127         wireType: ProtoBuf.WIRE_TYPES.VARINT,\r
128         defaultValue: ProtoBuf.Long ? ProtoBuf.Long.UZERO : undefined\r
129     },\r
130     "sint64": {\r
131         name: "sint64",\r
132         wireType: ProtoBuf.WIRE_TYPES.VARINT,\r
133         defaultValue: ProtoBuf.Long ? ProtoBuf.Long.ZERO : undefined\r
134     },\r
135     "bool": {\r
136         name: "bool",\r
137         wireType: ProtoBuf.WIRE_TYPES.VARINT,\r
138         defaultValue: false\r
139     },\r
140     "double": {\r
141         name: "double",\r
142         wireType: ProtoBuf.WIRE_TYPES.BITS64,\r
143         defaultValue: 0\r
144     },\r
145     "string": {\r
146         name: "string",\r
147         wireType: ProtoBuf.WIRE_TYPES.LDELIM,\r
148         defaultValue: ""\r
149     },\r
150     "bytes": {\r
151         name: "bytes",\r
152         wireType: ProtoBuf.WIRE_TYPES.LDELIM,\r
153         defaultValue: null // overridden in the code, must be a unique instance\r
154     },\r
155     "fixed32": {\r
156         name: "fixed32",\r
157         wireType: ProtoBuf.WIRE_TYPES.BITS32,\r
158         defaultValue: 0\r
159     },\r
160     "sfixed32": {\r
161         name: "sfixed32",\r
162         wireType: ProtoBuf.WIRE_TYPES.BITS32,\r
163         defaultValue: 0\r
164     },\r
165     "fixed64": {\r
166         name: "fixed64",\r
167         wireType: ProtoBuf.WIRE_TYPES.BITS64,\r
168         defaultValue:  ProtoBuf.Long ? ProtoBuf.Long.UZERO : undefined\r
169     },\r
170     "sfixed64": {\r
171         name: "sfixed64",\r
172         wireType: ProtoBuf.WIRE_TYPES.BITS64,\r
173         defaultValue: ProtoBuf.Long ? ProtoBuf.Long.ZERO : undefined\r
174     },\r
175     "float": {\r
176         name: "float",\r
177         wireType: ProtoBuf.WIRE_TYPES.BITS32,\r
178         defaultValue: 0\r
179     },\r
180     "enum": {\r
181         name: "enum",\r
182         wireType: ProtoBuf.WIRE_TYPES.VARINT,\r
183         defaultValue: 0\r
184     },\r
185     "message": {\r
186         name: "message",\r
187         wireType: ProtoBuf.WIRE_TYPES.LDELIM,\r
188         defaultValue: null\r
189     },\r
190     "group": {\r
191         name: "group",\r
192         wireType: ProtoBuf.WIRE_TYPES.STARTGROUP,\r
193         defaultValue: null\r
194     }\r
195 };\r
196 \r
197 /**\r
198  * Valid map key types.\r
199  * @type {!Array.<!Object.<string,{name: string, wireType: number, defaultValue: *}>>}\r
200  * @const\r
201  * @expose\r
202  */\r
203 ProtoBuf.MAP_KEY_TYPES = [\r
204     ProtoBuf.TYPES["int32"],\r
205     ProtoBuf.TYPES["sint32"],\r
206     ProtoBuf.TYPES["sfixed32"],\r
207     ProtoBuf.TYPES["uint32"],\r
208     ProtoBuf.TYPES["fixed32"],\r
209     ProtoBuf.TYPES["int64"],\r
210     ProtoBuf.TYPES["sint64"],\r
211     ProtoBuf.TYPES["sfixed64"],\r
212     ProtoBuf.TYPES["uint64"],\r
213     ProtoBuf.TYPES["fixed64"],\r
214     ProtoBuf.TYPES["bool"],\r
215     ProtoBuf.TYPES["string"],\r
216     ProtoBuf.TYPES["bytes"]\r
217 ];\r
218 \r
219 /**\r
220  * Minimum field id.\r
221  * @type {number}\r
222  * @const\r
223  * @expose\r
224  */\r
225 ProtoBuf.ID_MIN = 1;\r
226 \r
227 /**\r
228  * Maximum field id.\r
229  * @type {number}\r
230  * @const\r
231  * @expose\r
232  */\r
233 ProtoBuf.ID_MAX = 0x1FFFFFFF;\r
234 \r
235 /**\r
236  * If set to `true`, field names will be converted from underscore notation to camel case. Defaults to `false`.\r
237  *  Must be set prior to parsing.\r
238  * @type {boolean}\r
239  * @expose\r
240  */\r
241 ProtoBuf.convertFieldsToCamelCase = false;\r
242 \r
243 /**\r
244  * By default, messages are populated with (setX, set_x) accessors for each field. This can be disabled by\r
245  *  setting this to `false` prior to building messages.\r
246  * @type {boolean}\r
247  * @expose\r
248  */\r
249 ProtoBuf.populateAccessors = true;\r
250 \r
251 /**\r
252  * By default, messages are populated with default values if a field is not present on the wire. To disable\r
253  *  this behavior, set this setting to `false`.\r
254  * @type {boolean}\r
255  * @expose\r
256  */\r
257 ProtoBuf.populateDefaults = true;\r
258 \r
259 //? include("ProtoBuf/Util.js");\r
260 \r
261 //? include("ProtoBuf/Lang.js");\r
262 \r
263 //? if (DOTPROTO) include("ProtoBuf/DotProto.js");\r
264 \r
265 //? include("ProtoBuf/Reflect.js");\r
266 \r
267 //? include("ProtoBuf/Builder.js");\r
268 \r
269 //? include("ProtoBuf/Map.js");\r
270 \r
271 //? if (DOTPROTO) {\r
272 \r
273 /**\r
274  * Loads a .proto string and returns the Builder.\r
275  * @param {string} proto .proto file contents\r
276  * @param {(ProtoBuf.Builder|string|{root: string, file: string})=} builder Builder to append to. Will create a new one if omitted.\r
277  * @param {(string|{root: string, file: string})=} filename The corresponding file name if known. Must be specified for imports.\r
278  * @return {ProtoBuf.Builder} Builder to create new messages\r
279  * @throws {Error} If the definition cannot be parsed or built\r
280  * @expose\r
281  */\r
282 ProtoBuf.loadProto = function(proto, builder, filename) {\r
283     if (typeof builder === 'string' || (builder && typeof builder["file"] === 'string' && typeof builder["root"] === 'string'))\r
284         filename = builder,\r
285         builder = undefined;\r
286     return ProtoBuf.loadJson(ProtoBuf.DotProto.Parser.parse(proto), builder, filename);\r
287 };\r
288 \r
289 /**\r
290  * Loads a .proto string and returns the Builder. This is an alias of {@link ProtoBuf.loadProto}.\r
291  * @function\r
292  * @param {string} proto .proto file contents\r
293  * @param {(ProtoBuf.Builder|string)=} builder Builder to append to. Will create a new one if omitted.\r
294  * @param {(string|{root: string, file: string})=} filename The corresponding file name if known. Must be specified for imports.\r
295  * @return {ProtoBuf.Builder} Builder to create new messages\r
296  * @throws {Error} If the definition cannot be parsed or built\r
297  * @expose\r
298  */\r
299 ProtoBuf.protoFromString = ProtoBuf.loadProto; // Legacy\r
300 \r
301 /**\r
302  * Loads a .proto file and returns the Builder.\r
303  * @param {string|{root: string, file: string}} filename Path to proto file or an object specifying 'file' with\r
304  *  an overridden 'root' path for all imported files.\r
305  * @param {function(?Error, !ProtoBuf.Builder=)=} callback Callback that will receive `null` as the first and\r
306  *  the Builder as its second argument on success, otherwise the error as its first argument. If omitted, the\r
307  *  file will be read synchronously and this function will return the Builder.\r
308  * @param {ProtoBuf.Builder=} builder Builder to append to. Will create a new one if omitted.\r
309  * @return {?ProtoBuf.Builder|undefined} The Builder if synchronous (no callback specified, will be NULL if the\r
310  *   request has failed), else undefined\r
311  * @expose\r
312  */\r
313 ProtoBuf.loadProtoFile = function(filename, callback, builder) {\r
314     if (callback && typeof callback === 'object')\r
315         builder = callback,\r
316         callback = null;\r
317     else if (!callback || typeof callback !== 'function')\r
318         callback = null;\r
319     if (callback)\r
320         return ProtoBuf.Util.fetch(typeof filename === 'string' ? filename : filename["root"]+"/"+filename["file"], function(contents) {\r
321             if (contents === null) {\r
322                 callback(Error("Failed to fetch file"));\r
323                 return;\r
324             }\r
325             try {\r
326                 callback(null, ProtoBuf.loadProto(contents, builder, filename));\r
327             } catch (e) {\r
328                 callback(e);\r
329             }\r
330         });\r
331     var contents = ProtoBuf.Util.fetch(typeof filename === 'object' ? filename["root"]+"/"+filename["file"] : filename);\r
332     return contents === null ? null : ProtoBuf.loadProto(contents, builder, filename);\r
333 };\r
334 \r
335 /**\r
336  * Loads a .proto file and returns the Builder. This is an alias of {@link ProtoBuf.loadProtoFile}.\r
337  * @function\r
338  * @param {string|{root: string, file: string}} filename Path to proto file or an object specifying 'file' with\r
339  *  an overridden 'root' path for all imported files.\r
340  * @param {function(?Error, !ProtoBuf.Builder=)=} callback Callback that will receive `null` as the first and\r
341  *  the Builder as its second argument on success, otherwise the error as its first argument. If omitted, the\r
342  *  file will be read synchronously and this function will return the Builder.\r
343  * @param {ProtoBuf.Builder=} builder Builder to append to. Will create a new one if omitted.\r
344  * @return {!ProtoBuf.Builder|undefined} The Builder if synchronous (no callback specified, will be NULL if the\r
345  *   request has failed), else undefined\r
346  * @expose\r
347  */\r
348 ProtoBuf.protoFromFile = ProtoBuf.loadProtoFile; // Legacy\r
349 \r
350 //? } // DOTPROTO\r
351 \r
352 /**\r
353  * Constructs a new empty Builder.\r
354  * @param {Object.<string,*>=} options Builder options, defaults to global options set on ProtoBuf\r
355  * @return {!ProtoBuf.Builder} Builder\r
356  * @expose\r
357  */\r
358 ProtoBuf.newBuilder = function(options) {\r
359     options = options || {};\r
360     if (typeof options['convertFieldsToCamelCase'] === 'undefined')\r
361         options['convertFieldsToCamelCase'] = ProtoBuf.convertFieldsToCamelCase;\r
362     if (typeof options['populateAccessors'] === 'undefined')\r
363         options['populateAccessors'] = ProtoBuf.populateAccessors;\r
364     return new ProtoBuf.Builder(options);\r
365 };\r
366 \r
367 /**\r
368  * Loads a .json definition and returns the Builder.\r
369  * @param {!*|string} json JSON definition\r
370  * @param {(ProtoBuf.Builder|string|{root: string, file: string})=} builder Builder to append to. Will create a new one if omitted.\r
371  * @param {(string|{root: string, file: string})=} filename The corresponding file name if known. Must be specified for imports.\r
372  * @return {ProtoBuf.Builder} Builder to create new messages\r
373  * @throws {Error} If the definition cannot be parsed or built\r
374  * @expose\r
375  */\r
376 ProtoBuf.loadJson = function(json, builder, filename) {\r
377     if (typeof builder === 'string' || (builder && typeof builder["file"] === 'string' && typeof builder["root"] === 'string'))\r
378         filename = builder,\r
379         builder = null;\r
380     if (!builder || typeof builder !== 'object')\r
381         builder = ProtoBuf.newBuilder();\r
382     if (typeof json === 'string')\r
383         json = JSON.parse(json);\r
384     builder["import"](json, filename);\r
385     builder.resolveAll();\r
386     return builder;\r
387 };\r
388 \r
389 /**\r
390  * Loads a .json file and returns the Builder.\r
391  * @param {string|!{root: string, file: string}} filename Path to json file or an object specifying 'file' with\r
392  *  an overridden 'root' path for all imported files.\r
393  * @param {function(?Error, !ProtoBuf.Builder=)=} callback Callback that will receive `null` as the first and\r
394  *  the Builder as its second argument on success, otherwise the error as its first argument. If omitted, the\r
395  *  file will be read synchronously and this function will return the Builder.\r
396  * @param {ProtoBuf.Builder=} builder Builder to append to. Will create a new one if omitted.\r
397  * @return {?ProtoBuf.Builder|undefined} The Builder if synchronous (no callback specified, will be NULL if the\r
398  *   request has failed), else undefined\r
399  * @expose\r
400  */\r
401 ProtoBuf.loadJsonFile = function(filename, callback, builder) {\r
402     if (callback && typeof callback === 'object')\r
403         builder = callback,\r
404         callback = null;\r
405     else if (!callback || typeof callback !== 'function')\r
406         callback = null;\r
407     if (callback)\r
408         return ProtoBuf.Util.fetch(typeof filename === 'string' ? filename : filename["root"]+"/"+filename["file"], function(contents) {\r
409             if (contents === null) {\r
410                 callback(Error("Failed to fetch file"));\r
411                 return;\r
412             }\r
413             try {\r
414                 callback(null, ProtoBuf.loadJson(JSON.parse(contents), builder, filename));\r
415             } catch (e) {\r
416                 callback(e);\r
417             }\r
418         });\r
419     var contents = ProtoBuf.Util.fetch(typeof filename === 'object' ? filename["root"]+"/"+filename["file"] : filename);\r
420     return contents === null ? null : ProtoBuf.loadJson(JSON.parse(contents), builder, filename);\r
421 };\r