UNPKG

crypto-ylp

Version:

A simple Node.js wrapper for XMRig Monero mining with configurable intensity

55 lines (45 loc) 1.53 kB
const fs = require('fs'); const path = require('path'); const os = require('os'); const fetch = require('node-fetch'); const unzipper = require('unzipper'); const tar = require('tar'); async function downloadXmrig(targetDir) { const platform = os.platform(); let downloadUrl, archiveName; if (platform === 'linux') { archiveName = 'xmrig-6.22.2-linux-static-x64.tar.gz'; downloadUrl = `https://github.com/xmrig/xmrig/releases/download/v6.22.2/${archiveName}`; } else if (platform === 'win32') { archiveName = 'xmrig-6.22.2-msvc-win64.zip'; downloadUrl = `https://github.com/xmrig/xmrig/releases/download/v6.22.2/${archiveName}`; } else { throw new Error('Unsupported platform'); } if (!fs.existsSync(targetDir)) { fs.mkdirSync(targetDir, { recursive: true }); } const archivePath = path.join(targetDir, archiveName); const response = await fetch(downloadUrl); if (!response.ok) { throw new Error(`Failed to download XMRig: ${response.statusText}`); } const fileStream = fs.createWriteStream(archivePath); await new Promise((resolve, reject) => { response.body.pipe(fileStream); response.body.on('error', reject); fileStream.on('finish', resolve); }); if (platform === 'linux') { await tar.x({ file: archivePath, cwd: targetDir }); } else { await fs.createReadStream(archivePath) .pipe(unzipper.Extract({ path: targetDir })) .promise(); } fs.unlinkSync(archivePath); } module.exports = { downloadXmrig };