Skip to content

Commit 430dab0

Browse files
committed
Fix "connection.threadId" missing on handshake failure
1 parent 856767c commit 430dab0

File tree

7 files changed

+37
-6
lines changed

7 files changed

+37
-6
lines changed

Changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ you spot any mistakes.
66

77
## HEAD
88

9+
* Fix `connection.threadId` missing on handshake failure
910
* Small performance improvement starting command sequence
1011

1112
## v2.16.0 (2018-07-17)

lib/Connection.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ Connection.prototype.connect = function connect(options, callback) {
100100
this._socket.on('error', this._handleNetworkError.bind(this));
101101
this._socket.on('connect', this._handleProtocolConnect.bind(this));
102102
this._protocol.on('handshake', this._handleProtocolHandshake.bind(this));
103+
this._protocol.on('initialize', this._handleProtocolInitialize.bind(this));
103104
this._protocol.on('unhandledError', this._handleProtocolError.bind(this));
104105
this._protocol.on('drain', this._handleProtocolDrain.bind(this));
105106
this._protocol.on('end', this._handleProtocolEnd.bind(this));
@@ -434,8 +435,11 @@ Connection.prototype._handleProtocolConnect = function() {
434435
this.emit('connect');
435436
};
436437

437-
Connection.prototype._handleProtocolHandshake = function _handleProtocolHandshake(packet) {
438-
this.state = 'authenticated';
438+
Connection.prototype._handleProtocolHandshake = function _handleProtocolHandshake() {
439+
this.state = 'authenticated';
440+
};
441+
442+
Connection.prototype._handleProtocolInitialize = function _handleProtocolInitialize(packet) {
439443
this.threadId = packet.threadId;
440444
};
441445

lib/protocol/Protocol.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ Protocol.prototype._parsePacket = function() {
265265

266266
if (Packet === Packets.HandshakeInitializationPacket) {
267267
this._handshakeInitializationPacket = packet;
268+
this.emit('initialize', packet);
268269
}
269270

270271
sequence._timer.active();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var assert = require('assert');
2+
var common = require('../../common');
3+
var connection = common.createConnection({port: common.fakeServerPort});
4+
5+
var server = common.createFakeServer();
6+
7+
server.listen(common.fakeServerPort, function (err) {
8+
assert.ifError(err);
9+
assert.strictEqual(connection.threadId, null);
10+
11+
connection.connect(function (err) {
12+
assert.ok(err);
13+
assert.strictEqual(err.code, 'ER_ACCESS_DENIED_ERROR');
14+
assert.strictEqual(connection.threadId, 42);
15+
16+
server.destroy();
17+
});
18+
});
19+
20+
server.on('connection', function (incomingConnection) {
21+
incomingConnection.handshake({threadId: 42});
22+
incomingConnection.on('clientAuthentication', function () {
23+
this.deny();
24+
});
25+
});

test/unit/connection/test-debug-parser-error.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ server.listen(common.fakeServerPort, function (err) {
2121
assert.equal(messages.length, 6);
2222
assert.deepEqual(messages, [
2323
'<-- HandshakeInitializationPacket',
24-
'--> ClientAuthenticationPacket',
24+
'--> (1) ClientAuthenticationPacket',
2525
'<-- (1) OkPacket',
2626
'--> (1) ComQueryPacket',
2727
'<-- (1) ResultSetHeaderPacket',

test/unit/connection/test-debug.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ server.listen(common.fakeServerPort, function (err) {
2121
assert.equal(messages.length, 5);
2222
assert.deepEqual(messages, [
2323
'<-- HandshakeInitializationPacket',
24-
'--> ClientAuthenticationPacket',
24+
'--> (1) ClientAuthenticationPacket',
2525
'<-- (1) OkPacket',
2626
'--> (1) ComPingPacket',
2727
'<-- (1) OkPacket'

test/unit/pool/test-debug.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ server.listen(common.fakeServerPort, function (err) {
3131
assert.equal(messages.length, 20);
3232
assert.deepEqual(messages, [
3333
'<-- HandshakeInitializationPacket',
34-
'--> ClientAuthenticationPacket',
34+
'--> (1) ClientAuthenticationPacket',
3535
'<-- (1) OkPacket',
3636
'--> (1) ComQueryPacket',
3737
'<-- (1) ResultSetHeaderPacket',
@@ -40,7 +40,7 @@ server.listen(common.fakeServerPort, function (err) {
4040
'<-- (1) RowDataPacket',
4141
'<-- (1) EofPacket',
4242
'<-- HandshakeInitializationPacket',
43-
'--> ClientAuthenticationPacket',
43+
'--> (2) ClientAuthenticationPacket',
4444
'<-- (2) OkPacket',
4545
'--> (2) ComQueryPacket',
4646
'<-- (2) ResultSetHeaderPacket',

0 commit comments

Comments
 (0)