智慧水务管理系统 - 精河县供水工程综合管理平台

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. "use strict";
  2. var $protobuf = require("../light");
  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. patterns = $protobuf.util.patterns;
  14. var numberRe = patterns.numberRe,
  15. typeRefRe = patterns.typeRefRe;
  16. // --- Root ---
  17. /**
  18. * Properties of a FileDescriptorSet message.
  19. * @interface IFileDescriptorSet
  20. * @property {IFileDescriptorProto[]} file Files
  21. */
  22. /**
  23. * Properties of a FileDescriptorProto message.
  24. * @interface IFileDescriptorProto
  25. * @property {string} [name] File name
  26. * @property {string} [package] Package
  27. * @property {*} [dependency] Not supported
  28. * @property {*} [publicDependency] Not supported
  29. * @property {*} [weakDependency] Not supported
  30. * @property {IDescriptorProto[]} [messageType] Nested message types
  31. * @property {IEnumDescriptorProto[]} [enumType] Nested enums
  32. * @property {IServiceDescriptorProto[]} [service] Nested services
  33. * @property {IFieldDescriptorProto[]} [extension] Nested extension fields
  34. * @property {IFileOptions} [options] Options
  35. * @property {*} [sourceCodeInfo] Not supported
  36. * @property {string} [syntax="proto2"] Syntax
  37. * @property {IEdition} [edition] Edition
  38. */
  39. /**
  40. * Values of the Edition enum.
  41. * @typedef IEdition
  42. * @type {number}
  43. * @property {number} EDITION_UNKNOWN=0
  44. * @property {number} EDITION_LEGACY=900
  45. * @property {number} EDITION_PROTO2=998
  46. * @property {number} EDITION_PROTO3=999
  47. * @property {number} EDITION_2023=1000
  48. * @property {number} EDITION_2024=1001
  49. * @property {number} EDITION_1_TEST_ONLY=1
  50. * @property {number} EDITION_2_TEST_ONLY=2
  51. * @property {number} EDITION_99997_TEST_ONLY=99997
  52. * @property {number} EDITION_99998_TEST_ONLY=99998
  53. * @property {number} EDITION_99998_TEST_ONLY=99999
  54. * @property {number} EDITION_MAX=2147483647
  55. */
  56. /**
  57. * Properties of a FileOptions message.
  58. * @interface IFileOptions
  59. * @property {string} [javaPackage]
  60. * @property {string} [javaOuterClassname]
  61. * @property {boolean} [javaMultipleFiles]
  62. * @property {boolean} [javaGenerateEqualsAndHash]
  63. * @property {boolean} [javaStringCheckUtf8]
  64. * @property {IFileOptionsOptimizeMode} [optimizeFor=1]
  65. * @property {string} [goPackage]
  66. * @property {boolean} [ccGenericServices]
  67. * @property {boolean} [javaGenericServices]
  68. * @property {boolean} [pyGenericServices]
  69. * @property {boolean} [deprecated]
  70. * @property {boolean} [ccEnableArenas]
  71. * @property {string} [objcClassPrefix]
  72. * @property {string} [csharpNamespace]
  73. */
  74. /**
  75. * Values of he FileOptions.OptimizeMode enum.
  76. * @typedef IFileOptionsOptimizeMode
  77. * @type {number}
  78. * @property {number} SPEED=1
  79. * @property {number} CODE_SIZE=2
  80. * @property {number} LITE_RUNTIME=3
  81. */
  82. /**
  83. * Descriptor context.
  84. * @interface IDescriptorContext
  85. * @property {string} [edition="proto2"] Syntax or edition to use for direct object descriptor imports
  86. * @property {IFeatureSet} [features] File-level features to apply for direct object descriptor imports
  87. * @property {boolean} [keepCase=false] Uses proto field names as reflected field names
  88. */
  89. /**
  90. * Creates a root from a descriptor set.
  91. * @param {IFileDescriptorSet|Reader|Uint8Array} descriptor Descriptor
  92. * @param {{keepCase?: boolean}} [options] Conversion options
  93. * @returns {Root} Root instance
  94. */
  95. Root.fromDescriptor = function fromDescriptor(descriptor, options) {
  96. descriptor = decodeDescriptor(descriptor, exports.FileDescriptorSet);
  97. options = options || {};
  98. var root = new Root();
  99. if (descriptor.file) {
  100. var fileDescriptor,
  101. filePackage;
  102. for (var j = 0, i; j < descriptor.file.length; ++j) {
  103. filePackage = root;
  104. if ((fileDescriptor = descriptor.file[j])["package"] && fileDescriptor["package"].length)
  105. filePackage = root.define(fileDescriptor["package"]);
  106. var fileOptions = fromDescriptorOptions(fileDescriptor.options, exports.FileOptions),
  107. ctx = descriptorContext({
  108. edition: editionFromDescriptor(fileDescriptor),
  109. features: fileOptions && fileOptions.features,
  110. keepCase: options && options.keepCase
  111. });
  112. if (fileDescriptor.name && fileDescriptor.name.length)
  113. root.files.push(filePackage.filename = fileDescriptor.name);
  114. var groupTypes = groupTypeNames(fileDescriptor.extension);
  115. if (fileDescriptor.messageType)
  116. for (i = 0; i < fileDescriptor.messageType.length; ++i) {
  117. var type = Type_fromDescriptor(fileDescriptor.messageType[i], ctx, false, 0);
  118. if (groupTypes[type.name])
  119. type.group = true;
  120. filePackage.add(type);
  121. }
  122. if (fileDescriptor.enumType)
  123. for (i = 0; i < fileDescriptor.enumType.length; ++i)
  124. filePackage.add(Enum_fromDescriptor(fileDescriptor.enumType[i], ctx, false));
  125. if (fileDescriptor.extension)
  126. for (i = 0; i < fileDescriptor.extension.length; ++i)
  127. filePackage.add(Field_fromDescriptor(fileDescriptor.extension[i], ctx, false));
  128. if (fileDescriptor.service)
  129. for (i = 0; i < fileDescriptor.service.length; ++i)
  130. filePackage.add(Service_fromDescriptor(fileDescriptor.service[i], ctx, false));
  131. if (fileOptions) {
  132. var ks = Object.keys(fileOptions);
  133. for (i = 0; i < ks.length; ++i)
  134. filePackage.setOption(ks[i], fileOptions[ks[i]]);
  135. }
  136. }
  137. }
  138. return root.resolveAll();
  139. };
  140. /**
  141. * Converts a root to a descriptor set.
  142. * @returns {Message<IFileDescriptorSet>} Descriptor
  143. * @param {string} [edition="proto2"] The syntax or edition to use
  144. */
  145. Root.prototype.toDescriptor = function toDescriptor(edition) {
  146. var set = exports.FileDescriptorSet.create();
  147. Root_toDescriptorRecursive(this, set.file, edition);
  148. return set;
  149. };
  150. // Traverses a namespace and assembles the descriptor set
  151. function Root_toDescriptorRecursive(ns, files, edition) {
  152. // Create a new file
  153. var file = exports.FileDescriptorProto.create({ name: ns.filename || (ns.fullName.substring(1).replace(/\./g, "_") || "root") + ".proto" });
  154. editionToDescriptor(edition, file);
  155. if (!(ns instanceof Root))
  156. file["package"] = ns.fullName.substring(1);
  157. // Add nested types
  158. for (var i = 0, nested; i < ns.nestedArray.length; ++i)
  159. if ((nested = ns._nestedArray[i]) instanceof Type)
  160. file.messageType.push(nested.toDescriptor(edition));
  161. else if (nested instanceof Enum)
  162. file.enumType.push(nested.toDescriptor());
  163. else if (nested instanceof Field)
  164. file.extension.push(nested.toDescriptor(edition));
  165. else if (nested instanceof Service)
  166. file.service.push(nested.toDescriptor());
  167. else if (nested instanceof /* plain */ Namespace)
  168. Root_toDescriptorRecursive(nested, files, edition); // requires new file
  169. // Keep package-level options
  170. file.options = toDescriptorOptions(ns.options, exports.FileOptions);
  171. // And keep the file only if there is at least one nested object
  172. if (file.messageType.length + file.enumType.length + file.extension.length + file.service.length)
  173. files.push(file);
  174. }
  175. // --- Type ---
  176. /**
  177. * Properties of a DescriptorProto message.
  178. * @interface IDescriptorProto
  179. * @property {string} [name] Message type name
  180. * @property {IFieldDescriptorProto[]} [field] Fields
  181. * @property {IFieldDescriptorProto[]} [extension] Extension fields
  182. * @property {IDescriptorProto[]} [nestedType] Nested message types
  183. * @property {IEnumDescriptorProto[]} [enumType] Nested enums
  184. * @property {IDescriptorProtoExtensionRange[]} [extensionRange] Extension ranges
  185. * @property {IOneofDescriptorProto[]} [oneofDecl] Oneofs
  186. * @property {IMessageOptions} [options] Not supported
  187. * @property {IDescriptorProtoReservedRange[]} [reservedRange] Reserved ranges
  188. * @property {string[]} [reservedName] Reserved names
  189. */
  190. /**
  191. * Properties of a MessageOptions message.
  192. * @interface IMessageOptions
  193. * @property {boolean} [mapEntry=false] Whether this message is a map entry
  194. */
  195. /**
  196. * Properties of an ExtensionRange message.
  197. * @interface IDescriptorProtoExtensionRange
  198. * @property {number} [start] Start field id
  199. * @property {number} [end] End field id
  200. */
  201. /**
  202. * Properties of a ReservedRange message.
  203. * @interface IDescriptorProtoReservedRange
  204. * @property {number} [start] Start field id
  205. * @property {number} [end] End field id
  206. */
  207. var unnamedMessageIndex = 0;
  208. /**
  209. * Creates a type from a descriptor.
  210. * @param {IDescriptorProto|Reader|Uint8Array} descriptor Descriptor
  211. * @param {string|IDescriptorContext} [editionOrContext="proto2"] Syntax/edition shorthand or descriptor context
  212. * @returns {Type} Type instance
  213. */
  214. Type.fromDescriptor = function fromDescriptor(descriptor, editionOrContext) {
  215. return Type_fromDescriptor(descriptor, descriptorContext(editionOrContext), false, 0);
  216. };
  217. function Type_fromDescriptor(descriptor, ctx, nested, depth) {
  218. if (depth === undefined)
  219. depth = 0;
  220. if (depth > $protobuf.util.nestingLimit)
  221. throw Error("max depth exceeded");
  222. descriptor = decodeDescriptor(descriptor, exports.DescriptorProto);
  223. var type = new Type(descriptor.name.length ? descriptor.name : "Type" + unnamedMessageIndex++, fromDescriptorOptions(descriptor.options, exports.MessageOptions)),
  224. i,
  225. mapEntries = {};
  226. if (!nested) {
  227. type._edition = ctx.edition;
  228. applyContextFeatures(type, ctx);
  229. }
  230. if (descriptor.nestedType)
  231. for (i = 0; i < descriptor.nestedType.length; ++i)
  232. if (descriptor.nestedType[i].options && descriptor.nestedType[i].options.mapEntry)
  233. mapEntries[descriptor.nestedType[i].name] = descriptor.nestedType[i];
  234. var groupTypes = groupTypeNames(descriptor.field, descriptor.extension);
  235. /* Oneofs */ if (descriptor.oneofDecl)
  236. for (i = 0; i < descriptor.oneofDecl.length; ++i)
  237. type.add(OneOf_fromDescriptor(descriptor.oneofDecl[i], ctx, true));
  238. /* Fields */ if (descriptor.field)
  239. for (i = 0; i < descriptor.field.length; ++i) {
  240. var field = FieldBase_fromDescriptor(descriptor.field[i], ctx, true, mapEntries);
  241. type.add(field);
  242. if (descriptor.field[i].hasOwnProperty("oneofIndex")) // eslint-disable-line no-prototype-builtins
  243. type.oneofsArray[descriptor.field[i].oneofIndex].add(field);
  244. }
  245. /* Extension fields */ if (descriptor.extension)
  246. for (i = 0; i < descriptor.extension.length; ++i)
  247. type.add(Field_fromDescriptor(descriptor.extension[i], ctx, true));
  248. /* Nested types */ if (descriptor.nestedType)
  249. for (i = 0; i < descriptor.nestedType.length; ++i) {
  250. if (descriptor.nestedType[i].options && descriptor.nestedType[i].options.mapEntry)
  251. continue;
  252. var nestedType = Type_fromDescriptor(descriptor.nestedType[i], ctx, true, depth + 1);
  253. if (groupTypes[nestedType.name])
  254. nestedType.group = true;
  255. type.add(nestedType);
  256. }
  257. /* Nested enums */ if (descriptor.enumType)
  258. for (i = 0; i < descriptor.enumType.length; ++i)
  259. type.add(Enum_fromDescriptor(descriptor.enumType[i], ctx, true));
  260. /* Extension ranges */ if (descriptor.extensionRange && descriptor.extensionRange.length) {
  261. type.extensions = [];
  262. for (i = 0; i < descriptor.extensionRange.length; ++i)
  263. type.extensions.push([ descriptor.extensionRange[i].start, descriptor.extensionRange[i].end - 1 ]);
  264. }
  265. /* Reserved... */ if (descriptor.reservedRange && descriptor.reservedRange.length || descriptor.reservedName && descriptor.reservedName.length) {
  266. type.reserved = [];
  267. /* Ranges */ if (descriptor.reservedRange)
  268. for (i = 0; i < descriptor.reservedRange.length; ++i)
  269. type.reserved.push([ descriptor.reservedRange[i].start, descriptor.reservedRange[i].end - 1 ]);
  270. /* Names */ if (descriptor.reservedName)
  271. for (i = 0; i < descriptor.reservedName.length; ++i)
  272. type.reserved.push(descriptor.reservedName[i]);
  273. }
  274. return type;
  275. }
  276. /**
  277. * Converts a type to a descriptor.
  278. * @returns {Message<IDescriptorProto>} Descriptor
  279. * @param {string} [edition="proto2"] The syntax or edition to use
  280. */
  281. Type.prototype.toDescriptor = function toDescriptor(edition) {
  282. var descriptor = exports.DescriptorProto.create({ name: this.name }),
  283. i;
  284. /* Fields */ for (i = 0; i < this.fieldsArray.length; ++i) {
  285. var fieldDescriptor;
  286. descriptor.field.push(fieldDescriptor = this._fieldsArray[i].toDescriptor(edition));
  287. if (this._fieldsArray[i] instanceof MapField) { // map fields are repeated FieldNameEntry
  288. var keyType = toDescriptorType(this._fieldsArray[i].keyType, this._fieldsArray[i].resolvedKeyType, false),
  289. valueType = toDescriptorType(this._fieldsArray[i].type, this._fieldsArray[i].resolvedType, false),
  290. valueTypeName = valueType === /* type */ 11 || valueType === /* enum */ 14
  291. ? this._fieldsArray[i].resolvedType && shortname(this.parent, this._fieldsArray[i].resolvedType) || this._fieldsArray[i].type
  292. : undefined;
  293. descriptor.nestedType.push(exports.DescriptorProto.create({
  294. name: fieldDescriptor.typeName,
  295. field: [
  296. exports.FieldDescriptorProto.create({ name: "key", number: 1, label: 1, type: keyType }), // can't reference a type or enum
  297. exports.FieldDescriptorProto.create({ name: "value", number: 2, label: 1, type: valueType, typeName: valueTypeName })
  298. ],
  299. options: exports.MessageOptions.create({ mapEntry: true })
  300. }));
  301. }
  302. }
  303. /* Oneofs */ for (i = 0; i < this.oneofsArray.length; ++i)
  304. descriptor.oneofDecl.push(this._oneofsArray[i].toDescriptor());
  305. /* Nested... */ for (i = 0; i < this.nestedArray.length; ++i) {
  306. /* Extension fields */ if (this._nestedArray[i] instanceof Field)
  307. descriptor.field.push(this._nestedArray[i].toDescriptor(edition));
  308. /* Types */ else if (this._nestedArray[i] instanceof Type)
  309. descriptor.nestedType.push(this._nestedArray[i].toDescriptor(edition));
  310. /* Enums */ else if (this._nestedArray[i] instanceof Enum)
  311. descriptor.enumType.push(this._nestedArray[i].toDescriptor());
  312. // plain nested namespaces become packages instead in Root#toDescriptor
  313. }
  314. /* Extension ranges */ if (this.extensions)
  315. for (i = 0; i < this.extensions.length; ++i)
  316. descriptor.extensionRange.push(exports.DescriptorProto.ExtensionRange.create({ start: this.extensions[i][0], end: this.extensions[i][1] + 1 }));
  317. /* Reserved... */ if (this.reserved)
  318. for (i = 0; i < this.reserved.length; ++i)
  319. /* Names */ if (typeof this.reserved[i] === "string")
  320. descriptor.reservedName.push(this.reserved[i]);
  321. /* Ranges */ else
  322. descriptor.reservedRange.push(exports.DescriptorProto.ReservedRange.create({ start: this.reserved[i][0], end: this.reserved[i][1] + 1 }));
  323. descriptor.options = toDescriptorOptions(this.options, exports.MessageOptions);
  324. return descriptor;
  325. };
  326. // --- FieldBase ---
  327. function FieldBase_fromDescriptor(descriptor, ctx, nested, mapEntries) {
  328. var entryName = descriptor.typeName && descriptor.typeName.substring(descriptor.typeName.lastIndexOf(".") + 1),
  329. mapEntry = descriptor.label === 3 && descriptor.type === 11 && entryName
  330. ? mapEntries[entryName]
  331. : null;
  332. return mapEntry
  333. ? MapField_fromDescriptor(descriptor, mapEntry, ctx)
  334. : Field_fromDescriptor(descriptor, ctx, nested);
  335. }
  336. function fieldNameFromDescriptor(descriptor, ctx) {
  337. return ctx.keepCase && descriptor.name.length
  338. ? descriptor.name
  339. : descriptor.jsonName && descriptor.jsonName.length
  340. ? descriptor.jsonName
  341. : descriptor.name.length ? descriptor.name : "field" + descriptor.number;
  342. }
  343. function applyFieldNamesFromDescriptor(field, descriptor) {
  344. if (descriptor.name.length && descriptor.name !== field.name)
  345. field.protoName = descriptor.name;
  346. if (descriptor.jsonName && descriptor.jsonName.length)
  347. field.jsonName = descriptor.jsonName;
  348. return field;
  349. }
  350. // --- Field ---
  351. /**
  352. * Properties of a FieldDescriptorProto message.
  353. * @interface IFieldDescriptorProto
  354. * @property {string} [name] Field name
  355. * @property {number} [number] Field id
  356. * @property {IFieldDescriptorProtoLabel} [label] Field rule
  357. * @property {IFieldDescriptorProtoType} [type] Field basic type
  358. * @property {string} [typeName] Field type name
  359. * @property {string} [extendee] Extended type name
  360. * @property {string} [defaultValue] Literal default value
  361. * @property {number} [oneofIndex] Oneof index if part of a oneof
  362. * @property {string} [jsonName] JSON name (lowerCamelCase)
  363. * @property {IFieldOptions} [options] Field options
  364. * @property {boolean} [proto3Optional] Whether this is a proto3 optional field
  365. */
  366. /**
  367. * Values of the FieldDescriptorProto.Label enum.
  368. * @typedef IFieldDescriptorProtoLabel
  369. * @type {number}
  370. * @property {number} LABEL_OPTIONAL=1
  371. * @property {number} LABEL_REQUIRED=2
  372. * @property {number} LABEL_REPEATED=3
  373. */
  374. /**
  375. * Values of the FieldDescriptorProto.Type enum.
  376. * @typedef IFieldDescriptorProtoType
  377. * @type {number}
  378. * @property {number} TYPE_DOUBLE=1
  379. * @property {number} TYPE_FLOAT=2
  380. * @property {number} TYPE_INT64=3
  381. * @property {number} TYPE_UINT64=4
  382. * @property {number} TYPE_INT32=5
  383. * @property {number} TYPE_FIXED64=6
  384. * @property {number} TYPE_FIXED32=7
  385. * @property {number} TYPE_BOOL=8
  386. * @property {number} TYPE_STRING=9
  387. * @property {number} TYPE_GROUP=10
  388. * @property {number} TYPE_MESSAGE=11
  389. * @property {number} TYPE_BYTES=12
  390. * @property {number} TYPE_UINT32=13
  391. * @property {number} TYPE_ENUM=14
  392. * @property {number} TYPE_SFIXED32=15
  393. * @property {number} TYPE_SFIXED64=16
  394. * @property {number} TYPE_SINT32=17
  395. * @property {number} TYPE_SINT64=18
  396. */
  397. /**
  398. * Properties of a FieldOptions message.
  399. * @interface IFieldOptions
  400. * @property {boolean} [packed] Whether packed or not (defaults to `false` for proto2 and `true` for proto3)
  401. * @property {IFieldOptionsJSType} [jstype] JavaScript value type (not used by protobuf.js)
  402. */
  403. /**
  404. * Values of the FieldOptions.JSType enum.
  405. * @typedef IFieldOptionsJSType
  406. * @type {number}
  407. * @property {number} JS_NORMAL=0
  408. * @property {number} JS_STRING=1
  409. * @property {number} JS_NUMBER=2
  410. */
  411. /**
  412. * Creates a field from a descriptor.
  413. * @param {IFieldDescriptorProto|Reader|Uint8Array} descriptor Descriptor
  414. * @param {string|IDescriptorContext} [editionOrContext="proto2"] Syntax/edition shorthand or descriptor context
  415. * @returns {Field} Field instance
  416. */
  417. Field.fromDescriptor = function fromDescriptor(descriptor, editionOrContext) {
  418. return Field_fromDescriptor(descriptor, descriptorContext(editionOrContext), false);
  419. };
  420. function Field_fromDescriptor(descriptor, ctx, nested) {
  421. descriptor = decodeDescriptor(descriptor, exports.FieldDescriptorProto);
  422. if (typeof descriptor.number !== "number")
  423. throw Error("missing field id");
  424. // Rewire field type
  425. var typeName = descriptor.typeName,
  426. fieldType;
  427. if (typeName != null && typeName !== "") {
  428. if (typeof typeName !== "string" || !typeRefRe.test(typeName))
  429. throw Error("illegal type name: " + typeName);
  430. fieldType = typeName;
  431. } else
  432. fieldType = fromDescriptorType(descriptor.type);
  433. // Rewire field rule
  434. var fieldRule;
  435. switch (descriptor.label) {
  436. // 0 is reserved for errors
  437. case 1: fieldRule = undefined; break;
  438. case 2: fieldRule = "required"; break;
  439. case 3: fieldRule = "repeated"; break;
  440. default: throw Error("illegal label: " + descriptor.label);
  441. }
  442. var extendee = descriptor.extendee;
  443. if (extendee != null && extendee !== "") {
  444. if (typeof extendee !== "string" || !typeRefRe.test(extendee))
  445. throw Error("illegal type name: " + extendee);
  446. } else
  447. extendee = undefined;
  448. var field = applyFieldNamesFromDescriptor(new Field(
  449. fieldNameFromDescriptor(descriptor, ctx),
  450. descriptor.number,
  451. fieldType,
  452. fieldRule,
  453. extendee
  454. ), descriptor);
  455. if (!nested)
  456. field._edition = ctx.edition;
  457. field.options = fromDescriptorOptions(descriptor.options, exports.FieldOptions);
  458. if (!nested)
  459. applyContextFeatures(field, ctx);
  460. if (descriptor.proto3Optional || descriptor.proto3_optional)
  461. (field.options || (field.options = {})).proto3_optional = true;
  462. if (descriptor.defaultValue && descriptor.defaultValue.length) {
  463. var defaultValue = descriptor.defaultValue;
  464. switch (defaultValue) {
  465. case "true": case "TRUE":
  466. defaultValue = true;
  467. break;
  468. case "false": case "FALSE":
  469. defaultValue = false;
  470. break;
  471. default:
  472. var match = numberRe.exec(defaultValue);
  473. if (match)
  474. defaultValue = parseInt(defaultValue); // eslint-disable-line radix
  475. break;
  476. }
  477. field.setOption("default", defaultValue);
  478. }
  479. return field;
  480. }
  481. // --- MapField ---
  482. function MapField_fromDescriptor(descriptor, entryDescriptor, ctx) {
  483. function entryField(number) {
  484. if (entryDescriptor.field)
  485. for (var i = 0; i < entryDescriptor.field.length; ++i)
  486. if (entryDescriptor.field[i].number === number)
  487. return entryDescriptor.field[i];
  488. throw Error("invalid map entry descriptor: " + entryDescriptor.name);
  489. }
  490. var keyDescriptor = entryField(1),
  491. valueDescriptor = entryField(2),
  492. valueType = valueDescriptor.typeName && valueDescriptor.typeName.length
  493. ? valueDescriptor.typeName
  494. : fromDescriptorType(valueDescriptor.type);
  495. return applyFieldNamesFromDescriptor(new MapField(
  496. fieldNameFromDescriptor(descriptor, ctx),
  497. descriptor.number,
  498. fromDescriptorType(keyDescriptor.type),
  499. valueType,
  500. fromDescriptorOptions(descriptor.options, exports.FieldOptions)
  501. ), descriptor);
  502. }
  503. /**
  504. * Converts a field to a descriptor.
  505. * @returns {Message<IFieldDescriptorProto>} Descriptor
  506. * @param {string} [edition="proto2"] The syntax or edition to use
  507. */
  508. Field.prototype.toDescriptor = function toDescriptor(edition) {
  509. // Emit descriptor names in FieldDescriptorProto form, including json_name only
  510. // when it differs, and derive missing names so unresolved fields serialize consistently.
  511. var protoName = this.protoName || this.name;
  512. var jsonName = this.jsonName || $protobuf.util.jsonName(protoName);
  513. var descriptor = exports.FieldDescriptorProto.create({ name: protoName, number: this.id });
  514. if (jsonName !== protoName)
  515. descriptor.jsonName = jsonName;
  516. if (this.map) {
  517. descriptor.type = 11; // message
  518. descriptor.typeName = $protobuf.util.ucFirst(this.name); // fieldName -> FieldNameEntry (built in Type#toDescriptor)
  519. descriptor.label = 3; // repeated
  520. } else {
  521. // Rewire field type
  522. switch (descriptor.type = toDescriptorType(this.type, this.resolve().resolvedType, this.delimited)) {
  523. case 10: // group
  524. case 11: // type
  525. case 14: // enum
  526. descriptor.typeName = this.resolvedType ? shortname(this.parent, this.resolvedType) : this.type;
  527. break;
  528. }
  529. // Rewire field rule
  530. if (this.rule === "repeated") {
  531. descriptor.label = 3;
  532. } else if (this.required && edition === "proto2") {
  533. descriptor.label = 2;
  534. } else {
  535. descriptor.label = 1;
  536. }
  537. }
  538. // Handle extension field
  539. descriptor.extendee = this.extensionField ? this.extensionField.parent.fullName : this.extend;
  540. // Handle part of oneof (only meaningful for message types)
  541. if (this.partOf && this.parent instanceof Type) {
  542. if ((descriptor.oneofIndex = this.parent.oneofsArray.indexOf(this.partOf)) < 0)
  543. throw Error("missing oneof");
  544. }
  545. if (this.options) {
  546. descriptor.options = toDescriptorOptions(this.options, exports.FieldOptions);
  547. if (this.options["default"] != null)
  548. descriptor.defaultValue = String(this.options["default"]);
  549. if (this.options.proto3_optional)
  550. descriptor.proto3Optional = true;
  551. }
  552. if (edition === "proto3") { // defaults to packed=true
  553. if (!this.packed)
  554. (descriptor.options || (descriptor.options = exports.FieldOptions.create())).packed = false;
  555. } else if ((!edition || edition === "proto2") && this.packed) // defaults to packed=false
  556. (descriptor.options || (descriptor.options = exports.FieldOptions.create())).packed = true;
  557. return descriptor;
  558. };
  559. // --- Enum ---
  560. /**
  561. * Properties of an EnumDescriptorProto message.
  562. * @interface IEnumDescriptorProto
  563. * @property {string} [name] Enum name
  564. * @property {IEnumValueDescriptorProto[]} [value] Enum values
  565. * @property {IEnumOptions} [options] Enum options
  566. */
  567. /**
  568. * Properties of an EnumValueDescriptorProto message.
  569. * @interface IEnumValueDescriptorProto
  570. * @property {string} [name] Name
  571. * @property {number} [number] Value
  572. * @property {IEnumValueOptions} [options] Enum value options
  573. */
  574. /**
  575. * Properties of an EnumValueOptions message.
  576. * @interface IEnumValueOptions
  577. * @property {boolean} [deprecated]
  578. * @property {IFeatureSet} [features]
  579. * @property {boolean} [debugRedact]
  580. * @property {*} [featureSupport]
  581. * @property {Array.<*>} [uninterpretedOption]
  582. */
  583. /**
  584. * Properties of an EnumOptions message.
  585. * @interface IEnumOptions
  586. * @property {boolean} [allowAlias] Whether aliases are allowed
  587. * @property {boolean} [deprecated]
  588. */
  589. /**
  590. * Properties of a FeatureSet message.
  591. * @interface IFeatureSet
  592. * @property {number} [fieldPresence]
  593. * @property {number} [enumType]
  594. * @property {number} [repeatedFieldEncoding]
  595. * @property {number} [utf8Validation]
  596. * @property {number} [messageEncoding]
  597. * @property {number} [jsonFormat]
  598. * @property {number} [enforceNamingStyle]
  599. * @property {number} [defaultSymbolVisibility]
  600. */
  601. var unnamedEnumIndex = 0;
  602. /**
  603. * Creates an enum from a descriptor.
  604. * @param {IEnumDescriptorProto|Reader|Uint8Array} descriptor Descriptor
  605. * @param {string|IDescriptorContext} [editionOrContext="proto2"] Syntax/edition shorthand or descriptor context
  606. * @returns {Enum} Enum instance
  607. */
  608. Enum.fromDescriptor = function fromDescriptor(descriptor, editionOrContext) {
  609. return Enum_fromDescriptor(descriptor, descriptorContext(editionOrContext), false);
  610. };
  611. function Enum_fromDescriptor(descriptor, ctx, nested) {
  612. descriptor = decodeDescriptor(descriptor, exports.EnumDescriptorProto);
  613. // Construct values object
  614. var values = {},
  615. valuesOptions,
  616. i;
  617. if (descriptor.value)
  618. for (i = 0; i < descriptor.value.length; ++i) {
  619. var name = descriptor.value[i].name,
  620. valueName = name && name.length ? name : "NAME" + (descriptor.value[i].number || 0),
  621. value = descriptor.value[i].number || 0,
  622. options = fromDescriptorOptions(descriptor.value[i].options, exports.EnumValueOptions);
  623. values[valueName] = value;
  624. if (options)
  625. (valuesOptions || (valuesOptions = {}))[valueName] = options;
  626. }
  627. var enm = new Enum(
  628. descriptor.name && descriptor.name.length ? descriptor.name : "Enum" + unnamedEnumIndex++,
  629. values,
  630. fromDescriptorOptions(descriptor.options, exports.EnumOptions),
  631. undefined,
  632. undefined,
  633. valuesOptions
  634. );
  635. if (!nested) {
  636. enm._edition = ctx.edition;
  637. applyContextFeatures(enm, ctx);
  638. }
  639. /* Reserved... */ if (descriptor.reservedRange && descriptor.reservedRange.length || descriptor.reservedName && descriptor.reservedName.length) {
  640. enm.reserved = [];
  641. /* Ranges */ if (descriptor.reservedRange)
  642. for (i = 0; i < descriptor.reservedRange.length; ++i)
  643. enm.reserved.push([ descriptor.reservedRange[i].start, descriptor.reservedRange[i].end ]);
  644. /* Names */ if (descriptor.reservedName)
  645. for (i = 0; i < descriptor.reservedName.length; ++i)
  646. enm.reserved.push(descriptor.reservedName[i]);
  647. }
  648. return enm;
  649. }
  650. /**
  651. * Converts an enum to a descriptor.
  652. * @returns {Message<IEnumDescriptorProto>} Descriptor
  653. */
  654. Enum.prototype.toDescriptor = function toDescriptor() {
  655. // Values
  656. var values = [],
  657. i,
  658. ks = Object.keys(this.values);
  659. for (i = 0; i < ks.length; ++i)
  660. values.push(exports.EnumValueDescriptorProto.create({
  661. name: ks[i],
  662. number: this.values[ks[i]],
  663. options: this.valuesOptions && toDescriptorOptions(this.valuesOptions[ks[i]], exports.EnumValueOptions)
  664. }));
  665. var descriptor = exports.EnumDescriptorProto.create({
  666. name: this.name,
  667. value: values,
  668. options: toDescriptorOptions(this.options, exports.EnumOptions)
  669. });
  670. /* Reserved... */ if (this.reserved)
  671. for (i = 0; i < this.reserved.length; ++i)
  672. /* Names */ if (typeof this.reserved[i] === "string")
  673. descriptor.reservedName.push(this.reserved[i]);
  674. /* Ranges */ else
  675. descriptor.reservedRange.push(exports.EnumDescriptorProto.EnumReservedRange.create({ start: this.reserved[i][0], end: this.reserved[i][1] }));
  676. return descriptor;
  677. };
  678. // --- OneOf ---
  679. /**
  680. * Properties of a OneofDescriptorProto message.
  681. * @interface IOneofDescriptorProto
  682. * @property {string} [name] Oneof name
  683. * @property {IOneofOptions} [options] Oneof options
  684. */
  685. /**
  686. * Properties of a OneofOptions message.
  687. * @interface IOneofOptions
  688. * @property {IFeatureSet} [features]
  689. * @property {Array.<*>} [uninterpretedOption]
  690. */
  691. var unnamedOneofIndex = 0;
  692. /**
  693. * Creates a oneof from a descriptor.
  694. * @param {IOneofDescriptorProto|Reader|Uint8Array} descriptor Descriptor
  695. * @param {string|IDescriptorContext} [editionOrContext="proto2"] Syntax/edition shorthand or descriptor context
  696. * @returns {OneOf} OneOf instance
  697. */
  698. OneOf.fromDescriptor = function fromDescriptor(descriptor, editionOrContext) {
  699. return OneOf_fromDescriptor(descriptor, descriptorContext(editionOrContext), false);
  700. };
  701. function OneOf_fromDescriptor(descriptor, ctx, nested) {
  702. descriptor = decodeDescriptor(descriptor, exports.OneofDescriptorProto);
  703. var oneof = new OneOf(
  704. // unnamedOneOfIndex is global, not per type, because we have no ref to a type here
  705. descriptor.name && descriptor.name.length ? descriptor.name : "oneof" + unnamedOneofIndex++,
  706. fromDescriptorOptions(descriptor.options, exports.OneofOptions)
  707. );
  708. if (!nested) {
  709. oneof._edition = ctx.edition;
  710. applyContextFeatures(oneof, ctx);
  711. }
  712. return oneof;
  713. }
  714. /**
  715. * Converts a oneof to a descriptor.
  716. * @returns {Message<IOneofDescriptorProto>} Descriptor
  717. */
  718. OneOf.prototype.toDescriptor = function toDescriptor() {
  719. return exports.OneofDescriptorProto.create({
  720. name: this.name,
  721. options: toDescriptorOptions(this.options, exports.OneofOptions)
  722. });
  723. };
  724. // --- Service ---
  725. /**
  726. * Properties of a ServiceDescriptorProto message.
  727. * @interface IServiceDescriptorProto
  728. * @property {string} [name] Service name
  729. * @property {IMethodDescriptorProto[]} [method] Methods
  730. * @property {IServiceOptions} [options] Options
  731. */
  732. /**
  733. * Properties of a ServiceOptions message.
  734. * @interface IServiceOptions
  735. * @property {boolean} [deprecated]
  736. */
  737. var unnamedServiceIndex = 0;
  738. /**
  739. * Creates a service from a descriptor.
  740. * @param {IServiceDescriptorProto|Reader|Uint8Array} descriptor Descriptor
  741. * @param {string|IDescriptorContext} [editionOrContext="proto2"] Syntax/edition shorthand or descriptor context
  742. * @returns {Service} Service instance
  743. */
  744. Service.fromDescriptor = function fromDescriptor(descriptor, editionOrContext) {
  745. return Service_fromDescriptor(descriptor, descriptorContext(editionOrContext), false);
  746. };
  747. function Service_fromDescriptor(descriptor, ctx, nested) {
  748. descriptor = decodeDescriptor(descriptor, exports.ServiceDescriptorProto);
  749. var service = new Service(descriptor.name && descriptor.name.length ? descriptor.name : "Service" + unnamedServiceIndex++, fromDescriptorOptions(descriptor.options, exports.ServiceOptions));
  750. if (!nested) {
  751. service._edition = ctx.edition;
  752. applyContextFeatures(service, ctx);
  753. }
  754. if (descriptor.method)
  755. for (var i = 0; i < descriptor.method.length; ++i)
  756. service.add(Method_fromDescriptor(descriptor.method[i], ctx, true));
  757. return service;
  758. }
  759. /**
  760. * Converts a service to a descriptor.
  761. * @returns {Message<IServiceDescriptorProto>} Descriptor
  762. */
  763. Service.prototype.toDescriptor = function toDescriptor() {
  764. // Methods
  765. var methods = [];
  766. for (var i = 0; i < this.methodsArray.length; ++i)
  767. methods.push(this._methodsArray[i].toDescriptor());
  768. return exports.ServiceDescriptorProto.create({
  769. name: this.name,
  770. method: methods,
  771. options: toDescriptorOptions(this.options, exports.ServiceOptions)
  772. });
  773. };
  774. // --- Method ---
  775. /**
  776. * Properties of a MethodDescriptorProto message.
  777. * @interface IMethodDescriptorProto
  778. * @property {string} [name] Method name
  779. * @property {string} [inputType] Request type name
  780. * @property {string} [outputType] Response type name
  781. * @property {IMethodOptions} [options] Not supported
  782. * @property {boolean} [clientStreaming=false] Whether requests are streamed
  783. * @property {boolean} [serverStreaming=false] Whether responses are streamed
  784. */
  785. /**
  786. * Properties of a MethodOptions message.
  787. * @interface IMethodOptions
  788. * @property {boolean} [deprecated]
  789. */
  790. var unnamedMethodIndex = 0;
  791. /**
  792. * Creates a method from a descriptor.
  793. * @param {IMethodDescriptorProto|Reader|Uint8Array} descriptor Descriptor
  794. * @param {string|IDescriptorContext} [editionOrContext="proto2"] Syntax/edition shorthand or descriptor context
  795. * @returns {Method} Reflected method instance
  796. */
  797. Method.fromDescriptor = function fromDescriptor(descriptor, editionOrContext) {
  798. return Method_fromDescriptor(descriptor, descriptorContext(editionOrContext), false);
  799. };
  800. function Method_fromDescriptor(descriptor, ctx, nested) {
  801. descriptor = decodeDescriptor(descriptor, exports.MethodDescriptorProto);
  802. var inputType = descriptor.inputType,
  803. outputType = descriptor.outputType;
  804. if (inputType != null && inputType !== "") {
  805. if (typeof inputType !== "string" || !typeRefRe.test(inputType))
  806. throw Error("illegal type name: " + inputType);
  807. }
  808. if (outputType != null && outputType !== "") {
  809. if (typeof outputType !== "string" || !typeRefRe.test(outputType))
  810. throw Error("illegal type name: " + outputType);
  811. }
  812. var method = new Method(
  813. // unnamedMethodIndex is global, not per service, because we have no ref to a service here
  814. descriptor.name && descriptor.name.length ? descriptor.name : "Method" + unnamedMethodIndex++,
  815. "rpc",
  816. inputType,
  817. outputType,
  818. Boolean(descriptor.clientStreaming),
  819. Boolean(descriptor.serverStreaming),
  820. fromDescriptorOptions(descriptor.options, exports.MethodOptions)
  821. );
  822. if (!nested) {
  823. method._edition = ctx.edition;
  824. applyContextFeatures(method, ctx);
  825. }
  826. return method;
  827. }
  828. /**
  829. * Converts a method to a descriptor.
  830. * @returns {Message<IMethodDescriptorProto>} Descriptor
  831. */
  832. Method.prototype.toDescriptor = function toDescriptor() {
  833. return exports.MethodDescriptorProto.create({
  834. name: this.name,
  835. inputType: this.resolvedRequestType ? this.resolvedRequestType.fullName : this.requestType,
  836. outputType: this.resolvedResponseType ? this.resolvedResponseType.fullName : this.responseType,
  837. clientStreaming: this.requestStream,
  838. serverStreaming: this.responseStream,
  839. options: toDescriptorOptions(this.options, exports.MethodOptions)
  840. });
  841. };
  842. // --- utility ---
  843. function descriptorContext(editionOrContext) {
  844. if (editionOrContext && typeof editionOrContext === "object") {
  845. var ctx = $protobuf.util.merge({}, editionOrContext);
  846. if (!ctx.edition)
  847. ctx.edition = "proto2";
  848. return ctx;
  849. }
  850. return { edition: editionOrContext || "proto2" };
  851. }
  852. function applyContextFeatures(object, ctx) {
  853. if (!ctx.features)
  854. return object;
  855. var options = object.options || (object.options = {});
  856. options.features = $protobuf.util.merge({}, ctx.features, options.features);
  857. return object;
  858. }
  859. // Converts a descriptor type to a protobuf.js basic type
  860. function fromDescriptorType(type) {
  861. switch (type) {
  862. // 0 is reserved for errors
  863. case 1: return "double";
  864. case 2: return "float";
  865. case 3: return "int64";
  866. case 4: return "uint64";
  867. case 5: return "int32";
  868. case 6: return "fixed64";
  869. case 7: return "fixed32";
  870. case 8: return "bool";
  871. case 9: return "string";
  872. case 12: return "bytes";
  873. case 13: return "uint32";
  874. case 15: return "sfixed32";
  875. case 16: return "sfixed64";
  876. case 17: return "sint32";
  877. case 18: return "sint64";
  878. }
  879. throw Error("illegal type: " + type);
  880. }
  881. function groupTypeNames() {
  882. var names = {};
  883. for (var a = 0; a < arguments.length; ++a) {
  884. var fields = arguments[a];
  885. if (!fields)
  886. continue;
  887. for (var i = 0; i < fields.length; ++i)
  888. if (fields[i].type === 10 && fields[i].typeName) {
  889. var name = fields[i].typeName.split(".").pop();
  890. if (name)
  891. names[name] = true;
  892. }
  893. }
  894. return names;
  895. }
  896. // Converts a protobuf.js basic type to a descriptor type
  897. function toDescriptorType(type, resolvedType, delimited) {
  898. switch (type) {
  899. // 0 is reserved for errors
  900. case "double": return 1;
  901. case "float": return 2;
  902. case "int64": return 3;
  903. case "uint64": return 4;
  904. case "int32": return 5;
  905. case "fixed64": return 6;
  906. case "fixed32": return 7;
  907. case "bool": return 8;
  908. case "string": return 9;
  909. case "bytes": return 12;
  910. case "uint32": return 13;
  911. case "sfixed32": return 15;
  912. case "sfixed64": return 16;
  913. case "sint32": return 17;
  914. case "sint64": return 18;
  915. }
  916. if (resolvedType instanceof Enum)
  917. return 14;
  918. if (resolvedType instanceof Type)
  919. return delimited ? 10 : 11;
  920. throw Error("illegal type: " + type);
  921. }
  922. function fromDescriptorOptionsRecursive(obj, type) {
  923. var val = {};
  924. for (var i = 0, field, key; i < type.fieldsArray.length; ++i) {
  925. if ((key = (field = type._fieldsArray[i]).name) === "uninterpretedOption") continue;
  926. if (!Object.prototype.hasOwnProperty.call(obj, key)) continue;
  927. var newKey = underScore(key);
  928. if (field.resolvedType instanceof Type) {
  929. val[newKey] = fromDescriptorOptionsRecursive(obj[key], field.resolvedType);
  930. } else if(field.resolvedType instanceof Enum) {
  931. val[newKey] = field.resolvedType.valuesById[obj[key]];
  932. } else {
  933. val[newKey] = obj[key];
  934. }
  935. }
  936. return val;
  937. }
  938. // Converts descriptor options to an options object
  939. function fromDescriptorOptions(options, type) {
  940. if (!options)
  941. return undefined;
  942. return fromDescriptorOptionsRecursive(type.toObject(options), type);
  943. }
  944. function toDescriptorOptionsRecursive(obj, type) {
  945. var val = {};
  946. var keys = Object.keys(obj);
  947. for (var i = 0; i < keys.length; ++i) {
  948. var key = keys[i];
  949. var newKey = $protobuf.util.camelCase(key);
  950. if (!Object.prototype.hasOwnProperty.call(type.fields, newKey)) continue;
  951. var field = type.fields[newKey];
  952. if (field.resolvedType instanceof Type) {
  953. val[newKey] = toDescriptorOptionsRecursive(obj[key], field.resolvedType);
  954. } else {
  955. val[newKey] = obj[key];
  956. }
  957. if (field.repeated && !Array.isArray(val[newKey])) {
  958. val[newKey] = [val[newKey]];
  959. }
  960. }
  961. return val;
  962. }
  963. // Converts an options object to descriptor options
  964. function toDescriptorOptions(options, type) {
  965. if (!options)
  966. return undefined;
  967. return type.fromObject(toDescriptorOptionsRecursive(options, type));
  968. }
  969. // Decodes descriptor input supplied as either a reader or buffer.
  970. function decodeDescriptor(descriptor, type) {
  971. if (descriptor instanceof $protobuf.Reader || typeof descriptor.length === "number")
  972. return type.decode(descriptor);
  973. return descriptor;
  974. }
  975. // Calculates the shortest relative path from `from` to `to`.
  976. function shortname(from, to) {
  977. var fromPath = from.fullName.split("."),
  978. toPath = to.fullName.split("."),
  979. i = 0,
  980. j = 0,
  981. k = toPath.length - 1;
  982. if (!(from instanceof Root) && to instanceof Namespace)
  983. while (i < fromPath.length && j < k && fromPath[i] === toPath[j]) {
  984. var other = to.lookup(fromPath[i++], true);
  985. if (other !== null && other !== to)
  986. break;
  987. ++j;
  988. }
  989. else
  990. for (; i < fromPath.length && j < k && fromPath[i] === toPath[j]; ++i, ++j);
  991. return toPath.slice(j).join(".");
  992. }
  993. // copied here from cli/targets/proto.js
  994. function underScore(str) {
  995. return str.substring(0,1)
  996. + str.substring(1)
  997. .replace(/([A-Z])(?=[a-z]|$)/g, function($0, $1) { return "_" + $1.toLowerCase(); });
  998. }
  999. function editionFromDescriptor(fileDescriptor) {
  1000. if (fileDescriptor.syntax === "editions") {
  1001. switch(fileDescriptor.edition) {
  1002. case exports.Edition.EDITION_2023:
  1003. return "2023";
  1004. case exports.Edition.EDITION_2024:
  1005. return "2024";
  1006. default:
  1007. throw new Error("Unsupported edition " + fileDescriptor.edition);
  1008. }
  1009. }
  1010. if (fileDescriptor.syntax === "proto3") {
  1011. return "proto3";
  1012. }
  1013. return "proto2";
  1014. }
  1015. function editionToDescriptor(edition, fileDescriptor) {
  1016. if (!edition) return;
  1017. if (edition === "proto2" || edition === "proto3") {
  1018. fileDescriptor.syntax = edition;
  1019. } else {
  1020. fileDescriptor.syntax = "editions";
  1021. switch(edition) {
  1022. case "2023":
  1023. fileDescriptor.edition = exports.Edition.EDITION_2023;
  1024. break;
  1025. case "2024":
  1026. fileDescriptor.edition = exports.Edition.EDITION_2024;
  1027. break;
  1028. default:
  1029. throw new Error("Unsupported edition " + edition);
  1030. }
  1031. }
  1032. }
  1033. // --- exports ---
  1034. /**
  1035. * Reflected file descriptor set.
  1036. * @name FileDescriptorSet
  1037. * @type {$protobuf.Type}
  1038. * @const
  1039. */
  1040. /**
  1041. * Reflected file descriptor proto.
  1042. * @name FileDescriptorProto
  1043. * @type {$protobuf.Type}
  1044. * @const
  1045. */
  1046. /**
  1047. * Reflected descriptor proto.
  1048. * @name DescriptorProto
  1049. * @type {$protobuf.Type & {
  1050. * ExtensionRange: $protobuf.Type,
  1051. * ReservedRange: $protobuf.Type
  1052. * }}
  1053. * @const
  1054. */
  1055. /**
  1056. * Reflected field descriptor proto.
  1057. * @name FieldDescriptorProto
  1058. * @type {$protobuf.Type & {
  1059. * Label: $protobuf.Enum,
  1060. * Type: $protobuf.Enum
  1061. * }}
  1062. * @const
  1063. */
  1064. /**
  1065. * Reflected oneof descriptor proto.
  1066. * @name OneofDescriptorProto
  1067. * @type {$protobuf.Type}
  1068. * @const
  1069. */
  1070. /**
  1071. * Reflected enum descriptor proto.
  1072. * @name EnumDescriptorProto
  1073. * @type {$protobuf.Type}
  1074. * @const
  1075. */
  1076. /**
  1077. * Reflected service descriptor proto.
  1078. * @name ServiceDescriptorProto
  1079. * @type {$protobuf.Type}
  1080. * @const
  1081. */
  1082. /**
  1083. * Reflected enum value descriptor proto.
  1084. * @name EnumValueDescriptorProto
  1085. * @type {$protobuf.Type}
  1086. * @const
  1087. */
  1088. /**
  1089. * Reflected method descriptor proto.
  1090. * @name MethodDescriptorProto
  1091. * @type {$protobuf.Type}
  1092. * @const
  1093. */
  1094. /**
  1095. * Reflected file options.
  1096. * @name FileOptions
  1097. * @type {$protobuf.Type & {
  1098. * OptimizeMode: $protobuf.Enum
  1099. * }}
  1100. * @const
  1101. */
  1102. /**
  1103. * Reflected message options.
  1104. * @name MessageOptions
  1105. * @type {$protobuf.Type}
  1106. * @const
  1107. */
  1108. /**
  1109. * Reflected field options.
  1110. * @name FieldOptions
  1111. * @type {$protobuf.Type & {
  1112. * CType: $protobuf.Enum,
  1113. * JSType: $protobuf.Enum
  1114. * }}
  1115. * @const
  1116. */
  1117. /**
  1118. * Reflected oneof options.
  1119. * @name OneofOptions
  1120. * @type {$protobuf.Type}
  1121. * @const
  1122. */
  1123. /**
  1124. * Reflected enum options.
  1125. * @name EnumOptions
  1126. * @type {$protobuf.Type}
  1127. * @const
  1128. */
  1129. /**
  1130. * Reflected enum value options.
  1131. * @name EnumValueOptions
  1132. * @type {$protobuf.Type}
  1133. * @const
  1134. */
  1135. /**
  1136. * Reflected service options.
  1137. * @name ServiceOptions
  1138. * @type {$protobuf.Type}
  1139. * @const
  1140. */
  1141. /**
  1142. * Reflected method options.
  1143. * @name MethodOptions
  1144. * @type {$protobuf.Type}
  1145. * @const
  1146. */
  1147. /**
  1148. * Reflected feature set.
  1149. * @name FeatureSet
  1150. * @type {$protobuf.Type & {
  1151. * FieldPresence: $protobuf.Enum,
  1152. * EnumType: $protobuf.Enum,
  1153. * RepeatedFieldEncoding: $protobuf.Enum,
  1154. * Utf8Validation: $protobuf.Enum,
  1155. * MessageEncoding: $protobuf.Enum,
  1156. * JsonFormat: $protobuf.Enum,
  1157. * EnforceNamingStyle: $protobuf.Enum,
  1158. * VisibilityFeature: $protobuf.Type
  1159. * }}
  1160. * @const
  1161. */
  1162. /**
  1163. * Reflected feature set defaults.
  1164. * @name FeatureSetDefaults
  1165. * @type {$protobuf.Type & {
  1166. * FeatureSetEditionDefault: $protobuf.Type
  1167. * }}
  1168. * @const
  1169. */
  1170. /**
  1171. * Reflected uninterpretet option.
  1172. * @name UninterpretedOption
  1173. * @type {$protobuf.Type & {
  1174. * NamePart: $protobuf.Type
  1175. * }}
  1176. * @const
  1177. */
  1178. /**
  1179. * Reflected source code info.
  1180. * @name SourceCodeInfo
  1181. * @type {$protobuf.Type & {
  1182. * Location: $protobuf.Type
  1183. * }}
  1184. * @const
  1185. */
  1186. /**
  1187. * Reflected generated code info.
  1188. * @name GeneratedCodeInfo
  1189. * @type {$protobuf.Type & {
  1190. * Annotation: $protobuf.Type
  1191. * }}
  1192. * @const
  1193. */