@git.zone/cli 6.11.0 → 6.13.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/dist_ts/00_commitinfo_data.js +1 -1
- package/dist_ts/gitzone.cli.js +6 -1
- package/dist_ts/helpers.changelog.d.ts +4 -0
- package/dist_ts/helpers.changelog.js +73 -24
- package/dist_ts/helpers.climode.js +47 -3
- package/dist_ts/helpers.workflow.d.ts +2 -2
- package/dist_ts/helpers.workflow.js +42 -6
- package/dist_ts/mod_commit/helpers.manual.d.ts +10 -0
- package/dist_ts/mod_commit/helpers.manual.js +97 -0
- package/dist_ts/mod_commit/index.js +156 -35
- package/dist_ts/mod_commit/mod.plugins.d.ts +7 -7
- package/dist_ts/mod_commit/mod.plugins.js +9 -8
- package/dist_ts/mod_release/helpers.releasepreparation.d.ts +2 -0
- package/dist_ts/mod_release/helpers.releasepreparation.js +12 -8
- package/dist_ts/mod_release/index.js +23 -4
- package/dist_ts/mod_standard/index.js +8 -2
- package/dist_ts/mod_testing/classes.commands.d.ts +27 -0
- package/dist_ts/mod_testing/classes.commands.js +407 -0
- package/dist_ts/mod_testing/classes.runtime.d.ts +37 -0
- package/dist_ts/mod_testing/classes.runtime.js +252 -0
- package/dist_ts/mod_testing/index.d.ts +4 -0
- package/dist_ts/mod_testing/index.js +149 -0
- package/dist_ts/plugins.d.ts +7 -1
- package/dist_ts/plugins.js +6 -2
- package/package.json +3 -1
- package/readme.md +161 -10
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts/gitzone.cli.ts +5 -0
- package/ts/helpers.changelog.ts +108 -32
- package/ts/helpers.climode.ts +51 -2
- package/ts/helpers.workflow.ts +46 -5
- package/ts/mod_commit/helpers.manual.ts +156 -0
- package/ts/mod_commit/index.ts +272 -53
- package/ts/mod_commit/mod.plugins.ts +8 -7
- package/ts/mod_release/helpers.releasepreparation.ts +17 -7
- package/ts/mod_release/index.ts +26 -2
- package/ts/mod_standard/index.ts +7 -1
- package/ts/mod_testing/classes.commands.ts +620 -0
- package/ts/mod_testing/classes.runtime.ts +310 -0
- package/ts/mod_testing/index.ts +197 -0
- package/ts/plugins.ts +10 -0
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import * as plugins from "../plugins.js";
|
|
2
|
+
|
|
3
|
+
export class TestingCommandError extends Error {
|
|
4
|
+
constructor(
|
|
5
|
+
public readonly code: string,
|
|
6
|
+
message: string,
|
|
7
|
+
public readonly details?: { operationId?: string; retryAt?: number },
|
|
8
|
+
) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = "TestingCommandError";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** One command owns its deadline, signal handlers and cancellable waits. */
|
|
15
|
+
export class TestingCommandRuntime {
|
|
16
|
+
public readonly controller = new AbortController();
|
|
17
|
+
public readonly signal = this.controller.signal;
|
|
18
|
+
private readonly deadline: number;
|
|
19
|
+
private timer: ReturnType<typeof setTimeout> | undefined;
|
|
20
|
+
private receivedSignal: "SIGINT" | "SIGTERM" | undefined;
|
|
21
|
+
private readonly onInterrupt = () => this.abort("SIGINT");
|
|
22
|
+
private readonly onTerminate = () => this.abort("SIGTERM");
|
|
23
|
+
|
|
24
|
+
constructor(timeoutMs: number) {
|
|
25
|
+
this.deadline = Date.now() + timeoutMs;
|
|
26
|
+
this.timer = setTimeout(() => this.controller.abort(), timeoutMs);
|
|
27
|
+
process.on("SIGINT", this.onInterrupt);
|
|
28
|
+
process.on("SIGTERM", this.onTerminate);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
private abort(signal: "SIGINT" | "SIGTERM") {
|
|
32
|
+
this.receivedSignal = signal;
|
|
33
|
+
this.controller.abort();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public get exitCode() {
|
|
37
|
+
return this.receivedSignal === "SIGINT"
|
|
38
|
+
? 130
|
|
39
|
+
: this.receivedSignal === "SIGTERM"
|
|
40
|
+
? 143
|
|
41
|
+
: 1;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public check(): number {
|
|
45
|
+
const remaining = this.deadline - Date.now();
|
|
46
|
+
if (this.signal.aborted || remaining <= 0) {
|
|
47
|
+
throw new TestingCommandError(
|
|
48
|
+
this.receivedSignal ? "cancelled" : "timeout",
|
|
49
|
+
this.receivedSignal
|
|
50
|
+
? "Testing command cancelled."
|
|
51
|
+
: "Testing command deadline exceeded.",
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
return remaining;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
public requestOptions() {
|
|
58
|
+
return {
|
|
59
|
+
timeoutMs: Math.min(15000, this.check()),
|
|
60
|
+
abortSignal: this.signal,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
public async delay(delayMs: number): Promise<void> {
|
|
65
|
+
const remaining = this.check();
|
|
66
|
+
await new Promise<void>((resolve, reject) => {
|
|
67
|
+
const finish = () => {
|
|
68
|
+
clearTimeout(timer);
|
|
69
|
+
this.signal.removeEventListener("abort", abort);
|
|
70
|
+
resolve();
|
|
71
|
+
};
|
|
72
|
+
const abort = () => {
|
|
73
|
+
clearTimeout(timer);
|
|
74
|
+
this.signal.removeEventListener("abort", abort);
|
|
75
|
+
reject(new TestingCommandError("cancelled", "Testing wait cancelled."));
|
|
76
|
+
};
|
|
77
|
+
const timer = setTimeout(finish, Math.min(remaining, delayMs));
|
|
78
|
+
this.signal.addEventListener("abort", abort, { once: true });
|
|
79
|
+
if (this.signal.aborted) abort();
|
|
80
|
+
});
|
|
81
|
+
this.check();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
public async readToken(): Promise<string> {
|
|
85
|
+
if (process.stdin.isTTY)
|
|
86
|
+
throw new TestingCommandError(
|
|
87
|
+
"invalid_input",
|
|
88
|
+
"Secret stdin must be a pipe.",
|
|
89
|
+
);
|
|
90
|
+
this.check();
|
|
91
|
+
return new Promise<string>((resolve, reject) => {
|
|
92
|
+
let chunks: Buffer[] = [];
|
|
93
|
+
let bytes = 0;
|
|
94
|
+
const cleanup = () => {
|
|
95
|
+
process.stdin.pause();
|
|
96
|
+
process.stdin.removeListener("data", data);
|
|
97
|
+
process.stdin.removeListener("end", end);
|
|
98
|
+
process.stdin.removeListener("error", error);
|
|
99
|
+
this.signal.removeEventListener("abort", abort);
|
|
100
|
+
chunks = [];
|
|
101
|
+
};
|
|
102
|
+
const fail = (code: string, message: string) => {
|
|
103
|
+
cleanup();
|
|
104
|
+
reject(new TestingCommandError(code, message));
|
|
105
|
+
};
|
|
106
|
+
const data = (chunk: Buffer | string) => {
|
|
107
|
+
const value = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
108
|
+
bytes += value.length;
|
|
109
|
+
if (bytes > 8192) {
|
|
110
|
+
fail("invalid_input", "Secret stdin exceeds the size limit.");
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
chunks.push(value);
|
|
114
|
+
};
|
|
115
|
+
const end = () => {
|
|
116
|
+
const token = Buffer.concat(chunks).toString("utf8").trim();
|
|
117
|
+
cleanup();
|
|
118
|
+
if (!token || /\s/.test(token))
|
|
119
|
+
reject(
|
|
120
|
+
new TestingCommandError(
|
|
121
|
+
"invalid_input",
|
|
122
|
+
"Secret stdin must contain one credential.",
|
|
123
|
+
),
|
|
124
|
+
);
|
|
125
|
+
else resolve(token);
|
|
126
|
+
};
|
|
127
|
+
const error = () =>
|
|
128
|
+
fail("input_unavailable", "Could not read secret stdin.");
|
|
129
|
+
const abort = () => fail("cancelled", "Secret input cancelled.");
|
|
130
|
+
process.stdin.on("data", data);
|
|
131
|
+
process.stdin.once("end", end);
|
|
132
|
+
process.stdin.once("error", error);
|
|
133
|
+
this.signal.addEventListener("abort", abort, { once: true });
|
|
134
|
+
if (this.signal.aborted) abort();
|
|
135
|
+
else if (process.stdin.readableEnded) end();
|
|
136
|
+
else process.stdin.resume();
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
public static validateSecretFd(fd: number): void {
|
|
141
|
+
try {
|
|
142
|
+
if (
|
|
143
|
+
!Number.isSafeInteger(fd) ||
|
|
144
|
+
fd < 3 ||
|
|
145
|
+
fd > 255 ||
|
|
146
|
+
plugins.tty.isatty(fd)
|
|
147
|
+
)
|
|
148
|
+
throw new Error();
|
|
149
|
+
const stat = plugins.fsSync.fstatSync(fd);
|
|
150
|
+
if (!stat.isFile() && !stat.isFIFO() && !stat.isSocket())
|
|
151
|
+
throw new Error();
|
|
152
|
+
plugins.fsSync.writeSync(fd, Buffer.alloc(0));
|
|
153
|
+
} catch {
|
|
154
|
+
throw new TestingCommandError(
|
|
155
|
+
"invalid_input",
|
|
156
|
+
"Secret output requires a writable, non-terminal, preopened file descriptor from 3 to 255.",
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
public static deliverSecret(fd: number, token: string): void {
|
|
162
|
+
try {
|
|
163
|
+
if (
|
|
164
|
+
typeof token !== "string" ||
|
|
165
|
+
!token ||
|
|
166
|
+
token.length > 8192 ||
|
|
167
|
+
/\s/.test(token)
|
|
168
|
+
)
|
|
169
|
+
throw new Error();
|
|
170
|
+
const bytes = Buffer.from(token + "\n");
|
|
171
|
+
let offset = 0;
|
|
172
|
+
while (offset < bytes.length) {
|
|
173
|
+
const written = plugins.fsSync.writeSync(
|
|
174
|
+
fd,
|
|
175
|
+
bytes,
|
|
176
|
+
offset,
|
|
177
|
+
bytes.length - offset,
|
|
178
|
+
);
|
|
179
|
+
if (!written) throw new Error();
|
|
180
|
+
offset += written;
|
|
181
|
+
}
|
|
182
|
+
} catch {
|
|
183
|
+
throw new TestingCommandError(
|
|
184
|
+
"credential_delivery_failed",
|
|
185
|
+
"Credential delivery failed. List registrations and rotate the credential to recover.",
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** The acquisition deadline ends once the child starts; signals still stop it. */
|
|
191
|
+
public async runChild(
|
|
192
|
+
material: plugins.dcrouterData.ITestingCertificateDownload,
|
|
193
|
+
args: string[],
|
|
194
|
+
): Promise<number> {
|
|
195
|
+
this.check();
|
|
196
|
+
if (
|
|
197
|
+
!args.length ||
|
|
198
|
+
args.length > 256 ||
|
|
199
|
+
args.some((arg) => typeof arg !== "string" || arg.includes("\0")) ||
|
|
200
|
+
args.join("").length > 65536
|
|
201
|
+
)
|
|
202
|
+
throw new TestingCommandError(
|
|
203
|
+
"invalid_input",
|
|
204
|
+
"Specify a bounded child command after --.",
|
|
205
|
+
);
|
|
206
|
+
if (
|
|
207
|
+
typeof material.certificatePem !== "string" ||
|
|
208
|
+
material.certificatePem.length > 262144 ||
|
|
209
|
+
typeof material.privateKeyPem !== "string" ||
|
|
210
|
+
material.privateKeyPem.length > 32768 ||
|
|
211
|
+
!Array.isArray(material.identifiers) ||
|
|
212
|
+
material.identifiers.length !== 1 ||
|
|
213
|
+
typeof material.identifiers[0] !== "string" ||
|
|
214
|
+
material.identifiers[0].length > 253
|
|
215
|
+
) {
|
|
216
|
+
throw new TestingCommandError(
|
|
217
|
+
"invalid_response",
|
|
218
|
+
"The server returned invalid certificate material.",
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
let directory: string | undefined;
|
|
222
|
+
try {
|
|
223
|
+
directory = await plugins.fs.mkdtemp(
|
|
224
|
+
plugins.path.join(plugins.os.tmpdir(), "gitzone-testing-"),
|
|
225
|
+
);
|
|
226
|
+
await plugins.fs.chmod(directory, 0o700);
|
|
227
|
+
const certificatePath = plugins.path.join(directory, "certificate.pem");
|
|
228
|
+
const keyPath = plugins.path.join(directory, "private-key.pem");
|
|
229
|
+
await plugins.fs.writeFile(certificatePath, material.certificatePem, {
|
|
230
|
+
mode: 0o600,
|
|
231
|
+
flag: "wx",
|
|
232
|
+
});
|
|
233
|
+
await plugins.fs.writeFile(keyPath, material.privateKeyPem, {
|
|
234
|
+
mode: 0o600,
|
|
235
|
+
flag: "wx",
|
|
236
|
+
});
|
|
237
|
+
this.check();
|
|
238
|
+
clearTimeout(this.timer);
|
|
239
|
+
this.timer = undefined;
|
|
240
|
+
const env = Object.fromEntries(
|
|
241
|
+
Object.entries(process.env).filter(
|
|
242
|
+
([key]) => !key.startsWith("GITZONE_TESTING_"),
|
|
243
|
+
),
|
|
244
|
+
);
|
|
245
|
+
env.GITZONE_TESTING_CERTIFICATE_FILE = certificatePath;
|
|
246
|
+
env.GITZONE_TESTING_PRIVATE_KEY_FILE = keyPath;
|
|
247
|
+
env.GITZONE_TESTING_HOSTNAME = material.identifiers[0];
|
|
248
|
+
return await new Promise<number>((resolve, reject) => {
|
|
249
|
+
const child = plugins.childProcess.spawn(args[0], args.slice(1), {
|
|
250
|
+
env,
|
|
251
|
+
stdio: "inherit",
|
|
252
|
+
shell: false,
|
|
253
|
+
});
|
|
254
|
+
let killTimer: ReturnType<typeof setTimeout> | undefined;
|
|
255
|
+
const abort = () => {
|
|
256
|
+
child.kill(this.receivedSignal ?? "SIGTERM");
|
|
257
|
+
killTimer ??= setTimeout(() => child.kill("SIGKILL"), 5000);
|
|
258
|
+
};
|
|
259
|
+
const cleanup = () => {
|
|
260
|
+
clearTimeout(killTimer);
|
|
261
|
+
this.signal.removeEventListener("abort", abort);
|
|
262
|
+
};
|
|
263
|
+
child.once("error", () => {
|
|
264
|
+
cleanup();
|
|
265
|
+
reject(
|
|
266
|
+
new TestingCommandError(
|
|
267
|
+
"child_failed",
|
|
268
|
+
"Could not start the child command.",
|
|
269
|
+
),
|
|
270
|
+
);
|
|
271
|
+
});
|
|
272
|
+
child.once("close", (code, signal) => {
|
|
273
|
+
cleanup();
|
|
274
|
+
resolve(
|
|
275
|
+
this.receivedSignal
|
|
276
|
+
? this.exitCode
|
|
277
|
+
: (code ??
|
|
278
|
+
(signal === "SIGINT" ? 130 : signal === "SIGTERM" ? 143 : 1)),
|
|
279
|
+
);
|
|
280
|
+
});
|
|
281
|
+
this.signal.addEventListener("abort", abort, { once: true });
|
|
282
|
+
if (this.signal.aborted) abort();
|
|
283
|
+
});
|
|
284
|
+
} catch (error) {
|
|
285
|
+
if (error instanceof TestingCommandError) throw error;
|
|
286
|
+
throw new TestingCommandError(
|
|
287
|
+
"child_failed",
|
|
288
|
+
"Could not prepare the child certificate files.",
|
|
289
|
+
);
|
|
290
|
+
} finally {
|
|
291
|
+
if (directory) {
|
|
292
|
+
try {
|
|
293
|
+
await plugins.fs.rm(directory, { recursive: true, force: true });
|
|
294
|
+
} catch {
|
|
295
|
+
throw new TestingCommandError(
|
|
296
|
+
"cleanup_failed",
|
|
297
|
+
"Could not remove the temporary certificate directory.",
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
public stop(): void {
|
|
305
|
+
clearTimeout(this.timer);
|
|
306
|
+
this.controller.abort();
|
|
307
|
+
process.removeListener("SIGINT", this.onInterrupt);
|
|
308
|
+
process.removeListener("SIGTERM", this.onTerminate);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import * as plugins from "../plugins.js";
|
|
2
|
+
import { getCliMode, printJson } from "../helpers.climode.js";
|
|
3
|
+
import type { ICliMode } from "../helpers.climode.js";
|
|
4
|
+
import {
|
|
5
|
+
executeTesting,
|
|
6
|
+
TestingInput,
|
|
7
|
+
testingCommands,
|
|
8
|
+
unsuccessfulStates,
|
|
9
|
+
} from "./classes.commands.js";
|
|
10
|
+
import type { TTestingArgv } from "./classes.commands.js";
|
|
11
|
+
import {
|
|
12
|
+
TestingCommandError,
|
|
13
|
+
TestingCommandRuntime,
|
|
14
|
+
} from "./classes.runtime.js";
|
|
15
|
+
|
|
16
|
+
export function showHelp(mode: ICliMode): void {
|
|
17
|
+
const help = {
|
|
18
|
+
usage: "gitzone testing <command> [options]",
|
|
19
|
+
commands: testingCommands.map(([name, options, description]) => ({
|
|
20
|
+
name,
|
|
21
|
+
options,
|
|
22
|
+
description,
|
|
23
|
+
})),
|
|
24
|
+
environment: [
|
|
25
|
+
"GITZONE_TESTING_BASE_URL",
|
|
26
|
+
"GITZONE_TESTING_TOKEN",
|
|
27
|
+
"GITZONE_TESTING_ADMIN_TOKEN",
|
|
28
|
+
],
|
|
29
|
+
options: [
|
|
30
|
+
"--token-stdin: read the selected credential from a bounded non-terminal stdin instead of the environment",
|
|
31
|
+
"--timeout-ms <n>: acquisition/request deadline, default 300000, maximum 3600000",
|
|
32
|
+
"--json: structured metadata and safe errors",
|
|
33
|
+
],
|
|
34
|
+
notes: [
|
|
35
|
+
"Credentials are never accepted in argv or saved to configuration.",
|
|
36
|
+
"Enrollment and rotation require a writable preopened --secret-fd of at least 3.",
|
|
37
|
+
"cert run disallows JSON output, strips testing credentials from the child environment, and removes its private temporary files when the child exits.",
|
|
38
|
+
"The child receives GITZONE_TESTING_CERTIFICATE_FILE, GITZONE_TESTING_PRIVATE_KEY_FILE, and GITZONE_TESTING_HOSTNAME.",
|
|
39
|
+
"Certificate renewal is on demand. cert run obtains material once; restart the command to renew.",
|
|
40
|
+
"Exit codes: 0 success/admitted work; 1 failure or unresolved terminal operation; 2 invalid input; 130/143 interrupted.",
|
|
41
|
+
"A deadline does not cancel durable server work. Reuse the original idempotency key or poll its operation ID.",
|
|
42
|
+
],
|
|
43
|
+
};
|
|
44
|
+
if (mode.json) {
|
|
45
|
+
printJson(help);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
console.log(help.usage);
|
|
49
|
+
for (const command of help.commands)
|
|
50
|
+
console.log(
|
|
51
|
+
` ${command.name} ${command.options}\n ${command.description}`,
|
|
52
|
+
);
|
|
53
|
+
console.log(
|
|
54
|
+
"\n" + [...help.environment, ...help.options, ...help.notes].join("\n"),
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export async function run(argv: TTestingArgv = {}): Promise<void> {
|
|
59
|
+
const mode = await getCliMode(argv);
|
|
60
|
+
if (mode.help || (argv._ ?? []).length === 1) {
|
|
61
|
+
showHelp(mode);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
let runtime: TestingCommandRuntime | undefined;
|
|
65
|
+
let client: plugins.DcRouterApiClient | undefined;
|
|
66
|
+
let output:
|
|
67
|
+
| {
|
|
68
|
+
ok: boolean;
|
|
69
|
+
value?: unknown;
|
|
70
|
+
error?: {
|
|
71
|
+
code: string;
|
|
72
|
+
message: string;
|
|
73
|
+
operationId?: string;
|
|
74
|
+
retryAt?: number;
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
| undefined;
|
|
78
|
+
try {
|
|
79
|
+
const input = new TestingInput(argv);
|
|
80
|
+
const child = argv["--"];
|
|
81
|
+
if (
|
|
82
|
+
input.command === "cert run" &&
|
|
83
|
+
(mode.json ||
|
|
84
|
+
!Array.isArray(child) ||
|
|
85
|
+
!child.length ||
|
|
86
|
+
child.length > 256 ||
|
|
87
|
+
child.some((arg) => typeof arg !== "string" || arg.includes("\0")) ||
|
|
88
|
+
child.join("").length > 65536)
|
|
89
|
+
) {
|
|
90
|
+
throw new TestingCommandError(
|
|
91
|
+
"invalid_input",
|
|
92
|
+
"cert run requires a child after -- and does not support JSON output.",
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
const baseUrl = process.env.GITZONE_TESTING_BASE_URL;
|
|
96
|
+
try {
|
|
97
|
+
const url = new URL(baseUrl ?? "");
|
|
98
|
+
if (
|
|
99
|
+
url.username ||
|
|
100
|
+
url.password ||
|
|
101
|
+
url.search ||
|
|
102
|
+
url.hash ||
|
|
103
|
+
url.pathname !== "/" ||
|
|
104
|
+
(url.protocol !== "https:" &&
|
|
105
|
+
!(
|
|
106
|
+
url.protocol === "http:" &&
|
|
107
|
+
["localhost", "127.0.0.1", "[::1]"].includes(url.hostname)
|
|
108
|
+
))
|
|
109
|
+
)
|
|
110
|
+
throw new Error();
|
|
111
|
+
} catch {
|
|
112
|
+
throw new TestingCommandError(
|
|
113
|
+
"invalid_input",
|
|
114
|
+
"Set GITZONE_TESTING_BASE_URL to an HTTPS origin (HTTP is allowed only for loopback tests).",
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
runtime = new TestingCommandRuntime(
|
|
118
|
+
input.number("timeout-ms", 1, 3600000, 300000),
|
|
119
|
+
);
|
|
120
|
+
const credentialName = input.admin
|
|
121
|
+
? "GITZONE_TESTING_ADMIN_TOKEN"
|
|
122
|
+
: "GITZONE_TESTING_TOKEN";
|
|
123
|
+
let token = process.env[credentialName];
|
|
124
|
+
if (argv["token-stdin"] === true) {
|
|
125
|
+
if (token)
|
|
126
|
+
throw new TestingCommandError(
|
|
127
|
+
"invalid_input",
|
|
128
|
+
"Select one credential source: environment or secret stdin.",
|
|
129
|
+
);
|
|
130
|
+
token = await runtime.readToken();
|
|
131
|
+
}
|
|
132
|
+
if (!token || token.length > 8192 || /\s/.test(token))
|
|
133
|
+
throw new TestingCommandError(
|
|
134
|
+
"invalid_input",
|
|
135
|
+
`Set ${credentialName} or use --token-stdin.`,
|
|
136
|
+
);
|
|
137
|
+
const { DcRouterApiClient } = await plugins.loadDcrouterApi();
|
|
138
|
+
client = new DcRouterApiClient({ baseUrl: baseUrl!, apiToken: token });
|
|
139
|
+
token = undefined;
|
|
140
|
+
const result = await executeTesting(input, client.testing, runtime);
|
|
141
|
+
if (result !== undefined) {
|
|
142
|
+
output = { ok: true, value: result };
|
|
143
|
+
if (
|
|
144
|
+
result &&
|
|
145
|
+
typeof result === "object" &&
|
|
146
|
+
"state" in result &&
|
|
147
|
+
unsuccessfulStates.has(String(result.state))
|
|
148
|
+
)
|
|
149
|
+
process.exitCode = 1;
|
|
150
|
+
}
|
|
151
|
+
} catch (error) {
|
|
152
|
+
const failure = runtime?.signal.aborted
|
|
153
|
+
? new TestingCommandError(
|
|
154
|
+
runtime.exitCode === 1 ? "timeout" : "cancelled",
|
|
155
|
+
"Testing command stopped before completion.",
|
|
156
|
+
error instanceof TestingCommandError ? error.details : undefined,
|
|
157
|
+
)
|
|
158
|
+
: error instanceof TestingCommandError
|
|
159
|
+
? error
|
|
160
|
+
: new TestingCommandError(
|
|
161
|
+
"transport_unavailable",
|
|
162
|
+
"The testing request could not be completed.",
|
|
163
|
+
);
|
|
164
|
+
process.exitCode =
|
|
165
|
+
failure.code === "invalid_input" ? 2 : (runtime?.exitCode ?? 1);
|
|
166
|
+
output = {
|
|
167
|
+
ok: false,
|
|
168
|
+
error: {
|
|
169
|
+
code: failure.code,
|
|
170
|
+
message: failure.message,
|
|
171
|
+
...failure.details,
|
|
172
|
+
},
|
|
173
|
+
};
|
|
174
|
+
} finally {
|
|
175
|
+
runtime?.stop();
|
|
176
|
+
if (client) {
|
|
177
|
+
client.apiToken = undefined;
|
|
178
|
+
try {
|
|
179
|
+
await client.stop();
|
|
180
|
+
} catch {
|
|
181
|
+
process.exitCode = process.exitCode || 1;
|
|
182
|
+
output = {
|
|
183
|
+
ok: false,
|
|
184
|
+
error: {
|
|
185
|
+
code: "cleanup_failed",
|
|
186
|
+
message: "Could not stop the testing client.",
|
|
187
|
+
},
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
if (output) {
|
|
193
|
+
if (mode.json) printJson(output);
|
|
194
|
+
else if (output.ok) console.log(JSON.stringify(output.value, null, 2));
|
|
195
|
+
else console.error(JSON.stringify(output));
|
|
196
|
+
}
|
|
197
|
+
}
|
package/ts/plugins.ts
CHANGED
|
@@ -6,6 +6,9 @@ import * as os from 'node:os';
|
|
|
6
6
|
import * as fs from 'node:fs/promises';
|
|
7
7
|
import { constants as fsConstants } from 'node:fs';
|
|
8
8
|
import * as crypto from 'node:crypto';
|
|
9
|
+
import * as fsSync from 'node:fs';
|
|
10
|
+
import * as childProcess from 'node:child_process';
|
|
11
|
+
import * as tty from 'node:tty';
|
|
9
12
|
import * as projectinfo from '@push.rocks/projectinfo';
|
|
10
13
|
import * as smartcli from '@push.rocks/smartcli';
|
|
11
14
|
import * as smartpath from '@push.rocks/smartpath';
|
|
@@ -39,6 +42,10 @@ export type {
|
|
|
39
42
|
// Create smartfs instance for filesystem operations
|
|
40
43
|
export const smartfs = new SmartFs(new SmartFsProviderNode());
|
|
41
44
|
|
|
45
|
+
export const loadDcrouterApi = () => import('@serve.zone/dcrouter/apiclient');
|
|
46
|
+
export type { DcRouterApiClient, TestingApi } from '@serve.zone/dcrouter/apiclient';
|
|
47
|
+
export type { data as dcrouterData } from '@serve.zone/dcrouter/interfaces';
|
|
48
|
+
|
|
42
49
|
export {
|
|
43
50
|
smartlog,
|
|
44
51
|
smartlogDestinationLocal,
|
|
@@ -48,6 +55,9 @@ export {
|
|
|
48
55
|
fs,
|
|
49
56
|
fsConstants,
|
|
50
57
|
crypto,
|
|
58
|
+
fsSync,
|
|
59
|
+
childProcess,
|
|
60
|
+
tty,
|
|
51
61
|
projectinfo,
|
|
52
62
|
smartcli,
|
|
53
63
|
smartpath,
|