{"id":13591902,"url":"https://github.com/T-PWK/flake-idgen","last_synced_at":"2025-04-08T18:30:59.032Z","repository":{"id":13069117,"uuid":"15749731","full_name":"T-PWK/flake-idgen","owner":"T-PWK","description":"Flake ID generator yields k-ordered, conflict-free ids in a distributed environment in Node.js","archived":false,"fork":false,"pushed_at":"2023-01-09T05:43:17.000Z","size":152,"stargazers_count":244,"open_issues_count":4,"forks_count":20,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-29T18:38:21.159Z","etag":null,"topics":["buffer","bytes","flake","flake-ids","identifier","javascript","maintainer-wanted","node-js","node-module","nodejs","unique","unique-id","unique-identifier","uniqueid","uniquid"],"latest_commit_sha":null,"homepage":"https://www.npmjs.org/package/flake-idgen","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"loopj/android-async-http","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/T-PWK.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-01-08T22:26:33.000Z","updated_at":"2025-03-25T16:51:41.000Z","dependencies_parsed_at":"2023-01-13T17:17:03.232Z","dependency_job_id":null,"html_url":"https://github.com/T-PWK/flake-idgen","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/T-PWK%2Fflake-idgen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/T-PWK%2Fflake-idgen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/T-PWK%2Fflake-idgen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/T-PWK%2Fflake-idgen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/T-PWK","download_url":"https://codeload.github.com/T-PWK/flake-idgen/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247901995,"owners_count":21015379,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["buffer","bytes","flake","flake-ids","identifier","javascript","maintainer-wanted","node-js","node-module","nodejs","unique","unique-id","unique-identifier","uniqueid","uniquid"],"created_at":"2024-08-01T16:01:03.540Z","updated_at":"2025-04-08T18:30:58.650Z","avatar_url":"https://github.com/T-PWK.png","language":"JavaScript","funding_links":[],"categories":["JavaScript","Generation"],"sub_categories":["JavaScript"],"readme":"Flake ID Generator \n===========\n[![Build Status](https://travis-ci.org/T-PWK/flake-idgen.svg?branch=master)](https://travis-ci.org/T-PWK/flake-idgen) \n[![npm version](https://badge.fury.io/js/flake-idgen.svg)](http://badge.fury.io/js/flake-idgen)\n[![npm downloads](https://img.shields.io/npm/dm/flake-idgen.svg)](https://www.npmjs.com/package/flake-idgen)\n[![Code Climate](https://codeclimate.com/github/T-PWK/flake-idgen/badges/gpa.svg)](https://codeclimate.com/github/T-PWK/flake-idgen)\n[![Coverage Status](https://coveralls.io/repos/github/T-PWK/go-flakeid/badge.svg?branch=master)](https://coveralls.io/github/T-PWK/go-flakeid?branch=master)\n[![GitHub issues](https://img.shields.io/github/issues/T-PWK/flake-idgen.svg)](https://github.com/T-PWK/flake-idgen/issues)\n[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](http://blog.tompawlak.org/mit-license)\n\nFlake ID generator yields k-ordered, conflict-free ids in a distributed environment.\n\n## Installation ##\n$ npm install --save flake-idgen ⏎\n\n## Flake Numbers Format ##\n\nThe Flake ID is made up of: `timestamp`, `datacenter`, `worker` and `counter`. Examples in the following table: \n```\n+-------------+------------+--------+---------+--------------------+\n|  Timestamp  | Datacenter | Worker | Counter | Flake ID           |\n+-------------+------------+--------+---------+--------------------+\n| 0x8c20543b0 |   00000b   | 00000b |  0x000  | 0x02308150ec000000 |\n+-------------+------------+--------+---------+--------------------+\n| 0x8c20543b1 |   00000b   | 00000b |  0x000  | 0x02308150ec400000 |\n+-------------+------------+--------+---------+--------------------+\n| 0x8c20543b1 |   00000b   | 00000b |  0x001  | 0x02308150ec400001 |\n+-------------+------------+--------+---------+--------------------+\n| 0x8c20543b1 |   00000b   | 00000b |  0x002  | 0x02308150ec400002 |\n+-------------+------------+--------+---------+--------------------+\n| 0x8c20543b1 |   00000b   | 00000b |  0x003  | 0x02308150ec400003 |\n+-------------+------------+--------+---------+--------------------+\n| 0x8c20c0335 |   00011b   | 00001b |  0x000  | 0x02308300cd461000 |\n+-------------+------------+--------+---------+--------------------+\n| 0x8c20c0335 |   00011b   | 00001b |  0x001  | 0x02308300cd461001 |\n+-------------+------------+--------+---------+--------------------+\n```\n\nAs you can see, each Flake ID is 64 bits long, consisting of:\n* `timestamp`, a 42 bit long number of milliseconds elapsed since 1 January 1970 00:00:00 UTC \n* `datacenter`, a 5 bit long datacenter identifier. It can take up to 32 unique values (including 0)\n* `worker`, a 5 bit long worker identifier. It can take up to 32 unique values (including 0)\n* `counter`, a 12 bit long counter of ids in the same millisecond. It can take up to 4096 unique values. \n\nBreakdown of bits for an id e.g. `5828128208445124608` (counter is `0`, datacenter is `7` and worker `3`) is as follows:\n```\n 010100001110000110101011101110100001000111 00111 00011 000000000000\n                                                       |------------| 12 bit counter\n                                                 |-----|               5 bit worker\n                                           |-----|                     5 bit datacenter\n                                           |----- -----|              10 bit generator identifier\n|------------------------------------------|                          42 bit timestamp\n```\n\nNote that composition of `datacenter id` and `worker id` makes 1024 unique generator identifiers. By modifying datacenter and worker id we can get up to 1024 id generators on a single machine (e.g. each running in a separate process) or have 1024 machines with a single id generator on each. It is also possible to provide a single 10 bit long identifier (up to 1024 values). That id is internally split into `datacenter` (the most significant 5 bits) and `worker` (the least significant 5 bits).\n\n## Usage ##\n\nFlake ID Generator returns 8 byte long node [Buffer](http://nodejs.org/api/buffer.html) objects with its bytes representing 64 bit long id. Note that the number is stored in Big Endian format i.e. the most significant byte of the number is stored in the smallest address given and the least significant byte is stored in the largest.\n\nFlake id generator instance has one method `next(cb)` returning generated id (if a callback function is not provided) or calling provided callback function with two arguments: `error` and `generated id`.\n\nThe following example uses `next` with no callback function:\n\n```js\nvar FlakeId = require('flake-idgen');\nvar flakeIdGen = new FlakeId();\n\nconsole.log(flakeIdGen.next());\nconsole.log(flakeIdGen.next());\nconsole.log(flakeIdGen.next());\n```\n\nIt would give something like:\n```\n\u003cBuffer 50 dd d5 99 01 c0 00 00\u003e\n\u003cBuffer 50 dd d5 99 02 80 00 00\u003e\n\u003cBuffer 50 dd d5 99 02 80 00 01\u003e\n```\n\nThe following example uses `next` with callback function:\n```js\nvar FlakeId = require('flake-idgen');\nvar flakeIdGen = new FlakeId();\n\nflakeIdGen.next(function (err, id) {\n     console.info(id);\n})\n\nflakeIdGen.next(function (err, id) {\n     console.info(id);\n})\n```\n\nIt would give something like:\n```\n\u003cBuffer 50 dd d6 49 ef c0 00 00\u003e\n\u003cBuffer 50 dd d6 49 f0 00 00 00\u003e\n```\n\n### Counter overflow ###\nFlake ID Generator can generate up to 4096 unique identifiers within a millisecond. When generator tries to generate more than 4096 identifiers within a millisecond, the following things will happen:\n* When using `next()` without a callback function, an error is thrown.\n* When using `next(cb)` with a callback function, the callback function is called in the following millisecond without any error.\n\n### Additional generator setup parameters ###\nFlake Id generator constructor takes optional parameter (generator configuration options) with the following properties:\n* `datacenter` (5 bit) - datacenter identifier. It can have values from 0 to 31.\n* `worker` (5 bit) - worker identifier. It can have values from 0 to 31.\n* `id` (10 bit) - generator identifier. It can have values from 0 to 1023. It can be provided instead of `datacenter` and `worker` identifiers.\n* `epoch` - number used to reduce value of a generated timestamp. Note that this number should not exceed number of milliseconds elapsed since 1 January 1970 00:00:00 UTC. It can be used to generate _smaller_ ids.\n\nExample of using `datacenter` and `worker` identifiers:\n```js\nvar FlakeId = require('flake-idgen')\n\nvar flakeIdGen1 = new FlakeId();\nvar flakeIdGen2 = new FlakeId({ datacenter: 9, worker: 7 });\n\nconsole.info(flakeIdGen1.next());\nconsole.info(flakeIdGen2.next());\n```\n\nIt would give something like:\n```\n\u003cBuffer 50 dd da 8f 43 40 00 00\u003e\n\u003cBuffer 50 dd da 8f 43 d2 70 00\u003e\n```\n\nExample of using `epoch` parameter:\n```js\nvar FlakeId = require('flake-idgen')\n\nvar flakeIdGen1 = new FlakeId();\nvar flakeIdGen2 = new FlakeId({ epoch: 1300000000000 });\n\nconsole.info(flakeIdGen1.next());\nconsole.info(flakeIdGen2.next());\n```\n\nIt would give something like:\n```\n\u003cBuffer 50 dd db 00 d1 c0 00 00\u003e\n\u003cBuffer 05 32 58 8e d2 40 00 00\u003e\n```\n\n### Properties ###\nFlake Id generator has some properties that can be read from a generator instance:\n* `datacenter` - returns datacenter number used for generator creation; otherwise it returns `undefined` value.\n* `worker` - returns worker number used for generator creation; otherwise it returns `undefined` value.\n* `id` - returns worker identifier number used for generator creation or combines its value from datacenter and worker numbers. Identifier is always available and it is defaulted to zero.\n\nFlake Id generator instantiated without any parameter gets `datacenter`, `worker` and `id` values defaulted to zeros.\n\n```js\nvar FlakeId = require('flake-idgen')\n\nvar flakeIdGen1 = new FlakeId({ id: 100 });\nvar flakeIdGen2 = new FlakeId({ datacenter: 9, worker: 7 });\nvar flakeIdGen3 = new FlakeId();\n\nconsole.info(flakeIdGen1.id);           // 100\nconsole.info(flakeIdGen1.datacenter);   // undefined\nconsole.info(flakeIdGen1.worker);       // undefined\n\nconsole.info(flakeIdGen2.datacenter);   // 9\nconsole.info(flakeIdGen2.worker);       // 7\nconsole.info(flakeIdGen2.id);           // 259\n\nconsole.info(flakeIdGen3.datacenter);   // 0\nconsole.info(flakeIdGen3.worker);       // 0\nconsole.info(flakeIdGen3.id);           // 0\n```\n\nIt would give something like:\n\n```js\n100\nundefined\nundefined\n\n9\n7\n295\n\n0\n0\n0\n```\n\n### Clock moving backward ###\nFrom time to time Node.js clock may move backward. In most cases it is only a few millisecond. However, as the generator relies on current timestamp, it won't be able to generate conflict-free identifiers (i.e. without duplicates) until the clock catches up with the last timestamp value. In case of clock move backward the following things will happen:\n* When using `next()` without a callback function, an error is thrown.\n* When using `next(cb)` with a callback function, the callback function is called with a new identifier generated once the clock catches up with the last timestamp.\n\n## Formatting ##\n\nFlake Id generator returns node Buffer representing 64-bit number for the sake of future extensions or returned buffer modifications. Node Buffer can also be very easily converted to string format. There is a NPM [biguint-format](https://npmjs.org/package/biguint-format) module which provides Buffer to string conversion functionality e.g.\n\n```js\nvar intformat = require('biguint-format')\n    , FlakeId = require('flake-idgen')\n\nvar flakeIdGen1 = new FlakeId();\nvar flakeIdGen2 = new FlakeId({ epoch: 1300000000000 });\n\nconsole.info(intformat(flakeIdGen1.next(), 'dec'));\nconsole.info(intformat(flakeIdGen1.next(), 'hex', { prefix: '0x' }));\n\nconsole.info(intformat(flakeIdGen2.next(), 'dec'));\nconsole.info(intformat(flakeIdGen2.next(), 'hex', { prefix: '0x' }));\n```\n\nIt would give something like:\n```js\n5827056208820830208 // flakeIdGen1 decimal format\n0x50dddcbfb5c00001  // flakeIdGen1 hex format\n\n374461008833413120 // flakeIdGen2 decimal format\n0x5325a4db6000002  // flakeIdGen2 hex format\n```\n\nGenerated id could also be converted to binary string format, split into 4 digit groups of 0's and 1's e.g.\n```js\nvar intformat = require('biguint-format')\n    , idGen = new (require('flake-idgen'))\n\nfor (var i = 0; i \u003c 5; i++) {\n\tconsole.info(intformat(idGen.next(), 'bin', { groupsize: 4 }));\n};\n```\n\nIt would give something like:\n```js\n0101 0000 1101 1111 1011 0110 0001 0101 1100 0001 0100 0000 0000 0000 0000 0000 // 0x50 df b6 15 c1 40 00 00\n0101 0000 1101 1111 1011 0110 0001 0101 1100 0101 0000 0000 0000 0000 0000 0000 // 0x50 df b6 15 c5 00 00 00\n0101 0000 1101 1111 1011 0110 0001 0101 1100 0101 0000 0000 0000 0000 0000 0001 // 0x50 df b6 15 c5 00 00 01\n0101 0000 1101 1111 1011 0110 0001 0101 1100 0101 0100 0000 0000 0000 0000 0000 // 0x50 df b6 15 c5 40 00 00\n0101 0000 1101 1111 1011 0110 0001 0101 1100 0101 0100 0000 0000 0000 0000 0001 // 0x50 df b6 15 c5 40 00 01\n```\n\n## Author ##\nWritten by Tom Pawlak - [Blog](https://blog.abelotech.com)\n\n## License ##\n\nCopyright (c) 2014 Tom Pawlak\n\nMIT License : https://blog.abelotech.com/mit-license/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FT-PWK%2Fflake-idgen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FT-PWK%2Fflake-idgen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FT-PWK%2Fflake-idgen/lists"}