-
Notifications
You must be signed in to change notification settings - Fork 7
/
Util.js
102 lines (87 loc) · 2.42 KB
/
Util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { platform, release } from 'os'
const HOST_REGEX = /^(?!\w+:\/\/)([\w-:]+\.)+([\w-:]+)(?::(\d+))?(?!:)$/
export function isHost (host) {
return HOST_REGEX.test(host)
}
export function isNode () {
return typeof process !== 'undefined' && !process.browser
}
export function getNodeVersion () {
return process.versions.node ? `v${process.versions.node}` : process.version
}
function isReactNative () {
return typeof window !== 'undefined' && 'navigator' in window && 'product' in window.navigator && window.navigator.product === 'ReactNative'
}
function getBrowserOS () {
if (!window) {
return null
}
const userAgent = window.navigator.userAgent
const platform = window.navigator.platform
const macosPlatforms = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K']
const windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE']
const iosPlatforms = ['iPhone', 'iPad', 'iPod']
let os = null
if (macosPlatforms.indexOf(platform) !== -1) {
os = 'macOS'
} else if (iosPlatforms.indexOf(platform) !== -1) {
os = 'iOS'
} else if (windowsPlatforms.indexOf(platform) !== -1) {
os = 'Windows'
} else if (/Android/.test(userAgent)) {
os = 'Android'
} else if (/Linux/.test(platform)) {
os = 'Linux'
}
return os
}
function getNodeOS () {
const os = platform() || 'linux'
const version = release() || '0.0.0'
const osMap = {
android: 'Android',
aix: 'Linux',
darwin: 'macOS',
freebsd: 'Linux',
linux: 'Linux',
openbsd: 'Linux',
sunos: 'Linux',
win32: 'Windows'
}
if (os in osMap) {
return `${osMap[os] || 'Linux'}/${version}`
}
return null
}
export default function getUserAgent (sdk, application, integration, feature) {
const headerParts = []
if (application) {
headerParts.push(`app ${application}`)
}
if (integration) {
headerParts.push(`integration ${integration}`)
}
if (feature) {
headerParts.push('feature ' + feature)
}
headerParts.push(`sdk ${sdk}`)
let os = null
try {
if (isReactNative()) {
os = getBrowserOS()
headerParts.push('platform ReactNative')
} else if (isNode()) {
os = getNodeOS()
headerParts.push(`platform node.js/${getNodeVersion()}`)
} else {
os = getBrowserOS()
headerParts.push(`platform browser`)
}
} catch (e) {
os = null
}
if (os) {
headerParts.push(`os ${os}`)
}
return `${headerParts.filter((item) => item !== '').join('; ')};`
}