/
index.js
130 lines (101 loc) · 2.5 KB
/
index.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* global BigInt */
'use strict'
const Hashids = require('hashids/cjs')
class ShortDUID {
constructor (shardId, salt, epochStart) {
if (!(this instanceof ShortDUID)) {
return new ShortDUID(shardId, salt, epochStart)
}
const duid = this
duid.shardId = BigInt.asUintN(10, BigInt(shardId))
duid.salt = salt
const now = BigInt(+new Date())
duid.epochStart = BigInt(epochStart)
if (now < duid.epochStart) {
duid.epochStart = 0n
}
duid.timeDrift = 0n
duid.hashids = new Hashids(duid.salt)
duid.sequence = 0n
duid.tsSequence = []
return duid
}
getDUID (count) {
const ret = []
let cnt
if (count === 0) {
return []
}
if (count > 8192 || count < 1) {
cnt = 1
} else {
cnt = count
}
for (let i = 0; i < cnt; i++) {
ret.push(this.hashids.encodeHex(this.getID().toString(16)))
}
return ret
}
getDUIDInt (count) {
const ret = []
let cnt
if (count === 0) {
return []
}
if (count > 8192 || count < 0) {
cnt = 1
} else {
cnt = count
}
for (let i = 0; i < cnt; i++) {
ret.push(this.getID().toString(10))
}
return ret
}
getID () {
const duid = this
// Calculate time part
let now = BigInt.asUintN(
42,
BigInt(+new Date()) - (duid.epochStart + duid.timeDrift)
)
// Calculate shard id part
const shid = duid.shardId << 12n // Shift left by 12 bit
// Calculate sequence part
const seq = BigInt.asUintN(12, ++duid.sequence)
if (
typeof duid.tsSequence[seq] !== 'undefined' &&
duid.tsSequence[seq] >= now
) {
// Overflowing or drifting backwards
now = BigInt.asUintN(42, ++duid.tsSequence[seq])
}
duid.tsSequence[seq] = now
now <<= 22n
// Calculate final ID
return now | shid | seq
}
getShardID () {
return parseInt(this.shardId.toString(10), 10)
}
getEpochStart () {
return this.epochStart.toString(10)
}
getSalt () {
return this.salt
}
getCurrentTimeMs () {
const duid = this
const estart = duid.epochStart
let now = BigInt(+new Date()) // Current system time in milliseconds
now = BigInt.asUintN(42, now - estart) // Calculate custom epoch and add/subtract drift time
return now.toString(10)
}
driftTime (drift) {
if (typeof drift !== 'undefined') {
this.timeDrift = BigInt(Math.round(drift))
}
return parseInt(this.timeDrift.toString(), 10)
}
}
exports.init = ShortDUID