16
16
* See the License for the specific language governing permissions and
17
17
* limitations under the License.
18
18
*/
19
-
19
+
20
20
import WebSocketChannel from "./ch-websocket" ;
21
21
import NodeChannel from "./ch-node" ;
22
22
import chunking from "./chunking" ;
@@ -53,7 +53,9 @@ FAILURE = 0x7F, // 0111 1111 // FAILURE <metadata>
53
53
NODE = 0x4E ,
54
54
RELATIONSHIP = 0x52 ,
55
55
UNBOUND_RELATIONSHIP = 0x72 ,
56
- PATH = 0x50 ;
56
+ PATH = 0x50 ,
57
+ //sent before version negotiation
58
+ MAGIC_PREAMBLE = 0x6060B017 ;
57
59
58
60
let URLREGEX = new RegExp ( [
59
61
"[^/]+//" , // scheme
@@ -80,14 +82,14 @@ let NO_OP_OBSERVER = {
80
82
/** Maps from packstream structures to Neo4j domain objects */
81
83
let _mappers = {
82
84
node : ( unpacker , buf ) => {
83
- return new GraphType . Node (
85
+ return new GraphType . Node (
84
86
unpacker . unpack ( buf ) , // Identity
85
87
unpacker . unpack ( buf ) , // Labels
86
88
unpacker . unpack ( buf ) // Properties
87
89
) ;
88
90
} ,
89
91
rel : ( unpacker , buf ) => {
90
- return new GraphType . Relationship (
92
+ return new GraphType . Relationship (
91
93
unpacker . unpack ( buf ) , // Identity
92
94
unpacker . unpack ( buf ) , // Start Node Identity
93
95
unpacker . unpack ( buf ) , // End Node Identity
@@ -103,7 +105,7 @@ let _mappers = {
103
105
) ;
104
106
} ,
105
107
path : ( unpacker , buf ) => {
106
- let nodes = unpacker . unpack ( buf ) ,
108
+ let nodes = unpacker . unpack ( buf ) ,
107
109
rels = unpacker . unpack ( buf ) ,
108
110
sequence = unpacker . unpack ( buf ) ;
109
111
let prevNode = nodes [ 0 ] ,
@@ -115,8 +117,8 @@ let _mappers = {
115
117
if ( relIndex > 0 ) {
116
118
rel = rels [ relIndex - 1 ] ;
117
119
if ( rel instanceof GraphType . UnboundRelationship ) {
118
- // To avoid duplication, relationships in a path do not contain
119
- // information about their start and end nodes, that's instead
120
+ // To avoid duplication, relationships in a path do not contain
121
+ // information about their start and end nodes, that's instead
120
122
// inferred from the path sequence. This is us inferring (and,
121
123
// for performance reasons remembering) the start/end of a rel.
122
124
rels [ relIndex - 1 ] = rel = rel . bind ( prevNode . identity , nextNode . identity ) ;
@@ -142,7 +144,7 @@ let _mappers = {
142
144
* same message structure with very little frills. This means Connectors are
143
145
* naturally tied to a specific version of the protocol, and we expect
144
146
* another layer will be needed to support multiple versions.
145
- *
147
+ *
146
148
* The connector tries to batch outbound messages by requiring its users
147
149
* to call 'sync' when messages need to be sent, and it routes response
148
150
* messages back to the originators of the requests that created those
@@ -152,11 +154,11 @@ let _mappers = {
152
154
class Connection {
153
155
/**
154
156
* @constructor
155
- * @param channel - channel with a 'write' function and a 'onmessage'
157
+ * @param channel - channel with a 'write' function and a 'onmessage'
156
158
* callback property
157
159
*/
158
160
constructor ( channel ) {
159
- /**
161
+ /**
160
162
* An ordered queue of observers, each exchange response (zero or more
161
163
* RECORD messages followed by a SUCCESS message) we recieve will be routed
162
164
* to the next pending observer.
@@ -191,7 +193,7 @@ class Connection {
191
193
}
192
194
193
195
} else {
194
- // TODO: Report error
196
+ // TODO: Report error
195
197
console . log ( "FATAL, unknown protocol version:" , proposed )
196
198
}
197
199
} ;
@@ -200,15 +202,18 @@ class Connection {
200
202
self . _handleMessage ( self . _unpacker . unpack ( buf ) ) ;
201
203
}
202
204
203
- let version_proposal = alloc ( 4 * 4 ) ;
204
- version_proposal . writeInt32 ( 1 ) ;
205
- version_proposal . writeInt32 ( 0 ) ;
206
- version_proposal . writeInt32 ( 0 ) ;
207
- version_proposal . writeInt32 ( 0 ) ;
208
- version_proposal . reset ( ) ;
209
- this . _ch . write ( version_proposal ) ;
205
+ let handshake = alloc ( 5 * 4 ) ;
206
+ //magic preamble
207
+ handshake . writeInt32 ( MAGIC_PREAMBLE ) ;
208
+ //proposed versions
209
+ handshake . writeInt32 ( 1 ) ;
210
+ handshake . writeInt32 ( 0 ) ;
211
+ handshake . writeInt32 ( 0 ) ;
212
+ handshake . writeInt32 ( 0 ) ;
213
+ handshake . reset ( ) ;
214
+ this . _ch . write ( handshake ) ;
210
215
}
211
-
216
+
212
217
_handleMessage ( msg ) {
213
218
switch ( msg . signature ) {
214
219
case RECORD :
@@ -227,7 +232,7 @@ class Connection {
227
232
} finally {
228
233
this . _currentObserver = this . _pendingObservers . shift ( ) ;
229
234
// Things are now broken. Pending observers will get FAILURE messages routed until
230
- // We are done handling this failure.
235
+ // We are done handling this failure.
231
236
if ( ! this . _isHandlingFailure ) {
232
237
this . _isHandlingFailure = true ;
233
238
let self = this ;
0 commit comments