|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as util from 'util'; |
| 3 | +import * as path from 'path'; |
| 4 | +import * as os from 'os'; |
| 5 | +import {spawn} from 'child_process'; |
| 6 | +import {linuxLddDirectories, BrowserDescriptor} from '../install/browserPaths.js'; |
| 7 | + |
| 8 | +const accessAsync = util.promisify(fs.access.bind(fs)); |
| 9 | +const checkExecutable = (filePath: string) => accessAsync(filePath, fs.constants.X_OK).then(() => true).catch(e => false); |
| 10 | +const statAsync = util.promisify(fs.stat.bind(fs)); |
| 11 | +const readdirAsync = util.promisify(fs.readdir.bind(fs)); |
| 12 | + |
| 13 | +export async function validateDependencies(browserPath: string, browser: BrowserDescriptor) { |
| 14 | + // We currently only support Linux. |
| 15 | + if (os.platform() !== 'linux') |
| 16 | + return; |
| 17 | + const directoryPaths = linuxLddDirectories(browserPath, browser); |
| 18 | + const lddPaths: string[] = []; |
| 19 | + for (const directoryPath of directoryPaths) |
| 20 | + lddPaths.push(...(await executablesOrSharedLibraries(directoryPath))); |
| 21 | + const allMissingDeps = await Promise.all(lddPaths.map(lddPath => missingFileDependencies(lddPath))); |
| 22 | + const missingDeps = new Set(); |
| 23 | + for (const deps of allMissingDeps) { |
| 24 | + for (const dep of deps) |
| 25 | + missingDeps.add(dep); |
| 26 | + } |
| 27 | + if (!missingDeps.size) |
| 28 | + return; |
| 29 | + const deps = [...missingDeps].sort().map(dep => ' ' + dep).join('\n'); |
| 30 | + throw new Error('Host system is missing dependencies to run browser:\n' + deps); |
| 31 | +} |
| 32 | + |
| 33 | +async function executablesOrSharedLibraries(directoryPath: string): Promise<string[]> { |
| 34 | + const allPaths = (await readdirAsync(directoryPath)).map(file => path.resolve(directoryPath, file)); |
| 35 | + const allStats = await Promise.all(allPaths.map(aPath => statAsync(aPath))); |
| 36 | + const filePaths = allPaths.filter((aPath, index) => allStats[index].isFile()); |
| 37 | + |
| 38 | + const executablersOrLibraries = (await Promise.all(filePaths.map(async filePath => { |
| 39 | + const basename = path.basename(filePath).toLowerCase(); |
| 40 | + if (basename.endsWith('.so') || basename.includes('.so.')) |
| 41 | + return filePath; |
| 42 | + if (await checkExecutable(filePath)) |
| 43 | + return filePath; |
| 44 | + return false; |
| 45 | + }))).filter(Boolean); |
| 46 | + |
| 47 | + return executablersOrLibraries as string[]; |
| 48 | +} |
| 49 | + |
| 50 | +async function missingFileDependencies(filePath: string): Promise<Array<string>> { |
| 51 | + const {stdout} = await lddAsync(filePath); |
| 52 | + const missingDeps = stdout.split('\n').map(line => line.trim()).filter(line => line.endsWith('not found') && line.includes('=>')).map(line => line.split('=>')[0].trim()); |
| 53 | + return missingDeps; |
| 54 | +} |
| 55 | + |
| 56 | +function lddAsync(filePath: string): Promise<{stdout: string, stderr: string, code: number}> { |
| 57 | + const dirname = path.dirname(filePath); |
| 58 | + const ldd = spawn('ldd', [filePath], { |
| 59 | + cwd: dirname, |
| 60 | + env: { |
| 61 | + ...process.env, |
| 62 | + LD_LIBRARY_PATH: dirname, |
| 63 | + }, |
| 64 | + }); |
| 65 | + |
| 66 | + return new Promise((resolve) => { |
| 67 | + let stdout = ''; |
| 68 | + let stderr = ''; |
| 69 | + ldd.stdout.on('data', data => stdout += data); |
| 70 | + ldd.stderr.on('data', data => stderr += data); |
| 71 | + ldd.on('close', (code) => { |
| 72 | + resolve({stdout, stderr, code}); |
| 73 | + }); |
| 74 | + }); |
| 75 | +} |
0 commit comments