UNPKG

6.43 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';
22import Long = require('long');
23export { Options, Long };
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 declare enum IdempotencyLevel {
87 IDEMPOTENCY_UNKNOWN = "IDEMPOTENCY_UNKNOWN",
88 NO_SIDE_EFFECTS = "NO_SIDE_EFFECTS",
89 IDEMPOTENT = "IDEMPOTENT"
90}
91export interface NamePart {
92 name_part: string;
93 is_extension: boolean;
94}
95export interface UninterpretedOption {
96 name?: NamePart[];
97 identifier_value?: string;
98 positive_int_value?: number;
99 negative_int_value?: number;
100 double_value?: number;
101 string_value?: string;
102 aggregate_value?: string;
103}
104export interface MethodOptions {
105 deprecated: boolean;
106 idempotency_level: IdempotencyLevel;
107 uninterpreted_option: UninterpretedOption[];
108 [k: string]: unknown;
109}
110export interface MethodDefinition<RequestType, ResponseType, OutputRequestType = RequestType, OutputResponseType = ResponseType> {
111 path: string;
112 requestStream: boolean;
113 responseStream: boolean;
114 requestSerialize: Serialize<RequestType>;
115 responseSerialize: Serialize<ResponseType>;
116 requestDeserialize: Deserialize<OutputRequestType>;
117 responseDeserialize: Deserialize<OutputResponseType>;
118 originalName?: string;
119 requestType: MessageTypeDefinition;
120 responseType: MessageTypeDefinition;
121 options: MethodOptions;
122}
123export interface ServiceDefinition {
124 [index: string]: MethodDefinition<object, object>;
125}
126export declare type AnyDefinition = ServiceDefinition | MessageTypeDefinition | EnumTypeDefinition;
127export interface PackageDefinition {
128 [index: string]: AnyDefinition;
129}
130/**
131 * Load a .proto file with the specified options.
132 * @param filename One or multiple file paths to load. Can be an absolute path
133 * or relative to an include path.
134 * @param options.keepCase Preserve field names. The default is to change them
135 * to camel case.
136 * @param options.longs The type that should be used to represent `long` values.
137 * Valid options are `Number` and `String`. Defaults to a `Long` object type
138 * from a library.
139 * @param options.enums The type that should be used to represent `enum` values.
140 * The only valid option is `String`. Defaults to the numeric value.
141 * @param options.bytes The type that should be used to represent `bytes`
142 * values. Valid options are `Array` and `String`. The default is to use
143 * `Buffer`.
144 * @param options.defaults Set default values on output objects. Defaults to
145 * `false`.
146 * @param options.arrays Set empty arrays for missing array values even if
147 * `defaults` is `false`. Defaults to `false`.
148 * @param options.objects Set empty objects for missing object values even if
149 * `defaults` is `false`. Defaults to `false`.
150 * @param options.oneofs Set virtual oneof properties to the present field's
151 * name
152 * @param options.json Represent Infinity and NaN as strings in float fields,
153 * and automatically decode google.protobuf.Any values.
154 * @param options.includeDirs Paths to search for imported `.proto` files.
155 */
156export declare function load(filename: string | string[], options?: Options): Promise<PackageDefinition>;
157export declare function loadSync(filename: string | string[], options?: Options): PackageDefinition;
158export declare function fromJSON(json: Protobuf.INamespace, options?: Options): PackageDefinition;
159export declare function loadFileDescriptorSetFromBuffer(descriptorSet: Buffer, options?: Options): PackageDefinition;
160export declare function loadFileDescriptorSetFromObject(descriptorSet: Parameters<typeof descriptor.FileDescriptorSet.fromObject>[0], options?: Options): PackageDefinition;