-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtars-uuid-generator.js
More file actions
130 lines (122 loc) · 3.21 KB
/
tars-uuid-generator.js
File metadata and controls
130 lines (122 loc) · 3.21 KB
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
/**
* Tencent is pleased to support the open source community by making Tars available.
*
* Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
'use strict';
var os = require("os");
function UuidGenerator(){
this.ip = 0;
this.pid = 0;
this.seq = 0;
this.initOK = false;
this.uuidBuf = "allocUnsafe" in Buffer ? Buffer.allocUnsafe(16) : new Buffer(16)
this.init("")
}
//最大 uint32,超过后写入会报错
UuidGenerator.MAX_SEQ = 4294967295;
/**
* 初始化uuid生成器
* @param {*} sIP
* @returns
*/
UuidGenerator.prototype.init = function(sIP){
if (this.isIPV4(sIP))
{
this.ip = this.ipv4Toint(sIP);
} else {
this.ip = this.ipv4Toint(this.getLocalIP());
}
if (this.ip == 0)
{
this.initOK = false;
return;
}
this.pid = process.pid;
this.seq = 0;
this.uuidBuf.writeUInt32BE(this.ip);
this.uuidBuf.writeUInt32BE(this.pid, 4);
this.initOK = true;
}
/**
* 判断是否是ipv4字符
* @param {*} sIp
* @returns
*/
UuidGenerator.prototype.isIPV4 = function(sIp){
if(!sIp) return false
var vs = sIp.split(".");
if (vs.length != 4)
{
return false;
}
for (var i = 0; i < vs.length; i++)
{
if(isNaN(vs[i])) return false;
var vsItem = Number(vs[i]);
if(vsItem < 0 ||vsItem > 255)
{
return false;
}
}
return true;
}
/**
* ipv4字符转int
* @param {*} sIp
* @returns
*/
UuidGenerator.prototype.ipv4Toint = function(sIp){
var vs = sIp.split(".");
if (vs.length != 4)
{
return 0;
}
var ipInt = 0;
for (var i = 3; i >= 0; i--)
{
if(isNaN(vs[i])) return 0;
var vsItem = Number(vs[i]);
if (vsItem < 0 || vsItem > 255)
{
return 0;
}
else
{
ipInt = ((ipInt << 8) + vsItem) >>> 0;
}
}
return ipInt;
}
UuidGenerator.prototype.getLocalIP = function(){
var interfaces = os.networkInterfaces();
for(var key in interfaces){
var face = interfaces[key];
for(var i =0; i < face.length; i++){
let alias = face[i];
if (alias.family === "IPv4" && alias.address !== "127.0.0.1" && !alias.internal) {
return alias.address;
}
}
}
return "127.0.0.1";
}
UuidGenerator.prototype.genID = function(){
if(!this.initOK) return "";
this.uuidBuf.writeUInt32BE(new Date().getTime() / 1000, 8);
if(this.seq >= UuidGenerator.MAX_SEQ) this.seq = 0;
this.uuidBuf.writeUInt32BE(this.seq, 12);
this.seq++;
return this.uuidBuf.toString("hex");
}
module.exports = new UuidGenerator()