1 | ;
|
2 | /*!
|
3 | * Copyright 2021 Google LLC
|
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 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.calculateMessageSize = void 0;
|
19 | /**
|
20 | * Precisely calculates the size of a message with optional `data` and
|
21 | * `attributes` fields. If a `data` field is present, its {@link Buffer#length}
|
22 | * member will be used. If `attributes` are present, each attribute's
|
23 | * key and value will be calculated for byte length.
|
24 | *
|
25 | * When the calculation is complete, the result will be stored in
|
26 | * `calculatedSize`. Since this calculation is generally only done
|
27 | * after the message has been handed off to this library, there shouldn't
|
28 | * be an issue with it being updated after the fact.
|
29 | *
|
30 | * This should not be used outside of this library. Its implementation
|
31 | * may change.
|
32 | *
|
33 | * @private
|
34 | */
|
35 | function calculateMessageSize(message) {
|
36 | // If it's not a PubsubMessage, we'll augment it into one.
|
37 | const msg = message;
|
38 | if (msg.calculatedSize !== undefined) {
|
39 | return msg.calculatedSize;
|
40 | }
|
41 | let size = 0;
|
42 | if (msg.data) {
|
43 | size += msg.data.length;
|
44 | }
|
45 | if (msg.attributes) {
|
46 | const attrs = msg.attributes;
|
47 | for (const key of Object.getOwnPropertyNames(attrs)) {
|
48 | const val = attrs[key] || '';
|
49 | size += Buffer.byteLength(key + val);
|
50 | }
|
51 | }
|
52 | msg.calculatedSize = size;
|
53 | return size;
|
54 | }
|
55 | exports.calculateMessageSize = calculateMessageSize;
|
56 | //# sourceMappingURL=pubsub-message.js.map |
\ | No newline at end of file |