Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makes mysqljs easier to minify and support minifier mangle - fix: Error: Received packet in the wrong sequence after minifying #2375

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/protocol/Protocol.js
Expand Up @@ -157,7 +157,8 @@ Protocol.prototype._enqueue = function(sequence) {
self._emitPacket(packet);
})
.on('timeout', function() {
var err = new Error(sequence.constructor.name + ' inactivity timeout');
var sequenceId = sequence._id || sequence.constructor.name;
var err = new Error(sequenceId + ' inactivity timeout');

err.code = 'PROTOCOL_SEQUENCE_TIMEOUT';
err.fatal = true;
Expand Down Expand Up @@ -206,7 +207,8 @@ Protocol.prototype._enqueue = function(sequence) {

Protocol.prototype._validateEnqueue = function _validateEnqueue(sequence) {
var err;
var prefix = 'Cannot enqueue ' + sequence.constructor.name;
var sequenceId = sequence._id || sequence.constructor.name;
var prefix = 'Cannot enqueue ' + sequenceId;

if (this._fatalError) {
err = new Error(prefix + ' after fatal error.');
Expand Down Expand Up @@ -253,7 +255,7 @@ Protocol.prototype._parsePacket = function() {

var Packet = this._determinePacket(sequence);
var packet = new Packet({protocol41: this._config.protocol41});
var packetName = Packet.name;
var packetName = packet._id || Packet.name;

// Special case: Faster dispatch, and parsing done inside sequence
if (Packet === Packets.RowDataPacket) {
Expand Down Expand Up @@ -447,7 +449,7 @@ Protocol.prototype._debugPacket = function(incoming, packet) {
var direction = incoming
? '<--'
: '-->';
var packetName = packet.constructor.name;
var packetName = packet._id || packet.constructor.name;
var threadId = connection && connection.threadId !== null
? ' (' + connection.threadId + ')'
: '';
Expand Down
2 changes: 2 additions & 0 deletions lib/protocol/packets/AuthSwitchRequestPacket.js
Expand Up @@ -7,6 +7,8 @@ function AuthSwitchRequestPacket(options) {
this.authMethodData = options.authMethodData;
}

AuthSwitchRequestPacket.prototype._id = 'AuthSwitchRequestPacket';

AuthSwitchRequestPacket.prototype.parse = function parse(parser) {
this.status = parser.parseUnsignedNumber(1);
this.authMethodName = parser.parseNullTerminatedString();
Expand Down
2 changes: 2 additions & 0 deletions lib/protocol/packets/AuthSwitchResponsePacket.js
Expand Up @@ -5,6 +5,8 @@ function AuthSwitchResponsePacket(options) {
this.data = options.data;
}

AuthSwitchResponsePacket.prototype._id = 'AuthSwitchResponsePacket';

AuthSwitchResponsePacket.prototype.parse = function parse(parser) {
this.data = parser.parsePacketTerminatedBuffer();
};
Expand Down
2 changes: 2 additions & 0 deletions lib/protocol/packets/ClientAuthenticationPacket.js
Expand Up @@ -14,6 +14,8 @@ function ClientAuthenticationPacket(options) {
this.protocol41 = options.protocol41;
}

ClientAuthenticationPacket.prototype._id = 'ClientAuthenticationPacket';

ClientAuthenticationPacket.prototype.parse = function(parser) {
if (this.protocol41) {
this.clientFlags = parser.parseUnsignedNumber(4);
Expand Down
2 changes: 2 additions & 0 deletions lib/protocol/packets/ComChangeUserPacket.js
Expand Up @@ -9,6 +9,8 @@ function ComChangeUserPacket(options) {
this.charsetNumber = options.charsetNumber;
}

ComChangeUserPacket.prototype._id = 'ComChangeUserPacket';

ComChangeUserPacket.prototype.parse = function(parser) {
this.command = parser.parseUnsignedNumber(1);
this.user = parser.parseNullTerminatedString();
Expand Down
2 changes: 2 additions & 0 deletions lib/protocol/packets/ComPingPacket.js
Expand Up @@ -3,6 +3,8 @@ function ComPingPacket() {
this.command = 0x0e;
}

ComPingPacket.prototype._id = 'ComPingPacket';

ComPingPacket.prototype.write = function(writer) {
writer.writeUnsignedNumber(1, this.command);
};
Expand Down
2 changes: 2 additions & 0 deletions lib/protocol/packets/ComQueryPacket.js
Expand Up @@ -4,6 +4,8 @@ function ComQueryPacket(sql) {
this.sql = sql;
}

ComQueryPacket.prototype._id = 'ComQueryPacket';

ComQueryPacket.prototype.write = function(writer) {
writer.writeUnsignedNumber(1, this.command);
writer.writeString(this.sql);
Expand Down
2 changes: 2 additions & 0 deletions lib/protocol/packets/ComQuitPacket.js
Expand Up @@ -3,6 +3,8 @@ function ComQuitPacket() {
this.command = 0x01;
}

ComQuitPacket.prototype._id = 'ComQuitPacket';

ComQuitPacket.prototype.parse = function parse(parser) {
this.command = parser.parseUnsignedNumber(1);
};
Expand Down
2 changes: 2 additions & 0 deletions lib/protocol/packets/ComStatisticsPacket.js
Expand Up @@ -3,6 +3,8 @@ function ComStatisticsPacket() {
this.command = 0x09;
}

ComStatisticsPacket.prototype._id = 'ComStatisticsPacket';

ComStatisticsPacket.prototype.write = function(writer) {
writer.writeUnsignedNumber(1, this.command);
};
Expand Down
2 changes: 2 additions & 0 deletions lib/protocol/packets/EmptyPacket.js
Expand Up @@ -2,6 +2,8 @@ module.exports = EmptyPacket;
function EmptyPacket() {
}

EmptyPacket.prototype._id = 'EmptyPacket';

EmptyPacket.prototype.parse = function parse() {
};

Expand Down
2 changes: 2 additions & 0 deletions lib/protocol/packets/EofPacket.js
Expand Up @@ -8,6 +8,8 @@ function EofPacket(options) {
this.protocol41 = options.protocol41;
}

EofPacket.prototype._id = 'EofPacket';

EofPacket.prototype.parse = function(parser) {
this.fieldCount = parser.parseUnsignedNumber(1);
if (this.protocol41) {
Expand Down
2 changes: 2 additions & 0 deletions lib/protocol/packets/ErrorPacket.js
Expand Up @@ -9,6 +9,8 @@ function ErrorPacket(options) {
this.message = options.message;
}

ErrorPacket.prototype._id = 'ErrorPacket';

ErrorPacket.prototype.parse = function(parser) {
this.fieldCount = parser.parseUnsignedNumber(1);
this.errno = parser.parseUnsignedNumber(2);
Expand Down
2 changes: 2 additions & 0 deletions lib/protocol/packets/Field.js
Expand Up @@ -13,6 +13,8 @@ function Field(options) {
this.length = options.packet.length;
}

Field.prototype._id = 'Field';

Field.prototype.string = function () {
return this.parser.parseLengthCodedString();
};
Expand Down
2 changes: 2 additions & 0 deletions lib/protocol/packets/FieldPacket.js
Expand Up @@ -18,6 +18,8 @@ function FieldPacket(options) {
this.protocol41 = options.protocol41;
}

FieldPacket.prototype._id = 'FieldPacket';

FieldPacket.prototype.parse = function(parser) {
if (this.protocol41) {
this.catalog = parser.parseLengthCodedString();
Expand Down
2 changes: 2 additions & 0 deletions lib/protocol/packets/HandshakeInitializationPacket.js
Expand Up @@ -27,6 +27,8 @@ function HandshakeInitializationPacket(options) {
}
}

HandshakeInitializationPacket.prototype._id = 'HandshakeInitializationPacket';

HandshakeInitializationPacket.prototype.parse = function(parser) {
this.protocolVersion = parser.parseUnsignedNumber(1);
this.serverVersion = parser.parseNullTerminatedString();
Expand Down
2 changes: 2 additions & 0 deletions lib/protocol/packets/LocalDataFilePacket.js
Expand Up @@ -10,6 +10,8 @@ function LocalDataFilePacket(data) {
this.data = data;
}

LocalDataFilePacket.prototype._id = 'LocalDataFilePacket';

LocalDataFilePacket.prototype.write = function(writer) {
writer.writeBuffer(this.data);
};
2 changes: 2 additions & 0 deletions lib/protocol/packets/LocalInfileRequestPacket.js
Expand Up @@ -5,6 +5,8 @@ function LocalInfileRequestPacket(options) {
this.filename = options.filename;
}

LocalInfileRequestPacket.prototype._id = 'LocalInfileRequestPacket';

LocalInfileRequestPacket.prototype.parse = function parse(parser) {
if (parser.parseLengthCodedNumber() !== null) {
var err = new TypeError('Received invalid field length');
Expand Down
2 changes: 2 additions & 0 deletions lib/protocol/packets/OkPacket.js
Expand Up @@ -15,6 +15,8 @@ function OkPacket(options) {
this.protocol41 = options.protocol41;
}

OkPacket.prototype._id = 'OkPacket';

OkPacket.prototype.parse = function(parser) {
this.fieldCount = parser.parseUnsignedNumber(1);
this.affectedRows = parser.parseLengthCodedNumber();
Expand Down
2 changes: 2 additions & 0 deletions lib/protocol/packets/OldPasswordPacket.js
Expand Up @@ -5,6 +5,8 @@ function OldPasswordPacket(options) {
this.scrambleBuff = options.scrambleBuff;
}

OldPasswordPacket.prototype._id = 'OldPasswordPacket';

OldPasswordPacket.prototype.parse = function(parser) {
this.scrambleBuff = parser.parsePacketTerminatedBuffer();
};
Expand Down
2 changes: 2 additions & 0 deletions lib/protocol/packets/ResultSetHeaderPacket.js
Expand Up @@ -5,6 +5,8 @@ function ResultSetHeaderPacket(options) {
this.fieldCount = options.fieldCount;
}

ResultSetHeaderPacket.prototype._id = 'ResultSetHeaderPacket';

ResultSetHeaderPacket.prototype.parse = function(parser) {
this.fieldCount = parser.parseLengthCodedNumber();
};
Expand Down
6 changes: 6 additions & 0 deletions lib/protocol/packets/RowDataPacket.js
Expand Up @@ -19,6 +19,12 @@ Object.defineProperty(RowDataPacket.prototype, '_typeCast', {
value : typeCast
});

Object.defineProperty(RowDataPacket.prototype, '_id', {
configurable : true,
enumerable : false,
value : 'RowDataPacket'
});

function parse(parser, fieldPackets, typeCast, nestTables, connection) {
var self = this;
var next = function () {
Expand Down
2 changes: 2 additions & 0 deletions lib/protocol/packets/SSLRequestPacket.js
Expand Up @@ -12,6 +12,8 @@ function SSLRequestPacket(options) {
this.charsetNumber = options.charsetNumber;
}

SSLRequestPacket.prototype._id = 'SSLRequestPacket';

SSLRequestPacket.prototype.parse = function(parser) {
// TODO: check SSLRequest packet v41 vs pre v41
this.clientFlags = parser.parseUnsignedNumber(4);
Expand Down
2 changes: 2 additions & 0 deletions lib/protocol/packets/StatisticsPacket.js
Expand Up @@ -3,6 +3,8 @@ function StatisticsPacket() {
this.message = undefined;
}

StatisticsPacket.prototype._id = 'StatisticsPacket';

StatisticsPacket.prototype.parse = function(parser) {
this.message = parser.parsePacketTerminatedString();

Expand Down
2 changes: 2 additions & 0 deletions lib/protocol/packets/UseOldPasswordPacket.js
Expand Up @@ -5,6 +5,8 @@ function UseOldPasswordPacket(options) {
this.firstByte = options.firstByte || 0xfe;
}

UseOldPasswordPacket.prototype._id = 'UseOldPasswordPacket';

UseOldPasswordPacket.prototype.parse = function(parser) {
this.firstByte = parser.parseUnsignedNumber(1);
};
Expand Down
2 changes: 2 additions & 0 deletions lib/protocol/sequences/ChangeUser.js
Expand Up @@ -23,6 +23,8 @@ ChangeUser.prototype.determinePacket = function determinePacket(firstByte) {
}
};

ChangeUser.prototype._id = 'ChangeUser';

ChangeUser.prototype.start = function(handshakeInitializationPacket) {
var scrambleBuff = handshakeInitializationPacket.scrambleBuff();
scrambleBuff = Auth.token(this._password, scrambleBuff);
Expand Down
2 changes: 2 additions & 0 deletions lib/protocol/sequences/Handshake.js
Expand Up @@ -33,6 +33,8 @@ Handshake.prototype.determinePacket = function determinePacket(firstByte, parser
return undefined;
};

Handshake.prototype._id = 'Handshake';

Handshake.prototype['AuthSwitchRequestPacket'] = function (packet) {
var name = packet.authMethodName;
var data = Auth.auth(name, packet.authMethodData, {
Expand Down
2 changes: 2 additions & 0 deletions lib/protocol/sequences/Ping.js
Expand Up @@ -14,6 +14,8 @@ function Ping(options, callback) {
Sequence.call(this, options, callback);
}

Ping.prototype._id = 'Ping';

Ping.prototype.start = function() {
this.emit('packet', new Packets.ComPingPacket());
};
2 changes: 2 additions & 0 deletions lib/protocol/sequences/Query.js
Expand Up @@ -26,6 +26,8 @@ function Query(options, callback) {
this._loadError = null;
}

Query.prototype._id = 'Query';

Query.prototype.start = function() {
this.emit('packet', new Packets.ComQueryPacket(this.sql));
};
Expand Down
2 changes: 2 additions & 0 deletions lib/protocol/sequences/Quit.js
Expand Up @@ -15,6 +15,8 @@ function Quit(options, callback) {
this._started = false;
}

Quit.prototype._id = 'Quit';

Quit.prototype.end = function end(err) {
if (this._ended) {
return;
Expand Down
2 changes: 2 additions & 0 deletions lib/protocol/sequences/Sequence.js
Expand Up @@ -38,6 +38,8 @@ Sequence.determinePacket = function(byte) {
}
};

Sequence.prototype._id = 'Sequence';

Sequence.prototype.hasErrorHandler = function() {
return Boolean(this._callback) || listenerCount(this, 'error') > 1;
};
Expand Down
2 changes: 2 additions & 0 deletions lib/protocol/sequences/Statistics.js
Expand Up @@ -13,6 +13,8 @@ function Statistics(options, callback) {
Sequence.call(this, options, callback);
}

Statistics.prototype._id = 'Statistics';

Statistics.prototype.start = function() {
this.emit('packet', new Packets.ComStatisticsPacket());
};
Expand Down
4 changes: 2 additions & 2 deletions test/FakeServer.js
Expand Up @@ -332,8 +332,8 @@ FakeConnection.prototype._parsePacket = function _parsePacket(packetHeader) {
}
break;
default:
if (!this.emit(packet.constructor.name, packet)) {
throw new Error('Unexpected packet: ' + Packet.name);
if (!this.emit(packet._id, packet)) {
throw new Error('Unexpected packet: ' + packet._id);
}
}
};
Expand Down