summary refs log tree commit diff
path: root/src/util/Send.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/Send.ts')
-rw-r--r--src/util/Send.ts21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/util/Send.ts b/src/util/Send.ts
new file mode 100644

index 00000000..d38865b3 --- /dev/null +++ b/src/util/Send.ts
@@ -0,0 +1,21 @@ +import erlpack from "erlpack"; +import { promisify } from "util"; +import { Payload } from "../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); + + // TODO: compression + + return new Promise((res, rej) => { + socket.send(buffer, (err) => { + if (err) return rej(err); + return res(null); + }); + }); +}