diff --git a/.autod.conf.js b/.autod.conf.js new file mode 100644 index 0000000..c8e74b3 --- /dev/null +++ b/.autod.conf.js @@ -0,0 +1,27 @@ +'use strict'; + +module.exports = { + write: true, + prefix: '^', + test: [ + 'test', + ], + devdep: [ + 'egg-ci', + 'egg-bin', + 'mm', + 'autod', + 'eslint', + 'eslint-config-egg', + ], + exclude: [ + './test/fixtures', + 'benchmark', + ], + semver: [ + 'debug@3', + 'egg-bin@1', + 'eslint@4', + 'eslint-config-egg@6', + ], +} diff --git a/.gitignore b/.gitignore index 54b5fc1..9b849eb 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ run !.eslintrc !.gitignore !.travis.yml +!.autod.conf.js diff --git a/.travis.yml b/.travis.yml index 1651002..faeb0c0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,8 @@ node_js: - '4' - '6' - '8' + - '10' + - '11' install: - npm i npminstall && npminstall script: diff --git a/appveyor.yml b/appveyor.yml index 56f601f..d3108c3 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -3,6 +3,7 @@ environment: - nodejs_version: '4' - nodejs_version: '6' - nodejs_version: '8' + - nodejs_version: '10' install: - ps: Install-Product node $env:nodejs_version diff --git a/benchmark/heap.js b/benchmark/heap.js new file mode 100644 index 0000000..65780c5 --- /dev/null +++ b/benchmark/heap.js @@ -0,0 +1,25 @@ +'use strict'; + +const url = require('url'); +const JSON = require('..'); +const path = require('path'); +const heapdump = require('heapdump'); + +// const address = url.parse('hsf://127.0.0.1:12200?xx=xx', true); + +// const buf = JSON.encode(address); +// console.log(buf.toString('hex')); +const buf = Buffer.from('70726f746f636f6c7c6873663a7c736c61736865737c617574687c686f73747c3132372e302e302e313a31323230307c706f72747c31323230307c686f73746e616d657c3132372e302e302e317c686173687c7365617263687c3f78783d78787c71756572797c78787c78787c706174686e616d657c706174687c3f78783d78787c687265667c6873663a2f2f3132372e302e302e313a31323230303f78783d78785e5e5e5e24307c317c327c2d317c337c2d337c347c357c367c377c387c397c417c2d337c427c437c447c24457c465d7c477c2d337c487c497c4a7c4b5d', 'hex'); +const o = JSON.decode(buf); + +console.log(o); + +process.o = o; +// const file = path.join(__dirname, Date.now() + '.heapsnapshot'); +// console.log(file); +// heapdump.writeSnapshot(file); + +heapdump.writeSnapshot(function(err, filename) { + console.log('dump written to', filename); +}); + diff --git a/lib/decoder.js b/lib/decoder.js index 29b5b8d..4ff8255 100644 --- a/lib/decoder.js +++ b/lib/decoder.js @@ -26,7 +26,11 @@ class JSONDecoder { } _decodeString(str) { - return str.replace(REG_STR_REPLACER, a => DECODER_REPLACER[a]); + // avoid Parent in (sliced string) + // https://github.com/nodejs/help/issues/711 + // https://stackoverflow.com/questions/31712808/how-to-force-javascript-to-deep-copy-a-string + const r = str.replace(REG_STR_REPLACER, a => DECODER_REPLACER[a]); + return (' ' + r).slice(1); } _decodeDate(str) { @@ -81,11 +85,11 @@ class JSONDecoder { const token = this.tokens[this.tokensIndex]; if (token === ']') { debug('--> unpack buffer end, %j', arr); - return new Buffer(arr); + return Buffer.from(arr); } arr.push(this._unpack()); } - return new Buffer(arr); + return Buffer.from(arr); } case '#': // error { diff --git a/lib/encoder.js b/lib/encoder.js index 280af9b..49c8bee 100644 --- a/lib/encoder.js +++ b/lib/encoder.js @@ -40,7 +40,7 @@ class JSONEncoder { packed += `^${this._pack(ast)}`; debug('pack the json => %s', packed); - return new Buffer(packed); + return Buffer.from(packed); } _pack(ast) { diff --git a/package.json b/package.json index a730e43..cd472dd 100644 --- a/package.json +++ b/package.json @@ -7,13 +7,13 @@ "lib" ], "scripts": { - "autod": "autod --check", + "autod": "autod", "pkgfiles": "egg-bin pkgfiles --check", "lint": "eslint --ext .js lib test", "test": "npm run lint && npm run test-local", "test-local": "egg-bin test", "cov": "egg-bin cov", - "ci": "npm run autod && npm run lint && npm run cov" + "ci": "npm run autod -- --check && npm run lint && npm run cov" }, "repository": { "type": "git", @@ -30,24 +30,24 @@ }, "homepage": "https://github.com/node-modules/serialize-json#readme", "dependencies": { - "debug": "^3.0.1", - "is-type-of": "^1.2.0", - "utility": "^1.12.0" + "debug": "^3.2.6", + "is-type-of": "^1.2.1", + "utility": "^1.15.0" }, "devDependencies": { - "autod": "^2.9.0", + "autod": "^3.0.1", "beautify-benchmark": "^0.2.4", "benchmark": "^2.1.4", "egg-bin": "^1.11.1", - "egg-ci": "^1.8.0", - "eslint": "^4.7.2", - "eslint-config-egg": "^5.1.1", - "mm": "^2.2.0" + "egg-ci": "^1.10.0", + "eslint": "^4.19.1", + "eslint-config-egg": "^6.0.0", + "mm": "^2.4.1" }, "engines": { "node": ">= 4.0.0" }, "ci": { - "version": "4, 6, 8" + "version": "4, 6, 8, 10, 11" } } diff --git a/test/index.test.js b/test/index.test.js index 439c0b3..761c6fb 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -56,7 +56,7 @@ describe('test/index.test.js', () => { }); it('should encode & decode buffer', () => { - const buffer = new Buffer('hello world'); + const buffer = Buffer.from('hello world'); const buf = JSON.encode(buffer); assert(is.buffer(buf)); assert.deepEqual(JSON.decode(buf), buffer); @@ -101,7 +101,7 @@ describe('test/index.test.js', () => { f: null, g: undefined, h: new Date(), - i: new Buffer('this is a buffer'), + i: Buffer.from('this is a buffer'), j: new Error('this is a error'), }; let buf = JSON.encode(json);