cloudstorm 0.4.0 → 0.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -4
- package/dist/Client.js +6 -16
- package/dist/Intents.d.ts +2 -0
- package/dist/Intents.js +3 -1
- package/dist/Shard.d.ts +0 -1
- package/dist/Shard.js +2 -9
- package/dist/ShardManager.js +12 -30
- package/dist/Types.d.ts +4 -31
- package/dist/connector/DiscordConnector.d.ts +5 -4
- package/dist/connector/DiscordConnector.js +67 -93
- package/dist/structures/BetterWs.d.ts +17 -54
- package/dist/structures/BetterWs.js +504 -154
- package/dist/structures/RatelimitBucket.js +3 -7
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +9 -14
|
@@ -2,193 +2,543 @@
|
|
|
2
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
|
+
// This ultra light weigth WS code is a slimmed down version originally found at https://github.com/timotejroiko/tiny-discord
|
|
6
|
+
// Modifications and use of this code was granted for this project by the author, Timotej Roiko.
|
|
7
|
+
// A major thank you to Tim for better performing software.
|
|
5
8
|
const events_1 = require("events");
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
catch (e) {
|
|
12
|
-
Erlpack = null;
|
|
13
|
-
}
|
|
9
|
+
const crypto_1 = require("crypto");
|
|
10
|
+
const zlib_1 = require("zlib");
|
|
11
|
+
const https_1 = require("https");
|
|
12
|
+
const util_1 = __importDefault(require("util"));
|
|
13
|
+
const Z_SYNC_FLUSH = zlib_1.constants.Z_SYNC_FLUSH;
|
|
14
14
|
const Constants_1 = require("../Constants");
|
|
15
|
-
const ws_1 = __importDefault(require("ws"));
|
|
16
15
|
const RatelimitBucket_1 = __importDefault(require("./RatelimitBucket"));
|
|
17
16
|
/**
|
|
18
17
|
* Helper Class for simplifying the websocket connection to Discord.
|
|
19
18
|
*/
|
|
20
19
|
class BetterWs extends events_1.EventEmitter {
|
|
21
|
-
|
|
22
|
-
* Create a new BetterWs instance.
|
|
23
|
-
*/
|
|
24
|
-
constructor(address, options = {}) {
|
|
20
|
+
constructor(address, options) {
|
|
25
21
|
super();
|
|
26
|
-
this.zlibInflate = null;
|
|
27
|
-
this.ws = new ws_1.default(address, options.socket);
|
|
28
|
-
this.bindWs(this.ws);
|
|
29
22
|
this.wsBucket = new RatelimitBucket_1.default(120, 60000);
|
|
30
|
-
this.presenceBucket = new RatelimitBucket_1.default(5,
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
else
|
|
36
|
-
this.compress = false;
|
|
23
|
+
this.presenceBucket = new RatelimitBucket_1.default(5, 60000);
|
|
24
|
+
this._connecting = false;
|
|
25
|
+
this.encoding = options.encoding === "etf" ? "etf" : "json";
|
|
26
|
+
this.compress = options.compress || false;
|
|
27
|
+
this.address = address;
|
|
37
28
|
this.options = options;
|
|
29
|
+
this._socket = null;
|
|
30
|
+
this._internal = {
|
|
31
|
+
closePromise: null,
|
|
32
|
+
zlib: null,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
get status() {
|
|
36
|
+
const internal = this._internal;
|
|
37
|
+
if (this._connecting)
|
|
38
|
+
return 2;
|
|
39
|
+
if (internal.closePromise)
|
|
40
|
+
return 3; // closing
|
|
41
|
+
if (!this._socket)
|
|
42
|
+
return 4; // closed
|
|
43
|
+
return 1; // connected
|
|
38
44
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
45
|
+
connect() {
|
|
46
|
+
if (this._socket)
|
|
47
|
+
return Promise.resolve(void 0);
|
|
48
|
+
const key = (0, crypto_1.randomBytes)(16).toString("base64");
|
|
49
|
+
this.emit("debug", "Creating connection");
|
|
50
|
+
const url = new URL(this.address);
|
|
51
|
+
const req = (0, https_1.request)({
|
|
52
|
+
hostname: url.hostname,
|
|
53
|
+
path: `${url.pathname}${url.search}`,
|
|
54
|
+
headers: {
|
|
55
|
+
"Connection": "Upgrade",
|
|
56
|
+
"Upgrade": "websocket",
|
|
57
|
+
"Sec-WebSocket-Key": key,
|
|
58
|
+
"Sec-WebSocket-Version": "13",
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
this._connecting = true;
|
|
62
|
+
return new Promise((resolve, reject) => {
|
|
63
|
+
req.on("upgrade", (res, socket) => {
|
|
64
|
+
const hash = (0, crypto_1.createHash)("sha1").update(key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").digest("base64");
|
|
65
|
+
const accept = res.headers["sec-websocket-accept"];
|
|
66
|
+
if (hash !== accept) {
|
|
67
|
+
socket.end(() => {
|
|
68
|
+
this.emit("debug", "Failed websocket-key validation");
|
|
69
|
+
this._connecting = false;
|
|
70
|
+
reject(new Error(`Invalid Sec-Websocket-Accept | expected: ${hash} | received: ${accept}`));
|
|
71
|
+
});
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
socket.on("error", this._onError.bind(this));
|
|
75
|
+
socket.on("close", this._onClose.bind(this));
|
|
76
|
+
socket.on("readable", this._onReadable.bind(this));
|
|
77
|
+
this._socket = socket;
|
|
78
|
+
this._connecting = false;
|
|
79
|
+
if (this.compress) {
|
|
80
|
+
const z = (0, zlib_1.createInflate)();
|
|
81
|
+
// @ts-ignore
|
|
82
|
+
z._c = z.close;
|
|
83
|
+
// @ts-ignore
|
|
84
|
+
z._h = z._handle;
|
|
85
|
+
// @ts-ignore
|
|
86
|
+
z._hc = z._handle.close;
|
|
87
|
+
// @ts-ignore
|
|
88
|
+
z._v = () => void 0;
|
|
89
|
+
this._internal.zlib = z;
|
|
90
|
+
}
|
|
91
|
+
this.emit("ws_open");
|
|
92
|
+
resolve(void 0);
|
|
93
|
+
});
|
|
94
|
+
req.on("error", e => {
|
|
95
|
+
this._connecting = false;
|
|
96
|
+
reject(e);
|
|
97
|
+
});
|
|
98
|
+
req.end();
|
|
99
|
+
});
|
|
44
100
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
101
|
+
async close(code, reason) {
|
|
102
|
+
const internal = this._internal;
|
|
103
|
+
if (internal.closePromise)
|
|
104
|
+
return internal.closePromise;
|
|
105
|
+
if (!this._socket)
|
|
106
|
+
return Promise.resolve(void 0);
|
|
107
|
+
let resolver;
|
|
108
|
+
const promise = new Promise(resolve => {
|
|
109
|
+
resolver = resolve;
|
|
110
|
+
const from = Buffer.from([code >> 8, code & 255]);
|
|
111
|
+
this._write(reason ? Buffer.concat([from, Buffer.from(reason)]) : from, 8);
|
|
112
|
+
}).then(() => {
|
|
113
|
+
internal.closePromise = null;
|
|
52
114
|
});
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
115
|
+
// @ts-ignore
|
|
116
|
+
promise.resolve = resolver;
|
|
117
|
+
internal.closePromise = promise;
|
|
118
|
+
}
|
|
119
|
+
sendMessage(data) {
|
|
120
|
+
if (!isValidRequest(data))
|
|
121
|
+
return Promise.reject(new Error("Invalid request"));
|
|
122
|
+
return new Promise(res => {
|
|
123
|
+
const presence = data.op === Constants_1.GATEWAY_OP_CODES.PRESENCE_UPDATE;
|
|
124
|
+
const sendMsg = () => {
|
|
125
|
+
this.wsBucket.queue(() => {
|
|
126
|
+
this.emit("debug_send", data);
|
|
127
|
+
if (this.encoding === "json")
|
|
128
|
+
this._write(Buffer.from(JSON.stringify(data)), 1);
|
|
129
|
+
else {
|
|
130
|
+
const etf = writeETF(data);
|
|
131
|
+
this._write(etf, 2);
|
|
132
|
+
}
|
|
133
|
+
res(void 0);
|
|
134
|
+
});
|
|
135
|
+
};
|
|
136
|
+
if (presence)
|
|
137
|
+
this.presenceBucket.queue(sendMsg);
|
|
138
|
+
else
|
|
139
|
+
sendMsg();
|
|
56
140
|
});
|
|
57
|
-
ws.on("open", () => this.onOpen());
|
|
58
141
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
142
|
+
_write(packet, opcode) {
|
|
143
|
+
const socket = this._socket;
|
|
144
|
+
if (!socket || !socket.writable)
|
|
145
|
+
return;
|
|
146
|
+
const length = packet.length;
|
|
147
|
+
let frame;
|
|
148
|
+
if (length < 126) {
|
|
149
|
+
frame = Buffer.allocUnsafe(6 + length);
|
|
150
|
+
frame[1] = 128 + length;
|
|
151
|
+
}
|
|
152
|
+
else if (length < (1 << 16)) {
|
|
153
|
+
frame = Buffer.allocUnsafe(8 + length);
|
|
154
|
+
frame[1] = 254;
|
|
155
|
+
frame[2] = length >> 8;
|
|
156
|
+
frame[3] = length & 255;
|
|
69
157
|
}
|
|
70
158
|
else {
|
|
71
|
-
|
|
72
|
-
|
|
159
|
+
frame = Buffer.allocUnsafe(14 + length);
|
|
160
|
+
frame[1] = 255;
|
|
161
|
+
frame.writeBigUInt64BE(BigInt(length), 2);
|
|
73
162
|
}
|
|
74
|
-
|
|
75
|
-
|
|
163
|
+
frame[0] = 128 + opcode;
|
|
164
|
+
frame.writeUInt32BE(0, frame.length - length - 4);
|
|
165
|
+
frame.set(packet, frame.length - length);
|
|
166
|
+
socket.write(frame);
|
|
167
|
+
}
|
|
168
|
+
_onError(error) {
|
|
169
|
+
if (!this._socket)
|
|
170
|
+
return;
|
|
171
|
+
this.emit("debug", util_1.default.inspect(error, true, 1, false));
|
|
172
|
+
this._write(Buffer.allocUnsafe(0), 8);
|
|
173
|
+
}
|
|
174
|
+
_onClose() {
|
|
175
|
+
const socket = this._socket;
|
|
176
|
+
const internal = this._internal;
|
|
177
|
+
if (!socket)
|
|
178
|
+
return;
|
|
179
|
+
this.emit("debug", "Connection closed");
|
|
180
|
+
socket.removeListener("data", this._onReadable);
|
|
181
|
+
socket.removeListener("error", this._onError);
|
|
182
|
+
socket.removeListener("close", this._onClose);
|
|
76
183
|
this.wsBucket.dropQueue();
|
|
77
184
|
this.presenceBucket.dropQueue();
|
|
78
|
-
this.
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
185
|
+
this._socket = null;
|
|
186
|
+
if (internal.zlib) {
|
|
187
|
+
internal.zlib.close();
|
|
188
|
+
internal.zlib = null;
|
|
189
|
+
}
|
|
190
|
+
if (internal.closePromise) {
|
|
191
|
+
// @ts-ignore
|
|
192
|
+
internal.closePromise.resolve(void 0);
|
|
193
|
+
}
|
|
87
194
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
let msg;
|
|
97
|
-
if (this.compress && this.zlibInflate) {
|
|
98
|
-
const length = message.length;
|
|
99
|
-
const flush = length >= 4 &&
|
|
100
|
-
message[length - 4] === 0x00 &&
|
|
101
|
-
message[length - 3] === 0x00 &&
|
|
102
|
-
message[length - 2] === 0xFF &&
|
|
103
|
-
message[length - 1] === 0xFF;
|
|
104
|
-
this.zlibInflate.push(message, flush ? zlib_sync_1.default.Z_SYNC_FLUSH : false);
|
|
105
|
-
if (!flush)
|
|
195
|
+
_onReadable() {
|
|
196
|
+
const socket = this._socket;
|
|
197
|
+
while (socket.readableLength > 1) {
|
|
198
|
+
let length = readRange(socket, 1, 1) & 127;
|
|
199
|
+
let bytes = 0;
|
|
200
|
+
if (length > 125) {
|
|
201
|
+
bytes = length === 126 ? 2 : 8;
|
|
202
|
+
if (socket.readableLength < 2 + bytes)
|
|
106
203
|
return;
|
|
107
|
-
|
|
204
|
+
length = readRange(socket, 2, bytes);
|
|
108
205
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
206
|
+
const frame = socket.read(2 + bytes + length);
|
|
207
|
+
if (!frame)
|
|
208
|
+
return;
|
|
209
|
+
const fin = frame[0] >> 7;
|
|
210
|
+
const opcode = frame[0] & 15;
|
|
211
|
+
if (fin !== 1 || opcode === 0)
|
|
212
|
+
this.emit("debug", "discord actually does send messages with fin=0. if you see this error let me know");
|
|
213
|
+
const payload = frame.slice(2 + bytes);
|
|
214
|
+
this._processFrame(opcode, payload);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
_processFrame(opcode, message) {
|
|
218
|
+
const internal = this._internal;
|
|
219
|
+
switch (opcode) {
|
|
220
|
+
case 1: {
|
|
221
|
+
const packet = JSON.parse(message.toString());
|
|
222
|
+
this.emit("ws_message", packet);
|
|
223
|
+
break;
|
|
113
224
|
}
|
|
114
|
-
|
|
115
|
-
|
|
225
|
+
case 2: {
|
|
226
|
+
let packet;
|
|
227
|
+
if (this.compress) {
|
|
228
|
+
const z = internal.zlib;
|
|
229
|
+
let error = null;
|
|
230
|
+
let data = null;
|
|
231
|
+
// @ts-ignore
|
|
232
|
+
z.close = z._handle.close = z._v;
|
|
233
|
+
try {
|
|
234
|
+
// @ts-ignore
|
|
235
|
+
data = z._processChunk(message, Z_SYNC_FLUSH);
|
|
236
|
+
}
|
|
237
|
+
catch (e) {
|
|
238
|
+
error = e;
|
|
239
|
+
}
|
|
240
|
+
const l = message.length;
|
|
241
|
+
if (message[l - 4] !== 0 || message[l - 3] !== 0 || message[l - 2] !== 255 || message[l - 1] !== 255)
|
|
242
|
+
this.emit("debug", "discord actually does send fragmented zlib messages. if you see this error let me know");
|
|
243
|
+
// @ts-ignore
|
|
244
|
+
z.close = z._c;
|
|
245
|
+
// @ts-ignore
|
|
246
|
+
z._handle = z._h;
|
|
247
|
+
// @ts-ignore
|
|
248
|
+
z._handle.close = z._hc;
|
|
249
|
+
// @ts-ignore
|
|
250
|
+
z._events.error = void 0;
|
|
251
|
+
// @ts-ignore
|
|
252
|
+
z._eventCount--;
|
|
253
|
+
z.removeAllListeners("error");
|
|
254
|
+
if (error) {
|
|
255
|
+
this.emit("debug", "Zlib error");
|
|
256
|
+
this._write(Buffer.allocUnsafe(0), 8);
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
if (!data)
|
|
260
|
+
return; // This should never run, but TS is lame
|
|
261
|
+
packet = this.encoding === "json" ? JSON.parse(String(data)) : readETF(data, 1);
|
|
262
|
+
}
|
|
263
|
+
else if (this.encoding === "json") {
|
|
264
|
+
const data = (0, zlib_1.inflateSync)(message);
|
|
265
|
+
packet = JSON.parse(data.toString());
|
|
266
|
+
}
|
|
267
|
+
else
|
|
268
|
+
packet = readETF(message, 1);
|
|
269
|
+
this.emit("ws_message", packet);
|
|
270
|
+
break;
|
|
271
|
+
}
|
|
272
|
+
case 8: {
|
|
273
|
+
const code = message.length > 1 ? (message[0] << 8) + message[1] : 0;
|
|
274
|
+
const reason = message.length > 2 ? message.slice(2).toString() : "";
|
|
275
|
+
this.emit("ws_close", code, reason);
|
|
276
|
+
this._write(Buffer.from([code >> 8, code & 255]), 8);
|
|
277
|
+
break;
|
|
278
|
+
}
|
|
279
|
+
case 9: {
|
|
280
|
+
this._write(message, 10);
|
|
281
|
+
break;
|
|
116
282
|
}
|
|
117
283
|
}
|
|
118
|
-
catch (e) {
|
|
119
|
-
this.emit("error", `Message: ${message} was not parseable`);
|
|
120
|
-
return;
|
|
121
|
-
}
|
|
122
|
-
this.emit("ws_message", parsed);
|
|
123
284
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
285
|
+
}
|
|
286
|
+
function isValidRequest(value) {
|
|
287
|
+
return value && typeof value === "object" && Number.isInteger(value.op) && typeof value.d !== "undefined";
|
|
288
|
+
}
|
|
289
|
+
function readRange(socket, index, bytes) {
|
|
290
|
+
// @ts-ignore
|
|
291
|
+
let head = socket._readableState.buffer.head;
|
|
292
|
+
let cursor = 0;
|
|
293
|
+
let read = 0;
|
|
294
|
+
let num = 0;
|
|
295
|
+
do {
|
|
296
|
+
for (let i = 0; i < head.data.length; i++) {
|
|
297
|
+
if (++cursor > index) {
|
|
298
|
+
num *= 256;
|
|
299
|
+
num += head.data[i];
|
|
300
|
+
if (++read === bytes)
|
|
301
|
+
return num;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
} while ((head = head.next));
|
|
305
|
+
throw new Error("readRange failed?");
|
|
306
|
+
}
|
|
307
|
+
function readETF(data, start) {
|
|
308
|
+
let view;
|
|
309
|
+
let x = start;
|
|
310
|
+
const loop = () => {
|
|
311
|
+
const type = data[x++];
|
|
312
|
+
switch (type) {
|
|
313
|
+
case 97: {
|
|
314
|
+
return data[x++];
|
|
315
|
+
}
|
|
316
|
+
case 98: {
|
|
317
|
+
const int = data.readInt32BE(x);
|
|
318
|
+
x += 4;
|
|
319
|
+
return int;
|
|
320
|
+
}
|
|
321
|
+
case 100: {
|
|
322
|
+
const length = data.readUInt16BE(x);
|
|
323
|
+
let atom = "";
|
|
324
|
+
if (length > 30) {
|
|
325
|
+
// @ts-ignore
|
|
326
|
+
atom = data.latin1Slice(x += 2, x + length);
|
|
145
327
|
}
|
|
146
328
|
else {
|
|
147
|
-
|
|
329
|
+
for (let i = x += 2; i < x + length; i++) {
|
|
330
|
+
atom += String.fromCharCode(data[i]);
|
|
331
|
+
}
|
|
148
332
|
}
|
|
333
|
+
x += length;
|
|
334
|
+
if (!atom)
|
|
335
|
+
return undefined;
|
|
336
|
+
if (atom === "nil" || atom === "null")
|
|
337
|
+
return null;
|
|
338
|
+
if (atom === "true")
|
|
339
|
+
return true;
|
|
340
|
+
if (atom === "false")
|
|
341
|
+
return false;
|
|
342
|
+
return atom;
|
|
149
343
|
}
|
|
150
|
-
|
|
151
|
-
|
|
344
|
+
case 108:
|
|
345
|
+
case 106: {
|
|
346
|
+
const array = [];
|
|
347
|
+
if (type === 108) {
|
|
348
|
+
const length = data.readUInt32BE(x);
|
|
349
|
+
x += 4;
|
|
350
|
+
for (let i = 0; i < length; i++) {
|
|
351
|
+
array.push(loop());
|
|
352
|
+
}
|
|
353
|
+
x++;
|
|
354
|
+
}
|
|
355
|
+
return array;
|
|
152
356
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
});
|
|
162
|
-
});
|
|
163
|
-
};
|
|
164
|
-
if (presence) {
|
|
165
|
-
// same here
|
|
166
|
-
this.presenceBucket.queue(sendMsg);
|
|
357
|
+
case 107: {
|
|
358
|
+
const array = [];
|
|
359
|
+
const length = data.readUInt16BE(x);
|
|
360
|
+
x += 2;
|
|
361
|
+
for (let i = 0; i < length; i++) {
|
|
362
|
+
array.push(data[x++]);
|
|
363
|
+
}
|
|
364
|
+
return array;
|
|
167
365
|
}
|
|
168
|
-
|
|
169
|
-
|
|
366
|
+
case 109: {
|
|
367
|
+
const length = data.readUInt32BE(x);
|
|
368
|
+
let str = "";
|
|
369
|
+
if (length > 30) {
|
|
370
|
+
// @ts-ignore
|
|
371
|
+
str = data.utf8Slice(x += 4, x + length);
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
let i = x += 4;
|
|
375
|
+
const l = x + length;
|
|
376
|
+
while (i < l) {
|
|
377
|
+
const byte = data[i++];
|
|
378
|
+
if (byte < 128)
|
|
379
|
+
str += String.fromCharCode(byte);
|
|
380
|
+
else if (byte < 224)
|
|
381
|
+
str += String.fromCharCode(((byte & 31) << 6) + (data[i++] & 63));
|
|
382
|
+
else if (byte < 240)
|
|
383
|
+
str += String.fromCharCode(((byte & 15) << 12) + ((data[i++] & 63) << 6) + (data[i++] & 63));
|
|
384
|
+
else
|
|
385
|
+
str += String.fromCodePoint(((byte & 7) << 18) + ((data[i++] & 63) << 12) + ((data[i++] & 63) << 6) + (data[i++] & 63));
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
x += length;
|
|
389
|
+
return str;
|
|
170
390
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
391
|
+
case 110: {
|
|
392
|
+
// @ts-ignore
|
|
393
|
+
if (!view)
|
|
394
|
+
view = new DataView(data.buffer, data.offset, data.byteLength);
|
|
395
|
+
const length = data[x++];
|
|
396
|
+
const sign = data[x++];
|
|
397
|
+
let left = length;
|
|
398
|
+
let num = BigInt(0);
|
|
399
|
+
while (left > 0) {
|
|
400
|
+
if (left >= 8) {
|
|
401
|
+
num <<= BigInt(64);
|
|
402
|
+
num += view.getBigUint64(x + (left -= 8), true);
|
|
403
|
+
}
|
|
404
|
+
else if (left >= 4) {
|
|
405
|
+
num <<= BigInt(32);
|
|
406
|
+
// @ts-ignore
|
|
407
|
+
num += BigInt(view.getUint32(x + (left -= 4)), true);
|
|
408
|
+
}
|
|
409
|
+
else if (left >= 2) {
|
|
410
|
+
num <<= BigInt(16);
|
|
411
|
+
// @ts-ignore
|
|
412
|
+
num += BigInt(view.getUint16(x + (left -= 2)), true);
|
|
413
|
+
}
|
|
414
|
+
else {
|
|
415
|
+
num <<= BigInt(8);
|
|
416
|
+
num += BigInt(data[x]);
|
|
417
|
+
left--;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
x += length;
|
|
421
|
+
return (sign ? -num : num).toString();
|
|
422
|
+
}
|
|
423
|
+
case 116: {
|
|
424
|
+
const obj = {};
|
|
425
|
+
const length = data.readUInt32BE(x);
|
|
426
|
+
x += 4;
|
|
427
|
+
for (let i = 0; i < length; i++) {
|
|
428
|
+
const key = loop();
|
|
429
|
+
// @ts-ignore
|
|
430
|
+
obj[key] = loop();
|
|
431
|
+
}
|
|
432
|
+
return obj;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
throw new Error(`Missing etf type: ${type}`);
|
|
436
|
+
};
|
|
437
|
+
return loop();
|
|
438
|
+
}
|
|
439
|
+
function writeETF(data) {
|
|
440
|
+
const b = Buffer.allocUnsafe(1 << 12);
|
|
441
|
+
b[0] = 131;
|
|
442
|
+
let i = 1;
|
|
443
|
+
const loop = (obj) => {
|
|
444
|
+
const type = typeof obj;
|
|
445
|
+
switch (type) {
|
|
446
|
+
case "boolean": {
|
|
447
|
+
b[i++] = 100;
|
|
448
|
+
if (obj) {
|
|
449
|
+
b.writeUInt16BE(4, i);
|
|
450
|
+
// @ts-ignore
|
|
451
|
+
b.latin1Write("true", i += 2);
|
|
452
|
+
i += 4;
|
|
453
|
+
}
|
|
454
|
+
else {
|
|
455
|
+
b.writeUInt16BE(5, i);
|
|
456
|
+
// @ts-ignore
|
|
457
|
+
b.latin1Write("false", i += 2);
|
|
458
|
+
i += 5;
|
|
459
|
+
}
|
|
460
|
+
break;
|
|
461
|
+
}
|
|
462
|
+
case "string": {
|
|
463
|
+
const length = Buffer.byteLength(obj);
|
|
464
|
+
b[i++] = 109;
|
|
465
|
+
b.writeUInt32BE(length, i);
|
|
466
|
+
// @ts-ignore
|
|
467
|
+
b.utf8Write(obj, i += 4);
|
|
468
|
+
i += length;
|
|
469
|
+
break;
|
|
470
|
+
}
|
|
471
|
+
case "number": {
|
|
472
|
+
if (Number.isInteger(obj)) {
|
|
473
|
+
const abs = Math.abs(obj);
|
|
474
|
+
if (abs < 2147483648) {
|
|
475
|
+
b[i++] = 98;
|
|
476
|
+
b.writeInt32BE(obj, i);
|
|
477
|
+
i += 4;
|
|
478
|
+
}
|
|
479
|
+
else if (abs < Number.MAX_SAFE_INTEGER) {
|
|
480
|
+
b[i++] = 110;
|
|
481
|
+
b[i++] = 8;
|
|
482
|
+
b[i++] = Number(obj < 0);
|
|
483
|
+
b.writeBigUInt64LE(BigInt(abs), i);
|
|
484
|
+
i += 8;
|
|
485
|
+
break;
|
|
486
|
+
}
|
|
487
|
+
else {
|
|
488
|
+
b[i++] = 70;
|
|
489
|
+
b.writeDoubleBE(obj, i);
|
|
490
|
+
i += 8;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
else {
|
|
494
|
+
b[i++] = 70;
|
|
495
|
+
b.writeDoubleBE(obj, i);
|
|
496
|
+
i += 8;
|
|
497
|
+
}
|
|
498
|
+
break;
|
|
499
|
+
}
|
|
500
|
+
case "bigint": {
|
|
501
|
+
b[i++] = 110;
|
|
502
|
+
b[i++] = 8;
|
|
503
|
+
b[i++] = Number(obj < 0);
|
|
504
|
+
b.writeBigUInt64LE(obj, i);
|
|
505
|
+
i += 8;
|
|
506
|
+
break;
|
|
507
|
+
}
|
|
508
|
+
case "object": {
|
|
509
|
+
if (obj === null) {
|
|
510
|
+
b[i++] = 100;
|
|
511
|
+
b.writeUInt16BE(3, i);
|
|
512
|
+
// @ts-ignore
|
|
513
|
+
b.latin1Write("nil", i += 2);
|
|
514
|
+
i += 3;
|
|
515
|
+
}
|
|
516
|
+
else if (Array.isArray(obj)) {
|
|
517
|
+
if (obj.length) {
|
|
518
|
+
b[i++] = 108;
|
|
519
|
+
b.writeUInt32BE(obj.length, i);
|
|
520
|
+
i += 4;
|
|
521
|
+
for (const item of obj) {
|
|
522
|
+
loop(item);
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
b[i++] = 106;
|
|
526
|
+
}
|
|
527
|
+
else {
|
|
528
|
+
const entries = Object.entries(obj).filter(x => typeof x[1] !== "undefined");
|
|
529
|
+
b[i++] = 116;
|
|
530
|
+
b.writeUInt32BE(entries.length, i);
|
|
531
|
+
i += 4;
|
|
532
|
+
for (const [key, value] of entries) {
|
|
533
|
+
loop(key);
|
|
534
|
+
loop(value);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
break;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
};
|
|
541
|
+
loop(data);
|
|
542
|
+
return Buffer.from(b.slice(0, i));
|
|
192
543
|
}
|
|
193
|
-
BetterWs.default = BetterWs;
|
|
194
544
|
module.exports = BetterWs;
|