@hadoku/game-server 0.1.130 → 0.1.132

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,28 @@
1
+ import type { JsonLog } from '@wolffm/service-kernel/log';
2
+ import { type ExternalGameManifest, type ExternalGameManifestInput } from '@wolffm/platform-wire/external-game-manifest';
3
+ /** A hosted game as the reader needs it: where its embed is, or (a test fixture) its manifest inline. */
4
+ export interface ManifestedGame {
5
+ id: string;
6
+ embedSrc: string;
7
+ /** A fixture with no publisher behind it carries its manifest here, and nothing is fetched. */
8
+ manifest?: ExternalGameManifestInput;
9
+ }
10
+ export interface ManifestReaderOptions {
11
+ /** The origin the embed paths are under: `https://hadoku.me`. */
12
+ origin: string;
13
+ /** Sent as `X-User-Key`, which the edge needs for a gated bundle route. */
14
+ serviceKey?: string;
15
+ /** Where each game's last good manifest is kept, `<id>.json`. */
16
+ cacheDir: string;
17
+ log: JsonLog;
18
+ fetch?: typeof fetch;
19
+ timeoutMs?: number;
20
+ }
21
+ /** Where a manifest came from: the game's own file just now, the copy on disk, or the host's config. */
22
+ export type ManifestSource = 'fetched' | 'cached' | 'inline';
23
+ export interface ReadManifest {
24
+ manifest: ExternalGameManifest;
25
+ source: ManifestSource;
26
+ }
27
+ /** Reads every game's manifest the same way; one reader per server. */
28
+ export declare function manifestReader(opts: ManifestReaderOptions): (game: ManifestedGame) => Promise<ReadManifest | null>;
@@ -0,0 +1,84 @@
1
+ /**
2
+ * Reading each hosted external game's `manifest.json` (`@hadoku/platform-wire/external-game-manifest`):
3
+ * fetched through the edge beside the game's embed entry, checked against the schema, and kept on
4
+ * disk as the last good copy.
5
+ *
6
+ * The last good copy is what keeps a room type alive across a bad release or an unreachable edge.
7
+ * A host that boots without a type registered quarantines every saved room of it, so a manifest
8
+ * that cannot be fetched or read never unregisters a game that was read once: the copy on disk
9
+ * stands in, and the reason is logged. Only a game never read anywhere has no manifest.
10
+ */
11
+ import { mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
12
+ import { join } from 'node:path';
13
+ import { externalGameManifest, manifestPathOf, } from '@wolffm/platform-wire/external-game-manifest';
14
+ const DEFAULT_TIMEOUT_MS = 5_000;
15
+ function describe(err) {
16
+ return err instanceof Error ? err.message : String(err);
17
+ }
18
+ function parsed(raw) {
19
+ const checked = externalGameManifest.safeParse(raw);
20
+ if (!checked.success)
21
+ throw new Error(checked.error.issues.map((i) => `${i.path.join('.') || '(root)'}: ${i.message}`).join('; '));
22
+ return checked.data;
23
+ }
24
+ /** Reads every game's manifest the same way; one reader per server. */
25
+ export function manifestReader(opts) {
26
+ const doFetch = opts.fetch ?? fetch;
27
+ const timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS;
28
+ const cacheFile = (id) => join(opts.cacheDir, `${id}.json`);
29
+ const fetchOne = async (game) => {
30
+ const url = `${opts.origin}${manifestPathOf(game.embedSrc)}`;
31
+ const res = await doFetch(url, {
32
+ headers: { 'User-Agent': 'Mozilla/5.0 hadoku-cardtable', ...(opts.serviceKey ? { 'X-User-Key': opts.serviceKey } : {}) },
33
+ signal: AbortSignal.timeout(timeoutMs),
34
+ });
35
+ if (!res.ok)
36
+ throw new Error(`${url} answered ${res.status}`);
37
+ return parsed(await res.json());
38
+ };
39
+ const readCache = (id) => {
40
+ let text;
41
+ try {
42
+ text = readFileSync(cacheFile(id), 'utf8');
43
+ }
44
+ catch (err) {
45
+ if (err.code === 'ENOENT')
46
+ return null;
47
+ throw err;
48
+ }
49
+ return parsed(JSON.parse(text));
50
+ };
51
+ const writeCache = (id, manifest) => {
52
+ mkdirSync(opts.cacheDir, { recursive: true });
53
+ const tmp = `${cacheFile(id)}.tmp`;
54
+ writeFileSync(tmp, `${JSON.stringify(manifest, null, 2)}\n`, 'utf8');
55
+ renameSync(tmp, cacheFile(id));
56
+ };
57
+ return async (game) => {
58
+ if (game.manifest !== undefined)
59
+ return { manifest: parsed(game.manifest), source: 'inline' };
60
+ let cached = null;
61
+ let cacheProblem = null;
62
+ try {
63
+ cached = readCache(game.id);
64
+ }
65
+ catch (err) {
66
+ cacheProblem = describe(err);
67
+ }
68
+ try {
69
+ const manifest = await fetchOne(game);
70
+ if (JSON.stringify(manifest) !== JSON.stringify(cached))
71
+ writeCache(game.id, manifest);
72
+ return { manifest, source: 'fetched' };
73
+ }
74
+ catch (err) {
75
+ const reason = describe(err);
76
+ if (cached !== null) {
77
+ opts.log({ evt: 'external-game-manifest-stale', gameType: game.id, reason });
78
+ return { manifest: cached, source: 'cached' };
79
+ }
80
+ opts.log({ evt: 'external-game-manifest-missing', gameType: game.id, reason, ...(cacheProblem === null ? {} : { cache: cacheProblem }) });
81
+ return null;
82
+ }
83
+ };
84
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hadoku/game-server",
3
- "version": "0.1.130",
4
- "gitHead": "5c383d59fad31e2e2146743580438f162ddedee1",
3
+ "version": "0.1.132",
4
+ "gitHead": "5dd41313efc861da44f781b3e8479a6b9f6b3494",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "description": "The server parts any hosted game shares, above the kernel: the app config and edge auth, the room core (identity, seats, sessions, membership, the room list, the generic room routes), match hosting (the host contract, local and remote JVMs, placement, the start queue, recordings, the in-process and relay runtimes), streams, erase, telemetry and usage. Knows no game; Magic plugs in from apps/server. MIT; imports only node built-ins, express, socket.io, @wolffm/* and the platform packages.",
@@ -17,12 +17,12 @@
17
17
  "play/remote/"
18
18
  ],
19
19
  "dependencies": {
20
- "@wolffm/host-wire": "npm:@hadoku/host-wire@0.1.130",
21
- "@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.130",
22
- "@wolffm/service-kernel": "npm:@hadoku/service-kernel@0.1.163",
20
+ "@wolffm/host-wire": "npm:@hadoku/host-wire@0.1.132",
21
+ "@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.132",
22
+ "@wolffm/service-kernel": "npm:@hadoku/service-kernel@0.1.165",
23
23
  "@wolffm/prefs-client": "npm:@hadoku/prefs-client@^1.2.1",
24
- "@wolffm/rooms-protocol": "npm:@hadoku/rooms-protocol@^0.45.138",
25
- "@wolffm/rooms-server": "npm:@hadoku/rooms-server@^0.45.138",
24
+ "@wolffm/rooms-protocol": "npm:@hadoku/rooms-protocol@^0.45.139",
25
+ "@wolffm/rooms-server": "npm:@hadoku/rooms-server@^0.45.139",
26
26
  "express": "^5.1.0",
27
27
  "socket.io": "^4.8.3",
28
28
  "zod": "4.5.4"