Compare commits

..
Author SHA1 Message Date
blacksmith-sh[bot] d2121bcd7a Migrate workflows to Blacksmith 2026-11-20 12:32:37 +00:00
86 changed files with 586 additions and 1862 deletions

No files matched your search

+2 -2
View File
@@ -15,10 +15,10 @@ jobs:
check:
# Bot pull requests do not fill in the template.
if: github.event.pull_request.user.type != 'Bot'
runs-on: ubuntu-latest
runs-on: blacksmith-4vcpu-ubuntu-2604
timeout-minutes: 5
steps:
- uses: actions/checkout@v7
- uses: useblacksmith/checkout@v1
with:
sparse-checkout: scripts
- uses: actions/setup-node@v7
+2 -2
View File
@@ -15,14 +15,14 @@ on:
jobs:
changes:
runs-on: ubuntu-latest
runs-on: blacksmith-4vcpu-ubuntu-2604
timeout-minutes: 5
outputs:
build_and_test: ${{ steps.syntax.outputs.build_and_test || steps.paths.outputs.code }}
js_lint: ${{ steps.lint.outputs.js_lint }}
lua_lint: ${{ steps.lint.outputs.lua_lint }}
steps:
- uses: actions/checkout@v7
- uses: useblacksmith/checkout@v1
with:
fetch-depth: 0
- uses: dorny/paths-filter@v4
+2 -2
View File
@@ -18,9 +18,9 @@ jobs:
lint:
needs: changes
if: ${{ !cancelled() && needs.changes.outputs.lua_lint != 'false' }}
runs-on: ubuntu-latest
runs-on: blacksmith-4vcpu-ubuntu-2604
steps:
- uses: actions/checkout@v7
- uses: useblacksmith/checkout@v1
- uses: actions/setup-node@v7
with:
node-version: lts/*
+4 -4
View File
@@ -23,7 +23,7 @@ jobs:
test:
needs: changes
if: ${{ !cancelled() && needs.changes.outputs.build_and_test != 'false' }}
runs-on: ubuntu-latest
runs-on: blacksmith-4vcpu-ubuntu-2604
timeout-minutes: 15
strategy:
fail-fast: false
@@ -89,7 +89,7 @@ jobs:
version-compat:
needs: changes
if: ${{ !cancelled() && needs.changes.outputs.build_and_test != 'false' }}
runs-on: ubuntu-latest
runs-on: blacksmith-4vcpu-ubuntu-2604
timeout-minutes: 15
env:
# npm version spec for the published side of the compatibility test.
@@ -121,7 +121,7 @@ jobs:
lint:
needs: changes
if: ${{ !cancelled() && needs.changes.outputs.js_lint != 'false' }}
runs-on: ubuntu-latest
runs-on: blacksmith-4vcpu-ubuntu-2604
timeout-minutes: 5
steps:
- uses: actions/checkout@v7
@@ -134,7 +134,7 @@ jobs:
build:
needs: changes
if: ${{ !cancelled() && needs.changes.outputs.build_and_test != 'false' }}
runs-on: ubuntu-latest
runs-on: blacksmith-4vcpu-ubuntu-2604
timeout-minutes: 5
steps:
- uses: actions/checkout@v7
+5 -5
View File
@@ -12,10 +12,10 @@ permissions:
jobs:
npm:
runs-on: ubuntu-latest
runs-on: blacksmith-4vcpu-ubuntu-2604
steps:
- uses: actions/checkout@v7
- uses: useblacksmith/checkout@v1
- uses: pnpm/action-setup@v6
- uses: actions/setup-node@v7
with:
@@ -30,17 +30,17 @@ jobs:
files: pnpm-lock.yaml
factorio:
runs-on: ubuntu-latest
runs-on: blacksmith-4vcpu-ubuntu-2604
steps:
- uses: actions/checkout@v7
- uses: useblacksmith/checkout@v1
- uses: pnpm/action-setup@v6
- uses: actions/setup-node@v7
with:
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 ./scripts/publish.js factorio ./dist
- run: node ./publish.js factorio ./dist
env:
FACTORIO_TOKEN: ${{ secrets.FACTORIO_TOKEN }}
- uses: softprops/action-gh-release@v3
+1 -1
View File
@@ -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 scripts/clean.js --dry-run`; see the [cleanup scopes](docs/contributing.md#cleaning-generated-and-runtime-files).
- Preview cleanup with `node 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).
+23 -28
View File
@@ -1,46 +1,41 @@
# Changelog
## Version 2.1.1
<!-- 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
- Hosts on ARM64 Linux download and run the ARM64 headless build of Factorio, available from Factorio 2.1.18. [#1228](<https://github.com/clusterio/clusterio/pull/1228>)
- Added `requiredMods` to plugin declarations. Instances refuse to start when their mod pack lacks a mod a loaded plugin requires, and the controller downloads missing required mods and their dependencies from the mod portal on start. Subspace Storage requires the subspace_storage mod. [#535](<https://github.com/clusterio/clusterio/issues/535>)
- Added `--output json` and `--output wire` to clusterioctl for machine readable results and errors. [#1230](<https://github.com/clusterio/clusterio/pull/1230>)
### Fixes
- Fixed a newly paired host reporting that it needs a restart. [#1204](<https://github.com/clusterio/clusterio/issues/1204>)
- Fixed the installer's run-controller and run-host scripts exiting with 0 when the controller or host exited with 8 to stop the restart loop. [#1208](<https://github.com/clusterio/clusterio/issues/1208>)
- Fixed clusterioctl user list, user show and role list printing [object Set] for roles, instances and permissions. [#1205](<https://github.com/clusterio/clusterio/issues/1205>)
- Fixed typo in the delete instance confirmation. [#1209](<https://github.com/clusterio/clusterio/issues/1209>)
- The Search Mod Portal dialog now opens on the default mod pack's Factorio version instead of always 2.0. [#1209](<https://github.com/clusterio/clusterio/issues/1209>)
- Fixed the mod portal cache key logging "undefined" when hide_deprecated was not sent. [#1209](<https://github.com/clusterio/clusterio/issues/1209>)
- Fixed player_sync losing quick bar slots of blueprint library records when a player joins an instance for the first time. [#1219](<https://github.com/clusterio/clusterio/issues/1219>)
- Fixed cramped plugin rows and wrapping header branding on mobile screens. [#1223](<https://github.com/clusterio/clusterio/pull/1223>)
- Fixed player_sync keeping an instance's old copy of a player after the controller refused its upload, which let an instance restarted after a crash overwrite newer player data. [#1218](<https://github.com/clusterio/clusterio/issues/1218>)
- New features added.
### Changes
- Restarting the controller or a host from the web interface now asks for confirmation, and for hosts lists the running instances that will be stopped. [#1207](<https://github.com/clusterio/clusterio/issues/1207>)
- New clusters default controller.default_mod_pack_id to the newest Base Game mod pack, so instances can create saves and start without setting it first. [#1203](<https://github.com/clusterio/clusterio/issues/1203>)
- Output from npm during `plugin install` now goes through the logger. [#1224](<https://github.com/clusterio/clusterio/pull/1224>)
- The controller collects system info, including restart required, right after it starts and when a host connects instead of waiting for the first system metrics interval. [#1227](<https://github.com/clusterio/clusterio/pull/1227>)
- clusterioctl prints all tables with the same formatter and prints command results to stdout instead of the log. [#1230](<https://github.com/clusterio/clusterio/pull/1230>)
- clusterioctl prints "Command ran successfully" for commands that have no other output. [#1230](<https://github.com/clusterio/clusterio/pull/1230>)
- Small changes to existing behaviour.
### Fixes
- Bugfixes
### Breaking Changes
- Changes which break backwards compatibility with older hosts or plugins code.
### Meta
- Added a CI check that pull request descriptions have a valid changelog section. [#1226](<https://github.com/clusterio/clusterio/pull/1226>)
### Contributors
- 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:
[@bbassie](<https://github.com/bbassie>), [@ILLISIS](<https://github.com/ILLISIS>), [@Danielv123](<https://github.com/Danielv123>)
[@username](https://github.com/username)
-->
### Statistics
## Unreleased
Commits: 44 Pull Requests: 19 Files Changed: 81 Additions: +2,676 Deletions: -739
### Fixes
- Fixed cramped plugin rows and wrapping header branding on mobile screens.
## Version 2.1.0
+1 -1
View File
@@ -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"));
+4 -7
View File
@@ -241,20 +241,17 @@ Must be greater than 0.
Defaults to 15.
### controller.transfer_min_speed
### controller.transfer_min_progress
Speed in KiB/s at which a request to the controller is never closed for moving too slowly.
Amount in KiB a request to the controller has to move every 30 seconds, requests moving less are closed.
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.
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.
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.
Set to 0 for no limit.
Must be 0 or greater.
Defaults to 64.
Defaults to 30.
### controller.factorio_username
+4 -4
View File
@@ -229,13 +229,13 @@ For documentation changes, check that links, paths, and example commands are cor
### Cleaning generated and runtime files
Inspect `node scripts/clean.js --help` and preview the selected scope with `--dry-run` before cleaning.
Inspect `node clean.js --help` and preview the selected scope with `--dry-run` before cleaning.
| Command | Removal scope |
| --- | --- |
| `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`. |
| `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`. |
`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
View File
@@ -8,24 +8,6 @@ Before `clusterioctl` can be used it needs to be configured for the cluster it w
The easiest way to do this is to run `npx clusteriocontroller bootstrap create-ctl-config <username>` on the controller, which creates the necessary `config-ctl.json` for managing the cluster as the given user.
## Output Format
ctl> --output json <command>
Print the result of the command as JSON on stdout instead of text meant for reading.
Lists are printed as an array of objects, other results as an object, and commands that have no result print `{"success": true}`.
If the command fails it prints `{"error": "<ClassName>", "message": "<message>"}` and exits with a non-zero exit code.
Log messages are written to stderr in all formats.
`--output wire` prints the same results in the format they are sent between the controller, hosts and clusterioctl, which uses snake_case names like `is_admin`.
The `json` format uses the property names shown in the tables of the human format.
`log follow` and `debug dump-ws` keep running and print one JSON object per line as entries arrive.
`instance start-all` and `instance stop-all` print an array with an entry per instance, which has an `error` property if that instance failed to start or stop.
The default is `--output human`, which prints `Command ran successfully` for commands that have no result.
## Hosts
### List hosts
+22 -18
View File
@@ -5,8 +5,7 @@
- CHANGELOG.md in the repository root
- The changelog.txt file 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.
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.
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.
@@ -16,10 +15,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, 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.
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.
Publishing the draft as a stable release triggers the workflow; pushing a tag alone does not.
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.
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.
The steps below describe publishing npm packages manually instead.
> [!NOTE]
@@ -34,31 +33,36 @@ The steps below describe publishing npm packages manually instead.
git status
```
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.
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.
```sh
node scripts/prepare_release.mts 2.0.2
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
```
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`.
3. Review and commit the version and changelog changes, then open and merge a release pull request.
```sh
git show HEAD
git push upstream master v2.0.2
git add -p
git commit -m "Prepare 2.0.2 release"
```
4. For manual publication, perform a clean install and publish the packages. For automated publication, publish the GitHub release as described above instead.
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.
The repository does not track `pnpm-lock.yaml`, so allow pnpm to generate or update it during installation.
```sh
node scripts/clean.js --dry-run
node clean.js --dry-run
pnpm clean
pnpm install --no-frozen-lockfile
pnpm -r --filter='./packages/*' --filter='./plugins/*' --fail-if-no-match publish
-14
View File
@@ -995,20 +995,6 @@ For a command the `definition` is the arguments to pass to [yargs.command](http:
The `handler` is an async function that's invoked when the command is executed and it's passed the parsed command line arguments and a reference to the `Ctl` class of clusterioctl.
Note that messages sent from clusterioctl needs to have `"control"` as a part of `src` for it to be accepted by the controller, see [Defining Link Messages](#defining-link-messages) for how to define the messages that can be sent to the controller.
Commands print their results through `ctl.output` so that `--output json` works for them:
```js
handler: async function(args, ctl) {
const result = await ctl.sendTo("controller", new messages.FrobnicateRequest(args.type));
ctl.output.result(result, () => console.log(`Frobnicated ${result.count} foos`));
},
```
`ctl.output.result(data, human)` prints `data` as JSON in json and wire mode and calls `human` otherwise.
`ctl.output.table(rows, map)` prints an array of objects as a JSON array, or as a table in human mode with `map` applied to each row first.
`ctl.output.human` is true when the output is meant for reading.
Commands that print nothing get `Command ran successfully` or `{"success": true}` printed for them, and errors thrown from the handler are printed as `{"error": "<ClassName>", "message": "<message>"}` in json and wire mode.
To have the command tree become part of clusterioctl it needs to be added to the rootCommand tree in the `addCommands` hook of the ctl entrypoint:
```js
+1 -1
View File
@@ -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 ./scripts/clean.js",
"clean": "node ./clean.js",
"bundle-dependencies": "bundle-dependencies"
},
"c8": {
+7 -4
View File
@@ -21,6 +21,7 @@ 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
@@ -354,17 +355,19 @@ async function initialize(): Promise<InitializeParameters> {
level: "verbose",
levels,
});
clusterLogger.add(new lib.DailyFileTransport({
clusterLogger.add(new DailyRotateFile({
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 lib.DailyFileTransport({
logger.add(new DailyRotateFile({
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,
+2 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@clusterio/controller",
"version": "2.1.1",
"version": "2.1.0",
"description": "Implementation of Clusterio controller",
"homepage": "https://github.com/clusterio/clusterio#readme",
"author": "Hornwitser <github@hornwitser.no>",
@@ -45,6 +45,7 @@
"jsonwebtoken": "catalog:",
"semver": "catalog:",
"winston": "catalog:",
"winston-daily-rotate-file": "catalog:",
"ws": "catalog:",
"yargs": "catalog:"
},
+23 -35
View File
@@ -419,7 +419,7 @@ export interface ProxyStream {
timeout: ReturnType<typeof setTimeout>;
closed: boolean;
/** Bytes the receiver has taken, set once the stream is flowing */
bytesTaken?: () => number;
progress?: () => number;
}
export async function createProxyStream(app: Application): Promise<ProxyStream> {
@@ -473,23 +473,18 @@ 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 too little during a
* check interval for controller.transfer_min_speed, so that a
* The connection is also closed if the request moved less than
* controller.transfer_min_progress during a check interval, 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, burstAllowanceBytes: number) {
function guardRequest(checkIntervalMs: number) {
return function(req: Request, res: Response, next: any) {
const socket = req.socket;
res.once("finish", () => {
@@ -506,36 +501,33 @@ function guardRequest(checkIntervalMs: number, burstAllowanceBytes: number) {
});
});
const bytesReceived = () => socket.bytesRead;
let measuredBytes: (() => number) | undefined = bytesReceived;
let lastBytes = bytesReceived();
const received = () => socket.bytesRead;
let measure: (() => number) | undefined = received;
let last = received();
const check = setInterval(() => {
const stream: ProxyStream | undefined = res.locals.proxyStream;
let currentBytes: (() => number) | undefined;
let current: (() => number) | undefined;
if (stream && !stream.closed) {
currentBytes = stream.bytesTaken;
current = stream.progress;
} else if (!req.complete) {
currentBytes = bytesReceived;
current = received;
}
const minSpeedKiBps = req.app.locals.controller.config.get("controller.transfer_min_speed");
if (currentBytes !== measuredBytes || !currentBytes || !minSpeedKiBps) {
const minProgress = req.app.locals.controller.config.get("controller.transfer_min_progress");
if (current !== measure || !current || !minProgress) {
// What to measure changed, start over from here.
measuredBytes = currentBytes;
lastBytes = currentBytes ? currentBytes() : 0;
measure = current;
last = current ? current() : 0;
return;
}
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) {
const progress = current();
if (progress - last < minProgress * 1024) {
logger.warn(
`Closing ${req.method} ${req.path}, it moved ${bytes - lastBytes} bytes in ${intervalS} seconds, ` +
`less than the ${requiredBytes} required for ${minSpeedKiBps} KiB/s`
`Closing ${req.method} ${req.path}, it moved less than ${minProgress} KiB ` +
`in ${checkIntervalMs / 1000} seconds`
);
socket.destroy();
}
lastBytes = bytes;
last = progress;
}, checkIntervalMs);
res.once("close", () => clearInterval(check));
next();
@@ -575,7 +567,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.bytesTaken = () => socket.bytesWritten - socket.writableLength;
stream.progress = () => 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) {
@@ -836,7 +828,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.ModStore.readModInfo(tempFilePath);
const modInfo = await lib.ModInfo.fromModFile(tempFilePath);
checkModName(modInfo.filename);
await fs.rename(tempFilePath, path.join(modsDirectory, modInfo.filename));
req.app.locals.controller.modStore.addMod(modInfo);
@@ -864,12 +856,8 @@ async function uploadMod(req: Request, res: Response) {
}
export function addRouteHandlers(
app: Application,
progressCheckIntervalMs = 60_000,
burstAllowanceBytes = 2 * 1024 * 1024,
) {
app.use(guardRequest(progressCheckIntervalMs, burstAllowanceBytes));
export function addRouteHandlers(app: Application, progressCheckIntervalMs = 30_000) {
app.use(guardRequest(progressCheckIntervalMs));
app.get("/metrics",
validateUserToken,
(req:Request, res:Response, next:any) => getMetrics(req, res).catch(next)
+5 -2
View File
@@ -1,5 +1,5 @@
/* eslint-disable no-console */
import { styleText } from "node:util";
import chalk from "chalk";
export const levels = Object.freeze({
fatal: 0,
@@ -27,7 +27,10 @@ for (let [name, level] of Object.entries(levels)) {
if (level > maxLogLevel) {
return;
}
const style = text => styleText(colors[name].split(" "), text);
let style = chalk;
for (let part of colors[name].split(" ")) {
style = style[part];
}
if (["fatal", "error"].includes(name)) {
console.error(`${style(name)} ${message}`);
} else if (["warn"].includes(name)) {
+2 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@clusterio/create",
"version": "2.1.1",
"version": "2.1.0",
"description": "Installer for clusterio",
"homepage": "https://github.com/clusterio/clusterio#readme",
"author": "Hornwitser <github@hornwitser.no>",
@@ -36,6 +36,7 @@
"@inquirer/confirm": "^6.3.5",
"@inquirer/input": "^5.1.9",
"@inquirer/select": "^5.2.8",
"chalk": "catalog:",
"yargs": "catalog:"
}
}
-5
View File
@@ -23,11 +23,6 @@ Common options:
* `--create-config` If the config file is missing, create a new one and carry on instead of exiting with an error.
Alternative to the `config create` command.
* `--output <human|json|wire>` Format to print the results of cluster commands in.
Defaults to `human`.
See [Output format](/docs/managing-a-cluster.md#output-format) for what `json` and `wire` print.
The `plugin` and `config` commands described below always print human readable text.
### `plugin <command>`
+15 -55
View File
@@ -17,7 +17,6 @@ import { ConsoleTransport, levels, logger } from "@clusterio/lib";
import * as commands from "./src/commands.js";
import { BaseCtlPlugin, CtlHooks } from "./src/BaseCtlPlugin.js";
import { CommandOutput, errorToJson, outputFormats, print, type OutputFormat } from "./src/command_ops.js";
/**
@@ -56,8 +55,6 @@ export class Ctl extends lib.Link {
config: lib.CtlConfig;
/** Keep the control connection alive after the command completes. */
keepOpen = false;
/** Output of the command being run. */
output = new CommandOutput();
constructor(
connector: CtlConnector,
@@ -85,20 +82,12 @@ export class Ctl extends lib.Link {
}
async handleLogMessageEvent(event: lib.LogMessageEvent) {
// Only log following commands print log events as results, the rest log them to stderr
if (this.output.streamingLog) {
this.output.result(event.info, () => logger.log(event.info as any));
} else {
logger.log(event.info as any);
}
}
async handleDebugWsMessageEvent(event: lib.DebugWsMessageEvent) {
this.output.result(
{ direction: event.direction, content: event.content },
// eslint-disable-next-line no-console
() => console.log("WS", event.direction, event.content),
);
console.log("WS", event.direction, event.content);
}
async shutdown() {
@@ -137,7 +126,6 @@ interface CtlArguments {
$0: string,
_: (string | number)[],
logLevel: keyof typeof levels,
output: OutputFormat,
config: string,
pluginList: string,
bypassLockFile: boolean,
@@ -177,13 +165,6 @@ export async function initialize(
choices: Object.keys(levels),
type: "string",
})
.option("output", {
nargs: 1,
describe: "Format to print command results in",
default: "human",
choices: outputFormats,
type: "string",
})
.option("config", {
nargs: 1,
describe: "config file to get credentails from",
@@ -279,20 +260,6 @@ export function selectTargetCommand(args: CtlArguments, rootCommands: lib.Comman
return targetCommand;
}
/**
* Run the command selected by args, printing results in the format given by args.output
*/
export async function runCommand(
args: CtlArguments,
rootCommands: lib.CommandTree,
link: lib.Link & { output?: CommandOutput },
) {
link.output = new CommandOutput(args.output);
const targetCommand = selectTargetCommand(args, rootCommands);
await targetCommand.run(args, link);
link.output.finish();
}
async function startCtl() {
const {
args,
@@ -304,19 +271,10 @@ async function startCtl() {
return;
}
try {
await connectAndRun(args, rootCommands, ctlConfig);
} catch (err) {
if (args.output !== "human") {
print(JSON.stringify(errorToJson(err)));
}
throw err;
}
}
async function connectAndRun(args: CtlArguments, rootCommands: lib.CommandTree, ctlConfig: lib.CtlConfig) {
if (!ctlConfig.get("ctl.controller_url") || !ctlConfig.get("ctl.controller_token")) {
throw new lib.StartupError("Missing URL and/or token to connect with. See README.md for setting up access.");
logger.error("Missing URL and/or token to connect with. See README.md for setting up access.");
process.exitCode = 1;
return;
}
let controlConnector = new CtlConnector(
@@ -340,25 +298,26 @@ async function connectAndRun(args: CtlArguments, rootCommands: lib.CommandTree,
process.on("SIGTERM", lib.createShutdownGuard(logger, "termination", ctl.shutdown.bind(ctl)));
try {
await runCommand(args, rootCommands, ctl);
const targetCommand = selectTargetCommand(args, rootCommands);
await targetCommand.run(args, ctl);
} catch (err) {
ctl.keepOpen = false;
let message: string;
if (err instanceof lib.CommandError) {
message = `Error running command: ${err.message}`;
logger.error(`Error running command: ${err.message}`);
process.exitCode = 1;
} else if (err instanceof lib.ClientError || err instanceof lib.UnavailableError) {
message = `Error sending request: ${err.message}`;
logger.error(`Error sending request: ${err.message}`);
process.exitCode = 1;
} else if (err instanceof lib.ServerError) {
message = `Error sending request:\n${err.stack ?? err.message}`;
logger.error(`Error sending request:\n${err.stack ?? err.message}`);
process.exitCode = 1;
} else {
throw err;
}
ctl.output.error(err, () => logger.error(message));
process.exitCode = 1;
} finally {
if (!ctl.keepOpen) {
@@ -369,7 +328,6 @@ async function connectAndRun(args: CtlArguments, rootCommands: lib.CommandTree,
export function bootstrap() {
startCtl().catch(err => {
process.exitCode = 1;
if (err.errors) {
logger.fatal(JSON.stringify(err.errors, null, "\t"));
}
@@ -382,13 +340,15 @@ export function bootstrap() {
${err.stack}`
);
} else {
logger.fatal(`
logger.error(`
+---------------------------------+
| Unable to to start clusterioctl |
+---------------------------------+
${err.stack}`
);
}
process.exitCode = 1;
});
}
-1
View File
@@ -1,6 +1,5 @@
import { bootstrap } from "./ctl.js";
export * from "./ctl.js";
export { CommandOutput, type OutputFormat } from "./src/command_ops.js";
export { BaseCtlPlugin, CtlHooks, type CtlPluginContext } from "./src/BaseCtlPlugin.js";
if (import.meta.main) {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@clusterio/ctl",
"version": "2.1.1",
"version": "2.1.0",
"description": "Clusterio command line management tool",
"homepage": "https://github.com/clusterio/clusterio#readme",
"author": "Hornwitser <github@hornwitser.no>",
-117
View File
@@ -13,120 +13,3 @@ function formatCell(value: unknown) {
}
export const asTable = asTableModule.configure({ delimiter: " | ", print: formatCell });
/** Copy of object with only the given fields, or all of them if fields includes "all" */
export function pickFields(object: object, fields: string[]) {
if (fields.includes("all")) {
return object;
}
return Object.fromEntries(Object.entries(object).filter(([field]) => fields.includes(field)));
}
/** Convert class instances, Sets and Maps into plain JSON values, ignoring wire format toJSON methods */
export function toPlain(value: unknown): unknown {
if (value instanceof Set || value instanceof Array) {
return [...value].map(toPlain);
}
if (value instanceof Map) {
return Object.fromEntries([...value].map(([key, entry]) => [key, toPlain(entry)]));
}
if (value instanceof Date) {
return value.toISOString();
}
if (typeof value === "object" && value !== null) {
return Object.fromEntries(Object.entries(value).map(([key, entry]) => [key, toPlain(entry)]));
}
return value;
}
/** Describe an error as `{ error: ClassName, message: string }` */
export function errorToJson(err: unknown) {
if (err instanceof Error) {
return { error: err.constructor.name, message: err.message };
}
return { error: "Error", message: String(err) };
}
/** JSON.stringify replacer for Sets and Maps that have no toJSON method */
function wireReplacer(key: string, value: unknown) {
if (value instanceof Set) {
return [...value];
}
if (value instanceof Map) {
return Object.fromEntries(value);
}
return value;
}
export const outputFormats = ["human", "json", "wire"] as const;
export type OutputFormat = typeof outputFormats[number];
/**
* Prints the results of a ctl command in the format chosen with --output
*
* In json and wire mode each call to result prints one line of JSON to stdout. json uses the
* property names of the objects, wire uses their toJSON methods to give the format sent over the link.
*/
export class CommandOutput {
/** True once a result has been printed */
written = false;
/** Command prints log events as results while it runs and has no final result */
streamingLog = false;
constructor(
public format: OutputFormat = "human",
) { }
get human() {
return this.format === "human";
}
/**
* Print the result of a command
*
* @param data - Printed as JSON in json and wire mode.
* @param human - Prints the result in human mode, defaults to printing data as is.
*/
result(data: unknown, human: () => void = () => print(data)) {
this.written = true;
if (this.format === "json") {
print(JSON.stringify(toPlain(data)));
} else if (this.format === "wire") {
print(JSON.stringify(data, wireReplacer));
} else {
human();
}
}
/**
* Print a list of objects as a table, or as an array in json and wire mode
*
* @param rows - Printed as is in json and wire mode.
* @param map - Converts each row into the table row printed in human mode.
*/
table<T extends object>(rows: T[], map: (row: T) => object = row => row) {
this.result(rows, () => print(asTable(rows.map(map))));
}
/**
* Print an error as `{ "error": ClassName, "message": string }` in json and wire mode
*
* @param err - Error to print.
* @param human - Reports the error in human mode.
*/
error(err: unknown, human: () => void) {
this.written = true;
if (this.human) {
human();
} else {
print(JSON.stringify(errorToJson(err)));
}
}
/** Print a success result for commands that finished without printing one */
finish() {
if (!this.written && !this.streamingLog) {
this.result({ success: true }, () => print("Command ran successfully"));
}
}
}
+26 -25
View File
@@ -1,15 +1,15 @@
import fs from "node:fs/promises";
import asTableModule from "as-table";
import events from "events";
import os from "os";
import child_process from "child_process";
import * as lib from "@clusterio/lib";
import { logger } from "@clusterio/lib";
import type { Ctl } from "../ctl.js";
import { asTable, print } from "./command_ops.js";
import {
serializedConfigToString, getEditor, configToKeyVal, printConfig, printRestartRequired,
} from "./config_ops.js";
import { print } from "./command_ops.js";
import { serializedConfigToString, getEditor, configToKeyVal } from "./config_ops.js";
const asTable = asTableModule.configure({ delimiter: " | " });
export const controllerCommands = new lib.CommandTree({ name: "controller", description: "Controller management" });
controllerCommands.add(new lib.Command({
@@ -44,8 +44,9 @@ controllerCommands.add(new lib.Command({
}
if (args.restart) {
await ctl.send(new lib.ControllerRestartRequest());
} else {
print("Controller updated; a restart is required to apply the changes.");
}
printRestartRequired(ctl, !args.restart, "Controller updated");
},
}));
@@ -56,7 +57,11 @@ const controllerConfigCommands = new lib.CommandTree({
controllerConfigCommands.add(new lib.Command({
definition: ["list", "List controller configuration"],
handler: async function(args: object, ctl: Ctl) {
printConfig(ctl, await ctl.send(new lib.ControllerConfigGetRequest()));
let config = await ctl.send(new lib.ControllerConfigGetRequest());
for (let [name, value] of Object.entries(config)) {
print(`${name} ${JSON.stringify(value)}`);
}
},
}));
@@ -141,22 +146,19 @@ controllerConfigCommands.add(new lib.Command({
index,
final[index],
));
} catch (err: any) {
} catch (err) {
// eslint-disable-next-line
print(`Attempt to set ${index} to ${final[index] || String(null)} failed; set back to previous value.`);
print(err);
// If the string is empty, it's better to just print "" instead of nothing
logger.error(
`Attempt to set ${index} to ${final[index] || String(null)} failed; ` +
`set back to previous value: ${err.message}`
);
}
}
}
try {
await fs.unlink(tmpFile);
} catch (err) {
logger.warn(
`Temporary file ${tmpFile} could not be deleted. ` +
"This is not fatal, but they may build up over time if the issue persists."
);
print("err: temporary file", tmpFile, "could not be deleted.");
print("This is not fatal, but they may build up over time if the issue persists.");
}
},
}));
@@ -173,7 +175,7 @@ controllerPluginCommands.add(new lib.Command({
let url = new URL(ctl.config.get("ctl.controller_url")!);
url.pathname += "api/plugins";
let response = await fetch(url);
ctl.output.table(await response.json() as object[]);
print(asTable(await response.json() as []));
},
}));
controllerPluginCommands.add(new lib.Command({
@@ -185,8 +187,9 @@ controllerPluginCommands.add(new lib.Command({
await ctl.sendTo("controller", new lib.PluginUpdateRequest(args.plugin));
if (args.restart) {
await ctl.send(new lib.ControllerRestartRequest());
} else {
print("Plugin updated; a restart is required to apply the changes.");
}
printRestartRequired(ctl, !args.restart, "Plugin updated");
},
}));
controllerPluginCommands.add(new lib.Command({
@@ -199,16 +202,13 @@ controllerPluginCommands.add(new lib.Command({
const response = await ctl.sendTo(
"controller", new lib.PluginSearchRequest(args.query, args.page, args.pageSize)
);
const results = response.results.map(plugin => ({
print(asTable(response.results.map(plugin => ({
name: plugin.name,
version: plugin.version,
publisher: plugin.publisher ?? "",
description: plugin.description ?? "",
}));
ctl.output.result({ total: response.total, results }, () => {
print(asTable(results));
print(`Showing ${results.length} of ${response.total} results`);
});
}))));
print(`Showing ${response.results.length} of ${response.total} results`);
},
}));
controllerPluginCommands.add(new lib.Command({
@@ -220,8 +220,9 @@ controllerPluginCommands.add(new lib.Command({
await ctl.sendTo("controller", new lib.PluginInstallRequest(args.plugin));
if (args.restart) {
await ctl.send(new lib.ControllerRestartRequest());
} else {
print("Plugin installed; a restart is required to apply the changes.");
}
printRestartRequired(ctl, !args.restart, "Plugin installed");
},
}));
+7 -3
View File
@@ -1,6 +1,5 @@
import * as lib from "@clusterio/lib";
import type { Ctl } from "../ctl.js";
import { print } from "./command_ops.js";
export const debugCommands = new lib.CommandTree({ name: "debug", description: "Debugging utilities" });
debugCommands.add(new lib.Command({
@@ -8,7 +7,6 @@ debugCommands.add(new lib.Command({
handler: async function(args: object, ctl: Ctl) {
await ctl.send(new lib.DebugDumpWsRequest());
ctl.keepOpen = true;
ctl.output.streamingLog = true;
},
}));
@@ -22,6 +20,12 @@ debugCommands.add(new lib.Command({
}],
handler: async function(args: { exchangeString: string, pretty: boolean }, ctl: Ctl) {
const result = lib.readMapExchangeString(args.exchangeString);
ctl.output.result(result, () => print(JSON.stringify(result, null, args.pretty ? 2 : undefined)));
if (args.pretty) {
// eslint-disable-next-line no-console
console.log(JSON.stringify(result, null, 2));
} else {
// eslint-disable-next-line no-console
console.log(JSON.stringify(result));
}
},
}));
+27 -27
View File
@@ -1,15 +1,15 @@
import fs from "node:fs/promises";
import asTableModule from "as-table";
import events from "events";
import os from "os";
import child_process from "child_process";
import * as lib from "@clusterio/lib";
import { logger } from "@clusterio/lib";
import type { Ctl } from "../ctl.js";
import { asTable, print } from "./command_ops.js";
import {
serializedConfigToString, getEditor, configToKeyVal, printConfig, printRestartRequired,
} from "./config_ops.js";
import { print } from "./command_ops.js";
import { serializedConfigToString, getEditor, configToKeyVal } from "./config_ops.js";
const asTable = asTableModule.configure({ delimiter: " | " });
export const hostCommands = new lib.CommandTree({ name: "host", description: "Host management" });
hostCommands.add(new lib.Command({
@@ -45,7 +45,8 @@ hostCommands.add(new lib.Command({
hostCommands.add(new lib.Command({
definition: [["list", "l"], "List hosts connected to the controller"],
handler: async function(args: object, ctl: Ctl) {
ctl.output.table(await ctl.send(new lib.HostListRequest()));
let hosts = await ctl.send(new lib.HostListRequest());
print(asTable(hosts));
},
}));
@@ -70,7 +71,6 @@ hostCommands.add(new lib.Command({
definition: ["pending", "List host installers waiting for approval"],
handler: async function(args: object, ctl: Ctl) {
let requests = await ctl.send(new lib.HostPairingListRequest());
ctl.output.result(requests, () => {
if (!requests.length) {
print("No pending host requests");
return;
@@ -82,7 +82,6 @@ hostCommands.add(new lib.Command({
address: request.remoteAddress,
age: formatAge(now - request.createdAtMs),
}))));
});
},
}));
@@ -100,10 +99,7 @@ hostCommands.add(new lib.Command({
name = requests.find(request => plainCode(request.code) === plainCode(args.code))?.name;
}
let hostId = await ctl.send(new lib.HostPairingApproveRequest(args.code, args.name));
ctl.output.result(
{ id: hostId, name },
() => print(name ? `Approved host ${name} with id ${hostId}` : `Approved host with id ${hostId}`),
);
print(name ? `Approved host ${name} with id ${hostId}` : `Approved host with id ${hostId}`);
},
}));
@@ -141,8 +137,9 @@ hostCommands.add(new lib.Command({
}
if (args.restart) {
await ctl.sendTo({ hostId }, new lib.HostRestartRequest());
} else {
print("Host updated; a restart is required to apply the changes.");
}
printRestartRequired(ctl, !args.restart, "Host updated");
},
}));
@@ -156,7 +153,11 @@ hostConfigCommands.add(new lib.Command({
}],
handler: async function(args: { host: string }, ctl: Ctl) {
let hostId = await lib.resolveHost(ctl, args.host);
printConfig(ctl, await ctl.sendTo({ hostId }, new lib.HostConfigGetRequest()));
let config = await ctl.sendTo({ hostId }, new lib.HostConfigGetRequest());
for (let [name, value] of Object.entries(config)) {
print(`${name} ${JSON.stringify(value)}`);
}
},
}));
@@ -254,22 +255,19 @@ hostConfigCommands.add(new lib.Command({
index,
final[index],
));
} catch (err: any) {
} catch (err) {
// eslint-disable-next-line
print(`Attempt to set ${index} to ${final[index] || String(null)} failed; set back to previous value.`);
// If the string is empty, it's better to just print "" instead of nothing
logger.error(
`Attempt to set ${index} to ${final[index] || String(null)} failed; ` +
`set back to previous value: ${err.message}`
);
print(err);
}
}
}
try {
await fs.unlink(tmpFile);
} catch (err) {
logger.warn(
`Temporary file ${tmpFile} could not be deleted. ` +
"This is not fatal, but they may build up over time if the issue persists."
);
print("err: temporary file", tmpFile, "could not be deleted.");
print("This is not fatal, but they may build up over time if the issue persists.");
}
},
}));
@@ -287,9 +285,9 @@ hostPluginCommands.add(new lib.Command({
handler: async function(args: { host: string }, ctl: Ctl) {
let hostId = await lib.resolveHost(ctl, args.host);
const plugins = await ctl.sendTo({ hostId }, new lib.PluginListRequest());
ctl.output.table(plugins, p => ({
print(asTable(plugins.map(p => ({
title: p.title, version: p.version, state: p.state, npmPackage: p.npmPackage,
}));
}))));
},
}));
hostPluginCommands.add(new lib.Command({
@@ -303,8 +301,9 @@ hostPluginCommands.add(new lib.Command({
await ctl.sendTo({ hostId }, new lib.PluginUpdateRequest(args.plugin));
if (args.restart) {
await ctl.sendTo({ hostId }, new lib.HostRestartRequest());
} else {
print("Plugin updated; a restart is required to apply the changes.");
}
printRestartRequired(ctl, !args.restart, "Plugin updated");
},
}));
hostPluginCommands.add(new lib.Command({
@@ -318,8 +317,9 @@ hostPluginCommands.add(new lib.Command({
await ctl.sendTo({ hostId }, new lib.PluginInstallRequest(args.plugin));
if (args.restart) {
await ctl.sendTo({ hostId }, new lib.HostRestartRequest());
} else {
print("Plugin installed; a restart is required to apply the changes.");
}
printRestartRequired(ctl, !args.restart, "Plugin installed");
},
}));
hostCommands.add(hostPluginCommands);
+43 -60
View File
@@ -1,4 +1,5 @@
import fs from "node:fs/promises";
import asTableModule from "as-table";
import events from "events";
import path from "path";
import os from "os";
@@ -9,9 +10,10 @@ import util from "util";
import * as lib from "@clusterio/lib";
import { logger } from "@clusterio/lib";
import type { Ctl } from "../ctl.js";
import { errorToJson, print } from "./command_ops.js";
import { serializedConfigToString, getEditor, configToKeyVal, printConfig } from "./config_ops.js";
import { print } from "./command_ops.js";
import { serializedConfigToString, getEditor, configToKeyVal } from "./config_ops.js";
const asTable = asTableModule.configure({ delimiter: " | " });
const finished = util.promisify(stream.finished);
export const instanceCommands = new lib.CommandTree({
@@ -25,7 +27,7 @@ instanceCommands.add(new lib.Command({
}],
handler: async function(args: { templates: boolean }, ctl: Ctl) {
let list = await ctl.send(new lib.InstanceDetailsListRequest());
ctl.output.table(list.filter(instance => args.templates || !instance.isTemplate));
print(asTable(list.filter(instance => args.templates || !instance.isTemplate)));
},
}));
@@ -62,7 +64,6 @@ instanceCommands.add(new lib.Command({
instanceConfig.toRemote("controller", fields),
args.from
));
ctl.output.result({ id: instanceConfig.get("instance.id") }, () => {});
},
}));
@@ -80,13 +81,19 @@ instanceConfigCommands.add(new lib.Command({
? config["instance.inherited_fields"] as Record<string, true | string[]> ?? {}
: {};
printConfig(ctl, config, name => {
for (let [name, value] of Object.entries(config)) {
if (name === "instance.inherited_fields") {
continue;
}
const entry = inherited[name];
let note = "";
if (entry instanceof Array && entry.length) {
return ` (inherited except ${entry.join(", ")})`;
note = ` (inherited except ${entry.join(", ")})`;
} else if (entry) {
note = " (inherited)";
}
print(`${name} ${JSON.stringify(value)}${note}`);
}
return entry ? " (inherited)" : "";
});
},
}));
@@ -218,22 +225,19 @@ instanceConfigCommands.add(new lib.Command({
index,
final[index],
));
} catch (err: any) {
} catch (err) {
// eslint-disable-next-line
print(`Attempt to set ${index} to ${final[index] || String(null)} failed; set back to previous value.`);
// If the string is empty, it's better to just print "" instead of nothing
logger.error(
`Attempt to set ${index} to ${final[index] || String(null)} failed; ` +
`set back to previous value: ${err.message}`
);
print(err);
}
}
}
try {
await fs.unlink(tmpFile);
} catch (err) {
logger.warn(
`Temporary file ${tmpFile} could not be deleted. ` +
"This is not fatal, but they may build up over time if the issue persists."
);
print("err: temporary file", tmpFile, "could not be deleted.");
print("This is not fatal, but they may build up over time if the issue persists.");
}
},
}));
@@ -287,7 +291,9 @@ instanceSaveCommands.add(new lib.Command({
let instanceId = await lib.resolveInstance(ctl, args.instance);
let saves = await ctl.sendTo("controller", new lib.InstanceSaveDetailsListRequest());
saves = saves.filter(save => save.instanceId === instanceId);
ctl.output.table(saves, ({ mtimeMs, ...rest }) => ({ ...rest, mtime: new Date(mtimeMs).toLocaleString() }));
print(asTable(saves.map(
({ mtimeMs, ...rest }) => ({ ...rest, mtime: new Date(mtimeMs).toLocaleString() })
)));
},
}));
@@ -388,16 +394,13 @@ instanceSaveCommands.add(new lib.Command({
logger.error(requestError);
}
if (result.saves && result.saves.length) {
logger.info(`Successfully uploaded as ${result.saves[0]}`);
}
if ((result.errors || []).length || (result.request_errors || []).length) {
throw new lib.CommandError("Uploading save failed");
}
const name = result.saves?.[0];
ctl.output.result({ name }, () => {
if (name) {
print(`Successfully uploaded as ${name}`);
}
});
},
}));
@@ -436,10 +439,7 @@ instanceSaveCommands.add(new lib.Command({
args.copy
)
);
ctl.output.result(
{ name: storedName, instanceId: targetInstanceId },
() => print(`Transferred as ${storedName} to ${args.targetInstance}.`),
);
print(`Transferred as ${storedName} to ${args.targetInstance}.`);
},
}));
@@ -456,10 +456,7 @@ instanceSaveCommands.add(new lib.Command({
url.pathname += `api/stream/${streamId}`;
const filename = await lib.downloadFile(url, args.save, "rename");
ctl.output.result(
{ filename },
() => print(`Downloaded ${args.save}${args.save === filename ? "" : ` as ${filename}`}`),
);
logger.info(`Downloaded ${args.save}${args.save === filename ? "" : ` as ${filename}`}`);
},
}));
@@ -569,34 +566,27 @@ instanceCommands.add(new lib.Command({
});
if (instancesToStart.length === 0) {
ctl.output.result([], () => print("No instances to start."));
print("No instances to start.");
return;
}
const human = ctl.output.human;
if (human) {
print(`Starting ${instancesToStart.length} instance(s):`);
for (const instance of instancesToStart) {
print(` - ${instance.name} (ID: ${instance.id})`);
}
}
// Start all filtered instances
const results = await Promise.all(instancesToStart.map(async (instance: lib.InstanceDetails) => {
const startPromises = instancesToStart.map(async (instance: lib.InstanceDetails) => {
try {
await ctl.sendTo({ instanceId: instance.id }, new lib.InstanceStartRequest());
if (human) {
print(`✓ Started ${instance.name}`);
}
return { id: instance.id, name: instance.name };
} catch (error) {
if (human) {
print(`✗ Failed to start ${instance.name}: ${error}`);
}
return { id: instance.id, name: instance.name, error: errorToJson(error) };
}
}));
ctl.output.result(results, () => print("Start all operation completed."));
});
await Promise.all(startPromises);
print("Start all operation completed.");
},
}));
@@ -656,34 +646,27 @@ instanceCommands.add(new lib.Command({
.filter((instance: lib.InstanceDetails) => ["starting", "running"].includes(instance.status));
if (instancesToStop.length === 0) {
ctl.output.result([], () => print("No instances to stop."));
print("No instances to stop.");
return;
}
const human = ctl.output.human;
if (human) {
print(`Stopping ${instancesToStop.length} instance(s):`);
for (const instance of instancesToStop) {
print(` - ${instance.name} (ID: ${instance.id})`);
}
}
// Stop all filtered instances
const results = await Promise.all(instancesToStop.map(async (instance: lib.InstanceDetails) => {
const stopPromises = instancesToStop.map(async (instance: lib.InstanceDetails) => {
try {
await ctl.sendTo({ instanceId: instance.id }, new lib.InstanceStopRequest());
if (human) {
print(`✓ Stopped ${instance.name}`);
}
return { id: instance.id, name: instance.name };
} catch (error) {
if (human) {
print(`✗ Failed to stop ${instance.name}: ${error}`);
}
return { id: instance.id, name: instance.name, error: errorToJson(error) };
}
}));
ctl.output.result(results, () => print("Stop all operation completed."));
});
await Promise.all(stopPromises);
print("Stop all operation completed.");
},
}));
@@ -720,6 +703,6 @@ instanceCommands.add(new lib.Command({
);
// Factorio includes a newline in its response output.
ctl.output.result({ result }, () => process.stdout.write(result));
process.stdout.write(result);
},
}));
-3
View File
@@ -27,7 +27,6 @@ logCommands.add(new lib.Command({
let hostIds = args.host ? [await lib.resolveHost(ctl, args.host)] : [];
await ctl.setLogSubscriptions({ all: args.all, controller: args.controller, hostIds, instanceIds });
ctl.keepOpen = true;
ctl.output.streamingLog = true;
},
}));
@@ -76,7 +75,6 @@ logCommands.add(new lib.Command({
result.log.reverse();
}
ctl.output.result(result.log, () => {
let stdoutLogger = winston.createLogger({
level: "verbose",
levels,
@@ -88,6 +86,5 @@ logCommands.add(new lib.Command({
for (let info of result.log) {
stdoutLogger.log(info as any);
}
});
},
}));
+32 -21
View File
@@ -1,4 +1,5 @@
import fs from "node:fs/promises";
import asTableModule from "as-table";
import events from "events";
import path from "path";
import stream from "stream";
@@ -7,7 +8,9 @@ import util from "util";
import * as lib from "@clusterio/lib";
import { logger } from "@clusterio/lib";
import type { Ctl } from "../ctl.js";
import { asTable, pickFields, print } from "./command_ops.js";
import { print } from "./command_ops.js";
const asTable = asTableModule.configure({ delimiter: " | " });
const finished = util.promisify(stream.finished);
export const modCommands = new lib.CommandTree({ name: "mod", description: "Manage uploaded mods" });
@@ -21,7 +24,6 @@ modCommands.add(new lib.Command({
throw new lib.CommandError("mod-version must match format digit.digit.digit");
}
let modInfo = await ctl.send(new lib.ModGetRequest(args.name, args.modVersion));
ctl.output.result(modInfo, () => {
for (let [field, value] of Object.entries(modInfo)) {
if (value instanceof Array) {
print(`${field}:`);
@@ -32,7 +34,6 @@ modCommands.add(new lib.Command({
print(`${field}: ${value}`);
}
}
});
},
}));
@@ -49,7 +50,17 @@ modCommands.add(new lib.Command({
}],
handler: async function(args: { fields: string[] }, ctl: Ctl) {
let mods = await ctl.send(new lib.ModListRequest());
ctl.output.table(mods, mod => pickFields(mod, args.fields));
if (!args.fields.includes("all")) {
for (let entry of mods) {
for (let field of Object.keys(entry)) {
if (!args.fields.includes(field)) {
// @ts-expect-error terrible hack
delete entry[field];
}
}
}
}
print(asTable(mods));
},
}));
@@ -110,19 +121,21 @@ modCommands.add(new lib.Command({
args.sortOrder
));
let results = response.results.flatMap(result => result.versions);
ctl.output.result({
page: args.page,
pageCount: response.pageCount,
resultCount: response.resultCount,
queryIssues: response.queryIssues,
results,
}, () => {
if (!args.fields.includes("all")) {
for (let entry of results) {
for (let field of Object.keys(entry)) {
if (!args.fields.includes(field)) {
// @ts-expect-error terrible hack
delete entry[field];
}
}
}
}
for (let issue of response.queryIssues) {
print(issue);
}
print(`page ${args.page} of ${response.pageCount} (${response.resultCount} results)`);
print(asTable(results.map(mod => pickFields(mod, args.fields))));
});
print(asTable(results));
},
}));
@@ -161,16 +174,14 @@ modCommands.add(new lib.Command({
logger.error(requestError);
}
if (result.mods && result.mods.length) {
const mod = lib.ModInfo.fromJSON(result.mods[0]);
logger.info(`Successfully uploaded ${mod.filename}`);
}
if ((result.errors || []).length || (result.request_errors || []).length) {
throw new lib.CommandError("Uploading mod failed");
}
const uploaded = result.mods?.length ? lib.ModInfo.fromJSON(result.mods[0]).filename : undefined;
ctl.output.result({ filename: uploaded }, () => {
if (uploaded) {
print(`Successfully uploaded ${uploaded}`);
}
});
},
}));
@@ -190,7 +201,7 @@ modCommands.add(new lib.Command({
let filename = `${args.name}_${args.modVersion}.zip`;
await lib.downloadFile(url, filename, "overwrite");
ctl.output.result({ filename }, () => print(`Downloaded ${filename}`));
logger.info(`Downloaded ${filename}`);
},
}));
+20 -18
View File
@@ -1,7 +1,11 @@
import asTableModule from "as-table";
import * as lib from "@clusterio/lib";
import { logger } from "@clusterio/lib";
import type { Ctl } from "../ctl.js";
import { pickFields, print } from "./command_ops.js";
import { print } from "./command_ops.js";
const asTable = asTableModule.configure({ delimiter: " | " });
export const modPackCommands = new lib.CommandTree({ name: "mod-pack", description: "Mod Pack" });
modPackCommands.add(new lib.Command({
@@ -15,11 +19,7 @@ modPackCommands.add(new lib.Command({
let modPack = await ctl.send(
new lib.ModPackGetRequest(await lib.resolveModPack(ctl, args.modPack))
);
ctl.output.result(modPack, () => printModPack(modPack, mods));
},
}));
function printModPack(modPack: lib.ModPack, mods: Map<string, lib.ModInfo>) {
for (let [field, value] of Object.entries(modPack)) {
if (field === "mods") {
print(`${field}:`);
@@ -52,13 +52,23 @@ function printModPack(modPack: lib.ModPack, mods: Map<string, lib.ModInfo>) {
print(`${field}: ${value}`);
}
}
}
},
}));
modPackCommands.add(new lib.Command({
definition: [["list", "l"], "List mod packs in the cluster"],
handler: async function(args: object, ctl: Ctl) {
let modPacks = await ctl.send(new lib.ModPackListRequest());
ctl.output.table(modPacks, modPack => pickFields(modPack, ["id", "name", "factorioVersion"]));
let fields = ["id", "name", "factorioVersion"];
for (let entry of modPacks) {
for (let field of Object.keys(entry)) {
if (!fields.includes(field)) {
// @ts-expect-error terrible hack
delete entry[field];
}
}
}
print(asTable(modPacks));
},
}));
@@ -184,17 +194,10 @@ modPackCommands.add(new lib.Command({
setModPackModsEnabled(modPack, args.disabledMods, false);
setModPackSettings(modPack, args);
await ctl.send(new lib.ModPackCreateRequest(modPack));
printCreated(ctl, modPack);
print(`Created mod pack ${modPack.name} (${modPack.id})`);
},
}));
function printCreated(ctl: Ctl, modPack: lib.ModPack) {
ctl.output.result(
{ id: modPack.id, name: modPack.name },
() => print(`Created mod pack ${modPack.name} (${modPack.id})`),
);
}
modPackCommands.add(new lib.Command({
definition: ["import <string>", "Import mod pack string", (yargs) => {
yargs.positional("string", { describe: "Mod pack string to import", type: "string" });
@@ -202,7 +205,7 @@ modPackCommands.add(new lib.Command({
handler: async function(args: { string: string }, ctl: Ctl) {
const modPack = lib.ModPack.fromModPackString(args.string);
await ctl.send(new lib.ModPackCreateRequest(modPack));
printCreated(ctl, modPack);
print(`Created mod pack ${modPack.name} (${modPack.id})`);
},
}));
@@ -214,8 +217,7 @@ modPackCommands.add(new lib.Command({
const modPack = await ctl.send(
new lib.ModPackGetRequest(await lib.resolveModPack(ctl, args.modPack))
);
const modPackString = modPack.toModPackString();
ctl.output.result({ modPackString }, () => print(modPackString));
print(modPack.toModPackString());
},
}));
+7 -3
View File
@@ -1,7 +1,11 @@
import asTableModule from "as-table";
import * as lib from "@clusterio/lib";
import type { Ctl } from "../ctl.js";
import { print } from "./command_ops.js";
const asTable = asTableModule.configure({ delimiter: " | " });
export const noteCommands = new lib.CommandTree({
name: "note", description: "Notes attached to hosts, instances, users and other resources",
});
@@ -16,12 +20,12 @@ noteCommands.add(new lib.Command({
if (args.type !== undefined) {
notes = notes.filter(note => note.resourceType === args.type);
}
ctl.output.table(notes, note => ({
print(asTable(notes.map(note => ({
type: note.resourceType,
id: note.resourceId,
updatedBy: note.updatedBy,
content: note.content,
}));
}))));
},
}));
@@ -38,7 +42,7 @@ noteCommands.add(new lib.Command({
if (!note) {
throw new lib.CommandError(`No note for ${args.type} ${args.id}`);
}
ctl.output.result(note, () => print(note.content));
print(note.content);
},
}));
+7 -1
View File
@@ -1,10 +1,16 @@
import asTableModule from "as-table";
import * as lib from "@clusterio/lib";
import type { Ctl } from "../ctl.js";
import { print } from "./command_ops.js";
const asTable = asTableModule.configure({ delimiter: " | " });
export const permissionCommands = new lib.CommandTree({ name: "permission", description: "Permission inspection" });
permissionCommands.add(new lib.Command({
definition: [["list", "l"], "List permissions in the cluster"],
handler: async function(args: object, ctl: Ctl) {
ctl.output.table(await ctl.send(new lib.PermissionListRequest()));
let permissions = await ctl.send(new lib.PermissionListRequest());
print(asTable(permissions));
},
}));
+5 -3
View File
@@ -1,13 +1,15 @@
import * as lib from "@clusterio/lib";
import { logger } from "@clusterio/lib";
import type { Ctl } from "../ctl.js";
import { print } from "./command_ops.js";
import { asTable, print } from "./command_ops.js";
export const roleCommands = new lib.CommandTree({ name: "role", description: "Role management" });
roleCommands.add(new lib.Command({
definition: [["list", "l"], "List roles in the cluster"],
handler: async function(args: object, ctl: Ctl) {
ctl.output.table(await ctl.send(new lib.RoleListRequest()));
let roles = await ctl.send(new lib.RoleListRequest());
print(asTable(roles));
},
}));
@@ -25,7 +27,7 @@ roleCommands.add(new lib.Command({
args.description,
args.permissions,
));
ctl.output.result({ id }, () => print(`Created role ID ${id}`));
logger.info(`Created role ID ${id}`);
},
}));
+48 -42
View File
@@ -5,10 +5,6 @@ import * as lib from "@clusterio/lib";
import type { Ctl } from "../ctl.js";
import { asTable, print } from "./command_ops.js";
function printProperties(object: object) {
print(asTable(Object.entries(object).map(([property, value]) => ({ property, value }))));
}
export const userCommands = new lib.CommandTree({ name: "user", alias: ["u"], description: "User management" });
userCommands.add(new lib.Command({
definition: ["show <name>", "Show details for one user", (yargs) => {
@@ -18,23 +14,30 @@ userCommands.add(new lib.Command({
});
}],
handler: async function(args: { name: string, instanceStats: boolean }, ctl: Ctl) {
const user = await ctl.send(new lib.UserGetRequest(args.name));
const { playerStats, instanceStats, ...properties } = user;
const instances = args.instanceStats && ctl.output.human
? await ctl.send(new lib.InstanceDetailsListRequest())
: [];
ctl.output.result(user, () => {
printProperties({ ...properties, ...playerStats });
if (!args.instanceStats) {
return;
let user = await ctl.send(new lib.UserGetRequest(args.name));
Object.assign(user, user.playerStats);
// @ts-expect-error Terrible hack
delete user.playerStats;
let instanceStats = user.instanceStats;
// @ts-expect-error Terrible hack
delete user.instanceStats;
print(asTable(Object.entries(user).map(([property, value]) => ({ property, value }))));
if (args.instanceStats) {
let instances = await ctl.send(new lib.InstanceDetailsListRequest());
function instanceName(id: number) {
let instance = instances.find(i => i.id === id);
if (instance) {
return instance.name;
}
for (let [id, playerInstanceStats] of instanceStats) {
const name = instances.find(i => i.id === id)?.name ?? "<deleted>";
return "<deleted>";
}
for (let [id, playerInstanceStats] of instanceStats || []) {
print();
print(`Instance ${name} (${id}):`);
printProperties(playerInstanceStats);
print(`Instance ${instanceName(id)} (${id}):`);
print(asTable(Object.entries(playerInstanceStats).map(([property, value]) => ({ property, value }))));
}
}
});
},
}));
@@ -46,17 +49,29 @@ userCommands.add(new lib.Command({
});
}],
handler: async function(args: { stats: boolean, attributes: boolean }, ctl: Ctl) {
const users = await ctl.send(new lib.UserListRequest());
ctl.output.table(users, user => {
const {
playerStats, isDeleted, banReason, instanceStats, isAdmin, isWhitelisted, isBanned, ...row
} = user;
return {
...row,
...args.attributes && { isAdmin, isWhitelisted, isBanned },
...args.stats && playerStats,
};
});
let users = await ctl.send(new lib.UserListRequest());
for (let user of users) {
if (args.stats) {
Object.assign(user, user.playerStats);
}
// @ts-expect-error Terrible hack
delete user.playerStats;
// @ts-expect-error Terrible hack
delete user.isDeleted;
// @ts-expect-error Terrible hack
delete user.banReason;
// @ts-expect-error Terrible hack
delete user.instanceStats;
if (!args.attributes) {
// @ts-expect-error Terrible hack
delete user.isAdmin;
// @ts-expect-error Terrible hack
delete user.isWhitelisted;
// @ts-expect-error Terrible hack
delete user.isBanned;
}
}
print(asTable(users));
},
}));
@@ -204,17 +219,11 @@ async function handleImportOrRestore(args: {
new lib.UserBulkImportRequest(importType, importType === "users" ? data.users : data, restore)
);
let backupFile: string | undefined;
if (restore && backup && !args.noBackup) {
backupFile = `${importType}-backup.json`;
await fs.writeFile(backupFile, JSON.stringify(backup, undefined, "\t"));
}
ctl.output.result({ type: importType, backupFile }, () => {
print(`${restore ? "Restored" : "Imported"} ${importType} from ${args.filepath}`);
if (backupFile) {
print(`Wrote backup to ${backupFile}`);
if (restore && backup && !args.noBackup) {
await fs.writeFile(`${importType}-backup.json`, JSON.stringify(backup, undefined, "\t"));
print(`Wrote backup to ${importType}-backup.json`);
}
});
}
userCommands.add(new lib.Command({
@@ -287,9 +296,6 @@ userCommands.add(new lib.Command({
await fs.writeFile(args.filepath, JSON.stringify(await ctl.send(
new lib.UserBulkExportRequest(exportType)
), undefined, "\t"));
ctl.output.result(
{ type: exportType, filepath: args.filepath },
() => print(`Exported ${exportType} to ${args.filepath}`),
);
print(`Exported ${exportType} to ${args.filepath}`);
},
}));
-23
View File
@@ -1,6 +1,4 @@
import * as lib from "@clusterio/lib";
import type { Ctl } from "../ctl.js";
import { print } from "./command_ops.js";
export async function getEditor(argsEditor: string) {
// eslint-disable-next-line
@@ -55,24 +53,3 @@ export function serializedConfigToString(
}
return allConfigElements;
}
/** Print config fields as `name value` lines, or as an object in json mode */
export function printConfig(ctl: Ctl, config: lib.ConfigSchema, note = (name: string) => "") {
ctl.output.result(config, () => {
for (let [name, value] of Object.entries(config)) {
// Shown as an (inherited) note on each field by instance config list instead
if (name !== "instance.inherited_fields") {
print(`${name} ${JSON.stringify(value)}${note(name)}`);
}
}
});
}
/** Print whether a restart is still needed after an update or install */
export function printRestartRequired(ctl: Ctl, restartRequired: boolean, action: string) {
ctl.output.result({ restartRequired }, () => {
if (restartRequired) {
print(`${action}; a restart is required to apply the changes.`);
}
});
}
+4 -2
View File
@@ -17,6 +17,7 @@ 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.
@@ -146,10 +147,11 @@ async function startHost() {
.parseSync()
;
logger.add(new lib.DailyFileTransport({
logger.add(new winston.transports.DailyRotateFile({
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({
+2 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@clusterio/host",
"version": "2.1.1",
"version": "2.1.0",
"description": "Implementation of Clusterio host server",
"homepage": "https://github.com/clusterio/clusterio#readme",
"author": "Hornwitser <github@hornwitser.no>",
@@ -49,6 +49,7 @@
"semver": "catalog:",
"set-blocking": "catalog:",
"winston": "catalog:",
"winston-daily-rotate-file": "catalog:",
"yargs": "catalog:",
"yazl": "catalog:"
},
+4 -11
View File
@@ -1,6 +1,6 @@
{
"name": "@clusterio/lib",
"version": "2.1.1",
"version": "2.1.0",
"description": "Shared library for Clusterio",
"homepage": "https://github.com/clusterio/clusterio#readme",
"author": "Hornwitser <github@hornwitser.no>",
@@ -31,13 +31,6 @@
},
"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",
@@ -58,9 +51,11 @@
"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:",
@@ -76,8 +71,6 @@
"@types/triple-beam": "^1.3.5",
"@types/ws": "catalog:",
"@types/yargs": "catalog:",
"@types/yauzl": "catalog:",
"@types/yazl": "catalog:",
"typescript": "catalog:"
}
}
+1 -39
View File
@@ -4,8 +4,6 @@ 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";
@@ -56,44 +54,8 @@ 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 ModStore.readModInfo(path.join(modsDirectory, file));
let modInfo = await ModInfo.fromModFile(path.join(modsDirectory, file));
if (modInfo.filename !== file) {
throw new Error(`filename does not match the expected name ${modInfo.filename}.`);
}
+5 -13
View File
@@ -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_speed": number;
"controller.transfer_min_progress": number;
"controller.factorio_username": string | null;
"controller.factorio_token": string | null;
"controller.share_factorio_credentials_with_hosts": boolean;
@@ -63,14 +63,6 @@ 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;
}
@@ -232,11 +224,11 @@ export class ControllerConfig extends classes.Config<ControllerConfigFields> {
initialValue: 15,
validator: validators.greaterThanZero,
},
"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.",
"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.",
type: "number",
initialValue: 64,
initialValue: 30,
validator: validators.greaterThanEqualZero,
},
"controller.session_timeout": {
+34
View File
@@ -1,6 +1,10 @@
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 {
@@ -345,4 +349,34 @@ 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,
});
}
}
+2 -2
View File
@@ -1,4 +1,4 @@
import * as zlib from "#zlib_sync";
import zlib from "zlib";
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 Buffer.from(buf).toString("base64");
return buf.toString("base64");
}
static fromModPackString(modPackString: string) {
+1 -1
View File
@@ -1,4 +1,4 @@
import * as zlib from "#zlib_sync";
import zlib from "zlib";
class MapReaderState {
pos = 0;
+2 -2
View File
@@ -2,6 +2,7 @@
* @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";
@@ -257,8 +258,7 @@ export async function createTempWriteStream(filePath: string) {
async function discardPartialDownload(writeStream: stream.Writable, tempFilePath: string) {
if (!writeStream.closed) {
writeStream.destroy();
// Not events.once, it rejects if the stream is still erroring from the aborted pipe.
await new Promise(resolve => writeStream.once("close", resolve));
await events.once(writeStream, "close");
}
try {
await fs.unlink(tempFilePath);
-6
View File
@@ -1,6 +0,0 @@
/** 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 -1
View File
@@ -1,4 +1,5 @@
// Connection adapters for Links
import { strict as assert } from "assert";
import events from "events";
import { WebSocket } from "#WebSocket";
@@ -6,7 +7,6 @@ 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";
/**
+1 -1
View File
@@ -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 { assert } from "./assert.js";
import { strict as assert } from "assert";
import type { PluginNodeEnvInfo, PluginWebEnvInfo } from "../plugin.js";
import type {
AddressType, JSONDeserialisable, MessageRoutable, MessageRequest, MessageEvent, IUser,
+12 -61
View File
@@ -5,14 +5,14 @@
* @module lib/logging_utils
*/
import fs from "node:fs/promises";
import { type ReadStream, type WriteStream, createWriteStream, mkdirSync } from "node:fs";
import os from "node:os";
import type { ReadStream } from "node:fs";
import path from "path";
import stream from "stream";
import util, { styleText } from "util";
import util 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 = `${styleText("yellow", parsed.time.padStart(8))} `;
time = `${chalk.yellow(parsed.time.padStart(8))} `;
} else if (parsed.format === "date") {
time = `${styleText("yellow", parsed.time)} `;
time = `${chalk.yellow(parsed.time)} `;
}
let info = "";
if (parsed.type === "log") {
let level = parsed.level;
if (level === "Script") {
level = styleText(["bold", "greenBright"], level);
level = chalk.bold.greenBright(level);
} else if (level === "Verbose") {
level = styleText(["bold", "gray"], level);
level = chalk.bold.gray(level);
} else if (level === "Info") {
level = styleText(["bold", "blueBright"], level);
level = chalk.bold.blueBright(level);
} else if (parsed.level === "Warning") {
level = styleText(["bold", "yellowBright"], level);
level = chalk.bold.yellowBright(level);
} else if (parsed.level === "Error") {
level = styleText(["bold", "redBright"], level);
level = chalk.bold.redBright(level);
}
info = `${level} ${styleText("gray", parsed.file)}: `;
info = `${level} ${chalk.gray(parsed.file)}: `;
} else if (parsed.type === "action") {
info = `[${styleText("yellow", parsed.action)}] `;
info = `[${chalk.yellow(parsed.action)}] `;
}
return time + info + parsed.message;
@@ -226,55 +226,6 @@ 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;
+10 -42
View File
@@ -7,31 +7,10 @@ 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
@@ -42,9 +21,10 @@ export interface ZipEntry {
*/
export class ZipArchive {
private constructor(
private zipfile: yauzl.ZipFile,
/** Underlying yauzl zip file. */
public zipfile: yauzl.ZipFile,
/** Entries by file name in the order they appear in the central directory. */
public entries: Map<string, ZipEntry>,
public entries: Map<string, yauzl.Entry>,
) { }
/**
@@ -97,17 +77,8 @@ export class ZipArchive {
*
* @param entry - Entry to read, must belong to this archive.
*/
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 });
openStream(entry: yauzl.Entry): Promise<Readable> {
return this.zipfile.openReadStreamPromise(entry);
}
/**
@@ -115,7 +86,7 @@ export class ZipArchive {
*
* @param entry - Entry to read, must belong to this archive.
*/
async readEntry(entry: ZipEntry) {
async readEntry(entry: yauzl.Entry) {
const chunks: Buffer[] = [];
for await (const chunk of await this.openStream(entry)) {
chunks.push(chunk);
@@ -182,10 +153,7 @@ export function findRoot(zip: ZipArchive) {
*
* @param zipFile - zip file being written.
*/
export function zipOutputStream(zipFile: {
outputStream: NodeJS.ReadableStream,
on(event: "error", listener: (err: Error) => void): unknown,
}) {
export function zipOutputStream(zipFile: yazl.ZipFile) {
const output = zipFile.outputStream as Readable;
zipFile.on("error", err => output.destroy(err));
return output;
@@ -256,7 +224,7 @@ export class ZipWriter {
* @param zip - Archive to copy from.
* @param entry - Entry to copy, must belong to zip.
*/
async addEntry(zip: ZipArchive, entry: ZipEntry) {
async addEntry(zip: ZipArchive, entry: yauzl.Entry) {
const writerEntry: WriterEntry = {
fileName: Buffer.from(entry.fileName, "utf8"),
fileComment: Buffer.from(entry.fileComment, "utf8"),
@@ -274,7 +242,7 @@ export class ZipWriter {
offset: this.offset,
};
await this.write(this.localFileHeader(writerEntry));
const stream = await zip.openRawStream(entry);
const stream = await zip.zipfile.openReadStreamPromise(entry, { decodeFileData: false });
for await (const chunk of stream) {
await this.write(chunk);
}
-15
View File
@@ -1,15 +0,0 @@
/* 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);
}
-11
View File
@@ -1,11 +0,0 @@
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);
}
-1
View File
@@ -19,7 +19,6 @@
"./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",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@clusterio/web_ui",
"version": "2.1.1",
"version": "2.1.0",
"description": "Clusterio web interface implementation",
"homepage": "https://github.com/clusterio/clusterio#readme",
"author": "Hornwitser",
@@ -600,53 +600,6 @@ 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
@@ -869,8 +822,7 @@ function ModsTable(props: ModsTableProps) {
},
{
title: "Version",
key: "version",
render: (_, mod: lib.ModRecord) => versionSelect(mod),
dataIndex: "version",
sorter: (a, b) => strcmp(a.version, b.version),
},
{
@@ -25,7 +25,6 @@ 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;
@@ -525,7 +524,6 @@ 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",
@@ -584,7 +582,6 @@ export default function ModsPage() {
"X-Access-Token": control.connector.token || "",
}}
action={`${webRoot}api/upload-mod`}
customRequest={customRequest}
>
<UploadButton />
</Upload>;
@@ -638,7 +635,6 @@ export default function ModsPage() {
<SearchModsButton />
{uploadButton}
</Space>} />
{uploadList}
<Upload.Dragger
className="save-list-dragger"
@@ -650,7 +646,6 @@ export default function ModsPage() {
"X-Access-Token": control.connector.token || "",
}}
action={`${webRoot}api/upload-mod`}
customRequest={customRequest}
showUploadList={false}
>
<Dropzone />
+53 -6
View File
@@ -1,7 +1,7 @@
import React, { useContext, useState } from "react";
import {
message, Button, Checkbox, Form, Input, Modal,
Popconfirm, Select, Space, Table, Tooltip, Upload,
message, Button, Checkbox, Form, Input, List, Modal,
Popconfirm, Progress, Select, Space, Table, Tooltip, Upload,
} from "antd";
import CaretLeftOutlined from "@ant-design/icons/CaretLeftOutlined";
import LeftOutlined from "@ant-design/icons/LeftOutlined";
@@ -19,7 +19,6 @@ import { useSavesOfInstance } from "../model/saves";
import { notifyErrorHandler } from "../util/notify";
import { Dropzone } from "./Dropzone";
import UploadButton from "./UploadButton";
import { useUploadProgress } from "./UploadProgress";
type ModalProps = {
@@ -194,12 +193,19 @@ function TransferModal(props: ModalProps) {
}
type File = {
uid: string;
name: string;
percent: number;
status: string;
};
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);
const { customRequest, list: uploadList } = useUploadProgress();
let [uploadingFiles, setUploadingFiles] = useState<File[]>([]);
const tableState = useTableQueryState<lib.SaveDetails>({
namespace: "save", defaultSortKey: "mtimeMs", defaultSortOrder: "descend", pagination: { defaultPageSize: 10 },
});
@@ -303,6 +309,38 @@ 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",
@@ -315,7 +353,7 @@ export default function SavesList(props: { instance: lib.InstanceDetails }) {
},
showUploadList: false,
action: `${webRoot}api/upload-save`,
customRequest,
onChange,
};
return <div>
@@ -327,7 +365,6 @@ 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}>
@@ -336,5 +373,15 @@ 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>;
}
@@ -1,93 +0,0 @@
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 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@clusterio/plugin-global_chat",
"version": "2.1.1",
"version": "2.1.0",
"description": "Clusterio plugin forwarding between Factorio servers",
"homepage": "https://github.com/clusterio/clusterio#readme",
"author": "Hornwitser <github@hornwitser.no>",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@clusterio/plugin-player_auth",
"version": "2.1.1",
"version": "2.1.0",
"description": "Clusterio plugin authenticating logged in players to the web interface",
"homepage": "https://github.com/clusterio/clusterio#readme",
"author": "Hornwitser <github@hornwitser.no>",
+15 -40
View File
@@ -30,20 +30,14 @@ playerSyncCommands.add(new lib.Command({
handler: async function(args: object, ctl: Ctl) {
let players = await ctl.send(new msg.ListPlayersRequest());
players.sort((a, b) => a.name.localeCompare(b.name));
if (!ctl.output.human) {
ctl.output.result(players);
return;
}
let rows = [];
print("name | generation | size | acquired by");
for (let player of players) {
rows.push({
name: player.name,
generation: player.generation ?? "-",
size: player.size === undefined ? "-" : lib.formatBytes(player.size),
"acquired by": await instanceName(ctl, player.instanceId),
});
print(
`${player.name} | ${player.generation ?? "-"} | ` +
`${player.size === undefined ? "-" : lib.formatBytes(player.size)} | ` +
`${await instanceName(ctl, player.instanceId)}`
);
}
ctl.output.table(rows);
},
}));
@@ -53,29 +47,17 @@ playerSyncCommands.add(new lib.Command({
}],
handler: async function(args: { player: string }, ctl: Ctl) {
let response = await ctl.send(new msg.GetPlayerDataRequest(args.player));
print(`acquired by: ${await instanceName(ctl, response.instanceId)}`);
let playerData = response.playerData;
const summary = {
instanceId: response.instanceId,
...playerData && {
generation: playerData.generation,
size: JSON.stringify(playerData).length,
controller: playerData.controller,
force: playerData.force,
inventories: summarizePlayerInventories(playerData),
},
};
const acquiredBy = ctl.output.human ? await instanceName(ctl, response.instanceId) : "";
ctl.output.result(summary, () => {
print(`acquired by: ${acquiredBy}`);
if (!playerData) {
print(`No player data stored for ${args.player}`);
return;
}
print(`generation: ${summary.generation}`);
print(`size: ${lib.formatBytes(summary.size!)}`);
print(`controller: ${summary.controller}`);
print(`force: ${summary.force}`);
for (let inventory of summary.inventories!) {
print(`generation: ${playerData.generation}`);
print(`size: ${lib.formatBytes(JSON.stringify(playerData).length)}`);
print(`controller: ${playerData.controller}`);
print(`force: ${playerData.force}`);
for (let inventory of summarizePlayerInventories(playerData)) {
print(`${inventory.name}:`);
for (let item of inventory.items) {
let quality = item.quality ? ` (${item.quality})` : "";
@@ -83,7 +65,6 @@ playerSyncCommands.add(new lib.Command({
print(` ${item.count} ${item.name}${quality}${size}`);
}
}
});
},
}));
@@ -100,12 +81,9 @@ playerSyncCommands.add(new lib.Command({
let content = JSON.stringify(response.playerData, null, "\t");
if (args.file) {
await fs.writeFile(args.file, content);
ctl.output.result(
{ file: args.file },
() => print(`Wrote player data for ${args.player} to ${args.file}`),
);
print(`Wrote player data for ${args.player} to ${args.file}`);
} else {
ctl.output.result(response.playerData, () => print(content));
print(content);
}
},
}));
@@ -123,10 +101,7 @@ playerSyncCommands.add(new lib.Command({
playerData.name = args.player;
playerData.generation ??= 0;
let response = await ctl.send(new msg.SetPlayerDataRequest(args.player, playerData));
ctl.output.result(
{ generation: response.generation },
() => print(`Stored player data for ${args.player} as generation ${response.generation}`),
);
print(`Stored player data for ${args.player} as generation ${response.generation}`);
},
}));
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@clusterio/plugin-player_sync",
"version": "2.1.1",
"version": "2.1.0",
"description": "Clusterio plugin forwarding between Factorio servers",
"homepage": "https://github.com/clusterio/clusterio#readme",
"author": "Danielv <danielv@danielv.no>",
+3 -24
View File
@@ -4,7 +4,7 @@ import os from "node:os";
import path from "node:path";
import * as lib from "@clusterio/lib";
import { CommandOutput, CtlHooks } from "@clusterio/ctl";
import { CtlHooks } from "@clusterio/ctl";
import * as mock from "../../../test/mock.js";
import { testRoundTripJsonSerialisable, testMatrix } from "../../../test/common.js";
@@ -303,7 +303,6 @@ describe("player_sync plugin", function() {
let printed;
let responses;
const ctl = {
output: new CommandOutput(),
async send(message) {
sent.push(message);
const handler = responses.get(message.constructor);
@@ -332,7 +331,6 @@ describe("player_sync plugin", function() {
beforeEach(function() {
sent = [];
printed = [];
ctl.output = new CommandOutput();
responses = new Map([
[lib.InstanceDetailsGetRequest, request => ({ id: request.instanceId, name: "Test" })],
[msg.ListPlayersRequest, () => [
@@ -362,10 +360,8 @@ describe("player_sync plugin", function() {
it("list should print sorted entries", async function() {
await run("list");
const lines = printed.join("\n").split("\n").map(line => line.trimEnd());
assert.deepEqual(lines, [
assert.deepEqual(printed, [
"name | generation | size | acquired by",
"--------------------------------------------",
`alice | 2 | ${lib.formatBytes(100)} | -`,
"bob | - | - | Test (1)",
]);
@@ -388,7 +384,7 @@ describe("player_sync plugin", function() {
throw new lib.PermissionError("Permission denied");
});
await run("list");
assert.match(printed[0], /bob +\| - +\| - +\| 1/);
assert.equal(printed[2], "bob | - | - | 1");
printed = [];
await run("show", { player: "alice" });
assert.equal(printed[0], "acquired by: 1");
@@ -425,23 +421,6 @@ describe("player_sync plugin", function() {
new lib.CommandError("Player data must be a JSON object"),
);
});
it("should print JSON in json mode", async function() {
ctl.output = new CommandOutput("json");
await run("list");
assert.deepEqual(JSON.parse(printed[0]), [
{ name: "alice", generation: 2, size: 100 },
{ name: "bob", instanceId: 1 },
]);
printed = [];
await run("show", { player: "alice" });
const shown = JSON.parse(printed[0]);
assert.equal(shown.instanceId, 1);
assert.equal(shown.generation, 2);
assert.deepEqual(shown.inventories[0].items[0], { name: "wood", count: 5, quality: "rare" });
printed = [];
await run("export", { player: "alice" });
assert.deepEqual(JSON.parse(printed[0]), storedData);
});
it("delete and release should send requests", async function() {
await run("delete", { player: "alice" });
await run("release", { player: "alice" });
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@clusterio/plugin-research_sync",
"version": "2.1.1",
"version": "2.1.0",
"description": "Clusterio plugin for synchronizing research between Factorio servers",
"homepage": "https://github.com/clusterio/clusterio#readme",
"author": "Hornwitser <github@hornwitser.no>",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@clusterio/plugin-statistics_exporter",
"version": "2.1.1",
"version": "2.1.0",
"description": "Clusterio plugin for gathering statistics from Factorio servers",
"homepage": "https://github.com/clusterio/clusterio#readme",
"author": "Hornwitser <github@hornwitser.no>",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@clusterio/plugin-subspace_storage",
"version": "2.1.1",
"version": "2.1.0",
"description": "Clusterio plugin for sharing storage between Factorio servers",
"homepage": "https://github.com/clusterio/clusterio#readme",
"author": "Hornwitser <github@hornwitser.no>",
+2 -1
View File
@@ -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,6 +34,7 @@ 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
View File
File renamed without changes.
+24 -42
View File
@@ -195,21 +195,20 @@ 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) {
problems.push(`Failed to extract changelog in ${pr.html_url}: ${error}`);
console.error(`Failed to extract changelog in ${pr.html_url}: ${error}`)
continue;
}
for (const warning of warnings) {
problems.push(`Warning in changelog for ${pr.html_url}: ${warning}`);
console.warn(`Warning in changelog for ${pr.html_url}: ${warning}`)
}
for (const [section, items] of Object.entries(changelog)) {
mergedChangelog[section] = [...mergedChangelog[section] ?? [], ...items];
}
}
return [mergedChangelog, problems] as const;
return mergedChangelog;
}
async function fetchLastRelease() {
@@ -259,7 +258,7 @@ async function fetchStatistics(lastRelease: Release, pullRequests: Issue[]): Pro
};
}
function formatMarkdown(
function printMarkdown(
changelog: Changelog,
issues: Issue[],
users: Issue["user"][],
@@ -267,13 +266,12 @@ function formatMarkdown(
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) {
lines.push(`### ${section}`, "");
console.log(`### ${section}\n`);
const entries = changelog[section];
lines.push(...entries.map(text => (
console.log(entries.map(text => (
`- ${text.replaceAll(/#(\d+)/g, (ref, idAsText) => {
const id = Number.parseInt(idAsText, 10);
if (!Number.isFinite(id)) {
@@ -285,29 +283,27 @@ function formatMarkdown(
}
return refText(issue);
})}`
)));
lines.push("");
)).join("\n"));
console.log();
}
}
lines.push("### Contributors", "");
lines.push("Many thanks to the following for contributing to this release:");
lines.push(users.map(userText).join(", "));
console.log("### Contributors\n");
console.log(`Many thanks to the following for contributing to this release:`);
console.log(users.map(userText).join(", "));
const format = (value: number) => value.toLocaleString("en-US");
lines.push("", "### Statistics", "");
lines.push([
console.log("\n### Statistics\n");
console.log([
`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");
}
/** Builds the release notes for the changes merged since the last release. */
export async function generateReleaseNotes() {
async function main() {
const lastRelease = await fetchLastRelease();
const issues = await fetchIssuesUpdatedSince(lastRelease.created_at);
@@ -317,37 +313,23 @@ export async function generateReleaseNotes() {
&& issue.pull_request.merged_at > lastRelease.created_at
));
const [changelog, problems] = changelogFromPullRequests(pullRequests, issues);
const users = [...new Map(pullRequests.map(issue => [issue.user.login, issue.user])).values()];
const changelog = 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 ===");
console.log(notes.github);
printMarkdown(changelog, issues, users, statistics,
issue => `${repository}#${issue.number}`,
user => `@${user.login}`,
);
console.log();
console.log("=== Discord / Changelog Markdown ===");
console.log(notes.changelog);
printMarkdown(changelog, issues, users, statistics,
issue => `[#${issue.number}](<${issue.html_url}>)`,
user => `[@${user.login}](<${user.html_url}>)`
);
}
if (import.meta.main) {
-103
View File
@@ -1,103 +0,0 @@
// 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;
});
}
+15 -76
View File
@@ -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 without a burst allowance.
routes.addRouteHandlers(controller.app, 500, 0);
// Check the progress of requests every 500 ms instead of every 30 seconds.
routes.addRouteHandlers(controller.app, 500);
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_speed", 2);
controller.mockConfigEntries.set("controller.transfer_min_progress", 1);
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_speed", 8);
controller.mockConfigEntries.set("controller.transfer_min_progress", 4);
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_speed", 2);
controller.mockConfigEntries.set("controller.transfer_min_progress", 1);
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,21 +216,13 @@ 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(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) });
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) });
const received = await new Promise((resolve, reject) => {
http.get(`${slowEndpoint}/${stream.id}`, response => {
http.get(`${endpoint}/${stream.id}`, response => {
let bytes = 0;
response.on("data", chunk => {
bytes += chunk.length;
@@ -243,14 +235,11 @@ 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_speed", 2);
controller.mockConfigEntries.set("controller.transfer_min_progress", 1);
const stream = await routes.createProxyStream(controller.app);
const download = fetch(`${endpoint}/${stream.id}`);
await lib.wait(1500);
@@ -1268,7 +1257,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_speed", 8);
controller.mockConfigEntries.set("controller.transfer_min_progress", 4);
const modsDirectory = await fs.mkdtemp(path.join(os.tmpdir(), "clusterio-mods-"));
try {
controller.mockConfigEntries.set("controller.mods_directory", modsDirectory);
@@ -1288,65 +1277,15 @@ 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_speed", 8);
controller.mockConfigEntries.set("controller.transfer_min_progress", 4);
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_speed", 0);
controller.mockConfigEntries.set("controller.transfer_min_progress", 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`);
-95
View File
@@ -1,95 +0,0 @@
import assert from "node:assert/strict";
import * as lib from "@clusterio/lib";
import { CommandOutput, errorToJson, pickFields, toPlain } from "@clusterio/ctl/dist/node/src/command_ops.js";
/** Run fn and return the lines it printed with console.log */
function captureLog(fn) {
const lines = [];
const originalLog = console.log; // eslint-disable-line no-console
console.log = (...args) => lines.push(args.join(" ")); // eslint-disable-line no-console
try {
fn();
} finally {
console.log = originalLog; // eslint-disable-line no-console
}
return lines;
}
describe("ctl/command_ops", function() {
describe("toPlain()", function() {
it("converts Sets, Maps and Dates", function() {
assert.deepEqual(
toPlain({ set: new Set([1, 2]), map: new Map([[3, { a: new Set(["b"]) }]]), date: new Date(0) }),
{ set: [1, 2], map: { 3: { a: ["b"] } }, date: "1970-01-01T00:00:00.000Z" },
);
});
it("uses own properties instead of toJSON", function() {
const role = new lib.Role(1, "name", "desc", new Set(["core.admin"]));
assert.deepEqual(toPlain(role).permissions, ["core.admin"]);
});
});
describe("pickFields()", function() {
it("keeps only the given fields unless all is given", function() {
assert.deepEqual(pickFields({ a: 1, b: 2 }, ["a"]), { a: 1 });
assert.deepEqual(pickFields({ a: 1, b: 2 }, ["all"]), { a: 1, b: 2 });
});
});
describe("errorToJson()", function() {
it("uses the class name of the error", function() {
assert.deepEqual(errorToJson(new lib.NotFoundError("gone")), { error: "NotFoundError", message: "gone" });
assert.deepEqual(errorToJson("text"), { error: "Error", message: "text" });
});
});
describe("class CommandOutput", function() {
it("calls the human formatter in human mode", function() {
const output = new CommandOutput("human");
const human = () => console.log("human"); // eslint-disable-line no-console
assert.deepEqual(captureLog(() => output.result({ a: 1 }, human)), ["human"]);
assert.deepEqual(captureLog(() => output.finish()), []);
});
it("prints a success line in human mode for commands without a result", function() {
assert.deepEqual(captureLog(() => new CommandOutput("human").finish()), ["Command ran successfully"]);
});
it("prints one line of JSON per result in json mode", function() {
const output = new CommandOutput("json");
const lines = captureLog(() => {
output.result({ a: new Set([1]) }, () => assert.fail("called human formatter"));
output.table([{ b: 2 }]);
output.finish();
});
assert.deepEqual(lines, ['{"a":[1]}', '[{"b":2}]']);
});
it("prints the toJSON format in wire mode", function() {
const output = new CommandOutput("wire");
const role = new lib.Role(1, "name", "desc", new Set(["core.admin"]));
const lines = captureLog(() => output.result({ role, set: new Set([1]), map: new Map([["a", 2]]) }));
assert.deepEqual(JSON.parse(lines[0]), { role: JSON.parse(JSON.stringify(role)), set: [1], map: { a: 2 } });
});
it("prints success for commands without a result", function() {
assert.deepEqual(captureLog(() => new CommandOutput("json").finish()), ['{"success":true}']);
const streaming = new CommandOutput("json");
streaming.streamingLog = true;
assert.deepEqual(captureLog(() => streaming.finish()), []);
});
it("prints errors as JSON", function() {
const output = new CommandOutput("json");
const lines = captureLog(() => {
output.error(new lib.CommandError("failed"), () => assert.fail("called human formatter"));
output.finish();
});
assert.deepEqual(lines, ['{"error":"CommandError","message":"failed"}']);
});
it("calls the human error reporter in human mode", function() {
const output = new CommandOutput("human");
const human = () => console.log("failed"); // eslint-disable-line no-console
const lines = captureLog(() => {
output.error(new lib.CommandError("failed"), human);
output.finish();
});
assert.deepEqual(lines, ["failed"]);
});
});
});
-88
View File
@@ -1,88 +0,0 @@
import assert from "node:assert/strict";
import * as lib from "@clusterio/lib";
import { modCommands } from "@clusterio/ctl/dist/node/src/commands_mod.js";
import { modPackCommands } from "@clusterio/ctl/dist/node/src/commands_mod_pack.js";
import { userCommands } from "@clusterio/ctl/dist/node/src/commands_user.js";
import { CommandOutput } from "@clusterio/ctl/dist/node/src/command_ops.js";
/** Run fn and return the lines it printed with console.log */
async function captureLog(fn) {
const lines = [];
const originalLog = console.log; // eslint-disable-line no-console
console.log = (...args) => lines.push(args.join(" ")); // eslint-disable-line no-console
try {
await fn();
} finally {
console.log = originalLog; // eslint-disable-line no-console
}
return lines;
}
/** Mock ctl answering requests by their class */
function mockCtl(responses) {
return {
output: new CommandOutput(),
async send(message) {
return responses.get(message.constructor)(message);
},
async sendTo(destination, message) {
return this.send(message);
},
};
}
describe("ctl/commands", function() {
describe("mod show", function() {
const show = modCommands.get("show");
it("prints list fields one entry per line", async function() {
const ctl = mockCtl(new Map([
[lib.ModGetRequest, () => ({ name: "a_mod", dependencies: ["base", "? other"] })],
]));
const lines = await captureLog(() => show.run({ name: "a_mod", modVersion: "1.0.0" }, ctl));
assert.deepEqual(lines, ["name: a_mod", "dependencies:", " base", " ? other"]);
});
it("rejects an invalid version", async function() {
await assert.rejects(
show.run({ name: "a_mod", modVersion: "latest" }, mockCtl(new Map())),
new lib.CommandError("mod-version must match format digit.digit.digit"),
);
});
});
describe("mod-pack show", function() {
it("marks missing mods and checksum mismatches and lists export assets", async function() {
const modPack = lib.ModPack.fromJSON({ id: 7, name: "pack" });
modPack.mods.set("gone", { name: "gone", enabled: true, version: "1.0.0" });
modPack.mods.set("changed", { name: "changed", enabled: false, version: "1.0.0", sha1: "aaa" });
modPack.exportManifest = { assets: { settings: "settings.json" } };
const ctl = mockCtl(new Map([
[lib.ModListRequest, () => [{ id: "changed_1.0.0", sha1: "bbb" }]],
[lib.ModPackListRequest, () => [modPack]],
[lib.ModPackGetRequest, () => modPack],
]));
const lines = await captureLog(() => modPackCommands.get("show").run({ modPack: "pack" }, ctl));
assert(lines.includes(" ? gone 1.0.0"), lines.join("\n"));
assert(lines.includes(" ! (disabled) changed 1.0.0 (aaa)"), lines.join("\n"));
assert(lines.includes(" settings: settings.json"), lines.join("\n"));
});
});
describe("user show", function() {
const show = userCommands.get("show");
it("prints stats per instance", async function() {
const stats = new lib.PlayerStats();
stats.joinCount = 2;
const user = new lib.UserDetails("alice");
user.instanceStats = new Map([[1, stats], [2, stats]]);
user.recalculatePlayerStats();
const ctl = mockCtl(new Map([
[lib.UserGetRequest, () => user],
[lib.InstanceDetailsListRequest, () => [{ id: 1, name: "one" }]],
]));
const lines = await captureLog(() => show.run({ name: "alice", instanceStats: true }, ctl));
assert(lines.includes("Instance one (1):"), lines.join("\n"));
assert(lines.includes("Instance <deleted> (2):"), lines.join("\n"));
assert(lines.some(line => /joinCount +\| 4/.test(line)), lines.join("\n"));
});
});
});
-91
View File
@@ -1,91 +0,0 @@
import assert from "node:assert/strict";
import * as lib from "@clusterio/lib";
import { instanceCommands } from "@clusterio/ctl/dist/node/src/commands_instance.js";
import { CommandOutput } from "@clusterio/ctl/dist/node/src/command_ops.js";
/** Run fn and return the lines it printed with console.log */
async function captureLog(fn) {
const lines = [];
const originalLog = console.log; // eslint-disable-line no-console
console.log = (...args) => lines.push(args.join(" ")); // eslint-disable-line no-console
try {
await fn();
} finally {
console.log = originalLog; // eslint-disable-line no-console
}
return lines;
}
describe("ctl/commands_instance", function() {
let instances;
let sentTo;
const ctl = {
output: new CommandOutput(),
async send(message) {
assert(message instanceof lib.InstanceDetailsListRequest);
return instances;
},
async sendTo(destination, message) {
sentTo.push([destination.instanceId, message.constructor.name]);
if (destination.instanceId === 2) {
throw new lib.CommandError("broken");
}
},
};
beforeEach(function() {
sentTo = [];
instances = [
{ id: 1, name: "one", status: "stopped", excludeFromStartAll: false },
{ id: 2, name: "two", status: "stopped", excludeFromStartAll: false },
{ id: 3, name: "three", status: "stopped", excludeFromStartAll: true },
{ id: 4, name: "four", status: "running", excludeFromStartAll: false },
];
});
describe("start-all", function() {
const startAll = instanceCommands.get("start-all");
it("starts stopped instances and reports each one", async function() {
ctl.output = new CommandOutput("human");
const lines = await captureLog(() => startAll.run({ force: false }, ctl));
assert.deepEqual(sentTo, [[1, "InstanceStartRequest"], [2, "InstanceStartRequest"]]);
assert(lines.includes("✓ Started one"), lines.join("\n"));
assert(lines.some(line => line.startsWith("✗ Failed to start two")), lines.join("\n"));
});
it("prints an entry per instance in json mode", async function() {
ctl.output = new CommandOutput("json");
const lines = await captureLog(() => startAll.run({ force: true }, ctl));
assert.deepEqual(JSON.parse(lines[0]), [
{ id: 1, name: "one" },
{ id: 2, name: "two", error: { error: "CommandError", message: "broken" } },
{ id: 3, name: "three" },
]);
});
it("reports when there is nothing to start", async function() {
instances = [];
ctl.output = new CommandOutput("human");
assert.deepEqual(await captureLog(() => startAll.run({ force: false }, ctl)), ["No instances to start."]);
});
});
describe("stop-all", function() {
const stopAll = instanceCommands.get("stop-all");
it("stops running instances and reports each one", async function() {
instances[1].status = "running";
ctl.output = new CommandOutput("human");
const lines = await captureLog(() => stopAll.run({}, ctl));
assert.deepEqual(sentTo, [[2, "InstanceStopRequest"], [4, "InstanceStopRequest"]]);
assert(lines.includes("✓ Stopped four"), lines.join("\n"));
assert(lines.some(line => line.startsWith("✗ Failed to stop two")), lines.join("\n"));
});
it("prints an entry per instance in json mode", async function() {
ctl.output = new CommandOutput("json");
const lines = await captureLog(() => stopAll.run({}, ctl));
assert.deepEqual(JSON.parse(lines[0]), [{ id: 4, name: "four" }]);
});
it("reports when there is nothing to stop", async function() {
instances = [];
ctl.output = new CommandOutput("human");
assert.deepEqual(await captureLog(() => stopAll.run({}, ctl)), ["No instances to stop."]);
});
});
});
-56
View File
@@ -1,56 +0,0 @@
import assert from "node:assert/strict";
import * as lib from "@clusterio/lib";
import { Ctl } from "@clusterio/ctl/dist/node/ctl.js";
import { CommandOutput } from "@clusterio/ctl/dist/node/src/command_ops.js";
/** Run fn and return the lines it printed with console.log */
async function captureLog(fn) {
const lines = [];
const originalLog = console.log; // eslint-disable-line no-console
console.log = (...args) => lines.push(args.join(" ")); // eslint-disable-line no-console
try {
await fn();
} finally {
console.log = originalLog; // eslint-disable-line no-console
}
return lines;
}
describe("ctl/ctl", function() {
describe("class Ctl", function() {
let ctl;
beforeEach(function() {
const connector = new lib.VirtualConnector(
lib.Address.fromShorthand({ controlId: 1 }),
lib.Address.fromShorthand("controller"),
);
ctl = new Ctl(connector, new lib.CtlConfig("control"));
});
it("prints log events as JSON lines only when streaming the log", async function() {
const info = { level: "info", message: "hello" };
ctl.output = new CommandOutput("json");
const logged = await captureLog(() => ctl.handleLogMessageEvent(new lib.LogMessageEvent({ ...info })));
assert(logged.every(line => !line.startsWith("{")), "log event printed as a result");
ctl.output.streamingLog = true;
assert.deepEqual(
await captureLog(() => ctl.handleLogMessageEvent(new lib.LogMessageEvent({ ...info, timestamp: 1 }))),
['{"level":"info","message":"hello","timestamp":1}'],
);
});
it("logs log events in human mode", async function() {
ctl.output.streamingLog = true;
const info = { level: "verbose", message: "hello" };
assert.deepEqual(await captureLog(() => ctl.handleLogMessageEvent(new lib.LogMessageEvent(info))), []);
});
it("prints debug WebSocket messages", async function() {
const event = new lib.DebugWsMessageEvent("in", "content");
assert.deepEqual(await captureLog(() => ctl.handleDebugWsMessageEvent(event)), ["WS in content"]);
ctl.output = new CommandOutput("json");
assert.deepEqual(
await captureLog(() => ctl.handleDebugWsMessageEvent(event)),
['{"direction":"in","content":"content"}'],
);
});
});
});
-124
View File
@@ -2064,9 +2064,6 @@ describe("Integration of Clusterio", function() {
const roundtrip = lib.ModPack.fromModPackString(result.stdout.trim());
roundtrip.id = reference.id;
assert.deepEqual(roundtrip, reference);
const shown = await execCtlOutput("mod-pack show imported-pack");
assert.match(shown, /empty_mod 1\.0\.0/);
assert.match(shown, /MyDouble: 12\.25/);
});
});
@@ -2122,7 +2119,6 @@ describe("Integration of Clusterio", function() {
describe("mod show", function() {
it("gives details of a mod", async function() {
assert.match(await execCtlOutput("mod show empty_mod 1.0.0"), /name: empty_mod/);
let result = await execCtlProcess("mod show empty_mod 1.0.0");
let hash = await lib.hashFile(path.join("temp", "test", "mods", "empty_mod_1.0.0.zip"));
let stat = await fs.stat(path.join("temp", "test", "mods", "empty_mod_1.0.0.zip"));
@@ -2321,126 +2317,6 @@ describe("Integration of Clusterio", function() {
});
});
describe("config edit", function() {
/** Write an editor script that replaces the edited file with content */
async function writeEditor(content) {
const file = path.resolve("temp", "test", "editor.sh");
await fs.writeFile(file, `#!/bin/sh\ncat > "$1" <<'EOF'\n${content}\nEOF\n`, { mode: 0o755 });
return file;
}
before(function() {
if (process.platform === "win32") {
this.skip();
}
});
it("sets controller config fields and reports failed ones", async function() {
const config = await getControl().send(new lib.ControllerConfigGetRequest());
const name = config["controller.name"];
const editor = await writeEditor("controller.name = Edited-Cluster\ncontroller.no_such_field = 1");
await execCtl(`controller config edit ${editor}`);
const edited = await getControl().send(new lib.ControllerConfigGetRequest());
assert.equal(edited["controller.name"], "Edited-Cluster");
await execCtl(`controller config set controller.name ${name}`);
});
it("sets host config fields and reports failed ones", async function() {
const config = await getControl().sendTo({ hostId }, new lib.HostConfigGetRequest());
const editor = await writeEditor(`host.name = ${config["host.name"]}\nhost.no_such_field = 1`);
await execCtl(`host config edit ${hostId} ${editor}`);
});
it("sets instance config fields and reports failed ones", async function() {
const { id } = JSON.parse(await execCtlOutput("--output json instance create config-edit"));
try {
const editor = await writeEditor("instance.auto_start = true\ninstance.no_such_field = 1");
await execCtl(`instance config edit ${id} ${editor}`);
const config = await getControl().send(new lib.InstanceConfigGetRequest(id));
assert.equal(config["instance.auto_start"], true);
} finally {
await execCtl(`instance delete ${id}`);
}
});
});
describe("--output json", function() {
it("prints lists as an array of objects", async function() {
const roles = JSON.parse(await execCtlOutput("--output json role list"));
assert.deepEqual(roles.find(role => role.id === 0).permissions, ["core.admin"]);
});
it("prints single results as an object", async function() {
const { id } = JSON.parse(await execCtlOutput("--output json role create json-role"));
const roles = await getControl().send(new lib.RoleListRequest());
assert.equal(roles.find(role => role.id === id)?.name, "json-role");
assert.equal(await execCtlOutput(`--output json role delete ${id}`), '{"success":true}');
});
it("prints the toJSON format in wire mode", async function() {
const [jsonUser] = JSON.parse(await execCtlOutput("--output json user list"));
const [wireUser] = JSON.parse(await execCtlOutput("--output wire user list"));
assert("roleIds" in jsonUser);
assert(!("roleIds" in wireUser));
assert.equal(wireUser.name, jsonUser.name);
});
it("prints a success line for commands without a result", async function() {
const { id } = JSON.parse(await execCtlOutput("--output json role create human-role"));
assert.equal(await execCtlOutput(`role delete ${id}`), "Command ran successfully");
});
it("prints whole objects for lists that hide fields in human mode", async function() {
const [mod] = JSON.parse(await execCtlOutput("--output json mod list"));
assert("sha1" in mod);
const [wireMod] = JSON.parse(await execCtlOutput("--output wire mod list"));
assert("factorio_version" in wireMod);
const [modPack] = JSON.parse(await execCtlOutput("--output wire mod-pack list"));
assert("mods" in modPack);
const user = JSON.parse(await execCtlOutput("--output wire user show test --instance-stats"));
assert.equal(user.name, "test");
});
it("prints the user and instance stats in human mode", async function() {
const output = await execCtlOutput("user show test --instance-stats");
assert.match(output, /name +\| test/);
});
it("prints the result of human mode commands with more options", async function() {
assert.match(await execCtlOutput("user list --stats --attributes"), /isAdmin/);
const exchange = JSON.parse(await execCtlOutput(
`--output json debug parse-exchange-string ${testStrings.default.replace(/\s/g, "")}`
));
assert.equal(typeof exchange, "object");
});
it("follows the log without a final result", async function() {
try {
assert.equal(await execCtlOutput("--output json log follow --controller"), "");
} finally {
getControl().keepOpen = false;
}
});
it("prints log queries in human mode", async function() {
assert.notEqual(await execCtlOutput("log query --controller --limit 1"), "");
});
it("prints pending host requests in human mode", async function() {
assert.notEqual(await execCtlOutput("host pending"), "");
});
it("prints log queries as an array", async function() {
const log = JSON.parse(await execCtlOutput("--output json log query --controller --limit 1"));
assert.equal(log.length, 1);
assert.equal(typeof log[0].message, "string");
});
it("prints errors as JSON with a non-zero exit code", async function() {
await assert.rejects(
execCtlProcess("--output json instance stop no-such-instance"),
err => {
assert.equal(err.code, 1);
assert.deepEqual(
JSON.parse(err.stdout),
{ error: "CommandError", message: "No instance named no-such-instance" },
);
return true;
},
);
await assert.rejects(
execCtlProcess("--output json user set-admin no-such-user"),
err => JSON.parse(err.stdout).error === "NotFoundError",
);
});
});
describe("user list", function() {
it("prints roles and instances as lists", async function() {
const output = await execCtlOutput("user list");
+3 -2
View File
@@ -10,7 +10,7 @@ import events from "node:events";
import * as lib from "@clusterio/lib";
import { LineSplitter, logger } from "@clusterio/lib";
import { runCommand, initialize as initializeCtl } from "@clusterio/ctl";
import { selectTargetCommand, initialize as initializeCtl } from "@clusterio/ctl";
import { _listFactorioVersions } from "@clusterio/host/dist/node/src/server.js";
import { pairHost } from "@clusterio/host";
@@ -242,7 +242,8 @@ export async function execCtl(command) {
}
control.config = initArgs.ctlConfig;
await runCommand(initArgs.args, initArgs.rootCommands, control);
const targetCommand = selectTargetCommand(initArgs.args, initArgs.rootCommands);
await targetCommand.run(initArgs.args, control);
} finally {
process.chdir("../..");
inExecCtl = false;
+1 -34
View File
@@ -6,8 +6,7 @@ import stream from "node:stream";
// Capture native fetch before any potential mocks are applied
const nativeFetch = global.fetch;
import { ModStore, ModInfo, ModVersionEquality, zipOutputStream, hashFile } from "@clusterio/lib";
import * as libBuildMod from "@clusterio/lib/build_mod.js";
import { ModStore, ModInfo, ModVersionEquality, zipOutputStream } from "@clusterio/lib";
import { externalTest, createZip, createZipBuffer } from "../common.js";
import "../setup_logging.js";
@@ -707,38 +706,6 @@ 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";
-10
View File
@@ -15,16 +15,6 @@ 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() {
+32
View File
@@ -1,6 +1,9 @@
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";
@@ -410,6 +413,35 @@ 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"] });
-14
View File
@@ -412,20 +412,6 @@ 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.
-72
View File
@@ -1,6 +1,5 @@
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";
@@ -67,77 +66,6 @@ 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 = [
-25
View File
@@ -1,25 +0,0 @@
/* 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
View File
@@ -175,7 +175,7 @@ export class MockController {
["controller.public_url", "test"],
["controller.auth_secret", "TestSecretDoNotUse"],
["controller.proxy_stream_timeout", 1],
["controller.transfer_min_speed", 64],
["controller.transfer_min_progress", 30],
["controller.metrics_timeout", 1],
["controller.host_pairing_timeout", 3600],
["controller.host_pairing_max_pending", 50],
-35
View File
@@ -1,35 +0,0 @@
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
View File
@@ -4,7 +4,7 @@ import fs from "node:fs/promises";
import path from "node:path";
import { promisify } from "node:util";
import { publishMods, uploadModRelease } from "../scripts/publish.js";
import { publishMods, uploadModRelease } from "../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)}`,
"scripts/publish.js", "factorio", directory, "--dry-run",
"publish.js", "factorio", directory, "--dry-run",
]);
assert.equal(stderr, "");
assert(stdout.includes("Requesting current releases of cli_publish_test"));