From 62418176ae87e1ce6b02da787ef1ef09c69c8cbe Mon Sep 17 00:00:00 2001 From: Pavel Fediukovich Date: Thu, 20 Apr 2023 15:23:40 +0300 Subject: [PATCH] fix: expose enabled use attributes feature for server --- lib/gc-pubsub.client.spec.ts | 2 ++ lib/gc-pubsub.client.ts | 2 ++ lib/gc-pubsub.server.ts | 9 ++++++++- tests/src/gc-pubsub.controller.ts | 9 ++++++--- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/gc-pubsub.client.spec.ts b/lib/gc-pubsub.client.spec.ts index 5c9aefe..0ec1df0 100644 --- a/lib/gc-pubsub.client.spec.ts +++ b/lib/gc-pubsub.client.spec.ts @@ -209,6 +209,7 @@ describe('GCPubSubClient', () => { expect(message.json).to.be.eql(msg.data); expect(message.attributes.pattern).to.be.eql(pattern); expect(message.attributes.id).to.be.not.empty; + expect(message.attributes.useAttributes).to.be.eql('true'); }); }); }); @@ -355,6 +356,7 @@ describe('GCPubSubClient', () => { const message = topicMock.publishMessage.getCall(0).args[0]; expect(message.json).to.be.eql(msg.data); expect(message.attributes.pattern).to.be.eql(msg.pattern); + expect(message.attributes.useAttributes).to.be.eql('true'); }); it('should throw error', async () => { diff --git a/lib/gc-pubsub.client.ts b/lib/gc-pubsub.client.ts index 7df56a7..115c33a 100644 --- a/lib/gc-pubsub.client.ts +++ b/lib/gc-pubsub.client.ts @@ -180,6 +180,7 @@ export class GCPubSubClient extends ClientProxy { json: serializedPacket.data, attributes: { pattern: serializedPacket.pattern, + useAttributes: 'true', }, }); } else { @@ -206,6 +207,7 @@ export class GCPubSubClient extends ClientProxy { replyTo: this.replyTopicName, pattern: serializedPacket.pattern, id: serializedPacket.id, + useAttributes: 'true', }, }) .catch((err) => callback({ err })); diff --git a/lib/gc-pubsub.server.ts b/lib/gc-pubsub.server.ts index 2334f9b..b91b246 100644 --- a/lib/gc-pubsub.server.ts +++ b/lib/gc-pubsub.server.ts @@ -151,7 +151,14 @@ export class GCPubSubServer extends Server implements CustomTransportStrategy { : JSON.stringify(packet.pattern); const context = new GCPubSubContext([message, pattern]); - const correlationId = packet.id || attributes.id; + + let correlationId: string; + + if (attributes.useAttibutes === 'true') { + correlationId = attributes.id; + } else { + correlationId = packet.id; + } if (isUndefined(correlationId)) { return this.handleEvent(pattern, packet, context); diff --git a/tests/src/gc-pubsub.controller.ts b/tests/src/gc-pubsub.controller.ts index a493427..6d0d5be 100644 --- a/tests/src/gc-pubsub.controller.ts +++ b/tests/src/gc-pubsub.controller.ts @@ -88,11 +88,14 @@ export class GCPubSubController implements OnApplicationShutdown { @Post('notify') async sendNotification(): Promise { - return this.client.emit('notification', true); + return this.client.emit<{ notification: boolean; id: string }>( + 'notification', + { notification: true, id: 'id' }, + ); } @EventPattern('notification') - eventHandler(data: boolean) { - GCPubSubController.IS_NOTIFIED = data; + eventHandler(data: { notification: boolean; id: string }) { + GCPubSubController.IS_NOTIFIED = data.notification; } }