grpc-js: Implement getPeer on the client and server

This commit is contained in:
Michael Lumish 2020-08-04 13:03:08 -07:00
parent f08d3aefd0
commit 33a4c85f89
3 changed files with 22 additions and 9 deletions

View File

@ -630,7 +630,7 @@ export class Http2CallStream implements Call {
}
getPeer(): string {
throw new Error('Not yet implemented');
return this.subchannel?.getAddress() ?? this.channel.getTarget();
}
getMethod(): string {

View File

@ -93,7 +93,7 @@ export class ClientUnaryCallImpl extends EventEmitter
}
getPeer(): string {
return this.call?.getPeer() ?? '';
return this.call?.getPeer() ?? 'unknown';
}
}
@ -109,7 +109,7 @@ export class ClientReadableStreamImpl<ResponseType> extends Readable
}
getPeer(): string {
return this.call?.getPeer() ?? '';
return this.call?.getPeer() ?? 'unknown';
}
_read(_size: number): void {
@ -129,7 +129,7 @@ export class ClientWritableStreamImpl<RequestType> extends Writable
}
getPeer(): string {
return this.call?.getPeer() ?? '';
return this.call?.getPeer() ?? 'unknown';
}
_write(chunk: RequestType, encoding: string, cb: WriteCallback) {
@ -164,7 +164,7 @@ export class ClientDuplexStreamImpl<RequestType, ResponseType> extends Duplex
}
getPeer(): string {
return this.call?.getPeer() ?? '';
return this.call?.getPeer() ?? 'unknown';
}
_read(_size: number): void {

View File

@ -112,7 +112,7 @@ export class ServerUnaryCallImpl<RequestType, ResponseType> extends EventEmitter
}
getPeer(): string {
throw new Error('not implemented yet');
return this.call.getPeer();
}
sendMetadata(responseMetadata: Metadata): void {
@ -145,7 +145,7 @@ export class ServerReadableStreamImpl<RequestType, ResponseType>
}
getPeer(): string {
throw new Error('not implemented yet');
return this.call.getPeer();
}
sendMetadata(responseMetadata: Metadata): void {
@ -178,7 +178,7 @@ export class ServerWritableStreamImpl<RequestType, ResponseType>
}
getPeer(): string {
throw new Error('not implemented yet');
return this.call.getPeer();
}
sendMetadata(responseMetadata: Metadata): void {
@ -249,7 +249,7 @@ export class ServerDuplexStreamImpl<RequestType, ResponseType> extends Duplex
}
getPeer(): string {
throw new Error('not implemented yet');
return this.call.getPeer();
}
sendMetadata(responseMetadata: Metadata): void {
@ -738,6 +738,19 @@ export class Http2ServerCallStream<
);
}
}
getPeer(): string {
const socket = this.stream.session.socket;
if (socket.remoteAddress) {
if (socket.remotePort) {
return `${socket.remoteAddress}:${socket.remotePort}`;
} else {
return socket.remoteAddress;
}
} else {
return 'unknown';
}
}
}
/* eslint-disable @typescript-eslint/no-explicit-any */