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 |
|
18 | declare module "grpc" {
|
19 | import { Message, Service as ProtobufService } from "protobufjs";
|
20 | import { Duplex, Readable, Writable } from "stream";
|
21 | import { SecureContext } from "tls";
|
22 |
|
23 | /**
|
24 | * Load a ProtoBuf.js object as a gRPC object.
|
25 | * @param value The ProtoBuf.js reflection object to load
|
26 | * @param options Options to apply to the loaded file
|
27 | * @return The resulting gRPC object.
|
28 | */
|
29 | export function loadObject<T = GrpcObject>(value: object, options?: LoadObjectOptions): T;
|
30 |
|
31 | /**
|
32 | * Options for loading proto object as gRPC object
|
33 | * @param {(number|string)=} [options.protobufjsVersion='detect'] 5 and 6
|
34 | * respectively indicate that an object from the corresponding version of
|
35 | * Protobuf.js is provided in the value argument. If the option is 'detect',
|
36 | * gRPC will guess what the version is based on the structure of the value.
|
37 | */
|
38 | export interface LoadObjectOptions {
|
39 | /**
|
40 | * Deserialize bytes values as base64 strings instead of Buffers.
|
41 | * Defaults to `false`.
|
42 | */
|
43 | binaryAsBase64?: boolean;
|
44 |
|
45 | /**
|
46 | * Deserialize long values as strings instead of objects.
|
47 | * Defaults to `true`.
|
48 | */
|
49 | longsAsStrings?: boolean;
|
50 |
|
51 | /**
|
52 | * Deserialize enum values as strings instead of numbers. Only works with
|
53 | * Protobuf.js 6 values.
|
54 | * Defaults to `true`.
|
55 | */
|
56 | enumsAsStrings?: boolean;
|
57 |
|
58 | /**
|
59 | * use the beta method argument order for client methods, with optional
|
60 | * arguments after the callback. This option is only a temporary stopgap
|
61 | * measure to smooth an API breakage. It is deprecated, and new code
|
62 | * should not use it.
|
63 | * Defaults to `false`
|
64 | */
|
65 | deprecatedArgumentOrder?: boolean;
|
66 |
|
67 | /**
|
68 | * 5 and 6 respectively indicate that an object from the corresponding
|
69 | * version of Protobuf.js is provided in the value argument. If the option
|
70 | * is 'detect', gRPC wll guess what the version is based on the structure
|
71 | * of the value.
|
72 | */
|
73 | protobufjsVersion?: 5 | 6 | "detect";
|
74 | }
|
75 |
|
76 | /**
|
77 | * Map from `.proto` file.
|
78 | * - Namespaces become maps from the names of their direct members to those member objects
|
79 | * - Service definitions become client constructors for clients for that service. They also
|
80 | * have a service member that can be used for constructing servers.
|
81 | * - Message definitions become Message constructors like those that ProtoBuf.js would create
|
82 | * - Enum definitions become Enum objects like those that ProtoBuf.js would create
|
83 | * - Anything else becomes the relevant reflection object that ProtoBuf.js would create
|
84 | */
|
85 | export interface GrpcObject {
|
86 | [name: string]: GrpcObject | typeof Client | Message;
|
87 | }
|
88 |
|
89 | /**
|
90 | * Load a gRPC object from a .proto file.
|
91 | * @param filename The file to load
|
92 | * @param format The file format to expect. Defaults to 'proto'
|
93 | * @param options Options to apply to the loaded file
|
94 | * @return The resulting gRPC object
|
95 | */
|
96 | export function load<T = GrpcObject>(filename: Filename, format?: "proto" | "json", options?: LoadOptions): T;
|
97 |
|
98 | /**
|
99 | * Load a gRPC package definition as a gRPC object hierarchy
|
100 | * @param packageDef The package definition object
|
101 | * @return The resulting gRPC object
|
102 | */
|
103 | export function loadPackageDefinition(packageDefinition: PackageDefinition): GrpcObject;
|
104 |
|
105 | /**
|
106 | * A filename
|
107 | */
|
108 | export type Filename = string | { root: string, file: string };
|
109 |
|
110 | /**
|
111 | * Options for loading proto file as gRPC object
|
112 | */
|
113 | export interface LoadOptions {
|
114 | /**
|
115 | * Load this file with field names in camel case instead of their original case.
|
116 | * Defaults to `false`.
|
117 | */
|
118 | convertFieldsToCamelCase?: boolean;
|
119 |
|
120 | /**
|
121 | * Deserialize bytes values as base64 strings instead of Buffers.
|
122 | * Defaults to `false`.
|
123 | */
|
124 | binaryAsBase64?: boolean;
|
125 |
|
126 | /**
|
127 | * Deserialize long values as strings instead of objects.
|
128 | * Defaults to `true`.
|
129 | */
|
130 | longsAsStrings?: boolean;
|
131 |
|
132 | /**
|
133 | * Use the beta method argument order for client methods, with optional
|
134 | * arguments after the callback. This option is only a temporary stopgap
|
135 | * measure to smooth an API breakage. It is deprecated, and new code
|
136 | * should not use it.
|
137 | * Defaults to `false`
|
138 | */
|
139 | deprecatedArgumentOrder?: boolean;
|
140 | }
|
141 |
|
142 | /**
|
143 | * Sets the logger function for the gRPC module. For debugging purposes, the C
|
144 | * core will log synchronously directly to stdout unless this function is
|
145 | * called. Note: the output format here is intended to be informational, and
|
146 | * is not guaranteed to stay the same in the future.
|
147 | * Logs will be directed to logger.error.
|
148 | * @param logger A Console-like object.
|
149 | */
|
150 | export function setLogger(logger: Console): void;
|
151 |
|
152 | /**
|
153 | * Sets the logger verbosity for gRPC module logging. The options are members
|
154 | * of the grpc.logVerbosity map.
|
155 | * @param verbosity The minimum severity to log
|
156 | */
|
157 | export function setLogVerbosity(verbosity: logVerbosity): void;
|
158 |
|
159 | /**
|
160 | * Server object that stores request handlers and delegates incoming requests to those handlers
|
161 | */
|
162 | export class Server {
|
163 | /**
|
164 | * Constructs a server object that stores request handlers and delegates
|
165 | * incoming requests to those handlers
|
166 | * @param options Options that should be passed to the internal server
|
167 | * implementation
|
168 | * ```
|
169 | * var server = new grpc.Server();
|
170 | * server.addProtoService(protobuf_service_descriptor, service_implementation);
|
171 | * server.bind('address:port', server_credential);
|
172 | * server.start();
|
173 | * ```
|
174 | */
|
175 | constructor(options?: object);
|
176 |
|
177 | /**
|
178 | * Start the server and begin handling requests
|
179 | */
|
180 | start(): void;
|
181 |
|
182 | /**
|
183 | * Registers a handler to handle the named method. Fails if there already is
|
184 | * a handler for the given method. Returns true on success
|
185 | * @param name The name of the method that the provided function should
|
186 | * handle/respond to.
|
187 | * @param handler Function that takes a stream of
|
188 | * request values and returns a stream of response values
|
189 | * @param serialize Serialization function for responses
|
190 | * @param deserialize Deserialization function for requests
|
191 | * @param type The streaming type of method that this handles
|
192 | * @return True if the handler was set. False if a handler was already
|
193 | * set for that name.
|
194 | */
|
195 | register<RequestType, ResponseType>(
|
196 | name: string,
|
197 | handler: handleCall<RequestType, ResponseType>,
|
198 | serialize: serialize<ResponseType>,
|
199 | deserialize: deserialize<RequestType>,
|
200 | type: string
|
201 | ): boolean;
|
202 |
|
203 | /**
|
204 | * Gracefully shuts down the server. The server will stop receiving new calls,
|
205 | * and any pending calls will complete. The callback will be called when all
|
206 | * pending calls have completed and the server is fully shut down. This method
|
207 | * is idempotent with itself and forceShutdown.
|
208 | * @param {function()} callback The shutdown complete callback
|
209 | */
|
210 | tryShutdown(callback: () => void): void;
|
211 |
|
212 | /**
|
213 | * Forcibly shuts down the server. The server will stop receiving new calls
|
214 | * and cancel all pending calls. When it returns, the server has shut down.
|
215 | * This method is idempotent with itself and tryShutdown, and it will trigger
|
216 | * any outstanding tryShutdown callbacks.
|
217 | */
|
218 | forceShutdown(): void;
|
219 |
|
220 | /**
|
221 | * Add a service to the server, with a corresponding implementation.
|
222 | * @param service The service descriptor
|
223 | * @param implementation Map of method names to method implementation
|
224 | * for the provided service.
|
225 | */
|
226 | addService<ImplementationType = UntypedServiceImplementation>(
|
227 | service: ServiceDefinition<ImplementationType>,
|
228 | implementation: ImplementationType
|
229 | ): void;
|
230 |
|
231 | /**
|
232 | * Add a proto service to the server, with a corresponding implementation
|
233 | * @deprecated Use `Server#addService` instead
|
234 | * @param service The proto service descriptor
|
235 | * @param implementation Map of method names to method implementation
|
236 | * for the provided service.
|
237 | */
|
238 | addProtoService<ImplementationType = UntypedServiceImplementation>(
|
239 | service: ServiceDefinition<ImplementationType>,
|
240 | implementation: ImplementationType
|
241 | ): void;
|
242 |
|
243 | /**
|
244 | * Binds the server to the given port, with SSL disabled if creds is an
|
245 | * insecure credentials object
|
246 | * @param port The port that the server should bind on, in the format
|
247 | * "address:port"
|
248 | * @param creds Server credential object to be used for SSL. Pass an
|
249 | * insecure credentials object for an insecure port.
|
250 | * @return The bound port number or 0 if the operation failed.
|
251 | */
|
252 | bind(port: string, creds: ServerCredentials): number;
|
253 |
|
254 | /**
|
255 | * Binds the server to the given port, with SSL disabled if creds is an
|
256 | * insecure credentials object. Provides the result asynchronously.
|
257 | * @param port The port that the server should bind on, in the format "address:port"
|
258 | * @param creds Server credential object to be used for
|
259 | * SSL. Pass an insecure credentials object for an insecure port.
|
260 | * @param callback Called with the result of attempting to bind a port
|
261 | * - error: If non-null, indicates that binding the port failed.
|
262 | * - port: The bound port number. If binding the port fails, this will be negative to match the output of bind.
|
263 | */
|
264 | bindAsync(port: string, creds: ServerCredentials, callback: (error: Error | null, port: number) => void): void;
|
265 | }
|
266 |
|
267 | /**
|
268 | * A type that servers as a default for an untyped service.
|
269 | */
|
270 | export type UntypedServiceImplementation = { [name: string]: handleCall<any, any> };
|
271 |
|
272 | /**
|
273 | * An object that completely defines a service.
|
274 | */
|
275 | export type ServiceDefinition<ImplementationType> = {
|
276 | readonly [I in keyof ImplementationType]: MethodDefinition<any, any>;
|
277 | }
|
278 |
|
279 | /**
|
280 | * An object that defines a protobuf type
|
281 | */
|
282 | export interface ProtobufTypeDefinition {
|
283 | format: string;
|
284 | type: object;
|
285 | fileDescriptorProtos: Buffer[];
|
286 | }
|
287 |
|
288 | /**
|
289 | * An object that defines a package containing multiple services
|
290 | */
|
291 | export type PackageDefinition = {
|
292 | readonly [fullyQualifiedName: string]: ServiceDefinition<any> | ProtobufTypeDefinition;
|
293 | }
|
294 |
|
295 | /**
|
296 | * An object that completely defines a service method signature.
|
297 | */
|
298 | export interface MethodDefinition<RequestType, ResponseType> {
|
299 | /**
|
300 | * The method's URL path
|
301 | */
|
302 | path: string;
|
303 | /**
|
304 | * Indicates whether the method accepts a stream of requests
|
305 | */
|
306 | requestStream: boolean;
|
307 | /**
|
308 | * Indicates whether the method returns a stream of responses
|
309 | */
|
310 | responseStream: boolean;
|
311 | /**
|
312 | * Serialization function for request values
|
313 | */
|
314 | requestSerialize: serialize<RequestType>;
|
315 | /**
|
316 | * Serialization function for response values
|
317 | */
|
318 | responseSerialize: serialize<ResponseType>;
|
319 | /**
|
320 | * Deserialization function for request data
|
321 | */
|
322 | requestDeserialize: deserialize<RequestType>;
|
323 | /**
|
324 | * Deserialization function for repsonse data
|
325 | */
|
326 | responseDeserialize: deserialize<ResponseType>;
|
327 | }
|
328 |
|
329 | type handleCall<RequestType, ResponseType> =
|
330 | handleUnaryCall<RequestType, ResponseType> |
|
331 | handleClientStreamingCall<RequestType, ResponseType> |
|
332 | handleServerStreamingCall<RequestType, ResponseType> |
|
333 | handleBidiStreamingCall<RequestType, ResponseType>;
|
334 |
|
335 | /**
|
336 | * User-provided method to handle unary requests on a server
|
337 | */
|
338 | type handleUnaryCall<RequestType, ResponseType> =
|
339 | (call: ServerUnaryCall<RequestType>, callback: sendUnaryData<ResponseType>) => void;
|
340 |
|
341 | /**
|
342 | * An EventEmitter. Used for unary calls.
|
343 | */
|
344 | export class ServerUnaryCall<RequestType> {
|
345 | /**
|
346 | * Indicates if the call has been cancelled
|
347 | */
|
348 | cancelled: boolean;
|
349 |
|
350 | /**
|
351 | * The request metadata from the client
|
352 | */
|
353 | metadata: Metadata;
|
354 |
|
355 | /**
|
356 | * The request message from the client
|
357 | */
|
358 | request: RequestType;
|
359 |
|
360 | private constructor();
|
361 |
|
362 | /**
|
363 | * Get the endpoint this call/stream is connected to.
|
364 | * @return The URI of the endpoint
|
365 | */
|
366 | getPeer(): string;
|
367 |
|
368 | /**
|
369 | * Send the initial metadata for a writable stream.
|
370 | * @param responseMetadata Metadata to send
|
371 | */
|
372 | sendMetadata(responseMetadata: Metadata): void;
|
373 | }
|
374 |
|
375 | /**
|
376 | * User provided method to handle client streaming methods on the server.
|
377 | */
|
378 | type handleClientStreamingCall<RequestType, ResponseType> =
|
379 | (call: ServerReadableStream<RequestType>, callback: sendUnaryData<ResponseType>) => void;
|
380 |
|
381 | /**
|
382 | * A stream that the server can read from. Used for calls that are streaming
|
383 | * from the client side.
|
384 | */
|
385 | export class ServerReadableStream<RequestType> extends Readable {
|
386 | /**
|
387 | * Indicates if the call has been cancelled
|
388 | */
|
389 | cancelled: boolean;
|
390 |
|
391 | /**
|
392 | * The request metadata from the client
|
393 | */
|
394 | metadata: Metadata;
|
395 |
|
396 | private constructor();
|
397 |
|
398 | /**
|
399 | * Get the endpoint this call/stream is connected to.
|
400 | * @return The URI of the endpoint
|
401 | */
|
402 | getPeer(): string;
|
403 |
|
404 | /**
|
405 | * Send the initial metadata for a writable stream.
|
406 | * @param responseMetadata Metadata to send
|
407 | */
|
408 | sendMetadata(responseMetadata: Metadata): void;
|
409 | }
|
410 |
|
411 | /**
|
412 | * User provided method to handle server streaming methods on the server.
|
413 | */
|
414 | type handleServerStreamingCall<RequestType, ResponseType> =
|
415 | (call: ServerWriteableStream<RequestType>) => void;
|
416 |
|
417 | /**
|
418 | * A stream that the server can write to. Used for calls that are streaming
|
419 | * from the server side.
|
420 | */
|
421 | export class ServerWriteableStream<RequestType> extends Writable {
|
422 | /**
|
423 | * Indicates if the call has been cancelled
|
424 | */
|
425 | cancelled: boolean;
|
426 |
|
427 | /**
|
428 | * The request metadata from the client
|
429 | */
|
430 | metadata: Metadata;
|
431 |
|
432 | /**
|
433 | * The request message from the client
|
434 | */
|
435 | request: RequestType;
|
436 |
|
437 | private constructor();
|
438 |
|
439 | /**
|
440 | * Get the endpoint this call/stream is connected to.
|
441 | * @return The URI of the endpoint
|
442 | */
|
443 | getPeer(): string;
|
444 |
|
445 | /**
|
446 | * Send the initial metadata for a writable stream.
|
447 | * @param responseMetadata Metadata to send
|
448 | */
|
449 | sendMetadata(responseMetadata: Metadata): void;
|
450 | }
|
451 |
|
452 | /**
|
453 | * User provided method to handle bidirectional streaming calls on the server.
|
454 | */
|
455 | type handleBidiStreamingCall<RequestType, ResponseType> =
|
456 | (call: ServerDuplexStream<RequestType, ResponseType>) => void;
|
457 |
|
458 | /**
|
459 | * A stream that the server can read from or write to. Used for calls
|
460 | * with duplex streaming.
|
461 | */
|
462 | export class ServerDuplexStream<RequestType, ResponseType> extends Duplex {
|
463 | /**
|
464 | * Indicates if the call has been cancelled
|
465 | */
|
466 | cancelled: boolean;
|
467 |
|
468 | /**
|
469 | * The request metadata from the client
|
470 | */
|
471 | metadata: Metadata;
|
472 |
|
473 | private constructor();
|
474 |
|
475 | /**
|
476 | * Get the endpoint this call/stream is connected to.
|
477 | * @return The URI of the endpoint
|
478 | */
|
479 | getPeer(): string;
|
480 |
|
481 | /**
|
482 | * Send the initial metadata for a writable stream.
|
483 | * @param responseMetadata Metadata to send
|
484 | */
|
485 | sendMetadata(responseMetadata: Metadata): void;
|
486 | }
|
487 |
|
488 | /**
|
489 | * A deserialization function
|
490 | * @param data The byte sequence to deserialize
|
491 | * @return The data deserialized as a value
|
492 | */
|
493 | type deserialize<T> = (data: Buffer) => T;
|
494 |
|
495 | /**
|
496 | * A serialization function
|
497 | * @param value The value to serialize
|
498 | * @return The value serialized as a byte sequence
|
499 | */
|
500 | type serialize<T> = (value: T) => Buffer;
|
501 |
|
502 | /**
|
503 | * Callback function passed to server handlers that handle methods with
|
504 | * unary responses.
|
505 | */
|
506 | type sendUnaryData<ResponseType> =
|
507 | (error: ServiceError | null, value: ResponseType | null, trailer?: Metadata, flags?: number) => void;
|
508 |
|
509 | /**
|
510 | * A class for storing metadata. Keys are normalized to lowercase ASCII.
|
511 | */
|
512 | export class Metadata {
|
513 | /**
|
514 | * Sets the given value for the given key by replacing any other values
|
515 | * associated with that key. Normalizes the key.
|
516 | * @param key The key to whose value should be set.
|
517 | * @param value The value to set. Must be a buffer if and only
|
518 | * if the normalized key ends with '-bin'.
|
519 | */
|
520 | set(key: string, value: MetadataValue): void;
|
521 |
|
522 | /**
|
523 | * Adds the given value for the given key by appending to a list of previous
|
524 | * values associated with that key. Normalizes the key.
|
525 | * @param key The key for which a new value should be appended.
|
526 | * @param value The value to add. Must be a buffer if and only
|
527 | * if the normalized key ends with '-bin'.
|
528 | */
|
529 | add(key: string, value: MetadataValue): void;
|
530 |
|
531 | /**
|
532 | * Removes the given key and any associated values. Normalizes the key.
|
533 | * @param key The key whose values should be removed.
|
534 | */
|
535 | remove(key: string): void;
|
536 |
|
537 | /**
|
538 | * Gets a list of all values associated with the key. Normalizes the key.
|
539 | * @param key The key whose value should be retrieved.
|
540 | * @return A list of values associated with the given key.
|
541 | */
|
542 | get(key: string): MetadataValue[];
|
543 |
|
544 | /**
|
545 | * Gets a plain object mapping each key to the first value associated with it.
|
546 | * This reflects the most common way that people will want to see metadata.
|
547 | * @return A key/value mapping of the metadata.
|
548 | */
|
549 | getMap(): { [key: string]: MetadataValue };
|
550 |
|
551 | /**
|
552 | * Clones the metadata object.
|
553 | * @return The newly cloned object.
|
554 | */
|
555 | clone(): Metadata;
|
556 | }
|
557 |
|
558 | export type MetadataValue = string | Buffer;
|
559 |
|
560 | /**
|
561 | * Represents the status of a completed request. If `code` is
|
562 | * `grpc.status.OK`, then the request has completed successfully.
|
563 | * Otherwise, the request has failed, `details` will contain a description of
|
564 | * the error. Either way, `metadata` contains the trailing response metadata
|
565 | * sent by the server when it finishes processing the call.
|
566 | */
|
567 | export interface StatusObject {
|
568 | /**
|
569 | * The error code, a key of `grpc.status`
|
570 | */
|
571 | code: status;
|
572 | /**
|
573 | * Human-readable description of the status
|
574 | */
|
575 | details: string;
|
576 | /**
|
577 | * Trailing metadata sent with the status, if applicable
|
578 | */
|
579 | metadata: Metadata;
|
580 | }
|
581 |
|
582 | /**
|
583 | * Describes how a request has failed. The member `message` will be the same as
|
584 | * `details` in `StatusObject`, and `code` and `metadata` are the
|
585 | * same as in that object.
|
586 | */
|
587 | export interface ServiceError extends Error {
|
588 | /**
|
589 | * The error code, a key of {@link grpc.status} that is not `grpc.status.OK`
|
590 | */
|
591 | code?: status;
|
592 | /**
|
593 | * Trailing metadata sent with the status, if applicable
|
594 | */
|
595 | metadata?: Metadata;
|
596 | /**
|
597 | * Original status details string
|
598 | */
|
599 | details?: string;
|
600 | }
|
601 |
|
602 | /**
|
603 | * ServerCredentials factories
|
604 | */
|
605 | export class ServerCredentials {
|
606 | /**
|
607 | * Create insecure server credentials
|
608 | * @return The ServerCredentials
|
609 | */
|
610 | static createInsecure(): ServerCredentials;
|
611 | /**
|
612 | * Create SSL server credentials
|
613 | * @param rootCerts Root CA certificates for validating client certificates
|
614 | * @param keyCertPairs A list of private key and certificate chain pairs to
|
615 | * be used for authenticating the server
|
616 | * @param checkClientCertificate Indicates that the server should request
|
617 | * and verify the client's certificates.
|
618 | * Defaults to `false`.
|
619 | * @return The ServerCredentials
|
620 | */
|
621 | static createSsl(rootCerts: Buffer | null, keyCertPairs: KeyCertPair[], checkClientCertificate?: boolean): ServerCredentials;
|
622 | }
|
623 |
|
624 | /**
|
625 | * A private key and certificate pair
|
626 | */
|
627 | export interface KeyCertPair {
|
628 | /**
|
629 | * The server's private key
|
630 | */
|
631 | private_key: Buffer;
|
632 |
|
633 | /**
|
634 | * The server's certificate chain
|
635 | */
|
636 | cert_chain: Buffer;
|
637 | }
|
638 |
|
639 | /**
|
640 | * Enum of status codes that gRPC can return
|
641 | */
|
642 | export enum status {
|
643 | /**
|
644 | * Not an error; returned on success
|
645 | */
|
646 | OK = 0,
|
647 | /**
|
648 | * The operation was cancelled (typically by the caller).
|
649 | */
|
650 | CANCELLED = 1,
|
651 | /**
|
652 | * Unknown error. An example of where this error may be returned is
|
653 | * if a status value received from another address space belongs to
|
654 | * an error-space that is not known in this address space. Also
|
655 | * errors raised by APIs that do not return enough error information
|
656 | * may be converted to this error.
|
657 | */
|
658 | UNKNOWN = 2,
|
659 | /**
|
660 | * Client specified an invalid argument. Note that this differs
|
661 | * from FAILED_PRECONDITION. INVALID_ARGUMENT indicates arguments
|
662 | * that are problematic regardless of the state of the system
|
663 | * (e.g., a malformed file name).
|
664 | */
|
665 | INVALID_ARGUMENT = 3,
|
666 | /**
|
667 | * Deadline expired before operation could complete. For operations
|
668 | * that change the state of the system, this error may be returned
|
669 | * even if the operation has completed successfully. For example, a
|
670 | * successful response from a server could have been delayed long
|
671 | * enough for the deadline to expire.
|
672 | */
|
673 | DEADLINE_EXCEEDED = 4,
|
674 | /**
|
675 | * Some requested entity (e.g., file or directory) was not found.
|
676 | */
|
677 | NOT_FOUND = 5,
|
678 | /**
|
679 | * Some entity that we attempted to create (e.g., file or directory)
|
680 | * already exists.
|
681 | */
|
682 | ALREADY_EXISTS = 6,
|
683 | /**
|
684 | * The caller does not have permission to execute the specified
|
685 | * operation. PERMISSION_DENIED must not be used for rejections
|
686 | * caused by exhausting some resource (use RESOURCE_EXHAUSTED
|
687 | * instead for those errors). PERMISSION_DENIED must not be
|
688 | * used if the caller can not be identified (use UNAUTHENTICATED
|
689 | * instead for those errors).
|
690 | */
|
691 | PERMISSION_DENIED = 7,
|
692 | /**
|
693 | * Some resource has been exhausted, perhaps a per-user quota, or
|
694 | * perhaps the entire file system is out of space.
|
695 | */
|
696 | RESOURCE_EXHAUSTED = 8,
|
697 | /**
|
698 | * Operation was rejected because the system is not in a state
|
699 | * required for the operation's execution. For example, directory
|
700 | * to be deleted may be non-empty, an rmdir operation is applied to
|
701 | * a non-directory, etc.
|
702 | *
|
703 | * A litmus test that may help a service implementor in deciding
|
704 | * between FAILED_PRECONDITION, ABORTED, and UNAVAILABLE:
|
705 | *
|
706 | * - Use UNAVAILABLE if the client can retry just the failing call.
|
707 | * - Use ABORTED if the client should retry at a higher-level
|
708 | * (e.g., restarting a read-modify-write sequence).
|
709 | * - Use FAILED_PRECONDITION if the client should not retry until
|
710 | * the system state has been explicitly fixed. E.g., if an "rmdir"
|
711 | * fails because the directory is non-empty, FAILED_PRECONDITION
|
712 | * should be returned since the client should not retry unless
|
713 | * they have first fixed up the directory by deleting files from it.
|
714 | * - Use FAILED_PRECONDITION if the client performs conditional
|
715 | * REST Get/Update/Delete on a resource and the resource on the
|
716 | * server does not match the condition. E.g., conflicting
|
717 | * read-modify-write on the same resource.
|
718 | */
|
719 | FAILED_PRECONDITION = 9,
|
720 | /**
|
721 | * The operation was aborted, typically due to a concurrency issue
|
722 | * like sequencer check failures, transaction aborts, etc.
|
723 | *
|
724 | * See litmus test above for deciding between FAILED_PRECONDITION,
|
725 | * ABORTED, and UNAVAILABLE.
|
726 | */
|
727 | ABORTED = 10,
|
728 | /**
|
729 | * Operation was attempted past the valid range. E.g., seeking or
|
730 | * reading past end of file.
|
731 | *
|
732 | * Unlike INVALID_ARGUMENT, this error indicates a problem that may
|
733 | * be fixed if the system state changes. For example, a 32-bit file
|
734 | * system will generate INVALID_ARGUMENT if asked to read at an
|
735 | * offset that is not in the range [0,2^32-1], but it will generate
|
736 | * OUT_OF_RANGE if asked to read from an offset past the current
|
737 | * file size.
|
738 | *
|
739 | * There is a fair bit of overlap between FAILED_PRECONDITION and
|
740 | * OUT_OF_RANGE. We recommend using OUT_OF_RANGE (the more specific
|
741 | * error) when it applies so that callers who are iterating through
|
742 | * a space can easily look for an OUT_OF_RANGE error to detect when
|
743 | * they are done.
|
744 | */
|
745 | OUT_OF_RANGE = 11,
|
746 | /**
|
747 | * Operation is not implemented or not supported/enabled in this service.
|
748 | */
|
749 | UNIMPLEMENTED = 12,
|
750 | /**
|
751 | * Internal errors. Means some invariants expected by underlying
|
752 | * system has been broken. If you see one of these errors,
|
753 | * something is very broken.
|
754 | */
|
755 | INTERNAL = 13,
|
756 | /**
|
757 | * The service is currently unavailable. This is a most likely a
|
758 | * transient condition and may be corrected by retrying with
|
759 | * a backoff.
|
760 | *
|
761 | * See litmus test above for deciding between FAILED_PRECONDITION,
|
762 | * ABORTED, and UNAVAILABLE.
|
763 | */
|
764 | UNAVAILABLE = 14,
|
765 | /**
|
766 | * Unrecoverable data loss or corruption.
|
767 | */
|
768 | DATA_LOSS = 15,
|
769 | /**
|
770 | * The request does not have valid authentication credentials for the
|
771 | * operation.
|
772 | */
|
773 | UNAUTHENTICATED = 16,
|
774 | }
|
775 |
|
776 | /**
|
777 | * Propagation flags: these can be bitwise or-ed to form the propagation option
|
778 | * for calls.
|
779 | *
|
780 | * Users are encouraged to write propagation masks as deltas from the default.
|
781 | * i.e. write `grpc.propagate.DEFAULTS & ~grpc.propagate.DEADLINE` to disable
|
782 | * deadline propagation.
|
783 | */
|
784 | export enum propagate {
|
785 | DEADLINE,
|
786 | CENSUS_STATS_CONTEXT,
|
787 | CENSUS_TRACING_CONTEXT,
|
788 | CANCELLATION,
|
789 | DEFAULTS,
|
790 | }
|
791 |
|
792 | /**
|
793 | * Call error constants. Call errors almost always indicate bugs in the gRPC
|
794 | * library, and these error codes are mainly useful for finding those bugs.
|
795 | */
|
796 | export enum callError {
|
797 | OK,
|
798 | ERROR,
|
799 | NOT_ON_SERVER,
|
800 | NOT_ON_CLIENT,
|
801 | ALREADY_INVOKED,
|
802 | NOT_INVOKED,
|
803 | ALREADY_FINISHED,
|
804 | TOO_MANY_OPERATIONS,
|
805 | INVALID_FLAGS,
|
806 | INVALID_METADATA,
|
807 | INVALID_MESSAGE,
|
808 | NOT_SERVER_COMPLETION_QUEUE,
|
809 | BATCH_TOO_BIG,
|
810 | PAYLOAD_TYPE_MISMATCH,
|
811 | }
|
812 |
|
813 | /**
|
814 | * Write flags: these can be bitwise or-ed to form write options that modify
|
815 | * how data is written.
|
816 | */
|
817 | export enum writeFlags {
|
818 | /**
|
819 | * Hint that the write may be buffered and need not go out on the wire
|
820 | * immediately. GRPC is free to buffer the message until the next non-buffered
|
821 | * write, or until writes_done, but it need not buffer completely or at all.
|
822 | */
|
823 | BUFFER_HINT = 1,
|
824 | /**
|
825 | * Force compression to be disabled for a particular write
|
826 | */
|
827 | NO_COMPRESS,
|
828 | }
|
829 |
|
830 | /**
|
831 | * Log verbosity constants. Maps setting names to code numbers.
|
832 | */
|
833 | export enum logVerbosity {
|
834 | DEBUG,
|
835 | INFO,
|
836 | ERROR,
|
837 | }
|
838 |
|
839 | /**
|
840 | * A certificate as received by the checkServerIdentity callback.
|
841 | */
|
842 | export interface Certificate {
|
843 | /**
|
844 | * The raw certificate in DER form.
|
845 | */
|
846 | raw: Buffer;
|
847 | }
|
848 |
|
849 | /**
|
850 | * A callback that will receive the expected hostname and presented peer
|
851 | * certificate as parameters. The callback should return an error to
|
852 | * indicate that the presented certificate is considered invalid and
|
853 | * otherwise returned undefined.
|
854 | */
|
855 | export type CheckServerIdentityCallback = (hostname: string, cert: Certificate) => Error | undefined;
|
856 |
|
857 | /**
|
858 | * Additional peer verification options that can be set when creating
|
859 | * SSL credentials.
|
860 | */
|
861 | export interface VerifyOptions {
|
862 | /**
|
863 | * If set, this callback will be invoked after the usual hostname verification
|
864 | * has been performed on the peer certificate.
|
865 | */
|
866 | checkServerIdentity?: CheckServerIdentityCallback;
|
867 | }
|
868 |
|
869 | /**
|
870 | * Credentials module
|
871 | *
|
872 | * This module contains factory methods for two different credential types:
|
873 | * CallCredentials and ChannelCredentials. ChannelCredentials are things like
|
874 | * SSL credentials that can be used to secure a connection, and are used to
|
875 | * construct a Client object. CallCredentials generally modify metadata, so they
|
876 | * can be attached to an individual method call.
|
877 | *
|
878 | * CallCredentials can be composed with other CallCredentials to create
|
879 | * CallCredentials. ChannelCredentials can be composed with CallCredentials
|
880 | * to create ChannelCredentials. No combined credential can have more than
|
881 | * one ChannelCredentials.
|
882 | *
|
883 | * For example, to create a client secured with SSL that uses Google
|
884 | * default application credentials to authenticate:
|
885 | *
|
886 | * ```
|
887 | * var channel_creds = credentials.createSsl(root_certs);
|
888 | * (new GoogleAuth()).getApplicationDefault(function(err, credential) {
|
889 | * var call_creds = credentials.createFromGoogleCredential(credential);
|
890 | * var combined_creds = credentials.combineChannelCredentials(
|
891 | * channel_creds, call_creds);
|
892 | * var client = new Client(address, combined_creds);
|
893 | * });
|
894 | * ```
|
895 | */
|
896 | export const credentials: {
|
897 | /**
|
898 | * Create an SSL Credentials object. If using a client-side certificate, both
|
899 | * the second and third arguments must be passed.
|
900 | * @param rootCerts The root certificate data
|
901 | * @param privateKey The client certificate private key, if applicable
|
902 | * @param certChain The client certificate cert chain, if applicable
|
903 | * @param verifyOptions Additional peer verification options, if desired
|
904 | * @return The SSL Credentials object
|
905 | */
|
906 | createSsl(rootCerts?: Buffer, privateKey?: Buffer, certChain?: Buffer, verifyOptions?: VerifyOptions): ChannelCredentials;
|
907 |
|
908 | /**
|
909 | * Create a gRPC credentials object from a metadata generation function. This
|
910 | * function gets the service URL and a callback as parameters. The error
|
911 | * passed to the callback can optionally have a 'code' value attached to it,
|
912 | * which corresponds to a status code that this library uses.
|
913 | * @param metadataGenerator The function that generates metadata
|
914 | * @return The credentials object
|
915 | */
|
916 | createFromMetadataGenerator(metadataGenerator: metadataGenerator): CallCredentials;
|
917 |
|
918 | /**
|
919 | * Create a gRPC credential from a Google credential object.
|
920 | * @param googleCredential The Google credential object to use
|
921 | * @return The resulting credentials object
|
922 | */
|
923 | createFromGoogleCredential(googleCredential: GoogleOAuth2Client): CallCredentials;
|
924 |
|
925 | /**
|
926 | * Combine a ChannelCredentials with any number of CallCredentials into a single
|
927 | * ChannelCredentials object.
|
928 | * @param channelCredential The ChannelCredentials to start with
|
929 | * @param credentials The CallCredentials to compose
|
930 | * @return A credentials object that combines all of the input credentials
|
931 | */
|
932 | combineChannelCredentials(channelCredential: ChannelCredentials, ...credentials: CallCredentials[]): ChannelCredentials;
|
933 |
|
934 | /**
|
935 | * Combine any number of CallCredentials into a single CallCredentials object
|
936 | * @param credentials The CallCredentials to compose
|
937 | * @return A credentials object that combines all of the input credentials
|
938 | */
|
939 | combineCallCredentials(...credentials: CallCredentials[]): CallCredentials;
|
940 |
|
941 | /**
|
942 | * Create an insecure credentials object. This is used to create a channel that
|
943 | * does not use SSL. This cannot be composed with anything.
|
944 | * @return The insecure credentials object
|
945 | */
|
946 | createInsecure(): ChannelCredentials;
|
947 | };
|
948 |
|
949 | /**
|
950 | * Metadata generator function.
|
951 | */
|
952 | export type metadataGenerator = (params: { service_url: string }, callback: (error: Error | null, metadata?: Metadata) => void) => void;
|
953 |
|
954 | /**
|
955 | * This cannot be constructed directly. Instead, instances of this class should
|
956 | * be created using the factory functions in `grpc.credentials`
|
957 | */
|
958 | export interface ChannelCredentials {
|
959 | /**
|
960 | * Returns a copy of this object with the included set of per-call credentials
|
961 | * expanded to include callCredentials.
|
962 | * @param callCredentials A CallCredentials object to associate with this
|
963 | * instance.
|
964 | */
|
965 | compose(callCredentials: CallCredentials): ChannelCredentials;
|
966 | }
|
967 |
|
968 | /**
|
969 | * This cannot be constructed directly. Instead, instances of this class should
|
970 | * be created using the factory functions in `grpc.credentials`
|
971 | */
|
972 | export interface CallCredentials {
|
973 | /**
|
974 | * Asynchronously generates a new Metadata object.
|
975 | * @param options Options used in generating the Metadata object.
|
976 | */
|
977 | generateMetadata(options: object): Promise<Metadata>;
|
978 |
|
979 | /**
|
980 | * Creates a new CallCredentials object from properties of both this and
|
981 | * another CallCredentials object. This object's metadata generator will be
|
982 | * called first.
|
983 | * @param callCredentials The other CallCredentials object.
|
984 | */
|
985 | compose(callCredentials: CallCredentials): CallCredentials;
|
986 | }
|
987 |
|
988 | /**
|
989 | * This is the required interface from the OAuth2Client object
|
990 | * from https://github.com/google/google-auth-library-nodejs lib.
|
991 | * The definition is copied from `ts/lib/auth/oauth2client.ts`
|
992 | */
|
993 | export interface GoogleOAuth2Client {
|
994 | getRequestMetadata(optUri: string, metadataCallback: (err: Error, headers: any) => void): void;
|
995 | }
|
996 |
|
997 | /**
|
998 | * Creates a constructor for a client with the given methods, as specified in
|
999 | * the methods argument. The resulting class will have an instance method for
|
1000 | * each method in the service, which is a partial application of one of the
|
1001 | * `grpc.Client` request methods, depending on `requestSerialize`
|
1002 | * and `responseSerialize`, with the `method`, `serialize`, and `deserialize`
|
1003 | * arguments predefined.
|
1004 | * @param methods An object mapping method names to method attributes
|
1005 | * @param serviceName The fully qualified name of the service
|
1006 | * @param classOptions An options object.
|
1007 | * @return New client constructor, which is a subclass of `grpc.Client`, and
|
1008 | * has the same arguments as that constructor.
|
1009 | */
|
1010 | export function makeGenericClientConstructor(
|
1011 | methods: ServiceDefinition<any>,
|
1012 | serviceName: string,
|
1013 | classOptions: GenericClientOptions,
|
1014 | ): typeof Client;
|
1015 |
|
1016 | /**
|
1017 | * Options for generic client constructor.
|
1018 | */
|
1019 | export interface GenericClientOptions {
|
1020 | /**
|
1021 | * Indicates that the old argument order should be used for methods, with
|
1022 | * optional arguments at the end instead of the callback at the end. This
|
1023 | * option is only a temporary stopgap measure to smooth an API breakage.
|
1024 | * It is deprecated, and new code should not use it.
|
1025 | */
|
1026 | deprecatedArgumentOrder?: boolean;
|
1027 | }
|
1028 |
|
1029 | /**
|
1030 | * Create a client with the given methods
|
1031 | */
|
1032 | export class Client {
|
1033 | /**
|
1034 | * A generic gRPC client. Primarily useful as a base class for generated clients
|
1035 | * @param address Server address to connect to
|
1036 | * @param credentials Credentials to use to connect to the server
|
1037 | * @param options Options to apply to channel creation
|
1038 | */
|
1039 | constructor(address: string, credentials: ChannelCredentials, options?: object)
|
1040 |
|
1041 | /**
|
1042 | * Make a unary request to the given method, using the given serialize
|
1043 | * and deserialize functions, with the given argument.
|
1044 | * @param method The name of the method to request
|
1045 | * @param serialize The serialization function for inputs
|
1046 | * @param deserialize The deserialization function for outputs
|
1047 | * @param argument The argument to the call. Should be serializable with
|
1048 | * serialize
|
1049 | * @param metadata Metadata to add to the call
|
1050 | * @param options Options map
|
1051 | * @param callback The callback to for when the response is received
|
1052 | * @return An event emitter for stream related events
|
1053 | */
|
1054 | makeUnaryRequest<RequestType, ResponseType>(
|
1055 | method: string,
|
1056 | serialize: serialize<RequestType>,
|
1057 | deserialize: deserialize<ResponseType>,
|
1058 | argument: RequestType | null,
|
1059 | metadata: Metadata | null,
|
1060 | options: CallOptions | null,
|
1061 | callback: requestCallback<ResponseType>,
|
1062 | ): ClientUnaryCall;
|
1063 |
|
1064 | /**
|
1065 | * Make a client stream request to the given method, using the given serialize
|
1066 | * and deserialize functions, with the given argument.
|
1067 | * @param method The name of the method to request
|
1068 | * @param serialize The serialization function for inputs
|
1069 | * @param deserialize The deserialization function for outputs
|
1070 | * @param metadata Array of metadata key/value pairs to add to the call
|
1071 | * @param options Options map
|
1072 | * @param callback The callback to for when the response is received
|
1073 | * @return An event emitter for stream related events
|
1074 | */
|
1075 | makeClientStreamRequest<RequestType, ResponseType>(
|
1076 | method: string,
|
1077 | serialize: serialize<RequestType>,
|
1078 | deserialize: deserialize<ResponseType>,
|
1079 | metadata: Metadata | null,
|
1080 | options: CallOptions | null,
|
1081 | callback: requestCallback<ResponseType>,
|
1082 | ): ClientWritableStream<RequestType>;
|
1083 |
|
1084 | /**
|
1085 | * Make a server stream request to the given method, with the given serialize
|
1086 | * and deserialize function, using the given argument
|
1087 | * @param method The name of the method to request
|
1088 | * @param serialize The serialization function for inputs
|
1089 | * @param deserialize The deserialization function for outputs
|
1090 | * @param argument The argument to the call. Should be serializable with
|
1091 | * serialize
|
1092 | * @param metadata Array of metadata key/value pairs to add to the call
|
1093 | * @param options Options map
|
1094 | * @return An event emitter for stream related events
|
1095 | */
|
1096 | makeServerStreamRequest<RequestType, ResponseType>(
|
1097 | method: string,
|
1098 | serialize: serialize<RequestType>,
|
1099 | deserialize: deserialize<ResponseType>,
|
1100 | argument: RequestType,
|
1101 | metadata?: Metadata | null,
|
1102 | options?: CallOptions | null,
|
1103 | ): ClientReadableStream<RequestType>;
|
1104 |
|
1105 | /**
|
1106 | * Make a bidirectional stream request with this method on the given channel.
|
1107 | * @param method The name of the method to request
|
1108 | * @param serialize The serialization function for inputs
|
1109 | * @param deserialize The deserialization
|
1110 | * function for outputs
|
1111 | * @param metadata Array of metadata key/value
|
1112 | * pairs to add to the call
|
1113 | * @param options Options map
|
1114 | * @return An event emitter for stream related events
|
1115 | */
|
1116 | makeBidiStreamRequest<RequestType, ResponseType>(
|
1117 | method: string,
|
1118 | serialize: serialize<RequestType>,
|
1119 | deserialize: deserialize<ResponseType>,
|
1120 | metadata?: Metadata | null,
|
1121 | options?: CallOptions | null,
|
1122 | ): ClientDuplexStream<RequestType, ResponseType>;
|
1123 |
|
1124 | /**
|
1125 | * Close this client.
|
1126 | */
|
1127 | close(): void;
|
1128 |
|
1129 | /**
|
1130 | * Return the underlying channel object for the specified client
|
1131 | * @return The channel
|
1132 | */
|
1133 | getChannel(): Channel;
|
1134 |
|
1135 | /**
|
1136 | * Wait for the client to be ready. The callback will be called when the
|
1137 | * client has successfully connected to the server, and it will be called
|
1138 | * with an error if the attempt to connect to the server has unrecoverablly
|
1139 | * failed or if the deadline expires. This function will make the channel
|
1140 | * start connecting if it has not already done so.
|
1141 | * @param deadline When to stop waiting for a connection.
|
1142 | * @param callback The callback to call when done attempting to connect.
|
1143 | */
|
1144 | waitForReady(deadline: Deadline, callback: (error: Error | null) => void): void;
|
1145 | }
|
1146 |
|
1147 | /**
|
1148 | * Options that can be set on a call.
|
1149 | */
|
1150 | export interface CallOptions {
|
1151 | /**
|
1152 | * The deadline for the entire call to complete.
|
1153 | */
|
1154 | deadline?: Deadline;
|
1155 | /**
|
1156 | * Server hostname to set on the call. Only meaningful if different from
|
1157 | * the server address used to construct the client.
|
1158 | */
|
1159 | host?: string;
|
1160 | /**
|
1161 | * Parent call. Used in servers when making a call as part of the process
|
1162 | * of handling a call. Used to propagate some information automatically,
|
1163 | * as specified by propagate_flags.
|
1164 | */
|
1165 | parent?: Call;
|
1166 | /**
|
1167 | * Indicates which properties of a parent call should propagate to this
|
1168 | * call. Bitwise combination of flags in `grpc.propagate`.
|
1169 | */
|
1170 | propagate_flags: number;
|
1171 | /**
|
1172 | * The credentials that should be used to make this particular call.
|
1173 | */
|
1174 | credentials: CallCredentials;
|
1175 | /**
|
1176 | * Additional custom call options. These can be used to pass additional
|
1177 | * data per-call to client interceptors
|
1178 | */
|
1179 | [key: string]: any;
|
1180 | }
|
1181 |
|
1182 | /**
|
1183 | * The deadline of an operation. If it is a date, the deadline is reached at
|
1184 | * the date and time specified. If it is a finite number, it is treated as
|
1185 | * a number of milliseconds since the Unix Epoch. If it is Infinity, the
|
1186 | * deadline will never be reached. If it is -Infinity, the deadline has already
|
1187 | * passed.
|
1188 | */
|
1189 | export type Deadline = number | Date;
|
1190 |
|
1191 | /**
|
1192 | * Any client call type
|
1193 | */
|
1194 | type Call =
|
1195 | ClientUnaryCall |
|
1196 | ClientReadableStream<any> |
|
1197 | ClientWritableStream<any> |
|
1198 | ClientDuplexStream<any, any>;
|
1199 |
|
1200 | /**
|
1201 | * An EventEmitter. Used for unary calls.
|
1202 | */
|
1203 | export class ClientUnaryCall {
|
1204 | private constructor();
|
1205 |
|
1206 | /**
|
1207 | * Cancel the ongoing call. Results in the call ending with a CANCELLED status,
|
1208 | * unless it has already ended with some other status.
|
1209 | */
|
1210 | cancel(): void;
|
1211 |
|
1212 | /**
|
1213 | * Get the endpoint this call/stream is connected to.
|
1214 | * @return The URI of the endpoint
|
1215 | */
|
1216 | getPeer(): string;
|
1217 | }
|
1218 |
|
1219 | /**
|
1220 | * A stream that the client can read from. Used for calls that are streaming
|
1221 | * from the server side.
|
1222 | */
|
1223 | export class ClientReadableStream<ResponseType> extends Readable {
|
1224 | private constructor();
|
1225 |
|
1226 | /**
|
1227 | * Cancel the ongoing call. Results in the call ending with a CANCELLED status,
|
1228 | * unless it has already ended with some other status.
|
1229 | */
|
1230 | cancel(): void;
|
1231 |
|
1232 | /**
|
1233 | * Get the endpoint this call/stream is connected to.
|
1234 | * @return The URI of the endpoint
|
1235 | */
|
1236 | getPeer(): string;
|
1237 | }
|
1238 |
|
1239 | /**
|
1240 | * A stream that the client can write to. Used for calls that are streaming from
|
1241 | * the client side.
|
1242 | */
|
1243 | export class ClientWritableStream<RequestType> extends Writable {
|
1244 | private constructor();
|
1245 |
|
1246 | /**
|
1247 | * Write a message to the request stream. If serializing the argument fails,
|
1248 | * the call will be cancelled and the stream will end with an error.
|
1249 | * @param message The message to write. Must be a valid argument to the
|
1250 | * serialize function of the corresponding method
|
1251 | * @param flags Flags to modify how the message is written
|
1252 | * @param callback Callback for when this chunk of data is flushed
|
1253 | * @return As defined for [Writable]{ external:Writable}
|
1254 | */
|
1255 | write(message: RequestType, flags?: any&writeFlags, callback?: Function): boolean;
|
1256 |
|
1257 | /**
|
1258 | * Cancel the ongoing call. Results in the call ending with a CANCELLED status,
|
1259 | * unless it has already ended with some other status.
|
1260 | */
|
1261 | cancel(): void;
|
1262 |
|
1263 | /**
|
1264 | * Get the endpoint this call/stream is connected to.
|
1265 | * @return The URI of the endpoint
|
1266 | */
|
1267 | getPeer(): string;
|
1268 | }
|
1269 |
|
1270 | /**
|
1271 | * A stream that the client can read from or write to. Used for calls with
|
1272 | * duplex streaming.
|
1273 | */
|
1274 | export class ClientDuplexStream<RequestType, ResponseType> extends Duplex {
|
1275 | private constructor();
|
1276 |
|
1277 | /**
|
1278 | * Write a message to the request stream. If serializing the argument fails,
|
1279 | * the call will be cancelled and the stream will end with an error.
|
1280 | * @param message The message to write. Must be a valid argument to the
|
1281 | * serialize function of the corresponding method
|
1282 | * @param flags Flags to modify how the message is written
|
1283 | * @param callback Callback for when this chunk of data is flushed
|
1284 | * @return As defined for [Writable]{ external:Writable}
|
1285 | */
|
1286 | write(message: RequestType, flags?: any&writeFlags, callback?: Function): boolean;
|
1287 |
|
1288 | /**
|
1289 | * Cancel the ongoing call. Results in the call ending with a CANCELLED status,
|
1290 | * unless it has already ended with some other status.
|
1291 | */
|
1292 | cancel(): void;
|
1293 |
|
1294 | /**
|
1295 | * Get the endpoint this call/stream is connected to.
|
1296 | * @return The URI of the endpoint
|
1297 | */
|
1298 | getPeer(): string;
|
1299 | }
|
1300 |
|
1301 | /**
|
1302 | * Client request callback
|
1303 | * @param error The error, if the call failed
|
1304 | * @param value The response value, if the call succeeded
|
1305 | */
|
1306 | export type requestCallback<ResponseType> =
|
1307 | (error: ServiceError | null, value?: ResponseType) => void;
|
1308 |
|
1309 | /**
|
1310 | * Return the underlying channel object for the specified client
|
1311 | * @see grpc.Client#getChannel
|
1312 | * @param client The client
|
1313 | * @return The channel
|
1314 | */
|
1315 | export function getClientChannel(client: Client): Channel;
|
1316 |
|
1317 | /**
|
1318 | * Wait for the client to be ready. The callback will be called when the
|
1319 | * client has successfully connected to the server, and it will be called
|
1320 | * with an error if the attempt to connect to the server has unrecoverably
|
1321 | * failed or if the deadline expires. This function will make the channel
|
1322 | * start connecting if it has not already done so.
|
1323 | * @see grpc.Client#waitForReady
|
1324 | * @param client The client to wait on
|
1325 | * @param deadline When to stop waiting for a connection. Pass Infinity to
|
1326 | * wait forever.
|
1327 | * @param callback The callback to call when done attempting to connect.
|
1328 | */
|
1329 | export function waitForClientReady(client: Client, deadline: Deadline, callback: (error: Error | null) => void): void;
|
1330 |
|
1331 | /**
|
1332 | * Close client.
|
1333 | * @param clientObj The client to close
|
1334 | */
|
1335 | export function closeClient(clientObj: Client): void;
|
1336 |
|
1337 | /**
|
1338 | * A builder for gRPC status objects
|
1339 | */
|
1340 | export class StatusBuilder {
|
1341 | constructor()
|
1342 |
|
1343 | /**
|
1344 | * Adds a status code to the builder
|
1345 | * @param code The status code
|
1346 | */
|
1347 | withCode(code: number): this;
|
1348 |
|
1349 | /**
|
1350 | * Adds details to the builder
|
1351 | * @param details A status message
|
1352 | */
|
1353 | withDetails(details: string): this;
|
1354 |
|
1355 | /**
|
1356 | * Adds metadata to the builder
|
1357 | * @param metadata The gRPC status metadata
|
1358 | */
|
1359 | withMetadata(metadata: Metadata): this;
|
1360 |
|
1361 | /**
|
1362 | * Builds the status object
|
1363 | * @return A gRPC status
|
1364 | */
|
1365 | build(): StatusObject;
|
1366 | }
|
1367 |
|
1368 | export type MetadataListener = (metadata: Metadata, next: Function) => void;
|
1369 |
|
1370 | export type MessageListener = (message: any, next: Function) => void;
|
1371 |
|
1372 | export type StatusListener = (status: StatusObject, next: Function) => void;
|
1373 |
|
1374 | export interface Listener {
|
1375 | onReceiveMetadata?: MetadataListener;
|
1376 | onReceiveMessage?: MessageListener;
|
1377 | onReceiveStatus?: StatusListener;
|
1378 | }
|
1379 |
|
1380 | /**
|
1381 | * A builder for listener interceptors
|
1382 | */
|
1383 | export class ListenerBuilder {
|
1384 | constructor();
|
1385 |
|
1386 | /**
|
1387 | * Adds onReceiveMetadata method to the builder
|
1388 | * @param onReceiveMetadata A listener method for receiving metadata
|
1389 | */
|
1390 | withOnReceiveMetadata(onReceiveMetadata: MetadataListener): this;
|
1391 |
|
1392 | /**
|
1393 | * Adds onReceiveMessage method to the builder
|
1394 | * @param onReceiveMessage A listener method for receiving message
|
1395 | */
|
1396 | withOnReceiveMessage(onReceiveMessage: MessageListener): this;
|
1397 |
|
1398 | /**
|
1399 | * Adds onReceiveStatus method to the builder
|
1400 | * @param onReceiveStatus A listener method for receiving status
|
1401 | */
|
1402 | withOnReceiveStatus(onReceiveStatus: StatusListener): this;
|
1403 |
|
1404 | /**
|
1405 | * Builds the call listener
|
1406 | */
|
1407 | build(): Listener;
|
1408 | }
|
1409 |
|
1410 | export type MetadataRequester = (metadata: Metadata, listener: Listener, next: Function) => void;
|
1411 |
|
1412 | export type MessageRequester = (message: any, next: Function) => void;
|
1413 |
|
1414 | export type CloseRequester = (next: Function) => void;
|
1415 |
|
1416 | export type CancelRequester = (next: Function) => void;
|
1417 |
|
1418 | export type GetPeerRequester = (next: Function) => string;
|
1419 |
|
1420 | export interface Requester {
|
1421 | start?: MetadataRequester;
|
1422 | sendMessage?: MessageRequester;
|
1423 | halfClose?: CloseRequester;
|
1424 | cancel?: CancelRequester;
|
1425 | getPeer?: GetPeerRequester;
|
1426 | }
|
1427 |
|
1428 | /**
|
1429 | * A builder for the outbound methods of an interceptor
|
1430 | */
|
1431 | export class RequesterBuilder {
|
1432 | constructor();
|
1433 |
|
1434 | /**
|
1435 | * Add a metadata requester to the builder
|
1436 | * @param start A requester method for handling metadata
|
1437 | */
|
1438 | withStart(start: MetadataRequester): this;
|
1439 |
|
1440 | /**
|
1441 | * Add a message requester to the builder.
|
1442 | * @param sendMessage A requester method for handling
|
1443 | * messages.
|
1444 | */
|
1445 | withSendMessage(sendMessage: MessageRequester): this;
|
1446 |
|
1447 | /**
|
1448 | * Add a close requester to the builder.
|
1449 | * @param halfClose A requester method for handling client
|
1450 | * close.
|
1451 | */
|
1452 | withHalfClose(halfClose: CloseRequester): this;
|
1453 |
|
1454 | /**
|
1455 | * Add a cancel requester to the builder.
|
1456 | * @param cancel A requester method for handling `cancel`
|
1457 | */
|
1458 | withCancel(cancel: CancelRequester): this;
|
1459 |
|
1460 | /**
|
1461 | * Builds the requester's interceptor methods.
|
1462 | */
|
1463 | build(): Requester;
|
1464 | }
|
1465 |
|
1466 | /**
|
1467 | * A chainable gRPC call proxy which will delegate to an optional requester
|
1468 | * object. By default, interceptor methods will chain to nextCall. If a
|
1469 | * requester is provided which implements an interceptor method, that
|
1470 | * requester method will be executed as part of the chain.
|
1471 | * operations.
|
1472 | */
|
1473 | export class InterceptingCall {
|
1474 | /**
|
1475 | * @param next_Call The next call in the chain
|
1476 | * @param requester Interceptor methods to handle request
|
1477 | */
|
1478 | constructor(nextCall: InterceptingCall|null, requester?: Requester);
|
1479 |
|
1480 | /**
|
1481 | * Starts a call through the outbound interceptor chain and adds an element to
|
1482 | * the reciprocal inbound listener chain.
|
1483 | */
|
1484 | start(metadata: Metadata, listener: Listener): void;
|
1485 |
|
1486 | /**
|
1487 | * Pass a message through the interceptor chain.
|
1488 | */
|
1489 | sendMessage(message: any): void;
|
1490 |
|
1491 | /**
|
1492 | * Run a close operation through the interceptor chain
|
1493 | */
|
1494 | halfClose(): void;
|
1495 |
|
1496 | /**
|
1497 | * Run a cancel operation through the interceptor chain
|
1498 | */
|
1499 | cancel(): void;
|
1500 |
|
1501 | /**
|
1502 | * Run a cancelWithStatus operation through the interceptor chain.
|
1503 | * @param status
|
1504 | * @param message
|
1505 | */
|
1506 | cancelWithStatus(status: StatusObject, message: string): void;
|
1507 |
|
1508 | /**
|
1509 | * Pass a getPeer call down to the base gRPC call (should not be intercepted)
|
1510 | */
|
1511 | getPeer(): object;
|
1512 |
|
1513 | /**
|
1514 | * For streaming calls, we need to transparently pass the stream's context
|
1515 | * through the interceptor chain. Passes the context between InterceptingCalls
|
1516 | * but hides it from any requester implementations.
|
1517 | * @param context Carries objects needed for streaming operations.
|
1518 | * @param message The message to send.
|
1519 | */
|
1520 | sendMessageWithContext(context: object, message: any): void;
|
1521 |
|
1522 | /**
|
1523 | * For receiving streaming messages, we need to seed the base interceptor with
|
1524 | * the streaming context to create a RECV_MESSAGE batch.
|
1525 | * @param context Carries objects needed for streaming operations
|
1526 | */
|
1527 | recvMessageWithContext(context: object): void;
|
1528 | }
|
1529 | export enum connectivityState {
|
1530 | IDLE = 0,
|
1531 | CONNECTING = 1,
|
1532 | READY = 2,
|
1533 | TRANSIENT_FAILURE = 3,
|
1534 | SHUTDOWN = 4
|
1535 | }
|
1536 |
|
1537 | export class Channel {
|
1538 | /**
|
1539 | * This constructor API is almost identical to the Client constructor,
|
1540 | * except that some of the options for the Client constructor are not valid
|
1541 | * here.
|
1542 | * @param target The address of the server to connect to
|
1543 | * @param credentials Channel credentials to use when connecting
|
1544 | * @param options A map of channel options that will be passed to the core
|
1545 | */
|
1546 | constructor(target: string, credentials: ChannelCredentials, options: {[key:string]: string|number});
|
1547 | /**
|
1548 | * Close the channel. This has the same functionality as the existing grpc.Client.prototype.close
|
1549 | */
|
1550 | close(): void;
|
1551 | /**
|
1552 | * Return the target that this channel connects to
|
1553 | */
|
1554 | getTarget(): string;
|
1555 | /**
|
1556 | * Get the channel's current connectivity state.
|
1557 | * @param tryToConnect If true, the channel will start connecting if it is
|
1558 | * idle. Otherwise, idle channels will only start connecting when a
|
1559 | * call starts.
|
1560 | */
|
1561 | getConnectivityState(tryToConnect: boolean): connectivityState;
|
1562 | /**
|
1563 | * Watch for connectivity state changes.
|
1564 | * @param currentState The state to watch for transitions from. This should
|
1565 | * always be populated by calling getConnectivityState immediately
|
1566 | * before.
|
1567 | * @param deadline A deadline for waiting for a state change
|
1568 | * @param callback Called with no error when a state change, or with an
|
1569 | * error if the deadline passes without a state change.
|
1570 | */
|
1571 | watchConnectivityState(currentState: connectivityState, deadline: Date|number, callback: (error?: Error) => void): void;
|
1572 | /**
|
1573 | * Create a call object. Call is an opaque type that is used by the Client
|
1574 | * and Server classes. This function is called by the gRPC library when
|
1575 | * starting a request. Implementers should return an instance of Call that
|
1576 | * is returned from calling createCall on an instance of the provided
|
1577 | * Channel class.
|
1578 | * @param method The full method string to request.
|
1579 | * @param deadline The call deadline
|
1580 | * @param host A host string override for making the request
|
1581 | * @param parentCall A server call to propagate some information from
|
1582 | * @param propagateFlags A bitwise combination of elements of grpc.propagate
|
1583 | * that indicates what information to propagate from parentCall.
|
1584 | */
|
1585 | createCall(method: string, deadline: Date|number, host: string|null, parentCall: Call|null, propagateFlags: number|null): Call;
|
1586 | }
|
1587 | }
|