File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ function Client(config) {
2424
this.flags = Client.defaultFlags;
2525
this.maxPacketSize = 0x01000000;
2626
this.charsetNumber = 8;
27+
this.debug = false;
2728

2829
this._queue = [];
2930
this._connection = null;
@@ -113,6 +114,10 @@ Client..query = function(sql, params, cb) {
113114
};
114115

115116
Client..write = function(packet) {
117+
if (this.debug) {
118+
console.log('-> %s', packet.buffer.inspect());
119+
}
120+
116121
this._connection.write(packet.buffer);
117122
};
118123

@@ -180,6 +185,10 @@ Client.._dequeue = function() {
180185
};
181186

182187
Client.._handlePacket = function(packet) {
188+
if (this.debug) {
189+
this._debugPacket(packet);
190+
}
191+
183192
if (packet.type == Parser.GREETING_PACKET) {
184193
this._sendAuth(packet);
185194
return;
@@ -260,6 +269,21 @@ Client._packetToUserObject = function(packet) {
260269
delete packet.errorNumber;
261270
};
262271

272+
Client.._debugPacket = function(packet) {
273+
var packetName = null;
274+
for (var key in Parser) {
275+
if (!key.match(/_PACKET$/)) {
276+
continue;
277+
}
278+
279+
if (Parser[key] == packet.type) {
280+
packetName = key;
281+
break;
282+
}
283+
}
284+
console.log('<- %s: %j', packetName, packet);
285+
};
286+
263287
// Client Flags
264288
Client.LONG_PASSWORD = 1;
265289
Client.FOUND_ROWS = 2;
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ test(function constructor() {
2828
assert.strictEqual(client.password, null);
2929
assert.strictEqual(client.database, null);
3030

31+
assert.strictEqual(client.debug, false);
32+
3133
assert.strictEqual(client.flags, Client.defaultFlags);
3234
assert.strictEqual(client.maxPacketSize, 0x01000000);
3335
assert.strictEqual(client.charsetNumber, 8);
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ client.connect();
1010
client.query('SELECT 1 as field_a, 2 as field_b', function(err, results) {
1111
if (err) throw err;
1212

13-
console.log(results);
1413
assert.equal(results[0].field_a, 1);
1514
assert.equal(results[0].field_b, 2);
15+
client.end();
1616
});

0 commit comments

Comments
 (0)