(version)
| 304 | } |
| 305 | |
| 306 | async function downloadBinary(version) { |
| 307 | const platformKey = `${process.platform}-${process.arch}` |
| 308 | const fileName = PLATFORM_TARGETS[platformKey] |
| 309 | |
| 310 | if (!fileName) { |
| 311 | const error = new Error(`Unsupported platform: ${process.platform} ${process.arch}`) |
| 312 | trackUpdateFailed(error.message, version, { stage: 'platform_check' }) |
| 313 | throw error |
| 314 | } |
| 315 | |
| 316 | const downloadUrl = `${ |
| 317 | process.env.NEXT_PUBLIC_CODEBUFF_APP_URL || 'https://codebuff.com' |
| 318 | }/api/releases/download/${version}/${fileName}` |
| 319 | |
| 320 | fs.mkdirSync(CONFIG.configDir, { recursive: true }) |
| 321 | |
| 322 | if (fs.existsSync(CONFIG.tempDownloadDir)) { |
| 323 | fs.rmSync(CONFIG.tempDownloadDir, { recursive: true }) |
| 324 | } |
| 325 | fs.mkdirSync(CONFIG.tempDownloadDir, { recursive: true }) |
| 326 | |
| 327 | term.write('Downloading...') |
| 328 | |
| 329 | const res = await httpGet(downloadUrl) |
| 330 | |
| 331 | if (res.statusCode !== 200) { |
| 332 | fs.rmSync(CONFIG.tempDownloadDir, { recursive: true }) |
| 333 | const error = new Error(`Download failed: HTTP ${res.statusCode}`) |
| 334 | trackUpdateFailed(error.message, version, { stage: 'http_download', statusCode: res.statusCode }) |
| 335 | throw error |
| 336 | } |
| 337 | |
| 338 | const totalSize = parseInt(res.headers['content-length'] || '0', 10) |
| 339 | let downloadedSize = 0 |
| 340 | let lastProgressTime = Date.now() |
| 341 | |
| 342 | res.on('data', (chunk) => { |
| 343 | downloadedSize += chunk.length |
| 344 | const now = Date.now() |
| 345 | if (now - lastProgressTime >= 100 || downloadedSize === totalSize) { |
| 346 | lastProgressTime = now |
| 347 | if (totalSize > 0) { |
| 348 | const pct = Math.round((downloadedSize / totalSize) * 100) |
| 349 | term.write( |
| 350 | `Downloading... ${createProgressBar(pct)} ${pct}% of ${formatBytes( |
| 351 | totalSize, |
| 352 | )}`, |
| 353 | ) |
| 354 | } else { |
| 355 | term.write(`Downloading... ${formatBytes(downloadedSize)}`) |
| 356 | } |
| 357 | } |
| 358 | }) |
| 359 | |
| 360 | await new Promise((resolve, reject) => { |
| 361 | res |
| 362 | .pipe(zlib.createGunzip()) |
| 363 | .pipe(tar.x({ cwd: CONFIG.tempDownloadDir })) |
no test coverage detected