export * from './types/index.js'; import { Worker } from 'worker_threads'; import type { WAClientConfig, ActionSendMessage, ActionClearUnreadCount, MasterToWorkerActions, WorkerToMasterEvents, CacheConfig, MigrationPolicyConfig, TopicsInput } from './types/index.js'; import utils from './utils/index.js'; import { MasterPipe } from './pipe/MasterPipe.js'; import { MongoStorage } from './store/index.js'; export interface BaseMasterConfig { storePath?: string; debug?: boolean; cacheConfig?: CacheConfig; migrations?: MigrationPolicyConfig; } export interface GracefulConfig { concurrency: number; timeout: number; } export interface MongoClientConfig extends BaseMasterConfig { type: 'mongo'; mongoUrl: string; archiveDaysOld?: number; graceful?: GracefulConfig; } export type WAMasterConfig = MongoClientConfig; /** * Main controller class for managing WhatsApp clients using worker threads and Mongo storage. */ export declare class WAMaster extends MasterPipe { /** * Configuration details for all active clients. */ readonly configs: Record; /** * Worker instances for each active client. */ readonly workers: Record; /** * Mongo storage instances for each client. */ private readonly storages; private readonly timers; private readonly restartAttempts; private readonly storePath; /** * URL of the Mongo store used for storage and communication. */ readonly mongoUrl?: string; /** * Utility functions for file operations and configuration management. */ readonly utils: ReturnType; private readonly mongo?; private readonly debug; private readonly cacheConfig?; private readonly migrationsConfig; private readonly MAX_RESTART_ATTEMPTS; private readonly logger; private readonly gracefulConfig?; /** * Initializes a new instance of `WAMaster`. * @param config - Optional configuration for WAMaster. */ constructor(config?: WAMasterConfig); private terminate; /** * Starts multiple WhatsApp clients in a controlled, batched manner. * * This method helps prevent performance bottlenecks when starting many clients * at once by limiting concurrency and spacing out startup calls with a delay. * It is particularly useful for production systems where starting all clients * simultaneously could overload CPU, memory, or network. * * Internally uses a `fastq` queue to manage concurrency and scheduling. * * @param configs - An array of client configuration objects to start. * @param concurrency - The number of clients to start in parallel. Default is `1` (sequential). * @param delayMs - Delay (in milliseconds) between client startups, per slot. Default is `300ms`. * * @example * await waMaster.startClientsInBatch([ * { clientId: '123', type: 'mongo', socketConfig: { syncFullHistory: true }, browser: ['Mac OS', 'Chrome', '119.0.0.0'] }, * { clientId: '456', type: 'mongo', socketConfig: { syncFullHistory: true }, browser: ['Mac OS', 'Chrome', '119.0.0.0'] }, * ], 2, 500); * * // Starts 2 clients at a time, with 500ms delay between each batch */ startClientsInBatch(configs: WAClientConfig[], concurrency?: number, delayMs?: number): Promise; /** * Starts a new client with the specified configuration. * @param clientConfig - Configuration for the new client. */ startClient(clientConfig: WAClientConfig): void; /** * Gracefully restarts a running client and reinitializes it from scratch. * @param clientId - The ID of the client to restart. */ restartClient(clientId: string): Promise; /** * Destroy a running client and cleans up associated resources and logout from account * @param clientId - The ID of the client to stop. */ destroyClient(clientId: string): Promise; /** * Sends a subscription request to a specific WhatsApp client. * * This allows the master process (e.g. via WebSocket layer or API) to * dynamically register interest in specific topics (e.g. chat updates, presence) * on behalf of one or more users. * * Internally delegates to the client’s subscription manager. * * @param clientId - The unique identifier of the target WhatsApp client. * @param payload - An object containing the `topics` array to subscribe to. * * @example * waMaster.postSubscribeTopics('client-abc', { topics: ['client-abc/123/messages.upsert'] }); */ postSubscribeTopics(clientId: string, payload: TopicsInput): void; /** * Sends an unsubscription request to a specific WhatsApp client. * * This is typically triggered when a user navigates away from a chat view * or disconnects their session, and the master no longer needs to receive * events for certain topics. * * If the client is not running, logs a warning and skips the request. * * @param clientId - The unique identifier of the target WhatsApp client. * @param payload - An object containing the `topics` array to unsubscribe from. * * @example * waMaster.postUnsubscribeTopics('client-abc', { topics: ['client-abc/123/messages.upsert'] }); */ postUnsubscribeTopics(clientId: string, payload: TopicsInput): void; /** * Sends a heartbeat signal to the specified client to check its status. * @param clientId - The ID of the client to ping. */ heartBeat(clientId: string): void; /** * Sends a message through a specific client. * @param clientId - The ID of the client to use. * @param post - Details of the message to send. */ postSendMessage(clientId: string, post: ActionSendMessage): void; /** * Clears the unread message count for a specific chat. * @param clientId - The ID of the client to use. * @param post - Details of the chat to clear unread counts for. */ postClearUnreadCount(clientId: string, post: ActionClearUnreadCount): void; /** * Triggers an immediate profile picture fetch for a specific client. * * This method sends a `fetch.pps` signal to the given client’s worker thread, * instructing it to initiate a profile picture fetch operation for its contacts. * * This is useful in scenarios such as: * - Immediately after a client connects or restarts. * - Manually requesting an up-to-date set of profile pictures. * * If the client is not running (i.e., no worker found), the request is skipped silently. * * @param clientId - The unique identifier of the client to fetch profile pictures for. */ postfetchPPs(clientId: string): void; private handleWorkerError; private restartQueue; private handleWorkerExit; /** * Getter for the storage instance with dynamic typing. */ get stores(): Record; /** * Graceful close a running client and cleans up associated resources * @param clientId - The ID of the client to stop. */ graceful(): Promise; }