From 3036507baa6001c87cf604c7ade6926399841632 Mon Sep 17 00:00:00 2001 From: Luciole <31817566+Luciole0x@users.noreply.github.com> Date: Mon, 14 Aug 2023 10:54:30 +0200 Subject: [PATCH] clarify null / undefined / 0 remove useless type import and signature export directly cleanup --- packages/controller/controller.ts | 2 +- packages/controller/src/ControlConnection.ts | 17 ++++++++------- packages/controller/src/Controller.ts | 22 ++++++++++---------- packages/controller/src/ControllerRouter.ts | 12 ++--------- packages/controller/src/routes.ts | 15 ++++--------- packages/lib/src/data/messages_controller.ts | 2 +- packages/lib/src/data/messages_core.ts | 2 +- 7 files changed, 30 insertions(+), 42 deletions(-) diff --git a/packages/controller/controller.ts b/packages/controller/controller.ts index a6344fb1..19068d38 100644 --- a/packages/controller/controller.ts +++ b/packages/controller/controller.ts @@ -1,6 +1,6 @@ #!/usr/bin/env node -/** +/** * Clusterio controller * * Facilitates communication between hosts and control of the cluster diff --git a/packages/controller/src/ControlConnection.ts b/packages/controller/src/ControlConnection.ts index c30512ed..de7d303b 100644 --- a/packages/controller/src/ControlConnection.ts +++ b/packages/controller/src/ControlConnection.ts @@ -8,8 +8,8 @@ import path from "path"; import * as lib from "@clusterio/lib"; const { logFilter, logger } = lib; -import BaseConnection from "./BaseConnection"; -import routes from "./routes"; +import BaseConnection from "./BaseConnection"; +import * as routes from "./routes"; import Controller from "./Controller"; import { promises } from "dns"; import InstanceInfo from "./InstanceInfo"; @@ -237,7 +237,7 @@ export default class ControlConnection extends BaseConnection { async handleHostGenerateTokenRequest(message: lib.HostGenerateTokenRequest) { let hostId = message.hostId; - if (hostId === null) { + if (hostId === undefined) { hostId = Math.random() * 2**31 | 0; } return this._controller.generateHostToken(hostId); @@ -347,7 +347,7 @@ export default class ControlConnection extends BaseConnection { } async handleInstanceAssignRequest(request: lib.InstanceAssignRequest) { - await this._controller.instanceAssign(request.instanceId, request.hostId || null); + await this._controller.instanceAssign(request.instanceId, request.hostId); } async handleInstanceSetSaveListSubscriptionsRequest(request: lib.InstanceSetSaveListSubscriptionsRequest) { @@ -466,7 +466,10 @@ export default class ControlConnection extends BaseConnection { async handleModPackCreateRequest(request: lib.ModPackCreateRequest) { let modPack = request.modPack; - if (!modPack.id || this._controller.modPacks.has(modPack.id)) { + if (modPack.id === undefined) { + throw new lib.RequestError(`Mod pack need an ID to be created`); + } + if (this._controller.modPacks.has(modPack.id)) { throw new lib.RequestError(`Mod pack with ID ${modPack.id} already exist`); } this._controller.modPacks.set(modPack.id, modPack); @@ -475,7 +478,7 @@ export default class ControlConnection extends BaseConnection { async handleModPackUpdateRequest(request: lib.ModPackUpdateRequest) { let modPack = request.modPack; - if (!modPack.id || !this._controller.modPacks.has(modPack.id)) { + if (modPack.id === undefined || !this._controller.modPacks.has(modPack.id)) { throw new lib.RequestError(`Mod pack with ID ${modPack.id} does not exist`); } this._controller.modPacks.set(modPack.id, modPack); @@ -496,7 +499,7 @@ export default class ControlConnection extends BaseConnection { modPackUpdated(modPack: lib.ModPack) { if ( this.modPackSubscriptions.all - || this.modPackSubscriptions.modPackIds.includes(modPack.id||-1) + || this.modPackSubscriptions.modPackIds.includes(modPack.id!) ) { this.send(new lib.ModPackUpdateEvent(modPack)); } diff --git a/packages/controller/src/Controller.ts b/packages/controller/src/Controller.ts index 8592ba57..6e35efab 100644 --- a/packages/controller/src/Controller.ts +++ b/packages/controller/src/Controller.ts @@ -21,7 +21,7 @@ const { logger, Summary, Gauge } = lib; import HttpCloser from "./HttpCloser"; import InstanceInfo, { InstanceStatus } from "./InstanceInfo"; import metrics from "./metrics"; -import routes from "./routes"; +import * as routes from "./routes"; import UserManager from "./UserManager"; import WsServer from "./WsServer"; import yargs from "yargs"; @@ -671,12 +671,12 @@ export default class Controller { * @param instanceId - ID of Instance to assign. * @param hostId - ID of host to assign instance to. */ - async instanceAssign(instanceId: number, hostId: number | null) { + async instanceAssign(instanceId: number, hostId?: number) { let instance = this.getRequestInstance(instanceId); // Check if target host is connected let newHostConnection: HostConnection | undefined; - if (hostId !== null) { + if (hostId !== undefined) { newHostConnection = this.wsServer.hostConnections.get(hostId); if (!newHostConnection) { // The case of the host not getting the assign instance message @@ -690,7 +690,7 @@ export default class Controller { // Unassign from currently assigned host if it is connected. let currentAssignedHost = instance.config.get("instance.assigned_host"); - if (currentAssignedHost !== null && currentAssignedHost !== undefined && hostId !== currentAssignedHost) { + if (currentAssignedHost !== null && hostId !== currentAssignedHost) { let oldHostConnection = this.wsServer.hostConnections.get(currentAssignedHost); if (oldHostConnection && !oldHostConnection.connector.closing) { await oldHostConnection.send(new lib.InstanceUnassignInternalRequest(instanceId)); @@ -704,7 +704,7 @@ export default class Controller { new lib.InstanceAssignInternalRequest(instanceId, instance.config.serialize("host")) ); } else { - instance.status = "unassigned" + instance.status = "unassigned"; this.instanceUpdated(instance); } } @@ -720,7 +720,7 @@ export default class Controller { async instanceDelete(instanceId: number) { let instance = this.getRequestInstance(instanceId); let hostId = instance.config.get("instance.assigned_host"); - if (hostId !== null && hostId !== undefined) { + if (hostId !== null) { await this.sendTo({ hostId }, new lib.InstanceDeleteInternalRequest(instanceId)); } this.instances.delete(instanceId); @@ -742,7 +742,7 @@ export default class Controller { */ async instanceConfigUpdated(instance: InstanceInfo) { let hostId = instance.config.get("instance.assigned_host"); - if (hostId !== null && hostId !== undefined) { + if (hostId !== null) { let connection = this.wsServer.hostConnections.get(hostId); if (connection) { await connection.send( @@ -1036,13 +1036,13 @@ export default class Controller { * destination argument supports address shorthands, see {@link * module:lib.Address.fromShorthand} * - * @param {*|module:lib.Address} address - Where to send it. - * @param {*} requestOrEvent - The request or event to send. - * @returns {Promise<*>|undefined} + * @param address - Where to send it. + * @param requestOrEvent - The request or event to send. + * @returns * Promise that resolves to the response if a request was sent or * undefined if it was an event. */ - sendTo(address: lib.AddressShorthand, requestOrEvent: any) { + sendTo(address: lib.AddressShorthand, requestOrEvent: any): Promise | void { let dst = lib.Address.fromShorthand(address); if (requestOrEvent.constructor.type === "request") { return this.sendRequest(requestOrEvent, dst); diff --git a/packages/controller/src/ControllerRouter.ts b/packages/controller/src/ControllerRouter.ts index b94da0e6..744a98c9 100644 --- a/packages/controller/src/ControllerRouter.ts +++ b/packages/controller/src/ControllerRouter.ts @@ -13,14 +13,6 @@ export default class ControllerRouter { public controller: Controller ) {} - addHostConnection(connection: HostConnection) { - this.controller.wsServer.hostConnections.set(connection.id, connection); - } - - addControlConnection(connection: ControlConnection) { - this.controller.wsServer.controlConnections.set(connection.id, connection); - } - /** * @param origin - Source link of the message. * @param message - Link the message originated from. @@ -72,7 +64,7 @@ export default class ControllerRouter { } if (nextHop === origin) { - msg = `Message would return back to sender ${origin.id}.`; + msg = `Message would return back to sender ${origin.connector.dst}.`; nextHop = undefined; } @@ -92,7 +84,7 @@ export default class ControllerRouter { if (message.type === "request") { nextHop.forwardRequest(message as lib.MessageRequest, origin); } else { - nextHop.connector._sendInternal(message); + nextHop.connector.send(message); } } else { this.warnUnrouted(message, msg); diff --git a/packages/controller/src/routes.ts b/packages/controller/src/routes.ts index efbff12d..b401bd2c 100644 --- a/packages/controller/src/routes.ts +++ b/packages/controller/src/routes.ts @@ -1,5 +1,4 @@ -import type { IncomingMessage, ServerResponse } from "http"; "http"; -import type { Application, Express, Request, Response } from "express"; +import type { Application, Request, Response } from "express"; import type Controller from "./Controller"; import busboy from "busboy"; @@ -259,7 +258,7 @@ interface ProxyStream { timeout: NodeJS.Timeout; } -async function createProxyStream(app: Application): Promise { +export async function createProxyStream(app: Application): Promise { let asyncRandomBytes = util.promisify(crypto.randomBytes); let id = (await asyncRandomBytes(8)).toString("hex"); let stream: ProxyStream = { @@ -632,7 +631,7 @@ async function uploadMod(req: Request, res: Response) { } -function addRouteHandlers(app: Application) { +export function addRouteHandlers(app: Application) { app.get("/metrics", (req:Request, res:Response, next:any) => getMetrics(req, res, next).catch(next)); app.get("/api/plugins", getPlugins); app.put("/api/upload-export", @@ -652,7 +651,7 @@ function addRouteHandlers(app: Application) { } // Routes used in the web interface and served by the controller -const webRoutes = [ +export const webRoutes = [ "/", "/controller", "/hosts", @@ -668,9 +667,3 @@ const webRoutes = [ "/plugins", "/plugins/:name/view", ]; - -export default { - addRouteHandlers, - webRoutes, - createProxyStream, -} diff --git a/packages/lib/src/data/messages_controller.ts b/packages/lib/src/data/messages_controller.ts index 6534fad3..4e2807f7 100644 --- a/packages/lib/src/data/messages_controller.ts +++ b/packages/lib/src/data/messages_controller.ts @@ -80,7 +80,7 @@ export class HostGenerateTokenRequest { static permission = "core.host.generate_token" as const; constructor( - public hostId: number | null = null, + public hostId?: number, ) { } static jsonSchema = Type.Object({ diff --git a/packages/lib/src/data/messages_core.ts b/packages/lib/src/data/messages_core.ts index e27c2bd1..62df2711 100644 --- a/packages/lib/src/data/messages_core.ts +++ b/packages/lib/src/data/messages_core.ts @@ -48,7 +48,7 @@ export class Address { * Shorthand to translate with. * @returns Translated address. */ - static fromShorthand(shorthand: AddressShorthand | Address) { + static fromShorthand(shorthand: AddressShorthand) { if (shorthand instanceof Address) { return shorthand; } if (shorthand === "controller") { return new Address(Address.controller, 0); } if (shorthand === "allHosts") { return new Address(Address.broadcast, Address.host); }