@aikidosec/firewall
Version:
Zen by Aikido is an embedded Web Application Firewall that autonomously protects Node.js apps against common and critical attacks
58 lines (57 loc) • 1.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Hostnames = void 0;
class Hostnames {
constructor(maxEntries = 200) {
this.maxEntries = maxEntries;
this.map = new Map();
}
add(hostname, port) {
if (port <= 0) {
return;
}
if (!this.map.has(hostname)) {
this.map.set(hostname, new Map([[port, 1]]));
}
else {
const ports = this.map.get(hostname);
if (!ports.has(port)) {
ports.set(port, 1);
}
else {
ports.set(port, ports.get(port) + 1);
}
}
if (this.length > this.maxEntries) {
const firstAdded = this.map.keys().next().value;
if (firstAdded) {
const ports = this.map.get(firstAdded);
if (ports.size > 1) {
const firstPort = ports.keys().next().value;
if (firstPort) {
ports.delete(firstPort);
}
}
else {
this.map.delete(firstAdded);
}
}
}
}
get length() {
return Array.from(this.map.values()).reduce((total, ports) => total + ports.size, 0);
}
asArray() {
return Array.from(this.map.entries()).flatMap(([hostname, ports]) => Array.from(ports.entries()).map(([port, hits]) => {
return {
hostname,
port,
hits,
};
}));
}
clear() {
this.map.clear();
}
}
exports.Hostnames = Hostnames;