Open History to see the last commit
1import type {
2 AuthClientMetadataDeviceType,
3 AuthClientPresentationMetadata,
4 ClientOs,
5 DesktopBridge,
6} from "@t3tools/contracts";
7
8export interface BrowserIdentity {
9 readonly userAgent: string;
10 readonly platform: string;
11 readonly maxTouchPoints: number;
12}
13
14function clientOsFromElectronPlatform(platform: string | undefined): ClientOs {
15 switch (platform) {
16 case "darwin":
17 return "macOS";
18 case "win32":
19 return "Windows";
20 case "linux":
21 return "Linux";
22 default:
23 return platform ? "other" : "unknown";
24 }
25}
26
27function isIpadosDesktopUserAgent(identity: BrowserIdentity): boolean {
28 return (
29 /macintosh/i.test(identity.userAgent) &&
30 /mac/i.test(identity.platform) &&
31 identity.maxTouchPoints > 1
32 );
33}
34
35export function browserClientOs(identity: BrowserIdentity): ClientOs {
36 const userAgent = identity.userAgent;
37 if (userAgent.trim() === "") return "unknown";
38 if (/iphone|ipad|ipod/i.test(userAgent) || isIpadosDesktopUserAgent(identity)) return "iOS";
39 if (/android/i.test(userAgent)) return "Android";
40 if (/cros/i.test(userAgent)) return "ChromeOS";
41 if (/windows/i.test(userAgent)) return "Windows";
42 if (/macintosh|mac os x/i.test(userAgent)) return "macOS";
43 if (/linux|x11/i.test(userAgent)) return "Linux";
44 return "other";
45}
46
47function browserFamily(userAgent: string): string {
48 if (userAgent.trim() === "") return "unknown";
49 if (/edg(?:e|a|ios)?\//i.test(userAgent)) return "Edge";
50 if (/opr\/|opios\//i.test(userAgent)) return "Opera";
51 if (/samsungbrowser\//i.test(userAgent)) return "Samsung Internet";
52 if (/firefox\/|fxios\//i.test(userAgent)) return "Firefox";
53 if (/chrome\/|crios\//i.test(userAgent)) return "Chrome";
54 if (/safari\//i.test(userAgent)) return "Safari";
55 return "other";
56}
57
58function browserDeviceType(identity: BrowserIdentity): AuthClientMetadataDeviceType {
59 const userAgent = identity.userAgent;
60 if (userAgent.trim() === "") return "unknown";
61 if (
62 /ipad|tablet|kindle|silk/i.test(userAgent) ||
63 (/android/i.test(userAgent) && !/mobile/i.test(userAgent)) ||
64 isIpadosDesktopUserAgent(identity)
65 ) {
66 return "tablet";
67 }
68 if (/iphone|ipod|android.+mobile|mobile/i.test(userAgent)) return "mobile";
69 return "desktop";
70}
71
72export function clientPresentationMetadata(input: {
73 readonly appVersion: string;
74 readonly hosted: boolean;
75 readonly identity: BrowserIdentity;
76 readonly desktopBridge: Pick<DesktopBridge, "getClientPlatform"> | undefined;
77}): AuthClientPresentationMetadata {
78 if (input.desktopBridge !== undefined) {
79 return {
80 label: "T3 Code Desktop",
81 deviceType: "desktop",
82 os: clientOsFromElectronPlatform(input.desktopBridge.getClientPlatform()),
83 surface: "desktop",
84 ...(input.appVersion === "0.0.0" ? {} : { appVersion: input.appVersion }),
85 };
86 }
87
88 return {
89 label: "T3 Code Web",
90 deviceType: browserDeviceType(input.identity),
91 os: browserClientOs(input.identity),
92 surface: "web",
93 webDeployment: input.hosted ? "hosted" : "server",
94 browser: browserFamily(input.identity.userAgent),
95 ...(input.appVersion === "0.0.0" ? {} : { appVersion: input.appVersion }),
96 };
97}