@hadoku/game-server 0.1.201 → 0.1.202

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,27 @@
1
+ /**
2
+ * A host that stands in for one that does not exist yet, or that may be swapped underneath its
3
+ * match: a queued start's host until its JVM is asked for (`start-queue.ts`), and the host a
4
+ * placed match talks to while a failed start is placed again (`placement/coordinator.ts`).
5
+ */
6
+ import type { MatchHost } from './host.js';
7
+ type Remembered = Parameters<NonNullable<MatchHost<{
8
+ t: string;
9
+ }>['remember']>>[0];
10
+ export declare class DeferredHost<C extends {
11
+ t: string;
12
+ }> implements MatchHost<C> {
13
+ private target;
14
+ private readonly onStopWithoutTarget;
15
+ private readonly held;
16
+ private remembered;
17
+ /** `onStopWithoutTarget`: what `stop` does while there is no target to stop. */
18
+ constructor(target?: MatchHost<C> | null, onStopWithoutTarget?: () => void);
19
+ /** The real host: the latest remembered state goes to it, then every held command, in order. */
20
+ become(target: MatchHost<C>): void;
21
+ send(command: C): void;
22
+ stop(): void;
23
+ remember(state: Remembered): void;
24
+ /** With no target there is no JVM to leave running. */
25
+ handOver(): boolean;
26
+ }
27
+ export {};
@@ -0,0 +1,44 @@
1
+ /**
2
+ * A host that stands in for one that does not exist yet, or that may be swapped underneath its
3
+ * match: a queued start's host until its JVM is asked for (`start-queue.ts`), and the host a
4
+ * placed match talks to while a failed start is placed again (`placement/coordinator.ts`).
5
+ */
6
+ export class DeferredHost {
7
+ target;
8
+ onStopWithoutTarget;
9
+ held = [];
10
+ remembered = null;
11
+ /** `onStopWithoutTarget`: what `stop` does while there is no target to stop. */
12
+ constructor(target = null, onStopWithoutTarget = () => { }) {
13
+ this.target = target;
14
+ this.onStopWithoutTarget = onStopWithoutTarget;
15
+ }
16
+ /** The real host: the latest remembered state goes to it, then every held command, in order. */
17
+ become(target) {
18
+ this.target = target;
19
+ if (this.remembered !== null)
20
+ target.remember?.(this.remembered);
21
+ for (const command of this.held.splice(0))
22
+ target.send(command);
23
+ }
24
+ send(command) {
25
+ if (this.target !== null)
26
+ this.target.send(command);
27
+ else
28
+ this.held.push(command);
29
+ }
30
+ stop() {
31
+ if (this.target !== null)
32
+ this.target.stop();
33
+ else
34
+ this.onStopWithoutTarget();
35
+ }
36
+ remember(state) {
37
+ this.remembered = state;
38
+ this.target?.remember?.(state);
39
+ }
40
+ /** With no target there is no JVM to leave running. */
41
+ handOver() {
42
+ return this.target?.handOver?.() ?? false;
43
+ }
44
+ }
package/dist/play/host.js CHANGED
@@ -123,8 +123,9 @@
123
123
  import { appendFileSync, closeSync, existsSync, mkdirSync, openSync, readdirSync, readFileSync, renameSync, rmSync } from 'node:fs';
124
124
  import { randomBytes } from 'node:crypto';
125
125
  import { join } from 'node:path';
126
+ import { writeFileAtomicSync } from '@wolffm/host-wire';
126
127
  import { jsonLogger } from '@wolffm/service-kernel/log';
127
- import { JVM_HEAP_MB, MATCHES_PER_JVM, RUN_DIR_NAME, localJvmLauncher, writeFileAtomic } from './local-jvm.js';
128
+ import { JVM_HEAP_MB, MATCHES_PER_JVM, RUN_DIR_NAME, localJvmLauncher } from './local-jvm.js';
128
129
  const LIFECYCLE_T = new Set(['ready', 'fatal', 'frame', 'snapshot']);
129
130
  /** Narrows any engine's line onto the four the host itself understands. See {@link HostLifecycleLine}. */
130
131
  export function isLifecycle(line) {
@@ -280,12 +281,12 @@ export function jvmHostFactory(config, options = {}) {
280
281
  matches: hosted.map((h) => recordEntry(h.entry)),
281
282
  };
282
283
  try {
283
- writeFileAtomic(join(jvm.dir, JVM_RECORD_FILE), `${JSON.stringify(record, null, 2)}\n`);
284
+ writeFileAtomicSync(join(jvm.dir, JVM_RECORD_FILE), `${JSON.stringify(record, null, 2)}\n`);
284
285
  for (const h of hosted)
285
286
  h.changed = false;
286
287
  }
287
288
  catch (err) {
288
- // The old record is whole (`writeFileAtomic`), so it names each match as it was. One whose
289
+ // The old record is whole (`writeFileAtomicSync`), so it names each match as it was. One whose
289
290
  // entry changed since cannot be re-attached as it is now, and must not be handed over.
290
291
  poolLog({ evt: 'jvm-record-failed', id: jvm.hostId, error: err.message });
291
292
  for (const h of hosted)
@@ -252,13 +252,6 @@ export declare function bridgeLaunch(config: BridgeConfig, log: (entry: Record<s
252
252
  export declare function bridgeVersion(config: BridgeConfig): string;
253
253
  /** `{hostId, pid, startedAt}`: what a later process finds a running JVM by. Written at launch. */
254
254
  export declare const LAUNCH_RECORD_FILE = "launch.json";
255
- /**
256
- * Write `file` whole or not at all: into a temporary file beside it, then renamed over
257
- * it. `writeFileSync` in place truncates first, so a full disk left `jvm.json` empty and
258
- * its match un-re-attachable (hosting-test-plan C6); here a failed write leaves the old
259
- * file as it was, and throws.
260
- */
261
- export declare function writeFileAtomic(file: string, text: string): void;
262
255
  /**
263
256
  * Forge resolves `res/` against the working directory (`ForgeConstants.RES_DIR`
264
257
  * is "res/" when the version string carries no "git"), and reads its user and
@@ -78,6 +78,7 @@ import { closeSync, existsSync, mkdirSync, openSync, readdirSync, readSync, read
78
78
  import { homedir, tmpdir } from 'node:os';
79
79
  import { basename, dirname, join, resolve } from 'node:path';
80
80
  import { createInterface } from 'node:readline';
81
+ import { writeFileAtomicSync } from '@wolffm/host-wire';
81
82
  import { bridgeDiagnostics } from './diagnostics.js';
82
83
  import { CRASH_MARKER, IN_FIFO, JVM_TMP_DIR, OUT_FIFO, PID_POLL_MS, REAP_CONFIRM_MS, cwdIsUnder, defaultJava, makeFifos, openJvmIo, pidIsRunning, pidIsStandingIn, pidIsStandingInRun, positiveInt, processCwds, pruneCrashRecords, runDirs, sleepSync, wrapChildProcess, } from './engine-runtime.js';
83
84
  /**
@@ -301,23 +302,6 @@ export const LAUNCH_RECORD_FILE = 'launch.json';
301
302
  // under the default `/tmp` every killed match leaked one copy (3,036 of them on the host's
302
303
  // RAM-backed /tmp, 2026-10-10). Inside the run dir it goes wherever the run dir goes: removed on
303
304
  // a clean end, pruned on a crash.
304
- /**
305
- * Write `file` whole or not at all: into a temporary file beside it, then renamed over
306
- * it. `writeFileSync` in place truncates first, so a full disk left `jvm.json` empty and
307
- * its match un-re-attachable (hosting-test-plan C6); here a failed write leaves the old
308
- * file as it was, and throws.
309
- */
310
- export function writeFileAtomic(file, text) {
311
- const tmp = `${file}.tmp-${process.pid}-${randomBytes(4).toString('hex')}`;
312
- try {
313
- writeFileSync(tmp, text);
314
- renameSync(tmp, file);
315
- }
316
- catch (err) {
317
- rmSync(tmp, { force: true });
318
- throw err;
319
- }
320
- }
321
305
  /**
322
306
  * Forge resolves `res/` against the working directory (`ForgeConstants.RES_DIR`
323
307
  * is "res/" when the version string carries no "git"), and reads its user and
@@ -445,7 +429,7 @@ function keepCrashRecord(run, record) {
445
429
  if (!keep.has(entry))
446
430
  rmSync(join(run, entry), { recursive: true, force: true });
447
431
  }
448
- writeFileAtomic(join(run, CRASH_MARKER), `${JSON.stringify(record, null, 2)}\n`);
432
+ writeFileAtomicSync(join(run, CRASH_MARKER), `${JSON.stringify(record, null, 2)}\n`);
449
433
  }
450
434
  /** `<run>/launch.json`, or null when it is missing, unreadable or not one. */
451
435
  export function readLaunchRecord(run) {
@@ -584,7 +568,7 @@ class LocalJvm {
584
568
  this.pid = engineProcess.pid;
585
569
  live.add(this);
586
570
  log({ evt: 'jvm-spawned', id: hostId, pid: this.pid, run, bridge: bridge.how });
587
- writeFileAtomic(join(run, LAUNCH_RECORD_FILE), `${JSON.stringify({ hostId, pid: this.pid, startedAt: new Date().toISOString() })}\n`);
571
+ writeFileAtomicSync(join(run, LAUNCH_RECORD_FILE), `${JSON.stringify({ hostId, pid: this.pid, startedAt: new Date().toISOString() })}\n`);
588
572
  this.read();
589
573
  engineProcess.onError((err) => this.finish(`the JVM could not be started: ${err.message}`, { code: null, signal: null }));
590
574
  engineProcess.onExit((code, signal) => {
@@ -26,6 +26,7 @@
26
26
  * everything else on the factory.
27
27
  */
28
28
  import { positiveInt } from './engine-runtime.js';
29
+ import { DeferredHost } from './deferred-host.js';
29
30
  /**
30
31
  * JVMs booting at once before a start that needs another waits. A lone boot on a claw under CI
31
32
  * load took 7.8–10.5 s and ten together ~50 s, so two keeps one well inside the 60 s start timeout
@@ -36,61 +37,6 @@ export const COLD_STARTS = 2;
36
37
  export function coldStartsFrom(env = process.env) {
37
38
  return positiveInt(env.CARDTABLE_PLAY_COLD_STARTS, COLD_STARTS);
38
39
  }
39
- /** A waiting start's host until its JVM is asked for. */
40
- class WaitingHost {
41
- leave;
42
- events;
43
- host = null;
44
- held = [];
45
- remembered = null;
46
- gone = false;
47
- constructor(leave, events) {
48
- this.leave = leave;
49
- this.events = events;
50
- }
51
- /** The real host: everything held goes to it, in order. */
52
- become(host) {
53
- this.host = host;
54
- if (this.remembered !== null)
55
- host.remember?.(this.remembered);
56
- for (const command of this.held.splice(0))
57
- host.send(command);
58
- }
59
- /** Never booted: the queue was dropped (the server is closing or handing over). */
60
- drop(reason) {
61
- if (this.gone)
62
- return;
63
- this.gone = true;
64
- this.events.exit(reason);
65
- }
66
- send(command) {
67
- if (this.host !== null)
68
- this.host.send(command);
69
- else
70
- this.held.push(command);
71
- }
72
- stop() {
73
- if (this.host !== null) {
74
- this.host.stop();
75
- return;
76
- }
77
- if (this.gone)
78
- return;
79
- this.leave();
80
- // A host's exit always comes after `stop` returns; so does this one.
81
- queueMicrotask(() => this.drop('stopped before its JVM started'));
82
- }
83
- remember(state) {
84
- if (this.host !== null)
85
- this.host.remember?.(state);
86
- else
87
- this.remembered = state;
88
- }
89
- /** A start still waiting has no JVM to leave running. */
90
- handOver() {
91
- return this.host?.handOver?.() ?? false;
92
- }
93
- }
94
40
  export function queueColdStarts(factory, limit, log) {
95
41
  const waiting = [];
96
42
  const booting = () => factory.booting?.() ?? 0;
@@ -120,7 +66,7 @@ export function queueColdStarts(factory, limit, log) {
120
66
  }
121
67
  catch (err) {
122
68
  next.events.unstartable?.(err);
123
- next.host.drop(`Forge could not be started: ${err.message}`);
69
+ next.drop(`Forge could not be started: ${err.message}`);
124
70
  continue;
125
71
  }
126
72
  next.host.become(host);
@@ -138,23 +84,35 @@ export function queueColdStarts(factory, limit, log) {
138
84
  }
139
85
  function dropAll(reason) {
140
86
  for (const w of waiting.splice(0))
141
- w.host.drop(reason);
87
+ w.drop(reason);
142
88
  }
143
89
  const queued = ((matchId, spec, events) => {
144
90
  if (waiting.length === 0 && free())
145
91
  return begin(matchId, spec, events, null);
92
+ let gone = false;
93
+ const drop = (reason) => {
94
+ if (gone)
95
+ return;
96
+ gone = true;
97
+ events.exit(reason);
98
+ };
146
99
  const entry = {
147
100
  matchId,
148
101
  spec,
149
102
  events,
103
+ drop,
150
104
  place: 0,
151
105
  since: Date.now(),
152
- host: new WaitingHost(() => {
106
+ host: new DeferredHost(null, () => {
107
+ if (gone)
108
+ return;
153
109
  const i = waiting.indexOf(entry);
154
110
  if (i >= 0)
155
111
  waiting.splice(i, 1);
156
112
  pump();
157
- }, events),
113
+ // A host's exit always comes after `stop` returns; so does this one.
114
+ queueMicrotask(() => drop('stopped before its JVM started'));
115
+ }),
158
116
  };
159
117
  waiting.push(entry);
160
118
  log({ evt: 'start-queued', matchId, place: waiting.length, booting: booting(), limit });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hadoku/game-server",
3
- "version": "0.1.201",
4
- "gitHead": "5f99a4b32b35131c69046b111e7ce269ca333b6c",
3
+ "version": "0.1.202",
4
+ "gitHead": "4ba2350938ced47e5519c9acf331d81ff6f6235c",
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.201",
21
- "@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.201",
22
- "@wolffm/service-kernel": "npm:@hadoku/service-kernel@0.1.234",
20
+ "@wolffm/host-wire": "npm:@hadoku/host-wire@0.1.202",
21
+ "@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.202",
22
+ "@wolffm/service-kernel": "npm:@hadoku/service-kernel@0.1.235",
23
23
  "@wolffm/prefs-client": "npm:@hadoku/prefs-client@^1.2.3",
24
24
  "@wolffm/rooms-protocol": "npm:@hadoku/rooms-protocol@^0.45.150",
25
25
  "@wolffm/rooms-server": "npm:@hadoku/rooms-server@^0.45.150",