Skip to content

Commit

Permalink
feat: support hessian4 (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
gxcsoccer authored Jun 24, 2020
1 parent e908dec commit 21de7ec
Show file tree
Hide file tree
Showing 14 changed files with 265 additions and 199 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ${{ matrix.os }}

strategy:
matrix:
node-version: [10.x, 12.x, 14.x]
os: [ubuntu-latest, windows-latest, macos-latest]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm i -g npminstall && npminstall
- run: npm run ci
env:
CI: true
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
sudo: false

language: node_js
node_js:
- '8'
- '10'
- '12'
- '14'
before_install:
- npm i npminstall -g
install:
Expand Down
3 changes: 2 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
environment:
matrix:
- nodejs_version: '8'
- nodejs_version: '10'
- nodejs_version: '12'
- nodejs_version: '14'

install:
- ps: Install-Product node $env:nodejs_version
Expand Down
13 changes: 4 additions & 9 deletions lib/codec/hessian/app_type/rpc_server_exception.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
'use strict';

const compile = require('sofa-hessian-node').compile;
const { compile } = require('../hessian');
const builtInClassMap = require('./builtin_class_map');

const encodeRpcServerExceptionMap = {
hessian2: compile({
type: 'com.alipay.remoting.rpc.exception.RpcServerException',
}, '2.0', builtInClassMap),
};

exports.encode = (target, encoder) => {
const codecType = target.codecType;
const err = target.obj;

const detailMessage = err.name + ': ' + err.message;
Expand Down Expand Up @@ -52,7 +45,9 @@ exports.encode = (target, encoder) => {
}
}

encodeRpcServerExceptionMap[codecType]({
compile({
type: 'com.alipay.remoting.rpc.exception.RpcServerException',
}, '2.0', builtInClassMap)({
detailMessage,
stackTrace,
cause: null,
Expand Down
12 changes: 4 additions & 8 deletions lib/codec/hessian/app_type/sofa_request.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
'use strict';

const utils = require('../../../utils');
const compile = require('sofa-hessian-node').compile;
const { compile } = require('../hessian');
const builtInClassMap = require('./builtin_class_map');

const encodeSofaRequestMap = {
hessian2: compile({
type: 'com.alipay.sofa.rpc.core.request.SofaRequest',
}, '2.0', builtInClassMap),
};

exports.encode = (target, encoder) => {
const req = target.obj;
const classMap = target.classMap;
Expand All @@ -22,7 +16,9 @@ exports.encode = (target, encoder) => {
methodArgSigs.push(utils.getJavaClassname(methodArgs[i]));
}
// encode SofaRequest
encodeSofaRequestMap[codecType]({
compile({
type: 'com.alipay.sofa.rpc.core.request.SofaRequest',
}, '2.0', builtInClassMap)({
targetAppName: req.targetAppName,
methodName: req.methodName,
methodArgSigs,
Expand Down
13 changes: 4 additions & 9 deletions lib/codec/hessian/app_type/sofa_response.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
'use strict';

const compile = require('sofa-hessian-node').compile;
const { compile } = require('../hessian');
const builtInClassMap = require('./builtin_class_map');

const encodeSofaResponseMap = {
hessian2: compile({
type: 'com.alipay.sofa.rpc.core.response.SofaResponse',
}, '2.0', builtInClassMap),
};

exports.encode = (target, encoder) => {
const codecType = target.codecType;
encodeSofaResponseMap[codecType](target.obj, encoder, target.classMap);
compile({
type: 'com.alipay.sofa.rpc.core.response.SofaResponse',
}, '2.0', builtInClassMap)(target.obj, encoder, target.classMap);
};

exports.decode = decoder => {
Expand Down
31 changes: 31 additions & 0 deletions lib/codec/hessian/hessian.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

const { v3, v4 } = require('sofa-hessian-node');

const encoder_v3 = new v3.Encoder({ size: 1 });
const decoder_v3 = new v3.Decoder();
const encoder_v4 = new v4.Encoder({ size: 1 });
const decoder_v4 = new v4.Decoder();

// 默认为 hessian 3.0
let version = '3.0';

exports.getEncoder = () => {
return version === '3.0' ? encoder_v3 : encoder_v4;
};

exports.getDecoder = () => {
return version === '3.0' ? decoder_v3 : decoder_v4;
};


exports.compile = (...args) => {
if (version === '3.0') {
return v3.compile(...args);
}
return v4.compile(...args);
};

exports.setVersion = ver => {
version = ver;
};
14 changes: 8 additions & 6 deletions lib/codec/hessian/v2.js → lib/codec/hessian/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
'use strict';

const EncoderV2 = require('hessian.js-1').EncoderV2;
const DecoderV2 = require('hessian.js-1').DecoderV2;
const appTypeMap = require('./app_type/app_type_map');
const encoder = new EncoderV2({ size: 1 });
const decoder = new DecoderV2();

const originByteBuffer = encoder.byteBuffer;
const { compile, getEncoder, getDecoder, setVersion } = require('./hessian');

/**
* 编码
Expand All @@ -15,13 +10,16 @@ const originByteBuffer = encoder.byteBuffer;
* @return {void}
*/
exports.encode = (byteBuffer, cmd) => {
const encoder = getEncoder();
const className = cmd.className;
const originByteBuffer = encoder.byteBuffer;

encoder.byteBuffer = originByteBuffer;
encoder.reset();
encoder.byteBuffer = byteBuffer;

appTypeMap[className].encode(cmd, encoder);
encoder.byteBuffer = originByteBuffer;
};

/**
Expand All @@ -33,6 +31,7 @@ exports.encode = (byteBuffer, cmd) => {
* @return {Object} 反序列化结果
*/
exports.decode = (byteBuffer, className, options) => {
const decoder = getDecoder();
className = className.trim();
decoder.byteBuffer = byteBuffer;
decoder.refMap = {};
Expand All @@ -47,3 +46,6 @@ exports.decode = (byteBuffer, className, options) => {
}
return decoder.read();
};

exports.setVersion = setVersion;
exports.compile = compile;
2 changes: 1 addition & 1 deletion lib/codec/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

module.exports = {
hessian2: require('./hessian/v2'),
hessian2: require('./hessian'),
protobuf: require('./protobuf'),
};
4 changes: 2 additions & 2 deletions lib/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ class ProtocolDecoder extends Writable {
return false;
}

_destroy() {
_destroy(err, callback) {
this._buf = null;
this.emit('close');
callback(err);
}
}

Expand Down
20 changes: 10 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
const assert = require('assert');
const ProtocolEncoder = require('./encoder');
const ProtocolDecoder = require('./decoder');
const compile = require('sofa-hessian-node').compile;
const { compile, setVersion } = require('./codec/hessian');
const builtinClazzMap = require('./codec/hessian/app_type/builtin_class_map');
const appTypeMap = require('./codec/hessian/app_type/app_type_map');
exports.BaseRpcCmd = require('./protocol/rpc_cmd');
exports.codec = require('./codec');

const globalOptions = {};

exports.setOptions = options => {
exports.setOptions = (options = {}) => {
if (options.hessianVersion) {
setVersion(options.hessianVersion);
}
Object.assign(globalOptions, options);
};

Expand All @@ -28,20 +31,17 @@ exports.registerAppClazzMap = function(clazzMap) {
const clazzs = Object.keys(clazzMap);
Object.assign(builtinClazzMap, clazzMap);
for (const clazz of clazzs) {
const encodeMap = {
hessian: compile({ type: clazz }, '1.0', builtinClazzMap),
hessian2: compile({ type: clazz }, '2.0', builtinClazzMap),
};

appTypeMap[clazz] = {
encode: (target, encoder) => {
encodeMap[ target.codecType ](target.obj, encoder);
encode(target, encoder) {
compile({ type: clazz }, '2.0', builtinClazzMap)(target.obj, encoder);
},
decode: decoder => {
decode(decoder) {
return decoder.readObject();
},
};
}
};

exports.setHessianVersion = setVersion;

exports.RpcClient = require('./rpc_client');
25 changes: 12 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,33 @@
"homepage": "https://github.com/alipay/sofa-bolt-node#readme",
"dependencies": {
"byte": "^2.0.0",
"connection": "^1.0.0",
"connection": "^1.1.0",
"crc": "^3.8.0",
"hessian.js-1": "^1.8.3",
"is-type-of": "^1.2.0",
"is-type-of": "^1.2.1",
"sdk-base": "^3.6.0",
"sofa-hessian-node": "^1.0.1",
"utility": "^1.14.0"
"sofa-hessian-node": "^2.0.0",
"utility": "^1.16.3"
},
"devDependencies": {
"antpb": "^1.0.0",
"autod": "^3.0.1",
"autod": "^3.1.0",
"await-event": "^2.1.0",
"benchmark": "^2.1.4",
"contributors": "^0.5.1",
"egg-bin": "^4.7.1",
"egg-ci": "^1.8.0",
"eslint": "^5.2.0",
"eslint-config-egg": "^7.0.0",
"egg-bin": "^4.14.1",
"egg-ci": "^1.15.0",
"eslint": "^7.2.0",
"eslint-config-egg": "^8.0.1",
"js-to-java": "^2.5.0",
"long": "^4.0.0",
"mm": "^2.2.2",
"mm": "^3.2.0",
"mz-modules": "^2.1.0",
"pump": "^3.0.0"
},
"engines": {
"node": ">= 8.0.0"
"node": ">= 10.0.0"
},
"ci": {
"version": "8, 10"
"version": "10, 12, 14"
}
}
2 changes: 1 addition & 1 deletion test/exception.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const assert = require('assert');
const ByteBuffer = require('byte');
const hessianV2 = require('../lib/codec/hessian/v2');
const hessianV2 = require('../lib/codec/hessian');

describe('test/exception.test.js', () => {
it('should decode java Exception ok', () => {
Expand Down
Loading

0 comments on commit 21de7ec

Please sign in to comment.