diff --git a/.gitignore b/.gitignore index b512c09..9513b53 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -node_modules \ No newline at end of file +node_modules +/.idea +/.nyc_output diff --git a/README.md b/README.md index 0d9d8b8..eca68be 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,8 @@ buffer, signed or unsigned data and has tests. Derived from the sample CRC implementation in the PNG specification: http://www.w3.org/TR/PNG/#D-CRCAppendix +This package requires Node 8+ to work. + # install ``` npm install buffer-crc32 diff --git a/index.js b/index.js index 4434f36..a8b7309 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,4 @@ -var Buffer = require('buffer').Buffer; - -var CRC_TABLE = [ +const CRC_TABLE = new Int32Array([ 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, @@ -53,21 +51,7 @@ var CRC_TABLE = [ 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d -]; - -var hasNewBufferAPI = - typeof Buffer.alloc === "function" && - typeof Buffer.from === "function"; - -if (typeof Int32Array !== 'undefined') { - CRC_TABLE = new Int32Array(CRC_TABLE); -} - -function newEmptyBufferLegacy(length) { - var buffer = new Buffer(length); - buffer.fill(0x00); - return buffer; -} +]) function ensureBuffer(input) { if (Buffer.isBuffer(input)) { @@ -75,10 +59,10 @@ function ensureBuffer(input) { } if (typeof input === "number") { - return hasNewBufferAPI ? Buffer.alloc(input) : newEmptyBufferLegacy(input); + return Buffer.alloc(input); } else if (typeof input === "string") { - return hasNewBufferAPI ? Buffer.from(input) : new Buffer(input); + return Buffer.from(input); } else { throw new Error("input must be buffer, number, or string, received " + @@ -87,7 +71,7 @@ function ensureBuffer(input) { } function bufferizeInt(num) { - var tmp = ensureBuffer(4); + const tmp = ensureBuffer(4); tmp.writeInt32BE(num, 0); return tmp; } @@ -97,7 +81,7 @@ function _crc32(buf, previous) { if (Buffer.isBuffer(previous)) { previous = previous.readUInt32BE(0); } - var crc = ~~previous ^ -1; + let crc = ~~previous ^ -1; for (var n = 0; n < buf.length; n++) { crc = CRC_TABLE[(crc ^ buf[n]) & 0xff] ^ (crc >>> 8); } diff --git a/package.json b/package.json index a714076..f1e7847 100644 --- a/package.json +++ b/package.json @@ -22,16 +22,16 @@ }, "main": "index.js", "scripts": { - "test": "./node_modules/.bin/tap tests/*.test.js" + "test": "tap tests/*.test.js --reporter classic" }, "type": "commonjs", "dependencies": {}, "devDependencies": { - "tap": "~0.2.5" + "tap": "~11.1.5" }, "optionalDependencies": {}, "engines": { - "node": "*" + "node": ">=8.0.0" }, "license": "MIT", "files": [ diff --git a/tests/crc.test.js b/tests/crc.test.js index b6b68f4..048801b 100644 --- a/tests/crc.test.js +++ b/tests/crc.test.js @@ -1,37 +1,37 @@ -var crc32 = require('..'); -var test = require('tap').test; +const crc32 = require('..'); +const test = require('tap').test; test('simple crc32 is no problem', function (t) { - var input = new Buffer('hey sup bros'); - var expected = new Buffer([0x47, 0xfa, 0x55, 0x70]); + var input = Buffer.from('hey sup bros'); + var expected = Buffer.from([0x47, 0xfa, 0x55, 0x70]); t.same(crc32(input), expected); t.end(); }); test('another simple one', function (t) { - var input = new Buffer('IEND'); - var expected = new Buffer([0xae, 0x42, 0x60, 0x82]); + var input = Buffer.from('IEND'); + var expected = Buffer.from([0xae, 0x42, 0x60, 0x82]); t.same(crc32(input), expected); t.end(); }); test('slightly more complex', function (t) { - var input = new Buffer([0x00, 0x00, 0x00]); - var expected = new Buffer([0xff, 0x41, 0xd9, 0x12]); + var input = Buffer.from([0x00, 0x00, 0x00]); + var expected = Buffer.from([0xff, 0x41, 0xd9, 0x12]); t.same(crc32(input), expected); t.end(); }); test('complex crc32 gets calculated like a champ', function (t) { - var input = new Buffer('शीर्षक'); - var expected = new Buffer([0x17, 0xb8, 0xaf, 0xf1]); + var input = Buffer.from('शीर्षक'); + var expected = Buffer.from([0x17, 0xb8, 0xaf, 0xf1]); t.same(crc32(input), expected); t.end(); }); test('casts to buffer if necessary', function (t) { var input = 'शीर्षक'; - var expected = new Buffer([0x17, 0xb8, 0xaf, 0xf1]); + var expected = Buffer.from([0x17, 0xb8, 0xaf, 0xf1]); t.same(crc32(input), expected); t.end(); }); @@ -52,8 +52,8 @@ test('can do unsigned', function (t) { test('simple crc32 in append mode', function (t) { - var input = [new Buffer('hey'), new Buffer(' '), new Buffer('sup'), new Buffer(' '), new Buffer('bros')]; - var expected = new Buffer([0x47, 0xfa, 0x55, 0x70]); + var input = [Buffer.from('hey'), Buffer.from(' '), Buffer.from('sup'), Buffer.from(' '), Buffer.from('bros')]; + var expected = Buffer.from([0x47, 0xfa, 0x55, 0x70]); for (var crc = 0, i = 0; i < input.length; i++) { crc = crc32(input[i], crc); } @@ -78,7 +78,7 @@ test('can do signed in append mode', function (t) { test('make sure crc32 can accept integers as first arg ', function (t) { try { - t.same(crc32(0), new Buffer([0x00, 0x00, 0x00, 0x00])); + t.same(crc32(0), Buffer.from([0x00, 0x00, 0x00, 0x00])); } catch (e) { t.fail("should be able to accept integer"); } finally {