@grupodiariodaregiao/bunstone 0.4.1 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -117583,6 +117583,33 @@ if (document.readyState === 'loading') {
117583
117583
  return;
117584
117584
  }
117585
117585
  const injectables = Reflect.getMetadata("dip:injectables", module);
117586
+ function matchRoutingKey(pattern, routingKey) {
117587
+ if (pattern === routingKey)
117588
+ return true;
117589
+ if (!pattern.includes("*") && !pattern.includes("#"))
117590
+ return false;
117591
+ const pp = pattern.split(".");
117592
+ const kp = routingKey.split(".");
117593
+ function go2(pi3, ki3) {
117594
+ if (pi3 === pp.length && ki3 === kp.length)
117595
+ return true;
117596
+ if (pi3 === pp.length)
117597
+ return false;
117598
+ if (pp[pi3] === "#") {
117599
+ for (let j4 = ki3;j4 <= kp.length; j4++) {
117600
+ if (go2(pi3 + 1, j4))
117601
+ return true;
117602
+ }
117603
+ return false;
117604
+ }
117605
+ if (ki3 === kp.length)
117606
+ return false;
117607
+ if (pp[pi3] === "*" || pp[pi3] === kp[ki3])
117608
+ return go2(pi3 + 1, ki3 + 1);
117609
+ return false;
117610
+ }
117611
+ return go2(0, 0);
117612
+ }
117586
117613
  (async () => {
117587
117614
  try {
117588
117615
  await RabbitMQConnection.initialise();
@@ -117652,7 +117679,10 @@ if (document.readyState === 'loading') {
117652
117679
  }
117653
117680
  for (const [queue2, handlers] of queueMap.entries()) {
117654
117681
  const noAck = handlers.every((h3) => h3.noAck);
117655
- const handlerList = handlers.map((h3) => `${h3.providerName}.${h3.descriptor.methodName}()`).join(", ");
117682
+ const handlerList = handlers.map((h3) => {
117683
+ const rk = h3.descriptor.options.routingKey;
117684
+ return `${h3.providerName}.${h3.descriptor.methodName}()${rk ? ` [${rk}]` : ""}`;
117685
+ }).join(", ");
117656
117686
  AppStartup.logger.log(`Registering RabbitMQ consumer for queue: "${queue2}" \u2192 [${handlerList}]`);
117657
117687
  try {
117658
117688
  const channel = await RabbitMQConnection.getConsumerChannel(queue2);
@@ -117686,6 +117716,10 @@ if (document.readyState === 'loading') {
117686
117716
  noAck: handlerNoAck,
117687
117717
  providerName
117688
117718
  } of handlers) {
117719
+ const handlerRoutingKey = descriptor.options.routingKey;
117720
+ if (handlerRoutingKey && !matchRoutingKey(handlerRoutingKey, raw.fields.routingKey)) {
117721
+ continue;
117722
+ }
117689
117723
  try {
117690
117724
  await instance[descriptor.methodName](msg);
117691
117725
  } catch (err) {
@@ -117695,6 +117729,9 @@ if (document.readyState === 'loading') {
117695
117729
  }
117696
117730
  }
117697
117731
  }
117732
+ if (!noAck && !settled) {
117733
+ settle(() => channel.ack(raw));
117734
+ }
117698
117735
  }, { noAck });
117699
117736
  } catch (err) {
117700
117737
  AppStartup.logger.error(`Failed to register consumer for queue "${queue2}": ${err.message}`);
@@ -879,6 +879,34 @@ if (document.readyState === 'loading') {
879
879
  module,
880
880
  );
881
881
 
882
+ /**
883
+ * Matches a RabbitMQ topic routing key pattern against a concrete routing key.
884
+ * Supports `*` (exactly one word) and `#` (zero or more words).
885
+ */
886
+ function matchRoutingKey(pattern: string, routingKey: string): boolean {
887
+ if (pattern === routingKey) return true;
888
+ if (!pattern.includes("*") && !pattern.includes("#")) return false;
889
+
890
+ const pp = pattern.split(".");
891
+ const kp = routingKey.split(".");
892
+
893
+ function go(pi: number, ki: number): boolean {
894
+ if (pi === pp.length && ki === kp.length) return true;
895
+ if (pi === pp.length) return false;
896
+ if (pp[pi] === "#") {
897
+ for (let j = ki; j <= kp.length; j++) {
898
+ if (go(pi + 1, j)) return true;
899
+ }
900
+ return false;
901
+ }
902
+ if (ki === kp.length) return false;
903
+ if (pp[pi] === "*" || pp[pi] === kp[ki]) return go(pi + 1, ki + 1);
904
+ return false;
905
+ }
906
+
907
+ return go(0, 0);
908
+ }
909
+
882
910
  type QueueHandler = {
883
911
  instance: any;
884
912
  descriptor: RabbitMQMethodDescriptor;
@@ -995,7 +1023,10 @@ if (document.readyState === 'loading') {
995
1023
  const noAck = handlers.every((h) => h.noAck);
996
1024
 
997
1025
  const handlerList = handlers
998
- .map((h) => `${h.providerName}.${h.descriptor.methodName}()`)
1026
+ .map((h) => {
1027
+ const rk = h.descriptor.options.routingKey;
1028
+ return `${h.providerName}.${h.descriptor.methodName}()${rk ? ` [${rk}]` : ""}`;
1029
+ })
999
1030
  .join(", ");
1000
1031
 
1001
1032
  AppStartup.logger.log(
@@ -1043,6 +1074,17 @@ if (document.readyState === 'loading') {
1043
1074
  noAck: handlerNoAck,
1044
1075
  providerName,
1045
1076
  } of handlers) {
1077
+ // ── Routing-key filter ─────────────────────────────────────────────
1078
+ // When { queue, routingKey } is set without exchange, only dispatch
1079
+ // if the message's routing key matches the declared pattern.
1080
+ const handlerRoutingKey = descriptor.options.routingKey;
1081
+ if (
1082
+ handlerRoutingKey &&
1083
+ !matchRoutingKey(handlerRoutingKey, raw.fields.routingKey)
1084
+ ) {
1085
+ continue;
1086
+ }
1087
+
1046
1088
  try {
1047
1089
  await instance[descriptor.methodName](msg);
1048
1090
  } catch (err: any) {
@@ -1054,6 +1096,13 @@ if (document.readyState === 'loading') {
1054
1096
  }
1055
1097
  }
1056
1098
  }
1099
+
1100
+ // ── Auto-ack if no handler consumed the message ────────────────
1101
+ // All handlers were filtered out by routingKey – ack silently
1102
+ // to prevent the message from piling up as unacked.
1103
+ if (!noAck && !settled) {
1104
+ settle(() => channel.ack(raw));
1105
+ }
1057
1106
  },
1058
1107
  { noAck },
1059
1108
  );
package/package.json CHANGED
@@ -13,7 +13,7 @@
13
13
  "types": "./dist/*.d.ts"
14
14
  }
15
15
  },
16
- "version": "0.4.1",
16
+ "version": "0.4.2",
17
17
  "homepage": "https://bunstone.diario.one/",
18
18
  "repository": {
19
19
  "url": "https://github.com/diariodaregiao/bunstone.git",