2018-12-10 21:35:04 +00:00
|
|
|
import UAParser from 'ua-parser-js'
|
|
|
|
import semver from 'semver'
|
2018-11-07 23:37:06 +00:00
|
|
|
|
2019-04-23 09:16:56 +00:00
|
|
|
export const ModernBrowsers = {
|
|
|
|
Edge: '16',
|
|
|
|
Firefox: '60',
|
|
|
|
Chrome: '61',
|
|
|
|
'Chrome Headless': '61',
|
|
|
|
Chromium: '61',
|
|
|
|
Iron: '61',
|
|
|
|
Safari: '10.1',
|
|
|
|
Opera: '48',
|
|
|
|
Yandex: '18',
|
|
|
|
Vivaldi: '1.14',
|
|
|
|
'Mobile Safari': '10.3'
|
|
|
|
}
|
2018-12-22 21:05:13 +00:00
|
|
|
|
2018-11-07 23:37:06 +00:00
|
|
|
const modernBrowsers = Object.keys(ModernBrowsers)
|
2018-12-10 21:35:04 +00:00
|
|
|
.reduce((allBrowsers, browser) => {
|
|
|
|
allBrowsers[browser] = semver.coerce(ModernBrowsers[browser])
|
|
|
|
return allBrowsers
|
|
|
|
}, {})
|
2018-11-07 23:37:06 +00:00
|
|
|
|
2019-04-23 09:16:56 +00:00
|
|
|
export const isModernBrowser = (ua) => {
|
2018-12-10 21:35:04 +00:00
|
|
|
if (!ua) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
const { browser } = UAParser(ua)
|
|
|
|
const browserVersion = semver.coerce(browser.version)
|
2019-01-02 21:52:09 +00:00
|
|
|
if (!browserVersion) {
|
|
|
|
return false
|
|
|
|
}
|
2019-02-14 14:01:30 +00:00
|
|
|
return Boolean(modernBrowsers[browser.name] && semver.gte(browserVersion, modernBrowsers[browser.name]))
|
2018-11-07 23:37:06 +00:00
|
|
|
}
|
|
|
|
|
2019-04-23 09:16:56 +00:00
|
|
|
export const isModernRequest = (req, modernMode = false) => {
|
|
|
|
if (modernMode === false) {
|
|
|
|
return false
|
2018-12-24 16:26:39 +00:00
|
|
|
}
|
2019-02-08 16:25:11 +00:00
|
|
|
|
2019-04-23 09:16:56 +00:00
|
|
|
const { socket = {}, headers } = req
|
|
|
|
if (socket._modern === undefined) {
|
|
|
|
const ua = headers && headers['user-agent']
|
|
|
|
socket._modern = isModernBrowser(ua)
|
2019-02-14 13:21:11 +00:00
|
|
|
}
|
2019-04-23 09:16:56 +00:00
|
|
|
|
|
|
|
return socket._modern
|
2018-11-07 23:37:06 +00:00
|
|
|
}
|