Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Commit

Permalink
fix(server): step in before node-uap parses Sync UA strings
Browse files Browse the repository at this point in the history
#1949
r=vbudhram
  • Loading branch information
philbooth committed Jun 19, 2017
1 parent 13eeab2 commit 3f78f6e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 94 deletions.
38 changes: 19 additions & 19 deletions lib/userAgent.js
Expand Up @@ -36,25 +36,25 @@ const ELLIPSIS = '\u2026'
const SYNC_USER_AGENT = /^(Firefox|Mobile)-(\w+)-(?:FxA(?:ccounts)?|Sync)\/([^\sb]*)(?:b\S+)? ?(?:\(([\w\s]+); [\w\s]+ ([^\s()]+)\))?(?: \((.+)\))?$/

module.exports = function (userAgentString, log) {
const userAgentData = ua.parse(userAgentString)

this.uaBrowser = getFamily(userAgentData.ua) || null
this.uaBrowserVersion = getVersion(userAgentData.ua) || null
this.uaOS = getFamily(userAgentData.os) || null
this.uaOSVersion = getVersion(userAgentData.os) || null
this.uaDeviceType = getDeviceType(userAgentData) || null

if (! this.uaBrowser) {
const matches = SYNC_USER_AGENT.exec(userAgentString)
if (matches && matches.length > 2) {
this.uaBrowser = matches[6] || matches[1]
this.uaBrowserVersion = matches[3] || null
this.uaOS = matches[2]
this.uaOSVersion = matches[5]
if (! this.uaDeviceType) {
this.uaDeviceType = marshallFormFactor(matches[4])
}
} else if (! this.uaOS) {
const matches = SYNC_USER_AGENT.exec(userAgentString)
if (matches && matches.length > 2) {
// Always parse known Sync user-agents ourselves,
// because node-uap makes a pig's ear of it.
this.uaBrowser = matches[6] || matches[1]
this.uaBrowserVersion = matches[3] || null
this.uaOS = matches[2]
this.uaOSVersion = matches[5]
this.uaDeviceType = marshallFormFactor(matches[4])
} else {
const userAgentData = ua.parse(userAgentString)

this.uaBrowser = getFamily(userAgentData.ua) || null
this.uaBrowserVersion = getVersion(userAgentData.ua) || null
this.uaOS = getFamily(userAgentData.os) || null
this.uaOSVersion = getVersion(userAgentData.os) || null
this.uaDeviceType = getDeviceType(userAgentData) || null

if (! this.uaBrowser && ! this.uaOS) {
// In the worst case, fall back to a truncated user agent string
this.uaBrowser = truncate(userAgentString || '', log)
}
Expand Down
91 changes: 16 additions & 75 deletions test/local/user_agent.js
Expand Up @@ -503,17 +503,7 @@ describe('userAgent', () => {
it(
'recognises old Firefox-iOS user agents',
() => {
parserResult = {
ua: {
family: 'Other'
},
os: {
family: 'iOS'
},
device: {
family: 'Other'
}
}
parserResult = null
const context = {}
const userAgentString = 'Firefox-iOS-FxA/5.3 (Firefox)'
const result = userAgent.call(context, userAgentString, log)
Expand All @@ -530,27 +520,18 @@ describe('userAgent', () => {
)

it(
'preserves device type on old Firefox-iOS user agents',
'recognises new Firefox-iOS user agents',
() => {
parserResult = {
ua: {
family: 'Other'
},
os: {
family: 'Other'
},
device: {
family: 'iPad'
}
}
parserResult = null
const context = {}
const userAgentString = 'Firefox-iOS-FxA/6.0 (Firefox)'
const userAgentString = 'Firefox-iOS-FxA/6.0b42 (iPhone 6S; iPhone OS 10.3) (Nightly)'
const result = userAgent.call(context, userAgentString, log)

assert.equal(result.uaBrowser, 'Firefox')
assert.equal(result.uaBrowser, 'Nightly')
assert.equal(result.uaBrowserVersion, '6.0')
assert.equal(result.uaOS, 'iOS')
assert.equal(result.uaDeviceType, 'tablet')
assert.equal(result.uaOSVersion, '10.3')
assert.equal(result.uaDeviceType, 'mobile')

assert.equal(log.info.callCount, 0)

Expand All @@ -559,28 +540,18 @@ describe('userAgent', () => {
)

it(
'recognises new Firefox-iOS user agents',
'recognises new Firefox-iOS user agent on iPads',
() => {
parserResult = {
ua: {
family: 'Other'
},
os: {
family: 'Other'
},
device: {
family: 'Other'
}
}
parserResult = null
const context = {}
const userAgentString = 'Firefox-iOS-FxA/6.0b42 (iPhone 6S; iPhone OS 10.3) (Nightly)'
const userAgentString = 'Firefox-iOS-FxA/6.0b42 (iPad Mini; iPhone OS 10.3) (Nightly)'
const result = userAgent.call(context, userAgentString, log)

assert.equal(result.uaBrowser, 'Nightly')
assert.equal(result.uaBrowserVersion, '6.0')
assert.equal(result.uaOS, 'iOS')
assert.equal(result.uaOSVersion, '10.3')
assert.equal(result.uaDeviceType, 'mobile')
assert.equal(result.uaDeviceType, 'tablet')

assert.equal(log.info.callCount, 0)

Expand All @@ -591,17 +562,7 @@ describe('userAgent', () => {
it(
'recognises Firefox-Android user agents',
() => {
parserResult = {
ua: {
family: 'Other'
},
os: {
family: 'Android'
},
device: {
family: 'Other'
}
}
parserResult = null
const context = {}
const userAgentString = 'Firefox-Android-FxAccounts/49.0.2 (Firefox)'
const result = userAgent.call(context, userAgentString, log)
Expand All @@ -618,25 +579,15 @@ describe('userAgent', () => {
)

it('recognises new mobile Sync library user agents on Android', () => {
parserResult = {
ua: {
family: 'Other'
},
os: {
family: 'Other'
},
device: {
family: 'Other'
}
}
parserResult = null
const context = {}
const userAgentString = 'Mobile-Android-Sync/(Mobile; Android 4.4.2) (foo() bar)'
const userAgentString = 'Mobile-Android-Sync/(Mobile; Android 6.0) (foo() bar)'
const result = userAgent.call(context, userAgentString, log)

assert.equal(result.uaBrowser, 'foo() bar')
assert.equal(result.uaBrowserVersion, null)
assert.equal(result.uaOS, 'Android')
assert.equal(result.uaOSVersion, '4.4.2')
assert.equal(result.uaOSVersion, '6.0')
assert.equal(result.uaDeviceType, 'mobile')

assert.equal(log.info.callCount, 0)
Expand All @@ -645,17 +596,7 @@ describe('userAgent', () => {
})

it('recognises new mobile Sync library user agents on iOS', () => {
parserResult = {
ua: {
family: 'Other'
},
os: {
family: 'Other'
},
device: {
family: 'Other'
}
}
parserResult = null
const context = {}
const userAgentString = 'Mobile-iOS-Sync/(iPad Mini; iOS 10.3) (wibble)'
const result = userAgent.call(context, userAgentString, log)
Expand Down

0 comments on commit 3f78f6e

Please sign in to comment.