arcadible 1.0.34 → 1.0.36
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.
- package/dist/action/delete/tool.js +2 -2
- package/dist/action/deploy/tool.js +5 -4
- package/dist/action/function/logs.js +238 -0
- package/dist/action/function/tool.js +49 -24
- package/dist/action/game.js +12 -1
- package/dist/action/key/tool.js +7 -7
- package/dist/action/secret/tool.js +7 -7
- package/dist/action/serve/tool.js +2 -0
- package/dist/action/value/tool.js +11 -9
- package/dist/action/wire/tool.js +2 -2
- package/dist/api.js +30 -5
- package/dist/command/function/index.js +5 -4
- package/dist/command/function/logs.js +154 -0
- package/dist/command/function/status.js +26 -9
- package/dist/command/serve.js +5 -0
- package/dist/command/value/usage.js +15 -5
- package/dist/commands.js +1 -1
- package/dist/config/code.js +16 -4
- package/dist/config/limit.js +2 -0
- package/dist/phrase/function.js +80 -17
- package/dist/phrase/game.js +5 -3
- package/dist/phrase/serve.js +6 -0
- package/dist/runner/bundle.js +10 -2
- package/dist/runner/entry.js +59 -3
- package/dist/serve/page.js +25 -8
- package/dist/serve/runner.js +43 -16
- package/dist/serve/server.js +8 -4
- package/dist/serve/wires.js +10 -5
- package/dist/util/error.js +10 -7
- package/dist/util/http.js +6 -1
- package/dist/util/request.js +5 -3
- package/dist/wires/room.js +10 -0
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
|
@@ -6,7 +6,7 @@ import { GAME_ID_MISSING, GAME_UNAVAILABLE } from "../../config/code.js";
|
|
|
6
6
|
import { ArcError } from "../../util/error.js";
|
|
7
7
|
import { headersOf, isStatus } from "../game.js";
|
|
8
8
|
import { given } from "../../parser/given.js";
|
|
9
|
-
import { gameIdMissing,
|
|
9
|
+
import { gameIdMissing, gameNotOwned } from "../../phrase/index.js";
|
|
10
10
|
import { textOf } from "../../util/objs.js";
|
|
11
11
|
// [function]
|
|
12
12
|
// a value of the manifest's as a name, when it's text with something in it.
|
|
@@ -38,7 +38,7 @@ export async function deleteTool(current, { id }) {
|
|
|
38
38
|
}
|
|
39
39
|
catch (err) {
|
|
40
40
|
if (isStatus(err, 404)) {
|
|
41
|
-
throw new ArcError(GAME_UNAVAILABLE,
|
|
41
|
+
throw new ArcError(GAME_UNAVAILABLE, gameNotOwned(id), {
|
|
42
42
|
cause: err,
|
|
43
43
|
});
|
|
44
44
|
}
|
|
@@ -3,7 +3,7 @@ import { auth, postUpload } from "../../api.js";
|
|
|
3
3
|
import { DEPLOY_RATE_LIMITED, GAME_UNAVAILABLE } from "../../config/code.js";
|
|
4
4
|
import { ArcError } from "../../util/error.js";
|
|
5
5
|
import { releasing } from "../predeploy/game.js";
|
|
6
|
-
import {
|
|
6
|
+
import { isRefused } from "../game.js";
|
|
7
7
|
import { deployRateLimited, gameUnavailable } from "../../phrase/index.js";
|
|
8
8
|
import { published } from "../../util/links.js";
|
|
9
9
|
import { upload as s3Upload } from "../../util/s3.js";
|
|
@@ -18,13 +18,14 @@ function uploadBuild(current, artifact, { id, version }, onProgress) {
|
|
|
18
18
|
form.append("size", String(artifact.size));
|
|
19
19
|
return postUpload(form, null, auth(current?.session.access_token)).then((upload) => s3Upload(upload.upload_url, artifact, onProgress).then(() => upload), (err) => {
|
|
20
20
|
// the API refuses a game that was deleted or is another account's
|
|
21
|
-
// (403), and the same version deployed again within 5 minutes (429)
|
|
22
|
-
|
|
21
|
+
// (403), and the same version deployed again within 5 minutes (429),
|
|
22
|
+
// each by its code.
|
|
23
|
+
if (isRefused(err, GAME_UNAVAILABLE, 403)) {
|
|
23
24
|
throw new ArcError(GAME_UNAVAILABLE, gameUnavailable(id), {
|
|
24
25
|
cause: err,
|
|
25
26
|
});
|
|
26
27
|
}
|
|
27
|
-
if (err
|
|
28
|
+
if (isRefused(err, DEPLOY_RATE_LIMITED, 429)) {
|
|
28
29
|
throw new ArcError(DEPLOY_RATE_LIMITED, deployRateLimited(version), {
|
|
29
30
|
cause: err,
|
|
30
31
|
});
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
// a game's function's logs (plan/FUNCTION-LOGS.md, Reading them): kept by
|
|
2
|
+
// Arcadible's logs Worker, not the API, which the game's owner reads with a
|
|
3
|
+
// token the API makes for them, good for 10 minutes: the newest page, or
|
|
4
|
+
// that page and then each entry as it comes, followed over a WebSocket,
|
|
5
|
+
// opened again with a new token whenever a follow closes.
|
|
6
|
+
// [import]
|
|
7
|
+
import { WebSocket } from "ws";
|
|
8
|
+
import { postGameFunctionLogsToken } from "../../api.js";
|
|
9
|
+
import { API_REFUSED, API_UNREACHABLE, ARGUMENT_INVALID, GAME_UNAVAILABLE, INTERNAL, } from "../../config/code.js";
|
|
10
|
+
import { ArcError } from "../../util/error.js";
|
|
11
|
+
import { sleep } from "../../util/delay.js";
|
|
12
|
+
import { isTable } from "../../util/objs.js";
|
|
13
|
+
import { channelOf, gameOf, headersOf, isStatus } from "../game.js";
|
|
14
|
+
import { answerUnread, functionLogsBusy, functionLogsRefused, functionLogsSinceInvalid, functionLogsVersionInvalid, gameNotOwned, unableToReach, } from "../../phrase/index.js";
|
|
15
|
+
// [declare]
|
|
16
|
+
// the protocol a follow speaks, offered with the token beside it, and the
|
|
17
|
+
// code it's closed with at its hour (the logs Worker's src/logs.js and
|
|
18
|
+
// src/game.js).
|
|
19
|
+
const PROTOCOL = "arcadible-logs";
|
|
20
|
+
// how often a follow says it's there, so no proxy takes it for idle; the
|
|
21
|
+
// game's object answers `pong`.
|
|
22
|
+
const PING = 30 * 1000;
|
|
23
|
+
// how many follows in a row may fail to open before the command does, and
|
|
24
|
+
// the longest wait between them.
|
|
25
|
+
const TRIES = 5;
|
|
26
|
+
const WAIT = 30 * 1000;
|
|
27
|
+
// a version as the Worker takes it, and a span back from now.
|
|
28
|
+
const VERSION = /^[0-9A-Za-z.+-]{1,64}$/u;
|
|
29
|
+
const SPAN = /^([1-9][0-9]{0,4})([mhd])$/u;
|
|
30
|
+
const UNIT = {
|
|
31
|
+
d: 24 * 60 * 60 * 1000,
|
|
32
|
+
h: 60 * 60 * 1000,
|
|
33
|
+
m: 60 * 1000,
|
|
34
|
+
};
|
|
35
|
+
// [function]
|
|
36
|
+
// `--since` as an ISO time: a span back from `now` (`10m`, `2h`, `1d`), or
|
|
37
|
+
// a time or a day as ISO writes it (`2026-11-19T14:00:00Z`,
|
|
38
|
+
// `2026-11-19`, UTC's midnight). Anything else is argument_invalid.
|
|
39
|
+
export function sinceOf(text, now = Date.now()) {
|
|
40
|
+
const span = SPAN.exec(text.trim());
|
|
41
|
+
if (span) {
|
|
42
|
+
return new Date(now - Number(span[1]) * (UNIT[span[2] ?? "m"] ?? 0)).toISOString();
|
|
43
|
+
}
|
|
44
|
+
const at = Date.parse(text);
|
|
45
|
+
if (/^\d{4}-\d{2}-\d{2}/u.test(text) && Number.isFinite(at)) {
|
|
46
|
+
return new Date(at).toISOString();
|
|
47
|
+
}
|
|
48
|
+
throw new ArcError(ARGUMENT_INVALID, functionLogsSinceInvalid(text));
|
|
49
|
+
}
|
|
50
|
+
// [function]
|
|
51
|
+
// the query a page or a follow asks with.
|
|
52
|
+
function queryOf({ errors, since, version }, channel) {
|
|
53
|
+
const query = new URLSearchParams();
|
|
54
|
+
if (channel)
|
|
55
|
+
query.set("channel", channel);
|
|
56
|
+
if (version) {
|
|
57
|
+
if (!VERSION.test(version)) {
|
|
58
|
+
throw new ArcError(ARGUMENT_INVALID, functionLogsVersionInvalid(version));
|
|
59
|
+
}
|
|
60
|
+
query.set("version", version);
|
|
61
|
+
}
|
|
62
|
+
if (since)
|
|
63
|
+
query.set("since", sinceOf(since));
|
|
64
|
+
if (errors)
|
|
65
|
+
query.set("outcome", "error");
|
|
66
|
+
return query;
|
|
67
|
+
}
|
|
68
|
+
// [function]
|
|
69
|
+
// whether an answer is a page as the Worker answers it.
|
|
70
|
+
function isPage(value) {
|
|
71
|
+
return (isTable(value) &&
|
|
72
|
+
Array.isArray(value["data"]) &&
|
|
73
|
+
value["data"].every((each) => isEntry(each)));
|
|
74
|
+
}
|
|
75
|
+
// [function]
|
|
76
|
+
function isEntry(value) {
|
|
77
|
+
return (isTable(value) &&
|
|
78
|
+
typeof value["id"] === "number" &&
|
|
79
|
+
typeof value["at"] === "number");
|
|
80
|
+
}
|
|
81
|
+
// [function]
|
|
82
|
+
// the Worker's refusal as the CLI's failure: Arcadible's side (a 5xx) is
|
|
83
|
+
// internal, and anything else api_refused, never a 401 that would end the
|
|
84
|
+
// session, since the token isn't the session's.
|
|
85
|
+
function refused(status, code) {
|
|
86
|
+
return new ArcError(status >= 500 ? INTERNAL : API_REFUSED, functionLogsRefused({ code, status }));
|
|
87
|
+
}
|
|
88
|
+
// [function]
|
|
89
|
+
// a token for the game's logs: the game is the caller's, or
|
|
90
|
+
// game_unavailable, as for the rest of `arc function`.
|
|
91
|
+
async function tokenOf(id, headers) {
|
|
92
|
+
try {
|
|
93
|
+
return await postGameFunctionLogsToken(id, null, headers);
|
|
94
|
+
}
|
|
95
|
+
catch (err) {
|
|
96
|
+
if (isStatus(err, 404)) {
|
|
97
|
+
throw new ArcError(GAME_UNAVAILABLE, gameNotOwned(id), { cause: err });
|
|
98
|
+
}
|
|
99
|
+
throw err;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
// [function]
|
|
103
|
+
// the newest page, newest first, or null when the token had expired.
|
|
104
|
+
async function pageOf(token, query) {
|
|
105
|
+
let res;
|
|
106
|
+
try {
|
|
107
|
+
res = await fetch(query.size ? `${token.url}?${query.toString()}` : token.url, {
|
|
108
|
+
headers: { authorization: `Bearer ${token.token}` },
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
catch (ex) {
|
|
112
|
+
throw new ArcError(API_UNREACHABLE, unableToReach(), { cause: ex });
|
|
113
|
+
}
|
|
114
|
+
const body = await res.text().then((text) => {
|
|
115
|
+
try {
|
|
116
|
+
return JSON.parse(text);
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
}, () => null);
|
|
122
|
+
if (!res.ok) {
|
|
123
|
+
const code = isTable(body) && typeof body["code"] === "string" ? body["code"] : null;
|
|
124
|
+
if (res.status === 401 && code === "expired")
|
|
125
|
+
return null;
|
|
126
|
+
throw refused(res.status, code);
|
|
127
|
+
}
|
|
128
|
+
if (!isPage(body)) {
|
|
129
|
+
throw new ArcError(INTERNAL, answerUnread("a function's logs"));
|
|
130
|
+
}
|
|
131
|
+
return body.data;
|
|
132
|
+
}
|
|
133
|
+
// [function]
|
|
134
|
+
// the game's newest entries that match, at most 100, newest first. A token
|
|
135
|
+
// that ended on its way is asked for again, once.
|
|
136
|
+
export async function logs(current, { channel, ...conf }) {
|
|
137
|
+
const where = channelOf(channel);
|
|
138
|
+
const query = queryOf(conf, where);
|
|
139
|
+
const id = await gameOf(conf);
|
|
140
|
+
const headers = headersOf(current);
|
|
141
|
+
for (let tries = 0;; tries += 1) {
|
|
142
|
+
const page = await pageOf(await tokenOf(id, headers), query);
|
|
143
|
+
if (page)
|
|
144
|
+
return page;
|
|
145
|
+
if (tries)
|
|
146
|
+
throw refused(401, "expired");
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
// [function]
|
|
150
|
+
// a follow, open until it closes: each entry it sends goes to `shown`, and
|
|
151
|
+
// it resolves with how it ended.
|
|
152
|
+
function followOnce(token, query, shown) {
|
|
153
|
+
const url = new URL(token.url);
|
|
154
|
+
url.protocol = url.protocol === "http:" ? "ws:" : "wss:";
|
|
155
|
+
url.search = new URLSearchParams([["follow", ""], ...query]).toString();
|
|
156
|
+
return new Promise((resolve) => {
|
|
157
|
+
let opened = false;
|
|
158
|
+
let done = false;
|
|
159
|
+
let pinging = null;
|
|
160
|
+
const end = (status) => {
|
|
161
|
+
if (done)
|
|
162
|
+
return;
|
|
163
|
+
done = true;
|
|
164
|
+
if (pinging)
|
|
165
|
+
clearInterval(pinging);
|
|
166
|
+
resolve({ opened, status });
|
|
167
|
+
};
|
|
168
|
+
const socket = new WebSocket(url.href, [PROTOCOL, token.token]);
|
|
169
|
+
socket.on("unexpected-response", (req, res) => {
|
|
170
|
+
req.destroy();
|
|
171
|
+
res.resume();
|
|
172
|
+
end(res.statusCode ?? 500);
|
|
173
|
+
});
|
|
174
|
+
socket.on("open", () => {
|
|
175
|
+
opened = true;
|
|
176
|
+
pinging = setInterval(() => {
|
|
177
|
+
socket.send("ping");
|
|
178
|
+
}, PING);
|
|
179
|
+
});
|
|
180
|
+
socket.on("message", (data) => {
|
|
181
|
+
let entry = null;
|
|
182
|
+
try {
|
|
183
|
+
entry = JSON.parse(String(data));
|
|
184
|
+
}
|
|
185
|
+
catch {
|
|
186
|
+
// `pong`, or anything else that isn't an entry
|
|
187
|
+
}
|
|
188
|
+
if (isEntry(entry))
|
|
189
|
+
shown(entry);
|
|
190
|
+
});
|
|
191
|
+
socket.on("error", () => {
|
|
192
|
+
// its close follows
|
|
193
|
+
});
|
|
194
|
+
socket.on("close", () => {
|
|
195
|
+
end(null);
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
// [function]
|
|
200
|
+
// the game's newest entries that match, then each new one as it comes,
|
|
201
|
+
// until the command is stopped: `shown` is given each once, oldest first.
|
|
202
|
+
// A follow closes at its hour, or when the connection drops, and is opened
|
|
203
|
+
// again with a new token; one the Worker refuses ends the command, and so
|
|
204
|
+
// does one that can't be opened five times in a row.
|
|
205
|
+
export async function follow(current, { channel, ...conf }, shown) {
|
|
206
|
+
const where = channelOf(channel);
|
|
207
|
+
const query = queryOf(conf, where);
|
|
208
|
+
const id = await gameOf(conf);
|
|
209
|
+
const headers = headersOf(current);
|
|
210
|
+
let last = 0;
|
|
211
|
+
const once = (entry) => {
|
|
212
|
+
// a follow sends its newest few as it opens: those shown already aren't
|
|
213
|
+
// shown again
|
|
214
|
+
if (entry.id <= last)
|
|
215
|
+
return;
|
|
216
|
+
last = entry.id;
|
|
217
|
+
shown(entry);
|
|
218
|
+
};
|
|
219
|
+
const first = await logs(current, { channel, ...conf });
|
|
220
|
+
for (const entry of first.reverse())
|
|
221
|
+
once(entry);
|
|
222
|
+
// a follow takes no `since`: it sends what's newest
|
|
223
|
+
query.delete("since");
|
|
224
|
+
for (let failed = 0;;) {
|
|
225
|
+
const closed = await followOnce(await tokenOf(id, headers), query, once);
|
|
226
|
+
if (closed.status === 429)
|
|
227
|
+
throw new ArcError(API_REFUSED, functionLogsBusy());
|
|
228
|
+
if (closed.status && closed.status < 500 && closed.status !== 401) {
|
|
229
|
+
throw refused(closed.status, null);
|
|
230
|
+
}
|
|
231
|
+
failed = closed.opened ? 0 : failed + 1;
|
|
232
|
+
if (failed >= TRIES) {
|
|
233
|
+
throw new ArcError(API_UNREACHABLE, unableToReach());
|
|
234
|
+
}
|
|
235
|
+
if (failed)
|
|
236
|
+
await sleep(Math.min(WAIT, 1000 * 2 ** failed));
|
|
237
|
+
}
|
|
238
|
+
}
|
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
// too, and so can its allowance, spent: a pause has a reason, and an owner
|
|
4
4
|
// lifts only their own.
|
|
5
5
|
import { deleteGameFunctionPause, discoverGameBuilds, gameFunctionPauses, gameFunctionUsages, putGameFunctionPause, } from "../../api.js";
|
|
6
|
-
import {
|
|
6
|
+
import { FUNCTION_PAUSE_RESERVED, GAME_UNAVAILABLE, } from "../../config/code.js";
|
|
7
7
|
import { ArcError } from "../../util/error.js";
|
|
8
8
|
import { channelOf, CHANNELS, gameOf, headersOf, isStatus, needOf, } from "../game.js";
|
|
9
|
-
import {
|
|
9
|
+
import { functionPauseReserved, gameNotOwned } from "../../phrase/index.js";
|
|
10
10
|
// [declare]
|
|
11
11
|
// the reasons a function is paused for, the one that holds longest first:
|
|
12
12
|
// what a status says when there's more than one.
|
|
@@ -21,6 +21,11 @@ function pausedOf(pauses, channel) {
|
|
|
21
21
|
return REASONS.find((reason) => reasons.includes(reason)) ?? null;
|
|
22
22
|
}
|
|
23
23
|
// [function]
|
|
24
|
+
// when a channel's pause for a limit ends by itself, or null for none.
|
|
25
|
+
function resumesOf(pauses, channel) {
|
|
26
|
+
return ((pauses ?? []).find((pause) => pause.channel === channel && pause.reason === "limit")?.resumes_at ?? null);
|
|
27
|
+
}
|
|
28
|
+
// [function]
|
|
24
29
|
// whether a channel's live build has a function: true, false, or null when
|
|
25
30
|
// that can't be read, which never stands in a pause's way.
|
|
26
31
|
async function hasFunction(id, channel) {
|
|
@@ -57,7 +62,7 @@ export async function pause(current, { channel, ...conf }) {
|
|
|
57
62
|
}
|
|
58
63
|
catch (err) {
|
|
59
64
|
if (isStatus(err, 404)) {
|
|
60
|
-
throw new ArcError(GAME_UNAVAILABLE,
|
|
65
|
+
throw new ArcError(GAME_UNAVAILABLE, gameNotOwned(id), {
|
|
61
66
|
cause: err,
|
|
62
67
|
});
|
|
63
68
|
}
|
|
@@ -66,9 +71,10 @@ export async function pause(current, { channel, ...conf }) {
|
|
|
66
71
|
}
|
|
67
72
|
// [function]
|
|
68
73
|
// resumes the game's function on a channel: lifts its owner's pause, and
|
|
69
|
-
// resolves with `{ channel, paused }`, `paused` the reason it's
|
|
70
|
-
// for, or null.
|
|
71
|
-
//
|
|
74
|
+
// resolves with `{ channel, lifted, paused }`, `paused` the reason it's
|
|
75
|
+
// still paused for, or null. One that isn't paused stays as it was, as `pause` leaves one
|
|
76
|
+
// that's paused already. A pause of Arcadible's, or a limit's, isn't
|
|
77
|
+
// its owner's to lift: function_pause_reserved, saying who lifts it.
|
|
72
78
|
export async function resume(current, { channel, ...conf }) {
|
|
73
79
|
const where = needOf(channel);
|
|
74
80
|
const id = await gameOf(conf);
|
|
@@ -84,19 +90,19 @@ export async function resume(current, { channel, ...conf }) {
|
|
|
84
90
|
lifted = false;
|
|
85
91
|
}
|
|
86
92
|
const paused = pausedOf(await gameFunctionPauses(id, null, headers), where);
|
|
87
|
-
if (!lifted) {
|
|
88
|
-
throw new ArcError(
|
|
93
|
+
if (!lifted && paused) {
|
|
94
|
+
throw new ArcError(FUNCTION_PAUSE_RESERVED, functionPauseReserved({ channel: where, paused }));
|
|
89
95
|
}
|
|
90
|
-
return { channel: where, paused };
|
|
96
|
+
return { channel: where, lifted, paused };
|
|
91
97
|
}
|
|
92
98
|
// [function]
|
|
93
99
|
// the game's function on each channel, or on one: `{ channel, function,
|
|
94
100
|
// paused, requests }`, `function` whether the channel's live build has one
|
|
95
101
|
// (a pause and a usage are the channel's, so neither says), `paused` the
|
|
96
|
-
// reason it's paused for, or null for none,
|
|
97
|
-
// this month. With them,
|
|
98
|
-
//
|
|
99
|
-
//
|
|
102
|
+
// reason it's paused for, or null for none, `requests` what it answered
|
|
103
|
+
// this month, and `cpu_ms` its CPU time. With them, what holds it, whichever channels were asked for:
|
|
104
|
+
// the game's day against its own limit, its month, and its account's pool
|
|
105
|
+
// against its plan's, each as the API states it.
|
|
100
106
|
export async function status(current, { channel, ...conf }) {
|
|
101
107
|
const where = channelOf(channel);
|
|
102
108
|
const id = await gameOf(conf);
|
|
@@ -111,17 +117,36 @@ export async function status(current, { channel, ...conf }) {
|
|
|
111
117
|
const has = await Promise.all(shown.map((each) => hasFunction(id, each)));
|
|
112
118
|
const rows = usages ?? [];
|
|
113
119
|
const [first] = rows;
|
|
114
|
-
const sum = (of) => of.reduce((total, each) => total + Number(each
|
|
120
|
+
const sum = (of, field) => of.reduce((total, each) => total + Number(each[field] ?? 0), 0);
|
|
121
|
+
// null is no limit, and stays null
|
|
122
|
+
const limit = (value) => value === null ? null : Number(value);
|
|
115
123
|
return {
|
|
116
|
-
channels: shown.map((each, i) =>
|
|
117
|
-
channel
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
124
|
+
channels: shown.map((each, i) => {
|
|
125
|
+
const own = rows.filter((row) => row.channel === each);
|
|
126
|
+
return {
|
|
127
|
+
channel: each,
|
|
128
|
+
cpu_ms: sum(own, "cpu_ms"),
|
|
129
|
+
function: has[i] ?? null,
|
|
130
|
+
paused: pausedOf(pauses, each),
|
|
131
|
+
requests: sum(own, "requests"),
|
|
132
|
+
resumes_at: resumesOf(pauses, each),
|
|
133
|
+
};
|
|
134
|
+
}),
|
|
135
|
+
day: first
|
|
136
|
+
? { allowance: limit(first.allowance), requests: Number(first.used) }
|
|
137
|
+
: null,
|
|
138
|
+
month: first
|
|
139
|
+
? {
|
|
140
|
+
cpu_ms: Number(first.month_cpu_ms ?? 0),
|
|
141
|
+
requests: Number(first.month_used),
|
|
142
|
+
}
|
|
143
|
+
: null,
|
|
144
|
+
pool: first
|
|
145
|
+
? {
|
|
146
|
+
allowance: limit(first.pool_allowance),
|
|
147
|
+
cpu_ms: Number(first.pool_cpu_ms ?? 0),
|
|
148
|
+
requests: Number(first.pool_used),
|
|
149
|
+
}
|
|
150
|
+
: null,
|
|
126
151
|
};
|
|
127
152
|
}
|
package/dist/action/game.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// command names, the session's headers, and telling the API's refusals
|
|
4
4
|
// apart.
|
|
5
5
|
import { auth } from "../api.js";
|
|
6
|
-
import { ARGUMENT_INVALID, GAME_ID_MISSING, NOT_LOGGED_IN, } from "../config/code.js";
|
|
6
|
+
import { ALL, ARGUMENT_INVALID, GAME_ID_MISSING, NOT_LOGGED_IN, } from "../config/code.js";
|
|
7
7
|
import { ArcError } from "../util/error.js";
|
|
8
8
|
import { given } from "../parser/given.js";
|
|
9
9
|
import { HttpError } from "../util/http.js";
|
|
@@ -60,3 +60,14 @@ export function headersOf(current) {
|
|
|
60
60
|
export function isStatus(err, status) {
|
|
61
61
|
return err instanceof HttpError && err.status === status;
|
|
62
62
|
}
|
|
63
|
+
// [function]
|
|
64
|
+
// whether a failure is the API's refusal with this code, which the
|
|
65
|
+
// database names (ERRORS.md): read first, and, from an API that doesn't say
|
|
66
|
+
// one, its status.
|
|
67
|
+
export function isRefused(err, code, status) {
|
|
68
|
+
if (!(err instanceof HttpError))
|
|
69
|
+
return false;
|
|
70
|
+
if (err.code === code)
|
|
71
|
+
return true;
|
|
72
|
+
return (err.status === status && (err.code === undefined || !ALL.has(err.code)));
|
|
73
|
+
}
|
package/dist/action/key/tool.js
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
// is for one game and one channel, made by the game's owner, shown once, and
|
|
3
3
|
// revoked at once. Nothing here keeps one.
|
|
4
4
|
import { deleteKey, gameKeys, postGameKey, } from "../../api.js";
|
|
5
|
-
import {
|
|
5
|
+
import { ARGUMENT_INVALID, GAME_UNAVAILABLE, KEY_LIMIT_REACHED, KEY_NOT_FOUND, } from "../../config/code.js";
|
|
6
6
|
import { ArcError } from "../../util/error.js";
|
|
7
|
-
import { channelOf, gameOf, headersOf, isStatus, needOf } from "../game.js";
|
|
8
|
-
import {
|
|
7
|
+
import { channelOf, gameOf, headersOf, isRefused, isStatus, needOf, } from "../game.js";
|
|
8
|
+
import { gameNotOwned, keyLimitReached, keyNameInvalid, keyNotFound, } from "../../phrase/index.js";
|
|
9
9
|
// [declare]
|
|
10
10
|
const NAME_MAX = 64;
|
|
11
11
|
// the name a key gets when none is given.
|
|
@@ -36,12 +36,12 @@ export async function create(current, { channel, name, ...conf }) {
|
|
|
36
36
|
}
|
|
37
37
|
catch (err) {
|
|
38
38
|
if (isStatus(err, 404)) {
|
|
39
|
-
throw new ArcError(GAME_UNAVAILABLE,
|
|
39
|
+
throw new ArcError(GAME_UNAVAILABLE, gameNotOwned(id), {
|
|
40
40
|
cause: err,
|
|
41
41
|
});
|
|
42
42
|
}
|
|
43
|
-
if (
|
|
44
|
-
throw new ArcError(
|
|
43
|
+
if (isRefused(err, KEY_LIMIT_REACHED, 409)) {
|
|
44
|
+
throw new ArcError(KEY_LIMIT_REACHED, keyLimitReached(where), {
|
|
45
45
|
cause: err,
|
|
46
46
|
});
|
|
47
47
|
}
|
|
@@ -79,7 +79,7 @@ export async function revoke(current, { id, ...conf }) {
|
|
|
79
79
|
}
|
|
80
80
|
catch (err) {
|
|
81
81
|
if (isStatus(err, 404) || isStatus(err, 400)) {
|
|
82
|
-
throw new ArcError(
|
|
82
|
+
throw new ArcError(KEY_NOT_FOUND, keyNotFound(id), {
|
|
83
83
|
cause: err,
|
|
84
84
|
});
|
|
85
85
|
}
|
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
// channel, by the name the manifest declares (function.secrets), set by the
|
|
4
4
|
// game's owner, and never shown again. Nothing here keeps one.
|
|
5
5
|
import { deleteGameFunctionSecret, gameFunctionSecrets, putGameFunctionSecret, } from "../../api.js";
|
|
6
|
-
import {
|
|
6
|
+
import { ARGUMENT_INVALID, GAME_UNAVAILABLE, SECRET_LIMIT_REACHED, SECRET_NOT_FOUND, } from "../../config/code.js";
|
|
7
7
|
import { ArcError } from "../../util/error.js";
|
|
8
|
-
import { channelOf, gameOf, headersOf, isStatus, needOf } from "../game.js";
|
|
9
|
-
import {
|
|
8
|
+
import { channelOf, gameOf, headersOf, isRefused, isStatus, needOf, } from "../game.js";
|
|
9
|
+
import { gameNotOwned, secretLimitReached, secretNameInvalid, secretNotFound, secretValueInvalid, } from "../../phrase/index.js";
|
|
10
10
|
// [declare]
|
|
11
11
|
// a secret's name, as an environment variable is named; ARCADIBLE_ is kept
|
|
12
12
|
// for Arcadible's own.
|
|
@@ -42,12 +42,12 @@ export async function set(current, { channel, name, value, ...conf }) {
|
|
|
42
42
|
}
|
|
43
43
|
catch (err) {
|
|
44
44
|
if (isStatus(err, 404)) {
|
|
45
|
-
throw new ArcError(GAME_UNAVAILABLE,
|
|
45
|
+
throw new ArcError(GAME_UNAVAILABLE, gameNotOwned(id), {
|
|
46
46
|
cause: err,
|
|
47
47
|
});
|
|
48
48
|
}
|
|
49
|
-
if (
|
|
50
|
-
throw new ArcError(
|
|
49
|
+
if (isRefused(err, SECRET_LIMIT_REACHED, 409)) {
|
|
50
|
+
throw new ArcError(SECRET_LIMIT_REACHED, secretLimitReached(where), {
|
|
51
51
|
cause: err,
|
|
52
52
|
});
|
|
53
53
|
}
|
|
@@ -84,7 +84,7 @@ export async function remove(current, { channel, name, ...conf }) {
|
|
|
84
84
|
}
|
|
85
85
|
catch (err) {
|
|
86
86
|
if (isStatus(err, 404)) {
|
|
87
|
-
throw new ArcError(
|
|
87
|
+
throw new ArcError(SECRET_NOT_FOUND, secretNotFound({ channel: where, name: named }), { cause: err });
|
|
88
88
|
}
|
|
89
89
|
throw err;
|
|
90
90
|
}
|
|
@@ -130,6 +130,7 @@ export async function serveTool(conf) {
|
|
|
130
130
|
const { port, root, url } = conf;
|
|
131
131
|
const handler = {
|
|
132
132
|
onBuild() { },
|
|
133
|
+
onLink() { },
|
|
133
134
|
onReload() { },
|
|
134
135
|
onRemote() { },
|
|
135
136
|
onRunnerError() { },
|
|
@@ -167,6 +168,7 @@ export async function serveTool(conf) {
|
|
|
167
168
|
},
|
|
168
169
|
// arc serve's own tickets, signed with a key kept beside the session
|
|
169
170
|
keyFile: join(configDir(), "ticket-key.json"),
|
|
171
|
+
onLink: (url) => handler.onLink(url),
|
|
170
172
|
onRemote: handler.onRemote,
|
|
171
173
|
player,
|
|
172
174
|
port,
|
|
@@ -6,10 +6,10 @@
|
|
|
6
6
|
import fs from "node:fs";
|
|
7
7
|
import path from "node:path";
|
|
8
8
|
import { gameValuesList, gameValuesRead, gameValuesRemove, gameValuesWrite, gameValueCalls, gameValueUsages, } from "../../api.js";
|
|
9
|
-
import {
|
|
9
|
+
import { ARGUMENT_INVALID, GAME_UNAVAILABLE, VALUE_NOT_FOUND, VALUE_STORAGE_FULL, VALUE_TOO_LARGE, } from "../../config/code.js";
|
|
10
10
|
import { ArcError } from "../../util/error.js";
|
|
11
11
|
import { gameOf, headersOf, isStatus, needOf } from "../game.js";
|
|
12
|
-
import {
|
|
12
|
+
import { gameNotOwned, valueDirEmpty, valueFileUnread, valueFull, valueKeyInvalid, valueNotFound, valueNotJson, valueSetWhat, valueTooLarge, } from "../../phrase/index.js";
|
|
13
13
|
// [declare]
|
|
14
14
|
// a key, as Arcadible takes one (db/function/game_value.sql, value_keys).
|
|
15
15
|
export const KEY = /^[A-Za-z0-9_./-]{1,128}$/u;
|
|
@@ -101,12 +101,14 @@ function batches(items) {
|
|
|
101
101
|
function refused(id, channel) {
|
|
102
102
|
return (err) => {
|
|
103
103
|
if (isStatus(err, 404)) {
|
|
104
|
-
throw new ArcError(GAME_UNAVAILABLE,
|
|
104
|
+
throw new ArcError(GAME_UNAVAILABLE, gameNotOwned(id), {
|
|
105
105
|
cause: err,
|
|
106
106
|
});
|
|
107
107
|
}
|
|
108
108
|
if (isStatus(err, 507)) {
|
|
109
|
-
throw new ArcError(
|
|
109
|
+
throw new ArcError(VALUE_STORAGE_FULL, valueFull(channel), {
|
|
110
|
+
cause: err,
|
|
111
|
+
});
|
|
110
112
|
}
|
|
111
113
|
throw err;
|
|
112
114
|
};
|
|
@@ -139,7 +141,7 @@ export async function set(current, { channel, dir, file, input, key, ...conf })
|
|
|
139
141
|
if (isStatus(err, 413)) {
|
|
140
142
|
// a batch is under what a call holds, so it's one value's own size
|
|
141
143
|
const [largest] = [...batch].sort((a, b) => JSON.stringify(b.value).length - JSON.stringify(a.value).length);
|
|
142
|
-
throw new ArcError(
|
|
144
|
+
throw new ArcError(VALUE_TOO_LARGE, valueTooLarge(largest?.key ?? ""), {
|
|
143
145
|
cause: err,
|
|
144
146
|
});
|
|
145
147
|
}
|
|
@@ -158,7 +160,7 @@ export async function get(current, { channel, key, ...conf }) {
|
|
|
158
160
|
const headers = headersOf(current);
|
|
159
161
|
const [value] = await gameValuesRead(id, where, [named], headers).catch(refused(id, where));
|
|
160
162
|
if (!value) {
|
|
161
|
-
throw new ArcError(
|
|
163
|
+
throw new ArcError(VALUE_NOT_FOUND, valueNotFound({ channel: where, key: named }));
|
|
162
164
|
}
|
|
163
165
|
return { channel: where, ...value };
|
|
164
166
|
}
|
|
@@ -193,15 +195,15 @@ export async function remove(current, { channel, key, ...conf }) {
|
|
|
193
195
|
const headers = headersOf(current);
|
|
194
196
|
const { removed } = await gameValuesRemove(id, where, [named], headers).catch(refused(id, where));
|
|
195
197
|
if (removed === 0) {
|
|
196
|
-
throw new ArcError(
|
|
198
|
+
throw new ArcError(VALUE_NOT_FOUND, valueNotFound({ channel: where, key: named }));
|
|
197
199
|
}
|
|
198
200
|
return { channel: where, keys: [named] };
|
|
199
201
|
}
|
|
200
202
|
// [function]
|
|
201
203
|
// what each scope of the game's values holds on a channel, against what it
|
|
202
204
|
// may: the developer's, the game's, its players', and its server's. And the
|
|
203
|
-
// calls the game's clients made today on that channel, with the game's
|
|
204
|
-
//
|
|
205
|
+
// calls the game's clients made today on that channel, with the game's
|
|
206
|
+
// day, its month, and its account's pool, as the API states them; null when they
|
|
205
207
|
// can't be had, which fails nothing.
|
|
206
208
|
export async function usage(current, { channel, ...conf }) {
|
|
207
209
|
const where = needOf(channel);
|
package/dist/action/wire/tool.js
CHANGED
|
@@ -5,7 +5,7 @@ import { gameWireUsages } from "../../api.js";
|
|
|
5
5
|
import { GAME_UNAVAILABLE } from "../../config/code.js";
|
|
6
6
|
import { ArcError } from "../../util/error.js";
|
|
7
7
|
import { channelOf, CHANNELS, gameOf, headersOf, } from "../game.js";
|
|
8
|
-
import {
|
|
8
|
+
import { gameNotOwned } from "../../phrase/index.js";
|
|
9
9
|
// [function]
|
|
10
10
|
// what the game's wires carried on each channel, or on one: this month's,
|
|
11
11
|
// and today's of it.
|
|
@@ -16,7 +16,7 @@ export async function usage(current, { channel, ...conf }) {
|
|
|
16
16
|
// its owner always reads today's row for each channel: no rows at all is
|
|
17
17
|
// a game that isn't the caller's
|
|
18
18
|
if (rows.length === 0) {
|
|
19
|
-
throw new ArcError(GAME_UNAVAILABLE,
|
|
19
|
+
throw new ArcError(GAME_UNAVAILABLE, gameNotOwned(id));
|
|
20
20
|
}
|
|
21
21
|
const today = new Date().toISOString().slice(0, 10);
|
|
22
22
|
const sum = (of) => ({
|