forked from foolip/mdn-bcd-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ua-parser.js
154 lines (134 loc) · 4.44 KB
/
ua-parser.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//
// mdn-bcd-collector: ua-parser.js
// Module to parse user agent strings and compare them with BCD browser data
//
// © Gooborg Studios, Google LLC
// See the LICENSE file for copyright details
//
import {
compare as compareVersions,
compareVersions as compareVersionsSort
} from 'compare-versions';
import uaParser from 'ua-parser-js';
const getMajorVersion = (version) => {
return version.split('.')[0];
};
const getMajorMinorVersion = (version) => {
const [major, minor] = version.split('.');
return `${major}.${minor || 0}`;
};
const parseUA = (userAgent, browsers) => {
const ua = uaParser(userAgent);
const data = {
browser: {id: null, name: null},
version: null,
fullVersion: null,
os: {name: null, version: null},
inBcd: undefined
};
if (!ua.browser.name) {
return data;
}
data.browser.id = ua.browser.name.toLowerCase().replace(/ /g, '_');
data.browser.name = ua.browser.name;
data.os.name = ua.os.name || '';
data.os.version = ua.os.version || '';
switch (data.browser.id) {
case 'mobile_safari':
data.browser.id = 'safari';
break;
case 'oculus_browser':
data.browser.id = 'oculus';
break;
case 'samsung_browser':
data.browser.id = 'samsunginternet';
break;
case 'android_browser':
case 'chrome_webview':
data.browser.id = 'webview';
break;
}
const os = data.os.name.toLowerCase();
if (os === 'android') {
data.browser.id += '_android';
data.browser.name += ' Android';
if (ua.browser.name === 'Android Browser') {
// For early WebView Android, use the OS version
data.fullVersion = compareVersions(ua.os.version, '5.0', '<')
? ua.os.version
: ua.engine.version;
}
} else if (os === 'ios') {
data.browser.id += '_ios';
data.browser.name += ' iOS';
// https://github.com/mdn/browser-compat-data/blob/main/docs/data-guidelines.md#safari-for-ios-versioning
data.fullVersion = ua.os.version;
}
data.fullVersion = data.fullVersion || ua.browser.version;
data.version = getMajorMinorVersion(data.fullVersion);
if (!(data.browser.id in browsers)) {
return data;
}
data.browser.name = browsers[data.browser.id].name;
data.inBcd = false;
const versions = Object.keys(browsers[data.browser.id].releases);
versions.sort(compareVersionsSort);
// Android 4.4.3 needs to be handled as a special case, because its data
// differs from 4.4, and the code below will strip out the patch versions from
// our version numbers.
if (
data.browser.id === 'webview_android' &&
compareVersions(data.fullVersion, '4.4.3', '>=') &&
compareVersions(data.fullVersion, '5.0', '<')
) {
data.version = '4.4.3';
data.inBcd = true;
return data;
}
// Certain Safari versions are backports of newer versions, but contain less
// features, particularly ones involving OS integration. We are explicitly
// marking these versions as "not in BCD" to avoid confusion.
if (
data.browser.id === 'safari' &&
['4.1', '6.1', '6.2', '7.1'].includes(data.version)
) {
return data;
}
// The |version| from the UA string is typically more precise than |versions|
// from BCD, and some "uninteresting" releases are missing from BCD. To deal
// with this, find the pair of versions in |versions| that sandwiches
// |version|, and use the first of this pair. For example, given |version|
// "10.1" and |versions| entries "10.0" and "10.2", return "10.0".
for (let i = 0; i < versions.length - 1; i++) {
const current = versions[i];
const next = versions[i + 1];
if (
compareVersions(data.version, current, '>=') &&
compareVersions(data.version, next, '<')
) {
data.inBcd = true;
data.version = current;
break;
}
}
// We reached the last entry in |versions|. With no |next| to compare against
// we have to check if it looks like a significant release or not. By default
// that means a new major version, but for Safari and Samsung Internet the
// major and minor version are significant.
let normalize = getMajorVersion;
if (
data.browser.id.startsWith('safari') ||
data.browser.id === 'samsunginternet_android'
) {
normalize = getMajorMinorVersion;
}
if (
data.inBcd == false &&
normalize(data.version) === normalize(versions[versions.length - 1])
) {
data.inBcd = true;
data.version = versions[versions.length - 1];
}
return data;
};
export {getMajorMinorVersion, parseUA};