From 9f3c3a51b2a4f5d64146be7cd67ef91fc6eff0aa Mon Sep 17 00:00:00 2001 From: Gus Caplan Date: Mon, 27 Jul 2020 14:08:49 -0500 Subject: [PATCH] some cleanup --- src/client.js | 8 ++++++-- src/transports/ipc.js | 6 +++--- src/transports/websocket.js | 7 +++++-- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/client.js b/src/client.js index af68814..f4e2d5c 100644 --- a/src/client.js +++ b/src/client.js @@ -58,7 +58,12 @@ class RPCClient extends EventEmitter { headers: { Authorization: `Bearer ${this.accessToken}`, }, - }).then((r) => r.json()); + }).then((r) => { + if (!r.ok) { + throw new Error(r.status); + } + return r.json(); + }); this.fetch.endpoint = 'https://discord.com/api'; @@ -212,7 +217,6 @@ class RPCClient extends EventEmitter { scopes, client_id: this.clientId, rpc_token: rpcToken, - redirect_uri: redirectUri, }); const response = await this.fetch('POST', '/oauth2/token', { diff --git a/src/transports/ipc.js b/src/transports/ipc.js index d8d3f6e..8d9dce1 100644 --- a/src/transports/ipc.js +++ b/src/transports/ipc.js @@ -47,10 +47,10 @@ async function findEndpoint(tries = 0) { const endpoint = `http://127.0.0.1:${6463 + (tries % 10)}`; try { const r = await fetch(endpoint); - if (r.status !== 401) { - return findEndpoint(tries + 1); + if (r.status === 404) { + return endpoint; } - return endpoint; + return findEndpoint(tries + 1); } catch (e) { return findEndpoint(tries + 1); } diff --git a/src/transports/websocket.js b/src/transports/websocket.js index cc0bcc2..5e4a3f8 100644 --- a/src/transports/websocket.js +++ b/src/transports/websocket.js @@ -17,7 +17,7 @@ class WebSocketTransport extends EventEmitter { this.tries = 0; } - async connect(options, tries = this.tries) { + async connect(tries = this.tries) { if (this.connected) { return; } @@ -25,6 +25,9 @@ class WebSocketTransport extends EventEmitter { this.hostAndPort = `127.0.0.1:${port}`; const ws = this.ws = new WebSocket( `ws://${this.hostAndPort}/?v=1&client_id=${this.client.clientId}`, + { + origin: this.client.options.origin, + }, ); ws.onopen = this.onOpen.bind(this); ws.onclose = ws.onerror = this.onClose.bind(this); @@ -65,7 +68,7 @@ class WebSocketTransport extends EventEmitter { } if (!derr) { // eslint-disable-next-line no-plusplus - setTimeout(() => this.connect(undefined, e.code === 1006 ? ++this.tries : 0), 250); + setTimeout(() => this.connect(e.code === 1006 ? ++this.tries : 0), 250); } } }