@nathapp/nestjs-redis 1.3.3 → 1.3.4
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 +5 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/lock.constants.d.ts +1 -0
- package/dist/lock.constants.js +4 -0
- package/dist/lock.interface.d.ts +10 -0
- package/dist/lock.interface.js +2 -0
- package/dist/lock.service.d.ts +14 -0
- package/dist/lock.service.js +62 -0
- package/dist/redis.module.d.ts +14 -2
- package/dist/redis.module.js +59 -2
- package/package.json +18 -15
- package/.nvmrc +0 -1
package/README.md
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
|
|
2
2
|
# NOTES
|
|
3
|
-
this is fork from https://github.com/skunight/nestjs-redis and upgrade to nestjs 8
|
|
3
|
+
this is fork from https://github.com/skunight/nestjs-redis and upgrade to nestjs 8/9/10
|
|
4
|
+
|
|
5
|
+
This module has added RedisLockService by using 'redlock' to make use of redis distributed lock
|
|
6
|
+
|
|
7
|
+
module registration had a backwards compatibility of RedisModule.register(options) (RedisModuleOptions) or RedisModule.registerAsync(options) (RedisModuleAsyncOptions) which register default setting of RedisLockService, if you would like to change the lockSetting, please use RedisRegisterOptions or RedisRegisterAsyncOptions on module registration
|
|
4
8
|
|
|
5
9
|
# Nestjs Redis
|
|
6
10
|
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -17,3 +17,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("./redis.service"), exports);
|
|
18
18
|
__exportStar(require("./redis.module"), exports);
|
|
19
19
|
__exportStar(require("./redis.interface"), exports);
|
|
20
|
+
__exportStar(require("./lock.interface"), exports);
|
|
21
|
+
__exportStar(require("./lock.service"), exports);
|
|
22
|
+
__exportStar(require("./lock.constants"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const LOCK_SETTINGS: unique symbol;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Settings } from "redlock";
|
|
2
|
+
export interface LockSettings {
|
|
3
|
+
logging?: boolean;
|
|
4
|
+
settings?: Partial<Settings>;
|
|
5
|
+
scripts?: {
|
|
6
|
+
readonly acquireScript?: string | ((script: string) => string);
|
|
7
|
+
readonly extendScript?: string | ((script: string) => string);
|
|
8
|
+
readonly releaseScript?: string | ((script: string) => string);
|
|
9
|
+
};
|
|
10
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Logger, OnApplicationShutdown } from "@nestjs/common";
|
|
2
|
+
import Redlock from "redlock";
|
|
3
|
+
import { RedisService } from "./redis.service";
|
|
4
|
+
import { LockSettings } from "./lock.interface";
|
|
5
|
+
export declare class RedisLockService implements OnApplicationShutdown {
|
|
6
|
+
private readonly redisService;
|
|
7
|
+
protected readonly logger: Logger;
|
|
8
|
+
protected readonly redLock: Redlock;
|
|
9
|
+
protected readonly locks: {};
|
|
10
|
+
constructor(redisService: RedisService, lockSettings?: LockSettings);
|
|
11
|
+
onApplicationShutdown(signal?: string): Promise<void>;
|
|
12
|
+
acquireLock(name: string, expiryMs?: number): Promise<void>;
|
|
13
|
+
releaseLock(name: string): Promise<void>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
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 RedisLockService_1;
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.RedisLockService = void 0;
|
|
14
|
+
const common_1 = require("@nestjs/common");
|
|
15
|
+
const redlock_1 = require("redlock");
|
|
16
|
+
const redis_service_1 = require("./redis.service");
|
|
17
|
+
let RedisLockService = RedisLockService_1 = class RedisLockService {
|
|
18
|
+
constructor(redisService, lockSettings) {
|
|
19
|
+
this.redisService = redisService;
|
|
20
|
+
this.logger = new common_1.Logger(RedisLockService_1.name);
|
|
21
|
+
this.locks = {};
|
|
22
|
+
const defaultSettings = {
|
|
23
|
+
driftFactor: 0.01,
|
|
24
|
+
retryCount: 0,
|
|
25
|
+
retryDelay: 200,
|
|
26
|
+
retryJitter: 200,
|
|
27
|
+
automaticExtensionThreshold: 500,
|
|
28
|
+
};
|
|
29
|
+
const { settings = defaultSettings, scripts } = lockSettings;
|
|
30
|
+
this.redLock = new redlock_1.default([this.redisService.getClient()], settings, scripts);
|
|
31
|
+
}
|
|
32
|
+
async onApplicationShutdown(signal) {
|
|
33
|
+
if (this.redLock) {
|
|
34
|
+
await this.redLock.quit();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
async acquireLock(name, expiryMs = 60000) {
|
|
38
|
+
this.logger.log(`acquireLock redis Lock for ${name}`);
|
|
39
|
+
const lock = await this.redLock.acquire([name], expiryMs);
|
|
40
|
+
if (lock) {
|
|
41
|
+
this.locks[name] = lock;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
async releaseLock(name) {
|
|
45
|
+
this.logger.log(`releaseLock redis Lock for ${name}`);
|
|
46
|
+
if (this.locks.hasOwnProperty(name)) {
|
|
47
|
+
try {
|
|
48
|
+
const lock = this.locks[name];
|
|
49
|
+
await lock.release();
|
|
50
|
+
this.locks[name] = undefined;
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
this.logger.log(`releaseLock redis Lock for ${name} error:`, error);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
RedisLockService = RedisLockService_1 = __decorate([
|
|
59
|
+
(0, common_1.Injectable)(),
|
|
60
|
+
__metadata("design:paramtypes", [redis_service_1.RedisService, Object])
|
|
61
|
+
], RedisLockService);
|
|
62
|
+
exports.RedisLockService = RedisLockService;
|
package/dist/redis.module.d.ts
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
import { DynamicModule } from '@nestjs/common';
|
|
2
2
|
import { RedisModuleAsyncOptions, RedisModuleOptions } from './redis.interface';
|
|
3
|
+
import { LockSettings } from './lock.interface';
|
|
4
|
+
export declare type RedisRegisterOptions = {
|
|
5
|
+
redisOptions: RedisModuleOptions | RedisModuleOptions[];
|
|
6
|
+
lockSettings?: LockSettings;
|
|
7
|
+
};
|
|
8
|
+
export declare type RedisRegisterAsyncOptions = {
|
|
9
|
+
redisOptions: RedisModuleAsyncOptions;
|
|
10
|
+
lockSettings?: LockSettings;
|
|
11
|
+
};
|
|
3
12
|
export declare class RedisModule {
|
|
4
|
-
static register(options: RedisModuleOptions | RedisModuleOptions[]): DynamicModule;
|
|
5
|
-
static forRootAsync(options: RedisModuleAsyncOptions): DynamicModule;
|
|
13
|
+
static register(options: RedisRegisterOptions | RedisModuleOptions | RedisModuleOptions[]): DynamicModule;
|
|
14
|
+
static forRootAsync(options: RedisModuleAsyncOptions | RedisRegisterAsyncOptions): DynamicModule;
|
|
15
|
+
private static createDefaultLockService;
|
|
16
|
+
private static isRegisterOptions;
|
|
17
|
+
private static isRegisterAsyncOptions;
|
|
6
18
|
}
|
package/dist/redis.module.js
CHANGED
|
@@ -10,19 +10,76 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
10
10
|
exports.RedisModule = void 0;
|
|
11
11
|
const common_1 = require("@nestjs/common");
|
|
12
12
|
const redis_core_module_1 = require("./redis-core.module");
|
|
13
|
+
const lock_service_1 = require("./lock.service");
|
|
14
|
+
const redis_service_1 = require("./redis.service");
|
|
15
|
+
const lock_constants_1 = require("./lock.constants");
|
|
16
|
+
const defaultLockSetting = {
|
|
17
|
+
settings: {
|
|
18
|
+
driftFactor: 0.01,
|
|
19
|
+
retryCount: 0,
|
|
20
|
+
retryDelay: 200,
|
|
21
|
+
retryJitter: 200,
|
|
22
|
+
automaticExtensionThreshold: 500,
|
|
23
|
+
},
|
|
24
|
+
};
|
|
13
25
|
let RedisModule = RedisModule_1 = class RedisModule {
|
|
14
26
|
static register(options) {
|
|
27
|
+
let redisOptions;
|
|
28
|
+
let lockSettings;
|
|
29
|
+
if (this.isRegisterOptions(options)) {
|
|
30
|
+
redisOptions = options.redisOptions;
|
|
31
|
+
lockSettings = options.lockSettings;
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
redisOptions = options;
|
|
35
|
+
}
|
|
36
|
+
const lockSetting = {
|
|
37
|
+
provide: lock_constants_1.LOCK_SETTINGS,
|
|
38
|
+
useValue: lockSettings || defaultLockSetting,
|
|
39
|
+
};
|
|
40
|
+
const lockService = this.createDefaultLockService();
|
|
15
41
|
return {
|
|
16
42
|
module: RedisModule_1,
|
|
17
|
-
imports: [redis_core_module_1.RedisCoreModule.register(
|
|
43
|
+
imports: [redis_core_module_1.RedisCoreModule.register(redisOptions)],
|
|
44
|
+
providers: [lockSetting, lockService],
|
|
45
|
+
exports: [lockSetting, lockService]
|
|
18
46
|
};
|
|
19
47
|
}
|
|
20
48
|
static forRootAsync(options) {
|
|
49
|
+
let redisOptions;
|
|
50
|
+
let lockSettings;
|
|
51
|
+
if (this.isRegisterAsyncOptions(options)) {
|
|
52
|
+
redisOptions = options.redisOptions;
|
|
53
|
+
lockSettings = options.lockSettings;
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
redisOptions = options;
|
|
57
|
+
}
|
|
58
|
+
const lockSetting = {
|
|
59
|
+
provide: lock_constants_1.LOCK_SETTINGS,
|
|
60
|
+
useValue: lockSettings || defaultLockSetting,
|
|
61
|
+
};
|
|
62
|
+
const lockService = this.createDefaultLockService();
|
|
21
63
|
return {
|
|
22
64
|
module: RedisModule_1,
|
|
23
|
-
imports: [redis_core_module_1.RedisCoreModule.forRootAsync(
|
|
65
|
+
imports: [redis_core_module_1.RedisCoreModule.forRootAsync(redisOptions)],
|
|
66
|
+
providers: [lockSetting, lockService],
|
|
67
|
+
exports: [lockSetting, lockService]
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
static createDefaultLockService() {
|
|
71
|
+
return {
|
|
72
|
+
provide: lock_service_1.RedisLockService,
|
|
73
|
+
useFactory: (redisService, lockSetting) => new lock_service_1.RedisLockService(redisService, lockSetting),
|
|
74
|
+
inject: [redis_service_1.RedisService, lock_constants_1.LOCK_SETTINGS],
|
|
24
75
|
};
|
|
25
76
|
}
|
|
77
|
+
static isRegisterOptions(options) {
|
|
78
|
+
return 'redisOptions' in options || 'lockSettings' in options;
|
|
79
|
+
}
|
|
80
|
+
static isRegisterAsyncOptions(options) {
|
|
81
|
+
return 'redisOptions' in options || 'lockSettings' in options;
|
|
82
|
+
}
|
|
26
83
|
};
|
|
27
84
|
RedisModule = RedisModule_1 = __decorate([
|
|
28
85
|
(0, common_1.Module)({})
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nathapp/nestjs-redis",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.4",
|
|
4
4
|
"description": "a NestJS ioRedis module",
|
|
5
5
|
"author": "skunight",
|
|
6
6
|
"license": "MIT",
|
|
@@ -11,27 +11,22 @@
|
|
|
11
11
|
"scripts": {
|
|
12
12
|
"build": "rimraf dist && tsc -p tsconfig.json",
|
|
13
13
|
"precommit": "lint-staged",
|
|
14
|
-
"prepublish": "
|
|
15
|
-
"publish-public": "
|
|
14
|
+
"prepublish": "npm run build",
|
|
15
|
+
"publish-public": "npm publish --access public",
|
|
16
16
|
"test": "jest",
|
|
17
17
|
"test:watch": "jest --watch",
|
|
18
18
|
"test:cov": "jest --coverage",
|
|
19
19
|
"test:e2e": "jest --config ./test/jest-e2e.json",
|
|
20
20
|
"lint": "eslint -c .eslintrc.js"
|
|
21
21
|
},
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"@nestjs/core": "^8.0.0",
|
|
25
|
-
"@types/ioredis": "*",
|
|
26
|
-
"@types/uuid": "*",
|
|
27
|
-
"ioredis": "^4",
|
|
28
|
-
"reflect-metadata": "*",
|
|
29
|
-
"rxjs": "^7.2.0",
|
|
30
|
-
"uuid": "^8"
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=16"
|
|
31
24
|
},
|
|
32
25
|
"devDependencies": {
|
|
33
|
-
"@nestjs/testing": "^
|
|
26
|
+
"@nestjs/testing": "^10.0.0",
|
|
34
27
|
"@types/node": "*",
|
|
28
|
+
"@types/ioredis": "*",
|
|
29
|
+
"@types/uuid": "*",
|
|
35
30
|
"@typescript-eslint/eslint-plugin": "^4",
|
|
36
31
|
"@typescript-eslint/parser": "^4",
|
|
37
32
|
"cz-conventional-changelog": "^3",
|
|
@@ -39,12 +34,20 @@
|
|
|
39
34
|
"eslint-config-prettier": "^6",
|
|
40
35
|
"eslint-plugin-jest": "^24",
|
|
41
36
|
"eslint-plugin-prettier": "^3",
|
|
42
|
-
"jest": "^
|
|
37
|
+
"jest": "^29.5.0",
|
|
43
38
|
"prettier": "^2",
|
|
44
39
|
"rimraf": "*",
|
|
45
|
-
"ts-jest": "^
|
|
40
|
+
"ts-jest": "^29.1.0",
|
|
46
41
|
"typescript": "^4"
|
|
47
42
|
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"@nestjs/common": "^8.0.0 || ^9.0.0 || ^10.0.0",
|
|
45
|
+
"@nestjs/core": "^8.0.0 || ^9.0.0 || ^10.0.0",
|
|
46
|
+
"ioredis": "^4 || ^5",
|
|
47
|
+
"redlock": "^5.0.0-beta.2",
|
|
48
|
+
"rxjs": "*",
|
|
49
|
+
"uuid": "^8 || ^9"
|
|
50
|
+
},
|
|
48
51
|
"keywords": [
|
|
49
52
|
"nestjs",
|
|
50
53
|
"nest",
|
package/.nvmrc
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
v16.14.2
|