From 26d236642cade914d3d2b1c16f94b6f8e06523cc Mon Sep 17 00:00:00 2001 From: Antonio Barcelos Date: Wed, 24 May 2023 13:47:34 +0200 Subject: [PATCH 1/2] Handling null/undefined platform information in Browser --- .../internal/bolt-agent/browser/bolt-agent.ts | 10 ++++--- .../bolt-agent/browser/bolt-agent.test.ts | 29 +++++++++++++++++++ .../internal/bolt-agent/browser/bolt-agent.ts | 10 ++++--- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/packages/core/src/internal/bolt-agent/browser/bolt-agent.ts b/packages/core/src/internal/bolt-agent/browser/bolt-agent.ts index 69755f85f..b2d2a554c 100644 --- a/packages/core/src/internal/bolt-agent/browser/bolt-agent.ts +++ b/packages/core/src/internal/bolt-agent/browser/bolt-agent.ts @@ -21,7 +21,7 @@ import { BoltAgent } from "../../../types"; interface SystemInfo { - appVersion: string + appVersion?: string } /** @@ -34,14 +34,16 @@ 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 appVersion() { + // @ts-ignore: browser code so must be skipped by ts + return window.navigator.appVersion + } }) ): 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]; + const OS = systemInfo.appVersion != null ? systemInfo.appVersion.split("(")[1].split(")")[0] : undefined return { product: `neo4j-javascript/${version}`, diff --git a/packages/core/test/internal/bolt-agent/browser/bolt-agent.test.ts b/packages/core/test/internal/bolt-agent/browser/bolt-agent.test.ts index e78020574..3d57e2c90 100644 --- a/packages/core/test/internal/bolt-agent/browser/bolt-agent.test.ts +++ b/packages/core/test/internal/bolt-agent/browser/bolt-agent.test.ts @@ -35,4 +35,33 @@ describe('#unit boltAgent', () => { platform: 'Macintosh; Intel Mac OS X 10_15_7' }) }) + + it('should handle null appVersion', () => { + const version = '5.3' + const getSystemInfo = (): any => { + return { + appVersion: null + } + } + + const boltAgent = fromVersion(version, getSystemInfo) + + expect(boltAgent).toEqual({ + product: 'neo4j-javascript/5.3' + }) + }) + + it('should handle undefined appVersion', () => { + const version = '5.3' + const getSystemInfo = (): any => { + return { + } + } + + const boltAgent = fromVersion(version, getSystemInfo) + + expect(boltAgent).toEqual({ + product: 'neo4j-javascript/5.3' + }) + }) }) diff --git a/packages/neo4j-driver-deno/lib/core/internal/bolt-agent/browser/bolt-agent.ts b/packages/neo4j-driver-deno/lib/core/internal/bolt-agent/browser/bolt-agent.ts index 69755f85f..b2d2a554c 100644 --- a/packages/neo4j-driver-deno/lib/core/internal/bolt-agent/browser/bolt-agent.ts +++ b/packages/neo4j-driver-deno/lib/core/internal/bolt-agent/browser/bolt-agent.ts @@ -21,7 +21,7 @@ import { BoltAgent } from "../../../types"; interface SystemInfo { - appVersion: string + appVersion?: string } /** @@ -34,14 +34,16 @@ 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 appVersion() { + // @ts-ignore: browser code so must be skipped by ts + return window.navigator.appVersion + } }) ): 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]; + const OS = systemInfo.appVersion != null ? systemInfo.appVersion.split("(")[1].split(")")[0] : undefined return { product: `neo4j-javascript/${version}`, From 5d2a3eb7c586a5ec7960cfaba4dd192010392493 Mon Sep 17 00:00:00 2001 From: Antonio Barcelos Date: Wed, 24 May 2023 14:13:03 +0200 Subject: [PATCH 2/2] Use user agent instead of appVersion --- .../src/internal/bolt-agent/browser/bolt-agent.ts | 15 +++++++++------ .../bolt-agent/browser/bolt-agent.test.ts | 12 +++++++----- .../internal/bolt-agent/browser/bolt-agent.ts | 15 +++++++++------ 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/packages/core/src/internal/bolt-agent/browser/bolt-agent.ts b/packages/core/src/internal/bolt-agent/browser/bolt-agent.ts index b2d2a554c..ae29b1048 100644 --- a/packages/core/src/internal/bolt-agent/browser/bolt-agent.ts +++ b/packages/core/src/internal/bolt-agent/browser/bolt-agent.ts @@ -21,7 +21,7 @@ import { BoltAgent } from "../../../types"; interface SystemInfo { - appVersion?: string + userAgent?: string } /** @@ -34,20 +34,23 @@ interface SystemInfo { export function fromVersion ( version: string, getSystemInfo: () => SystemInfo = () => ({ - get appVersion() { + get userAgent() { // @ts-ignore: browser code so must be skipped by ts - return window.navigator.appVersion + 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 != null ? systemInfo.appVersion.split("(")[1].split(")")[0] : undefined + //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 */ diff --git a/packages/core/test/internal/bolt-agent/browser/bolt-agent.test.ts b/packages/core/test/internal/bolt-agent/browser/bolt-agent.test.ts index 3d57e2c90..4f882ae75 100644 --- a/packages/core/test/internal/bolt-agent/browser/bolt-agent.test.ts +++ b/packages/core/test/internal/bolt-agent/browser/bolt-agent.test.ts @@ -22,9 +22,10 @@ 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 } } @@ -32,15 +33,16 @@ describe('#unit boltAgent', () => { 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 appVersion', () => { + it('should handle null userAgent', () => { const version = '5.3' const getSystemInfo = (): any => { return { - appVersion: null + userAgent: null } } @@ -51,7 +53,7 @@ describe('#unit boltAgent', () => { }) }) - it('should handle undefined appVersion', () => { + it('should handle undefined userAgent', () => { const version = '5.3' const getSystemInfo = (): any => { return { diff --git a/packages/neo4j-driver-deno/lib/core/internal/bolt-agent/browser/bolt-agent.ts b/packages/neo4j-driver-deno/lib/core/internal/bolt-agent/browser/bolt-agent.ts index b2d2a554c..ae29b1048 100644 --- a/packages/neo4j-driver-deno/lib/core/internal/bolt-agent/browser/bolt-agent.ts +++ b/packages/neo4j-driver-deno/lib/core/internal/bolt-agent/browser/bolt-agent.ts @@ -21,7 +21,7 @@ import { BoltAgent } from "../../../types"; interface SystemInfo { - appVersion?: string + userAgent?: string } /** @@ -34,20 +34,23 @@ interface SystemInfo { export function fromVersion ( version: string, getSystemInfo: () => SystemInfo = () => ({ - get appVersion() { + get userAgent() { // @ts-ignore: browser code so must be skipped by ts - return window.navigator.appVersion + 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 != null ? systemInfo.appVersion.split("(")[1].split(")")[0] : undefined + //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 */