mirror of
https://github.com/danbulant/discord.js
synced 2026-07-06 19:50:55 +00:00
Webpack build for branch wsrewrite: 872cd8cc9dc3a1885a04441f80a2439a1d9ad18d
This commit is contained in:
parent
8a49e8691a
commit
a14956741a
2 changed files with 153 additions and 23 deletions
|
|
@ -15966,13 +15966,45 @@ class WebSocketConnection extends EventEmitter {
|
||||||
*/
|
*/
|
||||||
constructor(manager, gateway) {
|
constructor(manager, gateway) {
|
||||||
super();
|
super();
|
||||||
|
/**
|
||||||
|
* The WebSocket Manager of this connection
|
||||||
|
* @type {WebSocketManager}
|
||||||
|
*/
|
||||||
this.manager = manager;
|
this.manager = manager;
|
||||||
|
/**
|
||||||
|
* The client this belongs to
|
||||||
|
* @type {Client}
|
||||||
|
*/
|
||||||
this.client = manager.client;
|
this.client = manager.client;
|
||||||
|
/**
|
||||||
|
* The WebSocket connection itself
|
||||||
|
* @type {WebSocket}
|
||||||
|
*/
|
||||||
this.ws = null;
|
this.ws = null;
|
||||||
|
/**
|
||||||
|
* The current sequence of the WebSocket
|
||||||
|
* @type {number}
|
||||||
|
*/
|
||||||
this.sequence = -1;
|
this.sequence = -1;
|
||||||
|
/**
|
||||||
|
* The current status of the client
|
||||||
|
* @type {number}
|
||||||
|
*/
|
||||||
this.status = Constants.Status.IDLE;
|
this.status = Constants.Status.IDLE;
|
||||||
|
/**
|
||||||
|
* The packet manager of the connection
|
||||||
|
* @type {WebSocketPacketManager}
|
||||||
|
*/
|
||||||
this.packetManager = new PacketManager(this);
|
this.packetManager = new PacketManager(this);
|
||||||
|
/**
|
||||||
|
* The last time a ping was sent
|
||||||
|
* @type {number}
|
||||||
|
*/
|
||||||
this.pingSendTime = 0;
|
this.pingSendTime = 0;
|
||||||
|
/**
|
||||||
|
* Contains the rate limit queue and metadata
|
||||||
|
* @type {Object}
|
||||||
|
*/
|
||||||
this.rateLimit = {
|
this.rateLimit = {
|
||||||
queue: [],
|
queue: [],
|
||||||
remaining: 120,
|
remaining: 120,
|
||||||
|
|
@ -15981,6 +16013,10 @@ class WebSocketConnection extends EventEmitter {
|
||||||
this.connect(gateway);
|
this.connect(gateway);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Causes the client to be marked as ready and emits the ready event
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
triggerReady() {
|
triggerReady() {
|
||||||
if (this.status === Constants.Status.READY) return this.debug('Tried to mark self as ready, but already ready');
|
if (this.status === Constants.Status.READY) return this.debug('Tried to mark self as ready, but already ready');
|
||||||
this.status = Constants.Status.READY;
|
this.status = Constants.Status.READY;
|
||||||
|
|
@ -15988,6 +16024,10 @@ class WebSocketConnection extends EventEmitter {
|
||||||
return this.packetManager.handleQueue();
|
return this.packetManager.handleQueue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether the client is ready to be marked as ready
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
checkIfReady() {
|
checkIfReady() {
|
||||||
if (this.status === Constants.Status.READY || this.status === Constants.Status.NEARLY) return false;
|
if (this.status === Constants.Status.READY || this.status === Constants.Status.NEARLY) return false;
|
||||||
let unavailableGuilds = 0;
|
let unavailableGuilds = 0;
|
||||||
|
|
@ -16010,11 +16050,21 @@ class WebSocketConnection extends EventEmitter {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Util
|
// Util
|
||||||
|
/**
|
||||||
|
* Emits a debug message
|
||||||
|
* @param {string} message the debug message
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
debug(message) {
|
debug(message) {
|
||||||
if (message instanceof Error) message = message.stack;
|
if (message instanceof Error) message = message.stack;
|
||||||
return this.manager.debug(`[connection] ${message}`);
|
return this.manager.debug(`[connection] ${message}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attempts to serialise data from the WebSocket
|
||||||
|
* @param {string|Object} data the data to unpack
|
||||||
|
* @returns {Object}
|
||||||
|
*/
|
||||||
unpack(data) {
|
unpack(data) {
|
||||||
if (erlpack && typeof data !== 'string') {
|
if (erlpack && typeof data !== 'string') {
|
||||||
if (data instanceof ArrayBuffer) data = Buffer.from(new Uint8Array(data));
|
if (data instanceof ArrayBuffer) data = Buffer.from(new Uint8Array(data));
|
||||||
|
|
@ -16025,10 +16075,18 @@ class WebSocketConnection extends EventEmitter {
|
||||||
return JSON.parse(data);
|
return JSON.parse(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Packs an object ready to be sent
|
||||||
|
* @param {Object} data the data to pack
|
||||||
|
* @returns {string|Buffer}
|
||||||
|
*/
|
||||||
pack(data) {
|
pack(data) {
|
||||||
return erlpack ? erlpack.pack(data) : JSON.stringify(data);
|
return erlpack ? erlpack.pack(data) : JSON.stringify(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Processes the current WebSocket queue
|
||||||
|
*/
|
||||||
processQueue() {
|
processQueue() {
|
||||||
if (this.rateLimit.remaining === 0) return;
|
if (this.rateLimit.remaining === 0) return;
|
||||||
if (this.rateLimit.queue.length === 0) return;
|
if (this.rateLimit.queue.length === 0) return;
|
||||||
|
|
@ -16046,6 +16104,11 @@ class WebSocketConnection extends EventEmitter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends data, bypassing the queue
|
||||||
|
* @param {Object} data The packet to send
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
_send(data) {
|
_send(data) {
|
||||||
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
||||||
return this.debug(`Tried to send packet ${data} but no WebSocket is available!`);
|
return this.debug(`Tried to send packet ${data} but no WebSocket is available!`);
|
||||||
|
|
@ -16053,6 +16116,11 @@ class WebSocketConnection extends EventEmitter {
|
||||||
return this.ws.send(this.pack(data));
|
return this.ws.send(this.pack(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds data to the queue to be sent
|
||||||
|
* @param {Object} data The packet to send
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
send(data) {
|
send(data) {
|
||||||
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
||||||
return this.debug(`Tried to send packet ${data} but no WebSocket is available!`);
|
return this.debug(`Tried to send packet ${data} but no WebSocket is available!`);
|
||||||
|
|
@ -16061,6 +16129,13 @@ class WebSocketConnection extends EventEmitter {
|
||||||
return this.processQueue();
|
return this.processQueue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a connection to a gateway
|
||||||
|
* @param {string} gateway the gateway to connect to
|
||||||
|
* @param {number} [after=0] How long to wait before connecting
|
||||||
|
* @param {boolean} [force=false] Whether or not to force a new connection even if one already exists
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
connect(gateway = this.gateway, after = 0, force = false) {
|
connect(gateway = this.gateway, after = 0, force = false) {
|
||||||
if (after) return this.client.setTimeout(() => this.connect(gateway, 0, force), after); // eslint-disable-line
|
if (after) return this.client.setTimeout(() => this.connect(gateway, 0, force), after); // eslint-disable-line
|
||||||
if (this.ws && !force) return this.debug('WebSocket connection already exists');
|
if (this.ws && !force) return this.debug('WebSocket connection already exists');
|
||||||
|
|
@ -16078,6 +16153,10 @@ class WebSocketConnection extends EventEmitter {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destroys the connection
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
destroy() {
|
destroy() {
|
||||||
const ws = this.ws;
|
const ws = this.ws;
|
||||||
if (!ws) return this.debug('Attempted to destroy WebSocket but no connection exists!');
|
if (!ws) return this.debug('Attempted to destroy WebSocket but no connection exists!');
|
||||||
|
|
@ -16088,6 +16167,11 @@ class WebSocketConnection extends EventEmitter {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called whenever a message is received
|
||||||
|
* @param {Event} event The event received
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
onMessage(event) {
|
onMessage(event) {
|
||||||
try {
|
try {
|
||||||
this.onPacket(this.unpack(event.data));
|
this.onPacket(this.unpack(event.data));
|
||||||
|
|
@ -16098,10 +16182,19 @@ class WebSocketConnection extends EventEmitter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the current sequence of the connection
|
||||||
|
* @param {number} s The new sequence
|
||||||
|
*/
|
||||||
setSequence(s) {
|
setSequence(s) {
|
||||||
this.sequence = s > this.sequence ? s : this.sequence;
|
this.sequence = s > this.sequence ? s : this.sequence;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called whenever a packet is received
|
||||||
|
* @param {Object} packet The received packet
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
onPacket(packet) {
|
onPacket(packet) {
|
||||||
if (!packet) return this.debug('Received null packet');
|
if (!packet) return this.debug('Received null packet');
|
||||||
this.client.emit('raw', packet);
|
this.client.emit('raw', packet);
|
||||||
|
|
@ -16125,20 +16218,35 @@ class WebSocketConnection extends EventEmitter {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called whenever a connection is opened to the gateway
|
||||||
|
* @param {Event} event The received open event
|
||||||
|
*/
|
||||||
onOpen(event) {
|
onOpen(event) {
|
||||||
this.gateway = event.target.url;
|
this.gateway = event.target.url;
|
||||||
this.debug(`Connected to gateway ${this.gateway}`);
|
this.debug(`Connected to gateway ${this.gateway}`);
|
||||||
this.identify();
|
this.identify();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Causes a reconnection to the gateway
|
||||||
|
*/
|
||||||
reconnect() {
|
reconnect() {
|
||||||
this.connect(this.gateway, 5500, true);
|
this.connect(this.gateway, 5500, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called whenever an error occurs with the WebSocket
|
||||||
|
* @param {Error} error The error
|
||||||
|
*/
|
||||||
onError(error) {
|
onError(error) {
|
||||||
this.debug(error);
|
this.debug(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called whenever a connection to the gateway is closed
|
||||||
|
* @param {CloseEvent} event The close event
|
||||||
|
*/
|
||||||
onClose(event) {
|
onClose(event) {
|
||||||
this.debug(`Closed: ${event.code}`);
|
this.debug(`Closed: ${event.code}`);
|
||||||
// Reset the state before trying to fix anything
|
// Reset the state before trying to fix anything
|
||||||
|
|
@ -16150,10 +16258,18 @@ class WebSocketConnection extends EventEmitter {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Heartbeat
|
// Heartbeat
|
||||||
|
/**
|
||||||
|
* Acknowledges a heartbeat
|
||||||
|
*/
|
||||||
ackHeartbeat() {
|
ackHeartbeat() {
|
||||||
this.client._pong(this.pingSendTime);
|
this.client._pong(this.pingSendTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a heartbeat or sets an interval for sending heartbeats.
|
||||||
|
* @param {number} [time] If -1, clears the interval, any other number sets an interval.
|
||||||
|
* If no value is given, a heartbeat will be sent instantly.
|
||||||
|
*/
|
||||||
heartbeat(time) {
|
heartbeat(time) {
|
||||||
if (!isNaN(time)) {
|
if (!isNaN(time)) {
|
||||||
if (time === -1) {
|
if (time === -1) {
|
||||||
|
|
@ -16175,11 +16291,20 @@ class WebSocketConnection extends EventEmitter {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Identification
|
// Identification
|
||||||
|
/**
|
||||||
|
* Identifies the client on a connection
|
||||||
|
* @param {number} [after] How long to wait before identifying
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
identify(after) {
|
identify(after) {
|
||||||
if (after) return this.client.setTimeout(this.identify.apply(this), after);
|
if (after) return this.client.setTimeout(this.identify.apply(this), after);
|
||||||
return this.sessionID ? this.identifyResume() : this.identifyNew();
|
return this.sessionID ? this.identifyResume() : this.identifyNew();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Identifies as a new connection on the gateway
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
identifyNew() {
|
identifyNew() {
|
||||||
if (!this.client.token) {
|
if (!this.client.token) {
|
||||||
return this.debug('No token available to identify a new session with');
|
return this.debug('No token available to identify a new session with');
|
||||||
|
|
@ -16196,6 +16321,10 @@ class WebSocketConnection extends EventEmitter {
|
||||||
return this.send({ op: Constants.OPCodes.IDENTIFY, d });
|
return this.send({ op: Constants.OPCodes.IDENTIFY, d });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resumes a session on the gateway
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
identifyResume() {
|
identifyResume() {
|
||||||
if (!this.sessionID) {
|
if (!this.sessionID) {
|
||||||
this.debug('Warning: wanted to resume but session ID not available; identifying as a new session instead');
|
this.debug('Warning: wanted to resume but session ID not available; identifying as a new session instead');
|
||||||
|
|
@ -16683,13 +16812,6 @@ class Client extends EventEmitter {
|
||||||
*/
|
*/
|
||||||
this.pings = [];
|
this.pings = [];
|
||||||
|
|
||||||
/**
|
|
||||||
* Timestamp of the latest ping's start time
|
|
||||||
* @type {number}
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
this._pingTimestamp = 0;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Timeouts set by {@link Client#setTimeout} that are still active
|
* Timeouts set by {@link Client#setTimeout} that are still active
|
||||||
* @type {Set<Timeout>}
|
* @type {Set<Timeout>}
|
||||||
|
|
@ -16709,6 +16831,15 @@ class Client extends EventEmitter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Timestamp of the latest ping's start time
|
||||||
|
* @type {number}
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
get _pingTimestamp() {
|
||||||
|
return this.ws.connection ? this.ws.connection.pingSendTime : 0;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Current status of the client's connection to Discord
|
* Current status of the client's connection to Discord
|
||||||
* @type {?number}
|
* @type {?number}
|
||||||
|
|
@ -22337,6 +22468,14 @@ class ClientManager {
|
||||||
this.heartbeatInterval = null;
|
this.heartbeatInterval = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The status of the client
|
||||||
|
* @type {number}
|
||||||
|
*/
|
||||||
|
get status() {
|
||||||
|
return this.connection ? this.connection.status : Constants.Status.IDLE;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Connects the Client to the WebSocket
|
* Connects the Client to the WebSocket
|
||||||
* @param {string} token The authorization token
|
* @param {string} token The authorization token
|
||||||
|
|
@ -22364,15 +22503,6 @@ class ClientManager {
|
||||||
}, reject);
|
}, reject);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets up a keep-alive interval to keep the Client's connection valid
|
|
||||||
* @param {number} time The interval in milliseconds at which heartbeat packets should be sent
|
|
||||||
*/
|
|
||||||
setupKeepAlive(time) {
|
|
||||||
this.heartbeatInterval = time;
|
|
||||||
this.client.setInterval(() => this.client.ws.heartbeat(true), time);
|
|
||||||
}
|
|
||||||
|
|
||||||
destroy() {
|
destroy() {
|
||||||
this.client.ws.destroy();
|
this.client.ws.destroy();
|
||||||
this.client.rest.destroy();
|
this.client.rest.destroy();
|
||||||
|
|
@ -24685,7 +24815,7 @@ class WebSocketManager extends EventEmitter {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Emits a debug event
|
* Emits a debug event
|
||||||
* @param {string} message the debug message
|
* @param {string} message The debug message
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
debug(message) {
|
debug(message) {
|
||||||
|
|
@ -24703,7 +24833,7 @@ class WebSocketManager extends EventEmitter {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a packet on the available WebSocket
|
* Send a packet on the available WebSocket
|
||||||
* @param {Object} packet the packet to send
|
* @param {Object} packet The packet to send
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
send(packet) {
|
send(packet) {
|
||||||
|
|
@ -24713,7 +24843,7 @@ class WebSocketManager extends EventEmitter {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Connects the client to a gateway
|
* Connects the client to a gateway
|
||||||
* @param {string} gateway the gateway to connect to
|
* @param {string} gateway The gateway to connect to
|
||||||
* @returns {boolean}
|
* @returns {boolean}
|
||||||
*/
|
*/
|
||||||
connect(gateway) {
|
connect(gateway) {
|
||||||
|
|
|
||||||
8
discord.wsrewrite.min.js
vendored
8
discord.wsrewrite.min.js
vendored
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue