Socket
Book a DemoInstallSign in
Socket

snowflake-uuid

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

snowflake-uuid

Twitter's Snowflake generator implementation for NodeJS

1.0.0
latest
Source
npmnpm
Version published
Weekly downloads
753
22.24%
Maintainers
1
Weekly downloads
 
Created
Source

NPM Repository

GitHub issues GitHub license node-current NPM version

Snowflake-UUID

Twitter's Snowflake generator for NodeJS.

  • Speedy
  • Lightweight
  • Zero dependencies

Snowflake-UUID can generate unique Id numbers at high scale with some simple guarantees.

Installing

npm install snowflake-uuid
// or
yarn add snowflake-uuid
// or
pnpm add snowflake-uuid

Example

import { Worker } from 'snowflake-uuid';
// const { Worker } = require('snowflake-uuid');

const generator = new Worker(0, 1, {
	workerIdBits: 5,
	datacenterIdBits: 5,
	sequenceBits: 12,
});

generator.nextId(); // 87559690812260352n
generator.nextId().toString(); // 87559690833231872

generator.getCurrentSequence(); // 0n
generator.getDatacenterId(); // 1n
generator.getLastTimestamp(); // 1630335057075n
generator.getWorkerId(); // 0n

Generated IDs are BigInt and can be easily converted into strings.

Constructor Options

PropertyDescriptionOptional?TypeDefault Value
workerIdId for the workerYesNumber or BigInt0
datacenterIdIdentifier of the datacenter to which the worker belongsYesNumber or BigInt0
optionsCustom options for the workerYesObjectundefined
options.epochCustom epoch for timestamp generation. By default, the number of milliseconds since the first second of 2021YesNumber or BigInt1609459200000
options.workerIdBitsNumber of usable bits for Worker Id. 5 by default, allows up to 31 WorkersYesNumber or BigInt5
options.datacenterIdBitsNumber of usable bits for Datacenter Id. 5 by default, allows up to 31 DatacentersYesNumber or BigInt5
options.sequenceFor every Id that is generated on that process, this number is incrementedYesNumber or BigInt0
options.sequenceBitsNumber of usable bits for Sequence Id. 12 by default, allows up to 4095 generations per millisecond per Worker per DatacenterYesNumber or BigInt12

License

Refer to the LICENSE file.

Keywords

snowflake

FAQs

Package last updated on 06 Sep 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

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.