import * as Sf from './Snowflake.js'; import { ESequenceStrategy, ETimeReversedStrategy } from './Constants.js'; export interface ISnowflakeSiOptions extends Pick, Partial> { /** * The strategy for handling time reversal events. * * @default ETimeReversedStrategy.THROW_ERROR */ onTimeReversed?: ETimeReversedStrategy; /** * The strategy for handling the sequence number when the time changes. * * This option also accepts a custom function that takes the current sequence number * and returns the new sequence number. * * @default ESequenceStrategy.RESET */ onTimeChanged?: ESequenceStrategy | Sf.ISnowflakeUpdateSequenceOnTimeChanged; /** * When the sequence strategy is set to `ESequenceStrategy.RESET`, * this option specifies the threshold for resetting the sequence number. * So only when the sequence number exceeds this threshold, the sequence number * will be reset to 0. * * @default 0 */ sequenceResetThreshold?: number; } /** * Minimal epoch when the click bit-width is 40: `2003-03-18T07:20:19.225Z`. */ export declare const MIN_EPOCH_40 = 1047972019225; /** * The default bit width of the machine ID is 5 bits, which means the range of the machine ID is [0, 31]. * * > So the default capacity of snowflake-si ID is 2^7 = 128, per millisecond. */ export declare const DEFAULT_MACHINE_ID_BIT_WIDTH = 5; /** * The default bit width of the clock part is 41 bits. */ export declare const DEFAULT_CLOCK_BIT_WIDTH = 40; /** * The class for generating Snowflake (Safe-Integer) IDs. */ export declare class SnowflakeSiGenerator { /** * The strategy for the time reversal event. */ private readonly _onTimeReversedStrategy; private readonly _onTimeChangedCallback; private readonly _onTimeChangedStrategy; private readonly _onTimeChangedResetThreshold; /** * The identifier of the machine that generates the UUID. * * For the SnowflakeSI ID of a specific resource, each generator must use a unique machine ID * so that the generated IDs will not conflict with each other. * * The range of the machine ID depends on the `machineIdBitWidth`: * * - If `machineIdBitWidth` is 1, the range is [0, 1]. * - If `machineIdBitWidth` is 2, the range is [0, 3]. * - ... * - If `machineIdBitWidth` is 7, the range is [0, 127]. * - If `machineIdBitWidth` is 8, the range is [0, 255]. */ readonly machineId: number; /** * The epoch of the generator. */ readonly epoch: number; /** * The bit width of the machine ID, must be an integer between 1 and 8. * * The sum of `machineIdBitWidth` and `sequenceBitWidth` must be 12. * * @default 5 */ readonly machineIdBitWidth: number; /** * The bit width of the sequence number, must be an integer between 8 and 11. * * The sum of `machineIdBitWidth` and `sequenceBitWidth` must be 12. * * @default 7 */ readonly sequenceBitWidth: number; /** * The bit width of the clock part. * * It must be an integer between 40 and 41. * When 40 is chosen, the epoch must not be earlier than `2003-03-18T07:20:19.225Z` to * keep the clock part within 40 bits (before `2038-01-19T03:14:07Z`). * * @default 40 */ readonly clockBitWidth: number; private readonly _midPart; /** * The maximum sequence of the generator, per millisecond. */ readonly maximumSequence: number; private _prevTime; private _seq; private _countPerMs; private readonly _clockFactor; constructor(opts: ISnowflakeSiOptions); /** * Generate a bulk of Snowflake IDs. * * This method helps generate batch of Snowflake IDs, handling the time * reversed and sequence overflow scenarios automatically. * * @param qty The quantity of IDs to generate. * @param opts The options for bulk generation. * * @returns A promise that resolves to an array of Snowflake IDs. */ bulkGenerate(qty: number, opts?: Sf.IBulkGenerationOptions): Promise; /** * Generate the next SnowflakeSI ID, based on the current time and the next sequence number. * * @returns A SnowflakeSI ID, which is a 53-bit integer (number type). * * @throws {E_TIME_REVERSED} If the current time is earlier than the previous time. * @throws {E_SEQUENCE_OVERFLOWED} If the sequence number exceeds the maximum value. * @throws {E_TIME_BEFORE_EPOCH} If the current time is earlier than the epoch. */ generate(): number; /** * Generate a SnowflakeSI ID by specifying the timestamp and sequence number. * * @param timestamp The timestamp to use for the ID, must be greater than or equal to the epoch. * @param sequence The sequence number to use for the ID, must be between 0 and the maximum sequence value. * * @returns A SnowflakeSI ID, which is a 53-bit integer (number type). * * @throws {E_SEQUENCE_OVERFLOWED} If the sequence number exceeds the maximum value. * @throws {E_TIME_BEFORE_EPOCH} If the timestamp is earlier than the epoch. */ generateBy(timestamp: number, sequence: number): number; } //# sourceMappingURL=SnowflakeSI.d.ts.map