Skip to content

Commit 9c2519a

Browse files
authored
Fix browser boltAgent in web workers context (#1094)
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` directly 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.
1 parent 5127f3a commit 9c2519a

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,12 @@ export function fromVersion (
3535
version: string,
3636
getSystemInfo: () => SystemInfo = () => ({
3737
get userAgent() {
38+
// this should be defined as an `var` since we need to get information
39+
// came from the global scope which not always will be defined
40+
// and we don't want to override the information
41+
var navigator
3842
// @ts-ignore: browser code so must be skipped by ts
39-
return window.navigator.userAgent
43+
return navigator?.userAgent
4044
}
4145
})
4246
): BoltAgent {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,14 @@ describe('#unit boltAgent', () => {
6666
product: 'neo4j-javascript/5.3'
6767
})
6868
})
69+
70+
it('should handle navigator object does not exist in the default getSystemInfo', () => {
71+
const version = '5.3'
72+
73+
const boltAgent = fromVersion(version)
74+
75+
expect(boltAgent).toEqual({
76+
product: 'neo4j-javascript/5.3'
77+
})
78+
})
6979
})

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,12 @@ export function fromVersion (
3535
version: string,
3636
getSystemInfo: () => SystemInfo = () => ({
3737
get userAgent() {
38+
// this should be defined as an `var` since we need to get information
39+
// came from the global scope which not always will be defined
40+
// and we don't want to override the information
41+
var navigator
3842
// @ts-ignore: browser code so must be skipped by ts
39-
return window.navigator.userAgent
43+
return navigator?.userAgent
4044
}
4145
})
4246
): BoltAgent {

0 commit comments

Comments
 (0)