diff --git a/lib/exporter.test.ts b/lib/exporter.test.ts index 1634d02c9..bff0ddc4c 100644 --- a/lib/exporter.test.ts +++ b/lib/exporter.test.ts @@ -30,7 +30,7 @@ const REPORTS: { }, expected: { slug: "1.2.3-safari-12.0-mac-os-10.14-cadc34e83f", - title: "Results from Safari 12 / Mac OS 10.14 / Collector v1.2.3", + title: "Results from Safari 12 / macOS 10.14 / Collector v1.2.3", body: "User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Safari/605.1.15\nBrowser: Safari 12 (on Mac OS 10.14)\nHash Digest: cadc34e83f\nTest URLs: ", }, }, @@ -43,7 +43,7 @@ const REPORTS: { }, expected: { slug: "1.2.3-dev-chrome-86.0.4240.198-mac-os-11.0.0-32f70f2e14", - title: "Results from Chrome 86 / Mac OS 11.0.0 / Collector v1.2.3-dev", + title: "Results from Chrome 86 / macOS 11.0.0 / Collector v1.2.3-dev", body: "User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36\nBrowser: Chrome 86 (on Mac OS 11.0.0)\nHash Digest: 32f70f2e14\nTest URLs: \n\n**WARNING:** this PR was created from a development/staging version!", }, }, @@ -75,7 +75,7 @@ const REPORTS: { }, expected: { slug: "1.2.3-chrome-800.0.1.2-mac-os-11.0.0-ee13f09a68", - title: "Results from Chrome 800.0 / Mac OS 11.0.0 / Collector v1.2.3", + title: "Results from Chrome 800.0 / macOS 11.0.0 / Collector v1.2.3", body: "User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/800.0.1.2 Safari/537.36\nBrowser: Chrome 800.0 (on Mac OS 11.0.0) - **Not in BCD**\nHash Digest: ee13f09a68\nTest URLs: https://mdn-bcd-collector.gooborg.com/tests/?exposure=Window, https://mdn-bcd-collector.gooborg.com/tests/?exposure=Worker", }, }, diff --git a/lib/ua-parser.test.ts b/lib/ua-parser.test.ts index 691e4065e..9ca4625b4 100644 --- a/lib/ua-parser.test.ts +++ b/lib/ua-parser.test.ts @@ -80,7 +80,7 @@ describe("parseUA", () => { browser: {id: "chrome", name: "Chrome"}, version: "85", fullVersion: "85.0.4183.121", - os: {name: "Mac OS", version: "10.15.6"}, + os: {name: "macOS", version: "10.15.6"}, inBcd: true, }, ); @@ -96,7 +96,7 @@ describe("parseUA", () => { browser: {id: "chrome", name: "Chrome"}, version: "1000.1", fullVersion: "1000.1.4183.121", - os: {name: "Mac OS", version: "10.15.6"}, + os: {name: "macOS", version: "10.15.6"}, inBcd: false, }, ); @@ -224,7 +224,7 @@ describe("parseUA", () => { browser: {id: "safari", name: "Safari"}, version: "14", fullVersion: "14.0", - os: {name: "Mac OS", version: "10.15.6"}, + os: {name: "macOS", version: "10.15.6"}, inBcd: true, }, ); @@ -240,7 +240,7 @@ describe("parseUA", () => { browser: {id: "safari", name: "Safari"}, version: "14", fullVersion: "14.1", - os: {name: "Mac OS", version: "10.15.6"}, + os: {name: "macOS", version: "10.15.6"}, inBcd: true, }, ); @@ -256,7 +256,7 @@ describe("parseUA", () => { browser: {id: "safari", name: "Safari"}, version: "15", fullVersion: "15.0", - os: {name: "Mac OS", version: "10.15.6"}, + os: {name: "macOS", version: "10.15.6"}, inBcd: true, }, ); @@ -272,7 +272,7 @@ describe("parseUA", () => { browser: {id: "safari", name: "Safari"}, version: "15.2", fullVersion: "15.2", - os: {name: "Mac OS", version: "10.15.6"}, + os: {name: "macOS", version: "10.15.6"}, inBcd: true, }, ); @@ -288,7 +288,7 @@ describe("parseUA", () => { browser: {id: "safari", name: "Safari"}, version: "15.3", fullVersion: "15.3", - os: {name: "Mac OS", version: "10.15.6"}, + os: {name: "macOS", version: "10.15.6"}, inBcd: false, }, ); @@ -304,7 +304,7 @@ describe("parseUA", () => { browser: {id: "safari", name: "Safari"}, version: "16.0", fullVersion: "16.0", - os: {name: "Mac OS", version: "10.15.6"}, + os: {name: "macOS", version: "10.15.6"}, inBcd: false, }, ); @@ -320,7 +320,7 @@ describe("parseUA", () => { browser: {id: "safari", name: "Safari"}, version: "7.1", fullVersion: "7.1", - os: {name: "Mac OS", version: "10.15.6"}, + os: {name: "macOS", version: "10.15.6"}, inBcd: false, }, ); diff --git a/lib/ua-parser.ts b/lib/ua-parser.ts index c0ced50c4..588066e87 100644 --- a/lib/ua-parser.ts +++ b/lib/ua-parser.ts @@ -11,7 +11,7 @@ import { compare as compareVersions, compareVersions as compareVersionsSort, } from "compare-versions"; -import uaParser from "ua-parser-js"; +import {UAParser} from "ua-parser-js"; import {ParsedUserAgent} from "../types/types"; /** @@ -41,7 +41,7 @@ const getMajorMinorVersion = (version: string): string => { * @returns An object containing the parsed browser and operating system information. */ const parseUA = (userAgent: string, browsers: Browsers): ParsedUserAgent => { - const ua = uaParser(userAgent); + const ua = UAParser(userAgent); const data: ParsedUserAgent = { browser: {id: "", name: ""}, version: "", @@ -66,10 +66,10 @@ const parseUA = (userAgent: string, browsers: Browsers): ParsedUserAgent => { data.os.version = ua.os.version || ""; } + data.browser.id = data.browser.id.replace("mobile_", ""); + data.browser.name = data.browser.name.replace("Mobile ", ""); + switch (data.browser.id) { - case "mobile_safari": - data.browser.id = "safari"; - break; case "oculus_browser": data.browser.id = "oculus"; break; @@ -92,9 +92,10 @@ const parseUA = (userAgent: string, browsers: Browsers): ParsedUserAgent => { if (ua.browser.name === "Android Browser") { // For early WebView Android, use the OS version - data.fullVersion = compareVersions(ua.os.version, "5.0", "<") - ? ua.os.version - : ua.engine.version; + data.fullVersion = + (compareVersions(ua.os.version || "0", "5.0", "<") + ? ua.os.version + : ua.engine.version) || "0"; } } else if (os === "ios") { if (data.browser.id === "webkit") { @@ -105,10 +106,10 @@ const parseUA = (userAgent: string, browsers: Browsers): ParsedUserAgent => { data.browser.name += " iOS"; // https://github.com/mdn/browser-compat-data/blob/main/docs/data-guidelines.md#safari-for-ios-versioning - data.fullVersion = ua.os.version; + data.fullVersion = ua.os.version || "0"; } - data.fullVersion = data.fullVersion || ua.browser.version; + data.fullVersion = data.fullVersion || ua.browser.version || "0"; data.version = getMajorMinorVersion(data.fullVersion); if (!(data.browser.id in browsers)) { diff --git a/package-lock.json b/package-lock.json index ad70f48d7..6df03e5b4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -33,7 +33,7 @@ "mermaid": "^11.4.0", "slugify": "1.6.6", "ts-node": "10.9.2", - "ua-parser-js": "1.0.39", + "ua-parser-js": "2.0.0", "wasm-feature-detect": "^1.8.0", "winston": "3.17.0", "yargs": "17.7.2" @@ -6594,6 +6594,25 @@ "npm": "1.2.8000 || >= 1.4.16" } }, + "node_modules/detect-europe-js": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/detect-europe-js/-/detect-europe-js-0.1.2.tgz", + "integrity": "sha512-lgdERlL3u0aUdHocoouzT10d9I89VVhk0qNRmll7mXdGfJT1/wqZ2ZLA4oJAjeACPY5fT1wsbq2AT+GkuInsow==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/faisalman" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/ua-parser-js" + }, + { + "type": "paypal", + "url": "https://paypal.me/faisalman" + } + ] + }, "node_modules/detect-libc": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-1.0.3.tgz", @@ -9746,6 +9765,25 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/is-standalone-pwa": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/is-standalone-pwa/-/is-standalone-pwa-0.1.1.tgz", + "integrity": "sha512-9Cbovsa52vNQCjdXOzeQq5CnCbAcRk05aU62K20WO372NrTv0NxibLFCK6lQ4/iZEFdEA3p3t2VNOn8AJ53F5g==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/faisalman" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/ua-parser-js" + }, + { + "type": "paypal", + "url": "https://paypal.me/faisalman" + } + ] + }, "node_modules/is-stream": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-2.0.1.tgz", @@ -14300,10 +14338,29 @@ "node": ">=14.17" } }, + "node_modules/ua-is-frozen": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/ua-is-frozen/-/ua-is-frozen-0.1.2.tgz", + "integrity": "sha512-RwKDW2p3iyWn4UbaxpP2+VxwqXh0jpvdxsYpZ5j/MLLiQOfbsV5shpgQiw93+KMYQPcteeMQ289MaAFzs3G9pw==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/faisalman" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/ua-parser-js" + }, + { + "type": "paypal", + "url": "https://paypal.me/faisalman" + } + ] + }, "node_modules/ua-parser-js": { - "version": "1.0.39", - "resolved": "https://registry.npmjs.org/ua-parser-js/-/ua-parser-js-1.0.39.tgz", - "integrity": "sha512-k24RCVWlEcjkdOxYmVJgeD/0a1TiSpqLg+ZalVGV9lsnr4yqu0w7tX/x2xX6G4zpkgQnRf89lxuZ1wsbjXM8lw==", + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ua-parser-js/-/ua-parser-js-2.0.0.tgz", + "integrity": "sha512-SASgD4RlB7+SCMmlVNqrhPw0f/2pGawWBzJ2+LwGTD0GgNnrKGzPJDiraGHJDwW9Zm5DH2lTmUpqDpbZjJY4+Q==", "funding": [ { "type": "opencollective", @@ -14318,6 +14375,11 @@ "url": "https://github.com/sponsors/faisalman" } ], + "dependencies": { + "detect-europe-js": "^0.1.2", + "is-standalone-pwa": "^0.1.1", + "ua-is-frozen": "^0.1.2" + }, "bin": { "ua-parser-js": "script/cli.js" }, diff --git a/package.json b/package.json index d9b7ab04e..8970d4a5e 100644 --- a/package.json +++ b/package.json @@ -67,7 +67,7 @@ "mermaid": "^11.4.0", "slugify": "1.6.6", "ts-node": "10.9.2", - "ua-parser-js": "1.0.39", + "ua-parser-js": "2.0.0", "wasm-feature-detect": "^1.8.0", "winston": "3.17.0", "yargs": "17.7.2"