Skip to content

Commit

Permalink
Merge c66f101 into a673aa8
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-redFox committed Mar 5, 2020
2 parents a673aa8 + c66f101 commit f40d1d6
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 52 deletions.
10 changes: 6 additions & 4 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
35 changes: 5 additions & 30 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions test/unit/browser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

Expand Down Expand Up @@ -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', () => {
Expand Down
38 changes: 23 additions & 15 deletions test/unit/helper.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,100 +14,100 @@ 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', () => {
expecting(
'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', () => {
expecting(
'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', () => {
expecting(
'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', () => {
expecting(
'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', () => {
expecting(
'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', () => {
expecting(
'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', () => {
expecting(
'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', () => {
expecting(
'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', () => {
expecting(
'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', () => {
expecting(
'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
Expand All @@ -116,15 +116,23 @@ 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', () => {
expecting(
'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)')
})
})

Expand Down

0 comments on commit f40d1d6

Please sign in to comment.