mocha-flake-reporter 0.0.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of mocha-flake-reporter might be problematic. Click here for more details.
- package/.istanbul.yml +3 -0
- package/.sonarcloud.properties +1 -0
- package/CHANGELOG.md +5 -0
- package/LICENSE +20 -0
- package/README.md +249 -0
- package/flake-reporter.js +124 -0
- package/mocha.py +3494 -0
- package/package.json +19 -6
- package/test/test.js +147 -0
- package/index.js +0 -1
package/.istanbul.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
|
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 Tom Pawlak
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
@@ -0,0 +1,249 @@
|
|
1
|
+
Flake ID Generator
|
2
|
+
===========
|
3
|
+
[![Build Status](https://travis-ci.org/T-PWK/flake-idgen.svg?branch=master)](https://travis-ci.org/T-PWK/flake-idgen)
|
4
|
+
[![npm version](https://badge.fury.io/js/flake-idgen.svg)](http://badge.fury.io/js/flake-idgen)
|
5
|
+
[![npm downloads](https://img.shields.io/npm/dm/flake-idgen.svg)](https://www.npmjs.com/package/flake-idgen)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/T-PWK/flake-idgen/badges/gpa.svg)](https://codeclimate.com/github/T-PWK/flake-idgen)
|
7
|
+
[![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)
|
8
|
+
[![GitHub issues](https://img.shields.io/github/issues/T-PWK/flake-idgen.svg)](https://github.com/T-PWK/flake-idgen/issues)
|
9
|
+
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](http://blog.tompawlak.org/mit-license)
|
10
|
+
|
11
|
+
Flake ID generator yields k-ordered, conflict-free ids in a distributed environment.
|
12
|
+
|
13
|
+
## Installation ##
|
14
|
+
$ npm install --save flake-idgen ⏎
|
15
|
+
|
16
|
+
## Flake Numbers Format ##
|
17
|
+
|
18
|
+
The Flake ID is made up of: `timestamp`, `datacenter`, `worker` and `counter`. Examples in the following table:
|
19
|
+
```
|
20
|
+
+-------------+------------+--------+---------+--------------------+
|
21
|
+
| Timestamp | Datacenter | Worker | Counter | Flake ID |
|
22
|
+
+-------------+------------+--------+---------+--------------------+
|
23
|
+
| 0x8c20543b0 | 00000b | 00000b | 0x000 | 0x02308150ec000000 |
|
24
|
+
+-------------+------------+--------+---------+--------------------+
|
25
|
+
| 0x8c20543b1 | 00000b | 00000b | 0x000 | 0x02308150ec400000 |
|
26
|
+
+-------------+------------+--------+---------+--------------------+
|
27
|
+
| 0x8c20543b1 | 00000b | 00000b | 0x001 | 0x02308150ec400001 |
|
28
|
+
+-------------+------------+--------+---------+--------------------+
|
29
|
+
| 0x8c20543b1 | 00000b | 00000b | 0x002 | 0x02308150ec400002 |
|
30
|
+
+-------------+------------+--------+---------+--------------------+
|
31
|
+
| 0x8c20543b1 | 00000b | 00000b | 0x003 | 0x02308150ec400003 |
|
32
|
+
+-------------+------------+--------+---------+--------------------+
|
33
|
+
| 0x8c20c0335 | 00011b | 00001b | 0x000 | 0x02308300cd461000 |
|
34
|
+
+-------------+------------+--------+---------+--------------------+
|
35
|
+
| 0x8c20c0335 | 00011b | 00001b | 0x001 | 0x02308300cd461001 |
|
36
|
+
+-------------+------------+--------+---------+--------------------+
|
37
|
+
```
|
38
|
+
|
39
|
+
As you can see, each Flake ID is 64 bits long, consisting of:
|
40
|
+
* `timestamp`, a 42 bit long number of milliseconds elapsed since 1 January 1970 00:00:00 UTC
|
41
|
+
* `datacenter`, a 5 bit long datacenter identifier. It can take up to 32 unique values (including 0)
|
42
|
+
* `worker`, a 5 bit long worker identifier. It can take up to 32 unique values (including 0)
|
43
|
+
* `counter`, a 12 bit long counter of ids in the same millisecond. It can take up to 4096 unique values.
|
44
|
+
|
45
|
+
Breakdown of bits for an id e.g. `5828128208445124608` (counter is `0`, datacenter is `7` and worker `3`) is as follows:
|
46
|
+
```
|
47
|
+
010100001110000110101011101110100001000111 00111 00011 000000000000
|
48
|
+
|------------| 12 bit counter
|
49
|
+
|-----| 5 bit worker
|
50
|
+
|-----| 5 bit datacenter
|
51
|
+
|----- -----| 10 bit generator identifier
|
52
|
+
|------------------------------------------| 42 bit timestamp
|
53
|
+
```
|
54
|
+
|
55
|
+
Note 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).
|
56
|
+
|
57
|
+
## Usage ##
|
58
|
+
|
59
|
+
Flake 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.
|
60
|
+
|
61
|
+
Flake 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`.
|
62
|
+
|
63
|
+
The following example uses `next` with no callback function:
|
64
|
+
|
65
|
+
```js
|
66
|
+
var FlakeId = require('flake-idgen');
|
67
|
+
var flakeIdGen = new FlakeId();
|
68
|
+
|
69
|
+
console.log(flakeIdGen.next());
|
70
|
+
console.log(flakeIdGen.next());
|
71
|
+
console.log(flakeIdGen.next());
|
72
|
+
```
|
73
|
+
|
74
|
+
It would give something like:
|
75
|
+
```
|
76
|
+
<Buffer 50 dd d5 99 01 c0 00 00>
|
77
|
+
<Buffer 50 dd d5 99 02 80 00 00>
|
78
|
+
<Buffer 50 dd d5 99 02 80 00 01>
|
79
|
+
```
|
80
|
+
|
81
|
+
The following example uses `next` with callback function:
|
82
|
+
```js
|
83
|
+
var FlakeId = require('flake-idgen');
|
84
|
+
var flakeIdGen = new FlakeId();
|
85
|
+
|
86
|
+
flakeIdGen.next(function (err, id) {
|
87
|
+
console.info(id);
|
88
|
+
})
|
89
|
+
|
90
|
+
flakeIdGen.next(function (err, id) {
|
91
|
+
console.info(id);
|
92
|
+
})
|
93
|
+
```
|
94
|
+
|
95
|
+
It would give something like:
|
96
|
+
```
|
97
|
+
<Buffer 50 dd d6 49 ef c0 00 00>
|
98
|
+
<Buffer 50 dd d6 49 f0 00 00 00>
|
99
|
+
```
|
100
|
+
|
101
|
+
### Counter overflow ###
|
102
|
+
Flake 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:
|
103
|
+
* When using `next()` without a callback function, an error is thrown.
|
104
|
+
* When using `next(cb)` with a callback function, the callback function is called in the following millisecond without any error.
|
105
|
+
|
106
|
+
### Additional generator setup parameters ###
|
107
|
+
Flake Id generator constructor takes optional parameter (generator configuration options) with the following properties:
|
108
|
+
* `datacenter` (5 bit) - datacenter identifier. It can have values from 0 to 31.
|
109
|
+
* `worker` (5 bit) - worker identifier. It can have values from 0 to 31.
|
110
|
+
* `id` (10 bit) - generator identifier. It can have values from 0 to 1023. It can be provided instead of `datacenter` and `worker` identifiers.
|
111
|
+
* `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.
|
112
|
+
|
113
|
+
Example of using `datacenter` and `worker` identifiers:
|
114
|
+
```js
|
115
|
+
var FlakeId = require('flake-idgen')
|
116
|
+
|
117
|
+
var flakeIdGen1 = new FlakeId();
|
118
|
+
var flakeIdGen2 = new FlakeId({ datacenter: 9, worker: 7 });
|
119
|
+
|
120
|
+
console.info(flakeIdGen1.next());
|
121
|
+
console.info(flakeIdGen2.next());
|
122
|
+
```
|
123
|
+
|
124
|
+
It would give something like:
|
125
|
+
```
|
126
|
+
<Buffer 50 dd da 8f 43 40 00 00>
|
127
|
+
<Buffer 50 dd da 8f 43 d2 70 00>
|
128
|
+
```
|
129
|
+
|
130
|
+
Example of using `epoch` parameter:
|
131
|
+
```js
|
132
|
+
var FlakeId = require('flake-idgen')
|
133
|
+
|
134
|
+
var flakeIdGen1 = new FlakeId();
|
135
|
+
var flakeIdGen2 = new FlakeId({ epoch: 1300000000000 });
|
136
|
+
|
137
|
+
console.info(flakeIdGen1.next());
|
138
|
+
console.info(flakeIdGen2.next());
|
139
|
+
```
|
140
|
+
|
141
|
+
It would give something like:
|
142
|
+
```
|
143
|
+
<Buffer 50 dd db 00 d1 c0 00 00>
|
144
|
+
<Buffer 05 32 58 8e d2 40 00 00>
|
145
|
+
```
|
146
|
+
|
147
|
+
### Properties ###
|
148
|
+
Flake Id generator has some properties that can be read from a generator instance:
|
149
|
+
* `datacenter` - returns datacenter number used for generator creation; otherwise it returns `undefined` value.
|
150
|
+
* `worker` - returns worker number used for generator creation; otherwise it returns `undefined` value.
|
151
|
+
* `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.
|
152
|
+
|
153
|
+
Flake Id generator instantiated without any parameter gets `datacenter`, `worker` and `id` values defaulted to zeros.
|
154
|
+
|
155
|
+
```js
|
156
|
+
var FlakeId = require('flake-idgen')
|
157
|
+
|
158
|
+
var flakeIdGen1 = new FlakeId({ id: 100 });
|
159
|
+
var flakeIdGen2 = new FlakeId({ datacenter: 9, worker: 7 });
|
160
|
+
var flakeIdGen3 = new FlakeId();
|
161
|
+
|
162
|
+
console.info(flakeIdGen1.id); // 100
|
163
|
+
console.info(flakeIdGen1.datacenter); // undefined
|
164
|
+
console.info(flakeIdGen1.worker); // undefined
|
165
|
+
|
166
|
+
console.info(flakeIdGen2.datacenter); // 9
|
167
|
+
console.info(flakeIdGen2.worker); // 7
|
168
|
+
console.info(flakeIdGen2.id); // 259
|
169
|
+
|
170
|
+
console.info(flakeIdGen3.datacenter); // 0
|
171
|
+
console.info(flakeIdGen3.worker); // 0
|
172
|
+
console.info(flakeIdGen3.id); // 0
|
173
|
+
```
|
174
|
+
|
175
|
+
It would give something like:
|
176
|
+
|
177
|
+
```js
|
178
|
+
100
|
179
|
+
undefined
|
180
|
+
undefined
|
181
|
+
|
182
|
+
9
|
183
|
+
7
|
184
|
+
295
|
185
|
+
|
186
|
+
0
|
187
|
+
0
|
188
|
+
0
|
189
|
+
```
|
190
|
+
|
191
|
+
### Clock moving backward ###
|
192
|
+
From 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:
|
193
|
+
* When using `next()` without a callback function, an error is thrown.
|
194
|
+
* 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.
|
195
|
+
|
196
|
+
## Formatting ##
|
197
|
+
|
198
|
+
Flake 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.
|
199
|
+
|
200
|
+
```js
|
201
|
+
var intformat = require('biguint-format')
|
202
|
+
, FlakeId = require('flake-idgen')
|
203
|
+
|
204
|
+
var flakeIdGen1 = new FlakeId();
|
205
|
+
var flakeIdGen2 = new FlakeId({ epoch: 1300000000000 });
|
206
|
+
|
207
|
+
console.info(intformat(flakeIdGen1.next(), 'dec'));
|
208
|
+
console.info(intformat(flakeIdGen1.next(), 'hex', { prefix: '0x' }));
|
209
|
+
|
210
|
+
console.info(intformat(flakeIdGen2.next(), 'dec'));
|
211
|
+
console.info(intformat(flakeIdGen2.next(), 'hex', { prefix: '0x' }));
|
212
|
+
```
|
213
|
+
|
214
|
+
It would give something like:
|
215
|
+
```js
|
216
|
+
5827056208820830208 // flakeIdGen1 decimal format
|
217
|
+
0x50dddcbfb5c00001 // flakeIdGen1 hex format
|
218
|
+
|
219
|
+
374461008833413120 // flakeIdGen2 decimal format
|
220
|
+
0x5325a4db6000002 // flakeIdGen2 hex format
|
221
|
+
```
|
222
|
+
|
223
|
+
Generated id could also be converted to binary string format, split into 4 digit groups of 0's and 1's e.g.
|
224
|
+
```js
|
225
|
+
var intformat = require('biguint-format')
|
226
|
+
, idGen = new (require('flake-idgen'))
|
227
|
+
|
228
|
+
for (var i = 0; i < 5; i++) {
|
229
|
+
console.info(intformat(idGen.next(), 'bin', { groupsize: 4 }));
|
230
|
+
};
|
231
|
+
```
|
232
|
+
|
233
|
+
It would give something like:
|
234
|
+
```js
|
235
|
+
0101 0000 1101 1111 1011 0110 0001 0101 1100 0001 0100 0000 0000 0000 0000 0000 // 0x50 df b6 15 c1 40 00 00
|
236
|
+
0101 0000 1101 1111 1011 0110 0001 0101 1100 0101 0000 0000 0000 0000 0000 0000 // 0x50 df b6 15 c5 00 00 00
|
237
|
+
0101 0000 1101 1111 1011 0110 0001 0101 1100 0101 0000 0000 0000 0000 0000 0001 // 0x50 df b6 15 c5 00 00 01
|
238
|
+
0101 0000 1101 1111 1011 0110 0001 0101 1100 0101 0100 0000 0000 0000 0000 0000 // 0x50 df b6 15 c5 40 00 00
|
239
|
+
0101 0000 1101 1111 1011 0110 0001 0101 1100 0101 0100 0000 0000 0000 0000 0001 // 0x50 df b6 15 c5 40 00 01
|
240
|
+
```
|
241
|
+
|
242
|
+
## Author ##
|
243
|
+
Written by Tom Pawlak - [Blog](https://blog.abelotech.com)
|
244
|
+
|
245
|
+
## License ##
|
246
|
+
|
247
|
+
Copyright (c) 2014 Tom Pawlak
|
248
|
+
|
249
|
+
MIT License : https://blog.abelotech.com/mit-license/
|
@@ -0,0 +1,124 @@
|
|
1
|
+
/**
|
2
|
+
* Flake ID generator yields k-ordered, conflict-free ids in a distributed environment.
|
3
|
+
*/
|
4
|
+
(function () {
|
5
|
+
"use strict";
|
6
|
+
|
7
|
+
/**
|
8
|
+
* Represents an ID generator.
|
9
|
+
* @exports FlakeId
|
10
|
+
* @constructor
|
11
|
+
* @param {object=} options - Generator options
|
12
|
+
* @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.
|
13
|
+
* @param {Number} options.datacenter - Datacenter identifier. It can have values from 0 to 31.
|
14
|
+
* @param {Number} options.worker - Worker identifier. It can have values from 0 to 31.
|
15
|
+
* @param {Number} options.epoch - Number used to reduce value of a generated timestamp.
|
16
|
+
* @param {Number} options.seqMask
|
17
|
+
*/
|
18
|
+
var FlakeId = module.exports = function (options) {
|
19
|
+
this.options = options || {};
|
20
|
+
|
21
|
+
// Set generator id from 'id' option or combination of 'datacenter' and 'worker'
|
22
|
+
if (typeof this.options.id !== 'undefined') {
|
23
|
+
/*jslint bitwise: true */
|
24
|
+
this.id = this.options.id & 0x3FF;
|
25
|
+
} else {
|
26
|
+
this.datacenter = (this.options.datacenter || 0) & 0x1F;
|
27
|
+
this.worker = (this.options.worker || 0) & 0x1F;
|
28
|
+
this.id = (this.datacenter) << 5 | this.worker;
|
29
|
+
}
|
30
|
+
this.genId = this.id;
|
31
|
+
this.genId <<= 12; // id generator identifier - will not change while generating ids
|
32
|
+
this.epoch = +this.options.epoch || 0;
|
33
|
+
this.seq = 0;
|
34
|
+
this.lastTime = 0;
|
35
|
+
this.overflow = false;
|
36
|
+
this.seqMask = this.options.seqMask || 0xFFF;
|
37
|
+
};
|
38
|
+
|
39
|
+
FlakeId.POW10 = Math.pow(2, 10); // 2 ^ 10
|
40
|
+
FlakeId.POW26 = Math.pow(2, 26); // 2 ^ 26
|
41
|
+
|
42
|
+
FlakeId.prototype = {
|
43
|
+
/**
|
44
|
+
* Generates conflice-free id
|
45
|
+
* @param {cb=} callback The callback that handles the response.
|
46
|
+
* @returns {Buffer} Generated id if callback is not provided
|
47
|
+
* @exception if a sequence exceeded its maximum value and a callback function is not provided
|
48
|
+
*/
|
49
|
+
next: function (cb) {
|
50
|
+
/**
|
51
|
+
* This callback receives generated id
|
52
|
+
* @callback callback
|
53
|
+
* @param {Error} error - Error occurred during id generation
|
54
|
+
* @param {Buffer} id - Generated id
|
55
|
+
*/
|
56
|
+
|
57
|
+
var id;
|
58
|
+
if (Buffer.alloc) {
|
59
|
+
id = Buffer.alloc(8);
|
60
|
+
} else {
|
61
|
+
id = new Buffer(8);
|
62
|
+
id.fill(0);
|
63
|
+
}
|
64
|
+
var time = Date.now() - this.epoch;
|
65
|
+
|
66
|
+
// Generates id in the same millisecond as the previous id
|
67
|
+
if (time < this.lastTime) {
|
68
|
+
if (cb) {
|
69
|
+
setTimeout(self.next.bind(self, cb), this.lastTime - time);
|
70
|
+
return;
|
71
|
+
}
|
72
|
+
throw new Error(`Clock moved backwards. Refusing to generate id for ${this.lastTime - time} milliseconds`);
|
73
|
+
}
|
74
|
+
if (time === this.lastTime) {
|
75
|
+
|
76
|
+
// If all sequence values (4096 unique values including 0) have been used
|
77
|
+
// to generate ids in the current millisecond (overflow is true) wait till next millisecond
|
78
|
+
if (this.overflow) {
|
79
|
+
overflowCond(this, cb);
|
80
|
+
return;
|
81
|
+
}
|
82
|
+
|
83
|
+
// Increase sequence counter
|
84
|
+
/*jslint bitwise: true */
|
85
|
+
this.seq = (this.seq + 1) & this.seqMask;
|
86
|
+
|
87
|
+
// sequence counter exceeded its max value (4095)
|
88
|
+
// - set overflow flag and wait till next millisecond
|
89
|
+
if (this.seq === 0) {
|
90
|
+
this.overflow = true;
|
91
|
+
overflowCond(this, cb);
|
92
|
+
return;
|
93
|
+
}
|
94
|
+
} else {
|
95
|
+
this.overflow = false;
|
96
|
+
this.seq = 0;
|
97
|
+
}
|
98
|
+
this.lastTime = time;
|
99
|
+
|
100
|
+
id.writeUInt32BE(((time & 0x3) << 22) | this.genId | this.seq, 4);
|
101
|
+
id.writeUInt8(Math.floor(time / 4) & 0xFF, 4);
|
102
|
+
id.writeUInt16BE(Math.floor(time / FlakeId.POW10) & 0xFFFF, 2);
|
103
|
+
id.writeUInt16BE(Math.floor(time / FlakeId.POW26) & 0xFFFF, 0);
|
104
|
+
|
105
|
+
if (cb) {
|
106
|
+
process.nextTick(cb.bind(null, null, id));
|
107
|
+
}
|
108
|
+
else {
|
109
|
+
return id;
|
110
|
+
}
|
111
|
+
}
|
112
|
+
};
|
113
|
+
|
114
|
+
function overflowCond(self, cb) {
|
115
|
+
if (cb) {
|
116
|
+
setTimeout(self.next.bind(self, cb), 1);
|
117
|
+
}
|
118
|
+
else {
|
119
|
+
throw new Error('Sequence exceeded its maximum value. Provide callback function to handle sequence overflow');
|
120
|
+
}
|
121
|
+
}
|
122
|
+
|
123
|
+
|
124
|
+
}());
|