Skip to content

Commit a8d338e

Browse files
pontusmelkejakewins
authored andcommitted
Added magic handshake to driver
Send `0x6060B017` before negotiating version.
1 parent 7202783 commit a8d338e

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

src/v1/internal/connector.js

+25-20
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* See the License for the specific language governing permissions and
1717
* limitations under the License.
1818
*/
19-
19+
2020
import WebSocketChannel from "./ch-websocket";
2121
import NodeChannel from "./ch-node";
2222
import chunking from "./chunking";
@@ -53,7 +53,9 @@ FAILURE = 0x7F, // 0111 1111 // FAILURE <metadata>
5353
NODE = 0x4E,
5454
RELATIONSHIP = 0x52,
5555
UNBOUND_RELATIONSHIP = 0x72,
56-
PATH = 0x50;
56+
PATH = 0x50,
57+
//sent before version negotiation
58+
MAGIC_PREAMBLE = 0x6060B017;
5759

5860
let URLREGEX = new RegExp([
5961
"[^/]+//", // scheme
@@ -80,14 +82,14 @@ let NO_OP_OBSERVER = {
8082
/** Maps from packstream structures to Neo4j domain objects */
8183
let _mappers = {
8284
node : ( unpacker, buf ) => {
83-
return new GraphType.Node(
85+
return new GraphType.Node(
8486
unpacker.unpack(buf), // Identity
8587
unpacker.unpack(buf), // Labels
8688
unpacker.unpack(buf) // Properties
8789
);
8890
},
8991
rel : ( unpacker, buf ) => {
90-
return new GraphType.Relationship(
92+
return new GraphType.Relationship(
9193
unpacker.unpack(buf), // Identity
9294
unpacker.unpack(buf), // Start Node Identity
9395
unpacker.unpack(buf), // End Node Identity
@@ -103,7 +105,7 @@ let _mappers = {
103105
);
104106
},
105107
path : ( unpacker, buf ) => {
106-
let nodes = unpacker.unpack(buf),
108+
let nodes = unpacker.unpack(buf),
107109
rels = unpacker.unpack(buf),
108110
sequence = unpacker.unpack(buf);
109111
let prevNode = nodes[0],
@@ -115,8 +117,8 @@ let _mappers = {
115117
if (relIndex > 0) {
116118
rel = rels[relIndex - 1];
117119
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
120122
// inferred from the path sequence. This is us inferring (and,
121123
// for performance reasons remembering) the start/end of a rel.
122124
rels[relIndex - 1] = rel = rel.bind(prevNode.identity, nextNode.identity);
@@ -142,7 +144,7 @@ let _mappers = {
142144
* same message structure with very little frills. This means Connectors are
143145
* naturally tied to a specific version of the protocol, and we expect
144146
* another layer will be needed to support multiple versions.
145-
*
147+
*
146148
* The connector tries to batch outbound messages by requiring its users
147149
* to call 'sync' when messages need to be sent, and it routes response
148150
* messages back to the originators of the requests that created those
@@ -152,11 +154,11 @@ let _mappers = {
152154
class Connection {
153155
/**
154156
* @constructor
155-
* @param channel - channel with a 'write' function and a 'onmessage'
157+
* @param channel - channel with a 'write' function and a 'onmessage'
156158
* callback property
157159
*/
158160
constructor (channel) {
159-
/**
161+
/**
160162
* An ordered queue of observers, each exchange response (zero or more
161163
* RECORD messages followed by a SUCCESS message) we recieve will be routed
162164
* to the next pending observer.
@@ -191,7 +193,7 @@ class Connection {
191193
}
192194

193195
} else {
194-
// TODO: Report error
196+
// TODO: Report error
195197
console.log("FATAL, unknown protocol version:", proposed)
196198
}
197199
};
@@ -200,15 +202,18 @@ class Connection {
200202
self._handleMessage( self._unpacker.unpack( buf ) );
201203
}
202204

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 );
210215
}
211-
216+
212217
_handleMessage( msg ) {
213218
switch( msg.signature ) {
214219
case RECORD:
@@ -227,7 +232,7 @@ class Connection {
227232
} finally {
228233
this._currentObserver = this._pendingObservers.shift();
229234
// 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.
231236
if( !this._isHandlingFailure ) {
232237
this._isHandlingFailure = true;
233238
let self = this;

test/internal/connector.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe('connector', function() {
4545
conn.run( "RETURN 1.0", {} );
4646
conn.pullAll( {
4747
onNext: function( record ) {
48-
records.push( record );
48+
records.push( record );
4949
},
5050
onCompleted: function( tail ) {
5151
expect( records[0][0] ).toBe( 1 );

0 commit comments

Comments
 (0)