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
|
|
|
|
2018-12-22 21:05:13 +00:00
|
|
|
import ModernBrowsers from './modern-browsers'
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-12-24 16:26:39 +00:00
|
|
|
const detectModernBrowser = ({ socket = {}, headers }) => {
|
2019-03-03 07:52:59 +00:00
|
|
|
if (socket.isModernBrowser === undefined) {
|
|
|
|
const ua = headers && headers['user-agent']
|
|
|
|
socket.isModernBrowser = isModernBrowser(ua)
|
2018-12-24 16:26:39 +00:00
|
|
|
}
|
2019-02-08 16:25:11 +00:00
|
|
|
|
2019-03-03 07:52:59 +00:00
|
|
|
return socket.isModernBrowser
|
2018-11-26 22:49:47 +00:00
|
|
|
}
|
|
|
|
|
2019-04-20 12:02:51 +00:00
|
|
|
export default ({ serverContext }) => {
|
2019-03-13 19:15:26 +00:00
|
|
|
return (req, res, next) => {
|
2019-04-20 12:02:51 +00:00
|
|
|
if (serverContext.options.modern !== false) {
|
|
|
|
req._modern = detectModernBrowser(req)
|
2019-03-13 19:15:26 +00:00
|
|
|
}
|
|
|
|
next()
|
2019-02-14 13:21:11 +00:00
|
|
|
}
|
2018-11-07 23:37:06 +00:00
|
|
|
}
|