UNPKG

@aikidosec/firewall

Version:

Zen by Aikido is an embedded Web Application Firewall that autonomously protects Node.js apps against common and critical attacks

62 lines (61 loc) 1.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PubSub = void 0; const Context_1 = require("../agent/Context"); const wrapExport_1 = require("../agent/hooks/wrapExport"); class PubSub { 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: (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; }, }); }); } } 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 = 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); }); }; } function tryParseJSON(jsonString) { try { return JSON.parse(jsonString); } catch { return undefined; } }