diff --git a/lib/helper.js b/lib/helper.js index b3d64b24f..148e2ce18 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -3,14 +3,16 @@ const fs = require('graceful-fs') const path = require('path') const _ = require('lodash') -const useragent = require('useragent') +const useragent = require('ua-parser-js') const Promise = require('bluebird') const mm = require('minimatch') exports.browserFullNameToShort = (fullName) => { - const agent = useragent.parse(fullName) - const isUnknown = agent.family === 'Other' || agent.os.family === 'Other' - return isUnknown ? fullName : `${agent.toAgent()} (${agent.os})` + const au = useragent(fullName) + if (!au.browser.name && !au.browser.version && !au.os.name && !au.os.version) { + return 'NonexistentBot/1.2.3' + } + return `${au.browser.name} ${au.browser.version || '0.0.0'} (${au.os.name} ${au.os.version || '0.0.0'})` } exports.isDefined = (value) => { diff --git a/package-lock.json b/package-lock.json index 1d3089f57..cf77e75bd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6327,22 +6327,6 @@ "integrity": "sha1-miyr0bno4K6ZOkv31YdcOcQujqw=", "dev": true }, - "lru-cache": { - "version": "4.1.5", - "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-4.1.5.tgz", - "integrity": "sha512-sWZlbEP2OsHNkXrMl5GYk/jKk70MBng6UU4YI/qGDYbgf6YbP4EvmqISbXCoJiRKs+1bSpFHVgQxvJ17F2li5g==", - "requires": { - "pseudomap": "^1.0.2", - "yallist": "^2.1.2" - }, - "dependencies": { - "yallist": { - "version": "2.1.2", - "resolved": "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz", - "integrity": "sha1-HBH5IY8HYImkfdUS+TxmmaaoHVI=" - } - } - }, "make-iterator": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/make-iterator/-/make-iterator-1.0.1.tgz", @@ -7672,11 +7656,6 @@ "event-stream": "=3.3.4" } }, - "pseudomap": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/pseudomap/-/pseudomap-1.0.2.tgz", - "integrity": "sha1-8FKijacOYYkX7wqKw0wa5aaChrM=" - }, "public-encrypt": { "version": "4.0.3", "resolved": "https://registry.npmjs.org/public-encrypt/-/public-encrypt-4.0.3.tgz", @@ -9201,6 +9180,11 @@ "integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=", "dev": true }, + "ua-parser-js": { + "version": "0.7.21", + "resolved": "https://registry.npmjs.org/ua-parser-js/-/ua-parser-js-0.7.21.tgz", + "integrity": "sha512-+O8/qh/Qj8CgC6eYBVBykMrNtp5Gebn4dlGD/kKXVkJNDwyrAwSIqwz8CDf+tsAIWVycKcku6gIXJ0qwx/ZXaQ==" + }, "uglify-js": { "version": "3.6.0", "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.6.0.tgz", @@ -9382,15 +9366,6 @@ "integrity": "sha512-cwESVXlO3url9YWlFW/TA9cshCEhtu7IKJ/p5soJ/gGpj7vbvFrAY/eIioQ6Dw23KjZhYgiIo8HOs1nQ2vr/oQ==", "dev": true }, - "useragent": { - "version": "2.3.0", - "resolved": "https://registry.npmjs.org/useragent/-/useragent-2.3.0.tgz", - "integrity": "sha512-4AoH4pxuSvHCjqLO04sU6U/uE65BYza8l/KKBS0b0hnUPWi+cQ2BpeTEwejCSx9SPV5/U03nniDTrWx5NrmKdw==", - "requires": { - "lru-cache": "4.1.x", - "tmp": "0.0.x" - } - }, "util": { "version": "0.10.4", "resolved": "https://registry.npmjs.org/util/-/util-0.10.4.tgz", diff --git a/package.json b/package.json index f0fa1d647..962e76d9f 100644 --- a/package.json +++ b/package.json @@ -415,7 +415,7 @@ "socket.io": "2.1.1", "source-map": "^0.6.1", "tmp": "0.0.33", - "useragent": "2.3.0" + "ua-parser-js": "0.7.21" }, "devDependencies": { "browserify": "^16.2.3", diff --git a/test/unit/browser.spec.js b/test/unit/browser.spec.js index 3c1113c09..ec889b083 100644 --- a/test/unit/browser.spec.js +++ b/test/unit/browser.spec.js @@ -28,7 +28,7 @@ describe('Browser', () => { it('should set fullName and name', () => { const fullName = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.7 ' + '(KHTML, like Gecko) Chrome/16.0.912.63 Safari/535.7' browser = new Browser('id', fullName, collection, emitter, socket) - expect(browser.name).to.equal('Chrome 16.0.912 (Mac OS X 10.6.8)') + expect(browser.name).to.equal('Chrome 16.0.912.63 (Mac OS 10.6.8)') expect(browser.fullName).to.equal(fullName) }) @@ -66,7 +66,7 @@ describe('Browser', () => { it('should return browser name', () => { const fullName = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.7 ' + '(KHTML, like Gecko) Chrome/16.0.912.63 Safari/535.7' browser = new Browser('id', fullName, collection, emitter, socket) - expect(browser.toString()).to.equal('Chrome 16.0.912 (Mac OS X 10.6.8)') + expect(browser.toString()).to.equal('Chrome 16.0.912.63 (Mac OS 10.6.8)') }) it('should return verbatim user agent string for unrecognized browser', () => { diff --git a/test/unit/helper.spec.js b/test/unit/helper.spec.js index 43a08b043..8c5808c50 100644 --- a/test/unit/helper.spec.js +++ b/test/unit/helper.spec.js @@ -14,7 +14,7 @@ describe('helper', () => { 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 ' + '(KHTML, like Gecko) Version/6.0 Mobile/10A403 Safari/8536.25' ) - .to.be.equal('Mobile Safari 6.0.0 (iOS 6.0.0)') + .to.be.equal('Mobile Safari 6.0 (iOS 6.0)') }) it('should parse Linux', () => { @@ -22,14 +22,14 @@ describe('helper', () => { 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.19) Gecko/20081216 ' + 'Ubuntu/8.04 (hardy) Firefox/2.0.0.19' ) - .to.be.equal('Firefox 2.0.0 (Ubuntu 8.04.0)') + .to.be.equal('Firefox 2.0.0.19 (Ubuntu 8.04)') }) it('should degrade gracefully when OS not recognized', () => { expecting( 'Mozilla/5.0 (X11; U; FreeBSD; i386; en-US; rv:1.7) Gecko/20081216 ' + 'Firefox/2.0.0.19' - ).to.be.equal('Firefox 2.0.0 (FreeBSD 0.0.0)') + ).to.be.equal('Firefox 2.0.0.19 (FreeBSD 0.0.0)') }) it('should parse Chrome', () => { @@ -37,13 +37,13 @@ describe('helper', () => { 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.7 ' + '(KHTML, like Gecko) Chrome/16.0.912.63 Safari/535.7' ) - .to.be.equal('Chrome 16.0.912 (Mac OS X 10.6.8)') + .to.be.equal('Chrome 16.0.912.63 (Mac OS 10.6.8)') expecting( 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.15 ' + '(KHTML, like Gecko) Chrome/18.0.985.0 Safari/535.15' ) - .to.be.equal('Chrome 18.0.985 (Mac OS X 10.6.8)') + .to.be.equal('Chrome 18.0.985.0 (Mac OS 10.6.8)') }) it('should parse Firefox', () => { @@ -51,7 +51,7 @@ describe('helper', () => { 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:7.0.1) Gecko/20100101 ' + 'Firefox/7.0.1' ) - .to.be.equal('Firefox 7.0.1 (Mac OS X 10.6.0)') + .to.be.equal('Firefox 7.0.1 (Mac OS 10.6)') }) it('should parse Opera', () => { @@ -59,7 +59,7 @@ describe('helper', () => { 'Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.9.168 ' + 'Version/11.52' ) - .to.be.equal('Opera 11.52.0 (Mac OS X 10.6.8)') + .to.be.equal('Opera 11.52 (Mac OS 10.6.8)') }) it('should parse Safari', () => { @@ -67,7 +67,7 @@ describe('helper', () => { 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.52.7 ' + '(KHTML, like Gecko) Version/5.1.2 Safari/534.52.7' ) - .to.be.equal('Safari 5.1.2 (Mac OS X 10.6.8)') + .to.be.equal('Safari 5.1.2 (Mac OS 10.6.8)') }) it('should parse IE7', () => { @@ -75,7 +75,7 @@ describe('helper', () => { 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; WOW64; SLCC1; ' + '.NET CLR 2.0.50727; .NET4.0C; .NET4.0E)' ) - .to.be.equal('IE 7.0.0 (Windows Vista.0.0)') + .to.be.equal('IE 7.0 (Windows Vista)') }) it('should parse IE8', () => { @@ -83,7 +83,7 @@ describe('helper', () => { 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; ' + 'SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E; InfoPath.3)"' ) - .to.be.equal('IE 8.0.0 (Windows 7.0.0)') + .to.be.equal('IE 8.0 (Windows 7)') }) it('should parse IE9', () => { @@ -91,7 +91,7 @@ describe('helper', () => { 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; ' + '.NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)' ) - .to.be.equal('IE 9.0.0 (Windows 7.0.0)') + .to.be.equal('IE 9.0 (Windows 7)') }) it('should parse IE10', () => { @@ -99,7 +99,7 @@ describe('helper', () => { 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0; ' + '.NET4.0E; .NET4.0C)' ) - .to.be.equal('IE 10.0.0 (Windows 8.0.0)') + .to.be.equal('IE 10.0 (Windows 8)') }) it('should parse PhantomJS', () => { @@ -107,7 +107,7 @@ describe('helper', () => { 'Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/534.34 (KHTML, like Gecko) ' + 'PhantomJS/1.6.0 Safari/534.34' ) - .to.be.equal('PhantomJS 1.6.0 (Mac OS X 0.0.0)') + .to.be.equal('PhantomJS 1.6.0 (Mac OS 0.0.0)') }) // Fix for #318 @@ -116,7 +116,7 @@ describe('helper', () => { 'Mozilla/5.0 (Linux; U; Android 4.2; en-us; sdk Build/JB_MR1) ' + 'AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30' ) - .to.be.equal('Android 4.2.0 (Android 4.2.0)') + .to.be.equal('Android Browser 4.0 (Android 4.2)') }) it('should parse Headless Chrome', () => { @@ -124,7 +124,15 @@ describe('helper', () => { 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) ' + 'HeadlessChrome/70.0.3538.77 Safari/537.36' ) - .to.be.equal('HeadlessChrome 70.0.3538 (Linux 0.0.0)') + .to.be.equal('Chrome Headless 70.0.3538.77 (Linux x86_64)') + }) + + it('should parse MS Edge Chromium', () => { + expecting( + 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ' + + 'Chrome/80.0.3987.132 Safari/537.36 Edg/80.0.361.66' + ) + .to.be.equal('Edge 80.0.361.66 (Windows 10)') }) })