bot-guardian-js
Version:
A powerful bot detection and prevention library for Node.js applications
61 lines (60 loc) • 2.59 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 });
// src/routes/guardian.ts
const express_1 = require("express");
const guardianStorage_1 = require("../services/guardianStorage");
const router = (0, express_1.Router)();
// Protect these routes with your auth middleware
router.get('/detections', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
try {
const detections = yield guardianStorage_1.guardianStorage.getDetections();
res.json(detections);
}
catch (error) {
res.status(500).json({ error: 'Failed to fetch detections' });
}
}));
router.get('/stats', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
try {
const detections = yield guardianStorage_1.guardianStorage.getDetections();
const pathStats = detections.reduce((acc, curr) => {
const path = curr.path || 'unknown';
acc[path] = (acc[path] || 0) + 1;
return acc;
}, {});
res.json({
total: detections.length,
bots: detections.filter(d => d.isBot).length,
paths: pathStats
});
}
catch (error) {
res.status(500).json({ error: 'Failed to fetch stats' });
}
}));
router.get('/data', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
try {
const detections = guardianStorage_1.guardianStorage.getDetections();
const stats = guardianStorage_1.guardianStorage.getStats();
res.json({
totalRequests: stats.totalRequests,
detectedBots: stats.detectedBots,
pathStats: stats.pathStats,
recentDetections: detections.slice(0, 10)
});
}
catch (error) {
console.error('Error fetching dashboard data:', error);
res.status(500).json({ error: 'Failed to fetch dashboard data' });
}
}));
exports.default = router;