@xnestjs/ioredis 1.2.0 → 1.2.2
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/LICENSE +21 -0
- package/cjs/index.js +8 -0
- package/cjs/package.json +3 -0
- package/cjs/redis-client.js +59 -0
- package/cjs/redis-core.module.js +125 -0
- package/cjs/redis.constants.js +5 -0
- package/cjs/redis.interface.js +2 -0
- package/cjs/redis.module.js +25 -0
- package/cjs/shared-lock.js +72 -0
- package/cjs/types.js +2 -0
- package/cjs/utils.js +6 -0
- package/esm/index.js +5 -0
- package/esm/package.json +3 -0
- package/esm/redis-client.js +55 -0
- package/esm/redis-core.module.js +122 -0
- package/esm/redis.constants.js +2 -0
- package/esm/redis.interface.js +1 -0
- package/esm/redis.module.js +22 -0
- package/esm/shared-lock.js +69 -0
- package/esm/types.js +1 -0
- package/esm/utils.js +3 -0
- package/package.json +3 -21
- package/types/index.d.cts +5 -0
- package/types/index.d.ts +5 -0
- package/types/redis-client.d.ts +22 -0
- package/types/redis-core.module.d.ts +12 -0
- package/types/redis.constants.d.ts +2 -0
- package/types/redis.interface.d.ts +47 -0
- package/types/redis.module.d.ts +6 -0
- package/types/shared-lock.d.ts +3 -0
- package/types/types.d.ts +7 -0
- package/types/utils.d.ts +2 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Panates
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/cjs/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./redis.interface.js"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./redis.module.js"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./redis-client.js"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./shared-lock.js"), exports);
|
|
8
|
+
tslib_1.__exportStar(require("./types.js"), exports);
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RedisClient = void 0;
|
|
4
|
+
const redis_semaphore_1 = require("redis-semaphore");
|
|
5
|
+
const shared_lock_js_1 = require("./shared-lock.js");
|
|
6
|
+
class RedisClient {
|
|
7
|
+
constructor(options) {
|
|
8
|
+
this.locks = new Map();
|
|
9
|
+
this.cluster = options.cluster;
|
|
10
|
+
this.standalone = options.standalone;
|
|
11
|
+
if (!(this.cluster || this.standalone))
|
|
12
|
+
throw new TypeError('One of "cluster" or "standalone" must be set');
|
|
13
|
+
}
|
|
14
|
+
get isCluster() {
|
|
15
|
+
return !!this.cluster;
|
|
16
|
+
}
|
|
17
|
+
get redis() {
|
|
18
|
+
return (this.cluster || this.standalone);
|
|
19
|
+
}
|
|
20
|
+
quit() {
|
|
21
|
+
return this.redis.quit();
|
|
22
|
+
}
|
|
23
|
+
_getLock(kind, key) {
|
|
24
|
+
const lock = this.locks.get(key);
|
|
25
|
+
if (lock) {
|
|
26
|
+
if (lock.refCount > 0) {
|
|
27
|
+
if (lock._kind !== kind)
|
|
28
|
+
throw new Error(`Lock "${key}" is already in use by a different kind of lock (${lock._kind})`);
|
|
29
|
+
return lock;
|
|
30
|
+
}
|
|
31
|
+
this.locks.delete(key);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
obtainMutex(key, options) {
|
|
35
|
+
let lock = this._getLock('mutex', key);
|
|
36
|
+
if (lock)
|
|
37
|
+
return lock;
|
|
38
|
+
lock = (0, shared_lock_js_1.sharedLock)(new redis_semaphore_1.Mutex(this.redis, key, options));
|
|
39
|
+
this.locks.set(key, lock);
|
|
40
|
+
return lock;
|
|
41
|
+
}
|
|
42
|
+
obtainSemaphore(key, limit, options) {
|
|
43
|
+
let lock = this._getLock('semaphore', key);
|
|
44
|
+
if (lock)
|
|
45
|
+
return lock;
|
|
46
|
+
lock = (0, shared_lock_js_1.sharedLock)(new redis_semaphore_1.Semaphore(this.redis, key, limit, options));
|
|
47
|
+
this.locks.set(key, lock);
|
|
48
|
+
return lock;
|
|
49
|
+
}
|
|
50
|
+
obtainMultiSemaphore(key, limit, options) {
|
|
51
|
+
let lock = this._getLock('multi-semaphore', key);
|
|
52
|
+
if (lock)
|
|
53
|
+
return lock;
|
|
54
|
+
lock = (0, shared_lock_js_1.sharedLock)(new redis_semaphore_1.Semaphore(this.redis, key, limit, options));
|
|
55
|
+
this.locks.set(key, lock);
|
|
56
|
+
return lock;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
exports.RedisClient = RedisClient;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var RedisCoreModule_1;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.RedisCoreModule = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const common_1 = require("@nestjs/common");
|
|
7
|
+
const core_1 = require("@nestjs/core");
|
|
8
|
+
const crypto = tslib_1.__importStar(require("crypto"));
|
|
9
|
+
const ioredis_1 = tslib_1.__importStar(require("ioredis"));
|
|
10
|
+
const redis_constants_js_1 = require("./redis.constants.js");
|
|
11
|
+
const redis_client_js_1 = require("./redis-client.js");
|
|
12
|
+
const utils_js_1 = require("./utils.js");
|
|
13
|
+
let RedisCoreModule = RedisCoreModule_1 = class RedisCoreModule {
|
|
14
|
+
constructor(options, moduleRef) {
|
|
15
|
+
this.options = options;
|
|
16
|
+
this.moduleRef = moduleRef;
|
|
17
|
+
}
|
|
18
|
+
static forRoot(options) {
|
|
19
|
+
const optionsProvider = {
|
|
20
|
+
provide: redis_constants_js_1.IOREDIS_MODULE_OPTIONS,
|
|
21
|
+
useValue: options,
|
|
22
|
+
};
|
|
23
|
+
const token = options.token || redis_client_js_1.RedisClient;
|
|
24
|
+
const connectionProvider = {
|
|
25
|
+
provide: token,
|
|
26
|
+
useFactory: () => this._createClient(options),
|
|
27
|
+
};
|
|
28
|
+
return {
|
|
29
|
+
module: RedisCoreModule_1,
|
|
30
|
+
providers: [connectionProvider, optionsProvider],
|
|
31
|
+
exports: [connectionProvider],
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
static forRootAsync(asyncOptions) {
|
|
35
|
+
if (!asyncOptions.useFactory)
|
|
36
|
+
throw new Error('Invalid configuration. Must provide "useFactory"');
|
|
37
|
+
const token = asyncOptions.token || redis_client_js_1.RedisClient;
|
|
38
|
+
const connectionProvider = {
|
|
39
|
+
provide: token,
|
|
40
|
+
inject: [redis_constants_js_1.IOREDIS_MODULE_OPTIONS],
|
|
41
|
+
useFactory: async (moduleOptions) => this._createClient(moduleOptions),
|
|
42
|
+
};
|
|
43
|
+
return {
|
|
44
|
+
module: RedisCoreModule_1,
|
|
45
|
+
imports: asyncOptions.imports,
|
|
46
|
+
providers: [
|
|
47
|
+
{
|
|
48
|
+
provide: redis_constants_js_1.IOREDIS_MODULE_OPTIONS,
|
|
49
|
+
useFactory: asyncOptions.useFactory,
|
|
50
|
+
inject: asyncOptions.inject || [],
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
provide: redis_constants_js_1.IOREDIS_MODULE_TOKEN,
|
|
54
|
+
useValue: crypto.randomUUID(),
|
|
55
|
+
},
|
|
56
|
+
connectionProvider,
|
|
57
|
+
],
|
|
58
|
+
exports: [connectionProvider],
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
static async _createClient(options) {
|
|
62
|
+
if (options.host && options.nodes) {
|
|
63
|
+
throw new TypeError(`You should set either "host" or "nodes", not both`);
|
|
64
|
+
}
|
|
65
|
+
const opts = { ...options };
|
|
66
|
+
const isCluster = (0, utils_js_1.isClusterOptions)(opts);
|
|
67
|
+
let client;
|
|
68
|
+
if (isCluster) {
|
|
69
|
+
delete opts.name;
|
|
70
|
+
delete opts.nodes;
|
|
71
|
+
const cluster = new ioredis_1.Cluster(opts.nodes, opts);
|
|
72
|
+
client = new redis_client_js_1.RedisClient({ cluster });
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
if (options.host && options.host.includes('://')) {
|
|
76
|
+
const url = new URL(options.host);
|
|
77
|
+
options.host = url.hostname;
|
|
78
|
+
if (url.port)
|
|
79
|
+
options.port = parseInt(url.port, 10);
|
|
80
|
+
if (url.username)
|
|
81
|
+
options.username = url.username;
|
|
82
|
+
if (url.password)
|
|
83
|
+
options.password = url.password;
|
|
84
|
+
if (url.protocol === 'rediss:') {
|
|
85
|
+
// @ts-ignore
|
|
86
|
+
options.tls = true;
|
|
87
|
+
}
|
|
88
|
+
const db = parseInt(url.pathname.substring(1), 10);
|
|
89
|
+
if (db > 0)
|
|
90
|
+
options.db = db;
|
|
91
|
+
}
|
|
92
|
+
const standalone = new ioredis_1.default(options);
|
|
93
|
+
client = new redis_client_js_1.RedisClient({ standalone });
|
|
94
|
+
}
|
|
95
|
+
if (!options.lazyConnect) {
|
|
96
|
+
await new Promise((resolve, reject) => {
|
|
97
|
+
client.redis.once('ready', () => {
|
|
98
|
+
client.redis.removeListener('error', reject);
|
|
99
|
+
resolve();
|
|
100
|
+
});
|
|
101
|
+
client.redis.once('error', e => {
|
|
102
|
+
client.redis.removeListener('ready', resolve);
|
|
103
|
+
reject(e);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
return client;
|
|
108
|
+
}
|
|
109
|
+
async onApplicationShutdown() {
|
|
110
|
+
try {
|
|
111
|
+
const client = this.moduleRef.get(this.options.token || redis_client_js_1.RedisClient);
|
|
112
|
+
await client.quit();
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
//
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
exports.RedisCoreModule = RedisCoreModule;
|
|
120
|
+
exports.RedisCoreModule = RedisCoreModule = RedisCoreModule_1 = tslib_1.__decorate([
|
|
121
|
+
(0, common_1.Global)(),
|
|
122
|
+
(0, common_1.Module)({}),
|
|
123
|
+
tslib_1.__param(0, (0, common_1.Inject)(redis_constants_js_1.IOREDIS_MODULE_OPTIONS)),
|
|
124
|
+
tslib_1.__metadata("design:paramtypes", [Object, core_1.ModuleRef])
|
|
125
|
+
], RedisCoreModule);
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IOREDIS_MODULE_TOKEN = exports.IOREDIS_MODULE_OPTIONS = void 0;
|
|
4
|
+
exports.IOREDIS_MODULE_OPTIONS = Symbol('IOREDIS_MODULE_OPTIONS');
|
|
5
|
+
exports.IOREDIS_MODULE_TOKEN = Symbol('IOREDIS_MODULE_ID');
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var RedisModule_1;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.RedisModule = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const common_1 = require("@nestjs/common");
|
|
7
|
+
const redis_core_module_js_1 = require("./redis-core.module.js");
|
|
8
|
+
let RedisModule = RedisModule_1 = class RedisModule {
|
|
9
|
+
static forRoot(options) {
|
|
10
|
+
return {
|
|
11
|
+
module: RedisModule_1,
|
|
12
|
+
imports: [redis_core_module_js_1.RedisCoreModule.forRoot(options)],
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
static forRootAsync(options) {
|
|
16
|
+
return {
|
|
17
|
+
module: RedisModule_1,
|
|
18
|
+
imports: [redis_core_module_js_1.RedisCoreModule.forRootAsync(options)],
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
exports.RedisModule = RedisModule;
|
|
23
|
+
exports.RedisModule = RedisModule = RedisModule_1 = tslib_1.__decorate([
|
|
24
|
+
(0, common_1.Module)({})
|
|
25
|
+
], RedisModule);
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sharedLock = sharedLock;
|
|
4
|
+
function sharedLock(lock) {
|
|
5
|
+
let _this;
|
|
6
|
+
const _sharedLock = (_this = {
|
|
7
|
+
get refCount() {
|
|
8
|
+
return _this._refCount;
|
|
9
|
+
},
|
|
10
|
+
async acquire(callback) {
|
|
11
|
+
if (callback) {
|
|
12
|
+
await this.acquire();
|
|
13
|
+
try {
|
|
14
|
+
return await callback();
|
|
15
|
+
}
|
|
16
|
+
finally {
|
|
17
|
+
await this.release();
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
if (_this._refCount <= 0) {
|
|
21
|
+
_this._refCount = 1;
|
|
22
|
+
await lock.acquire();
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
_this._refCount++;
|
|
26
|
+
},
|
|
27
|
+
async tryAcquire(callback) {
|
|
28
|
+
if (callback) {
|
|
29
|
+
if (!(await this.tryAcquire()))
|
|
30
|
+
return false;
|
|
31
|
+
try {
|
|
32
|
+
return await callback();
|
|
33
|
+
}
|
|
34
|
+
finally {
|
|
35
|
+
await this.release();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (_this._refCount <= 0) {
|
|
39
|
+
_this._refCount = 1;
|
|
40
|
+
if (await lock.tryAcquire())
|
|
41
|
+
return true;
|
|
42
|
+
_this._refCount = -1;
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
_this._refCount++;
|
|
46
|
+
return true;
|
|
47
|
+
},
|
|
48
|
+
async release(force) {
|
|
49
|
+
if (_this._refCount <= 0)
|
|
50
|
+
return;
|
|
51
|
+
if (force || _this._refCount === 1) {
|
|
52
|
+
try {
|
|
53
|
+
_this._refCount = -1;
|
|
54
|
+
await lock.release();
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
// do nothing
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
_this._refCount--;
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
Object.setPrototypeOf(_sharedLock, lock);
|
|
65
|
+
Object.defineProperty(_sharedLock, '_refCount', {
|
|
66
|
+
value: 0,
|
|
67
|
+
writable: true,
|
|
68
|
+
enumerable: false,
|
|
69
|
+
configurable: false,
|
|
70
|
+
});
|
|
71
|
+
return lock;
|
|
72
|
+
}
|
package/cjs/types.js
ADDED
package/cjs/utils.js
ADDED
package/esm/index.js
ADDED
package/esm/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { Mutex, Semaphore } from 'redis-semaphore';
|
|
2
|
+
import { sharedLock } from './shared-lock.js';
|
|
3
|
+
export class RedisClient {
|
|
4
|
+
constructor(options) {
|
|
5
|
+
this.locks = new Map();
|
|
6
|
+
this.cluster = options.cluster;
|
|
7
|
+
this.standalone = options.standalone;
|
|
8
|
+
if (!(this.cluster || this.standalone))
|
|
9
|
+
throw new TypeError('One of "cluster" or "standalone" must be set');
|
|
10
|
+
}
|
|
11
|
+
get isCluster() {
|
|
12
|
+
return !!this.cluster;
|
|
13
|
+
}
|
|
14
|
+
get redis() {
|
|
15
|
+
return (this.cluster || this.standalone);
|
|
16
|
+
}
|
|
17
|
+
quit() {
|
|
18
|
+
return this.redis.quit();
|
|
19
|
+
}
|
|
20
|
+
_getLock(kind, key) {
|
|
21
|
+
const lock = this.locks.get(key);
|
|
22
|
+
if (lock) {
|
|
23
|
+
if (lock.refCount > 0) {
|
|
24
|
+
if (lock._kind !== kind)
|
|
25
|
+
throw new Error(`Lock "${key}" is already in use by a different kind of lock (${lock._kind})`);
|
|
26
|
+
return lock;
|
|
27
|
+
}
|
|
28
|
+
this.locks.delete(key);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
obtainMutex(key, options) {
|
|
32
|
+
let lock = this._getLock('mutex', key);
|
|
33
|
+
if (lock)
|
|
34
|
+
return lock;
|
|
35
|
+
lock = sharedLock(new Mutex(this.redis, key, options));
|
|
36
|
+
this.locks.set(key, lock);
|
|
37
|
+
return lock;
|
|
38
|
+
}
|
|
39
|
+
obtainSemaphore(key, limit, options) {
|
|
40
|
+
let lock = this._getLock('semaphore', key);
|
|
41
|
+
if (lock)
|
|
42
|
+
return lock;
|
|
43
|
+
lock = sharedLock(new Semaphore(this.redis, key, limit, options));
|
|
44
|
+
this.locks.set(key, lock);
|
|
45
|
+
return lock;
|
|
46
|
+
}
|
|
47
|
+
obtainMultiSemaphore(key, limit, options) {
|
|
48
|
+
let lock = this._getLock('multi-semaphore', key);
|
|
49
|
+
if (lock)
|
|
50
|
+
return lock;
|
|
51
|
+
lock = sharedLock(new Semaphore(this.redis, key, limit, options));
|
|
52
|
+
this.locks.set(key, lock);
|
|
53
|
+
return lock;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
var RedisCoreModule_1;
|
|
2
|
+
import { __decorate, __metadata, __param } from "tslib";
|
|
3
|
+
import { Global, Inject, Module } from '@nestjs/common';
|
|
4
|
+
import { ModuleRef } from '@nestjs/core';
|
|
5
|
+
import * as crypto from 'crypto';
|
|
6
|
+
import Redis, { Cluster } from 'ioredis';
|
|
7
|
+
import { IOREDIS_MODULE_OPTIONS, IOREDIS_MODULE_TOKEN } from './redis.constants.js';
|
|
8
|
+
import { RedisClient } from './redis-client.js';
|
|
9
|
+
import { isClusterOptions } from './utils.js';
|
|
10
|
+
let RedisCoreModule = RedisCoreModule_1 = class RedisCoreModule {
|
|
11
|
+
constructor(options, moduleRef) {
|
|
12
|
+
this.options = options;
|
|
13
|
+
this.moduleRef = moduleRef;
|
|
14
|
+
}
|
|
15
|
+
static forRoot(options) {
|
|
16
|
+
const optionsProvider = {
|
|
17
|
+
provide: IOREDIS_MODULE_OPTIONS,
|
|
18
|
+
useValue: options,
|
|
19
|
+
};
|
|
20
|
+
const token = options.token || RedisClient;
|
|
21
|
+
const connectionProvider = {
|
|
22
|
+
provide: token,
|
|
23
|
+
useFactory: () => this._createClient(options),
|
|
24
|
+
};
|
|
25
|
+
return {
|
|
26
|
+
module: RedisCoreModule_1,
|
|
27
|
+
providers: [connectionProvider, optionsProvider],
|
|
28
|
+
exports: [connectionProvider],
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
static forRootAsync(asyncOptions) {
|
|
32
|
+
if (!asyncOptions.useFactory)
|
|
33
|
+
throw new Error('Invalid configuration. Must provide "useFactory"');
|
|
34
|
+
const token = asyncOptions.token || RedisClient;
|
|
35
|
+
const connectionProvider = {
|
|
36
|
+
provide: token,
|
|
37
|
+
inject: [IOREDIS_MODULE_OPTIONS],
|
|
38
|
+
useFactory: async (moduleOptions) => this._createClient(moduleOptions),
|
|
39
|
+
};
|
|
40
|
+
return {
|
|
41
|
+
module: RedisCoreModule_1,
|
|
42
|
+
imports: asyncOptions.imports,
|
|
43
|
+
providers: [
|
|
44
|
+
{
|
|
45
|
+
provide: IOREDIS_MODULE_OPTIONS,
|
|
46
|
+
useFactory: asyncOptions.useFactory,
|
|
47
|
+
inject: asyncOptions.inject || [],
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
provide: IOREDIS_MODULE_TOKEN,
|
|
51
|
+
useValue: crypto.randomUUID(),
|
|
52
|
+
},
|
|
53
|
+
connectionProvider,
|
|
54
|
+
],
|
|
55
|
+
exports: [connectionProvider],
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
static async _createClient(options) {
|
|
59
|
+
if (options.host && options.nodes) {
|
|
60
|
+
throw new TypeError(`You should set either "host" or "nodes", not both`);
|
|
61
|
+
}
|
|
62
|
+
const opts = { ...options };
|
|
63
|
+
const isCluster = isClusterOptions(opts);
|
|
64
|
+
let client;
|
|
65
|
+
if (isCluster) {
|
|
66
|
+
delete opts.name;
|
|
67
|
+
delete opts.nodes;
|
|
68
|
+
const cluster = new Cluster(opts.nodes, opts);
|
|
69
|
+
client = new RedisClient({ cluster });
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
if (options.host && options.host.includes('://')) {
|
|
73
|
+
const url = new URL(options.host);
|
|
74
|
+
options.host = url.hostname;
|
|
75
|
+
if (url.port)
|
|
76
|
+
options.port = parseInt(url.port, 10);
|
|
77
|
+
if (url.username)
|
|
78
|
+
options.username = url.username;
|
|
79
|
+
if (url.password)
|
|
80
|
+
options.password = url.password;
|
|
81
|
+
if (url.protocol === 'rediss:') {
|
|
82
|
+
// @ts-ignore
|
|
83
|
+
options.tls = true;
|
|
84
|
+
}
|
|
85
|
+
const db = parseInt(url.pathname.substring(1), 10);
|
|
86
|
+
if (db > 0)
|
|
87
|
+
options.db = db;
|
|
88
|
+
}
|
|
89
|
+
const standalone = new Redis(options);
|
|
90
|
+
client = new RedisClient({ standalone });
|
|
91
|
+
}
|
|
92
|
+
if (!options.lazyConnect) {
|
|
93
|
+
await new Promise((resolve, reject) => {
|
|
94
|
+
client.redis.once('ready', () => {
|
|
95
|
+
client.redis.removeListener('error', reject);
|
|
96
|
+
resolve();
|
|
97
|
+
});
|
|
98
|
+
client.redis.once('error', e => {
|
|
99
|
+
client.redis.removeListener('ready', resolve);
|
|
100
|
+
reject(e);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
return client;
|
|
105
|
+
}
|
|
106
|
+
async onApplicationShutdown() {
|
|
107
|
+
try {
|
|
108
|
+
const client = this.moduleRef.get(this.options.token || RedisClient);
|
|
109
|
+
await client.quit();
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
//
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
RedisCoreModule = RedisCoreModule_1 = __decorate([
|
|
117
|
+
Global(),
|
|
118
|
+
Module({}),
|
|
119
|
+
__param(0, Inject(IOREDIS_MODULE_OPTIONS)),
|
|
120
|
+
__metadata("design:paramtypes", [Object, ModuleRef])
|
|
121
|
+
], RedisCoreModule);
|
|
122
|
+
export { RedisCoreModule };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
var RedisModule_1;
|
|
2
|
+
import { __decorate } from "tslib";
|
|
3
|
+
import { Module } from '@nestjs/common';
|
|
4
|
+
import { RedisCoreModule } from './redis-core.module.js';
|
|
5
|
+
let RedisModule = RedisModule_1 = class RedisModule {
|
|
6
|
+
static forRoot(options) {
|
|
7
|
+
return {
|
|
8
|
+
module: RedisModule_1,
|
|
9
|
+
imports: [RedisCoreModule.forRoot(options)],
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
static forRootAsync(options) {
|
|
13
|
+
return {
|
|
14
|
+
module: RedisModule_1,
|
|
15
|
+
imports: [RedisCoreModule.forRootAsync(options)],
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
RedisModule = RedisModule_1 = __decorate([
|
|
20
|
+
Module({})
|
|
21
|
+
], RedisModule);
|
|
22
|
+
export { RedisModule };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
export function sharedLock(lock) {
|
|
2
|
+
let _this;
|
|
3
|
+
const _sharedLock = (_this = {
|
|
4
|
+
get refCount() {
|
|
5
|
+
return _this._refCount;
|
|
6
|
+
},
|
|
7
|
+
async acquire(callback) {
|
|
8
|
+
if (callback) {
|
|
9
|
+
await this.acquire();
|
|
10
|
+
try {
|
|
11
|
+
return await callback();
|
|
12
|
+
}
|
|
13
|
+
finally {
|
|
14
|
+
await this.release();
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
if (_this._refCount <= 0) {
|
|
18
|
+
_this._refCount = 1;
|
|
19
|
+
await lock.acquire();
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
_this._refCount++;
|
|
23
|
+
},
|
|
24
|
+
async tryAcquire(callback) {
|
|
25
|
+
if (callback) {
|
|
26
|
+
if (!(await this.tryAcquire()))
|
|
27
|
+
return false;
|
|
28
|
+
try {
|
|
29
|
+
return await callback();
|
|
30
|
+
}
|
|
31
|
+
finally {
|
|
32
|
+
await this.release();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (_this._refCount <= 0) {
|
|
36
|
+
_this._refCount = 1;
|
|
37
|
+
if (await lock.tryAcquire())
|
|
38
|
+
return true;
|
|
39
|
+
_this._refCount = -1;
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
_this._refCount++;
|
|
43
|
+
return true;
|
|
44
|
+
},
|
|
45
|
+
async release(force) {
|
|
46
|
+
if (_this._refCount <= 0)
|
|
47
|
+
return;
|
|
48
|
+
if (force || _this._refCount === 1) {
|
|
49
|
+
try {
|
|
50
|
+
_this._refCount = -1;
|
|
51
|
+
await lock.release();
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
// do nothing
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
_this._refCount--;
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
Object.setPrototypeOf(_sharedLock, lock);
|
|
62
|
+
Object.defineProperty(_sharedLock, '_refCount', {
|
|
63
|
+
value: 0,
|
|
64
|
+
writable: true,
|
|
65
|
+
enumerable: false,
|
|
66
|
+
configurable: false,
|
|
67
|
+
});
|
|
68
|
+
return lock;
|
|
69
|
+
}
|
package/esm/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/esm/utils.js
ADDED
package/package.json
CHANGED
|
@@ -1,26 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xnestjs/ioredis",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.2",
|
|
4
4
|
"description": "NestJS extension library for ioredis",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
"prebuild": "npm run lint && npm run clean",
|
|
10
|
-
"build": "npm run build:cjs && npm run build:esm",
|
|
11
|
-
"build:cjs": "tsc -b tsconfig-build-cjs.json && cp ../../support/package.cjs.json ./build/cjs/package.json",
|
|
12
|
-
"build:esm": "tsc -b tsconfig-build-esm.json && cp ../../support/package.esm.json ./build/esm/package.json",
|
|
13
|
-
"postbuild": "cp README.md ../../LICENSE ./build && node ../../support/postbuild.cjs",
|
|
14
|
-
"lint": "eslint . --max-warnings=0",
|
|
15
|
-
"lint:fix": "eslint . --max-warnings=0 --fix",
|
|
16
|
-
"format": "prettier . --write --log-level=warn",
|
|
17
|
-
"check": "madge --circular src/**",
|
|
18
|
-
"test": "jest",
|
|
19
|
-
"cover": "jest --collect-coverage",
|
|
20
|
-
"clean": "npm run clean:src && npm run clean:build && npm run clean:cover",
|
|
21
|
-
"clean:src": "ts-cleanup -s src --all && ts-cleanup -s test --all",
|
|
22
|
-
"clean:build": "rimraf build",
|
|
23
|
-
"clean:cover": "rimraf coverage"
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"tslib": "^2.8.1"
|
|
24
9
|
},
|
|
25
10
|
"peerDependencies": {
|
|
26
11
|
"@nestjs/common": "^10.0.0 || ^11.0.0",
|
|
@@ -28,9 +13,6 @@
|
|
|
28
13
|
"ioredis": "^5.0.0",
|
|
29
14
|
"redis-semaphore": "^5.0.0"
|
|
30
15
|
},
|
|
31
|
-
"devDependencies": {
|
|
32
|
-
"@nestjs/testing": "^11.0.2"
|
|
33
|
-
},
|
|
34
16
|
"type": "module",
|
|
35
17
|
"exports": {
|
|
36
18
|
".": {
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Cluster as IORedisCluster, Redis as IORedisClient } from 'ioredis';
|
|
2
|
+
import { type LockOptions } from 'redis-semaphore';
|
|
3
|
+
import type { SharedLock } from './types.js';
|
|
4
|
+
export declare namespace RedisClient {
|
|
5
|
+
interface Options {
|
|
6
|
+
cluster?: IORedisCluster;
|
|
7
|
+
standalone?: IORedisClient;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export declare class RedisClient {
|
|
11
|
+
private locks;
|
|
12
|
+
cluster?: IORedisCluster;
|
|
13
|
+
standalone?: IORedisClient;
|
|
14
|
+
constructor(options: RedisClient.Options);
|
|
15
|
+
get isCluster(): boolean;
|
|
16
|
+
get redis(): IORedisCluster | IORedisClient;
|
|
17
|
+
quit(): Promise<"OK">;
|
|
18
|
+
protected _getLock(kind: string, key: string): SharedLock | undefined;
|
|
19
|
+
obtainMutex(key: string, options?: LockOptions): SharedLock;
|
|
20
|
+
obtainSemaphore(key: string, limit: number, options?: LockOptions): SharedLock;
|
|
21
|
+
obtainMultiSemaphore(key: string, limit: number, options?: LockOptions): SharedLock;
|
|
22
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { DynamicModule, OnApplicationShutdown } from '@nestjs/common';
|
|
2
|
+
import { ModuleRef } from '@nestjs/core';
|
|
3
|
+
import { RedisClientAsyncOptions, RedisClientOptions, RedisClusterAsyncOptions, RedisClusterOptions } from './redis.interface.js';
|
|
4
|
+
export declare class RedisCoreModule implements OnApplicationShutdown {
|
|
5
|
+
private readonly options;
|
|
6
|
+
private readonly moduleRef;
|
|
7
|
+
constructor(options: RedisClientOptions | RedisClusterOptions, moduleRef: ModuleRef);
|
|
8
|
+
static forRoot(options: RedisClientOptions | RedisClusterOptions): DynamicModule;
|
|
9
|
+
static forRootAsync(asyncOptions: RedisClientAsyncOptions | RedisClusterAsyncOptions): DynamicModule;
|
|
10
|
+
private static _createClient;
|
|
11
|
+
onApplicationShutdown(): Promise<void>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { ModuleMetadata } from '@nestjs/common/interfaces';
|
|
2
|
+
import type { RedisOptions } from 'ioredis';
|
|
3
|
+
import type { ClusterOptions } from 'ioredis/built/cluster/ClusterOptions';
|
|
4
|
+
import type { ClusterNode } from 'ioredis/built/cluster/index.js';
|
|
5
|
+
export interface RedisClientOptions extends RedisOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Injection token
|
|
8
|
+
*/
|
|
9
|
+
token?: any;
|
|
10
|
+
}
|
|
11
|
+
export interface RedisClusterOptions extends ClusterOptions {
|
|
12
|
+
/**
|
|
13
|
+
* Injection token
|
|
14
|
+
*/
|
|
15
|
+
token?: any;
|
|
16
|
+
nodes: ClusterNode[];
|
|
17
|
+
}
|
|
18
|
+
export interface RedisClientAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
|
|
19
|
+
/**
|
|
20
|
+
* Injection token
|
|
21
|
+
*/
|
|
22
|
+
token?: any;
|
|
23
|
+
useFactory?: (...args: any[]) => Promise<RedisClientOptions> | RedisClientOptions;
|
|
24
|
+
inject?: any[];
|
|
25
|
+
}
|
|
26
|
+
export interface RedisClusterAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
|
|
27
|
+
/**
|
|
28
|
+
* Injection token
|
|
29
|
+
*/
|
|
30
|
+
token?: any;
|
|
31
|
+
useFactory?: (...args: any[]) => Promise<RedisClusterOptions> | RedisClusterOptions;
|
|
32
|
+
inject?: any[];
|
|
33
|
+
}
|
|
34
|
+
export interface LockSettings {
|
|
35
|
+
/**
|
|
36
|
+
* This parameter is only used if lock has been acquired without leaseTimeout parameter definition.
|
|
37
|
+
* Lock expires after `lockWatchdogTimeout` if watchdog
|
|
38
|
+
* didn't extend it to next `lockWatchdogTimeout` time interval.
|
|
39
|
+
*
|
|
40
|
+
* This prevents against infinity locked locks due to Redisson client crush or
|
|
41
|
+
* any other reason when lock can't be released in proper way.
|
|
42
|
+
*
|
|
43
|
+
* - Unit: milliseconds
|
|
44
|
+
* - Default: 30000 milliseconds
|
|
45
|
+
*/
|
|
46
|
+
lockWatchdogTimeout?: bigint;
|
|
47
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { DynamicModule } from '@nestjs/common';
|
|
2
|
+
import { RedisClientAsyncOptions, RedisClientOptions, RedisClusterAsyncOptions, RedisClusterOptions } from './redis.interface.js';
|
|
3
|
+
export declare class RedisModule {
|
|
4
|
+
static forRoot(options: RedisClientOptions | RedisClusterOptions): DynamicModule;
|
|
5
|
+
static forRootAsync(options: RedisClientAsyncOptions | RedisClusterAsyncOptions): DynamicModule;
|
|
6
|
+
}
|
package/types/types.d.ts
ADDED
package/types/utils.d.ts
ADDED