arcadible 1.0.31 → 1.0.33
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/serve/page.js +216 -46
- package/dist/serve/runner.js +79 -7
- package/dist/serve/ticket.js +17 -3
- package/dist/serve/values.js +166 -116
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
package/dist/serve/page.js
CHANGED
|
@@ -325,6 +325,122 @@ async function sendLogIn(req, res, { game, playerOf, tickets, }) {
|
|
|
325
325
|
res.writeHead(204, HEADERS).end();
|
|
326
326
|
}
|
|
327
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]
|
|
424
|
+
// the items a conflict names, as the API says them: in the items form,
|
|
425
|
+
// each with its scope (and `player` for a player's) and its key; a single
|
|
426
|
+
// scope's, its key.
|
|
427
|
+
function conflictsAnswer(conflicts, single) {
|
|
428
|
+
return conflicts.map((item) => single
|
|
429
|
+
? { key: item.key }
|
|
430
|
+
: {
|
|
431
|
+
scope: item.scope,
|
|
432
|
+
...(item.scope === "player" ? { player: item.owner } : {}),
|
|
433
|
+
key: item.key,
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
// [function]
|
|
437
|
+
// whether a remove skips its conflicts: a body's `on_conflict`, "skip", or
|
|
438
|
+
// "reject", as with none. Null for anything else.
|
|
439
|
+
function skipsOf(body) {
|
|
440
|
+
const given = body["on_conflict"] ?? "reject";
|
|
441
|
+
return given === "skip" ? true : given === "reject" ? false : null;
|
|
442
|
+
}
|
|
443
|
+
// [function]
|
|
328
444
|
// a game's values, for the page (the game's host), as the play site's route
|
|
329
445
|
// answers (arcadible-play's /api/games/<game>/values/<verb>, and the API
|
|
330
446
|
// behind it): the game's call, read, written, removed, or listed, for who
|
|
@@ -332,7 +448,8 @@ async function sendLogIn(req, res, { game, playerOf, tickets, }) {
|
|
|
332
448
|
// and reads and writes its own player's: the server's scope, a write to any
|
|
333
449
|
// but a player's, a guest's call for a player's, or a player who isn't the
|
|
334
450
|
// one playing is 403. Only JSON is taken, as for a ticket, so a page
|
|
335
|
-
// elsewhere can't ask. A refusal is its status, with no body
|
|
451
|
+
// elsewhere can't ask. A refusal is its status, with no body, but a
|
|
452
|
+
// conflict's (409), which names its items: `{ data: { conflicts } }`.
|
|
336
453
|
async function sendGameValues(req, res, verb, { player, values }) {
|
|
337
454
|
const refuse = function (status) {
|
|
338
455
|
res.writeHead(status, HEADERS).end();
|
|
@@ -345,45 +462,76 @@ async function sendGameValues(req, res, verb, { player, values }) {
|
|
|
345
462
|
refuse(400);
|
|
346
463
|
return;
|
|
347
464
|
}
|
|
348
|
-
const scope = SCOPES.find((each) => each === body["scope"]);
|
|
349
|
-
if (!scope) {
|
|
350
|
-
refuse(400);
|
|
351
|
-
return;
|
|
352
|
-
}
|
|
353
465
|
const write = verb === "write" || verb === "remove";
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
return;
|
|
359
|
-
}
|
|
360
|
-
const owner = scope === "player" && player ? player.id : "game";
|
|
361
|
-
const answer = function (given) {
|
|
362
|
-
if (given.status !== 200) {
|
|
466
|
+
// whether the call names one scope for all its keys; a list's does
|
|
467
|
+
let single = true;
|
|
468
|
+
const answer = function (given, shape) {
|
|
469
|
+
if (given.status !== 200 && given.status !== 409) {
|
|
363
470
|
refuse(given.status);
|
|
364
471
|
return;
|
|
365
472
|
}
|
|
366
|
-
res.writeHead(
|
|
473
|
+
res.writeHead(given.status, {
|
|
367
474
|
...HEADERS,
|
|
368
475
|
"content-type": "application/json; charset=utf-8",
|
|
369
476
|
});
|
|
370
|
-
res.end(JSON.stringify({
|
|
477
|
+
res.end(JSON.stringify({
|
|
478
|
+
data: given.status === 409
|
|
479
|
+
? { conflicts: conflictsAnswer(given.conflicts, single) }
|
|
480
|
+
: shape
|
|
481
|
+
? shape(given.data)
|
|
482
|
+
: given.data,
|
|
483
|
+
}));
|
|
371
484
|
};
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
485
|
+
// what a game may: read the developer's, the game's, and its own
|
|
486
|
+
// player's, and write only its own player's, in every item
|
|
487
|
+
const allowed = function (scope, named) {
|
|
488
|
+
return !(scope === "server" ||
|
|
489
|
+
(write && scope !== "player") ||
|
|
490
|
+
(scope === "player" && (!player || named !== player.id)));
|
|
491
|
+
};
|
|
492
|
+
if (verb === "list") {
|
|
493
|
+
const scope = SCOPES.find((each) => each === body["scope"]);
|
|
494
|
+
if (!scope) {
|
|
495
|
+
refuse(400);
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
if (!allowed(scope, body["player"])) {
|
|
499
|
+
refuse(403);
|
|
500
|
+
return;
|
|
501
|
+
}
|
|
502
|
+
const owner = scope === "player" && player ? player.id : "game";
|
|
376
503
|
answer(values.list(scope, owner, {
|
|
377
504
|
after: body["after"] ?? "",
|
|
378
505
|
limit: body["limit"] ?? 50,
|
|
379
506
|
prefix: body["prefix"] ?? "",
|
|
380
507
|
}));
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
const call = valueItemsOf(body, verb);
|
|
511
|
+
if (!call) {
|
|
512
|
+
refuse(400);
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
const { items } = call;
|
|
516
|
+
({ single } = call);
|
|
517
|
+
if (!items.every((item) => allowed(item.scope, item.scope === "player" ? item.owner : null))) {
|
|
518
|
+
refuse(403);
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
const skips = verb === "remove" ? skipsOf(body) : false;
|
|
522
|
+
if (skips === null) {
|
|
523
|
+
refuse(400);
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
const shape = (data) => itemsAnswer(verb, items, single, data);
|
|
527
|
+
if (verb === "read") {
|
|
528
|
+
answer(values.readItems(items), shape);
|
|
381
529
|
}
|
|
382
530
|
else if (verb === "write") {
|
|
383
|
-
answer(values.
|
|
531
|
+
answer(values.writeItems(items), shape);
|
|
384
532
|
}
|
|
385
533
|
else {
|
|
386
|
-
answer(values.
|
|
534
|
+
answer(values.removeItems(items, skips));
|
|
387
535
|
}
|
|
388
536
|
}
|
|
389
537
|
// [function]
|
|
@@ -439,64 +587,86 @@ function windowOf(pathname, named) {
|
|
|
439
587
|
return player ? { path, player } : false;
|
|
440
588
|
}
|
|
441
589
|
// [function]
|
|
442
|
-
// a refusal in the API's own envelope: what it says of the status
|
|
443
|
-
|
|
590
|
+
// a refusal in the API's own envelope: what it says of the status, and,
|
|
591
|
+
// for a conflict, the items it names.
|
|
592
|
+
function sendRefusal(req, res, status, conflicts) {
|
|
444
593
|
const [title, message] = REFUSALS[status] ?? ["", ""];
|
|
445
594
|
sendData(req, res, status, {
|
|
446
595
|
code: status,
|
|
447
596
|
message,
|
|
448
597
|
title,
|
|
449
598
|
type: "HttpError",
|
|
599
|
+
...(conflicts ? { conflicts } : {}),
|
|
450
600
|
});
|
|
451
601
|
}
|
|
452
602
|
// [function]
|
|
453
603
|
// a game's values, for its server, as the API's own route answers
|
|
454
604
|
// (app/src/web/arcadible-api's post_server_values): the scope the body names,
|
|
455
605
|
// with `player` for a player's, read, written, removed, or listed. A server
|
|
456
|
-
// reads every scope and writes all but the developer's.
|
|
606
|
+
// reads every scope and writes all but the developer's. A conflict names
|
|
607
|
+
// its items (`conflicts`), and a remove with `on_conflict`, "skip", leaves
|
|
608
|
+
// those and removes the rest.
|
|
457
609
|
async function sendValues(req, res, verb, values) {
|
|
458
610
|
const body = await readJson(req, VALUES_BODY_MAX);
|
|
459
611
|
if (!values || !isTable(body)) {
|
|
460
612
|
sendRefusal(req, res, values ? 400 : 404);
|
|
461
613
|
return;
|
|
462
614
|
}
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
(scope === "player" &&
|
|
467
|
-
(typeof player !== "string" || !PLAYER_ID.test(player)))) {
|
|
468
|
-
sendRefusal(req, res, 400);
|
|
469
|
-
return;
|
|
470
|
-
}
|
|
471
|
-
// a player's are the player's, and every other scope's the game's: one
|
|
472
|
-
// game is served, so any owner will do for those.
|
|
473
|
-
const owner = scope === "player" ? String(player) : "game";
|
|
474
|
-
const answer = function (given) {
|
|
615
|
+
// whether the call names one scope for all its keys; a list's does
|
|
616
|
+
let single = true;
|
|
617
|
+
const answer = function (given, shape) {
|
|
475
618
|
if (given.status === 200) {
|
|
476
|
-
sendData(req, res, 200, given.data);
|
|
619
|
+
sendData(req, res, 200, shape ? shape(given.data) : given.data);
|
|
620
|
+
}
|
|
621
|
+
else if (given.status === 409) {
|
|
622
|
+
sendRefusal(req, res, 409, conflictsAnswer(given.conflicts, single));
|
|
477
623
|
}
|
|
478
624
|
else {
|
|
479
625
|
sendRefusal(req, res, given.status);
|
|
480
626
|
}
|
|
481
627
|
};
|
|
482
|
-
if (verb === "
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
628
|
+
if (verb === "list") {
|
|
629
|
+
const scope = SCOPES.find((each) => each === body["scope"]);
|
|
630
|
+
const player = body["player"];
|
|
631
|
+
if (!scope ||
|
|
632
|
+
(scope === "player" &&
|
|
633
|
+
(typeof player !== "string" || !PLAYER_ID.test(player)))) {
|
|
634
|
+
sendRefusal(req, res, 400);
|
|
635
|
+
return;
|
|
636
|
+
}
|
|
637
|
+
// a player's are the player's, and every other scope's the game's: one
|
|
638
|
+
// game is served, so any owner will do for those.
|
|
639
|
+
const owner = scope === "player" ? String(player) : "game";
|
|
486
640
|
answer(values.list(scope, owner, {
|
|
487
641
|
after: body["after"] ?? "",
|
|
488
642
|
limit: body["limit"] ?? 50,
|
|
489
643
|
prefix: body["prefix"] ?? "",
|
|
490
644
|
}));
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
647
|
+
const call = valueItemsOf(body, verb);
|
|
648
|
+
if (!call) {
|
|
649
|
+
sendRefusal(req, res, 400);
|
|
650
|
+
return;
|
|
491
651
|
}
|
|
492
|
-
|
|
652
|
+
const { items } = call;
|
|
653
|
+
({ single } = call);
|
|
654
|
+
const skips = verb === "remove" ? skipsOf(body) : false;
|
|
655
|
+
const shape = (data) => itemsAnswer(verb, items, single, data);
|
|
656
|
+
if (verb === "read") {
|
|
657
|
+
answer(values.readItems(items), shape);
|
|
658
|
+
}
|
|
659
|
+
else if (!items.every((item) => VALUE_WRITES.includes(item.scope))) {
|
|
493
660
|
sendRefusal(req, res, 403);
|
|
494
661
|
}
|
|
662
|
+
else if (skips === null) {
|
|
663
|
+
sendRefusal(req, res, 400);
|
|
664
|
+
}
|
|
495
665
|
else if (verb === "write") {
|
|
496
|
-
answer(values.
|
|
666
|
+
answer(values.writeItems(items), shape);
|
|
497
667
|
}
|
|
498
668
|
else {
|
|
499
|
-
answer(values.
|
|
669
|
+
answer(values.removeItems(items, skips));
|
|
500
670
|
}
|
|
501
671
|
}
|
|
502
672
|
// [function]
|
package/dist/serve/runner.js
CHANGED
|
@@ -36,6 +36,14 @@ export const DROPPED = [
|
|
|
36
36
|
// the code a game is given when its function failed, as on Arcadible: it
|
|
37
37
|
// threw, or it isn't running.
|
|
38
38
|
export const FAILED = "function_failed";
|
|
39
|
+
// the largest body a request may bring a function, in bytes, as on
|
|
40
|
+
// Arcadible, where the dispatcher refuses one past it with TOO_LARGE.
|
|
41
|
+
export const BODY_MAX = 1024 * 1024;
|
|
42
|
+
export const TOO_LARGE = "too_large";
|
|
43
|
+
// what marks an answer as Arcadible's own, with its code, as the dispatcher
|
|
44
|
+
// marks its refusals: dropped from what a function's code answers, so a
|
|
45
|
+
// game can't be told a refusal that isn't one.
|
|
46
|
+
export const REFUSAL = "x-arcadible-refusal";
|
|
39
47
|
// the runtime, fetched the first time a project needs it: workerd is about
|
|
40
48
|
// 150 MB, and most games have no runner, so it's no dependency of the CLI
|
|
41
49
|
// (the bundler is: `arc build` bundles a runner too, ../runner/bundle.ts).
|
|
@@ -46,15 +54,26 @@ const COMPATIBILITY_DATE = "2026-01-01";
|
|
|
46
54
|
const OUTBOUND = join(import.meta.dirname, "..", "runner", "outbound.js");
|
|
47
55
|
// what stands in front of the function's code in the runtime, as the
|
|
48
56
|
// dispatcher does on Arcadible: code that throws is the game's
|
|
49
|
-
// `function_failed`,
|
|
50
|
-
|
|
57
|
+
// `function_failed`, marked as Arcadible's, and the error is the
|
|
58
|
+
// terminal's, where its developer is; what the code answers goes back
|
|
59
|
+
// without the mark.
|
|
60
|
+
const FRONT = `const REFUSAL = ${JSON.stringify(REFUSAL)};
|
|
61
|
+
export default {
|
|
51
62
|
async fetch(request, env) {
|
|
63
|
+
let answer;
|
|
52
64
|
try {
|
|
53
|
-
|
|
65
|
+
answer = await env.RUNNER.fetch(request);
|
|
54
66
|
} catch (err) {
|
|
55
67
|
console.error("The function threw:", String(err));
|
|
56
|
-
return Response.json(
|
|
68
|
+
return Response.json(
|
|
69
|
+
{ code: ${JSON.stringify(FAILED)} },
|
|
70
|
+
{ headers: { [REFUSAL]: ${JSON.stringify(FAILED)} }, status: 502 },
|
|
71
|
+
);
|
|
57
72
|
}
|
|
73
|
+
if (!answer.headers.has(REFUSAL)) return answer;
|
|
74
|
+
const headers = new Headers(answer.headers);
|
|
75
|
+
headers.delete(REFUSAL);
|
|
76
|
+
return new Response(answer.body, { headers, status: answer.status, statusText: answer.statusText });
|
|
58
77
|
},
|
|
59
78
|
};
|
|
60
79
|
`;
|
|
@@ -64,6 +83,8 @@ const READY_MS = 15000;
|
|
|
64
83
|
// origin, and a dev server framed with --url is another origin.
|
|
65
84
|
const CORS = {
|
|
66
85
|
"access-control-allow-headers": "authorization, content-type",
|
|
86
|
+
// so a game on another origin (--url) reads Arcadible's refusal too
|
|
87
|
+
"access-control-expose-headers": REFUSAL,
|
|
67
88
|
"access-control-allow-methods": "GET, POST, PUT, PATCH, DELETE, OPTIONS",
|
|
68
89
|
"access-control-allow-origin": "*",
|
|
69
90
|
"access-control-max-age": "600",
|
|
@@ -192,6 +213,30 @@ const outbound :Workerd.Worker = (
|
|
|
192
213
|
`;
|
|
193
214
|
}
|
|
194
215
|
// [function]
|
|
216
|
+
// a request's body, held to BODY_MAX: `true` when it fits as it came, with
|
|
217
|
+
// a length it says (Node holds the bytes to it), its bytes when it fits and
|
|
218
|
+
// came without one (streamed), or `null` when it's larger.
|
|
219
|
+
async function bodyOf(req) {
|
|
220
|
+
const said = req.headers["content-length"];
|
|
221
|
+
if (said !== undefined && /^\d+$/u.test(said.trim())) {
|
|
222
|
+
return Number(said) <= BODY_MAX ? true : null;
|
|
223
|
+
}
|
|
224
|
+
if (req.headers["transfer-encoding"] === undefined)
|
|
225
|
+
return true;
|
|
226
|
+
const parts = [];
|
|
227
|
+
let size = 0;
|
|
228
|
+
for await (const chunk of req) {
|
|
229
|
+
const part = chunk;
|
|
230
|
+
size += part.length;
|
|
231
|
+
if (size > BODY_MAX) {
|
|
232
|
+
req.resume();
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
parts.push(part);
|
|
236
|
+
}
|
|
237
|
+
return Buffer.concat(parts);
|
|
238
|
+
}
|
|
239
|
+
// [function]
|
|
195
240
|
// a game's runner, running. `main` is its code's path, `dir` where the
|
|
196
241
|
// runtime is kept, `origin` the game's own origin (a ticket for the runner
|
|
197
242
|
// is for it), `serve` arc serve's page (where Arcadible is, here), `hosts`
|
|
@@ -254,15 +299,35 @@ export async function runner({ dir, handler, hosts = [], main, origin, secrets =
|
|
|
254
299
|
? await tickets.check(ticket, origin)
|
|
255
300
|
: { data: { code: "invalid" }, status: 422 };
|
|
256
301
|
if (checked.status !== 200) {
|
|
257
|
-
res.writeHead(401, {
|
|
302
|
+
res.writeHead(401, {
|
|
303
|
+
...CORS,
|
|
304
|
+
"content-type": "application/json",
|
|
305
|
+
[REFUSAL]: checked.data.code,
|
|
306
|
+
});
|
|
258
307
|
res.end(JSON.stringify({ code: checked.data.code }));
|
|
259
308
|
return;
|
|
260
309
|
}
|
|
310
|
+
// a body past what a function may be sent, said or streamed, as the
|
|
311
|
+
// dispatcher refuses it: a streamed one is read here, to hold it
|
|
312
|
+
const body = await bodyOf(req);
|
|
313
|
+
if (body === null) {
|
|
314
|
+
res.writeHead(413, {
|
|
315
|
+
...CORS,
|
|
316
|
+
"content-type": "application/json",
|
|
317
|
+
[REFUSAL]: TOO_LARGE,
|
|
318
|
+
});
|
|
319
|
+
res.end(JSON.stringify({ code: TOO_LARGE }));
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
261
322
|
// the game's answer when its function failed, as the dispatcher's: why
|
|
262
323
|
// is the terminal's, said as it happened
|
|
263
324
|
const fail = function () {
|
|
264
325
|
if (!res.headersSent) {
|
|
265
|
-
res.writeHead(502, {
|
|
326
|
+
res.writeHead(502, {
|
|
327
|
+
...CORS,
|
|
328
|
+
"content-type": "application/json",
|
|
329
|
+
[REFUSAL]: FAILED,
|
|
330
|
+
});
|
|
266
331
|
}
|
|
267
332
|
res.end(JSON.stringify({ code: FAILED }));
|
|
268
333
|
};
|
|
@@ -276,6 +341,10 @@ export async function runner({ dir, handler, hosts = [], main, origin, secrets =
|
|
|
276
341
|
...Object.fromEntries(Object.entries(req.headers).filter(([name]) => !dropped.includes(name))),
|
|
277
342
|
[PLAYER]: typeof player === "string" ? player : "",
|
|
278
343
|
};
|
|
344
|
+
if (body !== true) {
|
|
345
|
+
delete headers["transfer-encoding"];
|
|
346
|
+
headers["content-length"] = String(body.length);
|
|
347
|
+
}
|
|
279
348
|
// `request` is called through the module, as the tests fake Node's own.
|
|
280
349
|
const forward = http.request({
|
|
281
350
|
headers,
|
|
@@ -288,7 +357,10 @@ export async function runner({ dir, handler, hosts = [], main, origin, secrets =
|
|
|
288
357
|
answer.pipe(res);
|
|
289
358
|
});
|
|
290
359
|
forward.on("error", fail);
|
|
291
|
-
|
|
360
|
+
if (body === true)
|
|
361
|
+
req.pipe(forward);
|
|
362
|
+
else
|
|
363
|
+
forward.end(body);
|
|
292
364
|
};
|
|
293
365
|
await start();
|
|
294
366
|
return {
|
package/dist/serve/ticket.js
CHANGED
|
@@ -18,12 +18,17 @@ const generateKeyPair = promisify(crypto.generateKeyPair);
|
|
|
18
18
|
export const CHANNEL = "local";
|
|
19
19
|
// how long a ticket is good for, as Arcadible's.
|
|
20
20
|
export const LIFETIME = 600;
|
|
21
|
+
// how far a clock may be from the signer's, in seconds, either way, as the
|
|
22
|
+
// Server SDK's and the API's checks allow.
|
|
23
|
+
export const LEEWAY = 30;
|
|
21
24
|
// the ticket's own type, as Arcadible's.
|
|
22
25
|
export const TYPE = "arcadible-ticket+jwt";
|
|
23
26
|
// the hosts that are this machine: a server on one needs no declaring.
|
|
24
27
|
const LOOPBACK = new Set(["localhost", "127.0.0.1", "[::1]"]);
|
|
25
28
|
// the longest name a player is given.
|
|
26
29
|
const NAME_MAX = 200;
|
|
30
|
+
// A player's id in a game, a ticket's `sub`: a UUID, in lowercase.
|
|
31
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/u;
|
|
27
32
|
// [function]
|
|
28
33
|
function b64(data) {
|
|
29
34
|
return Buffer.from(data).toString("base64url");
|
|
@@ -160,17 +165,26 @@ export function tickets({ file, game, issuer, now = () => Date.now(), servers =
|
|
|
160
165
|
claims["game"] !== game) {
|
|
161
166
|
return refused("invalid");
|
|
162
167
|
}
|
|
168
|
+
// a player is named by their id in the game, a UUID, as the API and
|
|
169
|
+
// the Server SDK ask; no `sub` is a guest
|
|
170
|
+
const sub = claims["sub"];
|
|
171
|
+
if (sub !== undefined && (typeof sub !== "string" || !UUID.test(sub))) {
|
|
172
|
+
return refused("invalid");
|
|
173
|
+
}
|
|
163
174
|
const exp = claims["exp"];
|
|
164
|
-
|
|
175
|
+
// good until its time and the leeway a clock is allowed, as the
|
|
176
|
+
// Server SDK's `tickets.check` and the API's check give it
|
|
177
|
+
if (typeof exp === "number" && exp <= now() / 1000 - LEEWAY) {
|
|
165
178
|
return refused("expired");
|
|
166
179
|
}
|
|
167
180
|
return {
|
|
168
181
|
data: {
|
|
169
182
|
channel: claims["channel"],
|
|
170
|
-
|
|
183
|
+
// an ISO time, as the API answers one, never the ticket's seconds
|
|
184
|
+
expires_at: typeof exp === "number" ? new Date(exp * 1000).toISOString() : null,
|
|
171
185
|
game: claims["game"],
|
|
172
186
|
id: claims["jti"],
|
|
173
|
-
player:
|
|
187
|
+
player: sub ?? null,
|
|
174
188
|
site: claims["site"],
|
|
175
189
|
version: claims["version"],
|
|
176
190
|
},
|
package/dist/serve/values.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// same ones. It answers as Arcadible's database functions do
|
|
5
5
|
// (app/src/web/arcadible-api/db/function/game_value.sql): the same keys,
|
|
6
6
|
// the same limits, a write of several all or none, a named revision
|
|
7
|
-
// replaced or a conflict, with the API's statuses. The developer's values
|
|
7
|
+
// replaced or a conflict, which names the items, with the API's statuses. The developer's values
|
|
8
8
|
// are the project's own files, read as they change, and never written here;
|
|
9
9
|
// the rest are kept in arc's folder in the project, so a match survives a
|
|
10
10
|
// restart.
|
|
@@ -56,17 +56,38 @@ export function sizeOf(value) {
|
|
|
56
56
|
return Buffer.byteLength(text(value));
|
|
57
57
|
}
|
|
58
58
|
// [function]
|
|
59
|
-
// a
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
59
|
+
// a whole number a revision may be: 0 or more.
|
|
60
|
+
function isRevision(value) {
|
|
61
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
|
|
62
|
+
}
|
|
63
|
+
// [function]
|
|
64
|
+
// a call's items, or null: 1 to a call's keys, each an object with a scope,
|
|
65
|
+
// an owner, and a key, none named twice, and each of the verb's own fields
|
|
66
|
+
// (`fields`) a revision where it's given; `value`, for a write, there.
|
|
67
|
+
function itemsOf(given, fields, needsValue) {
|
|
68
|
+
if (!Array.isArray(given) ||
|
|
69
|
+
given.length < 1 ||
|
|
70
|
+
given.length > LIMITS.callKeys) {
|
|
67
71
|
return null;
|
|
68
72
|
}
|
|
69
|
-
|
|
73
|
+
const seen = new Set();
|
|
74
|
+
for (const item of given) {
|
|
75
|
+
if (!isTable(item) ||
|
|
76
|
+
!SCOPES.includes(item["scope"]) ||
|
|
77
|
+
typeof item["owner"] !== "string" ||
|
|
78
|
+
typeof item["key"] !== "string" ||
|
|
79
|
+
!KEY.test(item["key"]) ||
|
|
80
|
+
(needsValue && !Object.hasOwn(item, "value")) ||
|
|
81
|
+
fields.some((field) => item[field] !== undefined && !isRevision(item[field]))) {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
const name = `${String(item["scope"])}:${item["owner"]}:${item["key"]}`;
|
|
85
|
+
if (seen.has(name)) {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
seen.add(name);
|
|
89
|
+
}
|
|
90
|
+
return given;
|
|
70
91
|
}
|
|
71
92
|
// [function]
|
|
72
93
|
// the developer's values, from the project's folder: each `.json` file a
|
|
@@ -164,7 +185,142 @@ export function values({ isPlayer, now = () => Date.now(), root, }) {
|
|
|
164
185
|
const refusal = function (scope, owner) {
|
|
165
186
|
return scope === "player" && !isPlayer(owner) ? 403 : null;
|
|
166
187
|
};
|
|
188
|
+
// the first refusal of the items' owners, as the database checks each
|
|
189
|
+
const refusalOf = function (items) {
|
|
190
|
+
return items.some((item) => refusal(item.scope, item.owner)) ? 403 : null;
|
|
191
|
+
};
|
|
192
|
+
const rowOf = function (item) {
|
|
193
|
+
return held(item.scope, item.owner).get(item.key);
|
|
194
|
+
};
|
|
195
|
+
// the items whose named revision isn't their value's now, or 0 for a key
|
|
196
|
+
// with none, as the database's value_conflicts
|
|
197
|
+
const conflictsOf = function (items) {
|
|
198
|
+
return items.filter((item) => item.revision !== undefined &&
|
|
199
|
+
item.revision !== (rowOf(item)?.revision ?? 0));
|
|
200
|
+
};
|
|
201
|
+
const readItems = function (given) {
|
|
202
|
+
const items = itemsOf(given, ["since"], false);
|
|
203
|
+
if (!items) {
|
|
204
|
+
return { data: null, status: 400 };
|
|
205
|
+
}
|
|
206
|
+
const refused = refusalOf(items);
|
|
207
|
+
if (refused) {
|
|
208
|
+
return { data: null, status: refused };
|
|
209
|
+
}
|
|
210
|
+
const rows = items.map(rowOf);
|
|
211
|
+
// what's read together, the unchanged left out
|
|
212
|
+
if (rows.reduce((sum, row, at) => sum + (row && row.revision !== items[at]?.since ? row.size : 0), 0) > LIMITS.readSize) {
|
|
213
|
+
return { data: null, status: 413 };
|
|
214
|
+
}
|
|
215
|
+
return {
|
|
216
|
+
data: items.map((item, at) => {
|
|
217
|
+
const row = rows[at];
|
|
218
|
+
if (!row) {
|
|
219
|
+
return null;
|
|
220
|
+
}
|
|
221
|
+
const unchanged = row.revision === item.since;
|
|
222
|
+
return {
|
|
223
|
+
key: row.key,
|
|
224
|
+
owner: item.owner,
|
|
225
|
+
revision: row.revision,
|
|
226
|
+
scope: item.scope,
|
|
227
|
+
unchanged,
|
|
228
|
+
updated_at: row.updated_at,
|
|
229
|
+
...(unchanged ? {} : { value: row.value }),
|
|
230
|
+
};
|
|
231
|
+
}),
|
|
232
|
+
status: 200,
|
|
233
|
+
};
|
|
234
|
+
};
|
|
235
|
+
const writeItems = function (given) {
|
|
236
|
+
const items = itemsOf(given, ["revision"], true);
|
|
237
|
+
if (!items) {
|
|
238
|
+
return { data: null, status: 400 };
|
|
239
|
+
}
|
|
240
|
+
const refused = refusalOf(items);
|
|
241
|
+
if (refused) {
|
|
242
|
+
return { data: null, status: refused };
|
|
243
|
+
}
|
|
244
|
+
const sizes = items.map((item) => sizeOf(item.value));
|
|
245
|
+
if (sizes.some((size) => size > LIMITS.size) ||
|
|
246
|
+
sizes.reduce((sum, size) => sum + size, 0) > LIMITS.callSize) {
|
|
247
|
+
return { data: null, status: 413 };
|
|
248
|
+
}
|
|
249
|
+
// every named revision first, so a call writes all or none
|
|
250
|
+
const conflicts = conflictsOf(items);
|
|
251
|
+
if (conflicts.length > 0) {
|
|
252
|
+
return { conflicts, data: null, status: 409 };
|
|
253
|
+
}
|
|
254
|
+
const next = { ...kept.rows };
|
|
255
|
+
let revision = kept.revision;
|
|
256
|
+
const at = new Date(now()).toISOString();
|
|
257
|
+
const written = items.map((item, index) => {
|
|
258
|
+
const name = `${item.scope}:${item.owner}`;
|
|
259
|
+
const rows = { ...(next[name] ?? {}) };
|
|
260
|
+
const key = item.key;
|
|
261
|
+
revision += 1;
|
|
262
|
+
rows[key] = {
|
|
263
|
+
key,
|
|
264
|
+
revision,
|
|
265
|
+
size: sizes[index] ?? 0,
|
|
266
|
+
updated_at: at,
|
|
267
|
+
value: item.value,
|
|
268
|
+
};
|
|
269
|
+
next[name] = rows;
|
|
270
|
+
return {
|
|
271
|
+
key,
|
|
272
|
+
owner: item.owner,
|
|
273
|
+
revision,
|
|
274
|
+
scope: item.scope,
|
|
275
|
+
updated_at: at,
|
|
276
|
+
};
|
|
277
|
+
});
|
|
278
|
+
// each scope it wrote, held to its limits
|
|
279
|
+
for (const item of items) {
|
|
280
|
+
const all = Object.values(next[`${item.scope}:${item.owner}`] ?? {});
|
|
281
|
+
const limits = item.scope === "player" ? "player" : "other";
|
|
282
|
+
if (all.length > LIMITS.keys[limits] ||
|
|
283
|
+
all.reduce((sum, row) => sum + row.size, 0) > LIMITS.bytes[limits]) {
|
|
284
|
+
return { data: null, status: 507 };
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
kept = { revision, rows: next };
|
|
288
|
+
save();
|
|
289
|
+
return { data: written, status: 200 };
|
|
290
|
+
};
|
|
291
|
+
const removeItems = function (given, skips = false) {
|
|
292
|
+
const items = itemsOf(given, ["revision"], false);
|
|
293
|
+
if (!items) {
|
|
294
|
+
return { data: null, status: 400 };
|
|
295
|
+
}
|
|
296
|
+
const refused = refusalOf(items);
|
|
297
|
+
if (refused) {
|
|
298
|
+
return { data: null, status: refused };
|
|
299
|
+
}
|
|
300
|
+
const conflicts = conflictsOf(items);
|
|
301
|
+
if (conflicts.length > 0 && !skips) {
|
|
302
|
+
return { conflicts, data: null, status: 409 };
|
|
303
|
+
}
|
|
304
|
+
let removed = 0;
|
|
305
|
+
for (const item of items) {
|
|
306
|
+
if (conflicts.includes(item)) {
|
|
307
|
+
continue;
|
|
308
|
+
}
|
|
309
|
+
const rows = kept.rows[`${item.scope}:${item.owner}`] ?? {};
|
|
310
|
+
if (Object.hasOwn(rows, item.key)) {
|
|
311
|
+
Reflect.deleteProperty(rows, item.key);
|
|
312
|
+
removed += 1;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
if (removed) {
|
|
316
|
+
save();
|
|
317
|
+
}
|
|
318
|
+
return { data: { removed }, status: 200 };
|
|
319
|
+
};
|
|
167
320
|
return {
|
|
321
|
+
readItems,
|
|
322
|
+
removeItems,
|
|
323
|
+
writeItems,
|
|
168
324
|
list(scope, owner, { after, limit, prefix }) {
|
|
169
325
|
const refused = refusal(scope, owner);
|
|
170
326
|
if (refused) {
|
|
@@ -207,111 +363,5 @@ export function values({ isPlayer, now = () => Date.now(), root, }) {
|
|
|
207
363
|
status: 200,
|
|
208
364
|
};
|
|
209
365
|
},
|
|
210
|
-
read(scope, owner, asked) {
|
|
211
|
-
const refused = refusal(scope, owner);
|
|
212
|
-
if (refused) {
|
|
213
|
-
return { data: null, status: refused };
|
|
214
|
-
}
|
|
215
|
-
const keys = keysOf(asked);
|
|
216
|
-
if (!keys) {
|
|
217
|
-
return { data: null, status: 400 };
|
|
218
|
-
}
|
|
219
|
-
const rows = held(scope, owner);
|
|
220
|
-
// more than a read may hold together is the caller's to ask for less
|
|
221
|
-
if (keys.reduce((sum, key) => sum + (rows.get(key)?.size ?? 0), 0) >
|
|
222
|
-
LIMITS.readSize) {
|
|
223
|
-
return { data: null, status: 413 };
|
|
224
|
-
}
|
|
225
|
-
return {
|
|
226
|
-
data: keys.map((key) => {
|
|
227
|
-
const row = rows.get(key);
|
|
228
|
-
return row
|
|
229
|
-
? {
|
|
230
|
-
key,
|
|
231
|
-
revision: row.revision,
|
|
232
|
-
updated_at: row.updated_at,
|
|
233
|
-
value: row.value,
|
|
234
|
-
}
|
|
235
|
-
: null;
|
|
236
|
-
}),
|
|
237
|
-
status: 200,
|
|
238
|
-
};
|
|
239
|
-
},
|
|
240
|
-
remove(scope, owner, asked) {
|
|
241
|
-
const refused = refusal(scope, owner);
|
|
242
|
-
if (refused) {
|
|
243
|
-
return { data: null, status: refused };
|
|
244
|
-
}
|
|
245
|
-
const keys = keysOf(asked);
|
|
246
|
-
if (!keys) {
|
|
247
|
-
return { data: null, status: 400 };
|
|
248
|
-
}
|
|
249
|
-
const rows = kept.rows[`${scope}:${owner}`] ?? {};
|
|
250
|
-
const there = keys.filter((key) => Object.hasOwn(rows, key));
|
|
251
|
-
for (const key of there) {
|
|
252
|
-
Reflect.deleteProperty(rows, key);
|
|
253
|
-
}
|
|
254
|
-
if (there.length) {
|
|
255
|
-
save();
|
|
256
|
-
}
|
|
257
|
-
return { data: { removed: there.length }, status: 200 };
|
|
258
|
-
},
|
|
259
|
-
write(scope, owner, given) {
|
|
260
|
-
const refused = refusal(scope, owner);
|
|
261
|
-
if (refused) {
|
|
262
|
-
return { data: null, status: refused };
|
|
263
|
-
}
|
|
264
|
-
if (!Array.isArray(given) ||
|
|
265
|
-
!given.every((item) => isTable(item) &&
|
|
266
|
-
Object.hasOwn(item, "value") &&
|
|
267
|
-
(item["revision"] === undefined ||
|
|
268
|
-
(typeof item["revision"] === "number" &&
|
|
269
|
-
Number.isSafeInteger(item["revision"]) &&
|
|
270
|
-
item["revision"] >= 0)))) {
|
|
271
|
-
return { data: null, status: 400 };
|
|
272
|
-
}
|
|
273
|
-
const items = given;
|
|
274
|
-
const keys = keysOf(items.map((item) => item.key));
|
|
275
|
-
if (!keys) {
|
|
276
|
-
return { data: null, status: 400 };
|
|
277
|
-
}
|
|
278
|
-
const sizes = items.map((item) => sizeOf(item.value));
|
|
279
|
-
if (sizes.some((size) => size > LIMITS.size) ||
|
|
280
|
-
sizes.reduce((sum, size) => sum + size, 0) > LIMITS.callSize) {
|
|
281
|
-
return { data: null, status: 413 };
|
|
282
|
-
}
|
|
283
|
-
const name = `${scope}:${owner}`;
|
|
284
|
-
const rows = { ...(kept.rows[name] ?? {}) };
|
|
285
|
-
// every named revision first, so a call writes all or none
|
|
286
|
-
for (const [at, item] of items.entries()) {
|
|
287
|
-
const revision = rows[keys[at] ?? ""]?.revision ?? 0;
|
|
288
|
-
if (item.revision !== undefined && item.revision !== revision) {
|
|
289
|
-
return { data: null, status: 409 };
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
let revision = kept.revision;
|
|
293
|
-
const at = new Date(now()).toISOString();
|
|
294
|
-
const written = items.map((item, index) => {
|
|
295
|
-
const key = keys[index] ?? "";
|
|
296
|
-
revision += 1;
|
|
297
|
-
rows[key] = {
|
|
298
|
-
key,
|
|
299
|
-
revision,
|
|
300
|
-
size: sizes[index] ?? 0,
|
|
301
|
-
updated_at: at,
|
|
302
|
-
value: item.value,
|
|
303
|
-
};
|
|
304
|
-
return { key, revision, updated_at: at };
|
|
305
|
-
});
|
|
306
|
-
const limits = scope === "player" ? "player" : "other";
|
|
307
|
-
const all = Object.values(rows);
|
|
308
|
-
if (all.length > LIMITS.keys[limits] ||
|
|
309
|
-
all.reduce((sum, row) => sum + row.size, 0) > LIMITS.bytes[limits]) {
|
|
310
|
-
return { data: null, status: 507 };
|
|
311
|
-
}
|
|
312
|
-
kept = { revision, rows: { ...kept.rows, [name]: rows } };
|
|
313
|
-
save();
|
|
314
|
-
return { data: written, status: 200 };
|
|
315
|
-
},
|
|
316
366
|
};
|
|
317
367
|
}
|
package/npm-shrinkwrap.json
CHANGED
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
},
|
|
44
44
|
"license": "Apache-2.0",
|
|
45
45
|
"name": "arcadible",
|
|
46
|
-
"version": "1.0.
|
|
46
|
+
"version": "1.0.33"
|
|
47
47
|
},
|
|
48
48
|
"node_modules/@asamuzakjp/css-color": {
|
|
49
49
|
"dependencies": {
|
|
@@ -3266,5 +3266,5 @@
|
|
|
3266
3266
|
}
|
|
3267
3267
|
},
|
|
3268
3268
|
"requires": true,
|
|
3269
|
-
"version": "1.0.
|
|
3269
|
+
"version": "1.0.33"
|
|
3270
3270
|
}
|
package/package.json
CHANGED