From a221ef715a282993e91674b927b3d406a9a4121e Mon Sep 17 00:00:00 2001 From: Flummi Date: Thu, 13 Feb 2020 12:35:39 +0100 Subject: [PATCH] - slack: bugfix - update flumm-fetch-cookies - version bump to 1.0.5 --- package.json | 4 ++-- src/clients/slack.mjs | 35 +++++++++++++++++++---------------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index cb84019..f970b9b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cuffeo", - "version": "1.0.4", + "version": "1.0.5", "description": "A multi-protocol chatbot library with nearly zero dependencies.", "main": "src/index.mjs", "scripts": {}, @@ -17,7 +17,7 @@ "author": "Flummi & jkhsjdhjs", "license": "MIT", "dependencies": { - "flumm-fetch-cookies": "^1.1.1" + "flumm-fetch-cookies": "^1.3.4" }, "bugs": { "url": "https://github.com/kein-Bot/cuffeo/issues" diff --git a/src/clients/slack.mjs b/src/clients/slack.mjs index 42b8cbb..366e7fe 100644 --- a/src/clients/slack.mjs +++ b/src/clients/slack.mjs @@ -31,15 +31,15 @@ export default class slack extends EventEmitter { async connect() { const res = await (await fetch(`${this.api}/rtm.start?token=${this.token}`)).json(); + if (!res.ok) + return this.emit("data", [ "error", res.description ]); // more infos + res.channels.forEach(channel => this.server.channel.set(channel.id, channel.name)); res.users.forEach(user => this.server.user.set(user.id, { account: user.name, nickname: user.real_name })); - if (!res.ok) - return this.emit("data", [ "error", res.description ]); // more infos - this.server.wss.url = url.parse(res.url); https.get({ @@ -60,26 +60,26 @@ export default class slack extends EventEmitter { this.server.wss.socket.on("data", async data => { try { - if(data.length < 2) + if (data.length < 2) throw "payload too short"; - if(data[1] & 0x80) + if (data[1] & 0x80) throw "we no accept masked data"; let offset = 2; let length = data[1] & 0x7F; - if(length === 126) { + if (length === 126) { offset = 4; length = data.readUInt16BE(2); } else if(length === 127) throw "payload too long"; - if(data.length < length + offset) + if (data.length < length + offset) throw "payload shorter than given length"; //console.log(data, offset, length); data = JSON.parse(data.slice(offset, length + offset).toString().replace(/\0+$/, "")); // trim null bytes at the end //console.log(data, data.type); - if(data.type !== "message") + if (data.type !== "message") return false; await Promise.all([this.getChannel(data.channel), this.getUser(data.user)]); @@ -90,25 +90,25 @@ export default class slack extends EventEmitter { this.emit("data", [ "error", err ]); } }) - .on("end", () => { + .on("end", async () => { this.emit("data", [ "debug", "stream ended" ]); - this.reconnect(); + await this.reconnect(); }) - .on("error", err => { + .on("error", async err => { this.emit("data", [ "error", err ]); - this.reconnect(); + await this.reconnect(); }); }); } - reconnect() { + async reconnect() { this.server.wss.url = null; this.server.wss.socket = null; - this.connect(); + await this.connect(); } async getChannel(channelId) { - if(this.server.channel.has(channelId)) + if (this.server.channel.has(channelId)) return this.server.channel.get(channelId); const res = await (await fetch(`${this.api}/conversations.info?channel=${channelId}&token=${this.token}`)).json(); @@ -117,7 +117,7 @@ export default class slack extends EventEmitter { } async getUser(userId) { - if(this.server.user.has(userId)) + if (this.server.user.has(userId)) return this.server.user.get(userId); const res = await (await fetch(`${this.api}/users.info?user=${userId}&token=${this.token}`)).json(); @@ -167,6 +167,9 @@ export default class slack extends EventEmitter { if(frame_length > 6) frame.writeUInt16BE(payload.length, 2); + if (!this.server.wss.socket) + await this.reconnect(); + this.server.wss.socket.cork(); this.server.wss.socket.write(frame); this.server.wss.socket.write(Buffer.from(msg));