arcadible 1.0.30 → 1.0.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/command/serve.js +5 -0
- package/dist/phrase/function.js +7 -0
- package/dist/phrase/serve.js +6 -0
- package/dist/runner/bundle.js +43 -2
- package/dist/runner/entry.js +37 -9
- package/dist/serve/page.js +255 -57
- package/dist/serve/runner.js +31 -6
- package/dist/serve/values.js +160 -115
- package/npm-shrinkwrap.json +95 -81
- package/package.json +3 -3
package/dist/command/serve.js
CHANGED
|
@@ -148,6 +148,11 @@ export default defineCommand({
|
|
|
148
148
|
[phrase.playLabel(), `${served.page}/`],
|
|
149
149
|
[phrase.serveAlone(), served.game],
|
|
150
150
|
...(served.runner ? [[phrase.runnerLabel(), served.runner.url]] : []),
|
|
151
|
+
// a game with a function or servers of its own can be played by
|
|
152
|
+
// more than one player: a window each, named by its address
|
|
153
|
+
...(served.runner || served.forServer
|
|
154
|
+
? [[phrase.servePlayer(), `${served.page}/?player=Kim`]]
|
|
155
|
+
: []),
|
|
151
156
|
...(served.forServer
|
|
152
157
|
? [[phrase.serveServer(), `ARCADIBLE_SERVE=${served.page}`]]
|
|
153
158
|
: []),
|
package/dist/phrase/function.js
CHANGED
|
@@ -3,6 +3,13 @@
|
|
|
3
3
|
// [import]
|
|
4
4
|
import { blocks } from "./internal.js";
|
|
5
5
|
// [function]
|
|
6
|
+
// a function that imports a package that isn't the Function SDK: the
|
|
7
|
+
// Server SDK, for a server of one's own, or the SDK, for a game. A fact, as
|
|
8
|
+
// a warning is: the build goes on.
|
|
9
|
+
export function functionImportWrong({ for: meant, name, }) {
|
|
10
|
+
return blocks(`Your function imports ${name}, which is for ${meant === "game" ? "a game" : "a server of your own"}.`, "A function uses @arcadible/function-sdk.");
|
|
11
|
+
}
|
|
12
|
+
// [function]
|
|
6
13
|
export function secretRemoveQuestion({ channel, name, }) {
|
|
7
14
|
return `Remove ${name} from ${channel}? Your function reads nothing for it within a minute.`;
|
|
8
15
|
}
|
package/dist/phrase/serve.js
CHANGED
|
@@ -43,6 +43,12 @@ export function serveServer() {
|
|
|
43
43
|
return "For your server:";
|
|
44
44
|
}
|
|
45
45
|
// [function]
|
|
46
|
+
// the label before the play page's address for a second local player: each
|
|
47
|
+
// window opened with `?player=<name>` plays as a player of its own.
|
|
48
|
+
export function servePlayer() {
|
|
49
|
+
return "As another player:";
|
|
50
|
+
}
|
|
51
|
+
// [function]
|
|
46
52
|
export function playerInvalid() {
|
|
47
53
|
return "`--player` must be a name, up to 200 characters.";
|
|
48
54
|
}
|
package/dist/runner/bundle.js
CHANGED
|
@@ -19,27 +19,68 @@ import { ArcError } from "../util/error.js";
|
|
|
19
19
|
// origin keeps for Arcadible's own files, which is never served.
|
|
20
20
|
export const BUILT = join(".arc", "runner", "function.js");
|
|
21
21
|
export const PACKED = "arcadible/function.js";
|
|
22
|
+
// the package a function uses with Arcadible, and the two it mustn't, each
|
|
23
|
+
// with what it's for: the Server SDK asks for a key a function never has,
|
|
24
|
+
// and the SDK starts a game's client.
|
|
25
|
+
export const FUNCTION_SDK = "@arcadible/function-sdk";
|
|
26
|
+
export const WRONG = {
|
|
27
|
+
"@arcadible/sdk": "game",
|
|
28
|
+
"@arcadible/server-sdk": "server",
|
|
29
|
+
};
|
|
22
30
|
// [function]
|
|
23
31
|
// bundles the runner whose code is at `main`, a full path, into `outfile`;
|
|
24
32
|
// code that isn't there, or doesn't build, fails as a build does.
|
|
25
|
-
|
|
33
|
+
//
|
|
34
|
+
// `onPackages`, when given, hears the `@arcadible/` packages the bundle took
|
|
35
|
+
// in, by name, each once, so a build can say when a function imports one
|
|
36
|
+
// that isn't the Function SDK (`misimported`). Nothing passes it yet: what it
|
|
37
|
+
// finds is a warning, a warning has a code, and a code has an entry on the
|
|
38
|
+
// public Errors page, which says nothing of functions until they ship
|
|
39
|
+
// (TODO.md, Agreed next).
|
|
40
|
+
export async function bundle({ main, onPackages, outfile, }) {
|
|
26
41
|
if (!(await pathExists(main))) {
|
|
27
42
|
throw new ArcError(BUILD_FAILED, phrase.runnerMissing(main));
|
|
28
43
|
}
|
|
44
|
+
let inputs;
|
|
29
45
|
try {
|
|
30
|
-
await build({
|
|
46
|
+
const result = await build({
|
|
31
47
|
bundle: true,
|
|
32
48
|
conditions: ["workerd", "worker", "browser"],
|
|
33
49
|
format: "esm",
|
|
34
50
|
logLevel: "silent",
|
|
51
|
+
metafile: Boolean(onPackages),
|
|
35
52
|
outfile,
|
|
36
53
|
platform: "neutral",
|
|
37
54
|
entryPoints: [main],
|
|
38
55
|
target: "es2022",
|
|
39
56
|
});
|
|
57
|
+
inputs = Object.keys(result.metafile?.inputs ?? {});
|
|
40
58
|
}
|
|
41
59
|
catch (ex) {
|
|
42
60
|
throw new ArcError(BUILD_FAILED, phrase.runnerBuildFailed(ex instanceof Error ? ex.message : String(ex)), { cause: ex });
|
|
43
61
|
}
|
|
62
|
+
onPackages?.(packagesOf(inputs));
|
|
44
63
|
return outfile;
|
|
45
64
|
}
|
|
65
|
+
// [function]
|
|
66
|
+
// the `@arcadible/` packages a bundle's inputs come from, by name, each
|
|
67
|
+
// once, in the order first met: an input under `node_modules/@arcadible/<name>/`.
|
|
68
|
+
export function packagesOf(inputs) {
|
|
69
|
+
const names = new Set();
|
|
70
|
+
for (const input of inputs) {
|
|
71
|
+
const found = /(?:^|\/)node_modules\/(@arcadible\/[^/]+)\//u.exec(input.split("\\").join("/"));
|
|
72
|
+
if (found?.[1])
|
|
73
|
+
names.add(found[1]);
|
|
74
|
+
}
|
|
75
|
+
return [...names];
|
|
76
|
+
}
|
|
77
|
+
// [function]
|
|
78
|
+
// of the packages a function took in, those it mustn't use, each with what
|
|
79
|
+
// it's for: a game's (`@arcadible/sdk`) or a server of one's own
|
|
80
|
+
// (`@arcadible/server-sdk`).
|
|
81
|
+
export function misimported(names) {
|
|
82
|
+
return names.flatMap((name) => {
|
|
83
|
+
const meant = WRONG[name];
|
|
84
|
+
return meant ? [{ for: meant, name }] : [];
|
|
85
|
+
});
|
|
86
|
+
}
|
package/dist/runner/entry.js
CHANGED
|
@@ -12,26 +12,54 @@
|
|
|
12
12
|
export const ENTRY_MODULE = "entry.js";
|
|
13
13
|
export const FUNCTION_MODULE = "function.js";
|
|
14
14
|
// [declare]
|
|
15
|
-
// A
|
|
16
|
-
//
|
|
17
|
-
//
|
|
18
|
-
//
|
|
19
|
-
//
|
|
20
|
-
//
|
|
15
|
+
// A function's calls to Arcadible go through the transport the entry sets
|
|
16
|
+
// (plan/SERVER-SDK-SPLIT.md, 5), which the Function SDK reads: `fetch(path,
|
|
17
|
+
// init)` sends a request to the server API (`path` after /v1/server, `init`
|
|
18
|
+
// fetch's own), with the newest of the dispatcher's claims as its bearer
|
|
19
|
+
// (x-arcadible-claim, on each request), or under arc serve to its own API
|
|
20
|
+
// with none; it resolves with the answer, and never throws (Arcadible
|
|
21
|
+
// unreached is a 503). `version` is raised whenever the transport gains
|
|
22
|
+
// something, and never goes down: every Function SDK published reads it.
|
|
23
|
+
// Until online Reversi is released on the Function SDK, the entry also
|
|
24
|
+
// keeps the Server SDK's old way: its mark, and the claim and arc serve's
|
|
25
|
+
// address in a `process.env` of the entry's making. A function's secrets
|
|
26
|
+
// arrive as a header too (x-arcadible-secrets, opened by the dispatcher),
|
|
27
|
+
// and are added to what the code is given, `env`. Neither header reaches
|
|
28
|
+
// the developer's code.
|
|
21
29
|
export const ENTRY = `import handler from "./${FUNCTION_MODULE}";
|
|
22
30
|
const CLAIM = "x-arcadible-claim";
|
|
23
31
|
const SECRETS = "x-arcadible-secrets";
|
|
24
|
-
|
|
25
|
-
//
|
|
32
|
+
const API = "https://api.arcadible.com/v1/server";
|
|
33
|
+
// the newest claim, and arc serve's address under it
|
|
34
|
+
let claimed = null;
|
|
35
|
+
let serve = null;
|
|
36
|
+
globalThis[Symbol.for("arcadible.function.api")] = {
|
|
37
|
+
version: 1,
|
|
38
|
+
async fetch(path, init = {}) {
|
|
39
|
+
try {
|
|
40
|
+
const headers = new Headers(init.headers);
|
|
41
|
+
if (!serve && claimed) headers.set("authorization", "Bearer " + claimed);
|
|
42
|
+
const base = serve ? serve + "/_arcadible/v1/server" : API;
|
|
43
|
+
return await fetch(base + path, { ...init, headers });
|
|
44
|
+
} catch {
|
|
45
|
+
return new Response(null, { status: 503 });
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
// the old mark, which says the code is running as a function
|
|
26
50
|
globalThis[Symbol.for("arcadible.function")] = true;
|
|
27
51
|
export default {
|
|
28
52
|
async fetch(request, env, ctx) {
|
|
29
53
|
const process = (globalThis.process ??= {});
|
|
30
54
|
const set = (process.env ??= {});
|
|
31
|
-
if (env.ARCADIBLE_SERVE)
|
|
55
|
+
if (env.ARCADIBLE_SERVE) {
|
|
56
|
+
serve = env.ARCADIBLE_SERVE;
|
|
57
|
+
set.ARCADIBLE_SERVE = env.ARCADIBLE_SERVE;
|
|
58
|
+
}
|
|
32
59
|
const claim = request.headers.get(CLAIM);
|
|
33
60
|
const secrets = request.headers.get(SECRETS);
|
|
34
61
|
if (claim) {
|
|
62
|
+
claimed = claim;
|
|
35
63
|
set.ARCADIBLE_CLAIM = claim;
|
|
36
64
|
} else {
|
|
37
65
|
delete set.ARCADIBLE_CLAIM;
|
package/dist/serve/page.js
CHANGED
|
@@ -36,6 +36,13 @@ export const JWKS = "/.well-known/jwks.json";
|
|
|
36
36
|
export const LOGIN = "/_arcadible/api/login";
|
|
37
37
|
// who a guest logs in as, without `--player` to name one.
|
|
38
38
|
const LOGIN_NAME = "Player";
|
|
39
|
+
// where a window's own player asks for its tickets and values: a window
|
|
40
|
+
// opened as `/?player=<name>` plays as that local player (or, with no name,
|
|
41
|
+
// a guest), and its page asks under `/_arcadible/as/<id>` (or `.../guest`),
|
|
42
|
+
// since every window shares the page's cookies. One arc serve plays as any
|
|
43
|
+
// number of local players, each with its own id, saves, and values.
|
|
44
|
+
export const AS = "/_arcadible/as";
|
|
45
|
+
const GUEST = "guest";
|
|
39
46
|
// the server API, as a game's own server reaches Arcadible's with its key
|
|
40
47
|
// (/v1/server/...): here it needs none, since only this machine can ask.
|
|
41
48
|
const SERVER = "/_arcadible/v1/server";
|
|
@@ -162,7 +169,10 @@ export function policy(src) {
|
|
|
162
169
|
// with the play page's styles and loading screen; without a host that can,
|
|
163
170
|
// the page frames the game by itself, and the game stands alone, as off
|
|
164
171
|
// Arcadible.
|
|
165
|
-
export function html(game, pictures = { art: false, icon: false }) {
|
|
172
|
+
export function html(game, pictures = { art: false, icon: false }, opened) {
|
|
173
|
+
// a window named by its address asks under its own path, and offers no
|
|
174
|
+
// log-in: it plays as the player its address names.
|
|
175
|
+
const own = opened ? `${AS}/${opened.player ? opened.player.id : GUEST}` : "";
|
|
166
176
|
const config = {
|
|
167
177
|
allow: ALLOW,
|
|
168
178
|
// the loading screen's art, or the icon, blurred, with none, as the play
|
|
@@ -173,21 +183,21 @@ export function html(game, pictures = { art: false, icon: false }) {
|
|
|
173
183
|
game: game.id,
|
|
174
184
|
icon: pictures.icon ? ICON_PNG : null,
|
|
175
185
|
// where a guest logs in, when the game asks for a log-in
|
|
176
|
-
logIn: LOGIN,
|
|
186
|
+
...(opened ? {} : { logIn: LOGIN }),
|
|
177
187
|
// who the game runs for: a local player (`arc serve --player`), or null,
|
|
178
188
|
// a guest.
|
|
179
|
-
player: game.player ?? null,
|
|
189
|
+
player: opened ? opened.player : (game.player ?? null),
|
|
180
190
|
// where the game's runner is, when the project has one
|
|
181
191
|
runner: game.runner?.url ?? null,
|
|
182
192
|
sandbox: SANDBOX,
|
|
183
193
|
src: game.src,
|
|
184
194
|
// tickets for the game's own servers, from arc serve itself, as the
|
|
185
195
|
// play page asks its own server
|
|
186
|
-
tickets: TICKETS
|
|
196
|
+
tickets: `${own}${TICKETS}`,
|
|
187
197
|
title: game.title,
|
|
188
198
|
// the game's values, from arc serve itself, as the play page asks its
|
|
189
199
|
// own server
|
|
190
|
-
values: VALUES
|
|
200
|
+
values: `${own}${VALUES}`,
|
|
191
201
|
version: game.version,
|
|
192
202
|
};
|
|
193
203
|
return `<!doctype html>
|
|
@@ -261,9 +271,9 @@ function readJson(req, max = BODY_MAX) {
|
|
|
261
271
|
// a ticket for one of the game's servers, for the page (the game's host), or
|
|
262
272
|
// for a shell on this machine. Only JSON is taken: a page elsewhere can't
|
|
263
273
|
// send that across origins without asking first, and isn't answered. The
|
|
264
|
-
// player is the
|
|
265
|
-
//
|
|
266
|
-
async function sendTicket(req, res, {
|
|
274
|
+
// player is the window's (`playing`), or, from a shell, the one the request
|
|
275
|
+
// names.
|
|
276
|
+
async function sendTicket(req, res, { playerOf, playing, tickets, }) {
|
|
267
277
|
const type = ((req.headers["content-type"] ?? "").split(";")[0] ?? "").trim();
|
|
268
278
|
const data = type.toLowerCase() === "application/json" ? await readJson(req) : null;
|
|
269
279
|
if (data === null || typeof data !== "object") {
|
|
@@ -273,7 +283,7 @@ async function sendTicket(req, res, { game, playerOf, tickets, }) {
|
|
|
273
283
|
// a list names no server, and no player.
|
|
274
284
|
const asked = isTable(data) ? data : {};
|
|
275
285
|
const named = Object.hasOwn(asked, "player") && !req.headers.origin;
|
|
276
|
-
const player = named ? playerOf(asked["player"]) :
|
|
286
|
+
const player = named ? playerOf(asked["player"]) : playing;
|
|
277
287
|
if (player === false) {
|
|
278
288
|
res.writeHead(400, HEADERS).end();
|
|
279
289
|
return;
|
|
@@ -315,15 +325,111 @@ async function sendLogIn(req, res, { game, playerOf, tickets, }) {
|
|
|
315
325
|
res.writeHead(204, HEADERS).end();
|
|
316
326
|
}
|
|
317
327
|
// [function]
|
|
328
|
+
// a call's items, as the API reads them (app/src/web/arcadible-api's
|
|
329
|
+
// app/value.py, value_items): `items`, each naming its scope (and `player`,
|
|
330
|
+
// an id in the game, for a player's), or a body that names one `scope` for
|
|
331
|
+
// all its `keys` or `items`, as released games send, with `single` saying
|
|
332
|
+
// so; each item with its owner, a player's id or the game ("game", one is
|
|
333
|
+
// served), and only its verb's fields. Null for a body that's neither.
|
|
334
|
+
function valueItemsOf(body, verb) {
|
|
335
|
+
const fields = verb === "read"
|
|
336
|
+
? ["since"]
|
|
337
|
+
: verb === "write"
|
|
338
|
+
? ["value", "revision"]
|
|
339
|
+
: ["revision"];
|
|
340
|
+
const itemOf = function (given, scope, player) {
|
|
341
|
+
const known = SCOPES.find((each) => each === scope);
|
|
342
|
+
if (!isTable(given) ||
|
|
343
|
+
!known ||
|
|
344
|
+
(known === "player" &&
|
|
345
|
+
(typeof player !== "string" || !PLAYER_ID.test(player)))) {
|
|
346
|
+
return null;
|
|
347
|
+
}
|
|
348
|
+
const item = {
|
|
349
|
+
key: given["key"],
|
|
350
|
+
owner: known === "player" ? String(player) : "game",
|
|
351
|
+
scope: known,
|
|
352
|
+
};
|
|
353
|
+
for (const field of fields) {
|
|
354
|
+
if (Object.hasOwn(given, field)) {
|
|
355
|
+
item[field] = given[field];
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
return item;
|
|
359
|
+
};
|
|
360
|
+
const single = body["scope"] !== undefined;
|
|
361
|
+
let given;
|
|
362
|
+
if (single) {
|
|
363
|
+
given =
|
|
364
|
+
verb === "write"
|
|
365
|
+
? body["items"]
|
|
366
|
+
: Array.isArray(body["keys"])
|
|
367
|
+
? body["keys"].map((key) => ({ key }))
|
|
368
|
+
: null;
|
|
369
|
+
}
|
|
370
|
+
else {
|
|
371
|
+
given = body["items"];
|
|
372
|
+
}
|
|
373
|
+
if (!Array.isArray(given)) {
|
|
374
|
+
return null;
|
|
375
|
+
}
|
|
376
|
+
const items = [];
|
|
377
|
+
for (const each of given) {
|
|
378
|
+
const item = single
|
|
379
|
+
? itemOf(each, body["scope"], body["player"])
|
|
380
|
+
: itemOf(each, isTable(each) ? each["scope"] : null, isTable(each) ? each["player"] : null);
|
|
381
|
+
if (!item) {
|
|
382
|
+
return null;
|
|
383
|
+
}
|
|
384
|
+
items.push(item);
|
|
385
|
+
}
|
|
386
|
+
return { items, single };
|
|
387
|
+
}
|
|
388
|
+
// [function]
|
|
389
|
+
// an item call's answer, as the API gives it: in the items form, each with
|
|
390
|
+
// its scope (and `player` for a player's); a single scope's as before.
|
|
391
|
+
function itemsAnswer(verb, items, single, data) {
|
|
392
|
+
if (verb === "remove" || !Array.isArray(data)) {
|
|
393
|
+
return data;
|
|
394
|
+
}
|
|
395
|
+
return data.map((row, at) => {
|
|
396
|
+
const item = items[at];
|
|
397
|
+
if (!row || !item) {
|
|
398
|
+
return null;
|
|
399
|
+
}
|
|
400
|
+
const said = {
|
|
401
|
+
scope: item.scope,
|
|
402
|
+
...(item.scope === "player" ? { player: item.owner } : {}),
|
|
403
|
+
key: row["key"],
|
|
404
|
+
};
|
|
405
|
+
if (verb === "read" && row["unchanged"]) {
|
|
406
|
+
return { ...said, revision: row["revision"], unchanged: true };
|
|
407
|
+
}
|
|
408
|
+
const kept = verb === "read"
|
|
409
|
+
? {
|
|
410
|
+
key: row["key"],
|
|
411
|
+
revision: row["revision"],
|
|
412
|
+
updated_at: row["updated_at"],
|
|
413
|
+
value: row["value"],
|
|
414
|
+
}
|
|
415
|
+
: {
|
|
416
|
+
key: row["key"],
|
|
417
|
+
revision: row["revision"],
|
|
418
|
+
updated_at: row["updated_at"],
|
|
419
|
+
};
|
|
420
|
+
return single ? kept : { ...said, ...kept };
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
// [function]
|
|
318
424
|
// a game's values, for the page (the game's host), as the play site's route
|
|
319
425
|
// answers (arcadible-play's /api/games/<game>/values/<verb>, and the API
|
|
320
426
|
// behind it): the game's call, read, written, removed, or listed, for who
|
|
321
|
-
// the page runs the game for. A game reads the developer's and the game's,
|
|
427
|
+
// the page's window runs the game for. A game reads the developer's and the game's,
|
|
322
428
|
// and reads and writes its own player's: the server's scope, a write to any
|
|
323
429
|
// but a player's, a guest's call for a player's, or a player who isn't the
|
|
324
430
|
// one playing is 403. Only JSON is taken, as for a ticket, so a page
|
|
325
431
|
// elsewhere can't ask. A refusal is its status, with no body.
|
|
326
|
-
async function sendGameValues(req, res, verb, {
|
|
432
|
+
async function sendGameValues(req, res, verb, { player, values }) {
|
|
327
433
|
const refuse = function (status) {
|
|
328
434
|
res.writeHead(status, HEADERS).end();
|
|
329
435
|
};
|
|
@@ -335,21 +441,8 @@ async function sendGameValues(req, res, verb, { game, values }) {
|
|
|
335
441
|
refuse(400);
|
|
336
442
|
return;
|
|
337
443
|
}
|
|
338
|
-
const scope = SCOPES.find((each) => each === body["scope"]);
|
|
339
|
-
if (!scope) {
|
|
340
|
-
refuse(400);
|
|
341
|
-
return;
|
|
342
|
-
}
|
|
343
444
|
const write = verb === "write" || verb === "remove";
|
|
344
|
-
const
|
|
345
|
-
if (scope === "server" ||
|
|
346
|
-
(write && scope !== "player") ||
|
|
347
|
-
(scope === "player" && (!player || body["player"] !== player.id))) {
|
|
348
|
-
refuse(403);
|
|
349
|
-
return;
|
|
350
|
-
}
|
|
351
|
-
const owner = scope === "player" && player ? player.id : "game";
|
|
352
|
-
const answer = function (given) {
|
|
445
|
+
const answer = function (given, shape) {
|
|
353
446
|
if (given.status !== 200) {
|
|
354
447
|
refuse(given.status);
|
|
355
448
|
return;
|
|
@@ -358,23 +451,52 @@ async function sendGameValues(req, res, verb, { game, values }) {
|
|
|
358
451
|
...HEADERS,
|
|
359
452
|
"content-type": "application/json; charset=utf-8",
|
|
360
453
|
});
|
|
361
|
-
res.end(JSON.stringify({ data: given.data }));
|
|
454
|
+
res.end(JSON.stringify({ data: shape ? shape(given.data) : given.data }));
|
|
362
455
|
};
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
456
|
+
// what a game may: read the developer's, the game's, and its own
|
|
457
|
+
// player's, and write only its own player's, in every item
|
|
458
|
+
const allowed = function (scope, named) {
|
|
459
|
+
return !(scope === "server" ||
|
|
460
|
+
(write && scope !== "player") ||
|
|
461
|
+
(scope === "player" && (!player || named !== player.id)));
|
|
462
|
+
};
|
|
463
|
+
if (verb === "list") {
|
|
464
|
+
const scope = SCOPES.find((each) => each === body["scope"]);
|
|
465
|
+
if (!scope) {
|
|
466
|
+
refuse(400);
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
if (!allowed(scope, body["player"])) {
|
|
470
|
+
refuse(403);
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
const owner = scope === "player" && player ? player.id : "game";
|
|
367
474
|
answer(values.list(scope, owner, {
|
|
368
475
|
after: body["after"] ?? "",
|
|
369
476
|
limit: body["limit"] ?? 50,
|
|
370
477
|
prefix: body["prefix"] ?? "",
|
|
371
478
|
}));
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
481
|
+
const call = valueItemsOf(body, verb);
|
|
482
|
+
if (!call) {
|
|
483
|
+
refuse(400);
|
|
484
|
+
return;
|
|
485
|
+
}
|
|
486
|
+
const { items, single } = call;
|
|
487
|
+
if (!items.every((item) => allowed(item.scope, item.scope === "player" ? item.owner : null))) {
|
|
488
|
+
refuse(403);
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
const shape = (data) => itemsAnswer(verb, items, single, data);
|
|
492
|
+
if (verb === "read") {
|
|
493
|
+
answer(values.readItems(items), shape);
|
|
372
494
|
}
|
|
373
495
|
else if (verb === "write") {
|
|
374
|
-
answer(values.
|
|
496
|
+
answer(values.writeItems(items), shape);
|
|
375
497
|
}
|
|
376
498
|
else {
|
|
377
|
-
answer(values.
|
|
499
|
+
answer(values.removeItems(items));
|
|
378
500
|
}
|
|
379
501
|
}
|
|
380
502
|
// [function]
|
|
@@ -392,6 +514,44 @@ function sendData(req, res, status, data, count) {
|
|
|
392
514
|
endWith(req, res, JSON.stringify({ data, meta }));
|
|
393
515
|
}
|
|
394
516
|
// [function]
|
|
517
|
+
// the player a window's address names: `?player=<name>`, a local player
|
|
518
|
+
// made from the name and the game, as `--player` makes one; `?player=` with
|
|
519
|
+
// no name, a guest (null); without it, undefined, the page's own. False for
|
|
520
|
+
// a name that can't be one.
|
|
521
|
+
function playerAsked(req, playerOf) {
|
|
522
|
+
let query;
|
|
523
|
+
try {
|
|
524
|
+
query = new URL(req.url ?? "", "http://page").searchParams;
|
|
525
|
+
}
|
|
526
|
+
catch {
|
|
527
|
+
return undefined;
|
|
528
|
+
}
|
|
529
|
+
const name = query.get("player");
|
|
530
|
+
if (name === null)
|
|
531
|
+
return undefined;
|
|
532
|
+
if (name.trim() === "")
|
|
533
|
+
return null;
|
|
534
|
+
return playerOf(name) || false;
|
|
535
|
+
}
|
|
536
|
+
// [function]
|
|
537
|
+
// a window's own player and the rest of the path, from a path under
|
|
538
|
+
// `/_arcadible/as/<id>/`: undefined for any other path, and false for an id
|
|
539
|
+
// no window was opened as.
|
|
540
|
+
function windowOf(pathname, named) {
|
|
541
|
+
if (!pathname.startsWith(`${AS}/`))
|
|
542
|
+
return undefined;
|
|
543
|
+
const rest = pathname.slice(AS.length + 1);
|
|
544
|
+
const cut = rest.indexOf("/");
|
|
545
|
+
if (cut < 0)
|
|
546
|
+
return false;
|
|
547
|
+
const key = rest.slice(0, cut);
|
|
548
|
+
const path = rest.slice(cut);
|
|
549
|
+
if (key === GUEST)
|
|
550
|
+
return { path, player: null };
|
|
551
|
+
const player = named.get(key);
|
|
552
|
+
return player ? { path, player } : false;
|
|
553
|
+
}
|
|
554
|
+
// [function]
|
|
395
555
|
// a refusal in the API's own envelope: what it says of the status.
|
|
396
556
|
function sendRefusal(req, res, status) {
|
|
397
557
|
const [title, message] = REFUSALS[status] ?? ["", ""];
|
|
@@ -413,43 +573,51 @@ async function sendValues(req, res, verb, values) {
|
|
|
413
573
|
sendRefusal(req, res, values ? 400 : 404);
|
|
414
574
|
return;
|
|
415
575
|
}
|
|
416
|
-
const
|
|
417
|
-
const player = body["player"];
|
|
418
|
-
if (!scope ||
|
|
419
|
-
(scope === "player" &&
|
|
420
|
-
(typeof player !== "string" || !PLAYER_ID.test(player)))) {
|
|
421
|
-
sendRefusal(req, res, 400);
|
|
422
|
-
return;
|
|
423
|
-
}
|
|
424
|
-
// a player's are the player's, and every other scope's the game's: one
|
|
425
|
-
// game is served, so any owner will do for those.
|
|
426
|
-
const owner = scope === "player" ? String(player) : "game";
|
|
427
|
-
const answer = function (given) {
|
|
576
|
+
const answer = function (given, shape) {
|
|
428
577
|
if (given.status === 200) {
|
|
429
|
-
sendData(req, res, 200, given.data);
|
|
578
|
+
sendData(req, res, 200, shape ? shape(given.data) : given.data);
|
|
430
579
|
}
|
|
431
580
|
else {
|
|
432
581
|
sendRefusal(req, res, given.status);
|
|
433
582
|
}
|
|
434
583
|
};
|
|
435
|
-
if (verb === "
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
584
|
+
if (verb === "list") {
|
|
585
|
+
const scope = SCOPES.find((each) => each === body["scope"]);
|
|
586
|
+
const player = body["player"];
|
|
587
|
+
if (!scope ||
|
|
588
|
+
(scope === "player" &&
|
|
589
|
+
(typeof player !== "string" || !PLAYER_ID.test(player)))) {
|
|
590
|
+
sendRefusal(req, res, 400);
|
|
591
|
+
return;
|
|
592
|
+
}
|
|
593
|
+
// a player's are the player's, and every other scope's the game's: one
|
|
594
|
+
// game is served, so any owner will do for those.
|
|
595
|
+
const owner = scope === "player" ? String(player) : "game";
|
|
439
596
|
answer(values.list(scope, owner, {
|
|
440
597
|
after: body["after"] ?? "",
|
|
441
598
|
limit: body["limit"] ?? 50,
|
|
442
599
|
prefix: body["prefix"] ?? "",
|
|
443
600
|
}));
|
|
601
|
+
return;
|
|
602
|
+
}
|
|
603
|
+
const call = valueItemsOf(body, verb);
|
|
604
|
+
if (!call) {
|
|
605
|
+
sendRefusal(req, res, 400);
|
|
606
|
+
return;
|
|
607
|
+
}
|
|
608
|
+
const { items, single } = call;
|
|
609
|
+
const shape = (data) => itemsAnswer(verb, items, single, data);
|
|
610
|
+
if (verb === "read") {
|
|
611
|
+
answer(values.readItems(items), shape);
|
|
444
612
|
}
|
|
445
|
-
else if (!VALUE_WRITES.includes(scope)) {
|
|
613
|
+
else if (!items.every((item) => VALUE_WRITES.includes(item.scope))) {
|
|
446
614
|
sendRefusal(req, res, 403);
|
|
447
615
|
}
|
|
448
616
|
else if (verb === "write") {
|
|
449
|
-
answer(values.
|
|
617
|
+
answer(values.writeItems(items), shape);
|
|
450
618
|
}
|
|
451
619
|
else {
|
|
452
|
-
answer(values.
|
|
620
|
+
answer(values.removeItems(items));
|
|
453
621
|
}
|
|
454
622
|
}
|
|
455
623
|
// [function]
|
|
@@ -532,9 +700,19 @@ export function pageHandler({ art = () => Promise.resolve(null), faviconFile = F
|
|
|
532
700
|
return null;
|
|
533
701
|
});
|
|
534
702
|
};
|
|
703
|
+
// the players windows were opened as, by id, so a window's path names one
|
|
704
|
+
const named = new Map();
|
|
535
705
|
return function (req, res) {
|
|
536
706
|
// a target that can't be read as a URL (`//`) is no path of the page's.
|
|
537
|
-
const
|
|
707
|
+
const whole = pathOf(req) ?? "";
|
|
708
|
+
// a window's own player, from its path (undefined: the page's default)
|
|
709
|
+
const opened = windowOf(whole, named);
|
|
710
|
+
if (opened === false) {
|
|
711
|
+
res.writeHead(404, HEADERS).end();
|
|
712
|
+
return Promise.resolve();
|
|
713
|
+
}
|
|
714
|
+
const pathname = opened ? opened.path : whole;
|
|
715
|
+
const playing = opened ? opened.player : (game.player ?? null);
|
|
538
716
|
if (tickets && pathname.startsWith(`${SERVER}/`)) {
|
|
539
717
|
return sendServer(req, res, pathname.slice(SERVER.length), {
|
|
540
718
|
game,
|
|
@@ -552,9 +730,9 @@ export function pageHandler({ art = () => Promise.resolve(null), faviconFile = F
|
|
|
552
730
|
res.writeHead(405, { ...HEADERS, allow: "POST" }).end();
|
|
553
731
|
return Promise.resolve();
|
|
554
732
|
}
|
|
555
|
-
return sendGameValues(req, res, verb, {
|
|
733
|
+
return sendGameValues(req, res, verb, { player: playing, values });
|
|
556
734
|
}
|
|
557
|
-
if (pathname === LOGIN) {
|
|
735
|
+
if (pathname === LOGIN && !opened) {
|
|
558
736
|
if (req.method !== "POST") {
|
|
559
737
|
res.writeHead(405, { ...HEADERS, allow: "POST" }).end();
|
|
560
738
|
return Promise.resolve();
|
|
@@ -566,13 +744,33 @@ export function pageHandler({ art = () => Promise.resolve(null), faviconFile = F
|
|
|
566
744
|
res.writeHead(405, { ...HEADERS, allow: "POST" }).end();
|
|
567
745
|
return Promise.resolve();
|
|
568
746
|
}
|
|
569
|
-
return sendTicket(req, res, {
|
|
747
|
+
return sendTicket(req, res, { playerOf, playing, tickets });
|
|
748
|
+
}
|
|
749
|
+
if (opened) {
|
|
750
|
+
// only the page's own API is under a window's path
|
|
751
|
+
res.writeHead(404, HEADERS).end();
|
|
752
|
+
return Promise.resolve();
|
|
570
753
|
}
|
|
571
754
|
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
572
755
|
res.writeHead(405, { ...HEADERS, allow: "GET, HEAD" }).end();
|
|
573
756
|
return Promise.resolve();
|
|
574
757
|
}
|
|
575
758
|
if (pathname === "/") {
|
|
759
|
+
// the window's player, when its address names one: `?player=<name>`,
|
|
760
|
+
// or `?player=` for a guest
|
|
761
|
+
const asked = playerAsked(req, playerOf);
|
|
762
|
+
if (asked === false) {
|
|
763
|
+
res.writeHead(400, {
|
|
764
|
+
...HEADERS,
|
|
765
|
+
"content-type": "text/plain; charset=utf-8",
|
|
766
|
+
});
|
|
767
|
+
res.end("?player= must be a name, up to 200 characters, or empty for a guest.\n");
|
|
768
|
+
return Promise.resolve();
|
|
769
|
+
}
|
|
770
|
+
if (asked) {
|
|
771
|
+
named.set(asked.id, asked);
|
|
772
|
+
tickets?.remember(asked);
|
|
773
|
+
}
|
|
576
774
|
// read for each request, so art added while serving shows on the next
|
|
577
775
|
// load.
|
|
578
776
|
return Promise.all([
|
|
@@ -584,7 +782,7 @@ export function pageHandler({ art = () => Promise.resolve(null), faviconFile = F
|
|
|
584
782
|
"content-security-policy": policy(game.src),
|
|
585
783
|
"content-type": "text/html; charset=utf-8",
|
|
586
784
|
});
|
|
587
|
-
endWith(req, res, html(game, { art: Boolean(splash), icon: Boolean(mark) }));
|
|
785
|
+
endWith(req, res, html(game, { art: Boolean(splash), icon: Boolean(mark) }, asked === undefined ? undefined : { player: asked }));
|
|
588
786
|
});
|
|
589
787
|
}
|
|
590
788
|
if (pathname === ICON) {
|