UNPKG

@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.

35 lines (34 loc) 1.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.contextFromRequest = contextFromRequest; const getIPAddressFromRequest_1 = require("../../helpers/getIPAddressFromRequest"); const buildRouteFromURL_1 = require("../../helpers/buildRouteFromURL"); function contextFromRequest(ctx) { // On Node.js, Elysia uses the srvx library under the hood. srvx adds an `ip` // field to the request that holds the client's address. // https://github.com/h3js/srvx/blob/main/src/adapters/_node/request.ts#L41-L43 const ip = ctx.request.ip; return { method: ctx.request.method, remoteAddress: (0, getIPAddressFromRequest_1.getIPAddressFromRequest)({ headers: ctx.headers, remoteAddress: typeof ip === "string" ? ip : undefined, }), body: ctx.body, url: ctx.request.url, headers: ctx.headers, routeParams: ctx.params, query: ctx.query, cookies: convertCookies(ctx.cookie), source: "elysia", route: (0, buildRouteFromURL_1.buildRouteFromURL)(ctx.request.url), }; } function convertCookies(cookies) { if (!cookies) { return {}; } return Object.fromEntries(Object.entries(cookies) .map(([k, v]) => [k, v.value]) .filter(([_, v]) => typeof v === "string")); }