@xnestjs/ioredis 1.12.2 → 1.13.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xnestjs/ioredis",
3
- "version": "1.12.2",
3
+ "version": "1.13.0",
4
4
  "description": "NestJS extension library for ioredis",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -17,38 +17,22 @@
17
17
  "redis-semaphore": ">=5.6.0 <6"
18
18
  },
19
19
  "type": "module",
20
+ "module": "./index.js",
21
+ "types": "./index.d.ts",
20
22
  "exports": {
21
23
  ".": {
22
- "import": {
23
- "types": "./types/index.d.ts",
24
- "default": "./esm/index.js"
25
- },
26
- "require": {
27
- "types": "./types/index.d.cts",
28
- "default": "./cjs/index.js"
29
- },
30
- "default": "./esm/index.js"
24
+ "types": "./index.d.ts",
25
+ "default": "./index.js"
31
26
  },
32
27
  "./package.json": "./package.json"
33
28
  },
34
- "main": "./cjs/index.js",
35
- "module": "./esm/index.js",
36
- "types": "./types/index.d.ts",
29
+ "engines": {
30
+ "node": ">=20.0"
31
+ },
37
32
  "repository": {
38
33
  "registry": "https://github.com/panates/xnestjs.git",
39
34
  "directory": "packages/ioredis"
40
35
  },
41
- "engines": {
42
- "node": ">=16.0",
43
- "npm": ">=7.0.0"
44
- },
45
- "files": [
46
- "cjs/",
47
- "esm/",
48
- "types/",
49
- "LICENSE",
50
- "README.md"
51
- ],
52
36
  "keywords": [
53
37
  "nestjs",
54
38
  "ioredis",
@@ -1,8 +1,10 @@
1
1
  import { Mutex, Semaphore } from 'redis-semaphore';
2
2
  import { sharedLock } from './shared-lock.js';
3
3
  export class RedisClient {
4
+ locks = new Map();
5
+ cluster;
6
+ standalone;
4
7
  constructor(options) {
5
- this.locks = new Map();
6
8
  this.cluster = options.cluster;
7
9
  this.standalone = options.standalone;
8
10
  if (!(this.cluster || this.standalone))
@@ -11,6 +11,9 @@ import { RedisClient } from './redis-client.js';
11
11
  import { isClusterOptions, isStandaloneOptions } from './utils.js';
12
12
  const CLIENT_TOKEN = Symbol('CLIENT_TOKEN');
13
13
  let RedisCoreModule = RedisCoreModule_1 = class RedisCoreModule {
14
+ client;
15
+ connectionOptions;
16
+ logger;
14
17
  static forRoot(moduleOptions) {
15
18
  const connectionOptions = getRedisConfig(moduleOptions.useValue, moduleOptions.envPrefix);
16
19
  return this._createDynamicModule(moduleOptions, {
package/cjs/constants.js DELETED
@@ -1,5 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.IOREDIS_MODULE_TOKEN = exports.IOREDIS_CONNECTION_OPTIONS = void 0;
4
- exports.IOREDIS_CONNECTION_OPTIONS = Symbol('IOREDIS_CONNECTION_OPTIONS');
5
- exports.IOREDIS_MODULE_TOKEN = Symbol('IOREDIS_MODULE_ID');
@@ -1,55 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getRedisConfig = getRedisConfig;
4
- const tslib_1 = require("tslib");
5
- const node_process_1 = tslib_1.__importDefault(require("node:process"));
6
- const objects_1 = require("@jsopen/objects");
7
- const putil_varhelpers_1 = require("putil-varhelpers");
8
- const utils_js_1 = require("./utils.js");
9
- function getRedisConfig(options, prefix = 'REDIS_') {
10
- const env = node_process_1.default.env;
11
- const out = (0, objects_1.clone)(options || {});
12
- let redisOptions;
13
- if ((0, utils_js_1.isClusterOptions)(out)) {
14
- redisOptions = out.redisOptions = out.redisOptions || {};
15
- out.nodes =
16
- out.nodes ?? (env[prefix + 'NODES'] || 'localhost:6379').split(/\s*,\s*/);
17
- }
18
- else {
19
- redisOptions = out;
20
- out.host = out.host ?? env[prefix + 'HOST'] ?? 'localhost';
21
- out.port = out.port ?? (0, putil_varhelpers_1.toIntDef)(env[prefix + 'PORT'], 6379);
22
- }
23
- redisOptions.db = redisOptions.db ?? (0, putil_varhelpers_1.toIntDef)(env[prefix + 'DB'], 0);
24
- redisOptions.username = redisOptions.username ?? env[prefix + 'USERNAME'];
25
- redisOptions.password = redisOptions.password ?? env[prefix + 'PASSWORD'];
26
- redisOptions.autoResubscribe =
27
- redisOptions.autoResubscribe ?? (0, putil_varhelpers_1.toBoolean)(env[prefix + 'AUTO_RESUBSCRIBE']);
28
- if (!redisOptions.reconnectOnError) {
29
- let n = env[prefix + 'RECONNECT_ON_ERROR'];
30
- if (n === 'true' || n === 'false') {
31
- n = (0, putil_varhelpers_1.toBoolean)(n);
32
- }
33
- else
34
- n = (0, putil_varhelpers_1.toInt)(n);
35
- redisOptions.reconnectOnError = () => n;
36
- }
37
- redisOptions.connectTimeout =
38
- redisOptions.connectTimeout ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'CONNECT_TIMEOUT']);
39
- redisOptions.socketTimeout =
40
- redisOptions.socketTimeout ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'SOCKET_TIMEOUT']);
41
- redisOptions.keepAlive =
42
- redisOptions.keepAlive ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'KEEP_ALIVE']);
43
- redisOptions.noDelay =
44
- redisOptions.noDelay ?? (0, putil_varhelpers_1.toBoolean)(env[prefix + 'NO_DELAY']);
45
- redisOptions.connectionName =
46
- redisOptions.connectionName ?? env[prefix + 'CONNECTION_NAME'];
47
- redisOptions.maxRetriesPerRequest =
48
- redisOptions.maxRetriesPerRequest === null
49
- ? null
50
- : (redisOptions.maxRetriesPerRequest ??
51
- (0, putil_varhelpers_1.toInt)(env[prefix + 'MAX_RETRIES_PER_REQUEST']));
52
- out.lazyConnect =
53
- options?.lazyConnect ?? (0, putil_varhelpers_1.toBoolean)(env[prefix + 'LAZY_CONNECT'] ?? 'false');
54
- return out;
55
- }
package/cjs/index.js DELETED
@@ -1,8 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
- tslib_1.__exportStar(require("./get-redis-config.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 DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "type": "commonjs"
3
- }
@@ -1,59 +0,0 @@
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;
@@ -1,170 +0,0 @@
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 node_assert_1 = tslib_1.__importDefault(require("node:assert"));
7
- const common_1 = require("@nestjs/common");
8
- const ansi_colors_1 = tslib_1.__importDefault(require("ansi-colors"));
9
- const crypto = tslib_1.__importStar(require("crypto"));
10
- const ioredis_1 = tslib_1.__importStar(require("ioredis"));
11
- const constants_js_1 = require("./constants.js");
12
- const get_redis_config_js_1 = require("./get-redis-config.js");
13
- const redis_client_js_1 = require("./redis-client.js");
14
- const utils_js_1 = require("./utils.js");
15
- const CLIENT_TOKEN = Symbol('CLIENT_TOKEN');
16
- let RedisCoreModule = RedisCoreModule_1 = class RedisCoreModule {
17
- static forRoot(moduleOptions) {
18
- const connectionOptions = (0, get_redis_config_js_1.getRedisConfig)(moduleOptions.useValue, moduleOptions.envPrefix);
19
- return this._createDynamicModule(moduleOptions, {
20
- providers: [
21
- {
22
- provide: constants_js_1.IOREDIS_CONNECTION_OPTIONS,
23
- useValue: connectionOptions,
24
- },
25
- ],
26
- });
27
- }
28
- static forRootAsync(asyncOptions) {
29
- node_assert_1.default.ok(asyncOptions.useFactory, 'useFactory is required');
30
- return this._createDynamicModule(asyncOptions, {
31
- providers: [
32
- {
33
- provide: constants_js_1.IOREDIS_CONNECTION_OPTIONS,
34
- inject: asyncOptions.inject,
35
- useFactory: async (...args) => {
36
- const opts = await asyncOptions.useFactory(...args);
37
- return (0, get_redis_config_js_1.getRedisConfig)(opts, asyncOptions.envPrefix);
38
- },
39
- },
40
- ],
41
- });
42
- }
43
- static _createDynamicModule(opts, metadata) {
44
- const token = opts.token ?? redis_client_js_1.RedisClient;
45
- const providers = [
46
- {
47
- provide: token,
48
- inject: [constants_js_1.IOREDIS_CONNECTION_OPTIONS],
49
- useFactory: async (connectionOptions) => {
50
- return this._createClient(connectionOptions);
51
- },
52
- },
53
- {
54
- provide: CLIENT_TOKEN,
55
- useExisting: token,
56
- },
57
- {
58
- provide: common_1.Logger,
59
- useValue: typeof opts.logger === 'string'
60
- ? new common_1.Logger(opts.logger)
61
- : opts.logger,
62
- },
63
- ];
64
- return {
65
- global: opts.global,
66
- module: RedisCoreModule_1,
67
- imports: opts.imports,
68
- ...metadata,
69
- providers: [
70
- ...(metadata.providers ?? []),
71
- ...providers,
72
- {
73
- provide: constants_js_1.IOREDIS_MODULE_TOKEN,
74
- useValue: crypto.randomUUID(),
75
- },
76
- ],
77
- exports: [constants_js_1.IOREDIS_CONNECTION_OPTIONS, token, ...(metadata.exports ?? [])],
78
- };
79
- }
80
- static _createClient(options) {
81
- const opts = { ...options };
82
- let client;
83
- if ((0, utils_js_1.isClusterOptions)(opts)) {
84
- const startupNodes = opts.nodes;
85
- delete opts.name;
86
- delete opts.nodes;
87
- opts.lazyConnect = true;
88
- const cluster = new ioredis_1.Cluster(startupNodes, opts);
89
- client = new redis_client_js_1.RedisClient({ cluster });
90
- }
91
- else if ((0, utils_js_1.isStandaloneOptions)(opts)) {
92
- if (opts.host && opts.host.includes('://')) {
93
- const url = new URL(opts.host);
94
- opts.host = url.hostname;
95
- if (url.port)
96
- opts.port = parseInt(url.port, 10);
97
- if (url.username)
98
- opts.username = url.username;
99
- if (url.password)
100
- opts.password = url.password;
101
- if (url.protocol === 'rediss:') {
102
- // @ts-ignore
103
- opts.tls = true;
104
- }
105
- const db = parseInt(url.pathname.substring(1), 10);
106
- if (db > 0)
107
- opts.db = db;
108
- }
109
- const standalone = new ioredis_1.default({
110
- ...opts,
111
- lazyConnect: true,
112
- });
113
- client = new redis_client_js_1.RedisClient({ standalone });
114
- }
115
- else
116
- throw new TypeError(`Invalid connection options`);
117
- return client;
118
- }
119
- /**
120
- *
121
- * @constructor
122
- */
123
- constructor(client, connectionOptions, logger) {
124
- this.client = client;
125
- this.connectionOptions = connectionOptions;
126
- this.logger = logger;
127
- }
128
- async onApplicationBootstrap() {
129
- const opts = this.connectionOptions;
130
- if (opts.lazyConnect)
131
- return;
132
- const isCluster = (0, utils_js_1.isClusterOptions)(opts);
133
- const hosts = isCluster
134
- ? opts.nodes
135
- .map(x => typeof x === 'object'
136
- ? x.host + ':' + x.port
137
- : typeof x === 'number'
138
- ? 'localhost:' + x
139
- : x)
140
- .join(', ')
141
- : opts.host;
142
- if (hosts) {
143
- this.logger?.log('Connecting to redis at ' + ansi_colors_1.default.blue(hosts));
144
- common_1.Logger.flush();
145
- try {
146
- if (this.client.redis.status === 'wait')
147
- await this.client.redis.connect();
148
- await this.client.redis.ping();
149
- }
150
- catch (e) {
151
- this.logger?.error('Redis connection failed: ' + e.message);
152
- throw e;
153
- }
154
- }
155
- }
156
- async onApplicationShutdown() {
157
- try {
158
- await this.client.quit();
159
- }
160
- catch {
161
- //
162
- }
163
- }
164
- };
165
- exports.RedisCoreModule = RedisCoreModule;
166
- exports.RedisCoreModule = RedisCoreModule = RedisCoreModule_1 = tslib_1.__decorate([
167
- tslib_1.__param(0, (0, common_1.Inject)(CLIENT_TOKEN)),
168
- tslib_1.__param(1, (0, common_1.Inject)(constants_js_1.IOREDIS_CONNECTION_OPTIONS)),
169
- tslib_1.__metadata("design:paramtypes", [redis_client_js_1.RedisClient, Object, common_1.Logger])
170
- ], RedisCoreModule);
@@ -1,25 +0,0 @@
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);
@@ -1,72 +0,0 @@
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 DELETED
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
package/cjs/utils.js DELETED
@@ -1,10 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isClusterOptions = isClusterOptions;
4
- exports.isStandaloneOptions = isStandaloneOptions;
5
- function isClusterOptions(options) {
6
- return options && typeof options === 'object' && Array.isArray(options.nodes);
7
- }
8
- function isStandaloneOptions(options) {
9
- return options && typeof options === 'object' && options.host;
10
- }
package/esm/package.json DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "type": "module"
3
- }
package/types/index.d.cts DELETED
@@ -1,5 +0,0 @@
1
- export * from './get-redis-config.js';
2
- export * from './redis.module.js';
3
- export * from './redis-client.js';
4
- export * from './shared-lock.js';
5
- export * from './types.js';
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes