UNPKG

bot-guardian-js

Version:

A powerful bot detection and prevention library for Node.js applications

56 lines (55 loc) 2.68 kB
"use strict"; 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.useGuardian = exports.GuardianProvider = exports.withGuardian = exports.getGuardianInstance = void 0; const GuardianJS_1 = require("../core/GuardianJS"); // Singleton instance let guardianInstance = null; function getGuardianInstance(config = {}) { if (!guardianInstance) { guardianInstance = new GuardianJS_1.GuardianJS(Object.assign({ useBehavior: true, threshold: 0.5 }, config)); } return guardianInstance; } exports.getGuardianInstance = getGuardianInstance; // HOC for API routes function withGuardian(handler) { return (req, res) => __awaiter(this, void 0, void 0, function* () { const guardian = getGuardianInstance(); try { const result = yield guardian.isBot({ userAgent: req.headers['user-agent'] || '', ip: req.headers['x-forwarded-for'] || req.socket.remoteAddress || '', req }); req.botDetection = { isBot: result.isBot, confidence: result.confidence, score: result.score, reasons: result.reasons, behavior: result.behavior }; } catch (error) { console.error('GuardianJS error:', error); } return handler(req, res); }); } exports.withGuardian = withGuardian; // For client-side tracking var GuardianProvider_1 = require("../components/GuardianProvider"); Object.defineProperty(exports, "GuardianProvider", { enumerable: true, get: function () { return __importDefault(GuardianProvider_1).default; } }); var useGuardian_1 = require("../hooks/useGuardian"); Object.defineProperty(exports, "useGuardian", { enumerable: true, get: function () { return __importDefault(useGuardian_1).default; } });