@@ -3,11 +3,17 @@ import { db } from '@db';
33import type { SupportedOS } from './types' ;
44
55/**
6- * Detects the operating system from a User-Agent string
6+ * Detects the operating system (and for macOS, the CPU architecture) from a User-Agent string.
7+ *
8+ * Returns:
9+ * - 'windows' for Windows OS
10+ * - 'macos' for Apple Silicon (ARM-based) Macs
11+ * - 'macos-intel' for Intel-based Macs
712 *
813 * Examples of User-Agent strings:
914 * - Windows: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
10- * - macOS: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36"
15+ * - macOS (Intel): "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36"
16+ * - macOS (Apple Silicon): "Mozilla/5.0 (Macintosh; ARM Mac OS X 11_2_3) AppleWebKit/537.36"
1117 */
1218export function detectOSFromUserAgent ( userAgent : string | null ) : SupportedOS | null {
1319 if ( ! userAgent ) return null ;
@@ -19,8 +25,17 @@ export function detectOSFromUserAgent(userAgent: string | null): SupportedOS | n
1925 return 'windows' ;
2026 }
2127
22- // Check for macOS (must check before iOS since iOS UA contains "mac" )
28+ // Check for macOS (and further distinguish Apple Silicon vs Intel )
2329 if ( ua . includes ( 'macintosh' ) || ( ua . includes ( 'mac os' ) && ! ua . includes ( 'like mac' ) ) ) {
30+ // User-Agent containing 'arm' or 'apple' usually means Apple Silicon
31+ if ( ua . includes ( 'arm' ) || ua . includes ( 'apple' ) ) {
32+ return 'macos' ;
33+ }
34+ // 'intel' in UA indicates Intel-based mac
35+ if ( ua . includes ( 'intel' ) ) {
36+ return 'macos-intel' ;
37+ }
38+ // Fallback for when arch info is missing, treat as Apple Silicon (modern default)
2439 return 'macos' ;
2540 }
2641
0 commit comments