arcadible 1.0.35 → 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/function/logs.js +238 -0
- package/dist/action/function/tool.js +40 -16
- package/dist/action/value/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/value/usage.js +15 -5
- package/dist/commands.js +1 -1
- package/dist/phrase/function.js +65 -11
- package/dist/runner/entry.js +59 -3
- package/dist/serve/runner.js +9 -2
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
|
@@ -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
|
+
}
|
|
@@ -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) {
|
|
@@ -68,7 +73,7 @@ export async function pause(current, { channel, ...conf }) {
|
|
|
68
73
|
// resumes the game's function on a channel: lifts its owner's pause, and
|
|
69
74
|
// resolves with `{ channel, lifted, paused }`, `paused` the reason it's
|
|
70
75
|
// still paused for, or null. One that isn't paused stays as it was, as `pause` leaves one
|
|
71
|
-
// that's paused already. A pause of Arcadible's, or
|
|
76
|
+
// that's paused already. A pause of Arcadible's, or a limit's, isn't
|
|
72
77
|
// its owner's to lift: function_pause_reserved, saying who lifts it.
|
|
73
78
|
export async function resume(current, { channel, ...conf }) {
|
|
74
79
|
const where = needOf(channel);
|
|
@@ -94,10 +99,10 @@ export async function resume(current, { channel, ...conf }) {
|
|
|
94
99
|
// the game's function on each channel, or on one: `{ channel, function,
|
|
95
100
|
// paused, requests }`, `function` whether the channel's live build has one
|
|
96
101
|
// (a pause and a usage are the channel's, so neither says), `paused` the
|
|
97
|
-
// reason it's paused for, or null for none,
|
|
98
|
-
// this month. With them,
|
|
99
|
-
//
|
|
100
|
-
//
|
|
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.
|
|
101
106
|
export async function status(current, { channel, ...conf }) {
|
|
102
107
|
const where = channelOf(channel);
|
|
103
108
|
const id = await gameOf(conf);
|
|
@@ -112,17 +117,36 @@ export async function status(current, { channel, ...conf }) {
|
|
|
112
117
|
const has = await Promise.all(shown.map((each) => hasFunction(id, each)));
|
|
113
118
|
const rows = usages ?? [];
|
|
114
119
|
const [first] = rows;
|
|
115
|
-
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);
|
|
116
123
|
return {
|
|
117
|
-
channels: shown.map((each, i) =>
|
|
118
|
-
channel
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
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,
|
|
127
151
|
};
|
|
128
152
|
}
|
|
@@ -202,8 +202,8 @@ export async function remove(current, { channel, key, ...conf }) {
|
|
|
202
202
|
// [function]
|
|
203
203
|
// what each scope of the game's values holds on a channel, against what it
|
|
204
204
|
// may: the developer's, the game's, its players', and its server's. And the
|
|
205
|
-
// calls the game's clients made today on that channel, with the game's
|
|
206
|
-
//
|
|
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
|
|
207
207
|
// can't be had, which fails nothing.
|
|
208
208
|
export async function usage(current, { channel, ...conf }) {
|
|
209
209
|
const where = needOf(channel);
|
package/dist/api.js
CHANGED
|
@@ -88,23 +88,35 @@ const isValueListed = shaped({
|
|
|
88
88
|
const isValueRemoved = shaped({ removed: num });
|
|
89
89
|
const isValueUsage = shaped({
|
|
90
90
|
bytes: num,
|
|
91
|
-
bytes_limit: num,
|
|
91
|
+
bytes_limit: nul(num),
|
|
92
92
|
bytes_most: num,
|
|
93
93
|
channel: str,
|
|
94
94
|
keys: num,
|
|
95
|
-
keys_limit: num,
|
|
95
|
+
keys_limit: nul(num),
|
|
96
96
|
keys_most: num,
|
|
97
97
|
owners: num,
|
|
98
98
|
scope: str,
|
|
99
99
|
});
|
|
100
100
|
const isValueCalls = shaped({
|
|
101
|
-
allowance: num,
|
|
101
|
+
allowance: nul(num),
|
|
102
102
|
channel: str,
|
|
103
|
+
month_used: num,
|
|
104
|
+
pool_allowance: nul(num),
|
|
105
|
+
pool_used: num,
|
|
103
106
|
reads: num,
|
|
104
107
|
used: num,
|
|
105
108
|
writes: num,
|
|
106
109
|
});
|
|
107
|
-
const
|
|
110
|
+
const isLogsToken = shaped({
|
|
111
|
+
expires_at: str,
|
|
112
|
+
token: str,
|
|
113
|
+
url: str,
|
|
114
|
+
});
|
|
115
|
+
const isPause = shaped({
|
|
116
|
+
channel: str,
|
|
117
|
+
reason: str,
|
|
118
|
+
resumes_at: opt(nul(str)),
|
|
119
|
+
});
|
|
108
120
|
const isWireUsage = shaped({
|
|
109
121
|
channel: str,
|
|
110
122
|
connections: opt(nul(either(num, str))),
|
|
@@ -112,9 +124,16 @@ const isWireUsage = shaped({
|
|
|
112
124
|
messages: opt(nul(either(num, str))),
|
|
113
125
|
});
|
|
114
126
|
const isUsage = shaped({
|
|
115
|
-
allowance: either(num, str),
|
|
127
|
+
allowance: nul(either(num, str)),
|
|
116
128
|
channel: str,
|
|
129
|
+
cpu_ms: opt(nul(either(num, str))),
|
|
130
|
+
month_cpu_ms: opt(nul(either(num, str))),
|
|
131
|
+
month_used: either(num, str),
|
|
132
|
+
pool_allowance: nul(either(num, str)),
|
|
133
|
+
pool_cpu_ms: opt(nul(either(num, str))),
|
|
134
|
+
pool_used: either(num, str),
|
|
117
135
|
requests: opt(nul(either(num, str))),
|
|
136
|
+
used: either(num, str),
|
|
118
137
|
});
|
|
119
138
|
// [function]
|
|
120
139
|
// the API's answer, checked as it arrives: `is` says whether it has the
|
|
@@ -306,6 +325,12 @@ export function gameFunctionUsages(id, params, headers) {
|
|
|
306
325
|
return answer(listed(isUsage), "a function's usage", get(endpoint(pathOf `/games/${id}/function_usages`, params), headers));
|
|
307
326
|
}
|
|
308
327
|
// [function]
|
|
328
|
+
// a token to read the caller's own game's function's logs, good for 10
|
|
329
|
+
// minutes.
|
|
330
|
+
export function postGameFunctionLogsToken(id, params, headers) {
|
|
331
|
+
return answer(isLogsToken, "a token for a function's logs", post(endpoint(pathOf `/games/${id}/function_logs/token`, params), null, headers));
|
|
332
|
+
}
|
|
333
|
+
// [function]
|
|
309
334
|
// the usage of the caller's own game's wires this month: a row a channel
|
|
310
335
|
// and a day.
|
|
311
336
|
export function gameWireUsages(id, params, headers) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// arc function: a game's function, the server code Arcadible runs for it:
|
|
2
|
-
// paused and resumed on a channel by the game's owner, with no release,
|
|
3
|
-
// its secrets (./secret/index.ts). All of it is left out of `arc --help`
|
|
2
|
+
// paused and resumed on a channel by the game's owner, with no release, its
|
|
3
|
+
// logs, and its secrets (./secret/index.ts). All of it is left out of `arc --help`
|
|
4
4
|
// until functions ship, with the docs.
|
|
5
5
|
// [import]
|
|
6
6
|
import { defineCommand } from "../../cli/command.js";
|
|
@@ -8,10 +8,11 @@ import secret from "./secret/index.js";
|
|
|
8
8
|
import pause from "./pause.js";
|
|
9
9
|
import resume from "./resume.js";
|
|
10
10
|
import status from "./status.js";
|
|
11
|
+
import logs from "./logs.js";
|
|
11
12
|
// [export]
|
|
12
13
|
export default defineCommand({
|
|
13
|
-
commands: [pause, resume, status, secret],
|
|
14
|
+
commands: [pause, resume, status, logs, secret],
|
|
14
15
|
hidden: true,
|
|
15
16
|
name: "function",
|
|
16
|
-
summary: "Pause, resume, and show the game's function, and its secrets",
|
|
17
|
+
summary: "Pause, resume, and show the game's function, its logs, and its secrets",
|
|
17
18
|
});
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// arc function logs: what the game's function said, as Arcadible keeps it,
|
|
2
|
+
// for its owner (plan/FUNCTION-LOGS.md, Reading them): the newest 100
|
|
3
|
+
// requests, oldest first, as `tail` prints, or those and then each as it
|
|
4
|
+
// comes, with `--follow`. With `--json`, an entry a line, as the logs
|
|
5
|
+
// Worker answers it, so a follow can be read as it goes (OUTPUT.md,
|
|
6
|
+
// Streams).
|
|
7
|
+
// [import]
|
|
8
|
+
import { resolve as resolvePath } from "node:path";
|
|
9
|
+
// [import]
|
|
10
|
+
import chalk from "chalk";
|
|
11
|
+
// [import]
|
|
12
|
+
import { ended } from "../../account.js";
|
|
13
|
+
import { defineCommand } from "../../cli/command.js";
|
|
14
|
+
import { of } from "../../cli/channel.js";
|
|
15
|
+
import { CHANNEL_SHOWS, CWD, JSON_OUTPUT } from "../../cli/options.js";
|
|
16
|
+
import { cpuTime, functionLogsNone } from "../../phrase/index.js";
|
|
17
|
+
import { p } from "../../util/log.js";
|
|
18
|
+
import { deepSort } from "../../util/objs.js";
|
|
19
|
+
import { SpinnerWheel } from "../../util/spinner.js";
|
|
20
|
+
import { follow, logs, } from "../../action/function/logs.js";
|
|
21
|
+
import { whoami } from "../../action/whoami/tool.js";
|
|
22
|
+
// [function]
|
|
23
|
+
// a time as the logs show it, in UTC: to the millisecond, with its day
|
|
24
|
+
// when it isn't today's.
|
|
25
|
+
function timeOf(at, now = Date.now()) {
|
|
26
|
+
const iso = new Date(at).toISOString();
|
|
27
|
+
const day = iso.slice(0, 10);
|
|
28
|
+
const time = iso.slice(11, 23);
|
|
29
|
+
return day === new Date(now).toISOString().slice(0, 10)
|
|
30
|
+
? time
|
|
31
|
+
: `${day} ${time}`;
|
|
32
|
+
}
|
|
33
|
+
// [function]
|
|
34
|
+
// text under an entry's first line, indented, each of its lines.
|
|
35
|
+
function under(text, indent) {
|
|
36
|
+
return text
|
|
37
|
+
.split("\n")
|
|
38
|
+
.map((line) => `${indent}${line}`)
|
|
39
|
+
.join("\n");
|
|
40
|
+
}
|
|
41
|
+
// [function]
|
|
42
|
+
// an exception: its name and message, then its stack, which Cloudflare
|
|
43
|
+
// gives as its frames alone, and V8 with the name and message first.
|
|
44
|
+
function exceptionText({ message, name, stack, }) {
|
|
45
|
+
const head = `${name ?? "Error"}: ${message ?? ""}`;
|
|
46
|
+
if (!stack)
|
|
47
|
+
return head;
|
|
48
|
+
return stack.startsWith(head) ? stack : `${head}\n${stack}`;
|
|
49
|
+
}
|
|
50
|
+
// [function]
|
|
51
|
+
// an entry as it's read: its time, channel, build, request, answer, and CPU
|
|
52
|
+
// time on a line, then its lines and exceptions under it, those that are
|
|
53
|
+
// errors in yellow (OUTPUT.md keeps red for headings).
|
|
54
|
+
export function entryText(entry) {
|
|
55
|
+
const outcome = entry.outcome ?? "unknown";
|
|
56
|
+
const answer = entry.status == null
|
|
57
|
+
? outcome
|
|
58
|
+
: outcome === "ok"
|
|
59
|
+
? String(entry.status)
|
|
60
|
+
: `${entry.status} ${outcome}`;
|
|
61
|
+
const head = [
|
|
62
|
+
timeOf(entry.at),
|
|
63
|
+
entry.channel,
|
|
64
|
+
entry.version,
|
|
65
|
+
entry.method ?? "-",
|
|
66
|
+
entry.path ?? "-",
|
|
67
|
+
answer,
|
|
68
|
+
`${cpuTime(entry.cpu_ms ?? 0)} CPU`,
|
|
69
|
+
].join(" ");
|
|
70
|
+
const body = [
|
|
71
|
+
...(entry.lines ?? []).map((line) => {
|
|
72
|
+
const level = line.level ?? "log";
|
|
73
|
+
// a message's later lines under its first
|
|
74
|
+
const text = ` ${level.padEnd(5)} ${under(line.message ?? "", " ".repeat(8)).trimStart()}`;
|
|
75
|
+
return level === "error" ? chalk.yellow(text) : text;
|
|
76
|
+
}),
|
|
77
|
+
...(entry.exceptions ?? []).map((each) => chalk.yellow(under(exceptionText(each), " "))),
|
|
78
|
+
...(entry.truncated ? [" … more than was kept"] : []),
|
|
79
|
+
];
|
|
80
|
+
return [head, ...body].join("\n");
|
|
81
|
+
}
|
|
82
|
+
// [function]
|
|
83
|
+
// an entry, as JSON on a line of its own, or as it's read.
|
|
84
|
+
function show(entry, json) {
|
|
85
|
+
if (json) {
|
|
86
|
+
process.stdout.write(`${JSON.stringify(deepSort(entry))}\n`);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
p(entryText(entry));
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
// [export]
|
|
93
|
+
export default defineCommand({
|
|
94
|
+
name: "logs",
|
|
95
|
+
options: {
|
|
96
|
+
...CHANNEL_SHOWS,
|
|
97
|
+
// a build, by its version: `--version` is arc's own, as in arc play
|
|
98
|
+
release: {
|
|
99
|
+
alias: "r",
|
|
100
|
+
describe: "Only this release's, by its version",
|
|
101
|
+
type: "string",
|
|
102
|
+
},
|
|
103
|
+
since: {
|
|
104
|
+
describe: "From a time: 10m, 2h, 1d, or an ISO time",
|
|
105
|
+
type: "string",
|
|
106
|
+
},
|
|
107
|
+
errors: {
|
|
108
|
+
describe: "Only the requests that failed",
|
|
109
|
+
type: "boolean",
|
|
110
|
+
},
|
|
111
|
+
follow: {
|
|
112
|
+
alias: "f",
|
|
113
|
+
describe: "Keep printing each request as it comes",
|
|
114
|
+
type: "boolean",
|
|
115
|
+
},
|
|
116
|
+
json: JSON_OUTPUT,
|
|
117
|
+
cwd: CWD,
|
|
118
|
+
},
|
|
119
|
+
async run(argv, aborting) {
|
|
120
|
+
const json = argv.json;
|
|
121
|
+
const sw = new SpinnerWheel("Checking… %s");
|
|
122
|
+
aborting.sw = sw;
|
|
123
|
+
const where = of(argv);
|
|
124
|
+
const current = await whoami({}, {
|
|
125
|
+
onAccountRefresh(refresh) {
|
|
126
|
+
sw.start(json);
|
|
127
|
+
return refresh();
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
sw.stop(true);
|
|
131
|
+
const conf = {
|
|
132
|
+
channel: where,
|
|
133
|
+
errors: argv.errors,
|
|
134
|
+
root: resolvePath(process.cwd(), argv.cwd),
|
|
135
|
+
since: argv.since,
|
|
136
|
+
version: argv.release,
|
|
137
|
+
};
|
|
138
|
+
if (argv.follow) {
|
|
139
|
+
await follow(current, conf, (entry) => {
|
|
140
|
+
show(entry, json);
|
|
141
|
+
}).catch(ended(current));
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
const entries = await logs(current, conf).catch(ended(current));
|
|
145
|
+
for (const entry of entries.reverse())
|
|
146
|
+
show(entry, json);
|
|
147
|
+
if (!entries.length && !json) {
|
|
148
|
+
p(functionLogsNone({
|
|
149
|
+
filtered: Boolean(where || argv.release || argv.since || argv.errors),
|
|
150
|
+
}));
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
summary: "Show what the game's function logged, and its failures",
|
|
154
|
+
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// [import]
|
|
2
2
|
import { defineCommand } from "../../cli/command.js";
|
|
3
|
-
import {
|
|
3
|
+
import { cpuTime, functionState, functionUsage } from "../../phrase/index.js";
|
|
4
4
|
import { p } from "../../util/log.js";
|
|
5
5
|
import { CHANNEL_SHOWS, CWD, JSON_OUTPUT, QUIET } from "../../cli/options.js";
|
|
6
6
|
import { run } from "./run.js";
|
|
@@ -12,14 +12,16 @@ function count(value) {
|
|
|
12
12
|
return value.toLocaleString("en-US");
|
|
13
13
|
}
|
|
14
14
|
// [function]
|
|
15
|
-
// each channel's function as a table, a row each
|
|
15
|
+
// each channel's function as a table, a row each: its requests and CPU
|
|
16
|
+
// time this month.
|
|
16
17
|
function rows(channels) {
|
|
17
18
|
return table([
|
|
18
|
-
["CHANNEL", "STATUS", "REQUESTS"],
|
|
19
|
+
["CHANNEL", "STATUS", "REQUESTS", "CPU"],
|
|
19
20
|
...channels.map((each) => [
|
|
20
21
|
each.channel,
|
|
21
22
|
functionState(each),
|
|
22
23
|
count(each.requests),
|
|
24
|
+
cpuTime(each.cpu_ms),
|
|
23
25
|
]),
|
|
24
26
|
]);
|
|
25
27
|
}
|
|
@@ -28,13 +30,28 @@ export default defineCommand({
|
|
|
28
30
|
name: "status",
|
|
29
31
|
options: { ...CHANNEL_SHOWS, json: JSON_OUTPUT, quiet: QUIET, cwd: CWD },
|
|
30
32
|
run(argv, aborting) {
|
|
31
|
-
return run(argv, aborting, status, ({ channels, month }) => {
|
|
33
|
+
return run(argv, aborting, status, ({ channels, day, month, pool }) => {
|
|
32
34
|
p(rows(channels));
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
// nothing to say of its limits when its usage couldn't be read
|
|
36
|
+
if (day && month && pool) {
|
|
37
|
+
const limit = (value) => value === null ? null : count(value);
|
|
38
|
+
p(functionUsage({
|
|
39
|
+
day: {
|
|
40
|
+
allowance: limit(day.allowance),
|
|
41
|
+
requests: count(day.requests),
|
|
42
|
+
},
|
|
43
|
+
month: {
|
|
44
|
+
cpu: cpuTime(month.cpu_ms),
|
|
45
|
+
requests: count(month.requests),
|
|
46
|
+
},
|
|
47
|
+
pool: {
|
|
48
|
+
allowance: limit(pool.allowance),
|
|
49
|
+
cpu: cpuTime(pool.cpu_ms),
|
|
50
|
+
requests: count(pool.requests),
|
|
51
|
+
},
|
|
52
|
+
}));
|
|
53
|
+
}
|
|
37
54
|
});
|
|
38
55
|
},
|
|
39
|
-
summary: "Show whether the game's function is running, and its usage
|
|
56
|
+
summary: "Show whether the game's function is running, and its usage against its limits",
|
|
40
57
|
});
|
|
@@ -32,7 +32,9 @@ function rows(scopes) {
|
|
|
32
32
|
["SCOPE", "KEYS", "SIZE", "MAY HOLD"],
|
|
33
33
|
...scopes.map((each) => {
|
|
34
34
|
const players = each.scope === "player";
|
|
35
|
-
const limit =
|
|
35
|
+
const limit = each.keys_limit === null && each.bytes_limit === null
|
|
36
|
+
? "no limit"
|
|
37
|
+
: `${each.keys_limit === null ? "any number of" : each.keys_limit.toLocaleString("en-US")} keys, ${each.bytes_limit === null ? "any size" : size(each.bytes_limit)}`;
|
|
36
38
|
return [
|
|
37
39
|
NAMES[each.scope] ?? each.scope,
|
|
38
40
|
players
|
|
@@ -41,17 +43,25 @@ function rows(scopes) {
|
|
|
41
43
|
players
|
|
42
44
|
? `${size(each.bytes)} (${size(each.bytes_most)} at most)`
|
|
43
45
|
: size(each.bytes),
|
|
44
|
-
players ? `${limit} each` : limit,
|
|
46
|
+
players && limit !== "no limit" ? `${limit} each` : limit,
|
|
45
47
|
];
|
|
46
48
|
}),
|
|
47
49
|
]);
|
|
48
50
|
}
|
|
49
51
|
// [function]
|
|
50
|
-
// the game's clients' calls
|
|
51
|
-
// its
|
|
52
|
+
// the game's clients' calls: this channel's today, and the game's against
|
|
53
|
+
// its own limit for a day, which both channels share and the day (UTC)
|
|
54
|
+
// renews; then its month, and its account's, every game's together,
|
|
55
|
+
// against its plan's. Each number is the API's; null is no limit.
|
|
52
56
|
function called(calls) {
|
|
53
57
|
const n = (count) => count.toLocaleString("en-US");
|
|
54
|
-
|
|
58
|
+
const today = calls.allowance === null
|
|
59
|
+
? `${n(calls.used)} for the day, with no limit.`
|
|
60
|
+
: `${n(calls.used)} of ${n(calls.allowance)} for the day, both channels together.`;
|
|
61
|
+
const account = calls.pool_allowance === null
|
|
62
|
+
? `Your account's games together: ${n(calls.pool_used)}, with no limit.`
|
|
63
|
+
: `Your account's games together: ${n(calls.pool_used)} of ${n(calls.pool_allowance)}.`;
|
|
64
|
+
return `Calls from the game today: ${n(calls.reads)} reads, ${n(calls.writes)} writes on ${calls.channel}. ${today}\n${n(calls.month_used)} calls this month. ${account}`;
|
|
55
65
|
}
|
|
56
66
|
// [export]
|
|
57
67
|
export default defineCommand({
|
package/dist/commands.js
CHANGED
|
@@ -83,7 +83,7 @@ export const COMMANDS = [
|
|
|
83
83
|
hidden: true,
|
|
84
84
|
load: () => import("./command/function/index.js"),
|
|
85
85
|
name: "function",
|
|
86
|
-
summary: "Pause, resume, and show the game's function, and its secrets",
|
|
86
|
+
summary: "Pause, resume, and show the game's function, its logs, and its secrets",
|
|
87
87
|
},
|
|
88
88
|
{
|
|
89
89
|
hidden: true,
|
package/dist/phrase/function.js
CHANGED
|
@@ -22,11 +22,15 @@ export function functionPaused({ channel, function: has, }) {
|
|
|
22
22
|
return `Paused the game's function on ${channel}. It stops answering within a minute.`;
|
|
23
23
|
}
|
|
24
24
|
// [function]
|
|
25
|
-
// why a function is paused, as a status says it.
|
|
26
|
-
|
|
25
|
+
// why a function is paused, as a status says it. A limit's pause ends by
|
|
26
|
+
// itself, as the day or the month turns (UTC): `resumes` is when, as the
|
|
27
|
+
// API states it.
|
|
28
|
+
export function functionPause(reason, resumes) {
|
|
27
29
|
const reasons = {
|
|
28
30
|
arcadible: "paused by Arcadible",
|
|
29
|
-
limit:
|
|
31
|
+
limit: resumes
|
|
32
|
+
? `at a limit until ${resumes.slice(0, 10)} (UTC)`
|
|
33
|
+
: "at a limit",
|
|
30
34
|
owner: "paused by you",
|
|
31
35
|
};
|
|
32
36
|
return reasons[reason] ?? "paused";
|
|
@@ -35,8 +39,8 @@ export function functionPause(reason) {
|
|
|
35
39
|
// a channel's function, as a status says it: that there's none, when its
|
|
36
40
|
// live build has none or it has no build, with any pause that waits for one;
|
|
37
41
|
// else why it's paused, or that it's running.
|
|
38
|
-
export function functionState({ function: has, paused, }) {
|
|
39
|
-
const pause = paused ? functionPause(paused) : null;
|
|
42
|
+
export function functionState({ function: has, paused, resumes_at: resumes, }) {
|
|
43
|
+
const pause = paused ? functionPause(paused, resumes) : null;
|
|
40
44
|
if (has === false) {
|
|
41
45
|
return pause ? `no function, ${pause}` : "no function";
|
|
42
46
|
}
|
|
@@ -54,11 +58,38 @@ export function functionResumed({ channel, lifted, paused, }) {
|
|
|
54
58
|
return `Resumed the game's function on ${channel}. It answers again within a minute.`;
|
|
55
59
|
}
|
|
56
60
|
// [function]
|
|
57
|
-
// a game's function
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
61
|
+
// what holds a game's function, as a status says it under its channels:
|
|
62
|
+
// the game's requests today against its own limit for a day, then its
|
|
63
|
+
// month and its account's, every game's together, against its plan's, then
|
|
64
|
+
// the CPU time of both, which no limit holds. Each number is the API's;
|
|
65
|
+
// null is no limit.
|
|
66
|
+
export function functionUsage({ day, month, pool, }) {
|
|
67
|
+
const today = day.allowance === null
|
|
68
|
+
? `${day.requests} requests today, with no limit for a day.`
|
|
69
|
+
: `${day.requests} of ${day.allowance} requests today, both channels together.`;
|
|
70
|
+
const account = pool.allowance === null
|
|
71
|
+
? `Your account's games together: ${pool.requests}, with no limit.`
|
|
72
|
+
: `Your account's games together: ${pool.requests} of ${pool.allowance}.`;
|
|
73
|
+
const cpu = month.cpu && pool.cpu
|
|
74
|
+
? `\nCPU time this month: ${month.cpu}. Your account's games together: ${pool.cpu}.`
|
|
75
|
+
: "";
|
|
76
|
+
return `${today}\n${month.requests} requests this month. ${account}${cpu}`;
|
|
77
|
+
}
|
|
78
|
+
// [function]
|
|
79
|
+
// a function's CPU time, given in milliseconds, as it's read: "640 ms",
|
|
80
|
+
// "1.2 s", "4.5 min", "1,234.5 h".
|
|
81
|
+
export function cpuTime(ms) {
|
|
82
|
+
const one = (value) => value.toLocaleString("en-US", {
|
|
83
|
+
maximumFractionDigits: 1,
|
|
84
|
+
minimumFractionDigits: 1,
|
|
85
|
+
});
|
|
86
|
+
if (ms < 1000)
|
|
87
|
+
return `${Math.round(ms)} ms`;
|
|
88
|
+
if (ms < 60 * 1000)
|
|
89
|
+
return `${one(ms / 1000)} s`;
|
|
90
|
+
if (ms < 60 * 60 * 1000)
|
|
91
|
+
return `${one(ms / 60 / 1000)} min`;
|
|
92
|
+
return `${one(ms / 60 / 60 / 1000)} h`;
|
|
62
93
|
}
|
|
63
94
|
// [function]
|
|
64
95
|
// a resume with no pause of the owner's to lift, and one of another's: who
|
|
@@ -68,7 +99,7 @@ export function functionPauseReserved({ channel, paused, }) {
|
|
|
68
99
|
return blocks(`The game's function is paused by Arcadible on ${channel}.`, "Only Arcadible can resume it.");
|
|
69
100
|
}
|
|
70
101
|
if (paused === "limit") {
|
|
71
|
-
return blocks(`The game's function
|
|
102
|
+
return blocks(`The game's function is at a limit on ${channel}.`, "It resumes by itself as the day or the month turns (UTC): `arc function status` says when.");
|
|
72
103
|
}
|
|
73
104
|
return `The game's function is paused on ${channel}, and not by you.`;
|
|
74
105
|
}
|
|
@@ -153,3 +184,26 @@ export function runnerFailed(message) {
|
|
|
153
184
|
export function runnerLabel() {
|
|
154
185
|
return "Function:";
|
|
155
186
|
}
|
|
187
|
+
// [function]
|
|
188
|
+
// no entry that matches: what's kept, and where else lines are printed.
|
|
189
|
+
export function functionLogsNone({ filtered }) {
|
|
190
|
+
return blocks(filtered ? "No logs that match." : "No logs yet.", "Arcadible keeps a function's logs for 7 days.\nUnder `arc serve`, its lines are printed where it runs.");
|
|
191
|
+
}
|
|
192
|
+
// [function]
|
|
193
|
+
export function functionLogsSinceInvalid(text) {
|
|
194
|
+
return blocks(`Not a time to read from: ${text}`, "--since takes a span back from now (10m, 2h, 1d), or an ISO time.");
|
|
195
|
+
}
|
|
196
|
+
// [function]
|
|
197
|
+
export function functionLogsVersionInvalid(text) {
|
|
198
|
+
return blocks(`Not a version: ${text}`, "--release takes a release's version, such as 1.2.0.");
|
|
199
|
+
}
|
|
200
|
+
// [function]
|
|
201
|
+
// a sixth follow of a game's logs, while five are open.
|
|
202
|
+
export function functionLogsBusy() {
|
|
203
|
+
return blocks("The game's logs are followed five times already.", "Stop one of them, then try again.");
|
|
204
|
+
}
|
|
205
|
+
// [function]
|
|
206
|
+
// the logs' refusal, which the CLI didn't expect: its status and code.
|
|
207
|
+
export function functionLogsRefused({ code, status, }) {
|
|
208
|
+
return `Unable to read the function's logs (${status}${code ? `, ${code}` : ""}).`;
|
|
209
|
+
}
|
package/dist/runner/entry.js
CHANGED
|
@@ -25,14 +25,58 @@ export const FUNCTION_MODULE = "function.js";
|
|
|
25
25
|
// address in a `process.env` of the entry's making. A function's secrets
|
|
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
|
-
// the developer's code.
|
|
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
33
|
export const ENTRY = `import handler from "./${FUNCTION_MODULE}";
|
|
30
34
|
const CLAIM = "x-arcadible-claim";
|
|
31
35
|
const SECRETS = "x-arcadible-secrets";
|
|
32
36
|
const API = "https://api.arcadible.com/v1/server";
|
|
37
|
+
// a secret shorter than this isn't hidden in what's logged: it would be
|
|
38
|
+
// found in ordinary words
|
|
39
|
+
const SHORTEST = 8;
|
|
33
40
|
// the newest claim, and arc serve's address under it
|
|
34
41
|
let claimed = null;
|
|
35
42
|
let serve = null;
|
|
43
|
+
// the secrets' values the code has been given, each with its name, longest
|
|
44
|
+
// first, so one inside another is hidden as the longer
|
|
45
|
+
const hidden = [];
|
|
46
|
+
const remember = (values) => {
|
|
47
|
+
let added = false;
|
|
48
|
+
for (const [name, value] of Object.entries(values)) {
|
|
49
|
+
if (typeof value !== "string" || value.length < SHORTEST) continue;
|
|
50
|
+
if (hidden.some(([known]) => known === value)) continue;
|
|
51
|
+
hidden.push([value, name]);
|
|
52
|
+
added = true;
|
|
53
|
+
}
|
|
54
|
+
if (added) hidden.sort((a, b) => b[0].length - a[0].length);
|
|
55
|
+
};
|
|
56
|
+
const hide = (text) => {
|
|
57
|
+
for (const [value, name] of hidden) text = text.split(value).join("[secret " + name + "]");
|
|
58
|
+
return text;
|
|
59
|
+
};
|
|
60
|
+
// one of console's arguments with any secret in it hidden: a string as it
|
|
61
|
+
// is, an error as its stack, anything else as JSON, and as the console
|
|
62
|
+
// would show it when no secret's in it
|
|
63
|
+
const shown = (value) => {
|
|
64
|
+
if (typeof value === "string") return hide(value);
|
|
65
|
+
let text;
|
|
66
|
+
try {
|
|
67
|
+
text = value instanceof Error ? String(value.stack ?? value) : JSON.stringify(value);
|
|
68
|
+
} catch {
|
|
69
|
+
return value;
|
|
70
|
+
}
|
|
71
|
+
if (typeof text !== "string") return value;
|
|
72
|
+
const safe = hide(text);
|
|
73
|
+
return safe === text ? value : safe;
|
|
74
|
+
};
|
|
75
|
+
for (const level of ["debug", "error", "info", "log", "trace", "warn"]) {
|
|
76
|
+
const original = console[level];
|
|
77
|
+
if (typeof original !== "function") continue;
|
|
78
|
+
console[level] = (...args) => original.apply(console, hidden.length > 0 ? args.map(shown) : args);
|
|
79
|
+
}
|
|
36
80
|
globalThis[Symbol.for("arcadible.function.api")] = {
|
|
37
81
|
version: 1,
|
|
38
82
|
async fetch(path, init = {}) {
|
|
@@ -68,7 +112,12 @@ export default {
|
|
|
68
112
|
if (secrets) {
|
|
69
113
|
const text = atob(secrets.replace(/-/g, "+").replace(/_/g, "/"));
|
|
70
114
|
const bytes = Uint8Array.from(text, (char) => char.charCodeAt(0));
|
|
71
|
-
|
|
115
|
+
const opened = JSON.parse(new TextDecoder().decode(bytes));
|
|
116
|
+
remember(opened);
|
|
117
|
+
given = { ...env, ...opened };
|
|
118
|
+
}
|
|
119
|
+
if (typeof env.ARCADIBLE_SECRETS === "string") {
|
|
120
|
+
remember(Object.fromEntries(env.ARCADIBLE_SECRETS.split(",").map((name) => [name, env[name]])));
|
|
72
121
|
}
|
|
73
122
|
if (claim || secrets) {
|
|
74
123
|
const headers = new Headers(request.headers);
|
|
@@ -79,7 +128,14 @@ export default {
|
|
|
79
128
|
if (typeof handler?.fetch !== "function") {
|
|
80
129
|
return new Response("The function's default export has no fetch(request).\\n", { status: 500 });
|
|
81
130
|
}
|
|
82
|
-
|
|
131
|
+
try {
|
|
132
|
+
return await handler.fetch(request, given, ctx);
|
|
133
|
+
} 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.");
|
|
138
|
+
}
|
|
83
139
|
},
|
|
84
140
|
};
|
|
85
141
|
`;
|
package/dist/serve/runner.js
CHANGED
|
@@ -196,12 +196,19 @@ async function runtime(dir, onInstall) {
|
|
|
196
196
|
// the manifest's, and to arc serve; only that worker reaches the network,
|
|
197
197
|
// this machine included, which arc serve is on, and HTTPS hosts, checked
|
|
198
198
|
// against the certificate authorities browsers trust. `secrets` are the
|
|
199
|
-
// values the code is given, each by its name
|
|
199
|
+
// values the code is given, each by its name, and named to the entry in
|
|
200
|
+
// ARCADIBLE_SECRETS.
|
|
200
201
|
function config({ hosts, port, secrets, serve, }) {
|
|
201
202
|
// each secret that has a value here, as the code's own binding
|
|
202
203
|
const given = Object.entries(secrets)
|
|
203
204
|
.map(([name, value]) => `, (name = ${JSON.stringify(name)}, text = ${JSON.stringify(value)})`)
|
|
204
205
|
.join("");
|
|
206
|
+
// which bindings are secrets, so the entry hides their values in what the
|
|
207
|
+
// code logs, as it does on Arcadible
|
|
208
|
+
const names = Object.keys(secrets);
|
|
209
|
+
const named = names.length > 0
|
|
210
|
+
? `, (name = "ARCADIBLE_SECRETS", text = ${JSON.stringify(names.join(","))})`
|
|
211
|
+
: "";
|
|
205
212
|
return `using Workerd = import "/workerd/workerd.capnp";
|
|
206
213
|
const config :Workerd.Config = (
|
|
207
214
|
services = [
|
|
@@ -223,7 +230,7 @@ const runner :Workerd.Worker = (
|
|
|
223
230
|
(name = "${FUNCTION_MODULE}", esModule = embed "${FUNCTION_MODULE}"),
|
|
224
231
|
],
|
|
225
232
|
compatibilityDate = "${COMPATIBILITY_DATE}",
|
|
226
|
-
bindings = [(name = "ARCADIBLE_SERVE", text = ${JSON.stringify(serve)})${given}],
|
|
233
|
+
bindings = [(name = "ARCADIBLE_SERVE", text = ${JSON.stringify(serve)})${given}${named}],
|
|
227
234
|
globalOutbound = "outbound",
|
|
228
235
|
);
|
|
229
236
|
const outbound :Workerd.Worker = (
|
package/npm-shrinkwrap.json
CHANGED
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
},
|
|
45
45
|
"license": "Apache-2.0",
|
|
46
46
|
"name": "arcadible",
|
|
47
|
-
"version": "1.0.
|
|
47
|
+
"version": "1.0.36"
|
|
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.
|
|
3270
|
+
"version": "1.0.36"
|
|
3271
3271
|
}
|
package/package.json
CHANGED