From 44219ebb5f77dcef60324a94018e5a3ae6cabc3c Mon Sep 17 00:00:00 2001 From: Jake Gealer Date: Fri, 29 Oct 2021 21:09:08 +0100 Subject: [PATCH 1/6] Add support for snap --- src/transports/ipc.js | 45 ++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/src/transports/ipc.js b/src/transports/ipc.js index f050a01..0e37103 100644 --- a/src/transports/ipc.js +++ b/src/transports/ipc.js @@ -13,33 +13,48 @@ const OPCodes = { PONG: 4, }; -function getIPCPath(id) { +function getIPCPath(id, snap) { if (process.platform === 'win32') { return `\\\\?\\pipe\\discord-ipc-${id}`; } const { env: { XDG_RUNTIME_DIR, TMPDIR, TMP, TEMP } } = process; const prefix = XDG_RUNTIME_DIR || TMPDIR || TMP || TEMP || '/tmp'; - return `${prefix.replace(/\/$/, '')}/discord-ipc-${id}`; + return `${prefix.replace(/\/$/, '')}${snap ? '/snap.discord' : ''}/discord-ipc-${id}`; } -function getIPC(id = 0) { - return new Promise((resolve, reject) => { - const path = getIPCPath(id); - const onerror = () => { - if (id < 10) { - resolve(getIPC(id + 1)); - } else { - reject(new Error('Could not connect')); - } - }; +function makeSocket(path) { + return new Promise((res, rej) => { const sock = net.createConnection(path, () => { - sock.removeListener('error', onerror); - resolve(sock); + sock.removeListener('error', rej); + res(sock); }); - sock.once('error', onerror); + sock.once('error', rej); }); } +async function getIPC() { + // Attempt a connection the usual way first. + const connect = async snap => { + for (let i = 0; i < 10; i++) { + try { + return await makeSocket(getIPCPath(i, snap)); + } catch (_) { + // Something went wrong with this connection. Go to the next iteration. + } + } + }; + let res = await connect(false); + if (res) return res; + + // Handle snap connections. + if (process.platform === "linux") { + if (res = await connect(true)) return res + } + + // If all else fails, throw an error. + throw new Error('Could not connect'); +} + async function findEndpoint(tries = 0) { if (tries > 30) { throw new Error('Could not find endpoint'); From 1f8a9d041944605dd3bf303397780166a8e48b3f Mon Sep 17 00:00:00 2001 From: Jake Gealer Date: Fri, 29 Oct 2021 21:10:58 +0100 Subject: [PATCH 2/6] missing semi-colon --- src/transports/ipc.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transports/ipc.js b/src/transports/ipc.js index 0e37103..6f22f8a 100644 --- a/src/transports/ipc.js +++ b/src/transports/ipc.js @@ -48,7 +48,7 @@ async function getIPC() { // Handle snap connections. if (process.platform === "linux") { - if (res = await connect(true)) return res + if (res = await connect(true)) return res; } // If all else fails, throw an error. From 2af95d98aed71f8ebb20512d86c9de45444f79b2 Mon Sep 17 00:00:00 2001 From: Jake Gealer Date: Fri, 29 Oct 2021 21:27:39 +0100 Subject: [PATCH 3/6] do not assign on one line --- src/transports/ipc.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/transports/ipc.js b/src/transports/ipc.js index 6f22f8a..325d01c 100644 --- a/src/transports/ipc.js +++ b/src/transports/ipc.js @@ -48,7 +48,8 @@ async function getIPC() { // Handle snap connections. if (process.platform === "linux") { - if (res = await connect(true)) return res; + res = await connect(true); + if (res) return res; } // If all else fails, throw an error. From 96771ae2b16f1754eccf4b06988b7d6f6e902c81 Mon Sep 17 00:00:00 2001 From: Jake Gealer Date: Fri, 29 Oct 2021 21:33:49 +0100 Subject: [PATCH 4/6] linting --- src/transports/ipc.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/transports/ipc.js b/src/transports/ipc.js index 325d01c..9ff5ac1 100644 --- a/src/transports/ipc.js +++ b/src/transports/ipc.js @@ -34,22 +34,28 @@ function makeSocket(path) { async function getIPC() { // Attempt a connection the usual way first. - const connect = async snap => { - for (let i = 0; i < 10; i++) { + const connect = async (snap) => { + for (let i = 0; i < 10; i += 1) { try { + // eslint-disable-next-line no-await-in-loop return await makeSocket(getIPCPath(i, snap)); } catch (_) { // Something went wrong with this connection. Go to the next iteration. } } + return undefined; }; let res = await connect(false); - if (res) return res; + if (res) { + return res; + } // Handle snap connections. - if (process.platform === "linux") { + if (process.platform === 'linux') { res = await connect(true); - if (res) return res; + if (res) { + return res; + } } // If all else fails, throw an error. From 2a43b2377b9f4ae01549428023c459fa16c952e0 Mon Sep 17 00:00:00 2001 From: Jake Gealer Date: Tue, 25 Jan 2022 19:48:20 +0000 Subject: [PATCH 5/6] Update src/transports/ipc.js Co-authored-by: Vlad Frangu --- src/transports/ipc.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transports/ipc.js b/src/transports/ipc.js index 9ff5ac1..2bf0c0d 100644 --- a/src/transports/ipc.js +++ b/src/transports/ipc.js @@ -39,7 +39,7 @@ async function getIPC() { try { // eslint-disable-next-line no-await-in-loop return await makeSocket(getIPCPath(i, snap)); - } catch (_) { + } catch { // Something went wrong with this connection. Go to the next iteration. } } From 417ddd53d9d26cad23123a2a359905a557150ade Mon Sep 17 00:00:00 2001 From: Jake Gealer Date: Tue, 25 Jan 2022 19:48:26 +0000 Subject: [PATCH 6/6] Update src/transports/ipc.js Co-authored-by: Vlad Frangu --- src/transports/ipc.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transports/ipc.js b/src/transports/ipc.js index 2bf0c0d..e545233 100644 --- a/src/transports/ipc.js +++ b/src/transports/ipc.js @@ -50,7 +50,7 @@ async function getIPC() { return res; } - // Handle snap connections. + // Check for snap connections if (process.platform === 'linux') { res = await connect(true); if (res) {