@@ -6,13 +6,10 @@ var EventEmitter = require('events');
66var msgpack = require ( 'msgpack' ) ;
77var vow = require ( 'vow' ) ;
88var crypto = require ( 'crypto' ) ;
9- var shasum = crypto . createHash ( 'sha1' ) ;
109var xor = require ( 'bitwise-xor' ) ;
1110
1211var shatransform = function ( t ) {
13- var sha1 = crypto . createHash ( 'sha1' ) ;
14- sha1 . update ( t ) ;
15- return sha1 . digest ( ) ;
12+ return crypto . createHash ( 'sha1' ) . update ( t ) . digest ( ) ;
1613} ;
1714
1815var states = {
@@ -67,7 +64,6 @@ function TarantoolConnection (options){
6764}
6865
6966TarantoolConnection . prototype . onData = function ( data ) {
70- console . log ( data . length , data . toString ( ) ) ;
7167 switch ( this . state ) {
7268 case states . PREHELLO :
7369 for ( var i = 0 ; i < this . commandsQueue . length ; i ++ )
@@ -79,12 +75,10 @@ TarantoolConnection.prototype.onData = function(data){
7975 i -- ;
8076 }
8177 }
82- console . log ( 'connect data' , data , data . toString ( ) , data . slice ( 64 , 108 ) . toString ( ) , data . length ) ;
8378 this . salt = data . slice ( 64 , 108 ) . toString ( 'utf8' ) ;
8479 this . state = states . CONNECTED ;
8580 break ;
8681 case states . CONNECTED :
87- //new TarantoolResponse(data);
8882 var trackResult = this . _responseBufferTrack ( data ) ;
8983 if ( trackResult . length == 2 )
9084 {
@@ -145,30 +139,23 @@ TarantoolConnection.prototype._responseBufferTrack = function(buffer, length){
145139} ;
146140
147141TarantoolConnection . prototype . _processResponse = function ( buffer ) {
148- try {
149- var success = buffer [ 1 ] == 0x00 ;
150- console . log ( buffer )
151- // add fixarraymap with 2 objects before main object
152- var dataBuffer = Buffer . concat ( [ new Buffer ( [ 0x92 ] ) , buffer ] ) ;
153- console . log ( dataBuffer ) ;
154- var obj = msgpack . unpack ( dataBuffer ) ;
155- console . log ( obj ) ;
156- var reqId = obj [ 0 ] [ 1 ] ;
157- var task = this . commandsQueue . filter ( function ( t ) {
158- return t [ 1 ] == reqId ;
159- } ) [ 0 ] ;
160- var dfd = task [ 2 ] ;
161- if ( success )
162- dfd . resolve ( this . _processResponseBody ( task [ 0 ] , obj [ 1 ] [ tarantoolConstants . KeysCode . data ] ) ) ;
163- else
164- dfd . reject ( obj [ 1 ] [ tarantoolConstants . KeysCode . e ] ) ;
165- } catch ( e ) {
166- console . log ( e , e . stack ) ;
167- }
142+ // add fixarraymap with 2 objects before main object
143+ var dataBuffer = Buffer . concat ( [ new Buffer ( [ 0x92 ] ) , buffer ] ) ;
144+ var obj = msgpack . unpack ( dataBuffer ) ;
145+ var reqId = obj [ 0 ] [ 1 ] ;
146+ var task = this . commandsQueue . filter ( function ( t ) {
147+ return t [ 1 ] == reqId ;
148+ } ) [ 0 ] ;
149+ var dfd = task [ 2 ] ;
150+ var success = obj [ 0 ] [ 0 ] == 0 ? true : false ;
151+ if ( success )
152+ dfd . resolve ( this . _processResponseBody ( task [ 0 ] , obj [ 1 ] [ tarantoolConstants . KeysCode . data ] ) ) ;
153+ else
154+ dfd . reject ( obj [ 1 ] [ tarantoolConstants . KeysCode . error ] ) ;
168155} ;
169156
170157TarantoolConnection . prototype . _processResponseBody = function ( cmd , data ) {
171- return data ;
158+ return cmd == tarantoolConstants . RequestCode . rqAuth ? true : data ;
172159} ;
173160
174161TarantoolConnection . prototype . _dropAll = function ( ) {
@@ -186,7 +173,6 @@ TarantoolConnection.prototype.onError = function(error){
186173} ;
187174
188175TarantoolConnection . prototype . connect = function ( ) {
189- console . log ( 'pre connect' ) ;
190176 this . state = states . CONNECTING ;
191177 return new Promise ( function ( resolve , reject ) {
192178 this . commandsQueue . push ( [ commandType . CONNECT , resolve , reject ] ) ;
@@ -196,10 +182,8 @@ TarantoolConnection.prototype.connect = function(){
196182
197183TarantoolConnection . prototype . ping = function ( ) {
198184 var dfd = vow . defer ( ) ;
199- console . log ( 'start ping' ) ;
200185 var reqId = requestId . getId ( ) ;
201186 var header = this . _header ( tarantoolConstants . RequestCode . rqPing , reqId ) ;
202- console . log ( 'header' , header ) ;
203187 var body = new Buffer ( 0 ) ;
204188 this . _request ( header , body ) ;
205189 this . commandsQueue . push ( [ tarantoolConstants . RequestCode . rqPing , reqId , dfd ] ) ;
@@ -210,7 +194,6 @@ TarantoolConnection.prototype.select = function(spaceId, indexId, limit, offset,
210194 var dfd = vow . defer ( ) ;
211195 var reqId = requestId . getId ( ) ;
212196 var header = this . _header ( tarantoolConstants . RequestCode . rqSelect , reqId ) ;
213- console . log ( 'header' , header ) ;
214197 var buffered = {
215198 spaceId : msgpack . pack ( spaceId ) ,
216199 indexId : msgpack . pack ( indexId ) ,
@@ -226,8 +209,6 @@ TarantoolConnection.prototype.select = function(spaceId, indexId, limit, offset,
226209 tarantoolConstants . KeysCode . key ] ) ,
227210 buffered . key
228211 ] ) ;
229- console . log ( body , msgpack . unpack ( body ) ) ;
230- console . log ( buffered ) ;
231212 this . _request ( header , body ) ;
232213 this . commandsQueue . push ( [ tarantoolConstants . RequestCode . rqSelect , reqId , dfd ] ) ;
233214 return dfd . promise ( ) ;
@@ -237,7 +218,6 @@ TarantoolConnection.prototype.delete = function(spaceId, indexId, key, limit){
237218 var dfd = vow . defer ( ) ;
238219 var reqId = requestId . getId ( ) ;
239220 var header = this . _header ( tarantoolConstants . RequestCode . rqDelete , reqId ) ;
240- console . log ( 'header' , header ) ;
241221 var buffered = {
242222 spaceId : msgpack . pack ( spaceId ) ,
243223 indexId : msgpack . pack ( indexId ) ,
@@ -246,8 +226,6 @@ TarantoolConnection.prototype.delete = function(spaceId, indexId, key, limit){
246226 var body = Buffer . concat ( [ new Buffer ( [ 0x86 , tarantoolConstants . KeysCode . space_id ] ) , buffered . spaceId ,
247227 new Buffer ( [ tarantoolConstants . KeysCode . index_id ] ) , buffered . indexId ,
248228 new Buffer ( [ tarantoolConstants . KeysCode . key ] ) , buffered . key ] ) ;
249- console . log ( body , msgpack . unpack ( body ) ) ;
250- console . log ( buffered ) ;
251229 this . _request ( header , body ) ;
252230 this . commandsQueue . push ( [ tarantoolConstants . RequestCode . rqSelect , reqId , dfd ] ) ;
253231 return dfd . promise ( ) ;
@@ -258,7 +236,6 @@ TarantoolConnection.prototype.update = function(spaceId, indexId, key, ops){
258236 if ( Array . isArray ( ops ) ) {
259237 var reqId = requestId . getId ( ) ;
260238 var header = this . _header ( tarantoolConstants . RequestCode . rqUpdate , reqId ) ;
261- console . log ( 'header' , header ) ;
262239 var buffered = {
263240 spaceId : msgpack . pack ( spaceId ) ,
264241 indexId : msgpack . pack ( indexId ) ,
@@ -269,8 +246,6 @@ TarantoolConnection.prototype.update = function(spaceId, indexId, key, ops){
269246 new Buffer ( [ tarantoolConstants . KeysCode . index_id ] ) , buffered . indexId ,
270247 new Buffer ( [ tarantoolConstants . KeysCode . key ] ) , buffered . key ,
271248 new Buffer ( [ tarantoolConstants . KeysCode . tuple ] ) , buffered . ops ] ) ;
272- console . log ( 'update body' , body , msgpack . unpack ( body ) ) ;
273- console . log ( buffered ) ;
274249 this . _request ( header , body ) ;
275250 this . commandsQueue . push ( [ tarantoolConstants . RequestCode . rqUpdate , reqId , dfd ] ) ;
276251 }
@@ -290,8 +265,6 @@ TarantoolConnection.prototype.eval = function(expression, tuple){
290265 var body = Buffer . concat ( [ new Buffer ( [ 0x82 , tarantoolConstants . KeysCode . expression ] ) , buffered . expression ,
291266 new Buffer ( [ tarantoolConstants . KeysCode . tuple ] ) , buffered . tuple ] ) ;
292267 this . _request ( header , body ) ;
293- console . log ( 'eval body' , msgpack . unpack ( body ) ) ;
294- console . log ( 'eval hrader' , msgpack . unpack ( header ) ) ;
295268 this . commandsQueue . push ( [ tarantoolConstants . RequestCode . rqEval , reqId , dfd ] ) ;
296269 return dfd . promise ( ) ;
297270} ;
@@ -327,15 +300,12 @@ TarantoolConnection.prototype._replaceInsert = function(cmd, reqId, spaceId, tup
327300 var dfd = vow . defer ( ) ;
328301 if ( Array . isArray ( tuple ) ) {
329302 var header = this . _header ( cmd , reqId ) ;
330- console . log ( 'header' , header ) ;
331303 var buffered = {
332304 spaceId : msgpack . pack ( spaceId ) ,
333305 tuple : msgpack . pack ( tuple )
334306 } ;
335307 var body = Buffer . concat ( [ new Buffer ( [ 0x82 , tarantoolConstants . KeysCode . space_id ] ) , buffered . spaceId ,
336308 new Buffer ( [ tarantoolConstants . KeysCode . tuple ] ) , buffered . tuple ] ) ;
337- console . log ( body , msgpack . unpack ( body ) ) ;
338- console . log ( buffered ) ;
339309 this . _request ( header , body ) ;
340310 this . commandsQueue . push ( [ cmd , reqId , dfd ] ) ;
341311 }
@@ -345,65 +315,43 @@ TarantoolConnection.prototype._replaceInsert = function(cmd, reqId, spaceId, tup
345315} ;
346316
347317TarantoolConnection . prototype . auth = function ( username , password ) {
348- try {
349- var dfd = vow . defer ( ) ;
350- console . log ( 'auth' ) ;
351- var reqId = requestId . getId ( ) ;
352- var header = this . _header ( tarantoolConstants . RequestCode . rqAuth , reqId ) ;
353- var buffered = {
354- username : msgpack . pack ( username )
355- } ;
356- var scrambled = scramble ( password , this . salt ) ;
357- var body = Buffer . concat ( [ new Buffer ( [ 0x82 , tarantoolConstants . KeysCode . username ] ) , buffered . username ,
358- new Buffer ( [ 0x21 , 0x92 ] ) , tarantoolConstants . passEnter , new Buffer ( [ 0xb4 ] ) , scrambled ] ) ;
359- this . _request ( header , body ) ;
360- this . commandsQueue . push ( [ tarantoolConstants . RequestCode . rqAuth , reqId , dfd ] ) ;
361- return dfd . promise ( ) ;
362- } catch ( e ) {
363- console . log ( e , e . stack ) ;
364- }
365-
318+ var dfd = vow . defer ( ) ;
319+ var reqId = requestId . getId ( ) ;
320+ var header = this . _header ( tarantoolConstants . RequestCode . rqAuth , reqId ) ;
321+ var buffered = {
322+ username : msgpack . pack ( username )
323+ } ;
324+ var scrambled = scramble ( password , this . salt ) ;
325+ var body = Buffer . concat ( [ new Buffer ( [ 0x82 , tarantoolConstants . KeysCode . username ] ) , buffered . username ,
326+ new Buffer ( [ 0x21 , 0x92 ] ) , tarantoolConstants . passEnter , new Buffer ( [ 0xb4 ] ) , scrambled ] ) ;
327+ this . _request ( header , body ) ;
328+ this . commandsQueue . push ( [ tarantoolConstants . RequestCode . rqAuth , reqId , dfd ] ) ;
329+ return dfd . promise ( ) ;
366330} ;
367331
368332function scramble ( password , salt ) {
369333 var encSalt = new Buffer ( salt , 'base64' ) ;
370- console . log ( 'encSalt' , encSalt , encSalt . toString ( ) , encSalt . length ) ;
371334 var step1 = shatransform ( password ) ;
372- console . log ( 'step1' , step1 , step1 . length ) ;
373335 var step2 = shatransform ( step1 ) ;
374336 var step3 = shatransform ( Buffer . concat ( [ encSalt . slice ( 0 , 20 ) , step2 ] ) ) ;
375- console . log ( password , step1 , step2 , step3 , salt ) ;
376- console . log ( 'salt' , salt ) ;
377337 var scramble = xor ( step1 , step3 ) ;
378- console . log ( 'scramble' , step1 . length , step2 . length , scramble , scramble . length ) ;
379338 return scramble ;
380339}
381340
382-
383341TarantoolConnection . prototype . _header = function ( command , reqId ) {
384- try {
385- var header = new Buffer ( [ 0x82 , tarantoolConstants . KeysCode . code , command ,
386- tarantoolConstants . KeysCode . sync , 0xce , 0 , 0 , 0 , 0 ] ) ;
387- header . writeUIntBE ( reqId , 5 , 4 ) ;
388- return header ;
389- } catch ( e ) {
390- console . log ( e , e . stack ) ;
391- }
342+ var header = new Buffer ( [ 0x82 , tarantoolConstants . KeysCode . code , command ,
343+ tarantoolConstants . KeysCode . sync , 0xce , 0 , 0 , 0 , 0 ] ) ;
344+ header . writeUIntBE ( reqId , 5 , 4 ) ;
345+ return header ;
392346} ;
393347
394348TarantoolConnection . prototype . _request = function ( header , body ) {
395- console . log ( 'start request' ) ;
396349 var sumL = header . length + body . length ;
397350 var prefixSizeBuffer = new Buffer ( 5 ) ;
398351 prefixSizeBuffer [ 0 ] = 0xCE ;
399352 prefixSizeBuffer . writeUIntBE ( sumL , 1 , 4 ) ;
400- try {
401- var buffer = Buffer . concat ( [ prefixSizeBuffer , header , body ] ) ;
402- console . log ( buffer ) ;
403- this . socket . write ( buffer ) ;
404- } catch ( e ) {
405- console . log ( e , e . stack ) ;
406- }
353+ var buffer = Buffer . concat ( [ prefixSizeBuffer , header , body ] ) ;
354+ this . socket . write ( buffer ) ;
407355} ;
408356
409357TarantoolConnection . prototype . destroy = function ( interupt ) {
@@ -417,52 +365,4 @@ TarantoolConnection.prototype.destroy = function(interupt){
417365 }
418366} ;
419367
420- //@InputType (Buffer)
421- function TarantoolResponse ( buffer ) {
422- this . ended = false ;
423- this . currentBufferLength = 0 ;
424- var length = buffer . readUIntBE ( 1 , 4 ) ;
425- console . log ( 'len' , length ) ;
426- this . buffer = buffer . slice ( 5 ) ;
427- if ( this . buffer . length >= length )
428- {
429- this . ended = true ;
430- if ( this . buffer . length > length ) {
431- var plusResponse = new TarantoolResponse ( this . buffer . slice ( length ) ) ;
432- if ( plusResponse . ended )
433- {
434-
435- }
436- this . buffer = this . buffer . slice ( 0 , length ) ;
437- }
438- this . processing ( ) ;
439- }
440- }
441-
442- TarantoolResponse . prototype . add = function ( buffer ) {
443- if ( this . ended )
444- {
445-
446- }
447- } ;
448-
449- TarantoolResponse . prototype . processing = function ( ) {
450- try {
451- this . success = this . buffer [ 6 ] == 0x00 ;
452- console . log ( this . buffer ) ;
453- if ( this . success ) {
454- // add fixarraymap with 2 objects before main object
455- var dataBuffer = Buffer . concat ( [ new Buffer ( [ 0x92 ] ) , this . buffer . slice ( 5 ) ] ) ;
456- console . log ( dataBuffer ) ;
457- var obj = msgpack . unpack ( dataBuffer ) ;
458- console . log ( obj ) ;
459- }
460- else {
461- console . log ( 'its error' ) ;
462- }
463- } catch ( e ) {
464- console . log ( e , e . stack ) ;
465- }
466- } ;
467-
468368module . exports = TarantoolConnection ;
0 commit comments