@andrew_l/snowflake 0.3.22 → 0.4.1
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.d.mts
CHANGED
|
@@ -2,55 +2,55 @@
|
|
|
2
2
|
* Object returned by Snowflake#deconstruct
|
|
3
3
|
*/
|
|
4
4
|
interface DeconstructedSnowflake {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
29
|
}
|
|
30
30
|
/**
|
|
31
31
|
* Options for Snowflake
|
|
32
32
|
*/
|
|
33
33
|
interface SnowflakeOptions {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
54
|
}
|
|
55
55
|
/**
|
|
56
56
|
* The maximum value the `workerId` field accepts in snowflakes.
|
|
@@ -80,152 +80,157 @@ declare var MAX_INCREMENT: number;
|
|
|
80
80
|
* @group Main
|
|
81
81
|
*/
|
|
82
82
|
declare class Snowflake {
|
|
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
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
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 _timestamp;
|
|
111
|
+
private __buf;
|
|
112
|
+
private _packBuffer;
|
|
113
|
+
private _packBigInt;
|
|
114
|
+
private _unpackBuffer;
|
|
115
|
+
private _unpackBigInt;
|
|
116
|
+
/**
|
|
117
|
+
* @param epoch the epoch to use
|
|
118
|
+
*/
|
|
119
|
+
constructor(opts: SnowflakeOptions | Date | number | bigint);
|
|
120
|
+
/**
|
|
121
|
+
* The epoch for this snowflake, as a number
|
|
122
|
+
*/
|
|
123
|
+
get epoch(): number;
|
|
124
|
+
/**
|
|
125
|
+
* Gets the configured process ID
|
|
126
|
+
*/
|
|
127
|
+
get processId(): number;
|
|
128
|
+
/**
|
|
129
|
+
* Sets the process ID that will be used by default for the {@link generate} method
|
|
130
|
+
* @param value The new value, will be masked with `0b11111`
|
|
131
|
+
*/
|
|
132
|
+
set processId(value: number | bigint);
|
|
133
|
+
/**
|
|
134
|
+
* Gets the configured worker ID
|
|
135
|
+
*/
|
|
136
|
+
get workerId(): number;
|
|
137
|
+
/**
|
|
138
|
+
* Sets the worker ID that will be used by default for the {@link generate} method
|
|
139
|
+
* @param value The new value, will be masked with `0b11111`
|
|
140
|
+
*/
|
|
141
|
+
set workerId(value: number | bigint);
|
|
142
|
+
/**
|
|
143
|
+
* Get incrementor for generating snowflakes
|
|
144
|
+
*/
|
|
145
|
+
get increment(): number;
|
|
146
|
+
/**
|
|
147
|
+
* Sets the incrementor for generating snowflakes that will be used by default for the {@link generate} method
|
|
148
|
+
* @param value The new value
|
|
149
|
+
*/
|
|
150
|
+
set increment(value: number | bigint);
|
|
151
|
+
private _createSavePoint;
|
|
152
|
+
/**
|
|
153
|
+
* Sets most lowest values for generating snowflakes that will be used by default for the {@link generate} method
|
|
154
|
+
*/
|
|
155
|
+
setLowest(): void;
|
|
156
|
+
/**
|
|
157
|
+
* Sets most highest values for generating snowflakes that will be used by default for the {@link generate} method
|
|
158
|
+
*/
|
|
159
|
+
setHighest(): void;
|
|
160
|
+
/**
|
|
161
|
+
* Execute a function in context of most lowest values for generating snowflakes
|
|
162
|
+
* @example
|
|
163
|
+
* ```typescript
|
|
164
|
+
* const epoch = new Date('2000-01-01T00:00:00.000Z');
|
|
165
|
+
* const snowflake = new Snowflake();
|
|
166
|
+
* const id = snowflake.withLowest((v) => v.generate(epoch)); // the lowest possible id for that epoch
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
withLowest<Result = undefined>(fn: (instance: Snowflake) => Result): Result;
|
|
170
|
+
/**
|
|
171
|
+
* Execute a function in context of most highest values for generating snowflakes
|
|
172
|
+
* @example
|
|
173
|
+
* ```typescript
|
|
174
|
+
* const epoch = new Date('2000-01-01T00:00:00.000Z');
|
|
175
|
+
* const snowflake = new Snowflake();
|
|
176
|
+
* const id = snowflake.withLowest((v) => v.generate(epoch)); // the highest possible id for that epoch
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
179
|
+
withHighest<Result = undefined>(fn: (instance: Snowflake) => Result): Result;
|
|
180
|
+
/**
|
|
181
|
+
* Generates a Snowflake ID as bigint.
|
|
182
|
+
*/
|
|
183
|
+
generate(timestamp?: Date | number): bigint;
|
|
184
|
+
/**
|
|
185
|
+
* Generates a Snowflake ID as a `Uint8Array` buffer.
|
|
186
|
+
*
|
|
187
|
+
* ⚠️ Returns a reference to the **same** internal `Uint8Array` instance.
|
|
188
|
+
* Its contents may be overwritten on the next ID generation call.
|
|
189
|
+
*
|
|
190
|
+
* This method is intended for high-performance scenarios where you
|
|
191
|
+
* immediately transform the buffer (e.g., to base62) before calling again.
|
|
192
|
+
* Avoid storing or mutating the returned buffer directly.
|
|
193
|
+
*/
|
|
194
|
+
generateBufferUnsafe(timestamp?: Date | number): Uint8Array;
|
|
195
|
+
/**
|
|
196
|
+
* Generates a snowflake given an epoch and optionally a timestamp
|
|
197
|
+
* @example
|
|
198
|
+
* ```typescript
|
|
199
|
+
* const epoch = new Date('2000-01-01T00:00:00.000Z');
|
|
200
|
+
* const snowflake = new Snowflake({ epoch }).generate();
|
|
201
|
+
* ```
|
|
202
|
+
* @returns A unique snowflake as Uint8Array
|
|
203
|
+
*/
|
|
204
|
+
generateBuffer(timestamp?: Date | number): Uint8Array;
|
|
205
|
+
/**
|
|
206
|
+
* Deconstructs a snowflake given a snowflake ID
|
|
207
|
+
* @param id the snowflake to deconstruct
|
|
208
|
+
* @returns a deconstructed snowflake
|
|
209
|
+
* @example
|
|
210
|
+
* ```typescript
|
|
211
|
+
* const epoch = new Date('2000-01-01T00:00:00.000Z');
|
|
212
|
+
* const snowflake = new Snowflake(epoch).deconstruct('3971046231244935168');
|
|
213
|
+
* ```
|
|
214
|
+
*/
|
|
215
|
+
deconstruct(id: string | bigint | Uint8Array): DeconstructedSnowflake;
|
|
216
|
+
/**
|
|
217
|
+
* Retrieves the timestamp field's value from a snowflake.
|
|
218
|
+
* @param id The snowflake to get the timestamp value from.
|
|
219
|
+
* @returns The UNIX timestamp that is stored in `id`.
|
|
220
|
+
*/
|
|
221
|
+
timestampFrom(id: string | bigint | Uint8Array): number;
|
|
222
|
+
/**
|
|
223
|
+
* Returns a number indicating whether a reference snowflake comes before, or after, or is same as the given
|
|
224
|
+
* snowflake in sort order.
|
|
225
|
+
* @param a The first snowflake to compare.
|
|
226
|
+
* @param b The second snowflake to compare.
|
|
227
|
+
* @returns `-1` if `a` is older than `b`, `0` if `a` and `b` are equals, `1` if `a` is newer than `b`.
|
|
228
|
+
*/
|
|
229
|
+
static compare(a: string | bigint | Uint8Array, b: string | bigint | Uint8Array): -1 | 0 | 1;
|
|
230
|
+
/**
|
|
231
|
+
* Parse value as Uint8Array buffer
|
|
232
|
+
*/
|
|
233
|
+
static bufferFrom(value: bigint | string): Uint8Array;
|
|
229
234
|
}
|
|
230
|
-
|
|
231
|
-
|
|
235
|
+
export { DeconstructedSnowflake, MAX_INCREMENT, MAX_PROCESS_ID, MAX_WORKER_ID, Snowflake, SnowflakeOptions };
|
|
236
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":["id","timestamp","workerId","processId","increment","epoch","Date","decode","Uint8Array","DeconstructedSnowflake","_epoch","_increment","_processId","_workerId","_timestamp","__buf","_packBuffer","_packBigInt","_unpackBuffer","_unpackBigInt","constructor","SnowflakeOptions","opts","value","_createSavePoint","setLowest","setHighest","withLowest","Result","Snowflake","instance","fn","withHighest","generate","generateBufferUnsafe","generateBuffer","deconstruct","timestampFrom","compare","a","b","bufferFrom"],"sources":["../src/Snowflake.d.ts"],"mappings":"AAGA;;;AAAA,UAAiB,sBAAA;EAIbA;;;EAAAA,EAAAA;EAgBAI;;;EAZAH,SAAAA;E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