UNPKG

6.79 kBTypeScriptView Raw
1/// <reference types="node" />
2import { PartialStatusObject } from './call-interface';
3import { ServerMethodDefinition } from './make-client';
4import { Metadata } from './metadata';
5import { ChannelOptions } from './channel-options';
6import { Handler } from './server-call';
7import { Deadline } from './deadline';
8import * as http2 from 'http2';
9import { CallEventTracker } from './transport';
10export interface ServerMetadataListener {
11 (metadata: Metadata, next: (metadata: Metadata) => void): void;
12}
13export interface ServerMessageListener {
14 (message: any, next: (message: any) => void): void;
15}
16export interface ServerHalfCloseListener {
17 (next: () => void): void;
18}
19export interface ServerCancelListener {
20 (): void;
21}
22export interface FullServerListener {
23 onReceiveMetadata: ServerMetadataListener;
24 onReceiveMessage: ServerMessageListener;
25 onReceiveHalfClose: ServerHalfCloseListener;
26 onCancel: ServerCancelListener;
27}
28export type ServerListener = Partial<FullServerListener>;
29export declare class ServerListenerBuilder {
30 private metadata;
31 private message;
32 private halfClose;
33 private cancel;
34 withOnReceiveMetadata(onReceiveMetadata: ServerMetadataListener): this;
35 withOnReceiveMessage(onReceiveMessage: ServerMessageListener): this;
36 withOnReceiveHalfClose(onReceiveHalfClose: ServerHalfCloseListener): this;
37 withOnCancel(onCancel: ServerCancelListener): this;
38 build(): ServerListener;
39}
40export interface InterceptingServerListener {
41 onReceiveMetadata(metadata: Metadata): void;
42 onReceiveMessage(message: any): void;
43 onReceiveHalfClose(): void;
44 onCancel(): void;
45}
46export declare function isInterceptingServerListener(listener: ServerListener | InterceptingServerListener): listener is InterceptingServerListener;
47export interface StartResponder {
48 (next: (listener?: ServerListener) => void): void;
49}
50export interface MetadataResponder {
51 (metadata: Metadata, next: (metadata: Metadata) => void): void;
52}
53export interface MessageResponder {
54 (message: any, next: (message: any) => void): void;
55}
56export interface StatusResponder {
57 (status: PartialStatusObject, next: (status: PartialStatusObject) => void): void;
58}
59export interface FullResponder {
60 start: StartResponder;
61 sendMetadata: MetadataResponder;
62 sendMessage: MessageResponder;
63 sendStatus: StatusResponder;
64}
65export type Responder = Partial<FullResponder>;
66export declare class ResponderBuilder {
67 private start;
68 private metadata;
69 private message;
70 private status;
71 withStart(start: StartResponder): this;
72 withSendMetadata(sendMetadata: MetadataResponder): this;
73 withSendMessage(sendMessage: MessageResponder): this;
74 withSendStatus(sendStatus: StatusResponder): this;
75 build(): Responder;
76}
77export interface ServerInterceptingCallInterface {
78 /**
79 * Register the listener to handle inbound events.
80 */
81 start(listener: InterceptingServerListener): void;
82 /**
83 * Send response metadata.
84 */
85 sendMetadata(metadata: Metadata): void;
86 /**
87 * Send a response message.
88 */
89 sendMessage(message: any, callback: () => void): void;
90 /**
91 * End the call by sending this status.
92 */
93 sendStatus(status: PartialStatusObject): void;
94 /**
95 * Start a single read, eventually triggering either listener.onReceiveMessage or listener.onReceiveHalfClose.
96 */
97 startRead(): void;
98 /**
99 * Return the peer address of the client making the request, if known, or "unknown" otherwise
100 */
101 getPeer(): string;
102 /**
103 * Return the call deadline set by the client. The value is Infinity if there is no deadline.
104 */
105 getDeadline(): Deadline;
106 /**
107 * Return the host requested by the client in the ":authority" header.
108 */
109 getHost(): string;
110}
111export declare class ServerInterceptingCall implements ServerInterceptingCallInterface {
112 private nextCall;
113 private responder;
114 private processingMetadata;
115 private processingMessage;
116 private pendingMessage;
117 private pendingMessageCallback;
118 private pendingStatus;
119 constructor(nextCall: ServerInterceptingCallInterface, responder?: Responder);
120 private processPendingMessage;
121 private processPendingStatus;
122 start(listener: InterceptingServerListener): void;
123 sendMetadata(metadata: Metadata): void;
124 sendMessage(message: any, callback: () => void): void;
125 sendStatus(status: PartialStatusObject): void;
126 startRead(): void;
127 getPeer(): string;
128 getDeadline(): Deadline;
129 getHost(): string;
130}
131export interface ServerInterceptor {
132 (methodDescriptor: ServerMethodDefinition<any, any>, call: ServerInterceptingCallInterface): ServerInterceptingCall;
133}
134export declare class BaseServerInterceptingCall implements ServerInterceptingCallInterface {
135 private readonly stream;
136 private readonly callEventTracker;
137 private readonly handler;
138 private listener;
139 private metadata;
140 private deadlineTimer;
141 private deadline;
142 private maxSendMessageSize;
143 private maxReceiveMessageSize;
144 private cancelled;
145 private metadataSent;
146 private wantTrailers;
147 private cancelNotified;
148 private incomingEncoding;
149 private decoder;
150 private readQueue;
151 private isReadPending;
152 private receivedHalfClose;
153 private streamEnded;
154 private host;
155 constructor(stream: http2.ServerHttp2Stream, headers: http2.IncomingHttpHeaders, callEventTracker: CallEventTracker | null, handler: Handler<any, any>, options: ChannelOptions);
156 private handleTimeoutHeader;
157 private checkCancelled;
158 private notifyOnCancel;
159 /**
160 * A server handler can start sending messages without explicitly sending
161 * metadata. In that case, we need to send headers before sending any
162 * messages. This function does that if necessary.
163 */
164 private maybeSendMetadata;
165 /**
166 * Serialize a message to a length-delimited byte string.
167 * @param value
168 * @returns
169 */
170 private serializeMessage;
171 private decompressMessage;
172 private decompressAndMaybePush;
173 private maybePushNextMessage;
174 private handleDataFrame;
175 private handleEndEvent;
176 start(listener: InterceptingServerListener): void;
177 sendMetadata(metadata: Metadata): void;
178 sendMessage(message: any, callback: () => void): void;
179 sendStatus(status: PartialStatusObject): void;
180 startRead(): void;
181 getPeer(): string;
182 getDeadline(): Deadline;
183 getHost(): string;
184}
185export declare function getServerInterceptingCall(interceptors: ServerInterceptor[], stream: http2.ServerHttp2Stream, headers: http2.IncomingHttpHeaders, callEventTracker: CallEventTracker | null, handler: Handler<any, any>, options: ChannelOptions): ServerInterceptingCallInterface;