Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grpc-js: Implement getPeer on the client and server #1526

Merged
merged 1 commit into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/grpc-js/src/call-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions packages/grpc-js/src/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export class ClientUnaryCallImpl extends EventEmitter
}

getPeer(): string {
return this.call?.getPeer() ?? '';
return this.call?.getPeer() ?? 'unknown';
}
}

Expand All @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
21 changes: 17 additions & 4 deletions packages/grpc-js/src/server-call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 */
Expand Down