API Reference
All public exports from @riavzon/bot-detector are documented below. Every getter (getDataSources, getStorage, getDb, getBatchQueue) throws if called before defineConfiguration resolves.
Initialization
defineConfiguration(config)
Initializes the middleware. Opens all MMDB and LMDB databases, starts the batch write queue, and sets up the cache and database connection. Call it once before attaching detectBots to your app. The full configuration reference is in the Configuration guide.
| Parameter | Type | Description |
|---|---|---|
config | BotDetectorConfigInput | Library configuration object. Only store.main is required; see the configuration guide for full schema. |
Returns: Promise<void>
warmUp()
Warms the database connection pool by running parallel SELECT 1 queries, then fires a dummy visitor query to prime the query plan cache. Call this after defineConfiguration resolves but before the server starts accepting traffic.
Returns: Promise<void>
createTables(getDb())
Creates the required database tables (visitors, banned) in the configured database. The CLI equivalent is npx bot-detector load-schema. Call this programmatically when you need to control table creation from code rather than from the CLI.
| Parameter | Type | Description |
|---|---|---|
db | Database | A db0 Database instance to run the schema creation against (pass getDb() to provide the initialized database). |
Returns: Promise<void>
Middleware
detectBots(buildCustomContext?)
Returns an Express RequestHandler. Always call it as a factory: detectBots(). The optional buildCustomContext function runs once per request before any checker executes and populates ctx.custom with typed data you define. When a request passes detection, the middleware sets req.botDetection with success, banned, time, and ipAddress fields.
| Parameter | Type | Description |
|---|---|---|
buildCustomContext | (req: Request) => unknown (optional) | Optional factory that runs per request to build a custom context object passed to checkers. |
Returns: RequestHandler
app.use(
detectBots<ClientSignals>((req) => ({
hasWebDriver: req.headers['x-webdriver'] === 'true',
}))
)
ApiResponse
An Express Router that mounts detectBots() at /check and returns { results: req.botDetection, message: 'Fingerprint logged successfully' }.
Type: express.Router
Usage: mount under a path app.use('/bot', ApiResponse) which will expose POST /bot/check.
import { ApiResponse } from '@riavzon/bot-detector'
app.use('/bot', ApiResponse)
Data Access
getDataSources()
Returns the initialized DataSources instance. Throws if called before defineConfiguration resolves. DataSources exposes synchronous MMDB and LMDB lookup methods for every compiled database: ASN, city, country, Tor nodes, proxies, good bots, FireHOL threat feeds, banned IPs, high-risk IPs, and User-Agent patterns. See the Data Sources guide for the full method reference.
Returns: DataSources
getStorage()
Returns the initialized unstorage Storage instance. Throws if called before defineConfiguration resolves. Use it to read and write shared state from within custom checkers.
Returns: Storage (unstorage)
getBatchQueue()
Returns the initialized BatchQueue instance used for deferred database writes. Throws if called before defineConfiguration resolves. Jobs are deduplicated by key within each flush interval.
Returns: BatchQueue
getDb()
Returns the initialized db0 Database instance. Throws if called before defineConfiguration resolves. Use it for direct SQL access to the visitor persistence layer.
Returns: Database
Utilities
parseUA(uaString)
Parses a User-Agent string and returns a ParsedUAResult with browser name, version, type, OS, device type, vendor, and model.
| Parameter | Type | Description |
|---|---|---|
uaString | `string | number` |
Returns: ParsedUAResult
getGeoData(ip)
Returns the full GeoResponse for any IP address using the compiled MMDB databases. Returns null if the IP is not in the databases. Useful for geo lookups outside the middleware context.
| Parameter | Type | Description |
|---|---|---|
ip | string | IPv4 or IPv6 address to resolve against compiled MMDB data. |
Returns: GeoResponse
banIp(ip, info)
Issues a ufw firewall rule (sudo ufw insert 1 deny from <ip>) to block the IP at the OS level. Only runs when punishmentType.enableFireWallBan is true, returns immediately otherwise. Requires the Node.js process to have passwordless sudo access to ufw.
| Parameter | Type | Description |
|---|---|---|
ip | string | IP address to ban. |
info | BannedInfo | Ban metadata including score and reasons array used for logging. |
Returns: Promise<void> | void
updateIsBot(isBot, cookie)
Updates the is_bot column in the visitors table for the given canary_id.
| Parameter | Type | Description |
|---|---|---|
isBot | boolean | Whether the visitor is a bot. |
cookie | string | The canary_id cookie value identifying the visitor. |
Returns: Promise<void>
updateBannedIP(cookie, ipAddress, country, userAgent, info)
Upserts a row into the banned table with the visitor's canary cookie, IP address, country, User-Agent, ban reasons, and score.
| Parameter | Type | Description |
|---|---|---|
cookie | string | The canary_id cookie value identifying the visitor. |
ipAddress | string | Visitor IP address. |
country | string | Country name or code. |
userAgent | string | Raw User-Agent string. |
info | BannedInfo | Ban metadata (score and reasons) used for the reason/score fields. |
Returns: Promise<void>
updateVisitors(data, cookie, visitorId)
Updates the full fingerprint record in the visitors table for a given canary and visitor ID pair. Returns { success: boolean, reason?: string }.
| Parameter | Type | Description |
|---|---|---|
data | VisitorFingerPrint | Full fingerprint object (userAgent, ipAddress, device info, geo fields, etc.). |
cookie | string | The canary_id cookie value identifying the visitor. |
visitorId | string | Visitor's visitor_id UUID. |
Returns: Promise<{ success: boolean; reason?: string }>
runGeneration()
Programmatic equivalent of npx bot-detector generate. Compiles banned.mmdb and highRisk.mmdb from your database. When generator.deleteAfterBuild is true, source rows are deleted after each successful compile.
Returns: Promise<void>
Checker System
CheckerRegistry
Registry for custom bot checker plugins. Use CheckerRegistry.register(checker) to add a checker that implements IBotChecker. Checkers are partitioned into cheap and heavy phases and filtered by your configuration at runtime. CheckerRegistry.clear() removes all registered checkers, which is useful in tests.
| Method | Signature | Description |
|---|---|---|
register | (checker: IBotChecker<BanReasonCode>) => void | Register a checker plugin instance. |
getEnabled | `(phase: 'cheap' | 'heavy', config: BotDetectorConfig) => IBotChecker |
clear | () => void | Remove all registered checkers (useful in tests). |
BadBotDetected / GoodBotDetected
Error subclasses thrown when a checker conclusively identifies a bad or good bot. Re-exported from helpers/exceptions for use in custom checkers and error-handling middleware.
| Constructor Parameter | Type | Description |
|---|---|---|
message | string (optional) | Optional error message; defaults to 'Bad bot detected' or 'Good bot detected' respectively. |
TypeScript Types
| Type | Description |
|---|---|
BotDetectorConfig | Fully resolved configuration object with all defaults applied. |
BotDetectorConfigInput | Input shape for defineConfiguration. Only store.main is required. |
ValidationContext<TCustom> | Per-request context passed to every checker. |
IBotChecker<Code, TCustom> | Interface for custom checker classes. |
GeoResponse | Object returned by getGeoData. |
ParsedUAResult | Object returned by parseUA. |
BannedInfo | { score: number; reasons: BanReasonCode[] }. |
BanReasonCode | Union of all ban reason string literals. |
DbConfig | Discriminated union of all supported database driver configs. |
SupportedDbDrivers | Interface mapping each driver key to its options type. |
CacheConfig | Discriminated union of all supported cache driver configs. |
VisitorFingerPrint | Shape of the visitor record in the visitors table. |
ThreatRecordModified | FireHOL threat record extended with a network CIDR field. |