Skip to content

Commit 4507244

Browse files
committed
0.4.0
1 parent 4690fca commit 4507244

File tree

5 files changed

+113
-31
lines changed

5 files changed

+113
-31
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ language: node_js
22
node_js:
33
- "0.12"
44
- "iojs"
5+
- "stable"
6+
- "4.1"
7+
- "4.0"
58
cache: false
69

710
before_script:

README.md

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,38 @@ conn.connect()
3535
});
3636
```
3737

38+
39+
##Msgpack implentation
40+
41+
You can yse any implementation that can be duck typing with next interface:
42+
43+
```
44+
45+
//msgpack implementation example
46+
/*
47+
@interface
48+
decode: (Buffer buf)
49+
encode: (Object obj)
50+
*/
51+
var exampleCustomMsgpack = {
52+
encode: function(obj){
53+
return yourmsgpack.encode(obj);
54+
},
55+
decode: function(buf){
56+
return yourmsgpack.decode(obj);
57+
}
58+
};
59+
```
60+
3861
##API
3962

4063
**class TarantoolConnection(options)**
4164
```
4265
var defaultOptions = {
4366
host: 'localhost',
4467
port: '3301',
45-
log: false
68+
log: false,
69+
msgpack: require('msgpack-lite')
4670
};
4771
```
4872
You can overrid default options with options.
@@ -89,12 +113,11 @@ Promise resolve a new tuple.
89113

90114
**upsert(spaceId: Number or String, key: tuple, ops: array of operations, tuple: tuple) : Promise()**
91115

92-
About operation: https://github.com/tarantool/tarantool/issues/905
116+
About operation: http://tarantool.org/doc/book/box/box_space.html#lua-function.space_object.upsert
93117

94118
Ops: http://tarantool.org/doc/book/box/box_space.html (search for update here).
95119

96120
Promise resolve nothing.
97-
Knowing issues if it cannot be updated by your ops it wouldn't return error just only at tarantool side but not at protocol. See https://github.com/tarantool/tarantool/issues/966
98121

99122
**replace(spaceId: Number or String, tuple: tuple) : Promise(Tuple)**
100123

@@ -149,6 +172,12 @@ It's ok you can do whatever you need. I add log options for some technical infor
149172

150173
##Changelog
151174

175+
###0.4.0
176+
177+
Change msgpack5 to msgpack-lite(thx to @arusakov).
178+
Add msgpack as option for connection.
179+
Bump msgpack5 for work at new version.
180+
152181
###0.3.0
153182
Add upsert operation.
154183
Key is now can be just a number.

lib/connection.js

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,25 @@ var tarantoolConstants = require('./const');
33
var net = require('net');
44
var _ = require('underscore');
55
var EventEmitter = require('events');
6-
var msgpack = require('msgpack5')();
6+
var msgpack = require('msgpack-lite');
77
var crypto = require('crypto');
88
var xor = require('bitwise-xor');
99

10+
//msgpack implementation example
11+
/*
12+
@interface
13+
decode: (Buffer buf)
14+
encode: (Object obj)
15+
*/
16+
var exampleCustomMsgpack = {
17+
encode: function(obj){
18+
return msgpack.encode(obj);
19+
},
20+
decode: function(buf){
21+
return msgpack.decode(obj);
22+
}
23+
};
24+
1025
var shatransform = function(t){
1126
return crypto.createHash('sha1').update(t).digest();
1227
};
@@ -41,6 +56,7 @@ function TarantoolConnection (options){
4156
readable: true,
4257
writable: true
4358
});
59+
this.msgpack = options.msgpack || msgpack;
4460
this.state = states.INITED;
4561
this.options = _.extend({}, defaultOptions, options);
4662
this.commandsQueue = [];
@@ -224,7 +240,7 @@ TarantoolConnection.prototype._responseBufferTrack = function(buffer, length){
224240
TarantoolConnection.prototype._processResponse = function(buffer){
225241
// add fixarraymap with 2 objects before main object
226242
var dataBuffer = Buffer.concat([new Buffer([0x92]), buffer]);
227-
var obj = msgpack.decode(dataBuffer);
243+
var obj = this.msgpack.decode(dataBuffer);
228244

229245
var reqId = obj[0][1];
230246
for(var i = 0; i<this.commandsQueue.length; i++)
@@ -330,11 +346,11 @@ TarantoolConnection.prototype.select = function(spaceId, indexId, limit, offset,
330346
if (iterator == 'all')
331347
key = [];
332348
var buffered = {
333-
spaceId: msgpack.encode(spaceId),
334-
indexId: msgpack.encode(indexId),
335-
limit: msgpack.encode(limit),
336-
offset: msgpack.encode(offset),
337-
key: msgpack.encode(key)
349+
spaceId: this.msgpack.encode(spaceId),
350+
indexId: this.msgpack.encode(indexId),
351+
limit: this.msgpack.encode(limit),
352+
offset: this.msgpack.encode(offset),
353+
key: this.msgpack.encode(key)
338354
};
339355
var body = Buffer.concat([new Buffer([0x86,tarantoolConstants.KeysCode.space_id]), buffered.spaceId,
340356
new Buffer([tarantoolConstants.KeysCode.index_id]), buffered.indexId,
@@ -367,9 +383,9 @@ TarantoolConnection.prototype.delete = function(spaceId, indexId, key){
367383
var reqId = requestId.getId();
368384
var header = this._header(tarantoolConstants.RequestCode.rqDelete, reqId);
369385
var buffered = {
370-
spaceId: msgpack.encode(spaceId),
371-
indexId: msgpack.encode(indexId),
372-
key: msgpack.encode(key)
386+
spaceId: this.msgpack.encode(spaceId),
387+
indexId: this.msgpack.encode(indexId),
388+
key: this.msgpack.encode(key)
373389
};
374390
var body = Buffer.concat([new Buffer([0x86,tarantoolConstants.KeysCode.space_id]), buffered.spaceId,
375391
new Buffer([tarantoolConstants.KeysCode.index_id]), buffered.indexId,
@@ -398,11 +414,12 @@ TarantoolConnection.prototype.update = function(spaceId, indexId, key, ops){
398414
}
399415
var reqId = requestId.getId();
400416
var header = this._header(tarantoolConstants.RequestCode.rqUpdate, reqId);
417+
401418
var buffered = {
402-
spaceId: msgpack.encode(spaceId),
403-
indexId: msgpack.encode(indexId),
404-
ops: msgpack.encode(ops),
405-
key: msgpack.encode(key)
419+
spaceId: this.msgpack.encode(spaceId),
420+
indexId: this.msgpack.encode(indexId),
421+
ops: this.msgpack.encode(ops),
422+
key: this.msgpack.encode(key)
406423
};
407424
var body = Buffer.concat([new Buffer([0x84,tarantoolConstants.KeysCode.space_id]), buffered.spaceId,
408425
new Buffer([tarantoolConstants.KeysCode.index_id]), buffered.indexId,
@@ -435,11 +452,11 @@ TarantoolConnection.prototype.upsert = function(spaceId, key, ops, tuple){
435452
var reqId = requestId.getId();
436453
var header = this._header(tarantoolConstants.RequestCode.rqUpsert, reqId);
437454
var buffered = {
438-
spaceId: msgpack.encode(spaceId),
455+
spaceId: this.msgpack.encode(spaceId),
439456
//indexId: msgpack.encode(indexId),
440-
ops: msgpack.encode(ops),
441-
key: msgpack.encode(key),
442-
tuple: msgpack.encode(tuple)
457+
ops: this.msgpack.encode(ops),
458+
key: this.msgpack.encode(key),
459+
tuple: this.msgpack.encode(tuple)
443460
};
444461
var body = Buffer.concat([new Buffer([0x84,tarantoolConstants.KeysCode.space_id]), buffered.spaceId,
445462
//new Buffer([tarantoolConstants.KeysCode.index_id]), buffered.indexId,
@@ -463,8 +480,8 @@ TarantoolConnection.prototype.eval = function(expression){
463480
var reqId = requestId.getId();
464481
var header = this._header(tarantoolConstants.RequestCode.rqEval, reqId);
465482
var buffered = {
466-
expression: msgpack.encode(expression),
467-
tuple: msgpack.encode(tuple)
483+
expression: this.msgpack.encode(expression),
484+
tuple: this.msgpack.encode(tuple)
468485
};
469486
var body = Buffer.concat([new Buffer([0x82,tarantoolConstants.KeysCode.expression]), buffered.expression,
470487
new Buffer([tarantoolConstants.KeysCode.tuple]), buffered.tuple]);
@@ -479,8 +496,8 @@ TarantoolConnection.prototype.call = function(functionName){
479496
var reqId = requestId.getId();
480497
var header = this._header(tarantoolConstants.RequestCode.rqCall, reqId);
481498
var buffered = {
482-
functionName: msgpack.encode(functionName),
483-
tuple: msgpack.encode(tuple ? tuple : [])
499+
functionName: this.msgpack.encode(functionName),
500+
tuple: this.msgpack.encode(tuple ? tuple : [])
484501
};
485502
var body = Buffer.concat([new Buffer([0x82,tarantoolConstants.KeysCode.function_name]), buffered.functionName,
486503
new Buffer([tarantoolConstants.KeysCode.tuple]), buffered.tuple]);
@@ -513,8 +530,8 @@ TarantoolConnection.prototype._replaceInsert = function(cmd, reqId, spaceId, tup
513530
}
514531
var header = this._header(cmd, reqId);
515532
var buffered = {
516-
spaceId: msgpack.encode(spaceId),
517-
tuple: msgpack.encode(tuple)
533+
spaceId: this.msgpack.encode(spaceId),
534+
tuple: this.msgpack.encode(tuple)
518535
};
519536
var body = Buffer.concat([new Buffer([0x82,tarantoolConstants.KeysCode.space_id]), buffered.spaceId,
520537
new Buffer([tarantoolConstants.KeysCode.tuple]), buffered.tuple]);
@@ -531,7 +548,7 @@ TarantoolConnection.prototype.auth = function(username, password){
531548
var reqId = requestId.getId();
532549
var header = this._header(tarantoolConstants.RequestCode.rqAuth, reqId);
533550
var buffered = {
534-
username: msgpack.encode(username)
551+
username: this.msgpack.encode(username)
535552
};
536553
var scrambled = scramble(password, this.salt);
537554
var body = Buffer.concat([new Buffer([0x82, tarantoolConstants.KeysCode.username]), buffered.username,

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tarantool-driver",
3-
"version": "0.3.0",
3+
"version": "0.4.0",
44
"description": "Tarantool driver for 1.6",
55
"main": "index.js",
66
"scripts": {
@@ -24,10 +24,12 @@
2424
"homepage": "https://github.com/KlonD90/node-tarantool-driver",
2525
"dependencies": {
2626
"bitwise-xor": "0.0.0",
27-
"msgpack5": "^3.0.0",
27+
"msgpack-lite": "^0.1.15",
28+
"msgpack5": "^3.3.0",
2829
"underscore": "^1.8.3"
2930
},
3031
"devDependencies": {
32+
"benchmark": "^2.1.0",
3133
"mocha": "^2.2.4"
3234
}
3335
}

test/app.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
var fs = require('fs');
55
var assert = require('assert');
66
var TarantoolConnection = require('../lib/connection');
7+
var mlite = require('msgpack-lite');
78

89
describe('Tarantool Connection tests', function(){
910
before(function(){
@@ -167,7 +168,6 @@ describe('Tarantool Connection tests', function(){
167168
return conn.call('myget', 4)
168169
})
169170
.then(function(value){
170-
console.log('value', value);
171171
done()
172172
})
173173
.catch(function(e){
@@ -297,4 +297,35 @@ describe('Tarantool Connection tests', function(){
297297
})
298298
});
299299
});
300+
describe('connection test with custom msgpack implementation', function(){
301+
var customConn;
302+
beforeEach(function(){
303+
customConn = new TarantoolConnection(
304+
{
305+
port: 33013,
306+
msgpack: {
307+
encode: function(obj){
308+
return mlite.encode(obj);
309+
},
310+
decode: function(buf){
311+
return mlite.decode(buf);
312+
}
313+
}
314+
}
315+
);
316+
});
317+
it('connect', function(done){
318+
customConn.connect().then(function(){
319+
done();
320+
}, function(e){ throw 'not connected'; done();});
321+
});
322+
it('auth', function(done){
323+
customConn.connect().then(function(){
324+
return customConn.auth('test', 'test');
325+
}, function(e){ throw 'not connected'; done();})
326+
.then(function(){
327+
done();
328+
}, function(e){ throw 'not auth'; done();})
329+
});
330+
});
300331
});

0 commit comments

Comments
 (0)