Files
dataroom/lib/utils/user-agent.ts

40 lines
1002 B
TypeScript

import { UAParser } from "ua-parser-js";
export function isBot(input: string) {
return /bot|chatgpt|Googlebot|Mediapartners-Google|AdsBot-Google|googleweblight|Storebot-Google|Google-PageRenderer|Bingbot|BingPreview|Slurp|DuckDuckBot|baiduspider|yandex|sogou|LinkedInBot|bitlybot|tumblr|vkShare|quora link preview|facebookexternalhit|facebookcatalog|Twitterbot|applebot|redditbot|Slackbot|Discordbot|WhatsApp|SkypeUriPreview|ia_archiver/i.test(
input,
);
}
export function userAgentFromString(input: string | undefined): UserAgent {
return {
...new UAParser(input).getResult(),
isBot: input === undefined ? false : isBot(input),
};
}
interface UserAgent {
isBot: boolean;
ua: string;
browser: {
name?: string;
version?: string;
};
device: {
model?: string;
type?: string;
vendor?: string;
};
engine: {
name?: string;
version?: string;
};
os: {
name?: string;
version?: string;
};
cpu: {
architecture?: string;
};
}