UNPKG

@camera.ui/go2rtc

Version:
110 lines 4.77 kB
#!/usr/bin/env node import axios from 'axios'; import fs from 'fs-extra'; import { dirname, extname, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; import { pipeline } from 'stream/promises'; import { Open } from 'unzipper'; import { go2rtcPath } from './index.js'; import { getArchitecture, getPlatform } from './utils.js'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); const GO2RTC_VERSION = 'v1.9.9-cui.34'; const binaries = { darwin: { x64: `go2rtc-darwin-x64-${GO2RTC_VERSION.replace('v', '')}.zip`, arm64: `go2rtc-darwin-arm64-${GO2RTC_VERSION.replace('v', '')}.zip`, }, linux: { x64: `go2rtc-linux-x64-${GO2RTC_VERSION.replace('v', '')}.zip`, arm6: `go2rtc-linux-armv6-${GO2RTC_VERSION.replace('v', '')}.zip`, arm7: `go2rtc-linux-armv7-${GO2RTC_VERSION.replace('v', '')}.zip`, arm64: `go2rtc-linux-arm64-${GO2RTC_VERSION.replace('v', '')}.zip`, ia32: `go2rtc-linux-x86-${GO2RTC_VERSION.replace('v', '')}.zip`, mipsel: `go2rtc-linux-mipsel-${GO2RTC_VERSION.replace('v', '')}.zip`, }, win32: { x64: `go2rtc-windows-x64-${GO2RTC_VERSION.replace('v', '')}.zip`, arm64: `go2rtc-windows-arm64-${GO2RTC_VERSION.replace('v', '')}.zip`, ia32: `go2rtc-windows-x86-${GO2RTC_VERSION.replace('v', '')}.zip`, }, freebsd: { arm64: `go2rtc-freebsd-arm64-${GO2RTC_VERSION.replace('v', '')}.zip`, x64: `go2rtc-freebsd-x64-${GO2RTC_VERSION.replace('v', '')}.zip`, }, }; const arch = getArchitecture(); const sysPlatform = getPlatform(); const filename = binaries[sysPlatform]?.[arch]; const releasesUrl = 'https://api.github.com/repos/seydx/go2rtc/releases'; console.log(`Detected platform: ${sysPlatform} / ${arch}`); const go2rtcBinaryPath = resolve(__dirname, '../binary'); fs.ensureDirSync(go2rtcBinaryPath); const go2rtcFilePath = resolve(go2rtcBinaryPath, filename); const go2rtcExtractedFilePath = go2rtcPath(); const isZipUrl = (url) => { const pathArray = new URL(url).pathname.split('/'); const fileName = pathArray[pathArray.length - 1]; return fileName !== undefined && extname(fileName) === '.zip'; }; const downloadFile = async (url) => { console.log(`Downloading Go2Rtc ${GO2RTC_VERSION} to ${go2rtcFilePath}...`); const { data } = await axios.get(url, { onDownloadProgress: (progressEvent) => { if (process.env.CI || !progressEvent.total) { return; } const percent = Math.round((progressEvent.loaded / progressEvent.total) * 100) + '%'; process.stdout.write('\r' + percent); if (progressEvent.loaded === progressEvent.total) { process.stdout.write('\r'); } }, timeout: 30 * 1000, // 30s maxRedirects: 3, responseType: 'stream', }); await fs.ensureDir(go2rtcBinaryPath); const streams = [data, fs.createWriteStream(go2rtcFilePath)]; await pipeline(streams); if (isZipUrl(url)) { console.log(`Extracting ${go2rtcFilePath} to ${go2rtcExtractedFilePath}...`); const directory = await Open.file(go2rtcFilePath); await new Promise((resolve, reject) => { directory.files[0].stream().pipe(fs.createWriteStream(go2rtcExtractedFilePath)).on('error', reject).on('finish', resolve); }); console.log(`Removing ${go2rtcFilePath}...`); await fs.remove(go2rtcFilePath); } else { console.log(`Renaming ${go2rtcFilePath} to ${go2rtcExtractedFilePath}...`); await fs.rename(go2rtcFilePath, go2rtcExtractedFilePath); } }; const getReleaseAssets = async (version) => { const response = await axios.get(`${releasesUrl}/tags/${version}`); const assets = response.data.assets.map((asset) => asset.browser_download_url); const files = assets.map((asset) => asset.split(`${version}/`)[1]); return { assets, files, }; }; const downloadGo2Rtc = async () => { const release = await getReleaseAssets(GO2RTC_VERSION); if (!filename || !release.assets.find((r) => r.endsWith(filename))) { throw new Error(`No go2rtc binary found for architecture (${sysPlatform} / ${arch})`); } const downloadUrl = release.assets.find((r) => r.endsWith(filename)); await downloadFile(downloadUrl); if (sysPlatform === 'linux' || sysPlatform === 'darwin') { console.log(`Making ${go2rtcExtractedFilePath} executable...`); fs.chmodSync(go2rtcExtractedFilePath, 0o755); } console.log('Done!'); }; downloadGo2Rtc().catch((error) => { console.error('Error downloading Go2Rtc:', error); process.exit(1); }); //# sourceMappingURL=install.js.map