Skip to content

Commit

Permalink
This makes mysqljs easier to minify and support minifier mangle
Browse files Browse the repository at this point in the history
  • Loading branch information
lneves12 committed Jul 10, 2020
1 parent 283425b commit 3b25db2
Show file tree
Hide file tree
Showing 32 changed files with 66 additions and 6 deletions.
8 changes: 4 additions & 4 deletions lib/protocol/Protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.');
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 + ')'
: '';
Expand Down
2 changes: 2 additions & 0 deletions lib/protocol/packets/AuthSwitchRequestPacket.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
2 changes: 2 additions & 0 deletions lib/protocol/packets/RowDataPacket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
2 changes: 2 additions & 0 deletions lib/protocol/packets/SSLRequestPacket.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 3b25db2

Please sign in to comment.