Registry / database / cluster-key-slot

cluster-key-slot

JSON →
library1.1.2jsnpmunverified

The `cluster-key-slot` package provides a high-performance utility for calculating CRC16 hashes, specifically designed for determining key slots in Redis Cluster environments. This functionality is crucial for Node.js Redis clients such as `node_redis`, `ioredis`, and `redis-clustr` to correctly route commands to the appropriate Redis instance. The current stable version is 1.1.2. The package appears to have a stable, low-cadence release schedule, with updates primarily focusing on performance enhancements or supporting new key types like binary keys (introduced in v1.1.0). Its key differentiator lies in its optimized performance compared to older `Buffer`-based CRC calculations, offering significantly faster slot computations for both keys with and without tags, and supporting Redis Cluster key tags (e.g., `somekey{actualTag}`). It ships with TypeScript types, facilitating its use in modern TypeScript projects.

npm install cluster-key-slot
INSTALL
IMPORT
SIG · CLUSTER-KEY-SLOT
C
cluster-key-slot
databasejavascriptv1.1.2
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

calculateSlot
✓ import calculateSlot from 'cluster-key-slot'; // Or for CJS: const calculateSlot = require('cluster-key-slot');
✗ import { calculateSlot } from 'cluster-key-slot'; // Incorrect for CJS: const { calculateSlot } = require('cluster-key-slot');
`calculateSlot` is the default export in ESM and the `module.exports` in CommonJS. Using named import syntax in ESM is also generally supported by TypeScript's `esModuleInterop` for dual CJS/ESM packages, but the explicit default import is preferred for clarity.
generateMulti
✓ import { generateMulti } from 'cluster-key-slot'; // Or for CJS: const { generateMulti } = require('cluster-key-slot');
✗ import generateMulti from 'cluster-key-slot'; // Incorrect CJS: const generateMulti = require('cluster-key-slot').generateMulti;
`generateMulti` is a named export for both ESM and CommonJS. Attempting to import it as a default export will result in `undefined`.
Types
✓ import type { ClusterKeySlotCalculator } from 'cluster-key-slot';
The package ships with TypeScript definitions. Specific types for functions or return values can be imported as needed for stronger type checking.

Demonstrates calculating Redis cluster key slots for single string and buffer keys, and determining a common slot for multiple keys with matching tags, highlighting the `-1` return for non-matching tags.

import calculateSlot, { generateMulti } from 'cluster-key-slot'; import { Buffer } from 'node:buffer'; // Explicit import for Buffer async function runRedisSlotCalculations() { console.log('--- Redis Key Slot Calculations ---'); // Calculate a single slot for a string key const slotString = calculateSlot('test:key:{butOnlyThis}redis'); console.log(`Slot for 'test:key:{butOnlyThis}redis': ${slotString}`); // Calculate a single slot for a Buffer key const bufferKey = Buffer.from([0x7b, 0x7d, 0x2a, 0x68, 0x65, 0x6c, 0x6c, 0x6f]); // Example buffer "{}"*hello const slotBuffer = calculateSlot(bufferKey); console.log(`Slot for Buffer key ${bufferKey.toString('hex')}: ${slotBuffer}`); // Calculate a single slot for multiple keys (requires key tags to match or returns -1) const keysForMultiOp = [ 'test:key:{butOnlyThis}redis', 'something:key45:{butOnlyThis}hello', 'example:key46:{butOnlyThis}foobar' ]; const slotForRedisMulti = generateMulti(keysForMultiOp); if (slotForRedisMulti === -1) { console.log(`Keys for multi-operation (${keysForMultiOp.join(', ')}) do not share a common slot tag.`); } else { console.log(`Common slot for multiple keys: ${slotForRedisMulti}`); } // Example of keys not sharing a common tag const differentTagKeys = [ 'key1{tagA}', 'key2{tagB}' ]; const slotDifferentTags = generateMulti(differentTagKeys); console.log(`Common slot for different tags (${differentTagKeys.join(', ')}): ${slotDifferentTags} (expected -1)`); console.log('\nCalculations complete.'); } runRedisSlotCalculations().catch(console.error);
Debug
Known issues
gotchaThe `generateMulti` function returns `-1` if the provided array of keys does not share a common hash tag or if any key's slot does not match the base slot (derived from the first key). Users should explicitly check for `-1` to handle cases where multi-key operations are not possible.
fix
Always check the return value of `generateMulti` for `-1` before proceeding with multi-key Redis commands.
affects: >=1.0.0
gotchaOlder versions of `cluster-key-slot` (prior to v1.1.0) did not explicitly support binary keys (Buffers). Passing Buffer objects to `calculateSlot` on older versions might lead to incorrect results or errors. Ensure you are on v1.1.0 or newer for reliable Buffer support.
fix
Upgrade to `cluster-key-slot@^1.1.0` or newer to ensure full support for binary keys (Buffers).
affects: <1.1.0
Errors
Common errors & fixes
TypeError: calculateSlot is not a function
Attempting to import `calculateSlot` as a named export in ESM when it's primarily a default export, or destructured in CommonJS.
fix
For ESM: `import calculateSlot from 'cluster-key-slot';`. For CJS: `const calculateSlot = require('cluster-key-slot');`
TypeError: generateMulti is not a function
Attempting to import `generateMulti` as a default export in ESM or accessing it incorrectly in CommonJS.
fix
For ESM: `import { generateMulti } from 'cluster-key-slot';`. For CJS: `const { generateMulti } = require('cluster-key-slot');`
Upgrade
Version history
1.1.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
12 hits · last 30 days
node
8
OpenAI (training)
1
Resources
cluster-key-slot — npm install cluster-key-slot · libregistry