New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

snowflake-generator

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

snowflake-generator

A 64-bit Snowflake generator written in JavaScript

latest
Source
npmnpm
Version
1.0.2
Version published
Weekly downloads
212
17.78%
Maintainers
1
Weekly downloads
 
Created
Source

snowflake-generator

NPM version NPM downloads Patreon

A lightweight Twitter Snowflake generation package utilising TypeScript.

Getting Started

Install the npm package

npm install snowflake-generator

Require the package into your code

const { Generator } = require('snowflake-generator');

Construct a new Snowflake Generator with the EPOCH timestamp in milliseconds

const SnowflakeGenerator = new Generator(1420070400000); 
// Thursday, 1 January 2015 00:00:00

Generate a Snowflake

const Snowflake = SnowflakeGenerator.generate();

Generator (class)

Constructor

new Generator(epoch?: Date|number, shardID?: number);
paramtypeoptionaldefaultdescription
epochDate|numbertrue946684800000The epoch timestamp to generate from.
shardIDnumbertrue1Useful when running multiple generators at the same time to prevent duplicates.

Properties

.EPOCH

The generators epoch timestamp in milliseconds.
@type number

.SHARD_ID

The id of the shard running this generator.
@type number

.INCREMENT

The current increment iteration this generator is on.
@type number

Methods

Generates snowflakes.

Generator.generate(amount?: number, timestamp?: Date|number);
parametertypeoptionaldefaultdescription
amountnumbertrue1Amount of snowflakes to generate, recommended not to go above 1024 or duplicates will arise.
timestampDate|numbertrueDate.nowTimestamp to generate from

@returns bigint|bigint[]

Deconstruct a snowflake to its values using the Generator.epoch.

Generator.deconstruct(snowflake: SnowflakeResolvable);
parameterstypedescription
snowflakeSnowflakeResolvableSnowflake(s) to deconstruct

@returns DeconstructedSnowflake

Types & Interfaces

SnowflakeResolvable

Resolvable value types for a valid Snowflake.

  • string
  • number
  • bigint

DeconstructedSnowflake

Interface of a Snowflake after Generator.deconstruct().

  • snowflake - Snowflake deconstructed from
    @type bigint
  • timestamp - The timestamp the snowflake was generated
    @type bigint
  • shard_id - The shard_id used when generating
    @type bigint
  • increment - The increment of this snowflake
    @type bigint
  • binary - The 64Bit snowflake binary string
    @type string

Extra Credit

Generating more than 1024 snowflakes

let snowflakes = [];
let ts = Date.now();
let amount = 20000;
for (let i = 0; i < (amount / 1024); i++) {
    // this could be improved, but is proof of concept.
    let new_amount = i + 1 >= (amount / 1024) ? amount % 1024 : 1024;
    snowflakes = snowflakes.concat(generator.generate(new_amount, ts + i));
}

Note: When parsing this array through a duplication checking function it returns 0 found duplicates.

> console.log(generator) after running script.

> Note: increment == amount.

Generator { epoch: 1420070400000, shard_id: 1, increment: 20000 }

Keywords

idgen

FAQs

Package last updated on 01 Nov 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts