Skip to content

Commit 5cfcbb0

Browse files
authored
BREAKING: Switch Bolt protocol version to dedicated type and string for user API(#1321)
1 parent 4c30bfb commit 5cfcbb0

File tree

62 files changed

+444
-295
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+444
-295
lines changed

packages/bolt-connection/src/bolt/bolt-protocol-v1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export default class BoltProtocol {
100100
}
101101

102102
/**
103-
* Returns the numerical version identifier for this protocol
103+
* Returns the stringified version identifier for this protocol
104104
*/
105105
get version () {
106106
return BOLT_PROTOCOL_V1

packages/bolt-connection/src/bolt/create.js

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { newError } from 'neo4j-driver-core'
18+
import { newError, ProtocolVersion } from 'neo4j-driver-core'
1919
import BoltProtocolV1 from './bolt-protocol-v1'
2020
import BoltProtocolV2 from './bolt-protocol-v2'
2121
import BoltProtocolV3 from './bolt-protocol-v3'
@@ -112,8 +112,11 @@ function createProtocol (
112112
onProtocolError,
113113
log
114114
) {
115-
switch (version) {
116-
case 1:
115+
if (!(version instanceof ProtocolVersion) || version === undefined || version === null) {
116+
throw newError('Unknown Bolt protocol version: ' + version)
117+
}
118+
switch (true) {
119+
case version.isEqualTo({ major: 1, minor: 0 }):
117120
return new BoltProtocolV1(
118121
server,
119122
chunker,
@@ -122,7 +125,7 @@ function createProtocol (
122125
log,
123126
onProtocolError
124127
)
125-
case 2:
128+
case version.isEqualTo({ major: 2, minor: 0 }):
126129
return new BoltProtocolV2(
127130
server,
128131
chunker,
@@ -131,7 +134,7 @@ function createProtocol (
131134
log,
132135
onProtocolError
133136
)
134-
case 3:
137+
case version.isEqualTo({ major: 3, minor: 0 }):
135138
return new BoltProtocolV3(
136139
server,
137140
chunker,
@@ -140,7 +143,7 @@ function createProtocol (
140143
log,
141144
onProtocolError
142145
)
143-
case 4.0:
146+
case version.isEqualTo({ major: 4, minor: 0 }):
144147
return new BoltProtocolV4x0(
145148
server,
146149
chunker,
@@ -149,7 +152,7 @@ function createProtocol (
149152
log,
150153
onProtocolError
151154
)
152-
case 4.1:
155+
case version.isEqualTo({ major: 4, minor: 1 }):
153156
return new BoltProtocolV4x1(
154157
server,
155158
chunker,
@@ -159,7 +162,7 @@ function createProtocol (
159162
onProtocolError,
160163
serversideRouting
161164
)
162-
case 4.2:
165+
case version.isEqualTo({ major: 4, minor: 2 }):
163166
return new BoltProtocolV4x2(
164167
server,
165168
chunker,
@@ -169,7 +172,7 @@ function createProtocol (
169172
onProtocolError,
170173
serversideRouting
171174
)
172-
case 4.3:
175+
case version.isEqualTo({ major: 4, minor: 3 }):
173176
return new BoltProtocolV4x3(
174177
server,
175178
chunker,
@@ -179,7 +182,7 @@ function createProtocol (
179182
onProtocolError,
180183
serversideRouting
181184
)
182-
case 4.4:
185+
case version.isEqualTo({ major: 4, minor: 4 }):
183186
return new BoltProtocolV4x4(
184187
server,
185188
chunker,
@@ -189,7 +192,7 @@ function createProtocol (
189192
onProtocolError,
190193
serversideRouting
191194
)
192-
case 5.0:
195+
case version.isEqualTo({ major: 5, minor: 0 }):
193196
return new BoltProtocolV5x0(
194197
server,
195198
chunker,
@@ -199,7 +202,7 @@ function createProtocol (
199202
onProtocolError,
200203
serversideRouting
201204
)
202-
case 5.1:
205+
case version.isEqualTo({ major: 5, minor: 1 }):
203206
return new BoltProtocolV5x1(
204207
server,
205208
chunker,
@@ -209,7 +212,7 @@ function createProtocol (
209212
onProtocolError,
210213
serversideRouting
211214
)
212-
case 5.2:
215+
case version.isEqualTo({ major: 5, minor: 2 }):
213216
return new BoltProtocolV5x2(
214217
server,
215218
chunker,
@@ -219,55 +222,55 @@ function createProtocol (
219222
onProtocolError,
220223
serversideRouting
221224
)
222-
case 5.3:
225+
case version.isEqualTo({ major: 5, minor: 3 }):
223226
return new BoltProtocolV5x3(server,
224227
chunker,
225228
packingConfig,
226229
createResponseHandler,
227230
log,
228231
onProtocolError,
229232
serversideRouting)
230-
case 5.4:
233+
case version.isEqualTo({ major: 5, minor: 4 }):
231234
return new BoltProtocolV5x4(server,
232235
chunker,
233236
packingConfig,
234237
createResponseHandler,
235238
log,
236239
onProtocolError,
237240
serversideRouting)
238-
case 5.5:
241+
case version.isEqualTo({ major: 5, minor: 5 }):
239242
return new BoltProtocolV5x5(server,
240243
chunker,
241244
packingConfig,
242245
createResponseHandler,
243246
log,
244247
onProtocolError,
245248
serversideRouting)
246-
case 5.6:
249+
case version.isEqualTo({ major: 5, minor: 6 }):
247250
return new BoltProtocolV5x6(server,
248251
chunker,
249252
packingConfig,
250253
createResponseHandler,
251254
log,
252255
onProtocolError,
253256
serversideRouting)
254-
case 5.7:
257+
case version.isEqualTo({ major: 5, minor: 7 }):
255258
return new BoltProtocolV5x7(server,
256259
chunker,
257260
packingConfig,
258261
createResponseHandler,
259262
log,
260263
onProtocolError,
261264
serversideRouting)
262-
case 5.8:
265+
case version.isEqualTo({ major: 5, minor: 8 }):
263266
return new BoltProtocolV5x8(server,
264267
chunker,
265268
packingConfig,
266269
createResponseHandler,
267270
log,
268271
onProtocolError,
269272
serversideRouting)
270-
case 6.0:
273+
case version.isEqualTo({ major: 6, minor: 0 }):
271274
return new BoltProtocolV6x0(server,
272275
chunker,
273276
packingConfig,

packages/bolt-connection/src/bolt/handshake.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
import { alloc } from '../channel'
19-
import { newError } from 'neo4j-driver-core'
19+
import { newError, ProtocolVersion } from 'neo4j-driver-core'
2020

2121
const BOLT_MAGIC_PREAMBLE = 0x6060b017
2222
const AVAILABLE_BOLT_PROTOCOLS = ['6.0', '5.8', '5.7', '5.6', '5.4', '5.3', '5.2', '5.1', '5.0', '4.4', '4.3', '4.2', '3.0'] // bolt protocols the client will accept, ordered by preference
@@ -69,7 +69,7 @@ function parseNegotiatedResponse (buffer, log) {
6969
'(HTTP defaults to port 7474 whereas BOLT defaults to port 7687)'
7070
)
7171
}
72-
return Number(h[3] + '.' + h[2])
72+
return new ProtocolVersion(h[3], h[2])
7373
}
7474

7575
function handshakeNegotiationV2 (channel, buffer, log) {
@@ -112,7 +112,7 @@ function handshakeNegotiationV2 (channel, buffer, log) {
112112
selectionBuffer.writeVarInt(capabilites)
113113
channel.write(selectionBuffer)
114114
resolve({
115-
protocolVersion: Number(major + '.' + minor),
115+
protocolVersion: new ProtocolVersion(major, minor),
116116
capabilites,
117117
consumeRemainingBuffer: consumer => {
118118
if (buffer.hasRemaining()) {
@@ -146,7 +146,7 @@ function newHandshakeBuffer () {
146146
*/
147147
/**
148148
* @typedef HandshakeResult
149-
* @property {number} protocolVersion The protocol version negotiated in the handshake
149+
* @property {ProtocolVersion} protocolVersion The protocol version negotiated in the handshake
150150
* @property {number} capabilites A bitmask representing the capabilities negotiated in the handshake
151151
* @property {function(BufferConsumerCallback)} consumeRemainingBuffer A function to consume the remaining buffer if it exists
152152
*/
@@ -160,7 +160,7 @@ function newHandshakeBuffer () {
160160
*/
161161
export default function handshake (channel, log) {
162162
return initialHandshake(channel, log).then((result) => {
163-
if (result.protocolVersion === 255.1) {
163+
if (result.protocolVersion.isEqualTo(new ProtocolVersion(255, 1))) {
164164
return handshakeNegotiationV2(channel, result.buffer, log)
165165
} else {
166166
return result

packages/bolt-connection/src/connection-provider/connection-provider-direct.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export default class DirectConnectionProvider extends PooledConnectionProvider {
9191

9292
async supportsMultiDb () {
9393
return await this._hasProtocolVersion(
94-
version => version >= BOLT_PROTOCOL_V4_0
94+
version => version.isGreaterOrEqualTo(BOLT_PROTOCOL_V4_0)
9595
)
9696
}
9797

@@ -104,19 +104,19 @@ export default class DirectConnectionProvider extends PooledConnectionProvider {
104104

105105
async supportsTransactionConfig () {
106106
return await this._hasProtocolVersion(
107-
version => version >= BOLT_PROTOCOL_V3
107+
version => version.isGreaterOrEqualTo(BOLT_PROTOCOL_V3)
108108
)
109109
}
110110

111111
async supportsUserImpersonation () {
112112
return await this._hasProtocolVersion(
113-
version => version >= BOLT_PROTOCOL_V4_4
113+
version => version.isGreaterOrEqualTo(BOLT_PROTOCOL_V4_4)
114114
)
115115
}
116116

117117
async supportsSessionAuth () {
118118
return await this._hasProtocolVersion(
119-
version => version >= BOLT_PROTOCOL_V5_1
119+
version => version.isGreaterOrEqualTo(BOLT_PROTOCOL_V5_1)
120120
)
121121
}
122122

packages/bolt-connection/src/connection-provider/connection-provider-routing.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,25 +260,25 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
260260

261261
async supportsMultiDb () {
262262
return await this._hasProtocolVersion(
263-
version => version >= BOLT_PROTOCOL_V4_0
263+
version => version.isGreaterOrEqualTo(BOLT_PROTOCOL_V4_0)
264264
)
265265
}
266266

267267
async supportsTransactionConfig () {
268268
return await this._hasProtocolVersion(
269-
version => version >= BOLT_PROTOCOL_V3
269+
version => version.isGreaterOrEqualTo(BOLT_PROTOCOL_V3)
270270
)
271271
}
272272

273273
async supportsUserImpersonation () {
274274
return await this._hasProtocolVersion(
275-
version => version >= BOLT_PROTOCOL_V4_4
275+
version => version.isGreaterOrEqualTo(BOLT_PROTOCOL_V4_4)
276276
)
277277
}
278278

279279
async supportsSessionAuth () {
280280
return await this._hasProtocolVersion(
281-
version => version >= BOLT_PROTOCOL_V5_1
281+
version => version.isGreaterOrEqualTo(BOLT_PROTOCOL_V5_1)
282282
)
283283
}
284284

@@ -611,7 +611,7 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
611611
const connectionProvider = new SingleConnectionProvider(delegateConnection)
612612

613613
const protocolVersion = connection.protocol().version
614-
if (protocolVersion < 4.0) {
614+
if (protocolVersion.isLessThan({ major: 4, minor: 0 })) {
615615
return [new Session({
616616
mode: WRITE,
617617
bookmarks: Bookmarks.empty(),

packages/bolt-connection/test/bolt/bolt-protocol-v1.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ import {
3232
UnboundRelationship,
3333
Node,
3434
newError,
35-
Vector
35+
Vector,
36+
ProtocolVersion
3637
} from 'neo4j-driver-core'
3738
import utils from '../test-utils'
3839
import { LoginObserver } from '../../src/bolt/stream-observers'
@@ -224,7 +225,7 @@ describe('#unit BoltProtocolV1', () => {
224225
it('should return correct bolt version number', () => {
225226
const protocol = new BoltProtocolV1(null, null, false)
226227

227-
expect(protocol.version).toBe(1)
228+
expect(protocol.version).toEqual(new ProtocolVersion(1, 0))
228229
})
229230

230231
describe('Bolt V3', () => {

packages/bolt-connection/test/bolt/bolt-protocol-v2.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ import {
3131
Relationship,
3232
Time,
3333
UnboundRelationship,
34-
Node
34+
Node,
35+
ProtocolVersion
3536
} from 'neo4j-driver-core'
3637

3738
import { alloc } from '../../src/channel'
@@ -69,7 +70,7 @@ describe('#unit BoltProtocolV2', () => {
6970
it('should return correct bolt version number', () => {
7071
const protocol = new BoltProtocolV2(null, null, false)
7172

72-
expect(protocol.version).toBe(2)
73+
expect(protocol.version).toEqual(new ProtocolVersion(2, 0))
7374
})
7475

7576
describe('unpacker configuration', () => {

packages/bolt-connection/test/bolt/bolt-protocol-v3.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ import {
3636
Time,
3737
UnboundRelationship,
3838
Node,
39-
internal
39+
internal,
40+
ProtocolVersion
4041
} from 'neo4j-driver-core'
4142

4243
import { alloc } from '../../src/channel'
@@ -202,7 +203,7 @@ describe('#unit BoltProtocolV3', () => {
202203
it('should return correct bolt version number', () => {
203204
const protocol = new BoltProtocolV3(null, null, false)
204205

205-
expect(protocol.version).toBe(3)
206+
expect(protocol.version).toEqual(new ProtocolVersion(3, 0))
206207
})
207208

208209
it('should request the routing table from the correct procedure', () => {

packages/bolt-connection/test/bolt/bolt-protocol-v4x0.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ import {
3636
Time,
3737
UnboundRelationship,
3838
Node,
39-
internal
39+
internal,
40+
ProtocolVersion
4041
} from 'neo4j-driver-core'
4142

4243
import { alloc } from '../../src/channel'
@@ -149,7 +150,7 @@ describe('#unit BoltProtocolV4x0', () => {
149150
it('should return correct bolt version number', () => {
150151
const protocol = new BoltProtocolV4x0(null, null, false)
151152

152-
expect(protocol.version).toBe(4)
153+
expect(protocol.version).toEqual(new ProtocolVersion(4, 0))
153154
})
154155

155156
it('should request the routing table from the correct procedure', () => {

packages/bolt-connection/test/bolt/bolt-protocol-v4x3.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ import {
3232
Time,
3333
UnboundRelationship,
3434
Node,
35-
internal
35+
internal,
36+
ProtocolVersion
3637
} from 'neo4j-driver-core'
3738

3839
import { alloc } from '../../src/channel'
@@ -169,7 +170,7 @@ describe('#unit BoltProtocolV4x3', () => {
169170
it('should return correct bolt version number', () => {
170171
const protocol = new BoltProtocolV4x3(null, null, false)
171172

172-
expect(protocol.version).toBe(4.3)
173+
expect(protocol.version).toEqual(new ProtocolVersion(4, 3))
173174
})
174175

175176
it('should update metadata', () => {

0 commit comments

Comments
 (0)