@mostra/conformance 0.0.0-placeholder → 0.2.0
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/LICENSE +202 -0
- package/README.md +68 -1
- package/baselines/reference-host-rust-broken.json +8 -0
- package/baselines/reference-host-ts-broken.json +8 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +106 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.d.ts +403 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/protocol-RQ9lbehE.js +207 -0
- package/dist/protocol-RQ9lbehE.js.map +1 -0
- package/dist/reference-host-BJ-A_Qd_.d.ts +257 -0
- package/dist/reference-host-BJ-A_Qd_.d.ts.map +1 -0
- package/dist/reference-host-d6gs3Tal.js +409 -0
- package/dist/reference-host-d6gs3Tal.js.map +1 -0
- package/dist/reference-host.d.ts +2 -0
- package/dist/reference-host.js +3 -0
- package/dist/runner-CvoSJlfk.js +1940 -0
- package/dist/runner-CvoSJlfk.js.map +1 -0
- package/package.json +67 -4
- package/src/baseline.ts +51 -0
- package/src/cli.ts +117 -0
- package/src/fixtures.ts +87 -0
- package/src/harness/client.ts +224 -0
- package/src/harness/protocol.ts +381 -0
- package/src/harness/serve.ts +146 -0
- package/src/index.ts +65 -0
- package/src/reference-host.ts +359 -0
- package/src/runner.ts +189 -0
- package/src/scenario.ts +376 -0
- package/src/scenarios/delivery.ts +267 -0
- package/src/scenarios/index.ts +20 -0
- package/src/scenarios/interaction.ts +453 -0
- package/src/scenarios/ports.ts +278 -0
- package/src/scenarios/session.ts +183 -0
- package/src/scenarios/viewing.ts +184 -0
- package/src/version.ts +29 -0
- package/src/viewer.ts +380 -0
package/src/cli.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* `mostra-conformance`: runs the host conformance kit (HO-05) against a host's harness shim.
|
|
4
|
+
*
|
|
5
|
+
* mostra-conformance --host "<command>" [--baseline <file>] [--only <id-or-prefix>]…
|
|
6
|
+
* [--verify] [--json] [--list]
|
|
7
|
+
*
|
|
8
|
+
* Exit codes: 0 the run passes (every failure expected by the baseline, no stale entry), 1 it
|
|
9
|
+
* doesn't, 2 a usage error.
|
|
10
|
+
*/
|
|
11
|
+
import { parseArgs } from "node:util";
|
|
12
|
+
import { readBaseline } from "./baseline.ts";
|
|
13
|
+
import { runConformance, type ScenarioOutcome } from "./runner.ts";
|
|
14
|
+
import { SCENARIOS } from "./scenarios/index.ts";
|
|
15
|
+
|
|
16
|
+
const MARKS: Readonly<Record<ScenarioOutcome["status"], string>> = {
|
|
17
|
+
passed: "pass",
|
|
18
|
+
failed: "FAIL",
|
|
19
|
+
skipped: "skip",
|
|
20
|
+
"expected-failure": "xfail",
|
|
21
|
+
"unexpected-pass": "XPASS",
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
function parse() {
|
|
25
|
+
return parseArgs({
|
|
26
|
+
options: {
|
|
27
|
+
host: { type: "string" },
|
|
28
|
+
baseline: { type: "string" },
|
|
29
|
+
only: { type: "string", multiple: true },
|
|
30
|
+
verify: { type: "boolean" },
|
|
31
|
+
json: { type: "boolean" },
|
|
32
|
+
list: { type: "boolean" },
|
|
33
|
+
help: { type: "boolean" },
|
|
34
|
+
},
|
|
35
|
+
strict: true,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function main(): Promise<number> {
|
|
40
|
+
let parsed: ReturnType<typeof parse>;
|
|
41
|
+
try {
|
|
42
|
+
parsed = parse();
|
|
43
|
+
} catch (error) {
|
|
44
|
+
process.stderr.write(`mostra-conformance: ${(error as Error).message} (see --help).\n`);
|
|
45
|
+
return 2;
|
|
46
|
+
}
|
|
47
|
+
const { values } = parsed;
|
|
48
|
+
if (values.help) {
|
|
49
|
+
process.stdout.write(
|
|
50
|
+
[
|
|
51
|
+
"Usage: mostra-conformance --host <command> [--baseline <file>] [--only <id>]… [--verify] [--json]",
|
|
52
|
+
" mostra-conformance --list",
|
|
53
|
+
"",
|
|
54
|
+
"Runs the host conformance scenarios against the harness shim <command> starts.",
|
|
55
|
+
"Exit codes: 0 passes, 1 fails (a new failure, or a baseline entry that now passes), 2 usage.",
|
|
56
|
+
"",
|
|
57
|
+
].join("\n"),
|
|
58
|
+
);
|
|
59
|
+
return 0;
|
|
60
|
+
}
|
|
61
|
+
if (values.list) {
|
|
62
|
+
for (const scenario of SCENARIOS) {
|
|
63
|
+
process.stdout.write(
|
|
64
|
+
values.json
|
|
65
|
+
? `${JSON.stringify({ id: scenario.id, title: scenario.title, covers: scenario.covers, needs: scenario.needs ?? [] })}\n`
|
|
66
|
+
: `${scenario.id} ${scenario.title}\n`,
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
return 0;
|
|
70
|
+
}
|
|
71
|
+
if (!values.host) {
|
|
72
|
+
process.stderr.write("mostra-conformance: --host <command> is required (see --help).\n");
|
|
73
|
+
return 2;
|
|
74
|
+
}
|
|
75
|
+
const known = SCENARIOS.map((scenario) => scenario.id);
|
|
76
|
+
let baseline: ReturnType<typeof readBaseline> | undefined;
|
|
77
|
+
try {
|
|
78
|
+
baseline = values.baseline ? readBaseline(values.baseline, known) : undefined;
|
|
79
|
+
} catch (error) {
|
|
80
|
+
process.stderr.write(`mostra-conformance: ${(error as Error).message}\n`);
|
|
81
|
+
return 2;
|
|
82
|
+
}
|
|
83
|
+
const report = await runConformance({
|
|
84
|
+
host: values.host,
|
|
85
|
+
...(baseline ? { baseline } : {}),
|
|
86
|
+
...(values.only
|
|
87
|
+
? { only: values.only.flatMap((entry) => entry.split(",")).filter(Boolean) }
|
|
88
|
+
: {}),
|
|
89
|
+
verify: values.verify ?? false,
|
|
90
|
+
onOutcome: (outcome) => {
|
|
91
|
+
if (values.json) return;
|
|
92
|
+
const reason = outcome.reason ? `\n ${outcome.reason}` : "";
|
|
93
|
+
process.stderr.write(
|
|
94
|
+
`${MARKS[outcome.status].padEnd(5)} ${outcome.id} (${(outcome.durationMs / 1000).toFixed(1)} s)${reason}\n`,
|
|
95
|
+
);
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
if (values.json) process.stdout.write(`${JSON.stringify(report)}\n`);
|
|
99
|
+
else {
|
|
100
|
+
const count = (status: ScenarioOutcome["status"]): number =>
|
|
101
|
+
report.outcomes.filter((outcome) => outcome.status === status).length;
|
|
102
|
+
process.stderr.write(
|
|
103
|
+
`\n${report.host?.host.name ?? "host"}: ${count("passed")} passed, ${count("failed")} failed, ${count("expected-failure")} expected failures, ${count("unexpected-pass")} stale baseline entries, ${count("skipped")} skipped: ${report.ok ? "conformant" : "not conformant"}\n`,
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
return report.ok ? 0 : 1;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
main().then(
|
|
110
|
+
(code) => {
|
|
111
|
+
process.exitCode = code;
|
|
112
|
+
},
|
|
113
|
+
(error: unknown) => {
|
|
114
|
+
process.stderr.write(`mostra-conformance: ${(error as Error).stack ?? error}\n`);
|
|
115
|
+
process.exitCode = 2;
|
|
116
|
+
},
|
|
117
|
+
);
|
package/src/fixtures.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The artifacts the scenarios publish, written fresh into the run's authoring directory. They
|
|
3
|
+
* declare their fields with literal IDs, so every answer a scenario writes is covered by the
|
|
4
|
+
* version (D109) and none is orphaned by accident.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** A fixture: its files by relative path. */
|
|
8
|
+
export type Fixture = Readonly<Record<string, string>>;
|
|
9
|
+
|
|
10
|
+
/** A questionnaire with viewer fields, an artifact-scoped counter, a family and a link (H-06). */
|
|
11
|
+
export const FORM: Fixture = {
|
|
12
|
+
"index.tsx": `import { defineArtifact, useField, useSubmitControl } from "@mostra/react";
|
|
13
|
+
|
|
14
|
+
export const artifact = defineArtifact({
|
|
15
|
+
title: "Conformance questionnaire",
|
|
16
|
+
description: "The form the host conformance kit answers.",
|
|
17
|
+
family: "questionnaire",
|
|
18
|
+
links: [{ rel: "round-of", artifact: "conformance-round-1" }],
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
export default function Page() {
|
|
22
|
+
useSubmitControl({ label: "Send answers" });
|
|
23
|
+
const [name, setName] = useField("name", "", { label: "Your name", required: true });
|
|
24
|
+
const [comment, setComment] = useField("comment", "", { label: "Comment" });
|
|
25
|
+
const [count, setCount] = useField("count", 0, { label: "Count", scope: "artifact" });
|
|
26
|
+
const [done, setDone] = useField("done", false, { label: "Done", scope: "artifact" });
|
|
27
|
+
return (
|
|
28
|
+
<main>
|
|
29
|
+
<h1>Conformance questionnaire</h1>
|
|
30
|
+
<input name="name" aria-label="Your name" value={name} onChange={(e) => setName(e.target.value)} />
|
|
31
|
+
<textarea name="comment" aria-label="Comment" value={comment} onChange={(e) => setComment(e.target.value)} />
|
|
32
|
+
<button type="button" onClick={() => setCount(count + 1)}>Count {count}</button>
|
|
33
|
+
<label><input type="checkbox" checked={done} onChange={(e) => setDone(e.target.checked)} /> Done</label>
|
|
34
|
+
</main>
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
`,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/** The same questionnaire with one more question: a second version of {@link FORM}. */
|
|
41
|
+
export const FORM_V2: Fixture = {
|
|
42
|
+
"index.tsx": (FORM["index.tsx"] as string)
|
|
43
|
+
.replace("<h1>Conformance questionnaire</h1>", "<h1>Conformance questionnaire, round 2</h1>")
|
|
44
|
+
.replace(
|
|
45
|
+
" const [done, setDone]",
|
|
46
|
+
' const [extra, setExtra] = useField("extra", "", { label: "Extra" });\n const [done, setDone]',
|
|
47
|
+
)
|
|
48
|
+
.replace(
|
|
49
|
+
" <label>",
|
|
50
|
+
' <input name="extra" aria-label="Extra" value={extra} onChange={(e) => setExtra(e.target.value)} />\n <label>',
|
|
51
|
+
),
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Server code: an action that raises a signal and emits a host event (AC-05), and one that reads
|
|
56
|
+
* a secret through `secret()` (D131) and returns only its length, never the value.
|
|
57
|
+
*/
|
|
58
|
+
export const ACTIONS: Fixture = {
|
|
59
|
+
"index.tsx": `import { defineArtifact } from "@mostra/react";
|
|
60
|
+
|
|
61
|
+
export const artifact = defineArtifact({ title: "Conformance actions" });
|
|
62
|
+
|
|
63
|
+
export default function Page() {
|
|
64
|
+
return <main><h1>Conformance actions</h1></main>;
|
|
65
|
+
}
|
|
66
|
+
`,
|
|
67
|
+
"actions.ts": `"use server";
|
|
68
|
+
import { emit, secret, signal } from "@mostra/server";
|
|
69
|
+
|
|
70
|
+
export async function notify(to: string): Promise<string> {
|
|
71
|
+
signal("mail.sent", { to });
|
|
72
|
+
emit("host.mail.sent", { to });
|
|
73
|
+
return "sent";
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export async function secretLength(name: string): Promise<number> {
|
|
77
|
+
return secret(name).length;
|
|
78
|
+
}
|
|
79
|
+
`,
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/** The bundled fixtures by name. */
|
|
83
|
+
export const FIXTURES: Readonly<Record<string, Fixture>> = {
|
|
84
|
+
form: FORM,
|
|
85
|
+
"form-v2": FORM_V2,
|
|
86
|
+
actions: ACTIONS,
|
|
87
|
+
};
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The kit's side of the harness protocol: starts a host's shim (a command line), speaks to it
|
|
3
|
+
* over its stdio, and gives scenarios typed methods. The shim's stderr is kept (its last lines
|
|
4
|
+
* explain a failure). A shim that exits ends every pending call with
|
|
5
|
+
* `MOSTRA_HOST_CONNECTION_CLOSED`.
|
|
6
|
+
*/
|
|
7
|
+
import { type ChildProcess, spawn } from "node:child_process";
|
|
8
|
+
import {
|
|
9
|
+
createDiagnostic,
|
|
10
|
+
type Diagnostic,
|
|
11
|
+
encodeLine,
|
|
12
|
+
LineSplitter,
|
|
13
|
+
RpcError,
|
|
14
|
+
RpcPeer,
|
|
15
|
+
} from "@mostra/protocol";
|
|
16
|
+
import { kitVersion } from "../version.ts";
|
|
17
|
+
import {
|
|
18
|
+
type CallError,
|
|
19
|
+
type CallResult,
|
|
20
|
+
CallResult as CallResultSchema,
|
|
21
|
+
type EventsParams,
|
|
22
|
+
type EventsResult,
|
|
23
|
+
EventsResult as EventsResultSchema,
|
|
24
|
+
HARNESS_METHODS,
|
|
25
|
+
HARNESS_REVISIONS,
|
|
26
|
+
type InitializeResult,
|
|
27
|
+
InitializeResult as InitializeResultSchema,
|
|
28
|
+
type ScriptParams,
|
|
29
|
+
type StartParams,
|
|
30
|
+
type StartResult,
|
|
31
|
+
StartResult as StartResultSchema,
|
|
32
|
+
type StoreResult,
|
|
33
|
+
StoreResult as StoreResultSchema,
|
|
34
|
+
} from "./protocol.ts";
|
|
35
|
+
|
|
36
|
+
/** How long one harness request may take before the kit gives up on the host. */
|
|
37
|
+
const HARNESS_TIMEOUT_MS = 120_000;
|
|
38
|
+
|
|
39
|
+
/** An instance's error, carried through the host: its diagnostic code and JSON-RPC code. */
|
|
40
|
+
export class InstanceError extends Error {
|
|
41
|
+
/** The diagnostic code (`MOSTRA_…`). */
|
|
42
|
+
readonly code: string;
|
|
43
|
+
/** The JSON-RPC error code. */
|
|
44
|
+
readonly rpcCode: number;
|
|
45
|
+
/** The diagnostic as it arrived. */
|
|
46
|
+
readonly data: CallError["data"];
|
|
47
|
+
|
|
48
|
+
constructor(method: string, error: CallError) {
|
|
49
|
+
super(`${method} failed with ${error.data.code}: ${error.data.message ?? ""}`);
|
|
50
|
+
this.name = "InstanceError";
|
|
51
|
+
this.code = error.data.code;
|
|
52
|
+
this.rpcCode = error.code;
|
|
53
|
+
this.data = error.data;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** A host under test, reached through its harness shim. */
|
|
58
|
+
export class HostUnderTest {
|
|
59
|
+
readonly #child: ChildProcess;
|
|
60
|
+
readonly #peer: RpcPeer;
|
|
61
|
+
readonly #stderr: string[] = [];
|
|
62
|
+
readonly #exited: Promise<number | null>;
|
|
63
|
+
/** What the host said about itself. */
|
|
64
|
+
info: InitializeResult | undefined;
|
|
65
|
+
|
|
66
|
+
private constructor(command: string, env: Readonly<Record<string, string>>) {
|
|
67
|
+
this.#child = spawn(command, {
|
|
68
|
+
shell: true,
|
|
69
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
70
|
+
env: { ...process.env, ...env } as NodeJS.ProcessEnv,
|
|
71
|
+
detached: true,
|
|
72
|
+
});
|
|
73
|
+
this.#exited = new Promise((done) => {
|
|
74
|
+
this.#child.on("close", (code) => done(code));
|
|
75
|
+
this.#child.on("error", () => done(null));
|
|
76
|
+
});
|
|
77
|
+
this.#peer = new RpcPeer({
|
|
78
|
+
send: (message) => {
|
|
79
|
+
if (this.#child.stdin?.writable) this.#child.stdin.write(encodeLine(message));
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
const lines = new LineSplitter();
|
|
83
|
+
this.#child.stdout?.setEncoding("utf8");
|
|
84
|
+
this.#child.stdout?.on("data", (chunk: string) => {
|
|
85
|
+
for (const line of lines.push(chunk)) this.#peer.receiveLine(line);
|
|
86
|
+
});
|
|
87
|
+
const errors = new LineSplitter();
|
|
88
|
+
this.#child.stderr?.setEncoding("utf8");
|
|
89
|
+
this.#child.stderr?.on("data", (chunk: string) => {
|
|
90
|
+
for (const line of errors.push(chunk)) {
|
|
91
|
+
this.#stderr.push(line);
|
|
92
|
+
if (this.#stderr.length > 40) this.#stderr.shift();
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
this.#child.stdin?.on("error", () => {});
|
|
96
|
+
void this.#exited.then((code) =>
|
|
97
|
+
this.#peer.close(
|
|
98
|
+
createDiagnostic("MOSTRA_HOST_CONNECTION_CLOSED", {
|
|
99
|
+
message: `The host under test exited (${code ?? "signal"}): ${this.#stderr.slice(-3).join(" | ")}`,
|
|
100
|
+
}),
|
|
101
|
+
),
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Starts the shim and asks it who it is. */
|
|
106
|
+
static async start(
|
|
107
|
+
command: string,
|
|
108
|
+
env: Readonly<Record<string, string>> = {},
|
|
109
|
+
): Promise<HostUnderTest> {
|
|
110
|
+
const host = new HostUnderTest(command, env);
|
|
111
|
+
try {
|
|
112
|
+
const answer = await host.#request(HARNESS_METHODS.initialize, {
|
|
113
|
+
revision: HARNESS_REVISIONS.at(-1),
|
|
114
|
+
kit: { name: "@mostra/conformance", version: kitVersion() },
|
|
115
|
+
});
|
|
116
|
+
host.info = InitializeResultSchema.parse(answer);
|
|
117
|
+
} catch (error) {
|
|
118
|
+
await host.close();
|
|
119
|
+
throw error;
|
|
120
|
+
}
|
|
121
|
+
return host;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** The host's last stderr lines. */
|
|
125
|
+
get stderr(): readonly string[] {
|
|
126
|
+
return this.#stderr;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Whether the host serves `port` / supports `feature`. */
|
|
130
|
+
serves(port: string): boolean {
|
|
131
|
+
return this.info?.ports.includes(port) ?? false;
|
|
132
|
+
}
|
|
133
|
+
/** Whether the host supports the harness feature `feature`. */
|
|
134
|
+
supports(feature: string): boolean {
|
|
135
|
+
return this.info?.features.includes(feature) ?? false;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
#request(method: string, params: unknown, timeoutMs = HARNESS_TIMEOUT_MS): Promise<unknown> {
|
|
139
|
+
return this.#peer.request(method, params as never, { timeoutMs });
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** `harness/start`: the host starts or attaches to its instance. */
|
|
143
|
+
async startInstance(params: StartParams): Promise<StartResult> {
|
|
144
|
+
return StartResultSchema.parse(await this.#request(HARNESS_METHODS.start, params));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** `harness/call`, raw: the instance's answer or error as the host received it. */
|
|
148
|
+
async callRaw(method: string, params: unknown, timeoutMs?: number): Promise<CallResult> {
|
|
149
|
+
return CallResultSchema.parse(
|
|
150
|
+
await this.#request(
|
|
151
|
+
HARNESS_METHODS.call,
|
|
152
|
+
{ method, params, ...(timeoutMs !== undefined ? { timeoutMs } : {}) },
|
|
153
|
+
(timeoutMs ?? 60_000) + 30_000,
|
|
154
|
+
),
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** `harness/call`: the answer, or an {@link InstanceError}. */
|
|
159
|
+
async call<T = Record<string, unknown>>(
|
|
160
|
+
method: string,
|
|
161
|
+
params: unknown,
|
|
162
|
+
timeoutMs?: number,
|
|
163
|
+
): Promise<T> {
|
|
164
|
+
const answer = await this.callRaw(method, params, timeoutMs);
|
|
165
|
+
if ("error" in answer) throw new InstanceError(method, answer.error);
|
|
166
|
+
return answer.result as T;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** `harness/stop`: the host ends its session. */
|
|
170
|
+
async stopInstance(): Promise<void> {
|
|
171
|
+
await this.#request(HARNESS_METHODS.stop, {});
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** `harness/store`. */
|
|
175
|
+
async store(): Promise<StoreResult> {
|
|
176
|
+
return StoreResultSchema.parse(await this.#request(HARNESS_METHODS.store, {}));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** `harness/script` (feature `faults`). */
|
|
180
|
+
async script(params: ScriptParams): Promise<void> {
|
|
181
|
+
await this.#request(HARNESS_METHODS.script, params);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** `harness/events` (feature `events`). */
|
|
185
|
+
async events(params: EventsParams): Promise<EventsResult> {
|
|
186
|
+
return EventsResultSchema.parse(
|
|
187
|
+
await this.#request(HARNESS_METHODS.events, params, params.timeoutMs + 30_000),
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** Ends the shim: stdin closes, then SIGTERM to its process group, then SIGKILL. */
|
|
192
|
+
async close(): Promise<void> {
|
|
193
|
+
this.#child.stdin?.end();
|
|
194
|
+
const pid = this.#child.pid;
|
|
195
|
+
const signal = (name: NodeJS.Signals): void => {
|
|
196
|
+
if (pid === undefined) return;
|
|
197
|
+
try {
|
|
198
|
+
process.kill(-pid, name);
|
|
199
|
+
} catch {
|
|
200
|
+
// gone already
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
const outcome = await Promise.race([
|
|
204
|
+
this.#exited.then(() => "exited" as const),
|
|
205
|
+
new Promise<"waiting">((done) => setTimeout(() => done("waiting"), 5000)),
|
|
206
|
+
]);
|
|
207
|
+
if (outcome === "waiting") {
|
|
208
|
+
signal("SIGTERM");
|
|
209
|
+
const later = await Promise.race([
|
|
210
|
+
this.#exited.then(() => "exited" as const),
|
|
211
|
+
new Promise<"waiting">((done) => setTimeout(() => done("waiting"), 5000)),
|
|
212
|
+
]);
|
|
213
|
+
if (later === "waiting") signal("SIGKILL");
|
|
214
|
+
}
|
|
215
|
+
this.#peer.close();
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** A diagnostic code from an error the kit saw: the instance's, an RPC error's, or none. */
|
|
220
|
+
export function codeOf(error: unknown): string | undefined {
|
|
221
|
+
if (error instanceof InstanceError) return error.code;
|
|
222
|
+
if (error instanceof RpcError) return (error.diagnostic as Diagnostic).code;
|
|
223
|
+
return undefined;
|
|
224
|
+
}
|