@@ -6,6 +6,8 @@ var util = require('util');
66var utils = require ( __dirname + '/utils' ) ;
77var Writer = require ( 'buffer-writer' ) ;
88
9+ var TEXT_MODE = 0 ;
10+ var BINARY_MODE = 1 ;
911var Connection = function ( config ) {
1012 EventEmitter . call ( this ) ;
1113 config = config || { } ;
@@ -19,6 +21,7 @@ var Connection = function(config) {
1921 this . writer = new Writer ( ) ;
2022 this . ssl = config . ssl || false ;
2123 this . _ending = false ;
24+ this . _mode = TEXT_MODE ;
2225} ;
2326
2427util . inherits ( Connection , EventEmitter ) ;
@@ -488,8 +491,15 @@ Connection.prototype.parseField = function() {
488491 dataTypeID : this . parseInt32 ( ) ,
489492 dataTypeSize : this . parseInt16 ( ) ,
490493 dataTypeModifier : this . parseInt32 ( ) ,
491- format : this . parseInt16 ( ) === 0 ? 'text' : 'binary'
494+ format : undefined
492495 } ;
496+ if ( this . parseInt16 ( ) === TEXT_MODE ) {
497+ this . _mode = TEXT_MODE ;
498+ field . format = 'text' ;
499+ } else {
500+ this . _mode = BINARY_MODE ;
501+ field . format = 'binary' ;
502+ }
493503 return field ;
494504} ;
495505
@@ -500,7 +510,11 @@ Connection.prototype.parseD = function(msg) {
500510 var length = this . parseInt32 ( ) ;
501511 var value = null ;
502512 if ( length !== - 1 ) {
503- value = this . readBytes ( length ) ;
513+ if ( this . _mode === TEXT_MODE ) {
514+ value = this . readString ( length ) ;
515+ } else {
516+ value = this . readBytes ( length ) ;
517+ }
504518 }
505519 fields . push ( value ) ;
506520 }
@@ -569,7 +583,7 @@ Connection.prototype.parseGH = function (msg) {
569583} ;
570584
571585Connection . prototype . readChar = function ( ) {
572- return Buffer ( [ this . buffer [ this . offset ++ ] ] ) . toString ( this . encoding ) ;
586+ return this . readString ( 1 ) ;
573587} ;
574588
575589Connection . prototype . parseInt32 = function ( ) {
@@ -594,7 +608,7 @@ Connection.prototype.readBytes = function(length) {
594608
595609Connection . prototype . parseCString = function ( ) {
596610 var start = this . offset ;
597- while ( this . buffer [ this . offset ++ ] ) { }
611+ while ( this . buffer [ this . offset ++ ] !== 0 ) { }
598612 return this . buffer . toString ( this . encoding , start , this . offset - 1 ) ;
599613} ;
600614
0 commit comments