flake-idgen
Advanced tools
Comparing version 0.1.4 to 1.0.0
@@ -0,0 +0,0 @@ ## 1.4.0 (Nov 7, 2014) |
@@ -7,2 +7,13 @@ /** | ||
/** | ||
* Represents an ID generator. | ||
* @exports FlakeId | ||
* @constructor | ||
* @param {object=} options - Generator options | ||
* @param {Number} options.id - Generator identifier. It can have values from 0 to 1023. It can be provided instead of <tt>datacenter</tt> and <tt>worker</tt> identifiers. | ||
* @param {Number} options.datacenter - Datacenter identifier. It can have values from 0 to 31. | ||
* @param {Number} options.worker - Worker identifier. It can have values from 0 to 31. | ||
* @param {Number} options.epoch - Number used to reduce value of a generated timestamp. | ||
* @param {Number} options.seqMask | ||
*/ | ||
var FlakeId = module.exports = function (options) { | ||
@@ -13,5 +24,6 @@ this.options = options || {}; | ||
if (typeof this.options.id !== 'undefined') { | ||
/*jslint bitwise: true */ | ||
this.id = this.options.id & 0x3FF; | ||
} else { | ||
this.id = ((this.options.datacenter || 0) & 0x1F) << 5 | ((this.options.worker || 0) & 0x1F) | ||
this.id = ((this.options.datacenter || 0) & 0x1F) << 5 | ((this.options.worker || 0) & 0x1F); | ||
} | ||
@@ -30,3 +42,16 @@ this.id <<= 12; // id generator identifier - will not change while generating ids | ||
FlakeId.prototype = { | ||
/** | ||
* Generates conflice-free id | ||
* @param {cb=} callback The callback that handles the response. | ||
* @returns {Buffer} Generated id if callback is not provided | ||
* @exception if a sequence exceeded its maximum value and a callback function is not provided | ||
*/ | ||
next: function (cb) { | ||
/** | ||
* This callback receives generated id | ||
* @callback callback | ||
* @param {Error} error - Error occurred during id generation | ||
* @param {Buffer} id - Generated id | ||
*/ | ||
var id = new Buffer(8), time = Date.now() - this.epoch; | ||
@@ -41,12 +66,8 @@ id.fill(0); | ||
if (this.overflow) { | ||
if (cb) { | ||
setTimeout(this.next.bind(this, cb), 1); | ||
return; | ||
} | ||
else { | ||
throw new Error('Sequence exceeded its maximum value. Provide callback function to handle sequence overflow'); | ||
} | ||
overflowCond(this, cb); | ||
return; | ||
} | ||
// Increase sequence counter | ||
/*jslint bitwise: true */ | ||
this.seq = (this.seq + 1) & this.seqMask; | ||
@@ -58,9 +79,4 @@ | ||
this.overflow = true; | ||
if (cb) { | ||
setTimeout(this.next.bind(this, cb), 1); | ||
return; | ||
} | ||
else { | ||
throw new Error('Sequence exceeded its maximum value. Provide callback function to handle sequence overflow'); | ||
} | ||
overflowCond(this, cb); | ||
return; | ||
} | ||
@@ -87,2 +103,12 @@ } else { | ||
function overflowCond(self, cb) { | ||
if (cb) { | ||
setTimeout(self.next.bind(self, cb), 1); | ||
} | ||
else { | ||
throw new Error('Sequence exceeded its maximum value. Provide callback function to handle sequence overflow'); | ||
} | ||
} | ||
}()); |
{ | ||
"name": "flake-idgen", | ||
"version": "0.1.4", | ||
"version": "1.0.0", | ||
"description": "Flake ID generator yields k-ordered, conflict-free ids in a distributed environment", | ||
"main": "flake-id-gen.js", | ||
"scripts": { | ||
"test": "node test.js" | ||
"test": "istanbul cover _mocha --report lcovonly" | ||
}, | ||
@@ -30,3 +30,3 @@ "repository": { | ||
"type": "MIT", | ||
"url": "http://tompawlak.blogspot.com/p/mit.html" | ||
"url": "http://blog.tompawlak.org/mit-license" | ||
} | ||
@@ -33,0 +33,0 @@ ], |
Flake ID Generator | ||
=========== | ||
[![Build Status](https://travis-ci.org/T-PWK/flake-idgen.png?branch=master)](https://travis-ci.org/T-PWK/flake-idgen) [![NPM version](https://badge.fury.io/js/flake-idgen.png)](http://badge.fury.io/js/flake-idgen) [![Dependency Status](https://gemnasium.com/T-PWK/biguint-format.svg)](https://gemnasium.com/T-PWK/biguint-format) [![Coverage Status](https://coveralls.io/repos/T-PWK/flake-idgen/badge.png)](https://coveralls.io/r/T-PWK/flake-idgen) | ||
[![Build Status](https://travis-ci.org/T-PWK/flake-idgen.svg?branch=master)](https://travis-ci.org/T-PWK/flake-idgen) [![npm version](https://badge.fury.io/js/flake-idgen.svg)](http://badge.fury.io/js/flake-idgen) [![Dependency Status](https://gemnasium.com/T-PWK/biguint-format.svg)](https://gemnasium.com/T-PWK/biguint-format) [![Code Climate](https://codeclimate.com/github/T-PWK/flake-idgen/badges/gpa.svg)](https://codeclimate.com/github/T-PWK/flake-idgen) [![Test Coverage](https://codeclimate.com/github/T-PWK/flake-idgen/badges/coverage.svg)](https://codeclimate.com/github/T-PWK/flake-idgen) | ||
@@ -5,0 +5,0 @@ Flake ID generator yields k-ordered, conflict-free ids in a distributed environment. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
17915
163
0
9