detritus-utils 0.5.0-beta.3 → 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/lib/basecollection.d.ts +1 -0
- package/lib/basecollection.js +20 -13
- package/lib/constants.d.ts +1 -1
- package/lib/constants.js +1 -1
- package/lib/eventspewer.d.ts +2 -1
- package/lib/eventspewer.js +48 -33
- package/lib/snowflake.js +14 -21
- package/lib/tools.js +6 -11
- package/package.json +1 -1
package/lib/basecollection.d.ts
CHANGED
|
@@ -37,6 +37,7 @@ export declare class BaseCollection<K, V> extends BaseCollectionMixin<K, V> {
|
|
|
37
37
|
limit: number;
|
|
38
38
|
constructor({ expire, intervalTime, limit }?: BaseCollectionOptions);
|
|
39
39
|
get lastUsed(): Map<K, number>;
|
|
40
|
+
touch(key: K): void;
|
|
40
41
|
get shouldStartInterval(): boolean;
|
|
41
42
|
setExpire(value: number): this;
|
|
42
43
|
setLimit(value: number): this;
|
package/lib/basecollection.js
CHANGED
|
@@ -167,6 +167,11 @@ class BaseCollection extends BaseCollectionMixin {
|
|
|
167
167
|
}
|
|
168
168
|
return new Map();
|
|
169
169
|
}
|
|
170
|
+
touch(key) {
|
|
171
|
+
const lastUsed = this.lastUsed;
|
|
172
|
+
lastUsed.delete(key);
|
|
173
|
+
lastUsed.set(key, Date.now());
|
|
174
|
+
}
|
|
170
175
|
get shouldStartInterval() {
|
|
171
176
|
return !!this.intervalTime && (!this.interval || !this.interval.hasStarted);
|
|
172
177
|
}
|
|
@@ -220,11 +225,11 @@ class BaseCollection extends BaseCollectionMixin {
|
|
|
220
225
|
const expire = this.expire;
|
|
221
226
|
if (expire) {
|
|
222
227
|
const now = Date.now();
|
|
223
|
-
for (let [key,
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
this.delete(key);
|
|
228
|
+
for (let [key, lastUsed] of this.lastUsed) {
|
|
229
|
+
if (now - lastUsed <= expire) {
|
|
230
|
+
break;
|
|
227
231
|
}
|
|
232
|
+
this.delete(key);
|
|
228
233
|
}
|
|
229
234
|
}
|
|
230
235
|
else {
|
|
@@ -282,14 +287,16 @@ class BaseCollection extends BaseCollectionMixin {
|
|
|
282
287
|
}
|
|
283
288
|
get(key) {
|
|
284
289
|
const value = this.cache.get(key);
|
|
285
|
-
if (this.limit !== Infinity && this.cache.has(key)) {
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
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
|
+
}
|
|
293
300
|
}
|
|
294
301
|
return value;
|
|
295
302
|
}
|
|
@@ -332,7 +339,7 @@ class BaseCollection extends BaseCollectionMixin {
|
|
|
332
339
|
}
|
|
333
340
|
this.cache.set(key, value);
|
|
334
341
|
if (this.expire) {
|
|
335
|
-
this.
|
|
342
|
+
this.touch(key);
|
|
336
343
|
if (this.shouldStartInterval) {
|
|
337
344
|
this.startInterval();
|
|
338
345
|
}
|
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,10 +10,11 @@ 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;
|
|
18
19
|
removeListener(name: string | symbol, listener: (...args: any[]) => void, ...args: Array<any>): this;
|
|
19
20
|
off(name: string | symbol, listener: (...args: any[]) => void, ...args: Array<any>): this;
|
package/lib/eventspewer.js
CHANGED
|
@@ -28,49 +28,64 @@ class EventSpewer extends Emitter {
|
|
|
28
28
|
const subscription = new EventSubscription(this, name, listener);
|
|
29
29
|
this.on(name, listener);
|
|
30
30
|
if (!this._subscriptions) {
|
|
31
|
-
this._subscriptions =
|
|
31
|
+
this._subscriptions = new Map();
|
|
32
32
|
}
|
|
33
|
-
this._subscriptions.
|
|
33
|
+
let subscriptions = this._subscriptions.get(name);
|
|
34
|
+
if (!subscriptions) {
|
|
35
|
+
this._subscriptions.set(name, subscriptions = []);
|
|
36
|
+
}
|
|
37
|
+
subscriptions.push(subscription);
|
|
34
38
|
return subscription;
|
|
35
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
|
+
}
|
|
36
48
|
removeSubscription(subscription) {
|
|
37
49
|
if (this._subscriptions) {
|
|
38
|
-
const
|
|
39
|
-
if (
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
else {
|
|
44
|
-
this._subscriptions.splice(index, 1);
|
|
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);
|
|
45
55
|
}
|
|
46
|
-
|
|
47
|
-
if (!this._subscriptions.length) {
|
|
48
|
-
this._subscriptions = undefined;
|
|
56
|
+
this._deleteSubscriptions(subscription.name, subscriptions);
|
|
49
57
|
}
|
|
50
58
|
}
|
|
51
59
|
if (subscription.listener) {
|
|
52
|
-
|
|
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
|
+
}
|
|
53
67
|
}
|
|
54
68
|
}
|
|
55
69
|
removeListener(name, listener, ...args) {
|
|
56
70
|
super.removeListener.call(this, name, listener, ...args);
|
|
57
71
|
if (this._subscriptions) {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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
|
+
}
|
|
69
86
|
}
|
|
70
87
|
}
|
|
71
|
-
|
|
72
|
-
if (!this._subscriptions.length) {
|
|
73
|
-
this._subscriptions = undefined;
|
|
88
|
+
this._deleteSubscriptions(name, subscriptions);
|
|
74
89
|
}
|
|
75
90
|
}
|
|
76
91
|
return this;
|
|
@@ -81,16 +96,16 @@ class EventSpewer extends Emitter {
|
|
|
81
96
|
removeAllListeners(name) {
|
|
82
97
|
if (this._subscriptions) {
|
|
83
98
|
if (name !== undefined) {
|
|
84
|
-
|
|
85
|
-
|
|
99
|
+
const subscriptions = this._subscriptions.get(name);
|
|
100
|
+
if (subscriptions) {
|
|
101
|
+
for (let subscription of subscriptions.slice()) {
|
|
86
102
|
subscription.remove();
|
|
87
103
|
}
|
|
88
104
|
}
|
|
89
105
|
}
|
|
90
106
|
else {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
if (subscription) {
|
|
107
|
+
for (let subscriptions of this._subscriptions.values()) {
|
|
108
|
+
for (let subscription of subscriptions) {
|
|
94
109
|
subscription.listener = null;
|
|
95
110
|
subscription.spewer = null;
|
|
96
111
|
}
|
|
@@ -111,7 +126,7 @@ class EventSpewer extends Emitter {
|
|
|
111
126
|
}
|
|
112
127
|
removeAllSubscriptions() {
|
|
113
128
|
if (this._subscriptions) {
|
|
114
|
-
const subscriptions = this._subscriptions;
|
|
129
|
+
const subscriptions = Array.from(this._subscriptions.values()).flat();
|
|
115
130
|
this._subscriptions = undefined;
|
|
116
131
|
for (let subscription of subscriptions) {
|
|
117
132
|
subscription.remove();
|
package/lib/snowflake.js
CHANGED
|
@@ -25,23 +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
|
-
workerId: 0,
|
|
34
|
-
}, options);
|
|
35
|
-
const epoch = BigInt(options.epoch);
|
|
36
|
-
const processId = BigInt(options.processId) & max.processId;
|
|
37
|
-
const elapsed = BigInt(options.timestamp) - epoch;
|
|
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;
|
|
38
37
|
if (elapsed < 0n) {
|
|
39
|
-
throw new RangeError(`Timestamp ${
|
|
38
|
+
throw new RangeError(`Timestamp ${now} is before the epoch ${epoch}`);
|
|
40
39
|
}
|
|
41
40
|
const timestamp = elapsed % max.timestamp;
|
|
42
|
-
const workerId = BigInt(options.workerId) & max.workerId;
|
|
41
|
+
const workerId = BigInt(options.workerId ?? 0) & max.workerId;
|
|
43
42
|
let sequence;
|
|
44
|
-
if (options.sequence === undefined) {
|
|
43
|
+
if (options.sequence === undefined || options.sequence === null) {
|
|
45
44
|
sequence = cache.sequence = ++cache.sequence & max.sequence;
|
|
46
45
|
}
|
|
47
46
|
else {
|
|
@@ -51,7 +50,7 @@ function generate(options = {}) {
|
|
|
51
50
|
id: '',
|
|
52
51
|
processId: Number(processId),
|
|
53
52
|
sequence: Number(sequence),
|
|
54
|
-
timestamp: Number(timestamp),
|
|
53
|
+
timestamp: Number(timestamp + epoch),
|
|
55
54
|
workerId: Number(workerId),
|
|
56
55
|
};
|
|
57
56
|
snowflake.id = String((timestamp << shift.timestamp) |
|
|
@@ -61,10 +60,7 @@ function generate(options = {}) {
|
|
|
61
60
|
return snowflake;
|
|
62
61
|
}
|
|
63
62
|
function deconstruct(id, options = {}) {
|
|
64
|
-
|
|
65
|
-
epoch: constants_1.DISCORD_SNOWFLAKE_EPOCH,
|
|
66
|
-
}, options);
|
|
67
|
-
const epoch = BigInt(options.epoch);
|
|
63
|
+
const epoch = getEpoch(options.epoch);
|
|
68
64
|
const snowflake = BigInt(id);
|
|
69
65
|
return {
|
|
70
66
|
id,
|
|
@@ -75,11 +71,8 @@ function deconstruct(id, options = {}) {
|
|
|
75
71
|
};
|
|
76
72
|
}
|
|
77
73
|
function timestamp(id, options = {}) {
|
|
78
|
-
options = Object.assign({
|
|
79
|
-
epoch: constants_1.DISCORD_SNOWFLAKE_EPOCH,
|
|
80
|
-
}, options);
|
|
81
|
-
const epoch = BigInt(options.epoch);
|
|
82
74
|
if (id) {
|
|
75
|
+
const epoch = getEpoch(options.epoch);
|
|
83
76
|
return Number((BigInt(id) >> shift.timestamp) + epoch);
|
|
84
77
|
}
|
|
85
78
|
return 0;
|
package/lib/tools.js
CHANGED
|
@@ -32,18 +32,13 @@ function URIEncodeWrap(unsafe) {
|
|
|
32
32
|
continue;
|
|
33
33
|
}
|
|
34
34
|
safe[key] = ((...args) => {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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);
|
|
38
40
|
}
|
|
39
|
-
|
|
40
|
-
if (!value.includes(safeCharacter)) {
|
|
41
|
-
return encodeURIComponent(value);
|
|
42
|
-
}
|
|
43
|
-
return value.split('').map((char) => {
|
|
44
|
-
return (char === safeCharacter) ? char : encodeURIComponent(char);
|
|
45
|
-
}).join('');
|
|
46
|
-
});
|
|
41
|
+
}
|
|
47
42
|
return path(...args);
|
|
48
43
|
});
|
|
49
44
|
}
|