blob: 1b00e361c4b72f4bfd84a43a59b81296a441229a (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
var erlpack: any;
try {
erlpack = require("erlpack");
} catch (error) {}
import { Payload } from "@fosscord/gateway/util/Constants";
import WebSocket from "./WebSocket";
export async function Send(socket: WebSocket, data: Payload) {
let buffer: Buffer | string;
if (socket.encoding === "etf") buffer = erlpack.pack(data);
// TODO: encode circular object
else if (socket.encoding === "json") buffer = JSON.stringify(data);
else return;
// TODO: compression
if (socket.deflate) {
socket.deflate.write(buffer);
socket.deflate.flush();
return;
}
return new Promise((res, rej) => {
socket.send(buffer, (err) => {
if (err) return rej(err);
return res(null);
});
});
}
|