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.

48 lines (47 loc) 1.54 kB
"use strict"; /* * Largely based on parseHeaders function from nodejs/unici * MIT License * Copyright (c) Matteo Collina and Undici contributors * https://github.com/nodejs/undici */ Object.defineProperty(exports, "__esModule", { value: true }); exports.parseHeaders = parseHeaders; function parseHeaders(headers, obj) { if (obj === undefined) obj = {}; for (let i = 0; i < headers.length; i += 2) { const key = headerNameToString(headers[i]); let val = obj[key]; if (val) { if (typeof val === "string") { val = [val]; obj[key] = val; } val.push(headers[i + 1].toString("utf8")); } else { const headersValue = headers[i + 1]; if (typeof headersValue === "string") { obj[key] = headersValue; } else { obj[key] = Array.isArray(headersValue) ? headersValue.map((x) => x.toString("utf8")) : headersValue.toString("utf8"); } } } // See https://github.com/nodejs/node/pull/46528 if ("content-length" in obj && "content-disposition" in obj) { obj["content-disposition"] = Buffer.from( // @ts-expect-error Ignore obj["content-disposition"]).toString("latin1"); } return obj; } function headerNameToString(value) { return typeof value === "string" ? value.toLowerCase() : value.toString("latin1").toLowerCase(); }