@@ -8,6 +8,8 @@ var Readable = require('stream').Readable;
8
8
var utils = require ( __dirname + '/utils' ) ;
9
9
var Writer = require ( 'buffer-writer' ) ;
10
10
11
+ var TEXT_MODE = 0 ;
12
+ var BINARY_MODE = 1 ;
11
13
var Connection = function ( config ) {
12
14
EventEmitter . call ( this ) ;
13
15
config = config || { } ;
@@ -21,6 +23,7 @@ var Connection = function(config) {
21
23
this . writer = new Writer ( ) ;
22
24
this . ssl = config . ssl || false ;
23
25
this . _ending = false ;
26
+ this . _mode = TEXT_MODE ;
24
27
} ;
25
28
26
29
util . inherits ( Connection , EventEmitter ) ;
@@ -491,8 +494,15 @@ Connection.prototype.parseField = function() {
491
494
dataTypeID : this . parseInt32 ( ) ,
492
495
dataTypeSize : this . parseInt16 ( ) ,
493
496
dataTypeModifier : this . parseInt32 ( ) ,
494
- format : this . parseInt16 ( ) === 0 ? 'text' : 'binary'
497
+ format : undefined
495
498
} ;
499
+ if ( this . parseInt16 ( ) === TEXT_MODE ) {
500
+ this . _mode = TEXT_MODE ;
501
+ field . format = 'text' ;
502
+ } else {
503
+ this . _mode = BINARY_MODE ;
504
+ field . format = 'binary' ;
505
+ }
496
506
return field ;
497
507
} ;
498
508
@@ -503,7 +513,11 @@ Connection.prototype.parseD = function(msg) {
503
513
var length = this . parseInt32 ( ) ;
504
514
var value = null ;
505
515
if ( length !== - 1 ) {
506
- value = this . readBytes ( length ) ;
516
+ if ( this . _mode === TEXT_MODE ) {
517
+ value = this . readString ( length ) ;
518
+ } else {
519
+ value = this . readBytes ( length ) ;
520
+ }
507
521
}
508
522
fields . push ( value ) ;
509
523
}
@@ -572,7 +586,7 @@ Connection.prototype.parseGH = function (msg) {
572
586
} ;
573
587
574
588
Connection . prototype . readChar = function ( ) {
575
- return Buffer ( [ this . buffer [ this . offset ++ ] ] ) . toString ( this . encoding ) ;
589
+ return this . readString ( 1 ) ;
576
590
} ;
577
591
578
592
Connection . prototype . parseInt32 = function ( ) {
@@ -597,7 +611,7 @@ Connection.prototype.readBytes = function(length) {
597
611
598
612
Connection . prototype . parseCString = function ( ) {
599
613
var start = this . offset ;
600
- while ( this . buffer [ this . offset ++ ] ) { }
614
+ while ( this . buffer [ this . offset ++ ] !== 0 ) { }
601
615
return this . buffer . toString ( this . encoding , start , this . offset - 1 ) ;
602
616
} ;
603
617
0 commit comments