-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
see the linked issue/pr for more information on motivation
- Loading branch information
1 parent
4f5c28d
commit a153850
Showing
3 changed files
with
72 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { detectors } from './detectors' | ||
import { sources } from './sources' | ||
import { BotdError, BotDetectionResult, BotKind, Component, ComponentDict, DetectionDict, State } from './types' | ||
|
||
export function detect(components: ComponentDict): [DetectionDict, BotDetectionResult] { | ||
const detections = {} as DetectionDict | ||
let finalDetection: BotDetectionResult = { | ||
bot: false, | ||
} | ||
|
||
for (const detectorName in detectors) { | ||
const detector = detectors[detectorName as keyof typeof detectors] | ||
const detectorRes = detector(components) | ||
|
||
let detection: BotDetectionResult = { bot: false } | ||
|
||
if (typeof detectorRes === 'string') { | ||
detection = { bot: true, botKind: detectorRes } | ||
} else if (detectorRes) { | ||
detection = { bot: true, botKind: BotKind.Unknown } | ||
} | ||
|
||
detections[detectorName as keyof typeof detectors] = detection | ||
|
||
if (detection.bot) { | ||
finalDetection = detection | ||
} | ||
} | ||
|
||
return [detections, finalDetection] | ||
} | ||
|
||
export async function collect(): Promise<ComponentDict> { | ||
const components = {} as ComponentDict | ||
const sourcesKeys = Object.keys(sources) as (keyof typeof sources)[] | ||
|
||
await Promise.all( | ||
sourcesKeys.map(async (sourceKey) => { | ||
const res = sources[sourceKey] | ||
|
||
try { | ||
components[sourceKey] = ({ | ||
value: await res(), | ||
state: State.Success, | ||
} as Component<any>) as any | ||
} catch (error) { | ||
if (error instanceof BotdError) { | ||
components[sourceKey] = { | ||
state: error.state, | ||
error: `${error.name}: ${error.message}`, | ||
} | ||
} else { | ||
components[sourceKey] = { | ||
state: State.UnexpectedBehaviour, | ||
error: error instanceof Error ? `${error.name}: ${error.message}` : String(error), | ||
} | ||
} | ||
} | ||
}), | ||
) | ||
|
||
return components | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters