Skip to content

Implements BoltAgent as structure #3

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 1 commit into from
May 17, 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
11 changes: 10 additions & 1 deletion packages/bolt-connection/src/bolt/request-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,21 @@ export default class RequestMessage {
* @return {RequestMessage} new HELLO message.
*/
static hello5x3 (userAgent, boltAgent, notificationFilter = null, routing = null) {
const metadata = { bolt_agent: boltAgent }
const metadata = { }

if (userAgent) {
metadata.user_agent = userAgent
}

if (boltAgent) {
metadata.bolt_agent = {
product: boltAgent.product,
platform: boltAgent.platform,
language: boltAgent.language,
language_details: boltAgent.languageDetails
}
}

if (notificationFilter) {
if (notificationFilter.minimumSeverityLevel) {
metadata.notifications_minimum_severity = notificationFilter.minimumSeverityLevel
Expand Down
16 changes: 12 additions & 4 deletions packages/bolt-connection/test/bolt/bolt-protocol-v5x3.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,11 @@ describe('#unit BoltProtocolV5x3', () => {
utils.spyProtocolWrite(protocol)

const clientName = 'js-driver/1.2.3'
const boltAgent = 'js-driver/1.2.3 (bolt agent)'
const boltAgent = {
product: 'neo4j-javascript/5.6',
platform: 'netbsd 1.1.1; Some arch',
languageDetails: 'Node/16.0.1 (v8 1.7.0)'
}
const authToken = { username: 'neo4j', password: 'secret' }

const observer = protocol.initialize({ userAgent: clientName, boltAgent: boltAgent, authToken })
Expand Down Expand Up @@ -306,14 +310,18 @@ describe('#unit BoltProtocolV5x3', () => {
const protocol = new BoltProtocolV5x3(recorder, null, false)
utils.spyProtocolWrite(protocol)

const clientName = 'js-driver/1.2.3'
const boltAgent = {
product: 'neo4j-javascript/5.6',
platform: 'netbsd 1.1.1; Some arch',
languageDetails: 'Node/16.0.1 (v8 1.7.0)'
}
const authToken = { username: 'neo4j', password: 'secret' }

const observer = protocol.initialize({ userAgent, boltAgent: clientName, authToken })
const observer = protocol.initialize({ userAgent, boltAgent, authToken })

protocol.verifyMessageCount(2)
expect(protocol.messages[0]).toBeMessage(
RequestMessage.hello5x3(userAgent, clientName)
RequestMessage.hello5x3(userAgent, boltAgent)
)
expect(protocol.messages[1]).toBeMessage(
RequestMessage.logon(authToken)
Expand Down
135 changes: 119 additions & 16 deletions packages/bolt-connection/test/bolt/request-message.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,22 +531,125 @@ describe('#unit RequestMessage', () => {
)
})
})
})

function notificationFilterFixtures () {
return notificationFilterBehaviour.notificationFilterFixture()
.map(notificationFilter => {
const expectedNotificationFilter = {}
if (notificationFilter) {
if (notificationFilter.minimumSeverityLevel) {
expectedNotificationFilter.notifications_minimum_severity = notificationFilter.minimumSeverityLevel
}

if (notificationFilter.disabledCategories) {
expectedNotificationFilter.notifications_disabled_categories = notificationFilter.disabledCategories
}
}
return [notificationFilter, expectedNotificationFilter]
})
}
describe('Bolt5.3', () => {
it('should create HELLO with NodeJS Bolt Agent', () => {
const userAgent = 'my-driver/1.0.2'
const boltAgent = {
product: 'neo4j-javascript/5.6',
platform: 'netbsd 1.1.1; Some arch',
languageDetails: 'Node/16.0.1 (v8 1.7.0)'
}

const expectedFields = {
user_agent: userAgent,
bolt_agent: {
product: 'neo4j-javascript/5.6',
platform: 'netbsd 1.1.1; Some arch',
language_details: 'Node/16.0.1 (v8 1.7.0)'
}
}

const message = RequestMessage.hello5x3(userAgent, boltAgent)

expect(message.signature).toEqual(0x01)
expect(message.fields).toEqual([
expectedFields
])
expect(message.toString()).toEqual(
`HELLO ${json.stringify(expectedFields)}`
)
})

it('should create HELLO with Browser Bolt Agent', () => {
const userAgent = 'my-driver/1.0.2'

const boltAgent = {
product: 'neo4j-javascript/5.3',
platform: 'Macintosh; Intel Mac OS X 10_15_7'
}

const expectedFields = {
user_agent: userAgent,
bolt_agent: {
product: 'neo4j-javascript/5.3',
platform: 'Macintosh; Intel Mac OS X 10_15_7'
}
}

const message = RequestMessage.hello5x3(userAgent, boltAgent)

expect(message.signature).toEqual(0x01)
expect(message.fields).toEqual([
expectedFields
])
expect(message.toString()).toEqual(
`HELLO ${json.stringify(expectedFields)}`
)
})

it('should create HELLO with Deno Bolt Agent', () => {
const userAgent = 'my-driver/1.0.2'

const boltAgent = {
product: 'neo4j-javascript/5.3',
platform: 'macos 14.1; myArch',
languageDetails: 'Deno/1.19.1 (v8 8.1.39)'
}

const expectedFields = {
user_agent: userAgent,
bolt_agent: {
product: 'neo4j-javascript/5.3',
platform: 'macos 14.1; myArch',
language_details: 'Deno/1.19.1 (v8 8.1.39)'
}
}

const message = RequestMessage.hello5x3(userAgent, boltAgent)

expect(message.signature).toEqual(0x01)
expect(message.fields).toEqual([
expectedFields
])
expect(message.toString()).toEqual(
`HELLO ${json.stringify(expectedFields)}`
)
})

it.each(
notificationFilterFixtures()
)('should create HELLO message where notificationFilters=%o', (notificationFilter, expectedNotificationFilter) => {
const userAgent = 'my-driver/1.0.2'
const message = RequestMessage.hello5x3(userAgent, undefined, notificationFilter)

const expectedFields = { user_agent: userAgent, ...expectedNotificationFilter }

expect(message.signature).toEqual(0x01)
expect(message.fields).toEqual([
expectedFields
])
expect(message.toString()).toEqual(
`HELLO ${json.stringify(expectedFields)}`
)
})
})

function notificationFilterFixtures () {
return notificationFilterBehaviour.notificationFilterFixture()
.map(notificationFilter => {
const expectedNotificationFilter = {}
if (notificationFilter) {
if (notificationFilter.minimumSeverityLevel) {
expectedNotificationFilter.notifications_minimum_severity = notificationFilter.minimumSeverityLevel
}

if (notificationFilter.disabledCategories) {
expectedNotificationFilter.notifications_disabled_categories = notificationFilter.disabledCategories
}
}
return [notificationFilter, expectedNotificationFilter]
})
}
})
33 changes: 27 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 @@ -17,14 +17,35 @@
* limitations under the License.
*/
/* eslint-disable */
// @ts-ignore: browser code so must be skipped by ts
export function fromVersion (version: string, windowProvider = () => window): string {
// @ts-ignore: browser code so must be skipped by ts
const APP_VERSION = windowProvider().navigator.appVersion

import { BoltAgent } from "../../../types";

interface SystemInfo {
appVersion: string
}

/**
* Constructs a BoltAgent structure from a given product version.
*
* @param {string} version The product version
* @param {function():SystemInfo} getSystemInfo Parameter used of inject system information and mock calls to the APIs.
* @returns {BoltAgent} The bolt agent
*/
export function fromVersion (
version: string,
getSystemInfo: () => SystemInfo = () => ({
// @ts-ignore: browser code so must be skipped by ts
get appVersion(): 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 = APP_VERSION.split("(")[1].split(")")[0];
const OS = systemInfo.appVersion.split("(")[1].split(")")[0];

return `neo4j-javascript/${version} (${OS})`
return {
product: `neo4j-javascript/${version}`,
platform: OS
}
}
/* eslint-enable */
52 changes: 42 additions & 10 deletions packages/core/src/internal/bolt-agent/deno/bolt-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,49 @@
* limitations under the License.
*/

import { BoltAgent } from '../../../types.ts'

export interface SystemInfo {
hostArch: string
denoVersion: string
v8Version: string
osVersion: string
osRelease: string
}

/**
* Constructs a BoltAgent structure from a given product version.
*
* @param {string} version The product version
* @param {function():SystemInfo} getSystemInfo Parameter used of inject system information and mock calls to the APIs.
* @returns {BoltAgent} The bolt agent
*/
/* eslint-disable */
export function fromVersion (version: string): string {
//@ts-ignore
const HOST_ARCH = Deno.build.arch
//@ts-ignore
const DENO_VERSION = `Deno/${Deno.version.deno}`
//@ts-ignore
const NODE_V8_VERSION = Deno.version.v8
//@ts-ignore
const OS_NAME_VERSION = `${Deno.build.os} ${Deno.osRelease ? Deno.osRelease() : ''}`.trim()
export function fromVersion (
version: string,
getSystemInfo: () => SystemInfo = () => ({
//@ts-ignore
hostArch: Deno.build.arch,
//@ts-ignore
denoVersion: Deno.version.deno,
//@ts-ignore:
v8Version: Deno.version.v8,
//@ts-ignore
osVersion: Deno.build.os,
get osRelease() {
//@ts-ignore
return Deno.osRelease ? Deno.osRelease() : ''
}
})
): BoltAgent {
const systemInfo = getSystemInfo()
const DENO_VERSION = `Deno/${systemInfo.denoVersion}`
const OS_NAME_VERSION = `${systemInfo.osVersion} ${systemInfo.osRelease}`.trim()

return `neo4j-javascript/${version} (${OS_NAME_VERSION}; ${HOST_ARCH}) ${DENO_VERSION} (v8 ${NODE_V8_VERSION})`
return {
product: `neo4j-javascript/${version}`,
platform: `${OS_NAME_VERSION}; ${systemInfo.hostArch}`,
languageDetails: `${DENO_VERSION} (v8 ${systemInfo.v8Version})`
}
}
/* eslint-enable */
50 changes: 43 additions & 7 deletions packages/core/src/internal/bolt-agent/node/bolt-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,49 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as os from 'os'
import { platform, release } from 'os'
import { BoltAgent } from '../../../types'

export function fromVersion (version: string): string {
const HOST_ARCH = process.config.variables.host_arch
const NODE_VERSION = 'Node/' + process.versions.node
const NODE_V8_VERSION = process.versions.v8
const OS_NAME_VERSION = `${os.platform()} ${os.release()}`
interface SystemInfo {
hostArch: string
nodeVersion: string
v8Version: string
platform: NodeJS.Platform
release: string
}

/**
* Constructs a BoltAgent structure from a given product version.
*
* @param {string} version The product version
* @param {function():SystemInfo} getSystemInfo Parameter used of inject system information and mock calls to the APIs.
* @returns {BoltAgent} The bolt agent
*/
export function fromVersion (
version: string,
getSystemInfo: () => SystemInfo = () => ({
hostArch: process.config.variables.host_arch,
nodeVersion: process.versions.node,
v8Version: process.versions.v8,
get platform () {
return platform()
},
get release () {
return release()
}
})
): BoltAgent {
const systemInfo = getSystemInfo()
const HOST_ARCH = systemInfo.hostArch
const NODE_VERSION = 'Node/' + systemInfo.nodeVersion
const NODE_V8_VERSION = systemInfo.v8Version
const OS_NAME_VERSION = `${systemInfo.platform} ${systemInfo.release}`

return `neo4j-javascript/${version} (${OS_NAME_VERSION}; ${HOST_ARCH}) ${NODE_VERSION} (v8 ${NODE_V8_VERSION})`
return {
product: `neo4j-javascript/${version}`,
platform: `${OS_NAME_VERSION}; ${HOST_ARCH}`,
languageDetails: `${NODE_VERSION} (v8 ${NODE_V8_VERSION})`
}
}

export type { SystemInfo }
9 changes: 8 additions & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ export interface AuthToken {
parameters?: Parameters
}

export interface BoltAgent {
product?: string
platform?: string
language?: string
languageDetails?: string
}

export interface Config {
encrypted?: boolean | EncryptionLevel
trust?: TrustStrategy
Expand All @@ -65,7 +72,7 @@ export interface Config {
logging?: LoggingConfig
resolver?: (address: string) => string[] | Promise<string[]>
userAgent?: string
boltAgent?: string
boltAgent?: BoltAgent
}

/**
Expand Down
Loading