@dudousxd/nestjs-diagnostics-redis 0.2.5 → 0.3.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,26 @@
1
1
  # @dudousxd/nestjs-diagnostics-redis
2
2
 
3
+ ## 0.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#81](https://github.com/DavideCarvalho/nestjs-diagnostics/pull/81) [`ebdacd0`](https://github.com/DavideCarvalho/nestjs-diagnostics/commit/ebdacd09676892eca5bf2d359b6edd8079600015) Thanks [@DavideCarvalho](https://github.com/DavideCarvalho)! - Listeners with delivery transports and `role`.
8
+
9
+ - Every envelope from `emit` and `trace` carries a unique `id` (a UUID). Relays keep it, so the same event has the same `id` in every process.
10
+ - `@OnDiagnostic(lib, event?)` accepts a `RegExp` for `event`, and checks the handler's parameter against the `ChannelRegistry` payload for registered channels. Each listener has a stable id, `<ClassName>#<method>`; two listener classes with the same name fail the bootstrap.
11
+ - `DiagnosticsModule.forRoot({ default, transports, role, nodeId })` and `forRootAsync(...)` select how listeners run across processes, with the `transports` factory:
12
+ - `transports.redis({ pub, sub })`: broadcast over Redis pub/sub, best-effort.
13
+ - `transports.queue({ connection })`: one BullMQ job per `(listener, event)`, retried when the handler throws (optional peer `bullmq`).
14
+ - `transports.durable(options)`: one durable run per `(listener, event)` through `@dudousxd/nestjs-durable-diagnostics/delivery`, imported only when selected; bootstrap fails with a clear message when it is missing or durable is not configured.
15
+ With `queue` and `durable`, delivery happens once, from the emitting process.
16
+ - `role: 'all' | 'api' | 'worker'`: `'api'` emits and hands work to the transport but never consumes or runs listeners. `'api'`/`'worker'` without a `default` transport fail the bootstrap.
17
+ - The Redis relay now lives in `@dudousxd/nestjs-diagnostics` (`createDiagnosticsRedisRelay`); `@dudousxd/nestjs-diagnostics-redis` re-exports it. It takes `receive: false` for a publish-only relay (then `sub` is optional), and marks re-emitted copies (`isRelayed`).
18
+
19
+ ### Patch Changes
20
+
21
+ - Updated dependencies [[`ebdacd0`](https://github.com/DavideCarvalho/nestjs-diagnostics/commit/ebdacd09676892eca5bf2d359b6edd8079600015)]:
22
+ - @dudousxd/nestjs-diagnostics@0.9.0
23
+
3
24
  ## 0.2.5
4
25
 
5
26
  ### Patch Changes
package/dist/relay.d.ts CHANGED
@@ -1,51 +1,3 @@
1
- /** The minimal Redis pub/sub surface the relay uses. An ioredis instance satisfies it structurally. */
2
- export interface RedisLike {
3
- publish(channel: string, message: string): unknown;
4
- /** When present, a tick's forwarded events go out as one pipeline instead of one round trip each. */
5
- pipeline?(): RedisPipelineLike;
6
- subscribe(channel: string, callback?: (err: Error | null, count: number) => void): unknown;
7
- on(event: 'message', listener: (channel: string, message: string) => void): unknown;
8
- removeListener(event: 'message', listener: (channel: string, message: string) => void): unknown;
9
- unsubscribe(channel: string): unknown;
10
- }
11
- /** The slice of an ioredis pipeline the relay uses to batch publishes. */
12
- export interface RedisPipelineLike {
13
- publish(channel: string, message: string): unknown;
14
- exec(): unknown;
15
- }
16
- export interface ChannelRef {
17
- lib: string;
18
- event: string;
19
- }
20
- export interface DiagnosticsRedisRelayOptions {
21
- /** Publisher connection. */
22
- pub: RedisLike;
23
- /** Subscriber connection (separate from `pub`). For ioredis: `const sub = pub.duplicate()`. */
24
- sub: RedisLike;
25
- /** Forward every event of these libs (current + future channels). */
26
- libs?: string[];
27
- /** Forward these exact channels, in addition to `libs`. */
28
- channels?: ChannelRef[];
29
- /** Forward EVERY aviary channel (current + future). Overrides `libs`/`channels`. Default false. */
30
- all?: boolean;
31
- /** Redis channel to relay on. Default 'aviary:diagnostics:relay'. */
32
- redisChannel?: string;
33
- /** Unique id for THIS process, for echo suppression. Default a random id. */
34
- nodeId?: string;
35
- }
36
- /**
37
- * Relay diagnostics events across processes over Redis pub/sub. Forwards selected local
38
- * `aviary:<lib>:<event>` channels to Redis and re-emits Redis-received events onto the local bus, so
39
- * `@OnDiagnostic` handlers / `getChannel(...).subscribe(...)` fire cross-process. Loop-safe via nodeId
40
- * echo suppression and a re-emit guard. Never throws into `emit()` or the Redis handler. Does NOT
41
- * close the `pub`/`sub` connections — the caller owns them.
42
- *
43
- * Forwarding happens off the emitter's call stack: selected events are queued and, once per event
44
- * loop turn, serialized and published in emit order (one Redis message per event, sent as a single
45
- * pipeline when `pub` supports it).
46
- *
47
- * @returns a teardown that publishes any queued events, then removes all local subscriptions and
48
- * the Redis message handler.
49
- */
50
- export declare function createDiagnosticsRedisRelay(options: DiagnosticsRedisRelayOptions): () => void;
1
+ export { createDiagnosticsRedisRelay } from '@dudousxd/nestjs-diagnostics';
2
+ export type { ChannelRef, DiagnosticsRedisRelayOptions, RedisLike, RedisPipelineLike, } from '@dudousxd/nestjs-diagnostics';
51
3
  //# sourceMappingURL=relay.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"relay.d.ts","sourceRoot":"","sources":["../src/relay.ts"],"names":[],"mappings":"AAUA,uGAAuG;AACvG,MAAM,WAAW,SAAS;IACxB,OAAO,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC;IACnD,qGAAqG;IACrG,QAAQ,CAAC,IAAI,iBAAiB,CAAC;IAC/B,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,CAAC,GAAG,EAAE,KAAK,GAAG,IAAI,EAAE,KAAK,EAAE,MAAM,KAAK,IAAI,GAAG,OAAO,CAAC;IAC3F,EAAE,CAAC,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,IAAI,GAAG,OAAO,CAAC;IACpF,cAAc,CAAC,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,IAAI,GAAG,OAAO,CAAC;IAChG,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC;CACvC;AAED,0EAA0E;AAC1E,MAAM,WAAW,iBAAiB;IAChC,OAAO,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC;IACnD,IAAI,IAAI,OAAO,CAAC;CACjB;AAED,MAAM,WAAW,UAAU;IACzB,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,4BAA4B;IAC3C,4BAA4B;IAC5B,GAAG,EAAE,SAAS,CAAC;IACf,+FAA+F;IAC/F,GAAG,EAAE,SAAS,CAAC;IACf,qEAAqE;IACrE,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB,2DAA2D;IAC3D,QAAQ,CAAC,EAAE,UAAU,EAAE,CAAC;IACxB,mGAAmG;IACnG,GAAG,CAAC,EAAE,OAAO,CAAC;IACd,qEAAqE;IACrE,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,6EAA6E;IAC7E,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAeD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,2BAA2B,CAAC,OAAO,EAAE,4BAA4B,GAAG,MAAM,IAAI,CA6H7F"}
1
+ {"version":3,"file":"relay.d.ts","sourceRoot":"","sources":["../src/relay.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,8BAA8B,CAAC;AAC3E,YAAY,EACV,UAAU,EACV,4BAA4B,EAC5B,SAAS,EACT,iBAAiB,GAClB,MAAM,8BAA8B,CAAC"}
package/dist/relay.js CHANGED
@@ -1,163 +1,2 @@
1
- import { randomUUID } from 'node:crypto';
2
- import { CHANNEL_PREFIX, channelName, getChannel, onChannelRegistered, registeredChannels, } from '@dudousxd/nestjs-diagnostics';
3
- const DEFAULT_REDIS_CHANNEL = 'aviary:diagnostics:relay';
4
- /** Strip the `aviary:` prefix and split on the FIRST colon — the event segment may contain dots
5
- * (e.g. `durable:run.failed`), but the lib/event boundary is the first colon after the prefix. */
6
- function parseChannelName(name) {
7
- const prefix = `${CHANNEL_PREFIX}:`;
8
- if (!name.startsWith(prefix))
9
- return null;
10
- const rest = name.slice(prefix.length);
11
- const idx = rest.indexOf(':');
12
- if (idx <= 0 || idx === rest.length - 1)
13
- return null;
14
- return { lib: rest.slice(0, idx), event: rest.slice(idx + 1) };
15
- }
16
- /**
17
- * Relay diagnostics events across processes over Redis pub/sub. Forwards selected local
18
- * `aviary:<lib>:<event>` channels to Redis and re-emits Redis-received events onto the local bus, so
19
- * `@OnDiagnostic` handlers / `getChannel(...).subscribe(...)` fire cross-process. Loop-safe via nodeId
20
- * echo suppression and a re-emit guard. Never throws into `emit()` or the Redis handler. Does NOT
21
- * close the `pub`/`sub` connections — the caller owns them.
22
- *
23
- * Forwarding happens off the emitter's call stack: selected events are queued and, once per event
24
- * loop turn, serialized and published in emit order (one Redis message per event, sent as a single
25
- * pipeline when `pub` supports it).
26
- *
27
- * @returns a teardown that publishes any queued events, then removes all local subscriptions and
28
- * the Redis message handler.
29
- */
30
- export function createDiagnosticsRedisRelay(options) {
31
- const { pub, sub } = options;
32
- const redisChannel = options.redisChannel ?? DEFAULT_REDIS_CHANNEL;
33
- const nodeId = options.nodeId ?? randomUUID();
34
- const forwardAll = options.all === true;
35
- const libs = options.libs ?? [];
36
- const exact = options.channels ?? [];
37
- const subscriptions = [];
38
- const subscribed = new Set();
39
- const reEmitting = new WeakSet();
40
- const pending = [];
41
- let scheduled = null;
42
- const publishOne = (message) => {
43
- try {
44
- pub.publish(redisChannel, message);
45
- }
46
- catch {
47
- // a failing publish drops only its own event
48
- }
49
- };
50
- const flush = () => {
51
- if (scheduled !== null) {
52
- clearImmediate(scheduled);
53
- scheduled = null;
54
- }
55
- if (pending.length === 0)
56
- return;
57
- const messages = [];
58
- for (const env of pending.splice(0)) {
59
- try {
60
- messages.push(JSON.stringify({ node: nodeId, env }));
61
- }
62
- catch {
63
- // an unserializable event is dropped without affecting the rest of the batch
64
- }
65
- }
66
- if (messages.length > 1 && typeof pub.pipeline === 'function') {
67
- try {
68
- const pipeline = pub.pipeline();
69
- for (const message of messages)
70
- pipeline.publish(redisChannel, message);
71
- pipeline.exec();
72
- }
73
- catch {
74
- // never throw out of the flush
75
- }
76
- return;
77
- }
78
- for (const message of messages)
79
- publishOne(message);
80
- };
81
- const forward = (msg) => {
82
- if (typeof msg !== 'object' || msg === null)
83
- return;
84
- if (reEmitting.has(msg))
85
- return; // a re-emitted remote event — do not send it back
86
- pending.push(msg);
87
- scheduled ??= setImmediate(flush);
88
- };
89
- const subscribeRef = (ref) => {
90
- const name = channelName(ref.lib, ref.event);
91
- if (subscribed.has(name))
92
- return;
93
- getChannel(ref.lib, ref.event).subscribe(forward);
94
- subscribed.add(name);
95
- subscriptions.push({ ref, listener: forward });
96
- };
97
- const wildcardMatches = (name) => {
98
- if (forwardAll)
99
- return name.startsWith(`${CHANNEL_PREFIX}:`);
100
- return libs.some((lib) => name.startsWith(`${CHANNEL_PREFIX}:${lib}:`));
101
- };
102
- for (const ref of exact)
103
- subscribeRef(ref);
104
- const hasWildcard = forwardAll || libs.length > 0;
105
- if (hasWildcard) {
106
- for (const name of registeredChannels()) {
107
- if (wildcardMatches(name)) {
108
- const ref = parseChannelName(name);
109
- if (ref)
110
- subscribeRef(ref);
111
- }
112
- }
113
- }
114
- const offRegistered = hasWildcard
115
- ? onChannelRegistered((name) => {
116
- if (wildcardMatches(name)) {
117
- const ref = parseChannelName(name);
118
- if (ref)
119
- subscribeRef(ref);
120
- }
121
- })
122
- : null;
123
- const onMessage = (channel, raw) => {
124
- if (channel !== redisChannel)
125
- return;
126
- let parsed;
127
- try {
128
- parsed = JSON.parse(raw);
129
- }
130
- catch {
131
- return; // ignore malformed
132
- }
133
- if (parsed.node === nodeId)
134
- return; // our own echo
135
- const env = parsed.env;
136
- if (!env || typeof env.lib !== 'string' || typeof env.event !== 'string')
137
- return;
138
- reEmitting.add(env);
139
- try {
140
- getChannel(env.lib, env.event).publish(env);
141
- }
142
- catch {
143
- // a local subscriber threw — never propagate into the message handler
144
- }
145
- finally {
146
- reEmitting.delete(env);
147
- }
148
- };
149
- sub.subscribe(redisChannel);
150
- sub.on('message', onMessage);
151
- return () => {
152
- flush();
153
- for (const { ref, listener } of subscriptions) {
154
- getChannel(ref.lib, ref.event).unsubscribe(listener);
155
- }
156
- subscriptions.length = 0;
157
- subscribed.clear();
158
- offRegistered?.();
159
- sub.removeListener('message', onMessage);
160
- sub.unsubscribe(redisChannel);
161
- };
162
- }
1
+ export { createDiagnosticsRedisRelay } from '@dudousxd/nestjs-diagnostics';
163
2
  //# sourceMappingURL=relay.js.map
package/dist/relay.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"relay.js","sourceRoot":"","sources":["../src/relay.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EACL,cAAc,EAEd,WAAW,EACX,UAAU,EACV,mBAAmB,EACnB,kBAAkB,GACnB,MAAM,8BAA8B,CAAC;AAyCtC,MAAM,qBAAqB,GAAG,0BAA0B,CAAC;AAEzD;mGACmG;AACnG,SAAS,gBAAgB,CAAC,IAAY;IACpC,MAAM,MAAM,GAAG,GAAG,cAAc,GAAG,CAAC;IACpC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC;QAAE,OAAO,IAAI,CAAC;IAC1C,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACvC,MAAM,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IAC9B,IAAI,GAAG,IAAI,CAAC,IAAI,GAAG,KAAK,IAAI,CAAC,MAAM,GAAG,CAAC;QAAE,OAAO,IAAI,CAAC;IACrD,OAAO,EAAE,GAAG,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,GAA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KAAK,EAAE,CAAC;QACnB,aAAa,EAAE,EAAE,CAAC;QAClB,GAAG,CAAC,cAAc,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;QACzC,GAAG,CAAC,WAAW,CAAC,YAAY,CAAC,CAAC;IAChC,CAAC,CAAC;AACJ,CAAC"}
1
+ {"version":3,"file":"relay.js","sourceRoot":"","sources":["../src/relay.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,8BAA8B,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dudousxd/nestjs-diagnostics-redis",
3
- "version": "0.2.5",
3
+ "version": "0.3.0",
4
4
  "description": "Cross-process transport for @dudousxd/nestjs-diagnostics — relay aviary:<lib>:<event> events over Redis pub/sub so @OnDiagnostic handlers fire across processes.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -41,7 +41,7 @@
41
41
  ],
42
42
  "sideEffects": false,
43
43
  "dependencies": {
44
- "@dudousxd/nestjs-diagnostics": "^0.8.0"
44
+ "@dudousxd/nestjs-diagnostics": "^0.9.0"
45
45
  },
46
46
  "peerDependencies": {
47
47
  "ioredis": "^5",
@@ -61,9 +61,9 @@
61
61
  }
62
62
  },
63
63
  "devDependencies": {
64
- "@nestjs/common": "12.0.4",
65
- "@nestjs/core": "12.0.4",
66
- "@nestjs/testing": "12.0.4",
64
+ "@nestjs/common": "12.3.3",
65
+ "@nestjs/core": "12.3.3",
66
+ "@nestjs/testing": "12.3.3",
67
67
  "@types/node": "20.19.43",
68
68
  "ioredis": "5.11.1",
69
69
  "reflect-metadata": "0.2.2",
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: redis-transport
3
- description: Relay @dudousxd/nestjs-diagnostics events across processes/pods over Redis pub/sub with @dudousxd/nestjs-diagnostics-redis. DiagnosticsRedisModule.forRootAsync/forRoot({ pub, sub, libs, channels, all }) wires it from DI; createDiagnosticsRedisRelay() is the manual form returning a teardown. Forwards selected local aviary:<lib>:<event> channels to Redis and re-emits remote events locally so @OnDiagnostic fires cross-process. Needs separate pub and sub ioredis connections (redis.duplicate()); loop-safe via nodeId echo suppression; payloads cross as JSON.
3
+ description: "Relay @dudousxd/nestjs-diagnostics events across processes/pods over Redis pub/sub. The relay lives in `@dudousxd/nestjs-diagnostics` (`createDiagnosticsRedisRelay`, re-exported by `@dudousxd/nestjs-diagnostics-redis`) and is started via `DiagnosticsModule` `transports.redis()` or `DiagnosticsRedisModule.forRoot`/`forRootAsync({ pub, sub, receive, libs, channels, all }) wires it from DI; createDiagnosticsRedisRelay() is the manual form returning a teardown. Forwards selected local aviary:<lib>:<event> channels to Redis and re-emits remote events locally so @OnDiagnostic fires cross-process. Needs separate pub and sub ioredis connections (redis.duplicate()) unless `receive: false` publish-only; loop-safe via nodeId echo suppression with re-emitted copies marked (`isRelayed`); envelopes keep their `id`; payloads cross as JSON."
4
4
  license: MIT
5
5
  metadata:
6
6
  type: core
@@ -11,11 +11,15 @@ metadata:
11
11
 
12
12
  # Cross-process diagnostics over Redis
13
13
 
14
- `@dudousxd/nestjs-diagnostics-redis` is an opt-in **relay**: it forwards selected
14
+ The Redis relay is an opt-in **relay**: it forwards selected
15
15
  local `aviary:<lib>:<event>` channels onto Redis pub/sub and re-emits
16
16
  Redis-received events back onto the local diagnostics bus — so `@OnDiagnostic`
17
17
  handlers and `getChannel(...).subscribe(...)` reactions fire **across
18
- processes/pods**. The diagnostics core stays in-process and untouched.
18
+ processes/pods**. It lives in `@dudousxd/nestjs-diagnostics`
19
+ (`createDiagnosticsRedisRelay`); `@dudousxd/nestjs-diagnostics-redis`
20
+ re-exports it and provides `DiagnosticsRedisModule`. The same relay also
21
+ starts as `DiagnosticsModule` `transports.redis()` (libs with listeners are
22
+ forwarded automatically).
19
23
 
20
24
  ## Setup
21
25
 
@@ -24,7 +28,9 @@ pnpm add @dudousxd/nestjs-diagnostics-redis @dudousxd/nestjs-diagnostics ioredis
24
28
  ```
25
29
 
26
30
  Supply your own ioredis connections — a publisher and a **separate** subscriber
27
- (`redis.duplicate()`, since a subscribed connection can't publish). Prefer
31
+ (`redis.duplicate()`, since a subscribed connection can't publish), unless
32
+ `receive: false` for a publish-only relay (then `sub` is optional, e.g. a
33
+ `role: 'api'` process that publishes and never subscribes). Prefer
28
34
  `forRootAsync` so the relay's clients come from the same DI container:
29
35
 
30
36
  ```ts
@@ -52,7 +58,7 @@ export class AppModule {}
52
58
  Now an `@OnDiagnostic('durable', 'run.failed')` handler in **another** process
53
59
  fires when a worker elsewhere emits it.
54
60
 
55
- Source: `packages/redis/src/diagnostics-redis.module.ts`, `packages/redis/src/relay.ts`.
61
+ Source: `packages/redis/src/diagnostics-redis.module.ts`, `packages/core/src/redis-relay.ts` (re-exported by `packages/redis/src/relay.ts`).
56
62
 
57
63
  ## Core patterns
58
64
 
@@ -75,7 +81,7 @@ DiagnosticsRedisModule.forRoot({
75
81
  Wildcard selections subscribe to matching channels at startup and via
76
82
  `onChannelRegistered` for ones that appear later.
77
83
 
78
- Source: `packages/redis/src/relay.ts` (`wildcardMatches`, `subscribeRef`, `DiagnosticsRedisRelayOptions`).
84
+ Source: `packages/core/src/redis-relay.ts` (`wildcardMatches`, `subscribeRef`, `DiagnosticsRedisRelayOptions`).
79
85
 
80
86
  ### 2. Manual relay outside Nest
81
87
 
@@ -85,34 +91,40 @@ message handler. It never opens or closes your Redis connections — you own the
85
91
  lifecycle:
86
92
 
87
93
  ```ts
88
- import { createDiagnosticsRedisRelay } from '@dudousxd/nestjs-diagnostics-redis';
94
+ import { createDiagnosticsRedisRelay } from '@dudousxd/nestjs-diagnostics';
89
95
 
90
96
  const teardown = createDiagnosticsRedisRelay({
91
97
  pub: redis,
92
98
  sub: redis.duplicate(),
93
99
  libs: ['durable'],
94
100
  });
101
+ // publish-only (never subscribes; sub optional):
102
+ // createDiagnosticsRedisRelay({ pub: redis, receive: false, libs: ['durable'] });
95
103
  // … later
96
104
  teardown();
97
105
  ```
98
106
 
99
- Forwarding is batched off the emitter's call stack: `emit()` only queues the
107
+ Re-exported unchanged from `@dudousxd/nestjs-diagnostics-redis`, so the old
108
+ import path keeps working. Forwarding is batched off the emitter's call stack: `emit()` only queues the
100
109
  event, and the queue is serialized and published once per event loop turn in emit
101
- order (one Redis message per event, unchanged wire format) — via a single
110
+ order (one Redis message per event, unchanged wire format, `id` kept) — via a single
102
111
  pipeline when the turn queued more than one event and `pub` supports
103
112
  `pipeline()`, otherwise one `publish` per message.
104
113
 
105
- Source: `packages/redis/src/relay.ts` (`createDiagnosticsRedisRelay` `forward`/`flush`).
114
+ Source: `packages/core/src/redis-relay.ts` (`createDiagnosticsRedisRelay` `forward`/`flush`).
106
115
 
107
116
  ### 3. Loop-safety
108
117
 
109
118
  Each relay tags its publishes with a per-process `nodeId`. The receiver skips its
110
- own echoes (`node === nodeId`) and a re-emit guard (a `WeakSet`) stops a
111
- received event from being forwarded back — so two processes never ping-pong. The
119
+ own echoes (`node === nodeId`) and a re-emit guard stops a
120
+ received event from being forwarded back — so two processes never ping-pong.
121
+ Re-emitted copies are marked (`isRelayed`) so delivering transports
122
+ (`queue`, `durable`) deliver only at the origin and never twice; the envelope
123
+ keeps its `id` across the hop. The
112
124
  default Redis channel is `aviary:diagnostics:relay` (override with
113
125
  `redisChannel`).
114
126
 
115
- Source: `packages/redis/src/relay.ts` (`forward`, `onMessage`, `reEmitting`).
127
+ Source: `packages/core/src/redis-relay.ts` (`forward`, `onMessage`, `reEmitting`), `packages/core/src/relayed.ts` (`isRelayed`).
116
128
 
117
129
  ## Common mistakes
118
130
 
@@ -131,8 +143,9 @@ DiagnosticsRedisModule.forRoot({ pub: redis, sub: redis.duplicate(), libs: ['dur
131
143
  ```
132
144
 
133
145
  ioredis puts a connection that has `subscribe`d into subscriber mode where most
134
- commands (including `publish`) are rejected — the relay needs two connections.
135
- Source: `packages/redis/src/relay.ts` (`sub` docs: "separate from `pub`").
146
+ commands (including `publish`) are rejected — the relay needs two connections,
147
+ unless `receive: false` (publish-only, no `sub`).
148
+ Source: `packages/core/src/redis-relay.ts` (`sub` docs: "separate from `pub`").
136
149
 
137
150
  ### Reading methods/Date on a cross-process payload
138
151
 
@@ -154,7 +167,7 @@ onFail(e: DiagnosticEvent<{ failedAt: string }>) {
154
167
  Payloads cross the process boundary as JSON: class instances arrive as plain
155
168
  objects (no methods/prototype), `Date` as an ISO string. Read fields, not
156
169
  behavior, in cross-process handlers.
157
- Source: `packages/redis/src/relay.ts` (`forward`/`onMessage` JSON), redis README "Notes".
170
+ Source: `packages/core/src/redis-relay.ts` (`forward`/`onMessage` JSON), redis README "Notes".
158
171
 
159
172
  ### Expecting the module to manage your Redis connection lifecycle
160
173
 
@@ -173,4 +186,4 @@ The relay teardown first publishes any queued events, then removes its own
173
186
  subscriptions and Redis message handler; it never opens or closes `pub`/`sub`.
174
187
  Manage their lifecycle in your own provider.
175
188
  Source: `packages/redis/src/diagnostics-redis.module.ts` (`DiagnosticsRedisStarter` teardown),
176
- `packages/redis/src/relay.ts` (return teardown).
189
+ `packages/core/src/redis-relay.ts` (return teardown).