Skip to content

Commit

Permalink
Merge 37258b9 into c4386da
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Apr 28, 2014
2 parents c4386da + 37258b9 commit 5783934
Show file tree
Hide file tree
Showing 99 changed files with 851 additions and 128 deletions.
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"maxerr" : 50, // {int} Maximum error before stopping

// Enforcing
"bitwise" : true, // true: Prohibit bitwise operators (&, |, ^, etc.)
"bitwise" : false, // true: Prohibit bitwise operators (&, |, ^, etc.)
"camelcase" : false, // true: Identifiers must be in camelCase
"curly" : true, // true: Require {} for every new block or scope
"eqeqeq" : true, // true: Require triple equals (===) for comparison
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ NPM_INSTALL = npm install --registry=http://registry.npm.taobao.org --disturl=ht
install:
@$(NPM_INSTALL)

jshint: install
@./node_modules/.bin/jshint .

test: install
@NODE_ENV=test ./node_modules/mocha/bin/mocha \
--reporter $(REPORTER) \
Expand All @@ -26,7 +29,7 @@ test-coveralls: test
@echo TRAVIS_JOB_ID $(TRAVIS_JOB_ID)
@-$(MAKE) test MOCHA_OPTS='--require blanket' REPORTER=mocha-lcov-reporter | ./node_modules/.bin/coveralls

test-all: test test-cov
test-all: test test-cov jshint

contributors: install
@./node_modules/.bin/contributors -f plain -o AUTHORS
Expand Down
2 changes: 2 additions & 0 deletions lib/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
*/

'use strict';

exports.DEFAULT_CLASSNAME = {
boolean: 'boolean',
int: 'int',
Expand Down
3 changes: 3 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
*/

'use strict';

var object = require('./object');

var MAX_INT_8 = exports.MAX_INT_8 = Math.pow(2, 7);
Expand Down Expand Up @@ -63,6 +65,7 @@ exports.lengthOfUTF8 = function (head) {
};

var _hasOwnProperty = Object.prototype.hasOwnProperty;
/* jshint -W001 */
exports.hasOwnProperty = function hasOwnProperty(obj, property) {
return _hasOwnProperty.call(obj, property);
};
Expand Down
34 changes: 25 additions & 9 deletions lib/v1/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/

'use strict';

var ByteBuffer = require('byte');
var debug = require('debug')('hessian:v1:encoder');
var utils = require('../utils');
Expand Down Expand Up @@ -153,19 +155,33 @@ proto.writeString = function (str) {
this._assertType('writeString', 'string', str);
var offset = 0;

while (str.length - offset >= utils.MAX_CHAR_TRUNK_SIZE) {
var length = str.length;
var strOffset = 0;
while (length > 0x8000) {
var sublen = 0x8000;
// chunk can't end in high surrogate
var tail = str.charCodeAt(strOffset + sublen - 1);

if (0xd800 <= tail && tail <= 0xdbff) {
debug('writeString got tail: 0x%s', tail.toString(16));
sublen--;
}

this.byteBuffer
.putChar('s')
.putUInt16(utils.MAX_BYTE_TRUNK_SIZE)
.putRawString(str.slice(offset, offset + utils.MAX_CHAR_TRUNK_SIZE));
.put(0x73) // 's'
.putUInt16(sublen)
.putRawString(str.slice(strOffset, strOffset + sublen));

offset += utils.MAX_CHAR_TRUNK_SIZE;
length -= sublen;
strOffset += sublen;
debug('writeString strOffset: %s, length: %s, sublen: %s', strOffset, length, sublen);
}

debug('writeString left length: %s', length);
this.byteBuffer
.putChar('S')
.putUInt16(str.length - offset)
.putRawString(str.slice(offset));
.put(0x53) // 'S'
.putUInt16(length)
.putRawString(str.slice(strOffset));

return this;
};
Expand Down Expand Up @@ -263,7 +279,7 @@ proto.writeObject = function (obj) {
this.write(key);
this.write(realObj[key]);
}
//end with 'z'
// end with 'z'
this.byteBuffer.putChar('z');
return this;
};
Expand Down
8 changes: 4 additions & 4 deletions lib/v2/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ utils.addByteCodes(BYTE_CODES, [
* ::= [xd8-xef]
* ::= [xf0-xff] b0
* ::= [x38-x3f] b1 b0
* ::= x59 b3 b2 b1 b0
* ::= x77 b3 b2 b1 b0
* ```
* A 64-bit signed integer. An long is represented by the octet x4c ('L' )
* followed by the 8-bytes of the integer in big-endian order.
Expand Down Expand Up @@ -183,8 +183,8 @@ proto.readLong = function () {
var b0 = this.byteBuffer.get();
return ((code - 0x3c) << 16) + (b1 << 8) + b0;
}
// ::= x59 b3 b2 b1 b0 # 32-bit integer cast to long
if (code === 0x59) {
// ::= x77 b3 b2 b1 b0 # 32-bit integer cast to long
if (code === 0x77) {
// Longs between which fit into 32-bits are encoded in five octets with the leading byte x59.
// value = (b3 << 24) + (b2 << 16) + (b1 << 8) + b0
return this.byteBuffer.getInt32();
Expand All @@ -201,7 +201,7 @@ utils.addByteCodes(BYTE_CODES, [
[0xd8, 0xef],
[0xf0, 0xff],
[0x38, 0x3f],
0x59,
0x77,
0x4c
], 'readLong');

Expand Down
103 changes: 8 additions & 95 deletions lib/v2/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ proto.writeInt = function (val) {
* ::= [xd8-xef]
* ::= [xf0-xff] b0
* ::= [x38-x3f] b1 b0
* ::= x59 b3 b2 b1 b0
* ::= x77 b3 b2 b1 b0
* ```
* A 64-bit signed integer. An long is represented by the octet x4c ('L' )
* followed by the 8-bytes of the integer in big-endian order.
Expand Down Expand Up @@ -125,10 +125,10 @@ proto.writeLong = function (val) {
var code = (val >> 16) + 0x3c;
this.byteBuffer.put(code).putUInt16(b1b0);
} else if (val >= -2147483648 && val <= 2147483647) {
this.byteBuffer.put(0x59).putInt32(val);
this.byteBuffer.put(0x77).putInt32(val);
} else {
this.byteBuffer
.put(0x4c)
.put(0x4c) // 'L'
.putLong(val);
}
return this;
Expand Down Expand Up @@ -254,92 +254,6 @@ proto.writeBytes = function (buf) {
return this;
};

// writeString java impl
/**
* Writes a string value to the stream using UTF-8 encoding.
* The string will be written with the following syntax:
*
* <code><pre>
* S b16 b8 string-value
* </pre></code>
*
* If the value is null, it will be written as
*
* <code><pre>
* N
* </pre></code>
*
* @param value the string value to write.
*/
// public void writeString(String value)
// throws IOException {
// int offset = _offset;
// byte []buffer = _buffer;
//
// if (SIZE <= offset + 16) {
// flush();
// offset = 0;
// }
//
// if (value == null) {
// buffer[offset++] = (byte) 'N';
//
// _offset = offset;
// }
// else {
// int length = value.length();
// int strOffset = 0;
//
// while (length > 0x8000) {
// int sublen = 0x8000;
//
// offset = _offset;
//
// if (SIZE <= offset + 16) {
// flush();
// offset = 0;
// }
//
// // chunk can't end in high surrogate
// char tail = value.charAt(strOffset + sublen - 1);
//
// if (0xd800 <= tail && tail <= 0xdbff)
// sublen--;
//
// buffer[offset + 0] = (byte) 's';
// buffer[offset + 1] = (byte) (sublen >> 8);
// buffer[offset + 2] = (byte) (sublen);
//
// _offset = offset + 3;
//
// printString(value, strOffset, sublen);
//
// length -= sublen;
// strOffset += sublen;
// }
//
// offset = _offset;
//
// if (SIZE <= offset + 16) {
// flush();
// offset = 0;
// }
//
// if (length <= STRING_DIRECT_MAX) {
// buffer[offset++] = (byte) (STRING_DIRECT + length);
// }
// else {
// buffer[offset++] = (byte) ('S');
// buffer[offset++] = (byte) (length >> 8);
// buffer[offset++] = (byte) (length);
// }
//
// _offset = offset;
//
// printString(value, strOffset, length);
// }
// }

/**
* encode string
*
Expand Down Expand Up @@ -375,11 +289,11 @@ proto.writeString = function (str) {
while (length > 0x8000) {
var sublen = 0x8000;
// chunk can't end in high surrogate
var tail = str.charCodeAt(strOffset + sublen - 1);
var tail = str.charCodeAt(strOffset + sublen - 1);

if (0xd800 <= tail && tail <= 0xdbff) {
if (0xd800 <= tail && tail <= 0xdbff) {
debug('writeString got tail: 0x%s', tail.toString(16));
sublen--;
sublen--;
}

this.byteBuffer
Expand All @@ -397,7 +311,7 @@ proto.writeString = function (str) {
this.byteBuffer.put(length).putRawString(str.slice(strOffset));
} else {
this.byteBuffer
.put(0x53)
.put(0x53) // 'S'
.putUInt16(length)
.putRawString(str.slice(strOffset));
}
Expand Down Expand Up @@ -657,8 +571,7 @@ proto._writeListBegin = function (length, type) {
this.byteBuffer.put(0x56); // 'V';
this.writeType(type);

if (length < 0) {
} else if (length < 0x100) {
if (length < 0x100) {
// var LENGTH_BYTE = 0x6e;
this.byteBuffer.put(0x6e);
this.byteBuffer.put(length);
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@
},
"homepage": "https://github.com/node-modules/hessian.js",
"dependencies": {
"byte": "^0.2",
"debug": "0.8.0",
"byte": "0.2.x",
"debug": "0.8.1",
"is-type-of": "0.2.1"
},
"devDependencies": {
"autod": "~0.0.13",
"autod": "*",
"blanket": ">=1.1.6",
"contributors": "*",
"coveralls": "*",
"js-to-java": ">=0.0.3",
"js-to-java": "*",
"jshint": "*",
"mocha": "*",
"mocha-lcov-reporter": "*",
"should": "3.3.1",
Expand Down
26 changes: 25 additions & 1 deletion test/date.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,31 @@ describe('date.test.js', function () {
hessian.encode(new Date(0)).should.eql(new Buffer(['d'.charCodeAt(0), 0, 0, 0, 0, 0, 0, 0, 0]));
});

it('should read date 09:51:31 May 8, 1998 UTC', function () {
var d = hessian.decode(utils.bytes('v1/date/894621091000'), '1.0');
d.should.be.a.Date;
d.getFullYear().should.equal(1998);
d.getTime().should.equal(894621091000);
d.toUTCString().should.equal('Fri, 08 May 1998 09:51:31 GMT');
d.toISOString().should.equal('1998-05-08T09:51:31.000Z');
});

it('should read date 09:51:00 May 8, 1998 UTC', function () {
var d = hessian.decode(utils.bytes('v1/date/894621060000'), '1.0');
d.should.be.a.Date;
d.getFullYear().should.equal(1998);
d.getTime().should.equal(894621060000);
d.toUTCString().should.equal('Fri, 08 May 1998 09:51:00 GMT');
d.toISOString().should.equal('1998-05-08T09:51:00.000Z');
});

it('should write date', function () {
var now = new Date(1398280514000);
hessian.encode(now, '1.0').should.eql(utils.bytes('v1/date/now'));
// read it
hessian.decode(utils.bytes('v2/date/now'), '1.0').should.eql(now);
});

describe('hessian 2.0', function () {
it('should read date 09:51:31 May 8, 1998 UTC', function () {
var d = hessian.decode(utils.bytes('v2/date/894621091000'), '2.0');
Expand All @@ -58,7 +83,6 @@ describe('date.test.js', function () {
});

it('should write date', function () {
// hessian.encode(new Date(894621091000), '2.0').should.eql(dateV2Buffer);
var now = new Date(1398280514000);
hessian.encode(now, '2.0').should.eql(utils.bytes('v2/date/now'));
// read it
Expand Down
Binary file added test/fixtures/v1/date/894621060000.bin
Binary file not shown.
Binary file added test/fixtures/v1/date/894621091000.bin
Binary file not shown.
Binary file added test/fixtures/v1/date/now.bin
Binary file not shown.
Binary file added test/fixtures/v1/enum/blue.bin
Binary file not shown.
Binary file added test/fixtures/v1/enum/green.bin
Binary file not shown.
Binary file added test/fixtures/v1/enum/lists.bin
Binary file not shown.
Binary file added test/fixtures/v1/enum/red.bin
Binary file not shown.
Binary file added test/fixtures/v1/list/[int.bin
Binary file not shown.
Binary file added test/fixtures/v1/list/[string.bin
Binary file not shown.
Binary file added test/fixtures/v1/list/typed_list.bin
Binary file not shown.
Binary file added test/fixtures/v1/list/untyped_<String>[foo,bar].bin
Binary file not shown.
Binary file added test/fixtures/v1/list/untyped_[].bin
Binary file not shown.
Binary file added test/fixtures/v1/list/untyped_list.bin
Binary file not shown.
Binary file added test/fixtures/v1/long/-2048.bin
Binary file not shown.
1 change: 1 addition & 0 deletions test/fixtures/v1/long/-2049.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
L��������
Binary file added test/fixtures/v1/long/-2147483647.bin
Binary file not shown.
Binary file added test/fixtures/v1/long/-2147483648.bin
Binary file not shown.
Binary file added test/fixtures/v1/long/-262144.bin
Binary file not shown.
1 change: 1 addition & 0 deletions test/fixtures/v1/long/-7.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
L��������
1 change: 1 addition & 0 deletions test/fixtures/v1/long/-8.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
L��������
1 change: 1 addition & 0 deletions test/fixtures/v1/long/-9.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
L��������
Binary file added test/fixtures/v1/long/0.bin
Binary file not shown.
Binary file added test/fixtures/v1/long/14.bin
Binary file not shown.
Binary file added test/fixtures/v1/long/15.bin
Binary file not shown.
Binary file added test/fixtures/v1/long/16.bin
Binary file not shown.
Binary file added test/fixtures/v1/long/2047.bin
Binary file not shown.
Binary file added test/fixtures/v1/long/2048.bin
Binary file not shown.
Binary file added test/fixtures/v1/long/2147483646.bin
Binary file not shown.
Binary file added test/fixtures/v1/long/2147483647.bin
Binary file not shown.
Binary file added test/fixtures/v1/long/2147483648.bin
Binary file not shown.
Binary file added test/fixtures/v1/long/255.bin
Binary file not shown.
Binary file added test/fixtures/v1/long/262143.bin
Binary file not shown.
Binary file added test/fixtures/v1/map/car.bin
Binary file not shown.
Binary file added test/fixtures/v1/map/car1.bin
Binary file not shown.
Binary file added test/fixtures/v1/map/car2.bin
Binary file not shown.
Binary file added test/fixtures/v1/map/car_list.bin
Binary file not shown.
Binary file added test/fixtures/v1/map/foo_bar.bin
Binary file not shown.
Binary file added test/fixtures/v1/map/foo_empty.bin
Binary file not shown.
Binary file added test/fixtures/v1/map/one_car_list.bin
Binary file not shown.
Binary file added test/fixtures/v1/map/two_car_list.bin
Binary file not shown.
1 change: 1 addition & 0 deletions test/fixtures/v1/number/-16.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I����
Binary file added test/fixtures/v1/number/-2048.bin
Binary file not shown.
Binary file added test/fixtures/v1/number/-256.bin
Binary file not shown.
Binary file added test/fixtures/v1/number/-262144.bin
Binary file not shown.
1 change: 1 addition & 0 deletions test/fixtures/v1/number/-262145.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I����
Binary file added test/fixtures/v1/number/0.bin
Binary file not shown.
Binary file added test/fixtures/v1/number/1.bin
Binary file not shown.
Binary file added test/fixtures/v1/number/10.bin
Binary file not shown.
Binary file added test/fixtures/v1/number/16.bin
Binary file not shown.
Binary file added test/fixtures/v1/number/2047.bin
Binary file not shown.
Binary file added test/fixtures/v1/number/255.bin
Binary file not shown.
Binary file added test/fixtures/v1/number/256.bin
Binary file not shown.
Binary file added test/fixtures/v1/number/262143.bin
Binary file not shown.
Binary file added test/fixtures/v1/number/262144.bin
Binary file not shown.
Binary file added test/fixtures/v1/number/46.bin
Binary file not shown.
Binary file added test/fixtures/v1/number/47.bin
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added test/fixtures/v1/string/chinese.bin
Binary file not shown.
Binary file added test/fixtures/v1/string/empty.bin
Binary file not shown.
Binary file added test/fixtures/v1/string/foo.bin
Binary file not shown.
Binary file added test/fixtures/v1/string/large_string_65535.bin
Binary file not shown.
1 change: 1 addition & 0 deletions test/fixtures/v1/string/large_string_chars.bin

Large diffs are not rendered by default.

Loading

0 comments on commit 5783934

Please sign in to comment.