diff --git a/src/Defaults/index.ts b/src/Defaults/index.ts index ea737f15..5fb51da3 100644 --- a/src/Defaults/index.ts +++ b/src/Defaults/index.ts @@ -49,6 +49,7 @@ export const PROCESSABLE_HISTORY_TYPES = [ export const DEFAULT_CONNECTION_CONFIG: SocketConfig = { version: version as WAVersion, + fetchLatestVersion: false, browser: Browsers.macOS('Chrome'), waWebSocketUrl: 'wss://web.whatsapp.com/ws/chat', connectTimeoutMs: 20_000, diff --git a/src/Socket/index.ts b/src/Socket/index.ts index 3ee2d850..cd76a368 100644 --- a/src/Socket/index.ts +++ b/src/Socket/index.ts @@ -1,5 +1,6 @@ import { DEFAULT_CONNECTION_CONFIG } from '../Defaults' import type { UserFacingSocketConfig } from '../Types' +import { fetchLatestWaWebVersion } from '../Utils/generics' import { makeCommunitiesSocket } from './communities' // export the last socket layer @@ -19,4 +20,48 @@ const makeWASocket = (config: UserFacingSocketConfig) => { return makeCommunitiesSocket(newConfig) } +/** + * Creates a WhatsApp socket connection with automatic version fetching. + * Fetches the latest WhatsApp Web version from web.whatsapp.com before connecting. + * Falls back to bundled version if fetch fails. + * + * @example + * ```typescript + * const sock = await makeWASocketAutoVersion({ + * auth: state + * }) + * ``` + */ +export const makeWASocketAutoVersion = async (config: UserFacingSocketConfig) => { + const mergedConfig = { + ...DEFAULT_CONNECTION_CONFIG, + ...config + } + + const logger = mergedConfig.logger + + // Fetch latest version + try { + logger?.info('Fetching latest WhatsApp Web version...') + const result = await fetchLatestWaWebVersion() + + if (result.isLatest) { + logger?.info({ version: result.version }, 'Using latest WhatsApp Web version') + mergedConfig.version = result.version + } else { + logger?.warn( + { error: result.error, fallbackVersion: mergedConfig.version }, + 'Failed to fetch latest version, using bundled version' + ) + } + } catch (error) { + logger?.warn( + { error, fallbackVersion: mergedConfig.version }, + 'Error fetching latest version, using bundled version' + ) + } + + return makeWASocket(mergedConfig) +} + export default makeWASocket diff --git a/src/Types/Socket.ts b/src/Types/Socket.ts index 4a3f492c..1fb23b8c 100644 --- a/src/Types/Socket.ts +++ b/src/Types/Socket.ts @@ -49,6 +49,13 @@ export type SocketConfig = { logger: ILogger /** version to connect with */ version: WAVersion + /** + * Automatically fetch the latest WhatsApp Web version on connect. + * When enabled, fetches from web.whatsapp.com before connecting. + * Falls back to bundled version if fetch fails. + * @default false + */ + fetchLatestVersion: boolean /** override browser config */ browser: WABrowserDescription /** agent used for fetch requests -- uploading/downloading media */ diff --git a/src/index.ts b/src/index.ts index feb53e2a..b73f5bb0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import makeWASocket from './Socket/index' +import makeWASocket, { makeWASocketAutoVersion } from './Socket/index' export * from '../WAProto/index.js' export * from './Utils/index' @@ -9,5 +9,5 @@ export * from './WAM/index' export * from './WAUSync/index' export type WASocket = ReturnType -export { makeWASocket } +export { makeWASocket, makeWASocketAutoVersion } export default makeWASocket