UNPKG

1.89 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 { StatusObject, WriteObject } from './call-interface';
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. All async filters can be
24 * rejected. The rejection error must be a StatusObject, and a rejection will
25 * cause the call to end with that status.
26 */
27export interface Filter {
28 sendMetadata(metadata: Promise<Metadata>): Promise<Metadata>;
29
30 receiveMetadata(metadata: Metadata): Metadata;
31
32 sendMessage(message: Promise<WriteObject>): Promise<WriteObject>;
33
34 receiveMessage(message: Promise<Buffer>): Promise<Buffer>;
35
36 receiveTrailers(status: StatusObject): StatusObject;
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
61export interface FilterFactory<T extends Filter> {
62 createFilter(): T;
63}