Ingen beskrivning
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

index.d.ts 85KB

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