Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

flake-idgen

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

flake-idgen - npm Package Compare versions

Comparing version 1.2.0 to 1.3.0

9

flake-id-gen.js

@@ -26,5 +26,8 @@ /**

} else {
this.id = ((this.options.datacenter || 0) & 0x1F) << 5 | ((this.options.worker || 0) & 0x1F);
this.datacenter = (this.options.datacenter || 0) & 0x1F;
this.worker = (this.options.worker || 0) & 0x1F;
this.id = (this.datacenter) << 5 | this.worker;
}
this.id <<= 12; // id generator identifier - will not change while generating ids
this.genId = this.id;
this.genId <<= 12; // id generator identifier - will not change while generating ids
this.epoch = +this.options.epoch || 0;

@@ -91,3 +94,3 @@ this.seq = 0;

id.writeUInt32BE(((time & 0x3) << 22) | this.id | this.seq, 4);
id.writeUInt32BE(((time & 0x3) << 22) | this.genId | this.seq, 4);
id.writeUInt8(Math.floor(time / 4) & 0xFF, 4);

@@ -94,0 +97,0 @@ id.writeUInt16BE(Math.floor(time / FlakeId.POW10) & 0xFFFF, 2);

{
"name": "flake-idgen",
"version": "1.2.0",
"version": "1.3.0",
"description": "Flake ID generator yields k-ordered, conflict-free ids in a distributed environment",

@@ -5,0 +5,0 @@ "main": "flake-id-gen.js",

@@ -141,2 +141,46 @@ Flake ID Generator

### Properties ###
Flake Id generator has some properties that can be read from a generator instance:
* `datacenter` - returns worker number used for generator creation; otherwise it returns `undefined` value.
* `worker` - returns worker number used for generator creation; otherwise it returns `undefined` value.
* `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.
Flake Id generator instantiated without any parameter gets `datacenter`, `worker` and `id` values defaulted to zeros.
```js
var FlakeId = require('flake-idgen')
var flakeIdGen1 = new FlakeId({ id: 100 });
var flakeIdGen2 = new FlakeId({ datacenter: 9, worker: 7 });
var flakeIdGen3 = new FlakeId();
console.info(flakeIdGen1.id); // 100
console.info(flakeIdGen1.datacenter); // undefined
console.info(flakeIdGen1.worker); // undefined
console.info(flakeIdGen2.datacenter); // 9
console.info(flakeIdGen2.worker); // 7
console.info(flakeIdGen2.id); // 259
console.info(flakeIdGen3.datacenter); // 0
console.info(flakeIdGen3.worker); // 0
console.info(flakeIdGen3.id); // 0
```
It would give something like:
```js
100
undefined
undefined
9
7
295
0
0
0
```
### Formatting ###

@@ -143,0 +187,0 @@

@@ -1,90 +0,147 @@

"use strict";
'use strict'
var assert = require('assert'),
FlakeId = require('../flake-id-gen');
var assert = require('assert'),
FlakeId = require('../flake-id-gen')
describe('FlakeId', function () {
var idGen = new FlakeId()
var idGen = new FlakeId();
describe('next', function () {
it('should return unique id when callback is not present', function () {
testSynch(idGen, 1000)
})
describe('next', function () {
it('should return unique id when callback is not present', function () {
testSynch(idGen, 1000);
});
it('should return unique ids when callback is present', function () {
this.slow(200);
testWithCallback(idGen, 5000);
});
});
});
it('should return unique ids when callback is present', function () {
this.slow(200)
testWithCallback(idGen, 5000)
})
})
describe('property id', function () {
it('should return default value (0)', function () {
assert.equal(idGen.id, 0)
})
})
describe('property datacenter', function () {
it('should return default value (0)', function () {
assert.equal(idGen.datacenter, 0)
})
})
describe('property worker', function () {
it('should return defaulted value (0)', function () {
assert.equal(idGen.worker, 0)
})
})
})
describe('FlakeId({id:0x100})', function () {
var idGen = new FlakeId({ id: 0x100 })
var idGen = new FlakeId({id:0x100});
describe('next', function () {
it('should return unique id when callback is not present', function () {
testSynch(idGen, 1000)
})
it('should return unique ids when callback is present', function () {
this.slow(200)
testWithCallback(idGen, 5000)
})
})
describe('next', function () {
it('should return unique id when callback is not present', function () {
testSynch(idGen, 1000);
});
it('should return unique ids when callback is present', function () {
this.slow(200);
testWithCallback(idGen, 5000);
});
});
});
describe('property id', function () {
it('should return id value used to create generator', function () {
assert.equal(idGen.id, 0x100)
})
})
describe('property datacenter', function () {
it("should return 'undefined'", function () {
assert.equal(typeof idGen.datacenter, 'undefined')
})
})
describe('property worker', function () {
it("should return 'undefined'", function () {
assert.equal(typeof idGen.worker, 'undefined')
})
})
})
describe('FlakeId({seqMask:0x0F})', function () {
var idGen = new FlakeId({ seqMask: 0x0f })
var idGen = new FlakeId({seqMask:0x0F});
describe('next', function () {
it('should return unique id when callback is not present', function () {
// Maximum unique ids depends on seqMask - 16 in this case
testSynch(idGen, 16)
})
describe('next', function () {
it('should return unique id when callback is not present', function () {
// Maximum unique ids depends on seqMask - 16 in this case
testSynch(idGen, 16);
});
it('should return unique ids when callback is present', function () {
this.slow(200);
testWithCallback(idGen, 1000);
});
it('should return unique ids when callback is present', function () {
this.slow(200)
testWithCallback(idGen, 1000)
})
it('should throw an exception if counter has been exceeded and callback is not present', function () {
assert.throws(function () {
testSynch(idGen, 100);
});
});
});
});
it('should throw an exception if counter has been exceeded and callback is not present', function () {
assert.throws(function () {
testSynch(idGen, 100)
})
})
})
})
describe('FlakeId({datacenter: 0x0A, worker: 0x15})', function () {
var idGen = new FlakeId({ datacenter: 0x0a, worker: 0x15 })
describe('id property', function () {
it('should return value generated from datacenter and worker', function () {
assert.equal(idGen.id, 0x155)
})
})
describe('datacenter property', function () {
it('should return datacenter number used to create generator', function () {
assert.equal(idGen.datacenter, 10)
})
})
describe('worker property', function () {
it('should return worker number used to create generator', function () {
assert.equal(idGen.worker, 21)
})
})
})
function testSynch(generator, howMany) {
var ids = new Array(howMany), i;
var ids = new Array(howMany),
i
for(i = 0; i < ids.length; i++) {
ids[i] = generator.next().toString('hex');
}
for (i = 0; i < ids.length; i++) {
ids[i] = generator.next().toString('hex')
}
for(i = 0; i < ids.length - 1; i++) {
assert.notEqual(ids[i], ids[i+1]); // Two sibling ids are not equal
assert.ok(ids[i] < ids[i+1]); // Each id is greater than an id generated before
}
for (i = 0; i < ids.length - 1; i++) {
assert.notEqual(ids[i], ids[i + 1]) // Two sibling ids are not equal
assert.ok(ids[i] < ids[i + 1]) // Each id is greater than an id generated before
}
}
function testWithCallback(generator, howMany) {
var ids = new Array(howMany), i, index = 0;
var ids = new Array(howMany),
i,
index = 0
for(i = 0; i < ids.length; i++) {
generator.next(function (err, id) {
assert.ifError(err);
ids[index++] = id.toString('hex');
for (i = 0; i < ids.length; i++) {
generator.next(function (err, id) {
assert.ifError(err)
ids[index++] = id.toString('hex')
if(index === ids.length) {
for(i = 0; i < ids.length - 1; i++) {
assert.notEqual(ids[i], ids[i+1]); // Two sibling ids are not equal
assert.ok(ids[i] < ids[i+1]); // Each id is greater than an id generated before
}
}
});
}
}
if (index === ids.length) {
for (i = 0; i < ids.length - 1; i++) {
assert.notEqual(ids[i], ids[i + 1]) // Two sibling ids are not equal
assert.ok(ids[i] < ids[i + 1]) // Each id is greater than an id generated before
}
}
})
}
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc