-
Notifications
You must be signed in to change notification settings - Fork 30
/
index.ts
284 lines (240 loc) · 6.88 KB
/
index.ts
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import browserslist from 'browserslist';
import semver from 'semver';
import UAParser from 'ua-parser-js';
// @see https://github.com/ai/browserslist#browsers
// map of equivalent browsers,
// see https://github.com/ai/browserslist/issues/156
const browserNameMap: Record<string, string> = {
bb: 'BlackBerry',
and_chr: 'Chrome',
ChromeAndroid: 'Chrome',
FirefoxAndroid: 'Firefox',
ff: 'Firefox',
ie_mob: 'ExplorerMobile',
ie: 'Explorer',
and_ff: 'Firefox',
ios_saf: 'iOS',
op_mini: 'OperaMini',
op_mob: 'OperaMobile',
and_qq: 'QQAndroid',
and_uc: 'UCAndroid',
}
function resolveUserAgent(uaString: string): { family: string | null, version: string | null } {
const parsedUA = UAParser(uaString)
const parsedBrowserVersion = semverify(parsedUA.browser.version)
const parsedOSVersion = semverify(parsedUA.os.version)
const parsedEngineVersion = semverify(parsedUA.engine.version)
// Case A: For Safari on iOS, the use the browser version
if (
parsedUA.browser.name === 'Safari' && parsedUA.os.name === 'iOS') {
return {
family: 'iOS',
version: parsedBrowserVersion,
}
}
// Case B: The browser on iOS didn't report as safari,
// so we use the iOS version as a proxy to the browser
// version. This is based on the assumption that the
// underlying Safari Engine used will be *atleast* equal
// to the iOS version it's running on.
if (parsedUA.os.name === 'iOS') {
return {
family: 'iOS',
version: parsedOSVersion
}
}
if (
(parsedUA.browser.name === 'Opera' && parsedUA.device.type === 'mobile') ||
parsedUA.browser.name === 'Opera Mobi'
) {
return {
family: 'OperaMobile',
version: parsedBrowserVersion
}
}
if (parsedUA.browser.name === 'Samsung Browser') {
return {
family: 'Samsung',
version: parsedBrowserVersion
}
}
if (parsedUA.browser.name === 'IE') {
return {
family: 'Explorer',
version: parsedBrowserVersion
}
}
if (parsedUA.browser.name === 'IEMobile') {
return {
family: 'ExplorerMobile',
version: parsedBrowserVersion
}
}
// Use engine version for gecko-based browsers
if (parsedUA.engine.name === 'Gecko') {
return {
family: 'Firefox',
version: parsedEngineVersion
}
}
// Use engine version for blink-based browsers
if (parsedUA.engine.name === 'Blink') {
return {
family: 'Chrome',
version: parsedEngineVersion
}
}
// Chrome based browsers pre-blink (WebKit)
if (
parsedUA.browser.name &&
['Chrome', 'Chromium', 'Chrome WebView', 'Chrome Headless'].includes(parsedUA.browser.name)
) {
return {
family: 'Chrome',
version: parsedBrowserVersion
}
}
if (parsedUA.browser.name === 'Android Browser') {
// Versions prior to Blink were based
// on the OS version. Only after this
// did android start using system chrome for web-views
return {
family: 'Android',
version: parsedOSVersion
}
}
return {
family: parsedUA.browser.name || null,
version: parsedBrowserVersion
}
}
// Convert version to a semver value.
// 2.5 -> 2.5.0; 1 -> 1.0.0;
const semverify = (version: string | undefined | null) => {
if (!version) {
return null
}
const cooerced = semver.coerce(version, { loose: true })
if (!cooerced) {
return null
}
return cooerced.version
}
// 10.0-10.2 -> 10.0, 10.1, 10.2
function generateSemversInRange(versionRange: string) {
const [start, end] = versionRange.split('-')
const startSemver = semverify(start)
const endSemver = semverify(end)
if (!startSemver || !endSemver) {
return []
}
const versionsInRange = []
let curVersion = startSemver
while (semver.gte(endSemver, curVersion)) {
versionsInRange.push(curVersion)
curVersion = semver.inc(curVersion, 'minor') as string
}
return versionsInRange
}
function normalizeQuery(query: string) {
let normalizedQuery = query
const regex = `(${Object.keys(browserNameMap).join('|')})`
const match = query.match(new RegExp(regex))
if (match) {
normalizedQuery = query.replace(match[0], browserNameMap[match[0]])
}
return normalizedQuery
}
const parseBrowsersList = (browsersList: string[]): { family: string, version: string | null }[] => {
const browsers = browsersList
.map((browser) => {
const [name, version] = browser.split(' ')
return { name, version }
})
// #38 Filter out non-numerical browser versions
.filter((browser) => browser.version !== 'TP')
.map((browser) => {
let normalizedName = browser.name
let normalizedVersion = browser.version
if (browser.name in browserNameMap) {
normalizedName = browserNameMap[browser.name]
}
// browserslist might return ranges (9.0-9.2), unwrap them
// see https://github.com/browserslist/browserslist-useragent/issues/41
if (browser.version.indexOf('-') > 0) {
return generateSemversInRange(browser.version).map((version) => ({
family: normalizedName,
version,
}))
} else {
return {
family: normalizedName,
version: normalizedVersion,
}
}
})
return browsers.flat()
}
const compareBrowserSemvers = (versionA: string, versionB: string, options: Options) => {
const semverifiedA = semverify(versionA)
const semverifiedB = semverify(versionB)
if (!semverifiedA || !semverifiedB) {
return false
}
let referenceVersion = semverifiedB
if (options.ignorePatch) {
referenceVersion = `~${semverifiedB}`
}
if (options.ignoreMinor) {
referenceVersion = `^${semverifiedB}`
}
if (options.allowHigherVersions) {
return semver.gte(semverifiedA, semverifiedB)
} else {
return semver.satisfies(semverifiedA, referenceVersion)
}
}
type Options = {
browsers?: string[],
env?: string,
path?: string,
ignoreMinor?: boolean,
ignorePatch?: boolean,
allowHigherVersions?: boolean
}
const matchesUA = (uaString: string, opts: Options = {}) => {
// bail out early if the user agent is invalid
if (!uaString) {
return false
}
let normalizedQuery
if (opts.browsers) {
normalizedQuery = opts.browsers.map(normalizeQuery)
}
const browsers = browserslist(normalizedQuery, {
env: opts.env,
path: opts.path || process.cwd(),
})
const parsedBrowsers = parseBrowsersList(browsers)
const resolvedUserAgent = resolveUserAgent(uaString)
const options = {
ignoreMinor: false,
ignorePatch: true,
...opts,
}
return parsedBrowsers.some((browser) => {
if (!resolvedUserAgent.family) return false
if (!resolvedUserAgent.version) return false
if (!browser.version) return false
return (
browser.family.toLowerCase() ===
resolvedUserAgent.family.toLocaleLowerCase() &&
compareBrowserSemvers(resolvedUserAgent.version, browser.version, options)
)
})
}
export {
matchesUA,
resolveUserAgent,
normalizeQuery,
}