refactor: switch api and gateway to V8 (#4879)

Co-authored-by: Jan <66554238+Vaporox@users.noreply.github.com>
This commit is contained in:
Sugden
2021-02-11 17:10:35 +00:00
committed by GitHub
parent ae3c3d80ee
commit ee5bc1a5c4
33 changed files with 372 additions and 364 deletions

View File

@@ -7,12 +7,12 @@ const { RangeError } = require('../errors');
*/
class BitField {
/**
* @param {BitFieldResolvable} [bits=0] Bit(s) to read from
* @param {BitFieldResolvable} [bits=this.constructor.defaultBit] Bit(s) to read from
*/
constructor(bits) {
constructor(bits = this.constructor.defaultBit) {
/**
* Bitfield of the packed bits
* @type {number}
* @type {number|bigint}
*/
this.bitfield = this.constructor.resolve(bits);
}
@@ -23,7 +23,7 @@ class BitField {
* @returns {boolean}
*/
any(bit) {
return (this.bitfield & this.constructor.resolve(bit)) !== 0;
return (this.bitfield & this.constructor.resolve(bit)) !== this.constructor.defaultBit;
}
/**
@@ -71,7 +71,7 @@ class BitField {
* @returns {BitField} These bits or new BitField if the instance is frozen.
*/
add(...bits) {
let total = 0;
let total = this.constructor.defaultBit;
for (const bit of bits) {
total |= this.constructor.resolve(bit);
}
@@ -86,7 +86,7 @@ class BitField {
* @returns {BitField} These bits or new BitField if the instance is frozen.
*/
remove(...bits) {
let total = 0;
let total = this.constructor.defaultBit;
for (const bit of bits) {
total |= this.constructor.resolve(bit);
}
@@ -117,7 +117,7 @@ class BitField {
}
toJSON() {
return this.bitfield;
return typeof this.bitfield === 'number' ? this.bitfield : this.bitfield.toString();
}
valueOf() {
@@ -133,18 +133,20 @@ class BitField {
* * A bit number (this can be a number literal or a value taken from {@link BitField.FLAGS})
* * An instance of BitField
* * An Array of BitFieldResolvable
* @typedef {number|BitField|BitFieldResolvable[]} BitFieldResolvable
* @typedef {number|bigint|BitField|BitFieldResolvable[]} BitFieldResolvable
*/
/**
* Resolves bitfields to their numeric form.
* @param {BitFieldResolvable} [bit=0] - bit(s) to resolve
* @returns {number}
* @param {BitFieldResolvable} [bit] - bit(s) to resolve
* @returns {number|bigint}
*/
static resolve(bit = 0) {
if (typeof bit === 'number' && bit >= 0) return bit;
static resolve(bit) {
const { defaultBit } = this;
if (typeof bit === 'undefined') return defaultBit;
if (typeof defaultBit === typeof bit && bit >= defaultBit) return bit;
if (bit instanceof BitField) return bit.bitfield;
if (Array.isArray(bit)) return bit.map(p => this.resolve(p)).reduce((prev, p) => prev | p, 0);
if (Array.isArray(bit)) return bit.map(p => this.resolve(p)).reduce((prev, p) => prev | p, defaultBit);
if (typeof bit === 'string' && typeof this.FLAGS[bit] !== 'undefined') return this.FLAGS[bit];
throw new RangeError('BITFIELD_INVALID', bit);
}
@@ -158,4 +160,10 @@ class BitField {
*/
BitField.FLAGS = {};
/**
* @type {number|bigint}
* @private
*/
BitField.defaultBit = 0;
module.exports = BitField;

View File

@@ -30,7 +30,8 @@ const { Error, RangeError } = require('../errors');
* @property {number} [restSweepInterval=60] How frequently to delete inactive request buckets, in seconds
* (or 0 for never)
* @property {number} [retryLimit=1] How many times to retry on 5XX errors (Infinity for indefinite amount of retries)
* @property {PresenceData} [presence] Presence data to use upon login
* @property {PresenceData} [presence={}] Presence data to use upon login
* @property {IntentsResolvable} intents Intents to enable for this connection
* @property {WebsocketOptions} [ws] Options for the WebSocket
* @property {HTTPOptions} [http] HTTP options
*/
@@ -52,7 +53,6 @@ exports.DefaultOptions = {
* @typedef {Object} WebsocketOptions
* @property {number} [large_threshold=50] Number of members in a guild after which offline users will no longer be
* sent in the initial guild member list, must be between 50 and 250
* @property {IntentsResolvable} [intents] Intents to enable for this connection
*/
ws: {
large_threshold: 50,
@@ -62,7 +62,7 @@ exports.DefaultOptions = {
$browser: 'discord.js',
$device: 'discord.js',
},
version: 6,
version: 8,
},
/**
@@ -75,7 +75,7 @@ exports.DefaultOptions = {
* @property {string} [template='https://discord.new'] Base url of templates
*/
http: {
version: 7,
version: 8,
api: 'https://discord.com/api',
cdn: 'https://cdn.discordapp.com',
invite: 'https://discord.gg',
@@ -557,6 +557,7 @@ exports.VerificationLevels = ['NONE', 'LOW', 'MEDIUM', 'HIGH', 'VERY_HIGH'];
* * UNKNOWN_GUILD_TEMPLATE
* * BOT_PROHIBITED_ENDPOINT
* * BOT_ONLY_ENDPOINT
* * ANNOUNCEMENT_EDIT_LIMIT_EXCEEDED
* * CHANNEL_HIT_WRITE_RATELIMIT
* * MAXIMUM_GUILDS
* * MAXIMUM_FRIENDS
@@ -593,7 +594,9 @@ exports.VerificationLevels = ['NONE', 'LOW', 'MEDIUM', 'HIGH', 'VERY_HIGH'];
* * CANNOT_PIN_MESSAGE_IN_OTHER_CHANNEL
* * INVALID_OR_TAKEN_INVITE_CODE
* * CANNOT_EXECUTE_ON_SYSTEM_MESSAGE
* * CANNOT_EXECUTE_ON_CHANNEL_TYPE
* * INVALID_OAUTH_TOKEN
* * INVALID_RECIPIENTS
* * BULK_DELETE_MESSAGE_TOO_OLD
* * INVALID_FORM_BODY
* * INVITE_ACCEPTED_TO_GUILD_NOT_CONTAINING_BOT
@@ -623,6 +626,7 @@ exports.APIErrors = {
UNKNOWN_GUILD_TEMPLATE: 10057,
BOT_PROHIBITED_ENDPOINT: 20001,
BOT_ONLY_ENDPOINT: 20002,
ANNOUNCEMENT_EDIT_LIMIT_EXCEEDED: 20022,
CHANNEL_HIT_WRITE_RATELIMIT: 20028,
MAXIMUM_GUILDS: 30001,
MAXIMUM_FRIENDS: 30002,
@@ -659,7 +663,9 @@ exports.APIErrors = {
CANNOT_PIN_MESSAGE_IN_OTHER_CHANNEL: 50019,
INVALID_OR_TAKEN_INVITE_CODE: 50020,
CANNOT_EXECUTE_ON_SYSTEM_MESSAGE: 50021,
CANNOT_EXECUTE_ON_CHANNEL_TYPE: 50024,
INVALID_OAUTH_TOKEN: 50025,
INVALID_RECIPIENTS: 50033,
BULK_DELETE_MESSAGE_TOO_OLD: 50034,
INVALID_FORM_BODY: 50035,
INVITE_ACCEPTED_TO_GUILD_NOT_CONTAINING_BOT: 50036,
@@ -703,8 +709,26 @@ exports.WebhookTypes = [
'Channel Follower',
];
/**
* An overwrite type:
* * role
* * member
* @typedef {string} OverwriteType
*/
exports.OverwriteTypes = createEnum(['role', 'member']);
function keyMirror(arr) {
let tmp = Object.create(null);
for (const value of arr) tmp[value] = value;
return tmp;
}
function createEnum(keys) {
const obj = {};
for (const [index, key] of keys.entries()) {
if (key === null) continue;
obj[key] = index;
obj[index] = key;
}
return obj;
}

View File

@@ -15,6 +15,12 @@ class MessageFlags extends BitField {}
* @param {BitFieldResolvable} [bits=0] Bit(s) to read from
*/
/**
* Bitfield of the packed bits
* @type {number}
* @name MessageFlags#bitfield
*/
/**
* Numeric message flags. All available properties:
* * `CROSSPOSTED`

View File

@@ -10,10 +10,9 @@ const BitField = require('./BitField');
*/
class Permissions extends BitField {
/**
* @name Permissions
* @kind constructor
* @memberof Permissions
* @param {PermissionResolvable} [bits=0] Bit(s) to read from
* Bitfield of the packed bits
* @type {bigint}
* @name Permissions#bitfield
*/
/**
@@ -22,7 +21,7 @@ class Permissions extends BitField {
* * A permission number
* * An instance of Permissions
* * An Array of PermissionResolvable
* @typedef {string|number|Permissions|PermissionResolvable[]} PermissionResolvable
* @typedef {string|bigint|Permissions|PermissionResolvable[]} PermissionResolvable
*/
/**
@@ -79,53 +78,55 @@ class Permissions extends BitField {
* * `MANAGE_ROLES`
* * `MANAGE_WEBHOOKS`
* * `MANAGE_EMOJIS`
* @type {Object}
* @type {Object<string, bigint>}
* @see {@link https://discord.com/developers/docs/topics/permissions}
*/
Permissions.FLAGS = {
CREATE_INSTANT_INVITE: 1 << 0,
KICK_MEMBERS: 1 << 1,
BAN_MEMBERS: 1 << 2,
ADMINISTRATOR: 1 << 3,
MANAGE_CHANNELS: 1 << 4,
MANAGE_GUILD: 1 << 5,
ADD_REACTIONS: 1 << 6,
VIEW_AUDIT_LOG: 1 << 7,
PRIORITY_SPEAKER: 1 << 8,
STREAM: 1 << 9,
VIEW_CHANNEL: 1 << 10,
SEND_MESSAGES: 1 << 11,
SEND_TTS_MESSAGES: 1 << 12,
MANAGE_MESSAGES: 1 << 13,
EMBED_LINKS: 1 << 14,
ATTACH_FILES: 1 << 15,
READ_MESSAGE_HISTORY: 1 << 16,
MENTION_EVERYONE: 1 << 17,
USE_EXTERNAL_EMOJIS: 1 << 18,
VIEW_GUILD_INSIGHTS: 1 << 19,
CONNECT: 1 << 20,
SPEAK: 1 << 21,
MUTE_MEMBERS: 1 << 22,
DEAFEN_MEMBERS: 1 << 23,
MOVE_MEMBERS: 1 << 24,
USE_VAD: 1 << 25,
CHANGE_NICKNAME: 1 << 26,
MANAGE_NICKNAMES: 1 << 27,
MANAGE_ROLES: 1 << 28,
MANAGE_WEBHOOKS: 1 << 29,
MANAGE_EMOJIS: 1 << 30,
CREATE_INSTANT_INVITE: 1n << 0n,
KICK_MEMBERS: 1n << 1n,
BAN_MEMBERS: 1n << 2n,
ADMINISTRATOR: 1n << 3n,
MANAGE_CHANNELS: 1n << 4n,
MANAGE_GUILD: 1n << 5n,
ADD_REACTIONS: 1n << 6n,
VIEW_AUDIT_LOG: 1n << 7n,
PRIORITY_SPEAKER: 1n << 8n,
STREAM: 1n << 9n,
VIEW_CHANNEL: 1n << 10n,
SEND_MESSAGES: 1n << 11n,
SEND_TTS_MESSAGES: 1n << 12n,
MANAGE_MESSAGES: 1n << 13n,
EMBED_LINKS: 1n << 14n,
ATTACH_FILES: 1n << 15n,
READ_MESSAGE_HISTORY: 1n << 16n,
MENTION_EVERYONE: 1n << 17n,
USE_EXTERNAL_EMOJIS: 1n << 18n,
VIEW_GUILD_INSIGHTS: 1n << 19n,
CONNECT: 1n << 20n,
SPEAK: 1n << 21n,
MUTE_MEMBERS: 1n << 22n,
DEAFEN_MEMBERS: 1n << 23n,
MOVE_MEMBERS: 1n << 24n,
USE_VAD: 1n << 25n,
CHANGE_NICKNAME: 1n << 26n,
MANAGE_NICKNAMES: 1n << 27n,
MANAGE_ROLES: 1n << 28n,
MANAGE_WEBHOOKS: 1n << 29n,
MANAGE_EMOJIS: 1n << 30n,
};
/**
* Bitfield representing every permission combined
* @type {number}
* @type {bigint}
*/
Permissions.ALL = Object.values(Permissions.FLAGS).reduce((all, p) => all | p, 0);
Permissions.ALL = Object.values(Permissions.FLAGS).reduce((all, p) => all | p, 0n);
/**
* Bitfield representing the default permissions for users
* @type {number}
* @type {bigint}
*/
Permissions.DEFAULT = 104324673;
Permissions.DEFAULT = BigInt(104324673);
Permissions.defaultBit = BigInt(0);
module.exports = Permissions;

View File

@@ -16,6 +16,12 @@ class Speaking extends BitField {}
* @param {BitFieldResolvable} [bits=0] Bit(s) to read from
*/
/**
* Bitfield of the packed bits
* @type {number}
* @name Speaking#bitfield
*/
/**
* Numeric speaking flags. All available properties:
* * `SPEAKING`

View File

@@ -17,6 +17,12 @@ class SystemChannelFlags extends BitField {}
* @param {SystemChannelFlagsResolvable} [bits=0] Bit(s) to read from
*/
/**
* Bitfield of the packed bits
* @type {number}
* @name SystemChannelFlags#bitfield
*/
/**
* Data that can be resolved to give a sytem channel flag bitfield. This can be:
* * A string (see {@link SystemChannelFlags.FLAGS})

View File

@@ -14,6 +14,12 @@ class UserFlags extends BitField {}
* @param {BitFieldResolvable} [bits=0] Bit(s) to read from
*/
/**
* Bitfield of the packed bits
* @type {number}
* @name UserFlags#bitfield
*/
/**
* Numeric user flags. All available properties:
* * `DISCORD_EMPLOYEE`