-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
216 lines (164 loc) · 4.21 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
var netif = require('netif')
var def = {}
def.time = {
s: 0
, e: 8
}
def.seq = {
s: 8
, e: 10
}
def.mac = {
s: 10
, e: 16
}
function randomMac() {
var mac = ''
for (var i = 0; i < 12; i+=1) {
if (i !== 0 && !(i % 2)) mac += ':'
mac += Math.floor(Math.random() * 16).toString(16)
}
return mac
}
function padLeft(str, length) {
for (var i = str.length; i < length; i+=1) {
str = '0' + str
}
return str
}
function getMacBuffer(mac) {
var macBuf = new Buffer(6)
, mac = mac.split(':')
, tmp
for (var i = 0; i < 6; i+=1) {
tmp = parseInt(mac[i], 16)
macBuf.writeUInt8(tmp, i)
}
return macBuf
}
function con(opts) {
opts = opts || {}
var lastTimestamp = 0
, sequence = 0
, maxSequence = 32 * 1024 - 1
, macRegex = /^(?:[a-f0-9]{1,2}[:\-]){5}[a-f0-9]{1,2}$/i
, hexRegex = /^[0-9a-fA-F]{27}$/
, validMac = macRegex.test(opts.mac)
, mac = opts.mac && validMac ? opts.mac : randomMac()
if (opts.mac && !validMac) throw new Error(opts.mac + ' is not a valid mac address')
if (opts.interface) mac = netif.getMacAddress(opts.interface)
mac = getMacBuffer(mac)
function Unid(id) {
if (!(this instanceof Unid)) return new Unid(id)
if (id) {
if (!Buffer.isBuffer(id) || typeof id === 'string') {
id = Unid.createFromHexString(id)
}
} else {
id = Unid.generate()
}
this._id = id
}
Unid.generate = function () {
var now = Date.now()
, max = lastTimestamp
if (now < max) {
console.error('WARNING: Your time has drifted backwards!')
}
if (now === max) {
sequence += 1
if (sequence > maxSequence) {
sequence = 0
}
} else {
sequence = 0
}
if (now > max) {
lastTimestamp = now
}
var id = new Buffer(16)
id.writeDoubleBE(now, def.time.s)
id.writeInt16BE(sequence, def.seq.s)
mac.copy(id, def.mac.s, 0)
return id
}
Unid.createFromHexString = function (hexStr) {
var id = new Buffer(16)
, tmpStr = ''
, tmpNum = 0
// Timestamp
tmpStr = hexStr.slice(0, 11)
tmpNum = parseInt(tmpStr, 16)
id.writeDoubleBE(tmpNum, def.time.s)
// Sequence
tmpStr = hexStr.slice(11, 15)
tmpNum = parseInt(tmpStr, 16)
id.writeInt16BE(tmpNum, def.seq.s)
// Mac address
tmpStr = hexStr.slice(15)
for (var i = 0; i < 6; i+=1) {
tmpNum = parseInt(tmpStr.slice(i * 2, (i * 2) + 2), 16)
id.writeUInt8(tmpNum, i + def.mac.s)
}
return id
}
Unid.prototype.getNumber = function (section) {
var bin
if (section === 'timestamp') {
bin = this._id.readDoubleBE(def.time.s, def.time.e)
}
if (section === 'sequence') {
bin = this._id.readInt16BE(def.seq.s)
}
return bin
}
Unid.prototype.getHex = function (section) {
var hex = ''
if (section === 'timestamp') {
hex = this.getNumber(section).toString(16)
}
if (section === 'sequence') {
hex = this.getNumber(section).toString(16)
if (hex.length < 4) hex = padLeft(hex, 4)
}
if (section === 'mac') {
var tmp = ''
for (var i = 0; i < 6; i+=1) {
tmp = this._id.readUInt8(def.mac.s + i).toString(16)
if (tmp.length < 4) tmp = padLeft(tmp, 2)
hex += tmp
}
}
return hex
}
Unid.prototype.toHexString = function () {
if (this._hexStr) return this._hexStr
var str = ''
str += this.getHex('timestamp')
str += this.getHex('sequence')
str += this.getHex('mac')
return this._hexStr = str
}
Unid.prototype.toString = function () {
return this.toHexString()
}
Unid.prototype.toJSON = function () {
return this.toHexString()
}
Unid.prototype.inspect = Unid.prototype.toString
Unid.prototype.equals = function (otherId) {
var id = otherId instanceof Unid ? otherId.toString() : (new Unid(otherId)).toString()
return this.toString() === id
}
Unid.prototype.getSequence = function () {
return this.getNumber('sequence')
}
Unid.prototype.getTimestamp = function () {
return new Date(this.getNumber('timestamp'))
}
return Unid
}
con.randomMac = randomMac
con.padLeft = padLeft
con.getMacBuffer = getMacBuffer
module.exports = con