UNPKG

1.8 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 { Call, StatusObject, WriteObject } from './call-stream';
19import { Metadata } from './metadata';
20
21/**
22 * Filter classes represent related per-call logic and state that is primarily
23 * used to modify incoming and outgoing data
24 */
25export interface Filter {
26 sendMetadata(metadata: Promise<Metadata>): Promise<Metadata>;
27
28 receiveMetadata(metadata: Metadata): Metadata;
29
30 sendMessage(message: Promise<WriteObject>): Promise<WriteObject>;
31
32 receiveMessage(message: Promise<Buffer>): Promise<Buffer>;
33
34 receiveTrailers(status: StatusObject): StatusObject;
35
36 refresh(): void;
37}
38
39export abstract class BaseFilter implements Filter {
40 async sendMetadata(metadata: Promise<Metadata>): Promise<Metadata> {
41 return metadata;
42 }
43
44 receiveMetadata(metadata: Metadata): Metadata {
45 return metadata;
46 }
47
48 async sendMessage(message: Promise<WriteObject>): Promise<WriteObject> {
49 return message;
50 }
51
52 async receiveMessage(message: Promise<Buffer>): Promise<Buffer> {
53 return message;
54 }
55
56 receiveTrailers(status: StatusObject): StatusObject {
57 return status;
58 }
59
60 refresh(): void {}
61}
62
63export interface FilterFactory<T extends Filter> {
64 createFilter(callStream: Call): T;
65}