Skip to content

Commit

Permalink
fix: fix Edge browser regex, minor version is required (#1533)
Browse files Browse the repository at this point in the history
fixes #1530
  • Loading branch information
dangreen committed Dec 24, 2023
1 parent 6f48342 commit f6108cf
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 60 deletions.
32 changes: 16 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,40 +67,40 @@
},
"dependencies": {
"argue-cli": "^2.1.0",
"easy-table": "^1.1.1",
"easy-table": "^1.2.0",
"picocolors": "^1.0.0",
"regexp-tree": "^0.1.24",
"ua-regexes-lite": "^1.1.3"
"ua-regexes-lite": "^1.2.1"
},
"devDependencies": {
"@commitlint/cli": "^18.0.0",
"@commitlint/config-conventional": "^18.0.0",
"@commitlint/cz-commitlint": "^18.0.0",
"@commitlint/cli": "^18.4.3",
"@commitlint/config-conventional": "^18.4.3",
"@commitlint/cz-commitlint": "^18.4.3",
"@rollup/plugin-node-resolve": "^15.0.1",
"@size-limit/file": "^11.0.0",
"@swc/core": "^1.3.20",
"@swc/helpers": "^0.5.0",
"@swc/helpers": "^0.5.1",
"@trigen/browserslist-config": "8.0.0-alpha.27",
"@trigen/eslint-config": "8.0.0-alpha.29",
"@trigen/scripts": "8.0.0-alpha.29",
"@types/node": "^20.0.0",
"@vitest/coverage-v8": "^0.34.0",
"browserslist": "^4.21.4",
"@types/node": "^20.10.1",
"@vitest/coverage-v8": "^0.34.4",
"browserslist": "^4.22.2",
"clean-publish": "^4.0.1",
"commitizen": "^4.2.5",
"del-cli": "^5.0.0",
"eslint": "^8.28.0",
"nano-staged": "^0.8.0",
"rollup": "^4.0.0",
"rollup-plugin-add-shebang": "^0.3.0",
"rollup-plugin-swc3": "^0.10.0",
"rollup": "^4.6.1",
"rollup-plugin-add-shebang": "^0.3.1",
"rollup-plugin-swc3": "^0.10.1",
"simple-git-hooks": "^2.8.1",
"simple-github-release": "^1.0.0",
"size-limit": "^11.0.0",
"standard-version": "^9.5.0",
"typedoc": "^0.25.0",
"typescript": "^5.0.0",
"vite": "^5.0.0",
"vitest": "^0.34.0"
"typedoc": "^0.25.1",
"typescript": "^5.1.3",
"vite": "^5.0.4",
"vitest": "^0.34.4"
}
}
70 changes: 35 additions & 35 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 20 additions & 9 deletions src/useragentRegex/useragentRegex.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ import {

function* getUserAgents() {
const regexesCache = new Map<string, RegExp>()
const getRegex = (query: string) => {
let regex = regexesCache.get(query)
const getRegex = (query: string, allowHigherVersions = true) => {
const key = `${query}:${allowHigherVersions}`
let regex = regexesCache.get(key)

if (!regex) {
regex = getUserAgentRegex({
browsers: query,
allowHigherVersions: true,
allowHigherVersions,
allowZeroSubversions: true
})
regexesCache.set(query, regex)
regexesCache.set(key, regex)
}

return regex
Expand All @@ -27,8 +28,9 @@ function* getUserAgents() {
for (const query of useragent.yes) {
yield {
ua: useragent.ua,
regex: getRegex(query),
regex: getRegex(query, useragent.allowHigherVersions),
query,
allowHigherVersions: useragent.allowHigherVersions,
should: true
}
}
Expand All @@ -38,7 +40,8 @@ function* getUserAgents() {
for (const query of useragent.no) {
yield {
ua: useragent.ua,
regex: getRegex(query),
regex: getRegex(query, useragent.allowHigherVersions),
allowHigherVersions: useragent.allowHigherVersions,
query,
should: false
}
Expand All @@ -47,10 +50,18 @@ function* getUserAgents() {
}
}

function inspect(query: string, ua: string, should: boolean) {
interface UserAgentTest {
ua: string
regex: RegExp
query: string
allowHigherVersions?: boolean
should: boolean
}

function inspect({ query, ua, should, allowHigherVersions }: UserAgentTest) {
const info = getUserAgentRegexes({
browsers: query,
allowHigherVersions: true,
allowHigherVersions,
allowZeroSubversions: true
})
const message = `${should ? 'Should' : 'Should not'} matches:
Expand All @@ -74,7 +85,7 @@ describe('UserAgentRegex', () => {
res = ua.regex.test(ua.ua)

if (res !== ua.should) {
inspect(ua.query, ua.ua, ua.should)
inspect(ua)
}
}
})
Expand Down
27 changes: 27 additions & 0 deletions test/useragents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,47 @@ export const useragents = [
ua: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.25',
yes: ['edge 105', 'edge >= 105', 'chrome 105']
},
/**
* Edge bug #1530
*/
{
ua: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36 Edge/120.0',
allowHigherVersions: false,
yes: ['edge 120'],
no: ['edge 12']
},
/**
* Firefox Desktop
*/
{
ua: 'Mozilla/5.0 (Windows NT 5.2; rv:42.0) Gecko/20100101 Firefox/42.0',
yes: ['firefox >= 40']
},
/**
* Firefox Desktop bug #1530
*/
{
ua: 'Mozilla/5.0 (Windows NT 5.2; rv:42.0) Gecko/20100101 Firefox/120.0',
allowHigherVersions: false,
yes: ['firefox 120'],
no: ['firefox 12']
},
/**
* Chrome Desktop
*/
{
ua: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36',
yes: ['chrome >= 60']
},
/**
* Chrome Desktop bug #1530
*/
{
ua: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36',
allowHigherVersions: false,
yes: ['chrome 120'],
no: ['chrome 12']
},
/**
* Safari Desktop
*/
Expand Down

0 comments on commit f6108cf

Please sign in to comment.