@tsplc/agent 0.2.17 → 0.2.19
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 +2 -2
- package/dist/cli.js +936 -550
- package/dist/cli.js.map +20 -10
- package/dist/client.js +46 -20
- package/dist/client.js.map +4 -4
- package/dist/index.js +879 -538
- package/dist/index.js.map +18 -9
- package/package.json +50 -49
- package/src/agent.ts +175 -642
- package/src/cli.ts +95 -47
- package/src/client.ts +148 -112
- package/src/codesys.ts +117 -0
- package/src/debugging.ts +134 -0
- package/src/deploy.ts +193 -0
- package/src/discovery.ts +61 -53
- package/src/errors.ts +17 -0
- package/src/index.ts +30 -1
- package/src/login.ts +238 -0
- package/src/logs.ts +36 -26
- package/src/operations.ts +87 -0
- package/src/protocol.ts +108 -69
- package/src/releases.ts +89 -57
- package/src/secrets.ts +118 -0
- package/src/server-process.ts +98 -77
- package/src/state.ts +71 -0
- package/src/terminal.ts +57 -0
package/src/releases.ts
CHANGED
|
@@ -1,81 +1,113 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
2
|
import { existsSync } from "node:fs";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
mkdir,
|
|
5
|
+
mkdtemp,
|
|
6
|
+
readFile,
|
|
7
|
+
rename,
|
|
8
|
+
rm,
|
|
9
|
+
writeFile,
|
|
10
|
+
} from "node:fs/promises";
|
|
4
11
|
import { dirname, join, posix } from "node:path";
|
|
5
12
|
import { z } from "zod";
|
|
6
|
-
import {
|
|
13
|
+
import {
|
|
14
|
+
type ReleaseManifest,
|
|
15
|
+
releaseManifestSchema,
|
|
16
|
+
uploadEncoding,
|
|
17
|
+
} from "./protocol";
|
|
7
18
|
|
|
8
19
|
/** Uploaded files are named `files/<path in the release>`. */
|
|
9
20
|
const filePrefix = "files/";
|
|
10
21
|
|
|
11
22
|
const releasePath = (name: string) => {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
23
|
+
const path = posix.normalize(name.slice(filePrefix.length));
|
|
24
|
+
if (posix.isAbsolute(path) || path.startsWith("..") || path.includes("\\")) {
|
|
25
|
+
throw new Error(`Invalid release file name: ${name}`);
|
|
26
|
+
}
|
|
27
|
+
return path;
|
|
16
28
|
};
|
|
17
29
|
|
|
18
30
|
/** An uploaded release, from `tsplc deploy`'s form. */
|
|
19
31
|
export const readUpload = async (form: FormData) => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
32
|
+
if (form.get("encoding") !== uploadEncoding) {
|
|
33
|
+
throw new Error(
|
|
34
|
+
"This deployment came from an older tsplc: update tsplc where you deploy from.",
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
const manifest = releaseManifestSchema.parse(
|
|
38
|
+
JSON.parse(z.string().parse(form.get("manifest"))),
|
|
39
|
+
);
|
|
40
|
+
const files = await Promise.all(
|
|
41
|
+
[...form.entries()]
|
|
42
|
+
.filter(([name]) => name.startsWith(filePrefix))
|
|
43
|
+
.map(async ([name, value]) => ({
|
|
44
|
+
path: releasePath(name),
|
|
45
|
+
content: Bun.gunzipSync(await z.instanceof(File).parse(value).bytes()),
|
|
46
|
+
})),
|
|
47
|
+
);
|
|
48
|
+
if (manifest.server && !files.some((file) => file.path === manifest.server)) {
|
|
49
|
+
throw new Error(
|
|
50
|
+
`The release names a server ${manifest.server} it doesn't contain`,
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
return { manifest, files };
|
|
36
54
|
};
|
|
55
|
+
export type Upload = Awaited<ReturnType<typeof readUpload>>;
|
|
56
|
+
|
|
57
|
+
/** A release in the releases directory. */
|
|
58
|
+
export interface StoredRelease {
|
|
59
|
+
id: string;
|
|
60
|
+
directory: string;
|
|
61
|
+
manifest: ReleaseManifest;
|
|
62
|
+
}
|
|
37
63
|
|
|
38
64
|
/**
|
|
39
65
|
* Stores a release under `releases/<id>`, where the id hashes its contents, so the same
|
|
40
66
|
* upload twice is one release. Each release is complete before it appears.
|
|
41
67
|
*/
|
|
42
68
|
export const saveRelease = async (
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
) => {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
69
|
+
releases: string,
|
|
70
|
+
{ manifest, files }: Upload,
|
|
71
|
+
): Promise<StoredRelease> => {
|
|
72
|
+
const hash = createHash("sha256").update(JSON.stringify(manifest));
|
|
73
|
+
for (const file of [...files].sort((a, b) => a.path.localeCompare(b.path))) {
|
|
74
|
+
hash
|
|
75
|
+
.update(file.path)
|
|
76
|
+
.update(createHash("sha256").update(file.content).digest("hex"));
|
|
77
|
+
}
|
|
78
|
+
const id = hash.digest("hex").slice(0, 16);
|
|
79
|
+
const directory = join(releases, id);
|
|
80
|
+
if (!existsSync(directory)) {
|
|
81
|
+
await mkdir(releases, { recursive: true });
|
|
82
|
+
const staging = await mkdtemp(join(releases, ".upload-"));
|
|
83
|
+
try {
|
|
84
|
+
for (const file of files) {
|
|
85
|
+
await mkdir(dirname(join(staging, file.path)), { recursive: true });
|
|
86
|
+
// Uploads lose their mode; the server must stay executable outside Windows.
|
|
87
|
+
await writeFile(join(staging, file.path), file.content, {
|
|
88
|
+
mode: file.path === manifest.server ? 0o755 : 0o644,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
await writeFile(
|
|
92
|
+
join(staging, "manifest.json"),
|
|
93
|
+
JSON.stringify(manifest, null, 2),
|
|
94
|
+
);
|
|
95
|
+
await rename(staging, directory);
|
|
96
|
+
} catch (cause) {
|
|
97
|
+
await rm(staging, { recursive: true, force: true });
|
|
98
|
+
throw cause;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return { id, directory, manifest };
|
|
70
102
|
};
|
|
71
103
|
|
|
72
104
|
export const loadRelease = async (
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
): Promise<
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
105
|
+
releases: string,
|
|
106
|
+
id: string,
|
|
107
|
+
): Promise<StoredRelease> => {
|
|
108
|
+
const directory = join(releases, id);
|
|
109
|
+
const manifest = releaseManifestSchema.parse(
|
|
110
|
+
JSON.parse(await readFile(join(directory, "manifest.json"), "utf8")),
|
|
111
|
+
);
|
|
112
|
+
return { id, directory, manifest };
|
|
81
113
|
};
|
package/src/secrets.ts
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
2
|
+
import { dirname, join, relative } from "node:path";
|
|
3
|
+
import { secretPlaceholder, secretValue } from "@tsplc/config";
|
|
4
|
+
import type { ResolvedConfig } from "@tsplc/config/build";
|
|
5
|
+
import { escapeString } from "@tsplc/iec/st";
|
|
6
|
+
import { AgentError } from "./errors";
|
|
7
|
+
import type { Credentials, ReleaseManifest } from "./protocol";
|
|
8
|
+
|
|
9
|
+
/** The agent's own Bun.secrets service, which holds the controller user. */
|
|
10
|
+
export const agentService = "tsplc-agent";
|
|
11
|
+
export const controllerSecretNames = {
|
|
12
|
+
username: "controller-username",
|
|
13
|
+
password: "controller-password",
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/** The controller user stored with `tsplc-agent secrets set-controller`, or null. */
|
|
17
|
+
export const storedCredentials = async (): Promise<Credentials | null> => {
|
|
18
|
+
const [username, password] = await Promise.all([
|
|
19
|
+
Bun.secrets.get({
|
|
20
|
+
service: agentService,
|
|
21
|
+
name: controllerSecretNames.username,
|
|
22
|
+
}),
|
|
23
|
+
Bun.secrets.get({
|
|
24
|
+
service: agentService,
|
|
25
|
+
name: controllerSecretNames.password,
|
|
26
|
+
}),
|
|
27
|
+
]);
|
|
28
|
+
return username && password ? { username, password } : null;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/** The controller user a request sent, or the stored one when it sent null. */
|
|
32
|
+
export const controllerCredentials = async (sent: Credentials | null) => {
|
|
33
|
+
const credentials = sent ?? (await storedCredentials());
|
|
34
|
+
if (!credentials) {
|
|
35
|
+
throw new AgentError(
|
|
36
|
+
"No controller user is stored on the plant PC: run tsplc-agent secrets set-controller there",
|
|
37
|
+
409,
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
return credentials;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/** The release's secrets from Bun.secrets, by name. Fails naming every one that is missing. */
|
|
44
|
+
export const readSecrets = async (secrets: ReleaseManifest["secrets"]) => {
|
|
45
|
+
const values = new Map<string, string>();
|
|
46
|
+
if (!secrets) {
|
|
47
|
+
return values;
|
|
48
|
+
}
|
|
49
|
+
const { service } = secrets;
|
|
50
|
+
const missing: string[] = [];
|
|
51
|
+
for (const name of secrets.names) {
|
|
52
|
+
const value = await Bun.secrets.get({ service, name });
|
|
53
|
+
const valid = secretValue.safeParse(value);
|
|
54
|
+
if (!value) {
|
|
55
|
+
missing.push(name);
|
|
56
|
+
} else if (!valid.success) {
|
|
57
|
+
throw new AgentError(
|
|
58
|
+
`Secret ${name} in Bun.secrets service ${service} ${valid.error.issues.map((issue) => issue.message).join(" and ")}`,
|
|
59
|
+
409,
|
|
60
|
+
);
|
|
61
|
+
} else {
|
|
62
|
+
values.set(name, value);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (missing.length) {
|
|
66
|
+
throw new AgentError(
|
|
67
|
+
`The plant PC has no ${missing.join(", ")} in Bun.secrets service ${service}: run tsplc-agent secrets set <name> --service ${service} there`,
|
|
68
|
+
409,
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
return values;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/** Variable `name` for the agent's own use: from Bun.secrets when the release names it as a secret, otherwise the agent's environment. */
|
|
75
|
+
export const releaseVariable = async (
|
|
76
|
+
manifest: ReleaseManifest,
|
|
77
|
+
name: string,
|
|
78
|
+
) =>
|
|
79
|
+
manifest.secrets?.names.includes(name)
|
|
80
|
+
? Bun.secrets.get({ service: manifest.secrets.service, name })
|
|
81
|
+
: process.env[name];
|
|
82
|
+
|
|
83
|
+
/** Structured Text with each secret's placeholder replaced by its value, inside the STRING literal that holds it. */
|
|
84
|
+
const fillSecrets = (st: string, secrets: ReadonlyMap<string, string>) =>
|
|
85
|
+
[...secrets].reduce(
|
|
86
|
+
// A replacer function, since a replacement string would read `$'` and `$$` as patterns.
|
|
87
|
+
(text, [name, value]) =>
|
|
88
|
+
text.replaceAll(secretPlaceholder(name), () => escapeString(value, "'")),
|
|
89
|
+
st,
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
/** The config with its ST copied to `directory`, the secrets filled in. */
|
|
93
|
+
export const withSecrets = async (
|
|
94
|
+
config: ResolvedConfig,
|
|
95
|
+
secrets: ReadonlyMap<string, string>,
|
|
96
|
+
directory: string,
|
|
97
|
+
): Promise<ResolvedConfig> => {
|
|
98
|
+
await rm(directory, { recursive: true, force: true });
|
|
99
|
+
for await (const path of new Bun.Glob("**/*").scan({
|
|
100
|
+
cwd: config.sourceDir,
|
|
101
|
+
dot: true,
|
|
102
|
+
})) {
|
|
103
|
+
const file = join(directory, path);
|
|
104
|
+
await mkdir(dirname(file), { recursive: true });
|
|
105
|
+
await writeFile(
|
|
106
|
+
file,
|
|
107
|
+
fillSecrets(
|
|
108
|
+
await readFile(join(config.sourceDir, path), "utf8"),
|
|
109
|
+
secrets,
|
|
110
|
+
),
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
...config,
|
|
115
|
+
sourceDir: directory,
|
|
116
|
+
entry: join(directory, relative(config.sourceDir, config.entry)),
|
|
117
|
+
};
|
|
118
|
+
};
|
package/src/server-process.ts
CHANGED
|
@@ -12,87 +12,108 @@ const restartDelayMs = 2000;
|
|
|
12
12
|
* another release's. The server reads its own environment and secrets, as it would anywhere.
|
|
13
13
|
*/
|
|
14
14
|
export const createServerSupervisor = (options: {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
/** The server's `/health` route, which answers once it is listening. */
|
|
16
|
+
healthUrl?: string;
|
|
17
|
+
log: (message: string) => void;
|
|
18
18
|
}) => {
|
|
19
|
-
|
|
19
|
+
let current: { executable: string; process: Subprocess } | undefined;
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
21
|
+
const spawn = (executable: string): Subprocess => {
|
|
22
|
+
// A bundled server runs on the agent's own Bun; releases before bundles shipped an executable.
|
|
23
|
+
const command = executable.endsWith(".js")
|
|
24
|
+
? [process.execPath, executable]
|
|
25
|
+
: [executable];
|
|
26
|
+
const child = Bun.spawn(command, {
|
|
27
|
+
cwd: dirname(executable),
|
|
28
|
+
stdio: ["ignore", "inherit", "inherit"],
|
|
29
|
+
onExit: (_, code) => {
|
|
30
|
+
// `stop` and `replace` clear or change `current` before killing a server.
|
|
31
|
+
if (current?.process !== child) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
options.log(
|
|
35
|
+
`Server exited (${code}); restarting it in ${restartDelayMs / 1000} s`,
|
|
36
|
+
);
|
|
37
|
+
void delay(restartDelayMs).then(() => {
|
|
38
|
+
if (current?.process === child) {
|
|
39
|
+
current = { executable, process: spawn(executable) };
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
return child;
|
|
45
|
+
};
|
|
38
46
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
47
|
+
const start = (executable: string) => {
|
|
48
|
+
current = { executable, process: spawn(executable) };
|
|
49
|
+
return current.process;
|
|
50
|
+
};
|
|
43
51
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
52
|
+
const stop = async () => {
|
|
53
|
+
const running = current;
|
|
54
|
+
current = undefined;
|
|
55
|
+
if (!running) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
running.process.kill();
|
|
59
|
+
await running.process.exited;
|
|
60
|
+
};
|
|
51
61
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
62
|
+
const healthy = async (child: Subprocess) => {
|
|
63
|
+
if (!options.healthUrl) {
|
|
64
|
+
await delay(settleMs);
|
|
65
|
+
return child.exitCode === null;
|
|
66
|
+
}
|
|
67
|
+
const deadline = Date.now() + healthyWithinMs;
|
|
68
|
+
while (Date.now() < deadline && child.exitCode === null) {
|
|
69
|
+
if (
|
|
70
|
+
await fetch(options.healthUrl).then(
|
|
71
|
+
(response) => response.ok,
|
|
72
|
+
() => false,
|
|
73
|
+
)
|
|
74
|
+
) {
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
await delay(1000);
|
|
78
|
+
}
|
|
79
|
+
return false;
|
|
80
|
+
};
|
|
70
81
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
82
|
+
return {
|
|
83
|
+
start,
|
|
84
|
+
stop,
|
|
85
|
+
/** Replaces the running server; if the new one doesn't come up, the previous one runs again. */
|
|
86
|
+
replace: async (executable: string) => {
|
|
87
|
+
const previous = current?.executable;
|
|
88
|
+
await stop();
|
|
89
|
+
let failure: unknown;
|
|
90
|
+
const started = await (async () => healthy(start(executable)))().catch(
|
|
91
|
+
(error: unknown) => {
|
|
92
|
+
failure = error;
|
|
93
|
+
return false;
|
|
94
|
+
},
|
|
95
|
+
);
|
|
96
|
+
if (started) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
await stop();
|
|
100
|
+
if (previous) {
|
|
101
|
+
start(previous);
|
|
102
|
+
}
|
|
103
|
+
const reason =
|
|
104
|
+
failure instanceof Error
|
|
105
|
+
? failure.message
|
|
106
|
+
: `it did not start${options.healthUrl ? ` answering ${options.healthUrl}` : ""}`;
|
|
107
|
+
throw new Error(
|
|
108
|
+
`The new server failed: ${reason}. ${previous ? "The previous one is running again." : ""}`.trim(),
|
|
109
|
+
{ cause: failure },
|
|
110
|
+
);
|
|
111
|
+
},
|
|
112
|
+
status: () =>
|
|
113
|
+
current && {
|
|
114
|
+
executable: current.executable,
|
|
115
|
+
running: current.process.exitCode === null,
|
|
116
|
+
},
|
|
117
|
+
};
|
|
98
118
|
};
|
|
119
|
+
export type ServerSupervisor = ReturnType<typeof createServerSupervisor>;
|
package/src/state.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import type { z } from "zod";
|
|
5
|
+
import { AgentError } from "./errors";
|
|
6
|
+
import {
|
|
7
|
+
type Release,
|
|
8
|
+
releaseSchema,
|
|
9
|
+
type Target,
|
|
10
|
+
targetSchema,
|
|
11
|
+
} from "./protocol";
|
|
12
|
+
import { loadRelease } from "./releases";
|
|
13
|
+
|
|
14
|
+
const readJsonFile = async <T>(path: string, schema: z.ZodType<T>) =>
|
|
15
|
+
existsSync(path)
|
|
16
|
+
? schema.parse(JSON.parse(await readFile(path, "utf8")))
|
|
17
|
+
: null;
|
|
18
|
+
|
|
19
|
+
const writeJsonFile = (path: string, value: unknown) =>
|
|
20
|
+
writeFile(path, JSON.stringify(value, null, 2));
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The agent's state directory: releases, the controller's download baseline, the selected
|
|
24
|
+
* target, the deployed release and the logs.
|
|
25
|
+
*/
|
|
26
|
+
export const openState = async (directory: string) => {
|
|
27
|
+
const paths = {
|
|
28
|
+
releases: join(directory, "releases"),
|
|
29
|
+
controller: join(directory, "controller"),
|
|
30
|
+
/** The deploying release's ST with its secrets filled in. */
|
|
31
|
+
sources: join(directory, "sources"),
|
|
32
|
+
release: join(directory, "release.json"),
|
|
33
|
+
target: join(directory, "target.json"),
|
|
34
|
+
deployments: join(directory, "deployments.jsonl"),
|
|
35
|
+
audit: join(directory, "audit.jsonl"),
|
|
36
|
+
debug: join(directory, "debug"),
|
|
37
|
+
};
|
|
38
|
+
await mkdir(directory, { recursive: true });
|
|
39
|
+
|
|
40
|
+
const target = () => readJsonFile(paths.target, targetSchema);
|
|
41
|
+
const release = () => readJsonFile(paths.release, releaseSchema);
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
paths,
|
|
45
|
+
/** The selected controller, or null before `tsplc target`. */
|
|
46
|
+
target,
|
|
47
|
+
setTarget: (selected: Target) => writeJsonFile(paths.target, selected),
|
|
48
|
+
requireTarget: async () => {
|
|
49
|
+
const selected = await target();
|
|
50
|
+
if (!selected) {
|
|
51
|
+
throw new AgentError("Select the controller first: tsplc target", 409);
|
|
52
|
+
}
|
|
53
|
+
return selected;
|
|
54
|
+
},
|
|
55
|
+
/** The deployed release, or null before the first deployment. */
|
|
56
|
+
release,
|
|
57
|
+
setRelease: (deployed: Release) => writeJsonFile(paths.release, deployed),
|
|
58
|
+
/** The deployed release's files and manifest. */
|
|
59
|
+
requireRelease: async () => {
|
|
60
|
+
const deployed = await release();
|
|
61
|
+
if (!deployed) {
|
|
62
|
+
throw new AgentError(
|
|
63
|
+
"Nothing has been deployed yet: tsplc deploy",
|
|
64
|
+
409,
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
return loadRelease(paths.releases, deployed.id);
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
export type AgentState = Awaited<ReturnType<typeof openState>>;
|
package/src/terminal.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { createInterface } from "node:readline/promises";
|
|
2
|
+
import type { Credentials } from "./protocol";
|
|
3
|
+
|
|
4
|
+
export const ask = async (question: string) => {
|
|
5
|
+
const lines = createInterface({
|
|
6
|
+
input: process.stdin,
|
|
7
|
+
output: process.stdout,
|
|
8
|
+
});
|
|
9
|
+
try {
|
|
10
|
+
return (await lines.question(question)).trim();
|
|
11
|
+
} finally {
|
|
12
|
+
lines.close();
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/** Reads a line without echoing it. */
|
|
17
|
+
export const askSecret = (question: string) =>
|
|
18
|
+
new Promise<string>((accept, reject) => {
|
|
19
|
+
if (!process.stdin.isTTY) {
|
|
20
|
+
reject(new Error("Typing a secret needs an interactive terminal."));
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
// Echo goes off before the prompt appears, so nothing typed at it can show.
|
|
24
|
+
process.stdin.setRawMode(true);
|
|
25
|
+
process.stdin.setEncoding("utf8");
|
|
26
|
+
process.stdin.resume();
|
|
27
|
+
process.stdout.write(question);
|
|
28
|
+
let secret = "";
|
|
29
|
+
const finish = () => {
|
|
30
|
+
process.stdin.off("data", read);
|
|
31
|
+
process.stdin.setRawMode(false);
|
|
32
|
+
process.stdin.pause();
|
|
33
|
+
process.stdout.write("\n");
|
|
34
|
+
};
|
|
35
|
+
const read = (text: string) => {
|
|
36
|
+
for (const character of text) {
|
|
37
|
+
if (character === "\r" || character === "\n") {
|
|
38
|
+
finish();
|
|
39
|
+
accept(secret);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (character === "\u0003") {
|
|
43
|
+
finish();
|
|
44
|
+
reject(new Error("Cancelled."));
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
secret =
|
|
48
|
+
character === "\u007f" ? secret.slice(0, -1) : secret + character;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
process.stdin.on("data", read);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
export const askCredentials = async (): Promise<Credentials> => ({
|
|
55
|
+
username: await ask("Controller username: "),
|
|
56
|
+
password: await askSecret("Controller password: "),
|
|
57
|
+
});
|