Skip to content

Commit 3995a72

Browse files
committed
Implement CMD_RESET_CONNECTION
1 parent 3c300a8 commit 3995a72

File tree

8 files changed

+70
-2
lines changed

8 files changed

+70
-2
lines changed

lib/commands/command.js

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ class Command extends EventEmitter {
2020
return 'unknown name';
2121
}
2222

23+
/**
24+
*
25+
* @param {*} packet
26+
* @param {*} connection
27+
* @returns
28+
*/
2329
execute(packet, connection) {
2430
if (!this.next) {
2531
this.next = this.start;

lib/commands/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const RegisterSlave = require('./register_slave.js');
1111
const BinlogDump = require('./binlog_dump.js');
1212
const ChangeUser = require('./change_user.js');
1313
const Quit = require('./quit.js');
14+
const ResetConnection = require('./reset_connection.js');
1415

1516
module.exports = {
1617
ClientHandshake,
@@ -23,5 +24,6 @@ module.exports = {
2324
RegisterSlave,
2425
BinlogDump,
2526
ChangeUser,
26-
Quit
27+
Quit,
28+
ResetConnection
2729
};

lib/commands/reset_connection.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
const Command = require('./command');
4+
const Packets = require('../packets/index.js');
5+
6+
class ResetConnection extends Command {
7+
constructor(callback) {
8+
super();
9+
this.onResult = callback;
10+
}
11+
12+
start(packet, connection) {
13+
const req = new Packets.ResetConnection();
14+
connection.writePacket(req.toPacket());
15+
return ResetConnection.prototype.resetConnectionResponse;
16+
}
17+
18+
resetConnectionResponse() {
19+
if (this.onResult) {
20+
process.nextTick(this.onResult.bind(this));
21+
}
22+
return null;
23+
}
24+
}
25+
26+
module.exports = ResetConnection;

lib/connection.js

+4
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,10 @@ class Connection extends EventEmitter {
707707
return this.addCommand(new Commands.Ping(cb));
708708
}
709709

710+
reset(cb) {
711+
return this.addCommand(new Commands.ResetConnection(cb));
712+
}
713+
710714
_registerSlave(opts, cb) {
711715
return this.addCommand(new Commands.RegisterSlave(opts, cb));
712716
}

lib/constants/commands.js

+1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ module.exports = {
3232
STMT_FETCH: 0x1c,
3333
DAEMON: 0x1d, // deprecated
3434
BINLOG_DUMP_GTID: 0x1e,
35+
RESET_CONNECTION: 0x1f, // introduced in 5.7.3
3536
UNKNOWN: 0xff // bad!
3637
};

lib/packets/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const RegisterSlave = require('./register_slave');
2525
const ResultSetHeader = require('./resultset_header');
2626
const SSLRequest = require('./ssl_request');
2727
const TextRow = require('./text_row');
28+
const ResetConnection = require('./reset_connection');
2829

2930
const ctorMap = {
3031
AuthSwitchRequest,
@@ -44,7 +45,8 @@ const ctorMap = {
4445
RegisterSlave,
4546
ResultSetHeader,
4647
SSLRequest,
47-
TextRow
48+
TextRow,
49+
ResetConnection
4850
};
4951
Object.entries(ctorMap).forEach(([name, ctor]) => {
5052
module.exports[name] = ctor;

lib/packets/reset_connection.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
const Packet = require('../packets/packet');
4+
const CommandCodes = require('../constants/commands');
5+
6+
class ResetConnection {
7+
constructor() {
8+
}
9+
10+
toPacket() {
11+
const packet = new Packet(0, Buffer.allocUnsafe(5), 0, 5);
12+
packet.offset = 4;
13+
packet.writeInt8(CommandCodes.RESET_CONNECTION);
14+
return packet;
15+
}
16+
}
17+
18+
module.exports = ResetConnection;

promise.js

+9
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,15 @@ class PromiseConnection extends EventEmitter {
166166
});
167167
}
168168

169+
reset() {
170+
const c = this.connection;
171+
const localErr = new Error();
172+
return new this.Promise((resolve, reject) => {
173+
const done = makeDoneCb(resolve, reject, localErr);
174+
c.reset(done);
175+
});
176+
}
177+
169178
connect() {
170179
const c = this.connection;
171180
const localErr = new Error();

0 commit comments

Comments
 (0)