@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
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NATS_QUEUE_DEFAULTS = void 0;
|
|
4
|
+
exports.defineNatsQueue = defineNatsQueue;
|
|
5
|
+
const nats_errors_1 = require("./nats.errors");
|
|
6
|
+
const nats_utils_1 = require("./nats.utils");
|
|
7
|
+
exports.NATS_QUEUE_DEFAULTS = {
|
|
8
|
+
maxAgeMs: 7 * 24 * 60 * 60 * 1000,
|
|
9
|
+
maxBytes: 1024 * 1024 * 1024,
|
|
10
|
+
maxMsgSize: 1024 * 1024,
|
|
11
|
+
duplicateWindowMs: 2 * 60 * 1000,
|
|
12
|
+
ackWaitMs: 30_000,
|
|
13
|
+
maxDeliver: 5,
|
|
14
|
+
backoffMs: [5_000, 15_000, 60_000, 300_000, 900_000],
|
|
15
|
+
concurrency: 5,
|
|
16
|
+
};
|
|
17
|
+
function defineNatsQueue(input) {
|
|
18
|
+
if (!input?.name) {
|
|
19
|
+
throw new nats_errors_1.NatsQueueConfigError('NATS queue name is required');
|
|
20
|
+
}
|
|
21
|
+
(0, nats_utils_1.assertName)(input.name, 'queue name');
|
|
22
|
+
(0, nats_utils_1.assertSubject)(input.subject, 'queue subject');
|
|
23
|
+
const stream = input.stream ?? input.name;
|
|
24
|
+
const consumer = input.consumer ?? `${input.name}-worker`;
|
|
25
|
+
(0, nats_utils_1.assertName)(stream, 'stream');
|
|
26
|
+
(0, nats_utils_1.assertName)(consumer, 'consumer');
|
|
27
|
+
const maxAgeMs = duration(input.maxAgeMs, exports.NATS_QUEUE_DEFAULTS.maxAgeMs, 'maxAgeMs');
|
|
28
|
+
const duplicateWindowMs = duration(input.duplicateWindowMs, exports.NATS_QUEUE_DEFAULTS.duplicateWindowMs, 'duplicateWindowMs');
|
|
29
|
+
if (maxAgeMs > 0 && duplicateWindowMs > maxAgeMs) {
|
|
30
|
+
throw new nats_errors_1.NatsQueueConfigError('NATS duplicateWindowMs cannot be greater than maxAgeMs');
|
|
31
|
+
}
|
|
32
|
+
const concurrency = positiveInteger(input.concurrency, exports.NATS_QUEUE_DEFAULTS.concurrency, 'concurrency');
|
|
33
|
+
const maxDeliver = maxDeliverOption(input.maxDeliver);
|
|
34
|
+
const backoffMs = backoffOption(input.backoffMs, maxDeliver);
|
|
35
|
+
const maxAckPending = positiveInteger(input.maxAckPending, concurrency * 2, 'maxAckPending');
|
|
36
|
+
if (maxAckPending < concurrency) {
|
|
37
|
+
throw new nats_errors_1.NatsQueueConfigError('NATS maxAckPending must be greater than or equal to concurrency');
|
|
38
|
+
}
|
|
39
|
+
const replicas = optionalReplicas(input.replicas);
|
|
40
|
+
const dlq = input.dlq ? normalizeDlq(input, stream) : undefined;
|
|
41
|
+
return Object.freeze({
|
|
42
|
+
name: input.name,
|
|
43
|
+
stream,
|
|
44
|
+
subject: input.subject,
|
|
45
|
+
consumer,
|
|
46
|
+
maxAgeMs,
|
|
47
|
+
maxBytes: positiveInteger(input.maxBytes, exports.NATS_QUEUE_DEFAULTS.maxBytes, 'maxBytes'),
|
|
48
|
+
maxMsgSize: positiveInteger(input.maxMsgSize, exports.NATS_QUEUE_DEFAULTS.maxMsgSize, 'maxMsgSize'),
|
|
49
|
+
duplicateWindowMs,
|
|
50
|
+
replicas,
|
|
51
|
+
ackWaitMs: positiveInteger(input.ackWaitMs, exports.NATS_QUEUE_DEFAULTS.ackWaitMs, 'ackWaitMs'),
|
|
52
|
+
maxDeliver,
|
|
53
|
+
backoffMs: Object.freeze(backoffMs),
|
|
54
|
+
maxAckPending,
|
|
55
|
+
concurrency,
|
|
56
|
+
description: input.description,
|
|
57
|
+
dlq,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
function normalizeDlq(input, stream) {
|
|
61
|
+
if (!input.dlq)
|
|
62
|
+
return undefined;
|
|
63
|
+
const dlqInput = typeof input.dlq === 'object' ? input.dlq : undefined;
|
|
64
|
+
if (dlqInput?.enabled === false)
|
|
65
|
+
return undefined;
|
|
66
|
+
const dlqStream = dlqInput?.stream ?? `${stream}-dlq`;
|
|
67
|
+
const dlqSubject = dlqInput?.subject ?? `dlq.${input.name}`;
|
|
68
|
+
(0, nats_utils_1.assertName)(dlqStream, 'DLQ stream');
|
|
69
|
+
(0, nats_utils_1.assertSubject)(dlqSubject, 'DLQ subject');
|
|
70
|
+
if (dlqSubject === input.subject) {
|
|
71
|
+
throw new nats_errors_1.NatsQueueConfigError('NATS DLQ subject must be different from the queue subject');
|
|
72
|
+
}
|
|
73
|
+
return Object.freeze({
|
|
74
|
+
stream: dlqStream,
|
|
75
|
+
subject: dlqSubject,
|
|
76
|
+
maxAgeMs: duration(dlqInput?.maxAgeMs, 14 * 24 * 60 * 60 * 1000, 'DLQ maxAgeMs'),
|
|
77
|
+
maxBytes: positiveInteger(dlqInput?.maxBytes, 1024 * 1024 * 1024, 'DLQ maxBytes'),
|
|
78
|
+
replicas: optionalReplicas(dlqInput?.replicas),
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
function duration(value, fallback, label) {
|
|
82
|
+
if (value === undefined || value === null)
|
|
83
|
+
return fallback;
|
|
84
|
+
if (!Number.isFinite(value) || value < 0) {
|
|
85
|
+
throw new nats_errors_1.NatsQueueConfigError(`NATS ${label} must be >= 0`);
|
|
86
|
+
}
|
|
87
|
+
return Math.round(value);
|
|
88
|
+
}
|
|
89
|
+
function positiveInteger(value, fallback, label) {
|
|
90
|
+
if (value === undefined || value === null)
|
|
91
|
+
return fallback;
|
|
92
|
+
if (!Number.isInteger(value) || value <= 0) {
|
|
93
|
+
throw new nats_errors_1.NatsQueueConfigError(`NATS ${label} must be a positive integer`);
|
|
94
|
+
}
|
|
95
|
+
return value;
|
|
96
|
+
}
|
|
97
|
+
function maxDeliverOption(value) {
|
|
98
|
+
if (value === undefined || value === null) {
|
|
99
|
+
return exports.NATS_QUEUE_DEFAULTS.maxDeliver;
|
|
100
|
+
}
|
|
101
|
+
if (value === -1)
|
|
102
|
+
return value;
|
|
103
|
+
if (!Number.isInteger(value) || value < 1) {
|
|
104
|
+
throw new nats_errors_1.NatsQueueConfigError('NATS maxDeliver must be -1 or a positive integer');
|
|
105
|
+
}
|
|
106
|
+
return value;
|
|
107
|
+
}
|
|
108
|
+
function backoffOption(value, maxDeliver) {
|
|
109
|
+
const backoff = value ? [...value] : [...exports.NATS_QUEUE_DEFAULTS.backoffMs];
|
|
110
|
+
if (backoff.length === 0) {
|
|
111
|
+
throw new nats_errors_1.NatsQueueConfigError('NATS backoffMs cannot be empty');
|
|
112
|
+
}
|
|
113
|
+
if (backoff.some((item) => !Number.isFinite(item) || item < 0)) {
|
|
114
|
+
throw new nats_errors_1.NatsQueueConfigError('NATS backoffMs must contain only non-negative numbers');
|
|
115
|
+
}
|
|
116
|
+
if (maxDeliver > 0 && backoff.length > maxDeliver) {
|
|
117
|
+
throw new nats_errors_1.NatsQueueConfigError('NATS backoffMs cannot have more entries than maxDeliver');
|
|
118
|
+
}
|
|
119
|
+
return backoff.map((item) => Math.round(item));
|
|
120
|
+
}
|
|
121
|
+
function optionalReplicas(value) {
|
|
122
|
+
if (value === undefined || value === null)
|
|
123
|
+
return undefined;
|
|
124
|
+
if (!Number.isInteger(value) || value < 1 || value > 5) {
|
|
125
|
+
throw new nats_errors_1.NatsQueueConfigError('NATS replicas must be an integer between 1 and 5');
|
|
126
|
+
}
|
|
127
|
+
return value;
|
|
128
|
+
}
|
|
129
|
+
//# sourceMappingURL=nats-queue.definition.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nats-queue.definition.js","sourceRoot":"","sources":["../../../src/lib/nats/nats-queue.definition.ts"],"names":[],"mappings":";;;AAeA,0CAiFC;AAhGD,+CAAqD;AAErD,6CAAyD;AAE5C,QAAA,mBAAmB,GAAG;IACjC,QAAQ,EAAE,CAAC,GAAG,EAAE,GAAG,EA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AM,KAAK,CAAC,EAAE,CAAC;QACzB,MAAM,IAAI,kCAAoB,CAAC,gCAAgC,CAAC,CAAC;IACnE,CAAC;IACD,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,IAAI,GAAG,CAAC,CAAC,EAAE,CAAC;QAC/D,MAAM,IAAI,kCAAoB,CAC5B,uDAAuD,CACxD,CAAC;IACJ,CAAC;IACD,IAAI,UAAU,GAAG,CAAC,IAAI,OAAO,CAAC,MAAM,GAAG,UAAU,EAAE,CAAC;QAClD,MAAM,IAAI,kCAAoB,CAC5B,yDAAyD,CAC1D,CAAC;IACJ,CAAC;IACD,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;AACjD,CAAC;AAED,SAAS,gBAAgB,CAAC,KAAc;IACtC,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,IAAI;QAAE,OAAO,SAAS,CAAC;IAC5D,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;QACvD,MAAM,IAAI,kCAAoB,CAC5B,kDAAkD,CACnD,CAAC;IACJ,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { Consumer, JsMsg, PubAck } from '@nats-io/jetstream';
|
|
2
|
+
import { NatsCoreService } from './nats-core.service';
|
|
3
|
+
import type { NatsCoreStatus, NatsEvent, NatsPublishOptions, NatsQueueConsumerStats, NatsQueueDefinition, NatsQueueNames, NormalizedNatsOptions } from './nats.types';
|
|
4
|
+
export declare class NatsQueueService {
|
|
5
|
+
private readonly core;
|
|
6
|
+
private readonly options;
|
|
7
|
+
private readonly logger;
|
|
8
|
+
private readonly streams;
|
|
9
|
+
private readonly consumers;
|
|
10
|
+
private readonly stats;
|
|
11
|
+
constructor(core: NatsCoreService, options: NormalizedNatsOptions);
|
|
12
|
+
get enabled(): boolean;
|
|
13
|
+
waitUntilConnected(timeoutMs?: number): Promise<boolean>;
|
|
14
|
+
resolveNames(queue: NatsQueueDefinition): NatsQueueNames;
|
|
15
|
+
publish<T>(queue: NatsQueueDefinition<T>, payload: T, options?: NatsPublishOptions): Promise<PubAck>;
|
|
16
|
+
publishRaw<T>(queue: NatsQueueDefinition<T>, data: Uint8Array, options?: NatsPublishOptions): Promise<PubAck>;
|
|
17
|
+
publishEvent<T>(queue: NatsQueueDefinition<NatsEvent<T>>, event: NatsEvent<T>, options?: NatsPublishOptions): Promise<PubAck>;
|
|
18
|
+
ensureStream(queue: NatsQueueDefinition): Promise<void>;
|
|
19
|
+
ensureConsumer(queue: NatsQueueDefinition): Promise<void>;
|
|
20
|
+
getConsumer(queue: NatsQueueDefinition): Promise<Consumer>;
|
|
21
|
+
invalidateConsumer(queue: NatsQueueDefinition): void;
|
|
22
|
+
publishToDlq(queue: NatsQueueDefinition, message: JsMsg, error: unknown): Promise<boolean>;
|
|
23
|
+
registerConsumer(queue: NatsQueueDefinition): NatsQueueConsumerStats;
|
|
24
|
+
recordProcessed(queue: NatsQueueDefinition): void;
|
|
25
|
+
recordFailed(queue: NatsQueueDefinition, error: unknown): void;
|
|
26
|
+
recordRetried(queue: NatsQueueDefinition, error: unknown): void;
|
|
27
|
+
recordInFlight(queue: NatsQueueDefinition, count: number): void;
|
|
28
|
+
getStats(): NatsQueueConsumerStats[];
|
|
29
|
+
getStatus(): NatsCoreStatus;
|
|
30
|
+
private publishPayload;
|
|
31
|
+
private ensureStreamByName;
|
|
32
|
+
private doEnsureStream;
|
|
33
|
+
private doEnsureConsumer;
|
|
34
|
+
private streamConfig;
|
|
35
|
+
private dlqStreamConfig;
|
|
36
|
+
private consumerConfig;
|
|
37
|
+
private streamUpdate;
|
|
38
|
+
private warnStreamDrift;
|
|
39
|
+
private consumerDrift;
|
|
40
|
+
private consumerUpdate;
|
|
41
|
+
private ensureDlqStream;
|
|
42
|
+
}
|
|
@@ -0,0 +1,480 @@
|
|
|
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 NatsQueueService_1;
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.NatsQueueService = void 0;
|
|
17
|
+
const common_1 = require("@nestjs/common");
|
|
18
|
+
const jetstream_1 = require("@nats-io/jetstream");
|
|
19
|
+
const nats_constants_1 = require("./nats.constants");
|
|
20
|
+
const nats_core_service_1 = require("./nats-core.service");
|
|
21
|
+
const nats_errors_1 = require("./nats.errors");
|
|
22
|
+
const nats_utils_1 = require("./nats.utils");
|
|
23
|
+
let NatsQueueService = NatsQueueService_1 = class NatsQueueService {
|
|
24
|
+
constructor(core, options) {
|
|
25
|
+
this.core = core;
|
|
26
|
+
this.options = options;
|
|
27
|
+
this.logger = new common_1.Logger(NatsQueueService_1.name);
|
|
28
|
+
this.streams = new Map();
|
|
29
|
+
this.consumers = new Map();
|
|
30
|
+
this.stats = new Map();
|
|
31
|
+
}
|
|
32
|
+
get enabled() {
|
|
33
|
+
return this.core.enabled;
|
|
34
|
+
}
|
|
35
|
+
waitUntilConnected(timeoutMs = 30_000) {
|
|
36
|
+
return this.core.waitUntilConnected(timeoutMs);
|
|
37
|
+
}
|
|
38
|
+
resolveNames(queue) {
|
|
39
|
+
return (0, nats_utils_1.resolveQueueNames)(queue, this.options.prefix);
|
|
40
|
+
}
|
|
41
|
+
async publish(queue, payload, options = {}) {
|
|
42
|
+
if (!this.enabled)
|
|
43
|
+
throw new nats_errors_1.NatsDisabledError();
|
|
44
|
+
const names = this.resolveNames(queue);
|
|
45
|
+
const data = (0, nats_utils_1.encodeJson)(payload);
|
|
46
|
+
if (!options.skipEnsure) {
|
|
47
|
+
await this.ensureStream(queue);
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
return await this.publishPayload(names.subject, data, queue, options);
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
if ((0, nats_utils_1.isJetStreamNotFound)(error)) {
|
|
54
|
+
this.streams.delete(names.stream);
|
|
55
|
+
}
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
async publishRaw(queue, data, options = {}) {
|
|
60
|
+
if (!this.enabled)
|
|
61
|
+
throw new nats_errors_1.NatsDisabledError();
|
|
62
|
+
const names = this.resolveNames(queue);
|
|
63
|
+
if (!options.skipEnsure) {
|
|
64
|
+
await this.ensureStream(queue);
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
return await this.publishPayload(names.subject, data, queue, options);
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
if ((0, nats_utils_1.isJetStreamNotFound)(error)) {
|
|
71
|
+
this.streams.delete(names.stream);
|
|
72
|
+
}
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
async publishEvent(queue, event, options = {}) {
|
|
77
|
+
return this.publish(queue, event, {
|
|
78
|
+
...options,
|
|
79
|
+
msgId: options.msgId ?? event.id,
|
|
80
|
+
headers: {
|
|
81
|
+
'x-event-id': event.id,
|
|
82
|
+
'x-event-type': event.type,
|
|
83
|
+
'x-event-version': String(event.version),
|
|
84
|
+
'x-producer': event.producer,
|
|
85
|
+
...(event.correlationId
|
|
86
|
+
? { 'x-correlation-id': event.correlationId }
|
|
87
|
+
: {}),
|
|
88
|
+
...(event.causationId ? { 'x-causation-id': event.causationId } : {}),
|
|
89
|
+
...options.headers,
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
async ensureStream(queue) {
|
|
94
|
+
if (!this.enabled)
|
|
95
|
+
throw new nats_errors_1.NatsDisabledError();
|
|
96
|
+
const names = this.resolveNames(queue);
|
|
97
|
+
const config = this.streamConfig(queue, names);
|
|
98
|
+
return this.ensureStreamByName(names.stream, config, this.options.provisionStreams);
|
|
99
|
+
}
|
|
100
|
+
async ensureConsumer(queue) {
|
|
101
|
+
if (!this.enabled)
|
|
102
|
+
throw new nats_errors_1.NatsDisabledError();
|
|
103
|
+
await this.ensureStream(queue);
|
|
104
|
+
const names = this.resolveNames(queue);
|
|
105
|
+
const key = `${names.stream}/${names.consumer}`;
|
|
106
|
+
const cached = this.consumers.get(key);
|
|
107
|
+
if (cached)
|
|
108
|
+
return cached;
|
|
109
|
+
const promise = this.doEnsureConsumer(queue, names).catch((error) => {
|
|
110
|
+
this.consumers.delete(key);
|
|
111
|
+
throw error;
|
|
112
|
+
});
|
|
113
|
+
this.consumers.set(key, promise);
|
|
114
|
+
return promise;
|
|
115
|
+
}
|
|
116
|
+
async getConsumer(queue) {
|
|
117
|
+
const names = this.resolveNames(queue);
|
|
118
|
+
return this.core
|
|
119
|
+
.requireJetStream()
|
|
120
|
+
.consumers.get(names.stream, names.consumer);
|
|
121
|
+
}
|
|
122
|
+
invalidateConsumer(queue) {
|
|
123
|
+
const names = this.resolveNames(queue);
|
|
124
|
+
this.consumers.delete(`${names.stream}/${names.consumer}`);
|
|
125
|
+
this.streams.delete(names.stream);
|
|
126
|
+
const dlq = (0, nats_utils_1.resolveDlqNames)(queue, this.options.prefix);
|
|
127
|
+
if (dlq)
|
|
128
|
+
this.streams.delete(dlq.stream);
|
|
129
|
+
}
|
|
130
|
+
async publishToDlq(queue, message, error) {
|
|
131
|
+
const names = (0, nats_utils_1.resolveDlqNames)(queue, this.options.prefix);
|
|
132
|
+
if (!names)
|
|
133
|
+
return false;
|
|
134
|
+
await this.ensureDlqStream(queue, names);
|
|
135
|
+
const headers = (0, nats_utils_1.toNatsHeaders)({
|
|
136
|
+
'x-dlq-source': message.subject,
|
|
137
|
+
'x-dlq-stream': message.info.stream,
|
|
138
|
+
'x-dlq-stream-sequence': String(message.info.streamSequence),
|
|
139
|
+
'x-dlq-consumer': message.info.consumer,
|
|
140
|
+
'x-dlq-deliveries': String(message.info.deliveryCount),
|
|
141
|
+
'x-dlq-error': (0, nats_utils_1.truncate)((0, nats_utils_1.errorMessage)(error), 500),
|
|
142
|
+
});
|
|
143
|
+
await this.publishPayload(names.subject, message.data, queue, {
|
|
144
|
+
msgId: `dlq-${message.info.stream}-${message.info.streamSequence}`,
|
|
145
|
+
}, headers);
|
|
146
|
+
return true;
|
|
147
|
+
}
|
|
148
|
+
registerConsumer(queue) {
|
|
149
|
+
const names = this.resolveNames(queue);
|
|
150
|
+
const key = `${names.stream}/${names.consumer}`;
|
|
151
|
+
const existing = this.stats.get(key);
|
|
152
|
+
if (existing)
|
|
153
|
+
return existing;
|
|
154
|
+
const stats = {
|
|
155
|
+
queue: queue.name,
|
|
156
|
+
stream: names.stream,
|
|
157
|
+
consumer: names.consumer,
|
|
158
|
+
startedAt: new Date().toISOString(),
|
|
159
|
+
processed: 0,
|
|
160
|
+
failed: 0,
|
|
161
|
+
retried: 0,
|
|
162
|
+
inFlight: 0,
|
|
163
|
+
};
|
|
164
|
+
this.stats.set(key, stats);
|
|
165
|
+
return stats;
|
|
166
|
+
}
|
|
167
|
+
recordProcessed(queue) {
|
|
168
|
+
const stats = this.registerConsumer(queue);
|
|
169
|
+
stats.processed += 1;
|
|
170
|
+
stats.lastError = undefined;
|
|
171
|
+
}
|
|
172
|
+
recordFailed(queue, error) {
|
|
173
|
+
const stats = this.registerConsumer(queue);
|
|
174
|
+
stats.failed += 1;
|
|
175
|
+
stats.lastError = (0, nats_utils_1.truncate)((0, nats_utils_1.errorMessage)(error), 500);
|
|
176
|
+
}
|
|
177
|
+
recordRetried(queue, error) {
|
|
178
|
+
const stats = this.registerConsumer(queue);
|
|
179
|
+
stats.retried += 1;
|
|
180
|
+
stats.lastError = (0, nats_utils_1.truncate)((0, nats_utils_1.errorMessage)(error), 500);
|
|
181
|
+
}
|
|
182
|
+
recordInFlight(queue, count) {
|
|
183
|
+
const stats = this.registerConsumer(queue);
|
|
184
|
+
stats.inFlight = Math.max(0, count);
|
|
185
|
+
}
|
|
186
|
+
getStats() {
|
|
187
|
+
return [...this.stats.values()]
|
|
188
|
+
.map((stats) => ({ ...stats }))
|
|
189
|
+
.sort((a, b) => a.queue.localeCompare(b.queue));
|
|
190
|
+
}
|
|
191
|
+
getStatus() {
|
|
192
|
+
return this.core.getStatus();
|
|
193
|
+
}
|
|
194
|
+
async publishPayload(subject, data, queue, options, preparedHeaders) {
|
|
195
|
+
if (data.byteLength > queue.maxMsgSize) {
|
|
196
|
+
throw new nats_errors_1.NatsQueueConfigError(`NATS payload for "${queue.name}" is ${data.byteLength} bytes, ` +
|
|
197
|
+
`exceeding maxMsgSize=${queue.maxMsgSize}`);
|
|
198
|
+
}
|
|
199
|
+
const js = this.core.requireJetStream();
|
|
200
|
+
const attempts = Math.max(1, options.retryAttempts ?? 3);
|
|
201
|
+
let lastError;
|
|
202
|
+
for (let attempt = 1; attempt <= attempts; attempt++) {
|
|
203
|
+
try {
|
|
204
|
+
return await js.publish(subject, data, {
|
|
205
|
+
msgID: options.msgId,
|
|
206
|
+
headers: preparedHeaders ?? (0, nats_utils_1.toNatsHeaders)(options.headers),
|
|
207
|
+
timeout: options.timeoutMs ?? this.options.requestTimeoutMs,
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
catch (error) {
|
|
211
|
+
lastError = error;
|
|
212
|
+
if (attempt >= attempts || !(0, nats_utils_1.isRetryableNatsError)(error)) {
|
|
213
|
+
throw error;
|
|
214
|
+
}
|
|
215
|
+
if (!options.msgId) {
|
|
216
|
+
this.logger.debug(`Retrying NATS publish on "${subject}" without msgID; ` +
|
|
217
|
+
'the consumer must be idempotent');
|
|
218
|
+
}
|
|
219
|
+
await (0, nats_utils_1.delayWithJitter)(250 * 2 ** (attempt - 1));
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
throw lastError;
|
|
223
|
+
}
|
|
224
|
+
async ensureStreamByName(stream, config, provision) {
|
|
225
|
+
const cached = this.streams.get(stream);
|
|
226
|
+
if (cached)
|
|
227
|
+
return cached;
|
|
228
|
+
const promise = this.doEnsureStream(stream, config, provision).catch((error) => {
|
|
229
|
+
this.streams.delete(stream);
|
|
230
|
+
throw error;
|
|
231
|
+
});
|
|
232
|
+
this.streams.set(stream, promise);
|
|
233
|
+
return promise;
|
|
234
|
+
}
|
|
235
|
+
async doEnsureStream(stream, config, provision) {
|
|
236
|
+
const jsm = this.core.requireManager();
|
|
237
|
+
const desired = config;
|
|
238
|
+
let info;
|
|
239
|
+
try {
|
|
240
|
+
info = await jsm.streams.info(stream);
|
|
241
|
+
}
|
|
242
|
+
catch (error) {
|
|
243
|
+
if (!(0, nats_utils_1.isJetStreamNotFound)(error))
|
|
244
|
+
throw error;
|
|
245
|
+
if (!provision) {
|
|
246
|
+
throw new nats_errors_1.NatsQueueConfigError(`NATS stream "${stream}" does not exist and provisioning is disabled`);
|
|
247
|
+
}
|
|
248
|
+
try {
|
|
249
|
+
info = await jsm.streams.add(config);
|
|
250
|
+
}
|
|
251
|
+
catch (addError) {
|
|
252
|
+
try {
|
|
253
|
+
info = await jsm.streams.info(stream);
|
|
254
|
+
}
|
|
255
|
+
catch {
|
|
256
|
+
throw addError;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
if (info.config.retention !== desired.retention ||
|
|
261
|
+
info.config.storage !== desired.storage) {
|
|
262
|
+
throw new nats_errors_1.NatsQueueConfigError(`NATS stream "${stream}" has incompatible retention/storage ` +
|
|
263
|
+
`(${info.config.retention}/${info.config.storage}); manual migration required`);
|
|
264
|
+
}
|
|
265
|
+
if (provision) {
|
|
266
|
+
const update = this.streamUpdate(info.config, desired);
|
|
267
|
+
if (update) {
|
|
268
|
+
await jsm.streams.update(stream, update);
|
|
269
|
+
this.logger.log(`NATS stream "${stream}" updated`);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
else {
|
|
273
|
+
this.warnStreamDrift(stream, info.config, desired);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
async doEnsureConsumer(queue, names) {
|
|
277
|
+
const jsm = this.core.requireManager();
|
|
278
|
+
const desired = this.consumerConfig(queue, names);
|
|
279
|
+
let info;
|
|
280
|
+
try {
|
|
281
|
+
info = await jsm.consumers.info(names.stream, names.consumer);
|
|
282
|
+
}
|
|
283
|
+
catch (error) {
|
|
284
|
+
if (!(0, nats_utils_1.isJetStreamNotFound)(error))
|
|
285
|
+
throw error;
|
|
286
|
+
if (!this.options.provisionConsumers) {
|
|
287
|
+
throw new nats_errors_1.NatsQueueConfigError(`NATS consumer "${names.stream}/${names.consumer}" does not exist ` +
|
|
288
|
+
'and consumer provisioning is disabled');
|
|
289
|
+
}
|
|
290
|
+
try {
|
|
291
|
+
info = await jsm.consumers.add(names.stream, desired);
|
|
292
|
+
}
|
|
293
|
+
catch (addError) {
|
|
294
|
+
try {
|
|
295
|
+
info = await jsm.consumers.info(names.stream, names.consumer);
|
|
296
|
+
}
|
|
297
|
+
catch {
|
|
298
|
+
throw addError;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
const drift = this.consumerDrift(info.config, desired);
|
|
303
|
+
if (drift.length === 0)
|
|
304
|
+
return;
|
|
305
|
+
if (!this.options.provisionConsumers) {
|
|
306
|
+
throw new nats_errors_1.NatsQueueConfigError(`NATS consumer "${names.stream}/${names.consumer}" config drift: ` +
|
|
307
|
+
drift.join(', ') +
|
|
308
|
+
' (consumer provisioning is disabled)');
|
|
309
|
+
}
|
|
310
|
+
const immutable = drift.filter((field) => ['ack_policy', 'deliver_policy', 'replay_policy'].includes(field));
|
|
311
|
+
if (immutable.length > 0) {
|
|
312
|
+
throw new nats_errors_1.NatsQueueConfigError(`NATS consumer "${names.stream}/${names.consumer}" has immutable ` +
|
|
313
|
+
`config drift (${immutable.join(', ')}); delete/recreate the consumer manually`);
|
|
314
|
+
}
|
|
315
|
+
try {
|
|
316
|
+
await jsm.consumers.update(names.stream, names.consumer, this.consumerUpdate(desired));
|
|
317
|
+
this.logger.log(`NATS consumer "${names.stream}/${names.consumer}" updated (${drift.join(', ')})`);
|
|
318
|
+
}
|
|
319
|
+
catch (error) {
|
|
320
|
+
throw new nats_errors_1.NatsQueueConfigError(`Cannot update NATS consumer "${names.stream}/${names.consumer}": ` +
|
|
321
|
+
(0, nats_utils_1.truncate)((0, nats_utils_1.errorMessage)(error), 500));
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
streamConfig(queue, names) {
|
|
325
|
+
return {
|
|
326
|
+
name: names.stream,
|
|
327
|
+
subjects: [names.subject],
|
|
328
|
+
retention: jetstream_1.RetentionPolicy.Workqueue,
|
|
329
|
+
storage: jetstream_1.StorageType.File,
|
|
330
|
+
discard: jetstream_1.DiscardPolicy.New,
|
|
331
|
+
max_age: (0, nats_utils_1.toNanos)(queue.maxAgeMs),
|
|
332
|
+
max_bytes: queue.maxBytes,
|
|
333
|
+
max_msg_size: queue.maxMsgSize,
|
|
334
|
+
duplicate_window: (0, nats_utils_1.toNanos)(queue.duplicateWindowMs),
|
|
335
|
+
num_replicas: queue.replicas ?? this.options.streamReplicas,
|
|
336
|
+
max_msgs: -1,
|
|
337
|
+
max_msgs_per_subject: -1,
|
|
338
|
+
max_consumers: -1,
|
|
339
|
+
allow_rollup_hdrs: false,
|
|
340
|
+
deny_delete: false,
|
|
341
|
+
deny_purge: false,
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
dlqStreamConfig(queue, names) {
|
|
345
|
+
const dlq = queue.dlq;
|
|
346
|
+
return {
|
|
347
|
+
name: names.stream,
|
|
348
|
+
subjects: [names.subject],
|
|
349
|
+
retention: jetstream_1.RetentionPolicy.Limits,
|
|
350
|
+
storage: jetstream_1.StorageType.File,
|
|
351
|
+
discard: jetstream_1.DiscardPolicy.Old,
|
|
352
|
+
max_age: (0, nats_utils_1.toNanos)(dlq.maxAgeMs),
|
|
353
|
+
max_bytes: dlq.maxBytes,
|
|
354
|
+
num_replicas: dlq.replicas ?? this.options.streamReplicas,
|
|
355
|
+
max_msgs: -1,
|
|
356
|
+
max_msgs_per_subject: -1,
|
|
357
|
+
max_consumers: -1,
|
|
358
|
+
allow_rollup_hdrs: false,
|
|
359
|
+
deny_delete: false,
|
|
360
|
+
deny_purge: false,
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
consumerConfig(queue, names) {
|
|
364
|
+
return {
|
|
365
|
+
durable_name: names.consumer,
|
|
366
|
+
ack_policy: jetstream_1.AckPolicy.Explicit,
|
|
367
|
+
deliver_policy: jetstream_1.DeliverPolicy.All,
|
|
368
|
+
filter_subject: names.subject,
|
|
369
|
+
max_deliver: queue.maxDeliver,
|
|
370
|
+
ack_wait: (0, nats_utils_1.toNanos)(queue.ackWaitMs),
|
|
371
|
+
backoff: queue.backoffMs.map((value) => (0, nats_utils_1.toNanos)(value, 'backoff')),
|
|
372
|
+
max_ack_pending: queue.maxAckPending,
|
|
373
|
+
max_waiting: Math.max(512, queue.concurrency * 2),
|
|
374
|
+
replay_policy: jetstream_1.ReplayPolicy.Instant,
|
|
375
|
+
description: queue.description ?? `Queue ${queue.name}`,
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
streamUpdate(current, desired) {
|
|
379
|
+
const update = {};
|
|
380
|
+
if (!sameArray(current.subjects, desired.subjects)) {
|
|
381
|
+
update.subjects = desired.subjects;
|
|
382
|
+
}
|
|
383
|
+
if (current.max_age !== desired.max_age) {
|
|
384
|
+
update.max_age = desired.max_age;
|
|
385
|
+
}
|
|
386
|
+
if (current.max_bytes !== desired.max_bytes) {
|
|
387
|
+
update.max_bytes = desired.max_bytes;
|
|
388
|
+
}
|
|
389
|
+
if (current.max_msg_size !== desired.max_msg_size) {
|
|
390
|
+
update.max_msg_size = desired.max_msg_size;
|
|
391
|
+
}
|
|
392
|
+
if (current.duplicate_window !== desired.duplicate_window) {
|
|
393
|
+
update.duplicate_window = desired.duplicate_window;
|
|
394
|
+
}
|
|
395
|
+
if (current.num_replicas !== desired.num_replicas) {
|
|
396
|
+
update.num_replicas = desired.num_replicas;
|
|
397
|
+
}
|
|
398
|
+
if (current.discard !== desired.discard) {
|
|
399
|
+
update.discard = desired.discard;
|
|
400
|
+
}
|
|
401
|
+
return Object.keys(update).length > 0 ? update : undefined;
|
|
402
|
+
}
|
|
403
|
+
warnStreamDrift(stream, current, desired) {
|
|
404
|
+
const fields = [];
|
|
405
|
+
if (!sameArray(current.subjects, desired.subjects))
|
|
406
|
+
fields.push('subjects');
|
|
407
|
+
if (current.max_age !== desired.max_age)
|
|
408
|
+
fields.push('max_age');
|
|
409
|
+
if (current.max_bytes !== desired.max_bytes)
|
|
410
|
+
fields.push('max_bytes');
|
|
411
|
+
if (current.max_msg_size !== desired.max_msg_size) {
|
|
412
|
+
fields.push('max_msg_size');
|
|
413
|
+
}
|
|
414
|
+
if (current.duplicate_window !== desired.duplicate_window) {
|
|
415
|
+
fields.push('duplicate_window');
|
|
416
|
+
}
|
|
417
|
+
if (current.num_replicas !== desired.num_replicas) {
|
|
418
|
+
fields.push('num_replicas');
|
|
419
|
+
}
|
|
420
|
+
if (current.discard !== desired.discard)
|
|
421
|
+
fields.push('discard');
|
|
422
|
+
if (fields.length > 0) {
|
|
423
|
+
this.logger.warn(`NATS stream "${stream}" drift (${fields.join(', ')}); provisioning is disabled, keeping current config`);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
consumerDrift(current, desired) {
|
|
427
|
+
const drift = [];
|
|
428
|
+
if (current.ack_policy !== desired.ack_policy)
|
|
429
|
+
drift.push('ack_policy');
|
|
430
|
+
if (current.deliver_policy !== desired.deliver_policy) {
|
|
431
|
+
drift.push('deliver_policy');
|
|
432
|
+
}
|
|
433
|
+
if (current.filter_subject !== desired.filter_subject) {
|
|
434
|
+
drift.push('filter_subject');
|
|
435
|
+
}
|
|
436
|
+
if (current.max_deliver !== desired.max_deliver) {
|
|
437
|
+
drift.push('max_deliver');
|
|
438
|
+
}
|
|
439
|
+
if (current.ack_wait !== desired.ack_wait)
|
|
440
|
+
drift.push('ack_wait');
|
|
441
|
+
if (current.max_ack_pending !== desired.max_ack_pending) {
|
|
442
|
+
drift.push('max_ack_pending');
|
|
443
|
+
}
|
|
444
|
+
if (!sameArray(current.backoff, desired.backoff)) {
|
|
445
|
+
drift.push('backoff');
|
|
446
|
+
}
|
|
447
|
+
if (current.replay_policy !== desired.replay_policy) {
|
|
448
|
+
drift.push('replay_policy');
|
|
449
|
+
}
|
|
450
|
+
return drift;
|
|
451
|
+
}
|
|
452
|
+
consumerUpdate(desired) {
|
|
453
|
+
return {
|
|
454
|
+
ack_wait: desired.ack_wait,
|
|
455
|
+
max_deliver: desired.max_deliver,
|
|
456
|
+
max_ack_pending: desired.max_ack_pending,
|
|
457
|
+
backoff: desired.backoff,
|
|
458
|
+
description: desired.description,
|
|
459
|
+
filter_subject: desired.filter_subject,
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
async ensureDlqStream(queue, names) {
|
|
463
|
+
const config = this.dlqStreamConfig(queue, names);
|
|
464
|
+
await this.ensureStreamByName(names.stream, config, this.options.provisionStreams);
|
|
465
|
+
}
|
|
466
|
+
};
|
|
467
|
+
exports.NatsQueueService = NatsQueueService;
|
|
468
|
+
exports.NatsQueueService = NatsQueueService = NatsQueueService_1 = __decorate([
|
|
469
|
+
(0, common_1.Injectable)(),
|
|
470
|
+
__param(1, (0, common_1.Inject)(nats_constants_1.NATS_OPTIONS)),
|
|
471
|
+
__metadata("design:paramtypes", [nats_core_service_1.NatsCoreService, Object])
|
|
472
|
+
], NatsQueueService);
|
|
473
|
+
function sameArray(left, right) {
|
|
474
|
+
const a = left ?? [];
|
|
475
|
+
const b = right ?? [];
|
|
476
|
+
if (a.length !== b.length)
|
|
477
|
+
return false;
|
|
478
|
+
return a.every((value) => b.includes(value));
|
|
479
|
+
}
|
|
480
|
+
//# sourceMappingURL=nats-queue.service.js.map
|