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

snowflake.io

v0.0.0-alpha.3

Published

Snowflake.io, generates k-ordered, conflict-free snowflake IDs in a distributed environment.

Downloads

47

Readme

snowflake.io

  • Snowflake.io, generates k-ordered, conflict-free snowflake IDs in a distributed environment.
  • Snowflake.io,在分布式环境中生成 k 有序、无冲突的雪花ID。

Installation

$ pnpm add snowflake.io ⏎

Simple usage:

import { snowflakeId } from 'snowflake.io'
// snowflakeId returns the Bigint value converted to a string.
// Configuration options are required in distributed environments.
// 生成为String类型的的雪花ID
snowflakeId({
  id: 100,
  datacenter: 9,
  worker: 7
}) // 7176875713503428608
// OR
snowflakeId() // '7176875713503428608'

Usage

// SnowflakeOptions
export interface SnowflakeOptions {
  datacenter?: number | undefined;
  worker?: number | undefined;
  id?: number | undefined;
  epoch?: number | undefined;
  seqMask?: number | undefined;
}
import { generateSnowflakeIdBuffer, generateSnowflakeIdBigint, generateSnowflakeIdString } from 'snowflake.io'
    
// snowflake.generateSnowflakeIdBuffer returns an 8-byte long node Buffer object, whose bytes represent a 64-bit long ID.
// Configuration options are required in distributed environments.
// 生成为Buffer的雪花ID
generateSnowflakeIdBuffer({
  id: 100,
  datacenter: 9,
  worker: 7
}) // <Buffer 63 99 62 6f cc 80 00 00>
// OR
generateSnowflakeIdBuffer() // <Buffer 63 99 62 6f cc 80 00 00>

// snowflake.generateSnowflakeIdBigint returns the Bigint value after processing the Buffer.
// Configuration options are required in distributed environments.
// 生成为Bigint的雪花ID
generateSnowflakeIdBigint({
  id: 100,
  datacenter: 9,
  worker: 7
}) // 7176875713503428608n
// OR
generateSnowflakeIdBigint() // 7176875713503428608n

// snowflake.generateSnowflakeIdBigint returns the Bigint value converted to a string.
// Configuration options are required in distributed environments.
// 生成为字符串的雪花ID
generateSnowflakeIdString({
  id: 100,
  datacenter: 9,
  worker: 7
}) // '7176875713503428608'
// OR
generateSnowflakeIdString() // '7176875713503428608'
// OR
snowflakeId() // '7176875713503428608'
import { Snowflake } from 'snowflake.io'
    
// Configuration options are required in distributed environments.
const options: SnowflakeOptions = {
  id: 100,
  datacenter: 9,
  worker: 7
}

// snowflake.generateSnowflakeIdBuffer returns an 8-byte long node Buffer object, whose bytes represent a 64-bit long ID.
// Configuration options are required in distributed environments.
Snowflake.generateSnowflakeIdBuffer({
  id: 100,
  datacenter: 9,
  worker: 7
}) // <Buffer 63 99 62 6f cc 80 00 00>
// OR
Snowflake.generateSnowflakeIdBuffer() // <Buffer 63 99 62 6f cc 80 00 00>

// snowflake.generateSnowflakeIdBigint returns the Bigint value after processing the Buffer.
// Configuration options are required in distributed environments.
Snowflake.generateSnowflakeIdBigint({
  id: 100,
  datacenter: 9,
  worker: 7
}) // 7176875713503428608n
// OR
Snowflake.generateSnowflakeIdBigint() // 7176875713503428608n

// snowflake.generateSnowflakeIdBigint returns the Bigint value converted to a string.
// Configuration options are required in distributed environments.
Snowflake.generateSnowflakeIdString({
  id: 100,
  datacenter: 9,
  worker: 7
}) // '7176875713503428608'
// OR
Snowflake.generateSnowflakeIdString() // '7176875713503428608'

Snowflake Numbers Format

The Snowflake ID is made up of: timestamp, datacenter, worker and counter. Examples in the following table:

+-------------+------------+--------+---------+--------------------+
|  Timestamp  | Datacenter | Worker | Counter | Flake ID           |
+-------------+------------+--------+---------+--------------------+
| 0x8c20543b0 |   00000b   | 00000b |  0x000  | 0x02308150ec000000 |
+-------------+------------+--------+---------+--------------------+
| 0x8c20543b1 |   00000b   | 00000b |  0x000  | 0x02308150ec400000 |
+-------------+------------+--------+---------+--------------------+
| 0x8c20543b1 |   00000b   | 00000b |  0x001  | 0x02308150ec400001 |
+-------------+------------+--------+---------+--------------------+
| 0x8c20543b1 |   00000b   | 00000b |  0x002  | 0x02308150ec400002 |
+-------------+------------+--------+---------+--------------------+
| 0x8c20543b1 |   00000b   | 00000b |  0x003  | 0x02308150ec400003 |
+-------------+------------+--------+---------+--------------------+
| 0x8c20c0335 |   00011b   | 00001b |  0x000  | 0x02308300cd461000 |
+-------------+------------+--------+---------+--------------------+
| 0x8c20c0335 |   00011b   | 00001b |  0x001  | 0x02308300cd461001 |
+-------------+------------+--------+---------+--------------------+

Breakdown of bits for an id e.g. 5828128208445124608 (counter is 0, datacenter is 7 and worker 3) is as follows:

 010100001110000110101011101110100001000111 00111 00011 000000000000
                                                       |------------| 12 bit counter
                                                 |-----|               5 bit worker
                                           |-----|                     5 bit datacenter
                                           |----- -----|              10 bit generator identifier
|------------------------------------------|                          42 bit timestamp

Note that composition of datacenter id and worker id makes 1024 unique generator identifiers. By modifying datacenter and worker id we can get up to 1024 id generators on a single machine (e.g. each running in a separate process) or have 1024 machines with a single id generator on each. It is also possible to provide a single 10 bit long identifier (up to 1024 values). That id is internally split into datacenter (the most significant 5 bits) and worker (the least significant 5 bits).