feat: Improve update verification using the GitHub API for always-fresh data and manage rate limits

This commit is contained in:
2025-11-11 13:23:02 +01:00
parent a359e97125
commit 759229902f

View File

@@ -265,7 +265,8 @@ function getUpdateMode(configData) {
/** /**
* Check if update is available by comparing versions * Check if update is available by comparing versions
* Returns true if versions differ (allows both upgrades and downgrades) * Uses GitHub API directly (no CDN cache issues, always fresh data)
* Rate limit: 60 requests/hour (sufficient for bot updates)
*/ */
async function checkVersion() { async function checkVersion() {
try { try {
@@ -279,32 +280,32 @@ async function checkVersion() {
const localPkg = JSON.parse(readFileSync(localPkgPath, 'utf8')) const localPkg = JSON.parse(readFileSync(localPkgPath, 'utf8'))
const localVersion = localPkg.version const localVersion = localPkg.version
// Fetch remote version from GitHub // Fetch remote version from GitHub API (no cache)
const repoOwner = 'Obsidian-wtf' const repoOwner = 'Obsidian-wtf'
const repoName = 'Microsoft-Rewards-Bot' const repoName = 'Microsoft-Rewards-Bot'
const branch = 'main' const branch = 'main'
// Add cache-buster to prevent GitHub from serving stale cached version
const cacheBuster = Date.now()
const pkgUrl = `https://raw.githubusercontent.com/${repoOwner}/${repoName}/refs/heads/${branch}/package.json?cb=${cacheBuster}`
console.log('🔍 Checking for updates...') console.log('🔍 Checking for updates...')
console.log(` Local: ${localVersion}`) console.log(` Local: ${localVersion}`)
// Use GitHub API directly - no CDN cache, always fresh
const apiUrl = `https://api.github.com/repos/${repoOwner}/${repoName}/contents/package.json?ref=${branch}`
return new Promise((resolve) => { return new Promise((resolve) => {
// Request with cache-busting headers
const options = { const options = {
headers: { headers: {
'Cache-Control': 'no-cache, no-store, must-revalidate', 'User-Agent': 'Microsoft-Rewards-Bot-Updater',
'Pragma': 'no-cache', 'Accept': 'application/vnd.github.v3.raw', // Returns raw file content
'Expires': '0', 'Cache-Control': 'no-cache'
'User-Agent': 'Microsoft-Rewards-Bot-Updater'
} }
} }
const request = httpsGet(pkgUrl, options, (res) => { const request = httpsGet(apiUrl, options, (res) => {
if (res.statusCode !== 200) { if (res.statusCode !== 200) {
console.log(` ⚠️ Could not check remote version (HTTP ${res.statusCode})`) console.log(` ⚠️ GitHub API returned HTTP ${res.statusCode}`)
if (res.statusCode === 403) {
console.log(' Rate limit may be exceeded (60/hour). Try again later.')
}
resolve({ updateAvailable: false, localVersion, remoteVersion: 'unknown' }) resolve({ updateAvailable: false, localVersion, remoteVersion: 'unknown' })
return return
} }
@@ -327,7 +328,6 @@ async function checkVersion() {
}) })
}) })
// Timeout after 10 seconds
request.on('error', (err) => { request.on('error', (err) => {
console.log(` ⚠️ Network error: ${err.message}`) console.log(` ⚠️ Network error: ${err.message}`)
resolve({ updateAvailable: false, localVersion, remoteVersion: 'unknown' }) resolve({ updateAvailable: false, localVersion, remoteVersion: 'unknown' })
@@ -335,7 +335,7 @@ async function checkVersion() {
request.setTimeout(10000, () => { request.setTimeout(10000, () => {
request.destroy() request.destroy()
console.log(' ⚠️ Request timeout') console.log(' ⚠️ Request timeout (10s)')
resolve({ updateAvailable: false, localVersion, remoteVersion: 'unknown' }) resolve({ updateAvailable: false, localVersion, remoteVersion: 'unknown' })
}) })
}) })