@cdlab996/genid 1.2.1 → 1.4.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/README.md +280 -257
- package/README.zh-CN.md +377 -0
- package/dist/index.cjs +95 -240
- package/dist/index.d.cts +83 -236
- package/dist/index.d.mts +83 -236
- package/dist/index.mjs +93 -238
- package/package.json +7 -9
package/dist/index.d.cts
CHANGED
|
@@ -1,103 +1,103 @@
|
|
|
1
|
-
//#region src/types
|
|
2
|
-
/**
|
|
3
|
-
* ID 生成算法类型
|
|
4
|
-
*/
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
/** ID 生成算法类型 */
|
|
5
3
|
declare enum GenidMethod {
|
|
6
|
-
/**
|
|
4
|
+
/** 漂移算法(推荐,高并发下可突破每毫秒序列号上限) */
|
|
7
5
|
DRIFT = 1,
|
|
8
|
-
/**
|
|
6
|
+
/** 传统算法(序列号耗尽时等待下一毫秒) */
|
|
9
7
|
TRADITIONAL = 2
|
|
10
8
|
}
|
|
11
|
-
/**
|
|
12
|
-
* ID 生成器配置选项
|
|
13
|
-
*/
|
|
9
|
+
/** ID 生成器构造选项 */
|
|
14
10
|
interface GenidOptions {
|
|
15
|
-
/**
|
|
11
|
+
/** 工作节点 ID(必须,范围 0 到 2^workerIdBitLength-1) */
|
|
16
12
|
workerId: number;
|
|
17
|
-
/** 算法类型(默认:
|
|
13
|
+
/** 算法类型(默认:DRIFT) */
|
|
18
14
|
method?: GenidMethod;
|
|
19
|
-
/**
|
|
15
|
+
/** 起始时间戳/毫秒(默认:2020-01-01) */
|
|
20
16
|
baseTime?: number;
|
|
21
|
-
/** 工作节点 ID
|
|
17
|
+
/** 工作节点 ID 位数(1-15,默认:6) */
|
|
22
18
|
workerIdBitLength?: number;
|
|
23
|
-
/**
|
|
19
|
+
/** 序列号位数(3-21,默认:6) */
|
|
24
20
|
seqBitLength?: number;
|
|
25
21
|
/** 最大序列号(默认:2^seqBitLength - 1) */
|
|
26
22
|
maxSeqNumber?: number;
|
|
27
|
-
/** 最小序列号(默认:5,0-4
|
|
23
|
+
/** 最小序列号(默认:5,0-4 保留用于时钟回拨) */
|
|
28
24
|
minSeqNumber?: number;
|
|
29
|
-
/**
|
|
25
|
+
/** 最大漂移次数(默认:2000) */
|
|
30
26
|
topOverCostCount?: number;
|
|
31
27
|
}
|
|
32
|
-
/**
|
|
33
|
-
|
|
34
|
-
|
|
28
|
+
/** 内部配置(所有字段必填,由 initConfig 生成) */
|
|
29
|
+
interface GenidConfig {
|
|
30
|
+
workerId: number;
|
|
31
|
+
method: GenidMethod;
|
|
32
|
+
baseTime: number;
|
|
33
|
+
workerIdBitLength: number;
|
|
34
|
+
seqBitLength: number;
|
|
35
|
+
maxSeqNumber: number;
|
|
36
|
+
minSeqNumber: number;
|
|
37
|
+
topOverCostCount: number;
|
|
38
|
+
}
|
|
39
|
+
/** 内部统计数据(BigInt 确保大数精度) */
|
|
40
|
+
interface Stats {
|
|
41
|
+
totalGenerated: bigint;
|
|
42
|
+
overCostCount: bigint;
|
|
43
|
+
turnBackCount: bigint;
|
|
44
|
+
startTime: number;
|
|
45
|
+
}
|
|
46
|
+
/** ID 解析结果 */
|
|
35
47
|
interface ParseResult {
|
|
36
|
-
/**
|
|
48
|
+
/** 生成时间 */
|
|
37
49
|
timestamp: Date;
|
|
38
|
-
/**
|
|
50
|
+
/** 生成时间戳/毫秒 */
|
|
39
51
|
timestampMs: number;
|
|
40
52
|
/** 工作节点 ID */
|
|
41
53
|
workerId: number;
|
|
42
54
|
/** 序列号 */
|
|
43
55
|
sequence: number;
|
|
44
56
|
}
|
|
45
|
-
/**
|
|
46
|
-
* 统计信息结果(对外暴露,使用 Number 类型)
|
|
47
|
-
*/
|
|
57
|
+
/** 对外统计信息 */
|
|
48
58
|
interface StatsResult {
|
|
49
|
-
/** 总生成 ID 数量 */
|
|
50
59
|
totalGenerated: number;
|
|
51
|
-
/** 漂移次数 */
|
|
52
60
|
overCostCount: number;
|
|
53
|
-
/** 时钟回拨次数 */
|
|
54
61
|
turnBackCount: number;
|
|
55
|
-
/**
|
|
62
|
+
/** 运行时长/毫秒 */
|
|
56
63
|
uptimeMs: number;
|
|
57
|
-
/** 平均每秒生成 ID 数量 */
|
|
58
64
|
avgPerSecond: number;
|
|
59
|
-
/** 当前状态(OVER_COST: 漂移中, NORMAL: 正常) */
|
|
60
65
|
currentState: 'OVER_COST' | 'NORMAL';
|
|
61
66
|
}
|
|
62
|
-
/**
|
|
63
|
-
|
|
64
|
-
*/
|
|
67
|
+
/** isValid 校验选项 */
|
|
68
|
+
interface ValidateOptions {
|
|
69
|
+
/** 为 true 时要求 workerId 匹配当前实例(默认:false) */
|
|
70
|
+
strictWorkerId?: boolean;
|
|
71
|
+
/** ID 的生成时间不得早于此时间戳/毫秒(默认:baseTime) */
|
|
72
|
+
afterTime?: number;
|
|
73
|
+
}
|
|
74
|
+
/** 配置信息 */
|
|
65
75
|
interface ConfigResult {
|
|
66
|
-
/** 算法类型(DRIFT: 漂移算法, TRADITIONAL: 传统算法) */
|
|
67
76
|
method: 'DRIFT' | 'TRADITIONAL';
|
|
68
|
-
/** 当前工作节点 ID */
|
|
69
77
|
workerId: number;
|
|
70
|
-
/**
|
|
78
|
+
/** 格式:"0-63" */
|
|
71
79
|
workerIdRange: string;
|
|
72
|
-
/**
|
|
80
|
+
/** 格式:"5-63" */
|
|
73
81
|
sequenceRange: string;
|
|
74
|
-
/** 最大序列号值 */
|
|
75
82
|
maxSequence: number;
|
|
76
|
-
/** 每毫秒可生成的 ID 数量 */
|
|
77
83
|
idsPerMillisecond: number;
|
|
78
|
-
/** 起始时间(Date 对象) */
|
|
79
84
|
baseTime: Date;
|
|
80
|
-
/** 时间戳占用的位数 */
|
|
81
85
|
timestampBits: number;
|
|
82
|
-
/** 工作节点 ID 占用的位数 */
|
|
83
86
|
workerIdBits: number;
|
|
84
|
-
/** 序列号占用的位数 */
|
|
85
87
|
sequenceBits: number;
|
|
86
88
|
}
|
|
87
89
|
//#endregion
|
|
88
|
-
//#region src/
|
|
90
|
+
//#region src/genid.d.ts
|
|
89
91
|
/**
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
* 基于 Snowflake 算法的高性能分布式唯一 ID 生成器,支持漂移算法和时钟回拨处理。
|
|
92
|
+
* 基于 Snowflake 算法的分布式唯一 ID 生成器
|
|
93
93
|
*
|
|
94
|
-
*
|
|
95
|
-
* -
|
|
96
|
-
* -
|
|
97
|
-
* - 可配置的位长度,灵活性高
|
|
98
|
-
* - 支持传统和漂移两种生成方法
|
|
94
|
+
* - 漂移算法:高并发下借用未来时间戳,突破每毫秒序列号上限
|
|
95
|
+
* - 时钟回拨:使用保留序列号(0-4)优雅降级,不阻塞生成
|
|
96
|
+
* - 非线程安全,每个 Worker/进程应使用独立实例和不同 workerId
|
|
99
97
|
*
|
|
100
|
-
*
|
|
98
|
+
* @example
|
|
99
|
+
* const genid = new GenidOptimized({ workerId: 1 });
|
|
100
|
+
* const id = genid.nextId();
|
|
101
101
|
*/
|
|
102
102
|
declare class GenidOptimized {
|
|
103
103
|
private method;
|
|
@@ -116,216 +116,63 @@ declare class GenidOptimized {
|
|
|
116
116
|
private _isOverCost;
|
|
117
117
|
private _overCostCountInOneTerm;
|
|
118
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
119
|
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
120
|
private _initVariables;
|
|
159
|
-
/**
|
|
160
|
-
* 获取当前时间戳(相对于 baseTime 的毫秒数)
|
|
161
|
-
* @private
|
|
162
|
-
* @returns {bigint} 当前时间戳
|
|
163
|
-
*/
|
|
121
|
+
/** 获取相对于 baseTime 的当前时间戳 */
|
|
164
122
|
private _getCurrentTimeTick;
|
|
165
|
-
/**
|
|
166
|
-
* 等待下一毫秒
|
|
167
|
-
* @private
|
|
168
|
-
* @returns {bigint} 下一毫秒时间戳
|
|
169
|
-
*/
|
|
123
|
+
/** 自旋等待直到时间前进到下一毫秒 */
|
|
170
124
|
private _getNextTimeTick;
|
|
171
|
-
/**
|
|
172
|
-
* 根据组件计算 ID
|
|
173
|
-
* @private
|
|
174
|
-
* @param {bigint} useTimeTick - 使用的时间戳
|
|
175
|
-
* @returns {bigint} 计算得到的 ID
|
|
176
|
-
*/
|
|
125
|
+
/** 组装 ID:timestamp | workerId | sequence,并自增序列号 */
|
|
177
126
|
private _calcId;
|
|
178
|
-
/**
|
|
179
|
-
* 计算时钟回拨时的 ID
|
|
180
|
-
* @private
|
|
181
|
-
* @param {bigint} useTimeTick - 使用的时间戳
|
|
182
|
-
* @returns {bigint} 计算得到的 ID
|
|
183
|
-
*/
|
|
127
|
+
/** 时钟回拨时组装 ID,使用保留序列号(0-4)避免冲突 */
|
|
184
128
|
private _calcTurnBackId;
|
|
185
|
-
/**
|
|
186
|
-
* 处理漂移情况(漂移算法)
|
|
187
|
-
* @private
|
|
188
|
-
* @returns {bigint} 生成的 ID
|
|
189
|
-
*/
|
|
129
|
+
/** 漂移状态下生成 ID */
|
|
190
130
|
private _nextOverCostId;
|
|
191
|
-
/**
|
|
192
|
-
* 正常生成 ID
|
|
193
|
-
* @private
|
|
194
|
-
* @returns {bigint} 生成的 ID
|
|
195
|
-
*/
|
|
131
|
+
/** 正常状态下生成 ID */
|
|
196
132
|
private _nextNormalId;
|
|
133
|
+
protected _beginOverCostAction(_useTimeTick: bigint): void;
|
|
134
|
+
protected _endOverCostAction(_useTimeTick: bigint): void;
|
|
135
|
+
protected _beginTurnBackAction(_useTimeTick: bigint): void;
|
|
136
|
+
protected _endTurnBackAction(_useTimeTick: bigint): void;
|
|
197
137
|
/**
|
|
198
|
-
*
|
|
199
|
-
* @
|
|
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
|
|
138
|
+
* 生成 ID,返回 number。超出安全整数范围时抛错。
|
|
139
|
+
* @throws 当 ID >= Number.MAX_SAFE_INTEGER + 1 时
|
|
230
140
|
*/
|
|
231
141
|
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
|
-
*/
|
|
142
|
+
/** 生成 ID,安全范围内返回 number,否则返回 bigint */
|
|
243
143
|
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
|
-
*/
|
|
144
|
+
/** 生成 ID,始终返回 bigint */
|
|
253
145
|
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
|
-
*/
|
|
146
|
+
/** 批量生成 ID */
|
|
265
147
|
nextBatch(count: number, asBigInt?: boolean): Array<number | bigint>;
|
|
266
148
|
/**
|
|
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 - 序列号
|
|
149
|
+
* 解析 ID,提取时间戳、workerId、序列号
|
|
275
150
|
*
|
|
276
151
|
* @example
|
|
277
|
-
*
|
|
278
|
-
*
|
|
279
|
-
* // {
|
|
280
|
-
* // timestamp: Date,
|
|
281
|
-
* // timestampMs: 1609459200000,
|
|
282
|
-
* // workerId: 1,
|
|
283
|
-
* // sequence: 42
|
|
284
|
-
* // }
|
|
152
|
+
* genid.parse(id)
|
|
153
|
+
* // { timestamp: Date, timestampMs: 1609459200000, workerId: 1, sequence: 42 }
|
|
285
154
|
*/
|
|
286
155
|
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
|
-
*/
|
|
156
|
+
/** 获取运行统计信息 */
|
|
296
157
|
getStats(): StatsResult;
|
|
297
|
-
/**
|
|
298
|
-
* 重置统计数据
|
|
299
|
-
*/
|
|
158
|
+
/** 重置统计数据 */
|
|
300
159
|
resetStats(): void;
|
|
301
|
-
/**
|
|
302
|
-
* 获取配置信息
|
|
303
|
-
*
|
|
304
|
-
* @returns {Object} 配置详情
|
|
305
|
-
*/
|
|
160
|
+
/** 获取当前配置信息 */
|
|
306
161
|
getConfig(): ConfigResult;
|
|
307
162
|
/**
|
|
308
|
-
* 验证 ID
|
|
163
|
+
* 验证 ID 是否为当前配置下合法的 Snowflake ID
|
|
309
164
|
*
|
|
310
|
-
* @param
|
|
311
|
-
* @param {boolean} [strictWorkerId=false] - 是否严格验证 workerId 必须匹配当前实例
|
|
312
|
-
* @returns {boolean} ID 是否有效
|
|
165
|
+
* @param options - 校验选项,或传 boolean 表示 strictWorkerId(向后兼容)
|
|
313
166
|
*
|
|
314
167
|
* @example
|
|
315
|
-
*
|
|
316
|
-
*
|
|
317
|
-
* genid.isValid(id)
|
|
318
|
-
* genid.isValid(
|
|
319
|
-
* genid.isValid(id, true); // true (workerId 匹配)
|
|
320
|
-
*/
|
|
321
|
-
isValid(id: number | bigint | string, strictWorkerId?: boolean): boolean;
|
|
322
|
-
/**
|
|
323
|
-
* 将 ID 格式化为二进制字符串以便调试
|
|
324
|
-
*
|
|
325
|
-
* @param {number|bigint|string} id - 要格式化的 ID
|
|
326
|
-
* @returns {string} 格式化的二进制表示
|
|
168
|
+
* genid.isValid(id) // 宽松校验
|
|
169
|
+
* genid.isValid(id, true) // 要求 workerId 匹配
|
|
170
|
+
* genid.isValid(id, { strictWorkerId: true }) // 同上
|
|
171
|
+
* genid.isValid(id, { afterTime: startupTime }) // 要求 ID 生成时间晚于 startupTime
|
|
327
172
|
*/
|
|
173
|
+
isValid(id: number | bigint | string, options?: boolean | ValidateOptions): boolean;
|
|
174
|
+
/** 将 ID 格式化为带标注的二进制字符串(调试用) */
|
|
328
175
|
formatBinary(id: number | bigint | string): string;
|
|
329
176
|
}
|
|
330
177
|
//#endregion
|
|
331
|
-
export { GenidOptimized };
|
|
178
|
+
export { ConfigResult, GenidConfig, GenidMethod, GenidOptimized, GenidOptions, ParseResult, Stats, StatsResult, ValidateOptions };
|