Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
265 changes: 265 additions & 0 deletions scripts/game-feed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
import { randomUUID } from "node:crypto";
import {
lstat,
mkdtemp,
mkdir,
readdir,
rename,
rm,
writeFile,
} from "node:fs/promises";
import path from "node:path";

export interface GameRecord {
id: string;
name: string;
slug: string;
description: string;
instructions: string;
category: string;
tags: string[];
url: string;
embedUrl: string;
image: string;
width: string;
height: string;
}

export interface NormalizeResult {
games: GameRecord[];
skipped: Array<{ index: number; reason: string }>;
}

export interface FetchOptions {
attempts?: number;
timeoutMs?: number;
baseDelayMs?: number;
maxDelayMs?: number;
}

function asText(value: unknown): string {
return typeof value === "string" || typeof value === "number" ? String(value).trim() : "";
}

function safeHttpUrl(value: unknown): string | null {
const raw = asText(value);
if (!raw) return null;

try {
const parsed = new URL(raw);
if (!["http:", "https:"].includes(parsed.protocol) || parsed.username || parsed.password) {
return null;
}
return parsed.toString();
} catch {
return null;
}
}

function slugify(value: string): string {
return value
.normalize("NFKD")
.replace(/[\u0300-\u036f]/g, "")
.toLowerCase()
.trim()
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-+|-+$/g, "");
}

function normalizeTags(value: unknown): string[] {
const raw = Array.isArray(value)
? value
: typeof value === "string"
? value.split(",")
: [];

const seen = new Set<string>();
const tags: string[] = [];

for (const entry of raw) {
const tag = asText(entry);
const key = tag.toLowerCase();
if (tag && !seen.has(key)) {
seen.add(key);
tags.push(tag);
}
}

return tags;
}

function dimension(value: unknown, fallback: string): string {
const parsed = Number.parseInt(asText(value), 10);
return Number.isSafeInteger(parsed) && parsed > 0 && parsed <= 10000
? String(parsed)
: fallback;
}

export function normalizeGameFeed(payload: unknown): NormalizeResult {
if (!Array.isArray(payload)) {
throw new Error("Game feed must be a JSON array.");
}

const games: GameRecord[] = [];
const skipped: NormalizeResult["skipped"] = [];
const seenIds = new Set<string>();
const seenSlugs = new Set<string>();

payload.forEach((raw: unknown, index: number) => {
if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
skipped.push({ index, reason: "Record is not an object." });
return;
}

const item = raw as Record<string, unknown>;
const id = asText(item.id);
const name = asText(item.title);
const url = safeHttpUrl(item.url);
const image = safeHttpUrl(item.thumb);

if (!/^[a-zA-Z0-9_-]{1,100}$/.test(id)) {
skipped.push({ index, reason: "Missing or unsafe game ID." });
return;
}
if (!name) {
skipped.push({ index, reason: "Missing game title." });
return;
}
if (!url || !image) {
skipped.push({ index, reason: "Game URL or thumbnail is not a valid HTTP(S) URL." });
return;
}
if (seenIds.has(id)) {
skipped.push({ index, reason: `Duplicate game ID: ${id}.` });
return;
}

const baseSlug = slugify(name) || `game-${slugify(id)}`;
let slug = baseSlug;
if (seenSlugs.has(slug)) slug = `${baseSlug}-${slugify(id)}`;
let suffix = 2;
while (seenSlugs.has(slug)) slug = `${baseSlug}-${slugify(id)}-${suffix++}`;

seenIds.add(id);
seenSlugs.add(slug);

games.push({
id,
name,
slug,
description: asText(item.description) || `Play ${name} online for free.`,
instructions: asText(item.instructions) || "Use keyboard or mouse to play.",
category: asText(item.category) || "Casual",
tags: normalizeTags(item.tags),
url,
embedUrl: url,
image,
width: dimension(item.width, "800"),
height: dimension(item.height, "600"),
});
});

return { games, skipped };
}

export async function fetchJsonWithRetry(
url: string,
fetchImpl: typeof fetch = fetch,
options: FetchOptions = {},
): Promise<unknown> {
const attempts = Math.max(1, Math.floor(options.attempts ?? 3));
const timeoutMs = Math.max(1, options.timeoutMs ?? 15_000);
const baseDelayMs = Math.max(0, options.baseDelayMs ?? 500);
const maxDelayMs = Math.max(baseDelayMs, options.maxDelayMs ?? 5_000);
let lastError: unknown;

for (let attempt = 1; attempt <= attempts; attempt++) {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), timeoutMs);

try {
const response = await fetchImpl(url, {
signal: controller.signal,
headers: { accept: "application/json" },
});

if (!response.ok) {
const retryable = response.status === 408 || response.status === 429 || response.status >= 500;
const error = new Error(`Feed request failed with HTTP ${response.status}.`);
if (!retryable || attempt === attempts) throw error;
const retryAfter = Number(response.headers.get("retry-after"));
const delay = Number.isFinite(retryAfter) && retryAfter > 0
? Math.min(maxDelayMs, retryAfter * 1000)
: Math.min(maxDelayMs, baseDelayMs * attempt);
await new Promise((resolve) => setTimeout(resolve, delay));
continue;
}

return await response.json();
} catch (error) {
lastError = error;
if (attempt === attempts) break;
const delay = Math.min(maxDelayMs, baseDelayMs * attempt);
if (delay > 0) await new Promise((resolve) => setTimeout(resolve, delay));
} finally {
clearTimeout(timeout);
}
}

throw new Error(
`Unable to fetch game feed after ${attempts} attempt(s): ${lastError instanceof Error ? lastError.message : String(lastError)}`,
);
}

async function pathExists(target: string): Promise<boolean> {
try {
await lstat(target);
return true;
} catch (error) {
if ((error as NodeJS.ErrnoException).code === "ENOENT") return false;
throw error;
}
}

export async function persistGameRecords(
games: GameRecord[],
rootDirectory = process.cwd(),
outputDirectory = "games",
): Promise<void> {
if (games.length === 0) throw new Error("Refusing to replace the game catalog with an empty feed.");

const outputPath = path.join(rootDirectory, outputDirectory);
const stagingPath = await mkdtemp(path.join(rootDirectory, ".games-stage-"));
const backupPath = path.join(rootDirectory, `.games-backup-${randomUUID()}`);
let movedOldDirectory = false;

try {
for (const game of games) {
await writeFile(
path.join(stagingPath, `${game.id}.json`),
JSON.stringify(game, null, 2) + "\n",
{ encoding: "utf8", flag: "wx" },
);
}

if (await pathExists(outputPath)) {
const info = await lstat(outputPath);
if (!info.isDirectory() || info.isSymbolicLink()) {
throw new Error(`Refusing to replace non-directory output path: ${outputPath}`);
}
await rename(outputPath, backupPath);
movedOldDirectory = true;
}

try {
await rename(stagingPath, outputPath);
} catch (error) {
if (movedOldDirectory) await rename(backupPath, outputPath);
throw error;
}

if (movedOldDirectory) await rm(backupPath, { recursive: true, force: true });
} finally {
await rm(stagingPath, { recursive: true, force: true });
}
}
121 changes: 121 additions & 0 deletions tests/game-feed.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { describe, expect, test } from "bun:test";
import {
fetchJsonWithRetry,
normalizeGameFeed,
persistGameRecords,
} from "../scripts/game-feed";
import { mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises";
import path from "node:path";
import os from "node:os";

const valid = (overrides: Record<string, unknown> = {}) => ({
id: "100",
title: "Puzzle Hero",
description: "A puzzle game",
instructions: "Use the arrow keys",
category: "Puzzle",
tags: "Logic, Brain, Logic",
url: "https://games.example.test/play/100",
thumb: "https://images.example.test/100.jpg",
width: "800",
height: "600",
...overrides,
});

describe("normalizeGameFeed", () => {
test("normalizes valid records and de-duplicates tags", () => {
const result = normalizeGameFeed([valid()]);
expect(result.games).toHaveLength(1);
expect(result.games[0]?.slug).toBe("puzzle-hero");
expect(result.games[0]?.tags).toEqual(["Logic", "Brain"]);
expect(result.games[0]?.rating).toBeUndefined();
expect(result.skipped).toHaveLength(0);
});

test("skips malformed records, duplicate IDs, and unsafe URLs", () => {
const result = normalizeGameFeed([
null,
valid(),
valid({ id: "100", title: "Duplicate" }),
valid({ id: "bad/id" }),
valid({ id: "101", url: "javascript:alert(1)" }),
]);
expect(result.games).toHaveLength(1);
expect(result.skipped).toHaveLength(4);
});

test("makes duplicate titles produce stable unique slugs", () => {
const result = normalizeGameFeed([
valid({ id: "101", title: "Same Name" }),
valid({ id: "102", title: "Same Name" }),
]);
expect(result.games.map((game) => game.slug)).toEqual(["same-name", "same-name-102"]);
});

test("rejects a non-array feed payload", () => {
expect(() => normalizeGameFeed({ games: [] })).toThrow("JSON array");
});
});

describe("fetchJsonWithRetry", () => {
test("retries transient HTTP failures and returns the successful response", async () => {
let calls = 0;
const fakeFetch: typeof fetch = async () => {
calls += 1;
return calls === 1
? new Response("temporary failure", { status: 503 })
: new Response(JSON.stringify([valid()]), {
status: 200,
headers: { "content-type": "application/json" },
});
};

const payload = await fetchJsonWithRetry("https://feed.example.test", fakeFetch, {
attempts: 2,
baseDelayMs: 0,
});
expect(calls).toBe(2);
expect(Array.isArray(payload)).toBe(true);
});

test("does not retry permanent client errors", async () => {
let calls = 0;
const fakeFetch: typeof fetch = async () => {
calls += 1;
return new Response("not found", { status: 404 });
};
await expect(fetchJsonWithRetry("https://feed.example.test", fakeFetch, {
attempts: 3,
baseDelayMs: 0,
})).rejects.toThrow("HTTP 404");
expect(calls).toBe(1);
});
});

describe("persistGameRecords", () => {
test("replaces a populated directory only after staging all new records", async () => {
const root = await mkdtemp(path.join(os.tmpdir(), "sopgames-test-"));
try {
await writeFile(path.join(root, "sentinel.txt"), "keep root");
await persistGameRecords(normalizeGameFeed([valid()]).games, root);
const files = await readdir(path.join(root, "games"));
expect(files).toEqual(["100.json"]);
const saved = JSON.parse(await readFile(path.join(root, "games", "100.json"), "utf8"));
expect(saved.slug).toBe("puzzle-hero");
} finally {
await rm(root, { recursive: true, force: true });
}
});

test("keeps existing catalog when the new feed is empty", async () => {
const root = await mkdtemp(path.join(os.tmpdir(), "sopgames-test-"));
try {
await import("node:fs/promises").then(({ mkdir }) => mkdir(path.join(root, "games")));
await writeFile(path.join(root, "games", "old.json"), "{}");
await expect(persistGameRecords([], root)).rejects.toThrow("empty feed");
expect(await readdir(path.join(root, "games"))).toEqual(["old.json"]);
} finally {
await rm(root, { recursive: true, force: true });
}
});
});
Loading