clarify null / undefined / 0

remove useless type import and signature
export directly
cleanup
This commit is contained in:
Luciole committed 2023-08-14 10:54:30 +02:00
1 parent c96a13ef30
commit 3036507baa
7 files changed
+30 -42

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
#!/usr/bin/env node
/**
/**
* Clusterio controller
*
* Facilitates communication between hosts and control of the cluster
+10 -7
View File
@@ -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));
}
+11 -11
View File
@@ -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<any> | void {
let dst = lib.Address.fromShorthand(address);
if (requestOrEvent.constructor.type === "request") {
return this.sendRequest(requestOrEvent, dst);
+2 -10
View File
@@ -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);
+4 -11
View File
@@ -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<ProxyStream> {
export async function createProxyStream(app: Application): Promise<ProxyStream> {
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,
}
+1 -1
View File
@@ -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({
+1 -1
View File
@@ -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); }