@nathapp/nestjs-queue 2.1.0-rc.1
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/CLAUDE.md +106 -0
- package/TODO.md +44 -0
- package/dist/constant/index.d.ts +1 -0
- package/dist/constant/index.js +18 -0
- package/dist/constant/index.js.map +1 -0
- package/dist/constant/queue.constant.d.ts +6 -0
- package/dist/constant/queue.constant.js +10 -0
- package/dist/constant/queue.constant.js.map +1 -0
- package/dist/decorator/index.d.ts +3 -0
- package/dist/decorator/index.js +20 -0
- package/dist/decorator/index.js.map +1 -0
- package/dist/decorator/on-queue-event.decorator.d.ts +6 -0
- package/dist/decorator/on-queue-event.decorator.js +18 -0
- package/dist/decorator/on-queue-event.decorator.js.map +1 -0
- package/dist/decorator/process.decorator.d.ts +5 -0
- package/dist/decorator/process.decorator.js +18 -0
- package/dist/decorator/process.decorator.js.map +1 -0
- package/dist/decorator/processor.decorator.d.ts +5 -0
- package/dist/decorator/processor.decorator.js +12 -0
- package/dist/decorator/processor.decorator.js.map +1 -0
- package/dist/enum/index.d.ts +3 -0
- package/dist/enum/index.js +20 -0
- package/dist/enum/index.js.map +1 -0
- package/dist/enum/job-status.enum.d.ts +8 -0
- package/dist/enum/job-status.enum.js +13 -0
- package/dist/enum/job-status.enum.js.map +1 -0
- package/dist/enum/provider.enum.d.ts +7 -0
- package/dist/enum/provider.enum.js +12 -0
- package/dist/enum/provider.enum.js.map +1 -0
- package/dist/enum/queue-event.enum.d.ts +11 -0
- package/dist/enum/queue-event.enum.js +16 -0
- package/dist/enum/queue-event.enum.js.map +1 -0
- package/dist/explorer/index.d.ts +1 -0
- package/dist/explorer/index.js +18 -0
- package/dist/explorer/index.js.map +1 -0
- package/dist/explorer/processor.explorer.d.ts +25 -0
- package/dist/explorer/processor.explorer.js +241 -0
- package/dist/explorer/processor.explorer.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/dist/interface/circuit-breaker.interface.d.ts +21 -0
- package/dist/interface/circuit-breaker.interface.js +10 -0
- package/dist/interface/circuit-breaker.interface.js.map +1 -0
- package/dist/interface/health.interface.d.ts +12 -0
- package/dist/interface/health.interface.js +3 -0
- package/dist/interface/health.interface.js.map +1 -0
- package/dist/interface/idempotency-store.interface.d.ts +14 -0
- package/dist/interface/idempotency-store.interface.js +3 -0
- package/dist/interface/idempotency-store.interface.js.map +1 -0
- package/dist/interface/index.d.ts +5 -0
- package/dist/interface/index.js +22 -0
- package/dist/interface/index.js.map +1 -0
- package/dist/interface/job.interface.d.ts +56 -0
- package/dist/interface/job.interface.js +3 -0
- package/dist/interface/job.interface.js.map +1 -0
- package/dist/interface/metrics.interface.d.ts +12 -0
- package/dist/interface/metrics.interface.js +3 -0
- package/dist/interface/metrics.interface.js.map +1 -0
- package/dist/interface/queue-options.interface.d.ts +84 -0
- package/dist/interface/queue-options.interface.js +3 -0
- package/dist/interface/queue-options.interface.js.map +1 -0
- package/dist/interface/queue-provider.interface.d.ts +55 -0
- package/dist/interface/queue-provider.interface.js +7 -0
- package/dist/interface/queue-provider.interface.js.map +1 -0
- package/dist/module/index.d.ts +3 -0
- package/dist/module/index.js +20 -0
- package/dist/module/index.js.map +1 -0
- package/dist/module/queue.module-definition.d.ts +2 -0
- package/dist/module/queue.module-definition.js +11 -0
- package/dist/module/queue.module-definition.js.map +1 -0
- package/dist/module/queue.module.d.ts +3 -0
- package/dist/module/queue.module.js +26 -0
- package/dist/module/queue.module.js.map +1 -0
- package/dist/module/queue.provider.d.ts +2 -0
- package/dist/module/queue.provider.js +57 -0
- package/dist/module/queue.provider.js.map +1 -0
- package/dist/provider/bullmq/bullmq.options.d.ts +12 -0
- package/dist/provider/bullmq/bullmq.options.js +3 -0
- package/dist/provider/bullmq/bullmq.options.js.map +1 -0
- package/dist/provider/bullmq/bullmq.provider.d.ts +52 -0
- package/dist/provider/bullmq/bullmq.provider.js +476 -0
- package/dist/provider/bullmq/bullmq.provider.js.map +1 -0
- package/dist/provider/bullmq/index.d.ts +1 -0
- package/dist/provider/bullmq/index.js +18 -0
- package/dist/provider/bullmq/index.js.map +1 -0
- package/dist/provider/index.d.ts +4 -0
- package/dist/provider/index.js +21 -0
- package/dist/provider/index.js.map +1 -0
- package/dist/provider/kafka/index.d.ts +1 -0
- package/dist/provider/kafka/index.js +18 -0
- package/dist/provider/kafka/index.js.map +1 -0
- package/dist/provider/kafka/kafka.provider.d.ts +59 -0
- package/dist/provider/kafka/kafka.provider.js +598 -0
- package/dist/provider/kafka/kafka.provider.js.map +1 -0
- package/dist/provider/rabbitmq/index.d.ts +1 -0
- package/dist/provider/rabbitmq/index.js +18 -0
- package/dist/provider/rabbitmq/index.js.map +1 -0
- package/dist/provider/rabbitmq/rabbitmq.provider.d.ts +48 -0
- package/dist/provider/rabbitmq/rabbitmq.provider.js +498 -0
- package/dist/provider/rabbitmq/rabbitmq.provider.js.map +1 -0
- package/dist/provider/redis-pubsub/index.d.ts +1 -0
- package/dist/provider/redis-pubsub/index.js +18 -0
- package/dist/provider/redis-pubsub/index.js.map +1 -0
- package/dist/provider/redis-pubsub/redis-pubsub.provider.d.ts +40 -0
- package/dist/provider/redis-pubsub/redis-pubsub.provider.js +226 -0
- package/dist/provider/redis-pubsub/redis-pubsub.provider.js.map +1 -0
- package/dist/service/index.d.ts +1 -0
- package/dist/service/index.js +18 -0
- package/dist/service/index.js.map +1 -0
- package/dist/service/queue.service.d.ts +36 -0
- package/dist/service/queue.service.js +89 -0
- package/dist/service/queue.service.js.map +1 -0
- package/dist/store/in-memory-idempotency.store.d.ts +15 -0
- package/dist/store/in-memory-idempotency.store.js +84 -0
- package/dist/store/in-memory-idempotency.store.js.map +1 -0
- package/dist/store/index.d.ts +1 -0
- package/dist/store/index.js +6 -0
- package/dist/store/index.js.map +1 -0
- package/dist/utils/circuit-breaker.d.ts +30 -0
- package/dist/utils/circuit-breaker.js +133 -0
- package/dist/utils/circuit-breaker.js.map +1 -0
- package/dist/utils/delayed-scheduler.d.ts +16 -0
- package/dist/utils/delayed-scheduler.js +100 -0
- package/dist/utils/delayed-scheduler.js.map +1 -0
- package/dist/utils/errors.d.ts +23 -0
- package/dist/utils/errors.js +85 -0
- package/dist/utils/errors.js.map +1 -0
- package/dist/utils/index.d.ts +5 -0
- package/dist/utils/index.js +22 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/lock-renewal.d.ts +14 -0
- package/dist/utils/lock-renewal.js +61 -0
- package/dist/utils/lock-renewal.js.map +1 -0
- package/dist/utils/redis-idempotency-store.d.ts +21 -0
- package/dist/utils/redis-idempotency-store.js +88 -0
- package/dist/utils/redis-idempotency-store.js.map +1 -0
- package/dist/utils/sanitize.d.ts +3 -0
- package/dist/utils/sanitize.js +55 -0
- package/dist/utils/sanitize.js.map +1 -0
- package/dist/utils/validation.d.ts +8 -0
- package/dist/utils/validation.js +60 -0
- package/dist/utils/validation.js.map +1 -0
- package/docker-compose.e2e.yml +69 -0
- package/docs/FINAL_CODE_REVIEW.md +328 -0
- package/docs/IMPLEMENTATION_PLAN.md +748 -0
- package/docs/archive/CIRCUIT_BREAKER_PLAN.md +35 -0
- package/docs/archive/CODE_REVIEW.md +596 -0
- package/docs/archive/CODE_REVIEW_V2.md +1081 -0
- package/docs/archive/CODE_REVIEW_V3.md +402 -0
- package/docs/archive/IMPLEMENTATION_PLAN.md +505 -0
- package/docs/archive/IMPLEMENTATION_PLAN_BUGFIX.md +428 -0
- package/package.json +70 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sanitizeErrorMessage = sanitizeErrorMessage;
|
|
4
|
+
exports.sanitizeError = sanitizeError;
|
|
5
|
+
exports.sanitizeConnectionOptions = sanitizeConnectionOptions;
|
|
6
|
+
const SENSITIVE_PATTERNS = [
|
|
7
|
+
/password[=:]\s*['"]?[^\s'"]+['"]?/gi,
|
|
8
|
+
/secret[=:]\s*['"]?[^\s'"]+['"]?/gi,
|
|
9
|
+
/token[=:]\s*['"]?[^\s'"]+['"]?/gi,
|
|
10
|
+
/api[_-]?key[=:]\s*['"]?[^\s'"]+['"]?/gi,
|
|
11
|
+
/auth[=:]\s*['"]?[^\s'"]+['"]?/gi,
|
|
12
|
+
/credential[s]?[=:]\s*['"]?[^\s'"]+['"]?/gi,
|
|
13
|
+
/bearer\s+[a-zA-Z0-9._-]+/gi,
|
|
14
|
+
/basic\s+[a-zA-Z0-9+/=]+/gi,
|
|
15
|
+
];
|
|
16
|
+
const REDACTED = '***REDACTED***';
|
|
17
|
+
function sanitizeErrorMessage(message) {
|
|
18
|
+
if (!message || typeof message !== 'string') {
|
|
19
|
+
return message;
|
|
20
|
+
}
|
|
21
|
+
let sanitized = message;
|
|
22
|
+
for (const pattern of SENSITIVE_PATTERNS) {
|
|
23
|
+
sanitized = sanitized.replace(pattern, (match) => {
|
|
24
|
+
const colonIndex = match.indexOf(':');
|
|
25
|
+
const equalsIndex = match.indexOf('=');
|
|
26
|
+
const separatorIndex = colonIndex >= 0 ? colonIndex : equalsIndex;
|
|
27
|
+
if (separatorIndex >= 0) {
|
|
28
|
+
const key = match.substring(0, separatorIndex + 1);
|
|
29
|
+
return `${key}${REDACTED}`;
|
|
30
|
+
}
|
|
31
|
+
return REDACTED;
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
return sanitized;
|
|
35
|
+
}
|
|
36
|
+
function sanitizeError(error) {
|
|
37
|
+
const sanitizedMessage = sanitizeErrorMessage(error.message);
|
|
38
|
+
const sanitizedError = new Error(sanitizedMessage);
|
|
39
|
+
sanitizedError.name = error.name;
|
|
40
|
+
if (error.stack) {
|
|
41
|
+
sanitizedError.stack = sanitizeErrorMessage(error.stack);
|
|
42
|
+
}
|
|
43
|
+
return sanitizedError;
|
|
44
|
+
}
|
|
45
|
+
function sanitizeConnectionOptions(options) {
|
|
46
|
+
const sensitiveFields = ['password', 'secret', 'token', 'apiKey', 'auth', 'credentials'];
|
|
47
|
+
const sanitized = { ...options };
|
|
48
|
+
for (const field of sensitiveFields) {
|
|
49
|
+
if (field in sanitized) {
|
|
50
|
+
sanitized[field] = REDACTED;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return sanitized;
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=sanitize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sanitize.js","sourceRoot":"","sources":["../../lib/utils/sanitize.ts"],"names":[],"mappings":";;AAwBA,oDAyBC;AAOD,sCAWC;AAOD,8DAWC;AAlFD,MAAM,kBAAkB,GAAa;IACnC,qCAAqC;IACrC,mCAAmC;IACnC,kCAAkC;IAClC,wCAAwC;IACxC,iCAAiC;IACjC,2CAA2C;IAC3C,4BAA4B;IAC5B,2BAA2B;CAC5B,CAAC;AAKF,MAAM,QAAQ,GAAG,gBAAgB,CAAC;AAOlC,SAAgB,oBAAoB,CAAC,OAAe;IAClD,IAAI,CAAC,OAAO,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;QAC5C,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,IAAI,SAAS,GAAG,OAAO,CAAC;IAExB,KAAK,MAAM,OAAO,IAAI,kBAAkB,EAAE,CAAC;QACzC,SAAS,GAAG,SAAS,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,EAAE;YAE/C,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;YACtC,MAAM,WAAW,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;YACvC,MAAM,cAAc,GAAG,UAAU,IAAI,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,WAAW,CAAC;YAElE,IAAI,cAAc,IAAI,CAAC,EAAE,CAAC;gBACxB,MAAM,GAAG,GAAG,KAAK,CAAC,SAAS,CAAC,CAAC,EAAE,cAAc,GAAG,CAAC,CAAC,CAAC;gBACnD,OAAO,GAAG,GAAG,GAAG,QAAQ,EAAE,CAAC;YAC7B,CAAC;YAGD,OAAO,QAAQ,CAAC;QAClB,CAAC,CAAC,CAAC;IACL,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC;AAOD,SAAgB,aAAa,CAAC,KAAY;IACxC,MAAM,gBAAgB,GAAG,oBAAoB,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IAC7D,MAAM,cAAc,GAAG,IAAI,KAAK,CAAC,gBAAgB,CAAC,CAAC;IACnD,cAAc,CAAC,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC;IAGjC,IAAI,KAAK,CAAC,KAAK,EAAE,CAAC;QAChB,cAAc,CAAC,KAAK,GAAG,oBAAoB,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IAC3D,CAAC;IAED,OAAO,cAAc,CAAC;AACxB,CAAC;AAOD,SAAgB,yBAAyB,CAAoC,OAAU;IACrF,MAAM,eAAe,GAAG,CAAC,UAAU,EAAE,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,aAAa,CAAC,CAAC;IACzF,MAAM,SAAS,GAAG,EAAE,GAAG,OAAO,EAAE,CAAC;IAEjC,KAAK,MAAM,KAAK,IAAI,eAAe,EAAE,CAAC;QACpC,IAAI,KAAK,IAAI,SAAS,EAAE,CAAC;YACtB,SAAqC,CAAC,KAAK,CAAC,GAAG,QAAQ,CAAC;QAC3D,CAAC;IACH,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export interface ValidationOptions {
|
|
2
|
+
maxPayloadSize?: number;
|
|
3
|
+
maxJobIdLength?: number;
|
|
4
|
+
allowedJobIdPattern?: RegExp;
|
|
5
|
+
}
|
|
6
|
+
export declare function validateJobId(id: string, options?: ValidationOptions): void;
|
|
7
|
+
export declare function validateJobData<T>(data: T, options?: ValidationOptions): void;
|
|
8
|
+
export declare function validateJob<T>(id: string, data: T, options?: ValidationOptions): void;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateJobId = validateJobId;
|
|
4
|
+
exports.validateJobData = validateJobData;
|
|
5
|
+
exports.validateJob = validateJob;
|
|
6
|
+
const common_1 = require("@nestjs/common");
|
|
7
|
+
const logger = new common_1.Logger('QueueValidation');
|
|
8
|
+
const DEFAULT_OPTIONS = {
|
|
9
|
+
maxPayloadSize: 1024 * 1024,
|
|
10
|
+
maxJobIdLength: 256,
|
|
11
|
+
allowedJobIdPattern: /^[a-zA-Z0-9_-]+$/,
|
|
12
|
+
};
|
|
13
|
+
const DANGEROUS_KEYS = ['__proto__', 'constructor', 'prototype'];
|
|
14
|
+
function validateJobId(id, options = DEFAULT_OPTIONS) {
|
|
15
|
+
if (!id || typeof id !== 'string') {
|
|
16
|
+
throw new Error('Job ID must be a non-empty string');
|
|
17
|
+
}
|
|
18
|
+
const maxLength = options.maxJobIdLength || DEFAULT_OPTIONS.maxJobIdLength;
|
|
19
|
+
if (id.length > maxLength) {
|
|
20
|
+
throw new Error(`Job ID exceeds maximum length of ${maxLength} characters`);
|
|
21
|
+
}
|
|
22
|
+
const pattern = options.allowedJobIdPattern || DEFAULT_OPTIONS.allowedJobIdPattern;
|
|
23
|
+
if (!pattern.test(id)) {
|
|
24
|
+
throw new Error('Job ID contains invalid characters. Only alphanumeric, underscore, and hyphen are allowed.');
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function validateJobData(data, options = DEFAULT_OPTIONS) {
|
|
28
|
+
const serialized = JSON.stringify(data);
|
|
29
|
+
const maxSize = options.maxPayloadSize || DEFAULT_OPTIONS.maxPayloadSize;
|
|
30
|
+
if (serialized.length > maxSize) {
|
|
31
|
+
throw new Error(`Job payload exceeds maximum size of ${maxSize} bytes (actual: ${serialized.length} bytes)`);
|
|
32
|
+
}
|
|
33
|
+
checkForDangerousKeys(data, '');
|
|
34
|
+
}
|
|
35
|
+
function checkForDangerousKeys(obj, path) {
|
|
36
|
+
if (obj === null || obj === undefined) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (typeof obj !== 'object') {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (Array.isArray(obj)) {
|
|
43
|
+
obj.forEach((item, index) => {
|
|
44
|
+
checkForDangerousKeys(item, `${path}[${index}]`);
|
|
45
|
+
});
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
for (const key of Object.keys(obj)) {
|
|
49
|
+
if (DANGEROUS_KEYS.includes(key)) {
|
|
50
|
+
const fullPath = path ? `${path}.${key}` : key;
|
|
51
|
+
throw new Error(`Dangerous key "${key}" found in job data at path: ${fullPath}`);
|
|
52
|
+
}
|
|
53
|
+
checkForDangerousKeys(obj[key], path ? `${path}.${key}` : key);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function validateJob(id, data, options = DEFAULT_OPTIONS) {
|
|
57
|
+
validateJobId(id, options);
|
|
58
|
+
validateJobData(data, options);
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=validation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validation.js","sourceRoot":"","sources":["../../lib/utils/validation.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
services:
|
|
2
|
+
redis:
|
|
3
|
+
image: redis:7-alpine
|
|
4
|
+
ports:
|
|
5
|
+
- '6379:6379'
|
|
6
|
+
healthcheck:
|
|
7
|
+
test: ['CMD', 'redis-cli', 'ping']
|
|
8
|
+
interval: 5s
|
|
9
|
+
timeout: 3s
|
|
10
|
+
retries: 5
|
|
11
|
+
networks:
|
|
12
|
+
- queue-e2e
|
|
13
|
+
|
|
14
|
+
zookeeper:
|
|
15
|
+
image: confluentinc/cp-zookeeper:7.5.0
|
|
16
|
+
environment:
|
|
17
|
+
ZOOKEEPER_CLIENT_PORT: 2181
|
|
18
|
+
ZOOKEEPER_TICK_TIME: 2000
|
|
19
|
+
healthcheck:
|
|
20
|
+
test: ['CMD', 'nc', '-z', 'localhost', '2181']
|
|
21
|
+
interval: 10s
|
|
22
|
+
timeout: 5s
|
|
23
|
+
retries: 5
|
|
24
|
+
networks:
|
|
25
|
+
- queue-e2e
|
|
26
|
+
|
|
27
|
+
kafka:
|
|
28
|
+
image: confluentinc/cp-kafka:7.5.0
|
|
29
|
+
ports:
|
|
30
|
+
- '9092:9092'
|
|
31
|
+
depends_on:
|
|
32
|
+
zookeeper:
|
|
33
|
+
condition: service_healthy
|
|
34
|
+
environment:
|
|
35
|
+
KAFKA_BROKER_ID: 1
|
|
36
|
+
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
|
|
37
|
+
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
|
|
38
|
+
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
|
|
39
|
+
KAFKA_AUTO_CREATE_TOPICS_ENABLE: 'true'
|
|
40
|
+
KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
|
|
41
|
+
healthcheck:
|
|
42
|
+
test: ['CMD', 'kafka-broker-api-versions', '--bootstrap-server', 'localhost:9092']
|
|
43
|
+
interval: 5s
|
|
44
|
+
timeout: 10s
|
|
45
|
+
retries: 10
|
|
46
|
+
start_period: 20s
|
|
47
|
+
networks:
|
|
48
|
+
- queue-e2e
|
|
49
|
+
|
|
50
|
+
rabbitmq:
|
|
51
|
+
image: rabbitmq:3-management-alpine
|
|
52
|
+
ports:
|
|
53
|
+
- '5672:5672'
|
|
54
|
+
- '15672:15672'
|
|
55
|
+
environment:
|
|
56
|
+
RABBITMQ_DEFAULT_USER: guest
|
|
57
|
+
RABBITMQ_DEFAULT_PASS: guest
|
|
58
|
+
healthcheck:
|
|
59
|
+
test: ['CMD', 'rabbitmq-diagnostics', 'check_port_connectivity']
|
|
60
|
+
interval: 5s
|
|
61
|
+
timeout: 5s
|
|
62
|
+
retries: 10
|
|
63
|
+
start_period: 15s
|
|
64
|
+
networks:
|
|
65
|
+
- queue-e2e
|
|
66
|
+
|
|
67
|
+
networks:
|
|
68
|
+
queue-e2e:
|
|
69
|
+
driver: bridge
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
# Final Code Review: @nathapp/nestjs-queue
|
|
2
|
+
|
|
3
|
+
**Date**: 2026-02-09
|
|
4
|
+
**Reviewer**: Subrina (AI)
|
|
5
|
+
**Scope**: Memory leaks, race conditions, duplicate processing, job loss on restart, security
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## 1. Memory Leak Analysis
|
|
10
|
+
|
|
11
|
+
### ✅ Fixed Issues
|
|
12
|
+
|
|
13
|
+
| Component | Issue | Status |
|
|
14
|
+
|-----------|-------|--------|
|
|
15
|
+
| BullMQProvider.listenerRegistry | Was not cleared in `close()` | ✅ Fixed |
|
|
16
|
+
| BullMQProvider.isHealthy() | Was creating persistent `__health__` queue | ✅ Fixed (uses existing queue or temp client) |
|
|
17
|
+
| KafkaQueueProvider.verifiedTopics | Was not cleared in `close()` | ✅ Fixed |
|
|
18
|
+
| KafkaQueueProvider.processedDelayedJobs | Unbounded growth potential | ✅ Fixed (TTL cleanup every 5 min) |
|
|
19
|
+
| ProcessorExplorer event listeners | Not removed on destroy | ✅ Fixed (calls `removeAllListeners` in `onModuleDestroy`) |
|
|
20
|
+
|
|
21
|
+
### ⚠️ Remaining Concerns
|
|
22
|
+
|
|
23
|
+
#### 1.1 BullMQ QueueEvents Connections (Low Risk)
|
|
24
|
+
```typescript
|
|
25
|
+
// bullmq.provider.ts
|
|
26
|
+
private getQueueEvents(queueName: string): QueueEvents {
|
|
27
|
+
if (!this.queueEvents.has(queueName)) {
|
|
28
|
+
const events = new QueueEvents(queueName, {...});
|
|
29
|
+
this.queueEvents.set(queueName, events);
|
|
30
|
+
}
|
|
31
|
+
return this.queueEvents.get(queueName)!;
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
**Issue**: QueueEvents are created on-demand but may accumulate if many unique queue names are used dynamically.
|
|
35
|
+
**Recommendation**: Add queue name validation or maximum cache size limit.
|
|
36
|
+
**Risk**: Low - queues are typically defined at compile time.
|
|
37
|
+
|
|
38
|
+
#### 1.2 RabbitMQ Channel Accumulation (Low Risk)
|
|
39
|
+
```typescript
|
|
40
|
+
// rabbitmq.provider.ts - channels Map
|
|
41
|
+
private readonly channels: Map<string, ChannelWrapper> = new Map();
|
|
42
|
+
```
|
|
43
|
+
**Issue**: Same as above - channels accumulate per queue.
|
|
44
|
+
**Risk**: Low - same reasoning.
|
|
45
|
+
|
|
46
|
+
#### 1.3 Kafka Delayed Job Scheduler (Medium Risk)
|
|
47
|
+
```typescript
|
|
48
|
+
// DelayedJobScheduler internal Map
|
|
49
|
+
```
|
|
50
|
+
**Issue**: If scheduler fails to execute jobs due to shutdown race, entries may persist.
|
|
51
|
+
**Recommendation**: Review `DelayedJobScheduler.shutdown()` for edge cases.
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
|
|
55
|
+
## 2. Race Condition Analysis
|
|
56
|
+
|
|
57
|
+
### ✅ Fixed Issues
|
|
58
|
+
|
|
59
|
+
| Component | Issue | Status |
|
|
60
|
+
|-----------|-------|--------|
|
|
61
|
+
| ProcessorExplorer | Multiple workers per queue causing job competition | ✅ Fixed (single router worker) |
|
|
62
|
+
| BullMQ process() | Duplicate worker registration | ✅ Fixed (throws WORKER_EXISTS error) |
|
|
63
|
+
|
|
64
|
+
### ⚠️ Remaining Concerns
|
|
65
|
+
|
|
66
|
+
#### 2.1 Idempotency Store Race Window (Medium Risk)
|
|
67
|
+
```typescript
|
|
68
|
+
// All providers use this pattern:
|
|
69
|
+
const canProcess = await idempotencyStore.markProcessing(job.id, lockTtl);
|
|
70
|
+
if (!canProcess) {
|
|
71
|
+
// Skip
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
// ... process ...
|
|
75
|
+
await idempotencyStore.markComplete(job.id, completedTtl);
|
|
76
|
+
```
|
|
77
|
+
**Issue**: Between `markProcessing` and `markComplete`, if pod crashes, lock expires, and another pod picks up the job → potential duplicate processing after lock TTL expires.
|
|
78
|
+
**Mitigation**:
|
|
79
|
+
- Use shorter `lockTtl` + extend lock during processing
|
|
80
|
+
- Implement lock renewal heartbeat for long-running jobs
|
|
81
|
+
|
|
82
|
+
#### 2.2 RabbitMQ Retry Race (Low Risk)
|
|
83
|
+
```typescript
|
|
84
|
+
// rabbitmq.provider.ts retry logic
|
|
85
|
+
await this.add(queueName, job.name, job.data, retryJobOpts);
|
|
86
|
+
channel.ack(msg); // Ack AFTER successful re-add
|
|
87
|
+
```
|
|
88
|
+
**Status**: ✅ Correctly handles this - only acks after successful retry add.
|
|
89
|
+
**Edge case**: If connection drops between `add` and `ack`, job is duplicated. Mitigated by idempotency store if configured.
|
|
90
|
+
|
|
91
|
+
#### 2.3 Kafka Consumer Rebalancing (Medium Risk)
|
|
92
|
+
```typescript
|
|
93
|
+
// kafka.provider.ts
|
|
94
|
+
partitionsConsumedConcurrently: options?.concurrency || 1,
|
|
95
|
+
```
|
|
96
|
+
**Issue**: During partition rebalancing, in-flight jobs may be reprocessed by new partition owner.
|
|
97
|
+
**Mitigation**:
|
|
98
|
+
- Idempotency store handles this
|
|
99
|
+
- Consider setting `autoCommit: false` and manual offset management
|
|
100
|
+
|
|
101
|
+
---
|
|
102
|
+
|
|
103
|
+
## 3. Duplicate Job Processing Analysis
|
|
104
|
+
|
|
105
|
+
### ✅ Protection Mechanisms In Place
|
|
106
|
+
|
|
107
|
+
| Mechanism | Provider | Implementation |
|
|
108
|
+
|-----------|----------|----------------|
|
|
109
|
+
| Idempotency Store | All | `markProcessing()` / `markComplete()` pattern |
|
|
110
|
+
| Job ID Tracking | BullMQ | Native support via Redis |
|
|
111
|
+
| Consumer Groups | Kafka | Partition-based distribution |
|
|
112
|
+
| Prefetch | RabbitMQ | `prefetchCount` limits concurrent messages |
|
|
113
|
+
|
|
114
|
+
### ⚠️ Gaps
|
|
115
|
+
|
|
116
|
+
#### 3.1 Idempotency Store Not Required (High Risk for Production)
|
|
117
|
+
```typescript
|
|
118
|
+
// Options are optional
|
|
119
|
+
idempotency?: {
|
|
120
|
+
store?: IdempotencyStore;
|
|
121
|
+
lockTtl?: number;
|
|
122
|
+
completedTtl?: number;
|
|
123
|
+
};
|
|
124
|
+
```
|
|
125
|
+
**Issue**: Without idempotency store, all providers are vulnerable to duplicates during failures.
|
|
126
|
+
**Recommendation**: Make idempotency store required for production, or at minimum log warning if not configured.
|
|
127
|
+
|
|
128
|
+
#### 3.2 Kafka Delayed Job Deduplication (Fixed)
|
|
129
|
+
```typescript
|
|
130
|
+
// kafka.provider.ts
|
|
131
|
+
const delayKey = `${queueName}-${partition}-${message.offset}`;
|
|
132
|
+
if (this.processedDelayedJobs.has(delayKey)) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
**Status**: ✅ Fixed - uses partition+offset as dedup key.
|
|
137
|
+
|
|
138
|
+
#### 3.3 RabbitMQ Retry Job ID (Potential Issue)
|
|
139
|
+
```typescript
|
|
140
|
+
// rabbitmq.provider.ts
|
|
141
|
+
jobId: `${job.id}-retry-${job.attemptsMade}`, // New ID to avoid conflicts
|
|
142
|
+
```
|
|
143
|
+
**Issue**: Changes job ID on retry, which may break idempotency tracking if store uses job ID as key.
|
|
144
|
+
**Recommendation**: Store should use original job ID (x-original-job-id header) for deduplication.
|
|
145
|
+
|
|
146
|
+
---
|
|
147
|
+
|
|
148
|
+
## 4. Job Loss on Pod/App Restart Analysis
|
|
149
|
+
|
|
150
|
+
### ✅ Protection Mechanisms
|
|
151
|
+
|
|
152
|
+
| Provider | Mechanism | Status |
|
|
153
|
+
|----------|-----------|--------|
|
|
154
|
+
| BullMQ | Redis persistence | ✅ Jobs survive pod restart |
|
|
155
|
+
| Kafka | Topic retention | ✅ Messages persist, consumer resumes from committed offset |
|
|
156
|
+
| RabbitMQ | Durable queues + persistent messages | ✅ Jobs survive broker restart |
|
|
157
|
+
|
|
158
|
+
### ⚠️ Job Loss Scenarios
|
|
159
|
+
|
|
160
|
+
#### 4.1 In-Memory Delayed Jobs (High Risk)
|
|
161
|
+
```typescript
|
|
162
|
+
// DelayedJobScheduler - all providers
|
|
163
|
+
private readonly pendingJobs: Map<string, NodeJS.Timeout> = new Map();
|
|
164
|
+
```
|
|
165
|
+
**Issue**: Delayed jobs scheduled locally (not in broker) are lost on pod restart.
|
|
166
|
+
**Affected Providers**: Kafka, RabbitMQ (when not using delayed message plugin)
|
|
167
|
+
**Mitigation Options**:
|
|
168
|
+
1. Use BullMQ for delayed jobs (native Redis-based delay)
|
|
169
|
+
2. RabbitMQ: Enable `rabbitmq_delayed_message_exchange` plugin
|
|
170
|
+
3. Implement persistent delay queue (store in Redis/DB with polling)
|
|
171
|
+
|
|
172
|
+
#### 4.2 Graceful Shutdown Window (Medium Risk)
|
|
173
|
+
```typescript
|
|
174
|
+
// All providers close() method
|
|
175
|
+
const timeout = options.timeout ?? 30000;
|
|
176
|
+
```
|
|
177
|
+
**Issue**: If shutdown takes longer than pod termination grace period, in-flight jobs may be lost.
|
|
178
|
+
**Kubernetes Recommendation**:
|
|
179
|
+
```yaml
|
|
180
|
+
spec:
|
|
181
|
+
terminationGracePeriodSeconds: 60 # Match or exceed shutdown timeout
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
#### 4.3 DelayedJobScheduler Shutdown Behavior
|
|
185
|
+
```typescript
|
|
186
|
+
// delayed-scheduler.ts
|
|
187
|
+
async shutdown(options: { executeImmediately?: boolean } = {}): Promise<void> {
|
|
188
|
+
if (options.executeImmediately) {
|
|
189
|
+
// Execute pending jobs before shutdown
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
```
|
|
193
|
+
**Status**: ✅ Good - supports immediate execution on shutdown.
|
|
194
|
+
**Risk**: Jobs may still be lost if `executeImmediately: false` or if execution fails.
|
|
195
|
+
|
|
196
|
+
#### 4.4 Circuit Breaker State Loss (Low Risk)
|
|
197
|
+
```typescript
|
|
198
|
+
// Circuit breaker state is in-memory
|
|
199
|
+
private state: CircuitBreakerState = CircuitBreakerState.CLOSED;
|
|
200
|
+
```
|
|
201
|
+
**Issue**: Circuit breaker state resets on pod restart.
|
|
202
|
+
**Impact**: May cause brief surge of requests to recovering backend before circuit re-opens.
|
|
203
|
+
**Recommendation**: For critical circuits, persist state in Redis.
|
|
204
|
+
|
|
205
|
+
---
|
|
206
|
+
|
|
207
|
+
## 5. Security Analysis
|
|
208
|
+
|
|
209
|
+
### ✅ Fixed Issues
|
|
210
|
+
|
|
211
|
+
| Issue | Status |
|
|
212
|
+
|-------|--------|
|
|
213
|
+
| CorrelationId injection | ✅ Fixed - `sanitizeCorrelationId()` validates format |
|
|
214
|
+
| Job structure validation | ✅ Fixed - `validateJobStructure()` in Kafka/RabbitMQ |
|
|
215
|
+
|
|
216
|
+
### ⚠️ Remaining Concerns
|
|
217
|
+
|
|
218
|
+
#### 5.1 JSON Parsing Without Schema Validation (Medium Risk)
|
|
219
|
+
```typescript
|
|
220
|
+
// All providers
|
|
221
|
+
const parsed = JSON.parse(message.value.toString());
|
|
222
|
+
validateJobStructure(parsed); // Only validates basic structure
|
|
223
|
+
job = parsed;
|
|
224
|
+
```
|
|
225
|
+
**Issue**: `validateJobStructure` only checks for `id`, `name`, `data` fields. Malicious payloads in `data` could cause issues.
|
|
226
|
+
**Recommendation**:
|
|
227
|
+
1. Add `data` schema validation option (e.g., Zod, class-validator)
|
|
228
|
+
2. Sanitize/escape data before use in logs or error messages
|
|
229
|
+
|
|
230
|
+
#### 5.2 Error Message Exposure (Low Risk)
|
|
231
|
+
```typescript
|
|
232
|
+
// Errors may expose internal details
|
|
233
|
+
job.failedReason = error.message;
|
|
234
|
+
await this.sendToDLQ(queueName, job, error); // Includes stack trace
|
|
235
|
+
```
|
|
236
|
+
**Issue**: Stack traces in DLQ may expose internal paths/structure.
|
|
237
|
+
**Recommendation**: Sanitize error details before writing to DLQ.
|
|
238
|
+
|
|
239
|
+
#### 5.3 Redis Connection Security (Configuration)
|
|
240
|
+
```typescript
|
|
241
|
+
// bullmq.provider.ts
|
|
242
|
+
this.connection = {
|
|
243
|
+
password: options.connection.password, // Plaintext in config
|
|
244
|
+
};
|
|
245
|
+
```
|
|
246
|
+
**Recommendation**: Use environment variables or secrets management for credentials.
|
|
247
|
+
|
|
248
|
+
#### 5.4 Kafka SASL Configuration (Configuration)
|
|
249
|
+
```typescript
|
|
250
|
+
sasl: options.sasl as any, // Type assertion
|
|
251
|
+
```
|
|
252
|
+
**Recommendation**: Validate SASL configuration properly, avoid `as any`.
|
|
253
|
+
|
|
254
|
+
#### 5.5 RabbitMQ URL Credentials (Configuration)
|
|
255
|
+
```typescript
|
|
256
|
+
connect(options.urls, {...}); // URLs may contain credentials
|
|
257
|
+
```
|
|
258
|
+
**Recommendation**: Use `amqplib` connection object format instead of URL strings in production.
|
|
259
|
+
|
|
260
|
+
---
|
|
261
|
+
|
|
262
|
+
## 6. Summary
|
|
263
|
+
|
|
264
|
+
### Critical Items (Prioritize)
|
|
265
|
+
|
|
266
|
+
| Priority | Issue | Recommendation |
|
|
267
|
+
|----------|-------|----------------|
|
|
268
|
+
| P1 | In-memory delayed jobs lost on restart | Use broker-native delays or persistent delay store |
|
|
269
|
+
| P1 | Idempotency store optional | Require for production or add warning |
|
|
270
|
+
| P2 | Lock TTL expiry allows duplicates | Implement lock renewal for long jobs |
|
|
271
|
+
| P2 | RabbitMQ retry changes job ID | Use original job ID for idempotency |
|
|
272
|
+
|
|
273
|
+
### Monitoring Recommendations
|
|
274
|
+
|
|
275
|
+
```typescript
|
|
276
|
+
// Metrics to track
|
|
277
|
+
interface QueueMonitoring {
|
|
278
|
+
// Memory
|
|
279
|
+
activeQueues: number;
|
|
280
|
+
activeWorkers: number;
|
|
281
|
+
pendingDelayedJobs: number;
|
|
282
|
+
circuitBreakerStates: Record<string, CircuitBreakerState>;
|
|
283
|
+
|
|
284
|
+
// Processing
|
|
285
|
+
jobsProcessed: Counter;
|
|
286
|
+
jobsFailed: Counter;
|
|
287
|
+
jobsRetried: Counter;
|
|
288
|
+
duplicateJobsSkipped: Counter;
|
|
289
|
+
|
|
290
|
+
// Latency
|
|
291
|
+
jobProcessingDuration: Histogram;
|
|
292
|
+
idempotencyLockDuration: Histogram;
|
|
293
|
+
}
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
### Kubernetes Configuration
|
|
297
|
+
|
|
298
|
+
```yaml
|
|
299
|
+
# Recommended pod spec for queue workers
|
|
300
|
+
spec:
|
|
301
|
+
terminationGracePeriodSeconds: 60
|
|
302
|
+
containers:
|
|
303
|
+
- name: worker
|
|
304
|
+
lifecycle:
|
|
305
|
+
preStop:
|
|
306
|
+
exec:
|
|
307
|
+
command: ["/bin/sh", "-c", "sleep 5"] # Allow graceful shutdown signal
|
|
308
|
+
resources:
|
|
309
|
+
requests:
|
|
310
|
+
memory: "256Mi" # Account for in-flight job memory
|
|
311
|
+
limits:
|
|
312
|
+
memory: "512Mi"
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
---
|
|
316
|
+
|
|
317
|
+
## 7. Test Coverage Gaps
|
|
318
|
+
|
|
319
|
+
| Area | Current | Recommended |
|
|
320
|
+
|------|---------|-------------|
|
|
321
|
+
| Pod restart simulation | ❌ None | Add integration test with delayed jobs |
|
|
322
|
+
| Idempotency under load | ❌ None | Add concurrent duplicate job test |
|
|
323
|
+
| Network partition handling | ❌ None | Add chaos testing for reconnect |
|
|
324
|
+
| Long-running job + shutdown | ❌ None | Add timeout boundary test |
|
|
325
|
+
|
|
326
|
+
---
|
|
327
|
+
|
|
328
|
+
*Review complete. All findings should be triaged based on production requirements.*
|