Skip to content

Commit 96213d1

Browse files
authored
Merge pull request #2521 from murgatroid99/v1.8.x_upmerge_3
Merge 1.8.x into master
2 parents 426768d + 1d38cc3 commit 96213d1

File tree

4 files changed

+120
-38
lines changed

4 files changed

+120
-38
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.8.19",
3+
"version": "1.8.21",
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/server.ts

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ import {
7373
unregisterChannelzRef,
7474
} from './channelz';
7575
import { CipherNameAndProtocol, TLSSocket } from 'tls';
76-
import { getErrorCode, getErrorMessage } from './error';
7776

7877
const UNLIMITED_CONNECTION_AGE_MS = ~(1 << 31);
7978
const KEEPALIVE_MAX_TIME_MS = ~(1 << 31);
@@ -846,11 +845,7 @@ export class Server {
846845
return true;
847846
}
848847

849-
private _retrieveHandler(
850-
headers: http2.IncomingHttpHeaders
851-
): Handler<any, any> {
852-
const path = headers[HTTP2_HEADER_PATH] as string;
853-
848+
private _retrieveHandler(path: string): Handler<any, any> | null {
854849
this.trace(
855850
'Received call to method ' +
856851
path +
@@ -866,7 +861,7 @@ export class Server {
866861
path +
867862
'. Sending UNIMPLEMENTED status.'
868863
);
869-
throw getUnimplementedStatusResponse(path);
864+
return null;
870865
}
871866

872867
return handler;
@@ -908,15 +903,12 @@ export class Server {
908903
return;
909904
}
910905

911-
let handler: Handler<any, any>;
912-
try {
913-
handler = this._retrieveHandler(headers);
914-
} catch (err) {
906+
const path = headers[HTTP2_HEADER_PATH] as string;
907+
908+
const handler = this._retrieveHandler(path);
909+
if (!handler) {
915910
this._respondWithError(
916-
{
917-
details: getErrorMessage(err),
918-
code: getErrorCode(err) ?? undefined,
919-
},
911+
getUnimplementedStatusResponse(path),
920912
stream,
921913
channelzSessionInfo
922914
);
@@ -970,15 +962,12 @@ export class Server {
970962
return;
971963
}
972964

973-
let handler: Handler<any, any>;
974-
try {
975-
handler = this._retrieveHandler(headers);
976-
} catch (err) {
965+
const path = headers[HTTP2_HEADER_PATH] as string;
966+
967+
const handler = this._retrieveHandler(path);
968+
if (!handler) {
977969
this._respondWithError(
978-
{
979-
details: getErrorMessage(err),
980-
code: getErrorCode(err) ?? undefined,
981-
},
970+
getUnimplementedStatusResponse(path),
982971
stream,
983972
null
984973
);

packages/grpc-js/src/transport.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,18 @@ class Http2Transport implements Transport {
153153
*/
154154
private remoteName: string | null
155155
) {
156+
/* Populate subchannelAddressString and channelzRef before doing anything
157+
* else, because they are used in the trace methods. */
158+
this.subchannelAddressString = subchannelAddressToString(subchannelAddress);
159+
160+
if (options['grpc.enable_channelz'] === 0) {
161+
this.channelzEnabled = false;
162+
}
163+
this.channelzRef = registerChannelzSocket(
164+
this.subchannelAddressString,
165+
() => this.getChannelzInfo(),
166+
this.channelzEnabled
167+
);
156168
// Build user-agent string.
157169
this.userAgent = [
158170
options['grpc.primary_user_agent'],
@@ -174,20 +186,6 @@ class Http2Transport implements Transport {
174186
} else {
175187
this.keepaliveWithoutCalls = false;
176188
}
177-
if (this.keepaliveWithoutCalls) {
178-
this.maybeStartKeepalivePingTimer();
179-
}
180-
181-
this.subchannelAddressString = subchannelAddressToString(subchannelAddress);
182-
183-
if (options['grpc.enable_channelz'] === 0) {
184-
this.channelzEnabled = false;
185-
}
186-
this.channelzRef = registerChannelzSocket(
187-
this.subchannelAddressString,
188-
() => this.getChannelzInfo(),
189-
this.channelzEnabled
190-
);
191189

192190
session.once('close', () => {
193191
this.trace('session closed');
@@ -233,6 +231,11 @@ class Http2Transport implements Transport {
233231
);
234232
});
235233
}
234+
/* Start the keepalive timer last, because this can trigger trace logs,
235+
* which should only happen after everything else is set up. */
236+
if (this.keepaliveWithoutCalls) {
237+
this.maybeStartKeepalivePingTimer();
238+
}
236239
}
237240

238241
private getChannelzInfo(): SocketInfo {

packages/grpc-js/test/test-server.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ describe('Server', () => {
430430
(error: ServiceError, response: any) => {
431431
assert(error);
432432
assert.strictEqual(error.code, grpc.status.UNIMPLEMENTED);
433+
assert.match(error.details, /does not implement the method.*Div/);
433434
done();
434435
}
435436
);
@@ -439,6 +440,7 @@ describe('Server', () => {
439440
const call = client.sum((error: ServiceError, response: any) => {
440441
assert(error);
441442
assert.strictEqual(error.code, grpc.status.UNIMPLEMENTED);
443+
assert.match(error.details, /does not implement the method.*Sum/);
442444
done();
443445
});
444446

@@ -455,6 +457,7 @@ describe('Server', () => {
455457
call.on('error', (err: ServiceError) => {
456458
assert(err);
457459
assert.strictEqual(err.code, grpc.status.UNIMPLEMENTED);
460+
assert.match(err.details, /does not implement the method.*Fib/);
458461
done();
459462
});
460463
});
@@ -469,6 +472,93 @@ describe('Server', () => {
469472
call.on('error', (err: ServiceError) => {
470473
assert(err);
471474
assert.strictEqual(err.code, grpc.status.UNIMPLEMENTED);
475+
assert.match(err.details, /does not implement the method.*DivMany/);
476+
done();
477+
});
478+
479+
call.end();
480+
});
481+
});
482+
483+
describe('Unregistered service', () => {
484+
let server: Server;
485+
let client: ServiceClient;
486+
487+
const mathProtoFile = path.join(__dirname, 'fixtures', 'math.proto');
488+
const mathClient = (loadProtoFile(mathProtoFile).math as any).Math;
489+
490+
before(done => {
491+
server = new Server();
492+
// Don't register a service at all
493+
server.bindAsync(
494+
'localhost:0',
495+
ServerCredentials.createInsecure(),
496+
(err, port) => {
497+
assert.ifError(err);
498+
client = new mathClient(
499+
`localhost:${port}`,
500+
grpc.credentials.createInsecure()
501+
);
502+
server.start();
503+
done();
504+
}
505+
);
506+
});
507+
508+
after(done => {
509+
client.close();
510+
server.tryShutdown(done);
511+
});
512+
513+
it('should respond to a unary call with UNIMPLEMENTED', done => {
514+
client.div(
515+
{ divisor: 4, dividend: 3 },
516+
(error: ServiceError, response: any) => {
517+
assert(error);
518+
assert.strictEqual(error.code, grpc.status.UNIMPLEMENTED);
519+
assert.match(error.details, /does not implement the method.*Div/);
520+
done();
521+
}
522+
);
523+
});
524+
525+
it('should respond to a client stream with UNIMPLEMENTED', done => {
526+
const call = client.sum((error: ServiceError, response: any) => {
527+
assert(error);
528+
assert.strictEqual(error.code, grpc.status.UNIMPLEMENTED);
529+
assert.match(error.details, /does not implement the method.*Sum/);
530+
done();
531+
});
532+
533+
call.end();
534+
});
535+
536+
it('should respond to a server stream with UNIMPLEMENTED', done => {
537+
const call = client.fib({ limit: 5 });
538+
539+
call.on('data', (value: any) => {
540+
assert.fail('No messages expected');
541+
});
542+
543+
call.on('error', (err: ServiceError) => {
544+
assert(err);
545+
assert.strictEqual(err.code, grpc.status.UNIMPLEMENTED);
546+
assert.match(err.details, /does not implement the method.*Fib/);
547+
done();
548+
});
549+
});
550+
551+
it('should respond to a bidi call with UNIMPLEMENTED', done => {
552+
const call = client.divMany();
553+
554+
call.on('data', (value: any) => {
555+
assert.fail('No messages expected');
556+
});
557+
558+
call.on('error', (err: ServiceError) => {
559+
assert(err);
560+
assert.strictEqual(err.code, grpc.status.UNIMPLEMENTED);
561+
assert.match(err.details, /does not implement the method.*DivMany/);
472562
done();
473563
});
474564

0 commit comments

Comments
 (0)