From c3e657e215c2dce572c72447444d9c787352f728 Mon Sep 17 00:00:00 2001 From: adrigzr Date: Thu, 23 Mar 2023 10:46:45 +0100 Subject: [PATCH] refactor(transport-tcp): rename `object` to `data` in `Payload` --- .../packet-connection.aggregate-root.ts | 18 +++------ .../client-login.event-handler.ts | 2 +- .../device-battery-update.event-handler.ts | 2 +- ...ice-clean-map-data-report.event-handler.ts | 2 +- .../device-clean-map-report.event-handler.ts | 2 +- ...p-charger-position-update.event-handler.ts | 2 +- .../device-map-update.event-handler.ts | 2 +- ...ce-map-work-status-update.event-handler.ts | 2 +- .../device-register.event-handler.ts | 2 +- .../device-settings-update.event-handler.ts | 2 +- .../device-version-update.event-handler.ts | 2 +- .../device-wlan-update.event-handler.ts | 2 +- .../src/packet-server.connection-handler.ts | 12 +++--- packages/adapter-tcp/src/packet.event-bus.ts | 4 +- .../adapter-tcp/src/packet.event-handler.ts | 4 +- packages/adapter-tcp/src/packet.message.ts | 8 ++-- packages/adapter-tcp/src/tcp.server.ts | 4 +- .../test/integration/tcp.server.test.ts | 14 +++---- packages/cli/src/cli.ts | 4 +- .../cli/src/commands/decode.command.test.ts | 8 ++-- .../cli/src/commands/encode.command.test.ts | 4 +- .../cli/src/commands/read.command.test.ts | 8 ++-- .../packet-encode-transform.stream.test.ts | 2 +- .../streams/packet-encode-transform.stream.ts | 8 ++-- packages/cli/src/test-support.ts | 2 +- .../src/constants/payloads.constant.ts | 8 ++-- .../src/factories/packet.factory.test.ts | 4 +- .../src/factories/packet.factory.ts | 16 ++++---- packages/transport-tcp/src/index.ts | 2 +- .../src/mappers/packet.mapper.test.ts | 2 +- .../src/mappers/packet.mapper.ts | 4 +- .../src/mappers/payload.mapper.test.ts | 24 ++++++------ .../src/mappers/payload.mapper.ts | 24 ++++++------ ...ts => payload-data-parser.service.test.ts} | 38 +++++++++---------- ...vice.ts => payload-data-parser.service.ts} | 20 +++++----- packages/transport-tcp/src/test-support.ts | 2 +- .../src/utils/get-custom-decoders.util.ts | 2 +- .../value-objects/packet.value-object.test.ts | 4 +- .../src/value-objects/packet.value-object.ts | 6 +-- .../payload.value-object.test.ts | 22 +++++------ .../src/value-objects/payload.value-object.ts | 22 +++++------ 41 files changed, 158 insertions(+), 164 deletions(-) rename packages/transport-tcp/src/services/{payload-object-parser.service.test.ts => payload-data-parser.service.test.ts} (82%) rename packages/transport-tcp/src/services/{payload-object-parser.service.ts => payload-data-parser.service.ts} (63%) diff --git a/packages/adapter-tcp/src/aggregate-roots/packet-connection.aggregate-root.ts b/packages/adapter-tcp/src/aggregate-roots/packet-connection.aggregate-root.ts index d817e1ee..f474091f 100644 --- a/packages/adapter-tcp/src/aggregate-roots/packet-connection.aggregate-root.ts +++ b/packages/adapter-tcp/src/aggregate-roots/packet-connection.aggregate-root.ts @@ -4,13 +4,7 @@ import { PacketSocket } from '@agnoc/transport-tcp'; import type { PacketEventBus } from '../packet.event-bus'; import type { PacketMessage } from '../packet.message'; import type { ConnectionProps } from '@agnoc/domain'; -import type { - Packet, - PacketFactory, - PayloadObjectName, - PayloadObjectFrom, - CreatePacketProps, -} from '@agnoc/transport-tcp'; +import type { Packet, PacketFactory, PayloadDataName, PayloadDataFrom, CreatePacketProps } from '@agnoc/transport-tcp'; export interface PacketConnectionProps extends ConnectionProps { socket: PacketSocket; @@ -31,25 +25,25 @@ export class PacketConnection extends Connection { return this.props.socket; } - send(name: Name, object: PayloadObjectFrom): Promise { + send(name: Name, object: PayloadDataFrom): Promise { const packet = this.packetFactory.create(name, object, this.getPacketProps()); return this.write(packet); } - respond(name: Name, object: PayloadObjectFrom, packet: Packet): Promise { + respond(name: Name, object: PayloadDataFrom, packet: Packet): Promise { return this.write(this.packetFactory.create(name, object, packet)); } - sendAndWait(name: Name, object: PayloadObjectFrom): Promise { + sendAndWait(name: Name, object: PayloadDataFrom): Promise { const packet = this.packetFactory.create(name, object, this.getPacketProps()); return this.writeAndWait(packet); } - respondAndWait( + respondAndWait( name: Name, - object: PayloadObjectFrom, + object: PayloadDataFrom, packet: Packet, ): Promise { return this.writeAndWait(this.packetFactory.create(name, object, packet)); diff --git a/packages/adapter-tcp/src/packet-event-handlers/client-login.event-handler.ts b/packages/adapter-tcp/src/packet-event-handlers/client-login.event-handler.ts index f5a89dd4..511b98b1 100644 --- a/packages/adapter-tcp/src/packet-event-handlers/client-login.event-handler.ts +++ b/packages/adapter-tcp/src/packet-event-handlers/client-login.event-handler.ts @@ -8,7 +8,7 @@ export class ClientLoginEventHandler implements PacketEventHandler { if (!message.device) { const data = { result: 12002, - reason: `Device not registered(devsn: ${message.packet.payload.object.deviceSerialNumber})`, + reason: `Device not registered(devsn: ${message.packet.payload.data.deviceSerialNumber})`, }; return message.respond('CLIENT_ONLINE_RSP', data); diff --git a/packages/adapter-tcp/src/packet-event-handlers/device-battery-update.event-handler.ts b/packages/adapter-tcp/src/packet-event-handlers/device-battery-update.event-handler.ts index 848282a9..8f35f416 100644 --- a/packages/adapter-tcp/src/packet-event-handlers/device-battery-update.event-handler.ts +++ b/packages/adapter-tcp/src/packet-event-handlers/device-battery-update.event-handler.ts @@ -13,7 +13,7 @@ export class DeviceBatteryUpdateEventHandler implements PacketEventHandler { throw new DomainException('Device not found'); } - const data = message.packet.payload.object; + const data = message.packet.payload.data; message.device.updateBattery(this.deviceBatteryMapper.toDomain(data.battery.level)); diff --git a/packages/adapter-tcp/src/packet-event-handlers/device-clean-map-data-report.event-handler.ts b/packages/adapter-tcp/src/packet-event-handlers/device-clean-map-data-report.event-handler.ts index b5ff40bc..e1abd193 100644 --- a/packages/adapter-tcp/src/packet-event-handlers/device-clean-map-data-report.event-handler.ts +++ b/packages/adapter-tcp/src/packet-event-handlers/device-clean-map-data-report.event-handler.ts @@ -10,7 +10,7 @@ export class DeviceCleanMapDataReportEventHandler implements PacketEventHandler throw new DomainException('Device not found'); } - const data = message.packet.payload.object; + const data = message.packet.payload.data; // TODO: save device clean map data diff --git a/packages/adapter-tcp/src/packet-event-handlers/device-clean-map-report.event-handler.ts b/packages/adapter-tcp/src/packet-event-handlers/device-clean-map-report.event-handler.ts index cf54020a..a8b0098c 100644 --- a/packages/adapter-tcp/src/packet-event-handlers/device-clean-map-report.event-handler.ts +++ b/packages/adapter-tcp/src/packet-event-handlers/device-clean-map-report.event-handler.ts @@ -10,7 +10,7 @@ export class DeviceCleanMapReportEventHandler implements PacketEventHandler { throw new DomainException('Device not found'); } - const data = message.packet.payload.object; + const data = message.packet.payload.data; // TODO: save device clean map data diff --git a/packages/adapter-tcp/src/packet-event-handlers/device-map-charger-position-update.event-handler.ts b/packages/adapter-tcp/src/packet-event-handlers/device-map-charger-position-update.event-handler.ts index d42d02df..4488a8f6 100644 --- a/packages/adapter-tcp/src/packet-event-handlers/device-map-charger-position-update.event-handler.ts +++ b/packages/adapter-tcp/src/packet-event-handlers/device-map-charger-position-update.event-handler.ts @@ -11,7 +11,7 @@ export class DeviceMapChargerPositionUpdateEventHandler implements PacketEventHa throw new DomainException('Device not found'); } - const data = message.packet.payload.object; + const data = message.packet.payload.data; message.device.map?.updateCharger( new MapPosition({ diff --git a/packages/adapter-tcp/src/packet-event-handlers/device-map-update.event-handler.ts b/packages/adapter-tcp/src/packet-event-handlers/device-map-update.event-handler.ts index c165d7e1..21a5d52b 100644 --- a/packages/adapter-tcp/src/packet-event-handlers/device-map-update.event-handler.ts +++ b/packages/adapter-tcp/src/packet-event-handlers/device-map-update.event-handler.ts @@ -47,7 +47,7 @@ export class DeviceMapUpdateEventHandler implements PacketEventHandler { spotInfo, cleanPlanList, currentPlanId, - } = message.packet.payload.object; + } = message.packet.payload.data; if (statusInfo) { const { diff --git a/packages/adapter-tcp/src/packet-event-handlers/device-map-work-status-update.event-handler.ts b/packages/adapter-tcp/src/packet-event-handlers/device-map-work-status-update.event-handler.ts index ba256f7f..84d320cd 100644 --- a/packages/adapter-tcp/src/packet-event-handlers/device-map-work-status-update.event-handler.ts +++ b/packages/adapter-tcp/src/packet-event-handlers/device-map-work-status-update.event-handler.ts @@ -37,7 +37,7 @@ export class DeviceMapWorkStatusUpdateEventHandler implements PacketEventHandler mopType, cleanSize, cleanTime, - } = message.packet.payload.object; + } = message.packet.payload.data; message.device.updateCurrentClean( new DeviceCleanWork({ diff --git a/packages/adapter-tcp/src/packet-event-handlers/device-register.event-handler.ts b/packages/adapter-tcp/src/packet-event-handlers/device-register.event-handler.ts index c7d25287..ba998bd0 100644 --- a/packages/adapter-tcp/src/packet-event-handlers/device-register.event-handler.ts +++ b/packages/adapter-tcp/src/packet-event-handlers/device-register.event-handler.ts @@ -10,7 +10,7 @@ export class DeviceRegisterEventHandler implements PacketEventHandler { constructor(private readonly deviceRepository: DeviceRepository) {} async handle(message: PacketMessage<'DEVICE_REGISTER_REQ'>): Promise { - const data = message.packet.payload.object; + const data = message.packet.payload.data; const device = new Device({ id: ID.generate(), userId: ID.generate(), diff --git a/packages/adapter-tcp/src/packet-event-handlers/device-settings-update.event-handler.ts b/packages/adapter-tcp/src/packet-event-handlers/device-settings-update.event-handler.ts index 2dd0e749..92c03ae9 100644 --- a/packages/adapter-tcp/src/packet-event-handlers/device-settings-update.event-handler.ts +++ b/packages/adapter-tcp/src/packet-event-handlers/device-settings-update.event-handler.ts @@ -14,7 +14,7 @@ export class DeviceSettingsUpdateEventHandler implements PacketEventHandler { throw new DomainException('Device not found'); } - const data = message.packet.payload.object; + const data = message.packet.payload.data; const deviceSettings = new DeviceSettings({ voice: this.deviceVoiceMapper.toDomain({ isEnabled: data.voice.voiceMode, diff --git a/packages/adapter-tcp/src/packet-event-handlers/device-version-update.event-handler.ts b/packages/adapter-tcp/src/packet-event-handlers/device-version-update.event-handler.ts index 370dd53e..19eeef52 100644 --- a/packages/adapter-tcp/src/packet-event-handlers/device-version-update.event-handler.ts +++ b/packages/adapter-tcp/src/packet-event-handlers/device-version-update.event-handler.ts @@ -11,7 +11,7 @@ export class DeviceVersionUpdateEventHandler implements PacketEventHandler { throw new DomainException('Device not found'); } - const data = message.packet.payload.object; + const data = message.packet.payload.data; message.device.updateVersion(new DeviceVersion({ software: data.softwareVersion, hardware: data.hardwareVersion })); diff --git a/packages/adapter-tcp/src/packet-event-handlers/device-wlan-update.event-handler.ts b/packages/adapter-tcp/src/packet-event-handlers/device-wlan-update.event-handler.ts index 96b705f0..b4cccff1 100644 --- a/packages/adapter-tcp/src/packet-event-handlers/device-wlan-update.event-handler.ts +++ b/packages/adapter-tcp/src/packet-event-handlers/device-wlan-update.event-handler.ts @@ -11,7 +11,7 @@ export class DeviceWlanUpdateEventHandler implements PacketEventHandler { throw new DomainException('Device not found'); } - const data = message.packet.payload.object.body; + const data = message.packet.payload.data.body; message.device.updateWlan( new DeviceWlan({ diff --git a/packages/adapter-tcp/src/packet-server.connection-handler.ts b/packages/adapter-tcp/src/packet-server.connection-handler.ts index e183361b..72ed16b4 100644 --- a/packages/adapter-tcp/src/packet-server.connection-handler.ts +++ b/packages/adapter-tcp/src/packet-server.connection-handler.ts @@ -4,7 +4,7 @@ import type { PacketConnection } from './aggregate-roots/packet-connection.aggre import type { PacketConnectionFactory } from './factories/connection.factory'; import type { PacketEventBus, PacketEventBusEvents } from './packet.event-bus'; import type { DeviceRepository, Device, Connection, ConnectionRepository } from '@agnoc/domain'; -import type { PacketServer, Packet, PayloadObjectName } from '@agnoc/transport-tcp'; +import type { PacketServer, Packet, PayloadDataName } from '@agnoc/transport-tcp'; export class PackerServerConnectionHandler { private readonly servers = new Map>(); @@ -55,21 +55,21 @@ export class PackerServerConnectionHandler { }); } - private async emitPacketEvent(message: PacketMessage) { - const name = message.packet.payload.opcode.name as PayloadObjectName; + private async emitPacketEvent(message: PacketMessage) { + const name = message.packet.payload.opcode.name as PayloadDataName; const sequence = message.packet.sequence.toString(); this.checkForPacketEventHandler(name); // Emit the packet event by the sequence string. // This is used to wait for a response from a packet. - await this.packetEventBus.emit(sequence, message as PacketEventBusEvents[PayloadObjectName]); + await this.packetEventBus.emit(sequence, message as PacketEventBusEvents[PayloadDataName]); // Emit the packet event by the opcode name. - await this.packetEventBus.emit(name, message as PacketEventBusEvents[PayloadObjectName]); + await this.packetEventBus.emit(name, message as PacketEventBusEvents[PayloadDataName]); } - private checkForPacketEventHandler(event: PayloadObjectName) { + private checkForPacketEventHandler(event: PayloadDataName) { const count = this.packetEventBus.listenerCount(event); // Throw an error if there is no event handler for the packet event. diff --git a/packages/adapter-tcp/src/packet.event-bus.ts b/packages/adapter-tcp/src/packet.event-bus.ts index d1a2109d..f5387f2d 100644 --- a/packages/adapter-tcp/src/packet.event-bus.ts +++ b/packages/adapter-tcp/src/packet.event-bus.ts @@ -1,10 +1,10 @@ import { EventBus } from '@agnoc/toolkit'; import type { PacketMessage } from './packet.message'; -import type { PayloadObjectName } from '@agnoc/transport-tcp'; +import type { PayloadDataName } from '@agnoc/transport-tcp'; /** Events for the packet event bus. */ export type PacketEventBusEvents = { - [Name in PayloadObjectName]: PacketMessage; + [Name in PayloadDataName]: PacketMessage; } & { [key: string]: PacketMessage; }; diff --git a/packages/adapter-tcp/src/packet.event-handler.ts b/packages/adapter-tcp/src/packet.event-handler.ts index 50974cf5..4d8bbbbe 100644 --- a/packages/adapter-tcp/src/packet.event-handler.ts +++ b/packages/adapter-tcp/src/packet.event-handler.ts @@ -1,11 +1,11 @@ import type { PacketMessage } from './packet.message'; import type { EventHandler } from '@agnoc/toolkit'; -import type { PayloadObjectName } from '@agnoc/transport-tcp'; +import type { PayloadDataName } from '@agnoc/transport-tcp'; /** Base class for packet event handlers. */ export abstract class PacketEventHandler implements EventHandler { /** The name of the event to listen to. */ - abstract forName: PayloadObjectName; + abstract forName: PayloadDataName; /** Handle the event. */ abstract handle(message: PacketMessage): void; diff --git a/packages/adapter-tcp/src/packet.message.ts b/packages/adapter-tcp/src/packet.message.ts index b3e7d9fc..8bdda9bf 100644 --- a/packages/adapter-tcp/src/packet.message.ts +++ b/packages/adapter-tcp/src/packet.message.ts @@ -1,19 +1,19 @@ import type { PacketConnection } from './aggregate-roots/packet-connection.aggregate-root'; import type { Device } from '@agnoc/domain'; -import type { Packet, PayloadObjectFrom, PayloadObjectName } from '@agnoc/transport-tcp'; +import type { Packet, PayloadDataFrom, PayloadDataName } from '@agnoc/transport-tcp'; -export class PacketMessage { +export class PacketMessage { constructor(readonly connection: PacketConnection, readonly packet: Packet) {} get device(): Device | undefined { return this.connection.device; } - respond(name: Name, object: PayloadObjectFrom): Promise { + respond(name: Name, object: PayloadDataFrom): Promise { return this.connection.respond(name, object, this.packet); } - respondAndWait(name: Name, object: PayloadObjectFrom): Promise { + respondAndWait(name: Name, object: PayloadDataFrom): Promise { return this.connection.respondAndWait(name, object, this.packet); } } diff --git a/packages/adapter-tcp/src/tcp.server.ts b/packages/adapter-tcp/src/tcp.server.ts index faafdbbd..ae68c898 100644 --- a/packages/adapter-tcp/src/tcp.server.ts +++ b/packages/adapter-tcp/src/tcp.server.ts @@ -5,7 +5,7 @@ import { PacketMapper, PacketServer, PayloadMapper, - PayloadObjectParserService, + PayloadDataParserService, PacketFactory, } from '@agnoc/transport-tcp'; import { LocateDeviceEventHandler } from './command-handlers/locate-device.event-handler'; @@ -59,7 +59,7 @@ export class TCPServer implements Server { private readonly commandQueryHandlerRegistry: TaskHandlerRegistry, ) { // Packet foundation - const payloadMapper = new PayloadMapper(new PayloadObjectParserService(getProtobufRoot(), getCustomDecoders())); + const payloadMapper = new PayloadMapper(new PayloadDataParserService(getProtobufRoot(), getCustomDecoders())); const packetMapper = new PacketMapper(payloadMapper); const packetFactory = new PacketFactory(); diff --git a/packages/adapter-tcp/test/integration/tcp.server.test.ts b/packages/adapter-tcp/test/integration/tcp.server.test.ts index 8e397c95..c5f3d057 100644 --- a/packages/adapter-tcp/test/integration/tcp.server.test.ts +++ b/packages/adapter-tcp/test/integration/tcp.server.test.ts @@ -10,7 +10,7 @@ import { PacketMapper, PacketSocket, PayloadMapper, - PayloadObjectParserService, + PayloadDataParserService, } from '@agnoc/transport-tcp'; import { expect } from 'chai'; import { TCPServer } from '@agnoc/adapter-tcp'; @@ -48,7 +48,7 @@ describe('Integration', function () { ); // Client blocks - const payloadMapper = new PayloadMapper(new PayloadObjectParserService(getProtobufRoot(), getCustomDecoders())); + const payloadMapper = new PayloadMapper(new PayloadDataParserService(getProtobufRoot(), getCustomDecoders())); const packetMapper = new PacketMapper(payloadMapper); packetSocket = new PacketSocket(packetMapper); @@ -79,7 +79,7 @@ describe('Integration', function () { expect(receivedPacket.deviceId.equals(sentPacket.deviceId)).to.be.true; expect(receivedPacket.userId.equals(sentPacket.userId)).to.be.true; expect(receivedPacket.payload.opcode.value).to.be.equal('CLIENT_HEARTBEAT_RSP'); - expect(receivedPacket.payload.object).to.be.deep.equal({}); + expect(receivedPacket.payload.data).to.be.deep.equal({}); }); it('should handle heartbeat packets on map server', async function () { @@ -100,7 +100,7 @@ describe('Integration', function () { expect(receivedPacket.deviceId.equals(sentPacket.deviceId)).to.be.true; expect(receivedPacket.userId.equals(sentPacket.userId)).to.be.true; expect(receivedPacket.payload.opcode.value).to.be.equal('CLIENT_HEARTBEAT_RSP'); - expect(receivedPacket.payload.object).to.be.deep.equal({}); + expect(receivedPacket.payload.data).to.be.deep.equal({}); }); it('should handle ntp connections', async function () { @@ -117,8 +117,8 @@ describe('Integration', function () { expect(receivedPacket.deviceId.value).to.be.equal(0); expect(receivedPacket.userId.value).to.be.equal(0); expect(receivedPacket.payload.opcode.value).to.be.equal('DEVICE_TIME_SYNC_RSP'); - expect(receivedPacket.payload.object.result).to.be.equal(0); - expect(receivedPacket.payload.object.body.time).to.be.greaterThanOrEqual(now); + expect(receivedPacket.payload.data.result).to.be.equal(0); + expect(receivedPacket.payload.data.body.time).to.be.greaterThanOrEqual(now); }); }); @@ -136,7 +136,7 @@ describe('Integration', function () { expect(receivedPacket.payload.opcode.value).to.be.equal('DEVICE_REGISTER_RSP'); - const device = await deviceRepository.findOneById(new ID(receivedPacket.payload.object.device.id)); + const device = await deviceRepository.findOneById(new ID(receivedPacket.payload.data.device.id)); expect(device).to.exist; }); diff --git a/packages/cli/src/cli.ts b/packages/cli/src/cli.ts index 5cec9e8a..dd344d82 100644 --- a/packages/cli/src/cli.ts +++ b/packages/cli/src/cli.ts @@ -1,6 +1,6 @@ /* istanbul ignore file */ import { - PayloadObjectParserService, + PayloadDataParserService, getProtobufRoot, PacketMapper, getCustomDecoders, @@ -33,7 +33,7 @@ export function main(): void { stderr: process.stderr, }; - const payloadMapper = new PayloadMapper(new PayloadObjectParserService(getProtobufRoot(), getCustomDecoders())); + const payloadMapper = new PayloadMapper(new PayloadDataParserService(getProtobufRoot(), getCustomDecoders())); const packetMapper = new PacketMapper(payloadMapper); const decodeCommand = new DecodeCommand(stdio, packetMapper); const readCommand = new ReadCommand(stdio, packetMapper); diff --git a/packages/cli/src/commands/decode.command.test.ts b/packages/cli/src/commands/decode.command.test.ts index f4519a74..cd11297e 100644 --- a/packages/cli/src/commands/decode.command.test.ts +++ b/packages/cli/src/commands/decode.command.test.ts @@ -47,7 +47,7 @@ describe('DecodeCommand', function () { const [ret] = await readStream(stdio.stdout, 'utf8'); expect(ret).to.equal( - '[fb3dd1ebc0e6c58f] [ctype: 2] [flow: 1] [userId: 4] [deviceId: 3] {"opcode":"DEVICE_GETTIME_RSP","object":{"result":0,"body":{"deviceTime":1606129555,"deviceTimezone":3600}}}\n', + '[fb3dd1ebc0e6c58f] [ctype: 2] [flow: 1] [userId: 4] [deviceId: 3] {"opcode":"DEVICE_GETTIME_RSP","data":{"result":0,"body":{"deviceTime":1606129555,"deviceTimezone":3600}}}\n', ); verify(packetMapper.toDomain(deepEqual(buffer))).once(); @@ -73,7 +73,7 @@ describe('DecodeCommand', function () { sequence: 'fb3dd1ebc0e6c58f', payload: { opcode: 'DEVICE_GETTIME_RSP', - object: { result: +0, body: { deviceTime: 1606129555, deviceTimezone: 3600 } }, + data: { result: +0, body: { deviceTime: 1606129555, deviceTimezone: 3600 } }, }, }, ]); @@ -91,7 +91,7 @@ describe('DecodeCommand', function () { const [ret] = await readStream(stdio.stdout, 'utf8'); expect(ret).to.equal( - '[fb3dd1ebc0e6c58f] [ctype: 2] [flow: 1] [userId: 4] [deviceId: 3] {"opcode":"DEVICE_GETTIME_RSP","object":{"result":0,"body":{"deviceTime":1606129555,"deviceTimezone":3600}}}\n', + '[fb3dd1ebc0e6c58f] [ctype: 2] [flow: 1] [userId: 4] [deviceId: 3] {"opcode":"DEVICE_GETTIME_RSP","data":{"result":0,"body":{"deviceTime":1606129555,"deviceTimezone":3600}}}\n', ); verify(packetMapper.toDomain(deepEqual(buffer))).once(); @@ -115,7 +115,7 @@ describe('DecodeCommand', function () { sequence: 'fb3dd1ebc0e6c58f', payload: { opcode: 'DEVICE_GETTIME_RSP', - object: { result: +0, body: { deviceTime: 1606129555, deviceTimezone: 3600 } }, + data: { result: +0, body: { deviceTime: 1606129555, deviceTimezone: 3600 } }, }, }, ]); diff --git a/packages/cli/src/commands/encode.command.test.ts b/packages/cli/src/commands/encode.command.test.ts index f4077d24..04985700 100644 --- a/packages/cli/src/commands/encode.command.test.ts +++ b/packages/cli/src/commands/encode.command.test.ts @@ -61,7 +61,7 @@ describe('EncodeCommand', function () { sequence: PacketSequence.fromString(jsonPacket.sequence), payload: new Payload({ opcode: OPCode.fromName(jsonPacket.payload.opcode), - object: jsonPacket.payload.object, + data: jsonPacket.payload.data, }), }), ), @@ -91,7 +91,7 @@ describe('EncodeCommand', function () { sequence: PacketSequence.fromString(jsonPacket.sequence), payload: new Payload({ opcode: OPCode.fromName(jsonPacket.payload.opcode), - object: jsonPacket.payload.object, + data: jsonPacket.payload.data, }), }), ), diff --git a/packages/cli/src/commands/read.command.test.ts b/packages/cli/src/commands/read.command.test.ts index 4dfd208b..1561b781 100644 --- a/packages/cli/src/commands/read.command.test.ts +++ b/packages/cli/src/commands/read.command.test.ts @@ -35,10 +35,10 @@ describe('ReadCommand', function () { const [ret1, ret2] = await readStream(stdio.stdout, 'utf8'); expect(ret1).to.equal( - '[fb3dd1ebc0e6c58f] [ctype: 2] [flow: 1] [userId: 4] [deviceId: 3] {"opcode":"DEVICE_GETTIME_RSP","object":{"result":0,"body":{"deviceTime":1606129555,"deviceTimezone":3600}}}\n', + '[fb3dd1ebc0e6c58f] [ctype: 2] [flow: 1] [userId: 4] [deviceId: 3] {"opcode":"DEVICE_GETTIME_RSP","data":{"result":0,"body":{"deviceTime":1606129555,"deviceTimezone":3600}}}\n', ); expect(ret2).to.equal( - '[fb3dd1ebc0e6c58f] [ctype: 2] [flow: 1] [userId: 4] [deviceId: 3] {"opcode":"DEVICE_GETTIME_RSP","object":{"result":0,"body":{"deviceTime":1606129555,"deviceTimezone":3600}}}\n', + '[fb3dd1ebc0e6c58f] [ctype: 2] [flow: 1] [userId: 4] [deviceId: 3] {"opcode":"DEVICE_GETTIME_RSP","data":{"result":0,"body":{"deviceTime":1606129555,"deviceTimezone":3600}}}\n', ); verify(packetMapper.toDomain(anything())).twice(); @@ -63,7 +63,7 @@ describe('ReadCommand', function () { sequence: 'fb3dd1ebc0e6c58f', payload: { opcode: 'DEVICE_GETTIME_RSP', - object: { result: +0, body: { deviceTime: 1606129555, deviceTimezone: 3600 } }, + data: { result: +0, body: { deviceTime: 1606129555, deviceTimezone: 3600 } }, }, }, { @@ -74,7 +74,7 @@ describe('ReadCommand', function () { sequence: 'fb3dd1ebc0e6c58f', payload: { opcode: 'DEVICE_GETTIME_RSP', - object: { result: +0, body: { deviceTime: 1606129555, deviceTimezone: 3600 } }, + data: { result: +0, body: { deviceTime: 1606129555, deviceTimezone: 3600 } }, }, }, ]); diff --git a/packages/cli/src/streams/packet-encode-transform.stream.test.ts b/packages/cli/src/streams/packet-encode-transform.stream.test.ts index ab3d15c4..18e98cb9 100644 --- a/packages/cli/src/streams/packet-encode-transform.stream.test.ts +++ b/packages/cli/src/streams/packet-encode-transform.stream.test.ts @@ -39,7 +39,7 @@ describe('PacketEncodeTransform', function () { sequence: PacketSequence.fromString(jsonPacket.sequence), payload: new Payload({ opcode: OPCode.fromName(jsonPacket.payload.opcode), - object: jsonPacket.payload.object, + data: jsonPacket.payload.data, }), }), ), diff --git a/packages/cli/src/streams/packet-encode-transform.stream.ts b/packages/cli/src/streams/packet-encode-transform.stream.ts index 08976960..acf0407e 100644 --- a/packages/cli/src/streams/packet-encode-transform.stream.ts +++ b/packages/cli/src/streams/packet-encode-transform.stream.ts @@ -1,7 +1,7 @@ import { Transform } from 'stream'; import { ID } from '@agnoc/toolkit'; import { OPCode, Packet, PacketSequence, Payload } from '@agnoc/transport-tcp'; -import type { PayloadObjectName, PacketProps, JSONPayload, PacketMapper } from '@agnoc/transport-tcp'; +import type { PayloadDataName, PacketProps, JSONPayload, PacketMapper } from '@agnoc/transport-tcp'; import type { TransformCallback } from 'stream'; export class PacketEncodeTransform extends Transform { @@ -16,21 +16,21 @@ export class PacketEncodeTransform extends Transform { done(); } - buildPacketFromJSON(serialized: JSONPacket): Packet { + buildPacketFromJSON(serialized: JSONPacket): Packet { const props: PacketProps = { ctype: serialized.ctype, flow: serialized.flow, deviceId: new ID(serialized.deviceId), userId: new ID(serialized.userId), sequence: PacketSequence.fromString(serialized.sequence), - payload: new Payload({ opcode: OPCode.fromName(serialized.payload.opcode), object: serialized.payload.object }), + payload: new Payload({ opcode: OPCode.fromName(serialized.payload.opcode), data: serialized.payload.data }), }; return new Packet(props); } } -export interface JSONPacket { +export interface JSONPacket { ctype: number; flow: number; deviceId: number; diff --git a/packages/cli/src/test-support.ts b/packages/cli/src/test-support.ts index 3e7b7943..98d638d2 100644 --- a/packages/cli/src/test-support.ts +++ b/packages/cli/src/test-support.ts @@ -9,7 +9,7 @@ export function givenAJSONPacket(): JSONPacket<'DEVICE_GETTIME_RSP'> { sequence: '7a479a0fbb978c12', payload: { opcode: 'DEVICE_GETTIME_RSP', - object: { + data: { result: 1, body: { deviceTime: 2, diff --git a/packages/transport-tcp/src/constants/payloads.constant.ts b/packages/transport-tcp/src/constants/payloads.constant.ts index 90518013..26d383bc 100644 --- a/packages/transport-tcp/src/constants/payloads.constant.ts +++ b/packages/transport-tcp/src/constants/payloads.constant.ts @@ -99,7 +99,7 @@ import type { IUSER_SET_DEVICE_QUIETHOURS_RSP, } from '@agnoc/schemas-tcp'; -interface PayloadObjectMap { +interface PayloadDataMap { CLIENT_HEARTBEAT_REQ: ICLIENT_HEARTBEAT_REQ; CLIENT_HEARTBEAT_RSP: ICLIENT_HEARTBEAT_RSP; CLIENT_IDLE_TIMEOUT: ICLIENT_IDLE_TIMEOUT; @@ -202,6 +202,6 @@ interface PayloadObjectMap { USER_SET_DEVICE_QUIETHOURS_RSP: IUSER_SET_DEVICE_QUIETHOURS_RSP; } -export type PayloadObject = PayloadObjectMap[PayloadObjectName]; -export type PayloadObjectName = keyof PayloadObjectMap & OPNameLiteral; -export type PayloadObjectFrom = PayloadObjectMap[Name]; +export type PayloadData = PayloadDataMap[PayloadDataName]; +export type PayloadDataName = keyof PayloadDataMap & OPNameLiteral; +export type PayloadDataFrom = PayloadDataMap[Name]; diff --git a/packages/transport-tcp/src/factories/packet.factory.test.ts b/packages/transport-tcp/src/factories/packet.factory.test.ts index 21a33ace..c541430f 100644 --- a/packages/transport-tcp/src/factories/packet.factory.test.ts +++ b/packages/transport-tcp/src/factories/packet.factory.test.ts @@ -24,7 +24,7 @@ describe('PacketFactory', function () { expect(packet.deviceId).to.equal(props.userId); expect(packet.sequence).to.be.instanceOf(PacketSequence); expect(packet.payload.opcode.name).to.equal('DEVICE_GETTIME_RSP'); - expect(packet.payload.object).to.equal(object); + expect(packet.payload.data).to.equal(object); }); it('should create a packet from another packet', function () { @@ -40,6 +40,6 @@ describe('PacketFactory', function () { expect(packet.deviceId).to.equal(sourcePacketProps.userId); expect(packet.sequence).to.be.equal(sourcePacketProps.sequence); expect(packet.payload.opcode.name).to.equal('DEVICE_GETTIME_RSP'); - expect(packet.payload.object).to.equal(object); + expect(packet.payload.data).to.equal(object); }); }); diff --git a/packages/transport-tcp/src/factories/packet.factory.ts b/packages/transport-tcp/src/factories/packet.factory.ts index 45f74d7a..9879a79a 100644 --- a/packages/transport-tcp/src/factories/packet.factory.ts +++ b/packages/transport-tcp/src/factories/packet.factory.ts @@ -2,7 +2,7 @@ import { OPCode } from '../domain-primitives/opcode.domain-primitive'; import { PacketSequence } from '../domain-primitives/packet-sequence.domain-primitive'; import { Packet } from '../value-objects/packet.value-object'; import { Payload } from '../value-objects/payload.value-object'; -import type { PayloadObjectFrom, PayloadObjectName } from '../constants/payloads.constant'; +import type { PayloadDataFrom, PayloadDataName } from '../constants/payloads.constant'; import type { Factory, ID } from '@agnoc/toolkit'; /** The props to create a packet. */ @@ -19,15 +19,15 @@ export class PacketFactory implements Factory { * To use this method you must provider a `deviceId` and `userId` in the `props` object, * or a `Packet` object in the `packet` argument to copy the packet props from. */ - create( + create( name: Name, - object: PayloadObjectFrom, + object: PayloadDataFrom, props: CreatePacketProps, ): Packet; - create(name: Name, object: PayloadObjectFrom, packet: Packet): Packet; - create( + create(name: Name, object: PayloadDataFrom, packet: Packet): Packet; + create( name: Name, - object: PayloadObjectFrom, + object: PayloadDataFrom, propsOrPacket: CreatePacketProps | Packet, ): Packet { if (propsOrPacket instanceof Packet) { @@ -38,7 +38,7 @@ export class PacketFactory implements Factory { userId: propsOrPacket.deviceId, deviceId: propsOrPacket.userId, sequence: propsOrPacket.sequence, - payload: new Payload({ opcode: OPCode.fromName(name), object }), + payload: new Payload({ opcode: OPCode.fromName(name), data: object }), }); } @@ -49,7 +49,7 @@ export class PacketFactory implements Factory { userId: propsOrPacket.deviceId, deviceId: propsOrPacket.userId, sequence: PacketSequence.generate(), - payload: new Payload({ opcode: OPCode.fromName(name), object }), + payload: new Payload({ opcode: OPCode.fromName(name), data: object }), }); } } diff --git a/packages/transport-tcp/src/index.ts b/packages/transport-tcp/src/index.ts index 835156b7..f159992b 100644 --- a/packages/transport-tcp/src/index.ts +++ b/packages/transport-tcp/src/index.ts @@ -12,7 +12,7 @@ export * from './mappers/packet.mapper'; export * from './mappers/payload.mapper'; export * from './packet.server'; export * from './packet.socket'; -export * from './services/payload-object-parser.service'; +export * from './services/payload-data-parser.service'; export * from './utils/get-custom-decoders.util'; export * from './utils/get-protobuf-root.util'; export * from './value-objects/packet.value-object'; diff --git a/packages/transport-tcp/src/mappers/packet.mapper.test.ts b/packages/transport-tcp/src/mappers/packet.mapper.test.ts index 118c741f..eca05bda 100644 --- a/packages/transport-tcp/src/mappers/packet.mapper.test.ts +++ b/packages/transport-tcp/src/mappers/packet.mapper.test.ts @@ -84,7 +84,7 @@ describe('PacketMapper', function () { sequence: PacketSequence.fromString('7a479a0fbb978c12'), payload: new Payload({ opcode: OPCode.fromName('DEVICE_GETTIME_RSP'), - object: { result: 1, body: { deviceTime: 1 } }, + data: { result: 1, body: { deviceTime: 1 } }, }), }); diff --git a/packages/transport-tcp/src/mappers/packet.mapper.ts b/packages/transport-tcp/src/mappers/packet.mapper.ts index 39853b3f..38797039 100644 --- a/packages/transport-tcp/src/mappers/packet.mapper.ts +++ b/packages/transport-tcp/src/mappers/packet.mapper.ts @@ -16,7 +16,7 @@ import { OPCode } from '../domain-primitives/opcode.domain-primitive'; import { PacketSequence } from '../domain-primitives/packet-sequence.domain-primitive'; import { Packet } from '../value-objects/packet.value-object'; import type { PayloadMapper } from './payload.mapper'; -import type { PayloadObjectName } from '../constants/payloads.constant'; +import type { PayloadDataName } from '../constants/payloads.constant'; import type { Mapper } from '@agnoc/toolkit'; /** Mapper for converting packets to and from buffers. */ @@ -24,7 +24,7 @@ export class PacketMapper implements Mapper { constructor(private readonly payloadMapper: PayloadMapper) {} /** Converts a buffer to a packet. */ - toDomain(data: Buffer): Packet { + toDomain(data: Buffer): Packet { const stream = toStream(data); const size = readWord(stream); diff --git a/packages/transport-tcp/src/mappers/payload.mapper.test.ts b/packages/transport-tcp/src/mappers/payload.mapper.test.ts index bc6a0313..91e9a63e 100644 --- a/packages/transport-tcp/src/mappers/payload.mapper.test.ts +++ b/packages/transport-tcp/src/mappers/payload.mapper.test.ts @@ -4,19 +4,19 @@ import { expect } from 'chai'; import { OPCode } from '../domain-primitives/opcode.domain-primitive'; import { Payload } from '../value-objects/payload.value-object'; import { PayloadMapper } from './payload.mapper'; -import type { Decoder, Encoder, PayloadObjectParserService } from '../services/payload-object-parser.service'; +import type { Decoder, Encoder, PayloadDataParserService } from '../services/payload-data-parser.service'; describe('PayloadMapper', function () { let decoder: Decoder; let encoder: Encoder; - let payloadObjectParserService: PayloadObjectParserService; + let payloadDataParserService: PayloadDataParserService; let payloadMapper: PayloadMapper; beforeEach(function () { decoder = fnmock(); encoder = fnmock(); - payloadObjectParserService = imock(); - payloadMapper = new PayloadMapper(instance(payloadObjectParserService)); + payloadDataParserService = imock(); + payloadMapper = new PayloadMapper(instance(payloadDataParserService)); }); describe('#toDomain()', function () { @@ -25,14 +25,14 @@ describe('PayloadMapper', function () { const opcode = OPCode.fromName('CLIENT_HEARTBEAT_REQ'); const object = { foo: 'bar' }; - when(payloadObjectParserService.getDecoder(anything())).thenReturn(instance(decoder)); + when(payloadDataParserService.getDecoder(anything())).thenReturn(instance(decoder)); when(decoder(anything())).thenReturn(object); const payload = payloadMapper.toDomain(buffer, opcode); expect(payload).to.be.instanceOf(Payload); expect(payload.opcode).to.equal(opcode); - expect(payload.object).to.equal(object); + expect(payload.data).to.equal(object); verify(decoder(buffer)).once(); }); @@ -41,7 +41,7 @@ describe('PayloadMapper', function () { const buffer = Buffer.from('test'); const opcode = OPCode.fromName('CLIENT_HEARTBEAT_REQ'); - when(payloadObjectParserService.getDecoder(anything())).thenReturn(undefined); + when(payloadDataParserService.getDecoder(anything())).thenReturn(undefined); expect(() => payloadMapper.toDomain(buffer, opcode)).to.throw( ArgumentInvalidException, @@ -54,10 +54,10 @@ describe('PayloadMapper', function () { it('should create a buffer from a payload', function () { const opcode = OPCode.fromName('CLIENT_HEARTBEAT_REQ'); const object = { foo: 'bar' }; - const payload = new Payload({ opcode, object }); + const payload = new Payload({ opcode, data: object }); const buffer = Buffer.from('test'); - when(payloadObjectParserService.getEncoder(anything())).thenReturn(instance(encoder)); + when(payloadDataParserService.getEncoder(anything())).thenReturn(instance(encoder)); when(encoder(anything())).thenReturn(buffer); const ret = payloadMapper.fromDomain(payload); @@ -70,13 +70,13 @@ describe('PayloadMapper', function () { it('should throw an error when the encoder does not exist', function () { const opcode = OPCode.fromName('CLIENT_HEARTBEAT_REQ'); const object = { foo: 'bar' }; - const payload = new Payload({ opcode, object }); + const payload = new Payload({ opcode, data: object }); - when(payloadObjectParserService.getEncoder(anything())).thenReturn(undefined); + when(payloadDataParserService.getEncoder(anything())).thenReturn(undefined); expect(() => payloadMapper.fromDomain(payload)).to.throw( ArgumentInvalidException, - `Encoder not found for opcode 'CLIENT_HEARTBEAT_REQ' while creating payload from object: {"foo":"bar"}`, + `Encoder not found for opcode 'CLIENT_HEARTBEAT_REQ' while creating payload from data: {"foo":"bar"}`, ); }); }); diff --git a/packages/transport-tcp/src/mappers/payload.mapper.ts b/packages/transport-tcp/src/mappers/payload.mapper.ts index 6f41963d..ba8a6402 100644 --- a/packages/transport-tcp/src/mappers/payload.mapper.ts +++ b/packages/transport-tcp/src/mappers/payload.mapper.ts @@ -1,15 +1,15 @@ import { ArgumentInvalidException } from '@agnoc/toolkit'; import { Payload } from '../value-objects/payload.value-object'; -import type { PayloadObjectName } from '../constants/payloads.constant'; +import type { PayloadDataName } from '../constants/payloads.constant'; import type { OPCode } from '../domain-primitives/opcode.domain-primitive'; -import type { PayloadObjectParserService } from '../services/payload-object-parser.service'; +import type { PayloadDataParserService } from '../services/payload-data-parser.service'; import type { Mapper } from '@agnoc/toolkit'; export class PayloadMapper implements Mapper { - constructor(private readonly payloadObjectParserService: PayloadObjectParserService) {} + constructor(private readonly payloadDataParserService: PayloadDataParserService) {} - toDomain(buffer: Buffer, opcode: OPCode): Payload { - const decoder = this.payloadObjectParserService.getDecoder(opcode.name as Name); + toDomain(buffer: Buffer, opcode: OPCode): Payload { + const decoder = this.payloadDataParserService.getDecoder(opcode.name as Name); if (!decoder) { throw new ArgumentInvalidException( @@ -17,25 +17,25 @@ export class PayloadMapper implements Mapper { ); } - const object = decoder(buffer); + const data = decoder(buffer); return new Payload({ opcode, - object, + data, }); } - fromDomain(payload: Payload): Buffer { - const encoder = this.payloadObjectParserService.getEncoder(payload.opcode.name as Name); + fromDomain(payload: Payload): Buffer { + const encoder = this.payloadDataParserService.getEncoder(payload.opcode.name as Name); if (!encoder) { throw new ArgumentInvalidException( - `Encoder not found for opcode '${payload.opcode.name}' while creating payload from object: ${JSON.stringify( - payload.object, + `Encoder not found for opcode '${payload.opcode.name}' while creating payload from data: ${JSON.stringify( + payload.data, )}`, ); } - return encoder(payload.object); + return encoder(payload.data); } } diff --git a/packages/transport-tcp/src/services/payload-object-parser.service.test.ts b/packages/transport-tcp/src/services/payload-data-parser.service.test.ts similarity index 82% rename from packages/transport-tcp/src/services/payload-object-parser.service.test.ts rename to packages/transport-tcp/src/services/payload-data-parser.service.test.ts index 42b7cee9..c7f821f0 100644 --- a/packages/transport-tcp/src/services/payload-object-parser.service.test.ts +++ b/packages/transport-tcp/src/services/payload-data-parser.service.test.ts @@ -1,19 +1,19 @@ import { ArgumentInvalidException } from '@agnoc/toolkit'; import { anything, fnmock, imock, instance, verify, when } from '@johanblumenberg/ts-mockito'; import { expect } from 'chai'; -import { PayloadObjectParserService } from './payload-object-parser.service'; -import type { Decoder, DecoderMap, Encoder, EncoderMap } from './payload-object-parser.service'; -import type { PayloadObject } from '../constants/payloads.constant'; +import { PayloadDataParserService } from './payload-data-parser.service'; +import type { Decoder, DecoderMap, Encoder, EncoderMap } from './payload-data-parser.service'; +import type { PayloadData } from '../constants/payloads.constant'; import type { MapInfo } from '../decoders/map.interface'; import type { Root, Type, Message, Writer } from 'protobufjs/light'; -describe('PayloadObjectParserService', function () { - let service: PayloadObjectParserService; +describe('PayloadDataParserService', function () { + let service: PayloadDataParserService; let protoRoot: Root; let protoType: Type; let protoMessage: Message; let protoWriter: Writer; - let payloadObject: PayloadObject; + let payloadData: PayloadData; let buffer: Buffer; let customDecoders: Partial; let customDecoder: Decoder; @@ -25,26 +25,26 @@ describe('PayloadObjectParserService', function () { protoType = imock(); protoMessage = imock(); protoWriter = imock(); - payloadObject = instance(imock()); + payloadData = instance(imock()); buffer = Buffer.from('example'); customDecoder = fnmock(); customDecoders = { DEVICE_MAPID_GET_GLOBAL_INFO_RSP: instance(customDecoder) }; customEncoder = fnmock(); customEncoders = { DEVICE_MAPID_GET_GLOBAL_INFO_RSP: instance(customEncoder) }; - service = new PayloadObjectParserService(instance(protoRoot), customDecoders, customEncoders); + service = new PayloadDataParserService(instance(protoRoot), customDecoders, customEncoders); }); describe('#getDecoder()', function () { it('should return a decoder function from the protobuf schema', function () { when(protoRoot.get(anything())).thenReturn(instance(protoType)); when(protoType.decode(anything())).thenReturn(instance(protoMessage)); - when(protoType.toObject(anything())).thenReturn(payloadObject); + when(protoType.toObject(anything())).thenReturn(payloadData); when(customDecoder(anything())).thenReturn({}); const fn = service.getDecoder('CLIENT_HEARTBEAT_REQ'); const ret = fn?.(buffer); - expect(ret).to.equal(payloadObject); + expect(ret).to.equal(payloadData); verify(protoRoot.get('CLIENT_HEARTBEAT_REQ')).once(); verify(protoType.decode(buffer)).once(); @@ -55,12 +55,12 @@ describe('PayloadObjectParserService', function () { when(protoRoot.get(anything())).thenReturn(null); when(protoType.decode(anything())).thenReturn(instance(protoMessage)); when(protoType.toObject(anything())).thenReturn({}); - when(customDecoder(anything())).thenReturn(payloadObject); + when(customDecoder(anything())).thenReturn(payloadData); const fn = service.getDecoder('DEVICE_MAPID_GET_GLOBAL_INFO_RSP'); const ret = fn?.(buffer); - expect(ret).to.equal(payloadObject); + expect(ret).to.equal(payloadData); verify(protoRoot.get('DEVICE_MAPID_GET_GLOBAL_INFO_RSP')).once(); verify(protoType.decode(anything())).never(); @@ -78,14 +78,14 @@ describe('PayloadObjectParserService', function () { when(customEncoder(anything())).thenReturn(Buffer.alloc(0)); const fn = service.getEncoder('CLIENT_HEARTBEAT_REQ'); - const ret = fn?.(payloadObject); + const ret = fn?.(payloadData); expect(ret).to.exist; expect(Buffer.compare(ret as Buffer, buffer)).to.equal(0); verify(protoRoot.get('CLIENT_HEARTBEAT_REQ')).once(); - verify(protoType.verify(payloadObject)).once(); - verify(protoType.create(payloadObject)).once(); + verify(protoType.verify(payloadData)).once(); + verify(protoType.create(payloadData)).once(); verify(customEncoder(anything())).never(); }); @@ -98,13 +98,13 @@ describe('PayloadObjectParserService', function () { when(customEncoder(anything())).thenReturn(Buffer.alloc(0)); const fn = service.getEncoder('CLIENT_HEARTBEAT_REQ'); - expect(() => fn?.(payloadObject)).to.throw( + expect(() => fn?.(payloadData)).to.throw( ArgumentInvalidException, `Cannot encode a payload for opcode 'CLIENT_HEARTBEAT_REQ' for object 'null': Verify error`, ); verify(protoRoot.get('CLIENT_HEARTBEAT_REQ')).once(); - verify(protoType.verify(payloadObject)).once(); + verify(protoType.verify(payloadData)).once(); verify(protoType.create(anything())).never(); verify(customEncoder(anything())).never(); }); @@ -118,7 +118,7 @@ describe('PayloadObjectParserService', function () { when(customEncoder(anything())).thenReturn(buffer); const fn = service.getEncoder('DEVICE_MAPID_GET_GLOBAL_INFO_RSP'); - const ret = fn?.(payloadObject as MapInfo); + const ret = fn?.(payloadData as MapInfo); expect(ret).to.exist; expect(Buffer.compare(ret as Buffer, buffer)).to.equal(0); @@ -126,7 +126,7 @@ describe('PayloadObjectParserService', function () { verify(protoRoot.get('DEVICE_MAPID_GET_GLOBAL_INFO_RSP')).once(); verify(protoType.verify(anything())).never(); verify(protoType.create(anything())).never(); - verify(customEncoder(payloadObject)).once(); + verify(customEncoder(payloadData)).once(); }); }); }); diff --git a/packages/transport-tcp/src/services/payload-object-parser.service.ts b/packages/transport-tcp/src/services/payload-data-parser.service.ts similarity index 63% rename from packages/transport-tcp/src/services/payload-object-parser.service.ts rename to packages/transport-tcp/src/services/payload-data-parser.service.ts index 06014243..baab9788 100644 --- a/packages/transport-tcp/src/services/payload-object-parser.service.ts +++ b/packages/transport-tcp/src/services/payload-data-parser.service.ts @@ -1,15 +1,15 @@ import { ArgumentInvalidException } from '@agnoc/toolkit'; -import type { PayloadObjectFrom, PayloadObjectName } from '../constants/payloads.constant'; +import type { PayloadDataFrom, PayloadDataName } from '../constants/payloads.constant'; import type { Root, Type } from 'protobufjs/light'; -export type Decoder = (buffer: Buffer) => PayloadObjectFrom; -export type Encoder = (object: PayloadObjectFrom) => Buffer; +export type Decoder = (buffer: Buffer) => PayloadDataFrom; +export type Encoder = (object: PayloadDataFrom) => Buffer; -export type EncoderMap = Record>; -export type DecoderMap = Record>; +export type EncoderMap = Record>; +export type DecoderMap = Record>; /** Service to encde and decode payload objects. */ -export class PayloadObjectParserService { +export class PayloadDataParserService { constructor( private readonly protoRoot: Root, private readonly decoders?: Partial, @@ -17,7 +17,7 @@ export class PayloadObjectParserService { ) {} /** Returns a decoder for a payload object. */ - getDecoder(name: Name): Decoder | undefined { + getDecoder(name: Name): Decoder | undefined { const schema = this.protoRoot.get(name) as Type | null; if (schema) { @@ -28,7 +28,7 @@ export class PayloadObjectParserService { } /** Returns an encoder for a payload object. */ - getEncoder(name: Name): Encoder | undefined { + getEncoder(name: Name): Encoder | undefined { const schema = this.protoRoot.get(name) as Type | null; if (schema) { @@ -38,7 +38,7 @@ export class PayloadObjectParserService { return this.encoders?.[name] as Encoder | undefined; } - private buildSchemaDecoder(name: PayloadObjectName, schema: Type): Decoder { + private buildSchemaDecoder(name: PayloadDataName, schema: Type): Decoder { const decoder = (buffer: Buffer) => { const message = schema.decode(buffer); @@ -50,7 +50,7 @@ export class PayloadObjectParserService { return decoder as Decoder; } - private buildSchemaEncoder(name: PayloadObjectName, schema: Type): Encoder { + private buildSchemaEncoder(name: PayloadDataName, schema: Type): Encoder { const encoder = (object: Buffer) => { const err = schema.verify(object); diff --git a/packages/transport-tcp/src/test-support.ts b/packages/transport-tcp/src/test-support.ts index 77f42c78..208dd7c0 100644 --- a/packages/transport-tcp/src/test-support.ts +++ b/packages/transport-tcp/src/test-support.ts @@ -8,7 +8,7 @@ import type { PayloadProps } from './value-objects/payload.value-object'; export function givenSomePayloadProps(): PayloadProps<'DEVICE_GETTIME_RSP'> { return { opcode: OPCode.fromName('DEVICE_GETTIME_RSP'), - object: { result: 0, body: { deviceTime: 1606129555, deviceTimezone: 3600 } }, + data: { result: 0, body: { deviceTime: 1606129555, deviceTimezone: 3600 } }, }; } diff --git a/packages/transport-tcp/src/utils/get-custom-decoders.util.ts b/packages/transport-tcp/src/utils/get-custom-decoders.util.ts index d1d2837f..523f92a6 100644 --- a/packages/transport-tcp/src/utils/get-custom-decoders.util.ts +++ b/packages/transport-tcp/src/utils/get-custom-decoders.util.ts @@ -2,7 +2,7 @@ import { decodeArea } from '../decoders/area.decoder'; import { decodeChargePosition } from '../decoders/charge-position.decoder'; import { decodeMap } from '../decoders/map.decoder'; import { decodeRobotPosition } from '../decoders/robot-position.decoder'; -import type { DecoderMap } from '../services/payload-object-parser.service'; +import type { DecoderMap } from '../services/payload-data-parser.service'; const decoders: Partial = { DEVICE_MAPID_GET_GLOBAL_INFO_RSP: decodeMap, diff --git a/packages/transport-tcp/src/value-objects/packet.value-object.test.ts b/packages/transport-tcp/src/value-objects/packet.value-object.test.ts index cd232b82..e8626702 100644 --- a/packages/transport-tcp/src/value-objects/packet.value-object.test.ts +++ b/packages/transport-tcp/src/value-objects/packet.value-object.test.ts @@ -126,12 +126,12 @@ describe('Packet', function () { sequence: new PacketSequence(5n), payload: new Payload({ opcode: OPCode.fromName('DEVICE_GETTIME_RSP'), - object: { result: 0, body: { deviceTime: 1606129555, deviceTimezone: 3600 } }, + data: { result: 0, body: { deviceTime: 1606129555, deviceTimezone: 3600 } }, }), }); expect(packet.toString()).to.be.equal( - '[5] [ctype: 2] [flow: 1] [userId: 4] [deviceId: 3] {"opcode":"DEVICE_GETTIME_RSP","object":{"result":0,"body":{"deviceTime":1606129555,"deviceTimezone":3600}}}', + '[5] [ctype: 2] [flow: 1] [userId: 4] [deviceId: 3] {"opcode":"DEVICE_GETTIME_RSP","data":{"result":0,"body":{"deviceTime":1606129555,"deviceTimezone":3600}}}', ); }); }); diff --git a/packages/transport-tcp/src/value-objects/packet.value-object.ts b/packages/transport-tcp/src/value-objects/packet.value-object.ts index af0eb03f..37f3eb03 100644 --- a/packages/transport-tcp/src/value-objects/packet.value-object.ts +++ b/packages/transport-tcp/src/value-objects/packet.value-object.ts @@ -1,10 +1,10 @@ import { ValueObject, ID } from '@agnoc/toolkit'; import { PacketSequence } from '../domain-primitives/packet-sequence.domain-primitive'; import { Payload } from './payload.value-object'; -import type { PayloadObjectName } from '../constants/payloads.constant'; +import type { PayloadDataName } from '../constants/payloads.constant'; /** Describes the properties of a packet. */ -export interface PacketProps { +export interface PacketProps { /** The packet type. */ ctype: number; /** The packet flow. */ @@ -20,7 +20,7 @@ export interface PacketProps { } /** Describes a packet. */ -export class Packet extends ValueObject> { +export class Packet extends ValueObject> { /** Returns the packet type. */ get ctype(): number { return this.props.ctype; diff --git a/packages/transport-tcp/src/value-objects/payload.value-object.test.ts b/packages/transport-tcp/src/value-objects/payload.value-object.test.ts index 1c72c48e..bfca9899 100644 --- a/packages/transport-tcp/src/value-objects/payload.value-object.test.ts +++ b/packages/transport-tcp/src/value-objects/payload.value-object.test.ts @@ -11,7 +11,7 @@ describe('Payload', function () { expect(payload).to.be.instanceOf(ValueObject); expect(payload.opcode).to.be.equal(payloadProps.opcode); - expect(payload.object).to.be.equal(payloadProps.object); + expect(payload.data).to.be.equal(payloadProps.data); }); it("should throw an error when 'opcode' is not provided", function () { @@ -24,9 +24,9 @@ describe('Payload', function () { it("should throw an error when 'object' is not provided", function () { // @ts-expect-error - missing property - expect(() => new Payload({ ...givenSomePayloadProps(), object: undefined })).to.throw( + expect(() => new Payload({ ...givenSomePayloadProps(), data: undefined })).to.throw( ArgumentNotProvidedException, - `Property 'object' for Payload not provided`, + `Property 'data' for Payload not provided`, ); }); @@ -38,11 +38,11 @@ describe('Payload', function () { ); }); - it('should throw an error when "object" is not an object', function () { + it('should throw an error when "data" is not an object', function () { // @ts-expect-error - invalid property - expect(() => new Payload({ ...givenSomePayloadProps(), object: 'foo' })).to.throw( + expect(() => new Payload({ ...givenSomePayloadProps(), data: 'foo' })).to.throw( ArgumentInvalidException, - `Value 'foo' for property 'object' of Payload is not an instance of Object`, + `Value 'foo' for property 'data' of Payload is not an instance of Object`, ); }); @@ -50,26 +50,26 @@ describe('Payload', function () { it('should return a string representation of the Payload', function () { const payload = new Payload<'DEVICE_MAPID_PUSH_MAP_INFO'>({ opcode: OPCode.fromName('DEVICE_MAPID_PUSH_MAP_INFO'), - object: { mask: 0, mapGrid: Buffer.from('example') }, + data: { mask: 0, mapGrid: Buffer.from('example') }, }); expect(payload.toString()).to.be.equal( - '{"opcode":"DEVICE_MAPID_PUSH_MAP_INFO","object":{"mask":0,"mapGrid":"[Buffer]"}}', + '{"opcode":"DEVICE_MAPID_PUSH_MAP_INFO","data":{"mask":0,"mapGrid":"[Buffer]"}}', ); }); }); describe('#toJSON()', function () { it('should return a JSON representation of the Payload', function () { - const object = { mask: 0, mapGrid: Buffer.from('example') }; + const data = { mask: 0, mapGrid: Buffer.from('example') }; const payload = new Payload<'DEVICE_MAPID_PUSH_MAP_INFO'>({ opcode: OPCode.fromName('DEVICE_MAPID_PUSH_MAP_INFO'), - object, + data, }); expect(payload.toJSON()).to.be.deep.equal({ opcode: 'DEVICE_MAPID_PUSH_MAP_INFO', - object, + data, }); }); }); diff --git a/packages/transport-tcp/src/value-objects/payload.value-object.ts b/packages/transport-tcp/src/value-objects/payload.value-object.ts index 413ef350..5ee1ed58 100644 --- a/packages/transport-tcp/src/value-objects/payload.value-object.ts +++ b/packages/transport-tcp/src/value-objects/payload.value-object.ts @@ -1,33 +1,33 @@ import { isObject, ValueObject } from '@agnoc/toolkit'; import { OPCode } from '../domain-primitives/opcode.domain-primitive'; -import type { PayloadObjectFrom, PayloadObjectName } from '../constants/payloads.constant'; +import type { PayloadDataFrom, PayloadDataName } from '../constants/payloads.constant'; /** Describes the properties of a payload. */ -export interface PayloadProps { +export interface PayloadProps { /** The opcode of the payload. */ opcode: OPCode; /** The object representation of the payload. */ - object: PayloadObjectFrom; + data: PayloadDataFrom; } /** Describes the JSON representation of a payload. */ -export interface JSONPayload { +export interface JSONPayload { /** The name of the opcode for the payload. */ opcode: Name; /** The object representation of the payload. */ - object: PayloadObjectFrom; + data: PayloadDataFrom; } /** Describes a payload. */ -export class Payload extends ValueObject> { +export class Payload extends ValueObject> { /** Returns the opcode of the payload. */ get opcode(): OPCode { return this.props.opcode; } /** Returns the object representation of the payload. */ - get object(): PayloadObjectFrom { - return this.props.object; + get data(): PayloadDataFrom { + return this.props.data; } /** Returns the string representation of the payload with some properties filtered. */ @@ -37,18 +37,18 @@ export class Payload extends /** Returns the JSON representation of the payload. */ override toJSON(): JSONPayload { - return { opcode: this.props.opcode.name as Name, object: this.props.object }; + return { opcode: this.props.opcode.name as Name, data: this.props.data }; } protected validate(props: PayloadProps): void { - const keys: (keyof PayloadProps)[] = ['opcode', 'object']; + const keys: (keyof PayloadProps)[] = ['opcode', 'data']; keys.forEach((prop) => { this.validateDefinedProp(props, prop); }); this.validateInstanceProp(props, 'opcode', OPCode); - this.validateInstanceProp(props, 'object', Object); + this.validateInstanceProp(props, 'data', Object); } }