Nuxt/packages/utils/src/modern.js

49 lines
1.1 KiB
JavaScript
Raw Normal View History

import UAParser from 'ua-parser-js'
import semver from 'semver'
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
const modernBrowsers = Object.keys(ModernBrowsers)
.reduce((allBrowsers, browser) => {
allBrowsers[browser] = semver.coerce(ModernBrowsers[browser])
return allBrowsers
}, {})
export const isModernBrowser = (ua) => {
if (!ua) {
return false
}
const { browser } = UAParser(ua)
const browserVersion = semver.coerce(browser.version)
if (!browserVersion) {
return false
}
return Boolean(modernBrowsers[browser.name] && semver.gte(browserVersion, modernBrowsers[browser.name]))
}
export const isModernRequest = (req, modernMode = false) => {
if (modernMode === false) {
return false
2018-12-24 16:26:39 +00:00
}
const { socket = {}, headers } = req
if (socket._modern === undefined) {
const ua = headers && headers['user-agent']
socket._modern = isModernBrowser(ua)
}
return socket._modern
}