@mulingai-npm/redis 1.1.3 → 1.2.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/dist/index.d.ts CHANGED
@@ -2,13 +2,18 @@ export interface RedisConfig {
2
2
  host: string;
3
3
  port: number;
4
4
  password?: string;
5
- db?: number;
5
+ db: number;
6
6
  }
7
7
  export declare class RedisClient {
8
8
  private client;
9
9
  constructor(config: RedisConfig);
10
10
  set(key: string, value: string): Promise<void>;
11
11
  get(key: string): Promise<string | null>;
12
+ hset(key: string, data: Record<string, string>): Promise<number>;
13
+ hgetall(key: string): Promise<Record<string, string>>;
14
+ sadd(key: string, ...values: string[]): Promise<number>;
15
+ smembers(key: string): Promise<string[]>;
16
+ keys(pattern: string): Promise<string[]>;
12
17
  flushAll(): Promise<string>;
13
18
  flushDb(): Promise<string>;
14
19
  }
package/dist/index.js CHANGED
@@ -4,31 +4,51 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.RedisClient = void 0;
7
+ // redis-client.ts
7
8
  const ioredis_1 = __importDefault(require("ioredis"));
8
9
  class RedisClient {
9
10
  constructor(config) {
10
- var _a;
11
11
  this.client = new ioredis_1.default({
12
12
  host: config.host,
13
13
  port: config.port,
14
14
  password: config.password,
15
- db: (_a = config.db) !== null && _a !== void 0 ? _a : 0
15
+ db: config.db
16
16
  });
17
17
  this.client.on('error', (err) => {
18
18
  console.error('Redis error:', err);
19
19
  });
20
20
  }
21
+ // existing set/get
21
22
  async set(key, value) {
22
23
  await this.client.set(key, value);
23
24
  }
24
25
  async get(key) {
25
26
  return this.client.get(key);
26
27
  }
27
- // Flush ALL databases
28
+ // add these methods for hash, sets, keys, etc.
29
+ async hset(key, data) {
30
+ return this.client.hset(key, data);
31
+ }
32
+ async hgetall(key) {
33
+ return this.client.hgetall(key);
34
+ }
35
+ // Insert one or more values into a set
36
+ async sadd(key, ...values) {
37
+ return this.client.sadd(key, values);
38
+ }
39
+ // Get all set members
40
+ async smembers(key) {
41
+ return this.client.smembers(key);
42
+ }
43
+ // Search for all keys matching a pattern
44
+ // NOTE: If you have lots of keys in Redis, you should look into using SCAN instead of KEYS
45
+ async keys(pattern) {
46
+ return this.client.keys(pattern);
47
+ }
48
+ // flush methods
28
49
  async flushAll() {
29
50
  return this.client.flushall();
30
51
  }
31
- // Flush ONLY the current database
32
52
  async flushDb() {
33
53
  return this.client.flushdb();
34
54
  }
@@ -0,0 +1,24 @@
1
+ import { RedisClient } from '../redis-client';
2
+ export type MulingstreamListenerData = {
3
+ listenerId: string;
4
+ roomId: string;
5
+ socketId?: string;
6
+ token: string;
7
+ name?: string;
8
+ firstJoined: number;
9
+ language?: string;
10
+ isActive?: boolean;
11
+ };
12
+ export declare class MulingstreamListenerManager {
13
+ private redisClient;
14
+ constructor(redisClient: RedisClient);
15
+ private parseHashData;
16
+ /**
17
+ * Creates a new listener.
18
+ * 1) Generates a unique listenerId and adds it to the room's set.
19
+ * 2) Stores the listener data in a Redis hash under `listener:{listenerId}`.
20
+ */
21
+ addListener(listenerData: Omit<MulingstreamListenerData, 'listenerId'>): Promise<string>;
22
+ getAllListeners(): Promise<MulingstreamListenerData[]>;
23
+ getListenersByRoom(roomId: string): Promise<MulingstreamListenerData[]>;
24
+ }
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MulingstreamListenerManager = void 0;
4
+ const uuid_1 = require("uuid");
5
+ class MulingstreamListenerManager {
6
+ constructor(redisClient) {
7
+ this.redisClient = redisClient;
8
+ }
9
+ parseHashData(data) {
10
+ return {
11
+ listenerId: data.listenerId,
12
+ roomId: data.roomId,
13
+ socketId: data.socketId || '',
14
+ token: data.token,
15
+ name: data.name || '',
16
+ firstJoined: parseInt(data.firstJoined, 10),
17
+ language: data.language || '',
18
+ isActive: data.isActive === 'true'
19
+ };
20
+ }
21
+ /**
22
+ * Creates a new listener.
23
+ * 1) Generates a unique listenerId and adds it to the room's set.
24
+ * 2) Stores the listener data in a Redis hash under `listener:{listenerId}`.
25
+ */
26
+ async addListener(listenerData) {
27
+ var _a, _b, _c;
28
+ // You can combine roomId + uuid, or just a uuid, up to you:
29
+ const listenerId = `${listenerData.roomId}--${(0, uuid_1.v4)()}`;
30
+ // Add listenerId to the set for that room
31
+ await this.redisClient.sadd(`room:${listenerData.roomId}:listeners`, listenerId);
32
+ // Store listener data in a hash
33
+ await this.redisClient.hset(`listener:${listenerId}`, {
34
+ listenerId,
35
+ roomId: listenerData.roomId,
36
+ socketId: (_a = listenerData.socketId) !== null && _a !== void 0 ? _a : '',
37
+ token: listenerData.token,
38
+ name: (_b = listenerData.name) !== null && _b !== void 0 ? _b : '',
39
+ firstJoined: listenerData.firstJoined.toString(),
40
+ language: (_c = listenerData.language) !== null && _c !== void 0 ? _c : '',
41
+ isActive: listenerData.isActive ? 'true' : 'false'
42
+ });
43
+ return listenerId;
44
+ }
45
+ async getAllListeners() {
46
+ // 1) Get all keys that match 'listener:*'
47
+ // If you have many keys, consider using SCAN instead of KEYS to avoid performance issues.
48
+ const keys = await this.redisClient.keys('listener:*');
49
+ if (!keys || keys.length === 0) {
50
+ return [];
51
+ }
52
+ // 2) Fetch each hash with HGETALL
53
+ const listeners = [];
54
+ for (const key of keys) {
55
+ const data = await this.redisClient.hgetall(key);
56
+ listeners.push(this.parseHashData(data));
57
+ }
58
+ return listeners;
59
+ }
60
+ async getListenersByRoom(roomId) {
61
+ // 1) Get the set of listener IDs
62
+ const listenerIds = await this.redisClient.smembers(`room:${roomId}:listeners`);
63
+ if (!listenerIds || listenerIds.length === 0) {
64
+ return [];
65
+ }
66
+ // 2) For each ID, fetch the hash from Redis
67
+ const listeners = [];
68
+ for (const id of listenerIds) {
69
+ const data = await this.redisClient.hgetall(`listener:${id}`);
70
+ if (data) {
71
+ listeners.push(this.parseHashData(data));
72
+ }
73
+ }
74
+ return listeners;
75
+ }
76
+ }
77
+ exports.MulingstreamListenerManager = MulingstreamListenerManager;
@@ -0,0 +1,19 @@
1
+ export interface RedisConfig {
2
+ host: string;
3
+ port: number;
4
+ password?: string;
5
+ db: number;
6
+ }
7
+ export declare class RedisClient {
8
+ private client;
9
+ constructor(config: RedisConfig);
10
+ set(key: string, value: string): Promise<void>;
11
+ get(key: string): Promise<string | null>;
12
+ hset(key: string, data: Record<string, string>): Promise<number>;
13
+ hgetall(key: string): Promise<Record<string, string>>;
14
+ sadd(key: string, ...values: string[]): Promise<number>;
15
+ smembers(key: string): Promise<string[]>;
16
+ keys(pattern: string): Promise<string[]>;
17
+ flushAll(): Promise<string>;
18
+ flushDb(): Promise<string>;
19
+ }
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.RedisClient = void 0;
7
+ const ioredis_1 = __importDefault(require("ioredis"));
8
+ class RedisClient {
9
+ constructor(config) {
10
+ this.client = new ioredis_1.default({
11
+ host: config.host,
12
+ port: config.port,
13
+ password: config.password,
14
+ db: config.db
15
+ });
16
+ this.client.on('error', (err) => {
17
+ console.error('Redis error:', err);
18
+ });
19
+ }
20
+ // existing set/get
21
+ async set(key, value) {
22
+ await this.client.set(key, value);
23
+ }
24
+ async get(key) {
25
+ return this.client.get(key);
26
+ }
27
+ // add these methods for hash, sets, keys, etc.
28
+ async hset(key, data) {
29
+ return this.client.hset(key, data);
30
+ }
31
+ async hgetall(key) {
32
+ return this.client.hgetall(key);
33
+ }
34
+ // Insert one or more values into a set
35
+ async sadd(key, ...values) {
36
+ return this.client.sadd(key, values);
37
+ }
38
+ // Get all set members
39
+ async smembers(key) {
40
+ return this.client.smembers(key);
41
+ }
42
+ // Search for all keys matching a pattern
43
+ // NOTE: If you have lots of keys in Redis, you should look into using SCAN instead of KEYS
44
+ async keys(pattern) {
45
+ return this.client.keys(pattern);
46
+ }
47
+ // flush methods
48
+ async flushAll() {
49
+ return this.client.flushall();
50
+ }
51
+ async flushDb() {
52
+ return this.client.flushdb();
53
+ }
54
+ }
55
+ exports.RedisClient = RedisClient;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mulingai-npm/redis",
3
- "version": "1.1.3",
3
+ "version": "1.2.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "repository": {