19
19
20
20
import { alloc } from './buf' ;
21
21
import { newError } from '../error' ;
22
- import * as v1 from './packstream-v1' ;
23
- import * as v2 from './packstream-v2' ;
24
- import BoltProtocol from './bolt-protocol-v1' ;
22
+ import BoltProtocolV1 from './bolt-protocol-v1' ;
23
+ import BoltProtocolV2 from './bolt-protocol-v2' ;
25
24
26
25
const HTTP_MAGIC_PREAMBLE = 1213486160 ; // == 0x48545450 == "HTTP"
27
26
const BOLT_MAGIC_PREAMBLE = 0x6060B017 ;
28
27
29
- const PACKER_CONSTRUCTORS_BY_VERSION = [ null , v1 . Packer , v2 . Packer ] ;
30
- const UNPACKER_CONSTRUCTORS_BY_VERSION = [ null , v1 . Unpacker , v2 . Unpacker ] ;
28
+ const LATEST_PROTOCOL_VERSION = 2 ;
31
29
32
30
export default class ProtocolHandshaker {
33
31
32
+ /**
33
+ * @constructor
34
+ * @param {Connection } connection the connection owning this protocol.
35
+ * @param {NodeChannel|WebSocketChannel } channel the network channel.
36
+ * @param {Chunker } chunker the message chunker.
37
+ * @param {boolean } disableLosslessIntegers flag to use native JS numbers.
38
+ * @param {Logger } log the logger.
39
+ */
34
40
constructor ( connection , channel , chunker , disableLosslessIntegers , log ) {
35
41
this . _connection = connection ;
36
42
this . _channel = channel ;
@@ -44,7 +50,7 @@ export default class ProtocolHandshaker {
44
50
* @return {BoltProtocol } the protocol.
45
51
*/
46
52
createLatestProtocol ( ) {
47
- return this . _createProtocolWithVersion ( PACKER_CONSTRUCTORS_BY_VERSION . length - 1 ) ;
53
+ return this . _createProtocolWithVersion ( LATEST_PROTOCOL_VERSION ) ;
48
54
}
49
55
50
56
/**
@@ -62,54 +68,27 @@ export default class ProtocolHandshaker {
62
68
*/
63
69
readHandshakeResponse ( buffer ) {
64
70
const proposedVersion = buffer . readInt32 ( ) ;
65
-
66
- if ( proposedVersion === 1 || proposedVersion === 2 ) {
67
- return this . _createProtocolWithVersion ( proposedVersion ) ;
68
- } else if ( proposedVersion === HTTP_MAGIC_PREAMBLE ) {
69
- throw newError ( 'Server responded HTTP. Make sure you are not trying to connect to the http endpoint ' +
70
- '(HTTP defaults to port 7474 whereas BOLT defaults to port 7687)' ) ;
71
- } else {
72
- throw newError ( 'Unknown Bolt protocol version: ' + proposedVersion ) ;
71
+ if ( this . _log . isDebugEnabled ( ) ) {
72
+ this . _log . debug ( `${ this } negotiated protocol version ${ proposedVersion } ` ) ;
73
73
}
74
+ return this . _createProtocolWithVersion ( proposedVersion ) ;
74
75
}
75
76
76
77
/**
77
78
* @return {BoltProtocol }
78
79
* @private
79
80
*/
80
81
_createProtocolWithVersion ( version ) {
81
- if ( this . _log . isDebugEnabled ( ) ) {
82
- this . _log . debug ( `${ this } negotiated protocol version ${ version } ` ) ;
83
- }
84
- const packer = this . _createPackerForProtocolVersion ( version ) ;
85
- const unpacker = this . _createUnpackerForProtocolVersion ( version ) ;
86
- return new BoltProtocol ( this . _connection , packer , unpacker ) ;
87
- }
88
-
89
- /**
90
- * @param {number } version
91
- * @return {Packer }
92
- * @private
93
- */
94
- _createPackerForProtocolVersion ( version ) {
95
- const packerConstructor = PACKER_CONSTRUCTORS_BY_VERSION [ version ] ;
96
- if ( ! packerConstructor ) {
97
- throw new Error ( `Packer can't be created for protocol version ${ version } ` ) ;
98
- }
99
- return new packerConstructor ( this . _chunker ) ;
100
- }
101
-
102
- /**
103
- * @param {number } version
104
- * @return {Unpacker }
105
- * @private
106
- */
107
- _createUnpackerForProtocolVersion ( version ) {
108
- const unpackerConstructor = UNPACKER_CONSTRUCTORS_BY_VERSION [ version ] ;
109
- if ( ! unpackerConstructor ) {
110
- throw new Error ( `Unpacker can't be created for protocol version ${ version } ` ) ;
82
+ if ( version === 1 ) {
83
+ return new BoltProtocolV1 ( this . _connection , this . _chunker , this . _disableLosslessIntegers ) ;
84
+ } else if ( version === 2 ) {
85
+ return new BoltProtocolV2 ( this . _connection , this . _chunker , this . _disableLosslessIntegers ) ;
86
+ } else if ( version === HTTP_MAGIC_PREAMBLE ) {
87
+ throw newError ( 'Server responded HTTP. Make sure you are not trying to connect to the http endpoint ' +
88
+ '(HTTP defaults to port 7474 whereas BOLT defaults to port 7687)' ) ;
89
+ } else {
90
+ throw newError ( 'Unknown Bolt protocol version: ' + version ) ;
111
91
}
112
- return new unpackerConstructor ( this . _disableLosslessIntegers ) ;
113
92
}
114
93
}
115
94
0 commit comments