Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions lib/detect-libc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

'use strict';

const childProcess = require('child_process');
const { safeRequire } = require('./utils');
const childProcess = safeRequire('child_process', 'node:child_process');
const { isLinux, getReport } = require('./process');
const { LDD_PATH, readFile, readFileSync } = require('./filesystem');

Expand All @@ -16,10 +17,13 @@ let commandOut = '';
const safeCommand = () => {
if (!commandOut) {
return new Promise((resolve) => {
childProcess.exec(command, (err, out) => {
commandOut = err ? ' ' : out;
resolve(commandOut);
});
// null check
if (childProcess) {
childProcess.exec(command, (err, out) => {
commandOut = err ? ' ' : out;
resolve(commandOut);
});
}
});
}
return commandOut;
Expand All @@ -28,6 +32,7 @@ const safeCommand = () => {
const safeCommandSync = () => {
if (!commandOut) {
try {
// No null check needed since the error will be caught
commandOut = childProcess.execSync(command, { encoding: 'utf8' });
} catch (_err) {
commandOut = ' ';
Expand Down
22 changes: 13 additions & 9 deletions lib/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

'use strict';

const fs = require('fs');
const { safeRequire } = require('./utils');
const fs = safeRequire('fs', 'node:fs');

/**
* The path where we can find the ldd
Expand All @@ -16,7 +17,7 @@ const LDD_PATH = '/usr/bin/ldd';
* @param {string} path
* @returns {string}
*/
const readFileSync = (path) => fs.readFileSync(path, 'utf-8');
const readFileSync = (path) => fs ? fs.readFileSync(path, 'utf-8') : null;

/**
* Read the content of a file
Expand All @@ -25,13 +26,16 @@ const readFileSync = (path) => fs.readFileSync(path, 'utf-8');
* @returns {Promise<string>}
*/
const readFile = (path) => new Promise((resolve, reject) => {
fs.readFile(path, 'utf-8', (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
// null check
if (fs) {
fs.readFile(path, 'utf-8', (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
}
});

module.exports = {
Expand Down
23 changes: 23 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

// Safe require
const safeRequire = (moduleName, ...fallbacks) => {
try {
// Try to require the primary module
return require(moduleName);
} catch (error) {
// If module not found, try fallbacks
for (let i = 0; i < fallbacks.length; i++) {
const fallback = fallbacks[i];
try {
return require(fallback);
} catch (fallbackError) {
// Skip to the next fallback if it fails
}
}

return null;
}
};

module.exports = { safeRequire };
Loading