mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-19 23:33:13 +09:00
Fix code style errors
Remove extra whitespace, use more consistent spacing, add missing semicolons, using dangling commas, break up long lines, consistent multiline operator placement, add missing JSDoc strings and so on.
This commit is contained in:
1 parent
97e05c0610
commit
7314dc99c8
71 files changed
+403
-290
No files matched your search
@@ -3,7 +3,7 @@ import { Type, Static } from "@sinclair/typebox";
|
||||
import events from "events";
|
||||
|
||||
import isDeepStrictEqual from "../is_deep_strict_equal";
|
||||
import { basicType } from"../helpers";
|
||||
import { basicType } from "../helpers";
|
||||
|
||||
|
||||
export type ConfigLocation = "controller" | "host" | "instance" | "control";
|
||||
|
||||
@@ -264,7 +264,7 @@ export class HostConfig extends classes.Config {
|
||||
get(name: "host.public_address"): string;
|
||||
get(name: "host.max_reconnect_delay"): number;
|
||||
get(name: string): unknown;
|
||||
get(name: string) {
|
||||
get(name: string) {
|
||||
return super.get(name);
|
||||
}
|
||||
}
|
||||
@@ -503,7 +503,10 @@ export class ControlConfig extends classes.Config { }
|
||||
ControlConfig.registerGroup(ControlGroup);
|
||||
|
||||
|
||||
function validateGroup(pluginInfo: PluginNodeEnvInfo|PluginWebpackEnvInfo, groupName: "ControllerConfigGroup"|"InstanceConfigGroup") {
|
||||
function validateGroup(
|
||||
pluginInfo: PluginNodeEnvInfo | PluginWebpackEnvInfo,
|
||||
groupName: "ControllerConfigGroup"|"InstanceConfigGroup",
|
||||
) {
|
||||
if (!(pluginInfo[groupName]?.prototype instanceof classes.PluginConfigGroup)) {
|
||||
throw new Error(
|
||||
`Expected ${groupName} for ${pluginInfo.name} to be a subclass of PluginConfigGroup`
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
"use strict";
|
||||
import zlib from "zlib";
|
||||
import { Type, Static } from "@sinclair/typebox";
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Type } from "@sinclair/typebox";
|
||||
|
||||
export function StringEnum<T extends string[]>(values: [...T]) {
|
||||
return Type.Unsafe<T[number]>({ type: 'string', enum: values })
|
||||
return Type.Unsafe<T[number]>({ type: "string", enum: values });
|
||||
}
|
||||
|
||||
export interface JSONDeserialisable<T> {
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
import type { ValidateFunction } from "ajv";
|
||||
import type { Message } from "./messages_core";
|
||||
var message_validate: ValidateFunction<Message>;
|
||||
const message_validate: ValidateFunction<Message>;
|
||||
export default message_validate;
|
||||
@@ -166,6 +166,7 @@ export class LogQueryRequest {
|
||||
json.all, json.controller, json.hostIds, json.instanceIds, json.maxLevel, json.limit, json.order
|
||||
);
|
||||
}
|
||||
|
||||
static Response = class Response {
|
||||
constructor(
|
||||
public log: object[],
|
||||
|
||||
@@ -23,7 +23,15 @@ export class HostDetails {
|
||||
});
|
||||
|
||||
static fromJSON(json: Static<typeof this.jsonSchema>) {
|
||||
return new this(json.agent, json.version, json.name, json.id, json.connected, json.publicAddress, json.tokenValidAfter);
|
||||
return new this(
|
||||
json.agent,
|
||||
json.version,
|
||||
json.name,
|
||||
json.id,
|
||||
json.connected,
|
||||
json.publicAddress,
|
||||
json.tokenValidAfter,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -211,7 +211,7 @@ export class RawUser { // TODO refactor into lib/user.User
|
||||
isDeleted: this.isDeleted,
|
||||
};
|
||||
if (this.playerStats) {
|
||||
json.playerStats = this.playerStats.toJSON()
|
||||
json.playerStats = this.playerStats.toJSON();
|
||||
}
|
||||
if (this.instanceStats) {
|
||||
json.instanceStats = [...this.instanceStats].map(([k, v]) => [k, v.toJSON()]);
|
||||
|
||||
@@ -277,8 +277,9 @@ export class ItemDatabase {
|
||||
|
||||
/**
|
||||
* Allow to iterate through the items.
|
||||
* @returns array of items.
|
||||
*/
|
||||
getEntries(): IterableIterator<[string, number]> {
|
||||
return this._items.entries()
|
||||
return this._items.entries();
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
"use strict";
|
||||
import zlib from "zlib";
|
||||
|
||||
class MapReaderState {
|
||||
@@ -72,7 +71,7 @@ function readString(state: MapReaderState) {
|
||||
return data;
|
||||
}
|
||||
|
||||
function readOptional<T>(state: MapReaderState, readValue: (state: MapReaderState) => T) {
|
||||
function readOptional<T>(state: MapReaderState, readValue: (s: MapReaderState) => T) {
|
||||
let load = readUInt8(state) !== 0;
|
||||
if (!load) {
|
||||
return null;
|
||||
@@ -80,7 +79,7 @@ function readOptional<T>(state: MapReaderState, readValue: (state: MapReaderStat
|
||||
return readValue(state);
|
||||
}
|
||||
|
||||
function readArray<T>(state: MapReaderState, readItem: (state: MapReaderState) => T) {
|
||||
function readArray<T>(state: MapReaderState, readItem: (s: MapReaderState) => T) {
|
||||
let size = readUInt32so(state);
|
||||
|
||||
let array: T[] = [];
|
||||
@@ -94,8 +93,8 @@ function readArray<T>(state: MapReaderState, readItem: (state: MapReaderState) =
|
||||
|
||||
function readDict<K, V>(
|
||||
state: MapReaderState,
|
||||
readKey: (state: MapReaderState) => K,
|
||||
readValue: (state: MapReaderState) => V
|
||||
readKey: (s: MapReaderState) => K,
|
||||
readValue: (s: MapReaderState) => V
|
||||
) {
|
||||
let size = readUInt32so(state);
|
||||
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
export default function isDeepStrictEqual(a: any, b: any): boolean
|
||||
export default function isDeepStrictEqual(a: any, b: any): boolean;
|
||||
@@ -1,13 +1,12 @@
|
||||
// Connection adapters for Links
|
||||
"use strict";
|
||||
import { strict as assert } from "assert";
|
||||
import events from "events";
|
||||
import WebSocket from "./WebSocket";
|
||||
|
||||
import * as libData from "../data";
|
||||
import * as libErrors from "../errors";
|
||||
import { logger } from "../logging";
|
||||
import ExponentialBackoff from "../ExponentialBackoff";
|
||||
import * as libErrors from "../errors";
|
||||
import { logger } from "../logging";
|
||||
import ExponentialBackoff from "../ExponentialBackoff";
|
||||
import type { Request, Event } from "./link";
|
||||
|
||||
|
||||
|
||||
@@ -3,5 +3,5 @@
|
||||
* @module lib/link
|
||||
* @author Hornwitser
|
||||
*/
|
||||
export * from "./link"
|
||||
export * from "./connectors"
|
||||
export * from "./link";
|
||||
export * from "./connectors";
|
||||
@@ -316,7 +316,7 @@ export class Link {
|
||||
if (!this.router) {
|
||||
let err = new libErrors.InvalidMessage(
|
||||
`Received message addressed to ${(message as libData.MessageRequest).dst} but this link `+
|
||||
`does not route messages`
|
||||
"does not route messages"
|
||||
);
|
||||
if (message.type === "request") {
|
||||
this.connector.sendResponseError(
|
||||
@@ -401,8 +401,8 @@ export class Link {
|
||||
this.connector.sendResponseError(
|
||||
new libData.ResponseError(err.message, err.code, err.stack), message.src, spoofedSrc
|
||||
);
|
||||
} catch (err: any) {
|
||||
logger.error(`Unexpected error sending error response for ${message.name}:\n${err.stack}`);
|
||||
} catch (subErr: any) {
|
||||
logger.error(`Unexpected error sending error response for ${message.name}:\n${subErr.stack}`);
|
||||
}
|
||||
}
|
||||
);
|
||||
@@ -527,7 +527,7 @@ export class Link {
|
||||
let requestId = this._nextRequestId;
|
||||
this._nextRequestId += 1;
|
||||
this._pendingRequests.set(
|
||||
requestId, { request: entry, promise, resolve: resolve!, reject: reject!, dst, }
|
||||
requestId, { request: entry, promise, resolve: resolve!, reject: reject!, dst }
|
||||
);
|
||||
this.connector.sendRequest(request, requestId, dst);
|
||||
return promise;
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
"use strict";
|
||||
import * as core from "../data/messages_core";
|
||||
import * as controller from "../data/messages_controller";
|
||||
import * as host from "../data/messages_host";
|
||||
import * as instance from "../data/messages_instance";
|
||||
import * as mod from "../data/messages_mod";
|
||||
import * as user from "../data/messages_user";
|
||||
import * as subscriptions from "../subscriptions"
|
||||
import * as subscriptions from "../subscriptions";
|
||||
import type { RequestClass, EventClass } from "./link";
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
"use strict";
|
||||
import winston from "winston";
|
||||
import Transport from "winston-transport";
|
||||
import { LEVEL, MESSAGE } from "triple-beam";
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
* @author Hornwitser
|
||||
* @module lib/logging_utils
|
||||
*/
|
||||
"use strict";
|
||||
import fs from "fs-extra";
|
||||
import path from "path";
|
||||
import stream from "stream";
|
||||
|
||||
@@ -120,6 +120,7 @@ export class BaseInstancePlugin {
|
||||
message: string,
|
||||
expectEmpty: boolean,
|
||||
}[] = [];
|
||||
|
||||
private _sendingRconMessages = false;
|
||||
|
||||
constructor(
|
||||
@@ -655,7 +656,6 @@ export class BaseControlPlugin {
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Plugin supplied login form
|
||||
*/
|
||||
|
||||
@@ -89,7 +89,7 @@ export async function loadControllerPluginClass(
|
||||
|
||||
/**
|
||||
* Load instance plugin class of a plugin
|
||||
|
||||
*
|
||||
* @param pluginInfo -
|
||||
* Plugin info object returned from {@link
|
||||
* loadPluginInfos} to load class from.
|
||||
|
||||
@@ -1188,7 +1188,7 @@ class HistogramChild {
|
||||
protected _bucketValues: Map<string, number>;
|
||||
protected _sumValues: Map<string, number>;
|
||||
protected _countValues: Map<string, number>;
|
||||
protected _key: string
|
||||
protected _key: string;
|
||||
protected _bucketKeys: Map<number, string>;
|
||||
|
||||
constructor(collector: Histogram, key: string) {
|
||||
@@ -1438,6 +1438,7 @@ export class Histogram extends LabeledCollector<HistogramChild> {
|
||||
* Observe a given value and increment matching buckets
|
||||
*
|
||||
* Note: Only works if this is an unlabeled collector.
|
||||
* @param value - number to count into histogram buckets.
|
||||
*/
|
||||
observe(value: number) {
|
||||
this._defaultChild!.observe(value);
|
||||
|
||||
@@ -22,13 +22,13 @@ export function compile<T>(schema: JSONSchemaType<T>) {
|
||||
}
|
||||
|
||||
let doValidate: ValidateFunction<T>;
|
||||
const validate = <ValidateFunction<T>>function (data) {
|
||||
const validate = <ValidateFunction<T>> function (data) {
|
||||
if (!doValidate) {
|
||||
doValidate = ajv.compile(schema);
|
||||
}
|
||||
let result = doValidate(data);
|
||||
validate.errors = doValidate.errors;
|
||||
return result;
|
||||
}
|
||||
return validate
|
||||
};
|
||||
return validate;
|
||||
}
|
||||
@@ -2,7 +2,6 @@
|
||||
* Implementation of commands shared between controller/host/ctl.
|
||||
* @module lib/shared_commands
|
||||
*/
|
||||
"use strict";
|
||||
import path from "path";
|
||||
|
||||
import * as libConfig from "./config";
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
"use strict";
|
||||
import assert from "assert";
|
||||
import fs from "fs-extra";
|
||||
import stream from "stream";
|
||||
|
||||
@@ -33,16 +33,15 @@ export class SubscriptionResponse {
|
||||
Type.String(),
|
||||
Type.Unknown(),
|
||||
]),
|
||||
Type.Null()
|
||||
])
|
||||
Type.Null(),
|
||||
]);
|
||||
|
||||
toJSON() {
|
||||
if (this.eventReplay) {
|
||||
const entry = Link._eventsByClass.get(this.eventReplay.constructor)!;
|
||||
return [entry.name, this.eventReplay];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static fromJSON(json: Static<typeof SubscriptionResponse.jsonSchema>): SubscriptionResponse {
|
||||
@@ -67,7 +66,7 @@ export class SubscriptionResponse {
|
||||
export class SubscriptionRequest {
|
||||
declare ["constructor"]: typeof SubscriptionRequest;
|
||||
static type = "request" as const;
|
||||
static src = ["control", "instance"] as const;
|
||||
static src = ["control", "instance"] as const;
|
||||
static dst = "controller" as const;
|
||||
static Response = SubscriptionResponse;
|
||||
static permission(user: User, message: MessageRequest) {
|
||||
@@ -100,7 +99,7 @@ export class SubscriptionRequest {
|
||||
Type.Boolean(),
|
||||
Type.Array(Type.Union([Type.String(), Type.Number()])),
|
||||
Type.Number(),
|
||||
])
|
||||
]);
|
||||
|
||||
toJSON() {
|
||||
return [this.eventName, this.allChannels, this.channels, this.lastRequestTime];
|
||||
@@ -130,6 +129,9 @@ export class SubscriptionController {
|
||||
/**
|
||||
* Allow clients to subscribe to an event by telling the subscription controller to accept them
|
||||
* Has an optional subscription update handler which is called when any client updates their subscription
|
||||
* @param Event - Event class which is sent out as updates.
|
||||
* @param subscriptionUpdate -
|
||||
* Optional handler called when a client updates its subscriptions.
|
||||
*/
|
||||
handle<T>(Event: EventClass<T>, subscriptionUpdate?: SubscriptionRequestHandler<T>) {
|
||||
const entry = Link._eventsByClass.get(Event);
|
||||
@@ -147,6 +149,7 @@ export class SubscriptionController {
|
||||
|
||||
/**
|
||||
* Broadcast an event to all subscribers of that event, will be filtered by channel if a ChannelEvent is provided
|
||||
* @param event - Event to broadcast.
|
||||
*/
|
||||
broadcast<T>(event: Event<T> | ChannelEvent<T>) {
|
||||
const entry = Link._eventsByClass.get(event.constructor);
|
||||
@@ -169,6 +172,11 @@ export class SubscriptionController {
|
||||
|
||||
/**
|
||||
* Handle incoming event subscription requests
|
||||
* @param event - incomming event.
|
||||
* @param src - Source address of incomming request.
|
||||
* @param dst - destination address of incomming request.
|
||||
* @returns Response to subscription request.
|
||||
* @internal
|
||||
*/
|
||||
async _handleRequest(event: SubscriptionRequest, src: Address, dst: Address) {
|
||||
if (!Link._eventsByName.has(event.eventName)) {
|
||||
@@ -192,7 +200,9 @@ export class SubscriptionController {
|
||||
onceClose = () => eventData.subscriptions.delete(link);
|
||||
link.connector.once("close", onceClose);
|
||||
}
|
||||
eventData.subscriptions.set(link, { all: event.allChannels, channels: event.channels, onceClose: onceClose });
|
||||
eventData.subscriptions.set(
|
||||
link, { all: event.allChannels, channels: event.channels, onceClose: onceClose },
|
||||
);
|
||||
}
|
||||
return new SubscriptionResponse(eventReplay);
|
||||
}
|
||||
@@ -204,15 +214,15 @@ export class SubscriptionController {
|
||||
* Multiple handlers can be subscribed at the same time, on the same or different channels
|
||||
*/
|
||||
export class EventSubscriber<T, V=T> {
|
||||
_callbacks = new Array<EventSubscriberCallback<V>>()
|
||||
_channelCallbacks = new Map<string | number, Array<EventSubscriberCallback<V>>>()
|
||||
_callbacks = new Array<EventSubscriberCallback<V>>();
|
||||
_channelCallbacks = new Map<string | number, Array<EventSubscriberCallback<V>>>();
|
||||
lastResponse: Event<T> | null = null;
|
||||
lastResponseTime = 0;
|
||||
control?: Link
|
||||
control?: Link;
|
||||
|
||||
constructor(
|
||||
private event: EventClass<T> | ChannelEventClass<T>,
|
||||
private prehandler?: (event: T) => V,
|
||||
private prehandler?: (e: T) => V,
|
||||
control?: Link
|
||||
) {
|
||||
const entry = Link._eventsByClass.get(this.event);
|
||||
@@ -226,6 +236,8 @@ export class EventSubscriber<T, V=T> {
|
||||
|
||||
/**
|
||||
* Handle incoming events and distribute it to the correct callbacks
|
||||
* @param response - event from subscribed resource
|
||||
* @internal
|
||||
*/
|
||||
async _handle(response: Event<T> | ChannelEvent<T>) {
|
||||
this.lastResponse = response;
|
||||
@@ -246,10 +258,14 @@ export class EventSubscriber<T, V=T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* If a control link was not connected at creation, it can be connected here (normally during onControllerConnectionEvent)
|
||||
* If a control link was not connected at creation, it can be connected
|
||||
* here (normally during onControllerConnectionEvent)
|
||||
* @param control - Control link to associate with.
|
||||
*/
|
||||
async connectControl(control: Link) {
|
||||
if (this.control === control) return;
|
||||
if (this.control === control) {
|
||||
return;
|
||||
}
|
||||
this.control = control;
|
||||
this.control.handle(this.event, this._handle.bind(this));
|
||||
await this._updateSubscription();
|
||||
@@ -257,6 +273,8 @@ export class EventSubscriber<T, V=T> {
|
||||
|
||||
/**
|
||||
* Subscribe to receive all event notifications
|
||||
* @param handler -
|
||||
* callback invoked whenever the subscribed resource changes.
|
||||
*/
|
||||
async subscribe(handler: EventSubscriberCallback<V>) {
|
||||
this._callbacks.push(handler);
|
||||
@@ -265,6 +283,10 @@ export class EventSubscriber<T, V=T> {
|
||||
|
||||
/**
|
||||
* Subscribe to receive event notifications for the given channel
|
||||
* @param channel - Channel to subscribe to.
|
||||
* @param handler -
|
||||
* callback invoked whenever the subscribed resource changes on the
|
||||
* given channel.
|
||||
*/
|
||||
async subscribeToChannel(channel: string | number, handler: EventSubscriberCallback<V>) {
|
||||
if (!this._channelCallbacks.get(channel)) {
|
||||
@@ -275,8 +297,14 @@ export class EventSubscriber<T, V=T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Unsubscribe from receiving all event notifications, does not effect the subscriptions of other handlers
|
||||
* Will throw an error if your handler is not subscribed to all channels, does not accept anonymous functions
|
||||
* Unsubscribe from receiving all event notifications, does not effect
|
||||
* the subscriptions of other handlers.
|
||||
*
|
||||
* @param handler - A callback previously passed to {@link subscribe}
|
||||
*
|
||||
* @throws {Error}
|
||||
* If your handler is not subscribed to all channels, does not
|
||||
* accept anonymous functions
|
||||
*/
|
||||
async unsubscribe(handler: EventSubscriberCallback<V>) {
|
||||
let index = this._callbacks.lastIndexOf(handler);
|
||||
@@ -289,8 +317,15 @@ export class EventSubscriber<T, V=T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Unsubscribe from receiving event notifications for a given channel, does not effect the subscriptions of other handlers
|
||||
* Will throw an error if your handler is not subscribed to the channel, does not accept anonymous functions
|
||||
* Unsubscribe from receiving event notifications for a given channel,
|
||||
* does not effect the subscriptions of other handlers.
|
||||
*
|
||||
* @param channel - A channel previously passed to {@link subscribeToChannel}
|
||||
* @param handler - A callback previously passed to {@link subscribeToChannel}
|
||||
*
|
||||
* @throws {Error}
|
||||
* If your handler is not subscribed to the channel or does not
|
||||
* accept anonymous functions
|
||||
*/
|
||||
async unsubscribeFromChannel(channel: string | number, handler: EventSubscriberCallback<V>) {
|
||||
const channelCallbacks = this._channelCallbacks.get(channel);
|
||||
@@ -315,7 +350,9 @@ export class EventSubscriber<T, V=T> {
|
||||
* Update the subscription with the controller based on current handler counts
|
||||
*/
|
||||
async _updateSubscription() {
|
||||
if (!this.control || !(this.control.connector as WebSocketClientConnector).connected) return;
|
||||
if (!this.control || !(this.control.connector as WebSocketClientConnector).connected) {
|
||||
return;
|
||||
}
|
||||
const entry = Link._eventsByClass.get(this.event)!;
|
||||
|
||||
const response = await this.control.send(new SubscriptionRequest(
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
* @author Hornwitser
|
||||
* @module lib/users
|
||||
*/
|
||||
"use strict";
|
||||
import * as libErrors from "./errors";
|
||||
|
||||
import PlayerStats from "./PlayerStats";
|
||||
@@ -328,6 +327,7 @@ export class User {
|
||||
}
|
||||
return playerStats;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set of users currently online in the cluster.
|
||||
*/
|
||||
|
||||
Reference in new issue
Block a user