UNPKG

pika-id

Version:

The pragmatic ID system

74 lines (73 loc) 2.52 kB
import { EpochResolvable } from "./snowflake"; export interface PikaPrefixDefinition<P extends string> { prefix: P; description?: string; secure?: boolean; metadata?: Record<string, unknown>; } export interface DecodedPika<P extends string> { prefix: P; /** * The tail after the prefix, which is base64 encoding of the snowflake. * * However, if the pika is cryptographically secure, then the base64 decoded string value will start with an `s_` prefix, * followed by a cryptographically random string, then followed by another underscore and the Snowflake ID. */ tail: string; /** * The snowfake that was generated for this ID */ snowflake: bigint; /** * The ID this Pika was generated from. */ nodeId: number; /** * A rolling number between 1 to 4096 to introduce entropy. * Allows for doing 4096 ids per ms per node. */ seq: number; /** * The version of the pika encoding. */ version: 1; /** * The definition for this prefix */ prefixRecord: PikaPrefixDefinition<P>; /** * @deprecated use `.prefixRecord` instead */ prefix_record: PikaPrefixDefinition<P>; } export interface PikaInitializationOptions { epoch?: EpochResolvable; nodeId?: number; suppressPrefixWarnings?: boolean; disableLowercase?: boolean; } export declare const VALID_PREFIX: RegExp; export declare const DEFAULT_EPOCH = 1640995200000n; export declare type PrefixInit<V extends string> = V | PikaPrefixDefinition<V>; export declare type LowercasePrefixInit<V extends string> = Lowercase<V> extends V ? PrefixInit<V> : PrefixInit<Lowercase<V>>; export declare class Pika<Prefixes extends string> { #private; readonly prefixes: Record<string, PikaPrefixDefinition<Prefixes>>; /** * @param prefixes a list of PikaPrefixRecords to initialize pika with * @param opts misc. options to initialize pika with */ constructor(prefixes: readonly LowercasePrefixInit<Prefixes>[], { nodeId, ...opts }?: PikaInitializationOptions); gen<Prefix extends Prefixes>(prefix: Prefix): `${Prefix}_${string}`; /** * Gen a Snowflake, if you really need one */ genSnowflake(): string; decode(id: string): DecodedPika<Prefixes>; /** * Derives this machine's node ID from the MAC address of the first * public network interface it finds * @returns The computed node ID (0-1023) */ private computeNodeId; }