@hadoku/game-server 0.1.114 → 0.1.115
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.
- package/dist/erase/users.d.ts +11 -0
- package/dist/erase/users.js +15 -0
- package/dist/users/store.d.ts +48 -0
- package/dist/users/store.js +115 -0
- package/package.json +5 -5
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Account erase in the per-person store (`users/store.ts`): the person's document is deleted,
|
|
3
|
+
* whole. It holds nothing anyone else needs (their favourites, when they last played each deck,
|
|
4
|
+
* the decks they built, their art picks), so nothing is anonymised and kept.
|
|
5
|
+
*
|
|
6
|
+
* An account merge folds the merged-away account's document into the kept one, by the game's own
|
|
7
|
+
* merge (the kept account wins each conflict), and deletes it.
|
|
8
|
+
*/
|
|
9
|
+
import type { UserStore } from '../users/store.js';
|
|
10
|
+
import type { EraseStep } from './index.js';
|
|
11
|
+
export declare function usersEraseStep(store: Pick<UserStore<unknown>, 'remove' | 'move'>): EraseStep;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Account erase in the per-person store (`users/store.ts`): the person's document is deleted,
|
|
3
|
+
* whole. It holds nothing anyone else needs (their favourites, when they last played each deck,
|
|
4
|
+
* the decks they built, their art picks), so nothing is anonymised and kept.
|
|
5
|
+
*
|
|
6
|
+
* An account merge folds the merged-away account's document into the kept one, by the game's own
|
|
7
|
+
* merge (the kept account wins each conflict), and deletes it.
|
|
8
|
+
*/
|
|
9
|
+
export function usersEraseStep(store) {
|
|
10
|
+
return {
|
|
11
|
+
what: 'users',
|
|
12
|
+
erase: ({ userId }) => store.remove(userId),
|
|
13
|
+
merge: ({ from, into }) => store.move(from.userId, into.userId),
|
|
14
|
+
};
|
|
15
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The per-person store: one JSON document per account, `<dir>/<userId>.json`, owned by this
|
|
3
|
+
* server and kept on its own disk (operator, 2026-11-23: "i would prefer if all the data was
|
|
4
|
+
* self-hosted so we don't rely on cloudflare for this").
|
|
5
|
+
*
|
|
6
|
+
* The store knows nothing of what a document holds. The game hands it the shape: an empty
|
|
7
|
+
* document, a parse of what is on disk, and a merge for an account merge. So Magic's favourites,
|
|
8
|
+
* last-played stamps, built decks and art picks live in Magic's own file, and this one stays MIT.
|
|
9
|
+
*
|
|
10
|
+
* Every write is a temp file renamed over the target, as `draftRooms.ts` writes a pod, so a
|
|
11
|
+
* reader never sees half a document and the nightly state backup (which carries the state dir
|
|
12
|
+
* whole) never copies one. Updates to one person are serialised in this process, so two requests
|
|
13
|
+
* that land together both keep their change.
|
|
14
|
+
*
|
|
15
|
+
* `get` and `update` are the whole interface the rest of the server uses, so moving the documents
|
|
16
|
+
* into dataplatform's store later (`docs/planning/telemetry-and-state-inventory.md` §4) replaces
|
|
17
|
+
* this file and nothing that reads it.
|
|
18
|
+
*
|
|
19
|
+
* A file that is not a document (truncated by a full disk, edited by hand) is moved aside to
|
|
20
|
+
* `<userId>.json.unreadable-<ms>`, logged, and the person starts from an empty document: their
|
|
21
|
+
* bytes are kept for the operator, and nothing they do afterwards is refused for it.
|
|
22
|
+
*/
|
|
23
|
+
export declare function isStorableUserId(userId: string): boolean;
|
|
24
|
+
export interface UserDocShape<D> {
|
|
25
|
+
/** A person with no document yet. */
|
|
26
|
+
empty(): D;
|
|
27
|
+
/** What is on disk, as a document; throws when it is not one. */
|
|
28
|
+
parse(raw: unknown): D;
|
|
29
|
+
/** An account merge: `from`'s document folded into `into`'s, `into` winning every conflict. */
|
|
30
|
+
merge(from: D, into: D): D;
|
|
31
|
+
}
|
|
32
|
+
export interface UserStore<D> {
|
|
33
|
+
/** The person's document, or an empty one. */
|
|
34
|
+
get(userId: string): Promise<D>;
|
|
35
|
+
/** Change the person's document and write it. `fn` edits the document in place or returns a new one. Answers what was written. */
|
|
36
|
+
update(userId: string, fn: (doc: D) => D | void): Promise<D>;
|
|
37
|
+
/** Account erase: the person's document is deleted. Answers 1 when there was one. */
|
|
38
|
+
remove(userId: string): Promise<number>;
|
|
39
|
+
/** Account merge: `fromUserId`'s document folds into `intoUserId`'s and is deleted. Answers 1 when there was one to move. */
|
|
40
|
+
move(fromUserId: string, intoUserId: string): Promise<number>;
|
|
41
|
+
}
|
|
42
|
+
export interface UserStoreOptions<D> {
|
|
43
|
+
dir: string;
|
|
44
|
+
shape: UserDocShape<D>;
|
|
45
|
+
log?: (entry: Record<string, unknown>) => void;
|
|
46
|
+
now?: () => number;
|
|
47
|
+
}
|
|
48
|
+
export declare function createUserStore<D>(opts: UserStoreOptions<D>): UserStore<D>;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The per-person store: one JSON document per account, `<dir>/<userId>.json`, owned by this
|
|
3
|
+
* server and kept on its own disk (operator, 2026-11-23: "i would prefer if all the data was
|
|
4
|
+
* self-hosted so we don't rely on cloudflare for this").
|
|
5
|
+
*
|
|
6
|
+
* The store knows nothing of what a document holds. The game hands it the shape: an empty
|
|
7
|
+
* document, a parse of what is on disk, and a merge for an account merge. So Magic's favourites,
|
|
8
|
+
* last-played stamps, built decks and art picks live in Magic's own file, and this one stays MIT.
|
|
9
|
+
*
|
|
10
|
+
* Every write is a temp file renamed over the target, as `draftRooms.ts` writes a pod, so a
|
|
11
|
+
* reader never sees half a document and the nightly state backup (which carries the state dir
|
|
12
|
+
* whole) never copies one. Updates to one person are serialised in this process, so two requests
|
|
13
|
+
* that land together both keep their change.
|
|
14
|
+
*
|
|
15
|
+
* `get` and `update` are the whole interface the rest of the server uses, so moving the documents
|
|
16
|
+
* into dataplatform's store later (`docs/planning/telemetry-and-state-inventory.md` §4) replaces
|
|
17
|
+
* this file and nothing that reads it.
|
|
18
|
+
*
|
|
19
|
+
* A file that is not a document (truncated by a full disk, edited by hand) is moved aside to
|
|
20
|
+
* `<userId>.json.unreadable-<ms>`, logged, and the person starts from an empty document: their
|
|
21
|
+
* bytes are kept for the operator, and nothing they do afterwards is refused for it.
|
|
22
|
+
*/
|
|
23
|
+
import { mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises';
|
|
24
|
+
import { join } from 'node:path';
|
|
25
|
+
/** A registry userId: what the edge sends as `X-User-Id`. Anything else never becomes a file name. */
|
|
26
|
+
const USER_ID = /^[A-Za-z0-9][A-Za-z0-9_-]{0,127}$/;
|
|
27
|
+
export function isStorableUserId(userId) {
|
|
28
|
+
return USER_ID.test(userId);
|
|
29
|
+
}
|
|
30
|
+
export function createUserStore(opts) {
|
|
31
|
+
const { dir, shape } = opts;
|
|
32
|
+
const log = opts.log ?? (() => undefined);
|
|
33
|
+
const now = opts.now ?? Date.now;
|
|
34
|
+
/** The tail of each person's queue of reads-then-writes. */
|
|
35
|
+
const queues = new Map();
|
|
36
|
+
const pathOf = (userId) => {
|
|
37
|
+
if (!isStorableUserId(userId))
|
|
38
|
+
throw new Error(`not a userId: ${JSON.stringify(userId)}`);
|
|
39
|
+
return join(dir, `${userId}.json`);
|
|
40
|
+
};
|
|
41
|
+
/** Run `job` after every earlier job for these people. */
|
|
42
|
+
function serial(userIds, job) {
|
|
43
|
+
const before = Promise.all(userIds.map((id) => queues.get(id)));
|
|
44
|
+
const run = before.then(job, job);
|
|
45
|
+
const tail = run.catch(() => undefined);
|
|
46
|
+
for (const id of userIds)
|
|
47
|
+
queues.set(id, tail);
|
|
48
|
+
void tail.then(() => {
|
|
49
|
+
for (const id of userIds)
|
|
50
|
+
if (queues.get(id) === tail)
|
|
51
|
+
queues.delete(id);
|
|
52
|
+
});
|
|
53
|
+
return run;
|
|
54
|
+
}
|
|
55
|
+
async function read(userId) {
|
|
56
|
+
const path = pathOf(userId);
|
|
57
|
+
let text;
|
|
58
|
+
try {
|
|
59
|
+
text = await readFile(path, 'utf8');
|
|
60
|
+
}
|
|
61
|
+
catch (err) {
|
|
62
|
+
if (err.code === 'ENOENT')
|
|
63
|
+
return null;
|
|
64
|
+
throw err;
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
return shape.parse(JSON.parse(text));
|
|
68
|
+
}
|
|
69
|
+
catch (err) {
|
|
70
|
+
const aside = `${path}.unreadable-${now()}`;
|
|
71
|
+
await rename(path, aside);
|
|
72
|
+
log({ evt: 'user-doc-unreadable', userId, movedTo: aside, error: err.message });
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
async function write(userId, doc) {
|
|
77
|
+
const path = pathOf(userId);
|
|
78
|
+
await mkdir(dir, { recursive: true });
|
|
79
|
+
const tmp = `${path}.${process.pid}.${now()}.tmp`;
|
|
80
|
+
await writeFile(tmp, JSON.stringify(doc));
|
|
81
|
+
await rename(tmp, path);
|
|
82
|
+
}
|
|
83
|
+
async function drop(userId) {
|
|
84
|
+
try {
|
|
85
|
+
await rm(pathOf(userId));
|
|
86
|
+
return 1;
|
|
87
|
+
}
|
|
88
|
+
catch (err) {
|
|
89
|
+
if (err.code === 'ENOENT')
|
|
90
|
+
return 0;
|
|
91
|
+
throw err;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
get: (userId) => serial([userId], async () => (await read(userId)) ?? shape.empty()),
|
|
96
|
+
update: (userId, fn) => serial([userId], async () => {
|
|
97
|
+
const doc = (await read(userId)) ?? shape.empty();
|
|
98
|
+
const next = fn(doc) ?? doc;
|
|
99
|
+
await write(userId, next);
|
|
100
|
+
return next;
|
|
101
|
+
}),
|
|
102
|
+
// An id that cannot be a file name names no document: nothing to erase or move.
|
|
103
|
+
remove: (userId) => (isStorableUserId(userId) ? serial([userId], () => drop(userId)) : Promise.resolve(0)),
|
|
104
|
+
move: (fromUserId, intoUserId) => serial([fromUserId, intoUserId], async () => {
|
|
105
|
+
if (fromUserId === intoUserId || !isStorableUserId(fromUserId))
|
|
106
|
+
return 0;
|
|
107
|
+
const from = await read(fromUserId);
|
|
108
|
+
if (from === null)
|
|
109
|
+
return 0;
|
|
110
|
+
await write(intoUserId, shape.merge(from, (await read(intoUserId)) ?? shape.empty()));
|
|
111
|
+
await drop(fromUserId);
|
|
112
|
+
return 1;
|
|
113
|
+
}),
|
|
114
|
+
};
|
|
115
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hadoku/game-server",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"gitHead": "
|
|
3
|
+
"version": "0.1.115",
|
|
4
|
+
"gitHead": "d9abd787456c0d6e2ca5a8fbd799bfa09948553c",
|
|
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,9 +17,9 @@
|
|
|
17
17
|
"play/remote/"
|
|
18
18
|
],
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@wolffm/host-wire": "npm:@hadoku/host-wire@0.1.
|
|
21
|
-
"@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.
|
|
22
|
-
"@wolffm/service-kernel": "npm:@hadoku/service-kernel@0.1.
|
|
20
|
+
"@wolffm/host-wire": "npm:@hadoku/host-wire@0.1.115",
|
|
21
|
+
"@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.115",
|
|
22
|
+
"@wolffm/service-kernel": "npm:@hadoku/service-kernel@0.1.148",
|
|
23
23
|
"@wolffm/prefs-client": "npm:@hadoku/prefs-client@^1.1.32",
|
|
24
24
|
"@wolffm/rooms-protocol": "npm:@hadoku/rooms-protocol@^0.45.131",
|
|
25
25
|
"@wolffm/rooms-server": "npm:@hadoku/rooms-server@^0.45.131",
|