mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-20 15:43:13 +09:00
Improve typing of various controller entities
This commit is contained in:
1 parent
8b0d7a67b5
commit
94cb607930
5 files changed
+58
-44
No files matched your search
@@ -13,7 +13,7 @@ import ControllerRouter from "./ControllerRouter";
|
|||||||
* @alias module:controller/src/BaseConnection
|
* @alias module:controller/src/BaseConnection
|
||||||
*/
|
*/
|
||||||
export default class BaseConnection extends lib.Link {
|
export default class BaseConnection extends lib.Link {
|
||||||
declare connector: lib.WebSocketBaseConnector;
|
declare ["connector"]: WsServerConnector;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
connector: lib.WebSocketBaseConnector,
|
connector: lib.WebSocketBaseConnector,
|
||||||
|
|||||||
@@ -88,9 +88,9 @@ export default class ControlConnection extends BaseConnection {
|
|||||||
this.handle(lib.InstanceConfigSetFieldRequest, this.handleInstanceConfigSetFieldRequest.bind(this));
|
this.handle(lib.InstanceConfigSetFieldRequest, this.handleInstanceConfigSetFieldRequest.bind(this));
|
||||||
this.handle(lib.InstanceConfigSetPropRequest, this.handleInstanceConfigSetPropRequest.bind(this));
|
this.handle(lib.InstanceConfigSetPropRequest, this.handleInstanceConfigSetPropRequest.bind(this));
|
||||||
this.handle(lib.InstanceAssignRequest, this.handleInstanceAssignRequest.bind(this));
|
this.handle(lib.InstanceAssignRequest, this.handleInstanceAssignRequest.bind(this));
|
||||||
this.handle(lib.InstanceRenameSaveRequest, this._controller.sendToHostByInstanceId.bind(this._controller));
|
this.handle(lib.InstanceRenameSaveRequest, controller.sendRequestToHostByInstanceId.bind(controller));
|
||||||
this.handle(lib.InstanceCopySaveRequest, this._controller.sendToHostByInstanceId.bind(this._controller));
|
this.handle(lib.InstanceCopySaveRequest, controller.sendRequestToHostByInstanceId.bind(controller));
|
||||||
this.handle(lib.InstanceDeleteSaveRequest, this._controller.sendToHostByInstanceId.bind(this._controller));
|
this.handle(lib.InstanceDeleteSaveRequest, controller.sendRequestToHostByInstanceId.bind(controller));
|
||||||
this.handle(lib.InstanceDownloadSaveRequest, this.handleInstanceDownloadSaveRequest.bind(this));
|
this.handle(lib.InstanceDownloadSaveRequest, this.handleInstanceDownloadSaveRequest.bind(this));
|
||||||
this.handle(lib.InstanceTransferSaveRequest, this.handleInstanceTransferSaveRequest.bind(this));
|
this.handle(lib.InstanceTransferSaveRequest, this.handleInstanceTransferSaveRequest.bind(this));
|
||||||
this.handle(lib.ModPackListRequest, this.handleModPackListRequest.bind(this));
|
this.handle(lib.ModPackListRequest, this.handleModPackListRequest.bind(this));
|
||||||
@@ -576,7 +576,7 @@ export default class ControlConnection extends BaseConnection {
|
|||||||
stream.filename = mod.filename;
|
stream.filename = mod.filename;
|
||||||
stream.source = fs.createReadStream(modPath);
|
stream.source = fs.createReadStream(modPath);
|
||||||
stream.mime = "application/zip";
|
stream.mime = "application/zip";
|
||||||
stream.size = mod.size;
|
stream.size = String(mod.size);
|
||||||
|
|
||||||
return stream.id;
|
return stream.id;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,6 +36,8 @@ const logSizeGauge = new Gauge(
|
|||||||
"Size of all log files currently stored on the controller."
|
"Size of all log files currently stored on the controller."
|
||||||
);
|
);
|
||||||
|
|
||||||
|
type InstanceId = { instanceId: number };
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Manages all controller related operations
|
* Manages all controller related operations
|
||||||
* @alias module:controller/src/Controller
|
* @alias module:controller/src/Controller
|
||||||
@@ -1207,17 +1209,19 @@ export default class Controller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendToHostByInstanceId(requestOrEvent: any) {
|
async sendToHostByInstanceId<Req extends InstanceId, Res>(request: Req & lib.Request<Req, Res>): Promise<Res>;
|
||||||
|
sendToHostByInstanceId<T extends InstanceId>(event: T & lib.Event<T>): void;
|
||||||
|
async sendToHostByInstanceId(requestOrEvent: lib.Request<unknown, unknown> | lib.Event<unknown>) {
|
||||||
if (requestOrEvent.constructor.type === "request") {
|
if (requestOrEvent.constructor.type === "request") {
|
||||||
return this.sendRequestToHostByInstanceId(requestOrEvent);
|
return this.sendRequestToHostByInstanceId(requestOrEvent as any);
|
||||||
}
|
}
|
||||||
if (requestOrEvent.constructor.type === "event") {
|
if (requestOrEvent.constructor.type === "event") {
|
||||||
return this.sendEventToHostByInstanceId(requestOrEvent);
|
return this.sendEventToHostByInstanceId(requestOrEvent as any);
|
||||||
}
|
}
|
||||||
throw Error(`Unknown type ${requestOrEvent.constructor.type}.`);
|
throw Error(`Unknown type ${(requestOrEvent.constructor as any).type}.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendRequestToHostByInstanceId(request: any) {
|
async sendRequestToHostByInstanceId<Req extends InstanceId, Res>(request: Req & lib.Request<Req, Res>) {
|
||||||
let instance = this.getRequestInstance(request.instanceId);
|
let instance = this.getRequestInstance(request.instanceId);
|
||||||
let hostId = instance.config.get("instance.assigned_host");
|
let hostId = instance.config.get("instance.assigned_host");
|
||||||
if (hostId === null) {
|
if (hostId === null) {
|
||||||
@@ -1230,7 +1234,7 @@ export default class Controller {
|
|||||||
return await connection.send(request);
|
return await connection.send(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
sendEventToHostByInstanceId(event: any) {
|
sendEventToHostByInstanceId<T extends { instanceId: number }>(event: T & lib.Event<T>) {
|
||||||
let instance = this.getRequestInstance(event.instanceId);
|
let instance = this.getRequestInstance(event.instanceId);
|
||||||
let hostId = instance.config.get("instance.assigned_host");
|
let hostId = instance.config.get("instance.assigned_host");
|
||||||
if (hostId === null) {
|
if (hostId === null) {
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ export interface HostInfo {
|
|||||||
* @alias module:controller/src/HostConnection
|
* @alias module:controller/src/HostConnection
|
||||||
*/
|
*/
|
||||||
export default class HostConnection extends BaseConnection {
|
export default class HostConnection extends BaseConnection {
|
||||||
declare connector: WsServerConnector;
|
|
||||||
private _agent: any;
|
private _agent: any;
|
||||||
private _id: any;
|
private _id: any;
|
||||||
private _name: any;
|
private _name: any;
|
||||||
|
|||||||
@@ -16,6 +16,17 @@ const { logger } = lib;
|
|||||||
|
|
||||||
const finished = util.promisify(nodeStream.finished);
|
const finished = util.promisify(nodeStream.finished);
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-namespace
|
||||||
|
namespace Express {
|
||||||
|
export interface Locals {
|
||||||
|
controller: Controller,
|
||||||
|
mainBundle: string,
|
||||||
|
devPlugins: Map<string, number>,
|
||||||
|
streams: Map<string, ProxyStream>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Merges samples from sourceResult to destinationResult
|
// Merges samples from sourceResult to destinationResult
|
||||||
function mergeSamples(destinationResult: lib.CollectorResultSerialized, sourceResult: lib.CollectorResultSerialized) {
|
function mergeSamples(destinationResult: lib.CollectorResultSerialized, sourceResult: lib.CollectorResultSerialized) {
|
||||||
@@ -95,14 +106,14 @@ async function getMetrics(req: Request, res: Response, next: any) {
|
|||||||
|
|
||||||
function getPlugins(req: Request, res: Response) {
|
function getPlugins(req: Request, res: Response) {
|
||||||
let plugins: lib.PluginWebApi[] = [];
|
let plugins: lib.PluginWebApi[] = [];
|
||||||
for (let pluginInfo of (req.app.locals.controller as Controller).pluginInfos) {
|
for (let pluginInfo of req.app.locals.controller.pluginInfos) {
|
||||||
let name = pluginInfo.name;
|
let name = pluginInfo.name;
|
||||||
let loaded = req.app.locals.controller.plugins.has(name);
|
let loaded = req.app.locals.controller.plugins.has(name);
|
||||||
let enabled = loaded && req.app.locals.controller.config.group(pluginInfo.name).get("load_plugin");
|
let enabled = loaded && req.app.locals.controller.config.group(pluginInfo.name).get("load_plugin") as boolean;
|
||||||
let web: { main:any, error:string|undefined } = { main: undefined, error: undefined};
|
let web: { main:any, error:string|undefined } = { main: undefined, error: undefined};
|
||||||
let devPlugins = req.app.locals.devPlugins;
|
let devPlugins = req.app.locals.devPlugins;
|
||||||
if (devPlugins && devPlugins.has(name)) {
|
if (devPlugins && devPlugins.has(name)) {
|
||||||
let stats = res.locals.webpack.devMiddleware.stats.stats[devPlugins.get(name)];
|
let stats = res.locals.webpack.devMiddleware.stats.stats[devPlugins.get(name)!];
|
||||||
web.main = stats.toJson().assetsByChunkName[name];
|
web.main = stats.toJson().assetsByChunkName[name];
|
||||||
} else if (pluginInfo.manifest) {
|
} else if (pluginInfo.manifest) {
|
||||||
web.main = pluginInfo.manifest[`${pluginInfo.name}.js`];
|
web.main = pluginInfo.manifest[`${pluginInfo.name}.js`];
|
||||||
@@ -137,7 +148,7 @@ function validateHostToken(req: Request, res: Response, next: any) {
|
|||||||
if (typeof tokenPayload === "string") {
|
if (typeof tokenPayload === "string") {
|
||||||
throw new Error("unexpected JsonWebToken type");
|
throw new Error("unexpected JsonWebToken type");
|
||||||
}
|
}
|
||||||
let host = (req.app.locals.controller as Controller).hosts.get(tokenPayload.host);
|
let host = req.app.locals.controller.hosts.get(tokenPayload.host);
|
||||||
if (!host) {
|
if (!host) {
|
||||||
throw new Error("invalid host");
|
throw new Error("invalid host");
|
||||||
}
|
}
|
||||||
@@ -251,12 +262,12 @@ async function uploadExport(req: Request, res: Response) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
interface ProxyStream {
|
export interface ProxyStream {
|
||||||
id: string;
|
id: string;
|
||||||
source: nodeStream.Readable | null;
|
source: nodeStream.Readable | null;
|
||||||
flowing: boolean;
|
flowing: boolean;
|
||||||
size: number | null;
|
size?: string;
|
||||||
mime: string | null;
|
mime?: string;
|
||||||
filename: string | null;
|
filename: string | null;
|
||||||
events: events.EventEmitter;
|
events: events.EventEmitter;
|
||||||
timeout: ReturnType<typeof setTimeout>;
|
timeout: ReturnType<typeof setTimeout>;
|
||||||
@@ -269,8 +280,8 @@ export async function createProxyStream(app: Application): Promise<ProxyStream>
|
|||||||
id,
|
id,
|
||||||
source: null,
|
source: null,
|
||||||
flowing: false,
|
flowing: false,
|
||||||
size: null,
|
size: undefined,
|
||||||
mime: null,
|
mime: undefined,
|
||||||
filename: null,
|
filename: null,
|
||||||
events: new events.EventEmitter(),
|
events: new events.EventEmitter(),
|
||||||
timeout: setTimeout(() => {
|
timeout: setTimeout(() => {
|
||||||
@@ -289,7 +300,7 @@ export async function createProxyStream(app: Application): Promise<ProxyStream>
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function putStream(req: Request, res: Response) {
|
async function putStream(req: Request, res: Response) {
|
||||||
let stream = req.app.locals.streams.get(req.params.id);
|
const stream = req.app.locals.streams.get(req.params.id);
|
||||||
if (!stream || stream.source) {
|
if (!stream || stream.source) {
|
||||||
res.sendStatus(404);
|
res.sendStatus(404);
|
||||||
return;
|
return;
|
||||||
@@ -309,35 +320,35 @@ async function putStream(req: Request, res: Response) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function startStream(res: Response, stream: ProxyStream) {
|
||||||
|
res.append("Content-Type", stream.mime);
|
||||||
|
if (stream.size) {
|
||||||
|
res.append("Content-Length", stream.size);
|
||||||
|
}
|
||||||
|
if (stream.filename) {
|
||||||
|
res.append("Content-Disposition", `attachment; filename="${stream.filename}"`);
|
||||||
|
} else {
|
||||||
|
res.append("Content-Disposition", "attachment");
|
||||||
|
}
|
||||||
|
stream.source!.pipe(res);
|
||||||
|
stream.flowing = true;
|
||||||
|
res.on("close", () => {
|
||||||
|
stream.events.emit("close");
|
||||||
|
});
|
||||||
|
clearTimeout(stream.timeout);
|
||||||
|
}
|
||||||
|
|
||||||
async function getStream(req: Request, res: Response) {
|
async function getStream(req: Request, res: Response) {
|
||||||
let stream = req.app.locals.streams.get(req.params.id);
|
const stream = req.app.locals.streams.get(req.params.id);
|
||||||
if (!stream || stream.flowing) {
|
if (!stream || stream.flowing) {
|
||||||
res.sendStatus(404);
|
res.sendStatus(404);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
function startStream() {
|
|
||||||
res.append("Content-Type", stream.mime);
|
|
||||||
if (stream.size) {
|
|
||||||
res.append("Content-Length", stream.size);
|
|
||||||
}
|
|
||||||
if (stream.filename) {
|
|
||||||
res.append("Content-Disposition", `attachment; filename="${stream.filename}"`);
|
|
||||||
} else {
|
|
||||||
res.append("Content-Disposition", "attachment");
|
|
||||||
}
|
|
||||||
stream.source.pipe(res);
|
|
||||||
stream.flowing = true;
|
|
||||||
res.on("close", () => {
|
|
||||||
stream.events.emit("close");
|
|
||||||
});
|
|
||||||
clearTimeout(stream.timeout);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stream.source) {
|
if (stream.source) {
|
||||||
startStream();
|
startStream(res, stream);
|
||||||
} else {
|
} else {
|
||||||
stream.events.on("source", startStream);
|
stream.events.on("source", () => startStream(res, stream));
|
||||||
stream.events.on("timeout", () => {
|
stream.events.on("timeout", () => {
|
||||||
res.sendStatus(500);
|
res.sendStatus(500);
|
||||||
});
|
});
|
||||||
@@ -377,7 +388,7 @@ async function uploadSave(req: Request, res: Response) {
|
|||||||
let proxyStream = await createProxyStream(req.app);
|
let proxyStream = await createProxyStream(req.app);
|
||||||
proxyStream.source = stream;
|
proxyStream.source = stream;
|
||||||
proxyStream.mime = streamMime;
|
proxyStream.mime = streamMime;
|
||||||
let timeout = new Promise((_, reject) => {
|
let timeout = new Promise<never>((_, reject) => {
|
||||||
proxyStream.events.on("timeout", () => {
|
proxyStream.events.on("timeout", () => {
|
||||||
stream.resume();
|
stream.resume();
|
||||||
reject(new Error("Timed out establishing stream to host"));
|
reject(new Error("Timed out establishing stream to host"));
|
||||||
|
|||||||
Reference in new issue
Block a user