baileys-redis-auth 1.1.0 → 2.0.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 +33 -8
- package/lib/index.d.ts +3 -2
- package/lib/index.js +23 -34
- package/package.json +25 -11
package/README.md
CHANGED
|
@@ -4,18 +4,43 @@
|
|
|
4
4
|
|
|
5
5
|
This library provides flexible ways to store authentication data in Redis, using either simple key-value pairs or Redis Hashes for optimized storage.
|
|
6
6
|
|
|
7
|
+
## ⚠️ Version Compatibility
|
|
8
|
+
|
|
9
|
+
| baileys-redis-auth | Baileys Version | Package Name |
|
|
10
|
+
|-------------------|-----------------|--------------|
|
|
11
|
+
| v2.x | `baileys` v7.0.0-rc.1+ | `baileys` |
|
|
12
|
+
| v1.x | `@whiskeysockets/baileys` v6.x | `@whiskeysockets/baileys` |
|
|
13
|
+
|
|
14
|
+
**v2.0.0 Breaking Changes:**
|
|
15
|
+
- Now requires **Baileys v7+** (the new `baileys` package)
|
|
16
|
+
- Library is now **ESM-only** (no CommonJS support)
|
|
17
|
+
- Baileys is now a **peer dependency** - you must install it separately
|
|
18
|
+
|
|
7
19
|
## Prerequisites
|
|
8
20
|
|
|
9
21
|
Before using `baileys-redis-auth`, ensure you have the following installed and configured:
|
|
10
22
|
|
|
11
23
|
* **Node.js:** Version 18.x or higher is recommended.
|
|
12
24
|
* **Redis:** A running Redis server instance. You'll need its connection details (host, port, password if any).
|
|
13
|
-
* **Baileys
|
|
25
|
+
* **Baileys v7+:** This library is an auth handler for Baileys. Install the new `baileys` package (not `@whiskeysockets/baileys`).
|
|
14
26
|
|
|
15
27
|
## Installation
|
|
16
28
|
|
|
17
29
|
```bash
|
|
18
|
-
|
|
30
|
+
# Install both the auth library and Baileys v7
|
|
31
|
+
npm install baileys-redis-auth baileys
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Or with other package managers:
|
|
35
|
+
```bash
|
|
36
|
+
# Bun
|
|
37
|
+
bun add baileys-redis-auth baileys
|
|
38
|
+
|
|
39
|
+
# Yarn
|
|
40
|
+
yarn add baileys-redis-auth baileys
|
|
41
|
+
|
|
42
|
+
# pnpm
|
|
43
|
+
pnpm add baileys-redis-auth baileys
|
|
19
44
|
```
|
|
20
45
|
|
|
21
46
|
## Usage
|
|
@@ -31,7 +56,7 @@ This is the recommended method for storing Baileys authentication data in Redis.
|
|
|
31
56
|
|
|
32
57
|
```typescript
|
|
33
58
|
import {useRedisAuthStateWithHSet, deleteHSetKeys} from 'baileys-redis-auth';
|
|
34
|
-
import
|
|
59
|
+
import type {RedisOptions} from 'ioredis';
|
|
35
60
|
|
|
36
61
|
// Define your Redis connection options
|
|
37
62
|
const redisOptions: RedisOptions = {
|
|
@@ -86,7 +111,7 @@ To remove all authentication data associated with a specific session prefix used
|
|
|
86
111
|
|
|
87
112
|
```typescript
|
|
88
113
|
import { deleteHSetKeys } from 'baileys-redis-auth';
|
|
89
|
-
|
|
114
|
+
// Use the Redis instance from useRedisAuthStateWithHSet
|
|
90
115
|
|
|
91
116
|
// Assuming 'authRedisInstance' is the Redis instance from useRedisAuthStateWithHSet
|
|
92
117
|
// or a new instance configured with the same options.
|
|
@@ -114,7 +139,7 @@ This method stores each piece of authentication data as a separate key-value pai
|
|
|
114
139
|
|
|
115
140
|
```typescript
|
|
116
141
|
import {useRedisAuthState, deleteKeysWithPattern} from 'baileys-redis-auth';
|
|
117
|
-
import
|
|
142
|
+
import type {RedisOptions} from 'ioredis';
|
|
118
143
|
|
|
119
144
|
// Define your Redis connection options
|
|
120
145
|
const redisOptions: RedisOptions = {
|
|
@@ -167,7 +192,7 @@ To remove all authentication data associated with a specific session prefix used
|
|
|
167
192
|
|
|
168
193
|
```typescript
|
|
169
194
|
import { deleteKeysWithPattern } from 'baileys-redis-auth';
|
|
170
|
-
|
|
195
|
+
// Use the Redis instance from useRedisAuthState
|
|
171
196
|
|
|
172
197
|
// Assuming 'authRedisInstance' is the Redis instance from useRedisAuthState
|
|
173
198
|
// or a new instance configured with the same options.
|
|
@@ -193,7 +218,7 @@ import Redis, { RedisOptions } from 'ioredis'; // Or use the instance from useRe
|
|
|
193
218
|
|
|
194
219
|
```typescript
|
|
195
220
|
import {deleteKeysWithPattern, useRedisAuthState} from 'baileys-redis-auth'
|
|
196
|
-
import makeWASocket, {DisconnectReason} from '
|
|
221
|
+
import makeWASocket, {DisconnectReason} from 'baileys'
|
|
197
222
|
import type {Boom} from '@hapi/boom'
|
|
198
223
|
|
|
199
224
|
const sessionPrefix = 'my-session'
|
|
@@ -261,7 +286,7 @@ This project includes an example script to demonstrate the usage of `baileys-red
|
|
|
261
286
|
# or
|
|
262
287
|
# bun run example
|
|
263
288
|
```
|
|
264
|
-
This command executes `
|
|
289
|
+
This command executes `tsx example/example.ts`.
|
|
265
290
|
The example will guide you through connecting to WhatsApp using Baileys with Redis for authentication storage. You'll see a QR code in your terminal to scan with WhatsApp.
|
|
266
291
|
|
|
267
292
|
**Interactive Commands:**
|
package/lib/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { type AuthenticationState } from '
|
|
2
|
-
import {
|
|
1
|
+
import { type AuthenticationState } from 'baileys';
|
|
2
|
+
import { Redis, type RedisOptions } from 'ioredis';
|
|
3
|
+
type RedisClient = Redis;
|
|
3
4
|
interface IDeleteHSetKeyOptions {
|
|
4
5
|
redis: RedisClient;
|
|
5
6
|
key: string;
|
package/lib/index.js
CHANGED
|
@@ -1,11 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.deleteKeysWithPattern = exports.useRedisAuthState = exports.deleteHSetKeys = exports.useRedisAuthStateWithHSet = void 0;
|
|
7
|
-
const baileys_1 = require("@whiskeysockets/baileys");
|
|
8
|
-
const ioredis_1 = __importDefault(require("ioredis"));
|
|
1
|
+
import { BufferJSON, initAuthCreds, proto, } from 'baileys';
|
|
2
|
+
import { Redis } from 'ioredis';
|
|
9
3
|
/**
|
|
10
4
|
* Sanitizes a string to make it safe for use as a Redis key
|
|
11
5
|
* Replaces "/" with "__" and ":" with "-"
|
|
@@ -17,24 +11,24 @@ const createKey = (key, prefix) => `${prefix}:${key}`;
|
|
|
17
11
|
* Stores all authentication data in a single Redis Hash per prefix
|
|
18
12
|
* More efficient than key-value approach for Redis memory and operations
|
|
19
13
|
*/
|
|
20
|
-
const useRedisAuthStateWithHSet = async (redisOptions, prefix = 'session', logger) => {
|
|
21
|
-
const redis = new
|
|
14
|
+
export const useRedisAuthStateWithHSet = async (redisOptions, prefix = 'session', logger) => {
|
|
15
|
+
const redis = new Redis(redisOptions);
|
|
22
16
|
redis.on('connect', async () => {
|
|
23
17
|
const redisClientName = `baileys-auth-${prefix}`;
|
|
24
18
|
await redis.client('SETNAME', redisClientName);
|
|
25
|
-
logger
|
|
19
|
+
logger?.(`Redis client name set to ${redisClientName}`);
|
|
26
20
|
});
|
|
27
21
|
const writeData = async (key, data) => {
|
|
28
|
-
await redis.hset(createKey('auth', prefix), key, JSON.stringify(data,
|
|
22
|
+
await redis.hset(createKey('auth', prefix), key, JSON.stringify(data, BufferJSON.replacer));
|
|
29
23
|
};
|
|
30
24
|
const readData = async (key) => {
|
|
31
25
|
const data = await redis.hget(createKey('auth', prefix), key);
|
|
32
|
-
return data ? JSON.parse(data,
|
|
26
|
+
return data ? JSON.parse(data, BufferJSON.reviver) : null;
|
|
33
27
|
};
|
|
34
28
|
const removeData = async (key) => {
|
|
35
29
|
await redis.hdel(createKey('auth', prefix), key);
|
|
36
30
|
};
|
|
37
|
-
const creds = (await readData('creds')) ||
|
|
31
|
+
const creds = (await readData('creds')) || initAuthCreds();
|
|
38
32
|
return {
|
|
39
33
|
state: {
|
|
40
34
|
creds,
|
|
@@ -45,7 +39,7 @@ const useRedisAuthStateWithHSet = async (redisOptions, prefix = 'session', logge
|
|
|
45
39
|
const key = `${type}-${fixFileName(id)}`;
|
|
46
40
|
const value = await readData(key);
|
|
47
41
|
if (value) {
|
|
48
|
-
data[id] = (type === 'app-state-sync-key' ?
|
|
42
|
+
data[id] = (type === 'app-state-sync-key' ? proto.Message.AppStateSyncKeyData.fromObject(value) : value);
|
|
49
43
|
}
|
|
50
44
|
}));
|
|
51
45
|
return data;
|
|
@@ -74,46 +68,43 @@ const useRedisAuthStateWithHSet = async (redisOptions, prefix = 'session', logge
|
|
|
74
68
|
redis,
|
|
75
69
|
};
|
|
76
70
|
};
|
|
77
|
-
exports.useRedisAuthStateWithHSet = useRedisAuthStateWithHSet;
|
|
78
71
|
/**
|
|
79
72
|
* Deletes all authentication data for a specific prefix using Hash (HSET)
|
|
80
73
|
*/
|
|
81
|
-
const deleteHSetKeys = async ({ redis, key, logger }) => {
|
|
74
|
+
export const deleteHSetKeys = async ({ redis, key, logger }) => {
|
|
82
75
|
try {
|
|
83
|
-
logger
|
|
76
|
+
logger?.('Removing auth state keys for prefix:', key);
|
|
84
77
|
await redis.del(createKey('auth', key));
|
|
85
78
|
}
|
|
86
79
|
catch (err) {
|
|
87
80
|
const error = err;
|
|
88
|
-
logger
|
|
81
|
+
logger?.('Error deleting keys:', error.message);
|
|
89
82
|
throw error;
|
|
90
83
|
}
|
|
91
84
|
};
|
|
92
|
-
exports.deleteHSetKeys = deleteHSetKeys;
|
|
93
85
|
/**
|
|
94
86
|
* Redis-based authentication state storage using key-value pairs
|
|
95
87
|
* Stores each piece of authentication data as a separate Redis key
|
|
96
88
|
* Less efficient than Hash approach but more compatible with existing systems
|
|
97
89
|
*/
|
|
98
|
-
const useRedisAuthState = async (redisOptions, prefix = 'session', logger) => {
|
|
99
|
-
|
|
100
|
-
const redis = new ioredis_1.default(redisOptions);
|
|
90
|
+
export const useRedisAuthState = async (redisOptions, prefix = 'session', logger) => {
|
|
91
|
+
const redis = new Redis(redisOptions);
|
|
101
92
|
redis.on('connect', async () => {
|
|
102
93
|
const redisClientName = `baileys-auth-${prefix}`;
|
|
103
94
|
await redis.client('SETNAME', redisClientName);
|
|
104
|
-
logger
|
|
95
|
+
logger?.(`Redis client name set to ${redisClientName}`);
|
|
105
96
|
});
|
|
106
97
|
const writeData = async (key, data) => {
|
|
107
|
-
await redis.set(createKey(key, prefix), JSON.stringify(data,
|
|
98
|
+
await redis.set(createKey(key, prefix), JSON.stringify(data, BufferJSON.replacer));
|
|
108
99
|
};
|
|
109
100
|
const readData = async (key) => {
|
|
110
101
|
const data = await redis.get(createKey(key, prefix));
|
|
111
|
-
return data ? JSON.parse(data,
|
|
102
|
+
return data ? JSON.parse(data, BufferJSON.reviver) : null;
|
|
112
103
|
};
|
|
113
104
|
const removeData = async (key) => {
|
|
114
105
|
await redis.del(createKey(key, prefix));
|
|
115
106
|
};
|
|
116
|
-
const creds = (
|
|
107
|
+
const creds = (await readData('creds')) ?? initAuthCreds();
|
|
117
108
|
return {
|
|
118
109
|
state: {
|
|
119
110
|
creds,
|
|
@@ -124,7 +115,7 @@ const useRedisAuthState = async (redisOptions, prefix = 'session', logger) => {
|
|
|
124
115
|
const key = `${type}-${fixFileName(id)}`;
|
|
125
116
|
const value = await readData(key);
|
|
126
117
|
if (value) {
|
|
127
|
-
data[id] = (type === 'app-state-sync-key' ?
|
|
118
|
+
data[id] = (type === 'app-state-sync-key' ? proto.Message.AppStateSyncKeyData.fromObject(value) : value);
|
|
128
119
|
}
|
|
129
120
|
}));
|
|
130
121
|
return data;
|
|
@@ -153,28 +144,26 @@ const useRedisAuthState = async (redisOptions, prefix = 'session', logger) => {
|
|
|
153
144
|
redis,
|
|
154
145
|
};
|
|
155
146
|
};
|
|
156
|
-
exports.useRedisAuthState = useRedisAuthState;
|
|
157
147
|
/**
|
|
158
148
|
* Deletes all authentication keys matching a pattern using key-value approach
|
|
159
149
|
* Uses SCAN to safely iterate through keys without blocking Redis
|
|
160
150
|
*/
|
|
161
|
-
const deleteKeysWithPattern = async ({ redis, pattern, logger }) => {
|
|
151
|
+
export const deleteKeysWithPattern = async ({ redis, pattern, logger }) => {
|
|
162
152
|
try {
|
|
163
|
-
logger
|
|
153
|
+
logger?.('Removing auth state keys matching pattern:', pattern);
|
|
164
154
|
let cursor = 0;
|
|
165
155
|
do {
|
|
166
156
|
const [nextCursor, keys] = await redis.scan(cursor, 'MATCH', pattern, 'COUNT', 100);
|
|
167
157
|
cursor = Number.parseInt(nextCursor, 10);
|
|
168
158
|
if (keys.length > 0) {
|
|
169
159
|
await redis.unlink(...keys);
|
|
170
|
-
logger
|
|
160
|
+
logger?.(`Deleted keys: ${keys.join(', ')}`);
|
|
171
161
|
}
|
|
172
162
|
} while (cursor !== 0);
|
|
173
163
|
}
|
|
174
164
|
catch (err) {
|
|
175
165
|
const error = err;
|
|
176
|
-
logger
|
|
166
|
+
logger?.('Error deleting keys:', error.message);
|
|
177
167
|
throw error;
|
|
178
168
|
}
|
|
179
169
|
};
|
|
180
|
-
exports.deleteKeysWithPattern = deleteKeysWithPattern;
|
package/package.json
CHANGED
|
@@ -1,20 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "baileys-redis-auth",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Redis Auth for Baileys",
|
|
5
5
|
"author": "heriyanto binduni <hbinduni@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
7
8
|
"main": "lib/index.js",
|
|
9
|
+
"module": "lib/index.js",
|
|
8
10
|
"types": "lib/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": "./lib/index.js",
|
|
14
|
+
"types": "./lib/index.d.ts"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
9
17
|
"homepage": "https://github.com/hbinduni/baileys-redis-auth",
|
|
10
18
|
"repository": {
|
|
11
|
-
"url": "https://github.com/hbinduni/baileys-redis-auth.git"
|
|
19
|
+
"url": "git+https://github.com/hbinduni/baileys-redis-auth.git"
|
|
12
20
|
},
|
|
13
21
|
"scripts": {
|
|
14
22
|
"clean": "rm -rf lib",
|
|
15
23
|
"build": "bun run clean && tsc -p tsconfig.json",
|
|
16
24
|
"prebuild": "bun run check:all",
|
|
17
|
-
"example": "
|
|
25
|
+
"example": "tsx example/example.ts",
|
|
18
26
|
"example:no-all": "bun run example -- --no-store --no-reply",
|
|
19
27
|
"typecheck": "bun x tsc --noEmit -p tsconfig.json",
|
|
20
28
|
"format": "biome format --write .",
|
|
@@ -36,21 +44,27 @@
|
|
|
36
44
|
"auth"
|
|
37
45
|
],
|
|
38
46
|
"devDependencies": {
|
|
39
|
-
"@adiwajshing/keyed-db": "^0.2.4",
|
|
40
47
|
"@biomejs/biome": "^1.9.4",
|
|
41
48
|
"@hapi/boom": "^10.0.1",
|
|
42
|
-
"@types/node": "^24.
|
|
49
|
+
"@types/node": "^24.10.4",
|
|
50
|
+
"baileys": "^7.0.0-rc.9",
|
|
43
51
|
"dotenv": "^17.2.3",
|
|
44
52
|
"node-cache": "^5.1.2",
|
|
45
|
-
"pino": "^9.
|
|
46
|
-
"pino-pretty": "^13.1.
|
|
53
|
+
"pino": "^9.14.0",
|
|
54
|
+
"pino-pretty": "^13.1.3",
|
|
47
55
|
"qrcode-terminal": "^0.12.0",
|
|
48
|
-
"
|
|
49
|
-
"tsconfig-paths": "^4.2.0",
|
|
56
|
+
"tsx": "^4.19.0",
|
|
50
57
|
"typescript": "^5.9.3"
|
|
51
58
|
},
|
|
52
59
|
"dependencies": {
|
|
53
|
-
"
|
|
54
|
-
|
|
60
|
+
"ioredis": "^5.8.2"
|
|
61
|
+
},
|
|
62
|
+
"peerDependencies": {
|
|
63
|
+
"baileys": ">=7.0.0-rc.1"
|
|
64
|
+
},
|
|
65
|
+
"peerDependenciesMeta": {
|
|
66
|
+
"baileys": {
|
|
67
|
+
"optional": false
|
|
68
|
+
}
|
|
55
69
|
}
|
|
56
70
|
}
|