refactor: centralize WhatsApp version to single source of truth
Instead of maintaining the version number in 3 separate files (baileys-version.json, index.ts, generics.ts), this change makes baileys-version.json the single source of truth. Other files now import the version from this JSON file. This follows the DRY principle and reduces: - Risk of version inconsistency across files - Maintenance burden (only 1 file to update) - PR diff size (3 files → 1 file for version updates) - Merge conflict probability Addresses inefficiency found in Baileys PR #2269.
This commit is contained in:
+78
-88
@@ -1,40 +1,101 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
/**
|
/**
|
||||||
* Script to update WhatsApp Web version across the codebase.
|
* Script to update WhatsApp Web version.
|
||||||
* Fetches the latest version from web.whatsapp.com and updates:
|
* Fetches the latest version from web.whatsapp.com and updates:
|
||||||
* - src/Defaults/baileys-version.json
|
* - src/Defaults/baileys-version.json (SINGLE SOURCE OF TRUTH)
|
||||||
* - src/Defaults/index.ts
|
*
|
||||||
* - src/Utils/generics.ts
|
* Other files (index.ts, generics.ts) import from this JSON file,
|
||||||
|
* so only one file needs to be updated.
|
||||||
*
|
*
|
||||||
* Usage: yarn update:version
|
* Usage: yarn update:version
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { readFileSync, writeFileSync } from 'fs'
|
import { readFileSync, writeFileSync, appendFileSync } from 'fs'
|
||||||
import { dirname, join } from 'path'
|
import { dirname, join } from 'path'
|
||||||
import { fileURLToPath } from 'url'
|
import { fileURLToPath } from 'url'
|
||||||
import { fetchLatestWaWebVersion } from '../src/Utils/generics.ts'
|
|
||||||
|
|
||||||
const __filename = fileURLToPath(import.meta.url)
|
const __filename = fileURLToPath(import.meta.url)
|
||||||
const __dirname = dirname(__filename)
|
const __dirname = dirname(__filename)
|
||||||
const ROOT_DIR = join(__dirname, '..')
|
const ROOT_DIR = join(__dirname, '..')
|
||||||
|
const VERSION_FILE_PATH = join(ROOT_DIR, 'src/Defaults/baileys-version.json')
|
||||||
|
|
||||||
function updateBaileysVersionJson(version: [number, number, number]): boolean {
|
type WAVersion = [number, number, number]
|
||||||
const filePath = join(ROOT_DIR, 'src/Defaults/baileys-version.json')
|
|
||||||
const content = {
|
interface VersionResult {
|
||||||
version
|
version: WAVersion
|
||||||
}
|
isLatest: boolean
|
||||||
|
error?: unknown
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetches the latest WhatsApp Web version from web.whatsapp.com
|
||||||
|
* Extracted here to avoid circular dependency with generics.ts
|
||||||
|
*/
|
||||||
|
async function fetchLatestWaWebVersion(): Promise<VersionResult> {
|
||||||
|
// Read current version as fallback
|
||||||
|
const currentContent = readFileSync(VERSION_FILE_PATH, 'utf-8')
|
||||||
|
const fallbackVersion = JSON.parse(currentContent).version as WAVersion
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const currentContent = readFileSync(filePath, 'utf-8')
|
const headers = {
|
||||||
|
'sec-fetch-site': 'none',
|
||||||
|
'user-agent':
|
||||||
|
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await fetch('https://web.whatsapp.com/sw.js', {
|
||||||
|
method: 'GET',
|
||||||
|
headers
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Failed to fetch sw.js: ${response.statusText}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await response.text()
|
||||||
|
const regex = /\\?"client_revision\\?":\s*(\d+)/
|
||||||
|
const match = data.match(regex)
|
||||||
|
|
||||||
|
if (!match?.[1]) {
|
||||||
|
return {
|
||||||
|
version: fallbackVersion,
|
||||||
|
isLatest: false,
|
||||||
|
error: { message: 'Could not find client revision in the fetched content' }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
version: [2, 3000, +match[1]] as WAVersion,
|
||||||
|
isLatest: true
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
return {
|
||||||
|
version: fallbackVersion,
|
||||||
|
isLatest: false,
|
||||||
|
error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateBaileysVersionJson(version: WAVersion): boolean {
|
||||||
|
const content = { version }
|
||||||
|
|
||||||
|
try {
|
||||||
|
const currentContent = readFileSync(VERSION_FILE_PATH, 'utf-8')
|
||||||
const currentVersion = JSON.parse(currentContent).version as number[]
|
const currentVersion = JSON.parse(currentContent).version as number[]
|
||||||
|
|
||||||
if (currentVersion[0] === version[0] && currentVersion[1] === version[1] && currentVersion[2] === version[2]) {
|
if (
|
||||||
|
currentVersion[0] === version[0] &&
|
||||||
|
currentVersion[1] === version[1] &&
|
||||||
|
currentVersion[2] === version[2]
|
||||||
|
) {
|
||||||
console.log(`✓ baileys-version.json already up to date`)
|
console.log(`✓ baileys-version.json already up to date`)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
writeFileSync(filePath, JSON.stringify(content) + '\n')
|
writeFileSync(VERSION_FILE_PATH, JSON.stringify(content) + '\n')
|
||||||
console.log(`✓ Updated baileys-version.json: [${currentVersion.join(', ')}] → [${version.join(', ')}]`)
|
console.log(`✓ Updated baileys-version.json: [${currentVersion.join(', ')}] → [${version.join(', ')}]`)
|
||||||
|
console.log(` (index.ts and generics.ts will automatically use the new version)`)
|
||||||
return true
|
return true
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`✗ Failed to update baileys-version.json:`, error)
|
console.error(`✗ Failed to update baileys-version.json:`, error)
|
||||||
@@ -42,69 +103,6 @@ function updateBaileysVersionJson(version: [number, number, number]): boolean {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateGenerics(version: [number, number, number]): boolean {
|
|
||||||
const filePath = join(ROOT_DIR, 'src/Utils/generics.ts')
|
|
||||||
|
|
||||||
try {
|
|
||||||
const content = readFileSync(filePath, 'utf-8')
|
|
||||||
const versionRegex = /const baileysVersion = \[(\d+),\s*(\d+),\s*(\d+)\]/
|
|
||||||
const match = content.match(versionRegex)
|
|
||||||
|
|
||||||
if (!match) {
|
|
||||||
throw new Error('Could not find baileysVersion declaration in generics.ts')
|
|
||||||
}
|
|
||||||
|
|
||||||
const currentVersion = [+match[1]!, +match[2]!, +match[3]!]
|
|
||||||
|
|
||||||
if (currentVersion[0] === version[0] && currentVersion[1] === version[1] && currentVersion[2] === version[2]) {
|
|
||||||
console.log(`✓ src/Utils/generics.ts already up to date`)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
const newContent = content.replace(
|
|
||||||
versionRegex,
|
|
||||||
`const baileysVersion = [${version[0]}, ${version[1]}, ${version[2]}]`
|
|
||||||
)
|
|
||||||
|
|
||||||
writeFileSync(filePath, newContent)
|
|
||||||
console.log(`✓ Updated src/Utils/generics.ts: [${currentVersion.join(', ')}] → [${version.join(', ')}]`)
|
|
||||||
return true
|
|
||||||
} catch (error) {
|
|
||||||
console.error(`✗ Failed to update src/Utils/generics.ts:`, error)
|
|
||||||
throw error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function updateIndex(version: [number, number, number]): boolean {
|
|
||||||
const filePath = join(ROOT_DIR, 'src/Defaults/index.ts')
|
|
||||||
|
|
||||||
try {
|
|
||||||
const content = readFileSync(filePath, 'utf-8')
|
|
||||||
const versionRegex = /const version = \[(\d+),\s*(\d+),\s*(\d+)\]/
|
|
||||||
const match = content.match(versionRegex)
|
|
||||||
|
|
||||||
if (!match) {
|
|
||||||
throw new Error('Could not find version declaration in index.ts')
|
|
||||||
}
|
|
||||||
|
|
||||||
const currentVersion = [+match[1]!, +match[2]!, +match[3]!]
|
|
||||||
|
|
||||||
if (currentVersion[0] === version[0] && currentVersion[1] === version[1] && currentVersion[2] === version[2]) {
|
|
||||||
console.log(`✓ src/Defaults/index.ts already up to date`)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
const newContent = content.replace(versionRegex, `const version = [${version[0]}, ${version[1]}, ${version[2]}]`)
|
|
||||||
|
|
||||||
writeFileSync(filePath, newContent)
|
|
||||||
console.log(`✓ Updated src/Defaults/index.ts: [${currentVersion.join(', ')}] → [${version.join(', ')}]`)
|
|
||||||
return true
|
|
||||||
} catch (error) {
|
|
||||||
console.error(`✗ Failed to update src/Defaults/index.ts:`, error)
|
|
||||||
throw error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
console.log('Fetching latest WhatsApp Web version...\n')
|
console.log('Fetching latest WhatsApp Web version...\n')
|
||||||
|
|
||||||
@@ -117,27 +115,19 @@ async function main() {
|
|||||||
|
|
||||||
console.log(`Latest version: [${result.version.join(', ')}]\n`)
|
console.log(`Latest version: [${result.version.join(', ')}]\n`)
|
||||||
|
|
||||||
const updates = [
|
const hasUpdates = updateBaileysVersionJson(result.version)
|
||||||
updateBaileysVersionJson(result.version),
|
|
||||||
updateGenerics(result.version),
|
|
||||||
updateIndex(result.version)
|
|
||||||
]
|
|
||||||
|
|
||||||
const hasUpdates = updates.some(Boolean)
|
|
||||||
|
|
||||||
console.log('')
|
console.log('')
|
||||||
if (hasUpdates) {
|
if (hasUpdates) {
|
||||||
console.log('Version update complete!')
|
console.log('Version update complete!')
|
||||||
// Set GitHub Actions output if running in CI
|
console.log('Note: Only baileys-version.json needs updating - other files import from it.')
|
||||||
if (process.env.GITHUB_OUTPUT) {
|
if (process.env.GITHUB_OUTPUT) {
|
||||||
const { appendFileSync } = await import('fs')
|
|
||||||
appendFileSync(process.env.GITHUB_OUTPUT, `updated=true\n`)
|
appendFileSync(process.env.GITHUB_OUTPUT, `updated=true\n`)
|
||||||
appendFileSync(process.env.GITHUB_OUTPUT, `version=${result.version.join('.')}\n`)
|
appendFileSync(process.env.GITHUB_OUTPUT, `version=${result.version.join('.')}\n`)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
console.log('All files are already up to date.')
|
console.log('Already up to date.')
|
||||||
if (process.env.GITHUB_OUTPUT) {
|
if (process.env.GITHUB_OUTPUT) {
|
||||||
const { appendFileSync } = await import('fs')
|
|
||||||
appendFileSync(process.env.GITHUB_OUTPUT, `updated=false\n`)
|
appendFileSync(process.env.GITHUB_OUTPUT, `updated=false\n`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,10 @@ import { makeLibSignalRepository } from '../Signal/libsignal'
|
|||||||
import type { AuthenticationState, SocketConfig, WAVersion } from '../Types'
|
import type { AuthenticationState, SocketConfig, WAVersion } from '../Types'
|
||||||
import { Browsers } from '../Utils/browser-utils'
|
import { Browsers } from '../Utils/browser-utils'
|
||||||
import logger from '../Utils/logger'
|
import logger from '../Utils/logger'
|
||||||
|
// Single source of truth for WhatsApp Web version - imported from JSON
|
||||||
|
import baileysVersionData from './baileys-version.json'
|
||||||
|
|
||||||
const version = [2, 3000, 1032141294]
|
const version = baileysVersionData.version
|
||||||
|
|
||||||
export const UNAUTHORIZED_CODES = [401, 403, 419]
|
export const UNAUTHORIZED_CODES = [401, 403, 419]
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
import { Boom } from '@hapi/boom'
|
import { Boom } from '@hapi/boom'
|
||||||
import { createHash, randomBytes } from 'crypto'
|
import { createHash, randomBytes } from 'crypto'
|
||||||
import { proto } from '../../WAProto/index.js'
|
import { proto } from '../../WAProto/index.js'
|
||||||
const baileysVersion = [2, 3000, 1032141294]
|
// Single source of truth for WhatsApp Web version - imported from JSON
|
||||||
|
import baileysVersionData from '../Defaults/baileys-version.json'
|
||||||
|
|
||||||
|
const baileysVersion = baileysVersionData.version
|
||||||
import type {
|
import type {
|
||||||
BaileysEventEmitter,
|
BaileysEventEmitter,
|
||||||
BaileysEventMap,
|
BaileysEventMap,
|
||||||
|
|||||||
Reference in New Issue
Block a user