pax_global_header00006660000000000000000000000064145762601770014531gustar00rootroot0000000000000052 comment=db8ea62694a6bd274366c2b4d982b13c074537a3 detect-libc-2.0.3/000077500000000000000000000000001457626017700137125ustar00rootroot00000000000000detect-libc-2.0.3/.github/000077500000000000000000000000001457626017700152525ustar00rootroot00000000000000detect-libc-2.0.3/.github/workflows/000077500000000000000000000000001457626017700173075ustar00rootroot00000000000000detect-libc-2.0.3/.github/workflows/integration.yml000066400000000000000000000156031457626017700223620ustar00rootroot00000000000000name: Integration tests on: - push - pull_request jobs: integration: name: ${{ matrix.name }} container: ${{ matrix.container }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: include: - name: Alpine 3.11 / Node.js 8 container: node:8-alpine3.11 node-version: 8 expected-family: musl expected-version: 1.1.24 - name: Alpine 3.11 / Node.js 16 container: node:16-alpine3.11 node-version: 16 expected-family: musl expected-version: 1.1.24 - name: Alpine 3.12 / Node.js 12 container: node:12-alpine3.12 node-version: 12 expected-family: musl expected-version: 1.1.24 - name: Alpine 3.15 / Node.js 16 container: node:16-alpine3.15 node-version: 16 expected-family: musl expected-version: 1.2.2 - name: Alpine 3.18 / Node.js 20 container: node:20-alpine3.18 node-version: 20 expected-family: musl expected-version: 1.2.4 - name: Amazon 2 / Node.js 16 container: amazonlinux:2 node-version: 16 expected-family: glibc expected-version: 2.26 - name: Amazon 2023 / Node.js 20 container: amazonlinux:2023 node-version: 20 expected-family: glibc expected-version: 2.34 - name: CentOS 7 / Node.js 8 container: centos:centos7 node-version: 8 expected-family: glibc expected-version: 2.17 - name: CentOS 7 / Node.js 16 container: centos:centos7 node-version: 16 expected-family: glibc expected-version: 2.17 - name: CentOS 8 / Node.js 10 container: quay.io/centos/centos:stream8 node-version: 10 expected-family: glibc expected-version: 2.28 - name: CentOS 8 / Node.js 16 container: quay.io/centos/centos:stream8 node-version: 16 expected-family: glibc expected-version: 2.28 - name: Debian 9 / Node.js 8 container: node:8-stretch-slim node-version: 8 expected-family: glibc expected-version: 2.24 - name: Debian 9 / Node.js 16 container: node:16-stretch-slim node-version: 16 expected-family: glibc expected-version: 2.24 - name: Debian 10 / Node.js 8 container: node:8-buster-slim node-version: 8 expected-family: glibc expected-version: 2.28 - name: Debian 11 / Node.js 16 container: node:16-bullseye-slim node-version: 16 expected-family: glibc expected-version: 2.31 - name: Debian 12 / Node.js 20 container: node:20-bookworm-slim node-version: 20 expected-family: glibc expected-version: 2.36 - name: Fedora 33 / Node.js 10 container: fedora:33 node-version: 10 expected-family: glibc expected-version: 2.32 - name: Fedora 33 / Node.js 16 container: fedora:33 node-version: 16 expected-family: glibc expected-version: 2.32 - name: Fedora 35 / Node.js 12 container: fedora:35 node-version: 12 expected-family: glibc expected-version: 2.34 - name: Fedora 35 / Node.js 16 container: fedora:35 node-version: 16 expected-family: glibc expected-version: 2.34 - name: Fedora 39 / Node.js 18 container: fedora:39 node-version: 18 expected-family: glibc expected-version: 2.38 - name: OpenSUSE 15.1 / Node.js 10 container: opensuse/leap:15.1 node-version: 10 expected-family: glibc expected-version: 2.26 - name: OpenSUSE 15.3 / Node.js 14 container: opensuse/leap:15.3 node-version: 14 expected-family: glibc expected-version: 2.31 - name: Ubuntu 14.04 / Node.js 8 container: ubuntu:14.04 node-version: 8 expected-family: glibc expected-version: 2.19 - name: Ubuntu 18.04 / Node.js 8 container: ubuntu:18.04 node-version: 8 expected-family: glibc expected-version: 2.27 - name: Ubuntu 22.04 / Node.js 18 container: ubuntu:22.04 node-version: 18 expected-family: glibc expected-version: 2.35 - name: Void glibc / Node.js 16 container: ghcr.io/void-linux/void-linux:latest-thin-x86_64 node-version: 16 expected-family: glibc expected-version: 2.39 - name: Void musl / Node.js 16 container: ghcr.io/void-linux/void-linux:latest-thin-x86_64-musl node-version: 16 expected-family: musl expected-version: 1.1.24 steps: - name: Install Node.js (RHEL) if: contains(matrix.container, 'amazonlinux') || contains(matrix.container, 'centos') || contains(matrix.container, 'fedora:33') || contains(matrix.container, 'fedora:35') run: | yum module disable -y nodejs || true curl -sL https://rpm.nodesource.com/setup_${{ matrix.node-version }}.x | bash - yum install -y git nodejs - name: Install Node.js (Fedora 39) if: contains(matrix.container, 'fedora:39') run: dnf install -y git nodejs - name: Install Node.js (OpenSUSE) if: contains(matrix.container, 'opensuse') run: | zypper refresh zypper --non-interactive install git nodejs${{ matrix.node-version }} - name: Install Node.js (Ubuntu) if: contains(matrix.container, 'ubuntu') run: | apt-get update apt-get install -y curl git gnupg curl -sL https://deb.nodesource.com/gpgkey/nodesource.gpg.key | apt-key add - curl -sL https://deb.nodesource.com/setup_${{ matrix.node-version }}.x | bash - apt-get install -y nodejs - name: Install Node.js (Void) if: contains(matrix.container, 'void') run: | xbps-install -Suy xbps xbps-install -Sy git nodejs - name: Checkout uses: actions/checkout@v1 - name: Verify expectations run: | export EXPECTED="${{ matrix.expected-family }} ${{ matrix.expected-version }}" export ACTUAL="$(node ./test/integration.js)" echo "Expected: $EXPECTED" echo "Actual: $ACTUAL" test "$EXPECTED" = "$ACTUAL" detect-libc-2.0.3/.github/workflows/unit.yml000066400000000000000000000011071457626017700210100ustar00rootroot00000000000000name: Unit tests on: - push - pull_request jobs: unit: name: Node.js ${{ matrix.node-version }} runs-on: ubuntu-latest strategy: matrix: node-version: - 10 - 12 - 14 - 16 - 18 - 20 steps: - name: Checkout uses: actions/checkout@v4 - name: Install Node.js uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} - name: Install dependencies run: npm install - name: Run unit tests run: npm test detect-libc-2.0.3/.gitignore000066400000000000000000000000671457626017700157050ustar00rootroot00000000000000.nyc_output/ coverage/ node_modules/ package-lock.json detect-libc-2.0.3/LICENSE000066400000000000000000000261351457626017700147260ustar00rootroot00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright {yyyy} {name of copyright owner} Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. detect-libc-2.0.3/README.md000066400000000000000000000062171457626017700151770ustar00rootroot00000000000000# detect-libc Node.js module to detect details of the C standard library (libc) implementation provided by a given Linux system. Currently supports detection of GNU glibc and MUSL libc. Provides asychronous and synchronous functions for the family (e.g. `glibc`, `musl`) and version (e.g. `1.23`, `1.2.3`). The version numbers of libc implementations are not guaranteed to be semver-compliant. For previous v1.x releases, please see the [v1](https://github.com/lovell/detect-libc/tree/v1) branch. ## Install ```sh npm install detect-libc ``` ## API ### GLIBC ```ts const GLIBC: string = 'glibc'; ``` A String constant containing the value `glibc`. ### MUSL ```ts const MUSL: string = 'musl'; ``` A String constant containing the value `musl`. ### family ```ts function family(): Promise; ``` Resolves asychronously with: * `glibc` or `musl` when the libc family can be determined * `null` when the libc family cannot be determined * `null` when run on a non-Linux platform ```js const { family, GLIBC, MUSL } = require('detect-libc'); switch (await family()) { case GLIBC: ... case MUSL: ... case null: ... } ``` ### familySync ```ts function familySync(): string | null; ``` Synchronous version of `family()`. ```js const { familySync, GLIBC, MUSL } = require('detect-libc'); switch (familySync()) { case GLIBC: ... case MUSL: ... case null: ... } ``` ### version ```ts function version(): Promise; ``` Resolves asychronously with: * The version when it can be determined * `null` when the libc family cannot be determined * `null` when run on a non-Linux platform ```js const { version } = require('detect-libc'); const v = await version(); if (v) { const [major, minor, patch] = v.split('.'); } ``` ### versionSync ```ts function versionSync(): string | null; ``` Synchronous version of `version()`. ```js const { versionSync } = require('detect-libc'); const v = versionSync(); if (v) { const [major, minor, patch] = v.split('.'); } ``` ### isNonGlibcLinux ```ts function isNonGlibcLinux(): Promise; ``` Resolves asychronously with: * `false` when the libc family is `glibc` * `true` when the libc family is not `glibc` * `false` when run on a non-Linux platform ```js const { isNonGlibcLinux } = require('detect-libc'); if (await isNonGlibcLinux()) { ... } ``` ### isNonGlibcLinuxSync ```ts function isNonGlibcLinuxSync(): boolean; ``` Synchronous version of `isNonGlibcLinux()`. ```js const { isNonGlibcLinuxSync } = require('detect-libc'); if (isNonGlibcLinuxSync()) { ... } ``` ## Licensing Copyright 2017 Lovell Fuller and others. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0.html) Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. detect-libc-2.0.3/benchmark/000077500000000000000000000000001457626017700156445ustar00rootroot00000000000000detect-libc-2.0.3/benchmark/call-familySync.js000066400000000000000000000003101457626017700212230ustar00rootroot00000000000000const performance = require('perf_hooks').performance; const libc = require('..'); const now = performance.now(); libc.familySync(); console.log(`[family] Time Spent ${performance.now() - now}ms`); detect-libc-2.0.3/benchmark/call-isNonGlibcLinuxSync.js000066400000000000000000000003321457626017700230150ustar00rootroot00000000000000const performance = require('perf_hooks').performance; const libc = require('..'); const now = performance.now(); libc.isNonGlibcLinuxSync(); console.log(`[isNonGlibcLinux] Time Spent ${performance.now() - now}ms`); detect-libc-2.0.3/benchmark/call-versionSync.js000066400000000000000000000003161457626017700214350ustar00rootroot00000000000000const performance = require('perf_hooks').performance; const libc = require('..'); const now = performance.now(); libc.versionSync(); console.log(`[versionSync] Time Spent ${performance.now() - now}ms`); detect-libc-2.0.3/benchmark/detect-libc.js000066400000000000000000000014311457626017700203600ustar00rootroot00000000000000const Benchmark = require('benchmark'); const suite = new Benchmark.Suite(); const libc = require('../'); suite.add('family', async function () { await libc.family(); }); suite.add('familySync', function () { libc.familySync(); }); suite.add('version', async function () { await libc.version(); }); suite.add('versionSync', function () { libc.versionSync(); }); suite.add('isNonGlibcLinux', async function () { await libc.isNonGlibcLinux(); }); suite.add('isNonGlibcLinuxSync', function () { libc.isNonGlibcLinuxSync(); }); suite // add listeners .on('cycle', function (event) { console.log(String(event.target)); }) .on('complete', function () { console.log('Fastest operation is ' + this.filter('fastest').map('name')); }) .run({ async: true }); detect-libc-2.0.3/index.d.ts000066400000000000000000000006641457626017700156210ustar00rootroot00000000000000// Copyright 2017 Lovell Fuller and others. // SPDX-License-Identifier: Apache-2.0 export const GLIBC: 'glibc'; export const MUSL: 'musl'; export function family(): Promise; export function familySync(): string | null; export function isNonGlibcLinux(): Promise; export function isNonGlibcLinuxSync(): boolean; export function version(): Promise; export function versionSync(): string | null; detect-libc-2.0.3/lib/000077500000000000000000000000001457626017700144605ustar00rootroot00000000000000detect-libc-2.0.3/lib/detect-libc.js000066400000000000000000000141271457626017700172020ustar00rootroot00000000000000// Copyright 2017 Lovell Fuller and others. // SPDX-License-Identifier: Apache-2.0 'use strict'; const childProcess = require('child_process'); const { isLinux, getReport } = require('./process'); const { LDD_PATH, readFile, readFileSync } = require('./filesystem'); 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; }; /** * A String constant containing the value `glibc`. * @type {string} * @public */ const GLIBC = 'glibc'; /** * A Regexp constant to get the GLIBC Version. * @type {string} */ const RE_GLIBC_VERSION = /LIBC[a-z0-9 \-).]*?(\d+\.\d+)/i; /** * A String constant containing the value `musl`. * @type {string} * @public */ 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 !== undefined) { return cachedFamilyFilesystem; } cachedFamilyFilesystem = null; try { const lddContent = await readFile(LDD_PATH); cachedFamilyFilesystem = getFamilyFromLddContent(lddContent); } catch (e) {} return cachedFamilyFilesystem; }; const familyFromFilesystemSync = () => { if (cachedFamilyFilesystem !== undefined) { return cachedFamilyFilesystem; } cachedFamilyFilesystem = null; try { const lddContent = readFileSync(LDD_PATH); cachedFamilyFilesystem = getFamilyFromLddContent(lddContent); } catch (e) {} return cachedFamilyFilesystem; }; /** * Resolves with the libc family when it can be determined, `null` otherwise. * @returns {Promise} */ const family = async () => { let family = null; if (isLinux()) { family = await familyFromFilesystem(); if (!family) { family = familyFromReport(); } if (!family) { const out = await safeCommand(); family = familyFromCommand(out); } } return family; }; /** * Returns the libc family when it can be determined, `null` otherwise. * @returns {?string} */ const familySync = () => { let family = null; if (isLinux()) { family = familyFromFilesystemSync(); if (!family) { family = familyFromReport(); } if (!family) { const out = safeCommandSync(); family = familyFromCommand(out); } } return family; }; /** * Resolves `true` only when the platform is Linux and the libc family is not `glibc`. * @returns {Promise} */ const isNonGlibcLinux = async () => isLinux() && await family() !== GLIBC; /** * Returns `true` only when the platform is Linux and the libc family is not `glibc`. * @returns {boolean} */ const isNonGlibcLinuxSync = () => isLinux() && familySync() !== GLIBC; const versionFromFilesystem = async () => { if (cachedVersionFilesystem !== undefined) { 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 !== undefined) { 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; }; /** * Resolves with the libc version when it can be determined, `null` otherwise. * @returns {Promise} */ const version = async () => { let version = null; if (isLinux()) { version = await versionFromFilesystem(); if (!version) { version = versionFromReport(); } if (!version) { const out = await safeCommand(); version = versionFromCommand(out); } } return version; }; /** * Returns the libc version when it can be determined, `null` otherwise. * @returns {?string} */ const versionSync = () => { let version = null; if (isLinux()) { version = versionFromFilesystemSync(); if (!version) { version = versionFromReport(); } if (!version) { const out = safeCommandSync(); version = versionFromCommand(out); } } return version; }; module.exports = { GLIBC, MUSL, family, familySync, isNonGlibcLinux, isNonGlibcLinuxSync, version, versionSync }; detect-libc-2.0.3/lib/filesystem.js000066400000000000000000000013151457626017700172020ustar00rootroot00000000000000// Copyright 2017 Lovell Fuller and others. // SPDX-License-Identifier: Apache-2.0 'use strict'; const fs = require('fs'); /** * The path where we can find the ldd */ const LDD_PATH = '/usr/bin/ldd'; /** * Read the content of a file synchronous * * @param {string} path * @returns {string} */ const readFileSync = (path) => fs.readFileSync(path, 'utf-8'); /** * Read the content of a file * * @param {string} path * @returns {Promise} */ const readFile = (path) => new Promise((resolve, reject) => { fs.readFile(path, 'utf-8', (err, data) => { if (err) { reject(err); } else { resolve(data); } }); }); module.exports = { LDD_PATH, readFileSync, readFile }; detect-libc-2.0.3/lib/process.js000066400000000000000000000010711457626017700164730ustar00rootroot00000000000000// Copyright 2017 Lovell Fuller and others. // SPDX-License-Identifier: Apache-2.0 'use strict'; const isLinux = () => process.platform === 'linux'; let report = null; const getReport = () => { if (!report) { /* istanbul ignore next */ if (isLinux() && process.report) { const orig = process.report.excludeNetwork; process.report.excludeNetwork = true; report = process.report.getReport(); process.report.excludeNetwork = orig; } else { report = {}; } } return report; }; module.exports = { isLinux, getReport }; detect-libc-2.0.3/package.json000066400000000000000000000021161457626017700162000ustar00rootroot00000000000000{ "name": "detect-libc", "version": "2.0.3", "description": "Node.js module to detect the C standard library (libc) implementation family and version", "main": "lib/detect-libc.js", "files": [ "lib/", "index.d.ts" ], "scripts": { "test": "semistandard && nyc --reporter=text --check-coverage --branches=100 ava test/unit.js", "bench": "node benchmark/detect-libc", "bench:calls": "node benchmark/call-familySync.js && sleep 1 && node benchmark/call-isNonGlibcLinuxSync.js && sleep 1 && node benchmark/call-versionSync.js" }, "repository": { "type": "git", "url": "git://github.com/lovell/detect-libc" }, "keywords": [ "libc", "glibc", "musl" ], "author": "Lovell Fuller ", "contributors": [ "Niklas Salmoukas ", "Vinícius Lourenço " ], "license": "Apache-2.0", "devDependencies": { "ava": "^2.4.0", "benchmark": "^2.1.4", "nyc": "^15.1.0", "proxyquire": "^2.1.3", "semistandard": "^14.2.3" }, "engines": { "node": ">=8" } } detect-libc-2.0.3/test/000077500000000000000000000000001457626017700146715ustar00rootroot00000000000000detect-libc-2.0.3/test/integration.js000066400000000000000000000004561457626017700175570ustar00rootroot00000000000000// Copyright 2017 Lovell Fuller and others. // SPDX-License-Identifier: Apache-2.0 'use strict'; const { familySync, versionSync } = require('../'); const family = familySync() || 'unknown-family'; const version = versionSync() || 'unknown-version'; process.stdout.write(`${family} ${version}\n`); detect-libc-2.0.3/test/test-fixture.txt000066400000000000000000000000011457626017700200640ustar00rootroot000000000000001detect-libc-2.0.3/test/unit.js000066400000000000000000000373401457626017700162150ustar00rootroot00000000000000// Copyright 2017 Lovell Fuller and others. // SPDX-License-Identifier: Apache-2.0 'use strict'; const test = require('ava'); const path = require('path'); const proxyquire = require('proxyquire') .noCallThru() .noPreserveCache(); const filePermissionError = new Error('Read error'); filePermissionError.code = 'ERR_ACCESS_DENIED'; test('filesystem - file found', async (t) => { t.plan(2); const filesystem = require('../lib/filesystem'); const notExistFile = path.join(__dirname, './non-exist.txt'); try { await filesystem.readFile(notExistFile); } catch (e) { t.true(e instanceof Error); } try { filesystem.readFileSync(notExistFile); } catch (e) { t.true(e instanceof Error); } }); test('filesystem - file not found', async (t) => { t.plan(2); const filesystem = require('../lib/filesystem'); const testFixtureFilePath = path.join(__dirname, './test-fixture.txt'); t.is(await filesystem.readFile(testFixtureFilePath), '1'); t.is(filesystem.readFileSync(testFixtureFilePath), '1'); }); test('constants', (t) => { t.plan(2); const libc = require('../'); t.is(libc.GLIBC, 'glibc'); t.is(libc.MUSL, 'musl'); }); test('linux - glibc family detected via ldd', async (t) => { t.plan(2); const libc = proxyquire('../', { './process': { isLinux: () => true }, './filesystem': { readFile: () => Promise.resolve('# This file is part of the GNU C Library.') } }); t.is(await libc.family(), libc.GLIBC); t.false(await libc.isNonGlibcLinux()); }); test('linux - glibc familySync detected via ldd', async (t) => { t.plan(2); const libc = proxyquire('../', { './process': { isLinux: () => true }, './filesystem': { readFileSync: () => '# The GNU C Library is free software; you can redistribute it and/or' } }); t.is(libc.familySync(), libc.GLIBC); t.false(libc.isNonGlibcLinuxSync()); }); test('linux - glibc family detected via ldd on error fallback', async (t) => { t.plan(2); const libc = proxyquire('../', { './process': { isLinux: () => true, getReport: () => ({ header: { glibcVersionRuntime: '1.23' } }) }, './filesystem': { readFile: () => Promise.reject(filePermissionError) } }); t.is(await libc.family(), libc.GLIBC); t.false(await libc.isNonGlibcLinux()); }); test('linux - glibc familySync detected via ldd on error fallback', async (t) => { t.plan(2); const libc = proxyquire('../', { './process': { isLinux: () => true, getReport: () => ({ header: { glibcVersionRuntime: '1.23' } }) }, './filesystem': { readFileSync: () => { throw filePermissionError; } } }); t.is(libc.familySync(), libc.GLIBC); t.false(libc.isNonGlibcLinuxSync()); }); test('linux - glibc family and version detected via report', async (t) => { t.plan(3); const libc = proxyquire('../', { './process': { isLinux: () => true, getReport: () => ({ header: { glibcVersionRuntime: '1.23' } }) }, './filesystem': { readFile: () => Promise.resolve('bunch-of-text') } }); t.is(await libc.family(), libc.GLIBC); t.is(await libc.version(), '1.23'); t.false(await libc.isNonGlibcLinux()); }); test('linux - glibc familySync and version detected via report', async (t) => { t.plan(3); const libc = proxyquire('../', { './process': { isLinux: () => true, getReport: () => ({ header: { glibcVersionRuntime: '1.23' } }) }, './filesystem': { readFileSync: () => 'bunch-of-text' } }); t.is(libc.familySync(), libc.GLIBC); t.is(libc.versionSync(), '1.23'); t.false(libc.isNonGlibcLinuxSync()); }); test('linux - musl family detected via ldd', async (t) => { t.plan(2); const libc = proxyquire('../', { './process': { isLinux: () => true }, './filesystem': { readFile: () => Promise.resolve('bunch-of-text-musl') } }); t.is(await libc.family(), libc.MUSL); t.true(await libc.isNonGlibcLinux()); }); test('linux - musl familySync detected via ldd', async (t) => { t.plan(2); const libc = proxyquire('../', { './process': { isLinux: () => true }, './filesystem': { readFileSync: () => 'bunch-of-text-musl' } }); t.is(libc.familySync(), libc.MUSL); t.true(libc.isNonGlibcLinuxSync()); }); test('linux - musl family fallback when not found ldd', async (t) => { t.plan(2); const someError = new Error('Some error'); const libc = proxyquire('../', { './process': { isLinux: () => true, getReport: () => ({ sharedObjects: ['/lib/ld-musl-x86_64.so.1'] }) }, './filesystem': { readFile: () => Promise.reject(someError) } }); t.is(await libc.family(), libc.MUSL); t.true(await libc.isNonGlibcLinux()); }); test('linux - musl familySync fallback when not found ldd', async (t) => { t.plan(2); const someError = new Error('Some error'); const libc = proxyquire('../', { './process': { isLinux: () => true, getReport: () => ({ sharedObjects: ['/lib/ld-musl-x86_64.so.1'] }) }, './filesystem': { readFileSync: () => { throw someError; } } }); t.is(libc.familySync(), libc.MUSL); t.true(libc.isNonGlibcLinuxSync()); }); test('linux - musl family detected via report', async (t) => { t.plan(4); const libc = proxyquire('../', { './process': { isLinux: () => true, getReport: () => ({ sharedObjects: ['/lib/ld-musl-x86_64.so.1'] }) }, './filesystem': { readFile: () => Promise.reject(filePermissionError), readFileSync: () => { throw filePermissionError; } } }); t.is(await libc.family(), libc.MUSL); t.true(await libc.isNonGlibcLinux()); t.is(libc.familySync(), libc.MUSL); t.true(libc.isNonGlibcLinuxSync()); }); test('linux - glibc family detected via async child process', async (t) => { t.plan(2); const out = 'glibc 1.23\nldd (GLIBC) 1.23\nCopyright\netc'; const libc = proxyquire('../', { './process': { isLinux: () => true, getReport: () => ({}) }, child_process: { exec: (_c, cb) => cb(null, out) }, './filesystem': { readFile: () => Promise.reject(filePermissionError), readFileSync: () => { throw filePermissionError; } } }); t.is(await libc.family(), libc.GLIBC); t.false(await libc.isNonGlibcLinux()); }); test('linux - glibc family detected via sync child process', async (t) => { t.plan(2); const out = 'glibc 1.23\nldd (GLIBC) 1.23\nCopyright\netc'; const libc = proxyquire('../', { './process': { isLinux: () => true, getReport: () => ({}) }, child_process: { execSync: () => out }, './filesystem': { readFile: () => Promise.reject(filePermissionError), readFileSync: () => { throw filePermissionError; } } }); t.is(libc.familySync(), libc.GLIBC); t.false(libc.isNonGlibcLinuxSync()); }); test('linux - musl family detected via async child process', async (t) => { t.plan(2); const out = 'getconf: GNU_LIBC_VERSION: unknown variable\nmusl libc\nVersion 1.2.3\netc'; const libc = proxyquire('../', { './process': { isLinux: () => true, getReport: () => ({ sharedObjects: [] }) }, child_process: { exec: (_c, cb) => cb(null, out) }, './filesystem': { readFile: () => Promise.reject(filePermissionError), readFileSync: () => { throw filePermissionError; } } }); t.is(await libc.family(), libc.MUSL); t.true(await libc.isNonGlibcLinux()); }); test('linux - musl family detected via sync child process', async (t) => { t.plan(2); const out = 'getconf: GNU_LIBC_VERSION: unknown variable\nmusl libc\nVersion 1.2.3\netc'; const libc = proxyquire('../', { './process': { isLinux: () => true, getReport: () => ({ sharedObjects: [] }) }, child_process: { execSync: () => out }, './filesystem': { readFile: () => Promise.reject(filePermissionError), readFileSync: () => { throw filePermissionError; } } }); t.is(libc.familySync(), libc.MUSL); t.true(libc.isNonGlibcLinuxSync()); }); test('linux - unknown family', async (t) => { t.plan(4); const out = 'unknown'; const libc = proxyquire('../', { './process': { isLinux: () => true, getReport: () => ({}) }, child_process: { exec: (_c, cb) => cb(null, out), execSync: () => out }, './filesystem': { readFile: () => Promise.reject(filePermissionError), readFileSync: () => { throw filePermissionError; } } }); t.is(await libc.family(), null); t.true(await libc.isNonGlibcLinux()); t.is(libc.familySync(), null); t.true(libc.isNonGlibcLinuxSync()); }); test('linux - unknown family (exec fails)', async (t) => { t.plan(2); const libc = proxyquire('../', { './process': { isLinux: () => true, getReport: () => ({}) }, child_process: { exec: (_c, cb) => cb(new Error()) }, './filesystem': { readFile: () => Promise.reject(filePermissionError), readFileSync: () => { throw filePermissionError; } } }); t.is(await libc.family(), null); t.true(await libc.isNonGlibcLinux()); }); test('linux - unknown family (execSync fails)', async (t) => { t.plan(2); const libc = proxyquire('../', { './process': { isLinux: () => true, getReport: () => ({}) }, child_process: { execSync: () => { throw new Error(); } }, './filesystem': { readFileSync: () => { throw filePermissionError; } } }); t.is(libc.familySync(), null); t.true(libc.isNonGlibcLinuxSync()); }); test('non-linux - unknown family', async (t) => { t.plan(2); const libc = proxyquire('../', { './process': { isLinux: () => false } }); t.is(await libc.family(), null); t.false(await libc.isNonGlibcLinux()); }); test('non-linux - unknown familySync', async (t) => { t.plan(2); const libc = proxyquire('../', { './process': { isLinux: () => false } }); t.is(libc.familySync(), null); t.false(libc.isNonGlibcLinuxSync()); }); // version test('linux - glibc version detected via filesystem', async (t) => { t.plan(1); const out = '--vers | --versi | --versio | --version)\necho \'ldd (Ubuntu GLIBC 1.23-0ubuntu9.9) 1.23\''; const libc = proxyquire('../', { './process': { isLinux: () => true }, './filesystem': { readFile: () => Promise.resolve(out) } }); t.is(await libc.version(), '1.23'); }); test('linux - glibc version detected via filesystem (libc)', async (t) => { t.plan(1); const out = '--vers | --versi | --versio | --version)\necho \'ldd (GNU libc) 2.39\''; const libc = proxyquire('../', { './process': { isLinux: () => true }, './filesystem': { readFile: () => Promise.resolve(out) } }); t.is(await libc.version(), '2.39'); }); test('linux - libc version not detected via filesystem (void linux musl)', async (t) => { t.plan(1); const out = 'startlibc_startGNU AS 2.35.1'; const libc = proxyquire('../', { './process': { isLinux: () => true, getReport: () => ({}) }, './filesystem': { readFile: () => Promise.resolve(out) }, child_process: { exec: (_c, cb) => cb(null, out), execSync: () => out } }); t.is(await libc.version(), null); }); test('linux - glibc version detected via filesystemSync', async (t) => { t.plan(1); const out = '--vers | --versi | --versio | --version)\necho \'ldd (Ubuntu GLIBC 1.23-0ubuntu9.9) 1.23\''; const libc = proxyquire('../', { './process': { isLinux: () => true }, './filesystem': { readFileSync: () => out } }); t.is(libc.versionSync(), '1.23'); }); test('linux - glibc version detected via filesystemSync (libc)', async (t) => { t.plan(1); const out = '--vers | --versi | --versio | --version)\necho \'ldd (GNU libc) 2.39\''; const libc = proxyquire('../', { './process': { isLinux: () => true }, './filesystem': { readFileSync: () => out } }); t.is(libc.versionSync(), '2.39'); }); test('linux - libc version not detected via filesystemSync (void linux musl)', (t) => { t.plan(1); const out = 'startlibc_startGNU AS 2.35.1'; const libc = proxyquire('../', { './process': { isLinux: () => true, getReport: () => ({}) }, './filesystem': { readFile: () => Promise.resolve(out) }, child_process: { exec: (_c, cb) => cb(null, out), execSync: () => out } }); t.is(libc.versionSync(), null); }); test('linux - glibc version detected via child process', async (t) => { t.plan(1); const out = 'glibc 1.23\nldd (GLIBC) 1.23\nCopyright\netc'; const libc = proxyquire('../', { './process': { isLinux: () => true, getReport: () => ({}) }, child_process: { exec: (_c, cb) => cb(null, out), execSync: () => out }, './filesystem': { readFile: () => Promise.reject(filePermissionError) } }); t.is(await libc.version(), '1.23'); }); test('linux - glibc version detected via child process sync', async (t) => { t.plan(1); const out = 'glibc 1.23\nldd (GLIBC) 1.23\nCopyright\netc'; const libc = proxyquire('../', { './process': { isLinux: () => true, getReport: () => ({}) }, child_process: { execSync: () => out }, './filesystem': { readFileSync: () => { throw filePermissionError; } } }); t.is(libc.versionSync(), '1.23'); }); test('linux - musl version detected via child process', async (t) => { t.plan(4); const out = 'getconf: GNU_LIBC_VERSION: unknown variable\nmusl libc\nVersion 1.2.3\netc'; const libc = proxyquire('../', { './process': { isLinux: () => true, getReport: () => ({}) }, child_process: { exec: (_c, cb) => cb(null, out), execSync: () => out }, './filesystem': { readFile: () => Promise.resolve('does not have version') } }); t.is(await libc.version(), '1.2.3'); t.is(libc.versionSync(), '1.2.3'); // calling twice to check the cache t.is(await libc.version(), '1.2.3'); t.is(libc.versionSync(), '1.2.3'); }); test('linux - unknown version', async (t) => { t.plan(2); const out = 'unknown'; const libc = proxyquire('../', { './process': { isLinux: () => true, getReport: () => ({}) }, child_process: { exec: (_c, cb) => cb(null, out), execSync: () => out }, './filesystem': { readFile: () => Promise.resolve('does not have version') } }); t.is(await libc.version(), null); t.is(libc.versionSync(), null); }); test('linux - unknown version (exec fails)', async (t) => { t.plan(2); const libc = proxyquire('../', { './process': { isLinux: () => true, getReport: () => ({}) }, child_process: { exec: (_c, cb) => cb(new Error()), execSync: () => { throw new Error(); } }, './filesystem': { readFile: () => Promise.resolve('does not have version') } }); t.is(await libc.version(), null); t.is(libc.versionSync(), null); }); test('non-linux - unknown version', async (t) => { t.plan(2); const libc = proxyquire('../', { './process': { isLinux: () => false } }); t.is(await libc.version(), null); t.is(libc.versionSync(), null); }); test('process (internal)', (t) => { t.plan(3); const process = require('../lib/process'); t.is(typeof process.isLinux(), 'boolean'); t.is(typeof process.getReport(), 'object'); t.is(process.getReport(), process.getReport()); });