mirror of
https://github.com/danbulant/discord.js
synced 2026-05-27 14:02:02 +00:00
fix websocket unpacking (#3301)
This commit is contained in:
parent
db56e0cbae
commit
065908956b
2 changed files with 13 additions and 7 deletions
|
|
@ -6,9 +6,13 @@ try {
|
|||
if (!erlpack.pack) erlpack = null;
|
||||
} catch (err) {} // eslint-disable-line no-empty
|
||||
|
||||
let TextDecoder;
|
||||
|
||||
if (browser) {
|
||||
TextDecoder = window.TextDecoder; // eslint-disable-line no-undef
|
||||
exports.WebSocket = window.WebSocket; // eslint-disable-line no-undef
|
||||
} else {
|
||||
TextDecoder = require('util').TextDecoder;
|
||||
try {
|
||||
exports.WebSocket = require('@discordjs/uws');
|
||||
} catch (err) {
|
||||
|
|
@ -16,12 +20,19 @@ if (browser) {
|
|||
}
|
||||
}
|
||||
|
||||
const ab = new TextDecoder();
|
||||
|
||||
exports.encoding = erlpack ? 'etf' : 'json';
|
||||
|
||||
exports.pack = erlpack ? erlpack.pack : JSON.stringify;
|
||||
|
||||
exports.unpack = data => {
|
||||
if (!erlpack || data[0] === '{') return JSON.parse(data);
|
||||
if (exports.encoding === 'json') {
|
||||
if (typeof data !== 'string') {
|
||||
data = ab.decode(data);
|
||||
}
|
||||
return JSON.parse(data);
|
||||
}
|
||||
if (!Buffer.isBuffer(data)) data = Buffer.from(new Uint8Array(data));
|
||||
return erlpack.unpack(data);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -3,16 +3,12 @@
|
|||
const EventEmitter = require('events');
|
||||
const WebSocket = require('../../WebSocket');
|
||||
const { Status, Events, ShardEvents, OPCodes, WSEvents } = require('../../util/Constants');
|
||||
const { TextDecoder } = require('util');
|
||||
|
||||
let zstd;
|
||||
let decoder;
|
||||
|
||||
let zlib;
|
||||
|
||||
try {
|
||||
zstd = require('zucc');
|
||||
decoder = new TextDecoder('utf8');
|
||||
} catch (e) {
|
||||
try {
|
||||
zlib = require('zlib-sync');
|
||||
|
|
@ -259,8 +255,7 @@ class WebSocketShard extends EventEmitter {
|
|||
onMessage({ data }) {
|
||||
let raw;
|
||||
if (zstd) {
|
||||
const ab = this.inflate.decompress(new Uint8Array(data).buffer);
|
||||
raw = decoder.decode(ab);
|
||||
raw = this.inflate.decompress(new Uint8Array(data).buffer);
|
||||
} else {
|
||||
if (data instanceof ArrayBuffer) data = new Uint8Array(data);
|
||||
const l = data.length;
|
||||
|
|
|
|||
Loading…
Reference in a new issue