mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-26 00:33:12 +09:00
clarify null / undefined / 0
remove useless type import and signature export directly cleanup
This commit is contained in:
1 parent
c96a13ef30
commit
3036507baa
7 files changed
+30
-42
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clusterio controller
|
* Clusterio controller
|
||||||
*
|
*
|
||||||
* Facilitates communication between hosts and control of the cluster
|
* Facilitates communication between hosts and control of the cluster
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ import path from "path";
|
|||||||
import * as lib from "@clusterio/lib";
|
import * as lib from "@clusterio/lib";
|
||||||
const { logFilter, logger } = lib;
|
const { logFilter, logger } = lib;
|
||||||
|
|
||||||
import BaseConnection from "./BaseConnection";
|
import BaseConnection from "./BaseConnection";
|
||||||
import routes from "./routes";
|
import * as routes from "./routes";
|
||||||
import Controller from "./Controller";
|
import Controller from "./Controller";
|
||||||
import { promises } from "dns";
|
import { promises } from "dns";
|
||||||
import InstanceInfo from "./InstanceInfo";
|
import InstanceInfo from "./InstanceInfo";
|
||||||
@@ -237,7 +237,7 @@ export default class ControlConnection extends BaseConnection {
|
|||||||
|
|
||||||
async handleHostGenerateTokenRequest(message: lib.HostGenerateTokenRequest) {
|
async handleHostGenerateTokenRequest(message: lib.HostGenerateTokenRequest) {
|
||||||
let hostId = message.hostId;
|
let hostId = message.hostId;
|
||||||
if (hostId === null) {
|
if (hostId === undefined) {
|
||||||
hostId = Math.random() * 2**31 | 0;
|
hostId = Math.random() * 2**31 | 0;
|
||||||
}
|
}
|
||||||
return this._controller.generateHostToken(hostId);
|
return this._controller.generateHostToken(hostId);
|
||||||
@@ -347,7 +347,7 @@ export default class ControlConnection extends BaseConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async handleInstanceAssignRequest(request: lib.InstanceAssignRequest) {
|
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) {
|
async handleInstanceSetSaveListSubscriptionsRequest(request: lib.InstanceSetSaveListSubscriptionsRequest) {
|
||||||
@@ -466,7 +466,10 @@ export default class ControlConnection extends BaseConnection {
|
|||||||
|
|
||||||
async handleModPackCreateRequest(request: lib.ModPackCreateRequest) {
|
async handleModPackCreateRequest(request: lib.ModPackCreateRequest) {
|
||||||
let modPack = request.modPack;
|
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`);
|
throw new lib.RequestError(`Mod pack with ID ${modPack.id} already exist`);
|
||||||
}
|
}
|
||||||
this._controller.modPacks.set(modPack.id, modPack);
|
this._controller.modPacks.set(modPack.id, modPack);
|
||||||
@@ -475,7 +478,7 @@ export default class ControlConnection extends BaseConnection {
|
|||||||
|
|
||||||
async handleModPackUpdateRequest(request: lib.ModPackUpdateRequest) {
|
async handleModPackUpdateRequest(request: lib.ModPackUpdateRequest) {
|
||||||
let modPack = request.modPack;
|
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`);
|
throw new lib.RequestError(`Mod pack with ID ${modPack.id} does not exist`);
|
||||||
}
|
}
|
||||||
this._controller.modPacks.set(modPack.id, modPack);
|
this._controller.modPacks.set(modPack.id, modPack);
|
||||||
@@ -496,7 +499,7 @@ export default class ControlConnection extends BaseConnection {
|
|||||||
modPackUpdated(modPack: lib.ModPack) {
|
modPackUpdated(modPack: lib.ModPack) {
|
||||||
if (
|
if (
|
||||||
this.modPackSubscriptions.all
|
this.modPackSubscriptions.all
|
||||||
|| this.modPackSubscriptions.modPackIds.includes(modPack.id||-1)
|
|| this.modPackSubscriptions.modPackIds.includes(modPack.id!)
|
||||||
) {
|
) {
|
||||||
this.send(new lib.ModPackUpdateEvent(modPack));
|
this.send(new lib.ModPackUpdateEvent(modPack));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ const { logger, Summary, Gauge } = lib;
|
|||||||
import HttpCloser from "./HttpCloser";
|
import HttpCloser from "./HttpCloser";
|
||||||
import InstanceInfo, { InstanceStatus } from "./InstanceInfo";
|
import InstanceInfo, { InstanceStatus } from "./InstanceInfo";
|
||||||
import metrics from "./metrics";
|
import metrics from "./metrics";
|
||||||
import routes from "./routes";
|
import * as routes from "./routes";
|
||||||
import UserManager from "./UserManager";
|
import UserManager from "./UserManager";
|
||||||
import WsServer from "./WsServer";
|
import WsServer from "./WsServer";
|
||||||
import yargs from "yargs";
|
import yargs from "yargs";
|
||||||
@@ -671,12 +671,12 @@ export default class Controller {
|
|||||||
* @param instanceId - ID of Instance to assign.
|
* @param instanceId - ID of Instance to assign.
|
||||||
* @param hostId - ID of host to assign instance to.
|
* @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);
|
let instance = this.getRequestInstance(instanceId);
|
||||||
|
|
||||||
// Check if target host is connected
|
// Check if target host is connected
|
||||||
let newHostConnection: HostConnection | undefined;
|
let newHostConnection: HostConnection | undefined;
|
||||||
if (hostId !== null) {
|
if (hostId !== undefined) {
|
||||||
newHostConnection = this.wsServer.hostConnections.get(hostId);
|
newHostConnection = this.wsServer.hostConnections.get(hostId);
|
||||||
if (!newHostConnection) {
|
if (!newHostConnection) {
|
||||||
// The case of the host not getting the assign instance message
|
// 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.
|
// Unassign from currently assigned host if it is connected.
|
||||||
let currentAssignedHost = instance.config.get("instance.assigned_host");
|
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);
|
let oldHostConnection = this.wsServer.hostConnections.get(currentAssignedHost);
|
||||||
if (oldHostConnection && !oldHostConnection.connector.closing) {
|
if (oldHostConnection && !oldHostConnection.connector.closing) {
|
||||||
await oldHostConnection.send(new lib.InstanceUnassignInternalRequest(instanceId));
|
await oldHostConnection.send(new lib.InstanceUnassignInternalRequest(instanceId));
|
||||||
@@ -704,7 +704,7 @@ export default class Controller {
|
|||||||
new lib.InstanceAssignInternalRequest(instanceId, instance.config.serialize("host"))
|
new lib.InstanceAssignInternalRequest(instanceId, instance.config.serialize("host"))
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
instance.status = "unassigned"
|
instance.status = "unassigned";
|
||||||
this.instanceUpdated(instance);
|
this.instanceUpdated(instance);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -720,7 +720,7 @@ export default class Controller {
|
|||||||
async instanceDelete(instanceId: number) {
|
async instanceDelete(instanceId: number) {
|
||||||
let instance = this.getRequestInstance(instanceId);
|
let instance = this.getRequestInstance(instanceId);
|
||||||
let hostId = instance.config.get("instance.assigned_host");
|
let hostId = instance.config.get("instance.assigned_host");
|
||||||
if (hostId !== null && hostId !== undefined) {
|
if (hostId !== null) {
|
||||||
await this.sendTo({ hostId }, new lib.InstanceDeleteInternalRequest(instanceId));
|
await this.sendTo({ hostId }, new lib.InstanceDeleteInternalRequest(instanceId));
|
||||||
}
|
}
|
||||||
this.instances.delete(instanceId);
|
this.instances.delete(instanceId);
|
||||||
@@ -742,7 +742,7 @@ export default class Controller {
|
|||||||
*/
|
*/
|
||||||
async instanceConfigUpdated(instance: InstanceInfo) {
|
async instanceConfigUpdated(instance: InstanceInfo) {
|
||||||
let hostId = instance.config.get("instance.assigned_host");
|
let hostId = instance.config.get("instance.assigned_host");
|
||||||
if (hostId !== null && hostId !== undefined) {
|
if (hostId !== null) {
|
||||||
let connection = this.wsServer.hostConnections.get(hostId);
|
let connection = this.wsServer.hostConnections.get(hostId);
|
||||||
if (connection) {
|
if (connection) {
|
||||||
await connection.send(
|
await connection.send(
|
||||||
@@ -1036,13 +1036,13 @@ export default class Controller {
|
|||||||
* destination argument supports address shorthands, see {@link
|
* destination argument supports address shorthands, see {@link
|
||||||
* module:lib.Address.fromShorthand}
|
* module:lib.Address.fromShorthand}
|
||||||
*
|
*
|
||||||
* @param {*|module:lib.Address} address - Where to send it.
|
* @param address - Where to send it.
|
||||||
* @param {*} requestOrEvent - The request or event to send.
|
* @param requestOrEvent - The request or event to send.
|
||||||
* @returns {Promise<*>|undefined}
|
* @returns
|
||||||
* Promise that resolves to the response if a request was sent or
|
* Promise that resolves to the response if a request was sent or
|
||||||
* undefined if it was an event.
|
* 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);
|
let dst = lib.Address.fromShorthand(address);
|
||||||
if (requestOrEvent.constructor.type === "request") {
|
if (requestOrEvent.constructor.type === "request") {
|
||||||
return this.sendRequest(requestOrEvent, dst);
|
return this.sendRequest(requestOrEvent, dst);
|
||||||
|
|||||||
@@ -13,14 +13,6 @@ export default class ControllerRouter {
|
|||||||
public controller: Controller
|
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 origin - Source link of the message.
|
||||||
* @param message - Link the message originated from.
|
* @param message - Link the message originated from.
|
||||||
@@ -72,7 +64,7 @@ export default class ControllerRouter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (nextHop === origin) {
|
if (nextHop === origin) {
|
||||||
msg = `Message would return back to sender ${origin.id}.`;
|
msg = `Message would return back to sender ${origin.connector.dst}.`;
|
||||||
nextHop = undefined;
|
nextHop = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,7 +84,7 @@ export default class ControllerRouter {
|
|||||||
if (message.type === "request") {
|
if (message.type === "request") {
|
||||||
nextHop.forwardRequest(message as lib.MessageRequest, origin);
|
nextHop.forwardRequest(message as lib.MessageRequest, origin);
|
||||||
} else {
|
} else {
|
||||||
nextHop.connector._sendInternal(message);
|
nextHop.connector.send(message);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
this.warnUnrouted(message, msg);
|
this.warnUnrouted(message, msg);
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import type { IncomingMessage, ServerResponse } from "http"; "http";
|
import type { Application, Request, Response } from "express";
|
||||||
import type { Application, Express, Request, Response } from "express";
|
|
||||||
import type Controller from "./Controller";
|
import type Controller from "./Controller";
|
||||||
|
|
||||||
import busboy from "busboy";
|
import busboy from "busboy";
|
||||||
@@ -259,7 +258,7 @@ interface ProxyStream {
|
|||||||
timeout: NodeJS.Timeout;
|
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 asyncRandomBytes = util.promisify(crypto.randomBytes);
|
||||||
let id = (await asyncRandomBytes(8)).toString("hex");
|
let id = (await asyncRandomBytes(8)).toString("hex");
|
||||||
let stream: ProxyStream = {
|
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("/metrics", (req:Request, res:Response, next:any) => getMetrics(req, res, next).catch(next));
|
||||||
app.get("/api/plugins", getPlugins);
|
app.get("/api/plugins", getPlugins);
|
||||||
app.put("/api/upload-export",
|
app.put("/api/upload-export",
|
||||||
@@ -652,7 +651,7 @@ function addRouteHandlers(app: Application) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Routes used in the web interface and served by the controller
|
// Routes used in the web interface and served by the controller
|
||||||
const webRoutes = [
|
export const webRoutes = [
|
||||||
"/",
|
"/",
|
||||||
"/controller",
|
"/controller",
|
||||||
"/hosts",
|
"/hosts",
|
||||||
@@ -668,9 +667,3 @@ const webRoutes = [
|
|||||||
"/plugins",
|
"/plugins",
|
||||||
"/plugins/:name/view",
|
"/plugins/:name/view",
|
||||||
];
|
];
|
||||||
|
|
||||||
export default {
|
|
||||||
addRouteHandlers,
|
|
||||||
webRoutes,
|
|
||||||
createProxyStream,
|
|
||||||
}
|
|
||||||
@@ -80,7 +80,7 @@ export class HostGenerateTokenRequest {
|
|||||||
static permission = "core.host.generate_token" as const;
|
static permission = "core.host.generate_token" as const;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
public hostId: number | null = null,
|
public hostId?: number,
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
static jsonSchema = Type.Object({
|
static jsonSchema = Type.Object({
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ export class Address {
|
|||||||
* Shorthand to translate with.
|
* Shorthand to translate with.
|
||||||
* @returns Translated address.
|
* @returns Translated address.
|
||||||
*/
|
*/
|
||||||
static fromShorthand(shorthand: AddressShorthand | Address) {
|
static fromShorthand(shorthand: AddressShorthand) {
|
||||||
if (shorthand instanceof Address) { return shorthand; }
|
if (shorthand instanceof Address) { return shorthand; }
|
||||||
if (shorthand === "controller") { return new Address(Address.controller, 0); }
|
if (shorthand === "controller") { return new Address(Address.controller, 0); }
|
||||||
if (shorthand === "allHosts") { return new Address(Address.broadcast, Address.host); }
|
if (shorthand === "allHosts") { return new Address(Address.broadcast, Address.host); }
|
||||||
|
|||||||
Reference in new issue
Block a user