@@ -208,7 +208,6 @@ TarantoolConnection.prototype.ping = function(){
208208
209209TarantoolConnection . prototype . select = function ( spaceId , indexId , limit , offset , iterator , key ) {
210210 var dfd = vow . defer ( ) ;
211- console . log ( 'start ping' ) ;
212211 var reqId = requestId . getId ( ) ;
213212 var header = this . _header ( tarantoolConstants . RequestCode . rqSelect , reqId ) ;
214213 console . log ( 'header' , header ) ;
@@ -234,13 +233,100 @@ TarantoolConnection.prototype.select = function(spaceId, indexId, limit, offset,
234233 return dfd . promise ( ) ;
235234} ;
236235
236+ TarantoolConnection . prototype . delete = function ( spaceId , indexId , key , limit ) {
237+ var dfd = vow . defer ( ) ;
238+ var reqId = requestId . getId ( ) ;
239+ var header = this . _header ( tarantoolConstants . RequestCode . rqDelete , reqId ) ;
240+ console . log ( 'header' , header ) ;
241+ var buffered = {
242+ spaceId : msgpack . pack ( spaceId ) ,
243+ indexId : msgpack . pack ( indexId ) ,
244+ key : msgpack . pack ( key )
245+ } ;
246+ var body = Buffer . concat ( [ new Buffer ( [ 0x86 , tarantoolConstants . KeysCode . space_id ] ) , buffered . spaceId ,
247+ new Buffer ( [ tarantoolConstants . KeysCode . index_id ] ) , buffered . indexId ,
248+ new Buffer ( [ tarantoolConstants . KeysCode . key ] ) , buffered . key ] ) ;
249+ console . log ( body , msgpack . unpack ( body ) ) ;
250+ console . log ( buffered ) ;
251+ this . _request ( header , body ) ;
252+ this . commandsQueue . push ( [ tarantoolConstants . RequestCode . rqSelect , reqId , dfd ] ) ;
253+ return dfd . promise ( ) ;
254+ } ;
255+
256+ TarantoolConnection . prototype . update = function ( spaceId , indexId , key , ops ) {
257+ var dfd = vow . defer ( ) ;
258+ if ( Array . isArray ( ops ) ) {
259+ var reqId = requestId . getId ( ) ;
260+ var header = this . _header ( tarantoolConstants . RequestCode . rqUpdate , reqId ) ;
261+ console . log ( 'header' , header ) ;
262+ var buffered = {
263+ spaceId : msgpack . pack ( spaceId ) ,
264+ indexId : msgpack . pack ( indexId ) ,
265+ ops : msgpack . pack ( ops ) ,
266+ key : msgpack . pack ( key )
267+ } ;
268+ var body = Buffer . concat ( [ new Buffer ( [ 0x84 , tarantoolConstants . KeysCode . space_id ] ) , buffered . spaceId ,
269+ new Buffer ( [ tarantoolConstants . KeysCode . index_id ] ) , buffered . indexId ,
270+ new Buffer ( [ tarantoolConstants . KeysCode . key ] ) , buffered . key ,
271+ new Buffer ( [ tarantoolConstants . KeysCode . tuple ] ) , buffered . ops ] ) ;
272+ console . log ( 'update body' , body , msgpack . unpack ( body ) ) ;
273+ console . log ( buffered ) ;
274+ this . _request ( header , body ) ;
275+ this . commandsQueue . push ( [ tarantoolConstants . RequestCode . rqUpdate , reqId , dfd ] ) ;
276+ }
277+ else
278+ dfd . reject ( new Error ( 'need array' ) ) ;
279+ return dfd . promise ( ) ;
280+ } ;
281+
282+ TarantoolConnection . prototype . eval = function ( expression , tuple ) {
283+ var dfd = vow . defer ( ) ;
284+ var reqId = requestId . getId ( ) ;
285+ var header = this . _header ( tarantoolConstants . RequestCode . rqEval , reqId ) ;
286+ var buffered = {
287+ expression : msgpack . pack ( expression ) ,
288+ tuple : msgpack . pack ( tuple )
289+ } ;
290+ var body = Buffer . concat ( [ new Buffer ( [ 0x82 , tarantoolConstants . KeysCode . expression ] ) , buffered . expression ,
291+ new Buffer ( [ tarantoolConstants . KeysCode . tuple ] ) , buffered . tuple ] ) ;
292+ this . _request ( header , body ) ;
293+ console . log ( 'eval body' , msgpack . unpack ( body ) ) ;
294+ console . log ( 'eval hrader' , msgpack . unpack ( header ) ) ;
295+ this . commandsQueue . push ( [ tarantoolConstants . RequestCode . rqEval , reqId , dfd ] ) ;
296+ return dfd . promise ( ) ;
297+ } ;
298+
299+
300+ TarantoolConnection . prototype . call = function ( functionName , tuple ) {
301+ var dfd = vow . defer ( ) ;
302+ var reqId = requestId . getId ( ) ;
303+ var header = this . _header ( tarantoolConstants . RequestCode . rqCall , reqId ) ;
304+ var buffered = {
305+ functionName : msgpack . pack ( functionName ) ,
306+ tuple : msgpack . pack ( tuple )
307+ } ;
308+ var body = Buffer . concat ( [ new Buffer ( [ 0x82 , tarantoolConstants . KeysCode . function_name ] ) , buffered . functionName ,
309+ new Buffer ( [ tarantoolConstants . KeysCode . tuple ] ) , buffered . tuple ] ) ;
310+ this . _request ( header , body ) ;
311+ this . commandsQueue . push ( [ tarantoolConstants . RequestCode . rqCall , reqId , dfd ] ) ;
312+ return dfd . promise ( ) ;
313+ } ;
314+
237315
238316TarantoolConnection . prototype . insert = function ( spaceId , tuple ) {
317+ var reqId = requestId . getId ( ) ;
318+ return this . _replaceInsert ( tarantoolConstants . RequestCode . rqInsert , reqId , spaceId , tuple ) ;
319+ } ;
320+
321+ TarantoolConnection . prototype . replace = function ( spaceId , tuple ) {
322+ var reqId = requestId . getId ( ) ;
323+ return this . _replaceInsert ( tarantoolConstants . RequestCode . rqReplace , reqId , spaceId , tuple ) ;
324+ } ;
325+
326+ TarantoolConnection . prototype . _replaceInsert = function ( cmd , reqId , spaceId , tuple ) {
239327 var dfd = vow . defer ( ) ;
240328 if ( Array . isArray ( tuple ) ) {
241- console . log ( 'start ping' ) ;
242- var reqId = requestId . getId ( ) ;
243- var header = this . _header ( tarantoolConstants . RequestCode . rqInsert , reqId ) ;
329+ var header = this . _header ( cmd , reqId ) ;
244330 console . log ( 'header' , header ) ;
245331 var buffered = {
246332 spaceId : msgpack . pack ( spaceId ) ,
@@ -251,7 +337,7 @@ TarantoolConnection.prototype.insert = function(spaceId, tuple){
251337 console . log ( body , msgpack . unpack ( body ) ) ;
252338 console . log ( buffered ) ;
253339 this . _request ( header , body ) ;
254- this . commandsQueue . push ( [ tarantoolConstants . RequestCode . rqInsert , reqId , dfd ] ) ;
340+ this . commandsQueue . push ( [ cmd , reqId , dfd ] ) ;
255341 }
256342 else
257343 dfd . reject ( new Error ( 'need array' ) ) ;
@@ -267,42 +353,32 @@ TarantoolConnection.prototype.auth = function(username, password){
267353 var buffered = {
268354 username : msgpack . pack ( username )
269355 } ;
270-
356+ var scrambled = scramble ( password , this . salt ) ;
271357 var body = Buffer . concat ( [ new Buffer ( [ 0x82 , tarantoolConstants . KeysCode . username ] ) , buffered . username ,
272- new Buffer ( [ 0x21 , 0x92 ] ) , tarantoolConstants . passEnter ,
273- msgpack . pack ( scramble ( password , this . salt ) . toString ( 'ascii' ) ) ] ) ;
274- console . log ( body , msgpack . unpack ( body ) ) ;
275- console . log ( buffered , msgpack . pack ( scramble ( password , this . salt ) ) ) ;
358+ new Buffer ( [ 0x21 , 0x92 ] ) , tarantoolConstants . passEnter , new Buffer ( [ 0xb4 ] ) , scrambled ] ) ;
276359 this . _request ( header , body ) ;
277360 this . commandsQueue . push ( [ tarantoolConstants . RequestCode . rqAuth , reqId , dfd ] ) ;
278361 return dfd . promise ( ) ;
279362 } catch ( e ) {
280- console . log ( e ) ;
363+ console . log ( e , e . stack ) ;
281364 }
282365
283366} ;
284367
285368function scramble ( password , salt ) {
286- var encSalt = new Buffer ( salt , 'base64' ) . toString ( 'ascii' ) ;
287- console . log ( 'encSalt' , encSalt ) ;
369+ var encSalt = new Buffer ( salt , 'base64' ) ;
370+ console . log ( 'encSalt' , encSalt , encSalt . toString ( ) , encSalt . length ) ;
288371 var step1 = shatransform ( password ) ;
289372 console . log ( 'step1' , step1 , step1 . length ) ;
290373 var step2 = shatransform ( step1 ) ;
291- var step3 = shatransform ( salt , step2 ) ;
374+ var step3 = shatransform ( Buffer . concat ( [ encSalt . slice ( 0 , 20 ) , step2 ] ) ) ;
292375 console . log ( password , step1 , step2 , step3 , salt ) ;
293376 console . log ( 'salt' , salt ) ;
294- var scramble = xor ( step1 , step3 ) . toString ( 'ascii' ) ;
377+ var scramble = xor ( step1 , step3 ) ;
295378 console . log ( 'scramble' , step1 . length , step2 . length , scramble , scramble . length ) ;
296379 return scramble ;
297380}
298381
299- function myXor ( l , r ) {
300- var x = [ ] ;
301- for ( var i = 0 ; i < l . length ; i ++ )
302- x . push ( l [ i ] ^ r [ i ] ) ;
303- return new Buffer ( x ) ;
304- }
305-
306382
307383TarantoolConnection . prototype . _header = function ( command , reqId ) {
308384 try {
0 commit comments