@@ -57,15 +57,16 @@ Util.inherits(FakeConnection, EventEmitter);
5757function FakeConnection ( socket ) {
5858 EventEmitter . call ( this ) ;
5959
60+ this . database = null ;
61+ this . user = null ;
62+
6063 this . _socket = socket ;
6164 this . _ssl = null ;
6265 this . _stream = socket ;
6366 this . _parser = new Parser ( { onPacket : this . _parsePacket . bind ( this ) } ) ;
6467
68+ this . _expectedNextPacket = null ;
6569 this . _handshakeInitializationPacket = null ;
66- this . _clientAuthenticationPacket = null ;
67- this . _oldPasswordPacket = null ;
68- this . _authSwitchResponse = null ;
6970 this . _handshakeOptions = { } ;
7071
7172 socket . on ( 'data' , this . _handleData . bind ( this ) ) ;
@@ -107,6 +108,21 @@ FakeConnection.prototype._sendAuthResponse = function _sendAuthResponse(got, exp
107108} ;
108109
109110FakeConnection . prototype . _sendPacket = function ( packet ) {
111+ switch ( packet . constructor ) {
112+ case Packets . AuthSwitchRequestPacket :
113+ this . _expectedNextPacket = Packets . AuthSwitchResponsePacket ;
114+ break ;
115+ case Packets . HandshakeInitializationPacket :
116+ this . _expectedNextPacket = Packets . ClientAuthenticationPacket ;
117+ break ;
118+ case Packets . UseOldPasswordPacket :
119+ this . _expectedNextPacket = Packets . OldPasswordPacket ;
120+ break ;
121+ default :
122+ this . _expectedNextPacket = null ;
123+ break ;
124+ }
125+
110126 var writer = new PacketWriter ( ) ;
111127 packet . write ( writer ) ;
112128 this . _stream . write ( writer . toBuffer ( this . _parser ) ) ;
@@ -164,7 +180,7 @@ FakeConnection.prototype._handleQueryPacket = function _handleQueryPacket(packet
164180 this . _sendPacket ( new Packets . EofPacket ( ) ) ;
165181
166182 var writer = new PacketWriter ( ) ;
167- writer . writeLengthCodedString ( ( this . _clientAuthenticationPacket . user || '' ) + '@localhost' ) ;
183+ writer . writeLengthCodedString ( ( this . user || '' ) + '@localhost' ) ;
168184 this . _socket . write ( writer . toBuffer ( this . _parser ) ) ;
169185
170186 this . _sendPacket ( new Packets . EofPacket ( ) ) ;
@@ -266,7 +282,9 @@ FakeConnection.prototype._parsePacket = function() {
266282
267283 switch ( Packet ) {
268284 case Packets . ClientAuthenticationPacket :
269- this . _clientAuthenticationPacket = packet ;
285+ this . database = ( packet . database || null ) ;
286+ this . user = ( packet . user || null ) ;
287+
270288 if ( this . _handshakeOptions . oldPassword ) {
271289 this . _sendPacket ( new Packets . UseOldPasswordPacket ( ) ) ;
272290 } else if ( this . _handshakeOptions . authMethodName ) {
@@ -285,17 +303,11 @@ FakeConnection.prototype._parsePacket = function() {
285303 this . _startTLS ( ) ;
286304 break ;
287305 case Packets . OldPasswordPacket :
288- this . _oldPasswordPacket = packet ;
289-
290306 var expected = Auth . scramble323 ( this . _handshakeInitializationPacket . scrambleBuff ( ) , this . _handshakeOptions . password ) ;
291-
292307 this . _sendAuthResponse ( packet . scrambleBuff , expected ) ;
293308 break ;
294309 case Packets . AuthSwitchResponsePacket :
295- this . _authSwitchResponse = packet ;
296-
297310 var expected = Auth . token ( this . _handshakeOptions . password , Buffer . from ( '00112233445566778899AABBCCDDEEFF01020304' , 'hex' ) ) ;
298-
299311 this . _sendAuthResponse ( packet . data , expected ) ;
300312 break ;
301313 case Packets . ComQueryPacket :
@@ -327,16 +339,9 @@ FakeConnection.prototype._parsePacket = function() {
327339 break ;
328340 }
329341
330- this . _clientAuthenticationPacket = new Packets . ClientAuthenticationPacket ( {
331- clientFlags : this . _clientAuthenticationPacket . clientFlags ,
332- filler : this . _clientAuthenticationPacket . filler ,
333- maxPacketSize : this . _clientAuthenticationPacket . maxPacketSize ,
334- protocol41 : this . _clientAuthenticationPacket . protocol41 ,
335- charsetNumber : packet . charsetNumber ,
336- database : packet . database ,
337- scrambleBuff : packet . scrambleBuff ,
338- user : packet . user
339- } ) ;
342+ this . database = ( packet . database || null ) ;
343+ this . user = ( packet . user || null ) ;
344+
340345 this . _sendPacket ( new Packets . OkPacket ( ) ) ;
341346 this . _parser . resetPacketNumber ( ) ;
342347 }
@@ -352,18 +357,18 @@ FakeConnection.prototype._parsePacket = function() {
352357} ;
353358
354359FakeConnection . prototype . _determinePacket = function _determinePacket ( ) {
355- if ( ! this . _clientAuthenticationPacket ) {
356- return ! this . _ssl && ( this . _parser . peak ( 1 ) << 8 ) & ClientConstants . CLIENT_SSL
357- ? Packets . SSLRequestPacket
358- : Packets . ClientAuthenticationPacket ;
359- }
360+ if ( this . _expectedNextPacket ) {
361+ var Packet = this . _expectedNextPacket ;
360362
361- if ( this . _handshakeOptions . oldPassword && ! this . _oldPasswordPacket ) {
362- return Packets . OldPasswordPacket ;
363- }
363+ if ( Packet === Packets . ClientAuthenticationPacket ) {
364+ return ! this . _ssl && ( this . _parser . peak ( 1 ) << 8 ) & ClientConstants . CLIENT_SSL
365+ ? Packets . SSLRequestPacket
366+ : Packets . ClientAuthenticationPacket ;
367+ }
368+
369+ this . _expectedNextPacket = null ;
364370
365- if ( this . _handshakeOptions . authMethodName && ! this . _authSwitchResponse ) {
366- return Packets . AuthSwitchResponsePacket ;
371+ return Packet ;
367372 }
368373
369374 var firstByte = this . _parser . peak ( ) ;
0 commit comments