UNPKG

8.25 kBTypeScriptView Raw
1/// <reference types="node" />
2import { EventEmitter } from 'events';
3import * as http2 from 'http2';
4import { Duplex, Readable, Writable } from 'stream';
5import { Deadline, StatusObject, PartialStatusObject } from './call-stream';
6import { Deserialize, Serialize } from './make-client';
7import { Metadata } from './metadata';
8import { ObjectReadable, ObjectWritable } from './object-stream';
9import { ChannelOptions } from './channel-options';
10export declare type ServerStatusResponse = Partial<StatusObject>;
11export declare type ServerErrorResponse = ServerStatusResponse & Error;
12export declare type ServerSurfaceCall = {
13 cancelled: boolean;
14 readonly metadata: Metadata;
15 getPeer(): string;
16 sendMetadata(responseMetadata: Metadata): void;
17 getDeadline(): Deadline;
18 getPath(): string;
19} & EventEmitter;
20export declare type ServerUnaryCall<RequestType, ResponseType> = ServerSurfaceCall & {
21 request: RequestType;
22};
23export declare type ServerReadableStream<RequestType, ResponseType> = ServerSurfaceCall & ObjectReadable<RequestType>;
24export declare type ServerWritableStream<RequestType, ResponseType> = ServerSurfaceCall & ObjectWritable<ResponseType> & {
25 request: RequestType;
26 end: (metadata?: Metadata) => void;
27};
28export declare type ServerDuplexStream<RequestType, ResponseType> = ServerSurfaceCall & ObjectReadable<RequestType> & ObjectWritable<ResponseType> & {
29 end: (metadata?: Metadata) => void;
30};
31export declare class ServerUnaryCallImpl<RequestType, ResponseType> extends EventEmitter implements ServerUnaryCall<RequestType, ResponseType> {
32 private call;
33 metadata: Metadata;
34 request: RequestType;
35 cancelled: boolean;
36 constructor(call: Http2ServerCallStream<RequestType, ResponseType>, metadata: Metadata, request: RequestType);
37 getPeer(): string;
38 sendMetadata(responseMetadata: Metadata): void;
39 getDeadline(): Deadline;
40 getPath(): string;
41}
42export declare class ServerReadableStreamImpl<RequestType, ResponseType> extends Readable implements ServerReadableStream<RequestType, ResponseType> {
43 private call;
44 metadata: Metadata;
45 deserialize: Deserialize<RequestType>;
46 cancelled: boolean;
47 constructor(call: Http2ServerCallStream<RequestType, ResponseType>, metadata: Metadata, deserialize: Deserialize<RequestType>, encoding: string);
48 _read(size: number): void;
49 getPeer(): string;
50 sendMetadata(responseMetadata: Metadata): void;
51 getDeadline(): Deadline;
52 getPath(): string;
53}
54export declare class ServerWritableStreamImpl<RequestType, ResponseType> extends Writable implements ServerWritableStream<RequestType, ResponseType> {
55 private call;
56 metadata: Metadata;
57 serialize: Serialize<ResponseType>;
58 request: RequestType;
59 cancelled: boolean;
60 private trailingMetadata;
61 constructor(call: Http2ServerCallStream<RequestType, ResponseType>, metadata: Metadata, serialize: Serialize<ResponseType>, request: RequestType);
62 getPeer(): string;
63 sendMetadata(responseMetadata: Metadata): void;
64 getDeadline(): Deadline;
65 getPath(): string;
66 _write(chunk: ResponseType, encoding: string, callback: (...args: any[]) => void): void;
67 _final(callback: Function): void;
68 end(metadata?: any): this;
69}
70export declare class ServerDuplexStreamImpl<RequestType, ResponseType> extends Duplex implements ServerDuplexStream<RequestType, ResponseType> {
71 private call;
72 metadata: Metadata;
73 serialize: Serialize<ResponseType>;
74 deserialize: Deserialize<RequestType>;
75 cancelled: boolean;
76 private trailingMetadata;
77 constructor(call: Http2ServerCallStream<RequestType, ResponseType>, metadata: Metadata, serialize: Serialize<ResponseType>, deserialize: Deserialize<RequestType>, encoding: string);
78 getPeer(): string;
79 sendMetadata(responseMetadata: Metadata): void;
80 getDeadline(): Deadline;
81 getPath(): string;
82 end(metadata?: any): this;
83}
84export declare type sendUnaryData<ResponseType> = (error: ServerErrorResponse | ServerStatusResponse | null, value?: ResponseType | null, trailer?: Metadata, flags?: number) => void;
85export declare type handleUnaryCall<RequestType, ResponseType> = (call: ServerUnaryCall<RequestType, ResponseType>, callback: sendUnaryData<ResponseType>) => void;
86export declare type handleClientStreamingCall<RequestType, ResponseType> = (call: ServerReadableStream<RequestType, ResponseType>, callback: sendUnaryData<ResponseType>) => void;
87export declare type handleServerStreamingCall<RequestType, ResponseType> = (call: ServerWritableStream<RequestType, ResponseType>) => void;
88export declare type handleBidiStreamingCall<RequestType, ResponseType> = (call: ServerDuplexStream<RequestType, ResponseType>) => void;
89export declare type HandleCall<RequestType, ResponseType> = handleUnaryCall<RequestType, ResponseType> | handleClientStreamingCall<RequestType, ResponseType> | handleServerStreamingCall<RequestType, ResponseType> | handleBidiStreamingCall<RequestType, ResponseType>;
90export interface UnaryHandler<RequestType, ResponseType> {
91 func: handleUnaryCall<RequestType, ResponseType>;
92 serialize: Serialize<ResponseType>;
93 deserialize: Deserialize<RequestType>;
94 type: HandlerType;
95 path: string;
96}
97export interface ClientStreamingHandler<RequestType, ResponseType> {
98 func: handleClientStreamingCall<RequestType, ResponseType>;
99 serialize: Serialize<ResponseType>;
100 deserialize: Deserialize<RequestType>;
101 type: HandlerType;
102 path: string;
103}
104export interface ServerStreamingHandler<RequestType, ResponseType> {
105 func: handleServerStreamingCall<RequestType, ResponseType>;
106 serialize: Serialize<ResponseType>;
107 deserialize: Deserialize<RequestType>;
108 type: HandlerType;
109 path: string;
110}
111export interface BidiStreamingHandler<RequestType, ResponseType> {
112 func: handleBidiStreamingCall<RequestType, ResponseType>;
113 serialize: Serialize<ResponseType>;
114 deserialize: Deserialize<RequestType>;
115 type: HandlerType;
116 path: string;
117}
118export declare type Handler<RequestType, ResponseType> = UnaryHandler<RequestType, ResponseType> | ClientStreamingHandler<RequestType, ResponseType> | ServerStreamingHandler<RequestType, ResponseType> | BidiStreamingHandler<RequestType, ResponseType>;
119export declare type HandlerType = 'bidi' | 'clientStream' | 'serverStream' | 'unary';
120export declare class Http2ServerCallStream<RequestType, ResponseType> extends EventEmitter {
121 private stream;
122 private handler;
123 private options;
124 cancelled: boolean;
125 deadlineTimer: NodeJS.Timer | null;
126 private statusSent;
127 private deadline;
128 private wantTrailers;
129 private metadataSent;
130 private canPush;
131 private isPushPending;
132 private bufferedMessages;
133 private messagesToPush;
134 private maxSendMessageSize;
135 private maxReceiveMessageSize;
136 constructor(stream: http2.ServerHttp2Stream, handler: Handler<RequestType, ResponseType>, options: ChannelOptions);
137 private checkCancelled;
138 private getDecompressedMessage;
139 sendMetadata(customMetadata?: Metadata): void;
140 receiveMetadata(headers: http2.IncomingHttpHeaders): Metadata;
141 receiveUnaryMessage(encoding: string, next: (err: Partial<ServerStatusResponse> | null, request?: RequestType) => void): void;
142 private safeDeserializeMessage;
143 serializeMessage(value: ResponseType): Buffer;
144 deserializeMessage(bytes: Buffer): RequestType;
145 sendUnaryMessage(err: ServerErrorResponse | ServerStatusResponse | null, value?: ResponseType | null, metadata?: Metadata | null, flags?: number): Promise<void>;
146 sendStatus(statusObj: PartialStatusObject): void;
147 sendError(error: ServerErrorResponse | ServerStatusResponse): void;
148 write(chunk: Buffer): boolean | undefined;
149 resume(): void;
150 setupSurfaceCall(call: ServerSurfaceCall): void;
151 setupReadable(readable: ServerReadableStream<RequestType, ResponseType> | ServerDuplexStream<RequestType, ResponseType>, encoding: string): void;
152 consumeUnpushedMessages(readable: ServerReadableStream<RequestType, ResponseType> | ServerDuplexStream<RequestType, ResponseType>): boolean;
153 private pushOrBufferMessage;
154 private pushMessage;
155 getPeer(): string;
156 getDeadline(): Deadline;
157 getPath(): string;
158}