@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.ts 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 };