detritus-utils 0.5.0-beta.5 → 0.5.0-beta.6
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 +7 -2
- package/lib/basecollection.js +56 -12
- package/lib/baseset.d.ts +2 -2
- package/lib/baseset.js +20 -2
- package/lib/constants.d.ts +1 -1
- package/lib/constants.js +4 -1
- package/lib/eventspewer.d.ts +5 -0
- package/lib/eventspewer.js +28 -20
- package/lib/snowflake.js +37 -7
- package/lib/tools.d.ts +14 -1
- package/lib/tools.js +17 -3
- package/package.json +1 -1
package/lib/basecollection.d.ts
CHANGED
|
@@ -31,12 +31,17 @@ export declare class BaseCollection<K, V> extends BaseCollectionMixin<K, V> {
|
|
|
31
31
|
readonly cache: Map<K, V>;
|
|
32
32
|
_lastUsed?: Map<K, number>;
|
|
33
33
|
_order?: Map<K, true>;
|
|
34
|
-
|
|
34
|
+
_intervalTime: number;
|
|
35
|
+
readonly expire?: number;
|
|
35
36
|
interval?: Timers.Interval;
|
|
36
|
-
intervalTime: number;
|
|
37
|
+
readonly intervalTime: number;
|
|
37
38
|
limit: number;
|
|
38
39
|
constructor({ expire, intervalTime, limit }?: BaseCollectionOptions);
|
|
39
40
|
get lastUsed(): Map<K, number>;
|
|
41
|
+
/**
|
|
42
|
+
* If the item outlived `expire` but the interval hasn't swept it yet
|
|
43
|
+
*/
|
|
44
|
+
_hasExpired(key: K): boolean;
|
|
40
45
|
touch(key: K): void;
|
|
41
46
|
get shouldStartInterval(): boolean;
|
|
42
47
|
setExpire(value: number): this;
|
package/lib/basecollection.js
CHANGED
|
@@ -90,8 +90,17 @@ class BaseCollectionMixin {
|
|
|
90
90
|
return map;
|
|
91
91
|
}
|
|
92
92
|
reduce(func, initialValue) {
|
|
93
|
+
const values = this.values();
|
|
93
94
|
let reduced = initialValue;
|
|
94
|
-
|
|
95
|
+
// like `Array.prototype.reduce`, with no initial value the first item is the starting one
|
|
96
|
+
if (arguments.length < 2) {
|
|
97
|
+
const first = values.next();
|
|
98
|
+
if (first.done) {
|
|
99
|
+
throw new TypeError('Reduce of empty collection with no initial value');
|
|
100
|
+
}
|
|
101
|
+
reduced = first.value;
|
|
102
|
+
}
|
|
103
|
+
for (let value of values) {
|
|
95
104
|
reduced = func(reduced, value);
|
|
96
105
|
}
|
|
97
106
|
return reduced;
|
|
@@ -127,9 +136,14 @@ class BaseCollectionMixin {
|
|
|
127
136
|
}
|
|
128
137
|
}
|
|
129
138
|
exports.BaseCollectionMixin = BaseCollectionMixin;
|
|
139
|
+
// a `NaN` limit would never be satisfied and evict everything on each insert
|
|
140
|
+
function normalizeLimit(value) {
|
|
141
|
+
return (value === undefined || value === null || Number.isNaN(value)) ? Infinity : value;
|
|
142
|
+
}
|
|
130
143
|
class BaseCollection extends BaseCollectionMixin {
|
|
131
144
|
cache = new Map();
|
|
132
145
|
_lastUsed;
|
|
146
|
+
// read only, use `setExpire()` and `setIntervalTimeout()` to change them
|
|
133
147
|
expire;
|
|
134
148
|
interval = undefined;
|
|
135
149
|
intervalTime = 5000;
|
|
@@ -142,13 +156,15 @@ class BaseCollection extends BaseCollectionMixin {
|
|
|
142
156
|
if (intervalTime !== undefined) {
|
|
143
157
|
this.intervalTime = intervalTime;
|
|
144
158
|
}
|
|
159
|
+
this._intervalTime = this.intervalTime;
|
|
145
160
|
if (limit !== undefined) {
|
|
146
|
-
this.limit = limit;
|
|
161
|
+
this.limit = normalizeLimit(limit);
|
|
147
162
|
}
|
|
148
163
|
if (this.expire) {
|
|
149
164
|
this.intervalTime = Math.min(this.expire, this.intervalTime);
|
|
150
165
|
}
|
|
151
166
|
Object.defineProperties(this, {
|
|
167
|
+
_intervalTime: { enumerable: false, writable: true },
|
|
152
168
|
_lastUsed: { enumerable: false, writable: true },
|
|
153
169
|
_order: { enumerable: false, writable: true },
|
|
154
170
|
cache: { enumerable: false },
|
|
@@ -167,6 +183,16 @@ class BaseCollection extends BaseCollectionMixin {
|
|
|
167
183
|
}
|
|
168
184
|
return new Map();
|
|
169
185
|
}
|
|
186
|
+
/**
|
|
187
|
+
* If the item outlived `expire` but the interval hasn't swept it yet
|
|
188
|
+
*/
|
|
189
|
+
_hasExpired(key) {
|
|
190
|
+
if (this.expire && this._lastUsed) {
|
|
191
|
+
const lastUsed = this._lastUsed.get(key);
|
|
192
|
+
return lastUsed !== undefined && this.expire < Date.now() - lastUsed;
|
|
193
|
+
}
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
170
196
|
touch(key) {
|
|
171
197
|
const lastUsed = this.lastUsed;
|
|
172
198
|
lastUsed.delete(key);
|
|
@@ -179,7 +205,8 @@ class BaseCollection extends BaseCollectionMixin {
|
|
|
179
205
|
const wasExpiring = !!this.expire;
|
|
180
206
|
Object.defineProperty(this, 'expire', { value });
|
|
181
207
|
if (value) {
|
|
182
|
-
|
|
208
|
+
// based on the asked for interval, so raising `expire` again doesn't keep a tiny interval from a smaller one
|
|
209
|
+
Object.defineProperty(this, 'intervalTime', { value: Math.min(value, this._intervalTime) });
|
|
183
210
|
const now = Date.now();
|
|
184
211
|
const lastUsed = this.lastUsed;
|
|
185
212
|
for (let key of this.cache.keys()) {
|
|
@@ -192,6 +219,7 @@ class BaseCollection extends BaseCollectionMixin {
|
|
|
192
219
|
}
|
|
193
220
|
}
|
|
194
221
|
else {
|
|
222
|
+
Object.defineProperty(this, 'intervalTime', { value: this._intervalTime });
|
|
195
223
|
this.stopInterval();
|
|
196
224
|
if (this._lastUsed) {
|
|
197
225
|
this._lastUsed.clear();
|
|
@@ -200,11 +228,12 @@ class BaseCollection extends BaseCollectionMixin {
|
|
|
200
228
|
return this;
|
|
201
229
|
}
|
|
202
230
|
setLimit(value) {
|
|
203
|
-
this.limit = value;
|
|
231
|
+
this.limit = normalizeLimit(value);
|
|
204
232
|
this.evict(0);
|
|
205
233
|
return this;
|
|
206
234
|
}
|
|
207
235
|
setIntervalTimeout(value) {
|
|
236
|
+
this._intervalTime = value;
|
|
208
237
|
Object.defineProperty(this, 'intervalTime', { value });
|
|
209
238
|
if (value) {
|
|
210
239
|
if (this.size) {
|
|
@@ -242,10 +271,10 @@ class BaseCollection extends BaseCollectionMixin {
|
|
|
242
271
|
}
|
|
243
272
|
}
|
|
244
273
|
stopInterval() {
|
|
274
|
+
// keep the interval object around so an `unref()` on it still applies the next time it starts
|
|
245
275
|
if (this.interval) {
|
|
246
276
|
this.interval.stop();
|
|
247
277
|
}
|
|
248
|
-
this.interval = undefined;
|
|
249
278
|
}
|
|
250
279
|
get size() {
|
|
251
280
|
return this.cache.size;
|
|
@@ -267,6 +296,10 @@ class BaseCollection extends BaseCollectionMixin {
|
|
|
267
296
|
for (let key of keys) {
|
|
268
297
|
collection.set(key, this.cache.get(key));
|
|
269
298
|
}
|
|
299
|
+
if (collection.expire && this._lastUsed) {
|
|
300
|
+
// `set` stamped every item as just used, keep their real ages (the map is already oldest first)
|
|
301
|
+
collection._lastUsed = new Map(this._lastUsed);
|
|
302
|
+
}
|
|
270
303
|
return collection;
|
|
271
304
|
}
|
|
272
305
|
delete(key) {
|
|
@@ -288,6 +321,11 @@ class BaseCollection extends BaseCollectionMixin {
|
|
|
288
321
|
get(key) {
|
|
289
322
|
const value = this.cache.get(key);
|
|
290
323
|
if ((this.limit !== Infinity || this.expire) && (value !== undefined || this.cache.has(key))) {
|
|
324
|
+
if (this._hasExpired(key)) {
|
|
325
|
+
// the interval just hasn't swept it yet, don't hand it out (or refresh it) past its expiry
|
|
326
|
+
this.delete(key);
|
|
327
|
+
return undefined;
|
|
328
|
+
}
|
|
291
329
|
if (this.limit !== Infinity) {
|
|
292
330
|
// mark it as the most recently used
|
|
293
331
|
const order = this.getOrder();
|
|
@@ -301,21 +339,27 @@ class BaseCollection extends BaseCollectionMixin {
|
|
|
301
339
|
return value;
|
|
302
340
|
}
|
|
303
341
|
getOrInsert(key, defaultValue) {
|
|
304
|
-
|
|
305
|
-
|
|
342
|
+
const value = this.get(key);
|
|
343
|
+
if (value !== undefined || this.cache.has(key)) {
|
|
344
|
+
return value;
|
|
306
345
|
}
|
|
307
346
|
this.set(key, defaultValue);
|
|
308
347
|
return defaultValue;
|
|
309
348
|
}
|
|
310
349
|
getOrInsertComputed(key, func) {
|
|
311
|
-
|
|
312
|
-
|
|
350
|
+
const value = this.get(key);
|
|
351
|
+
if (value !== undefined || this.cache.has(key)) {
|
|
352
|
+
return value;
|
|
313
353
|
}
|
|
314
|
-
const
|
|
315
|
-
this.set(key,
|
|
316
|
-
return
|
|
354
|
+
const computed = func(key);
|
|
355
|
+
this.set(key, computed);
|
|
356
|
+
return computed;
|
|
317
357
|
}
|
|
318
358
|
has(key) {
|
|
359
|
+
if (this._hasExpired(key)) {
|
|
360
|
+
this.delete(key);
|
|
361
|
+
return false;
|
|
362
|
+
}
|
|
319
363
|
return this.cache.has(key);
|
|
320
364
|
}
|
|
321
365
|
keys() {
|
package/lib/baseset.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
export declare class BaseSet<V> extends Set<V> {
|
|
2
2
|
get length(): number;
|
|
3
3
|
clone(): BaseSet<V>;
|
|
4
|
-
every(func:
|
|
4
|
+
every(func: (v: V) => boolean): boolean;
|
|
5
5
|
filter(func: (v: V) => boolean): Array<V>;
|
|
6
6
|
find(func: (v: V) => boolean): undefined | V;
|
|
7
7
|
first(): undefined | V;
|
|
8
8
|
join(separator?: string): string;
|
|
9
9
|
map(func: (v: V) => any): Array<any>;
|
|
10
|
-
reduce(cb: any, initialValue?: any): any;
|
|
10
|
+
reduce(cb: (accumulator: any, v: V) => any, initialValue?: any): any;
|
|
11
11
|
some(func: (v: V) => boolean): boolean;
|
|
12
12
|
sort(func?: (x: V, y: V) => number): Array<V>;
|
|
13
13
|
toArray(): Array<V>;
|
package/lib/baseset.js
CHANGED
|
@@ -9,7 +9,12 @@ class BaseSet extends Set {
|
|
|
9
9
|
return new BaseSet(this.values());
|
|
10
10
|
}
|
|
11
11
|
every(func) {
|
|
12
|
-
|
|
12
|
+
for (let value of this) {
|
|
13
|
+
if (!func(value)) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return true;
|
|
13
18
|
}
|
|
14
19
|
filter(func) {
|
|
15
20
|
const map = [];
|
|
@@ -41,7 +46,20 @@ class BaseSet extends Set {
|
|
|
41
46
|
return map;
|
|
42
47
|
}
|
|
43
48
|
reduce(cb, initialValue) {
|
|
44
|
-
|
|
49
|
+
const values = this.values();
|
|
50
|
+
let reduced = initialValue;
|
|
51
|
+
// like `Array.prototype.reduce`, with no initial value the first item is the starting one
|
|
52
|
+
if (arguments.length < 2) {
|
|
53
|
+
const first = values.next();
|
|
54
|
+
if (first.done) {
|
|
55
|
+
throw new TypeError('Reduce of empty set with no initial value');
|
|
56
|
+
}
|
|
57
|
+
reduced = first.value;
|
|
58
|
+
}
|
|
59
|
+
for (let value of values) {
|
|
60
|
+
reduced = cb(reduced, value);
|
|
61
|
+
}
|
|
62
|
+
return reduced;
|
|
45
63
|
}
|
|
46
64
|
some(func) {
|
|
47
65
|
for (let value of this) {
|
package/lib/constants.d.ts
CHANGED
package/lib/constants.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
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
|
+
// package.json is in every published package and sits one folder up from both src/ and lib/, `require` keeps tsc
|
|
5
|
+
// from pulling it into the build (it's outside rootDir)
|
|
6
|
+
const { version } = require('../package.json');
|
|
4
7
|
exports.Package = Object.freeze({
|
|
5
8
|
URL: 'https://github.com/detritusjs/utils',
|
|
6
|
-
VERSION:
|
|
9
|
+
VERSION: version,
|
|
7
10
|
});
|
|
8
11
|
exports.DISCORD_SNOWFLAKE_EPOCH = 1420070400000;
|
|
9
12
|
exports.DISCORD_TOKEN_EPOCH = 1293840000000;
|
package/lib/eventspewer.d.ts
CHANGED
|
@@ -24,6 +24,11 @@ export declare class EventSubscription {
|
|
|
24
24
|
listener: ((...args: any[]) => void) | null;
|
|
25
25
|
name: string | symbol;
|
|
26
26
|
spewer: EventSpewer | null;
|
|
27
|
+
/**
|
|
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
|
+
*/
|
|
31
|
+
wrapped: ((...args: any[]) => void) | null;
|
|
27
32
|
constructor(spewer: EventSpewer, name: string | symbol, listener: (...args: any[]) => void);
|
|
28
33
|
remove(): void;
|
|
29
34
|
}
|
package/lib/eventspewer.js
CHANGED
|
@@ -21,7 +21,7 @@ class EventSpewer extends events_1.EventEmitter {
|
|
|
21
21
|
}
|
|
22
22
|
subscribe(name, listener) {
|
|
23
23
|
const subscription = new EventSubscription(this, name, listener);
|
|
24
|
-
this.on(name,
|
|
24
|
+
this.on(name, subscription.wrapped);
|
|
25
25
|
if (!this._subscriptions) {
|
|
26
26
|
this._subscriptions = new Map();
|
|
27
27
|
}
|
|
@@ -51,33 +51,27 @@ class EventSpewer extends events_1.EventEmitter {
|
|
|
51
51
|
this._deleteSubscriptions(subscription.name, subscriptions);
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
|
-
if (subscription.
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
super.removeListener.call(this, name, listener);
|
|
59
|
-
for (let attached = count(); attached < before - 1; attached++) {
|
|
60
|
-
super.on.call(this, name, listener);
|
|
61
|
-
}
|
|
54
|
+
if (subscription.wrapped) {
|
|
55
|
+
// the wrapper is only ever attached by this subscription, so this can't take out another registration of the
|
|
56
|
+
// same function (like a `once()`, which `removeListener(name, listener)` would remove first)
|
|
57
|
+
super.removeListener.call(this, subscription.name, subscription.wrapped);
|
|
62
58
|
}
|
|
63
59
|
}
|
|
64
60
|
removeListener(name, listener, ...args) {
|
|
65
61
|
super.removeListener.call(this, name, listener, ...args);
|
|
66
62
|
if (this._subscriptions) {
|
|
67
63
|
const subscriptions = this._subscriptions.get(name);
|
|
68
|
-
|
|
69
|
-
|
|
64
|
+
// `listener` is either the subscribed function or a subscription's wrapper (when node removes it, like in `removeAllListeners()`)
|
|
65
|
+
if (subscriptions && subscriptions.some((subscription) => subscription.listener === listener || subscription.wrapped === listener)) {
|
|
66
|
+
// drop the subscriptions whose wrapper isn't attached anymore
|
|
67
|
+
const attached = new Set(this.rawListeners(name));
|
|
70
68
|
for (let i = subscriptions.length - 1; 0 <= i; i--) {
|
|
71
69
|
const subscription = subscriptions[i];
|
|
72
|
-
if (subscription.
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
subscriptions.splice(i, 1);
|
|
78
|
-
subscription.listener = null;
|
|
79
|
-
subscription.spewer = null;
|
|
80
|
-
}
|
|
70
|
+
if (!attached.has(subscription.wrapped)) {
|
|
71
|
+
subscriptions.splice(i, 1);
|
|
72
|
+
subscription.listener = null;
|
|
73
|
+
subscription.spewer = null;
|
|
74
|
+
subscription.wrapped = null;
|
|
81
75
|
}
|
|
82
76
|
}
|
|
83
77
|
this._deleteSubscriptions(name, subscriptions);
|
|
@@ -103,6 +97,7 @@ class EventSpewer extends events_1.EventEmitter {
|
|
|
103
97
|
for (let subscription of subscriptions) {
|
|
104
98
|
subscription.listener = null;
|
|
105
99
|
subscription.spewer = null;
|
|
100
|
+
subscription.wrapped = null;
|
|
106
101
|
}
|
|
107
102
|
}
|
|
108
103
|
this._subscriptions = undefined;
|
|
@@ -135,10 +130,22 @@ class EventSubscription {
|
|
|
135
130
|
listener;
|
|
136
131
|
name;
|
|
137
132
|
spewer;
|
|
133
|
+
/**
|
|
134
|
+
* 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
|
+
*/
|
|
137
|
+
wrapped;
|
|
138
138
|
constructor(spewer, name, listener) {
|
|
139
139
|
this.name = name;
|
|
140
140
|
this.listener = listener;
|
|
141
141
|
this.spewer = spewer;
|
|
142
|
+
const wrapped = function (...args) {
|
|
143
|
+
return listener.apply(this, args);
|
|
144
|
+
};
|
|
145
|
+
// the same trick node uses for `once()`, so `listeners()`, `listenerCount(name, listener)` and
|
|
146
|
+
// `removeListener(name, listener)` still see the original function
|
|
147
|
+
wrapped.listener = listener;
|
|
148
|
+
this.wrapped = wrapped;
|
|
142
149
|
}
|
|
143
150
|
remove() {
|
|
144
151
|
if (this.spewer) {
|
|
@@ -146,6 +153,7 @@ class EventSubscription {
|
|
|
146
153
|
}
|
|
147
154
|
this.listener = null;
|
|
148
155
|
this.spewer = null;
|
|
156
|
+
this.wrapped = null;
|
|
149
157
|
}
|
|
150
158
|
}
|
|
151
159
|
exports.EventSubscription = EventSubscription;
|
package/lib/snowflake.js
CHANGED
|
@@ -22,30 +22,56 @@ const max = Object.freeze({
|
|
|
22
22
|
sequence: -1n ^ (-1n << bits.sequence),
|
|
23
23
|
workerId: -1n ^ (-1n << bits.workerId),
|
|
24
24
|
});
|
|
25
|
+
// the last generated millisecond (absolute, not relative to an epoch) and the sequence used in it
|
|
25
26
|
const cache = {
|
|
26
27
|
sequence: 0n,
|
|
28
|
+
timestamp: -1n,
|
|
27
29
|
};
|
|
28
30
|
const DEFAULT_EPOCH = BigInt(constants_1.DISCORD_SNOWFLAKE_EPOCH);
|
|
31
|
+
const MAX_ID = 1n << 64n;
|
|
29
32
|
function getEpoch(epoch) {
|
|
30
33
|
return (epoch === undefined || epoch === null) ? DEFAULT_EPOCH : BigInt(epoch);
|
|
31
34
|
}
|
|
35
|
+
function parseId(id) {
|
|
36
|
+
const snowflake = BigInt(id);
|
|
37
|
+
// snowflakes are unsigned 64 bit, anything outside of that would decode into garbage
|
|
38
|
+
if (snowflake < 0n || MAX_ID <= snowflake) {
|
|
39
|
+
throw new RangeError(`Snowflake ${id} is not an unsigned 64 bit integer`);
|
|
40
|
+
}
|
|
41
|
+
return snowflake;
|
|
42
|
+
}
|
|
32
43
|
function generate(options = {}) {
|
|
33
44
|
const epoch = getEpoch(options.epoch);
|
|
34
45
|
const processId = BigInt(options.processId ?? 0) & max.processId;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (elapsed < 0n) {
|
|
46
|
+
let now = BigInt(options.timestamp ?? Date.now());
|
|
47
|
+
if (now < epoch) {
|
|
38
48
|
throw new RangeError(`Timestamp ${now} is before the epoch ${epoch}`);
|
|
39
49
|
}
|
|
40
|
-
const timestamp = elapsed % max.timestamp;
|
|
41
50
|
const workerId = BigInt(options.workerId ?? 0) & max.workerId;
|
|
42
51
|
let sequence;
|
|
43
52
|
if (options.sequence === undefined || options.sequence === null) {
|
|
44
|
-
|
|
53
|
+
// the sequence only counts within one millisecond, so ids stay unique until 4096 are made in the same one
|
|
54
|
+
if ((options.timestamp === undefined || options.timestamp === null) && now < cache.timestamp) {
|
|
55
|
+
// the clock went backwards (or the last millisecond ran out of sequences), keep counting from the last one
|
|
56
|
+
now = cache.timestamp;
|
|
57
|
+
}
|
|
58
|
+
if (now === cache.timestamp) {
|
|
59
|
+
sequence = (cache.sequence + 1n) & max.sequence;
|
|
60
|
+
if (!sequence) {
|
|
61
|
+
// every sequence in this millisecond is used, borrow the next one
|
|
62
|
+
now++;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
sequence = 0n;
|
|
67
|
+
}
|
|
68
|
+
cache.sequence = sequence;
|
|
69
|
+
cache.timestamp = now;
|
|
45
70
|
}
|
|
46
71
|
else {
|
|
47
72
|
sequence = BigInt(options.sequence) & max.sequence;
|
|
48
73
|
}
|
|
74
|
+
const timestamp = (now - epoch) % max.timestamp;
|
|
49
75
|
const snowflake = {
|
|
50
76
|
id: '',
|
|
51
77
|
processId: Number(processId),
|
|
@@ -60,8 +86,12 @@ function generate(options = {}) {
|
|
|
60
86
|
return snowflake;
|
|
61
87
|
}
|
|
62
88
|
function deconstruct(id, options = {}) {
|
|
89
|
+
if (!id) {
|
|
90
|
+
// same as `timestamp()`, an empty id has no timestamp instead of the epoch
|
|
91
|
+
return { id, processId: 0, sequence: 0, timestamp: 0, workerId: 0 };
|
|
92
|
+
}
|
|
63
93
|
const epoch = getEpoch(options.epoch);
|
|
64
|
-
const snowflake =
|
|
94
|
+
const snowflake = parseId(id);
|
|
65
95
|
return {
|
|
66
96
|
id,
|
|
67
97
|
processId: Number((snowflake & 0x1f000n) >> shift.processId),
|
|
@@ -73,7 +103,7 @@ function deconstruct(id, options = {}) {
|
|
|
73
103
|
function timestamp(id, options = {}) {
|
|
74
104
|
if (id) {
|
|
75
105
|
const epoch = getEpoch(options.epoch);
|
|
76
|
-
return Number((
|
|
106
|
+
return Number((parseId(id) >> shift.timestamp) + epoch);
|
|
77
107
|
}
|
|
78
108
|
return 0;
|
|
79
109
|
}
|
package/lib/tools.d.ts
CHANGED
|
@@ -8,4 +8,17 @@ export type URIEncodeWrapFunc = (...args: Array<any>) => string;
|
|
|
8
8
|
export type URIEncodeWrapped = {
|
|
9
9
|
[key: string]: any;
|
|
10
10
|
};
|
|
11
|
-
|
|
11
|
+
/**
|
|
12
|
+
* What a wrapped function actually gets for an argument: truthy ones are stringified and encoded, falsy ones pass through
|
|
13
|
+
*/
|
|
14
|
+
export type URIEncodedArgument<T> = string | Extract<T, '' | 0 | 0n | false | null | undefined>;
|
|
15
|
+
/**
|
|
16
|
+
* Each function has to accept what it'll really be called with, so a parameter typed `number` or `bigint` (that would
|
|
17
|
+
* get a string at runtime) is a type error instead of something like `id + 1n` silently concatenating
|
|
18
|
+
*/
|
|
19
|
+
export type URIEncodeWrapSafe<T> = {
|
|
20
|
+
[K in keyof T]: T[K] extends (...args: infer A) => any ? (...args: {
|
|
21
|
+
[I in keyof A]: URIEncodedArgument<A[I]>;
|
|
22
|
+
}) => any : T[K];
|
|
23
|
+
};
|
|
24
|
+
export declare function URIEncodeWrap<T extends URIEncodeWrapSafe<T>>(unsafe: T): Readonly<T>;
|
package/lib/tools.js
CHANGED
|
@@ -10,7 +10,8 @@ function normalize(object) {
|
|
|
10
10
|
return Object.freeze(object);
|
|
11
11
|
}
|
|
12
12
|
function guildIdToShardId(guildId, shardCount = 0) {
|
|
13
|
-
|
|
13
|
+
// `BigInt()` throws on a fractional or `NaN` count
|
|
14
|
+
if (!Number.isInteger(shardCount) || shardCount < 1) {
|
|
14
15
|
return 0;
|
|
15
16
|
}
|
|
16
17
|
let id;
|
|
@@ -20,9 +21,17 @@ function guildIdToShardId(guildId, shardCount = 0) {
|
|
|
20
21
|
catch (e) {
|
|
21
22
|
return NaN;
|
|
22
23
|
}
|
|
24
|
+
// a negative id isn't a snowflake, and bigint `%` would keep the sign and give a negative shard
|
|
25
|
+
if (id < 0n) {
|
|
26
|
+
return NaN;
|
|
27
|
+
}
|
|
23
28
|
return Number((id >> 22n) % BigInt(shardCount));
|
|
24
29
|
}
|
|
30
|
+
// `encodeURIComponent` leaves `.` alone, so a `.` or `..` argument would become a path segment that climbs out of the url
|
|
31
|
+
// `%2E` counts as a dot to the url parser too, so the percent itself is encoded (same as `detritus-rest` routes)
|
|
32
|
+
const DotSegmentRegex = /^\.{1,2}$/;
|
|
25
33
|
const safeCharacter = '@';
|
|
34
|
+
// generic so the wrapped route builders keep their types, the functions keep their signatures (only their arguments get encoded)
|
|
26
35
|
function URIEncodeWrap(unsafe) {
|
|
27
36
|
const safe = {};
|
|
28
37
|
for (let key in unsafe) {
|
|
@@ -35,8 +44,13 @@ function URIEncodeWrap(unsafe) {
|
|
|
35
44
|
for (let i = 0; i < args.length; i++) {
|
|
36
45
|
const arg = args[i];
|
|
37
46
|
if (arg) {
|
|
38
|
-
|
|
39
|
-
|
|
47
|
+
// a lone surrogate would make `encodeURIComponent` throw, they get replaced with U+FFFD instead
|
|
48
|
+
const value = String(arg).toWellFormed();
|
|
49
|
+
let encoded = (value.includes(safeCharacter)) ? value.split(safeCharacter).map(encodeURIComponent).join(safeCharacter) : encodeURIComponent(value);
|
|
50
|
+
if (DotSegmentRegex.test(encoded)) {
|
|
51
|
+
encoded = encoded.replace(/\./g, '%252E');
|
|
52
|
+
}
|
|
53
|
+
args[i] = encoded;
|
|
40
54
|
}
|
|
41
55
|
}
|
|
42
56
|
return path(...args);
|