index.d.ts 82 KB

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