1 line
20 KiB
JSON
1 line
20 KiB
JSON
|
{"ast":null,"code":"// Copyright 2017 Lovell Fuller and others.\n// SPDX-License-Identifier: Apache-2.0\n\n'use strict';\n\nconst childProcess = require('child_process');\nconst {\n isLinux,\n getReport\n} = require('./process');\nconst {\n LDD_PATH,\n readFile,\n readFileSync\n} = require('./filesystem');\nlet cachedFamilyFilesystem;\nlet cachedVersionFilesystem;\nconst command = 'getconf GNU_LIBC_VERSION 2>&1 || true; ldd --version 2>&1 || true';\nlet commandOut = '';\nconst safeCommand = () => {\n if (!commandOut) {\n return new Promise(resolve => {\n childProcess.exec(command, (err, out) => {\n commandOut = err ? ' ' : out;\n resolve(commandOut);\n });\n });\n }\n return commandOut;\n};\nconst safeCommandSync = () => {\n if (!commandOut) {\n try {\n commandOut = childProcess.execSync(command, {\n encoding: 'utf8'\n });\n } catch (_err) {\n commandOut = ' ';\n }\n }\n return commandOut;\n};\n\n/**\n * A String constant containing the value `glibc`.\n * @type {string}\n * @public\n */\nconst GLIBC = 'glibc';\n\n/**\n * A Regexp constant to get the GLIBC Version.\n * @type {string}\n */\nconst RE_GLIBC_VERSION = /GLIBC\\s(\\d+\\.\\d+)/;\n\n/**\n * A String constant containing the value `musl`.\n * @type {string}\n * @public\n */\nconst MUSL = 'musl';\n\n/**\n * This string is used to find if the {@link LDD_PATH} is GLIBC\n * @type {string}\n */\nconst GLIBC_ON_LDD = GLIBC.toUpperCase();\n\n/**\n * This string is used to find if the {@link LDD_PATH} is musl\n * @type {string}\n */\nconst MUSL_ON_LDD = MUSL.toLowerCase();\nconst isFileMusl = f => f.includes('libc.musl-') || f.includes('ld-musl-');\nconst familyFromReport = () => {\n const report = getReport();\n if (report.header && report.header.glibcVersionRuntime) {\n return GLIBC;\n }\n if (Array.isArray(report.sharedObjects)) {\n if (report.sharedObjects.some(isFileMusl)) {\n return MUSL;\n }\n }\n return null;\n};\nconst familyFromCommand = out => {\n const [getconf, ldd1] = out.split(/[\\r\\n]+/);\n if (getconf && getconf.includes(GLIBC)) {\n return GLIBC;\n }\n if (ldd1 && ldd1.includes(MUSL)) {\n return MUSL;\n }\n return null;\n};\nconst getFamilyFromLddContent = content => {\n if (content.includes(MUSL_ON_LDD)) {\n return MUSL;\n }\n if (content.includes(GLIBC_ON_LDD)) {\n return GLIBC;\n }\n return null;\n};\nconst familyFromFilesystem = async () => {\n if (cachedFamilyFilesystem !== undefined) {\n return cachedFamilyFilesystem;\n }\n cachedFamilyFilesystem = null;\n try {\n const lddContent = await readFile(LDD_PATH);\n cachedFamilyFilesystem = getFamilyFromLddContent(lddContent);\n } catch (e) {}\n return cachedFamilyFilesystem;\n};\nconst familyFromFilesystemSync = () => {\n if (cachedFamilyFilesystem !== undefined) {\n return cachedFamilyFilesystem;\n }\n cachedFamilyFilesystem = null;\n try {\n const lddContent = readFileSync(LDD_PATH);\n cachedFamilyFilesystem = getFamilyFromLddContent(lddContent);\n } catch (e) {}\n return cachedFamilyFilesystem;\n};\n\n/**\n * Resolves with the libc family when it can be determined, `null` otherwise.\n * @returns {Promise<?string>}\n */\nconst family = async () => {\n let family = null;\n if (isLinux()) {\n family = await familyFromFilesystem();\n if (!family) {\n family = familyFromReport();\n }\n if (!family) {\n const out = await safeCommand();\n family = familyFromCommand(out);\n }\n }\n return family;\n};\n\n/**\n * Returns the libc family when it can be determined, `null` otherwise.\n * @returns {?string}\n */\nconst familySync = () => {\n let family = null;\n if (isLinux()) {\n family = familyFromFilesystemSync();\n if (!family) {\n family = familyFromReport();\n }\n if (!family) {\n const out = safeCommandSync();\n family = familyFromCommand(out);\n }\n }\n return family;\n};\n\n/**\n * Resolves `true` only when the platform is Linux and the libc family is not `glibc`.\n * @returns {Promise<boolean>}\
|