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:
parent
3b8c331556
commit
9597d89079
11 changed files with 879 additions and 211 deletions
|
|
@ -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 };
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue