Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Replace runtime enum BotKind with plain object #136

Merged
merged 4 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/detectors/distinctive_properties.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { BotKind, ComponentDict, DetectorResponse, State } from '../types'
import { BotKindType, ComponentDict, DetectorResponse, State } from '../types'

export function detectDistinctiveProperties({ distinctiveProps }: ComponentDict): DetectorResponse {
if (distinctiveProps.state !== State.Success) return false
const value = distinctiveProps.value
let bot: BotKind
let bot: BotKindType
for (bot in value) if (value[bot]) return bot
}
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { version } from '../package.json'
import BotDetector from './detector'
import { sources, WindowSizePayload, ProcessPayload, DistinctivePropertiesPayload } from './sources'
import { BotdError, BotDetectorInterface, BotKind, BotDetectionResult } from './types'
import { BotdError, BotDetectorInterface, BotKind, BotKindType, BotDetectionResult } from './types'

/**
* Options for BotD loading
Expand Down Expand Up @@ -53,5 +53,6 @@ export {
ProcessPayload,
DistinctivePropertiesPayload,
BotDetectionResult,
BotKind,
BotKindType as BotKind,
BotKind as BotKindEnum,
Finesse marked this conversation as resolved.
Show resolved Hide resolved
}
8 changes: 4 additions & 4 deletions src/sources/distinctive_properties.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { BotKind } from '../types'
import { BotKind, BotKindType } from '../types'
import { getObjectProps, includes } from '../utils/misc'

export type DistinctivePropertiesPayload = Partial<Record<BotKind, boolean>>
export type DistinctivePropertiesPayload = Partial<Record<BotKindType, boolean>>

export default function checkDistinctiveProperties(): DistinctivePropertiesPayload {
type PropsList = Partial<Record<'window' | 'document', (string | RegExp)[]>>
// The order in the following list matters, because specific types of bots come first, followed by automation technologies.
const distinctivePropsList: Partial<Record<BotKind, PropsList>> = {
const distinctivePropsList: Partial<Record<BotKindType, PropsList>> = {
[BotKind.Awesomium]: {
window: ['awesomium'],
},
Expand Down Expand Up @@ -75,7 +75,7 @@ export default function checkDistinctiveProperties(): DistinctivePropertiesPaylo
window: ['domAutomation', 'domAutomationController'],
},
}
let botName: BotKind
let botName: BotKindType
const result: DistinctivePropertiesPayload = {}
const windowProps = getObjectProps(window)
let documentProps: string[] = []
Expand Down
50 changes: 26 additions & 24 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { sources } from './sources'
export type BotDetectionResult =
| {
bot: true
botKind: BotKind
botKind: BotKindType
}
| {
bot: false
Expand All @@ -31,29 +31,31 @@ export const enum State {
* @readonly
* @enum {string}
*/
export enum BotKind {
Awesomium = 'awesomium',
Cef = 'cef',
CefSharp = 'cefsharp',
CoachJS = 'coachjs',
Electron = 'electron',
FMiner = 'fminer',
Geb = 'geb',
NightmareJS = 'nightmarejs',
Phantomas = 'phantomas',
PhantomJS = 'phantomjs',
Rhino = 'rhino',
Selenium = 'selenium',
Sequentum = 'sequentum',
SlimerJS = 'slimerjs',
WebDriverIO = 'webdriverio',

WebDriver = 'webdriver',
HeadlessChrome = 'headless_chrome',
Unknown = 'unknown',
}

export type DetectorResponse = boolean | BotKind | undefined
export const BotKind = {
Finesse marked this conversation as resolved.
Show resolved Hide resolved
Awesomium: 'awesomium',
Cef: 'cef',
CefSharp: 'cefsharp',
CoachJS: 'coachjs',
Electron: 'electron',
FMiner: 'fminer',
Geb: 'geb',
NightmareJS: 'nightmarejs',
Phantomas: 'phantomas',
PhantomJS: 'phantomjs',
Rhino: 'rhino',
Selenium: 'selenium',
Sequentum: 'sequentum',
SlimerJS: 'slimerjs',
WebDriverIO: 'webdriverio',

WebDriver: 'webdriver',
HeadlessChrome: 'headless_chrome',
Unknown: 'unknown',
} as const

export type BotKindType = typeof BotKind[keyof typeof BotKind]

export type DetectorResponse = boolean | BotKindType | undefined

/**
* Represents a component with state and value.
Expand Down
Loading