From 62f602789cde95085471cb4387017d8fcd34f115 Mon Sep 17 00:00:00 2001 From: Antonio Barcelos Date: Fri, 9 Jun 2023 10:04:31 +0200 Subject: [PATCH] Fix browser `boltAgent` in web workers context The driver was trying to access the `window` object inside web workers. This object doesn't exist in this context. So, the driver was throwing an error and not creating connections at all. This problem is solved by access the `navigator` directy from the global scope (which is the `window` outside the web workers). Some guards were added to avoid the code break in case `navigator` is not defined. --- .../core/src/internal/bolt-agent/browser/bolt-agent.ts | 6 +++++- .../internal/bolt-agent/browser/bolt-agent.test.ts | 10 ++++++++++ .../lib/core/internal/bolt-agent/browser/bolt-agent.ts | 6 +++++- 3 files changed, 20 insertions(+), 2 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 ae29b1048..f31dcec51 100644 --- a/packages/core/src/internal/bolt-agent/browser/bolt-agent.ts +++ b/packages/core/src/internal/bolt-agent/browser/bolt-agent.ts @@ -35,8 +35,12 @@ export function fromVersion ( version: string, getSystemInfo: () => SystemInfo = () => ({ get userAgent() { + // this should be defined as an `var` since we need to get information + // came from the global scope which not always will be defined + // and we don't want to override the information + var navigator // @ts-ignore: browser code so must be skipped by ts - return window.navigator.userAgent + return navigator?.userAgent } }) ): BoltAgent { 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 4f882ae75..3ad87b033 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 @@ -66,4 +66,14 @@ describe('#unit boltAgent', () => { product: 'neo4j-javascript/5.3' }) }) + + it('should handle navigator object does not exist in the default getSystemInfo', () => { + const version = '5.3' + + const boltAgent = fromVersion(version) + + 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 ae29b1048..f31dcec51 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 @@ -35,8 +35,12 @@ export function fromVersion ( version: string, getSystemInfo: () => SystemInfo = () => ({ get userAgent() { + // this should be defined as an `var` since we need to get information + // came from the global scope which not always will be defined + // and we don't want to override the information + var navigator // @ts-ignore: browser code so must be skipped by ts - return window.navigator.userAgent + return navigator?.userAgent } }) ): BoltAgent {