forked from foolip/mdn-bcd-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ua-parser.js
137 lines (118 loc) · 4.06 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
//
// mdn-bcd-collector: ua-parser.js
// Module to parse user agent strings and compare them with BCD browser data
//
// © Gooborg Studios, Google LLC
// See LICENSE.txt for copyright details
//
import compareVersions from 'compare-versions';
import uaParser from 'ua-parser-js';
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 '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.compare(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(compareVersions);
// 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.compare(data.fullVersion, '4.4.3', '>=') &&
compareVersions.compare(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.compare(data.version, current, '>=') &&
compareVersions.compare(data.version, next, '<')
) {
data.inBcd = true;
data.version = current;
break;
}
}
// This is the last entry in |versions|. With no |next| to compare against
// we have to check that the major versions match. Given |version| "10.3"
// and |versions| entries "10.0" and "10.2", return "10.2". Given |version|
// "11.0", skip.
if (
data.inBcd == false &&
data.version.split('.')[0] === versions[versions.length - 1].split('.')[0]
) {
data.inBcd = true;
data.version = versions[versions.length - 1];
}
return data;
};
export {getMajorMinorVersion, parseUA};