mirror of
https://github.com/valitydev/thrift.git
synced 2024-11-06 18:35:19 +00:00
THRIFT-2511 Node.js compact protocol
Client: Node Patch: Randy Abernethy Adds Compact Protocol to Node.js, tests in testAll.sh and repairs all library JSHint warnings.
This commit is contained in:
parent
facc8dc6c7
commit
20aeba3e38
@ -17,18 +17,18 @@
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
var POW_8 = Math.pow(2, 8)
|
||||
var POW_16 = Math.pow(2, 16)
|
||||
var POW_24 = Math.pow(2, 24)
|
||||
var POW_32 = Math.pow(2, 32)
|
||||
var POW_40 = Math.pow(2, 40)
|
||||
var POW_48 = Math.pow(2, 48)
|
||||
var POW_52 = Math.pow(2, 52)
|
||||
var POW_1022 = Math.pow(2, 1022)
|
||||
var POW_8 = Math.pow(2, 8);
|
||||
var POW_16 = Math.pow(2, 16);
|
||||
var POW_24 = Math.pow(2, 24);
|
||||
var POW_32 = Math.pow(2, 32);
|
||||
var POW_40 = Math.pow(2, 40);
|
||||
var POW_48 = Math.pow(2, 48);
|
||||
var POW_52 = Math.pow(2, 52);
|
||||
var POW_1022 = Math.pow(2, 1022);
|
||||
|
||||
exports.readByte = function(byte){
|
||||
return byte > 127 ? byte-256 : byte;
|
||||
}
|
||||
exports.readByte = function(b){
|
||||
return b > 127 ? b-256 : b;
|
||||
};
|
||||
|
||||
exports.readI16 = function(buff, off) {
|
||||
off = off || 0;
|
||||
@ -38,7 +38,7 @@ exports.readI16 = function(buff, off) {
|
||||
v -= POW_16;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
};
|
||||
|
||||
exports.readI32 = function(buff, off) {
|
||||
off = off || 0;
|
||||
@ -50,14 +50,14 @@ exports.readI32 = function(buff, off) {
|
||||
v -= POW_32;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
};
|
||||
|
||||
exports.writeI16 = function(buff, v) {
|
||||
buff[1] = v & 0xff;
|
||||
v >>= 8;
|
||||
buff[0] = v & 0xff;
|
||||
return buff;
|
||||
}
|
||||
};
|
||||
|
||||
exports.writeI32 = function(buff, v) {
|
||||
buff[3] = v & 0xff;
|
||||
@ -68,7 +68,7 @@ exports.writeI32 = function(buff, v) {
|
||||
v >>= 8;
|
||||
buff[0] = v & 0xff;
|
||||
return buff;
|
||||
}
|
||||
};
|
||||
|
||||
exports.readDouble = function(buff, off) {
|
||||
off = off || 0;
|
||||
@ -86,7 +86,7 @@ exports.readDouble = function(buff, off) {
|
||||
|
||||
switch (e) {
|
||||
case 0:
|
||||
e = -1022
|
||||
e = -1022;
|
||||
break;
|
||||
case 2047:
|
||||
return m ? NaN : (signed ? -Infinity : Infinity);
|
||||
@ -100,7 +100,7 @@ exports.readDouble = function(buff, off) {
|
||||
}
|
||||
|
||||
return m * Math.pow(2, e - 52);
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
* Based on code from the jspack module:
|
||||
@ -109,9 +109,9 @@ exports.readDouble = function(buff, off) {
|
||||
exports.writeDouble = function(buff, v) {
|
||||
var m, e, c;
|
||||
|
||||
buff[0] = (v < 0 ? 0x80 : 0x00)
|
||||
buff[0] = (v < 0 ? 0x80 : 0x00);
|
||||
|
||||
v = Math.abs(v)
|
||||
v = Math.abs(v);
|
||||
if (v !== v) {
|
||||
// NaN, use QNaN IEEE format
|
||||
m = 2251799813685248;
|
||||
@ -120,8 +120,8 @@ exports.writeDouble = function(buff, v) {
|
||||
m = 0;
|
||||
e = 2047;
|
||||
} else {
|
||||
e = Math.floor(Math.log(v) / Math.LN2)
|
||||
c = Math.pow(2, -e)
|
||||
e = Math.floor(Math.log(v) / Math.LN2);
|
||||
c = Math.pow(2, -e);
|
||||
if (v * c < 1) {
|
||||
e--;
|
||||
c *= 2;
|
||||
@ -165,4 +165,4 @@ exports.writeDouble = function(buff, v) {
|
||||
buff[1] |= m & 0x0f;
|
||||
|
||||
return buff;
|
||||
}
|
||||
};
|
||||
|
@ -54,3 +54,4 @@ exports.TFramedTransport = require('./transport').TFramedTransport;
|
||||
exports.TBufferedTransport = require('./transport').TBufferedTransport;
|
||||
exports.TBinaryProtocol = require('./protocol').TBinaryProtocol;
|
||||
exports.TJSONProtocol = require('./protocol').TJSONProtocol;
|
||||
exports.TCompactProtocol = require('./protocol').TCompactProtocol;
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -36,19 +36,19 @@ var Type = exports.Type = {
|
||||
LIST: 15,
|
||||
UTF8: 16,
|
||||
UTF16: 17,
|
||||
}
|
||||
};
|
||||
|
||||
exports.MessageType = {
|
||||
CALL: 1,
|
||||
REPLY: 2,
|
||||
EXCEPTION: 3,
|
||||
ONEWAY: 4,
|
||||
}
|
||||
};
|
||||
|
||||
var TException = exports.TException = function(message) {
|
||||
Error.call(this, message);
|
||||
this.name = 'TException';
|
||||
}
|
||||
};
|
||||
util.inherits(TException, Error);
|
||||
|
||||
var TApplicationExceptionType = exports.TApplicationExceptionType = {
|
||||
@ -63,81 +63,73 @@ var TApplicationExceptionType = exports.TApplicationExceptionType = {
|
||||
INVALID_TRANSFORM: 8,
|
||||
INVALID_PROTOCOL: 9,
|
||||
UNSUPPORTED_CLIENT_TYPE: 10
|
||||
}
|
||||
};
|
||||
|
||||
var TApplicationException = exports.TApplicationException = function(type, message) {
|
||||
TException.call(this, message);
|
||||
this.type = type || TApplicationExceptionType.UNKNOWN;
|
||||
this.name = 'TApplicationException';
|
||||
}
|
||||
};
|
||||
util.inherits(TApplicationException, TException);
|
||||
|
||||
TApplicationException.prototype.read = function(input) {
|
||||
var ftype
|
||||
var fid
|
||||
var ret = input.readStructBegin('TApplicationException')
|
||||
var ftype;
|
||||
var ret = input.readStructBegin('TApplicationException');
|
||||
|
||||
while(1){
|
||||
|
||||
ret = input.readFieldBegin()
|
||||
|
||||
ret = input.readFieldBegin();
|
||||
if(ret.ftype == Type.STOP)
|
||||
break
|
||||
break;
|
||||
|
||||
var fid = ret.fid
|
||||
|
||||
switch(fid){
|
||||
switch(ret.fid){
|
||||
case 1:
|
||||
if( ret.ftype == Type.STRING ){
|
||||
ret = input.readString()
|
||||
this.message = ret
|
||||
ret = input.readString();
|
||||
this.message = ret;
|
||||
} else {
|
||||
ret = input.skip(ret.ftype)
|
||||
ret = input.skip(ret.ftype);
|
||||
}
|
||||
|
||||
break
|
||||
break;
|
||||
case 2:
|
||||
if( ret.ftype == Type.I32 ){
|
||||
ret = input.readI32()
|
||||
this.type = ret
|
||||
ret = input.readI32();
|
||||
this.type = ret;
|
||||
} else {
|
||||
ret = input.skip(ret.ftype)
|
||||
ret = input.skip(ret.ftype);
|
||||
}
|
||||
break
|
||||
|
||||
break;
|
||||
default:
|
||||
ret = input.skip(ret.ftype)
|
||||
break
|
||||
ret = input.skip(ret.ftype);
|
||||
break;
|
||||
}
|
||||
input.readFieldEnd()
|
||||
input.readFieldEnd();
|
||||
}
|
||||
|
||||
input.readStructEnd()
|
||||
}
|
||||
input.readStructEnd();
|
||||
};
|
||||
|
||||
TApplicationException.prototype.write = function(output){
|
||||
output.writeStructBegin('TApplicationException');
|
||||
|
||||
if (this.message) {
|
||||
output.writeFieldBegin('message', Type.STRING, 1)
|
||||
output.writeString(this.message)
|
||||
output.writeFieldEnd()
|
||||
output.writeFieldBegin('message', Type.STRING, 1);
|
||||
output.writeString(this.message);
|
||||
output.writeFieldEnd();
|
||||
}
|
||||
|
||||
if (this.code) {
|
||||
output.writeFieldBegin('type', Type.I32, 2)
|
||||
output.writeI32(this.code)
|
||||
output.writeFieldEnd()
|
||||
output.writeFieldBegin('type', Type.I32, 2);
|
||||
output.writeI32(this.code);
|
||||
output.writeFieldEnd();
|
||||
}
|
||||
|
||||
output.writeFieldStop()
|
||||
output.writeStructEnd()
|
||||
}
|
||||
output.writeFieldStop();
|
||||
output.writeStructEnd();
|
||||
};
|
||||
|
||||
exports.objectLength = function(obj) {
|
||||
return Object.keys(obj).length;
|
||||
}
|
||||
};
|
||||
|
||||
exports.inherits = function(constructor, superConstructor) {
|
||||
util.inherits(constructor, superConstructor);
|
||||
}
|
||||
};
|
||||
|
@ -169,7 +169,7 @@ TFramedTransport.prototype = {
|
||||
if (this.onFlush) {
|
||||
// TODO: optimize this better, allocate one buffer instead of both:
|
||||
var msg = new Buffer(out.length + 4);
|
||||
binary.writeI32(msg, out.length)
|
||||
binary.writeI32(msg, out.length);
|
||||
frameLeft = binary.readI32(this.inBuf, 0);
|
||||
out.copy(msg, 4, 0, out.length);
|
||||
this.onFlush(msg);
|
||||
@ -235,7 +235,7 @@ TBufferedTransport.prototype = {
|
||||
},
|
||||
|
||||
read: function(len) {
|
||||
this.ensureAvailable(len)
|
||||
this.ensureAvailable(len);
|
||||
var buf = new Buffer(len);
|
||||
this.inBuf.copy(buf, 0, this.readCursor, this.readCursor + len);
|
||||
this.readCursor += len;
|
||||
@ -243,33 +243,33 @@ TBufferedTransport.prototype = {
|
||||
},
|
||||
|
||||
readByte: function() {
|
||||
this.ensureAvailable(1)
|
||||
this.ensureAvailable(1);
|
||||
return binary.readByte(this.inBuf[this.readCursor++]);
|
||||
},
|
||||
|
||||
readI16: function() {
|
||||
this.ensureAvailable(2)
|
||||
this.ensureAvailable(2);
|
||||
var i16 = binary.readI16(this.inBuf, this.readCursor);
|
||||
this.readCursor += 2;
|
||||
return i16;
|
||||
},
|
||||
|
||||
readI32: function() {
|
||||
this.ensureAvailable(4)
|
||||
this.ensureAvailable(4);
|
||||
var i32 = binary.readI32(this.inBuf, this.readCursor);
|
||||
this.readCursor += 4;
|
||||
return i32;
|
||||
},
|
||||
|
||||
readDouble: function() {
|
||||
this.ensureAvailable(8)
|
||||
this.ensureAvailable(8);
|
||||
var d = binary.readDouble(this.inBuf, this.readCursor);
|
||||
this.readCursor += 8;
|
||||
return d;
|
||||
},
|
||||
|
||||
readString: function(len) {
|
||||
this.ensureAvailable(len)
|
||||
this.ensureAvailable(len);
|
||||
var str = this.inBuf.toString('utf8', this.readCursor, this.readCursor + len);
|
||||
this.readCursor += len;
|
||||
return str;
|
||||
|
@ -41,6 +41,8 @@ program
|
||||
var protocol = thrift.TBinaryProtocol;
|
||||
if (program.protocol === "json") {
|
||||
protocol = thrift.TJSONProtocol;
|
||||
} else if (program.protocol === "compact") {
|
||||
protocol = thrift.TCompactProtocol;
|
||||
}
|
||||
|
||||
var transport = thrift.TBufferedTransport;
|
||||
|
@ -41,6 +41,8 @@ if (program.transport === "framed") {
|
||||
var protocol = thrift.TBinaryProtocol;
|
||||
if (program.protocol === "json") {
|
||||
protocol = thrift.TJSONProtocol;
|
||||
} else if (program.protocol === "compact") {
|
||||
protocol = thrift.TCompactProtocol;
|
||||
}
|
||||
|
||||
var handler = ThriftTestHandler;
|
||||
|
@ -73,6 +73,8 @@ node ${DIR}/binary.test.js || TESTOK=1
|
||||
#integration tests
|
||||
|
||||
#TCP connection tests
|
||||
testClientServer compact buffered || TESTOK=1
|
||||
testClientServer compact framed || TESTOK=1
|
||||
testClientServer binary buffered || TESTOK=1
|
||||
testClientServer json buffered || TESTOK=1
|
||||
testClientServer binary framed || TESTOK=1
|
||||
@ -82,7 +84,7 @@ testClientServer json framed || TESTOK=1
|
||||
testMultiplexedClientServer binary buffered || TESTOK=1
|
||||
testMultiplexedClientServer json buffered || TESTOK=1
|
||||
testMultiplexedClientServer binary framed || TESTOK=1
|
||||
testMultiplexedClientServer json framed || TESTOK=1
|
||||
testMultiplexedClientServer compact framed || TESTOK=1
|
||||
|
||||
#test ssl connection
|
||||
testClientServer binary framed --ssl || TESTOK=1
|
||||
@ -90,8 +92,11 @@ testMultiplexedClientServer binary framed --ssl || TESTOK=1
|
||||
|
||||
#test promise style
|
||||
testClientServer binary framed --promise || TESTOK=1
|
||||
testClientServer compact buffered --promise || TESTOK=1
|
||||
|
||||
#HTTP tests
|
||||
testHttpClientServer compact buffered || TESTOK=1
|
||||
testHttpClientServer compact framed || TESTOK=1
|
||||
testHttpClientServer json buffered || TESTOK=1
|
||||
testHttpClientServer json framed || TESTOK=1
|
||||
testHttpClientServer binary buffered || TESTOK=1
|
||||
|
@ -146,11 +146,21 @@ client.testI64(-5, function(err, response) {
|
||||
assert.equal(-5, response);
|
||||
});
|
||||
|
||||
client.testI64(734359738368, function(err, response) {
|
||||
assert( ! err);
|
||||
assert.equal(734359738368, response);
|
||||
});
|
||||
|
||||
client.testI64(-34359738368, function(err, response) {
|
||||
assert( ! err);
|
||||
assert.equal(-34359738368, response);
|
||||
});
|
||||
|
||||
client.testI64(-734359738368, function(err, response) {
|
||||
assert( ! err);
|
||||
assert.equal(-734359738368, response);
|
||||
});
|
||||
|
||||
client.testDouble(-5.2098523, function(err, response) {
|
||||
assert( ! err);
|
||||
assert.equal(-5.2098523, response);
|
||||
@ -192,21 +202,22 @@ client.testMap(mapout, function(err, response) {
|
||||
|
||||
var mapTestInput = {
|
||||
"a":"123", "a b":"with spaces ", "same":"same", "0":"numeric key",
|
||||
"longValue":stringTest, stringTest:"long key"
|
||||
"longValue":stringTest, "Afrikaans, Alemannisch, Aragonés, العربية, مصرى, ":"long key"
|
||||
};
|
||||
client.testStringMap(mapTestInput, function(err, response) {
|
||||
assert( ! err);
|
||||
assert.deepEqual(mapTestInput, response);
|
||||
});
|
||||
|
||||
var setTestInput = [1,2,3];
|
||||
client.testSet(setTestInput, function(err, response) {
|
||||
var setTestSetInput = [1,2,3];
|
||||
client.testSet(setTestSetInput, function(err, response) {
|
||||
assert( ! err);
|
||||
assert.deepEqual(setTestInput, response);
|
||||
assert.deepEqual(setTestSetInput, response);
|
||||
});
|
||||
client.testList(setTestInput, function(err, response) {
|
||||
var setTestListInput = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
|
||||
client.testList(setTestListInput, function(err, response) {
|
||||
assert( ! err);
|
||||
assert.deepEqual(setTestInput, response);
|
||||
assert.deepEqual(setTestListInput, response);
|
||||
});
|
||||
|
||||
client.testEnum(ttypes.Numberz.ONE, function(err, response) {
|
||||
@ -223,7 +234,7 @@ var mapMapTest = {
|
||||
"4": {"1":1, "2":2, "3":3, "4":4},
|
||||
"-4": {"-4":-4, "-3":-3, "-2":-2, "-1":-1}
|
||||
};
|
||||
client.testMapMap(mapMapTest, function(err, response) {
|
||||
client.testMapMap(1, function(err, response) {
|
||||
assert( ! err);
|
||||
assert.deepEqual(mapMapTest, response);
|
||||
});
|
||||
|
@ -289,7 +289,7 @@ var mapMapTest = {
|
||||
"4": {"1":1, "2":2, "3":3, "4":4},
|
||||
"-4": {"-4":-4, "-3":-3, "-2":-2, "-1":-1}
|
||||
};
|
||||
client.testMapMap(mapMapTest)
|
||||
client.testMapMap(1)
|
||||
.then(function(response) {
|
||||
assert.deepEqual(mapMapTest, response);
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user