perf(cli): improve musl verification (#1660)

This commit is contained in:
Vinicius Lourenço 2023-07-25 00:26:55 -03:00 committed by GitHub
parent a7eeb0c31c
commit 3ee6be4e5f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -16,18 +16,52 @@ let nativeBinding = null
let localFileExisted = false let localFileExisted = false
let loadError = null let loadError = null
function isMusl() { const isMusl = () => {
// For Node 10 let musl = false;
if (!process.report || typeof process.report.getReport !== 'function') { if (process.platform === 'linux') {
try { musl = isMuslFromFilesystem();
const lddPath = require('child_process').execSync('which ldd').toString().trim() if (musl === null) {
return readFileSync(lddPath, 'utf8').includes('musl') musl = isMuslFromReport();
} catch (e) {
return true
} }
} else { if (musl === null) {
const { glibcVersionRuntime } = process.report.getReport().header musl = isMuslFromChildProcess();
return !glibcVersionRuntime }
}
return musl;
};
const isFileMusl = (f) => f.includes('libc.musl-') || f.includes('ld-musl-');
const isMuslFromFilesystem = () => {
try {
return readFileSync('/usr/bin/ldd', 'utf-8').includes('musl');
} catch {
return null;
}
}
const isMuslFromReport = () => {
const report = typeof process.report.getReport === 'function' ? process.report.getReport() : null;
if (!report) {
return null;
}
if (report.header && report.header.glibcVersionRuntime) {
return false;
}
if (Array.isArray(report.sharedObjects)) {
if (report.sharedObjects.some(isFileMusl)) {
return true;
}
}
return false;
};
const isMuslFromChildProcess = () => {
try {
return require('child_process').execSync('ldd --version', { encoding: 'utf8' }).includes('musl');
} catch (e) {
// If we reach this case, we don't know if the system is musl or not, so is better to just fallback to false
return false;
} }
} }