Skip to content

Commit 28dd4c9

Browse files
technigePeter Wilhelmsson
authored andcommitted
Use protocol version instead of server agent string
1 parent e81bde8 commit 28dd4c9

30 files changed

+457
-454
lines changed

src/internal/bolt-protocol-v4.js renamed to src/internal/bolt-protocol-v4x0.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
import BoltProtocolV3 from './bolt-protocol-v3'
2020
import RequestMessage, { ALL } from './request-message'
2121
import { ResultStreamObserver } from './stream-observers'
22-
import { BOLT_PROTOCOL_V4 } from './constants'
22+
import { BOLT_PROTOCOL_V4_0 } from './constants'
2323

2424
export default class BoltProtocol extends BoltProtocolV3 {
2525
get version () {
26-
return BOLT_PROTOCOL_V4
26+
return BOLT_PROTOCOL_V4_0
2727
}
2828

2929
beginTransaction ({

src/internal/connection-provider-direct.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import PooledConnectionProvider from './connection-provider-pooled'
2121
import DelegateConnection from './connection-delegate'
2222
import ChannelConnection from './connection-channel'
23-
import { BOLT_PROTOCOL_V4, BOLT_PROTOCOL_V3 } from './constants'
23+
import { BOLT_PROTOCOL_V4_0, BOLT_PROTOCOL_V3 } from './constants'
2424

2525
export default class DirectConnectionProvider extends PooledConnectionProvider {
2626
constructor ({ id, config, log, address, userAgent, authToken }) {
@@ -63,7 +63,7 @@ export default class DirectConnectionProvider extends PooledConnectionProvider {
6363

6464
async supportsMultiDb () {
6565
return await this._hasProtocolVersion(
66-
version => version >= BOLT_PROTOCOL_V4
66+
version => version >= BOLT_PROTOCOL_V4_0
6767
)
6868
}
6969

src/internal/connection-provider-routing.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,14 @@ import Rediscovery from './rediscovery'
2525
import RoutingUtil from './routing-util'
2626
import { HostNameResolver } from './node'
2727
import SingleConnectionProvider from './connection-provider-single'
28-
import { ServerVersion, VERSION_4_0_0 } from './server-version'
2928
import PooledConnectionProvider from './connection-provider-pooled'
3029
import ConnectionErrorHandler from './connection-error-handler'
3130
import DelegateConnection from './connection-delegate'
3231
import LeastConnectedLoadBalancingStrategy from './least-connected-load-balancing-strategy'
3332
import Bookmark from './bookmark'
3433
import ChannelConnection from './connection-channel'
3534
import { int } from '../integer'
36-
import { BOLT_PROTOCOL_V4, BOLT_PROTOCOL_V3 } from './constants'
35+
import { BOLT_PROTOCOL_V3, BOLT_PROTOCOL_V4_0 } from './constants'
3736

3837
const UNAUTHORIZED_ERROR_CODE = 'Neo.ClientError.Security.Unauthorized'
3938
const DATABASE_NOT_FOUND_ERROR_CODE =
@@ -189,7 +188,7 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
189188

190189
async supportsMultiDb () {
191190
return await this._hasProtocolVersion(
192-
version => version >= BOLT_PROTOCOL_V4
191+
version => version >= BOLT_PROTOCOL_V4_0
193192
)
194193
}
195194

@@ -424,8 +423,8 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
424423
const connection = await this._connectionPool.acquire(routerAddress)
425424
const connectionProvider = new SingleConnectionProvider(connection)
426425

427-
const version = ServerVersion.fromString(connection.version)
428-
if (version.compareTo(VERSION_4_0_0) < 0) {
426+
const protocolVersion = connection.protocol().version
427+
if (protocolVersion < 4.0) {
429428
return new Session({
430429
mode: WRITE,
431430
bookmark: Bookmark.empty(),

src/internal/constants.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ const ACCESS_MODE_WRITE = 'WRITE'
2323
const BOLT_PROTOCOL_V1 = 1
2424
const BOLT_PROTOCOL_V2 = 2
2525
const BOLT_PROTOCOL_V3 = 3
26-
const BOLT_PROTOCOL_V4 = 4
26+
const BOLT_PROTOCOL_V4_0 = 4.0
2727

2828
export {
2929
ACCESS_MODE_READ,
3030
ACCESS_MODE_WRITE,
3131
BOLT_PROTOCOL_V1,
3232
BOLT_PROTOCOL_V2,
3333
BOLT_PROTOCOL_V3,
34-
BOLT_PROTOCOL_V4
34+
BOLT_PROTOCOL_V4_0
3535
}

src/internal/protocol-handshaker.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ import { newError } from '../error'
2222
import BoltProtocolV1 from './bolt-protocol-v1'
2323
import BoltProtocolV2 from './bolt-protocol-v2'
2424
import BoltProtocolV3 from './bolt-protocol-v3'
25-
import BoltProtocolV4 from './bolt-protocol-v4'
25+
import BoltProtocolV4x0 from './bolt-protocol-v4x0'
2626

27-
const HTTP_MAGIC_PREAMBLE = 1213486160 // == 0x48545450 == "HTTP"
2827
const BOLT_MAGIC_PREAMBLE = 0x6060b017
2928

3029
export default class ProtocolHandshaker {
@@ -58,7 +57,19 @@ export default class ProtocolHandshaker {
5857
* @throws {Neo4jError} when bolt protocol can't be instantiated.
5958
*/
6059
createNegotiatedProtocol (buffer) {
61-
const negotiatedVersion = buffer.readInt32()
60+
const h = [
61+
buffer.readUInt8(),
62+
buffer.readUInt8(),
63+
buffer.readUInt8(),
64+
buffer.readUInt8()
65+
]
66+
if (h[0] === 0x48 && h[1] === 0x54 && h[2] === 0x54 && h[3] === 0x50) {
67+
throw newError(
68+
'Server responded HTTP. Make sure you are not trying to connect to the http endpoint ' +
69+
'(HTTP defaults to port 7474 whereas BOLT defaults to port 7687)'
70+
)
71+
}
72+
const negotiatedVersion = Number(h[3] + '.' + h[2])
6273
if (this._log.isDebugEnabled()) {
6374
this._log.debug(
6475
`${this._connection} negotiated protocol version ${negotiatedVersion}`
@@ -91,17 +102,12 @@ export default class ProtocolHandshaker {
91102
this._chunker,
92103
this._disableLosslessIntegers
93104
)
94-
case 4:
95-
return new BoltProtocolV4(
105+
case 4.0:
106+
return new BoltProtocolV4x0(
96107
this._connection,
97108
this._chunker,
98109
this._disableLosslessIntegers
99110
)
100-
case HTTP_MAGIC_PREAMBLE:
101-
throw newError(
102-
'Server responded HTTP. Make sure you are not trying to connect to the http endpoint ' +
103-
'(HTTP defaults to port 7474 whereas BOLT defaults to port 7687)'
104-
)
105111
default:
106112
throw newError('Unknown Bolt protocol version: ' + version)
107113
}

src/internal/routing-util.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ export default class RoutingUtil {
141141
let query
142142
let params
143143

144-
const version = ServerVersion.fromString(connection.version)
145-
if (version.compareTo(VERSION_4_0_0) >= 0) {
144+
const protocolVersion = connection.protocol().version
145+
if (protocolVersion >= 4.0) {
146146
query = CALL_GET_ROUTING_TABLE_MULTI_DB
147147
params = {
148148
context: this._routingContext,

src/result-summary.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ class ResultSummary {
2929
* @param {string} query - The query this summary is for
3030
* @param {Object} parameters - Parameters for the query
3131
* @param {Object} metadata - Query metadata
32+
* @param {number} protocolVersion - Bolt protocol version
3233
*/
33-
constructor (query, parameters, metadata) {
34+
constructor (query, parameters, metadata, protocolVersion) {
3435
/**
3536
* The query and parameters this summary is for.
3637
* @type {{text: string, parameters: Object}}
@@ -96,7 +97,7 @@ class ResultSummary {
9697
* @type {ServerInfo}
9798
* @public
9899
*/
99-
this.server = new ServerInfo(metadata.server)
100+
this.server = new ServerInfo(metadata.server, protocolVersion)
100101

101102
/**
102103
* The time it took the server to consume the result.
@@ -313,12 +314,14 @@ class ServerInfo {
313314
* Create a ServerInfo instance
314315
* @constructor
315316
* @param {Object} serverMeta - Object with serverMeta data
317+
* @param {number} protocolVersion - Bolt protocol version
316318
*/
317-
constructor (serverMeta) {
319+
constructor (serverMeta, protocolVersion) {
318320
if (serverMeta) {
319321
this.address = serverMeta.address
320322
this.version = serverMeta.version
321323
}
324+
this.protocolVersion = protocolVersion
322325
}
323326
}
324327

src/result.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,34 @@ class Result {
162162
subscribe (observer) {
163163
const onCompletedOriginal = observer.onCompleted || DEFAULT_ON_COMPLETED
164164
const onCompletedWrapper = metadata => {
165-
// notify connection holder that the used connection is not needed any more because result has
166-
// been fully consumed; call the original onCompleted callback after that
167-
this._connectionHolder.releaseConnection().then(() => {
168-
onCompletedOriginal.call(
169-
observer,
170-
new ResultSummary(this._query, this._parameters, metadata)
171-
)
172-
})
165+
const connectionHolder = this._connectionHolder
166+
const query = this._query
167+
const parameters = this._parameters
168+
169+
function release (protocolVersion) {
170+
// notify connection holder that the used connection is not needed any more because result has
171+
// been fully consumed; call the original onCompleted callback after that
172+
connectionHolder.releaseConnection().then(() => {
173+
onCompletedOriginal.call(
174+
observer,
175+
new ResultSummary(query, parameters, metadata, protocolVersion)
176+
)
177+
})
178+
}
179+
180+
connectionHolder.getConnection().then(
181+
// onFulfilled:
182+
connection => {
183+
release(connection ? connection.protocol().version : undefined)
184+
},
185+
186+
// onRejected:
187+
_ => {
188+
release()
189+
}
190+
)
173191
}
192+
174193
observer.onCompleted = onCompletedWrapper
175194

176195
const onErrorOriginal = observer.onError || DEFAULT_ON_ERROR

test/bolt-v3.test.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import neo4j from '../src'
2121
import sharedNeo4j from './internal/shared-neo4j'
22-
import { ServerVersion, VERSION_3_5_0 } from '../src/internal/server-version'
2322

2423
const TX_CONFIG_WITH_METADATA = { metadata: { a: 1, b: 2 } }
2524
const TX_CONFIG_WITH_TIMEOUT = { timeout: 42 }
@@ -34,7 +33,7 @@ const INVALID_METADATA_VALUES = [
3433
describe('#integration Bolt V3 API', () => {
3534
let driver
3635
let session
37-
let serverVersion
36+
let protocolVersion
3837
let originalTimeout
3938

4039
beforeEach(async () => {
@@ -43,7 +42,7 @@ describe('#integration Bolt V3 API', () => {
4342
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL
4443
jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000
4544

46-
serverVersion = await sharedNeo4j.cleanupAndGetVersion(driver)
45+
protocolVersion = await sharedNeo4j.cleanupAndGetProtocolVersion(driver)
4746
})
4847

4948
afterEach(async () => {
@@ -497,6 +496,6 @@ describe('#integration Bolt V3 API', () => {
497496
}
498497

499498
function databaseSupportsBoltV3 () {
500-
return serverVersion.compareTo(VERSION_3_5_0) >= 0
499+
return protocolVersion >= 3
501500
}
502501
})

test/bolt-v4.test.js renamed to test/bolt-v4x0.test.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@
1919

2020
import neo4j from '../src'
2121
import sharedNeo4j from './internal/shared-neo4j'
22-
import { ServerVersion, VERSION_4_0_0 } from '../src/internal/server-version'
2322

24-
describe('#integration Bolt V4 API', () => {
23+
describe('#integration Bolt V4.0 API', () => {
2524
let driver
2625
let session
27-
let serverVersion
26+
let protocolVersion
2827
let originalTimeout
2928

3029
beforeEach(async () => {
@@ -33,7 +32,7 @@ describe('#integration Bolt V4 API', () => {
3332
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL
3433
jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000
3534

36-
serverVersion = await sharedNeo4j.cleanupAndGetVersion(driver)
35+
protocolVersion = await sharedNeo4j.cleanupAndGetProtocolVersion(driver)
3736
})
3837

3938
afterEach(async () => {
@@ -231,6 +230,6 @@ describe('#integration Bolt V4 API', () => {
231230
}
232231

233232
function databaseSupportsBoltV4 () {
234-
return serverVersion.compareTo(VERSION_4_0_0) >= 0
233+
return protocolVersion >= 4.0
235234
}
236235
})

0 commit comments

Comments
 (0)