Skip to content

Commit

Permalink
feat: support custom decode handler (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
gxcsoccer authored May 15, 2019
1 parent 3f0b392 commit ee57e93
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 15 deletions.
4 changes: 3 additions & 1 deletion .autod.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ module.exports = {
'istanbul',
],
semver: [
'byte@1',
'debug@3',
'mocha@3',
],
};
};
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@ exports.encode = function encode(obj, version) {
encoder.reset();
return encoder.write(obj).get();
};

var custom = require('./lib/custom_handler');

exports.registerDecodeHandler = custom.registerDecodeHandler;
exports.deregisterDecodeHandler= custom.deregisterDecodeHandler;
28 changes: 28 additions & 0 deletions lib/custom_handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

var assert = require('assert');

var handlers = {};

function deregisterDecodeHandler(className) {
delete handlers[className];
}

function registerDecodeHandler(className, handler) {
assert(typeof handler === 'function', 'handler should be a function');
handlers[className] = handler;
}

function handle(result, withType) {
var className = result.$class;
var handler = handlers[className];

if (handler) {
result = handler(result);
}
return withType ? result : result.$;
}

exports.handle = handle;
exports.registerDecodeHandler = registerDecodeHandler;
exports.deregisterDecodeHandler = deregisterDecodeHandler;
3 changes: 2 additions & 1 deletion lib/v1/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var ByteBuffer = require('byte');
var is = require('is-type-of');
var utils = require('../utils');
var object = require('../object');
var handle = require('../custom_handler').handle;
var JavaExceptionError = object.JavaExceptionError;
var supportES6Map = require('../utils').supportES6Map;

Expand Down Expand Up @@ -417,7 +418,7 @@ proto.readObject = function (withType) {
result.$ = new JavaExceptionError(result, withType);
}

return withType ? result : result.$;
return handle(result, withType);
};

utils.addByteCodes(BYTE_CODES, [
Expand Down
9 changes: 5 additions & 4 deletions lib/v2/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var is = require('is-type-of');
var debug = require('debug')('hessian:v2:decoder');
var DecoderV1 = require('../v1/decoder');
var utils = require('../utils');
var handle = require('../custom_handler').handle;
var JavaExceptionError = require('../object').JavaExceptionError;
var supportES6Map = require('../utils').supportES6Map;

Expand Down Expand Up @@ -612,7 +613,7 @@ proto.readObject = function (withType) {
result.$ = new JavaExceptionError(result, withType);
}

return withType ? result : result.$;
return handle(result, withType);
};

utils.addByteCodes(BYTE_CODES, [
Expand Down Expand Up @@ -777,7 +778,7 @@ proto.readHashMap = function (withType) {
var result = {};
this._addRef(result);
this._readMap(result, withType);
return result;
return handle({ $class: 'java.util.HashMap', $: result }, withType);
};

utils.addByteCodes(BYTE_CODES, [
Expand Down Expand Up @@ -820,7 +821,7 @@ proto.readMap = function (withType) {
var map = {};
this._addRef(map);
this._readMap(map);
return map;
return handle({ $class: 'java.util.HashMap', $: map }, withType);
}

var result = {
Expand All @@ -836,7 +837,7 @@ proto.readMap = function (withType) {
result.$ = new JavaExceptionError(result);
}

return withType ? result : result.$;
return handle(result, withType);
};

utils.addByteCodes(BYTE_CODES, [
Expand Down
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,20 @@
},
"homepage": "https://github.com/node-modules/hessian.js",
"dependencies": {
"byte": "^1.3.0",
"debug": "^3.1.0",
"is-type-of": "^1.2.0",
"long": "^3.2.0",
"utility": "^1.13.1"
"byte": "^1.4.1",
"debug": "^3.2.6",
"is-type-of": "^1.2.1",
"long": "^4.0.0",
"utility": "^1.16.1"
},
"devDependencies": {
"autod": "^3.0.1",
"autod": "^3.1.0",
"beautify-benchmark": "^0.2.4",
"benchmark": "^2.1.4",
"istanbul": "^0.4.5",
"js-to-java": "^2.4.0",
"jshint": "^2.9.5",
"mm": "^2.2.0",
"js-to-java": "^2.6.1",
"jshint": "^2.10.2",
"mm": "^2.5.0",
"mocha": "^3.5.3"
},
"engines": {
Expand Down
97 changes: 97 additions & 0 deletions test/custom_handler.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
'use strict';

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

describe('utils.test.js', function () {
describe('v1.0', function () {
it('should decode with custom handler', function () {
hessian.registerDecodeHandler('java.math.BigDecimal', function (result) {
return {
$class: result.$class,
$: result.$.value,
};
});
var o = { $class: 'java.math.BigDecimal', $: { value: '100.06' } };
var buf = hessian.encode(o, '1.0');
var output = hessian.decode(buf, '1.0');
assert(output === '100.06');
hessian.deregisterDecodeHandler('java.math.BigDecimal');
output = hessian.decode(buf, '1.0');
assert.deepEqual(output, { value: '100.06' });
});

if (!supportES6Map) {
return;
}

it('should decode map with custom handler', function () {
hessian.registerDecodeHandler('java.util.HashMap', function (result) {
return {
$class: result.$class,
$: result.$.$map,
};
});
var map = new Map();
map.set(1, 'fee');
map.set(2, 'fie');
map.set(3, 'foe');
var buf = hessian.encode({
$class: 'java.util.HashMap',
$: map
}, '1.0');
var output = hessian.decode(buf, '1.0');
assert(output instanceof Map);
assert(output.get(1) === 'fee');
assert(output.get(2) === 'fie');
assert(output.get(3) === 'foe');
hessian.deregisterDecodeHandler('java.util.HashMap');
});
});

describe('v2.0', function () {
it('should decode with custom handler', function () {
hessian.registerDecodeHandler('java.math.BigDecimal', function (result) {
return {
$class: result.$class,
$: result.$.value,
};
});
var o = { $class: 'java.math.BigDecimal', $: { value: '100.06' } };
var buf = hessian.encode(o, '2.0');
var output = hessian.decode(buf, '2.0');
assert(output === '100.06');
hessian.deregisterDecodeHandler('java.math.BigDecimal');
output = hessian.decode(buf, '2.0');
assert.deepEqual(output, { value: '100.06' });
});

if (!supportES6Map) {
return;
}

it('should decode map with custom handler', function () {
hessian.registerDecodeHandler('java.util.HashMap', function (result) {
return {
$class: result.$class,
$: result.$.$map,
};
});
var map = new Map();
map.set(1, 'fee');
map.set(2, 'fie');
map.set(3, 'foe');
var buf = hessian.encode({
$class: 'java.util.HashMap',
$: map
}, '2.0');
var output = hessian.decode(buf, '2.0');
assert(output instanceof Map);
assert(output.get(1) === 'fee');
assert(output.get(2) === 'fie');
assert(output.get(3) === 'foe');
hessian.deregisterDecodeHandler('java.util.HashMap');
});
});
});

0 comments on commit ee57e93

Please sign in to comment.