From 739deacbc9a2f9233073c2ed3ade767f65737c06 Mon Sep 17 00:00:00 2001 From: Luis Neves Date: Fri, 10 Jul 2020 11:31:09 +0100 Subject: [PATCH] This makes mysqljs easier to minify and support minifier mangle --- lib/protocol/Protocol.js | 10 ++++++---- lib/protocol/packets/AuthSwitchRequestPacket.js | 2 ++ lib/protocol/packets/AuthSwitchResponsePacket.js | 2 ++ lib/protocol/packets/ClientAuthenticationPacket.js | 2 ++ lib/protocol/packets/ComChangeUserPacket.js | 2 ++ lib/protocol/packets/ComPingPacket.js | 2 ++ lib/protocol/packets/ComQueryPacket.js | 2 ++ lib/protocol/packets/ComQuitPacket.js | 2 ++ lib/protocol/packets/ComStatisticsPacket.js | 2 ++ lib/protocol/packets/EmptyPacket.js | 2 ++ lib/protocol/packets/EofPacket.js | 2 ++ lib/protocol/packets/ErrorPacket.js | 2 ++ lib/protocol/packets/Field.js | 2 ++ lib/protocol/packets/FieldPacket.js | 2 ++ lib/protocol/packets/HandshakeInitializationPacket.js | 2 ++ lib/protocol/packets/LocalDataFilePacket.js | 2 ++ lib/protocol/packets/LocalInfileRequestPacket.js | 2 ++ lib/protocol/packets/OkPacket.js | 2 ++ lib/protocol/packets/OldPasswordPacket.js | 2 ++ lib/protocol/packets/ResultSetHeaderPacket.js | 2 ++ lib/protocol/packets/RowDataPacket.js | 6 ++++++ lib/protocol/packets/SSLRequestPacket.js | 2 ++ lib/protocol/packets/StatisticsPacket.js | 2 ++ lib/protocol/packets/UseOldPasswordPacket.js | 2 ++ lib/protocol/sequences/ChangeUser.js | 2 ++ lib/protocol/sequences/Handshake.js | 2 ++ lib/protocol/sequences/Ping.js | 2 ++ lib/protocol/sequences/Query.js | 2 ++ lib/protocol/sequences/Quit.js | 2 ++ lib/protocol/sequences/Sequence.js | 2 ++ lib/protocol/sequences/Statistics.js | 2 ++ test/FakeServer.js | 4 ++-- 32 files changed, 72 insertions(+), 6 deletions(-) diff --git a/lib/protocol/Protocol.js b/lib/protocol/Protocol.js index ab371059b..684f21a4e 100644 --- a/lib/protocol/Protocol.js +++ b/lib/protocol/Protocol.js @@ -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; @@ -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.'); @@ -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) { @@ -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 + ')' : ''; diff --git a/lib/protocol/packets/AuthSwitchRequestPacket.js b/lib/protocol/packets/AuthSwitchRequestPacket.js index c74e6ec23..a4a71f7cf 100644 --- a/lib/protocol/packets/AuthSwitchRequestPacket.js +++ b/lib/protocol/packets/AuthSwitchRequestPacket.js @@ -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(); diff --git a/lib/protocol/packets/AuthSwitchResponsePacket.js b/lib/protocol/packets/AuthSwitchResponsePacket.js index 488abbd03..db7f2ba4b 100644 --- a/lib/protocol/packets/AuthSwitchResponsePacket.js +++ b/lib/protocol/packets/AuthSwitchResponsePacket.js @@ -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(); }; diff --git a/lib/protocol/packets/ClientAuthenticationPacket.js b/lib/protocol/packets/ClientAuthenticationPacket.js index 595db77a0..83b62fa24 100644 --- a/lib/protocol/packets/ClientAuthenticationPacket.js +++ b/lib/protocol/packets/ClientAuthenticationPacket.js @@ -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); diff --git a/lib/protocol/packets/ComChangeUserPacket.js b/lib/protocol/packets/ComChangeUserPacket.js index 327884235..1de232989 100644 --- a/lib/protocol/packets/ComChangeUserPacket.js +++ b/lib/protocol/packets/ComChangeUserPacket.js @@ -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(); diff --git a/lib/protocol/packets/ComPingPacket.js b/lib/protocol/packets/ComPingPacket.js index dd332c93c..642817754 100644 --- a/lib/protocol/packets/ComPingPacket.js +++ b/lib/protocol/packets/ComPingPacket.js @@ -3,6 +3,8 @@ function ComPingPacket() { this.command = 0x0e; } +ComPingPacket.prototype._id = 'ComPingPacket'; + ComPingPacket.prototype.write = function(writer) { writer.writeUnsignedNumber(1, this.command); }; diff --git a/lib/protocol/packets/ComQueryPacket.js b/lib/protocol/packets/ComQueryPacket.js index 7ac191fd0..cf7c8ec9f 100644 --- a/lib/protocol/packets/ComQueryPacket.js +++ b/lib/protocol/packets/ComQueryPacket.js @@ -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); diff --git a/lib/protocol/packets/ComQuitPacket.js b/lib/protocol/packets/ComQuitPacket.js index 1104061cb..001df19e2 100644 --- a/lib/protocol/packets/ComQuitPacket.js +++ b/lib/protocol/packets/ComQuitPacket.js @@ -3,6 +3,8 @@ function ComQuitPacket() { this.command = 0x01; } +ComQuitPacket.prototype._id = 'ComQuitPacket'; + ComQuitPacket.prototype.parse = function parse(parser) { this.command = parser.parseUnsignedNumber(1); }; diff --git a/lib/protocol/packets/ComStatisticsPacket.js b/lib/protocol/packets/ComStatisticsPacket.js index 5e3913e15..f7bba79d7 100644 --- a/lib/protocol/packets/ComStatisticsPacket.js +++ b/lib/protocol/packets/ComStatisticsPacket.js @@ -3,6 +3,8 @@ function ComStatisticsPacket() { this.command = 0x09; } +ComStatisticsPacket.prototype._id = 'ComStatisticsPacket'; + ComStatisticsPacket.prototype.write = function(writer) { writer.writeUnsignedNumber(1, this.command); }; diff --git a/lib/protocol/packets/EmptyPacket.js b/lib/protocol/packets/EmptyPacket.js index 27dd68604..3a4023b39 100644 --- a/lib/protocol/packets/EmptyPacket.js +++ b/lib/protocol/packets/EmptyPacket.js @@ -2,6 +2,8 @@ module.exports = EmptyPacket; function EmptyPacket() { } +EmptyPacket.prototype._id = 'EmptyPacket'; + EmptyPacket.prototype.parse = function parse() { }; diff --git a/lib/protocol/packets/EofPacket.js b/lib/protocol/packets/EofPacket.js index b80ca5ef2..54de8b68d 100644 --- a/lib/protocol/packets/EofPacket.js +++ b/lib/protocol/packets/EofPacket.js @@ -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) { diff --git a/lib/protocol/packets/ErrorPacket.js b/lib/protocol/packets/ErrorPacket.js index e03de00ce..b7528912e 100644 --- a/lib/protocol/packets/ErrorPacket.js +++ b/lib/protocol/packets/ErrorPacket.js @@ -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); diff --git a/lib/protocol/packets/Field.js b/lib/protocol/packets/Field.js index a5d58edb6..b3167902a 100644 --- a/lib/protocol/packets/Field.js +++ b/lib/protocol/packets/Field.js @@ -13,6 +13,8 @@ function Field(options) { this.length = options.packet.length; } +Field.prototype._id = 'Field'; + Field.prototype.string = function () { return this.parser.parseLengthCodedString(); }; diff --git a/lib/protocol/packets/FieldPacket.js b/lib/protocol/packets/FieldPacket.js index 12cfed10c..cd0ba66ab 100644 --- a/lib/protocol/packets/FieldPacket.js +++ b/lib/protocol/packets/FieldPacket.js @@ -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(); diff --git a/lib/protocol/packets/HandshakeInitializationPacket.js b/lib/protocol/packets/HandshakeInitializationPacket.js index b2510633b..9c29d4039 100644 --- a/lib/protocol/packets/HandshakeInitializationPacket.js +++ b/lib/protocol/packets/HandshakeInitializationPacket.js @@ -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(); diff --git a/lib/protocol/packets/LocalDataFilePacket.js b/lib/protocol/packets/LocalDataFilePacket.js index af7aaa045..5572db0f9 100644 --- a/lib/protocol/packets/LocalDataFilePacket.js +++ b/lib/protocol/packets/LocalDataFilePacket.js @@ -10,6 +10,8 @@ function LocalDataFilePacket(data) { this.data = data; } +LocalDataFilePacket.prototype._id = 'LocalDataFilePacket'; + LocalDataFilePacket.prototype.write = function(writer) { writer.writeBuffer(this.data); }; diff --git a/lib/protocol/packets/LocalInfileRequestPacket.js b/lib/protocol/packets/LocalInfileRequestPacket.js index b1f68bab4..6a31b6d23 100644 --- a/lib/protocol/packets/LocalInfileRequestPacket.js +++ b/lib/protocol/packets/LocalInfileRequestPacket.js @@ -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'); diff --git a/lib/protocol/packets/OkPacket.js b/lib/protocol/packets/OkPacket.js index 7caf3b0e7..9e5f74fa7 100644 --- a/lib/protocol/packets/OkPacket.js +++ b/lib/protocol/packets/OkPacket.js @@ -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(); diff --git a/lib/protocol/packets/OldPasswordPacket.js b/lib/protocol/packets/OldPasswordPacket.js index a72951021..7163d87ac 100644 --- a/lib/protocol/packets/OldPasswordPacket.js +++ b/lib/protocol/packets/OldPasswordPacket.js @@ -5,6 +5,8 @@ function OldPasswordPacket(options) { this.scrambleBuff = options.scrambleBuff; } +OldPasswordPacket.prototype._id = 'OldPasswordPacket'; + OldPasswordPacket.prototype.parse = function(parser) { this.scrambleBuff = parser.parsePacketTerminatedBuffer(); }; diff --git a/lib/protocol/packets/ResultSetHeaderPacket.js b/lib/protocol/packets/ResultSetHeaderPacket.js index a097ea1f2..d0f700898 100644 --- a/lib/protocol/packets/ResultSetHeaderPacket.js +++ b/lib/protocol/packets/ResultSetHeaderPacket.js @@ -5,6 +5,8 @@ function ResultSetHeaderPacket(options) { this.fieldCount = options.fieldCount; } +ResultSetHeaderPacket.prototype._id = 'ResultSetHeaderPacket'; + ResultSetHeaderPacket.prototype.parse = function(parser) { this.fieldCount = parser.parseLengthCodedNumber(); }; diff --git a/lib/protocol/packets/RowDataPacket.js b/lib/protocol/packets/RowDataPacket.js index b8ec4b895..9c74f1895 100644 --- a/lib/protocol/packets/RowDataPacket.js +++ b/lib/protocol/packets/RowDataPacket.js @@ -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 () { diff --git a/lib/protocol/packets/SSLRequestPacket.js b/lib/protocol/packets/SSLRequestPacket.js index a57cfc1a1..85639cf02 100644 --- a/lib/protocol/packets/SSLRequestPacket.js +++ b/lib/protocol/packets/SSLRequestPacket.js @@ -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); diff --git a/lib/protocol/packets/StatisticsPacket.js b/lib/protocol/packets/StatisticsPacket.js index 5f70b3ba7..257ff3f8a 100644 --- a/lib/protocol/packets/StatisticsPacket.js +++ b/lib/protocol/packets/StatisticsPacket.js @@ -3,6 +3,8 @@ function StatisticsPacket() { this.message = undefined; } +StatisticsPacket.prototype._id = 'StatisticsPacket'; + StatisticsPacket.prototype.parse = function(parser) { this.message = parser.parsePacketTerminatedString(); diff --git a/lib/protocol/packets/UseOldPasswordPacket.js b/lib/protocol/packets/UseOldPasswordPacket.js index d73bf4459..4ba7ef665 100644 --- a/lib/protocol/packets/UseOldPasswordPacket.js +++ b/lib/protocol/packets/UseOldPasswordPacket.js @@ -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); }; diff --git a/lib/protocol/sequences/ChangeUser.js b/lib/protocol/sequences/ChangeUser.js index e1cc1fbc3..234f37017 100644 --- a/lib/protocol/sequences/ChangeUser.js +++ b/lib/protocol/sequences/ChangeUser.js @@ -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); diff --git a/lib/protocol/sequences/Handshake.js b/lib/protocol/sequences/Handshake.js index 8fad0fcf3..1d5151c00 100644 --- a/lib/protocol/sequences/Handshake.js +++ b/lib/protocol/sequences/Handshake.js @@ -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, { diff --git a/lib/protocol/sequences/Ping.js b/lib/protocol/sequences/Ping.js index 230f3c1ab..f96eb5346 100644 --- a/lib/protocol/sequences/Ping.js +++ b/lib/protocol/sequences/Ping.js @@ -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()); }; diff --git a/lib/protocol/sequences/Query.js b/lib/protocol/sequences/Query.js index b7632959b..cf6f441e4 100644 --- a/lib/protocol/sequences/Query.js +++ b/lib/protocol/sequences/Query.js @@ -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)); }; diff --git a/lib/protocol/sequences/Quit.js b/lib/protocol/sequences/Quit.js index 3c34c5820..a943596ff 100644 --- a/lib/protocol/sequences/Quit.js +++ b/lib/protocol/sequences/Quit.js @@ -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; diff --git a/lib/protocol/sequences/Sequence.js b/lib/protocol/sequences/Sequence.js index de82dc270..dfba61932 100644 --- a/lib/protocol/sequences/Sequence.js +++ b/lib/protocol/sequences/Sequence.js @@ -38,6 +38,8 @@ Sequence.determinePacket = function(byte) { } }; +Sequence.prototype._id = 'Sequence'; + Sequence.prototype.hasErrorHandler = function() { return Boolean(this._callback) || listenerCount(this, 'error') > 1; }; diff --git a/lib/protocol/sequences/Statistics.js b/lib/protocol/sequences/Statistics.js index c75b5d936..ee17c527f 100644 --- a/lib/protocol/sequences/Statistics.js +++ b/lib/protocol/sequences/Statistics.js @@ -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()); }; diff --git a/test/FakeServer.js b/test/FakeServer.js index 28f67f257..a5fb1de35 100644 --- a/test/FakeServer.js +++ b/test/FakeServer.js @@ -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); } } };