@chat-adapter/state-ioredis 4.18.0 → 4.19.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 +56 -4
- package/dist/index.d.ts +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -3,12 +3,12 @@
|
|
|
3
3
|
[](https://www.npmjs.com/package/@chat-adapter/state-ioredis)
|
|
4
4
|
[](https://www.npmjs.com/package/@chat-adapter/state-ioredis)
|
|
5
5
|
|
|
6
|
-
Redis state adapter for [Chat SDK](https://chat-sdk.dev
|
|
6
|
+
Alternative Redis state adapter for [Chat SDK](https://chat-sdk.dev) using [ioredis](https://www.npmjs.com/package/ioredis). Use this if you already have ioredis in your project or need Redis Cluster/Sentinel support.
|
|
7
7
|
|
|
8
8
|
## Installation
|
|
9
9
|
|
|
10
10
|
```bash
|
|
11
|
-
|
|
11
|
+
pnpm add @chat-adapter/state-ioredis
|
|
12
12
|
```
|
|
13
13
|
|
|
14
14
|
## Usage
|
|
@@ -26,9 +26,61 @@ const bot = new Chat({
|
|
|
26
26
|
});
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
### Using an existing client
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
```typescript
|
|
32
|
+
import Redis from "ioredis";
|
|
33
|
+
|
|
34
|
+
const client = new Redis("redis://localhost:6379");
|
|
35
|
+
|
|
36
|
+
const state = createIORedisState({ client });
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Configuration
|
|
40
|
+
|
|
41
|
+
| Option | Required | Description |
|
|
42
|
+
|--------|----------|-------------|
|
|
43
|
+
| `url` | Yes* | Redis connection URL |
|
|
44
|
+
| `client` | No | Existing `ioredis` client instance |
|
|
45
|
+
| `keyPrefix` | No | Prefix for all keys (default: `"chat-sdk"`) |
|
|
46
|
+
|
|
47
|
+
*Either `url` or `client` is required.
|
|
48
|
+
|
|
49
|
+
## When to use ioredis vs redis
|
|
50
|
+
|
|
51
|
+
**Use `@chat-adapter/state-ioredis` when:**
|
|
52
|
+
|
|
53
|
+
- You already use ioredis in your project
|
|
54
|
+
- You need Redis Cluster support
|
|
55
|
+
- You need Redis Sentinel support
|
|
56
|
+
- You prefer the ioredis API
|
|
57
|
+
|
|
58
|
+
**Use `@chat-adapter/state-redis` when:**
|
|
59
|
+
|
|
60
|
+
- You want the official Redis client
|
|
61
|
+
- You're starting a new project
|
|
62
|
+
- You don't need Cluster or Sentinel
|
|
63
|
+
|
|
64
|
+
## Key structure
|
|
65
|
+
|
|
66
|
+
```
|
|
67
|
+
{keyPrefix}:subscriptions - SET of subscribed thread IDs
|
|
68
|
+
{keyPrefix}:lock:{threadId} - Lock key with TTL
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Features
|
|
72
|
+
|
|
73
|
+
| Feature | Supported |
|
|
74
|
+
|---------|-----------|
|
|
75
|
+
| Persistence | Yes |
|
|
76
|
+
| Multi-instance | Yes |
|
|
77
|
+
| Subscriptions | Yes |
|
|
78
|
+
| Distributed locking | Yes |
|
|
79
|
+
| Key-value caching | Yes |
|
|
80
|
+
| Automatic reconnection | Yes |
|
|
81
|
+
| Redis Cluster support | Yes |
|
|
82
|
+
| Redis Sentinel support | Yes |
|
|
83
|
+
| Key prefix namespacing | Yes |
|
|
32
84
|
|
|
33
85
|
## License
|
|
34
86
|
|
package/dist/index.d.ts
CHANGED
|
@@ -49,6 +49,7 @@ declare class IoRedisStateAdapter implements StateAdapter {
|
|
|
49
49
|
unsubscribe(threadId: string): Promise<void>;
|
|
50
50
|
isSubscribed(threadId: string): Promise<boolean>;
|
|
51
51
|
acquireLock(threadId: string, ttlMs: number): Promise<Lock | null>;
|
|
52
|
+
forceReleaseLock(threadId: string): Promise<void>;
|
|
52
53
|
releaseLock(lock: Lock): Promise<void>;
|
|
53
54
|
extendLock(lock: Lock, ttlMs: number): Promise<boolean>;
|
|
54
55
|
get<T = unknown>(key: string): Promise<T | null>;
|
package/dist/index.js
CHANGED
|
@@ -86,6 +86,11 @@ var IoRedisStateAdapter = class {
|
|
|
86
86
|
}
|
|
87
87
|
return null;
|
|
88
88
|
}
|
|
89
|
+
async forceReleaseLock(threadId) {
|
|
90
|
+
this.ensureConnected();
|
|
91
|
+
const lockKey = this.key("lock", threadId);
|
|
92
|
+
await this.client.del(lockKey);
|
|
93
|
+
}
|
|
89
94
|
async releaseLock(lock) {
|
|
90
95
|
this.ensureConnected();
|
|
91
96
|
const lockKey = this.key("lock", lock.threadId);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { Lock, Logger, StateAdapter } from \"chat\";\nimport Redis from \"ioredis\";\n\nexport interface IoRedisStateAdapterOptions {\n /** Key prefix for all Redis keys (default: \"chat-sdk\") */\n keyPrefix?: string;\n /** Logger instance for error reporting */\n logger: Logger;\n /** Redis connection URL (e.g., redis://localhost:6379) */\n url: string;\n}\n\nexport interface IoRedisStateClientOptions {\n /** Existing ioredis client instance */\n client: Redis;\n /** Key prefix for all Redis keys (default: \"chat-sdk\") */\n keyPrefix?: string;\n /** Logger instance for error reporting */\n logger: Logger;\n}\n\n/**\n * Redis state adapter using ioredis for production use.\n *\n * Provides persistent subscriptions and distributed locking\n * across multiple server instances.\n *\n * @example\n * ```typescript\n * // With URL\n * const state = createIoRedisState({ url: process.env.REDIS_URL });\n *\n * // With existing client\n * const client = new Redis(process.env.REDIS_URL);\n * const state = createIoRedisState({ client });\n * ```\n */\nexport class IoRedisStateAdapter implements StateAdapter {\n private readonly client: Redis;\n private readonly keyPrefix: string;\n private readonly logger: Logger;\n private connected = false;\n private connectPromise: Promise<void> | null = null;\n private readonly ownsClient: boolean;\n\n constructor(options: IoRedisStateAdapterOptions | IoRedisStateClientOptions) {\n if (\"client\" in options) {\n this.client = options.client;\n this.ownsClient = false;\n } else {\n this.client = new Redis(options.url);\n this.ownsClient = true;\n }\n this.keyPrefix = options.keyPrefix || \"chat-sdk\";\n this.logger = options.logger;\n\n // Handle connection errors\n this.client.on(\"error\", (err) => {\n this.logger.error(\"ioredis client error\", { error: err });\n });\n }\n\n private key(type: \"sub\" | \"lock\" | \"cache\", id: string): string {\n return `${this.keyPrefix}:${type}:${id}`;\n }\n\n private subscriptionsSetKey(): string {\n return `${this.keyPrefix}:subscriptions`;\n }\n\n async connect(): Promise<void> {\n // ioredis auto-connects, but we track state for consistency\n if (this.connected) {\n return;\n }\n\n // Reuse existing connection attempt to avoid race conditions\n if (!this.connectPromise) {\n this.connectPromise = new Promise<void>((resolve, reject) => {\n if (this.client.status === \"ready\") {\n this.connected = true;\n resolve();\n return;\n }\n\n this.client.once(\"ready\", () => {\n this.connected = true;\n resolve();\n });\n\n this.client.once(\"error\", (err) => {\n reject(err);\n });\n });\n }\n\n await this.connectPromise;\n }\n\n async disconnect(): Promise<void> {\n if (this.connected && this.ownsClient) {\n await this.client.quit();\n this.connected = false;\n this.connectPromise = null;\n }\n }\n\n async subscribe(threadId: string): Promise<void> {\n this.ensureConnected();\n await this.client.sadd(this.subscriptionsSetKey(), threadId);\n }\n\n async unsubscribe(threadId: string): Promise<void> {\n this.ensureConnected();\n await this.client.srem(this.subscriptionsSetKey(), threadId);\n }\n\n async isSubscribed(threadId: string): Promise<boolean> {\n this.ensureConnected();\n const result = await this.client.sismember(\n this.subscriptionsSetKey(),\n threadId\n );\n return result === 1;\n }\n\n async acquireLock(threadId: string, ttlMs: number): Promise<Lock | null> {\n this.ensureConnected();\n\n const token = generateToken();\n const lockKey = this.key(\"lock\", threadId);\n\n // Use SET NX PX for atomic lock acquisition\n const acquired = await this.client.set(lockKey, token, \"PX\", ttlMs, \"NX\");\n\n if (acquired === \"OK\") {\n return {\n threadId,\n token,\n expiresAt: Date.now() + ttlMs,\n };\n }\n\n return null;\n }\n\n async releaseLock(lock: Lock): Promise<void> {\n this.ensureConnected();\n\n const lockKey = this.key(\"lock\", lock.threadId);\n\n // Use Lua script for atomic check-and-delete\n const script = `\n if redis.call(\"get\", KEYS[1]) == ARGV[1] then\n return redis.call(\"del\", KEYS[1])\n else\n return 0\n end\n `;\n\n await this.client.eval(script, 1, lockKey, lock.token);\n }\n\n async extendLock(lock: Lock, ttlMs: number): Promise<boolean> {\n this.ensureConnected();\n\n const lockKey = this.key(\"lock\", lock.threadId);\n\n // Use Lua script for atomic check-and-extend\n const script = `\n if redis.call(\"get\", KEYS[1]) == ARGV[1] then\n return redis.call(\"pexpire\", KEYS[1], ARGV[2])\n else\n return 0\n end\n `;\n\n const result = await this.client.eval(\n script,\n 1,\n lockKey,\n lock.token,\n ttlMs.toString()\n );\n\n return result === 1;\n }\n\n async get<T = unknown>(key: string): Promise<T | null> {\n this.ensureConnected();\n\n const cacheKey = this.key(\"cache\", key);\n const value = await this.client.get(cacheKey);\n\n if (value === null) {\n return null;\n }\n\n try {\n return JSON.parse(value) as T;\n } catch {\n // If parsing fails, return as string\n return value as unknown as T;\n }\n }\n\n async set<T = unknown>(key: string, value: T, ttlMs?: number): Promise<void> {\n this.ensureConnected();\n\n const cacheKey = this.key(\"cache\", key);\n const serialized = JSON.stringify(value);\n\n if (ttlMs) {\n await this.client.set(cacheKey, serialized, \"PX\", ttlMs);\n } else {\n await this.client.set(cacheKey, serialized);\n }\n }\n\n async setIfNotExists(\n key: string,\n value: unknown,\n ttlMs?: number\n ): Promise<boolean> {\n this.ensureConnected();\n\n const cacheKey = this.key(\"cache\", key);\n const serialized = JSON.stringify(value);\n\n const result = ttlMs\n ? await this.client.set(cacheKey, serialized, \"PX\", ttlMs, \"NX\")\n : await this.client.set(cacheKey, serialized, \"NX\");\n\n return result === \"OK\";\n }\n\n async delete(key: string): Promise<void> {\n this.ensureConnected();\n\n const cacheKey = this.key(\"cache\", key);\n await this.client.del(cacheKey);\n }\n\n private ensureConnected(): void {\n if (!this.connected) {\n throw new Error(\n \"IoRedisStateAdapter is not connected. Call connect() first.\"\n );\n }\n }\n\n /**\n * Get the underlying ioredis client for advanced usage.\n */\n getClient(): Redis {\n return this.client;\n }\n}\n\nfunction generateToken(): string {\n return `ioredis_${Date.now()}_${Math.random().toString(36).substring(2, 15)}`;\n}\n\n/**\n * Create an ioredis state adapter.\n *\n * @example\n * ```typescript\n * // With URL\n * const state = createIoRedisState({ url: process.env.REDIS_URL });\n *\n * // With existing client\n * import Redis from \"ioredis\";\n * const client = new Redis(process.env.REDIS_URL);\n * const state = createIoRedisState({ client });\n * ```\n */\nexport function createIoRedisState(\n options: IoRedisStateAdapterOptions | IoRedisStateClientOptions\n): IoRedisStateAdapter {\n return new IoRedisStateAdapter(options);\n}\n"],"mappings":";AACA,OAAO,WAAW;AAoCX,IAAM,sBAAN,MAAkD;AAAA,EACtC;AAAA,EACA;AAAA,EACA;AAAA,EACT,YAAY;AAAA,EACZ,iBAAuC;AAAA,EAC9B;AAAA,EAEjB,YAAY,SAAiE;AAC3E,QAAI,YAAY,SAAS;AACvB,WAAK,SAAS,QAAQ;AACtB,WAAK,aAAa;AAAA,IACpB,OAAO;AACL,WAAK,SAAS,IAAI,MAAM,QAAQ,GAAG;AACnC,WAAK,aAAa;AAAA,IACpB;AACA,SAAK,YAAY,QAAQ,aAAa;AACtC,SAAK,SAAS,QAAQ;AAGtB,SAAK,OAAO,GAAG,SAAS,CAAC,QAAQ;AAC/B,WAAK,OAAO,MAAM,wBAAwB,EAAE,OAAO,IAAI,CAAC;AAAA,IAC1D,CAAC;AAAA,EACH;AAAA,EAEQ,IAAI,MAAgC,IAAoB;AAC9D,WAAO,GAAG,KAAK,SAAS,IAAI,IAAI,IAAI,EAAE;AAAA,EACxC;AAAA,EAEQ,sBAA8B;AACpC,WAAO,GAAG,KAAK,SAAS;AAAA,EAC1B;AAAA,EAEA,MAAM,UAAyB;AAE7B,QAAI,KAAK,WAAW;AAClB;AAAA,IACF;AAGA,QAAI,CAAC,KAAK,gBAAgB;AACxB,WAAK,iBAAiB,IAAI,QAAc,CAAC,SAAS,W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tC,UAAM,aAAa,KAAK,UAAU,KAAK;AAEvC,UAAM,SAAS,QACX,MAAM,KAAK,OAAO,IAAI,UAAU,YAAY,MAAM,OAAO,IAAI,IAC7D,MAAM,KAAK,OAAO,IAAI,UAAU,YAAY,IAAI;AAEpD,WAAO,WAAW;AAAA,EACpB;AAAA,EAEA,MAAM,OAAO,KAA4B;AACvC,SAAK,gBAAgB;AAErB,UAAM,WAAW,KAAK,IAAI,SAAS,GAAG;AACtC,UAAM,KAAK,OAAO,IAAI,QAAQ;AAAA,EAChC;AAAA,EAEQ,kBAAwB;AAC9B,QAAI,CAAC,KAAK,WAAW;AACnB,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKA,YAAmB;AACjB,WAAO,KAAK;AAAA,EACd;AACF;AAEA,SAAS,gBAAwB;AAC/B,SAAO,WAAW,KAAK,IAAI,CAAC,IAAI,KAAK,OAAO,EAAE,SAAS,EAAE,EAAE,UAAU,GAAG,EAAE,CAAC;AAC7E;AAgBO,SAAS,mBACd,SACqB;AACrB,SAAO,IAAI,oBAAoB,OAAO;AACxC;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { Lock, Logger, StateAdapter } from \"chat\";\nimport Redis from \"ioredis\";\n\nexport interface IoRedisStateAdapterOptions {\n /** Key prefix for all Redis keys (default: \"chat-sdk\") */\n keyPrefix?: string;\n /** Logger instance for error reporting */\n logger: Logger;\n /** Redis connection URL (e.g., redis://localhost:6379) */\n url: string;\n}\n\nexport interface IoRedisStateClientOptions {\n /** Existing ioredis client instance */\n client: Redis;\n /** Key prefix for all Redis keys (default: \"chat-sdk\") */\n keyPrefix?: string;\n /** Logger instance for error reporting */\n logger: Logger;\n}\n\n/**\n * Redis state adapter using ioredis for production use.\n *\n * Provides persistent subscriptions and distributed locking\n * across multiple server instances.\n *\n * @example\n * ```typescript\n * // With URL\n * const state = createIoRedisState({ url: process.env.REDIS_URL });\n *\n * // With existing client\n * const client = new Redis(process.env.REDIS_URL);\n * const state = createIoRedisState({ client });\n * ```\n */\nexport class IoRedisStateAdapter implements StateAdapter {\n private readonly client: Redis;\n private readonly keyPrefix: string;\n private readonly logger: Logger;\n private connected = false;\n private connectPromise: Promise<void> | null = null;\n private readonly ownsClient: boolean;\n\n constructor(options: IoRedisStateAdapterOptions | IoRedisStateClientOptions) {\n if (\"client\" in options) {\n this.client = options.client;\n this.ownsClient = false;\n } else {\n this.client = new Redis(options.url);\n this.ownsClient = true;\n }\n this.keyPrefix = options.keyPrefix || \"chat-sdk\";\n this.logger = options.logger;\n\n // Handle connection errors\n this.client.on(\"error\", (err) => {\n this.logger.error(\"ioredis client error\", { error: err });\n });\n }\n\n private key(type: \"sub\" | \"lock\" | \"cache\", id: string): string {\n return `${this.keyPrefix}:${type}:${id}`;\n }\n\n private subscriptionsSetKey(): string {\n return `${this.keyPrefix}:subscriptions`;\n }\n\n async connect(): Promise<void> {\n // ioredis auto-connects, but we track state for consistency\n if (this.connected) {\n return;\n }\n\n // Reuse existing connection attempt to avoid race conditions\n if (!this.connectPromise) {\n this.connectPromise = new Promise<void>((resolve, reject) => {\n if (this.client.status === \"ready\") {\n this.connected = true;\n resolve();\n return;\n }\n\n this.client.once(\"ready\", () => {\n this.connected = true;\n resolve();\n });\n\n this.client.once(\"error\", (err) => {\n reject(err);\n });\n });\n }\n\n await this.connectPromise;\n }\n\n async disconnect(): Promise<void> {\n if (this.connected && this.ownsClient) {\n await this.client.quit();\n this.connected = false;\n this.connectPromise = null;\n }\n }\n\n async subscribe(threadId: string): Promise<void> {\n this.ensureConnected();\n await this.client.sadd(this.subscriptionsSetKey(), threadId);\n }\n\n async unsubscribe(threadId: string): Promise<void> {\n this.ensureConnected();\n await this.client.srem(this.subscriptionsSetKey(), threadId);\n }\n\n async isSubscribed(threadId: string): Promise<boolean> {\n this.ensureConnected();\n const result = await this.client.sismember(\n this.subscriptionsSetKey(),\n threadId\n );\n return result === 1;\n }\n\n async acquireLock(threadId: string, ttlMs: number): Promise<Lock | null> {\n this.ensureConnected();\n\n const token = generateToken();\n const lockKey = this.key(\"lock\", threadId);\n\n // Use SET NX PX for atomic lock acquisition\n const acquired = await this.client.set(lockKey, token, \"PX\", ttlMs, \"NX\");\n\n if (acquired === \"OK\") {\n return {\n threadId,\n token,\n expiresAt: Date.now() + ttlMs,\n };\n }\n\n return null;\n }\n\n async forceReleaseLock(threadId: string): Promise<void> {\n this.ensureConnected();\n const lockKey = this.key(\"lock\", threadId);\n await this.client.del(lockKey);\n }\n\n async releaseLock(lock: Lock): Promise<void> {\n this.ensureConnected();\n\n const lockKey = this.key(\"lock\", lock.threadId);\n\n // Use Lua script for atomic check-and-delete\n const script = `\n if redis.call(\"get\", KEYS[1]) == ARGV[1] then\n return redis.call(\"del\", KEYS[1])\n else\n return 0\n end\n `;\n\n await this.client.eval(script, 1, lockKey, lock.token);\n }\n\n async extendLock(lock: Lock, ttlMs: number): Promise<boolean> {\n this.ensureConnected();\n\n const lockKey = this.key(\"lock\", lock.threadId);\n\n // Use Lua script for atomic check-and-extend\n const script = `\n if redis.call(\"get\", KEYS[1]) == ARGV[1] then\n return redis.call(\"pexpire\", KEYS[1], ARGV[2])\n else\n return 0\n end\n `;\n\n const result = await this.client.eval(\n script,\n 1,\n lockKey,\n lock.token,\n ttlMs.toString()\n );\n\n return result === 1;\n }\n\n async get<T = unknown>(key: string): Promise<T | null> {\n this.ensureConnected();\n\n const cacheKey = this.key(\"cache\", key);\n const value = await this.client.get(cacheKey);\n\n if (value === null) {\n return null;\n }\n\n try {\n return JSON.parse(value) as T;\n } catch {\n // If parsing fails, return as string\n return value as unknown as T;\n }\n }\n\n async set<T = unknown>(key: string, value: T, ttlMs?: number): Promise<void> {\n this.ensureConnected();\n\n const cacheKey = this.key(\"cache\", key);\n const serialized = JSON.stringify(value);\n\n if (ttlMs) {\n await this.client.set(cacheKey, serialized, \"PX\", ttlMs);\n } else {\n await this.client.set(cacheKey, serialized);\n }\n }\n\n async setIfNotExists(\n key: string,\n value: unknown,\n ttlMs?: number\n ): Promise<boolean> {\n this.ensureConnected();\n\n const cacheKey = this.key(\"cache\", key);\n const serialized = JSON.stringify(value);\n\n const result = ttlMs\n ? await this.client.set(cacheKey, serialized, \"PX\", ttlMs, \"NX\")\n : await this.client.set(cacheKey, serialized, \"NX\");\n\n return result === \"OK\";\n }\n\n async delete(key: string): Promise<void> {\n this.ensureConnected();\n\n const cacheKey = this.key(\"cache\", key);\n await this.client.del(cacheKey);\n }\n\n private ensureConnected(): void {\n if (!this.connected) {\n throw new Error(\n \"IoRedisStateAdapter is not connected. Call connect() first.\"\n );\n }\n }\n\n /**\n * Get the underlying ioredis client for advanced usage.\n */\n getClient(): Redis {\n return this.client;\n }\n}\n\nfunction generateToken(): string {\n return `ioredis_${Date.now()}_${Math.random().toString(36).substring(2, 15)}`;\n}\n\n/**\n * Create an ioredis state adapter.\n *\n * @example\n * ```typescript\n * // With URL\n * const state = createIoRedisState({ url: process.env.REDIS_URL });\n *\n * // With existing client\n * import Redis from \"ioredis\";\n * const client = new Redis(process.env.REDIS_URL);\n * const state = createIoRedisState({ client });\n * ```\n */\nexport function createIoRedisState(\n options: IoRedisStateAdapterOptions | IoRedisStateClientOptions\n): IoRedisStateAdapter {\n return new IoRedisStateAdapter(options);\n}\n"],"mappings":";AACA,OAAO,WAAW;AAoCX,IAAM,sBAAN,MAAkD;AAAA,EACtC;AAAA,EACA;AAAA,EACA;AAAA,EACT,YAAY;AAAA,EACZ,iBAAuC;AAAA,EAC9B;AAAA,EAEjB,YAAY,SAAiE;AAC3E,QAAI,YAAY,SAAS;AACvB,WAAK,SAAS,QAAQ;AACtB,WAAK,aAAa;AAAA,IACpB,OAAO;AACL,WAAK,SAAS,IAAI,MAAM,QAAQ,GAAG;AACnC,WAAK,aAAa;AAAA,IACpB;AACA,SAAK,YAAY,QAAQ,aAAa;AACtC,SAAK,SAAS,QAAQ;AAGtB,SAAK,OAAO,GA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AS,mBACd,SACqB;AACrB,SAAO,IAAI,oBAAoB,OAAO;AACxC;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chat-adapter/state-ioredis",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.19.0",
|
|
4
4
|
"description": "ioredis state adapter for chat (production)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
],
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"ioredis": "^5.4.1",
|
|
20
|
-
"chat": "4.
|
|
20
|
+
"chat": "4.19.0"
|
|
21
21
|
},
|
|
22
22
|
"repository": {
|
|
23
23
|
"type": "git",
|