2024-01-25 16:51:06 +01:00
|
|
|
const child_process = require('child_process')
|
|
|
|
const { promisify } = require('util')
|
|
|
|
const exec = promisify(child_process.exec)
|
2023-12-05 20:19:17 +01:00
|
|
|
const path = require('path')
|
2024-07-27 20:51:31 +02:00
|
|
|
const axios = require('axios')
|
2023-12-05 20:19:17 +01:00
|
|
|
const which = require('../libs/which')
|
|
|
|
const fs = require('../libs/fsExtra')
|
|
|
|
const Logger = require('../Logger')
|
2023-12-14 08:47:18 +01:00
|
|
|
const fileUtils = require('../utils/fileUtils')
|
2024-07-27 20:51:31 +02:00
|
|
|
const StreamZip = require('../libs/nodeStreamZip')
|
2023-12-05 20:19:17 +01:00
|
|
|
|
2024-07-27 20:51:31 +02:00
|
|
|
class GithubAssetDownloader {
|
|
|
|
constructor(owner, repo) {
|
|
|
|
this.owner = owner
|
|
|
|
this.repo = repo
|
|
|
|
this.assetCache = {}
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAssetUrl(releaseTag, assetName) {
|
|
|
|
// Check if the assets information is already cached for the release tag
|
|
|
|
if (this.assetCache[releaseTag]) {
|
|
|
|
Logger.debug(`[GithubAssetDownloader] Repo ${this.repo} release ${releaseTag}: assets found in cache.`)
|
|
|
|
} else {
|
|
|
|
// Get the release information
|
|
|
|
const releaseUrl = `https://api.github.com/repos/${this.owner}/${this.repo}/releases/tags/${releaseTag}`
|
|
|
|
const releaseResponse = await axios.get(releaseUrl, {
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/vnd.github.v3+json',
|
|
|
|
'User-Agent': 'axios'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Cache the assets information for the release tag
|
|
|
|
this.assetCache[releaseTag] = releaseResponse.data.assets
|
|
|
|
Logger.debug(`[GithubAssetDownloader] Repo ${this.repo} release ${releaseTag}: assets fetched from API.`)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the asset URL
|
|
|
|
const assets = this.assetCache[releaseTag]
|
|
|
|
const asset = assets.find((asset) => asset.name === assetName)
|
|
|
|
if (!asset) {
|
|
|
|
throw new Error(`[GithubAssetDownloader] Repo ${this.repo} release ${releaseTag}: asset ${assetName} not found`)
|
|
|
|
}
|
|
|
|
|
|
|
|
return asset.browser_download_url
|
|
|
|
}
|
|
|
|
|
|
|
|
async downloadAsset(assetUrl, destDir) {
|
|
|
|
const zipPath = path.join(destDir, 'temp.zip')
|
|
|
|
const writer = fs.createWriteStream(zipPath)
|
|
|
|
|
|
|
|
const assetResponse = await axios({
|
|
|
|
url: assetUrl,
|
|
|
|
method: 'GET',
|
|
|
|
responseType: 'stream'
|
|
|
|
})
|
|
|
|
|
|
|
|
assetResponse.data.pipe(writer)
|
|
|
|
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
writer.on('finish', () => {
|
|
|
|
Logger.debug(`[GithubAssetDownloader] Downloaded asset ${assetUrl} to ${zipPath}`)
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
writer.on('error', (err) => {
|
|
|
|
Logger.error(`[GithubAssetDownloader] Error downloading asset ${assetUrl}: ${err.message}`)
|
|
|
|
reject(err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return zipPath
|
|
|
|
}
|
|
|
|
|
|
|
|
async extractFiles(zipPath, filesToExtract, destDir) {
|
|
|
|
const zip = new StreamZip.async({ file: zipPath })
|
|
|
|
|
|
|
|
for (const file of filesToExtract) {
|
|
|
|
const outputPath = path.join(destDir, file.outputFileName)
|
|
|
|
await zip.extract(file.pathInsideZip, outputPath)
|
|
|
|
Logger.debug(`[GithubAssetDownloader] Extracted file ${file.pathInsideZip} to ${outputPath}`)
|
2024-07-27 22:03:40 +02:00
|
|
|
|
|
|
|
// Set executable permission for Linux
|
|
|
|
if (process.platform !== 'win32') {
|
|
|
|
await fs.chmod(outputPath, 0o755)
|
|
|
|
}
|
2024-07-27 20:51:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
await zip.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
async downloadAndExtractFiles(releaseTag, assetName, filesToExtract, destDir) {
|
|
|
|
let zipPath
|
|
|
|
try {
|
|
|
|
await fs.ensureDir(destDir)
|
|
|
|
const assetUrl = await this.getAssetUrl(releaseTag, assetName)
|
|
|
|
zipPath = await this.downloadAsset(assetUrl, destDir)
|
|
|
|
await this.extractFiles(zipPath, filesToExtract, destDir)
|
|
|
|
} catch (error) {
|
|
|
|
Logger.error(`[GithubAssetDownloader] Error downloading or extracting files: ${error.message}`)
|
|
|
|
throw error
|
|
|
|
} finally {
|
|
|
|
if (zipPath) await fs.remove(zipPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class FFBinariesDownloader extends GithubAssetDownloader {
|
|
|
|
constructor() {
|
|
|
|
super('ffbinaries', 'ffbinaries-prebuilt')
|
|
|
|
}
|
|
|
|
|
|
|
|
getPlatformSuffix() {
|
|
|
|
const platform = process.platform
|
|
|
|
const arch = process.arch
|
|
|
|
|
|
|
|
switch (platform) {
|
|
|
|
case 'win32':
|
|
|
|
return 'win-64'
|
|
|
|
case 'darwin':
|
|
|
|
return 'macos-64'
|
|
|
|
case 'linux':
|
|
|
|
switch (arch) {
|
|
|
|
case 'x64':
|
|
|
|
return 'linux-64'
|
|
|
|
case 'x32':
|
|
|
|
case 'ia32':
|
|
|
|
return 'linux-32'
|
|
|
|
case 'arm64':
|
|
|
|
return 'linux-arm-64'
|
|
|
|
case 'arm':
|
|
|
|
return 'linux-armhf-32'
|
|
|
|
default:
|
|
|
|
throw new Error(`Unsupported architecture: ${arch}`)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
throw new Error(`Unsupported platform: ${platform}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async downloadBinary(binaryName, releaseTag, destDir) {
|
|
|
|
const platformSuffix = this.getPlatformSuffix()
|
|
|
|
const assetName = `${binaryName}-${releaseTag}-${platformSuffix}.zip`
|
|
|
|
const fileName = process.platform === 'win32' ? `${binaryName}.exe` : binaryName
|
|
|
|
const filesToExtract = [{ pathInsideZip: fileName, outputFileName: fileName }]
|
|
|
|
releaseTag = `v${releaseTag}`
|
|
|
|
|
|
|
|
await this.downloadAndExtractFiles(releaseTag, assetName, filesToExtract, destDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class SQLeanDownloader extends GithubAssetDownloader {
|
|
|
|
constructor() {
|
|
|
|
super('nalgeon', 'sqlean')
|
|
|
|
}
|
|
|
|
|
|
|
|
getPlatformSuffix() {
|
|
|
|
const platform = process.platform
|
|
|
|
const arch = process.arch
|
|
|
|
|
|
|
|
switch (platform) {
|
|
|
|
case 'win32':
|
|
|
|
return arch === 'x64' ? 'win-x64' : 'win-x86'
|
|
|
|
case 'darwin':
|
|
|
|
return arch === 'arm64' ? 'macos-arm64' : 'macos-x86'
|
|
|
|
case 'linux':
|
|
|
|
return arch === 'arm64' ? 'linux-arm64' : 'linux-x86'
|
|
|
|
default:
|
|
|
|
throw new Error(`Unsupported platform or architecture: ${platform}, ${arch}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getLibraryName(binaryName) {
|
|
|
|
const platform = process.platform
|
|
|
|
|
|
|
|
switch (platform) {
|
|
|
|
case 'win32':
|
|
|
|
return `${binaryName}.dll`
|
|
|
|
case 'darwin':
|
|
|
|
return `${binaryName}.dylib`
|
|
|
|
case 'linux':
|
|
|
|
return `${binaryName}.so`
|
|
|
|
default:
|
|
|
|
throw new Error(`Unsupported platform: ${platform}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async downloadBinary(binaryName, releaseTag, destDir) {
|
|
|
|
const platformSuffix = this.getPlatformSuffix()
|
|
|
|
const assetName = `sqlean-${platformSuffix}.zip`
|
|
|
|
const fileName = this.getLibraryName(binaryName)
|
|
|
|
const filesToExtract = [{ pathInsideZip: fileName, outputFileName: fileName }]
|
|
|
|
|
|
|
|
await this.downloadAndExtractFiles(releaseTag, assetName, filesToExtract, destDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Binary {
|
|
|
|
constructor(name, type, envVariable, validVersions, source) {
|
|
|
|
this.name = name
|
|
|
|
this.type = type
|
|
|
|
this.envVariable = envVariable
|
|
|
|
this.validVersions = validVersions
|
|
|
|
this.source = source
|
|
|
|
this.fileName = this.getFileName()
|
|
|
|
this.exec = exec
|
|
|
|
}
|
|
|
|
|
|
|
|
async find(mainInstallDir, altInstallDir) {
|
|
|
|
// 1. check path specified in environment variable
|
|
|
|
const defaultPath = process.env[this.envVariable]
|
|
|
|
if (await this.isGood(defaultPath)) return defaultPath
|
|
|
|
// 2. find the first instance of the binary in the PATH environment variable
|
|
|
|
if (this.type === 'executable') {
|
|
|
|
const whichPath = which.sync(this.fileName, { nothrow: true })
|
|
|
|
if (await this.isGood(whichPath)) return whichPath
|
|
|
|
}
|
|
|
|
// 3. check main install path (binary root dir)
|
|
|
|
const mainInstallPath = path.join(mainInstallDir, this.fileName)
|
|
|
|
if (await this.isGood(mainInstallPath)) return mainInstallPath
|
|
|
|
// 4. check alt install path (/config)
|
|
|
|
const altInstallPath = path.join(altInstallDir, this.fileName)
|
|
|
|
if (await this.isGood(altInstallPath)) return altInstallPath
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
getFileName() {
|
|
|
|
if (this.type === 'executable') {
|
|
|
|
return this.name + (process.platform == 'win32' ? '.exe' : '')
|
|
|
|
} else if (this.type === 'library') {
|
|
|
|
return this.name + (process.platform == 'win32' ? '.dll' : '.so')
|
|
|
|
} else {
|
|
|
|
return this.name
|
|
|
|
}
|
|
|
|
}
|
2023-12-05 20:19:17 +01:00
|
|
|
|
2024-07-27 20:51:31 +02:00
|
|
|
async isGood(binaryPath) {
|
|
|
|
if (!binaryPath || !(await fs.pathExists(binaryPath))) return false
|
|
|
|
if (!this.validVersions.length) return true
|
|
|
|
if (this.type === 'library') return true
|
|
|
|
try {
|
|
|
|
const { stdout } = await this.exec('"' + binaryPath + '"' + ' -version')
|
|
|
|
const version = stdout.match(/version\s([\d\.]+)/)?.[1]
|
|
|
|
if (!version) return false
|
|
|
|
return this.validVersions.some((validVersion) => version.startsWith(validVersion))
|
|
|
|
} catch (err) {
|
|
|
|
Logger.error(`[Binary] Failed to check version of ${binaryPath}`)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async download(destination) {
|
|
|
|
await this.source.downloadBinary(this.name, this.validVersions[0], destination)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const ffbinaries = new FFBinariesDownloader()
|
2024-07-27 21:44:01 +02:00
|
|
|
module.exports.ffbinaries = ffbinaries // for testing
|
2024-07-27 20:51:31 +02:00
|
|
|
const sqlean = new SQLeanDownloader()
|
2024-07-27 21:44:01 +02:00
|
|
|
module.exports.sqlean = sqlean // for testing
|
2024-07-27 20:51:31 +02:00
|
|
|
|
|
|
|
class BinaryManager {
|
2023-12-06 00:35:15 +01:00
|
|
|
defaultRequiredBinaries = [
|
2024-07-27 20:51:31 +02:00
|
|
|
new Binary('ffmpeg', 'executable', 'FFMPEG_PATH', ['5.1'], ffbinaries), // ffmpeg executable
|
|
|
|
new Binary('ffprobe', 'executable', 'FFPROBE_PATH', ['5.1'], ffbinaries), // ffprobe executable
|
|
|
|
new Binary('unicode', 'library', 'SQLEAN_UNICODE_PATH', ['0.24.2'], sqlean) // sqlean unicode extension
|
2023-12-05 20:19:17 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
constructor(requiredBinaries = this.defaultRequiredBinaries) {
|
|
|
|
this.requiredBinaries = requiredBinaries
|
2024-07-27 20:51:31 +02:00
|
|
|
this.mainInstallDir = process.pkg ? path.dirname(process.execPath) : global.appRoot
|
|
|
|
this.altInstallDir = global.ConfigPath
|
2024-02-18 00:40:33 +01:00
|
|
|
this.initialized = false
|
2023-12-05 20:19:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
2024-03-14 22:29:01 +01:00
|
|
|
// Optional skip binaries check
|
|
|
|
if (process.env.SKIP_BINARIES_CHECK === '1') {
|
2024-08-01 21:25:57 +02:00
|
|
|
for (const binary of this.requiredBinaries) {
|
|
|
|
if (!process.env[binary.envVariable]) {
|
|
|
|
await Logger.fatal(`[BinaryManager] Environment variable ${binary.envVariable} must be set`)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
}
|
2024-03-14 22:29:01 +01:00
|
|
|
Logger.info('[BinaryManager] Skipping check for binaries')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-05 20:19:17 +01:00
|
|
|
if (this.initialized) return
|
2024-03-14 22:29:01 +01:00
|
|
|
|
2023-12-05 20:19:17 +01:00
|
|
|
const missingBinaries = await this.findRequiredBinaries()
|
|
|
|
if (missingBinaries.length == 0) return
|
2024-01-25 16:51:06 +01:00
|
|
|
await this.removeOldBinaries(missingBinaries)
|
2023-12-05 20:19:17 +01:00
|
|
|
await this.install(missingBinaries)
|
|
|
|
const missingBinariesAfterInstall = await this.findRequiredBinaries()
|
2024-02-18 00:40:33 +01:00
|
|
|
if (missingBinariesAfterInstall.length) {
|
2023-12-05 20:19:17 +01:00
|
|
|
Logger.error(`[BinaryManager] Failed to find or install required binaries: ${missingBinariesAfterInstall.join(', ')}`)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
this.initialized = true
|
|
|
|
}
|
|
|
|
|
2024-07-27 20:51:31 +02:00
|
|
|
async removeBinary(destination, binary) {
|
|
|
|
const binaryPath = path.join(destination, binary.fileName)
|
|
|
|
if (await fs.pathExists(binaryPath)) {
|
|
|
|
Logger.debug(`[BinaryManager] Removing binary: ${binaryPath}`)
|
|
|
|
await fs.remove(binaryPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async removeOldBinaries(binaries) {
|
|
|
|
for (const binary of binaries) {
|
|
|
|
await this.removeBinary(this.mainInstallDir, binary)
|
|
|
|
await this.removeBinary(this.altInstallDir, binary)
|
2024-01-25 16:51:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-18 00:40:33 +01:00
|
|
|
/**
|
|
|
|
* Find required binaries and return array of binary names that are missing
|
2024-07-27 20:51:31 +02:00
|
|
|
*
|
2024-02-18 00:40:33 +01:00
|
|
|
* @returns {Promise<string[]>}
|
|
|
|
*/
|
2023-12-05 20:19:17 +01:00
|
|
|
async findRequiredBinaries() {
|
|
|
|
const missingBinaries = []
|
|
|
|
for (const binary of this.requiredBinaries) {
|
2024-07-27 20:51:31 +02:00
|
|
|
const binaryPath = await binary.find(this.mainInstallDir, this.altInstallDir)
|
2023-12-05 20:19:17 +01:00
|
|
|
if (binaryPath) {
|
2024-02-18 00:40:33 +01:00
|
|
|
Logger.info(`[BinaryManager] Found valid binary ${binary.name} at ${binaryPath}`)
|
2023-12-08 00:32:06 +01:00
|
|
|
if (process.env[binary.envVariable] !== binaryPath) {
|
|
|
|
Logger.info(`[BinaryManager] Updating process.env.${binary.envVariable}`)
|
|
|
|
process.env[binary.envVariable] = binaryPath
|
|
|
|
}
|
2023-12-05 20:19:17 +01:00
|
|
|
} else {
|
2024-01-25 16:51:06 +01:00
|
|
|
Logger.info(`[BinaryManager] ${binary.name} not found or version too old`)
|
2024-07-27 20:51:31 +02:00
|
|
|
missingBinaries.push(binary)
|
2023-12-05 20:19:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return missingBinaries
|
|
|
|
}
|
|
|
|
|
|
|
|
async install(binaries) {
|
2024-02-18 00:40:33 +01:00
|
|
|
if (!binaries.length) return
|
2024-07-27 20:51:31 +02:00
|
|
|
Logger.info(`[BinaryManager] Installing binaries: ${binaries.map((binary) => binary.name).join(', ')}`)
|
|
|
|
let destination = (await fileUtils.isWritable(this.mainInstallDir)) ? this.mainInstallDir : this.altInstallDir
|
|
|
|
for (const binary of binaries) {
|
|
|
|
await binary.download(destination)
|
|
|
|
}
|
2023-12-05 20:19:17 +01:00
|
|
|
Logger.info(`[BinaryManager] Binaries installed to ${destination}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-27 20:51:31 +02:00
|
|
|
module.exports = BinaryManager
|
|
|
|
module.exports.Binary = Binary // for testing
|