diff --git a/.autod.conf.js b/.autod.conf.js index e147cd8..36dcde8 100644 --- a/.autod.conf.js +++ b/.autod.conf.js @@ -1,20 +1,23 @@ -'use strict'; +'ues strict'; module.exports = { write: true, prefix: '^', devprefix: '^', - exclude: [], + exclude: [ + 'test/fixtures', + ], + dep: [ + 'long', + ], devdep: [ 'autod', - 'mm', - 'jshint', - 'mocha', - 'istanbul', - ], - semver: [ - 'mocha@3', - 'byte@1', - 'debug@3', + 'egg-bin', + 'egg-ci', + 'eslint', + 'eslint-config-egg', + 'contributors', ], + keep: [], + semver: [], }; diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..e115745 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,6 @@ +test/fixtures +coverage +examples/**/app/public +logs +run +benchmark diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..b517747 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,8 @@ +{ + "extends": "eslint-config-egg", + "rules": { + "no-bitwise": 0, + "jsdoc/require-returns-description": 0, + "jsdoc/require-param": 0 + } +} diff --git a/.jshintignore b/.jshintignore deleted file mode 100644 index 63c6333..0000000 --- a/.jshintignore +++ /dev/null @@ -1,4 +0,0 @@ -node_modules/ -coverage/ -.tmp/ -.git/ diff --git a/.jshintrc b/.jshintrc deleted file mode 100644 index fd00944..0000000 --- a/.jshintrc +++ /dev/null @@ -1,95 +0,0 @@ -{ - // JSHint Default Configuration File (as on JSHint website) - // See http://jshint.com/docs/ for more details - - "maxerr" : 50, // {int} Maximum error before stopping - - // Enforcing - "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 - "forin" : false, // true: Require filtering for..in loops with obj.hasOwnProperty() - "immed" : false, // true: Require immediate invocations to be wrapped in parens e.g. `(function () { } ());` - "indent" : false, // {int} Number of spaces to use for indentation - "latedef" : false, // true: Require variables/functions to be defined before being used - "newcap" : false, // true: Require capitalization of all constructor functions e.g. `new F()` - "noarg" : true, // true: Prohibit use of `arguments.caller` and `arguments.callee` - "noempty" : true, // true: Prohibit use of empty blocks - "nonew" : false, // true: Prohibit use of constructors for side-effects (without assignment) - "plusplus" : false, // true: Prohibit use of `++` & `--` - "quotmark" : false, // Quotation mark consistency: - // false : do nothing (default) - // true : ensure whatever is used is consistent - // "single" : require single quotes - // "double" : require double quotes - "undef" : true, // true: Require all non-global variables to be declared (prevents global leaks) - "unused" : false, // true: Require all defined variables be used - "strict" : true, // true: Requires all functions run in ES5 Strict Mode - "trailing" : false, // true: Prohibit trailing whitespaces - "maxparams" : false, // {int} Max number of formal params allowed per function - "maxdepth" : false, // {int} Max depth of nested blocks (within functions) - "maxstatements" : false, // {int} Max number statements per function - "maxcomplexity" : false, // {int} Max cyclomatic complexity per function - "maxlen" : false, // {int} Max number of characters per line - - // Relaxing - "asi" : false, // true: Tolerate Automatic Semicolon Insertion (no semicolons) - "boss" : true, // true: Tolerate assignments where comparisons would be expected - "debug" : false, // true: Allow debugger statements e.g. browser breakpoints. - "eqnull" : false, // true: Tolerate use of `== null` - "es5" : false, // true: Allow ES5 syntax (ex: getters and setters) - "esnext" : true, // true: Allow ES.next (ES6) syntax (ex: `const`) - "moz" : false, // true: Allow Mozilla specific syntax (extends and overrides esnext features) - // (ex: `for each`, multiple try/catch, function expression…) - "evil" : false, // true: Tolerate use of `eval` and `new Function()` - "expr" : true, // true: Tolerate `ExpressionStatement` as Programs - "funcscope" : false, // true: Tolerate defining variables inside control statements" - "globalstrict" : false, // true: Allow global "use strict" (also enables 'strict') - "iterator" : false, // true: Tolerate using the `__iterator__` property - "lastsemic" : false, // true: Tolerate omitting a semicolon for the last statement of a 1-line block - "laxbreak" : true, // true: Tolerate possibly unsafe line breakings - "laxcomma" : false, // true: Tolerate comma-first style coding - "loopfunc" : true, // true: Tolerate functions being defined in loops - "multistr" : true, // true: Tolerate multi-line strings - "proto" : false, // true: Tolerate using the `__proto__` property - "scripturl" : false, // true: Tolerate script-targeted URLs - "smarttabs" : false, // true: Tolerate mixed tabs/spaces when used for alignment - "shadow" : true, // true: Allows re-define variables later in code e.g. `var x=1; x=2;` - "sub" : false, // true: Tolerate using `[]` notation when it can still be expressed in dot notation - "supernew" : false, // true: Tolerate `new function () { ... };` and `new Object;` - "validthis" : true, // true: Tolerate using this in a non-constructor function - - // Environments - "browser" : true, // Web Browser (window, document, etc) - "couch" : false, // CouchDB - "devel" : true, // Development/debugging (alert, confirm, etc) - "dojo" : false, // Dojo Toolkit - "jquery" : false, // jQuery - "mootools" : false, // MooTools - "node" : true, // Node.js - "nonstandard" : false, // Widely adopted globals (escape, unescape, etc) - "prototypejs" : false, // Prototype and Scriptaculous - "rhino" : false, // Rhino - "worker" : false, // Web Workers - "wsh" : false, // Windows Scripting Host - "yui" : false, // Yahoo User Interface - "noyield" : true, // allow generators without a yield - - // Legacy - "nomen" : false, // true: Prohibit dangling `_` in variables - "onevar" : false, // true: Allow only one `var` statement per function - "passfail" : false, // true: Stop on first error - "white" : false, // true: Check against strict whitespace and indentation rules - - // Custom Globals - "globals" : { // additional predefined global variables - // mocha - "describe": true, - "it": true, - "before": true, - "afterEach": true, - "beforeEach": true, - "after": true - } -} diff --git a/.travis.yml b/.travis.yml index c38c794..39d409a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,12 @@ sudo: false language: node_js node_js: - - '10' - '8' - - '6' - - '4' -script: "make test-cov" -after_script: "npm i codecov && codecov" + - '10' + - '11' +install: + - npm i npminstall && npminstall +script: + - npm run ci +after_script: + - npminstall codecov && codecov diff --git a/Makefile b/Makefile deleted file mode 100644 index 238d0ac..0000000 --- a/Makefile +++ /dev/null @@ -1,39 +0,0 @@ -TESTS = test/*.test.js -REPORTER = spec -TIMEOUT = 10000 -MOCHA_OPTS = - -install: - @npm install - -jshint: - @./node_modules/.bin/jshint . - -test: - @NODE_ENV=test ./node_modules/.bin/mocha \ - --reporter $(REPORTER) \ - --timeout $(TIMEOUT) \ - $(MOCHA_OPTS) \ - $(TESTS) - -test-cov cov: install - @NODE_ENV=test node \ - node_modules/.bin/istanbul cover --preserve-comments \ - ./node_modules/.bin/_mocha \ - -- \ - --reporter $(REPORTER) \ - --timeout $(TIMEOUT) \ - $(MOCHA_OPTS) \ - $(TESTS) - -test-all: test test-cov jshint - -benchmark: - @node benchmark/encode.js - @node benchmark/decode.js - -autod: install - @./node_modules/.bin/autod -w -e benchmark --prefix "~" - @$(MAKE) install - -.PHONY: test benchmark diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 0000000..7cce29a --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,16 @@ +environment: + matrix: + - nodejs_version: '8' + - nodejs_version: '10' + - nodejs_version: '11' + +install: + - ps: Install-Product node $env:nodejs_version + - npm i npminstall && node_modules\.bin\npminstall + +test_script: + - node --version + - npm --version + - npm run test + +build: off diff --git a/benchmark/cache.js b/benchmark/cache.js index ea970cb..9cabfe9 100644 --- a/benchmark/cache.js +++ b/benchmark/cache.js @@ -1,40 +1,40 @@ 'use strict'; -var Benchmark = require('benchmark'); -var benchmarks = require('beautify-benchmark'); -var path = require('path'); -var fs = require('fs'); -var assert = require('assert'); -var hessian = require('../'); +const Benchmark = require('benchmark'); +const benchmarks = require('beautify-benchmark'); +const path = require('path'); +const fs = require('fs'); +const assert = require('assert'); +const hessian = require('../'); -var buf = fs.readFileSync(path.join(__dirname, 'buf.txt')); -var cache = new Map(); +const buf = fs.readFileSync(path.join(__dirname, 'buf.txt')); +const cache = new Map(); assert.deepEqual(hessian.decode(buf, '2.0', { classCache: cache }), hessian.decode(buf, '2.0')); assert.deepEqual(hessian.decode(buf, '2.0', { classCache: cache }), hessian.decode(buf, '2.0')); -var suite = new Benchmark.Suite(); +const suite = new Benchmark.Suite(); suite -.add('with cache', function() { - hessian.decode(buf, '2.0', { classCache: cache }); -}) -.add('without cache', function() { - hessian.decode(buf, '2.0'); -}) - -.on('cycle', function(event) { - benchmarks.add(event.target); -}) -.on('start', function(event) { - console.log('\n Cache Benchmark\n node version: %s, date: %s\n Starting...', - process.version, Date()); -}) -.on('complete', function done() { - benchmarks.log(); -}) -.run({ 'async': false }); + .add('with cache', function() { + hessian.decode(buf, '2.0', { classCache: cache }); + }) + .add('without cache', function() { + hessian.decode(buf, '2.0'); + }) + + .on('cycle', function(event) { + benchmarks.add(event.target); + }) + .on('start', function(event) { + console.log('\n Cache Benchmark\n node version: %s, date: %s\n Starting...', + process.version, Date()); + }) + .on('complete', function done() { + benchmarks.log(); + }) + .run({ async: false }); // Cache Benchmark // node version: v8.9.0, date: Thu Nov 16 2017 13:26:18 GMT+0800 (CST) diff --git a/benchmark/decode.js b/benchmark/decode.js index d5452b9..f53dc39 100644 --- a/benchmark/decode.js +++ b/benchmark/decode.js @@ -14,184 +14,184 @@ * Module dependencies. */ -var ByteBuffer = require('byte'); -var Benchmark = require('benchmark'); -var benchmarks = require('beautify-benchmark'); -var java = require('js-to-java'); -var hessian = require('../'); +const ByteBuffer = require('byte'); +const Benchmark = require('benchmark'); +const benchmarks = require('beautify-benchmark'); +const java = require('js-to-java'); +const hessian = require('../'); -var suite = new Benchmark.Suite(); +const suite = new Benchmark.Suite(); -var simpleObject = { +const simpleObject = { $class: 'com.hessiantest.org.MockRequest', $: { id: 123, name: 'getData', - args: [1, "name", "xxx1231231231231xxx123"] - } + args: [ 1, 'name', 'xxx1231231231231xxx123' ], + }, }; -var simpleObjectBuf1 = hessian.encode(simpleObject, '1.0'); -var simpleObjectBuf2 = hessian.encode(simpleObject, '2.0'); +const simpleObjectBuf1 = hessian.encode(simpleObject, '1.0'); +const simpleObjectBuf2 = hessian.encode(simpleObject, '2.0'); -var complexObject = { +const complexObject = { $class: 'com.hessiantest.org.MockRequest', $: { id: 123, name: 'getData', - args: [1, "name", "xxx1231231231231xxx123"], + args: [ 1, 'name', 'xxx1231231231231xxx123' ], conn: { $class: 'com.hessiantest.org.MockRequestConnection', $: { - ctx: java.long(1024) - } - } - } + ctx: java.long(1024), + }, + }, + }, }; -var complexObjectBuf1 = hessian.encode(complexObject, '1.0'); -var complexObjectBuf2 = hessian.encode(complexObject, '1.0'); +const complexObjectBuf1 = hessian.encode(complexObject, '1.0'); +const complexObjectBuf2 = hessian.encode(complexObject, '1.0'); -var number1Buf1 = hessian.encode(1, '1.0'); -var number1Buf2 = hessian.encode(1, '2.0'); +const number1Buf1 = hessian.encode(1, '1.0'); +const number1Buf2 = hessian.encode(1, '2.0'); -var dateBuf1 = hessian.encode(new Date(), '1.0'); -var dateBuf2 = hessian.encode(new Date(), '2.0'); +const dateBuf1 = hessian.encode(new Date(), '1.0'); +const dateBuf2 = hessian.encode(new Date(), '2.0'); -var longBuf1 = hessian.encode(java.long(300), '1.0'); -var longBuf2 = hessian.encode(java.long(300), '2.0'); +const longBuf1 = hessian.encode(java.long(300), '1.0'); +const longBuf2 = hessian.encode(java.long(300), '2.0'); -var stringBuf1 = hessian.encode('xxx1231231231231xxx123', '1.0'); -var stringBuf2 = hessian.encode('xxx1231231231231xxx123', '2.0'); +const stringBuf1 = hessian.encode('xxx1231231231231xxx123', '1.0'); +const stringBuf2 = hessian.encode('xxx1231231231231xxx123', '2.0'); -var arrBuf1 = hessian.encode([1, 2, 3], '1.0'); -var arrBuf2 = hessian.encode([1, 2, 3], '2.0'); +const arrBuf1 = hessian.encode([ 1, 2, 3 ], '1.0'); +const arrBuf2 = hessian.encode([ 1, 2, 3 ], '2.0'); -var arrObjectBuf1 = hessian.encode([1, "name", "xxx1231231231231xxx123"], '1.0'); -var arrObjectBuf2 = hessian.encode([1, "name", "xxx1231231231231xxx123"], '2.0'); +const arrObjectBuf1 = hessian.encode([ 1, 'name', 'xxx1231231231231xxx123' ], '1.0'); +const arrObjectBuf2 = hessian.encode([ 1, 'name', 'xxx1231231231231xxx123' ], '2.0'); suite -.add('hessian1 decode: number', function() { - hessian.decode(number1Buf1, '1.0'); -}) -.add('hessian2 decode: number', function() { - hessian.decode(number1Buf2, '2.0'); -}) - -.add('hessian1 decode: date', function() { - hessian.decode(dateBuf1, '1.0'); -}) -.add('hessian2 decode: date', function() { - hessian.decode(dateBuf2, '2.0'); -}) - -.add('hessian1 decode: long', function() { - hessian.decode(longBuf1, '1.0'); -}) -.add('hessian2 decode: long', function() { - hessian.decode(longBuf2, '2.0'); -}) - -.add('hessian1 decode: string', function() { - hessian.decode(stringBuf1, '1.0'); -}) -.add('hessian2 decode: string', function() { - hessian.decode(stringBuf2, '2.0'); -}) - -.add('hessian1 decode: [1, 2, 3]', function() { - hessian.decode(arrBuf1, '1.0'); -}) -.add('hessian2 decode: [1, 2, 3]', function() { - hessian.decode(arrBuf2, '2.0'); -}) -.add('hessian1 decode array', function() { - hessian.decode(arrObjectBuf1, '1.0'); -}) -.add('hessian2 decode array', function() { - hessian.decode(arrObjectBuf2, '2.0'); -}) - -.add('hessian1 decode: simple object', function() { - hessian.decode(simpleObjectBuf1, '1.0'); -}) -.add('hessian2 decode: simple object', function() { - hessian.decode(simpleObjectBuf2, '2.0'); -}) - -.add('hessian1 decode: complex object', function() { - hessian.decode(complexObjectBuf1, '1.0'); -}) -.add('hessian2 decode: complex object', function() { - hessian.decode(complexObjectBuf2, '2.0'); -}) -.add('hessian1 decode with type: number', function() { - hessian.decode(number1Buf1, '1.0', true); -}) -.add('hessian2 decode with type: number', function() { - hessian.decode(number1Buf2, '2.0', true); -}) - -.add('hessian1 decode with type: date', function() { - hessian.decode(dateBuf1, '1.0', true); -}) -.add('hessian2 decode with type: date', function() { - hessian.decode(dateBuf2, '2.0', true); -}) - -.add('hessian1 decode with type: long', function() { - hessian.decode(longBuf1, '1.0', true); -}) -.add('hessian2 decode with type: long', function() { - hessian.decode(longBuf2, '2.0', true); -}) - -.add('hessian1 decode with type: string', function() { - hessian.decode(stringBuf1, '1.0', true); -}) -.add('hessian2 decode with type: string', function() { - hessian.decode(stringBuf2, '2.0', true); -}) - -.add('hessian1 decode with type: [1, 2, 3]', function() { - hessian.decode(arrBuf1, '1.0', true); -}) -.add('hessian2 decode with type: [1, 2, 3]', function() { - hessian.decode(arrBuf2, '2.0', true); -}) -.add('hessian1 decode with type array', function() { - hessian.decode(arrObjectBuf1, '1.0', true); -}) -.add('hessian2 decode with type array', function() { - hessian.decode(arrObjectBuf2, '2.0', true); -}) - -.add('hessian1 decode with type: simple object', function() { - hessian.decode(simpleObjectBuf1, '1.0', true); -}) -.add('hessian2 decode with type: simple object', function() { - hessian.decode(simpleObjectBuf2, '2.0', true); -}) - -.add('hessian1 decode with type: complex object', function() { - hessian.decode(complexObjectBuf1, '1.0', true); -}) -.add('hessian2 decode with type: complex object', function() { - hessian.decode(complexObjectBuf2, '2.0', true); -}) - -.on('cycle', function(event) { - benchmarks.add(event.target); -}) -.on('start', function(event) { - console.log('\n Hessian Decode Benchmark\n node version: %s, date: %s\n Starting...', - process.version, Date()); -}) -.on('complete', function done() { - benchmarks.log(); -}) -.run({ 'async': false }); + .add('hessian1 decode: number', function() { + hessian.decode(number1Buf1, '1.0'); + }) + .add('hessian2 decode: number', function() { + hessian.decode(number1Buf2, '2.0'); + }) + + .add('hessian1 decode: date', function() { + hessian.decode(dateBuf1, '1.0'); + }) + .add('hessian2 decode: date', function() { + hessian.decode(dateBuf2, '2.0'); + }) + + .add('hessian1 decode: long', function() { + hessian.decode(longBuf1, '1.0'); + }) + .add('hessian2 decode: long', function() { + hessian.decode(longBuf2, '2.0'); + }) + + .add('hessian1 decode: string', function() { + hessian.decode(stringBuf1, '1.0'); + }) + .add('hessian2 decode: string', function() { + hessian.decode(stringBuf2, '2.0'); + }) + + .add('hessian1 decode: [1, 2, 3]', function() { + hessian.decode(arrBuf1, '1.0'); + }) + .add('hessian2 decode: [1, 2, 3]', function() { + hessian.decode(arrBuf2, '2.0'); + }) + .add('hessian1 decode array', function() { + hessian.decode(arrObjectBuf1, '1.0'); + }) + .add('hessian2 decode array', function() { + hessian.decode(arrObjectBuf2, '2.0'); + }) + + .add('hessian1 decode: simple object', function() { + hessian.decode(simpleObjectBuf1, '1.0'); + }) + .add('hessian2 decode: simple object', function() { + hessian.decode(simpleObjectBuf2, '2.0'); + }) + + .add('hessian1 decode: complex object', function() { + hessian.decode(complexObjectBuf1, '1.0'); + }) + .add('hessian2 decode: complex object', function() { + hessian.decode(complexObjectBuf2, '2.0'); + }) + .add('hessian1 decode with type: number', function() { + hessian.decode(number1Buf1, '1.0', true); + }) + .add('hessian2 decode with type: number', function() { + hessian.decode(number1Buf2, '2.0', true); + }) + + .add('hessian1 decode with type: date', function() { + hessian.decode(dateBuf1, '1.0', true); + }) + .add('hessian2 decode with type: date', function() { + hessian.decode(dateBuf2, '2.0', true); + }) + + .add('hessian1 decode with type: long', function() { + hessian.decode(longBuf1, '1.0', true); + }) + .add('hessian2 decode with type: long', function() { + hessian.decode(longBuf2, '2.0', true); + }) + + .add('hessian1 decode with type: string', function() { + hessian.decode(stringBuf1, '1.0', true); + }) + .add('hessian2 decode with type: string', function() { + hessian.decode(stringBuf2, '2.0', true); + }) + + .add('hessian1 decode with type: [1, 2, 3]', function() { + hessian.decode(arrBuf1, '1.0', true); + }) + .add('hessian2 decode with type: [1, 2, 3]', function() { + hessian.decode(arrBuf2, '2.0', true); + }) + .add('hessian1 decode with type array', function() { + hessian.decode(arrObjectBuf1, '1.0', true); + }) + .add('hessian2 decode with type array', function() { + hessian.decode(arrObjectBuf2, '2.0', true); + }) + + .add('hessian1 decode with type: simple object', function() { + hessian.decode(simpleObjectBuf1, '1.0', true); + }) + .add('hessian2 decode with type: simple object', function() { + hessian.decode(simpleObjectBuf2, '2.0', true); + }) + + .add('hessian1 decode with type: complex object', function() { + hessian.decode(complexObjectBuf1, '1.0', true); + }) + .add('hessian2 decode with type: complex object', function() { + hessian.decode(complexObjectBuf2, '2.0', true); + }) + + .on('cycle', function(event) { + benchmarks.add(event.target); + }) + .on('start', function(event) { + console.log('\n Hessian Decode Benchmark\n node version: %s, date: %s\n Starting...', + process.version, Date()); + }) + .on('complete', function done() { + benchmarks.log(); + }) + .run({ async: false }); // Hessian Decode Benchmark // node version: v8.9.0, date: Thu Nov 16 2017 13:22:44 GMT+0800 (CST) diff --git a/benchmark/decode_compile.js b/benchmark/decode_compile.js index 33c07a1..98dd108 100644 --- a/benchmark/decode_compile.js +++ b/benchmark/decode_compile.js @@ -43,7 +43,7 @@ suite .on('complete', function done() { benchmarks.log(); }) - .run({ 'async': false }); + .run({ async: false }); // Cache Benchmark // node version: v8.9.4, date: Thu Feb 01 2018 21:17:48 GMT+0800 (CST) diff --git a/benchmark/decode_more.js b/benchmark/decode_more.js index 3c42f75..c37e63c 100644 --- a/benchmark/decode_more.js +++ b/benchmark/decode_more.js @@ -14,40 +14,40 @@ * Module dependencies. */ -var ByteBuffer = require('byte'); -var Benchmark = require('benchmark'); -var benchmarks = require('beautify-benchmark'); -var java = require('js-to-java'); -var hessian = require('../'); +const ByteBuffer = require('byte'); +const Benchmark = require('benchmark'); +const benchmarks = require('beautify-benchmark'); +const java = require('js-to-java'); +const hessian = require('../'); -var suite = new Benchmark.Suite(); +const suite = new Benchmark.Suite(); -var simpleObject = { +const simpleObject = { $class: 'com.hessiantest.org.MockRequest', $: { id: 123, name: 'getData', - args: [1, "name", "xxx1231231231231xxx123"] - } + args: [ 1, 'name', 'xxx1231231231231xxx123' ], + }, }; -var simpleObjectBuf = hessian.encode(simpleObject, '2.0'); +const simpleObjectBuf = hessian.encode(simpleObject, '2.0'); -var complexObject = { +const complexObject = { $class: 'com.hessiantest.org.MockRequest', $: { id: 123, name: 'getData', - args: [1, "name", "xxx1231231231231xxx123"], + args: [ 1, 'name', 'xxx1231231231231xxx123' ], conn: { $class: 'com.hessiantest.org.MockRequestConnection', $: { - ctx: java.long(1024) - } - } - } + ctx: java.long(1024), + }, + }, + }, }; -var complexObjectBuf = hessian.encode(complexObject, '2.0'); +const complexObjectBuf = hessian.encode(complexObject, '2.0'); const options = { classCache: new Map(), @@ -88,7 +88,7 @@ suite .on('complete', function done() { benchmarks.log(); }) - .run({ 'async': false }); + .run({ async: false }); // Hessian Decode Benchmark // node version: v8.9.0, date: Thu Nov 16 2017 13:22:44 GMT+0800 (CST) diff --git a/benchmark/encode.js b/benchmark/encode.js index 0c9a568..9359842 100644 --- a/benchmark/encode.js +++ b/benchmark/encode.js @@ -14,106 +14,106 @@ * Module dependencies. */ -var ByteBuffer = require('byte'); -var Benchmark = require('benchmark'); -var benchmarks = require('beautify-benchmark'); -var java = require('js-to-java'); -var hessian = require('../'); +const ByteBuffer = require('byte'); +const Benchmark = require('benchmark'); +const benchmarks = require('beautify-benchmark'); +const java = require('js-to-java'); +const hessian = require('../'); -var suite = new Benchmark.Suite(); +const suite = new Benchmark.Suite(); -var simpleObject = { +const simpleObject = { $class: 'com.hessiantest.org.MockRequest', $: { id: 123, name: 'getData', - args: [1, "name", "xxx1231231231231xxx123"] - } + args: [ 1, 'name', 'xxx1231231231231xxx123' ], + }, }; -var complexObject = { +const complexObject = { $class: 'com.hessiantest.org.MockRequest', $: { id: 123, name: 'getData', - args: [1, "name", "xxx1231231231231xxx123"], + args: [ 1, 'name', 'xxx1231231231231xxx123' ], conn: { $class: 'com.hessiantest.org.MockRequestConnection', $: { - ctx: java.long(1024) - } - } - } + ctx: java.long(1024), + }, + }, + }, }; suite -.add('hessian1 encode: number', function() { - hessian.encode(1, '1.0'); -}) -.add('hessian2 encode: number', function() { - hessian.encode(1, '2.0'); -}) - -.add('hessian1 encode: date', function() { - hessian.encode(new Date(), '1.0'); -}) -.add('hessian2 encode: date', function() { - hessian.encode(new Date(), '2.0'); -}) - -.add('hessian1 encode: long', function() { - hessian.encode(java.long(300), '1.0'); -}) -.add('hessian2 encode: long', function() { - hessian.encode(java.long(300), '2.0'); -}) - -.add('hessian1 encode: string', function() { - hessian.encode('xxx1231231231231xxx123', '1.0'); -}) -.add('hessian2 encode: string', function() { - hessian.encode('xxx1231231231231xxx123', '2.0'); -}) - -.add('hessian1 encode: [1, 2, 3]', function() { - hessian.encode([1, 2, 3], '1.0'); -}) -.add('hessian2 encode: [1, 2, 3]', function() { - hessian.encode([1, 2, 3], '2.0'); -}) -.add('hessian1 encode array', function() { - hessian.encode([1, "name", "xxx1231231231231xxx123"], '1.0'); -}) -.add('hessian2 encode array', function() { - hessian.encode([1, "name", "xxx1231231231231xxx123"], '2.0'); -}) - -.add('hessian1 encode: simple object', function() { - hessian.encode(simpleObject, '1.0'); -}) -.add('hessian2 encode: simple object', function() { - hessian.encode(simpleObject, '2.0'); -}) - -.add('hessian1 encode: complex object', function() { - hessian.encode(complexObject, '1.0'); -}) -.add('hessian2 encode: complex object', function() { - hessian.encode(complexObject, '2.0'); -}) - -.on('cycle', function(event) { - benchmarks.add(event.target); -}) -.on('start', function(event) { - console.log('\n Hessian Encode Benchmark\n node version: %s, date: %s\n Starting...', - process.version, Date()); -}) -.on('complete', function done() { - benchmarks.log(); -}) -.run({ 'async': false }); + .add('hessian1 encode: number', function() { + hessian.encode(1, '1.0'); + }) + .add('hessian2 encode: number', function() { + hessian.encode(1, '2.0'); + }) + + .add('hessian1 encode: date', function() { + hessian.encode(new Date(), '1.0'); + }) + .add('hessian2 encode: date', function() { + hessian.encode(new Date(), '2.0'); + }) + + .add('hessian1 encode: long', function() { + hessian.encode(java.long(300), '1.0'); + }) + .add('hessian2 encode: long', function() { + hessian.encode(java.long(300), '2.0'); + }) + + .add('hessian1 encode: string', function() { + hessian.encode('xxx1231231231231xxx123', '1.0'); + }) + .add('hessian2 encode: string', function() { + hessian.encode('xxx1231231231231xxx123', '2.0'); + }) + + .add('hessian1 encode: [1, 2, 3]', function() { + hessian.encode([ 1, 2, 3 ], '1.0'); + }) + .add('hessian2 encode: [1, 2, 3]', function() { + hessian.encode([ 1, 2, 3 ], '2.0'); + }) + .add('hessian1 encode array', function() { + hessian.encode([ 1, 'name', 'xxx1231231231231xxx123' ], '1.0'); + }) + .add('hessian2 encode array', function() { + hessian.encode([ 1, 'name', 'xxx1231231231231xxx123' ], '2.0'); + }) + + .add('hessian1 encode: simple object', function() { + hessian.encode(simpleObject, '1.0'); + }) + .add('hessian2 encode: simple object', function() { + hessian.encode(simpleObject, '2.0'); + }) + + .add('hessian1 encode: complex object', function() { + hessian.encode(complexObject, '1.0'); + }) + .add('hessian2 encode: complex object', function() { + hessian.encode(complexObject, '2.0'); + }) + + .on('cycle', function(event) { + benchmarks.add(event.target); + }) + .on('start', function(event) { + console.log('\n Hessian Encode Benchmark\n node version: %s, date: %s\n Starting...', + process.version, Date()); + }) + .on('complete', function done() { + benchmarks.log(); + }) + .run({ async: false }); // node version: v0.11.12, date: Tue May 13 2014 10:30:18 GMT+0800 (CST) // Starting... diff --git a/benchmark/reg.js b/benchmark/reg.js index 73100cb..8bd507e 100644 --- a/benchmark/reg.js +++ b/benchmark/reg.js @@ -1,44 +1,44 @@ 'use strict'; -var Benchmark = require('benchmark'); -var benchmarks = require('beautify-benchmark'); +const Benchmark = require('benchmark'); +const benchmarks = require('beautify-benchmark'); -var suite = new Benchmark.Suite(); +const suite = new Benchmark.Suite(); -var INNER_CLASS_PROPERTY_REG = /^this\$\d+$/; -var INNER_CLASS_LABEL = '$$ignore_inner_property$$'; +const INNER_CLASS_PROPERTY_REG = /^this\$\d+$/; +const INNER_CLASS_LABEL = '$$ignore_inner_property$$'; -var name1 = 'foobar'; -var name2 = 'this$123'; -var name3 = INNER_CLASS_LABEL; +const name1 = 'foobar'; +const name2 = 'this$123'; +const name3 = INNER_CLASS_LABEL; suite -.add('dynamic', function() { - /^this\$\d+/.test(name1); - /^this\$\d+/.test(name2); -}) -.add('static', function() { - INNER_CLASS_PROPERTY_REG.test(name1); - INNER_CLASS_PROPERTY_REG.test(name2); -}) -.add('equal', function() { - name1 === INNER_CLASS_LABEL; - name3 === INNER_CLASS_LABEL; -}) - -.on('cycle', function(event) { - benchmarks.add(event.target); -}) -.on('start', function(event) { - console.log('\n Reg Benchmark\n node version: %s, date: %s\n Starting...', - process.version, Date()); -}) -.on('complete', function done() { - benchmarks.log(); -}) -.run({ 'async': false }); + .add('dynamic', function() { + /^this\$\d+/.test(name1); + /^this\$\d+/.test(name2); + }) + .add('static', function() { + INNER_CLASS_PROPERTY_REG.test(name1); + INNER_CLASS_PROPERTY_REG.test(name2); + }) + .add('equal', function() { + name1 === INNER_CLASS_LABEL; + name3 === INNER_CLASS_LABEL; + }) + + .on('cycle', function(event) { + benchmarks.add(event.target); + }) + .on('start', function(event) { + console.log('\n Reg Benchmark\n node version: %s, date: %s\n Starting...', + process.version, Date()); + }) + .on('complete', function done() { + benchmarks.log(); + }) + .run({ async: false }); // node version: v8.5.0, date: Sat Oct 21 2017 08:00:33 GMT+0800 (CST) // Starting... diff --git a/benchmark/require.js b/benchmark/require.js new file mode 100644 index 0000000..b1a09e4 --- /dev/null +++ b/benchmark/require.js @@ -0,0 +1,5 @@ +'use strict'; + +console.time('hessian.js-1'); +require('..'); +console.timeEnd('hessian.js-1'); diff --git a/benchmark/string.js b/benchmark/string.js index fc6491f..75978fb 100644 --- a/benchmark/string.js +++ b/benchmark/string.js @@ -47,7 +47,7 @@ assert(getUTF(buf) === buf.toString()); function getUTF2(buf) { const length = buf.length; const data = []; - let start = 0; + const start = 0; const numInts = length >> 2; for (let i = 0; i < numInts; i++) { const num = buf.readInt32BE(i * 4); @@ -106,7 +106,7 @@ suite .on('complete', function done() { benchmarks.log(); }) - .run({ 'async': false }); + .run({ async: false }); // Cache Benchmark // node version: v8.9.0, date: Thu Nov 16 2017 13:26:18 GMT+0800 (CST) diff --git a/index.js b/index.js index 17ad916..c5b51f6 100644 --- a/index.js +++ b/index.js @@ -1,32 +1,21 @@ -/**! - * hessian.js - index.js - * - * Copyright(c) 2014 - * MIT Licensed - * - * Authors: - * dead_horse (http://deadhorse.me) - * fengmk2 (http://fengmk2.github.com) - */ - 'use strict'; -var EncoderV1 = exports.EncoderV1 = exports.Encoder = require('./lib/v1/encoder'); -var DecoderV1 = exports.DecoderV1 = exports.Decoder = require('./lib/v1/decoder'); +const EncoderV1 = exports.EncoderV1 = exports.Encoder = require('./lib/v1/encoder'); +const DecoderV1 = exports.DecoderV1 = exports.Decoder = require('./lib/v1/decoder'); -var EncoderV2 = exports.EncoderV2 = require('./lib/v2/encoder'); -var DecoderV2 = exports.DecoderV2 = require('./lib/v2/decoder'); +const EncoderV2 = exports.EncoderV2 = require('./lib/v2/encoder'); +const DecoderV2 = exports.DecoderV2 = require('./lib/v2/decoder'); -exports.encoderV1 = new EncoderV1({size: 1024 * 1024}); -exports.encoderV2 = new EncoderV2({size: 1024 * 1024}); +exports.encoderV1 = new EncoderV1({ size: 1024 * 1024 }); +exports.encoderV2 = new EncoderV2({ size: 1024 * 1024 }); exports.decode = function decode(buf, version, options) { - var classCache; - var withType; + let classCache; + let withType; if (version && typeof version !== 'string') { // buf, withType, version - var t = version; + const t = version; version = options; options = t; } @@ -47,7 +36,7 @@ exports.decode = function decode(buf, version, options) { }; exports.encode = function encode(obj, version) { - var encoder; + let encoder; if (version === '2.0') { encoder = exports.encoderV2; } else { diff --git a/lib/debug.js b/lib/debug.js new file mode 100644 index 0000000..a172859 --- /dev/null +++ b/lib/debug.js @@ -0,0 +1,9 @@ +'use strict'; + +const noop = () => {}; + +if ('DEBUG' in process.env) { + module.exports = require('debug'); +} else { + module.exports = () => noop; +} diff --git a/lib/is_type_of.js b/lib/is_type_of.js new file mode 100644 index 0000000..1215c38 --- /dev/null +++ b/lib/is_type_of.js @@ -0,0 +1,28 @@ +'use strict'; + +const MAX_INT_31 = Math.pow(2, 31); + +exports.number = function(arg) { + return typeof arg === 'number'; +}; + +exports.int = function(obj) { + return exports.number(obj) && + obj % 1 === 0; +}; + +exports.int32 = function(obj) { + return exports.int(obj) && + obj < MAX_INT_31 && + obj >= -MAX_INT_31; +}; + +exports.string = function(arg) { + return typeof arg === 'string'; +}; + +exports.object = function(arg) { + return typeof arg === 'object' && arg !== null; +}; + +exports.buffer = Buffer.isBuffer; diff --git a/lib/object.js b/lib/object.js index f3e8048..4bde53f 100644 --- a/lib/object.js +++ b/lib/object.js @@ -1,17 +1,10 @@ -/**! - * hessian.js - lib/object.js - * Copyright(c) 2014 - * MIT Licensed - * - * Authors: - * dead_horse (http://deadhorse.me) - * fengmk2 (http://fengmk2.github.com) - */ - 'use strict'; -var util = require('util'); -var has = require('utility').has; +const util = require('util'); + +function has(obj, prop) { + return Object.prototype.hasOwnProperty.call(obj, prop); +} exports.DEFAULT_CLASSNAME = { boolean: 'boolean', @@ -24,18 +17,18 @@ exports.DEFAULT_CLASSNAME = { list: 'java.util.ArrayList', map: 'java.util.HashMap', iMap: 'java.util.Map', - exception: 'java.lang.RuntimeException' + exception: 'java.lang.RuntimeException', }; exports.Object = 'java.lang.Object'; -var SERIALIZER_MAP = exports.SERIALIZER_MAP = {}; +const SERIALIZER_MAP = exports.SERIALIZER_MAP = {}; [ 'boolean', 'java.lang.Boolean', 'bool', -].forEach(function (t) { +].forEach(function(t) { SERIALIZER_MAP[t] = 'Bool'; }); @@ -44,14 +37,14 @@ var SERIALIZER_MAP = exports.SERIALIZER_MAP = {}; 'java.lang.Double', 'float', 'java.lang.Float', -].forEach(function (t) { +].forEach(function(t) { SERIALIZER_MAP[t] = 'Double'; }); [ 'java.lang.Long', 'long', -].forEach(function (t) { +].forEach(function(t) { SERIALIZER_MAP[t] = 'Long'; }); @@ -62,7 +55,7 @@ var SERIALIZER_MAP = exports.SERIALIZER_MAP = {}; 'java.lang.Integer', 'byte', 'java.lang.Byte', -].forEach(function (t) { +].forEach(function(t) { SERIALIZER_MAP[t] = 'Int'; }); @@ -73,13 +66,13 @@ var SERIALIZER_MAP = exports.SERIALIZER_MAP = {}; 'char', 'char[]', 'java.lang.Character', -].forEach(function (t) { +].forEach(function(t) { SERIALIZER_MAP[t] = 'String'; }); [ - 'java.util.Date' -].forEach(function (t) { + 'java.util.Date', +].forEach(function(t) { SERIALIZER_MAP[t] = 'Date'; }); @@ -95,16 +88,16 @@ function JavaExceptionError(obj, withType) { // methodName: 'main', // fileName: 'Main.java', // lineNumber: 1282 } ] } - var undeclaredThrowable = obj.$.undeclaredThrowable; + let undeclaredThrowable = obj.$.undeclaredThrowable; if (undeclaredThrowable && withType) { undeclaredThrowable = undeclaredThrowable.$; } - var detailMessage = obj.$.detailMessage; + let detailMessage = obj.$.detailMessage; if (detailMessage && withType) { detailMessage = detailMessage.$; } - var cause = obj.$.cause; + let cause = obj.$.cause; if (cause && cause.$ && withType) { cause = cause.$; } @@ -143,22 +136,22 @@ function JavaExceptionError(obj, withType) { value: obj.$.cause, }); - var stack = this.name + ': ' + this.message; + let stack = this.name + ': ' + this.message; if (withType) { - var stackTraces = obj.$.stackTrace && obj.$.stackTrace.$ || []; - for (var i = 0; i < stackTraces.length; i++) { - var trace = stackTraces[i].$; - stack += '\n at ' + (trace.declaringClass && trace.declaringClass.$) - + '.' + (trace.methodName && trace.methodName.$) - + ' (' + (trace.fileName && trace.fileName.$) - + ':' + (trace.lineNumber && trace.lineNumber.$) + ')'; + const stackTraces = obj.$.stackTrace && obj.$.stackTrace.$ || []; + for (let i = 0; i < stackTraces.length; i++) { + const trace = stackTraces[i].$; + stack += '\n at ' + (trace.declaringClass && trace.declaringClass.$) + + '.' + (trace.methodName && trace.methodName.$) + + ' (' + (trace.fileName && trace.fileName.$) + + ':' + (trace.lineNumber && trace.lineNumber.$) + ')'; } } else { - var stackTraces = obj.$.stackTrace || []; - for (var i = 0; i < stackTraces.length; i++) { - var trace = stackTraces[i]; - stack += '\n at ' + trace.declaringClass + '.' + trace.methodName - + ' (' + trace.fileName + ':' + trace.lineNumber + ')'; + const stackTraces = obj.$.stackTrace || []; + for (let i = 0; i < stackTraces.length; i++) { + const trace = stackTraces[i]; + stack += '\n at ' + trace.declaringClass + '.' + trace.methodName + + ' (' + trace.fileName + ':' + trace.lineNumber + ')'; } } @@ -167,7 +160,7 @@ function JavaExceptionError(obj, withType) { util.inherits(JavaExceptionError, Error); -exports.isJavaException = function (obj) { +exports.isJavaException = function(obj) { if (has(obj, 'detailMessage') && has(obj, 'stackTrace')) { return true; } diff --git a/lib/utils.js b/lib/utils.js index 16629b1..c4e0499 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,27 +1,18 @@ -/**! - * hessian.js - lib/utils.js - * Copyright(c) 2014 - * MIT Licensed - * - * Authors: - * dead_horse (http://deadhorse.me) - */ - 'use strict'; -var debug = require('debug')('hessian.js:utils'); -var object = require('./object'); +const debug = require('./debug')('hessian.js:utils'); +const object = require('./object'); -var MAX_INT_8 = exports.MAX_INT_8 = Math.pow(2, 7); -var MAX_INT_16 = exports.MAX_INT_16 = Math.pow(2, 15); -var MAX_INT_32 = exports.MAX_INT_32 = Math.pow(2, 31); -var MAX_INT = exports.MAX_INT = Math.pow(2, 53); -var MAX_INT_HIGH = exports.MAX_INT_HIGH = Math.pow(2, 21); +exports.MAX_INT_8 = Math.pow(2, 7); +exports.MAX_INT_16 = Math.pow(2, 15); +exports.MAX_INT_32 = Math.pow(2, 31); +exports.MAX_INT = Math.pow(2, 53); +const MAX_INT_HIGH = exports.MAX_INT_HIGH = Math.pow(2, 21); -var MAX_BYTE_TRUNK_SIZE = exports.MAX_BYTE_TRUNK_SIZE = 0x8000; -var MAX_CHAR_TRUNK_SIZE = exports.MAX_CHAR_TRUNK_SIZE = 0x8000; +exports.MAX_BYTE_TRUNK_SIZE = 0x8000; +exports.MAX_CHAR_TRUNK_SIZE = 0x8000; -exports.getSerializer = function (type) { +exports.getSerializer = function(type) { // get from SERIALIZER_MAP if (object.SERIALIZER_MAP[type]) { return 'write' + object.SERIALIZER_MAP[type]; @@ -34,15 +25,15 @@ exports.getSerializer = function (type) { return 'writeObject'; }; -exports.isJavaObject = function (type) { +exports.isJavaObject = function(type) { return type === object.Object; }; -exports.handleLong = function (val) { - var notSafeInt = val.high > MAX_INT_HIGH || // bigger than 2^54 - (val.high === MAX_INT_HIGH && val.low > 0) || // between 2^53 ~ 2^54 - val.high < -1 * MAX_INT_HIGH || // smaller than -2^54 - (val.high === -1 * MAX_INT_HIGH && val.low < 0);// between -2^54 ~ -2^53 +exports.handleLong = function(val) { + const notSafeInt = val.high > MAX_INT_HIGH || // bigger than 2^54 + (val.high === MAX_INT_HIGH && val.low > 0) || // between 2^53 ~ 2^54 + val.high < -1 * MAX_INT_HIGH || // smaller than -2^54 + (val.high === -1 * MAX_INT_HIGH && val.low < 0); // between -2^54 ~ -2^53 if (notSafeInt) { debug('[hessian.js Warning] Read a not safe long, translate it to string'); @@ -51,19 +42,19 @@ exports.handleLong = function (val) { return val.toNumber(); }; -var _hasOwnProperty = Object.prototype.hasOwnProperty; +const _hasOwnProperty = Object.prototype.hasOwnProperty; /* jshint -W001 */ exports.hasOwnProperty = function hasOwnProperty(obj, property) { return _hasOwnProperty.call(obj, property); }; exports.addByteCodes = function addByteCodes(map, codes, method) { - for (var i = 0; i < codes.length; i++) { - var code = codes[i]; + for (let i = 0; i < codes.length; i++) { + const code = codes[i]; if (Array.isArray(code)) { - var startCode = code[0]; - var endCode = code[1]; - for (var c = startCode; c <= endCode; c++) { + const startCode = code[0]; + const endCode = code[1]; + for (let c = startCode; c <= endCode; c++) { map[c] = method; } } else { @@ -71,3 +62,43 @@ exports.addByteCodes = function addByteCodes(map, codes, method) { } } }; + +const MAX_SAFE_INTEGER_STR = '9007199254740991'; +const MAX_SAFE_INTEGER_STR_LENGTH = 16; // '9007199254740991'.length + +/** + * Detect a number string can safe convert to Javascript Number. + * + * @param {String} s number format string, like `"123"`, `"-1000123123123123123123"` + * @return {Boolean} * + */ +exports.isSafeNumberString = function isSafeNumberString(s) { + if (s[0] === '-') { + s = s.substring(1); + } + if (s.length < MAX_SAFE_INTEGER_STR_LENGTH || + (s.length === MAX_SAFE_INTEGER_STR_LENGTH && s <= MAX_SAFE_INTEGER_STR)) { + return true; + } + return false; +}; + +function isRegExp(re) { + return objectToString(re) === '[object RegExp]'; +} +exports.isRegExp = isRegExp; + +function isDate(d) { + return objectToString(d) === '[object Date]'; +} +exports.isDate = isDate; + +function isLong(obj) { + return obj && typeof obj.high === 'number' && typeof obj.low === 'number'; +} +exports.isLong = isLong; + + +function objectToString(o) { + return Object.prototype.toString.call(o); +} diff --git a/lib/v1/decoder.js b/lib/v1/decoder.js index b1e929b..e6971cf 100644 --- a/lib/v1/decoder.js +++ b/lib/v1/decoder.js @@ -1,24 +1,13 @@ -/**! - * hessian.js - lib/v1/decoder.js - * Copyright(c) 2014 - * MIT Licensed - * - * Authors: - * dead_horse (http://deadhorse.me) - * fengmk2 (http://fengmk2.github.com) - */ - 'use strict'; -var debug = require('debug')('hessian:v1:decoder'); -var ByteBuffer = require('byte'); -var is = require('is-type-of'); -var utils = require('../utils'); -var object = require('../object'); -var isJavaException = object.isJavaException; -var JavaExceptionError = object.JavaExceptionError; +const debug = require('../debug')('hessian:v1:decoder'); +const ByteBuffer = require('byte'); +const utils = require('../utils'); +const object = require('../object'); +const isJavaException = object.isJavaException; +const JavaExceptionError = object.JavaExceptionError; -var BYTE_CODES = {}; +const BYTE_CODES = {}; function Decoder(buf) { this.byteBuffer = buf ? ByteBuffer.wrap(buf) : null; @@ -31,31 +20,29 @@ function Decoder(buf) { * prototype of Decoder */ -var proto = Decoder.prototype; +const proto = Decoder.prototype; -proto.throwError = function (method, code) { +proto.throwError = function(method, code) { throw new TypeError('hessian ' + method + ' error, unexpect code: 0x' + code.toString(16)); }; -proto._addRef = function (obj) { +proto._addRef = function(obj) { this.refMap[this.refId++] = obj; }; /** * init from a buffer - * @param {Buffer} buf - * @api public + * @param {Buffer} buf - buf */ -proto.init = function (buf) { +proto.init = function(buf) { this.byteBuffer = ByteBuffer.wrap(buf); return this; }; /** * clean the decoder - * @api public */ -proto.clean = function () { +proto.clean = function() { this.byteBuffer = new ByteBuffer(); this.refMap = {}; this.refId = 0; @@ -64,11 +51,13 @@ proto.clean = function () { /** * check if the label match the method - * @api private + * @param {String} method - method + * @param {String} label - label + * @return {String} char */ -proto._checkLabel = function (method, label) { - var l = this.byteBuffer.getChar(); - var labelIsOk = l === label || label.indexOf(l) >= 0; +proto._checkLabel = function(method, label) { + const l = this.byteBuffer.getChar(); + const labelIsOk = l === label || label.indexOf(l) >= 0; if (!labelIsOk) { throw new TypeError('hessian ' + method + ' only accept label `' + label + '` but got unexpect label `' + l + '`'); @@ -77,7 +66,7 @@ proto._checkLabel = function (method, label) { }; proto.handleType = function(type, val, withType) { - return withType ? {$class: object.DEFAULT_CLASSNAME[type], $: val} : val; + return withType ? { $class: object.DEFAULT_CLASSNAME[type], $: val } : val; }; /** @@ -89,9 +78,8 @@ proto.handleType = function(type, val, withType) { * ``` * * @return {Null} - * @api public */ -proto.readNull = function () { +proto.readNull = function() { this._checkLabel('readNull', 'N'); return null; }; @@ -110,11 +98,10 @@ utils.addByteCodes(BYTE_CODES, [ * ``` * * @return {Boolean} - * @api public */ -proto.readBool = function (withType) { - var label = this._checkLabel('readBool', ['T', 'F']); - var val = label === 'T'; +proto.readBool = function(withType) { + const label = this._checkLabel('readBool', [ 'T', 'F' ]); + const val = label === 'T'; return this.handleType('boolean', val, withType); }; @@ -132,16 +119,15 @@ utils.addByteCodes(BYTE_CODES, [ * ``` * * @return {Number} - * @api public */ -proto.readInt = function (withType) { +proto.readInt = function(withType) { this._checkLabel('readInt', 'I'); - var val = this.byteBuffer.getInt(); + const val = this.byteBuffer.getInt(); return this.handleType('int', val, withType); }; utils.addByteCodes(BYTE_CODES, [ - 0x49 + 0x49, ], 'readInt'); /** @@ -153,16 +139,15 @@ utils.addByteCodes(BYTE_CODES, [ * ``` * * @return {Number} - * @api public */ -proto.readLong = function (withType) { +proto.readLong = function(withType) { this._checkLabel('readLong', 'L'); - var val = utils.handleLong(this.byteBuffer.getLong()); + const val = utils.handleLong(this.byteBuffer.getLong()); return this.handleType('long', val, withType); }; utils.addByteCodes(BYTE_CODES, [ - 0x4c + 0x4c, ], 'readLong'); /** @@ -174,16 +159,15 @@ utils.addByteCodes(BYTE_CODES, [ * ``` * * @return {Number} - * @api public */ -proto.readDouble = function (withType) { +proto.readDouble = function(withType) { this._checkLabel('readDouble', 'D'); - var val = this.byteBuffer.getDouble(); + const val = this.byteBuffer.getDouble(); return this.handleType('double', val, withType); }; utils.addByteCodes(BYTE_CODES, [ - 0x44 + 0x44, ], 'readDouble'); /** @@ -196,18 +180,17 @@ utils.addByteCodes(BYTE_CODES, [ * Date represented by a 64-bit long of milliseconds since Jan 1 1970 00:00H, UTC. * * @return {Date} - * @api public */ -proto.readDate = function (withType) { +proto.readDate = function(withType) { this._checkLabel('readDate', 'd'); - var date = utils.handleLong(this.byteBuffer.getLong()); + const date = utils.handleLong(this.byteBuffer.getLong()); debug('read a date with milliEpoch: %d', date); - var val = new Date(date); + const val = new Date(date); return this.handleType('date', val, withType); }; utils.addByteCodes(BYTE_CODES, [ - 0x64 + 0x64, ], 'readDate'); /** @@ -222,17 +205,16 @@ utils.addByteCodes(BYTE_CODES, [ * 'b' represents any initial chunk. Each chunk has a 16-bit length value. * * @return {Buffer} - * @api public */ -proto.readBytes = function () { - var label = this._checkLabel('readBytes', ['b', 'B']); - var bufs = []; - var length = 0; +proto.readBytes = function() { + let label = this._checkLabel('readBytes', [ 'b', 'B' ]); + const bufs = []; + let length = 0; // get all trunk start with 'b' while (label === 'b') { length = this.byteBuffer.getUInt16(); bufs.push(this.byteBuffer.read(length)); - label = this._checkLabel('readBytes', ['b', 'B']); + label = this._checkLabel('readBytes', [ 'b', 'B' ]); } // get the last trunk start with 'B' length = this.byteBuffer.getUInt16(); @@ -243,11 +225,11 @@ proto.readBytes = function () { utils.addByteCodes(BYTE_CODES, [ 0x62, - 0x42 + 0x42, ], 'readBytes'); -proto._readUTF8String = function (len) { - if (!is.number(len)) { +proto._readUTF8String = function(len) { + if (typeof len !== 'number') { len = this.byteBuffer.getUInt16(); } if (len === 0) { @@ -267,11 +249,10 @@ proto._readUTF8String = function (len) { * ``` * * @return {String} - * @api public */ -proto.readString = function (withType) { - var str = ''; - var code = this.byteBuffer.get(); +proto.readString = function(withType) { + let str = ''; + let code = this.byteBuffer.get(); // get all trunk start with 's' while (code === 0x73) { str += this._readUTF8String(); @@ -291,7 +272,7 @@ proto.readString = function (withType) { utils.addByteCodes(BYTE_CODES, [ 0x73, - 0x53 + 0x53, ], 'readString'); /** @@ -303,27 +284,27 @@ utils.addByteCodes(BYTE_CODES, [ * @param {Boolean} skip skip type, if true, will return empty string * @return {String} type string */ -proto.readType = function (skip) { +proto.readType = function(skip) { this._checkLabel('readType', 't'); - var typeLength = this.byteBuffer.getUInt16(); + const typeLength = this.byteBuffer.getUInt16(); if (skip) { this.byteBuffer.skip(typeLength); debug('ignore type, skip %d bytes', typeLength); return ''; - } else { - var type = this.byteBuffer.readRawString(typeLength); - debug('get type: %s', type); - return type; } + const type = this.byteBuffer.readRawString(typeLength); + debug('get type: %s', type); + return type; + }; utils.addByteCodes(BYTE_CODES, [ 0x74, ], 'readType'); -proto.readLength = function () { +proto.readLength = function() { this._checkLabel('readLength', 'l'); // x6c - var len = this.byteBuffer.getUInt(); + const len = this.byteBuffer.getUInt(); debug('read length: %s', len); return len; }; @@ -336,13 +317,13 @@ utils.addByteCodes(BYTE_CODES, [ * A sparse array, hessian v1.0 * http://hessian.caucho.com/doc/hessian-1.0-spec.xtp#map */ -proto._readSparseObject = function (withType) { - var obj = {}; - var label = this.byteBuffer.getChar(this.byteBuffer.position()); +proto._readSparseObject = function(withType) { + const obj = {}; + let label = this.byteBuffer.getChar(this.byteBuffer.position()); while (label !== 'z') { debug('sparse array label: %s', label); - var key = this.read(withType); - var val = this.read(withType); + const key = this.read(withType); + const val = this.read(withType); obj[key] = val; label = this.byteBuffer.getChar(this.byteBuffer.position()); } @@ -353,7 +334,7 @@ proto._readSparseObject = function (withType) { if (withType) { return { $class: object.DEFAULT_CLASSNAME.map, - $: obj + $: obj, }; } @@ -370,41 +351,40 @@ proto._readSparseObject = function (withType) { * * @param {Boolean} withType if need retain the type info * @return {Object} - * @api public */ -proto.readObject = function (withType) { +proto.readObject = function(withType) { this._checkLabel('readObject', 'M'); debug('start read an object'); - var typeLabel = this.byteBuffer.getChar(this.byteBuffer.position()); + const typeLabel = this.byteBuffer.getChar(this.byteBuffer.position()); if (typeLabel !== 't') { debug('read sparse object, start label: %s', typeLabel); return this._readSparseObject(withType); } - var type = this.readType(false) || object.DEFAULT_CLASSNAME.map; + const type = this.readType(false) || object.DEFAULT_CLASSNAME.map; // if object is 'java.util.HashMap', type will be '' - var result = { + const result = { $class: type, - $: {} + $: {}, }; this._addRef(result); // get - var label = this.byteBuffer.getChar(); - var key; + let label = this.byteBuffer.getChar(); + let key; while (label !== 'z') { this.byteBuffer.position(this.byteBuffer.position() - 1); key = this.read(); - var value = this.read(withType); + const value = this.read(withType); label = this.byteBuffer.getChar(); // property name will auto transfer to a String type. debug('read object prop: %j with type: %s', key, withType); if (!/^this\$\d+$/.test(key)) { - var k = is.object(key) && key.name ? key.name : key; + const k = key && key.name ? key.name : key; result.$[k] = value; } } @@ -432,7 +412,6 @@ utils.addByteCodes(BYTE_CODES, [ * * @param {Boolean} withType if need retain the type info * @return {Object} - * @api public */ proto.readMap = proto.readObject; @@ -440,9 +419,9 @@ proto.readMap = proto.readObject; * anonymous variable-length list = {0, "foobar"} * http://hessian.caucho.com/doc/hessian-1.0-spec.xtp#list */ -proto._readNoLengthArray = function (withType, type) { - var arr = []; - var label = this.byteBuffer.getChar(this.byteBuffer.position()); +proto._readNoLengthArray = function(withType, type) { + let arr = []; + let label = this.byteBuffer.getChar(this.byteBuffer.position()); while (label !== 'z') { debug('no length array item#%d label: %s', arr.length, label); arr.push(this.read(withType)); @@ -451,9 +430,8 @@ proto._readNoLengthArray = function (withType, type) { // skip 'z' char this.byteBuffer.position(this.byteBuffer.position() + 1); - arr = withType - ? { $class: type, $: arr } - : arr; + arr = withType ? { $class: type, $: arr } : + arr; return arr; }; @@ -467,20 +445,19 @@ proto._readNoLengthArray = function (withType, type) { * * @param {Boolean} withType if need retain the type info * @return {Array} - * @api public */ -proto.readArray = function (withType) { +proto.readArray = function(withType) { debug('start read an array'); this._checkLabel('readArray', 'V'); - var type = ''; + let type = ''; - var typeLabel = this.byteBuffer.getChar(this.byteBuffer.position()); + const typeLabel = this.byteBuffer.getChar(this.byteBuffer.position()); if (typeLabel === 't') { type = this.readType(!withType); } type = type || object.DEFAULT_CLASSNAME.list; - var lengthLabel = this.byteBuffer.getChar(this.byteBuffer.position()); + const lengthLabel = this.byteBuffer.getChar(this.byteBuffer.position()); if (lengthLabel !== 'l') { debug('read no length array, start label: %s', typeLabel); return this._readNoLengthArray(withType, type); @@ -488,23 +465,23 @@ proto.readArray = function (withType) { // if object is 'java.util.ArrayList', type will be '' - var realResult = []; - var result = realResult; + const realResult = []; + let result = realResult; if (withType) { result = { $class: type, - $: realResult + $: realResult, }; } this._addRef(result); - var len = this.readLength(); + let len = this.readLength(); while (len--) { realResult.push(this.read(withType)); } - var endLabel = this.byteBuffer.getChar(); + const endLabel = this.byteBuffer.getChar(); if (endLabel !== 'z') { throw new TypeError('hessian readArray error, unexpect end label: ' + endLabel); } @@ -523,9 +500,9 @@ proto.readList = proto.readArray; * * @return {Object} */ -proto.readRef = function (withType) { - var rid = this.readRefId(); - var obj = this.refMap[rid]; +proto.readRef = function(withType) { + const rid = this.readRefId(); + let obj = this.refMap[rid]; if (!withType && obj && utils.hasOwnProperty(obj, '$')) { obj = obj.$; } @@ -540,7 +517,7 @@ proto.readRef = function (withType) { * * @return {Number} */ -proto.readRefId = function (withType) { +proto.readRefId = function() { this._checkLabel('readRef', 'R'); return this.byteBuffer.getInt(); }; @@ -553,12 +530,11 @@ utils.addByteCodes(BYTE_CODES, [ * read any thing * * @param {Boolean} withType if need retain the type info - * @api public */ -proto.read = function (withType) { - var pos = this.byteBuffer.position(); - var code = this.byteBuffer.get(pos); - var method = this.BYTE_CODES[code]; +proto.read = function(withType) { + const pos = this.byteBuffer.position(); + const code = this.byteBuffer.get(pos); + const method = this.BYTE_CODES[code]; if (debug.enabled) { debug('read position: %s, code: 0x%s, method: %s', pos, code.toString(16), method); } @@ -573,8 +549,8 @@ proto.read = function (withType) { /** * set or get decoder byteBuffer position */ -proto.position = function (num) { - if (is.number(num)) { +proto.position = function(num) { + if (typeof num === 'number') { this.byteBuffer.position(num); return this; } diff --git a/lib/v1/encoder.js b/lib/v1/encoder.js index c6c3ac7..10938ec 100644 --- a/lib/v1/encoder.js +++ b/lib/v1/encoder.js @@ -1,39 +1,28 @@ -/**! - * hessian.js - lib/encoder.js - * Copyright(c) 2014 - * MIT Licensed - * - * Authors: - * dead_horse (http://deadhorse.me) - * fengmk2 (http://fengmk2.github.com) - */ - 'use strict'; -var ByteBuffer = require('byte'); -var debug = require('debug')('hessian:v1:encoder'); -var utils = require('../utils'); -var javaObject = require('../object'); -var is = require('is-type-of'); -var converts = require('../convert'); -var SUPPORT_ES6_MAP = typeof Map === 'function' && typeof Map.prototype.forEach === 'function'; +const ByteBuffer = require('byte'); +const debug = require('../debug')('hessian:v1:encoder'); +const utils = require('../utils'); +const javaObject = require('../object'); +const converts = require('../convert'); +const is = require('../is_type_of'); function Encoder(options) { options = options || {}; - //array of buffer + // array of buffer this.byteBuffer = new ByteBuffer({ - size: options.size + size: options.size, }); this.objects = []; } -var proto = Encoder.prototype; +const proto = Encoder.prototype; -proto._assertType = function (method, expectType, val, desc) { - var valType = typeof val; +proto._assertType = function(method, expectType, val, desc) { + const valType = typeof val; if (!is[expectType](val)) { - var msg = 'hessian ' + method + - ' expect input type is `' + expectType + '`, but got `' + valType + '`' + ' : ' + JSON.stringify(val) + ' ' + (desc || ''); + const msg = 'hessian ' + method + + ' expect input type is `' + expectType + '`, but got `' + valType + '` : ' + JSON.stringify(val) + ' ' + (desc || ''); throw new TypeError(msg); } }; @@ -42,14 +31,14 @@ proto._assertType = function (method, expectType, val, desc) { * get the encode buffer * @return {Buffer} */ -proto.get = function () { +proto.get = function() { return this.byteBuffer.array(); }; /** * clean the buf */ -proto.reset = proto.clean = function () { +proto.reset = proto.clean = function() { this.byteBuffer.reset(); this.objects = []; return this; @@ -59,7 +48,7 @@ proto.reset = proto.clean = function () { * encode null * : N */ -proto.writeNull = function () { +proto.writeNull = function() { this.byteBuffer.putChar('N'); return this; }; @@ -69,7 +58,7 @@ proto.writeNull = function () { * : T * : F */ -proto.writeBool = function (val) { +proto.writeBool = function(val) { this.byteBuffer.putChar(val ? 'T' : 'F'); return this; }; @@ -78,7 +67,7 @@ proto.writeBool = function (val) { * encode int * : I 0x00 0x00 0x00 0x10 */ -proto.writeInt = function (val) { +proto.writeInt = function(val) { this._assertType('writeInt', 'int32', val); this.byteBuffer .putChar('I') @@ -91,7 +80,7 @@ proto.writeInt = function (val) { * warning: we won't check if the long value is out of bound, be careful! * : L 0x00 0x00 0x00 0x00 0x10 0x32 0x33 0x12 */ -proto.writeLong = function (val) { +proto.writeLong = function(val) { this.byteBuffer .putChar('L') .putLong(val); @@ -102,7 +91,7 @@ proto.writeLong = function (val) { * encode double * : D 0x00 0x00 0x00 0x00 0x10 0x32 0x33 0x12 */ -proto.writeDouble = function (val) { +proto.writeDouble = function(val) { this._assertType('writeDouble', 'number', val); this.byteBuffer .putChar('D') @@ -115,7 +104,7 @@ proto.writeDouble = function (val) { * 1.0: http://hessian.caucho.com/doc/hessian-1.0-spec.xtp#date * : d 0x00 0x00 0x00 0x00 0x10 0x32 0x33 0x12 */ -proto.writeDate = function (milliEpoch) { +proto.writeDate = function(milliEpoch) { if (milliEpoch instanceof Date) { milliEpoch = milliEpoch.getTime(); } @@ -132,9 +121,9 @@ proto.writeDate = function (milliEpoch) { * : b 0x80 0x00 [...] * B 0x00 0x03 [0x01 0x02 0x03] */ -proto.writeBytes = function (buf) { +proto.writeBytes = function(buf) { this._assertType('writeBytes', 'buffer', buf); - var offset = 0; + let offset = 0; while (buf.length - offset > utils.MAX_BYTE_TRUNK_SIZE) { this.byteBuffer .putChar('b') @@ -156,18 +145,16 @@ proto.writeBytes = function (buf) { * : s 0x80 0x00 [...] * S 0x00 0x03 [0x01 0x02 0x03] */ -proto.writeString = function (str) { +proto.writeString = function(str) { this._assertType('writeString', 'string', str); - var offset = 0; - - var length = str.length; - var strOffset = 0; + let length = str.length; + let strOffset = 0; while (length > 0x8000) { - var sublen = 0x8000; + let sublen = 0x8000; // chunk can't end in high surrogate - var tail = str.charCodeAt(strOffset + sublen - 1); + const tail = str.charCodeAt(strOffset + sublen - 1); - if (0xd800 <= tail && tail <= 0xdbff) { + if (tail >= 0xd800 && tail <= 0xdbff) { debug('writeString got tail: 0x%s', tail.toString(16)); sublen--; } @@ -191,7 +178,7 @@ proto.writeString = function (str) { return this; }; -var _typecache = {}; +const _typecache = {}; /** * encode type @@ -200,21 +187,21 @@ var _typecache = {}; * type ::= 0x74(t) type-string-length(putUInt16) type-string(putRawString) * ``` */ -proto.writeType = function (type) { +proto.writeType = function(type) { type = type || ''; if (_typecache[type]) { this.byteBuffer.put(_typecache[type]); return this; } - var start = this.byteBuffer.position(); + const start = this.byteBuffer.position(); this.byteBuffer .put(0x74) .putUInt16(type.length) .putRawString(type); - var end = this.byteBuffer.position(); + const end = this.byteBuffer.position(); _typecache[type] = this.byteBuffer.copy(start, end); return this; @@ -227,7 +214,7 @@ proto.writeType = function (type) { * ref ::= R(0x52) int(putInt) * ``` */ -proto.writeRef = function (refId) { +proto.writeRef = function(refId) { this.byteBuffer .putChar('R') .putInt(refId); @@ -235,8 +222,8 @@ proto.writeRef = function (refId) { return this; }; -proto._checkRef = function (obj) { - var refIndex = this.objects.indexOf(obj); +proto._checkRef = function(obj) { + const refIndex = this.objects.indexOf(obj); if (refIndex >= 0) { // already have this object // just write ref @@ -255,7 +242,7 @@ proto._checkRef = function (obj) { * @param {Object} obj simple obj * @return {this} */ -proto._writeHashMap = function (obj, className) { +proto._writeHashMap = function(obj, className) { debug('_writeHashMap() %j, fields: %j', obj); // Real code in java impl: @@ -265,16 +252,16 @@ proto._writeHashMap = function (obj, className) { // hashmap's type is null this.writeType(className || ''); - if (SUPPORT_ES6_MAP && obj instanceof Map) { - obj.forEach(function (value, key) { + if (obj instanceof Map) { + obj.forEach(function(value, key) { this.write(key); this.write(value); }, this); } else { // hash map must sort keys - var keys = Object.keys(obj).sort(); - for (var i = 0; i < keys.length; i++) { - var k = keys[i]; + const keys = Object.keys(obj).sort(); + for (let i = 0; i < keys.length; i++) { + const k = keys[i]; this.writeString(k); this.write(obj[k]); } @@ -284,15 +271,15 @@ proto._writeHashMap = function (obj, className) { }; // M(0x4d) type(writeType) ( ) z(0x7a) -proto._writeObject = function (obj) { +proto._writeObject = function(obj) { this._assertType('writeObject / writeMap', 'object', obj.$, obj.$class); this.byteBuffer.put(0x4d); this.writeType(obj.$class); - var val = obj.$; - var keys = Object.keys(val); - for (var i = 0, len = keys.length; i < len; i++) { - var key = keys[i]; + const val = obj.$; + const keys = Object.keys(val); + for (let i = 0, len = keys.length; i < len; i++) { + const key = keys[i]; this.writeString(key); this.write(val[key]); } @@ -307,10 +294,10 @@ proto._writeObject = function (obj) { * : {a: 1} * : {$class: 'java.lang.Map', $: {a: 1}} */ -proto.writeObject = function (obj) { - if (is.nullOrUndefined(obj) || +proto.writeObject = function(obj) { + if (obj == null || // : { a: { '$class': 'xxx', '$': null } } - (is.string(obj.$class) && is.nullOrUndefined(obj.$))) { + (is.string(obj.$class) && obj.$ == null)) { debug('writeObject with a null'); return this.writeNull(); } @@ -321,8 +308,8 @@ proto.writeObject = function (obj) { return this; } - var className = ''; - var realObj; + let className = ''; + let realObj; if (!obj.$class || !obj.$) { // : {a: 1} realObj = obj; @@ -337,14 +324,14 @@ proto.writeObject = function (obj) { } debug('writeObject with complex object, className: %s', className); - var convertor = converts[obj.$class]; + const convertor = converts[obj.$class]; obj = convertor ? convertor(obj) : obj; return this._writeObject(obj); }; proto.writeMap = proto.writeObject; -proto._writeListBegin = function (length, type) { +proto._writeListBegin = function(length, type) { this.byteBuffer.putChar('V'); if (type) { @@ -386,21 +373,21 @@ proto._writeListBegin = function (length, type) { * @return {this} */ -proto.writeArray = function (arr) { +proto.writeArray = function(arr) { if (this._checkRef(arr)) { // if is ref, will write by _checkRef return this; } - var isSimpleArray = is.array(arr); - var className = ''; // empty string meaning: `javaObject.DEFAULT_CLASSNAME.list` - var realArray = arr; + const isSimpleArray = Array.isArray(arr); + let className = ''; // empty string meaning: `javaObject.DEFAULT_CLASSNAME.list` + let realArray = arr; if (!isSimpleArray) { - if (is.object(arr) && is.nullOrUndefined(arr.$)) { + if (is.object(arr) && arr.$ == null) { return this.writeNull(); } - var isComplexArray = is.object(arr) && - is.string(arr.$class) && is.array(arr.$); + const isComplexArray = is.object(arr) && + is.string(arr.$class) && Array.isArray(arr.$); if (!isComplexArray) { throw new TypeError('hessian writeArray input type invalid'); } @@ -411,9 +398,9 @@ proto.writeArray = function (arr) { realArray = arr.$; } - var hasEnd = this._writeListBegin(realArray.length, className); + const hasEnd = this._writeListBegin(realArray.length, className); - for (var i = 0; i < realArray.length; i++) { + for (let i = 0; i < realArray.length; i++) { this.write(realArray[i]); } @@ -432,49 +419,51 @@ proto.writeList = proto.writeArray; * : 1.1 => double * : */ -proto.write = function (val) { - var type = typeof val; - if (is.nullOrUndefined(val) || is.NaN(val) || is.function(val) || is.regExp(val)) { +proto.write = function(val) { + const type = typeof val; + if (val == null || Number.isNaN(val) || typeof val === 'function' || utils.isRegExp(val)) { return this.writeNull(); } switch (type) { - case 'string': - return this.writeString(val); - case 'boolean': - return this.writeBool(val); - case 'number': - // must check long value first - if (is.long(val)) { - debug('write number %d as long', val); - return this.writeLong(val); - } - - if (is.int(val)) { - debug('write number %d as int', val); - return this.writeInt(val); - } - - // double - debug('write number %d as double', val); - return this.writeDouble(val); + case 'string': + return this.writeString(val); + case 'boolean': + return this.writeBool(val); + case 'number': + // must check long value first + if (val >= utils.MAX_INT_32 || val < -utils.MAX_INT_32) { + debug('write number %d as long', val); + return this.writeLong(val); + } + + if (val % 1 === 0) { + debug('write number %d as int', val); + return this.writeInt(val); + } + + // double + debug('write number %d as double', val); + return this.writeDouble(val); + default: + break; } - if (is.long(val) || is.Long(val)) { + if (utils.isLong(val)) { debug('write long: high: %s, low: %s', val.high, val.low); return this.writeLong(val); } - if (is.date(val)) { + if (utils.isDate(val)) { debug('write Date: %s', val); return this.writeDate(val); } - if (is.buffer(val)) { + if (Buffer.isBuffer(val)) { debug('write Buffer with a length of %d', val.length); return this.writeBytes(val); } - if (is.array(val)) { + if (Array.isArray(val)) { debug('write simple array with a length of %d', val.length); return this.writeArray(val); } @@ -487,17 +476,17 @@ proto.write = function (val) { return this.writeObject(val); } - if (is.array(val.$)) { + if (Array.isArray(val.$)) { debug('detect val.$ is array'); return this.writeArray(val); } - var method = utils.getSerializer(val.$class); + const method = utils.getSerializer(val.$class); debug('write detect %s use serializer %s', val.$class, method); // {$class: 'long', $: 123} if (method !== 'writeObject' && method !== 'writeArray') { - if (is.nullOrUndefined(val.$)) { + if (val.$ == null) { return this.writeNull(); } return this[method](val.$); @@ -505,7 +494,7 @@ proto.write = function (val) { // java.lang.Object if (utils.isJavaObject(val.$class)) { - if (is.date(val.$) || !is.object(val.$)) { + if (utils.isDate(val.$) || !is.object(val.$)) { return this.write(val.$); } } diff --git a/lib/v2/decoder.js b/lib/v2/decoder.js index 179bfe7..4510ebd 100644 --- a/lib/v2/decoder.js +++ b/lib/v2/decoder.js @@ -1,31 +1,16 @@ -/**! - * hessian.js - lib/v2/decoder.js - * - * Copyright(c) 2014 - * MIT Licensed - * - * Authors: - * fengmk2 (http://fengmk2.github.com) - */ - -"use strict"; - -/** - * Module dependencies. - */ - -var util = require('util'); -var is = require('is-type-of'); -var debug = require('debug')('hessian:v2:decoder'); -var DecoderV1 = require('../v1/decoder'); -var utils = require('../utils'); -var isJavaException = require('../object').isJavaException; -var JavaExceptionError = require('../object').JavaExceptionError; -var decodeFnCtx = { JavaExceptionError: JavaExceptionError }; -var codegen = require('@protobufjs/codegen'); - -var BYTE_CODES = {}; -var errorProps = { +'use strict'; + +const util = require('util'); +const debug = require('../debug')('hessian:v2:decoder'); +const DecoderV1 = require('../v1/decoder'); +const utils = require('../utils'); +const isJavaException = require('../object').isJavaException; +const JavaExceptionError = require('../object').JavaExceptionError; +const decodeFnCtx = { JavaExceptionError }; +const codegen = require('@protobufjs/codegen'); + +const BYTE_CODES = {}; +const errorProps = { detailMessage: true, stackTrace: true, }; @@ -40,9 +25,9 @@ function Decoder(buf, classCache) { util.inherits(Decoder, DecoderV1); -var proto = Decoder.prototype; +const proto = Decoder.prototype; -proto.clean = function () { +proto.clean = function() { DecoderV1.prototype.clean.call(this); this.types = []; this.classes = []; @@ -96,10 +81,9 @@ utils.addByteCodes(BYTE_CODES, [ * ``` * * @return {Number} - * @api public */ -proto.readInt = function () { - var code = this.byteBuffer.get(); +proto.readInt = function() { + const code = this.byteBuffer.get(); // Compact int if (code >= 0x80 && code <= 0xbf) { // Integers between -16 and 47 can be encoded by a single octet in the range x80 to xbf. @@ -114,8 +98,8 @@ proto.readInt = function () { if (code >= 0xd0 && code <= 0xd7) { // Integers between -262144 and 262143 can be encoded in three bytes with the leading byte in the range xd0 to xd7. // value = ((code - 0xd4) << 16) + (b1 << 8) + b0; - var b1 = this.byteBuffer.get(); - var b0 = this.byteBuffer.get(); + const b1 = this.byteBuffer.get(); + const b0 = this.byteBuffer.get(); return ((code - 0xd4) << 16) + (b1 << 8) + b0; } if (code === 0x49) { @@ -126,10 +110,10 @@ proto.readInt = function () { }; utils.addByteCodes(BYTE_CODES, [ - [0x80, 0xbf], - [0xc0, 0xcf], - [0xd0, 0xd7], - 0x49 + [ 0x80, 0xbf ], + [ 0xc0, 0xcf ], + [ 0xd0, 0xd7 ], + 0x49, ], 'readInt'); /** @@ -171,11 +155,10 @@ utils.addByteCodes(BYTE_CODES, [ * ``` * * @return {Number} - * @api public */ -proto.readLong = function (withType) { - var result; - var code = this.byteBuffer.get(); +proto.readLong = function(withType) { + let result; + const code = this.byteBuffer.get(); // Compact long if (code >= 0xd8 && code <= 0xef) { // Longs between -8 and 15 are represented by a single octet in the range xd8 to xef. @@ -188,10 +171,10 @@ proto.readLong = function (withType) { } else if (code >= 0x38 && code <= 0x3f) { // Longs between -262144 and 262143 are encoded in three octets with the leading byte in the range x38 to x3f. // value = ((code - 0x3c) << 16) + (b1 << 8) + b0 - var b1 = this.byteBuffer.get(); - var b0 = this.byteBuffer.get(); + const b1 = this.byteBuffer.get(); + const b0 = this.byteBuffer.get(); result = ((code - 0x3c) << 16) + (b1 << 8) + b0; - // ::= x77 b3 b2 b1 b0 # 32-bit integer cast to long + // ::= x77 b3 b2 b1 b0 # 32-bit integer cast to long } else 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 @@ -206,11 +189,11 @@ proto.readLong = function (withType) { }; utils.addByteCodes(BYTE_CODES, [ - [0xd8, 0xef], - [0xf0, 0xff], - [0x38, 0x3f], + [ 0xd8, 0xef ], + [ 0xf0, 0xff ], + [ 0x38, 0x3f ], 0x77, - 0x4c + 0x4c, ], 'readLong'); /** @@ -219,14 +202,13 @@ utils.addByteCodes(BYTE_CODES, [ * format @see EncoderV2.prototype.writeDouble * * @return {Number} - * @api public */ -proto.readDouble = function (withType) { - var result; - var code = this.byteBuffer.get(); +proto.readDouble = function(withType) { + let result; + const code = this.byteBuffer.get(); if (code === 0x44) { result = this.byteBuffer.getDouble(); - // Compact double + // Compact double } else if (code === 0x67) { result = 0.0; } else if (code === 0x68) { @@ -250,7 +232,7 @@ utils.addByteCodes(BYTE_CODES, [ 0x68, 0x69, 0x6a, - 0x6b + 0x6b, ], 'readDouble'); /** @@ -282,7 +264,7 @@ utils.addByteCodes(BYTE_CODES, [ // // 0x4b, // ], 'readDate'); utils.addByteCodes(BYTE_CODES, [ - 0x64 + 0x64, ], 'readDate'); /** @@ -305,18 +287,17 @@ utils.addByteCodes(BYTE_CODES, [ * len = code - 0x20 * * @return {Buffer} - * @api public */ -proto.readBytes = function () { - var code = this.byteBuffer.get(); +proto.readBytes = function() { + let code = this.byteBuffer.get(); if (code >= 0x20 && code <= 0x2f) { // short binary - var len = code - 0x20; + const len = code - 0x20; return this.byteBuffer.read(len); } - var bufs = []; - var length = 0; + const bufs = []; + let length = 0; // get non-final trunk start with 'b' while (code === 0x62) { length = this.byteBuffer.getUInt16(); @@ -341,7 +322,7 @@ proto.readBytes = function () { utils.addByteCodes(BYTE_CODES, [ 0x62, 0x42, - [0x20, 0x2f], + [ 0x20, 0x2f ], ], 'readBytes'); /** @@ -370,11 +351,10 @@ utils.addByteCodes(BYTE_CODES, [ * ``` * * @return {String} - * @api public */ -proto.readString = function () { - var str = ''; - var code = this.byteBuffer.get(); +proto.readString = function() { + let str = ''; + let code = this.byteBuffer.get(); debug('readString() code: %s', code); // x73 ('s') represents any non-final chunk while (code === 0x73) { @@ -400,77 +380,79 @@ proto.readString = function () { utils.addByteCodes(BYTE_CODES, [ 0x73, 0x53, - [0x00, 0x1f], + [ 0x00, 0x1f ], // [0x30, 0x33], ], 'readString'); /** * @return {String} type string */ -proto.readType = function () { +proto.readType = function() { // http://grepcode.com/file/repo1.maven.org/maven2/com.caucho/hessian/3.1.3/com/caucho/hessian/io/Hessian2Input.java#Hessian2Input.readType%28%29 // Parses a type from the stream. // t b16 b8 - var pos = this.byteBuffer.position(); - var code = this.byteBuffer.get(); + const pos = this.byteBuffer.position(); + const code = this.byteBuffer.get(); - var type; + let type; + let len; + let ref; switch (code) { - case 0x74: // 't' String - // t(x74) len(putUInt16) type-string(putRawString) - var len = this.byteBuffer.getUInt16(); - type = this.byteBuffer.readRawString(len); - debug('got type#%d: %s', this.types.length, type); - this.types.push(type); - return type; - case 0x54: // 'T' Ref - case 0x75: // 'u' TYPE_REF = 0x75 - // u(x75) ref(writeInt) - var ref = this.readInt(); - type = this.types[ref]; - debug('got ref:%d type#%d: %s', ref, this.types.length, type); - return type; - default: - // reset the position - this.byteBuffer.position(pos); - return ""; + case 0x74: // 't' String + // t(x74) len(putUInt16) type-string(putRawString) + len = this.byteBuffer.getUInt16(); + type = this.byteBuffer.readRawString(len); + debug('got type#%d: %s', this.types.length, type); + this.types.push(type); + return type; + case 0x54: // 'T' Ref + case 0x75: // 'u' TYPE_REF = 0x75 + // u(x75) ref(writeInt) + ref = this.readInt(); + type = this.types[ref]; + debug('got ref:%d type#%d: %s', ref, this.types.length, type); + return type; + default: + // reset the position + this.byteBuffer.position(pos); + return ''; } }; // properties match with this$\d+ means inner properties // it is useless for node and is circular structure -var INNER_CLASS_PROPERTY_REG = /^this\$\d+$/; -var INNER_CLASS_LABEL = '$$ignore_inner_property$$'; +const INNER_CLASS_PROPERTY_REG = /^this\$\d+$/; +const INNER_CLASS_LABEL = '$$ignore_inner_property$$'; -proto._readObjectDefinition = function () { - var size = this.readInt(); - var classname = this._readUTF8String(size); - var fieldsLength = this.readInt(); +proto._readObjectDefinition = function() { + const size = this.readInt(); + const classname = this._readUTF8String(size); + const fieldsLength = this.readInt(); // compose cache key with class name and fields length // more safely - var cachekey = classname + '##' + fieldsLength; + const cachekey = classname + '##' + fieldsLength; // get class definition from cache - var cacheClz = this.classCache && this.classCache.get(cachekey); + const cacheClz = this.classCache && this.classCache.get(cachekey); if (cacheClz) { this.byteBuffer.skip(cacheClz.length); this.classes.push(cacheClz); return cacheClz; } - var pos = this.byteBuffer.position(); - var fields = []; - for (var i = 0; i < fieldsLength; i++) { - var name = this.readString(); + const pos = this.byteBuffer.position(); + const fields = []; + for (let i = 0; i < fieldsLength; i++) { + let name = this.readString(); if (INNER_CLASS_PROPERTY_REG.test(name)) { name = INNER_CLASS_LABEL; } fields.push(name); } debug('_readObjectDefinition got %s fields: %j', classname, fields); - var clz = { + const clz = { name: classname, - fields: fields, + fields, length: this.byteBuffer.position() - pos, decodeFn: null, }; @@ -479,13 +461,13 @@ proto._readObjectDefinition = function () { // set class definition into cache if (this.classCache) { if (this.classCache.enableCompile) { - var errorPropCount = 0; - var gen = codegen(['decoder', 'withType'], 'decode'); + let errorPropCount = 0; + const gen = codegen([ 'decoder', 'withType' ], 'decode'); gen('// %s', cachekey); gen('var result = {'); gen(' $class: \'%s\',', classname); gen(' $: {'); - for (var field of fields) { + for (const field of fields) { if (errorProps[field]) { errorPropCount++; } @@ -497,7 +479,7 @@ proto._readObjectDefinition = function () { gen(' },'); gen('};'); gen('decoder._addRef(result);'); - for (var field of fields) { + for (const field of fields) { if (INNER_CLASS_LABEL === field) { gen('decoder.read(false);'); } else { @@ -544,9 +526,8 @@ proto._readObjectDefinition = function () { * * @param {Boolean} withType if need retain the type info * @return {Object} - * @api public */ -proto.readObject = function (withType) { +proto.readObject = function(withType) { // class instance format: // // O(x4f) type fields-length(writeInt) fields-names(writeString) o ref fields-values(write) @@ -554,8 +535,8 @@ proto.readObject = function (withType) { // // type format: length(writeInt) stringbytes(putRawString) - var code = this.byteBuffer.get(); - var ref; + const code = this.byteBuffer.get(); + let ref; if (code === 0x4f) { this._readObjectDefinition(); return this.readObject(withType); @@ -566,25 +547,25 @@ proto.readObject = function (withType) { } // must be code: 'o' - var cls = this.classes[ref]; + const cls = this.classes[ref]; debug('readObject %s, ref: %s', cls.name, ref); if (cls.decodeFn) { return cls.decodeFn(this, withType); } - var result = { + const result = { $class: cls.name, - $: {} + $: {}, }; this._addRef(result); - var fields = cls.fields; - for (var i = 0; i < fields.length; i++) { - var name = fields[i]; + const fields = cls.fields; + for (let i = 0; i < fields.length; i++) { + const name = fields[i]; if (INNER_CLASS_LABEL === name) { this.read(false); continue; } - var value = this.read(withType); + const value = this.read(withType); result.$[name] = value; } @@ -613,8 +594,8 @@ utils.addByteCodes(BYTE_CODES, [ * * @return {Number} */ -proto.readRefId = function (withType) { - var code = this.byteBuffer.get(); +proto.readRefId = function() { + const code = this.byteBuffer.get(); // if (code === 0x51) { // return this.read(); // } @@ -639,8 +620,8 @@ utils.addByteCodes(BYTE_CODES, [ 0x52, ], 'readRef'); -proto._readVariableLengthItems = function (list, withType) { - var code = this.byteBuffer.get(this.byteBuffer.position()); +proto._readVariableLengthItems = function(list, withType) { + let code = this.byteBuffer.get(this.byteBuffer.position()); // Z while (code !== 0x5a) { list.push(this.read(withType)); @@ -651,8 +632,8 @@ proto._readVariableLengthItems = function (list, withType) { this.byteBuffer.skip(1); }; -proto._readFixedLengthItems = function (len, list, withType) { - for (var i = 0; i < len; i++) { +proto._readFixedLengthItems = function(len, list, withType) { + for (let i = 0; i < len; i++) { list.push(this.read(withType)); } }; @@ -682,25 +663,24 @@ proto._readFixedLengthItems = function (len, list, withType) { * * @param {Boolean} withType if need retain the type info * @return {Array} - * @api public */ -proto.readArray = function (withType) { +proto.readArray = function(withType) { // format: // // V(x56) type(writeType) 0x6e short-length(put) values 'z' // V(x56) type(writeType) 'l(0x6c)' long-length(putInt) values 'z' // v(x76) ref(writeInt) fix-length(writeInt) values - var code = this.byteBuffer.get(); - var type; - var result; - var list = []; - var hasEnd; - var length = null; + let code = this.byteBuffer.get(); + let type; + let result; + const list = []; + let hasEnd; + let length = null; if (code === 0x56) { type = this.readType(); hasEnd = true; } else { - var ref = this.readInt(); + const ref = this.readInt(); type = this.types[ref]; hasEnd = false; length = this.readInt(); @@ -713,7 +693,7 @@ proto.readArray = function (withType) { if (type) { result = { $class: type, - $: list + $: list, }; } else { result = list; @@ -729,7 +709,7 @@ proto.readArray = function (withType) { } } - for (var i = 0; i < length; i++) { + for (let i = 0; i < length; i++) { list.push(this.read(withType)); } @@ -756,16 +736,16 @@ utils.addByteCodes(BYTE_CODES, [ // [0x78, 0x7f], ], 'readArray'); -proto._readMap = function (map, withType) { - var code = this.byteBuffer.get(this.byteBuffer.position()); +proto._readMap = function(map, withType) { + let code = this.byteBuffer.get(this.byteBuffer.position()); map = map || {}; - var k; - var v; + let k; + let v; // z(x7a) while (code !== 0x7a) { k = this.read(withType); v = this.read(withType); - var key = is.object(k) && k.name ? k.name : k; + const key = k && k.name ? k.name : k; map[key] = v; code = this.byteBuffer.get(this.byteBuffer.position()); } @@ -782,21 +762,21 @@ proto._readMap = function (map, withType) { * * @return {Object} */ -proto.readHashMap = function (withType) { +proto.readHashMap = function(withType) { // H: x48 - var code = this.byteBuffer.get(); + const code = this.byteBuffer.get(); if (code !== 0x48) { this.throwError('readHashMap', code); } - var result = {}; + const result = {}; this._addRef(result); this._readMap(result, withType); return result; }; utils.addByteCodes(BYTE_CODES, [ - 0x48 + 0x48, ], 'readHashMap'); /** @@ -819,26 +799,25 @@ utils.addByteCodes(BYTE_CODES, [ * * @param {Boolean} withType if need retain the type info * @return {Object} - * @api public */ -proto.readMap = function (withType) { - var code = this.byteBuffer.get(); +proto.readMap = function(withType) { + const code = this.byteBuffer.get(); if (code !== 0x4d) { this.throwError('readMap', code); } - var type = this.readType(); + const type = this.readType(); debug('readMap() got type: %j, withType: %s', type, withType); if (!type) { - var map = {}; + const map = {}; this._addRef(map); this._readMap(map, withType); return map; } - var result = { + const result = { $class: type, - $: {} + $: {}, }; // obj maybe refers to itself @@ -853,7 +832,7 @@ proto.readMap = function (withType) { }; utils.addByteCodes(BYTE_CODES, [ - 0x4d + 0x4d, ], 'readMap'); module.exports = Decoder; diff --git a/lib/v2/encoder.js b/lib/v2/encoder.js index 87ba21c..f7c5a92 100644 --- a/lib/v2/encoder.js +++ b/lib/v2/encoder.js @@ -1,14 +1,11 @@ 'use strict'; -var debug = require('debug')('hessian:v2:encoder'); -var is = require('is-type-of'); -var util = require('util'); -var EncoderV1 = require('../v1/encoder'); -var javaObject = require('../object'); -var utility = require('utility'); -var Long = require('long'); - -var SUPPORT_ES6_MAP = typeof Map === 'function' && typeof Map.prototype.forEach === 'function'; +const debug = require('../debug')('hessian:v2:encoder'); +const importLazy = require('import-lazy')(require); +const Long = importLazy('long'); +const util = require('util'); +const EncoderV1 = require('../v1/encoder'); +const { isSafeNumberString } = require('../utils'); function Encoder(options) { EncoderV1.call(this, options); @@ -20,12 +17,12 @@ function Encoder(options) { util.inherits(Encoder, EncoderV1); -var proto = Encoder.prototype; +const proto = Encoder.prototype; /** * clean the buf */ -proto.reset = proto.clean = function () { +proto.reset = proto.clean = function() { EncoderV1.prototype.clean.call(this); this._classRefs = []; this._classRefFields = {}; @@ -33,17 +30,17 @@ proto.reset = proto.clean = function () { return this; }; -var INT_DIRECT_MIN = -0x10; // -16 -var INT_DIRECT_MAX = 0x2f; // 47 -var INT_ZERO = 0x90; // 144 +const INT_DIRECT_MIN = -0x10; // -16 +const INT_DIRECT_MAX = 0x2f; // 47 +const INT_ZERO = 0x90; // 144 -var INT_BYTE_MIN = -0x800; // -2048 -var INT_BYTE_MAX = 0x7ff; // 2047 -var INT_BYTE_ZERO = 0xc8; // 200 +const INT_BYTE_MIN = -0x800; // -2048 +const INT_BYTE_MAX = 0x7ff; // 2047 +const INT_BYTE_ZERO = 0xc8; // 200 -var INT_SHORT_MIN = -0x40000; // -262144 -var INT_SHORT_MAX = 0x3ffff; // 262143 -var INT_SHORT_ZERO = 0xd4; // 212 +const INT_SHORT_MIN = -0x40000; // -262144 +const INT_SHORT_MAX = 0x3ffff; // 262143 +const INT_SHORT_ZERO = 0xd4; // 212 /** * encode int @@ -61,17 +58,17 @@ var INT_SHORT_ZERO = 0xd4; // 212 * ::= [xd0-xd7] b1 b0 * ``` */ -proto.writeInt = function (val) { +proto.writeInt = function(val) { this._assertType('writeInt', 'int32', val); if (INT_DIRECT_MIN <= val && val <= INT_DIRECT_MAX) { this.byteBuffer.put(val + INT_ZERO); } else if (INT_BYTE_MIN <= val && val <= INT_BYTE_MAX) { - var b0 = val & 0xff; - var code = (val >> 8) + INT_BYTE_ZERO; + const b0 = val & 0xff; + const code = (val >> 8) + INT_BYTE_ZERO; this.byteBuffer.put(code).put(b0); } else if (INT_SHORT_MIN <= val && val <= INT_SHORT_MAX) { - var b1b0 = val & 0xffff; - var code = (val >> 16) + INT_SHORT_ZERO; + const b1b0 = val & 0xffff; + const code = (val >> 16) + INT_SHORT_ZERO; this.byteBuffer.put(code).putUInt16(b1b0); } else { this.byteBuffer @@ -126,9 +123,9 @@ proto.writeInt = function (val) { * value = (b3 << 24) + (b2 << 16) + (b1 << 8) + b0 * ``` */ -proto.writeLong = function (val) { +proto.writeLong = function(val) { if (typeof val === 'string') { - if (!utility.isSafeNumberString(val)) { + if (!isSafeNumberString(val)) { val = Long.fromString(val); } else { val = Number(val); @@ -141,12 +138,12 @@ proto.writeLong = function (val) { if (val >= -8 && val <= 15) { this.byteBuffer.put(val + 0xe0); } else if (val >= -2048 && val <= 2047) { - var b0 = val & 0xff; - var code = (val >> 8) + 0xf8; + const b0 = val & 0xff; + const code = (val >> 8) + 0xf8; this.byteBuffer.put(code).put(b0); } else if (val >= -262144 && val <= 262143) { - var b1b0 = val & 0xffff; - var code = (val >> 16) + 0x3c; + const b1b0 = val & 0xffff; + const code = (val >> 16) + 0x3c; this.byteBuffer.put(code).putUInt16(b1b0); } else if (val >= -2147483648 && val <= 2147483647) { this.byteBuffer.put(0x77).putInt32(val); @@ -201,8 +198,8 @@ proto.writeLong = function (val) { * Doubles which are equivalent to their 32-bit float representation * can be represented as the 4-octet float and then cast to double. */ -proto.writeDouble = function (val) { - var intValue = parseInt(val); +proto.writeDouble = function(val) { + const intValue = parseInt(val); if (intValue === val) { if (val === 0) { this.byteBuffer.put(0x67); @@ -210,13 +207,13 @@ proto.writeDouble = function (val) { } else if (val === 1) { this.byteBuffer.put(0x68); return this; - } else if (-0x80 <= intValue && intValue < 0x80) { + } else if (intValue >= -0x80 && intValue < 0x80) { this.byteBuffer.put(0x69).put(intValue); return this; - } else if (-0x8000 <= intValue && intValue < 0x8000) { + } else if (intValue >= -0x8000 && intValue < 0x8000) { this.byteBuffer.put(0x6a).putInt16(intValue); return this; - } else if (-0x80000000 <= intValue && intValue < 0x80000000) { + } else if (intValue >= -0x80000000 && intValue < 0x80000000) { // 0x80000000L <= 0x7fffffffL this.byteBuffer.put(0x6b).putFloat(intValue); return this; @@ -259,7 +256,7 @@ proto.writeDouble = function (val) { * * len = code - 0x20 */ -proto.writeBytes = function (buf) { +proto.writeBytes = function(buf) { // TODO: [x34-x37] b0 this._assertType('writeBytes', 'buffer', buf); if (buf.length <= 15) { @@ -267,8 +264,8 @@ proto.writeBytes = function (buf) { return this; } - var offset = 0; - var left = buf.length - offset; + let offset = 0; + let left = buf.length - offset; while (left > 0x8000) { // 32768 this.byteBuffer .put(0x62) // 'b' @@ -319,21 +316,21 @@ proto.writeBytes = function (buf) { * [x00-x1f] * ``` */ -proto.writeString = function (str) { +proto.writeString = function(str) { this._assertType('writeString', 'string', str); if (str.length <= 31) { this.byteBuffer.put(str.length).putRawString(str); return this; } - var length = str.length; - var strOffset = 0; + let length = str.length; + let strOffset = 0; while (length > 0x8000) { - var sublen = 0x8000; + let sublen = 0x8000; // chunk can't end in high surrogate - var tail = str.charCodeAt(strOffset + sublen - 1); + const tail = str.charCodeAt(strOffset + sublen - 1); - if (0xd800 <= tail && tail <= 0xdbff) { + if (tail >= 0xd800 && tail <= 0xdbff) { debug('writeString got tail: 0x%s', tail.toString(16)); sublen--; } @@ -360,7 +357,7 @@ proto.writeString = function (str) { return this; }; -var _typecache = {}; +const _typecache = {}; /** * encode type @@ -381,7 +378,7 @@ var _typecache = {}; * Repeated type strings MAY use the type map to refer to a previously used type. * The type reference is zero-based over all the types encountered during parsing. */ -proto.writeType = function (type) { +proto.writeType = function(type) { // type format: // t(x74) len(putUInt16) type-string(putRawString) // u(x75) ref(writeInt) @@ -390,20 +387,19 @@ proto.writeType = function (type) { return; } - var ref = this._typeRefs.indexOf(type); + const ref = this._typeRefs.indexOf(type); if (ref >= 0) { - var TYPE_REF = 0x75; // 'u' this.writeInt(ref); } else { this._typeRefs.push(type); if (_typecache[type]) { this.byteBuffer.put(_typecache[type]); } else { - var start = this.byteBuffer.position(); + const start = this.byteBuffer.position(); this.byteBuffer.put(0x74); // 't' this.byteBuffer.putUInt16(type.length); this.byteBuffer.putRawString(type); - var end = this.byteBuffer.position(); + const end = this.byteBuffer.position(); _typecache[type] = this.byteBuffer.copy(start, end); } } @@ -428,7 +424,7 @@ proto.writeType = function (type) { * Each map or list is stored into an array as it is parsed. * ref selects one of the stored objects. The first object is numbered '0'. */ -proto.writeRef = function (value) { +proto.writeRef = function(value) { // java code: // R b32 b24 b16 b8 // public static final int REF_BYTE = 0x4a; @@ -437,11 +433,11 @@ proto.writeRef = function (value) { // public static final int TYPE_REF = 0x75; // 'u' if (value < 0x100) { - var REF_BYTE = 0x4a; // 'J' + const REF_BYTE = 0x4a; // 'J' this.byteBuffer.put(REF_BYTE); this.byteBuffer.put(value); } else if (value < 0x10000) { - var REF_SHORT = 0x4b; // 'K' + const REF_SHORT = 0x4b; // 'K' this.byteBuffer.put(REF_SHORT); this.byteBuffer.putUInt16(value); } else { @@ -451,33 +447,33 @@ proto.writeRef = function (value) { return this; }; -var _classcache = {}; +const _classcache = {}; -proto._writeObjectBegin = function (type, fields) { +proto._writeObjectBegin = function(type, fields) { debug('_writeObjectBegin() type: %s, fields: %j', type, fields); - var ref = this._classRefs.indexOf(type); + const ref = this._classRefs.indexOf(type); if (ref >= 0) { // o(x6f) ref this.byteBuffer.put(0x6f); this.writeInt(ref); return ref; + } + // O(x4f) type-string-length(writeInt) type-string(putRawString) + this._classRefs.push(type); + this._classRefFields[type] = fields; + if (_classcache[type]) { + this.byteBuffer.put(_classcache[type]); } else { - // O(x4f) type-string-length(writeInt) type-string(putRawString) - this._classRefs.push(type); - this._classRefFields[type] = fields; - if (_classcache[type]) { - this.byteBuffer.put(_classcache[type]); - } else { - var start = this.byteBuffer.position(); - this.byteBuffer.put(0x4f); - this.writeInt(type.length); - this.byteBuffer.putRawString(type); - var end = this.byteBuffer.position(); - _classcache[type] = this.byteBuffer.copy(start, end); - } - - return -1; + const start = this.byteBuffer.position(); + this.byteBuffer.put(0x4f); + this.writeInt(type.length); + this.byteBuffer.putRawString(type); + const end = this.byteBuffer.position(); + _classcache[type] = this.byteBuffer.copy(start, end); } + + return -1; + }; // class instance format: @@ -486,20 +482,20 @@ proto._writeObjectBegin = function (type, fields) { // o(x6f) ref(writeInt) fields-length(writeInt) fields-names(writeString) o ref fields-values(write) // // type format: length(writeInt) stringbytes(putRawString) -proto._writeObject = function (obj) { - var className = obj.$class; - var realObj = obj.$; +proto._writeObject = function(obj) { + const className = obj.$class; + const realObj = obj.$; // hessian 2.0 // field defined sort must same as java Class defined - var keys = Object.keys(realObj); - var ref = this._writeObjectBegin(className, keys); + let keys = Object.keys(realObj); + const ref = this._writeObjectBegin(className, keys); if (ref === -1) { // writeDefinition20 // out.writeClassFieldLength(_fields.length); this.writeInt(keys.length); // write field names - for (var i = 0; i < keys.length; i++) { - var key = keys[i]; + for (let i = 0; i < keys.length; i++) { + const key = keys[i]; this.writeString(key); } this._writeObjectBegin(className, keys); @@ -508,8 +504,8 @@ proto._writeObject = function (obj) { // writeInstance keys = this._classRefFields[className]; debug('_writeObject %s, fields: %j', className, keys); - for (var i = 0; i < keys.length; i++) { - var key = keys[i]; + for (let i = 0; i < keys.length; i++) { + const key = keys[i]; this.write(realObj[key]); } @@ -524,8 +520,8 @@ proto._writeObject = function (obj) { // // fix lengthed format: // v(x76) ref(writeInt) fix-length(writeInt) values -proto._writeListBegin = function (length, type) { - var ref = this._typeRefs.indexOf(type); +proto._writeListBegin = function(length, type) { + const ref = this._typeRefs.indexOf(type); if (ref >= 0) { this.byteBuffer.put(0x76); // 'v' this.writeInt(ref); @@ -558,7 +554,7 @@ proto._writeListBegin = function (length, type) { * @param {Object} obj simple obj * @return {this} */ -proto._writeHashMap = function (obj, className) { +proto._writeHashMap = function(obj, className) { debug('_writeHashMap() %j, fields: %j', obj); this.byteBuffer.put(0x4d); // M @@ -569,16 +565,16 @@ proto._writeHashMap = function (obj, className) { this.byteBuffer.putRawString(className); } - if (SUPPORT_ES6_MAP && obj instanceof Map) { - obj.forEach(function (value, key) { + if (obj instanceof Map) { + obj.forEach(function(value, key) { this.write(key); this.write(value); }, this); } else { // hash map must sort keys - var keys = Object.keys(obj).sort(); - for (var i = 0; i < keys.length; i++) { - var k = keys[i]; + const keys = Object.keys(obj).sort(); + for (let i = 0; i < keys.length; i++) { + const k = keys[i]; this.writeString(k); this.write(obj[k]); } diff --git a/package.json b/package.json index e284fba..7056787 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,13 @@ ], "scripts": { "autod": "autod", - "test": "make jshint && make test" + "lint": "eslint . --ext .js", + "cov": "TEST_TIMEOUT=10000 egg-bin cov", + "test": "npm run lint && npm run test-local", + "test-local": "egg-bin test", + "pkgfiles": "egg-bin pkgfiles --check", + "ci": "npm run autod -- --check && npm run pkgfiles && npm run lint && npm run cov", + "contributors": "contributors -f plain -o AUTHORS" }, "repository": { "type": "git", @@ -31,24 +37,26 @@ "homepage": "https://github.com/node-modules/hessian.js", "dependencies": { "@protobufjs/codegen": "^2.0.4", - "byte": "^1.4.1", - "debug": "^3.2.6", - "is-type-of": "^1.2.1", - "long": "^4.0.0", - "utility": "^1.15.0" + "byte": "^2.0.0", + "debug": "^4.1.1", + "import-lazy": "^3.1.0", + "long": "^4.0.0" }, "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.6.0", - "jshint": "^2.9.6", - "mm": "^2.4.1", - "mocha": "^3.5.3", - "should": "^13.2.3" + "contributors": "^0.5.1", + "egg-bin": "^4.12.2", + "egg-ci": "^1.11.0", + "eslint": "^5.16.0", + "eslint-config-egg": "^7.3.1", + "js-to-java": "^2.6.1" }, "engines": { - "node": ">= 0.12.0" + "node": ">=8" + }, + "ci": { + "version": "8, 10, 11" } } diff --git a/test/array.test.js b/test/array.test.js index 0bc6e0d..3089a8b 100644 --- a/test/array.test.js +++ b/test/array.test.js @@ -6,69 +6,65 @@ * 汤尧 (http://tangyao.me) */ -"use strict"; +'use strict'; -var assert = require('assert'); -var hessian = require('../'); +const assert = require('assert'); +const hessian = require('../'); -describe('array.test.js', function () { - it('should write null v1', function () { - var b = hessian.encode([ - { - $class: '[java.lang.Integer', - $: null - }, - { - $class: '[java.lang.Integer', - $: [1] - } +describe('array.test.js', function() { + it('should write null v1', function() { + const b = hessian.encode([{ + $class: '[java.lang.Integer', + $: null, + }, + { + $class: '[java.lang.Integer', + $: [ 1 ], + }, ]); - var a = hessian.decode(b); - assert.deepEqual(a, [null, [1]]); + const a = hessian.decode(b); + assert.deepEqual(a, [ null, [ 1 ]]); }); - it('should write undefined v1', function () { - var b = hessian.encode([ - { - $class: '[java.lang.Integer', - $: undefined - }, - { - $class: '[java.lang.Integer', - $: [1] - } + it('should write undefined v1', function() { + const b = hessian.encode([{ + $class: '[java.lang.Integer', + $: undefined, + }, + { + $class: '[java.lang.Integer', + $: [ 1 ], + }, ]); - var a = hessian.decode(b); - assert.deepEqual(a, [null, [1]]); + const a = hessian.decode(b); + assert.deepEqual(a, [ null, [ 1 ]]); }); - it('should write null v2', function () { - var b = hessian.encode([ - { - $class: '[java.lang.Integer', - $: null - }, - { - $class: '[java.lang.Integer', - $: [1] - } + it('should write null v2', function() { + const b = hessian.encode([{ + $class: '[java.lang.Integer', + $: null, + }, + { + $class: '[java.lang.Integer', + $: [ 1 ], + }, ], '2.0'); - var a = hessian.decode(b, '2.0'); - assert.deepEqual(a, [null, [1]]); + const a = hessian.decode(b, '2.0'); + assert.deepEqual(a, [ null, [ 1 ]]); }); - it('should write undefined v2', function () { - var b = hessian.encode([ - { - $class: '[java.lang.Integer', - $: undefined - }, - { - $class: '[java.lang.Integer', - $: [1] - } + it('should write undefined v2', function() { + const b = hessian.encode([{ + $class: '[java.lang.Integer', + $: undefined, + }, + { + $class: '[java.lang.Integer', + $: [ 1 ], + }, ], '2.0'); - var a = hessian.decode(b, '2.0'); - assert.deepEqual(a, [null, [1]]); + const a = hessian.decode(b, '2.0'); + assert.deepEqual(a, [ null, [ 1 ]]); }); -}); \ No newline at end of file +}); diff --git a/test/binary.test.js b/test/binary.test.js index 93e7f26..d362983 100644 --- a/test/binary.test.js +++ b/test/binary.test.js @@ -1,223 +1,213 @@ -/*! - * hessian.js - test/binary.test.js - * - * Copyright(c) 2014 - * MIT Licensed - * - * Authors: - * fengmk2 (http://fengmk2.github.com) - */ - -"use strict"; - -var assert = require('assert'); -var hessian = require('../'); -var utils = require('./utils'); - -describe('binary.test.js', function () { - it('should read "foo" binary', function () { - hessian.decode(Buffer.concat([new Buffer(['B'.charCodeAt(0), 0x00, 0x03]), new Buffer('foo')])); +'use strict'; + +const hessian = require('../'); +const utils = require('./utils'); +const assert = require('assert'); + +describe('binary.test.js', function() { + it('should read "foo" binary', function() { + hessian.decode(Buffer.concat([ Buffer.from([ 'B'.charCodeAt(0), 0x00, 0x03 ]), Buffer.from('foo') ])); }); - it('should write "foo"', function () { + it('should write "foo"', function() { assert.deepEqual( - hessian.encode(new Buffer('foo')), - Buffer.concat([new Buffer(['B'.charCodeAt(0), 0x00, 0x03]), new Buffer('foo')]) + hessian.encode(Buffer.from('foo')), + Buffer.concat([ Buffer.from([ 'B'.charCodeAt(0), 0x00, 0x03 ]), Buffer.from('foo') ]) ); }); - it('should read and write empty binary', function () { - var empty = hessian.decode(new Buffer(['B'.charCodeAt(0), 0x00, 0x00])); + it('should read and write empty binary', function() { + const empty = hessian.decode(Buffer.from([ 'B'.charCodeAt(0), 0x00, 0x00 ])); assert(Buffer.isBuffer(empty)); assert(empty.length === 0); assert.deepEqual( - hessian.encode(new Buffer('')), - new Buffer(['B'.charCodeAt(0), 0x00, 0x00]) + hessian.encode(Buffer.from('')), + Buffer.from([ 'B'.charCodeAt(0), 0x00, 0x00 ]) ); }); - it('should write and read as java impl', function () { - var bytes = new Buffer(65535); + it('should write and read as java impl', function() { + let bytes = Buffer.alloc(65535); bytes.fill(0x41); - var buf = hessian.encode(bytes, '1.0'); + let buf = hessian.encode(bytes, '1.0'); 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 = Buffer.alloc(32768); bytes.fill(0x41); - var buf = hessian.encode(bytes, '1.0'); + buf = hessian.encode(bytes, '1.0'); 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 = Buffer.alloc(32769); bytes.fill(0x41); - var buf = hessian.encode(bytes, '1.0'); + buf = hessian.encode(bytes, '1.0'); 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 = Buffer.alloc(32767); bytes.fill(0x41); - var buf = hessian.encode(bytes, '1.0'); + buf = hessian.encode(bytes, '1.0'); 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 = Buffer.alloc(32769); bytes.fill(0x41); - var buf = hessian.encode(bytes, '1.0'); + buf = hessian.encode(bytes, '1.0'); 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 = Buffer.alloc(42769); bytes.fill(0x41); - var buf = hessian.encode(bytes, '1.0'); + buf = hessian.encode(bytes, '1.0'); 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 = Buffer.alloc(82769); bytes.fill(0x41); - var buf = hessian.encode(bytes, '1.0'); + buf = hessian.encode(bytes, '1.0'); 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'); + describe('v2.0', function() { + it('should read zero length binary data', function() { + const buf = hessian.decode(Buffer.from([ 0x20 ]), '2.0'); assert(buf.length === 0); - assert.deepEqual(buf, new Buffer(0)); + assert.deepEqual(buf, Buffer.alloc(0)); }); - it('should read short datas', function () { - var decoder = new hessian.DecoderV2(new Buffer([0x20, 0x23, 0x23, 0x02, 0x03, 0x20])); - var buf = decoder.read(); + it('should read short datas', function() { + const decoder = new hessian.DecoderV2(Buffer.from([ 0x20, 0x23, 0x23, 0x02, 0x03, 0x20 ])); + let buf = decoder.read(); assert(buf.length === 0); - assert.deepEqual(buf, new Buffer(0)); + assert.deepEqual(buf, Buffer.alloc(0)); buf = decoder.read(); assert(buf.length === 3); - assert.deepEqual(buf, new Buffer([0x23, 2, 3])); + assert.deepEqual(buf, Buffer.from([ 0x23, 2, 3 ])); buf = decoder.read(); assert(buf.length === 0); - assert.deepEqual(buf, new Buffer(0)); + assert.deepEqual(buf, Buffer.alloc(0)); }); - it('should read max length short datas', function () { - var input = new Buffer(16); + it('should read max length short datas', function() { + const input = Buffer.alloc(16); input.fill(0x2f); input[0] = 0x2f; - var buf = hessian.decode(input, '2.0'); + const buf = hessian.decode(input, '2.0'); assert(buf.length === 15); - var output = new Buffer(15); + const output = Buffer.alloc(15); output.fill(0x2f); assert.deepEqual(buf, output); }); - it('should read long binary', function () { - var buf = hessian.encode(new Buffer(65535), '2.0'); + it('should read long binary', function() { + let buf = hessian.encode(Buffer.alloc(65535), '2.0'); assert(buf[0] === 0x62); hessian.decode(buf, '2.0'); - buf = hessian.encode(new Buffer(65536), '2.0'); + buf = hessian.encode(Buffer.alloc(65536), '2.0'); hessian.decode(buf, '2.0'); - buf = hessian.encode(new Buffer(65535 * 2 - 10), '2.0'); + buf = hessian.encode(Buffer.alloc(65535 * 2 - 10), '2.0'); hessian.decode(buf, '2.0'); }); - it('should write short binary', function () { - assert.deepEqual(hessian.encode(new Buffer(''), '2.0'), new Buffer([0x20])); + it('should write short binary', function() { + assert.deepEqual(hessian.encode(Buffer.from(''), '2.0'), Buffer.from([ 0x20 ])); }); - it('should write and read as java impl', function () { - var bytes = new Buffer(65535); + it('should write and read as java impl', function() { + let bytes = Buffer.alloc(65535); bytes.fill(0x41); - var buf = hessian.encode(bytes, '2.0'); + let buf = hessian.encode(bytes, '2.0'); 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 = Buffer.alloc(32768); bytes.fill(0x41); - var buf = hessian.encode(bytes, '2.0'); + buf = hessian.encode(bytes, '2.0'); 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 = Buffer.alloc(32769); bytes.fill(0x41); - var buf = hessian.encode(bytes, '2.0'); + buf = hessian.encode(bytes, '2.0'); 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 = Buffer.alloc(32767); bytes.fill(0x41); - var buf = hessian.encode(bytes, '2.0'); + buf = hessian.encode(bytes, '2.0'); 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 = Buffer.alloc(32769); bytes.fill(0x41); - var buf = hessian.encode(bytes, '2.0'); + buf = hessian.encode(bytes, '2.0'); 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 = Buffer.alloc(42769); bytes.fill(0x41); - var buf = hessian.encode(bytes, '2.0'); + buf = hessian.encode(bytes, '2.0'); 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 = Buffer.alloc(82769); bytes.fill(0x41); - var buf = hessian.encode(bytes, '2.0'); + buf = hessian.encode(bytes, '2.0'); 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); + it('should read java hessian 1.0 bin format', function() { + let bytes = Buffer.alloc(65535); bytes.fill(0x41); assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/65535'), '2.0'), bytes); - var bytes = new Buffer(32767); + bytes = Buffer.alloc(32767); bytes.fill(0x41); assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/32767'), '2.0'), bytes); - var bytes = new Buffer(32768); + bytes = Buffer.alloc(32768); bytes.fill(0x41); assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/32768'), '2.0'), bytes); - var bytes = new Buffer(32769); + bytes = Buffer.alloc(32769); bytes.fill(0x41); assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/32769'), '2.0'), bytes); - var bytes = new Buffer(32767); + bytes = Buffer.alloc(32767); bytes.fill(0x41); assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/32767'), '2.0'), bytes); - var bytes = new Buffer(32769); + bytes = Buffer.alloc(32769); bytes.fill(0x41); assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/32769'), '2.0'), bytes); - var bytes = new Buffer(42769); + bytes = Buffer.alloc(42769); bytes.fill(0x41); assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/42769'), '2.0'), bytes); - var bytes = new Buffer(82769); + bytes = Buffer.alloc(82769); bytes.fill(0x41); assert.deepEqual(hessian.decode(utils.bytes('v1/bytes/82769'), '2.0'), bytes); }); diff --git a/test/boolean.test.js b/test/boolean.test.js index 83584fb..c3fe5bf 100644 --- a/test/boolean.test.js +++ b/test/boolean.test.js @@ -1,35 +1,25 @@ -/**! - * hessian.js - test/boolean.test.js - * - * Copyright(c) 2014 - * MIT Licensed - * - * Authors: - * fengmk2 (http://fengmk2.github.com) - */ +'use strict'; -"use strict"; +const assert = require('assert'); +const hessian = require('../'); -var assert = require('assert'); -var hessian = require('../'); - -describe('boolean.test.js', function () { - it('should read true and false', function () { - assert(hessian.decode(new Buffer('T')) === true); - assert(hessian.decode(new Buffer('F')) === false); +describe('boolean.test.js', function() { + it('should read true and false', function() { + assert(hessian.decode(Buffer.from('T')) === true); + assert(hessian.decode(Buffer.from('F')) === false); }); - it('should write true and false', function () { - assert.deepEqual(hessian.encode(true), new Buffer('T')); - assert.deepEqual(hessian.encode(false), new Buffer('F')); + it('should write true and false', function() { + assert.deepEqual(hessian.encode(true), Buffer.from('T')); + assert.deepEqual(hessian.encode(false), Buffer.from('F')); }); - describe('v2.0', function () { - it('should read write as 1.0', function () { - 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); + describe('v2.0', function() { + it('should read write as 1.0', function() { + assert.deepEqual(hessian.encode(true, '2.0'), Buffer.from('T')); + assert.deepEqual(hessian.encode(false, '2.0'), Buffer.from('F')); + assert(hessian.decode(Buffer.from('T'), '2.0') === true); + assert(hessian.decode(Buffer.from('F'), '2.0') === false); }); }); }); diff --git a/test/convert.test.js b/test/convert.test.js index 53cf02b..7f5b830 100644 --- a/test/convert.test.js +++ b/test/convert.test.js @@ -1,17 +1,17 @@ 'use strict'; -var assert = require('assert'); -var hessian = require('../'); -var java = require('js-to-java'); +const assert = require('assert'); +const hessian = require('../'); +const java = require('js-to-java'); describe('test/convert.test.js', function() { [ - '1.0', '2.0' + '1.0', '2.0', ].forEach(function(version) { describe(version, function() { it('should convert java.util.Locale to com.caucho.hessian.io.LocaleHandle', function() { - var buf1 = hessian.encode(java.Locale('zh_CN'), version); - var buf2 = hessian.encode({ + const buf1 = hessian.encode(java.Locale('zh_CN'), version); + const buf2 = hessian.encode({ $class: 'java.util.Locale', $: 'zh_CN', }, version); diff --git a/test/date.test.js b/test/date.test.js index f4efa7b..531a5d1 100644 --- a/test/date.test.js +++ b/test/date.test.js @@ -8,17 +8,17 @@ * fengmk2 (http://fengmk2.github.com) */ -"use strict"; +'use strict'; -var assert = require('assert'); -var hessian = require('../'); -var utils = require('./utils'); +const assert = require('assert'); +const hessian = require('../'); +const utils = require('./utils'); -describe('date.test.js', function () { - var dateBuffer = new Buffer(['d'.charCodeAt(0), 0x00, 0x00, 0x00, 0xd0, 0x4b, 0x92, 0x84, 0xb8]); +describe('date.test.js', function() { + const dateBuffer = Buffer.from([ 'd'.charCodeAt(0), 0x00, 0x00, 0x00, 0xd0, 0x4b, 0x92, 0x84, 0xb8 ]); - it('should read date 2:51:31 May 8, 1998', function () { - var d = hessian.decode(dateBuffer); + it('should read date 2:51:31 May 8, 1998', function() { + const d = hessian.decode(dateBuffer); assert(Object.prototype.toString.call(d) === '[object Date]'); assert(d.getFullYear() === 1998); assert(d.getTime() === 894621091000); @@ -26,19 +26,19 @@ describe('date.test.js', function () { assert(d.toISOString() === '1998-05-08T09:51:31.000Z'); }); - it('should write date 2:51:31 May 8, 1998', function () { + it('should write date 2:51:31 May 8, 1998', function() { assert.deepEqual(hessian.encode(new Date(894621091000)), dateBuffer); }); - it('should write date 0 and read', function () { + it('should write date 0 and read', function() { assert.deepEqual( hessian.encode(new Date(0)), - new Buffer(['d'.charCodeAt(0), 0, 0, 0, 0, 0, 0, 0, 0]) + Buffer.from([ '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'); + it('should read date 09:51:31 May 8, 1998 UTC', function() { + const d = hessian.decode(utils.bytes('v1/date/894621091000'), '1.0'); assert(Object.prototype.toString.call(d) === '[object Date]'); assert(d.getFullYear() === 1998); assert(d.getTime() === 894621091000); @@ -46,8 +46,8 @@ describe('date.test.js', function () { assert(d.toISOString() === '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'); + it('should read date 09:51:00 May 8, 1998 UTC', function() { + const d = hessian.decode(utils.bytes('v1/date/894621060000'), '1.0'); assert(Object.prototype.toString.call(d) === '[object Date]'); assert(d.getFullYear() === 1998); assert(d.getTime() === 894621060000); @@ -55,16 +55,16 @@ describe('date.test.js', function () { assert(d.toISOString() === '1998-05-08T09:51:00.000Z'); }); - it('should write date', function () { - var now = new Date(1398280514000); + it('should write date', function() { + const now = new Date(1398280514000); assert.deepEqual(hessian.encode(now, '1.0'), utils.bytes('v1/date/now')); // read it assert.deepEqual(hessian.decode(utils.bytes('v1/date/now'), '1.0'), 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'); + describe('hessian 2.0', function() { + it('should read date 09:51:31 May 8, 1998 UTC', function() { + const d = hessian.decode(utils.bytes('v2/date/894621091000'), '2.0'); assert(Object.prototype.toString.call(d) === '[object Date]'); assert(d.getFullYear() === 1998); assert(d.getTime() === 894621091000); @@ -72,8 +72,8 @@ describe('date.test.js', function () { assert(d.toISOString() === '1998-05-08T09:51:31.000Z'); }); - it('should read Compact: date in minutes, 09:51:00 May 8, 1998 UTC', function () { - var d = hessian.decode(utils.bytes('v2/date/894621060000'), '2.0'); + it('should read Compact: date in minutes, 09:51:00 May 8, 1998 UTC', function() { + const d = hessian.decode(utils.bytes('v2/date/894621060000'), '2.0'); assert(Object.prototype.toString.call(d) === '[object Date]'); assert(d.getFullYear() === 1998); assert(d.getTime() === 894621060000); @@ -81,14 +81,14 @@ describe('date.test.js', function () { assert(d.toISOString() === '1998-05-08T09:51:00.000Z'); }); - it('should write and read date', function () { - var now = new Date(1398280514000); + it('should write and read date', function() { + const now = new Date(1398280514000); assert.deepEqual(hessian.encode(now, '2.0'), utils.bytes('v2/date/now')); // read it assert.deepEqual(hessian.decode(utils.bytes('v2/date/now'), '2.0'), now); }); - it('should read 1.0 format', function () { + it('should read 1.0 format', function() { assert( hessian.decode(utils.bytes('v1/date/894621091000'), '2.0').getTime() === 894621091000 ); diff --git a/test/decode.circular.test.js b/test/decode.circular.test.js index bcb9777..b120d0e 100644 --- a/test/decode.circular.test.js +++ b/test/decode.circular.test.js @@ -1,35 +1,25 @@ - -/**! - * hessian.js - test/decode.circular.test.js - * - * Copyright(c) 2015 - * - * Authors: - * tangyao (http://tangyao.me/) - */ - 'use strict'; -var assert = require('assert'); -var hessian = require('../'); -var utils = require('./utils'); +const assert = require('assert'); +const hessian = require('../'); +const utils = require('./utils'); -describe('test/decode.circular.test.js', function () { +describe('test/decode.circular.test.js', function() { - it('v1 decode()', function () { - var data = new Buffer([77, 116, 0, 49, 99, 111, 109, 46, 97, 108, 105, 112, 97, 121, 46, 99, 111, 110, 102, 105, 103, 115, 101, 114, 118, 101, 114, 46, 99, 111, 110, 102, 114, 101, 103, 95, 116, 101, 115, 116, 46, 79, 110, 108, 105, 110, 101, 77, 111, 100, 117, 108, 101, 83, 0, 6, 109, 111, 100, 117, 108, 101, 83, 0, 1, 97, 83, 0, 4, 100, 101, 115, 99, 83, 0, 1, 98, 83, 0, 8, 118, 101, 114, 115, 105, 111, 110, 115, 86, 108, 0, 0, 0, 1, 77, 116, 0, 57, 99, 111, 109, 46, 97, 108, 105, 112, 97, 121, 46, 99, 111, 110, 102, 105, 103, 115, 101, 114, 118, 101, 114, 46, 99, 111, 110, 102, 114, 101, 103, 95, 116, 101, 115, 116, 46, 79, 110, 108, 105, 110, 101, 77, 111, 100, 117, 108, 101, 36, 86, 101, 114, 115, 105, 111, 110, 83, 0, 7, 118, 101, 114, 115, 105, 111, 110, 83, 0, 1, 99, 83, 0, 6, 97, 115, 115, 101, 116, 115, 86, 108, 0, 0, 0, 1, 83, 0, 1, 105, 122, 83, 0, 6, 116, 104, 105, 115, 36, 48, 82, 0, 0, 0, 0, 122, 122, 122]); - var rs = hessian.decode(data); + it('v1 decode()', function() { + const data = Buffer.from([ 77, 116, 0, 49, 99, 111, 109, 46, 97, 108, 105, 112, 97, 121, 46, 99, 111, 110, 102, 105, 103, 115, 101, 114, 118, 101, 114, 46, 99, 111, 110, 102, 114, 101, 103, 95, 116, 101, 115, 116, 46, 79, 110, 108, 105, 110, 101, 77, 111, 100, 117, 108, 101, 83, 0, 6, 109, 111, 100, 117, 108, 101, 83, 0, 1, 97, 83, 0, 4, 100, 101, 115, 99, 83, 0, 1, 98, 83, 0, 8, 118, 101, 114, 115, 105, 111, 110, 115, 86, 108, 0, 0, 0, 1, 77, 116, 0, 57, 99, 111, 109, 46, 97, 108, 105, 112, 97, 121, 46, 99, 111, 110, 102, 105, 103, 115, 101, 114, 118, 101, 114, 46, 99, 111, 110, 102, 114, 101, 103, 95, 116, 101, 115, 116, 46, 79, 110, 108, 105, 110, 101, 77, 111, 100, 117, 108, 101, 36, 86, 101, 114, 115, 105, 111, 110, 83, 0, 7, 118, 101, 114, 115, 105, 111, 110, 83, 0, 1, 99, 83, 0, 6, 97, 115, 115, 101, 116, 115, 86, 108, 0, 0, 0, 1, 83, 0, 1, 105, 122, 83, 0, 6, 116, 104, 105, 115, 36, 48, 82, 0, 0, 0, 0, 122, 122, 122 ]); + const rs = hessian.decode(data); assert( JSON.stringify(rs) === '{"module":"a","desc":"b","versions":[{"version":"c","assets":["i"]}]}' ); }); - - it('v2 decode()', function () { - var javabuf = utils.bytes('v2/object/ConnectionRequest'); - var connreq1 = hessian.decode(javabuf, '2.0'); + + it('v2 decode()', function() { + const javabuf = utils.bytes('v2/object/ConnectionRequest'); + const connreq1 = hessian.decode(javabuf, '2.0'); assert(connreq1.ctx && connreq1.ctx.id); assert(JSON.stringify(connreq1) === '{"ctx":{"id":101}}'); }); -}); \ No newline at end of file +}); diff --git a/test/double.test.js b/test/double.test.js index a88b80a..433e60d 100644 --- a/test/double.test.js +++ b/test/double.test.js @@ -1,4 +1,4 @@ -/*! +/* ! * hessian.js - test/double.test.js * * Copyright(c) 2014 @@ -8,45 +8,46 @@ * fengmk2 (http://fengmk2.github.com) */ -"use strict"; +'use strict'; -var assert = require('assert'); -var java = require('js-to-java'); -var hessian = require('../'); -var utils = require('./utils'); +const assert = require('assert'); +const java = require('js-to-java'); +const hessian = require('../'); +const utils = require('./utils'); -describe('double.test.js', function () { - var doubleBuffer = new Buffer(['D'.charCodeAt(0), - 0x40, 0x28, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00]); +describe('double.test.js', function() { + const doubleBuffer = Buffer.from([ 'D'.charCodeAt(0), + 0x40, 0x28, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, + ]); - it('should read double 12.25', function () { + it('should read double 12.25', function() { assert(hessian.decode(doubleBuffer) === 12.25); }); - it('should write double 12.25', function () { + it('should write double 12.25', function() { assert.deepEqual(hessian.encode(12.25), doubleBuffer); assert.deepEqual(hessian.encode(java.double(12.25)), doubleBuffer); assert.deepEqual(hessian.encode({ $class: 'double', - $: 12.25 + $: 12.25, }), doubleBuffer); }); - it('should write double 100', function () { + it('should write double 100', function() { assert.deepEqual( hessian.encode(java.double(100)), - new Buffer(['D'.charCodeAt(0), 0x40, 0x59, 0, 0, 0, 0, 0, 0]) + Buffer.from([ 'D'.charCodeAt(0), 0x40, 0x59, 0, 0, 0, 0, 0, 0 ]) ); }); - it('should write double 0', function () { + it('should write double 0', function() { assert.deepEqual( hessian.encode(java.double(0)), - new Buffer(['D'.charCodeAt(0), 0, 0, 0, 0, 0, 0, 0, 0]) + Buffer.from([ 'D'.charCodeAt(0), 0, 0, 0, 0, 0, 0, 0, 0 ]) ); }); - it('should write as java impl', function () { + it('should write as java impl', function() { assert.deepEqual(hessian.encode(java.double(0), '1.0'), utils.bytes('v1/double/0')); assert.deepEqual(hessian.encode(java.double(0.0), '1.0'), utils.bytes('v1/double/0')); assert.deepEqual(hessian.encode(java.double(1), '1.0'), utils.bytes('v1/double/1')); @@ -121,7 +122,7 @@ describe('double.test.js', function () { ); }); - it('should read java bin format', function () { + it('should read java bin format', function() { assert(hessian.decode(utils.bytes('v1/double/0'), '1.0') === 0); assert(hessian.decode(utils.bytes('v1/double/1'), '1.0') === 1); assert(hessian.decode(utils.bytes('v1/double/10'), '1.0') === 10); @@ -161,7 +162,7 @@ describe('double.test.js', function () { ); }); - it('should decode with type', function () { + it('should decode with type', function() { assert.deepEqual(hessian.decode(utils.bytes('v1/double/0'), '1.0', true), { $class: 'double', $: 0, @@ -178,70 +179,69 @@ describe('double.test.js', function () { }); }); - describe('v2.0', function () { - it('should read 0.0 and 1.0', function () { - assert(hessian.decode(new Buffer([0x67]), '2.0') === 0.0); - assert(hessian.decode(new Buffer([0x68]), '2.0') === 1.0); + describe('v2.0', function() { + it('should read 0.0 and 1.0', function() { + assert(hessian.decode(Buffer.from([ 0x67 ]), '2.0') === 0.0); + assert(hessian.decode(Buffer.from([ 0x68 ]), '2.0') === 1.0); }); - it('should read 8 bits double', function () { - assert(hessian.decode(new Buffer([0x69, 0x00]), '2.0') === 0.0); - assert(hessian.decode(new Buffer([0x69, 0x01]), '2.0') === 1.0); - assert(hessian.decode(new Buffer([0x69, 0x80]), '2.0') === -128.0); - assert(hessian.decode(new Buffer([0x69, 0x7f]), '2.0') === 127.0); + it('should read 8 bits double', function() { + assert(hessian.decode(Buffer.from([ 0x69, 0x00 ]), '2.0') === 0.0); + assert(hessian.decode(Buffer.from([ 0x69, 0x01 ]), '2.0') === 1.0); + assert(hessian.decode(Buffer.from([ 0x69, 0x80 ]), '2.0') === -128.0); + assert(hessian.decode(Buffer.from([ 0x69, 0x7f ]), '2.0') === 127.0); }); - it('should read 16 bits double', function () { - assert(hessian.decode(new Buffer([0x6a, 0x00, 0x00]), '2.0') === 0.0); - assert(hessian.decode(new Buffer([0x6a, 0x00, 0x01]), '2.0') === 1.0); - assert(hessian.decode(new Buffer([0x6a, 0x00, 0x80]), '2.0') === 128.0); - assert(hessian.decode(new Buffer([0x6a, 0x00, 0x7f]), '2.0') === 127.0); - assert(hessian.decode(new Buffer([0x6a, 0x80, 0x00]), '2.0') === -32768.0); - assert(hessian.decode(new Buffer([0x6a, 0x7f, 0xff]), '2.0') === 32767.0); + it('should read 16 bits double', function() { + assert(hessian.decode(Buffer.from([ 0x6a, 0x00, 0x00 ]), '2.0') === 0.0); + assert(hessian.decode(Buffer.from([ 0x6a, 0x00, 0x01 ]), '2.0') === 1.0); + assert(hessian.decode(Buffer.from([ 0x6a, 0x00, 0x80 ]), '2.0') === 128.0); + assert(hessian.decode(Buffer.from([ 0x6a, 0x00, 0x7f ]), '2.0') === 127.0); + assert(hessian.decode(Buffer.from([ 0x6a, 0x80, 0x00 ]), '2.0') === -32768.0); + assert(hessian.decode(Buffer.from([ 0x6a, 0x7f, 0xff ]), '2.0') === 32767.0); }); - it('should read 32 bits float double', function () { - assert(hessian.decode(new Buffer([0x6b, 0x00, 0x00, 0x00, 0x00]), '2.0') === 0.0); + it('should read 32 bits float double', function() { + assert(hessian.decode(Buffer.from([ 0x6b, 0x00, 0x00, 0x00, 0x00 ]), '2.0') === 0.0); assert( - hessian.decode(new Buffer([0x6b, 0x41, 0x44, 0x00, 0x00]), '2.0') === 12.25 + hessian.decode(Buffer.from([ 0x6b, 0x41, 0x44, 0x00, 0x00 ]), '2.0') === 12.25 ); }); - it('should read normal double', function () { + it('should read normal double', function() { assert( - hessian.decode(new Buffer([0x44, 0x40, 0x24, 0, 0, 0, 0, 0, 0]), '2.0') === 10.0 + hessian.decode(Buffer.from([ 0x44, 0x40, 0x24, 0, 0, 0, 0, 0, 0 ]), '2.0') === 10.0 ); }); - it('should decode with type', function () { - assert.deepEqual(hessian.decode(new Buffer([0x69, 0x01]), '2.0', true), { + it('should decode with type', function() { + assert.deepEqual(hessian.decode(Buffer.from([ 0x69, 0x01 ]), '2.0', true), { $class: 'double', $: 1.0, }); assert.deepEqual( - hessian.decode(new Buffer([0x44, 0x40, 0x24, 0, 0, 0, 0, 0, 0]), '2.0', true), - { + hessian.decode(Buffer.from([ 0x44, 0x40, 0x24, 0, 0, 0, 0, 0, 0 ]), '2.0', true), { $class: 'double', $: 10.0, } ); - assert.deepEqual(hessian.decode(new Buffer([0x6a, 0x00, 0x80]), '2.0', true), { + assert.deepEqual(hessian.decode(Buffer.from([ 0x6a, 0x00, 0x80 ]), '2.0', true), { $class: 'double', $: 128.0, }); }); - it('should write 0.0 and 1.0', function () { - assert.deepEqual(hessian.encode(java.double(0), '2.0'), new Buffer([0x67])); - assert.deepEqual(hessian.encode(java.double(0.0), '2.0'), new Buffer([0x67])); + it('should write 0.0 and 1.0', function() { + assert.deepEqual(hessian.encode(java.double(0), '2.0'), Buffer.from([ 0x67 ])); + assert.deepEqual(hessian.encode(java.double(0.0), '2.0'), Buffer.from([ 0x67 ])); - assert.deepEqual(hessian.encode(java.double(1), '2.0'), new Buffer([0x68])); - assert.deepEqual(hessian.encode(java.double(1.0), '2.0'), new Buffer([0x68])); + assert.deepEqual(hessian.encode(java.double(1), '2.0'), Buffer.from([ 0x68 ])); + assert.deepEqual(hessian.encode(java.double(1.0), '2.0'), Buffer.from([ 0x68 ])); }); - it('should write as java impl', function () { + it('should write as java impl', function() { assert.deepEqual(hessian.encode(java.double(0), '2.0'), utils.bytes('v2/double/0')); assert.deepEqual(hessian.encode(java.double(0.0), '2.0'), utils.bytes('v2/double/0')); assert.deepEqual(hessian.encode(java.double(1), '2.0'), utils.bytes('v2/double/1')); @@ -314,7 +314,7 @@ describe('double.test.js', function () { ); }); - it('should read java bin format', function () { + it('should read java bin format', function() { assert(hessian.decode(utils.bytes('v2/double/0'), '2.0') === 0); assert(hessian.decode(utils.bytes('v2/double/1'), '2.0') === 1); assert(hessian.decode(utils.bytes('v2/double/10'), '2.0') === 10); @@ -360,7 +360,7 @@ describe('double.test.js', function () { ); }); - it('should read java hessian 1.0 bin format', function () { + it('should read java hessian 1.0 bin format', function() { assert(hessian.decode(utils.bytes('v1/double/0'), '2.0') === 0); assert(hessian.decode(utils.bytes('v1/double/1'), '2.0') === 1); assert(hessian.decode(utils.bytes('v1/double/10'), '2.0') === 10); diff --git a/test/exception.test.js b/test/exception.test.js index b1eaa76..bdadcf4 100644 --- a/test/exception.test.js +++ b/test/exception.test.js @@ -1,24 +1,14 @@ -/*! - * hessian.js - test/exception.test.js - * - * Copyright(c) 2014 - * MIT Licensed - * - * Authors: - * fengmk2 (http://fengmk2.github.com) - */ - -"use strict"; - -var assert = require('assert'); -var hessian = require('../'); -var utils = require('./utils'); -var java = require('js-to-java'); - -describe('exception.test.js', function () { - describe('v1.0', function () { - it('should read java exception as js error', function () { - var ioe = hessian.decode(utils.bytes('v1/exception/IOException')); +'use strict'; + +const hessian = require('../'); +const assert = require('assert'); +const utils = require('./utils'); +const java = require('js-to-java'); + +describe('exception.test.js', function() { + describe('v1.0', function() { + it('should read java exception as js error', function() { + let ioe = hessian.decode(utils.bytes('v1/exception/IOException')); assert(ioe instanceof Error); assert(ioe.name === 'java.io.IOException'); assert(ioe.message === 'this is a java IOException instance'); @@ -26,7 +16,7 @@ describe('exception.test.js', function () { ioe.stack === 'java.io.IOException: this is a java IOException instance\n at hessian.Main.main (Main.java:1283)' ); - var ioe = hessian.decode(utils.bytes('v1/exception/IOException'), true); + ioe = hessian.decode(utils.bytes('v1/exception/IOException'), true); assert(ioe.$ instanceof Error); assert(ioe.$.name === 'java.io.IOException'); assert(ioe.$.message === 'this is a java IOException instance'); @@ -34,7 +24,7 @@ describe('exception.test.js', function () { ioe.$.stack === 'java.io.IOException: this is a java IOException instance\n at hessian.Main.main (Main.java:1283)' ); - var e = hessian.decode(utils.bytes('v1/exception/UndeclaredThrowableException')); + let e = hessian.decode(utils.bytes('v1/exception/UndeclaredThrowableException')); assert(e instanceof Error); assert((e instanceof Error) === true); assert(e.name === 'java.io.IOException'); @@ -46,7 +36,7 @@ describe('exception.test.js', function () { assert(e.cause); assert(e.cause.detailMessage === 'this is a java IOException instance'); - var e = hessian.decode(utils.bytes('v1/exception/UndeclaredThrowableException'), true); + e = hessian.decode(utils.bytes('v1/exception/UndeclaredThrowableException'), true); assert(e.$ instanceof Error); assert(e.$.name === 'java.io.IOException'); assert(e.$.message === 'this is a java IOException instance'); @@ -58,7 +48,7 @@ describe('exception.test.js', function () { assert(e.$.cause.$ instanceof Error); assert(e.$.cause.$.name === 'java.io.IOException'); - var e = hessian.decode(utils.bytes('v1/exception/UndeclaredThrowableException2')); + e = hessian.decode(utils.bytes('v1/exception/UndeclaredThrowableException2')); assert(e instanceof Error); assert((e instanceof Error) === true); assert(e.name === 'java.io.IOException'); @@ -67,7 +57,7 @@ describe('exception.test.js', function () { e.stack === 'java.io.IOException: 模拟测试异常; this is a java IOException instance\n at hessian.Main.main (Main.java:1303)' ); - var e = hessian.decode(utils.bytes('v1/exception/UndeclaredThrowableException2'), true); + e = hessian.decode(utils.bytes('v1/exception/UndeclaredThrowableException2'), true); assert(e.$ instanceof Error); assert(e.$.name === 'java.io.IOException'); assert(e.$.message === '模拟测试异常; this is a java IOException instance'); @@ -77,9 +67,9 @@ describe('exception.test.js', function () { }); }); - describe('v2.0', function () { - it('should read java exception as js error', function () { - var ioe = hessian.decode(utils.bytes('v2/exception/IOException'), '2.0'); + describe('v2.0', function() { + it('should read java exception as js error', function() { + const ioe = hessian.decode(utils.bytes('v2/exception/IOException'), '2.0'); assert(ioe instanceof Error); assert((ioe instanceof Error) === true); assert(ioe.name === 'java.io.IOException'); @@ -88,7 +78,7 @@ describe('exception.test.js', function () { ioe.stack === 'java.io.IOException: this is a java IOException instance\n at hessian.Main.main (Main.java:1283)' ); - var e = hessian.decode(utils.bytes('v2/exception/UndeclaredThrowableException'), '2.0'); + let e = hessian.decode(utils.bytes('v2/exception/UndeclaredThrowableException'), '2.0'); assert(e instanceof Error); assert((e instanceof Error) === true); assert(e.name === 'java.io.IOException'); @@ -97,7 +87,7 @@ describe('exception.test.js', function () { e.stack === 'java.io.IOException: this is a java IOException instance\n at hessian.Main.main (Main.java:1283)' ); - var e = hessian.decode(utils.bytes('v2/exception/UndeclaredThrowableException2'), '2.0'); + e = hessian.decode(utils.bytes('v2/exception/UndeclaredThrowableException2'), '2.0'); assert(e instanceof Error); assert((e instanceof Error) === true); assert(e.name === 'java.io.IOException'); @@ -107,8 +97,8 @@ describe('exception.test.js', function () { ); }); - it('should read hessian 1.0 exception', function () { - var ioe = hessian.decode(utils.bytes('v1/exception/IOException'), '2.0'); + it('should read hessian 1.0 exception', function() { + const ioe = hessian.decode(utils.bytes('v1/exception/IOException'), '2.0'); assert(ioe instanceof Error); assert((ioe instanceof Error) === true); assert(ioe.name === 'java.io.IOException'); @@ -117,7 +107,7 @@ describe('exception.test.js', function () { ioe.stack === 'java.io.IOException: this is a java IOException instance\n at hessian.Main.main (Main.java:1283)' ); - var e = hessian.decode(utils.bytes('v1/exception/UndeclaredThrowableException'), '2.0'); + let e = hessian.decode(utils.bytes('v1/exception/UndeclaredThrowableException'), '2.0'); assert(e instanceof Error); assert((e instanceof Error) === true); assert(e.name === 'java.io.IOException'); @@ -126,7 +116,7 @@ describe('exception.test.js', function () { e.stack === 'java.io.IOException: this is a java IOException instance\n at hessian.Main.main (Main.java:1283)' ); - var e = hessian.decode(utils.bytes('v1/exception/UndeclaredThrowableException2'), '2.0'); + e = hessian.decode(utils.bytes('v1/exception/UndeclaredThrowableException2'), '2.0'); assert(e instanceof Error); assert((e instanceof Error) === true); assert(e.name === 'java.io.IOException'); @@ -136,21 +126,21 @@ describe('exception.test.js', function () { ); }); - it('should read exception type not endsWith Exception', function () { - var e = hessian.decode(new Buffer('4FB9636F6D2E616C697061792E736F66612E7270632E717569636B73746172742E54657374244572726F72940D64657461696C4D6573736167650563617573650A737461636B54726163651473757070726573736564457863657074696F6E736F90076D6573736167654A005674001C5B6A6176612E6C616E672E537461636B5472616365456C656D656E746E014FAB6A6176612E6C616E672E537461636B5472616365456C656D656E74940E6465636C6172696E67436C6173730A6D6574686F644E616D650866696C654E616D650A6C696E654E756D6265726F91530023636F6D2E616C697061792E736F66612E7270632E717569636B73746172742E54657374046D61696E09546573742E6A617661AE7A567400326A6176612E7574696C2E436F6C6C656374696F6E7324556E6D6F6469666961626C6552616E646F6D4163636573734C6973746E007A', 'hex'), '2.0'); + it('should read exception type not endsWith Exception', function() { + const e = hessian.decode(Buffer.from('4FB9636F6D2E616C697061792E736F66612E7270632E717569636B73746172742E54657374244572726F72940D64657461696C4D6573736167650563617573650A737461636B54726163651473757070726573736564457863657074696F6E736F90076D6573736167654A005674001C5B6A6176612E6C616E672E537461636B5472616365456C656D656E746E014FAB6A6176612E6C616E672E537461636B5472616365456C656D656E74940E6465636C6172696E67436C6173730A6D6574686F644E616D650866696C654E616D650A6C696E654E756D6265726F91530023636F6D2E616C697061792E736F66612E7270632E717569636B73746172742E54657374046D61696E09546573742E6A617661AE7A567400326A6176612E7574696C2E436F6C6C656374696F6E7324556E6D6F6469666961626C6552616E646F6D4163636573734C6973746E007A', 'hex'), '2.0'); assert(e instanceof Error); assert(e.name === 'com.alipay.sofa.rpc.quickstart.Test$Error'); assert(e.message === 'message'); assert(e.stack === 'com.alipay.sofa.rpc.quickstart.Test$Error: message\n at com.alipay.sofa.rpc.quickstart.Test.main (Test.java:30)'); }); - it('should detect exception without cause', function () { - var e = java.exception(new Error('mock error')); + it('should detect exception without cause', function() { + const e = java.exception(new Error('mock error')); delete e.$.cause; - var buf = hessian.encode(e, '2.0'); + let buf = hessian.encode(e, '2.0'); assert(Buffer.isBuffer(buf)); - var err = hessian.decode(buf, '2.0'); + let err = hessian.decode(buf, '2.0'); assert(err instanceof Error); assert(err.message === 'Error: mock error'); diff --git a/test/inner_class.test.js b/test/inner_class.test.js index 669498f..cd74847 100644 --- a/test/inner_class.test.js +++ b/test/inner_class.test.js @@ -1,28 +1,28 @@ 'use strict'; -var assert = require('assert'); -var hessian = require('../'); +const assert = require('assert'); +const hessian = require('../'); describe('test/inner_class.test.js', function() { - var buf = new Buffer('4F9654657374564F940773756363657373096572726F72436F64650C6572726F724D657373616765046C6973746F90544E4E566E024F9C54657374564F24496E6E657292026964067468697324306F91033132334A006F91033332314A007A', 'hex'); + const buf = Buffer.from('4F9654657374564F940773756363657373096572726F72436F64650C6572726F724D657373616765046C6973746F90544E4E566E024F9C54657374564F24496E6E657292026964067468697324306F91033132334A006F91033332314A007A', 'hex'); it('should decode inner class and without $this prop', function() { - var r = hessian.decode(buf, '2.0'); + let r = hessian.decode(buf, '2.0'); assert.deepEqual(r, { success: true, errorCode: null, errorMessage: null, - list: [{ id: '123' }, { id: '321' }] + list: [{ id: '123' }, { id: '321' }], }); - var classCache = new Map(); + const classCache = new Map(); classCache.enableCompile = true; - r = hessian.decode(buf, '2.0', { classCache: classCache }); + r = hessian.decode(buf, '2.0', { classCache }); assert.deepEqual(r, { success: true, errorCode: null, errorMessage: null, - list: [{ id: '123' }, { id: '321' }] + list: [{ id: '123' }, { id: '321' }], }); }); }); diff --git a/test/int.test.js b/test/int.test.js index b15faf8..ec764ca 100644 --- a/test/int.test.js +++ b/test/int.test.js @@ -1,4 +1,4 @@ -/*! +/* ! * hessian.js - test/int.test.js * * Copyright(c) 2014 @@ -8,31 +8,31 @@ * fengmk2 (http://fengmk2.github.com) */ -"use strict"; +'use strict'; -var assert = require('assert'); -var hessian = require('../'); -var utils = require('./utils'); +const assert = require('assert'); +const hessian = require('../'); +const utils = require('./utils'); -describe('int.test.js', function () { - it('should read integer 300', function () { +describe('int.test.js', function() { + it('should read integer 300', function() { assert( - hessian.decode(new Buffer(['I'.charCodeAt(0), 0x00, 0x00, 0x01, 0x2c])) === 300 + hessian.decode(Buffer.from([ 'I'.charCodeAt(0), 0x00, 0x00, 0x01, 0x2c ])) === 300 ); }); - it('should write integer 300', function () { + it('should write integer 300', function() { assert.deepEqual( hessian.encode(300), - new Buffer(['I'.charCodeAt(0), 0x00, 0x00, 0x01, 0x2c]) + Buffer.from([ 'I'.charCodeAt(0), 0x00, 0x00, 0x01, 0x2c ]) ); }); - it('should write integer 0', function () { - assert.deepEqual(hessian.encode(0), new Buffer(['I'.charCodeAt(0), 0, 0, 0, 0])); + it('should write integer 0', function() { + assert.deepEqual(hessian.encode(0), Buffer.from([ 'I'.charCodeAt(0), 0, 0, 0, 0 ])); }); - it('should write number as java write', function () { + it('should write number as java write', function() { assert.deepEqual(hessian.encode(0, '1.0'), utils.bytes('v1/number/0')); assert.deepEqual(hessian.encode(1), utils.bytes('v1/number/1')); assert.deepEqual(hessian.encode(10), utils.bytes('v1/number/10')); @@ -52,7 +52,7 @@ describe('int.test.js', function () { assert.deepEqual(hessian.encode(-262145, '1.0'), utils.bytes('v1/number/-262145')); }); - it('should read java number bin', function () { + it('should read java number bin', function() { assert(hessian.decode(utils.bytes('v1/number/0'), '1.0') === 0); assert(hessian.decode(utils.bytes('v1/number/1'), '1.0') === 1); assert(hessian.decode(utils.bytes('v1/number/10'), '1.0') === 10); @@ -71,30 +71,30 @@ describe('int.test.js', function () { assert(hessian.decode(utils.bytes('v1/number/-262145'), '1.0') === -262145); }); - describe('v2.0', function () { - it('should read compact integers', function () { - assert(hessian.decode(new Buffer([0x90]), '2.0') === 0); - assert(hessian.decode(new Buffer([0x80]), '2.0') === -16); - assert(hessian.decode(new Buffer([0xbf]), '2.0') === 47); + describe('v2.0', function() { + it('should read compact integers', function() { + assert(hessian.decode(Buffer.from([ 0x90 ]), '2.0') === 0); + assert(hessian.decode(Buffer.from([ 0x80 ]), '2.0') === -16); + assert(hessian.decode(Buffer.from([ 0xbf ]), '2.0') === 47); - assert(hessian.decode(new Buffer([0xc8, 0x00]), '2.0') === 0); - assert(hessian.decode(new Buffer([0xc0, 0x00]), '2.0') === -2048); - assert(hessian.decode(new Buffer([0xc7, 0x00]), '2.0') === -256); - assert(hessian.decode(new Buffer([0xcf, 0xff]), '2.0') === 2047); + assert(hessian.decode(Buffer.from([ 0xc8, 0x00 ]), '2.0') === 0); + assert(hessian.decode(Buffer.from([ 0xc0, 0x00 ]), '2.0') === -2048); + assert(hessian.decode(Buffer.from([ 0xc7, 0x00 ]), '2.0') === -256); + assert(hessian.decode(Buffer.from([ 0xcf, 0xff ]), '2.0') === 2047); - assert(hessian.decode(new Buffer([0xd4, 0x00, 0x00]), '2.0') === 0); - assert(hessian.decode(new Buffer([0xd0, 0x00, 0x00]), '2.0') === -262144); - assert(hessian.decode(new Buffer([0xd7, 0xff, 0xff]), '2.0') === 262143); + assert(hessian.decode(Buffer.from([ 0xd4, 0x00, 0x00 ]), '2.0') === 0); + assert(hessian.decode(Buffer.from([ 0xd0, 0x00, 0x00 ]), '2.0') === -262144); + assert(hessian.decode(Buffer.from([ 0xd7, 0xff, 0xff ]), '2.0') === 262143); assert( - hessian.decode(new Buffer(['I'.charCodeAt(0), 0x00, 0x00, 0x00, 0x00]), '2.0') === 0 + hessian.decode(Buffer.from([ 'I'.charCodeAt(0), 0x00, 0x00, 0x00, 0x00 ]), '2.0') === 0 ); assert( - hessian.decode(new Buffer(['I'.charCodeAt(0), 0x00, 0x00, 0x01, 0x2c]), '2.0') === 300 + hessian.decode(Buffer.from([ 'I'.charCodeAt(0), 0x00, 0x00, 0x01, 0x2c ]), '2.0') === 300 ); }); - it('should write number as java write', function () { + it('should write number as java write', function() { assert.deepEqual(hessian.encode(0, '2.0'), utils.bytes('v2/number/0')); assert.deepEqual(hessian.encode(1, '2.0'), utils.bytes('v2/number/1')); assert.deepEqual(hessian.encode(10, '2.0'), utils.bytes('v2/number/10')); @@ -113,7 +113,7 @@ describe('int.test.js', function () { assert.deepEqual(hessian.encode(-262145, '2.0'), utils.bytes('v2/number/-262145')); }); - it('should read java number bin', function () { + it('should read java number bin', function() { assert(hessian.decode(utils.bytes('v2/number/0'), '2.0') === 0); assert(hessian.decode(utils.bytes('v2/number/1'), '2.0') === 1); assert(hessian.decode(utils.bytes('v2/number/10'), '2.0') === 10); @@ -132,7 +132,7 @@ describe('int.test.js', function () { assert(hessian.decode(utils.bytes('v2/number/-262145'), '2.0') === -262145); }); - it('should read hessian 1.0 number bin', function () { + it('should read hessian 1.0 number bin', function() { assert(hessian.decode(utils.bytes('v1/number/0'), '2.0') === 0); assert(hessian.decode(utils.bytes('v1/number/1'), '2.0') === 1); assert(hessian.decode(utils.bytes('v1/number/10'), '2.0') === 10); @@ -151,63 +151,63 @@ describe('int.test.js', function () { assert(hessian.decode(utils.bytes('v1/number/-262145'), '2.0') === -262145); }); - it('should write compact integers', function () { + it('should write compact integers', function() { // -16 ~ 47 - var buf = hessian.encode(0, '2.0'); + let buf = hessian.encode(0, '2.0'); assert(buf.length === 1); assert(buf[0] === 0x90); - assert.deepEqual(buf, new Buffer([0x90])); + assert.deepEqual(buf, Buffer.from([ 0x90 ])); buf = hessian.encode(1, '2.0'); assert(buf.length === 1); assert(buf[0] === 0x91); - assert.deepEqual(buf, new Buffer([0x91])); + assert.deepEqual(buf, Buffer.from([ 0x91 ])); buf = hessian.encode(-16, '2.0'); assert(buf.length === 1); assert(buf[0] === 0x80); - assert.deepEqual(buf, new Buffer([0x80])); + assert.deepEqual(buf, Buffer.from([ 0x80 ])); buf = hessian.encode(46, '2.0'); assert(buf.length === 1); assert(buf[0] === 0xbe); - assert.deepEqual(buf, new Buffer([0xbe])); + assert.deepEqual(buf, Buffer.from([ 0xbe ])); buf = hessian.encode(47, '2.0'); assert(buf.length === 1); assert(buf[0] === 0xbf); - assert.deepEqual(buf, new Buffer([0xbf])); + assert.deepEqual(buf, Buffer.from([ 0xbf ])); // -2048 ~ 2047 buf = hessian.encode(-2048, '2.0'); assert(buf.length === 2); assert(buf[0] === 0xc0); assert(buf[1] === 0x00); - assert.deepEqual(buf, new Buffer([0xc0, 0x00])); + assert.deepEqual(buf, Buffer.from([ 0xc0, 0x00 ])); buf = hessian.encode(-256, '2.0'); assert(buf.length === 2); assert(buf[0] === 0xc7); assert(buf[1] === 0x00); - assert.deepEqual(buf, new Buffer([0xc7, 0x00])); + assert.deepEqual(buf, Buffer.from([ 0xc7, 0x00 ])); buf = hessian.encode(255, '2.0'); assert(buf.length === 2); assert(buf[0] === 0xc8); assert(buf[1] === 0xff); - assert.deepEqual(buf, new Buffer([0xc8, 0xff])); + assert.deepEqual(buf, Buffer.from([ 0xc8, 0xff ])); buf = hessian.encode(256, '2.0'); assert(buf.length === 2); assert(buf[0] === 0xc9); assert(buf[1] === 0x00); - assert.deepEqual(buf, new Buffer([0xc9, 0x00])); + assert.deepEqual(buf, Buffer.from([ 0xc9, 0x00 ])); buf = hessian.encode(2047, '2.0'); assert(buf.length === 2); assert(buf[0] === 0xcf); assert(buf[1] === 0xff); - assert.deepEqual(buf, new Buffer([0xcf, 0xff])); + assert.deepEqual(buf, Buffer.from([ 0xcf, 0xff ])); // -262144 ~ 262143 buf = hessian.encode(-262144, '2.0'); @@ -215,14 +215,14 @@ describe('int.test.js', function () { assert(buf[0] === 0xd0); assert(buf[1] === 0x00); assert(buf[2] === 0x00); - assert.deepEqual(buf, new Buffer([0xd0, 0x00, 0x00])); + assert.deepEqual(buf, Buffer.from([ 0xd0, 0x00, 0x00 ])); buf = hessian.encode(262143, '2.0'); assert(buf.length === 3); assert(buf[0] === 0xd7); assert(buf[1] === 0xff); assert(buf[2] === 0xff); - assert.deepEqual(buf, new Buffer([0xd7, 0xff, 0xff])); + assert.deepEqual(buf, Buffer.from([ 0xd7, 0xff, 0xff ])); // 262144 buf = hessian.encode(262144, '2.0'); @@ -232,7 +232,7 @@ describe('int.test.js', function () { assert(buf[2] === 0x04); assert(buf[3] === 0x00); assert(buf[4] === 0x00); - assert.deepEqual(buf, new Buffer(['I'.charCodeAt(0), 0x00, 0x04, 0x00, 0x00])); + assert.deepEqual(buf, Buffer.from([ 'I'.charCodeAt(0), 0x00, 0x04, 0x00, 0x00 ])); buf = hessian.encode(-262145, '2.0'); assert(buf.length === 5); @@ -241,7 +241,7 @@ describe('int.test.js', function () { assert(buf[2] === 0xfb); assert(buf[3] === 0xff); assert(buf[4] === 0xff); - assert.deepEqual(buf, new Buffer(['I'.charCodeAt(0), 0xff, 0xfb, 0xff, 0xff])); + assert.deepEqual(buf, Buffer.from([ 'I'.charCodeAt(0), 0xff, 0xfb, 0xff, 0xff ])); }); }); }); diff --git a/test/list.test.js b/test/list.test.js index 7590d0e..4303f6b 100644 --- a/test/list.test.js +++ b/test/list.test.js @@ -8,242 +8,241 @@ * fengmk2 (http://fengmk2.github.com) */ -"use strict"; +'use strict'; -var assert = require('assert'); -var java = require('js-to-java'); -var hessian = require('../'); -var utils = require('./utils'); +const assert = require('assert'); +const java = require('js-to-java'); +const hessian = require('../'); +const utils = require('./utils'); -describe('list.test.js', function () { - var intListBuffer = Buffer.concat([ - new Buffer([ +describe('list.test.js', function() { + const intListBuffer = Buffer.concat([ + Buffer.from([ 'V'.charCodeAt(0), - 't'.charCodeAt(0), 0x00, 0x04 + 't'.charCodeAt(0), 0x00, 0x04, ]), - new Buffer('[int'), - new Buffer([ + Buffer.from('[int'), + Buffer.from([ 'l'.charCodeAt(0), 0, 0, 0, 0x02, 'I'.charCodeAt(0), 0, 0, 0, 0x00, 'I'.charCodeAt(0), 0, 0, 0, 0x01, - 'z'.charCodeAt(0) - ]) + 'z'.charCodeAt(0), + ]), ]); - it('should read int[] = {0, 1}', function () { - assert.deepEqual(hessian.decode(intListBuffer), [0, 1]); + it('should read int[] = {0, 1}', function() { + assert.deepEqual(hessian.decode(intListBuffer), [ 0, 1 ]); assert.deepEqual(hessian.decode(intListBuffer, true), { - '$class': '[int', - '$': [ { '$class': 'int', '$': 0 }, { '$class': 'int', '$': 1 } ] + $class: '[int', + $: [{ $class: 'int', $: 0 }, { $class: 'int', $: 1 }], }); }); - it('should write int[] = {0, 1}', function () { - var buf = hessian.encode(java.array.int([0, 1])); + it('should write int[] = {0, 1}', function() { + const buf = hessian.encode(java.array.int([ 0, 1 ])); assert(Buffer.isBuffer(buf)); assert(buf.length === intListBuffer.length); assert.deepEqual(buf, intListBuffer); assert.deepEqual(hessian.encode({ $class: '[int', - $: [0, 1] + $: [ 0, 1 ], }), intListBuffer); // empty list - var empty = Buffer.concat([ - new Buffer([ + const empty = Buffer.concat([ + Buffer.from([ 'V'.charCodeAt(0), - 't'.charCodeAt(0), 0x00, 0x04 + 't'.charCodeAt(0), 0x00, 0x04, ]), - new Buffer('[int'), - new Buffer([ + Buffer.from('[int'), + Buffer.from([ 'l'.charCodeAt(0), 0, 0, 0, 0x00, - 'z'.charCodeAt(0) - ]) + 'z'.charCodeAt(0), + ]), ]); assert.deepEqual(hessian.decode(empty), []); }); - it('should read write anonymous variable-length list = {0, null, "foobar"}', function () { - var anonymousList = Buffer.concat([ - new Buffer('V'), - new Buffer([ + it('should read write anonymous variable-length list = {0, null, "foobar"}', function() { + const anonymousList = Buffer.concat([ + Buffer.from('V'), + Buffer.from([ 'I'.charCodeAt(0), 0, 0, 0, 0x00, 'N'.charCodeAt(0), 'S'.charCodeAt(0), 0, 0x06, ]), - new Buffer('foobar'), - new Buffer('z'), + Buffer.from('foobar'), + Buffer.from('z'), ]); - assert.deepEqual(hessian.decode(anonymousList), [0, null, 'foobar']); + assert.deepEqual(hessian.decode(anonymousList), [ 0, null, 'foobar' ]); // empty - var emptyList = Buffer.concat([ - new Buffer('V'), - new Buffer('z'), + const emptyList = Buffer.concat([ + Buffer.from('V'), + Buffer.from('z'), ]); assert.deepEqual(hessian.decode(emptyList), []); }); - it('should write and read untyped list', function () { - assert.deepEqual(hessian.encode([1, 2, 'foo'], '1.0'), utils.bytes('v1/list/untyped_list')); + it('should write and read untyped list', function() { + assert.deepEqual(hessian.encode([ 1, 2, 'foo' ], '1.0'), utils.bytes('v1/list/untyped_list')); assert.deepEqual(hessian.encode([], '1.0'), utils.bytes('v1/list/untyped_[]')); - assert.deepEqual(hessian.decode(utils.bytes('v1/list/untyped_list'), '1.0'), [1, 2, 'foo']); + assert.deepEqual(hessian.decode(utils.bytes('v1/list/untyped_list'), '1.0'), [ 1, 2, 'foo' ]); assert.deepEqual(hessian.decode(utils.bytes('v1/list/untyped_list'), '1.0', true), { $class: 'java.util.ArrayList', $: [ - { '$class': 'int', '$': 1 }, - { '$class': 'int', '$': 2 }, - { '$class': 'java.lang.String', '$': 'foo' } - ] + { $class: 'int', $: 1 }, + { $class: 'int', $: 2 }, + { $class: 'java.lang.String', $: 'foo' }, + ], }); assert.deepEqual(hessian.decode(utils.bytes('v1/list/untyped_[]'), '1.0'), []); assert.deepEqual( - hessian.decode(utils.bytes('v1/list/untyped_[foo,bar]'), '1.0', true), - { + hessian.decode(utils.bytes('v1/list/untyped_[foo,bar]'), '1.0', true), { $class: 'java.util.ArrayList', $: [ - { '$class': 'java.lang.String', '$': 'foo' }, - { '$class': 'java.lang.String', '$': 'bar' } - ] + { $class: 'java.lang.String', $: 'foo' }, + { $class: 'java.lang.String', $: 'bar' }, + ], } ); // java.util.ArrayList as simple assert.deepEqual(hessian.encode({ $class: 'java.util.ArrayList', - $: [1, 2, 'foo'] + $: [ 1, 2, 'foo' ], }, '1.0'), utils.bytes('v1/list/untyped_list')); }); - it('should write and read typed fixed-length list', function () { + it('should write and read typed fixed-length list', function() { assert.deepEqual(hessian.encode({ $class: 'hessian.demo.SomeArrayList', - $: ['ok', 'some list'] + $: [ 'ok', 'some list' ], }, '1.0'), utils.bytes('v1/list/typed_list')); assert.deepEqual(hessian.decode(utils.bytes('v1/list/typed_list'), '1.0', true), { - '$class': 'hessian.demo.SomeArrayList', - '$': [ - { '$class': 'java.lang.String', '$': 'ok' }, - { '$class': 'java.lang.String', '$': 'some list' } - ] - }); + $class: 'hessian.demo.SomeArrayList', + $: [ + { $class: 'java.lang.String', $: 'ok' }, + { $class: 'java.lang.String', $: 'some list' }, + ], + }); assert.deepEqual( hessian.decode(utils.bytes('v1/list/typed_list'), '1.0'), [ 'ok', 'some list' ] ); - var list = { + const list = { $class: '[int', - $: [1, 2, 3] + $: [ 1, 2, 3 ], }; assert.deepEqual(hessian.encode(list, '1.0'), utils.bytes('v1/list/[int')); - assert.deepEqual(hessian.decode(utils.bytes('v1/list/[int'), '1.0'), [1, 2, 3]); + assert.deepEqual(hessian.decode(utils.bytes('v1/list/[int'), '1.0'), [ 1, 2, 3 ]); assert.deepEqual(hessian.decode(utils.bytes('v1/list/[int'), '1.0', true), { - '$class': '[int', - '$': [ - { '$class': 'int', '$': 1 }, - { '$class': 'int', '$': 2 }, - { '$class': 'int', '$': 3 } - ] + $class: '[int', + $: [ + { $class: 'int', $: 1 }, + { $class: 'int', $: 2 }, + { $class: 'int', $: 3 }, + ], }); - var strs = { + const strs = { $class: '[string', - $: ['1', '@', '3'] + $: [ '1', '@', '3' ], }; assert.deepEqual(hessian.encode(strs, '1.0'), utils.bytes('v1/list/[string')); assert.deepEqual(hessian.decode(utils.bytes('v1/list/[string'), '1.0', true), { - '$class': '[string', - '$': [ - { '$class': 'java.lang.String', '$': '1' }, - { '$class': 'java.lang.String', '$': '@' }, - { '$class': 'java.lang.String', '$': '3' } - ] + $class: '[string', + $: [ + { $class: 'java.lang.String', $: '1' }, + { $class: 'java.lang.String', $: '@' }, + { $class: 'java.lang.String', $: '3' }, + ], }); }); - describe('v2.0', function () { - it('should write and read untyped list', function () { - assert.deepEqual(hessian.encode([1, 2, 'foo'], '2.0'), utils.bytes('v2/list/untyped_list')); + describe('v2.0', function() { + it('should write and read untyped list', function() { + assert.deepEqual(hessian.encode([ 1, 2, 'foo' ], '2.0'), utils.bytes('v2/list/untyped_list')); assert.deepEqual(hessian.encode([], '2.0'), utils.bytes('v2/list/untyped_[]')); - assert.deepEqual(hessian.decode(utils.bytes('v2/list/untyped_list'), '2.0'), [1, 2, 'foo']); + assert.deepEqual(hessian.decode(utils.bytes('v2/list/untyped_list'), '2.0'), [ 1, 2, 'foo' ]); assert.deepEqual( hessian.decode(utils.bytes('v2/list/untyped_list'), '2.0', true), - [1, 2, 'foo'] + [ 1, 2, 'foo' ] ); assert.deepEqual(hessian.decode(utils.bytes('v2/list/untyped_[]'), '2.0'), []); assert.deepEqual( hessian.decode(utils.bytes('v2/list/untyped_[foo,bar]'), '2.0', true), - ['foo', 'bar'] + [ 'foo', 'bar' ] ); // java.util.ArrayList as simple assert.deepEqual(hessian.encode({ $class: 'java.util.ArrayList', - $: [1, 2, 'foo'] + $: [ 1, 2, 'foo' ], }, '2.0'), utils.bytes('v2/list/untyped_list')); // encode again should cache class assert.deepEqual(hessian.encode({ $class: 'java.util.ArrayList', - $: [1, 2, 'foo'] + $: [ 1, 2, 'foo' ], }, '2.0'), utils.bytes('v2/list/untyped_list')); }); - it('should write and read typed fixed-length list', function () { + it('should write and read typed fixed-length list', function() { assert.deepEqual(hessian.encode({ $class: 'hessian.demo.SomeArrayList', - $: ['ok', 'some list'] + $: [ 'ok', 'some list' ], }, '2.0'), utils.bytes('v2/list/typed_list')); // encode again should use type cache assert.deepEqual(hessian.encode({ $class: 'hessian.demo.SomeArrayList', - $: ['ok', 'some list'] + $: [ 'ok', 'some list' ], }, '2.0'), utils.bytes('v2/list/typed_list')); assert.deepEqual(hessian.decode(utils.bytes('v2/list/typed_list'), '2.0', true), { - '$class': 'hessian.demo.SomeArrayList', - '$': [ 'ok', 'some list' ] - }); + $class: 'hessian.demo.SomeArrayList', + $: [ 'ok', 'some list' ], + }); assert.deepEqual( hessian.decode(utils.bytes('v2/list/typed_list'), '2.0'), [ 'ok', 'some list' ] ); - var list = { + const list = { $class: '[int', - $: [1, 2, 3] + $: [ 1, 2, 3 ], }; assert.deepEqual(hessian.encode(list, '2.0'), utils.bytes('v2/list/[int')); assert.deepEqual(hessian.encode(list, '2.0'), utils.bytes('v2/list/[int')); - assert.deepEqual(hessian.decode(utils.bytes('v2/list/[int'), '2.0'), [1, 2, 3]); + assert.deepEqual(hessian.decode(utils.bytes('v2/list/[int'), '2.0'), [ 1, 2, 3 ]); // encode again should use type cache assert.deepEqual(hessian.decode(utils.bytes('v2/list/[int'), '2.0', true), list); - var strs = { + const strs = { $class: '[string', - $: ['1', '@', '3'] + $: [ '1', '@', '3' ], }; assert.deepEqual(hessian.encode(strs, '2.0'), utils.bytes('v2/list/[string')); assert.deepEqual(hessian.decode(utils.bytes('v2/list/[string'), '2.0', true), strs); }); - it('should read hessian 1.0 untyped list', function () { - assert.deepEqual(hessian.decode(utils.bytes('v1/list/untyped_list'), '2.0'), [1, 2, 'foo']); + it('should read hessian 1.0 untyped list', function() { + assert.deepEqual(hessian.decode(utils.bytes('v1/list/untyped_list'), '2.0'), [ 1, 2, 'foo' ]); assert.deepEqual( hessian.decode(utils.bytes('v1/list/untyped_list'), '2.0', true), - [1, 2, 'foo'] + [ 1, 2, 'foo' ] ); assert.deepEqual(hessian.decode(utils.bytes('v1/list/untyped_[]'), '2.0'), []); assert.deepEqual( hessian.decode(utils.bytes('v1/list/untyped_[foo,bar]'), '2.0', true), - ['foo', 'bar'] + [ 'foo', 'bar' ] ); }); }); diff --git a/test/long.test.js b/test/long.test.js index 46d3e87..7f08f6a 100644 --- a/test/long.test.js +++ b/test/long.test.js @@ -1,4 +1,4 @@ -/*! +/* ! * hessian.js - test/long.test.js * * Copyright(c) 2014 @@ -8,32 +8,33 @@ * fengmk2 (http://fengmk2.github.com) */ -"use strict"; +'use strict'; -var assert = require('assert'); -var java = require('js-to-java'); -var Long = require('long'); -var hessian = require('../'); -var utils = require('./utils'); +const assert = require('assert'); +const java = require('js-to-java'); +const Long = require('long'); +const hessian = require('../'); +const utils = require('./utils'); -describe('long.test.js', function () { - var longBuffer = new Buffer(['L'.charCodeAt(0), 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x2c]); +describe('long.test.js', function() { + const longBuffer = Buffer.from([ 'L'.charCodeAt(0), 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x2c ]); - it('should write {$class: "java.lang.Long", $: null} => null', function () { - var obj = hessian.decode(hessian.encode({ - $class: "java.lang.Long", $: null + it('should write {$class: "java.lang.Long", $: null} => null', function() { + const obj = hessian.decode(hessian.encode({ + $class: 'java.lang.Long', + $: null, })); assert(obj === null); }); - it('should read long 300', function () { + it('should read long 300', function() { assert(hessian.decode(longBuffer) === 300); }); - it('should write long 300', function () { + it('should write long 300', function() { assert.deepEqual(hessian.encode({ $class: 'long', - $: 300 + $: 300, }), longBuffer); assert.deepEqual(hessian.encode(java.long(300)), longBuffer); assert.deepEqual(hessian.encode(java.long(300)), longBuffer); @@ -41,14 +42,14 @@ describe('long.test.js', function () { assert.deepEqual(hessian.encode(Long.fromNumber(300)), longBuffer); }); - it('should write long 0', function () { + it('should write long 0', function() { assert.deepEqual( hessian.encode(java.long(0)), - new Buffer(['L'.charCodeAt(0), 0, 0, 0, 0, 0, 0, 0, 0]) + Buffer.from([ 'L'.charCodeAt(0), 0, 0, 0, 0, 0, 0, 0, 0 ]) ); }); - it('should write and read equal java impl', function () { + it('should write and read equal java impl', function() { assert.deepEqual(hessian.encode(java.long(0), '1.0'), utils.bytes('v1/long/0')); assert(hessian.decode(utils.bytes('v1/long/0')) === 0); assert.deepEqual(hessian.encode(java.long(-8), '1.0'), utils.bytes('v1/long/-8')); @@ -104,7 +105,7 @@ describe('long.test.js', function () { assert(hessian.decode(utils.bytes('v1/long/2147483648')) === 2147483648); }); - it('should decode with type', function () { + it('should decode with type', function() { assert.deepEqual(hessian.decode(utils.bytes('v1/long/-7'), true), { $class: 'long', $: -7, @@ -121,95 +122,95 @@ describe('long.test.js', function () { }); }); - describe('v2.0', function () { - it('should read compact long', function () { - assert(hessian.decode(new Buffer([0xe0]), '2.0') === 0); - assert(hessian.decode(new Buffer([0xd8]), '2.0') === -8); - assert(hessian.decode(new Buffer([0xef]), '2.0') === 15); + describe('v2.0', function() { + it('should read compact long', function() { + assert(hessian.decode(Buffer.from([ 0xe0 ]), '2.0') === 0); + assert(hessian.decode(Buffer.from([ 0xd8 ]), '2.0') === -8); + assert(hessian.decode(Buffer.from([ 0xef ]), '2.0') === 15); - assert(hessian.decode(new Buffer([0xf8, 0x00]), '2.0') === 0); - assert(hessian.decode(new Buffer([0xf0, 0x00]), '2.0') === -2048); - assert(hessian.decode(new Buffer([0xf7, 0x00]), '2.0') === -256); - assert(hessian.decode(new Buffer([0xff, 0xff]), '2.0') === 2047); + assert(hessian.decode(Buffer.from([ 0xf8, 0x00 ]), '2.0') === 0); + assert(hessian.decode(Buffer.from([ 0xf0, 0x00 ]), '2.0') === -2048); + assert(hessian.decode(Buffer.from([ 0xf7, 0x00 ]), '2.0') === -256); + assert(hessian.decode(Buffer.from([ 0xff, 0xff ]), '2.0') === 2047); - assert(hessian.decode(new Buffer([0x3c, 0x00, 0x00]), '2.0') === 0); - assert(hessian.decode(new Buffer([0x38, 0x00, 0x00]), '2.0') === -262144); - assert(hessian.decode(new Buffer([0x3f, 0xff, 0xff]), '2.0') === 262143); + assert(hessian.decode(Buffer.from([ 0x3c, 0x00, 0x00 ]), '2.0') === 0); + assert(hessian.decode(Buffer.from([ 0x38, 0x00, 0x00 ]), '2.0') === -262144); + assert(hessian.decode(Buffer.from([ 0x3f, 0xff, 0xff ]), '2.0') === 262143); // four octet longs - assert(hessian.decode(new Buffer([0x77, 0x00, 0x00, 0x00, 0x00]), '2.0') === 0); - assert(hessian.decode(new Buffer([0x77, 0x00, 0x00, 0x01, 0x2c]), '2.0') === 300); + assert(hessian.decode(Buffer.from([ 0x77, 0x00, 0x00, 0x00, 0x00 ]), '2.0') === 0); + assert(hessian.decode(Buffer.from([ 0x77, 0x00, 0x00, 0x01, 0x2c ]), '2.0') === 300); assert( - hessian.decode(new Buffer([0x77, 0x7f, 0xff, 0xff, 0xff]), '2.0') === 2147483647 + hessian.decode(Buffer.from([ 0x77, 0x7f, 0xff, 0xff, 0xff ]), '2.0') === 2147483647 ); assert( - hessian.decode(new Buffer([0x77, 0x80, 0x00, 0x00, 0x00]), '2.0') === -2147483648 + hessian.decode(Buffer.from([ 0x77, 0x80, 0x00, 0x00, 0x00 ]), '2.0') === -2147483648 ); }); - it('should read normal long', function () { + it('should read normal long', function() { assert( - hessian.decode(new Buffer([0x4c, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00]), '2.0') === 2147483648 + hessian.decode(Buffer.from([ 0x4c, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 ]), '2.0') === 2147483648 ); }); - it('should write compact long', function () { + it('should write compact long', function() { // -8 ~ 15 - var buf = hessian.encode(java.long(0), '2.0'); + let buf = hessian.encode(java.long(0), '2.0'); assert(buf.length === 1); assert(buf[0] === 0xe0); - assert.deepEqual(buf, new Buffer([0xe0])); + assert.deepEqual(buf, Buffer.from([ 0xe0 ])); buf = hessian.encode(java.long(-8), '2.0'); assert(buf.length === 1); assert(buf[0] === 0xd8); - assert.deepEqual(buf, new Buffer([0xd8])); + assert.deepEqual(buf, Buffer.from([ 0xd8 ])); buf = hessian.encode(java.long(-7), '2.0'); assert(buf.length === 1); assert(buf[0] === 0xd9); - assert.deepEqual(buf, new Buffer([0xd9])); + assert.deepEqual(buf, Buffer.from([ 0xd9 ])); buf = hessian.encode(java.long(15), '2.0'); assert(buf.length === 1); assert(buf[0] === 0xef); - assert.deepEqual(buf, new Buffer([0xef])); + assert.deepEqual(buf, Buffer.from([ 0xef ])); buf = hessian.encode(java.long(14), '2.0'); assert(buf.length === 1); assert(buf[0] === 0xee); - assert.deepEqual(buf, new Buffer([0xee])); + assert.deepEqual(buf, Buffer.from([ 0xee ])); // -2048 ~ 2047 buf = hessian.encode(java.long(-9), '2.0'); assert(buf.length === 2); assert(buf[0] === 0xf7); assert(buf[1] === 0xf7); - assert.deepEqual(buf, new Buffer([0xf7, 0xf7])); + assert.deepEqual(buf, Buffer.from([ 0xf7, 0xf7 ])); buf = hessian.encode(java.long(16), '2.0'); assert(buf.length === 2); assert(buf[0] === 0xf8); assert(buf[1] === 0x10); - assert.deepEqual(buf, new Buffer([0xf8, 0x10])); + assert.deepEqual(buf, Buffer.from([ 0xf8, 0x10 ])); buf = hessian.encode(java.long(255), '2.0'); assert(buf.length === 2); assert(buf[0] === 0xf8); assert(buf[1] === 0xff); - assert.deepEqual(buf, new Buffer([0xf8, 0xff])); + assert.deepEqual(buf, Buffer.from([ 0xf8, 0xff ])); buf = hessian.encode(java.long(-2048), '2.0'); assert(buf.length === 2); assert(buf[0] === 0xf0); assert(buf[1] === 0); - assert.deepEqual(buf, new Buffer([0xf0, 0x00])); + assert.deepEqual(buf, Buffer.from([ 0xf0, 0x00 ])); buf = hessian.encode(java.long(2047), '2.0'); assert(buf.length === 2); assert(buf[0] === 0xff); assert(buf[1] === 0xff); - assert.deepEqual(buf, new Buffer([0xff, 0xff])); + assert.deepEqual(buf, Buffer.from([ 0xff, 0xff ])); // -262144 ~ 262143 buf = hessian.encode(java.long(262143), '2.0'); @@ -217,28 +218,28 @@ describe('long.test.js', function () { assert(buf[0] === 0x3f); assert(buf[1] === 0xff); assert(buf[2] === 0xff); - assert.deepEqual(buf, new Buffer([0x3f, 0xff, 0xff])); + assert.deepEqual(buf, Buffer.from([ 0x3f, 0xff, 0xff ])); buf = hessian.encode(java.long(-262144), '2.0'); assert(buf.length === 3); assert(buf[0] === 0x38); assert(buf[1] === 0); assert(buf[2] === 0); - assert.deepEqual(buf, new Buffer([0x38, 0x00, 0x00])); + assert.deepEqual(buf, Buffer.from([ 0x38, 0x00, 0x00 ])); buf = hessian.encode(java.long(2048), '2.0'); assert(buf.length === 3); assert(buf[0] === 0x3c); assert(buf[1] === 0x08); assert(buf[2] === 0x00); - assert.deepEqual(buf, new Buffer([0x3c, 0x08, 0x00])); + assert.deepEqual(buf, Buffer.from([ 0x3c, 0x08, 0x00 ])); buf = hessian.encode(java.long(-2049), '2.0'); assert(buf.length === 3); assert(buf[0] === 0x3b); assert(buf[1] === 0xf7); assert(buf[2] === 0xff); - assert.deepEqual(buf, new Buffer([0x3b, 0xf7, 0xff])); + assert.deepEqual(buf, Buffer.from([ 0x3b, 0xf7, 0xff ])); // -2147483648 ~ 2147483647 buf = hessian.encode(java.long(-2147483648), '2.0'); @@ -248,7 +249,7 @@ describe('long.test.js', function () { assert(buf[2] === 0x00); assert(buf[3] === 0x00); assert(buf[4] === 0x00); - assert.deepEqual(buf, new Buffer([0x77, 0x80, 0x00, 0x00, 0x00])); + assert.deepEqual(buf, Buffer.from([ 0x77, 0x80, 0x00, 0x00, 0x00 ])); buf = hessian.encode(java.long(2147483647), '2.0'); assert(buf.length === 5); @@ -257,71 +258,71 @@ describe('long.test.js', function () { assert(buf[2] === 0xff); assert(buf[3] === 0xff); assert(buf[4] === 0xff); - assert.deepEqual(buf, new Buffer([0x77, 0x7f, 0xff, 0xff, 0xff])); + assert.deepEqual(buf, Buffer.from([ 0x77, 0x7f, 0xff, 0xff, 0xff ])); // L buf = hessian.encode(java.long(2147483648), '2.0'); assert(buf.length === 9); - assert.deepEqual(buf, new Buffer([0x4c, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00])); + assert.deepEqual(buf, Buffer.from([ 0x4c, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 ])); }); - it('should write { $class: "long": $: "..." } ok', function () { + it('should write { $class: "long": $: "..." } ok', function() { // -8 ~ 15 - var buf = hessian.encode(java.long('0'), '2.0'); + let buf = hessian.encode(java.long('0'), '2.0'); assert.equal(buf.length, 1); assert.equal(buf[0], 0xe0); - assert.deepEqual(buf, new Buffer([0xe0])); + assert.deepEqual(buf, Buffer.from([ 0xe0 ])); buf = hessian.encode(java.long('-8'), '2.0'); assert.equal(buf.length, 1); assert.equal(buf[0], 0xd8); - assert.deepEqual(buf, new Buffer([0xd8])); + assert.deepEqual(buf, Buffer.from([ 0xd8 ])); buf = hessian.encode(java.long('-7'), '2.0'); assert.equal(buf.length, 1); assert.equal(buf[0], 0xd9); - assert.deepEqual(buf, new Buffer([0xd9])); + assert.deepEqual(buf, Buffer.from([ 0xd9 ])); buf = hessian.encode(java.long('15'), '2.0'); assert.equal(buf.length, 1); assert.equal(buf[0], 0xef); - assert.deepEqual(buf, new Buffer([0xef])); + assert.deepEqual(buf, Buffer.from([ 0xef ])); buf = hessian.encode(java.long('14'), '2.0'); assert.equal(buf.length, 1); assert.equal(buf[0], 0xee); - assert.deepEqual(buf, new Buffer([0xee])); + assert.deepEqual(buf, Buffer.from([ 0xee ])); // -2048 ~ 2047 buf = hessian.encode(java.long('-9'), '2.0'); assert.equal(buf.length, 2); assert.equal(buf[0], 0xf7); assert.equal(buf[1], 0xf7); - assert.deepEqual(buf, new Buffer([0xf7, 0xf7])); + assert.deepEqual(buf, Buffer.from([ 0xf7, 0xf7 ])); buf = hessian.encode(java.long('16'), '2.0'); assert.equal(buf.length, 2); assert.equal(buf[0], 0xf8); assert.equal(buf[1], 0x10); - assert.deepEqual(buf, new Buffer([0xf8, 0x10])); + assert.deepEqual(buf, Buffer.from([ 0xf8, 0x10 ])); buf = hessian.encode(java.long('255'), '2.0'); assert.equal(buf.length, 2); assert.equal(buf[0], 0xf8); assert.equal(buf[1], 0xff); - assert.deepEqual(buf, new Buffer([0xf8, 0xff])); + assert.deepEqual(buf, Buffer.from([ 0xf8, 0xff ])); buf = hessian.encode(java.long('-2048'), '2.0'); assert.equal(buf.length, 2); assert.equal(buf[0], 0xf0); assert.equal(buf[1], 0); - assert.deepEqual(buf, new Buffer([0xf0, 0x00])); + assert.deepEqual(buf, Buffer.from([ 0xf0, 0x00 ])); buf = hessian.encode(java.long('2047'), '2.0'); assert.equal(buf.length, 2); assert.equal(buf[0], 0xff); assert.equal(buf[1], 0xff); - assert.deepEqual(buf, new Buffer([0xff, 0xff])); + assert.deepEqual(buf, Buffer.from([ 0xff, 0xff ])); // -262144 ~ 262143 buf = hessian.encode(java.long('262143'), '2.0'); @@ -329,28 +330,28 @@ describe('long.test.js', function () { assert.equal(buf[0], 0x3f); assert.equal(buf[1], 0xff); assert.equal(buf[2], 0xff); - assert.deepEqual(buf, new Buffer([0x3f, 0xff, 0xff])); + assert.deepEqual(buf, Buffer.from([ 0x3f, 0xff, 0xff ])); buf = hessian.encode(java.long('-262144'), '2.0'); assert.equal(buf.length, 3); assert.equal(buf[0], 0x38); assert.equal(buf[1], 0); assert.equal(buf[2], 0); - assert.deepEqual(buf, new Buffer([0x38, 0x00, 0x00])); + assert.deepEqual(buf, Buffer.from([ 0x38, 0x00, 0x00 ])); buf = hessian.encode(java.long('2048'), '2.0'); assert.equal(buf.length, 3); assert.equal(buf[0], 0x3c); assert.equal(buf[1], 0x08); assert.equal(buf[2], 0x00); - assert.deepEqual(buf, new Buffer([0x3c, 0x08, 0x00])); + assert.deepEqual(buf, Buffer.from([ 0x3c, 0x08, 0x00 ])); buf = hessian.encode(java.long('-2049'), '2.0'); assert.equal(buf.length, 3); assert.equal(buf[0], 0x3b); assert.equal(buf[1], 0xf7); assert.equal(buf[2], 0xff); - assert.deepEqual(buf, new Buffer([0x3b, 0xf7, 0xff])); + assert.deepEqual(buf, Buffer.from([ 0x3b, 0xf7, 0xff ])); // -2147483648 ~ 2147483647 buf = hessian.encode(java.long('-2147483648'), '2.0'); @@ -360,7 +361,7 @@ describe('long.test.js', function () { assert.equal(buf[2], 0x00); assert.equal(buf[3], 0x00); assert.equal(buf[4], 0x00); - assert.deepEqual(buf, new Buffer([0x77, 0x80, 0x00, 0x00, 0x00])); + assert.deepEqual(buf, Buffer.from([ 0x77, 0x80, 0x00, 0x00, 0x00 ])); buf = hessian.encode(java.long('2147483647'), '2.0'); assert.equal(buf.length, 5); @@ -369,20 +370,20 @@ describe('long.test.js', function () { assert.equal(buf[2], 0xff); assert.equal(buf[3], 0xff); assert.equal(buf[4], 0xff); - assert.deepEqual(buf, new Buffer([0x77, 0x7f, 0xff, 0xff, 0xff])); + assert.deepEqual(buf, Buffer.from([ 0x77, 0x7f, 0xff, 0xff, 0xff ])); // L buf = hessian.encode(java.long('2147483648'), '2.0'); assert.equal(buf.length, 9); - assert.deepEqual(buf, new Buffer([0x4c, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00])); + assert.deepEqual(buf, Buffer.from([ 0x4c, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 ])); // unsafe long value buf = hessian.encode(java.long('9007199254740992'), '2.0'); assert.equal(buf.length, 9); - assert.deepEqual(buf, new Buffer([0x4c, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])); + assert.deepEqual(buf, Buffer.from([ 0x4c, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ])); }); - it('should write and read equal java impl', function () { + it('should write and read equal java impl', function() { assert.deepEqual(hessian.encode(java.long(Long.fromNumber(15)), '2.0'), utils.bytes('v2/long/15')); assert.deepEqual(hessian.encode(java.long(Long.fromNumber(-8)), '2.0'), utils.bytes('v2/long/-8')); assert.deepEqual(hessian.encode(java.long(Long.fromNumber(-2048)), '2.0'), utils.bytes('v2/long/-2048')); @@ -453,7 +454,7 @@ describe('long.test.js', function () { assert(hessian.decode(utils.bytes('v2/long/2147483648'), '2.0') === 2147483648); }); - it('should read 1.0 bin as well', function () { + it('should read 1.0 bin as well', function() { assert(hessian.decode(utils.bytes('v1/long/0'), '2.0') === 0); assert(hessian.decode(utils.bytes('v1/long/-8'), '2.0') === -8); assert(hessian.decode(utils.bytes('v1/long/-7'), '2.0') === -7); @@ -476,8 +477,8 @@ describe('long.test.js', function () { }); }); - it('should decode with type', function () { - assert.deepEqual(hessian.decode(new Buffer([0x38, 0x00, 0x00]), '2.0', true), { + it('should decode with type', function() { + assert.deepEqual(hessian.decode(Buffer.from([ 0x38, 0x00, 0x00 ]), '2.0', true), { $class: 'long', $: -262144, }); diff --git a/test/map.test.js b/test/map.test.js index f539d9a..d9f266f 100644 --- a/test/map.test.js +++ b/test/map.test.js @@ -1,4 +1,4 @@ -/*! +/* ! * hessian.js - test/map.test.js * * Copyright(c) 2014 @@ -8,14 +8,14 @@ * fengmk2 (http://fengmk2.github.com) */ -"use strict"; +'use strict'; -var assert = require('assert'); -var hessian = require('../'); -var utils = require('./utils'); +const assert = require('assert'); +const hessian = require('../'); +const utils = require('./utils'); -describe('map.test.js', function () { - it('should read Serialization of a Java Object', function () { +describe('map.test.js', function() { + it('should read Serialization of a Java Object', function() { // public class Car implements Serializable { // String model = "Beetle"; // String color = "aquamarine"; @@ -35,124 +35,124 @@ describe('map.test.js', function () { // I x00 x01 x00 x00 // z - var mapBuffer = Buffer.concat([ - new Buffer('M'), - new Buffer([ + const mapBuffer = Buffer.concat([ + Buffer.from('M'), + Buffer.from([ 't'.charCodeAt(0), 0x00, 0x13, ]), - new Buffer('com.caucho.test.Car'), - new Buffer([ + Buffer.from('com.caucho.test.Car'), + Buffer.from([ 'S'.charCodeAt(0), 0x00, 0x05, ]), - new Buffer('model'), - new Buffer([ + Buffer.from('model'), + Buffer.from([ 'S'.charCodeAt(0), 0x00, 0x06, ]), - new Buffer('Beetle'), + Buffer.from('Beetle'), - new Buffer([ + Buffer.from([ 'S'.charCodeAt(0), 0x00, 0x05, ]), - new Buffer('color'), - new Buffer([ + Buffer.from('color'), + Buffer.from([ 'S'.charCodeAt(0), 0x00, 0x0a, ]), - new Buffer('aquamarine'), + Buffer.from('aquamarine'), - new Buffer([ + Buffer.from([ 'S'.charCodeAt(0), 0x00, 0x07, ]), - new Buffer('mileage'), - new Buffer([ + Buffer.from('mileage'), + Buffer.from([ 'I'.charCodeAt(0), 0x00, 0x01, 0x00, 0x00, ]), - new Buffer('z'), + Buffer.from('z'), ]); assert.deepEqual(hessian.decode(mapBuffer), { model: 'Beetle', color: 'aquamarine', - mileage: 65536 + mileage: 65536, }); assert.deepEqual(hessian.decode(mapBuffer, true), { - '$class': 'com.caucho.test.Car', - '$': { - model: { '$class': 'java.lang.String', '$': 'Beetle' }, - color: { '$class': 'java.lang.String', '$': 'aquamarine' }, - mileage: { '$class': 'int', '$': 65536 } - } + $class: 'com.caucho.test.Car', + $: { + model: { $class: 'java.lang.String', $: 'Beetle' }, + color: { $class: 'java.lang.String', $: 'aquamarine' }, + mileage: { $class: 'int', $: 65536 }, + }, }); }); - it('should write {hasOwnProperty: 1, a: 0, b: false, c: null} obj', function () { + it('should write {hasOwnProperty: 1, a: 0, b: false, c: null} obj', function() { /* jshint -W001 */ - var buf = hessian.encode({hasOwnProperty: 1, a: 0, b: false, c: null}); + const buf = hessian.encode({ hasOwnProperty: 1, a: 0, b: false, c: null }); assert(Buffer.isBuffer(buf)); - assert.deepEqual(hessian.decode(buf), {hasOwnProperty: 1, a: 0, b: false, c: null}); + assert.deepEqual(hessian.decode(buf), { hasOwnProperty: 1, a: 0, b: false, c: null }); }); - it('should read A sparse array', function () { + it('should read A sparse array', function() { // map = new HashMap(); // map.put(new Integer(1), "fee"); // map.put(new Integer(16), "fie"); // map.put(new Integer(256), "foe"); - var mapBuffer = Buffer.concat([ - new Buffer('M'), - new Buffer([ + const mapBuffer = Buffer.concat([ + Buffer.from('M'), + Buffer.from([ 'I'.charCodeAt(0), 0, 0, 0, 0x01, ]), - new Buffer([ + Buffer.from([ 'S'.charCodeAt(0), 0x00, 0x03, ]), - new Buffer('fee'), + Buffer.from('fee'), - new Buffer([ + Buffer.from([ 'I'.charCodeAt(0), 0, 0, 0, 0x10, ]), - new Buffer([ + Buffer.from([ 'S'.charCodeAt(0), 0x00, 0x03, ]), - new Buffer('fie'), + Buffer.from('fie'), - new Buffer([ + Buffer.from([ 'I'.charCodeAt(0), 0, 0, 0x01, 0x00, ]), - new Buffer([ + Buffer.from([ 'S'.charCodeAt(0), 0x00, 0x03, ]), - new Buffer('foe'), + Buffer.from('foe'), - new Buffer('z'), + Buffer.from('z'), ]); assert.deepEqual(hessian.decode(mapBuffer), { - '1': 'fee', - '16': 'fie', - '256': 'foe', + 1: 'fee', + 16: 'fie', + 256: 'foe', }); }); - it('should write js object to no type hash map', function () { - var buf = hessian.encode({ foo: '' }); + it('should write js object to no type hash map', function() { + const buf = hessian.encode({ foo: '' }); assert.deepEqual(buf, utils.bytes('v1/map/foo_empty')); assert.deepEqual(hessian.decode(utils.bytes('v1/map/foo_empty'), '1.0'), { - foo: '' + foo: '', }); assert.deepEqual(hessian.encode({ - '123': 456, + 123: 456, foo: 'bar', zero: 0, - '中文key': '中文哈哈value', + 中文key: '中文哈哈value', }), utils.bytes('v1/map/foo_bar')); // read it assert.deepEqual(hessian.decode(utils.bytes('v1/map/foo_bar'), '1.0'), { foo: 'bar', - '中文key': '中文哈哈value', - '123': 456, + 中文key: '中文哈哈value', + 123: 456, zero: 0, }); }); @@ -162,75 +162,75 @@ describe('map.test.js', function () { // pass if not support es6 Map return; } - - var map = new Map(); - map.set({ '$class': 'java.lang.Long', '$': 123 }, 123456); - map.set({ '$class': 'java.lang.Long', '$': 123456 }, 123); - var buf = hessian.encode(map); + + const map = new Map(); + map.set({ $class: 'java.lang.Long', $: 123 }, 123456); + map.set({ $class: 'java.lang.Long', $: 123456 }, 123); + let buf = hessian.encode(map); assert.deepEqual(buf, utils.bytes('v1/map/generic')); - buf = hessian.encode({ '$class': 'java.util.HashMap', '$': map }); + buf = hessian.encode({ $class: 'java.util.HashMap', $: map }); assert.deepEqual(buf, utils.bytes('v1/map/generic')); // decode auto transfer key to string assert.deepEqual(hessian.decode(utils.bytes('v1/map/generic'), '1.0'), { - '123': 123456, - '123456': 123 + 123: 123456, + 123456: 123, }); }); - describe('v2.0', function () { + describe('v2.0', function() { it('should write es6 Map to java.util.Map', function() { if (typeof Map !== 'function') { // pass if not support es6 Map return; } - var generic = new Buffer('4df87bd5e2403de240c87b7a', 'hex'); - var map = new Map(); - map.set({ '$class': 'java.lang.Long', '$': 123 }, 123456); - map.set({ '$class': 'java.lang.Long', '$': 123456 }, 123); - var encoder = new hessian.EncoderV2(); - var buf = encoder.write(map).get(); + const generic = Buffer.from('4df87bd5e2403de240c87b7a', 'hex'); + const map = new Map(); + map.set({ $class: 'java.lang.Long', $: 123 }, 123456); + map.set({ $class: 'java.lang.Long', $: 123456 }, 123); + const encoder = new hessian.EncoderV2(); + let buf = encoder.write(map).get(); assert.deepEqual(buf, generic); encoder.reset(); - buf = encoder.write({ '$class': 'java.util.HashMap', '$': map }).get(); + buf = encoder.write({ $class: 'java.util.HashMap', $: map }).get(); assert.deepEqual(buf, generic); // decode auto transfer key to string assert.deepEqual(hessian.decode(generic, '2.0'), { - '123': 123456, - '123456': 123 + 123: 123456, + 123456: 123, }); }); // map = new HashMap(); // map.put(new Integer(1), "fee"); // map.put(new Integer(16), "fie"); // map.put(new Integer(256), "foe"); - var hashmapBuffer = Buffer.concat([ - new Buffer([ + const hashmapBuffer = Buffer.concat([ + Buffer.from([ 'M'.charCodeAt(0), 0x91, // 1 0x03, ]), - new Buffer('fee'), // 'fee' - new Buffer([ + Buffer.from('fee'), // 'fee' + Buffer.from([ 0xa0, // 16 0x03, ]), - new Buffer('fie'), // 'fie' - new Buffer([ + Buffer.from('fie'), // 'fie' + Buffer.from([ 0xc9, 0x00, // 256 0x03, ]), - new Buffer('foe'), // 'foe' - new Buffer('z') + Buffer.from('foe'), // 'foe' + Buffer.from('z'), ]); - it('should write a java Class instance', function () { - var encoder = new hessian.EncoderV2(); - var car = { + it('should write a java Class instance', function() { + const encoder = new hessian.EncoderV2(); + const car = { $class: 'hessian.demo.Car', $: { // field defined sort must same as java Class defined @@ -240,32 +240,32 @@ describe('map.test.js', function () { model: 'Beetle', color: 'aquamarine', mileage: 65536, - } + }, }; - var buf = encoder.write(car).get(); + const buf = encoder.write(car).get(); assert.deepEqual(buf, utils.bytes('v2/map/car')); }); - it('should read java hash map', function () { + it('should read java hash map', function() { assert.deepEqual(hessian.decode(hashmapBuffer, '2.0'), { 1: 'fee', 16: 'fie', - 256: 'foe' + 256: 'foe', }); }); - it('should read a Circular java Object', function () { - var car = hessian.decode(utils.bytes('v2/map/car'), '2.0'); + it('should read a Circular java Object', function() { + const car = hessian.decode(utils.bytes('v2/map/car'), '2.0'); assert.deepEqual(car, { a: 'a', c: 'c', b: 'b', model: 'Beetle', color: 'aquamarine', - mileage: 65536 + mileage: 65536, }); - var obj = hessian.decode(utils.bytes('v2/map/car1'), '2.0'); + const obj = hessian.decode(utils.bytes('v2/map/car1'), '2.0'); [ 'color', 'model', 'mileage', 'self', 'prev' ].forEach(function(p) { assert(Object.prototype.hasOwnProperty.call(obj, p)); }); @@ -276,21 +276,21 @@ describe('map.test.js', function () { }); }); - it('should write js object to no type hash map', function () { - var encoder = new hessian.EncoderV2(); - var fooEmpty = new Buffer('4d03666f6f007a', 'hex'); - var buf = encoder.write({ foo: '' }).get(); + it('should write js object to no type hash map', function() { + let encoder = new hessian.EncoderV2(); + const fooEmpty = Buffer.from('4d03666f6f007a', 'hex'); + let buf = encoder.write({ foo: '' }).get(); assert.deepEqual(buf, fooEmpty); assert.deepEqual(hessian.decode(fooEmpty, '2.0'), { - foo: '' + foo: '', }); - var fooBar = new Buffer('4d03313233c9c803666f6f03626172047a65726f9005e4b8ade696876b657909e4b8ade69687e59388e5938876616c75657a', 'hex'); + const fooBar = Buffer.from('4d03313233c9c803666f6f03626172047a65726f9005e4b8ade696876b657909e4b8ade69687e59388e5938876616c75657a', 'hex'); encoder = new hessian.EncoderV2(); buf = encoder.write({ foo: 'bar', - '中文key': '中文哈哈value', - '123': 456, + 中文key: '中文哈哈value', + 123: 456, zero: 0, }).get(); assert.deepEqual(buf, fooBar); @@ -298,25 +298,25 @@ describe('map.test.js', function () { // read it assert.deepEqual(hessian.decode(fooBar, '2.0'), { foo: 'bar', - '中文key': '中文哈哈value', - '123': 456, + 中文key: '中文哈哈value', + 123: 456, zero: 0, }); }); - it('should read hessian 1.0 hash map', function () { + it('should read hessian 1.0 hash map', function() { assert.deepEqual(hessian.decode(utils.bytes('v1/map/foo_empty'), '2.0'), { - foo: '' + foo: '', }); assert.deepEqual(hessian.decode(utils.bytes('v1/map/foo_bar'), '2.0'), { foo: 'bar', - '中文key': '中文哈哈value', - '123': 456, + 中文key: '中文哈哈value', + 123: 456, zero: 0, }); }); - it('should write simple map to java hash map', function () { + it('should write simple map to java hash map', function() { // map = new HashMap(); // map.put(new Integer(1), "fee"); // map.put(new Integer(16), "fie"); @@ -334,32 +334,32 @@ describe('map.test.js', function () { // x03 foe # "foe" // Z - var map = { - 1: "fee", - 2: "fie", - 3: "foe" + const map = { + 1: 'fee', + 2: 'fie', + 3: 'foe', }; - var encoder = new hessian.EncoderV2(); - var buf = encoder.write(map).get(); + const encoder = new hessian.EncoderV2(); + const buf = encoder.write(map).get(); assert.deepEqual(hessian.decode(buf, '2.0'), map); // writeRef - var bufRef = encoder.write(map).get(); + const bufRef = encoder.write(map).get(); assert.deepEqual(hessian.decode(bufRef, '2.0'), map); - assert.deepEqual(bufRef.slice(buf.length), new Buffer([0x4a, 0x00])); + assert.deepEqual(bufRef.slice(buf.length), Buffer.from([ 0x4a, 0x00 ])); - var buf2 = hessian.encode({ + const buf2 = hessian.encode({ $class: 'java.util.HashMap', - $: map + $: map, }, '2.0'); assert.deepEqual(buf2, buf); assert.deepEqual(hessian.decode(buf2, '2.0'), map); }); }); - it('should decode successful when key is null', function () { - var data = new Buffer([77, 116, 0, 0, 78, 83, 0, 4, 110, 117, 108, 108, 122]); - var rv = hessian.decode(data); - assert.deepEqual(rv, {null: 'null'}); + it('should decode successful when key is null', function() { + const data = Buffer.from([ 77, 116, 0, 0, 78, 83, 0, 4, 110, 117, 108, 108, 122 ]); + const rv = hessian.decode(data); + assert.deepEqual(rv, { null: 'null' }); }); }); diff --git a/test/null.test.js b/test/null.test.js index bcb5c15..ea79094 100644 --- a/test/null.test.js +++ b/test/null.test.js @@ -1,32 +1,22 @@ -/**! - * hessian.js - test/null.test.js - * - * Copyright(c) 2014 - * MIT Licensed - * - * Authors: - * fengmk2 (http://fengmk2.github.com) - */ +'use strict'; -"use strict"; +const assert = require('assert'); +const hessian = require('../'); -var assert = require('assert'); -var hessian = require('../'); - -describe('null.test.js', function () { - it('should read null', function () { - var a = hessian.decode(new Buffer('N')); +describe('null.test.js', function() { + it('should read null', function() { + const a = hessian.decode(Buffer.from('N')); assert(a === null); }); - it('should write null', function () { - assert.deepEqual(hessian.encode(null), new Buffer('N')); + it('should write null', function() { + assert.deepEqual(hessian.encode(null), Buffer.from('N')); }); - describe('v2.0', function () { - it('should read write as 1.0', function () { - assert.deepEqual(hessian.encode(null, '2.0'), new Buffer('N')); - assert(hessian.decode(new Buffer('N'), '2.0') === null); + describe('v2.0', function() { + it('should read write as 1.0', function() { + assert.deepEqual(hessian.encode(null, '2.0'), Buffer.from('N')); + assert(hessian.decode(Buffer.from('N'), '2.0') === null); }); }); }); diff --git a/test/object.test.js b/test/object.test.js index a9e484b..8cba6b1 100644 --- a/test/object.test.js +++ b/test/object.test.js @@ -1,57 +1,50 @@ -/**! - * hessian.js - test/object.test.js - * - * Copyright(c) 2014 - * MIT Licensed - * - * Authors: - * fengmk2 (http://fengmk2.github.com) - */ - -"use strict"; - -var assert = require('assert'); -var hessian = require('../'); -var utils = require('./utils'); -var supportES6Map = require('../lib/utils').supportES6Map; - -describe('object.test.js', function () { - describe('v1.0', function () { +'use strict'; + +const assert = require('assert'); +const hessian = require('../'); +const utils = require('./utils'); + +describe('object.test.js', function() { + describe('v1.0', function() { it('should skip function', function() { - var o = { foo: 'bar', fn: function() {} }; - var buf = hessian.encode(o, '1.0'); - var output = hessian.decode(buf, '1.0'); + const o = { foo: 'bar', fn() {} }; + const buf = hessian.encode(o, '1.0'); + const output = hessian.decode(buf, '1.0'); assert.deepEqual(output, { foo: 'bar', fn: null }); }); - it('should write null for property like: { a: { "$class": "yyy.yyy", "$": null } }', function () { - var o = { '$class': 'xxx.xxx', - '$': { a: { '$class': 'yyy.yyy', '$': null } } }; - var rightBuf = new Buffer('4d7400077878782e787878530001614e7a', 'hex'); + it('should write null for property like: { a: { "$class": "yyy.yyy", "$": null } }', function() { + const o = { + $class: 'xxx.xxx', + $: { a: { $class: 'yyy.yyy', $: null } }, + }; + const rightBuf = Buffer.from('4d7400077878782e787878530001614e7a', 'hex'); - var buf = hessian.encode(o, '1.0'); + const buf = hessian.encode(o, '1.0'); assert(buf.length === rightBuf.length); assert.deepEqual(buf, rightBuf); }); - it('should write object for property like: { a: { "$class": "yyy.yyy", "$": {} } }', function () { - var o = { '$class': 'xxx.xxx', - '$': { a: { '$class': 'yyy.yyy', '$': {} } } }; - var rightBuf = new Buffer('4d7400077878782e787878530001614d7400077979792e7979797a7a', 'hex'); + it('should write object for property like: { a: { "$class": "yyy.yyy", "$": {} } }', function() { + const o = { + $class: 'xxx.xxx', + $: { a: { $class: 'yyy.yyy', $: {} } }, + }; + const rightBuf = Buffer.from('4d7400077878782e787878530001614d7400077979792e7979797a7a', 'hex'); - var buf = hessian.encode(o, '1.0'); + const buf = hessian.encode(o, '1.0'); assert(buf.length === rightBuf.length); assert.deepEqual(buf, rightBuf); }); - it('should _assertType error when encode wrong object', function () { - var req = { + it('should _assertType error when encode wrong object', function() { + const req = { $class: 'com.alipay.x.biz.User', - $: 'abc' + $: 'abc', }; - var rs; - var buf; + let rs; + let buf; try { buf = hessian.encode(req, '1.0'); } catch (err) { @@ -62,85 +55,84 @@ describe('object.test.js', function () { assert(!buf); }); - it('should decode and encode ConnectionRequest', function () { - var javabuf = utils.bytes('v1/object/ConnectionRequest'); - var connreq = hessian.decode(javabuf, '1.0', true); + it('should decode and encode ConnectionRequest', function() { + const javabuf = utils.bytes('v1/object/ConnectionRequest'); + const connreq = hessian.decode(javabuf, '1.0', true); - var jsconnreq = { + const jsconnreq = { $class: 'hessian.ConnectionRequest', $: { ctx: { $class: 'hessian.ConnectionRequest$RequestContext', $: { id: 101, - } - } - } + }, + }, + }, }; - var jsbuf = hessian.encode(connreq, '1.0'); - var jsbuf2 = hessian.encode(jsconnreq, '1.0'); + hessian.encode(connreq, '1.0'); + const jsbuf2 = hessian.encode(jsconnreq, '1.0'); // jsbuf2.should.length(javabuf.length); // jsbuf2.should.eql(javabuf); // jsbuf.should.length(javabuf.length); // jsbuf.should.eql(javabuf); - var jsbuf2Again = hessian.encode(jsconnreq, '1.0'); + const jsbuf2Again = hessian.encode(jsconnreq, '1.0'); assert.deepEqual(jsbuf2Again, jsbuf2); }); - it('should write enum Color', function () { + it('should write enum Color', function() { assert.deepEqual(hessian.encode({ $class: 'hessian.Main$Color', $: { - name: 'RED' - } + name: 'RED', + }, }, '1.0'), utils.bytes('v1/enum/red')); assert.deepEqual(hessian.encode({ $class: 'hessian.Main$Color', $: { - name: 'GREEN' - } + name: 'GREEN', + }, }, '1.0'), utils.bytes('v1/enum/green')); assert.deepEqual(hessian.encode({ $class: 'hessian.Main$Color', $: { - name: 'BLUE' - } + name: 'BLUE', + }, }, '1.0'), utils.bytes('v1/enum/blue')); }); - it('should write enum with ref', function () { + it('should write enum with ref', function() { // list = new ArrayList(); // list.add(Color.BLUE); // list.add(Color.RED); // list.add(Color.GREEN); - assert.deepEqual(hessian.encode([ - { - $class: 'hessian.Main$Color', - $: { - name: 'BLUE' - } + assert.deepEqual(hessian.encode([{ + $class: 'hessian.Main$Color', + $: { + name: 'BLUE', }, - { - $class: 'hessian.Main$Color', - $: { - name: 'RED' - } + }, + { + $class: 'hessian.Main$Color', + $: { + name: 'RED', }, - { - $class: 'hessian.Main$Color', - $: { - name: 'GREEN' - } + }, + { + $class: 'hessian.Main$Color', + $: { + name: 'GREEN', }, + }, ], '1.0'), utils.bytes('v1/enum/lists')); }); - it('should read enum Color', function () { + it('should read enum Color', function() { // enum Color { // RED, // GREEN, @@ -148,274 +140,276 @@ describe('object.test.js', function () { // } assert.deepEqual(hessian.decode(utils.bytes('v1/enum/red'), '1.0'), { - name: 'RED' + name: 'RED', }); assert.deepEqual(hessian.decode(utils.bytes('v1/enum/green'), '1.0', true), { - '$class': 'hessian.Main$Color', - '$': { - name: { '$class': 'java.lang.String', '$': 'GREEN' } - } + $class: 'hessian.Main$Color', + $: { + name: { $class: 'java.lang.String', $: 'GREEN' }, + }, }); assert.deepEqual(hessian.decode(utils.bytes('v1/enum/blue'), '1.0'), { - name: 'BLUE' + name: 'BLUE', }); assert.deepEqual(hessian.decode(utils.bytes('v1/enum/green'), '1.0'), { - name: 'GREEN' + name: 'GREEN', }); assert.deepEqual(hessian.decode(utils.bytes('v1/enum/red'), '1.0', true), { - '$class': 'hessian.Main$Color', - '$': { - name: { '$class': 'java.lang.String', '$': 'RED' } - } + $class: 'hessian.Main$Color', + $: { + name: { $class: 'java.lang.String', $: 'RED' }, + }, }); assert.deepEqual( hessian.decode(utils.bytes('v1/enum/lists'), '1.0'), - [ { name: 'BLUE' }, { name: 'RED' }, { name: 'GREEN' } ] + [{ name: 'BLUE' }, { name: 'RED' }, { name: 'GREEN' }] ); assert.deepEqual(hessian.decode(utils.bytes('v1/enum/lists'), '1.0', true), { $class: 'java.util.ArrayList', $: [ - { '$class': 'hessian.Main$Color', '$': { name: { '$class': 'java.lang.String', '$': 'BLUE' } } }, - { '$class': 'hessian.Main$Color', '$': { name: { '$class': 'java.lang.String', '$': 'RED' } } }, - { '$class': 'hessian.Main$Color', '$': { name: { '$class': 'java.lang.String', '$': 'GREEN' } } } - ] + { $class: 'hessian.Main$Color', $: { name: { $class: 'java.lang.String', $: 'BLUE' } } }, + { $class: 'hessian.Main$Color', $: { name: { $class: 'java.lang.String', $: 'RED' } } }, + { $class: 'hessian.Main$Color', $: { name: { $class: 'java.lang.String', $: 'GREEN' } } }, + ], }); }); - it('should write "{$class: "hessian.test.demo.Car", $: {a: 1}}"', function () { - var obj = { + it('should write "{$class: "hessian.test.demo.Car", $: {a: 1}}"', function() { + const obj = { $class: 'hessian.test.demo.Car', - $: {a: 1, b: 'map'} + $: { a: 1, b: 'map' }, }; - var buf = hessian.encode(obj, '1.0'); + const buf = hessian.encode(obj, '1.0'); assert(buf[0] === 0x4d); // 'M' assert.deepEqual(hessian.decode(buf, '1.0'), obj.$); assert.deepEqual(hessian.decode(buf, '1.0', true), { - '$class': 'hessian.test.demo.Car', - '$': { - a: { '$class': 'int', '$': 1 }, - b: { '$class': 'java.lang.String', '$': 'map' } - } + $class: 'hessian.test.demo.Car', + $: { + a: { $class: 'int', $: 1 }, + b: { $class: 'java.lang.String', $: 'map' }, + }, }); }); - it('should read and write one car list', function () { - assert.deepEqual(hessian.decode(utils.bytes('v1/map/one_car_list'), '1.0'), [ - { a: 'a', - c: 'c', - b: 'b', - model: 'model 1', - color: 'aquamarine', - mileage: 65536 } - ]); - - var cars = hessian.decode(utils.bytes('v1/map/one_car_list'), '1.0', true); + it('should read and write one car list', function() { + assert.deepEqual(hessian.decode(utils.bytes('v1/map/one_car_list'), '1.0'), [{ + a: 'a', + c: 'c', + b: 'b', + model: 'model 1', + color: 'aquamarine', + mileage: 65536, + }]); + + const cars = hessian.decode(utils.bytes('v1/map/one_car_list'), '1.0', true); assert.deepEqual(cars, { $class: 'java.util.ArrayList', - $: [ - { - '$class': 'hessian.demo.Car', - '$': { - a: { '$class': 'java.lang.String', '$': 'a' }, - c: { '$class': 'java.lang.String', '$': 'c' }, - b: { '$class': 'java.lang.String', '$': 'b' }, - model: { '$class': 'java.lang.String', '$': 'model 1' }, - color: { '$class': 'java.lang.String', '$': 'aquamarine' }, - mileage: { '$class': 'int', '$': 65536 } - } - } - ] + $: [{ + $class: 'hessian.demo.Car', + $: { + a: { $class: 'java.lang.String', $: 'a' }, + c: { $class: 'java.lang.String', $: 'c' }, + b: { $class: 'java.lang.String', $: 'b' }, + model: { $class: 'java.lang.String', $: 'model 1' }, + color: { $class: 'java.lang.String', $: 'aquamarine' }, + mileage: { $class: 'int', $: 65536 }, + }, + }], }); assert.deepEqual(hessian.encode(cars, '1.0'), utils.bytes('v1/map/one_car_list')); }); - it('should read and write two car list', function () { - assert.deepEqual(hessian.decode(utils.bytes('v1/map/two_car_list'), '1.0'), [ - { a: 'a', - c: 'c', - b: 'b', - model: 'model 1', - color: 'aquamarine', - mileage: 65536 }, - { a: 'a', - c: 'c', - b: 'b', - model: 'model 2', - color: 'aquamarine', - mileage: 65536 } + it('should read and write two car list', function() { + assert.deepEqual(hessian.decode(utils.bytes('v1/map/two_car_list'), '1.0'), [{ + a: 'a', + c: 'c', + b: 'b', + model: 'model 1', + color: 'aquamarine', + mileage: 65536, + }, + { + a: 'a', + c: 'c', + b: 'b', + model: 'model 2', + color: 'aquamarine', + mileage: 65536, + }, ]); - var cars = hessian.decode(utils.bytes('v1/map/two_car_list'), '1.0', true); + const cars = hessian.decode(utils.bytes('v1/map/two_car_list'), '1.0', true); assert.deepEqual(cars, { $class: 'java.util.ArrayList', - $: [ - { - '$class': 'hessian.demo.Car', - '$': { - a: { '$class': 'java.lang.String', '$': 'a' }, - c: { '$class': 'java.lang.String', '$': 'c' }, - b: { '$class': 'java.lang.String', '$': 'b' }, - model: { '$class': 'java.lang.String', '$': 'model 1' }, - color: { '$class': 'java.lang.String', '$': 'aquamarine' }, - mileage: { '$class': 'int', '$': 65536 } - } - }, { - '$class': 'hessian.demo.Car', - '$': { - a: { '$class': 'java.lang.String', '$': 'a' }, - c: { '$class': 'java.lang.String', '$': 'c' }, - b: { '$class': 'java.lang.String', '$': 'b' }, - model: { '$class': 'java.lang.String', '$': 'model 2' }, - color: { '$class': 'java.lang.String', '$': 'aquamarine' }, - mileage: { '$class': 'int', '$': 65536 } - } - } - ] + $: [{ + $class: 'hessian.demo.Car', + $: { + a: { $class: 'java.lang.String', $: 'a' }, + c: { $class: 'java.lang.String', $: 'c' }, + b: { $class: 'java.lang.String', $: 'b' }, + model: { $class: 'java.lang.String', $: 'model 1' }, + color: { $class: 'java.lang.String', $: 'aquamarine' }, + mileage: { $class: 'int', $: 65536 }, + }, + }, { + $class: 'hessian.demo.Car', + $: { + a: { $class: 'java.lang.String', $: 'a' }, + c: { $class: 'java.lang.String', $: 'c' }, + b: { $class: 'java.lang.String', $: 'b' }, + model: { $class: 'java.lang.String', $: 'model 2' }, + color: { $class: 'java.lang.String', $: 'aquamarine' }, + mileage: { $class: 'int', $: 65536 }, + }, + }], }); - var buf = hessian.encode(cars, '1.0'); + const buf = hessian.encode(cars, '1.0'); assert(buf.length === utils.bytes('v1/map/two_car_list').length); assert.deepEqual(buf, utils.bytes('v1/map/two_car_list')); }); - it('should read and write many cars', function () { + it('should read and write many cars', function() { // list = new ArrayList(); // list.add(new Car("model 1")); // list.add(new Car("model 2")); // list.add(new Car("model 3")); - assert.deepEqual(hessian.decode(utils.bytes('v1/map/car_list'), '1.0'), [ - { a: 'a', - c: 'c', - b: 'b', - model: 'model 1', - color: 'aquamarine', - mileage: 65536 }, - { a: 'a', - c: 'c', - b: 'b', - model: 'model 2', - color: 'aquamarine', - mileage: 65536 }, - { a: 'a', - c: 'c', - b: 'b', - model: 'model 3', - color: 'aquamarine', - mileage: 65536 } + assert.deepEqual(hessian.decode(utils.bytes('v1/map/car_list'), '1.0'), [{ + a: 'a', + c: 'c', + b: 'b', + model: 'model 1', + color: 'aquamarine', + mileage: 65536, + }, + { + a: 'a', + c: 'c', + b: 'b', + model: 'model 2', + color: 'aquamarine', + mileage: 65536, + }, + { + a: 'a', + c: 'c', + b: 'b', + model: 'model 3', + color: 'aquamarine', + mileage: 65536, + }, ]); - var cars = hessian.decode(utils.bytes('v1/map/car_list'), '1.0', true); + const cars = hessian.decode(utils.bytes('v1/map/car_list'), '1.0', true); assert.deepEqual(cars, { $class: 'java.util.ArrayList', - $: [ - { - '$class': 'hessian.demo.Car', - '$': { - a: { '$class': 'java.lang.String', '$': 'a' }, - c: { '$class': 'java.lang.String', '$': 'c' }, - b: { '$class': 'java.lang.String', '$': 'b' }, - model: { '$class': 'java.lang.String', '$': 'model 1' }, - color: { '$class': 'java.lang.String', '$': 'aquamarine' }, - mileage: { '$class': 'int', '$': 65536 } - } - }, { - '$class': 'hessian.demo.Car', - '$': { - a: { '$class': 'java.lang.String', '$': 'a' }, - c: { '$class': 'java.lang.String', '$': 'c' }, - b: { '$class': 'java.lang.String', '$': 'b' }, - model: { '$class': 'java.lang.String', '$': 'model 2' }, - color: { '$class': 'java.lang.String', '$': 'aquamarine' }, - mileage: { '$class': 'int', '$': 65536 } - } - }, { - '$class': 'hessian.demo.Car', - '$': { - a: { '$class': 'java.lang.String', '$': 'a' }, - c: { '$class': 'java.lang.String', '$': 'c' }, - b: { '$class': 'java.lang.String', '$': 'b' }, - model: { '$class': 'java.lang.String', '$': 'model 3' }, - color: { '$class': 'java.lang.String', '$': 'aquamarine' }, - mileage: { '$class': 'int', '$': 65536 } - } - } - ] + $: [{ + $class: 'hessian.demo.Car', + $: { + a: { $class: 'java.lang.String', $: 'a' }, + c: { $class: 'java.lang.String', $: 'c' }, + b: { $class: 'java.lang.String', $: 'b' }, + model: { $class: 'java.lang.String', $: 'model 1' }, + color: { $class: 'java.lang.String', $: 'aquamarine' }, + mileage: { $class: 'int', $: 65536 }, + }, + }, { + $class: 'hessian.demo.Car', + $: { + a: { $class: 'java.lang.String', $: 'a' }, + c: { $class: 'java.lang.String', $: 'c' }, + b: { $class: 'java.lang.String', $: 'b' }, + model: { $class: 'java.lang.String', $: 'model 2' }, + color: { $class: 'java.lang.String', $: 'aquamarine' }, + mileage: { $class: 'int', $: 65536 }, + }, + }, { + $class: 'hessian.demo.Car', + $: { + a: { $class: 'java.lang.String', $: 'a' }, + c: { $class: 'java.lang.String', $: 'c' }, + b: { $class: 'java.lang.String', $: 'b' }, + model: { $class: 'java.lang.String', $: 'model 3' }, + color: { $class: 'java.lang.String', $: 'aquamarine' }, + mileage: { $class: 'int', $: 65536 }, + }, + }], }); assert.deepEqual(hessian.encode(cars, '1.0'), utils.bytes('v1/map/car_list')); }); - describe('java.util.concurrent.atomic.AtomicLong', function () { - it('should read and write', function () { - var javabuf = utils.bytes('v1/object/AtomicLong0'); - var a0 = hessian.decode(javabuf); - assert.deepEqual(a0, {value: 0}); - var a0 = hessian.decode(javabuf, true); + describe('java.util.concurrent.atomic.AtomicLong', function() { + it('should read and write', function() { + let javabuf = utils.bytes('v1/object/AtomicLong0'); + let a0 = hessian.decode(javabuf); + assert.deepEqual(a0, { value: 0 }); + a0 = hessian.decode(javabuf, true); assert.deepEqual(a0, { $class: 'java.util.concurrent.atomic.AtomicLong', $: { value: { $: 0, - $class: 'long' - } - } + $class: 'long', + }, + }, }); assert.deepEqual(hessian.encode(a0), javabuf); javabuf = utils.bytes('v1/object/AtomicLong1'); - var a1 = hessian.decode(javabuf); - assert.deepEqual(a1, {value: 1}); - var a1 = hessian.decode(javabuf, true); + let a1 = hessian.decode(javabuf); + assert.deepEqual(a1, { value: 1 }); + a1 = hessian.decode(javabuf, true); assert.deepEqual(a1, { $class: 'java.util.concurrent.atomic.AtomicLong', $: { value: { $: 1, - $class: 'long' - } - } + $class: 'long', + }, + }, }); assert.deepEqual(hessian.encode(a1), javabuf); }); }); }); - describe('v2.0', function () { + describe('v2.0', function() { it('should skip function', function() { - var o = { foo: 'bar', fn: function() {} }; - var buf = hessian.encode(o, '2.0'); - var output = hessian.decode(buf, '2.0'); + const o = { foo: 'bar', fn() {} }; + const buf = hessian.encode(o, '2.0'); + const output = hessian.decode(buf, '2.0'); assert.deepEqual(output, { foo: 'bar', fn: null }); }); - it('should decode and encode ConnectionRequest', function () { - var javabuf = utils.bytes('v2/object/ConnectionRequest'); - var connreq1 = hessian.decode(javabuf, '2.0'); + it('should decode and encode ConnectionRequest', function() { + const javabuf = utils.bytes('v2/object/ConnectionRequest'); + const connreq1 = hessian.decode(javabuf, '2.0'); assert(connreq1.ctx); assert(connreq1.ctx.id === 101); - var connreq = hessian.decode(javabuf, '2.0', true); - var jsconnreq = { + const connreq = hessian.decode(javabuf, '2.0', true); + const jsconnreq = { $class: 'hessian.ConnectionRequest', $: { ctx: { $class: 'hessian.ConnectionRequest$RequestContext', $: { id: 101, - } - } - } + }, + }, + }, }; - var jsbuf = hessian.encode(connreq, '2.0'); - var jsbuf2 = hessian.encode(jsconnreq, '2.0'); + hessian.encode(connreq, '2.0'); + hessian.encode(jsconnreq, '2.0'); // jsbuf2.should.length(javabuf.length); // jsbuf2.should.eql(javabuf); @@ -423,84 +417,83 @@ describe('object.test.js', function () { // jsbuf.should.eql(javabuf); }); - it('should decode hessian 1.0 ConnectionRequest', function () { - var javabuf = utils.bytes('v1/object/ConnectionRequest'); - var connreq = hessian.decode(javabuf, '1.0', true); + it('should decode hessian 1.0 ConnectionRequest', function() { + const javabuf = utils.bytes('v1/object/ConnectionRequest'); + const connreq = hessian.decode(javabuf, '1.0', true); assert(connreq.$class === 'hessian.ConnectionRequest'); assert(connreq.$.ctx.$class === 'hessian.ConnectionRequest$RequestContext'); }); - it('should write enum Color', function () { + it('should write enum Color', function() { assert.deepEqual(hessian.encode({ $class: 'hessian.Main$Color', $: { - name: 'RED' - } + name: 'RED', + }, }, '2.0'), utils.bytes('v2/enum/red')); assert.deepEqual(hessian.encode({ $class: 'hessian.Main$Color', $: { - name: 'GREEN' - } + name: 'GREEN', + }, }, '2.0'), utils.bytes('v2/enum/green')); assert.deepEqual(hessian.encode({ $class: 'hessian.Main$Color', $: { - name: 'BLUE' - } + name: 'BLUE', + }, }, '2.0'), utils.bytes('v2/enum/blue')); }); - it('should read hessian 1.0 enum Color', function () { + it('should read hessian 1.0 enum Color', function() { assert.deepEqual(hessian.decode(utils.bytes('v1/enum/red'), '2.0', true), { $class: 'hessian.Main$Color', $: { - name: 'RED' - } + name: 'RED', + }, }); assert.deepEqual(hessian.decode(utils.bytes('v1/enum/green'), '2.0', false), { - name: 'GREEN' + name: 'GREEN', }); assert.deepEqual(hessian.decode(utils.bytes('v1/enum/blue'), '2.0', true), { $class: 'hessian.Main$Color', $: { - name: 'BLUE' - } + name: 'BLUE', + }, }); }); - it('should write enum with ref', function () { + it('should write enum with ref', function() { // list = new ArrayList(); // list.add(Color.BLUE); // list.add(Color.RED); // list.add(Color.GREEN); - assert.deepEqual(hessian.encode([ - { - $class: 'hessian.Main$Color', - $: { - name: 'BLUE' - } + assert.deepEqual(hessian.encode([{ + $class: 'hessian.Main$Color', + $: { + name: 'BLUE', }, - { - $class: 'hessian.Main$Color', - $: { - name: 'RED' - } + }, + { + $class: 'hessian.Main$Color', + $: { + name: 'RED', }, - { - $class: 'hessian.Main$Color', - $: { - name: 'GREEN' - } + }, + { + $class: 'hessian.Main$Color', + $: { + name: 'GREEN', }, + }, ], '2.0'), utils.bytes('v2/enum/lists')); }); - it('should read enum Color', function () { + it('should read enum Color', function() { // enum Color { // RED, // GREEN, @@ -510,310 +503,342 @@ describe('object.test.js', function () { // enum format: // O type 1 "name" o ref name-value assert.deepEqual(hessian.decode(utils.bytes('v2/enum/red'), '2.0'), { - name: 'RED' + name: 'RED', }); assert.deepEqual(hessian.decode(utils.bytes('v2/enum/green'), '2.0', true), { $class: 'hessian.Main$Color', $: { - name: 'GREEN' - } + name: 'GREEN', + }, }); assert.deepEqual(hessian.decode(utils.bytes('v2/enum/blue'), '2.0'), { - name: 'BLUE' + name: 'BLUE', }); assert.deepEqual(hessian.decode(utils.bytes('v2/enum/green'), '2.0'), { - name: 'GREEN' + name: 'GREEN', }); assert.deepEqual(hessian.decode(utils.bytes('v2/enum/red'), '2.0', true), { $class: 'hessian.Main$Color', $: { - name: 'RED' - } + name: 'RED', + }, }); assert.deepEqual( hessian.decode(utils.bytes('v2/enum/lists'), '2.0'), - [ { name: 'BLUE' }, { name: 'RED' }, { name: 'GREEN' } ] + [{ name: 'BLUE' }, { name: 'RED' }, { name: 'GREEN' }] ); assert.deepEqual(hessian.decode(utils.bytes('v2/enum/lists'), '2.0', true), [ - { '$class': 'hessian.Main$Color', '$': { name: 'BLUE' } }, - { '$class': 'hessian.Main$Color', '$': { name: 'RED' } }, - { '$class': 'hessian.Main$Color', '$': { name: 'GREEN' } } + { $class: 'hessian.Main$Color', $: { name: 'BLUE' } }, + { $class: 'hessian.Main$Color', $: { name: 'RED' } }, + { $class: 'hessian.Main$Color', $: { name: 'GREEN' } }, ]); }); - it('should write "{$class: "hessian.test.demo.Car", $: {a: 1}}"', function () { - var obj = { + it('should write "{$class: "hessian.test.demo.Car", $: {a: 1}}"', function() { + const obj = { $class: 'hessian.test.demo.Car', - $: {a: 1, b: 'map'} + $: { a: 1, b: 'map' }, }; - var buf = hessian.encode(obj, '2.0'); + const buf = hessian.encode(obj, '2.0'); assert(buf[0] === 0x4f); assert.deepEqual(hessian.decode(buf, '2.0'), obj.$); assert.deepEqual(hessian.decode(buf, '2.0', true), obj); }); - it('should read one car list', function () { - assert.deepEqual(hessian.decode(utils.bytes('v2/map/one_car_list'), '2.0'), [ - { a: 'a', + it('should read one car list', function() { + assert.deepEqual(hessian.decode(utils.bytes('v2/map/one_car_list'), '2.0'), [{ + a: 'a', + c: 'c', + b: 'b', + model: 'model 1', + color: 'aquamarine', + mileage: 65536, + }]); + + const cars = hessian.decode(utils.bytes('v2/map/one_car_list'), '2.0', true); + assert.deepEqual(cars, [{ + $class: 'hessian.demo.Car', + $: { + a: 'a', c: 'c', b: 'b', model: 'model 1', color: 'aquamarine', - mileage: 65536 } - ]); - - var cars = hessian.decode(utils.bytes('v2/map/one_car_list'), '2.0', true); - assert.deepEqual(cars, [ - { - $class: 'hessian.demo.Car', - $: { - a: 'a', - c: 'c', - b: 'b', - model: 'model 1', - color: 'aquamarine', - mileage: 65536 } - } - ]); + mileage: 65536, + }, + }]); assert.deepEqual(hessian.encode(cars, '2.0'), utils.bytes('v2/map/one_car_list')); }); - it('should read two car list', function () { - assert.deepEqual(hessian.decode(utils.bytes('v2/map/two_car_list'), '2.0'), [ - { a: 'a', + it('should read two car list', function() { + assert.deepEqual(hessian.decode(utils.bytes('v2/map/two_car_list'), '2.0'), [{ + a: 'a', + c: 'c', + b: 'b', + model: 'model 1', + color: 'aquamarine', + mileage: 65536, + }, + { + a: 'a', + c: 'c', + b: 'b', + model: 'model 2', + color: 'aquamarine', + mileage: 65536, + }, + ]); + + const cars = hessian.decode(utils.bytes('v2/map/two_car_list'), '2.0', true); + assert.deepEqual(cars, [{ + $class: 'hessian.demo.Car', + $: { + a: 'a', c: 'c', b: 'b', model: 'model 1', color: 'aquamarine', - mileage: 65536 }, - { a: 'a', + mileage: 65536, + }, + }, + { + $class: 'hessian.demo.Car', + $: { + a: 'a', c: 'c', b: 'b', model: 'model 2', color: 'aquamarine', - mileage: 65536 } - ]); - - var cars = hessian.decode(utils.bytes('v2/map/two_car_list'), '2.0', true); - assert.deepEqual(cars, [ - { - $class: 'hessian.demo.Car', - $: { - a: 'a', - c: 'c', - b: 'b', - model: 'model 1', - color: 'aquamarine', - mileage: 65536 } - }, - { - $class: 'hessian.demo.Car', - $: { - a: 'a', - c: 'c', - b: 'b', - model: 'model 2', - color: 'aquamarine', - mileage: 65536 } - } + mileage: 65536, + }, + }, ]); - var buf = hessian.encode(cars, '2.0'); + const buf = hessian.encode(cars, '2.0'); assert(buf.length === utils.bytes('v2/map/two_car_list').length); assert.deepEqual(buf, utils.bytes('v2/map/two_car_list')); }); - it('should read many cars', function () { + it('should read many cars', function() { // list = new ArrayList(); // list.add(new Car("model 1")); // list.add(new Car("model 2")); // list.add(new Car("model 3")); - assert.deepEqual(hessian.decode(utils.bytes('v2/map/car_list'), '2.0'), [ - { a: 'a', + assert.deepEqual(hessian.decode(utils.bytes('v2/map/car_list'), '2.0'), [{ + a: 'a', + c: 'c', + b: 'b', + model: 'model 1', + color: 'aquamarine', + mileage: 65536, + }, + { + a: 'a', + c: 'c', + b: 'b', + model: 'model 2', + color: 'aquamarine', + mileage: 65536, + }, + { + a: 'a', + c: 'c', + b: 'b', + model: 'model 3', + color: 'aquamarine', + mileage: 65536, + }, + ]); + + const cars = hessian.decode(utils.bytes('v2/map/car_list'), '2.0', true); + assert.deepEqual(cars, [{ + $class: 'hessian.demo.Car', + $: { + a: 'a', c: 'c', b: 'b', model: 'model 1', color: 'aquamarine', - mileage: 65536 }, - { a: 'a', + mileage: 65536, + }, + }, + { + $class: 'hessian.demo.Car', + $: { + a: 'a', c: 'c', b: 'b', model: 'model 2', color: 'aquamarine', - mileage: 65536 }, - { a: 'a', + mileage: 65536, + }, + }, + { + $class: 'hessian.demo.Car', + $: { + a: 'a', c: 'c', b: 'b', model: 'model 3', color: 'aquamarine', - mileage: 65536 } - ]); - - var cars = hessian.decode(utils.bytes('v2/map/car_list'), '2.0', true); - assert.deepEqual(cars, [ - { '$class': 'hessian.demo.Car', - '$': - { a: 'a', - c: 'c', - b: 'b', - model: 'model 1', - color: 'aquamarine', - mileage: 65536 } }, - { '$class': 'hessian.demo.Car', - '$': - { a: 'a', - c: 'c', - b: 'b', - model: 'model 2', - color: 'aquamarine', - mileage: 65536 } }, - { '$class': 'hessian.demo.Car', - '$': - { a: 'a', - c: 'c', - b: 'b', - model: 'model 3', - color: 'aquamarine', - mileage: 65536 } } + mileage: 65536, + }, + }, ]); assert.deepEqual(hessian.encode(cars, '2.0'), utils.bytes('v2/map/car_list')); }); - it('should read hessian 1.0 one car list', function () { - assert.deepEqual(hessian.decode(utils.bytes('v1/map/one_car_list'), '2.0'), [ - { a: 'a', + it('should read hessian 1.0 one car list', function() { + assert.deepEqual(hessian.decode(utils.bytes('v1/map/one_car_list'), '2.0'), [{ + a: 'a', + c: 'c', + b: 'b', + model: 'model 1', + color: 'aquamarine', + mileage: 65536, + }]); + + const cars = hessian.decode(utils.bytes('v1/map/one_car_list'), '2.0', true); + assert.deepEqual(cars, [{ + $class: 'hessian.demo.Car', + $: { + a: 'a', c: 'c', b: 'b', model: 'model 1', color: 'aquamarine', - mileage: 65536 } - ]); + mileage: 65536, + }, + }]); + }); - var cars = hessian.decode(utils.bytes('v1/map/one_car_list'), '2.0', true); - assert.deepEqual(cars, [ - { - $class: 'hessian.demo.Car', - $: { - a: 'a', - c: 'c', - b: 'b', - model: 'model 1', - color: 'aquamarine', - mileage: 65536 } - } + it('should read hessian 1.0 two car list', function() { + assert.deepEqual(hessian.decode(utils.bytes('v1/map/two_car_list'), '2.0'), [{ + a: 'a', + c: 'c', + b: 'b', + model: 'model 1', + color: 'aquamarine', + mileage: 65536, + }, + { + a: 'a', + c: 'c', + b: 'b', + model: 'model 2', + color: 'aquamarine', + mileage: 65536, + }, ]); - }); - it('should read hessian 1.0 two car list', function () { - assert.deepEqual(hessian.decode(utils.bytes('v1/map/two_car_list'), '2.0'), [ - { a: 'a', + const cars = hessian.decode(utils.bytes('v1/map/two_car_list'), '2.0', true); + assert.deepEqual(cars, [{ + $class: 'hessian.demo.Car', + $: { + a: 'a', c: 'c', b: 'b', model: 'model 1', color: 'aquamarine', - mileage: 65536 }, - { a: 'a', + mileage: 65536, + }, + }, + { + $class: 'hessian.demo.Car', + $: { + a: 'a', c: 'c', b: 'b', model: 'model 2', color: 'aquamarine', - mileage: 65536 } - ]); - - var cars = hessian.decode(utils.bytes('v1/map/two_car_list'), '2.0', true); - assert.deepEqual(cars, [ - { - $class: 'hessian.demo.Car', - $: { - a: 'a', - c: 'c', - b: 'b', - model: 'model 1', - color: 'aquamarine', - mileage: 65536 } - }, - { - $class: 'hessian.demo.Car', - $: { - a: 'a', - c: 'c', - b: 'b', - model: 'model 2', - color: 'aquamarine', - mileage: 65536 } - } + mileage: 65536, + }, + }, ]); }); - it('should read hessian 1.0 many cars', function () { + it('should read hessian 1.0 many cars', function() { // list = new ArrayList(); // list.add(new Car("model 1")); // list.add(new Car("model 2")); // list.add(new Car("model 3")); - assert.deepEqual(hessian.decode(utils.bytes('v1/map/car_list'), '2.0'), [ - { a: 'a', + assert.deepEqual(hessian.decode(utils.bytes('v1/map/car_list'), '2.0'), [{ + a: 'a', + c: 'c', + b: 'b', + model: 'model 1', + color: 'aquamarine', + mileage: 65536, + }, + { + a: 'a', + c: 'c', + b: 'b', + model: 'model 2', + color: 'aquamarine', + mileage: 65536, + }, + { + a: 'a', + c: 'c', + b: 'b', + model: 'model 3', + color: 'aquamarine', + mileage: 65536, + }, + ]); + + const cars = hessian.decode(utils.bytes('v1/map/car_list'), '2.0', true); + assert.deepEqual(cars, [{ + $class: 'hessian.demo.Car', + $: { + a: 'a', c: 'c', b: 'b', model: 'model 1', color: 'aquamarine', - mileage: 65536 }, - { a: 'a', + mileage: 65536, + }, + }, + { + $class: 'hessian.demo.Car', + $: { + a: 'a', c: 'c', b: 'b', model: 'model 2', color: 'aquamarine', - mileage: 65536 }, - { a: 'a', + mileage: 65536, + }, + }, + { + $class: 'hessian.demo.Car', + $: { + a: 'a', c: 'c', b: 'b', model: 'model 3', color: 'aquamarine', - mileage: 65536 } - ]); - - var cars = hessian.decode(utils.bytes('v1/map/car_list'), '2.0', true); - assert.deepEqual(cars, [ - { '$class': 'hessian.demo.Car', - '$': - { a: 'a', - c: 'c', - b: 'b', - model: 'model 1', - color: 'aquamarine', - mileage: 65536 } }, - { '$class': 'hessian.demo.Car', - '$': - { a: 'a', - c: 'c', - b: 'b', - model: 'model 2', - color: 'aquamarine', - mileage: 65536 } }, - { '$class': 'hessian.demo.Car', - '$': - { a: 'a', - c: 'c', - b: 'b', - model: 'model 3', - color: 'aquamarine', - mileage: 65536 } } + mileage: 65536, + }, + }, ]); }); - describe('java.util.concurrent.atomic.AtomicLong', function () { - it('should read and write', function () { - var javabuf = utils.bytes('v2/object/AtomicLong0'); - var a0 = hessian.decode(javabuf, '2.0'); - assert.deepEqual(a0, {value: 0}); - var a0 = hessian.decode(javabuf, '2.0', true); + describe('java.util.concurrent.atomic.AtomicLong', function() { + it('should read and write', function() { + let javabuf = utils.bytes('v2/object/AtomicLong0'); + let a0 = hessian.decode(javabuf, '2.0'); + assert.deepEqual(a0, { value: 0 }); + a0 = hessian.decode(javabuf, '2.0', true); assert.deepEqual(a0, { $class: 'java.util.concurrent.atomic.AtomicLong', $: { @@ -821,22 +846,22 @@ describe('object.test.js', function () { $class: 'long', $: 0, }, - } + }, }); assert.deepEqual(hessian.encode({ $class: 'java.util.concurrent.atomic.AtomicLong', $: { value: { $class: 'long', - $: 0 - } - } + $: 0, + }, + }, }, '2.0'), javabuf); javabuf = utils.bytes('v2/object/AtomicLong1'); - var a1 = hessian.decode(javabuf, '2.0'); - assert.deepEqual(a1, {value: 1}); - var a1 = hessian.decode(javabuf, '2.0', true); + let a1 = hessian.decode(javabuf, '2.0'); + assert.deepEqual(a1, { value: 1 }); + a1 = hessian.decode(javabuf, '2.0', true); assert.deepEqual(a1, { $class: 'java.util.concurrent.atomic.AtomicLong', $: { @@ -844,56 +869,50 @@ describe('object.test.js', function () { $class: 'long', $: 1, }, - } + }, }); assert.deepEqual(hessian.encode({ $class: 'java.util.concurrent.atomic.AtomicLong', $: { value: { $class: 'long', - $: 1 - } - } + $: 1, + }, + }, }, '2.0'), javabuf); }); }); }); + describe('map key object', function() { - if (supportES6Map) { - describe('map key object', function() { - - it('enum should use name v1', function() { - var key = { - $class: 'com.hessian.enums.TestEnum', - $: { - name: 'KEY', - }, - }; - var obj = new Map(); - obj.set(key, 'hello'); - var buf = hessian.encode(obj); - var rs = hessian.decode(buf); - rs.should.eql({ 'KEY': 'hello' }); - }); - - it('enum should use name v2', function() { - var key = { - $class: 'com.hessian.enums.TestEnum', - $: { - name: 'KEY', - }, - }; - var obj = new Map(); - obj.set(key, 'hello'); - var buf = hessian.encode(obj, '2.0'); - var rs = hessian.decode(buf, '2.0'); - rs.should.eql({ 'KEY': 'hello' }); - }); - + it('enum should use name v1', function() { + const key = { + $class: 'com.hessian.enums.TestEnum', + $: { + name: 'KEY', + }, + }; + const obj = new Map(); + obj.set(key, 'hello'); + const buf = hessian.encode(obj); + const rs = hessian.decode(buf); + assert.deepEqual(rs, { KEY: 'hello' }); }); - } - + it('enum should use name v2', function() { + const key = { + $class: 'com.hessian.enums.TestEnum', + $: { + name: 'KEY', + }, + }; + const obj = new Map(); + obj.set(key, 'hello'); + const buf = hessian.encode(obj, '2.0'); + const rs = hessian.decode(buf, '2.0'); + assert.deepEqual(rs, { KEY: 'hello' }); + }); + }); }); diff --git a/test/string.test.js b/test/string.test.js index 28590d0..49aa285 100644 --- a/test/string.test.js +++ b/test/string.test.js @@ -1,104 +1,96 @@ -/*! - * hessian.js - test/string.test.js - * - * Copyright(c) 2014 - * MIT Licensed - * - * Authors: - * fengmk2 (http://fengmk2.github.com) - */ - -"use strict"; - -var assert = require('assert'); -var hessian = require('../'); -var utils = require('./utils'); - -describe('string.test.js', function () { +'use strict'; + +const assert = require('assert'); +const hessian = require('../'); +const utils = require('./utils'); + +describe('string.test.js', function() { this.timeout(0); - - var helloBuffer = Buffer.concat([new Buffer(['S'.charCodeAt(0), 0x00, 0x05]), - new Buffer('hello')]); - it('should read string', function () { + const helloBuffer = Buffer.concat([ Buffer.from([ 'S'.charCodeAt(0), 0x00, 0x05 ]), + Buffer.from('hello'), + ]); + + it('should read string', function() { assert(hessian.decode(helloBuffer) === 'hello'); - assert(hessian.decode(Buffer.concat([new Buffer(['s'.charCodeAt(0), 0x00, 0x07]), - new Buffer('hello, '), new Buffer(['S'.charCodeAt(0), 0x00, 0x05]), - new Buffer('world')])) === 'hello, world'); + assert(hessian.decode(Buffer.concat([ Buffer.from([ 's'.charCodeAt(0), 0x00, 0x07 ]), + Buffer.from('hello, '), Buffer.from([ 'S'.charCodeAt(0), 0x00, 0x05 ]), + Buffer.from('world'), + ])) === 'hello, world'); }); - it('should write string', function () { - var s = hessian.encode('hello'); + it('should write string', function() { + const s = hessian.encode('hello'); assert(Buffer.isBuffer(s)); assert(s.length === 8); assert.deepEqual(s, helloBuffer); }); - it('should read write empty string', function () { - assert(hessian.decode(new Buffer(['S'.charCodeAt(0), 0, 0])) === ''); - assert.deepEqual(hessian.encode(''), new Buffer(['S'.charCodeAt(0), 0, 0])); + it('should read write empty string', function() { + assert(hessian.decode(Buffer.from([ 'S'.charCodeAt(0), 0, 0 ])) === ''); + assert.deepEqual(hessian.encode(''), Buffer.from([ 'S'.charCodeAt(0), 0, 0 ])); }); - it('should read and write utf8 string as java', function () { - var str = ''; - for (var i = 0; i < 32767; i++) { + it('should read and write utf8 string as java', function() { + let str = ''; + for (let i = 0; i < 32767; i++) { str += '锋'; } assert.deepEqual(hessian.encode(str, '1.0'), utils.bytes('v1/string/utf8_32767')); assert(hessian.decode(utils.bytes('v1/string/utf8_32767')) === str); - var str = ''; - for (var i = 0; i < 32768; i++) { + str = ''; + for (let i = 0; i < 32768; i++) { str += '锋'; } assert.deepEqual(hessian.encode(str, '1.0'), utils.bytes('v1/string/utf8_32768')); assert(hessian.decode(utils.bytes('v1/string/utf8_32768')) === str); - var str = ''; - for (var i = 0; i < 32769; i++) { + str = ''; + for (let i = 0; i < 32769; i++) { str += '锋'; } assert.deepEqual(hessian.encode(str, '1.0'), utils.bytes('v1/string/utf8_32769')); assert(hessian.decode(utils.bytes('v1/string/utf8_32769')) === str); - var str = ''; - for (var i = 0; i < 65534; i++) { + str = ''; + for (let i = 0; i < 65534; i++) { str += '锋'; } assert.deepEqual(hessian.encode(str, '1.0'), utils.bytes('v1/string/utf8_65534')); assert(hessian.decode(utils.bytes('v1/string/utf8_65534')) === str); - var str = ''; - for (var i = 0; i < 65535; i++) { + str = ''; + for (let i = 0; i < 65535; i++) { str += '锋'; } assert.deepEqual(hessian.encode(str, '1.0'), utils.bytes('v1/string/utf8_65535')); assert(hessian.decode(utils.bytes('v1/string/utf8_65535')) === str); - var str = ''; - for (var i = 0; i < 65536; i++) { + str = ''; + for (let i = 0; i < 65536; i++) { str += '锋'; } assert.deepEqual(hessian.encode(str, '1.0'), utils.bytes('v1/string/utf8_65536')); assert(hessian.decode(utils.bytes('v1/string/utf8_65536')) === str); - var str = ''; - for (var i = 0; i < 65537; i++) { + str = ''; + for (let i = 0; i < 65537; i++) { str += '锋'; } assert.deepEqual(hessian.encode(str, '1.0'), utils.bytes('v1/string/utf8_65537')); assert(hessian.decode(utils.bytes('v1/string/utf8_65537')) === str); }); - it('should write string same as java write', function () { + it('should write string same as java write', function() { assert.deepEqual(hessian.encode('', '1.0'), utils.bytes('v1/string/empty')); assert.deepEqual(hessian.encode('foo'), utils.bytes('v1/string/foo')); assert.deepEqual(hessian.encode('中文 Chinese', '1.0'), utils.bytes('v1/string/chinese')); - var text4k = utils.string('4k'); + const text4k = utils.string('4k'); assert.deepEqual(hessian.encode(text4k, '1.0'), utils.bytes('v1/string/text4k')); assert(hessian.decode(utils.bytes('v1/string/text4k')) === text4k); - var largeBuf = new Buffer(32767); + let largeBuf = Buffer.alloc(32767); largeBuf.fill(0x41); assert.deepEqual( hessian.encode(largeBuf.toString(), '1.0'), @@ -108,7 +100,7 @@ describe('string.test.js', function () { hessian.decode(utils.bytes('v1/string/large_string_32767')) === largeBuf.toString() ); - var largeBuf = new Buffer(32768); + largeBuf = Buffer.alloc(32768); largeBuf.fill(0x41); assert.deepEqual( hessian.encode(largeBuf.toString(), '1.0'), @@ -118,7 +110,7 @@ describe('string.test.js', function () { hessian.decode(utils.bytes('v1/string/large_string_32768')) === largeBuf.toString() ); - var largeBuf = new Buffer(32769); + largeBuf = Buffer.alloc(32769); largeBuf.fill(0x41); assert.deepEqual( hessian.encode(largeBuf.toString(), '1.0'), @@ -128,7 +120,7 @@ describe('string.test.js', function () { hessian.decode(utils.bytes('v1/string/large_string_32769')) === largeBuf.toString() ); - var largeBuf = new Buffer(65534); + largeBuf = Buffer.alloc(65534); largeBuf.fill(0x41); assert.deepEqual( hessian.encode(largeBuf.toString(), '1.0'), @@ -138,7 +130,7 @@ describe('string.test.js', function () { hessian.decode(utils.bytes('v1/string/large_string_65534')) === largeBuf.toString() ); - var largeBuf = new Buffer(65535); + largeBuf = Buffer.alloc(65535); largeBuf.fill(0x41); assert.deepEqual( hessian.encode(largeBuf.toString(), '1.0'), @@ -148,7 +140,7 @@ describe('string.test.js', function () { hessian.decode(utils.bytes('v1/string/large_string_65535')) === largeBuf.toString() ); - var largeBuf = new Buffer(65536); + largeBuf = Buffer.alloc(65536); largeBuf.fill(0x41); assert.deepEqual( hessian.encode(largeBuf.toString(), '1.0'), @@ -158,7 +150,7 @@ describe('string.test.js', function () { hessian.decode(utils.bytes('v1/string/large_string_65536')) === largeBuf.toString() ); - var largeBuf = new Buffer(65537); + largeBuf = Buffer.alloc(65537); largeBuf.fill(0x41); assert.deepEqual( hessian.encode(largeBuf.toString(), '1.0'), @@ -169,10 +161,10 @@ describe('string.test.js', function () { ); }); - - it.skip('should write string same as java write exclude', function () { - var largeString = new Array(65535); - for (var i = 0; i < largeString.length; i += 2) { + + it.skip('should write string same as java write exclude', function() { + let largeString = new Array(65535); + for (let i = 0; i < largeString.length; i += 2) { largeString[i] = '\ud800'; // largeString[i] = String.fromCharCode(0xd800); if (i + 1 < largeString.length) { @@ -191,56 +183,60 @@ describe('string.test.js', function () { ); }); - describe('v2.0', function () { - it('should read short strings', function () { - assert(hessian.decode(new Buffer([0x00]), '2.0') === ''); - assert(hessian.decode(new Buffer([0x00]), '2.0', true) === ''); - assert(hessian.decode(Buffer.concat([new Buffer([0x05]), - new Buffer('hello')]), '2.0') === 'hello'); - assert(hessian.decode(new Buffer([0x01, 0xc3, 0x83]), '2.0') === '\u00c3'); - assert(hessian.decode(Buffer.concat([new Buffer([0x09]), - new Buffer('hello, 中文')]), '2.0') === 'hello, 中文'); + describe('v2.0', function() { + it('should read short strings', function() { + assert(hessian.decode(Buffer.from([ 0x00 ]), '2.0') === ''); + assert(hessian.decode(Buffer.from([ 0x00 ]), '2.0', true) === ''); + assert(hessian.decode(Buffer.concat([ Buffer.from([ 0x05 ]), + Buffer.from('hello'), + ]), '2.0') === 'hello'); + assert(hessian.decode(Buffer.from([ 0x01, 0xc3, 0x83 ]), '2.0') === '\u00c3'); + assert(hessian.decode(Buffer.concat([ Buffer.from([ 0x09 ]), + Buffer.from('hello, 中文'), + ]), '2.0') === 'hello, 中文'); }); - it('should read "hello" in long form', function () { - assert(hessian.decode(Buffer.concat([new Buffer(['S'.charCodeAt(0), 0x00, 0x05]), - new Buffer('hello')]), '2.0') === 'hello'); + it('should read "hello" in long form', function() { + assert(hessian.decode(Buffer.concat([ Buffer.from([ 'S'.charCodeAt(0), 0x00, 0x05 ]), + Buffer.from('hello'), + ]), '2.0') === 'hello'); }); - it('should read split into two chunks: s and short strings', function () { - assert(hessian.decode(Buffer.concat([new Buffer(['s'.charCodeAt(0), 0x00, 0x07]), - new Buffer('hello, '), new Buffer([0x05]), new Buffer('world')]), '2.0') === 'hello, world'); + it('should read split into two chunks: s and short strings', function() { + assert(hessian.decode(Buffer.concat([ Buffer.from([ 's'.charCodeAt(0), 0x00, 0x07 ]), + Buffer.from('hello, '), Buffer.from([ 0x05 ]), Buffer.from('world'), + ]), '2.0') === 'hello, world'); }); - it('should write short strings', function () { - assert.deepEqual(hessian.encode('', '2.0'), new Buffer([0x00])); + it('should write short strings', function() { + assert.deepEqual(hessian.encode('', '2.0'), Buffer.from([ 0x00 ])); assert.deepEqual(hessian.encode('foo', '2.0'), Buffer.concat([ - new Buffer([0x03]), - new Buffer('foo') + Buffer.from([ 0x03 ]), + Buffer.from('foo'), ])); assert.deepEqual(hessian.encode('0123456789012345678901234567890', '2.0'), Buffer.concat([ - new Buffer([0x1f]), - new Buffer('0123456789012345678901234567890') + Buffer.from([ 0x1f ]), + Buffer.from('0123456789012345678901234567890'), ])); - var len32Buf = new Buffer(2); + const len32Buf = Buffer.alloc(2); len32Buf.writeInt16BE(32, 0); assert.deepEqual(hessian.encode('01234567890123456789012345678901', '2.0'), Buffer.concat([ - new Buffer([0x53]), + Buffer.from([ 0x53 ]), len32Buf, - new Buffer('01234567890123456789012345678901') + Buffer.from('01234567890123456789012345678901'), ])); - var largeBuf = new Buffer(65535); + let largeBuf = Buffer.alloc(65535); largeBuf.fill(0x41); hessian.encode(largeBuf.toString(), '2.0'); - largeBuf = new Buffer(65535 * 3 + 100); + largeBuf = Buffer.alloc(65535 * 3 + 100); largeBuf.fill(0x41); hessian.encode(largeBuf.toString(), '2.0'); }); - it('should read java string', function () { + it('should read java string', function() { assert(hessian.decode(utils.bytes('v2/string/empty'), '2.0') === ''); assert(hessian.decode(utils.bytes('v2/string/foo'), '2.0') === 'foo'); assert(hessian.decode(utils.bytes('v2/string/chinese'), '2.0') === '中文 Chinese'); @@ -248,97 +244,97 @@ describe('string.test.js', function () { hessian.decode(utils.bytes('v2/string/text4k'), '2.0') === utils.string('4k') ); - var largeBuf = new Buffer(32767); + let largeBuf = Buffer.alloc(32767); largeBuf.fill(0x41); assert( hessian.decode(utils.bytes('v2/string/large_string_32767'), '2.0') === largeBuf.toString() ); - var largeBuf = new Buffer(32768); + largeBuf = Buffer.alloc(32768); largeBuf.fill(0x41); assert( hessian.decode(utils.bytes('v2/string/large_string_32768'), '2.0') === largeBuf.toString() ); - var largeBuf = new Buffer(32769); + largeBuf = Buffer.alloc(32769); largeBuf.fill(0x41); assert( hessian.decode(utils.bytes('v2/string/large_string_32769'), '2.0') === largeBuf.toString() ); - var largeBuf = new Buffer(65534); + largeBuf = Buffer.alloc(65534); largeBuf.fill(0x41); assert( hessian.decode(utils.bytes('v2/string/large_string_65534'), '2.0') === largeBuf.toString() ); - var largeBuf = new Buffer(65535); + largeBuf = Buffer.alloc(65535); largeBuf.fill(0x41); assert( hessian.decode(utils.bytes('v2/string/large_string_65535'), '2.0') === largeBuf.toString() ); - var largeBuf = new Buffer(65536); + largeBuf = Buffer.alloc(65536); largeBuf.fill(0x41); assert( hessian.decode(utils.bytes('v2/string/large_string_65536'), '2.0') === largeBuf.toString() ); - var largeBuf = new Buffer(65537); + largeBuf = Buffer.alloc(65537); largeBuf.fill(0x41); assert( hessian.decode(utils.bytes('v2/string/large_string_65537'), '2.0') === largeBuf.toString() ); }); - it('should write string same as java write', function () { + it('should write string same as java write', function() { assert.deepEqual(hessian.encode('', '2.0'), utils.bytes('v2/string/empty')); assert.deepEqual(hessian.encode('foo', '2.0'), utils.bytes('v2/string/foo')); assert.deepEqual(hessian.encode('中文 Chinese', '2.0'), utils.bytes('v2/string/chinese')); - var text4k = utils.string('4k'); + const text4k = utils.string('4k'); assert.deepEqual(hessian.encode(text4k, '2.0'), utils.bytes('v2/string/text4k')); - var largeBuf = new Buffer(32767); + let largeBuf = Buffer.alloc(32767); largeBuf.fill(0x41); assert.deepEqual( hessian.encode(largeBuf.toString(), '2.0'), utils.bytes('v2/string/large_string_32767') ); - var largeBuf = new Buffer(32768); + largeBuf = Buffer.alloc(32768); largeBuf.fill(0x41); assert.deepEqual( hessian.encode(largeBuf.toString(), '2.0'), utils.bytes('v2/string/large_string_32768') ); - var largeBuf = new Buffer(32769); + largeBuf = Buffer.alloc(32769); largeBuf.fill(0x41); assert.deepEqual( hessian.encode(largeBuf.toString(), '2.0'), utils.bytes('v2/string/large_string_32769') ); - var largeBuf = new Buffer(65534); + largeBuf = Buffer.alloc(65534); largeBuf.fill(0x41); assert.deepEqual( hessian.encode(largeBuf.toString(), '2.0'), utils.bytes('v2/string/large_string_65534') ); - var largeBuf = new Buffer(65535); + largeBuf = Buffer.alloc(65535); largeBuf.fill(0x41); assert.deepEqual( hessian.encode(largeBuf.toString(), '2.0'), utils.bytes('v2/string/large_string_65535') ); - var largeBuf = new Buffer(65536); + largeBuf = Buffer.alloc(65536); largeBuf.fill(0x41); assert.deepEqual( hessian.encode(largeBuf.toString(), '2.0'), utils.bytes('v2/string/large_string_65536') ); - var largeBuf = new Buffer(65537); + largeBuf = Buffer.alloc(65537); largeBuf.fill(0x41); assert.deepEqual( hessian.encode(largeBuf.toString(), '2.0'), @@ -346,10 +342,10 @@ describe('string.test.js', function () { ); }); - - it.skip('should write string same as java write exclude', function () { - var largeString = new Array(65535); - for (var i = 0; i < largeString.length; i += 2) { + + it.skip('should write string same as java write exclude', function() { + let largeString = new Array(65535); + for (let i = 0; i < largeString.length; i += 2) { largeString[i] = String.fromCharCode(0xd800); if (i + 1 < largeString.length) { largeString[i + 1] = String.fromCharCode(0xdbff); @@ -362,57 +358,57 @@ describe('string.test.js', function () { ); }); - it('should read and write utf8 string as java', function () { - var str = ''; - for (var i = 0; i < 32767; i++) { + it('should read and write utf8 string as java', function() { + let str = ''; + for (let i = 0; i < 32767; i++) { str += '锋'; } assert.deepEqual(hessian.encode(str, '2.0'), utils.bytes('v2/string/utf8_32767')); assert(hessian.decode(utils.bytes('v2/string/utf8_32767'), '2.0') === str); assert(hessian.decode(utils.bytes('v1/string/utf8_32767'), '2.0') === str); - var str = ''; - for (var i = 0; i < 32768; i++) { + str = ''; + for (let i = 0; i < 32768; i++) { str += '锋'; } assert.deepEqual(hessian.encode(str, '2.0'), utils.bytes('v2/string/utf8_32768')); assert(hessian.decode(utils.bytes('v2/string/utf8_32768'), '2.0') === str); assert(hessian.decode(utils.bytes('v1/string/utf8_32768'), '2.0') === str); - var str = ''; - for (var i = 0; i < 32769; i++) { + str = ''; + for (let i = 0; i < 32769; i++) { str += '锋'; } assert.deepEqual(hessian.encode(str, '2.0'), utils.bytes('v2/string/utf8_32769')); assert(hessian.decode(utils.bytes('v2/string/utf8_32769'), '2.0') === str); assert(hessian.decode(utils.bytes('v1/string/utf8_32769'), '2.0') === str); - var str = ''; - for (var i = 0; i < 65534; i++) { + str = ''; + for (let i = 0; i < 65534; i++) { str += '锋'; } assert.deepEqual(hessian.encode(str, '2.0'), utils.bytes('v1/string/utf8_65534')); assert(hessian.decode(utils.bytes('v1/string/utf8_65534'), '2.0') === str); assert(hessian.decode(utils.bytes('v1/string/utf8_65534'), '2.0') === str); - var str = ''; - for (var i = 0; i < 65535; i++) { + str = ''; + for (let i = 0; i < 65535; i++) { str += '锋'; } assert.deepEqual(hessian.encode(str, '2.0'), utils.bytes('v2/string/utf8_65535')); assert(hessian.decode(utils.bytes('v2/string/utf8_65535'), '2.0') === str); assert(hessian.decode(utils.bytes('v1/string/utf8_65535'), '2.0') === str); - var str = ''; - for (var i = 0; i < 65536; i++) { + str = ''; + for (let i = 0; i < 65536; i++) { str += '锋'; } assert.deepEqual(hessian.encode(str, '2.0'), utils.bytes('v2/string/utf8_65536')); assert(hessian.decode(utils.bytes('v2/string/utf8_65536'), '2.0') === str); assert(hessian.decode(utils.bytes('v1/string/utf8_65536'), '2.0') === str); - var str = ''; - for (var i = 0; i < 65537; i++) { + str = ''; + for (let i = 0; i < 65537; i++) { str += '锋'; } assert.deepEqual(hessian.encode(str, '2.0'), utils.bytes('v2/string/utf8_65537')); diff --git a/test/utils.js b/test/utils.js index efc5ebd..e7532e5 100644 --- a/test/utils.js +++ b/test/utils.js @@ -14,14 +14,14 @@ * Module dependencies. */ -var fs = require('fs'); -var path = require('path'); -var fixtures = path.join(__dirname, 'fixtures'); +const fs = require('fs'); +const path = require('path'); +const fixtures = path.join(__dirname, 'fixtures'); -exports.bytes = function (name) { +exports.bytes = function(name) { return fs.readFileSync(path.join(fixtures, name + '.bin')); }; -exports.string = function (name) { +exports.string = function(name) { return fs.readFileSync(path.join(fixtures, name + '.txt'), 'utf8'); }; diff --git a/test/utils.test.js b/test/utils.test.js index 927e61d..f3ae121 100644 --- a/test/utils.test.js +++ b/test/utils.test.js @@ -8,18 +8,18 @@ * fengmk2 (http://fengmk2.github.com) */ -"use strict"; +'use strict'; -var assert = require('assert'); -var utils = require('../lib/utils'); +const assert = require('assert'); +const utils = require('../lib/utils'); -describe('utils.test.js', function () { - describe('getSerializer()', function () { - it('should [int get writeArray', function () { +describe('utils.test.js', function() { + describe('getSerializer()', function() { + it('should [int get writeArray', function() { assert(utils.getSerializer('[int') === 'writeArray'); }); - it('should [string get writeArray', function () { + it('should [string get writeArray', function() { assert(utils.getSerializer('[string') === 'writeArray'); }); }); diff --git a/test/v1.test.js b/test/v1.test.js index 7402ed9..d9c813f 100644 --- a/test/v1.test.js +++ b/test/v1.test.js @@ -1,63 +1,63 @@ 'use strict'; -var assert = require('assert'); -var fs = require('fs'); -var path = require('path'); -var hessian = require('../'); -var Encoder = hessian.Encoder; -var Decoder = hessian.Decoder; -var utils = require('../lib/utils'); - -var encoder = new Encoder(); -var decoder = new Decoder(); - -var fixtureString = fs.readFileSync(path.join(__dirname, 'support', 'fixture.dat'), 'utf8'); -var fixtureBytes = fs.readFileSync(path.join(__dirname, 'support', 'fixture.png')); - -describe('hessian v1', function () { - afterEach(function () { +const assert = require('assert'); +const fs = require('fs'); +const path = require('path'); +const hessian = require('../'); +const Encoder = hessian.Encoder; +const Decoder = hessian.Decoder; +const utils = require('../lib/utils'); + +const encoder = new Encoder(); +const decoder = new Decoder(); + +const fixtureString = fs.readFileSync(path.join(__dirname, 'support', 'fixture.dat'), 'utf8'); +const fixtureBytes = fs.readFileSync(path.join(__dirname, 'support', 'fixture.png')); + +describe('hessian v1', function() { + afterEach(function() { encoder.clean(); decoder.clean(); }); - describe('null', function () { - it('should write and read null ok', function () { - var buf = encoder.writeNull().get(); - assert.deepEqual(buf, new Buffer('N')); + describe('null', function() { + it('should write and read null ok', function() { + const buf = encoder.writeNull().get(); + assert.deepEqual(buf, Buffer.from('N')); decoder.init(buf); assert(decoder.readNull() === null); }); }); - describe('bool', function () { - it('should write and read true ok', function () { - var buf = encoder.writeBool(true).get(); - assert.deepEqual(buf, new Buffer('T')); + describe('bool', function() { + it('should write and read true ok', function() { + const buf = encoder.writeBool(true).get(); + assert.deepEqual(buf, Buffer.from('T')); assert(decoder.init(buf).readBool() === true); }); - it('should write and read false ok', function () { - var buf = encoder.writeBool(false).get(); - assert.deepEqual(buf, new Buffer('F')); + it('should write and read false ok', function() { + const buf = encoder.writeBool(false).get(); + assert.deepEqual(buf, Buffer.from('F')); assert(decoder.init(buf).readBool() === false); }); }); - describe('int', function () { - it('should write and read int ok', function () { - var tests = [ - [-10000, ''], - [-1, ''], - [0, ''], - [100000, ''], - [Math.pow(2, 31) - 1, ''], + describe('int', function() { + it('should write and read int ok', function() { + const tests = [ + [ -10000, '' ], + [ -1, '' ], + [ 0, '' ], + [ 100000, '' ], + [ Math.pow(2, 31) - 1, '' ], ]; - tests.forEach(function (t) { - var buf = encoder.writeInt(t[0]).get(); + tests.forEach(function(t) { + const buf = encoder.writeInt(t[0]).get(); assert(buf.inspect() === t[1]); decoder.init(buf); assert(decoder.readInt() === t[0]); @@ -66,47 +66,47 @@ describe('hessian v1', function () { }); }); - it('should write int error', function () { - var tests = [ + it('should write int error', function() { + const tests = [ 1.1, Math.pow(2, 31), - -Math.pow(2, 31) - 1 + -Math.pow(2, 31) - 1, ]; - tests.forEach(function (t, idx) { - assert.throws(function () { - var buf = encoder.writeInt(t); + tests.forEach(function(t, idx) { + assert.throws(function() { + encoder.writeInt(t); }, null, 'hessian writeInt expect input type is `int32`, but got `number` : ' + tests[idx] + ' '); }); }); - it('should read int error', function () { - var tests = [ - [new Buffer([0x48, 0x00, 0x00, 0x00, 0x00]), 'hessian readInt only accept label `I` but got unexpect label `H`'], + it('should read int error', function() { + const tests = [ + [ Buffer.from([ 0x48, 0x00, 0x00, 0x00, 0x00 ]), 'hessian readInt only accept label `I` but got unexpect label `H`' ], ]; - tests.forEach(function (t) { - assert.throws(function () { + tests.forEach(function(t) { + assert.throws(function() { decoder.init(t[0]).readInt(); }, null, t[1]); }); }); }); - describe('long', function () { - it('should write and read long ok', function () { - var tests = [ - ['-9223372036854775808', ''], - [-10000, ''], - [-1, ''], - [0, ''], - [10000, ''], - [9007199254740992, ''], - ['9007199254740993', ''], - ['9223372036854775807', ''], + describe('long', function() { + it('should write and read long ok', function() { + const tests = [ + [ '-9223372036854775808', '' ], + [ -10000, '' ], + [ -1, '' ], + [ 0, '' ], + [ 10000, '' ], + [ 9007199254740992, '' ], + [ '9007199254740993', '' ], + [ '9223372036854775807', '' ], ]; - tests.forEach(function (t) { - var buf = encoder.writeLong(t[0]).get(); + tests.forEach(function(t) { + const buf = encoder.writeLong(t[0]).get(); assert(buf.inspect() === t[1]); assert.deepEqual(decoder.init(buf).readLong(), t[0]); encoder.clean(); @@ -114,46 +114,47 @@ describe('hessian v1', function () { }); }); - it('should write long error', function () { - var tests = [ + it('should write long error', function() { + const tests = [ '-9223372036854775.1', - '92233720368547758111' + '92233720368547758111', ]; - tests.forEach(function (t) { - var buf = encoder.writeLong(t).get(); + tests.forEach(function(t) { + const buf = encoder.writeLong(t).get(); assert.notStrictEqual(decoder.init(buf).readLong(), t); }); }); - it('should read long error', function () { - var tests = [ - [new Buffer([0x4b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), - 'hessian readLong only accept label `L` but got unexpect label `K`'] + it('should read long error', function() { + const tests = [ + [ Buffer.from([ 0x4b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ]), + 'hessian readLong only accept label `L` but got unexpect label `K`', + ], ]; - tests.forEach(function (t) { - assert.throws(function () { + tests.forEach(function(t) { + assert.throws(function() { decoder.init(t[0]).readLong(); }, null, t[1]); }); }); }); - describe('double', function () { - it('should write and read double ok', function () { - var tests = [ - [-1e100, ''], - [-1.123, ''], - [-1, ''], - [0, ''], - [1, ''], - [1.111, ''], + describe('double', function() { + it('should write and read double ok', function() { + const tests = [ + [ -1e100, '' ], + [ -1.123, '' ], + [ -1, '' ], + [ 0, '' ], + [ 1, '' ], + [ 1.111, '' ], // 1e320 - [Infinity, ''], + [ Infinity, '' ], ]; - tests.forEach(function (t) { - var buf = encoder.writeDouble(t[0]).get(); + tests.forEach(function(t) { + const buf = encoder.writeDouble(t[0]).get(); assert(buf.inspect() === t[1]); assert(decoder.init(buf).readDouble() === t[0]); encoder.clean(); @@ -161,28 +162,29 @@ describe('hessian v1', function () { }); }); - it('should read double error', function () { - var tests = [ - [new Buffer([0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), - 'hessian readDouble only accept label `D` but got unexpect label `E`'] + it('should read double error', function() { + const tests = [ + [ Buffer.from([ 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ]), + 'hessian readDouble only accept label `D` but got unexpect label `E`', + ], ]; - tests.forEach(function (t) { - assert.throws(function () { + tests.forEach(function(t) { + assert.throws(function() { decoder.init(t[0]).readDouble(); }, null, t[1]); }); }); }); - describe('date', function () { - it('should write and read Date ok', function () { - var tests = [ - [new Date('2010-10-10'), ''], - [new Date(0), ''], + describe('date', function() { + it('should write and read Date ok', function() { + const tests = [ + [ new Date('2010-10-10'), '' ], + [ new Date(0), '' ], ]; - tests.forEach(function (t) { - var buf = encoder.writeDate(t[0]).get(); + tests.forEach(function(t) { + const buf = encoder.writeDate(t[0]).get(); assert(buf.inspect() === t[1]); assert.deepEqual(decoder.init(buf).readDate(), t[0]); encoder.clean(); @@ -190,184 +192,185 @@ describe('hessian v1', function () { }); }); - it('should read date error', function () { - var tests = [ - [new Buffer([0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), - 'hessian readDate only accept label `d` but got unexpect label `e`'] + it('should read date error', function() { + const tests = [ + [ Buffer.from([ 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ]), + 'hessian readDate only accept label `d` but got unexpect label `e`', + ], ]; - tests.forEach(function (t) { - assert.throws(function () { + tests.forEach(function(t) { + assert.throws(function() { decoder.init(t[0]).readDate(); }, null, t[1]); }); }); }); - describe('bytes', function () { - it('should write and read small bytes ok', function () { - var inputBuffer = new Buffer([1, 2, 3, 4, 5]); - var buf = encoder.writeBytes(inputBuffer).get(); + describe('bytes', function() { + it('should write and read small bytes ok', function() { + const inputBuffer = Buffer.from([ 1, 2, 3, 4, 5 ]); + const buf = encoder.writeBytes(inputBuffer).get(); assert(buf.inspect() === ''); assert.deepEqual(decoder.init(buf).readBytes(), inputBuffer); }); - it('should write and read big bytes ok', function () { - var inputBuffer = fixtureBytes; - var inputLength = inputBuffer.length; - var buf = encoder.writeBytes(inputBuffer).get(); + it('should write and read big bytes ok', function() { + const inputBuffer = fixtureBytes; + const inputLength = inputBuffer.length; + const buf = encoder.writeBytes(inputBuffer).get(); assert(buf.length === inputLength + Math.ceil(inputLength / utils.MAX_BYTE_TRUNK_SIZE) * 3); assert.deepEqual(decoder.init(buf).readBytes(), inputBuffer); }); - it('should read bytes error', function () { - var tests = [ - [new Buffer([0x41, 0x00, 0x02, 0x00]), 'hessian readBytes only accept label `b,B` but got unexpect label `A`'], - [new Buffer([0x62, 0x00, 0x01, 0x00, 0x01]), 'hessian readBytes only accept label `b,B` but got unexpect label `\u0001`'], + it('should read bytes error', function() { + const tests = [ + [ Buffer.from([ 0x41, 0x00, 0x02, 0x00 ]), 'hessian readBytes only accept label `b,B` but got unexpect label `A`' ], + [ Buffer.from([ 0x62, 0x00, 0x01, 0x00, 0x01 ]), 'hessian readBytes only accept label `b,B` but got unexpect label `\u0001`' ], ]; - tests.forEach(function (t) { - assert.throws(function () { - var buf = decoder.init(t[0]).readBytes(); + tests.forEach(function(t) { + assert.throws(function() { + decoder.init(t[0]).readBytes(); }, null, t[1]); }); }); - it('should bytes length equal MAX_BYTE_TRUNK_SIZE work', function () { - var oneTrunkBuf = new Buffer(utils.MAX_BYTE_TRUNK_SIZE); - var buf = encoder.writeBytes(oneTrunkBuf).get(); + it('should bytes length equal MAX_BYTE_TRUNK_SIZE work', function() { + const oneTrunkBuf = Buffer.alloc(utils.MAX_BYTE_TRUNK_SIZE); + let buf = encoder.writeBytes(oneTrunkBuf).get(); assert.deepEqual(decoder.init(buf).readBytes(), oneTrunkBuf); encoder.clean(); - var twoTrunkBuf = new Buffer(utils.MAX_BYTE_TRUNK_SIZE * 2); + const twoTrunkBuf = Buffer.alloc(utils.MAX_BYTE_TRUNK_SIZE * 2); buf = encoder.writeBytes(twoTrunkBuf).get(); assert.deepEqual(decoder.init(buf).readBytes(), twoTrunkBuf); }); - it('should write type error', function () { - assert.throws(function () { + it('should write type error', function() { + assert.throws(function() { encoder.writeBytes(); }, null, 'hessian writeBytes expect input type is `buffer`, but got `undefined` : undefined '); - assert.throws(function () { + assert.throws(function() { encoder.writeBytes(''); }, null, 'hessian writeBytes expect input type is `buffer`, but got `string` : "" '); - assert.throws(function () { + assert.throws(function() { encoder.writeBytes(null); }, null, 'hessian writeBytes expect input type is `buffer`, but got `object` : null '); - assert.throws(function () { + assert.throws(function() { encoder.writeBytes(100); }, null, 'hessian writeBytes expect input type is `buffer`, but got `number` : 100 '); }); - it('should write and read empty bytes', function () { - var buf = encoder.writeBytes(new Buffer('')).get(); - assert.deepEqual(decoder.init(buf).readBytes(), new Buffer('')); + it('should write and read empty bytes', function() { + const buf = encoder.writeBytes(Buffer.from('')).get(); + assert.deepEqual(decoder.init(buf).readBytes(), Buffer.from('')); }); }); - describe('string', function () { - it('should write and read small string ok', function () { - var inputStr = '你好,hessian.∆∆˚œø∂πß∂µ'; - var buf = encoder.writeString(inputStr).get(); + describe('string', function() { + it('should write and read small string ok', function() { + const inputStr = '你好,hessian.∆∆˚œø∂πß∂µ'; + const buf = encoder.writeString(inputStr).get(); assert( buf.inspect() === '' ); assert(decoder.init(buf).readString() === inputStr); }); - it('should write and read big string ok', function () { - var inputStr = fixtureString; - var inputStrLength = inputStr.length; - var buf = encoder.writeString(inputStr).get(); - assert(buf.length === new Buffer(inputStr).length + + it('should write and read big string ok', function() { + const inputStr = fixtureString; + const inputStrLength = inputStr.length; + const buf = encoder.writeString(inputStr).get(); + assert(buf.length === Buffer.from(inputStr).length + Math.ceil(inputStrLength / utils.MAX_CHAR_TRUNK_SIZE) * 3); assert(decoder.init(buf).readString() === inputStr); }); - it('should read string error', function () { - var tests = [ - [new Buffer([0x72, 0x00, 0x02, 0x00]), 'hessian readString error, unexpect string code: 0x72'], - [new Buffer([0x73, 0x00, 0x01, 0x00, 0x01]), 'hessian readString error, unexpect string code: 0x1'], - [new Buffer([0x73, 0x00, 0x01, 0xf0, 0x20]), 'string is not valid UTF-8 encode'], + it('should read string error', function() { + const tests = [ + [ Buffer.from([ 0x72, 0x00, 0x02, 0x00 ]), 'hessian readString error, unexpect string code: 0x72' ], + [ Buffer.from([ 0x73, 0x00, 0x01, 0x00, 0x01 ]), 'hessian readString error, unexpect string code: 0x1' ], + [ Buffer.from([ 0x73, 0x00, 0x01, 0xf0, 0x20 ]), 'string is not valid UTF-8 encode' ], ]; - tests.forEach(function (t) { - assert.throws(function () { - var buf = decoder.init(t[0]).readString(); + tests.forEach(function(t) { + assert.throws(function() { + decoder.init(t[0]).readString(); }, null, t[1]); }); }); - it('should write type error', function () { - assert.throws(function () { + it('should write type error', function() { + assert.throws(function() { encoder.writeString(); }, null, 'hessian writeString expect input type is `string`, but got `undefined` : undefined '); // v0.10.28 return [1,2,3,4,5] // (function () { - // encoder.writeString(new Buffer([1,2,3,4,5])); + // encoder.writeString(Buffer.from([1,2,3,4,5])); // }).should.throw('hessian writeString expect input type is `string`, but got `object` : {"type":"Buffer","data":[1,2,3,4,5]} '); - assert.throws(function () { + assert.throws(function() { encoder.writeString(null); }, null, 'hessian writeString expect input type is `string`, but got `object` : null '); - assert.throws(function () { + assert.throws(function() { encoder.writeString(100); }, null, 'hessian writeString expect input type is `string`, but got `number` : 100 '); }); - it('should string length equal MAX_CHAR_TRUNK_SIZE work', function () { - var oneTrunkString = new Buffer(utils.MAX_CHAR_TRUNK_SIZE); + it('should string length equal MAX_CHAR_TRUNK_SIZE work', function() { + let oneTrunkString = Buffer.alloc(utils.MAX_CHAR_TRUNK_SIZE); oneTrunkString.fill(0x41); oneTrunkString = oneTrunkString.toString(); - var buf = encoder.writeString(oneTrunkString).get(); + let buf = encoder.writeString(oneTrunkString).get(); assert.deepEqual(decoder.init(buf).readString(), oneTrunkString); encoder.clean(); - var twoTrunkString = new Buffer(utils.MAX_CHAR_TRUNK_SIZE * 2); + let twoTrunkString = Buffer.alloc(utils.MAX_CHAR_TRUNK_SIZE * 2); twoTrunkString.fill(0x41); twoTrunkString = twoTrunkString.toString(); buf = encoder.writeString(twoTrunkString).get(); assert.deepEqual(decoder.init(buf).read(), twoTrunkString); }); - it('should write and read empty string', function () { - var buf = encoder.writeString('').get(); + it('should write and read empty string', function() { + const buf = encoder.writeString('').get(); assert(decoder.init(buf).read() === ''); }); }); - describe('object', function () { - it('should write and get simple object ok', function () { - var testObject = { + describe('object', function() { + it('should write and get simple object ok', function() { + const testObject = { a: 1, b: 'string', c: true, d: 1.1, e: Math.pow(2, 40), - f: [1, 2, 3, '4', true, 5], - g: {a: 1, b: true, c: 'string'} + f: [ 1, 2, 3, '4', true, 5 ], + g: { a: 1, b: true, c: 'string' }, }; - var buf = encoder.writeObject(testObject).get(); + const buf = encoder.writeObject(testObject).get(); assert.deepEqual(decoder.init(buf).readObject(), testObject); }); - it('should write null obejct ok', function () { - var nullObject = null; - var nullBuf = encoder.writeObject(nullObject).get(); + it('should write null obejct ok', function() { + const nullObject = null; + const nullBuf = encoder.writeObject(nullObject).get(); assert(decoder.init(nullBuf).read() === null); }); - it('should write and read object with circular ok', function () { - var testObject = { + it('should write and read object with circular ok', function() { + const testObject = { a: 1, b: 'string', c: {}, - d: [1, 2] + d: [ 1, 2 ], }; testObject.c.a = testObject; testObject.d.push(testObject.c); - var buf = encoder.writeObject(testObject).get(); - var res = decoder.init(buf).readObject(); + const buf = encoder.writeObject(testObject).get(); + const res = decoder.init(buf).readObject(); assert(res.a === testObject.a); assert(res.b === testObject.b); assert(res.c.a.a === testObject.a); @@ -376,243 +379,242 @@ describe('hessian v1', function () { assert(res.d[2].a.b === testObject.b); }); - it('should write and read complex object ok', function () { - var testObject = { + it('should write and read complex object ok', function() { + const testObject = { $class: 'com.hessian.TestObject', $: { a: 1, - b: {$class: 'java.util.List', $: [1, 2, 3]} - } + b: { $class: 'java.util.List', $: [ 1, 2, 3 ] }, + }, }; - var buf = encoder.writeObject(testObject).get(); - var res = decoder.init(buf).readObject(); - assert.deepEqual(res, {a:1, b:[1, 2, 3]}); - var resWithType = decoder.init(buf).readObject(true); + const buf = encoder.writeObject(testObject).get(); + const res = decoder.init(buf).readObject(); + assert.deepEqual(res, { a: 1, b: [ 1, 2, 3 ] }); + const resWithType = decoder.init(buf).readObject(true); assert.deepEqual(resWithType, { - '$class': 'com.hessian.TestObject', - '$': { - a: { '$class': 'int', '$': 1 }, + $class: 'com.hessian.TestObject', + $: { + a: { $class: 'int', $: 1 }, b: { - '$class': 'java.util.List', - '$': [ - { '$class': 'int', '$': 1 }, - { '$class': 'int', '$': 2 }, - { '$class': 'int', '$': 3 } - ] - } - } + $class: 'java.util.List', + $: [ + { $class: 'int', $: 1 }, + { $class: 'int', $: 2 }, + { $class: 'int', $: 3 }, + ], + }, + }, }); }); - it('should read complex object type use positon ok', function () { - var testObject = { + it('should read complex object type use positon ok', function() { + const testObject = { $class: 'com.hessian.TestObject', $: { a: 1, - b: {$class: 'java.util.List', $: [1, 2, 3]} - } + b: { $class: 'java.util.List', $: [ 1, 2, 3 ] }, + }, }; - var buf = encoder.writeObject(testObject).get(); + const buf = encoder.writeObject(testObject).get(); decoder.init(buf); assert(decoder.position() === 0); - decoder.position(1); //skip 'M' + decoder.position(1); // skip 'M' assert(decoder.readType() === 'com.hessian.TestObject'); assert(decoder.position() === 26); assert.deepEqual(decoder.position(0).readObject(true), { - '$class': 'com.hessian.TestObject', - '$': { - a: { '$class': 'int', '$': 1 }, + $class: 'com.hessian.TestObject', + $: { + a: { $class: 'int', $: 1 }, b: { - '$class': 'java.util.List', - '$': [ - { '$class': 'int', '$': 1 }, - { '$class': 'int', '$': 2 }, - { '$class': 'int', '$': 3 } - ] - } - } + $class: 'java.util.List', + $: [ + { $class: 'int', $: 1 }, + { $class: 'int', $: 2 }, + { $class: 'int', $: 3 }, + ], + }, + }, }); }); - it('should write "java.util.HashMap" treat as {}', function () { - var testObject = { + it('should write "java.util.HashMap" treat as {}', function() { + const testObject = { $class: 'java.util.HashMap', - $: {foo: 'bar'} + $: { foo: 'bar' }, }; - var buf = encoder.writeObject(testObject).get(); + const buf = encoder.writeObject(testObject).get(); encoder.clean(); - assert.deepEqual(buf, encoder.writeObject({foo: 'bar'}).get()); - assert.deepEqual(decoder.init(buf).read(), {foo: 'bar'}); + assert.deepEqual(buf, encoder.writeObject({ foo: 'bar' }).get()); + assert.deepEqual(decoder.init(buf).read(), { foo: 'bar' }); assert.deepEqual(decoder.init(buf).read(true), { - '$class': 'java.util.HashMap', - '$': { - foo: { '$class': 'java.lang.String', '$': 'bar' } - } + $class: 'java.util.HashMap', + $: { + foo: { $class: 'java.lang.String', $: 'bar' }, + }, }); }); - it('should write type error', function () { - assert.throws(function () { + it('should write type error', function() { + assert.throws(function() { encoder.writeObject('123'); }, null, 'hessian writeObject / writeMap expect input type is `object`, but got `string` : "123" '); - assert.throws(function () { + assert.throws(function() { encoder.writeObject(1.111); }, null, 'hessian writeObject / writeMap expect input type is `object`, but got `number` : 1.111 '); - assert.throws(function () { + assert.throws(function() { encoder.writeObject(100); }, null, 'hessian writeObject / writeMap expect input type is `object`, but got `number` : 100 '); }); }); - describe('array', function () { - it('should write and read simple array ok', function () { - var testArray = [1, true, 'string', 1.1, new Date()]; - var buf = encoder.writeArray(testArray).get(); + describe('array', function() { + it('should write and read simple array ok', function() { + const testArray = [ 1, true, 'string', 1.1, new Date() ]; + const buf = encoder.writeArray(testArray).get(); assert.deepEqual(decoder.init(buf).readArray(), testArray); }); - it('should write circular array ok', function () { - var testArray = [1]; + it('should write circular array ok', function() { + const testArray = [ 1 ]; testArray.push(testArray); - var buf = encoder.writeArray(testArray).get(); - var res = decoder.init(buf).readArray(); + const buf = encoder.writeArray(testArray).get(); + const res = decoder.init(buf).readArray(); assert(res[0] === testArray[0]); assert(res[1][1][1][0] === testArray[0]); }); - it('should write and read complex array ok', function () { - var testArray = { + it('should write and read complex array ok', function() { + const testArray = { $class: 'java.util.Set', $: [{ $class: 'java.util.List', - $: [1, 2, 3] - }] + $: [ 1, 2, 3 ], + }], }; - var buf = encoder.writeArray(testArray).get(); - assert.deepEqual(decoder.init(buf).readArray(), [[1, 2, 3]]); + const buf = encoder.writeArray(testArray).get(); + assert.deepEqual(decoder.init(buf).readArray(), [ + [ 1, 2, 3 ], + ]); assert.deepEqual(decoder.init(buf).readArray(true), { - '$class': 'java.util.Set', - '$': [ - { - '$class': 'java.util.List', - '$': [ - { '$class': 'int', '$': 1 }, - { '$class': 'int', '$': 2 }, - { '$class': 'int', '$': 3 } - ] - } - ] + $class: 'java.util.Set', + $: [{ + $class: 'java.util.List', + $: [ + { $class: 'int', $: 1 }, + { $class: 'int', $: 2 }, + { $class: 'int', $: 3 }, + ], + }], }); }); - it('should write "java.util.ArrayList" treat as []', function () { - var testArray = { + it('should write "java.util.ArrayList" treat as []', function() { + const testArray = { $class: 'java.util.ArrayList', - $: [1, 2, 3] + $: [ 1, 2, 3 ], }; - var buf = encoder.writeArray(testArray).get(); + const buf = encoder.writeArray(testArray).get(); encoder.clean(); - assert.deepEqual(buf, encoder.writeArray([1, 2, 3]).get()); - assert.deepEqual(decoder.init(buf).read(), [1, 2, 3]); + assert.deepEqual(buf, encoder.writeArray([ 1, 2, 3 ]).get()); + assert.deepEqual(decoder.init(buf).read(), [ 1, 2, 3 ]); assert.deepEqual(decoder.init(buf).read(true), { $class: 'java.util.ArrayList', $: [ - { '$class': 'int', '$': 1 }, - { '$class': 'int', '$': 2 }, - { '$class': 'int', '$': 3 } - ] + { $class: 'int', $: 1 }, + { $class: 'int', $: 2 }, + { $class: 'int', $: 3 }, + ], }); }); - it('should read unexpect end label', function () { - var buf = encoder.writeArray([1, 2, 3]).get(); + it('should read unexpect end label', function() { + const buf = encoder.writeArray([ 1, 2, 3 ]).get(); buf[buf.length - 1] = 40; - assert.throws(function () { + assert.throws(function() { decoder.init(buf).read('hessian readArray error, unexpect end label: ('); }); }); - it('should write type error', function () { - assert.throws(function () { + it('should write type error', function() { + assert.throws(function() { encoder.writeArray(); }, null, 'hessian writeArray input type invalid'); - assert.throws(function () { + assert.throws(function() { encoder.writeArray('123'); }, null, 'hessian writeArray input type invalid'); - assert.throws(function () { + assert.throws(function() { encoder.writeArray(1.111); }, null, 'hessian writeArray input type invalid'); - assert.throws(function () { + assert.throws(function() { encoder.writeArray(100); }, null, 'hessian writeArray input type invalid'); }); }); - describe('java.lang.Object', function () { - it('should encode and decode ok', function () { + describe('java.lang.Object', function() { + it('should encode and decode ok', function() { [ 100, 'string', true, new Date(), - [1, 2, 3], - {foo: 'bar'} + [ 1, 2, 3 ], + { foo: 'bar' }, ].forEach(function(val) { - var buf = encoder.write({$class:'java.lang.Object', $: val}).get(); + const buf = encoder.write({ $class: 'java.lang.Object', $: val }).get(); encoder.clean(); assert.deepEqual(decoder.init(buf).read(), val); }); }); }); - describe('encode and decode', function () { - it('should encode and decode work ok', function () { - var now = new Date(); - var tests = [ - [1, {$class: 'int', $: 1}], - [1.1, {$class: 'double', $: 1.1}], - [-10, {$class: 'int', $: -10}], - [Math.pow(2, 50), { '$class': 'long', '$': Math.pow(2, 50)}], - [{$class: 'long', $: '288230376151711740'}], - [{$class: 'boolean', $: 1}, {$class: 'boolean', $: true}], - [now, {'$class': 'java.util.Date', $: now}], - [true, {$class: 'boolean', $: true}], - [false, {$class: 'boolean', $: false}], - [null], - [undefined], - [{ a: 1, b: [true, false], c: now, d: {}, e: null }, { - $class: 'java.util.HashMap', - $:{ - a: { '$class': 'int', '$': 1 }, - b: { - $class: 'java.util.ArrayList', - $: [ { '$class': 'boolean', '$': true }, { '$class': 'boolean', '$': false } ] - }, - c: { '$class': 'java.util.Date', '$': now }, - d: { '$class': 'java.util.HashMap', '$': {} }, - e: null - } - } - ] + describe('encode and decode', function() { + it('should encode and decode work ok', function() { + const now = new Date(); + const tests = [ + [ 1, { $class: 'int', $: 1 }], + [ 1.1, { $class: 'double', $: 1.1 }], + [ -10, { $class: 'int', $: -10 }], + [ Math.pow(2, 50), { $class: 'long', $: Math.pow(2, 50) }], + [{ $class: 'long', $: '288230376151711740' }], + [{ $class: 'boolean', $: 1 }, { $class: 'boolean', $: true }], + [ now, { $class: 'java.util.Date', $: now }], + [ true, { $class: 'boolean', $: true }], + [ false, { $class: 'boolean', $: false }], + [ null ], + [ undefined ], + [{ a: 1, b: [ true, false ], c: now, d: {}, e: null }, { + $class: 'java.util.HashMap', + $: { + a: { $class: 'int', $: 1 }, + b: { + $class: 'java.util.ArrayList', + $: [{ $class: 'boolean', $: true }, { $class: 'boolean', $: false }], + }, + c: { $class: 'java.util.Date', $: now }, + d: { $class: 'java.util.HashMap', $: {} }, + e: null, + }, + }], ]; - tests.forEach(function (t) { - var buf = hessian.encode(t[0]); - var res = hessian.decode(buf, true); + tests.forEach(function(t) { + const buf = hessian.encode(t[0]); + const res = hessian.decode(buf, true); if (res) { assert.deepEqual(res, t[1] || t[0]); } else { /* jshint eqeqeq: false */ - assert(res == t[0]); + assert.deepEqual(res, t[0]); } }); }); - it('should decode error', function () { - var buf = new Buffer([0x50, 0x11]); + it('should decode error', function() { + const buf = Buffer.from([ 0x50, 0x11 ]); assert.throws(function() { hessian.decode(buf); }, null, 'hessian read got an unexpect code: 0x50'); diff --git a/test/v2.decode.cache.test.js b/test/v2.decode.cache.test.js index 2be8599..fd5446f 100644 --- a/test/v2.decode.cache.test.js +++ b/test/v2.decode.cache.test.js @@ -1,19 +1,18 @@ 'use strict'; -var assert = require('assert'); -var hessian = require('../'); -var utils = require('./utils'); -var supportES6Map = require('../lib/utils').supportES6Map; +const hessian = require('../'); +const utils = require('./utils'); +const assert = require('assert'); -describe('v2.decode.cache.test.js', function () { - var decode = hessian.decode; +describe('v2.decode.cache.test.js', function() { + const decode = hessian.decode; // ensure all v2 decode use classCache [ - ['enableCompile=true', true], - ['enableCompile=false', false], - ].forEach(function (item) { - describe(item[0], function () { - var cache = new Map(); + [ 'enableCompile=true', true ], + [ 'enableCompile=false', false ], + ].forEach(function(item) { + describe(item[0], function() { + const cache = new Map(); before(function() { cache.enableCompile = item[1]; hessian.decode = function(buf, version, options) { @@ -41,59 +40,59 @@ describe('v2.decode.cache.test.js', function () { cache.clear(); }); - for (var i = 0; i < 3; i++) { + for (let i = 0; i < 3; i++) { describe('repeat ' + i, function() { it('should skip function', function() { - var o = { foo: 'bar', fn: function() {} }; - var buf = hessian.encode(o, '2.0'); - var output = hessian.decode(buf, '2.0'); + const o = { foo: 'bar', fn() {} }; + const buf = hessian.encode(o, '2.0'); + const output = hessian.decode(buf, '2.0'); assert.deepEqual(output, { foo: 'bar', fn: null }); }); - it('should decode and encode ConnectionRequest', function () { - var javabuf = utils.bytes('v2/object/ConnectionRequest'); - var connreq1 = hessian.decode(javabuf, '2.0'); + it('should decode and encode ConnectionRequest', function() { + const javabuf = utils.bytes('v2/object/ConnectionRequest'); + const connreq1 = hessian.decode(javabuf, '2.0'); assert(connreq1.ctx); assert(connreq1.ctx.id === 101); - var connreq = hessian.decode(javabuf, '2.0', true); - var jsconnreq = { + const connreq = hessian.decode(javabuf, '2.0', true); + const jsconnreq = { $class: 'hessian.ConnectionRequest', $: { ctx: { $class: 'hessian.ConnectionRequest$RequestContext', $: { id: 101, - } - } - } + }, + }, + }, }; assert.deepEqual(connreq, jsconnreq); - var jsbuf = hessian.encode(connreq, '2.0'); - var jsbuf2 = hessian.encode(jsconnreq, '2.0'); + hessian.encode(connreq, '2.0'); + hessian.encode(jsconnreq, '2.0'); }); - it('should read hessian 1.0 enum Color', function () { + it('should read hessian 1.0 enum Color', function() { assert.deepEqual(hessian.decode(utils.bytes('v1/enum/red'), '2.0', true), { $class: 'hessian.Main$Color', $: { - name: 'RED' - } + name: 'RED', + }, }); assert.deepEqual(hessian.decode(utils.bytes('v1/enum/green'), '2.0', false), { - name: 'GREEN' + name: 'GREEN', }); assert.deepEqual(hessian.decode(utils.bytes('v1/enum/blue'), '2.0', true), { $class: 'hessian.Main$Color', $: { - name: 'BLUE' - } + name: 'BLUE', + }, }); }); - it('should read enum Color', function () { + it('should read enum Color', function() { // enum Color { // RED, // GREEN, @@ -103,321 +102,353 @@ describe('v2.decode.cache.test.js', function () { // enum format: // O type 1 "name" o ref name-value assert.deepEqual(hessian.decode(utils.bytes('v2/enum/red'), '2.0'), { - name: 'RED' + name: 'RED', }); assert.deepEqual(hessian.decode(utils.bytes('v2/enum/green'), '2.0', true), { $class: 'hessian.Main$Color', $: { - name: 'GREEN' - } + name: 'GREEN', + }, }); assert.deepEqual(hessian.decode(utils.bytes('v2/enum/blue'), '2.0'), { - name: 'BLUE' + name: 'BLUE', }); assert.deepEqual(hessian.decode(utils.bytes('v2/enum/green'), '2.0'), { - name: 'GREEN' + name: 'GREEN', }); assert.deepEqual(hessian.decode(utils.bytes('v2/enum/red'), '2.0', true), { $class: 'hessian.Main$Color', $: { - name: 'RED' - } + name: 'RED', + }, }); assert.deepEqual( hessian.decode(utils.bytes('v2/enum/lists'), '2.0'), - [ { name: 'BLUE' }, { name: 'RED' }, { name: 'GREEN' } ] + [{ name: 'BLUE' }, { name: 'RED' }, { name: 'GREEN' }] ); assert.deepEqual(hessian.decode(utils.bytes('v2/enum/lists'), '2.0', true), [ - { '$class': 'hessian.Main$Color', '$': { name: 'BLUE' } }, - { '$class': 'hessian.Main$Color', '$': { name: 'RED' } }, - { '$class': 'hessian.Main$Color', '$': { name: 'GREEN' } } + { $class: 'hessian.Main$Color', $: { name: 'BLUE' } }, + { $class: 'hessian.Main$Color', $: { name: 'RED' } }, + { $class: 'hessian.Main$Color', $: { name: 'GREEN' } }, ]); }); - it('should write "{$class: "hessian.test.demo.Car", $: {a: 1, b: "map"}}"', function () { - var obj = { + it('should write "{$class: "hessian.test.demo.Car", $: {a: 1, b: "map"}}"', function() { + const obj = { $class: 'hessian.test.demo.Car', - $: {a: 1, b: 'map'} + $: { a: 1, b: 'map' }, }; - var buf = hessian.encode(obj, '2.0'); + const buf = hessian.encode(obj, '2.0'); assert(buf[0] === 0x4f); assert.deepEqual(hessian.decode(buf, '2.0'), obj.$); assert.deepEqual(hessian.decode(buf, '2.0', true), obj); }); - it('should write "{$class: "hessian.test.demo.Car", $: {a: 1, b: "map", c: 2}}"', function () { - var obj = { + it('should write "{$class: "hessian.test.demo.Car", $: {a: 1, b: "map", c: 2}}"', function() { + const obj = { $class: 'hessian.test.demo.Car', - $: {a: 1, b: 'map', c: 2} + $: { a: 1, b: 'map', c: 2 }, }; - var buf = hessian.encode(obj, '2.0'); + const buf = hessian.encode(obj, '2.0'); assert(buf[0] === 0x4f); assert.deepEqual(hessian.decode(buf, '2.0'), obj.$); assert.deepEqual(hessian.decode(buf, '2.0', true), obj); }); - it('should read one car list', function () { - assert.deepEqual(hessian.decode(utils.bytes('v2/map/one_car_list'), '2.0'), [ - { a: 'a', + it('should read one car list', function() { + assert.deepEqual(hessian.decode(utils.bytes('v2/map/one_car_list'), '2.0'), [{ + a: 'a', + c: 'c', + b: 'b', + model: 'model 1', + color: 'aquamarine', + mileage: 65536, + }]); + + const cars = hessian.decode(utils.bytes('v2/map/one_car_list'), '2.0', true); + assert.deepEqual(cars, [{ + $class: 'hessian.demo.Car', + $: { + a: 'a', c: 'c', b: 'b', model: 'model 1', color: 'aquamarine', - mileage: 65536 } - ]); - - var cars = hessian.decode(utils.bytes('v2/map/one_car_list'), '2.0', true); - assert.deepEqual(cars, [ - { - $class: 'hessian.demo.Car', - $: { - a: 'a', - c: 'c', - b: 'b', - model: 'model 1', - color: 'aquamarine', - mileage: 65536 } - } - ]); + mileage: 65536, + }, + }]); assert.deepEqual(hessian.encode(cars, '2.0'), utils.bytes('v2/map/one_car_list')); }); - it('should read two car list', function () { - assert.deepEqual(hessian.decode(utils.bytes('v2/map/two_car_list'), '2.0'), [ - { a: 'a', + it('should read two car list', function() { + assert.deepEqual(hessian.decode(utils.bytes('v2/map/two_car_list'), '2.0'), [{ + a: 'a', + c: 'c', + b: 'b', + model: 'model 1', + color: 'aquamarine', + mileage: 65536, + }, + { + a: 'a', + c: 'c', + b: 'b', + model: 'model 2', + color: 'aquamarine', + mileage: 65536, + }, + ]); + + const cars = hessian.decode(utils.bytes('v2/map/two_car_list'), '2.0', true); + assert.deepEqual(cars, [{ + $class: 'hessian.demo.Car', + $: { + a: 'a', c: 'c', b: 'b', model: 'model 1', color: 'aquamarine', - mileage: 65536 }, - { a: 'a', + mileage: 65536, + }, + }, + { + $class: 'hessian.demo.Car', + $: { + a: 'a', c: 'c', b: 'b', model: 'model 2', color: 'aquamarine', - mileage: 65536 } - ]); - - var cars = hessian.decode(utils.bytes('v2/map/two_car_list'), '2.0', true); - assert.deepEqual(cars, [ - { - $class: 'hessian.demo.Car', - $: { - a: 'a', - c: 'c', - b: 'b', - model: 'model 1', - color: 'aquamarine', - mileage: 65536 } + mileage: 65536, }, - { - $class: 'hessian.demo.Car', - $: { - a: 'a', - c: 'c', - b: 'b', - model: 'model 2', - color: 'aquamarine', - mileage: 65536 } - } + }, ]); - var buf = hessian.encode(cars, '2.0'); + const buf = hessian.encode(cars, '2.0'); assert(buf.length === utils.bytes('v2/map/two_car_list').length); assert.deepEqual(buf, utils.bytes('v2/map/two_car_list')); }); - it('should read many cars', function () { + it('should read many cars', function() { // list = new ArrayList(); // list.add(new Car("model 1")); // list.add(new Car("model 2")); // list.add(new Car("model 3")); - assert.deepEqual(hessian.decode(utils.bytes('v2/map/car_list'), '2.0'), [ - { a: 'a', + assert.deepEqual(hessian.decode(utils.bytes('v2/map/car_list'), '2.0'), [{ + a: 'a', + c: 'c', + b: 'b', + model: 'model 1', + color: 'aquamarine', + mileage: 65536, + }, + { + a: 'a', + c: 'c', + b: 'b', + model: 'model 2', + color: 'aquamarine', + mileage: 65536, + }, + { + a: 'a', + c: 'c', + b: 'b', + model: 'model 3', + color: 'aquamarine', + mileage: 65536, + }, + ]); + + const cars = hessian.decode(utils.bytes('v2/map/car_list'), '2.0', true); + assert.deepEqual(cars, [{ + $class: 'hessian.demo.Car', + $: { + a: 'a', c: 'c', b: 'b', model: 'model 1', color: 'aquamarine', - mileage: 65536 }, - { a: 'a', + mileage: 65536, + }, + }, + { + $class: 'hessian.demo.Car', + $: { + a: 'a', c: 'c', b: 'b', model: 'model 2', color: 'aquamarine', - mileage: 65536 }, - { a: 'a', + mileage: 65536, + }, + }, + { + $class: 'hessian.demo.Car', + $: { + a: 'a', c: 'c', b: 'b', model: 'model 3', color: 'aquamarine', - mileage: 65536 } - ]); - - var cars = hessian.decode(utils.bytes('v2/map/car_list'), '2.0', true); - assert.deepEqual(cars, [ - { '$class': 'hessian.demo.Car', - '$': - { a: 'a', - c: 'c', - b: 'b', - model: 'model 1', - color: 'aquamarine', - mileage: 65536 } }, - { '$class': 'hessian.demo.Car', - '$': - { a: 'a', - c: 'c', - b: 'b', - model: 'model 2', - color: 'aquamarine', - mileage: 65536 } }, - { '$class': 'hessian.demo.Car', - '$': - { a: 'a', - c: 'c', - b: 'b', - model: 'model 3', - color: 'aquamarine', - mileage: 65536 } } + mileage: 65536, + }, + }, ]); assert.deepEqual(hessian.encode(cars, '2.0'), utils.bytes('v2/map/car_list')); }); - it('should read hessian 1.0 one car list', function () { - assert.deepEqual(hessian.decode(utils.bytes('v1/map/one_car_list'), '2.0'), [ - { a: 'a', + it('should read hessian 1.0 one car list', function() { + assert.deepEqual(hessian.decode(utils.bytes('v1/map/one_car_list'), '2.0'), [{ + a: 'a', + c: 'c', + b: 'b', + model: 'model 1', + color: 'aquamarine', + mileage: 65536, + }]); + + const cars = hessian.decode(utils.bytes('v1/map/one_car_list'), '2.0', true); + assert.deepEqual(cars, [{ + $class: 'hessian.demo.Car', + $: { + a: 'a', c: 'c', b: 'b', model: 'model 1', color: 'aquamarine', - mileage: 65536 } - ]); + mileage: 65536, + }, + }]); + }); - var cars = hessian.decode(utils.bytes('v1/map/one_car_list'), '2.0', true); - assert.deepEqual(cars, [ - { - $class: 'hessian.demo.Car', - $: { - a: 'a', - c: 'c', - b: 'b', - model: 'model 1', - color: 'aquamarine', - mileage: 65536 } - } + it('should read hessian 1.0 two car list', function() { + assert.deepEqual(hessian.decode(utils.bytes('v1/map/two_car_list'), '2.0'), [{ + a: 'a', + c: 'c', + b: 'b', + model: 'model 1', + color: 'aquamarine', + mileage: 65536, + }, + { + a: 'a', + c: 'c', + b: 'b', + model: 'model 2', + color: 'aquamarine', + mileage: 65536, + }, ]); - }); - it('should read hessian 1.0 two car list', function () { - assert.deepEqual(hessian.decode(utils.bytes('v1/map/two_car_list'), '2.0'), [ - { a: 'a', + const cars = hessian.decode(utils.bytes('v1/map/two_car_list'), '2.0', true); + assert.deepEqual(cars, [{ + $class: 'hessian.demo.Car', + $: { + a: 'a', c: 'c', b: 'b', model: 'model 1', color: 'aquamarine', - mileage: 65536 }, - { a: 'a', + mileage: 65536, + }, + }, + { + $class: 'hessian.demo.Car', + $: { + a: 'a', c: 'c', b: 'b', model: 'model 2', color: 'aquamarine', - mileage: 65536 } - ]); - - var cars = hessian.decode(utils.bytes('v1/map/two_car_list'), '2.0', true); - assert.deepEqual(cars, [ - { - $class: 'hessian.demo.Car', - $: { - a: 'a', - c: 'c', - b: 'b', - model: 'model 1', - color: 'aquamarine', - mileage: 65536 } + mileage: 65536, }, - { - $class: 'hessian.demo.Car', - $: { - a: 'a', - c: 'c', - b: 'b', - model: 'model 2', - color: 'aquamarine', - mileage: 65536 } - } + }, ]); }); - it('should read hessian 1.0 many cars', function () { + it('should read hessian 1.0 many cars', function() { // list = new ArrayList(); // list.add(new Car("model 1")); // list.add(new Car("model 2")); // list.add(new Car("model 3")); - assert.deepEqual(hessian.decode(utils.bytes('v1/map/car_list'), '2.0'), [ - { a: 'a', + assert.deepEqual(hessian.decode(utils.bytes('v1/map/car_list'), '2.0'), [{ + a: 'a', + c: 'c', + b: 'b', + model: 'model 1', + color: 'aquamarine', + mileage: 65536, + }, + { + a: 'a', + c: 'c', + b: 'b', + model: 'model 2', + color: 'aquamarine', + mileage: 65536, + }, + { + a: 'a', + c: 'c', + b: 'b', + model: 'model 3', + color: 'aquamarine', + mileage: 65536, + }, + ]); + + const cars = hessian.decode(utils.bytes('v1/map/car_list'), '2.0', true); + assert.deepEqual(cars, [{ + $class: 'hessian.demo.Car', + $: { + a: 'a', c: 'c', b: 'b', model: 'model 1', color: 'aquamarine', - mileage: 65536 }, - { a: 'a', + mileage: 65536, + }, + }, + { + $class: 'hessian.demo.Car', + $: { + a: 'a', c: 'c', b: 'b', model: 'model 2', color: 'aquamarine', - mileage: 65536 }, - { a: 'a', + mileage: 65536, + }, + }, + { + $class: 'hessian.demo.Car', + $: { + a: 'a', c: 'c', b: 'b', model: 'model 3', color: 'aquamarine', - mileage: 65536 } - ]); - - var cars = hessian.decode(utils.bytes('v1/map/car_list'), '2.0', true); - assert.deepEqual(cars, [ - { '$class': 'hessian.demo.Car', - '$': - { a: 'a', - c: 'c', - b: 'b', - model: 'model 1', - color: 'aquamarine', - mileage: 65536 } }, - { '$class': 'hessian.demo.Car', - '$': - { a: 'a', - c: 'c', - b: 'b', - model: 'model 2', - color: 'aquamarine', - mileage: 65536 } }, - { '$class': 'hessian.demo.Car', - '$': - { a: 'a', - c: 'c', - b: 'b', - model: 'model 3', - color: 'aquamarine', - mileage: 65536 } } + mileage: 65536, + }, + }, ]); }); - describe('java.util.concurrent.atomic.AtomicLong', function () { - it('should read and write', function () { - var javabuf = utils.bytes('v2/object/AtomicLong0'); - var a0 = hessian.decode(javabuf, '2.0'); - assert.deepEqual(a0, {value: 0}); - var a0 = hessian.decode(javabuf, '2.0', true); + describe('java.util.concurrent.atomic.AtomicLong', function() { + it('should read and write', function() { + let javabuf = utils.bytes('v2/object/AtomicLong0'); + let a0 = hessian.decode(javabuf, '2.0'); + assert.deepEqual(a0, { value: 0 }); + a0 = hessian.decode(javabuf, '2.0', true); assert.deepEqual(a0, { $class: 'java.util.concurrent.atomic.AtomicLong', $: { @@ -425,22 +456,22 @@ describe('v2.decode.cache.test.js', function () { $class: 'long', $: 0, }, - } + }, }); assert.deepEqual(hessian.encode({ $class: 'java.util.concurrent.atomic.AtomicLong', $: { value: { $class: 'long', - $: 0 - } - } + $: 0, + }, + }, }, '2.0'), javabuf); javabuf = utils.bytes('v2/object/AtomicLong1'); - var a1 = hessian.decode(javabuf, '2.0'); - assert.deepEqual(a1, {value: 1}); - var a1 = hessian.decode(javabuf, '2.0', true); + let a1 = hessian.decode(javabuf, '2.0'); + assert.deepEqual(a1, { value: 1 }); + a1 = hessian.decode(javabuf, '2.0', true); assert.deepEqual(a1, { $class: 'java.util.concurrent.atomic.AtomicLong', $: { @@ -448,39 +479,38 @@ describe('v2.decode.cache.test.js', function () { $class: 'long', $: 1, }, - } + }, }); assert.deepEqual(hessian.encode({ $class: 'java.util.concurrent.atomic.AtomicLong', $: { value: { $class: 'long', - $: 1 - } - } + $: 1, + }, + }, }, '2.0'), javabuf); }); }); - if (supportES6Map) { - describe('map key object', function() { - it('enum should use name v2', function() { - var key = { - $class: 'com.hessian.enums.TestEnum', - $: { - name: 'KEY', - }, - }; - var obj = new Map(); - obj.set(key, 'hello'); - var buf = hessian.encode(obj, '2.0'); - var rs = hessian.decode(buf, '2.0'); - rs.should.eql({ 'KEY': 'hello' }); - }); - + describe('map key object', function() { + it('enum should use name v2', function() { + const key = { + $class: 'com.hessian.enums.TestEnum', + $: { + name: 'KEY', + }, + }; + const obj = new Map(); + obj.set(key, 'hello'); + const buf = hessian.encode(obj, '2.0'); + const rs = hessian.decode(buf, '2.0'); + assert.deepEqual(rs, { KEY: 'hello' }); }); - } + + }); + }); } }); diff --git a/test/v2.list.encode.test.js b/test/v2.list.encode.test.js index 6105d86..9570257 100644 --- a/test/v2.list.encode.test.js +++ b/test/v2.list.encode.test.js @@ -1,206 +1,202 @@ 'use strict'; -var fs = require('fs'); -var path = require('path'); -var should = require('should'); -var hessian = require('../'); -var utils = require('../lib/utils'); -var java = require('js-to-java'); +const hessian = require('../'); +const assert = require('assert'); +const java = require('js-to-java'); -describe('hessian v2', function () { - it('v2 list encode should ok', function () { - var arg = { - "$class": "com.x.Site", - "$": { - "id": { - "$class": "long", - "$": 1637 +describe('hessian v2', function() { + it('v2 list encode should ok', function() { + const arg = { + $class: 'com.x.Site', + $: { + id: { + $class: 'long', + $: 1637, }, - "groupId": { - "$class": "long", - "$": 2 + groupId: { + $class: 'long', + $: 2, }, - "description": { - "$class": "java.lang.String", - "$": "" + description: { + $class: 'java.lang.String', + $: '', }, - "name": { - "$class": "java.lang.String", - "$": "acltimeout1" + name: { + $class: 'java.lang.String', + $: 'acltimeout1', }, - "url": { - "$class": "java.lang.String", - "$": "https://pages.x.com" + url: { + $class: 'java.lang.String', + $: 'https://pages.x.com', }, - "spma": { - "$class": "java.lang.String", - "$": "a2279" + spma: { + $class: 'java.lang.String', + $: 'a2279', }, - "extendFields": { - "$class": "java.util.List", - "$": [ - { - "$class": "com.x.ExtendField", - "$": { - "fieldKey": { - "$class": "java.lang.String", - "$": "site-tmc" - }, - "bizId": { - "$class": "long", - "$": 1637 - }, - "fieldValue": { - "$class": "java.lang.String", - "$": "1" - }, - "id": { - "$class": "long", - "$": 0 - }, - "type": { - "$class": "int", - "$": 0 - }, - "status": { - "$class": "int", - "$": 0 - }, - "metaId": { - "$class": "long", - "$": 0 - }, - "valueType": null, - "gmtCreate": null, - "gmtModified": null - } + extendFields: { + $class: 'java.util.List', + $: [{ + $class: 'com.x.ExtendField', + $: { + fieldKey: { + $class: 'java.lang.String', + $: 'site-tmc', + }, + bizId: { + $class: 'long', + $: 1637, + }, + fieldValue: { + $class: 'java.lang.String', + $: '1', + }, + id: { + $class: 'long', + $: 0, + }, + type: { + $class: 'int', + $: 0, + }, + status: { + $class: 'int', + $: 0, + }, + metaId: { + $class: 'long', + $: 0, + }, + valueType: null, + gmtCreate: null, + gmtModified: null, }, - { - "$class": "com.x.ExtendField", - "$": { - "fieldKey": { - "$class": "java.lang.String", - "$": "site-solution" - }, - "bizId": { - "$class": "long", - "$": 1637 - }, - "fieldValue": { - "$class": "java.lang.String", - "$": "base" - }, - "id": { - "$class": "long", - "$": 0 - }, - "type": { - "$class": "int", - "$": 0 - }, - "status": { - "$class": "int", - "$": 0 - }, - "metaId": { - "$class": "long", - "$": 0 - }, - "valueType": null, - "gmtCreate": null, - "gmtModified": null - } + }, + { + $class: 'com.x.ExtendField', + $: { + fieldKey: { + $class: 'java.lang.String', + $: 'site-solution', + }, + bizId: { + $class: 'long', + $: 1637, + }, + fieldValue: { + $class: 'java.lang.String', + $: 'base', + }, + id: { + $class: 'long', + $: 0, + }, + type: { + $class: 'int', + $: 0, + }, + status: { + $class: 'int', + $: 0, + }, + metaId: { + $class: 'long', + $: 0, + }, + valueType: null, + gmtCreate: null, + gmtModified: null, }, - { - "$class": "com.x.ExtendField", - "$": { - "bizId": { - "$class": "long", - "$": 1637 - }, - "fieldKey": { - "$class": "java.lang.String", - "$": "site-template-whitelist" - }, - "fieldValue": { - "$class": "java.lang.String", - "$": "julu" - }, - "id": { - "$class": "long", - "$": 0 - }, - "type": { - "$class": "int", - "$": 0 - }, - "status": { - "$class": "int", - "$": 0 - }, - "metaId": { - "$class": "long", - "$": 0 - }, - "valueType": null, - "gmtCreate": null, - "gmtModified": null - } + }, + { + $class: 'com.x.ExtendField', + $: { + bizId: { + $class: 'long', + $: 1637, + }, + fieldKey: { + $class: 'java.lang.String', + $: 'site-template-whitelist', + }, + fieldValue: { + $class: 'java.lang.String', + $: 'julu', + }, + id: { + $class: 'long', + $: 0, + }, + type: { + $class: 'int', + $: 0, + }, + status: { + $class: 'int', + $: 0, + }, + metaId: { + $class: 'long', + $: 0, + }, + valueType: null, + gmtCreate: null, + gmtModified: null, }, - { - "$class": "com.x.ExtendField", - "$": { - "bizId": { - "$class": "long", - "$": 1637 - }, - "fieldKey": { - "$class": "java.lang.String", - "$": "site-owner" - }, - "fieldValue": { - "$class": "java.lang.String", - "$": "julu" - }, - "id": { - "$class": "long", - "$": 0 - }, - "type": { - "$class": "int", - "$": 0 - }, - "status": { - "$class": "int", - "$": 0 - }, - "metaId": { - "$class": "long", - "$": 0 - }, - "valueType": null, - "gmtCreate": null, - "gmtModified": null - } - } - ] + }, + { + $class: 'com.x.ExtendField', + $: { + bizId: { + $class: 'long', + $: 1637, + }, + fieldKey: { + $class: 'java.lang.String', + $: 'site-owner', + }, + fieldValue: { + $class: 'java.lang.String', + $: 'julu', + }, + id: { + $class: 'long', + $: 0, + }, + type: { + $class: 'int', + $: 0, + }, + status: { + $class: 'int', + $: 0, + }, + metaId: { + $class: 'long', + $: 0, + }, + valueType: null, + gmtCreate: null, + gmtModified: null, + }, + }, + ], }, - "engName": null, - "ownerWorkIds": null, - "status": { - "$class": "int", - "$": 0 + engName: null, + ownerWorkIds: null, + status: { + $class: 'int', + $: 0, }, - "creator": null, - "mobileUrl": null, - "groupName": null, - "gmtCreate": null, - "gmtModified": null - } + creator: null, + mobileUrl: null, + groupName: null, + gmtCreate: null, + gmtModified: null, + }, }; - var bytes = hessian.encode(arg, '2.0'); - var rs = hessian.decode(bytes, '2.0'); - java.revert(arg).should.eql(rs); + const bytes = hessian.encode(arg, '2.0'); + const rs = hessian.decode(bytes, '2.0'); + assert.deepEqual(java.revert(arg), rs); }); });