@naturalcycles/js-lib
Version:
Standard library for universal (browser + Node.js) javascript
55 lines (54 loc) • 1.83 kB
TypeScript
import { BotReason } from '../bot.model.js';
export interface BotDetectionServiceCfg {
/**
* Defaults to true.
* Set to false if you need to disable it under certain conditions (e.g in Angular unit tests so it doesn't scream 'Error!')
*/
enabled?: boolean;
/**
* Defaults to false.
* If true - the instance will memoize (remember) the results of the detection
* and won't re-run it.
*/
memoizeResults?: boolean;
/**
* Defaults to false.
* If set to true: `getBotReason()` would return BotReason.CDP if CDP is detected.
* Otherwise - `getBotReason()` will not perform the CDP check.
*/
treatCDPAsBotReason?: boolean;
}
/**
* Service to detect bots and CDP (Chrome DevTools Protocol).
*
* @experimental
*/
export declare class BotDetectionService {
cfg: BotDetectionServiceCfg;
constructor(cfg?: BotDetectionServiceCfg);
private botReason;
private cdp;
isBotOrCDP(): boolean;
isBot(): boolean;
/**
* Returns null if it's not a Bot,
* otherwise a truthy BotReason.
*/
getBotReason(): BotReason | null;
private detectBotReason;
/**
* CDP stands for Chrome DevTools Protocol.
* This function tests if the current environment is a CDP environment.
* If it's true - it's one of:
*
* 1. Bot, automated with CDP, e.g Puppeteer, Playwright or such.
* 2. Developer with Chrome DevTools open.
*
* 2 is certainly not a bot, but unfortunately we can't distinguish between the two.
* That's why this function is not part of `isBot()`, because it can give "false positive" with DevTools.
*
* Based on: https://deviceandbrowserinfo.com/learning_zone/articles/detecting-headless-chrome-puppeteer-2024
*/
isCDP(): boolean;
private detectCDP;
}