conduithub 1.0.0 → 1.0.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/dist/core/conduit-hub/index.cjs +1 -1
- package/dist/core/conduit-hub/index.mjs +1 -1
- package/dist/core/event-bus/index.cjs +4 -4
- package/dist/core/event-bus/index.mjs +1 -1
- package/dist/core/hook/index.cjs +6 -6
- package/dist/core/hook/index.mjs +1 -1
- package/dist/core/index.cjs +1 -1
- package/dist/core/index.mjs +1 -1
- package/dist/core/service-container/index.cjs +9 -9
- package/dist/core/service-container/index.mjs +1 -1
- package/dist/core/state-manager/index.cjs +3 -3
- package/dist/core/state-manager/index.mjs +1 -1
- package/dist/{shared/conduithub.BqUYv04j.cjs → error/index.cjs} +3 -0
- package/dist/error/index.d.cts +47 -0
- package/dist/error/index.d.mts +47 -0
- package/dist/error/index.d.ts +47 -0
- package/dist/{shared/conduithub.BDwZXllF.mjs → error/index.mjs} +3 -1
- package/dist/index.cjs +10 -6
- package/dist/index.d.cts +2 -48
- package/dist/index.d.mts +2 -48
- package/dist/index.d.ts +2 -48
- package/dist/index.mjs +5 -2
- package/dist/plugins/index.cjs +12 -0
- package/dist/plugins/index.d.cts +8 -0
- package/dist/plugins/index.d.mts +8 -0
- package/dist/plugins/index.d.ts +8 -0
- package/dist/plugins/index.mjs +6 -0
- package/dist/plugins/redis/index.cjs +12 -0
- package/dist/plugins/redis/index.d.cts +109 -0
- package/dist/plugins/redis/index.d.mts +109 -0
- package/dist/plugins/redis/index.d.ts +109 -0
- package/dist/plugins/redis/index.mjs +6 -0
- package/dist/shared/conduithub.-bZD30_I.mjs +769 -0
- package/dist/shared/{conduithub.gF2DFc43.cjs → conduithub.Dlvl2xGE.cjs} +3 -3
- package/dist/shared/{conduithub.bsiNMTVD.mjs → conduithub.Up0QYVao.mjs} +1 -1
- package/dist/shared/conduithub.cvEjE62V.cjs +775 -0
- package/dist/utils/index.cjs +3 -2
- package/dist/utils/index.mjs +3 -2
- package/package.json +1 -1
|
@@ -0,0 +1,769 @@
|
|
|
1
|
+
import Redis from 'ioredis';
|
|
2
|
+
import { BasePlugin } from '../core/plugin/index.mjs';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
|
|
5
|
+
const redisConfigSchema = z.object({
|
|
6
|
+
host: z.string().default("localhost"),
|
|
7
|
+
port: z.number().default(6379),
|
|
8
|
+
password: z.string().optional(),
|
|
9
|
+
db: z.number().default(0),
|
|
10
|
+
username: z.string().optional(),
|
|
11
|
+
url: z.string().url().optional(),
|
|
12
|
+
keyPrefix: z.string().optional(),
|
|
13
|
+
tls: z.object({
|
|
14
|
+
ca: z.string().optional(),
|
|
15
|
+
cert: z.string().optional(),
|
|
16
|
+
key: z.string().optional(),
|
|
17
|
+
rejectUnauthorized: z.boolean().default(true)
|
|
18
|
+
}).optional(),
|
|
19
|
+
connectionTimeout: z.number().default(5e4),
|
|
20
|
+
commandTimeout: z.number().default(5e4),
|
|
21
|
+
maxRetriesPerRequest: z.number().default(3),
|
|
22
|
+
retryDelayOnFailover: z.number().default(100),
|
|
23
|
+
enableReadyCheck: z.boolean().default(true),
|
|
24
|
+
enableOfflineQueue: z.boolean().default(true),
|
|
25
|
+
keepAlive: z.boolean().default(true),
|
|
26
|
+
lazyConnect: z.boolean().default(false),
|
|
27
|
+
family: z.enum(["IPv4", "IPv6"]).default("IPv4")
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
function createNoopRedisClient() {
|
|
31
|
+
return {
|
|
32
|
+
set: async () => "OK",
|
|
33
|
+
get: async () => null,
|
|
34
|
+
del: async () => 0,
|
|
35
|
+
exists: async () => 0,
|
|
36
|
+
expire: async () => 1,
|
|
37
|
+
ttl: async () => -1,
|
|
38
|
+
hset: async () => 1,
|
|
39
|
+
hget: async () => null,
|
|
40
|
+
hgetall: async () => ({}),
|
|
41
|
+
hdel: async () => 0,
|
|
42
|
+
lpush: async () => 0,
|
|
43
|
+
rpush: async () => 0,
|
|
44
|
+
lpop: async () => null,
|
|
45
|
+
rpop: async () => null,
|
|
46
|
+
llen: async () => 0,
|
|
47
|
+
lrange: async () => [],
|
|
48
|
+
sadd: async () => 0,
|
|
49
|
+
srem: async () => 0,
|
|
50
|
+
smembers: async () => [],
|
|
51
|
+
sismember: async () => 0,
|
|
52
|
+
scard: async () => 0,
|
|
53
|
+
zadd: async () => 0,
|
|
54
|
+
zrange: async () => [],
|
|
55
|
+
ping: async () => "PONG",
|
|
56
|
+
flushdb: async () => "OK",
|
|
57
|
+
flushall: async () => "OK"
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
class RedisPlugin extends BasePlugin {
|
|
61
|
+
redis;
|
|
62
|
+
ioredisClient;
|
|
63
|
+
isConnected = false;
|
|
64
|
+
stats = {
|
|
65
|
+
totalCommands: 0,
|
|
66
|
+
successfulCommands: 0,
|
|
67
|
+
failedCommands: 0,
|
|
68
|
+
averageResponseTime: 0
|
|
69
|
+
};
|
|
70
|
+
async run(operation, payload, call) {
|
|
71
|
+
await this.executeHook("before-redis-operation", {
|
|
72
|
+
operation,
|
|
73
|
+
...payload
|
|
74
|
+
});
|
|
75
|
+
const start = Date.now();
|
|
76
|
+
try {
|
|
77
|
+
const result = await call();
|
|
78
|
+
const duration = Date.now() - start;
|
|
79
|
+
this.stats.totalCommands++;
|
|
80
|
+
this.stats.successfulCommands++;
|
|
81
|
+
const n = this.stats.successfulCommands;
|
|
82
|
+
this.stats.averageResponseTime = this.stats.averageResponseTime + (duration - this.stats.averageResponseTime) / n;
|
|
83
|
+
this.stats.lastCommandTime = Date.now();
|
|
84
|
+
await this.executeHook("after-redis-operation", {
|
|
85
|
+
operation,
|
|
86
|
+
...payload,
|
|
87
|
+
result
|
|
88
|
+
});
|
|
89
|
+
return result;
|
|
90
|
+
} catch (error) {
|
|
91
|
+
this.stats.totalCommands++;
|
|
92
|
+
this.stats.failedCommands++;
|
|
93
|
+
await this.executeHook("after-redis-operation", {
|
|
94
|
+
operation,
|
|
95
|
+
...payload,
|
|
96
|
+
error
|
|
97
|
+
});
|
|
98
|
+
this.logger.log?.("error", `Redis ${operation} failed`, error);
|
|
99
|
+
throw error;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
constructor(config) {
|
|
103
|
+
const normalized = redisConfigSchema.parse(config ?? {});
|
|
104
|
+
super({
|
|
105
|
+
name: "redis",
|
|
106
|
+
version: "1.0.0",
|
|
107
|
+
enabled: true,
|
|
108
|
+
dependencies: [],
|
|
109
|
+
config: normalized
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
createRedisWrapper(redisClient) {
|
|
113
|
+
return {
|
|
114
|
+
set: async (key, value, options) => {
|
|
115
|
+
if (!options || Object.keys(options).length === 0) {
|
|
116
|
+
return redisClient.set(key, String(value));
|
|
117
|
+
}
|
|
118
|
+
const setOptions = {};
|
|
119
|
+
if (options.EX) setOptions.EX = options.EX;
|
|
120
|
+
if (options.PX) setOptions.PX = options.PX;
|
|
121
|
+
if (options.NX) setOptions.GET = false;
|
|
122
|
+
if (options.XX) setOptions.GET = false;
|
|
123
|
+
return redisClient.set(key, String(value), setOptions);
|
|
124
|
+
},
|
|
125
|
+
get: (key) => redisClient.get(key),
|
|
126
|
+
del: (key) => Array.isArray(key) ? redisClient.del(...key) : redisClient.del(key),
|
|
127
|
+
exists: (key) => Array.isArray(key) ? redisClient.exists(...key) : redisClient.exists(key),
|
|
128
|
+
expire: (key, seconds) => redisClient.expire(key, seconds),
|
|
129
|
+
ttl: (key) => redisClient.ttl(key),
|
|
130
|
+
hset: (key, field, value) => redisClient.hset(key, field, value),
|
|
131
|
+
hget: (key, field) => redisClient.hget(key, field),
|
|
132
|
+
hgetall: (key) => redisClient.hgetall(key),
|
|
133
|
+
hdel: (key, field) => Array.isArray(field) ? redisClient.hdel(key, ...field) : redisClient.hdel(key, field),
|
|
134
|
+
lpush: (key, ...elements) => redisClient.lpush(key, ...elements),
|
|
135
|
+
rpush: (key, ...elements) => redisClient.rpush(key, ...elements),
|
|
136
|
+
lpop: (key) => redisClient.lpop(key),
|
|
137
|
+
rpop: (key) => redisClient.rpop(key),
|
|
138
|
+
llen: (key) => redisClient.llen(key),
|
|
139
|
+
lrange: (key, start, stop) => redisClient.lrange(key, start, stop),
|
|
140
|
+
sadd: (key, ...members) => redisClient.sadd(key, ...members),
|
|
141
|
+
srem: (key, ...members) => redisClient.srem(key, ...members),
|
|
142
|
+
smembers: (key) => redisClient.smembers(key),
|
|
143
|
+
sismember: (key, member) => redisClient.sismember(key, member),
|
|
144
|
+
scard: (key) => redisClient.scard(key),
|
|
145
|
+
zadd: (key, score, member) => redisClient.zadd(key, score, member),
|
|
146
|
+
zrange: (key, start, stop) => redisClient.zrange(key, start, stop),
|
|
147
|
+
ping: () => redisClient.ping(),
|
|
148
|
+
flushdb: () => redisClient.flushdb(),
|
|
149
|
+
flushall: () => redisClient.flushall()
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
getMetadata() {
|
|
153
|
+
return {
|
|
154
|
+
name: "redis",
|
|
155
|
+
version: "1.0.0",
|
|
156
|
+
description: "Redis plugin for ConduitHub - provides Redis client functionality",
|
|
157
|
+
author: "ConduitHub",
|
|
158
|
+
dependencies: [],
|
|
159
|
+
hooks: ["before-redis-operation", "after-redis-operation"],
|
|
160
|
+
events: ["redis-connected", "redis-disconnected", "redis-error"]
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
async onInitialize() {
|
|
164
|
+
this.logger.log?.("info", "Initializing Redis plugin...");
|
|
165
|
+
const config = this.getConfig().config;
|
|
166
|
+
try {
|
|
167
|
+
const redisOptions = {
|
|
168
|
+
host: config.host,
|
|
169
|
+
port: config.port,
|
|
170
|
+
db: config.db,
|
|
171
|
+
retryDelayOnFailover: config.retryDelayOnFailover,
|
|
172
|
+
maxRetriesPerRequest: config.maxRetriesPerRequest,
|
|
173
|
+
connectTimeout: config.connectionTimeout,
|
|
174
|
+
commandTimeout: config.commandTimeout
|
|
175
|
+
};
|
|
176
|
+
if (config.password) redisOptions.password = config.password;
|
|
177
|
+
if (config.username) redisOptions.username = config.username;
|
|
178
|
+
if (config.keyPrefix) redisOptions.keyPrefix = config.keyPrefix;
|
|
179
|
+
if (config.tls) redisOptions.tls = config.tls;
|
|
180
|
+
const redisClient = new Redis(redisOptions);
|
|
181
|
+
await redisClient.ping();
|
|
182
|
+
this.ioredisClient = redisClient;
|
|
183
|
+
this.redis = this.createRedisWrapper(redisClient);
|
|
184
|
+
this.logger.log?.("info", "Successfully connected to Redis server");
|
|
185
|
+
} catch (error) {
|
|
186
|
+
this.logger.log?.(
|
|
187
|
+
"error",
|
|
188
|
+
"Failed to connect to Redis server, using no-op client",
|
|
189
|
+
error
|
|
190
|
+
);
|
|
191
|
+
this.ioredisClient = void 0;
|
|
192
|
+
this.redis = createNoopRedisClient();
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
async onStart() {
|
|
196
|
+
this.isConnected = true;
|
|
197
|
+
const cfg = this.getConfig().config;
|
|
198
|
+
await this.emit("redis-connected", {
|
|
199
|
+
host: cfg.host,
|
|
200
|
+
port: cfg.port,
|
|
201
|
+
db: cfg.db,
|
|
202
|
+
connected: true,
|
|
203
|
+
timestamp: Date.now()
|
|
204
|
+
});
|
|
205
|
+
this.logger.log?.("info", "Redis plugin started");
|
|
206
|
+
}
|
|
207
|
+
async onStop() {
|
|
208
|
+
this.isConnected = false;
|
|
209
|
+
const cfg = this.getConfig().config;
|
|
210
|
+
await this.emit("redis-disconnected", {
|
|
211
|
+
host: cfg.host,
|
|
212
|
+
port: cfg.port,
|
|
213
|
+
db: cfg.db,
|
|
214
|
+
connected: false,
|
|
215
|
+
timestamp: Date.now()
|
|
216
|
+
});
|
|
217
|
+
this.logger.log?.("info", "Redis plugin stopped");
|
|
218
|
+
}
|
|
219
|
+
async onDestroy() {
|
|
220
|
+
try {
|
|
221
|
+
if (this.ioredisClient) {
|
|
222
|
+
await Promise.race([
|
|
223
|
+
this.ioredisClient.quit(),
|
|
224
|
+
new Promise(
|
|
225
|
+
(_, reject) => setTimeout(() => reject(new Error("Quit timeout")), 1e3)
|
|
226
|
+
)
|
|
227
|
+
]);
|
|
228
|
+
this.logger.log?.("info", "Redis connection closed gracefully");
|
|
229
|
+
}
|
|
230
|
+
} catch (error) {
|
|
231
|
+
this.logger.log?.("error", "Error closing Redis connection", error);
|
|
232
|
+
try {
|
|
233
|
+
if (this.ioredisClient) {
|
|
234
|
+
await Promise.race([
|
|
235
|
+
this.ioredisClient.disconnect(),
|
|
236
|
+
new Promise(
|
|
237
|
+
(_, reject) => setTimeout(
|
|
238
|
+
() => reject(new Error("Force disconnect timeout")),
|
|
239
|
+
500
|
|
240
|
+
)
|
|
241
|
+
)
|
|
242
|
+
]);
|
|
243
|
+
this.logger.log?.("info", "Redis connection force disconnected");
|
|
244
|
+
}
|
|
245
|
+
} catch (disconnectError) {
|
|
246
|
+
this.logger.log?.(
|
|
247
|
+
"error",
|
|
248
|
+
"Error during force disconnect",
|
|
249
|
+
disconnectError
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
} finally {
|
|
253
|
+
this.ioredisClient = void 0;
|
|
254
|
+
this.redis = createNoopRedisClient();
|
|
255
|
+
this.logger.log?.("info", "Redis plugin destroyed");
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
async set(key, value, options) {
|
|
259
|
+
return this.run(
|
|
260
|
+
"set",
|
|
261
|
+
{ key, value, options },
|
|
262
|
+
() => this.redis.set(key, value, options)
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
async get(key) {
|
|
266
|
+
return this.run("get", { key }, () => this.redis.get(key));
|
|
267
|
+
}
|
|
268
|
+
async del(key) {
|
|
269
|
+
return this.run("del", { key }, () => this.redis.del(key));
|
|
270
|
+
}
|
|
271
|
+
async exists(key) {
|
|
272
|
+
await this.executeHook("before-redis-operation", {
|
|
273
|
+
operation: "exists",
|
|
274
|
+
key
|
|
275
|
+
});
|
|
276
|
+
try {
|
|
277
|
+
const result = await this.redis.exists(key);
|
|
278
|
+
await this.executeHook("after-redis-operation", {
|
|
279
|
+
operation: "exists",
|
|
280
|
+
key,
|
|
281
|
+
result
|
|
282
|
+
});
|
|
283
|
+
return result;
|
|
284
|
+
} catch (error) {
|
|
285
|
+
this.logger.log?.(
|
|
286
|
+
"error",
|
|
287
|
+
`Redis EXISTS operation failed for key: ${key}`,
|
|
288
|
+
error
|
|
289
|
+
);
|
|
290
|
+
throw error;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
async expire(key, seconds) {
|
|
294
|
+
await this.executeHook("before-redis-operation", {
|
|
295
|
+
operation: "expire",
|
|
296
|
+
key,
|
|
297
|
+
seconds
|
|
298
|
+
});
|
|
299
|
+
try {
|
|
300
|
+
const result = await this.redis.expire(key, seconds);
|
|
301
|
+
await this.executeHook("after-redis-operation", {
|
|
302
|
+
operation: "expire",
|
|
303
|
+
key,
|
|
304
|
+
seconds,
|
|
305
|
+
result
|
|
306
|
+
});
|
|
307
|
+
return result;
|
|
308
|
+
} catch (error) {
|
|
309
|
+
this.logger.log?.(
|
|
310
|
+
"error",
|
|
311
|
+
`Redis EXPIRE operation failed for key: ${key}`,
|
|
312
|
+
error
|
|
313
|
+
);
|
|
314
|
+
throw error;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
async ttl(key) {
|
|
318
|
+
await this.executeHook("before-redis-operation", { operation: "ttl", key });
|
|
319
|
+
try {
|
|
320
|
+
const result = await this.redis.ttl(key);
|
|
321
|
+
await this.executeHook("after-redis-operation", {
|
|
322
|
+
operation: "ttl",
|
|
323
|
+
key,
|
|
324
|
+
result
|
|
325
|
+
});
|
|
326
|
+
return result;
|
|
327
|
+
} catch (error) {
|
|
328
|
+
this.logger.log?.(
|
|
329
|
+
"error",
|
|
330
|
+
`Redis TTL operation failed for key: ${key}`,
|
|
331
|
+
error
|
|
332
|
+
);
|
|
333
|
+
throw error;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
// Hash operations
|
|
337
|
+
async hset(key, field, value) {
|
|
338
|
+
await this.executeHook("before-redis-operation", {
|
|
339
|
+
operation: "hset",
|
|
340
|
+
key,
|
|
341
|
+
field,
|
|
342
|
+
value
|
|
343
|
+
});
|
|
344
|
+
try {
|
|
345
|
+
const result = await this.redis.hset(key, field, value);
|
|
346
|
+
await this.executeHook("after-redis-operation", {
|
|
347
|
+
operation: "hset",
|
|
348
|
+
key,
|
|
349
|
+
field,
|
|
350
|
+
value,
|
|
351
|
+
result
|
|
352
|
+
});
|
|
353
|
+
return result;
|
|
354
|
+
} catch (error) {
|
|
355
|
+
this.logger.log?.(
|
|
356
|
+
"error",
|
|
357
|
+
`Redis HSET operation failed for key: ${key}`,
|
|
358
|
+
error
|
|
359
|
+
);
|
|
360
|
+
throw error;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
async hget(key, field) {
|
|
364
|
+
await this.executeHook("before-redis-operation", {
|
|
365
|
+
operation: "hget",
|
|
366
|
+
key,
|
|
367
|
+
field
|
|
368
|
+
});
|
|
369
|
+
try {
|
|
370
|
+
const result = await this.redis.hget(key, field);
|
|
371
|
+
await this.executeHook("after-redis-operation", {
|
|
372
|
+
operation: "hget",
|
|
373
|
+
key,
|
|
374
|
+
field,
|
|
375
|
+
result
|
|
376
|
+
});
|
|
377
|
+
return result;
|
|
378
|
+
} catch (error) {
|
|
379
|
+
this.logger.log?.(
|
|
380
|
+
"error",
|
|
381
|
+
`Redis HGET operation failed for key: ${key}`,
|
|
382
|
+
error
|
|
383
|
+
);
|
|
384
|
+
throw error;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
async hgetall(key) {
|
|
388
|
+
await this.executeHook("before-redis-operation", {
|
|
389
|
+
operation: "hgetall",
|
|
390
|
+
key
|
|
391
|
+
});
|
|
392
|
+
try {
|
|
393
|
+
const result = await this.redis.hgetall(key);
|
|
394
|
+
await this.executeHook("after-redis-operation", {
|
|
395
|
+
operation: "hgetall",
|
|
396
|
+
key,
|
|
397
|
+
result
|
|
398
|
+
});
|
|
399
|
+
return result;
|
|
400
|
+
} catch (error) {
|
|
401
|
+
this.logger.log?.(
|
|
402
|
+
"error",
|
|
403
|
+
`Redis HGETALL operation failed for key: ${key}`,
|
|
404
|
+
error
|
|
405
|
+
);
|
|
406
|
+
throw error;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
async hdel(key, field) {
|
|
410
|
+
await this.executeHook("before-redis-operation", {
|
|
411
|
+
operation: "hdel",
|
|
412
|
+
key,
|
|
413
|
+
field
|
|
414
|
+
});
|
|
415
|
+
try {
|
|
416
|
+
const result = await this.redis.hdel(key, field);
|
|
417
|
+
await this.executeHook("after-redis-operation", {
|
|
418
|
+
operation: "hdel",
|
|
419
|
+
key,
|
|
420
|
+
field,
|
|
421
|
+
result
|
|
422
|
+
});
|
|
423
|
+
return result;
|
|
424
|
+
} catch (error) {
|
|
425
|
+
this.logger.log?.(
|
|
426
|
+
"error",
|
|
427
|
+
`Redis HDEL operation failed for key: ${key}`,
|
|
428
|
+
error
|
|
429
|
+
);
|
|
430
|
+
throw error;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
// List operations
|
|
434
|
+
async lpush(key, ...elements) {
|
|
435
|
+
await this.executeHook("before-redis-operation", {
|
|
436
|
+
operation: "lpush",
|
|
437
|
+
key,
|
|
438
|
+
elements
|
|
439
|
+
});
|
|
440
|
+
try {
|
|
441
|
+
const result = await this.redis.lpush(key, ...elements);
|
|
442
|
+
await this.executeHook("after-redis-operation", {
|
|
443
|
+
operation: "lpush",
|
|
444
|
+
key,
|
|
445
|
+
elements,
|
|
446
|
+
result
|
|
447
|
+
});
|
|
448
|
+
return result;
|
|
449
|
+
} catch (error) {
|
|
450
|
+
this.logger.log?.(
|
|
451
|
+
"error",
|
|
452
|
+
`Redis LPUSH operation failed for key: ${key}`,
|
|
453
|
+
error
|
|
454
|
+
);
|
|
455
|
+
throw error;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
async rpush(key, ...elements) {
|
|
459
|
+
await this.executeHook("before-redis-operation", {
|
|
460
|
+
operation: "rpush",
|
|
461
|
+
key,
|
|
462
|
+
elements
|
|
463
|
+
});
|
|
464
|
+
try {
|
|
465
|
+
const result = await this.redis.rpush(key, ...elements);
|
|
466
|
+
await this.executeHook("after-redis-operation", {
|
|
467
|
+
operation: "rpush",
|
|
468
|
+
key,
|
|
469
|
+
elements,
|
|
470
|
+
result
|
|
471
|
+
});
|
|
472
|
+
return result;
|
|
473
|
+
} catch (error) {
|
|
474
|
+
this.logger.log?.(
|
|
475
|
+
"error",
|
|
476
|
+
`Redis RPUSH operation failed for key: ${key}`,
|
|
477
|
+
error
|
|
478
|
+
);
|
|
479
|
+
throw error;
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
async lpop(key) {
|
|
483
|
+
await this.executeHook("before-redis-operation", {
|
|
484
|
+
operation: "lpop",
|
|
485
|
+
key
|
|
486
|
+
});
|
|
487
|
+
try {
|
|
488
|
+
const result = await this.redis.lpop(key);
|
|
489
|
+
await this.executeHook("after-redis-operation", {
|
|
490
|
+
operation: "lpop",
|
|
491
|
+
key,
|
|
492
|
+
result
|
|
493
|
+
});
|
|
494
|
+
return result;
|
|
495
|
+
} catch (error) {
|
|
496
|
+
this.logger.log?.(
|
|
497
|
+
"error",
|
|
498
|
+
`Redis LPOP operation failed for key: ${key}`,
|
|
499
|
+
error
|
|
500
|
+
);
|
|
501
|
+
throw error;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
async rpop(key) {
|
|
505
|
+
await this.executeHook("before-redis-operation", {
|
|
506
|
+
operation: "rpop",
|
|
507
|
+
key
|
|
508
|
+
});
|
|
509
|
+
try {
|
|
510
|
+
const result = await this.redis.rpop(key);
|
|
511
|
+
await this.executeHook("after-redis-operation", {
|
|
512
|
+
operation: "rpop",
|
|
513
|
+
key,
|
|
514
|
+
result
|
|
515
|
+
});
|
|
516
|
+
return result;
|
|
517
|
+
} catch (error) {
|
|
518
|
+
this.logger.log?.(
|
|
519
|
+
"error",
|
|
520
|
+
`Redis RPOP operation failed for key: ${key}`,
|
|
521
|
+
error
|
|
522
|
+
);
|
|
523
|
+
throw error;
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
async llen(key) {
|
|
527
|
+
await this.executeHook("before-redis-operation", {
|
|
528
|
+
operation: "llen",
|
|
529
|
+
key
|
|
530
|
+
});
|
|
531
|
+
try {
|
|
532
|
+
const result = await this.redis.llen(key);
|
|
533
|
+
await this.executeHook("after-redis-operation", {
|
|
534
|
+
operation: "llen",
|
|
535
|
+
key,
|
|
536
|
+
result
|
|
537
|
+
});
|
|
538
|
+
return result;
|
|
539
|
+
} catch (error) {
|
|
540
|
+
this.logger.log?.(
|
|
541
|
+
"error",
|
|
542
|
+
`Redis LLEN operation failed for key: ${key}`,
|
|
543
|
+
error
|
|
544
|
+
);
|
|
545
|
+
throw error;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
async lrange(key, start, stop) {
|
|
549
|
+
await this.executeHook("before-redis-operation", {
|
|
550
|
+
operation: "lrange",
|
|
551
|
+
key,
|
|
552
|
+
start,
|
|
553
|
+
stop
|
|
554
|
+
});
|
|
555
|
+
try {
|
|
556
|
+
const result = await this.redis.lrange(key, start, stop);
|
|
557
|
+
await this.executeHook("after-redis-operation", {
|
|
558
|
+
operation: "lrange",
|
|
559
|
+
key,
|
|
560
|
+
start,
|
|
561
|
+
stop,
|
|
562
|
+
result
|
|
563
|
+
});
|
|
564
|
+
return result;
|
|
565
|
+
} catch (error) {
|
|
566
|
+
this.logger.log?.(
|
|
567
|
+
"error",
|
|
568
|
+
`Redis LRANGE operation failed for key: ${key}`,
|
|
569
|
+
error
|
|
570
|
+
);
|
|
571
|
+
throw error;
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
// Set operations
|
|
575
|
+
async sadd(key, ...members) {
|
|
576
|
+
await this.executeHook("before-redis-operation", {
|
|
577
|
+
operation: "sadd",
|
|
578
|
+
key,
|
|
579
|
+
members
|
|
580
|
+
});
|
|
581
|
+
try {
|
|
582
|
+
const result = await this.redis.sadd(key, ...members);
|
|
583
|
+
await this.executeHook("after-redis-operation", {
|
|
584
|
+
operation: "sadd",
|
|
585
|
+
key,
|
|
586
|
+
members,
|
|
587
|
+
result
|
|
588
|
+
});
|
|
589
|
+
return result;
|
|
590
|
+
} catch (error) {
|
|
591
|
+
this.logger.log?.(
|
|
592
|
+
"error",
|
|
593
|
+
`Redis SADD operation failed for key: ${key}`,
|
|
594
|
+
error
|
|
595
|
+
);
|
|
596
|
+
throw error;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
async srem(key, ...members) {
|
|
600
|
+
await this.executeHook("before-redis-operation", {
|
|
601
|
+
operation: "srem",
|
|
602
|
+
key,
|
|
603
|
+
members
|
|
604
|
+
});
|
|
605
|
+
try {
|
|
606
|
+
const result = await this.redis.srem(key, ...members);
|
|
607
|
+
await this.executeHook("after-redis-operation", {
|
|
608
|
+
operation: "srem",
|
|
609
|
+
key,
|
|
610
|
+
members,
|
|
611
|
+
result
|
|
612
|
+
});
|
|
613
|
+
return result;
|
|
614
|
+
} catch (error) {
|
|
615
|
+
this.logger.log?.(
|
|
616
|
+
"error",
|
|
617
|
+
`Redis SREM operation failed for key: ${key}`,
|
|
618
|
+
error
|
|
619
|
+
);
|
|
620
|
+
throw error;
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
async smembers(key) {
|
|
624
|
+
await this.executeHook("before-redis-operation", {
|
|
625
|
+
operation: "smembers",
|
|
626
|
+
key
|
|
627
|
+
});
|
|
628
|
+
try {
|
|
629
|
+
const result = await this.redis.smembers(key);
|
|
630
|
+
await this.executeHook("after-redis-operation", {
|
|
631
|
+
operation: "smembers",
|
|
632
|
+
key,
|
|
633
|
+
result
|
|
634
|
+
});
|
|
635
|
+
return result;
|
|
636
|
+
} catch (error) {
|
|
637
|
+
this.logger.log?.(
|
|
638
|
+
"error",
|
|
639
|
+
`Redis SMEMBERS operation failed for key: ${key}`,
|
|
640
|
+
error
|
|
641
|
+
);
|
|
642
|
+
throw error;
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
async sismember(key, member) {
|
|
646
|
+
await this.executeHook("before-redis-operation", {
|
|
647
|
+
operation: "sismember",
|
|
648
|
+
key,
|
|
649
|
+
member
|
|
650
|
+
});
|
|
651
|
+
try {
|
|
652
|
+
const result = await this.redis.sismember(key, member);
|
|
653
|
+
await this.executeHook("after-redis-operation", {
|
|
654
|
+
operation: "sismember",
|
|
655
|
+
key,
|
|
656
|
+
member,
|
|
657
|
+
result
|
|
658
|
+
});
|
|
659
|
+
return result;
|
|
660
|
+
} catch (error) {
|
|
661
|
+
this.logger.log?.(
|
|
662
|
+
"error",
|
|
663
|
+
`Redis SISMEMBER operation failed for key: ${key}`,
|
|
664
|
+
error
|
|
665
|
+
);
|
|
666
|
+
throw error;
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
async scard(key) {
|
|
670
|
+
await this.executeHook("before-redis-operation", {
|
|
671
|
+
operation: "scard",
|
|
672
|
+
key
|
|
673
|
+
});
|
|
674
|
+
try {
|
|
675
|
+
const result = await this.redis.scard(key);
|
|
676
|
+
await this.executeHook("after-redis-operation", {
|
|
677
|
+
operation: "scard",
|
|
678
|
+
key,
|
|
679
|
+
result
|
|
680
|
+
});
|
|
681
|
+
return result;
|
|
682
|
+
} catch (error) {
|
|
683
|
+
this.logger.log?.(
|
|
684
|
+
"error",
|
|
685
|
+
`Redis SCARD operation failed for key: ${key}`,
|
|
686
|
+
error
|
|
687
|
+
);
|
|
688
|
+
throw error;
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
// Sorted set operations
|
|
692
|
+
async zadd(key, score, member) {
|
|
693
|
+
await this.executeHook("before-redis-operation", {
|
|
694
|
+
operation: "zadd",
|
|
695
|
+
key,
|
|
696
|
+
score,
|
|
697
|
+
member
|
|
698
|
+
});
|
|
699
|
+
try {
|
|
700
|
+
const result = await this.redis.zadd(key, score, member);
|
|
701
|
+
await this.executeHook("after-redis-operation", {
|
|
702
|
+
operation: "zadd",
|
|
703
|
+
key,
|
|
704
|
+
score,
|
|
705
|
+
member,
|
|
706
|
+
result
|
|
707
|
+
});
|
|
708
|
+
return result;
|
|
709
|
+
} catch (error) {
|
|
710
|
+
this.logger.log?.(
|
|
711
|
+
"error",
|
|
712
|
+
`Redis ZADD operation failed for key: ${key}`,
|
|
713
|
+
error
|
|
714
|
+
);
|
|
715
|
+
throw error;
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
async zrange(key, start, stop) {
|
|
719
|
+
await this.executeHook("before-redis-operation", {
|
|
720
|
+
operation: "zrange",
|
|
721
|
+
key,
|
|
722
|
+
start,
|
|
723
|
+
stop
|
|
724
|
+
});
|
|
725
|
+
try {
|
|
726
|
+
const result = await this.redis.zrange(key, start, stop);
|
|
727
|
+
await this.executeHook("after-redis-operation", {
|
|
728
|
+
operation: "zrange",
|
|
729
|
+
key,
|
|
730
|
+
start,
|
|
731
|
+
stop,
|
|
732
|
+
result
|
|
733
|
+
});
|
|
734
|
+
return result;
|
|
735
|
+
} catch (error) {
|
|
736
|
+
this.logger.log?.(
|
|
737
|
+
"error",
|
|
738
|
+
`Redis ZRANGE operation failed for key: ${key}`,
|
|
739
|
+
error
|
|
740
|
+
);
|
|
741
|
+
throw error;
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
// Utility methods
|
|
745
|
+
getConnectionInfo() {
|
|
746
|
+
const cfg = this.getConfig().config;
|
|
747
|
+
return {
|
|
748
|
+
host: cfg.host,
|
|
749
|
+
port: cfg.port,
|
|
750
|
+
db: cfg.db,
|
|
751
|
+
connected: this.isConnected,
|
|
752
|
+
timestamp: Date.now()
|
|
753
|
+
};
|
|
754
|
+
}
|
|
755
|
+
getStats() {
|
|
756
|
+
return { ...this.stats };
|
|
757
|
+
}
|
|
758
|
+
async ping() {
|
|
759
|
+
return this.run("ping", {}, () => this.redis.ping());
|
|
760
|
+
}
|
|
761
|
+
async flushdb() {
|
|
762
|
+
return this.run("flushdb", {}, () => this.redis.flushdb());
|
|
763
|
+
}
|
|
764
|
+
async flushall() {
|
|
765
|
+
return this.run("flushall", {}, () => this.redis.flushall());
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
export { RedisPlugin as R };
|