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
muslnode 18–226 runs
build_error
glibcnode 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);
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.
fixFor 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.
fixFor ESM: `import { generateMulti } from 'cluster-key-slot';`. For CJS: `const { generateMulti } = require('cluster-key-slot');` Audit
Dependencies
No dependency data recorded yet.