UNPKG

7.88 kBPlain TextView Raw
1/*
2 * Copyright 2019 gRPC authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18import {
19 ClientDuplexStream,
20 ClientReadableStream,
21 ClientUnaryCall,
22 ClientWritableStream,
23 ServiceError,
24} from './call';
25import { CallCredentials, OAuth2Client } from './call-credentials';
26import { StatusObject } from './call-interface';
27import { Channel, ChannelImplementation } from './channel';
28import { CompressionAlgorithms } from './compression-algorithms';
29import { ConnectivityState } from './connectivity-state';
30import { ChannelCredentials } from './channel-credentials';
31import {
32 CallOptions,
33 Client,
34 ClientOptions,
35 CallInvocationTransformer,
36 CallProperties,
37 UnaryCallback,
38} from './client';
39import { LogVerbosity, Status, Propagate } from './constants';
40import * as logging from './logging';
41import {
42 Deserialize,
43 loadPackageDefinition,
44 makeClientConstructor,
45 MethodDefinition,
46 Serialize,
47 ServiceDefinition,
48} from './make-client';
49import { Metadata, MetadataOptions, MetadataValue } from './metadata';
50import {
51 Server,
52 UntypedHandleCall,
53 UntypedServiceImplementation,
54} from './server';
55import { KeyCertPair, ServerCredentials } from './server-credentials';
56import { StatusBuilder } from './status-builder';
57import {
58 handleBidiStreamingCall,
59 handleServerStreamingCall,
60 handleClientStreamingCall,
61 handleUnaryCall,
62 sendUnaryData,
63 ServerUnaryCall,
64 ServerReadableStream,
65 ServerWritableStream,
66 ServerDuplexStream,
67 ServerErrorResponse,
68} from './server-call';
69
70export { OAuth2Client };
71
72/**** Client Credentials ****/
73
74// Using assign only copies enumerable properties, which is what we want
75export const credentials = {
76 /**
77 * Combine a ChannelCredentials with any number of CallCredentials into a
78 * single ChannelCredentials object.
79 * @param channelCredentials The ChannelCredentials object.
80 * @param callCredentials Any number of CallCredentials objects.
81 * @return The resulting ChannelCredentials object.
82 */
83 combineChannelCredentials: (
84 channelCredentials: ChannelCredentials,
85 ...callCredentials: CallCredentials[]
86 ): ChannelCredentials => {
87 return callCredentials.reduce(
88 (acc, other) => acc.compose(other),
89 channelCredentials
90 );
91 },
92
93 /**
94 * Combine any number of CallCredentials into a single CallCredentials
95 * object.
96 * @param first The first CallCredentials object.
97 * @param additional Any number of additional CallCredentials objects.
98 * @return The resulting CallCredentials object.
99 */
100 combineCallCredentials: (
101 first: CallCredentials,
102 ...additional: CallCredentials[]
103 ): CallCredentials => {
104 return additional.reduce((acc, other) => acc.compose(other), first);
105 },
106
107 // from channel-credentials.ts
108 createInsecure: ChannelCredentials.createInsecure,
109 createSsl: ChannelCredentials.createSsl,
110 createFromSecureContext: ChannelCredentials.createFromSecureContext,
111
112 // from call-credentials.ts
113 createFromMetadataGenerator: CallCredentials.createFromMetadataGenerator,
114 createFromGoogleCredential: CallCredentials.createFromGoogleCredential,
115 createEmpty: CallCredentials.createEmpty,
116};
117
118/**** Metadata ****/
119
120export { Metadata, MetadataOptions, MetadataValue };
121
122/**** Constants ****/
123
124export {
125 LogVerbosity as logVerbosity,
126 Status as status,
127 ConnectivityState as connectivityState,
128 Propagate as propagate,
129 CompressionAlgorithms as compressionAlgorithms,
130 // TODO: Other constants as well
131};
132
133/**** Client ****/
134
135export {
136 Client,
137 ClientOptions,
138 loadPackageDefinition,
139 makeClientConstructor,
140 makeClientConstructor as makeGenericClientConstructor,
141 CallProperties,
142 CallInvocationTransformer,
143 ChannelImplementation as Channel,
144 Channel as ChannelInterface,
145 UnaryCallback as requestCallback,
146};
147
148/**
149 * Close a Client object.
150 * @param client The client to close.
151 */
152export const closeClient = (client: Client) => client.close();
153
154export const waitForClientReady = (
155 client: Client,
156 deadline: Date | number,
157 callback: (error?: Error) => void
158) => client.waitForReady(deadline, callback);
159
160/* Interfaces */
161
162export {
163 sendUnaryData,
164 ChannelCredentials,
165 CallCredentials,
166 Deadline,
167 Serialize as serialize,
168 Deserialize as deserialize,
169 ClientUnaryCall,
170 ClientReadableStream,
171 ClientWritableStream,
172 ClientDuplexStream,
173 CallOptions,
174 MethodDefinition,
175 StatusObject,
176 ServiceError,
177 ServerUnaryCall,
178 ServerReadableStream,
179 ServerWritableStream,
180 ServerDuplexStream,
181 ServerErrorResponse,
182 ServiceDefinition,
183 UntypedHandleCall,
184 UntypedServiceImplementation,
185};
186
187/**** Server ****/
188
189export {
190 handleBidiStreamingCall,
191 handleServerStreamingCall,
192 handleUnaryCall,
193 handleClientStreamingCall,
194};
195
196/* eslint-disable @typescript-eslint/no-explicit-any */
197export type Call =
198 | ClientUnaryCall
199 | ClientReadableStream<any>
200 | ClientWritableStream<any>
201 | ClientDuplexStream<any, any>;
202/* eslint-enable @typescript-eslint/no-explicit-any */
203
204/**** Unimplemented function stubs ****/
205
206/* eslint-disable @typescript-eslint/no-explicit-any */
207
208export const loadObject = (value: any, options: any): never => {
209 throw new Error(
210 'Not available in this library. Use @grpc/proto-loader and loadPackageDefinition instead'
211 );
212};
213
214export const load = (filename: any, format: any, options: any): never => {
215 throw new Error(
216 'Not available in this library. Use @grpc/proto-loader and loadPackageDefinition instead'
217 );
218};
219
220export const setLogger = (logger: Partial<Console>): void => {
221 logging.setLogger(logger);
222};
223
224export const setLogVerbosity = (verbosity: LogVerbosity): void => {
225 logging.setLoggerVerbosity(verbosity);
226};
227
228export { Server };
229export { ServerCredentials };
230export { KeyCertPair };
231
232export const getClientChannel = (client: Client) => {
233 return Client.prototype.getChannel.call(client);
234};
235
236export { StatusBuilder };
237
238export { Listener, InterceptingListener } from './call-interface';
239
240export {
241 Requester,
242 ListenerBuilder,
243 RequesterBuilder,
244 Interceptor,
245 InterceptorOptions,
246 InterceptorProvider,
247 InterceptingCall,
248 InterceptorConfigurationError,
249 NextCall,
250} from './client-interceptors';
251
252export {
253 GrpcObject,
254 ServiceClientConstructor,
255 ProtobufTypeDefinition,
256} from './make-client';
257
258export { ChannelOptions } from './channel-options';
259
260export { getChannelzServiceDefinition, getChannelzHandlers } from './channelz';
261
262export { addAdminServicesToServer } from './admin';
263
264import * as experimental from './experimental';
265export { experimental };
266
267import * as resolver_dns from './resolver-dns';
268import * as resolver_uds from './resolver-uds';
269import * as resolver_ip from './resolver-ip';
270import * as load_balancer_pick_first from './load-balancer-pick-first';
271import * as load_balancer_round_robin from './load-balancer-round-robin';
272import * as load_balancer_outlier_detection from './load-balancer-outlier-detection';
273import * as channelz from './channelz';
274import { Deadline } from './deadline';
275
276const clientVersion = require('../../package.json').version;
277
278(() => {
279 logging.trace(
280 LogVerbosity.DEBUG,
281 'index',
282 'Loading @grpc/grpc-js version ' + clientVersion
283 );
284 resolver_dns.setup();
285 resolver_uds.setup();
286 resolver_ip.setup();
287 load_balancer_pick_first.setup();
288 load_balancer_round_robin.setup();
289 load_balancer_outlier_detection.setup();
290 channelz.setup();
291})();