mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-22 08:13:12 +09:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8865038af5 | ||
|
|
f338c5fd91 | ||
|
|
bee6714ff8 | ||
|
|
ef3b4cb8fc | ||
|
|
cd2933417a | ||
|
|
bd912a8fd4 | ||
|
|
86befd1231 | ||
|
|
73d70cbc91 | ||
|
|
516dc1b945 | ||
|
|
079340e5dd | ||
|
|
565e931a96 | ||
|
|
7a2aade25b | ||
|
|
2bba4a8ccd | ||
|
|
c03666e749 | ||
|
|
3ca203c753 | ||
|
|
fcf858023e | ||
|
|
faf214e2a1 | ||
|
|
8e01a96df9 | ||
|
|
8692ea3941 | ||
|
|
a49841f3fc | ||
|
|
2e273dc630 | ||
|
|
42bc31e406 | ||
|
|
19569aefa4 | ||
|
|
e9a11013a4 | ||
|
|
59fa8403de | ||
|
|
1c18dc3661 | ||
|
|
81c7d24867 | ||
|
|
c4a9545d28 | ||
|
|
6dc48b6732 | ||
|
|
4d7c1020b0 | ||
|
|
909f6ab08b | ||
|
|
aaafce4ace |
No files matched your search
@@ -40,7 +40,7 @@ jobs:
|
||||
node-version: 24.x
|
||||
- run: pnpm install --no-frozen-lockfile
|
||||
- run: pnpm -r --filter='./packages/*' --filter='./plugins/*' --fail-if-no-match run build-mod --output-dir ../../dist
|
||||
- run: node ./publish.js factorio ./dist
|
||||
- run: node ./scripts/publish.js factorio ./dist
|
||||
env:
|
||||
FACTORIO_TOKEN: ${{ secrets.FACTORIO_TOKEN }}
|
||||
- uses: softprops/action-gh-release@v3
|
||||
|
||||
@@ -30,7 +30,7 @@ Use the detailed guides for the affected area:
|
||||
The harness needs Factorio, recreates `temp/test/`, and shares ports 8880/4443; do not run competing suites.
|
||||
- Report checks, failures, skips, and unverified behavior precisely.
|
||||
Documentation-only changes need path, command, and formatting checks.
|
||||
- Preview cleanup with `node clean.js --dry-run`; see the [cleanup scopes](docs/contributing.md#cleaning-generated-and-runtime-files).
|
||||
- Preview cleanup with `node scripts/clean.js --dry-run`; see the [cleanup scopes](docs/contributing.md#cleaning-generated-and-runtime-files).
|
||||
Runtime-data deletion and release publication require explicit authorization.
|
||||
Keep local configs, credentials, runtime data, and generated output out of commits.
|
||||
- Follow the [PR template](.github/pull_request_template.md), including `### Changelog` (`None.` when appropriate).
|
||||
@@ -1,36 +1,5 @@
|
||||
# Changelog
|
||||
|
||||
<!-- Template for entries. Add sections that apply as changes are made.
|
||||
## Version 2.0.0-alpha.xx
|
||||
|
||||
### Major Features
|
||||
|
||||
- Major additions and/or major improvements to managing clusters.
|
||||
|
||||
### Features
|
||||
|
||||
- New features added.
|
||||
|
||||
### Changes
|
||||
|
||||
- Small changes to existing behaviour.
|
||||
|
||||
### Fixes
|
||||
|
||||
- Bugfixes
|
||||
|
||||
### Breaking Changes
|
||||
|
||||
- Changes which break backwards compatibility with older hosts or plugins code.
|
||||
|
||||
### Meta
|
||||
|
||||
- Changes to the project activities / structure worth highlighting but have no impact on the published package.
|
||||
|
||||
Many thanks to the following for contributing to this release:
|
||||
[@username](https://github.com/username)
|
||||
-->
|
||||
|
||||
## Version 2.1.1
|
||||
|
||||
### Features
|
||||
|
||||
+24
-4
@@ -241,17 +241,20 @@ Must be greater than 0.
|
||||
Defaults to 15.
|
||||
|
||||
|
||||
### controller.transfer_min_progress
|
||||
### controller.transfer_min_speed
|
||||
|
||||
Amount in KiB a request to the controller has to move every 30 seconds, requests moving less are closed.
|
||||
Speed in KiB/s at which a request to the controller is never closed for moving too slowly.
|
||||
This closes uploads and downloads that have stalled or are sent too slowly to ever finish, while there is no limit on how long a transfer making progress can take.
|
||||
It applies to saves, mods and exports uploaded to the controller, proxy streams through it, and the body of any other request.
|
||||
Time spent waiting for a proxy stream to start is not counted, see controller.proxy_stream_timeout.
|
||||
Data is passed on in chunks of up to 64 KiB and progress is counted per chunk, so a proxy stream moving less than about that in 30 seconds may also be closed.
|
||||
Requests are checked every 60 seconds and must have moved this speed times 60 seconds, less 2 MiB, since the last check.
|
||||
The 2 MiB covers the OS sending data in bursts, it can buffer a couple of MiB per connection and only accept more once a large part of that has gone out.
|
||||
So at the default a request moving slower than about 30 KiB/s is closed, and one moving between that and 64 KiB/s may be.
|
||||
Set below about 34 KiB/s, only requests that moved nothing in 60 seconds are closed.
|
||||
Set to 0 for no limit.
|
||||
Must be 0 or greater.
|
||||
|
||||
Defaults to 30.
|
||||
Defaults to 64.
|
||||
|
||||
|
||||
### controller.factorio_username
|
||||
@@ -546,6 +549,23 @@ Does not affect start or stop of the instance while the host is running.
|
||||
Defaults to false.
|
||||
|
||||
|
||||
### instance.restart_after_crash
|
||||
|
||||
If enabled the host starts this instance again when the Factorio server exits unexpectedly after running for at least 10 minutes.
|
||||
A crash sooner than that is logged as an error and the instance is left stopped, as it is likely to crash again.
|
||||
|
||||
Defaults to false.
|
||||
|
||||
|
||||
### instance.crash_retry_attempts
|
||||
|
||||
Number of times the host restarts this instance after a crash before giving up and logging an error.
|
||||
The count resets when a user starts or restarts the instance, or the host is restarted.
|
||||
Set to 0 for no limit.
|
||||
|
||||
Defaults to 3.
|
||||
|
||||
|
||||
### instance.exclude_from_start_all
|
||||
|
||||
If enabled this instance will be excluded from the "Start all" button operation in the web interface.
|
||||
|
||||
@@ -229,13 +229,13 @@ For documentation changes, check that links, paths, and example commands are cor
|
||||
|
||||
### Cleaning generated and runtime files
|
||||
|
||||
Inspect `node clean.js --help` and preview the selected scope with `--dry-run` before cleaning.
|
||||
Inspect `node scripts/clean.js --help` and preview the selected scope with `--dry-run` before cleaning.
|
||||
|
||||
| Command | Removal scope |
|
||||
| --- | --- |
|
||||
| `node clean.js --dry-run` | Preview workspace package/plugin build output and Vite dependency caches; omit `--dry-run` to clean, or use `pnpm clean`. |
|
||||
| `node clean.js tests --dry-run` | Also preview root `.nyc_output/`, `coverage/`, the whole `temp/` directory, and `static/`. |
|
||||
| `node clean.js all --dry-run` | Also preview runtime databases, instances, logs, mods, local configs, `FactorioAdminToken.txt`, and `plugin-list.json`. |
|
||||
| `node scripts/clean.js --dry-run` | Preview workspace package/plugin build output and Vite dependency caches; omit `--dry-run` to clean, or use `pnpm clean`. |
|
||||
| `node scripts/clean.js tests --dry-run` | Also preview root `.nyc_output/`, `coverage/`, the whole `temp/` directory, and `static/`. |
|
||||
| `node scripts/clean.js all --dry-run` | Also preview runtime databases, instances, logs, mods, local configs, `FactorioAdminToken.txt`, and `plugin-list.json`. |
|
||||
|
||||
`all` deletes cluster runtime data, including configurations and saves; back up anything you want to keep before using it.
|
||||
The cleaner excludes `external_plugins/` and does not remove mod archives in root `dist/`.
|
||||
|
||||
+18
-22
@@ -5,7 +5,8 @@
|
||||
- CHANGELOG.md in the repository root
|
||||
- The changelog.txt file in mod directories
|
||||
|
||||
Pull requests will usually have suggestions for changelog entries. These can be automatically parsed and formatted by running `node scripts/pr_to_changelog.mts`, though you will have to manually sort through which should go in the changelog in the repository root and which should go in the changelogs in mod directories.
|
||||
Pull requests have a changelog section that is parsed and formatted by `node scripts/pr_to_changelog.mts`.
|
||||
The prepare release script below adds its output to CHANGELOG.md, but changes to mods still have to be copied into the changelogs in the mod directories by hand.
|
||||
|
||||
If the script fails due to GitHub's 60/hour public access rate limit, you can set the `GH_TOKEN` environment variable to a fine-grained personal access token which has a much higher rate limits.
|
||||
|
||||
@@ -15,10 +16,10 @@ The [Publish workflow](../.github/workflows/publish.yml) publishes npm packages
|
||||
Publishing a prerelease does not trigger it; see [GitHub's release event documentation](https://docs.github.com/en/actions/reference/workflows-and-actions/events-that-trigger-workflows#release).
|
||||
The npm release includes the packages under `packages/` and the six core plugins under `plugins/`.
|
||||
Community plugins hosted in separate repositories have independent versions and releases.
|
||||
For this workflow, first merge the changelog and package version updates into `master`, then create and push the release tag on that merged commit.
|
||||
Create a draft GitHub release for the existing tag and use the GitHub Release Markdown from `scripts/pr_to_changelog.mts` as its notes.
|
||||
For this workflow, push the commit and tag made by `scripts/prepare_release.mts` to `master`.
|
||||
Create a draft GitHub release for the existing tag and use the GitHub Release Markdown printed by `scripts/prepare_release.mts` as its notes.
|
||||
Publishing the draft as a stable release triggers the workflow; pushing a tag alone does not.
|
||||
Preparing a release PR, pushing a tag, and publishing a release or packages are separate actions; obtain authorization for the publication steps before executing them on someone else's behalf.
|
||||
Preparing a release, pushing a tag, and publishing a release or packages are separate actions; obtain authorization for the publication steps before executing them on someone else's behalf.
|
||||
The steps below describe publishing npm packages manually instead.
|
||||
|
||||
> [!NOTE]
|
||||
@@ -33,36 +34,31 @@ The steps below describe publishing npm packages manually instead.
|
||||
git status
|
||||
```
|
||||
|
||||
2. Create a release branch and bump the version in the `package.json` files. The commands below use 2.0.1 to 2.0.2 as an example; substitute the versions for the release you are preparing.
|
||||
2. Run the prepare release script with the new version.
|
||||
It sets the version in the `package.json` files under `packages/` and `plugins/`, adds the changelog from merged pull requests to the top of CHANGELOG.md, commits it as "Prepare 2.0.2" and tags that commit `v2.0.2`.
|
||||
Nothing is pushed.
|
||||
It also prints the GitHub Release Markdown for the release notes.
|
||||
If a merged pull request has a missing or malformed changelog section, the script lists the problems and stops before changing anything.
|
||||
Fix the pull request description and run it again, or pass `--force` to continue anyway. Entries with warnings are kept, pull requests whose changelog section could not be read are left out.
|
||||
|
||||
```sh
|
||||
git switch -c release/2.0.2
|
||||
sed -i 's/"version": "2\.0\.1"/"version": "2.0.2"/' {packages,plugins}/*/package.json
|
||||
pnpm install --no-frozen-lockfile
|
||||
node scripts/prepare_release.mts 2.0.2
|
||||
```
|
||||
|
||||
3. Review and commit the version and changelog changes, then open and merge a release pull request.
|
||||
3. Review the commit, amend it if the changelog needs edits, then push it and the tag.
|
||||
If you amend the commit, move the tag with `git tag -f v2.0.2`.
|
||||
|
||||
```sh
|
||||
git add -p
|
||||
git commit -m "Prepare 2.0.2 release"
|
||||
git show HEAD
|
||||
git push upstream master v2.0.2
|
||||
```
|
||||
|
||||
4. After the release pull request is merged, tag the merged commit and push the tag.
|
||||
|
||||
```sh
|
||||
git switch master
|
||||
git pull --ff-only upstream master
|
||||
git tag v2.0.2
|
||||
git push upstream v2.0.2
|
||||
```
|
||||
|
||||
5. For manual publication, perform a clean install and publish the packages. For automated publication, publish the GitHub release as described above instead.
|
||||
4. For manual publication, perform a clean install and publish the packages. For automated publication, publish the GitHub release as described above instead.
|
||||
|
||||
The repository does not track `pnpm-lock.yaml`, so allow pnpm to generate or update it during installation.
|
||||
|
||||
```sh
|
||||
node clean.js --dry-run
|
||||
node scripts/clean.js --dry-run
|
||||
pnpm clean
|
||||
pnpm install --no-frozen-lockfile
|
||||
pnpm -r --filter='./packages/*' --filter='./plugins/*' --fail-if-no-match publish
|
||||
|
||||
+1
-1
@@ -17,7 +17,7 @@
|
||||
"ci-cover": "c8 --reporter=lcovonly pnpm test",
|
||||
"lint": "oxlint packages plugins test",
|
||||
"lint-fix": "oxlint --fix packages plugins test",
|
||||
"clean": "node ./clean.js",
|
||||
"clean": "node ./scripts/clean.js",
|
||||
"bundle-dependencies": "bundle-dependencies"
|
||||
},
|
||||
"c8": {
|
||||
|
||||
@@ -21,7 +21,6 @@ import crypto from "crypto";
|
||||
import yargs from "yargs";
|
||||
import util from "util";
|
||||
import winston from "winston";
|
||||
import DailyRotateFile from "winston-daily-rotate-file";
|
||||
|
||||
|
||||
// homebrew modules
|
||||
@@ -355,19 +354,17 @@ async function initialize(): Promise<InitializeParameters> {
|
||||
level: "verbose",
|
||||
levels,
|
||||
});
|
||||
clusterLogger.add(new DailyRotateFile({
|
||||
clusterLogger.add(new lib.DailyFileTransport({
|
||||
format: winston.format.json(),
|
||||
filename: "cluster-%DATE%.log",
|
||||
utc: true,
|
||||
dirname: path.join(args.logDirectory, "cluster"),
|
||||
prefix: "cluster",
|
||||
}));
|
||||
|
||||
// Log stream for the controller.
|
||||
logger.add(new DailyRotateFile({
|
||||
logger.add(new lib.DailyFileTransport({
|
||||
format: winston.format.json(),
|
||||
filename: "controller-%DATE%.log",
|
||||
utc: true,
|
||||
dirname: path.join(args.logDirectory, "controller"),
|
||||
prefix: "controller",
|
||||
}));
|
||||
logger.add(new winston.transports.Stream({
|
||||
stream: clusterLogger,
|
||||
|
||||
@@ -45,7 +45,6 @@
|
||||
"jsonwebtoken": "catalog:",
|
||||
"semver": "catalog:",
|
||||
"winston": "catalog:",
|
||||
"winston-daily-rotate-file": "catalog:",
|
||||
"ws": "catalog:",
|
||||
"yargs": "catalog:"
|
||||
},
|
||||
|
||||
@@ -419,7 +419,7 @@ export interface ProxyStream {
|
||||
timeout: ReturnType<typeof setTimeout>;
|
||||
closed: boolean;
|
||||
/** Bytes the receiver has taken, set once the stream is flowing */
|
||||
progress?: () => number;
|
||||
bytesTaken?: () => number;
|
||||
}
|
||||
|
||||
export async function createProxyStream(app: Application): Promise<ProxyStream> {
|
||||
@@ -473,18 +473,23 @@ const answeredUploadDrainMs = 1000;
|
||||
* so the connection is closed if the body hasn't ended shortly after the
|
||||
* response. Closing it right away could make the client lose the response.
|
||||
*
|
||||
* The connection is also closed if the request moved less than
|
||||
* controller.transfer_min_progress during a check interval, so that a
|
||||
* The connection is also closed if the request moved too little during a
|
||||
* check interval for controller.transfer_min_speed, so that a
|
||||
* request which stalled or is trickled in does not stay open forever while
|
||||
* a slow one can take the time it needs. For a request body this is what
|
||||
* was received of it. For both ends of a proxy stream it is what the
|
||||
* receiver has taken, as the sender is held back when the receiver is the
|
||||
* slow one. Time spent waiting for a proxy stream to start is not counted,
|
||||
* controller.proxy_stream_timeout limits that.
|
||||
*
|
||||
* The OS hands data on in bursts, so a transfer at the minimum speed can
|
||||
* move up to a burst less than that in a check. The burst allowance is
|
||||
* taken off what a check requires so such a transfer is never closed.
|
||||
* @param checkIntervalMs - Interval to check the progress of requests on.
|
||||
* @param burstAllowanceBytes - Bytes taken off what a check requires.
|
||||
* @returns Express middleware.
|
||||
*/
|
||||
function guardRequest(checkIntervalMs: number) {
|
||||
function guardRequest(checkIntervalMs: number, burstAllowanceBytes: number) {
|
||||
return function(req: Request, res: Response, next: any) {
|
||||
const socket = req.socket;
|
||||
res.once("finish", () => {
|
||||
@@ -501,33 +506,36 @@ function guardRequest(checkIntervalMs: number) {
|
||||
});
|
||||
});
|
||||
|
||||
const received = () => socket.bytesRead;
|
||||
let measure: (() => number) | undefined = received;
|
||||
let last = received();
|
||||
const bytesReceived = () => socket.bytesRead;
|
||||
let measuredBytes: (() => number) | undefined = bytesReceived;
|
||||
let lastBytes = bytesReceived();
|
||||
const check = setInterval(() => {
|
||||
const stream: ProxyStream | undefined = res.locals.proxyStream;
|
||||
let current: (() => number) | undefined;
|
||||
let currentBytes: (() => number) | undefined;
|
||||
if (stream && !stream.closed) {
|
||||
current = stream.progress;
|
||||
currentBytes = stream.bytesTaken;
|
||||
} else if (!req.complete) {
|
||||
current = received;
|
||||
currentBytes = bytesReceived;
|
||||
}
|
||||
const minProgress = req.app.locals.controller.config.get("controller.transfer_min_progress");
|
||||
if (current !== measure || !current || !minProgress) {
|
||||
const minSpeedKiBps = req.app.locals.controller.config.get("controller.transfer_min_speed");
|
||||
if (currentBytes !== measuredBytes || !currentBytes || !minSpeedKiBps) {
|
||||
// What to measure changed, start over from here.
|
||||
measure = current;
|
||||
last = current ? current() : 0;
|
||||
measuredBytes = currentBytes;
|
||||
lastBytes = currentBytes ? currentBytes() : 0;
|
||||
return;
|
||||
}
|
||||
const progress = current();
|
||||
if (progress - last < minProgress * 1024) {
|
||||
const bytes = currentBytes();
|
||||
const intervalS = checkIntervalMs / 1000;
|
||||
// At least 1 so a transfer that moved nothing is closed at any speed setting.
|
||||
const requiredBytes = Math.max(minSpeedKiBps * 1024 * intervalS - burstAllowanceBytes, 1);
|
||||
if (bytes - lastBytes < requiredBytes) {
|
||||
logger.warn(
|
||||
`Closing ${req.method} ${req.path}, it moved less than ${minProgress} KiB ` +
|
||||
`in ${checkIntervalMs / 1000} seconds`
|
||||
`Closing ${req.method} ${req.path}, it moved ${bytes - lastBytes} bytes in ${intervalS} seconds, ` +
|
||||
`less than the ${requiredBytes} required for ${minSpeedKiBps} KiB/s`
|
||||
);
|
||||
socket.destroy();
|
||||
}
|
||||
last = progress;
|
||||
lastBytes = bytes;
|
||||
}, checkIntervalMs);
|
||||
res.once("close", () => clearInterval(check));
|
||||
next();
|
||||
@@ -567,7 +575,7 @@ function startStream(res: Response, stream: ProxyStream) {
|
||||
source.pipe(res);
|
||||
stream.flowing = true;
|
||||
// Counts completed writes, so data waiting in a buffer is not progress.
|
||||
stream.progress = () => socket.bytesWritten - socket.writableLength;
|
||||
stream.bytesTaken = () => socket.bytesWritten - socket.writableLength;
|
||||
// Pipe does not pass on a source that was cut short, the receiver would wait forever.
|
||||
const onSourceClose = () => {
|
||||
if (!res.writableEnded) {
|
||||
@@ -828,7 +836,7 @@ async function uploadMod(req: Request, res: Response) {
|
||||
// Pipe does not pass on an upload that was cut short.
|
||||
await Promise.all([finished(file.stream), finished(writeStream)]);
|
||||
|
||||
const modInfo = await lib.ModInfo.fromModFile(tempFilePath);
|
||||
const modInfo = await lib.ModStore.readModInfo(tempFilePath);
|
||||
checkModName(modInfo.filename);
|
||||
await fs.rename(tempFilePath, path.join(modsDirectory, modInfo.filename));
|
||||
req.app.locals.controller.modStore.addMod(modInfo);
|
||||
@@ -856,8 +864,12 @@ async function uploadMod(req: Request, res: Response) {
|
||||
}
|
||||
|
||||
|
||||
export function addRouteHandlers(app: Application, progressCheckIntervalMs = 30_000) {
|
||||
app.use(guardRequest(progressCheckIntervalMs));
|
||||
export function addRouteHandlers(
|
||||
app: Application,
|
||||
progressCheckIntervalMs = 60_000,
|
||||
burstAllowanceBytes = 2 * 1024 * 1024,
|
||||
) {
|
||||
app.use(guardRequest(progressCheckIntervalMs, burstAllowanceBytes));
|
||||
app.get("/metrics",
|
||||
validateUserToken,
|
||||
(req:Request, res:Response, next:any) => getMetrics(req, res).catch(next)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/* eslint-disable no-console */
|
||||
import chalk from "chalk";
|
||||
import { styleText } from "node:util";
|
||||
|
||||
export const levels = Object.freeze({
|
||||
fatal: 0,
|
||||
@@ -27,10 +27,7 @@ for (let [name, level] of Object.entries(levels)) {
|
||||
if (level > maxLogLevel) {
|
||||
return;
|
||||
}
|
||||
let style = chalk;
|
||||
for (let part of colors[name].split(" ")) {
|
||||
style = style[part];
|
||||
}
|
||||
const style = text => styleText(colors[name].split(" "), text);
|
||||
if (["fatal", "error"].includes(name)) {
|
||||
console.error(`${style(name)} ${message}`);
|
||||
} else if (["warn"].includes(name)) {
|
||||
|
||||
@@ -36,7 +36,6 @@
|
||||
"@inquirer/confirm": "^6.3.5",
|
||||
"@inquirer/input": "^5.1.9",
|
||||
"@inquirer/select": "^5.2.8",
|
||||
"chalk": "catalog:",
|
||||
"yargs": "catalog:"
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,6 @@ import path from "path";
|
||||
import yargs from "yargs";
|
||||
import packageConfig from "@clusterio/host/package.json" with { type: "json" };
|
||||
import winston from "winston";
|
||||
import "winston-daily-rotate-file";
|
||||
|
||||
// internal libraries
|
||||
// Reduce startup time by lazy compiling schemas.
|
||||
@@ -147,11 +146,10 @@ async function startHost() {
|
||||
.parseSync()
|
||||
;
|
||||
|
||||
logger.add(new winston.transports.DailyRotateFile({
|
||||
logger.add(new lib.DailyFileTransport({
|
||||
format: winston.format.json(),
|
||||
filename: "host-%DATE%.log",
|
||||
utc: true,
|
||||
dirname: path.join(args.logDirectory, "host"),
|
||||
prefix: "host",
|
||||
}));
|
||||
if (args.logLevel !== "none") {
|
||||
logger.add(new ConsoleTransport({
|
||||
|
||||
@@ -49,7 +49,6 @@
|
||||
"semver": "catalog:",
|
||||
"set-blocking": "catalog:",
|
||||
"winston": "catalog:",
|
||||
"winston-daily-rotate-file": "catalog:",
|
||||
"yargs": "catalog:",
|
||||
"yazl": "catalog:"
|
||||
},
|
||||
|
||||
@@ -351,6 +351,8 @@ export default class Host extends lib.Link {
|
||||
_shuttingDown = false;
|
||||
/** Instances waiting on {@link Instance.fromConfig} */
|
||||
_startingInstances = new Set<number>();
|
||||
/** Restarts after a crash per instance ID since the last manual start */
|
||||
_crashRetryAttempts = new Map<number, number>();
|
||||
|
||||
startedAtMs = Date.now();
|
||||
|
||||
|
||||
@@ -44,6 +44,9 @@ const transitoryStatuses: ReadonlySet<Instance["_status"]> = new Set([
|
||||
"starting", "stopping", "creating_save", "exporting_data",
|
||||
]);
|
||||
|
||||
// Crashes sooner than this after starting are not restarted to avoid boot loops.
|
||||
const crashRestartMinUptimeMs = 10 * 60e3;
|
||||
|
||||
const instanceRconCommandDuration = new lib.Histogram(
|
||||
"clusterio_instance_rcon_command_duration_seconds",
|
||||
"Histogram of the RCON command duration from request to response.",
|
||||
@@ -194,6 +197,8 @@ export default class Instance extends lib.Link {
|
||||
_playerAutosaveSlot = 1;
|
||||
_expectedUserUpdates: { action: string, name: string, reason: string }[] = [];
|
||||
_settleWaiters: (() => void)[] = [];
|
||||
_startedAtMs: number | null = null;
|
||||
_killRequested = false;
|
||||
|
||||
constructor(
|
||||
host: Host,
|
||||
@@ -594,6 +599,9 @@ end`.replace(/\r?\n/g, " ");
|
||||
|
||||
notifyStatus(status: Instance["_status"]) {
|
||||
this._status = status;
|
||||
if (status === "running") {
|
||||
this._startedAtMs = Date.now();
|
||||
}
|
||||
if (!transitoryStatuses.has(status)) {
|
||||
const waiters = this._settleWaiters;
|
||||
this._settleWaiters = [];
|
||||
@@ -643,6 +651,7 @@ end`.replace(/\r?\n/g, " ");
|
||||
}
|
||||
|
||||
notifyExit() {
|
||||
const crashed = this._status === "running" && !this._killRequested;
|
||||
this._loadedSave = null;
|
||||
this._autoPause = false;
|
||||
this.notifyStatus("stopped");
|
||||
@@ -668,6 +677,29 @@ end`.replace(/\r?\n/g, " ");
|
||||
this._recordPlayerLeave(player, "server_quit");
|
||||
}
|
||||
this._saveStats().catch(err => this.logger.error(`Error saving stats:\n${err.stack}`));
|
||||
|
||||
if (crashed && this.config.get("instance.restart_after_crash") && !this._host._shuttingDown) {
|
||||
this._restartAfterCrash(Date.now() - this._startedAtMs!);
|
||||
}
|
||||
}
|
||||
|
||||
_restartAfterCrash(uptimeMs: number) {
|
||||
if (uptimeMs < crashRestartMinUptimeMs) {
|
||||
this.logger.error(`Instance ${this.name} crashed within 10 minutes of starting, not restarting it`);
|
||||
return;
|
||||
}
|
||||
const limit = this.config.get("instance.crash_retry_attempts");
|
||||
const attempts = this._host._crashRetryAttempts.get(this.id) ?? 0;
|
||||
if (limit > 0 && attempts >= limit) {
|
||||
this.logger.error(`Instance ${this.name} reached the limit of ${limit} crash restarts, not restarting it`);
|
||||
return;
|
||||
}
|
||||
this._host._crashRetryAttempts.set(this.id, attempts + 1);
|
||||
const of = limit > 0 ? ` of ${limit}` : "";
|
||||
this.logger.warn(`Restarting instance ${this.name} after crash, attempt ${attempts + 1}${of}`);
|
||||
this._host._connectInstance(this.id)
|
||||
.then(connection => connection.instance._startSave())
|
||||
.catch(err => this.logger.error(`Error restarting instance ${this.name} after crash:\n${err.stack}`));
|
||||
}
|
||||
|
||||
static async _readStats(statsPath: string) {
|
||||
@@ -1358,6 +1390,7 @@ end`.replace(/\r?\n/g, " ");
|
||||
if (this._status === "stopped") {
|
||||
return;
|
||||
}
|
||||
this._killRequested = true;
|
||||
await this.server.kill(true);
|
||||
}
|
||||
|
||||
@@ -1395,7 +1428,11 @@ end`.replace(/\r?\n/g, " ");
|
||||
}
|
||||
|
||||
async handleInstanceStartRequest(request: lib.InstanceStartRequest) {
|
||||
let saveName = request.save;
|
||||
this._host._crashRetryAttempts.delete(this.id);
|
||||
await this._startSave(request.save);
|
||||
}
|
||||
|
||||
async _startSave(saveName?: string) {
|
||||
if (saveName !== undefined) {
|
||||
checkRequestSaveName(saveName);
|
||||
}
|
||||
@@ -1424,6 +1461,7 @@ end`.replace(/\r?\n/g, " ");
|
||||
}
|
||||
|
||||
async handleInstanceLoadScenarioRequest(request: lib.InstanceLoadScenarioRequest) {
|
||||
this._host._crashRetryAttempts.delete(this.id);
|
||||
if (this.config.get("factorio.enable_save_patching")) {
|
||||
this.notifyExit();
|
||||
throw new lib.ConflictError("Load scenario cannot be used with save patching enabled");
|
||||
|
||||
@@ -31,6 +31,13 @@
|
||||
},
|
||||
"import": "./src/link/WebSocket.browser.ts"
|
||||
},
|
||||
"#zlib_sync": {
|
||||
"node": {
|
||||
"types": "./src/zlib_sync.node.ts",
|
||||
"default": "./dist/node/src/zlib_sync.node.js"
|
||||
},
|
||||
"import": "./src/zlib_sync.browser.ts"
|
||||
},
|
||||
"#is_deep_strict_equal": {
|
||||
"node": {
|
||||
"types": "./src/is_deep_strict_equal.node.ts",
|
||||
@@ -51,11 +58,9 @@
|
||||
|
||||
"dependencies": {
|
||||
"@sinclair/typebox": "catalog:",
|
||||
"@types/yauzl": "catalog:",
|
||||
"@types/yazl": "catalog:",
|
||||
"ajv": "^8.20.0",
|
||||
"chalk": "catalog:",
|
||||
"fast-deep-equal": "^3.1.3",
|
||||
"fflate": "catalog:",
|
||||
"set-blocking": "catalog:",
|
||||
"triple-beam": "^1.4.1",
|
||||
"winston": "catalog:",
|
||||
@@ -71,6 +76,8 @@
|
||||
"@types/triple-beam": "^1.3.5",
|
||||
"@types/ws": "catalog:",
|
||||
"@types/yargs": "catalog:",
|
||||
"@types/yauzl": "catalog:",
|
||||
"@types/yazl": "catalog:",
|
||||
"typescript": "catalog:"
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,8 @@ import events from "events";
|
||||
import { Static } from "@sinclair/typebox";
|
||||
import { logger } from "./logging.js";
|
||||
import { downloadFile, atomicOverwriteFile } from "./file_ops.js";
|
||||
import { hashFile } from "./hash.js";
|
||||
import { ZipArchive, findRoot } from "./zip_ops.js";
|
||||
import { ModPortalReleaseSchema, ModPortalDetailsSchema, ModNameVersionPair } from "./data/messages_mod.js";
|
||||
import { ModInfo, ModPack, MajorMinorVersion, ModVersionEquality, SourceVersion } from "./data/index.js";
|
||||
|
||||
@@ -54,8 +56,44 @@ export default class ModStore extends events.EventEmitter<ModStoreEvents> {
|
||||
|
||||
mods() { return this.files.values(); }
|
||||
|
||||
/**
|
||||
* Read the info.json of a mod zip file along with its size, mtime and hash
|
||||
*
|
||||
* @param modPath - Path to the mod zip file.
|
||||
* @returns Mod info for the file.
|
||||
*/
|
||||
static async readModInfo(modPath: string) {
|
||||
let modInfo: Static<typeof ModInfo.jsonSchema>;
|
||||
let zip = await ZipArchive.fromFile(modPath);
|
||||
try {
|
||||
let content = await zip.readFile(path.posix.join(findRoot(zip), "info.json"));
|
||||
if (content === null) {
|
||||
throw new Error("Mod contains no info.json file");
|
||||
}
|
||||
modInfo = JSON.parse(content.toString("utf8"));
|
||||
} finally {
|
||||
zip.close();
|
||||
}
|
||||
|
||||
let valid = ModInfo.validateInfo(modInfo);
|
||||
if (!valid) {
|
||||
throw new Error("Mod's info.json is not valid");
|
||||
}
|
||||
|
||||
const stat = await fs.stat(modPath);
|
||||
return ModInfo.fromJSON({
|
||||
...modInfo,
|
||||
|
||||
size: stat.size,
|
||||
sha1: await hashFile(modPath),
|
||||
mtime_ms: stat.mtimeMs,
|
||||
updated_at_ms: stat.mtimeMs,
|
||||
is_deleted: false,
|
||||
});
|
||||
}
|
||||
|
||||
static async loadFile(modsDirectory: string, file: string) {
|
||||
let modInfo = await ModInfo.fromModFile(path.join(modsDirectory, file));
|
||||
let modInfo = await ModStore.readModInfo(path.join(modsDirectory, file));
|
||||
if (modInfo.filename !== file) {
|
||||
throw new Error(`filename does not match the expected name ${modInfo.filename}.`);
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ export interface ControllerConfigFields {
|
||||
"controller.max_subscription_filters": number;
|
||||
"controller.system_metrics_interval": number;
|
||||
"controller.proxy_stream_timeout": number;
|
||||
"controller.transfer_min_progress": number;
|
||||
"controller.transfer_min_speed": number;
|
||||
"controller.factorio_username": string | null;
|
||||
"controller.factorio_token": string | null;
|
||||
"controller.share_factorio_credentials_with_hosts": boolean;
|
||||
@@ -63,6 +63,14 @@ export class ControllerConfig extends classes.Config<ControllerConfigFields> {
|
||||
config["controller.public_url"] = config["controller.external_address"];
|
||||
delete config["controller.external_address"];
|
||||
}
|
||||
if (config.hasOwnProperty("controller.transfer_min_progress")) {
|
||||
// Was KiB per 30 s, the old default is left out so the new one applies
|
||||
const kib = config["controller.transfer_min_progress"];
|
||||
if (kib !== 30) {
|
||||
config["controller.transfer_min_speed"] = typeof kib === "number" ? kib / 30 : kib;
|
||||
}
|
||||
delete config["controller.transfer_min_progress"];
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
@@ -224,11 +232,11 @@ export class ControllerConfig extends classes.Config<ControllerConfigFields> {
|
||||
initialValue: 15,
|
||||
validator: validators.greaterThanZero,
|
||||
},
|
||||
"controller.transfer_min_progress": {
|
||||
title: "Transfer Minimum Progress",
|
||||
description: "KiB an upload or download has to move every 30 seconds to not be closed, 0 for no limit.",
|
||||
"controller.transfer_min_speed": {
|
||||
title: "Transfer Minimum Speed",
|
||||
description: "KiB/s at which an upload or download is never closed for moving too slowly, 0 for no limit.",
|
||||
type: "number",
|
||||
initialValue: 30,
|
||||
initialValue: 64,
|
||||
validator: validators.greaterThanEqualZero,
|
||||
},
|
||||
"controller.session_timeout": {
|
||||
@@ -512,6 +520,8 @@ export interface InstanceConfigFields {
|
||||
"instance.assigned_host": number | null;
|
||||
"instance.auto_start": boolean;
|
||||
"instance.exclude_from_start_all": boolean;
|
||||
"instance.restart_after_crash": boolean;
|
||||
"instance.crash_retry_attempts": number;
|
||||
"instance.is_template": boolean;
|
||||
"instance.template_id": number | null;
|
||||
"instance.inherited_fields": Record<string, true | string[]>;
|
||||
@@ -625,6 +635,16 @@ export class InstanceConfig extends classes.Config<InstanceConfigFields> {
|
||||
type: "boolean",
|
||||
initialValue: false,
|
||||
},
|
||||
"instance.restart_after_crash": {
|
||||
description: "Start this instance again if it crashes after running for at least 10 minutes",
|
||||
type: "boolean",
|
||||
initialValue: false,
|
||||
},
|
||||
"instance.crash_retry_attempts": {
|
||||
description: "Restarts after a crash before giving up, 0 for no limit. Reset when the instance is started",
|
||||
type: "number",
|
||||
initialValue: 3,
|
||||
},
|
||||
"instance.is_template": {
|
||||
description:
|
||||
"Mark this instance as a template other instances can inherit config from. " +
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
import fs from "node:fs/promises";
|
||||
import path from "path";
|
||||
import { Type, Static } from "@sinclair/typebox";
|
||||
|
||||
import * as libHash from "../hash.js";
|
||||
import * as libSchema from "../schema.js";
|
||||
import { ZipArchive, findRoot } from "../zip_ops.js";
|
||||
import { ModRecord } from "./ModPack.js";
|
||||
|
||||
import {
|
||||
@@ -349,34 +345,4 @@ export default class ModInfo {
|
||||
if (this.isDeleted) { json.is_deleted = this.isDeleted; }
|
||||
return json;
|
||||
}
|
||||
|
||||
static async fromModFile(modPath: string) {
|
||||
let modInfo: Static<typeof ModInfo.jsonSchema>;
|
||||
let zip = await ZipArchive.fromFile(modPath);
|
||||
try {
|
||||
let content = await zip.readFile(path.posix.join(findRoot(zip), "info.json"));
|
||||
if (content === null) {
|
||||
throw new Error("Mod contains no info.json file");
|
||||
}
|
||||
modInfo = JSON.parse(content.toString("utf8"));
|
||||
} finally {
|
||||
zip.close();
|
||||
}
|
||||
|
||||
let valid = this.validateInfo(modInfo);
|
||||
if (!valid) {
|
||||
throw new Error("Mod's info.json is not valid");
|
||||
}
|
||||
|
||||
const stat = await fs.stat(modPath);
|
||||
return this.fromJSON({
|
||||
...modInfo,
|
||||
|
||||
size: stat.size,
|
||||
sha1: await libHash.hashFile(modPath),
|
||||
mtime_ms: stat.mtimeMs,
|
||||
updated_at_ms: stat.mtimeMs,
|
||||
is_deleted: false,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import zlib from "zlib";
|
||||
import * as zlib from "#zlib_sync";
|
||||
import { Type, Static } from "@sinclair/typebox";
|
||||
|
||||
import * as libSchema from "../schema.js";
|
||||
@@ -306,7 +306,7 @@ export default class ModPack {
|
||||
|
||||
// eslint-disable-next-line node/no-sync
|
||||
let buf = zlib.deflateSync(JSON.stringify(json));
|
||||
return buf.toString("base64");
|
||||
return Buffer.from(buf).toString("base64");
|
||||
}
|
||||
|
||||
static fromModPackString(modPackString: string) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import zlib from "zlib";
|
||||
import * as zlib from "#zlib_sync";
|
||||
|
||||
class MapReaderState {
|
||||
pos = 0;
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
* @module lib/file_ops
|
||||
*/
|
||||
import crypto from "crypto";
|
||||
import events from "events";
|
||||
import fs from "node:fs/promises";
|
||||
import path from "path";
|
||||
import stream from "stream";
|
||||
@@ -258,7 +257,8 @@ export async function createTempWriteStream(filePath: string) {
|
||||
async function discardPartialDownload(writeStream: stream.Writable, tempFilePath: string) {
|
||||
if (!writeStream.closed) {
|
||||
writeStream.destroy();
|
||||
await events.once(writeStream, "close");
|
||||
// Not events.once, it rejects if the stream is still erroring from the aborted pipe.
|
||||
await new Promise(resolve => writeStream.once("close", resolve));
|
||||
}
|
||||
try {
|
||||
await fs.unlink(tempFilePath);
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
/** Throws if condition is falsy, node:assert is not available in the browser. */
|
||||
export function assert(condition: unknown): asserts condition {
|
||||
if (!condition) {
|
||||
throw new Error("Assertion failed");
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
// Connection adapters for Links
|
||||
import { strict as assert } from "assert";
|
||||
import events from "events";
|
||||
|
||||
import { WebSocket } from "#WebSocket";
|
||||
@@ -7,6 +6,7 @@ import * as libData from "../data/index.js";
|
||||
import * as libErrors from "../errors.js";
|
||||
import { logger } from "../logging.js";
|
||||
import ExponentialBackoff from "../ExponentialBackoff.js";
|
||||
import { assert } from "./assert.js";
|
||||
import type { Request, Event } from "./link.js";
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,7 +5,7 @@ import { logger } from "../logging.js";
|
||||
import * as libSchema from "../schema.js";
|
||||
import { dataClasses, errorClasses } from "./messages.js";
|
||||
import { BaseConnector, WebSocketBaseConnector } from "./connectors.js";
|
||||
import { strict as assert } from "assert";
|
||||
import { assert } from "./assert.js";
|
||||
import type { PluginNodeEnvInfo, PluginWebEnvInfo } from "../plugin.js";
|
||||
import type {
|
||||
AddressType, JSONDeserialisable, MessageRoutable, MessageRequest, MessageEvent, IUser,
|
||||
|
||||
@@ -5,14 +5,14 @@
|
||||
* @module lib/logging_utils
|
||||
*/
|
||||
import fs from "node:fs/promises";
|
||||
import type { ReadStream } from "node:fs";
|
||||
import { type ReadStream, type WriteStream, createWriteStream, mkdirSync } from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "path";
|
||||
import stream from "stream";
|
||||
import util from "util";
|
||||
import util, { styleText } from "util";
|
||||
import winston from "winston";
|
||||
import Transport from "winston-transport";
|
||||
import { LEVEL, MESSAGE } from "triple-beam";
|
||||
import chalk from "chalk";
|
||||
|
||||
import * as libData from "./data/index.js";
|
||||
import * as libErrors from "./errors.js";
|
||||
@@ -108,30 +108,30 @@ export type ParsedFactorioOutput =
|
||||
function formatServerOutput(parsed: ParsedFactorioOutput) {
|
||||
let time = "";
|
||||
if (parsed.format === "seconds") {
|
||||
time = `${chalk.yellow(parsed.time.padStart(8))} `;
|
||||
time = `${styleText("yellow", parsed.time.padStart(8))} `;
|
||||
} else if (parsed.format === "date") {
|
||||
time = `${chalk.yellow(parsed.time)} `;
|
||||
time = `${styleText("yellow", parsed.time)} `;
|
||||
}
|
||||
|
||||
let info = "";
|
||||
if (parsed.type === "log") {
|
||||
let level = parsed.level;
|
||||
if (level === "Script") {
|
||||
level = chalk.bold.greenBright(level);
|
||||
level = styleText(["bold", "greenBright"], level);
|
||||
} else if (level === "Verbose") {
|
||||
level = chalk.bold.gray(level);
|
||||
level = styleText(["bold", "gray"], level);
|
||||
} else if (level === "Info") {
|
||||
level = chalk.bold.blueBright(level);
|
||||
level = styleText(["bold", "blueBright"], level);
|
||||
} else if (parsed.level === "Warning") {
|
||||
level = chalk.bold.yellowBright(level);
|
||||
level = styleText(["bold", "yellowBright"], level);
|
||||
} else if (parsed.level === "Error") {
|
||||
level = chalk.bold.redBright(level);
|
||||
level = styleText(["bold", "redBright"], level);
|
||||
}
|
||||
|
||||
info = `${level} ${chalk.gray(parsed.file)}: `;
|
||||
info = `${level} ${styleText("gray", parsed.file)}: `;
|
||||
|
||||
} else if (parsed.type === "action") {
|
||||
info = `[${chalk.yellow(parsed.action)}] `;
|
||||
info = `[${styleText("yellow", parsed.action)}] `;
|
||||
}
|
||||
|
||||
return time + info + parsed.message;
|
||||
@@ -226,6 +226,55 @@ export class LinkTransport extends Transport {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes log entries as lines to dirname/prefix-YYYY-MM-DD.log, opening
|
||||
* a new file when the UTC date changes.
|
||||
*/
|
||||
export class DailyFileTransport extends Transport {
|
||||
dirname: string;
|
||||
prefix: string;
|
||||
now: () => number;
|
||||
date?: string;
|
||||
stream?: WriteStream;
|
||||
|
||||
constructor(
|
||||
options: Transport.TransportStreamOptions & {
|
||||
dirname: string,
|
||||
prefix: string,
|
||||
now?: () => number,
|
||||
}
|
||||
) {
|
||||
super(options);
|
||||
this.dirname = options.dirname;
|
||||
this.prefix = options.prefix;
|
||||
this.now = options.now ?? Date.now;
|
||||
// eslint-disable-next-line node/no-sync
|
||||
mkdirSync(this.dirname, { recursive: true });
|
||||
}
|
||||
|
||||
log(info: any, callback: () => void) {
|
||||
const date = new Date(this.now()).toISOString().slice(0, 10);
|
||||
if (!this.stream || date !== this.date) {
|
||||
this.stream?.end();
|
||||
this.date = date;
|
||||
this.stream = createWriteStream(path.join(this.dirname, `${this.prefix}-${date}.log`), { flags: "a" });
|
||||
this.stream.on("error", err => this.emit("error", err));
|
||||
}
|
||||
this.stream.write(info[MESSAGE] + os.EOL);
|
||||
return callback();
|
||||
}
|
||||
|
||||
close(callback?: () => void) {
|
||||
const fileStream = this.stream;
|
||||
this.stream = undefined;
|
||||
if (fileStream) {
|
||||
fileStream.end(callback);
|
||||
} else {
|
||||
callback?.();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const logFileGlob = /^[a-z]+-(\d{4}-\d{2}-\d{2})\.log$/;
|
||||
const logIndexVersion = 2;
|
||||
|
||||
|
||||
+42
-10
@@ -7,10 +7,31 @@ import { PassThrough, type Readable } from "stream";
|
||||
import util from "util";
|
||||
import zlib from "zlib";
|
||||
import yauzl from "yauzl";
|
||||
import type yazl from "yazl";
|
||||
|
||||
const deflateRaw = util.promisify(zlib.deflateRaw);
|
||||
|
||||
/**
|
||||
* Entry in a ZipArchive
|
||||
*
|
||||
* Local subset of yauzl.Entry so the published declarations do not import
|
||||
* yauzl, which would make @types/yauzl a runtime dependency of lib.
|
||||
*/
|
||||
export interface ZipEntry {
|
||||
fileName: string;
|
||||
fileComment: string;
|
||||
versionMadeBy: number;
|
||||
versionNeededToExtract: number;
|
||||
generalPurposeBitFlag: number;
|
||||
compressionMethod: number;
|
||||
lastModFileTime: number;
|
||||
lastModFileDate: number;
|
||||
crc32: number;
|
||||
compressedSize: number;
|
||||
uncompressedSize: number;
|
||||
externalFileAttributes: number;
|
||||
getLastModDate(options?: { timezone?: "local" | "UTC" | null, forceDosFormat?: boolean }): Date;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Zip archive opened for random access reading
|
||||
@@ -21,10 +42,9 @@ const deflateRaw = util.promisify(zlib.deflateRaw);
|
||||
*/
|
||||
export class ZipArchive {
|
||||
private constructor(
|
||||
/** Underlying yauzl zip file. */
|
||||
public zipfile: yauzl.ZipFile,
|
||||
private zipfile: yauzl.ZipFile,
|
||||
/** Entries by file name in the order they appear in the central directory. */
|
||||
public entries: Map<string, yauzl.Entry>,
|
||||
public entries: Map<string, ZipEntry>,
|
||||
) { }
|
||||
|
||||
/**
|
||||
@@ -77,8 +97,17 @@ export class ZipArchive {
|
||||
*
|
||||
* @param entry - Entry to read, must belong to this archive.
|
||||
*/
|
||||
openStream(entry: yauzl.Entry): Promise<Readable> {
|
||||
return this.zipfile.openReadStreamPromise(entry);
|
||||
openStream(entry: ZipEntry): Promise<Readable> {
|
||||
return this.zipfile.openReadStreamPromise(entry as yauzl.Entry);
|
||||
}
|
||||
|
||||
/**
|
||||
* Open a stream of the content of an entry as stored in the archive
|
||||
*
|
||||
* @param entry - Entry to read, must belong to this archive.
|
||||
*/
|
||||
openRawStream(entry: ZipEntry): Promise<Readable> {
|
||||
return this.zipfile.openReadStreamPromise(entry as yauzl.Entry, { decodeFileData: false });
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -86,7 +115,7 @@ export class ZipArchive {
|
||||
*
|
||||
* @param entry - Entry to read, must belong to this archive.
|
||||
*/
|
||||
async readEntry(entry: yauzl.Entry) {
|
||||
async readEntry(entry: ZipEntry) {
|
||||
const chunks: Buffer[] = [];
|
||||
for await (const chunk of await this.openStream(entry)) {
|
||||
chunks.push(chunk);
|
||||
@@ -153,7 +182,10 @@ export function findRoot(zip: ZipArchive) {
|
||||
*
|
||||
* @param zipFile - zip file being written.
|
||||
*/
|
||||
export function zipOutputStream(zipFile: yazl.ZipFile) {
|
||||
export function zipOutputStream(zipFile: {
|
||||
outputStream: NodeJS.ReadableStream,
|
||||
on(event: "error", listener: (err: Error) => void): unknown,
|
||||
}) {
|
||||
const output = zipFile.outputStream as Readable;
|
||||
zipFile.on("error", err => output.destroy(err));
|
||||
return output;
|
||||
@@ -224,7 +256,7 @@ export class ZipWriter {
|
||||
* @param zip - Archive to copy from.
|
||||
* @param entry - Entry to copy, must belong to zip.
|
||||
*/
|
||||
async addEntry(zip: ZipArchive, entry: yauzl.Entry) {
|
||||
async addEntry(zip: ZipArchive, entry: ZipEntry) {
|
||||
const writerEntry: WriterEntry = {
|
||||
fileName: Buffer.from(entry.fileName, "utf8"),
|
||||
fileComment: Buffer.from(entry.fileComment, "utf8"),
|
||||
@@ -242,7 +274,7 @@ export class ZipWriter {
|
||||
offset: this.offset,
|
||||
};
|
||||
await this.write(this.localFileHeader(writerEntry));
|
||||
const stream = await zip.zipfile.openReadStreamPromise(entry, { decodeFileData: false });
|
||||
const stream = await zip.openRawStream(entry);
|
||||
for await (const chunk of stream) {
|
||||
await this.write(chunk);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
/* eslint-disable node/no-sync */
|
||||
import { strToU8, unzlibSync, zlibSync } from "fflate";
|
||||
|
||||
export function inflateSync(data: Uint8Array): Uint8Array {
|
||||
try {
|
||||
return unzlibSync(data);
|
||||
} catch (err: any) {
|
||||
// Match the error code node:zlib gives for invalid data.
|
||||
throw Object.assign(new Error(err.message), { code: "Z_DATA_ERROR" });
|
||||
}
|
||||
}
|
||||
|
||||
export function deflateSync(data: Uint8Array | string): Uint8Array {
|
||||
return zlibSync(typeof data === "string" ? strToU8(data) : data);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import zlib from "zlib";
|
||||
|
||||
export function inflateSync(data: Uint8Array): Uint8Array {
|
||||
// eslint-disable-next-line node/no-sync
|
||||
return zlib.inflateSync(data);
|
||||
}
|
||||
|
||||
export function deflateSync(data: Uint8Array | string): Uint8Array {
|
||||
// eslint-disable-next-line node/no-sync
|
||||
return zlib.deflateSync(data);
|
||||
}
|
||||
@@ -19,6 +19,7 @@
|
||||
"./src/hash.ts",
|
||||
"./src/ini.ts",
|
||||
"./src/is_deep_strict_equal.browser.ts",
|
||||
"./src/zlib_sync.browser.ts",
|
||||
"./src/link/*.ts",
|
||||
"./src/lua_tools.ts",
|
||||
"./src/logging.ts",
|
||||
|
||||
@@ -140,6 +140,46 @@ export type ExtensionComponent<K extends PluginExtensionSlot> = React.ComponentT
|
||||
|
||||
export type ExtensionComponents = { [K in PluginExtensionSlot]: Map<string, ExtensionComponent<K>> };
|
||||
|
||||
/**
|
||||
* Action added by a plugin to the actions dropdown of a page.
|
||||
*/
|
||||
export type PluginPageAction = {
|
||||
/** Unique within the plugin, the web UI prefixes it with the plugin name. */
|
||||
key: string;
|
||||
label: React.ReactNode;
|
||||
icon?: React.ReactNode;
|
||||
disabled?: boolean;
|
||||
danger?: boolean;
|
||||
onClick?: () => void;
|
||||
};
|
||||
|
||||
export type PageActionsSlotProps = {
|
||||
/** Controller control button on the controller page. */
|
||||
ControllerPage: Record<never, never>;
|
||||
/** Host control button on each host page. */
|
||||
HostViewPage: {
|
||||
host: lib.HostDetails;
|
||||
};
|
||||
/** More dropdown on each instance and template page. */
|
||||
InstanceViewPage: {
|
||||
instance: lib.InstanceDetails;
|
||||
};
|
||||
/** More dropdown on each user page. */
|
||||
UserViewPage: {
|
||||
user: lib.UserDetails;
|
||||
};
|
||||
};
|
||||
|
||||
export type PageActionsSlot = keyof PageActionsSlotProps;
|
||||
|
||||
/**
|
||||
* Returns the actions to add to a page. It is called during render like a
|
||||
* React hook, so it may call hooks such as useAccount or useState.
|
||||
*/
|
||||
export type PageActionsHook<K extends PageActionsSlot> = (props: PageActionsSlotProps[K]) => PluginPageAction[];
|
||||
|
||||
export type PageActions = { [K in PageActionsSlot]: Map<string, PageActionsHook<K>> };
|
||||
|
||||
export type WebPluginContext = lib.PluginLoadContext<{
|
||||
control: Control;
|
||||
container: any;
|
||||
@@ -156,6 +196,7 @@ export class WebHooks extends lib.AsyncHookCollection {
|
||||
this.extensionComponents = this.newHook();
|
||||
this.inputComponents = this.newHook();
|
||||
this.loginForms = this.newHook();
|
||||
this.pageActions = this.newHook();
|
||||
this.pages = this.newHook();
|
||||
}
|
||||
|
||||
@@ -225,6 +266,28 @@ export class WebHooks extends lib.AsyncHookCollection {
|
||||
*/
|
||||
readonly loginForms: lib.AsyncHook<[], PluginLoginForm[]>;
|
||||
|
||||
/**
|
||||
* Additional actions for the actions dropdown on resource pages.
|
||||
*
|
||||
* Each plugin may return a partial mapping of page names to
|
||||
* functions returning the actions for that page. Plugin actions are
|
||||
* placed after the built in ones and before stop and delete.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* control.hooks.pageActions.attach("my_plugin", () => ({
|
||||
* UserViewPage: ({ user }) => [{
|
||||
* key: "warn",
|
||||
* label: "Warn",
|
||||
* onClick: () => control.send(new WarnUserRequest(user.name)),
|
||||
* }],
|
||||
* }));
|
||||
* ```
|
||||
*/
|
||||
readonly pageActions: lib.AsyncHook<[], Partial<{
|
||||
[K in PageActionsSlot]: PageActionsHook<K>;
|
||||
}>>;
|
||||
|
||||
/**
|
||||
* Additional pages accessible on the web ui.
|
||||
*
|
||||
|
||||
@@ -12,6 +12,7 @@ import { InputInstanceTemplate } from "./components/InputInstance";
|
||||
import { InputTargetVersion, InputPartialVersion, InputFullVersion } from "./components/InputVersion";
|
||||
import { Control, ControlConnector } from "./util/websocket";
|
||||
import { loadedPluginSetKey } from "./util/pluginSet";
|
||||
import { pageActionsFromHooks } from "./util/plugin_actions";
|
||||
import * as WebPlugin from "./BaseWebPlugin";
|
||||
import { pages } from "./pages";
|
||||
|
||||
@@ -209,6 +210,7 @@ export default async function bootstrap() {
|
||||
|
||||
control.inputComponents = await inputComponentsFromHooks(control);
|
||||
control.extensionComponents = await extensionComponentsFromHooks(control);
|
||||
control.pageActions = await pageActionsFromHooks(control.hooks);
|
||||
control.loginForms = await loginFormsFromHooks(control);
|
||||
control.pages = await pagesFromHooks(control);
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ import PageLayout from "./PageLayout";
|
||||
import notify, { notifyErrorHandler } from "../util/notify";
|
||||
import webUiPackage from "../../package.json";
|
||||
import { hasNpmButtonPermission, NpmButton } from "./NpmButton";
|
||||
import usePluginActions from "../util/usePluginActions";
|
||||
import VariableDropdownButton, { VariableDropdownButtonProps } from "./VariableDropdownButton";
|
||||
|
||||
const { Title } = Typography;
|
||||
@@ -30,6 +31,7 @@ function ControllerControlButton({ canRestart, restartRequired }: ControllerCont
|
||||
const control = useContext(ControlContext);
|
||||
const [modal, modalContext] = Modal.useModal();
|
||||
const actions: VariableDropdownButtonProps["actions"] = [];
|
||||
const pluginActions = usePluginActions("ControllerPage", {});
|
||||
|
||||
if (account.hasPermission("core.controller.restart") && canRestart) {
|
||||
actions.push({
|
||||
@@ -71,6 +73,8 @@ function ControllerControlButton({ canRestart, restartRequired }: ControllerCont
|
||||
});
|
||||
}
|
||||
|
||||
actions.push(...pluginActions);
|
||||
|
||||
if (account.hasPermission("core.controller.stop")) {
|
||||
actions.push({
|
||||
key: "stop",
|
||||
|
||||
@@ -25,22 +25,25 @@ import {
|
||||
import { useSystems } from "../model/system";
|
||||
import useResourceState from "../util/useResourceState";
|
||||
import { hasNpmButtonPermission, NpmButton } from "./NpmButton";
|
||||
import usePluginActions from "../util/usePluginActions";
|
||||
import VariableDropdownButton, { VariableDropdownButtonProps } from "./VariableDropdownButton";
|
||||
|
||||
const { Title } = Typography;
|
||||
|
||||
type HostControlButtonProps = {
|
||||
hostId: number,
|
||||
host: lib.HostDetails,
|
||||
instances: ReadonlyMap<number, lib.InstanceDetails>,
|
||||
canRestart?: boolean,
|
||||
restartRequired?: boolean,
|
||||
};
|
||||
|
||||
function HostControlButton({ hostId, instances, canRestart, restartRequired }: HostControlButtonProps) {
|
||||
function HostControlButton({ host, instances, canRestart, restartRequired }: HostControlButtonProps) {
|
||||
const account = useAccount();
|
||||
const control = useContext(ControlContext);
|
||||
const [modal, modalContext] = Modal.useModal();
|
||||
const hostId = host.id;
|
||||
const actions: VariableDropdownButtonProps["actions"] = [];
|
||||
const pluginActions = usePluginActions("HostViewPage", { host });
|
||||
|
||||
if (account.hasPermission("core.host.restart") && canRestart) {
|
||||
actions.push({
|
||||
@@ -93,6 +96,8 @@ function HostControlButton({ hostId, instances, canRestart, restartRequired }: H
|
||||
});
|
||||
}
|
||||
|
||||
actions.push(...pluginActions);
|
||||
|
||||
if (account.hasPermission("core.host.stop")) {
|
||||
actions.push({
|
||||
key: "stop",
|
||||
@@ -174,7 +179,7 @@ export default function HostViewPage() {
|
||||
|
||||
let hostButtons = <Space wrap>
|
||||
<HostControlButton
|
||||
hostId={hostId}
|
||||
host={host}
|
||||
instances={hostInstances}
|
||||
canRestart={system?.canRestart}
|
||||
restartRequired={system?.restartRequired}
|
||||
|
||||
@@ -36,6 +36,7 @@ import { useDefaultModPack, useModPacks } from "../model/mod_pack";
|
||||
import InstanceModPack from "./InstanceModPack";
|
||||
import useLocalStorage from "../util/useLocalStorage";
|
||||
import useResourceState from "../util/useResourceState";
|
||||
import usePluginActions from "../util/usePluginActions";
|
||||
import { MetricRelativeDate } from "./system_metrics";
|
||||
|
||||
type MenuItem = Required<MenuProps>["items"][number];
|
||||
@@ -150,6 +151,7 @@ function InstanceButtons(props: { instance: lib.InstanceDetails }) {
|
||||
let [host] = useHost(instance.assignedHost);
|
||||
const [instances] = useInstances();
|
||||
const canCreate = account.hasPermission("core.instance.create");
|
||||
const pluginActions = usePluginActions("InstanceViewPage", { instance });
|
||||
|
||||
let instanceButtonMenuItems: MenuItem[] = [];
|
||||
if (account.hasPermission("core.instance.export_data")) {
|
||||
@@ -166,6 +168,7 @@ function InstanceButtons(props: { instance: lib.InstanceDetails }) {
|
||||
label: "Extract players from save",
|
||||
});
|
||||
}
|
||||
instanceButtonMenuItems.push(...pluginActions);
|
||||
if (account.hasPermission("core.instance.delete")) {
|
||||
if (instanceButtonMenuItems.length) {
|
||||
instanceButtonMenuItems.push({ type: "divider" });
|
||||
@@ -230,6 +233,10 @@ function InstanceButtons(props: { instance: lib.InstanceDetails }) {
|
||||
icon={<DeleteOutlined />}
|
||||
onClick={confirmDelete}
|
||||
>Delete</Button>}
|
||||
{pluginActions.length > 0
|
||||
&& <Dropdown placement="bottomRight" trigger={["click"]} menu={{ items: pluginActions }}>
|
||||
<Button>More <DownOutlined /></Button>
|
||||
</Dropdown>}
|
||||
</Space>;
|
||||
}
|
||||
|
||||
@@ -256,11 +263,7 @@ function InstanceButtons(props: { instance: lib.InstanceDetails }) {
|
||||
buttonText="Create template"
|
||||
buttonType="default"
|
||||
/>}
|
||||
{account.hasAnyPermission(
|
||||
"core.instance.export_data",
|
||||
"core.instance.extract_players",
|
||||
"core.instance.delete",
|
||||
)
|
||||
{instanceButtonMenuItems.length > 0
|
||||
&& <Dropdown placement="bottomRight" trigger={["click"]} menu={instanceButtonsMenuProps}>
|
||||
<Button>More <DownOutlined /></Button>
|
||||
</Dropdown>}
|
||||
|
||||
@@ -4,8 +4,9 @@ import { useNavigate, useParams } from "react-router-dom";
|
||||
import {
|
||||
Button, Card, ColorPicker, Checkbox, Col, ConfigProvider, Descriptions, Form, Input, Pagination,
|
||||
Popconfirm, Row, Table, Tag, Typography, Select, Skeleton, Space, Switch, Modal, Tooltip,
|
||||
TablePaginationConfig, Alert, notification, FormProps, Collapse,
|
||||
TablePaginationConfig, Alert, notification, FormProps, Collapse, Upload,
|
||||
} from "antd";
|
||||
import type { UploadChangeParam } from "antd/es/upload";
|
||||
|
||||
import {
|
||||
ExportOutlined, FileUnknownOutlined, FileExclamationOutlined, FileSyncOutlined,
|
||||
@@ -31,6 +32,9 @@ import SectionHeader from "./SectionHeader";
|
||||
import ResourceNotes from "./ResourceNotes";
|
||||
import ResourceState from "./ResourceState";
|
||||
import ModDetails from "./ModDetails";
|
||||
import { Dropzone } from "./Dropzone";
|
||||
import UploadButton from "./UploadButton";
|
||||
import { useUploadProgress } from "./UploadProgress";
|
||||
import { InputPartialVersion } from "./InputVersion";
|
||||
import Link from "./Link";
|
||||
|
||||
@@ -526,6 +530,16 @@ function DownloadDependenciesButton(props: DownloadDependenciesProps) {
|
||||
</>;
|
||||
}
|
||||
|
||||
function UploadDragger(props: React.ComponentProps<typeof Upload.Dragger>) {
|
||||
if (props.disabled) {
|
||||
return <>{props.children}</>;
|
||||
}
|
||||
return <Upload.Dragger className="save-list-dragger" openFileDialogOnClick={false} {...props}>
|
||||
<Dropzone />
|
||||
{props.children}
|
||||
</Upload.Dragger>;
|
||||
}
|
||||
|
||||
type ModsTableProps = {
|
||||
modPack: lib.ModPack;
|
||||
changes: ModChange[];
|
||||
@@ -535,6 +549,8 @@ type ModsTableProps = {
|
||||
};
|
||||
function ModsTable(props: ModsTableProps) {
|
||||
const account = useAccount();
|
||||
const control = useContext(ControlContext);
|
||||
const { customRequest, list: uploadList } = useUploadProgress();
|
||||
const [modInfos] = useMods();
|
||||
|
||||
const [showAddMods, setShowAddMods] = useState(false);
|
||||
@@ -600,6 +616,53 @@ function ModsTable(props: ModsTableProps) {
|
||||
}
|
||||
mods = lib.applyModRecordAdvisories(mods, lib.getInstalledModUpdates(mods, modInfos.values()));
|
||||
|
||||
const versionsByName = new Map<string, lib.ModInfo[]>();
|
||||
for (const info of modInfos.values()) {
|
||||
const versions = versionsByName.get(info.name) ?? [];
|
||||
versions.push(info);
|
||||
versionsByName.set(info.name, versions);
|
||||
}
|
||||
for (const versions of versionsByName.values()) {
|
||||
versions.sort((a, b) => b.integerVersion - a.integerVersion);
|
||||
}
|
||||
let packFactorioVersion: lib.MajorMinorVersion | undefined;
|
||||
try {
|
||||
packFactorioVersion = lib.normaliseMajorMinorVersion(props.modPack.factorioVersion);
|
||||
} catch {
|
||||
// Invalid pack version, every mod already shows wrong_factorio_version
|
||||
}
|
||||
|
||||
function versionSelect(mod: lib.ModRecord) {
|
||||
const versions = versionsByName.get(mod.name) ?? [];
|
||||
if (deletedMods.has(mod.name) || !versions.some(info => info.version !== mod.version)) {
|
||||
return mod.version;
|
||||
}
|
||||
const options = versions.map(info => ({
|
||||
value: info.version,
|
||||
label: info.factorioVersion === packFactorioVersion
|
||||
? info.version : `${info.version} (Factorio ${info.factorioVersion})`,
|
||||
}));
|
||||
if (!versions.some(info => info.version === mod.version)) {
|
||||
options.unshift({ value: mod.version, label: `${mod.version} (missing)` });
|
||||
}
|
||||
return <Select
|
||||
size="small"
|
||||
variant="borderless"
|
||||
style={{ marginInlineStart: -8 }} // Line up with the plain text versions
|
||||
popupMatchSelectWidth={false}
|
||||
value={mod.version}
|
||||
options={options}
|
||||
onChange={(version: string) => {
|
||||
const info = versions.find(candidate => candidate.version === version)!;
|
||||
props.onChange({
|
||||
type: "mods.set",
|
||||
name: mod.name,
|
||||
value: { ...props.modPack.mods.get(mod.name)!, version: info.version, sha1: info.sha1 },
|
||||
});
|
||||
}}
|
||||
/>;
|
||||
}
|
||||
|
||||
async function fixDependencyIssues(mod: lib.ModRecord) {
|
||||
if (!mod.info) {
|
||||
throw new Error("Mod record has no info"); // Should be unreachable
|
||||
@@ -696,6 +759,33 @@ function ModsTable(props: ModsTableProps) {
|
||||
</Space>;
|
||||
}
|
||||
|
||||
function onUpload({ file }: UploadChangeParam) {
|
||||
if (file.status !== "done") {
|
||||
return;
|
||||
}
|
||||
for (const json of file.response.mods) {
|
||||
const mod = lib.ModInfo.fromJSON(json);
|
||||
props.onChange({
|
||||
type: "mods.set",
|
||||
name: mod.name,
|
||||
value: { name: mod.name, enabled: true, version: mod.version, sha1: mod.sha1 },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const canUpload = account.hasPermission("core.mod.upload");
|
||||
const uploadProps = {
|
||||
disabled: !canUpload,
|
||||
name: "file",
|
||||
accept: ".zip",
|
||||
multiple: true,
|
||||
showUploadList: false,
|
||||
headers: { "X-Access-Token": control.connector.token || "" },
|
||||
action: `${webRoot}api/upload-mod`,
|
||||
customRequest,
|
||||
onChange: onUpload,
|
||||
};
|
||||
|
||||
return <>
|
||||
<SectionHeader
|
||||
title="Mods"
|
||||
@@ -732,6 +822,7 @@ function ModsTable(props: ModsTableProps) {
|
||||
builtInModNames={props.builtInModNames}
|
||||
checkForUpdates
|
||||
/>}
|
||||
{canUpload && <Upload {...uploadProps}><UploadButton /></Upload>}
|
||||
<Button icon={<PlusOutlined />} onClick={() => setShowAddMods(true)}>Add</Button>
|
||||
</Space>}
|
||||
/>
|
||||
@@ -743,6 +834,8 @@ function ModsTable(props: ModsTableProps) {
|
||||
>
|
||||
<SearchModsTable modPack={props.modPack} changes={props.changes} onChange={props.onChange} />
|
||||
</Card>}
|
||||
{uploadList}
|
||||
<UploadDragger {...uploadProps}>
|
||||
<Table
|
||||
size="small"
|
||||
rowSelection={{
|
||||
@@ -822,7 +915,8 @@ function ModsTable(props: ModsTableProps) {
|
||||
},
|
||||
{
|
||||
title: "Version",
|
||||
dataIndex: "version",
|
||||
key: "version",
|
||||
render: (_, mod: lib.ModRecord) => versionSelect(mod),
|
||||
sorter: (a, b) => strcmp(a.version, b.version),
|
||||
},
|
||||
{
|
||||
@@ -833,7 +927,11 @@ function ModsTable(props: ModsTableProps) {
|
||||
},
|
||||
]}
|
||||
expandable={{
|
||||
expandedRowRender: mod => <ModDetails mod={mod} mods={mods.filter(m => m.enabled)} actions={actions} />,
|
||||
expandedRowRender: mod => <ModDetails
|
||||
mod={mod}
|
||||
mods={mods.filter(m => m.enabled)}
|
||||
actions={actions}
|
||||
/>,
|
||||
expandedRowClassName: () => "no-expanded-padding",
|
||||
}}
|
||||
dataSource={mods}
|
||||
@@ -849,6 +947,7 @@ function ModsTable(props: ModsTableProps) {
|
||||
return "";
|
||||
}}
|
||||
/>
|
||||
</UploadDragger>
|
||||
</>;
|
||||
}
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ import Link from "./Link";
|
||||
import ModDetails from "./ModDetails";
|
||||
import { Dropzone } from "./Dropzone";
|
||||
import UploadButton from "./UploadButton";
|
||||
import { useUploadProgress } from "./UploadProgress";
|
||||
import { InputPartialVersion } from "./InputVersion";
|
||||
|
||||
const strcmp = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" }).compare;
|
||||
@@ -524,6 +525,7 @@ export default function ModsPage() {
|
||||
let account = useAccount();
|
||||
let control = useContext(ControlContext);
|
||||
let [mods] = useMods();
|
||||
const { customRequest, list: uploadList } = useUploadProgress();
|
||||
let [modPacks] = useModPacks();
|
||||
const modPackTable = useTableQueryState<lib.ModPack>({
|
||||
namespace: "modPack", defaultSortKey: "name",
|
||||
@@ -582,6 +584,7 @@ export default function ModsPage() {
|
||||
"X-Access-Token": control.connector.token || "",
|
||||
}}
|
||||
action={`${webRoot}api/upload-mod`}
|
||||
customRequest={customRequest}
|
||||
>
|
||||
<UploadButton />
|
||||
</Upload>;
|
||||
@@ -635,6 +638,7 @@ export default function ModsPage() {
|
||||
<SearchModsButton />
|
||||
{uploadButton}
|
||||
</Space>} />
|
||||
{uploadList}
|
||||
|
||||
<Upload.Dragger
|
||||
className="save-list-dragger"
|
||||
@@ -646,6 +650,7 @@ export default function ModsPage() {
|
||||
"X-Access-Token": control.connector.token || "",
|
||||
}}
|
||||
action={`${webRoot}api/upload-mod`}
|
||||
customRequest={customRequest}
|
||||
showUploadList={false}
|
||||
>
|
||||
<Dropzone />
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import React, { useContext, useState } from "react";
|
||||
import {
|
||||
message, Button, Checkbox, Form, Input, List, Modal,
|
||||
Popconfirm, Progress, Select, Space, Table, Tooltip, Upload,
|
||||
message, Button, Checkbox, Form, Input, Modal,
|
||||
Popconfirm, Select, Space, Table, Tooltip, Typography, Upload,
|
||||
} from "antd";
|
||||
import CaretLeftOutlined from "@ant-design/icons/CaretLeftOutlined";
|
||||
import LeftOutlined from "@ant-design/icons/LeftOutlined";
|
||||
@@ -10,6 +10,8 @@ import * as lib from "@clusterio/lib";
|
||||
|
||||
import { useAccount } from "../model/account";
|
||||
import useTableQueryState from "../util/useTableQueryState";
|
||||
import useColumnSearch from "../util/useColumnSearch";
|
||||
import type { Control } from "../util/websocket";
|
||||
import ControlContext from "./ControlContext";
|
||||
import CreateSaveModal from "./CreateSaveModal";
|
||||
import SectionHeader from "./SectionHeader";
|
||||
@@ -19,6 +21,7 @@ import { useSavesOfInstance } from "../model/saves";
|
||||
import { notifyErrorHandler } from "../util/notify";
|
||||
import { Dropzone } from "./Dropzone";
|
||||
import UploadButton from "./UploadButton";
|
||||
import { useUploadProgress } from "./UploadProgress";
|
||||
|
||||
|
||||
type ModalProps = {
|
||||
@@ -106,17 +109,66 @@ function CopyModal(props: ModalProps) {
|
||||
}
|
||||
|
||||
|
||||
function TransferModal(props: ModalProps) {
|
||||
/** Runs action on each save in turn, returning the names that failed. */
|
||||
async function forEachSave(
|
||||
saves: lib.SaveDetails[],
|
||||
action: (save: lib.SaveDetails) => Promise<unknown>,
|
||||
errorText: string,
|
||||
) {
|
||||
const failed: string[] = [];
|
||||
for (const save of saves) {
|
||||
try {
|
||||
await action(save);
|
||||
} catch (err: any) {
|
||||
failed.push(save.name);
|
||||
notifyErrorHandler(`${errorText} ${save.name}`)(err);
|
||||
}
|
||||
}
|
||||
return failed;
|
||||
}
|
||||
|
||||
function downloadSave(control: Control, instanceId: number, name: string) {
|
||||
return control.send(new lib.InstanceDownloadSaveRequest(instanceId, name)).then(streamId => {
|
||||
let url = new URL(webRoot, document.location.href);
|
||||
url.pathname += `api/stream/${streamId}`;
|
||||
// A download link instead of navigating, so several downloads do not cancel each other.
|
||||
const link = document.createElement("a");
|
||||
link.href = url.href;
|
||||
link.download = name;
|
||||
link.click();
|
||||
});
|
||||
}
|
||||
|
||||
type TransferModalProps = {
|
||||
disabled: boolean;
|
||||
saves: lib.SaveDetails[];
|
||||
instanceId: number;
|
||||
onDone?: (failed: string[]) => void;
|
||||
};
|
||||
|
||||
function TransferModal(props: TransferModalProps) {
|
||||
let account = useAccount();
|
||||
let control = useContext(ControlContext);
|
||||
let [open, setOpen] = useState(false);
|
||||
let [form] = Form.useForm();
|
||||
let [instances] = useInstances();
|
||||
const single = props.saves.length === 1 ? props.saves[0] : undefined;
|
||||
const canCopy = account.hasPermission("core.instance.save.copy");
|
||||
|
||||
function transfer(values: { targetInstanceId: number, transferredName?: string, copy: boolean }) {
|
||||
return forEachSave(props.saves, save => control.send(new lib.InstanceTransferSaveRequest(
|
||||
props.instanceId,
|
||||
save.name,
|
||||
values.targetInstanceId,
|
||||
(single && values.transferredName) || save.name,
|
||||
values.copy,
|
||||
)), "Error transfering");
|
||||
}
|
||||
|
||||
return <>
|
||||
<Button disabled={props.disabled} onClick={() => setOpen(true)}>Transfer</Button>
|
||||
<Modal
|
||||
title="Transfer save"
|
||||
title={single ? "Transfer save" : `Transfer ${props.saves.length} saves`}
|
||||
okText="Transfer"
|
||||
open={open}
|
||||
onOk={() => form.submit()}
|
||||
@@ -127,22 +179,17 @@ function TransferModal(props: ModalProps) {
|
||||
form={form}
|
||||
labelCol={{ span: 6 }}
|
||||
wrapperCol={{ span: 18 }}
|
||||
initialValues={{ transferredName: props.save.name, copy: false }}
|
||||
initialValues={{ transferredName: single?.name, copy: !single && canCopy }}
|
||||
onFinish={values => {
|
||||
setOpen(false);
|
||||
let hide = message.loading("Transferring save...", 0);
|
||||
control.send(new lib.InstanceTransferSaveRequest(
|
||||
props.instanceId,
|
||||
props.save.name,
|
||||
values.targetInstanceId,
|
||||
values.transferredName || props.save.name,
|
||||
values.copy,
|
||||
)).then(() => {
|
||||
message.success("Transfer complete");
|
||||
let hide = message.loading(single ? "Transferring save..." : "Transferring saves...", 0);
|
||||
transfer(values).then(failed => {
|
||||
if (failed.length < props.saves.length) {
|
||||
message.success(failed.length ? "Transfer partially complete" : "Transfer complete");
|
||||
}
|
||||
form.resetFields();
|
||||
}).catch(
|
||||
notifyErrorHandler("Error transfering save")
|
||||
).finally(() => hide());
|
||||
props.onDone?.(failed);
|
||||
}).finally(() => hide());
|
||||
}}
|
||||
>
|
||||
<Form.Item
|
||||
@@ -169,23 +216,21 @@ function TransferModal(props: ModalProps) {
|
||||
</Select.Option>)}
|
||||
</Select>
|
||||
</Form.Item>
|
||||
<Form.Item
|
||||
{single && <Form.Item
|
||||
name="transferredName"
|
||||
label="Transferred name"
|
||||
>
|
||||
<Input
|
||||
disabled={!account.hasAnyPermission("core.instance.save.rename", "core.instance.save.copy")}
|
||||
/>
|
||||
</Form.Item>
|
||||
</Form.Item>}
|
||||
<Form.Item
|
||||
name="copy"
|
||||
valuePropName="checked"
|
||||
label="Copy"
|
||||
tooltip="Copy instead of moving the save to the new instance."
|
||||
>
|
||||
<Checkbox
|
||||
disabled={!account.hasPermission("core.instance.save.copy")}
|
||||
/>
|
||||
<Checkbox disabled={!canCopy} />
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
@@ -193,22 +238,78 @@ function TransferModal(props: ModalProps) {
|
||||
}
|
||||
|
||||
|
||||
type File = {
|
||||
uid: string;
|
||||
name: string;
|
||||
percent: number;
|
||||
status: string;
|
||||
type BulkActionsProps = {
|
||||
disabled: boolean;
|
||||
instanceId: number;
|
||||
saves: lib.SaveDetails[];
|
||||
onDone: (failed: string[]) => void;
|
||||
};
|
||||
|
||||
function BulkActions(props: BulkActionsProps) {
|
||||
const account = useAccount();
|
||||
const control = useContext(ControlContext);
|
||||
const [running, setRunning] = useState<string>();
|
||||
const count = props.saves.length;
|
||||
|
||||
function run(name: string, action: (save: lib.SaveDetails) => Promise<unknown>, errorText: string) {
|
||||
setRunning(name);
|
||||
forEachSave(props.saves, action, errorText)
|
||||
.then(props.onDone)
|
||||
.finally(() => setRunning(undefined));
|
||||
}
|
||||
|
||||
const disabled = props.disabled || running !== undefined;
|
||||
return <Space wrap>
|
||||
<Typography.Text type="secondary">{count} selected</Typography.Text>
|
||||
{account.hasPermission("core.instance.save.download") && <Button
|
||||
disabled={disabled}
|
||||
loading={running === "download"}
|
||||
onClick={() => run(
|
||||
"download", save => downloadSave(control, props.instanceId, save.name), "Error downloading"
|
||||
)}
|
||||
>Download</Button>}
|
||||
{account.hasPermission("core.instance.save.transfer") && <TransferModal
|
||||
disabled={disabled} instanceId={props.instanceId} saves={props.saves} onDone={props.onDone}
|
||||
/>}
|
||||
{account.hasPermission("core.instance.save.delete") && <Popconfirm
|
||||
title={`Permanently delete ${count} ${count === 1 ? "save" : "saves"}?`}
|
||||
okText="Delete"
|
||||
okButtonProps={{ danger: true }}
|
||||
onConfirm={() => run("delete", save => control.send(
|
||||
new lib.InstanceDeleteSaveRequest(props.instanceId, save.name)
|
||||
), "Error deleting")}
|
||||
>
|
||||
<Button danger disabled={disabled} loading={running === "delete"}>Delete</Button>
|
||||
</Popconfirm>}
|
||||
</Space>;
|
||||
}
|
||||
|
||||
|
||||
export default function SavesList(props: { instance: lib.InstanceDetails }) {
|
||||
let account = useAccount();
|
||||
let control = useContext(ControlContext);
|
||||
let [saves] = useSavesOfInstance(props.instance.id);
|
||||
let [starting, setStarting] = useState(false);
|
||||
let [uploadingFiles, setUploadingFiles] = useState<File[]>([]);
|
||||
const { customRequest, list: uploadList } = useUploadProgress();
|
||||
const tableState = useTableQueryState<lib.SaveDetails>({
|
||||
namespace: "save", defaultSortKey: "mtimeMs", defaultSortOrder: "descend", pagination: { defaultPageSize: 10 },
|
||||
});
|
||||
const nameSearch = useColumnSearch<lib.SaveDetails>(tableState, "name", save => save.name, "Search saves");
|
||||
const [selectedNames, setSelectedNames] = useState<string[]>([]);
|
||||
|
||||
const saveList = [...saves.values()];
|
||||
const selectedSaves = saveList.filter(save => selectedNames.includes(save.name));
|
||||
const nameFilter = tableState.filteredValue("name")?.[0]?.toLowerCase();
|
||||
const visibleNames = saveList
|
||||
.filter(save => !nameFilter || save.name.toLowerCase().includes(nameFilter))
|
||||
.map(save => save.name);
|
||||
const visibleSelected = visibleNames.filter(name => selectedNames.includes(name)).length;
|
||||
const canTransfer = account.hasPermission("core.instance.save.transfer");
|
||||
const canSelect = account.hasAnyPermission(
|
||||
"core.instance.save.download",
|
||||
"core.instance.save.transfer",
|
||||
"core.instance.save.delete",
|
||||
);
|
||||
|
||||
let hostOffline = ["unassigned", "unknown"].includes(props.instance.status!);
|
||||
const saveTable = <Table
|
||||
@@ -225,6 +326,8 @@ export default function SavesList(props: { instance: lib.InstanceDetails }) {
|
||||
</>,
|
||||
sorter: (a, b) => a.name.localeCompare(b.name),
|
||||
sortOrder: tableState.sortOrder("name"),
|
||||
filteredValue: tableState.filteredValue("name"),
|
||||
...nameSearch,
|
||||
},
|
||||
{
|
||||
title: "Size",
|
||||
@@ -243,10 +346,24 @@ export default function SavesList(props: { instance: lib.InstanceDetails }) {
|
||||
sortOrder: tableState.sortOrder("mtimeMs"),
|
||||
},
|
||||
]}
|
||||
dataSource={[...saves.values()]}
|
||||
dataSource={saveList}
|
||||
pagination={tableState.pagination}
|
||||
onChange={tableState.onChange}
|
||||
rowKey={save => save.name}
|
||||
rowSelection={canSelect ? {
|
||||
selectedRowKeys: selectedSaves.map(save => save.name),
|
||||
onChange: keys => setSelectedNames(keys as string[]),
|
||||
// Select all covers every save matching the search, not just the current page.
|
||||
columnTitle: <Checkbox
|
||||
disabled={!visibleNames.length}
|
||||
checked={visibleNames.length > 0 && visibleSelected === visibleNames.length}
|
||||
indeterminate={visibleSelected > 0 && visibleSelected < visibleNames.length}
|
||||
onChange={e => setSelectedNames(e.target.checked
|
||||
? [...new Set([...selectedNames, ...visibleNames])]
|
||||
: selectedNames.filter(name => !visibleNames.includes(name))
|
||||
)}
|
||||
/>,
|
||||
} : undefined}
|
||||
expandable={{
|
||||
columnWidth: 33,
|
||||
expandRowByClick: true,
|
||||
@@ -276,19 +393,13 @@ export default function SavesList(props: { instance: lib.InstanceDetails }) {
|
||||
{account.hasPermission("core.instance.save.download") && <Button
|
||||
disabled={hostOffline}
|
||||
onClick={() => {
|
||||
control.send(
|
||||
new lib.InstanceDownloadSaveRequest(props.instance.id!, save.name)
|
||||
).then(streamId => {
|
||||
let url = new URL(webRoot, document.location.href);
|
||||
url.pathname += `api/stream/${streamId}`;
|
||||
document.location.assign(url);
|
||||
}).catch(
|
||||
downloadSave(control, props.instance.id!, save.name).catch(
|
||||
notifyErrorHandler("Error downloading save")
|
||||
);
|
||||
}}
|
||||
>Download</Button>}
|
||||
{account.hasPermission("core.instance.save.transfer") && <TransferModal
|
||||
disabled={hostOffline} instanceId={props.instance.id!} save={save}
|
||||
{canTransfer && <TransferModal
|
||||
disabled={hostOffline} instanceId={props.instance.id!} saves={[save]}
|
||||
/>}
|
||||
{account.hasPermission("core.instance.save.delete") && <Popconfirm
|
||||
title="Permanently delete save?"
|
||||
@@ -309,38 +420,6 @@ export default function SavesList(props: { instance: lib.InstanceDetails }) {
|
||||
}}
|
||||
/>;
|
||||
|
||||
function onChange(changeEvent: any) {
|
||||
if (["done", "error"].includes(changeEvent.file.status)) {
|
||||
if (changeEvent.file.status === "error") {
|
||||
notifyErrorHandler("Error uploading file")(changeEvent.file.error);
|
||||
}
|
||||
|
||||
let newUploadingFiles = [...uploadingFiles];
|
||||
let index = newUploadingFiles.findIndex(f => f.uid === changeEvent.file.uid);
|
||||
if (index !== -1) {
|
||||
newUploadingFiles.splice(index, 1);
|
||||
setUploadingFiles(newUploadingFiles);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
let file: File = {
|
||||
uid: changeEvent.file.uid,
|
||||
name: changeEvent.file.name,
|
||||
percent: changeEvent.file.percent,
|
||||
status: changeEvent.file.status,
|
||||
};
|
||||
|
||||
let newUploadingFiles = [...uploadingFiles];
|
||||
let index = newUploadingFiles.findIndex(f => f.uid === changeEvent.file.uid);
|
||||
if (index !== -1) {
|
||||
newUploadingFiles[index] = file;
|
||||
} else {
|
||||
newUploadingFiles.push(file);
|
||||
}
|
||||
setUploadingFiles(newUploadingFiles);
|
||||
}
|
||||
|
||||
let uploadProps = {
|
||||
disabled: hostOffline,
|
||||
name: "file",
|
||||
@@ -353,11 +432,17 @@ export default function SavesList(props: { instance: lib.InstanceDetails }) {
|
||||
},
|
||||
showUploadList: false,
|
||||
action: `${webRoot}api/upload-save`,
|
||||
onChange,
|
||||
customRequest,
|
||||
};
|
||||
|
||||
return <div>
|
||||
<SectionHeader title="Saves" extra={<Space wrap>
|
||||
{selectedSaves.length > 0 && <BulkActions
|
||||
disabled={hostOffline}
|
||||
instanceId={props.instance.id!}
|
||||
saves={selectedSaves}
|
||||
onDone={setSelectedNames}
|
||||
/>}
|
||||
{account.hasPermission("core.instance.save.upload") && <Upload {...uploadProps} >
|
||||
<UploadButton disabled={hostOffline}>
|
||||
Upload save
|
||||
@@ -365,6 +450,7 @@ export default function SavesList(props: { instance: lib.InstanceDetails }) {
|
||||
</Upload>}
|
||||
{account.hasPermission("core.instance.save.create") && <CreateSaveModal instance={props.instance} />}
|
||||
</Space>} />
|
||||
{uploadList}
|
||||
{
|
||||
account.hasPermission("core.instance.save.upload")
|
||||
? <Upload.Dragger className="save-list-dragger" openFileDialogOnClick={false} {...uploadProps}>
|
||||
@@ -373,15 +459,5 @@ export default function SavesList(props: { instance: lib.InstanceDetails }) {
|
||||
</Upload.Dragger>
|
||||
: saveTable
|
||||
}
|
||||
{uploadingFiles.length ? <List>
|
||||
{uploadingFiles.map(file => <List.Item key={file.uid}>
|
||||
{file.name}
|
||||
<Progress
|
||||
percent={file.percent}
|
||||
format={percent => `${Math.floor(percent || 0)}%`}
|
||||
status={file.status === "error" ? "exception" : "normal"}
|
||||
/>
|
||||
</List.Item>)}
|
||||
</List> : null}
|
||||
</div>;
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
import { useCallback, useState } from "react";
|
||||
import { Button, List, Progress, Typography, type UploadProps } from "antd";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
|
||||
import notify, { notifyErrorHandler } from "../util/notify";
|
||||
|
||||
type CustomRequest = NonNullable<UploadProps["customRequest"]>;
|
||||
|
||||
interface Upload {
|
||||
uid: string;
|
||||
name: string;
|
||||
loaded: number;
|
||||
total: number;
|
||||
abort: () => void;
|
||||
}
|
||||
|
||||
function errorMessage(err: any, body: any) {
|
||||
const errors = [...body?.request_errors ?? [], ...body?.errors ?? []];
|
||||
if (errors.length) {
|
||||
return errors.join(", ");
|
||||
}
|
||||
if (err instanceof Error) {
|
||||
return err.message;
|
||||
}
|
||||
return "Connection to the controller failed";
|
||||
}
|
||||
|
||||
/**
|
||||
* Track uploads made by antd Upload components and show them with a cancel button.
|
||||
* @returns customRequest to pass to every Upload sharing the list, and the list to render.
|
||||
*/
|
||||
export function useUploadProgress(): { customRequest: CustomRequest, list: React.ReactNode } {
|
||||
const [uploads, setUploads] = useState<Upload[]>([]);
|
||||
|
||||
function update(uid: string, change: Partial<Upload>) {
|
||||
setUploads(current => current.map(upload => (upload.uid === uid ? { ...upload, ...change } : upload)));
|
||||
}
|
||||
function remove(uid: string) {
|
||||
setUploads(current => current.filter(upload => upload.uid !== uid));
|
||||
}
|
||||
|
||||
const customRequest = useCallback<CustomRequest>((options, { defaultRequest }) => {
|
||||
const file = options.file as File & { uid: string };
|
||||
const uid = file.uid;
|
||||
// antd types this as void, rc-upload returns the abort handle.
|
||||
const request = defaultRequest({
|
||||
...options,
|
||||
onProgress(event) {
|
||||
options.onProgress?.(event);
|
||||
update(uid, { loaded: event.loaded ?? 0, total: event.total ?? file.size });
|
||||
},
|
||||
onSuccess(body, xhr) {
|
||||
options.onSuccess?.(body, xhr);
|
||||
remove(uid);
|
||||
},
|
||||
onError(err, body) {
|
||||
options.onError?.(err, body);
|
||||
remove(uid);
|
||||
notifyErrorHandler(`Error uploading ${file.name}`)(new Error(errorMessage(err, body)));
|
||||
},
|
||||
}) as unknown as { abort: () => void };
|
||||
const abort = () => {
|
||||
request.abort();
|
||||
// An aborted XMLHttpRequest calls neither onload nor onerror.
|
||||
options.onError?.(new Error("Upload cancelled"));
|
||||
remove(uid);
|
||||
notify(`Cancelled upload of ${file.name}`);
|
||||
};
|
||||
setUploads(current => [...current, { uid, name: file.name, loaded: 0, total: file.size, abort }]);
|
||||
return { abort };
|
||||
}, []);
|
||||
|
||||
const list = uploads.length ? <List
|
||||
size="small"
|
||||
className="upload-progress-list"
|
||||
dataSource={uploads}
|
||||
rowKey={upload => upload.uid}
|
||||
renderItem={upload => <List.Item
|
||||
actions={[<Button key="cancel" size="small" onClick={upload.abort}>Cancel</Button>]}
|
||||
>
|
||||
<div style={{ flex: 1, minWidth: 0 }}>
|
||||
<Typography.Text ellipsis>{upload.name}</Typography.Text>
|
||||
<Progress
|
||||
percent={upload.total ? upload.loaded / upload.total * 100 : 0}
|
||||
format={() => `${lib.formatBytes(upload.loaded)} / ${lib.formatBytes(upload.total)}`}
|
||||
/>
|
||||
</div>
|
||||
</List.Item>}
|
||||
/> : null;
|
||||
|
||||
return { customRequest, list };
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
import React, { useEffect, useContext, useState } from "react";
|
||||
import { useNavigate, useParams } from "react-router-dom";
|
||||
import {
|
||||
Button, Col, Descriptions, Form, Input, Popconfirm,
|
||||
Button, Col, Descriptions, Dropdown, Form, Input, Popconfirm,
|
||||
Row, Table, Tag, Select, Space, Switch,
|
||||
} from "antd";
|
||||
import DeleteOutlined from "@ant-design/icons/DeleteOutlined";
|
||||
import DownOutlined from "@ant-design/icons/DownOutlined";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
|
||||
@@ -21,6 +22,7 @@ import ResourceNotes from "./ResourceNotes";
|
||||
import ResourceState from "./ResourceState";
|
||||
import useRowNavigation from "../util/useRowNavigation";
|
||||
import useResourceState from "../util/useResourceState";
|
||||
import usePluginActions from "../util/usePluginActions";
|
||||
import Link from "./Link";
|
||||
import notify, { notifyErrorHandler } from "../util/notify";
|
||||
import { formatDuration } from "../util/time_format";
|
||||
@@ -29,6 +31,16 @@ import { formatFirstSeen, formatLastSeen, sortFirstSeen, sortLastSeen, useUser }
|
||||
const strcmp = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" }).compare;
|
||||
|
||||
|
||||
function UserPluginActions(props: { user: lib.UserDetails }) {
|
||||
const pluginActions = usePluginActions("UserViewPage", { user: props.user });
|
||||
if (!pluginActions.length) {
|
||||
return null;
|
||||
}
|
||||
return <Dropdown placement="bottomRight" trigger={["click"]} menu={{ items: pluginActions }}>
|
||||
<Button>More <DownOutlined /></Button>
|
||||
</Dropdown>;
|
||||
}
|
||||
|
||||
// The user page has a lot of optional elements, that does not make it
|
||||
// praticularly complicated.
|
||||
// eslint-disable-next-line complexity
|
||||
@@ -167,6 +179,7 @@ export default function UserViewPage() {
|
||||
<DeleteOutlined />
|
||||
</Button>
|
||||
</Popconfirm>}
|
||||
<UserPluginActions user={user} />
|
||||
</Space>}
|
||||
/>
|
||||
<Form
|
||||
|
||||
@@ -15,19 +15,20 @@ export default function VariableDropdownButton(props: VariableDropdownButtonProp
|
||||
return <></>;
|
||||
}
|
||||
|
||||
const mainDisabled = props.disabled || mainAction.disabled;
|
||||
const buttonProps: DropdownButtonProps = {
|
||||
...props,
|
||||
danger: mainAction.danger,
|
||||
onClick: (event) => {
|
||||
event.stopPropagation();
|
||||
if (mainAction.onClick) {
|
||||
if (!mainDisabled && mainAction.onClick) {
|
||||
mainAction.onClick();
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
return menuActions.length === 0
|
||||
? <Button {...buttonProps}>
|
||||
? <Button {...buttonProps} disabled={mainDisabled}>
|
||||
{mainAction.label}
|
||||
</Button>
|
||||
: <Dropdown.Button
|
||||
@@ -35,7 +36,8 @@ export default function VariableDropdownButton(props: VariableDropdownButtonProp
|
||||
// stopPropagation is needed to prevent propagation to table rows
|
||||
menu={{ items: menuActions, onClick: (e: any) => e.domEvent.stopPropagation() }}
|
||||
buttonsRender={([left, right]) => [
|
||||
left, React.cloneElement(right as any, { onClick: (e: any) => e.stopPropagation() }),
|
||||
React.cloneElement(left as any, { disabled: mainDisabled }),
|
||||
React.cloneElement(right as any, { onClick: (e: any) => e.stopPropagation() }),
|
||||
]}
|
||||
>
|
||||
{mainAction.label}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
import type {
|
||||
PageActions, PageActionsHook, PageActionsSlot, PageActionsSlotProps, PluginPageAction, WebHooks,
|
||||
} from "../BaseWebPlugin";
|
||||
|
||||
export async function pageActionsFromHooks(hooks: WebHooks) {
|
||||
const pageActions: Partial<PageActions> = {};
|
||||
for (const [source, record] of await hooks.pageActions.collectEntries()) {
|
||||
for (const [slot, useActions] of Object.entries(record) as [PageActionsSlot, PageActionsHook<any>][]) {
|
||||
if (!pageActions[slot]) {
|
||||
pageActions[slot] = new Map();
|
||||
}
|
||||
pageActions[slot].set(source, useActions);
|
||||
}
|
||||
}
|
||||
return pageActions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Call each plugin's actions hook for a page and prefix the keys with the
|
||||
* plugin name. Must be called during render, the set of plugins is fixed
|
||||
* after bootstrap so the hooks are called in the same order every time.
|
||||
*/
|
||||
export function callPageActions<K extends PageActionsSlot>(
|
||||
hooks: Map<string, PageActionsHook<K>> | undefined,
|
||||
props: PageActionsSlotProps[K],
|
||||
) {
|
||||
const actions: PluginPageAction[] = [];
|
||||
for (const [source, useActions] of hooks ?? []) {
|
||||
for (const action of useActions(props)) {
|
||||
actions.push({ ...action, key: `${source}.${action.key}` });
|
||||
}
|
||||
}
|
||||
return actions;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { useContext } from "react";
|
||||
|
||||
import ControlContext from "../components/ControlContext";
|
||||
import type { PageActionsSlot, PageActionsSlotProps, PluginPageAction } from "../BaseWebPlugin";
|
||||
import { callPageActions } from "./plugin_actions";
|
||||
|
||||
/** Actions plugins add to the dropdown of a page. */
|
||||
export default function usePluginActions<K extends PageActionsSlot>(
|
||||
slot: K,
|
||||
props: PageActionsSlotProps[K],
|
||||
): PluginPageAction[] {
|
||||
const control = useContext(ControlContext);
|
||||
return callPageActions(control.pageActions[slot], props);
|
||||
}
|
||||
@@ -64,6 +64,7 @@ export class Control extends lib.Link {
|
||||
public loadedPlugins = new Map<string, lib.PluginWebEnvInfo>();
|
||||
public inputComponents = new Map<string, WebPlugin.InputComponent>();
|
||||
public extensionComponents: Partial<WebPlugin.ExtensionComponents> = {};
|
||||
public pageActions: Partial<WebPlugin.PageActions> = {};
|
||||
public loginForms: WebPlugin.PluginLoginForm[] = [];
|
||||
public pages: WebPlugin.PluginPage[] = [];
|
||||
|
||||
|
||||
+1
-2
@@ -21,8 +21,8 @@ catalog:
|
||||
'@types/yazl': ^3.3.1
|
||||
'@vitejs/plugin-react': ^6.1.2
|
||||
antd: ^5.29.3
|
||||
chalk: ^6.0.1
|
||||
express: ^5.3.0
|
||||
fflate: ^0.8.3
|
||||
jsonwebtoken: ^9.0.3
|
||||
react: ^18.3.1
|
||||
react-dom: ^18.3.1
|
||||
@@ -34,7 +34,6 @@ catalog:
|
||||
typescript: ^7.0.2
|
||||
vite: ^8.3.4
|
||||
winston: ^3.19.0
|
||||
winston-daily-rotate-file: ^5.0.0
|
||||
winston-transport: ^4.9.0
|
||||
ws: ^8.22.0
|
||||
yargs: ^18.2.0
|
||||
|
||||
@@ -10,7 +10,7 @@ import path from "node:path";
|
||||
import fs from "node:fs/promises";
|
||||
import yargs from "yargs";
|
||||
|
||||
import npmPackage from "./package.json" with { type: "json" };
|
||||
import npmPackage from "../package.json" with { type: "json" };
|
||||
const workspaces = npmPackage.workspaces
|
||||
.map(p => p.slice(0, -2))
|
||||
.filter(p => !p.includes("external_plugins"));
|
||||
+42
-24
@@ -195,20 +195,21 @@ export function checkPullRequestBody(
|
||||
|
||||
function changelogFromPullRequests(pullRequests: Issue[], issues: Issue[]) {
|
||||
const mergedChangelog: Changelog = Object.create(null);
|
||||
const problems: string[] = [];
|
||||
for (const pr of pullRequests) {
|
||||
const { changelog, error, warnings } = checkPullRequestBody(pr.body, pr, issues);
|
||||
if (error) {
|
||||
console.error(`Failed to extract changelog in ${pr.html_url}: ${error}`)
|
||||
problems.push(`Failed to extract changelog in ${pr.html_url}: ${error}`);
|
||||
continue;
|
||||
}
|
||||
for (const warning of warnings) {
|
||||
console.warn(`Warning in changelog for ${pr.html_url}: ${warning}`)
|
||||
problems.push(`Warning in changelog for ${pr.html_url}: ${warning}`);
|
||||
}
|
||||
for (const [section, items] of Object.entries(changelog)) {
|
||||
mergedChangelog[section] = [...mergedChangelog[section] ?? [], ...items];
|
||||
}
|
||||
}
|
||||
return mergedChangelog;
|
||||
return [mergedChangelog, problems] as const;
|
||||
}
|
||||
|
||||
async function fetchLastRelease() {
|
||||
@@ -258,7 +259,7 @@ async function fetchStatistics(lastRelease: Release, pullRequests: Issue[]): Pro
|
||||
};
|
||||
}
|
||||
|
||||
function printMarkdown(
|
||||
function formatMarkdown(
|
||||
changelog: Changelog,
|
||||
issues: Issue[],
|
||||
users: Issue["user"][],
|
||||
@@ -266,12 +267,13 @@ function printMarkdown(
|
||||
refText: (issue: Issue) => string,
|
||||
userText: (user: Issue["user"]) => string,
|
||||
) {
|
||||
const lines: string[] = [];
|
||||
const sections = new Set([...changelogSections, ...Object.keys(changelog)]);
|
||||
for (const section of sections) {
|
||||
if (section in changelog) {
|
||||
console.log(`### ${section}\n`);
|
||||
lines.push(`### ${section}`, "");
|
||||
const entries = changelog[section];
|
||||
console.log(entries.map(text => (
|
||||
lines.push(...entries.map(text => (
|
||||
`- ${text.replaceAll(/#(\d+)/g, (ref, idAsText) => {
|
||||
const id = Number.parseInt(idAsText, 10);
|
||||
if (!Number.isFinite(id)) {
|
||||
@@ -283,27 +285,29 @@ function printMarkdown(
|
||||
}
|
||||
return refText(issue);
|
||||
})}`
|
||||
)).join("\n"));
|
||||
console.log();
|
||||
)));
|
||||
lines.push("");
|
||||
}
|
||||
}
|
||||
|
||||
console.log("### Contributors\n");
|
||||
console.log(`Many thanks to the following for contributing to this release:`);
|
||||
console.log(users.map(userText).join(", "));
|
||||
lines.push("### Contributors", "");
|
||||
lines.push("Many thanks to the following for contributing to this release:");
|
||||
lines.push(users.map(userText).join(", "));
|
||||
|
||||
const format = (value: number) => value.toLocaleString("en-US");
|
||||
console.log("\n### Statistics\n");
|
||||
console.log([
|
||||
lines.push("", "### Statistics", "");
|
||||
lines.push([
|
||||
`Commits: ${format(statistics.commits)}`,
|
||||
`Pull Requests: ${format(statistics.pullRequests)}`,
|
||||
`Files Changed: ${format(statistics.filesChanged)}`,
|
||||
`Additions: +${format(statistics.lineAdditions)}`,
|
||||
`Deletions: -${format(statistics.lineDeletions)}`,
|
||||
].join(" "));
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
async function main() {
|
||||
/** Builds the release notes for the changes merged since the last release. */
|
||||
export async function generateReleaseNotes() {
|
||||
const lastRelease = await fetchLastRelease();
|
||||
const issues = await fetchIssuesUpdatedSince(lastRelease.created_at);
|
||||
|
||||
@@ -313,23 +317,37 @@ async function main() {
|
||||
&& issue.pull_request.merged_at > lastRelease.created_at
|
||||
));
|
||||
|
||||
const changelog = changelogFromPullRequests(pullRequests, issues);
|
||||
const users = [...new Map(pullRequests.map(issue => [issue.user.login, issue.user])).values()]
|
||||
const [changelog, problems] = changelogFromPullRequests(pullRequests, issues);
|
||||
const users = [...new Map(pullRequests.map(issue => [issue.user.login, issue.user])).values()];
|
||||
const statistics = await fetchStatistics(lastRelease, pullRequests);
|
||||
|
||||
return {
|
||||
/** Errors and warnings from the pull request changelogs. */
|
||||
problems,
|
||||
github: formatMarkdown(changelog, issues, users, statistics,
|
||||
issue => `${repository}#${issue.number}`,
|
||||
user => `@${user.login}`,
|
||||
),
|
||||
changelog: formatMarkdown(changelog, issues, users, statistics,
|
||||
issue => `[#${issue.number}](<${issue.html_url}>)`,
|
||||
user => `[@${user.login}](<${user.html_url}>)`
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const notes = await generateReleaseNotes();
|
||||
for (const problem of notes.problems) {
|
||||
console.warn(problem);
|
||||
}
|
||||
|
||||
console.log();
|
||||
console.log("=== Github Release Markdown ===");
|
||||
printMarkdown(changelog, issues, users, statistics,
|
||||
issue => `${repository}#${issue.number}`,
|
||||
user => `@${user.login}`,
|
||||
);
|
||||
console.log(notes.github);
|
||||
|
||||
console.log();
|
||||
console.log("=== Discord / Changelog Markdown ===");
|
||||
printMarkdown(changelog, issues, users, statistics,
|
||||
issue => `[#${issue.number}](<${issue.html_url}>)`,
|
||||
user => `[@${user.login}](<${user.html_url}>)`
|
||||
);
|
||||
console.log(notes.changelog);
|
||||
}
|
||||
|
||||
if (import.meta.main) {
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
// Bumps the package versions, adds the release notes to CHANGELOG.md, then commits and tags the release.
|
||||
// Usage: node scripts/prepare_release.mts [--force] <version>
|
||||
// --force continues when pull request changelogs have errors or warnings.
|
||||
import { execFile } from "node:child_process";
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
import { generateReleaseNotes } from "./pr_to_changelog.mts";
|
||||
|
||||
const root = path.resolve(import.meta.dirname, "..");
|
||||
const versionPattern = /^\d+\.\d+\.\d+(-[0-9A-Za-z.-]+)?$/;
|
||||
|
||||
async function git(...args: string[]) {
|
||||
return (await promisify(execFile)("git", args, { cwd: root, encoding: "utf8" })).stdout;
|
||||
}
|
||||
|
||||
export function setPackageVersion(text: string, version: string) {
|
||||
const pattern = /^([ \t]*"version":[ \t]*)"[^"]*"/m;
|
||||
if (!pattern.test(text)) {
|
||||
throw new Error("No version field found");
|
||||
}
|
||||
return text.replace(pattern, `$1"${version}"`);
|
||||
}
|
||||
|
||||
export function addChangelogEntry(text: string, version: string, notes: string) {
|
||||
if (new RegExp(`^## Version ${version.replaceAll(".", "\\.")}$`, "m").test(text)) {
|
||||
throw new Error(`CHANGELOG.md already has an entry for ${version}`);
|
||||
}
|
||||
const firstEntry = text.search(/^## /m);
|
||||
if (firstEntry === -1) {
|
||||
throw new Error("No existing entry found in CHANGELOG.md");
|
||||
}
|
||||
return `${text.slice(0, firstEntry)}## Version ${version}\n\n${notes.trim()}\n\n\n${text.slice(firstEntry)}`;
|
||||
}
|
||||
|
||||
async function packageFiles() {
|
||||
const files: string[] = [];
|
||||
for (const parent of ["packages", "plugins"]) {
|
||||
for (const entry of await fs.readdir(path.join(root, parent), { withFileTypes: true })) {
|
||||
const file = path.join(parent, entry.name, "package.json");
|
||||
if (entry.isDirectory() && await fs.stat(path.join(root, file)).catch(() => null)) {
|
||||
files.push(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const args = process.argv.slice(2);
|
||||
const force = args.includes("--force");
|
||||
const [version, ...rest] = args.filter(arg => arg !== "--force");
|
||||
if (!version || rest.length || !versionPattern.test(version)) {
|
||||
console.error("Usage: node scripts/prepare_release.mts [--force] <version>, e.g. 2.1.2");
|
||||
process.exitCode = 1;
|
||||
return;
|
||||
}
|
||||
if (await git("status", "--porcelain", "--untracked-files=no")) {
|
||||
throw new Error("Working tree has uncommitted changes");
|
||||
}
|
||||
if (await git("tag", "--list", `v${version}`)) {
|
||||
throw new Error(`Tag v${version} already exists`);
|
||||
}
|
||||
|
||||
// Done first so a GitHub API failure leaves the working tree untouched.
|
||||
const notes = await generateReleaseNotes();
|
||||
for (const problem of notes.problems) {
|
||||
console.warn(problem);
|
||||
}
|
||||
if (notes.problems.length && !force) {
|
||||
throw new Error(
|
||||
`Found ${notes.problems.length} changelog problem(s) in merged pull requests. ` +
|
||||
"Fix the pull request descriptions and run it again, or pass --force to continue anyway."
|
||||
);
|
||||
}
|
||||
const changelogPath = path.join(root, "CHANGELOG.md");
|
||||
const changelog = addChangelogEntry(await fs.readFile(changelogPath, "utf8"), version, notes.changelog);
|
||||
|
||||
const files = await packageFiles();
|
||||
for (const file of files) {
|
||||
const filePath = path.join(root, file);
|
||||
await fs.writeFile(filePath, setPackageVersion(await fs.readFile(filePath, "utf8"), version));
|
||||
}
|
||||
await fs.writeFile(changelogPath, changelog);
|
||||
|
||||
await git("add", "CHANGELOG.md", ...files);
|
||||
await git("commit", "--quiet", "-m", `Prepare ${version}`);
|
||||
await git("tag", `v${version}`);
|
||||
|
||||
console.log();
|
||||
console.log(`Committed "Prepare ${version}" and tagged it v${version}. Nothing was pushed.`);
|
||||
console.log("Review it with: git show --stat HEAD");
|
||||
console.log();
|
||||
console.log("=== Github Release Markdown ===");
|
||||
console.log(notes.github);
|
||||
}
|
||||
|
||||
if (import.meta.main) {
|
||||
main().catch(err => {
|
||||
console.error(err.message ?? err);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
}
|
||||
File renamed without changes.
+76
-15
@@ -22,8 +22,8 @@ describe("controller/src/routes", function() {
|
||||
let port;
|
||||
beforeEach(async function() {
|
||||
controller = new mock.MockController();
|
||||
// Check the progress of requests every 500 ms instead of every 30 seconds.
|
||||
routes.addRouteHandlers(controller.app, 500);
|
||||
// Check the progress of requests every 500 ms without a burst allowance.
|
||||
routes.addRouteHandlers(controller.app, 500, 0);
|
||||
server = http.createServer(routes.httpServerOptions, controller.app);
|
||||
server.listen(0, "localhost");
|
||||
await events.once(server, "listening");
|
||||
@@ -179,7 +179,7 @@ describe("controller/src/routes", function() {
|
||||
return status;
|
||||
}
|
||||
it("should close a transfer that stops making progress", async function() {
|
||||
controller.mockConfigEntries.set("controller.transfer_min_progress", 1);
|
||||
controller.mockConfigEntries.set("controller.transfer_min_speed", 2);
|
||||
const stream = await routes.createProxyStream(controller.app);
|
||||
const request = http.request(
|
||||
`${endpoint}/${stream.id}`, { method: "PUT", headers: { "Content-Length": 1024 * 1024 } }
|
||||
@@ -196,7 +196,7 @@ describe("controller/src/routes", function() {
|
||||
});
|
||||
it("should close a transfer that is trickled in", async function() {
|
||||
this.timeout(10000);
|
||||
controller.mockConfigEntries.set("controller.transfer_min_progress", 4);
|
||||
controller.mockConfigEntries.set("controller.transfer_min_speed", 8);
|
||||
const stream = await routes.createProxyStream(controller.app);
|
||||
const start = Date.now();
|
||||
const upload = putPaced(stream.id, 200, 50);
|
||||
@@ -208,7 +208,7 @@ describe("controller/src/routes", function() {
|
||||
});
|
||||
it("should not close a slow transfer that makes progress", async function() {
|
||||
this.timeout(10000);
|
||||
controller.mockConfigEntries.set("controller.transfer_min_progress", 1);
|
||||
controller.mockConfigEntries.set("controller.transfer_min_speed", 2);
|
||||
const stream = await routes.createProxyStream(controller.app);
|
||||
const upload = putPaced(stream.id, 1024, 20);
|
||||
const download = fetch(`${endpoint}/${stream.id}`).then(response => response.arrayBuffer());
|
||||
@@ -216,13 +216,21 @@ describe("controller/src/routes", function() {
|
||||
assert.equal((await download).byteLength, 20 * 1024);
|
||||
});
|
||||
it("should not close a transfer held back by a slow receiver", async function() {
|
||||
this.timeout(20000);
|
||||
controller.mockConfigEntries.set("controller.transfer_min_progress", 16);
|
||||
const size = 4 * 1024 * 1024;
|
||||
const stream = await routes.createProxyStream(controller.app);
|
||||
const upload = fetch(`${endpoint}/${stream.id}`, { method: "PUT", body: Buffer.alloc(size) });
|
||||
this.timeout(40000);
|
||||
// The receiver takes about 1.2 MB/s, which the OS hands on in bursts of over a MB.
|
||||
const slowController = new mock.MockController();
|
||||
slowController.mockConfigEntries.set("controller.transfer_min_speed", 1024);
|
||||
routes.addRouteHandlers(slowController.app, 3000);
|
||||
const slowServer = http.createServer(routes.httpServerOptions, slowController.app);
|
||||
slowServer.listen(0, "localhost");
|
||||
await events.once(slowServer, "listening");
|
||||
const slowEndpoint = `http://localhost:${slowServer.address().port}/api/stream`;
|
||||
try {
|
||||
const size = 16 * 1024 * 1024;
|
||||
const stream = await routes.createProxyStream(slowController.app);
|
||||
const upload = fetch(`${slowEndpoint}/${stream.id}`, { method: "PUT", body: Buffer.alloc(size) });
|
||||
const received = await new Promise((resolve, reject) => {
|
||||
http.get(`${endpoint}/${stream.id}`, response => {
|
||||
http.get(`${slowEndpoint}/${stream.id}`, response => {
|
||||
let bytes = 0;
|
||||
response.on("data", chunk => {
|
||||
bytes += chunk.length;
|
||||
@@ -235,11 +243,14 @@ describe("controller/src/routes", function() {
|
||||
});
|
||||
assert.equal(received, size);
|
||||
assert.equal((await upload).status, 200);
|
||||
} finally {
|
||||
slowServer.close();
|
||||
}
|
||||
});
|
||||
it("should not count the time waiting for the stream to start", async function() {
|
||||
this.timeout(10000);
|
||||
controller.mockConfigEntries.set("controller.proxy_stream_timeout", 3);
|
||||
controller.mockConfigEntries.set("controller.transfer_min_progress", 1);
|
||||
controller.mockConfigEntries.set("controller.transfer_min_speed", 2);
|
||||
const stream = await routes.createProxyStream(controller.app);
|
||||
const download = fetch(`${endpoint}/${stream.id}`);
|
||||
await lib.wait(1500);
|
||||
@@ -1257,7 +1268,7 @@ describe("controller/src/routes", function() {
|
||||
});
|
||||
it("should close an upload that is trickled in", async function() {
|
||||
this.timeout(10000);
|
||||
controller.mockConfigEntries.set("controller.transfer_min_progress", 4);
|
||||
controller.mockConfigEntries.set("controller.transfer_min_speed", 8);
|
||||
const modsDirectory = await fs.mkdtemp(path.join(os.tmpdir(), "clusterio-mods-"));
|
||||
try {
|
||||
controller.mockConfigEntries.set("controller.mods_directory", modsDirectory);
|
||||
@@ -1277,15 +1288,65 @@ describe("controller/src/routes", function() {
|
||||
});
|
||||
it("should close a request to another route that is trickled in", async function() {
|
||||
this.timeout(10000);
|
||||
controller.mockConfigEntries.set("controller.transfer_min_progress", 4);
|
||||
controller.mockConfigEntries.set("controller.transfer_min_speed", 8);
|
||||
controller.app.post("/test", express.raw({ type: "*/*", limit: "2mb" }), (req, res) => res.sendStatus(200));
|
||||
const result = await trickle(`http://localhost:${port}/test`, { "Content-Type": "text/plain" });
|
||||
assert.equal(result.status, undefined);
|
||||
assert(result.duration < 2000, `closed after ${result.duration} ms`);
|
||||
});
|
||||
describe("with a burst allowance", function() {
|
||||
let burstServer, burstUrl;
|
||||
beforeEach(async function() {
|
||||
// Each check requires 256 KiB/s * 0.5 s - 64 KiB = 64 KiB.
|
||||
const burstController = new mock.MockController();
|
||||
burstController.mockConfigEntries.set("controller.transfer_min_speed", 256);
|
||||
routes.addRouteHandlers(burstController.app, 500, 64 * 1024);
|
||||
burstController.app.post("/test", express.raw({ type: "*/*", limit: "2mb" }), (req, res) => {
|
||||
res.sendStatus(200);
|
||||
});
|
||||
burstServer = http.createServer(routes.httpServerOptions, burstController.app);
|
||||
burstServer.listen(0, "localhost");
|
||||
await events.once(burstServer, "listening");
|
||||
burstUrl = `http://localhost:${burstServer.address().port}/test`;
|
||||
});
|
||||
afterEach(function() {
|
||||
burstServer.close();
|
||||
});
|
||||
async function sendBursts(burstBytes) {
|
||||
const bursts = 12;
|
||||
const request = http.request(burstUrl, {
|
||||
method: "POST", headers: { "Content-Type": "text/plain", "Content-Length": burstBytes * bursts },
|
||||
});
|
||||
let status;
|
||||
request.on("response", response => {
|
||||
status = response.statusCode;
|
||||
response.resume();
|
||||
});
|
||||
request.on("error", () => {});
|
||||
const closed = new Promise(resolve => request.on("close", resolve));
|
||||
for (let i = 0; i < bursts && !request.closed; i++) {
|
||||
request.write(Buffer.alloc(burstBytes));
|
||||
await lib.wait(300);
|
||||
}
|
||||
if (!request.closed) {
|
||||
request.end();
|
||||
}
|
||||
await closed;
|
||||
return status;
|
||||
}
|
||||
it("should not close a request at the minimum speed arriving in bursts", async function() {
|
||||
this.timeout(10000);
|
||||
// 78 KiB every 300 ms is 260 KiB/s, a check can see only one burst.
|
||||
assert.equal(await sendBursts(78 * 1024), 200);
|
||||
});
|
||||
it("should close a request below the minimum speed arriving in bursts", async function() {
|
||||
this.timeout(10000);
|
||||
assert.equal(await sendBursts(39 * 1024), undefined);
|
||||
});
|
||||
});
|
||||
it("should not close a trickled in request when the limit is 0", async function() {
|
||||
this.timeout(10000);
|
||||
controller.mockConfigEntries.set("controller.transfer_min_progress", 0);
|
||||
controller.mockConfigEntries.set("controller.transfer_min_speed", 0);
|
||||
controller.app.post("/test", express.raw({ type: "*/*", limit: "2mb" }), (req, res) => res.sendStatus(200));
|
||||
const result = await trickle(`http://localhost:${port}/test`, { "Content-Type": "text/plain" });
|
||||
assert(result.duration >= 5000, `closed after ${result.duration} ms`);
|
||||
|
||||
+76
-1
@@ -95,7 +95,9 @@ describe("class Instance", function() {
|
||||
instanceConfig.set("instance.name", "foo");
|
||||
src = addr({ instanceId: instanceConfig.get("instance.id") });
|
||||
connector = new MockConnector(src, dst);
|
||||
instance = new Instance({}, connector, "dir", instanceConfig, new MockServer());
|
||||
instance = new Instance(
|
||||
{ _crashRetryAttempts: new Map() }, connector, "dir", instanceConfig, new MockServer()
|
||||
);
|
||||
});
|
||||
|
||||
describe(".name", function() {
|
||||
@@ -308,6 +310,79 @@ describe("class Instance", function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe("restart after crash", function() {
|
||||
let errors;
|
||||
let starts;
|
||||
beforeEach(function() {
|
||||
errors = [];
|
||||
starts = [];
|
||||
instance.logger = new MockLogger();
|
||||
instance.logger.error = message => errors.push(message);
|
||||
instance._saveStats = async () => {};
|
||||
instance._host._connectInstance = async id => {
|
||||
starts.push(id);
|
||||
return { instance: { _startSave: async () => {} } };
|
||||
};
|
||||
instance.config.set("instance.restart_after_crash", true);
|
||||
});
|
||||
function crash(uptimeMinutes) {
|
||||
instance.notifyStatus("running");
|
||||
instance._startedAtMs = Date.now() - uptimeMinutes * 60e3;
|
||||
instance.notifyExit();
|
||||
}
|
||||
|
||||
it("should restart after running for 10 minutes until the attempts run out", function() {
|
||||
for (let i = 0; i < 4; i++) {
|
||||
crash(11);
|
||||
}
|
||||
assert.deepEqual(starts, [instance.id, instance.id, instance.id]);
|
||||
assert.deepEqual(errors, ["Instance foo reached the limit of 3 crash restarts, not restarting it"]);
|
||||
});
|
||||
it("should restart without limit when attempts is 0", function() {
|
||||
instance.config.set("instance.crash_retry_attempts", 0);
|
||||
for (let i = 0; i < 5; i++) {
|
||||
crash(11);
|
||||
}
|
||||
assert.equal(starts.length, 5);
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
it("should not restart a crash within 10 minutes of starting", function() {
|
||||
crash(9);
|
||||
assert.deepEqual(starts, []);
|
||||
assert.deepEqual(errors, ["Instance foo crashed within 10 minutes of starting, not restarting it"]);
|
||||
});
|
||||
it("should not restart when disabled", function() {
|
||||
instance.config.set("instance.restart_after_crash", false);
|
||||
crash(11);
|
||||
assert.deepEqual(starts, []);
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
it("should not restart after a stop or kill", async function() {
|
||||
instance.server.kill = async () => {};
|
||||
instance.notifyStatus("running");
|
||||
instance._startedAtMs = 0;
|
||||
await instance.handleInstanceKillRequest();
|
||||
instance.notifyExit();
|
||||
instance.notifyStatus("stopping");
|
||||
instance.notifyExit();
|
||||
assert.deepEqual(starts, []);
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
it("should not restart while the host shuts down", function() {
|
||||
instance._host._shuttingDown = true;
|
||||
crash(11);
|
||||
assert.deepEqual(starts, []);
|
||||
});
|
||||
it("should reset the attempts on a manual start", async function() {
|
||||
instance.config.set("instance.crash_retry_attempts", 1);
|
||||
instance._startSave = async () => {};
|
||||
crash(11);
|
||||
await instance.handleInstanceStartRequest(new lib.InstanceStartRequest());
|
||||
crash(11);
|
||||
assert.equal(starts.length, 2);
|
||||
});
|
||||
});
|
||||
|
||||
describe(".checkModPackVersion()", function() {
|
||||
function modPack(factorioVersion) {
|
||||
return lib.ModPack.fromJSON({ name: "pack", factorio_version: factorioVersion });
|
||||
|
||||
@@ -6,7 +6,8 @@ import stream from "node:stream";
|
||||
// Capture native fetch before any potential mocks are applied
|
||||
const nativeFetch = global.fetch;
|
||||
|
||||
import { ModStore, ModInfo, ModVersionEquality, zipOutputStream } from "@clusterio/lib";
|
||||
import { ModStore, ModInfo, ModVersionEquality, zipOutputStream, hashFile } from "@clusterio/lib";
|
||||
import * as libBuildMod from "@clusterio/lib/build_mod.js";
|
||||
import { externalTest, createZip, createZipBuffer } from "../common.js";
|
||||
import "../setup_logging.js";
|
||||
|
||||
@@ -706,6 +707,38 @@ describe("lib/ModStore", function () {
|
||||
});
|
||||
|
||||
// === Instance Methods ===
|
||||
describe("readModInfo (static)", function () {
|
||||
it("should load from a mod zip file", async function () {
|
||||
await libBuildMod.build({
|
||||
build: true,
|
||||
pack: true,
|
||||
sourceDir: path.join("test", "file", "empty_mod"),
|
||||
outputDir: path.join("temp", "test"),
|
||||
});
|
||||
const filePath = path.join("temp", "test", "empty_mod_1.0.0.zip");
|
||||
const hash = await hashFile(filePath);
|
||||
const stat = await fs.stat(filePath);
|
||||
const mod = await ModStore.readModInfo(filePath);
|
||||
assert.deepEqual(
|
||||
mod,
|
||||
ModInfo.fromJSON({
|
||||
name: "empty_mod",
|
||||
version: "1.0.0",
|
||||
title: "An Empty Mod",
|
||||
author: "Me",
|
||||
description: "An empty mod for testing",
|
||||
factorio_version: "1.1",
|
||||
dependencies: [],
|
||||
filename: "empty_mod_1.0.0.zip",
|
||||
size: stat.size,
|
||||
mtime_ms: stat.mtimeMs,
|
||||
updated_at_ms: stat.mtimeMs,
|
||||
sha1: hash,
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Instance Methods", function () {
|
||||
let modStore;
|
||||
let testMod1Name = "test-mod-1";
|
||||
|
||||
@@ -15,6 +15,16 @@ describe("lib/config/definitions/migrations", function() {
|
||||
assert.equal(config.fields["controller.external_address"], undefined,
|
||||
"external_address was not removed");
|
||||
});
|
||||
it("should migrate 'transfer_min_progress' to 'transfer_min_speed'", function() { // 2.1.2
|
||||
const migrate = value => lib.ControllerConfig.fromJSON({
|
||||
"controller.transfer_min_progress": value,
|
||||
}, "controller");
|
||||
assert.equal(migrate(30).get("controller.transfer_min_speed"), 64, "old default was kept");
|
||||
assert.equal(migrate(0).get("controller.transfer_min_speed"), 0, "no limit was not kept");
|
||||
assert.equal(migrate(3000).get("controller.transfer_min_speed"), 100, "value was not converted");
|
||||
assert.equal(migrate(30).fields["controller.transfer_min_progress"], undefined,
|
||||
"transfer_min_progress was not removed");
|
||||
});
|
||||
});
|
||||
describe("Host Config", function() {
|
||||
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
import * as libBuildMod from "@clusterio/lib/build_mod.js";
|
||||
import { ModInfo, ModDependency } from "@clusterio/lib";
|
||||
|
||||
import { testMatrix, testRoundTripJsonSerialisable } from "../../common.js";
|
||||
@@ -413,35 +410,6 @@ describe("lib/data/ModInfo", function() {
|
||||
["UltraMod", "SuperLib >= 1.00", "! bad-mod"].map(dep => new ModDependency(dep))
|
||||
);
|
||||
});
|
||||
it("should load from a mod zip file", async function() {
|
||||
await libBuildMod.build({
|
||||
build: true,
|
||||
pack: true,
|
||||
sourceDir: path.join("test", "file", "empty_mod"),
|
||||
outputDir: path.join("temp", "test"),
|
||||
});
|
||||
const filePath = path.join("temp", "test", "empty_mod_1.0.0.zip");
|
||||
const hash = await lib.hashFile(filePath);
|
||||
const stat = await fs.stat(filePath);
|
||||
const mod = await ModInfo.fromModFile(filePath);
|
||||
assert.deepEqual(
|
||||
mod,
|
||||
ModInfo.fromJSON({
|
||||
name: "empty_mod",
|
||||
version: "1.0.0",
|
||||
title: "An Empty Mod",
|
||||
author: "Me",
|
||||
description: "An empty mod for testing",
|
||||
factorio_version: "1.1",
|
||||
dependencies: [],
|
||||
filename: "empty_mod_1.0.0.zip",
|
||||
size: stat.size,
|
||||
mtime_ms: stat.mtimeMs,
|
||||
updated_at_ms: stat.mtimeMs,
|
||||
sha1: hash,
|
||||
})
|
||||
);
|
||||
});
|
||||
describe("checkDependencySatisfaction()", function() {
|
||||
it("should return undefined when satisfied", function() {
|
||||
const mod = ModInfo.fromJSON({ dependencies: ["! a", "b = 1.0.0", "? c = 2.0.0"] });
|
||||
|
||||
@@ -412,6 +412,20 @@ describe("lib/file_ops", function() {
|
||||
await assert.rejects(fs.readFile(downloadPath), { code: "ENOENT" });
|
||||
});
|
||||
|
||||
it("should not leave a temporary file behind if interrupted while writing", async function () {
|
||||
const downloadPath = path.join(downloadDir, "interrupted-writing.txt");
|
||||
onStream = (res) => {
|
||||
res.write(Buffer.alloc(1024 * 1024));
|
||||
setTimeout(() => res.destroy(), 100);
|
||||
};
|
||||
await assert.rejects(lib.downloadFile(new URL("/stream", baseUrl), downloadPath, "rename"));
|
||||
await assert.rejects(
|
||||
fs.readFile(path.join(downloadDir, "interrupted-writing.tmp.txt")),
|
||||
{ code: "ENOENT" },
|
||||
);
|
||||
await assert.rejects(fs.readFile(downloadPath), { code: "ENOENT" });
|
||||
});
|
||||
|
||||
it("should throw if overwriteMode is rename and directory disappeared", async function () {
|
||||
if (process.platform === "win32") {
|
||||
// Can't remove the directory while the stream is running on Windows.
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
import { testLines } from "./factorio/lines.js";
|
||||
@@ -66,6 +67,77 @@ describe("lib/logging_utils.js", function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe("class DailyFileTransport", function() {
|
||||
let tempDir = path.join("temp", "test", "daily_file");
|
||||
beforeEach(async function() {
|
||||
await fs.rm(tempDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
function create(now, dirname = tempDir) {
|
||||
return new lib.DailyFileTransport({ dirname, prefix: "test", now });
|
||||
}
|
||||
function write(transport, message) {
|
||||
transport.log({ [Symbol.for("message")]: JSON.stringify({ message }) }, () => {});
|
||||
}
|
||||
async function close(transport) {
|
||||
await new Promise(resolve => transport.close(resolve));
|
||||
}
|
||||
async function read(file) {
|
||||
let content = await fs.readFile(path.join(tempDir, file), "utf8");
|
||||
assert(content.endsWith(os.EOL));
|
||||
return content.split(os.EOL).slice(0, -1).map(line => JSON.parse(line).message);
|
||||
}
|
||||
|
||||
it("should create the directory and write lines to the file for the UTC date", async function() {
|
||||
let transport = create(() => Date.parse("2026-11-19T12:00:00Z"), path.join(tempDir, "nested"));
|
||||
write(transport, "first");
|
||||
write(transport, "second");
|
||||
await close(transport);
|
||||
assert.deepEqual(await read(path.join("nested", "test-2026-11-19.log")), ["first", "second"]);
|
||||
});
|
||||
|
||||
it("should append to an existing file", async function() {
|
||||
await fs.mkdir(tempDir, { recursive: true });
|
||||
await fs.writeFile(path.join(tempDir, "test-2026-11-19.log"), `{"message":"old"}${os.EOL}`);
|
||||
let transport = create(() => Date.parse("2026-11-19T12:00:00Z"));
|
||||
write(transport, "new");
|
||||
await close(transport);
|
||||
assert.deepEqual(await read("test-2026-11-19.log"), ["old", "new"]);
|
||||
});
|
||||
|
||||
it("should switch files when the UTC date changes", async function() {
|
||||
let time = Date.parse("2026-11-19T23:59:59.999Z");
|
||||
let transport = create(() => time);
|
||||
write(transport, "before");
|
||||
time += 1;
|
||||
write(transport, "after");
|
||||
await close(transport);
|
||||
assert.deepEqual((await fs.readdir(tempDir)).sort(), ["test-2026-11-19.log", "test-2026-11-20.log"]);
|
||||
assert.deepEqual(await read("test-2026-11-19.log"), ["before"]);
|
||||
assert.deepEqual(await read("test-2026-11-20.log"), ["after"]);
|
||||
});
|
||||
|
||||
it("should use the UTC date rather than the local one", async function() {
|
||||
let transport = create(() => Date.parse("2026-11-20T00:30:00+02:00"));
|
||||
write(transport, "message");
|
||||
await close(transport);
|
||||
assert.deepEqual(await fs.readdir(tempDir), ["test-2026-11-19.log"]);
|
||||
});
|
||||
|
||||
it("should emit write errors", async function() {
|
||||
await fs.mkdir(path.join(tempDir, "test-2026-11-19.log"), { recursive: true });
|
||||
let transport = create(() => Date.parse("2026-11-19T12:00:00Z"));
|
||||
let error = new Promise(resolve => transport.once("error", resolve));
|
||||
write(transport, "message");
|
||||
assert.equal((await error).code, "EISDIR");
|
||||
});
|
||||
|
||||
it("should close when nothing was written", async function() {
|
||||
await close(create(Date.now));
|
||||
assert.deepEqual(await fs.readdir(tempDir), []);
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteOldLogs", function() {
|
||||
let tempDir = path.join("temp", "test", "log_retention");
|
||||
let files = [
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
/* eslint-disable node/no-sync */
|
||||
import assert from "node:assert/strict";
|
||||
import zlib from "node:zlib";
|
||||
|
||||
import * as browserZlib from "../../packages/lib/src/zlib_sync.browser.ts";
|
||||
|
||||
describe("lib/zlib_sync.browser", function() {
|
||||
const text = JSON.stringify({ name: "pack", mods: [{ name: "base", version: "2.0.0" }] });
|
||||
|
||||
it("should inflate data deflated by node:zlib", function() {
|
||||
const inflated = browserZlib.inflateSync(zlib.deflateSync(text));
|
||||
assert.equal(Buffer.from(inflated).toString(), text);
|
||||
});
|
||||
it("should deflate data node:zlib can inflate", function() {
|
||||
assert.equal(zlib.inflateSync(browserZlib.deflateSync(text)).toString(), text);
|
||||
const bytes = Buffer.from(text);
|
||||
assert.equal(zlib.inflateSync(browserZlib.deflateSync(bytes)).toString(), text);
|
||||
});
|
||||
it("should throw with a Z_ code on invalid data", function() {
|
||||
assert.throws(
|
||||
() => browserZlib.inflateSync(Buffer.from("not zlib data")),
|
||||
err => err.code === "Z_DATA_ERROR"
|
||||
);
|
||||
});
|
||||
});
|
||||
+1
-1
@@ -175,7 +175,7 @@ export class MockController {
|
||||
["controller.public_url", "test"],
|
||||
["controller.auth_secret", "TestSecretDoNotUse"],
|
||||
["controller.proxy_stream_timeout", 1],
|
||||
["controller.transfer_min_progress", 30],
|
||||
["controller.transfer_min_speed", 64],
|
||||
["controller.metrics_timeout", 1],
|
||||
["controller.host_pairing_timeout", 3600],
|
||||
["controller.host_pairing_max_pending", 50],
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
import { addChangelogEntry, setPackageVersion } from "../scripts/prepare_release.mts";
|
||||
|
||||
|
||||
describe("scripts/prepare_release", function() {
|
||||
describe("setPackageVersion()", function() {
|
||||
it("replaces only the package version", function() {
|
||||
const text = (version) => `${JSON.stringify({
|
||||
name: "@clusterio/lib",
|
||||
version,
|
||||
dependencies: { a: "2.1.1" },
|
||||
}, null, "\t")}\n`;
|
||||
assert.equal(setPackageVersion(text("2.1.1"), "2.2.0"), text("2.2.0"));
|
||||
});
|
||||
it("throws if there is no version", function() {
|
||||
assert.throws(() => setPackageVersion('{\n\t"name": "x"\n}\n', "2.2.0"), /No version field/);
|
||||
});
|
||||
});
|
||||
|
||||
describe("addChangelogEntry()", function() {
|
||||
const changelog = "# Changelog\n\n## Version 2.1.1\n\n### Fixes\n\n- Old fix.\n";
|
||||
|
||||
it("adds the entry above the previous release", function() {
|
||||
assert.equal(
|
||||
addChangelogEntry(changelog, "2.1.2", "### Fixes\n\n- New fix.\n"),
|
||||
"# Changelog\n\n## Version 2.1.2\n\n### Fixes\n\n- New fix.\n\n\n" +
|
||||
"## Version 2.1.1\n\n### Fixes\n\n- Old fix.\n",
|
||||
);
|
||||
});
|
||||
it("throws if the version already has an entry", function() {
|
||||
assert.throws(() => addChangelogEntry(changelog, "2.1.1", "- Fix."), /already has an entry/);
|
||||
});
|
||||
});
|
||||
});
|
||||
+2
-2
@@ -4,7 +4,7 @@ import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
|
||||
import { publishMods, uploadModRelease } from "../publish.js";
|
||||
import { publishMods, uploadModRelease } from "../scripts/publish.js";
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
|
||||
@@ -94,7 +94,7 @@ describe("mod publishing", function() {
|
||||
`;
|
||||
const { stdout, stderr } = await execFileAsync(process.execPath, [
|
||||
"--import", `data:text/javascript,${encodeURIComponent(mockFetch)}`,
|
||||
"publish.js", "factorio", directory, "--dry-run",
|
||||
"scripts/publish.js", "factorio", directory, "--dry-run",
|
||||
]);
|
||||
assert.equal(stderr, "");
|
||||
assert(stdout.includes("Requesting current releases of cli_publish_test"));
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { callPageActions, pageActionsFromHooks } from "../../packages/web_ui/src/util/plugin_actions.ts";
|
||||
|
||||
// BaseWebPlugin.ts uses parameter properties, which Node can't strip
|
||||
class WebHooks extends lib.AsyncHookCollection {
|
||||
pageActions = this.newHook();
|
||||
}
|
||||
|
||||
describe("web_ui/util/plugin_actions", function() {
|
||||
it("should prefix keys with the plugin name and keep plugin order", async function() {
|
||||
const hooks = new WebHooks(lib.logger);
|
||||
hooks.pageActions.attach("alpha", () => ({
|
||||
UserViewPage: ({ user }) => [{ key: "greet", label: `Hello ${user.name}` }],
|
||||
}));
|
||||
hooks.pageActions.attach("beta", () => ({
|
||||
UserViewPage: () => [{ key: "a", label: "A" }, { key: "b", label: "B", danger: true }],
|
||||
HostViewPage: () => [{ key: "c", label: "C" }],
|
||||
}));
|
||||
hooks.pageActions.attach("gamma", () => ({}));
|
||||
|
||||
const pageActions = await pageActionsFromHooks(hooks);
|
||||
assert.deepEqual(Object.keys(pageActions).sort(), ["HostViewPage", "UserViewPage"]);
|
||||
assert.deepEqual(callPageActions(pageActions.UserViewPage, { user: { name: "bob" } }), [
|
||||
{ key: "alpha.greet", label: "Hello bob" },
|
||||
{ key: "beta.a", label: "A" },
|
||||
{ key: "beta.b", label: "B", danger: true },
|
||||
]);
|
||||
assert.deepEqual(callPageActions(pageActions.InstanceViewPage, { instance: {} }), []);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user