@@ -5,6 +5,7 @@ var util = require('util');
5
5
6
6
var utils = require ( __dirname + '/utils' ) ;
7
7
var Writer = require ( 'buffer-writer' ) ;
8
+ var Reader = require ( 'packet-reader' ) ;
8
9
9
10
var TEXT_MODE = 0 ;
10
11
var BINARY_MODE = 1 ;
@@ -23,6 +24,10 @@ var Connection = function(config) {
23
24
this . _ending = false ;
24
25
this . _mode = TEXT_MODE ;
25
26
this . _emitMessage = false ;
27
+ this . _reader = new Reader ( {
28
+ headerSize : 1 ,
29
+ lengthPadding : - 4
30
+ } ) ;
26
31
var self = this ;
27
32
this . on ( 'newListener' , function ( eventName ) {
28
33
if ( eventName == 'message' ) {
@@ -87,17 +92,19 @@ Connection.prototype.connect = function(port, host) {
87
92
} ;
88
93
89
94
Connection . prototype . attachListeners = function ( stream ) {
95
+ var self = this ;
90
96
stream . on ( 'data' , function ( buff ) {
91
- this . setBuffer ( buff ) ;
92
- var msg = this . parseMessage ( ) ;
93
- while ( msg ) {
94
- if ( this . _emitMessage ) {
95
- this . emit ( 'message' , msg ) ;
97
+ self . _reader . addChunk ( buff ) ;
98
+ var packet = self . _reader . read ( ) ;
99
+ while ( packet ) {
100
+ var msg = self . parseMessage ( packet ) ;
101
+ if ( self . _emitMessage ) {
102
+ self . emit ( 'message' , msg ) ;
96
103
}
97
- this . emit ( msg . name , msg ) ;
98
- msg = this . parseMessage ( ) ;
104
+ self . emit ( msg . name , msg ) ;
105
+ packet = self . _reader . read ( ) ;
99
106
}
100
- } . bind ( this ) ) ;
107
+ } ) ;
101
108
} ;
102
109
103
110
Connection . prototype . requestSsl = function ( config ) {
@@ -306,63 +313,16 @@ Connection.prototype.sendCopyFail = function (msg) {
306
313
this . _send ( 0x66 ) ;
307
314
} ;
308
315
309
- //parsing methods
310
- Connection . prototype . setBuffer = function ( buffer ) {
311
- if ( this . lastBuffer ) { //we have unfinished biznaz
312
- //need to combine last two buffers
313
- var remaining = this . lastBuffer . length - this . lastOffset ;
314
- var combinedBuffer = new Buffer ( buffer . length + remaining ) ;
315
- this . lastBuffer . copy ( combinedBuffer , 0 , this . lastOffset ) ;
316
- buffer . copy ( combinedBuffer , remaining , 0 ) ;
317
- buffer = combinedBuffer ;
318
- }
319
- this . lastBuffer = false ;
320
- this . buffer = buffer ;
321
- this . offset = 0 ;
322
- } ;
323
-
324
- Connection . prototype . readSslResponse = function ( ) {
325
- var remaining = this . buffer . length - ( this . offset ) ;
326
- if ( remaining < 1 ) {
327
- this . lastBuffer = this . buffer ;
328
- this . lastOffset = this . offset ;
329
- return false ;
330
- }
331
- return {
332
- name : 'sslresponse' ,
333
- text : this . buffer [ this . offset ++ ]
334
- } ;
335
- } ;
336
-
337
316
var Message = function ( name , length ) {
338
317
this . name = name ;
339
318
this . length = length ;
340
319
} ;
341
320
342
- Connection . prototype . parseMessage = function ( ) {
343
- var remaining = this . buffer . length - ( this . offset ) ;
344
- if ( remaining < 5 ) {
345
- //cannot read id + length without at least 5 bytes
346
- //just abort the read now
347
- this . lastBuffer = this . buffer ;
348
- this . lastOffset = this . offset ;
349
- return false ;
350
- }
351
-
352
- //read message id code
353
- var id = this . buffer [ this . offset ++ ] ;
354
- var buffer = this . buffer ;
355
- //read message length
356
- var length = this . parseInt32 ( buffer ) ;
357
-
358
- if ( remaining <= length ) {
359
- this . lastBuffer = this . buffer ;
360
- //rewind the last 5 bytes we read
361
- this . lastOffset = this . offset - 5 ;
362
- return false ;
363
- }
321
+ Connection . prototype . parseMessage = function ( buffer ) {
364
322
365
- switch ( id )
323
+ this . offset = 0 ;
324
+ var length = buffer . length + 4 ;
325
+ switch ( this . _reader . header )
366
326
{
367
327
368
328
case 0x52 : //R
@@ -422,7 +382,6 @@ Connection.prototype.parseMessage = function() {
422
382
case 0x64 : //d
423
383
return this . parsed ( buffer , length ) ;
424
384
}
425
- return false ;
426
385
} ;
427
386
428
387
Connection . prototype . parseR = function ( buffer , length ) {
@@ -440,7 +399,7 @@ Connection.prototype.parseR = function(buffer, length) {
440
399
if ( code === 5 ) { //md5 required
441
400
msg . name = 'authenticationMD5Password' ;
442
401
msg . salt = new Buffer ( 4 ) ;
443
- this . buffer . copy ( msg . salt , 0 , this . offset , this . offset + 4 ) ;
402
+ buffer . copy ( msg . salt , 0 , this . offset , this . offset + 4 ) ;
444
403
this . offset += 4 ;
445
404
return msg ;
446
405
}
@@ -610,7 +569,7 @@ Connection.prototype.parseH = function(buffer, length) {
610
569
} ;
611
570
612
571
Connection . prototype . parseGH = function ( buffer , msg ) {
613
- var isBinary = this . buffer [ this . offset ] !== 0 ;
572
+ var isBinary = buffer [ this . offset ] !== 0 ;
614
573
this . offset ++ ;
615
574
msg . binary = isBinary ;
616
575
var columnCount = this . parseInt16 ( buffer ) ;
0 commit comments