1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | import * as http2 from 'http2';
|
19 | import { log } from './logging';
|
20 | import { LogVerbosity } from './constants';
|
21 | import { getErrorMessage } from './error';
|
22 | const LEGAL_KEY_REGEX = /^[0-9a-z_.-]+$/;
|
23 | const LEGAL_NON_BINARY_VALUE_REGEX = /^[ -~]*$/;
|
24 |
|
25 | export type MetadataValue = string | Buffer;
|
26 | export type MetadataObject = Map<string, MetadataValue[]>;
|
27 |
|
28 | function isLegalKey(key: string): boolean {
|
29 | return LEGAL_KEY_REGEX.test(key);
|
30 | }
|
31 |
|
32 | function isLegalNonBinaryValue(value: string): boolean {
|
33 | return LEGAL_NON_BINARY_VALUE_REGEX.test(value);
|
34 | }
|
35 |
|
36 | function isBinaryKey(key: string): boolean {
|
37 | return key.endsWith('-bin');
|
38 | }
|
39 |
|
40 | function isCustomMetadata(key: string): boolean {
|
41 | return !key.startsWith('grpc-');
|
42 | }
|
43 |
|
44 | function normalizeKey(key: string): string {
|
45 | return key.toLowerCase();
|
46 | }
|
47 |
|
48 | function validate(key: string, value?: MetadataValue): void {
|
49 | if (!isLegalKey(key)) {
|
50 | throw new Error('Metadata key "' + key + '" contains illegal characters');
|
51 | }
|
52 |
|
53 | if (value !== null && value !== undefined) {
|
54 | if (isBinaryKey(key)) {
|
55 | if (!Buffer.isBuffer(value)) {
|
56 | throw new Error("keys that end with '-bin' must have Buffer values");
|
57 | }
|
58 | } else {
|
59 | if (Buffer.isBuffer(value)) {
|
60 | throw new Error(
|
61 | "keys that don't end with '-bin' must have String values"
|
62 | );
|
63 | }
|
64 | if (!isLegalNonBinaryValue(value)) {
|
65 | throw new Error(
|
66 | 'Metadata string value "' + value + '" contains illegal characters'
|
67 | );
|
68 | }
|
69 | }
|
70 | }
|
71 | }
|
72 |
|
73 | export interface MetadataOptions {
|
74 |
|
75 | idempotentRequest?: boolean;
|
76 | |
77 |
|
78 | waitForReady?: boolean;
|
79 | |
80 |
|
81 | cacheableRequest?: boolean;
|
82 |
|
83 | corked?: boolean;
|
84 | }
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | export class Metadata {
|
90 | protected internalRepr: MetadataObject = new Map<string, MetadataValue[]>();
|
91 | private options: MetadataOptions;
|
92 |
|
93 | constructor(options: MetadataOptions = {}) {
|
94 | this.options = options;
|
95 | }
|
96 |
|
97 | |
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | set(key: string, value: MetadataValue): void {
|
105 | key = normalizeKey(key);
|
106 | validate(key, value);
|
107 | this.internalRepr.set(key, [value]);
|
108 | }
|
109 |
|
110 | |
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | add(key: string, value: MetadataValue): void {
|
118 | key = normalizeKey(key);
|
119 | validate(key, value);
|
120 |
|
121 | const existingValue: MetadataValue[] | undefined = this.internalRepr.get(key);
|
122 |
|
123 | if (existingValue === undefined) {
|
124 | this.internalRepr.set(key, [value]);
|
125 | } else {
|
126 | existingValue.push(value);
|
127 | }
|
128 | }
|
129 |
|
130 | |
131 |
|
132 |
|
133 |
|
134 | remove(key: string): void {
|
135 | key = normalizeKey(key);
|
136 |
|
137 | this.internalRepr.delete(key);
|
138 | }
|
139 |
|
140 | |
141 |
|
142 |
|
143 |
|
144 |
|
145 | get(key: string): MetadataValue[] {
|
146 | key = normalizeKey(key);
|
147 |
|
148 | return this.internalRepr.get(key) || [];
|
149 | }
|
150 |
|
151 | |
152 |
|
153 |
|
154 |
|
155 |
|
156 | getMap(): { [key: string]: MetadataValue } {
|
157 | const result: { [key: string]: MetadataValue } = {};
|
158 |
|
159 | for (const [key, values] of this.internalRepr) {
|
160 | if (values.length > 0) {
|
161 | const v = values[0];
|
162 | result[key] = Buffer.isBuffer(v) ? Buffer.from(v) : v;
|
163 | }
|
164 | }
|
165 | return result;
|
166 | }
|
167 |
|
168 | |
169 |
|
170 |
|
171 |
|
172 | clone(): Metadata {
|
173 | const newMetadata = new Metadata(this.options);
|
174 | const newInternalRepr = newMetadata.internalRepr;
|
175 |
|
176 | for (const [key, value] of this.internalRepr) {
|
177 | const clonedValue: MetadataValue[] = value.map((v) => {
|
178 | if (Buffer.isBuffer(v)) {
|
179 | return Buffer.from(v);
|
180 | } else {
|
181 | return v;
|
182 | }
|
183 | });
|
184 |
|
185 | newInternalRepr.set(key, clonedValue);
|
186 | }
|
187 |
|
188 | return newMetadata;
|
189 | }
|
190 |
|
191 | |
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | merge(other: Metadata): void {
|
199 | for (const [key, values] of other.internalRepr) {
|
200 | const mergedValue: MetadataValue[] = (
|
201 | this.internalRepr.get(key) || []
|
202 | ).concat(values);
|
203 |
|
204 | this.internalRepr.set(key, mergedValue);
|
205 | }
|
206 | }
|
207 |
|
208 | setOptions(options: MetadataOptions) {
|
209 | this.options = options;
|
210 | }
|
211 |
|
212 | getOptions(): MetadataOptions {
|
213 | return this.options;
|
214 | }
|
215 |
|
216 | |
217 |
|
218 |
|
219 | toHttp2Headers(): http2.OutgoingHttpHeaders {
|
220 |
|
221 | const result: http2.OutgoingHttpHeaders = {};
|
222 |
|
223 | for (const [key, values] of this.internalRepr) {
|
224 |
|
225 |
|
226 | result[key] = values.map(bufToString);
|
227 | }
|
228 |
|
229 | return result;
|
230 | }
|
231 |
|
232 |
|
233 | private _getCoreRepresentation() {
|
234 | return this.internalRepr;
|
235 | }
|
236 |
|
237 | |
238 |
|
239 |
|
240 |
|
241 | toJSON() {
|
242 | const result: { [key: string]: MetadataValue[] } = {};
|
243 | for (const [key, values] of this.internalRepr) {
|
244 | result[key] = values;
|
245 | }
|
246 | return result;
|
247 | }
|
248 |
|
249 | |
250 |
|
251 |
|
252 |
|
253 |
|
254 | static fromHttp2Headers(headers: http2.IncomingHttpHeaders): Metadata {
|
255 | const result = new Metadata();
|
256 | for (const key of Object.keys(headers)) {
|
257 |
|
258 | if (key.charAt(0) === ':') {
|
259 | continue;
|
260 | }
|
261 |
|
262 | const values = headers[key];
|
263 |
|
264 | try {
|
265 | if (isBinaryKey(key)) {
|
266 | if (Array.isArray(values)) {
|
267 | values.forEach((value) => {
|
268 | result.add(key, Buffer.from(value, 'base64'));
|
269 | });
|
270 | } else if (values !== undefined) {
|
271 | if (isCustomMetadata(key)) {
|
272 | values.split(',').forEach((v) => {
|
273 | result.add(key, Buffer.from(v.trim(), 'base64'));
|
274 | });
|
275 | } else {
|
276 | result.add(key, Buffer.from(values, 'base64'));
|
277 | }
|
278 | }
|
279 | } else {
|
280 | if (Array.isArray(values)) {
|
281 | values.forEach((value) => {
|
282 | result.add(key, value);
|
283 | });
|
284 | } else if (values !== undefined) {
|
285 | result.add(key, values);
|
286 | }
|
287 | }
|
288 | } catch (error) {
|
289 | const message = `Failed to add metadata entry ${key}: ${values}. ${getErrorMessage(error)}. For more information see https://github.com/grpc/grpc-node/issues/1173`;
|
290 | log(LogVerbosity.ERROR, message);
|
291 | }
|
292 | }
|
293 |
|
294 | return result;
|
295 | }
|
296 | }
|
297 |
|
298 | const bufToString = (val: string | Buffer): string => {
|
299 | return Buffer.isBuffer(val) ? val.toString('base64') : val
|
300 | };
|