@aikidosec/firewall
Version:
Zen by Aikido is an embedded Application Firewall that autonomously protects Node.js apps against common and critical attacks, provides rate limiting, detects malicious traffic (including bots), and more.
32 lines (31 loc) • 1.24 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPackageVersion = getPackageVersion;
const path_1 = require("path");
const wrapRequire_1 = require("../agent/hooks/wrapRequire");
/**
* Get the installed version of a package
*/
function getPackageVersion(pkg) {
try {
const path = require.resolve(pkg);
const parts = path.split(path_1.sep);
// e.g. @google-cloud/functions-framework
const pkgParts = pkg.split("/");
let lookup = pkgParts[0];
if (pkgParts.length > 1) {
lookup = pkgParts[1];
}
// Normally we can just require(`${pkg}/package.json`)
// but @google-cloud/functions-framework is a special case
// the package.json contains "exports" which results in the following error:
// Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './package.json' is not defined by "exports"
// Thus we manually build the path to the package.json
const index = parts.indexOf(lookup);
const root = parts.slice(0, index + 1).join(path_1.sep);
return (0, wrapRequire_1.getOriginalRequire)()(`${root}/package.json`).version;
}
catch {
return null;
}
}
;