@andrew_l/snowflake 0.3.21 → 0.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/dist/index.d.mts +197 -192
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +200 -293
- package/dist/index.mjs.map +1 -1
- package/package.json +9 -8
- package/dist/index.cjs +0 -303
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -231
- package/dist/index.d.ts +0 -231
package/dist/index.cjs
DELETED
|
@@ -1,303 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const toolkit = require('@andrew_l/toolkit');
|
|
4
|
-
|
|
5
|
-
var MAX_WORKER_ID = 31;
|
|
6
|
-
var MAX_PROCESS_ID = 31;
|
|
7
|
-
var MAX_INCREMENT = 4095;
|
|
8
|
-
class Snowflake {
|
|
9
|
-
/**
|
|
10
|
-
* Alias for {@link deconstruct}
|
|
11
|
-
*/
|
|
12
|
-
decode = this.deconstruct;
|
|
13
|
-
/**
|
|
14
|
-
* Internal reference of the epoch passed in the constructor
|
|
15
|
-
* @internal
|
|
16
|
-
*/
|
|
17
|
-
_epoch;
|
|
18
|
-
/**
|
|
19
|
-
* Internal incrementor for generating snowflakes
|
|
20
|
-
* @internal
|
|
21
|
-
*/
|
|
22
|
-
_increment = 0;
|
|
23
|
-
/**
|
|
24
|
-
* The process ID that will be used by default in the generate method
|
|
25
|
-
* @internal
|
|
26
|
-
*/
|
|
27
|
-
_processId = 1;
|
|
28
|
-
/**
|
|
29
|
-
* The worker ID that will be used by default in the generate method
|
|
30
|
-
* @internal
|
|
31
|
-
*/
|
|
32
|
-
_workerId = 0;
|
|
33
|
-
/**
|
|
34
|
-
* @internal
|
|
35
|
-
*/
|
|
36
|
-
_buffer;
|
|
37
|
-
/**
|
|
38
|
-
* @param epoch the epoch to use
|
|
39
|
-
*/
|
|
40
|
-
constructor(opts) {
|
|
41
|
-
if (toolkit.isDate(opts) || toolkit.isNumber(opts)) {
|
|
42
|
-
this._epoch = toolkit.timestampMs(opts);
|
|
43
|
-
} else if (toolkit.isBigInt(opts)) {
|
|
44
|
-
this._epoch = toolkit.timestampMs(Number(opts));
|
|
45
|
-
} else {
|
|
46
|
-
const { increment = 0, processId = 1, workerId = 0, epoch } = opts;
|
|
47
|
-
this._epoch = toolkit.timestampMs(toolkit.isBigInt(epoch) ? Number(epoch) : epoch);
|
|
48
|
-
this.workerId = workerId;
|
|
49
|
-
this.processId = processId;
|
|
50
|
-
this.increment = increment;
|
|
51
|
-
}
|
|
52
|
-
this._buffer = new Uint8Array(8);
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* The epoch for this snowflake, as a number
|
|
56
|
-
*/
|
|
57
|
-
get epoch() {
|
|
58
|
-
return this._epoch;
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Gets the configured process ID
|
|
62
|
-
*/
|
|
63
|
-
get processId() {
|
|
64
|
-
return this._processId;
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Sets the process ID that will be used by default for the {@link generate} method
|
|
68
|
-
* @param value The new value, will be masked with `0b11111`
|
|
69
|
-
*/
|
|
70
|
-
set processId(value) {
|
|
71
|
-
this._processId = Number(value) & MAX_PROCESS_ID;
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Gets the configured worker ID
|
|
75
|
-
*/
|
|
76
|
-
get workerId() {
|
|
77
|
-
return this._workerId;
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Sets the worker ID that will be used by default for the {@link generate} method
|
|
81
|
-
* @param value The new value, will be masked with `0b11111`
|
|
82
|
-
*/
|
|
83
|
-
set workerId(value) {
|
|
84
|
-
this._workerId = Number(value) & MAX_WORKER_ID;
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Get incrementor for generating snowflakes
|
|
88
|
-
*/
|
|
89
|
-
get increment() {
|
|
90
|
-
return this._increment;
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Sets the incrementor for generating snowflakes that will be used by default for the {@link generate} method
|
|
94
|
-
* @param value The new value
|
|
95
|
-
*/
|
|
96
|
-
set increment(value) {
|
|
97
|
-
this._increment = Number(value) & MAX_INCREMENT;
|
|
98
|
-
}
|
|
99
|
-
_createSavePoint() {
|
|
100
|
-
var increment = this._increment;
|
|
101
|
-
var workerId = this._workerId;
|
|
102
|
-
var processId = this._processId;
|
|
103
|
-
return () => {
|
|
104
|
-
this._increment = increment;
|
|
105
|
-
this._workerId = workerId;
|
|
106
|
-
this._processId = processId;
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Sets most lowest values for generating snowflakes that will be used by default for the {@link generate} method
|
|
111
|
-
*/
|
|
112
|
-
setLowest() {
|
|
113
|
-
this._increment = 0;
|
|
114
|
-
this._workerId = 0;
|
|
115
|
-
this._processId = 0;
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Sets most highest values for generating snowflakes that will be used by default for the {@link generate} method
|
|
119
|
-
*/
|
|
120
|
-
setHighest() {
|
|
121
|
-
this._increment = MAX_INCREMENT;
|
|
122
|
-
this._workerId = MAX_WORKER_ID;
|
|
123
|
-
this._processId = MAX_PROCESS_ID;
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Execute a function in context of most lowest values for generating snowflakes
|
|
127
|
-
* @example
|
|
128
|
-
* ```typescript
|
|
129
|
-
* const epoch = new Date('2000-01-01T00:00:00.000Z');
|
|
130
|
-
* const snowflake = new Snowflake();
|
|
131
|
-
* const id = snowflake.withLowest((v) => v.generate(epoch)); // the lowest possible id for that epoch
|
|
132
|
-
* ```
|
|
133
|
-
*/
|
|
134
|
-
withLowest(fn) {
|
|
135
|
-
toolkit.assert.fn(fn, "withLowest expected a function as first argument");
|
|
136
|
-
var reset = this._createSavePoint();
|
|
137
|
-
this.setLowest();
|
|
138
|
-
var result = fn(this);
|
|
139
|
-
reset();
|
|
140
|
-
return result;
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* Execute a function in context of most highest values for generating snowflakes
|
|
144
|
-
* @example
|
|
145
|
-
* ```typescript
|
|
146
|
-
* const epoch = new Date('2000-01-01T00:00:00.000Z');
|
|
147
|
-
* const snowflake = new Snowflake();
|
|
148
|
-
* const id = snowflake.withLowest((v) => v.generate(epoch)); // the highest possible id for that epoch
|
|
149
|
-
* ```
|
|
150
|
-
*/
|
|
151
|
-
withHighest(fn) {
|
|
152
|
-
toolkit.assert.fn(fn, "withHighest expected a function as first argument");
|
|
153
|
-
var reset = this._createSavePoint();
|
|
154
|
-
this.setHighest();
|
|
155
|
-
var result = fn(this);
|
|
156
|
-
reset();
|
|
157
|
-
return result;
|
|
158
|
-
}
|
|
159
|
-
/**
|
|
160
|
-
* Generates a Snowflake ID as bigint.
|
|
161
|
-
*/
|
|
162
|
-
generate(timestamp = Date.now()) {
|
|
163
|
-
if (timestamp instanceof Date) timestamp = timestamp.getTime();
|
|
164
|
-
if (!toolkit.isNumber(timestamp)) {
|
|
165
|
-
throw new Error(
|
|
166
|
-
`"timestamp" argument must be a number or Date (received ${typeof timestamp})`
|
|
167
|
-
);
|
|
168
|
-
}
|
|
169
|
-
var increment = this._increment;
|
|
170
|
-
this._increment = this._increment + 1 & MAX_INCREMENT;
|
|
171
|
-
return BigInt(timestamp - this._epoch) << 22n | BigInt(this._workerId & MAX_WORKER_ID) << 17n | BigInt(this._processId & MAX_PROCESS_ID) << 12n | BigInt(increment & MAX_INCREMENT);
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* Generates a Snowflake ID as a `Uint8Array` buffer.
|
|
175
|
-
*
|
|
176
|
-
* ⚠️ Returns a reference to the **same** internal `Uint8Array` instance.
|
|
177
|
-
* Its contents may be overwritten on the next ID generation call.
|
|
178
|
-
*
|
|
179
|
-
* This method is intended for high-performance scenarios where you
|
|
180
|
-
* immediately transform the buffer (e.g., to base62) before calling again.
|
|
181
|
-
* Avoid storing or mutating the returned buffer directly.
|
|
182
|
-
*/
|
|
183
|
-
generateBufferUnsafe(timestamp = Date.now()) {
|
|
184
|
-
if (timestamp instanceof Date) timestamp = timestamp.getTime();
|
|
185
|
-
if (!toolkit.isNumber(timestamp)) {
|
|
186
|
-
throw new Error(
|
|
187
|
-
`"timestamp" argument must be a number or Date (received ${typeof timestamp})`
|
|
188
|
-
);
|
|
189
|
-
}
|
|
190
|
-
var increment = this._increment;
|
|
191
|
-
this._increment = this._increment + 1 & MAX_INCREMENT;
|
|
192
|
-
var timestampDelta = timestamp - this._epoch;
|
|
193
|
-
var timestampHigh = Math.floor(timestampDelta / 4294967295);
|
|
194
|
-
var timestampLow = timestampDelta >>> 0;
|
|
195
|
-
var high32 = (timestampHigh << 22 | timestampLow >>> 10) >>> 0;
|
|
196
|
-
var low32 = (timestampLow << 22 | this._workerId << 17 | this._processId << 12 | increment & MAX_INCREMENT) >>> 0;
|
|
197
|
-
var buffer = this._buffer;
|
|
198
|
-
buffer[0] = high32 >>> 24;
|
|
199
|
-
buffer[1] = high32 >>> 16;
|
|
200
|
-
buffer[2] = high32 >>> 8;
|
|
201
|
-
buffer[3] = high32;
|
|
202
|
-
buffer[4] = low32 >>> 24;
|
|
203
|
-
buffer[5] = low32 >>> 16;
|
|
204
|
-
buffer[6] = low32 >>> 8;
|
|
205
|
-
buffer[7] = low32;
|
|
206
|
-
return buffer;
|
|
207
|
-
}
|
|
208
|
-
/**
|
|
209
|
-
* Generates a snowflake given an epoch and optionally a timestamp
|
|
210
|
-
* @example
|
|
211
|
-
* ```typescript
|
|
212
|
-
* const epoch = new Date('2000-01-01T00:00:00.000Z');
|
|
213
|
-
* const snowflake = new Snowflake({ epoch }).generate();
|
|
214
|
-
* ```
|
|
215
|
-
* @returns A unique snowflake as Uint8Array
|
|
216
|
-
*/
|
|
217
|
-
generateBuffer(timestamp = Date.now()) {
|
|
218
|
-
this.generateBufferUnsafe(timestamp);
|
|
219
|
-
return new Uint8Array(this._buffer);
|
|
220
|
-
}
|
|
221
|
-
/**
|
|
222
|
-
* Deconstructs a snowflake given a snowflake ID
|
|
223
|
-
* @param id the snowflake to deconstruct
|
|
224
|
-
* @returns a deconstructed snowflake
|
|
225
|
-
* @example
|
|
226
|
-
* ```typescript
|
|
227
|
-
* const epoch = new Date('2000-01-01T00:00:00.000Z');
|
|
228
|
-
* const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');
|
|
229
|
-
* ```
|
|
230
|
-
*/
|
|
231
|
-
deconstruct(id) {
|
|
232
|
-
var high32, low32;
|
|
233
|
-
if (id instanceof Uint8Array) {
|
|
234
|
-
high32 = (id[0] << 24 | id[1] << 16 | id[2] << 8 | id[3]) >>> 0;
|
|
235
|
-
low32 = (id[4] << 24 | id[5] << 16 | id[6] << 8 | id[7]) >>> 0;
|
|
236
|
-
} else if (toolkit.isBigInt(id)) {
|
|
237
|
-
return this.deconstruct(toolkit.bigIntBytes(id));
|
|
238
|
-
} else {
|
|
239
|
-
return this.deconstruct(toolkit.bigIntBytes(BigInt(id)));
|
|
240
|
-
}
|
|
241
|
-
return {
|
|
242
|
-
id: BigInt(high32) * 0x100000000n + BigInt(low32),
|
|
243
|
-
timestamp: high32 * 1024 + (low32 >>> 22) + this._epoch,
|
|
244
|
-
workerId: low32 >>> 17 & MAX_WORKER_ID,
|
|
245
|
-
processId: low32 >>> 12 & MAX_PROCESS_ID,
|
|
246
|
-
increment: low32 & MAX_INCREMENT,
|
|
247
|
-
epoch: this._epoch
|
|
248
|
-
};
|
|
249
|
-
}
|
|
250
|
-
/**
|
|
251
|
-
* Retrieves the timestamp field's value from a snowflake.
|
|
252
|
-
* @param id The snowflake to get the timestamp value from.
|
|
253
|
-
* @returns The UNIX timestamp that is stored in `id`.
|
|
254
|
-
*/
|
|
255
|
-
timestampFrom(id) {
|
|
256
|
-
if (toolkit.isString(id) || toolkit.isBigInt(id)) {
|
|
257
|
-
return this.timestampFrom(Snowflake.bufferFrom(id));
|
|
258
|
-
}
|
|
259
|
-
var high32 = (id[0] << 24 | id[1] << 16 | id[2] << 8 | id[3]) >>> 0;
|
|
260
|
-
var low32 = (id[4] << 24 | id[5] << 16 | id[6] << 8 | id[7]) >>> 0;
|
|
261
|
-
return high32 * 1024 + (low32 >>> 22) + this._epoch;
|
|
262
|
-
}
|
|
263
|
-
/**
|
|
264
|
-
* Returns a number indicating whether a reference snowflake comes before, or after, or is same as the given
|
|
265
|
-
* snowflake in sort order.
|
|
266
|
-
* @param a The first snowflake to compare.
|
|
267
|
-
* @param b The second snowflake to compare.
|
|
268
|
-
* @returns `-1` if `a` is older than `b`, `0` if `a` and `b` are equals, `1` if `a` is newer than `b`.
|
|
269
|
-
*/
|
|
270
|
-
static compare(a, b) {
|
|
271
|
-
if (toolkit.isString(a)) {
|
|
272
|
-
a = BigInt(a);
|
|
273
|
-
} else if (a instanceof Uint8Array) {
|
|
274
|
-
a = toolkit.bigIntFromBytes(a);
|
|
275
|
-
}
|
|
276
|
-
if (toolkit.isString(b)) {
|
|
277
|
-
b = BigInt(b);
|
|
278
|
-
} else if (b instanceof Uint8Array) {
|
|
279
|
-
b = toolkit.bigIntFromBytes(b);
|
|
280
|
-
}
|
|
281
|
-
return a === b ? 0 : a < b ? -1 : 1;
|
|
282
|
-
}
|
|
283
|
-
/**
|
|
284
|
-
* Parse value as Uint8Array buffer
|
|
285
|
-
*/
|
|
286
|
-
static bufferFrom(value) {
|
|
287
|
-
if (toolkit.isString(value)) value = BigInt(value);
|
|
288
|
-
var buf = toolkit.bigIntBytes(value);
|
|
289
|
-
var bufSize = buf.byteLength;
|
|
290
|
-
if (buf.byteLength < 8) {
|
|
291
|
-
var buf2 = new Uint8Array(8);
|
|
292
|
-
buf2.set(buf, 8 - bufSize);
|
|
293
|
-
buf = buf2;
|
|
294
|
-
}
|
|
295
|
-
return buf;
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
exports.MAX_INCREMENT = MAX_INCREMENT;
|
|
300
|
-
exports.MAX_PROCESS_ID = MAX_PROCESS_ID;
|
|
301
|
-
exports.MAX_WORKER_ID = MAX_WORKER_ID;
|
|
302
|
-
exports.Snowflake = Snowflake;
|
|
303
|
-
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/Snowflake.ts"],"sourcesContent":["/**\n * Based on: https://github.com/sapphiredev/utilities/blob/main/packages/snowflake/src/lib/Snowflake.ts\n */\nimport {\n type AnyFunction,\n assert,\n bigIntBytes,\n bigIntFromBytes,\n isBigInt,\n isDate,\n isNumber,\n isString,\n timestampMs,\n} from '@andrew_l/toolkit';\n\n/**\n * Object returned by Snowflake#deconstruct\n */\nexport interface DeconstructedSnowflake {\n /**\n * The id as a number\n */\n id: bigint;\n\n /**\n * The timestamp stored in the snowflake\n */\n timestamp: number;\n\n /**\n * The worker id stored in the snowflake\n */\n workerId: number;\n\n /**\n * The process id stored in the snowflake\n */\n processId: number;\n\n /**\n * The increment stored in the snowflake\n */\n increment: number;\n\n /**\n * The epoch to use in the snowflake\n */\n epoch: number;\n}\n\n/**\n * Options for Snowflake\n */\nexport interface SnowflakeOptions {\n /**\n * Timestamp or date of the snowflake to generate\n */\n epoch: number | bigint | Date;\n\n /**\n * The increment to use\n * @default 0\n * @remark keep in mind that this number is auto-incremented between generate calls\n */\n increment?: number | bigint;\n\n /**\n * The worker ID to use, will be truncated to 5 bits (0-31)\n * @default 0\n */\n workerId?: number | bigint;\n\n /**\n * The process ID to use, will be truncated to 5 bits (0-31)\n * @default 1\n */\n processId?: number | bigint;\n}\n\n/**\n * The maximum value the `workerId` field accepts in snowflakes.\n */\nexport var MAX_WORKER_ID = 0x1f;\n\n/**\n * The maximum value the `processId` field accepts in snowflakes.\n */\nexport var MAX_PROCESS_ID = 0x1f;\n\n/**\n * The maximum value the `increment` field accepts in snowflakes.\n */\nexport var MAX_INCREMENT = 0xfff;\n\n/**\n * A class for generating and deconstructing Twitter snowflakes.\n *\n * A {@link https://developer.twitter.com/en/docs/twitter-ids Twitter snowflake}\n * is a 64-bit unsigned integer with 4 fields that have a fixed epoch value.\n *\n * If we have a snowflake `266241948824764416` we can represent it as binary:\n * ```\n * 64 22 17 12 0\n * 000000111011000111100001101001000101000000 00001 00000 000000000000\n * number of ms since epoch worker pid increment\n * ```\n *\n * @group Main\n */\nexport class Snowflake {\n /**\n * Alias for {@link deconstruct}\n */\n public decode = this.deconstruct;\n\n /**\n * Internal reference of the epoch passed in the constructor\n * @internal\n */\n private readonly _epoch: number;\n\n /**\n * Internal incrementor for generating snowflakes\n * @internal\n */\n private _increment = 0;\n\n /**\n * The process ID that will be used by default in the generate method\n * @internal\n */\n private _processId = 1;\n\n /**\n * The worker ID that will be used by default in the generate method\n * @internal\n */\n private _workerId = 0;\n\n /**\n * @internal\n */\n private _buffer: Uint8Array;\n\n /**\n * @param epoch the epoch to use\n */\n public constructor(opts: SnowflakeOptions | Date | number | bigint) {\n if (isDate(opts) || isNumber(opts)) {\n this._epoch = timestampMs(opts);\n } else if (isBigInt(opts)) {\n this._epoch = timestampMs(Number(opts));\n } else {\n const { increment = 0, processId = 1, workerId = 0, epoch } = opts;\n this._epoch = timestampMs(isBigInt(epoch) ? Number(epoch) : epoch);\n this.workerId = workerId;\n this.processId = processId;\n this.increment = increment;\n }\n\n this._buffer = new Uint8Array(8);\n }\n\n /**\n * The epoch for this snowflake, as a number\n */\n public get epoch(): number {\n return this._epoch;\n }\n\n /**\n * Gets the configured process ID\n */\n public get processId(): number {\n return this._processId;\n }\n\n /**\n * Sets the process ID that will be used by default for the {@link generate} method\n * @param value The new value, will be masked with `0b11111`\n */\n public set processId(value: number | bigint) {\n this._processId = Number(value) & MAX_PROCESS_ID;\n }\n\n /**\n * Gets the configured worker ID\n */\n public get workerId(): number {\n return this._workerId;\n }\n\n /**\n * Sets the worker ID that will be used by default for the {@link generate} method\n * @param value The new value, will be masked with `0b11111`\n */\n public set workerId(value: number | bigint) {\n this._workerId = Number(value) & MAX_WORKER_ID;\n }\n\n /**\n * Get incrementor for generating snowflakes\n */\n public get increment(): number {\n return this._increment;\n }\n\n /**\n * Sets the incrementor for generating snowflakes that will be used by default for the {@link generate} method\n * @param value The new value\n */\n public set increment(value: number | bigint) {\n this._increment = Number(value) & MAX_INCREMENT;\n }\n\n private _createSavePoint(): AnyFunction {\n var increment = this._increment;\n var workerId = this._workerId;\n var processId = this._processId;\n\n return () => {\n this._increment = increment;\n this._workerId = workerId;\n this._processId = processId;\n };\n }\n\n /**\n * Sets most lowest values for generating snowflakes that will be used by default for the {@link generate} method\n */\n setLowest(): void {\n this._increment = 0;\n this._workerId = 0;\n this._processId = 0;\n }\n\n /**\n * Sets most highest values for generating snowflakes that will be used by default for the {@link generate} method\n */\n setHighest(): void {\n this._increment = MAX_INCREMENT;\n this._workerId = MAX_WORKER_ID;\n this._processId = MAX_PROCESS_ID;\n }\n\n /**\n * Execute a function in context of most lowest values for generating snowflakes\n * @example\n * ```typescript\n * const epoch = new Date('2000-01-01T00:00:00.000Z');\n * const snowflake = new Snowflake();\n * const id = snowflake.withLowest((v) => v.generate(epoch)); // the lowest possible id for that epoch\n * ```\n */\n withLowest<Result = undefined>(fn: (instance: Snowflake) => Result): Result {\n assert.fn(fn, 'withLowest expected a function as first argument');\n\n var reset = this._createSavePoint();\n this.setLowest();\n\n var result = fn(this);\n reset();\n\n return result;\n }\n\n /**\n * Execute a function in context of most highest values for generating snowflakes\n * @example\n * ```typescript\n * const epoch = new Date('2000-01-01T00:00:00.000Z');\n * const snowflake = new Snowflake();\n * const id = snowflake.withLowest((v) => v.generate(epoch)); // the highest possible id for that epoch\n * ```\n */\n withHighest<Result = undefined>(fn: (instance: Snowflake) => Result): Result {\n assert.fn(fn, 'withHighest expected a function as first argument');\n\n var reset = this._createSavePoint();\n\n this.setHighest();\n\n var result = fn(this);\n reset();\n\n return result;\n }\n\n /**\n * Generates a Snowflake ID as bigint.\n */\n public generate(timestamp: Date | number = Date.now()): bigint {\n if (timestamp instanceof Date) timestamp = timestamp.getTime();\n if (!isNumber(timestamp)) {\n throw new Error(\n `\"timestamp\" argument must be a number or Date (received ${typeof timestamp})`,\n );\n }\n\n var increment = this._increment;\n this._increment = (this._increment + 1) & MAX_INCREMENT;\n\n return (\n (BigInt(timestamp - this._epoch) << 22n) |\n (BigInt(this._workerId & MAX_WORKER_ID) << 17n) |\n (BigInt(this._processId & MAX_PROCESS_ID) << 12n) |\n BigInt(increment & MAX_INCREMENT)\n );\n }\n\n /**\n * Generates a Snowflake ID as a `Uint8Array` buffer.\n *\n * ⚠️ Returns a reference to the **same** internal `Uint8Array` instance.\n * Its contents may be overwritten on the next ID generation call.\n *\n * This method is intended for high-performance scenarios where you\n * immediately transform the buffer (e.g., to base62) before calling again.\n * Avoid storing or mutating the returned buffer directly.\n */\n public generateBufferUnsafe(\n timestamp: Date | number = Date.now(),\n ): Uint8Array {\n if (timestamp instanceof Date) timestamp = timestamp.getTime();\n if (!isNumber(timestamp)) {\n throw new Error(\n `\"timestamp\" argument must be a number or Date (received ${typeof timestamp})`,\n );\n }\n\n var increment = this._increment;\n this._increment = (this._increment + 1) & MAX_INCREMENT;\n\n // Calculate the timestamp delta\n var timestampDelta = timestamp - this._epoch;\n\n // Split into 32-bit parts for bitwise operations\n var timestampHigh = Math.floor(timestampDelta / 0xffffffff);\n var timestampLow = timestampDelta >>> 0;\n\n var high32 = ((timestampHigh << 22) | (timestampLow >>> 10)) >>> 0;\n var low32 =\n ((timestampLow << 22) |\n (this._workerId << 17) |\n (this._processId << 12) |\n (increment & MAX_INCREMENT)) >>>\n 0;\n\n var buffer = this._buffer;\n\n buffer[0] = high32 >>> 24;\n buffer[1] = high32 >>> 16;\n buffer[2] = high32 >>> 8;\n buffer[3] = high32;\n buffer[4] = low32 >>> 24;\n buffer[5] = low32 >>> 16;\n buffer[6] = low32 >>> 8;\n buffer[7] = low32;\n\n return buffer;\n }\n\n /**\n * Generates a snowflake given an epoch and optionally a timestamp\n * @example\n * ```typescript\n * const epoch = new Date('2000-01-01T00:00:00.000Z');\n * const snowflake = new Snowflake({ epoch }).generate();\n * ```\n * @returns A unique snowflake as Uint8Array\n */\n public generateBuffer(timestamp: Date | number = Date.now()): Uint8Array {\n this.generateBufferUnsafe(timestamp);\n return new Uint8Array(this._buffer);\n }\n\n /**\n * Deconstructs a snowflake given a snowflake ID\n * @param id the snowflake to deconstruct\n * @returns a deconstructed snowflake\n * @example\n * ```typescript\n * const epoch = new Date('2000-01-01T00:00:00.000Z');\n * const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');\n * ```\n */\n public deconstruct(id: string | bigint | Uint8Array): DeconstructedSnowflake {\n var high32: number, low32: number;\n\n if (id instanceof Uint8Array) {\n // Convert from Uint8Array\n high32 = ((id[0] << 24) | (id[1] << 16) | (id[2] << 8) | id[3]) >>> 0;\n low32 = ((id[4] << 24) | (id[5] << 16) | (id[6] << 8) | id[7]) >>> 0;\n } else if (isBigInt(id)) {\n return this.deconstruct(bigIntBytes(id));\n } else {\n // Convert from string\n return this.deconstruct(bigIntBytes(BigInt(id)));\n }\n\n return {\n id: BigInt(high32) * 0x100000000n + BigInt(low32),\n timestamp: high32 * 0x400 + (low32 >>> 22) + this._epoch,\n workerId: (low32 >>> 17) & MAX_WORKER_ID,\n processId: (low32 >>> 12) & MAX_PROCESS_ID,\n increment: low32 & MAX_INCREMENT,\n epoch: this._epoch,\n };\n }\n\n /**\n * Retrieves the timestamp field's value from a snowflake.\n * @param id The snowflake to get the timestamp value from.\n * @returns The UNIX timestamp that is stored in `id`.\n */\n public timestampFrom(id: string | bigint | Uint8Array): number {\n if (isString(id) || isBigInt(id)) {\n return this.timestampFrom(Snowflake.bufferFrom(id));\n }\n\n var high32 = ((id[0] << 24) | (id[1] << 16) | (id[2] << 8) | id[3]) >>> 0;\n var low32 = ((id[4] << 24) | (id[5] << 16) | (id[6] << 8) | id[7]) >>> 0;\n\n return high32 * 0x400 + (low32 >>> 22) + this._epoch;\n }\n\n /**\n * Returns a number indicating whether a reference snowflake comes before, or after, or is same as the given\n * snowflake in sort order.\n * @param a The first snowflake to compare.\n * @param b The second snowflake to compare.\n * @returns `-1` if `a` is older than `b`, `0` if `a` and `b` are equals, `1` if `a` is newer than `b`.\n */\n public static compare(\n a: string | bigint | Uint8Array,\n b: string | bigint | Uint8Array,\n ): -1 | 0 | 1 {\n if (isString(a)) {\n a = BigInt(a);\n } else if (a instanceof Uint8Array) {\n a = bigIntFromBytes(a);\n }\n\n if (isString(b)) {\n b = BigInt(b);\n } else if (b instanceof Uint8Array) {\n b = bigIntFromBytes(b);\n }\n\n return a === b ? 0 : a < b ? -1 : 1;\n }\n\n /**\n * Parse value as Uint8Array buffer\n */\n public static bufferFrom(value: bigint | string): Uint8Array {\n if (isString(value)) value = BigInt(value);\n\n var buf = bigIntBytes(value);\n var bufSize = buf.byteLength;\n\n // Add padding\n if (buf.byteLength < 8) {\n var buf2 = new Uint8Array(8);\n buf2.set(buf, 8 - bufSize);\n buf = buf2;\n }\n\n return buf;\n }\n}\n"],"names":["isDate","isNumber","timestampMs","isBigInt","assert","bigIntBytes","isString","bigIntFromBytes"],"mappings":";;;;AAkFO,IAAI,aAAA,GAAgB;AAKpB,IAAI,cAAA,GAAiB;AAKrB,IAAI,aAAA,GAAgB;AAiBpB,MAAM,SAAA,CAAU;AAAA;AAAA;AAAA;AAAA,EAId,SAAS,IAAA,CAAK,WAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMJ,MAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMT,UAAA,GAAa,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMb,UAAA,GAAa,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMb,SAAA,GAAY,CAAA;AAAA;AAAA;AAAA;AAAA,EAKZ,OAAA;AAAA;AAAA;AAAA;AAAA,EAKD,YAAY,IAAA,EAAiD;AAClE,IAAA,IAAIA,cAAA,CAAO,IAAI,CAAA,IA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AAA;AAAA,IACvB;AAEA,IAAA,IAAID,gBAAA,CAAS,CAAC,CAAA,EAAG;AACf,MAAA,CAAA,GAAI,OAAO,CAAC,CAAA;AAAA,IACd,CAAA,MAAA,IAAW,aAAa,UAAA,EAAY;AAClC,MAAA,CAAA,GAAIC,wBAAgB,CAAC,CAAA;AAAA,IACvB;AAEA,IAAA,OAAO,CAAA,KAAM,CAAA,GAAI,CAAA,GAAI,CAAA,GAAI,IAAI,EAAA,GAAK,CAAA;AAAA,EACpC;AAAA;AAAA;AAAA;AAAA,EAKA,OAAc,WAAW,KAAA,EAAoC;AAC3D,IAAA,IAAID,gBAAA,CAAS,KAAK,CAAA,EAAG,KAAA,GAAQ,OAAO,KAAK,CAAA;AAEzC,IAAA,IAAI,GAAA,GAAMD,oBAAY,KAAK,CAAA;AAC3B,IAAA,IAAI,UAAU,GAAA,CAAI,UAAA;AAGlB,IAAA,IAAI,GAAA,CAAI,aAAa,CAAA,EAAG;AACtB,MAAA,IAAI,IAAA,GAAO,IAAI,UAAA,CAAW,CAAC,CAAA;AAC3B,MAAA,IAAA,CAAK,GAAA,CAAI,GAAA,EAAK,CAAA,GAAI,OAAO,CAAA;AACzB,MAAA,GAAA,GAAM,IAAA;AAAA,IACR;AAEA,IAAA,OAAO,GAAA;AAAA,EACT;AACF;;;;;;;"}
|
package/dist/index.d.cts
DELETED
|
@@ -1,231 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Object returned by Snowflake#deconstruct
|
|
3
|
-
*/
|
|
4
|
-
interface DeconstructedSnowflake {
|
|
5
|
-
/**
|
|
6
|
-
* The id as a number
|
|
7
|
-
*/
|
|
8
|
-
id: bigint;
|
|
9
|
-
/**
|
|
10
|
-
* The timestamp stored in the snowflake
|
|
11
|
-
*/
|
|
12
|
-
timestamp: number;
|
|
13
|
-
/**
|
|
14
|
-
* The worker id stored in the snowflake
|
|
15
|
-
*/
|
|
16
|
-
workerId: number;
|
|
17
|
-
/**
|
|
18
|
-
* The process id stored in the snowflake
|
|
19
|
-
*/
|
|
20
|
-
processId: number;
|
|
21
|
-
/**
|
|
22
|
-
* The increment stored in the snowflake
|
|
23
|
-
*/
|
|
24
|
-
increment: number;
|
|
25
|
-
/**
|
|
26
|
-
* The epoch to use in the snowflake
|
|
27
|
-
*/
|
|
28
|
-
epoch: number;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Options for Snowflake
|
|
32
|
-
*/
|
|
33
|
-
interface SnowflakeOptions {
|
|
34
|
-
/**
|
|
35
|
-
* Timestamp or date of the snowflake to generate
|
|
36
|
-
*/
|
|
37
|
-
epoch: number | bigint | Date;
|
|
38
|
-
/**
|
|
39
|
-
* The increment to use
|
|
40
|
-
* @default 0
|
|
41
|
-
* @remark keep in mind that this number is auto-incremented between generate calls
|
|
42
|
-
*/
|
|
43
|
-
increment?: number | bigint;
|
|
44
|
-
/**
|
|
45
|
-
* The worker ID to use, will be truncated to 5 bits (0-31)
|
|
46
|
-
* @default 0
|
|
47
|
-
*/
|
|
48
|
-
workerId?: number | bigint;
|
|
49
|
-
/**
|
|
50
|
-
* The process ID to use, will be truncated to 5 bits (0-31)
|
|
51
|
-
* @default 1
|
|
52
|
-
*/
|
|
53
|
-
processId?: number | bigint;
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* The maximum value the `workerId` field accepts in snowflakes.
|
|
57
|
-
*/
|
|
58
|
-
declare var MAX_WORKER_ID: number;
|
|
59
|
-
/**
|
|
60
|
-
* The maximum value the `processId` field accepts in snowflakes.
|
|
61
|
-
*/
|
|
62
|
-
declare var MAX_PROCESS_ID: number;
|
|
63
|
-
/**
|
|
64
|
-
* The maximum value the `increment` field accepts in snowflakes.
|
|
65
|
-
*/
|
|
66
|
-
declare var MAX_INCREMENT: number;
|
|
67
|
-
/**
|
|
68
|
-
* A class for generating and deconstructing Twitter snowflakes.
|
|
69
|
-
*
|
|
70
|
-
* A {@link https://developer.twitter.com/en/docs/twitter-ids Twitter snowflake}
|
|
71
|
-
* is a 64-bit unsigned integer with 4 fields that have a fixed epoch value.
|
|
72
|
-
*
|
|
73
|
-
* If we have a snowflake `266241948824764416` we can represent it as binary:
|
|
74
|
-
* ```
|
|
75
|
-
* 64 22 17 12 0
|
|
76
|
-
* 000000111011000111100001101001000101000000 00001 00000 000000000000
|
|
77
|
-
* number of ms since epoch worker pid increment
|
|
78
|
-
* ```
|
|
79
|
-
*
|
|
80
|
-
* @group Main
|
|
81
|
-
*/
|
|
82
|
-
declare class Snowflake {
|
|
83
|
-
/**
|
|
84
|
-
* Alias for {@link deconstruct}
|
|
85
|
-
*/
|
|
86
|
-
decode: (id: string | bigint | Uint8Array) => DeconstructedSnowflake;
|
|
87
|
-
/**
|
|
88
|
-
* Internal reference of the epoch passed in the constructor
|
|
89
|
-
* @internal
|
|
90
|
-
*/
|
|
91
|
-
private readonly _epoch;
|
|
92
|
-
/**
|
|
93
|
-
* Internal incrementor for generating snowflakes
|
|
94
|
-
* @internal
|
|
95
|
-
*/
|
|
96
|
-
private _increment;
|
|
97
|
-
/**
|
|
98
|
-
* The process ID that will be used by default in the generate method
|
|
99
|
-
* @internal
|
|
100
|
-
*/
|
|
101
|
-
private _processId;
|
|
102
|
-
/**
|
|
103
|
-
* The worker ID that will be used by default in the generate method
|
|
104
|
-
* @internal
|
|
105
|
-
*/
|
|
106
|
-
private _workerId;
|
|
107
|
-
/**
|
|
108
|
-
* @internal
|
|
109
|
-
*/
|
|
110
|
-
private _buffer;
|
|
111
|
-
/**
|
|
112
|
-
* @param epoch the epoch to use
|
|
113
|
-
*/
|
|
114
|
-
constructor(opts: SnowflakeOptions | Date | number | bigint);
|
|
115
|
-
/**
|
|
116
|
-
* The epoch for this snowflake, as a number
|
|
117
|
-
*/
|
|
118
|
-
get epoch(): number;
|
|
119
|
-
/**
|
|
120
|
-
* Gets the configured process ID
|
|
121
|
-
*/
|
|
122
|
-
get processId(): number;
|
|
123
|
-
/**
|
|
124
|
-
* Sets the process ID that will be used by default for the {@link generate} method
|
|
125
|
-
* @param value The new value, will be masked with `0b11111`
|
|
126
|
-
*/
|
|
127
|
-
set processId(value: number | bigint);
|
|
128
|
-
/**
|
|
129
|
-
* Gets the configured worker ID
|
|
130
|
-
*/
|
|
131
|
-
get workerId(): number;
|
|
132
|
-
/**
|
|
133
|
-
* Sets the worker ID that will be used by default for the {@link generate} method
|
|
134
|
-
* @param value The new value, will be masked with `0b11111`
|
|
135
|
-
*/
|
|
136
|
-
set workerId(value: number | bigint);
|
|
137
|
-
/**
|
|
138
|
-
* Get incrementor for generating snowflakes
|
|
139
|
-
*/
|
|
140
|
-
get increment(): number;
|
|
141
|
-
/**
|
|
142
|
-
* Sets the incrementor for generating snowflakes that will be used by default for the {@link generate} method
|
|
143
|
-
* @param value The new value
|
|
144
|
-
*/
|
|
145
|
-
set increment(value: number | bigint);
|
|
146
|
-
private _createSavePoint;
|
|
147
|
-
/**
|
|
148
|
-
* Sets most lowest values for generating snowflakes that will be used by default for the {@link generate} method
|
|
149
|
-
*/
|
|
150
|
-
setLowest(): void;
|
|
151
|
-
/**
|
|
152
|
-
* Sets most highest values for generating snowflakes that will be used by default for the {@link generate} method
|
|
153
|
-
*/
|
|
154
|
-
setHighest(): void;
|
|
155
|
-
/**
|
|
156
|
-
* Execute a function in context of most lowest values for generating snowflakes
|
|
157
|
-
* @example
|
|
158
|
-
* ```typescript
|
|
159
|
-
* const epoch = new Date('2000-01-01T00:00:00.000Z');
|
|
160
|
-
* const snowflake = new Snowflake();
|
|
161
|
-
* const id = snowflake.withLowest((v) => v.generate(epoch)); // the lowest possible id for that epoch
|
|
162
|
-
* ```
|
|
163
|
-
*/
|
|
164
|
-
withLowest<Result = undefined>(fn: (instance: Snowflake) => Result): Result;
|
|
165
|
-
/**
|
|
166
|
-
* Execute a function in context of most highest values for generating snowflakes
|
|
167
|
-
* @example
|
|
168
|
-
* ```typescript
|
|
169
|
-
* const epoch = new Date('2000-01-01T00:00:00.000Z');
|
|
170
|
-
* const snowflake = new Snowflake();
|
|
171
|
-
* const id = snowflake.withLowest((v) => v.generate(epoch)); // the highest possible id for that epoch
|
|
172
|
-
* ```
|
|
173
|
-
*/
|
|
174
|
-
withHighest<Result = undefined>(fn: (instance: Snowflake) => Result): Result;
|
|
175
|
-
/**
|
|
176
|
-
* Generates a Snowflake ID as bigint.
|
|
177
|
-
*/
|
|
178
|
-
generate(timestamp?: Date | number): bigint;
|
|
179
|
-
/**
|
|
180
|
-
* Generates a Snowflake ID as a `Uint8Array` buffer.
|
|
181
|
-
*
|
|
182
|
-
* ⚠️ Returns a reference to the **same** internal `Uint8Array` instance.
|
|
183
|
-
* Its contents may be overwritten on the next ID generation call.
|
|
184
|
-
*
|
|
185
|
-
* This method is intended for high-performance scenarios where you
|
|
186
|
-
* immediately transform the buffer (e.g., to base62) before calling again.
|
|
187
|
-
* Avoid storing or mutating the returned buffer directly.
|
|
188
|
-
*/
|
|
189
|
-
generateBufferUnsafe(timestamp?: Date | number): Uint8Array;
|
|
190
|
-
/**
|
|
191
|
-
* Generates a snowflake given an epoch and optionally a timestamp
|
|
192
|
-
* @example
|
|
193
|
-
* ```typescript
|
|
194
|
-
* const epoch = new Date('2000-01-01T00:00:00.000Z');
|
|
195
|
-
* const snowflake = new Snowflake({ epoch }).generate();
|
|
196
|
-
* ```
|
|
197
|
-
* @returns A unique snowflake as Uint8Array
|
|
198
|
-
*/
|
|
199
|
-
generateBuffer(timestamp?: Date | number): Uint8Array;
|
|
200
|
-
/**
|
|
201
|
-
* Deconstructs a snowflake given a snowflake ID
|
|
202
|
-
* @param id the snowflake to deconstruct
|
|
203
|
-
* @returns a deconstructed snowflake
|
|
204
|
-
* @example
|
|
205
|
-
* ```typescript
|
|
206
|
-
* const epoch = new Date('2000-01-01T00:00:00.000Z');
|
|
207
|
-
* const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');
|
|
208
|
-
* ```
|
|
209
|
-
*/
|
|
210
|
-
deconstruct(id: string | bigint | Uint8Array): DeconstructedSnowflake;
|
|
211
|
-
/**
|
|
212
|
-
* Retrieves the timestamp field's value from a snowflake.
|
|
213
|
-
* @param id The snowflake to get the timestamp value from.
|
|
214
|
-
* @returns The UNIX timestamp that is stored in `id`.
|
|
215
|
-
*/
|
|
216
|
-
timestampFrom(id: string | bigint | Uint8Array): number;
|
|
217
|
-
/**
|
|
218
|
-
* Returns a number indicating whether a reference snowflake comes before, or after, or is same as the given
|
|
219
|
-
* snowflake in sort order.
|
|
220
|
-
* @param a The first snowflake to compare.
|
|
221
|
-
* @param b The second snowflake to compare.
|
|
222
|
-
* @returns `-1` if `a` is older than `b`, `0` if `a` and `b` are equals, `1` if `a` is newer than `b`.
|
|
223
|
-
*/
|
|
224
|
-
static compare(a: string | bigint | Uint8Array, b: string | bigint | Uint8Array): -1 | 0 | 1;
|
|
225
|
-
/**
|
|
226
|
-
* Parse value as Uint8Array buffer
|
|
227
|
-
*/
|
|
228
|
-
static bufferFrom(value: bigint | string): Uint8Array;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
export { type DeconstructedSnowflake, MAX_INCREMENT, MAX_PROCESS_ID, MAX_WORKER_ID, Snowflake, type SnowflakeOptions };
|