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

@street-devs/snowflake-id

v1.0.3

Published

Provides a highly customizable Snowflake ID generator for Javascript/Typescript applications. This generator produces unique 64-bit IDs based on the Snowflake algorithm, which consists of a timestamp, a node (instance) ID, and a sequence number. It also i

Downloads

139

Readme

@street-devs/snowflake-id User Guide

Overview

The @street-devs/snowflake-id package provides a class for generating unique 64-bit IDs using the Snowflake algorithm. It supports customizable options for epoch, machine ID, and worker ID, making each ID unique across distributed systems. Each generated ID consists of a timestamp, data center ID, worker ID, and sequence number.

Installation

Install the package via npm:

npm install @street-devs/snowflake-id

Usage

1. Import and Configure

import { SnowflakeId, SnowflakeIdOptions } from '@street-devs/snowflake-id';

2. Initialize SnowflakeId

The SnowflakeId class accepts an optional SnowflakeIdOptions configuration object to specify epoch, machine ID, and worker ID.

const options: SnowflakeIdOptions = {
  customEpoch: Date.now(),  // Optional, defaults to 2024-01-01T00:00:00Z
  machineId: 1,             // Optional, unique identifier for the machine
  workerId: 2               // Optional, unique identifier for the worker
};
const snowflakeId = new SnowflakeId(options);

3. Generate an ID

Use the generate() method to create a unique Snowflake ID.

const id = snowflakeId.generate();
console.log(id.toString());  // Example output: '1234567890123456789'

4. Decode an ID

The decode(id) method extracts the components of a Snowflake ID, providing details on when and where the ID was generated.

const decoded = snowflakeId.decode(id);
console.log(decoded);
// Output:
// {
//   dateTime: Date object,
//   timestamp: bigint,
//   dataCenterId: bigint,
//   workerId: bigint,
//   sequence: bigint,
//   epoch: number
// }

API Reference

SnowflakeId Class

Constructor: new SnowflakeId(options?: SnowflakeIdOptions)

  • options.customEpoch - Custom epoch time in milliseconds (default: 2024-01-01T00:00:00Z).-
  • options.machineId - Machine ID for uniqueness across machines (default: random).-
  • options.workerId - Worker ID for uniqueness across workers (default: random).

Methods

  • generate(): bigint - Generates a unique Snowflake ID as a 64-bit bigint.
  • decode(id: bigint): SnowflakeIdDecoded - Decodes a given Snowflake ID, returning:
  • dateTime: Date instance based on the timestamp.
  • timestamp: Timestamp component of the ID.
  • dataCenterId: Data center ID.
  • workerId: Worker ID.
  • sequence: Sequence number to prevent collisions within the same millisecond.
  • epoch: Custom epoch used for the timestamp.

Examples

const id = snowflakeId.generate();
console.log(`Generated ID: ${id}`);

const decodedId = snowflakeId.decode(id);
console.log(`Decoded components: `, decodedId);

Additional Notes

  • JSON Serialization: Includes a custom BigInt.toJSON() method that converts BigInt values to numbers for JSON compatibility.
  • Bit Structure: Uses a fixed 64-bit Snowflake ID structure, allocating 42 bits for the timestamp, 5 bits for data center ID, 5 bits for worker ID, and 12 bits for the sequence.