-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(interceptor): adds websockets to OgmaInterceptor's logging ability
Websockets are now able to be logged via the Ogma Interceptor. However, unlike HTTP which can be done automatically via global binding, devs must use the `@UseInterceptors()` binding on either the message handler or the gateway. fix #8
- Loading branch information
Showing
9 changed files
with
68 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,44 @@ | ||
import { ExecutionContext, HttpException, Injectable } from '@nestjs/common'; | ||
import { MESSAGE_METADATA } from '@nestjs/websockets/constants'; | ||
import { OgmaClient } from '../interfaces/ogma-types.interface'; | ||
import { WsLike } from '../interfaces/ws-like.interface'; | ||
import { AbstractInterceptorService } from './abstract-interceptor.service'; | ||
|
||
@Injectable() | ||
export class WebsocketInterceptorService extends AbstractInterceptorService { | ||
getCallPoint(context: ExecutionContext): string { | ||
console.log(JSON.stringify(context.switchToWs(), null, 2)); | ||
return 'something'; | ||
return this.reflector.get<string>(MESSAGE_METADATA, context.getHandler()); | ||
} | ||
|
||
getCallerIp(context: ExecutionContext): string[] | string { | ||
return 'something'; | ||
const client = this.getClient(context); | ||
if (this.isWsClient(client)) { | ||
return client._socket.remoteAddress; | ||
} | ||
return client.handshake.address; | ||
} | ||
|
||
getMethod(context: ExecutionContext): string { | ||
return 'something'; | ||
return 'websocket'; | ||
} | ||
|
||
getProtocol(context: ExecutionContext): string { | ||
return 'something'; | ||
return 'WS'; | ||
} | ||
|
||
getStatus( | ||
context: ExecutionContext, | ||
inColor: boolean, | ||
error?: HttpException | Error, | ||
): string { | ||
return 'something'; | ||
return error ? '500' : '200'; | ||
} | ||
|
||
private getClient(context: ExecutionContext): OgmaClient { | ||
return context.switchToWs().getClient(); | ||
} | ||
|
||
private isWsClient(client: OgmaClient): client is WsLike { | ||
return Object.keys(client).indexOf('_socket') !== -1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface SocketIoLike { | ||
handshake: { | ||
address: string; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface WsLike { | ||
_socket: { | ||
remoteAddress: string; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters