Server-run engine: review fixes

The review of track T2 found these; each has a test in tests/mp-sim-*.

- Built-ins: an arg, array entry or draft that is '__proto__', 'constructor'
  or 'prototype' is refused by the envelope check and again by the realm
  (free text may still say them). After every realm call the trampoline
  checks the realm's built-ins against what they were at load; a realm
  whose built-ins changed answers {srvErr, polluted} and is replaced from
  the worker's mark like one that timed out. S as one JSON per key is a
  null-prototype object, so the epilogue's for…in never reads a prototype.
- A realm that cannot be built again is never used again: rebuild() tries
  once more without the packed R, and when that fails too the network is
  dropped (NO_REALM), and the engine tells the host (opts.onLost).
- A mark that fails or runs out of time changes nothing: it clears the old
  mark first, the realm is built again from its own save, never from an
  older mark, and the reply names the mark. A day rebuilds only from its
  own attempt's mark.
- A day whose alone or nets is not a list is refused whole (ERR); a throw
  in a load or day step answers that call, never ends the worker.
- An exit counts against the session the worker was running (the worker
  says so before each job and step), else as before. Workers of their own
  are capped (opts.maxOwn, 4): past that a session is given up.
- A worker that exits before it is ready is started again after a pause
  that doubles; after 5 in a row every call answers SIM_DOWN.
- The game text is read and compiled once, in createEngine: a src/ that
  does not compile throws there, and every worker, also one started after
  an exit, runs the game engine.simId names.
- A damaged route memory never breaks a save that loads (routed from
  nothing, logged), and only a server network loads (S.mp.sim, Normal).
- Routing memory (UI.rk, UI.rkR) is kept by the in-place restore and
  carried in rt (rk), so a reloaded or rolled-back network skips the same
  routings as the one that kept going.
- A fresh realm takes over the fx of the one it replaces (the mark's after
  a failed call, all of them after a read): badges, windows and toasts
  waiting for a page are not lost to a timeout.
- Windows: the stand-in elements answer querySelector from what was
  written (a [data-x] marker) and have a working classList, never the
  inert proxy that said every window is open. A request's ctx may name the
  open window, ctx.win = {k, ui} (mp-sim-acts.js WINDOWS: bstd, routing,
  charter, ops, pie), with the view state only the page holds (the
  intern's POP, the pie window's tab and POP), checked in the realm. This
  is an addition to the contract of design 5.8 that the page (T4) sends.
  The stand-in is hidden again after an act.
- xpBuild's via is a TEXT arg (256): a six-span express fits.
- actDone, askDone and pulled carry ms, the worker time of the call;
  engine.mem() and engine.has(code, nid) are public; prefs() passes a
  realm's err through; after close() every call rejects CLOSED.
- The fake engine fails as the engine does (NO_REALM, ERR, a load without
  a save, a day of an unknown session or naming an unloaded network,
  GAVE_UP, SIM_DOWN, CLOSED, onLost), its greet says its one-shots once and
  never 'told'; a test runs the same edge calls on both.
- SIM_DRAFT is built from DRAFT_TYPES.
This commit is contained in:
jvanvik 2026-11-09 22:16:52 +01:00
parent 3b8c331556
commit 9597d89079
11 changed files with 879 additions and 211 deletions

View file

@ -458,6 +458,8 @@ const TEXT = {
teCheck: { script: 4000 },
teDeploy: { script: 4000 },
utilityCaches: { quote: 1024 },
// An express's spans, as link ids joined by ',' (up to XP_HOPS = 6 ids of about 10 characters).
xpBuild: { via: 256 },
};
/* Acts that charge a total the player saw in a window, planned again at the click: the arg that carries what the window
@ -472,6 +474,21 @@ const QUOTE = {
utilityKit: 'cost',
};
/* The windows a request's ctx.win may name: the one the page has open when the click came from inside it. The game asks
which window is open by its class and by a marker inside it (pieRe: class 'pie'; bstdShown: [data-sheet="bstd"];
routingRefresh: [data-routing]; charter), so the realm's stand-in has to be that window, or a redraw on the server
opens another one. c: its class (#modal's className); sel: the marker inside #modalCard, if it has one; ui: the view
state it draws from that only the page holds (set by LOCAL acts: the intern window's POP, the pie window's tab and
POP), each checked against the game in the realm. The page names the first entry, in this order, whose c is the open
window's class and whose sel (when it has one) matches inside it; a window none names is sent as ctx.modal alone. */
const WINDOWS = {
bstd: { c: 'ops', sel: '[data-sheet="bstd"]' },
routing: { c: '', sel: '[data-routing]' },
charter: { c: 'charter' },
ops: { c: 'ops', ui: ['opsCity'] },
pie: { c: 'pie', ui: ['pieCity', 'pieTab'] },
};
const ID_RE = /^[a-z0-9]{8,16}$/, SIM_RE = /^[0-9a-f]{12}$/, KEY_RE = /^[A-Za-z][A-Za-z0-9]{0,15}$/, VIEW_RE = /^[a-z0-9]{1,16}$/, SHORT_RE = /^[A-Za-z0-9_.:|~-]{1,40}$/;
const SPEEDS = [0, 0.5, 1, 2, 4, 8];
// The viewport a request's ctx may set on the realm, in CSS pixels: checkEnvelope clamps ctx.w and ctx.h into it.
@ -481,8 +498,10 @@ const SETS = { SEND: new Set(SEND), ASK: new Set(ASK), CONFIRM: new Set(Object.k
const own = (o, k) => Object.prototype.hasOwnProperty.call(o, k);
const plain = (o) => !!o && typeof o === 'object' && !Array.isArray(o) && (Object.getPrototypeOf(o) === Object.prototype || Object.getPrototypeOf(o) === null);
const prim = (v, cap) => v === null || typeof v === 'boolean' || (typeof v === 'number' && Number.isFinite(v)) || (typeof v === 'string' && v.length <= cap);
// Keys a request object may never carry, whatever the regex says.
// Keys a request object may never carry, whatever the regex says; nor values, where a value names something: written
// through as a key (S.pops[city]), they reach a prototype. Free text (TEXT) may say them.
const BAD_KEY = new Set(['__proto__', 'constructor', 'prototype']);
const badVal = (v) => (typeof v === 'string' ? BAD_KEY.has(v) : Array.isArray(v) ? v.some(badVal) : !!v && typeof v === 'object' && Object.keys(v).some((k) => BAD_KEY.has(k) || badVal(v[k])));
function bad(error, code = 400, extra = {}) { return { ok: false, code, error, ...extra }; }
@ -494,6 +513,7 @@ function checkArgs(act, args) {
for (const k of keys) {
if (!KEY_RE.test(k) || BAD_KEY.has(k)) return `bad arg name ${JSON.stringify(k).slice(0, 40)}`;
const v = args[k], cap = own(caps, k) ? caps[k] : 64;
if (!own(caps, k) && badVal(v)) return `bad arg ${k}`;
if (Array.isArray(v)) { if (v.length > 64 || !v.every((x) => prim(x, 64))) return `bad arg ${k}`; continue; }
if (!prim(v, cap)) return `bad arg ${k}`;
}
@ -502,6 +522,7 @@ function checkArgs(act, args) {
}
function checkDraft(k, v) {
const t = DRAFT_TYPES[k];
if (badVal(v)) return false;
if (Array.isArray(t)) return t.includes(v);
if (t === 'boolean') return typeof v === 'boolean';
if (t === 'string') return typeof v === 'string' && v.length <= 64;
@ -521,6 +542,12 @@ function checkCtx(c) {
// The page sends its innerWidth/innerHeight as they are: a 5120-px monitor, or a desktop zoomed out, is wider than
// anything the layout tells apart (isPhone() and the like only ask whether it is under about 760). Clamped, not refused.
else if (k === 'w' || k === 'h') { if (!(typeof v === 'number' && Number.isFinite(v) && v > 0 && v <= 1e5)) return `bad ctx.${k}`; c[k] = Math.min(VIEW_MAX, Math.max(VIEW_MIN, v)); }
// The open window, by name, with its view state (WINDOWS): {k, ui?}.
else if (k === 'win') {
const W = plain(v) && typeof v.k === 'string' && own(WINDOWS, v.k) ? WINDOWS[v.k] : null;
if (!W || !Object.keys(v).every((x) => x === 'k' || x === 'ui')) return 'bad ctx.win';
if (v.ui !== undefined && !(plain(v.ui) && Object.keys(v.ui).every((u) => (W.ui || []).includes(u) && (v.ui[u] === null || (typeof v.ui[u] === 'string' && SHORT_RE.test(v.ui[u]) && !BAD_KEY.has(v.ui[u])))))) return 'bad ctx.win.ui';
}
else return `unknown ctx.${String(k).slice(0, 20)}`;
}
return null;
@ -586,4 +613,4 @@ function checkEnvelope(kind, body, opts = {}) {
// Body size caps per kind, in bytes (section 10), for the host's readJson.
const BODY_MAX = { act: 16384, ask: 8192, here: 2048, prefs: 1024, vote: 256 };
module.exports = { SEND, ASK, VOTE, LOCAL, PUZZLE, CONFIRM, SRV_ARM, armKey, DRAFTS, DRAFT_TYPES, TEXT, QUOTE, SPEEDS, PREFS, VIEW_MIN, VIEW_MAX, BODY_MAX, checkEnvelope };
module.exports = { SEND, ASK, VOTE, LOCAL, PUZZLE, CONFIRM, SRV_ARM, armKey, DRAFTS, DRAFT_TYPES, TEXT, QUOTE, WINDOWS, SPEEDS, PREFS, VIEW_MIN, VIEW_MAX, BODY_MAX, checkEnvelope };

View file

@ -3,7 +3,7 @@
// (scripts/lib/mp-sim.js) keeps the clock, presence and votes, and asks this for days, actions, what-ifs and pulls.
// tests/lib/mp-sim-fake-engine.js has the same API with canned answers.
//
// const engine = createEngine({workers: 2, heapMb: 1536, root, log});
// const engine = createEngine({workers: 2, heapMb: 1536, root, log, maxOwn: 4, onLost});
// engine.load(code, [{nid, env?, begin?, lang, prefs, rng?}]) → [{t:'loaded', nid, epoch, day, lost, saved?} |
// {t:'loaded', nid, err}], one per network, in order. env: the checkpoint envelope {s, uidN, rt, last,
// day} (s: the save text; rt: what the realm needs besides it to play on exactly as before, opaque
@ -14,36 +14,53 @@
// engine.day(code, {d, speed, last, alone:[nid], nets:[nid], mute}) → {t:'dayDone', code, d, nets:{[nid]: {day, lost,
// pause?, err?, retried?, ms}}}. Each network in sorted order; one that failed twice answers err and
// its day as it was, and is broken from then on (no more days; actions answer 'stopped').
// engine.act(code, nid, msg) → {t:'actDone', nid, epoch, rid, outcome, why?, v, upd} (upd: the update with the result,
// version v; null when stopped) | {t:'err', code, nid, rid, what:'timeout'|'throw', msg}
// engine.ask(code, nid, msg) → {t:'askDone', nid, rid, modal?, ui?, err?} | {t:'err', …}
// engine.pull(code, nid, {snap, views}) → {t:'pulled', nid, epoch, v, upd?, snap?, subs?}. snap is the realm's half
// of a snapshot: snapWith(snap, {clock, roster, since}) makes the stream event.
// engine.act(code, nid, msg) → {t:'actDone', nid, epoch, rid, outcome, why?, v, upd, ms} (upd: the update with the
// result, version v; null when stopped) | {t:'err', code, nid, rid, what:'timeout'|'throw', msg}
// engine.ask(code, nid, msg) → {t:'askDone', nid, rid, modal?, ui?, err?, ms} (neither modal nor err: the what-if
// opened nothing) | {t:'err', …}
// engine.pull(code, nid, {snap, views}) → {t:'pulled', nid, epoch, v, upd?, snap?, subs?, ms}. snap is the realm's
// half of a snapshot: snapWith(snap, {clock, roster, since}) makes the stream event.
// ms: the worker's time for the call (the mark, the realm, the update), without the wait behind other calls: what
// the host's rate limit of worker time counts (section 10).
// engine.sub(code, nid, {views}) → {t:'subbed', nid, epoch, v, subs}; engine.greet(code, nid) → a pulled
// engine.prefs(code, nid, {prefs}) → undefined; engine.save(code, nid) → {t:'saved', nid, txt, uidN, rt, day, lost}
// engine.card(code, nid) → {t:'card', nid, card}; engine.unload(code, nid?) → {t:'unloaded', code, nid?}
// engine.stats() → {workers, realms, sessions} (not a promise); engine.close() → stops the workers
// engine.simId the build id this engine's game belongs to (SIM_ID of the multiplayer page from the same src/)
// engine.prefs(code, nid, {prefs}) → undefined (or the {t:'err'} of a realm that failed);
// engine.save(code, nid) → {t:'saved', nid, txt, uidN, rt, day, lost}; engine.card(code, nid) → {t:'card', nid, card}
// engine.unload(code, nid?) → {t:'unloaded', code, nid?}; engine.has(code, nid) → whether that network has a realm
// engine.stats() → {workers, realms, sessions} (not a promise); engine.mem() → each pool worker's heap and rss
// engine.close() → stops the workers
// engine.simId the build id this engine's game belongs to (SIM_ID of the multiplayer page from the same src/). The
// game text is read once, here: every worker, also one started after an exit, runs that game, and
// a src/ that does not compile makes createEngine throw.
//
// Rejections: a call for a network that is not loaded (code 'NO_REALM'), a malformed call (code 'ERR': a load that
// names no network, or one twice, changes nothing), and every call pending on a worker that exits (out of memory, a
// crash): Error('worker exited'), code 'EXIT'; that worker's sessions are gone and the host loads them again (4.7). Every
// call is answered or rejected; none waits for ever. An exit counts against the sessions whose load was under way on
// that worker (a save that crashes or overflows it), or, when none was, against every session it held. A session with
// two exits against it within an hour gets a worker of its own; when that one exits too, its loads reject with code
// 'GAVE_UP' until the engine is made again (the server restarts). A session stays on its worker while a load of it is
// under way, so an unload and a load sent back to back (a language change) land on the same worker.
// names no network, or one twice, changes nothing), every call pending on a worker that exits (out of memory, a crash):
// Error('worker exited'), code 'EXIT', and every call while no worker can start (code 'SIM_DOWN', 503 for the host).
// Every call is answered or rejected; none waits for ever.
// Lost networks. A worker that exits takes the realms of every session on it, also of those with no call pending, and
// the worker drops a network whose realm cannot be built again after a failure. Such a network answers NO_REALM: for a
// network the host believes loaded, NO_REALM means "load it again from its checkpoint" (4.7). opts.onLost(code, nids,
// why) says it as it happens ('exit', or the worker's reason).
// Blame. An exit counts against the session the worker was running (a call or a step that crashed or overflowed it),
// else against those whose load was under way, else against every session it held; a worker that never started
// blames nobody. A session with two exits against it within an hour gets a worker of its own (at most opts.maxOwn of
// them, 4 by default: past that it is given up); when that one exits too, its loads reject with code 'GAVE_UP' until
// the engine is made again (the server restarts). A worker that exits before it is ready is started again after a
// pause that doubles; after 5 such starts in a row the engine is down (SIM_DOWN). A session stays on its worker while
// a load of it is under way, so an unload and a load sent back to back (a language change) land on the same worker.
// Epochs: every realm load mints one (a load, a begin, the fresh realm after a timeout); loaded, pulled, actDone and
// subbed carry the realm's current one. Versions are the realm's: only a pull (or an action's own pull) makes one.
//
// Tests only, with createEngine({test: {seed, now, step} | true}): engine.eval(code, nid, src) runs code in a realm
// ({value} | {error} | {timeout, epoch}); engine.mem() reports each worker's heap; engine.kill(i) ends pool worker i,
// engine.killOf(code) the worker of that session; engine.workerOf(code) is its pool index (-1: none, or its own),
// engine.dedicated(code) whether it has a worker of its own.
// ({value} | {error} | {timeout, epoch}); engine.kill(i) ends pool worker i, engine.killOf(code) the worker of that
// session; engine.workerOf(code) is its pool index (-1: none, or its own), engine.dedicated(code) whether it has a
// worker of its own, engine.running(i) the session pool worker i is running as far as the engine knows.
// opts.test.startFail: no worker starts; opts.startBackoffMs: the first pause before starting one again (1000).
'use strict';
const path = require('node:path');
const vm = require('node:vm');
const { Worker } = require('node:worker_threads');
const page = require('./page');
const { gameSource } = require('./mp-sim-realm');
const FILE = path.join(__dirname, 'mp-sim-worker.js');
const HOUR = 3600e3;
@ -61,10 +78,21 @@ function createEngine(opts = {}) {
}
const N = Math.max(1, Math.floor(+opts.workers || 2));
const heapMb = Math.max(64, Math.floor(+opts.heapMb || 1536));
const maxOwn = Math.max(0, Math.floor(opts.maxOwn !== undefined ? +opts.maxOwn || 0 : 4));
const backoff = Math.max(1, +opts.startBackoffMs || 1000);
const log = typeof opts.log === 'function' ? opts.log : () => {};
const lostTo = (code, nids, why) => { if (typeof opts.onLost === 'function' && nids.length) try { opts.onLost(code, nids, why); } catch (e) { log('error', 'mp-sim: onLost threw: ' + (e && e.message)); } };
const test = opts.test || null;
const W = { root: opts.root, page: opts.page, test, strict: opts.strict !== undefined ? !!opts.strict : process.env.SIM_STRICT === '1', timeouts: opts.timeouts };
let seq = 0, closed = false;
// The game, read once, here: a src/ that does not compile throws now, and every worker runs the game simId names.
const root = opts.root || page.ROOT, src = gameSource({ root, page: opts.page });
try { new vm.Script(src, { filename: 'game.js' }); } catch (e) { throw new Error(`mp-sim: the game in ${path.join(root, 'src')} does not compile: ${e.message}`); }
const simId = page.simId(page.assemble({ src: path.join(root, 'src'), mp: true }).html);
const W = { root: opts.root, page: opts.page, src, simId, test, strict: opts.strict !== undefined ? !!opts.strict : process.env.SIM_STRICT === '1', timeouts: opts.timeouts };
let seq = 0, closed = false, down = null, fails = 0;
const DOWN = () => Object.assign(new Error(`server-run play is down: ${down}`), { code: 'SIM_DOWN' });
const CLOSED = () => Object.assign(new Error('engine closed'), { code: 'CLOSED' });
// Why no call can go out now: closed, or down (else null).
const shut = () => (closed ? CLOSED() : down ? DOWN() : null);
const pool = [], own = new Map(), placed = new Map(), exits = new Map(), gaveUp = new Set();
// A worker's realms (sessions: code → its loaded nids), and for placement the networks of its loads under way too
// (loading: code → how many).
@ -72,12 +100,16 @@ function createEngine(opts = {}) {
const count = (w) => size(w) + [...w.loading.values()].reduce((a, b) => a + b, 0);
function spawn(dedicated = false) {
const w = { worker: null, pending: new Map(), sessions: new Map(), loading: new Map(), dedicated, dead: false };
const w = { worker: null, pending: new Map(), sessions: new Map(), loading: new Map(), dedicated, dead: false, ready: false, fatal: null, running: null };
w.worker = new Worker(FILE, { workerData: { mpSim: W }, resourceLimits: { maxOldGenerationSizeMb: heapMb } });
w.worker.on('message', (m) => {
if (m.log) { log(m.log[0], m.log[1]); return; }
if (m.fatal) { log('error', 'mp-sim worker could not start: ' + m.fatal); return; }
if (m.ready) return;
if (m.fatal) { w.fatal = String(m.fatal).split('\n')[0].slice(0, 300); return; }
if (m.ready) { w.ready = true; fails = 0; return; }
// The session it runs from now until its next reply: whose doing an exit then is.
if ('run' in m) { w.running = m.run; return; }
if (m.lost) { const s = w.sessions.get(m.lost.code); if (s) s.delete(m.lost.nid); lostTo(m.lost.code, [m.lost.nid], m.lost.why); return; }
w.running = null;
const p = w.pending.get(m.id);
if (!p) return;
w.pending.delete(m.id);
@ -94,25 +126,36 @@ function createEngine(opts = {}) {
function died(w, c) {
if (w.dead) return;
w.dead = true;
// Whose exit it is: the sessions whose load was under way (a save that crashes or overflows its worker), else every
// session it held. The others lose their realms (the host loads them again) without an exit against them.
const held = new Set([...w.sessions.keys(), ...w.loading.keys()]), blamed = new Set(w.loading.size ? w.loading.keys() : held);
// Whose exit it is: the session it was running (a call or a step that crashed or overflowed it), else those whose
// load was under way, else every session it held; nobody's when it never started. The others lose their realms
// (the host loads them again) without an exit against them.
const held = new Set([...w.sessions.keys(), ...w.loading.keys()]);
const blamed = new Set(!w.ready ? [] : w.running && held.has(w.running) ? [w.running] : w.loading.size ? w.loading.keys() : held);
const e = Object.assign(new Error('worker exited'), { code: 'EXIT', exitCode: c });
for (const p of w.pending.values()) p.no(e);
w.pending.clear();
const t = Date.now();
for (const code of held) {
if (placed.get(code) === w) placed.delete(code);
lostTo(code, [...(w.sessions.get(code) || [])], 'exit');
if (!blamed.has(code)) continue;
if (w.dedicated) { gaveUp.add(code); own.delete(code); log('error', `mp-sim: session ${code} took down its own worker too; not loading it again until a restart`); continue; }
exits.set(code, (exits.get(code) || []).filter((x) => t - x < HOUR).concat(t));
}
w.sessions.clear(); w.loading.clear();
if (!closed) {
log('warn', `mp-sim: a worker exited (code ${c}); its sessions load again from their checkpoints`);
const i = pool.indexOf(w);
if (i >= 0) pool[i] = spawn();
for (const [k, x] of own) if (x === w) own.delete(k);
if (closed) return;
const i = pool.indexOf(w);
if (!w.ready) {
// It never started (the game does not run in a worker): not again at once, but after a pause that doubles; after
// 5 such starts in a row, server-run play is down until the engine is made again.
if (++fails >= 5) { if (!down) { down = w.fatal || `a worker exited before it started (code ${c})`; log('error', `mp-sim: no worker can start (${down}); server-run play is down until a restart`); } return; }
log('warn', `mp-sim: a worker could not start (${w.fatal || 'code ' + c}); trying again in ${backoff * 2 ** (fails - 1)} ms`);
if (i >= 0) setTimeout(() => { if (!closed && !down && pool[i] === w) pool[i] = spawn(); }, backoff * 2 ** (fails - 1)).unref();
return;
}
log('warn', `mp-sim: a worker exited (code ${c}); its sessions load again from their checkpoints`);
if (i >= 0) pool[i] = spawn();
}
for (let i = 0; i < N; i++) pool.push(spawn());
@ -121,15 +164,25 @@ function createEngine(opts = {}) {
let w = placed.get(code);
if (w && !w.dead) return w;
if (!create) return null;
if (down) throw DOWN();
if (gaveUp.has(code)) throw Object.assign(new Error(`session ${code} kept failing its worker`), { code: 'GAVE_UP' });
if ((exits.get(code) || []).filter((x) => Date.now() - x < HOUR).length >= 2) {
if (own.size >= maxOwn) {
gaveUp.add(code);
log('error', `mp-sim: session ${code} keeps failing its worker, and ${own.size} sessions have workers of their own already; not loading it again until a restart`);
throw Object.assign(new Error(`session ${code} kept failing its worker`), { code: 'GAVE_UP' });
}
w = spawn(true); own.set(code, w);
} else w = pool.reduce((a, b) => (count(b) < count(a) ? b : a));
} else {
const live = pool.filter((x) => !x.dead);
if (!live.length) throw Object.assign(new Error('no worker is up yet: try again'), { code: 'SIM_DOWN' });
w = live.reduce((a, b) => (count(b) < count(a) ? b : a));
}
placed.set(code, w);
return w;
}
function send(w, m) {
if (closed) return Promise.reject(Object.assign(new Error('engine closed'), { code: 'CLOSED' }));
if (closed) return Promise.reject(CLOSED());
if (!w || w.dead) return Promise.reject(Object.assign(new Error('worker exited'), { code: 'EXIT' }));
const id = ++seq;
return new Promise((ok, no) => {
@ -140,12 +193,13 @@ function createEngine(opts = {}) {
});
}
const noRealm = (code, nid) => Promise.reject(Object.assign(new Error(`no realm ${code}/${nid}`), { code: 'NO_REALM' }));
const to = (code, nid, m) => { const w = workerOf(code, false); return w ? send(w, { code, nid, ...m }) : noRealm(code, nid); };
const to = (code, nid, m) => { const x = shut(); if (x) return Promise.reject(x); const w = workerOf(code, false); return w ? send(w, { code, nid, ...m }) : noRealm(code, nid); };
const mine = (w, code) => { let s = w.sessions.get(code); if (!s) w.sessions.set(code, s = new Set()); return s; };
const engine = {
simId: null,
simId,
load(code, nets) {
{ const x = shut(); if (x) return Promise.reject(x); }
if (!Array.isArray(nets) || !nets.length) return Promise.reject(Object.assign(new Error('load: give the networks to load'), { code: 'ERR' }));
let w;
try { w = workerOf(code, true); } catch (e) { return Promise.reject(e); }
@ -160,6 +214,7 @@ function createEngine(opts = {}) {
}, (e) => { done(); throw e; });
},
day(code, { d, speed = 1, last = 1, alone = [], nets = [], mute = false } = {}) {
{ const x = shut(); if (x) return Promise.reject(x); }
const w = workerOf(code, false);
if (!w) return Promise.reject(Object.assign(new Error(`no session ${code} here`), { code: 'NO_REALM' }));
return send(w, { t: 'day', code, d, speed, last, alone, nets, mute });
@ -168,11 +223,12 @@ function createEngine(opts = {}) {
ask(code, nid, msg) { return to(code, nid, { t: 'ask', msg }); },
pull(code, nid, { snap = false, views } = {}) { return to(code, nid, { t: 'pull', snap: !!snap, ...(views ? { views } : {}) }); },
sub(code, nid, { views = [] } = {}) { return to(code, nid, { t: 'sub', views }); },
prefs(code, nid, { prefs } = {}) { return to(code, nid, { t: 'prefs', prefs }).then(() => undefined); },
prefs(code, nid, { prefs } = {}) { return to(code, nid, { t: 'prefs', prefs }).then((r) => (r && r.t === 'err' ? r : undefined)); },
greet(code, nid) { return to(code, nid, { t: 'greet' }); },
save(code, nid) { return to(code, nid, { t: 'save' }); },
card(code, nid) { return to(code, nid, { t: 'card' }); },
unload(code, nid) {
if (closed) return Promise.reject(CLOSED());
const w = workerOf(code, false);
if (!w) return Promise.resolve({ t: 'unloaded', code, ...(nid !== undefined ? { nid } : {}) });
return send(w, { t: 'unload', code, ...(nid !== undefined ? { nid } : {}) }).then((r) => {
@ -185,6 +241,8 @@ function createEngine(opts = {}) {
return r;
});
},
has(code, nid) { const w = placed.get(code), s = w && !w.dead && w.sessions.get(code); return !!(s && s.has(nid)); },
mem() { return Promise.all(pool.filter((w) => !w.dead).map((w) => send(w, { t: 'mem' }))); },
stats() {
const ws = [...pool, ...own.values()].filter((w) => !w.dead);
return { workers: ws.length, realms: ws.reduce((a, w) => a + size(w), 0), sessions: ws.reduce((a, w) => a + w.sessions.size, 0) };
@ -198,13 +256,12 @@ function createEngine(opts = {}) {
};
if (test) {
engine.eval = (code, nid, src, ms) => to(code, nid, { t: 'eval', src: String(src), ...(ms ? { ms } : {}) });
engine.mem = () => Promise.all(pool.filter((w) => !w.dead).map((w) => send(w, { t: 'mem' })));
engine.kill = (i = 0) => { const w = pool[i]; if (w && !w.dead) w.worker.terminate(); };
engine.killOf = (code) => { const w = placed.get(code); if (w && !w.dead) w.worker.terminate(); };
engine.workerOf = (code) => pool.indexOf(placed.get(code));
engine.dedicated = (code) => !!(placed.get(code) && placed.get(code).dedicated);
engine.running = (i = 0) => (pool[i] ? pool[i].running : null);
}
{ const page = require('./page'); engine.simId = page.simId(page.assemble({ src: path.join(opts.root || page.ROOT, 'src'), mp: true }).html); }
return engine;
}

View file

@ -9,13 +9,16 @@
// (globalThis.__in), the answer comes back as one string, and no object crosses between host and realm. An answer is
// a JSON header, optionally followed by more parts, each on its own line (JSON text never holds a raw newline):
// srvPull answers `{"v":3,"parts":["upd","snap"]}\n<upd JSON>\n<snap JSON>`, ready to write to a stream.
// A call that throws inside the realm answers {"srvErr": stack}; a call that runs over its time limit throws
// ERR_SCRIPT_EXECUTION_TIMEOUT here, and the realm must not be used for play again (a timeout skips finally blocks).
// A call that throws inside the realm answers {"srvErr": stack}; a call after which the realm's built-ins are not what
// they were at load (a prototype written through) answers {"srvErr", "polluted":1}, and the realm must not be used for
// play again; a call that runs over its time limit throws ERR_SCRIPT_EXECUTION_TIMEOUT here, and the realm must not be
// used for play again either (a timeout skips finally blocks).
//
// gameSource({root, page}) the game script (the largest inline <script> of the assembled page)
// dictSource(lang, root) the source dictionary of a language, or null for English
// prelude(opts), EPILOGUE the realm's own code around the game, as plain strings
// compileCode({root, page}) {game, epilogue, simId} as vm.Scripts, once per process (a worker)
// compileCode({root, page, src, simId}) {game, epilogue, simId} as vm.Scripts, once per process (a worker); src and
// simId: the game text and build id the engine read once (every worker runs the same)
// new Realm({code, lang, dict, test, log, nid}) a realm, the game loaded; .call(name, input, ms), .raw(...)
// parts(text) [header, ...parts] of an answer
'use strict';
@ -74,8 +77,22 @@ const _txt=h=>String(h).replace(/<[^>]*>/g,'').replace(/&(#x[0-9a-f]+|#\\d+|[a-z
const _el=(tag,init)=>{const t={tagName:String(tag||'div').toUpperCase(),_h:'',hidden:true,value:'',firstChild:null,lastChild:null,nextSibling:null,previousSibling:null,parentNode:null,children:[],childNodes:[],attributes:[],...init};
return new Proxy(t,{get(o,k){if(k==='innerHTML')return o._h;if(k==='textContent')return _txt(o._h);
if(k==='firstElementChild'){const m=/^\\s*<([a-zA-Z][a-zA-Z0-9]*)\\b[^>]*>([\\s\\S]*?)<\\/\\1>/.exec(o._h);return m?_el(m[1],{_h:m[2]}):null;}
if(k==='querySelector'||k==='querySelectorAll')return s=>_q(o._h,s,k==='querySelectorAll');if(k==='classList')return _cls(o);
if(k in o)return o[k];return _n;},
set(o,k,v){if(k==='innerHTML')o._h=String(v);else if(k==='textContent')o._h=String(v).replace(/[&<>]/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;'})[c]);else o[k]=v;return true;}});};
// Which window is open, as the game asks it (pieRe, bstdShown, routingRefresh, the sea survey: a class, or a [data-x] /
// [data-x="v"] marker inside the card), is answered from what was written, never with the inert proxy, which would say
// every window is open. Other selectors match nothing; matches carry their data-* attributes. (closest() stays inert:
// a stand-in has no ancestors, and the page's own start asks one for its chip.)
const _cls=o=>{const g=()=>String(o.className||'').split(/\\s+/).filter(Boolean),p=a=>{o.className=a.join(' ');};
return new Proxy({contains:c=>g().includes(String(c)),add:(...c)=>p([...new Set([...g(),...c.map(String)])]),remove:(...c)=>{const x=c.map(String);p(g().filter(y=>!x.includes(y)));},
toggle:(c,f)=>{c=String(c);const on=f===undefined?!g().includes(c):!!f;p(on?[...new Set([...g(),c])]:g().filter(y=>y!==c));return on;}},{get:(t,k)=>k in t?t[k]:_n});};
const _cc=x=>x.replace(/-([a-z])/g,(q,c)=>c.toUpperCase());
const _ds=a=>{const d=Object.create(null);a.replace(/\\sdata-([a-z0-9-]+)(?:\\s*=\\s*(?:"([^"]*)"|'([^']*)'|([^\\s"'>]+)))?/gi,(m,k,x,y,z)=>{d[_cc(k.toLowerCase())]=_txt(x??y??z??'');return m;});return d;};
const _q=(h,sel,all)=>{const m=/^\\[data-([a-z0-9-]+)(?:=(?:"([^"]*)"|'([^']*)'|([^\\]"']*)))?\\]$/i.exec(String(sel).trim()),out=[];
if(m){const k=_cc(m[1].toLowerCase()),v=m[2]??m[3]??m[4],re=/<([a-zA-Z][a-zA-Z0-9]*)(\\s[^>]*)?>/g;let t;
while((t=re.exec(h))){const d=_ds(t[2]||'');if(Object.hasOwn(d,k)&&(v===undefined||d[k]===v)){out.push(_el(t[1],{dataset:d}));if(!all)break;}}}
return all?out:out[0]||null;};
const _byId=new Map();
const _doc=new Proxy({},{get(o,k){if(k==='createElement')return tag=>_el(tag);if(k==='getElementById')return id=>{if(!_byId.has(id))_byId.set(id,_el('div',{id}));return _byId.get(id);};return _n;},set:()=>true});
self.__srvEl=_byId;self.__srvCoarse=false;
@ -94,11 +111,11 @@ for(const [k,v] of Object.entries({document:_doc,localStorage:_store(),sessionSt
/* The epilogue: the server's side of a network, in the game's global scope. It only uses the game's own functions and
the tables of mp-sim-acts.js, written in as data. Everything a page needs to know comes out as fx or in the answers. */
const TABLES = JSON.stringify({ SEND: ACTS.SEND, ASK: ACTS.ASK, CONFIRM: ACTS.CONFIRM, DRAFTS: ACTS.DRAFTS, PREFS: ACTS.PREFS });
const TABLES = JSON.stringify({ SEND: ACTS.SEND, ASK: ACTS.ASK, CONFIRM: ACTS.CONFIRM, DRAFTS: ACTS.DRAFTS, DRAFT_TYPES: ACTS.DRAFT_TYPES, PREFS: ACTS.PREFS, TEXT: ACTS.TEXT, WINDOWS: ACTS.WINDOWS });
const EPILOGUE = `'use strict';
const SIMT=${TABLES},SIM_SEND=new Set(SIMT.SEND),SIM_ASK=new Set(SIMT.ASK),SIM_KEEP=new Set(['badge','modal']);
const SIM_RNEVER=new Set(['ef','fl','efp','flc','efg','egDesc','l2route','bbF','peered','cityBlocked']);
const SIM_DRAFT=['pending','route','via','cnPrem','dfSrc','dfSys','seaDesign','seaterminal','seaengineering','decom','slop','pieIns','pieLas'];
const SIM_DRAFT=Object.keys(SIMT.DRAFT_TYPES);
// The realm's own memory: epoch and version of what it sent (5.10), the last keys sent, the fx and actions since the
// last pull, the marks, and what the running call saw (a window opened or closed, a refusal toast, an auto-pause).
const SIMR={epoch:'',v:0,seen:false,base:null,rSent:null,fx:[],acts:[],floats:0,alone:false,last:1,pause:false,modal:null,closed:false,bad:false,
@ -106,6 +123,17 @@ const SIMR={epoch:'',v:0,seen:false,base:null,rSent:null,fx:[],acts:[],floats:0,
const srvOwn=(o,k)=>Object.prototype.hasOwnProperty.call(o,k);
const srvOut=(h,...p)=>[JSON.stringify(h),...p].join('\\n');
function srvLog(t){console.warn(String(t).slice(0,600));}
/* The realm's built-ins as the game left them at load. A click that writes through a prototype (a city named
__proto__ or constructor) changes them, and every for…in, the game's and this file's, would carry that from then on.
The trampoline checks after every call; a realm whose built-ins changed is replaced like one that timed out. */
const srvFp=(()=>{const n=Object.getOwnPropertyNames,L=[Object,Object.prototype,Array,Array.prototype,Function.prototype,String.prototype,Number.prototype,Boolean.prototype,Map.prototype,Set.prototype];
return ()=>{for(const k in {})return 'Object.prototype.'+k;for(const k in [])return 'Array.prototype.'+k;return L.map(o=>n(o).length).join(',');};})();
const SIM_FP0=srvFp();
function srvDirty(){const f=srvFp();return f===SIM_FP0?'':/^(Object|Array)\\./.test(f)?f:'a built-in has a new property';}
// Strings no arg names: written through as a key, they reach a prototype. Free text (TEXT) may say them.
const SIM_BADV=new Set(['__proto__','constructor','prototype']);
const srvBadV=v=>typeof v==='string'?SIM_BADV.has(v):Array.isArray(v)?v.some(srvBadV):!!v&&typeof v==='object'&&Object.keys(v).some(k=>SIM_BADV.has(k)||srvBadV(v[k]));
const srvBadArgs=(act,a)=>{const t=srvOwn(SIMT.TEXT,act)?SIMT.TEXT[act]:{};return Object.keys(a).some(k=>SIM_BADV.has(k)||(!srvOwn(t,k)&&srvBadV(a[k])));};
/* fx: a ring of 200; when it overflows, the oldest go first, but badges and windows (win, lose) stay, at most 32. */
function srvFx(f){const q=SIMR.fx;q.push(f);
@ -149,7 +177,7 @@ srvOn=function(){return false;};
UI.clockLocked=true;UI.started=true;UI.pitched=true;UI.speed=1;
/* ---- S as top-level keys, R as the page gets it ---- */
function srvKeyJson(){const o={};for(const k of Object.keys(S)){const j=JSON.stringify(S[k]);if(j!==undefined)o[k]=j;}return o;}
function srvKeyJson(){const o=Object.create(null);for(const k of Object.keys(S)){const j=JSON.stringify(S[k]);if(j!==undefined)o[k]=j;}return o;}
function srvJoin(K){let t='{',f=true;for(const k in K){t+=(f?'':',')+JSON.stringify(k)+':'+K[k];f=false;}return t+'}';}
// Slim R (5.5): rwPack's view without the flow tables, routing's own inputs and what goes on demand; the top 4 networks
// per transit port, the top 8 over all of them, numbers to 6 significant digits. Packed once per R object.
@ -194,11 +222,15 @@ function srvOrdOut(txt,K){const u0=uidN,r0=S.rng;let B;
function srvOrdIn(o){if(!o||typeof o!=='object'||Array.isArray(o))return;for(const k of Object.keys(o))if(srvOwn(S,k)&&srvSame(o[k],S[k]))S[k]=o[k];}
// rt as JSON text: R's own route memory, its routing point against S as K holds it (when R was routed here), ord when
// given, and the desk's memory sl.
function srvRtText(r,K,ord,sl){const p=K&&SIM_RIN.get(r);let t=JSON.stringify(srvRtOut(r)).slice(0,-1);
function srvRtText(r,K,ord,sl,rk){const p=K&&SIM_RIN.get(r);let t=JSON.stringify(srvRtOut(r)).slice(0,-1);
if(p){const at=[],gone=[];for(const k in p.k)if(K[k]!==p.k[k])at.push(JSON.stringify(k)+':'+p.k[k]);for(const k in K)if(!srvOwn(p.k,k))gone.push(k);
t+=',"pt":'+p.rt.slice(0,-1)+',"at":{'+at.join(',')+'},"gone":'+JSON.stringify(gone)+'}';}
return t+(ord?',"ord":'+ord:'')+',"sl":'+JSON.stringify(sl||[])+'}';}
const srvKeysOf=s=>{const o=JSON.parse(s),K={};for(const k of Object.keys(o)){const j=JSON.stringify(o[k]);if(j!==undefined)K[k]=j;}return K;};
return t+(ord?',"ord":'+ord:'')+(rk?',"rk":true':'')+',"sl":'+JSON.stringify(sl||[])+'}';}
// rk: R is the routing of S as it is (routeIfChanged's memory, UI.rk and UI.rkR, against K: routeKey() is the join of
// K without RK_SKIP). A fresh realm given it skips the routing of a click that changes nothing routing reads, as this
// one would; without it that click routes again, and compute() is not always a fixed point.
const srvRkOk=(r,K,rk,rkR)=>{if(!K||rkR!==r||typeof rk!=='string')return false;let t='{',f=true;for(const k in K){if(RK_SKIP.has(k))continue;t+=(f?'':',')+JSON.stringify(k)+':'+K[k];f=false;}return t+'}'===rk;};
const srvKeysOf=s=>{const o=JSON.parse(s),K=Object.create(null);for(const k of Object.keys(o)){const j=JSON.stringify(o[k]);if(j!==undefined)K[k]=j;}return K;};
// The S of a routing point (pt), one JSON per key: the loaded S with the keys of at put back and those of gone left out.
function srvAtK(pt){if(!pt||typeof pt!=='object'||!pt.at||typeof pt.at!=='object'||Array.isArray(pt.at)||!Array.isArray(pt.gone))return null;
const K=srvKeyJson();for(const k of Object.keys(pt.at))K[k]=JSON.stringify(pt.at[k]);for(const k of pt.gone)if(typeof k==='string')delete K[k];return K;}
@ -214,8 +246,10 @@ function srvPointIn(r,pt){const K=srvAtK(pt);if(K)SIM_RIN.set(r,{k:K,rt:JSON.str
function srvAttacks(){if(!R||!Array.isArray(R.ddos))return;const as=ddosAll(S,false);for(const x of R.ddos)if(x.attack)x.attack=as.find(a=>a.id===x.attack.id)||null;}
// mark: rt of the marked R against the marked S (a day's mark has it at once); else of R against S now.
function srvRt(inp){const m=inp?JSON.parse(inp):{},k=SIMR.mark;
if(!m.mark)return srvRtText(R,srvKeyJson(),'',srvSlOut());
return SIMR.markRt||(k?srvRtText(k.R,SIMR.markK||srvKeysOf(k.s),'',k.sl):srvRtText(R,null,'',[]));}
if(!m.mark){const K=srvKeyJson();return srvRtText(R,K,'',srvSlOut(),srvRkOk(R,K,UI.rk,UI.rkR));}
if(SIMR.markRt)return SIMR.markRt;
if(!k)return srvRtText(R,null,'',[]);
const K=SIMR.markK||srvKeysOf(k.s);return srvRtText(k.R,K,'',k.sl,srvRkOk(k.R,K,k.ui.rk,k.ui.rkR));}
/* R itself, whole, for the fresh realm that replaces this one after a timeout (routing it again from its point costs a
routing; the old realm still answers), the way the route worker sends it (rwPack/rwUnpack), as text: Maps, Sets, typed
arrays and non-finite numbers tagged. A checkpoint carries the point instead: 20 KB gzip, where R is 375 KB. */
@ -225,16 +259,30 @@ const srvDec=(k,v)=>!v||typeof v!=='object'||Array.isArray(v)?v:'__n' in v?Numbe
function srvRPack(inp){const m=inp?JSON.parse(inp):{},r=m.mark&&SIMR.mark?SIMR.mark.R:R;if(!r||!r.efp||!r.flc)return '';const {o,p}=rwPack(r);return JSON.stringify({o,p},srvEnc);}
function srvSetPrefs(p){if(!p||typeof p!=='object')return;for(const k of SIMT.PREFS)if(typeof p[k]==='boolean')PREFS[k]=p[k];}
/* The window the page has open (ctx.modal; ctx.win names it, mp-sim-acts.js WINDOWS): the stand-in #modal is that
window as the game asks it (its class, its marker), with the view state it draws from (checked here). No win: no
window, or one nothing asks about. The view state of an earlier request never stays. */
const SIM_WINUI=[...new Set(Object.values(SIMT.WINDOWS).flatMap(w=>w.ui||[]))];
const SIM_WINOK={opsCity:v=>srvOwn(S.pops,v),pieCity:v=>srvOwn(S.pops,v),pieTab:v=>PIE_TABS.includes(v)};
function srvWin(c){const w=c&&c.win&&typeof c.win==='object'?c.win:null,W=w&&typeof w.k==='string'&&srvOwn(SIMT.WINDOWS,w.k)?SIMT.WINDOWS[w.k]:null,m=$('modal');
for(const k of SIM_WINUI)delete UI[k];
m.hidden=!((c&&c.modal===true)||W);m.className=W?W.c:'';$('modalCard').innerHTML=W&&W.sel?'<div '+W.sel.slice(1,-1)+'></div>':'';
if(W&&W.ui&&w.ui&&typeof w.ui==='object')for(const k of W.ui){const v=w.ui[k];if(typeof v==='string'&&srvOwn(SIM_WINOK,k)&&SIM_WINOK[k](v))UI[k]=v;}}
function srvWinOff(){const m=$('modal');m.hidden=true;m.className='';$('modalCard').innerHTML='';}
function srvFresh(epoch){SIMR.epoch=String(epoch||'');SIMR.v=0;SIMR.seen=false;SIMR.fx=[];SIMR.acts=[];SIMR.floats=0;SIMR.rSent=new WeakRef(R);SIMR.base=srvKeyJson();SIMR.mark=null;SIMR.markK=null;SIMR.markRt=null;
UI.sel=null;UI.confirm=null;UI.pending=null;UI.started=true;UI.pitched=true;UI.clockLocked=true;$('modal').hidden=true;}
UI.sel=null;UI.confirm=null;UI.pending=null;UI.started=true;UI.pitched=true;UI.clockLocked=true;srvWinOff();}
/* srvLoad: a network from its save (a checkpoint, or the mark of a realm that timed out) and its rt: S in its live key
order, R routed again from its routing point, the desk's memory; then the days to come are the ones the old realm
had. A realm that replaces one that timed out gets the old realm's R itself when it could still pack it (rpack); live:
the save is the old realm's own S as text (a mark), key order and all. An rt without a point (an older checkpoint)
seeds R with its memory before compute(), the way the route worker does: close, not always the same. */
seeds R with its memory before compute(), the way the route worker does: close, not always the same. A damaged rt
never breaks a save that loads: R is routed from nothing then (close, logged). fx: the fx a fresh realm takes over
from the one it replaces (srvFxOut). Only a server network loads: a Normal game with the session's marks (S.mp.sim;
a local save is stamped on its way in, 5.2), else the load fails with the reason. */
function srvLoad(inp){const m=JSON.parse(inp),rt=m.rt&&typeof m.rt==='object'?m.rt:{};SIMR.strict=!!m.strict;
S=migrate(JSON.parse(m.save));
{const no=S.mode!=='normal'||S.scn?'server networks are Normal games':!S.mp||S.mp.sim!==1?'not a server network (S.mp.sim)':'';if(no)throw new Error('srvLoad: '+no);}
if(typeof S.rng!=='number')S.rng=(m.rng|0)||1;
if(Number.isInteger(m.uidN)&&m.uidN>0)uidN=m.uidN;
srvSetPrefs(m.prefs);
@ -242,9 +290,11 @@ function srvLoad(inp){const m=JSON.parse(inp),rt=m.rt&&typeof m.rt==='object'?m.
if(self.__srvClock&&typeof m.clock==='number')__srvClock(m.clock);
srvOrdIn(m.live?JSON.parse(m.save):rt.ord);
if(typeof m.rpack==='string'&&m.rpack){const x=JSON.parse(m.rpack,srvDec);R=rwUnpack(x.o,x.p);UI.hotMap=hotByCity();srvPointIn(R,rt.pt);}
else if(!srvReroute(rt.pt)){R=srvRtIn(rt);compute();}
else if(!srvReroute(rt.pt)){try{R=srvRtIn(rt);compute();}catch(e){srvLog('the route memory does not load ('+String(e&&e.message||e).slice(0,200)+'): routed from nothing');R=emptyR();compute();}}
if(rt.rk===true){UI.rk=routeKey();UI.rkR=R;}
srvSlIn(rt.sl);
srvFresh(m.epoch);
if(m.fx&&typeof m.fx==='object'&&Array.isArray(m.fx.fx)){SIMR.fx=m.fx.fx.filter(f=>Array.isArray(f)&&typeof f[0]==='string').slice(-200);SIMR.floats=Math.min(40,Math.max(0,m.fx.floats|0));}
// Whatever a loaded network says before its first snapshot (days nobody watched) is older than that snapshot: the
// first pull handles it like any (5.11). Only a new network's welcome waits for its first page (srvBegin).
SIMR.seen=true;
@ -269,9 +319,9 @@ function srvPrefs(inp){const m=JSON.parse(inp);srvSetPrefs(m.prefs);return '{}';
function srvMark(inp){const m=inp?JSON.parse(inp):{};let s,K=null;
if(m.keys||m.day){K=srvKeyJson();s=srvJoin(K);}else s=JSON.stringify(S);
SIMR.markK=m.keys?K:null;const sl=srvSlOut();
SIMR.mark={s,uidN,rng:S.rng,R,sl,...srvClockNow(),fx:SIMR.fx.slice(),floats:SIMR.floats,ui:{sel:UI.sel,tab:UI.tab,left:UI.left,confirm:UI.confirm,pending:UI.pending}};
SIMR.mark={s,uidN,rng:S.rng,R,sl,...srvClockNow(),fx:SIMR.fx.slice(),floats:SIMR.floats,ui:{sel:UI.sel,tab:UI.tab,left:UI.left,confirm:UI.confirm,pending:UI.pending,rk:UI.rk,rkR:UI.rkR}};
// As text: nothing the day does reaches it.
const rtJ=m.day?srvRtText(R,K,'',sl):'';SIMR.markRt=rtJ||null;const h=JSON.stringify({uidN,day:S.day,...srvClockNow()});
const rtJ=m.day?srvRtText(R,K,'',sl,srvRkOk(R,K,UI.rk,UI.rkR)):'';SIMR.markRt=rtJ||null;const h=JSON.stringify({uidN,day:S.day,...srvClockNow()});
return (rtJ?h.slice(0,-1)+',"rt":'+rtJ+'}':h)+'\\n'+s;}
/* srvRestore: back to the mark, in place (after a throw: the finally blocks ran). S is parsed back only when it
changed, so the S object stays on every normal path; R is the marked object, its attacks looked up again in a parsed
@ -284,8 +334,11 @@ function srvRestore(inp){const k=SIMR.mark;if(!k)return '{"none":1}';
// so the marked one is still the R the network had: route memory and compute() would only give one close to it.
R=k.R;if(parsed)srvAttacks();
UI.hotMap=hotByCity();UI.slTried=k.sl.length?{s:S,m:new Map(k.sl)}:null;
Object.assign(UI,k.ui);SIMR.fx=k.fx.slice();SIMR.floats=k.floats;SIMR.modal=null;SIMR.closed=false;$('modal').hidden=true;
Object.assign(UI,k.ui);SIMR.fx=k.fx.slice();SIMR.floats=k.floats;SIMR.modal=null;SIMR.closed=false;srvWinOff();
return JSON.stringify({parsed});}
// The fx a fresh realm takes over from this one: the mark's after a failed call (its own fx are dropped, 4.3), else
// (a read that ran out of time) all of them. Badges and windows wait for a page for days; a timeout does not lose them.
function srvFxOut(inp){const m=inp?JSON.parse(inp):{},k=SIMR.mark;return JSON.stringify(m.mark&&k?{fx:k.fx,floats:k.floats}:{fx:SIMR.fx,floats:SIMR.floats});}
/* srvDay: one day of this network, as the page runs it (dayTick, then the day's end render), then the tutorial. mute:
a catch-up day, whose fx are dropped (badges and windows stay). */
@ -339,10 +392,11 @@ function srvAct(inp){const m=JSON.parse(inp),act=String(m.act),ent={rid:String(m
let outcome,why;const K0=SIMR.markK;
const done=(o,w)=>{outcome=o;why=w;};
if(!SIM_SEND.has(act)||!srvOwn(ACT,act)||typeof ACT[act]!=='function')done('rejected','not an act the server runs');
else if(srvBadArgs(act,args)||srvBadV(m.ui))done('rejected','an arg names a built-in');
else{const w=srvDrafts(act,m.ui,args);
if(w)done('rejected',w);
else{const c=m.ctx&&typeof m.ctx==='object'?m.ctx:{};
UI.sel=srvSel(c.sel);if(typeof c.tab==='string')UI.tab=c.tab;if(typeof c.left==='string')UI.left=c.left;srvView(c);$('modal').hidden=!c.modal;
UI.sel=srvSel(c.sel);if(typeof c.tab==='string')UI.tab=c.tab;if(typeof c.left==='string')UI.left=c.left;srvView(c);srvWin(c);
let a2=args;UI.confirm=null;
if(m.confirmed===true&&srvOwn(SIMT.CONFIRM,act)){if(SIMT.CONFIRM[act]==='arg')a2={...args,ok:1};else UI.confirm=srvArm(act,args);}
const sent={sel:UI.sel,tab:UI.tab,left:UI.left,confirm:UI.confirm};
@ -358,7 +412,7 @@ function srvAct(inp){const m=JSON.parse(inp),act=String(m.act),ent={rid:String(m
else done('applied');}
if(outcome!=='error'){for(const k of ['sel','tab','left','confirm'])if(JSON.stringify(UI[k]??null)!==JSON.stringify(sent[k]??null))ent.ui[k]=UI[k]??null;
if(SIMR.closed)ent.ui.closed=1;if(SIMR.modal)ent.ui.modal=SIMR.modal;}}}}
SIMR.mark=null;SIMR.markK=null;SIMR.acts.push(ent);
srvWinOff();SIMR.mark=null;SIMR.markK=null;SIMR.acts.push(ent);
const p=srvPullParts({});
return srvOut({outcome,...(why?{why}:{}),v:SIMR.v},p.upd);}
@ -366,14 +420,15 @@ function srvAct(inp){const m=JSON.parse(inp),act=String(m.act),ent={rid:String(m
function srvAsk(inp){const m=JSON.parse(inp),k=SIMR.mark,act=String(m.act),out={};
if(!SIM_ASK.has(act)||!srvOwn(ACT,act))return JSON.stringify({err:'not a what-if the server answers'});
const args=m.args&&typeof m.args==='object'&&!Array.isArray(m.args)?m.args:{},td=UI.teDraft,sl=UI.slop,fx0=SIMR.fx.slice(),fl=SIMR.floats,hot=UI.hotMap;
srvView(m.ctx);$('modal').hidden=!(m.ctx&&m.ctx.modal);SIMR.modal=null;
if(srvBadArgs(act,args))return JSON.stringify({err:'an arg names a built-in'});
srvView(m.ctx);srvWin(m.ctx);SIMR.modal=null;
try{ACT[act](args);}catch(e){out.err='the game threw: '+String(e&&e.message||e).slice(0,200);srvLog('ask '+act+' threw: '+String(e&&e.stack||e));}
if(SIMR.modal)out.modal=SIMR.modal;
if(act==='teCheck'||act==='teSlop'){out.ui={teDraft:typeof UI.teDraft==='string'?UI.teDraft:null};if(act==='teSlop')out.ui.slop=typeof UI.slop==='string'?UI.slop:null;}
R=k.R;UI.hotMap=hot;uidN=k.uidN;S.rng=k.rng;
if(JSON.stringify(S)!==k.s){srvLog('ask '+act+' changed the game: put back');srvRestore('{}');}
else Object.assign(UI,k.ui);
SIMR.mark=null;UI.teDraft=td;UI.slop=sl;SIMR.fx=fx0;SIMR.floats=fl;SIMR.modal=null;$('modal').hidden=true;
SIMR.mark=null;UI.teDraft=td;UI.slop=sl;SIMR.fx=fx0;SIMR.floats=fl;SIMR.modal=null;srvWinOff();
return JSON.stringify(out);}
/* ---- pulls (5.3-5.6, 5.10): the only place the version moves ---- */
@ -389,7 +444,7 @@ function srvPullParts(m,K){K=K||srvKeyJson();const now=Date.now();
// make; what happened meanwhile (a new network's welcome) waits for the first one, which that page will get.
if(m.snap&&!SIMR.seen){SIMR.seen=true;SIMR.base=K;SIMR.rSent=new WeakRef(R);
return {upd:'',snap:'{"t":"snap","epoch":'+JSON.stringify(SIMR.epoch)+',"v":'+SIMR.v+',"now":'+now+',"S":'+srvJoin(K)+',"r":'+srvSlimR()+',"keep":[]}',subs:m.views?srvSubs(m.views):null};}
const B=SIMR.base||{},s=[],del=[];
const B=SIMR.base||Object.create(null),s=[],del=[];
for(const k in K)if(B[k]!==K[k])s.push(k);for(const k in B)if(!(k in K))del.push(k);
const rNew=!SIMR.rSent||SIMR.rSent.deref()!==R,fx=SIMR.fx,acts=SIMR.acts;let upd='';
if(s.length||del.length||rNew||fx.length||acts.length){const from=SIMR.v,v=++SIMR.v;
@ -414,28 +469,35 @@ function srvSub(inp){const m=inp?JSON.parse(inp):{};return JSON.stringify({v:SIM
function srvGreet(inp){bootNotes();return srvPull(inp);}
// A checkpoint: the save and its rt, ord included.
function srvSave(){const txt=JSON.stringify(packSave(S)),K=srvKeyJson(),h=JSON.stringify({uidN,day:S.day,lost:!!S.lost,...srvClockNow()});
return h.slice(0,-1)+',"rt":'+srvRtText(R,K,srvOrdOut(txt,K),srvSlOut())+'}\\n'+txt;}
return h.slice(0,-1)+',"rt":'+srvRtText(R,K,srvOrdOut(txt,K),srvSlOut(),srvRkOk(R,K,UI.rk,UI.rkR))+'}\\n'+txt;}
function srvCard(){return JSON.stringify({card:mpCard()});}
`;
// The realm API: one precompiled trampoline per name. Input in __in, a string out; a throw comes back as {srvErr}.
const API = ['srvLoad', 'srvBegin', 'srvPrefs', 'srvMark', 'srvRestore', 'srvRt', 'srvRPack', 'srvDay', 'srvAct', 'srvAsk', 'srvPull', 'srvSub', 'srvGreet', 'srvSave', 'srvCard'];
let TRAMP = null;
const tramps = () => TRAMP || (TRAMP = Object.fromEntries(API.map((n) => [n, new vm.Script(
`(()=>{try{return ${n}(globalThis.__in);}catch(e){return JSON.stringify({srvErr:String(e&&e.stack||e).slice(0,4000)});}})()`, { filename: `srv-${n}.js` })])));
// The realm API: one precompiled trampoline per name. Input in __in, a string out; a throw comes back as {srvErr}, and
// changed built-ins as {srvErr, polluted}. The unchecked ones only read what a fresh realm needs from one that is done
// (its save, rt, R and fx): the worker's recovery after a timeout or a pollution.
const API = ['srvLoad', 'srvBegin', 'srvPrefs', 'srvMark', 'srvRestore', 'srvRt', 'srvRPack', 'srvFxOut', 'srvDay', 'srvAct', 'srvAsk', 'srvPull', 'srvSub', 'srvGreet', 'srvSave', 'srvCard'];
const PEEK = ['srvRt', 'srvRPack', 'srvFxOut', 'srvSave'];
let TRAMP = null, TRAMP0 = null;
const tramp = (n, check) => new vm.Script(`(()=>{let r;try{r=${n}(globalThis.__in);}catch(e){r=JSON.stringify({srvErr:String(e&&e.stack||e).slice(0,4000)});}`
+ (check ? `const d=srvDirty();return d?JSON.stringify({srvErr:'the built-ins of the realm changed: '+d,polluted:1}):r;})()` : 'return r;})()'), { filename: `srv-${n}.js` });
const tramps = (check = true) => (check ? TRAMP || (TRAMP = Object.fromEntries(API.map((n) => [n, tramp(n, true)])))
: TRAMP0 || (TRAMP0 = Object.fromEntries(PEEK.map((n) => [n, tramp(n, false)]))));
// Tests only (createEngine({test})): run a piece of code in a realm, answer its value as JSON.
const EVAL = new vm.Script('(()=>{const f=new Function(globalThis.__in);const v=f();return v===undefined?"null":JSON.stringify(v);})()', { filename: 'srv-test-eval.js' });
/* The game, the epilogue and the build id, compiled once per process. */
function compileCode({ root = ROOT, page = REALM_PAGE } = {}) {
/* The game, the epilogue and the build id, compiled once per process. The engine passes the game text and its build
id (read once, in the main thread), so a worker started later runs the game the engine names, whatever src/ holds by
then; without them they are read from root. */
function compileCode({ root = ROOT, page = REALM_PAGE, src = null, simId: id = null } = {}) {
if (!vm.constants || vm.constants.DONT_CONTEXTIFY === undefined) {
throw new Error(`server-run play needs Node 22.8 or newer (vm.constants.DONT_CONTEXTIFY); this is ${process.version}`);
}
const src = gameSource({ root, page });
const game = typeof src === 'string' && src ? src : gameSource({ root, page });
return {
game: new vm.Script(src, { filename: 'game.js' }),
game: new vm.Script(game, { filename: 'game.js' }),
epilogue: new vm.Script(EPILOGUE, { filename: 'srv-epilogue.js' }),
simId: simId(assemble({ src: path.join(root, 'src'), mp: true }).html),
simId: id || simId(assemble({ src: path.join(root, 'src'), mp: true }).html),
root,
};
}
@ -469,9 +531,10 @@ class Realm {
this.ctx = ctx;
this.ms = performance.now() - t0;
}
// The answer as the realm gave it (a string). Throws ERR_SCRIPT_EXECUTION_TIMEOUT past ms.
raw(name, input, ms = 1000) {
const s = tramps()[name];
// The answer as the realm gave it (a string). Throws ERR_SCRIPT_EXECUTION_TIMEOUT past ms. check false: one of PEEK,
// whatever the built-ins are now (recovery only).
raw(name, input, ms = 1000, check = true) {
const s = tramps(check)[name];
if (!s) throw new Error(`mp-sim-realm: no realm call ${name}`);
this.ctx.__in = input === undefined ? '' : JSON.stringify(input);
try { const r = s.runInContext(this.ctx, { timeout: ms }); return typeof r === 'string' ? r : ''; } finally { this.ctx.__in = ''; }

View file

@ -5,31 +5,40 @@
// network order, with a turn for other messages between two networks: an action for network X lands between days of
// other networks, never inside X's own day. The networks of a day are fixed when it starts. Loads, new networks and
// unloads of a session wait for its day; actions for a network that is still loading wait for it. Every message is
// answered: a load that names no network (or one twice) is refused whole and changes nothing; a network in it that
// has no save, or whose save does not load, answers err (broken) and the others load.
// answered: a load that names no network (or one twice), or a day whose lists are not lists, is refused whole and
// changes nothing; a network in a load that has no save, or whose save does not load, answers err (broken) and the
// others load. Nothing a message holds, and nothing a step throws, takes the worker down.
//
// Failures. Every realm call has a time limit (a day 2 s, an action 1 s, a what-if 3 s, a pull or a save 1 s). Before
// a day or an action the realm is marked (its S as text, uidN, the dice, and for a day its rt: route memory and routing
// point), and the worker keeps the mark. A call that throws has run its finally blocks: the realm goes back to the mark
// in place. A call that times out has not (vm skips them), so that realm is never used again: a fresh one is built from
// the mark, with a new epoch. A failed day runs once more; failing again, the network is broken at its state before
// the day.
// point), and the worker keeps the mark. A mark changes nothing: one that fails leaves the realm as it was, and one
// that runs out of time has the realm built again from its own save, never from an older mark (that would undo what
// came after it). A call that throws has run its finally blocks: the realm goes back to the mark in place. A call that
// times out has not (vm skips them), and one after which the realm's built-ins changed (a prototype written through)
// left them changed: such a realm is never called again for play. A fresh one is built from the mark, with a new
// epoch, and takes over the fx the mark held. A failed day runs once more; failing again, the network is broken at its
// state before the day. When not even a fresh realm can be built, the network is dropped: its calls answer NO_REALM,
// and the engine is told ({lost}), so the host loads it again from its checkpoint (4.7).
//
// The worker → host replies are the shapes of tests/lib/mp-sim-shape.js checkWorker(), sent as {id, ok, reply} (or
// {id, ok:false, error}) for the engine to match its promises; logs go up as {log:[level, text]}.
// {id, ok:false, error}) for the engine to match its promises; logs go up as {log:[level, text]}, a dropped network as
// {lost:{code, nid, why}}, and before each job or step that runs a realm {run: code}, the session it runs (the engine
// forgets it at the next reply): a worker that dies then is that session's doing.
'use strict';
const { isMainThread, parentPort, workerData } = require('node:worker_threads');
const crypto = require('node:crypto');
const v8 = require('node:v8');
const { compileCode, Realm, isTimeout } = require('./mp-sim-realm');
const { compileCode, Realm, isTimeout, parts } = require('./mp-sim-realm');
const TIMEOUTS = { day: 2000, act: 1000, ask: 3000, pull: 1000, save: 1000, mark: 1000, small: 1000, load: 30000 };
const now = () => performance.now();
const MSG = (e) => String((e && (e.stack || e.message)) || e).slice(0, 2000);
const LINE = (e) => String((e && e.message) || e).split('\n')[0].slice(0, 300);
const msOf = (t0) => +(now() - t0).toFixed(2);
/* opts: {root, page, test: {seed, now, step} | true | null, strict, timeouts, log(level, text), post(message)}. */
/* opts: {root, page, src, simId, test: {seed, now, step} | true | null, strict, timeouts, log(level, text), post(message)}. */
function createCore(opts = {}) {
const code = compileCode({ root: opts.root, page: opts.page });
const code = compileCode({ root: opts.root, page: opts.page, src: opts.src, simId: opts.simId });
const T = { ...TIMEOUTS, ...(opts.timeouts || {}) };
const test = opts.test ? (typeof opts.test === 'object' ? opts.test : {}) : null;
const log = opts.log || (() => {});
@ -39,52 +48,97 @@ function createCore(opts = {}) {
const key = (c, n) => c + '/' + n;
const epochOf = () => crypto.randomBytes(4).toString('hex') + (++epochN).toString(36);
const session = (c) => { let s = sessions.get(c); if (!s) sessions.set(c, s = { code: c, day: null, load: null }); return s; };
const running = (c) => post({ run: c });
/* ---- realms ---- */
function build(nid, lang) {
return new Realm({ code, lang, test: test && { seed: test.seed, now: test.now, step: test.step }, loadMs: T.load,
log: (lvl, txt) => log(lvl === 'log' || lvl === 'info' || lvl === 'debug' ? 'info' : 'warn', `[${nid}] ${txt}`) });
}
// A realm call: [header, ...parts]; a throw inside the realm comes back as {srvErr}, a timeout throws.
const call = (e, name, input, ms) => e.realm.call(name, input, ms);
/* A fresh realm in place of one that timed out, from {s, uidN, rt, live}: same network, new epoch, versions from 0. R
comes from the old realm, which still answers after a timeout: the marked R (mark) or its current one, packed whole.
When it cannot, R is routed again from rt's routing point. live: s is a mark, the realm's own S as text. */
// A realm call: [header, ...parts]; a throw inside the realm comes back as {srvErr}, a timeout throws. A realm whose
// built-ins changed is marked polluted: the caller replaces it.
const call = (e, name, input, ms) => { const r = e.realm.call(name, input, ms); if (r[0] && r[0].polluted) e.polluted = true; return r; };
// What a fresh realm takes from one that is done (its save, rt, R, fx), whatever its built-ins are now.
const peek = (e, name, input, ms) => (e.realm ? parts(e.realm.raw(name, input, ms, false)) : [{ srvErr: 'gone' }]);
/* A fresh realm in place of one that is done (timed out, polluted, or could not go back in place), from {s, uidN, rt,
live}: same network, new epoch, versions from 0, the fx the old one held (the mark's, or after a read all of them).
R comes from the old realm, which still answers after a timeout: the marked R (mark) or its current one, packed
whole; when it cannot (or is polluted), or that R does not load, R is routed again from rt's routing point. The old
realm is never called again, even when this throws. */
function rebuild(e, from, mark = true) {
let rpack = null;
try { const s = e.realm && e.realm.raw('srvRPack', { mark }, T.load); if (typeof s === 'string' && s.startsWith('{"o"')) rpack = s; } catch (x) { /* gone */ }
const r = build(e.nid, e.lang), epoch = epochOf();
const [h] = r.call('srvLoad', { save: from.s, uidN: from.uidN, rt: from.rt, rpack, live: !!from.live, prefs: e.prefs, epoch, strict: !!opts.strict, clock: from.clock }, T.load);
if (h.srvErr) throw new Error('rebuilding the realm failed: ' + h.srvErr);
e.realm = r; e.epoch = epoch; e.v = 0; e.day = h.day; e.lost = h.lost;
log('warn', `[${e.nid}] realm rebuilt after a timeout (epoch ${epoch}${rpack ? '' : ', R routed again'})`);
if (!from || typeof from.s !== 'string') { e.realm = null; throw new Error('rebuilding the realm failed: nothing to build from'); }
let rpack = null, fx = null;
if (!e.polluted) try { const s = e.realm && e.realm.raw('srvRPack', { mark }, T.load, false); if (typeof s === 'string' && s.startsWith('{"o"')) rpack = s; } catch (x) { /* gone */ }
try { const [h] = peek(e, 'srvFxOut', { mark }, T.small); if (h && Array.isArray(h.fx)) fx = h; } catch (x) { /* gone */ }
e.realm = null;
const epoch = epochOf();
const load = (rp) => {
const r = build(e.nid, e.lang);
const [h] = r.call('srvLoad', { save: from.s, uidN: from.uidN, rt: from.rt, rpack: rp, fx, live: !!from.live, prefs: e.prefs, epoch, strict: !!opts.strict, clock: from.clock }, T.load);
if (h.srvErr) throw new Error(h.srvErr);
return [r, h];
};
let r, h;
try { [r, h] = load(rpack); } catch (x) {
if (!rpack) throw new Error('rebuilding the realm failed: ' + LINE(x));
log('warn', `[${e.nid}] R as it was did not load (${LINE(x)}): routed again`);
rpack = null;
try { [r, h] = load(null); } catch (y) { throw new Error('rebuilding the realm failed: ' + LINE(y)); }
}
e.realm = r; e.polluted = false; e.epoch = epoch; e.v = 0; e.day = h.day; e.lost = h.lost;
log('warn', `[${e.nid}] realm rebuilt (epoch ${epoch}${rpack ? '' : ', R routed again'})`);
}
// The network cannot go on: dropped (later calls answer NO_REALM), and the engine tells the host (4.7).
function lose(e, why) {
e.realm = null; e.broken = true;
if (realms.get(key(e.code, e.nid)) === e) realms.delete(key(e.code, e.nid));
log('error', `[${e.nid}] dropped, its realm could not be built again: ${why}`);
post({ lost: { code: e.code, nid: e.nid, why: String(why).slice(0, 300) } });
}
// A recovery: true when the network has a realm to play on again, else it is dropped.
const recover = (e, f) => { try { f(); return true; } catch (x) { lose(e, LINE(x)); return false; } };
const gone = (e) => (e.realm ? '' : '; the network was dropped: load it again');
// The rt of the old realm's mark, if it still answers; else the last day's, whose routing point belongs to that day's
// S: with another mark only its route memory counts (a close R).
function markRt(e) {
try { const [h] = call(e, 'srvRt', { mark: true }, 200); if (h && !h.srvErr && h.load) return h; } catch (x) { /* gone */ }
return e.dayMark ? (e.dayMark === e.mark ? e.dayMark.rt : { ...e.dayMark.rt, pt: undefined }) : null;
try { const [h] = peek(e, 'srvRt', { mark: true }, 200); if (h && !h.srvErr && h.load) return h; } catch (x) { /* gone */ }
return e.dayMark ? (e.dayMark === e.mark ? e.dayMark.rt : { ...e.dayMark.rt, pt: undefined, rk: undefined }) : null;
}
// The restore point of the next day or action. A failed mark throws {mark:true} and changed nothing applied; one that
// ran out of time has the realm built again from its own save first (or the network dropped).
function mark(e, input) {
const [h, s] = call(e, 'srvMark', input, T.mark);
if (h.srvErr) throw new Error('mark failed: ' + h.srvErr);
e.mark = null;
if (input.day) e.dayMark = null;
let h, s;
try { [h, s] = call(e, 'srvMark', input, T.mark); } catch (x) {
if (!isTimeout(x)) throw x;
recover(e, () => rebuildAfterRead(e));
throw Object.assign(new Error(`marking the network took longer than ${T.mark} ms`), { mark: true, timeout: true });
}
if (h.srvErr) {
if (e.polluted) recover(e, () => rebuildAfterRead(e));
throw Object.assign(new Error('marking the network failed: ' + LINE(h.srvErr)), { mark: true });
}
const m = { s, uidN: h.uidN, rt: h.rt || null, day: h.day, clock: h.clock, live: true };
if (input.day) e.dayMark = m;
e.mark = m;
return m;
}
// After a throw: back to the mark in place; if even that fails, a fresh realm from the mark.
// After a throw: back to the mark in place; if even that fails (or the realm is polluted), a fresh realm from the mark.
function restore(e, input) {
try { const [h] = call(e, 'srvRestore', input, T.small); if (!h.srvErr) return; log('warn', `[${e.nid}] restore failed: ${h.srvErr}`); }
catch (x) { log('warn', `[${e.nid}] restore timed out`); }
if (!e.polluted) {
try { const [h] = call(e, 'srvRestore', input, T.small); if (!h.srvErr) return; log('warn', `[${e.nid}] restore failed: ${h.srvErr}`); }
catch (x) { log('warn', `[${e.nid}] restore failed: ${LINE(x)}`); }
}
rebuild(e, { ...e.mark, rt: input.day ? e.mark.rt : markRt(e) });
}
// A read-only call that timed out: the realm's state is whole (nothing applied was interrupted), so the fresh realm
// starts from its save when it still answers, else from the last mark.
// A call that changed nothing applied and timed out (or left the realm polluted): the fresh realm starts from the
// realm's own save. When even that cannot be read, this throws: never an older mark.
function rebuildAfterRead(e) {
let from = null;
try { const [h, txt] = call(e, 'srvSave', undefined, T.save); if (!h.srvErr) from = { s: txt, uidN: h.uidN, rt: h.rt, clock: h.clock }; } catch (x) { /* gone */ }
if (from) rebuild(e, from, false); else rebuild(e, { ...e.mark, rt: markRt(e) });
try { const [h, txt] = peek(e, 'srvSave', undefined, T.save); if (!h.srvErr && typeof txt === 'string') from = { s: txt, uidN: h.uidN, rt: h.rt, clock: h.clock }; } catch (x) { /* gone */ }
if (!from) { e.realm = null; throw new Error('rebuilding the realm failed: its save could not be read'); }
rebuild(e, from, false);
}
/* ---- the handlers: each returns the reply (or throws, which the engine turns into a rejection) ---- */
@ -105,66 +159,65 @@ function createCore(opts = {}) {
if (n.begin) out.saved = saved(e);
return out;
} catch (x) {
realms.delete(key(c, n.nid));
if (realms.get(key(c, n.nid)) === e) realms.delete(key(c, n.nid));
log('warn', `[${n.nid}] did not load: ${MSG(x)}`);
return { t: 'loaded', nid: n.nid, err: isTimeout(x) ? 'the network took too long to load' : String((x && x.message) || x).split('\n')[0].slice(0, 300) };
return { t: 'loaded', nid: n.nid, err: isTimeout(x) ? 'the network took too long to load' : LINE(x) };
}
}
function saved(e) {
const [h, txt] = call(e, 'srvSave', undefined, T.save);
if (h.srvErr) throw new Error(h.srvErr);
if (h.srvErr) { if (e.polluted) recover(e, () => rebuildAfterRead(e)); throw new Error(h.srvErr); }
return { t: 'saved', nid: e.nid, txt, uidN: h.uidN, rt: h.rt, day: h.day, lost: h.lost };
}
// One network's day, with its mark, one retry, and broken after a second failure.
function dayOne(e, m) {
const t0 = now(), d0 = e.day;
const input = { speed: m.speed, last: m.last, alone: (m.alone || []).includes(e.nid), mute: !!m.mute };
let retried = false, err = null;
for (let i = 0; i < 2; i++) {
const input = { speed: m.speed, last: m.last, alone: m.alone.includes(e.nid), mute: !!m.mute };
let err = null;
for (let i = 0; i < 2 && e.realm; i++) {
try {
mark(e, { day: true });
const [h] = call(e, 'srvDay', input, T.day);
if (h.srvErr) throw Object.assign(new Error(h.srvErr), { inRealm: true });
if (h.srvErr) throw new Error(h.srvErr);
e.day = h.day; e.lost = h.lost;
return { day: h.day, lost: h.lost, ...(h.pause ? { pause: true } : {}), ...(retried ? { retried: true } : {}), ms: +(now() - t0).toFixed(2) };
return { day: h.day, lost: h.lost, ...(h.pause ? { pause: true } : {}), ...(i ? { retried: true } : {}), ms: msOf(t0) };
} catch (x) {
err = x; retried = true;
log('warn', `[${e.nid}] day ${d0 + 1} failed (${isTimeout(x) ? 'timeout' : 'throw'}${i ? ', again' : ''}): ${MSG(x)}`);
try { if (isTimeout(x) || !e.mark) rebuild(e, e.dayMark || e.mark); else restore(e, { day: true }); }
catch (y) { log('error', `[${e.nid}] could not go back before the day: ${MSG(y)}`); break; }
err = x;
log('warn', `[${e.nid}] day ${d0 + 1} failed (${x.mark ? 'mark' : isTimeout(x) ? 'timeout' : e.polluted ? 'polluted' : 'throw'}${i ? ', again' : ''}): ${MSG(x)}`);
// A failed mark changed nothing (the realm is as it was, built again from its own save, or dropped). Else back
// to this attempt's mark: in place after a throw, a fresh realm after a timeout or a pollution.
if (!x.mark && !recover(e, () => { if (isTimeout(x) || e.polluted) rebuild(e, e.dayMark); else restore(e, { day: true }); })) break;
}
}
e.broken = true;
return { day: e.day, lost: !!e.lost, err: (isTimeout(err) ? 'timeout: ' : '') + String((err && err.message) || err).split('\n')[0].slice(0, 300), retried: true, ms: +(now() - t0).toFixed(2) };
return { day: e.day, lost: !!e.lost, err: (isTimeout(err) ? 'timeout: ' : '') + LINE(err) + gone(e), retried: true, ms: msOf(t0) };
}
// An action or a what-if: its mark, the call, and the way back when it fails.
function marked(m, e, name, input, ms, what) {
try { mark(e, input); } catch (x) { if (!x.mark) throw x; return [errReply(m, x.timeout ? 'timeout' : 'throw', x.message + gone(e))]; }
let r;
try { r = call(e, name, m.msg, ms); } catch (x) {
if (!isTimeout(x)) throw x;
recover(e, () => rebuild(e, { ...e.mark, rt: markRt(e) }));
return [errReply(m, 'timeout', `the ${what} took longer than ${ms} ms` + gone(e))];
}
if (r[0].srvErr) { recover(e, () => (e.polluted ? rebuild(e, { ...e.mark, rt: markRt(e) }) : restore(e, {}))); return [errReply(m, 'throw', r[0].srvErr + gone(e))]; }
return r;
}
const H = {
act(m, e) {
if (e.broken || e.lost) return { t: 'actDone', nid: m.nid, epoch: e.epoch, rid: m.msg.rid, outcome: 'stopped', v: e.v, upd: null };
try {
mark(e, { keys: true });
const [h, upd] = call(e, 'srvAct', m.msg, T.act);
if (h.srvErr) { restore(e, {}); return errReply(m, 'throw', h.srvErr); }
e.v = h.v;
if (h.outcome === 'stopped') e.lost = true;
return { t: 'actDone', nid: m.nid, epoch: e.epoch, rid: m.msg.rid, outcome: h.outcome, ...(h.why ? { why: h.why } : {}), v: h.v, upd: upd || null };
} catch (x) {
if (!isTimeout(x)) throw x;
rebuild(e, { ...e.mark, rt: markRt(e) });
return errReply(m, 'timeout', `the action took longer than ${T.act} ms`);
}
const t0 = now(), [h, upd] = marked(m, e, 'srvAct', { keys: true }, T.act, 'action');
if (h.t === 'err') return h;
e.v = h.v;
if (h.outcome === 'stopped') e.lost = true;
return { t: 'actDone', nid: m.nid, epoch: e.epoch, rid: m.msg.rid, outcome: h.outcome, ...(h.why ? { why: h.why } : {}), v: h.v, upd: upd || null, ms: msOf(t0) };
},
ask(m, e) {
if (e.broken || e.lost) return { t: 'askDone', nid: m.nid, rid: m.msg.rid, err: 'stopped' };
try {
mark(e, {});
const [h] = call(e, 'srvAsk', m.msg, T.ask);
if (h.srvErr) { restore(e, {}); return errReply(m, 'throw', h.srvErr); }
return { t: 'askDone', nid: m.nid, rid: m.msg.rid, ...h };
} catch (x) {
if (!isTimeout(x)) throw x;
rebuild(e, { ...e.mark, rt: markRt(e) });
return errReply(m, 'timeout', `the what-if took longer than ${T.ask} ms`);
}
const t0 = now(), [h] = marked(m, e, 'srvAsk', {}, T.ask, 'what-if');
if (h.t === 'err') return h;
return { t: 'askDone', nid: m.nid, rid: m.msg.rid, ...h, ms: msOf(t0) };
},
pull(m, e) { return pulled(m, e, 'srvPull', { snap: !!m.snap, views: m.views }); },
greet(m, e) { return pulled(m, e, 'srvGreet', {}); },
@ -173,28 +226,36 @@ function createCore(opts = {}) {
},
save(m, e) { return guarded(m, e, () => saved(e)); },
card(m, e) { return guarded(m, e, () => { const [h] = call(e, 'srvCard', undefined, T.small); if (h.srvErr) return errReply(m, 'throw', h.srvErr); return { t: 'card', nid: m.nid, card: h.card || null }; }); },
prefs(m, e) { if (m.prefs) e.prefs = { ...e.prefs, ...m.prefs }; return guarded(m, e, () => { call(e, 'srvPrefs', { prefs: m.prefs }, T.small); return null; }); },
prefs(m, e) { if (m.prefs) e.prefs = { ...e.prefs, ...m.prefs }; return guarded(m, e, () => { const [h] = call(e, 'srvPrefs', { prefs: m.prefs }, T.small); return h.srvErr ? errReply(m, 'throw', h.srvErr) : null; }); },
// Tests only: a piece of code in the realm, its value back.
eval(m, e) {
if (!test) throw new Error('eval is for tests');
try { return { value: e.realm.eval(m.src, m.ms || 5000) }; }
catch (x) { if (isTimeout(x)) { rebuildAfterRead(e); return { timeout: true, epoch: e.epoch }; } return { error: MSG(x) }; }
catch (x) { if (isTimeout(x)) { recover(e, () => rebuildAfterRead(e)); return { timeout: true, epoch: e.epoch }; } return { error: MSG(x) }; }
},
};
function pulled(m, e, name, input) {
return guarded(m, e, () => {
const [h, ...p] = call(e, name, input, T.pull);
const t0 = now(), [h, ...p] = call(e, name, input, T.pull);
if (h.srvErr) return errReply(m, 'throw', h.srvErr);
e.v = h.v;
const o = { t: 'pulled', nid: m.nid, epoch: e.epoch, v: h.v };
(h.parts || []).forEach((k, i) => { o[k] = p[i]; });
if (h.subs) o.subs = h.subs;
o.ms = msOf(t0);
return o;
});
}
// A call that changes nothing applied: after a timeout the realm is built again from its own save.
// A call that changes nothing applied: after a timeout (or a pollution) the realm is built again from its own save.
function guarded(m, e, f) {
try { return f(); } catch (x) { if (!isTimeout(x)) throw x; rebuildAfterRead(e); return errReply(m, 'timeout', `${m.t} took too long`); }
let r;
try { r = f(); } catch (x) {
if (!isTimeout(x)) { if (e.polluted && e.realm) recover(e, () => rebuildAfterRead(e)); throw x; }
recover(e, () => rebuildAfterRead(e));
return errReply(m, 'timeout', `${m.t} took too long` + gone(e));
}
if (e.polluted && e.realm) recover(e, () => rebuildAfterRead(e));
return r;
}
/* ---- the queue ---- */
@ -224,6 +285,7 @@ function createCore(opts = {}) {
}
return false;
}
const strings = (a) => [...new Set(a.filter((n) => typeof n === 'string'))];
function start(j) {
if (j.t === 'mem') { const h = v8.getHeapStatistics(), mu = process.memoryUsage(); return reply(j, { heapUsed: h.used_heap_size, heapTotal: h.total_heap_size, external: h.external_memory, rss: mu.rss, realms: realms.size }); }
if (j.t === 'load') {
@ -237,13 +299,16 @@ function createCore(opts = {}) {
seen.add(n.nid);
}
const s = session(j.code);
for (const n of nets) realms.set(key(j.code, n.nid), { code: j.code, nid: n.nid, lang: n.lang || 'en', prefs: n.prefs || {}, loading: true, realm: null, epoch: '', v: 0, day: 0, lost: false, broken: false, mark: null, dayMark: null });
for (const n of nets) realms.set(key(j.code, n.nid), { code: j.code, nid: n.nid, lang: n.lang || 'en', prefs: n.prefs || {}, loading: true, realm: null, epoch: '', v: 0, day: 0, lost: false, broken: false, polluted: false, mark: null, dayMark: null });
s.load = { j, i: 0, out: [] };
return;
}
if (j.t === 'day') {
// The lists as lists (of network ids), or the day is refused whole; speed and last the realm checks itself.
for (const k of ['nets', 'alone']) if (j[k] !== undefined && !Array.isArray(j[k])) throw new Error(`day: ${k} must be a list`);
j.alone = strings(j.alone || []);
const s = session(j.code);
const order = [...new Set(j.nets || [])].sort().filter((n) => { const e = realms.get(key(j.code, n)); return e && !e.loading && !e.broken && !e.lost; });
const order = strings(j.nets || []).sort().filter((n) => { const e = realms.get(key(j.code, n)); return e && !e.loading && !e.broken && !e.lost; });
s.day = { j, order, i: 0, out: {} };
return;
}
@ -254,25 +319,49 @@ function createCore(opts = {}) {
}
const e = realms.get(key(j.code, j.nid));
if (e && e.loading) { realms.delete(key(j.code, j.nid)); log('error', `[${j.nid}] marked loading with no load under way: dropped`); }
if (!e || e.loading) throw Object.assign(new Error(`no realm ${key(j.code, j.nid)}`), { code: 'NO_REALM' });
if (!e || e.loading || !e.realm) throw Object.assign(new Error(`no realm ${key(j.code, j.nid)}`), { code: 'NO_REALM' });
const h = H[j.t];
if (!h) throw new Error(`unknown message ${j.t}`);
running(j.code);
return reply(j, h(j, e));
}
// One step of a session's load (one network) or day (one network).
// One step of a session's load (one network) or day (one network). A throw answers the whole call, never the worker.
function runStep() {
const ss = [...sessions.values()].filter((s) => s.load || s.day);
if (!ss.length) return false;
const s = ss[turn++ % ss.length];
if (s.load) {
const L = s.load, n = L.j.nets[L.i++];
if (n) L.out.push(loadOne(L.j.code, n));
if (L.i >= L.j.nets.length) { s.load = null; reply(L.j, L.out); }
const L = s.load;
try {
const n = L.j.nets[L.i++];
if (n) { running(s.code); L.out.push(loadOne(L.j.code, n)); }
if (L.i >= L.j.nets.length) { s.load = null; reply(L.j, L.out); }
} catch (x) {
s.load = null;
for (const n of L.j.nets.slice(L.i)) { const e = n && realms.get(key(L.j.code, n.nid)); if (e && e.loading) realms.delete(key(L.j.code, n.nid)); }
log('error', `mp-sim worker: a load of ${s.code} failed: ${MSG(x)}`);
fail(L.j, x);
}
return true;
}
const D = s.day, nid = D.order[D.i++];
if (nid) { const e = realms.get(key(D.j.code, nid)); if (e && !e.broken && !e.lost) D.out[nid] = dayOne(e, D.j); }
if (D.i >= D.order.length) { s.day = null; reply(D.j, { t: 'dayDone', code: D.j.code, d: D.j.d, nets: D.out }); }
const D = s.day;
try {
const nid = D.order[D.i++];
const e = nid && realms.get(key(D.j.code, nid));
if (e && !e.broken && !e.lost && e.realm) {
running(s.code);
try { D.out[nid] = dayOne(e, D.j); } catch (x) {
e.broken = true;
log('error', `[${nid}] the day could not run: ${MSG(x)}`);
D.out[nid] = { day: e.day, lost: !!e.lost, err: 'the day could not run: ' + LINE(x), retried: false, ms: 0 };
}
}
if (D.i >= D.order.length) { s.day = null; reply(D.j, { t: 'dayDone', code: D.j.code, d: D.j.d, nets: D.out }); }
} catch (x) {
s.day = null;
log('error', `mp-sim worker: a day of ${s.code} failed: ${MSG(x)}`);
fail(D.j, x);
}
return true;
}
/* Turns: a message and a step of a session's load or day, in alternation. The worker goes on only while something ran:
@ -283,11 +372,13 @@ function createCore(opts = {}) {
function pump() {
pumping = false;
flip = !flip;
if (flip ? runJob() || runStep() : runStep() || runJob()) return kick();
if (!jobs.length) return;
const j = jobs.shift();
log('error', `mp-sim worker: ${j.t} for ${key(j.code, j.nid)} waited for nothing that could come; answered`);
fail(j, new Error(`${j.t} could not run`));
try {
if (flip ? runJob() || runStep() : runStep() || runJob()) return kick();
if (!jobs.length) return;
const j = jobs.shift();
log('error', `mp-sim worker: ${j.t} for ${key(j.code, j.nid)} waited for nothing that could come; answered`);
fail(j, new Error(`${j.t} could not run`));
} catch (x) { log('error', `mp-sim worker: ${MSG(x)}`); }
kick();
}
function kick() { if (!pumping) { pumping = true; setImmediate(pump); } }
@ -304,7 +395,11 @@ if (!isMainThread && workerData && workerData.mpSim) {
const log = (level, text) => post({ log: [level, text] });
process.on('unhandledRejection', (e) => log('warn', 'unhandled rejection in a realm: ' + MSG(e)));
let core;
try { core = createCore({ ...W, log, post }); } catch (e) { post({ fatal: MSG(e) }); process.exit(1); }
try {
// Tests only: a worker that cannot start (the engine backs off, then stops trying).
if (W.test && W.test.startFail) throw new Error('injected: the game does not start');
core = createCore({ ...W, log, post });
} catch (e) { post({ fatal: MSG(e) }); process.exit(1); }
parentPort.on('message', (m) => core.handle(m));
post({ ready: true, simId: core.simId });
}