Skip to content

Commit 159f983

Browse files
committed
implemented safeRequire
1 parent ba39587 commit 159f983

File tree

3 files changed

+57
-29
lines changed

3 files changed

+57
-29
lines changed

lib/detect-libc.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
// Copyright 2017 Lovell Fuller and others.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
'use strict';
4+
"use strict";
55

6-
const childProcess = require('node:child_process');
7-
const { isLinux, getReport } = require('./process');
8-
const { LDD_PATH, readFile, readFileSync } = require('./filesystem');
6+
const { safeRequire } = require("./utils");
7+
8+
const childProcess = safeRequire("child_process", "node:child_process");
9+
const { isLinux, getReport } = require("./process");
10+
const { LDD_PATH, readFile, readFileSync } = require("./filesystem");
911

1012
let cachedFamilyFilesystem;
1113
let cachedVersionFilesystem;
1214

13-
const command = 'getconf GNU_LIBC_VERSION 2>&1 || true; ldd --version 2>&1 || true';
14-
let commandOut = '';
15+
const command =
16+
"getconf GNU_LIBC_VERSION 2>&1 || true; ldd --version 2>&1 || true";
17+
let commandOut = "";
1518

1619
const safeCommand = () => {
1720
if (!commandOut) {
1821
return new Promise((resolve) => {
1922
childProcess.exec(command, (err, out) => {
20-
commandOut = err ? ' ' : out;
23+
commandOut = err ? " " : out;
2124
resolve(commandOut);
2225
});
2326
});
@@ -28,9 +31,9 @@ const safeCommand = () => {
2831
const safeCommandSync = () => {
2932
if (!commandOut) {
3033
try {
31-
commandOut = childProcess.execSync(command, { encoding: 'utf8' });
34+
commandOut = childProcess.execSync(command, { encoding: "utf8" });
3235
} catch (_err) {
33-
commandOut = ' ';
36+
commandOut = " ";
3437
}
3538
}
3639
return commandOut;
@@ -41,7 +44,7 @@ const safeCommandSync = () => {
4144
* @type {string}
4245
* @public
4346
*/
44-
const GLIBC = 'glibc';
47+
const GLIBC = "glibc";
4548

4649
/**
4750
* A Regexp constant to get the GLIBC Version.
@@ -54,9 +57,9 @@ const RE_GLIBC_VERSION = /LIBC[a-z0-9 \-).]*?(\d+\.\d+)/i;
5457
* @type {string}
5558
* @public
5659
*/
57-
const MUSL = 'musl';
60+
const MUSL = "musl";
5861

59-
const isFileMusl = (f) => f.includes('libc.musl-') || f.includes('ld-musl-');
62+
const isFileMusl = (f) => f.includes("libc.musl-") || f.includes("ld-musl-");
6063

6164
const familyFromReport = () => {
6265
const report = getReport();
@@ -83,10 +86,10 @@ const familyFromCommand = (out) => {
8386
};
8487

8588
const getFamilyFromLddContent = (content) => {
86-
if (content.includes('musl')) {
89+
if (content.includes("musl")) {
8790
return MUSL;
8891
}
89-
if (content.includes('GNU C Library')) {
92+
if (content.includes("GNU C Library")) {
9093
return GLIBC;
9194
}
9295
return null;
@@ -158,7 +161,7 @@ const familySync = () => {
158161
* Resolves `true` only when the platform is Linux and the libc family is not `glibc`.
159162
* @returns {Promise<boolean>}
160163
*/
161-
const isNonGlibcLinux = async () => isLinux() && await family() !== GLIBC;
164+
const isNonGlibcLinux = async () => isLinux() && (await family()) !== GLIBC;
162165

163166
/**
164167
* Returns `true` only when the platform is Linux and the libc family is not `glibc`.
@@ -263,5 +266,5 @@ module.exports = {
263266
isNonGlibcLinux,
264267
isNonGlibcLinuxSync,
265268
version,
266-
versionSync
269+
versionSync,
267270
};

lib/filesystem.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,44 @@
11
// Copyright 2017 Lovell Fuller and others.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
'use strict';
4+
"use strict";
55

6-
const fs = require('node:fs');
6+
const { safeRequire } = require("./utils");
7+
8+
const fs = safeRequire("fs", "node:fs");
79

810
/**
911
* The path where we can find the ldd
1012
*/
11-
const LDD_PATH = '/usr/bin/ldd';
13+
const LDD_PATH = "/usr/bin/ldd";
1214

1315
/**
1416
* Read the content of a file synchronous
1517
*
1618
* @param {string} path
1719
* @returns {string}
1820
*/
19-
const readFileSync = (path) => fs.readFileSync(path, 'utf-8');
21+
const readFileSync = (path) => fs.readFileSync(path, "utf-8");
2022

2123
/**
2224
* Read the content of a file
2325
*
2426
* @param {string} path
2527
* @returns {Promise<string>}
2628
*/
27-
const readFile = (path) => new Promise((resolve, reject) => {
28-
fs.readFile(path, 'utf-8', (err, data) => {
29-
if (err) {
30-
reject(err);
31-
} else {
32-
resolve(data);
33-
}
29+
const readFile = (path) =>
30+
new Promise((resolve, reject) => {
31+
fs.readFile(path, "utf-8", (err, data) => {
32+
if (err) {
33+
reject(err);
34+
} else {
35+
resolve(data);
36+
}
37+
});
3438
});
35-
});
3639

3740
module.exports = {
3841
LDD_PATH,
3942
readFileSync,
40-
readFile
43+
readFile,
4144
};

lib/utils.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"use strict";
2+
3+
// Safe require
4+
const safeRequire = (moduleName, ...fallbacks) => {
5+
try {
6+
// Try to require the primary module
7+
return require(moduleName);
8+
} catch (error) {
9+
// If module not found, try fallbacks
10+
for (const fallback of fallbacks) {
11+
try {
12+
return require(fallback);
13+
} catch (fallbackError) {
14+
// Skip to the next fallback if it fails
15+
}
16+
}
17+
18+
throw error; // Re-throw other errors
19+
}
20+
};
21+
22+
module.exports = { safeRequire };

0 commit comments

Comments
 (0)