Skip to content

Commit 9aabaee

Browse files
committed
Merge remote-tracking branch 'upstream/@grpc/[email protected]' into grpc-js_upmerge_1.7.x
2 parents e21d41d + 4bfd383 commit 9aabaee

File tree

6 files changed

+496
-390
lines changed

6 files changed

+496
-390
lines changed

packages/grpc-js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@grpc/grpc-js",
3-
"version": "1.7.2",
3+
"version": "1.7.3",
44
"description": "gRPC Library for Node - pure JS implementation",
55
"homepage": "https://grpc.io/",
66
"repository": "https://github.com/grpc/grpc-node/tree/master/packages/grpc-js",

packages/grpc-js/src/call-interface.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ export interface StatusObject {
3636
metadata: Metadata;
3737
}
3838

39+
export type PartialStatusObject = Pick<StatusObject, 'code' | 'details'> & {
40+
metadata: Metadata | null;
41+
}
42+
3943
export const enum WriteFlags {
4044
BufferHint = 1,
4145
NoCompress = 2,

packages/grpc-js/src/generated/grpc/channelz/v1/Channelz.ts

Lines changed: 52 additions & 52 deletions
Large diffs are not rendered by default.

packages/grpc-js/src/metadata.ts

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,14 @@ function validate(key: string, value?: MetadataValue): void {
4848
if (!isLegalKey(key)) {
4949
throw new Error('Metadata key "' + key + '" contains illegal characters');
5050
}
51+
5152
if (value !== null && value !== undefined) {
5253
if (isBinaryKey(key)) {
53-
if (!(value instanceof Buffer)) {
54+
if (!Buffer.isBuffer(value)) {
5455
throw new Error("keys that end with '-bin' must have Buffer values");
5556
}
5657
} else {
57-
if (value instanceof Buffer) {
58+
if (Buffer.isBuffer(value)) {
5859
throw new Error(
5960
"keys that don't end with '-bin' must have String values"
6061
);
@@ -88,12 +89,8 @@ export class Metadata {
8889
protected internalRepr: MetadataObject = new Map<string, MetadataValue[]>();
8990
private options: MetadataOptions;
9091

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;
9794
}
9895

9996
/**
@@ -120,9 +117,7 @@ export class Metadata {
120117
key = normalizeKey(key);
121118
validate(key, value);
122119

123-
const existingValue: MetadataValue[] | undefined = this.internalRepr.get(
124-
key
125-
);
120+
const existingValue: MetadataValue[] | undefined = this.internalRepr.get(key);
126121

127122
if (existingValue === undefined) {
128123
this.internalRepr.set(key, [value]);
@@ -137,7 +132,7 @@ export class Metadata {
137132
*/
138133
remove(key: string): void {
139134
key = normalizeKey(key);
140-
validate(key);
135+
// validate(key);
141136
this.internalRepr.delete(key);
142137
}
143138

@@ -148,7 +143,7 @@ export class Metadata {
148143
*/
149144
get(key: string): MetadataValue[] {
150145
key = normalizeKey(key);
151-
validate(key);
146+
// validate(key);
152147
return this.internalRepr.get(key) || [];
153148
}
154149

@@ -160,12 +155,12 @@ export class Metadata {
160155
getMap(): { [key: string]: MetadataValue } {
161156
const result: { [key: string]: MetadataValue } = {};
162157

163-
this.internalRepr.forEach((values, key) => {
158+
for (const [key, values] of this.internalRepr) {
164159
if (values.length > 0) {
165160
const v = values[0];
166-
result[key] = v instanceof Buffer ? v.slice() : v;
161+
result[key] = Buffer.isBuffer(v) ? Buffer.from(v) : v;
167162
}
168-
});
163+
}
169164
return result;
170165
}
171166

@@ -177,17 +172,17 @@ export class Metadata {
177172
const newMetadata = new Metadata(this.options);
178173
const newInternalRepr = newMetadata.internalRepr;
179174

180-
this.internalRepr.forEach((value, key) => {
175+
for (const [key, value] of this.internalRepr) {
181176
const clonedValue: MetadataValue[] = value.map((v) => {
182-
if (v instanceof Buffer) {
177+
if (Buffer.isBuffer(v)) {
183178
return Buffer.from(v);
184179
} else {
185180
return v;
186181
}
187182
});
188183

189184
newInternalRepr.set(key, clonedValue);
190-
});
185+
}
191186

192187
return newMetadata;
193188
}
@@ -200,13 +195,13 @@ export class Metadata {
200195
* @param other A Metadata object.
201196
*/
202197
merge(other: Metadata): void {
203-
other.internalRepr.forEach((values, key) => {
198+
for (const [key, values] of other.internalRepr) {
204199
const mergedValue: MetadataValue[] = (
205200
this.internalRepr.get(key) || []
206201
).concat(values);
207202

208203
this.internalRepr.set(key, mergedValue);
209-
});
204+
}
210205
}
211206

212207
setOptions(options: MetadataOptions) {
@@ -223,17 +218,13 @@ export class Metadata {
223218
toHttp2Headers(): http2.OutgoingHttpHeaders {
224219
// NOTE: Node <8.9 formats http2 headers incorrectly.
225220
const result: http2.OutgoingHttpHeaders = {};
226-
this.internalRepr.forEach((values, key) => {
221+
222+
for (const [key, values] of this.internalRepr) {
227223
// We assume that the user's interaction with this object is limited to
228224
// 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+
237228
return result;
238229
}
239230

@@ -248,7 +239,7 @@ export class Metadata {
248239
*/
249240
toJSON() {
250241
const result: { [key: string]: MetadataValue[] } = {};
251-
for (const [key, values] of this.internalRepr.entries()) {
242+
for (const [key, values] of this.internalRepr) {
252243
result[key] = values;
253244
}
254245
return result;
@@ -261,10 +252,10 @@ export class Metadata {
261252
*/
262253
static fromHttp2Headers(headers: http2.IncomingHttpHeaders): Metadata {
263254
const result = new Metadata();
264-
Object.keys(headers).forEach((key) => {
255+
for (const key of Object.keys(headers)) {
265256
// Reserved headers (beginning with `:`) are not valid keys.
266257
if (key.charAt(0) === ':') {
267-
return;
258+
continue;
268259
}
269260

270261
const values = headers[key];
@@ -297,7 +288,12 @@ export class Metadata {
297288
const message = `Failed to add metadata entry ${key}: ${values}. ${error.message}. For more information see https://github.com/grpc/grpc-node/issues/1173`;
298289
log(LogVerbosity.ERROR, message);
299290
}
300-
});
291+
}
292+
301293
return result;
302294
}
303295
}
296+
297+
const bufToString = (val: string | Buffer): string => {
298+
return Buffer.isBuffer(val) ? val.toString('base64') : val
299+
};

0 commit comments

Comments
 (0)