detritus-utils 0.5.0-beta.2 → 0.5.0-beta.4
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/LICENSE +21 -0
- package/lib/basecollection.d.ts +12 -0
- package/lib/basecollection.js +105 -16
- package/lib/constants.d.ts +1 -1
- package/lib/constants.js +1 -1
- package/lib/eventspewer.d.ts +4 -1
- package/lib/eventspewer.js +73 -27
- package/lib/snowflake.js +17 -20
- package/lib/tools.js +17 -12
- package/package.json +3 -2
- package/lib/src/basecollection.d.ts +0 -56
- package/lib/src/basecollection.js +0 -298
- package/lib/src/baseset.d.ts +0 -17
- package/lib/src/baseset.js +0 -70
- package/lib/src/constants.d.ts +0 -6
- package/lib/src/constants.js +0 -9
- package/lib/src/eventspewer.d.ts +0 -28
- package/lib/src/eventspewer.js +0 -110
- package/lib/src/index.d.ts +0 -8
- package/lib/src/index.js +0 -50
- package/lib/src/snowflake.d.ts +0 -20
- package/lib/src/snowflake.js +0 -82
- package/lib/src/timers.d.ts +0 -21
- package/lib/src/timers.js +0 -92
- package/lib/src/tools.d.ts +0 -11
- package/lib/src/tools.js +0 -41
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 Dan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/lib/basecollection.d.ts
CHANGED
|
@@ -30,14 +30,17 @@ export interface BaseCollectionOptions {
|
|
|
30
30
|
export declare class BaseCollection<K, V> extends BaseCollectionMixin<K, V> {
|
|
31
31
|
readonly cache: Map<K, V>;
|
|
32
32
|
_lastUsed?: Map<K, number>;
|
|
33
|
+
_order?: Map<K, true>;
|
|
33
34
|
expire?: number;
|
|
34
35
|
interval?: Timers.Interval;
|
|
35
36
|
intervalTime: number;
|
|
36
37
|
limit: number;
|
|
37
38
|
constructor({ expire, intervalTime, limit }?: BaseCollectionOptions);
|
|
38
39
|
get lastUsed(): Map<K, number>;
|
|
40
|
+
touch(key: K): void;
|
|
39
41
|
get shouldStartInterval(): boolean;
|
|
40
42
|
setExpire(value: number): this;
|
|
43
|
+
setLimit(value: number): this;
|
|
41
44
|
setIntervalTimeout(value: number): this;
|
|
42
45
|
startInterval(): void;
|
|
43
46
|
stopInterval(): void;
|
|
@@ -52,6 +55,15 @@ export declare class BaseCollection<K, V> extends BaseCollectionMixin<K, V> {
|
|
|
52
55
|
has(key: K): boolean;
|
|
53
56
|
keys(): IterableIterator<K>;
|
|
54
57
|
set(key: K, value: V): this;
|
|
58
|
+
/**
|
|
59
|
+
* Evicts the least recently used items until there's `room` free under the limit
|
|
60
|
+
*/
|
|
61
|
+
evict(room?: number): void;
|
|
62
|
+
/**
|
|
63
|
+
* The recency order (least recently used first), synced with the cache
|
|
64
|
+
* keys added before `limit` was set (or while it was `Infinity`) aren't tracked yet, so they count as the least recently used
|
|
65
|
+
*/
|
|
66
|
+
getOrder(): Map<K, true>;
|
|
55
67
|
values(): IterableIterator<V>;
|
|
56
68
|
[Symbol.iterator](): IterableIterator<[K, V]>;
|
|
57
69
|
get [Symbol.toStringTag](): string;
|
package/lib/basecollection.js
CHANGED
|
@@ -150,6 +150,7 @@ class BaseCollection extends BaseCollectionMixin {
|
|
|
150
150
|
}
|
|
151
151
|
Object.defineProperties(this, {
|
|
152
152
|
_lastUsed: { enumerable: false, writable: true },
|
|
153
|
+
_order: { enumerable: false, writable: true },
|
|
153
154
|
cache: { enumerable: false },
|
|
154
155
|
expire: { configurable: true, writable: false },
|
|
155
156
|
interval: { enumerable: false, writable: true },
|
|
@@ -166,21 +167,43 @@ class BaseCollection extends BaseCollectionMixin {
|
|
|
166
167
|
}
|
|
167
168
|
return new Map();
|
|
168
169
|
}
|
|
170
|
+
touch(key) {
|
|
171
|
+
const lastUsed = this.lastUsed;
|
|
172
|
+
lastUsed.delete(key);
|
|
173
|
+
lastUsed.set(key, Date.now());
|
|
174
|
+
}
|
|
169
175
|
get shouldStartInterval() {
|
|
170
|
-
return !!this.intervalTime && (!this.interval ||
|
|
176
|
+
return !!this.intervalTime && (!this.interval || !this.interval.hasStarted);
|
|
171
177
|
}
|
|
172
178
|
setExpire(value) {
|
|
179
|
+
const wasExpiring = !!this.expire;
|
|
173
180
|
Object.defineProperty(this, 'expire', { value });
|
|
174
181
|
if (value) {
|
|
182
|
+
Object.defineProperty(this, 'intervalTime', { value: Math.min(value, this.intervalTime) });
|
|
183
|
+
const now = Date.now();
|
|
184
|
+
const lastUsed = this.lastUsed;
|
|
185
|
+
for (let key of this.cache.keys()) {
|
|
186
|
+
if (!wasExpiring || !lastUsed.has(key)) {
|
|
187
|
+
lastUsed.set(key, now);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
175
190
|
if (this.size) {
|
|
176
191
|
this.startInterval();
|
|
177
192
|
}
|
|
178
193
|
}
|
|
179
194
|
else {
|
|
180
195
|
this.stopInterval();
|
|
196
|
+
if (this._lastUsed) {
|
|
197
|
+
this._lastUsed.clear();
|
|
198
|
+
}
|
|
181
199
|
}
|
|
182
200
|
return this;
|
|
183
201
|
}
|
|
202
|
+
setLimit(value) {
|
|
203
|
+
this.limit = value;
|
|
204
|
+
this.evict(0);
|
|
205
|
+
return this;
|
|
206
|
+
}
|
|
184
207
|
setIntervalTimeout(value) {
|
|
185
208
|
Object.defineProperty(this, 'intervalTime', { value });
|
|
186
209
|
if (value) {
|
|
@@ -202,11 +225,11 @@ class BaseCollection extends BaseCollectionMixin {
|
|
|
202
225
|
const expire = this.expire;
|
|
203
226
|
if (expire) {
|
|
204
227
|
const now = Date.now();
|
|
205
|
-
for (let [key,
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
this.delete(key);
|
|
228
|
+
for (let [key, lastUsed] of this.lastUsed) {
|
|
229
|
+
if (now - lastUsed <= expire) {
|
|
230
|
+
break;
|
|
209
231
|
}
|
|
232
|
+
this.delete(key);
|
|
210
233
|
}
|
|
211
234
|
}
|
|
212
235
|
else {
|
|
@@ -233,11 +256,16 @@ class BaseCollection extends BaseCollectionMixin {
|
|
|
233
256
|
if (this._lastUsed) {
|
|
234
257
|
this._lastUsed.clear();
|
|
235
258
|
}
|
|
259
|
+
if (this._order) {
|
|
260
|
+
this._order.clear();
|
|
261
|
+
}
|
|
236
262
|
}
|
|
237
263
|
clone() {
|
|
238
264
|
const collection = new BaseCollection(this);
|
|
239
|
-
|
|
240
|
-
|
|
265
|
+
// insert least recently used first so the clone keeps the same eviction order
|
|
266
|
+
const keys = (this.limit !== Infinity) ? this.getOrder().keys() : this.cache.keys();
|
|
267
|
+
for (let key of keys) {
|
|
268
|
+
collection.set(key, this.cache.get(key));
|
|
241
269
|
}
|
|
242
270
|
return collection;
|
|
243
271
|
}
|
|
@@ -246,6 +274,9 @@ class BaseCollection extends BaseCollectionMixin {
|
|
|
246
274
|
if (this._lastUsed) {
|
|
247
275
|
this._lastUsed.delete(key);
|
|
248
276
|
}
|
|
277
|
+
if (this._order) {
|
|
278
|
+
this._order.delete(key);
|
|
279
|
+
}
|
|
249
280
|
if (!this.cache.size) {
|
|
250
281
|
this.stopInterval();
|
|
251
282
|
}
|
|
@@ -256,8 +287,16 @@ class BaseCollection extends BaseCollectionMixin {
|
|
|
256
287
|
}
|
|
257
288
|
get(key) {
|
|
258
289
|
const value = this.cache.get(key);
|
|
259
|
-
if (this.expire && value) {
|
|
260
|
-
this.
|
|
290
|
+
if ((this.limit !== Infinity || this.expire) && (value !== undefined || this.cache.has(key))) {
|
|
291
|
+
if (this.limit !== Infinity) {
|
|
292
|
+
// mark it as the most recently used
|
|
293
|
+
const order = this.getOrder();
|
|
294
|
+
order.delete(key);
|
|
295
|
+
order.set(key, true);
|
|
296
|
+
}
|
|
297
|
+
if (this.expire) {
|
|
298
|
+
this.touch(key);
|
|
299
|
+
}
|
|
261
300
|
}
|
|
262
301
|
return value;
|
|
263
302
|
}
|
|
@@ -283,22 +322,72 @@ class BaseCollection extends BaseCollectionMixin {
|
|
|
283
322
|
return this.cache.keys();
|
|
284
323
|
}
|
|
285
324
|
set(key, value) {
|
|
325
|
+
if (this.limit !== Infinity) {
|
|
326
|
+
if (this.limit <= 0) {
|
|
327
|
+
this.evict(0);
|
|
328
|
+
return this;
|
|
329
|
+
}
|
|
330
|
+
const order = this.getOrder();
|
|
331
|
+
if (this.cache.has(key)) {
|
|
332
|
+
order.delete(key);
|
|
333
|
+
}
|
|
334
|
+
else {
|
|
335
|
+
// make room for the new key, evicting the least recently used ones
|
|
336
|
+
this.evict(1);
|
|
337
|
+
}
|
|
338
|
+
order.set(key, true);
|
|
339
|
+
}
|
|
286
340
|
this.cache.set(key, value);
|
|
287
341
|
if (this.expire) {
|
|
288
|
-
this.
|
|
342
|
+
this.touch(key);
|
|
289
343
|
if (this.shouldStartInterval) {
|
|
290
344
|
this.startInterval();
|
|
291
345
|
}
|
|
292
346
|
}
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
347
|
+
return this;
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Evicts the least recently used items until there's `room` free under the limit
|
|
351
|
+
*/
|
|
352
|
+
evict(room = 0) {
|
|
353
|
+
if (this.limit === Infinity) {
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
const max = Math.max(this.limit - room, 0);
|
|
357
|
+
if (this.cache.size <= max) {
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
const order = this.getOrder();
|
|
361
|
+
for (let key of order.keys()) {
|
|
362
|
+
if (this.cache.size <= max) {
|
|
363
|
+
break;
|
|
364
|
+
}
|
|
365
|
+
this.delete(key);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* The recency order (least recently used first), synced with the cache
|
|
370
|
+
* keys added before `limit` was set (or while it was `Infinity`) aren't tracked yet, so they count as the least recently used
|
|
371
|
+
*/
|
|
372
|
+
getOrder() {
|
|
373
|
+
let order = this._order;
|
|
374
|
+
if (!order || order.size !== this.cache.size) {
|
|
375
|
+
const synced = new Map();
|
|
376
|
+
for (let key of this.cache.keys()) {
|
|
377
|
+
if (!order || !order.has(key)) {
|
|
378
|
+
synced.set(key, true);
|
|
298
379
|
}
|
|
299
380
|
}
|
|
381
|
+
if (order) {
|
|
382
|
+
for (let key of order.keys()) {
|
|
383
|
+
if (this.cache.has(key)) {
|
|
384
|
+
synced.set(key, true);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
order = this._order = synced;
|
|
300
389
|
}
|
|
301
|
-
return
|
|
390
|
+
return order;
|
|
302
391
|
}
|
|
303
392
|
values() {
|
|
304
393
|
return this.cache.values();
|
package/lib/constants.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export declare const Package: Readonly<{
|
|
2
2
|
URL: "https://github.com/detritusjs/utils";
|
|
3
|
-
VERSION: "0.5.0-beta.
|
|
3
|
+
VERSION: "0.5.0-beta.4";
|
|
4
4
|
}>;
|
|
5
5
|
export declare const DISCORD_SNOWFLAKE_EPOCH = 1420070400000;
|
|
6
6
|
export declare const DISCORD_TOKEN_EPOCH = 1293840000000;
|
package/lib/constants.js
CHANGED
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.DISCORD_TOKEN_EPOCH = exports.DISCORD_SNOWFLAKE_EPOCH = exports.Package = void 0;
|
|
4
4
|
exports.Package = Object.freeze({
|
|
5
5
|
URL: 'https://github.com/detritusjs/utils',
|
|
6
|
-
VERSION: '0.5.0-beta.
|
|
6
|
+
VERSION: '0.5.0-beta.4',
|
|
7
7
|
});
|
|
8
8
|
exports.DISCORD_SNOWFLAKE_EPOCH = 1420070400000;
|
|
9
9
|
exports.DISCORD_TOKEN_EPOCH = 1293840000000;
|
package/lib/eventspewer.d.ts
CHANGED
|
@@ -10,11 +10,14 @@ export declare class EventSpewer extends Emitter implements EventEmitter {
|
|
|
10
10
|
/**
|
|
11
11
|
* @ignore
|
|
12
12
|
*/
|
|
13
|
-
_subscriptions: Array<EventSubscription
|
|
13
|
+
_subscriptions: Map<string | symbol, Array<EventSubscription>> | undefined;
|
|
14
14
|
constructor();
|
|
15
15
|
hasEventListener(name: string | symbol): boolean;
|
|
16
16
|
subscribe(name: string | symbol, listener: (...args: any[]) => void): EventSubscription;
|
|
17
|
+
_deleteSubscriptions(name: string | symbol, subscriptions: Array<EventSubscription>): void;
|
|
17
18
|
removeSubscription(subscription: EventSubscription): void;
|
|
19
|
+
removeListener(name: string | symbol, listener: (...args: any[]) => void, ...args: Array<any>): this;
|
|
20
|
+
off(name: string | symbol, listener: (...args: any[]) => void, ...args: Array<any>): this;
|
|
18
21
|
removeAllListeners(name?: string | symbol): this;
|
|
19
22
|
removeAllSubscriptions(): this;
|
|
20
23
|
}
|
package/lib/eventspewer.js
CHANGED
|
@@ -8,10 +8,6 @@ try {
|
|
|
8
8
|
}
|
|
9
9
|
catch (e) { }
|
|
10
10
|
class EventSpewer extends Emitter {
|
|
11
|
-
/**
|
|
12
|
-
* @ignore
|
|
13
|
-
*/
|
|
14
|
-
_events;
|
|
15
11
|
/**
|
|
16
12
|
* @ignore
|
|
17
13
|
*/
|
|
@@ -32,58 +28,108 @@ class EventSpewer extends Emitter {
|
|
|
32
28
|
const subscription = new EventSubscription(this, name, listener);
|
|
33
29
|
this.on(name, listener);
|
|
34
30
|
if (!this._subscriptions) {
|
|
35
|
-
this._subscriptions =
|
|
31
|
+
this._subscriptions = new Map();
|
|
32
|
+
}
|
|
33
|
+
let subscriptions = this._subscriptions.get(name);
|
|
34
|
+
if (!subscriptions) {
|
|
35
|
+
this._subscriptions.set(name, subscriptions = []);
|
|
36
36
|
}
|
|
37
|
-
|
|
37
|
+
subscriptions.push(subscription);
|
|
38
38
|
return subscription;
|
|
39
39
|
}
|
|
40
|
+
_deleteSubscriptions(name, subscriptions) {
|
|
41
|
+
if (!subscriptions.length && this._subscriptions) {
|
|
42
|
+
this._subscriptions.delete(name);
|
|
43
|
+
if (!this._subscriptions.size) {
|
|
44
|
+
this._subscriptions = undefined;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
40
48
|
removeSubscription(subscription) {
|
|
49
|
+
if (this._subscriptions) {
|
|
50
|
+
const subscriptions = this._subscriptions.get(subscription.name);
|
|
51
|
+
if (subscriptions) {
|
|
52
|
+
const index = subscriptions.indexOf(subscription);
|
|
53
|
+
if (index !== -1) {
|
|
54
|
+
subscriptions.splice(index, 1);
|
|
55
|
+
}
|
|
56
|
+
this._deleteSubscriptions(subscription.name, subscriptions);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
41
59
|
if (subscription.listener) {
|
|
42
|
-
|
|
60
|
+
const { listener, name } = subscription;
|
|
61
|
+
const count = () => this.listeners(name).filter((x) => x === listener).length;
|
|
62
|
+
const before = count();
|
|
63
|
+
super.removeListener.call(this, name, listener);
|
|
64
|
+
for (let attached = count(); attached < before - 1; attached++) {
|
|
65
|
+
super.on.call(this, name, listener);
|
|
66
|
+
}
|
|
43
67
|
}
|
|
68
|
+
}
|
|
69
|
+
removeListener(name, listener, ...args) {
|
|
70
|
+
super.removeListener.call(this, name, listener, ...args);
|
|
44
71
|
if (this._subscriptions) {
|
|
45
|
-
const
|
|
46
|
-
if (
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
72
|
+
const subscriptions = this._subscriptions.get(name);
|
|
73
|
+
if (subscriptions && subscriptions.some((subscription) => subscription.listener === listener)) {
|
|
74
|
+
let attached = this.listeners(name).filter((x) => x === listener).length;
|
|
75
|
+
for (let i = subscriptions.length - 1; 0 <= i; i--) {
|
|
76
|
+
const subscription = subscriptions[i];
|
|
77
|
+
if (subscription.listener === listener) {
|
|
78
|
+
if (attached) {
|
|
79
|
+
attached--;
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
subscriptions.splice(i, 1);
|
|
83
|
+
subscription.listener = null;
|
|
84
|
+
subscription.spewer = null;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
52
87
|
}
|
|
53
|
-
|
|
54
|
-
if (!this._subscriptions.length) {
|
|
55
|
-
this._subscriptions = undefined;
|
|
88
|
+
this._deleteSubscriptions(name, subscriptions);
|
|
56
89
|
}
|
|
57
90
|
}
|
|
91
|
+
return this;
|
|
92
|
+
}
|
|
93
|
+
off(name, listener, ...args) {
|
|
94
|
+
return this.removeListener(name, listener, ...args);
|
|
58
95
|
}
|
|
59
96
|
removeAllListeners(name) {
|
|
60
97
|
if (this._subscriptions) {
|
|
61
98
|
if (name !== undefined) {
|
|
62
|
-
|
|
63
|
-
|
|
99
|
+
const subscriptions = this._subscriptions.get(name);
|
|
100
|
+
if (subscriptions) {
|
|
101
|
+
for (let subscription of subscriptions.slice()) {
|
|
64
102
|
subscription.remove();
|
|
65
103
|
}
|
|
66
104
|
}
|
|
67
105
|
}
|
|
68
106
|
else {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
if (subscription) {
|
|
107
|
+
for (let subscriptions of this._subscriptions.values()) {
|
|
108
|
+
for (let subscription of subscriptions) {
|
|
72
109
|
subscription.listener = null;
|
|
73
110
|
subscription.spewer = null;
|
|
74
111
|
}
|
|
75
112
|
}
|
|
113
|
+
this._subscriptions = undefined;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if (name === undefined) {
|
|
117
|
+
return super.removeAllListeners();
|
|
118
|
+
}
|
|
119
|
+
if (!name) {
|
|
120
|
+
for (let listener of this.listeners(name)) {
|
|
121
|
+
this.removeListener(name, listener);
|
|
76
122
|
}
|
|
123
|
+
return this;
|
|
77
124
|
}
|
|
78
125
|
return super.removeAllListeners(name);
|
|
79
126
|
}
|
|
80
127
|
removeAllSubscriptions() {
|
|
81
128
|
if (this._subscriptions) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
129
|
+
const subscriptions = Array.from(this._subscriptions.values()).flat();
|
|
130
|
+
this._subscriptions = undefined;
|
|
131
|
+
for (let subscription of subscriptions) {
|
|
132
|
+
subscription.remove();
|
|
87
133
|
}
|
|
88
134
|
}
|
|
89
135
|
return this;
|
package/lib/snowflake.js
CHANGED
|
@@ -25,19 +25,22 @@ const max = Object.freeze({
|
|
|
25
25
|
const cache = {
|
|
26
26
|
sequence: 0n,
|
|
27
27
|
};
|
|
28
|
+
const DEFAULT_EPOCH = BigInt(constants_1.DISCORD_SNOWFLAKE_EPOCH);
|
|
29
|
+
function getEpoch(epoch) {
|
|
30
|
+
return (epoch === undefined || epoch === null) ? DEFAULT_EPOCH : BigInt(epoch);
|
|
31
|
+
}
|
|
28
32
|
function generate(options = {}) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const
|
|
37
|
-
const
|
|
38
|
-
const workerId = BigInt(options.workerId) & max.workerId;
|
|
33
|
+
const epoch = getEpoch(options.epoch);
|
|
34
|
+
const processId = BigInt(options.processId ?? 0) & max.processId;
|
|
35
|
+
const now = options.timestamp ?? Date.now();
|
|
36
|
+
const elapsed = BigInt(now) - epoch;
|
|
37
|
+
if (elapsed < 0n) {
|
|
38
|
+
throw new RangeError(`Timestamp ${now} is before the epoch ${epoch}`);
|
|
39
|
+
}
|
|
40
|
+
const timestamp = elapsed % max.timestamp;
|
|
41
|
+
const workerId = BigInt(options.workerId ?? 0) & max.workerId;
|
|
39
42
|
let sequence;
|
|
40
|
-
if (options.sequence === undefined) {
|
|
43
|
+
if (options.sequence === undefined || options.sequence === null) {
|
|
41
44
|
sequence = cache.sequence = ++cache.sequence & max.sequence;
|
|
42
45
|
}
|
|
43
46
|
else {
|
|
@@ -47,7 +50,7 @@ function generate(options = {}) {
|
|
|
47
50
|
id: '',
|
|
48
51
|
processId: Number(processId),
|
|
49
52
|
sequence: Number(sequence),
|
|
50
|
-
timestamp: Number(timestamp),
|
|
53
|
+
timestamp: Number(timestamp + epoch),
|
|
51
54
|
workerId: Number(workerId),
|
|
52
55
|
};
|
|
53
56
|
snowflake.id = String((timestamp << shift.timestamp) |
|
|
@@ -57,10 +60,7 @@ function generate(options = {}) {
|
|
|
57
60
|
return snowflake;
|
|
58
61
|
}
|
|
59
62
|
function deconstruct(id, options = {}) {
|
|
60
|
-
|
|
61
|
-
epoch: constants_1.DISCORD_SNOWFLAKE_EPOCH,
|
|
62
|
-
}, options);
|
|
63
|
-
const epoch = BigInt(options.epoch);
|
|
63
|
+
const epoch = getEpoch(options.epoch);
|
|
64
64
|
const snowflake = BigInt(id);
|
|
65
65
|
return {
|
|
66
66
|
id,
|
|
@@ -71,11 +71,8 @@ function deconstruct(id, options = {}) {
|
|
|
71
71
|
};
|
|
72
72
|
}
|
|
73
73
|
function timestamp(id, options = {}) {
|
|
74
|
-
options = Object.assign({
|
|
75
|
-
epoch: constants_1.DISCORD_SNOWFLAKE_EPOCH,
|
|
76
|
-
}, options);
|
|
77
|
-
const epoch = BigInt(options.epoch);
|
|
78
74
|
if (id) {
|
|
75
|
+
const epoch = getEpoch(options.epoch);
|
|
79
76
|
return Number((BigInt(id) >> shift.timestamp) + epoch);
|
|
80
77
|
}
|
|
81
78
|
return 0;
|
package/lib/tools.js
CHANGED
|
@@ -10,7 +10,17 @@ function normalize(object) {
|
|
|
10
10
|
return Object.freeze(object);
|
|
11
11
|
}
|
|
12
12
|
function guildIdToShardId(guildId, shardCount = 0) {
|
|
13
|
-
|
|
13
|
+
if (shardCount < 1) {
|
|
14
|
+
return 0;
|
|
15
|
+
}
|
|
16
|
+
let id;
|
|
17
|
+
try {
|
|
18
|
+
id = BigInt(guildId);
|
|
19
|
+
}
|
|
20
|
+
catch (e) {
|
|
21
|
+
return NaN;
|
|
22
|
+
}
|
|
23
|
+
return Number((id >> 22n) % BigInt(shardCount));
|
|
14
24
|
}
|
|
15
25
|
const safeCharacter = '@';
|
|
16
26
|
function URIEncodeWrap(unsafe) {
|
|
@@ -22,18 +32,13 @@ function URIEncodeWrap(unsafe) {
|
|
|
22
32
|
continue;
|
|
23
33
|
}
|
|
24
34
|
safe[key] = ((...args) => {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if (!value.includes(safeCharacter)) {
|
|
31
|
-
return encodeURIComponent(value);
|
|
35
|
+
for (let i = 0; i < args.length; i++) {
|
|
36
|
+
const arg = args[i];
|
|
37
|
+
if (arg) {
|
|
38
|
+
const value = String(arg);
|
|
39
|
+
args[i] = (value.includes(safeCharacter)) ? value.split(safeCharacter).map(encodeURIComponent).join(safeCharacter) : encodeURIComponent(value);
|
|
32
40
|
}
|
|
33
|
-
|
|
34
|
-
return (char === safeCharacter) ? char : encodeURIComponent(char);
|
|
35
|
-
}).join('');
|
|
36
|
-
});
|
|
41
|
+
}
|
|
37
42
|
return path(...args);
|
|
38
43
|
});
|
|
39
44
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "detritus-utils",
|
|
3
|
-
"version": "0.5.0-beta.
|
|
3
|
+
"version": "0.5.0-beta.4",
|
|
4
4
|
"description": "Detritus Utils",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -38,6 +38,7 @@
|
|
|
38
38
|
},
|
|
39
39
|
"homepage": "https://github.com/detritusjs/utils#readme",
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@types/node": "^26.1.2"
|
|
41
|
+
"@types/node": "^26.1.2",
|
|
42
|
+
"typescript": "^7.0.2"
|
|
42
43
|
}
|
|
43
44
|
}
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import * as Timers from './timers';
|
|
2
|
-
export declare class BaseCollectionMixin<K, V> {
|
|
3
|
-
get length(): number;
|
|
4
|
-
get size(): number;
|
|
5
|
-
clear(): void;
|
|
6
|
-
entries(): IterableIterator<[K, V]>;
|
|
7
|
-
every(func: (v: V, k: K) => boolean): boolean;
|
|
8
|
-
filter(func: (v: V, k: K) => boolean): Array<V>;
|
|
9
|
-
find(func: (v: V, k: K) => boolean): V | undefined;
|
|
10
|
-
first(): V | undefined;
|
|
11
|
-
forEach(func: (v: V, k: K, map: Map<K, V>) => void, thisArg?: any): void;
|
|
12
|
-
join(separator?: string): string;
|
|
13
|
-
map(func: (v: V, k: K) => any): Array<any>;
|
|
14
|
-
reduce(func: (intial: any, v: V) => any, initialValue?: any): any;
|
|
15
|
-
some(func: (v: V, k: K) => boolean): boolean;
|
|
16
|
-
sort(func?: (x: V, y: V) => number): Array<V>;
|
|
17
|
-
toArray(): Array<V>;
|
|
18
|
-
toJSON(): Array<V>;
|
|
19
|
-
toString(): string;
|
|
20
|
-
keys(): IterableIterator<K>;
|
|
21
|
-
values(): IterableIterator<V>;
|
|
22
|
-
[Symbol.iterator](): IterableIterator<[K, V]>;
|
|
23
|
-
get [Symbol.toStringTag](): string;
|
|
24
|
-
}
|
|
25
|
-
export interface BaseCollectionOptions {
|
|
26
|
-
expire?: number;
|
|
27
|
-
intervalTime?: number;
|
|
28
|
-
limit?: number;
|
|
29
|
-
}
|
|
30
|
-
export declare class BaseCollection<K, V> extends BaseCollectionMixin<K, V> {
|
|
31
|
-
readonly cache: Map<K, V>;
|
|
32
|
-
_lastUsed?: Map<K, number>;
|
|
33
|
-
expire?: number;
|
|
34
|
-
interval?: Timers.Interval;
|
|
35
|
-
intervalTime: number;
|
|
36
|
-
limit: number;
|
|
37
|
-
constructor({ expire, intervalTime, limit }?: BaseCollectionOptions);
|
|
38
|
-
get lastUsed(): Map<K, number>;
|
|
39
|
-
get shouldStartInterval(): boolean;
|
|
40
|
-
setExpire(value: number): this;
|
|
41
|
-
setIntervalTimeout(value: number): this;
|
|
42
|
-
startInterval(): void;
|
|
43
|
-
stopInterval(): void;
|
|
44
|
-
get size(): number;
|
|
45
|
-
clear(): void;
|
|
46
|
-
clone(): BaseCollection<K, V>;
|
|
47
|
-
delete(key: K): boolean;
|
|
48
|
-
forEach(func: (v: V, k: K, map: Map<K, V>) => void, thisArg?: any): void;
|
|
49
|
-
get(key: K): V | undefined;
|
|
50
|
-
has(key: K): boolean;
|
|
51
|
-
keys(): IterableIterator<K>;
|
|
52
|
-
set(key: K, value: V): this;
|
|
53
|
-
values(): IterableIterator<V>;
|
|
54
|
-
[Symbol.iterator](): IterableIterator<[K, V]>;
|
|
55
|
-
get [Symbol.toStringTag](): string;
|
|
56
|
-
}
|