UNPKG

3.3 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright 2020 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18Object.defineProperty(exports, "__esModule", { value: true });
19exports.MaxMessageSizeFilterFactory = exports.MaxMessageSizeFilter = void 0;
20const filter_1 = require("./filter");
21const constants_1 = require("./constants");
22class MaxMessageSizeFilter extends filter_1.BaseFilter {
23 constructor(options, callStream) {
24 super();
25 this.options = options;
26 this.callStream = callStream;
27 this.maxSendMessageSize = constants_1.DEFAULT_MAX_SEND_MESSAGE_LENGTH;
28 this.maxReceiveMessageSize = constants_1.DEFAULT_MAX_RECEIVE_MESSAGE_LENGTH;
29 if ('grpc.max_send_message_length' in options) {
30 this.maxSendMessageSize = options['grpc.max_send_message_length'];
31 }
32 if ('grpc.max_receive_message_length' in options) {
33 this.maxReceiveMessageSize = options['grpc.max_receive_message_length'];
34 }
35 }
36 async sendMessage(message) {
37 /* A configured size of -1 means that there is no limit, so skip the check
38 * entirely */
39 if (this.maxSendMessageSize === -1) {
40 return message;
41 }
42 else {
43 const concreteMessage = await message;
44 if (concreteMessage.message.length > this.maxSendMessageSize) {
45 this.callStream.cancelWithStatus(constants_1.Status.RESOURCE_EXHAUSTED, `Sent message larger than max (${concreteMessage.message.length} vs. ${this.maxSendMessageSize})`);
46 return Promise.reject('Message too large');
47 }
48 else {
49 return concreteMessage;
50 }
51 }
52 }
53 async receiveMessage(message) {
54 /* A configured size of -1 means that there is no limit, so skip the check
55 * entirely */
56 if (this.maxReceiveMessageSize === -1) {
57 return message;
58 }
59 else {
60 const concreteMessage = await message;
61 if (concreteMessage.length > this.maxReceiveMessageSize) {
62 this.callStream.cancelWithStatus(constants_1.Status.RESOURCE_EXHAUSTED, `Received message larger than max (${concreteMessage.length} vs. ${this.maxReceiveMessageSize})`);
63 return Promise.reject('Message too large');
64 }
65 else {
66 return concreteMessage;
67 }
68 }
69 }
70}
71exports.MaxMessageSizeFilter = MaxMessageSizeFilter;
72class MaxMessageSizeFilterFactory {
73 constructor(options) {
74 this.options = options;
75 }
76 createFilter(callStream) {
77 return new MaxMessageSizeFilter(this.options, callStream);
78 }
79}
80exports.MaxMessageSizeFilterFactory = MaxMessageSizeFilterFactory;
81//# sourceMappingURL=max-message-size-filter.js.map
\No newline at end of file