@rodrigogs/baileys-store
Version:
Baileys Store
138 lines (137 loc) • 5.97 kB
TypeScript
import { AuthenticationCreds } from 'baileys';
import Keyv from 'keyv';
/**
* Generic storage interface compatible with Keyv and other key-value stores
*/
export interface StorageAdapter {
/**
* Get a value from storage
* @param key - The key to retrieve
* @returns The value, or undefined if not found
*/
get(key: string): Promise<string | undefined>;
/**
* Set a value in storage
* @param key - The key to store
* @param value - The value to store
* @param ttl - Optional time-to-live in milliseconds
*/
set(key: string, value: string, ttl?: number): Promise<void>;
/**
* Delete a value from storage
* @param key - The key to delete
* @returns True if the key was deleted, false otherwise
*/
delete(key: string): Promise<boolean>;
/**
* Clear all values from storage
*/
clear(): Promise<void>;
}
/**
* Creates an authentication state backed by a generic key-value store (e.g. Keyv).
*
* @deprecated The function name is misleading as it no longer uses cache-manager. Use `makeKeyvAuthState` instead.
* This function is kept as an alias for backwards compatibility.
*
* The returned object is compatible with Baileys' `useMultiFileAuthState` / `useSingleFileAuthState`
* shape and can be passed directly to Baileys when initializing the connection.
*
* @param store - The storage backend used to persist credentials and keys. Can be a `Keyv` instance
* or any `StorageAdapter` implementation exposing `get`, `set`, `delete`, and `clear` methods.
* @param sessionKey - A unique identifier for the session. It is used as a prefix for all keys
* stored in the underlying `store` so that multiple sessions can safely share the same backend.
*
* @returns An object containing:
* - `state`: The current auth state with:
* - `creds`: The loaded or newly initialized `AuthenticationCreds` instance.
* - `keys`: A key store with:
* - `get(type, ids)`: Asynchronously retrieves key data for the given `type` and list of `ids`,
* returning an object mapping each id to its stored value (or `null` if missing).
* - `set(data)`: Persists or removes key data. For each `data[category][id]`, a truthy value is
* stored, and a falsy value causes the corresponding key to be deleted.
* - `saveCreds()`: A function that persists the current credentials (`creds`) to the storage backend.
* Call this after Baileys updates the credentials (e.g. inside the `creds.update` event handler).
* - `clearState()`: A function that clears all stored data. **Important limitation**: This calls
* `clear()` on the entire store, which will delete data from ALL sessions if multiple sessions
* share the same store instance. This breaks session isolation.
*
* **Recommended solutions for session isolation (prefer option 1 for most use cases)**:
* 1. Use Keyv with the `namespace` option (one namespace per session) when sharing a backend:
* ```ts
* const store = new Keyv({ namespace: 'session-1' })
* // clearState() will only affect this namespace while still sharing the same underlying store
* ```
* This is usually more efficient than creating separate Keyv instances because it reuses the same
* connection/pool while keeping per-session data isolated.
* 2. Use separate store instances per session (less efficient than namespaces, but useful if your
* storage backend does not support namespacing or similar isolation features).
* 3. If multiple sessions share one store instance and you cannot use namespaces, avoid calling
* `clearState()` and instead clear only the keys that belong to the specific session.
*
* @example
* ```ts
* import Keyv from 'keyv'
* import { makeKeyvAuthState } from '@rodrigogs/baileys-store'
* import makeWASocket from 'baileys'
*
* const store = new Keyv('sqlite://auth.db')
*
* async function init() {
* const { state, saveCreds, clearState } = await makeKeyvAuthState(store, 'session-1')
*
* // Pass `state` to Baileys when creating the socket
* const sock = makeWASocket({ auth: state })
*
* // When credentials change, persist them
* sock.ev.on('creds.update', saveCreds)
*
* // To fully reset this session later:
* // await clearState()
* }
* ```
*/
declare const makeCacheManagerAuthState: (store: Keyv | StorageAdapter, sessionKey: string) => Promise<{
clearState: () => Promise<void>;
saveCreds: () => Promise<void>;
state: {
creds: AuthenticationCreds;
keys: {
get: (type: string, ids: string[]) => Promise<{}>;
set: (data: any) => Promise<void>;
};
};
}>;
/**
* Creates an authentication state backed by Keyv or any compatible storage adapter.
*
* This is the recommended function name that clearly indicates it works with Keyv-based storage.
* Alias for `makeCacheManagerAuthState`.
*
* @param store - The storage backend (Keyv instance or StorageAdapter implementation)
* @param sessionKey - A unique identifier for the session used as key prefix
* @returns Authentication state object with state, saveCreds, and clearState
*
* @example
* ```ts
* import Keyv from 'keyv'
* import { makeKeyvAuthState } from '@rodrigogs/baileys-store'
*
* // Recommended: Use Keyv with namespace for session isolation
* const store = new Keyv({ namespace: 'session-1' })
* const { state, saveCreds } = await makeKeyvAuthState(store, 'session-1')
* ```
*/
declare const makeKeyvAuthState: (store: Keyv | StorageAdapter, sessionKey: string) => Promise<{
clearState: () => Promise<void>;
saveCreds: () => Promise<void>;
state: {
creds: AuthenticationCreds;
keys: {
get: (type: string, ids: string[]) => Promise<{}>;
set: (data: any) => Promise<void>;
};
};
}>;
export default makeCacheManagerAuthState;
export { makeCacheManagerAuthState, makeKeyvAuthState, Keyv };