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
@@ -0,0 +1,317 @@
1
+ // arc serve's values: the local stand-in for a game's values on Arcadible
2
+ // (plan/VALUES.md), kept by arc serve itself, in its own process, so the
3
+ // game, its function, and a server of the developer's beside it reach the
4
+ // same ones. It answers as Arcadible's database functions do
5
+ // (app/src/web/arcadible-api/db/function/game_value.sql): the same keys,
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
8
+ // are the project's own files, read as they change, and never written here;
9
+ // the rest are kept in arc's folder in the project, so a match survives a
10
+ // restart.
11
+ // [import]
12
+ import { mkdirSync, readdirSync, readFileSync, statSync, writeFileSync, } from "node:fs";
13
+ import { dirname, join, relative, sep } from "node:path";
14
+ // [import]
15
+ import { isTable } from "../util/objs.js";
16
+ // [declare]
17
+ export const SCOPES = [
18
+ "developer",
19
+ "game",
20
+ "player",
21
+ "server",
22
+ ];
23
+ // a key, and what one may start with.
24
+ const KEY = /^[A-Za-z0-9_./-]{1,128}$/u;
25
+ const PREFIX = /^[A-Za-z0-9_./-]{0,128}$/u;
26
+ // the limits, as Arcadible's (db/value/value_limit.sql), which
27
+ // test/unit/serve/values.test.mjs holds them to.
28
+ export const LIMITS = {
29
+ bytes: { other: 67108864, player: 1048576 },
30
+ callKeys: 32,
31
+ callSize: 262144,
32
+ keys: { other: 10000, player: 256 },
33
+ list: 100,
34
+ readSize: 1048576,
35
+ size: 65536,
36
+ };
37
+ // where the project keeps the developer's values, a file a value, and where
38
+ // arc keeps the rest, in its own folder in the project.
39
+ export const DEVELOPER_DIR = join(".arcadible-data", "values");
40
+ export const LOCAL_FILE = join(".arc", "values", "local.json");
41
+ // [function]
42
+ // a value's size as Arcadible's database keeps it: its JSON with a space
43
+ // after each colon and comma, which is how it counts a scope's bytes.
44
+ export function sizeOf(value) {
45
+ const text = function (each) {
46
+ if (Array.isArray(each)) {
47
+ return `[${each.map(text).join(", ")}]`;
48
+ }
49
+ if (isTable(each)) {
50
+ return `{${Object.entries(each)
51
+ .map(([name, held]) => `${JSON.stringify(name)}: ${text(held)}`)
52
+ .join(", ")}}`;
53
+ }
54
+ return each === undefined ? "null" : JSON.stringify(each);
55
+ };
56
+ return Buffer.byteLength(text(value));
57
+ }
58
+ // [function]
59
+ // a call's keys, or null: at least one, no more than a call takes, each a
60
+ // key, none twice.
61
+ function keysOf(keys) {
62
+ if (!Array.isArray(keys) ||
63
+ keys.length < 1 ||
64
+ keys.length > LIMITS.callKeys ||
65
+ !keys.every((key) => typeof key === "string" && KEY.test(key)) ||
66
+ new Set(keys).size !== keys.length) {
67
+ return null;
68
+ }
69
+ return keys;
70
+ }
71
+ // [function]
72
+ // the developer's values, from the project's folder: each `.json` file a
73
+ // value, its path its key. A file that isn't JSON, or whose path isn't a
74
+ // key, is no value.
75
+ function developerRows(dir) {
76
+ const rows = new Map();
77
+ const walk = function (at) {
78
+ let names;
79
+ try {
80
+ names = readdirSync(at);
81
+ }
82
+ catch {
83
+ return;
84
+ }
85
+ for (const name of names.sort()) {
86
+ const file = join(at, name);
87
+ let stat;
88
+ try {
89
+ stat = statSync(file);
90
+ }
91
+ catch {
92
+ continue;
93
+ }
94
+ if (stat.isDirectory()) {
95
+ walk(file);
96
+ continue;
97
+ }
98
+ if (!name.endsWith(".json")) {
99
+ continue;
100
+ }
101
+ const key = relative(dir, file)
102
+ .slice(0, -".json".length)
103
+ .split(sep)
104
+ .join("/");
105
+ if (!KEY.test(key)) {
106
+ continue;
107
+ }
108
+ try {
109
+ const value = JSON.parse(readFileSync(file, "utf8"));
110
+ rows.set(key, {
111
+ key,
112
+ // a file's revision is when it was last written
113
+ revision: Math.floor(stat.mtimeMs),
114
+ size: sizeOf(value),
115
+ updated_at: stat.mtime.toISOString(),
116
+ value,
117
+ });
118
+ }
119
+ catch {
120
+ // not JSON yet: a file being written
121
+ }
122
+ }
123
+ };
124
+ walk(dir);
125
+ return rows;
126
+ }
127
+ // [function]
128
+ // arc serve's values for the project at `root`. `isPlayer(id)` says whether
129
+ // an id is a player's this run knows, as a ticket named them; `now()` is the
130
+ // time, in milliseconds.
131
+ export function values({ isPlayer, now = () => Date.now(), root, }) {
132
+ const file = join(root, LOCAL_FILE);
133
+ const dir = join(root, DEVELOPER_DIR);
134
+ // what's kept: each scope and owner's values by key, and the last revision
135
+ // given, which only grows.
136
+ let kept = {
137
+ revision: 0,
138
+ rows: {},
139
+ };
140
+ try {
141
+ const read = JSON.parse(readFileSync(file, "utf8"));
142
+ if (isTable(read) &&
143
+ typeof read["revision"] === "number" &&
144
+ isTable(read["rows"])) {
145
+ kept = read;
146
+ }
147
+ }
148
+ catch {
149
+ // none kept yet
150
+ }
151
+ const save = function () {
152
+ mkdirSync(dirname(file), { recursive: true });
153
+ writeFileSync(file, JSON.stringify(kept));
154
+ };
155
+ // a scope and owner's values: the developer's from the project's files,
156
+ // the rest from what's kept.
157
+ const held = function (scope, owner) {
158
+ return scope === "developer"
159
+ ? developerRows(dir)
160
+ : new Map(Object.entries(kept.rows[`${scope}:${owner}`] ?? {}));
161
+ };
162
+ // whether the owner is the scope's: a player this run knows, for a
163
+ // player's, as the database asks that a player is the game's.
164
+ const refusal = function (scope, owner) {
165
+ return scope === "player" && !isPlayer(owner) ? 403 : null;
166
+ };
167
+ return {
168
+ list(scope, owner, { after, limit, prefix }) {
169
+ const refused = refusal(scope, owner);
170
+ if (refused) {
171
+ return { data: null, status: refused };
172
+ }
173
+ if (typeof prefix !== "string" ||
174
+ !PREFIX.test(prefix) ||
175
+ typeof after !== "string" ||
176
+ !PREFIX.test(after) ||
177
+ typeof limit !== "number" ||
178
+ !Number.isInteger(limit) ||
179
+ limit < 1 ||
180
+ limit > LIMITS.list) {
181
+ return { data: null, status: 400 };
182
+ }
183
+ // by byte, as the database's keys sort
184
+ const keys = [...held(scope, owner).keys()]
185
+ .filter((key) => key.startsWith(prefix) &&
186
+ Buffer.compare(Buffer.from(key), Buffer.from(after)) > 0)
187
+ .sort((a, b) => Buffer.compare(Buffer.from(a), Buffer.from(b)));
188
+ const rows = held(scope, owner);
189
+ const entries = keys.slice(0, limit).flatMap((key) => {
190
+ const row = rows.get(key);
191
+ return row
192
+ ? [
193
+ {
194
+ key,
195
+ revision: row.revision,
196
+ size: row.size,
197
+ updated_at: row.updated_at,
198
+ },
199
+ ]
200
+ : [];
201
+ });
202
+ return {
203
+ data: {
204
+ entries,
205
+ next: keys.length > limit ? (entries.at(-1)?.key ?? null) : null,
206
+ },
207
+ status: 200,
208
+ };
209
+ },
210
+ read(scope, owner, asked) {
211
+ const refused = refusal(scope, owner);
212
+ if (refused) {
213
+ return { data: null, status: refused };
214
+ }
215
+ const keys = keysOf(asked);
216
+ if (!keys) {
217
+ return { data: null, status: 400 };
218
+ }
219
+ const rows = held(scope, owner);
220
+ // more than a read may hold together is the caller's to ask for less
221
+ if (keys.reduce((sum, key) => sum + (rows.get(key)?.size ?? 0), 0) >
222
+ LIMITS.readSize) {
223
+ return { data: null, status: 413 };
224
+ }
225
+ return {
226
+ data: keys.map((key) => {
227
+ const row = rows.get(key);
228
+ return row
229
+ ? {
230
+ key,
231
+ revision: row.revision,
232
+ updated_at: row.updated_at,
233
+ value: row.value,
234
+ }
235
+ : null;
236
+ }),
237
+ status: 200,
238
+ };
239
+ },
240
+ remove(scope, owner, asked) {
241
+ const refused = refusal(scope, owner);
242
+ if (refused) {
243
+ return { data: null, status: refused };
244
+ }
245
+ const keys = keysOf(asked);
246
+ if (!keys) {
247
+ return { data: null, status: 400 };
248
+ }
249
+ const rows = kept.rows[`${scope}:${owner}`] ?? {};
250
+ const there = keys.filter((key) => Object.hasOwn(rows, key));
251
+ for (const key of there) {
252
+ Reflect.deleteProperty(rows, key);
253
+ }
254
+ if (there.length) {
255
+ save();
256
+ }
257
+ return { data: { removed: there.length }, status: 200 };
258
+ },
259
+ write(scope, owner, given) {
260
+ const refused = refusal(scope, owner);
261
+ if (refused) {
262
+ return { data: null, status: refused };
263
+ }
264
+ if (!Array.isArray(given) ||
265
+ !given.every((item) => isTable(item) &&
266
+ Object.hasOwn(item, "value") &&
267
+ (item["revision"] === undefined ||
268
+ (typeof item["revision"] === "number" &&
269
+ Number.isSafeInteger(item["revision"]) &&
270
+ item["revision"] >= 0)))) {
271
+ return { data: null, status: 400 };
272
+ }
273
+ const items = given;
274
+ const keys = keysOf(items.map((item) => item.key));
275
+ if (!keys) {
276
+ return { data: null, status: 400 };
277
+ }
278
+ const sizes = items.map((item) => sizeOf(item.value));
279
+ if (sizes.some((size) => size > LIMITS.size) ||
280
+ sizes.reduce((sum, size) => sum + size, 0) > LIMITS.callSize) {
281
+ return { data: null, status: 413 };
282
+ }
283
+ const name = `${scope}:${owner}`;
284
+ const rows = { ...(kept.rows[name] ?? {}) };
285
+ // every named revision first, so a call writes all or none
286
+ for (const [at, item] of items.entries()) {
287
+ const revision = rows[keys[at] ?? ""]?.revision ?? 0;
288
+ if (item.revision !== undefined && item.revision !== revision) {
289
+ return { data: null, status: 409 };
290
+ }
291
+ }
292
+ let revision = kept.revision;
293
+ const at = new Date(now()).toISOString();
294
+ const written = items.map((item, index) => {
295
+ const key = keys[index] ?? "";
296
+ revision += 1;
297
+ rows[key] = {
298
+ key,
299
+ revision,
300
+ size: sizes[index] ?? 0,
301
+ updated_at: at,
302
+ value: item.value,
303
+ };
304
+ return { key, revision, updated_at: at };
305
+ });
306
+ const limits = scope === "player" ? "player" : "other";
307
+ const all = Object.values(rows);
308
+ if (all.length > LIMITS.keys[limits] ||
309
+ all.reduce((sum, row) => sum + row.size, 0) > LIMITS.bytes[limits]) {
310
+ return { data: null, status: 507 };
311
+ }
312
+ kept = { revision, rows: { ...kept.rows, [name]: rows } };
313
+ save();
314
+ return { data: written, status: 200 };
315
+ },
316
+ };
317
+ }
package/dist/util/objs.js CHANGED
@@ -27,3 +27,20 @@ export function deepSort(obj) {
27
27
  const sorted = JSON.parse(stringify(obj));
28
28
  return sorted;
29
29
  }
30
+ // [function]
31
+ // a value as text, for one that's read as it was written: a manifest that
32
+ // isn't checked, or `.arcrc`, can hold anything where text is meant. Text is
33
+ // itself, a number or a yes or no as it reads, and anything else as JSON,
34
+ // never "[object Object]".
35
+ export function textOf(value) {
36
+ if (typeof value === "string") {
37
+ return value;
38
+ }
39
+ if (typeof value === "number" ||
40
+ typeof value === "boolean" ||
41
+ typeof value === "bigint") {
42
+ return String(value);
43
+ }
44
+ // nothing, for a value left out: JSON has no word for it
45
+ return value === undefined ? "" : JSON.stringify(value);
46
+ }
@@ -16,8 +16,20 @@ function formatValue(value, _opts, type) {
16
16
  return formatBytes(value);
17
17
  }
18
18
  // [function]
19
+ // the bar's width: what the terminal leaves beside the text at its widest
20
+ // (100%, and sizes such as 999.9 MB), from 10 to 40, so the numbers always
21
+ // show in a narrow terminal rather than being cut off at its edge.
22
+ export function barSize(format, columns = process.stderr.columns || 80) {
23
+ const text = format
24
+ .replace("{bar}", "")
25
+ .replace("{percentage}", "100")
26
+ .replace("{value}", "999.9 MB")
27
+ .replace("{total}", "999.9 MB");
28
+ return Math.max(10, Math.min(40, columns - text.length - 1));
29
+ }
30
+ // [function]
19
31
  function singleBar(format) {
20
- return new SingleBar({ format, formatValue, stream: process.stderr }, Presets.shades_classic);
32
+ return new SingleBar({ barsize: barSize(format), format, formatValue, stream: process.stderr }, Presets.shades_classic);
21
33
  }
22
34
  // [class]
23
35
  export class ProgressBar {
@@ -54,8 +54,9 @@ function parse(resp) {
54
54
  }
55
55
  // [function]
56
56
  // sends a request to a full URL. `headers` are added to the client's own, and
57
- // a form is sent as JSON. A request that gets no response, such as when
58
- // offline, rejects as api_unreachable.
57
+ // a form is sent as JSON, its fields as text; `{ json }` is sent as the JSON
58
+ // it is. A request that gets no response, such as when offline, rejects as
59
+ // api_unreachable.
59
60
  function send(method, url, form, headers = {}) {
60
61
  const all = {
61
62
  [`X-${CLIENT}-Client-Id`]: ID,
@@ -63,8 +64,13 @@ function send(method, url, form, headers = {}) {
63
64
  ...(form ? { "Content-Type": "application/json" } : {}),
64
65
  ...headers,
65
66
  };
67
+ const body = !form
68
+ ? null
69
+ : form instanceof FormData
70
+ ? JSON.stringify(Object.fromEntries(form))
71
+ : JSON.stringify(form.json);
66
72
  return fetch(url, {
67
- body: form ? JSON.stringify(Object.fromEntries(form)) : null,
73
+ body,
68
74
  headers: all,
69
75
  method,
70
76
  }).then(parse, (ex) => {
@@ -82,6 +88,12 @@ export function post(url, form, headers) {
82
88
  return send("POST", url, form, headers);
83
89
  }
84
90
  // [function]
91
+ // a POST whose body is JSON as it is: nested, and with values that aren't
92
+ // text, which a form's fields can't hold.
93
+ export function postJson(url, json, headers) {
94
+ return send("POST", url, { json }, headers);
95
+ }
96
+ // [function]
85
97
  export function put(url, form, headers) {
86
98
  return send("PUT", url, form, headers);
87
99
  }
@@ -69,7 +69,7 @@ function kindOf(type) {
69
69
  // options), in arc's words, as `{ message, details }`: "Must be a string, not
70
70
  // a number." Null for a message of our own.
71
71
  function zodMessage(issue) {
72
- if (issue.code === "invalid_value" && issue.values) {
72
+ if (issue.code === "invalid_value") {
73
73
  return { details: [issue.values.join(", ")], message: "Must be one of:" };
74
74
  }
75
75
  const given = Array.isArray(issue.input) ? "array" : typeof issue.input;
@@ -0,0 +1,49 @@
1
+ // what an answer must hold to be read: a check a field, for the fields the
2
+ // CLI reads, and nothing said of the rest, so the API may add to an answer
3
+ // and an installed CLI still reads it. A shape names every field of its
4
+ // type, so the type and the check can't come apart.
5
+ // [function]
6
+ export function str(value) {
7
+ return typeof value === "string";
8
+ }
9
+ // [function]
10
+ export function num(value) {
11
+ return typeof value === "number";
12
+ }
13
+ // [function]
14
+ // anything: a field the CLI passes on, and never reads into.
15
+ export function any() {
16
+ return true;
17
+ }
18
+ // [function]
19
+ // a field that may be left out.
20
+ export function opt(check) {
21
+ return (value) => value === undefined || check(value);
22
+ }
23
+ // [function]
24
+ // a field that may be null.
25
+ export function nul(check) {
26
+ return (value) => value === null || check(value);
27
+ }
28
+ // [function]
29
+ // a field of either kind.
30
+ export function either(a, b) {
31
+ return (value) => a(value) || b(value);
32
+ }
33
+ // [function]
34
+ // whether `value` is a `T`: an object whose fields each pass their check.
35
+ export function shaped(shape) {
36
+ const checks = Object.entries(shape);
37
+ return (value) => {
38
+ if (value === null || typeof value !== "object" || Array.isArray(value)) {
39
+ return false;
40
+ }
41
+ const held = value;
42
+ return checks.every(([name, check]) => check(held[name]));
43
+ };
44
+ }
45
+ // [function]
46
+ // whether `value` is a list of `T`, or null, as the API answers with none.
47
+ export function listed(item) {
48
+ return (value) => value === null || (Array.isArray(value) && value.every(item));
49
+ }
@@ -43,7 +43,7 @@
43
43
  },
44
44
  "license": "Apache-2.0",
45
45
  "name": "arcadible",
46
- "version": "1.0.24"
46
+ "version": "1.0.26"
47
47
  },
48
48
  "node_modules/@asamuzakjp/css-color": {
49
49
  "dependencies": {
@@ -3252,5 +3252,5 @@
3252
3252
  }
3253
3253
  },
3254
3254
  "requires": true,
3255
- "version": "1.0.24"
3255
+ "version": "1.0.26"
3256
3256
  }
package/package.json CHANGED
@@ -67,5 +67,5 @@
67
67
  "test": "tsc && npm run lint && node --test \"test/**/*.test.js\" \"test/**/*.test.mjs\""
68
68
  },
69
69
  "type": "module",
70
- "version": "1.0.24"
70
+ "version": "1.0.26"
71
71
  }