Buffer CRC32
buffer-crc32 provides a pure JavaScript implementation of the CRC32 algorithm, designed to work seamlessly with binary data and various character encodings by internally casting strings to Buffers. It offers methods to compute the CRC32 checksum and retrieve results as raw Buffer objects, signed integers, or unsigned integers. The library also supports an "append mode," allowing for incremental CRC calculations across multiple data chunks. Currently at version 1.0.0, this package has not seen updates in approximately seven years, suggesting it is stable but effectively abandoned. It requires Node.js version 8.0.0 or higher, potentially posing compatibility challenges for very modern environments that are primarily ESM-based or require newer Node.js features. Its key differentiator is its self-contained, pure JavaScript nature, avoiding native module dependencies.
Common errors
-
TypeError: crc32 is not a function
cause Attempting to import a default export as a named export in an ESM context.fixChange `import { crc32 } from 'buffer-crc32';` to `import crc32 from 'buffer-crc32';`. -
ERR_REQUIRE_ESM (or similar errors related to 'require' in ESM modules)
cause Using `require('buffer-crc32')` in a Node.js project configured with `"type": "module"` in its `package.json`.fixRefactor to use ESM import syntax: `import crc32 from 'buffer-crc32';`. If that causes issues with bundlers or runtime, ensure your build configuration supports CommonJS interoperability or use a dynamic import `import('buffer-crc32').then(m => m.default)`. Alternatively, if possible, set `"type": "commonjs"` in your `package.json`.
Warnings
- breaking The package requires Node.js >= 8.0.0. While it might still run on much newer Node.js versions, it has not been tested or updated for modern environments, including potential breaking changes in Node.js APIs or internal Buffer handling since its last release.
- gotcha The package is effectively abandoned, with its last update approximately seven years ago. This means no new features, bug fixes, or security patches will be provided. Relying on it in new projects carries a maintenance risk.
- gotcha Primarily designed for CommonJS environments. While it ships TypeScript types, direct ESM usage (e.g., in a 'type: module' project) might require specific import syntax or bundler configuration to resolve the default export correctly.
Install
-
npm install buffer-crc32 -
yarn add buffer-crc32 -
pnpm add buffer-crc32
Imports
- crc32 (default export)
import { crc32 } from 'buffer-crc32';import crc32 from 'buffer-crc32';
- crc32 (CommonJS)
const crc32 = require('buffer-crc32');
Quickstart
import crc32 from 'buffer-crc32';
// Works with Buffers
const buf = Buffer.from([0x00, 0x73, 0x75, 0x70, 0x20, 0x62, 0x72, 0x6f, 0x00]);
console.log('Buffer CRC32:', crc32(buf).toString('hex'));
// Convenience methods for signed or unsigned ints
console.log('Signed CRC32:', crc32.signed(buf));
console.log('Unsigned CRC32:', crc32.unsigned(buf));
// Automatically casts strings to buffers, supporting foreign characters
console.log('String CRC32 (自動販売機):', crc32('自動販売機').toString('hex'));
// Append mode for incremental calculation
let partialCrc = crc32('hey');
partialCrc = crc32(' ', partialCrc);
partialCrc = crc32('sup', partialCrc);
partialCrc = crc32(' ', partialCrc);
const finalCrc = crc32('bros', partialCrc);
console.log('Appended CRC32:', finalCrc.toString('hex'));