Niurmiguel / nestjs-service-bus

NestJs custom transport for Azure Service Bus.
MIT License
21 stars 8 forks source link

Prevent listening to non service bus events. #216

Open iam-amanxz opened 3 months ago

iam-amanxz commented 3 months ago

Hi! πŸ‘‹

Firstly, thanks for your work on this project! πŸ™‚

Today I used patch-package to patch @niur/nestjs-service-bus@1.1.0 for the project I'm working on.

I encountered an issue where the service bus listened to my RPC events while running the Nestjs server. Due to this reason, the server didn't start since the properties you are destructuring don't exist inside RPC events. This would be the case for any other event that is not a service bus event. In that case, wouldn't it be better to check for the particular event before doing any sort of logic? For now, I have done a patch to my project that returns if the event is RPC. But ideally, the subscribe function should listen to only the service bus events.

Here is the diff that solved my problem:

diff --git a/node_modules/@niur/nestjs-service-bus/lib/server/azure-service-bus.server.ts b/node_modules/@niur/nestjs-service-bus/lib/server/azure-service-bus.server.ts
index 42709b5..fb60eee 100644
--- a/node_modules/@niur/nestjs-service-bus/lib/server/azure-service-bus.server.ts
+++ b/node_modules/@niur/nestjs-service-bus/lib/server/azure-service-bus.server.ts
@@ -46,6 +46,15 @@ export class AzureServiceBusServer

   async bindEvents(): Promise<void> {
     const subscribe = async (pattern: string) => {
+      const data = JSON.parse(pattern);
+
+      if ('rpc' in data) {
+        /**
+         * prevent subscribing to rpc patterns
+         */
+        return;
+      }
+
       const {
         metaOptions: {
           topic,
@@ -55,7 +64,7 @@ export class AzureServiceBusServer
           receiveMode,
           options,
         },
-      }: SbSubscriberMetadata = JSON.parse(pattern);
+      }: SbSubscriberMetadata = data;

       const receiver = this.sbClient.createReceiver(topic, name, {
         receiveMode,

This issue body was partially generated by patch-package.

Niurmiguel commented 3 months ago

Hi,

Thanks for the report. We'll schedule the issue and apply the fix as soon as possible.

Thanks for your contribution!