Skip to content

Commit

Permalink
Remove compatibility legacy code
Browse files Browse the repository at this point in the history
  • Loading branch information
kibertoad committed Jan 21, 2024
1 parent 1fca538 commit 8b4281a
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 28 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": "~13.1.11"
},
"optionalDependencies": {},
"engines": {
"node": "*"
"node": ">=8.0.0"
},
"license": "MIT",
"files": [
Expand Down
4 changes: 2 additions & 2 deletions tests/crc.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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');
Expand Down

0 comments on commit 8b4281a

Please sign in to comment.