mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 00:53:31 +01:00
Separate websocket internals for RPC (#1893)
* websocket centralization * more centralization * whoops * Update WebSocket.js
This commit is contained in:
@@ -1,26 +1,7 @@
|
||||
const browser = typeof window !== 'undefined';
|
||||
const EventEmitter = require('events');
|
||||
const Constants = require('../../util/Constants');
|
||||
const zlib = require('zlib');
|
||||
const PacketManager = require('./packets/WebSocketPacketManager');
|
||||
const erlpack = (function findErlpack() {
|
||||
try {
|
||||
const e = require('erlpack');
|
||||
if (!e.pack) return null;
|
||||
return e;
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}());
|
||||
|
||||
const WebSocket = (function findWebSocket() {
|
||||
if (browser) return window.WebSocket; // eslint-disable-line no-undef
|
||||
try {
|
||||
return require('uws');
|
||||
} catch (e) {
|
||||
return require('ws');
|
||||
}
|
||||
}());
|
||||
const WebSocket = require('../../WebSocket');
|
||||
|
||||
/**
|
||||
* Abstracts a WebSocket connection with decoding/encoding for the Discord gateway.
|
||||
@@ -161,30 +142,6 @@ class WebSocketConnection extends EventEmitter {
|
||||
return this.manager.debug(`[connection] ${message}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to serialise data from the WebSocket.
|
||||
* @param {string|Object} data Data to unpack
|
||||
* @returns {Object}
|
||||
*/
|
||||
unpack(data) {
|
||||
if (Array.isArray(data)) data = Buffer.concat(data);
|
||||
if (data instanceof ArrayBuffer) data = Buffer.from(new Uint8Array(data));
|
||||
|
||||
if (erlpack && typeof data !== 'string') return erlpack.unpack(data);
|
||||
else if (data instanceof Buffer) data = zlib.inflateSync(data).toString();
|
||||
|
||||
return JSON.parse(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Packs an object ready to be sent.
|
||||
* @param {Object} data Data to pack
|
||||
* @returns {string|Buffer}
|
||||
*/
|
||||
pack(data) {
|
||||
return erlpack ? erlpack.pack(data) : JSON.stringify(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes the current WebSocket queue.
|
||||
*/
|
||||
@@ -215,7 +172,7 @@ class WebSocketConnection extends EventEmitter {
|
||||
this.debug(`Tried to send packet ${data} but no WebSocket is available!`);
|
||||
return;
|
||||
}
|
||||
this.ws.send(this.pack(data));
|
||||
this.ws.send(WebSocket.pack(data));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -251,8 +208,7 @@ class WebSocketConnection extends EventEmitter {
|
||||
this.expectingClose = false;
|
||||
this.gateway = gateway;
|
||||
this.debug(`Connecting to ${gateway}`);
|
||||
const ws = this.ws = new WebSocket(gateway);
|
||||
if (browser) ws.binaryType = 'arraybuffer';
|
||||
const ws = this.ws = WebSocket.create(gateway, { v: Constants.DefaultOptions.ws.version });
|
||||
ws.onmessage = this.onMessage.bind(this);
|
||||
ws.onopen = this.onOpen.bind(this);
|
||||
ws.onerror = this.onError.bind(this);
|
||||
@@ -289,7 +245,7 @@ class WebSocketConnection extends EventEmitter {
|
||||
onMessage(event) {
|
||||
let data;
|
||||
try {
|
||||
data = this.unpack(event.data);
|
||||
data = WebSocket.unpack(event.data);
|
||||
} catch (err) {
|
||||
this.emit('debug', err);
|
||||
}
|
||||
@@ -497,11 +453,4 @@ class WebSocketConnection extends EventEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Encoding the WebSocket connections will use.
|
||||
* @type {string}
|
||||
*/
|
||||
WebSocketConnection.ENCODING = erlpack ? 'etf' : 'json';
|
||||
WebSocketConnection.WebSocket = WebSocket;
|
||||
|
||||
module.exports = WebSocketConnection;
|
||||
|
||||
Reference in New Issue
Block a user