pika-id
Version:
The pragmatic ID system
42 lines (41 loc) • 1.46 kB
TypeScript
export declare type EpochResolvable = number | bigint | Date;
export interface SnowflakeGenOptions {
timestamp?: EpochResolvable;
}
export interface DeconstructedSnowflake {
id: bigint;
timestamp: bigint;
nodeId: number;
seq: number;
epoch: bigint;
}
/**
* A class for generating and deconstructing snowflakes.
*
* Pika has put it's own spin on Twitter snowflakes to simplify deployment
* and setup. Instead of having a separate worker and process ID, we have
* one node ID that takes up the 10 bits these fields would usually use.
*
* A node ID is computed by taking the MAC address of the first available
* public interface on the device, then calculating the modulo against
* 1024 (10b)
*
* If we have a snowflake `963584775274749952n` we can represent it as binary:
* ```
* 64 22 12 0
* 000011010101111101011111011110000011001010 0001000101 000000000000
* number of ms since epoch node id sequence
* ```
*/
export declare class Snowflake {
#private;
/**
* @param epoch the base epoch to use
* @param nodeId optionally pass a static node identifier (0-1023)
*/
constructor(epoch: EpochResolvable, nodeId: number | bigint);
get nodeId(): number;
gen({ timestamp }?: SnowflakeGenOptions): string;
deconstruct(id: string | bigint): DeconstructedSnowflake;
private normalizeEpoch;
}