@xnestjs/ioredis 1.5.2 → 1.6.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/README.md +4 -0
- package/cjs/get-redis-config.js +43 -0
- package/cjs/index.js +1 -0
- package/cjs/redis-core.module.js +3 -40
- package/esm/get-redis-config.js +39 -0
- package/esm/index.js +1 -0
- package/esm/redis-core.module.js +3 -40
- package/package.json +3 -3
- package/types/get-redis-config.d.ts +2 -0
- package/types/index.d.cts +1 -0
- package/types/index.d.ts +1 -0
- package/types/redis-core.module.d.ts +0 -1
package/README.md
CHANGED
|
@@ -108,6 +108,8 @@ export class MyModule {
|
|
|
108
108
|
The library supports configuration through environment variables. Environment variables below is accepted.
|
|
109
109
|
All environment variables starts with prefix (REDIS_). This can be configured while registering the module.
|
|
110
110
|
|
|
111
|
+
<--- BEGIN env --->
|
|
112
|
+
|
|
111
113
|
### Standalone Connection Variables
|
|
112
114
|
|
|
113
115
|
The following environment variables apply to the standalone connection.
|
|
@@ -140,3 +142,5 @@ The following environment variables apply to the standalone connection.
|
|
|
140
142
|
| REDIS_KEEP_ALIVE | Boolean | | Enable/disable keep-alive functionality. |
|
|
141
143
|
| REDIS_NO_DELAY | Boolean | | Enable/disable the use of Nagle's algorithm. |
|
|
142
144
|
| REDIS_MAX_RETRIES_PER_REQUEST | Number | | Defines max retries per request value |
|
|
145
|
+
|
|
146
|
+
<--- END env --->
|
|
@@ -0,0 +1,43 @@
|
|
|
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 = out.nodes ?? (env[prefix + 'NODES'] || 'localhost:6379').split(/\s*,\s*/);
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
redisOptions = out;
|
|
19
|
+
out.host = out.host ?? env[prefix + 'HOST'] ?? 'localhost';
|
|
20
|
+
out.port = out.port ?? (0, putil_varhelpers_1.toIntDef)(env[prefix + 'PORT'], 6379);
|
|
21
|
+
}
|
|
22
|
+
redisOptions.db = redisOptions.db ?? (0, putil_varhelpers_1.toIntDef)(env[prefix + 'DB'], 0);
|
|
23
|
+
redisOptions.username = redisOptions.username ?? env[prefix + 'USERNAME'];
|
|
24
|
+
redisOptions.password = redisOptions.password ?? env[prefix + 'PASSWORD'];
|
|
25
|
+
redisOptions.autoResubscribe = redisOptions.autoResubscribe ?? (0, putil_varhelpers_1.toBoolean)(env[prefix + 'AUTO_RESUBSCRIBE']);
|
|
26
|
+
if (!redisOptions.reconnectOnError) {
|
|
27
|
+
let n = env[prefix + 'RECONNECT_ON_ERROR'];
|
|
28
|
+
if (n === 'true' || n === 'false') {
|
|
29
|
+
n = (0, putil_varhelpers_1.toBoolean)(n);
|
|
30
|
+
}
|
|
31
|
+
else
|
|
32
|
+
n = (0, putil_varhelpers_1.toInt)(n);
|
|
33
|
+
redisOptions.reconnectOnError = () => n;
|
|
34
|
+
}
|
|
35
|
+
redisOptions.connectTimeout = redisOptions.connectTimeout ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'CONNECT_TIMEOUT']);
|
|
36
|
+
redisOptions.socketTimeout = redisOptions.socketTimeout ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'SOCKET_TIMEOUT']);
|
|
37
|
+
redisOptions.keepAlive = redisOptions.keepAlive ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'KEEP_ALIVE']);
|
|
38
|
+
redisOptions.noDelay = redisOptions.noDelay ?? (0, putil_varhelpers_1.toBoolean)(env[prefix + 'NO_DELAY']);
|
|
39
|
+
redisOptions.connectionName = redisOptions.connectionName ?? env[prefix + 'CONNECTION_NAME'];
|
|
40
|
+
redisOptions.maxRetriesPerRequest =
|
|
41
|
+
redisOptions.maxRetriesPerRequest ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'MAX_RETRIES_PER_REQUEST']);
|
|
42
|
+
return out;
|
|
43
|
+
}
|
package/cjs/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./get-redis-config.js"), exports);
|
|
4
5
|
tslib_1.__exportStar(require("./redis.module.js"), exports);
|
|
5
6
|
tslib_1.__exportStar(require("./redis-client.js"), exports);
|
|
6
7
|
tslib_1.__exportStar(require("./shared-lock.js"), exports);
|
package/cjs/redis-core.module.js
CHANGED
|
@@ -4,20 +4,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
4
4
|
exports.RedisCoreModule = void 0;
|
|
5
5
|
const tslib_1 = require("tslib");
|
|
6
6
|
const node_assert_1 = tslib_1.__importDefault(require("node:assert"));
|
|
7
|
-
const node_process_1 = tslib_1.__importDefault(require("node:process"));
|
|
8
|
-
const objects_1 = require("@jsopen/objects");
|
|
9
7
|
const common_1 = require("@nestjs/common");
|
|
10
8
|
const ansi_colors_1 = tslib_1.__importDefault(require("ansi-colors"));
|
|
11
9
|
const crypto = tslib_1.__importStar(require("crypto"));
|
|
12
10
|
const ioredis_1 = tslib_1.__importStar(require("ioredis"));
|
|
13
|
-
const putil_varhelpers_1 = require("putil-varhelpers");
|
|
14
11
|
const constants_js_1 = require("./constants.js");
|
|
12
|
+
const get_redis_config_js_1 = require("./get-redis-config.js");
|
|
15
13
|
const redis_client_js_1 = require("./redis-client.js");
|
|
16
14
|
const utils_js_1 = require("./utils.js");
|
|
17
15
|
const CLIENT_TOKEN = Symbol('CLIENT_TOKEN');
|
|
18
16
|
let RedisCoreModule = RedisCoreModule_1 = class RedisCoreModule {
|
|
19
17
|
static forRoot(moduleOptions) {
|
|
20
|
-
const connectionOptions =
|
|
18
|
+
const connectionOptions = (0, get_redis_config_js_1.getRedisConfig)(moduleOptions.useValue, moduleOptions.envPrefix);
|
|
21
19
|
return this._createDynamicModule(moduleOptions, {
|
|
22
20
|
global: moduleOptions.global,
|
|
23
21
|
providers: [
|
|
@@ -38,7 +36,7 @@ let RedisCoreModule = RedisCoreModule_1 = class RedisCoreModule {
|
|
|
38
36
|
inject: asyncOptions.inject,
|
|
39
37
|
useFactory: async (...args) => {
|
|
40
38
|
const opts = await asyncOptions.useFactory(...args);
|
|
41
|
-
return
|
|
39
|
+
return (0, get_redis_config_js_1.getRedisConfig)(opts, asyncOptions.envPrefix);
|
|
42
40
|
},
|
|
43
41
|
},
|
|
44
42
|
],
|
|
@@ -77,41 +75,6 @@ let RedisCoreModule = RedisCoreModule_1 = class RedisCoreModule {
|
|
|
77
75
|
exports: [constants_js_1.IOREDIS_CONNECTION_OPTIONS, token, ...(metadata.exports ?? [])],
|
|
78
76
|
};
|
|
79
77
|
}
|
|
80
|
-
static _readConnectionOptions(options, prefix = 'REDIS_') {
|
|
81
|
-
const env = node_process_1.default.env;
|
|
82
|
-
const out = (0, objects_1.clone)(options || {});
|
|
83
|
-
let redisOptions;
|
|
84
|
-
if ((0, utils_js_1.isClusterOptions)(out)) {
|
|
85
|
-
redisOptions = out.redisOptions = out.redisOptions || {};
|
|
86
|
-
out.nodes = out.nodes ?? (env[prefix + 'NODES'] || 'localhost:6379').split(/\s*,\s*/);
|
|
87
|
-
}
|
|
88
|
-
else {
|
|
89
|
-
redisOptions = out;
|
|
90
|
-
out.host = out.host ?? env[prefix + 'HOST'] ?? 'localhost';
|
|
91
|
-
out.port = out.port ?? (0, putil_varhelpers_1.toIntDef)(env[prefix + 'PORT'], 6379);
|
|
92
|
-
}
|
|
93
|
-
redisOptions.db = redisOptions.db ?? (0, putil_varhelpers_1.toIntDef)(env[prefix + 'DB'], 0);
|
|
94
|
-
redisOptions.username = redisOptions.username ?? env[prefix + 'USERNAME'];
|
|
95
|
-
redisOptions.password = redisOptions.password ?? env[prefix + 'PASSWORD'];
|
|
96
|
-
redisOptions.autoResubscribe = redisOptions.autoResubscribe ?? (0, putil_varhelpers_1.toBoolean)(env[prefix + 'AUTO_RESUBSCRIBE']);
|
|
97
|
-
if (!redisOptions.reconnectOnError) {
|
|
98
|
-
let n = env[prefix + 'RECONNECT_ON_ERROR'];
|
|
99
|
-
if (n === 'true' || n === 'false') {
|
|
100
|
-
n = (0, putil_varhelpers_1.toBoolean)(n);
|
|
101
|
-
}
|
|
102
|
-
else
|
|
103
|
-
n = (0, putil_varhelpers_1.toInt)(n);
|
|
104
|
-
redisOptions.reconnectOnError = () => n;
|
|
105
|
-
}
|
|
106
|
-
redisOptions.connectTimeout = redisOptions.connectTimeout ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'CONNECT_TIMEOUT']);
|
|
107
|
-
redisOptions.socketTimeout = redisOptions.socketTimeout ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'SOCKET_TIMEOUT']);
|
|
108
|
-
redisOptions.keepAlive = redisOptions.keepAlive ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'KEEP_ALIVE']);
|
|
109
|
-
redisOptions.noDelay = redisOptions.noDelay ?? (0, putil_varhelpers_1.toBoolean)(env[prefix + 'NO_DELAY']);
|
|
110
|
-
redisOptions.connectionName = redisOptions.connectionName ?? env[prefix + 'CONNECTION_NAME'];
|
|
111
|
-
redisOptions.maxRetriesPerRequest =
|
|
112
|
-
redisOptions.maxRetriesPerRequest ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'MAX_RETRIES_PER_REQUEST']);
|
|
113
|
-
return out;
|
|
114
|
-
}
|
|
115
78
|
static _createClient(options) {
|
|
116
79
|
const opts = { ...options };
|
|
117
80
|
let client;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import process from 'node:process';
|
|
2
|
+
import { clone } from '@jsopen/objects';
|
|
3
|
+
import { toBoolean, toInt, toIntDef } from 'putil-varhelpers';
|
|
4
|
+
import { isClusterOptions } from './utils.js';
|
|
5
|
+
export function getRedisConfig(options, prefix = 'REDIS_') {
|
|
6
|
+
const env = process.env;
|
|
7
|
+
const out = clone(options || {});
|
|
8
|
+
let redisOptions;
|
|
9
|
+
if (isClusterOptions(out)) {
|
|
10
|
+
redisOptions = out.redisOptions = out.redisOptions || {};
|
|
11
|
+
out.nodes = out.nodes ?? (env[prefix + 'NODES'] || 'localhost:6379').split(/\s*,\s*/);
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
redisOptions = out;
|
|
15
|
+
out.host = out.host ?? env[prefix + 'HOST'] ?? 'localhost';
|
|
16
|
+
out.port = out.port ?? toIntDef(env[prefix + 'PORT'], 6379);
|
|
17
|
+
}
|
|
18
|
+
redisOptions.db = redisOptions.db ?? toIntDef(env[prefix + 'DB'], 0);
|
|
19
|
+
redisOptions.username = redisOptions.username ?? env[prefix + 'USERNAME'];
|
|
20
|
+
redisOptions.password = redisOptions.password ?? env[prefix + 'PASSWORD'];
|
|
21
|
+
redisOptions.autoResubscribe = redisOptions.autoResubscribe ?? toBoolean(env[prefix + 'AUTO_RESUBSCRIBE']);
|
|
22
|
+
if (!redisOptions.reconnectOnError) {
|
|
23
|
+
let n = env[prefix + 'RECONNECT_ON_ERROR'];
|
|
24
|
+
if (n === 'true' || n === 'false') {
|
|
25
|
+
n = toBoolean(n);
|
|
26
|
+
}
|
|
27
|
+
else
|
|
28
|
+
n = toInt(n);
|
|
29
|
+
redisOptions.reconnectOnError = () => n;
|
|
30
|
+
}
|
|
31
|
+
redisOptions.connectTimeout = redisOptions.connectTimeout ?? toInt(env[prefix + 'CONNECT_TIMEOUT']);
|
|
32
|
+
redisOptions.socketTimeout = redisOptions.socketTimeout ?? toInt(env[prefix + 'SOCKET_TIMEOUT']);
|
|
33
|
+
redisOptions.keepAlive = redisOptions.keepAlive ?? toInt(env[prefix + 'KEEP_ALIVE']);
|
|
34
|
+
redisOptions.noDelay = redisOptions.noDelay ?? toBoolean(env[prefix + 'NO_DELAY']);
|
|
35
|
+
redisOptions.connectionName = redisOptions.connectionName ?? env[prefix + 'CONNECTION_NAME'];
|
|
36
|
+
redisOptions.maxRetriesPerRequest =
|
|
37
|
+
redisOptions.maxRetriesPerRequest ?? toInt(env[prefix + 'MAX_RETRIES_PER_REQUEST']);
|
|
38
|
+
return out;
|
|
39
|
+
}
|
package/esm/index.js
CHANGED
package/esm/redis-core.module.js
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
1
|
var RedisCoreModule_1;
|
|
2
2
|
import { __decorate, __metadata, __param } from "tslib";
|
|
3
3
|
import assert from 'node:assert';
|
|
4
|
-
import process from 'node:process';
|
|
5
|
-
import { clone } from '@jsopen/objects';
|
|
6
4
|
import { Inject, Logger } from '@nestjs/common';
|
|
7
5
|
import colors from 'ansi-colors';
|
|
8
6
|
import * as crypto from 'crypto';
|
|
9
7
|
import Redis, { Cluster } from 'ioredis';
|
|
10
|
-
import { toBoolean, toInt, toIntDef } from 'putil-varhelpers';
|
|
11
8
|
import { IOREDIS_CONNECTION_OPTIONS, IOREDIS_MODULE_TOKEN } from './constants.js';
|
|
9
|
+
import { getRedisConfig } from './get-redis-config.js';
|
|
12
10
|
import { RedisClient } from './redis-client.js';
|
|
13
11
|
import { isClusterOptions, isStandaloneOptions } from './utils.js';
|
|
14
12
|
const CLIENT_TOKEN = Symbol('CLIENT_TOKEN');
|
|
15
13
|
let RedisCoreModule = RedisCoreModule_1 = class RedisCoreModule {
|
|
16
14
|
static forRoot(moduleOptions) {
|
|
17
|
-
const connectionOptions =
|
|
15
|
+
const connectionOptions = getRedisConfig(moduleOptions.useValue, moduleOptions.envPrefix);
|
|
18
16
|
return this._createDynamicModule(moduleOptions, {
|
|
19
17
|
global: moduleOptions.global,
|
|
20
18
|
providers: [
|
|
@@ -35,7 +33,7 @@ let RedisCoreModule = RedisCoreModule_1 = class RedisCoreModule {
|
|
|
35
33
|
inject: asyncOptions.inject,
|
|
36
34
|
useFactory: async (...args) => {
|
|
37
35
|
const opts = await asyncOptions.useFactory(...args);
|
|
38
|
-
return
|
|
36
|
+
return getRedisConfig(opts, asyncOptions.envPrefix);
|
|
39
37
|
},
|
|
40
38
|
},
|
|
41
39
|
],
|
|
@@ -74,41 +72,6 @@ let RedisCoreModule = RedisCoreModule_1 = class RedisCoreModule {
|
|
|
74
72
|
exports: [IOREDIS_CONNECTION_OPTIONS, token, ...(metadata.exports ?? [])],
|
|
75
73
|
};
|
|
76
74
|
}
|
|
77
|
-
static _readConnectionOptions(options, prefix = 'REDIS_') {
|
|
78
|
-
const env = process.env;
|
|
79
|
-
const out = clone(options || {});
|
|
80
|
-
let redisOptions;
|
|
81
|
-
if (isClusterOptions(out)) {
|
|
82
|
-
redisOptions = out.redisOptions = out.redisOptions || {};
|
|
83
|
-
out.nodes = out.nodes ?? (env[prefix + 'NODES'] || 'localhost:6379').split(/\s*,\s*/);
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
redisOptions = out;
|
|
87
|
-
out.host = out.host ?? env[prefix + 'HOST'] ?? 'localhost';
|
|
88
|
-
out.port = out.port ?? toIntDef(env[prefix + 'PORT'], 6379);
|
|
89
|
-
}
|
|
90
|
-
redisOptions.db = redisOptions.db ?? toIntDef(env[prefix + 'DB'], 0);
|
|
91
|
-
redisOptions.username = redisOptions.username ?? env[prefix + 'USERNAME'];
|
|
92
|
-
redisOptions.password = redisOptions.password ?? env[prefix + 'PASSWORD'];
|
|
93
|
-
redisOptions.autoResubscribe = redisOptions.autoResubscribe ?? toBoolean(env[prefix + 'AUTO_RESUBSCRIBE']);
|
|
94
|
-
if (!redisOptions.reconnectOnError) {
|
|
95
|
-
let n = env[prefix + 'RECONNECT_ON_ERROR'];
|
|
96
|
-
if (n === 'true' || n === 'false') {
|
|
97
|
-
n = toBoolean(n);
|
|
98
|
-
}
|
|
99
|
-
else
|
|
100
|
-
n = toInt(n);
|
|
101
|
-
redisOptions.reconnectOnError = () => n;
|
|
102
|
-
}
|
|
103
|
-
redisOptions.connectTimeout = redisOptions.connectTimeout ?? toInt(env[prefix + 'CONNECT_TIMEOUT']);
|
|
104
|
-
redisOptions.socketTimeout = redisOptions.socketTimeout ?? toInt(env[prefix + 'SOCKET_TIMEOUT']);
|
|
105
|
-
redisOptions.keepAlive = redisOptions.keepAlive ?? toInt(env[prefix + 'KEEP_ALIVE']);
|
|
106
|
-
redisOptions.noDelay = redisOptions.noDelay ?? toBoolean(env[prefix + 'NO_DELAY']);
|
|
107
|
-
redisOptions.connectionName = redisOptions.connectionName ?? env[prefix + 'CONNECTION_NAME'];
|
|
108
|
-
redisOptions.maxRetriesPerRequest =
|
|
109
|
-
redisOptions.maxRetriesPerRequest ?? toInt(env[prefix + 'MAX_RETRIES_PER_REQUEST']);
|
|
110
|
-
return out;
|
|
111
|
-
}
|
|
112
75
|
static _createClient(options) {
|
|
113
76
|
const opts = { ...options };
|
|
114
77
|
let client;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xnestjs/ioredis",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0",
|
|
4
4
|
"description": "NestJS extension library for ioredis",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
@@ -13,8 +13,8 @@
|
|
|
13
13
|
"peerDependencies": {
|
|
14
14
|
"@nestjs/common": "^10.0.0 || ^11.0.0",
|
|
15
15
|
"@nestjs/core": "^10.0.0 || ^11.0.0",
|
|
16
|
-
"ioredis": "^5.
|
|
17
|
-
"redis-semaphore": "^5.
|
|
16
|
+
"ioredis": "^5.5.0",
|
|
17
|
+
"redis-semaphore": "^5.6.1"
|
|
18
18
|
},
|
|
19
19
|
"type": "module",
|
|
20
20
|
"exports": {
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import type { RedisClusterConnectionOptions, RedisStandaloneConnectionOptions } from './types';
|
|
2
|
+
export declare function getRedisConfig(options?: RedisStandaloneConnectionOptions | RedisClusterConnectionOptions, prefix?: string): RedisStandaloneConnectionOptions | RedisClusterConnectionOptions;
|
package/types/index.d.cts
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -8,7 +8,6 @@ export declare class RedisCoreModule implements OnApplicationBootstrap, OnApplic
|
|
|
8
8
|
static forRoot(moduleOptions: RedisModuleOptions): DynamicModule;
|
|
9
9
|
static forRootAsync(asyncOptions: RedisAsyncModuleOptions): DynamicModule;
|
|
10
10
|
private static _createDynamicModule;
|
|
11
|
-
private static _readConnectionOptions;
|
|
12
11
|
private static _createClient;
|
|
13
12
|
/**
|
|
14
13
|
*
|