index.d.ts 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741
  1. // DO NOT EDIT! This is a generated file. Edit the JSDoc in src/*.js instead and run 'npm run build:types'.
  2. export as namespace protobuf;
  3. /**
  4. * Provides common type definitions.
  5. * Can also be used to provide additional google types or your own custom types.
  6. * @param name Short name as in `google/protobuf/[name].proto` or full file name
  7. * @param json JSON definition within `google.protobuf` if a short name, otherwise the file's root definition
  8. */
  9. export function common(name: string, json: { [k: string]: any }): void;
  10. export namespace common {
  11. /** Properties of a google.protobuf.Any message. */
  12. interface IAny {
  13. typeUrl?: string;
  14. bytes?: Uint8Array;
  15. }
  16. /** Properties of a google.protobuf.Duration message. */
  17. interface IDuration {
  18. seconds?: (number|Long);
  19. nanos?: number;
  20. }
  21. /** Properties of a google.protobuf.Timestamp message. */
  22. interface ITimestamp {
  23. seconds?: (number|Long);
  24. nanos?: number;
  25. }
  26. /** Properties of a google.protobuf.Empty message. */
  27. interface IEmpty {
  28. }
  29. /** Properties of a google.protobuf.Struct message. */
  30. interface IStruct {
  31. fields?: { [k: string]: IValue };
  32. }
  33. /** Properties of a google.protobuf.Value message. */
  34. interface IValue {
  35. kind?: string;
  36. nullValue?: 0;
  37. numberValue?: number;
  38. stringValue?: string;
  39. boolValue?: boolean;
  40. structValue?: IStruct;
  41. listValue?: IListValue;
  42. }
  43. /** Properties of a google.protobuf.ListValue message. */
  44. interface IListValue {
  45. values?: IValue[];
  46. }
  47. /** Properties of a google.protobuf.DoubleValue message. */
  48. interface IDoubleValue {
  49. value?: number;
  50. }
  51. /** Properties of a google.protobuf.FloatValue message. */
  52. interface IFloatValue {
  53. value?: number;
  54. }
  55. /** Properties of a google.protobuf.Int64Value message. */
  56. interface IInt64Value {
  57. value?: (number|Long);
  58. }
  59. /** Properties of a google.protobuf.UInt64Value message. */
  60. interface IUInt64Value {
  61. value?: (number|Long);
  62. }
  63. /** Properties of a google.protobuf.Int32Value message. */
  64. interface IInt32Value {
  65. value?: number;
  66. }
  67. /** Properties of a google.protobuf.UInt32Value message. */
  68. interface IUInt32Value {
  69. value?: number;
  70. }
  71. /** Properties of a google.protobuf.BoolValue message. */
  72. interface IBoolValue {
  73. value?: boolean;
  74. }
  75. /** Properties of a google.protobuf.StringValue message. */
  76. interface IStringValue {
  77. value?: string;
  78. }
  79. /** Properties of a google.protobuf.BytesValue message. */
  80. interface IBytesValue {
  81. value?: Uint8Array;
  82. }
  83. /**
  84. * Gets the root definition of the specified common proto file.
  85. *
  86. * Bundled definitions are:
  87. * - google/protobuf/any.proto
  88. * - google/protobuf/duration.proto
  89. * - google/protobuf/empty.proto
  90. * - google/protobuf/field_mask.proto
  91. * - google/protobuf/struct.proto
  92. * - google/protobuf/timestamp.proto
  93. * - google/protobuf/wrappers.proto
  94. *
  95. * @param file Proto file name
  96. * @returns Root definition or `null` if not defined
  97. */
  98. function get(file: string): (INamespace|null);
  99. }
  100. /** Runtime message from/to plain object converters. */
  101. export namespace converter {
  102. /**
  103. * Generates a plain object to runtime message converter specific to the specified message type.
  104. * @param mtype Message type
  105. * @returns Codegen instance
  106. */
  107. function fromObject(mtype: Type): Codegen;
  108. /**
  109. * Generates a runtime message to plain object converter specific to the specified message type.
  110. * @param mtype Message type
  111. * @returns Codegen instance
  112. */
  113. function toObject(mtype: Type): Codegen;
  114. }
  115. /**
  116. * Generates a decoder specific to the specified message type.
  117. * @param mtype Message type
  118. * @returns Codegen instance
  119. */
  120. export function decoder(mtype: Type): Codegen;
  121. /**
  122. * Generates an encoder specific to the specified message type.
  123. * @param mtype Message type
  124. * @returns Codegen instance
  125. */
  126. export function encoder(mtype: Type): Codegen;
  127. /** Reflected enum. */
  128. export class Enum extends ReflectionObject {
  129. /**
  130. * Constructs a new enum instance.
  131. * @param name Unique name within its namespace
  132. * @param [values] Enum values as an object, by name
  133. * @param [options] Declared options
  134. * @param [comment] The comment for this enum
  135. * @param [comments] The value comments for this enum
  136. * @param [valuesOptions] The value options for this enum
  137. */
  138. constructor(name: string, values?: { [k: string]: number }, options?: { [k: string]: any }, comment?: string, comments?: { [k: string]: string }, valuesOptions?: ({ [k: string]: { [k: string]: any } }|undefined));
  139. /** Enum values by id. */
  140. public valuesById: { [k: number]: string };
  141. /** Enum values by name. */
  142. public values: { [k: string]: number };
  143. /** Enum comment text. */
  144. public comment: (string|null);
  145. /** Value comment texts, if any. */
  146. public comments: { [k: string]: string };
  147. /** Values options, if any */
  148. public valuesOptions?: { [k: string]: { [k: string]: any } };
  149. /** Reserved ranges, if any. */
  150. public reserved: (number[]|string)[];
  151. /**
  152. * Constructs an enum from an enum descriptor.
  153. * @param name Enum name
  154. * @param json Enum descriptor
  155. * @returns Created enum
  156. * @throws {TypeError} If arguments are invalid
  157. */
  158. public static fromJSON(name: string, json: IEnum): Enum;
  159. /**
  160. * Converts this enum to an enum descriptor.
  161. * @param [toJSONOptions] JSON conversion options
  162. * @returns Enum descriptor
  163. */
  164. public toJSON(toJSONOptions?: IToJSONOptions): IEnum;
  165. /**
  166. * Adds a value to this enum.
  167. * @param name Value name
  168. * @param id Value id
  169. * @param [comment] Comment, if any
  170. * @param {Object.<string, *>|undefined} [options] Options, if any
  171. * @returns `this`
  172. * @throws {TypeError} If arguments are invalid
  173. * @throws {Error} If there is already a value with this name or id
  174. */
  175. public add(name: string, id: number, comment?: string, options?: ({ [k: string]: any }|undefined)): Enum;
  176. /**
  177. * Removes a value from this enum
  178. * @param name Value name
  179. * @returns `this`
  180. * @throws {TypeError} If arguments are invalid
  181. * @throws {Error} If `name` is not a name of this enum
  182. */
  183. public remove(name: string): Enum;
  184. /**
  185. * Tests if the specified id is reserved.
  186. * @param id Id to test
  187. * @returns `true` if reserved, otherwise `false`
  188. */
  189. public isReservedId(id: number): boolean;
  190. /**
  191. * Tests if the specified name is reserved.
  192. * @param name Name to test
  193. * @returns `true` if reserved, otherwise `false`
  194. */
  195. public isReservedName(name: string): boolean;
  196. }
  197. /** Enum descriptor. */
  198. export interface IEnum {
  199. /** Enum values */
  200. values: { [k: string]: number };
  201. /** Enum options */
  202. options?: { [k: string]: any };
  203. }
  204. /** Reflected message field. */
  205. export class Field extends FieldBase {
  206. /**
  207. * Constructs a new message field instance. Note that {@link MapField|map fields} have their own class.
  208. * @param name Unique name within its namespace
  209. * @param id Unique id within its namespace
  210. * @param type Value type
  211. * @param [rule="optional"] Field rule
  212. * @param [extend] Extended type if different from parent
  213. * @param [options] Declared options
  214. */
  215. constructor(name: string, id: number, type: string, rule?: (string|{ [k: string]: any }), extend?: (string|{ [k: string]: any }), options?: { [k: string]: any });
  216. /**
  217. * Constructs a field from a field descriptor.
  218. * @param name Field name
  219. * @param json Field descriptor
  220. * @returns Created field
  221. * @throws {TypeError} If arguments are invalid
  222. */
  223. public static fromJSON(name: string, json: IField): Field;
  224. /** Determines whether this field is packed. Only relevant when repeated and working with proto2. */
  225. public readonly packed: boolean;
  226. /**
  227. * Field decorator (TypeScript).
  228. * @param fieldId Field id
  229. * @param fieldType Field type
  230. * @param [fieldRule="optional"] Field rule
  231. * @param [defaultValue] Default value
  232. * @returns Decorator function
  233. */
  234. public static d<T extends number | number[] | Long | Long[] | string | string[] | boolean | boolean[] | Uint8Array | Uint8Array[] | Buffer | Buffer[]>(fieldId: number, fieldType: ("double"|"float"|"int32"|"uint32"|"sint32"|"fixed32"|"sfixed32"|"int64"|"uint64"|"sint64"|"fixed64"|"sfixed64"|"string"|"bool"|"bytes"|object), fieldRule?: ("optional"|"required"|"repeated"), defaultValue?: T): FieldDecorator;
  235. /**
  236. * Field decorator (TypeScript).
  237. * @param fieldId Field id
  238. * @param fieldType Field type
  239. * @param [fieldRule="optional"] Field rule
  240. * @returns Decorator function
  241. */
  242. public static d<T extends Message<T>>(fieldId: number, fieldType: (Constructor<T>|string), fieldRule?: ("optional"|"required"|"repeated")): FieldDecorator;
  243. }
  244. /** Base class of all reflected message fields. This is not an actual class but here for the sake of having consistent type definitions. */
  245. export class FieldBase extends ReflectionObject {
  246. /**
  247. * Not an actual constructor. Use {@link Field} instead.
  248. * @param name Unique name within its namespace
  249. * @param id Unique id within its namespace
  250. * @param type Value type
  251. * @param [rule="optional"] Field rule
  252. * @param [extend] Extended type if different from parent
  253. * @param [options] Declared options
  254. * @param [comment] Comment associated with this field
  255. */
  256. constructor(name: string, id: number, type: string, rule?: (string|{ [k: string]: any }), extend?: (string|{ [k: string]: any }), options?: { [k: string]: any }, comment?: string);
  257. /** Field type. */
  258. public type: string;
  259. /** Unique field id. */
  260. public id: number;
  261. /** Extended type if different from parent. */
  262. public extend?: string;
  263. /** Whether this field is required. */
  264. public required: boolean;
  265. /** Whether this field is optional. */
  266. public optional: boolean;
  267. /** Whether this field is repeated. */
  268. public repeated: boolean;
  269. /** Whether this field is a map or not. */
  270. public map: boolean;
  271. /** Message this field belongs to. */
  272. public message: (Type|null);
  273. /** OneOf this field belongs to, if any, */
  274. public partOf: (OneOf|null);
  275. /** The field type's default value. */
  276. public typeDefault: any;
  277. /** The field's default value on prototypes. */
  278. public defaultValue: any;
  279. /** Whether this field's value should be treated as a long. */
  280. public long: boolean;
  281. /** Whether this field's value is a buffer. */
  282. public bytes: boolean;
  283. /** Resolved type if not a basic type. */
  284. public resolvedType: (Type|Enum|null);
  285. /** Sister-field within the extended type if a declaring extension field. */
  286. public extensionField: (Field|null);
  287. /** Sister-field within the declaring namespace if an extended field. */
  288. public declaringField: (Field|null);
  289. /** Comment for this field. */
  290. public comment: (string|null);
  291. /**
  292. * Converts this field to a field descriptor.
  293. * @param [toJSONOptions] JSON conversion options
  294. * @returns Field descriptor
  295. */
  296. public toJSON(toJSONOptions?: IToJSONOptions): IField;
  297. /**
  298. * Resolves this field's type references.
  299. * @returns `this`
  300. * @throws {Error} If any reference cannot be resolved
  301. */
  302. public resolve(): Field;
  303. }
  304. /** Field descriptor. */
  305. export interface IField {
  306. /** Field rule */
  307. rule?: string;
  308. /** Field type */
  309. type: string;
  310. /** Field id */
  311. id: number;
  312. /** Field options */
  313. options?: { [k: string]: any };
  314. }
  315. /** Extension field descriptor. */
  316. export interface IExtensionField extends IField {
  317. /** Extended type */
  318. extend: string;
  319. }
  320. /**
  321. * Decorator function as returned by {@link Field.d} and {@link MapField.d} (TypeScript).
  322. * @param prototype Target prototype
  323. * @param fieldName Field name
  324. */
  325. type FieldDecorator = (prototype: object, fieldName: string) => void;
  326. /**
  327. * A node-style callback as used by {@link load} and {@link Root#load}.
  328. * @param error Error, if any, otherwise `null`
  329. * @param [root] Root, if there hasn't been an error
  330. */
  331. type LoadCallback = (error: (Error|null), root?: Root) => void;
  332. /**
  333. * Loads one or multiple .proto or preprocessed .json files into a common root namespace and calls the callback.
  334. * @param filename One or multiple files to load
  335. * @param root Root namespace, defaults to create a new one if omitted.
  336. * @param callback Callback function
  337. * @see {@link Root#load}
  338. */
  339. export function load(filename: (string|string[]), root: Root, callback: LoadCallback): void;
  340. /**
  341. * Loads one or multiple .proto or preprocessed .json files into a common root namespace and calls the callback.
  342. * @param filename One or multiple files to load
  343. * @param callback Callback function
  344. * @see {@link Root#load}
  345. */
  346. export function load(filename: (string|string[]), callback: LoadCallback): void;
  347. /**
  348. * Loads one or multiple .proto or preprocessed .json files into a common root namespace and returns a promise.
  349. * @param filename One or multiple files to load
  350. * @param [root] Root namespace, defaults to create a new one if omitted.
  351. * @returns Promise
  352. * @see {@link Root#load}
  353. */
  354. export function load(filename: (string|string[]), root?: Root): Promise<Root>;
  355. /**
  356. * Synchronously loads one or multiple .proto or preprocessed .json files into a common root namespace (node only).
  357. * @param filename One or multiple files to load
  358. * @param [root] Root namespace, defaults to create a new one if omitted.
  359. * @returns Root namespace
  360. * @throws {Error} If synchronous fetching is not supported (i.e. in browsers) or if a file's syntax is invalid
  361. * @see {@link Root#loadSync}
  362. */
  363. export function loadSync(filename: (string|string[]), root?: Root): Root;
  364. /** Build type, one of `"full"`, `"light"` or `"minimal"`. */
  365. export const build: string;
  366. /** Reconfigures the library according to the environment. */
  367. export function configure(): void;
  368. /** Reflected map field. */
  369. export class MapField extends FieldBase {
  370. /**
  371. * Constructs a new map field instance.
  372. * @param name Unique name within its namespace
  373. * @param id Unique id within its namespace
  374. * @param keyType Key type
  375. * @param type Value type
  376. * @param [options] Declared options
  377. * @param [comment] Comment associated with this field
  378. */
  379. constructor(name: string, id: number, keyType: string, type: string, options?: { [k: string]: any }, comment?: string);
  380. /** Key type. */
  381. public keyType: string;
  382. /** Resolved key type if not a basic type. */
  383. public resolvedKeyType: (ReflectionObject|null);
  384. /**
  385. * Constructs a map field from a map field descriptor.
  386. * @param name Field name
  387. * @param json Map field descriptor
  388. * @returns Created map field
  389. * @throws {TypeError} If arguments are invalid
  390. */
  391. public static fromJSON(name: string, json: IMapField): MapField;
  392. /**
  393. * Converts this map field to a map field descriptor.
  394. * @param [toJSONOptions] JSON conversion options
  395. * @returns Map field descriptor
  396. */
  397. public toJSON(toJSONOptions?: IToJSONOptions): IMapField;
  398. /**
  399. * Map field decorator (TypeScript).
  400. * @param fieldId Field id
  401. * @param fieldKeyType Field key type
  402. * @param fieldValueType Field value type
  403. * @returns Decorator function
  404. */
  405. public static d<T extends { [key: string]: number | Long | string | boolean | Uint8Array | Buffer | number[] | Message<{}> }>(fieldId: number, fieldKeyType: ("int32"|"uint32"|"sint32"|"fixed32"|"sfixed32"|"int64"|"uint64"|"sint64"|"fixed64"|"sfixed64"|"bool"|"string"), fieldValueType: ("double"|"float"|"int32"|"uint32"|"sint32"|"fixed32"|"sfixed32"|"int64"|"uint64"|"sint64"|"fixed64"|"sfixed64"|"bool"|"string"|"bytes"|object|Constructor<{}>)): FieldDecorator;
  406. }
  407. /** Map field descriptor. */
  408. export interface IMapField extends IField {
  409. /** Key type */
  410. keyType: string;
  411. }
  412. /** Extension map field descriptor. */
  413. export interface IExtensionMapField extends IMapField {
  414. /** Extended type */
  415. extend: string;
  416. }
  417. /** Abstract runtime message. */
  418. export class Message<T extends object = object> {
  419. /**
  420. * Constructs a new message instance.
  421. * @param [properties] Properties to set
  422. */
  423. constructor(properties?: Properties<T>);
  424. /** Reference to the reflected type. */
  425. public static readonly $type: Type;
  426. /** Reference to the reflected type. */
  427. public readonly $type: Type;
  428. /**
  429. * Creates a new message of this type using the specified properties.
  430. * @param [properties] Properties to set
  431. * @returns Message instance
  432. */
  433. public static create<T extends Message<T>>(this: Constructor<T>, properties?: { [k: string]: any }): Message<T>;
  434. /**
  435. * Encodes a message of this type.
  436. * @param message Message to encode
  437. * @param [writer] Writer to use
  438. * @returns Writer
  439. */
  440. public static encode<T extends Message<T>>(this: Constructor<T>, message: (T|{ [k: string]: any }), writer?: Writer): Writer;
  441. /**
  442. * Encodes a message of this type preceeded by its length as a varint.
  443. * @param message Message to encode
  444. * @param [writer] Writer to use
  445. * @returns Writer
  446. */
  447. public static encodeDelimited<T extends Message<T>>(this: Constructor<T>, message: (T|{ [k: string]: any }), writer?: Writer): Writer;
  448. /**
  449. * Decodes a message of this type.
  450. * @param reader Reader or buffer to decode
  451. * @returns Decoded message
  452. */
  453. public static decode<T extends Message<T>>(this: Constructor<T>, reader: (Reader|Uint8Array)): T;
  454. /**
  455. * Decodes a message of this type preceeded by its length as a varint.
  456. * @param reader Reader or buffer to decode
  457. * @returns Decoded message
  458. */
  459. public static decodeDelimited<T extends Message<T>>(this: Constructor<T>, reader: (Reader|Uint8Array)): T;
  460. /**
  461. * Verifies a message of this type.
  462. * @param message Plain object to verify
  463. * @returns `null` if valid, otherwise the reason why it is not
  464. */
  465. public static verify(message: { [k: string]: any }): (string|null);
  466. /**
  467. * Creates a new message of this type from a plain object. Also converts values to their respective internal types.
  468. * @param object Plain object
  469. * @returns Message instance
  470. */
  471. public static fromObject<T extends Message<T>>(this: Constructor<T>, object: { [k: string]: any }): T;
  472. /**
  473. * Creates a plain object from a message of this type. Also converts values to other types if specified.
  474. * @param message Message instance
  475. * @param [options] Conversion options
  476. * @returns Plain object
  477. */
  478. public static toObject<T extends Message<T>>(this: Constructor<T>, message: T, options?: IConversionOptions): { [k: string]: any };
  479. /**
  480. * Converts this message to JSON.
  481. * @returns JSON object
  482. */
  483. public toJSON(): { [k: string]: any };
  484. }
  485. /** Reflected service method. */
  486. export class Method extends ReflectionObject {
  487. /**
  488. * Constructs a new service method instance.
  489. * @param name Method name
  490. * @param type Method type, usually `"rpc"`
  491. * @param requestType Request message type
  492. * @param responseType Response message type
  493. * @param [requestStream] Whether the request is streamed
  494. * @param [responseStream] Whether the response is streamed
  495. * @param [options] Declared options
  496. * @param [comment] The comment for this method
  497. * @param [parsedOptions] Declared options, properly parsed into an object
  498. */
  499. constructor(name: string, type: (string|undefined), requestType: string, responseType: string, requestStream?: (boolean|{ [k: string]: any }), responseStream?: (boolean|{ [k: string]: any }), options?: { [k: string]: any }, comment?: string, parsedOptions?: { [k: string]: any });
  500. /** Method type. */
  501. public type: string;
  502. /** Request type. */
  503. public requestType: string;
  504. /** Whether requests are streamed or not. */
  505. public requestStream?: boolean;
  506. /** Response type. */
  507. public responseType: string;
  508. /** Whether responses are streamed or not. */
  509. public responseStream?: boolean;
  510. /** Resolved request type. */
  511. public resolvedRequestType: (Type|null);
  512. /** Resolved response type. */
  513. public resolvedResponseType: (Type|null);
  514. /** Comment for this method */
  515. public comment: (string|null);
  516. /** Options properly parsed into an object */
  517. public parsedOptions: any;
  518. /**
  519. * Constructs a method from a method descriptor.
  520. * @param name Method name
  521. * @param json Method descriptor
  522. * @returns Created method
  523. * @throws {TypeError} If arguments are invalid
  524. */
  525. public static fromJSON(name: string, json: IMethod): Method;
  526. /**
  527. * Converts this method to a method descriptor.
  528. * @param [toJSONOptions] JSON conversion options
  529. * @returns Method descriptor
  530. */
  531. public toJSON(toJSONOptions?: IToJSONOptions): IMethod;
  532. }
  533. /** Method descriptor. */
  534. export interface IMethod {
  535. /** Method type */
  536. type?: string;
  537. /** Request type */
  538. requestType: string;
  539. /** Response type */
  540. responseType: string;
  541. /** Whether requests are streamed */
  542. requestStream?: boolean;
  543. /** Whether responses are streamed */
  544. responseStream?: boolean;
  545. /** Method options */
  546. options?: { [k: string]: any };
  547. /** Method comments */
  548. comment: string;
  549. /** Method options properly parsed into an object */
  550. parsedOptions?: { [k: string]: any };
  551. }
  552. /** Reflected namespace. */
  553. export class Namespace extends NamespaceBase {
  554. /**
  555. * Constructs a new namespace instance.
  556. * @param name Namespace name
  557. * @param [options] Declared options
  558. */
  559. constructor(name: string, options?: { [k: string]: any });
  560. /**
  561. * Constructs a namespace from JSON.
  562. * @param name Namespace name
  563. * @param json JSON object
  564. * @returns Created namespace
  565. * @throws {TypeError} If arguments are invalid
  566. */
  567. public static fromJSON(name: string, json: { [k: string]: any }): Namespace;
  568. /**
  569. * Converts an array of reflection objects to JSON.
  570. * @param array Object array
  571. * @param [toJSONOptions] JSON conversion options
  572. * @returns JSON object or `undefined` when array is empty
  573. */
  574. public static arrayToJSON(array: ReflectionObject[], toJSONOptions?: IToJSONOptions): ({ [k: string]: any }|undefined);
  575. /**
  576. * Tests if the specified id is reserved.
  577. * @param reserved Array of reserved ranges and names
  578. * @param id Id to test
  579. * @returns `true` if reserved, otherwise `false`
  580. */
  581. public static isReservedId(reserved: ((number[]|string)[]|undefined), id: number): boolean;
  582. /**
  583. * Tests if the specified name is reserved.
  584. * @param reserved Array of reserved ranges and names
  585. * @param name Name to test
  586. * @returns `true` if reserved, otherwise `false`
  587. */
  588. public static isReservedName(reserved: ((number[]|string)[]|undefined), name: string): boolean;
  589. }
  590. /** Base class of all reflection objects containing nested objects. This is not an actual class but here for the sake of having consistent type definitions. */
  591. export abstract class NamespaceBase extends ReflectionObject {
  592. /** Nested objects by name. */
  593. public nested?: { [k: string]: ReflectionObject };
  594. /** Nested objects of this namespace as an array for iteration. */
  595. public readonly nestedArray: ReflectionObject[];
  596. /**
  597. * Converts this namespace to a namespace descriptor.
  598. * @param [toJSONOptions] JSON conversion options
  599. * @returns Namespace descriptor
  600. */
  601. public toJSON(toJSONOptions?: IToJSONOptions): INamespace;
  602. /**
  603. * Adds nested objects to this namespace from nested object descriptors.
  604. * @param nestedJson Any nested object descriptors
  605. * @returns `this`
  606. */
  607. public addJSON(nestedJson: { [k: string]: AnyNestedObject }): Namespace;
  608. /**
  609. * Gets the nested object of the specified name.
  610. * @param name Nested object name
  611. * @returns The reflection object or `null` if it doesn't exist
  612. */
  613. public get(name: string): (ReflectionObject|null);
  614. /**
  615. * Gets the values of the nested {@link Enum|enum} of the specified name.
  616. * This methods differs from {@link Namespace#get|get} in that it returns an enum's values directly and throws instead of returning `null`.
  617. * @param name Nested enum name
  618. * @returns Enum values
  619. * @throws {Error} If there is no such enum
  620. */
  621. public getEnum(name: string): { [k: string]: number };
  622. /**
  623. * Adds a nested object to this namespace.
  624. * @param object Nested object to add
  625. * @returns `this`
  626. * @throws {TypeError} If arguments are invalid
  627. * @throws {Error} If there is already a nested object with this name
  628. */
  629. public add(object: ReflectionObject): Namespace;
  630. /**
  631. * Removes a nested object from this namespace.
  632. * @param object Nested object to remove
  633. * @returns `this`
  634. * @throws {TypeError} If arguments are invalid
  635. * @throws {Error} If `object` is not a member of this namespace
  636. */
  637. public remove(object: ReflectionObject): Namespace;
  638. /**
  639. * Defines additial namespaces within this one if not yet existing.
  640. * @param path Path to create
  641. * @param [json] Nested types to create from JSON
  642. * @returns Pointer to the last namespace created or `this` if path is empty
  643. */
  644. public define(path: (string|string[]), json?: any): Namespace;
  645. /**
  646. * Resolves this namespace's and all its nested objects' type references. Useful to validate a reflection tree, but comes at a cost.
  647. * @returns `this`
  648. */
  649. public resolveAll(): Namespace;
  650. /**
  651. * Recursively looks up the reflection object matching the specified path in the scope of this namespace.
  652. * @param path Path to look up
  653. * @param filterTypes Filter types, any combination of the constructors of `protobuf.Type`, `protobuf.Enum`, `protobuf.Service` etc.
  654. * @param [parentAlreadyChecked=false] If known, whether the parent has already been checked
  655. * @returns Looked up object or `null` if none could be found
  656. */
  657. public lookup(path: (string|string[]), filterTypes: (any|any[]), parentAlreadyChecked?: boolean): (ReflectionObject|null);
  658. /**
  659. * Looks up the reflection object at the specified path, relative to this namespace.
  660. * @param path Path to look up
  661. * @param [parentAlreadyChecked=false] Whether the parent has already been checked
  662. * @returns Looked up object or `null` if none could be found
  663. */
  664. public lookup(path: (string|string[]), parentAlreadyChecked?: boolean): (ReflectionObject|null);
  665. /**
  666. * Looks up the {@link Type|type} at the specified path, relative to this namespace.
  667. * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
  668. * @param path Path to look up
  669. * @returns Looked up type
  670. * @throws {Error} If `path` does not point to a type
  671. */
  672. public lookupType(path: (string|string[])): Type;
  673. /**
  674. * Looks up the values of the {@link Enum|enum} at the specified path, relative to this namespace.
  675. * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
  676. * @param path Path to look up
  677. * @returns Looked up enum
  678. * @throws {Error} If `path` does not point to an enum
  679. */
  680. public lookupEnum(path: (string|string[])): Enum;
  681. /**
  682. * Looks up the {@link Type|type} or {@link Enum|enum} at the specified path, relative to this namespace.
  683. * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
  684. * @param path Path to look up
  685. * @returns Looked up type or enum
  686. * @throws {Error} If `path` does not point to a type or enum
  687. */
  688. public lookupTypeOrEnum(path: (string|string[])): Type;
  689. /**
  690. * Looks up the {@link Service|service} at the specified path, relative to this namespace.
  691. * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
  692. * @param path Path to look up
  693. * @returns Looked up service
  694. * @throws {Error} If `path` does not point to a service
  695. */
  696. public lookupService(path: (string|string[])): Service;
  697. }
  698. /** Namespace descriptor. */
  699. export interface INamespace {
  700. /** Namespace options */
  701. options?: { [k: string]: any };
  702. /** Nested object descriptors */
  703. nested?: { [k: string]: AnyNestedObject };
  704. }
  705. /** Any extension field descriptor. */
  706. type AnyExtensionField = (IExtensionField|IExtensionMapField);
  707. /** Any nested object descriptor. */
  708. type AnyNestedObject = (IEnum|IType|IService|AnyExtensionField|INamespace|IOneOf);
  709. /** Base class of all reflection objects. */
  710. export abstract class ReflectionObject {
  711. /** Options. */
  712. public options?: { [k: string]: any };
  713. /** Parsed Options. */
  714. public parsedOptions?: { [k: string]: any[] };
  715. /** Unique name within its namespace. */
  716. public name: string;
  717. /** Parent namespace. */
  718. public parent: (Namespace|null);
  719. /** Whether already resolved or not. */
  720. public resolved: boolean;
  721. /** Comment text, if any. */
  722. public comment: (string|null);
  723. /** Defining file name. */
  724. public filename: (string|null);
  725. /** Reference to the root namespace. */
  726. public readonly root: Root;
  727. /** Full name including leading dot. */
  728. public readonly fullName: string;
  729. /**
  730. * Converts this reflection object to its descriptor representation.
  731. * @returns Descriptor
  732. */
  733. public toJSON(): { [k: string]: any };
  734. /**
  735. * Called when this object is added to a parent.
  736. * @param parent Parent added to
  737. */
  738. public onAdd(parent: ReflectionObject): void;
  739. /**
  740. * Called when this object is removed from a parent.
  741. * @param parent Parent removed from
  742. */
  743. public onRemove(parent: ReflectionObject): void;
  744. /**
  745. * Resolves this objects type references.
  746. * @returns `this`
  747. */
  748. public resolve(): ReflectionObject;
  749. /**
  750. * Gets an option value.
  751. * @param name Option name
  752. * @returns Option value or `undefined` if not set
  753. */
  754. public getOption(name: string): any;
  755. /**
  756. * Sets an option.
  757. * @param name Option name
  758. * @param value Option value
  759. * @param [ifNotSet] Sets the option only if it isn't currently set
  760. * @returns `this`
  761. */
  762. public setOption(name: string, value: any, ifNotSet?: boolean): ReflectionObject;
  763. /**
  764. * Sets a parsed option.
  765. * @param name parsed Option name
  766. * @param value Option value
  767. * @param propName dot '.' delimited full path of property within the option to set. if undefined\empty, will add a new option with that value
  768. * @returns `this`
  769. */
  770. public setParsedOption(name: string, value: any, propName: string): ReflectionObject;
  771. /**
  772. * Sets multiple options.
  773. * @param options Options to set
  774. * @param [ifNotSet] Sets an option only if it isn't currently set
  775. * @returns `this`
  776. */
  777. public setOptions(options: { [k: string]: any }, ifNotSet?: boolean): ReflectionObject;
  778. /**
  779. * Converts this instance to its string representation.
  780. * @returns Class name[, space, full name]
  781. */
  782. public toString(): string;
  783. }
  784. /** Reflected oneof. */
  785. export class OneOf extends ReflectionObject {
  786. /**
  787. * Constructs a new oneof instance.
  788. * @param name Oneof name
  789. * @param [fieldNames] Field names
  790. * @param [options] Declared options
  791. * @param [comment] Comment associated with this field
  792. */
  793. constructor(name: string, fieldNames?: (string[]|{ [k: string]: any }), options?: { [k: string]: any }, comment?: string);
  794. /** Field names that belong to this oneof. */
  795. public oneof: string[];
  796. /** Fields that belong to this oneof as an array for iteration. */
  797. public readonly fieldsArray: Field[];
  798. /** Comment for this field. */
  799. public comment: (string|null);
  800. /**
  801. * Constructs a oneof from a oneof descriptor.
  802. * @param name Oneof name
  803. * @param json Oneof descriptor
  804. * @returns Created oneof
  805. * @throws {TypeError} If arguments are invalid
  806. */
  807. public static fromJSON(name: string, json: IOneOf): OneOf;
  808. /**
  809. * Converts this oneof to a oneof descriptor.
  810. * @param [toJSONOptions] JSON conversion options
  811. * @returns Oneof descriptor
  812. */
  813. public toJSON(toJSONOptions?: IToJSONOptions): IOneOf;
  814. /**
  815. * Adds a field to this oneof and removes it from its current parent, if any.
  816. * @param field Field to add
  817. * @returns `this`
  818. */
  819. public add(field: Field): OneOf;
  820. /**
  821. * Removes a field from this oneof and puts it back to the oneof's parent.
  822. * @param field Field to remove
  823. * @returns `this`
  824. */
  825. public remove(field: Field): OneOf;
  826. /**
  827. * OneOf decorator (TypeScript).
  828. * @param fieldNames Field names
  829. * @returns Decorator function
  830. */
  831. public static d<T extends string>(...fieldNames: string[]): OneOfDecorator;
  832. }
  833. /** Oneof descriptor. */
  834. export interface IOneOf {
  835. /** Oneof field names */
  836. oneof: string[];
  837. /** Oneof options */
  838. options?: { [k: string]: any };
  839. }
  840. /**
  841. * Decorator function as returned by {@link OneOf.d} (TypeScript).
  842. * @param prototype Target prototype
  843. * @param oneofName OneOf name
  844. */
  845. type OneOfDecorator = (prototype: object, oneofName: string) => void;
  846. /**
  847. * Parses the given .proto source and returns an object with the parsed contents.
  848. * @param source Source contents
  849. * @param [options] Parse options. Defaults to {@link parse.defaults} when omitted.
  850. * @returns Parser result
  851. */
  852. export function parse(source: string, options?: IParseOptions): IParserResult;
  853. /** Result object returned from {@link parse}. */
  854. export interface IParserResult {
  855. /** Package name, if declared */
  856. package: (string|undefined);
  857. /** Imports, if any */
  858. imports: (string[]|undefined);
  859. /** Weak imports, if any */
  860. weakImports: (string[]|undefined);
  861. /** Syntax, if specified (either `"proto2"` or `"proto3"`) */
  862. syntax: (string|undefined);
  863. /** Populated root instance */
  864. root: Root;
  865. }
  866. /** Options modifying the behavior of {@link parse}. */
  867. export interface IParseOptions {
  868. /** Keeps field casing instead of converting to camel case */
  869. keepCase?: boolean;
  870. /** Recognize double-slash comments in addition to doc-block comments. */
  871. alternateCommentMode?: boolean;
  872. /** Use trailing comment when both leading comment and trailing comment exist. */
  873. preferTrailingComment?: boolean;
  874. }
  875. /** Options modifying the behavior of JSON serialization. */
  876. export interface IToJSONOptions {
  877. /** Serializes comments. */
  878. keepComments?: boolean;
  879. }
  880. /**
  881. * Parses the given .proto source and returns an object with the parsed contents.
  882. * @param source Source contents
  883. * @param root Root to populate
  884. * @param [options] Parse options. Defaults to {@link parse.defaults} when omitted.
  885. * @returns Parser result
  886. */
  887. export function parse(source: string, root: Root, options?: IParseOptions): IParserResult;
  888. /** Wire format reader using `Uint8Array` if available, otherwise `Array`. */
  889. export class Reader {
  890. /**
  891. * Constructs a new reader instance using the specified buffer.
  892. * @param buffer Buffer to read from
  893. */
  894. constructor(buffer: Uint8Array);
  895. /** Read buffer. */
  896. public buf: Uint8Array;
  897. /** Read buffer position. */
  898. public pos: number;
  899. /** Read buffer length. */
  900. public len: number;
  901. /**
  902. * Creates a new reader using the specified buffer.
  903. * @param buffer Buffer to read from
  904. * @returns A {@link BufferReader} if `buffer` is a Buffer, otherwise a {@link Reader}
  905. * @throws {Error} If `buffer` is not a valid buffer
  906. */
  907. public static create(buffer: (Uint8Array|Buffer)): (Reader|BufferReader);
  908. /**
  909. * Reads a varint as an unsigned 32 bit value.
  910. * @returns Value read
  911. */
  912. public uint32(): number;
  913. /**
  914. * Reads a varint as a signed 32 bit value.
  915. * @returns Value read
  916. */
  917. public int32(): number;
  918. /**
  919. * Reads a zig-zag encoded varint as a signed 32 bit value.
  920. * @returns Value read
  921. */
  922. public sint32(): number;
  923. /**
  924. * Reads a varint as a signed 64 bit value.
  925. * @returns Value read
  926. */
  927. public int64(): Long;
  928. /**
  929. * Reads a varint as an unsigned 64 bit value.
  930. * @returns Value read
  931. */
  932. public uint64(): Long;
  933. /**
  934. * Reads a zig-zag encoded varint as a signed 64 bit value.
  935. * @returns Value read
  936. */
  937. public sint64(): Long;
  938. /**
  939. * Reads a varint as a boolean.
  940. * @returns Value read
  941. */
  942. public bool(): boolean;
  943. /**
  944. * Reads fixed 32 bits as an unsigned 32 bit integer.
  945. * @returns Value read
  946. */
  947. public fixed32(): number;
  948. /**
  949. * Reads fixed 32 bits as a signed 32 bit integer.
  950. * @returns Value read
  951. */
  952. public sfixed32(): number;
  953. /**
  954. * Reads fixed 64 bits.
  955. * @returns Value read
  956. */
  957. public fixed64(): Long;
  958. /**
  959. * Reads zig-zag encoded fixed 64 bits.
  960. * @returns Value read
  961. */
  962. public sfixed64(): Long;
  963. /**
  964. * Reads a float (32 bit) as a number.
  965. * @returns Value read
  966. */
  967. public float(): number;
  968. /**
  969. * Reads a double (64 bit float) as a number.
  970. * @returns Value read
  971. */
  972. public double(): number;
  973. /**
  974. * Reads a sequence of bytes preceeded by its length as a varint.
  975. * @returns Value read
  976. */
  977. public bytes(): Uint8Array;
  978. /**
  979. * Reads a string preceeded by its byte length as a varint.
  980. * @returns Value read
  981. */
  982. public string(): string;
  983. /**
  984. * Skips the specified number of bytes if specified, otherwise skips a varint.
  985. * @param [length] Length if known, otherwise a varint is assumed
  986. * @returns `this`
  987. */
  988. public skip(length?: number): Reader;
  989. /**
  990. * Skips the next element of the specified wire type.
  991. * @param wireType Wire type received
  992. * @returns `this`
  993. */
  994. public skipType(wireType: number): Reader;
  995. }
  996. /** Wire format reader using node buffers. */
  997. export class BufferReader extends Reader {
  998. /**
  999. * Constructs a new buffer reader instance.
  1000. * @param buffer Buffer to read from
  1001. */
  1002. constructor(buffer: Buffer);
  1003. /**
  1004. * Reads a sequence of bytes preceeded by its length as a varint.
  1005. * @returns Value read
  1006. */
  1007. public bytes(): Buffer;
  1008. }
  1009. /** Root namespace wrapping all types, enums, services, sub-namespaces etc. that belong together. */
  1010. export class Root extends NamespaceBase {
  1011. /**
  1012. * Constructs a new root namespace instance.
  1013. * @param [options] Top level options
  1014. */
  1015. constructor(options?: { [k: string]: any });
  1016. /** Deferred extension fields. */
  1017. public deferred: Field[];
  1018. /** Resolved file names of loaded files. */
  1019. public files: string[];
  1020. /**
  1021. * Loads a namespace descriptor into a root namespace.
  1022. * @param json Nameespace descriptor
  1023. * @param [root] Root namespace, defaults to create a new one if omitted
  1024. * @returns Root namespace
  1025. */
  1026. public static fromJSON(json: INamespace, root?: Root): Root;
  1027. /**
  1028. * Resolves the path of an imported file, relative to the importing origin.
  1029. * This method exists so you can override it with your own logic in case your imports are scattered over multiple directories.
  1030. * @param origin The file name of the importing file
  1031. * @param target The file name being imported
  1032. * @returns Resolved path to `target` or `null` to skip the file
  1033. */
  1034. public resolvePath(origin: string, target: string): (string|null);
  1035. /**
  1036. * Fetch content from file path or url
  1037. * This method exists so you can override it with your own logic.
  1038. * @param path File path or url
  1039. * @param callback Callback function
  1040. */
  1041. public fetch(path: string, callback: FetchCallback): void;
  1042. /**
  1043. * Loads one or multiple .proto or preprocessed .json files into this root namespace and calls the callback.
  1044. * @param filename Names of one or multiple files to load
  1045. * @param options Parse options
  1046. * @param callback Callback function
  1047. */
  1048. public load(filename: (string|string[]), options: IParseOptions, callback: LoadCallback): void;
  1049. /**
  1050. * Loads one or multiple .proto or preprocessed .json files into this root namespace and calls the callback.
  1051. * @param filename Names of one or multiple files to load
  1052. * @param callback Callback function
  1053. */
  1054. public load(filename: (string|string[]), callback: LoadCallback): void;
  1055. /**
  1056. * Loads one or multiple .proto or preprocessed .json files into this root namespace and returns a promise.
  1057. * @param filename Names of one or multiple files to load
  1058. * @param [options] Parse options. Defaults to {@link parse.defaults} when omitted.
  1059. * @returns Promise
  1060. */
  1061. public load(filename: (string|string[]), options?: IParseOptions): Promise<Root>;
  1062. /**
  1063. * Synchronously loads one or multiple .proto or preprocessed .json files into this root namespace (node only).
  1064. * @param filename Names of one or multiple files to load
  1065. * @param [options] Parse options. Defaults to {@link parse.defaults} when omitted.
  1066. * @returns Root namespace
  1067. * @throws {Error} If synchronous fetching is not supported (i.e. in browsers) or if a file's syntax is invalid
  1068. */
  1069. public loadSync(filename: (string|string[]), options?: IParseOptions): Root;
  1070. }
  1071. /**
  1072. * Named roots.
  1073. * This is where pbjs stores generated structures (the option `-r, --root` specifies a name).
  1074. * Can also be used manually to make roots available across modules.
  1075. */
  1076. export let roots: { [k: string]: Root };
  1077. /** Streaming RPC helpers. */
  1078. export namespace rpc {
  1079. /**
  1080. * A service method callback as used by {@link rpc.ServiceMethod|ServiceMethod}.
  1081. *
  1082. * Differs from {@link RPCImplCallback} in that it is an actual callback of a service method which may not return `response = null`.
  1083. * @param error Error, if any
  1084. * @param [response] Response message
  1085. */
  1086. type ServiceMethodCallback<TRes extends Message<TRes>> = (error: (Error|null), response?: TRes) => void;
  1087. /**
  1088. * A service method part of a {@link rpc.Service} as created by {@link Service.create}.
  1089. * @param request Request message or plain object
  1090. * @param [callback] Node-style callback called with the error, if any, and the response message
  1091. * @returns Promise if `callback` has been omitted, otherwise `undefined`
  1092. */
  1093. type ServiceMethod<TReq extends Message<TReq>, TRes extends Message<TRes>> = (request: (TReq|Properties<TReq>), callback?: rpc.ServiceMethodCallback<TRes>) => Promise<Message<TRes>>;
  1094. /** An RPC service as returned by {@link Service#create}. */
  1095. class Service extends util.EventEmitter {
  1096. /**
  1097. * Constructs a new RPC service instance.
  1098. * @param rpcImpl RPC implementation
  1099. * @param [requestDelimited=false] Whether requests are length-delimited
  1100. * @param [responseDelimited=false] Whether responses are length-delimited
  1101. */
  1102. constructor(rpcImpl: RPCImpl, requestDelimited?: boolean, responseDelimited?: boolean);
  1103. /** RPC implementation. Becomes `null` once the service is ended. */
  1104. public rpcImpl: (RPCImpl|null);
  1105. /** Whether requests are length-delimited. */
  1106. public requestDelimited: boolean;
  1107. /** Whether responses are length-delimited. */
  1108. public responseDelimited: boolean;
  1109. /**
  1110. * Calls a service method through {@link rpc.Service#rpcImpl|rpcImpl}.
  1111. * @param method Reflected or static method
  1112. * @param requestCtor Request constructor
  1113. * @param responseCtor Response constructor
  1114. * @param request Request message or plain object
  1115. * @param callback Service callback
  1116. */
  1117. public rpcCall<TReq extends Message<TReq>, TRes extends Message<TRes>>(method: (Method|rpc.ServiceMethod<TReq, TRes>), requestCtor: Constructor<TReq>, responseCtor: Constructor<TRes>, request: (TReq|Properties<TReq>), callback: rpc.ServiceMethodCallback<TRes>): void;
  1118. /**
  1119. * Ends this service and emits the `end` event.
  1120. * @param [endedByRPC=false] Whether the service has been ended by the RPC implementation.
  1121. * @returns `this`
  1122. */
  1123. public end(endedByRPC?: boolean): rpc.Service;
  1124. }
  1125. }
  1126. /**
  1127. * RPC implementation passed to {@link Service#create} performing a service request on network level, i.e. by utilizing http requests or websockets.
  1128. * @param method Reflected or static method being called
  1129. * @param requestData Request data
  1130. * @param callback Callback function
  1131. */
  1132. type RPCImpl = (method: (Method|rpc.ServiceMethod<Message<{}>, Message<{}>>), requestData: Uint8Array, callback: RPCImplCallback) => void;
  1133. /**
  1134. * Node-style callback as used by {@link RPCImpl}.
  1135. * @param error Error, if any, otherwise `null`
  1136. * @param [response] Response data or `null` to signal end of stream, if there hasn't been an error
  1137. */
  1138. type RPCImplCallback = (error: (Error|null), response?: (Uint8Array|null)) => void;
  1139. /** Reflected service. */
  1140. export class Service extends NamespaceBase {
  1141. /**
  1142. * Constructs a new service instance.
  1143. * @param name Service name
  1144. * @param [options] Service options
  1145. * @throws {TypeError} If arguments are invalid
  1146. */
  1147. constructor(name: string, options?: { [k: string]: any });
  1148. /** Service methods. */
  1149. public methods: { [k: string]: Method };
  1150. /**
  1151. * Constructs a service from a service descriptor.
  1152. * @param name Service name
  1153. * @param json Service descriptor
  1154. * @returns Created service
  1155. * @throws {TypeError} If arguments are invalid
  1156. */
  1157. public static fromJSON(name: string, json: IService): Service;
  1158. /**
  1159. * Converts this service to a service descriptor.
  1160. * @param [toJSONOptions] JSON conversion options
  1161. * @returns Service descriptor
  1162. */
  1163. public toJSON(toJSONOptions?: IToJSONOptions): IService;
  1164. /** Methods of this service as an array for iteration. */
  1165. public readonly methodsArray: Method[];
  1166. /**
  1167. * Creates a runtime service using the specified rpc implementation.
  1168. * @param rpcImpl RPC implementation
  1169. * @param [requestDelimited=false] Whether requests are length-delimited
  1170. * @param [responseDelimited=false] Whether responses are length-delimited
  1171. * @returns RPC service. Useful where requests and/or responses are streamed.
  1172. */
  1173. public create(rpcImpl: RPCImpl, requestDelimited?: boolean, responseDelimited?: boolean): rpc.Service;
  1174. }
  1175. /** Service descriptor. */
  1176. export interface IService extends INamespace {
  1177. /** Method descriptors */
  1178. methods: { [k: string]: IMethod };
  1179. }
  1180. /**
  1181. * Gets the next token and advances.
  1182. * @returns Next token or `null` on eof
  1183. */
  1184. type TokenizerHandleNext = () => (string|null);
  1185. /**
  1186. * Peeks for the next token.
  1187. * @returns Next token or `null` on eof
  1188. */
  1189. type TokenizerHandlePeek = () => (string|null);
  1190. /**
  1191. * Pushes a token back to the stack.
  1192. * @param token Token
  1193. */
  1194. type TokenizerHandlePush = (token: string) => void;
  1195. /**
  1196. * Skips the next token.
  1197. * @param expected Expected token
  1198. * @param [optional=false] If optional
  1199. * @returns Whether the token matched
  1200. * @throws {Error} If the token didn't match and is not optional
  1201. */
  1202. type TokenizerHandleSkip = (expected: string, optional?: boolean) => boolean;
  1203. /**
  1204. * Gets the comment on the previous line or, alternatively, the line comment on the specified line.
  1205. * @param [line] Line number
  1206. * @returns Comment text or `null` if none
  1207. */
  1208. type TokenizerHandleCmnt = (line?: number) => (string|null);
  1209. /** Handle object returned from {@link tokenize}. */
  1210. export interface ITokenizerHandle {
  1211. /** Gets the next token and advances (`null` on eof) */
  1212. next: TokenizerHandleNext;
  1213. /** Peeks for the next token (`null` on eof) */
  1214. peek: TokenizerHandlePeek;
  1215. /** Pushes a token back to the stack */
  1216. push: TokenizerHandlePush;
  1217. /** Skips a token, returns its presence and advances or, if non-optional and not present, throws */
  1218. skip: TokenizerHandleSkip;
  1219. /** Gets the comment on the previous line or the line comment on the specified line, if any */
  1220. cmnt: TokenizerHandleCmnt;
  1221. /** Current line number */
  1222. line: number;
  1223. }
  1224. /**
  1225. * Tokenizes the given .proto source and returns an object with useful utility functions.
  1226. * @param source Source contents
  1227. * @param alternateCommentMode Whether we should activate alternate comment parsing mode.
  1228. * @returns Tokenizer handle
  1229. */
  1230. export function tokenize(source: string, alternateCommentMode: boolean): ITokenizerHandle;
  1231. export namespace tokenize {
  1232. /**
  1233. * Unescapes a string.
  1234. * @param str String to unescape
  1235. * @returns Unescaped string
  1236. */
  1237. function unescape(str: string): string;
  1238. }
  1239. /** Reflected message type. */
  1240. export class Type extends NamespaceBase {
  1241. /**
  1242. * Constructs a new reflected message type instance.
  1243. * @param name Message name
  1244. * @param [options] Declared options
  1245. */
  1246. constructor(name: string, options?: { [k: string]: any });
  1247. /** Message fields. */
  1248. public fields: { [k: string]: Field };
  1249. /** Oneofs declared within this namespace, if any. */
  1250. public oneofs: { [k: string]: OneOf };
  1251. /** Extension ranges, if any. */
  1252. public extensions: number[][];
  1253. /** Reserved ranges, if any. */
  1254. public reserved: (number[]|string)[];
  1255. /** Message fields by id. */
  1256. public readonly fieldsById: { [k: number]: Field };
  1257. /** Fields of this message as an array for iteration. */
  1258. public readonly fieldsArray: Field[];
  1259. /** Oneofs of this message as an array for iteration. */
  1260. public readonly oneofsArray: OneOf[];
  1261. /**
  1262. * The registered constructor, if any registered, otherwise a generic constructor.
  1263. * Assigning a function replaces the internal constructor. If the function does not extend {@link Message} yet, its prototype will be setup accordingly and static methods will be populated. If it already extends {@link Message}, it will just replace the internal constructor.
  1264. */
  1265. public ctor: Constructor<{}>;
  1266. /**
  1267. * Generates a constructor function for the specified type.
  1268. * @param mtype Message type
  1269. * @returns Codegen instance
  1270. */
  1271. public static generateConstructor(mtype: Type): Codegen;
  1272. /**
  1273. * Creates a message type from a message type descriptor.
  1274. * @param name Message name
  1275. * @param json Message type descriptor
  1276. * @returns Created message type
  1277. */
  1278. public static fromJSON(name: string, json: IType): Type;
  1279. /**
  1280. * Converts this message type to a message type descriptor.
  1281. * @param [toJSONOptions] JSON conversion options
  1282. * @returns Message type descriptor
  1283. */
  1284. public toJSON(toJSONOptions?: IToJSONOptions): IType;
  1285. /**
  1286. * Adds a nested object to this type.
  1287. * @param object Nested object to add
  1288. * @returns `this`
  1289. * @throws {TypeError} If arguments are invalid
  1290. * @throws {Error} If there is already a nested object with this name or, if a field, when there is already a field with this id
  1291. */
  1292. public add(object: ReflectionObject): Type;
  1293. /**
  1294. * Removes a nested object from this type.
  1295. * @param object Nested object to remove
  1296. * @returns `this`
  1297. * @throws {TypeError} If arguments are invalid
  1298. * @throws {Error} If `object` is not a member of this type
  1299. */
  1300. public remove(object: ReflectionObject): Type;
  1301. /**
  1302. * Tests if the specified id is reserved.
  1303. * @param id Id to test
  1304. * @returns `true` if reserved, otherwise `false`
  1305. */
  1306. public isReservedId(id: number): boolean;
  1307. /**
  1308. * Tests if the specified name is reserved.
  1309. * @param name Name to test
  1310. * @returns `true` if reserved, otherwise `false`
  1311. */
  1312. public isReservedName(name: string): boolean;
  1313. /**
  1314. * Creates a new message of this type using the specified properties.
  1315. * @param [properties] Properties to set
  1316. * @returns Message instance
  1317. */
  1318. public create(properties?: { [k: string]: any }): Message<{}>;
  1319. /**
  1320. * Sets up {@link Type#encode|encode}, {@link Type#decode|decode} and {@link Type#verify|verify}.
  1321. * @returns `this`
  1322. */
  1323. public setup(): Type;
  1324. /**
  1325. * Encodes a message of this type. Does not implicitly {@link Type#verify|verify} messages.
  1326. * @param message Message instance or plain object
  1327. * @param [writer] Writer to encode to
  1328. * @returns writer
  1329. */
  1330. public encode(message: (Message<{}>|{ [k: string]: any }), writer?: Writer): Writer;
  1331. /**
  1332. * Encodes a message of this type preceeded by its byte length as a varint. Does not implicitly {@link Type#verify|verify} messages.
  1333. * @param message Message instance or plain object
  1334. * @param [writer] Writer to encode to
  1335. * @returns writer
  1336. */
  1337. public encodeDelimited(message: (Message<{}>|{ [k: string]: any }), writer?: Writer): Writer;
  1338. /**
  1339. * Decodes a message of this type.
  1340. * @param reader Reader or buffer to decode from
  1341. * @param [length] Length of the message, if known beforehand
  1342. * @returns Decoded message
  1343. * @throws {Error} If the payload is not a reader or valid buffer
  1344. * @throws {util.ProtocolError<{}>} If required fields are missing
  1345. */
  1346. public decode(reader: (Reader|Uint8Array), length?: number): Message<{}>;
  1347. /**
  1348. * Decodes a message of this type preceeded by its byte length as a varint.
  1349. * @param reader Reader or buffer to decode from
  1350. * @returns Decoded message
  1351. * @throws {Error} If the payload is not a reader or valid buffer
  1352. * @throws {util.ProtocolError} If required fields are missing
  1353. */
  1354. public decodeDelimited(reader: (Reader|Uint8Array)): Message<{}>;
  1355. /**
  1356. * Verifies that field values are valid and that required fields are present.
  1357. * @param message Plain object to verify
  1358. * @returns `null` if valid, otherwise the reason why it is not
  1359. */
  1360. public verify(message: { [k: string]: any }): (null|string);
  1361. /**
  1362. * Creates a new message of this type from a plain object. Also converts values to their respective internal types.
  1363. * @param object Plain object to convert
  1364. * @returns Message instance
  1365. */
  1366. public fromObject(object: { [k: string]: any }): Message<{}>;
  1367. /**
  1368. * Creates a plain object from a message of this type. Also converts values to other types if specified.
  1369. * @param message Message instance
  1370. * @param [options] Conversion options
  1371. * @returns Plain object
  1372. */
  1373. public toObject(message: Message<{}>, options?: IConversionOptions): { [k: string]: any };
  1374. /**
  1375. * Type decorator (TypeScript).
  1376. * @param [typeName] Type name, defaults to the constructor's name
  1377. * @returns Decorator function
  1378. */
  1379. public static d<T extends Message<T>>(typeName?: string): TypeDecorator<T>;
  1380. }
  1381. /** Message type descriptor. */
  1382. export interface IType extends INamespace {
  1383. /** Oneof descriptors */
  1384. oneofs?: { [k: string]: IOneOf };
  1385. /** Field descriptors */
  1386. fields: { [k: string]: IField };
  1387. /** Extension ranges */
  1388. extensions?: number[][];
  1389. /** Reserved ranges */
  1390. reserved?: (number[]|string)[];
  1391. /** Whether a legacy group or not */
  1392. group?: boolean;
  1393. }
  1394. /** Conversion options as used by {@link Type#toObject} and {@link Message.toObject}. */
  1395. export interface IConversionOptions {
  1396. /**
  1397. * Long conversion type.
  1398. * Valid values are `String` and `Number` (the global types).
  1399. * Defaults to copy the present value, which is a possibly unsafe number without and a {@link Long} with a long library.
  1400. */
  1401. longs?: Function;
  1402. /**
  1403. * Enum value conversion type.
  1404. * Only valid value is `String` (the global type).
  1405. * Defaults to copy the present value, which is the numeric id.
  1406. */
  1407. enums?: Function;
  1408. /**
  1409. * Bytes value conversion type.
  1410. * Valid values are `Array` and (a base64 encoded) `String` (the global types).
  1411. * Defaults to copy the present value, which usually is a Buffer under node and an Uint8Array in the browser.
  1412. */
  1413. bytes?: Function;
  1414. /** Also sets default values on the resulting object */
  1415. defaults?: boolean;
  1416. /** Sets empty arrays for missing repeated fields even if `defaults=false` */
  1417. arrays?: boolean;
  1418. /** Sets empty objects for missing map fields even if `defaults=false` */
  1419. objects?: boolean;
  1420. /** Includes virtual oneof properties set to the present field's name, if any */
  1421. oneofs?: boolean;
  1422. /** Performs additional JSON compatibility conversions, i.e. NaN and Infinity to strings */
  1423. json?: boolean;
  1424. }
  1425. /**
  1426. * Decorator function as returned by {@link Type.d} (TypeScript).
  1427. * @param target Target constructor
  1428. */
  1429. type TypeDecorator<T extends Message<T>> = (target: Constructor<T>) => void;
  1430. /** Common type constants. */
  1431. export namespace types {
  1432. /** Basic type wire types. */
  1433. const basic: {
  1434. "double": number,
  1435. "float": number,
  1436. "int32": number,
  1437. "uint32": number,
  1438. "sint32": number,
  1439. "fixed32": number,
  1440. "sfixed32": number,
  1441. "int64": number,
  1442. "uint64": number,
  1443. "sint64": number,
  1444. "fixed64": number,
  1445. "sfixed64": number,
  1446. "bool": number,
  1447. "string": number,
  1448. "bytes": number
  1449. };
  1450. /** Basic type defaults. */
  1451. const defaults: {
  1452. "double": number,
  1453. "float": number,
  1454. "int32": number,
  1455. "uint32": number,
  1456. "sint32": number,
  1457. "fixed32": number,
  1458. "sfixed32": number,
  1459. "int64": number,
  1460. "uint64": number,
  1461. "sint64": number,
  1462. "fixed64": number,
  1463. "sfixed64": number,
  1464. "bool": boolean,
  1465. "string": string,
  1466. "bytes": number[],
  1467. "message": null
  1468. };
  1469. /** Basic long type wire types. */
  1470. const long: {
  1471. "int64": number,
  1472. "uint64": number,
  1473. "sint64": number,
  1474. "fixed64": number,
  1475. "sfixed64": number
  1476. };
  1477. /** Allowed types for map keys with their associated wire type. */
  1478. const mapKey: {
  1479. "int32": number,
  1480. "uint32": number,
  1481. "sint32": number,
  1482. "fixed32": number,
  1483. "sfixed32": number,
  1484. "int64": number,
  1485. "uint64": number,
  1486. "sint64": number,
  1487. "fixed64": number,
  1488. "sfixed64": number,
  1489. "bool": number,
  1490. "string": number
  1491. };
  1492. /** Allowed types for packed repeated fields with their associated wire type. */
  1493. const packed: {
  1494. "double": number,
  1495. "float": number,
  1496. "int32": number,
  1497. "uint32": number,
  1498. "sint32": number,
  1499. "fixed32": number,
  1500. "sfixed32": number,
  1501. "int64": number,
  1502. "uint64": number,
  1503. "sint64": number,
  1504. "fixed64": number,
  1505. "sfixed64": number,
  1506. "bool": number
  1507. };
  1508. }
  1509. /** Constructor type. */
  1510. export interface Constructor<T> extends Function {
  1511. new(...params: any[]): T; prototype: T;
  1512. }
  1513. /** Properties type. */
  1514. type Properties<T> = { [P in keyof T]?: T[P] };
  1515. /**
  1516. * Any compatible Buffer instance.
  1517. * This is a minimal stand-alone definition of a Buffer instance. The actual type is that exported by node's typings.
  1518. */
  1519. export interface Buffer extends Uint8Array {
  1520. }
  1521. /**
  1522. * Any compatible Long instance.
  1523. * This is a minimal stand-alone definition of a Long instance. The actual type is that exported by long.js.
  1524. */
  1525. export interface Long {
  1526. /** Low bits */
  1527. low: number;
  1528. /** High bits */
  1529. high: number;
  1530. /** Whether unsigned or not */
  1531. unsigned: boolean;
  1532. }
  1533. /**
  1534. * A OneOf getter as returned by {@link util.oneOfGetter}.
  1535. * @returns Set field name, if any
  1536. */
  1537. type OneOfGetter = () => (string|undefined);
  1538. /**
  1539. * A OneOf setter as returned by {@link util.oneOfSetter}.
  1540. * @param value Field name
  1541. */
  1542. type OneOfSetter = (value: (string|undefined)) => void;
  1543. /** Various utility functions. */
  1544. export namespace util {
  1545. /** Helper class for working with the low and high bits of a 64 bit value. */
  1546. class LongBits {
  1547. /**
  1548. * Constructs new long bits.
  1549. * @param lo Low 32 bits, unsigned
  1550. * @param hi High 32 bits, unsigned
  1551. */
  1552. constructor(lo: number, hi: number);
  1553. /** Low bits. */
  1554. public lo: number;
  1555. /** High bits. */
  1556. public hi: number;
  1557. /** Zero bits. */
  1558. public static zero: util.LongBits;
  1559. /** Zero hash. */
  1560. public static zeroHash: string;
  1561. /**
  1562. * Constructs new long bits from the specified number.
  1563. * @param value Value
  1564. * @returns Instance
  1565. */
  1566. public static fromNumber(value: number): util.LongBits;
  1567. /**
  1568. * Constructs new long bits from a number, long or string.
  1569. * @param value Value
  1570. * @returns Instance
  1571. */
  1572. public static from(value: (Long|number|string)): util.LongBits;
  1573. /**
  1574. * Converts this long bits to a possibly unsafe JavaScript number.
  1575. * @param [unsigned=false] Whether unsigned or not
  1576. * @returns Possibly unsafe number
  1577. */
  1578. public toNumber(unsigned?: boolean): number;
  1579. /**
  1580. * Converts this long bits to a long.
  1581. * @param [unsigned=false] Whether unsigned or not
  1582. * @returns Long
  1583. */
  1584. public toLong(unsigned?: boolean): Long;
  1585. /**
  1586. * Constructs new long bits from the specified 8 characters long hash.
  1587. * @param hash Hash
  1588. * @returns Bits
  1589. */
  1590. public static fromHash(hash: string): util.LongBits;
  1591. /**
  1592. * Converts this long bits to a 8 characters long hash.
  1593. * @returns Hash
  1594. */
  1595. public toHash(): string;
  1596. /**
  1597. * Zig-zag encodes this long bits.
  1598. * @returns `this`
  1599. */
  1600. public zzEncode(): util.LongBits;
  1601. /**
  1602. * Zig-zag decodes this long bits.
  1603. * @returns `this`
  1604. */
  1605. public zzDecode(): util.LongBits;
  1606. /**
  1607. * Calculates the length of this longbits when encoded as a varint.
  1608. * @returns Length
  1609. */
  1610. public length(): number;
  1611. }
  1612. /** Whether running within node or not. */
  1613. let isNode: boolean;
  1614. /** Global object reference. */
  1615. let global: object;
  1616. /** An immuable empty array. */
  1617. const emptyArray: any[];
  1618. /** An immutable empty object. */
  1619. const emptyObject: object;
  1620. /**
  1621. * Tests if the specified value is an integer.
  1622. * @param value Value to test
  1623. * @returns `true` if the value is an integer
  1624. */
  1625. function isInteger(value: any): boolean;
  1626. /**
  1627. * Tests if the specified value is a string.
  1628. * @param value Value to test
  1629. * @returns `true` if the value is a string
  1630. */
  1631. function isString(value: any): boolean;
  1632. /**
  1633. * Tests if the specified value is a non-null object.
  1634. * @param value Value to test
  1635. * @returns `true` if the value is a non-null object
  1636. */
  1637. function isObject(value: any): boolean;
  1638. /**
  1639. * Checks if a property on a message is considered to be present.
  1640. * This is an alias of {@link util.isSet}.
  1641. * @param obj Plain object or message instance
  1642. * @param prop Property name
  1643. * @returns `true` if considered to be present, otherwise `false`
  1644. */
  1645. function isset(obj: object, prop: string): boolean;
  1646. /**
  1647. * Checks if a property on a message is considered to be present.
  1648. * @param obj Plain object or message instance
  1649. * @param prop Property name
  1650. * @returns `true` if considered to be present, otherwise `false`
  1651. */
  1652. function isSet(obj: object, prop: string): boolean;
  1653. /** Node's Buffer class if available. */
  1654. let Buffer: Constructor<Buffer>;
  1655. /**
  1656. * Creates a new buffer of whatever type supported by the environment.
  1657. * @param [sizeOrArray=0] Buffer size or number array
  1658. * @returns Buffer
  1659. */
  1660. function newBuffer(sizeOrArray?: (number|number[])): (Uint8Array|Buffer);
  1661. /** Array implementation used in the browser. `Uint8Array` if supported, otherwise `Array`. */
  1662. let Array: Constructor<Uint8Array>;
  1663. /** Long.js's Long class if available. */
  1664. let Long: Constructor<Long>;
  1665. /** Regular expression used to verify 2 bit (`bool`) map keys. */
  1666. const key2Re: RegExp;
  1667. /** Regular expression used to verify 32 bit (`int32` etc.) map keys. */
  1668. const key32Re: RegExp;
  1669. /** Regular expression used to verify 64 bit (`int64` etc.) map keys. */
  1670. const key64Re: RegExp;
  1671. /**
  1672. * Converts a number or long to an 8 characters long hash string.
  1673. * @param value Value to convert
  1674. * @returns Hash
  1675. */
  1676. function longToHash(value: (Long|number)): string;
  1677. /**
  1678. * Converts an 8 characters long hash string to a long or number.
  1679. * @param hash Hash
  1680. * @param [unsigned=false] Whether unsigned or not
  1681. * @returns Original value
  1682. */
  1683. function longFromHash(hash: string, unsigned?: boolean): (Long|number);
  1684. /**
  1685. * Merges the properties of the source object into the destination object.
  1686. * @param dst Destination object
  1687. * @param src Source object
  1688. * @param [ifNotSet=false] Merges only if the key is not already set
  1689. * @returns Destination object
  1690. */
  1691. function merge(dst: { [k: string]: any }, src: { [k: string]: any }, ifNotSet?: boolean): { [k: string]: any };
  1692. /**
  1693. * Converts the first character of a string to lower case.
  1694. * @param str String to convert
  1695. * @returns Converted string
  1696. */
  1697. function lcFirst(str: string): string;
  1698. /**
  1699. * Creates a custom error constructor.
  1700. * @param name Error name
  1701. * @returns Custom error constructor
  1702. */
  1703. function newError(name: string): Constructor<Error>;
  1704. /** Error subclass indicating a protocol specifc error. */
  1705. class ProtocolError<T extends Message<T>> extends Error {
  1706. /**
  1707. * Constructs a new protocol error.
  1708. * @param message Error message
  1709. * @param [properties] Additional properties
  1710. */
  1711. constructor(message: string, properties?: { [k: string]: any });
  1712. /** So far decoded message instance. */
  1713. public instance: Message<T>;
  1714. }
  1715. /**
  1716. * Builds a getter for a oneof's present field name.
  1717. * @param fieldNames Field names
  1718. * @returns Unbound getter
  1719. */
  1720. function oneOfGetter(fieldNames: string[]): OneOfGetter;
  1721. /**
  1722. * Builds a setter for a oneof's present field name.
  1723. * @param fieldNames Field names
  1724. * @returns Unbound setter
  1725. */
  1726. function oneOfSetter(fieldNames: string[]): OneOfSetter;
  1727. /**
  1728. * Default conversion options used for {@link Message#toJSON} implementations.
  1729. *
  1730. * These options are close to proto3's JSON mapping with the exception that internal types like Any are handled just like messages. More precisely:
  1731. *
  1732. * - Longs become strings
  1733. * - Enums become string keys
  1734. * - Bytes become base64 encoded strings
  1735. * - (Sub-)Messages become plain objects
  1736. * - Maps become plain objects with all string keys
  1737. * - Repeated fields become arrays
  1738. * - NaN and Infinity for float and double fields become strings
  1739. *
  1740. * @see https://developers.google.com/protocol-buffers/docs/proto3?hl=en#json
  1741. */
  1742. let toJSONOptions: IConversionOptions;
  1743. /** Node's fs module if available. */
  1744. let fs: { [k: string]: any };
  1745. /**
  1746. * Converts an object's values to an array.
  1747. * @param object Object to convert
  1748. * @returns Converted array
  1749. */
  1750. function toArray(object: { [k: string]: any }): any[];
  1751. /**
  1752. * Converts an array of keys immediately followed by their respective value to an object, omitting undefined values.
  1753. * @param array Array to convert
  1754. * @returns Converted object
  1755. */
  1756. function toObject(array: any[]): { [k: string]: any };
  1757. /**
  1758. * Tests whether the specified name is a reserved word in JS.
  1759. * @param name Name to test
  1760. * @returns `true` if reserved, otherwise `false`
  1761. */
  1762. function isReserved(name: string): boolean;
  1763. /**
  1764. * Returns a safe property accessor for the specified property name.
  1765. * @param prop Property name
  1766. * @returns Safe accessor
  1767. */
  1768. function safeProp(prop: string): string;
  1769. /**
  1770. * Converts the first character of a string to upper case.
  1771. * @param str String to convert
  1772. * @returns Converted string
  1773. */
  1774. function ucFirst(str: string): string;
  1775. /**
  1776. * Converts a string to camel case.
  1777. * @param str String to convert
  1778. * @returns Converted string
  1779. */
  1780. function camelCase(str: string): string;
  1781. /**
  1782. * Compares reflected fields by id.
  1783. * @param a First field
  1784. * @param b Second field
  1785. * @returns Comparison value
  1786. */
  1787. function compareFieldsById(a: Field, b: Field): number;
  1788. /**
  1789. * Decorator helper for types (TypeScript).
  1790. * @param ctor Constructor function
  1791. * @param [typeName] Type name, defaults to the constructor's name
  1792. * @returns Reflected type
  1793. */
  1794. function decorateType<T extends Message<T>>(ctor: Constructor<T>, typeName?: string): Type;
  1795. /**
  1796. * Decorator helper for enums (TypeScript).
  1797. * @param object Enum object
  1798. * @returns Reflected enum
  1799. */
  1800. function decorateEnum(object: object): Enum;
  1801. /**
  1802. * Sets the value of a property by property path. If a value already exists, it is turned to an array
  1803. * @param dst Destination object
  1804. * @param path dot '.' delimited path of the property to set
  1805. * @param value the value to set
  1806. * @returns Destination object
  1807. */
  1808. function setProperty(dst: { [k: string]: any }, path: string, value: object): { [k: string]: any };
  1809. /** Decorator root (TypeScript). */
  1810. let decorateRoot: Root;
  1811. /**
  1812. * Returns a promise from a node-style callback function.
  1813. * @param fn Function to call
  1814. * @param ctx Function context
  1815. * @param params Function arguments
  1816. * @returns Promisified function
  1817. */
  1818. function asPromise(fn: asPromiseCallback, ctx: any, ...params: any[]): Promise<any>;
  1819. /** A minimal base64 implementation for number arrays. */
  1820. namespace base64 {
  1821. /**
  1822. * Calculates the byte length of a base64 encoded string.
  1823. * @param string Base64 encoded string
  1824. * @returns Byte length
  1825. */
  1826. function length(string: string): number;
  1827. /**
  1828. * Encodes a buffer to a base64 encoded string.
  1829. * @param buffer Source buffer
  1830. * @param start Source start
  1831. * @param end Source end
  1832. * @returns Base64 encoded string
  1833. */
  1834. function encode(buffer: Uint8Array, start: number, end: number): string;
  1835. /**
  1836. * Decodes a base64 encoded string to a buffer.
  1837. * @param string Source string
  1838. * @param buffer Destination buffer
  1839. * @param offset Destination offset
  1840. * @returns Number of bytes written
  1841. * @throws {Error} If encoding is invalid
  1842. */
  1843. function decode(string: string, buffer: Uint8Array, offset: number): number;
  1844. /**
  1845. * Tests if the specified string appears to be base64 encoded.
  1846. * @param string String to test
  1847. * @returns `true` if probably base64 encoded, otherwise false
  1848. */
  1849. function test(string: string): boolean;
  1850. }
  1851. /**
  1852. * Begins generating a function.
  1853. * @param functionParams Function parameter names
  1854. * @param [functionName] Function name if not anonymous
  1855. * @returns Appender that appends code to the function's body
  1856. */
  1857. function codegen(functionParams: string[], functionName?: string): Codegen;
  1858. namespace codegen {
  1859. /** When set to `true`, codegen will log generated code to console. Useful for debugging. */
  1860. let verbose: boolean;
  1861. }
  1862. /**
  1863. * Begins generating a function.
  1864. * @param [functionName] Function name if not anonymous
  1865. * @returns Appender that appends code to the function's body
  1866. */
  1867. function codegen(functionName?: string): Codegen;
  1868. /** A minimal event emitter. */
  1869. class EventEmitter {
  1870. /** Constructs a new event emitter instance. */
  1871. constructor();
  1872. /**
  1873. * Registers an event listener.
  1874. * @param evt Event name
  1875. * @param fn Listener
  1876. * @param [ctx] Listener context
  1877. * @returns `this`
  1878. */
  1879. public on(evt: string, fn: EventEmitterListener, ctx?: any): this;
  1880. /**
  1881. * Removes an event listener or any matching listeners if arguments are omitted.
  1882. * @param [evt] Event name. Removes all listeners if omitted.
  1883. * @param [fn] Listener to remove. Removes all listeners of `evt` if omitted.
  1884. * @returns `this`
  1885. */
  1886. public off(evt?: string, fn?: EventEmitterListener): this;
  1887. /**
  1888. * Emits an event by calling its listeners with the specified arguments.
  1889. * @param evt Event name
  1890. * @param args Arguments
  1891. * @returns `this`
  1892. */
  1893. public emit(evt: string, ...args: any[]): this;
  1894. }
  1895. /** Reads / writes floats / doubles from / to buffers. */
  1896. namespace float {
  1897. /**
  1898. * Writes a 32 bit float to a buffer using little endian byte order.
  1899. * @param val Value to write
  1900. * @param buf Target buffer
  1901. * @param pos Target buffer offset
  1902. */
  1903. function writeFloatLE(val: number, buf: Uint8Array, pos: number): void;
  1904. /**
  1905. * Writes a 32 bit float to a buffer using big endian byte order.
  1906. * @param val Value to write
  1907. * @param buf Target buffer
  1908. * @param pos Target buffer offset
  1909. */
  1910. function writeFloatBE(val: number, buf: Uint8Array, pos: number): void;
  1911. /**
  1912. * Reads a 32 bit float from a buffer using little endian byte order.
  1913. * @param buf Source buffer
  1914. * @param pos Source buffer offset
  1915. * @returns Value read
  1916. */
  1917. function readFloatLE(buf: Uint8Array, pos: number): number;
  1918. /**
  1919. * Reads a 32 bit float from a buffer using big endian byte order.
  1920. * @param buf Source buffer
  1921. * @param pos Source buffer offset
  1922. * @returns Value read
  1923. */
  1924. function readFloatBE(buf: Uint8Array, pos: number): number;
  1925. /**
  1926. * Writes a 64 bit double to a buffer using little endian byte order.
  1927. * @param val Value to write
  1928. * @param buf Target buffer
  1929. * @param pos Target buffer offset
  1930. */
  1931. function writeDoubleLE(val: number, buf: Uint8Array, pos: number): void;
  1932. /**
  1933. * Writes a 64 bit double to a buffer using big endian byte order.
  1934. * @param val Value to write
  1935. * @param buf Target buffer
  1936. * @param pos Target buffer offset
  1937. */
  1938. function writeDoubleBE(val: number, buf: Uint8Array, pos: number): void;
  1939. /**
  1940. * Reads a 64 bit double from a buffer using little endian byte order.
  1941. * @param buf Source buffer
  1942. * @param pos Source buffer offset
  1943. * @returns Value read
  1944. */
  1945. function readDoubleLE(buf: Uint8Array, pos: number): number;
  1946. /**
  1947. * Reads a 64 bit double from a buffer using big endian byte order.
  1948. * @param buf Source buffer
  1949. * @param pos Source buffer offset
  1950. * @returns Value read
  1951. */
  1952. function readDoubleBE(buf: Uint8Array, pos: number): number;
  1953. }
  1954. /**
  1955. * Fetches the contents of a file.
  1956. * @param filename File path or url
  1957. * @param options Fetch options
  1958. * @param callback Callback function
  1959. */
  1960. function fetch(filename: string, options: IFetchOptions, callback: FetchCallback): void;
  1961. /**
  1962. * Fetches the contents of a file.
  1963. * @param path File path or url
  1964. * @param callback Callback function
  1965. */
  1966. function fetch(path: string, callback: FetchCallback): void;
  1967. /**
  1968. * Fetches the contents of a file.
  1969. * @param path File path or url
  1970. * @param [options] Fetch options
  1971. * @returns Promise
  1972. */
  1973. function fetch(path: string, options?: IFetchOptions): Promise<(string|Uint8Array)>;
  1974. /**
  1975. * Requires a module only if available.
  1976. * @param moduleName Module to require
  1977. * @returns Required module if available and not empty, otherwise `null`
  1978. */
  1979. function inquire(moduleName: string): object;
  1980. /** A minimal path module to resolve Unix, Windows and URL paths alike. */
  1981. namespace path {
  1982. /**
  1983. * Tests if the specified path is absolute.
  1984. * @param path Path to test
  1985. * @returns `true` if path is absolute
  1986. */
  1987. function isAbsolute(path: string): boolean;
  1988. /**
  1989. * Normalizes the specified path.
  1990. * @param path Path to normalize
  1991. * @returns Normalized path
  1992. */
  1993. function normalize(path: string): string;
  1994. /**
  1995. * Resolves the specified include path against the specified origin path.
  1996. * @param originPath Path to the origin file
  1997. * @param includePath Include path relative to origin path
  1998. * @param [alreadyNormalized=false] `true` if both paths are already known to be normalized
  1999. * @returns Path to the include file
  2000. */
  2001. function resolve(originPath: string, includePath: string, alreadyNormalized?: boolean): string;
  2002. }
  2003. /**
  2004. * A general purpose buffer pool.
  2005. * @param alloc Allocator
  2006. * @param slice Slicer
  2007. * @param [size=8192] Slab size
  2008. * @returns Pooled allocator
  2009. */
  2010. function pool(alloc: PoolAllocator, slice: PoolSlicer, size?: number): PoolAllocator;
  2011. /** A minimal UTF8 implementation for number arrays. */
  2012. namespace utf8 {
  2013. /**
  2014. * Calculates the UTF8 byte length of a string.
  2015. * @param string String
  2016. * @returns Byte length
  2017. */
  2018. function length(string: string): number;
  2019. /**
  2020. * Reads UTF8 bytes as a string.
  2021. * @param buffer Source buffer
  2022. * @param start Source start
  2023. * @param end Source end
  2024. * @returns String read
  2025. */
  2026. function read(buffer: Uint8Array, start: number, end: number): string;
  2027. /**
  2028. * Writes a string as UTF8 bytes.
  2029. * @param string Source string
  2030. * @param buffer Destination buffer
  2031. * @param offset Destination offset
  2032. * @returns Bytes written
  2033. */
  2034. function write(string: string, buffer: Uint8Array, offset: number): number;
  2035. }
  2036. }
  2037. /**
  2038. * Generates a verifier specific to the specified message type.
  2039. * @param mtype Message type
  2040. * @returns Codegen instance
  2041. */
  2042. export function verifier(mtype: Type): Codegen;
  2043. /** Wrappers for common types. */
  2044. export const wrappers: { [k: string]: IWrapper };
  2045. /**
  2046. * From object converter part of an {@link IWrapper}.
  2047. * @param object Plain object
  2048. * @returns Message instance
  2049. */
  2050. type WrapperFromObjectConverter = (this: Type, object: { [k: string]: any }) => Message<{}>;
  2051. /**
  2052. * To object converter part of an {@link IWrapper}.
  2053. * @param message Message instance
  2054. * @param [options] Conversion options
  2055. * @returns Plain object
  2056. */
  2057. type WrapperToObjectConverter = (this: Type, message: Message<{}>, options?: IConversionOptions) => { [k: string]: any };
  2058. /** Common type wrapper part of {@link wrappers}. */
  2059. export interface IWrapper {
  2060. /** From object converter */
  2061. fromObject?: WrapperFromObjectConverter;
  2062. /** To object converter */
  2063. toObject?: WrapperToObjectConverter;
  2064. }
  2065. /** Wire format writer using `Uint8Array` if available, otherwise `Array`. */
  2066. export class Writer {
  2067. /** Constructs a new writer instance. */
  2068. constructor();
  2069. /** Current length. */
  2070. public len: number;
  2071. /** Operations head. */
  2072. public head: object;
  2073. /** Operations tail */
  2074. public tail: object;
  2075. /** Linked forked states. */
  2076. public states: (object|null);
  2077. /**
  2078. * Creates a new writer.
  2079. * @returns A {@link BufferWriter} when Buffers are supported, otherwise a {@link Writer}
  2080. */
  2081. public static create(): (BufferWriter|Writer);
  2082. /**
  2083. * Allocates a buffer of the specified size.
  2084. * @param size Buffer size
  2085. * @returns Buffer
  2086. */
  2087. public static alloc(size: number): Uint8Array;
  2088. /**
  2089. * Writes an unsigned 32 bit value as a varint.
  2090. * @param value Value to write
  2091. * @returns `this`
  2092. */
  2093. public uint32(value: number): Writer;
  2094. /**
  2095. * Writes a signed 32 bit value as a varint.
  2096. * @param value Value to write
  2097. * @returns `this`
  2098. */
  2099. public int32(value: number): Writer;
  2100. /**
  2101. * Writes a 32 bit value as a varint, zig-zag encoded.
  2102. * @param value Value to write
  2103. * @returns `this`
  2104. */
  2105. public sint32(value: number): Writer;
  2106. /**
  2107. * Writes an unsigned 64 bit value as a varint.
  2108. * @param value Value to write
  2109. * @returns `this`
  2110. * @throws {TypeError} If `value` is a string and no long library is present.
  2111. */
  2112. public uint64(value: (Long|number|string)): Writer;
  2113. /**
  2114. * Writes a signed 64 bit value as a varint.
  2115. * @param value Value to write
  2116. * @returns `this`
  2117. * @throws {TypeError} If `value` is a string and no long library is present.
  2118. */
  2119. public int64(value: (Long|number|string)): Writer;
  2120. /**
  2121. * Writes a signed 64 bit value as a varint, zig-zag encoded.
  2122. * @param value Value to write
  2123. * @returns `this`
  2124. * @throws {TypeError} If `value` is a string and no long library is present.
  2125. */
  2126. public sint64(value: (Long|number|string)): Writer;
  2127. /**
  2128. * Writes a boolish value as a varint.
  2129. * @param value Value to write
  2130. * @returns `this`
  2131. */
  2132. public bool(value: boolean): Writer;
  2133. /**
  2134. * Writes an unsigned 32 bit value as fixed 32 bits.
  2135. * @param value Value to write
  2136. * @returns `this`
  2137. */
  2138. public fixed32(value: number): Writer;
  2139. /**
  2140. * Writes a signed 32 bit value as fixed 32 bits.
  2141. * @param value Value to write
  2142. * @returns `this`
  2143. */
  2144. public sfixed32(value: number): Writer;
  2145. /**
  2146. * Writes an unsigned 64 bit value as fixed 64 bits.
  2147. * @param value Value to write
  2148. * @returns `this`
  2149. * @throws {TypeError} If `value` is a string and no long library is present.
  2150. */
  2151. public fixed64(value: (Long|number|string)): Writer;
  2152. /**
  2153. * Writes a signed 64 bit value as fixed 64 bits.
  2154. * @param value Value to write
  2155. * @returns `this`
  2156. * @throws {TypeError} If `value` is a string and no long library is present.
  2157. */
  2158. public sfixed64(value: (Long|number|string)): Writer;
  2159. /**
  2160. * Writes a float (32 bit).
  2161. * @param value Value to write
  2162. * @returns `this`
  2163. */
  2164. public float(value: number): Writer;
  2165. /**
  2166. * Writes a double (64 bit float).
  2167. * @param value Value to write
  2168. * @returns `this`
  2169. */
  2170. public double(value: number): Writer;
  2171. /**
  2172. * Writes a sequence of bytes.
  2173. * @param value Buffer or base64 encoded string to write
  2174. * @returns `this`
  2175. */
  2176. public bytes(value: (Uint8Array|string)): Writer;
  2177. /**
  2178. * Writes a string.
  2179. * @param value Value to write
  2180. * @returns `this`
  2181. */
  2182. public string(value: string): Writer;
  2183. /**
  2184. * Forks this writer's state by pushing it to a stack.
  2185. * Calling {@link Writer#reset|reset} or {@link Writer#ldelim|ldelim} resets the writer to the previous state.
  2186. * @returns `this`
  2187. */
  2188. public fork(): Writer;
  2189. /**
  2190. * Resets this instance to the last state.
  2191. * @returns `this`
  2192. */
  2193. public reset(): Writer;
  2194. /**
  2195. * Resets to the last state and appends the fork state's current write length as a varint followed by its operations.
  2196. * @returns `this`
  2197. */
  2198. public ldelim(): Writer;
  2199. /**
  2200. * Finishes the write operation.
  2201. * @returns Finished buffer
  2202. */
  2203. public finish(): Uint8Array;
  2204. }
  2205. /** Wire format writer using node buffers. */
  2206. export class BufferWriter extends Writer {
  2207. /** Constructs a new buffer writer instance. */
  2208. constructor();
  2209. /**
  2210. * Allocates a buffer of the specified size.
  2211. * @param size Buffer size
  2212. * @returns Buffer
  2213. */
  2214. public static alloc(size: number): Buffer;
  2215. /**
  2216. * Finishes the write operation.
  2217. * @returns Finished buffer
  2218. */
  2219. public finish(): Buffer;
  2220. }
  2221. /**
  2222. * Callback as used by {@link util.asPromise}.
  2223. * @param error Error, if any
  2224. * @param params Additional arguments
  2225. */
  2226. type asPromiseCallback = (error: (Error|null), ...params: any[]) => void;
  2227. /**
  2228. * Appends code to the function's body or finishes generation.
  2229. * @param [formatStringOrScope] Format string or, to finish the function, an object of additional scope variables, if any
  2230. * @param [formatParams] Format parameters
  2231. * @returns Itself or the generated function if finished
  2232. * @throws {Error} If format parameter counts do not match
  2233. */
  2234. type Codegen = (formatStringOrScope?: (string|{ [k: string]: any }), ...formatParams: any[]) => (Codegen|Function);
  2235. /**
  2236. * Event listener as used by {@link util.EventEmitter}.
  2237. * @param args Arguments
  2238. */
  2239. type EventEmitterListener = (...args: any[]) => void;
  2240. /**
  2241. * Node-style callback as used by {@link util.fetch}.
  2242. * @param error Error, if any, otherwise `null`
  2243. * @param [contents] File contents, if there hasn't been an error
  2244. */
  2245. type FetchCallback = (error: Error, contents?: string) => void;
  2246. /** Options as used by {@link util.fetch}. */
  2247. export interface IFetchOptions {
  2248. /** Whether expecting a binary response */
  2249. binary?: boolean;
  2250. /** If `true`, forces the use of XMLHttpRequest */
  2251. xhr?: boolean;
  2252. }
  2253. /**
  2254. * An allocator as used by {@link util.pool}.
  2255. * @param size Buffer size
  2256. * @returns Buffer
  2257. */
  2258. type PoolAllocator = (size: number) => Uint8Array;
  2259. /**
  2260. * A slicer as used by {@link util.pool}.
  2261. * @param start Start offset
  2262. * @param end End offset
  2263. * @returns Buffer slice
  2264. */
  2265. type PoolSlicer = (this: Uint8Array, start: number, end: number) => Uint8Array;