UNPKG

13.4 kBTypeScriptView Raw
1// Type definitions for ProtoBuf.js
2// Project: https://github.com/dcodeIO/ProtoBuf.js
3// Definitions by: Panu Horsmalahti <https://github.com/panuhorsmalahti>
4// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5
6/// <reference types="node" />
7
8declare namespace ProtoBuf {
9 // ==========
10 // protobufjs/src/ProtoBuf.js
11
12 var Builder: Builder;
13 var ByteBuffer: Buffer;
14 var Long: LongStatic;
15 var DotProto: DotProto;
16 var Reflect: Reflect;
17
18 // var Lang: Lang; TODO: implement interface Lang
19 // var Util: Util; TODO: implement interface Util
20
21 export function loadJson(json: string, builder?: ProtoBuilder,
22 filename?: string): ProtoBuilder;
23
24 export function loadJsonFile(filename: string,
25 callback?: (error: any, builder: ProtoBuilder) => void,
26 builder?: ProtoBuilder): ProtoBuilder;
27
28 export function loadProto(proto: string, builder?: ProtoBuilder,
29 filename?: string): ProtoBuilder;
30
31 export function loadProtoFile(filePath: string,
32 callback?: (error: any, builder: ProtoBuilder) => void,
33 builder?: ProtoBuilder): ProtoBuilder;
34
35 export function newBuilder(options?: {[key: string]: any}): ProtoBuilder;
36
37 export interface LongStatic {
38 new(low?: number, high?: number, unsigned?:boolean): Long;
39
40 MAX_UNSIGNED_VALUE: Long;
41 MAX_VALUE: Long;
42 MIN_VALUE: Long;
43 NEG_ONE: Long;
44 ONE: Long;
45 UONE: Long;
46 UZERO: Long;
47 ZERO: Long;
48
49 fromBits(lowBits: number, highBits: number, unsigned?: boolean): Long;
50 fromInt(value: number, unsigned?: boolean): Long;
51 fromNumber(value: number, unsigned?: boolean): Long;
52 fromString(str: string, unsigned?: boolean | number, radix?: number): Long;
53 fromValue(val: Long | number | string): Long;
54
55 isLong(obj: any): boolean;
56 }
57
58 // Based on https://github.com/dcodeIO/Long.js and https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/long/long.d.ts
59 export interface Long {
60 high: number;
61 low: number;
62 unsigned :boolean;
63
64 add(other: Long | number | string): Long;
65 and(other: Long | number | string): Long;
66 compare(other: Long | number | string): number;
67 div(divisor: Long | number | string): Long;
68 equals(other: Long | number | string): boolean;
69 getHighBits(): number;
70 getHighBitsUnsigned(): number;
71 getLowBits(): number;
72 getLowBitsUnsigned(): number;
73 getNumBitsAbs(): number;
74 greaterThan(other: Long | number | string): boolean;
75 greaterThanOrEqual(other: Long | number | string): boolean;
76 isEven(): boolean;
77 isNegative(): boolean;
78 isOdd(): boolean;
79 isPositive(): boolean;
80 isZero(): boolean;
81 lessThan(other: Long | number | string): boolean;
82 lessThanOrEqual(other: Long | number | string): boolean;
83 modulo(divisor: Long | number | string): Long;
84 multiply(multiplier: Long | number | string): Long;
85 negate(): Long;
86 not(): Long;
87 notEquals(other: Long | number | string): boolean;
88 or(other: Long | number | string): Long;
89 shiftLeft(numBits: number | Long): Long;
90 shiftRight(numBits: number | Long): Long;
91 shiftRightUnsigned(numBits: number | Long): Long;
92 subtract(other: Long | number | string): Long;
93 toInt(): number;
94 toNumber(): number;
95 toSigned(): Long;
96 toString(radix?: number): string;
97 toUnsigned(): Long;
98 xor(other: Long | number | string): Long;
99 }
100
101 // ==========
102 // protobufjs/src/ProtoBuf/Builder.js
103
104 export interface Builder {
105 new(options?: {[key: string]: any}): ProtoBuilder;
106 Message: Message;
107 Service: Service;
108 isValidMessage(def: {[key: string]: any}): boolean;
109 isValidMessageField(def: {[key: string]: any}): boolean;
110 isValidEnum(def: {[key: string]: any}): boolean;
111 isValidService(def: {[key: string]: any}): boolean;
112 isValidExtend(def: {[key: string]: any}): boolean;
113 }
114
115 /**
116 * TODO: Confirm that message needs no further implementation
117 */
118 export interface Message {
119 new(values?: {[key: string]: any}, var_args?: string[]): Message;
120 [field: string]: any;
121 }
122
123 /**
124 * TODO: Implement service interface
125 */
126 export interface Service {
127 new(rpcImpl?: Function): Service;
128 }
129
130
131 // ==========
132 // meta objects for constructing protobufs
133
134 export interface ProtoBuilder {
135 ns: ReflectNamespace;
136 ptr: ReflectNamespace;
137 resolved: boolean;
138 result: ProtoBuf;
139 files: string[];
140 importRoot: string;
141 options: {[key: string]: any};
142 syntax: string;
143 reset(): void;
144 define(pkg: string, options?: {[key: string]: any}): ProtoBuilder;
145 create(defs?: {[key: string]: any}[]): ProtoBuilder;
146 resolveAll(): void;
147 build(path?: string): ProtoBuf;
148 lookup(path?: string): ReflectT;
149 }
150
151 export interface ProtoBuf {
152 [package: string]: {[key: string]: MetaMessage | any};
153 }
154
155 export interface MetaMessage {
156 new(values?: {[key: string]: any}, var_args?: string[]): Message;
157 decode(buffer?: Buffer, enc?: string): Message;
158 decodeDelimited(buffer?: Buffer, enc?: string): Message;
159 decode64(str: string): Message;
160 decodeHex(str: string): Message;
161 }
162
163 // ==========
164 // protobufjs/src/ProtoBuf/DotProto.js
165
166 export interface DotProto {
167 Parser: Parser;
168 Tokenizer: Tokenizer;
169 }
170
171 export interface Parser {
172 new(proto: string): Parser;
173 tn: Tokenizer;
174 parse(): MetaProto;
175 toString(): string;
176 }
177
178 export interface Tokenizer {
179 new(proto: string): Tokenizer;
180 source: string;
181 index: number;
182 line: number;
183 stack: string[];
184 readingString: boolean;
185 stringEndsWith: string;
186 next(): string;
187 peek(): string;
188 toString(): string;
189 }
190
191 // ==========
192 // proto meta information returned by the Parser
193
194 export interface MetaProto {
195 package: string;
196 messages: ProtoMessage[];
197 enums: ProtoEnum[];
198 imports: string[];
199 options: {[key: string]: any};
200 services: ProtoService[];
201 }
202
203 export interface ProtoEnum {
204 name: string;
205 values: ProtoEnumValue;
206 options: {[key: string]: any};
207 }
208
209 export interface ProtoEnumValue {
210 name: string;
211 id: string;
212 }
213
214 export interface ProtoField {
215 rule: string;
216 options: {[key: string]: any};
217 type: string;
218 name: string;
219 id: number;
220 oneof?: string;
221 }
222
223 export interface ProtoMessage {
224 name: string;
225 isGroup?: boolean;
226 fields: ProtoField[];
227 enums: ProtoEnum[];
228 messages: ProtoMessage[];
229 options: {[key: string]: any};
230 oneofs: {[key: string]:number[]};
231 }
232
233 export interface ProtoRpcService {
234 request: string;
235 response: string;
236 options: {[key: string]: any};
237 }
238
239 export interface ProtoService {
240 name: string;
241 rpc: {[key: string]:ProtoRpcService};
242 options: {[key: string]: any};
243 }
244
245
246 // ==========
247 // protobufjs/src/ProtoBuf/Reflect.js
248
249 export interface Reflect {
250 T: ReflectT;
251 Namespace: ReflectNamespace;
252 Message: ReflectMessage;
253 Enum: ReflectEnum;
254 Extension: ReflectExtension;
255 Service: ReflectService;
256 }
257
258 export interface ReflectT {
259 new(builder?: ProtoBuilder, parent?: ReflectT, name?: string): ReflectT;
260 builder: ProtoBuilder;
261 parent: ReflectT;
262 name: string;
263 fqn(): string;
264 toString(includeClass?: boolean): string;
265 }
266
267 export interface ReflectNamespace extends ReflectT {
268 new(builder?: ProtoBuilder, parent?: ReflectNamespace, name?: string,
269 options?: {[key: string]: any}): ReflectNamespace;
270 className: string;
271 children: ReflectT[];
272 options: {[key: string]: any};
273 syntax: string;
274 getChildren(type?: ReflectT): ReflectT[];
275 addChild(child: ReflectT): void;
276 getChild(nameOrId?: string | number): ReflectT;
277 resolve(qn: string, excludeFields?: boolean): ReflectNamespace;
278 build(): ProtoBuf;
279 buildOpt(): {[key: string]: any};
280 getOption(name?: string): any;
281 }
282
283 export interface ReflectMessage extends ReflectNamespace {
284 new(builder?: ProtoBuilder, parent?: ReflectNamespace, name?: string,
285 options?: {[key: string]: any}, isGroup?: boolean): ReflectMessage;
286 Field: ReflectField; // NOTE: only for new ProtoBuf.Reflect.Message.Field();
287 ExtensionField: ReflectExtensionField; // NOTE: only for
288 // new ProtoBuf.Reflect.Message.ExtensionField();
289 OneOf: ReflectOneOf; // NOTE: only for new ProtoBuf.Reflect.Message.OneOf();
290 extensions: number[];
291 clazz(): MetaMessage;
292 isGroup: boolean;
293 build(rebuild?: boolean): MetaMessage|any;
294 encode(message: Message, buffer: Buffer, noVerify?: boolean): Buffer;
295 calculate(message: Message): number;
296 decode(buffer: Buffer, length?: number, expectedGroupEndId?: number): Message;
297 }
298
299 export interface ReflectEnum extends ReflectNamespace {
300 new(builder?: ProtoBuilder, parent?: ReflectT, name?: string,
301 options?: {[key: string]: any}): ReflectEnum;
302 Value: ReflectValue; // NOTE: only for new ProtoBuf.Reflect.Enum.Value();
303 object: {[key: string]:number};
304 build(): {[key: string]: any};
305 }
306
307 export interface ReflectExtension extends ReflectT {
308 new(builder?: ProtoBuilder, parent?: ReflectT, name?: string,
309 field?: ReflectField): ReflectExtension;
310 field: ReflectField;
311 }
312
313 export interface ReflectService extends ReflectNamespace {
314 new(): ReflectService;
315 Method: ReflectMethod; // NOTE: only for new ProtoBuf.Reflect.Service.Method();
316 RPCMethod: ReflectRPCMethod; // NOTE: only for new ProtoBuf.Reflect.Service.RPCMethod();
317 clazz(): Function;
318 build(rebuild?: boolean): Function|any;
319 }
320
321 // TODO: check that the runtime instance of this type reflects this definition
322 export interface ReflectField extends ReflectT {
323 new(builder: ProtoBuilder, message: ReflectMessage, rule: string, type: string,
324 name: string, id: number, options: {[key: string]: any}, oneof: ReflectOneOf): ReflectField;
325 className: string;
326 required: boolean;
327 repeated: boolean;
328 type: string | WireTuple;
329 resolvedType: ReflectT;
330 id: number;
331 options: {[key: string]: any};
332 defaultValue: any;
333 oneof: ReflectOneOf;
334 originalName: string;
335 build(): {[key: string]: any};
336 mkLong(value: any, unsigned?: boolean): number;
337 verifyValue(value: any, skipRepeated?: boolean): any;
338 encode(value: any, buffer: Buffer): Buffer;
339 encodeValue(value: any, buffer: Buffer): Buffer;
340 calculate(value: any): number;
341 calculateValue(value: any): number;
342 decode(wireType: number, buffer: Buffer, skipRepeated?: boolean): any;
343 }
344
345 export interface WireTuple {
346 name: string;
347 wireType: number;
348 }
349
350 // TODO: check that the runtime instance of this type reflects this definition
351 export interface ReflectExtensionField extends ReflectField {
352 new(builder: ProtoBuilder, message: ReflectMessage, rule: string, type: string,
353 name: string, id: number, options: {[key: string]: any}): ReflectExtensionField;
354 extension: ReflectExtension;
355 }
356
357 export interface ReflectOneOf extends ReflectT {
358 new(builder?: ProtoBuilder, message?: ReflectMessage, name?: string): ReflectOneOf;
359 fields: ReflectField[];
360 }
361
362 export interface ReflectValue extends ReflectT {
363 new(builder?: ProtoBuilder, enm?: ReflectEnum, name?: string, id?: number): ReflectValue;
364 className: string;
365 id: number;
366 }
367
368 export interface ReflectMethod extends ReflectT {
369 new(builder?: ProtoBuilder, svc?: ReflectService, name?: string,
370 options?: {[key: string]: any}): ReflectMethod;
371 className: string;
372 options: {[key: string]: any};
373 buildOpt(): {[key: string]: any};
374 }
375
376 export interface ReflectRPCMethod extends ReflectMethod {
377 new(builder?: ProtoBuilder, svc?: ReflectService, name?: string, request?: string,
378 response?: string, options?: {[key: string]: any}): ReflectRPCMethod;
379 requestName: string;
380 responseName: string;
381 resolvedRequestType: ReflectMessage;
382 resolvedResponseType: ReflectMessage;
383 }
384
385}
386
387declare module "protobufjs" {
388 export = ProtoBuf;
389}