UNPKG

@aikidosec/firewall

Version:

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

49 lines (48 loc) 2.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Koa = void 0; const wrapExport_1 = require("../agent/hooks/wrapExport"); const wrapNewInstance_1 = require("../agent/hooks/wrapNewInstance"); const wrapMiddleware_1 = require("./koa/wrapMiddleware"); const isDeprecatedGenerator_1 = require("./koa/isDeprecatedGenerator"); class Koa { wrapUseArgs(args) { var _a; if (typeof args[0] === "function") { // Check if the middleware is a generator function // We cant wrap them like normal functions, but they are already deprecated for a while and will be completely removed in koa v3 if (!(0, isDeprecatedGenerator_1.isDeprecatedGenerator)(args[0])) { // If it's a router (@koa/router or koa-router), we need to wrap all the middleware in the router // Wrapping .use directly breaks the router if ("router" in args[0] && args[0].router) { const router = args[0].router; const routesStack = (_a = router.stack) !== null && _a !== void 0 ? _a : []; for (const pathLayer of routesStack) { const pathStack = pathLayer.stack; for (let i = 0; i < pathStack.length; i++) { pathStack[i] = (0, wrapMiddleware_1.wrapMiddleware)(pathStack[i]); } } } else { args[0] = (0, wrapMiddleware_1.wrapMiddleware)(args[0]); } } } return args; } wrap(hooks) { hooks .addPackage("koa") .withVersion("^3.0.0 || ^2.0.0") .onRequire((exports, pkgInfo) => { return (0, wrapNewInstance_1.wrapNewInstance)(exports, undefined, pkgInfo, (instance) => { (0, wrapExport_1.wrapExport)(instance, "use", pkgInfo, { kind: undefined, modifyArgs: this.wrapUseArgs, }); }); }); } } exports.Koa = Koa;