tdlib-native
Version:
🚀 Telegram TDLib native nodejs wrapper
217 lines (216 loc) • 6.02 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require$$0 = require("child_process");
const process = require("./process.js");
const filesystem = require("./filesystem.js");
var detectLibc;
var hasRequiredDetectLibc;
function requireDetectLibc() {
if (hasRequiredDetectLibc) return detectLibc;
hasRequiredDetectLibc = 1;
const childProcess = require$$0;
const { isLinux, getReport } = process.__require();
const { LDD_PATH, readFile, readFileSync } = filesystem.__require();
let cachedFamilyFilesystem;
let cachedVersionFilesystem;
const command = "getconf GNU_LIBC_VERSION 2>&1 || true; ldd --version 2>&1 || true";
let commandOut = "";
const safeCommand = () => {
if (!commandOut) {
return new Promise((resolve) => {
childProcess.exec(command, (err, out) => {
commandOut = err ? " " : out;
resolve(commandOut);
});
});
}
return commandOut;
};
const safeCommandSync = () => {
if (!commandOut) {
try {
commandOut = childProcess.execSync(command, { encoding: "utf8" });
} catch (_err) {
commandOut = " ";
}
}
return commandOut;
};
const GLIBC = "glibc";
const RE_GLIBC_VERSION = /LIBC[a-z0-9 \-).]*?(\d+\.\d+)/i;
const MUSL = "musl";
const isFileMusl = (f) => f.includes("libc.musl-") || f.includes("ld-musl-");
const familyFromReport = () => {
const report = getReport();
if (report.header && report.header.glibcVersionRuntime) {
return GLIBC;
}
if (Array.isArray(report.sharedObjects)) {
if (report.sharedObjects.some(isFileMusl)) {
return MUSL;
}
}
return null;
};
const familyFromCommand = (out) => {
const [getconf, ldd1] = out.split(/[\r\n]+/);
if (getconf && getconf.includes(GLIBC)) {
return GLIBC;
}
if (ldd1 && ldd1.includes(MUSL)) {
return MUSL;
}
return null;
};
const getFamilyFromLddContent = (content) => {
if (content.includes("musl")) {
return MUSL;
}
if (content.includes("GNU C Library")) {
return GLIBC;
}
return null;
};
const familyFromFilesystem = async () => {
if (cachedFamilyFilesystem !== void 0) {
return cachedFamilyFilesystem;
}
cachedFamilyFilesystem = null;
try {
const lddContent = await readFile(LDD_PATH);
cachedFamilyFilesystem = getFamilyFromLddContent(lddContent);
} catch (e) {
}
return cachedFamilyFilesystem;
};
const familyFromFilesystemSync = () => {
if (cachedFamilyFilesystem !== void 0) {
return cachedFamilyFilesystem;
}
cachedFamilyFilesystem = null;
try {
const lddContent = readFileSync(LDD_PATH);
cachedFamilyFilesystem = getFamilyFromLddContent(lddContent);
} catch (e) {
}
return cachedFamilyFilesystem;
};
const family = async () => {
let family2 = null;
if (isLinux()) {
family2 = await familyFromFilesystem();
if (!family2) {
family2 = familyFromReport();
}
if (!family2) {
const out = await safeCommand();
family2 = familyFromCommand(out);
}
}
return family2;
};
const familySync = () => {
let family2 = null;
if (isLinux()) {
family2 = familyFromFilesystemSync();
if (!family2) {
family2 = familyFromReport();
}
if (!family2) {
const out = safeCommandSync();
family2 = familyFromCommand(out);
}
}
return family2;
};
const isNonGlibcLinux = async () => isLinux() && await family() !== GLIBC;
const isNonGlibcLinuxSync = () => isLinux() && familySync() !== GLIBC;
const versionFromFilesystem = async () => {
if (cachedVersionFilesystem !== void 0) {
return cachedVersionFilesystem;
}
cachedVersionFilesystem = null;
try {
const lddContent = await readFile(LDD_PATH);
const versionMatch = lddContent.match(RE_GLIBC_VERSION);
if (versionMatch) {
cachedVersionFilesystem = versionMatch[1];
}
} catch (e) {
}
return cachedVersionFilesystem;
};
const versionFromFilesystemSync = () => {
if (cachedVersionFilesystem !== void 0) {
return cachedVersionFilesystem;
}
cachedVersionFilesystem = null;
try {
const lddContent = readFileSync(LDD_PATH);
const versionMatch = lddContent.match(RE_GLIBC_VERSION);
if (versionMatch) {
cachedVersionFilesystem = versionMatch[1];
}
} catch (e) {
}
return cachedVersionFilesystem;
};
const versionFromReport = () => {
const report = getReport();
if (report.header && report.header.glibcVersionRuntime) {
return report.header.glibcVersionRuntime;
}
return null;
};
const versionSuffix = (s) => s.trim().split(/\s+/)[1];
const versionFromCommand = (out) => {
const [getconf, ldd1, ldd2] = out.split(/[\r\n]+/);
if (getconf && getconf.includes(GLIBC)) {
return versionSuffix(getconf);
}
if (ldd1 && ldd2 && ldd1.includes(MUSL)) {
return versionSuffix(ldd2);
}
return null;
};
const version = async () => {
let version2 = null;
if (isLinux()) {
version2 = await versionFromFilesystem();
if (!version2) {
version2 = versionFromReport();
}
if (!version2) {
const out = await safeCommand();
version2 = versionFromCommand(out);
}
}
return version2;
};
const versionSync = () => {
let version2 = null;
if (isLinux()) {
version2 = versionFromFilesystemSync();
if (!version2) {
version2 = versionFromReport();
}
if (!version2) {
const out = safeCommandSync();
version2 = versionFromCommand(out);
}
}
return version2;
};
detectLibc = {
GLIBC,
MUSL,
family,
familySync,
isNonGlibcLinux,
isNonGlibcLinuxSync,
version,
versionSync
};
return detectLibc;
}
exports.__require = requireDetectLibc;