const ProcessId = 1n ; const WorkerId = 0n ; /**
 * A class for generating and deconstructing Twitter snowflakes.
 *
 * A  https * is a 64-bit unsigned integer with 4 fields that have a fixed epoch value.
 *
 * If we have a snowflake `266241948824764416` we can represent it as binary:
 * ```
 * 64                                          22     17     12          0
 *  000000111011000111100001101001000101000000  00001  00000  000000000000
 *           number of ms since epoch           worker  pid    increment
 * ```
  export class Snowflake { internal #increment = 0n ; internal #epoch : bigint ; /**
     * Alias for  deconstruct public decode = this . deconstruct ; param epoch public constructor ( epoch ) { this . #epoch = BigInt ( epoch instanceof Date ? epoch . getTime ( ) : epoch ) ; } /**
     * The epoch for this snowflake.
     */ public get epoch ( ) : bigint { return this . #epoch ; } param options options to pass into the generator, see  SnowflakeGenerateOptions *
     * **note** when `increment` is not provided it defaults to the private `increment` of the instance
     *  example returns public generate ( { increment , timestamp = Date . now ( ) , workerId = WorkerId , processId = ProcessId } = { } ) { if ( timestamp instanceof Date ) timestamp = BigInt ( timestamp . getTime ( ) ) ; else if ( typeof timestamp === 'number' ) timestamp = BigInt ( timestamp ) ; else if ( typeof timestamp !== 'bigint' ) { throw new TypeError ( `"timestamp" argument must be a number, bigint, or Date (received ${ typeof timestamp })` ) ; } if ( typeof increment === 'bigint' && increment >= 4095n ) increment = 0n ; else { increment = this . #increment ++ ; if ( this . #increment >= 4095n ) this . #increment = 0n ; } return ( ( timestamp - this . #epoch ) << 22n ) | ( ( workerId & 31n ) << 17n ) | ( ( processId & 31n ) << 12n ) | increment ; } param id returns example public deconstruct ( id ) { const bigIntId = BigInt ( id ) ; return { id : bigIntId , timestamp : ( bigIntId >> 22n ) + this . #epoch , workerId : ( bigIntId >> 17n ) & 31n , processId : ( bigIntId >> 12n ) & 31n , increment : bigIntId & 4095n , epoch : this . #epoch } ; } param id returns public timestampFrom ( id ) { return Number ( ( BigInt ( id ) >> 22n ) + this . #epoch ) ; } } /**
 * Options for Snowflake#generate
 */ export interface SnowflakeGenerateOptions { default timestamp ? : number | bigint | Date ; default remark increment ? : bigint ; default workerId ? : bigint ; default processId ? : bigint ; } /**
 * Object returned by Snowflake#deconstruct
 */ export interface DeconstructedSnowflake { /**
     * The id in BigInt form
     */ id : bigint ; /**
     * The timestamp stored in the snowflake
     */ timestamp : bigint ; /**
     * The worker id stored in the snowflake
     */ workerId : bigint ; /**
     * The process id stored in the snowflake
     */ processId : bigint ; /**
     * The increment stored in the snowflake
     */ increment : bigint ; /**
     * The epoch to use in the snowflake
     */ epoch : bigint ; }