detritus-utils 0.5.0-beta.3 → 0.5.0-beta.5

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.
@@ -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;
@@ -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, value] of this.cache) {
224
- const lastUsed = this.lastUsed.get(key);
225
- if (lastUsed && expire < now - lastUsed) {
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
- // mark it as the most recently used
287
- const order = this.getOrder();
288
- order.delete(key);
289
- order.set(key, true);
290
- }
291
- if (this.expire && this.cache.has(key)) {
292
- this.lastUsed.set(key, Date.now());
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.lastUsed.set(key, Date.now());
342
+ this.touch(key);
336
343
  if (this.shouldStartInterval) {
337
344
  this.startInterval();
338
345
  }
@@ -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";
3
+ VERSION: "0.5.0-beta.5";
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.3',
6
+ VERSION: '0.5.0-beta.5',
7
7
  });
8
8
  exports.DISCORD_SNOWFLAKE_EPOCH = 1420070400000;
9
9
  exports.DISCORD_TOKEN_EPOCH = 1293840000000;
@@ -1,6 +1,5 @@
1
1
  import { EventEmitter } from 'events';
2
- declare let Emitter: typeof EventEmitter;
3
- export declare class EventSpewer extends Emitter implements EventEmitter {
2
+ export declare class EventSpewer extends EventEmitter {
4
3
  /**
5
4
  * @ignore
6
5
  */
@@ -10,10 +9,11 @@ export declare class EventSpewer extends Emitter implements EventEmitter {
10
9
  /**
11
10
  * @ignore
12
11
  */
13
- _subscriptions: Array<EventSubscription> | undefined;
12
+ _subscriptions: Map<string | symbol, Array<EventSubscription>> | undefined;
14
13
  constructor();
15
14
  hasEventListener(name: string | symbol): boolean;
16
15
  subscribe(name: string | symbol, listener: (...args: any[]) => void): EventSubscription;
16
+ _deleteSubscriptions(name: string | symbol, subscriptions: Array<EventSubscription>): void;
17
17
  removeSubscription(subscription: EventSubscription): void;
18
18
  removeListener(name: string | symbol, listener: (...args: any[]) => void, ...args: Array<any>): this;
19
19
  off(name: string | symbol, listener: (...args: any[]) => void, ...args: Array<any>): this;
@@ -27,4 +27,3 @@ export declare class EventSubscription {
27
27
  constructor(spewer: EventSpewer, name: string | symbol, listener: (...args: any[]) => void);
28
28
  remove(): void;
29
29
  }
30
- export {};
@@ -2,12 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.EventSubscription = exports.EventSpewer = void 0;
4
4
  const events_1 = require("events");
5
- let Emitter = events_1.EventEmitter;
6
- try {
7
- Emitter = require('eventemitter3');
8
- }
9
- catch (e) { }
10
- class EventSpewer extends Emitter {
5
+ class EventSpewer extends events_1.EventEmitter {
11
6
  /**
12
7
  * @ignore
13
8
  */
@@ -28,49 +23,64 @@ class EventSpewer extends Emitter {
28
23
  const subscription = new EventSubscription(this, name, listener);
29
24
  this.on(name, listener);
30
25
  if (!this._subscriptions) {
31
- this._subscriptions = [];
26
+ this._subscriptions = new Map();
27
+ }
28
+ let subscriptions = this._subscriptions.get(name);
29
+ if (!subscriptions) {
30
+ this._subscriptions.set(name, subscriptions = []);
32
31
  }
33
- this._subscriptions.push(subscription);
32
+ subscriptions.push(subscription);
34
33
  return subscription;
35
34
  }
35
+ _deleteSubscriptions(name, subscriptions) {
36
+ if (!subscriptions.length && this._subscriptions) {
37
+ this._subscriptions.delete(name);
38
+ if (!this._subscriptions.size) {
39
+ this._subscriptions = undefined;
40
+ }
41
+ }
42
+ }
36
43
  removeSubscription(subscription) {
37
44
  if (this._subscriptions) {
38
- const index = this._subscriptions.indexOf(subscription);
39
- if (index !== -1) {
40
- if (this._subscriptions.length === 1) {
41
- this._subscriptions.pop();
42
- }
43
- else {
44
- this._subscriptions.splice(index, 1);
45
+ const subscriptions = this._subscriptions.get(subscription.name);
46
+ if (subscriptions) {
47
+ const index = subscriptions.indexOf(subscription);
48
+ if (index !== -1) {
49
+ subscriptions.splice(index, 1);
45
50
  }
46
- }
47
- if (!this._subscriptions.length) {
48
- this._subscriptions = undefined;
51
+ this._deleteSubscriptions(subscription.name, subscriptions);
49
52
  }
50
53
  }
51
54
  if (subscription.listener) {
52
- this.removeListener(subscription.name, subscription.listener);
55
+ const { listener, name } = subscription;
56
+ const count = () => this.listeners(name).filter((x) => x === listener).length;
57
+ const before = count();
58
+ super.removeListener.call(this, name, listener);
59
+ for (let attached = count(); attached < before - 1; attached++) {
60
+ super.on.call(this, name, listener);
61
+ }
53
62
  }
54
63
  }
55
64
  removeListener(name, listener, ...args) {
56
65
  super.removeListener.call(this, name, listener, ...args);
57
66
  if (this._subscriptions) {
58
- let attached = this.listeners(name).filter((x) => x === listener).length;
59
- for (let i = this._subscriptions.length - 1; 0 <= i; i--) {
60
- const subscription = this._subscriptions[i];
61
- if (subscription.name === name && subscription.listener === listener) {
62
- if (attached) {
63
- attached--;
64
- }
65
- else {
66
- this._subscriptions.splice(i, 1);
67
- subscription.listener = null;
68
- subscription.spewer = null;
67
+ const subscriptions = this._subscriptions.get(name);
68
+ if (subscriptions && subscriptions.some((subscription) => subscription.listener === listener)) {
69
+ let attached = this.listeners(name).filter((x) => x === listener).length;
70
+ for (let i = subscriptions.length - 1; 0 <= i; i--) {
71
+ const subscription = subscriptions[i];
72
+ if (subscription.listener === listener) {
73
+ if (attached) {
74
+ attached--;
75
+ }
76
+ else {
77
+ subscriptions.splice(i, 1);
78
+ subscription.listener = null;
79
+ subscription.spewer = null;
80
+ }
69
81
  }
70
82
  }
71
- }
72
- if (!this._subscriptions.length) {
73
- this._subscriptions = undefined;
83
+ this._deleteSubscriptions(name, subscriptions);
74
84
  }
75
85
  }
76
86
  return this;
@@ -81,16 +91,16 @@ class EventSpewer extends Emitter {
81
91
  removeAllListeners(name) {
82
92
  if (this._subscriptions) {
83
93
  if (name !== undefined) {
84
- for (let subscription of this._subscriptions.slice()) {
85
- if (subscription.name === name) {
94
+ const subscriptions = this._subscriptions.get(name);
95
+ if (subscriptions) {
96
+ for (let subscription of subscriptions.slice()) {
86
97
  subscription.remove();
87
98
  }
88
99
  }
89
100
  }
90
101
  else {
91
- while (this._subscriptions.length) {
92
- const subscription = this._subscriptions.shift();
93
- if (subscription) {
102
+ for (let subscriptions of this._subscriptions.values()) {
103
+ for (let subscription of subscriptions) {
94
104
  subscription.listener = null;
95
105
  subscription.spewer = null;
96
106
  }
@@ -111,7 +121,7 @@ class EventSpewer extends Emitter {
111
121
  }
112
122
  removeAllSubscriptions() {
113
123
  if (this._subscriptions) {
114
- const subscriptions = this._subscriptions;
124
+ const subscriptions = Array.from(this._subscriptions.values()).flat();
115
125
  this._subscriptions = undefined;
116
126
  for (let subscription of subscriptions) {
117
127
  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
- options = Object.assign({
30
- epoch: constants_1.DISCORD_SNOWFLAKE_EPOCH,
31
- processId: 0,
32
- timestamp: Date.now(),
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 ${options.timestamp} is before the epoch ${options.epoch}`);
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
- options = Object.assign({
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
- args = args.map((arg) => {
36
- if (!arg) {
37
- return arg;
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
- const value = String(arg);
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
  }
package/package.json CHANGED
@@ -1,20 +1,12 @@
1
1
  {
2
2
  "name": "detritus-utils",
3
- "version": "0.5.0-beta.3",
3
+ "version": "0.5.0-beta.5",
4
4
  "description": "Detritus Utils",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
7
7
  "files": [
8
8
  "lib/**/*"
9
9
  ],
10
- "peerDependencies": {
11
- "eventemitter3": "^5.0.4"
12
- },
13
- "peerDependenciesMeta": {
14
- "eventemitter3": {
15
- "optional": true
16
- }
17
- },
18
10
  "scripts": {
19
11
  "build": "tsc",
20
12
  "test": "echo \"Error: no test specified\" && exit 1",
@@ -38,7 +30,7 @@
38
30
  },
39
31
  "homepage": "https://github.com/detritusjs/utils#readme",
40
32
  "devDependencies": {
41
- "@types/node": "^26.1.2",
33
+ "@types/node": "^26.6.5",
42
34
  "typescript": "^7.0.2"
43
35
  }
44
36
  }