@builderbot/provider-baileys 1.4.1 → 1.4.2-alpha.10
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/bailey.d.ts +19 -2
- package/dist/bailey.d.ts.map +1 -1
- package/dist/index.cjs +1016 -10
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +1010 -13
- package/dist/lidCache.d.ts +569 -0
- package/dist/lidCache.d.ts.map +1 -0
- package/dist/type.d.ts +13 -0
- package/dist/type.d.ts.map +1 -1
- package/package.json +79 -79
|
@@ -0,0 +1,569 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview LID (Local Identifier) Cache for WhatsApp Baileys Provider
|
|
3
|
+
*
|
|
4
|
+
* This module provides a caching layer for mapping WhatsApp Local Identifiers (LIDs)
|
|
5
|
+
* to Phone Numbers (PNs). LIDs are privacy-preserving identifiers used by WhatsApp
|
|
6
|
+
* that don't reveal the user's actual phone number.
|
|
7
|
+
*
|
|
8
|
+
* ## Architecture
|
|
9
|
+
*
|
|
10
|
+
* The cache uses a hybrid memory+file approach:
|
|
11
|
+
* - **Hot path**: In-memory lookups via NodeCache (O(1), ~1μs)
|
|
12
|
+
* - **Persistence**: JSON file for cross-restart durability
|
|
13
|
+
* - **Strategy**: Write-through to memory, async flush to disk every 30s
|
|
14
|
+
*
|
|
15
|
+
* ## Key Features
|
|
16
|
+
*
|
|
17
|
+
* - **Zero-config**: Works out of the box with sensible defaults
|
|
18
|
+
* - **Device suffix normalization**: `123:45@lid` and `123:99@lid` resolve to same entry
|
|
19
|
+
* - **Phone number normalization**: Accepts `+123 456-7890`, `1234567890@c.us`, etc.
|
|
20
|
+
* - **Security**: File permissions 0o600, PII masking in logs
|
|
21
|
+
* - **Resilience**: Corrupted files auto-rebuild, flush failures logged
|
|
22
|
+
*
|
|
23
|
+
* ## Usage
|
|
24
|
+
*
|
|
25
|
+
* ```typescript
|
|
26
|
+
* // Default: Hybrid (memory + file)
|
|
27
|
+
* const cache = new HybridLidCache('my-bot', 86400 * 7) // 7 day TTL
|
|
28
|
+
* await cache.ready()
|
|
29
|
+
*
|
|
30
|
+
* await cache.set('123456789:45@lid', '+34 691 015 468')
|
|
31
|
+
* const pn = await cache.get('123456789:99@lid') // Returns '34691015468@s.whatsapp.net'
|
|
32
|
+
*
|
|
33
|
+
* await cache.close() // Persists to disk
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* @module lidCache
|
|
37
|
+
* @author BuilderBot Team
|
|
38
|
+
* @since 1.4.2
|
|
39
|
+
*/
|
|
40
|
+
/**
|
|
41
|
+
* Branded type for WhatsApp Local Identifier (LID).
|
|
42
|
+
* Ensures compile-time distinction from regular strings.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* const lid = '123456789@lid' as LidJid
|
|
47
|
+
* const pn = resolveLidToPn(cache, fallback, logger, lid) // OK
|
|
48
|
+
* resolveLidToPn(cache, fallback, logger, '123456789@lid') // Error: not branded
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export type LidJid = string & {
|
|
52
|
+
readonly __brand: 'LidJid';
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Branded type for Phone Number JID.
|
|
56
|
+
* Format: `1234567890@s.whatsapp.net`
|
|
57
|
+
*/
|
|
58
|
+
export type PnJid = string & {
|
|
59
|
+
readonly __brand: 'PnJid';
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Helper to brand a string as LidJid (runtime check).
|
|
63
|
+
* Returns null if the string is not a valid LID.
|
|
64
|
+
*/
|
|
65
|
+
export declare function asLidJid(value: string): LidJid | null;
|
|
66
|
+
/**
|
|
67
|
+
* Helper to brand a string as PnJid (runtime check).
|
|
68
|
+
* Returns null if the string is not a valid phone number JID.
|
|
69
|
+
*/
|
|
70
|
+
export declare function asPnJid(value: string): PnJid | null;
|
|
71
|
+
/**
|
|
72
|
+
* Interface for LID cache implementations.
|
|
73
|
+
*
|
|
74
|
+
* Defines the contract for any cache that maps WhatsApp LIDs to phone numbers.
|
|
75
|
+
* Both {@link HybridLidCache} and {@link MemoryLidCache} implement this interface.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```typescript
|
|
79
|
+
* // Custom implementation (e.g., Redis)
|
|
80
|
+
* class RedisLidCache implements LidCache {
|
|
81
|
+
* async get(lid: LidJid): Promise<PnJid | null> {
|
|
82
|
+
* return redis.get(`lid:${lid}`) as Promise<PnJid | null>
|
|
83
|
+
* }
|
|
84
|
+
* // ... other methods
|
|
85
|
+
* }
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
export interface LidCache {
|
|
89
|
+
/**
|
|
90
|
+
* Retrieves the phone number for a given LID.
|
|
91
|
+
*
|
|
92
|
+
* @param lid - WhatsApp Local Identifier (e.g., '123456789@lid' or '123456789:45@lid')
|
|
93
|
+
* @returns Phone number in format '1234567890@s.whatsapp.net', or null if not found/invalid
|
|
94
|
+
*/
|
|
95
|
+
get(lid: LidJid | string): Promise<PnJid | string | null>;
|
|
96
|
+
/**
|
|
97
|
+
* Stores a LID → phone number mapping.
|
|
98
|
+
*
|
|
99
|
+
* @param lid - WhatsApp Local Identifier
|
|
100
|
+
* @param pn - Phone number (any format: +123, 123, 123@c.us, 123@s.whatsapp.net)
|
|
101
|
+
* @returns Promise that resolves when the value is stored in memory
|
|
102
|
+
*
|
|
103
|
+
* @remarks
|
|
104
|
+
* - PN is normalized to `123@s.whatsapp.net` format before storage
|
|
105
|
+
* - LID device suffix is normalized (e.g., `123:45@lid` → `123@lid`)
|
|
106
|
+
* - Invalid inputs are silently rejected (no throw)
|
|
107
|
+
*/
|
|
108
|
+
set(lid: LidJid | string, pn: PnJid | string): Promise<void>;
|
|
109
|
+
/**
|
|
110
|
+
* Checks if a LID exists in the cache.
|
|
111
|
+
*
|
|
112
|
+
* @param lid - WhatsApp Local Identifier
|
|
113
|
+
* @returns true if the LID exists and hasn't expired
|
|
114
|
+
*/
|
|
115
|
+
has(lid: LidJid | string): Promise<boolean>;
|
|
116
|
+
/**
|
|
117
|
+
* Clears all entries from the cache.
|
|
118
|
+
*
|
|
119
|
+
* @remarks Also triggers immediate flush to disk (if HybridLidCache)
|
|
120
|
+
*/
|
|
121
|
+
clear(): Promise<void>;
|
|
122
|
+
/**
|
|
123
|
+
* Closes the cache, releasing resources and triggering final persistence.
|
|
124
|
+
*
|
|
125
|
+
* @remarks After close(), all operations return null/void. Call only once.
|
|
126
|
+
*/
|
|
127
|
+
close?(): Promise<void>;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Normalizes a LID by removing the device suffix.
|
|
131
|
+
*
|
|
132
|
+
* WhatsApp sends LIDs with device identifiers (e.g., `:45`, `:99`) that vary
|
|
133
|
+
* based on the user's device. The same contact will have different suffixes
|
|
134
|
+
* on phone vs web vs tablet. This function strips the suffix for consistent
|
|
135
|
+
* caching.
|
|
136
|
+
*
|
|
137
|
+
* @param lid - LID to normalize (e.g., '123456789:45@lid')
|
|
138
|
+
* @returns Normalized LID (e.g., '123456789@lid'), or original if invalid
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```typescript
|
|
142
|
+
* normalizeLid('123456789:45@lid') // '123456789@lid'
|
|
143
|
+
* normalizeLid('123456789:99@lid') // '123456789@lid'
|
|
144
|
+
* normalizeLid('123456789@lid') // '123456789@lid' (no change)
|
|
145
|
+
* normalizeLid('123456789@c.us') // '123456789@c.us' (no change, not a LID)
|
|
146
|
+
* normalizeLid('invalid') // 'invalid' (no change, invalid)
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
export declare function normalizeLid(lid: string): string;
|
|
150
|
+
/**
|
|
151
|
+
* Hybrid LID cache implementation with memory hot-path and file persistence.
|
|
152
|
+
*
|
|
153
|
+
* This is the recommended production implementation, providing:
|
|
154
|
+
* - **Speed**: O(1) in-memory lookups via NodeCache (~1μs)
|
|
155
|
+
* - **Durability**: Automatic persistence to JSON file every 30s
|
|
156
|
+
* - **Resilience**: Survives process restarts, handles corrupted files gracefully
|
|
157
|
+
* - **Security**: File permissions 0o600 (owner read/write only)
|
|
158
|
+
*
|
|
159
|
+
* ## File Location
|
|
160
|
+
*
|
|
161
|
+
* Files are stored at: `{cwd}/{sessionName}_sessions/lid-cache.json`
|
|
162
|
+
*
|
|
163
|
+
* The session name is sanitized to prevent path traversal attacks.
|
|
164
|
+
*
|
|
165
|
+
* ## Configuration
|
|
166
|
+
*
|
|
167
|
+
* | Option | Default | Description |
|
|
168
|
+
* |--------|---------|-------------|
|
|
169
|
+
* | `sessionName` | required | Unique name for this bot instance |
|
|
170
|
+
* | `ttlSeconds` | 604800 (7 days) | Time-to-live for cache entries |
|
|
171
|
+
* | `basePath` | `process.cwd()` | Directory for session files |
|
|
172
|
+
* | `logger` | `console` | Logger for operational events |
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* ```typescript
|
|
176
|
+
* // Basic usage
|
|
177
|
+
* const cache = new HybridLidCache('my-bot')
|
|
178
|
+
* await cache.ready()
|
|
179
|
+
*
|
|
180
|
+
* // Custom TTL and logger
|
|
181
|
+
* const cache = new HybridLidCache('my-bot', 86400 * 30, undefined, winstonLogger)
|
|
182
|
+
*
|
|
183
|
+
* // Custom base path
|
|
184
|
+
* const cache = new HybridLidCache('my-bot', 86400, '/var/lib/bot')
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
export declare class HybridLidCache implements LidCache {
|
|
188
|
+
/** In-memory cache via NodeCache */
|
|
189
|
+
private memory;
|
|
190
|
+
/** Absolute path to the JSON persistence file */
|
|
191
|
+
private filePath;
|
|
192
|
+
/** True if memory has unwritten changes */
|
|
193
|
+
private dirty;
|
|
194
|
+
/** True if flushToDisk() is currently running (prevents concurrent flushes) */
|
|
195
|
+
private flushing;
|
|
196
|
+
/** Interval handle for periodic auto-flush */
|
|
197
|
+
private flushInterval?;
|
|
198
|
+
/** TTL in seconds (for expiry calculations on load) */
|
|
199
|
+
private readonly ttlSeconds;
|
|
200
|
+
/** Logger for operational events (defaults to console) */
|
|
201
|
+
private readonly logger;
|
|
202
|
+
/** Consecutive flush failure count (disables persistence after threshold) */
|
|
203
|
+
private consecutiveFlushFailures;
|
|
204
|
+
/** Promise tracking initial file load (prevents race conditions) */
|
|
205
|
+
private loadPromise;
|
|
206
|
+
/** True after close() has been called */
|
|
207
|
+
private isClosed;
|
|
208
|
+
/**
|
|
209
|
+
* Creates a new HybridLidCache instance.
|
|
210
|
+
*
|
|
211
|
+
* @param sessionName - Unique identifier for this cache instance (used in filename)
|
|
212
|
+
* @param ttlSeconds - Time-to-live for cache entries (default: 7 days)
|
|
213
|
+
* @param basePath - Base directory for session files (default: process.cwd())
|
|
214
|
+
* @param logger - Logger instance for operational events (default: console)
|
|
215
|
+
*
|
|
216
|
+
* @throws Error if sessionName is empty or not a string
|
|
217
|
+
* @throws Error if ttlSeconds is less than 60
|
|
218
|
+
*
|
|
219
|
+
* @remarks
|
|
220
|
+
* The constructor starts async file loading and sets up periodic auto-flush.
|
|
221
|
+
* Use {@link ready()} to wait for initial load completion before operations
|
|
222
|
+
* that require data from previous runs.
|
|
223
|
+
*/
|
|
224
|
+
constructor(sessionName: string, ttlSeconds?: number, basePath?: string, logger?: Console);
|
|
225
|
+
/**
|
|
226
|
+
* Waits for the initial file load to complete.
|
|
227
|
+
*
|
|
228
|
+
* Use this method before operations that depend on data from previous runs:
|
|
229
|
+
* - Before checking if a LID exists from a previous session
|
|
230
|
+
* - In tests to ensure deterministic state
|
|
231
|
+
* - During cache warming procedures
|
|
232
|
+
*
|
|
233
|
+
* @returns Promise that resolves when initial load completes
|
|
234
|
+
*
|
|
235
|
+
* @example
|
|
236
|
+
* ```typescript
|
|
237
|
+
* const cache = new HybridLidCache('my-bot')
|
|
238
|
+
* await cache.ready() // Wait for any existing data to load
|
|
239
|
+
* const pn = await cache.get('existing@lid') // Now safe to access
|
|
240
|
+
* ```
|
|
241
|
+
*/
|
|
242
|
+
ready(): Promise<void>;
|
|
243
|
+
/**
|
|
244
|
+
* Retrieves the phone number for a given LID.
|
|
245
|
+
*
|
|
246
|
+
* @param lid - WhatsApp Local Identifier (e.g., '123456789:45@lid')
|
|
247
|
+
* @returns Phone number in format '1234567890@s.whatsapp.net', or null if:
|
|
248
|
+
* - Cache is closed
|
|
249
|
+
* - LID is invalid (doesn't match `*@lid` pattern)
|
|
250
|
+
* - LID not found in cache
|
|
251
|
+
* - Entry has expired
|
|
252
|
+
*/
|
|
253
|
+
get(lid: string): Promise<string | null>;
|
|
254
|
+
/**
|
|
255
|
+
* Stores a LID → phone number mapping.
|
|
256
|
+
*
|
|
257
|
+
* Both the LID and phone number are normalized before storage:
|
|
258
|
+
* - LID: Device suffix removed (`123:45@lid` → `123@lid`)
|
|
259
|
+
* - PN: Formatted to `123@s.whatsapp.net`
|
|
260
|
+
*
|
|
261
|
+
* @param lid - WhatsApp Local Identifier
|
|
262
|
+
* @param pn - Phone number in any accepted format
|
|
263
|
+
* @returns Promise that resolves immediately (memory write is synchronous)
|
|
264
|
+
*
|
|
265
|
+
* @remarks
|
|
266
|
+
* - Invalid inputs are silently rejected (no throw)
|
|
267
|
+
* - The `dirty` flag is set, triggering async flush to disk within 30s
|
|
268
|
+
* - If the cache is closed, this is a no-op
|
|
269
|
+
*/
|
|
270
|
+
set(lid: string, pn: string): Promise<void>;
|
|
271
|
+
/**
|
|
272
|
+
* Checks if a LID exists in the cache.
|
|
273
|
+
*
|
|
274
|
+
* @param lid - WhatsApp Local Identifier
|
|
275
|
+
* @returns true if:
|
|
276
|
+
* - LID is valid
|
|
277
|
+
* - Cache is open
|
|
278
|
+
* - Entry exists and hasn't expired
|
|
279
|
+
*
|
|
280
|
+
* @example
|
|
281
|
+
* ```typescript
|
|
282
|
+
* if (await cache.has('123@lid')) {
|
|
283
|
+
* const pn = await cache.get('123@lid')
|
|
284
|
+
* // ...
|
|
285
|
+
* }
|
|
286
|
+
* ```
|
|
287
|
+
*/
|
|
288
|
+
has(lid: string): Promise<boolean>;
|
|
289
|
+
/**
|
|
290
|
+
* Clears all entries from the cache.
|
|
291
|
+
*
|
|
292
|
+
* @remarks
|
|
293
|
+
* - Immediately clears memory
|
|
294
|
+
* - Triggers synchronous flush to disk (empty file)
|
|
295
|
+
* - Logs the operation at info level
|
|
296
|
+
*/
|
|
297
|
+
clear(): Promise<void>;
|
|
298
|
+
/**
|
|
299
|
+
* Closes the cache, releasing resources and triggering final persistence.
|
|
300
|
+
*
|
|
301
|
+
* This method:
|
|
302
|
+
* 1. Stops the auto-flush interval
|
|
303
|
+
* 2. Waits for any in-progress file load
|
|
304
|
+
* 3. Performs a final flush to disk
|
|
305
|
+
* 4. Closes the NodeCache instance
|
|
306
|
+
* 5. Marks the cache as closed
|
|
307
|
+
*
|
|
308
|
+
* @returns Promise that resolves when cleanup is complete
|
|
309
|
+
*
|
|
310
|
+
* @remarks
|
|
311
|
+
* - After close(), all operations return null/void
|
|
312
|
+
* - Safe to call multiple times (subsequent calls are no-ops)
|
|
313
|
+
* - Flush errors are logged but don't throw
|
|
314
|
+
*/
|
|
315
|
+
close(): Promise<void>;
|
|
316
|
+
/**
|
|
317
|
+
* Forces compaction of the cache file by rewriting it with only valid entries.
|
|
318
|
+
*
|
|
319
|
+
* This removes any expired entries that might still be in the file
|
|
320
|
+
* (since NodeCache auto-expiry only removes from memory, not disk).
|
|
321
|
+
*
|
|
322
|
+
* @returns Promise that resolves when compaction completes
|
|
323
|
+
*
|
|
324
|
+
* @remarks
|
|
325
|
+
* - If the cache is empty, the file is deleted instead
|
|
326
|
+
* - Automatically called when file exceeds 10MB or 10k entries
|
|
327
|
+
*/
|
|
328
|
+
compact(): Promise<void>;
|
|
329
|
+
/**
|
|
330
|
+
* Persists the current cache state to disk.
|
|
331
|
+
*
|
|
332
|
+
* @internal
|
|
333
|
+
* @remarks
|
|
334
|
+
* - Concurrent calls are deduplicated (only one flush runs at a time)
|
|
335
|
+
* - No-op if nothing has changed since last flush (`dirty` flag check)
|
|
336
|
+
* - File is written with 0o600 permissions (owner read/write only)
|
|
337
|
+
* - After 10 consecutive failures, persistence is disabled
|
|
338
|
+
*/
|
|
339
|
+
flushToDisk(): Promise<void>;
|
|
340
|
+
/**
|
|
341
|
+
* Checks file size and triggers compaction if exceeds threshold.
|
|
342
|
+
*
|
|
343
|
+
* @internal
|
|
344
|
+
*/
|
|
345
|
+
private checkAndCompactIfNeeded;
|
|
346
|
+
/**
|
|
347
|
+
* Loads cache data from disk on startup.
|
|
348
|
+
*
|
|
349
|
+
* @internal
|
|
350
|
+
* @remarks
|
|
351
|
+
* - Silently handles missing file (first run)
|
|
352
|
+
* - Automatically removes and recovers from corrupted files
|
|
353
|
+
* - Validates TTL on each entry (entries older than TTL are skipped)
|
|
354
|
+
*/
|
|
355
|
+
private loadFromDisk;
|
|
356
|
+
/**
|
|
357
|
+
* Returns cache statistics for monitoring.
|
|
358
|
+
*
|
|
359
|
+
* @returns Object with:
|
|
360
|
+
* - `keys`: Number of entries currently in memory
|
|
361
|
+
* - `hits`: Cache hit count (from NodeCache stats)
|
|
362
|
+
* - `misses`: Cache miss count (from NodeCache stats)
|
|
363
|
+
*
|
|
364
|
+
* @example
|
|
365
|
+
* ```typescript
|
|
366
|
+
* const stats = cache.getStats()
|
|
367
|
+
* console.log(`Cache: ${stats.keys} entries, ${stats.hits} hits, ${stats.misses} misses`)
|
|
368
|
+
* // → Cache: 1523 entries, 4500 hits, 123 misses
|
|
369
|
+
* ```
|
|
370
|
+
*/
|
|
371
|
+
getStats(): {
|
|
372
|
+
keys: number;
|
|
373
|
+
hits: number;
|
|
374
|
+
misses: number;
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Memory-only LID cache implementation for testing.
|
|
379
|
+
*
|
|
380
|
+
* This implementation provides the same {@link LidCache} interface but without
|
|
381
|
+
* file persistence. Useful for:
|
|
382
|
+
* - Unit tests (no file I/O, no cleanup needed)
|
|
383
|
+
* - Ephemeral caches that don't need durability
|
|
384
|
+
* - Reducing disk wear in high-frequency test scenarios
|
|
385
|
+
*
|
|
386
|
+
* ## Differences from HybridLidCache
|
|
387
|
+
*
|
|
388
|
+
* | Feature | MemoryLidCache | HybridLidCache |
|
|
389
|
+
* |---------|---------------|----------------|
|
|
390
|
+
* | Persistence | ❌ None | ✅ JSON file |
|
|
391
|
+
* | `ready()` | Optional | Recommended |
|
|
392
|
+
* | `close()` | No-op | Flushes to disk |
|
|
393
|
+
* | `compact()` | No-op | Rewrites file |
|
|
394
|
+
* | Cross-restart | Data lost | Data preserved |
|
|
395
|
+
*
|
|
396
|
+
* @example
|
|
397
|
+
* ```typescript
|
|
398
|
+
* // Testing scenario
|
|
399
|
+
* const cache = new MemoryLidCache(3600) // 1 hour TTL
|
|
400
|
+
* await cache.set('123@lid', '456@s.whatsapp.net')
|
|
401
|
+
* expect(await cache.get('123@lid')).toBe('456@s.whatsapp.net')
|
|
402
|
+
* // No cleanup needed - data is ephemeral
|
|
403
|
+
* ```
|
|
404
|
+
*/
|
|
405
|
+
export declare class MemoryLidCache implements LidCache {
|
|
406
|
+
/** In-memory cache via NodeCache (no persistence) */
|
|
407
|
+
private memory;
|
|
408
|
+
/**
|
|
409
|
+
* Creates a new MemoryLidCache instance.
|
|
410
|
+
*
|
|
411
|
+
* @param ttlSeconds - Time-to-live for cache entries (default: 7 days)
|
|
412
|
+
*/
|
|
413
|
+
constructor(ttlSeconds?: number);
|
|
414
|
+
/**
|
|
415
|
+
* Retrieves the phone number for a given LID.
|
|
416
|
+
*
|
|
417
|
+
* @param lid - WhatsApp Local Identifier
|
|
418
|
+
* @returns Phone number or null if not found/invalid
|
|
419
|
+
*/
|
|
420
|
+
get(lid: string): Promise<string | null>;
|
|
421
|
+
/**
|
|
422
|
+
* Stores a LID → phone number mapping.
|
|
423
|
+
*
|
|
424
|
+
* @param lid - WhatsApp Local Identifier
|
|
425
|
+
* @param pn - Phone number in any format
|
|
426
|
+
*/
|
|
427
|
+
set(lid: string, pn: string): Promise<void>;
|
|
428
|
+
/**
|
|
429
|
+
* Checks if a LID exists in the cache.
|
|
430
|
+
*
|
|
431
|
+
* @param lid - WhatsApp Local Identifier
|
|
432
|
+
* @returns true if entry exists and hasn't expired
|
|
433
|
+
*/
|
|
434
|
+
has(lid: string): Promise<boolean>;
|
|
435
|
+
/**
|
|
436
|
+
* Clears all entries from the cache.
|
|
437
|
+
*/
|
|
438
|
+
clear(): Promise<void>;
|
|
439
|
+
/**
|
|
440
|
+
* Closes the cache. For MemoryLidCache, this is a no-op.
|
|
441
|
+
*
|
|
442
|
+
* @remarks The NodeCache instance is not closed to allow continued testing.
|
|
443
|
+
* If you need to free memory, use `clear()` before `close()`.
|
|
444
|
+
*/
|
|
445
|
+
close(): Promise<void>;
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* Configuration options for creating a LidCache instance.
|
|
449
|
+
*/
|
|
450
|
+
export interface LidCacheFactoryOptions {
|
|
451
|
+
/** Cache strategy: 'file' (default), 'memory', or custom LidCache instance */
|
|
452
|
+
strategy?: 'file' | 'memory' | LidCache;
|
|
453
|
+
/** Session name for file-based cache (used in filename) */
|
|
454
|
+
sessionName?: string;
|
|
455
|
+
/** TTL in seconds (default: 7 days) */
|
|
456
|
+
ttlSeconds?: number;
|
|
457
|
+
/** Base path for session files (default: process.cwd()) */
|
|
458
|
+
basePath?: string;
|
|
459
|
+
/** Logger for operational events (default: console) */
|
|
460
|
+
logger?: Console;
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Factory function to create a LidCache instance based on configuration.
|
|
464
|
+
*
|
|
465
|
+
* This factory centralizes cache creation logic, making it reusable across
|
|
466
|
+
* the codebase and easier to test/maintain.
|
|
467
|
+
*
|
|
468
|
+
* @param options - Factory configuration options
|
|
469
|
+
* @returns Configured LidCache instance
|
|
470
|
+
*
|
|
471
|
+
* @example
|
|
472
|
+
* ```typescript
|
|
473
|
+
* // Default: HybridLidCache (file + memory)
|
|
474
|
+
* const cache = createLidCache({ sessionName: 'my-bot' })
|
|
475
|
+
*
|
|
476
|
+
* // Memory-only (testing)
|
|
477
|
+
* const cache = createLidCache({ strategy: 'memory', ttlSeconds: 3600 })
|
|
478
|
+
*
|
|
479
|
+
* // Custom implementation
|
|
480
|
+
* const cache = createLidCache({ strategy: new RedisLidCache() })
|
|
481
|
+
* ```
|
|
482
|
+
*/
|
|
483
|
+
export declare function createLidCache(options?: LidCacheFactoryOptions): LidCache;
|
|
484
|
+
/**
|
|
485
|
+
* Minimal interface for Baileys message context key.
|
|
486
|
+
* Used for type-safe extraction of LID/PN mappings from incoming messages.
|
|
487
|
+
*
|
|
488
|
+
* @internal
|
|
489
|
+
*/
|
|
490
|
+
export interface MessageContextKey {
|
|
491
|
+
/** Remote JID (LID for DMs, group ID for groups) */
|
|
492
|
+
remoteJid?: string;
|
|
493
|
+
/** Participant LID in group messages */
|
|
494
|
+
participant?: string;
|
|
495
|
+
/** Participant phone number (if available) */
|
|
496
|
+
participantAlt?: string;
|
|
497
|
+
/** Alternative remote JID with phone number */
|
|
498
|
+
remoteJidAlt?: string;
|
|
499
|
+
/** Sender phone number (if available) */
|
|
500
|
+
senderPn?: string;
|
|
501
|
+
/** Participant phone number (alternative field) */
|
|
502
|
+
participantPn?: string;
|
|
503
|
+
}
|
|
504
|
+
/**
|
|
505
|
+
* Minimal interface for Baileys message context.
|
|
506
|
+
*
|
|
507
|
+
* @internal
|
|
508
|
+
*/
|
|
509
|
+
export interface MessageContext {
|
|
510
|
+
/** Message key with JID information */
|
|
511
|
+
key?: MessageContextKey;
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* Type guard to check if a value is a valid MessageContext.
|
|
515
|
+
*
|
|
516
|
+
* @param value - Value to check
|
|
517
|
+
* @returns true if the value matches MessageContext structure
|
|
518
|
+
*/
|
|
519
|
+
export declare function isMessageContext(value: unknown): value is MessageContext;
|
|
520
|
+
/**
|
|
521
|
+
* Extracts and caches LID → PN mapping from an incoming message.
|
|
522
|
+
*
|
|
523
|
+
* This utility function inspects the message context to find LID/PN pairs
|
|
524
|
+
* and stores them in the provided cache for future lookups.
|
|
525
|
+
*
|
|
526
|
+
* @param cache - LidCache instance to store the mapping
|
|
527
|
+
* @param messageCtx - Baileys message context (WAMessage)
|
|
528
|
+
* @returns Promise that resolves when caching is complete (or silently fails)
|
|
529
|
+
*
|
|
530
|
+
* @example
|
|
531
|
+
* ```typescript
|
|
532
|
+
* // In message handler:
|
|
533
|
+
* for (const message of messages) {
|
|
534
|
+
* await extractAndCacheLidFromMessage(lidCache, message)
|
|
535
|
+
* }
|
|
536
|
+
* ```
|
|
537
|
+
*/
|
|
538
|
+
export declare function extractAndCacheLidFromMessage(cache: LidCache, messageCtx: MessageContext): Promise<void>;
|
|
539
|
+
/**
|
|
540
|
+
* LID resolver function type.
|
|
541
|
+
* Takes a LID string and returns the corresponding phone number JID or null.
|
|
542
|
+
*/
|
|
543
|
+
export type LidResolver = (lid: LidJid) => Promise<PnJid | null>;
|
|
544
|
+
/**
|
|
545
|
+
* Resolves a LID to a phone number using cache-first strategy.
|
|
546
|
+
*
|
|
547
|
+
* This function implements the resolution chain:
|
|
548
|
+
* 1. Check cache first (O(1), ~1μs)
|
|
549
|
+
* 2. If miss, call fallback resolver (e.g., Baileys lidMapping)
|
|
550
|
+
* 3. If resolved, store in cache for future lookups
|
|
551
|
+
*
|
|
552
|
+
* @param cache - LidCache instance for storing/retrieving mappings
|
|
553
|
+
* @param fallbackResolver - Async function to resolve LID when not in cache
|
|
554
|
+
* @param logger - Logger for operational events (optional)
|
|
555
|
+
* @param lid - WhatsApp Local Identifier to resolve
|
|
556
|
+
* @returns Phone number in format '1234567890@s.whatsapp.net', or null
|
|
557
|
+
*
|
|
558
|
+
* @example
|
|
559
|
+
* ```typescript
|
|
560
|
+
* const pn = await resolveLidToPn(
|
|
561
|
+
* lidCache,
|
|
562
|
+
* (lid) => baileysSignalRepo.lidMapping.getPNForLID(lid),
|
|
563
|
+
* console,
|
|
564
|
+
* '123456789@lid' as LidJid
|
|
565
|
+
* )
|
|
566
|
+
* ```
|
|
567
|
+
*/
|
|
568
|
+
export declare function resolveLidToPn(cache: LidCache, fallbackResolver: LidResolver | ((lid: string) => Promise<string | null>), logger: Console | undefined, lid: LidJid | string): Promise<PnJid | string | null>;
|
|
569
|
+
//# sourceMappingURL=lidCache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lidCache.d.ts","sourceRoot":"","sources":["../src/lidCache.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AAoCH;;;;;;;;;;GAUG;AACH,MAAM,MAAM,MAAM,GAAG,MAAM,GAAG;IAAE,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAA;CAAE,CAAA;AAE5D;;;GAGG;AACH,MAAM,MAAM,KAAK,GAAG,MAAM,GAAG;IAAE,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAA;CAAE,CAAA;AAE1D;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAErD;AAED;;;GAGG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,GAAG,IAAI,CAInD;AAMD;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,WAAW,QAAQ;IACrB;;;;;OAKG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,KAAK,GAAG,MAAM,GAAG,IAAI,CAAC,CAAA;IAEzD;;;;;;;;;;;OAWG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,EAAE,EAAE,KAAK,GAAG,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAE5D;;;;;OAKG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,MAA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;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAsB,cAAc,CAChC,KAAK,EAAE,QAAQ,EACf,gBAAgB,EAAE,WAAW,GAAG,CAAC,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC,EACzE,MAAM,EAAE,OAAO,GAAG,SAAS,EAC3B,GAAG,EAAE,MAAM,GAAG,MAAM,GACrB,OAAO,CAAC,KAAK,GAAG,MAAM,GAAG,IAAI,CAAC,CAkChC"}
|
package/dist/type.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { GlobalVendorArgs } from '@builderbot/bot/dist/types';
|
|
2
2
|
import { proto, WABrowserDescription, WAVersion } from 'baileys';
|
|
3
|
+
import type { LidCache } from './lidCache';
|
|
3
4
|
export interface BaileyGlobalVendorArgs extends GlobalVendorArgs {
|
|
4
5
|
gifPlayback: boolean;
|
|
5
6
|
usePairingCode: boolean;
|
|
@@ -15,5 +16,17 @@ export interface BaileyGlobalVendorArgs extends GlobalVendorArgs {
|
|
|
15
16
|
version?: WAVersion;
|
|
16
17
|
autoRefresh?: number;
|
|
17
18
|
host?: any;
|
|
19
|
+
/**
|
|
20
|
+
* Estrategia de caché para resolución LID→PN.
|
|
21
|
+
* - 'file' (default): HybridLidCache (memory + file persistence)
|
|
22
|
+
* - 'memory': MemoryLidCache (solo memoria, no persiste)
|
|
23
|
+
* - LidCache: Implementación custom (e.g., Redis)
|
|
24
|
+
*/
|
|
25
|
+
lidCache?: 'file' | 'memory' | LidCache;
|
|
26
|
+
/**
|
|
27
|
+
* TTL (time-to-live) en segundos para entradas del LID cache.
|
|
28
|
+
* Default: 604800 (7 días)
|
|
29
|
+
*/
|
|
30
|
+
lidCacheTtl?: number;
|
|
18
31
|
}
|
|
19
32
|
//# sourceMappingURL=type.d.ts.map
|
package/dist/type.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../src/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAA;AAClE,OAAO,EAAE,KAAK,EAAE,oBAAoB,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../src/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAA;AAClE,OAAO,EAAE,KAAK,EAAE,oBAAoB,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAEhE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAE1C,MAAM,WAAW,sBAAuB,SAAQ,gBAAgB;IAC5D,WAAW,EAAE,OAAO,CAAA;IACpB,cAAc,EAAE,OAAO,CAAA;IACvB,WAAW,EAAE,MAAM,GAAG,IAAI,CAAA;IAC1B,OAAO,EAAE,oBAAoB,CAAA;IAC7B,uBAAuB,CAAC,EAAE,MAAM,CAAA;IAChC,cAAc,CAAC,EAAE,CAAC,GAAG,EAAE,KAAK,CAAC,eAAe,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;IAC9D,eAAe,EAAE,OAAO,CAAA;IACxB,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,iBAAiB,CAAC,EAAE,OAAO,CAAA;IAC3B,YAAY,EAAE,OAAO,CAAA;IACrB,UAAU,EAAE,OAAO,CAAA;IACnB,OAAO,CAAC,EAAE,SAAS,CAAA;IACnB,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,IAAI,CAAC,EAAE,GAAG,CAAA;IAEV;;;;;OAKG;IACH,QAAQ,CAAC,EAAE,MAAM,GAAG,QAAQ,GAAG,QAAQ,CAAA;IAEvC;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAA;CACvB"}
|