@nest-omni/core 3.1.1-9 → 3.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/.hygen/new/crud/controller.ejs.t +82 -0
- package/.hygen/new/crud/create.dto.ejs.t +12 -0
- package/.hygen/new/crud/entity.ejs.t +26 -0
- package/.hygen/new/crud/get.dto.ejs.t +19 -0
- package/.hygen/new/crud/index-dto.ejs.t +29 -0
- package/.hygen/new/crud/index-response.dto.ejs.t +18 -0
- package/.hygen/new/crud/module.ejs.t +32 -0
- package/.hygen/new/crud/prompt.js +89 -0
- package/.hygen/new/crud/service.ejs.t +76 -0
- package/.hygen/new/crud/update.dto.ejs.t +11 -0
- package/.hygen/new/module/controller.ejs.t +93 -0
- package/.hygen/new/module/create-command.ejs.t +90 -0
- package/.hygen/new/module/create.dto.ejs.t +11 -0
- package/.hygen/new/module/dto.ejs.t +24 -0
- package/.hygen/new/module/entity.ejs.t +33 -0
- package/.hygen/new/module/get-query.ejs.t +57 -0
- package/.hygen/new/module/module.ejs.t +44 -0
- package/.hygen/new/module/not-found.exception.ejs.t +18 -0
- package/.hygen/new/module/page-options.dto.ejs.t +12 -0
- package/.hygen/new/module/prompt.js +104 -0
- package/.hygen/new/module/repository.ejs.t +16 -0
- package/.hygen/new/module/service.ejs.t +139 -0
- package/.hygen/new/module/translation-dto.ejs.t +27 -0
- package/.hygen/new/module/translation-entity.ejs.t +40 -0
- package/.hygen/new/module/translation-repository.ejs.t +16 -0
- package/.hygen/new/module/update.dto.ejs.t +10 -0
- package/.hygen.js +116 -0
- package/cache/cache-metrics.service.d.ts +83 -0
- package/cache/cache-metrics.service.js +298 -0
- package/cache/cache-serialization.service.d.ts +22 -0
- package/cache/cache-serialization.service.js +122 -0
- package/cache/cache.constants.d.ts +2 -0
- package/cache/cache.constants.js +5 -0
- package/cache/cache.health.d.ts +35 -0
- package/cache/cache.health.js +193 -0
- package/cache/cache.module.d.ts +26 -0
- package/cache/cache.module.js +155 -0
- package/cache/cache.service.d.ts +37 -0
- package/cache/cache.service.js +308 -0
- package/cache/cache.warmup.service.d.ts +34 -0
- package/cache/cache.warmup.service.js +199 -0
- package/cache/decorators/cache-evict.decorator.d.ts +2 -0
- package/cache/decorators/cache-evict.decorator.js +72 -0
- package/cache/decorators/cache-put.decorator.d.ts +2 -0
- package/cache/decorators/cache-put.decorator.js +48 -0
- package/cache/decorators/cacheable.decorator.d.ts +4 -0
- package/cache/decorators/cacheable.decorator.js +68 -0
- package/cache/decorators/index.d.ts +3 -0
- package/cache/decorators/index.js +11 -0
- package/cache/dependencies/callback.dependency.d.ts +11 -0
- package/cache/dependencies/callback.dependency.js +92 -0
- package/cache/dependencies/chain.dependency.d.ts +20 -0
- package/cache/dependencies/chain.dependency.js +113 -0
- package/cache/dependencies/db.dependency.d.ts +15 -0
- package/cache/dependencies/db.dependency.js +71 -0
- package/cache/dependencies/file.dependency.d.ts +17 -0
- package/cache/dependencies/file.dependency.js +63 -0
- package/cache/dependencies/index.d.ts +6 -0
- package/cache/dependencies/index.js +22 -0
- package/cache/dependencies/tag.dependency.d.ts +16 -0
- package/cache/dependencies/tag.dependency.js +75 -0
- package/cache/dependencies/time.dependency.d.ts +19 -0
- package/cache/dependencies/time.dependency.js +71 -0
- package/cache/examples/basic-usage.d.ts +58 -0
- package/cache/examples/basic-usage.js +379 -0
- package/cache/index.d.ts +15 -0
- package/cache/index.js +44 -0
- package/cache/interfaces/cache-dependency.interface.d.ts +11 -0
- package/cache/interfaces/cache-dependency.interface.js +2 -0
- package/cache/interfaces/cache-options.interface.d.ts +32 -0
- package/cache/interfaces/cache-options.interface.js +9 -0
- package/cache/interfaces/cache-provider.interface.d.ts +28 -0
- package/cache/interfaces/cache-provider.interface.js +2 -0
- package/cache/interfaces/index.d.ts +3 -0
- package/cache/interfaces/index.js +19 -0
- package/cache/providers/base-cache.provider.d.ts +16 -0
- package/cache/providers/base-cache.provider.js +32 -0
- package/cache/providers/cls-cache.provider.d.ts +16 -0
- package/cache/providers/cls-cache.provider.js +140 -0
- package/cache/providers/index.d.ts +4 -0
- package/cache/providers/index.js +23 -0
- package/cache/providers/memory-cache.provider.d.ts +26 -0
- package/cache/providers/memory-cache.provider.js +171 -0
- package/cache/providers/redis-cache.provider.d.ts +23 -0
- package/cache/providers/redis-cache.provider.js +215 -0
- package/cache/utils/dependency-manager.util.d.ts +15 -0
- package/cache/utils/dependency-manager.util.js +141 -0
- package/cache/utils/index.d.ts +2 -0
- package/cache/utils/index.js +18 -0
- package/cache/utils/key-generator.util.d.ts +13 -0
- package/cache/utils/key-generator.util.js +74 -0
- package/common/abstract.entity.d.ts +20 -0
- package/common/abstract.entity.js +81 -0
- package/common/boilerplate.polyfill.d.ts +50 -0
- package/common/boilerplate.polyfill.js +152 -0
- package/common/dto/abstract.dto.d.ts +13 -0
- package/common/dto/abstract.dto.js +59 -0
- package/common/dto/bulk-id.dto.d.ts +3 -0
- package/common/dto/bulk-id.dto.js +27 -0
- package/common/dto/create-translation.dto.d.ts +5 -0
- package/common/dto/create-translation.dto.js +25 -0
- package/common/dto/dto-container.d.ts +9 -0
- package/common/dto/dto-container.js +59 -0
- package/common/dto/dto-decorators.d.ts +18 -0
- package/common/dto/dto-decorators.js +59 -0
- package/common/dto/dto-extensions.d.ts +12 -0
- package/common/dto/dto-extensions.js +63 -0
- package/common/dto/dto-service-accessor.d.ts +7 -0
- package/common/dto/dto-service-accessor.js +49 -0
- package/common/dto/dto-transformer.d.ts +11 -0
- package/common/dto/dto-transformer.js +49 -0
- package/common/dto/index.d.ts +12 -0
- package/common/dto/index.js +28 -0
- package/common/dto/page-meta.dto.d.ts +15 -0
- package/common/dto/page-meta.dto.js +48 -0
- package/common/dto/page-options.dto.d.ts +5 -0
- package/common/dto/page-options.dto.js +40 -0
- package/common/dto/page.dto.d.ts +11 -0
- package/common/dto/page.dto.js +53 -0
- package/common/examples/paginate-and-map.example.d.ts +14 -0
- package/common/examples/paginate-and-map.example.js +158 -0
- package/common/index.d.ts +6 -0
- package/common/index.js +22 -0
- package/common/snake-naming.strategy.d.ts +11 -0
- package/common/snake-naming.strategy.js +37 -0
- package/common/types.d.ts +9 -0
- package/common/types.js +2 -0
- package/common/utils.d.ts +3 -0
- package/common/utils.js +24 -0
- package/constants/index.d.ts +2 -0
- package/constants/index.js +18 -0
- package/constants/language-code.d.ts +5 -0
- package/constants/language-code.js +9 -0
- package/constants/order.d.ts +4 -0
- package/constants/order.js +8 -0
- package/decorators/api-page-ok-response.decorator.d.ts +5 -0
- package/decorators/api-page-ok-response.decorator.js +24 -0
- package/decorators/api-paginated-response.decorator.d.ts +2 -0
- package/decorators/api-paginated-response.decorator.js +9 -0
- package/decorators/auth-user.decorator.d.ts +1 -0
- package/decorators/auth-user.decorator.js +14 -0
- package/decorators/controller.decorator.d.ts +1 -0
- package/decorators/controller.decorator.js +10 -0
- package/decorators/examples/validation-decorators.example.d.ts +69 -0
- package/decorators/examples/validation-decorators.example.js +331 -0
- package/decorators/field.decorators.d.ts +79 -0
- package/decorators/field.decorators.js +499 -0
- package/decorators/http.decorators.d.ts +3 -0
- package/decorators/http.decorators.js +7 -0
- package/decorators/index.d.ts +14 -0
- package/decorators/index.js +30 -0
- package/decorators/operation.decorator.d.ts +1 -0
- package/decorators/operation.decorator.js +6 -0
- package/decorators/paginated-response.decorator.d.ts +11 -0
- package/decorators/paginated-response.decorator.js +60 -0
- package/decorators/property.decorators.d.ts +15 -0
- package/decorators/property.decorators.js +31 -0
- package/decorators/public-route.decorator.d.ts +3 -0
- package/decorators/public-route.decorator.js +7 -0
- package/decorators/swagger.schema.d.ts +5 -0
- package/decorators/swagger.schema.js +78 -0
- package/decorators/timestamp-column.decorator.d.ts +1 -0
- package/decorators/timestamp-column.decorator.js +27 -0
- package/decorators/transform.decorators.d.ts +8 -0
- package/decorators/transform.decorators.js +100 -0
- package/decorators/translate.decorator.d.ts +5 -0
- package/decorators/translate.decorator.js +17 -0
- package/decorators/use-dto.decorator.d.ts +4 -0
- package/decorators/use-dto.decorator.js +8 -0
- package/decorators/user-check.decorator.d.ts +6 -0
- package/decorators/user-check.decorator.js +21 -0
- package/decorators/user.decorator.d.ts +12 -0
- package/decorators/user.decorator.js +44 -0
- package/decorators/user_auth.decorator.d.ts +1 -0
- package/decorators/user_auth.decorator.js +15 -0
- package/decorators/validator.decorators.d.ts +18 -0
- package/decorators/validator.decorators.js +193 -0
- package/exceptions/file-not-image.exception.d.ts +4 -0
- package/exceptions/file-not-image.exception.js +10 -0
- package/exceptions/index.d.ts +2 -0
- package/exceptions/index.js +18 -0
- package/exceptions/page-type.exception.d.ts +4 -0
- package/exceptions/page-type.exception.js +10 -0
- package/exceptions/user-not-found.exception.d.ts +4 -0
- package/exceptions/user-not-found.exception.js +10 -0
- package/filters/bad-request.filter.d.ts +5 -0
- package/filters/bad-request.filter.js +71 -0
- package/filters/constraint-errors.d.ts +1 -0
- package/filters/constraint-errors.js +6 -0
- package/filters/index.d.ts +3 -0
- package/filters/index.js +19 -0
- package/filters/query-failed.filter.d.ts +10 -0
- package/filters/query-failed.filter.js +42 -0
- package/health-checker/health-checker.controller.d.ts +13 -0
- package/health-checker/health-checker.controller.js +69 -0
- package/health-checker/health-checker.module.d.ts +2 -0
- package/health-checker/health-checker.module.js +24 -0
- package/health-checker/index.d.ts +2 -0
- package/health-checker/index.js +18 -0
- package/helpers/common.helper.d.ts +3 -0
- package/helpers/common.helper.js +36 -0
- package/helpers/date.helper.d.ts +10 -0
- package/helpers/date.helper.js +57 -0
- package/helpers/index.d.ts +2 -0
- package/helpers/index.js +18 -0
- package/i18n/en_US/validation.json +44 -0
- package/i18n/zh_CN/validation.json +44 -0
- package/index.d.ts +18 -0
- package/index.js +3 -0
- package/interceptors/index.d.ts +2 -0
- package/interceptors/index.js +18 -0
- package/interceptors/language-interceptor.service.d.ts +6 -0
- package/interceptors/language-interceptor.service.js +29 -0
- package/interceptors/translation-interceptor.service.d.ts +5 -0
- package/interceptors/translation-interceptor.service.js +34 -0
- package/interfaces/IApiFile.d.ts +4 -0
- package/interfaces/IApiFile.js +2 -0
- package/interfaces/IFile.d.ts +8 -0
- package/interfaces/IFile.js +2 -0
- package/interfaces/ITranslationDecoratorInterface.d.ts +3 -0
- package/interfaces/ITranslationDecoratorInterface.js +2 -0
- package/interfaces/index.d.ts +3 -0
- package/interfaces/index.js +19 -0
- package/interfaces/response.d.ts +25 -0
- package/interfaces/response.js +2 -0
- package/middlewares/index.d.ts +4 -0
- package/middlewares/index.js +20 -0
- package/middlewares/omni-auth.middleware.d.ts +1 -0
- package/middlewares/omni-auth.middleware.js +13 -0
- package/middlewares/powerby.middleware.d.ts +1 -0
- package/middlewares/powerby.middleware.js +10 -0
- package/middlewares/request-id.middleware.d.ts +7 -0
- package/middlewares/request-id.middleware.js +22 -0
- package/middlewares/xml.middleware.d.ts +4 -0
- package/middlewares/xml.middleware.js +24 -0
- package/package.json +41 -116
- package/providers/context.provider.d.ts +34 -0
- package/providers/context.provider.js +80 -0
- package/providers/generator.provider.d.ts +9 -0
- package/providers/generator.provider.js +50 -0
- package/providers/index.d.ts +2 -0
- package/providers/index.js +18 -0
- package/redis-lock/examples/lock-strategy.examples.d.ts +73 -0
- package/redis-lock/examples/lock-strategy.examples.js +387 -0
- package/redis-lock/index.d.ts +4 -0
- package/redis-lock/index.js +16 -0
- package/redis-lock/redis-lock.decorator.d.ts +12 -0
- package/redis-lock/redis-lock.decorator.js +125 -0
- package/redis-lock/redis-lock.module.d.ts +23 -0
- package/redis-lock/redis-lock.module.js +117 -0
- package/redis-lock/redis-lock.service.d.ts +66 -0
- package/redis-lock/redis-lock.service.js +413 -0
- package/setup/bootstrap.setup.d.ts +2 -0
- package/setup/bootstrap.setup.js +184 -0
- package/setup/index.d.ts +4 -0
- package/setup/index.js +20 -0
- package/setup/mode.setup.d.ts +12 -0
- package/setup/mode.setup.js +60 -0
- package/setup/schedule.decorator.d.ts +23 -0
- package/setup/schedule.decorator.js +233 -0
- package/setup/worker.decorator.d.ts +14 -0
- package/setup/worker.decorator.js +130 -0
- package/shared/index.d.ts +2 -0
- package/shared/index.js +18 -0
- package/shared/serviceRegistryModule.d.ts +2 -0
- package/shared/serviceRegistryModule.js +170 -0
- package/shared/services/api-config.service.d.ts +54 -0
- package/shared/services/api-config.service.js +287 -0
- package/shared/services/generator.service.d.ts +4 -0
- package/shared/services/generator.service.js +23 -0
- package/shared/services/index.d.ts +4 -0
- package/shared/services/index.js +20 -0
- package/shared/services/translation.service.d.ts +9 -0
- package/shared/services/translation.service.js +70 -0
- package/shared/services/validator.service.d.ts +3 -0
- package/shared/services/validator.service.js +20 -0
- package/validator-json/decorators.d.ts +6 -0
- package/validator-json/decorators.js +22 -0
- package/validator-json/default.d.ts +6 -0
- package/validator-json/default.js +138 -0
- package/validator-json/defaultConverters.d.ts +8 -0
- package/validator-json/defaultConverters.js +376 -0
- package/validator-json/index.d.ts +4 -0
- package/validator-json/index.js +20 -0
- package/validator-json/options.d.ts +11 -0
- package/validator-json/options.js +10 -0
- package/validators/file-mimetype.validator.d.ts +13 -0
- package/validators/file-mimetype.validator.js +21 -0
- package/validators/index.d.ts +6 -0
- package/validators/index.js +22 -0
- package/validators/is-exists.validator.d.ts +18 -0
- package/validators/is-exists.validator.js +60 -0
- package/validators/is-unique.validator.d.ts +17 -0
- package/validators/is-unique.validator.js +91 -0
- package/validators/phone-country-code.validator.d.ts +2 -0
- package/validators/phone-country-code.validator.js +29 -0
- package/validators/same-as.validator.d.ts +2 -0
- package/validators/same-as.validator.js +24 -0
- package/validators/skip-empty.validator.d.ts +1 -0
- package/validators/skip-empty.validator.js +17 -0
- package/vault/index.d.ts +4 -0
- package/vault/index.js +20 -0
- package/vault/interfaces/vault-options.interface.d.ts +22 -0
- package/vault/interfaces/vault-options.interface.js +2 -0
- package/vault/vault-config.loader.d.ts +13 -0
- package/vault/vault-config.loader.js +246 -0
- package/vault/vault-config.service.d.ts +18 -0
- package/vault/vault-config.service.js +174 -0
- package/vault/vault.module.d.ts +2 -0
- package/vault/vault.module.js +21 -0
|
@@ -0,0 +1,117 @@
|
|
|
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 RedisLockModule_1;
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.RedisLockModule = exports.REDIS_LOCK_SERVICE = void 0;
|
|
14
|
+
const common_1 = require("@nestjs/common");
|
|
15
|
+
const redis_lock_service_1 = require("./redis-lock.service");
|
|
16
|
+
const redis_lock_decorator_1 = require("./redis-lock.decorator");
|
|
17
|
+
exports.REDIS_LOCK_SERVICE = Symbol('REDIS_LOCK_SERVICE');
|
|
18
|
+
let RedisLockModule = RedisLockModule_1 = class RedisLockModule {
|
|
19
|
+
constructor() { }
|
|
20
|
+
static forRoot(options) {
|
|
21
|
+
const { isGlobal = true, redisClient, defaultTtl, defaultKeyPrefix, defaultRetryCount, defaultRetryDelay, } = options;
|
|
22
|
+
return {
|
|
23
|
+
module: RedisLockModule_1,
|
|
24
|
+
global: isGlobal,
|
|
25
|
+
providers: [
|
|
26
|
+
{
|
|
27
|
+
provide: 'REDIS_LOCK_CLIENT',
|
|
28
|
+
useValue: redisClient,
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
provide: exports.REDIS_LOCK_SERVICE,
|
|
32
|
+
useFactory: () => {
|
|
33
|
+
const service = new redis_lock_service_1.RedisLockService();
|
|
34
|
+
service.setRedisClient(redisClient);
|
|
35
|
+
if (defaultTtl) {
|
|
36
|
+
service.setDefaultTtl(defaultTtl);
|
|
37
|
+
}
|
|
38
|
+
if (defaultKeyPrefix) {
|
|
39
|
+
service.setDefaultKeyPrefix(defaultKeyPrefix);
|
|
40
|
+
}
|
|
41
|
+
if (defaultRetryCount !== undefined) {
|
|
42
|
+
service.setDefaultRetryCount(defaultRetryCount);
|
|
43
|
+
}
|
|
44
|
+
if (defaultRetryDelay !== undefined) {
|
|
45
|
+
service.setDefaultRetryDelay(defaultRetryDelay);
|
|
46
|
+
}
|
|
47
|
+
return service;
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
provide: redis_lock_service_1.RedisLockService,
|
|
52
|
+
useExisting: exports.REDIS_LOCK_SERVICE,
|
|
53
|
+
},
|
|
54
|
+
],
|
|
55
|
+
exports: [exports.REDIS_LOCK_SERVICE, redis_lock_service_1.RedisLockService],
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
static forRootAsync(options) {
|
|
59
|
+
var _a;
|
|
60
|
+
return {
|
|
61
|
+
module: RedisLockModule_1,
|
|
62
|
+
global: (_a = options.isGlobal) !== null && _a !== void 0 ? _a : true,
|
|
63
|
+
imports: options.imports || [],
|
|
64
|
+
providers: [
|
|
65
|
+
{
|
|
66
|
+
provide: 'REDIS_LOCK_MODULE_OPTIONS',
|
|
67
|
+
useFactory: options.useFactory,
|
|
68
|
+
inject: options.inject || [],
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
provide: 'REDIS_LOCK_CLIENT',
|
|
72
|
+
useFactory: (moduleOptions) => moduleOptions.redisClient,
|
|
73
|
+
inject: ['REDIS_LOCK_MODULE_OPTIONS'],
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
provide: exports.REDIS_LOCK_SERVICE,
|
|
77
|
+
useFactory: (moduleOptions) => {
|
|
78
|
+
const service = new redis_lock_service_1.RedisLockService();
|
|
79
|
+
service.setRedisClient(moduleOptions.redisClient);
|
|
80
|
+
if (moduleOptions.defaultTtl) {
|
|
81
|
+
service.setDefaultTtl(moduleOptions.defaultTtl);
|
|
82
|
+
}
|
|
83
|
+
if (moduleOptions.defaultKeyPrefix) {
|
|
84
|
+
service.setDefaultKeyPrefix(moduleOptions.defaultKeyPrefix);
|
|
85
|
+
}
|
|
86
|
+
if (moduleOptions.defaultRetryCount !== undefined) {
|
|
87
|
+
service.setDefaultRetryCount(moduleOptions.defaultRetryCount);
|
|
88
|
+
}
|
|
89
|
+
if (moduleOptions.defaultRetryDelay !== undefined) {
|
|
90
|
+
service.setDefaultRetryDelay(moduleOptions.defaultRetryDelay);
|
|
91
|
+
}
|
|
92
|
+
return service;
|
|
93
|
+
},
|
|
94
|
+
inject: ['REDIS_LOCK_MODULE_OPTIONS'],
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
provide: redis_lock_service_1.RedisLockService,
|
|
98
|
+
useExisting: exports.REDIS_LOCK_SERVICE,
|
|
99
|
+
},
|
|
100
|
+
],
|
|
101
|
+
exports: [exports.REDIS_LOCK_SERVICE, redis_lock_service_1.RedisLockService],
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
onModuleInit() {
|
|
105
|
+
const globalInstance = redis_lock_service_1.RedisLockService.getGlobalInstance();
|
|
106
|
+
if (globalInstance) {
|
|
107
|
+
this.redisLockService = globalInstance;
|
|
108
|
+
(0, redis_lock_decorator_1.setLockService)(this.redisLockService);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
exports.RedisLockModule = RedisLockModule;
|
|
113
|
+
exports.RedisLockModule = RedisLockModule = RedisLockModule_1 = __decorate([
|
|
114
|
+
(0, common_1.Global)(),
|
|
115
|
+
(0, common_1.Module)({}),
|
|
116
|
+
__metadata("design:paramtypes", [])
|
|
117
|
+
], RedisLockModule);
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { OnModuleInit } from '@nestjs/common';
|
|
2
|
+
import type Redis from 'ioredis';
|
|
3
|
+
export declare enum LockStrategy {
|
|
4
|
+
SKIP = "SKIP",
|
|
5
|
+
THROW = "THROW",
|
|
6
|
+
WAIT = "WAIT"
|
|
7
|
+
}
|
|
8
|
+
export interface LockOptions {
|
|
9
|
+
ttl?: number;
|
|
10
|
+
retryCount?: number;
|
|
11
|
+
retryDelay?: number;
|
|
12
|
+
keyPrefix?: string;
|
|
13
|
+
throwOnFailure?: boolean;
|
|
14
|
+
strategy?: LockStrategy;
|
|
15
|
+
waitTimeout?: number;
|
|
16
|
+
autoExtend?: number;
|
|
17
|
+
}
|
|
18
|
+
export interface LockResult {
|
|
19
|
+
acquired: boolean;
|
|
20
|
+
lockValue?: string;
|
|
21
|
+
error?: string;
|
|
22
|
+
autoExtendTimer?: NodeJS.Timeout;
|
|
23
|
+
}
|
|
24
|
+
export declare class RedisLockService implements OnModuleInit {
|
|
25
|
+
private static globalInstance;
|
|
26
|
+
private readonly logger;
|
|
27
|
+
private redis;
|
|
28
|
+
private readonly defaultOptions;
|
|
29
|
+
constructor();
|
|
30
|
+
static getGlobalInstance(): RedisLockService | null;
|
|
31
|
+
static getOrCreateGlobalInstance(): RedisLockService;
|
|
32
|
+
static setGlobalInstance(instance: RedisLockService): void;
|
|
33
|
+
onModuleInit(): Promise<void>;
|
|
34
|
+
onModuleDestroy(): Promise<void>;
|
|
35
|
+
setRedisClient(redis: Redis): void;
|
|
36
|
+
setDefaultTtl(ttl: number): void;
|
|
37
|
+
setDefaultKeyPrefix(prefix: string): void;
|
|
38
|
+
setDefaultRetryCount(count: number): void;
|
|
39
|
+
setDefaultRetryDelay(delay: number): void;
|
|
40
|
+
acquireLock(key: string, options?: LockOptions): Promise<LockResult>;
|
|
41
|
+
releaseLock(key: string, lockValue: string, keyPrefix?: string): Promise<boolean>;
|
|
42
|
+
extendLock(key: string, lockValue: string, ttl: number, keyPrefix?: string): Promise<boolean>;
|
|
43
|
+
isLocked(key: string, keyPrefix?: string): Promise<boolean>;
|
|
44
|
+
withLock<T>(key: string, fn: () => Promise<T>, options?: LockOptions): Promise<T | null>;
|
|
45
|
+
getLockInfo(key: string, keyPrefix?: string): Promise<{
|
|
46
|
+
value: string | null;
|
|
47
|
+
ttl: number | null;
|
|
48
|
+
}>;
|
|
49
|
+
forceRelease(key: string, keyPrefix?: string): Promise<boolean>;
|
|
50
|
+
cleanupLocksByPattern(pattern: string, keyPrefix?: string): Promise<number>;
|
|
51
|
+
cleanupOnStartup(patterns: string[], keyPrefix?: string): Promise<number>;
|
|
52
|
+
healthCheck(): Promise<{
|
|
53
|
+
healthy: boolean;
|
|
54
|
+
redis: boolean;
|
|
55
|
+
message: string;
|
|
56
|
+
}>;
|
|
57
|
+
getActiveLocks(pattern?: string, keyPrefix?: string): Promise<Array<{
|
|
58
|
+
key: string;
|
|
59
|
+
value: string | null;
|
|
60
|
+
ttl: number | null;
|
|
61
|
+
}>>;
|
|
62
|
+
private getRedis;
|
|
63
|
+
private generateLockValue;
|
|
64
|
+
private buildLockKey;
|
|
65
|
+
private sleep;
|
|
66
|
+
}
|
|
@@ -0,0 +1,413 @@
|
|
|
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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
12
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
13
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
14
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
15
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
16
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
17
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
var RedisLockService_1;
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
exports.RedisLockService = exports.LockStrategy = void 0;
|
|
23
|
+
const common_1 = require("@nestjs/common");
|
|
24
|
+
var LockStrategy;
|
|
25
|
+
(function (LockStrategy) {
|
|
26
|
+
LockStrategy["SKIP"] = "SKIP";
|
|
27
|
+
LockStrategy["THROW"] = "THROW";
|
|
28
|
+
LockStrategy["WAIT"] = "WAIT";
|
|
29
|
+
})(LockStrategy || (exports.LockStrategy = LockStrategy = {}));
|
|
30
|
+
let RedisLockService = RedisLockService_1 = class RedisLockService {
|
|
31
|
+
constructor() {
|
|
32
|
+
this.logger = new common_1.Logger(RedisLockService_1.name);
|
|
33
|
+
this.redis = null;
|
|
34
|
+
this.defaultOptions = {
|
|
35
|
+
ttl: 300000,
|
|
36
|
+
retryCount: 0,
|
|
37
|
+
retryDelay: 100,
|
|
38
|
+
keyPrefix: 'lock',
|
|
39
|
+
throwOnFailure: false,
|
|
40
|
+
strategy: LockStrategy.SKIP,
|
|
41
|
+
waitTimeout: undefined,
|
|
42
|
+
autoExtend: 0,
|
|
43
|
+
};
|
|
44
|
+
if (!RedisLockService_1.globalInstance) {
|
|
45
|
+
RedisLockService_1.globalInstance = this;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
static getGlobalInstance() {
|
|
49
|
+
return RedisLockService_1.globalInstance;
|
|
50
|
+
}
|
|
51
|
+
static getOrCreateGlobalInstance() {
|
|
52
|
+
if (!RedisLockService_1.globalInstance) {
|
|
53
|
+
RedisLockService_1.globalInstance = new RedisLockService_1();
|
|
54
|
+
}
|
|
55
|
+
return RedisLockService_1.globalInstance;
|
|
56
|
+
}
|
|
57
|
+
static setGlobalInstance(instance) {
|
|
58
|
+
RedisLockService_1.globalInstance = instance;
|
|
59
|
+
}
|
|
60
|
+
onModuleInit() {
|
|
61
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
62
|
+
if (!this.redis) {
|
|
63
|
+
this.logger.warn('RedisLockService initialized but no Redis client provided. Redis Lock functionality will not work.');
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
this.logger.log('RedisLockService initialized with Redis client');
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
onModuleDestroy() {
|
|
71
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
72
|
+
this.logger.log('RedisLockService destroyed');
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
setRedisClient(redis) {
|
|
76
|
+
this.redis = redis;
|
|
77
|
+
this.logger.log('Redis client set for RedisLockService');
|
|
78
|
+
}
|
|
79
|
+
setDefaultTtl(ttl) {
|
|
80
|
+
this.defaultOptions.ttl = ttl;
|
|
81
|
+
}
|
|
82
|
+
setDefaultKeyPrefix(prefix) {
|
|
83
|
+
this.defaultOptions.keyPrefix = prefix;
|
|
84
|
+
}
|
|
85
|
+
setDefaultRetryCount(count) {
|
|
86
|
+
this.defaultOptions.retryCount = count;
|
|
87
|
+
}
|
|
88
|
+
setDefaultRetryDelay(delay) {
|
|
89
|
+
this.defaultOptions.retryDelay = delay;
|
|
90
|
+
}
|
|
91
|
+
acquireLock(key_1) {
|
|
92
|
+
return __awaiter(this, arguments, void 0, function* (key, options = {}) {
|
|
93
|
+
const redis = yield this.getRedis();
|
|
94
|
+
const opts = Object.assign(Object.assign({}, this.defaultOptions), options);
|
|
95
|
+
if (opts.throwOnFailure && !options.strategy) {
|
|
96
|
+
opts.strategy = LockStrategy.THROW;
|
|
97
|
+
}
|
|
98
|
+
const lockKey = this.buildLockKey(key, opts.keyPrefix);
|
|
99
|
+
const lockValue = this.generateLockValue();
|
|
100
|
+
let maxAttempts;
|
|
101
|
+
if (opts.strategy === LockStrategy.WAIT) {
|
|
102
|
+
maxAttempts = Number.MAX_SAFE_INTEGER;
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
maxAttempts = opts.retryCount + 1;
|
|
106
|
+
}
|
|
107
|
+
let attempts = 0;
|
|
108
|
+
const startTime = Date.now();
|
|
109
|
+
while (attempts < maxAttempts) {
|
|
110
|
+
if (opts.strategy === LockStrategy.WAIT && opts.waitTimeout) {
|
|
111
|
+
const elapsed = Date.now() - startTime;
|
|
112
|
+
if (elapsed >= opts.waitTimeout) {
|
|
113
|
+
const timeoutMessage = `Lock acquisition timed out for ${lockKey} after ${elapsed}ms (waitTimeout: ${opts.waitTimeout}ms)`;
|
|
114
|
+
this.logger.warn(timeoutMessage);
|
|
115
|
+
return { acquired: false, error: timeoutMessage };
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
try {
|
|
119
|
+
const result = yield redis.set(lockKey, lockValue, 'PX', opts.ttl, 'NX');
|
|
120
|
+
if (result === 'OK') {
|
|
121
|
+
this.logger.debug(`Lock acquired: ${lockKey} (value: ${lockValue}, ttl: ${opts.ttl}ms, attempts: ${attempts + 1})`);
|
|
122
|
+
let autoExtendTimer;
|
|
123
|
+
if (opts.autoExtend && opts.autoExtend > 0) {
|
|
124
|
+
autoExtendTimer = setInterval(() => __awaiter(this, void 0, void 0, function* () {
|
|
125
|
+
try {
|
|
126
|
+
yield this.extendLock(key, lockValue, opts.ttl, opts.keyPrefix);
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
this.logger.error(`Failed to auto-extend lock ${lockKey}:`, error);
|
|
130
|
+
}
|
|
131
|
+
}), opts.autoExtend);
|
|
132
|
+
}
|
|
133
|
+
return { acquired: true, lockValue, autoExtendTimer };
|
|
134
|
+
}
|
|
135
|
+
attempts++;
|
|
136
|
+
if (opts.strategy === LockStrategy.WAIT) {
|
|
137
|
+
if (attempts === 1 || attempts % 10 === 0) {
|
|
138
|
+
this.logger.debug(`Waiting for lock ${lockKey}... (attempt ${attempts})`);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
else if (attempts < maxAttempts) {
|
|
142
|
+
this.logger.debug(`Lock acquisition failed for ${lockKey}, retrying... (${attempts}/${opts.retryCount})`);
|
|
143
|
+
}
|
|
144
|
+
if (attempts < maxAttempts) {
|
|
145
|
+
yield this.sleep(opts.retryDelay);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
const errorMessage = `Error acquiring lock for ${lockKey}: ${error.message}`;
|
|
150
|
+
this.logger.error(errorMessage, error.stack);
|
|
151
|
+
if (opts.strategy === LockStrategy.THROW) {
|
|
152
|
+
throw new Error(errorMessage);
|
|
153
|
+
}
|
|
154
|
+
return { acquired: false, error: errorMessage };
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
const failureMessage = `Failed to acquire lock for ${lockKey} after ${attempts} attempts`;
|
|
158
|
+
this.logger.warn(failureMessage);
|
|
159
|
+
if (opts.strategy === LockStrategy.THROW) {
|
|
160
|
+
throw new Error(failureMessage);
|
|
161
|
+
}
|
|
162
|
+
return { acquired: false, error: failureMessage };
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
releaseLock(key_1, lockValue_1) {
|
|
166
|
+
return __awaiter(this, arguments, void 0, function* (key, lockValue, keyPrefix = 'lock') {
|
|
167
|
+
const redis = yield this.getRedis();
|
|
168
|
+
const lockKey = this.buildLockKey(key, keyPrefix);
|
|
169
|
+
try {
|
|
170
|
+
const script = `
|
|
171
|
+
if redis.call("get", KEYS[1]) == ARGV[1] then
|
|
172
|
+
return redis.call("del", KEYS[1])
|
|
173
|
+
else
|
|
174
|
+
return 0
|
|
175
|
+
end
|
|
176
|
+
`;
|
|
177
|
+
const result = yield redis.eval(script, 1, lockKey, lockValue);
|
|
178
|
+
if (result === 1) {
|
|
179
|
+
this.logger.debug(`Lock released: ${lockKey}`);
|
|
180
|
+
return true;
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
this.logger.warn(`Lock release failed: ${lockKey} (lock value mismatch or already expired)`);
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
catch (error) {
|
|
188
|
+
this.logger.error(`Error releasing lock for ${lockKey}: ${error.message}`, error.stack);
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
extendLock(key_1, lockValue_1, ttl_1) {
|
|
194
|
+
return __awaiter(this, arguments, void 0, function* (key, lockValue, ttl, keyPrefix = 'lock') {
|
|
195
|
+
const redis = yield this.getRedis();
|
|
196
|
+
const lockKey = this.buildLockKey(key, keyPrefix);
|
|
197
|
+
try {
|
|
198
|
+
const script = `
|
|
199
|
+
if redis.call("get", KEYS[1]) == ARGV[1] then
|
|
200
|
+
return redis.call("pexpire", KEYS[1], ARGV[2])
|
|
201
|
+
else
|
|
202
|
+
return 0
|
|
203
|
+
end
|
|
204
|
+
`;
|
|
205
|
+
const result = yield redis.eval(script, 1, lockKey, lockValue, ttl);
|
|
206
|
+
if (result === 1) {
|
|
207
|
+
this.logger.debug(`Lock extended: ${lockKey} (new ttl: ${ttl}ms)`);
|
|
208
|
+
return true;
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
this.logger.warn(`Lock extension failed: ${lockKey} (lock value mismatch or doesn't exist)`);
|
|
212
|
+
return false;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
catch (error) {
|
|
216
|
+
this.logger.error(`Error extending lock for ${lockKey}: ${error.message}`, error.stack);
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
isLocked(key_1) {
|
|
222
|
+
return __awaiter(this, arguments, void 0, function* (key, keyPrefix = 'lock') {
|
|
223
|
+
const redis = yield this.getRedis();
|
|
224
|
+
const lockKey = this.buildLockKey(key, keyPrefix);
|
|
225
|
+
try {
|
|
226
|
+
const exists = yield redis.exists(lockKey);
|
|
227
|
+
return exists === 1;
|
|
228
|
+
}
|
|
229
|
+
catch (error) {
|
|
230
|
+
this.logger.error(`Error checking lock existence for ${lockKey}: ${error.message}`, error.stack);
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
withLock(key_1, fn_1) {
|
|
236
|
+
return __awaiter(this, arguments, void 0, function* (key, fn, options = {}) {
|
|
237
|
+
const lockResult = yield this.acquireLock(key, options);
|
|
238
|
+
if (!lockResult.acquired) {
|
|
239
|
+
this.logger.warn(`Could not acquire lock for ${key}, skipping execution`);
|
|
240
|
+
return null;
|
|
241
|
+
}
|
|
242
|
+
try {
|
|
243
|
+
return yield fn();
|
|
244
|
+
}
|
|
245
|
+
catch (error) {
|
|
246
|
+
this.logger.error(`Error executing function with lock ${key}:`, error);
|
|
247
|
+
throw error;
|
|
248
|
+
}
|
|
249
|
+
finally {
|
|
250
|
+
if (lockResult.autoExtendTimer) {
|
|
251
|
+
clearInterval(lockResult.autoExtendTimer);
|
|
252
|
+
}
|
|
253
|
+
yield this.releaseLock(key, lockResult.lockValue, options.keyPrefix);
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
getLockInfo(key_1) {
|
|
258
|
+
return __awaiter(this, arguments, void 0, function* (key, keyPrefix = 'lock') {
|
|
259
|
+
const redis = yield this.getRedis();
|
|
260
|
+
const lockKey = this.buildLockKey(key, keyPrefix);
|
|
261
|
+
try {
|
|
262
|
+
const [value, ttl] = yield Promise.all([
|
|
263
|
+
redis.get(lockKey),
|
|
264
|
+
redis.pttl(lockKey),
|
|
265
|
+
]);
|
|
266
|
+
return {
|
|
267
|
+
value,
|
|
268
|
+
ttl: ttl >= 0 ? ttl : null,
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
catch (error) {
|
|
272
|
+
this.logger.error(`Error getting lock info for ${lockKey}: ${error.message}`, error.stack);
|
|
273
|
+
return { value: null, ttl: null };
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
forceRelease(key_1) {
|
|
278
|
+
return __awaiter(this, arguments, void 0, function* (key, keyPrefix = 'lock') {
|
|
279
|
+
const redis = yield this.getRedis();
|
|
280
|
+
const lockKey = this.buildLockKey(key, keyPrefix);
|
|
281
|
+
try {
|
|
282
|
+
const result = yield redis.del(lockKey);
|
|
283
|
+
if (result === 1) {
|
|
284
|
+
this.logger.debug(`Lock forcefully released: ${lockKey}`);
|
|
285
|
+
return true;
|
|
286
|
+
}
|
|
287
|
+
else {
|
|
288
|
+
this.logger.debug(`No lock found to forcefully release: ${lockKey}`);
|
|
289
|
+
return false;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
catch (error) {
|
|
293
|
+
this.logger.error(`Error force releasing lock for ${lockKey}: ${error.message}`, error.stack);
|
|
294
|
+
return false;
|
|
295
|
+
}
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
cleanupLocksByPattern(pattern_1) {
|
|
299
|
+
return __awaiter(this, arguments, void 0, function* (pattern, keyPrefix = 'lock') {
|
|
300
|
+
const redis = yield this.getRedis();
|
|
301
|
+
const searchPattern = `${keyPrefix}:${pattern}`;
|
|
302
|
+
try {
|
|
303
|
+
const keys = yield redis.keys(searchPattern);
|
|
304
|
+
if (keys.length === 0) {
|
|
305
|
+
this.logger.debug(`No locks found matching pattern: ${searchPattern}`);
|
|
306
|
+
return 0;
|
|
307
|
+
}
|
|
308
|
+
const result = yield redis.del(...keys);
|
|
309
|
+
this.logger.log(`Cleaned up ${result} lock(s) matching pattern: ${searchPattern}`);
|
|
310
|
+
return result;
|
|
311
|
+
}
|
|
312
|
+
catch (error) {
|
|
313
|
+
this.logger.error(`Error cleaning up locks by pattern ${searchPattern}: ${error.message}`, error.stack);
|
|
314
|
+
return 0;
|
|
315
|
+
}
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
cleanupOnStartup(patterns_1) {
|
|
319
|
+
return __awaiter(this, arguments, void 0, function* (patterns, keyPrefix = 'lock') {
|
|
320
|
+
this.logger.log(`Starting lock cleanup for ${patterns.length} pattern(s) on startup...`);
|
|
321
|
+
let totalDeleted = 0;
|
|
322
|
+
for (const pattern of patterns) {
|
|
323
|
+
const deleted = yield this.cleanupLocksByPattern(pattern, keyPrefix);
|
|
324
|
+
totalDeleted += deleted;
|
|
325
|
+
}
|
|
326
|
+
this.logger.log(`Startup lock cleanup completed. Deleted ${totalDeleted} lock(s)`);
|
|
327
|
+
return totalDeleted;
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
healthCheck() {
|
|
331
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
332
|
+
try {
|
|
333
|
+
const redis = yield this.getRedis();
|
|
334
|
+
const pong = yield redis.ping();
|
|
335
|
+
if (pong === 'PONG') {
|
|
336
|
+
return {
|
|
337
|
+
healthy: true,
|
|
338
|
+
redis: true,
|
|
339
|
+
message: 'Redis Lock Service is healthy',
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
else {
|
|
343
|
+
return {
|
|
344
|
+
healthy: false,
|
|
345
|
+
redis: false,
|
|
346
|
+
message: 'Redis ping returned unexpected response',
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
catch (error) {
|
|
351
|
+
return {
|
|
352
|
+
healthy: false,
|
|
353
|
+
redis: false,
|
|
354
|
+
message: `Redis Lock Service unhealthy: ${error.message}`,
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
getActiveLocks() {
|
|
360
|
+
return __awaiter(this, arguments, void 0, function* (pattern = '*', keyPrefix = 'lock') {
|
|
361
|
+
const redis = yield this.getRedis();
|
|
362
|
+
const searchPattern = `${keyPrefix}:${pattern}`;
|
|
363
|
+
try {
|
|
364
|
+
const keys = yield redis.keys(searchPattern);
|
|
365
|
+
if (keys.length === 0) {
|
|
366
|
+
return [];
|
|
367
|
+
}
|
|
368
|
+
const locks = yield Promise.all(keys.map((key) => __awaiter(this, void 0, void 0, function* () {
|
|
369
|
+
const [value, ttl] = yield Promise.all([
|
|
370
|
+
redis.get(key),
|
|
371
|
+
redis.pttl(key),
|
|
372
|
+
]);
|
|
373
|
+
const cleanKey = key.startsWith(`${keyPrefix}:`)
|
|
374
|
+
? key.substring(`${keyPrefix}:`.length)
|
|
375
|
+
: key;
|
|
376
|
+
return {
|
|
377
|
+
key: cleanKey,
|
|
378
|
+
value,
|
|
379
|
+
ttl: ttl >= 0 ? ttl : null,
|
|
380
|
+
};
|
|
381
|
+
})));
|
|
382
|
+
return locks.filter((lock) => lock.value !== null);
|
|
383
|
+
}
|
|
384
|
+
catch (error) {
|
|
385
|
+
this.logger.error(`Error getting active locks: ${error.message}`, error.stack);
|
|
386
|
+
return [];
|
|
387
|
+
}
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
getRedis() {
|
|
391
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
392
|
+
if (!this.redis) {
|
|
393
|
+
throw new Error('RedisLockService requires a Redis client to be set. Please provide a Redis client through the module configuration.');
|
|
394
|
+
}
|
|
395
|
+
return this.redis;
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
generateLockValue() {
|
|
399
|
+
return `${Date.now()}-${Math.random().toString(36).substring(2, 15)}-${process.pid}`;
|
|
400
|
+
}
|
|
401
|
+
buildLockKey(key, prefix = 'lock') {
|
|
402
|
+
return `${prefix}:${key}`;
|
|
403
|
+
}
|
|
404
|
+
sleep(ms) {
|
|
405
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
exports.RedisLockService = RedisLockService;
|
|
409
|
+
RedisLockService.globalInstance = null;
|
|
410
|
+
exports.RedisLockService = RedisLockService = RedisLockService_1 = __decorate([
|
|
411
|
+
(0, common_1.Injectable)(),
|
|
412
|
+
__metadata("design:paramtypes", [])
|
|
413
|
+
], RedisLockService);
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import type { NestExpressApplication } from '@nestjs/platform-express';
|
|
2
|
+
export declare function bootstrapSetup(AppModule: any, SetupSwagger: any): Promise<NestExpressApplication<import("http").Server<typeof import("http").IncomingMessage, typeof import("http").ServerResponse>>>;
|