Skip to content

Commit aacbe26

Browse files
committed
extract protocol factory
1 parent d56f7dd commit aacbe26

File tree

3 files changed

+90
-13
lines changed

3 files changed

+90
-13
lines changed

src/internal/bolt/create.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Copyright (c) 2002-2020 "Neo4j,"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
import { newError } from '../../error'
21+
import BoltProtocolV1 from '../bolt-protocol-v1'
22+
import BoltProtocolV2 from '../bolt-protocol-v2'
23+
import BoltProtocolV3 from '../bolt-protocol-v3'
24+
import BoltProtocolV4x0 from '../bolt-protocol-v4x0'
25+
import BoltProtocolV4x1 from '../bolt-protocol-v4x1'
26+
import BoltProtocolV4x2 from '../bolt-protocol-v4x2'
27+
import BoltProtocolV4x3 from '../bolt-protocol-v4x3'
28+
import { Chunker } from '../chunking'
29+
import Connection from '../connection'
30+
31+
/**
32+
* Creates a protocol with a given version
33+
*
34+
* @param {object} config
35+
* @param {number} config.version The version of the protocol
36+
* @param {Connection} config.connection The connection used to talk with the database
37+
* @param {Chunker} config.chunker The chunker
38+
* @param {boolean} config.disableLosslessIntegers Disable the lossless integers
39+
* @param {boolean} config.serversideRouting It's using server side routing
40+
*/
41+
export default function create ({
42+
version,
43+
connection, // should be removed
44+
chunker,
45+
disableLosslessIntegers,
46+
serversideRouting
47+
} = {}) {
48+
switch (version) {
49+
case 1:
50+
return new BoltProtocolV1(connection, chunker, disableLosslessIntegers)
51+
case 2:
52+
return new BoltProtocolV2(connection, chunker, disableLosslessIntegers)
53+
case 3:
54+
return new BoltProtocolV3(connection, chunker, disableLosslessIntegers)
55+
case 4.0:
56+
return new BoltProtocolV4x0(connection, chunker, disableLosslessIntegers)
57+
case 4.1:
58+
return new BoltProtocolV4x1(
59+
connection,
60+
chunker,
61+
disableLosslessIntegers,
62+
serversideRouting
63+
)
64+
case 4.2:
65+
return new BoltProtocolV4x2(
66+
connection,
67+
chunker,
68+
disableLosslessIntegers,
69+
serversideRouting
70+
)
71+
case 4.3:
72+
return new BoltProtocolV4x3(
73+
connection,
74+
chunker,
75+
disableLosslessIntegers,
76+
serversideRouting
77+
)
78+
default:
79+
throw newError('Unknown Bolt protocol version: ' + version)
80+
}
81+
}

src/internal/bolt/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import handshake from './handshake'
2+
import create from './create'
23

34
export default {
4-
handshake
5+
handshake,
6+
create
57
}

src/internal/connection-channel.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,13 @@ export function createChannelConnection (
8181
serversideRouting
8282
)
8383

84-
// Temporary, this object will not be used in the feature
85-
const protocolHandshaker = new ProtocolHandshaker(
84+
connection._protocol = Bolt.create({
85+
version: protocolVersion,
8686
connection,
87-
channel,
88-
connection._chunker,
89-
connection._disableLosslessIntegers,
90-
connection._log,
91-
connection._serversideRouting
92-
)
93-
94-
connection._protocol = protocolHandshaker._createProtocolWithVersion(
95-
protocolVersion
96-
)
87+
chunker: connection._chunker,
88+
disableLosslessIntegers: config.disableLosslessIntegers,
89+
serversideRouting
90+
})
9791

9892
// reset the error handler to just handle errors and forget about the handshake promise
9993
channel.onerror = connection._handleFatalError.bind(connection)

0 commit comments

Comments
 (0)