buffer-crc32
Advanced tools
Comparing version 0.2.10 to 0.2.11
30
index.js
@@ -58,7 +58,29 @@ var Buffer = require('buffer').Buffer; | ||
if (typeof Int32Array !== 'undefined') | ||
if (typeof Int32Array !== 'undefined') { | ||
CRC_TABLE = new Int32Array(CRC_TABLE); | ||
} | ||
function ensureBuffer(input) { | ||
if (Buffer.isBuffer(input)) { | ||
return input; | ||
} | ||
var hasNewBufferAPI = | ||
typeof Buffer.alloc === "function" && | ||
typeof Buffer.from === "function"; | ||
if (typeof input === "number") { | ||
return hasNewBufferAPI ? Buffer.alloc(input) : new Buffer(input); | ||
} | ||
else if (typeof input === "string") { | ||
return hasNewBufferAPI ? Buffer.from(input) : new Buffer(input); | ||
} | ||
else { | ||
throw new Error("input must be buffer, number, or string, received " + | ||
typeof input); | ||
} | ||
} | ||
function bufferizeInt(num) { | ||
var tmp = new Buffer(4); | ||
var tmp = ensureBuffer(4); | ||
tmp.writeInt32BE(num, 0); | ||
@@ -69,5 +91,3 @@ return tmp; | ||
function _crc32(buf, previous) { | ||
if (!Buffer.isBuffer(buf)) { | ||
buf = new Buffer(buf); | ||
} | ||
buf = ensureBuffer(buf); | ||
if (Buffer.isBuffer(previous)) { | ||
@@ -74,0 +94,0 @@ previous = previous.readUInt32BE(0); |
{ | ||
"author": "Brian J. Brennan <brianloveswords@gmail.com> (http://bjb.io)", | ||
"author": "Brian J. Brennan <brianloveswords@gmail.com>", | ||
"name": "buffer-crc32", | ||
"description": "A pure javascript CRC32 algorithm that plays nice with binary data", | ||
"version": "0.2.10", | ||
"version": "0.2.11", | ||
"licenses": [ | ||
@@ -33,5 +33,5 @@ { | ||
"engines": { | ||
"node": "*" | ||
"node": ">=6.0.0" | ||
}, | ||
"license": "MIT" | ||
} |
@@ -5,4 +5,4 @@ var crc32 = require('..'); | ||
test('simple crc32 is no problem', function (t) { | ||
var input = Buffer('hey sup bros'); | ||
var expected = Buffer([0x47, 0xfa, 0x55, 0x70]); | ||
var input = new Buffer('hey sup bros'); | ||
var expected = new Buffer([0x47, 0xfa, 0x55, 0x70]); | ||
t.same(crc32(input), expected); | ||
@@ -13,4 +13,4 @@ t.end(); | ||
test('another simple one', function (t) { | ||
var input = Buffer('IEND'); | ||
var expected = Buffer([0xae, 0x42, 0x60, 0x82]); | ||
var input = new Buffer('IEND'); | ||
var expected = new Buffer([0xae, 0x42, 0x60, 0x82]); | ||
t.same(crc32(input), expected); | ||
@@ -21,4 +21,4 @@ t.end(); | ||
test('slightly more complex', function (t) { | ||
var input = Buffer([0x00, 0x00, 0x00]); | ||
var expected = Buffer([0xff, 0x41, 0xd9, 0x12]); | ||
var input = new Buffer([0x00, 0x00, 0x00]); | ||
var expected = new Buffer([0xff, 0x41, 0xd9, 0x12]); | ||
t.same(crc32(input), expected); | ||
@@ -29,4 +29,4 @@ t.end(); | ||
test('complex crc32 gets calculated like a champ', function (t) { | ||
var input = Buffer('शीर्षक'); | ||
var expected = Buffer([0x17, 0xb8, 0xaf, 0xf1]); | ||
var input = new Buffer('शीर्षक'); | ||
var expected = new Buffer([0x17, 0xb8, 0xaf, 0xf1]); | ||
t.same(crc32(input), expected); | ||
@@ -38,3 +38,3 @@ t.end(); | ||
var input = 'शीर्षक'; | ||
var expected = Buffer([0x17, 0xb8, 0xaf, 0xf1]); | ||
var expected = new Buffer([0x17, 0xb8, 0xaf, 0xf1]); | ||
t.same(crc32(input), expected); | ||
@@ -60,4 +60,4 @@ t.end(); | ||
test('simple crc32 in append mode', function (t) { | ||
var input = [Buffer('hey'), Buffer(' '), Buffer('sup'), Buffer(' '), Buffer('bros')]; | ||
var expected = Buffer([0x47, 0xfa, 0x55, 0x70]); | ||
var input = [new Buffer('hey'), new Buffer(' '), new Buffer('sup'), new Buffer(' '), new Buffer('bros')]; | ||
var expected = new Buffer([0x47, 0xfa, 0x55, 0x70]); | ||
for (var crc = 0, i = 0; i < input.length; i++) { | ||
@@ -85,2 +85,23 @@ crc = crc32(input[i], crc); | ||
test('make sure crc32 can accept integers as first arg ', function (t) { | ||
try { | ||
t.same(crc32(0), new Buffer([0x00, 0x00, 0x00, 0x00])); | ||
} catch (e) { | ||
t.fail("should be able to accept integer"); | ||
} finally { | ||
t.end(); | ||
} | ||
}); | ||
test('make sure crc32 throws on bad input', function (t) { | ||
try { | ||
crc32({}); | ||
t.fail("should fail on garbage input"); | ||
} catch (e) { | ||
t.ok("should pass"); | ||
} finally { | ||
t.end(); | ||
} | ||
}); | ||
test('can do unsigned in append mode', function (t) { | ||
@@ -96,2 +117,1 @@ var input1 = 'bear san'; | ||
}); | ||
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10761
194