1 | ;
|
2 | /*
|
3 | * Copyright 2019 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 | */
|
18 | Object.defineProperty(exports, "__esModule", { value: true });
|
19 | exports.Metadata = void 0;
|
20 | const logging_1 = require("./logging");
|
21 | const constants_1 = require("./constants");
|
22 | const LEGAL_KEY_REGEX = /^[0-9a-z_.-]+$/;
|
23 | const LEGAL_NON_BINARY_VALUE_REGEX = /^[ -~]*$/;
|
24 | function isLegalKey(key) {
|
25 | return LEGAL_KEY_REGEX.test(key);
|
26 | }
|
27 | function isLegalNonBinaryValue(value) {
|
28 | return LEGAL_NON_BINARY_VALUE_REGEX.test(value);
|
29 | }
|
30 | function isBinaryKey(key) {
|
31 | return key.endsWith('-bin');
|
32 | }
|
33 | function isCustomMetadata(key) {
|
34 | return !key.startsWith('grpc-');
|
35 | }
|
36 | function normalizeKey(key) {
|
37 | return key.toLowerCase();
|
38 | }
|
39 | function validate(key, value) {
|
40 | if (!isLegalKey(key)) {
|
41 | throw new Error('Metadata key "' + key + '" contains illegal characters');
|
42 | }
|
43 | if (value !== null && value !== undefined) {
|
44 | if (isBinaryKey(key)) {
|
45 | if (!Buffer.isBuffer(value)) {
|
46 | throw new Error("keys that end with '-bin' must have Buffer values");
|
47 | }
|
48 | }
|
49 | else {
|
50 | if (Buffer.isBuffer(value)) {
|
51 | throw new Error("keys that don't end with '-bin' must have String values");
|
52 | }
|
53 | if (!isLegalNonBinaryValue(value)) {
|
54 | throw new Error('Metadata string value "' + value + '" contains illegal characters');
|
55 | }
|
56 | }
|
57 | }
|
58 | }
|
59 | /**
|
60 | * A class for storing metadata. Keys are normalized to lowercase ASCII.
|
61 | */
|
62 | class Metadata {
|
63 | constructor(options = {}) {
|
64 | this.internalRepr = new Map();
|
65 | this.options = options;
|
66 | }
|
67 | /**
|
68 | * Sets the given value for the given key by replacing any other values
|
69 | * associated with that key. Normalizes the key.
|
70 | * @param key The key to whose value should be set.
|
71 | * @param value The value to set. Must be a buffer if and only
|
72 | * if the normalized key ends with '-bin'.
|
73 | */
|
74 | set(key, value) {
|
75 | key = normalizeKey(key);
|
76 | validate(key, value);
|
77 | this.internalRepr.set(key, [value]);
|
78 | }
|
79 | /**
|
80 | * Adds the given value for the given key by appending to a list of previous
|
81 | * values associated with that key. Normalizes the key.
|
82 | * @param key The key for which a new value should be appended.
|
83 | * @param value The value to add. Must be a buffer if and only
|
84 | * if the normalized key ends with '-bin'.
|
85 | */
|
86 | add(key, value) {
|
87 | key = normalizeKey(key);
|
88 | validate(key, value);
|
89 | const existingValue = this.internalRepr.get(key);
|
90 | if (existingValue === undefined) {
|
91 | this.internalRepr.set(key, [value]);
|
92 | }
|
93 | else {
|
94 | existingValue.push(value);
|
95 | }
|
96 | }
|
97 | /**
|
98 | * Removes the given key and any associated values. Normalizes the key.
|
99 | * @param key The key whose values should be removed.
|
100 | */
|
101 | remove(key) {
|
102 | key = normalizeKey(key);
|
103 | // validate(key);
|
104 | this.internalRepr.delete(key);
|
105 | }
|
106 | /**
|
107 | * Gets a list of all values associated with the key. Normalizes the key.
|
108 | * @param key The key whose value should be retrieved.
|
109 | * @return A list of values associated with the given key.
|
110 | */
|
111 | get(key) {
|
112 | key = normalizeKey(key);
|
113 | // validate(key);
|
114 | return this.internalRepr.get(key) || [];
|
115 | }
|
116 | /**
|
117 | * Gets a plain object mapping each key to the first value associated with it.
|
118 | * This reflects the most common way that people will want to see metadata.
|
119 | * @return A key/value mapping of the metadata.
|
120 | */
|
121 | getMap() {
|
122 | const result = {};
|
123 | for (const [key, values] of this.internalRepr) {
|
124 | if (values.length > 0) {
|
125 | const v = values[0];
|
126 | result[key] = Buffer.isBuffer(v) ? Buffer.from(v) : v;
|
127 | }
|
128 | }
|
129 | return result;
|
130 | }
|
131 | /**
|
132 | * Clones the metadata object.
|
133 | * @return The newly cloned object.
|
134 | */
|
135 | clone() {
|
136 | const newMetadata = new Metadata(this.options);
|
137 | const newInternalRepr = newMetadata.internalRepr;
|
138 | for (const [key, value] of this.internalRepr) {
|
139 | const clonedValue = value.map((v) => {
|
140 | if (Buffer.isBuffer(v)) {
|
141 | return Buffer.from(v);
|
142 | }
|
143 | else {
|
144 | return v;
|
145 | }
|
146 | });
|
147 | newInternalRepr.set(key, clonedValue);
|
148 | }
|
149 | return newMetadata;
|
150 | }
|
151 | /**
|
152 | * Merges all key-value pairs from a given Metadata object into this one.
|
153 | * If both this object and the given object have values in the same key,
|
154 | * values from the other Metadata object will be appended to this object's
|
155 | * values.
|
156 | * @param other A Metadata object.
|
157 | */
|
158 | merge(other) {
|
159 | for (const [key, values] of other.internalRepr) {
|
160 | const mergedValue = (this.internalRepr.get(key) || []).concat(values);
|
161 | this.internalRepr.set(key, mergedValue);
|
162 | }
|
163 | }
|
164 | setOptions(options) {
|
165 | this.options = options;
|
166 | }
|
167 | getOptions() {
|
168 | return this.options;
|
169 | }
|
170 | /**
|
171 | * Creates an OutgoingHttpHeaders object that can be used with the http2 API.
|
172 | */
|
173 | toHttp2Headers() {
|
174 | // NOTE: Node <8.9 formats http2 headers incorrectly.
|
175 | const result = {};
|
176 | for (const [key, values] of this.internalRepr) {
|
177 | // We assume that the user's interaction with this object is limited to
|
178 | // through its public API (i.e. keys and values are already validated).
|
179 | result[key] = values.map(bufToString);
|
180 | }
|
181 | return result;
|
182 | }
|
183 | // For compatibility with the other Metadata implementation
|
184 | _getCoreRepresentation() {
|
185 | return this.internalRepr;
|
186 | }
|
187 | /**
|
188 | * This modifies the behavior of JSON.stringify to show an object
|
189 | * representation of the metadata map.
|
190 | */
|
191 | toJSON() {
|
192 | const result = {};
|
193 | for (const [key, values] of this.internalRepr) {
|
194 | result[key] = values;
|
195 | }
|
196 | return result;
|
197 | }
|
198 | /**
|
199 | * Returns a new Metadata object based fields in a given IncomingHttpHeaders
|
200 | * object.
|
201 | * @param headers An IncomingHttpHeaders object.
|
202 | */
|
203 | static fromHttp2Headers(headers) {
|
204 | const result = new Metadata();
|
205 | for (const key of Object.keys(headers)) {
|
206 | // Reserved headers (beginning with `:`) are not valid keys.
|
207 | if (key.charAt(0) === ':') {
|
208 | continue;
|
209 | }
|
210 | const values = headers[key];
|
211 | try {
|
212 | if (isBinaryKey(key)) {
|
213 | if (Array.isArray(values)) {
|
214 | values.forEach((value) => {
|
215 | result.add(key, Buffer.from(value, 'base64'));
|
216 | });
|
217 | }
|
218 | else if (values !== undefined) {
|
219 | if (isCustomMetadata(key)) {
|
220 | values.split(',').forEach((v) => {
|
221 | result.add(key, Buffer.from(v.trim(), 'base64'));
|
222 | });
|
223 | }
|
224 | else {
|
225 | result.add(key, Buffer.from(values, 'base64'));
|
226 | }
|
227 | }
|
228 | }
|
229 | else {
|
230 | if (Array.isArray(values)) {
|
231 | values.forEach((value) => {
|
232 | result.add(key, value);
|
233 | });
|
234 | }
|
235 | else if (values !== undefined) {
|
236 | result.add(key, values);
|
237 | }
|
238 | }
|
239 | }
|
240 | catch (error) {
|
241 | const message = `Failed to add metadata entry ${key}: ${values}. ${error.message}. For more information see https://github.com/grpc/grpc-node/issues/1173`;
|
242 | logging_1.log(constants_1.LogVerbosity.ERROR, message);
|
243 | }
|
244 | }
|
245 | return result;
|
246 | }
|
247 | }
|
248 | exports.Metadata = Metadata;
|
249 | const bufToString = (val) => {
|
250 | return Buffer.isBuffer(val) ? val.toString('base64') : val;
|
251 | };
|
252 | //# sourceMappingURL=metadata.js.map |
\ | No newline at end of file |