arcadible 1.0.40-next.0 → 1.0.40-next.2

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.
@@ -31,6 +31,39 @@ function warning(path, message, near) {
31
31
  };
32
32
  }
33
33
  // [function]
34
+ // the words of a name, each without a plural's s: `server_origins` is
35
+ // server and origin.
36
+ function words(name) {
37
+ return name
38
+ .toLowerCase()
39
+ .split(/[_\-.\s]+/u)
40
+ .filter((word) => word.length > 2)
41
+ .map((word) => word.replace(/s$/u, ""));
42
+ }
43
+ // [function]
44
+ // the field of that name in another table, such as function.main for a
45
+ // `main` in [game], or null.
46
+ function elsewhere(fields, name, table) {
47
+ const other = Object.keys(fields).find((each) => each !== table && (fields[each] ?? []).includes(name));
48
+ return other ? `${other}.${name}` : null;
49
+ }
50
+ // [function]
51
+ // the known field `name` might mean, when none is spelled near it: one
52
+ // sharing a word with it (`servers`, or a `[server]` table, for
53
+ // client.server_origins), its own table's first; or null.
54
+ function related(fields, name, table) {
55
+ const tables = Object.keys(fields);
56
+ const mine = new Set(words(name));
57
+ const order = table ? [table, ...tables.filter((t) => t !== table)] : tables;
58
+ for (const each of order) {
59
+ const found = (fields[each] ?? []).find((field) => words(field).some((word) => mine.has(word)));
60
+ if (found) {
61
+ return each === table ? found : `${each}.${found}`;
62
+ }
63
+ }
64
+ return null;
65
+ }
66
+ // [function]
34
67
  // a value outside any table, such as a `handle` above `[game]`: the field
35
68
  // it is, in the table that takes it, if any.
36
69
  function untabled(fields, name) {
@@ -49,10 +82,14 @@ export function unknown(root) {
49
82
  return taken ? [] : [untabled(fields, table)];
50
83
  }
51
84
  if (!taken) {
52
- return [warning(table, unknownTable(), closest(table, tables))];
85
+ return [
86
+ warning(table, unknownTable(), closest(table, tables) ?? related(fields, table, null)),
87
+ ];
53
88
  }
54
89
  return Object.keys(values)
55
90
  .filter((field) => !taken.includes(field))
56
- .map((field) => warning(`${table}.${field}`, unknownField(), closest(field, taken)));
91
+ .map((field) => warning(`${table}.${field}`, unknownField(), elsewhere(fields, field, table) ??
92
+ closest(field, taken) ??
93
+ related(fields, field, table)));
57
94
  }));
58
95
  }
@@ -95,21 +95,27 @@ export default defineCommand({
95
95
  "open": {
96
96
  alias: "o",
97
97
  default: false,
98
- describe: "Open the play page in a web browser",
98
+ describe: "Open the play page in a browser",
99
99
  rc: true,
100
100
  type: "boolean",
101
101
  },
102
102
  // run the game for a logged-in player of this name, the same one each
103
- // run; without it, a guest. Left out of the help until tickets ship,
104
- // with the docs.
105
- "player": { hidden: true, rc: true, type: "string" },
103
+ // run; without it, a guest.
104
+ "player": {
105
+ describe: "Play as a logged-in player of this name",
106
+ rc: true,
107
+ type: "string",
108
+ },
106
109
  // serve a play page's host built here, for working on the host itself;
107
110
  // without it, Arcadible's own. Left out of the help.
108
111
  "host": { hidden: true, rc: true, type: "string" },
109
112
  // drop each wire's connection this many seconds after it comes on, to
110
- // try a game's rejoin. Left out of the help until wires ship, with the
111
- // docs.
112
- "wires-drop": { hidden: true, rc: true, type: "number" },
113
+ // try a game's rejoin.
114
+ "wires-drop": {
115
+ describe: "Drop each wire's connection after this many seconds",
116
+ rc: true,
117
+ type: "number",
118
+ },
113
119
  "quiet": QUIET,
114
120
  "cwd": CWD,
115
121
  },
@@ -125,8 +125,8 @@ export function client() {
125
125
  });
126
126
  }
127
127
  if (clientNames(table).join(" ").length > MAX_NAMES_LENGTH) {
128
- // the servers are named only where the manifest lists them, since
129
- // the docs leave `server_origins` out until it ships (message.ts).
128
+ // the servers are named only where the manifest lists them, so a
129
+ // game with hosts alone reads of its hosts alone.
130
130
  const servers = table.server_origins?.length
131
131
  ? ", with client.server_origins,"
132
132
  : "";
@@ -7,14 +7,8 @@ import { indentLines } from "../util/strs.js";
7
7
  import { MANIFEST } from "../config/url.js";
8
8
  // [declare]
9
9
  // fields the CLI checks but the docs leave out until they ship, so their
10
- // issues have no link: a game's function and its own servers, until they do
11
- // (plan/SERVERS.md).
12
- export const UNDOCUMENTED = new Set([
13
- "function.main",
14
- "function.outbound_hosts",
15
- "function.secrets",
16
- "client.server_origins",
17
- ]);
10
+ // issues have no link: a function's secrets, until they do (doc/TODO.md).
11
+ export const UNDOCUMENTED = new Set(["function.secrets"]);
18
12
  // [function]
19
13
  // the URL of a field's rules in the manifest reference, e.g. #game.title,
20
14
  // or null for an undocumented field.
@@ -543,7 +543,9 @@ function skipsOf(body) {
543
543
  // but a player's, a guest's call for a player's, or a player who isn't the
544
544
  // one playing is 403. Only JSON is taken, as for a ticket, so a page
545
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 } }`.
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.
547
549
  async function sendGameValues(req, res, verb, { player, values }) {
548
550
  const refuse = function (status) {
549
551
  res.writeHead(status, HEADERS).end();
@@ -561,6 +563,16 @@ async function sendGameValues(req, res, verb, { player, values }) {
561
563
  let single = true;
562
564
  const answer = function (given, shape) {
563
565
  if (given.status !== 200 && given.status !== 409) {
566
+ if (given.code) {
567
+ res.writeHead(given.status, {
568
+ ...HEADERS,
569
+ "content-type": "application/json; charset=utf-8",
570
+ });
571
+ res.end(JSON.stringify({
572
+ data: { code: given.code, retry: retryOf(given.code) },
573
+ }));
574
+ return;
575
+ }
564
576
  refuse(given.status);
565
577
  return;
566
578
  }
@@ -684,9 +696,9 @@ function windowOf(pathname, named) {
684
696
  // [function]
685
697
  // a refusal in the API's own envelope: what it says of the status, and,
686
698
  // for a conflict, the items it names.
687
- function sendRefusal(req, res, status, conflicts) {
699
+ function sendRefusal(req, res, status, conflicts, named) {
688
700
  const [title, message] = REFUSALS[status] ?? ["", ""];
689
- const code = REFUSAL_CODES[status] ?? "invalid";
701
+ const code = named ?? REFUSAL_CODES[status] ?? "invalid";
690
702
  sendData(req, res, status, {
691
703
  code,
692
704
  message,
@@ -720,7 +732,7 @@ async function sendValues(req, res, verb, values) {
720
732
  sendRefusal(req, res, 409, conflictsAnswer(given.conflicts, single));
721
733
  }
722
734
  else {
723
- sendRefusal(req, res, given.status);
735
+ sendRefusal(req, res, given.status, undefined, given.code);
724
736
  }
725
737
  };
726
738
  if (verb === "list") {
@@ -807,8 +819,13 @@ async function sendServer(req, res, route, { game, tickets, values, wires, }) {
807
819
  }
808
820
  const asked = listed.split(",").map((id) => id.trim());
809
821
  const ids = [...new Set(asked)];
822
+ // past a call's players is too_many, as the API's PLAYERS_MAX answers
823
+ // it, where ids that aren't ids are invalid
824
+ if (ids.length > PLAYERS_MAX) {
825
+ sendRefusal(req, res, 400, undefined, "too_many");
826
+ return;
827
+ }
810
828
  if (asked.length > PLAYERS_ASKED_MAX ||
811
- ids.length > PLAYERS_MAX ||
812
829
  !ids.every((id) => PLAYER_ID.test(id))) {
813
830
  sendRefusal(req, res, 400);
814
831
  return;
@@ -20,6 +20,7 @@ const RETRIES = {
20
20
  no_function: "no",
21
21
  not_found: "no",
22
22
  too_large: "no",
23
+ too_many: "no",
23
24
  unavailable: "yes",
24
25
  };
25
26
  // [function]
@@ -61,15 +61,20 @@ function isRevision(value) {
61
61
  return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
62
62
  }
63
63
  // [function]
64
- // a call's items, or null: 1 to a call's keys, each an object with a scope,
64
+ // a call that names more items than a call takes: `too_many`, as the API's
65
+ // ITEMS_MAX answers it, where any other call that isn't one is `invalid`.
66
+ const TOO_MANY = { code: "too_many", data: null, status: 400 };
67
+ // [function]
68
+ // a call's items, or null, or TOO_MANY past a call's keys: 1 to a call's keys, each an object with a scope,
65
69
  // an owner, and a key, none named twice, and each of the verb's own fields
66
70
  // (`fields`) a revision where it's given; `value`, for a write, there.
67
71
  function itemsOf(given, fields, needsValue) {
68
- if (!Array.isArray(given) ||
69
- given.length < 1 ||
70
- given.length > LIMITS.callKeys) {
72
+ if (!Array.isArray(given) || given.length < 1) {
71
73
  return null;
72
74
  }
75
+ if (given.length > LIMITS.callKeys) {
76
+ return TOO_MANY;
77
+ }
73
78
  const seen = new Set();
74
79
  for (const item of given) {
75
80
  if (!isTable(item) ||
@@ -203,6 +208,9 @@ export function values({ isPlayer, now = () => Date.now(), root, }) {
203
208
  if (!items) {
204
209
  return { data: null, status: 400 };
205
210
  }
211
+ if (!Array.isArray(items)) {
212
+ return items;
213
+ }
206
214
  const refused = refusalOf(items);
207
215
  if (refused) {
208
216
  return { data: null, status: refused };
@@ -237,6 +245,9 @@ export function values({ isPlayer, now = () => Date.now(), root, }) {
237
245
  if (!items) {
238
246
  return { data: null, status: 400 };
239
247
  }
248
+ if (!Array.isArray(items)) {
249
+ return items;
250
+ }
240
251
  const refused = refusalOf(items);
241
252
  if (refused) {
242
253
  return { data: null, status: refused };
@@ -293,6 +304,9 @@ export function values({ isPlayer, now = () => Date.now(), root, }) {
293
304
  if (!items) {
294
305
  return { data: null, status: 400 };
295
306
  }
307
+ if (!Array.isArray(items)) {
308
+ return items;
309
+ }
296
310
  const refused = refusalOf(items);
297
311
  if (refused) {
298
312
  return { data: null, status: refused };
@@ -42,7 +42,7 @@
42
42
  },
43
43
  "license": "Apache-2.0",
44
44
  "name": "arcadible",
45
- "version": "1.0.40-next.0"
45
+ "version": "1.0.40-next.2"
46
46
  },
47
47
  "node_modules/@cacheable/memory": {
48
48
  "dependencies": {
@@ -2433,5 +2433,5 @@
2433
2433
  }
2434
2434
  },
2435
2435
  "requires": true,
2436
- "version": "1.0.40-next.0"
2436
+ "version": "1.0.40-next.2"
2437
2437
  }
package/package.json CHANGED
@@ -66,5 +66,5 @@
66
66
  "test": "tsc && npm run lint && node --test \"test/**/*.test.js\" \"test/**/*.test.mjs\""
67
67
  },
68
68
  "type": "module",
69
- "version": "1.0.40-next.0"
69
+ "version": "1.0.40-next.2"
70
70
  }