mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-15 11:03:30 +01:00
refactor(SnowflakeUtil): clean up utils and improve perf (#7036)
This commit is contained in:
@@ -1,10 +1,8 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const Util = require('./Util');
|
|
||||||
|
|
||||||
// Discord epoch (2015-01-01T00:00:00.000Z)
|
// Discord epoch (2015-01-01T00:00:00.000Z)
|
||||||
const EPOCH = 1_420_070_400_000;
|
const EPOCH = 1_420_070_400_000;
|
||||||
let INCREMENT = 0;
|
let INCREMENT = BigInt(0);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A container for useful snowflake-related methods.
|
* A container for useful snowflake-related methods.
|
||||||
@@ -36,11 +34,10 @@ class SnowflakeUtil extends null {
|
|||||||
`"timestamp" argument must be a number (received ${isNaN(timestamp) ? 'NaN' : typeof timestamp})`,
|
`"timestamp" argument must be a number (received ${isNaN(timestamp) ? 'NaN' : typeof timestamp})`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (INCREMENT >= 4095) INCREMENT = 0;
|
if (INCREMENT >= 4095n) INCREMENT = BigInt(0);
|
||||||
const BINARY = `${(timestamp - EPOCH).toString(2).padStart(42, '0')}0000100000${(INCREMENT++)
|
|
||||||
.toString(2)
|
// Assign WorkerId as 1 and ProcessId as 0:
|
||||||
.padStart(12, '0')}`;
|
return ((BigInt(timestamp - EPOCH) << 22n) | (1n << 17n) | INCREMENT++).toString();
|
||||||
return Util.binaryToId(BINARY);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -60,16 +57,16 @@ class SnowflakeUtil extends null {
|
|||||||
* @returns {DeconstructedSnowflake}
|
* @returns {DeconstructedSnowflake}
|
||||||
*/
|
*/
|
||||||
static deconstruct(snowflake) {
|
static deconstruct(snowflake) {
|
||||||
const BINARY = Util.idToBinary(snowflake).toString(2).padStart(64, '0');
|
const bigIntSnowflake = BigInt(snowflake);
|
||||||
return {
|
return {
|
||||||
timestamp: parseInt(BINARY.substring(0, 42), 2) + EPOCH,
|
timestamp: Number(bigIntSnowflake >> 22n) + EPOCH,
|
||||||
get date() {
|
get date() {
|
||||||
return new Date(this.timestamp);
|
return new Date(this.timestamp);
|
||||||
},
|
},
|
||||||
workerId: parseInt(BINARY.substring(42, 47), 2),
|
workerId: Number((bigIntSnowflake >> 17n) & 0b11111n),
|
||||||
processId: parseInt(BINARY.substring(47, 52), 2),
|
processId: Number((bigIntSnowflake >> 12n) & 0b11111n),
|
||||||
increment: parseInt(BINARY.substring(52, 64), 2),
|
increment: Number(bigIntSnowflake & 0b111111111111n),
|
||||||
binary: BINARY,
|
binary: bigIntSnowflake.toString(2).padStart(64, '0'),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -522,57 +522,6 @@ class Util extends null {
|
|||||||
return ext && res.ext.startsWith(ext) ? res.name : res.base.split('?')[0];
|
return ext && res.ext.startsWith(ext) ? res.name : res.base.split('?')[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Transforms a snowflake from a decimal string to a bit string.
|
|
||||||
* @param {Snowflake} num Snowflake to be transformed
|
|
||||||
* @returns {string}
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
static idToBinary(num) {
|
|
||||||
let bin = '';
|
|
||||||
let high = parseInt(num.slice(0, -10)) || 0;
|
|
||||||
let low = parseInt(num.slice(-10));
|
|
||||||
while (low > 0 || high > 0) {
|
|
||||||
bin = String(low & 1) + bin;
|
|
||||||
low = Math.floor(low / 2);
|
|
||||||
if (high > 0) {
|
|
||||||
low += 5_000_000_000 * (high % 2);
|
|
||||||
high = Math.floor(high / 2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return bin;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Transforms a snowflake from a bit string to a decimal string.
|
|
||||||
* @param {string} num Bit string to be transformed
|
|
||||||
* @returns {Snowflake}
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
static binaryToId(num) {
|
|
||||||
let dec = '';
|
|
||||||
|
|
||||||
while (num.length > 50) {
|
|
||||||
const high = parseInt(num.slice(0, -32), 2);
|
|
||||||
const low = parseInt((high % 10).toString(2) + num.slice(-32), 2);
|
|
||||||
|
|
||||||
dec = (low % 10).toString() + dec;
|
|
||||||
num =
|
|
||||||
Math.floor(high / 10).toString(2) +
|
|
||||||
Math.floor(low / 10)
|
|
||||||
.toString(2)
|
|
||||||
.padStart(32, '0');
|
|
||||||
}
|
|
||||||
|
|
||||||
num = parseInt(num, 2);
|
|
||||||
while (num > 0) {
|
|
||||||
dec = (num % 10).toString() + dec;
|
|
||||||
num = Math.floor(num / 10);
|
|
||||||
}
|
|
||||||
|
|
||||||
return dec;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Breaks user, role and everyone/here mentions by adding a zero width space after every @ character
|
* Breaks user, role and everyone/here mentions by adding a zero width space after every @ character
|
||||||
* @param {string} str The string to sanitize
|
* @param {string} str The string to sanitize
|
||||||
|
|||||||
2
typings/index.d.ts
vendored
2
typings/index.d.ts
vendored
@@ -2205,7 +2205,6 @@ export class Util extends null {
|
|||||||
private constructor();
|
private constructor();
|
||||||
public static archivedThreadSweepFilter<K, V>(lifetime?: number): SweepFilter<K, V>;
|
public static archivedThreadSweepFilter<K, V>(lifetime?: number): SweepFilter<K, V>;
|
||||||
public static basename(path: string, ext?: string): string;
|
public static basename(path: string, ext?: string): string;
|
||||||
public static binaryToId(num: string): Snowflake;
|
|
||||||
public static cleanContent(str: string, channel: TextBasedChannels): string;
|
public static cleanContent(str: string, channel: TextBasedChannels): string;
|
||||||
/** @deprecated Use {@link MessageOptions.allowedMentions} to control mentions in a message instead. */
|
/** @deprecated Use {@link MessageOptions.allowedMentions} to control mentions in a message instead. */
|
||||||
public static removeMentions(str: string): string;
|
public static removeMentions(str: string): string;
|
||||||
@@ -2225,7 +2224,6 @@ export class Util extends null {
|
|||||||
public static cleanCodeBlockContent(text: string): string;
|
public static cleanCodeBlockContent(text: string): string;
|
||||||
public static fetchRecommendedShards(token: string, options?: FetchRecommendedShardsOptions): Promise<number>;
|
public static fetchRecommendedShards(token: string, options?: FetchRecommendedShardsOptions): Promise<number>;
|
||||||
public static flatten(obj: unknown, ...props: Record<string, boolean | string>[]): unknown;
|
public static flatten(obj: unknown, ...props: Record<string, boolean | string>[]): unknown;
|
||||||
public static idToBinary(num: Snowflake): string;
|
|
||||||
public static makeError(obj: MakeErrorOptions): Error;
|
public static makeError(obj: MakeErrorOptions): Error;
|
||||||
public static makePlainError(err: Error): MakeErrorOptions;
|
public static makePlainError(err: Error): MakeErrorOptions;
|
||||||
public static mergeDefault(def: unknown, given: unknown): unknown;
|
public static mergeDefault(def: unknown, given: unknown): unknown;
|
||||||
|
|||||||
Reference in New Issue
Block a user