detritus-utils 0.5.0-beta.6 → 0.5.0-beta.7
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/lib/basecollection.d.ts +14 -5
- package/lib/basecollection.js +52 -24
- package/lib/baseset.js +1 -1
- package/lib/constants.js +1 -2
- package/lib/eventspewer.d.ts +4 -1
- package/lib/eventspewer.js +9 -7
- package/lib/snowflake.d.ts +5 -0
- package/lib/snowflake.js +37 -20
- package/lib/timers.d.ts +13 -3
- package/lib/timers.js +35 -13
- package/lib/tools.d.ts +1 -2
- package/lib/tools.js +5 -5
- package/package.json +2 -2
package/lib/basecollection.d.ts
CHANGED
|
@@ -26,6 +26,10 @@ export interface BaseCollectionOptions {
|
|
|
26
26
|
expire?: number;
|
|
27
27
|
intervalTime?: number;
|
|
28
28
|
limit?: number;
|
|
29
|
+
/**
|
|
30
|
+
* Don't let the expiry interval keep the process alive
|
|
31
|
+
*/
|
|
32
|
+
unref?: boolean;
|
|
29
33
|
}
|
|
30
34
|
export declare class BaseCollection<K, V> extends BaseCollectionMixin<K, V> {
|
|
31
35
|
readonly cache: Map<K, V>;
|
|
@@ -36,18 +40,23 @@ export declare class BaseCollection<K, V> extends BaseCollectionMixin<K, V> {
|
|
|
36
40
|
interval?: Timers.Interval;
|
|
37
41
|
readonly intervalTime: number;
|
|
38
42
|
limit: number;
|
|
39
|
-
constructor({ expire, intervalTime, limit }?: BaseCollectionOptions);
|
|
43
|
+
constructor({ expire, intervalTime, limit, unref }?: BaseCollectionOptions);
|
|
40
44
|
get lastUsed(): Map<K, number>;
|
|
41
|
-
/**
|
|
42
|
-
* If the item outlived `expire` but the interval hasn't swept it yet
|
|
43
|
-
*/
|
|
44
45
|
_hasExpired(key: K): boolean;
|
|
45
46
|
touch(key: K): void;
|
|
46
47
|
get shouldStartInterval(): boolean;
|
|
48
|
+
/**
|
|
49
|
+
* If the expiry interval is running, only while items expire and the collection isn't empty
|
|
50
|
+
*/
|
|
51
|
+
get isSweeping(): boolean;
|
|
47
52
|
setExpire(value: number): this;
|
|
48
53
|
setLimit(value: number): this;
|
|
49
54
|
setIntervalTimeout(value: number): this;
|
|
50
55
|
startInterval(): void;
|
|
56
|
+
/**
|
|
57
|
+
* Deletes every expired item now, returns how many were deleted
|
|
58
|
+
*/
|
|
59
|
+
sweep(): number;
|
|
51
60
|
stopInterval(): void;
|
|
52
61
|
get size(): number;
|
|
53
62
|
clear(): void;
|
|
@@ -66,7 +75,7 @@ export declare class BaseCollection<K, V> extends BaseCollectionMixin<K, V> {
|
|
|
66
75
|
evict(room?: number): void;
|
|
67
76
|
/**
|
|
68
77
|
* The recency order (least recently used first), synced with the cache
|
|
69
|
-
* keys added
|
|
78
|
+
* untracked keys (added while `limit` was `Infinity`) count as the least recently used
|
|
70
79
|
*/
|
|
71
80
|
getOrder(): Map<K, true>;
|
|
72
81
|
values(): IterableIterator<V>;
|
package/lib/basecollection.js
CHANGED
|
@@ -92,7 +92,7 @@ class BaseCollectionMixin {
|
|
|
92
92
|
reduce(func, initialValue) {
|
|
93
93
|
const values = this.values();
|
|
94
94
|
let reduced = initialValue;
|
|
95
|
-
// like `Array.prototype.reduce`,
|
|
95
|
+
// like `Array.prototype.reduce`, no initial value starts from the first item
|
|
96
96
|
if (arguments.length < 2) {
|
|
97
97
|
const first = values.next();
|
|
98
98
|
if (first.done) {
|
|
@@ -136,7 +136,7 @@ class BaseCollectionMixin {
|
|
|
136
136
|
}
|
|
137
137
|
}
|
|
138
138
|
exports.BaseCollectionMixin = BaseCollectionMixin;
|
|
139
|
-
// a `NaN` limit would
|
|
139
|
+
// a `NaN` limit would evict everything on each insert
|
|
140
140
|
function normalizeLimit(value) {
|
|
141
141
|
return (value === undefined || value === null || Number.isNaN(value)) ? Infinity : value;
|
|
142
142
|
}
|
|
@@ -148,8 +148,12 @@ class BaseCollection extends BaseCollectionMixin {
|
|
|
148
148
|
interval = undefined;
|
|
149
149
|
intervalTime = 5000;
|
|
150
150
|
limit = Infinity;
|
|
151
|
-
constructor({ expire, intervalTime, limit } = {}) {
|
|
151
|
+
constructor({ expire, intervalTime, limit, unref } = {}) {
|
|
152
152
|
super();
|
|
153
|
+
if (unref) {
|
|
154
|
+
this.interval = new Timers.Interval();
|
|
155
|
+
this.interval.unref();
|
|
156
|
+
}
|
|
153
157
|
if (expire !== undefined) {
|
|
154
158
|
this.expire = expire;
|
|
155
159
|
}
|
|
@@ -183,9 +187,6 @@ class BaseCollection extends BaseCollectionMixin {
|
|
|
183
187
|
}
|
|
184
188
|
return new Map();
|
|
185
189
|
}
|
|
186
|
-
/**
|
|
187
|
-
* If the item outlived `expire` but the interval hasn't swept it yet
|
|
188
|
-
*/
|
|
189
190
|
_hasExpired(key) {
|
|
190
191
|
if (this.expire && this._lastUsed) {
|
|
191
192
|
const lastUsed = this._lastUsed.get(key);
|
|
@@ -194,18 +195,28 @@ class BaseCollection extends BaseCollectionMixin {
|
|
|
194
195
|
return false;
|
|
195
196
|
}
|
|
196
197
|
touch(key) {
|
|
198
|
+
if (!this.cache.has(key)) {
|
|
199
|
+
// a timestamp for a missing key would later be swept for nothing
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
197
202
|
const lastUsed = this.lastUsed;
|
|
198
203
|
lastUsed.delete(key);
|
|
199
204
|
lastUsed.set(key, Date.now());
|
|
200
205
|
}
|
|
201
206
|
get shouldStartInterval() {
|
|
202
|
-
return !!this.intervalTime && (!this.interval || !this.interval.hasStarted);
|
|
207
|
+
return !!this.expire && !!this.intervalTime && (!this.interval || !this.interval.hasStarted);
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* If the expiry interval is running, only while items expire and the collection isn't empty
|
|
211
|
+
*/
|
|
212
|
+
get isSweeping() {
|
|
213
|
+
return !!this.interval && this.interval.hasStarted;
|
|
203
214
|
}
|
|
204
215
|
setExpire(value) {
|
|
205
216
|
const wasExpiring = !!this.expire;
|
|
206
217
|
Object.defineProperty(this, 'expire', { value });
|
|
207
218
|
if (value) {
|
|
208
|
-
//
|
|
219
|
+
// from the asked for interval, so raising `expire` again doesn't keep a smaller cap
|
|
209
220
|
Object.defineProperty(this, 'intervalTime', { value: Math.min(value, this._intervalTime) });
|
|
210
221
|
const now = Date.now();
|
|
211
222
|
const lastUsed = this.lastUsed;
|
|
@@ -234,7 +245,8 @@ class BaseCollection extends BaseCollectionMixin {
|
|
|
234
245
|
}
|
|
235
246
|
setIntervalTimeout(value) {
|
|
236
247
|
this._intervalTime = value;
|
|
237
|
-
|
|
248
|
+
// capped to `expire`, so expired items linger at most another `expire`
|
|
249
|
+
Object.defineProperty(this, 'intervalTime', { value: (this.expire) ? Math.min(this.expire, value) : value });
|
|
238
250
|
if (value) {
|
|
239
251
|
if (this.size) {
|
|
240
252
|
this.startInterval();
|
|
@@ -251,15 +263,8 @@ class BaseCollection extends BaseCollectionMixin {
|
|
|
251
263
|
this.interval = new Timers.Interval();
|
|
252
264
|
}
|
|
253
265
|
this.interval.start(this.intervalTime, () => {
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
const now = Date.now();
|
|
257
|
-
for (let [key, lastUsed] of this.lastUsed) {
|
|
258
|
-
if (now - lastUsed <= expire) {
|
|
259
|
-
break;
|
|
260
|
-
}
|
|
261
|
-
this.delete(key);
|
|
262
|
-
}
|
|
266
|
+
if (this.expire) {
|
|
267
|
+
this.sweep();
|
|
263
268
|
}
|
|
264
269
|
else {
|
|
265
270
|
this.stopInterval();
|
|
@@ -270,8 +275,27 @@ class BaseCollection extends BaseCollectionMixin {
|
|
|
270
275
|
this.stopInterval();
|
|
271
276
|
}
|
|
272
277
|
}
|
|
278
|
+
/**
|
|
279
|
+
* Deletes every expired item now, returns how many were deleted
|
|
280
|
+
*/
|
|
281
|
+
sweep() {
|
|
282
|
+
const expire = this.expire;
|
|
283
|
+
let deleted = 0;
|
|
284
|
+
if (expire && this._lastUsed) {
|
|
285
|
+
const now = Date.now();
|
|
286
|
+
// oldest first, so everything after the first fresh item is fresh too
|
|
287
|
+
for (let [key, lastUsed] of this._lastUsed) {
|
|
288
|
+
if (now - lastUsed <= expire) {
|
|
289
|
+
break;
|
|
290
|
+
}
|
|
291
|
+
this.delete(key);
|
|
292
|
+
deleted++;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
return deleted;
|
|
296
|
+
}
|
|
273
297
|
stopInterval() {
|
|
274
|
-
// keep the
|
|
298
|
+
// keep the object around so an `unref()` still applies on the next start
|
|
275
299
|
if (this.interval) {
|
|
276
300
|
this.interval.stop();
|
|
277
301
|
}
|
|
@@ -290,7 +314,13 @@ class BaseCollection extends BaseCollectionMixin {
|
|
|
290
314
|
}
|
|
291
315
|
}
|
|
292
316
|
clone() {
|
|
293
|
-
|
|
317
|
+
// the asked for interval, not the one capped to `expire`
|
|
318
|
+
const collection = new BaseCollection({
|
|
319
|
+
expire: this.expire,
|
|
320
|
+
intervalTime: this._intervalTime,
|
|
321
|
+
limit: this.limit,
|
|
322
|
+
unref: !!this.interval && !this.interval.hasRef(),
|
|
323
|
+
});
|
|
294
324
|
// insert least recently used first so the clone keeps the same eviction order
|
|
295
325
|
const keys = (this.limit !== Infinity) ? this.getOrder().keys() : this.cache.keys();
|
|
296
326
|
for (let key of keys) {
|
|
@@ -322,12 +352,11 @@ class BaseCollection extends BaseCollectionMixin {
|
|
|
322
352
|
const value = this.cache.get(key);
|
|
323
353
|
if ((this.limit !== Infinity || this.expire) && (value !== undefined || this.cache.has(key))) {
|
|
324
354
|
if (this._hasExpired(key)) {
|
|
325
|
-
//
|
|
355
|
+
// expired but not swept yet
|
|
326
356
|
this.delete(key);
|
|
327
357
|
return undefined;
|
|
328
358
|
}
|
|
329
359
|
if (this.limit !== Infinity) {
|
|
330
|
-
// mark it as the most recently used
|
|
331
360
|
const order = this.getOrder();
|
|
332
361
|
order.delete(key);
|
|
333
362
|
order.set(key, true);
|
|
@@ -376,7 +405,6 @@ class BaseCollection extends BaseCollectionMixin {
|
|
|
376
405
|
order.delete(key);
|
|
377
406
|
}
|
|
378
407
|
else {
|
|
379
|
-
// make room for the new key, evicting the least recently used ones
|
|
380
408
|
this.evict(1);
|
|
381
409
|
}
|
|
382
410
|
order.set(key, true);
|
|
@@ -411,7 +439,7 @@ class BaseCollection extends BaseCollectionMixin {
|
|
|
411
439
|
}
|
|
412
440
|
/**
|
|
413
441
|
* The recency order (least recently used first), synced with the cache
|
|
414
|
-
* keys added
|
|
442
|
+
* untracked keys (added while `limit` was `Infinity`) count as the least recently used
|
|
415
443
|
*/
|
|
416
444
|
getOrder() {
|
|
417
445
|
let order = this._order;
|
package/lib/baseset.js
CHANGED
|
@@ -48,7 +48,7 @@ class BaseSet extends Set {
|
|
|
48
48
|
reduce(cb, initialValue) {
|
|
49
49
|
const values = this.values();
|
|
50
50
|
let reduced = initialValue;
|
|
51
|
-
// like `Array.prototype.reduce`,
|
|
51
|
+
// like `Array.prototype.reduce`, no initial value starts from the first item
|
|
52
52
|
if (arguments.length < 2) {
|
|
53
53
|
const first = values.next();
|
|
54
54
|
if (first.done) {
|
package/lib/constants.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DISCORD_TOKEN_EPOCH = exports.DISCORD_SNOWFLAKE_EPOCH = exports.Package = void 0;
|
|
4
|
-
//
|
|
5
|
-
// from pulling it into the build (it's outside rootDir)
|
|
4
|
+
// `require` so tsc doesn't pull it (outside rootDir) into the build
|
|
6
5
|
const { version } = require('../package.json');
|
|
7
6
|
exports.Package = Object.freeze({
|
|
8
7
|
URL: 'https://github.com/detritusjs/utils',
|
package/lib/eventspewer.d.ts
CHANGED
|
@@ -26,9 +26,12 @@ export declare class EventSubscription {
|
|
|
26
26
|
spewer: EventSpewer | null;
|
|
27
27
|
/**
|
|
28
28
|
* What's actually attached to the spewer, a function only this subscription owns
|
|
29
|
-
* Removing it can't remove another registration of `listener` (or bring one back)
|
|
30
29
|
*/
|
|
31
30
|
wrapped: ((...args: any[]) => void) | null;
|
|
32
31
|
constructor(spewer: EventSpewer, name: string | symbol, listener: (...args: any[]) => void);
|
|
32
|
+
/**
|
|
33
|
+
* If it's still attached, `false` once removed (by `remove()`, `removeListener()` or `removeAllListeners()`)
|
|
34
|
+
*/
|
|
35
|
+
get active(): boolean;
|
|
33
36
|
remove(): void;
|
|
34
37
|
}
|
package/lib/eventspewer.js
CHANGED
|
@@ -52,8 +52,7 @@ class EventSpewer extends events_1.EventEmitter {
|
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
if (subscription.wrapped) {
|
|
55
|
-
//
|
|
56
|
-
// same function (like a `once()`, which `removeListener(name, listener)` would remove first)
|
|
55
|
+
// only this subscription attached the wrapper, so this can't remove another registration of the same function
|
|
57
56
|
super.removeListener.call(this, subscription.name, subscription.wrapped);
|
|
58
57
|
}
|
|
59
58
|
}
|
|
@@ -61,9 +60,8 @@ class EventSpewer extends events_1.EventEmitter {
|
|
|
61
60
|
super.removeListener.call(this, name, listener, ...args);
|
|
62
61
|
if (this._subscriptions) {
|
|
63
62
|
const subscriptions = this._subscriptions.get(name);
|
|
64
|
-
// `listener` is
|
|
63
|
+
// `listener` is a wrapper when node removes it, like in `removeAllListeners()`
|
|
65
64
|
if (subscriptions && subscriptions.some((subscription) => subscription.listener === listener || subscription.wrapped === listener)) {
|
|
66
|
-
// drop the subscriptions whose wrapper isn't attached anymore
|
|
67
65
|
const attached = new Set(this.rawListeners(name));
|
|
68
66
|
for (let i = subscriptions.length - 1; 0 <= i; i--) {
|
|
69
67
|
const subscription = subscriptions[i];
|
|
@@ -132,7 +130,6 @@ class EventSubscription {
|
|
|
132
130
|
spewer;
|
|
133
131
|
/**
|
|
134
132
|
* What's actually attached to the spewer, a function only this subscription owns
|
|
135
|
-
* Removing it can't remove another registration of `listener` (or bring one back)
|
|
136
133
|
*/
|
|
137
134
|
wrapped;
|
|
138
135
|
constructor(spewer, name, listener) {
|
|
@@ -142,11 +139,16 @@ class EventSubscription {
|
|
|
142
139
|
const wrapped = function (...args) {
|
|
143
140
|
return listener.apply(this, args);
|
|
144
141
|
};
|
|
145
|
-
//
|
|
146
|
-
// `removeListener(name, listener)` still see the original function
|
|
142
|
+
// like node's `once()`, so `listeners()` and `removeListener(name, listener)` still see the original function
|
|
147
143
|
wrapped.listener = listener;
|
|
148
144
|
this.wrapped = wrapped;
|
|
149
145
|
}
|
|
146
|
+
/**
|
|
147
|
+
* If it's still attached, `false` once removed (by `remove()`, `removeListener()` or `removeAllListeners()`)
|
|
148
|
+
*/
|
|
149
|
+
get active() {
|
|
150
|
+
return this.spewer !== null;
|
|
151
|
+
}
|
|
150
152
|
remove() {
|
|
151
153
|
if (this.spewer) {
|
|
152
154
|
this.spewer.removeSubscription(this);
|
package/lib/snowflake.d.ts
CHANGED
|
@@ -12,6 +12,11 @@ export interface SnowflakeGenerateOptions {
|
|
|
12
12
|
timestamp?: number;
|
|
13
13
|
workerId?: number;
|
|
14
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Ids made without `timestamp` and `sequence` are unique within this process (for the same `workerId` and `processId`), whatever other calls passed
|
|
17
|
+
* An explicit `timestamp` before the clock's last used millisecond (or past the real clock) can collide with ids made in it
|
|
18
|
+
* Out of range `workerId`s and `processId`s are masked to 5 bits
|
|
19
|
+
*/
|
|
15
20
|
export declare function generate(options?: SnowflakeGenerateOptions): Snowflake;
|
|
16
21
|
export interface SnowflakeDeconstructOptions {
|
|
17
22
|
epoch?: number;
|
package/lib/snowflake.js
CHANGED
|
@@ -22,11 +22,30 @@ const max = Object.freeze({
|
|
|
22
22
|
sequence: -1n ^ (-1n << bits.sequence),
|
|
23
23
|
workerId: -1n ^ (-1n << bits.workerId),
|
|
24
24
|
});
|
|
25
|
-
//
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
25
|
+
// ids made from `Date.now()`, this one only ever moves forward
|
|
26
|
+
const clock = { start: -1n, sequence: 0n, timestamp: -1n };
|
|
27
|
+
// ids made from a passed in `timestamp`, kept apart so they can't rewind the clock's
|
|
28
|
+
const manual = { start: -1n, sequence: 0n, timestamp: -1n };
|
|
29
|
+
function nextSequence(state, now, isClock) {
|
|
30
|
+
if (now < state.timestamp && (isClock || state.start <= now)) {
|
|
31
|
+
// the clock went backwards, or `now` is inside a run that borrowed ahead, keep counting from the last one
|
|
32
|
+
now = state.timestamp;
|
|
33
|
+
}
|
|
34
|
+
let sequence = 0n;
|
|
35
|
+
if (now === state.timestamp) {
|
|
36
|
+
sequence = (state.sequence + 1n) & max.sequence;
|
|
37
|
+
if (!sequence) {
|
|
38
|
+
// every sequence in this millisecond is used, borrow the next one
|
|
39
|
+
now++;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
state.start = now;
|
|
44
|
+
}
|
|
45
|
+
state.sequence = sequence;
|
|
46
|
+
state.timestamp = now;
|
|
47
|
+
return [now, sequence];
|
|
48
|
+
}
|
|
30
49
|
const DEFAULT_EPOCH = BigInt(constants_1.DISCORD_SNOWFLAKE_EPOCH);
|
|
31
50
|
const MAX_ID = 1n << 64n;
|
|
32
51
|
function getEpoch(epoch) {
|
|
@@ -34,12 +53,16 @@ function getEpoch(epoch) {
|
|
|
34
53
|
}
|
|
35
54
|
function parseId(id) {
|
|
36
55
|
const snowflake = BigInt(id);
|
|
37
|
-
// snowflakes are unsigned 64 bit, anything outside of that would decode into garbage
|
|
38
56
|
if (snowflake < 0n || MAX_ID <= snowflake) {
|
|
39
57
|
throw new RangeError(`Snowflake ${id} is not an unsigned 64 bit integer`);
|
|
40
58
|
}
|
|
41
59
|
return snowflake;
|
|
42
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* Ids made without `timestamp` and `sequence` are unique within this process (for the same `workerId` and `processId`), whatever other calls passed
|
|
63
|
+
* An explicit `timestamp` before the clock's last used millisecond (or past the real clock) can collide with ids made in it
|
|
64
|
+
* Out of range `workerId`s and `processId`s are masked to 5 bits
|
|
65
|
+
*/
|
|
43
66
|
function generate(options = {}) {
|
|
44
67
|
const epoch = getEpoch(options.epoch);
|
|
45
68
|
const processId = BigInt(options.processId ?? 0) & max.processId;
|
|
@@ -50,23 +73,17 @@ function generate(options = {}) {
|
|
|
50
73
|
const workerId = BigInt(options.workerId ?? 0) & max.workerId;
|
|
51
74
|
let sequence;
|
|
52
75
|
if (options.sequence === undefined || options.sequence === null) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
// the clock went backwards (or the last millisecond ran out of sequences), keep counting from the last one
|
|
56
|
-
now = cache.timestamp;
|
|
76
|
+
if (options.timestamp === undefined || options.timestamp === null) {
|
|
77
|
+
[now, sequence] = nextSequence(clock, now, true);
|
|
57
78
|
}
|
|
58
|
-
if (now
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
now++;
|
|
63
|
-
}
|
|
79
|
+
else if (clock.start <= now && (now <= clock.timestamp || now <= BigInt(Date.now()))) {
|
|
80
|
+
// a millisecond the clock already made ids in (or is about to), keep counting there so they can't collide
|
|
81
|
+
// past the real clock stays apart, or every later id would be dated in the future
|
|
82
|
+
[now, sequence] = nextSequence(clock, now, false);
|
|
64
83
|
}
|
|
65
84
|
else {
|
|
66
|
-
sequence =
|
|
85
|
+
[now, sequence] = nextSequence(manual, now, false);
|
|
67
86
|
}
|
|
68
|
-
cache.sequence = sequence;
|
|
69
|
-
cache.timestamp = now;
|
|
70
87
|
}
|
|
71
88
|
else {
|
|
72
89
|
sequence = BigInt(options.sequence) & max.sequence;
|
|
@@ -87,7 +104,7 @@ function generate(options = {}) {
|
|
|
87
104
|
}
|
|
88
105
|
function deconstruct(id, options = {}) {
|
|
89
106
|
if (!id) {
|
|
90
|
-
//
|
|
107
|
+
// like `timestamp()`, an empty id is 0 instead of the epoch
|
|
91
108
|
return { id, processId: 0, sequence: 0, timestamp: 0, workerId: 0 };
|
|
92
109
|
}
|
|
93
110
|
const epoch = getEpoch(options.epoch);
|
package/lib/timers.d.ts
CHANGED
|
@@ -6,7 +6,10 @@ export declare class Interval {
|
|
|
6
6
|
ref(): void;
|
|
7
7
|
unref(): void;
|
|
8
8
|
start(milliseconds: number, handler: Function): void;
|
|
9
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Returns if it was running
|
|
11
|
+
*/
|
|
12
|
+
stop(): boolean;
|
|
10
13
|
}
|
|
11
14
|
export declare class Timeout {
|
|
12
15
|
shouldRef: boolean;
|
|
@@ -15,7 +18,14 @@ export declare class Timeout {
|
|
|
15
18
|
hasRef(): boolean;
|
|
16
19
|
ref(): void;
|
|
17
20
|
unref(): void;
|
|
18
|
-
|
|
19
|
-
|
|
21
|
+
/**
|
|
22
|
+
* Returns if it was scheduled, `false` when it's already running and `override` is `false`
|
|
23
|
+
* `hasStarted` is `false` again by the time `handler` runs, so the handler can start it again
|
|
24
|
+
*/
|
|
25
|
+
start(milliseconds: number, handler: Function, override?: boolean): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Returns if it was running
|
|
28
|
+
*/
|
|
29
|
+
stop(): boolean;
|
|
20
30
|
}
|
|
21
31
|
export declare function sleep(milliseconds: number): Promise<void>;
|
package/lib/timers.js
CHANGED
|
@@ -24,8 +24,10 @@ class Interval {
|
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
start(milliseconds, handler) {
|
|
27
|
+
// schedule first, so a bad handler throws before the running interval is stopped
|
|
28
|
+
const reference = setInterval(handler, milliseconds);
|
|
27
29
|
this.stop();
|
|
28
|
-
this.reference =
|
|
30
|
+
this.reference = reference;
|
|
29
31
|
if (this.shouldRef) {
|
|
30
32
|
this.ref();
|
|
31
33
|
}
|
|
@@ -33,11 +35,16 @@ class Interval {
|
|
|
33
35
|
this.unref();
|
|
34
36
|
}
|
|
35
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* Returns if it was running
|
|
40
|
+
*/
|
|
36
41
|
stop() {
|
|
37
42
|
if (this.hasStarted) {
|
|
38
43
|
clearInterval(this.reference);
|
|
39
44
|
this.reference = null;
|
|
45
|
+
return true;
|
|
40
46
|
}
|
|
47
|
+
return false;
|
|
41
48
|
}
|
|
42
49
|
}
|
|
43
50
|
exports.Interval = Interval;
|
|
@@ -62,26 +69,41 @@ class Timeout {
|
|
|
62
69
|
this.reference.unref();
|
|
63
70
|
}
|
|
64
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* Returns if it was scheduled, `false` when it's already running and `override` is `false`
|
|
74
|
+
* `hasStarted` is `false` again by the time `handler` runs, so the handler can start it again
|
|
75
|
+
*/
|
|
65
76
|
start(milliseconds, handler, override = true) {
|
|
66
|
-
if (
|
|
67
|
-
|
|
68
|
-
this.reference = setTimeout(() => {
|
|
69
|
-
this.reference = null;
|
|
70
|
-
handler();
|
|
71
|
-
}, milliseconds);
|
|
72
|
-
if (this.shouldRef) {
|
|
73
|
-
this.ref();
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
this.unref();
|
|
77
|
-
}
|
|
77
|
+
if (this.hasStarted && !override) {
|
|
78
|
+
return false;
|
|
78
79
|
}
|
|
80
|
+
if (typeof (handler) !== 'function') {
|
|
81
|
+
// like `setTimeout()` would, instead of throwing later from inside the timer
|
|
82
|
+
throw new TypeError(`The "handler" argument must be of type function. Received ${typeof (handler)}`);
|
|
83
|
+
}
|
|
84
|
+
this.stop();
|
|
85
|
+
this.reference = setTimeout(() => {
|
|
86
|
+
this.reference = null;
|
|
87
|
+
handler();
|
|
88
|
+
}, milliseconds);
|
|
89
|
+
if (this.shouldRef) {
|
|
90
|
+
this.ref();
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
this.unref();
|
|
94
|
+
}
|
|
95
|
+
return true;
|
|
79
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* Returns if it was running
|
|
99
|
+
*/
|
|
80
100
|
stop() {
|
|
81
101
|
if (this.hasStarted) {
|
|
82
102
|
clearTimeout(this.reference);
|
|
83
103
|
this.reference = null;
|
|
104
|
+
return true;
|
|
84
105
|
}
|
|
106
|
+
return false;
|
|
85
107
|
}
|
|
86
108
|
}
|
|
87
109
|
exports.Timeout = Timeout;
|
package/lib/tools.d.ts
CHANGED
|
@@ -13,8 +13,7 @@ export type URIEncodeWrapped = {
|
|
|
13
13
|
*/
|
|
14
14
|
export type URIEncodedArgument<T> = string | Extract<T, '' | 0 | 0n | false | null | undefined>;
|
|
15
15
|
/**
|
|
16
|
-
*
|
|
17
|
-
* get a string at runtime) is a type error instead of something like `id + 1n` silently concatenating
|
|
16
|
+
* Parameters must accept strings, so a `number` or `bigint` one is a type error instead of `id + 1n` silently concatenating
|
|
18
17
|
*/
|
|
19
18
|
export type URIEncodeWrapSafe<T> = {
|
|
20
19
|
[K in keyof T]: T[K] extends (...args: infer A) => any ? (...args: {
|
package/lib/tools.js
CHANGED
|
@@ -21,17 +21,17 @@ function guildIdToShardId(guildId, shardCount = 0) {
|
|
|
21
21
|
catch (e) {
|
|
22
22
|
return NaN;
|
|
23
23
|
}
|
|
24
|
-
//
|
|
24
|
+
// bigint `%` keeps the sign, a negative id would give a negative shard
|
|
25
25
|
if (id < 0n) {
|
|
26
26
|
return NaN;
|
|
27
27
|
}
|
|
28
28
|
return Number((id >> 22n) % BigInt(shardCount));
|
|
29
29
|
}
|
|
30
|
-
// `encodeURIComponent` leaves `.` alone, so a `.` or `..` argument would
|
|
31
|
-
// `%2E`
|
|
30
|
+
// `encodeURIComponent` leaves `.` alone, so a `.` or `..` argument would climb out of the url
|
|
31
|
+
// `%2E` is a dot to the url parser too, so the percent itself is encoded
|
|
32
32
|
const DotSegmentRegex = /^\.{1,2}$/;
|
|
33
33
|
const safeCharacter = '@';
|
|
34
|
-
// generic so the wrapped route builders keep their
|
|
34
|
+
// generic so the wrapped route builders keep their signatures
|
|
35
35
|
function URIEncodeWrap(unsafe) {
|
|
36
36
|
const safe = {};
|
|
37
37
|
for (let key in unsafe) {
|
|
@@ -44,7 +44,7 @@ function URIEncodeWrap(unsafe) {
|
|
|
44
44
|
for (let i = 0; i < args.length; i++) {
|
|
45
45
|
const arg = args[i];
|
|
46
46
|
if (arg) {
|
|
47
|
-
// a lone surrogate would make `encodeURIComponent` throw
|
|
47
|
+
// a lone surrogate would make `encodeURIComponent` throw
|
|
48
48
|
const value = String(arg).toWellFormed();
|
|
49
49
|
let encoded = (value.includes(safeCharacter)) ? value.split(safeCharacter).map(encodeURIComponent).join(safeCharacter) : encodeURIComponent(value);
|
|
50
50
|
if (DotSegmentRegex.test(encoded)) {
|
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.7",
|
|
4
4
|
"description": "Detritus Utils",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
},
|
|
31
31
|
"homepage": "https://github.com/detritusjs/utils#readme",
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@types/node": "^26.6.
|
|
33
|
+
"@types/node": "^26.6.6",
|
|
34
34
|
"typescript": "^7.0.2"
|
|
35
35
|
}
|
|
36
36
|
}
|