-
-
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(mqtt): implements the mqtt parser for ogma
* feat(mqtt): implements the mqtt parser for ogma MQTT does not natively provide an option for retrieving the IP address of the client, so the only way to do it is to add the IP in the paylaod. Sucks, but it's life. Otherwise, super simple implementation of a parser. re #18 * ci(all): adds docker-compose for CI * ci(all): fix docker-compose version
- Loading branch information
Showing
12 changed files
with
429 additions
and
696 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,4 +35,4 @@ coverage/ | |
*.tgz | ||
|
||
*.txt | ||
*.gql | ||
*.gql |
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,12 @@ | ||
version: "3.3" | ||
services: | ||
mqtt: | ||
image: eclipse-mosquitto | ||
container_name: mqtt | ||
ports: | ||
- "1883:1883" | ||
# rabbitmq: | ||
# kafka: | ||
# grpc: | ||
# nats: | ||
# redis: |
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
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,11 +1,26 @@ | ||
# `@ogma/platform-mqtt` | ||
|
||
> TODO: description | ||
The `MqttParser` parser for the `OgmaInterceptor`. This plugin class parses MQTT request and response object to be able to successfully log the data about the request. For more information, check out [the @ogma/nestjs-module](../nestjs-module/README.md) documentation. | ||
|
||
## Installation | ||
|
||
Nothing special, standard `npm i @ogma/platform-mqtt` or `yarn add @ogma/platform-mqtt` | ||
|
||
## Usage | ||
|
||
``` | ||
const platformMqtt = require('@ogma/platform-mqtt'); | ||
This plugin is to be used in the `OgmaInterceptorOptions` portion of the `OgmaModule` during `forRoot` or `forRootAsync` registration. It can be used like so: | ||
|
||
// TODO: DEMONSTRATE API | ||
```ts | ||
@Module( | ||
OgmaModule.forRoot({ | ||
interceptor: { | ||
rpc: MqttParser | ||
} | ||
}) | ||
) | ||
export class AppModule {} | ||
``` | ||
|
||
## Important Notes | ||
|
||
Because of how MQTT requests are sent and the data available in them, to get the IP address in the request log, an IP property must be sent in the payload. This is the only way to get the IP address. If an IP property is not sent, the interceptor will use an empty string. [You can find more information here](https://stackoverflow.com/questions/45235080/how-to-know-the-ip-address-of-mqtt-client-in-node-js). |
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 @@ | ||
export * from './mqtt-interceptor.service'; |
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,41 @@ | ||
import { ExecutionContext, Injectable } from '@nestjs/common'; | ||
import { MqttContext } from '@nestjs/microservices'; | ||
import { PATTERN_METADATA } from '@nestjs/microservices/constants'; | ||
import { AbstractInterceptorService } from '@ogma/nestjs-module'; | ||
|
||
@Injectable() | ||
export class MqttParser extends AbstractInterceptorService { | ||
getCallPoint(context: ExecutionContext): string { | ||
return JSON.stringify( | ||
this.reflector.get(PATTERN_METADATA, context.getHandler()), | ||
); | ||
} | ||
|
||
getCallerIp(context: ExecutionContext): string { | ||
const client = this.getClient(context); | ||
const packet = client.getPacket(); | ||
const payload = JSON.parse(packet.payload.toString()); | ||
return payload.data?.ip || ''; | ||
} | ||
|
||
getMethod(): string { | ||
return 'MQTT'; | ||
} | ||
|
||
getStatus( | ||
context: ExecutionContext, | ||
inColor: boolean, | ||
error?: Error | ExecutionContext, | ||
): string { | ||
const status = error ? 500 : 200; | ||
return inColor ? this.wrapInColor(status) : status.toString(); | ||
} | ||
|
||
getProtocol(): string { | ||
return 'mqtt'; | ||
} | ||
|
||
private getClient(context: ExecutionContext): MqttContext { | ||
return context.switchToRpc().getContext<MqttContext>(); | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
packages/platform-mqtt/test/mqtt-interceptor.service.spec.ts
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,107 @@ | ||
import { createMock } from '@golevelup/ts-jest'; | ||
import { ExecutionContext } from '@nestjs/common'; | ||
import { Test } from '@nestjs/testing'; | ||
import { MqttParser } from '../src'; | ||
import { Reflector } from '@nestjs/core'; | ||
import { PATTERN_METADATA } from '@nestjs/microservices/constants'; | ||
import { color } from '@ogma/logger'; | ||
|
||
describe('MQTTParser', () => { | ||
let parser: MqttParser; | ||
let reflector: Reflector; | ||
|
||
beforeEach(async () => { | ||
const modRef = await Test.createTestingModule({ | ||
providers: [ | ||
MqttParser, | ||
{ | ||
provide: Reflector, | ||
useValue: { | ||
get: jest.fn(() => 'message'), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
parser = modRef.get(MqttParser); | ||
reflector = modRef.get(Reflector); | ||
}); | ||
describe('getCallPoint', () => { | ||
it('should get the reflected message', () => { | ||
const funcMock = () => 'string'; | ||
const ctxMock = createMock<ExecutionContext>({ | ||
getHandler: funcMock, | ||
}); | ||
expect(parser.getCallPoint(ctxMock)).toBe(JSON.stringify('message')); | ||
expect(reflector.get).toBeCalledWith(PATTERN_METADATA, funcMock()); | ||
}); | ||
}); | ||
describe('getCallerIp', () => { | ||
it('should return an ip from the payload', () => { | ||
const ctxMock = createMock<ExecutionContext>({ | ||
switchToRpc: () => ({ | ||
getContext: () => ({ | ||
getPacket: () => ({ | ||
payload: Buffer.from( | ||
JSON.stringify({ data: { hello: 'world', ip: '127.0.0.1' } }), | ||
), | ||
}), | ||
}), | ||
}), | ||
}); | ||
expect(parser.getCallerIp(ctxMock)).toBe('127.0.0.1'); | ||
}); | ||
it('should return an empty string from the payload without ip as a prop', () => { | ||
const ctxMock = createMock<ExecutionContext>({ | ||
switchToRpc: () => ({ | ||
getContext: () => ({ | ||
getPacket: () => ({ | ||
payload: Buffer.from( | ||
JSON.stringify({ data: { hello: 'world' } }), | ||
), | ||
}), | ||
}), | ||
}), | ||
}); | ||
expect(parser.getCallerIp(ctxMock)).toBe(''); | ||
}); | ||
it('should return an empty string', () => { | ||
const ctxMock = createMock<ExecutionContext>({ | ||
switchToRpc: () => ({ | ||
getContext: () => ({ | ||
getPacket: () => ({ | ||
payload: Buffer.from(JSON.stringify({})), | ||
}), | ||
}), | ||
}), | ||
}); | ||
expect(parser.getCallerIp(ctxMock)).toBe(''); | ||
}); | ||
}); | ||
describe('getMethod', () => { | ||
it('should return "MQTT"', () => { | ||
expect(parser.getMethod()).toBe('MQTT'); | ||
}); | ||
}); | ||
describe('getStatus', () => { | ||
it('should return a 200', () => { | ||
expect(parser.getStatus(createMock<ExecutionContext>(), false)).toBe( | ||
'200', | ||
); | ||
}); | ||
it('should return a 500', () => { | ||
expect( | ||
parser.getStatus(createMock<ExecutionContext>(), false, new Error()), | ||
).toBe('500'); | ||
}); | ||
it('should return a 200 in color', () => { | ||
expect(parser.getStatus(createMock<ExecutionContext>(), true)).toBe( | ||
color.green(200), | ||
); | ||
}); | ||
}); | ||
describe('getProtocol', () => { | ||
it('should return "mqtt"', () => { | ||
expect(parser.getProtocol()).toBe('mqtt'); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.