@@ -48,13 +48,14 @@ function validate(key: string, value?: MetadataValue): void {
48
48
if ( ! isLegalKey ( key ) ) {
49
49
throw new Error ( 'Metadata key "' + key + '" contains illegal characters' ) ;
50
50
}
51
+
51
52
if ( value !== null && value !== undefined ) {
52
53
if ( isBinaryKey ( key ) ) {
53
- if ( ! ( value instanceof Buffer ) ) {
54
+ if ( ! Buffer . isBuffer ( value ) ) {
54
55
throw new Error ( "keys that end with '-bin' must have Buffer values" ) ;
55
56
}
56
57
} else {
57
- if ( value instanceof Buffer ) {
58
+ if ( Buffer . isBuffer ( value ) ) {
58
59
throw new Error (
59
60
"keys that don't end with '-bin' must have String values"
60
61
) ;
@@ -88,12 +89,8 @@ export class Metadata {
88
89
protected internalRepr : MetadataObject = new Map < string , MetadataValue [ ] > ( ) ;
89
90
private options : MetadataOptions ;
90
91
91
- constructor ( options ?: MetadataOptions ) {
92
- if ( options === undefined ) {
93
- this . options = { } ;
94
- } else {
95
- this . options = options ;
96
- }
92
+ constructor ( options : MetadataOptions = { } ) {
93
+ this . options = options ;
97
94
}
98
95
99
96
/**
@@ -120,9 +117,7 @@ export class Metadata {
120
117
key = normalizeKey ( key ) ;
121
118
validate ( key , value ) ;
122
119
123
- const existingValue : MetadataValue [ ] | undefined = this . internalRepr . get (
124
- key
125
- ) ;
120
+ const existingValue : MetadataValue [ ] | undefined = this . internalRepr . get ( key ) ;
126
121
127
122
if ( existingValue === undefined ) {
128
123
this . internalRepr . set ( key , [ value ] ) ;
@@ -137,7 +132,7 @@ export class Metadata {
137
132
*/
138
133
remove ( key : string ) : void {
139
134
key = normalizeKey ( key ) ;
140
- validate ( key ) ;
135
+ // validate(key);
141
136
this . internalRepr . delete ( key ) ;
142
137
}
143
138
@@ -148,7 +143,7 @@ export class Metadata {
148
143
*/
149
144
get ( key : string ) : MetadataValue [ ] {
150
145
key = normalizeKey ( key ) ;
151
- validate ( key ) ;
146
+ // validate(key);
152
147
return this . internalRepr . get ( key ) || [ ] ;
153
148
}
154
149
@@ -160,12 +155,12 @@ export class Metadata {
160
155
getMap ( ) : { [ key : string ] : MetadataValue } {
161
156
const result : { [ key : string ] : MetadataValue } = { } ;
162
157
163
- this . internalRepr . forEach ( ( values , key ) => {
158
+ for ( const [ key , values ] of this . internalRepr ) {
164
159
if ( values . length > 0 ) {
165
160
const v = values [ 0 ] ;
166
- result [ key ] = v instanceof Buffer ? v . slice ( ) : v ;
161
+ result [ key ] = Buffer . isBuffer ( v ) ? Buffer . from ( v ) : v ;
167
162
}
168
- } ) ;
163
+ }
169
164
return result ;
170
165
}
171
166
@@ -177,17 +172,17 @@ export class Metadata {
177
172
const newMetadata = new Metadata ( this . options ) ;
178
173
const newInternalRepr = newMetadata . internalRepr ;
179
174
180
- this . internalRepr . forEach ( ( value , key ) => {
175
+ for ( const [ key , value ] of this . internalRepr ) {
181
176
const clonedValue : MetadataValue [ ] = value . map ( ( v ) => {
182
- if ( v instanceof Buffer ) {
177
+ if ( Buffer . isBuffer ( v ) ) {
183
178
return Buffer . from ( v ) ;
184
179
} else {
185
180
return v ;
186
181
}
187
182
} ) ;
188
183
189
184
newInternalRepr . set ( key , clonedValue ) ;
190
- } ) ;
185
+ }
191
186
192
187
return newMetadata ;
193
188
}
@@ -200,13 +195,13 @@ export class Metadata {
200
195
* @param other A Metadata object.
201
196
*/
202
197
merge ( other : Metadata ) : void {
203
- other . internalRepr . forEach ( ( values , key ) => {
198
+ for ( const [ key , values ] of other . internalRepr ) {
204
199
const mergedValue : MetadataValue [ ] = (
205
200
this . internalRepr . get ( key ) || [ ]
206
201
) . concat ( values ) ;
207
202
208
203
this . internalRepr . set ( key , mergedValue ) ;
209
- } ) ;
204
+ }
210
205
}
211
206
212
207
setOptions ( options : MetadataOptions ) {
@@ -223,17 +218,13 @@ export class Metadata {
223
218
toHttp2Headers ( ) : http2 . OutgoingHttpHeaders {
224
219
// NOTE: Node <8.9 formats http2 headers incorrectly.
225
220
const result : http2 . OutgoingHttpHeaders = { } ;
226
- this . internalRepr . forEach ( ( values , key ) => {
221
+
222
+ for ( const [ key , values ] of this . internalRepr ) {
227
223
// We assume that the user's interaction with this object is limited to
228
224
// through its public API (i.e. keys and values are already validated).
229
- result [ key ] = values . map ( ( value ) => {
230
- if ( value instanceof Buffer ) {
231
- return value . toString ( 'base64' ) ;
232
- } else {
233
- return value ;
234
- }
235
- } ) ;
236
- } ) ;
225
+ result [ key ] = values . map ( bufToString ) ;
226
+ }
227
+
237
228
return result ;
238
229
}
239
230
@@ -248,7 +239,7 @@ export class Metadata {
248
239
*/
249
240
toJSON ( ) {
250
241
const result : { [ key : string ] : MetadataValue [ ] } = { } ;
251
- for ( const [ key , values ] of this . internalRepr . entries ( ) ) {
242
+ for ( const [ key , values ] of this . internalRepr ) {
252
243
result [ key ] = values ;
253
244
}
254
245
return result ;
@@ -261,10 +252,10 @@ export class Metadata {
261
252
*/
262
253
static fromHttp2Headers ( headers : http2 . IncomingHttpHeaders ) : Metadata {
263
254
const result = new Metadata ( ) ;
264
- Object . keys ( headers ) . forEach ( ( key ) => {
255
+ for ( const key of Object . keys ( headers ) ) {
265
256
// Reserved headers (beginning with `:`) are not valid keys.
266
257
if ( key . charAt ( 0 ) === ':' ) {
267
- return ;
258
+ continue ;
268
259
}
269
260
270
261
const values = headers [ key ] ;
@@ -297,7 +288,12 @@ export class Metadata {
297
288
const message = `Failed to add metadata entry ${ key } : ${ values } . ${ error . message } . For more information see https://github.com/grpc/grpc-node/issues/1173` ;
298
289
log ( LogVerbosity . ERROR , message ) ;
299
290
}
300
- } ) ;
291
+ }
292
+
301
293
return result ;
302
294
}
303
295
}
296
+
297
+ const bufToString = ( val : string | Buffer ) : string => {
298
+ return Buffer . isBuffer ( val ) ? val . toString ( 'base64' ) : val
299
+ } ;
0 commit comments