style: format code biome
This commit is contained in:
1 parent
8efd032cc6
commit
df38dccbf1
735 files changed
+128321
-120870
No files matched your search
@@ -3,19 +3,21 @@ import { describe, expect, it } from "vitest";
|
||||
import { resolveGamedataFile } from "./asset-paths";
|
||||
|
||||
describe("resolveGamedataFile", () => {
|
||||
it("uses absolute external paths as-is", () => {
|
||||
const absolutePath = path.win32.join(
|
||||
"E:\\",
|
||||
"Users",
|
||||
"simol",
|
||||
"Desktop",
|
||||
"DEV",
|
||||
"Nitro-Files",
|
||||
"nitro-assets",
|
||||
"gamedata",
|
||||
"FigureMap.json",
|
||||
);
|
||||
it("uses absolute external paths as-is", () => {
|
||||
const absolutePath = path.win32.join(
|
||||
"E:\\",
|
||||
"Users",
|
||||
"simol",
|
||||
"Desktop",
|
||||
"DEV",
|
||||
"Nitro-Files",
|
||||
"nitro-assets",
|
||||
"gamedata",
|
||||
"FigureMap.json",
|
||||
);
|
||||
|
||||
expect(resolveGamedataFile(absolutePath, "FigureMap.json")).toBe(absolutePath);
|
||||
});
|
||||
expect(resolveGamedataFile(absolutePath, "FigureMap.json")).toBe(
|
||||
absolutePath,
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -9,7 +9,7 @@ import path from "node:path";
|
||||
// the client's served asset dir — see *_NITRO_DIR consts in the import services.)
|
||||
|
||||
function pub(...segs: string[]): string {
|
||||
return path.join(/*turbopackIgnore: true*/ process.cwd(), "public", ...segs);
|
||||
return path.join(/*turbopackIgnore: true*/ process.cwd(), "public", ...segs);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -19,10 +19,14 @@ function pub(...segs: string[]): string {
|
||||
* Returns the configured path when neither exists (write target for fresh setups).
|
||||
*/
|
||||
export function resolveGamedataFile(url: string, fallbackName: string): string {
|
||||
if (path.win32.isAbsolute(url) || (path.isAbsolute(url) && !url.startsWith("/"))) return url;
|
||||
const configured = pub(url.replace(/^\/+/, ""));
|
||||
if (existsSync(/*turbopackIgnore: true*/ configured)) return configured;
|
||||
const gamedata = pub("Gamedata", "config", fallbackName);
|
||||
if (existsSync(/*turbopackIgnore: true*/ gamedata)) return gamedata;
|
||||
return configured;
|
||||
if (
|
||||
path.win32.isAbsolute(url) ||
|
||||
(path.isAbsolute(url) && !url.startsWith("/"))
|
||||
)
|
||||
return url;
|
||||
const configured = pub(url.replace(/^\/+/, ""));
|
||||
if (existsSync(/*turbopackIgnore: true*/ configured)) return configured;
|
||||
const gamedata = pub("Gamedata", "config", fallbackName);
|
||||
if (existsSync(/*turbopackIgnore: true*/ gamedata)) return gamedata;
|
||||
return configured;
|
||||
}
|
||||
@@ -2,19 +2,23 @@ import { describe, expect, it } from "vitest";
|
||||
import { validatePngBytes, validateSwfBytes } from "./download";
|
||||
|
||||
describe("import/core/download validators", () => {
|
||||
it("accepts FWS/CWS/ZWS swf magic", () => {
|
||||
expect(validateSwfBytes(Buffer.from("FWS\x06\x00\x00\x00\x00"))).toBe(true);
|
||||
expect(validateSwfBytes(Buffer.from("CWS\x06\x00\x00\x00\x00"))).toBe(true);
|
||||
expect(validateSwfBytes(Buffer.from("ZWS\x06\x00\x00\x00\x00"))).toBe(true);
|
||||
});
|
||||
it("accepts FWS/CWS/ZWS swf magic", () => {
|
||||
expect(validateSwfBytes(Buffer.from("FWS\x06\x00\x00\x00\x00"))).toBe(true);
|
||||
expect(validateSwfBytes(Buffer.from("CWS\x06\x00\x00\x00\x00"))).toBe(true);
|
||||
expect(validateSwfBytes(Buffer.from("ZWS\x06\x00\x00\x00\x00"))).toBe(true);
|
||||
});
|
||||
|
||||
it("rejects non-swf and too-short buffers", () => {
|
||||
expect(validateSwfBytes(Buffer.from("PNG\x00\x00\x00\x00\x00"))).toBe(false);
|
||||
expect(validateSwfBytes(Buffer.from("FW"))).toBe(false);
|
||||
});
|
||||
it("rejects non-swf and too-short buffers", () => {
|
||||
expect(validateSwfBytes(Buffer.from("PNG\x00\x00\x00\x00\x00"))).toBe(
|
||||
false,
|
||||
);
|
||||
expect(validateSwfBytes(Buffer.from("FW"))).toBe(false);
|
||||
});
|
||||
|
||||
it("accepts png magic and rejects others", () => {
|
||||
expect(validatePngBytes(Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]))).toBe(true);
|
||||
expect(validatePngBytes(Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]))).toBe(false);
|
||||
});
|
||||
it("accepts png magic and rejects others", () => {
|
||||
expect(
|
||||
validatePngBytes(Buffer.from([137, 80, 78, 71, 13, 10, 26, 10])),
|
||||
).toBe(true);
|
||||
expect(validatePngBytes(Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]))).toBe(false);
|
||||
});
|
||||
});
|
||||
File diff suppressed because it is too large.
Load diff
@@ -2,42 +2,51 @@ import { promises as fs } from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { readGamedataJson, withGamedataLock, writeGamedataJsonAtomic } from "./gamedata-json";
|
||||
import {
|
||||
readGamedataJson,
|
||||
withGamedataLock,
|
||||
writeGamedataJsonAtomic,
|
||||
} from "./gamedata-json";
|
||||
|
||||
let dir: string;
|
||||
let file: string;
|
||||
|
||||
beforeEach(async () => {
|
||||
dir = await fs.mkdtemp(path.join(os.tmpdir(), "gamedata-"));
|
||||
file = path.join(dir, "EffectMap.json");
|
||||
await fs.writeFile(file, JSON.stringify({ effects: [{ id: "1", lib: "A" }] }));
|
||||
dir = await fs.mkdtemp(path.join(os.tmpdir(), "gamedata-"));
|
||||
file = path.join(dir, "EffectMap.json");
|
||||
await fs.writeFile(
|
||||
file,
|
||||
JSON.stringify({ effects: [{ id: "1", lib: "A" }] }),
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await fs.rm(dir, { recursive: true, force: true });
|
||||
await fs.rm(dir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
describe("import/core/gamedata-json", () => {
|
||||
it("reads parsed JSON", async () => {
|
||||
const data = await readGamedataJson<{ effects: unknown[] }>(file);
|
||||
expect(data.effects).toHaveLength(1);
|
||||
});
|
||||
it("reads parsed JSON", async () => {
|
||||
const data = await readGamedataJson<{ effects: unknown[] }>(file);
|
||||
expect(data.effects).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("writes atomically (round-trips)", async () => {
|
||||
await writeGamedataJsonAtomic(file, { effects: [{ id: "9", lib: "Z" }] });
|
||||
const data = await readGamedataJson<{ effects: Array<{ id: string }> }>(file);
|
||||
expect(data.effects[0].id).toBe("9");
|
||||
});
|
||||
it("writes atomically (round-trips)", async () => {
|
||||
await writeGamedataJsonAtomic(file, { effects: [{ id: "9", lib: "Z" }] });
|
||||
const data = await readGamedataJson<{ effects: Array<{ id: string }> }>(
|
||||
file,
|
||||
);
|
||||
expect(data.effects[0].id).toBe("9");
|
||||
});
|
||||
|
||||
it("serializes concurrent writers (no lost update)", async () => {
|
||||
const bump = (n: number) =>
|
||||
withGamedataLock(file, async () => {
|
||||
const d = await readGamedataJson<{ effects: unknown[] }>(file);
|
||||
d.effects.push({ id: String(n), lib: `L${n}` });
|
||||
await writeGamedataJsonAtomic(file, d);
|
||||
});
|
||||
await Promise.all([bump(2), bump(3), bump(4)]);
|
||||
const d = await readGamedataJson<{ effects: unknown[] }>(file);
|
||||
expect(d.effects).toHaveLength(4); // 1 seed + 3 appended, none lost
|
||||
});
|
||||
it("serializes concurrent writers (no lost update)", async () => {
|
||||
const bump = (n: number) =>
|
||||
withGamedataLock(file, async () => {
|
||||
const d = await readGamedataJson<{ effects: unknown[] }>(file);
|
||||
d.effects.push({ id: String(n), lib: `L${n}` });
|
||||
await writeGamedataJsonAtomic(file, d);
|
||||
});
|
||||
await Promise.all([bump(2), bump(3), bump(4)]);
|
||||
const d = await readGamedataJson<{ effects: unknown[] }>(file);
|
||||
expect(d.effects).toHaveLength(4); // 1 seed + 3 appended, none lost
|
||||
});
|
||||
});
|
||||
File diff suppressed because it is too large.
Load diff
@@ -2,30 +2,39 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { __clearGordonCache, resolveGordonBuildUrl } from "./gordon";
|
||||
|
||||
beforeEach(() => {
|
||||
__clearGordonCache();
|
||||
vi.restoreAllMocks();
|
||||
__clearGordonCache();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
afterEach(() => vi.restoreAllMocks());
|
||||
|
||||
describe("import/core/gordon", () => {
|
||||
it("parses flash.client.url from external_variables and normalizes", async () => {
|
||||
vi.spyOn(globalThis, "fetch").mockResolvedValue(
|
||||
new Response("flash.client.url=//images.habbo.com/gordon/PRODUCTION-XYZ/\n", { status: 200 }),
|
||||
);
|
||||
expect(await resolveGordonBuildUrl()).toBe("https://images.habbo.com/gordon/PRODUCTION-XYZ/");
|
||||
});
|
||||
it("parses flash.client.url from external_variables and normalizes", async () => {
|
||||
vi.spyOn(globalThis, "fetch").mockResolvedValue(
|
||||
new Response(
|
||||
"flash.client.url=//images.habbo.com/gordon/PRODUCTION-XYZ/\n",
|
||||
{ status: 200 },
|
||||
),
|
||||
);
|
||||
expect(await resolveGordonBuildUrl()).toBe(
|
||||
"https://images.habbo.com/gordon/PRODUCTION-XYZ/",
|
||||
);
|
||||
});
|
||||
|
||||
it("caches the resolved build (second call does not refetch)", async () => {
|
||||
const spy = vi
|
||||
.spyOn(globalThis, "fetch")
|
||||
.mockResolvedValue(new Response("flash.client.url=//x/gordon/B/\n", { status: 200 }));
|
||||
await resolveGordonBuildUrl();
|
||||
await resolveGordonBuildUrl();
|
||||
expect(spy).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
it("caches the resolved build (second call does not refetch)", async () => {
|
||||
const spy = vi
|
||||
.spyOn(globalThis, "fetch")
|
||||
.mockResolvedValue(
|
||||
new Response("flash.client.url=//x/gordon/B/\n", { status: 200 }),
|
||||
);
|
||||
await resolveGordonBuildUrl();
|
||||
await resolveGordonBuildUrl();
|
||||
expect(spy).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("throws when flash.client.url is absent", async () => {
|
||||
vi.spyOn(globalThis, "fetch").mockResolvedValue(new Response("other=1\n", { status: 200 }));
|
||||
await expect(resolveGordonBuildUrl()).rejects.toThrow();
|
||||
});
|
||||
it("throws when flash.client.url is absent", async () => {
|
||||
vi.spyOn(globalThis, "fetch").mockResolvedValue(
|
||||
new Response("other=1\n", { status: 200 }),
|
||||
);
|
||||
await expect(resolveGordonBuildUrl()).rejects.toThrow();
|
||||
});
|
||||
});
|
||||
@@ -1,22 +1,30 @@
|
||||
const EXTERNAL_VARIABLES_URL = "https://www.habbo.com/gamedata/external_variables/1";
|
||||
const EXTERNAL_VARIABLES_URL =
|
||||
"https://www.habbo.com/gamedata/external_variables/1";
|
||||
const CACHE_TTL = 30 * 60 * 1000;
|
||||
|
||||
let gordonCache: { url: string; ts: number } | null = null;
|
||||
|
||||
export function __clearGordonCache(): void {
|
||||
gordonCache = null;
|
||||
gordonCache = null;
|
||||
}
|
||||
|
||||
/** Resolve the current Habbo "gordon" asset build URL (trailing slash) from external_variables. */
|
||||
export async function resolveGordonBuildUrl(): Promise<string> {
|
||||
const now = Date.now();
|
||||
if (gordonCache && now - gordonCache.ts < CACHE_TTL) return gordonCache.url;
|
||||
const res = await fetch(EXTERNAL_VARIABLES_URL, { signal: AbortSignal.timeout(20000) });
|
||||
if (!res.ok) throw new Error(`external_variables fetch failed: ${res.status}`);
|
||||
const text = await res.text();
|
||||
const line = /^flash\.client\.url=(.+)$/m.exec(text)?.[1]?.trim();
|
||||
if (!line) throw new Error("flash.client.url not found in external_variables");
|
||||
const normalized = (line.startsWith("//") ? `https:${line}` : line).replace(/\/?$/, "/");
|
||||
gordonCache = { url: normalized, ts: now };
|
||||
return normalized;
|
||||
const now = Date.now();
|
||||
if (gordonCache && now - gordonCache.ts < CACHE_TTL) return gordonCache.url;
|
||||
const res = await fetch(EXTERNAL_VARIABLES_URL, {
|
||||
signal: AbortSignal.timeout(20000),
|
||||
});
|
||||
if (!res.ok)
|
||||
throw new Error(`external_variables fetch failed: ${res.status}`);
|
||||
const text = await res.text();
|
||||
const line = /^flash\.client\.url=(.+)$/m.exec(text)?.[1]?.trim();
|
||||
if (!line)
|
||||
throw new Error("flash.client.url not found in external_variables");
|
||||
const normalized = (line.startsWith("//") ? `https:${line}` : line).replace(
|
||||
/\/?$/,
|
||||
"/",
|
||||
);
|
||||
gordonCache = { url: normalized, ts: now };
|
||||
return normalized;
|
||||
}
|
||||
@@ -2,27 +2,42 @@ import { describe, expect, it } from "vitest";
|
||||
import { runSseBatch } from "./sse-batch";
|
||||
|
||||
async function collect(res: Response): Promise<Array<Record<string, unknown>>> {
|
||||
const text = await res.text();
|
||||
return text
|
||||
.split("\n\n")
|
||||
.filter((l) => l.startsWith("data: "))
|
||||
.map((l) => JSON.parse(l.slice(6)));
|
||||
const text = await res.text();
|
||||
return text
|
||||
.split("\n\n")
|
||||
.filter((l) => l.startsWith("data: "))
|
||||
.map((l) => JSON.parse(l.slice(6)));
|
||||
}
|
||||
|
||||
describe("import/core/sse-batch", () => {
|
||||
it("emits batch_start, per-item, and batch_complete with correct counts", async () => {
|
||||
const res = runSseBatch({
|
||||
items: ["a", "b", "c"],
|
||||
concurrency: 2,
|
||||
worker: async (item) => ({ ok: item !== "b", error: item === "b" ? "boom" : undefined }),
|
||||
labelOf: (item) => item,
|
||||
});
|
||||
const events = await collect(res);
|
||||
expect(events[0]).toMatchObject({ type: "batch_start", total: 3, concurrency: 2 });
|
||||
const done = events.filter((e) => e.type === "item_progress" && e.status === "done");
|
||||
const failed = events.filter((e) => e.type === "item_progress" && e.status === "failed");
|
||||
expect(done).toHaveLength(2);
|
||||
expect(failed).toHaveLength(1);
|
||||
expect(events.at(-1)).toMatchObject({ type: "batch_complete", succeeded: 2, failed: 1 });
|
||||
});
|
||||
it("emits batch_start, per-item, and batch_complete with correct counts", async () => {
|
||||
const res = runSseBatch({
|
||||
items: ["a", "b", "c"],
|
||||
concurrency: 2,
|
||||
worker: async (item) => ({
|
||||
ok: item !== "b",
|
||||
error: item === "b" ? "boom" : undefined,
|
||||
}),
|
||||
labelOf: (item) => item,
|
||||
});
|
||||
const events = await collect(res);
|
||||
expect(events[0]).toMatchObject({
|
||||
type: "batch_start",
|
||||
total: 3,
|
||||
concurrency: 2,
|
||||
});
|
||||
const done = events.filter(
|
||||
(e) => e.type === "item_progress" && e.status === "done",
|
||||
);
|
||||
const failed = events.filter(
|
||||
(e) => e.type === "item_progress" && e.status === "failed",
|
||||
);
|
||||
expect(done).toHaveLength(2);
|
||||
expect(failed).toHaveLength(1);
|
||||
expect(events.at(-1)).toMatchObject({
|
||||
type: "batch_complete",
|
||||
succeeded: 2,
|
||||
failed: 1,
|
||||
});
|
||||
});
|
||||
});
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user