npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@cordxapp/snowflake

v0.4.18

Published

A simple snowflake generator

Downloads

12

Readme

CordXSnowflake

CordXSnowflake is a simple yet customizable module for generating and decomposing unique ID strings, similar to Twitter and Discord's Snowflake IDs. Snowflake IDs are a way of generating unique, sortable identifiers distributedly, as they contain a timestamp component and machine identifier.

Table of Contents

Features

  • Generate unique ID strings
  • Decompose ID strings back into their components
  • Configurable worker ID, process ID, epoch, and increment
  • Optional debug logging

Installation

npm install @cordxapp/snowflake

Usage

First, you need to import the CordX Snowflake client/class:

import { CordXSnowflake } from '@cordxapp/snowflake';

Create a new instance

Once you have the module imported you can create a new CordXSnowflake client:

const snowflake = new CordXSnowflake({
    workerId: 1, // Unique identifier for the worker generating IDs
    processId: 2, // Unique identifier for the process generating IDs
    epoch: 3, // Starting point in time for generating IDs
    increment: 4, // Ensures uniqueness of IDs generated in the same millisecond
    sequence: 5n, // Additional measure to ensure uniqueness
    debug: false // Enable or disable debug logging
});

Generate a new snowflake

Now you can generate a new snowflake id:

const id = await snowflake.generate();

console.log(id); // Outputs: '12345678901234567890'

Decompose a snowflake

You can also decompose a snowflake to return its properties:

const components = snowflake.decompose(id);
console.log(components); // Outputs: { timestamp: 1234567890, workerId: 1, processId: 2, sequence: 5 }

API

CordXSnowflake(options: ICordXSnowflakeOptions)

Creates a new CordXSnowflake instance. The options object should contain the following properties:

  • workerId: A number representing the worker ID. Must be less than or equal to 31.
  • processId: A number representing the process ID. Must be less than or equal to 31.
  • epoch: A number representing the epoch.
  • increment: A number representing the increment. Must be less than or equal to 4095.
  • sequence: A bigint representing the sequence. Must be less than or equal to 4095.
  • debug: A boolean indicating whether debug logging should be enabled.

Example:

const snowflake = new CordXSnowflake({
    workerId: 1,
    processId: 2,
    epoch: 3,
    increment: 4,
    sequence: 5n,
    debug: false
});

generate(): string

Generates a new ID string. The ID is composed of the timestamp, worker ID, process ID, and sequence, each shifted to occupy a specific range of bits in the ID.

Example:

const id = snowflake.generate();
console.log(id); // Outputs: '12345678901234567890'

decompose(id: string): { timestamp: number, workerId: number, processId: number, sequence: number }

Decomposes an ID string back into its components. The returned object contains the following properties:

  • timestamp: The timestamp component of the ID.
  • workerId: The worker ID component of the ID.
  • processId: The process ID component of the ID.
  • sequence: The sequence component of the ID.

Example:

const components = snowflake.decompose(id);
console.log(components); // Outputs: { timestamp: 1234567890, workerId: 1, processId: 2, sequence: 5 }

Error Handling

If an error occurs during ID generation or decomposition, a SnowflakeError will be thrown. You can catch this error and handle it appropriately in your application.


Contributing

Contributions are welcome! Please submit a pull request or create an issue to discuss any changes you'd like to make.