@ngn-net/nestjs-telescope 0.3.7 → 0.3.8

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.
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@ngn-net/nestjs-telescope",
3
- "version": "0.3.7",
4
- "builtAt": "2026-06-13T11:07:43.039Z"
3
+ "version": "0.3.8",
4
+ "builtAt": "2026-06-13T11:10:47.835Z"
5
5
  }
@@ -6,6 +6,7 @@ export declare class RabbitMQWatcher implements OnModuleInit {
6
6
  constructor(telescope: TelescopeService);
7
7
  onModuleInit(): void;
8
8
  private patchAmqpConnection;
9
- private patchAmqplibChannel;
9
+ private patchAmqplibConnect;
10
+ private instrumentChannel;
10
11
  private sanitizeMessage;
11
12
  }
@@ -22,7 +22,7 @@ let RabbitMQWatcher = RabbitMQWatcher_1 = class RabbitMQWatcher {
22
22
  }
23
23
  onModuleInit() {
24
24
  this.patchAmqpConnection();
25
- this.patchAmqplibChannel();
25
+ this.patchAmqplibConnect();
26
26
  }
27
27
  patchAmqpConnection() {
28
28
  try {
@@ -53,61 +53,67 @@ let RabbitMQWatcher = RabbitMQWatcher_1 = class RabbitMQWatcher {
53
53
  }
54
54
  catch { }
55
55
  }
56
- patchAmqplibChannel() {
56
+ patchAmqplibConnect() {
57
57
  try {
58
58
  const amqplib = require('amqplib');
59
- if (!amqplib)
60
- return;
61
- const Channel = amqplib.Channel;
62
- if (!Channel || !Channel.prototype)
63
- return;
64
- const originalConsume = Channel.prototype.consume;
65
- if (!originalConsume)
59
+ if (!amqplib || typeof amqplib.connect !== 'function')
66
60
  return;
61
+ const originalConnect = amqplib.connect;
67
62
  const self = this;
68
- const handlerMap = new Map();
69
- Channel.prototype.consume = function (queue, onMessage, options) {
70
- const wrappedOnMessage = (msg) => {
71
- if (!msg || !msg.content)
72
- return onMessage(msg);
73
- const startTime = Date.now();
74
- let parsed;
75
- try {
76
- parsed = JSON.parse(msg.content.toString());
77
- }
78
- catch {
79
- parsed = msg.content.toString();
80
- }
81
- self.telescope.record({
82
- type: entry_type_enum_1.EntryType.RABBITMQ,
83
- content: {
84
- direction: 'incoming',
85
- queue,
86
- exchange: msg.fields?.exchange || '',
87
- routingKey: msg.fields?.routingKey || queue,
88
- message: self.sanitizeMessage(parsed),
89
- deliveryTag: msg.fields?.deliveryTag,
90
- redelivered: msg.fields?.redelivered,
91
- consumerTag: msg.fields?.consumerTag,
92
- timestamp: new Date().toISOString(),
93
- },
94
- }).catch(() => { });
95
- return onMessage(msg);
96
- };
97
- handlerMap.set(queue, wrappedOnMessage);
98
- return originalConsume.call(this, queue, wrappedOnMessage, options);
63
+ amqplib.connect = async function (...args) {
64
+ const conn = await originalConnect.apply(this, args);
65
+ if (conn && typeof conn.createChannel === 'function') {
66
+ const originalCreateChannel = conn.createChannel.bind(conn);
67
+ conn.createChannel = async function (...channelArgs) {
68
+ const channel = await originalCreateChannel(...channelArgs);
69
+ if (channel && typeof channel.consume === 'function') {
70
+ self.instrumentChannel(channel);
71
+ }
72
+ return channel;
73
+ };
74
+ }
75
+ return conn;
99
76
  };
100
- // Also patch cancel to clean up
101
- const originalCancel = Channel.prototype.cancel;
102
- if (originalCancel) {
103
- Channel.prototype.cancel = function (consumerTag) {
104
- handlerMap.delete(consumerTag);
105
- return originalCancel.call(this, consumerTag);
106
- };
107
- }
108
77
  }
109
78
  catch { }
110
79
  }
80
+ instrumentChannel(channel) {
81
+ const self = this;
82
+ if (channel.__telescopePatched)
83
+ return;
84
+ channel.__telescopePatched = true;
85
+ const originalConsume = channel.consume;
86
+ if (!originalConsume)
87
+ return;
88
+ channel.consume = function (queue, onMessage, options) {
89
+ const wrappedOnMessage = (msg) => {
90
+ if (!msg || !msg.content)
91
+ return onMessage(msg);
92
+ const startTime = Date.now();
93
+ let parsed;
94
+ try {
95
+ parsed = JSON.parse(msg.content.toString());
96
+ }
97
+ catch {
98
+ parsed = msg.content.toString();
99
+ }
100
+ self.telescope.record({
101
+ type: entry_type_enum_1.EntryType.RABBITMQ,
102
+ content: {
103
+ direction: 'incoming',
104
+ queue,
105
+ exchange: msg.fields?.exchange || '',
106
+ routingKey: msg.fields?.routingKey || queue,
107
+ message: self.sanitizeMessage(parsed),
108
+ deliveryTag: msg.fields?.deliveryTag,
109
+ timestamp: new Date().toISOString(),
110
+ },
111
+ }).catch(() => { });
112
+ return onMessage(msg);
113
+ };
114
+ return originalConsume.call(this, queue, wrappedOnMessage, options);
115
+ };
116
+ }
111
117
  sanitizeMessage(msg) {
112
118
  if (!msg)
113
119
  return null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ngn-net/nestjs-telescope",
3
- "version": "0.3.7",
3
+ "version": "0.3.8",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },