1 // DO NOT EDIT! This is a generated file. Edit the JSDoc in src/*.js instead and run 'npm run types'.
3 export as namespace protobuf;
6 * Provides common type definitions.
7 * Can also be used to provide additional google types or your own custom types.
8 * @param name Short name as in `google/protobuf/[name].proto` or full file name
9 * @param json JSON definition within `google.protobuf` if a short name, otherwise the file's root definition
11 export function common(name: string, json: { [k: string]: any }): void;
13 export namespace common {
15 /** Properties of a google.protobuf.Any message. */
21 /** Properties of a google.protobuf.Duration message. */
23 seconds?: (number|Long);
27 /** Properties of a google.protobuf.Timestamp message. */
28 interface ITimestamp {
29 seconds?: (number|Long);
33 /** Properties of a google.protobuf.Empty message. */
37 /** Properties of a google.protobuf.Struct message. */
39 fields?: { [k: string]: IValue };
42 /** Properties of a google.protobuf.Value message. */
49 structValue?: IStruct;
50 listValue?: IListValue;
53 /** Properties of a google.protobuf.ListValue message. */
54 interface IListValue {
58 /** Properties of a google.protobuf.DoubleValue message. */
59 interface IDoubleValue {
63 /** Properties of a google.protobuf.FloatValue message. */
64 interface IFloatValue {
68 /** Properties of a google.protobuf.Int64Value message. */
69 interface IInt64Value {
70 value?: (number|Long);
73 /** Properties of a google.protobuf.UInt64Value message. */
74 interface IUInt64Value {
75 value?: (number|Long);
78 /** Properties of a google.protobuf.Int32Value message. */
79 interface IInt32Value {
83 /** Properties of a google.protobuf.UInt32Value message. */
84 interface IUInt32Value {
88 /** Properties of a google.protobuf.BoolValue message. */
89 interface IBoolValue {
93 /** Properties of a google.protobuf.StringValue message. */
94 interface IStringValue {
98 /** Properties of a google.protobuf.BytesValue message. */
99 interface IBytesValue {
104 * Gets the root definition of the specified common proto file.
106 * Bundled definitions are:
107 * - google/protobuf/any.proto
108 * - google/protobuf/duration.proto
109 * - google/protobuf/empty.proto
110 * - google/protobuf/field_mask.proto
111 * - google/protobuf/struct.proto
112 * - google/protobuf/timestamp.proto
113 * - google/protobuf/wrappers.proto
115 * @param file Proto file name
116 * @returns Root definition or `null` if not defined
118 function get(file: string): (INamespace|null);
121 /** Runtime message from/to plain object converters. */
122 export namespace converter {
125 * Generates a plain object to runtime message converter specific to the specified message type.
126 * @param mtype Message type
127 * @returns Codegen instance
129 function fromObject(mtype: Type): Codegen;
132 * Generates a runtime message to plain object converter specific to the specified message type.
133 * @param mtype Message type
134 * @returns Codegen instance
136 function toObject(mtype: Type): Codegen;
140 * Generates a decoder specific to the specified message type.
141 * @param mtype Message type
142 * @returns Codegen instance
144 export function decoder(mtype: Type): Codegen;
147 * Generates an encoder specific to the specified message type.
148 * @param mtype Message type
149 * @returns Codegen instance
151 export function encoder(mtype: Type): Codegen;
153 /** Reflected enum. */
154 export class Enum extends ReflectionObject {
157 * Constructs a new enum instance.
158 * @param name Unique name within its namespace
159 * @param [values] Enum values as an object, by name
160 * @param [options] Declared options
161 * @param [comment] The comment for this enum
162 * @param [comments] The value comments for this enum
164 constructor(name: string, values?: { [k: string]: number }, options?: { [k: string]: any }, comment?: string, comments?: { [k: string]: string });
166 /** Enum values by id. */
167 public valuesById: { [k: number]: string };
169 /** Enum values by name. */
170 public values: { [k: string]: number };
172 /** Enum comment text. */
173 public comment: (string|null);
175 /** Value comment texts, if any. */
176 public comments: { [k: string]: string };
178 /** Reserved ranges, if any. */
179 public reserved: (number[]|string)[];
182 * Constructs an enum from an enum descriptor.
183 * @param name Enum name
184 * @param json Enum descriptor
185 * @returns Created enum
186 * @throws {TypeError} If arguments are invalid
188 public static fromJSON(name: string, json: IEnum): Enum;
191 * Converts this enum to an enum descriptor.
192 * @param [toJSONOptions] JSON conversion options
193 * @returns Enum descriptor
195 public toJSON(toJSONOptions?: IToJSONOptions): IEnum;
198 * Adds a value to this enum.
199 * @param name Value name
201 * @param [comment] Comment, if any
203 * @throws {TypeError} If arguments are invalid
204 * @throws {Error} If there is already a value with this name or id
206 public add(name: string, id: number, comment?: string): Enum;
209 * Removes a value from this enum
210 * @param name Value name
212 * @throws {TypeError} If arguments are invalid
213 * @throws {Error} If `name` is not a name of this enum
215 public remove(name: string): Enum;
218 * Tests if the specified id is reserved.
219 * @param id Id to test
220 * @returns `true` if reserved, otherwise `false`
222 public isReservedId(id: number): boolean;
225 * Tests if the specified name is reserved.
226 * @param name Name to test
227 * @returns `true` if reserved, otherwise `false`
229 public isReservedName(name: string): boolean;
232 /** Enum descriptor. */
233 export interface IEnum {
236 values: { [k: string]: number };
239 options?: { [k: string]: any };
242 /** Reflected message field. */
243 export class Field extends FieldBase {
246 * Constructs a new message field instance. Note that {@link MapField|map fields} have their own class.
247 * @param name Unique name within its namespace
248 * @param id Unique id within its namespace
249 * @param type Value type
250 * @param [rule="optional"] Field rule
251 * @param [extend] Extended type if different from parent
252 * @param [options] Declared options
254 constructor(name: string, id: number, type: string, rule?: (string|{ [k: string]: any }), extend?: (string|{ [k: string]: any }), options?: { [k: string]: any });
257 * Constructs a field from a field descriptor.
258 * @param name Field name
259 * @param json Field descriptor
260 * @returns Created field
261 * @throws {TypeError} If arguments are invalid
263 public static fromJSON(name: string, json: IField): Field;
265 /** Determines whether this field is packed. Only relevant when repeated and working with proto2. */
266 public readonly packed: boolean;
269 * Field decorator (TypeScript).
270 * @param fieldId Field id
271 * @param fieldType Field type
272 * @param [fieldRule="optional"] Field rule
273 * @param [defaultValue] Default value
274 * @returns Decorator function
276 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;
279 * Field decorator (TypeScript).
280 * @param fieldId Field id
281 * @param fieldType Field type
282 * @param [fieldRule="optional"] Field rule
283 * @returns Decorator function
285 public static d<T extends Message<T>>(fieldId: number, fieldType: (Constructor<T>|string), fieldRule?: ("optional"|"required"|"repeated")): FieldDecorator;
288 /** Base class of all reflected message fields. This is not an actual class but here for the sake of having consistent type definitions. */
289 export class FieldBase extends ReflectionObject {
292 * Not an actual constructor. Use {@link Field} instead.
293 * @param name Unique name within its namespace
294 * @param id Unique id within its namespace
295 * @param type Value type
296 * @param [rule="optional"] Field rule
297 * @param [extend] Extended type if different from parent
298 * @param [options] Declared options
299 * @param [comment] Comment associated with this field
301 constructor(name: string, id: number, type: string, rule?: (string|{ [k: string]: any }), extend?: (string|{ [k: string]: any }), options?: { [k: string]: any }, comment?: string);
303 /** Field rule, if any. */
304 public rule?: string;
309 /** Unique field id. */
312 /** Extended type if different from parent. */
313 public extend?: string;
315 /** Whether this field is required. */
316 public required: boolean;
318 /** Whether this field is optional. */
319 public optional: boolean;
321 /** Whether this field is repeated. */
322 public repeated: boolean;
324 /** Whether this field is a map or not. */
327 /** Message this field belongs to. */
328 public message: (Type|null);
330 /** OneOf this field belongs to, if any, */
331 public partOf: (OneOf|null);
333 /** The field type's default value. */
334 public typeDefault: any;
336 /** The field's default value on prototypes. */
337 public defaultValue: any;
339 /** Whether this field's value should be treated as a long. */
340 public long: boolean;
342 /** Whether this field's value is a buffer. */
343 public bytes: boolean;
345 /** Resolved type if not a basic type. */
346 public resolvedType: (Type|Enum|null);
348 /** Sister-field within the extended type if a declaring extension field. */
349 public extensionField: (Field|null);
351 /** Sister-field within the declaring namespace if an extended field. */
352 public declaringField: (Field|null);
354 /** Comment for this field. */
355 public comment: (string|null);
358 * Converts this field to a field descriptor.
359 * @param [toJSONOptions] JSON conversion options
360 * @returns Field descriptor
362 public toJSON(toJSONOptions?: IToJSONOptions): IField;
365 * Resolves this field's type references.
367 * @throws {Error} If any reference cannot be resolved
369 public resolve(): Field;
372 /** Field descriptor. */
373 export interface IField {
385 options?: { [k: string]: any };
388 /** Extension field descriptor. */
389 export interface IExtensionField extends IField {
396 * Decorator function as returned by {@link Field.d} and {@link MapField.d} (TypeScript).
397 * @param prototype Target prototype
398 * @param fieldName Field name
400 type FieldDecorator = (prototype: object, fieldName: string) => void;
403 * A node-style callback as used by {@link load} and {@link Root#load}.
404 * @param error Error, if any, otherwise `null`
405 * @param [root] Root, if there hasn't been an error
407 type LoadCallback = (error: (Error|null), root?: Root) => void;
410 * Loads one or multiple .proto or preprocessed .json files into a common root namespace and calls the callback.
411 * @param filename One or multiple files to load
412 * @param root Root namespace, defaults to create a new one if omitted.
413 * @param callback Callback function
414 * @see {@link Root#load}
416 export function load(filename: (string|string[]), root: Root, callback: LoadCallback): void;
419 * Loads one or multiple .proto or preprocessed .json files into a common root namespace and calls the callback.
420 * @param filename One or multiple files to load
421 * @param callback Callback function
422 * @see {@link Root#load}
424 export function load(filename: (string|string[]), callback: LoadCallback): void;
427 * Loads one or multiple .proto or preprocessed .json files into a common root namespace and returns a promise.
428 * @param filename One or multiple files to load
429 * @param [root] Root namespace, defaults to create a new one if omitted.
431 * @see {@link Root#load}
433 export function load(filename: (string|string[]), root?: Root): Promise<Root>;
436 * Synchronously loads one or multiple .proto or preprocessed .json files into a common root namespace (node only).
437 * @param filename One or multiple files to load
438 * @param [root] Root namespace, defaults to create a new one if omitted.
439 * @returns Root namespace
440 * @throws {Error} If synchronous fetching is not supported (i.e. in browsers) or if a file's syntax is invalid
441 * @see {@link Root#loadSync}
443 export function loadSync(filename: (string|string[]), root?: Root): Root;
445 /** Build type, one of `"full"`, `"light"` or `"minimal"`. */
446 export const build: string;
448 /** Reconfigures the library according to the environment. */
449 export function configure(): void;
451 /** Reflected map field. */
452 export class MapField extends FieldBase {
455 * Constructs a new map field instance.
456 * @param name Unique name within its namespace
457 * @param id Unique id within its namespace
458 * @param keyType Key type
459 * @param type Value type
460 * @param [options] Declared options
461 * @param [comment] Comment associated with this field
463 constructor(name: string, id: number, keyType: string, type: string, options?: { [k: string]: any }, comment?: string);
466 public keyType: string;
468 /** Resolved key type if not a basic type. */
469 public resolvedKeyType: (ReflectionObject|null);
472 * Constructs a map field from a map field descriptor.
473 * @param name Field name
474 * @param json Map field descriptor
475 * @returns Created map field
476 * @throws {TypeError} If arguments are invalid
478 public static fromJSON(name: string, json: IMapField): MapField;
481 * Converts this map field to a map field descriptor.
482 * @param [toJSONOptions] JSON conversion options
483 * @returns Map field descriptor
485 public toJSON(toJSONOptions?: IToJSONOptions): IMapField;
488 * Map field decorator (TypeScript).
489 * @param fieldId Field id
490 * @param fieldKeyType Field key type
491 * @param fieldValueType Field value type
492 * @returns Decorator function
494 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;
497 /** Map field descriptor. */
498 export interface IMapField extends IField {
504 /** Extension map field descriptor. */
505 export interface IExtensionMapField extends IMapField {
511 /** Abstract runtime message. */
512 export class Message<T extends object = object> {
515 * Constructs a new message instance.
516 * @param [properties] Properties to set
518 constructor(properties?: Properties<T>);
520 /** Reference to the reflected type. */
521 public static readonly $type: Type;
523 /** Reference to the reflected type. */
524 public readonly $type: Type;
527 * Creates a new message of this type using the specified properties.
528 * @param [properties] Properties to set
529 * @returns Message instance
531 public static create<T extends Message<T>>(this: Constructor<T>, properties?: { [k: string]: any }): Message<T>;
534 * Encodes a message of this type.
535 * @param message Message to encode
536 * @param [writer] Writer to use
539 public static encode<T extends Message<T>>(this: Constructor<T>, message: (T|{ [k: string]: any }), writer?: Writer): Writer;
542 * Encodes a message of this type preceeded by its length as a varint.
543 * @param message Message to encode
544 * @param [writer] Writer to use
547 public static encodeDelimited<T extends Message<T>>(this: Constructor<T>, message: (T|{ [k: string]: any }), writer?: Writer): Writer;
550 * Decodes a message of this type.
551 * @param reader Reader or buffer to decode
552 * @returns Decoded message
554 public static decode<T extends Message<T>>(this: Constructor<T>, reader: (Reader|Uint8Array)): T;
557 * Decodes a message of this type preceeded by its length as a varint.
558 * @param reader Reader or buffer to decode
559 * @returns Decoded message
561 public static decodeDelimited<T extends Message<T>>(this: Constructor<T>, reader: (Reader|Uint8Array)): T;
564 * Verifies a message of this type.
565 * @param message Plain object to verify
566 * @returns `null` if valid, otherwise the reason why it is not
568 public static verify(message: { [k: string]: any }): (string|null);
571 * Creates a new message of this type from a plain object. Also converts values to their respective internal types.
572 * @param object Plain object
573 * @returns Message instance
575 public static fromObject<T extends Message<T>>(this: Constructor<T>, object: { [k: string]: any }): T;
578 * Creates a plain object from a message of this type. Also converts values to other types if specified.
579 * @param message Message instance
580 * @param [options] Conversion options
581 * @returns Plain object
583 public static toObject<T extends Message<T>>(this: Constructor<T>, message: T, options?: IConversionOptions): { [k: string]: any };
586 * Converts this message to JSON.
587 * @returns JSON object
589 public toJSON(): { [k: string]: any };
592 /** Reflected service method. */
593 export class Method extends ReflectionObject {
596 * Constructs a new service method instance.
597 * @param name Method name
598 * @param type Method type, usually `"rpc"`
599 * @param requestType Request message type
600 * @param responseType Response message type
601 * @param [requestStream] Whether the request is streamed
602 * @param [responseStream] Whether the response is streamed
603 * @param [options] Declared options
604 * @param [comment] The comment for this method
606 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);
612 public requestType: string;
614 /** Whether requests are streamed or not. */
615 public requestStream?: boolean;
617 /** Response type. */
618 public responseType: string;
620 /** Whether responses are streamed or not. */
621 public responseStream?: boolean;
623 /** Resolved request type. */
624 public resolvedRequestType: (Type|null);
626 /** Resolved response type. */
627 public resolvedResponseType: (Type|null);
629 /** Comment for this method */
630 public comment: (string|null);
633 * Constructs a method from a method descriptor.
634 * @param name Method name
635 * @param json Method descriptor
636 * @returns Created method
637 * @throws {TypeError} If arguments are invalid
639 public static fromJSON(name: string, json: IMethod): Method;
642 * Converts this method to a method descriptor.
643 * @param [toJSONOptions] JSON conversion options
644 * @returns Method descriptor
646 public toJSON(toJSONOptions?: IToJSONOptions): IMethod;
649 /** Method descriptor. */
650 export interface IMethod {
659 responseType: string;
661 /** Whether requests are streamed */
662 requestStream?: boolean;
664 /** Whether responses are streamed */
665 responseStream?: boolean;
667 /** Method options */
668 options?: { [k: string]: any };
671 /** Reflected namespace. */
672 export class Namespace extends NamespaceBase {
675 * Constructs a new namespace instance.
676 * @param name Namespace name
677 * @param [options] Declared options
679 constructor(name: string, options?: { [k: string]: any });
682 * Constructs a namespace from JSON.
683 * @param name Namespace name
684 * @param json JSON object
685 * @returns Created namespace
686 * @throws {TypeError} If arguments are invalid
688 public static fromJSON(name: string, json: { [k: string]: any }): Namespace;
691 * Converts an array of reflection objects to JSON.
692 * @param array Object array
693 * @param [toJSONOptions] JSON conversion options
694 * @returns JSON object or `undefined` when array is empty
696 public static arrayToJSON(array: ReflectionObject[], toJSONOptions?: IToJSONOptions): ({ [k: string]: any }|undefined);
699 * Tests if the specified id is reserved.
700 * @param reserved Array of reserved ranges and names
701 * @param id Id to test
702 * @returns `true` if reserved, otherwise `false`
704 public static isReservedId(reserved: ((number[]|string)[]|undefined), id: number): boolean;
707 * Tests if the specified name is reserved.
708 * @param reserved Array of reserved ranges and names
709 * @param name Name to test
710 * @returns `true` if reserved, otherwise `false`
712 public static isReservedName(reserved: ((number[]|string)[]|undefined), name: string): boolean;
715 /** 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. */
716 export abstract class NamespaceBase extends ReflectionObject {
718 /** Nested objects by name. */
719 public nested?: { [k: string]: ReflectionObject };
721 /** Nested objects of this namespace as an array for iteration. */
722 public readonly nestedArray: ReflectionObject[];
725 * Converts this namespace to a namespace descriptor.
726 * @param [toJSONOptions] JSON conversion options
727 * @returns Namespace descriptor
729 public toJSON(toJSONOptions?: IToJSONOptions): INamespace;
732 * Adds nested objects to this namespace from nested object descriptors.
733 * @param nestedJson Any nested object descriptors
736 public addJSON(nestedJson: { [k: string]: AnyNestedObject }): Namespace;
739 * Gets the nested object of the specified name.
740 * @param name Nested object name
741 * @returns The reflection object or `null` if it doesn't exist
743 public get(name: string): (ReflectionObject|null);
746 * Gets the values of the nested {@link Enum|enum} of the specified name.
747 * This methods differs from {@link Namespace#get|get} in that it returns an enum's values directly and throws instead of returning `null`.
748 * @param name Nested enum name
749 * @returns Enum values
750 * @throws {Error} If there is no such enum
752 public getEnum(name: string): { [k: string]: number };
755 * Adds a nested object to this namespace.
756 * @param object Nested object to add
758 * @throws {TypeError} If arguments are invalid
759 * @throws {Error} If there is already a nested object with this name
761 public add(object: ReflectionObject): Namespace;
764 * Removes a nested object from this namespace.
765 * @param object Nested object to remove
767 * @throws {TypeError} If arguments are invalid
768 * @throws {Error} If `object` is not a member of this namespace
770 public remove(object: ReflectionObject): Namespace;
773 * Defines additial namespaces within this one if not yet existing.
774 * @param path Path to create
775 * @param [json] Nested types to create from JSON
776 * @returns Pointer to the last namespace created or `this` if path is empty
778 public define(path: (string|string[]), json?: any): Namespace;
781 * Resolves this namespace's and all its nested objects' type references. Useful to validate a reflection tree, but comes at a cost.
784 public resolveAll(): Namespace;
787 * Recursively looks up the reflection object matching the specified path in the scope of this namespace.
788 * @param path Path to look up
789 * @param filterTypes Filter types, any combination of the constructors of `protobuf.Type`, `protobuf.Enum`, `protobuf.Service` etc.
790 * @param [parentAlreadyChecked=false] If known, whether the parent has already been checked
791 * @returns Looked up object or `null` if none could be found
793 public lookup(path: (string|string[]), filterTypes: (any|any[]), parentAlreadyChecked?: boolean): (ReflectionObject|null);
796 * Looks up the reflection object at the specified path, relative to this namespace.
797 * @param path Path to look up
798 * @param [parentAlreadyChecked=false] Whether the parent has already been checked
799 * @returns Looked up object or `null` if none could be found
801 public lookup(path: (string|string[]), parentAlreadyChecked?: boolean): (ReflectionObject|null);
804 * Looks up the {@link Type|type} at the specified path, relative to this namespace.
805 * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
806 * @param path Path to look up
807 * @returns Looked up type
808 * @throws {Error} If `path` does not point to a type
810 public lookupType(path: (string|string[])): Type;
813 * Looks up the values of the {@link Enum|enum} at the specified path, relative to this namespace.
814 * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
815 * @param path Path to look up
816 * @returns Looked up enum
817 * @throws {Error} If `path` does not point to an enum
819 public lookupEnum(path: (string|string[])): Enum;
822 * Looks up the {@link Type|type} or {@link Enum|enum} at the specified path, relative to this namespace.
823 * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
824 * @param path Path to look up
825 * @returns Looked up type or enum
826 * @throws {Error} If `path` does not point to a type or enum
828 public lookupTypeOrEnum(path: (string|string[])): Type;
831 * Looks up the {@link Service|service} at the specified path, relative to this namespace.
832 * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`.
833 * @param path Path to look up
834 * @returns Looked up service
835 * @throws {Error} If `path` does not point to a service
837 public lookupService(path: (string|string[])): Service;
840 /** Namespace descriptor. */
841 export interface INamespace {
843 /** Namespace options */
844 options?: { [k: string]: any };
846 /** Nested object descriptors */
847 nested?: { [k: string]: AnyNestedObject };
850 /** Any extension field descriptor. */
851 type AnyExtensionField = (IExtensionField|IExtensionMapField);
853 /** Any nested object descriptor. */
854 type AnyNestedObject = (IEnum|IType|IService|AnyExtensionField|INamespace);
856 /** Base class of all reflection objects. */
857 export abstract class ReflectionObject {
860 public options?: { [k: string]: any };
862 /** Parsed Options. */
863 public parsedOptions?: { [k: string]: any[] };
865 /** Unique name within its namespace. */
868 /** Parent namespace. */
869 public parent: (Namespace|null);
871 /** Whether already resolved or not. */
872 public resolved: boolean;
874 /** Comment text, if any. */
875 public comment: (string|null);
877 /** Defining file name. */
878 public filename: (string|null);
880 /** Reference to the root namespace. */
881 public readonly root: Root;
883 /** Full name including leading dot. */
884 public readonly fullName: string;
887 * Converts this reflection object to its descriptor representation.
888 * @returns Descriptor
890 public toJSON(): { [k: string]: any };
893 * Called when this object is added to a parent.
894 * @param parent Parent added to
896 public onAdd(parent: ReflectionObject): void;
899 * Called when this object is removed from a parent.
900 * @param parent Parent removed from
902 public onRemove(parent: ReflectionObject): void;
905 * Resolves this objects type references.
908 public resolve(): ReflectionObject;
911 * Gets an option value.
912 * @param name Option name
913 * @returns Option value or `undefined` if not set
915 public getOption(name: string): any;
919 * @param name Option name
920 * @param value Option value
921 * @param [ifNotSet] Sets the option only if it isn't currently set
924 public setOption(name: string, value: any, ifNotSet?: boolean): ReflectionObject;
927 * Sets a parsed option.
928 * @param name parsed Option name
929 * @param value Option value
930 * @param propName dot '.' delimited full path of property within the option to set. if undefined\empty, will add a new option with that value
933 public setParsedOption(name: string, value: any, propName: string): ReflectionObject;
936 * Sets multiple options.
937 * @param options Options to set
938 * @param [ifNotSet] Sets an option only if it isn't currently set
941 public setOptions(options: { [k: string]: any }, ifNotSet?: boolean): ReflectionObject;
944 * Converts this instance to its string representation.
945 * @returns Class name[, space, full name]
947 public toString(): string;
950 /** Reflected oneof. */
951 export class OneOf extends ReflectionObject {
954 * Constructs a new oneof instance.
955 * @param name Oneof name
956 * @param [fieldNames] Field names
957 * @param [options] Declared options
958 * @param [comment] Comment associated with this field
960 constructor(name: string, fieldNames?: (string[]|{ [k: string]: any }), options?: { [k: string]: any }, comment?: string);
962 /** Field names that belong to this oneof. */
963 public oneof: string[];
965 /** Fields that belong to this oneof as an array for iteration. */
966 public readonly fieldsArray: Field[];
968 /** Comment for this field. */
969 public comment: (string|null);
972 * Constructs a oneof from a oneof descriptor.
973 * @param name Oneof name
974 * @param json Oneof descriptor
975 * @returns Created oneof
976 * @throws {TypeError} If arguments are invalid
978 public static fromJSON(name: string, json: IOneOf): OneOf;
981 * Converts this oneof to a oneof descriptor.
982 * @param [toJSONOptions] JSON conversion options
983 * @returns Oneof descriptor
985 public toJSON(toJSONOptions?: IToJSONOptions): IOneOf;
988 * Adds a field to this oneof and removes it from its current parent, if any.
989 * @param field Field to add
992 public add(field: Field): OneOf;
995 * Removes a field from this oneof and puts it back to the oneof's parent.
996 * @param field Field to remove
999 public remove(field: Field): OneOf;
1002 * OneOf decorator (TypeScript).
1003 * @param fieldNames Field names
1004 * @returns Decorator function
1006 public static d<T extends string>(...fieldNames: string[]): OneOfDecorator;
1009 /** Oneof descriptor. */
1010 export interface IOneOf {
1012 /** Oneof field names */
1015 /** Oneof options */
1016 options?: { [k: string]: any };
1020 * Decorator function as returned by {@link OneOf.d} (TypeScript).
1021 * @param prototype Target prototype
1022 * @param oneofName OneOf name
1024 type OneOfDecorator = (prototype: object, oneofName: string) => void;
1027 * Parses the given .proto source and returns an object with the parsed contents.
1028 * @param source Source contents
1029 * @param [options] Parse options. Defaults to {@link parse.defaults} when omitted.
1030 * @returns Parser result
1032 export function parse(source: string, options?: IParseOptions): IParserResult;
1034 /** Result object returned from {@link parse}. */
1035 export interface IParserResult {
1037 /** Package name, if declared */
1038 package: (string|undefined);
1040 /** Imports, if any */
1041 imports: (string[]|undefined);
1043 /** Weak imports, if any */
1044 weakImports: (string[]|undefined);
1046 /** Syntax, if specified (either `"proto2"` or `"proto3"`) */
1047 syntax: (string|undefined);
1049 /** Populated root instance */
1053 /** Options modifying the behavior of {@link parse}. */
1054 export interface IParseOptions {
1056 /** Keeps field casing instead of converting to camel case */
1059 /** Recognize double-slash comments in addition to doc-block comments. */
1060 alternateCommentMode?: boolean;
1062 /** Use trailing comment when both leading comment and trailing comment exist. */
1063 preferTrailingComment?: boolean;
1066 /** Options modifying the behavior of JSON serialization. */
1067 export interface IToJSONOptions {
1069 /** Serializes comments. */
1070 keepComments?: boolean;
1074 * Parses the given .proto source and returns an object with the parsed contents.
1075 * @param source Source contents
1076 * @param root Root to populate
1077 * @param [options] Parse options. Defaults to {@link parse.defaults} when omitted.
1078 * @returns Parser result
1080 export function parse(source: string, root: Root, options?: IParseOptions): IParserResult;
1082 /** Wire format reader using `Uint8Array` if available, otherwise `Array`. */
1083 export class Reader {
1086 * Constructs a new reader instance using the specified buffer.
1087 * @param buffer Buffer to read from
1089 constructor(buffer: Uint8Array);
1092 public buf: Uint8Array;
1094 /** Read buffer position. */
1097 /** Read buffer length. */
1101 * Creates a new reader using the specified buffer.
1102 * @param buffer Buffer to read from
1103 * @returns A {@link BufferReader} if `buffer` is a Buffer, otherwise a {@link Reader}
1104 * @throws {Error} If `buffer` is not a valid buffer
1106 public static create(buffer: (Uint8Array|Buffer)): (Reader|BufferReader);
1109 * Reads a varint as an unsigned 32 bit value.
1110 * @returns Value read
1112 public uint32(): number;
1115 * Reads a varint as a signed 32 bit value.
1116 * @returns Value read
1118 public int32(): number;
1121 * Reads a zig-zag encoded varint as a signed 32 bit value.
1122 * @returns Value read
1124 public sint32(): number;
1127 * Reads a varint as a signed 64 bit value.
1128 * @returns Value read
1130 public int64(): Long;
1133 * Reads a varint as an unsigned 64 bit value.
1134 * @returns Value read
1136 public uint64(): Long;
1139 * Reads a zig-zag encoded varint as a signed 64 bit value.
1140 * @returns Value read
1142 public sint64(): Long;
1145 * Reads a varint as a boolean.
1146 * @returns Value read
1148 public bool(): boolean;
1151 * Reads fixed 32 bits as an unsigned 32 bit integer.
1152 * @returns Value read
1154 public fixed32(): number;
1157 * Reads fixed 32 bits as a signed 32 bit integer.
1158 * @returns Value read
1160 public sfixed32(): number;
1163 * Reads fixed 64 bits.
1164 * @returns Value read
1166 public fixed64(): Long;
1169 * Reads zig-zag encoded fixed 64 bits.
1170 * @returns Value read
1172 public sfixed64(): Long;
1175 * Reads a float (32 bit) as a number.
1176 * @returns Value read
1178 public float(): number;
1181 * Reads a double (64 bit float) as a number.
1182 * @returns Value read
1184 public double(): number;
1187 * Reads a sequence of bytes preceeded by its length as a varint.
1188 * @returns Value read
1190 public bytes(): Uint8Array;
1193 * Reads a string preceeded by its byte length as a varint.
1194 * @returns Value read
1196 public string(): string;
1199 * Skips the specified number of bytes if specified, otherwise skips a varint.
1200 * @param [length] Length if known, otherwise a varint is assumed
1203 public skip(length?: number): Reader;
1206 * Skips the next element of the specified wire type.
1207 * @param wireType Wire type received
1210 public skipType(wireType: number): Reader;
1213 /** Wire format reader using node buffers. */
1214 export class BufferReader extends Reader {
1217 * Constructs a new buffer reader instance.
1218 * @param buffer Buffer to read from
1220 constructor(buffer: Buffer);
1223 * Reads a sequence of bytes preceeded by its length as a varint.
1224 * @returns Value read
1226 public bytes(): Buffer;
1229 /** Root namespace wrapping all types, enums, services, sub-namespaces etc. that belong together. */
1230 export class Root extends NamespaceBase {
1233 * Constructs a new root namespace instance.
1234 * @param [options] Top level options
1236 constructor(options?: { [k: string]: any });
1238 /** Deferred extension fields. */
1239 public deferred: Field[];
1241 /** Resolved file names of loaded files. */
1242 public files: string[];
1245 * Loads a namespace descriptor into a root namespace.
1246 * @param json Nameespace descriptor
1247 * @param [root] Root namespace, defaults to create a new one if omitted
1248 * @returns Root namespace
1250 public static fromJSON(json: INamespace, root?: Root): Root;
1253 * Resolves the path of an imported file, relative to the importing origin.
1254 * This method exists so you can override it with your own logic in case your imports are scattered over multiple directories.
1255 * @param origin The file name of the importing file
1256 * @param target The file name being imported
1257 * @returns Resolved path to `target` or `null` to skip the file
1259 public resolvePath(origin: string, target: string): (string|null);
1262 * Fetch content from file path or url
1263 * This method exists so you can override it with your own logic.
1264 * @param path File path or url
1265 * @param callback Callback function
1267 public fetch(path: string, callback: FetchCallback): void;
1270 * Loads one or multiple .proto or preprocessed .json files into this root namespace and calls the callback.
1271 * @param filename Names of one or multiple files to load
1272 * @param options Parse options
1273 * @param callback Callback function
1275 public load(filename: (string|string[]), options: IParseOptions, callback: LoadCallback): void;
1278 * Loads one or multiple .proto or preprocessed .json files into this root namespace and calls the callback.
1279 * @param filename Names of one or multiple files to load
1280 * @param callback Callback function
1282 public load(filename: (string|string[]), callback: LoadCallback): void;
1285 * Loads one or multiple .proto or preprocessed .json files into this root namespace and returns a promise.
1286 * @param filename Names of one or multiple files to load
1287 * @param [options] Parse options. Defaults to {@link parse.defaults} when omitted.
1290 public load(filename: (string|string[]), options?: IParseOptions): Promise<Root>;
1293 * Synchronously loads one or multiple .proto or preprocessed .json files into this root namespace (node only).
1294 * @param filename Names of one or multiple files to load
1295 * @param [options] Parse options. Defaults to {@link parse.defaults} when omitted.
1296 * @returns Root namespace
1297 * @throws {Error} If synchronous fetching is not supported (i.e. in browsers) or if a file's syntax is invalid
1299 public loadSync(filename: (string|string[]), options?: IParseOptions): Root;
1304 * This is where pbjs stores generated structures (the option `-r, --root` specifies a name).
1305 * Can also be used manually to make roots available accross modules.
1307 export let roots: { [k: string]: Root };
1309 /** Streaming RPC helpers. */
1310 export namespace rpc {
1313 * A service method callback as used by {@link rpc.ServiceMethod|ServiceMethod}.
1315 * Differs from {@link RPCImplCallback} in that it is an actual callback of a service method which may not return `response = null`.
1316 * @param error Error, if any
1317 * @param [response] Response message
1319 type ServiceMethodCallback<TRes extends Message<TRes>> = (error: (Error|null), response?: TRes) => void;
1322 * A service method part of a {@link rpc.Service} as created by {@link Service.create}.
1323 * @param request Request message or plain object
1324 * @param [callback] Node-style callback called with the error, if any, and the response message
1325 * @returns Promise if `callback` has been omitted, otherwise `undefined`
1327 type ServiceMethod<TReq extends Message<TReq>, TRes extends Message<TRes>> = (request: (TReq|Properties<TReq>), callback?: rpc.ServiceMethodCallback<TRes>) => Promise<Message<TRes>>;
1329 /** An RPC service as returned by {@link Service#create}. */
1330 class Service extends util.EventEmitter {
1333 * Constructs a new RPC service instance.
1334 * @param rpcImpl RPC implementation
1335 * @param [requestDelimited=false] Whether requests are length-delimited
1336 * @param [responseDelimited=false] Whether responses are length-delimited
1338 constructor(rpcImpl: RPCImpl, requestDelimited?: boolean, responseDelimited?: boolean);
1340 /** RPC implementation. Becomes `null` once the service is ended. */
1341 public rpcImpl: (RPCImpl|null);
1343 /** Whether requests are length-delimited. */
1344 public requestDelimited: boolean;
1346 /** Whether responses are length-delimited. */
1347 public responseDelimited: boolean;
1350 * Calls a service method through {@link rpc.Service#rpcImpl|rpcImpl}.
1351 * @param method Reflected or static method
1352 * @param requestCtor Request constructor
1353 * @param responseCtor Response constructor
1354 * @param request Request message or plain object
1355 * @param callback Service callback
1357 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;
1360 * Ends this service and emits the `end` event.
1361 * @param [endedByRPC=false] Whether the service has been ended by the RPC implementation.
1364 public end(endedByRPC?: boolean): rpc.Service;
1369 * RPC implementation passed to {@link Service#create} performing a service request on network level, i.e. by utilizing http requests or websockets.
1370 * @param method Reflected or static method being called
1371 * @param requestData Request data
1372 * @param callback Callback function
1374 type RPCImpl = (method: (Method|rpc.ServiceMethod<Message<{}>, Message<{}>>), requestData: Uint8Array, callback: RPCImplCallback) => void;
1377 * Node-style callback as used by {@link RPCImpl}.
1378 * @param error Error, if any, otherwise `null`
1379 * @param [response] Response data or `null` to signal end of stream, if there hasn't been an error
1381 type RPCImplCallback = (error: (Error|null), response?: (Uint8Array|null)) => void;
1383 /** Reflected service. */
1384 export class Service extends NamespaceBase {
1387 * Constructs a new service instance.
1388 * @param name Service name
1389 * @param [options] Service options
1390 * @throws {TypeError} If arguments are invalid
1392 constructor(name: string, options?: { [k: string]: any });
1394 /** Service methods. */
1395 public methods: { [k: string]: Method };
1398 * Constructs a service from a service descriptor.
1399 * @param name Service name
1400 * @param json Service descriptor
1401 * @returns Created service
1402 * @throws {TypeError} If arguments are invalid
1404 public static fromJSON(name: string, json: IService): Service;
1407 * Converts this service to a service descriptor.
1408 * @param [toJSONOptions] JSON conversion options
1409 * @returns Service descriptor
1411 public toJSON(toJSONOptions?: IToJSONOptions): IService;
1413 /** Methods of this service as an array for iteration. */
1414 public readonly methodsArray: Method[];
1417 * Creates a runtime service using the specified rpc implementation.
1418 * @param rpcImpl RPC implementation
1419 * @param [requestDelimited=false] Whether requests are length-delimited
1420 * @param [responseDelimited=false] Whether responses are length-delimited
1421 * @returns RPC service. Useful where requests and/or responses are streamed.
1423 public create(rpcImpl: RPCImpl, requestDelimited?: boolean, responseDelimited?: boolean): rpc.Service;
1426 /** Service descriptor. */
1427 export interface IService extends INamespace {
1429 /** Method descriptors */
1430 methods: { [k: string]: IMethod };
1434 * Gets the next token and advances.
1435 * @returns Next token or `null` on eof
1437 type TokenizerHandleNext = () => (string|null);
1440 * Peeks for the next token.
1441 * @returns Next token or `null` on eof
1443 type TokenizerHandlePeek = () => (string|null);
1446 * Pushes a token back to the stack.
1447 * @param token Token
1449 type TokenizerHandlePush = (token: string) => void;
1452 * Skips the next token.
1453 * @param expected Expected token
1454 * @param [optional=false] If optional
1455 * @returns Whether the token matched
1456 * @throws {Error} If the token didn't match and is not optional
1458 type TokenizerHandleSkip = (expected: string, optional?: boolean) => boolean;
1461 * Gets the comment on the previous line or, alternatively, the line comment on the specified line.
1462 * @param [line] Line number
1463 * @returns Comment text or `null` if none
1465 type TokenizerHandleCmnt = (line?: number) => (string|null);
1467 /** Handle object returned from {@link tokenize}. */
1468 export interface ITokenizerHandle {
1470 /** Gets the next token and advances (`null` on eof) */
1471 next: TokenizerHandleNext;
1473 /** Peeks for the next token (`null` on eof) */
1474 peek: TokenizerHandlePeek;
1476 /** Pushes a token back to the stack */
1477 push: TokenizerHandlePush;
1479 /** Skips a token, returns its presence and advances or, if non-optional and not present, throws */
1480 skip: TokenizerHandleSkip;
1482 /** Gets the comment on the previous line or the line comment on the specified line, if any */
1483 cmnt: TokenizerHandleCmnt;
1485 /** Current line number */
1490 * Tokenizes the given .proto source and returns an object with useful utility functions.
1491 * @param source Source contents
1492 * @param alternateCommentMode Whether we should activate alternate comment parsing mode.
1493 * @returns Tokenizer handle
1495 export function tokenize(source: string, alternateCommentMode: boolean): ITokenizerHandle;
1497 export namespace tokenize {
1500 * Unescapes a string.
1501 * @param str String to unescape
1502 * @returns Unescaped string
1504 function unescape(str: string): string;
1507 /** Reflected message type. */
1508 export class Type extends NamespaceBase {
1511 * Constructs a new reflected message type instance.
1512 * @param name Message name
1513 * @param [options] Declared options
1515 constructor(name: string, options?: { [k: string]: any });
1517 /** Message fields. */
1518 public fields: { [k: string]: Field };
1520 /** Oneofs declared within this namespace, if any. */
1521 public oneofs: { [k: string]: OneOf };
1523 /** Extension ranges, if any. */
1524 public extensions: number[][];
1526 /** Reserved ranges, if any. */
1527 public reserved: (number[]|string)[];
1529 /** Message fields by id. */
1530 public readonly fieldsById: { [k: number]: Field };
1532 /** Fields of this message as an array for iteration. */
1533 public readonly fieldsArray: Field[];
1535 /** Oneofs of this message as an array for iteration. */
1536 public readonly oneofsArray: OneOf[];
1539 * The registered constructor, if any registered, otherwise a generic constructor.
1540 * 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.
1542 public ctor: Constructor<{}>;
1545 * Generates a constructor function for the specified type.
1546 * @param mtype Message type
1547 * @returns Codegen instance
1549 public static generateConstructor(mtype: Type): Codegen;
1552 * Creates a message type from a message type descriptor.
1553 * @param name Message name
1554 * @param json Message type descriptor
1555 * @returns Created message type
1557 public static fromJSON(name: string, json: IType): Type;
1560 * Converts this message type to a message type descriptor.
1561 * @param [toJSONOptions] JSON conversion options
1562 * @returns Message type descriptor
1564 public toJSON(toJSONOptions?: IToJSONOptions): IType;
1567 * Adds a nested object to this type.
1568 * @param object Nested object to add
1570 * @throws {TypeError} If arguments are invalid
1571 * @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
1573 public add(object: ReflectionObject): Type;
1576 * Removes a nested object from this type.
1577 * @param object Nested object to remove
1579 * @throws {TypeError} If arguments are invalid
1580 * @throws {Error} If `object` is not a member of this type
1582 public remove(object: ReflectionObject): Type;
1585 * Tests if the specified id is reserved.
1586 * @param id Id to test
1587 * @returns `true` if reserved, otherwise `false`
1589 public isReservedId(id: number): boolean;
1592 * Tests if the specified name is reserved.
1593 * @param name Name to test
1594 * @returns `true` if reserved, otherwise `false`
1596 public isReservedName(name: string): boolean;
1599 * Creates a new message of this type using the specified properties.
1600 * @param [properties] Properties to set
1601 * @returns Message instance
1603 public create(properties?: { [k: string]: any }): Message<{}>;
1606 * Sets up {@link Type#encode|encode}, {@link Type#decode|decode} and {@link Type#verify|verify}.
1609 public setup(): Type;
1612 * Encodes a message of this type. Does not implicitly {@link Type#verify|verify} messages.
1613 * @param message Message instance or plain object
1614 * @param [writer] Writer to encode to
1617 public encode(message: (Message<{}>|{ [k: string]: any }), writer?: Writer): Writer;
1620 * Encodes a message of this type preceeded by its byte length as a varint. Does not implicitly {@link Type#verify|verify} messages.
1621 * @param message Message instance or plain object
1622 * @param [writer] Writer to encode to
1625 public encodeDelimited(message: (Message<{}>|{ [k: string]: any }), writer?: Writer): Writer;
1628 * Decodes a message of this type.
1629 * @param reader Reader or buffer to decode from
1630 * @param [length] Length of the message, if known beforehand
1631 * @returns Decoded message
1632 * @throws {Error} If the payload is not a reader or valid buffer
1633 * @throws {util.ProtocolError<{}>} If required fields are missing
1635 public decode(reader: (Reader|Uint8Array), length?: number): Message<{}>;
1638 * Decodes a message of this type preceeded by its byte length as a varint.
1639 * @param reader Reader or buffer to decode from
1640 * @returns Decoded message
1641 * @throws {Error} If the payload is not a reader or valid buffer
1642 * @throws {util.ProtocolError} If required fields are missing
1644 public decodeDelimited(reader: (Reader|Uint8Array)): Message<{}>;
1647 * Verifies that field values are valid and that required fields are present.
1648 * @param message Plain object to verify
1649 * @returns `null` if valid, otherwise the reason why it is not
1651 public verify(message: { [k: string]: any }): (null|string);
1654 * Creates a new message of this type from a plain object. Also converts values to their respective internal types.
1655 * @param object Plain object to convert
1656 * @returns Message instance
1658 public fromObject(object: { [k: string]: any }): Message<{}>;
1661 * Creates a plain object from a message of this type. Also converts values to other types if specified.
1662 * @param message Message instance
1663 * @param [options] Conversion options
1664 * @returns Plain object
1666 public toObject(message: Message<{}>, options?: IConversionOptions): { [k: string]: any };
1669 * Type decorator (TypeScript).
1670 * @param [typeName] Type name, defaults to the constructor's name
1671 * @returns Decorator function
1673 public static d<T extends Message<T>>(typeName?: string): TypeDecorator<T>;
1676 /** Message type descriptor. */
1677 export interface IType extends INamespace {
1679 /** Oneof descriptors */
1680 oneofs?: { [k: string]: IOneOf };
1682 /** Field descriptors */
1683 fields: { [k: string]: IField };
1685 /** Extension ranges */
1686 extensions?: number[][];
1688 /** Reserved ranges */
1689 reserved?: number[][];
1691 /** Whether a legacy group or not */
1695 /** Conversion options as used by {@link Type#toObject} and {@link Message.toObject}. */
1696 export interface IConversionOptions {
1699 * Long conversion type.
1700 * Valid values are `String` and `Number` (the global types).
1701 * Defaults to copy the present value, which is a possibly unsafe number without and a {@link Long} with a long library.
1706 * Enum value conversion type.
1707 * Only valid value is `String` (the global type).
1708 * Defaults to copy the present value, which is the numeric id.
1713 * Bytes value conversion type.
1714 * Valid values are `Array` and (a base64 encoded) `String` (the global types).
1715 * Defaults to copy the present value, which usually is a Buffer under node and an Uint8Array in the browser.
1719 /** Also sets default values on the resulting object */
1722 /** Sets empty arrays for missing repeated fields even if `defaults=false` */
1725 /** Sets empty objects for missing map fields even if `defaults=false` */
1728 /** Includes virtual oneof properties set to the present field's name, if any */
1731 /** Performs additional JSON compatibility conversions, i.e. NaN and Infinity to strings */
1736 * Decorator function as returned by {@link Type.d} (TypeScript).
1737 * @param target Target constructor
1739 type TypeDecorator<T extends Message<T>> = (target: Constructor<T>) => void;
1741 /** Common type constants. */
1742 export namespace types {
1744 /** Basic type wire types. */
1763 /** Basic type defaults. */
1783 /** Basic long type wire types. */
1792 /** Allowed types for map keys with their associated wire type. */
1808 /** Allowed types for packed repeated fields with their associated wire type. */
1826 /** Constructor type. */
1827 export interface Constructor<T> extends Function {
1828 new(...params: any[]): T; prototype: T;
1831 /** Properties type. */
1832 type Properties<T> = { [P in keyof T]?: T[P] };
1835 * Any compatible Buffer instance.
1836 * This is a minimal stand-alone definition of a Buffer instance. The actual type is that exported by node's typings.
1838 export interface Buffer extends Uint8Array {
1842 * Any compatible Long instance.
1843 * This is a minimal stand-alone definition of a Long instance. The actual type is that exported by long.js.
1845 export interface Long {
1853 /** Whether unsigned or not */
1858 * A OneOf getter as returned by {@link util.oneOfGetter}.
1859 * @returns Set field name, if any
1861 type OneOfGetter = () => (string|undefined);
1864 * A OneOf setter as returned by {@link util.oneOfSetter}.
1865 * @param value Field name
1867 type OneOfSetter = (value: (string|undefined)) => void;
1869 /** Various utility functions. */
1870 export namespace util {
1872 /** Helper class for working with the low and high bits of a 64 bit value. */
1876 * Constructs new long bits.
1877 * @param lo Low 32 bits, unsigned
1878 * @param hi High 32 bits, unsigned
1880 constructor(lo: number, hi: number);
1889 public static zero: util.LongBits;
1892 public static zeroHash: string;
1895 * Constructs new long bits from the specified number.
1896 * @param value Value
1899 public static fromNumber(value: number): util.LongBits;
1902 * Constructs new long bits from a number, long or string.
1903 * @param value Value
1906 public static from(value: (Long|number|string)): util.LongBits;
1909 * Converts this long bits to a possibly unsafe JavaScript number.
1910 * @param [unsigned=false] Whether unsigned or not
1911 * @returns Possibly unsafe number
1913 public toNumber(unsigned?: boolean): number;
1916 * Converts this long bits to a long.
1917 * @param [unsigned=false] Whether unsigned or not
1920 public toLong(unsigned?: boolean): Long;
1923 * Constructs new long bits from the specified 8 characters long hash.
1927 public static fromHash(hash: string): util.LongBits;
1930 * Converts this long bits to a 8 characters long hash.
1933 public toHash(): string;
1936 * Zig-zag encodes this long bits.
1939 public zzEncode(): util.LongBits;
1942 * Zig-zag decodes this long bits.
1945 public zzDecode(): util.LongBits;
1948 * Calculates the length of this longbits when encoded as a varint.
1951 public length(): number;
1954 /** Whether running within node or not. */
1955 let isNode: boolean;
1957 /** Global object reference. */
1960 /** An immuable empty array. */
1961 const emptyArray: any[];
1963 /** An immutable empty object. */
1964 const emptyObject: object;
1967 * Tests if the specified value is an integer.
1968 * @param value Value to test
1969 * @returns `true` if the value is an integer
1971 function isInteger(value: any): boolean;
1974 * Tests if the specified value is a string.
1975 * @param value Value to test
1976 * @returns `true` if the value is a string
1978 function isString(value: any): boolean;
1981 * Tests if the specified value is a non-null object.
1982 * @param value Value to test
1983 * @returns `true` if the value is a non-null object
1985 function isObject(value: any): boolean;
1988 * Checks if a property on a message is considered to be present.
1989 * This is an alias of {@link util.isSet}.
1990 * @param obj Plain object or message instance
1991 * @param prop Property name
1992 * @returns `true` if considered to be present, otherwise `false`
1994 function isset(obj: object, prop: string): boolean;
1997 * Checks if a property on a message is considered to be present.
1998 * @param obj Plain object or message instance
1999 * @param prop Property name
2000 * @returns `true` if considered to be present, otherwise `false`
2002 function isSet(obj: object, prop: string): boolean;
2004 /** Node's Buffer class if available. */
2005 let Buffer: Constructor<Buffer>;
2008 * Creates a new buffer of whatever type supported by the environment.
2009 * @param [sizeOrArray=0] Buffer size or number array
2012 function newBuffer(sizeOrArray?: (number|number[])): (Uint8Array|Buffer);
2014 /** Array implementation used in the browser. `Uint8Array` if supported, otherwise `Array`. */
2015 let Array: Constructor<Uint8Array>;
2017 /** Long.js's Long class if available. */
2018 let Long: Constructor<Long>;
2020 /** Regular expression used to verify 2 bit (`bool`) map keys. */
2021 const key2Re: RegExp;
2023 /** Regular expression used to verify 32 bit (`int32` etc.) map keys. */
2024 const key32Re: RegExp;
2026 /** Regular expression used to verify 64 bit (`int64` etc.) map keys. */
2027 const key64Re: RegExp;
2030 * Converts a number or long to an 8 characters long hash string.
2031 * @param value Value to convert
2034 function longToHash(value: (Long|number)): string;
2037 * Converts an 8 characters long hash string to a long or number.
2039 * @param [unsigned=false] Whether unsigned or not
2040 * @returns Original value
2042 function longFromHash(hash: string, unsigned?: boolean): (Long|number);
2045 * Merges the properties of the source object into the destination object.
2046 * @param dst Destination object
2047 * @param src Source object
2048 * @param [ifNotSet=false] Merges only if the key is not already set
2049 * @returns Destination object
2051 function merge(dst: { [k: string]: any }, src: { [k: string]: any }, ifNotSet?: boolean): { [k: string]: any };
2054 * Converts the first character of a string to lower case.
2055 * @param str String to convert
2056 * @returns Converted string
2058 function lcFirst(str: string): string;
2061 * Creates a custom error constructor.
2062 * @param name Error name
2063 * @returns Custom error constructor
2065 function newError(name: string): Constructor<Error>;
2067 /** Error subclass indicating a protocol specifc error. */
2068 class ProtocolError<T extends Message<T>> extends Error {
2071 * Constructs a new protocol error.
2072 * @param message Error message
2073 * @param [properties] Additional properties
2075 constructor(message: string, properties?: { [k: string]: any });
2077 /** So far decoded message instance. */
2078 public instance: Message<T>;
2082 * Builds a getter for a oneof's present field name.
2083 * @param fieldNames Field names
2084 * @returns Unbound getter
2086 function oneOfGetter(fieldNames: string[]): OneOfGetter;
2089 * Builds a setter for a oneof's present field name.
2090 * @param fieldNames Field names
2091 * @returns Unbound setter
2093 function oneOfSetter(fieldNames: string[]): OneOfSetter;
2096 * Default conversion options used for {@link Message#toJSON} implementations.
2098 * These options are close to proto3's JSON mapping with the exception that internal types like Any are handled just like messages. More precisely:
2100 * - Longs become strings
2101 * - Enums become string keys
2102 * - Bytes become base64 encoded strings
2103 * - (Sub-)Messages become plain objects
2104 * - Maps become plain objects with all string keys
2105 * - Repeated fields become arrays
2106 * - NaN and Infinity for float and double fields become strings
2108 * @see https://developers.google.com/protocol-buffers/docs/proto3?hl=en#json
2110 let toJSONOptions: IConversionOptions;
2112 /** Node's fs module if available. */
2113 let fs: { [k: string]: any };
2116 * Converts an object's values to an array.
2117 * @param object Object to convert
2118 * @returns Converted array
2120 function toArray(object: { [k: string]: any }): any[];
2123 * Converts an array of keys immediately followed by their respective value to an object, omitting undefined values.
2124 * @param array Array to convert
2125 * @returns Converted object
2127 function toObject(array: any[]): { [k: string]: any };
2130 * Tests whether the specified name is a reserved word in JS.
2131 * @param name Name to test
2132 * @returns `true` if reserved, otherwise `false`
2134 function isReserved(name: string): boolean;
2137 * Returns a safe property accessor for the specified property name.
2138 * @param prop Property name
2139 * @returns Safe accessor
2141 function safeProp(prop: string): string;
2144 * Converts the first character of a string to upper case.
2145 * @param str String to convert
2146 * @returns Converted string
2148 function ucFirst(str: string): string;
2151 * Converts a string to camel case.
2152 * @param str String to convert
2153 * @returns Converted string
2155 function camelCase(str: string): string;
2158 * Compares reflected fields by id.
2159 * @param a First field
2160 * @param b Second field
2161 * @returns Comparison value
2163 function compareFieldsById(a: Field, b: Field): number;
2166 * Decorator helper for types (TypeScript).
2167 * @param ctor Constructor function
2168 * @param [typeName] Type name, defaults to the constructor's name
2169 * @returns Reflected type
2171 function decorateType<T extends Message<T>>(ctor: Constructor<T>, typeName?: string): Type;
2174 * Decorator helper for enums (TypeScript).
2175 * @param object Enum object
2176 * @returns Reflected enum
2178 function decorateEnum(object: object): Enum;
2181 * Sets the value of a property by property path. If a value already exists, it is turned to an array
2182 * @param dst Destination object
2183 * @param path dot '.' delimited path of the property to set
2184 * @param value the value to set
2185 * @returns Destination object
2187 function setProperty(dst: { [k: string]: any }, path: string, value: object): { [k: string]: any };
2189 /** Decorator root (TypeScript). */
2190 let decorateRoot: Root;
2193 * Returns a promise from a node-style callback function.
2194 * @param fn Function to call
2195 * @param ctx Function context
2196 * @param params Function arguments
2197 * @returns Promisified function
2199 function asPromise(fn: asPromiseCallback, ctx: any, ...params: any[]): Promise<any>;
2201 /** A minimal base64 implementation for number arrays. */
2205 * Calculates the byte length of a base64 encoded string.
2206 * @param string Base64 encoded string
2207 * @returns Byte length
2209 function length(string: string): number;
2212 * Encodes a buffer to a base64 encoded string.
2213 * @param buffer Source buffer
2214 * @param start Source start
2215 * @param end Source end
2216 * @returns Base64 encoded string
2218 function encode(buffer: Uint8Array, start: number, end: number): string;
2221 * Decodes a base64 encoded string to a buffer.
2222 * @param string Source string
2223 * @param buffer Destination buffer
2224 * @param offset Destination offset
2225 * @returns Number of bytes written
2226 * @throws {Error} If encoding is invalid
2228 function decode(string: string, buffer: Uint8Array, offset: number): number;
2231 * Tests if the specified string appears to be base64 encoded.
2232 * @param string String to test
2233 * @returns `true` if probably base64 encoded, otherwise false
2235 function test(string: string): boolean;
2239 * Begins generating a function.
2240 * @param functionParams Function parameter names
2241 * @param [functionName] Function name if not anonymous
2242 * @returns Appender that appends code to the function's body
2244 function codegen(functionParams: string[], functionName?: string): Codegen;
2248 /** When set to `true`, codegen will log generated code to console. Useful for debugging. */
2249 let verbose: boolean;
2253 * Begins generating a function.
2254 * @param [functionName] Function name if not anonymous
2255 * @returns Appender that appends code to the function's body
2257 function codegen(functionName?: string): Codegen;
2259 /** A minimal event emitter. */
2260 class EventEmitter {
2262 /** Constructs a new event emitter instance. */
2266 * Registers an event listener.
2267 * @param evt Event name
2268 * @param fn Listener
2269 * @param [ctx] Listener context
2272 public on(evt: string, fn: EventEmitterListener, ctx?: any): this;
2275 * Removes an event listener or any matching listeners if arguments are omitted.
2276 * @param [evt] Event name. Removes all listeners if omitted.
2277 * @param [fn] Listener to remove. Removes all listeners of `evt` if omitted.
2280 public off(evt?: string, fn?: EventEmitterListener): this;
2283 * Emits an event by calling its listeners with the specified arguments.
2284 * @param evt Event name
2285 * @param args Arguments
2288 public emit(evt: string, ...args: any[]): this;
2291 /** Reads / writes floats / doubles from / to buffers. */
2295 * Writes a 32 bit float to a buffer using little endian byte order.
2296 * @param val Value to write
2297 * @param buf Target buffer
2298 * @param pos Target buffer offset
2300 function writeFloatLE(val: number, buf: Uint8Array, pos: number): void;
2303 * Writes a 32 bit float to a buffer using big endian byte order.
2304 * @param val Value to write
2305 * @param buf Target buffer
2306 * @param pos Target buffer offset
2308 function writeFloatBE(val: number, buf: Uint8Array, pos: number): void;
2311 * Reads a 32 bit float from a buffer using little endian byte order.
2312 * @param buf Source buffer
2313 * @param pos Source buffer offset
2314 * @returns Value read
2316 function readFloatLE(buf: Uint8Array, pos: number): number;
2319 * Reads a 32 bit float from a buffer using big endian byte order.
2320 * @param buf Source buffer
2321 * @param pos Source buffer offset
2322 * @returns Value read
2324 function readFloatBE(buf: Uint8Array, pos: number): number;
2327 * Writes a 64 bit double to a buffer using little endian byte order.
2328 * @param val Value to write
2329 * @param buf Target buffer
2330 * @param pos Target buffer offset
2332 function writeDoubleLE(val: number, buf: Uint8Array, pos: number): void;
2335 * Writes a 64 bit double to a buffer using big endian byte order.
2336 * @param val Value to write
2337 * @param buf Target buffer
2338 * @param pos Target buffer offset
2340 function writeDoubleBE(val: number, buf: Uint8Array, pos: number): void;
2343 * Reads a 64 bit double from a buffer using little endian byte order.
2344 * @param buf Source buffer
2345 * @param pos Source buffer offset
2346 * @returns Value read
2348 function readDoubleLE(buf: Uint8Array, pos: number): number;
2351 * Reads a 64 bit double from a buffer using big endian byte order.
2352 * @param buf Source buffer
2353 * @param pos Source buffer offset
2354 * @returns Value read
2356 function readDoubleBE(buf: Uint8Array, pos: number): number;
2360 * Fetches the contents of a file.
2361 * @param filename File path or url
2362 * @param options Fetch options
2363 * @param callback Callback function
2365 function fetch(filename: string, options: IFetchOptions, callback: FetchCallback): void;
2368 * Fetches the contents of a file.
2369 * @param path File path or url
2370 * @param callback Callback function
2372 function fetch(path: string, callback: FetchCallback): void;
2375 * Fetches the contents of a file.
2376 * @param path File path or url
2377 * @param [options] Fetch options
2380 function fetch(path: string, options?: IFetchOptions): Promise<(string|Uint8Array)>;
2383 * Requires a module only if available.
2384 * @param moduleName Module to require
2385 * @returns Required module if available and not empty, otherwise `null`
2387 function inquire(moduleName: string): object;
2389 /** A minimal path module to resolve Unix, Windows and URL paths alike. */
2393 * Tests if the specified path is absolute.
2394 * @param path Path to test
2395 * @returns `true` if path is absolute
2397 function isAbsolute(path: string): boolean;
2400 * Normalizes the specified path.
2401 * @param path Path to normalize
2402 * @returns Normalized path
2404 function normalize(path: string): string;
2407 * Resolves the specified include path against the specified origin path.
2408 * @param originPath Path to the origin file
2409 * @param includePath Include path relative to origin path
2410 * @param [alreadyNormalized=false] `true` if both paths are already known to be normalized
2411 * @returns Path to the include file
2413 function resolve(originPath: string, includePath: string, alreadyNormalized?: boolean): string;
2417 * A general purpose buffer pool.
2418 * @param alloc Allocator
2419 * @param slice Slicer
2420 * @param [size=8192] Slab size
2421 * @returns Pooled allocator
2423 function pool(alloc: PoolAllocator, slice: PoolSlicer, size?: number): PoolAllocator;
2425 /** A minimal UTF8 implementation for number arrays. */
2429 * Calculates the UTF8 byte length of a string.
2430 * @param string String
2431 * @returns Byte length
2433 function length(string: string): number;
2436 * Reads UTF8 bytes as a string.
2437 * @param buffer Source buffer
2438 * @param start Source start
2439 * @param end Source end
2440 * @returns String read
2442 function read(buffer: Uint8Array, start: number, end: number): string;
2445 * Writes a string as UTF8 bytes.
2446 * @param string Source string
2447 * @param buffer Destination buffer
2448 * @param offset Destination offset
2449 * @returns Bytes written
2451 function write(string: string, buffer: Uint8Array, offset: number): number;
2456 * Generates a verifier specific to the specified message type.
2457 * @param mtype Message type
2458 * @returns Codegen instance
2460 export function verifier(mtype: Type): Codegen;
2462 /** Wrappers for common types. */
2463 export const wrappers: { [k: string]: IWrapper };
2466 * From object converter part of an {@link IWrapper}.
2467 * @param object Plain object
2468 * @returns Message instance
2470 type WrapperFromObjectConverter = (this: Type, object: { [k: string]: any }) => Message<{}>;
2473 * To object converter part of an {@link IWrapper}.
2474 * @param message Message instance
2475 * @param [options] Conversion options
2476 * @returns Plain object
2478 type WrapperToObjectConverter = (this: Type, message: Message<{}>, options?: IConversionOptions) => { [k: string]: any };
2480 /** Common type wrapper part of {@link wrappers}. */
2481 export interface IWrapper {
2483 /** From object converter */
2484 fromObject?: WrapperFromObjectConverter;
2486 /** To object converter */
2487 toObject?: WrapperToObjectConverter;
2490 /** Wire format writer using `Uint8Array` if available, otherwise `Array`. */
2491 export class Writer {
2493 /** Constructs a new writer instance. */
2496 /** Current length. */
2499 /** Operations head. */
2500 public head: object;
2502 /** Operations tail */
2503 public tail: object;
2505 /** Linked forked states. */
2506 public states: (object|null);
2509 * Creates a new writer.
2510 * @returns A {@link BufferWriter} when Buffers are supported, otherwise a {@link Writer}
2512 public static create(): (BufferWriter|Writer);
2515 * Allocates a buffer of the specified size.
2516 * @param size Buffer size
2519 public static alloc(size: number): Uint8Array;
2522 * Writes an unsigned 32 bit value as a varint.
2523 * @param value Value to write
2526 public uint32(value: number): Writer;
2529 * Writes a signed 32 bit value as a varint.
2530 * @param value Value to write
2533 public int32(value: number): Writer;
2536 * Writes a 32 bit value as a varint, zig-zag encoded.
2537 * @param value Value to write
2540 public sint32(value: number): Writer;
2543 * Writes an unsigned 64 bit value as a varint.
2544 * @param value Value to write
2546 * @throws {TypeError} If `value` is a string and no long library is present.
2548 public uint64(value: (Long|number|string)): Writer;
2551 * Writes a signed 64 bit value as a varint.
2552 * @param value Value to write
2554 * @throws {TypeError} If `value` is a string and no long library is present.
2556 public int64(value: (Long|number|string)): Writer;
2559 * Writes a signed 64 bit value as a varint, zig-zag encoded.
2560 * @param value Value to write
2562 * @throws {TypeError} If `value` is a string and no long library is present.
2564 public sint64(value: (Long|number|string)): Writer;
2567 * Writes a boolish value as a varint.
2568 * @param value Value to write
2571 public bool(value: boolean): Writer;
2574 * Writes an unsigned 32 bit value as fixed 32 bits.
2575 * @param value Value to write
2578 public fixed32(value: number): Writer;
2581 * Writes a signed 32 bit value as fixed 32 bits.
2582 * @param value Value to write
2585 public sfixed32(value: number): Writer;
2588 * Writes an unsigned 64 bit value as fixed 64 bits.
2589 * @param value Value to write
2591 * @throws {TypeError} If `value` is a string and no long library is present.
2593 public fixed64(value: (Long|number|string)): Writer;
2596 * Writes a signed 64 bit value as fixed 64 bits.
2597 * @param value Value to write
2599 * @throws {TypeError} If `value` is a string and no long library is present.
2601 public sfixed64(value: (Long|number|string)): Writer;
2604 * Writes a float (32 bit).
2605 * @param value Value to write
2608 public float(value: number): Writer;
2611 * Writes a double (64 bit float).
2612 * @param value Value to write
2615 public double(value: number): Writer;
2618 * Writes a sequence of bytes.
2619 * @param value Buffer or base64 encoded string to write
2622 public bytes(value: (Uint8Array|string)): Writer;
2626 * @param value Value to write
2629 public string(value: string): Writer;
2632 * Forks this writer's state by pushing it to a stack.
2633 * Calling {@link Writer#reset|reset} or {@link Writer#ldelim|ldelim} resets the writer to the previous state.
2636 public fork(): Writer;
2639 * Resets this instance to the last state.
2642 public reset(): Writer;
2645 * Resets to the last state and appends the fork state's current write length as a varint followed by its operations.
2648 public ldelim(): Writer;
2651 * Finishes the write operation.
2652 * @returns Finished buffer
2654 public finish(): Uint8Array;
2657 /** Wire format writer using node buffers. */
2658 export class BufferWriter extends Writer {
2660 /** Constructs a new buffer writer instance. */
2664 * Allocates a buffer of the specified size.
2665 * @param size Buffer size
2668 public static alloc(size: number): Buffer;
2671 * Finishes the write operation.
2672 * @returns Finished buffer
2674 public finish(): Buffer;
2678 * Callback as used by {@link util.asPromise}.
2679 * @param error Error, if any
2680 * @param params Additional arguments
2682 type asPromiseCallback = (error: (Error|null), ...params: any[]) => void;
2685 * Appends code to the function's body or finishes generation.
2686 * @param [formatStringOrScope] Format string or, to finish the function, an object of additional scope variables, if any
2687 * @param [formatParams] Format parameters
2688 * @returns Itself or the generated function if finished
2689 * @throws {Error} If format parameter counts do not match
2691 type Codegen = (formatStringOrScope?: (string|{ [k: string]: any }), ...formatParams: any[]) => (Codegen|Function);
2694 * Event listener as used by {@link util.EventEmitter}.
2695 * @param args Arguments
2697 type EventEmitterListener = (...args: any[]) => void;
2700 * Node-style callback as used by {@link util.fetch}.
2701 * @param error Error, if any, otherwise `null`
2702 * @param [contents] File contents, if there hasn't been an error
2704 type FetchCallback = (error: Error, contents?: string) => void;
2706 /** Options as used by {@link util.fetch}. */
2707 export interface IFetchOptions {
2709 /** Whether expecting a binary response */
2712 /** If `true`, forces the use of XMLHttpRequest */
2717 * An allocator as used by {@link util.pool}.
2718 * @param size Buffer size
2721 type PoolAllocator = (size: number) => Uint8Array;
2724 * A slicer as used by {@link util.pool}.
2725 * @param start Start offset
2726 * @param end End offset
2727 * @returns Buffer slice
2729 type PoolSlicer = (this: Uint8Array, start: number, end: number) => Uint8Array;