Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: skip regExp & Function property #76

Merged
merged 1 commit into from
May 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ node_js:
- '5'
- '4'
- '0.12'
- '0.10'
script: "make test-cov"
after_script: "npm i codecov && codecov"
2 changes: 1 addition & 1 deletion lib/v1/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ proto.writeList = proto.writeArray;
*/
proto.write = function (val) {
var type = typeof val;
if (is.nullOrUndefined(val) || is.NaN(val)) {
if (is.nullOrUndefined(val) || is.NaN(val) || is.function(val) || is.regExp(val)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为了性能考虑,就不在 writeObject 再去判断类型了,如果是不支持的类型通通都写成 null

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没发版本。。。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is-type-of 没有升级,导致 is.regExp 判断很慢。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#80

return this.writeNull();
}
switch (type) {
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"js-to-java": "2",
"jshint": "*",
"long": "2",
"mocha": "*",
"should": "5"
"mocha": "*"
}
}
14 changes: 5 additions & 9 deletions test/array.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@

"use strict";

/**
* Module dependencies.
*/

var should = require('should');
var assert = require('assert');
var hessian = require('../');

describe('array.test.js', function () {
Expand All @@ -28,7 +24,7 @@ describe('array.test.js', function () {
}
]);
var a = hessian.decode(b);
a.should.eql([null, [1]]);
assert.deepEqual(a, [null, [1]]);
});

it('should write undefined v1', function () {
Expand All @@ -43,7 +39,7 @@ describe('array.test.js', function () {
}
]);
var a = hessian.decode(b);
a.should.eql([null, [1]]);
assert.deepEqual(a, [null, [1]]);
});

it('should write null v2', function () {
Expand All @@ -58,7 +54,7 @@ describe('array.test.js', function () {
}
], '2.0');
var a = hessian.decode(b, '2.0');
a.should.eql([null, [1]]);
assert.deepEqual(a, [null, [1]]);
});

it('should write undefined v2', function () {
Expand All @@ -73,6 +69,6 @@ describe('array.test.js', function () {
}
], '2.0');
var a = hessian.decode(b, '2.0');
a.should.eql([null, [1]]);
assert.deepEqual(a, [null, [1]]);
});
});
145 changes: 73 additions & 72 deletions test/binary.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@

"use strict";

/**
* Module dependencies.
*/

var should = require('should');
var assert = require('assert');
var hessian = require('../');
var utils = require('./utils');

Expand All @@ -24,105 +20,110 @@ describe('binary.test.js', function () {
});

it('should write "foo"', function () {
hessian.encode(new Buffer('foo')).should.eql(
Buffer.concat([new Buffer(['B'.charCodeAt(0), 0x00, 0x03]), new Buffer('foo')]));
assert.deepEqual(
hessian.encode(new Buffer('foo')),
Buffer.concat([new Buffer(['B'.charCodeAt(0), 0x00, 0x03]), new Buffer('foo')])
);
});

it('should read and write empty binary', function () {
var empty = hessian.decode(new Buffer(['B'.charCodeAt(0), 0x00, 0x00]));
empty.should.be.a.Buffer;
empty.should.length(0);
assert(Buffer.isBuffer(empty));
assert(empty.length === 0);

hessian.encode(new Buffer('')).should.eql(new Buffer(['B'.charCodeAt(0), 0x00, 0x00]));
assert.deepEqual(
hessian.encode(new Buffer('')),
new Buffer(['B'.charCodeAt(0), 0x00, 0x00])
);
});

it('should write and read as java impl', function () {
var bytes = new Buffer(65535);
bytes.fill(0x41);
var buf = hessian.encode(bytes, '1.0');
buf.should.length(utils.bytes('v1/bytes/65535').length);
buf.should.eql(utils.bytes('v1/bytes/65535'));
hessian.decode(utils.bytes('v1/bytes/65535'), '1.0').should.eql(bytes);
assert(buf.length === utils.bytes('v1/bytes/65535').length);
assert.deepEqual(buf, utils.bytes('v1/bytes/65535'));
assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/65535'), '1.0'), bytes);

var bytes = new Buffer(32768);
bytes.fill(0x41);
var buf = hessian.encode(bytes, '1.0');
buf.should.length(utils.bytes('v1/bytes/32768').length);
buf.should.eql(utils.bytes('v1/bytes/32768'));
hessian.decode(utils.bytes('v1/bytes/32768'), '1.0').should.eql(bytes);
assert(buf.length === utils.bytes('v1/bytes/32768').length);
assert.deepEqual(buf, utils.bytes('v1/bytes/32768'));
assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/32768'), '1.0'), bytes);

var bytes = new Buffer(32769);
bytes.fill(0x41);
var buf = hessian.encode(bytes, '1.0');
buf.should.length(utils.bytes('v1/bytes/32769').length);
buf.should.eql(utils.bytes('v1/bytes/32769'));
hessian.decode(utils.bytes('v1/bytes/32769'), '1.0').should.eql(bytes);
assert(buf.length === utils.bytes('v1/bytes/32769').length);
assert.deepEqual(buf, utils.bytes('v1/bytes/32769'));
assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/32769'), '1.0'), bytes);

var bytes = new Buffer(32767);
bytes.fill(0x41);
var buf = hessian.encode(bytes, '1.0');
buf.should.length(utils.bytes('v1/bytes/32767').length);
buf.should.eql(utils.bytes('v1/bytes/32767'));
hessian.decode(utils.bytes('v1/bytes/32767'), '1.0').should.eql(bytes);
assert(buf.length === utils.bytes('v1/bytes/32767').length);
assert.deepEqual(buf, utils.bytes('v1/bytes/32767'));
assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/32767'), '1.0'), bytes);

var bytes = new Buffer(32769);
bytes.fill(0x41);
var buf = hessian.encode(bytes, '1.0');
buf.should.length(utils.bytes('v1/bytes/32769').length);
buf.should.eql(utils.bytes('v1/bytes/32769'));
hessian.decode(utils.bytes('v1/bytes/32769'), '1.0').should.eql(bytes);
assert(buf.length === utils.bytes('v1/bytes/32769').length);
assert.deepEqual(buf, utils.bytes('v1/bytes/32769'));
assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/32769'), '1.0'), bytes);

var bytes = new Buffer(42769);
bytes.fill(0x41);
var buf = hessian.encode(bytes, '1.0');
buf.should.length(utils.bytes('v1/bytes/42769').length);
buf.should.eql(utils.bytes('v1/bytes/42769'));
hessian.decode(utils.bytes('v1/bytes/42769'), '1.0').should.eql(bytes);
assert(buf.length === utils.bytes('v1/bytes/42769').length);
assert.deepEqual(buf, utils.bytes('v1/bytes/42769'));
assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/42769'), '1.0'), bytes);

var bytes = new Buffer(82769);
bytes.fill(0x41);
var buf = hessian.encode(bytes, '1.0');
buf.should.length(utils.bytes('v1/bytes/82769').length);
buf.should.eql(utils.bytes('v1/bytes/82769'));
hessian.decode(utils.bytes('v1/bytes/82769'), '1.0').should.eql(bytes);
assert(buf.length === utils.bytes('v1/bytes/82769').length);
assert.deepEqual(buf, utils.bytes('v1/bytes/82769'));
assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/82769'), '1.0'), bytes);
});

describe('v2.0', function () {
it('should read zero length binary data', function () {
var buf = hessian.decode(new Buffer([0x20]), '2.0');
buf.should.length(0);
buf.should.eql(new Buffer(0));
assert(buf.length === 0);
assert.deepEqual(buf, new Buffer(0));
});

it('should read short datas', function () {
var decoder = new hessian.DecoderV2(new Buffer([0x20, 0x23, 0x23, 0x02, 0x03, 0x20]));
var buf = decoder.read();
buf.should.length(0);
buf.should.eql(new Buffer(0));
assert(buf.length === 0);
assert.deepEqual(buf, new Buffer(0));

buf = decoder.read();
buf.should.length(3);
buf.should.eql(new Buffer([0x23, 2, 3]));
assert(buf.length === 3);
assert.deepEqual(buf, new Buffer([0x23, 2, 3]));

buf = decoder.read();
buf.should.length(0);
buf.should.eql(new Buffer(0));
assert(buf.length === 0);
assert.deepEqual(buf, new Buffer(0));
});

it('should read max length short datas', function () {
var input = new Buffer(16);
input.fill(0x2f);
input[0] = 0x2f;
var buf = hessian.decode(input, '2.0');
buf.should.length(15);
assert(buf.length === 15);
var output = new Buffer(15);
output.fill(0x2f);
buf.should.eql(output);
assert.deepEqual(buf, output);
});

it('should read long binary', function () {
var buf = hessian.encode(new Buffer(65535), '2.0');
buf[0].should.equal(0x62);
assert(buf[0] === 0x62);
hessian.decode(buf, '2.0');

buf = hessian.encode(new Buffer(65536), '2.0');
Expand All @@ -133,92 +134,92 @@ describe('binary.test.js', function () {
});

it('should write short binary', function () {
hessian.encode(new Buffer(''), '2.0').should.eql(new Buffer([0x20]));
assert.deepEqual(hessian.encode(new Buffer(''), '2.0'), new Buffer([0x20]));
});

it('should write and read as java impl', function () {
var bytes = new Buffer(65535);
bytes.fill(0x41);
var buf = hessian.encode(bytes, '2.0');
buf.should.length(utils.bytes('v2/bytes/65535').length);
buf.should.eql(utils.bytes('v2/bytes/65535'));
hessian.decode(utils.bytes('v2/bytes/65535'), '2.0').should.eql(bytes);
assert(buf.length === utils.bytes('v2/bytes/65535').length);
assert.deepEqual(buf, utils.bytes('v2/bytes/65535'));
assert.deepEqual(hessian.decode(utils.bytes('v2/bytes/65535'), '2.0'), bytes);

var bytes = new Buffer(32768);
bytes.fill(0x41);
var buf = hessian.encode(bytes, '2.0');
buf.should.length(utils.bytes('v2/bytes/32768').length);
buf.should.eql(utils.bytes('v2/bytes/32768'));
hessian.decode(utils.bytes('v2/bytes/32768'), '2.0').should.eql(bytes);
assert(buf.length === utils.bytes('v2/bytes/32768').length);
assert.deepEqual(buf, utils.bytes('v2/bytes/32768'));
assert.deepEqual(hessian.decode(utils.bytes('v2/bytes/32768'), '2.0'), bytes);

var bytes = new Buffer(32769);
bytes.fill(0x41);
var buf = hessian.encode(bytes, '2.0');
buf.should.length(utils.bytes('v2/bytes/32769').length);
buf.should.eql(utils.bytes('v2/bytes/32769'));
hessian.decode(utils.bytes('v2/bytes/32769'), '2.0').should.eql(bytes);
assert(buf.length === utils.bytes('v2/bytes/32769').length);
assert.deepEqual(buf, utils.bytes('v2/bytes/32769'));
assert.deepEqual(hessian.decode(utils.bytes('v2/bytes/32769'), '2.0'), bytes);

var bytes = new Buffer(32767);
bytes.fill(0x41);
var buf = hessian.encode(bytes, '2.0');
buf.should.length(utils.bytes('v2/bytes/32767').length);
buf.should.eql(utils.bytes('v2/bytes/32767'));
hessian.decode(utils.bytes('v2/bytes/32767'), '2.0').should.eql(bytes);
assert(buf.length === utils.bytes('v2/bytes/32767').length);
assert.deepEqual(buf, utils.bytes('v2/bytes/32767'));
assert.deepEqual(hessian.decode(utils.bytes('v2/bytes/32767'), '2.0'), bytes);

var bytes = new Buffer(32769);
bytes.fill(0x41);
var buf = hessian.encode(bytes, '2.0');
buf.should.length(utils.bytes('v2/bytes/32769').length);
buf.should.eql(utils.bytes('v2/bytes/32769'));
hessian.decode(utils.bytes('v2/bytes/32769'), '2.0').should.eql(bytes);
assert(buf.length === utils.bytes('v2/bytes/32769').length);
assert.deepEqual(buf, utils.bytes('v2/bytes/32769'));
assert.deepEqual(hessian.decode(utils.bytes('v2/bytes/32769'), '2.0'), bytes);

var bytes = new Buffer(42769);
bytes.fill(0x41);
var buf = hessian.encode(bytes, '2.0');
buf.should.length(utils.bytes('v2/bytes/42769').length);
buf.should.eql(utils.bytes('v2/bytes/42769'));
hessian.decode(utils.bytes('v2/bytes/42769'), '2.0').should.eql(bytes);
assert(buf.length === utils.bytes('v2/bytes/42769').length);
assert.deepEqual(buf, utils.bytes('v2/bytes/42769'));
assert.deepEqual(hessian.decode(utils.bytes('v2/bytes/42769'), '2.0'), bytes);

var bytes = new Buffer(82769);
bytes.fill(0x41);
var buf = hessian.encode(bytes, '2.0');
buf.should.length(utils.bytes('v2/bytes/82769').length);
buf.should.eql(utils.bytes('v2/bytes/82769'));
hessian.decode(utils.bytes('v2/bytes/82769'), '2.0').should.eql(bytes);
assert(buf.length === utils.bytes('v2/bytes/82769').length);
assert.deepEqual(buf, utils.bytes('v2/bytes/82769'));
assert.deepEqual(hessian.decode(utils.bytes('v2/bytes/82769'), '2.0'), bytes);
});

it('should read java hessian 1.0 bin format', function () {
var bytes = new Buffer(65535);
bytes.fill(0x41);
hessian.decode(utils.bytes('v1/bytes/65535'), '2.0').should.eql(bytes);
assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/65535'), '2.0'), bytes);

var bytes = new Buffer(32767);
bytes.fill(0x41);
hessian.decode(utils.bytes('v1/bytes/32767'), '2.0').should.eql(bytes);
assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/32767'), '2.0'), bytes);

var bytes = new Buffer(32768);
bytes.fill(0x41);
hessian.decode(utils.bytes('v1/bytes/32768'), '2.0').should.eql(bytes);
assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/32768'), '2.0'), bytes);

var bytes = new Buffer(32769);
bytes.fill(0x41);
hessian.decode(utils.bytes('v1/bytes/32769'), '2.0').should.eql(bytes);
assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/32769'), '2.0'), bytes);

var bytes = new Buffer(32767);
bytes.fill(0x41);
hessian.decode(utils.bytes('v1/bytes/32767'), '2.0').should.eql(bytes);
assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/32767'), '2.0'), bytes);

var bytes = new Buffer(32769);
bytes.fill(0x41);
hessian.decode(utils.bytes('v1/bytes/32769'), '2.0').should.eql(bytes);
assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/32769'), '2.0'), bytes);

var bytes = new Buffer(42769);
bytes.fill(0x41);
hessian.decode(utils.bytes('v1/bytes/42769'), '2.0').should.eql(bytes);
assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/42769'), '2.0'), bytes);

var bytes = new Buffer(82769);
bytes.fill(0x41);
hessian.decode(utils.bytes('v1/bytes/82769'), '2.0').should.eql(bytes);
assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/82769'), '2.0'), bytes);
});
});
});
22 changes: 9 additions & 13 deletions test/boolean.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,26 @@

"use strict";

/**
* Module dependencies.
*/

var should = require('should');
var assert = require('assert');
var hessian = require('../');

describe('boolean.test.js', function () {
it('should read true and false', function () {
hessian.decode(new Buffer('T')).should.equal(true);
hessian.decode(new Buffer('F')).should.equal(false);
assert(hessian.decode(new Buffer('T')) === true);
assert(hessian.decode(new Buffer('F')) === false);
});

it('should write true and false', function () {
hessian.encode(true).should.eql(new Buffer('T'));
hessian.encode(false).should.eql(new Buffer('F'));
assert.deepEqual(hessian.encode(true), new Buffer('T'));
assert.deepEqual(hessian.encode(false), new Buffer('F'));
});

describe('v2.0', function () {
it('should read write as 1.0', function () {
hessian.encode(true, '2.0').should.eql(new Buffer('T'));
hessian.encode(false, '2.0').should.eql(new Buffer('F'));
hessian.decode(new Buffer('T'), '2.0').should.equal(true);
hessian.decode(new Buffer('F'), '2.0').should.equal(false);
assert.deepEqual(hessian.encode(true, '2.0'), new Buffer('T'));
assert.deepEqual(hessian.encode(false, '2.0'), new Buffer('F'));
assert(hessian.decode(new Buffer('T'), '2.0') === true);
assert(hessian.decode(new Buffer('F'), '2.0') === false);
});
});
});
Loading