arcadible 1.0.24 → 1.0.26

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.
Files changed (54) hide show
  1. package/dist/action/build/tool.js +6 -10
  2. package/dist/action/delete/tool.js +2 -1
  3. package/dist/action/deploy/tool-git.js +4 -9
  4. package/dist/action/deploy/tool.js +3 -8
  5. package/dist/action/deploy/wait.js +1 -1
  6. package/dist/action/function/tool.js +6 -6
  7. package/dist/action/game.js +3 -2
  8. package/dist/action/key/tool.js +6 -5
  9. package/dist/action/open/tool.js +2 -1
  10. package/dist/action/pack/tool.js +13 -15
  11. package/dist/action/play/tool.js +2 -1
  12. package/dist/action/predeploy/ac.js +3 -3
  13. package/dist/action/predeploy/game.js +2 -8
  14. package/dist/action/predeploy/secrets.js +2 -2
  15. package/dist/action/predeploy/tool-git.js +3 -8
  16. package/dist/action/predeploy/tool.js +1 -3
  17. package/dist/action/secret/tool.js +4 -4
  18. package/dist/action/serve/tool.js +26 -5
  19. package/dist/action/value/tool.js +216 -0
  20. package/dist/api.js +190 -38
  21. package/dist/command/inspect.js +2 -1
  22. package/dist/command/open.js +2 -1
  23. package/dist/command/play.js +4 -3
  24. package/dist/command/serve.js +3 -2
  25. package/dist/command/value/get.js +21 -0
  26. package/dist/command/value/index.js +19 -0
  27. package/dist/command/value/key.js +8 -0
  28. package/dist/command/value/list.js +41 -0
  29. package/dist/command/value/remove.js +26 -0
  30. package/dist/command/value/set.js +62 -0
  31. package/dist/command/value/usage.js +69 -0
  32. package/dist/commands.js +8 -1
  33. package/dist/phrase/common.js +9 -0
  34. package/dist/phrase/deploy.js +9 -1
  35. package/dist/phrase/function.js +6 -2
  36. package/dist/phrase/index.js +1 -0
  37. package/dist/phrase/server.js +1 -1
  38. package/dist/phrase/value.js +55 -0
  39. package/dist/runner/bundle.js +10 -60
  40. package/dist/runner/entry.js +57 -0
  41. package/dist/runner/outbound.js +1 -1
  42. package/dist/schema/workflow.js +1 -1
  43. package/dist/serve/page.js +205 -6
  44. package/dist/serve/runner.js +9 -2
  45. package/dist/serve/server.js +7 -1
  46. package/dist/serve/ticket.js +2 -2
  47. package/dist/serve/values.js +317 -0
  48. package/dist/util/objs.js +17 -0
  49. package/dist/util/progress.js +13 -1
  50. package/dist/util/request.js +15 -3
  51. package/dist/util/schema.js +1 -1
  52. package/dist/util/shape.js +49 -0
  53. package/npm-shrinkwrap.json +2 -2
  54. package/package.json +1 -1
@@ -13,6 +13,7 @@ import { join } from "node:path";
13
13
  import { endWith, pathOf } from "./game.js";
14
14
  import { isTable } from "../util/objs.js";
15
15
  import { originOf } from "./ticket.js";
16
+ import { SCOPES, } from "./values.js";
16
17
  // [declare]
17
18
  // the play page's iframe, word for word (test/unit/serve/page.test.mjs),
18
19
  // for a game the page frames by itself, when it has no host to mount it.
@@ -26,7 +27,15 @@ const ICON = "/_arcadible/icon";
26
27
  // mint, and the key its tickets are checked against, where a server looks
27
28
  // for an issuer's.
28
29
  export const TICKETS = "/_arcadible/api/tickets";
30
+ // the game's values, for the page (the game's host), as the play page asks
31
+ // its own server: a verb after it.
32
+ export const VALUES = "/_arcadible/api/values";
29
33
  export const JWKS = "/.well-known/jwks.json";
34
+ // where the page logs a guest in, when its game asked for a log-in and the
35
+ // player took it: there's no account here, so the run gets a local player.
36
+ export const LOGIN = "/_arcadible/api/login";
37
+ // who a guest logs in as, without `--player` to name one.
38
+ const LOGIN_NAME = "Player";
30
39
  // the server API, as a game's own server reaches Arcadible's with its key
31
40
  // (/v1/server/...): here it needs none, since only this machine can ask.
32
41
  const SERVER = "/_arcadible/v1/server";
@@ -41,7 +50,14 @@ const SERVER_ROUTES = [
41
50
  [/^\/players\/.+$/u, "GET"],
42
51
  [/^\/deleted_players$/u, "GET"],
43
52
  [/^\/tickets\/check$/u, "POST"],
53
+ [/^\/values\/(?:read|write|remove|list)$/u, "POST"],
44
54
  ];
55
+ // the most a values request holds: a call's values together, with room for
56
+ // its keys and its JSON, as the API's (app/value.py).
57
+ const VALUES_BODY_MAX = 512 * 1024;
58
+ // the scopes a server writes: all but the developer's, which is the
59
+ // project's own files here, and arc's and Studio's on Arcadible.
60
+ const VALUE_WRITES = ["game", "player", "server"];
45
61
  // what the API says of a request it refuses (app/lib/python/offst/error.py),
46
62
  // in an error's `data`, with its status as `code`.
47
63
  const REFUSALS = {
@@ -53,10 +69,26 @@ const REFUSALS = {
53
69
  "Not Found",
54
70
  "The server has not found anything matching the Request-URI.",
55
71
  ],
72
+ 403: [
73
+ "Forbidden",
74
+ "The server understood the request, but is refusing to fulfill it.",
75
+ ],
56
76
  405: [
57
77
  "Method Not Allowed",
58
78
  "The method specified in the Request-Line is not allowed for the resource identified by the Request-URI.",
59
79
  ],
80
+ 409: [
81
+ "Conflict",
82
+ "The request could not be completed due to a conflict with the current state of the resource.",
83
+ ],
84
+ 413: [
85
+ "Content Too Large",
86
+ "The server is refusing to process a request because the request content is larger than the server is willing or able to process.",
87
+ ],
88
+ 507: [
89
+ "Insufficient Storage",
90
+ "The server is unable to store the representation needed to complete the request.",
91
+ ],
60
92
  };
61
93
  // the most a ticket's request holds: a server's address.
62
94
  const BODY_MAX = 4096;
@@ -145,6 +177,8 @@ export function html(game, pictures = { art: false, icon: false }) {
145
177
  channel: game.channel,
146
178
  game: game.id,
147
179
  icon: pictures.icon ? ICON_PNG : null,
180
+ // where a guest logs in, when the game asks for a log-in
181
+ logIn: LOGIN,
148
182
  // who the game runs for: a local player (`arc serve --player`), or null,
149
183
  // a guest.
150
184
  player: game.player ?? null,
@@ -156,6 +190,9 @@ export function html(game, pictures = { art: false, icon: false }) {
156
190
  // play page asks its own server
157
191
  tickets: TICKETS,
158
192
  title: game.title,
193
+ // the game's values, from arc serve itself, as the play page asks its
194
+ // own server
195
+ values: VALUES,
159
196
  version: game.version,
160
197
  };
161
198
  return `<!doctype html>
@@ -203,21 +240,21 @@ export function html(game, pictures = { art: false, icon: false }) {
203
240
  `;
204
241
  }
205
242
  // [function]
206
- // a request's body as JSON, or null: too large, or not JSON.
207
- function readJson(req) {
243
+ // a request's body as JSON, or null: larger than `max`, or not JSON.
244
+ function readJson(req, max = BODY_MAX) {
208
245
  return new Promise((resolve) => {
209
246
  const chunks = [];
210
247
  let size = 0;
211
248
  req.on("data", (chunk) => {
212
249
  size += chunk.length;
213
- if (size <= BODY_MAX) {
250
+ if (size <= max) {
214
251
  chunks.push(chunk);
215
252
  }
216
253
  });
217
254
  req.on("error", () => resolve(null));
218
255
  req.on("end", () => {
219
256
  try {
220
- resolve(size > BODY_MAX ? null : JSON.parse(Buffer.concat(chunks).toString()));
257
+ resolve(size > max ? null : JSON.parse(Buffer.concat(chunks).toString()));
221
258
  }
222
259
  catch {
223
260
  resolve(null);
@@ -258,6 +295,94 @@ async function sendTicket(req, res, { game, playerOf, tickets, }) {
258
295
  res.end(JSON.stringify({ ticket }));
259
296
  }
260
297
  // [function]
298
+ // a log-in, for the page: its game asked for one, and the player took it.
299
+ // On Arcadible the page leaves for the account site and comes back served
300
+ // for the player; here no one has an account, so the run's guest becomes a
301
+ // local player, the one `--player Player` names, until arc serve stops, and
302
+ // the page loads again for them. Only JSON is taken, as for a ticket, so no
303
+ // page elsewhere can log this one in.
304
+ async function sendLogIn(req, res, { game, playerOf, tickets, }) {
305
+ const type = ((req.headers["content-type"] ?? "").split(";")[0] ?? "").trim();
306
+ const data = type.toLowerCase() === "application/json" ? await readJson(req) : null;
307
+ if (data === null || typeof data !== "object") {
308
+ res.writeHead(400, HEADERS).end();
309
+ return;
310
+ }
311
+ if (!game.player) {
312
+ const player = playerOf(LOGIN_NAME);
313
+ if (!player) {
314
+ res.writeHead(404, HEADERS).end();
315
+ return;
316
+ }
317
+ game.player = player;
318
+ tickets?.remember(player);
319
+ }
320
+ res.writeHead(204, HEADERS).end();
321
+ }
322
+ // [function]
323
+ // a game's values, for the page (the game's host), as the play site's route
324
+ // answers (arcadible-play's /api/games/<game>/values/<verb>, and the API
325
+ // behind it): the game's call, read, written, removed, or listed, for who
326
+ // the page runs the game for. A game reads the developer's and the game's,
327
+ // and reads and writes its own player's: the server's scope, a write to any
328
+ // but a player's, a guest's call for a player's, or a player who isn't the
329
+ // one playing is 403. Only JSON is taken, as for a ticket, so a page
330
+ // elsewhere can't ask. A refusal is its status, with no body.
331
+ async function sendGameValues(req, res, verb, { game, values }) {
332
+ const refuse = function (status) {
333
+ res.writeHead(status, HEADERS).end();
334
+ };
335
+ const type = ((req.headers["content-type"] ?? "").split(";")[0] ?? "").trim();
336
+ const body = type.toLowerCase() === "application/json"
337
+ ? await readJson(req, VALUES_BODY_MAX)
338
+ : null;
339
+ if (!isTable(body)) {
340
+ refuse(400);
341
+ return;
342
+ }
343
+ const scope = SCOPES.find((each) => each === body["scope"]);
344
+ if (!scope) {
345
+ refuse(400);
346
+ return;
347
+ }
348
+ const write = verb === "write" || verb === "remove";
349
+ const player = game.player ?? null;
350
+ if (scope === "server" ||
351
+ (write && scope !== "player") ||
352
+ (scope === "player" && (!player || body["player"] !== player.id))) {
353
+ refuse(403);
354
+ return;
355
+ }
356
+ const owner = scope === "player" && player ? player.id : "game";
357
+ const answer = function (given) {
358
+ if (given.status !== 200) {
359
+ refuse(given.status);
360
+ return;
361
+ }
362
+ res.writeHead(200, {
363
+ ...HEADERS,
364
+ "content-type": "application/json; charset=utf-8",
365
+ });
366
+ res.end(JSON.stringify({ data: given.data }));
367
+ };
368
+ if (verb === "read") {
369
+ answer(values.read(scope, owner, body["keys"]));
370
+ }
371
+ else if (verb === "list") {
372
+ answer(values.list(scope, owner, {
373
+ after: body["after"] ?? "",
374
+ limit: body["limit"] ?? 50,
375
+ prefix: body["prefix"] ?? "",
376
+ }));
377
+ }
378
+ else if (verb === "write") {
379
+ answer(values.write(scope, owner, body["items"]));
380
+ }
381
+ else {
382
+ answer(values.remove(scope, owner, body["keys"]));
383
+ }
384
+ }
385
+ // [function]
261
386
  // an answer in the API's own envelope, with how many when it's a list the
262
387
  // API counts.
263
388
  function sendData(req, res, status, data, count) {
@@ -283,12 +408,62 @@ function sendRefusal(req, res, status) {
283
408
  });
284
409
  }
285
410
  // [function]
411
+ // a game's values, for its server, as the API's own route answers
412
+ // (app/src/web/arcadible-api's post_server_values): the scope the body names,
413
+ // with `player` for a player's, read, written, removed, or listed. A server
414
+ // reads every scope and writes all but the developer's.
415
+ async function sendValues(req, res, verb, values) {
416
+ const body = await readJson(req, VALUES_BODY_MAX);
417
+ if (!values || !isTable(body)) {
418
+ sendRefusal(req, res, values ? 400 : 404);
419
+ return;
420
+ }
421
+ const scope = SCOPES.find((each) => each === body["scope"]);
422
+ const player = body["player"];
423
+ if (!scope ||
424
+ (scope === "player" &&
425
+ (typeof player !== "string" || !PLAYER_ID.test(player)))) {
426
+ sendRefusal(req, res, 400);
427
+ return;
428
+ }
429
+ // a player's are the player's, and every other scope's the game's: one
430
+ // game is served, so any owner will do for those.
431
+ const owner = scope === "player" ? String(player) : "game";
432
+ const answer = function (given) {
433
+ if (given.status === 200) {
434
+ sendData(req, res, 200, given.data);
435
+ }
436
+ else {
437
+ sendRefusal(req, res, given.status);
438
+ }
439
+ };
440
+ if (verb === "read") {
441
+ answer(values.read(scope, owner, body["keys"]));
442
+ }
443
+ else if (verb === "list") {
444
+ answer(values.list(scope, owner, {
445
+ after: body["after"] ?? "",
446
+ limit: body["limit"] ?? 50,
447
+ prefix: body["prefix"] ?? "",
448
+ }));
449
+ }
450
+ else if (!VALUE_WRITES.includes(scope)) {
451
+ sendRefusal(req, res, 403);
452
+ }
453
+ else if (verb === "write") {
454
+ answer(values.write(scope, owner, body["items"]));
455
+ }
456
+ else {
457
+ answer(values.remove(scope, owner, body["keys"]));
458
+ }
459
+ }
460
+ // [function]
286
461
  // the local server API: what a game's own server asks Arcadible with its
287
462
  // key, answered here from what arc serve knows, and refused as the API
288
463
  // refuses (app/src/web/arcadible-api's app/api/server.py). The game is the
289
464
  // project's, its channel is local, its players are the ones this run's
290
465
  // tickets named, and no one has deleted an account.
291
- async function sendServer(req, res, route, { game, tickets }) {
466
+ async function sendServer(req, res, route, { game, tickets, values, }) {
292
467
  const known = SERVER_ROUTES.find(([path]) => path.test(route));
293
468
  if (!known) {
294
469
  sendRefusal(req, res, 404);
@@ -328,6 +503,10 @@ async function sendServer(req, res, route, { game, tickets }) {
328
503
  sendData(req, res, 200, []);
329
504
  return;
330
505
  }
506
+ if (route.startsWith("/values/")) {
507
+ await sendValues(req, res, route.slice("/values/".length), values);
508
+ return;
509
+ }
331
510
  const body = await readJson(req);
332
511
  // a list holds no ticket, and names no server.
333
512
  const asked = isTable(body) ? body : {};
@@ -347,7 +526,7 @@ async function sendServer(req, res, route, { game, tickets }) {
347
526
  // `tickets` mints the game's, and publishes their key, and `playerOf(name)`
348
527
  // is a local player by name (./ticket.js). `faviconFile` is the play page's
349
528
  // favicon.ico, the package's own unless a test names another.
350
- export function pageHandler({ art = () => Promise.resolve(null), faviconFile = FAVICON, game, hostFile = null, icon = () => Promise.resolve(null), onRemote = () => null, playerOf = () => null, reloads, remote, tickets = null, }) {
529
+ export function pageHandler({ art = () => Promise.resolve(null), faviconFile = FAVICON, game, hostFile = null, icon = () => Promise.resolve(null), onRemote = () => null, playerOf = () => null, reloads, remote, tickets = null, values = null, }) {
351
530
  // the play page's favicon.ico, read once, or null when it can't be read,
352
531
  // which is tried again on the next request.
353
532
  let favicon = null;
@@ -365,8 +544,28 @@ export function pageHandler({ art = () => Promise.resolve(null), faviconFile = F
365
544
  return sendServer(req, res, pathname.slice(SERVER.length), {
366
545
  game,
367
546
  tickets,
547
+ values,
368
548
  });
369
549
  }
550
+ if (values && pathname.startsWith(`${VALUES}/`)) {
551
+ const verb = pathname.slice(VALUES.length + 1);
552
+ if (!["list", "read", "remove", "write"].includes(verb)) {
553
+ res.writeHead(404, HEADERS).end();
554
+ return Promise.resolve();
555
+ }
556
+ if (req.method !== "POST") {
557
+ res.writeHead(405, { ...HEADERS, allow: "POST" }).end();
558
+ return Promise.resolve();
559
+ }
560
+ return sendGameValues(req, res, verb, { game, values });
561
+ }
562
+ if (pathname === LOGIN) {
563
+ if (req.method !== "POST") {
564
+ res.writeHead(405, { ...HEADERS, allow: "POST" }).end();
565
+ return Promise.resolve();
566
+ }
567
+ return sendLogIn(req, res, { game, playerOf, tickets });
568
+ }
370
569
  if (tickets && pathname === TICKETS) {
371
570
  if (req.method !== "POST") {
372
571
  res.writeHead(405, { ...HEADERS, allow: "POST" }).end();
@@ -13,6 +13,7 @@ import os from "node:os";
13
13
  import { join } from "node:path";
14
14
  // [import]
15
15
  import { bundle } from "../runner/bundle.js";
16
+ import { ENTRY, ENTRY_MODULE, FUNCTION_MODULE } from "../runner/entry.js";
16
17
  // [declare]
17
18
  // where a runner answers on the game's origin, as on Arcadible, where the
18
19
  // folder is Arcadible's own on every build's site.
@@ -171,7 +172,10 @@ const front :Workerd.Worker = (
171
172
  bindings = [(name = "RUNNER", service = "runner")],
172
173
  );
173
174
  const runner :Workerd.Worker = (
174
- modules = [(name = "runner", esModule = embed "runner.js")],
175
+ modules = [
176
+ (name = "${ENTRY_MODULE}", esModule = embed "${ENTRY_MODULE}"),
177
+ (name = "${FUNCTION_MODULE}", esModule = embed "${FUNCTION_MODULE}"),
178
+ ],
175
179
  compatibilityDate = "${COMPATIBILITY_DATE}",
176
180
  bindings = [(name = "ARCADIBLE_SERVE", text = ${JSON.stringify(serve)})${given}],
177
181
  globalOutbound = "outbound",
@@ -213,7 +217,10 @@ export async function runner({ dir, handler, hosts = [], main, origin, secrets =
213
217
  // fetched the first time, and after a failed fetch, tried again
214
218
  const workerd = await runtime(dir, () => handler.onInstall());
215
219
  const began = Date.now();
216
- await bundle({ main, outfile: join(work, "runner.js") });
220
+ // the developer's code, and Arcadible's entry beside it, where the
221
+ // script starts, as the pipeline uploads a build's
222
+ await bundle({ main, outfile: join(work, FUNCTION_MODULE) });
223
+ await writeFile(join(work, ENTRY_MODULE), ENTRY);
217
224
  const free = await freePort();
218
225
  port = free;
219
226
  await copyFile(OUTBOUND, join(work, "outbound.mjs"));
@@ -10,6 +10,7 @@ import { pageHandler } from "./page.js";
10
10
  import { reloader } from "./reload.js";
11
11
  import { PREFIX as RUNNER, runner as startRunner, } from "./runner.js";
12
12
  import { playerOf, tickets as ticketsFor } from "./ticket.js";
13
+ import { values as valuesFor } from "./values.js";
13
14
  // [declare]
14
15
  // the hostnames a browser may use for the page, on IPv4 and IPv6. `localhost`
15
16
  // is IPv6's ::1 on some systems, so both are listened on.
@@ -38,7 +39,7 @@ function listen(handler, port, address) {
38
39
  // or a guest. Resolves with
39
40
  // `{ page, game, reload(), close() }`: the page's origin, the game's URL,
40
41
  // and the controls.
41
- export async function serve({ art, files, hostFile = null, icon, info, keyFile, onRemote, player = null, port, remote, runner: runs = null, servers = [], src, }) {
42
+ export async function serve({ art, files, hostFile = null, icon, info, keyFile, onRemote, player = null, port, remote, root, runner: runs = null, servers = [], src, }) {
42
43
  const origins = {
43
44
  game: `http://127.0.0.1:${port}`,
44
45
  page: `http://localhost:${port}`,
@@ -72,6 +73,10 @@ export async function serve({ art, files, hostFile = null, icon, info, keyFile,
72
73
  tickets,
73
74
  })
74
75
  : null;
76
+ // the game's values, kept here, for a player a ticket named
77
+ const values = root && tickets
78
+ ? valuesFor({ isPlayer: (id) => tickets.player(id) !== null, root })
79
+ : null;
75
80
  const forPage = pageHandler({
76
81
  art,
77
82
  game: {
@@ -87,6 +92,7 @@ export async function serve({ art, files, hostFile = null, icon, info, keyFile,
87
92
  reloads,
88
93
  remote,
89
94
  tickets,
95
+ values,
90
96
  });
91
97
  // a Map, so a Host named for one of an object's own members (`toString`)
92
98
  // is an unknown host like any other.
@@ -10,7 +10,7 @@ import { mkdir, readFile, writeFile } from "node:fs/promises";
10
10
  import { dirname } from "node:path";
11
11
  import { promisify } from "node:util";
12
12
  // [import]
13
- import { isTable } from "../util/objs.js";
13
+ import { isTable, textOf } from "../util/objs.js";
14
14
  // [use]
15
15
  const generateKeyPair = promisify(crypto.generateKeyPair);
16
16
  // [declare]
@@ -104,7 +104,7 @@ export function playerOf(game, name) {
104
104
  if (name === null || typeof name === "undefined") {
105
105
  return null;
106
106
  }
107
- const trimmed = String(name).trim();
107
+ const trimmed = textOf(name).trim();
108
108
  if (trimmed === "" || trimmed.length > NAME_MAX) {
109
109
  return false;
110
110
  }