arcadible 1.0.37 → 1.0.38

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.
@@ -26,10 +26,15 @@ export const FUNCTION_MODULE = "function.js";
26
26
  // arrive as a header too (x-arcadible-secrets, opened by the dispatcher),
27
27
  // and are added to what the code is given, `env`. Neither header reaches
28
28
  // the developer's code. Under arc serve they're the script's own bindings,
29
- // named by ARCADIBLE_SECRETS. Whatever the code logs has the secrets' values
30
- // it was given hidden, `[secret NAME]`, and an error it throws that holds
31
- // one is logged hidden and thrown as one that doesn't, so a function's logs
32
- // never keep a secret (plan/FUNCTION-LOGS.md, What's redacted).
29
+ // named by ARCADIBLE_SECRETS, which the code isn't given, nor ARCADIBLE_SERVE,
30
+ // as on Arcadible. Whatever the code logs has the secrets' values it was
31
+ // given hidden, `[secret NAME]`, as they are and as JSON escapes them, and an
32
+ // error that holds one, thrown by the handler, by work it handed on
33
+ // (waitUntil), by its response's body as it's sent, or by what no one
34
+ // awaited, is logged hidden and given on as one that doesn't. What's hidden
35
+ // is a secret as the code logs or throws it, in plain text: one encoded,
36
+ // split, or put into an error's message after it was made isn't found
37
+ // (plan/FUNCTION-LOGS.md, What's redacted).
33
38
  export const ENTRY = `import handler from "./${FUNCTION_MODULE}";
34
39
  const CLAIM = "x-arcadible-claim";
35
40
  const SECRETS = "x-arcadible-secrets";
@@ -43,13 +48,17 @@ let serve = null;
43
48
  // the secrets' values the code has been given, each with its name, longest
44
49
  // first, so one inside another is hidden as the longer
45
50
  const hidden = [];
51
+ // each value as it is, and as it reads inside JSON, escaped (a quote, a
52
+ // backslash, a newline), as a value logged inside an object is
46
53
  const remember = (values) => {
47
54
  let added = false;
48
55
  for (const [name, value] of Object.entries(values)) {
49
56
  if (typeof value !== "string" || value.length < SHORTEST) continue;
50
- if (hidden.some(([known]) => known === value)) continue;
51
- hidden.push([value, name]);
52
- added = true;
57
+ for (const form of new Set([value, JSON.stringify(value).slice(1, -1)])) {
58
+ if (hidden.some(([known]) => known === form)) continue;
59
+ hidden.push([form, name]);
60
+ added = true;
61
+ }
53
62
  }
54
63
  if (added) hidden.sort((a, b) => b[0].length - a[0].length);
55
64
  };
@@ -59,24 +68,108 @@ const hide = (text) => {
59
68
  };
60
69
  // one of console's arguments with any secret in it hidden: a string as it
61
70
  // is, an error as its stack, anything else as JSON, and as the console
62
- // would show it when no secret's in it
71
+ // would show it when no secret's in it. A value JSON can't hold (a BigInt)
72
+ // is shown as its text, hidden, since what's inside it can't be looked at.
63
73
  const shown = (value) => {
64
74
  if (typeof value === "string") return hide(value);
65
75
  let text;
66
76
  try {
67
77
  text = value instanceof Error ? String(value.stack ?? value) : JSON.stringify(value);
68
78
  } catch {
69
- return value;
79
+ try {
80
+ return hide(String(value));
81
+ } catch {
82
+ return "[unloggable]";
83
+ }
70
84
  }
71
85
  if (typeof text !== "string") return value;
72
86
  const safe = hide(text);
73
87
  return safe === text ? value : safe;
74
88
  };
75
- for (const level of ["debug", "error", "info", "log", "trace", "warn"]) {
89
+ // whether an error, or anything thrown, holds a secret
90
+ const holds = (err) => {
91
+ if (hidden.length === 0) return false;
92
+ let text;
93
+ try {
94
+ text = String(err?.stack ?? err);
95
+ } catch {
96
+ return false;
97
+ }
98
+ return hide(text) !== text;
99
+ };
100
+ // an error that held a secret: logged with it hidden, and given on in place
101
+ // of the error, so neither the logs nor what's thrown keep it
102
+ const holding = (err) => {
103
+ console.error(err);
104
+ return new Error("The function threw an error holding a secret: it's logged with the secret hidden.");
105
+ };
106
+ // every method of console's that prints what it's given (assert, its
107
+ // arguments after the condition)
108
+ for (const level of ["assert", "count", "debug", "dir", "error", "group", "groupCollapsed", "info", "log", "table", "timeLog", "trace", "warn"]) {
76
109
  const original = console[level];
77
110
  if (typeof original !== "function") continue;
78
- console[level] = (...args) => original.apply(console, hidden.length > 0 ? args.map(shown) : args);
111
+ console[level] =
112
+ level === "assert"
113
+ ? (condition, ...args) => original.call(console, condition, ...(hidden.length > 0 ? args.map(shown) : args))
114
+ : (...args) => original.apply(console, hidden.length > 0 ? args.map(shown) : args);
79
115
  }
116
+ // what fails outside the awaited handler (a promise no one waits for, a
117
+ // timer): logged with any secret hidden, in place of the runtime's own
118
+ // report of it
119
+ if (typeof addEventListener === "function") {
120
+ addEventListener("unhandledrejection", (event) => {
121
+ if (!holds(event.reason)) return;
122
+ event.preventDefault?.();
123
+ console.error(event.reason);
124
+ });
125
+ addEventListener("error", (event) => {
126
+ if (!holds(event.error)) return;
127
+ event.preventDefault?.();
128
+ console.error(event.error);
129
+ });
130
+ }
131
+ // the work the code hands on (ctx.waitUntil), whose failure holding a
132
+ // secret is given on as one that doesn't; the rest of ctx as it is, its
133
+ // methods called on ctx itself
134
+ const wrapped = (ctx) => {
135
+ if (!ctx || typeof ctx.waitUntil !== "function") return ctx;
136
+ const waitUntil = (promise) =>
137
+ ctx.waitUntil(
138
+ Promise.resolve(promise).catch((err) => {
139
+ throw holds(err) ? holding(err) : err;
140
+ }),
141
+ );
142
+ return new Proxy(ctx, {
143
+ get(target, key) {
144
+ if (key === "waitUntil") return waitUntil;
145
+ const value = target[key];
146
+ return typeof value === "function" ? value.bind(target) : value;
147
+ },
148
+ });
149
+ };
150
+ // a response whose body fails as it's sent, with a secret in its error,
151
+ // fails with one that doesn't hold it
152
+ const streamed = (response) => {
153
+ if (hidden.length === 0 || !(response instanceof Response) || !response.body || response.status === 101 || response.webSocket) {
154
+ return response;
155
+ }
156
+ const reader = response.body.getReader();
157
+ const body = new ReadableStream({
158
+ async pull(controller) {
159
+ try {
160
+ const { done, value } = await reader.read();
161
+ if (done) controller.close();
162
+ else controller.enqueue(value);
163
+ } catch (err) {
164
+ controller.error(holds(err) ? holding(err) : err);
165
+ }
166
+ },
167
+ cancel(reason) {
168
+ return reader.cancel(reason);
169
+ },
170
+ });
171
+ return new Response(body, response);
172
+ };
80
173
  globalThis[Symbol.for("arcadible.function.api")] = {
81
174
  version: 1,
82
175
  async fetch(path, init = {}) {
@@ -108,13 +201,19 @@ export default {
108
201
  } else {
109
202
  delete set.ARCADIBLE_CLAIM;
110
203
  }
204
+ // what the code is given: the script's bindings, but the entry's own
205
+ // two under arc serve, which Arcadible's env doesn't have
111
206
  let given = env;
207
+ if ("ARCADIBLE_SERVE" in env || "ARCADIBLE_SECRETS" in env) {
208
+ const { ARCADIBLE_SERVE: _serve, ARCADIBLE_SECRETS: _secrets, ...rest } = env;
209
+ given = rest;
210
+ }
112
211
  if (secrets) {
113
212
  const text = atob(secrets.replace(/-/g, "+").replace(/_/g, "/"));
114
213
  const bytes = Uint8Array.from(text, (char) => char.charCodeAt(0));
115
214
  const opened = JSON.parse(new TextDecoder().decode(bytes));
116
215
  remember(opened);
117
- given = { ...env, ...opened };
216
+ given = { ...given, ...opened };
118
217
  }
119
218
  if (typeof env.ARCADIBLE_SECRETS === "string") {
120
219
  remember(Object.fromEntries(env.ARCADIBLE_SECRETS.split(",").map((name) => [name, env[name]])));
@@ -129,12 +228,9 @@ export default {
129
228
  return new Response("The function's default export has no fetch(request).\\n", { status: 500 });
130
229
  }
131
230
  try {
132
- return await handler.fetch(request, given, ctx);
231
+ return streamed(await handler.fetch(request, given, wrapped(ctx)));
133
232
  } catch (err) {
134
- const text = String(err?.stack ?? err);
135
- if (hidden.length === 0 || hide(text) === text) throw err;
136
- console.error(err);
137
- throw new Error("The function threw an error holding a secret: it's logged with the secret hidden.");
233
+ throw holds(err) ? holding(err) : err;
138
234
  }
139
235
  },
140
236
  };
@@ -1,4 +1,4 @@
1
- // A runner's way out: every request a game's runner makes of its own goes
1
+ // A runner's way out: every request a game's function makes of its own goes
2
2
  // through here first (plan/SERVERS.md, The runner to Arcadible, and beyond),
3
3
  // and reaches only the hosts its manifest declares (function.outbound_hosts), so a
4
4
  // runner can't be turned on other sites. On Arcadible it's the rule the
@@ -28,18 +28,31 @@ export function allowed(url, hosts) {
28
28
  return (to.protocol === "https:" && to.port === "" && hosts.includes(to.hostname));
29
29
  }
30
30
  // [function]
31
- /** The answer to a request that isn't allowed, which says why. */
31
+ /** Whether `name` is one of Arcadible's own, which no manifest declares. */
32
+ export function isArcadible(name) {
33
+ return ["arcadible.com", "arcadible.io"].some((domain) => name === domain || name.endsWith(`.${domain}`));
34
+ }
35
+ // [function]
36
+ /**
37
+ * The answer to a request that isn't allowed, which says why: Arcadible's
38
+ * own addresses can't be declared, and are reached through the Function
39
+ * SDK; any other, by declaring it.
40
+ */
32
41
  export function refuse(url) {
33
42
  let host = "that address";
43
+ let name = "";
34
44
  try {
35
- host = new URL(url).host;
45
+ ({ host, hostname: name } = new URL(url));
36
46
  }
37
47
  catch {
38
48
  // said as it is.
39
49
  }
50
+ const arcadible = isArcadible(name);
40
51
  return Response.json({
41
52
  code: CODE,
42
- message: `The function may not call ${host}. To allow it, add it to function.outbound_hosts in arcadible.toml, as a name reached over HTTPS.`,
53
+ message: arcadible
54
+ ? `The function may not call ${host} itself. Arcadible is reached through the Function SDK (@arcadible/function-sdk), which needs no host declared.`
55
+ : `The function may not call ${host}. To allow it, add it to function.outbound_hosts in arcadible.toml, as a name reached over HTTPS.`,
43
56
  }, { status: STATUS });
44
57
  }
45
58
  // [function]
@@ -61,7 +74,10 @@ export default {
61
74
  if (new URL(request.url).origin !== own &&
62
75
  !allowed(request.url, declared(env.HOSTS))) {
63
76
  const refused = refuse(request.url);
64
- console.warn(`Function: refused ${request.method} ${request.url} (not in function.outbound_hosts)`);
77
+ const why = isArcadible(new URL(request.url).hostname)
78
+ ? "Arcadible's, reached through the Function SDK"
79
+ : "not in function.outbound_hosts";
80
+ console.warn(`Function: refused ${request.method} ${request.url} (${why})`);
65
81
  return refused;
66
82
  }
67
83
  return fetch(request, { redirect: "manual" });
@@ -266,18 +266,6 @@ function zArtVideo(field, root) {
266
266
  return z.optional(zMp4(`${field}_video`, root, { ...ART[field], maxSize: MAX_SIZE_MP4 }));
267
267
  }
268
268
  // [function]
269
- function zVimeo() {
270
- return z
271
- .string()
272
- .regex(/^https:\/\/(?:www\.)?(?:vimeo\.com|player\.vimeo\.com\/video)\/([0-9]+)(?:[/?#].*)?$/iu);
273
- }
274
- // [function]
275
- function zYouTube() {
276
- return z
277
- .string()
278
- .regex(/^https:\/\/(?:www\.)?(?:youtube\.com\/(?:watch\?v=|embed\/)|youtu\.be\/)([A-Za-z0-9_-]{11})(?:[?&#].*)?$/iu);
279
- }
280
- // [function]
281
269
  function zAi() {
282
270
  return z.optional(z.enum(AI));
283
271
  }
@@ -376,14 +364,6 @@ function zOrientation() {
376
364
  return z.optional(z.enum(ORIENTATION));
377
365
  }
378
366
  // [function]
379
- function zPrice() {
380
- const field = "price";
381
- return z.optional(z
382
- .int()
383
- .min(0, message(field, ["Must be 0 or more."]))
384
- .max(25, message(field, ["Must be 25 or less."])));
385
- }
386
- // [function]
387
367
  function zShortTitle() {
388
368
  const field = "short_title";
389
369
  return z.optional(zText(field, { max: 30 / 2, min: 1 }));
@@ -399,16 +379,6 @@ function zTitle() {
399
379
  return z.optional(zText(field, { max: 30 * 1, min: 1 }));
400
380
  }
401
381
  // [function]
402
- function zTrailer() {
403
- const field = "trailer";
404
- return z
405
- .string()
406
- .min(1, { ...atLeast(field, 1), abort: true })
407
- .max(255, message(field, ["Must be at most 255 characters."]))
408
- .refine((url) => zVimeo().safeParse(url).success || zYouTube().safeParse(url).success, message(field, ["Must be a YouTube or Vimeo URL."], ISSUE.URL_INVALID))
409
- .optional();
410
- }
411
- // [function]
412
382
  function zVersion() {
413
383
  const field = "version";
414
384
  return z
@@ -449,13 +419,11 @@ export function game(_value, { root }) {
449
419
  orientation: zOrientation(),
450
420
  poster: zArt("poster", root),
451
421
  poster_video: zArtVideo("poster", root),
452
- price: zPrice(),
453
422
  short_title: zShortTitle(),
454
423
  splash: zArt("splash", root),
455
424
  splash_video: zArtVideo("splash", root),
456
425
  tagline: zTagline(),
457
426
  title: zTitle(),
458
- trailer: zTrailer(),
459
427
  version: zVersion(),
460
428
  visibility: zVisibility(),
461
429
  });
@@ -7,10 +7,9 @@ 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: `price`, until paid games do (ONE.md), and a game's
11
- // function and its own servers, until they do (plan/SERVERS.md).
10
+ // issues have no link: a game's function and its own servers, until they do
11
+ // (plan/SERVERS.md).
12
12
  export const UNDOCUMENTED = new Set([
13
- "game.price",
14
13
  "function.main",
15
14
  "function.outbound_hosts",
16
15
  "function.secrets",
@@ -2,6 +2,7 @@
2
2
  import * as z from "zod";
3
3
  // [import]
4
4
  import { ISSUE } from "../config/code.js";
5
+ import { SECRET_NAME, SECRETS } from "../config/limit.js";
5
6
  import { zHosts } from "./host.js";
6
7
  import { messages } from "./message.js";
7
8
  // [declare]
@@ -10,16 +11,15 @@ const message = messages("function");
10
11
  const REQUIRED = message("main", [
11
12
  "Must be set: the path of the function's code, such as function/index.ts.",
12
13
  ]).message;
13
- // a secret's name, as an environment variable is named.
14
- const SECRET = /^[A-Z][A-Z0-9_]*$/u;
15
14
  // [function]
16
- // a list of distinct strings, each matching `pattern`.
17
- function zNames(field, pattern, line) {
15
+ // a list of distinct strings, each matching `pattern`, `max` at most.
16
+ function zNames(field, pattern, lines, max) {
18
17
  return z.optional(z
19
18
  .array(z
20
19
  .string()
21
- .regex(pattern, message(field, [line], ISSUE.CHARACTERS_INVALID)))
20
+ .regex(pattern, message(field, lines, ISSUE.CHARACTERS_INVALID)))
22
21
  .min(1, message(field, ["Must not be empty.", "Leave the field out instead."]))
22
+ .max(max, message(field, [`Must have at most ${max} items.`]))
23
23
  .refine((items) => new Set(items).size === items.length, message(field, ["Must not repeat an item."], ISSUE.DUPLICATE)));
24
24
  }
25
25
  // [function]
@@ -38,6 +38,8 @@ export function runner() {
38
38
  .min(1, message("main", ["Must not be empty."]))
39
39
  .refine((file) => !file.startsWith("/") && !file.split(/[/\\]/u).includes(".."), message("main", ["Must be a path within the project."], ISSUE.PATH_INVALID)),
40
40
  outbound_hosts: zHosts(message, "outbound_hosts", "a host's name"),
41
- secrets: zNames("secrets", SECRET, "Must be a name in capitals, such as EXAMPLE_KEY."),
41
+ secrets: zNames("secrets", SECRET_NAME, [
42
+ "Must be a name in capitals, such as EXAMPLE_KEY: letters, digits, and underscores, up to 64 characters, starting with a letter, and not with ARCADIBLE_.",
43
+ ], SECRETS),
42
44
  });
43
45
  }
@@ -215,7 +215,7 @@ export function html(game, pictures = { art: false, icon: false }, opened, link)
215
215
  // who the game runs for: a local player (`arc serve --player`), or null,
216
216
  // a guest.
217
217
  player: opened ? opened.player : (game.player ?? null),
218
- // where the game's runner is, when the project has one
218
+ // where the game's function is, when the project has one
219
219
  runner: game.runner?.url ?? null,
220
220
  sandbox: SANDBOX,
221
221
  src: game.src,
@@ -2,7 +2,11 @@
2
2
  // them, so a fix reaches `arc serve` without a new CLI: the SDK's hosted
3
3
  // script, as every game's origin serves it at /arcadible/sdk/, and the play
4
4
  // page's host, as the play page runs it. Each is fetched once a session, and
5
- // the last copy is kept on disk for when Arcadible can't be reached.
5
+ // the last copy is kept on disk for when Arcadible can't be reached, or
6
+ // answers 403 or 404 for a file that's always there: the names this module
7
+ // knows are at fixed addresses, so a refusal of one is Arcadible's fault
8
+ // (the router answered sdk.arcadible.io with 404 on 2026-11-19), and the
9
+ // game keeps its SDK.
6
10
  // [import]
7
11
  import { mkdir, readFile, writeFile } from "node:fs/promises";
8
12
  import { join } from "node:path";
@@ -56,10 +60,12 @@ async function keep(dir, name, { body, ...meta }) {
56
60
  // [function]
57
61
  // the files from Arcadible. `host` is the play site's origin, `sdk` an
58
62
  // origin that serves /arcadible/sdk/, and `dir` where copies are kept.
59
- // `get(name)` resolves with `{ body, type, fetchedAt, kept }`, where `kept`
60
- // says the copy is from disk because Arcadible couldn't be reached, or null
61
- // when Arcadible has no such file; it rejects when Arcadible can't be reached
62
- // and there's no copy. `name` is `host`, or a path under /arcadible/.
63
+ // `get(name)` resolves with `{ body, type, fetchedAt, kept, refused? }`,
64
+ // where `kept` says the copy is from disk because Arcadible couldn't be
65
+ // reached, or answered `refused` (403 or 404); or null when Arcadible has
66
+ // no such file and there's no copy; it rejects when Arcadible can't be
67
+ // reached and there's no copy. `name` is `host`, or a path under
68
+ // /arcadible/: a name this module doesn't know is never asked for.
63
69
  export function remoteFiles({ dir, host, request = fetch, sdk, }) {
64
70
  const session = new Map();
65
71
  const url = function (name) {
@@ -77,7 +83,9 @@ export function remoteFiles({ dir, host, request = fetch, sdk, }) {
77
83
  try {
78
84
  const res = await request(target);
79
85
  if (res.status === 403 || res.status === 404) {
80
- return null;
86
+ await res.body?.cancel();
87
+ const file = await readKept(dir, kept);
88
+ return file ? { ...file, kept: true, refused: res.status } : null;
81
89
  }
82
90
  if (!res.ok) {
83
91
  throw new Error(`${res.status}`);
@@ -6,18 +6,27 @@
6
6
  // read only when a project has a [function] table.
7
7
  // [import]
8
8
  import cp, {} from "node:child_process";
9
+ import fs from "node:fs";
9
10
  import { copyFile, mkdir, mkdtemp, readFile, rm, writeFile, } from "node:fs/promises";
10
11
  import http from "node:http";
11
12
  import net from "node:net";
12
13
  import os from "node:os";
13
- import { join } from "node:path";
14
+ import { dirname, join } from "node:path";
14
15
  // [import]
15
16
  import { bundle } from "../runner/bundle.js";
16
17
  import { ENTRY, ENTRY_MODULE, FUNCTION_MODULE } from "../runner/entry.js";
17
18
  // [declare]
18
19
  // where a runner answers on the game's origin, as on Arcadible, where the
19
- // folder is Arcadible's own on every build's site.
20
- export const PREFIX = "/arcadible/runner";
20
+ // folder is Arcadible's own on every build's site: the path itself, and
21
+ // anything under it.
22
+ export const PREFIX = "/arcadible/function";
23
+ // the function's own path for a request under PREFIX, as the dispatcher
24
+ // passes it on: `/arcadible/function/move` is `/move`, and the path itself
25
+ // is `/`, its query kept.
26
+ export function innerPath(url) {
27
+ const rest = url.slice(PREFIX.length);
28
+ return rest.startsWith("/") ? rest : `/${rest}`;
29
+ }
21
30
  // the header a runner reads who's asking from (the Server SDK's
22
31
  // players.primary): the player's id, or empty for a guest. Whatever a request
23
32
  // brings by that name is dropped.
@@ -268,8 +277,66 @@ async function bodyOf(req) {
268
277
  }
269
278
  return Buffer.concat(parts);
270
279
  }
280
+ // [declare]
281
+ // how long the code's files must stop changing before it's run again, as
282
+ // deploy_dir's reload waits (./reload.ts).
283
+ const SETTLE = 100;
271
284
  // [function]
272
- // a game's runner, running. `main` is its code's path, `dir` where the
285
+ // watches the files a function's bundle was made from, wherever they are
286
+ // (`main`'s folder and beyond: a game's rules its function imports), and
287
+ // calls `onChange` once they've stopped changing. `files(paths)` sets what's
288
+ // watched, after each bundle; until the first, `main` alone. Each folder
289
+ // holding one is watched, not recursively, and only its own files count.
290
+ export function watchInputs(main, onChange) {
291
+ let inputs = new Set([main]);
292
+ let timer;
293
+ const watchers = new Map();
294
+ const sync = function () {
295
+ const dirs = new Set([...inputs].map((path) => dirname(path)));
296
+ for (const [dir, watcher] of watchers) {
297
+ if (!dirs.has(dir)) {
298
+ watcher.close();
299
+ watchers.delete(dir);
300
+ }
301
+ }
302
+ for (const dir of dirs) {
303
+ if (watchers.has(dir))
304
+ continue;
305
+ try {
306
+ const watcher = fs.watch(dir, (_event, name) => {
307
+ if (name !== null && !inputs.has(join(dir, name)))
308
+ return;
309
+ clearTimeout(timer);
310
+ timer = setTimeout(onChange, SETTLE);
311
+ });
312
+ // a folder removed while it's watched: stop watching it
313
+ watcher.on("error", () => {
314
+ watcher.close();
315
+ watchers.delete(dir);
316
+ });
317
+ watchers.set(dir, watcher);
318
+ }
319
+ catch {
320
+ // a folder that isn't there (yet) isn't watched
321
+ }
322
+ }
323
+ };
324
+ sync();
325
+ return {
326
+ close() {
327
+ clearTimeout(timer);
328
+ for (const watcher of watchers.values())
329
+ watcher.close();
330
+ watchers.clear();
331
+ },
332
+ files(paths) {
333
+ inputs = new Set([main, ...paths]);
334
+ sync();
335
+ },
336
+ };
337
+ }
338
+ // [function]
339
+ // a game's function, running. `main` is its code's path, `dir` where the
273
340
  // runtime is kept, `origin` the game's own origin (a ticket for the runner
274
341
  // is for it), `serve` arc serve's page (where Arcadible is, here), `hosts`
275
342
  // the hosts its manifest declares, the only ones it may call, `secrets` the
@@ -282,6 +349,12 @@ export async function runner({ dir, handler, hosts = [], main, origin, secrets =
282
349
  let child = null;
283
350
  let port = null;
284
351
  let failed = null;
352
+ // what the bundle was made from, watched: a change to any of it runs the
353
+ // new code, set after each bundle that builds (one that doesn't keeps
354
+ // what was watched, so fixing it runs it)
355
+ const watched = watchInputs(main, () => {
356
+ void start();
357
+ });
285
358
  const stop = function () {
286
359
  if (child) {
287
360
  child.kill();
@@ -296,7 +369,13 @@ export async function runner({ dir, handler, hosts = [], main, origin, secrets =
296
369
  const began = Date.now();
297
370
  // the developer's code, and Arcadible's entry beside it, where the
298
371
  // script starts, as the pipeline uploads a build's
299
- await bundle({ main, outfile: join(work, FUNCTION_MODULE) });
372
+ await bundle({
373
+ main,
374
+ onInputs: (paths) => {
375
+ watched.files(paths);
376
+ },
377
+ outfile: join(work, FUNCTION_MODULE),
378
+ });
300
379
  await writeFile(join(work, ENTRY_MODULE), ENTRY);
301
380
  const free = await freePort();
302
381
  port = free;
@@ -372,6 +451,9 @@ export async function runner({ dir, handler, hosts = [], main, origin, secrets =
372
451
  ...Object.fromEntries(Object.entries(req.headers).filter(([name]) => !dropped.includes(name))),
373
452
  [PLAYER]: typeof player === "string" ? player : "",
374
453
  [GUEST]: typeof guest === "string" ? guest : "",
454
+ // the game's origin, as on Arcadible, where the code's request.url is
455
+ // its site's: never the runtime's own port
456
+ host: new URL(origin).host,
375
457
  };
376
458
  if (body !== true) {
377
459
  delete headers["transfer-encoding"];
@@ -382,7 +464,7 @@ export async function runner({ dir, handler, hosts = [], main, origin, secrets =
382
464
  headers,
383
465
  host: "127.0.0.1",
384
466
  method: req.method,
385
- path: (req.url ?? "").slice(PREFIX.length) || "/",
467
+ path: innerPath(req.url ?? ""),
386
468
  port,
387
469
  }, (answer) => {
388
470
  res.writeHead(answer.statusCode ?? 502, { ...answer.headers, ...CORS });
@@ -397,6 +479,7 @@ export async function runner({ dir, handler, hosts = [], main, origin, secrets =
397
479
  await start();
398
480
  return {
399
481
  close() {
482
+ watched.close();
400
483
  stop();
401
484
  rm(work, { force: true, recursive: true }).catch(() => null);
402
485
  },
@@ -15,7 +15,7 @@ import { PREFIX as WIRES, wires as wiresFor } from "./wires.js";
15
15
  // [declare]
16
16
  // the hostnames a browser may use for the page, on IPv4 and IPv6. `localhost`
17
17
  // is IPv6's ::1 on some systems, so both are listened on.
18
- const LOOPBACK = ["127.0.0.1", "::1"];
18
+ export const LOOPBACK = ["127.0.0.1", "::1"];
19
19
  // [function]
20
20
  // listens on `port` at `address`, resolving with the server, or rejecting with
21
21
  // the listen error, such as EADDRINUSE.
@@ -64,7 +64,7 @@ export async function serve({ art, files, hostFile = null, icon, info, keyFile,
64
64
  if (tickets) {
65
65
  tickets.remember(player);
66
66
  }
67
- // the game's runner, when the project has one: on the game's own origin,
67
+ // the game's function, when the project has one: on the game's own origin,
68
68
  // behind a ticket check, as on Arcadible
69
69
  const runner = runs && tickets
70
70
  ? await startRunner({
@@ -120,12 +120,14 @@ export async function serve({ art, files, hostFile = null, icon, info, keyFile,
120
120
  const host = req.headers.host;
121
121
  const onGame = host === `127.0.0.1:${port}`;
122
122
  const pathname = pathOf(req);
123
- // what's under the runner's folder, as Arcadible's edge sends it on:
124
- // /arcadible/runner itself, like /arcadible/runnerx, is a file of
125
- // Arcadible's, like any other there, and isn't found.
123
+ // the function's path, and what's under it, as Arcadible's edge sends
124
+ // them on: /arcadible/functionx is a file of Arcadible's, like any
125
+ // other there, and isn't found.
126
126
  // A project with no function is answered as Arcadible answers a build
127
127
  // with none.
128
- const forRunner = onGame && pathname !== null && pathname.startsWith(`${RUNNER}/`)
128
+ const forRunner = onGame &&
129
+ pathname !== null &&
130
+ (pathname === RUNNER || pathname.startsWith(`${RUNNER}/`))
129
131
  ? (runner?.handle ?? noFunction)
130
132
  : null;
131
133
  // a game opens a wire at Arcadible's path on its own origin, whether
package/dist/util/term.js CHANGED
@@ -19,3 +19,11 @@ export function configureColor() {
19
19
  chalkStderr.level = 0;
20
20
  }
21
21
  }
22
+ // [function]
23
+ // a Ctrl+C in a terminal leaves `^C` with the cursor after it, so whatever
24
+ // comes next, the shell's prompt included, starts on a new line.
25
+ export function interrupted() {
26
+ if (interactive()) {
27
+ process.stderr.write("\n");
28
+ }
29
+ }
@@ -157,10 +157,10 @@ export function createRoom(options) {
157
157
  left(member) {
158
158
  // their last connection: the rest hear they went
159
159
  const still = options.list().some((each) => each.member.id === member.id);
160
- if (!still) {
161
- sends.delete(member.id);
160
+ // their rate is kept, so leaving and coming back never starts it full
161
+ // (servers:21a)
162
+ if (!still)
162
163
  all({ leave: member });
163
- }
164
164
  },
165
165
  said(key, text) {
166
166
  const mine = options.list().find((each) => each.key === key);
@@ -44,7 +44,7 @@
44
44
  },
45
45
  "license": "Apache-2.0",
46
46
  "name": "arcadible",
47
- "version": "1.0.37"
47
+ "version": "1.0.38"
48
48
  },
49
49
  "node_modules/@asamuzakjp/css-color": {
50
50
  "dependencies": {
@@ -3267,5 +3267,5 @@
3267
3267
  }
3268
3268
  },
3269
3269
  "requires": true,
3270
- "version": "1.0.37"
3270
+ "version": "1.0.38"
3271
3271
  }
package/package.json CHANGED
@@ -68,5 +68,5 @@
68
68
  "test": "tsc && npm run lint && node --test \"test/**/*.test.js\" \"test/**/*.test.mjs\""
69
69
  },
70
70
  "type": "module",
71
- "version": "1.0.37"
71
+ "version": "1.0.38"
72
72
  }