From 62f17a20afdc2e7d8fd66c22414458b00d8b32bc Mon Sep 17 00:00:00 2001 From: bbassie <17990055+bbassie@users.noreply.github.com> Date: Wed, 18 Nov 2026 11:15:40 +0000 Subject: [PATCH] Take platform and arch in the lib headless helpers hasFactorioHeadless(version, platform, arch) replaces the host's hasArm64Headless, and factorioHeadlessUrl takes the platform too and throws when Factorio has no headless build for it, which for now is every platform except Linux. headlessUrl is marked deprecated. Co-Authored-By: Claude Opus 5.5 --- packages/host/src/server.ts | 8 +--- packages/lib/src/external/FactorioVersions.ts | 44 ++++++++++++++++--- test/lib/external/FactorioVersions.test.js | 34 ++++++++++++-- 3 files changed, 70 insertions(+), 16 deletions(-) diff --git a/packages/host/src/server.ts b/packages/host/src/server.ts index 9c4f8d7f..c5d01365 100644 --- a/packages/host/src/server.ts +++ b/packages/host/src/server.ts @@ -191,10 +191,6 @@ function factorioArch(): lib.FactorioHeadlessArch { return process.platform === "linux" && process.arch === "arm64" ? "arm64" : "x64"; } -function hasArm64Headless(version: lib.FullVersion) { - return lib.integerFullVersion(version) >= lib.integerFullVersion(lib.minimumArm64HeadlessVersion); -} - // Factorio downloads in progress by target directory, shared by every server in the process const downloadsInProgress = new Map>(); @@ -221,7 +217,7 @@ export async function checkForUpdates( const arch = factorioArch(); const matching = availableVersions.filter(v => matchesTarget(v.version)); const latestVersion = matching - .filter(v => arch !== "arm64" || hasArm64Headless(v.version)) + .filter(v => process.platform !== "linux" || lib.hasFactorioHeadless(v.version, "linux", arch)) .sort((v1, v2) => versionOrder(v1.version, v2.version)) [0]; @@ -268,7 +264,7 @@ export async function checkForUpdates( logger.info(`A newer version of factorio is available (${v}), waiting for download...`); } else { logger.info(`A newer version of factorio is available (${v}), starting download...`); - download = downloadAndExtractTar(lib.factorioHeadlessUrl(v, arch), dst).finally(() => { + download = downloadAndExtractTar(lib.factorioHeadlessUrl(v, "linux", arch), dst).finally(() => { downloadsInProgress.delete(dst); }); downloadsInProgress.set(dst, download); diff --git a/packages/lib/src/external/FactorioVersions.ts b/packages/lib/src/external/FactorioVersions.ts index 06b370f7..e04724cf 100644 --- a/packages/lib/src/external/FactorioVersions.ts +++ b/packages/lib/src/external/FactorioVersions.ts @@ -1,5 +1,5 @@ import { Static, Type } from "@sinclair/typebox"; -import { FullVersionSchema, integerFullVersion, isFullVersion } from "../data/version.js"; +import { FullVersion, FullVersionSchema, integerFullVersion, isFullVersion } from "../data/version.js"; const ARCHIVE_URL = "https://factorio.com/download/archive/"; @@ -9,25 +9,57 @@ const ARCHIVE_URL = "https://factorio.com/download/archive/"; export const ExternalFactorioVersionSchema = Type.Object({ stable: Type.Boolean(), version: FullVersionSchema, + /** @deprecated Linux x64 only, use {@link factorioHeadlessUrl} instead. */ headlessUrl: Type.String(), }); export type ExternalFactorioVersion = Static; -/** CPU architectures Factorio publishes Linux headless builds for */ +/** Platforms Factorio may publish headless builds for, named as in Node's process.platform */ +export type FactorioHeadlessPlatform = "linux" | "win32"; + +/** CPU architectures Factorio may publish headless builds for, named as in Node's process.arch */ export type FactorioHeadlessArch = "x64" | "arm64"; /** First Factorio version with an ARM64 Linux headless build */ export const minimumArm64HeadlessVersion = "2.1.18"; /** - * Download URL of the Linux headless build of a Factorio version + * Check if Factorio publishes a headless build of a version + * + * @param version - Factorio version to check. + * @param platform - Platform of the build. + * @param arch - CPU architecture of the build. + * @returns true if the build can be downloaded. + */ +export function hasFactorioHeadless( + version: FullVersion, + platform: FactorioHeadlessPlatform, + arch: FactorioHeadlessArch, +) { + if (platform !== "linux") { + return false; + } + return arch === "x64" || integerFullVersion(version) >= integerFullVersion(minimumArm64HeadlessVersion); +} + +/** + * Download URL of the headless build of a Factorio version * * @param version - Factorio version to download. + * @param platform - Platform of the build. * @param arch - CPU architecture of the build. - * @returns URL redirecting to the tarball. + * @returns URL redirecting to the archive. + * @throws Error if Factorio has no headless build for the platform and arch. */ -export function factorioHeadlessUrl(version: string, arch: FactorioHeadlessArch = "x64") { +export function factorioHeadlessUrl( + version: FullVersion, + platform: FactorioHeadlessPlatform, + arch: FactorioHeadlessArch, +) { + if (!hasFactorioHeadless(version, platform, arch)) { + throw new Error(`Factorio ${version} has no headless build for ${platform} ${arch}`); + } const build = arch === "arm64" ? "linux-arm64" : "linux64"; return `https://www.factorio.com/get-download/${version}/headless/${build}`; } @@ -78,7 +110,7 @@ function parseFactorioVersions(html: string): ExternalFactorioVersion[] { versions.set(version, { version: version, stable: kind === "stable", - headlessUrl: factorioHeadlessUrl(version), + headlessUrl: factorioHeadlessUrl(version, "linux", "x64"), }); } } diff --git a/test/lib/external/FactorioVersions.test.js b/test/lib/external/FactorioVersions.test.js index 5f15b140..083a9e9a 100644 --- a/test/lib/external/FactorioVersions.test.js +++ b/test/lib/external/FactorioVersions.test.js @@ -1,22 +1,48 @@ import assert from "node:assert/strict"; import { readFile } from "node:fs/promises"; -import { factorioHeadlessUrl, fetchFactorioVersions } from "@clusterio/lib"; +import { factorioHeadlessUrl, fetchFactorioVersions, hasFactorioHeadless } from "@clusterio/lib"; import { slowTest, externalTest } from "../../common.js"; describe("FactorioVersions", function() { + describe("hasFactorioHeadless", function() { + for (const [version, platform, arch, expected] of [ + ["0.17.79", "linux", "x64", true], + ["2.1.17", "linux", "arm64", false], + ["2.1.18", "linux", "arm64", true], + ["2.1.24", "win32", "x64", false], + ["2.1.24", "win32", "arm64", false], + ]) { + it(`should return ${expected} for ${version} ${platform} ${arch}`, function() { + assert.equal(hasFactorioHeadless(version, platform, arch), expected); + }); + } + }); describe("factorioHeadlessUrl", function() { - it("should return the linux64 build by default", function() { + it("should return the linux64 build for x64", function() { assert.equal( - factorioHeadlessUrl("2.1.24"), "https://www.factorio.com/get-download/2.1.24/headless/linux64" + factorioHeadlessUrl("2.1.24", "linux", "x64"), + "https://www.factorio.com/get-download/2.1.24/headless/linux64", ); }); it("should return the linux-arm64 build for arm64", function() { assert.equal( - factorioHeadlessUrl("2.1.24", "arm64"), + factorioHeadlessUrl("2.1.24", "linux", "arm64"), "https://www.factorio.com/get-download/2.1.24/headless/linux-arm64", ); }); + it("should throw for Windows", function() { + assert.throws( + () => factorioHeadlessUrl("2.1.24", "win32", "x64"), + new Error("Factorio 2.1.24 has no headless build for win32 x64"), + ); + }); + it("should throw for linux arm64 before 2.1.18", function() { + assert.throws( + () => factorioHeadlessUrl("2.1.17", "linux", "arm64"), + new Error("Factorio 2.1.17 has no headless build for linux arm64"), + ); + }); }); describe("fetchFactorioVersions", function() { const originalFetch = global.fetch;