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

snowflakey

v0.2.0

Published

❄️ Fast and easy snowflake generation and lookup and token generation

Downloads

12

Readme

snowflakey

A lightweight Node.js snowflake generator/lookup tool

GitHub | NPM

Installing

$ yarn add snowflakey # Install w/ Yarn
$ npm i snowflakey # Install w/ NPM

Usage

Using a worker
// Declare snowflakey
const snowflakey = require('snowflakey');
// Create the worker instance
const Worker = new snowflakey.Worker({
  name: 'starling',
  epoch: 1420070400000,
  workerId: process.env.CLUSTER_ID || 31,
  processId: process.pid || undefined,
  workerBits: 8,
  processBits: 0,
  incrementBits: 14
});

// Generate the snowflake
const flake = Worker.generate();
console.log(`Created snowflake: ${flake}`);
console.log(`Creation date    : ${snowflakey.lookup(flake, worker.options.epoch)}`);
console.log(`Deconstructed    : ${Worker.deconstruct(flake).timestamp.valueOf()}`);
Using a master
// ... Worker code
// Create the master instance and add the worker
const Master = new snowflakey.Master();
master.addWorker(Worker);
// Listen to the events
master.on('newSnowflake', (data) => {
  console.log(`created snowflake: ${data.snowflake} by Worker ${data.worker.options.name || data.worker.options.workerId}`)
  console.log(`Creation date    : ${Snowflake.lookup(flake, data.worker.options.epoch)}`);
  data.worker.deconstruct(data.snowflake);
});

master.on('deconstructedFlake', (data) => {
  console.log(`Deconstructed    : ${data.timestamp.valueOf()} by Worker ${data.worker.options.name || data.worker.options.workerId}`);
});
// Make the worker generate a snowflake
worker.generate();
Result
$ node test.js
Created snowflake: 534760094454759424
Creation date    : 2019-1-15 16:45:41
Deconstructed    : 1547567141880

What is a snowflake?

Snowflakes are strings that range from 14 to 19 characters long that can give every user it's unique ID. You can't get much data with just a snowflake, but you can get the creation date of the snowflake and identify every unique user with it.

    #--[ Example on how snowflakes work using Discord's Epoch ]--#
                        18
                        107130754189766656
                                | to binary
57                              ↓    23     18      13
[1011111001001101011011010011101000][00000][000011][000000000000]
Number of MS since Discord's Epoch  internal internal incremented for
(the first second of 2015)          worker   process  every generated ID
              | to decimal          ID       ID       on that process
              ↓
         12770981096
              | +1420070400000
              ↓ Discord Epoch (unix timestamp in ms)
        1432841381096
              | Parse unix timestamp (ms)
              ↓
  2015-05-28T19:29:41.096Z UTC

What is a token?

Tokens are almost always used to access an API, tokens are (almost) always secret and only available to be viewed by the creator of the token. Snowflakey makes tokens that exist out of 3 parts: the user's ID, the current time and a Hmac hash. Below is an example on how it works

                        #--[ Example on how tokens work ]--#
80                          56           44
[MTA3MTMwNzU0MTg5NzY2NjU2].[MTg1MzkyNzk].[efdZism4cPVwMynra4491_c_05Hi5WuCptgWqlW5bFbY0]
The user's ID              Current time  Hmac hash (digest: Base64) that Consists
             |________ __________|       out of TTF + version + part[0] + part[1]
                      | From base64
                      ↓
         [107130754189766656].[18539279]
                  | Resolve       | * 1000 to
                  ↓ the User      ↓ convert it to ms
           Wesselgame#0498   18539279000
                                  | +1546300800000
                                  ↓ add Epoch (the first second of 2019)
                            1564840079000
                                  | Parse unix timestamp (ms)
                                  ↓
                     2019-08-03T13:47:59.000Z UTC