Skip to content

Commit

Permalink
feat(interceptor): adds websockets to OgmaInterceptor's logging ability
Browse files Browse the repository at this point in the history
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
jmcdo29 committed Mar 22, 2020
1 parent 35bc265 commit 9c47252
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 8 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"postbuild:test": "rm -rf dist-test/src && mv dist-test/test/* dist-test && rmdir dist-test/test",
"build:test": "tsc -p tsconfig.test.json",
"start:test": "node dist-test/main.js",
"start:test:ws": "node dist-test/main.js ws",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"lint": "tslint -p tsconfig.json -c tslint.json",
"test": "jest",
Expand Down
7 changes: 6 additions & 1 deletion src/interceptor/ogma.interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
CallHandler,
ExecutionContext,
Inject,
Injectable,
NestInterceptor,
} from '@nestjs/common';
Expand All @@ -9,7 +10,10 @@ import { OgmaOptions } from 'ogma';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { OgmaInterceptorOptions } from '../interfaces/ogma-options.interface';
import { OGMA_INTERCEPTOR_SKIP } from '../ogma.constants';
import {
OGMA_INTERCEPTOR_OPTIONS,
OGMA_INTERCEPTOR_SKIP,
} from '../ogma.constants';
import { OgmaService } from '../ogma.service';
import { DelegatorService } from './delegator.service';
import { LogObject } from './interfaces/log.interface';
Expand All @@ -19,6 +23,7 @@ export class OgmaInterceptor implements NestInterceptor {
private json: boolean;
private color: boolean;
constructor(
@Inject(OGMA_INTERCEPTOR_OPTIONS)
private readonly options: OgmaInterceptorOptions,
private readonly service: OgmaService,
private readonly delegate: DelegatorService,
Expand Down
26 changes: 20 additions & 6 deletions src/interceptor/websocket-interceptor.service.ts
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;
}
}
3 changes: 3 additions & 0 deletions src/interfaces/ogma-types.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import {
FastifyLikeRequest,
FastifyLikeResponse,
} from './fastify-like.interface';
import { SocketIoLike } from './socket-io-like.interface';
import { WsLike } from './ws-like.interface';

export type OgmaRequest = FastifyLikeRequest | ExpressLikeRequest;
export type OgmaResponse = FastifyLikeResponse | ServerResponse;
export type OgmaClient = SocketIoLike | WsLike;
5 changes: 5 additions & 0 deletions src/interfaces/socket-io-like.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface SocketIoLike {
handshake: {
address: string;
};
}
5 changes: 5 additions & 0 deletions src/interfaces/ws-like.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface WsLike {
_socket: {
remoteAddress: string;
};
}
12 changes: 11 additions & 1 deletion src/ogma.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@ import {
import { createOgmaProvider } from './ogma.provider';
import { OgmaService } from './ogma.service';

@Module({})
@Module({
exports: [
OGMA_INTERCEPTOR_OPTIONS,
OGMA_SERVICE_OPTIONS,
OgmaService,
DelegatorService,
HttpInterceptorService,
WebsocketInterceptorService,
RpcInterceptorService,
],
})
export class OgmaModule extends createConfigurableDynamicRootModule<
OgmaModule,
OgmaModuleOptions
Expand Down
13 changes: 13 additions & 0 deletions test/app.gateway.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { BadRequestException, UseInterceptors } from '@nestjs/common';
import { SubscribeMessage, WebSocketGateway } from '@nestjs/websockets';
import { OgmaInterceptor } from '../dist';
import { AppService } from './app.service';

@UseInterceptors(OgmaInterceptor)
@WebSocketGateway()
export class AppGateway {
constructor(private readonly appService: AppService) {}
Expand All @@ -9,4 +12,14 @@ export class AppGateway {
handleMessage(): object {
return this.appService.getSimpleMessage();
}

@SubscribeMessage('diff')
handleOtherMessage(): object {
return this.appService.getSimpleMessage();
}

@SubscribeMessage('thrown')
throwAnError(): never {
throw new BadRequestException('error');
}
}
4 changes: 4 additions & 0 deletions test/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { NestFactory } from '@nestjs/core';
import { Transport } from '@nestjs/microservices';
import { WsAdapter } from '@nestjs/platform-ws';
import { AppModule } from './app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.connectMicroservice({
transport: Transport.TCP,
});
if (process.argv[2] === 'ws') {
app.useWebSocketAdapter(new WsAdapter(app));
}
await app.startAllMicroservicesAsync();
await app.listen(3001);
}
Expand Down

0 comments on commit 9c47252

Please sign in to comment.