Skip to content

Commit 23d8fff

Browse files
committed
add new server side commands via handleCommand hook
1 parent e09424f commit 23d8fff

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

lib/connection.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ class Connection extends EventEmitter {
100100
handshakeCommand = new Commands.ClientHandshake(this.config.clientFlags);
101101
handshakeCommand.on('end', () => {
102102
// this happens when handshake finishes early and first packet is error
103-
// and not server hello ( for example, 'Too many connactions' error)
104-
if (!handshakeCommand.handshake) {
103+
// and not server hello ( for example, 'Too many connections' error)
104+
// or server requested auth switch and responded with error to client auth switch data
105+
if (!this.authorized) {
105106
return;
106107
}
107108
this._handshakePacket = handshakeCommand.handshake;
@@ -256,7 +257,7 @@ class Connection extends EventEmitter {
256257
let chunk, offset, header;
257258
if (length < MAX_PACKET_LENGTH) {
258259
packet.writeHeader(this.sequenceId);
259-
if (this.config.debug) {
260+
if (this.config.debug && this._command) {
260261
// eslint-disable-next-line no-console
261262
console.log(
262263
`${this._internalId} ${this.connectionId} <== ${
@@ -425,11 +426,16 @@ class Connection extends EventEmitter {
425426
);
426427
}
427428
}
429+
if (!this._command && this.config.isServer) {
430+
const commandCode = packet.peekByte();
431+
this._command = this.config.serverOptions.handleCommand(commandCode);
432+
}
428433
if (!this._command) {
429434
this.protocolError(
430435
'Unexpected packet while no commands in the queue',
431436
'PROTOCOL_UNEXPECTED_PACKET'
432437
);
438+
console.log()
433439
this.close();
434440
return;
435441
}
@@ -760,7 +766,7 @@ class Connection extends EventEmitter {
760766
this.writePacket(Packets.ResultSetHeader.toPacket(columns.length));
761767
columns.forEach(column => {
762768
this.writePacket(
763-
Packets.ColumnDefinition.toPacket(column, this.serverConfig.encoding)
769+
Packets.ColumnDefinition.toPacket(column, this.config.serverOptions.encoding)
764770
);
765771
});
766772
this.writeEof();
@@ -769,7 +775,7 @@ class Connection extends EventEmitter {
769775
// row is array of columns, not hash
770776
writeTextRow(column) {
771777
this.writePacket(
772-
Packets.TextRow.toPacket(column, this.serverConfig.encoding)
778+
Packets.TextRow.toPacket(column, this.config.serverOptions.encoding)
773779
);
774780
}
775781

@@ -793,16 +799,17 @@ class Connection extends EventEmitter {
793799
if (!args) {
794800
args = { affectedRows: 0 };
795801
}
796-
this.writePacket(Packets.OK.toPacket(args, this.serverConfig.encoding));
802+
this.writePacket(Packets.OK.toPacket(args, this.config.serverOptions.encoding));
797803
}
798804

799805
writeError(args) {
800806
// if we want to send error before initial hello was sent, use default encoding
801-
const encoding = this.serverConfig ? this.serverConfig.encoding : 'cesu8';
807+
const encoding = this.config.serverOptions.encoding;
802808
this.writePacket(Packets.Error.toPacket(args, encoding));
803809
}
804810

805811
serverHandshake(args) {
812+
throw new Error('deprecated - use addCommand directly');
806813
this.serverConfig = args;
807814
this.serverConfig.encoding =
808815
CharsetToEncoding[this.serverConfig.characterSet];

0 commit comments

Comments
 (0)