UNPKG

5.74 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright 2018 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18/// <reference types="node" />
19import * as Protobuf from 'protobufjs';
20import * as descriptor from 'protobufjs/ext/descriptor';
21import { Options } from './util';
22export { Long } from 'long';
23export { Options };
24/**
25 * This type exists for use with code generated by the proto-loader-gen-types
26 * tool. This type should be used with another interface, e.g.
27 * MessageType & AnyExtension for an object that is converted to or from a
28 * google.protobuf.Any message.
29 * For example, when processing an Any message:
30 *
31 * ```ts
32 * if (isAnyExtension(message)) {
33 * switch (message['@type']) {
34 * case TYPE1_URL:
35 * handleType1(message as AnyExtension & Type1);
36 * break;
37 * case TYPE2_URL:
38 * handleType2(message as AnyExtension & Type2);
39 * break;
40 * // ...
41 * }
42 * }
43 * ```
44 */
45export interface AnyExtension {
46 /**
47 * The fully qualified name of the message type that this object represents,
48 * possibly including a URL prefix.
49 */
50 '@type': string;
51}
52export declare function isAnyExtension(obj: object): obj is AnyExtension;
53declare module 'protobufjs' {
54 interface Type {
55 toDescriptor(protoVersion: string): Protobuf.Message<descriptor.IDescriptorProto> & descriptor.IDescriptorProto;
56 }
57 interface RootConstructor {
58 new (options?: Options): Root;
59 fromDescriptor(descriptorSet: descriptor.IFileDescriptorSet | Protobuf.Reader | Uint8Array): Root;
60 fromJSON(json: Protobuf.INamespace, root?: Root): Root;
61 }
62 interface Root {
63 toDescriptor(protoVersion: string): Protobuf.Message<descriptor.IFileDescriptorSet> & descriptor.IFileDescriptorSet;
64 }
65 interface Enum {
66 toDescriptor(protoVersion: string): Protobuf.Message<descriptor.IEnumDescriptorProto> & descriptor.IEnumDescriptorProto;
67 }
68}
69export interface Serialize<T> {
70 (value: T): Buffer;
71}
72export interface Deserialize<T> {
73 (bytes: Buffer): T;
74}
75export interface ProtobufTypeDefinition {
76 format: string;
77 type: object;
78 fileDescriptorProtos: Buffer[];
79}
80export interface MessageTypeDefinition extends ProtobufTypeDefinition {
81 format: 'Protocol Buffer 3 DescriptorProto';
82}
83export interface EnumTypeDefinition extends ProtobufTypeDefinition {
84 format: 'Protocol Buffer 3 EnumDescriptorProto';
85}
86export interface MethodDefinition<RequestType, ResponseType, OutputRequestType = RequestType, OutputResponseType = ResponseType> {
87 path: string;
88 requestStream: boolean;
89 responseStream: boolean;
90 requestSerialize: Serialize<RequestType>;
91 responseSerialize: Serialize<ResponseType>;
92 requestDeserialize: Deserialize<OutputRequestType>;
93 responseDeserialize: Deserialize<OutputResponseType>;
94 originalName?: string;
95 requestType: MessageTypeDefinition;
96 responseType: MessageTypeDefinition;
97}
98export interface ServiceDefinition {
99 [index: string]: MethodDefinition<object, object>;
100}
101export declare type AnyDefinition = ServiceDefinition | MessageTypeDefinition | EnumTypeDefinition;
102export interface PackageDefinition {
103 [index: string]: AnyDefinition;
104}
105/**
106 * Load a .proto file with the specified options.
107 * @param filename One or multiple file paths to load. Can be an absolute path
108 * or relative to an include path.
109 * @param options.keepCase Preserve field names. The default is to change them
110 * to camel case.
111 * @param options.longs The type that should be used to represent `long` values.
112 * Valid options are `Number` and `String`. Defaults to a `Long` object type
113 * from a library.
114 * @param options.enums The type that should be used to represent `enum` values.
115 * The only valid option is `String`. Defaults to the numeric value.
116 * @param options.bytes The type that should be used to represent `bytes`
117 * values. Valid options are `Array` and `String`. The default is to use
118 * `Buffer`.
119 * @param options.defaults Set default values on output objects. Defaults to
120 * `false`.
121 * @param options.arrays Set empty arrays for missing array values even if
122 * `defaults` is `false`. Defaults to `false`.
123 * @param options.objects Set empty objects for missing object values even if
124 * `defaults` is `false`. Defaults to `false`.
125 * @param options.oneofs Set virtual oneof properties to the present field's
126 * name
127 * @param options.json Represent Infinity and NaN as strings in float fields,
128 * and automatically decode google.protobuf.Any values.
129 * @param options.includeDirs Paths to search for imported `.proto` files.
130 */
131export declare function load(filename: string | string[], options?: Options): Promise<PackageDefinition>;
132export declare function loadSync(filename: string | string[], options?: Options): PackageDefinition;
133export declare function fromJSON(json: Protobuf.INamespace, options?: Options): PackageDefinition;
134export declare function loadFileDescriptorSetFromBuffer(descriptorSet: Buffer, options?: Options): PackageDefinition;
135export declare function loadFileDescriptorSetFromObject(descriptorSet: Parameters<typeof descriptor.FileDescriptorSet.fromObject>[0], options?: Options): PackageDefinition;