From f28a931b60ad91dbfd674fd46f7b834facaaf4bb Mon Sep 17 00:00:00 2001 From: Alexis Tyler Date: Tue, 16 Feb 2021 23:06:55 +1030 Subject: [PATCH] fix: disconnection reason being too long --- app/mothership/custom-socket.ts | 10 +++------- app/mothership/sockets/internal-graphql.ts | 4 +--- app/mothership/sockets/mothership.ts | 7 ++----- 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/app/mothership/custom-socket.ts b/app/mothership/custom-socket.ts index dbbb1aa35..507f63387 100644 --- a/app/mothership/custom-socket.ts +++ b/app/mothership/custom-socket.ts @@ -93,9 +93,7 @@ export class CustomSocket { customSocket.connectionAttempts = 0; } catch (error: unknown) { if (isNodeError(error, AppError)) { - this.close(Number(error.code?.length === 4 ? error.code : `4${error.code ?? 500}`), JSON.stringify({ - message: error.message ?? 'Internal Server Error' - })); + this.close(Number(error.code?.length === 4 ? error.code : `4${error.code ?? 500}`), 'INTERNAL_SERVER_ERROR'); } } }; @@ -140,7 +138,7 @@ export class CustomSocket { try { if (this.connection && (this.connection.readyState !== this.connection.CLOSED)) { // 4200 === ok - this.connection.close(4200); + this.connection.close(4200, 'OK'); } } catch (error: unknown) { this.logger.error('Failed disconnecting reason=%s', (error as Error).message); @@ -255,9 +253,7 @@ export class CustomSocket { protected async cleanup() { // Kill existing socket connection if (this.connection?.heartbeat) { - this.connection.close(4200, JSON.stringify({ - message: 'Reconnecting' - })); + this.connection.close(4408, 'REQUEST_TIMEOUT'); } } diff --git a/app/mothership/sockets/internal-graphql.ts b/app/mothership/sockets/internal-graphql.ts index 4f2ebdced..08337ec2e 100644 --- a/app/mothership/sockets/internal-graphql.ts +++ b/app/mothership/sockets/internal-graphql.ts @@ -72,9 +72,7 @@ export class InternalGraphql extends CustomSocket { return async function (this: WebSocketWithHeartBeat) { // No API key, close internal connection if (!apiKey) { - this.close(4200, JSON.stringify({ - message: 'No API key' - })); + this.close(4403, 'FORBIDDEN'); } // Authenticate with ourselves diff --git a/app/mothership/sockets/mothership.ts b/app/mothership/sockets/mothership.ts index 4bcb1958b..eaa3cfd42 100644 --- a/app/mothership/sockets/mothership.ts +++ b/app/mothership/sockets/mothership.ts @@ -6,7 +6,6 @@ import { subscribeToServers } from '../subscribe-to-servers'; import { AppError } from '../../core/errors'; import { readFileIfExists } from '../utils'; import { CustomSocket, WebSocketWithHeartBeat } from '../custom-socket'; -import { sockets } from '../../sockets'; export class MothershipSocket extends CustomSocket { private mothershipServersEndpoint?: { @@ -36,9 +35,7 @@ export class MothershipSocket extends CustomSocket { } catch (error: unknown) { if (isNodeError(error, AppError)) { const code = (error.code) ?? 500; - this.close(`${code}`.length === 4 ? Number(code) : Number(`4${code}`), JSON.stringify({ - message: error.message ?? 'Internal Server Error' - })); + this.close(`${code}`.length === 4 ? Number(code) : Number(`4${code}`), 'INTERNAL_SERVER_ERROR'); } } }; @@ -111,7 +108,7 @@ export class MothershipSocket extends CustomSocket { logger.error('socket error', error); } finally { // Kick the connection - this.close(4500, JSON.stringify({ message: error.message })); + this.close(4408, 'REQUEST_TIMEOUT'); } }; }