distid
Version:
Distributed ID generator for large-scale systems
26 lines (25 loc) • 846 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.toBase62 = toBase62;
exports.validateNodeId = validateNodeId;
const BASE62_CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
if (typeof BigInt === 'undefined') {
throw new Error('BigInt is not supported in this environment. Please use a modern JavaScript runtime.');
}
function toBase62(num) {
let result = '';
let n = num;
while (n > 0n) {
result = BASE62_CHARS[Number(n % BigInt(62))] + result;
n = n / BigInt(62);
}
return result || '0';
}
function validateNodeId(nodeId, usedIds) {
if (nodeId < 0 || nodeId > 1023) {
throw new Error('Node ID must be between 0 and 1023');
}
if (usedIds.has(nodeId)) {
throw new Error(`Node ID ${nodeId} is already in use`);
}
}