@tsplc/agent 0.2.14 → 0.2.16
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/cli.js +292 -29
- package/dist/cli.js.map +7 -6
- package/dist/client.js +29 -4
- package/dist/client.js.map +4 -4
- package/dist/index.js +292 -28
- package/dist/index.js.map +6 -5
- package/package.json +4 -3
- package/src/agent.ts +267 -17
- package/src/cli.ts +9 -1
- package/src/client.ts +7 -2
- package/src/logs.ts +39 -0
- package/src/protocol.ts +39 -0
package/dist/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// packages/agent/src/agent.ts
|
|
2
2
|
import { execFile } from "node:child_process";
|
|
3
3
|
import { timingSafeEqual } from "node:crypto";
|
|
4
|
-
import { existsSync as
|
|
5
|
-
import { mkdir as mkdir2, readdir, readFile as
|
|
4
|
+
import { existsSync as existsSync3 } from "node:fs";
|
|
5
|
+
import { mkdir as mkdir2, readdir, readFile as readFile3, stat as stat2, writeFile as writeFile2 } from "node:fs/promises";
|
|
6
6
|
import { join as join2 } from "node:path";
|
|
7
7
|
import { setTimeout as delay2 } from "node:timers/promises";
|
|
8
8
|
import { promisify } from "node:util";
|
|
@@ -17,6 +17,15 @@ import {
|
|
|
17
17
|
startWarmController,
|
|
18
18
|
updateWarmController
|
|
19
19
|
} from "@tsplc/build";
|
|
20
|
+
import {
|
|
21
|
+
createDebugger,
|
|
22
|
+
createDebugHandler,
|
|
23
|
+
createLoginAdapter,
|
|
24
|
+
loadManifest,
|
|
25
|
+
LoginUnavailable,
|
|
26
|
+
noLogin,
|
|
27
|
+
timelineEntries
|
|
28
|
+
} from "@tsplc/debug";
|
|
20
29
|
import { z as z3 } from "zod";
|
|
21
30
|
|
|
22
31
|
// packages/agent/src/discovery.ts
|
|
@@ -69,6 +78,7 @@ var discoverControllers = async (network, port = runtimePort) => {
|
|
|
69
78
|
};
|
|
70
79
|
|
|
71
80
|
// packages/agent/src/protocol.ts
|
|
81
|
+
import { tsplcConfigSchema } from "@tsplc/config";
|
|
72
82
|
import { z } from "zod";
|
|
73
83
|
var targetSchema = z.strictObject({
|
|
74
84
|
host: z.string().nonempty(),
|
|
@@ -92,7 +102,9 @@ var variableSchema = z.strictObject({ path: z.string(), type: z.string() });
|
|
|
92
102
|
var releaseManifestSchema = z.strictObject({
|
|
93
103
|
config: z.record(z.string(), z.unknown()),
|
|
94
104
|
variables: z.array(variableSchema),
|
|
95
|
-
server: z.string().nonempty().optional()
|
|
105
|
+
server: z.string().nonempty().optional(),
|
|
106
|
+
debug: tsplcConfigSchema.shape.debug,
|
|
107
|
+
serverTokenEnv: z.string().nonempty().optional()
|
|
96
108
|
});
|
|
97
109
|
var uploadEncoding = "gzip";
|
|
98
110
|
var deployOptionsSchema = z.strictObject({
|
|
@@ -125,11 +137,65 @@ var statusSchema = z.strictObject({
|
|
|
125
137
|
z.strictObject({ error: z.string() })
|
|
126
138
|
]).nullable()
|
|
127
139
|
});
|
|
140
|
+
var logoutSchema = z.strictObject({ failure: z.string().optional() });
|
|
141
|
+
var clientHeaders = { user: "x-tsplc-user", host: "x-tsplc-host" };
|
|
142
|
+
var deploymentSchema = z.strictObject({
|
|
143
|
+
at: z.iso.datetime(),
|
|
144
|
+
release: z.string(),
|
|
145
|
+
build: z.string().optional(),
|
|
146
|
+
operation: z.enum(["download", "online-change"]),
|
|
147
|
+
user: z.string().optional(),
|
|
148
|
+
host: z.string().optional()
|
|
149
|
+
});
|
|
150
|
+
var auditSchema = z.strictObject({
|
|
151
|
+
at: z.iso.datetime(),
|
|
152
|
+
command: z.string(),
|
|
153
|
+
values: z.record(z.string(), z.string()),
|
|
154
|
+
previous: z.record(z.string(), z.string()).optional(),
|
|
155
|
+
user: z.string().optional(),
|
|
156
|
+
host: z.string().optional(),
|
|
157
|
+
release: z.string().optional(),
|
|
158
|
+
error: z.string().optional()
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
// packages/agent/src/logs.ts
|
|
162
|
+
import { existsSync } from "node:fs";
|
|
163
|
+
import { open, readFile, rename, stat } from "node:fs/promises";
|
|
164
|
+
var rotateBytes = 5 * 2 ** 20;
|
|
165
|
+
var appendJsonl = async (file, value) => {
|
|
166
|
+
if (existsSync(file) && (await stat(file)).size >= rotateBytes)
|
|
167
|
+
await rename(file, `${file}.1`);
|
|
168
|
+
const log = await open(file, "a+");
|
|
169
|
+
try {
|
|
170
|
+
const { size } = await log.stat();
|
|
171
|
+
const last = Buffer.alloc(1);
|
|
172
|
+
if (size)
|
|
173
|
+
await log.read(last, 0, 1, size - 1);
|
|
174
|
+
const separator = size && last[0] !== 10 ? `
|
|
175
|
+
` : "";
|
|
176
|
+
await log.writeFile(`${separator}${JSON.stringify(value)}
|
|
177
|
+
`);
|
|
178
|
+
} finally {
|
|
179
|
+
await log.close();
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
var readJsonl = async (file, schema, limit) => {
|
|
183
|
+
if (!existsSync(file))
|
|
184
|
+
return [];
|
|
185
|
+
return (await readFile(file, "utf8")).split(`
|
|
186
|
+
`).filter(Boolean).slice(-limit).flatMap((line) => {
|
|
187
|
+
try {
|
|
188
|
+
return [schema.parse(JSON.parse(line))];
|
|
189
|
+
} catch {
|
|
190
|
+
return [];
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
};
|
|
128
194
|
|
|
129
195
|
// packages/agent/src/releases.ts
|
|
130
196
|
import { createHash } from "node:crypto";
|
|
131
|
-
import { existsSync } from "node:fs";
|
|
132
|
-
import { mkdir, mkdtemp, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
197
|
+
import { existsSync as existsSync2 } from "node:fs";
|
|
198
|
+
import { mkdir, mkdtemp, readFile as readFile2, rename as rename2, rm, writeFile } from "node:fs/promises";
|
|
133
199
|
import { dirname, join, posix } from "node:path";
|
|
134
200
|
import { z as z2 } from "zod";
|
|
135
201
|
var filePrefix = "files/";
|
|
@@ -157,7 +223,7 @@ var saveRelease = async (releases, { manifest, files }) => {
|
|
|
157
223
|
hash.update(file.path).update(createHash("sha256").update(file.content).digest("hex"));
|
|
158
224
|
const id = hash.digest("hex").slice(0, 16);
|
|
159
225
|
const directory = join(releases, id);
|
|
160
|
-
if (!
|
|
226
|
+
if (!existsSync2(directory)) {
|
|
161
227
|
await mkdir(releases, { recursive: true });
|
|
162
228
|
const staging = await mkdtemp(join(releases, ".upload-"));
|
|
163
229
|
try {
|
|
@@ -168,7 +234,7 @@ var saveRelease = async (releases, { manifest, files }) => {
|
|
|
168
234
|
});
|
|
169
235
|
}
|
|
170
236
|
await writeFile(join(staging, "manifest.json"), JSON.stringify(manifest, null, 2));
|
|
171
|
-
await
|
|
237
|
+
await rename2(staging, directory);
|
|
172
238
|
} catch (cause) {
|
|
173
239
|
await rm(staging, { recursive: true, force: true });
|
|
174
240
|
throw cause;
|
|
@@ -178,7 +244,7 @@ var saveRelease = async (releases, { manifest, files }) => {
|
|
|
178
244
|
};
|
|
179
245
|
var loadRelease = async (releases, id) => {
|
|
180
246
|
const directory = join(releases, id);
|
|
181
|
-
const manifest = releaseManifestSchema.parse(JSON.parse(await
|
|
247
|
+
const manifest = releaseManifestSchema.parse(JSON.parse(await readFile2(join(directory, "manifest.json"), "utf8")));
|
|
182
248
|
return { id, directory, manifest };
|
|
183
249
|
};
|
|
184
250
|
|
|
@@ -277,7 +343,40 @@ class AgentError extends Error {
|
|
|
277
343
|
}
|
|
278
344
|
}
|
|
279
345
|
var json = (body, status = 200) => Response.json(body, { status });
|
|
280
|
-
var
|
|
346
|
+
var message = (error) => error instanceof Error ? error.message : String(error);
|
|
347
|
+
var loginStateTimeout = 30000;
|
|
348
|
+
var stoppedAnswering = { error: "The login stopped answering" };
|
|
349
|
+
var recentEntries = 500;
|
|
350
|
+
var clientOf = (request) => {
|
|
351
|
+
const header = (name) => request.headers.get(name)?.slice(0, 200) || undefined;
|
|
352
|
+
return { user: header(clientHeaders.user), host: header(clientHeaders.host) };
|
|
353
|
+
};
|
|
354
|
+
var auditOf = (command) => {
|
|
355
|
+
switch (command.command) {
|
|
356
|
+
case "write":
|
|
357
|
+
case "force":
|
|
358
|
+
return { command: command.command, values: command.values };
|
|
359
|
+
case "unforce":
|
|
360
|
+
return {
|
|
361
|
+
command: command.restore ? "unforce --restore" : "unforce",
|
|
362
|
+
values: Object.fromEntries(command.expressions.map((expression) => [expression, ""]))
|
|
363
|
+
};
|
|
364
|
+
case "start":
|
|
365
|
+
case "stop":
|
|
366
|
+
return { command: command.command, values: {} };
|
|
367
|
+
case "reset":
|
|
368
|
+
return { command: `reset ${command.kind.toLowerCase()}`, values: {} };
|
|
369
|
+
case "state":
|
|
370
|
+
case "read":
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
var matches = (presented, token) => {
|
|
375
|
+
const actual = Buffer.from(presented);
|
|
376
|
+
const expected = Buffer.from(token);
|
|
377
|
+
return actual.length === expected.length && timingSafeEqual(actual, expected);
|
|
378
|
+
};
|
|
379
|
+
var readJson = async (path, schema) => existsSync3(path) ? schema.parse(JSON.parse(await readFile3(path, "utf8"))) : null;
|
|
281
380
|
var docker = (...args) => execute("docker", args).then(({ stdout }) => stdout.trim());
|
|
282
381
|
var ensureNetwork = async (name, ...options) => {
|
|
283
382
|
if (!await docker("network", "ls", "--quiet", "--filter", `name=^${name}$`))
|
|
@@ -313,16 +412,16 @@ var followProgress = (controller, emit) => {
|
|
|
313
412
|
let offset = 0;
|
|
314
413
|
while (following) {
|
|
315
414
|
await delay2(2000);
|
|
316
|
-
if (!log &&
|
|
415
|
+
if (!log && existsSync3(jobs)) {
|
|
317
416
|
for (const job of await readdir(jobs)) {
|
|
318
417
|
const path = join2(jobs, job, ".codesys/controller/progress.log");
|
|
319
|
-
if (
|
|
418
|
+
if (existsSync3(path) && (await stat2(path)).mtimeMs >= since)
|
|
320
419
|
log = path;
|
|
321
420
|
}
|
|
322
421
|
}
|
|
323
422
|
if (!log)
|
|
324
423
|
continue;
|
|
325
|
-
const text = await
|
|
424
|
+
const text = await readFile3(log, "utf8");
|
|
326
425
|
for (const line of text.slice(offset).split(`
|
|
327
426
|
`).filter(Boolean))
|
|
328
427
|
emit({ kind: "progress", message: line.replace(/^\S+Z /, "") });
|
|
@@ -339,7 +438,10 @@ var createAgent = async (options) => {
|
|
|
339
438
|
releases: join2(options.state, "releases"),
|
|
340
439
|
controller: join2(options.state, "controller"),
|
|
341
440
|
release: join2(options.state, "release.json"),
|
|
342
|
-
target: join2(options.state, "target.json")
|
|
441
|
+
target: join2(options.state, "target.json"),
|
|
442
|
+
deployments: join2(options.state, "deployments.jsonl"),
|
|
443
|
+
audit: join2(options.state, "audit.jsonl"),
|
|
444
|
+
debug: join2(options.state, "debug")
|
|
343
445
|
};
|
|
344
446
|
await mkdir2(options.state, { recursive: true });
|
|
345
447
|
const server = createServerSupervisor({ healthUrl: options.serverHealthUrl, log: options.log });
|
|
@@ -376,6 +478,78 @@ var createAgent = async (options) => {
|
|
|
376
478
|
})().catch((error) => {
|
|
377
479
|
options.log(`Starting CODESYS failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
378
480
|
});
|
|
481
|
+
let loginGeneration = 0;
|
|
482
|
+
let commands = Promise.resolve();
|
|
483
|
+
const enqueue = (run) => {
|
|
484
|
+
const generation = loginGeneration;
|
|
485
|
+
const sent = commands.then(() => {
|
|
486
|
+
if (generation !== loginGeneration)
|
|
487
|
+
throw new AgentError("The login was closed", 409);
|
|
488
|
+
return run();
|
|
489
|
+
});
|
|
490
|
+
commands = sent.catch(() => {
|
|
491
|
+
return;
|
|
492
|
+
});
|
|
493
|
+
return sent;
|
|
494
|
+
};
|
|
495
|
+
const send = (login, loginCommand) => enqueue(() => login.send(loginCommand));
|
|
496
|
+
const readLogin = async ([first, ...rest]) => {
|
|
497
|
+
if (busy === "a login")
|
|
498
|
+
throw new LoginUnavailable("The engineering login is opening", { state: "connecting" });
|
|
499
|
+
if (busy)
|
|
500
|
+
throw new LoginUnavailable(`Live values pause during ${busy}`);
|
|
501
|
+
const login = await findControllerLogin(loginContainer);
|
|
502
|
+
if (!login)
|
|
503
|
+
throw new LoginUnavailable(noLogin);
|
|
504
|
+
if (!first)
|
|
505
|
+
return {};
|
|
506
|
+
const result = await send(login, { command: "read", expressions: [first, ...rest] });
|
|
507
|
+
if (!("values" in result))
|
|
508
|
+
throw new Error("The login answered a read without values");
|
|
509
|
+
return result.values;
|
|
510
|
+
};
|
|
511
|
+
const startDebugger = async () => {
|
|
512
|
+
const release = await readJson(paths.release, releaseSchema);
|
|
513
|
+
if (!release)
|
|
514
|
+
return null;
|
|
515
|
+
const { directory, manifest: releaseManifest } = await loadRelease(paths.releases, release.id);
|
|
516
|
+
const manifestFile = join2(directory, "debug.json");
|
|
517
|
+
if (!existsSync3(manifestFile))
|
|
518
|
+
return null;
|
|
519
|
+
const manifest = await loadManifest(manifestFile);
|
|
520
|
+
const serverToken = releaseManifest.serverTokenEnv && process.env[releaseManifest.serverTokenEnv];
|
|
521
|
+
const debug = await createDebugger({
|
|
522
|
+
manifest,
|
|
523
|
+
adapter: createLoginAdapter({
|
|
524
|
+
read: readLogin,
|
|
525
|
+
probe: manifest.build.target,
|
|
526
|
+
endpoint: "the agent's login"
|
|
527
|
+
}),
|
|
528
|
+
stateDirectory: paths.debug,
|
|
529
|
+
watch: releaseManifest.debug?.watch ?? {},
|
|
530
|
+
...options.serverHealthUrl && serverToken && {
|
|
531
|
+
server: { url: new URL(options.serverHealthUrl).origin, token: serverToken }
|
|
532
|
+
},
|
|
533
|
+
release: { id: release.id, deployedAt: release.deployedAt, operation: release.operation },
|
|
534
|
+
timeline: [
|
|
535
|
+
...(await readJsonl(paths.deployments, deploymentSchema, recentEntries)).map(({ at, ...deployment }) => timelineEntries.deploy(at, deployment)),
|
|
536
|
+
...(await readJsonl(paths.audit, auditSchema, recentEntries)).map(({ at, ...audit }) => timelineEntries.audit(at, audit))
|
|
537
|
+
],
|
|
538
|
+
log: options.log
|
|
539
|
+
});
|
|
540
|
+
return { debug, handle: createDebugHandler(debug, options.log) };
|
|
541
|
+
};
|
|
542
|
+
const debuggerFailed = (error) => {
|
|
543
|
+
options.log(`Starting the debugger failed: ${message(error)}`);
|
|
544
|
+
return null;
|
|
545
|
+
};
|
|
546
|
+
let debugging = startDebugger().catch(debuggerFailed);
|
|
547
|
+
const restartDebugger = () => {
|
|
548
|
+
debugging = debugging.then(async (current) => {
|
|
549
|
+
await current?.debug.close();
|
|
550
|
+
return startDebugger().catch(debuggerFailed);
|
|
551
|
+
});
|
|
552
|
+
};
|
|
379
553
|
const operation = (name, run) => {
|
|
380
554
|
if (busy)
|
|
381
555
|
throw new AgentError(`The agent is busy with ${busy}`, 409);
|
|
@@ -413,7 +587,7 @@ var createAgent = async (options) => {
|
|
|
413
587
|
}
|
|
414
588
|
}), { headers: { "Content-Type": "application/x-ndjson" } });
|
|
415
589
|
};
|
|
416
|
-
const deploy = async (form) => {
|
|
590
|
+
const deploy = async (form, client) => {
|
|
417
591
|
const settings = deployOptionsSchema.parse(JSON.parse(z3.string().parse(form.get("options"))));
|
|
418
592
|
const upload = await readUpload(form);
|
|
419
593
|
const target = await requireTarget();
|
|
@@ -422,7 +596,7 @@ var createAgent = async (options) => {
|
|
|
422
596
|
return operation("a deployment", async (emit) => {
|
|
423
597
|
const release = await saveRelease(paths.releases, upload);
|
|
424
598
|
const config = controllerConfig(release, target);
|
|
425
|
-
if (!settings.download && !
|
|
599
|
+
if (!settings.download && !existsSync3(join2(paths.controller, ".codesys/controller/current.json")))
|
|
426
600
|
throw new AgentError("The controller has no application downloaded by this agent; only a full download can deploy", 409, true);
|
|
427
601
|
const previous = await readJson(paths.release, releaseSchema);
|
|
428
602
|
const serverPath = release.manifest.server && `${release.id}/${release.manifest.server}`;
|
|
@@ -453,6 +627,16 @@ var createAgent = async (options) => {
|
|
|
453
627
|
server: serverPath ?? previous?.server
|
|
454
628
|
};
|
|
455
629
|
await writeFile2(paths.release, JSON.stringify(deployedRelease, null, 2));
|
|
630
|
+
const debugManifest = join2(release.directory, "debug.json");
|
|
631
|
+
const deployment = {
|
|
632
|
+
at: deployedRelease.deployedAt,
|
|
633
|
+
release: release.id,
|
|
634
|
+
...existsSync3(debugManifest) && { build: (await loadManifest(debugManifest)).build.id },
|
|
635
|
+
operation: kind,
|
|
636
|
+
...client
|
|
637
|
+
};
|
|
638
|
+
await appendJsonl(paths.deployments, deployment);
|
|
639
|
+
restartDebugger();
|
|
456
640
|
return deployedRelease;
|
|
457
641
|
});
|
|
458
642
|
};
|
|
@@ -465,21 +649,68 @@ var createAgent = async (options) => {
|
|
|
465
649
|
emit({ kind: "progress", message: "Logging in; CODESYS takes minutes to start" });
|
|
466
650
|
await ensureGateway();
|
|
467
651
|
const login = await withCredentials(credentials, () => openControllerLogin(config, loginContainer));
|
|
468
|
-
return
|
|
652
|
+
return send(login, { command: "state" });
|
|
469
653
|
});
|
|
470
654
|
};
|
|
471
655
|
const requireLogin = async () => {
|
|
656
|
+
if (busy)
|
|
657
|
+
throw new AgentError(`The agent is busy with ${busy}`, 409);
|
|
658
|
+
const generation = loginGeneration;
|
|
472
659
|
const login = await findControllerLogin(loginContainer);
|
|
660
|
+
if (generation !== loginGeneration)
|
|
661
|
+
throw new AgentError("The login was closed", 409);
|
|
662
|
+
if (busy)
|
|
663
|
+
throw new AgentError(`The agent is busy with ${busy}`, 409);
|
|
473
664
|
if (!login)
|
|
474
665
|
throw new AgentError("No login is open: tsplc login", 409);
|
|
475
666
|
return login;
|
|
476
667
|
};
|
|
668
|
+
const command = async (loginCommand, client) => {
|
|
669
|
+
const login = await requireLogin();
|
|
670
|
+
const change = auditOf(loginCommand);
|
|
671
|
+
if (!change)
|
|
672
|
+
return send(login, loginCommand);
|
|
673
|
+
return enqueue(async () => {
|
|
674
|
+
const [first, ...rest] = Object.keys(change.values);
|
|
675
|
+
const previous = first ? await login.send({ command: "read", expressions: [first, ...rest] }).then((result) => ("values" in result) ? result.values : undefined).catch(() => {
|
|
676
|
+
return;
|
|
677
|
+
}) : undefined;
|
|
678
|
+
const audit = async (error) => {
|
|
679
|
+
const entry = {
|
|
680
|
+
at: new Date().toISOString(),
|
|
681
|
+
...change,
|
|
682
|
+
...previous && { previous },
|
|
683
|
+
...client,
|
|
684
|
+
release: (await readJson(paths.release, releaseSchema))?.id,
|
|
685
|
+
...error !== undefined && { error: message(error) }
|
|
686
|
+
};
|
|
687
|
+
await appendJsonl(paths.audit, entry);
|
|
688
|
+
const { at, ...details } = entry;
|
|
689
|
+
(await debugging)?.debug.record(timelineEntries.audit(at, details));
|
|
690
|
+
};
|
|
691
|
+
try {
|
|
692
|
+
const result = await login.send(loginCommand);
|
|
693
|
+
await audit();
|
|
694
|
+
return result;
|
|
695
|
+
} catch (error) {
|
|
696
|
+
await audit(error);
|
|
697
|
+
throw error;
|
|
698
|
+
}
|
|
699
|
+
});
|
|
700
|
+
};
|
|
477
701
|
const loginStatus = async () => {
|
|
478
702
|
const login = await findControllerLogin(loginContainer);
|
|
479
|
-
if (!login
|
|
703
|
+
if (!login)
|
|
480
704
|
return null;
|
|
705
|
+
if (!await login.ready())
|
|
706
|
+
return busy === "a login" ? null : stoppedAnswering;
|
|
481
707
|
try {
|
|
482
|
-
const state = await
|
|
708
|
+
const state = await Promise.race([
|
|
709
|
+
send(login, { command: "state" }),
|
|
710
|
+
delay2(loginStateTimeout, null)
|
|
711
|
+
]);
|
|
712
|
+
if (!state)
|
|
713
|
+
return stoppedAnswering;
|
|
483
714
|
return "state" in state ? state : null;
|
|
484
715
|
} catch (error) {
|
|
485
716
|
return { error: failureReason(error) };
|
|
@@ -514,25 +745,51 @@ var createAgent = async (options) => {
|
|
|
514
745
|
case "GET /variables":
|
|
515
746
|
return json((await currentRelease()).manifest.variables);
|
|
516
747
|
case "POST /deploy":
|
|
517
|
-
return deploy(await request.formData());
|
|
748
|
+
return deploy(await request.formData(), clientOf(request));
|
|
518
749
|
case "POST /login":
|
|
519
750
|
return openLogin(credentialsSchema.parse(await request.json()));
|
|
520
751
|
case "POST /login/command":
|
|
521
|
-
return json(await (
|
|
522
|
-
case "DELETE /login":
|
|
523
|
-
|
|
524
|
-
|
|
752
|
+
return json(await command(loginCommandSchema.parse(await request.json()), clientOf(request)));
|
|
753
|
+
case "DELETE /login": {
|
|
754
|
+
const login = await requireLogin();
|
|
755
|
+
busy = "a logout";
|
|
756
|
+
loginGeneration++;
|
|
757
|
+
try {
|
|
758
|
+
const failure = await login.close();
|
|
759
|
+
commands = Promise.resolve();
|
|
760
|
+
if (failure)
|
|
761
|
+
options.log(`The login couldn't log out (${failure}); its container was removed`);
|
|
762
|
+
return json(failure ? { failure } : {});
|
|
763
|
+
} finally {
|
|
764
|
+
busy = undefined;
|
|
765
|
+
}
|
|
766
|
+
}
|
|
525
767
|
default:
|
|
526
768
|
return json({ error: `No route ${key}` }, 404);
|
|
527
769
|
}
|
|
528
770
|
};
|
|
529
|
-
const
|
|
771
|
+
const debugRoute = async (request, route) => {
|
|
772
|
+
const current = await debugging;
|
|
773
|
+
if (!current)
|
|
774
|
+
return json({ error: "The deployed release has no debug manifest, or the debugger failed to start" }, 503);
|
|
775
|
+
return current.handle(request, route);
|
|
776
|
+
};
|
|
530
777
|
return {
|
|
531
778
|
fetch: async (request) => {
|
|
532
|
-
const
|
|
533
|
-
|
|
779
|
+
const { pathname, search, searchParams } = new URL(request.url);
|
|
780
|
+
const debugPath = pathname.startsWith("/debug/");
|
|
781
|
+
if (pathname === "/debug")
|
|
782
|
+
return new Response(null, { status: 308, headers: { Location: `/debug/${search}` } });
|
|
783
|
+
if (request.method === "GET" && (pathname === "/debug/" || pathname === "/debug/ui.js"))
|
|
784
|
+
return debugRoute(request, pathname.slice("/debug".length));
|
|
785
|
+
const presented = /^Bearer (.+)$/.exec(request.headers.get("authorization") ?? "")?.[1] ?? (debugPath ? searchParams.get("token") : null) ?? "";
|
|
786
|
+
const full = matches(presented, options.token);
|
|
787
|
+
const read = options.readToken !== undefined && matches(presented, options.readToken) && (debugPath || request.method === "GET" && pathname === "/status");
|
|
788
|
+
if (!full && !read)
|
|
534
789
|
return json({ error: "Unauthorized" }, 401);
|
|
535
790
|
try {
|
|
791
|
+
if (debugPath)
|
|
792
|
+
return await debugRoute(request, pathname.slice("/debug".length));
|
|
536
793
|
return await route(request);
|
|
537
794
|
} catch (error) {
|
|
538
795
|
if (error instanceof AgentError)
|
|
@@ -543,15 +800,22 @@ var createAgent = async (options) => {
|
|
|
543
800
|
return json({ error: error instanceof Error ? error.message : String(error) }, 500);
|
|
544
801
|
}
|
|
545
802
|
},
|
|
546
|
-
stop: () =>
|
|
803
|
+
stop: async () => {
|
|
804
|
+
await (await debugging)?.debug.close();
|
|
805
|
+
await server.stop();
|
|
806
|
+
}
|
|
547
807
|
};
|
|
548
808
|
};
|
|
549
809
|
export {
|
|
810
|
+
auditSchema,
|
|
811
|
+
clientHeaders,
|
|
550
812
|
controllerSchema,
|
|
551
813
|
createAgent,
|
|
552
814
|
credentialsSchema,
|
|
553
815
|
deployOptionsSchema,
|
|
816
|
+
deploymentSchema,
|
|
554
817
|
eventSchema,
|
|
818
|
+
logoutSchema,
|
|
555
819
|
networkInterfaceSchema,
|
|
556
820
|
releaseManifestSchema,
|
|
557
821
|
releaseSchema,
|
|
@@ -561,5 +825,5 @@ export {
|
|
|
561
825
|
variableSchema
|
|
562
826
|
};
|
|
563
827
|
|
|
564
|
-
//# debugId=
|
|
828
|
+
//# debugId=1614B96D4F00471464756E2164756E21
|
|
565
829
|
//# sourceMappingURL=index.js.map
|