diff options
Diffstat (limited to 'src/helpers/userAgent-helpers.ts')
-rw-r--r-- | src/helpers/userAgent-helpers.ts | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/helpers/userAgent-helpers.ts b/src/helpers/userAgent-helpers.ts new file mode 100644 index 000000000..73c8bfd03 --- /dev/null +++ b/src/helpers/userAgent-helpers.ts | |||
@@ -0,0 +1,41 @@ | |||
1 | import os from 'os'; | ||
2 | import macosVersion from 'macos-version'; | ||
3 | import { chrome } from 'useragent-generator'; | ||
4 | import { | ||
5 | chromeVersion, isMac, isWindows, is64Bit, osArch, osRelease, | ||
6 | } from '../environment'; | ||
7 | |||
8 | function macOS() { | ||
9 | const version = macosVersion() || ''; | ||
10 | let cpuName = os.cpus()[0].model.split(' ')[0]; | ||
11 | if (cpuName && cpuName.match(/\(/)) { | ||
12 | cpuName = cpuName.split('(')[0]; | ||
13 | } | ||
14 | return `Macintosh; ${cpuName} Mac OS X ${version.replace(/\./g, '_')}`; | ||
15 | } | ||
16 | |||
17 | function windows() { | ||
18 | const version = osRelease; | ||
19 | const [majorVersion, minorVersion] = version.split('.'); | ||
20 | const archString = is64Bit ? 'Win64' : 'Win32'; | ||
21 | return `Windows NT ${majorVersion}.${minorVersion}; ${archString}; ${osArch}`; | ||
22 | } | ||
23 | |||
24 | function linux() { | ||
25 | const archString = is64Bit ? 'x86_64' : osArch; | ||
26 | return `X11; Ubuntu; Linux ${archString}`; | ||
27 | } | ||
28 | |||
29 | export default function userAgent() { | ||
30 | let platformString = ''; | ||
31 | |||
32 | if (isMac) { | ||
33 | platformString = macOS(); | ||
34 | } else if (isWindows) { | ||
35 | platformString = windows(); | ||
36 | } else { | ||
37 | platformString = linux(); | ||
38 | } | ||
39 | |||
40 | return chrome({ os: platformString, version: chromeVersion }); | ||
41 | } | ||