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

emiflake

v1.1.1

Published

A utility library for creating a service that generates unique IDs at scale. Think of it as containing the common logic of Twitter's Snowflake and Boundary's Flake.

Downloads

7

Readme

emiflake

Dependency status devDependency Status

NPM

emiflake is a node.js utility library for creating a service that generates unique IDs at scale. Think of it as containing the common logic of Twitter Snowflake and Boundary Flake, sans network and machine ID generation stuff.

emiflake is useful if you want something like Snowflake, but don't want to use Zookeeper to coordinate the machine ID allocation. Another situation where the library is useful is when you want something like Boundary Flake, but don't want to add a dependency to Erlang.

It is written in C++ as a node.js extension, because I am very uncomfortable trying to handle 64 bit integers with Javascript's floating point math. flake.h and flake.cc contain the actual logic, and are usable by themselves, without node.js.

The current implementation uses the C standard function gettimeofday to get the time.

Anatomy

The length of emiflake ids vary depending on the parameters: They are timestampLength+sequenceNumberLength+machineNumberLength bits long.

The structure of the ids are: The most significant bits contain the timestamp. After that is the sequence number, and last is the machine number.

When is it appropriate to use emiflake ids?

emiflake ids are appropriate when using node.js and when Boundary Flake ids or Twitter Snowflake ids would be appropriate. The exact details are well documented on their respective READMEs.

Recommended parameters

To replicate the beavior of Boundary Flake, use parameters like this:

  • machineNumberLength: 48, and use a MAC address as the machine number.
  • sequenceNumberLength: 16
  • timestampLength: 64
  • timestampOffset: 0

To do something similar to Twitter Snowflake, use parameters like this:

  • machineNumberLength: 10, and use some coordination mechanism to choose the machine number.
  • sequenceNumberLength: 12
  • timestampLength: 41
  • timestampOffset: Something like (2012-1970)365246060*1000

Getting started

Install with npm:

npm install emiflake

Generate an id:

var emiFlake = require('emiflake');

var f = new emiFlake(new Buffer("mid"),
                     /*machineNumberLength:*/24,
                     /*sequenceNumberLength:*/16,
                     /*timestampLength:*/24,
                     /*timestampOffset:*/20000);

f.generate(console.log);

API

new emiFlake(machineNumber, machineNumberLength, sequenceNumberLength, timestampLength[, timestampOffset])

Create a new flake object.

  • machineNumber must be a Buffer object
  • machineNumberLength should be an integer specifying how many bits of the machineNumber that should be used.
  • sequenceNumberLength should be an integer specifying how many bits the sequence number should use. Must be less than or equal to 64.
  • timestampLength should be an integer specifying how many bits the timestamp should use. Must be less than or equal to 64.
  • timestampOffset specifies the epoch, as a positive number of milliseconds since 00:00 Jan 1, 1970. Optional. The default value is 0. When using a small timestamp length, this parameter is useful to maximize the range. For instance, one could set the 0 timestamp to be at the start of 2012.

EmiFlake.generate(cb)

Generates a unique ID. cb should be a callback that takes the resulting ID as a buffer. In case of sequence number overflow or minor backward jumps in the wall time, it will wait and try again. If the wall time has jumped backwards a lot, cb will be invoked with null.

generate never invokes cb before it returns.

Unspecified return value.

EmiFlake.byteLength()

Returns the size of ID buffers in bytes. This is equal to ceil([the bit length]/8).

EmiFlake.bitLength()

Returns the size of the IDs in bits.

License

MIT