@seip/bluebird-js 0.0.1 → 0.1.1
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/.env_example +53 -53
- package/AGENTS.md +426 -403
- package/LICENSE +1 -1
- package/README.md +292 -285
- package/app/routes/api.js +50 -50
- package/app/routes/web.js +55 -53
- package/app/views/about.js +63 -0
- package/app/views/dashboard.js +147 -0
- package/app/views/index.js +80 -0
- package/app/views/layouts/main/view.js +81 -0
- package/app/views/legal/terms.html +19 -0
- package/app/views/login.js +80 -0
- package/core/app.d.ts +18 -18
- package/core/app.js +684 -644
- package/core/auth.d.ts +13 -13
- package/core/auth.js +326 -326
- package/core/cache.d.ts +17 -17
- package/core/cache.js +475 -475
- package/core/cli/doctor.js +287 -287
- package/core/cli/ecosystem.js +162 -162
- package/core/cli/init.js +779 -779
- package/core/cli/migrate.js +541 -541
- package/core/cli/model.js +66 -66
- package/core/cli/nginx.js +199 -199
- package/core/cli/route.js +120 -120
- package/core/cli/view.js +55 -36
- package/core/config.d.ts +3 -3
- package/core/config.js +111 -111
- package/core/csrf.d.ts +3 -3
- package/core/csrf.js +114 -114
- package/core/database.d.ts +15 -15
- package/core/database.js +636 -636
- package/core/debug.d.ts +3 -3
- package/core/debug.js +249 -249
- package/core/hash.d.ts +3 -3
- package/core/hash.js +194 -194
- package/core/html.d.ts +42 -0
- package/core/html.js +217 -0
- package/core/index.d.ts +1176 -1071
- package/core/logger.d.ts +3 -3
- package/core/logger.js +127 -124
- package/core/middleware.d.ts +3 -3
- package/core/middleware.js +28 -28
- package/core/model.d.ts +19 -19
- package/core/model.js +600 -600
- package/core/queue.d.ts +13 -13
- package/core/queue.js +172 -172
- package/core/ratelimit.d.ts +3 -3
- package/core/ratelimit.js +145 -145
- package/core/redis.d.ts +21 -21
- package/core/redis.js +230 -230
- package/core/render-cache.js +212 -0
- package/core/render.d.ts +3 -3
- package/core/render.js +199 -555
- package/core/router.d.ts +3 -3
- package/core/router.js +171 -171
- package/core/seo.d.ts +3 -3
- package/core/seo.js +106 -106
- package/core/swagger.d.ts +3 -3
- package/core/swagger.js +60 -60
- package/core/upload.d.ts +13 -13
- package/core/upload.js +102 -102
- package/core/util/colors.js +82 -82
- package/core/validate.d.ts +15 -15
- package/core/validate.js +364 -364
- package/core/view-loader.js +162 -0
- package/core/vite.d.ts +3 -3
- package/core/vite.js +145 -145
- package/core/ws.d.ts +13 -13
- package/core/ws.js +245 -245
- package/index.js +18 -18
- package/jsconfig.json +15 -15
- package/package.json +85 -86
- package/production.md +273 -273
- package/public/css/bluebird.min.css +1 -1
- package/public/js/bluebird-client.js +1780 -1662
- package/public/js/bluebird.min.js +1072 -1072
- package/app/views/about.html +0 -161
- package/app/views/dashboard.html +0 -140
- package/app/views/index.html +0 -150
- package/app/views/login.html +0 -144
package/core/redis.d.ts
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
export function initRedis(): Promise<any>;
|
|
2
|
-
export function getRedisClient(): any;
|
|
3
|
-
export function isRedisConnectedStatus(): boolean;
|
|
4
|
-
export function redisGet(key: string): Promise<any>;
|
|
5
|
-
export function redisSet(key: string, value: any, seconds?: number): Promise<boolean>;
|
|
6
|
-
export function redisDel(keys: string | string[]): Promise<boolean>;
|
|
7
|
-
export function redisClearPrefix(prefix: string): Promise<boolean>;
|
|
8
|
-
export function disconnectRedis(): Promise<void>;
|
|
9
|
-
|
|
10
|
-
declare const redisHandler: {
|
|
11
|
-
initRedis: typeof initRedis;
|
|
12
|
-
getRedisClient: typeof getRedisClient;
|
|
13
|
-
isRedisConnectedStatus: typeof isRedisConnectedStatus;
|
|
14
|
-
redisGet: typeof redisGet;
|
|
15
|
-
redisSet: typeof redisSet;
|
|
16
|
-
redisDel: typeof redisDel;
|
|
17
|
-
redisClearPrefix: typeof redisClearPrefix;
|
|
18
|
-
disconnectRedis: typeof disconnectRedis;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export default redisHandler;
|
|
1
|
+
export function initRedis(): Promise<any>;
|
|
2
|
+
export function getRedisClient(): any;
|
|
3
|
+
export function isRedisConnectedStatus(): boolean;
|
|
4
|
+
export function redisGet(key: string): Promise<any>;
|
|
5
|
+
export function redisSet(key: string, value: any, seconds?: number): Promise<boolean>;
|
|
6
|
+
export function redisDel(keys: string | string[]): Promise<boolean>;
|
|
7
|
+
export function redisClearPrefix(prefix: string): Promise<boolean>;
|
|
8
|
+
export function disconnectRedis(): Promise<void>;
|
|
9
|
+
|
|
10
|
+
declare const redisHandler: {
|
|
11
|
+
initRedis: typeof initRedis;
|
|
12
|
+
getRedisClient: typeof getRedisClient;
|
|
13
|
+
isRedisConnectedStatus: typeof isRedisConnectedStatus;
|
|
14
|
+
redisGet: typeof redisGet;
|
|
15
|
+
redisSet: typeof redisSet;
|
|
16
|
+
redisDel: typeof redisDel;
|
|
17
|
+
redisClearPrefix: typeof redisClearPrefix;
|
|
18
|
+
disconnectRedis: typeof disconnectRedis;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export default redisHandler;
|
package/core/redis.js
CHANGED
|
@@ -1,230 +1,230 @@
|
|
|
1
|
-
import fs from "node:fs";
|
|
2
|
-
|
|
3
|
-
/** @type {import('redis').RedisClientType|null} */
|
|
4
|
-
let redisClient = null;
|
|
5
|
-
let isRedisConnected = false;
|
|
6
|
-
let isRedisModuleChecked = false;
|
|
7
|
-
let redisModule = null;
|
|
8
|
-
|
|
9
|
-
const redisHost = process.env.REDIS_HOST || "localhost";
|
|
10
|
-
const redisPort = parseInt(process.env.REDIS_PORT || "6379", 10);
|
|
11
|
-
const redisPassword = process.env.REDIS_PASSWORD || "";
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Checks whether the redis package is installed.
|
|
15
|
-
* @returns {Promise<any|null>}
|
|
16
|
-
*/
|
|
17
|
-
async function loadRedisModule() {
|
|
18
|
-
if (isRedisModuleChecked) return redisModule;
|
|
19
|
-
isRedisModuleChecked = true;
|
|
20
|
-
|
|
21
|
-
try {
|
|
22
|
-
redisModule = await import("redis");
|
|
23
|
-
return redisModule;
|
|
24
|
-
} catch {
|
|
25
|
-
console.error(
|
|
26
|
-
"[REDIS ERROR] The 'redis' package is not installed. To use Redis, install it via 'npm install redis' or 'npx bluebird-js add redis'."
|
|
27
|
-
);
|
|
28
|
-
redisModule = null;
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Initializes and connects to the Redis server if available.
|
|
35
|
-
* @returns {Promise<import('redis').RedisClientType|null>}
|
|
36
|
-
*/
|
|
37
|
-
export async function initRedis() {
|
|
38
|
-
if (redisClient && isRedisConnected) {
|
|
39
|
-
return redisClient;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const mod = await loadRedisModule();
|
|
43
|
-
if (!mod) {
|
|
44
|
-
return null;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const { createClient } = mod;
|
|
48
|
-
const url = redisPassword
|
|
49
|
-
? `redis://:${encodeURIComponent(redisPassword)}@${redisHost}:${redisPort}`
|
|
50
|
-
: `redis://${redisHost}:${redisPort}`;
|
|
51
|
-
|
|
52
|
-
try {
|
|
53
|
-
redisClient = createClient({
|
|
54
|
-
url,
|
|
55
|
-
socket: {
|
|
56
|
-
connectTimeout: 1500,
|
|
57
|
-
reconnectStrategy: false,
|
|
58
|
-
},
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
redisClient.on("error", () => {
|
|
62
|
-
isRedisConnected = false;
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
redisClient.on("ready", () => {
|
|
66
|
-
isRedisConnected = true;
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
redisClient.on("connect", () => {
|
|
70
|
-
isRedisConnected = true;
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
redisClient.on("end", () => {
|
|
74
|
-
isRedisConnected = false;
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
await redisClient.connect();
|
|
78
|
-
isRedisConnected = true;
|
|
79
|
-
return redisClient;
|
|
80
|
-
} catch (err) {
|
|
81
|
-
try {
|
|
82
|
-
redisClient?.destroy?.();
|
|
83
|
-
} catch {}
|
|
84
|
-
redisClient = null;
|
|
85
|
-
isRedisConnected = false;
|
|
86
|
-
return null;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Returns the active Redis client instance or null if not connected.
|
|
92
|
-
* @returns {import('redis').RedisClientType|null}
|
|
93
|
-
*/
|
|
94
|
-
export function getRedisClient() {
|
|
95
|
-
return isRedisConnected ? redisClient : null;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Checks whether Redis is connected and ready for commands.
|
|
100
|
-
* @returns {boolean}
|
|
101
|
-
*/
|
|
102
|
-
export function isRedisConnectedStatus() {
|
|
103
|
-
return Boolean(isRedisConnected && redisClient);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Retrieves a value from Redis by key.
|
|
108
|
-
* @param {string} key
|
|
109
|
-
* @returns {Promise<any|null>}
|
|
110
|
-
*/
|
|
111
|
-
export async function redisGet(key) {
|
|
112
|
-
const client = await initRedis();
|
|
113
|
-
if (!client || !isRedisConnected) return null;
|
|
114
|
-
|
|
115
|
-
try {
|
|
116
|
-
const raw = await client.get(key);
|
|
117
|
-
if (!raw) return null;
|
|
118
|
-
try {
|
|
119
|
-
return JSON.parse(raw);
|
|
120
|
-
} catch {
|
|
121
|
-
return raw;
|
|
122
|
-
}
|
|
123
|
-
} catch {
|
|
124
|
-
isRedisConnected = false;
|
|
125
|
-
return null;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Stores a value in Redis with an optional TTL in seconds.
|
|
131
|
-
* @param {string} key
|
|
132
|
-
* @param {any} value
|
|
133
|
-
* @param {number} [seconds=60]
|
|
134
|
-
* @returns {Promise<boolean>}
|
|
135
|
-
*/
|
|
136
|
-
export async function redisSet(key, value, seconds = 60) {
|
|
137
|
-
const client = await initRedis();
|
|
138
|
-
if (!client || !isRedisConnected) return false;
|
|
139
|
-
|
|
140
|
-
try {
|
|
141
|
-
const serialized = typeof value === "string" ? value : JSON.stringify(value);
|
|
142
|
-
if (seconds > 0) {
|
|
143
|
-
await client.set(key, serialized, { EX: seconds });
|
|
144
|
-
} else {
|
|
145
|
-
await client.set(key, serialized);
|
|
146
|
-
}
|
|
147
|
-
return true;
|
|
148
|
-
} catch {
|
|
149
|
-
isRedisConnected = false;
|
|
150
|
-
return false;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Deletes one or more keys from Redis.
|
|
156
|
-
* @param {string|string[]} keys
|
|
157
|
-
* @returns {Promise<boolean>}
|
|
158
|
-
*/
|
|
159
|
-
export async function redisDel(keys) {
|
|
160
|
-
const client = await initRedis();
|
|
161
|
-
if (!client || !isRedisConnected) return false;
|
|
162
|
-
|
|
163
|
-
try {
|
|
164
|
-
const keyList = Array.isArray(keys) ? keys : [keys];
|
|
165
|
-
if (keyList.length > 0) {
|
|
166
|
-
await client.del(keyList);
|
|
167
|
-
}
|
|
168
|
-
return true;
|
|
169
|
-
} catch {
|
|
170
|
-
isRedisConnected = false;
|
|
171
|
-
return false;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Removes all keys matching a specific prefix from Redis.
|
|
177
|
-
* @param {string} prefix
|
|
178
|
-
* @returns {Promise<boolean>}
|
|
179
|
-
*/
|
|
180
|
-
export async function redisClearPrefix(prefix) {
|
|
181
|
-
const client = await initRedis();
|
|
182
|
-
if (!client || !isRedisConnected) return false;
|
|
183
|
-
|
|
184
|
-
try {
|
|
185
|
-
let cursor = "0";
|
|
186
|
-
do {
|
|
187
|
-
const reply = await client.scan(cursor, {
|
|
188
|
-
MATCH: `${prefix}*`,
|
|
189
|
-
COUNT: 100,
|
|
190
|
-
});
|
|
191
|
-
cursor = String(reply.cursor);
|
|
192
|
-
if (reply.keys.length > 0) {
|
|
193
|
-
await client.del(reply.keys);
|
|
194
|
-
}
|
|
195
|
-
} while (cursor !== "0");
|
|
196
|
-
return true;
|
|
197
|
-
} catch {
|
|
198
|
-
isRedisConnected = false;
|
|
199
|
-
return false;
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* Gracefully disconnects the Redis client.
|
|
205
|
-
* @returns {Promise<void>}
|
|
206
|
-
*/
|
|
207
|
-
export async function disconnectRedis() {
|
|
208
|
-
if (redisClient) {
|
|
209
|
-
try {
|
|
210
|
-
await redisClient.quit();
|
|
211
|
-
} catch {
|
|
212
|
-
try {
|
|
213
|
-
redisClient.disconnect();
|
|
214
|
-
} catch {}
|
|
215
|
-
}
|
|
216
|
-
redisClient = null;
|
|
217
|
-
isRedisConnected = false;
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
export default {
|
|
222
|
-
initRedis,
|
|
223
|
-
getRedisClient,
|
|
224
|
-
isRedisConnected: isRedisConnectedStatus,
|
|
225
|
-
get: redisGet,
|
|
226
|
-
set: redisSet,
|
|
227
|
-
del: redisDel,
|
|
228
|
-
clearPrefix: redisClearPrefix,
|
|
229
|
-
disconnect: disconnectRedis,
|
|
230
|
-
};
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
|
|
3
|
+
/** @type {import('redis').RedisClientType|null} */
|
|
4
|
+
let redisClient = null;
|
|
5
|
+
let isRedisConnected = false;
|
|
6
|
+
let isRedisModuleChecked = false;
|
|
7
|
+
let redisModule = null;
|
|
8
|
+
|
|
9
|
+
const redisHost = process.env.REDIS_HOST || "localhost";
|
|
10
|
+
const redisPort = parseInt(process.env.REDIS_PORT || "6379", 10);
|
|
11
|
+
const redisPassword = process.env.REDIS_PASSWORD || "";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Checks whether the redis package is installed.
|
|
15
|
+
* @returns {Promise<any|null>}
|
|
16
|
+
*/
|
|
17
|
+
async function loadRedisModule() {
|
|
18
|
+
if (isRedisModuleChecked) return redisModule;
|
|
19
|
+
isRedisModuleChecked = true;
|
|
20
|
+
|
|
21
|
+
try {
|
|
22
|
+
redisModule = await import("redis");
|
|
23
|
+
return redisModule;
|
|
24
|
+
} catch {
|
|
25
|
+
console.error(
|
|
26
|
+
"[REDIS ERROR] The 'redis' package is not installed. To use Redis, install it via 'npm install redis' or 'npx bluebird-js add redis'."
|
|
27
|
+
);
|
|
28
|
+
redisModule = null;
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Initializes and connects to the Redis server if available.
|
|
35
|
+
* @returns {Promise<import('redis').RedisClientType|null>}
|
|
36
|
+
*/
|
|
37
|
+
export async function initRedis() {
|
|
38
|
+
if (redisClient && isRedisConnected) {
|
|
39
|
+
return redisClient;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const mod = await loadRedisModule();
|
|
43
|
+
if (!mod) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const { createClient } = mod;
|
|
48
|
+
const url = redisPassword
|
|
49
|
+
? `redis://:${encodeURIComponent(redisPassword)}@${redisHost}:${redisPort}`
|
|
50
|
+
: `redis://${redisHost}:${redisPort}`;
|
|
51
|
+
|
|
52
|
+
try {
|
|
53
|
+
redisClient = createClient({
|
|
54
|
+
url,
|
|
55
|
+
socket: {
|
|
56
|
+
connectTimeout: 1500,
|
|
57
|
+
reconnectStrategy: false,
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
redisClient.on("error", () => {
|
|
62
|
+
isRedisConnected = false;
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
redisClient.on("ready", () => {
|
|
66
|
+
isRedisConnected = true;
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
redisClient.on("connect", () => {
|
|
70
|
+
isRedisConnected = true;
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
redisClient.on("end", () => {
|
|
74
|
+
isRedisConnected = false;
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
await redisClient.connect();
|
|
78
|
+
isRedisConnected = true;
|
|
79
|
+
return redisClient;
|
|
80
|
+
} catch (err) {
|
|
81
|
+
try {
|
|
82
|
+
redisClient?.destroy?.();
|
|
83
|
+
} catch {}
|
|
84
|
+
redisClient = null;
|
|
85
|
+
isRedisConnected = false;
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Returns the active Redis client instance or null if not connected.
|
|
92
|
+
* @returns {import('redis').RedisClientType|null}
|
|
93
|
+
*/
|
|
94
|
+
export function getRedisClient() {
|
|
95
|
+
return isRedisConnected ? redisClient : null;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Checks whether Redis is connected and ready for commands.
|
|
100
|
+
* @returns {boolean}
|
|
101
|
+
*/
|
|
102
|
+
export function isRedisConnectedStatus() {
|
|
103
|
+
return Boolean(isRedisConnected && redisClient);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Retrieves a value from Redis by key.
|
|
108
|
+
* @param {string} key
|
|
109
|
+
* @returns {Promise<any|null>}
|
|
110
|
+
*/
|
|
111
|
+
export async function redisGet(key) {
|
|
112
|
+
const client = await initRedis();
|
|
113
|
+
if (!client || !isRedisConnected) return null;
|
|
114
|
+
|
|
115
|
+
try {
|
|
116
|
+
const raw = await client.get(key);
|
|
117
|
+
if (!raw) return null;
|
|
118
|
+
try {
|
|
119
|
+
return JSON.parse(raw);
|
|
120
|
+
} catch {
|
|
121
|
+
return raw;
|
|
122
|
+
}
|
|
123
|
+
} catch {
|
|
124
|
+
isRedisConnected = false;
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Stores a value in Redis with an optional TTL in seconds.
|
|
131
|
+
* @param {string} key
|
|
132
|
+
* @param {any} value
|
|
133
|
+
* @param {number} [seconds=60]
|
|
134
|
+
* @returns {Promise<boolean>}
|
|
135
|
+
*/
|
|
136
|
+
export async function redisSet(key, value, seconds = 60) {
|
|
137
|
+
const client = await initRedis();
|
|
138
|
+
if (!client || !isRedisConnected) return false;
|
|
139
|
+
|
|
140
|
+
try {
|
|
141
|
+
const serialized = typeof value === "string" ? value : JSON.stringify(value);
|
|
142
|
+
if (seconds > 0) {
|
|
143
|
+
await client.set(key, serialized, { EX: seconds });
|
|
144
|
+
} else {
|
|
145
|
+
await client.set(key, serialized);
|
|
146
|
+
}
|
|
147
|
+
return true;
|
|
148
|
+
} catch {
|
|
149
|
+
isRedisConnected = false;
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Deletes one or more keys from Redis.
|
|
156
|
+
* @param {string|string[]} keys
|
|
157
|
+
* @returns {Promise<boolean>}
|
|
158
|
+
*/
|
|
159
|
+
export async function redisDel(keys) {
|
|
160
|
+
const client = await initRedis();
|
|
161
|
+
if (!client || !isRedisConnected) return false;
|
|
162
|
+
|
|
163
|
+
try {
|
|
164
|
+
const keyList = Array.isArray(keys) ? keys : [keys];
|
|
165
|
+
if (keyList.length > 0) {
|
|
166
|
+
await client.del(keyList);
|
|
167
|
+
}
|
|
168
|
+
return true;
|
|
169
|
+
} catch {
|
|
170
|
+
isRedisConnected = false;
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Removes all keys matching a specific prefix from Redis.
|
|
177
|
+
* @param {string} prefix
|
|
178
|
+
* @returns {Promise<boolean>}
|
|
179
|
+
*/
|
|
180
|
+
export async function redisClearPrefix(prefix) {
|
|
181
|
+
const client = await initRedis();
|
|
182
|
+
if (!client || !isRedisConnected) return false;
|
|
183
|
+
|
|
184
|
+
try {
|
|
185
|
+
let cursor = "0";
|
|
186
|
+
do {
|
|
187
|
+
const reply = await client.scan(cursor, {
|
|
188
|
+
MATCH: `${prefix}*`,
|
|
189
|
+
COUNT: 100,
|
|
190
|
+
});
|
|
191
|
+
cursor = String(reply.cursor);
|
|
192
|
+
if (reply.keys.length > 0) {
|
|
193
|
+
await client.del(reply.keys);
|
|
194
|
+
}
|
|
195
|
+
} while (cursor !== "0");
|
|
196
|
+
return true;
|
|
197
|
+
} catch {
|
|
198
|
+
isRedisConnected = false;
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Gracefully disconnects the Redis client.
|
|
205
|
+
* @returns {Promise<void>}
|
|
206
|
+
*/
|
|
207
|
+
export async function disconnectRedis() {
|
|
208
|
+
if (redisClient) {
|
|
209
|
+
try {
|
|
210
|
+
await redisClient.quit();
|
|
211
|
+
} catch {
|
|
212
|
+
try {
|
|
213
|
+
redisClient.disconnect();
|
|
214
|
+
} catch {}
|
|
215
|
+
}
|
|
216
|
+
redisClient = null;
|
|
217
|
+
isRedisConnected = false;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export default {
|
|
222
|
+
initRedis,
|
|
223
|
+
getRedisClient,
|
|
224
|
+
isRedisConnected: isRedisConnectedStatus,
|
|
225
|
+
get: redisGet,
|
|
226
|
+
set: redisSet,
|
|
227
|
+
del: redisDel,
|
|
228
|
+
clearPrefix: redisClearPrefix,
|
|
229
|
+
disconnect: disconnectRedis,
|
|
230
|
+
};
|