arcadible 1.0.32 → 1.0.34

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.
@@ -204,6 +204,7 @@ export async function serveTool(conf) {
204
204
  server_origins: servers,
205
205
  }),
206
206
  ...(conf.host ? { hostFile: resolvePath(root, conf.host) } : {}),
207
+ wiresDrop: conf.wiresDrop ?? null,
207
208
  };
208
209
  // the runner's code, watched: a change runs the new code
209
210
  const watchRunner = function (served) {
@@ -0,0 +1,36 @@
1
+ // arc wire: a game's wires (plan/SDKWIRES.md), live messages between its
2
+ // players. What its owner sees of them is their usage: wires keep nothing,
3
+ // and nothing about one is set from here.
4
+ import { gameWireUsages } from "../../api.js";
5
+ import { GAME_UNAVAILABLE } from "../../config/code.js";
6
+ import { ArcError } from "../../util/error.js";
7
+ import { channelOf, CHANNELS, gameOf, headersOf, } from "../game.js";
8
+ import { gameUnavailable } from "../../phrase/index.js";
9
+ // [function]
10
+ // what the game's wires carried on each channel, or on one: this month's,
11
+ // and today's of it.
12
+ export async function usage(current, { channel, ...conf }) {
13
+ const where = channelOf(channel);
14
+ const id = await gameOf(conf);
15
+ const rows = (await gameWireUsages(id, null, headersOf(current))) ?? [];
16
+ // its owner always reads today's row for each channel: no rows at all is
17
+ // a game that isn't the caller's
18
+ if (rows.length === 0) {
19
+ throw new ArcError(GAME_UNAVAILABLE, gameUnavailable(id));
20
+ }
21
+ const today = new Date().toISOString().slice(0, 10);
22
+ const sum = (of) => ({
23
+ connections: of.reduce((total, each) => total + Number(each.connections ?? 0), 0),
24
+ messages: of.reduce((total, each) => total + Number(each.messages ?? 0), 0),
25
+ });
26
+ return {
27
+ channels: CHANNELS.filter((each) => !where || each === where).map((each) => {
28
+ const own = rows.filter((row) => row.channel === each);
29
+ return {
30
+ channel: each,
31
+ month: sum(own),
32
+ today: sum(own.filter((row) => row.day === today)),
33
+ };
34
+ }),
35
+ };
36
+ }
package/dist/api.js CHANGED
@@ -105,6 +105,12 @@ const isValueCalls = shaped({
105
105
  writes: num,
106
106
  });
107
107
  const isPause = shaped({ channel: str, reason: str });
108
+ const isWireUsage = shaped({
109
+ channel: str,
110
+ connections: opt(nul(either(num, str))),
111
+ day: str,
112
+ messages: opt(nul(either(num, str))),
113
+ });
108
114
  const isUsage = shaped({
109
115
  allowance: either(num, str),
110
116
  channel: str,
@@ -300,6 +306,12 @@ export function gameFunctionUsages(id, params, headers) {
300
306
  return answer(listed(isUsage), "a function's usage", get(endpoint(pathOf `/games/${id}/function_usages`, params), headers));
301
307
  }
302
308
  // [function]
309
+ // the usage of the caller's own game's wires this month: a row a channel
310
+ // and a day.
311
+ export function gameWireUsages(id, params, headers) {
312
+ return answer(listed(isWireUsage), "a game's wires' usage", get(endpoint(pathOf `/games/${id}/wire_usages`, params), headers));
313
+ }
314
+ // [function]
303
315
  // pauses the function of the caller's own game on a channel.
304
316
  export function putGameFunctionPause(id, channel, params, headers) {
305
317
  return answer(isPause, "a pause", put(endpoint(pathOf `/games/${id}/function_pauses/${channel}`, params), null, headers));
@@ -41,6 +41,19 @@ function checkUrl(url) {
41
41
  return Promise.reject(new ArcError(ARGUMENT_INVALID, phrase.urlInvalid()));
42
42
  }
43
43
  // [function]
44
+ // the seconds --wires-drop names, or null without it; a failure for what
45
+ // isn't a time to wait.
46
+ function checkDrop(seconds) {
47
+ if (seconds === null || typeof seconds === "undefined") {
48
+ return Promise.resolve(null);
49
+ }
50
+ const drop = Number(seconds);
51
+ if (Number.isFinite(drop) && drop > 0 && drop <= 3600) {
52
+ return Promise.resolve(drop);
53
+ }
54
+ return Promise.reject(new ArcError(ARGUMENT_INVALID, phrase.wiresDropInvalid()));
55
+ }
56
+ // [function]
44
57
  // warns once for each of Arcadible's files that's a kept copy, or can't be
45
58
  // had, rather than on every request.
46
59
  function remoteWarnings({ quiet }) {
@@ -60,25 +73,25 @@ function remoteWarnings({ quiet }) {
60
73
  export default defineCommand({
61
74
  name: "serve",
62
75
  options: {
63
- port: {
76
+ "port": {
64
77
  alias: "p",
65
78
  default: PORT,
66
79
  describe: "Serve on this port",
67
80
  rc: true,
68
81
  type: "number",
69
82
  },
70
- build: {
83
+ "build": {
71
84
  default: true,
72
85
  describe: "Run the build command first",
73
86
  rc: true,
74
87
  type: "boolean",
75
88
  },
76
- url: {
89
+ "url": {
77
90
  describe: "Frame another server's page instead of deploy_dir",
78
91
  rc: true,
79
92
  type: "string",
80
93
  },
81
- open: {
94
+ "open": {
82
95
  alias: "o",
83
96
  default: false,
84
97
  describe: "Open the play page in a web browser",
@@ -88,12 +101,16 @@ export default defineCommand({
88
101
  // run the game for a logged-in player of this name, the same one each
89
102
  // run; without it, a guest. Left out of the help until tickets ship,
90
103
  // with the docs.
91
- player: { hidden: true, rc: true, type: "string" },
104
+ "player": { hidden: true, rc: true, type: "string" },
92
105
  // serve a play page's host built here, for working on the host itself;
93
106
  // without it, Arcadible's own. Left out of the help.
94
- host: { hidden: true, rc: true, type: "string" },
95
- quiet: QUIET,
96
- cwd: CWD,
107
+ "host": { hidden: true, rc: true, type: "string" },
108
+ // drop each wire's connection this many seconds after it comes on, to
109
+ // try a game's rejoin. Left out of the help until wires ship, with the
110
+ // docs.
111
+ "wires-drop": { hidden: true, rc: true, type: "number" },
112
+ "quiet": QUIET,
113
+ "cwd": CWD,
97
114
  },
98
115
  // resolves with nothing, so the process goes on serving until it's
99
116
  // stopped.
@@ -106,6 +123,7 @@ export default defineCommand({
106
123
  aborting.sw = sw;
107
124
  await checkPort(port);
108
125
  const url = await checkUrl(argv.url);
126
+ const wiresDrop = await checkDrop(argv["wires-drop"]);
109
127
  const served = await serveTool({
110
128
  build: argv.build,
111
129
  handler: {
@@ -135,6 +153,7 @@ export default defineCommand({
135
153
  port,
136
154
  root,
137
155
  url,
156
+ wiresDrop,
138
157
  });
139
158
  sw.stop(true);
140
159
  const stop = () => {
@@ -0,0 +1,14 @@
1
+ // arc wire: a game's wires (plan/SDKWIRES.md), live messages between its
2
+ // players, relayed and never kept. A game makes and joins them itself, so
3
+ // what's here is what they came to: their usage, by channel. Left out of
4
+ // `arc --help` until wires ship, with the docs.
5
+ // [import]
6
+ import { defineCommand } from "../../cli/command.js";
7
+ import usage from "./usage.js";
8
+ // [export]
9
+ export default defineCommand({
10
+ commands: [usage],
11
+ hidden: true,
12
+ name: "wire",
13
+ summary: "Show what the game's wires carried",
14
+ });
@@ -0,0 +1,43 @@
1
+ // [import]
2
+ import { defineCommand } from "../../cli/command.js";
3
+ import { p } from "../../util/log.js";
4
+ import { CHANNEL_SHOWS, CWD, JSON_OUTPUT, QUIET } from "../../cli/options.js";
5
+ import { run } from "../function/run.js";
6
+ import { table } from "../../util/table.js";
7
+ import { usage } from "../../action/wire/tool.js";
8
+ // [function]
9
+ // a count as it's read: 1,000,000.
10
+ function count(value) {
11
+ return value.toLocaleString("en-US");
12
+ }
13
+ // [function]
14
+ // each channel's wires as a table, a row each: today's, then the month's.
15
+ function rows(channels) {
16
+ return table([
17
+ [
18
+ "CHANNEL",
19
+ "MESSAGES TODAY",
20
+ "CONNECTIONS TODAY",
21
+ "MESSAGES THIS MONTH",
22
+ "CONNECTIONS THIS MONTH",
23
+ ],
24
+ ...channels.map((each) => [
25
+ each.channel,
26
+ count(each.today.messages),
27
+ count(each.today.connections),
28
+ count(each.month.messages),
29
+ count(each.month.connections),
30
+ ]),
31
+ ]);
32
+ }
33
+ // [export]
34
+ export default defineCommand({
35
+ name: "usage",
36
+ options: { ...CHANNEL_SHOWS, json: JSON_OUTPUT, quiet: QUIET, cwd: CWD },
37
+ run(argv, aborting) {
38
+ return run(argv, aborting, usage, ({ channels }) => {
39
+ p(rows(channels));
40
+ });
41
+ },
42
+ summary: "Show what the game's wires carried today and this month",
43
+ });
package/dist/commands.js CHANGED
@@ -77,7 +77,7 @@ export const COMMANDS = [
77
77
  name: "logout",
78
78
  summary: `Log out of ${title}`,
79
79
  },
80
- // left out of `arc --help` until functions, keys, and values ship, with the
80
+ // left out of `arc --help` until functions, keys, values, and wires ship, with the
81
81
  // docs.
82
82
  {
83
83
  hidden: true,
@@ -97,4 +97,10 @@ export const COMMANDS = [
97
97
  name: "value",
98
98
  summary: "Set, get, list, and remove the values you publish for the game",
99
99
  },
100
+ {
101
+ hidden: true,
102
+ load: () => import("./command/wire/index.js"),
103
+ name: "wire",
104
+ summary: "Show what the game's wires carried",
105
+ },
100
106
  ];
@@ -75,6 +75,10 @@ export function urlInvalid() {
75
75
  return "`--url` must start with http:// or https://.";
76
76
  }
77
77
  // [function]
78
+ export function wiresDropInvalid() {
79
+ return "`--wires-drop` must be a number of seconds, up to 3600.";
80
+ }
81
+ // [function]
78
82
  // --url's server sent no headers that let the play page frame it. `link`
79
83
  // says how to add them.
80
84
  export function urlNotFrameable(url, link) {
@@ -7,6 +7,7 @@
7
7
  // reloads the game, and only the game, when told to. Its tab shows the
8
8
  // game's store icon, as the play page's does, or Arcadible's.
9
9
  // [import]
10
+ import crypto from "node:crypto";
10
11
  import { readFile } from "node:fs/promises";
11
12
  // [import]
12
13
  import { endWith, pathOf } from "./game.js";
@@ -31,6 +32,15 @@ export const TICKETS = "/_arcadible/api/tickets";
31
32
  // its own server: a verb after it.
32
33
  export const VALUES = "/_arcadible/api/values";
33
34
  export const JWKS = "/.well-known/jwks.json";
35
+ // the game's links (plan/SDKLINKS.md), for the page (the game's host), as
36
+ // the play page asks its own server: a link is made here, kept for the run,
37
+ // and opened as the page's own address with `?l=<id>`.
38
+ export const LINKS = "/_arcadible/api/links";
39
+ // a link's id, as Arcadible makes one: twelve characters of 62; and the
40
+ // most its data is, as JSON, in bytes.
41
+ const LINK_ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
42
+ const LINK_ID = /^[0-9A-Za-z]{12}$/u;
43
+ const LINK_DATA_MAX = 1024;
34
44
  // where the page logs a guest in, when its game asked for a log-in and the
35
45
  // player took it: there's no account here, so the run gets a local player.
36
46
  export const LOGIN = "/_arcadible/api/login";
@@ -58,6 +68,7 @@ const SERVER_ROUTES = [
58
68
  [/^\/deleted_players$/u, "GET"],
59
69
  [/^\/tickets\/check$/u, "POST"],
60
70
  [/^\/values\/(?:read|write|remove|list)$/u, "POST"],
71
+ [/^\/wires\/(?:open|send)$/u, "POST"],
61
72
  ];
62
73
  // the most a values request holds: a call's values together, with room for
63
74
  // its keys and its JSON, as the API's (app/value.py).
@@ -169,7 +180,7 @@ export function policy(src) {
169
180
  // with the play page's styles and loading screen; without a host that can,
170
181
  // the page frames the game by itself, and the game stands alone, as off
171
182
  // Arcadible.
172
- export function html(game, pictures = { art: false, icon: false }, opened) {
183
+ export function html(game, pictures = { art: false, icon: false }, opened, link) {
173
184
  // a window named by its address asks under its own path, and offers no
174
185
  // log-in: it plays as the player its address names.
175
186
  const own = opened ? `${AS}/${opened.player ? opened.player.id : GUEST}` : "";
@@ -182,6 +193,12 @@ export function html(game, pictures = { art: false, icon: false }, opened) {
182
193
  channel: game.channel,
183
194
  game: game.id,
184
195
  icon: pictures.icon ? ICON_PNG : null,
196
+ // the link the window was opened with (`?l=`), and whether one was
197
+ // named that's gone; and where the game's links are made, by arc serve
198
+ // itself, as the play page asks its own server
199
+ link: link ?? null,
200
+ linkGone: link === null,
201
+ links: `${own}${LINKS}`,
185
202
  // where a guest logs in, when the game asks for a log-in
186
203
  ...(opened ? {} : { logIn: LOGIN }),
187
204
  // who the game runs for: a local player (`arc serve --player`), or null,
@@ -268,6 +285,64 @@ function readJson(req, max = BODY_MAX) {
268
285
  });
269
286
  }
270
287
  // [function]
288
+ // a link from the game, for the page (the game's host), as the play page
289
+ // asks its own server: the game's data kept under a new id, for this run of
290
+ // arc serve, by the window's player, or its guest by the id the page sends
291
+ // (the browser's own, taken at its word). Only JSON is taken, as for a
292
+ // ticket. 400 a body that isn't an object; 403 no one to make it; 413 data
293
+ // past 1 KB as JSON. None of Arcadible's rate is kept, nor its 30 days: a
294
+ // link lasts until arc serve stops.
295
+ async function sendLink(req, res, { links, playing }) {
296
+ const type = ((req.headers["content-type"] ?? "").split(";")[0] ?? "").trim();
297
+ const asked = type.toLowerCase() === "application/json" ? await readJson(req) : null;
298
+ if (!isTable(asked)) {
299
+ res.writeHead(400, HEADERS).end();
300
+ return;
301
+ }
302
+ const guest = asked["guest"];
303
+ const by = playing
304
+ ? { guest: false, id: playing.id, name: playing.name }
305
+ : typeof guest === "string" && PLAYER_ID.test(guest)
306
+ ? { guest: true, id: guest, name: null }
307
+ : null;
308
+ if (!by) {
309
+ res.writeHead(403, HEADERS).end();
310
+ return;
311
+ }
312
+ const data = asked["data"] ?? null;
313
+ if (Buffer.byteLength(JSON.stringify(data)) > LINK_DATA_MAX) {
314
+ res.writeHead(413, HEADERS).end();
315
+ return;
316
+ }
317
+ let id = "";
318
+ for (let i = 0; i < 12; i++) {
319
+ id += LINK_ALPHABET.charAt(crypto.randomInt(LINK_ALPHABET.length));
320
+ }
321
+ links.set(id, { by, data, id });
322
+ res.writeHead(200, {
323
+ ...HEADERS,
324
+ "content-type": "application/json; charset=utf-8",
325
+ });
326
+ res.end(JSON.stringify({ id }));
327
+ }
328
+ // [function]
329
+ // the link a window's address names, `?l=<id>`: the link, when it's one
330
+ // made this run; null for one that's gone, which every other is (one made
331
+ // before arc serve last started, or never: the expired case, to test);
332
+ // undefined with none.
333
+ function linkAsked(req, links) {
334
+ let id;
335
+ try {
336
+ id = new URL(req.url ?? "", "http://page").searchParams.get("l");
337
+ }
338
+ catch {
339
+ return undefined;
340
+ }
341
+ if (id === null)
342
+ return undefined;
343
+ return (LINK_ID.test(id) && links.get(id)) || null;
344
+ }
345
+ // [function]
271
346
  // a ticket for one of the game's servers, for the page (the game's host), or
272
347
  // for a shell on this machine. Only JSON is taken: a page elsewhere can't
273
348
  // send that across origins without asking first, and isn't answered. The
@@ -288,7 +363,7 @@ async function sendTicket(req, res, { playerOf, playing, tickets, }) {
288
363
  res.writeHead(400, HEADERS).end();
289
364
  return;
290
365
  }
291
- const { status, ticket } = await tickets.mint(asked["server"], player);
366
+ const { status, ticket } = await tickets.mint(asked["server"], player, asked["guest"]);
292
367
  if (status !== 200) {
293
368
  res.writeHead(status, HEADERS).end();
294
369
  return;
@@ -421,6 +496,26 @@ function itemsAnswer(verb, items, single, data) {
421
496
  });
422
497
  }
423
498
  // [function]
499
+ // the items a conflict names, as the API says them: in the items form,
500
+ // each with its scope (and `player` for a player's) and its key; a single
501
+ // scope's, its key.
502
+ function conflictsAnswer(conflicts, single) {
503
+ return conflicts.map((item) => single
504
+ ? { key: item.key }
505
+ : {
506
+ scope: item.scope,
507
+ ...(item.scope === "player" ? { player: item.owner } : {}),
508
+ key: item.key,
509
+ });
510
+ }
511
+ // [function]
512
+ // whether a remove skips its conflicts: a body's `on_conflict`, "skip", or
513
+ // "reject", as with none. Null for anything else.
514
+ function skipsOf(body) {
515
+ const given = body["on_conflict"] ?? "reject";
516
+ return given === "skip" ? true : given === "reject" ? false : null;
517
+ }
518
+ // [function]
424
519
  // a game's values, for the page (the game's host), as the play site's route
425
520
  // answers (arcadible-play's /api/games/<game>/values/<verb>, and the API
426
521
  // behind it): the game's call, read, written, removed, or listed, for who
@@ -428,7 +523,8 @@ function itemsAnswer(verb, items, single, data) {
428
523
  // and reads and writes its own player's: the server's scope, a write to any
429
524
  // but a player's, a guest's call for a player's, or a player who isn't the
430
525
  // 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.
526
+ // elsewhere can't ask. A refusal is its status, with no body, but a
527
+ // conflict's (409), which names its items: `{ data: { conflicts } }`.
432
528
  async function sendGameValues(req, res, verb, { player, values }) {
433
529
  const refuse = function (status) {
434
530
  res.writeHead(status, HEADERS).end();
@@ -442,16 +538,24 @@ async function sendGameValues(req, res, verb, { player, values }) {
442
538
  return;
443
539
  }
444
540
  const write = verb === "write" || verb === "remove";
541
+ // whether the call names one scope for all its keys; a list's does
542
+ let single = true;
445
543
  const answer = function (given, shape) {
446
- if (given.status !== 200) {
544
+ if (given.status !== 200 && given.status !== 409) {
447
545
  refuse(given.status);
448
546
  return;
449
547
  }
450
- res.writeHead(200, {
548
+ res.writeHead(given.status, {
451
549
  ...HEADERS,
452
550
  "content-type": "application/json; charset=utf-8",
453
551
  });
454
- res.end(JSON.stringify({ data: shape ? shape(given.data) : given.data }));
552
+ res.end(JSON.stringify({
553
+ data: given.status === 409
554
+ ? { conflicts: conflictsAnswer(given.conflicts, single) }
555
+ : shape
556
+ ? shape(given.data)
557
+ : given.data,
558
+ }));
455
559
  };
456
560
  // what a game may: read the developer's, the game's, and its own
457
561
  // player's, and write only its own player's, in every item
@@ -483,11 +587,17 @@ async function sendGameValues(req, res, verb, { player, values }) {
483
587
  refuse(400);
484
588
  return;
485
589
  }
486
- const { items, single } = call;
590
+ const { items } = call;
591
+ ({ single } = call);
487
592
  if (!items.every((item) => allowed(item.scope, item.scope === "player" ? item.owner : null))) {
488
593
  refuse(403);
489
594
  return;
490
595
  }
596
+ const skips = verb === "remove" ? skipsOf(body) : false;
597
+ if (skips === null) {
598
+ refuse(400);
599
+ return;
600
+ }
491
601
  const shape = (data) => itemsAnswer(verb, items, single, data);
492
602
  if (verb === "read") {
493
603
  answer(values.readItems(items), shape);
@@ -496,7 +606,7 @@ async function sendGameValues(req, res, verb, { player, values }) {
496
606
  answer(values.writeItems(items), shape);
497
607
  }
498
608
  else {
499
- answer(values.removeItems(items));
609
+ answer(values.removeItems(items, skips));
500
610
  }
501
611
  }
502
612
  // [function]
@@ -552,31 +662,40 @@ function windowOf(pathname, named) {
552
662
  return player ? { path, player } : false;
553
663
  }
554
664
  // [function]
555
- // a refusal in the API's own envelope: what it says of the status.
556
- function sendRefusal(req, res, status) {
665
+ // a refusal in the API's own envelope: what it says of the status, and,
666
+ // for a conflict, the items it names.
667
+ function sendRefusal(req, res, status, conflicts) {
557
668
  const [title, message] = REFUSALS[status] ?? ["", ""];
558
669
  sendData(req, res, status, {
559
670
  code: status,
560
671
  message,
561
672
  title,
562
673
  type: "HttpError",
674
+ ...(conflicts ? { conflicts } : {}),
563
675
  });
564
676
  }
565
677
  // [function]
566
678
  // a game's values, for its server, as the API's own route answers
567
679
  // (app/src/web/arcadible-api's post_server_values): the scope the body names,
568
680
  // with `player` for a player's, read, written, removed, or listed. A server
569
- // reads every scope and writes all but the developer's.
681
+ // reads every scope and writes all but the developer's. A conflict names
682
+ // its items (`conflicts`), and a remove with `on_conflict`, "skip", leaves
683
+ // those and removes the rest.
570
684
  async function sendValues(req, res, verb, values) {
571
685
  const body = await readJson(req, VALUES_BODY_MAX);
572
686
  if (!values || !isTable(body)) {
573
687
  sendRefusal(req, res, values ? 400 : 404);
574
688
  return;
575
689
  }
690
+ // whether the call names one scope for all its keys; a list's does
691
+ let single = true;
576
692
  const answer = function (given, shape) {
577
693
  if (given.status === 200) {
578
694
  sendData(req, res, 200, shape ? shape(given.data) : given.data);
579
695
  }
696
+ else if (given.status === 409) {
697
+ sendRefusal(req, res, 409, conflictsAnswer(given.conflicts, single));
698
+ }
580
699
  else {
581
700
  sendRefusal(req, res, given.status);
582
701
  }
@@ -605,7 +724,9 @@ async function sendValues(req, res, verb, values) {
605
724
  sendRefusal(req, res, 400);
606
725
  return;
607
726
  }
608
- const { items, single } = call;
727
+ const { items } = call;
728
+ ({ single } = call);
729
+ const skips = verb === "remove" ? skipsOf(body) : false;
609
730
  const shape = (data) => itemsAnswer(verb, items, single, data);
610
731
  if (verb === "read") {
611
732
  answer(values.readItems(items), shape);
@@ -613,11 +734,14 @@ async function sendValues(req, res, verb, values) {
613
734
  else if (!items.every((item) => VALUE_WRITES.includes(item.scope))) {
614
735
  sendRefusal(req, res, 403);
615
736
  }
737
+ else if (skips === null) {
738
+ sendRefusal(req, res, 400);
739
+ }
616
740
  else if (verb === "write") {
617
741
  answer(values.writeItems(items), shape);
618
742
  }
619
743
  else {
620
- answer(values.removeItems(items));
744
+ answer(values.removeItems(items, skips));
621
745
  }
622
746
  }
623
747
  // [function]
@@ -626,7 +750,7 @@ async function sendValues(req, res, verb, values) {
626
750
  // refuses (app/src/web/arcadible-api's app/api/server.py). The game is the
627
751
  // project's, its channel is local, its players are the ones this run's
628
752
  // tickets named, and no one has deleted an account.
629
- async function sendServer(req, res, route, { game, tickets, values, }) {
753
+ async function sendServer(req, res, route, { game, tickets, values, wires, }) {
630
754
  const known = SERVER_ROUTES.find(([path]) => path.test(route));
631
755
  if (!known) {
632
756
  sendRefusal(req, res, 404);
@@ -670,6 +794,28 @@ async function sendServer(req, res, route, { game, tickets, values, }) {
670
794
  await sendValues(req, res, route.slice("/values/".length), values);
671
795
  return;
672
796
  }
797
+ // a function on a wire, as Arcadible's wires answer one
798
+ // (io-arcadible-wires' src/wires.js): a new wire's id, or a message
799
+ // passed on, marked as the server's
800
+ if (route.startsWith("/wires/")) {
801
+ if (!wires) {
802
+ sendRefusal(req, res, 404);
803
+ }
804
+ else if (route === "/wires/open") {
805
+ sendData(req, res, 200, { id: wires.open() });
806
+ }
807
+ else {
808
+ const sent = await readJson(req);
809
+ const status = isTable(sent) && Object.hasOwn(sent, "data")
810
+ ? wires.send(sent["wire"], sent["data"])
811
+ : 400;
812
+ if (status === 200)
813
+ sendData(req, res, 200, {});
814
+ else
815
+ sendRefusal(req, res, status);
816
+ }
817
+ return;
818
+ }
673
819
  const body = await readJson(req);
674
820
  // a list holds no ticket, and names no server.
675
821
  const asked = isTable(body) ? body : {};
@@ -689,7 +835,7 @@ async function sendServer(req, res, route, { game, tickets, values, }) {
689
835
  // `tickets` mints the game's, and publishes their key, and `playerOf(name)`
690
836
  // is a local player by name (./ticket.js). `faviconFile` is the play page's
691
837
  // favicon.ico, the package's own unless a test names another.
692
- 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, }) {
838
+ 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, wires = null, }) {
693
839
  // the play page's favicon.ico, read once, or null when it can't be read,
694
840
  // which is tried again on the next request.
695
841
  let favicon = null;
@@ -702,6 +848,8 @@ export function pageHandler({ art = () => Promise.resolve(null), faviconFile = F
702
848
  };
703
849
  // the players windows were opened as, by id, so a window's path names one
704
850
  const named = new Map();
851
+ // the links made this run, by id
852
+ const links = new Map();
705
853
  return function (req, res) {
706
854
  // a target that can't be read as a URL (`//`) is no path of the page's.
707
855
  const whole = pathOf(req) ?? "";
@@ -718,6 +866,7 @@ export function pageHandler({ art = () => Promise.resolve(null), faviconFile = F
718
866
  game,
719
867
  tickets,
720
868
  values,
869
+ wires,
721
870
  });
722
871
  }
723
872
  if (values && pathname.startsWith(`${VALUES}/`)) {
@@ -746,6 +895,13 @@ export function pageHandler({ art = () => Promise.resolve(null), faviconFile = F
746
895
  }
747
896
  return sendTicket(req, res, { playerOf, playing, tickets });
748
897
  }
898
+ if (pathname === LINKS) {
899
+ if (req.method !== "POST") {
900
+ res.writeHead(405, { ...HEADERS, allow: "POST" }).end();
901
+ return Promise.resolve();
902
+ }
903
+ return sendLink(req, res, { links, playing });
904
+ }
749
905
  if (opened) {
750
906
  // only the page's own API is under a window's path
751
907
  res.writeHead(404, HEADERS).end();
@@ -782,7 +938,7 @@ export function pageHandler({ art = () => Promise.resolve(null), faviconFile = F
782
938
  "content-security-policy": policy(game.src),
783
939
  "content-type": "text/html; charset=utf-8",
784
940
  });
785
- endWith(req, res, html(game, { art: Boolean(splash), icon: Boolean(mark) }, asked === undefined ? undefined : { player: asked }));
941
+ endWith(req, res, html(game, { art: Boolean(splash), icon: Boolean(mark) }, asked === undefined ? undefined : { player: asked }, linkAsked(req, links)));
786
942
  });
787
943
  }
788
944
  if (pathname === ICON) {