Skip to content

Commit

Permalink
Merge pull request #27 from kibertoad/feat/drop-deprecated-code
Browse files Browse the repository at this point in the history
feat: Remove compatibility legacy code
  • Loading branch information
brianloveswords authored Jan 21, 2024
2 parents 1fca538 + 0a76020 commit fb140fc
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 40 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
node_modules
/.idea
/.nyc_output
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 6 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -53,32 +51,18 @@ 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)) {
return 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 " +
Expand All @@ -87,7 +71,7 @@ function ensureBuffer(input) {
}

function bufferizeInt(num) {
var tmp = ensureBuffer(4);
const tmp = ensureBuffer(4);
tmp.writeInt32BE(num, 0);
return tmp;
}
Expand All @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
28 changes: 14 additions & 14 deletions tests/crc.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
Expand All @@ -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);
}
Expand All @@ -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 {
Expand Down

0 comments on commit fb140fc

Please sign in to comment.