arcadible 1.0.32 → 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 +60 -11
- package/dist/serve/runner.js +48 -1
- package/dist/serve/ticket.js +17 -3
- package/dist/serve/values.js +17 -12
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
package/dist/serve/page.js
CHANGED
|
@@ -421,6 +421,26 @@ function itemsAnswer(verb, items, single, data) {
|
|
|
421
421
|
});
|
|
422
422
|
}
|
|
423
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]
|
|
424
444
|
// a game's values, for the page (the game's host), as the play site's route
|
|
425
445
|
// answers (arcadible-play's /api/games/<game>/values/<verb>, and the API
|
|
426
446
|
// behind it): the game's call, read, written, removed, or listed, for who
|
|
@@ -428,7 +448,8 @@ function itemsAnswer(verb, items, single, data) {
|
|
|
428
448
|
// and reads and writes its own player's: the server's scope, a write to any
|
|
429
449
|
// but a player's, a guest's call for a player's, or a player who isn't the
|
|
430
450
|
// one playing is 403. Only JSON is taken, as for a ticket, so a page
|
|
431
|
-
// 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 } }`.
|
|
432
453
|
async function sendGameValues(req, res, verb, { player, values }) {
|
|
433
454
|
const refuse = function (status) {
|
|
434
455
|
res.writeHead(status, HEADERS).end();
|
|
@@ -442,16 +463,24 @@ async function sendGameValues(req, res, verb, { player, values }) {
|
|
|
442
463
|
return;
|
|
443
464
|
}
|
|
444
465
|
const write = verb === "write" || verb === "remove";
|
|
466
|
+
// whether the call names one scope for all its keys; a list's does
|
|
467
|
+
let single = true;
|
|
445
468
|
const answer = function (given, shape) {
|
|
446
|
-
if (given.status !== 200) {
|
|
469
|
+
if (given.status !== 200 && given.status !== 409) {
|
|
447
470
|
refuse(given.status);
|
|
448
471
|
return;
|
|
449
472
|
}
|
|
450
|
-
res.writeHead(
|
|
473
|
+
res.writeHead(given.status, {
|
|
451
474
|
...HEADERS,
|
|
452
475
|
"content-type": "application/json; charset=utf-8",
|
|
453
476
|
});
|
|
454
|
-
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
|
+
}));
|
|
455
484
|
};
|
|
456
485
|
// what a game may: read the developer's, the game's, and its own
|
|
457
486
|
// player's, and write only its own player's, in every item
|
|
@@ -483,11 +512,17 @@ async function sendGameValues(req, res, verb, { player, values }) {
|
|
|
483
512
|
refuse(400);
|
|
484
513
|
return;
|
|
485
514
|
}
|
|
486
|
-
const { items
|
|
515
|
+
const { items } = call;
|
|
516
|
+
({ single } = call);
|
|
487
517
|
if (!items.every((item) => allowed(item.scope, item.scope === "player" ? item.owner : null))) {
|
|
488
518
|
refuse(403);
|
|
489
519
|
return;
|
|
490
520
|
}
|
|
521
|
+
const skips = verb === "remove" ? skipsOf(body) : false;
|
|
522
|
+
if (skips === null) {
|
|
523
|
+
refuse(400);
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
491
526
|
const shape = (data) => itemsAnswer(verb, items, single, data);
|
|
492
527
|
if (verb === "read") {
|
|
493
528
|
answer(values.readItems(items), shape);
|
|
@@ -496,7 +531,7 @@ async function sendGameValues(req, res, verb, { player, values }) {
|
|
|
496
531
|
answer(values.writeItems(items), shape);
|
|
497
532
|
}
|
|
498
533
|
else {
|
|
499
|
-
answer(values.removeItems(items));
|
|
534
|
+
answer(values.removeItems(items, skips));
|
|
500
535
|
}
|
|
501
536
|
}
|
|
502
537
|
// [function]
|
|
@@ -552,31 +587,40 @@ function windowOf(pathname, named) {
|
|
|
552
587
|
return player ? { path, player } : false;
|
|
553
588
|
}
|
|
554
589
|
// [function]
|
|
555
|
-
// a refusal in the API's own envelope: what it says of the status
|
|
556
|
-
|
|
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) {
|
|
557
593
|
const [title, message] = REFUSALS[status] ?? ["", ""];
|
|
558
594
|
sendData(req, res, status, {
|
|
559
595
|
code: status,
|
|
560
596
|
message,
|
|
561
597
|
title,
|
|
562
598
|
type: "HttpError",
|
|
599
|
+
...(conflicts ? { conflicts } : {}),
|
|
563
600
|
});
|
|
564
601
|
}
|
|
565
602
|
// [function]
|
|
566
603
|
// a game's values, for its server, as the API's own route answers
|
|
567
604
|
// (app/src/web/arcadible-api's post_server_values): the scope the body names,
|
|
568
605
|
// with `player` for a player's, read, written, removed, or listed. A server
|
|
569
|
-
// 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.
|
|
570
609
|
async function sendValues(req, res, verb, values) {
|
|
571
610
|
const body = await readJson(req, VALUES_BODY_MAX);
|
|
572
611
|
if (!values || !isTable(body)) {
|
|
573
612
|
sendRefusal(req, res, values ? 400 : 404);
|
|
574
613
|
return;
|
|
575
614
|
}
|
|
615
|
+
// whether the call names one scope for all its keys; a list's does
|
|
616
|
+
let single = true;
|
|
576
617
|
const answer = function (given, shape) {
|
|
577
618
|
if (given.status === 200) {
|
|
578
619
|
sendData(req, res, 200, shape ? shape(given.data) : given.data);
|
|
579
620
|
}
|
|
621
|
+
else if (given.status === 409) {
|
|
622
|
+
sendRefusal(req, res, 409, conflictsAnswer(given.conflicts, single));
|
|
623
|
+
}
|
|
580
624
|
else {
|
|
581
625
|
sendRefusal(req, res, given.status);
|
|
582
626
|
}
|
|
@@ -605,7 +649,9 @@ async function sendValues(req, res, verb, values) {
|
|
|
605
649
|
sendRefusal(req, res, 400);
|
|
606
650
|
return;
|
|
607
651
|
}
|
|
608
|
-
const { items
|
|
652
|
+
const { items } = call;
|
|
653
|
+
({ single } = call);
|
|
654
|
+
const skips = verb === "remove" ? skipsOf(body) : false;
|
|
609
655
|
const shape = (data) => itemsAnswer(verb, items, single, data);
|
|
610
656
|
if (verb === "read") {
|
|
611
657
|
answer(values.readItems(items), shape);
|
|
@@ -613,11 +659,14 @@ async function sendValues(req, res, verb, values) {
|
|
|
613
659
|
else if (!items.every((item) => VALUE_WRITES.includes(item.scope))) {
|
|
614
660
|
sendRefusal(req, res, 403);
|
|
615
661
|
}
|
|
662
|
+
else if (skips === null) {
|
|
663
|
+
sendRefusal(req, res, 400);
|
|
664
|
+
}
|
|
616
665
|
else if (verb === "write") {
|
|
617
666
|
answer(values.writeItems(items), shape);
|
|
618
667
|
}
|
|
619
668
|
else {
|
|
620
|
-
answer(values.removeItems(items));
|
|
669
|
+
answer(values.removeItems(items, skips));
|
|
621
670
|
}
|
|
622
671
|
}
|
|
623
672
|
// [function]
|
package/dist/serve/runner.js
CHANGED
|
@@ -36,6 +36,10 @@ 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";
|
|
39
43
|
// what marks an answer as Arcadible's own, with its code, as the dispatcher
|
|
40
44
|
// marks its refusals: dropped from what a function's code answers, so a
|
|
41
45
|
// game can't be told a refusal that isn't one.
|
|
@@ -209,6 +213,30 @@ const outbound :Workerd.Worker = (
|
|
|
209
213
|
`;
|
|
210
214
|
}
|
|
211
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]
|
|
212
240
|
// a game's runner, running. `main` is its code's path, `dir` where the
|
|
213
241
|
// runtime is kept, `origin` the game's own origin (a ticket for the runner
|
|
214
242
|
// is for it), `serve` arc serve's page (where Arcadible is, here), `hosts`
|
|
@@ -279,6 +307,18 @@ export async function runner({ dir, handler, hosts = [], main, origin, secrets =
|
|
|
279
307
|
res.end(JSON.stringify({ code: checked.data.code }));
|
|
280
308
|
return;
|
|
281
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
|
+
}
|
|
282
322
|
// the game's answer when its function failed, as the dispatcher's: why
|
|
283
323
|
// is the terminal's, said as it happened
|
|
284
324
|
const fail = function () {
|
|
@@ -301,6 +341,10 @@ export async function runner({ dir, handler, hosts = [], main, origin, secrets =
|
|
|
301
341
|
...Object.fromEntries(Object.entries(req.headers).filter(([name]) => !dropped.includes(name))),
|
|
302
342
|
[PLAYER]: typeof player === "string" ? player : "",
|
|
303
343
|
};
|
|
344
|
+
if (body !== true) {
|
|
345
|
+
delete headers["transfer-encoding"];
|
|
346
|
+
headers["content-length"] = String(body.length);
|
|
347
|
+
}
|
|
304
348
|
// `request` is called through the module, as the tests fake Node's own.
|
|
305
349
|
const forward = http.request({
|
|
306
350
|
headers,
|
|
@@ -313,7 +357,10 @@ export async function runner({ dir, handler, hosts = [], main, origin, secrets =
|
|
|
313
357
|
answer.pipe(res);
|
|
314
358
|
});
|
|
315
359
|
forward.on("error", fail);
|
|
316
|
-
|
|
360
|
+
if (body === true)
|
|
361
|
+
req.pipe(forward);
|
|
362
|
+
else
|
|
363
|
+
forward.end(body);
|
|
317
364
|
};
|
|
318
365
|
await start();
|
|
319
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.
|
|
@@ -192,6 +192,12 @@ export function values({ isPlayer, now = () => Date.now(), root, }) {
|
|
|
192
192
|
const rowOf = function (item) {
|
|
193
193
|
return held(item.scope, item.owner).get(item.key);
|
|
194
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
|
+
};
|
|
195
201
|
const readItems = function (given) {
|
|
196
202
|
const items = itemsOf(given, ["since"], false);
|
|
197
203
|
if (!items) {
|
|
@@ -241,11 +247,9 @@ export function values({ isPlayer, now = () => Date.now(), root, }) {
|
|
|
241
247
|
return { data: null, status: 413 };
|
|
242
248
|
}
|
|
243
249
|
// every named revision first, so a call writes all or none
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
return { data: null, status: 409 };
|
|
248
|
-
}
|
|
250
|
+
const conflicts = conflictsOf(items);
|
|
251
|
+
if (conflicts.length > 0) {
|
|
252
|
+
return { conflicts, data: null, status: 409 };
|
|
249
253
|
}
|
|
250
254
|
const next = { ...kept.rows };
|
|
251
255
|
let revision = kept.revision;
|
|
@@ -284,7 +288,7 @@ export function values({ isPlayer, now = () => Date.now(), root, }) {
|
|
|
284
288
|
save();
|
|
285
289
|
return { data: written, status: 200 };
|
|
286
290
|
};
|
|
287
|
-
const removeItems = function (given) {
|
|
291
|
+
const removeItems = function (given, skips = false) {
|
|
288
292
|
const items = itemsOf(given, ["revision"], false);
|
|
289
293
|
if (!items) {
|
|
290
294
|
return { data: null, status: 400 };
|
|
@@ -293,14 +297,15 @@ export function values({ isPlayer, now = () => Date.now(), root, }) {
|
|
|
293
297
|
if (refused) {
|
|
294
298
|
return { data: null, status: refused };
|
|
295
299
|
}
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
return { data: null, status: 409 };
|
|
300
|
-
}
|
|
300
|
+
const conflicts = conflictsOf(items);
|
|
301
|
+
if (conflicts.length > 0 && !skips) {
|
|
302
|
+
return { conflicts, data: null, status: 409 };
|
|
301
303
|
}
|
|
302
304
|
let removed = 0;
|
|
303
305
|
for (const item of items) {
|
|
306
|
+
if (conflicts.includes(item)) {
|
|
307
|
+
continue;
|
|
308
|
+
}
|
|
304
309
|
const rows = kept.rows[`${item.scope}:${item.owner}`] ?? {};
|
|
305
310
|
if (Object.hasOwn(rows, item.key)) {
|
|
306
311
|
Reflect.deleteProperty(rows, item.key);
|
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