@wasit-dev/cli 0.7.0 → 0.8.0-beta.1
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/README.md +32 -6
- package/dist/ci-report-command.d.ts +40 -0
- package/dist/ci-report-command.js +123 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +245 -67
- package/dist/live.d.ts +68 -0
- package/dist/live.js +255 -0
- package/dist/output.d.ts +46 -0
- package/dist/output.js +115 -0
- package/dist/run-format.d.ts +7 -0
- package/dist/run-format.js +55 -0
- package/dist/run.d.ts +64 -0
- package/dist/run.js +443 -0
- package/dist/service.d.ts +78 -0
- package/dist/service.js +392 -0
- package/dist/style.d.ts +107 -0
- package/dist/style.js +195 -0
- package/package.json +3 -3
package/dist/service.js
ADDED
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `wasit run --start`: start the builder's service, wait until it answers, and
|
|
3
|
+
* stop it again (spec A8 #1, A-A02).
|
|
4
|
+
*
|
|
5
|
+
* Why this lives in the CLI and not in the Action: a composite action is one
|
|
6
|
+
* step for the workflow that uses it, so a variable set on the action reaches
|
|
7
|
+
* every step inside, the one that starts the service included. Here the
|
|
8
|
+
* child's environment is built in one place, from the parent's with the
|
|
9
|
+
* variables Wasit keeps away from a service removed (see `denied-env.ts` in
|
|
10
|
+
* core), and the same code runs on a laptop and in CI.
|
|
11
|
+
*
|
|
12
|
+
* Stopping is the part that must not fail quietly. A service is a shell command
|
|
13
|
+
* (`npm start`), so the process that starts is a shell and the service is its
|
|
14
|
+
* child or grandchild. On POSIX the command is started in a process group of
|
|
15
|
+
* its own and the whole group is signalled: SIGTERM first, SIGKILL after a grace
|
|
16
|
+
* period. The service is stopped when a run ends normally, when it fails, on
|
|
17
|
+
* SIGINT, SIGTERM and SIGHUP, and, as a last synchronous resort, when the Node
|
|
18
|
+
* process exits. A SIGKILL of Wasit itself cannot be caught.
|
|
19
|
+
*
|
|
20
|
+
* Windows: the group kill is `taskkill /pid <pid> /T /F`. That path has NOT been
|
|
21
|
+
* run on Windows; no Windows machine was available.
|
|
22
|
+
*/
|
|
23
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
24
|
+
import { existsSync, realpathSync } from "node:fs";
|
|
25
|
+
import { constants as osConstants } from "node:os";
|
|
26
|
+
import path from "node:path";
|
|
27
|
+
import { deniedEnvValues, isLoopbackUrl, payerKeyVariables, sanitizeText, scrubEnv, urlOrigin } from "@wasit-dev/core";
|
|
28
|
+
/**
|
|
29
|
+
* The OS user a service runs as when it must not share a user with the payer
|
|
30
|
+
* key (D-022a option 2). Fixed in code: a config or a variable never chooses it.
|
|
31
|
+
*/
|
|
32
|
+
export const SERVICE_USER = "wasit-service";
|
|
33
|
+
/**
|
|
34
|
+
* The user to run the service as, or undefined for the runner's own user.
|
|
35
|
+
*
|
|
36
|
+
* Najmi, 2026-11-16: a separate user only when a payer key variable is set AND
|
|
37
|
+
* the runner is GitHub-hosted (`RUNNER_ENVIRONMENT`, which GitHub documents as
|
|
38
|
+
* `github-hosted` or `self-hosted`; not re-read on the day). A hosted runner's
|
|
39
|
+
* user has passwordless sudo, which makes the user; a self-hosted runner or a
|
|
40
|
+
* laptop may not, and there the service runs as today. What this separates is
|
|
41
|
+
* the service's own processes from the Wasit process that holds the key: on
|
|
42
|
+
* Linux the environment of a process (`/proc/<pid>/environ`) is readable by its
|
|
43
|
+
* own user and root only. It does NOT separate code that ran earlier in the job
|
|
44
|
+
* as the runner's user (install scripts), which the guide says.
|
|
45
|
+
*/
|
|
46
|
+
export function serviceUserFor(env) {
|
|
47
|
+
return payerKeyVariables(env).length > 0 && env["RUNNER_ENVIRONMENT"] === "github-hosted" ? SERVICE_USER : undefined;
|
|
48
|
+
}
|
|
49
|
+
/** The service did not start, did not answer, or exited. Nothing was learned about the target: exit 2. */
|
|
50
|
+
export class ServiceError extends Error {
|
|
51
|
+
constructor(message) {
|
|
52
|
+
super(message);
|
|
53
|
+
this.name = "ServiceError";
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const SUDO_TIMEOUT_MS = 120_000;
|
|
57
|
+
/**
|
|
58
|
+
* Runs as the service's user: reads `{ command, env }` as JSON from stdin, runs
|
|
59
|
+
* the command with `sh -c` and exactly that environment, passes signals on and
|
|
60
|
+
* exits with the command's code. CommonJS, for `node -e`.
|
|
61
|
+
*/
|
|
62
|
+
const SERVICE_LAUNCHER = [
|
|
63
|
+
'let input = "";',
|
|
64
|
+
'process.stdin.setEncoding("utf8");',
|
|
65
|
+
'process.stdin.on("data", (chunk) => { input += chunk; });',
|
|
66
|
+
'process.stdin.on("end", () => {',
|
|
67
|
+
' let request;',
|
|
68
|
+
' try { request = JSON.parse(input); } catch { process.exit(64); }',
|
|
69
|
+
' const child = require("node:child_process").spawn("sh", ["-c", String(request.command)], { env: request.env, stdio: ["ignore", "inherit", "inherit"] });',
|
|
70
|
+
' for (const signal of ["SIGTERM", "SIGINT", "SIGHUP"]) process.on(signal, () => { try { child.kill(signal); } catch {} });',
|
|
71
|
+
' child.on("error", () => process.exit(127));',
|
|
72
|
+
' child.on("exit", (code) => process.exit(code === null ? 1 : code));',
|
|
73
|
+
'});',
|
|
74
|
+
].join("\n");
|
|
75
|
+
/** Runs `sudo -n <args>`, collects its stderr, and never throws: a sudo that cannot be run is code 127. */
|
|
76
|
+
function runSudo(sudo, args) {
|
|
77
|
+
return new Promise((resolve) => {
|
|
78
|
+
let stderr = "";
|
|
79
|
+
const child = spawn(sudo, ["-n", ...args], { stdio: ["ignore", "ignore", "pipe"], windowsHide: true });
|
|
80
|
+
const timer = setTimeout(() => child.kill("SIGKILL"), SUDO_TIMEOUT_MS);
|
|
81
|
+
child.stderr?.setEncoding("utf-8");
|
|
82
|
+
child.stderr?.on("data", (chunk) => {
|
|
83
|
+
if (stderr.length < 2000)
|
|
84
|
+
stderr += chunk;
|
|
85
|
+
});
|
|
86
|
+
child.once("error", () => {
|
|
87
|
+
clearTimeout(timer);
|
|
88
|
+
resolve({ code: 127, stderr: "sudo could not be run" });
|
|
89
|
+
});
|
|
90
|
+
child.once("close", (code) => {
|
|
91
|
+
clearTimeout(timer);
|
|
92
|
+
resolve({ code: code ?? 1, stderr });
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Makes the user when it does not exist, and lets it reach the working
|
|
98
|
+
* directory, by ACL for that one user: traverse (`x`) on each directory above
|
|
99
|
+
* it, read and write inside it, nothing on its `.git` (where actions/checkout
|
|
100
|
+
* keeps the job's token unless persist-credentials is false). Write access is
|
|
101
|
+
* given to the working directory only; beyond it the user can read what the
|
|
102
|
+
* directories already let any user read.
|
|
103
|
+
*/
|
|
104
|
+
async function prepareUser(user, cwd, secrets) {
|
|
105
|
+
const sudo = user.sudo ?? "sudo";
|
|
106
|
+
const home = `/home/${user.name}`;
|
|
107
|
+
const refuse = (what, result) => {
|
|
108
|
+
const said = sanitizeText(result.stderr.trim().replace(/\s+/g, " "), { redact: secrets }).slice(0, 300);
|
|
109
|
+
throw new ServiceError(`the service must run as a separate user (${user.name}) on a GitHub-hosted runner when a payer key is set, ` +
|
|
110
|
+
`and ${what} failed (sudo -n, exit ${result.code}${said === "" ? "" : `: ${said}`}). It was not started as ` +
|
|
111
|
+
`the runner's own user instead. This needs passwordless sudo, useradd and setfacl, as GitHub-hosted Linux runners have.`);
|
|
112
|
+
};
|
|
113
|
+
const exists = await runSudo(sudo, ["id", "-u", user.name]);
|
|
114
|
+
if (exists.code === 127)
|
|
115
|
+
refuse("running sudo", exists);
|
|
116
|
+
if (exists.code !== 0) {
|
|
117
|
+
const made = await runSudo(sudo, [
|
|
118
|
+
"useradd",
|
|
119
|
+
"--system",
|
|
120
|
+
"--create-home",
|
|
121
|
+
"--home-dir",
|
|
122
|
+
home,
|
|
123
|
+
"--shell",
|
|
124
|
+
"/usr/sbin/nologin",
|
|
125
|
+
user.name,
|
|
126
|
+
]);
|
|
127
|
+
if (made.code !== 0)
|
|
128
|
+
refuse(`making the user (useradd)`, made);
|
|
129
|
+
}
|
|
130
|
+
let directory;
|
|
131
|
+
try {
|
|
132
|
+
directory = realpathSync(cwd);
|
|
133
|
+
}
|
|
134
|
+
catch {
|
|
135
|
+
throw new ServiceError(`the working directory of the service cannot be read, so the separate user ${user.name} cannot be given it`);
|
|
136
|
+
}
|
|
137
|
+
const above = [];
|
|
138
|
+
for (let parent = path.dirname(directory); parent !== path.dirname(parent); parent = path.dirname(parent)) {
|
|
139
|
+
above.push(parent);
|
|
140
|
+
}
|
|
141
|
+
if (above.length > 0) {
|
|
142
|
+
const traverse = await runSudo(sudo, ["setfacl", "-m", `u:${user.name}:x`, ...above]);
|
|
143
|
+
if (traverse.code !== 0)
|
|
144
|
+
refuse("letting the user reach the working directory (setfacl)", traverse);
|
|
145
|
+
}
|
|
146
|
+
const grant = await runSudo(sudo, ["setfacl", "-R", "-m", `u:${user.name}:rwX,d:u:${user.name}:rwX`, directory]);
|
|
147
|
+
if (grant.code !== 0)
|
|
148
|
+
refuse("giving the user the working directory (setfacl)", grant);
|
|
149
|
+
const git = path.join(directory, ".git");
|
|
150
|
+
if (existsSync(git)) {
|
|
151
|
+
const deny = await runSudo(sudo, ["setfacl", "-R", "-m", `u:${user.name}:---`, git]);
|
|
152
|
+
if (deny.code !== 0)
|
|
153
|
+
refuse("keeping the user out of .git (setfacl)", deny);
|
|
154
|
+
}
|
|
155
|
+
return home;
|
|
156
|
+
}
|
|
157
|
+
const SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP"];
|
|
158
|
+
const PROBE_TIMEOUT_MS = 2000;
|
|
159
|
+
const KILL_WAIT_MS = 2000;
|
|
160
|
+
const MAX_PENDING_LINE = 8192;
|
|
161
|
+
function sleep(ms) {
|
|
162
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
163
|
+
}
|
|
164
|
+
async function waitFor(condition, ms) {
|
|
165
|
+
const end = Date.now() + ms;
|
|
166
|
+
while (!condition()) {
|
|
167
|
+
if (Date.now() >= end)
|
|
168
|
+
return false;
|
|
169
|
+
await sleep(25);
|
|
170
|
+
}
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
/** Any HTTP answer, whatever the status. Never follows a redirect. */
|
|
174
|
+
async function answers(url) {
|
|
175
|
+
try {
|
|
176
|
+
const response = await fetch(url, { redirect: "manual", signal: AbortSignal.timeout(PROBE_TIMEOUT_MS) });
|
|
177
|
+
await response.body?.cancel().catch(() => undefined);
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
export async function startService(options) {
|
|
185
|
+
const { command, ready } = options;
|
|
186
|
+
const origin = urlOrigin(ready.url);
|
|
187
|
+
// Defence in depth: the config loader holds the url to this machine, and so does this.
|
|
188
|
+
if (!isLoopbackUrl(ready.url)) {
|
|
189
|
+
throw new ServiceError("service.ready.url must be an address on this machine (localhost, 127.x.x.x or [::1]): " +
|
|
190
|
+
"the wait probes the service the run starts, nothing else");
|
|
191
|
+
}
|
|
192
|
+
const log = options.log ?? ((line) => console.error(line));
|
|
193
|
+
const secrets = deniedEnvValues(options.env);
|
|
194
|
+
const posix = process.platform !== "win32";
|
|
195
|
+
const maxLogBytes = options.maxLogBytes ?? 256 * 1024;
|
|
196
|
+
const separate = options.separateUser;
|
|
197
|
+
const sudo = separate?.sudo ?? "sudo";
|
|
198
|
+
const childEnv = scrubEnv(options.env);
|
|
199
|
+
let child;
|
|
200
|
+
if (separate === undefined) {
|
|
201
|
+
child = spawn(command, {
|
|
202
|
+
shell: true,
|
|
203
|
+
...(options.cwd === undefined ? {} : { cwd: options.cwd }),
|
|
204
|
+
env: childEnv,
|
|
205
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
206
|
+
// Its own process group, so the whole tree can be signalled at once.
|
|
207
|
+
detached: posix,
|
|
208
|
+
windowsHide: true,
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
if (!posix)
|
|
213
|
+
throw new ServiceError("a separate user for the service is built for Linux runners only");
|
|
214
|
+
const home = await prepareUser(separate, options.cwd ?? process.cwd(), secrets);
|
|
215
|
+
log(`[wasit] the service runs as the user ${separate.name}, not as the user that holds the payer key`);
|
|
216
|
+
// The service's environment and its command go on stdin, as JSON, to a small launcher
|
|
217
|
+
// that runs as that user: sudo resets the environment, and the hosted runner's sudo
|
|
218
|
+
// ignores -E (run 43815743541, 2026-11-16). A command line can be read by any user;
|
|
219
|
+
// stdin cannot. The launcher's own code is the only thing on the command line.
|
|
220
|
+
child = spawn(sudo, ["-n", "-u", separate.name, "--", process.execPath, "-e", SERVICE_LAUNCHER], {
|
|
221
|
+
...(options.cwd === undefined ? {} : { cwd: options.cwd }),
|
|
222
|
+
env: { PATH: childEnv["PATH"] ?? "/usr/local/bin:/usr/bin:/bin" },
|
|
223
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
224
|
+
detached: true,
|
|
225
|
+
windowsHide: true,
|
|
226
|
+
});
|
|
227
|
+
child.stdin?.on("error", () => undefined);
|
|
228
|
+
child.stdin?.end(JSON.stringify({
|
|
229
|
+
command,
|
|
230
|
+
env: {
|
|
231
|
+
...childEnv,
|
|
232
|
+
PATH: childEnv["PATH"] ?? "/usr/local/bin:/usr/bin:/bin",
|
|
233
|
+
HOME: home,
|
|
234
|
+
USER: separate.name,
|
|
235
|
+
LOGNAME: separate.name,
|
|
236
|
+
},
|
|
237
|
+
}));
|
|
238
|
+
}
|
|
239
|
+
let spawnError;
|
|
240
|
+
let exitInfo;
|
|
241
|
+
child.once("error", (error) => {
|
|
242
|
+
spawnError = error;
|
|
243
|
+
});
|
|
244
|
+
child.once("exit", (code, signal) => {
|
|
245
|
+
exitInfo = { code, signal };
|
|
246
|
+
});
|
|
247
|
+
// The service's output, line by line, through the sanitiser and behind a
|
|
248
|
+
// prefix: it is text the repository chose, and a line that starts with `::`
|
|
249
|
+
// is a command to the GitHub runner.
|
|
250
|
+
let printed = 0;
|
|
251
|
+
let cut = false;
|
|
252
|
+
const emitLine = (raw) => {
|
|
253
|
+
if (cut)
|
|
254
|
+
return;
|
|
255
|
+
printed += raw.length + 1;
|
|
256
|
+
if (printed > maxLogBytes) {
|
|
257
|
+
cut = true;
|
|
258
|
+
log(`[service] output cut: more than ${Math.round(maxLogBytes / 1024)} KiB, the rest is not shown`);
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
log(`[service] ${sanitizeText(raw, { redact: secrets })}`);
|
|
262
|
+
};
|
|
263
|
+
for (const stream of [child.stdout, child.stderr]) {
|
|
264
|
+
if (stream === null)
|
|
265
|
+
continue;
|
|
266
|
+
let pending = "";
|
|
267
|
+
stream.setEncoding("utf-8");
|
|
268
|
+
stream.on("data", (chunk) => {
|
|
269
|
+
pending += chunk;
|
|
270
|
+
let newline = pending.indexOf("\n");
|
|
271
|
+
while (newline >= 0) {
|
|
272
|
+
emitLine(pending.slice(0, newline));
|
|
273
|
+
pending = pending.slice(newline + 1);
|
|
274
|
+
newline = pending.indexOf("\n");
|
|
275
|
+
}
|
|
276
|
+
if (pending.length > MAX_PENDING_LINE) {
|
|
277
|
+
emitLine(pending);
|
|
278
|
+
pending = "";
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
stream.on("end", () => {
|
|
282
|
+
if (pending !== "")
|
|
283
|
+
emitLine(pending);
|
|
284
|
+
pending = "";
|
|
285
|
+
});
|
|
286
|
+
stream.on("error", () => undefined);
|
|
287
|
+
}
|
|
288
|
+
const pid = child.pid;
|
|
289
|
+
if (pid === undefined) {
|
|
290
|
+
await sleep(0);
|
|
291
|
+
throw new ServiceError(`could not start the service${spawnError === undefined ? "" : ` (${spawnError.code ?? "spawn failed"})`}`);
|
|
292
|
+
}
|
|
293
|
+
// A service of another user is signalled through sudo, by user: the user is the
|
|
294
|
+
// service's alone, so this reaches everything it started, a process that left
|
|
295
|
+
// the group included.
|
|
296
|
+
const userAlive = (name) => spawnSync(sudo, ["-n", "pgrep", "-u", name], { stdio: "ignore", timeout: 10_000 }).status === 0;
|
|
297
|
+
const signalUser = (name, signal) => {
|
|
298
|
+
spawnSync(sudo, ["-n", "pkill", `-${signal.replace(/^SIG/, "")}`, "-u", name], { stdio: "ignore", timeout: 10_000 });
|
|
299
|
+
};
|
|
300
|
+
const groupAlive = () => {
|
|
301
|
+
if (separate !== undefined)
|
|
302
|
+
return userAlive(separate.name);
|
|
303
|
+
try {
|
|
304
|
+
process.kill(-pid, 0);
|
|
305
|
+
return true;
|
|
306
|
+
}
|
|
307
|
+
catch (error) {
|
|
308
|
+
return error.code === "EPERM";
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
const signalGroup = (signal) => {
|
|
312
|
+
if (separate !== undefined) {
|
|
313
|
+
signalUser(separate.name, signal);
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
try {
|
|
317
|
+
process.kill(-pid, signal);
|
|
318
|
+
}
|
|
319
|
+
catch {
|
|
320
|
+
// Already gone.
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
const taskkill = () => {
|
|
324
|
+
const killer = spawn("taskkill", ["/pid", String(pid), "/T", "/F"], { stdio: "ignore", windowsHide: true });
|
|
325
|
+
killer.on("error", () => undefined);
|
|
326
|
+
};
|
|
327
|
+
const terminate = async () => {
|
|
328
|
+
const grace = options.stopGraceMs ?? 5000;
|
|
329
|
+
if (posix) {
|
|
330
|
+
if (!groupAlive())
|
|
331
|
+
return;
|
|
332
|
+
signalGroup("SIGTERM");
|
|
333
|
+
if (await waitFor(() => !groupAlive(), grace))
|
|
334
|
+
return;
|
|
335
|
+
signalGroup("SIGKILL");
|
|
336
|
+
await waitFor(() => !groupAlive(), KILL_WAIT_MS);
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
if (exitInfo !== undefined)
|
|
340
|
+
return;
|
|
341
|
+
taskkill();
|
|
342
|
+
await waitFor(() => exitInfo !== undefined, grace);
|
|
343
|
+
};
|
|
344
|
+
// Last resort, synchronous: Node is exiting and a service is still there.
|
|
345
|
+
const onProcessExit = () => {
|
|
346
|
+
if (posix) {
|
|
347
|
+
signalGroup("SIGKILL");
|
|
348
|
+
}
|
|
349
|
+
else if (exitInfo === undefined) {
|
|
350
|
+
spawnSync("taskkill", ["/pid", String(pid), "/T", "/F"], { stdio: "ignore", windowsHide: true });
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
const signalHandlers = SIGNALS.map((signal) => {
|
|
354
|
+
const handler = () => {
|
|
355
|
+
// Stop the service, then leave with the conventional code for the signal.
|
|
356
|
+
void stop().finally(() => process.exit(128 + (osConstants.signals[signal] ?? 0)));
|
|
357
|
+
};
|
|
358
|
+
process.on(signal, handler);
|
|
359
|
+
return [signal, handler];
|
|
360
|
+
});
|
|
361
|
+
process.on("exit", onProcessExit);
|
|
362
|
+
let stopping;
|
|
363
|
+
function stop() {
|
|
364
|
+
stopping ??= terminate().finally(() => {
|
|
365
|
+
for (const [signal, handler] of signalHandlers)
|
|
366
|
+
process.removeListener(signal, handler);
|
|
367
|
+
process.removeListener("exit", onProcessExit);
|
|
368
|
+
});
|
|
369
|
+
return stopping;
|
|
370
|
+
}
|
|
371
|
+
const fail = async (message) => {
|
|
372
|
+
await stop();
|
|
373
|
+
throw new ServiceError(message);
|
|
374
|
+
};
|
|
375
|
+
const deadline = Date.now() + ready.timeoutSeconds * 1000;
|
|
376
|
+
const interval = options.pollIntervalMs ?? 250;
|
|
377
|
+
for (;;) {
|
|
378
|
+
if (exitInfo !== undefined) {
|
|
379
|
+
const how = exitInfo.code !== null ? `exited with code ${exitInfo.code}` : `was ended by ${exitInfo.signal ?? "a signal"}`;
|
|
380
|
+
return fail(`the service ${how} before it answered at ${origin}; its output is above`);
|
|
381
|
+
}
|
|
382
|
+
if (spawnError !== undefined)
|
|
383
|
+
return fail("could not start the service");
|
|
384
|
+
if (await answers(ready.url))
|
|
385
|
+
return { pid, stop };
|
|
386
|
+
if (Date.now() >= deadline) {
|
|
387
|
+
const seconds = ready.timeoutSeconds;
|
|
388
|
+
return fail(`no answer from ${origin} within ${seconds} second${seconds === 1 ? "" : "s"}; the service was stopped`);
|
|
389
|
+
}
|
|
390
|
+
await sleep(interval);
|
|
391
|
+
}
|
|
392
|
+
}
|
package/dist/style.d.ts
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Colours and line layout for a person watching the terminal (Najmi,
|
|
3
|
+
* 2026-11-19): Wasit's own look, in the palette the site and the dashboard use
|
|
4
|
+
* (`dashboard/theme.ts`), a spinner while a check runs, and the run's duration.
|
|
5
|
+
*
|
|
6
|
+
* Colour is a layer over the plain text, never a second wording: every line is
|
|
7
|
+
* sanitised first (`sanitizeText`), then styled, so nothing a service sent can
|
|
8
|
+
* carry an escape sequence of its own. Output that a program reads stays plain:
|
|
9
|
+
* `--json`, a pipe, CI and `NO_COLOR` all get the exact text Wasit printed
|
|
10
|
+
* before colours existed.
|
|
11
|
+
*/
|
|
12
|
+
/** The parts of a stream this module reads. */
|
|
13
|
+
export interface StyleStream {
|
|
14
|
+
readonly isTTY?: boolean;
|
|
15
|
+
readonly columns?: number;
|
|
16
|
+
}
|
|
17
|
+
type Env = Readonly<Record<string, string | undefined>>;
|
|
18
|
+
/**
|
|
19
|
+
* True when lines may carry colour: `NO_COLOR` set turns it off, `FORCE_COLOR`
|
|
20
|
+
* set (and not "0") turns it on, a dumb terminal and CI are plain, otherwise a
|
|
21
|
+
* TTY is coloured and anything else is not.
|
|
22
|
+
*/
|
|
23
|
+
export declare function colorEnabled(stream: StyleStream, env: Env): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* True when a line may be rewritten in place (the running line and its
|
|
26
|
+
* spinner): colour on, and a real terminal. A forced colour into a pipe gets
|
|
27
|
+
* colour but no cursor movement.
|
|
28
|
+
*/
|
|
29
|
+
export declare function liveEnabled(stream: StyleStream, env: Env): boolean;
|
|
30
|
+
/** True when the terminal says it shows 24-bit colour; the brand colours are then exact. */
|
|
31
|
+
export declare function truecolorEnabled(env: Env): boolean;
|
|
32
|
+
/** How many colours the terminal says it shows: 24-bit, 256, or the 16 of every terminal. */
|
|
33
|
+
export type ColorDepth = 16 | 256 | 24;
|
|
34
|
+
export declare function colorDepth(env: Env): ColorDepth;
|
|
35
|
+
/** The roles a line's parts are painted in. */
|
|
36
|
+
export type Role = "accent" | "muted" | "success" | "warning" | "danger" | "error" | "bold" | "dim";
|
|
37
|
+
/** Applies roles to text, or returns it unchanged when colour is off. */
|
|
38
|
+
export interface Styler {
|
|
39
|
+
readonly enabled: boolean;
|
|
40
|
+
paint(text: string, ...roles: Role[]): string;
|
|
41
|
+
/** ` LABEL ` on a background in the role's colour; the bare label when colour is off. */
|
|
42
|
+
badge(label: string, role: BadgeRole): string;
|
|
43
|
+
}
|
|
44
|
+
/** The roles a badge's background can take. */
|
|
45
|
+
export type BadgeRole = "success" | "danger" | "warning" | "error";
|
|
46
|
+
/**
|
|
47
|
+
* A styler. `depth` is the terminal's colour depth (`colorDepth`); `true` is
|
|
48
|
+
* read as 24-bit, `false` or nothing as the 16 colours of every terminal.
|
|
49
|
+
*/
|
|
50
|
+
export declare function styler(enabled: boolean, depth?: ColorDepth | boolean): Styler;
|
|
51
|
+
/** The width a line takes on screen: its length without the colour codes. */
|
|
52
|
+
export declare function visibleLength(text: string): number;
|
|
53
|
+
/**
|
|
54
|
+
* A check's status as a mark and a badge that agree (Najmi, 2026-11-20):
|
|
55
|
+
* ✓ PASS, ✗ FAIL, ○ SKIP, ! ERROR (the check could not run). Text marks, not
|
|
56
|
+
* emoji: an emoji takes two columns in many terminals and breaks the columns.
|
|
57
|
+
*/
|
|
58
|
+
export declare const MARKS: {
|
|
59
|
+
readonly PASS: {
|
|
60
|
+
readonly mark: "✓";
|
|
61
|
+
readonly markRole: "success";
|
|
62
|
+
readonly badge: "PASS";
|
|
63
|
+
readonly badgeRole: "success";
|
|
64
|
+
};
|
|
65
|
+
readonly FAIL: {
|
|
66
|
+
readonly mark: "✗";
|
|
67
|
+
readonly markRole: "danger";
|
|
68
|
+
readonly badge: "FAIL";
|
|
69
|
+
readonly badgeRole: "danger";
|
|
70
|
+
};
|
|
71
|
+
readonly SKIP: {
|
|
72
|
+
readonly mark: "○";
|
|
73
|
+
readonly markRole: "warning";
|
|
74
|
+
readonly badge: "SKIP";
|
|
75
|
+
readonly badgeRole: "warning";
|
|
76
|
+
};
|
|
77
|
+
readonly ERROR: {
|
|
78
|
+
readonly mark: "!";
|
|
79
|
+
readonly markRole: "error";
|
|
80
|
+
readonly badge: "ERROR";
|
|
81
|
+
readonly badgeRole: "error";
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
/** The spinner's frames, as the dashboard draws them, one every 80 ms. */
|
|
85
|
+
export declare const SPINNER_FRAMES: readonly ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
86
|
+
export declare const SPINNER_MS = 80;
|
|
87
|
+
/** "12ms" under a second, "6.82s" from a second on. */
|
|
88
|
+
export declare function duration(ms: number): string;
|
|
89
|
+
/**
|
|
90
|
+
* `left` and `right` on one line, `right` flush with the width. A line that
|
|
91
|
+
* does not fit keeps two spaces between them and runs over.
|
|
92
|
+
*/
|
|
93
|
+
export declare function spread(left: string, right: string, width: number): string;
|
|
94
|
+
/** Cuts plain text to `max` characters, ending in "…". */
|
|
95
|
+
export declare function truncate(text: string, max: number): string;
|
|
96
|
+
/**
|
|
97
|
+
* Plain text wrapped to `width` columns, every line after `indent`. A word
|
|
98
|
+
* longer than a line (a URL, a hash) is kept whole and runs over.
|
|
99
|
+
*/
|
|
100
|
+
export declare function wrap(text: string, width: number, indent: string): string[];
|
|
101
|
+
/**
|
|
102
|
+
* The width lines are laid out to: one column less than the terminal's (a line
|
|
103
|
+
* as wide as the screen makes some terminals start a new line), at most 100,
|
|
104
|
+
* 80 when unknown.
|
|
105
|
+
*/
|
|
106
|
+
export declare function lineWidth(stream: StyleStream): number;
|
|
107
|
+
export {};
|