Skip to content

Handling null/undefined platform information in Browser #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions packages/core/src/internal/bolt-agent/browser/bolt-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import { BoltAgent } from "../../../types";

interface SystemInfo {
appVersion: string
userAgent?: string
}

/**
Expand All @@ -34,18 +34,23 @@ interface SystemInfo {
export function fromVersion (
version: string,
getSystemInfo: () => SystemInfo = () => ({
// @ts-ignore: browser code so must be skipped by ts
get appVersion(): window.navigator.appVersion
get userAgent() {
// @ts-ignore: browser code so must be skipped by ts
return window.navigator.userAgent
}
})
): BoltAgent {
const systemInfo = getSystemInfo()

//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
const OS = systemInfo.appVersion.split("(")[1].split(")")[0];
//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'

const platform = systemInfo.userAgent != null ? systemInfo.userAgent.split("(")[1].split(")")[0] : undefined
const languageDetails = systemInfo.userAgent || undefined

return {
product: `neo4j-javascript/${version}`,
platform: OS
platform,
languageDetails
}
}
/* eslint-enable */
35 changes: 33 additions & 2 deletions packages/core/test/internal/bolt-agent/browser/bolt-agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,48 @@ describe('#unit boltAgent', () => {
// This test is very fragile but the exact look of this string should not change without PM approval
it('should return the correct bolt agent for specified version', () => {
const version = '5.3'
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'
const getSystemInfo = (): any => {
return {
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'
userAgent
}
}

const boltAgent = fromVersion(version, getSystemInfo)

expect(boltAgent).toEqual({
product: 'neo4j-javascript/5.3',
platform: 'Macintosh; Intel Mac OS X 10_15_7'
platform: 'Macintosh; Intel Mac OS X 10_15_7',
languageDetails: userAgent
})
})

it('should handle null userAgent', () => {
const version = '5.3'
const getSystemInfo = (): any => {
return {
userAgent: null
}
}

const boltAgent = fromVersion(version, getSystemInfo)

expect(boltAgent).toEqual({
product: 'neo4j-javascript/5.3'
})
})

it('should handle undefined userAgent', () => {
const version = '5.3'
const getSystemInfo = (): any => {
return {
}
}

const boltAgent = fromVersion(version, getSystemInfo)

expect(boltAgent).toEqual({
product: 'neo4j-javascript/5.3'
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import { BoltAgent } from "../../../types";

interface SystemInfo {
appVersion: string
userAgent?: string
}

/**
Expand All @@ -34,18 +34,23 @@ interface SystemInfo {
export function fromVersion (
version: string,
getSystemInfo: () => SystemInfo = () => ({
// @ts-ignore: browser code so must be skipped by ts
get appVersion(): window.navigator.appVersion
get userAgent() {
// @ts-ignore: browser code so must be skipped by ts
return window.navigator.userAgent
}
})
): BoltAgent {
const systemInfo = getSystemInfo()

//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
const OS = systemInfo.appVersion.split("(")[1].split(")")[0];
//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'

const platform = systemInfo.userAgent != null ? systemInfo.userAgent.split("(")[1].split(")")[0] : undefined
const languageDetails = systemInfo.userAgent || undefined

return {
product: `neo4j-javascript/${version}`,
platform: OS
platform,
languageDetails
}
}
/* eslint-enable */