@cdlab996/genid 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +295 -0
- package/dist/index.cjs +470 -0
- package/dist/index.d.cts +316 -0
- package/dist/index.d.ts +316 -0
- package/dist/index.js +469 -0
- package/package.json +66 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
//#region src/types/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* ID 生成算法类型
|
|
4
|
+
*/
|
|
5
|
+
declare enum GenidMethod {
|
|
6
|
+
/** 漂移算法(推荐用于高性能场景) */
|
|
7
|
+
DRIFT = 1,
|
|
8
|
+
/** 传统算法 */
|
|
9
|
+
TRADITIONAL = 2,
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* ID 生成器配置选项
|
|
13
|
+
*/
|
|
14
|
+
interface GenidOptions {
|
|
15
|
+
/** 工作节点/机器 ID(必须,范围 0 到 2^workerIdBitLength-1) */
|
|
16
|
+
workerId: number;
|
|
17
|
+
/** 算法类型(默认:GenidMethod.DRIFT) */
|
|
18
|
+
method?: GenidMethod;
|
|
19
|
+
/** 起始时间戳,单位:毫秒(默认:1577836800000,即 2020-01-01 00:00:00) */
|
|
20
|
+
baseTime?: number;
|
|
21
|
+
/** 工作节点 ID 的位数(范围:1-15,默认:6) */
|
|
22
|
+
workerIdBitLength?: number;
|
|
23
|
+
/** 序列号的位数(范围:3-21,默认:6) */
|
|
24
|
+
seqBitLength?: number;
|
|
25
|
+
/** 最大序列号(默认:2^seqBitLength - 1) */
|
|
26
|
+
maxSeqNumber?: number;
|
|
27
|
+
/** 最小序列号(默认:5,0-4 保留用于时钟回拨处理) */
|
|
28
|
+
minSeqNumber?: number;
|
|
29
|
+
/** 最大漂移次数,超过后等待下一毫秒(默认:2000) */
|
|
30
|
+
topOverCostCount?: number;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* ID 解析结果
|
|
34
|
+
*/
|
|
35
|
+
interface ParseResult {
|
|
36
|
+
/** ID 生成时间(Date 对象) */
|
|
37
|
+
timestamp: Date;
|
|
38
|
+
/** ID 生成时间戳,单位:毫秒 */
|
|
39
|
+
timestampMs: number;
|
|
40
|
+
/** 工作节点 ID */
|
|
41
|
+
workerId: number;
|
|
42
|
+
/** 序列号 */
|
|
43
|
+
sequence: number;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* 统计信息结果(对外暴露,使用 Number 类型)
|
|
47
|
+
*/
|
|
48
|
+
interface StatsResult {
|
|
49
|
+
/** 总生成 ID 数量 */
|
|
50
|
+
totalGenerated: number;
|
|
51
|
+
/** 漂移次数 */
|
|
52
|
+
overCostCount: number;
|
|
53
|
+
/** 时钟回拨次数 */
|
|
54
|
+
turnBackCount: number;
|
|
55
|
+
/** 运行时长,单位:毫秒 */
|
|
56
|
+
uptimeMs: number;
|
|
57
|
+
/** 平均每秒生成 ID 数量 */
|
|
58
|
+
avgPerSecond: number;
|
|
59
|
+
/** 当前状态(OVER_COST: 漂移中, NORMAL: 正常) */
|
|
60
|
+
currentState: 'OVER_COST' | 'NORMAL';
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* 配置信息结果
|
|
64
|
+
*/
|
|
65
|
+
interface ConfigResult {
|
|
66
|
+
/** 算法类型(DRIFT: 漂移算法, TRADITIONAL: 传统算法) */
|
|
67
|
+
method: 'DRIFT' | 'TRADITIONAL';
|
|
68
|
+
/** 当前工作节点 ID */
|
|
69
|
+
workerId: number;
|
|
70
|
+
/** 工作节点 ID 范围(格式:"0-63") */
|
|
71
|
+
workerIdRange: string;
|
|
72
|
+
/** 序列号范围(格式:"5-63") */
|
|
73
|
+
sequenceRange: string;
|
|
74
|
+
/** 最大序列号值 */
|
|
75
|
+
maxSequence: number;
|
|
76
|
+
/** 每毫秒可生成的 ID 数量 */
|
|
77
|
+
idsPerMillisecond: number;
|
|
78
|
+
/** 起始时间(Date 对象) */
|
|
79
|
+
baseTime: Date;
|
|
80
|
+
/** 时间戳占用的位数 */
|
|
81
|
+
timestampBits: number;
|
|
82
|
+
/** 工作节点 ID 占用的位数 */
|
|
83
|
+
workerIdBits: number;
|
|
84
|
+
/** 序列号占用的位数 */
|
|
85
|
+
sequenceBits: number;
|
|
86
|
+
}
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region src/index.d.ts
|
|
89
|
+
/**
|
|
90
|
+
* 优化版 Snowflake ID 生成器
|
|
91
|
+
*
|
|
92
|
+
* 基于 Snowflake 算法的高性能分布式唯一 ID 生成器,支持漂移算法和时钟回拨处理。
|
|
93
|
+
*
|
|
94
|
+
* 特性:
|
|
95
|
+
* - 漂移算法:提升高并发下的性能
|
|
96
|
+
* - 优雅处理时钟回拨,不阻塞生成
|
|
97
|
+
* - 可配置的位长度,灵活性高
|
|
98
|
+
* - 支持传统和漂移两种生成方法
|
|
99
|
+
*
|
|
100
|
+
* ⚠️ 注意:此实例不是线程安全的,每个 Worker/进程应该创建独立的实例并使用不同的 workerId
|
|
101
|
+
*/
|
|
102
|
+
declare class GenidOptimized {
|
|
103
|
+
private method;
|
|
104
|
+
private baseTime;
|
|
105
|
+
private workerId;
|
|
106
|
+
private workerIdBitLength;
|
|
107
|
+
private seqBitLength;
|
|
108
|
+
private maxSeqNumber;
|
|
109
|
+
private minSeqNumber;
|
|
110
|
+
private topOverCostCount;
|
|
111
|
+
private _timestampShift;
|
|
112
|
+
private _currentSeqNumber;
|
|
113
|
+
private _lastTimeTick;
|
|
114
|
+
private _turnBackTimeTick;
|
|
115
|
+
private _turnBackIndex;
|
|
116
|
+
private _isOverCost;
|
|
117
|
+
private _overCostCountInOneTerm;
|
|
118
|
+
private _stats;
|
|
119
|
+
/**
|
|
120
|
+
* 构造函数,初始化 ID 生成器
|
|
121
|
+
*
|
|
122
|
+
* @param {Object} options - 配置选项
|
|
123
|
+
* @param {number} options.workerId - 工作节点/机器 ID(必须,范围 0 到 2^workerIdBitLength-1)
|
|
124
|
+
* @param {GenidMethod} [options.method=GenidMethod.DRIFT] - 算法类型
|
|
125
|
+
* @param {number} [options.baseTime=1577836800000] - 起始时间戳(毫秒,默认:2020-01-01)
|
|
126
|
+
* @param {number} [options.workerIdBitLength=6] - 工作节点 ID 的位数(1-15,默认:6)
|
|
127
|
+
* @param {number} [options.seqBitLength=6] - 序列号的位数(3-21,默认:6)
|
|
128
|
+
* @param {number} [options.maxSeqNumber] - 最大序列号(默认:2^seqBitLength-1)
|
|
129
|
+
* @param {number} [options.minSeqNumber=5] - 最小序列号(默认:5,0-4 保留)
|
|
130
|
+
* @param {number} [options.topOverCostCount=2000] - 最大漂移次数(默认:2000)
|
|
131
|
+
*
|
|
132
|
+
* @throws {Error} 如果缺少 workerId 或配置无效
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* const genid = new GenidOptimized({ workerId: 1 });
|
|
136
|
+
* const id = genid.nextId();
|
|
137
|
+
*/
|
|
138
|
+
constructor(options: GenidOptions);
|
|
139
|
+
/**
|
|
140
|
+
* 初始化配置,设置默认值
|
|
141
|
+
* @private
|
|
142
|
+
* @param {Object} options - 用户提供的配置
|
|
143
|
+
* @returns {Object} 合并后的配置对象
|
|
144
|
+
*/
|
|
145
|
+
private _initConfig;
|
|
146
|
+
/**
|
|
147
|
+
* 验证配置参数的有效性
|
|
148
|
+
* @private
|
|
149
|
+
* @param {Object} config - 配置对象
|
|
150
|
+
* @throws {Error} 如果配置无效
|
|
151
|
+
*/
|
|
152
|
+
private _validateConfig;
|
|
153
|
+
/**
|
|
154
|
+
* 初始化实例变量
|
|
155
|
+
* @private
|
|
156
|
+
* @param {Object} config - 配置对象
|
|
157
|
+
*/
|
|
158
|
+
private _initVariables;
|
|
159
|
+
/**
|
|
160
|
+
* 获取当前时间戳(相对于 baseTime 的毫秒数)
|
|
161
|
+
* @private
|
|
162
|
+
* @returns {bigint} 当前时间戳
|
|
163
|
+
*/
|
|
164
|
+
private _getCurrentTimeTick;
|
|
165
|
+
/**
|
|
166
|
+
* 等待下一毫秒
|
|
167
|
+
* @private
|
|
168
|
+
* @returns {bigint} 下一毫秒时间戳
|
|
169
|
+
*/
|
|
170
|
+
private _getNextTimeTick;
|
|
171
|
+
/**
|
|
172
|
+
* 根据组件计算 ID
|
|
173
|
+
* @private
|
|
174
|
+
* @param {bigint} useTimeTick - 使用的时间戳
|
|
175
|
+
* @returns {bigint} 计算得到的 ID
|
|
176
|
+
*/
|
|
177
|
+
private _calcId;
|
|
178
|
+
/**
|
|
179
|
+
* 计算时钟回拨时的 ID
|
|
180
|
+
* @private
|
|
181
|
+
* @param {bigint} useTimeTick - 使用的时间戳
|
|
182
|
+
* @returns {bigint} 计算得到的 ID
|
|
183
|
+
*/
|
|
184
|
+
private _calcTurnBackId;
|
|
185
|
+
/**
|
|
186
|
+
* 处理漂移情况(漂移算法)
|
|
187
|
+
* @private
|
|
188
|
+
* @returns {bigint} 生成的 ID
|
|
189
|
+
*/
|
|
190
|
+
private _nextOverCostId;
|
|
191
|
+
/**
|
|
192
|
+
* 正常生成 ID
|
|
193
|
+
* @private
|
|
194
|
+
* @returns {bigint} 生成的 ID
|
|
195
|
+
*/
|
|
196
|
+
private _nextNormalId;
|
|
197
|
+
/**
|
|
198
|
+
* 钩子函数:开始漂移操作(可被子类重写)
|
|
199
|
+
* @protected
|
|
200
|
+
* @param {bigint} useTimeTick - 当前时间戳
|
|
201
|
+
*/
|
|
202
|
+
protected _beginOverCostAction(useTimeTick: bigint): void;
|
|
203
|
+
/**
|
|
204
|
+
* 钩子函数:结束漂移操作(可被子类重写)
|
|
205
|
+
* @protected
|
|
206
|
+
* @param {bigint} useTimeTick - 当前时间戳
|
|
207
|
+
*/
|
|
208
|
+
protected _endOverCostAction(useTimeTick: bigint): void;
|
|
209
|
+
/**
|
|
210
|
+
* 钩子函数:开始时钟回拨操作(可被子类重写)
|
|
211
|
+
* @protected
|
|
212
|
+
* @param {bigint} useTimeTick - 当前时间戳
|
|
213
|
+
*/
|
|
214
|
+
protected _beginTurnBackAction(useTimeTick: bigint): void;
|
|
215
|
+
/**
|
|
216
|
+
* 钩子函数:结束时钟回拨操作(可被子类重写)
|
|
217
|
+
* @protected
|
|
218
|
+
* @param {bigint} useTimeTick - 当前时间戳
|
|
219
|
+
*/
|
|
220
|
+
protected _endTurnBackAction(useTimeTick: bigint): void;
|
|
221
|
+
/**
|
|
222
|
+
* 生成下一个 ID
|
|
223
|
+
*
|
|
224
|
+
* @returns {number} 唯一 ID(Number 类型)
|
|
225
|
+
* @throws {Error} 如果 ID 超出 JavaScript 安全整数范围
|
|
226
|
+
*
|
|
227
|
+
* @example
|
|
228
|
+
* const id = genid.nextNumber();
|
|
229
|
+
* console.log(id); // 123456789012345
|
|
230
|
+
*/
|
|
231
|
+
nextNumber(): number;
|
|
232
|
+
/**
|
|
233
|
+
* 生成下一个 ID
|
|
234
|
+
*
|
|
235
|
+
* 如果 ID 在安全范围内返回 Number,否则返回 BigInt
|
|
236
|
+
*
|
|
237
|
+
* @returns {number|bigint} 唯一 ID
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
240
|
+
* const id = genid.nextId();
|
|
241
|
+
* console.log(typeof id); // 'number' 或 'bigint'
|
|
242
|
+
*/
|
|
243
|
+
nextId(): number | bigint;
|
|
244
|
+
/**
|
|
245
|
+
* 生成下一个 ID
|
|
246
|
+
*
|
|
247
|
+
* @returns {bigint} 唯一 ID(BigInt 类型)
|
|
248
|
+
*
|
|
249
|
+
* @example
|
|
250
|
+
* const id = genid.nextBigId();
|
|
251
|
+
* console.log(id); // 123456789012345678n
|
|
252
|
+
*/
|
|
253
|
+
nextBigId(): bigint;
|
|
254
|
+
/**
|
|
255
|
+
* 批量生成 ID
|
|
256
|
+
*
|
|
257
|
+
* @param {number} count - 要生成的 ID 数量
|
|
258
|
+
* @param {boolean} [asBigInt=false] - 是否返回 BigInt 数组
|
|
259
|
+
* @returns {Array<number|bigint>} 唯一 ID 数组
|
|
260
|
+
*
|
|
261
|
+
* @example
|
|
262
|
+
* const ids = genid.nextBatch(100);
|
|
263
|
+
* const bigIds = genid.nextBatch(100, true);
|
|
264
|
+
*/
|
|
265
|
+
nextBatch(count: number, asBigInt?: boolean): Array<number | bigint>;
|
|
266
|
+
/**
|
|
267
|
+
* 解析 ID,提取其组成部分
|
|
268
|
+
*
|
|
269
|
+
* @param {number|bigint|string} id - 要解析的 ID
|
|
270
|
+
* @returns {Object} 解析结果
|
|
271
|
+
* @returns {Date} return.timestamp - 生成时间戳
|
|
272
|
+
* @returns {number} return.timestampMs - 时间戳(毫秒)
|
|
273
|
+
* @returns {number} return.workerId - 工作节点 ID
|
|
274
|
+
* @returns {number} return.sequence - 序列号
|
|
275
|
+
*
|
|
276
|
+
* @example
|
|
277
|
+
* const info = genid.parse(id);
|
|
278
|
+
* console.log(info);
|
|
279
|
+
* // {
|
|
280
|
+
* // timestamp: Date,
|
|
281
|
+
* // timestampMs: 1609459200000,
|
|
282
|
+
* // workerId: 1,
|
|
283
|
+
* // sequence: 42
|
|
284
|
+
* // }
|
|
285
|
+
*/
|
|
286
|
+
parse(id: number | bigint | string): ParseResult;
|
|
287
|
+
/**
|
|
288
|
+
* 获取生成器统计信息
|
|
289
|
+
*
|
|
290
|
+
* @returns {Object} 统计数据
|
|
291
|
+
*
|
|
292
|
+
* @example
|
|
293
|
+
* const stats = genid.getStats();
|
|
294
|
+
* console.log(stats);
|
|
295
|
+
*/
|
|
296
|
+
getStats(): StatsResult;
|
|
297
|
+
/**
|
|
298
|
+
* 重置统计数据
|
|
299
|
+
*/
|
|
300
|
+
resetStats(): void;
|
|
301
|
+
/**
|
|
302
|
+
* 获取配置信息
|
|
303
|
+
*
|
|
304
|
+
* @returns {Object} 配置详情
|
|
305
|
+
*/
|
|
306
|
+
getConfig(): ConfigResult;
|
|
307
|
+
/**
|
|
308
|
+
* 将 ID 格式化为二进制字符串以便调试
|
|
309
|
+
*
|
|
310
|
+
* @param {number|bigint|string} id - 要格式化的 ID
|
|
311
|
+
* @returns {string} 格式化的二进制表示
|
|
312
|
+
*/
|
|
313
|
+
formatBinary(id: number | bigint | string): string;
|
|
314
|
+
}
|
|
315
|
+
//#endregion
|
|
316
|
+
export { GenidOptimized };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
//#region src/types/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* ID 生成算法类型
|
|
4
|
+
*/
|
|
5
|
+
declare enum GenidMethod {
|
|
6
|
+
/** 漂移算法(推荐用于高性能场景) */
|
|
7
|
+
DRIFT = 1,
|
|
8
|
+
/** 传统算法 */
|
|
9
|
+
TRADITIONAL = 2,
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* ID 生成器配置选项
|
|
13
|
+
*/
|
|
14
|
+
interface GenidOptions {
|
|
15
|
+
/** 工作节点/机器 ID(必须,范围 0 到 2^workerIdBitLength-1) */
|
|
16
|
+
workerId: number;
|
|
17
|
+
/** 算法类型(默认:GenidMethod.DRIFT) */
|
|
18
|
+
method?: GenidMethod;
|
|
19
|
+
/** 起始时间戳,单位:毫秒(默认:1577836800000,即 2020-01-01 00:00:00) */
|
|
20
|
+
baseTime?: number;
|
|
21
|
+
/** 工作节点 ID 的位数(范围:1-15,默认:6) */
|
|
22
|
+
workerIdBitLength?: number;
|
|
23
|
+
/** 序列号的位数(范围:3-21,默认:6) */
|
|
24
|
+
seqBitLength?: number;
|
|
25
|
+
/** 最大序列号(默认:2^seqBitLength - 1) */
|
|
26
|
+
maxSeqNumber?: number;
|
|
27
|
+
/** 最小序列号(默认:5,0-4 保留用于时钟回拨处理) */
|
|
28
|
+
minSeqNumber?: number;
|
|
29
|
+
/** 最大漂移次数,超过后等待下一毫秒(默认:2000) */
|
|
30
|
+
topOverCostCount?: number;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* ID 解析结果
|
|
34
|
+
*/
|
|
35
|
+
interface ParseResult {
|
|
36
|
+
/** ID 生成时间(Date 对象) */
|
|
37
|
+
timestamp: Date;
|
|
38
|
+
/** ID 生成时间戳,单位:毫秒 */
|
|
39
|
+
timestampMs: number;
|
|
40
|
+
/** 工作节点 ID */
|
|
41
|
+
workerId: number;
|
|
42
|
+
/** 序列号 */
|
|
43
|
+
sequence: number;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* 统计信息结果(对外暴露,使用 Number 类型)
|
|
47
|
+
*/
|
|
48
|
+
interface StatsResult {
|
|
49
|
+
/** 总生成 ID 数量 */
|
|
50
|
+
totalGenerated: number;
|
|
51
|
+
/** 漂移次数 */
|
|
52
|
+
overCostCount: number;
|
|
53
|
+
/** 时钟回拨次数 */
|
|
54
|
+
turnBackCount: number;
|
|
55
|
+
/** 运行时长,单位:毫秒 */
|
|
56
|
+
uptimeMs: number;
|
|
57
|
+
/** 平均每秒生成 ID 数量 */
|
|
58
|
+
avgPerSecond: number;
|
|
59
|
+
/** 当前状态(OVER_COST: 漂移中, NORMAL: 正常) */
|
|
60
|
+
currentState: 'OVER_COST' | 'NORMAL';
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* 配置信息结果
|
|
64
|
+
*/
|
|
65
|
+
interface ConfigResult {
|
|
66
|
+
/** 算法类型(DRIFT: 漂移算法, TRADITIONAL: 传统算法) */
|
|
67
|
+
method: 'DRIFT' | 'TRADITIONAL';
|
|
68
|
+
/** 当前工作节点 ID */
|
|
69
|
+
workerId: number;
|
|
70
|
+
/** 工作节点 ID 范围(格式:"0-63") */
|
|
71
|
+
workerIdRange: string;
|
|
72
|
+
/** 序列号范围(格式:"5-63") */
|
|
73
|
+
sequenceRange: string;
|
|
74
|
+
/** 最大序列号值 */
|
|
75
|
+
maxSequence: number;
|
|
76
|
+
/** 每毫秒可生成的 ID 数量 */
|
|
77
|
+
idsPerMillisecond: number;
|
|
78
|
+
/** 起始时间(Date 对象) */
|
|
79
|
+
baseTime: Date;
|
|
80
|
+
/** 时间戳占用的位数 */
|
|
81
|
+
timestampBits: number;
|
|
82
|
+
/** 工作节点 ID 占用的位数 */
|
|
83
|
+
workerIdBits: number;
|
|
84
|
+
/** 序列号占用的位数 */
|
|
85
|
+
sequenceBits: number;
|
|
86
|
+
}
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region src/index.d.ts
|
|
89
|
+
/**
|
|
90
|
+
* 优化版 Snowflake ID 生成器
|
|
91
|
+
*
|
|
92
|
+
* 基于 Snowflake 算法的高性能分布式唯一 ID 生成器,支持漂移算法和时钟回拨处理。
|
|
93
|
+
*
|
|
94
|
+
* 特性:
|
|
95
|
+
* - 漂移算法:提升高并发下的性能
|
|
96
|
+
* - 优雅处理时钟回拨,不阻塞生成
|
|
97
|
+
* - 可配置的位长度,灵活性高
|
|
98
|
+
* - 支持传统和漂移两种生成方法
|
|
99
|
+
*
|
|
100
|
+
* ⚠️ 注意:此实例不是线程安全的,每个 Worker/进程应该创建独立的实例并使用不同的 workerId
|
|
101
|
+
*/
|
|
102
|
+
declare class GenidOptimized {
|
|
103
|
+
private method;
|
|
104
|
+
private baseTime;
|
|
105
|
+
private workerId;
|
|
106
|
+
private workerIdBitLength;
|
|
107
|
+
private seqBitLength;
|
|
108
|
+
private maxSeqNumber;
|
|
109
|
+
private minSeqNumber;
|
|
110
|
+
private topOverCostCount;
|
|
111
|
+
private _timestampShift;
|
|
112
|
+
private _currentSeqNumber;
|
|
113
|
+
private _lastTimeTick;
|
|
114
|
+
private _turnBackTimeTick;
|
|
115
|
+
private _turnBackIndex;
|
|
116
|
+
private _isOverCost;
|
|
117
|
+
private _overCostCountInOneTerm;
|
|
118
|
+
private _stats;
|
|
119
|
+
/**
|
|
120
|
+
* 构造函数,初始化 ID 生成器
|
|
121
|
+
*
|
|
122
|
+
* @param {Object} options - 配置选项
|
|
123
|
+
* @param {number} options.workerId - 工作节点/机器 ID(必须,范围 0 到 2^workerIdBitLength-1)
|
|
124
|
+
* @param {GenidMethod} [options.method=GenidMethod.DRIFT] - 算法类型
|
|
125
|
+
* @param {number} [options.baseTime=1577836800000] - 起始时间戳(毫秒,默认:2020-01-01)
|
|
126
|
+
* @param {number} [options.workerIdBitLength=6] - 工作节点 ID 的位数(1-15,默认:6)
|
|
127
|
+
* @param {number} [options.seqBitLength=6] - 序列号的位数(3-21,默认:6)
|
|
128
|
+
* @param {number} [options.maxSeqNumber] - 最大序列号(默认:2^seqBitLength-1)
|
|
129
|
+
* @param {number} [options.minSeqNumber=5] - 最小序列号(默认:5,0-4 保留)
|
|
130
|
+
* @param {number} [options.topOverCostCount=2000] - 最大漂移次数(默认:2000)
|
|
131
|
+
*
|
|
132
|
+
* @throws {Error} 如果缺少 workerId 或配置无效
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* const genid = new GenidOptimized({ workerId: 1 });
|
|
136
|
+
* const id = genid.nextId();
|
|
137
|
+
*/
|
|
138
|
+
constructor(options: GenidOptions);
|
|
139
|
+
/**
|
|
140
|
+
* 初始化配置,设置默认值
|
|
141
|
+
* @private
|
|
142
|
+
* @param {Object} options - 用户提供的配置
|
|
143
|
+
* @returns {Object} 合并后的配置对象
|
|
144
|
+
*/
|
|
145
|
+
private _initConfig;
|
|
146
|
+
/**
|
|
147
|
+
* 验证配置参数的有效性
|
|
148
|
+
* @private
|
|
149
|
+
* @param {Object} config - 配置对象
|
|
150
|
+
* @throws {Error} 如果配置无效
|
|
151
|
+
*/
|
|
152
|
+
private _validateConfig;
|
|
153
|
+
/**
|
|
154
|
+
* 初始化实例变量
|
|
155
|
+
* @private
|
|
156
|
+
* @param {Object} config - 配置对象
|
|
157
|
+
*/
|
|
158
|
+
private _initVariables;
|
|
159
|
+
/**
|
|
160
|
+
* 获取当前时间戳(相对于 baseTime 的毫秒数)
|
|
161
|
+
* @private
|
|
162
|
+
* @returns {bigint} 当前时间戳
|
|
163
|
+
*/
|
|
164
|
+
private _getCurrentTimeTick;
|
|
165
|
+
/**
|
|
166
|
+
* 等待下一毫秒
|
|
167
|
+
* @private
|
|
168
|
+
* @returns {bigint} 下一毫秒时间戳
|
|
169
|
+
*/
|
|
170
|
+
private _getNextTimeTick;
|
|
171
|
+
/**
|
|
172
|
+
* 根据组件计算 ID
|
|
173
|
+
* @private
|
|
174
|
+
* @param {bigint} useTimeTick - 使用的时间戳
|
|
175
|
+
* @returns {bigint} 计算得到的 ID
|
|
176
|
+
*/
|
|
177
|
+
private _calcId;
|
|
178
|
+
/**
|
|
179
|
+
* 计算时钟回拨时的 ID
|
|
180
|
+
* @private
|
|
181
|
+
* @param {bigint} useTimeTick - 使用的时间戳
|
|
182
|
+
* @returns {bigint} 计算得到的 ID
|
|
183
|
+
*/
|
|
184
|
+
private _calcTurnBackId;
|
|
185
|
+
/**
|
|
186
|
+
* 处理漂移情况(漂移算法)
|
|
187
|
+
* @private
|
|
188
|
+
* @returns {bigint} 生成的 ID
|
|
189
|
+
*/
|
|
190
|
+
private _nextOverCostId;
|
|
191
|
+
/**
|
|
192
|
+
* 正常生成 ID
|
|
193
|
+
* @private
|
|
194
|
+
* @returns {bigint} 生成的 ID
|
|
195
|
+
*/
|
|
196
|
+
private _nextNormalId;
|
|
197
|
+
/**
|
|
198
|
+
* 钩子函数:开始漂移操作(可被子类重写)
|
|
199
|
+
* @protected
|
|
200
|
+
* @param {bigint} useTimeTick - 当前时间戳
|
|
201
|
+
*/
|
|
202
|
+
protected _beginOverCostAction(useTimeTick: bigint): void;
|
|
203
|
+
/**
|
|
204
|
+
* 钩子函数:结束漂移操作(可被子类重写)
|
|
205
|
+
* @protected
|
|
206
|
+
* @param {bigint} useTimeTick - 当前时间戳
|
|
207
|
+
*/
|
|
208
|
+
protected _endOverCostAction(useTimeTick: bigint): void;
|
|
209
|
+
/**
|
|
210
|
+
* 钩子函数:开始时钟回拨操作(可被子类重写)
|
|
211
|
+
* @protected
|
|
212
|
+
* @param {bigint} useTimeTick - 当前时间戳
|
|
213
|
+
*/
|
|
214
|
+
protected _beginTurnBackAction(useTimeTick: bigint): void;
|
|
215
|
+
/**
|
|
216
|
+
* 钩子函数:结束时钟回拨操作(可被子类重写)
|
|
217
|
+
* @protected
|
|
218
|
+
* @param {bigint} useTimeTick - 当前时间戳
|
|
219
|
+
*/
|
|
220
|
+
protected _endTurnBackAction(useTimeTick: bigint): void;
|
|
221
|
+
/**
|
|
222
|
+
* 生成下一个 ID
|
|
223
|
+
*
|
|
224
|
+
* @returns {number} 唯一 ID(Number 类型)
|
|
225
|
+
* @throws {Error} 如果 ID 超出 JavaScript 安全整数范围
|
|
226
|
+
*
|
|
227
|
+
* @example
|
|
228
|
+
* const id = genid.nextNumber();
|
|
229
|
+
* console.log(id); // 123456789012345
|
|
230
|
+
*/
|
|
231
|
+
nextNumber(): number;
|
|
232
|
+
/**
|
|
233
|
+
* 生成下一个 ID
|
|
234
|
+
*
|
|
235
|
+
* 如果 ID 在安全范围内返回 Number,否则返回 BigInt
|
|
236
|
+
*
|
|
237
|
+
* @returns {number|bigint} 唯一 ID
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
240
|
+
* const id = genid.nextId();
|
|
241
|
+
* console.log(typeof id); // 'number' 或 'bigint'
|
|
242
|
+
*/
|
|
243
|
+
nextId(): number | bigint;
|
|
244
|
+
/**
|
|
245
|
+
* 生成下一个 ID
|
|
246
|
+
*
|
|
247
|
+
* @returns {bigint} 唯一 ID(BigInt 类型)
|
|
248
|
+
*
|
|
249
|
+
* @example
|
|
250
|
+
* const id = genid.nextBigId();
|
|
251
|
+
* console.log(id); // 123456789012345678n
|
|
252
|
+
*/
|
|
253
|
+
nextBigId(): bigint;
|
|
254
|
+
/**
|
|
255
|
+
* 批量生成 ID
|
|
256
|
+
*
|
|
257
|
+
* @param {number} count - 要生成的 ID 数量
|
|
258
|
+
* @param {boolean} [asBigInt=false] - 是否返回 BigInt 数组
|
|
259
|
+
* @returns {Array<number|bigint>} 唯一 ID 数组
|
|
260
|
+
*
|
|
261
|
+
* @example
|
|
262
|
+
* const ids = genid.nextBatch(100);
|
|
263
|
+
* const bigIds = genid.nextBatch(100, true);
|
|
264
|
+
*/
|
|
265
|
+
nextBatch(count: number, asBigInt?: boolean): Array<number | bigint>;
|
|
266
|
+
/**
|
|
267
|
+
* 解析 ID,提取其组成部分
|
|
268
|
+
*
|
|
269
|
+
* @param {number|bigint|string} id - 要解析的 ID
|
|
270
|
+
* @returns {Object} 解析结果
|
|
271
|
+
* @returns {Date} return.timestamp - 生成时间戳
|
|
272
|
+
* @returns {number} return.timestampMs - 时间戳(毫秒)
|
|
273
|
+
* @returns {number} return.workerId - 工作节点 ID
|
|
274
|
+
* @returns {number} return.sequence - 序列号
|
|
275
|
+
*
|
|
276
|
+
* @example
|
|
277
|
+
* const info = genid.parse(id);
|
|
278
|
+
* console.log(info);
|
|
279
|
+
* // {
|
|
280
|
+
* // timestamp: Date,
|
|
281
|
+
* // timestampMs: 1609459200000,
|
|
282
|
+
* // workerId: 1,
|
|
283
|
+
* // sequence: 42
|
|
284
|
+
* // }
|
|
285
|
+
*/
|
|
286
|
+
parse(id: number | bigint | string): ParseResult;
|
|
287
|
+
/**
|
|
288
|
+
* 获取生成器统计信息
|
|
289
|
+
*
|
|
290
|
+
* @returns {Object} 统计数据
|
|
291
|
+
*
|
|
292
|
+
* @example
|
|
293
|
+
* const stats = genid.getStats();
|
|
294
|
+
* console.log(stats);
|
|
295
|
+
*/
|
|
296
|
+
getStats(): StatsResult;
|
|
297
|
+
/**
|
|
298
|
+
* 重置统计数据
|
|
299
|
+
*/
|
|
300
|
+
resetStats(): void;
|
|
301
|
+
/**
|
|
302
|
+
* 获取配置信息
|
|
303
|
+
*
|
|
304
|
+
* @returns {Object} 配置详情
|
|
305
|
+
*/
|
|
306
|
+
getConfig(): ConfigResult;
|
|
307
|
+
/**
|
|
308
|
+
* 将 ID 格式化为二进制字符串以便调试
|
|
309
|
+
*
|
|
310
|
+
* @param {number|bigint|string} id - 要格式化的 ID
|
|
311
|
+
* @returns {string} 格式化的二进制表示
|
|
312
|
+
*/
|
|
313
|
+
formatBinary(id: number | bigint | string): string;
|
|
314
|
+
}
|
|
315
|
+
//#endregion
|
|
316
|
+
export { GenidOptimized };
|