From bca1ad6535145f5db6ef6fd88fc33889e416fde8 Mon Sep 17 00:00:00 2001 From: lneves 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 | 8 ++++---- 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 | 2 ++ 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, 66 insertions(+), 6 deletions(-) diff --git a/lib/protocol/Protocol.js b/lib/protocol/Protocol.js index ab371059b..ab8d3c296 100644 --- a/lib/protocol/Protocol.js +++ b/lib/protocol/Protocol.js @@ -157,7 +157,7 @@ Protocol.prototype._enqueue = function(sequence) { self._emitPacket(packet); }) .on('timeout', function() { - var err = new Error(sequence.constructor.name + ' inactivity timeout'); + var err = new Error(sequence.id + ' inactivity timeout'); err.code = 'PROTOCOL_SEQUENCE_TIMEOUT'; err.fatal = true; @@ -206,7 +206,7 @@ Protocol.prototype._enqueue = function(sequence) { Protocol.prototype._validateEnqueue = function _validateEnqueue(sequence) { var err; - var prefix = 'Cannot enqueue ' + sequence.constructor.name; + var prefix = 'Cannot enqueue ' + sequence.id; if (this._fatalError) { err = new Error(prefix + ' after fatal error.'); @@ -253,7 +253,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; // Special case: Faster dispatch, and parsing done inside sequence if (Packet === Packets.RowDataPacket) { @@ -447,7 +447,7 @@ Protocol.prototype._debugPacket = function(incoming, packet) { var direction = incoming ? '<--' : '-->'; - var packetName = packet.constructor.name; + var packetName = packet.id; var threadId = connection && connection.threadId !== null ? ' (' + connection.threadId + ')' : ''; diff --git a/lib/protocol/packets/AuthSwitchRequestPacket.js b/lib/protocol/packets/AuthSwitchRequestPacket.js index c74e6ec23..a7b5dc63e 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..b085c96b3 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..da53d6af6 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..0c37f95fe 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..b7b5b3f43 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..fa061c86f 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..963df4af2 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..8b1f733df 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..ae7a618e4 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..a5684b05f 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..fcb9ec276 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..32ea7a254 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..21b38466e 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..8b878d37c 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..883a73b09 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..213e6edb1 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..34d51dbd6 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..ab026caaa 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..47ae6c10b 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..088a4cbb0 100644 --- a/lib/protocol/packets/RowDataPacket.js +++ b/lib/protocol/packets/RowDataPacket.js @@ -19,6 +19,8 @@ Object.defineProperty(RowDataPacket.prototype, '_typeCast', { value : typeCast }); +RowDataPacket.prototype.id = '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..662a96dd6 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..7a2e1460e 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..37300a30e 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..6050c697c 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..118faf10c 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..bd34f73c1 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..0af71d9f1 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..87e706ca8 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..8d5828b2c 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..7c16aaf5a 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..adbbcf320 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); } } };