@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.
75 lines (74 loc) • 2.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PubSub = void 0;
const Context_1 = require("../agent/Context");
const wrapExport_1 = require("../agent/hooks/wrapExport");
const tryParseJSON_1 = require("../helpers/tryParseJSON");
class PubSub {
wrapMessageHandler(args) {
if (args.length > 0 &&
typeof args[0] === "string" &&
args[0] === "message" &&
typeof args[1] === "function") {
const originalCallback = args[1];
args[1] = handleMessage(originalCallback);
}
return args;
}
wrap(hooks) {
hooks
.addPackage("@google-cloud/pubsub")
.withVersion("^5.0.0 || ^4.0.0")
.onFileRequire("build/src/subscription.js", (exports, pkgInfo) => {
(0, wrapExport_1.wrapExport)(exports.Subscription.prototype, "on", pkgInfo, {
kind: undefined,
modifyArgs: this.wrapMessageHandler,
});
})
.addFileInstrumentation({
path: "build/src/subscription.js",
functions: [
{
name: "constructor",
nodeType: "MethodDefinition",
operationKind: undefined,
inspectArgs: (args, agent, subscription) => {
(0, wrapExport_1.wrapExport)(subscription, "on", {
name: "@google-cloud/pubsub",
type: "external",
}, {
kind: undefined,
modifyArgs: this.wrapMessageHandler,
});
},
},
],
});
}
}
exports.PubSub = PubSub;
function handleMessage(handler) {
return function handleMessage(...args) {
let body = undefined;
if (args.length > 0) {
const message = args[0];
if (message && message.data) {
body = (0, tryParseJSON_1.tryParseJSON)(message.data.toString());
}
}
return (0, Context_1.runWithContext)({
body: body,
method: undefined,
remoteAddress: undefined,
url: undefined,
headers: {},
cookies: {},
query: {},
routeParams: {},
source: "pubsub",
route: undefined,
}, () => {
return handler(...args);
});
};
}