๐Ÿ—๏ธ Architecture Guide

How gif-captcha is structured internally โ€” modules, layers, and data flow

Contents

System Overview

gif-captcha is a modular CAPTCHA platform built around visual comprehension challenges. Rather than distorted text or image grids, it presents animated GIFs and asks users to describe what happens โ€” a task that requires temporal reasoning humans excel at but current AI models struggle with.

The codebase is organized into ~30 specialized modules in src/, each handling a distinct concern. Modules are pure JavaScript with zero external runtime dependencies, communicating through well-defined factory functions and event callbacks.

Module Layers

Modules fall into four conceptual layers, each building on the ones below:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Operations โ€” compliance, incidents, capacity planning โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚ Analytics โ€” stats, traffic analysis, A/B experiments โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚ Security โ€” rate limiting, trust scoring, fraud detect โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚ Core โ€” challenge creation, validation, difficulty โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Core Layer โ€” Challenge Engine

The foundation that creates, serves, and validates CAPTCHA challenges.

index.js Core

The main entry point (~420KB). Exports the public API: createChallenge(), validateAnswer(), pickChallenges(), sanitize(), and the built-in challenge pool. Handles both browser (DOM rendering, GIF loading with retry) and Node.js (sanitization, validation) environments.

adaptive-difficulty-tuner.js Core

Automatically adjusts CAPTCHA difficulty based on real-time solve rates. Monitors solve/fail events in sliding time windows and recommends adjustments across four dimensions: speed, complexity, distortion, and frame count. Keeps solve rates within a configurable target band (default 55โ€“75%).

challenge-pool-manager.js Core

Manages the available challenge pool โ€” adding, retiring, and selecting challenges. Ensures challenges are distributed evenly and tracks per-challenge usage statistics.

challenge-template-engine.js Core

Generates challenge variants from templates. Supports parameterized challenges where the same GIF can produce different questions/expected answers, increasing the effective pool size.

challenge-rotation-scheduler.js Core

Schedules when challenges are rotated in and out of the active pool. Prevents staleness by ensuring users don't see the same challenges repeatedly.

challenge-decay-manager.js Core

Tracks challenge effectiveness over time. As a challenge becomes "known" (high solve rates or detected in bot training data), it decays toward retirement, keeping the pool fresh.

Security Layer โ€” Threat Detection & Rate Limiting

Protects the CAPTCHA system from automated attacks and abuse.

trust-score-engine.js Security

Computes per-session trust scores using an LRU-cached scoring system. Combines multiple signals (solve accuracy, timing patterns, behavioral biometrics) into a single 0โ€“100 score. High-trust sessions can receive easier challenges; low-trust sessions get harder ones or are blocked entirely.

captcha-rate-limiter.js Security

Multi-algorithm rate limiting with three strategies: sliding window (count requests in a rolling window), token bucket (allow bursts up to capacity), and leaky bucket (smooth request flow). Includes automatic key cleanup, ban escalation after repeated violations, and per-key statistics.

behavioral-biometrics.js Security

Analyzes mouse movements, typing patterns, and interaction timing to build behavioral fingerprints. Distinguishes human interaction patterns from automated scripts that mimic human behavior.

bot-signature-database.js Security

Maintains signatures of known bot behaviors โ€” response patterns, timing profiles, and user-agent fingerprints. Used by the trust score engine to immediately flag known automated clients.

fraud-ring-detector.js Security

Detects coordinated attack patterns across multiple sessions. Identifies fraud rings by correlating timing, geographic, and behavioral signals across seemingly independent requests.

geo-risk-scorer.js Security

Assigns risk scores based on geographic signals โ€” IP geolocation, VPN/proxy detection, and regional attack pattern history. Feeds into the trust score engine.

honeypot-injector.js Security

Injects invisible honeypot fields into CAPTCHA forms. Bots that fill hidden fields are immediately flagged without requiring the user to solve anything.

session-risk-aggregator.js Security

Aggregates risk signals from all security modules into a unified session risk profile. Provides the final allow/challenge/block decision based on composite risk.

Analytics Layer โ€” Monitoring & Insights

Collects metrics, detects anomalies, and provides operational visibility.

captcha-stats-collector.js Analytics

Central metrics collection โ€” solve rates, response times, error rates, and throughput. Aggregates data for dashboards and alerting.

captcha-traffic-analyzer.js Analytics

Analyzes traffic patterns to detect surges, DDoS attempts, and unusual distribution changes. Provides time-series analysis of CAPTCHA request volumes.

captcha-anomaly-detector.js Analytics

Statistical anomaly detection for CAPTCHA metrics. Flags unusual solve rate changes, timing distribution shifts, and other statistical outliers that may indicate attacks.

ab-experiment-runner.js Analytics

A/B testing framework for CAPTCHA configurations. Run experiments comparing different difficulty settings, UI layouts, or validation thresholds with statistical significance testing.

solve-funnel-analyzer.js Analytics

Tracks the solve funnel: challenge presented โ†’ attempted โ†’ solved โ†’ verified. Identifies drop-off points and UX friction.

solve-pattern-fingerprinter.js Analytics

Creates behavioral fingerprints from solve patterns โ€” answer content, timing, error types. Used for cross-session tracking of suspicious actors.

response-time-profiler.js Analytics

Profiles CAPTCHA response time distributions. Builds statistical models of expected human response times to flag suspiciously fast (bot) or slow (farm) responses.

captcha-fatigue-detector.js Analytics

Detects user fatigue patterns โ€” declining solve accuracy, increasing response times, and abandonment signals. Recommends reducing challenge frequency for fatigued legitimate users.

captcha-session-replay.js Analytics

Records and replays CAPTCHA sessions for debugging and analysis. Captures the full interaction sequence to understand solve failures and improve challenge design.

Operations Layer โ€” Compliance, Incidents & Capacity

Production operational concerns โ€” from regulatory compliance to capacity planning.

compliance-reporter.js Operations

Generates compliance reports for accessibility (WCAG), privacy (GDPR/CCPA), and security standards. Tracks data retention policies and produces audit-ready documentation.

captcha-incident-manager.js Operations

Incident lifecycle management โ€” detection, escalation, mitigation, and post-mortem. Integrates with the anomaly detector and health monitor to automatically create and manage incidents.

captcha-health-monitor.js Operations

Comprehensive health checking โ€” module status, dependency health, performance budgets, and availability SLOs. Provides readiness and liveness probes for orchestration systems.

captcha-capacity-planner.js Operations

Forecasts resource needs based on traffic trends. Provides scaling recommendations and alerts before capacity limits are reached.

captcha-load-tester.js Operations

Built-in load testing for the CAPTCHA pipeline. Simulates realistic traffic patterns to validate performance under expected and peak loads.

captcha-audit-log.js Operations

Tamper-evident audit logging for all CAPTCHA operations. Records challenge generation, validation decisions, and administrative actions for forensic analysis.

captcha-localization-manager.js Operations

Internationalization support โ€” manages translated challenge text, error messages, and UI strings. Supports right-to-left languages and locale-specific formatting.

captcha-export-formatter.js Operations

Exports CAPTCHA data in multiple formats (JSON, CSV, XML) for integration with external analytics platforms, SIEMs, and reporting tools.

webhook-dispatcher.js Operations

Delivers real-time event notifications via webhooks. Supports configurable event filters, retry with exponential backoff, and dead-letter handling for failed deliveries.

captcha-accessibility-analyzer.js Operations

Audits CAPTCHA challenges for accessibility compliance. Checks contrast ratios, screen reader compatibility, keyboard navigation, and provides WCAG conformance reports.

Request Lifecycle

When a CAPTCHA request arrives, it flows through the system in this order:

1. Incoming request โ”‚ 2. Rate Limiter โ†’ โŒ reject if over limit โ”‚ โœ… 3. Session Risk Aggregator โ”œโ”€ Geo Risk Scorer โ”œโ”€ Bot Signature Database โ”œโ”€ Behavioral Biometrics โ””โ”€ Trust Score Engine โ”‚ 4. Difficulty Decision โ”œโ”€ High trust โ†’ easy challenge โ”œโ”€ Medium trust โ†’ standard challenge โ””โ”€ Low trust โ†’ hard challenge / block โ”‚ 5. Challenge Selection โ”œโ”€ Challenge Pool Manager (pick from active pool) โ”œโ”€ Challenge Template Engine (generate variant) โ”œโ”€ Challenge Rotation Scheduler (freshness check) โ””โ”€ Honeypot Injector (add hidden fields) โ”‚ 6. Serve challenge to client โ”‚ 7. User submits answer โ”‚ 8. Validation โ”œโ”€ validateAnswer() โ€” fuzzy text matching โ”œโ”€ Response Time Profiler โ€” timing check โ”œโ”€ Honeypot check โ€” hidden field detection โ””โ”€ Solve Pattern Fingerprinter โ€” behavioral check โ”‚ 9. Update State โ”œโ”€ Trust Score Engine (adjust score) โ”œโ”€ Adaptive Difficulty Tuner (update solve rates) โ”œโ”€ Stats Collector (record metrics) โ”œโ”€ Challenge Decay Manager (track effectiveness) โ””โ”€ Audit Log (record decision) โ”‚ 10. Return result (pass / fail / block)

Design Patterns

Zero Dependencies

Every module is pure JavaScript with no external runtime dependencies. This makes gif-captcha embeddable anywhere โ€” browsers, Node.js, edge workers, serverless functions โ€” without dependency conflicts or supply chain risk.

Factory Functions

Modules export factory functions (e.g., createCaptchaRateLimiter(opts)) rather than classes. This provides clean instantiation, avoids new keyword confusion, and makes testing straightforward โ€” just call the factory with test configuration.

LRU Eviction

Stateful modules (rate limiter, trust score engine, session tracker) use LRU eviction to bound memory usage. The LruTracker is a custom doubly-linked list implementation inlined in each module for independence โ€” no shared mutable state between modules.

Sliding Windows

Time-based metrics use sliding window algorithms (binary search on sorted timestamp arrays) rather than fixed buckets. This provides smooth, accurate rates without the boundary artifacts of fixed-window counters.

Event-Driven Composition

Modules communicate through callback hooks and event records rather than direct coupling. The stats collector, audit log, and webhook dispatcher can all observe the same events independently, making the system easy to extend.

Extending the System

To add a new module:

  1. Create a new file in src/ following the factory function pattern
  2. Include JSDoc documentation with @module and @example tags
  3. Add corresponding tests in tests/
  4. Wire it into the request lifecycle by passing it as a callback or dependency to existing modules

The module independence design means you can use any subset of modules โ€” the rate limiter works without the trust score engine, the stats collector works without the anomaly detector, etc.


GitHub ยท npm ยท Case Study ยท MIT License