-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
96 lines (79 loc) · 2.73 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import * as crypto from 'crypto';
const regexp = /(.{8})(.{4})(.{4})(.{4})(.{12})/;
const VERSION = 0xF0000000;
const SECONDS_SINCE_GREGORIAN_EPOCH = 12219292800;
const MAX_ID_PER_MILLISECONDS = 655360000;
class GeneratorBusy extends Error {
constructor() {
super("Identifier generation is too frequently");
}
}
export class Generator {
private counter: number;
private counterBegin: number;
private node: Buffer;
private lastTimestamp: number;
constructor(node?: Buffer) {
if (node && node.length >= 6)
this.node = node.slice(0,6);
else
this.node = crypto.pseudoRandomBytes(6);
this.lastTimestamp = 0;
this.counter = 0;
this.counterBegin = MAX_ID_PER_MILLISECONDS - 1;
}
generateSync(): Identifier{
let milliseconds = Date.now();
if (milliseconds == this.lastTimestamp) {
if (this.counter == MAX_ID_PER_MILLISECONDS)
throw new GeneratorBusy();
} else {
this.counter = 0;
}
this.lastTimestamp = milliseconds;
let counter = this.counter++;
let nanoseconds = milliseconds % 1000;
let seconds = (milliseconds - nanoseconds) / 1000 + SECONDS_SINCE_GREGORIAN_EPOCH;
nanoseconds *= 10000;
nanoseconds += counter >>> 16;
counter &= 0xFFFF;
return new Identifier(seconds, nanoseconds, counter, this.node);
}
generate(): Promise<Identifier> {
try {
return Promise.resolve(this.generateSync())
} catch (e) {
if (e instanceof GeneratorBusy)
return new Promise((done)=> {
setTimeout(done, 0);
}).then(() => this.generate());
else
Promise.reject(e);
}
}
}
export class Identifier {
private value: Buffer;
constructor (seconds: number, chns: number, counter: number, node: Buffer) {
let v = seconds % 0x10000000;
let u = (seconds - v) / 0x10000000;
let w = chns;
let n = (v * 10000000 + w) % 0x10000000;
let m = (v * 10000000 + w - n) / 0x10000000;
let high = m + u*10000000;
let low = n;
this.value = new Buffer(16);
this.value.writeUInt32BE(high, 0); // High 32bit
this.value.writeUInt16BE(low >>> 12, 4); //Middle 16bit
this.value.writeUInt32BE((VERSION | ((low & 0xFFF)<< 16) | counter) >>> 0, 6);
if (node == null || node.length < 6)
node = crypto.pseudoRandomBytes(6);
node.copy(this.value, 10, 0, 6);
}
toBuffer(): Buffer {
return this.value;
}
toString(): string{
return this.value.toString('hex').replace(regexp, '$1-$2-$3-$4-$5');
}
}