mirror of
https://github.com/Wessel/Snowflakey.git
synced 2026-06-06 03:05:48 +02:00
496cd5641c7dfa68295688d25a39f78c195dbdbc
snowflakey
A lightweight Node.js snowflake generator/lookup tool
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 idea. 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.

Languages
TypeScript
100%