arcadible 1.0.26 → 1.0.27

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.
@@ -29,6 +29,27 @@ const RESERVED = handle;
29
29
  // [declare]
30
30
  const SIZE_PARTIAL = ZIP_SIZE;
31
31
  // [function]
32
+ // the client table as the package carries it: the lists it declares, those
33
+ // with something in them, which the pipeline makes the build's policy from.
34
+ function packed(client) {
35
+ const out = {};
36
+ for (const field of ["outbound_hosts", "server_origins"]) {
37
+ const value = client?.[field];
38
+ const names = Array.isArray(value)
39
+ ? value.filter((each) => typeof each === "string")
40
+ : [];
41
+ if (names.length) {
42
+ out[field] = names;
43
+ }
44
+ }
45
+ return out;
46
+ }
47
+ // [function]
48
+ // whether a game declares anything its client may reach.
49
+ function declares(client) {
50
+ return Object.keys(packed(client)).length > 0;
51
+ }
52
+ // [function]
32
53
  // the first path at the build's root that's Arcadible's, if it has one: a
33
54
  // folder named like the reserved one, or the manifest or media that packing
34
55
  // adds, as `arc validate` checks deploy_dir. A folder ends with "/".
@@ -66,7 +87,7 @@ function verifyTotal(zip) {
66
87
  // [function]
67
88
  // `deployDir` when the input is deploy_dir, rather than a folder --input
68
89
  // named: a missing deploy_dir means the build hasn't written it.
69
- function pack(name, game, { deployDir, inputPath, mediaPath, onWarning, outputPath, runner, server, }) {
90
+ function pack(name, game, { deployDir, inputPath, mediaPath, onWarning, outputPath, runner, client, }) {
70
91
  const zip = new Zip(name);
71
92
  return pathExists(inputPath)
72
93
  .then((existsInput) => {
@@ -86,9 +107,10 @@ function pack(name, game, { deployDir, inputPath, mediaPath, onWarning, outputPa
86
107
  if (existsMedia) {
87
108
  zip.addDir(mediaPath, MEDIA);
88
109
  }
89
- // the game's own servers go with it, when it declares any: the build
90
- // keeps them, and a ticket is only ever for one of them. A runner goes
91
- // as what it may reach, never where its code was.
110
+ // what the build's client may reach goes with it, when it declares
111
+ // any: the pipeline makes its policy from it, and a ticket is only ever
112
+ // for one of its servers. A runner goes as what it may reach, never
113
+ // where its code was.
92
114
  zip.addJson(MANIFEST, {
93
115
  game,
94
116
  ...(runner
@@ -99,9 +121,7 @@ function pack(name, game, { deployDir, inputPath, mediaPath, onWarning, outputPa
99
121
  },
100
122
  }
101
123
  : {}),
102
- ...(Array.isArray(server?.origins) && server.origins.length
103
- ? { server }
104
- : {}),
124
+ ...(declares(client) ? { client: packed(client) } : {}),
105
125
  });
106
126
  return runner ? pathExists(runner.built) : false;
107
127
  })
@@ -131,7 +151,7 @@ function pack(name, game, { deployDir, inputPath, mediaPath, onWarning, outputPa
131
151
  // its deploy_dir is a path.
132
152
  export function packTool({ input, onWarning, output, ...conf }) {
133
153
  const root = conf.root;
134
- const { function: runner, game, server, workflow } = conf.project;
154
+ const { client, function: runner, game, workflow } = conf.project;
135
155
  const name = game.handle ?? game.id;
136
156
  const file = output ?? `${OUTPUT}/${name}-v${game.version}.zip`;
137
157
  const deployDir = resolve(root, workflow.deploy_dir);
@@ -143,6 +163,6 @@ export function packTool({ input, onWarning, output, ...conf }) {
143
163
  onWarning,
144
164
  outputPath: resolve(root, file),
145
165
  runner: runner ? { built: resolve(root, BUILT), table: runner } : null,
146
- server,
166
+ client,
147
167
  });
148
168
  }
@@ -9,6 +9,7 @@ import { setTimeout as sleep } from "node:timers/promises";
9
9
  import { pathExists } from "fs-extra";
10
10
  // [import]
11
11
  import { ARGUMENT_INVALID, DEPLOY_DIR_MISSING, PORT_IN_USE, } from "../../config/code.js";
12
+ import { clientNames } from "../../schema/client.js";
12
13
  import { buildCommand } from "../../util/command.js";
13
14
  import { ArcError } from "../../util/error.js";
14
15
  import { dirFiles } from "../../serve/files.js";
@@ -144,7 +145,8 @@ export async function serveTool(conf) {
144
145
  }
145
146
  // whether to say how a server finds this arc serve: only for a game with
146
147
  // servers of its own, or run for a player
147
- const forServer = Boolean(project.server) || Boolean(player);
148
+ const servers = strings(project.client?.server_origins);
149
+ const forServer = servers.length > 0 || Boolean(player);
148
150
  // the game's runner, when the project has one: its code, where its runtime
149
151
  // is kept, and who hears how it's doing
150
152
  const runs = project.function;
@@ -194,7 +196,12 @@ export async function serveTool(conf) {
194
196
  },
195
197
  }
196
198
  : {}),
197
- servers: strings(project.server?.origins),
199
+ servers,
200
+ // what the game may reach, as on Arcadible
201
+ connect: clientNames({
202
+ outbound_hosts: strings(project.client?.outbound_hosts),
203
+ server_origins: servers,
204
+ }),
198
205
  ...(conf.host ? { hostFile: resolvePath(root, conf.host) } : {}),
199
206
  };
200
207
  // the runner's code, watched: a change runs the new code
@@ -3,10 +3,10 @@ import { game } from "../../schema/game.js";
3
3
  import { given } from "../../parser/given.js";
4
4
  import { readme } from "../../schema/readme.js";
5
5
  import { read } from "../../parser/readme-file.js";
6
+ import { client } from "../../schema/client.js";
6
7
  import { runner } from "../../schema/runner.js";
7
8
  import { compare, validator } from "../../util/schema.js";
8
9
  import { split } from "../../parser/sections.js";
9
- import { server } from "../../schema/server.js";
10
10
  import { unknown } from "./unknown.js";
11
11
  import { unsound } from "./unsound.js";
12
12
  import { workflow } from "../../schema/workflow.js";
@@ -14,7 +14,7 @@ import { workflow } from "../../schema/workflow.js";
14
14
  const gameValidator = validator("game", game);
15
15
  const readmeValidator = validator("game", readme);
16
16
  const runnerValidator = validator("function", runner);
17
- const serverValidator = validator("server", server);
17
+ const clientValidator = validator("client", client);
18
18
  const workflowValidator = validator("workflow", workflow);
19
19
  // [function]
20
20
  // the README's structure, for its validator, or null without a README.
@@ -36,8 +36,8 @@ function validate(proj, readmeStructure, { root }) {
36
36
  if (proj.function) {
37
37
  results.push(runnerValidator(proj.function));
38
38
  }
39
- if (proj.server) {
40
- results.push(serverValidator(proj.server));
39
+ if (proj.client) {
40
+ results.push(clientValidator(proj.client));
41
41
  }
42
42
  if (readmeStructure) {
43
43
  const { file, ...value } = readmeStructure;
@@ -4,17 +4,17 @@ import { game } from "../../schema/game.js";
4
4
  import { didYouMean, unknownField, unknownTable } from "../../phrase/index.js";
5
5
  import { manifest } from "../../parser/manifest.js";
6
6
  import { isTable } from "../../util/objs.js";
7
+ import { client } from "../../schema/client.js";
7
8
  import { runner } from "../../schema/runner.js";
8
- import { server } from "../../schema/server.js";
9
9
  import { closest } from "../../util/strs.js";
10
10
  import { workflow } from "../../schema/workflow.js";
11
11
  // [function]
12
12
  // the fields each table takes, from its schema.
13
13
  function known(root) {
14
14
  return {
15
+ client: Object.keys(client().shape),
15
16
  function: Object.keys(runner().shape),
16
17
  game: Object.keys(game({}, { root }).shape),
17
- server: Object.keys(server().shape),
18
18
  workflow: Object.keys(workflow({}, { root }).shape),
19
19
  };
20
20
  }
@@ -14,8 +14,8 @@ import { media } from "./media.js";
14
14
  import { isTable } from "../util/objs.js";
15
15
  import { serial } from "../util/promise.js";
16
16
  import { readme } from "./readme.js";
17
+ import { client as clientSchema } from "../schema/client.js";
17
18
  import { runner as runnerSchema } from "../schema/runner.js";
18
- import { server as serverSchema } from "../schema/server.js";
19
19
  import * as cfgworkflow from "../config/workflow.js";
20
20
  import { workflow as workflowSchema, } from "../schema/workflow.js";
21
21
  // [use]
@@ -105,10 +105,11 @@ function parseSources(root, manifestData) {
105
105
  ...(isTable(manifestData["function"])
106
106
  ? { function: known(manifestData["function"], runnerSchema, root) }
107
107
  : {}),
108
- // only when arcadible.toml has the table, so a game without servers of
109
- // its own reads as it always has
110
- ...(isTable(manifestData["server"])
111
- ? { server: known(manifestData["server"], serverSchema, root) }
108
+ // what its client may reach besides its own origin (plan/EVERGREEN.md),
109
+ // only when arcadible.toml has the table, so a sealed game reads as it
110
+ // always has
111
+ ...(isTable(manifestData["client"])
112
+ ? { client: known(manifestData["client"], clientSchema, root) }
112
113
  : {}),
113
114
  workflow: known(configWorkflow(envData?.workflow, tableOf(manifestData["workflow"])), workflowSchema, root),
114
115
  }));
@@ -0,0 +1,136 @@
1
+ // [import]
2
+ import * as z from "zod";
3
+ // [import]
4
+ import { ISSUE } from "../config/code.js";
5
+ import { isName, MAX_NAMES_LENGTH, MAX_SERVERS, reserved, zHosts, } from "./host.js";
6
+ import { messages } from "./message.js";
7
+ // [declare]
8
+ const message = messages("client");
9
+ const PREFIX = "https://";
10
+ // [function]
11
+ // the name in an https origin on the default port, as the URL standard writes
12
+ // it (lower case, nothing after the host), so it compares exactly with what a
13
+ // ticket is asked for; or null.
14
+ export function originName(value) {
15
+ try {
16
+ const url = new URL(value);
17
+ const name = value.slice(PREFIX.length);
18
+ return url.protocol === "https:" && url.origin === value && isName(name)
19
+ ? name
20
+ : null;
21
+ }
22
+ catch {
23
+ return null;
24
+ }
25
+ }
26
+ // [function]
27
+ // whether an address is on the developer's own machine, which arc serve
28
+ // takes without it being declared, so it's never written here.
29
+ function isLocal(value) {
30
+ try {
31
+ return ["localhost", "127.0.0.1", "[::1]"].includes(new URL(value).hostname);
32
+ }
33
+ catch {
34
+ return false;
35
+ }
36
+ }
37
+ // [function]
38
+ // what to do about an address that isn't one: that a local one is left out,
39
+ // that the port goes, or the origin it almost is.
40
+ function hint(value) {
41
+ if (isLocal(value)) {
42
+ return "Leave your own machine out: arc serve reaches any server on it.";
43
+ }
44
+ try {
45
+ const url = new URL(value);
46
+ const plain = url.username === "" && url.password === "";
47
+ if (url.protocol === "https:" && plain && url.port !== "") {
48
+ return "A server is reached on port 443, the default: leave the port out.";
49
+ }
50
+ if (url.protocol === "https:" && plain && url.origin !== value) {
51
+ return `Did you mean ${url.origin}?`;
52
+ }
53
+ }
54
+ catch {
55
+ // not an address at all
56
+ }
57
+ return "A scheme and a host, with no path. A ticket is for the whole server.";
58
+ }
59
+ // [function]
60
+ // one of the developer's own servers: an https origin on the default port.
61
+ function zOrigin() {
62
+ const field = "server_origins";
63
+ return z.string().superRefine((value, ctx) => {
64
+ const name = originName(value);
65
+ if (name === null) {
66
+ ctx.addIssue({
67
+ code: "custom",
68
+ ...message(field, [
69
+ "Must be an https origin, such as https://api.example.com.",
70
+ hint(value),
71
+ ], ISSUE.URL_INVALID),
72
+ });
73
+ return;
74
+ }
75
+ const why = reserved(name);
76
+ if (why) {
77
+ ctx.addIssue({
78
+ code: "custom",
79
+ ...message(field, [why], ISSUE.INVALID),
80
+ });
81
+ }
82
+ });
83
+ }
84
+ // [function]
85
+ // the developer's own servers, which the build may reach and get a ticket for.
86
+ function zOrigins() {
87
+ const field = "server_origins";
88
+ return z.optional(z
89
+ .array(zOrigin())
90
+ .max(MAX_SERVERS, message(field, [`Must have at most ${MAX_SERVERS} items.`]))
91
+ .refine((items) => new Set(items).size === items.length, message(field, ["Must not repeat an item."], ISSUE.DUPLICATE)));
92
+ }
93
+ // [function]
94
+ // the names a client table declares, its servers' among them.
95
+ export function clientNames(table) {
96
+ return [
97
+ ...(table.outbound_hosts ?? []),
98
+ ...(table.server_origins ?? []).flatMap((each) => {
99
+ const name = originName(each);
100
+ return name === null ? [] : [name];
101
+ }),
102
+ ];
103
+ }
104
+ // [function]
105
+ // what a build may reach besides its own origin: the manifest's [client]
106
+ // table. A server is reached already, so it's never a host too, and all the
107
+ // names together fit the metadata the pipeline keeps them in.
108
+ export function client() {
109
+ return z
110
+ .object({
111
+ outbound_hosts: zHosts(message, "outbound_hosts", "a host's name"),
112
+ server_origins: zOrigins(),
113
+ })
114
+ .superRefine((table, ctx) => {
115
+ const servers = new Set(clientNames({ server_origins: table.server_origins }));
116
+ const both = (table.outbound_hosts ?? []).find((each) => servers.has(each));
117
+ if (both !== undefined) {
118
+ ctx.addIssue({
119
+ code: "custom",
120
+ path: ["outbound_hosts"],
121
+ ...message("outbound_hosts", [
122
+ `Must not list ${both}, a server in client.server_origins, which is reached already.`,
123
+ ], ISSUE.DUPLICATE),
124
+ });
125
+ }
126
+ if (clientNames(table).join(" ").length > MAX_NAMES_LENGTH) {
127
+ ctx.addIssue({
128
+ code: "custom",
129
+ path: ["outbound_hosts"],
130
+ ...message("outbound_hosts", [
131
+ `Must be, with client.server_origins, at most ${MAX_NAMES_LENGTH} characters together.`,
132
+ ], ISSUE.TOO_LONG),
133
+ });
134
+ }
135
+ });
136
+ }
@@ -0,0 +1,72 @@
1
+ // [import]
2
+ import * as z from "zod";
3
+ // [import]
4
+ import { ISSUE } from "../config/code.js";
5
+ // [declare]
6
+ // what a build or its function may reach besides Arcadible, as the release
7
+ // pipeline holds every release to it (its src/client.mts; plan/EVERGREEN.md,
8
+ // What each list allows; LIMITS.md): the most names in an `outbound_hosts`,
9
+ // the most servers, and all of a client's names together, in characters.
10
+ export const MAX_HOSTS = 8;
11
+ export const MAX_SERVERS = 4;
12
+ export const MAX_NAMES_LENGTH = 1024;
13
+ // a name, lower case, as DNS writes it: labels of letters, digits, and
14
+ // hyphens, a top-level domain of letters (or its punycode), never an address.
15
+ const LABEL = "[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?";
16
+ const TLD = "(?:[a-z]{2,63}|xn--[a-z0-9-]{1,59})";
17
+ const NAME = new RegExp(`^(?=.{1,253}$)(?:${LABEL}\\.)+${TLD}$`, "u");
18
+ // names only a private network answers, and Arcadible's own, which each side
19
+ // reaches already, undeclared.
20
+ const PRIVATE = ["localhost", "local", "internal", "home.arpa"];
21
+ const ARCADIBLE = ["arcadible.com", "arcadible.io"];
22
+ // [function]
23
+ function under(name, domain) {
24
+ return name === domain || name.endsWith(`.${domain}`);
25
+ }
26
+ // [function]
27
+ // whether `value` is a name as the pipeline takes one.
28
+ export function isName(value) {
29
+ return NAME.test(value);
30
+ }
31
+ // [function]
32
+ // why a name can't be declared, or null when it can.
33
+ export function reserved(name) {
34
+ if (PRIVATE.some((d) => under(name, d))) {
35
+ return "Must not be a private network's: arc serve reaches your own machine without it.";
36
+ }
37
+ if (ARCADIBLE.some((d) => under(name, d))) {
38
+ return "Must not be Arcadible's, which is reached already.";
39
+ }
40
+ return null;
41
+ }
42
+ // [function]
43
+ // a host's name, checked as the pipeline checks one, with why it isn't.
44
+ function zHost(message, field, what) {
45
+ return z.string().superRefine((value, ctx) => {
46
+ if (!isName(value)) {
47
+ ctx.addIssue({
48
+ code: "custom",
49
+ ...message(field, [
50
+ `Must be ${what}, such as api.example.com: lower case, with no scheme, port, path, or wildcard.`,
51
+ ], ISSUE.CHARACTERS_INVALID),
52
+ });
53
+ return;
54
+ }
55
+ const why = reserved(value);
56
+ if (why) {
57
+ ctx.addIssue({
58
+ code: "custom",
59
+ ...message(field, [why], ISSUE.INVALID),
60
+ });
61
+ }
62
+ });
63
+ }
64
+ // [function]
65
+ // a list of distinct hosts' names, at most MAX_HOSTS: what a build, or its
66
+ // function, may call out to besides Arcadible.
67
+ export function zHosts(message, field, what) {
68
+ return z.optional(z
69
+ .array(zHost(message, field, what))
70
+ .max(MAX_HOSTS, message(field, [`Must have at most ${MAX_HOSTS} items.`]))
71
+ .refine((items) => new Set(items).size === items.length, message(field, ["Must not repeat an item."], ISSUE.DUPLICATE)));
72
+ }
@@ -14,7 +14,7 @@ export const UNDOCUMENTED = new Set([
14
14
  "function.main",
15
15
  "function.outbound_hosts",
16
16
  "function.secrets",
17
- "server.origins",
17
+ "client.server_origins",
18
18
  ]);
19
19
  // [function]
20
20
  // the URL of a field's rules in the manifest reference, e.g. #game.title,
@@ -2,6 +2,7 @@
2
2
  import * as z from "zod";
3
3
  // [import]
4
4
  import { ISSUE } from "../config/code.js";
5
+ import { zHosts } from "./host.js";
5
6
  import { messages } from "./message.js";
6
7
  // [declare]
7
8
  const message = messages("function");
@@ -11,8 +12,6 @@ const REQUIRED = message("main", [
11
12
  ]).message;
12
13
  // a secret's name, as an environment variable is named.
13
14
  const SECRET = /^[A-Z][A-Z0-9_]*$/u;
14
- // a host a runner may call out to: a name, with no scheme, port, or path.
15
- const HOST = /^(?=.{1,253}$)([a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,}$/u;
16
15
  // [function]
17
16
  // a list of distinct strings, each matching `pattern`.
18
17
  function zNames(field, pattern, line) {
@@ -37,7 +36,7 @@ export function runner() {
37
36
  })
38
37
  .min(1, message("main", ["Must not be empty."]))
39
38
  .refine((file) => !file.startsWith("/") && !file.split(/[/\\]/u).includes(".."), message("main", ["Must be a path within the project."], ISSUE.PATH_INVALID)),
40
- outbound_hosts: zNames("outbound_hosts", HOST, "Must be a host's name, such as api.example.com."),
39
+ outbound_hosts: zHosts(message, "outbound_hosts", "a host's name"),
41
40
  secrets: zNames("secrets", SECRET, "Must be a name in capitals, such as EXAMPLE_KEY."),
42
41
  });
43
42
  }
@@ -52,12 +52,45 @@ function plain(res, status, headers = {}) {
52
52
  res.end(`${http.STATUS_CODES[status]}\n`);
53
53
  }
54
54
  // [function]
55
+ // what a build may load and reach, as Arcadible's edge sends it
56
+ // (app/src/cloudfront/io-arcadible-router): its own origin alone, which
57
+ // serves its files, the SDK, and its function, with no frames, and only
58
+ // `page` framing it; and the names its manifest declares (`[client]`), over
59
+ // https and wss, as the edge adds them. Locally, the developer's own machine
60
+ // is reached too, undeclared, so a server beside arc serve answers the game.
61
+ export function buildPolicy(page, connect = []) {
62
+ const declared = connect
63
+ .map((name) => ` https://${name} wss://${name}`)
64
+ .join("");
65
+ const local = ["localhost", "127.0.0.1"]
66
+ .flatMap((host) => ["http", "https", "ws", "wss"].map((scheme) => `${scheme}://${host}:*`))
67
+ .join(" ");
68
+ return [
69
+ "default-src 'self'",
70
+ "base-uri 'self'",
71
+ `connect-src 'self' data: blob:${declared} ${local}`,
72
+ "font-src 'self' data:",
73
+ "form-action 'none'",
74
+ `frame-ancestors ${page}`,
75
+ "frame-src 'none'",
76
+ "img-src 'self' data: blob:",
77
+ "manifest-src 'self'",
78
+ "media-src 'self' data: blob:",
79
+ "object-src 'none'",
80
+ "script-src 'self' 'unsafe-inline' 'unsafe-eval' blob:",
81
+ "style-src 'self' 'unsafe-inline'",
82
+ "worker-src 'self' blob:",
83
+ "sandbox allow-orientation-lock allow-pointer-lock allow-same-origin allow-scripts",
84
+ ].join("; ");
85
+ }
86
+ // [function]
55
87
  // the headers every response from the game's origin has, as Arcadible's: the
56
- // game is cross-origin isolated, and only the play page can frame it.
57
- function baseHeaders(page) {
88
+ // game is cross-origin isolated, sealed to its own origin, and only the play
89
+ // page can frame it.
90
+ function baseHeaders(page, connect) {
58
91
  return {
59
92
  "cache-control": "no-store",
60
- "content-security-policy": `frame-ancestors ${page};`,
93
+ "content-security-policy": buildPolicy(page, connect),
61
94
  "cross-origin-embedder-policy": "require-corp",
62
95
  "cross-origin-resource-policy": "cross-origin",
63
96
  "referrer-policy": "strict-origin-when-cross-origin",
@@ -134,9 +167,9 @@ function sendRemote(req, res, { headers, name, onRemote, remote, }) {
134
167
  // [function]
135
168
  // the request handler for the game's origin. `page` is the play page's
136
169
  // origin, the only one that may frame the game.
137
- export function gameHandler({ files, onRemote = () => null, page, remote, }) {
170
+ export function gameHandler({ connect = [], files, onRemote = () => null, page, remote, }) {
138
171
  return function (req, res) {
139
- const headers = baseHeaders(page);
172
+ const headers = baseHeaders(page, connect);
140
173
  if (req.method !== "GET" && req.method !== "HEAD") {
141
174
  plain(res, 405, { ...headers, allow: "GET, HEAD" });
142
175
  return Promise.resolve();
@@ -39,7 +39,7 @@ function listen(handler, port, address) {
39
39
  // or a guest. Resolves with
40
40
  // `{ page, game, reload(), close() }`: the page's origin, the game's URL,
41
41
  // and the controls.
42
- export async function serve({ art, files, hostFile = null, icon, info, keyFile, onRemote, player = null, port, remote, root, runner: runs = null, servers = [], src, }) {
42
+ export async function serve({ art, files, hostFile = null, icon, info, keyFile, onRemote, player = null, port, remote, root, runner: runs = null, servers = [], connect = [], src, }) {
43
43
  const origins = {
44
44
  game: `http://127.0.0.1:${port}`,
45
45
  page: `http://localhost:${port}`,
@@ -47,7 +47,7 @@ export async function serve({ art, files, hostFile = null, icon, info, keyFile,
47
47
  const framed = src ?? `${origins.game}/`;
48
48
  const reloads = reloader();
49
49
  const forGame = files
50
- ? gameHandler({ files, onRemote, page: origins.page, remote })
50
+ ? gameHandler({ connect, files, onRemote, page: origins.page, remote })
51
51
  : null;
52
52
  const tickets = keyFile
53
53
  ? ticketsFor({
@@ -91,7 +91,7 @@ export function originOf(value) {
91
91
  }
92
92
  // [function]
93
93
  // whether a ticket may be for `origin`: a server the project declared
94
- // (server.origins), or any on this machine, which is never declared.
94
+ // (client.server_origins), or any on this machine, which is never declared.
95
95
  function allowed(origin, servers) {
96
96
  return servers.includes(origin) || LOOPBACK.has(new URL(origin).hostname);
97
97
  }
@@ -43,7 +43,7 @@
43
43
  },
44
44
  "license": "Apache-2.0",
45
45
  "name": "arcadible",
46
- "version": "1.0.26"
46
+ "version": "1.0.27"
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.26"
3255
+ "version": "1.0.27"
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.26"
70
+ "version": "1.0.27"
71
71
  }
@@ -1,91 +0,0 @@
1
- // [import]
2
- import * as z from "zod";
3
- // [import]
4
- import { ISSUE } from "../config/code.js";
5
- import { messages } from "./message.js";
6
- // [declare]
7
- const message = messages("server");
8
- // the most servers of its own a game declares.
9
- export const MAX_ORIGINS = 8;
10
- // [function]
11
- // whether `value` is a server as a ticket names one: an https origin, written
12
- // as the URL standard writes it (lower case, no default port, nothing after
13
- // the host), so it compares exactly with what a ticket is asked for.
14
- function isOrigin(value) {
15
- try {
16
- const url = new URL(value);
17
- return url.protocol === "https:" && url.origin === value;
18
- }
19
- catch {
20
- return false;
21
- }
22
- }
23
- // [function]
24
- // the origin an address almost is, to suggest, or null: an https address
25
- // with a path, a trailing slash, capitals, or a default port.
26
- function nearOrigin(value) {
27
- try {
28
- const url = new URL(value);
29
- const plain = url.username === "" && url.password === "";
30
- return url.protocol === "https:" && plain ? url.origin : null;
31
- }
32
- catch {
33
- return null;
34
- }
35
- }
36
- // [function]
37
- // whether an address is on the developer's own machine, which arc serve
38
- // takes without it being declared, so it's never written here.
39
- function isLocal(value) {
40
- try {
41
- return ["localhost", "127.0.0.1", "[::1]"].includes(new URL(value).hostname);
42
- }
43
- catch {
44
- return false;
45
- }
46
- }
47
- // [function]
48
- // what to do about an address that isn't one: the origin it almost is, or
49
- // that a local one is left out, or what an origin is.
50
- function hint(value) {
51
- if (isLocal(value)) {
52
- return "Leave your own machine out: arc serve gives a ticket for any server on it.";
53
- }
54
- const near = nearOrigin(value);
55
- return near
56
- ? `Did you mean ${near}?`
57
- : "A scheme and a host, with no path. A ticket is for the whole server.";
58
- }
59
- // [function]
60
- // a server's address: https, and only the origin. The message names the
61
- // origin when the value has more than one, since that's the usual slip.
62
- function zOrigin() {
63
- const field = "origins";
64
- return z.string().superRefine((value, ctx) => {
65
- if (isOrigin(value)) {
66
- return;
67
- }
68
- ctx.addIssue({
69
- code: "custom",
70
- ...message(field, [
71
- "Must be an https origin, such as https://api.example.com.",
72
- hint(value),
73
- ], ISSUE.URL_INVALID),
74
- });
75
- });
76
- }
77
- // [function]
78
- // the game's own servers, each one a ticket may be for.
79
- function zOrigins() {
80
- const field = "origins";
81
- return z.optional(z
82
- .array(zOrigin())
83
- .max(MAX_ORIGINS, message(field, [`Must have at most ${MAX_ORIGINS} items.`]))
84
- .refine((items) => new Set(items).size === items.length, message(field, ["Must not repeat an item."], ISSUE.DUPLICATE)));
85
- }
86
- // [function]
87
- export function server() {
88
- return z.object({
89
- origins: zOrigins(),
90
- });
91
- }