bot-guardian-js
Version:
A powerful bot detection and prevention library for Node.js applications
56 lines (55 loc) • 2.2 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.guardianStorage = exports.GuardianStorage = void 0;
const GuardianJS_1 = require("../core/GuardianJS");
const guardian = new GuardianJS_1.GuardianJS();
const detections = [];
class GuardianStorage {
constructor() { }
static getInstance() {
if (!GuardianStorage.instance) {
GuardianStorage.instance = new GuardianStorage();
}
return GuardianStorage.instance;
}
detectBot(params) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield guardian.isBot(params);
detections.push(result);
return result;
});
}
getDetections() {
return [...detections].sort((a, b) => {
const bTime = b.timestamp.getTime();
const aTime = a.timestamp.getTime();
return bTime - aTime;
});
}
getStats() {
const total = detections.length;
const bots = detections.filter((d) => d.isBot).length;
const pathStats = detections.reduce((acc, curr) => {
const path = curr.path || 'unknown';
acc[path] = (acc[path] || 0) + 1;
return acc;
}, {});
return {
totalRequests: total,
detectedBots: bots,
pathStats
};
}
}
exports.GuardianStorage = GuardianStorage;
// Create and export a singleton instance
exports.guardianStorage = GuardianStorage.getInstance();