@dohana/shared 1.1.0 → 1.2.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/README.md +39 -0
- package/dist/lib/nats/index.d.ts +11 -0
- package/dist/lib/nats/index.js +28 -0
- package/dist/lib/nats/index.js.map +1 -0
- package/dist/lib/nats/nats-core.service.d.ts +32 -0
- package/dist/lib/nats/nats-core.service.js +248 -0
- package/dist/lib/nats/nats-core.service.js.map +1 -0
- package/dist/lib/nats/nats-event.d.ts +2 -0
- package/dist/lib/nats/nats-event.js +19 -0
- package/dist/lib/nats/nats-event.js.map +1 -0
- package/dist/lib/nats/nats-queue.consumer.d.ts +24 -0
- package/dist/lib/nats/nats-queue.consumer.js +196 -0
- package/dist/lib/nats/nats-queue.consumer.js.map +1 -0
- package/dist/lib/nats/nats-queue.definition.d.ts +12 -0
- package/dist/lib/nats/nats-queue.definition.js +129 -0
- package/dist/lib/nats/nats-queue.definition.js.map +1 -0
- package/dist/lib/nats/nats-queue.service.d.ts +42 -0
- package/dist/lib/nats/nats-queue.service.js +480 -0
- package/dist/lib/nats/nats-queue.service.js.map +1 -0
- package/dist/lib/nats/nats.config.d.ts +2 -0
- package/dist/lib/nats/nats.config.js +128 -0
- package/dist/lib/nats/nats.config.js.map +1 -0
- package/dist/lib/nats/nats.constants.d.ts +2 -0
- package/dist/lib/nats/nats.constants.js +6 -0
- package/dist/lib/nats/nats.constants.js.map +1 -0
- package/dist/lib/nats/nats.errors.d.ts +18 -0
- package/dist/lib/nats/nats.errors.js +46 -0
- package/dist/lib/nats/nats.errors.js.map +1 -0
- package/dist/lib/nats/nats.module.d.ts +7 -0
- package/dist/lib/nats/nats.module.js +62 -0
- package/dist/lib/nats/nats.module.js.map +1 -0
- package/dist/lib/nats/nats.types.d.ts +173 -0
- package/dist/lib/nats/nats.types.js +3 -0
- package/dist/lib/nats/nats.types.js.map +1 -0
- package/dist/lib/nats/nats.utils.d.ts +23 -0
- package/dist/lib/nats/nats.utils.js +227 -0
- package/dist/lib/nats/nats.utils.js.map +1 -0
- package/package.json +37 -2
package/README.md
CHANGED
|
@@ -22,6 +22,45 @@ Output is emitted to `dist/`.
|
|
|
22
22
|
npm test
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
+
## NATS queue module
|
|
26
|
+
|
|
27
|
+
Generic NATS JetStream work-queue module, exported via the `@dohana/shared/nats`
|
|
28
|
+
subpath. NATS packages are optional peer dependencies; install them only in
|
|
29
|
+
services that use NATS (Node >= 22 required for the NATS v3 client):
|
|
30
|
+
|
|
31
|
+
```shell
|
|
32
|
+
npm install @nats-io/nats-core @nats-io/jetstream @nats-io/transport-node
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { NatsCoreModule } from '@dohana/shared/nats';
|
|
37
|
+
|
|
38
|
+
@Module({
|
|
39
|
+
imports: [
|
|
40
|
+
NatsCoreModule.forRoot({
|
|
41
|
+
servers: ['nats://localhost:4222'],
|
|
42
|
+
auth: { type: 'nkey', seed: process.env.NATS_NKEY_SEED! },
|
|
43
|
+
prefix: 'dev',
|
|
44
|
+
provisionStreams: true,
|
|
45
|
+
provisionConsumers: true,
|
|
46
|
+
}),
|
|
47
|
+
],
|
|
48
|
+
})
|
|
49
|
+
export class AppModule {}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Define a queue once and share the definition between producer and consumer:
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
export const EMAIL_QUEUE = defineNatsQueue<EmailJob>({
|
|
56
|
+
name: 'notification-email',
|
|
57
|
+
subject: 'jobs.notification.email',
|
|
58
|
+
dlq: true,
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
See `dpm-backend/docs/nats-queue.md` for the full operational guide.
|
|
63
|
+
|
|
25
64
|
## License
|
|
26
65
|
|
|
27
66
|
MIT
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from './nats.constants';
|
|
2
|
+
export * from './nats.errors';
|
|
3
|
+
export * from './nats.types';
|
|
4
|
+
export * from './nats.utils';
|
|
5
|
+
export * from './nats.config';
|
|
6
|
+
export * from './nats-event';
|
|
7
|
+
export * from './nats-queue.definition';
|
|
8
|
+
export * from './nats-core.service';
|
|
9
|
+
export * from './nats-queue.service';
|
|
10
|
+
export * from './nats-queue.consumer';
|
|
11
|
+
export * from './nats.module';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./nats.constants"), exports);
|
|
18
|
+
__exportStar(require("./nats.errors"), exports);
|
|
19
|
+
__exportStar(require("./nats.types"), exports);
|
|
20
|
+
__exportStar(require("./nats.utils"), exports);
|
|
21
|
+
__exportStar(require("./nats.config"), exports);
|
|
22
|
+
__exportStar(require("./nats-event"), exports);
|
|
23
|
+
__exportStar(require("./nats-queue.definition"), exports);
|
|
24
|
+
__exportStar(require("./nats-core.service"), exports);
|
|
25
|
+
__exportStar(require("./nats-queue.service"), exports);
|
|
26
|
+
__exportStar(require("./nats-queue.consumer"), exports);
|
|
27
|
+
__exportStar(require("./nats.module"), exports);
|
|
28
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/nats/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,mDAAiC;AACjC,gDAA8B;AAC9B,+CAA6B;AAC7B,+CAA6B;AAC7B,gDAA8B;AAC9B,+CAA6B;AAC7B,0DAAwC;AACxC,sDAAoC;AACpC,uDAAqC;AACrC,wDAAsC;AACtC,gDAA8B"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { OnModuleDestroy, OnModuleInit } from '@nestjs/common';
|
|
2
|
+
import type { NatsConnection } from '@nats-io/nats-core';
|
|
3
|
+
import type { JetStreamClient, JetStreamManager } from '@nats-io/jetstream';
|
|
4
|
+
import type { NatsConnectFn, NatsCoreStatus, NormalizedNatsOptions } from './nats.types';
|
|
5
|
+
export declare class NatsCoreService implements OnModuleInit, OnModuleDestroy {
|
|
6
|
+
private readonly options;
|
|
7
|
+
private readonly connectFn;
|
|
8
|
+
private readonly logger;
|
|
9
|
+
private connection?;
|
|
10
|
+
private js?;
|
|
11
|
+
private jsm?;
|
|
12
|
+
private startPromise?;
|
|
13
|
+
private closing;
|
|
14
|
+
private state;
|
|
15
|
+
private connectedAt?;
|
|
16
|
+
private lastError?;
|
|
17
|
+
constructor(options: NormalizedNatsOptions, connectFn: NatsConnectFn);
|
|
18
|
+
onModuleInit(): void;
|
|
19
|
+
onModuleDestroy(): Promise<void>;
|
|
20
|
+
get enabled(): boolean;
|
|
21
|
+
get prefix(): string;
|
|
22
|
+
get isConnected(): boolean;
|
|
23
|
+
waitUntilConnected(timeoutMs?: number): Promise<boolean>;
|
|
24
|
+
requireConnection(): NatsConnection;
|
|
25
|
+
requireJetStream(): JetStreamClient;
|
|
26
|
+
requireManager(): JetStreamManager;
|
|
27
|
+
getStatus(): NatsCoreStatus;
|
|
28
|
+
private connectLoop;
|
|
29
|
+
private openConnection;
|
|
30
|
+
private buildConnectionOptions;
|
|
31
|
+
private watchStatus;
|
|
32
|
+
}
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
var NatsCoreService_1;
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.NatsCoreService = void 0;
|
|
17
|
+
const common_1 = require("@nestjs/common");
|
|
18
|
+
const nats_core_1 = require("@nats-io/nats-core");
|
|
19
|
+
const jetstream_1 = require("@nats-io/jetstream");
|
|
20
|
+
const nats_constants_1 = require("./nats.constants");
|
|
21
|
+
const nats_errors_1 = require("./nats.errors");
|
|
22
|
+
const nats_utils_1 = require("./nats.utils");
|
|
23
|
+
let NatsCoreService = NatsCoreService_1 = class NatsCoreService {
|
|
24
|
+
constructor(options, connectFn) {
|
|
25
|
+
this.options = options;
|
|
26
|
+
this.connectFn = connectFn;
|
|
27
|
+
this.logger = new common_1.Logger(NatsCoreService_1.name);
|
|
28
|
+
this.closing = false;
|
|
29
|
+
this.state = options.enabled ? 'connecting' : 'disabled';
|
|
30
|
+
}
|
|
31
|
+
onModuleInit() {
|
|
32
|
+
if (!this.options.enabled) {
|
|
33
|
+
this.logger.log('NATS disabled');
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
this.logger.log(`NATS connecting: servers=${redactServers(this.options.servers).join(',')} prefix="${this.options.prefix || '(none)'}"`);
|
|
37
|
+
this.startPromise = this.connectLoop();
|
|
38
|
+
}
|
|
39
|
+
async onModuleDestroy() {
|
|
40
|
+
this.closing = true;
|
|
41
|
+
this.state = 'closed';
|
|
42
|
+
const connection = this.connection;
|
|
43
|
+
this.connection = undefined;
|
|
44
|
+
this.js = undefined;
|
|
45
|
+
this.jsm = undefined;
|
|
46
|
+
this.connectedAt = undefined;
|
|
47
|
+
if (connection) {
|
|
48
|
+
try {
|
|
49
|
+
await (0, nats_utils_1.withTimeout)(connection.drain(), 10_000);
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
this.logger.warn(`NATS drain failed: ${(0, nats_utils_1.truncate)((0, nats_utils_1.errorMessage)(error), 300)}`);
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
await connection.close();
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
await (0, nats_utils_1.withTimeout)(this.startPromise, 5_000);
|
|
61
|
+
this.logger.log('NATS stopped');
|
|
62
|
+
}
|
|
63
|
+
get enabled() {
|
|
64
|
+
return this.options.enabled;
|
|
65
|
+
}
|
|
66
|
+
get prefix() {
|
|
67
|
+
return this.options.prefix;
|
|
68
|
+
}
|
|
69
|
+
get isConnected() {
|
|
70
|
+
const connection = this.connection;
|
|
71
|
+
return (this.state === 'connected' &&
|
|
72
|
+
!!connection &&
|
|
73
|
+
!connection.isClosed() &&
|
|
74
|
+
!connection.isDraining());
|
|
75
|
+
}
|
|
76
|
+
async waitUntilConnected(timeoutMs = 30_000) {
|
|
77
|
+
if (!this.options.enabled)
|
|
78
|
+
return false;
|
|
79
|
+
if (this.isConnected)
|
|
80
|
+
return true;
|
|
81
|
+
const deadline = Date.now() + timeoutMs;
|
|
82
|
+
while (Date.now() < deadline) {
|
|
83
|
+
if (this.isConnected)
|
|
84
|
+
return true;
|
|
85
|
+
if (this.closing)
|
|
86
|
+
return false;
|
|
87
|
+
await (0, nats_utils_1.delay)(250);
|
|
88
|
+
}
|
|
89
|
+
return this.isConnected;
|
|
90
|
+
}
|
|
91
|
+
requireConnection() {
|
|
92
|
+
if (!this.options.enabled) {
|
|
93
|
+
throw new nats_errors_1.NatsDisabledError();
|
|
94
|
+
}
|
|
95
|
+
if (!this.isConnected || !this.connection) {
|
|
96
|
+
throw new nats_errors_1.NatsUnavailableError(this.lastError ?? 'NATS is not connected');
|
|
97
|
+
}
|
|
98
|
+
return this.connection;
|
|
99
|
+
}
|
|
100
|
+
requireJetStream() {
|
|
101
|
+
const connection = this.requireConnection();
|
|
102
|
+
if (!this.js) {
|
|
103
|
+
throw new nats_errors_1.NatsUnavailableError('JetStream client is not ready');
|
|
104
|
+
}
|
|
105
|
+
return this.js;
|
|
106
|
+
}
|
|
107
|
+
requireManager() {
|
|
108
|
+
this.requireConnection();
|
|
109
|
+
if (!this.jsm) {
|
|
110
|
+
throw new nats_errors_1.NatsUnavailableError('JetStream manager is not ready');
|
|
111
|
+
}
|
|
112
|
+
return this.jsm;
|
|
113
|
+
}
|
|
114
|
+
getStatus() {
|
|
115
|
+
return {
|
|
116
|
+
enabled: this.options.enabled,
|
|
117
|
+
state: this.state,
|
|
118
|
+
connected: this.isConnected,
|
|
119
|
+
server: this.isConnected ? this.connection?.getServer() : undefined,
|
|
120
|
+
prefix: this.options.prefix,
|
|
121
|
+
since: this.connectedAt?.toISOString(),
|
|
122
|
+
lastError: this.lastError,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
async connectLoop() {
|
|
126
|
+
let attempt = 0;
|
|
127
|
+
while (!this.closing) {
|
|
128
|
+
try {
|
|
129
|
+
await this.openConnection();
|
|
130
|
+
attempt = 0;
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
if (this.closing)
|
|
134
|
+
break;
|
|
135
|
+
this.lastError = (0, nats_utils_1.truncate)((0, nats_utils_1.errorMessage)(error), 1_000);
|
|
136
|
+
this.state = 'connecting';
|
|
137
|
+
this.logger.error(`NATS connection failed: ${this.lastError}`);
|
|
138
|
+
}
|
|
139
|
+
if (this.closing)
|
|
140
|
+
break;
|
|
141
|
+
const wait = Math.min(1_000 * 2 ** Math.min(attempt, 5), 30_000);
|
|
142
|
+
await (0, nats_utils_1.delay)((0, nats_utils_1.delayWithJitter)(wait));
|
|
143
|
+
attempt++;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
async openConnection() {
|
|
147
|
+
this.state = 'connecting';
|
|
148
|
+
const connection = await this.connectFn(this.buildConnectionOptions());
|
|
149
|
+
this.connection = connection;
|
|
150
|
+
this.connectedAt = new Date();
|
|
151
|
+
this.lastError = undefined;
|
|
152
|
+
try {
|
|
153
|
+
this.jsm = await (0, jetstream_1.jetstreamManager)(connection, { checkAPI: false });
|
|
154
|
+
this.js = (0, jetstream_1.jetstream)(connection);
|
|
155
|
+
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
this.connection = undefined;
|
|
158
|
+
this.jsm = undefined;
|
|
159
|
+
this.js = undefined;
|
|
160
|
+
await connection.close();
|
|
161
|
+
throw error;
|
|
162
|
+
}
|
|
163
|
+
this.state = 'connected';
|
|
164
|
+
this.logger.log(`NATS connected: ${connection.getServer()} (prefix="${this.options.prefix || '(none)'}")`);
|
|
165
|
+
this.watchStatus(connection);
|
|
166
|
+
const closedError = await connection.closed();
|
|
167
|
+
if (!this.closing) {
|
|
168
|
+
this.lastError = (0, nats_utils_1.truncate)(closedError instanceof Error
|
|
169
|
+
? closedError.message
|
|
170
|
+
: 'connection closed', 1_000);
|
|
171
|
+
this.logger.error(`NATS connection closed: ${this.lastError}`);
|
|
172
|
+
}
|
|
173
|
+
this.connection = undefined;
|
|
174
|
+
this.js = undefined;
|
|
175
|
+
this.jsm = undefined;
|
|
176
|
+
this.connectedAt = undefined;
|
|
177
|
+
this.state = this.closing ? 'closed' : 'connecting';
|
|
178
|
+
}
|
|
179
|
+
buildConnectionOptions() {
|
|
180
|
+
const options = {
|
|
181
|
+
servers: this.options.servers,
|
|
182
|
+
name: this.options.name,
|
|
183
|
+
tls: this.options.tls,
|
|
184
|
+
reconnect: true,
|
|
185
|
+
maxReconnectAttempts: this.options.maxReconnectAttempts,
|
|
186
|
+
reconnectTimeWait: this.options.reconnectTimeWaitMs,
|
|
187
|
+
reconnectJitter: this.options.reconnectJitterMs,
|
|
188
|
+
timeout: this.options.connectTimeoutMs,
|
|
189
|
+
waitOnFirstConnect: false,
|
|
190
|
+
noAsyncTraces: true,
|
|
191
|
+
};
|
|
192
|
+
if (this.options.auth.type === 'nkey') {
|
|
193
|
+
options.authenticator = (0, nats_core_1.nkeyAuthenticator)(new TextEncoder().encode(this.options.auth.seed));
|
|
194
|
+
}
|
|
195
|
+
return options;
|
|
196
|
+
}
|
|
197
|
+
watchStatus(connection) {
|
|
198
|
+
if (!this.options.logConnectionEvents)
|
|
199
|
+
return;
|
|
200
|
+
void (async () => {
|
|
201
|
+
try {
|
|
202
|
+
for await (const status of connection.status()) {
|
|
203
|
+
switch (status.type) {
|
|
204
|
+
case 'disconnect':
|
|
205
|
+
this.logger.warn(`NATS disconnected from ${status.server}`);
|
|
206
|
+
break;
|
|
207
|
+
case 'reconnecting':
|
|
208
|
+
this.logger.warn('NATS reconnecting');
|
|
209
|
+
break;
|
|
210
|
+
case 'reconnect':
|
|
211
|
+
this.logger.log(`NATS reconnected to ${status.server}`);
|
|
212
|
+
break;
|
|
213
|
+
case 'ldm':
|
|
214
|
+
this.logger.warn(`NATS server in lame duck mode: ${status.server}`);
|
|
215
|
+
break;
|
|
216
|
+
case 'staleConnection':
|
|
217
|
+
this.logger.warn('NATS connection is stale');
|
|
218
|
+
break;
|
|
219
|
+
case 'error':
|
|
220
|
+
this.logger.error(`NATS async error: ${(0, nats_utils_1.truncate)((0, nats_utils_1.errorMessage)(status.error), 500)}`);
|
|
221
|
+
break;
|
|
222
|
+
case 'slowConsumer':
|
|
223
|
+
this.logger.warn(`NATS slow consumer: pending=${status.pending}`);
|
|
224
|
+
break;
|
|
225
|
+
default:
|
|
226
|
+
break;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
catch (error) {
|
|
231
|
+
if (!this.closing) {
|
|
232
|
+
this.logger.debug(`NATS status watcher stopped: ${(0, nats_utils_1.truncate)((0, nats_utils_1.errorMessage)(error), 300)}`);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
})();
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
exports.NatsCoreService = NatsCoreService;
|
|
239
|
+
exports.NatsCoreService = NatsCoreService = NatsCoreService_1 = __decorate([
|
|
240
|
+
(0, common_1.Injectable)(),
|
|
241
|
+
__param(0, (0, common_1.Inject)(nats_constants_1.NATS_OPTIONS)),
|
|
242
|
+
__param(1, (0, common_1.Inject)(nats_constants_1.NATS_CONNECT)),
|
|
243
|
+
__metadata("design:paramtypes", [Object, Function])
|
|
244
|
+
], NatsCoreService);
|
|
245
|
+
function redactServers(servers) {
|
|
246
|
+
return servers.map((server) => server.replace(/\/\/[^/@]+@/, '//***@'));
|
|
247
|
+
}
|
|
248
|
+
//# sourceMappingURL=nats-core.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nats-core.service.js","sourceRoot":"","sources":["../../../src/lib/nats/nats-core.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAMwB;AACxB,kDAAuD;AAEvD,kDAAiE;AAEjE,qDAA8D;AAC9D,+CAAwE;AAOxE,6CAMsB;AAGf,IAAM,eAAe,uBAArB,MAAM,eAAe;IAW1B,YAEE,OAA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OAAO,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,aAAa,EAAE,QAAQ,CAAC,CAAC,CAAC;AAC1E,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createNatsEvent = createNatsEvent;
|
|
4
|
+
const node_crypto_1 = require("node:crypto");
|
|
5
|
+
function createNatsEvent(input) {
|
|
6
|
+
return {
|
|
7
|
+
id: input.id ?? (0, node_crypto_1.randomUUID)(),
|
|
8
|
+
type: input.type,
|
|
9
|
+
version: input.version ?? 1,
|
|
10
|
+
occurredAt: input.occurredAt instanceof Date
|
|
11
|
+
? input.occurredAt.toISOString()
|
|
12
|
+
: (input.occurredAt ?? new Date().toISOString()),
|
|
13
|
+
producer: input.producer,
|
|
14
|
+
correlationId: input.correlationId,
|
|
15
|
+
causationId: input.causationId,
|
|
16
|
+
payload: input.payload,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=nats-event.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nats-event.js","sourceRoot":"","sources":["../../../src/lib/nats/nats-event.ts"],"names":[],"mappings":";;AAQA,0CAgBC;AAxBD,6CAAyC;AAQzC,SAAgB,eAAe,CAC7B,KAA8B;IAE9B,OAAO;QACL,EAAE,EAAE,KAAK,CAAC,EAAE,IAAI,IAAA,wBAAU,GAAE;QAC5B,IAAI,EAAE,KAAK,CAAC,IAAI;QAChB,OAAO,EAAE,KAAK,CAAC,OAAO,IAAI,CAAC;QAC3B,UAAU,EACR,KAAK,CAAC,UAAU,YAAY,IAAI;YAC9B,CAAC,CAAC,KAAK,CAAC,UAAU,CAAC,WAAW,EAAE;YAChC,CAAC,CAAC,CAAC,KAAK,CAAC,UAAU,IAAI,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;QACpD,QAAQ,EAAE,KAAK,CAAC,QAAQ;QACxB,aAAa,EAAE,KAAK,CAAC,aAAa;QAClC,WAAW,EAAE,KAAK,CAAC,WAAW;QAC9B,OAAO,EAAE,KAAK,CAAC,OAAO;KACvB,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Logger, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
|
|
2
|
+
import { NatsQueueService } from './nats-queue.service';
|
|
3
|
+
import type { NatsMessageContext, NatsQueueDefinition, NatsQueueNames } from './nats.types';
|
|
4
|
+
export declare abstract class NatsQueueConsumer<T> implements OnModuleInit, OnModuleDestroy {
|
|
5
|
+
protected readonly nats: NatsQueueService;
|
|
6
|
+
protected readonly queue: NatsQueueDefinition<T>;
|
|
7
|
+
protected readonly logger: Logger;
|
|
8
|
+
protected readonly names: NatsQueueNames;
|
|
9
|
+
private running?;
|
|
10
|
+
private messages?;
|
|
11
|
+
private closing;
|
|
12
|
+
private readonly inFlight;
|
|
13
|
+
protected constructor(nats: NatsQueueService, queue: NatsQueueDefinition<T>);
|
|
14
|
+
onModuleInit(): void;
|
|
15
|
+
onModuleDestroy(): Promise<void>;
|
|
16
|
+
protected abstract handle(payload: T, context: NatsMessageContext): Promise<void>;
|
|
17
|
+
protected get isClosing(): boolean;
|
|
18
|
+
private run;
|
|
19
|
+
private pump;
|
|
20
|
+
private process;
|
|
21
|
+
private decode;
|
|
22
|
+
private handleFailure;
|
|
23
|
+
private backoffFor;
|
|
24
|
+
}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NatsQueueConsumer = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
const nats_errors_1 = require("./nats.errors");
|
|
6
|
+
const nats_utils_1 = require("./nats.utils");
|
|
7
|
+
class NatsQueueConsumer {
|
|
8
|
+
constructor(nats, queue) {
|
|
9
|
+
this.nats = nats;
|
|
10
|
+
this.queue = queue;
|
|
11
|
+
this.closing = false;
|
|
12
|
+
this.inFlight = new Set();
|
|
13
|
+
this.logger = new common_1.Logger(`${new.target.name}:${queue.name}`);
|
|
14
|
+
this.names = nats.resolveNames(queue);
|
|
15
|
+
}
|
|
16
|
+
onModuleInit() {
|
|
17
|
+
if (!this.nats.enabled) {
|
|
18
|
+
this.logger.warn('NATS disabled — consumer skipped');
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
this.nats.registerConsumer(this.queue);
|
|
22
|
+
this.logger.log(`Starting ${this.names.stream}/${this.names.consumer} ` +
|
|
23
|
+
`(concurrency=${this.queue.concurrency})`);
|
|
24
|
+
this.running = this.run().catch((error) => {
|
|
25
|
+
if (!this.closing) {
|
|
26
|
+
this.logger.error(`Consumer stopped unexpectedly: ${(0, nats_utils_1.truncate)((0, nats_utils_1.errorMessage)(error), 500)}`);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
async onModuleDestroy() {
|
|
31
|
+
this.closing = true;
|
|
32
|
+
this.messages?.stop();
|
|
33
|
+
await (0, nats_utils_1.withTimeout)(this.running, 10_000);
|
|
34
|
+
await (0, nats_utils_1.withTimeout)(Promise.allSettled([...this.inFlight]), 10_000);
|
|
35
|
+
this.logger.log('Consumer stopped');
|
|
36
|
+
}
|
|
37
|
+
get isClosing() {
|
|
38
|
+
return this.closing;
|
|
39
|
+
}
|
|
40
|
+
async run() {
|
|
41
|
+
let backoff = 1_000;
|
|
42
|
+
while (!this.closing) {
|
|
43
|
+
try {
|
|
44
|
+
const connected = await this.nats.waitUntilConnected();
|
|
45
|
+
if (this.closing)
|
|
46
|
+
break;
|
|
47
|
+
if (!connected) {
|
|
48
|
+
throw new nats_errors_1.NatsUnavailableError('NATS is not connected');
|
|
49
|
+
}
|
|
50
|
+
await this.nats.ensureConsumer(this.queue);
|
|
51
|
+
if (this.closing)
|
|
52
|
+
break;
|
|
53
|
+
const consumer = await this.nats.getConsumer(this.queue);
|
|
54
|
+
if (this.closing)
|
|
55
|
+
break;
|
|
56
|
+
this.logger.log('Consumer ready');
|
|
57
|
+
await this.pump(consumer);
|
|
58
|
+
backoff = 1_000;
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
if (this.closing)
|
|
62
|
+
break;
|
|
63
|
+
this.nats.invalidateConsumer(this.queue);
|
|
64
|
+
this.logger.error(`Consumer loop failed: ${(0, nats_utils_1.truncate)((0, nats_utils_1.errorMessage)(error), 500)}`);
|
|
65
|
+
await new Promise((resolve) => setTimeout(resolve, (0, nats_utils_1.delayWithJitter)(backoff)));
|
|
66
|
+
backoff = Math.min(backoff * 2, 30_000);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
async pump(consumer) {
|
|
71
|
+
while (!this.closing) {
|
|
72
|
+
const messages = await consumer.fetch({
|
|
73
|
+
max_messages: this.queue.concurrency,
|
|
74
|
+
expires: 30_000,
|
|
75
|
+
});
|
|
76
|
+
this.messages = messages;
|
|
77
|
+
try {
|
|
78
|
+
const batch = [];
|
|
79
|
+
for await (const message of messages) {
|
|
80
|
+
if (this.closing) {
|
|
81
|
+
try {
|
|
82
|
+
message.nak();
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
}
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const task = this.process(message)
|
|
89
|
+
.catch((error) => {
|
|
90
|
+
this.logger.error(`Unhandled message error: ${(0, nats_utils_1.truncate)((0, nats_utils_1.errorMessage)(error), 500)}`);
|
|
91
|
+
})
|
|
92
|
+
.finally(() => {
|
|
93
|
+
this.inFlight.delete(task);
|
|
94
|
+
this.nats.recordInFlight(this.queue, this.inFlight.size);
|
|
95
|
+
});
|
|
96
|
+
this.inFlight.add(task);
|
|
97
|
+
this.nats.recordInFlight(this.queue, this.inFlight.size);
|
|
98
|
+
batch.push(task);
|
|
99
|
+
}
|
|
100
|
+
await Promise.allSettled(batch);
|
|
101
|
+
}
|
|
102
|
+
finally {
|
|
103
|
+
this.messages = undefined;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
async process(message) {
|
|
108
|
+
const context = (0, nats_utils_1.messageContext)(message, this.names);
|
|
109
|
+
const startedAt = Date.now();
|
|
110
|
+
let heartbeat;
|
|
111
|
+
let failure;
|
|
112
|
+
try {
|
|
113
|
+
heartbeat = setInterval(() => {
|
|
114
|
+
try {
|
|
115
|
+
message.working();
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
}
|
|
119
|
+
}, Math.max(1_000, Math.floor(this.queue.ackWaitMs / 3)));
|
|
120
|
+
const payload = this.decode(message);
|
|
121
|
+
await this.handle(payload, context);
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
failure = error;
|
|
125
|
+
}
|
|
126
|
+
finally {
|
|
127
|
+
if (heartbeat)
|
|
128
|
+
clearInterval(heartbeat);
|
|
129
|
+
}
|
|
130
|
+
if (failure) {
|
|
131
|
+
await this.handleFailure(message, failure, context);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
try {
|
|
135
|
+
message.ack();
|
|
136
|
+
this.nats.recordProcessed(this.queue);
|
|
137
|
+
this.logger.debug(`Processed ${context.stream}/${context.consumer} seq=${context.streamSequence} ` +
|
|
138
|
+
`in ${Date.now() - startedAt}ms`);
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
this.logger.warn(`Ack failed for ${context.stream}/${context.consumer} seq=${context.streamSequence}: ` +
|
|
142
|
+
(0, nats_utils_1.truncate)((0, nats_utils_1.errorMessage)(error), 300));
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
decode(message) {
|
|
146
|
+
try {
|
|
147
|
+
return (0, nats_utils_1.decodeJson)(message.data);
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
throw new nats_errors_1.NatsInvalidMessageError(`Invalid JSON payload: ${(0, nats_utils_1.errorMessage)(error)}`);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
async handleFailure(message, error, context) {
|
|
154
|
+
const messageText = (0, nats_utils_1.truncate)((0, nats_utils_1.errorMessage)(error), 500);
|
|
155
|
+
const terminal = error instanceof nats_errors_1.NonRetryableError ||
|
|
156
|
+
(this.queue.maxDeliver > 0 &&
|
|
157
|
+
context.deliveryCount >= this.queue.maxDeliver);
|
|
158
|
+
if (!terminal) {
|
|
159
|
+
const delay = this.backoffFor(context.deliveryCount);
|
|
160
|
+
try {
|
|
161
|
+
message.nak(delay);
|
|
162
|
+
}
|
|
163
|
+
catch (nakError) {
|
|
164
|
+
this.logger.warn(`NAK failed for seq=${context.streamSequence}: ${(0, nats_utils_1.truncate)((0, nats_utils_1.errorMessage)(nakError), 300)}`);
|
|
165
|
+
}
|
|
166
|
+
this.nats.recordRetried(this.queue, error);
|
|
167
|
+
this.logger.warn(`Retry ${context.deliveryCount}/${this.queue.maxDeliver} ` +
|
|
168
|
+
`${context.stream}/${context.consumer} seq=${context.streamSequence} ` +
|
|
169
|
+
`in ${delay}ms: ${messageText}`);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
let movedToDlq = false;
|
|
173
|
+
try {
|
|
174
|
+
movedToDlq = await this.nats.publishToDlq(this.queue, message, error);
|
|
175
|
+
}
|
|
176
|
+
catch (dlqError) {
|
|
177
|
+
this.logger.error(`DLQ publish failed for seq=${context.streamSequence}: ${(0, nats_utils_1.truncate)((0, nats_utils_1.errorMessage)(dlqError), 500)}`);
|
|
178
|
+
}
|
|
179
|
+
try {
|
|
180
|
+
message.term(`failed: ${messageText}`);
|
|
181
|
+
}
|
|
182
|
+
catch (termError) {
|
|
183
|
+
this.logger.warn(`TERM failed for seq=${context.streamSequence}: ${(0, nats_utils_1.truncate)((0, nats_utils_1.errorMessage)(termError), 300)}`);
|
|
184
|
+
}
|
|
185
|
+
this.nats.recordFailed(this.queue, error);
|
|
186
|
+
this.logger.error(`Message ${movedToDlq ? 'moved to DLQ' : 'terminated'} ` +
|
|
187
|
+
`${context.stream}/${context.consumer} seq=${context.streamSequence} ` +
|
|
188
|
+
`deliveries=${context.deliveryCount}: ${messageText}`);
|
|
189
|
+
}
|
|
190
|
+
backoffFor(deliveryCount) {
|
|
191
|
+
const index = Math.max(0, Math.min(deliveryCount - 1, this.queue.backoffMs.length - 1));
|
|
192
|
+
return (0, nats_utils_1.delayWithJitter)(this.queue.backoffMs[index], 0.2);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
exports.NatsQueueConsumer = NatsQueueConsumer;
|
|
196
|
+
//# sourceMappingURL=nats-queue.consumer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nats-queue.consumer.js","sourceRoot":"","sources":["../../../src/lib/nats/nats-queue.consumer.ts"],"names":[],"mappings":";;;AAAA,2CAAuE;AAGvE,+CAIuB;AAMvB,6CAOsB;AAQtB,MAAsB,iBAAiB;IAUrC,YACqB,IAAsB,EACtB,KAA6B;QAD7B,SAAI,GAAJ,IAAI,CAAkB;QACtB,UAAK,GAAL,KAAK,CAAwB;QAL1C,YAAO,GAAG,KAAK,CAAC;QACP,aAAQ,GAAG,IAAI,GAAG,EAAiB,CAAC;QAMnD,IAAI,CAAC,MAAM,GAAG,IAAI,eAAM,CAAC,GAAG,GAAG,CAAC,MAAM,CAAC,IAAI,IAAI,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;QAC7D,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;IACxC,CAAC;IAED,YAAY;QACV,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;YACvB,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,kCAAkC,CAAC,CAAC;YACrD,OAAO;QACT,CAAC;QACD,IAAI,CAAC,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACvC,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CAAC,MAAM,CAAC,KAAK,CACf,WAAW,UAAU,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,YAAY,GAAG;YACtD,GAAG,OAAO,CAAC,MAAM,IAAI,OAAO,CAAC,QAAQ,QAAQ,OAAO,CAAC,cAAc,GAAG;YACtE,cAAc,OAAO,CAAC,aAAa,KAAK,WAAW,EAAE,CACxD,CAAC;IACJ,CAAC;IAEO,UAAU,CAAC,aAAqB;QACtC,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CACpB,CAAC,EACD,IAAI,CAAC,GAAG,CAAC,aAAa,GAAG,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC,CAC7D,CAAC;QACF,OAAO,IAAA,4BAAe,EAAC,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,GAAG,CAAC,CAAC;IAC3D,CAAC;CACF;AApQD,8CAoQC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { NatsQueueDefinition, NatsQueueInput } from './nats.types';
|
|
2
|
+
export declare const NATS_QUEUE_DEFAULTS: {
|
|
3
|
+
readonly maxAgeMs: number;
|
|
4
|
+
readonly maxBytes: number;
|
|
5
|
+
readonly maxMsgSize: number;
|
|
6
|
+
readonly duplicateWindowMs: number;
|
|
7
|
+
readonly ackWaitMs: 30000;
|
|
8
|
+
readonly maxDeliver: 5;
|
|
9
|
+
readonly backoffMs: readonly [5000, 15000, 60000, 300000, 900000];
|
|
10
|
+
readonly concurrency: 5;
|
|
11
|
+
};
|
|
12
|
+
export declare function defineNatsQueue<T = unknown>(input: NatsQueueInput<T>): NatsQueueDefinition<T>;
|