Skip to content

Commit 07239dd

Browse files
authored
Merge pull request #4 from bigmontz/bolt-agent
Handling null/undefined platform information in Browser
2 parents 690256c + 5d2a3eb commit 07239dd

File tree

3 files changed

+55
-14
lines changed

3 files changed

+55
-14
lines changed

packages/core/src/internal/bolt-agent/browser/bolt-agent.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import { BoltAgent } from "../../../types";
2222

2323
interface SystemInfo {
24-
appVersion: string
24+
userAgent?: string
2525
}
2626

2727
/**
@@ -34,18 +34,23 @@ interface SystemInfo {
3434
export function fromVersion (
3535
version: string,
3636
getSystemInfo: () => SystemInfo = () => ({
37-
// @ts-ignore: browser code so must be skipped by ts
38-
get appVersion(): window.navigator.appVersion
37+
get userAgent() {
38+
// @ts-ignore: browser code so must be skipped by ts
39+
return window.navigator.userAgent
40+
}
3941
})
4042
): BoltAgent {
4143
const systemInfo = getSystemInfo()
4244

43-
//APP_VERSION looks like 5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36
44-
const OS = systemInfo.appVersion.split("(")[1].split(")")[0];
45+
//USER_AGENT looks like 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36'
46+
47+
const platform = systemInfo.userAgent != null ? systemInfo.userAgent.split("(")[1].split(")")[0] : undefined
48+
const languageDetails = systemInfo.userAgent || undefined
4549

4650
return {
4751
product: `neo4j-javascript/${version}`,
48-
platform: OS
52+
platform,
53+
languageDetails
4954
}
5055
}
5156
/* eslint-enable */

packages/core/test/internal/bolt-agent/browser/bolt-agent.test.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,48 @@ describe('#unit boltAgent', () => {
2222
// This test is very fragile but the exact look of this string should not change without PM approval
2323
it('should return the correct bolt agent for specified version', () => {
2424
const version = '5.3'
25+
const userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36'
2526
const getSystemInfo = (): any => {
2627
return {
27-
appVersion: '5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36'
28+
userAgent
2829
}
2930
}
3031

3132
const boltAgent = fromVersion(version, getSystemInfo)
3233

3334
expect(boltAgent).toEqual({
3435
product: 'neo4j-javascript/5.3',
35-
platform: 'Macintosh; Intel Mac OS X 10_15_7'
36+
platform: 'Macintosh; Intel Mac OS X 10_15_7',
37+
languageDetails: userAgent
38+
})
39+
})
40+
41+
it('should handle null userAgent', () => {
42+
const version = '5.3'
43+
const getSystemInfo = (): any => {
44+
return {
45+
userAgent: null
46+
}
47+
}
48+
49+
const boltAgent = fromVersion(version, getSystemInfo)
50+
51+
expect(boltAgent).toEqual({
52+
product: 'neo4j-javascript/5.3'
53+
})
54+
})
55+
56+
it('should handle undefined userAgent', () => {
57+
const version = '5.3'
58+
const getSystemInfo = (): any => {
59+
return {
60+
}
61+
}
62+
63+
const boltAgent = fromVersion(version, getSystemInfo)
64+
65+
expect(boltAgent).toEqual({
66+
product: 'neo4j-javascript/5.3'
3667
})
3768
})
3869
})

packages/neo4j-driver-deno/lib/core/internal/bolt-agent/browser/bolt-agent.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import { BoltAgent } from "../../../types";
2222

2323
interface SystemInfo {
24-
appVersion: string
24+
userAgent?: string
2525
}
2626

2727
/**
@@ -34,18 +34,23 @@ interface SystemInfo {
3434
export function fromVersion (
3535
version: string,
3636
getSystemInfo: () => SystemInfo = () => ({
37-
// @ts-ignore: browser code so must be skipped by ts
38-
get appVersion(): window.navigator.appVersion
37+
get userAgent() {
38+
// @ts-ignore: browser code so must be skipped by ts
39+
return window.navigator.userAgent
40+
}
3941
})
4042
): BoltAgent {
4143
const systemInfo = getSystemInfo()
4244

43-
//APP_VERSION looks like 5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36
44-
const OS = systemInfo.appVersion.split("(")[1].split(")")[0];
45+
//USER_AGENT looks like 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36'
46+
47+
const platform = systemInfo.userAgent != null ? systemInfo.userAgent.split("(")[1].split(")")[0] : undefined
48+
const languageDetails = systemInfo.userAgent || undefined
4549

4650
return {
4751
product: `neo4j-javascript/${version}`,
48-
platform: OS
52+
platform,
53+
languageDetails
4954
}
5055
}
5156
/* eslint-enable */

0 commit comments

Comments
 (0)