Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,44 @@ function bufferizeInt(num) {
}

function _crc32(buf, previous) {
// Parameter validation and coercion.
buf = ensureBuffer(buf);
if (Buffer.isBuffer(previous)) {
previous = previous.readUInt32BE(0);
}
// Delegate to the implementation.
return _impl(buf, previous);
}

function _jsImpl(buf, previous) {
let crc = ~~previous ^ -1;
for (var n = 0; n < buf.length; n++) {
crc = CRC_TABLE[(crc ^ buf[n]) & 0xff] ^ (crc >>> 8);
}
return crc ^ -1;
}
function _impl(buf, previous) {
// Returns a signed integer.
// After this function is called once, references to it are replaced by a different implementation.

// Determine which implementation to use.
try {
var nodeCrc32 = require('node:zlib').crc32;
} catch (e) {
// Older versions of node don't support the `node:` prefix,
// and also don't have the crc32 implementation anyway.
}
if (typeof nodeCrc32 === 'function') {
// Use node implementation.
_impl = function _nodeImpl(buf, previous) {
// Node returns an unsigned integer. Coerce to signed.
return nodeCrc32(buf, previous) | 0;
};
} else {
// Use JS implementation.
_impl = _jsImpl;
}
}

function crc32() {
return bufferizeInt(_crc32.apply(null, arguments));
Expand Down