mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-17 15:33:12 +09:00
Merge remote-tracking branch 'origin/master' into fix/headless-export-icons
This commit is contained in:
114 files changed
+5338
-1165
No files matched your search
@@ -1308,6 +1308,16 @@ end`.replace(/\r?\n/g, " ");
|
||||
}
|
||||
}
|
||||
|
||||
/** Stop the instance once a start, stop or save in progress has finished */
|
||||
async _stopWhenSettled() {
|
||||
// Stopping concurrently with these races them and asserts on the server state.
|
||||
// Checked right before stop() as another waiter may have started stopping.
|
||||
while (transitoryStatuses.has(this._status)) {
|
||||
await this._waitForSettledStatus();
|
||||
}
|
||||
await this.stop();
|
||||
}
|
||||
|
||||
async kill() {
|
||||
if (this._status === "stopped") {
|
||||
return;
|
||||
@@ -1494,7 +1504,7 @@ end`.replace(/\r?\n/g, " ");
|
||||
}
|
||||
|
||||
async handleInstanceRestartRequest(request: lib.InstanceRestartRequest) {
|
||||
await this.stop();
|
||||
await this._stopWhenSettled();
|
||||
// Stopping invalidates this instance object, a new one must be created by the host
|
||||
// To do this we simulate a waking an instance and sending a start request
|
||||
const instanceConnection = await this._host._connectInstance(this.id);
|
||||
@@ -1502,7 +1512,7 @@ end`.replace(/\r?\n/g, " ");
|
||||
}
|
||||
|
||||
async handleInstanceStopRequest() {
|
||||
await this.stop();
|
||||
await this._stopWhenSettled();
|
||||
}
|
||||
|
||||
async handleInstanceKillRequest() {
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
// Export of item icons and locale
|
||||
import fs from "node:fs/promises";
|
||||
import path from "path";
|
||||
import { Jimp, JimpMime, type JimpInstance } from "jimp";
|
||||
import { createJimp } from "@jimp/core";
|
||||
import png from "@jimp/js-png";
|
||||
import * as crop from "@jimp/plugin-crop";
|
||||
import * as resize from "@jimp/plugin-resize";
|
||||
import type {} from "@jimp/types"; // needed to name jimp types in export.d.ts
|
||||
import yazl from "yazl";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
@@ -160,7 +164,9 @@ async function loadFile(
|
||||
return await modZip.zip.readFile(path.posix.join(modZip.root, filePath));
|
||||
}
|
||||
|
||||
type Image = JimpInstance;
|
||||
// Only the parts of jimp the export uses
|
||||
const Jimp = createJimp({ formats: [png], plugins: [crop.methods, resize.methods] });
|
||||
type Image = InstanceType<typeof Jimp>;
|
||||
type IconCache = Map<string, Image | null>;
|
||||
async function loadIcon(
|
||||
server: FactorioServer,
|
||||
@@ -423,7 +429,13 @@ async function exportIcons(
|
||||
state.metadata[baseName] ??= metadata;
|
||||
let count = 0;
|
||||
for (let prototype of prototypes) {
|
||||
const icon = await exportIcon(server, modVersions, files, state, prototype);
|
||||
let icon: lib.ExportMetadataEntry["icon"];
|
||||
try {
|
||||
icon = await exportIcon(server, modVersions, files, state, prototype);
|
||||
} catch (err: any) {
|
||||
// One broken icon should not stop the rest of the export
|
||||
server._logger.warn(`Skipping icon of ${prototype.type} ${prototype.name}: ${err.message}`);
|
||||
}
|
||||
if (icon) {
|
||||
count += 1;
|
||||
}
|
||||
@@ -494,6 +506,7 @@ async function exportLocale(
|
||||
modZip = await loadZip(server, modVersions, zipCache, mod);
|
||||
} catch (err: any) {
|
||||
if (err.code === "ENOENT") {
|
||||
server._logger.warn(`Skipping locale of ${mod}, its mod zip was not found`);
|
||||
continue;
|
||||
}
|
||||
throw err;
|
||||
@@ -673,7 +686,7 @@ async function buildExport(server: FactorioServer, files: ExportFiles) {
|
||||
// Crop sheet to actual used height and write single spritesheet + metadata.
|
||||
const usedRows = Math.ceil(state.pos / (SHEET_WIDTH / SHEET_ICON_SIZE));
|
||||
state.sheet.crop({ x: 0, y: 0, w: SHEET_WIDTH, h: Math.max(usedRows * SHEET_ICON_SIZE, 1) });
|
||||
zip.addBuffer(await state.sheet.getBuffer(JimpMime.png), "export/spritesheet.png");
|
||||
zip.addBuffer(await state.sheet.getBuffer("image/png"), "export/spritesheet.png");
|
||||
addJson("export/metadata.json", state.metadata satisfies lib.ExportMetadata);
|
||||
zip.end();
|
||||
|
||||
@@ -684,6 +697,8 @@ async function buildExport(server: FactorioServer, files: ExportFiles) {
|
||||
|
||||
|
||||
// For testing only
|
||||
export const _createSheetState = createSheetState;
|
||||
export const _exportIcons = exportIcons;
|
||||
export const _exportLocale = exportLocale;
|
||||
export const _loadSimpleIcon = loadSimpleIcon;
|
||||
export const _loadLayeredIcon = loadLayeredIcon;
|
||||
|
||||
@@ -0,0 +1,305 @@
|
||||
import { setTimeout as sleep } from "node:timers/promises";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { logger } from "@clusterio/lib";
|
||||
|
||||
/** Time to wait for a response from the controller */
|
||||
const requestTimeoutMs = 30e3;
|
||||
/** Placeholder token of a host that has not been paired */
|
||||
const unpairedToken = "enter token here";
|
||||
|
||||
/** Error responded by the controller */
|
||||
export class PairingError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
/** HTTP status the controller responded with */
|
||||
public status?: number,
|
||||
) {
|
||||
super(message);
|
||||
this.name = "PairingError";
|
||||
}
|
||||
}
|
||||
|
||||
export interface PairingResult {
|
||||
hostId: number;
|
||||
token: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface PairHostOptions {
|
||||
/** URL of the controller, ending with a slash */
|
||||
controllerUrl: string;
|
||||
/** Name to request for the host */
|
||||
name: string;
|
||||
/** Version of the host, shown with the request */
|
||||
version?: string;
|
||||
/** Token of a user allowed to approve hosts, approves the host without waiting */
|
||||
token?: string;
|
||||
/** Interval to poll for the result at */
|
||||
pollIntervalMs?: number;
|
||||
/** Delay before trying again when the controller can't be reached or is busy */
|
||||
retryDelayMs?: number;
|
||||
/** Called with the code to approve the request with each time a request is made */
|
||||
onPending?: (code: string) => void;
|
||||
/** Called when the controller can't be reached or is busy and the request is retried */
|
||||
onRetry?: (err: Error, delayMs: number) => void;
|
||||
/** Signal to cancel pairing with */
|
||||
signal?: AbortSignal;
|
||||
}
|
||||
|
||||
interface Response {
|
||||
status: number;
|
||||
headers: Headers;
|
||||
json: any;
|
||||
}
|
||||
|
||||
async function request(
|
||||
controllerUrl: string,
|
||||
route: string,
|
||||
options: { body?: object, token?: string, signal?: AbortSignal, timeoutMs?: number },
|
||||
): Promise<Response> {
|
||||
const headers: Record<string, string> = { "Accept": "application/json" };
|
||||
if (options.body) {
|
||||
headers["Content-Type"] = "application/json";
|
||||
}
|
||||
if (options.token) {
|
||||
headers["Authorization"] = `Bearer ${options.token}`;
|
||||
}
|
||||
const timeout = AbortSignal.timeout(options.timeoutMs ?? requestTimeoutMs);
|
||||
const response = await fetch(new URL(route, controllerUrl), {
|
||||
method: options.body ? "POST" : "GET",
|
||||
headers,
|
||||
body: options.body ? JSON.stringify(options.body) : undefined,
|
||||
signal: options.signal ? AbortSignal.any([options.signal, timeout]) : timeout,
|
||||
});
|
||||
let json;
|
||||
try {
|
||||
json = await response.json();
|
||||
} catch {
|
||||
json = undefined;
|
||||
}
|
||||
return { status: response.status, headers: response.headers, json };
|
||||
}
|
||||
|
||||
function responseError(response: Response) {
|
||||
const message = typeof response.json?.error === "string"
|
||||
? response.json.error
|
||||
: `Unexpected response from controller: ${response.status}`;
|
||||
return new PairingError(message, response.status);
|
||||
}
|
||||
|
||||
function pairingResult(response: Response): PairingResult {
|
||||
const { hostId, token, name } = response.json ?? {};
|
||||
if (!Number.isInteger(hostId) || typeof token !== "string" || typeof name !== "string") {
|
||||
throw new PairingError("Invalid response from controller", response.status);
|
||||
}
|
||||
return { hostId, token, name };
|
||||
}
|
||||
|
||||
/** Responses that are worth trying again later */
|
||||
function isTemporary(status: number) {
|
||||
return status === 429 || status >= 500;
|
||||
}
|
||||
|
||||
function retryDelay(response: Response, defaultMs: number) {
|
||||
const seconds = Number(response.headers.get("retry-after"));
|
||||
return Number.isFinite(seconds) && seconds > 0 ? seconds * 1000 : defaultMs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Poll a request until it is approved, rejected or no longer known by the controller
|
||||
*
|
||||
* @returns the result if approved, or null if the controller no longer knows the request.
|
||||
*/
|
||||
async function poll(
|
||||
requestId: string,
|
||||
options: Required<Pick<PairHostOptions, "pollIntervalMs" | "retryDelayMs">> & PairHostOptions,
|
||||
) {
|
||||
let delayMs = options.pollIntervalMs;
|
||||
while (true) {
|
||||
await sleep(delayMs, undefined, { signal: options.signal });
|
||||
delayMs = options.pollIntervalMs;
|
||||
let response;
|
||||
try {
|
||||
response = await request(options.controllerUrl, "api/host-pairing/poll", {
|
||||
body: { requestId }, signal: options.signal,
|
||||
});
|
||||
} catch (err: any) {
|
||||
if (options.signal?.aborted) {
|
||||
throw err;
|
||||
}
|
||||
// Keep polling through network errors, the request is made again if it expired meanwhile.
|
||||
delayMs = options.retryDelayMs;
|
||||
options.onRetry?.(err, delayMs);
|
||||
continue;
|
||||
}
|
||||
if (response.status === 200) {
|
||||
return pairingResult(response);
|
||||
}
|
||||
if (response.status === 404) {
|
||||
return null;
|
||||
}
|
||||
if (isTemporary(response.status)) {
|
||||
delayMs = retryDelay(response, options.retryDelayMs);
|
||||
options.onRetry?.(responseError(response), delayMs);
|
||||
continue;
|
||||
}
|
||||
if (response.status !== 202) {
|
||||
throw responseError(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Request to join the cluster and wait until the request is approved
|
||||
*
|
||||
* Waits through network errors and controller restarts, making a new request
|
||||
* if the controller no longer knows the previous one.
|
||||
*
|
||||
* @param options - Pairing options.
|
||||
* @returns id, token and approved name of the host.
|
||||
* @throws {PairingError} if the controller refuses or rejects the request.
|
||||
* @throws {DOMException} AbortError if the signal is aborted.
|
||||
*/
|
||||
export async function pairHost(options: PairHostOptions): Promise<PairingResult> {
|
||||
const opts = { pollIntervalMs: 2000, retryDelayMs: 10e3, version: "", ...options };
|
||||
let requestId: string | undefined;
|
||||
try {
|
||||
while (true) {
|
||||
opts.signal?.throwIfAborted();
|
||||
let response;
|
||||
try {
|
||||
response = await request(opts.controllerUrl, "api/host-pairing", {
|
||||
body: { name: opts.name, version: opts.version }, token: opts.token, signal: opts.signal,
|
||||
});
|
||||
} catch (err: any) {
|
||||
if (opts.signal?.aborted) {
|
||||
throw err;
|
||||
}
|
||||
opts.onRetry?.(err, opts.retryDelayMs);
|
||||
await sleep(opts.retryDelayMs, undefined, { signal: opts.signal });
|
||||
continue;
|
||||
}
|
||||
|
||||
if (response.status === 200) {
|
||||
return pairingResult(response);
|
||||
}
|
||||
if (isTemporary(response.status)) {
|
||||
const delayMs = retryDelay(response, opts.retryDelayMs);
|
||||
opts.onRetry?.(responseError(response), delayMs);
|
||||
await sleep(delayMs, undefined, { signal: opts.signal });
|
||||
continue;
|
||||
}
|
||||
if (response.status === 404) {
|
||||
throw new PairingError("The controller must be updated to a version that supports host pairing", 404);
|
||||
}
|
||||
if (response.status !== 202) {
|
||||
throw responseError(response);
|
||||
}
|
||||
if (typeof response.json?.requestId !== "string" || typeof response.json.code !== "string") {
|
||||
throw new PairingError("Invalid response from controller", response.status);
|
||||
}
|
||||
|
||||
requestId = response.json.requestId as string;
|
||||
opts.onPending?.(response.json.code);
|
||||
const result = await poll(requestId, opts);
|
||||
requestId = undefined;
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
if (!opts.signal?.aborted) {
|
||||
throw err;
|
||||
}
|
||||
if (requestId !== undefined) {
|
||||
await request(opts.controllerUrl, "api/host-pairing/cancel", {
|
||||
body: { requestId }, timeoutMs: 5e3,
|
||||
}).catch(() => {});
|
||||
}
|
||||
throw new DOMException("Pairing was cancelled", "AbortError");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the controller accepts a host token
|
||||
*
|
||||
* @param controllerUrl - URL of the controller, ending with a slash.
|
||||
* @param token - Host token to check.
|
||||
* @param signal - Signal to cancel the check with.
|
||||
* @returns true if the token is valid and its host is registered.
|
||||
* @throws {PairingError} if the controller gives an unexpected response.
|
||||
*/
|
||||
export async function checkHostToken(controllerUrl: string, token: string, signal?: AbortSignal) {
|
||||
const response = await request(controllerUrl, "api/host-pairing/check", { token, signal });
|
||||
if (response.status === 200) {
|
||||
return true;
|
||||
}
|
||||
if (response.status === 401) {
|
||||
return false;
|
||||
}
|
||||
throw responseError(response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pair the host with the controller if it has not been paired, or the controller no longer accepts it
|
||||
*
|
||||
* Waits until the request is approved and saves the id, name and token of the host to its config.
|
||||
*
|
||||
* @param config - Config of the host.
|
||||
* @param options - Pairing options.
|
||||
* @throws {PairingError} if the controller refuses or rejects the request.
|
||||
* @throws {DOMException} AbortError if the signal is aborted.
|
||||
*/
|
||||
export async function ensurePaired(
|
||||
config: lib.HostConfig,
|
||||
options: { version: string, userToken?: string, signal?: AbortSignal },
|
||||
) {
|
||||
const controllerUrl = config.get("host.controller_url");
|
||||
const token = config.get("host.controller_token");
|
||||
if (token && token !== unpairedToken) {
|
||||
let valid;
|
||||
try {
|
||||
valid = await checkHostToken(controllerUrl, token, options.signal);
|
||||
} catch (err: any) {
|
||||
if (options.signal?.aborted) {
|
||||
throw err;
|
||||
}
|
||||
// Connecting reports the problem, or authenticates if the controller predates pairing.
|
||||
logger.verbose(`Unable to check if the controller accepts this host: ${err.message}`);
|
||||
return;
|
||||
}
|
||||
if (valid) {
|
||||
return;
|
||||
}
|
||||
logger.warn("The controller no longer accepts this host, requesting to join again");
|
||||
}
|
||||
|
||||
const name = config.get("host.name");
|
||||
logger.info(`Requesting to join the cluster at ${controllerUrl} as "${name}"`);
|
||||
const result = await pairHost({
|
||||
controllerUrl,
|
||||
name,
|
||||
version: options.version,
|
||||
token: options.userToken,
|
||||
signal: options.signal,
|
||||
onPending: code => {
|
||||
const approveUrl = new URL(`hosts?approve=${code}`, controllerUrl).href;
|
||||
// Logged as warnings for the systemd service, which only prints warnings and above.
|
||||
// The link is logged on its own so terminals can make it clickable without it wrapping.
|
||||
logger.warn(`Waiting for approval of "${name}" with code ${code}, open this link to approve it:`);
|
||||
logger.warn(approveUrl);
|
||||
logger.warn(`Or run: npx clusterioctl host approve ${code}`);
|
||||
},
|
||||
onRetry: (err, delayMs) => {
|
||||
logger.warn(`Requesting to join failed: ${err.message}, trying again in ${Math.ceil(delayMs / 1000)}s`);
|
||||
},
|
||||
});
|
||||
|
||||
config.set("host.id", result.hostId);
|
||||
config.set("host.name", result.name);
|
||||
config.set("host.controller_token", result.token);
|
||||
await config.save();
|
||||
logger.info(`Host approved as "${result.name}" with id ${result.hostId}`);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
// This needs to be a separate file due to ECMAScript Modules evaluating code in
|
||||
// imported modules before any code in the file that imports it.
|
||||
(global as any).lazySchemaCompilation = true;
|
||||
Reference in new issue
Block a user