diff --git a/src/helpers/ua-parser-helpers.js b/src/helpers/ua-parser-helpers.js index d6825e0..54ce86f 100644 --- a/src/helpers/ua-parser-helpers.js +++ b/src/helpers/ua-parser-helpers.js @@ -8,11 +8,12 @@ /*jshint esversion: 6 */ const { UAParser } = require('../main/ua-parser'); -const { CPUArch, OSName, EngineName, Extension } = require('../enums/ua-parser-enums'); -const { Bots } = require('../extensions/ua-parser-extensions'); +const { CPUArch, OSName, EngineName, Extension, BrowserType } = require('../enums/ua-parser-enums'); +const { Bots, Crawlers } = require('../extensions/ua-parser-extensions'); const { isFromEU } = require('detect-europe-js'); const { isFrozenUA } = require('ua-is-frozen'); const { isStandalonePWA } = require('is-standalone-pwa'); +const { Crawler } = Extension.BrowserName; const toResult = (value, head, ext) => typeof value === 'string' ? UAParser(value, head, ext) : value; @@ -41,7 +42,6 @@ const isAppleSilicon = (resultOrUA) => { return false; } -const Crawler = Extension.BrowserName.Crawlers; const isAIBot = (resultOrUA) => [ // AI2 @@ -158,13 +158,13 @@ const isAIBot = (resultOrUA) => [ Crawler.ZHIPU_CHATGLM_SPIDER ] .map((s) => s.toLowerCase()) - .includes(String(toResult(resultOrUA, Bots).browser.name).toLowerCase()); + .includes(String(toResult(resultOrUA, Crawlers).browser.name).toLowerCase()); const isBot = (resultOrUA) => [ - 'cli', - 'crawler', - 'fetcher', - 'library' + BrowserType.CLI, + BrowserType.CRAWLER, + BrowserType.FETCHER, + BrowserType.LIBRARY ].includes(toResult(resultOrUA, Bots).browser.type); const isChromeFamily = (resultOrUA) => toResult(resultOrUA).engine.is(EngineName.BLINK); diff --git a/test/unit/extensions.js b/test/unit/extensions.js index 9f1fa96..c3acfa2 100644 --- a/test/unit/extensions.js +++ b/test/unit/extensions.js @@ -5,6 +5,8 @@ const traverse = require('@babel/traverse').default; const safe = require('safe-regex'); const { UAParser } = require('../../src/main/ua-parser'); const { Bots, CLIs, Crawlers, Emails, Fetchers, InApps, Libraries, Vehicles } = require('../../src/extensions/ua-parser-extensions'); +const { BrowserType, OSName, Extension } = require('../../src/enums/ua-parser-enums'); +const { CLI, Crawler, Email, Fetcher, Library } = Extension.BrowserName; describe('Extensions', () => { [ @@ -42,29 +44,29 @@ describe('Extensions', () => { const jsdom = 'Mozilla/5.0 (darwin) AppleWebKit/537.36 (KHTML, like Gecko) jsdom/20.0.3'; const scrapy = 'Scrapy/1.5.0 (+https://scrapy.org)'; - assert.equal(UAParser(scrapy, Bots).browser.name, 'Scrapy'); + assert.equal(UAParser(scrapy, Bots).browser.name, Library.SCRAPY); const emailParser = new UAParser(Emails); - assert.deepEqual(emailParser.setUA(outlook).getBrowser(), {name: "Microsoft Outlook", version: "16.0.9126", major: "16", type: "email"}); - assert.deepEqual(emailParser.setUA(thunderbird).getBrowser(), {name: "Thunderbird", version: "78.13.0", major: "78", type: "email"}); + assert.deepEqual(emailParser.setUA(outlook).getBrowser(), {name: Email.MICROSOFT_OUTLOOK, version: "16.0.9126", major: "16", type: BrowserType.EMAIL}); + assert.deepEqual(emailParser.setUA(thunderbird).getBrowser(), {name: Email.THUNDERBIRD, version: "78.13.0", major: "78", type: BrowserType.EMAIL}); const libraryParser = new UAParser(Libraries); - assert.deepEqual(libraryParser.setUA(axios).getBrowser(), {name: "axios", version: "1.3.5", major: "1", type: "library"}); - assert.deepEqual(libraryParser.setUA(jsdom).getBrowser(), {name: "jsdom", version: "20.0.3", major: "20", type: "library"}); - assert.deepEqual(libraryParser.setUA(scrapy).getBrowser(), {name: "Scrapy", version: "1.5.0", major: "1", type: "library"}); + assert.deepEqual(libraryParser.setUA(axios).getBrowser(), {name: Library.AXIOS, version: "1.3.5", major: "1", type: BrowserType.LIBRARY}); + assert.deepEqual(libraryParser.setUA(jsdom).getBrowser(), {name: Library.JSDOM, version: "20.0.3", major: "20", type: BrowserType.LIBRARY}); + assert.deepEqual(libraryParser.setUA(scrapy).getBrowser(), {name: Library.SCRAPY, version: "1.5.0", major: "1", type: BrowserType.LIBRARY}); // Bluesky const bluesky = 'Mozilla/5.0 (compatible; Bluesky Cardyb/1.1; +mailto:support@bsky.app)'; assert.deepEqual(new UAParser(bluesky, Bots).getBrowser(), { - name: 'Bluesky', + name: Fetcher.BLUESKY, version: '1.1', major: '1', - type: 'fetcher' + type: BrowserType.FETCHER }); const whatsapp = "WhatsApp/2.0 A"; assert.deepEqual(new UAParser(whatsapp, Fetchers).getOS(), { - name : 'Android', + name : OSName.ANDROID, version : undefined }); }); @@ -77,14 +79,14 @@ describe('Merge', () => { // try merging crawlers & CLIs const crawlersAndCLIs = { browser : [...Crawlers.browser, ...CLIs.browser]}; const crawlersAndCLIsParser = new UAParser(crawlersAndCLIs); - assert.deepEqual(crawlersAndCLIsParser.setUA(wget).getBrowser(), {name: "Wget", version: "1.21.1", major: "1", type:"cli"}); - assert.deepEqual(crawlersAndCLIsParser.setUA(facebookBot).getBrowser(), {name: "FacebookBot", version: "1.0", major: "1", type:"crawler"}); + assert.deepEqual(crawlersAndCLIsParser.setUA(wget).getBrowser(), {name: CLI.WGET, version: "1.21.1", major: "1", type: BrowserType.CLI}); + assert.deepEqual(crawlersAndCLIsParser.setUA(facebookBot).getBrowser(), {name: Crawler.META_FACEBOOKBOT, version: "1.0", major: "1", type: BrowserType.CRAWLER}); // alternative merge options const crawlersAndCLIsParser2 = new UAParser([Crawlers, CLIs]); const crawlersAndCLIsParser3 = new UAParser(facebookBot, [Crawlers, CLIs]); - assert.deepEqual(crawlersAndCLIsParser2.setUA(wget).getBrowser(), {name: "Wget", version: "1.21.1", major: "1", type:"cli"}); - assert.deepEqual(crawlersAndCLIsParser3.getBrowser(), {name: "FacebookBot", version: "1.0", major: "1", type:"crawler"}); + assert.deepEqual(crawlersAndCLIsParser2.setUA(wget).getBrowser(), {name: CLI.WGET, version: "1.21.1", major: "1", type: BrowserType.CLI}); + assert.deepEqual(crawlersAndCLIsParser3.getBrowser(), {name: Crawler.META_FACEBOOKBOT, version: "1.0", major: "1", type: BrowserType.CRAWLER}); }); }); diff --git a/test/unit/helpers.js b/test/unit/helpers.js index 127133b..3fa7925 100644 --- a/test/unit/helpers.js +++ b/test/unit/helpers.js @@ -2,6 +2,7 @@ const assert = require('assert'); const { UAParser } = require('../../src/main/ua-parser'); const { getDeviceVendor, isAppleSilicon, isAIBot, isBot, isChromeFamily } = require('../../src/helpers/ua-parser-helpers'); const { Bots, Emails } = require('../../src/extensions/ua-parser-extensions'); +const { DeviceVendor } = require('../../src/enums/ua-parser-enums'); describe('getDeviceVendor', () => { it('Can guess the device vendor from a model name', () => { @@ -11,10 +12,10 @@ describe('getDeviceVendor', () => { const modelNexus = 'Nexus 6P'; const modelAquos = 'AQUOS-TVX19B'; - assert.equal(getDeviceVendor(modelSM), 'Samsung'); - assert.equal(getDeviceVendor(modelRedmi), 'Xiaomi'); - assert.equal(getDeviceVendor(modelNexus), 'Huawei'); - assert.equal(getDeviceVendor(modelAquos), 'Sharp'); + assert.equal(getDeviceVendor(modelSM), DeviceVendor.SAMSUNG); + assert.equal(getDeviceVendor(modelRedmi), DeviceVendor.XIAOMI); + assert.equal(getDeviceVendor(modelNexus), DeviceVendor.HUAWEI); + assert.equal(getDeviceVendor(modelAquos), DeviceVendor.SHARP); }); }); diff --git a/test/unit/ua-ch.js b/test/unit/ua-ch.js index a845a17..fd49f72 100644 --- a/test/unit/ua-ch.js +++ b/test/unit/ua-ch.js @@ -1,5 +1,6 @@ const assert = require('assert'); const { UAParser } = require('../../src/main/ua-parser'); +const { BrowserName, CPUArch, DeviceType, DeviceVendor, EngineName, OSName } = require('../../src/enums/ua-parser-enums'); const UACHTests = require('../data/ua-ch/headers'); describe('Map UA-CH headers', () => { @@ -26,27 +27,27 @@ describe('Map UA-CH headers', () => { it('Can read from client-hints headers using `withClientHints()`', () => { assert.strictEqual(uap.ua, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"); - assert.strictEqual(uap.browser.name, "Chrome"); + assert.strictEqual(uap.browser.name, BrowserName.CHROME); assert.strictEqual(uap.browser.version, "93.0.1.2"); assert.strictEqual(uap.browser.major, "93"); - assert.strictEqual(browser.name, "Chrome"); + assert.strictEqual(browser.name, BrowserName.CHROME); assert.strictEqual(browser.version, "93.0.1.2"); assert.strictEqual(browser.major, "93"); - assert.strictEqual(uap.cpu.architecture, "arm64"); - assert.strictEqual(cpu.architecture, "arm64"); - assert.strictEqual(uap.device.type, "mobile"); + assert.strictEqual(uap.cpu.architecture, CPUArch.ARM_64); + assert.strictEqual(cpu.architecture, CPUArch.ARM_64); + assert.strictEqual(uap.device.type, DeviceType.MOBILE); assert.strictEqual(uap.device.model, "Pixel 99"); - assert.strictEqual(uap.device.vendor, "Google"); - assert.strictEqual(device.type, "mobile"); + assert.strictEqual(uap.device.vendor, DeviceVendor.GOOGLE); + assert.strictEqual(device.type, DeviceType.MOBILE); assert.strictEqual(device.model, "Pixel 99"); - assert.strictEqual(device.vendor, "Google"); - assert.strictEqual(uap.engine.name, 'Blink'); + assert.strictEqual(device.vendor, DeviceVendor.GOOGLE); + assert.strictEqual(uap.engine.name, EngineName.BLINK); assert.strictEqual(uap.engine.version, '93.0.1.2'); - assert.strictEqual(engine.name, 'Blink'); + assert.strictEqual(engine.name, EngineName.BLINK); assert.strictEqual(engine.version, '93.0.1.2'); - assert.strictEqual(uap.os.name, "Windows"); + assert.strictEqual(uap.os.name, OSName.WINDOWS); assert.strictEqual(uap.os.version, "11"); - assert.strictEqual(os.name, "Windows"); + assert.strictEqual(os.name, OSName.WINDOWS); assert.strictEqual(os.version, "11"); }); @@ -59,16 +60,16 @@ describe('Map UA-CH headers', () => { engine = new UAParser(headers).getEngine(); os = new UAParser(headers).getOS(); - assert.strictEqual(uap.browser.name, "Chrome"); + assert.strictEqual(uap.browser.name, BrowserName.CHROME); assert.strictEqual(uap.browser.version, "110.0.0.0"); assert.strictEqual(uap.browser.major, "110"); - assert.strictEqual(uap.cpu.architecture, "amd64"); + assert.strictEqual(uap.cpu.architecture, CPUArch.X86_64); assert.strictEqual(uap.device.type, undefined); assert.strictEqual(uap.device.model, undefined); assert.strictEqual(uap.device.vendor, undefined); - assert.strictEqual(uap.engine.name, 'Blink'); + assert.strictEqual(uap.engine.name, EngineName.BLINK); assert.strictEqual(uap.engine.version, '110.0.0.0'); - assert.strictEqual(uap.os.name, "Linux"); + assert.strictEqual(uap.os.name, OSName.LINUX); assert.strictEqual(uap.os.version, undefined); }); @@ -81,16 +82,16 @@ describe('Map UA-CH headers', () => { uap = UAParser(headers2).withClientHints(); - assert.strictEqual(uap.browser.name, "Chrome"); + assert.strictEqual(uap.browser.name, BrowserName.CHROME); assert.strictEqual(uap.browser.version, "110.0.0.0"); assert.strictEqual(uap.browser.major, "110"); - assert.strictEqual(uap.cpu.architecture, "amd64"); - assert.strictEqual(uap.device.type, "mobile"); + assert.strictEqual(uap.cpu.architecture, CPUArch.X86_64); + assert.strictEqual(uap.device.type, DeviceType.MOBILE); assert.strictEqual(uap.device.model, undefined); assert.strictEqual(uap.device.vendor, undefined); - assert.strictEqual(uap.engine.name, 'Blink'); + assert.strictEqual(uap.engine.name, EngineName.BLINK); assert.strictEqual(uap.engine.version, '110.0.0.0'); - assert.strictEqual(uap.os.name, "Linux"); + assert.strictEqual(uap.os.name, OSName.LINUX); assert.strictEqual(uap.os.version, undefined); }); @@ -117,10 +118,10 @@ describe('Map UA-CH headers', () => { } */ - assert.strictEqual(ua.os.is("macOS"), true); - assert.strictEqual(ua.cpu.is("arm"), true); - assert.strictEqual(ua.device.is("mobile"), false); - assert.strictEqual(ua.device.is("tablet"), false); + assert.strictEqual(ua.os.is(OSName.MACOS), true); + assert.strictEqual(ua.cpu.is(CPUArch.ARM), true); + assert.strictEqual(ua.device.is(DeviceType.MOBILE), false); + assert.strictEqual(ua.device.is(DeviceType.TABLET), false); }); }); @@ -139,11 +140,11 @@ describe('Map UA-CH headers', () => { }; UAParser(FFVR).withClientHints().then(ua => { - assert.strictEqual(ua.device.type, 'xr'); + assert.strictEqual(ua.device.type, DeviceType.XR); }); UAParser(FFEInk).withClientHints().then(ua => { - assert.strictEqual(ua.device.type, 'tablet'); + assert.strictEqual(ua.device.type, DeviceType.TABLET); }); @@ -169,7 +170,7 @@ describe('Map UA-CH headers', () => { uap = UAParser(headers2).withClientHints(); - assert.strictEqual(uap.browser.name, "Chrome"); + assert.strictEqual(uap.browser.name, BrowserName.CHROME); assert.strictEqual(uap.browser.version, undefined); assert.strictEqual(uap.browser.major, undefined); }); @@ -196,27 +197,27 @@ describe('Map UA-CH headers', () => { }; uap = UAParser(headers3a).withClientHints(); - assert.strictEqual(uap.browser.name, "Chrome"); + assert.strictEqual(uap.browser.name, BrowserName.CHROME); assert.strictEqual(uap.browser.version, "120.0.6099.132"); uap = UAParser(headers3b).withClientHints(); - assert.strictEqual(uap.browser.name, "Chrome"); + assert.strictEqual(uap.browser.name, BrowserName.CHROME); assert.strictEqual(uap.browser.version, "120.0.6099.132"); uap = UAParser(headers3c).withClientHints(); - assert.strictEqual(uap.browser.name, "Chrome"); + assert.strictEqual(uap.browser.name, BrowserName.CHROME); assert.strictEqual(uap.browser.version, "120.0.6099.132"); uap = UAParser(headers3d).withClientHints(); - assert.strictEqual(uap.browser.name, "Edge"); + assert.strictEqual(uap.browser.name, BrowserName.EDGE); assert.strictEqual(uap.browser.version, "120.0.6099.133"); uap = UAParser(headers3e).withClientHints(); - assert.strictEqual(uap.browser.name, "Edge"); + assert.strictEqual(uap.browser.name, BrowserName.EDGE); assert.strictEqual(uap.browser.version, "120.0.6099.133"); uap = UAParser(headers3f).withClientHints(); - assert.strictEqual(uap.browser.name, "Edge"); + assert.strictEqual(uap.browser.name, BrowserName.EDGE); assert.strictEqual(uap.browser.version, "120.0.6099.133"); }); }); @@ -235,169 +236,169 @@ describe('Identify vendor & type of device from given model name', () => { { model: '220733SG', expect: { - vendor : 'Xiaomi', - type : 'mobile' + vendor : DeviceVendor.XIAOMI, + type : DeviceType.MOBILE } }, { model: '5087Z', expect: { - vendor : 'TCL', - type : 'mobile' + vendor : DeviceVendor.TCL, + type : DeviceType.MOBILE } }, { model: '9137W', expect: { - vendor : 'TCL', - type : 'tablet' + vendor : DeviceVendor.TCL, + type : DeviceType.TABLET } }, { model: 'BE2015', expect: { - vendor : 'OnePlus', - type : 'mobile' + vendor : DeviceVendor.ONEPLUS, + type : DeviceType.MOBILE } }, { model: 'CPH2389', expect: { - vendor : 'OnePlus', - type : 'mobile' + vendor : DeviceVendor.ONEPLUS, + type : DeviceType.MOBILE } }, { model: 'Infinix X669C', expect: { - vendor : 'Infinix', - type : 'mobile' + vendor : DeviceVendor.INFINIX, + type : DeviceType.MOBILE } }, { model: 'itel L6502', expect: { - vendor : 'itel', - type : 'mobile' + vendor : DeviceVendor.ITEL, + type : DeviceType.MOBILE } }, { model: 'Lenovo TB-X606F', expect: { - vendor : 'Lenovo', - type : 'tablet' + vendor : DeviceVendor.LENOVO, + type : DeviceType.TABLET } }, { model: 'LM-Q720', expect: { - vendor : 'LG', - type : 'mobile' + vendor : DeviceVendor.LG, + type : DeviceType.MOBILE } }, { model: 'M2003J15SC', expect: { - vendor : 'Xiaomi', - type : 'mobile' + vendor : DeviceVendor.XIAOMI, + type : DeviceType.MOBILE } }, { model: 'MAR-LX1A', expect: { - vendor : 'Huawei', - type : 'mobile' + vendor : DeviceVendor.HUAWEI, + type : DeviceType.MOBILE } }, { model: 'moto g(20)', expect: { - vendor : 'Motorola', - type : 'mobile' + vendor : DeviceVendor.MOTOROLA, + type : DeviceType.MOBILE } }, { model: 'Nokia C210', expect: { - vendor : 'Nokia', - type : 'mobile' + vendor : DeviceVendor.NOKIA, + type : DeviceType.MOBILE } }, { model: 'Pixel 8', expect: { - vendor : 'Google', - type : 'mobile' + vendor : DeviceVendor.GOOGLE, + type : DeviceType.MOBILE } }, { model: 'Redmi Note 9S', expect: { - vendor : 'Xiaomi', - type : 'mobile' + vendor : DeviceVendor.XIAOMI, + type : DeviceType.MOBILE } }, { model: 'RMX3830', expect: { - vendor : 'Realme', - type : 'mobile' + vendor : DeviceVendor.REALME, + type : DeviceType.MOBILE } }, { model: 'SM-S536DL', expect: { - vendor : 'Samsung', - type : 'mobile' + vendor : DeviceVendor.SAMSUNG, + type : DeviceType.MOBILE } }, { model: 'SM-S546VL', expect: { - vendor : 'Samsung', - type : 'mobile' + vendor : DeviceVendor.SAMSUNG, + type : DeviceType.MOBILE } }, { model: 'SM-T875', expect: { - vendor : 'Samsung', - type : 'tablet' + vendor : DeviceVendor.SAMSUNG, + type : DeviceType.TABLET } }, { model: 'STK-L21', expect: { - vendor : 'Huawei', - type : 'mobile' + vendor : DeviceVendor.HUAWEI, + type : DeviceType.MOBILE } }, { model: 'T430W', expect: { - vendor : 'TCL', - type : 'mobile' + vendor : DeviceVendor.TCL, + type : DeviceType.MOBILE } }, { model: 'TECNO KI5k', expect: { - vendor : 'TECNO', - type : 'mobile' + vendor : DeviceVendor.TECNO, + type : DeviceType.MOBILE } }, { model: 'vivo 1820', expect: { - vendor : 'Vivo', - type : 'mobile' + vendor : DeviceVendor.VIVO, + type : DeviceType.MOBILE } }, { model: 'Xbox', expect: { - vendor : 'Microsoft', - type : 'console' + vendor : DeviceVendor.MICROSOFT, + type : DeviceType.CONSOLE } } ]