diff --git a/.autod.conf.js b/.autod.conf.js index 85abc36..a24dc86 100644 --- a/.autod.conf.js +++ b/.autod.conf.js @@ -7,6 +7,9 @@ module.exports = { exclude: [ 'test/fixtures', ], + dep: [ + 'long', + ], devdep: [ 'autod', 'egg-bin', diff --git a/.travis.yml b/.travis.yml index ce21122..39d409a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,7 @@ language: node_js node_js: - '8' - '10' + - '11' install: - npm i npminstall && npminstall script: diff --git a/appveyor.yml b/appveyor.yml index 981e82b..7cce29a 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -2,6 +2,7 @@ environment: matrix: - nodejs_version: '8' - nodejs_version: '10' + - nodejs_version: '11' install: - ps: Install-Product node $env:nodejs_version diff --git a/benchmark/require.js b/benchmark/require.js new file mode 100644 index 0000000..ee87372 --- /dev/null +++ b/benchmark/require.js @@ -0,0 +1,5 @@ +'use strict'; + +console.time('byte'); +require('..'); +console.timeEnd('byte'); diff --git a/lib/byte.js b/lib/byte.js index 936c83f..590744a 100644 --- a/lib/byte.js +++ b/lib/byte.js @@ -1,16 +1,28 @@ 'use strict'; -const Long = require('long'); -const debug = require('debug')('byte'); +const importLazy = require('import-lazy')(require); +const Long = importLazy('long'); const numbers = require('./number'); -const utility = require('utility'); +const MAX_SAFE_INTEGER_STR = '9007199254740991'; +const MAX_SAFE_INTEGER_STR_LENGTH = 16; // '9007199254740991'.length const DEFAULT_SIZE = 1024; const BIG_ENDIAN = 1; const LITTLE_ENDIAN = 2; const MAX_INT_31 = Math.pow(2, 31); const ONE_HUNDRED_MB = 100 * 1024 * 1024; +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 ByteBuffer(options) { options = options || {}; this._order = options.order || BIG_ENDIAN; @@ -53,10 +65,8 @@ ByteBuffer.prototype._checkSize = function(afterSize) { if (this._size >= afterSize) { return; } - const old = this._size; this._size = afterSize * 2; this._limit = this._size; - debug('allocate new Buffer: from %d to %d bytes', old, this._size); const bytes = Buffer.alloc(this._size); this._bytes.copy(bytes, 0); this._bytes = bytes; @@ -228,7 +238,7 @@ ByteBuffer.prototype.putLong = function(index, value) { let isNumber = typeof value === 'number'; // convert safe number string to number - if (!isNumber && utility.isSafeNumberString(value)) { + if (!isNumber && isSafeNumberString(value)) { isNumber = true; value = Number(value); } diff --git a/package.json b/package.json index 684c156..e0827c1 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ ], "scripts": { "autod": "autod", - "lint": "eslint lib test *.js", + "lint": "eslint lib test --ext .js", "test": "npm run lint && egg-bin test", "test-local": "egg-bin test", "cov": "egg-bin cov", @@ -18,18 +18,17 @@ "optimized": "node --allow-natives-syntax --trace_opt --trace_deopt test/optimized.js" }, "dependencies": { - "debug": "^3.1.0", - "long": "^4.0.0", - "utility": "^1.13.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", - "egg-bin": "^4.7.0", - "egg-ci": "^1.8.0", - "eslint": "^4.19.1", - "eslint-config-egg": "^7.0.0", + "egg-bin": "^4.12.2", + "egg-ci": "^1.11.0", + "eslint": "^5.16.0", + "eslint-config-egg": "^7.3.1", "fastbench": "^1.0.1", "optimized": "^1.2.0" }, @@ -54,7 +53,7 @@ "node": ">= 8.0.0" }, "ci": { - "version": "8, 10" + "version": "8, 10, 11" }, "author": "fengmk2 (http://fengmk2.com)", "license": "MIT" diff --git a/test/byte.test.js b/test/byte.test.js index ff7285a..c26a024 100644 --- a/test/byte.test.js +++ b/test/byte.test.js @@ -19,7 +19,7 @@ describe('byte.test.js', function() { bytes.putString(''); bytes.putString(''); bytes.putString(null); - bytes.putString(new Buffer('')); + bytes.putString(Buffer.from('')); bytes.putString(0, ''); bytes.position(0); @@ -35,10 +35,10 @@ describe('byte.test.js', function() { assert(bytes.getString(0) === 'foo, 中文'); bytes.putString(0, 'foo, 中国'); assert(bytes.getString(0) === 'foo, 中国'); - bytes.putString(0, new Buffer('foo, 中国')); + bytes.putString(0, Buffer.from('foo, 中国')); assert(bytes.getString(0) === 'foo, 中国'); bytes.putString('foo2'); - assert(bytes.getString(new Buffer('foo, 中国').length + 4) === 'foo2'); + assert(bytes.getString(Buffer.from('foo, 中国').length + 4) === 'foo2'); bytes.position(0); assert(bytes.getString() === 'foo, 中国'); @@ -60,7 +60,7 @@ describe('byte.test.js', function() { bytes.putCString('bar123123, \u0000中文\u0000'); bytes.putCString('foo2foo, \u0000中文\u0000'); assert( - bytes.getCString(new Buffer('bar123123, \u0000中文\u0000').length + 4 + 1) === 'foo2foo, \u0000中文\u0000' + bytes.getCString(Buffer.from('bar123123, \u0000中文\u0000').length + 4 + 1) === 'foo2foo, \u0000中文\u0000' ); bytes.position(0); @@ -440,21 +440,21 @@ describe('byte.test.js', function() { assert(bytes.toString() === ''); assert(bytes.get(1) === 1); - bytes.put(new Buffer([ 255, 255, 255 ])); + bytes.put(Buffer.from([ 255, 255, 255 ])); assert(bytes.toString() === ''); - assert.deepEqual(bytes.get(2, 3), new Buffer([ 255, 255, 255 ])); + assert.deepEqual(bytes.get(2, 3), Buffer.from([ 255, 255, 255 ])); - bytes.put(new Buffer([ 255, 254, 1 ]), 1, 2); + bytes.put(Buffer.from([ 255, 254, 1 ]), 1, 2); assert(bytes.toString() === ''); - assert.deepEqual(bytes.get(5, 2), new Buffer([ 254, 1 ])); + assert.deepEqual(bytes.get(5, 2), Buffer.from([ 254, 1 ])); bytes.position(0); - assert.deepEqual(bytes.read(7), new Buffer([ 2, 1, 255, 255, 255, 254, 1 ])); + assert.deepEqual(bytes.read(7), Buffer.from([ 2, 1, 255, 255, 255, 254, 1 ])); assert(bytes.position() === 7); bytes.position(0); bytes.skip(5); - assert.deepEqual(bytes.read(2), new Buffer([ 254, 1 ])); + assert.deepEqual(bytes.read(2), Buffer.from([ 254, 1 ])); assert(bytes.position() === 7); }); }); @@ -668,17 +668,17 @@ describe('byte.test.js', function() { const bytes = ByteBuffer.allocate(8); bytes.putInt(0); bytes.putInt(1); - assert.deepEqual(bytes.copy(4), new Buffer([ 0, 0, 0, 1 ])); + assert.deepEqual(bytes.copy(4), Buffer.from([ 0, 0, 0, 1 ])); }); it('should copy(start, end)', function() { const bytes = ByteBuffer.allocate(9); bytes.putInt(0); bytes.putInt(1); - assert.deepEqual(bytes.copy(0, 8), new Buffer([ 0, 0, 0, 0, 0, 0, 0, 1 ])); - assert.deepEqual(bytes.copy(0, 9), new Buffer([ 0, 0, 0, 0, 0, 0, 0, 1 ])); - assert.deepEqual(bytes.copy(0, 4), new Buffer([ 0, 0, 0, 0 ])); - assert.deepEqual(bytes.copy(4, 8), new Buffer([ 0, 0, 0, 1 ])); + assert.deepEqual(bytes.copy(0, 8), Buffer.from([ 0, 0, 0, 0, 0, 0, 0, 1 ])); + assert.deepEqual(bytes.copy(0, 9), Buffer.from([ 0, 0, 0, 0, 0, 0, 0, 1 ])); + assert.deepEqual(bytes.copy(0, 4), Buffer.from([ 0, 0, 0, 0 ])); + assert.deepEqual(bytes.copy(4, 8), Buffer.from([ 0, 0, 0, 1 ])); }); }); @@ -751,11 +751,11 @@ describe('byte.test.js', function() { const bytes = ByteBuffer.allocate(4); bytes.putInt(1); bytes.flip(); // switch to read mode - const buf = new Buffer(4); + const buf = Buffer.alloc(4); bytes.get(buf); - assert.deepEqual(buf, new Buffer([ 0, 0, 0, 1 ])); + assert.deepEqual(buf, Buffer.from([ 0, 0, 0, 1 ])); - const buf2 = new Buffer(1); + const buf2 = Buffer.alloc(1); assert.throws(function() { bytes.get(buf2); }, null, 'BufferOverflowException'); @@ -766,11 +766,11 @@ describe('byte.test.js', function() { bytes.putInt(1); bytes.putInt(5); bytes.flip(); // switch to read mode - const buf = new Buffer(4); + const buf = Buffer.alloc(4); bytes.get(buf); - assert.deepEqual(buf, new Buffer([ 0, 0, 0, 1 ])); + assert.deepEqual(buf, Buffer.from([ 0, 0, 0, 1 ])); bytes.get(buf); - assert.deepEqual(buf, new Buffer([ 0, 0, 0, 5 ])); + assert.deepEqual(buf, Buffer.from([ 0, 0, 0, 5 ])); }); }); @@ -807,7 +807,7 @@ describe('byte.test.js', function() { it('should fill hello', function() { const str = 'hello'; bytes.putRawString(str); - const buff = new Buffer(10); + const buff = Buffer.alloc(10); initBuff(buff); const copied = bytes.memcpy(buff); @@ -820,7 +820,7 @@ describe('byte.test.js', function() { }); it('should fill hel', function() { - const buff = new Buffer(3); + const buff = Buffer.alloc(3); initBuff(buff); const copied = bytes.memcpy(buff);