1import fs from 'node:fs';
2import path from 'node:path';
3import { cloudflareVersion, cloudflareList } from '../../config/binaries.js';
4import { tmpFile, downloadToFile, extractBinary } from './downloadHelper.js';
5
6const binaryName = `cfspeedtest${process.platform === 'win32' ? '.exe' : ''}`;
7const binaryRegex = /cfspeedtest(.exe)?$/;
8const binaryDirectory = path.join(process.cwd(), 'bin');
9const binaryPath = path.join(binaryDirectory, binaryName);
10const downloadBaseURL = `https://github.com/code-inflation/cfspeedtest/releases/download/v${cloudflareVersion}/`;
11
12export const fileExists = async () => fs.existsSync(binaryPath);
13
14export const downloadFile = async () => {
15 let binary = cloudflareList.find(b => b.os === process.platform && b.arch === process.arch);
16
17 if (!binary && process.platform === 'darwin')
18 binary = cloudflareList.find(b => b.os === 'darwin' && b.arch === 'universal');
19
20 if (!binary)
21 throw new Error(`Your platform (${process.platform}-${process.arch}) is not supported by the Cloudflare CLI`);
22
23 const archivePath = tmpFile(binary.suffix);
24 await downloadToFile(downloadBaseURL + binary.suffix, archivePath);
25 await extractBinary(archivePath, binaryDirectory, binaryRegex, binaryName);
26};
27
28export const load = async () => {
29 if (!await fileExists()) await downloadFile();
30};
31