arcadible 1.0.42 → 1.0.44

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 (41) hide show
  1. package/dist/action/delete/tool.js +2 -2
  2. package/dist/action/deploy/tool.js +5 -5
  3. package/dist/action/function/logs.js +102 -30
  4. package/dist/action/function/tool.js +2 -2
  5. package/dist/action/game.js +10 -8
  6. package/dist/action/init/tool.js +26 -4
  7. package/dist/action/key/tool.js +5 -5
  8. package/dist/action/pack/tool.js +2 -2
  9. package/dist/action/predeploy/ac.js +4 -4
  10. package/dist/action/secret/tool.js +9 -8
  11. package/dist/action/serve/tool.js +3 -3
  12. package/dist/action/value/tool.js +6 -6
  13. package/dist/action/wire/tool.js +2 -2
  14. package/dist/api.js +2 -2
  15. package/dist/command/init.js +2 -2
  16. package/dist/config/code.js +22 -20
  17. package/dist/config/retry.js +105 -0
  18. package/dist/phrase/deploy.js +2 -2
  19. package/dist/phrase/function.js +2 -2
  20. package/dist/phrase/game.js +1 -1
  21. package/dist/phrase/init.js +7 -1
  22. package/dist/phrase/pack.js +6 -1
  23. package/dist/phrase/server.js +1 -1
  24. package/dist/runner/bundle.js +2 -2
  25. package/dist/runner/outbound.js +2 -0
  26. package/dist/schema/game.js +1 -1
  27. package/dist/serve/message.js +79 -0
  28. package/dist/serve/page.js +70 -60
  29. package/dist/serve/retry.js +4 -8
  30. package/dist/serve/runner.js +15 -6
  31. package/dist/serve/ticket.js +5 -5
  32. package/dist/serve/wires.js +10 -6
  33. package/dist/util/error.js +28 -8
  34. package/dist/util/http.js +6 -2
  35. package/dist/util/output.js +54 -16
  36. package/dist/util/request.js +2 -1
  37. package/dist/util/s3.js +1 -1
  38. package/dist/util/schema.js +4 -2
  39. package/dist/wires/room.js +30 -37
  40. package/npm-shrinkwrap.json +67 -67
  41. package/package.json +2 -2
@@ -13,6 +13,7 @@ import { readFile } from "node:fs/promises";
13
13
  import { endWith, pathOf } from "./game.js";
14
14
  import { FAVICON } from "../config/asset.js";
15
15
  import { isTable } from "../util/objs.js";
16
+ import { apiMessage, ticketMessage } from "./message.js";
16
17
  import { retryOf } from "./retry.js";
17
18
  import { originOf } from "./ticket.js";
18
19
  import { SCOPES, } from "./values.js";
@@ -79,48 +80,27 @@ const VALUES_BODY_MAX = 512 * 1024;
79
80
  // the scopes a server writes: all but the developer's, which is the
80
81
  // project's own files here, and arc's and Studio's on Arcadible.
81
82
  const VALUE_WRITES = ["game", "player", "server"];
82
- // what the API says of a request it refuses (app/lib/python/offst/error.py),
83
- // in an error's `data`, with its status as `code`.
84
83
  // each refusal's code, the status's word, as the API's own (its
85
84
  // app/result.py's CODES; ERRORS.md).
86
85
  const REFUSAL_CODES = {
87
86
  400: "invalid",
88
87
  403: "denied",
89
88
  404: "not_found",
90
- 405: "method_not_allowed",
89
+ 405: "invalid",
91
90
  409: "conflict",
92
91
  413: "too_large",
93
92
  507: "full",
94
93
  };
95
- const REFUSALS = {
96
- 400: [
97
- "Bad Request",
98
- "The request could not be understood by the server due to malformed syntax.",
99
- ],
100
- 404: [
101
- "Not Found",
102
- "The server has not found anything matching the Request-URI.",
103
- ],
104
- 403: [
105
- "Forbidden",
106
- "The server understood the request, but is refusing to fulfill it.",
107
- ],
108
- 405: [
109
- "Method Not Allowed",
110
- "The method specified in the Request-Line is not allowed for the resource identified by the Request-URI.",
111
- ],
112
- 409: [
113
- "Conflict",
114
- "The request could not be completed due to a conflict with the current state of the resource.",
115
- ],
116
- 413: [
117
- "Content Too Large",
118
- "The server is refusing to process a request because the request content is larger than the server is willing or able to process.",
119
- ],
120
- 507: [
121
- "Insufficient Storage",
122
- "The server is unable to store the representation needed to complete the request.",
123
- ],
94
+ // each status's title, as the API's answer names it beside the code's
95
+ // message (app/result.py's result_error).
96
+ const TITLES = {
97
+ 400: "Bad Request",
98
+ 403: "Forbidden",
99
+ 404: "Not Found",
100
+ 405: "Method Not Allowed",
101
+ 409: "Conflict",
102
+ 413: "Content Too Large",
103
+ 507: "Insufficient Storage",
124
104
  };
125
105
  // the most a ticket's request holds: a server's address.
126
106
  const BODY_MAX = 4096;
@@ -310,11 +290,28 @@ function readJson(req, max = BODY_MAX) {
310
290
  // ticket. 400 a body that isn't an object; 403 no one to make it; 413 data
311
291
  // past 1 KB as JSON. None of Arcadible's rate is kept, nor its 30 days: a
312
292
  // link lasts until arc serve stops.
293
+ // a refusal of the page's own API (values, tickets, links, a log-in), as
294
+ // the play site answers one: its status, and `{ data: { code, message,
295
+ // retry } }`, which the page reads before the status. The code is the
296
+ // API's for the status (`REFUSAL_CODES`), but where the play site names its
297
+ // own, and the message the API's for the code, which the play site passes
298
+ // on.
299
+ function sendPlayRefusal(res, status, code = REFUSAL_CODES[status] ?? "invalid", headers = {}) {
300
+ res.writeHead(status, {
301
+ ...HEADERS,
302
+ ...headers,
303
+ "content-type": "application/json; charset=utf-8",
304
+ });
305
+ res.end(JSON.stringify({
306
+ data: { code, message: apiMessage(code), retry: retryOf(code, status) },
307
+ }));
308
+ }
309
+ // [function]
313
310
  async function sendLink(req, res, { links, onLink, origin, playing, }) {
314
311
  const type = ((req.headers["content-type"] ?? "").split(";")[0] ?? "").trim();
315
312
  const asked = type.toLowerCase() === "application/json" ? await readJson(req) : null;
316
313
  if (!isTable(asked)) {
317
- res.writeHead(400, HEADERS).end();
314
+ sendPlayRefusal(res, 400);
318
315
  return;
319
316
  }
320
317
  const guest = asked["guest"];
@@ -324,12 +321,12 @@ async function sendLink(req, res, { links, onLink, origin, playing, }) {
324
321
  ? { guest: true, id: guest, name: null }
325
322
  : null;
326
323
  if (!by) {
327
- res.writeHead(403, HEADERS).end();
324
+ sendPlayRefusal(res, 403);
328
325
  return;
329
326
  }
330
327
  const data = asked["data"] ?? null;
331
328
  if (Buffer.byteLength(JSON.stringify(data)) > LINK_DATA_MAX) {
332
- res.writeHead(413, HEADERS).end();
329
+ sendPlayRefusal(res, 413);
333
330
  return;
334
331
  }
335
332
  let id = "";
@@ -371,7 +368,7 @@ async function sendTicket(req, res, { playerOf, playing, tickets, }) {
371
368
  const type = ((req.headers["content-type"] ?? "").split(";")[0] ?? "").trim();
372
369
  const data = type.toLowerCase() === "application/json" ? await readJson(req) : null;
373
370
  if (data === null || typeof data !== "object") {
374
- res.writeHead(400, HEADERS).end();
371
+ sendPlayRefusal(res, 400);
375
372
  return;
376
373
  }
377
374
  // a list names no server, and no player.
@@ -379,12 +376,14 @@ async function sendTicket(req, res, { playerOf, playing, tickets, }) {
379
376
  const named = Object.hasOwn(asked, "player") && !req.headers.origin;
380
377
  const player = named ? playerOf(asked["player"]) : playing;
381
378
  if (player === false) {
382
- res.writeHead(400, HEADERS).end();
379
+ sendPlayRefusal(res, 400);
383
380
  return;
384
381
  }
385
382
  const { status, ticket } = await tickets.mint(asked["server"], player, asked["guest"]);
383
+ // a server that isn't an address is `invalid`, and one the build didn't
384
+ // declare `denied`, as Arcadible's: the status's code (`REFUSAL_CODES`)
386
385
  if (status !== 200) {
387
- res.writeHead(status, HEADERS).end();
386
+ sendPlayRefusal(res, status);
388
387
  return;
389
388
  }
390
389
  res.writeHead(200, {
@@ -404,13 +403,13 @@ async function sendLogIn(req, res, { game, playerOf, tickets, }) {
404
403
  const type = ((req.headers["content-type"] ?? "").split(";")[0] ?? "").trim();
405
404
  const data = type.toLowerCase() === "application/json" ? await readJson(req) : null;
406
405
  if (data === null || typeof data !== "object") {
407
- res.writeHead(400, HEADERS).end();
406
+ sendPlayRefusal(res, 400);
408
407
  return;
409
408
  }
410
409
  if (!game.player) {
411
410
  const player = playerOf(LOGIN_NAME);
412
411
  if (!player) {
413
- res.writeHead(404, HEADERS).end();
412
+ sendPlayRefusal(res, 404);
414
413
  return;
415
414
  }
416
415
  game.player = player;
@@ -542,13 +541,13 @@ function skipsOf(body) {
542
541
  // and reads and writes its own player's: the server's scope, a write to any
543
542
  // but a player's, a guest's call for a player's, or a player who isn't the
544
543
  // one playing is 403. Only JSON is taken, as for a ticket, so a page
545
- // elsewhere can't ask. A refusal is its status, with no body, but a
546
- // conflict's (409), which names its items: `{ data: { conflicts } }`, and
547
- // one that names its code (`too_many`), `{ data: { code, retry } }`, as the
548
- // play site passes the API's on.
544
+ // elsewhere can't ask. A refusal is `{ data: { code, message, retry } }`, the
545
+ // code the values' own (`too_many`) or its status's (`sendPlayRefusal`),
546
+ // but a conflict's (409), which names its items: `{ data: { conflicts } }`,
547
+ // as the play site passes the API's on.
549
548
  async function sendGameValues(req, res, verb, { player, values }) {
550
549
  const refuse = function (status) {
551
- res.writeHead(status, HEADERS).end();
550
+ sendPlayRefusal(res, status);
552
551
  };
553
552
  const type = ((req.headers["content-type"] ?? "").split(";")[0] ?? "").trim();
554
553
  const body = type.toLowerCase() === "application/json"
@@ -569,7 +568,11 @@ async function sendGameValues(req, res, verb, { player, values }) {
569
568
  "content-type": "application/json; charset=utf-8",
570
569
  });
571
570
  res.end(JSON.stringify({
572
- data: { code: given.code, retry: retryOf(given.code) },
571
+ data: {
572
+ code: given.code,
573
+ message: apiMessage(given.code),
574
+ retry: retryOf(given.code),
575
+ },
573
576
  }));
574
577
  return;
575
578
  }
@@ -694,17 +697,17 @@ function windowOf(pathname, named) {
694
697
  return player ? { path, player } : false;
695
698
  }
696
699
  // [function]
697
- // a refusal in the API's own envelope: what it says of the status, and,
698
- // for a conflict, the items it names.
700
+ // a refusal in the API's own envelope: the status's title, the code's
701
+ // message, as the API's (`apiMessage`), and, for a conflict, the items it
702
+ // names.
699
703
  function sendRefusal(req, res, status, conflicts, named) {
700
- const [title, message] = REFUSALS[status] ?? ["", ""];
701
704
  const code = named ?? REFUSAL_CODES[status] ?? "invalid";
702
705
  sendData(req, res, status, {
703
706
  code,
704
- message,
707
+ message: apiMessage(code),
705
708
  retry: retryOf(code),
706
709
  status,
707
- title,
710
+ title: TITLES[status] ?? "",
708
711
  type: "HttpError",
709
712
  ...(conflicts ? { conflicts } : {}),
710
713
  });
@@ -875,8 +878,15 @@ async function sendServer(req, res, route, { game, tickets, values, wires, }) {
875
878
  return;
876
879
  }
877
880
  const { data, status } = await tickets.check(asked["ticket"], asked["server"]);
878
- // a ticket refused says whether to try again, as the API's refusals do
879
- sendData(req, res, status, status === 200 ? data : { ...data, retry: retryOf(data.code) });
881
+ // a ticket refused says why, and whether to try again, as the API's
882
+ // refusals do
883
+ sendData(req, res, status, status === 200
884
+ ? data
885
+ : {
886
+ ...data,
887
+ message: ticketMessage(data.code),
888
+ retry: retryOf(data.code),
889
+ });
880
890
  }
881
891
  // [function]
882
892
  // the request handler for the page's origin. `reloads` keeps the pages
@@ -906,7 +916,7 @@ export function pageHandler({ art = () => Promise.resolve(null), faviconFile = F
906
916
  // a window's own player, from its path (undefined: the page's default)
907
917
  const opened = windowOf(whole, named);
908
918
  if (opened === false) {
909
- res.writeHead(404, HEADERS).end();
919
+ sendPlayRefusal(res, 404);
910
920
  return Promise.resolve();
911
921
  }
912
922
  const pathname = opened ? opened.path : whole;
@@ -922,39 +932,39 @@ export function pageHandler({ art = () => Promise.resolve(null), faviconFile = F
922
932
  if (values && pathname.startsWith(`${VALUES}/`)) {
923
933
  const verb = pathname.slice(VALUES.length + 1);
924
934
  if (!["list", "read", "remove", "write"].includes(verb)) {
925
- res.writeHead(404, HEADERS).end();
935
+ sendPlayRefusal(res, 404);
926
936
  return Promise.resolve();
927
937
  }
928
938
  if (req.method !== "POST") {
929
- res.writeHead(405, { ...HEADERS, allow: "POST" }).end();
939
+ sendPlayRefusal(res, 405, undefined, { allow: "POST" });
930
940
  return Promise.resolve();
931
941
  }
932
942
  return sendGameValues(req, res, verb, { player: playing, values });
933
943
  }
934
944
  if (pathname === LOGIN && !opened) {
935
945
  if (req.method !== "POST") {
936
- res.writeHead(405, { ...HEADERS, allow: "POST" }).end();
946
+ sendPlayRefusal(res, 405, undefined, { allow: "POST" });
937
947
  return Promise.resolve();
938
948
  }
939
949
  return sendLogIn(req, res, { game, playerOf, tickets });
940
950
  }
941
951
  if (tickets && pathname === TICKETS) {
942
952
  if (req.method !== "POST") {
943
- res.writeHead(405, { ...HEADERS, allow: "POST" }).end();
953
+ sendPlayRefusal(res, 405, undefined, { allow: "POST" });
944
954
  return Promise.resolve();
945
955
  }
946
956
  return sendTicket(req, res, { playerOf, playing, tickets });
947
957
  }
948
958
  if (pathname === LINKS) {
949
959
  if (req.method !== "POST") {
950
- res.writeHead(405, { ...HEADERS, allow: "POST" }).end();
960
+ sendPlayRefusal(res, 405, undefined, { allow: "POST" });
951
961
  return Promise.resolve();
952
962
  }
953
963
  return sendLink(req, res, { links, onLink, origin, playing });
954
964
  }
955
965
  if (opened) {
956
966
  // only the page's own API is under a window's path
957
- res.writeHead(404, HEADERS).end();
967
+ sendPlayRefusal(res, 404);
958
968
  return Promise.resolve();
959
969
  }
960
970
  if (req.method !== "GET" && req.method !== "HEAD") {
@@ -9,31 +9,27 @@
9
9
  export const RETRY = "x-arcadible-retry";
10
10
  // each code `arc serve` refuses with, and its answer.
11
11
  const RETRIES = {
12
- busy: "yes",
13
12
  conflict: "later",
14
13
  denied: "no",
15
14
  expired: "later",
16
15
  full: "later",
17
16
  function_failed: "later",
18
17
  invalid: "no",
19
- method_not_allowed: "no",
20
18
  no_function: "no",
21
19
  not_found: "no",
22
20
  too_large: "no",
23
21
  too_many: "no",
22
+ unauthorized: "no",
24
23
  unavailable: "yes",
25
24
  };
26
25
  // [function]
27
26
  /**
28
27
  * A refusal's retry, as Arcadible's parts say it (the dispatcher's and the
29
- * wires' `retryOf`): a `401` "later" whatever its code, since its fix is a
30
- * new ticket; otherwise the code's own, or, for a code that isn't there,
31
- * its status's: a `429` or a `5xx` but `507` "yes", a `409` or a `507`
32
- * "later", anything else (or no status) "no".
28
+ * wires' `retryOf`): the code's own, or, for a code that isn't there, its
29
+ * status's: a `429` or a `5xx` but `507` "yes", a `409` or a `507` "later",
30
+ * anything else (or no status) "no".
33
31
  */
34
32
  export function retryOf(code, status) {
35
- if (status === 401)
36
- return "later";
37
33
  const own = RETRIES[code];
38
34
  if (own !== undefined)
39
35
  return own;
@@ -15,6 +15,7 @@ import { dirname, join } from "node:path";
15
15
  // [import]
16
16
  import { bundle } from "../runner/bundle.js";
17
17
  import { ENTRY, ENTRY_MODULE, FUNCTION_MODULE } from "../runner/entry.js";
18
+ import { runnerMessage } from "./message.js";
18
19
  import { RETRY, retryOf } from "./retry.js";
19
20
  // [declare]
20
21
  // where a runner answers on the game's origin, as on Arcadible, where the
@@ -80,7 +81,7 @@ export default {
80
81
  } catch (err) {
81
82
  console.error("The function threw:", String(err));
82
83
  return Response.json(
83
- { code: ${JSON.stringify(FAILED)}, retry: ${JSON.stringify(retryOf(FAILED))} },
84
+ { code: ${JSON.stringify(FAILED)}, message: ${JSON.stringify(runnerMessage(FAILED))}, retry: ${JSON.stringify(retryOf(FAILED))} },
84
85
  {
85
86
  headers: { [REFUSAL]: ${JSON.stringify(FAILED)}, [RETRY]: ${JSON.stringify(retryOf(FAILED))} },
86
87
  status: 502,
@@ -109,7 +110,8 @@ const CORS = {
109
110
  };
110
111
  // [function]
111
112
  // Arcadible's own answer, marked as the dispatcher marks it: a status, a
112
- // code the SDK's `functions.fetch` rejects with, and whether to try again.
113
+ // code the SDK's `functions.fetch` rejects with, its message, and whether
114
+ // to try again.
113
115
  function refuse(res, status, code) {
114
116
  const retry = retryOf(code, status);
115
117
  res.writeHead(status, {
@@ -118,7 +120,7 @@ function refuse(res, status, code) {
118
120
  [REFUSAL]: code,
119
121
  [RETRY]: retry,
120
122
  });
121
- res.end(JSON.stringify({ code, retry }));
123
+ res.end(JSON.stringify({ code, message: runnerMessage(code), retry }));
122
124
  }
123
125
  // [function]
124
126
  /**
@@ -452,9 +454,12 @@ export async function runner({ dir, handler, hosts = [], main, origin, secrets =
452
454
  const [scheme, ticket] = (req.headers.authorization ?? "").split(" ");
453
455
  const checked = scheme?.toLowerCase() === "bearer"
454
456
  ? await tickets.check(ticket, origin)
455
- : { data: { code: "invalid" }, status: 422 };
457
+ : { data: { code: "unauthorized" }, status: 422 };
458
+ // a ticket Arcadible doesn't take is `unauthorized`, or `expired`, as
459
+ // the check and the dispatcher say it
456
460
  if (checked.status !== 200) {
457
- refuse(res, 401, checked.data.code);
461
+ const { code } = checked.data;
462
+ refuse(res, 401, code);
458
463
  return;
459
464
  }
460
465
  // a body past what a function may be sent, said or streamed, as the
@@ -483,7 +488,11 @@ export async function runner({ dir, handler, hosts = [], main, origin, secrets =
483
488
  [RETRY]: retryOf(FAILED),
484
489
  });
485
490
  }
486
- res.end(JSON.stringify({ code: FAILED, retry: retryOf(FAILED) }));
491
+ res.end(JSON.stringify({
492
+ code: FAILED,
493
+ message: runnerMessage(FAILED),
494
+ retry: retryOf(FAILED),
495
+ }));
487
496
  };
488
497
  if (failed || !child) {
489
498
  fail();
@@ -134,7 +134,7 @@ export function tickets({ file, game, issuer, now = () => Date.now(), servers =
134
134
  return {
135
135
  // what a ticket says, checked as Arcadible's server API checks one for a
136
136
  // server with no JWT library: `{ status, data }`, 200 with what it says,
137
- // or 422 with `code`, "invalid" or "expired".
137
+ // or 422 with `code`, "unauthorized" or "expired".
138
138
  async check(ticket, server) {
139
139
  const refused = (code) => ({
140
140
  data: { code },
@@ -147,7 +147,7 @@ export function tickets({ file, game, issuer, now = () => Date.now(), servers =
147
147
  body === undefined ||
148
148
  mark === undefined ||
149
149
  rest.length) {
150
- return refused("invalid");
150
+ return refused("unauthorized");
151
151
  }
152
152
  const { key: signer } = await key();
153
153
  let claims;
@@ -163,20 +163,20 @@ export function tickets({ file, game, issuer, now = () => Date.now(), servers =
163
163
  if (!isTable(claims) ||
164
164
  claims["aud"] !== origin ||
165
165
  claims["game"] !== game) {
166
- return refused("invalid");
166
+ return refused("unauthorized");
167
167
  }
168
168
  // a player is named by their id in the game, a UUID, as the API and
169
169
  // the Server SDK ask; no `sub` is a guest
170
170
  const sub = claims["sub"];
171
171
  if (sub !== undefined && (typeof sub !== "string" || !UUID.test(sub))) {
172
- return refused("invalid");
172
+ return refused("unauthorized");
173
173
  }
174
174
  // a guest's id, when the page named one: a claim of its own, and
175
175
  // never beside a player
176
176
  const guest = claims["guest"];
177
177
  if (guest !== undefined &&
178
178
  (typeof guest !== "string" || !UUID.test(guest))) {
179
- return refused("invalid");
179
+ return refused("unauthorized");
180
180
  }
181
181
  const exp = claims["exp"];
182
182
  // good until its time and the leeway a clock is allowed, as the
@@ -10,7 +10,8 @@ import crypto from "node:crypto";
10
10
  // [import]
11
11
  import { WebSocketServer } from "ws";
12
12
  // [import]
13
- import { closeOf, createRoom, LIMITS, PING, PONG, protocolOf, PROTOCOLS, ticketClose, } from "../wires/room.js";
13
+ import { closeOf, createRoom, LIMITS, PING, PONG, protocolOf, PROTOCOLS, } from "../wires/room.js";
14
+ import { wiresMessage } from "./message.js";
14
15
  import { RETRY, retryOf } from "./retry.js";
15
16
  import { CHANNEL } from "./ticket.js";
16
17
  // [declare]
@@ -108,9 +109,12 @@ export function wires({ drop = null, game, origin, tickets, }) {
108
109
  const memberOf = async function (ticket) {
109
110
  const checked = typeof ticket === "string"
110
111
  ? await tickets.check(ticket, origin)
111
- : { data: { code: "invalid" }, status: 422 };
112
- if (checked.status !== 200)
112
+ : { data: { code: "unauthorized" }, status: 422 };
113
+ // a ticket Arcadible doesn't take is `unauthorized`, or `expired`, as
114
+ // the check and the wires' Worker say it
115
+ if (checked.status !== 200) {
113
116
  return { code: checked.data.code };
117
+ }
114
118
  const { guest, player } = checked.data;
115
119
  if (typeof player === "string") {
116
120
  return {
@@ -150,7 +154,7 @@ export function wires({ drop = null, game, origin, tickets, }) {
150
154
  [REFUSAL]: code,
151
155
  [RETRY]: retry,
152
156
  });
153
- res.end(JSON.stringify({ code, retry }));
157
+ res.end(JSON.stringify({ code, message: wiresMessage(status, code), retry }));
154
158
  };
155
159
  if (req.method !== "POST") {
156
160
  refuse(405, "invalid");
@@ -187,7 +191,7 @@ export function wires({ drop = null, game, origin, tickets, }) {
187
191
  // a connection that's refused opens, and closes with why, as its
188
192
  // protocol says it: a browser is told nothing of one that never
189
193
  // opens
190
- const refuse = (code, number) => ws.close(...closeOf(code, protocol, number));
194
+ const refuse = (code) => ws.close(...closeOf(code, protocol));
191
195
  ws.on("error", () => ws.terminate());
192
196
  if (protocolOf(offered) === null || !isOurs(id)) {
193
197
  refuse("invalid");
@@ -195,7 +199,7 @@ export function wires({ drop = null, game, origin, tickets, }) {
195
199
  }
196
200
  void memberOf(ticket).then((who) => {
197
201
  if ("code" in who) {
198
- refuse(who.code, ticketClose(who.code));
202
+ refuse(who.code);
199
203
  return;
200
204
  }
201
205
  // the wires a player is on, counted here as Arcadible counts
@@ -1,5 +1,5 @@
1
1
  // [import]
2
- import { ALL, API_REFUSED, INTERNAL, UNEXPECTED } from "../config/code.js";
2
+ import { ALL, API_REFUSED, UNAVAILABLE, UNEXPECTED } from "../config/code.js";
3
3
  import { HttpError } from "./http.js";
4
4
  // [class]
5
5
  // a failure with its code (config/code.js), which scripts match on while the
@@ -9,30 +9,50 @@ import { HttpError } from "./http.js";
9
9
  export class ArcError extends Error {
10
10
  code;
11
11
  data;
12
+ status;
12
13
  // [constructor]
13
14
  constructor(code, message, options) {
14
15
  if (!ALL.has(code)) {
15
16
  throw new TypeError(`Unknown error code: ${code}`);
16
17
  }
17
- const { data, ...rest } = options ?? {};
18
+ const { data, status, ...rest } = options ?? {};
18
19
  super(message, rest);
19
20
  this.code = code;
20
21
  this.data = data;
22
+ this.status = status;
21
23
  }
22
24
  }
23
25
  // [function]
24
26
  // the code a failure reports: its own; the API's, when it names one of the
25
- // CLI's (ERRORS.md); api_refused for any other request the API refused;
26
- // internal for a failure on Arcadible's side (a 5xx), worth trying again;
27
- // and unexpected for anything else, a bug in the CLI, which isn't.
27
+ // CLI's (ERRORS.md), read before its status, so a 503's `unavailable` is
28
+ // said as it came; unavailable for a 5xx that names no code, an answer that
29
+ // isn't Arcadible's own (a front end's 502 or 503), worth trying again;
30
+ // api_refused for any other refusal, carrying the code it was told
31
+ // (`error.cause`); and unexpected for anything else, a bug in the CLI,
32
+ // which isn't.
28
33
  export function codeOf(ex) {
29
34
  if (ex instanceof ArcError) {
30
35
  return ex.code;
31
36
  }
32
37
  if (ex instanceof HttpError) {
33
- if (ex.status >= 500)
34
- return INTERNAL;
35
- return ex.code !== undefined && ALL.has(ex.code) ? ex.code : API_REFUSED;
38
+ if (ex.code !== undefined && ALL.has(ex.code))
39
+ return ex.code;
40
+ return ex.status >= 500 && ex.code === undefined
41
+ ? UNAVAILABLE
42
+ : API_REFUSED;
36
43
  }
37
44
  return UNEXPECTED;
38
45
  }
46
+ // [function]
47
+ // what a failure was told and didn't pass on as its own code: the API's
48
+ // refusal, or another service's that the failure keeps as its cause (the
49
+ // logs Worker's). A catch-all says it beside its own code (ERRORS.md,
50
+ // Passing a code on), so the condition isn't lost to whoever has to act.
51
+ export function toldOf(ex) {
52
+ if (ex instanceof HttpError) {
53
+ return ex;
54
+ }
55
+ return ex instanceof ArcError && ex.cause instanceof HttpError
56
+ ? ex.cause
57
+ : null;
58
+ }
package/dist/util/http.js CHANGED
@@ -3,14 +3,18 @@ import { httpError } from "../phrase/index.js";
3
3
  // [class]
4
4
  export class HttpError extends Error {
5
5
  // the API's code for the refusal, which a client reads before its status:
6
- // the database's own (`game_unavailable`), or the status's word
6
+ // the database's own (`game_not_found`), or the status's word
7
7
  // (`denied`); undefined when the answer has none.
8
8
  code;
9
+ // whether to try again, as the API said beside its code; undefined when
10
+ // the answer has none.
11
+ retry;
9
12
  status;
10
13
  // [constructor]
11
- constructor(status, { code, message, title } = {}) {
14
+ constructor(status, { code, message, retry, title } = {}) {
12
15
  super(message);
13
16
  this.code = code;
17
+ this.retry = retry;
14
18
  this.status = status;
15
19
  this.name = title || "HTTP Error";
16
20
  this.message = httpError(this.name, this.status, message);