@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.
33 lines (32 loc) • 1.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformCode = transformCode;
const node_code_instrumentation_1 = require("./wasm/node_code_instrumentation");
const getSourceType_1 = require("./getSourceType");
const path_1 = require("path");
const isNewInstrumentationUnitTest_1 = require("../../../helpers/isNewInstrumentationUnitTest");
const isEsmUnitTest_1 = require("../../../helpers/isEsmUnitTest");
function transformCode(pkgName, pkgVersion, path, code, pkgLoadFormat, fileInstructions) {
try {
const result = (0, node_code_instrumentation_1.wasm_transform_code_str)(pkgName, pkgVersion, code, JSON.stringify(fileInstructions), (0, getSourceType_1.getSourceType)(path, pkgLoadFormat));
// Rewrite import path for unit tests if environment variable is set to true
if ((0, isNewInstrumentationUnitTest_1.isNewInstrumentationUnitTest)()) {
// ESM files generate static ESM import statements, but ts-node can't handle this
// Use a .mjs wrapper (which uses createRequire internally) for ESM files
const injectedFile = (0, isEsmUnitTest_1.isEsmUnitTest)()
? "injectedFunctions.js"
: pkgLoadFormat === "module"
? "injectedFunctions.mjs"
: "injectedFunctions.ts";
return result.replace("@aikidosec/firewall/instrument/internals", (0, path_1.join)(__dirname, injectedFile));
}
return result;
}
catch (error) {
// Convert string errors to Error objects
if (typeof error === "string") {
throw new Error(`Error transforming code: ${error}`);
}
throw error;
}
}