hybrid-id-generator
Version:
A powerful hybrid ID generator that combines timestamps, machine IDs, random bits, and sequence numbers to create globally unique identifiers. Features collision prevention, Base62 encoding, and optional ID expiry tracking, ideal for distributed systems a
85 lines (84 loc) • 3.77 kB
TypeScript
/**
* Generates random bits with an option to use either cryptographic or non-cryptographic random number generation.
* When useCrypto is true: uses globalThis.crypto.getRandomValues (browser/Node 19+) or Node crypto.randomBytes.
*
* @param {number} randomBits - The number of random bits to generate.
* @param {boolean} useCrypto - Whether to use cryptographic random number generation (true) or non-cryptographic (false).
* @returns {number} A random number within the range defined by the specified number of bits.
*/
export declare function generateRandomBits(randomBits: number, useCrypto: boolean): number;
/**
* Obfuscates a timestamp using SHA-256 hashing (Node.js crypto only).
* The first 16 characters of the hash are used to create a bigint representation.
*
* @param {bigint} timestamp - The timestamp in bigint format to be obfuscated.
* @returns {bigint} A bigint representation of the obfuscated timestamp.
* @throws {Error} When run outside Node.js (requires Node crypto module).
*/
export declare function obfuscateTimestamp(timestamp: bigint): bigint;
/**
* Validates and returns a machine ID for explicit numeric use.
* Used when no strategy is set, or when strategy is 'random' (number required).
* For 'env' and 'network' strategies, HybridIDGenerator uses MachineIDProviderFactory instead.
*
* @param {string | undefined} machineIdStrategy - The strategy ('random' or undefined).
* @param {number | string | undefined} machineId - The machine ID to validate (number when strategy is 'random').
* @param {number} maxMachineId - The maximum valid value for the machine ID.
* @returns {number} A validated machine ID.
* @throws {Error} If the machine ID is invalid based on the strategy.
*/
export declare function validateMachineId(machineIdStrategy: string | undefined, machineId: number | string | undefined, maxMachineId: number): number;
/**
* Encodes a bigint or string input to a Base62 string.
*
* @param {bigint | string} input - The bigint or string to encode.
* @returns {string} The Base62 encoded string.
*/
export declare function encodeBase62(input: bigint | string): string;
/**
* Decodes a Base62 encoded string to a bigint.
*
* @param {string} encoded - The Base62 encoded string to decode.
* @returns {bigint} The decoded bigint.
*/
export declare function decodeBase62(encoded: string): bigint;
/**
* Encodes a bigint or string input to a Base32 string.
*
* @param {bigint | string} input - The bigint or string to encode.
* @returns {string} The Base32 encoded string.
*/
export declare function encodeBase32(input: bigint | string): string;
/**
* Decodes a Base32 encoded string to a bigint.
*
* @param {string} encoded - The Base32 encoded string to decode.
* @returns {bigint} The decoded bigint.
*/
export declare function decodeBase32(encoded: string): bigint;
/**
* Decodes a Base64 encoded string to a bigint.
*
* @param {string} encoded - The Base64 encoded string to decode.
* @returns {bigint} The decoded bigint.
*/
export declare function decodeBase64(encoded: string): bigint;
/**
* Encodes a bigint or string input to a Base64 string.
*
* @param {bigint | string} input - The bigint or string to encode.
* @returns {string} The Base64 encoded string.
*/
export declare function encodeBase64(input: bigint | string): string;
declare const _default: {
generateRandomBits: typeof generateRandomBits;
obfuscateTimestamp: typeof obfuscateTimestamp;
validateMachineId: typeof validateMachineId;
encodeBase62: typeof encodeBase62;
decodeBase62: typeof decodeBase62;
encodeBase32: typeof encodeBase32;
decodeBase32: typeof decodeBase32;
decodeBase64: typeof decodeBase64;
encodeBase64: typeof encodeBase64;
};
export default _default;