No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 34KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  1. "use strict";
  2. var $protobuf = require("../..");
  3. module.exports = exports = $protobuf.descriptor = $protobuf.Root.fromJSON(require("../../google/protobuf/descriptor.json")).lookup(".google.protobuf");
  4. var Namespace = $protobuf.Namespace,
  5. Root = $protobuf.Root,
  6. Enum = $protobuf.Enum,
  7. Type = $protobuf.Type,
  8. Field = $protobuf.Field,
  9. MapField = $protobuf.MapField,
  10. OneOf = $protobuf.OneOf,
  11. Service = $protobuf.Service,
  12. Method = $protobuf.Method;
  13. // --- Root ---
  14. /**
  15. * Properties of a FileDescriptorSet message.
  16. * @interface IFileDescriptorSet
  17. * @property {IFileDescriptorProto[]} file Files
  18. */
  19. /**
  20. * Properties of a FileDescriptorProto message.
  21. * @interface IFileDescriptorProto
  22. * @property {string} [name] File name
  23. * @property {string} [package] Package
  24. * @property {*} [dependency] Not supported
  25. * @property {*} [publicDependency] Not supported
  26. * @property {*} [weakDependency] Not supported
  27. * @property {IDescriptorProto[]} [messageType] Nested message types
  28. * @property {IEnumDescriptorProto[]} [enumType] Nested enums
  29. * @property {IServiceDescriptorProto[]} [service] Nested services
  30. * @property {IFieldDescriptorProto[]} [extension] Nested extension fields
  31. * @property {IFileOptions} [options] Options
  32. * @property {*} [sourceCodeInfo] Not supported
  33. * @property {string} [syntax="proto2"] Syntax
  34. */
  35. /**
  36. * Properties of a FileOptions message.
  37. * @interface IFileOptions
  38. * @property {string} [javaPackage]
  39. * @property {string} [javaOuterClassname]
  40. * @property {boolean} [javaMultipleFiles]
  41. * @property {boolean} [javaGenerateEqualsAndHash]
  42. * @property {boolean} [javaStringCheckUtf8]
  43. * @property {IFileOptionsOptimizeMode} [optimizeFor=1]
  44. * @property {string} [goPackage]
  45. * @property {boolean} [ccGenericServices]
  46. * @property {boolean} [javaGenericServices]
  47. * @property {boolean} [pyGenericServices]
  48. * @property {boolean} [deprecated]
  49. * @property {boolean} [ccEnableArenas]
  50. * @property {string} [objcClassPrefix]
  51. * @property {string} [csharpNamespace]
  52. */
  53. /**
  54. * Values of he FileOptions.OptimizeMode enum.
  55. * @typedef IFileOptionsOptimizeMode
  56. * @type {number}
  57. * @property {number} SPEED=1
  58. * @property {number} CODE_SIZE=2
  59. * @property {number} LITE_RUNTIME=3
  60. */
  61. /**
  62. * Creates a root from a descriptor set.
  63. * @param {IFileDescriptorSet|Reader|Uint8Array} descriptor Descriptor
  64. * @returns {Root} Root instance
  65. */
  66. Root.fromDescriptor = function fromDescriptor(descriptor) {
  67. // Decode the descriptor message if specified as a buffer:
  68. if (typeof descriptor.length === "number")
  69. descriptor = exports.FileDescriptorSet.decode(descriptor);
  70. var root = new Root();
  71. if (descriptor.file) {
  72. var fileDescriptor,
  73. filePackage;
  74. for (var j = 0, i; j < descriptor.file.length; ++j) {
  75. filePackage = root;
  76. if ((fileDescriptor = descriptor.file[j])["package"] && fileDescriptor["package"].length)
  77. filePackage = root.define(fileDescriptor["package"]);
  78. if (fileDescriptor.name && fileDescriptor.name.length)
  79. root.files.push(filePackage.filename = fileDescriptor.name);
  80. if (fileDescriptor.messageType)
  81. for (i = 0; i < fileDescriptor.messageType.length; ++i)
  82. filePackage.add(Type.fromDescriptor(fileDescriptor.messageType[i], fileDescriptor.syntax));
  83. if (fileDescriptor.enumType)
  84. for (i = 0; i < fileDescriptor.enumType.length; ++i)
  85. filePackage.add(Enum.fromDescriptor(fileDescriptor.enumType[i]));
  86. if (fileDescriptor.extension)
  87. for (i = 0; i < fileDescriptor.extension.length; ++i)
  88. filePackage.add(Field.fromDescriptor(fileDescriptor.extension[i]));
  89. if (fileDescriptor.service)
  90. for (i = 0; i < fileDescriptor.service.length; ++i)
  91. filePackage.add(Service.fromDescriptor(fileDescriptor.service[i]));
  92. var opts = fromDescriptorOptions(fileDescriptor.options, exports.FileOptions);
  93. if (opts) {
  94. var ks = Object.keys(opts);
  95. for (i = 0; i < ks.length; ++i)
  96. filePackage.setOption(ks[i], opts[ks[i]]);
  97. }
  98. }
  99. }
  100. return root;
  101. };
  102. /**
  103. * Converts a root to a descriptor set.
  104. * @returns {Message<IFileDescriptorSet>} Descriptor
  105. * @param {string} [syntax="proto2"] Syntax
  106. */
  107. Root.prototype.toDescriptor = function toDescriptor(syntax) {
  108. var set = exports.FileDescriptorSet.create();
  109. Root_toDescriptorRecursive(this, set.file, syntax);
  110. return set;
  111. };
  112. // Traverses a namespace and assembles the descriptor set
  113. function Root_toDescriptorRecursive(ns, files, syntax) {
  114. // Create a new file
  115. var file = exports.FileDescriptorProto.create({ name: ns.filename || (ns.fullName.substring(1).replace(/\./g, "_") || "root") + ".proto" });
  116. if (syntax)
  117. file.syntax = syntax;
  118. if (!(ns instanceof Root))
  119. file["package"] = ns.fullName.substring(1);
  120. // Add nested types
  121. for (var i = 0, nested; i < ns.nestedArray.length; ++i)
  122. if ((nested = ns._nestedArray[i]) instanceof Type)
  123. file.messageType.push(nested.toDescriptor(syntax));
  124. else if (nested instanceof Enum)
  125. file.enumType.push(nested.toDescriptor());
  126. else if (nested instanceof Field)
  127. file.extension.push(nested.toDescriptor(syntax));
  128. else if (nested instanceof Service)
  129. file.service.push(nested.toDescriptor());
  130. else if (nested instanceof /* plain */ Namespace)
  131. Root_toDescriptorRecursive(nested, files, syntax); // requires new file
  132. // Keep package-level options
  133. file.options = toDescriptorOptions(ns.options, exports.FileOptions);
  134. // And keep the file only if there is at least one nested object
  135. if (file.messageType.length + file.enumType.length + file.extension.length + file.service.length)
  136. files.push(file);
  137. }
  138. // --- Type ---
  139. /**
  140. * Properties of a DescriptorProto message.
  141. * @interface IDescriptorProto
  142. * @property {string} [name] Message type name
  143. * @property {IFieldDescriptorProto[]} [field] Fields
  144. * @property {IFieldDescriptorProto[]} [extension] Extension fields
  145. * @property {IDescriptorProto[]} [nestedType] Nested message types
  146. * @property {IEnumDescriptorProto[]} [enumType] Nested enums
  147. * @property {IDescriptorProtoExtensionRange[]} [extensionRange] Extension ranges
  148. * @property {IOneofDescriptorProto[]} [oneofDecl] Oneofs
  149. * @property {IMessageOptions} [options] Not supported
  150. * @property {IDescriptorProtoReservedRange[]} [reservedRange] Reserved ranges
  151. * @property {string[]} [reservedName] Reserved names
  152. */
  153. /**
  154. * Properties of a MessageOptions message.
  155. * @interface IMessageOptions
  156. * @property {boolean} [mapEntry=false] Whether this message is a map entry
  157. */
  158. /**
  159. * Properties of an ExtensionRange message.
  160. * @interface IDescriptorProtoExtensionRange
  161. * @property {number} [start] Start field id
  162. * @property {number} [end] End field id
  163. */
  164. /**
  165. * Properties of a ReservedRange message.
  166. * @interface IDescriptorProtoReservedRange
  167. * @property {number} [start] Start field id
  168. * @property {number} [end] End field id
  169. */
  170. var unnamedMessageIndex = 0;
  171. /**
  172. * Creates a type from a descriptor.
  173. * @param {IDescriptorProto|Reader|Uint8Array} descriptor Descriptor
  174. * @param {string} [syntax="proto2"] Syntax
  175. * @returns {Type} Type instance
  176. */
  177. Type.fromDescriptor = function fromDescriptor(descriptor, syntax) {
  178. // Decode the descriptor message if specified as a buffer:
  179. if (typeof descriptor.length === "number")
  180. descriptor = exports.DescriptorProto.decode(descriptor);
  181. // Create the message type
  182. var type = new Type(descriptor.name.length ? descriptor.name : "Type" + unnamedMessageIndex++, fromDescriptorOptions(descriptor.options, exports.MessageOptions)),
  183. i;
  184. /* Oneofs */ if (descriptor.oneofDecl)
  185. for (i = 0; i < descriptor.oneofDecl.length; ++i)
  186. type.add(OneOf.fromDescriptor(descriptor.oneofDecl[i]));
  187. /* Fields */ if (descriptor.field)
  188. for (i = 0; i < descriptor.field.length; ++i) {
  189. var field = Field.fromDescriptor(descriptor.field[i], syntax);
  190. type.add(field);
  191. if (descriptor.field[i].hasOwnProperty("oneofIndex")) // eslint-disable-line no-prototype-builtins
  192. type.oneofsArray[descriptor.field[i].oneofIndex].add(field);
  193. }
  194. /* Extension fields */ if (descriptor.extension)
  195. for (i = 0; i < descriptor.extension.length; ++i)
  196. type.add(Field.fromDescriptor(descriptor.extension[i], syntax));
  197. /* Nested types */ if (descriptor.nestedType)
  198. for (i = 0; i < descriptor.nestedType.length; ++i) {
  199. type.add(Type.fromDescriptor(descriptor.nestedType[i], syntax));
  200. if (descriptor.nestedType[i].options && descriptor.nestedType[i].options.mapEntry)
  201. type.setOption("map_entry", true);
  202. }
  203. /* Nested enums */ if (descriptor.enumType)
  204. for (i = 0; i < descriptor.enumType.length; ++i)
  205. type.add(Enum.fromDescriptor(descriptor.enumType[i]));
  206. /* Extension ranges */ if (descriptor.extensionRange && descriptor.extensionRange.length) {
  207. type.extensions = [];
  208. for (i = 0; i < descriptor.extensionRange.length; ++i)
  209. type.extensions.push([ descriptor.extensionRange[i].start, descriptor.extensionRange[i].end ]);
  210. }
  211. /* Reserved... */ if (descriptor.reservedRange && descriptor.reservedRange.length || descriptor.reservedName && descriptor.reservedName.length) {
  212. type.reserved = [];
  213. /* Ranges */ if (descriptor.reservedRange)
  214. for (i = 0; i < descriptor.reservedRange.length; ++i)
  215. type.reserved.push([ descriptor.reservedRange[i].start, descriptor.reservedRange[i].end ]);
  216. /* Names */ if (descriptor.reservedName)
  217. for (i = 0; i < descriptor.reservedName.length; ++i)
  218. type.reserved.push(descriptor.reservedName[i]);
  219. }
  220. return type;
  221. };
  222. /**
  223. * Converts a type to a descriptor.
  224. * @returns {Message<IDescriptorProto>} Descriptor
  225. * @param {string} [syntax="proto2"] Syntax
  226. */
  227. Type.prototype.toDescriptor = function toDescriptor(syntax) {
  228. var descriptor = exports.DescriptorProto.create({ name: this.name }),
  229. i;
  230. /* Fields */ for (i = 0; i < this.fieldsArray.length; ++i) {
  231. var fieldDescriptor;
  232. descriptor.field.push(fieldDescriptor = this._fieldsArray[i].toDescriptor(syntax));
  233. if (this._fieldsArray[i] instanceof MapField) { // map fields are repeated FieldNameEntry
  234. var keyType = toDescriptorType(this._fieldsArray[i].keyType, this._fieldsArray[i].resolvedKeyType),
  235. valueType = toDescriptorType(this._fieldsArray[i].type, this._fieldsArray[i].resolvedType),
  236. valueTypeName = valueType === /* type */ 11 || valueType === /* enum */ 14
  237. ? this._fieldsArray[i].resolvedType && shortname(this.parent, this._fieldsArray[i].resolvedType) || this._fieldsArray[i].type
  238. : undefined;
  239. descriptor.nestedType.push(exports.DescriptorProto.create({
  240. name: fieldDescriptor.typeName,
  241. field: [
  242. exports.FieldDescriptorProto.create({ name: "key", number: 1, label: 1, type: keyType }), // can't reference a type or enum
  243. exports.FieldDescriptorProto.create({ name: "value", number: 2, label: 1, type: valueType, typeName: valueTypeName })
  244. ],
  245. options: exports.MessageOptions.create({ mapEntry: true })
  246. }));
  247. }
  248. }
  249. /* Oneofs */ for (i = 0; i < this.oneofsArray.length; ++i)
  250. descriptor.oneofDecl.push(this._oneofsArray[i].toDescriptor());
  251. /* Nested... */ for (i = 0; i < this.nestedArray.length; ++i) {
  252. /* Extension fields */ if (this._nestedArray[i] instanceof Field)
  253. descriptor.field.push(this._nestedArray[i].toDescriptor(syntax));
  254. /* Types */ else if (this._nestedArray[i] instanceof Type)
  255. descriptor.nestedType.push(this._nestedArray[i].toDescriptor(syntax));
  256. /* Enums */ else if (this._nestedArray[i] instanceof Enum)
  257. descriptor.enumType.push(this._nestedArray[i].toDescriptor());
  258. // plain nested namespaces become packages instead in Root#toDescriptor
  259. }
  260. /* Extension ranges */ if (this.extensions)
  261. for (i = 0; i < this.extensions.length; ++i)
  262. descriptor.extensionRange.push(exports.DescriptorProto.ExtensionRange.create({ start: this.extensions[i][0], end: this.extensions[i][1] }));
  263. /* Reserved... */ if (this.reserved)
  264. for (i = 0; i < this.reserved.length; ++i)
  265. /* Names */ if (typeof this.reserved[i] === "string")
  266. descriptor.reservedName.push(this.reserved[i]);
  267. /* Ranges */ else
  268. descriptor.reservedRange.push(exports.DescriptorProto.ReservedRange.create({ start: this.reserved[i][0], end: this.reserved[i][1] }));
  269. descriptor.options = toDescriptorOptions(this.options, exports.MessageOptions);
  270. return descriptor;
  271. };
  272. // --- Field ---
  273. /**
  274. * Properties of a FieldDescriptorProto message.
  275. * @interface IFieldDescriptorProto
  276. * @property {string} [name] Field name
  277. * @property {number} [number] Field id
  278. * @property {IFieldDescriptorProtoLabel} [label] Field rule
  279. * @property {IFieldDescriptorProtoType} [type] Field basic type
  280. * @property {string} [typeName] Field type name
  281. * @property {string} [extendee] Extended type name
  282. * @property {string} [defaultValue] Literal default value
  283. * @property {number} [oneofIndex] Oneof index if part of a oneof
  284. * @property {*} [jsonName] Not supported
  285. * @property {IFieldOptions} [options] Field options
  286. */
  287. /**
  288. * Values of the FieldDescriptorProto.Label enum.
  289. * @typedef IFieldDescriptorProtoLabel
  290. * @type {number}
  291. * @property {number} LABEL_OPTIONAL=1
  292. * @property {number} LABEL_REQUIRED=2
  293. * @property {number} LABEL_REPEATED=3
  294. */
  295. /**
  296. * Values of the FieldDescriptorProto.Type enum.
  297. * @typedef IFieldDescriptorProtoType
  298. * @type {number}
  299. * @property {number} TYPE_DOUBLE=1
  300. * @property {number} TYPE_FLOAT=2
  301. * @property {number} TYPE_INT64=3
  302. * @property {number} TYPE_UINT64=4
  303. * @property {number} TYPE_INT32=5
  304. * @property {number} TYPE_FIXED64=6
  305. * @property {number} TYPE_FIXED32=7
  306. * @property {number} TYPE_BOOL=8
  307. * @property {number} TYPE_STRING=9
  308. * @property {number} TYPE_GROUP=10
  309. * @property {number} TYPE_MESSAGE=11
  310. * @property {number} TYPE_BYTES=12
  311. * @property {number} TYPE_UINT32=13
  312. * @property {number} TYPE_ENUM=14
  313. * @property {number} TYPE_SFIXED32=15
  314. * @property {number} TYPE_SFIXED64=16
  315. * @property {number} TYPE_SINT32=17
  316. * @property {number} TYPE_SINT64=18
  317. */
  318. /**
  319. * Properties of a FieldOptions message.
  320. * @interface IFieldOptions
  321. * @property {boolean} [packed] Whether packed or not (defaults to `false` for proto2 and `true` for proto3)
  322. * @property {IFieldOptionsJSType} [jstype] JavaScript value type (not used by protobuf.js)
  323. */
  324. /**
  325. * Values of the FieldOptions.JSType enum.
  326. * @typedef IFieldOptionsJSType
  327. * @type {number}
  328. * @property {number} JS_NORMAL=0
  329. * @property {number} JS_STRING=1
  330. * @property {number} JS_NUMBER=2
  331. */
  332. // copied here from parse.js
  333. var numberRe = /^(?![eE])[0-9]*(?:\.[0-9]*)?(?:[eE][+-]?[0-9]+)?$/;
  334. /**
  335. * Creates a field from a descriptor.
  336. * @param {IFieldDescriptorProto|Reader|Uint8Array} descriptor Descriptor
  337. * @param {string} [syntax="proto2"] Syntax
  338. * @returns {Field} Field instance
  339. */
  340. Field.fromDescriptor = function fromDescriptor(descriptor, syntax) {
  341. // Decode the descriptor message if specified as a buffer:
  342. if (typeof descriptor.length === "number")
  343. descriptor = exports.DescriptorProto.decode(descriptor);
  344. if (typeof descriptor.number !== "number")
  345. throw Error("missing field id");
  346. // Rewire field type
  347. var fieldType;
  348. if (descriptor.typeName && descriptor.typeName.length)
  349. fieldType = descriptor.typeName;
  350. else
  351. fieldType = fromDescriptorType(descriptor.type);
  352. // Rewire field rule
  353. var fieldRule;
  354. switch (descriptor.label) {
  355. // 0 is reserved for errors
  356. case 1: fieldRule = undefined; break;
  357. case 2: fieldRule = "required"; break;
  358. case 3: fieldRule = "repeated"; break;
  359. default: throw Error("illegal label: " + descriptor.label);
  360. }
  361. var extendee = descriptor.extendee;
  362. if (descriptor.extendee !== undefined) {
  363. extendee = extendee.length ? extendee : undefined;
  364. }
  365. var field = new Field(
  366. descriptor.name.length ? descriptor.name : "field" + descriptor.number,
  367. descriptor.number,
  368. fieldType,
  369. fieldRule,
  370. extendee
  371. );
  372. field.options = fromDescriptorOptions(descriptor.options, exports.FieldOptions);
  373. if (descriptor.defaultValue && descriptor.defaultValue.length) {
  374. var defaultValue = descriptor.defaultValue;
  375. switch (defaultValue) {
  376. case "true": case "TRUE":
  377. defaultValue = true;
  378. break;
  379. case "false": case "FALSE":
  380. defaultValue = false;
  381. break;
  382. default:
  383. var match = numberRe.exec(defaultValue);
  384. if (match)
  385. defaultValue = parseInt(defaultValue); // eslint-disable-line radix
  386. break;
  387. }
  388. field.setOption("default", defaultValue);
  389. }
  390. if (packableDescriptorType(descriptor.type)) {
  391. if (syntax === "proto3") { // defaults to packed=true (internal preset is packed=true)
  392. if (descriptor.options && !descriptor.options.packed)
  393. field.setOption("packed", false);
  394. } else if (!(descriptor.options && descriptor.options.packed)) // defaults to packed=false
  395. field.setOption("packed", false);
  396. }
  397. return field;
  398. };
  399. /**
  400. * Converts a field to a descriptor.
  401. * @returns {Message<IFieldDescriptorProto>} Descriptor
  402. * @param {string} [syntax="proto2"] Syntax
  403. */
  404. Field.prototype.toDescriptor = function toDescriptor(syntax) {
  405. var descriptor = exports.FieldDescriptorProto.create({ name: this.name, number: this.id });
  406. if (this.map) {
  407. descriptor.type = 11; // message
  408. descriptor.typeName = $protobuf.util.ucFirst(this.name); // fieldName -> FieldNameEntry (built in Type#toDescriptor)
  409. descriptor.label = 3; // repeated
  410. } else {
  411. // Rewire field type
  412. switch (descriptor.type = toDescriptorType(this.type, this.resolve().resolvedType)) {
  413. case 10: // group
  414. case 11: // type
  415. case 14: // enum
  416. descriptor.typeName = this.resolvedType ? shortname(this.parent, this.resolvedType) : this.type;
  417. break;
  418. }
  419. // Rewire field rule
  420. switch (this.rule) {
  421. case "repeated": descriptor.label = 3; break;
  422. case "required": descriptor.label = 2; break;
  423. default: descriptor.label = 1; break;
  424. }
  425. }
  426. // Handle extension field
  427. descriptor.extendee = this.extensionField ? this.extensionField.parent.fullName : this.extend;
  428. // Handle part of oneof
  429. if (this.partOf)
  430. if ((descriptor.oneofIndex = this.parent.oneofsArray.indexOf(this.partOf)) < 0)
  431. throw Error("missing oneof");
  432. if (this.options) {
  433. descriptor.options = toDescriptorOptions(this.options, exports.FieldOptions);
  434. if (this.options["default"] != null)
  435. descriptor.defaultValue = String(this.options["default"]);
  436. }
  437. if (syntax === "proto3") { // defaults to packed=true
  438. if (!this.packed)
  439. (descriptor.options || (descriptor.options = exports.FieldOptions.create())).packed = false;
  440. } else if (this.packed) // defaults to packed=false
  441. (descriptor.options || (descriptor.options = exports.FieldOptions.create())).packed = true;
  442. return descriptor;
  443. };
  444. // --- Enum ---
  445. /**
  446. * Properties of an EnumDescriptorProto message.
  447. * @interface IEnumDescriptorProto
  448. * @property {string} [name] Enum name
  449. * @property {IEnumValueDescriptorProto[]} [value] Enum values
  450. * @property {IEnumOptions} [options] Enum options
  451. */
  452. /**
  453. * Properties of an EnumValueDescriptorProto message.
  454. * @interface IEnumValueDescriptorProto
  455. * @property {string} [name] Name
  456. * @property {number} [number] Value
  457. * @property {*} [options] Not supported
  458. */
  459. /**
  460. * Properties of an EnumOptions message.
  461. * @interface IEnumOptions
  462. * @property {boolean} [allowAlias] Whether aliases are allowed
  463. * @property {boolean} [deprecated]
  464. */
  465. var unnamedEnumIndex = 0;
  466. /**
  467. * Creates an enum from a descriptor.
  468. * @param {IEnumDescriptorProto|Reader|Uint8Array} descriptor Descriptor
  469. * @returns {Enum} Enum instance
  470. */
  471. Enum.fromDescriptor = function fromDescriptor(descriptor) {
  472. // Decode the descriptor message if specified as a buffer:
  473. if (typeof descriptor.length === "number")
  474. descriptor = exports.EnumDescriptorProto.decode(descriptor);
  475. // Construct values object
  476. var values = {};
  477. if (descriptor.value)
  478. for (var i = 0; i < descriptor.value.length; ++i) {
  479. var name = descriptor.value[i].name,
  480. value = descriptor.value[i].number || 0;
  481. values[name && name.length ? name : "NAME" + value] = value;
  482. }
  483. return new Enum(
  484. descriptor.name && descriptor.name.length ? descriptor.name : "Enum" + unnamedEnumIndex++,
  485. values,
  486. fromDescriptorOptions(descriptor.options, exports.EnumOptions)
  487. );
  488. };
  489. /**
  490. * Converts an enum to a descriptor.
  491. * @returns {Message<IEnumDescriptorProto>} Descriptor
  492. */
  493. Enum.prototype.toDescriptor = function toDescriptor() {
  494. // Values
  495. var values = [];
  496. for (var i = 0, ks = Object.keys(this.values); i < ks.length; ++i)
  497. values.push(exports.EnumValueDescriptorProto.create({ name: ks[i], number: this.values[ks[i]] }));
  498. return exports.EnumDescriptorProto.create({
  499. name: this.name,
  500. value: values,
  501. options: toDescriptorOptions(this.options, exports.EnumOptions)
  502. });
  503. };
  504. // --- OneOf ---
  505. /**
  506. * Properties of a OneofDescriptorProto message.
  507. * @interface IOneofDescriptorProto
  508. * @property {string} [name] Oneof name
  509. * @property {*} [options] Not supported
  510. */
  511. var unnamedOneofIndex = 0;
  512. /**
  513. * Creates a oneof from a descriptor.
  514. * @param {IOneofDescriptorProto|Reader|Uint8Array} descriptor Descriptor
  515. * @returns {OneOf} OneOf instance
  516. */
  517. OneOf.fromDescriptor = function fromDescriptor(descriptor) {
  518. // Decode the descriptor message if specified as a buffer:
  519. if (typeof descriptor.length === "number")
  520. descriptor = exports.OneofDescriptorProto.decode(descriptor);
  521. return new OneOf(
  522. // unnamedOneOfIndex is global, not per type, because we have no ref to a type here
  523. descriptor.name && descriptor.name.length ? descriptor.name : "oneof" + unnamedOneofIndex++
  524. // fromDescriptorOptions(descriptor.options, exports.OneofOptions) - only uninterpreted_option
  525. );
  526. };
  527. /**
  528. * Converts a oneof to a descriptor.
  529. * @returns {Message<IOneofDescriptorProto>} Descriptor
  530. */
  531. OneOf.prototype.toDescriptor = function toDescriptor() {
  532. return exports.OneofDescriptorProto.create({
  533. name: this.name
  534. // options: toDescriptorOptions(this.options, exports.OneofOptions) - only uninterpreted_option
  535. });
  536. };
  537. // --- Service ---
  538. /**
  539. * Properties of a ServiceDescriptorProto message.
  540. * @interface IServiceDescriptorProto
  541. * @property {string} [name] Service name
  542. * @property {IMethodDescriptorProto[]} [method] Methods
  543. * @property {IServiceOptions} [options] Options
  544. */
  545. /**
  546. * Properties of a ServiceOptions message.
  547. * @interface IServiceOptions
  548. * @property {boolean} [deprecated]
  549. */
  550. var unnamedServiceIndex = 0;
  551. /**
  552. * Creates a service from a descriptor.
  553. * @param {IServiceDescriptorProto|Reader|Uint8Array} descriptor Descriptor
  554. * @returns {Service} Service instance
  555. */
  556. Service.fromDescriptor = function fromDescriptor(descriptor) {
  557. // Decode the descriptor message if specified as a buffer:
  558. if (typeof descriptor.length === "number")
  559. descriptor = exports.ServiceDescriptorProto.decode(descriptor);
  560. var service = new Service(descriptor.name && descriptor.name.length ? descriptor.name : "Service" + unnamedServiceIndex++, fromDescriptorOptions(descriptor.options, exports.ServiceOptions));
  561. if (descriptor.method)
  562. for (var i = 0; i < descriptor.method.length; ++i)
  563. service.add(Method.fromDescriptor(descriptor.method[i]));
  564. return service;
  565. };
  566. /**
  567. * Converts a service to a descriptor.
  568. * @returns {Message<IServiceDescriptorProto>} Descriptor
  569. */
  570. Service.prototype.toDescriptor = function toDescriptor() {
  571. // Methods
  572. var methods = [];
  573. for (var i = 0; i < this.methodsArray.length; ++i)
  574. methods.push(this._methodsArray[i].toDescriptor());
  575. return exports.ServiceDescriptorProto.create({
  576. name: this.name,
  577. method: methods,
  578. options: toDescriptorOptions(this.options, exports.ServiceOptions)
  579. });
  580. };
  581. // --- Method ---
  582. /**
  583. * Properties of a MethodDescriptorProto message.
  584. * @interface IMethodDescriptorProto
  585. * @property {string} [name] Method name
  586. * @property {string} [inputType] Request type name
  587. * @property {string} [outputType] Response type name
  588. * @property {IMethodOptions} [options] Not supported
  589. * @property {boolean} [clientStreaming=false] Whether requests are streamed
  590. * @property {boolean} [serverStreaming=false] Whether responses are streamed
  591. */
  592. /**
  593. * Properties of a MethodOptions message.
  594. * @interface IMethodOptions
  595. * @property {boolean} [deprecated]
  596. */
  597. var unnamedMethodIndex = 0;
  598. /**
  599. * Creates a method from a descriptor.
  600. * @param {IMethodDescriptorProto|Reader|Uint8Array} descriptor Descriptor
  601. * @returns {Method} Reflected method instance
  602. */
  603. Method.fromDescriptor = function fromDescriptor(descriptor) {
  604. // Decode the descriptor message if specified as a buffer:
  605. if (typeof descriptor.length === "number")
  606. descriptor = exports.MethodDescriptorProto.decode(descriptor);
  607. return new Method(
  608. // unnamedMethodIndex is global, not per service, because we have no ref to a service here
  609. descriptor.name && descriptor.name.length ? descriptor.name : "Method" + unnamedMethodIndex++,
  610. "rpc",
  611. descriptor.inputType,
  612. descriptor.outputType,
  613. Boolean(descriptor.clientStreaming),
  614. Boolean(descriptor.serverStreaming),
  615. fromDescriptorOptions(descriptor.options, exports.MethodOptions)
  616. );
  617. };
  618. /**
  619. * Converts a method to a descriptor.
  620. * @returns {Message<IMethodDescriptorProto>} Descriptor
  621. */
  622. Method.prototype.toDescriptor = function toDescriptor() {
  623. return exports.MethodDescriptorProto.create({
  624. name: this.name,
  625. inputType: this.resolvedRequestType ? this.resolvedRequestType.fullName : this.requestType,
  626. outputType: this.resolvedResponseType ? this.resolvedResponseType.fullName : this.responseType,
  627. clientStreaming: this.requestStream,
  628. serverStreaming: this.responseStream,
  629. options: toDescriptorOptions(this.options, exports.MethodOptions)
  630. });
  631. };
  632. // --- utility ---
  633. // Converts a descriptor type to a protobuf.js basic type
  634. function fromDescriptorType(type) {
  635. switch (type) {
  636. // 0 is reserved for errors
  637. case 1: return "double";
  638. case 2: return "float";
  639. case 3: return "int64";
  640. case 4: return "uint64";
  641. case 5: return "int32";
  642. case 6: return "fixed64";
  643. case 7: return "fixed32";
  644. case 8: return "bool";
  645. case 9: return "string";
  646. case 12: return "bytes";
  647. case 13: return "uint32";
  648. case 15: return "sfixed32";
  649. case 16: return "sfixed64";
  650. case 17: return "sint32";
  651. case 18: return "sint64";
  652. }
  653. throw Error("illegal type: " + type);
  654. }
  655. // Tests if a descriptor type is packable
  656. function packableDescriptorType(type) {
  657. switch (type) {
  658. case 1: // double
  659. case 2: // float
  660. case 3: // int64
  661. case 4: // uint64
  662. case 5: // int32
  663. case 6: // fixed64
  664. case 7: // fixed32
  665. case 8: // bool
  666. case 13: // uint32
  667. case 14: // enum (!)
  668. case 15: // sfixed32
  669. case 16: // sfixed64
  670. case 17: // sint32
  671. case 18: // sint64
  672. return true;
  673. }
  674. return false;
  675. }
  676. // Converts a protobuf.js basic type to a descriptor type
  677. function toDescriptorType(type, resolvedType) {
  678. switch (type) {
  679. // 0 is reserved for errors
  680. case "double": return 1;
  681. case "float": return 2;
  682. case "int64": return 3;
  683. case "uint64": return 4;
  684. case "int32": return 5;
  685. case "fixed64": return 6;
  686. case "fixed32": return 7;
  687. case "bool": return 8;
  688. case "string": return 9;
  689. case "bytes": return 12;
  690. case "uint32": return 13;
  691. case "sfixed32": return 15;
  692. case "sfixed64": return 16;
  693. case "sint32": return 17;
  694. case "sint64": return 18;
  695. }
  696. if (resolvedType instanceof Enum)
  697. return 14;
  698. if (resolvedType instanceof Type)
  699. return resolvedType.group ? 10 : 11;
  700. throw Error("illegal type: " + type);
  701. }
  702. // Converts descriptor options to an options object
  703. function fromDescriptorOptions(options, type) {
  704. if (!options)
  705. return undefined;
  706. var out = [];
  707. for (var i = 0, field, key, val; i < type.fieldsArray.length; ++i)
  708. if ((key = (field = type._fieldsArray[i]).name) !== "uninterpretedOption")
  709. if (options.hasOwnProperty(key)) { // eslint-disable-line no-prototype-builtins
  710. val = options[key];
  711. if (field.resolvedType instanceof Enum && typeof val === "number" && field.resolvedType.valuesById[val] !== undefined)
  712. val = field.resolvedType.valuesById[val];
  713. out.push(underScore(key), val);
  714. }
  715. return out.length ? $protobuf.util.toObject(out) : undefined;
  716. }
  717. // Converts an options object to descriptor options
  718. function toDescriptorOptions(options, type) {
  719. if (!options)
  720. return undefined;
  721. var out = [];
  722. for (var i = 0, ks = Object.keys(options), key, val; i < ks.length; ++i) {
  723. val = options[key = ks[i]];
  724. if (key === "default")
  725. continue;
  726. var field = type.fields[key];
  727. if (!field && !(field = type.fields[key = $protobuf.util.camelCase(key)]))
  728. continue;
  729. out.push(key, val);
  730. }
  731. return out.length ? type.fromObject($protobuf.util.toObject(out)) : undefined;
  732. }
  733. // Calculates the shortest relative path from `from` to `to`.
  734. function shortname(from, to) {
  735. var fromPath = from.fullName.split("."),
  736. toPath = to.fullName.split("."),
  737. i = 0,
  738. j = 0,
  739. k = toPath.length - 1;
  740. if (!(from instanceof Root) && to instanceof Namespace)
  741. while (i < fromPath.length && j < k && fromPath[i] === toPath[j]) {
  742. var other = to.lookup(fromPath[i++], true);
  743. if (other !== null && other !== to)
  744. break;
  745. ++j;
  746. }
  747. else
  748. for (; i < fromPath.length && j < k && fromPath[i] === toPath[j]; ++i, ++j);
  749. return toPath.slice(j).join(".");
  750. }
  751. // copied here from cli/targets/proto.js
  752. function underScore(str) {
  753. return str.substring(0,1)
  754. + str.substring(1)
  755. .replace(/([A-Z])(?=[a-z]|$)/g, function($0, $1) { return "_" + $1.toLowerCase(); });
  756. }
  757. // --- exports ---
  758. /**
  759. * Reflected file descriptor set.
  760. * @name FileDescriptorSet
  761. * @type {Type}
  762. * @const
  763. * @tstype $protobuf.Type
  764. */
  765. /**
  766. * Reflected file descriptor proto.
  767. * @name FileDescriptorProto
  768. * @type {Type}
  769. * @const
  770. * @tstype $protobuf.Type
  771. */
  772. /**
  773. * Reflected descriptor proto.
  774. * @name DescriptorProto
  775. * @type {Type}
  776. * @property {Type} ExtensionRange
  777. * @property {Type} ReservedRange
  778. * @const
  779. * @tstype $protobuf.Type & {
  780. * ExtensionRange: $protobuf.Type,
  781. * ReservedRange: $protobuf.Type
  782. * }
  783. */
  784. /**
  785. * Reflected field descriptor proto.
  786. * @name FieldDescriptorProto
  787. * @type {Type}
  788. * @property {Enum} Label
  789. * @property {Enum} Type
  790. * @const
  791. * @tstype $protobuf.Type & {
  792. * Label: $protobuf.Enum,
  793. * Type: $protobuf.Enum
  794. * }
  795. */
  796. /**
  797. * Reflected oneof descriptor proto.
  798. * @name OneofDescriptorProto
  799. * @type {Type}
  800. * @const
  801. * @tstype $protobuf.Type
  802. */
  803. /**
  804. * Reflected enum descriptor proto.
  805. * @name EnumDescriptorProto
  806. * @type {Type}
  807. * @const
  808. * @tstype $protobuf.Type
  809. */
  810. /**
  811. * Reflected service descriptor proto.
  812. * @name ServiceDescriptorProto
  813. * @type {Type}
  814. * @const
  815. * @tstype $protobuf.Type
  816. */
  817. /**
  818. * Reflected enum value descriptor proto.
  819. * @name EnumValueDescriptorProto
  820. * @type {Type}
  821. * @const
  822. * @tstype $protobuf.Type
  823. */
  824. /**
  825. * Reflected method descriptor proto.
  826. * @name MethodDescriptorProto
  827. * @type {Type}
  828. * @const
  829. * @tstype $protobuf.Type
  830. */
  831. /**
  832. * Reflected file options.
  833. * @name FileOptions
  834. * @type {Type}
  835. * @property {Enum} OptimizeMode
  836. * @const
  837. * @tstype $protobuf.Type & {
  838. * OptimizeMode: $protobuf.Enum
  839. * }
  840. */
  841. /**
  842. * Reflected message options.
  843. * @name MessageOptions
  844. * @type {Type}
  845. * @const
  846. * @tstype $protobuf.Type
  847. */
  848. /**
  849. * Reflected field options.
  850. * @name FieldOptions
  851. * @type {Type}
  852. * @property {Enum} CType
  853. * @property {Enum} JSType
  854. * @const
  855. * @tstype $protobuf.Type & {
  856. * CType: $protobuf.Enum,
  857. * JSType: $protobuf.Enum
  858. * }
  859. */
  860. /**
  861. * Reflected oneof options.
  862. * @name OneofOptions
  863. * @type {Type}
  864. * @const
  865. * @tstype $protobuf.Type
  866. */
  867. /**
  868. * Reflected enum options.
  869. * @name EnumOptions
  870. * @type {Type}
  871. * @const
  872. * @tstype $protobuf.Type
  873. */
  874. /**
  875. * Reflected enum value options.
  876. * @name EnumValueOptions
  877. * @type {Type}
  878. * @const
  879. * @tstype $protobuf.Type
  880. */
  881. /**
  882. * Reflected service options.
  883. * @name ServiceOptions
  884. * @type {Type}
  885. * @const
  886. * @tstype $protobuf.Type
  887. */
  888. /**
  889. * Reflected method options.
  890. * @name MethodOptions
  891. * @type {Type}
  892. * @const
  893. * @tstype $protobuf.Type
  894. */
  895. /**
  896. * Reflected uninterpretet option.
  897. * @name UninterpretedOption
  898. * @type {Type}
  899. * @property {Type} NamePart
  900. * @const
  901. * @tstype $protobuf.Type & {
  902. * NamePart: $protobuf.Type
  903. * }
  904. */
  905. /**
  906. * Reflected source code info.
  907. * @name SourceCodeInfo
  908. * @type {Type}
  909. * @property {Type} Location
  910. * @const
  911. * @tstype $protobuf.Type & {
  912. * Location: $protobuf.Type
  913. * }
  914. */
  915. /**
  916. * Reflected generated code info.
  917. * @name GeneratedCodeInfo
  918. * @type {Type}
  919. * @property {Type} Annotation
  920. * @const
  921. * @tstype $protobuf.Type & {
  922. * Annotation: $protobuf.Type
  923. * }
  924. */