@cdlab996/genid 1.2.0 → 1.3.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/dist/index.d.cts CHANGED
@@ -1,103 +1,96 @@
1
- //#region src/types/index.d.ts
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
- /** 工作节点/机器 ID(必须,范围 0 到 2^workerIdBitLength-1) */
11
+ /** 工作节点 ID(必须,范围 0 到 2^workerIdBitLength-1) */
16
12
  workerId: number;
17
- /** 算法类型(默认:GenidMethod.DRIFT) */
13
+ /** 算法类型(默认:DRIFT) */
18
14
  method?: GenidMethod;
19
- /** 起始时间戳,单位:毫秒(默认:1577836800000,即 2020-01-01 00:00:00) */
15
+ /** 起始时间戳/毫秒(默认:2020-01-01) */
20
16
  baseTime?: number;
21
- /** 工作节点 ID 的位数(范围:1-15,默认:6) */
17
+ /** 工作节点 ID 位数(1-15,默认:6) */
22
18
  workerIdBitLength?: number;
23
- /** 序列号的位数(范围:3-21,默认:6) */
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
- /** 最大漂移次数,超过后等待下一毫秒(默认:2000) */
25
+ /** 最大漂移次数(默认:2000) */
30
26
  topOverCostCount?: number;
31
27
  }
32
- /**
33
- * ID 解析结果
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
- /** ID 生成时间(Date 对象) */
48
+ /** 生成时间 */
37
49
  timestamp: Date;
38
- /** ID 生成时间戳,单位:毫秒 */
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
+ /** 配置信息 */
65
68
  interface ConfigResult {
66
- /** 算法类型(DRIFT: 漂移算法, TRADITIONAL: 传统算法) */
67
69
  method: 'DRIFT' | 'TRADITIONAL';
68
- /** 当前工作节点 ID */
69
70
  workerId: number;
70
- /** 工作节点 ID 范围(格式:"0-63" */
71
+ /** 格式:"0-63" */
71
72
  workerIdRange: string;
72
- /** 序列号范围(格式:"5-63" */
73
+ /** 格式:"5-63" */
73
74
  sequenceRange: string;
74
- /** 最大序列号值 */
75
75
  maxSequence: number;
76
- /** 每毫秒可生成的 ID 数量 */
77
76
  idsPerMillisecond: number;
78
- /** 起始时间(Date 对象) */
79
77
  baseTime: Date;
80
- /** 时间戳占用的位数 */
81
78
  timestampBits: number;
82
- /** 工作节点 ID 占用的位数 */
83
79
  workerIdBits: number;
84
- /** 序列号占用的位数 */
85
80
  sequenceBits: number;
86
81
  }
87
82
  //#endregion
88
- //#region src/index.d.ts
83
+ //#region src/genid.d.ts
89
84
  /**
90
- * 优化版 Snowflake ID 生成器
91
- *
92
- * 基于 Snowflake 算法的高性能分布式唯一 ID 生成器,支持漂移算法和时钟回拨处理。
85
+ * 基于 Snowflake 算法的分布式唯一 ID 生成器
93
86
  *
94
- * 特性:
95
- * - 漂移算法:提升高并发下的性能
96
- * - 优雅处理时钟回拨,不阻塞生成
97
- * - 可配置的位长度,灵活性高
98
- * - 支持传统和漂移两种生成方法
87
+ * - 漂移算法:高并发下借用未来时间戳,突破每毫秒序列号上限
88
+ * - 时钟回拨:使用保留序列号(0-4)优雅降级,不阻塞生成
89
+ * - 非线程安全,每个 Worker/进程应使用独立实例和不同 workerId
99
90
  *
100
- * ⚠️ 注意:此实例不是线程安全的,每个 Worker/进程应该创建独立的实例并使用不同的 workerId
91
+ * @example
92
+ * const genid = new GenidOptimized({ workerId: 1 });
93
+ * const id = genid.nextId();
101
94
  */
102
95
  declare class GenidOptimized {
103
96
  private method;
@@ -116,216 +109,56 @@ declare class GenidOptimized {
116
109
  private _isOverCost;
117
110
  private _overCostCountInOneTerm;
118
111
  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
112
  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
113
  private _initVariables;
159
- /**
160
- * 获取当前时间戳(相对于 baseTime 的毫秒数)
161
- * @private
162
- * @returns {bigint} 当前时间戳
163
- */
114
+ /** 获取相对于 baseTime 的当前时间戳 */
164
115
  private _getCurrentTimeTick;
165
- /**
166
- * 等待下一毫秒
167
- * @private
168
- * @returns {bigint} 下一毫秒时间戳
169
- */
116
+ /** 自旋等待直到时间前进到下一毫秒 */
170
117
  private _getNextTimeTick;
171
- /**
172
- * 根据组件计算 ID
173
- * @private
174
- * @param {bigint} useTimeTick - 使用的时间戳
175
- * @returns {bigint} 计算得到的 ID
176
- */
118
+ /** 组装 ID:timestamp | workerId | sequence,并自增序列号 */
177
119
  private _calcId;
178
- /**
179
- * 计算时钟回拨时的 ID
180
- * @private
181
- * @param {bigint} useTimeTick - 使用的时间戳
182
- * @returns {bigint} 计算得到的 ID
183
- */
120
+ /** 时钟回拨时组装 ID,使用保留序列号(0-4)避免冲突 */
184
121
  private _calcTurnBackId;
185
- /**
186
- * 处理漂移情况(漂移算法)
187
- * @private
188
- * @returns {bigint} 生成的 ID
189
- */
122
+ /** 漂移状态下生成 ID */
190
123
  private _nextOverCostId;
191
- /**
192
- * 正常生成 ID
193
- * @private
194
- * @returns {bigint} 生成的 ID
195
- */
124
+ /** 正常状态下生成 ID */
196
125
  private _nextNormalId;
126
+ protected _beginOverCostAction(_useTimeTick: bigint): void;
127
+ protected _endOverCostAction(_useTimeTick: bigint): void;
128
+ protected _beginTurnBackAction(_useTimeTick: bigint): void;
129
+ protected _endTurnBackAction(_useTimeTick: bigint): void;
197
130
  /**
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
131
+ * 生成 ID,返回 number。超出安全整数范围时抛错。
132
+ * @throws 当 ID >= Number.MAX_SAFE_INTEGER + 1 时
230
133
  */
231
134
  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
- */
135
+ /** 生成 ID,安全范围内返回 number,否则返回 bigint */
243
136
  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
- */
137
+ /** 生成 ID,始终返回 bigint */
253
138
  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
- */
139
+ /** 批量生成 ID */
265
140
  nextBatch(count: number, asBigInt?: boolean): Array<number | bigint>;
266
141
  /**
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 - 序列号
142
+ * 解析 ID,提取时间戳、workerId、序列号
275
143
  *
276
144
  * @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
- * // }
145
+ * genid.parse(id)
146
+ * // { timestamp: Date, timestampMs: 1609459200000, workerId: 1, sequence: 42 }
285
147
  */
286
148
  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
- */
149
+ /** 获取运行统计信息 */
296
150
  getStats(): StatsResult;
297
- /**
298
- * 重置统计数据
299
- */
151
+ /** 重置统计数据 */
300
152
  resetStats(): void;
301
- /**
302
- * 获取配置信息
303
- *
304
- * @returns {Object} 配置详情
305
- */
153
+ /** 获取当前配置信息 */
306
154
  getConfig(): ConfigResult;
307
155
  /**
308
- * 验证 ID 是否为有效的 Snowflake ID
309
- *
310
- * @param {number|bigint|string} id - 要验证的 ID
311
- * @param {boolean} [strictWorkerId=false] - 是否严格验证 workerId 必须匹配当前实例
312
- * @returns {boolean} ID 是否有效
313
- *
314
- * @example
315
- * const genid = new GenidOptimized({ workerId: 1 });
316
- * const id = genid.nextId();
317
- * genid.isValid(id); // true
318
- * genid.isValid(12345); // false
319
- * genid.isValid(id, true); // true (workerId 匹配)
156
+ * 验证 ID 是否为当前配置下合法的 Snowflake ID
157
+ * @param strictWorkerId - 为 true 时要求 workerId 匹配当前实例
320
158
  */
321
159
  isValid(id: number | bigint | string, strictWorkerId?: boolean): boolean;
322
- /**
323
- * 将 ID 格式化为二进制字符串以便调试
324
- *
325
- * @param {number|bigint|string} id - 要格式化的 ID
326
- * @returns {string} 格式化的二进制表示
327
- */
160
+ /** 将 ID 格式化为带标注的二进制字符串(调试用) */
328
161
  formatBinary(id: number | bigint | string): string;
329
162
  }
330
163
  //#endregion
331
- export { GenidOptimized };
164
+ export { ConfigResult, GenidConfig, GenidMethod, GenidOptimized, GenidOptions, ParseResult, Stats, StatsResult };