From b4ecc6c405c1f029b250679da0f640c773edc493 Mon Sep 17 00:00:00 2001 From: Gus Caplan Date: Thu, 17 Sep 2020 11:23:47 -0500 Subject: [PATCH 1/2] bit of cleanup --- src/client.js | 7 ++-- src/transports/ipc.js | 19 ++++++---- src/transports/websocket.js | 69 +++++++++++++++++++------------------ test/rp.js | 10 ++++-- 4 files changed, 59 insertions(+), 46 deletions(-) diff --git a/src/client.js b/src/client.js index 55063e3..204ef3a 100644 --- a/src/client.js +++ b/src/client.js @@ -376,12 +376,11 @@ class RPCClient extends EventEmitter { * @param {Snowflake} id ID of the voice channel * @param {Object} [options] Options * @param {number} [options.timeout] Timeout for the command - * @param {boolean} [options.force] Force this move. This should only be done if you * have explicit permission from the user. * @returns {Promise} */ - selectTextChannel(id, { timeout, force = false } = {}) { - return this.request(RPCCommands.SELECT_TEXT_CHANNEL, { channel_id: id, timeout, force }); + selectTextChannel(id, { timeout } = {}) { + return this.request(RPCCommands.SELECT_TEXT_CHANNEL, { channel_id: id, timeout }); } /** @@ -672,7 +671,7 @@ class RPCClient extends EventEmitter { * Destroy the client */ async destroy() { - this.transport.close(); + await this.transport.close(); } } diff --git a/src/transports/ipc.js b/src/transports/ipc.js index 8d9dce1..f050a01 100644 --- a/src/transports/ipc.js +++ b/src/transports/ipc.js @@ -127,9 +127,13 @@ class IPCTransport extends EventEmitter { return; } if (data.cmd === 'AUTHORIZE' && data.evt !== 'ERROR') { - findEndpoint().then((endpoint) => { - this.client.request.endpoint = endpoint; - }); + findEndpoint() + .then((endpoint) => { + this.client.request.endpoint = endpoint; + }) + .catch((e) => { + this.client.emit('error', e); + }); } this.emit('message', data); break; @@ -151,9 +155,12 @@ class IPCTransport extends EventEmitter { this.socket.write(encode(op, data)); } - close() { - this.send({}, OPCodes.CLOSE); - this.socket.end(); + async close() { + return new Promise((r) => { + this.once('close', r); + this.send({}, OPCodes.CLOSE); + this.socket.end(); + }); } ping() { diff --git a/src/transports/websocket.js b/src/transports/websocket.js index 5e4a3f8..66e083a 100644 --- a/src/transports/websocket.js +++ b/src/transports/websocket.js @@ -17,59 +17,62 @@ class WebSocketTransport extends EventEmitter { this.tries = 0; } - async connect(tries = this.tries) { - if (this.connected) { - return; - } - const port = 6463 + (tries % 10); - this.hostAndPort = `127.0.0.1:${port}`; - const ws = this.ws = new WebSocket( - `ws://${this.hostAndPort}/?v=1&client_id=${this.client.clientId}`, + async connect() { + const port = 6463 + (this.tries % 10); + this.tries += 1; + + this.ws = new WebSocket( + `ws://127.0.0.1:${port}/?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); - ws.onmessage = this.onMessage.bind(this); + this.ws.onopen = this.onOpen.bind(this); + this.ws.onclose = this.onClose.bind(this); + this.ws.onerror = this.onError.bind(this); + this.ws.onmessage = this.onMessage.bind(this); } - send(data) { - if (!this.ws) { - return; - } - this.ws.send(pack(data)); + onOpen() { + this.emit('open'); } - close() { - if (!this.ws) { + onClose(event) { + if (!event.wasClean) { return; } - this.ws.close(); + this.emit('close', event); } - ping() {} // eslint-disable-line no-empty-function + onError(event) { + try { + this.ws.close(); + } catch {} // eslint-disable-line no-empty + + if (this.tries > 20) { + this.emit('error', event.error); + } else { + setTimeout(() => { + this.connect(); + }, 250); + } + } onMessage(event) { this.emit('message', unpack(event.data)); } - onOpen() { - this.emit('open'); + send(data) { + this.ws.send(pack(data)); } - onClose(e) { - try { + ping() {} // eslint-disable-line no-empty-function + + close() { + return new Promise((r) => { + this.once('close', r); this.ws.close(); - } catch (err) {} // eslint-disable-line no-empty - const derr = e.code >= 4000 && e.code < 5000; - if (!e.code || derr) { - this.emit('close', e); - } - if (!derr) { - // eslint-disable-next-line no-plusplus - setTimeout(() => this.connect(e.code === 1006 ? ++this.tries : 0), 250); - } + }); } } diff --git a/test/rp.js b/test/rp.js index 93fca29..a9a06bd 100644 --- a/test/rp.js +++ b/test/rp.js @@ -12,9 +12,13 @@ const client = new Client({ transport: 'ipc', }); -client.on('ready', () => { - client.subscribe('MESSAGE_CREATE', { channel_id: '381886868708655104' }, console.log) - .catch(console.error); +client.on('ready', async () => { + await client.selectTextChannel('201803114049699849'); + console.log(await client.getChannel('201803114049699849')); + client.destroy() + .then(() => { + console.log('closed!'); + }); }); client.login(require('./auth')).catch(console.error); From c6a309e087d6bd220bb17a0fb266573d4270e816 Mon Sep 17 00:00:00 2001 From: Gus Caplan Date: Thu, 17 Sep 2020 11:28:21 -0500 Subject: [PATCH 2/2] add docs workflow --- .github/workflows/docs.yml | 20 ++++++++++++++++++++ .gitignore | 2 +- package.json | 2 +- 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/docs.yml diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml new file mode 100644 index 0000000..67d8bc5 --- /dev/null +++ b/.github/workflows/docs.yml @@ -0,0 +1,20 @@ +on: + push: + branches: + - master + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v1 + with: + node-version: '14' + - run: "npm install" + - run: "npm run docs" + - uses: JamesIves/github-pages-deploy-action@3.5.9 + with: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + BRANCH: docs + FOLDER: docs-out diff --git a/.gitignore b/.gitignore index 97ee4bc..ee23ad8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ node_modules/** package-lock.json -docs.json browser.js test/auth.js +docs-out diff --git a/package.json b/package.json index 7911c8f..e35c3f1 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ }, "scripts": { "lint": "eslint src test --ext=js", - "docs": "docgen --source src --output docs.json --jsdoc jsdoc.json --custom docgen.json", + "docs": "mkdir -p docs-out && docgen --source src --output docs-out/master.json --jsdoc jsdoc.json --custom docgen.json", "example": "electron example/main.js", "build:browser": "webpack-cli", "prepublishOnly": "npm run lint && npm run build:browser"