mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-16 15:30:41 +09:00
Add Windows headless update downloads
This commit is contained in:
1 parent
bbd3cc7fdb
commit
5b4621a7de
4 files changed
+150
-53
No files matched your search
+102
-36
@@ -19,7 +19,7 @@ const execFileAsync = util.promisify(child_process.execFile);
|
||||
* @param factorioDir - Path factorio dir containing data/changelog.txt.
|
||||
*/
|
||||
async function getFactorioVersion(factorioDir: string) {
|
||||
let changelog;
|
||||
let changelog: string;
|
||||
const changelogPath = path.join(factorioDir, "data", "changelog.txt");
|
||||
try {
|
||||
changelog = await fs.readFile(changelogPath, "utf-8");
|
||||
@@ -106,34 +106,40 @@ async function listFactorioVersions(factorioDir: string) {
|
||||
* @throws Network error if status of request is not ok
|
||||
*/
|
||||
async function downloadAndExtractZip(url: string, targetDir: string) {
|
||||
const res = await fetch(url);
|
||||
if (!res.ok) {
|
||||
throw new Error(`Failed to fetch ${url}: ${res.status} ${res.statusText}`);
|
||||
}
|
||||
const res = await fetchDownload(url);
|
||||
|
||||
// Load the ZIP with JSZip
|
||||
// Load the ZIP with JSZip
|
||||
const buffer = Buffer.from(await res.arrayBuffer());
|
||||
const zip = await JSZip.loadAsync(buffer);
|
||||
const tmpTargetDir = `${targetDir}.tmp-extract`;
|
||||
const stripRootDir = hasSingleArchiveRoot(Object.values(zip.files));
|
||||
|
||||
// Extract each file/folder
|
||||
await fs.ensureDir(targetDir);
|
||||
await Promise.all(
|
||||
Object.values(zip.files).map(async (entry) => {
|
||||
const parts = entry.name.split("/").filter(Boolean);
|
||||
const strippedPath = parts.slice(1).join("/");
|
||||
if (!strippedPath) {
|
||||
return; // This copies the behaviour of tar --strip-components 1
|
||||
}
|
||||
await fs.remove(tmpTargetDir);
|
||||
await fs.ensureDir(tmpTargetDir);
|
||||
try {
|
||||
await Promise.all(
|
||||
Object.values(zip.files).map(async (entry) => {
|
||||
const parts = entry.name.split("/").filter(Boolean);
|
||||
const strippedParts = stripRootDir ? parts.slice(1) : parts;
|
||||
if (!strippedParts.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
const entryPath = path.join(targetDir, strippedPath);
|
||||
if (entry.dir) {
|
||||
await fs.ensureDir(entryPath);
|
||||
} else {
|
||||
await fs.ensureDir(path.dirname(entryPath));
|
||||
await fs.writeFile(entryPath, await entry.async("nodebuffer"));
|
||||
}
|
||||
})
|
||||
);
|
||||
const entryPath = validatedExtractPath(tmpTargetDir, strippedParts);
|
||||
if (entry.dir) {
|
||||
await fs.ensureDir(entryPath);
|
||||
} else {
|
||||
await fs.ensureDir(path.dirname(entryPath));
|
||||
await fs.writeFile(entryPath, await entry.async("nodebuffer"));
|
||||
}
|
||||
})
|
||||
);
|
||||
await fs.remove(targetDir);
|
||||
await fs.rename(tmpTargetDir, targetDir);
|
||||
} catch (err) {
|
||||
await fs.remove(tmpTargetDir);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -144,10 +150,7 @@ async function downloadAndExtractZip(url: string, targetDir: string) {
|
||||
* @throws Network error if status of request is not ok
|
||||
*/
|
||||
async function downloadAndExtractTar(url: string, targetDir: string) {
|
||||
const res = await fetch(url);
|
||||
if (!res.ok) {
|
||||
throw new Error(`Failed to fetch ${url}: ${res.status} ${res.statusText}`);
|
||||
}
|
||||
const res = await fetchDownload(url);
|
||||
|
||||
// Write the buffer to a temp file
|
||||
const tmpFilePath = `${targetDir}.tmp`;
|
||||
@@ -157,7 +160,9 @@ async function downloadAndExtractTar(url: string, targetDir: string) {
|
||||
|
||||
// Execute tar CLI to extract
|
||||
try {
|
||||
await execFileAsync("tar", ["-xf", tmpFilePath, "-C", targetDir, "--strip-components", "1"]);
|
||||
await execFileAsync("tar", [
|
||||
"-xf", tarPath(tmpFilePath), "-C", tarPath(targetDir), "--strip-components", "1",
|
||||
]);
|
||||
} catch (err: any) {
|
||||
if (err.code === "ENOENT") {
|
||||
throw new Error("error executing tar command, do you have 'xz-utils' installed?");
|
||||
@@ -169,6 +174,57 @@ async function downloadAndExtractTar(url: string, targetDir: string) {
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchDownload(url: string) {
|
||||
const res = await fetch(url);
|
||||
if (res.status === 202) {
|
||||
let detail = "";
|
||||
try {
|
||||
detail = await res.text();
|
||||
} catch {
|
||||
}
|
||||
throw new Error(`Download is not ready yet for ${url}${detail ? `: ${detail}` : ""}`);
|
||||
}
|
||||
if (!res.ok) {
|
||||
throw new Error(`Failed to fetch ${url}: ${res.status} ${res.statusText}`);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
function tarPath(filePath: string) {
|
||||
if (process.platform === "win32") {
|
||||
return filePath.replace(/\\/g, "/");
|
||||
}
|
||||
return filePath;
|
||||
}
|
||||
|
||||
function hasSingleArchiveRoot(entries: JSZip.JSZipObject[]) {
|
||||
const rootNames = new Set(
|
||||
entries
|
||||
.map(entry => entry.name.split("/").filter(Boolean)[0])
|
||||
.filter((value): value is string => Boolean(value))
|
||||
);
|
||||
return rootNames.size === 1;
|
||||
}
|
||||
|
||||
function validatedExtractPath(rootDir: string, parts: string[]) {
|
||||
const entryPath = path.resolve(rootDir, ...parts);
|
||||
const relative = path.relative(rootDir, entryPath);
|
||||
if (relative.startsWith("..") || path.isAbsolute(relative)) {
|
||||
throw new Error(`Refusing to extract unsafe ZIP entry ${parts.join("/")}`);
|
||||
}
|
||||
return entryPath;
|
||||
}
|
||||
|
||||
function getHeadlessDownload(version: lib.ExternalFactorioVersion) {
|
||||
if (process.platform === "linux") {
|
||||
return { url: version.headlessUrl, extract: downloadAndExtractTar };
|
||||
}
|
||||
if (process.platform === "win32" && version.windowsHeadlessUrl) {
|
||||
return { url: version.windowsHeadlessUrl, extract: downloadAndExtractZip };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find factorio data directory of the given version
|
||||
*
|
||||
@@ -749,7 +805,7 @@ export class FactorioServer extends events.EventEmitter<FactorioServerEvents> {
|
||||
;
|
||||
|
||||
let type = line.subarray(channelEnd + 1, channelEnd + 2).toString("utf-8");
|
||||
let content;
|
||||
let content: unknown;
|
||||
if (type === "j") {
|
||||
try {
|
||||
content = JSON.parse(line.subarray(channelEnd + 2).toString("utf-8"));
|
||||
@@ -905,7 +961,7 @@ export class FactorioServer extends events.EventEmitter<FactorioServerEvents> {
|
||||
return;
|
||||
}
|
||||
|
||||
let newWhitelistJson;
|
||||
let newWhitelistJson: unknown;
|
||||
try {
|
||||
newWhitelistJson = await fs.readJSON(filePath);
|
||||
} catch (err: any) {
|
||||
@@ -985,8 +1041,8 @@ export class FactorioServer extends events.EventEmitter<FactorioServerEvents> {
|
||||
const installedVersions = await listFactorioVersions(this._factorioDir);
|
||||
if (!installedVersions.versions.has(latestVersion.version)) {
|
||||
const v = latestVersion.version;
|
||||
// Can't download if not linux or this is a direct install
|
||||
if (installedVersions.direct || process.platform !== "linux") {
|
||||
const download = getHeadlessDownload(latestVersion);
|
||||
if (installedVersions.direct || !download) {
|
||||
this._logger.info(`A newer version of factorio is available (${v}) but must be manually downloaded`);
|
||||
return;
|
||||
}
|
||||
@@ -994,8 +1050,18 @@ export class FactorioServer extends events.EventEmitter<FactorioServerEvents> {
|
||||
// Download the newer version
|
||||
this._logger.info(`A newer version of factorio is available (${v}), starting download...`);
|
||||
const dst = path.join(this._factorioDir, latestVersion.version);
|
||||
await downloadAndExtractTar(latestVersion.headlessUrl, dst);
|
||||
this._logger.info(`Downloaded factorio version ${v}`);
|
||||
try {
|
||||
await download.extract(download.url, dst);
|
||||
this._logger.info(`Downloaded factorio version ${v}`);
|
||||
} catch (err: unknown) {
|
||||
if (installedVersions.versions.size > 0) {
|
||||
this._logger.warn(
|
||||
`Failed to download factorio version ${v}, continuing with installed versions: ${err}`
|
||||
);
|
||||
return;
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1051,7 +1117,7 @@ export class FactorioServer extends events.EventEmitter<FactorioServerEvents> {
|
||||
}
|
||||
|
||||
} else if (this._unexpected.length === 0) {
|
||||
let msg;
|
||||
let msg: string;
|
||||
if (code !== null) {
|
||||
msg = `Factorio server unexpectedly shut down with code ${code}`;
|
||||
} else {
|
||||
|
||||
+9
-5
@@ -2,6 +2,7 @@ import { Static, Type } from "@sinclair/typebox";
|
||||
import { FullVersionSchema, integerFullVersion, isFullVersion } from "../data/version";
|
||||
|
||||
const ARCHIVE_URL = "https://factorio.com/download/archive/";
|
||||
const WINDOWS_HEADLESS_BASE_URL = "https://factorio-repackager.fly.dev/get-download";
|
||||
|
||||
/**
|
||||
* Represents the parsed result of a factorio version
|
||||
@@ -10,9 +11,10 @@ export const ExternalFactorioVersionSchema = Type.Object({
|
||||
stable: Type.Boolean(),
|
||||
version: FullVersionSchema,
|
||||
headlessUrl: Type.String(),
|
||||
windowsHeadlessUrl: Type.Optional(Type.String()),
|
||||
});
|
||||
|
||||
export type ExternalFactorioVersion = Static<typeof ExternalFactorioVersionSchema>
|
||||
export type ExternalFactorioVersion = Static<typeof ExternalFactorioVersionSchema>;
|
||||
|
||||
/**
|
||||
* Fetch all factorio versions that support headless
|
||||
@@ -21,7 +23,7 @@ export type ExternalFactorioVersion = Static<typeof ExternalFactorioVersionSchem
|
||||
* @throws Network errors and non-ok status responses
|
||||
*/
|
||||
export async function fetchFactorioVersions(): Promise<ExternalFactorioVersion[]> {
|
||||
let res;
|
||||
let res: Response;
|
||||
|
||||
try {
|
||||
res = await fetch(ARCHIVE_URL);
|
||||
@@ -48,11 +50,11 @@ function parseFactorioVersions(html: string): ExternalFactorioVersion[] {
|
||||
|
||||
// Match <a class=" slot-button-inline version-button-stable " href="/download/archive/2.0.72">2.0.72</a>
|
||||
// eslint-disable-next-line max-len
|
||||
const anchorRegex = /<a[^>]*class="[^"]*version-button-(?<kind>stable|experimental)[^"]*"[^>]*href="[^"]*\/download\/archive\/(?<version>\d+\.\d+.\d+)"[^>]*>/g;
|
||||
const anchorRegex = /<a[^>]*class="[^"]*version-button-(?<kind>stable|experimental)[^"]*"[^>]*href="[^"]*\/download\/archive\/(?<version>\d+\.\d+\.\d+)"[^>]*>/g;
|
||||
|
||||
let match: RegExpExecArray | null;
|
||||
let match = anchorRegex.exec(html);
|
||||
const minimumMultiplayerVersion = integerFullVersion("0.12.35");
|
||||
while ((match = anchorRegex.exec(html)) !== null) {
|
||||
while (match !== null) {
|
||||
const kind = match.groups?.kind;
|
||||
const version = match.groups?.version;
|
||||
|
||||
@@ -61,8 +63,10 @@ function parseFactorioVersions(html: string): ExternalFactorioVersion[] {
|
||||
version: version,
|
||||
stable: kind === "stable",
|
||||
headlessUrl: `https://www.factorio.com/get-download/${version}/headless/linux64`,
|
||||
windowsHeadlessUrl: `${WINDOWS_HEADLESS_BASE_URL}/${version}/headless/win64`,
|
||||
});
|
||||
}
|
||||
match = anchorRegex.exec(html);
|
||||
}
|
||||
|
||||
return [...versions.values()];
|
||||
|
||||
+36
-12
@@ -364,10 +364,12 @@ describe("host/server", function() {
|
||||
stable: true,
|
||||
version: "0.2.1",
|
||||
headlessUrl: "test1",
|
||||
windowsHeadlessUrl: "win-test1",
|
||||
}, {
|
||||
stable: false,
|
||||
version: "0.2.0",
|
||||
headlessUrl: "test2",
|
||||
windowsHeadlessUrl: "win-test2",
|
||||
}]);
|
||||
|
||||
assert.equal(fetchCalledWith, null);
|
||||
@@ -380,10 +382,12 @@ describe("host/server", function() {
|
||||
stable: true,
|
||||
version: "0.1.1",
|
||||
headlessUrl: "test1",
|
||||
windowsHeadlessUrl: "win-test1",
|
||||
}, {
|
||||
stable: false,
|
||||
version: "0.1.0",
|
||||
headlessUrl: "test2",
|
||||
windowsHeadlessUrl: "win-test2",
|
||||
}]);
|
||||
|
||||
assert.equal(fetchCalledWith, null);
|
||||
@@ -395,21 +399,28 @@ describe("host/server", function() {
|
||||
stable: true,
|
||||
version: "0.1.5",
|
||||
headlessUrl: "test1",
|
||||
windowsHeadlessUrl: "win-test1",
|
||||
}, {
|
||||
stable: true,
|
||||
version: "0.1.1",
|
||||
headlessUrl: "test1",
|
||||
windowsHeadlessUrl: "win-test1",
|
||||
}, {
|
||||
stable: false,
|
||||
version: "0.1.0",
|
||||
headlessUrl: "test2",
|
||||
windowsHeadlessUrl: "win-test2",
|
||||
}]);
|
||||
|
||||
assert.equal(fetchCalledWith, null);
|
||||
});
|
||||
it("should do nothing when on windows", async function() {
|
||||
let logLine = null;
|
||||
server._logger = { info: line => { logLine = line; } };
|
||||
it("should do attempt to download on windows", async function() {
|
||||
let infoLine = null;
|
||||
let warnLine = null;
|
||||
server._logger = {
|
||||
info: line => { infoLine = line; },
|
||||
warn: line => { warnLine = line; },
|
||||
};
|
||||
Object.defineProperty(process, "platform", { value: "win32" });
|
||||
|
||||
server._factorioDir = path.join("test", "file", "factorio");
|
||||
@@ -418,44 +429,56 @@ describe("host/server", function() {
|
||||
stable: true,
|
||||
version: "0.1.5",
|
||||
headlessUrl: "test1",
|
||||
windowsHeadlessUrl: "win-test1",
|
||||
}, {
|
||||
stable: true,
|
||||
version: "0.1.1",
|
||||
headlessUrl: "test2",
|
||||
windowsHeadlessUrl: "win-test2",
|
||||
}, {
|
||||
stable: false,
|
||||
version: "0.1.0",
|
||||
headlessUrl: "test3",
|
||||
windowsHeadlessUrl: "win-test3",
|
||||
}]);
|
||||
|
||||
assert.equal(fetchCalledWith, null);
|
||||
assert.ok(logLine !== null);
|
||||
assert.ok(logLine.endsWith("but must be manually downloaded"));
|
||||
assert.equal(fetchCalledWith, "win-test1");
|
||||
assert.ok(infoLine !== null);
|
||||
assert.ok(infoLine.endsWith("starting download..."));
|
||||
assert.ok(warnLine?.includes("continuing with installed versions"));
|
||||
});
|
||||
it("should do attempt to download on linux", async function() {
|
||||
let logLine = null;
|
||||
server._logger = { info: line => { logLine = line; } };
|
||||
let infoLine = null;
|
||||
let warnLine = null;
|
||||
server._logger = {
|
||||
info: line => { infoLine = line; },
|
||||
warn: line => { warnLine = line; },
|
||||
};
|
||||
Object.defineProperty(process, "platform", { value: "linux" });
|
||||
|
||||
server._factorioDir = path.join("test", "file", "factorio");
|
||||
server._targetVersion = target === "0.1.1" ? "0.1.5" : target;
|
||||
await assert.rejects(server.checkForUpdates([{
|
||||
await server.checkForUpdates([{
|
||||
stable: true,
|
||||
version: "0.1.5",
|
||||
headlessUrl: "test1",
|
||||
windowsHeadlessUrl: "win-test1",
|
||||
}, {
|
||||
stable: true,
|
||||
version: "0.1.1",
|
||||
headlessUrl: "test2",
|
||||
windowsHeadlessUrl: "win-test2",
|
||||
}, {
|
||||
stable: false,
|
||||
version: "0.1.0",
|
||||
headlessUrl: "test3",
|
||||
}]), new Error("Failed to fetch test1: -1 Fetch called"));
|
||||
windowsHeadlessUrl: "win-test3",
|
||||
}]);
|
||||
|
||||
assert.equal(fetchCalledWith, "test1");
|
||||
assert.ok(logLine !== null);
|
||||
assert.ok(logLine.endsWith("starting download..."));
|
||||
assert.ok(infoLine !== null);
|
||||
assert.ok(infoLine.endsWith("starting download..."));
|
||||
assert.ok(warnLine?.includes("continuing with installed versions"));
|
||||
});
|
||||
});
|
||||
/* eslint-enable no-loop-func */
|
||||
@@ -474,6 +497,7 @@ describe("host/server", function() {
|
||||
stable: true,
|
||||
version: "2.0.73",
|
||||
headlessUrl: "https://www.factorio.com/get-download/2.0.73/headless/linux64",
|
||||
windowsHeadlessUrl: "https://factorio-repackager.fly.dev/get-download/2.0.73/headless/win64",
|
||||
}]);
|
||||
});
|
||||
});
|
||||
|
||||
+3
@@ -55,11 +55,13 @@ describe("FactorioVersions", function() {
|
||||
stable: false,
|
||||
version: "2.0.73",
|
||||
headlessUrl: "https://www.factorio.com/get-download/2.0.73/headless/linux64",
|
||||
windowsHeadlessUrl: "https://factorio-repackager.fly.dev/get-download/2.0.73/headless/win64",
|
||||
},
|
||||
{
|
||||
stable: true,
|
||||
version: "2.0.72",
|
||||
headlessUrl: "https://www.factorio.com/get-download/2.0.72/headless/linux64",
|
||||
windowsHeadlessUrl: "https://factorio-repackager.fly.dev/get-download/2.0.72/headless/win64",
|
||||
},
|
||||
]);
|
||||
});
|
||||
@@ -91,6 +93,7 @@ describe("FactorioVersions", function() {
|
||||
stable: true,
|
||||
version: "2.0.73",
|
||||
headlessUrl: "https://www.factorio.com/get-download/2.0.73/headless/linux64",
|
||||
windowsHeadlessUrl: "https://factorio-repackager.fly.dev/get-download/2.0.73/headless/win64",
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user