-
Notifications
You must be signed in to change notification settings - Fork 84
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
feat: expose detectors and separate detect and collect functions #144
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a153850
make it better
Le0Developer 1e5294a
add collect and detectors arguments to collect and detect
Le0Developer 89004ed
collect browser info
Le0Developer 6fe07cb
remove dependence on browser apis
Le0Developer 036e711
not used
Le0Developer b16e8f5
fix wrong access
Le0Developer a218454
Merge branch 'main' into feat/better-api
Le0Developer 2056ef4
fix merge
Le0Developer b4eebbc
fix circular import
Le0Developer 9111960
add type dependency between components and detectors
Le0Developer 980248c
style
Le0Developer 6f4063e
split browser info source
Le0Developer 8664514
move small sources into index.ts
Le0Developer 7efa6d8
remove unused function wrapper
Le0Developer 78cab34
Merge branch 'main' into feat/better-api
Le0Developer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,74 @@ | ||
import { | ||
AbstractDetectorDict, | ||
AbstractSourceDict, | ||
BotdError, | ||
BotDetectionResult, | ||
BotKind, | ||
Component, | ||
ComponentDict, | ||
DetectionDict, | ||
State, | ||
} from './types' | ||
|
||
export function detect<T extends ComponentDict, K extends AbstractDetectorDict<T>>( | ||
components: T, | ||
detectors: K, | ||
): [DetectionDict<K>, BotDetectionResult] { | ||
const detections = {} as DetectionDict<K> | ||
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<T extends AbstractSourceDict>(sources: T): Promise<ComponentDict<T>> { | ||
const components = {} as ComponentDict<T> | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,24 @@ | ||
import { arrayIncludes } from '../utils/ponyfills' | ||
import { BrowserEngineKind, BrowserKind, ComponentDict, DetectorResponse, State } from '../types' | ||
import { getBrowserEngineKind, getBrowserKind } from '../utils/browser' | ||
|
||
export function detectEvalLengthInconsistency({ evalLength }: ComponentDict): DetectorResponse { | ||
if (evalLength.state !== State.Success) return | ||
export function detectEvalLengthInconsistency({ | ||
evalLength, | ||
browserKind, | ||
browserEngineKind, | ||
}: ComponentDict): DetectorResponse { | ||
if ( | ||
evalLength.state !== State.Success || | ||
browserKind.state !== State.Success || | ||
browserEngineKind.state !== State.Success | ||
) | ||
return | ||
|
||
const length = evalLength.value | ||
const browser = getBrowserKind() | ||
const browserEngine = getBrowserEngineKind() | ||
if (browserEngine == BrowserEngineKind.Unknown) { | ||
if (browserEngineKind.value === BrowserEngineKind.Unknown) | ||
return false | ||
} | ||
return ( | ||
(length === 37 && !arrayIncludes([BrowserEngineKind.Webkit, BrowserEngineKind.Gecko], browserEngine)) || | ||
(length === 39 && !arrayIncludes([BrowserKind.IE], browser)) || | ||
(length === 33 && !arrayIncludes([BrowserEngineKind.Chromium], browserEngine)) | ||
(length === 37 && !arrayIncludes([BrowserEngineKind.Webkit, BrowserEngineKind.Gecko], browserEngineKind.value)) || | ||
(length === 39 && !arrayIncludes([BrowserKind.IE], browserKind.value)) || | ||
(length === 33 && !arrayIncludes([BrowserEngineKind.Chromium], browserEngineKind.value)) | ||
) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,23 @@ | ||
import { BotKind, BrowserEngineKind, BrowserKind, ComponentDict, DetectorResponse, State } from '../types' | ||
import { getBrowserEngineKind, getBrowserKind, isAndroid } from '../utils/browser' | ||
|
||
export function detectPluginsLengthInconsistency({ pluginsLength }: ComponentDict): DetectorResponse { | ||
if (pluginsLength.state !== State.Success) return | ||
const browserKind = getBrowserKind() | ||
const browserEngineKind = getBrowserEngineKind() | ||
if (browserKind !== BrowserKind.Chrome || isAndroid() || browserEngineKind !== BrowserEngineKind.Chromium) return | ||
export function detectPluginsLengthInconsistency({ | ||
pluginsLength, | ||
android, | ||
browserKind, | ||
browserEngineKind, | ||
}: ComponentDict): DetectorResponse { | ||
if ( | ||
pluginsLength.state !== State.Success || | ||
android.state !== State.Success || | ||
browserKind.state !== State.Success || | ||
browserEngineKind.state !== State.Success | ||
) | ||
return | ||
if ( | ||
browserKind.value !== BrowserKind.Chrome || | ||
android.value || | ||
browserEngineKind.value !== BrowserEngineKind.Chromium | ||
) | ||
return | ||
if (pluginsLength.value === 0) return BotKind.HeadlessChrome | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
import { BotKind, ComponentDict, DetectorResponse, State } from '../types' | ||
import { isAndroid } from '../utils/browser' | ||
|
||
export function detectRTT({ rtt }: ComponentDict): DetectorResponse { | ||
if (rtt.state !== State.Success) return | ||
export function detectRTT({ rtt, android }: ComponentDict): DetectorResponse { | ||
if (rtt.state !== State.Success || android.state !== State.Success) return | ||
// Rtt is 0 on android webview | ||
if (isAndroid()) return | ||
if (android.value) return | ||
if (rtt.value === 0) return BotKind.HeadlessChrome | ||
} |
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 |
---|---|---|
@@ -1,10 +1,9 @@ | ||
import { BotKind, ComponentDict, DetectorResponse, State } from '../types' | ||
import { getDocumentFocus } from '../utils/browser' | ||
|
||
export function detectWindowSize({ windowSize }: ComponentDict): DetectorResponse { | ||
if (windowSize.state !== State.Success) return false | ||
export function detectWindowSize({ windowSize, documentFocus }: ComponentDict): DetectorResponse { | ||
if (windowSize.state !== State.Success || documentFocus.state !== State.Success) return false | ||
const { outerWidth, outerHeight } = windowSize.value | ||
// When a page is opened in a new tab without focusing it right away, the window outer size is 0x0 | ||
if (!getDocumentFocus()) return | ||
if (!documentFocus.value) return | ||
if (outerWidth === 0 && outerHeight === 0) return BotKind.HeadlessChrome | ||
} |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, remove
.value
from enums.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also @Le0Developer please, merge new changes from
main