feat: add a function to handle log4j fix and its test, add it to arguments.ts and finish arguments.test.ts

This commit is contained in:
HerozDotExe 2025-10-30 23:30:31 +01:00
parent 976838bd3c
commit 71ccc0cbeb
5 changed files with 82 additions and 20 deletions

View file

@ -4,6 +4,7 @@ import { Argument, Auth, PoolFile, Version } from "../utils/types";
import { getJavaExecutable } from "./java";
import { getLibraries } from "./libraries";
import { version as packageVersion } from "../../package.json";
import { getArgument } from "./log4j";
function fillArguments(
arg: string,
@ -82,7 +83,7 @@ function generateClassPaths(versionManifest: Version, librariesRoot: string) {
return libs.join(path.delimiter);
}
export function generateLaunchArguments(
export async function generateLaunchArguments(
versionManifest: Version,
javaRoot: string,
gameRoot: string,
@ -115,5 +116,7 @@ export function generateLaunchArguments(
p(game, arg);
}
return `${getJavaExecutable(javaRoot)} ${jvm.join(" ")}${options.customJvm === "" ? "" : ` ${options.customJvm}`} -Xms${options.minRam} -Xmx${options.maxRam} -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:G1NewSizePercent=20 -XX:G1ReservePercent=20 -XX:MaxGCPauseMillis=50 -XX:G1HeapRegionSize=32M ${versionManifest.mainClass} ${game.join(" ")}`;
const log4j = await getArgument(versionManifest, gameRoot)
return `${getJavaExecutable(javaRoot)} ${jvm.join(" ")}${options.customJvm === "" ? "" : ` ${options.customJvm}`} -Xms${options.minRam} -Xmx${options.maxRam} -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:G1NewSizePercent=20 -XX:G1ReservePercent=20 -XX:MaxGCPauseMillis=50 -XX:G1HeapRegionSize=32M ${log4j} ${versionManifest.mainClass} ${game.join(" ")}`;
}

View file

@ -3,4 +3,5 @@ export * as version from "./version";
export * as natives from "./natives";
export * as libraries from "./libraries";
export * as assets from "./assets";
export * as arguments from "./arguments"
export * as arguments from "./arguments"
export * as log4j from "./log4j"

34
src/core/log4j.ts Normal file
View file

@ -0,0 +1,34 @@
import path from "path";
import { downloadFile } from "../utils/fetch";
import { Version } from "../utils/types";
import { exists } from "../utils/fs";
import { hash } from "crypto";
import fs from "fs/promises";
export async function getArgument(
versionManifest: Version,
destination: string,
) {
const xmlDestination = path.join(
destination,
versionManifest.logging.client.file.id,
);
if (!(await exists(xmlDestination))) {
await downloadFile({
url: versionManifest.logging.client.file.url,
path: xmlDestination,
});
if (
hash("sha1", await fs.readFile(xmlDestination, {encoding: "utf-8"})) !==
versionManifest.logging.client.file.sha1
) {
throw new Error("Wrong hash for log4j's xml config");
}
}
return versionManifest.logging.client.argument.replace(
"${path}",
xmlDestination,
);
}

File diff suppressed because one or more lines are too long

34
tests/log4j.test.ts Normal file
View file

@ -0,0 +1,34 @@
import { expect, test, vi } from "vitest";
import * as nlk from "../dist/index.js";
import path from "path";
import { hash } from "crypto";
import fs from "fs/promises";
const root = path.join(import.meta.dirname, "temp");
const xmlFile = path.join(root, "client-1.21.2.xml");
try {
await fs.rm(xmlFile, { recursive: true });
} catch {
// first time the test is runned
}
// mock os to linux for testing
vi.stubGlobal("process", { platform: "linux" });
test(
"download xml file and return correct arguments",
{ timeout: 10000 },
async () => {
const versionManifest = await nlk.core.version.getVersionManifest("1.21.8");
const arg = await nlk.core.log4j.getArgument(versionManifest, root);
expect(arg, "correct arg returned").toBe(
`-Dlog4j.configurationFile=${xmlFile}`,
);
expect(
hash("sha1", await fs.readFile(xmlFile, { encoding: "utf-8" })),
"correct hash",
).toBe("39384bd14c0606d812afec88d8aff595b2587dd9");
},
);