Skip to content

Commit b2e62e6

Browse files
authored
Merge pull request neo4j#400 from lutovich/1.7-bolt-protocol
Refactoring to better support new protocol versions
2 parents cb9ca48 + ccc86fe commit b2e62e6

23 files changed

+847
-348
lines changed

src/v1/driver.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ class Driver {
132132
* @access private
133133
*/
134134
_createConnection(hostPort, release) {
135-
let conn = connect(hostPort, this._config, this._connectionErrorCode(), this._log);
136-
let streamObserver = new _ConnectionStreamObserver(this, conn);
137-
conn.initialize(this._userAgent, this._token, streamObserver);
135+
const conn = connect(hostPort, this._config, this._connectionErrorCode(), this._log);
136+
const streamObserver = new _ConnectionStreamObserver(this, conn);
137+
conn.protocol().initialize(this._userAgent, this._token, streamObserver);
138138
conn._release = () => release(hostPort, conn);
139139

140140
this._openConnections[conn.id] = conn;

src/v1/internal/bolt-protocol-v1.js

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/**
2+
* Copyright (c) 2002-2018 "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+
import RequestMessage from './request-message';
20+
import * as v1 from './packstream-v1';
21+
22+
export default class BoltProtocol {
23+
24+
/**
25+
* @constructor
26+
* @param {Connection} connection the connection.
27+
* @param {Chunker} chunker the chunker.
28+
* @param {boolean} disableLosslessIntegers if this connection should convert all received integers to native JS numbers.
29+
*/
30+
constructor(connection, chunker, disableLosslessIntegers) {
31+
this._connection = connection;
32+
this._packer = this._createPacker(chunker);
33+
this._unpacker = this._createUnpacker(disableLosslessIntegers);
34+
}
35+
36+
/**
37+
* Get the packer.
38+
* @return {Packer} the protocol's packer.
39+
*/
40+
packer() {
41+
return this._packer;
42+
}
43+
44+
/**
45+
* Get the unpacker.
46+
* @return {Unpacker} the protocol's unpacker.
47+
*/
48+
unpacker() {
49+
return this._unpacker;
50+
}
51+
52+
/**
53+
* Perform initialization and authentication of the underlying connection.
54+
* @param {string} clientName the client name.
55+
* @param {object} authToken the authentication token.
56+
* @param {StreamObserver} observer the response observer.
57+
*/
58+
initialize(clientName, authToken, observer) {
59+
const message = RequestMessage.init(clientName, authToken);
60+
this._connection.write(message, observer, true);
61+
}
62+
63+
/**
64+
* Begin an explicit transaction.
65+
* @param {Bookmark} bookmark the bookmark.
66+
* @param {StreamObserver} observer the response observer.
67+
*/
68+
beginTransaction(bookmark, observer) {
69+
const runMessage = RequestMessage.run('BEGIN', bookmark.asBeginTransactionParameters());
70+
const pullAllMessage = RequestMessage.pullAll();
71+
72+
this._connection.write(runMessage, observer, false);
73+
this._connection.write(pullAllMessage, observer, false);
74+
}
75+
76+
/**
77+
* Commit the explicit transaction.
78+
* @param {StreamObserver} observer the response observer.
79+
*/
80+
commitTransaction(observer) {
81+
this.run('COMMIT', {}, observer);
82+
}
83+
84+
/**
85+
* Rollback the explicit transaction.
86+
* @param {StreamObserver} observer the response observer.
87+
*/
88+
rollbackTransaction(observer) {
89+
this.run('ROLLBACK', {}, observer);
90+
}
91+
92+
/**
93+
* Send a Cypher statement through the underlying connection.
94+
* @param {string} statement the cypher statement.
95+
* @param {object} parameters the statement parameters.
96+
* @param {StreamObserver} observer the response observer.
97+
*/
98+
run(statement, parameters, observer) {
99+
const runMessage = RequestMessage.run(statement, parameters);
100+
const pullAllMessage = RequestMessage.pullAll();
101+
102+
this._connection.write(runMessage, observer, false);
103+
this._connection.write(pullAllMessage, observer, true);
104+
}
105+
106+
/**
107+
* Send a RESET through the underlying connection.
108+
* @param {StreamObserver} observer the response observer.
109+
*/
110+
reset(observer) {
111+
const message = RequestMessage.reset();
112+
this._connection.write(message, observer, true);
113+
}
114+
115+
_createPacker(chunker) {
116+
return new v1.Packer(chunker);
117+
}
118+
119+
_createUnpacker(disableLosslessIntegers) {
120+
return new v1.Unpacker(disableLosslessIntegers);
121+
}
122+
}

src/v1/internal/bolt-protocol-v2.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright (c) 2002-2018 "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+
import BoltProtocolV1 from './bolt-protocol-v1';
20+
import * as v2 from './packstream-v2';
21+
22+
export default class BoltProtocol extends BoltProtocolV1 {
23+
24+
constructor(connection, chunker, disableLosslessIntegers) {
25+
super(connection, chunker, disableLosslessIntegers);
26+
}
27+
28+
_createPacker(chunker) {
29+
return new v2.Packer(chunker);
30+
}
31+
32+
_createUnpacker(disableLosslessIntegers) {
33+
return new v2.Unpacker(disableLosslessIntegers);
34+
}
35+
}

src/v1/internal/buf.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -578,11 +578,10 @@ try {
578578
} catch(e) {}
579579

580580
/**
581-
* Allocate a new buffer using whatever mechanism is most sensible for the
582-
* current platform
581+
* Allocate a new buffer using whatever mechanism is most sensible for the current platform.
583582
* @access private
584583
* @param {Integer} size
585-
* @return new buffer
584+
* @return {BaseBuffer} new buffer
586585
*/
587586
function alloc (size) {
588587
return new _DefaultBuffer(size);

0 commit comments

Comments
 (0)