soltask 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +398 -2
- package/dist/cli.d.ts +15 -0
- package/dist/cli.js +344 -0
- package/dist/client.d.ts +131 -0
- package/dist/client.js +879 -0
- package/dist/commands/actions.d.ts +26 -0
- package/dist/commands/actions.js +1181 -0
- package/dist/commands/auth.d.ts +10 -0
- package/dist/commands/auth.js +489 -0
- package/dist/commands/drafts.d.ts +22 -0
- package/dist/commands/drafts.js +736 -0
- package/dist/commands/index.d.ts +3 -0
- package/dist/commands/index.js +10 -0
- package/dist/commands/meta.d.ts +4 -0
- package/dist/commands/meta.js +195 -0
- package/dist/commands/read.d.ts +24 -0
- package/dist/commands/read.js +1106 -0
- package/dist/commands/watch.d.ts +2 -0
- package/dist/commands/watch.js +397 -0
- package/dist/config.d.ts +86 -0
- package/dist/config.js +365 -0
- package/dist/context.d.ts +70 -0
- package/dist/context.js +1 -0
- package/dist/domain.d.ts +311 -0
- package/dist/domain.js +747 -0
- package/dist/errors.d.ts +28 -0
- package/dist/errors.js +40 -0
- package/dist/fsutil.d.ts +35 -0
- package/dist/fsutil.js +243 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +18 -0
- package/dist/output.d.ts +27 -0
- package/dist/output.js +74 -0
- package/dist/types.d.ts +254 -0
- package/dist/types.js +1 -0
- package/dist/util.d.ts +32 -0
- package/dist/util.js +116 -0
- package/dist/version.d.ts +3 -0
- package/dist/version.js +13 -0
- package/package.json +63 -4
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { CommandContext, CommandSpec } from "../context.js";
|
|
2
|
+
/** `login` / `auth login`: sends `nsec` once to `POST /api/session` and stores only the issued session cookie + unlock key. */
|
|
3
|
+
export declare function cmdLogin(ctx: CommandContext): Promise<number>;
|
|
4
|
+
/** `logout` / `auth logout`: signs out on the server (`DELETE /api/session`) and clears saved session fields. */
|
|
5
|
+
export declare function cmdLogout(ctx: CommandContext): Promise<number>;
|
|
6
|
+
/** `whoami` / `auth whoami`: displays the signed-in profile, pubkey, and communities from `GET /api/me`. */
|
|
7
|
+
export declare function cmdWhoami(ctx: CommandContext): Promise<number>;
|
|
8
|
+
/** `doctor` / `auth doctor`: checks Node version, config permissions, server URL, session, and community sync status. */
|
|
9
|
+
export declare function cmdDoctor(ctx: CommandContext): Promise<number>;
|
|
10
|
+
export declare const authCommands: readonly CommandSpec[];
|
|
@@ -0,0 +1,489 @@
|
|
|
1
|
+
import { ApiClient } from "../client.js";
|
|
2
|
+
import { buildMissingAuthHint } from "../cli.js";
|
|
3
|
+
import { ENV_NSEC, ENV_SERVER, SESSION_CONFIG_KEYS, configPermWarning, hasEnvCredentials, inspectConfigFile, isNonLoopbackHttp, normalizeServerUrl, resolveServer, resolveSession, sameServerOrigin, saveConfig, withoutSessionFields, } from "../config.js";
|
|
4
|
+
import { CliError, EXIT_AUTH, EXIT_OK, EXIT_RUNTIME, EXIT_VALIDATION } from "../errors.js";
|
|
5
|
+
import { readUtf8File } from "../fsutil.js";
|
|
6
|
+
import { displayText, emitJson, errOut, formatTable, out } from "../output.js";
|
|
7
|
+
import { errorMessage, nonEmptyString, utcNowIso } from "../util.js";
|
|
8
|
+
import { PROGRAM, PROTOCOL_VERSION } from "../version.js";
|
|
9
|
+
const MIN_NODE_MAJOR = 22;
|
|
10
|
+
const MAX_SECRET_BYTES = 4096;
|
|
11
|
+
async function readSecretFromStdin(stream, maxBytes = MAX_SECRET_BYTES) {
|
|
12
|
+
const chunks = [];
|
|
13
|
+
let total = 0;
|
|
14
|
+
for await (const chunk of stream) {
|
|
15
|
+
const buf = typeof chunk === "string" ? Buffer.from(chunk, "utf8") : Buffer.from(chunk);
|
|
16
|
+
total += buf.byteLength;
|
|
17
|
+
if (total > maxBytes) {
|
|
18
|
+
throw new CliError(`Stdin secret exceeds the ${maxBytes}-byte limit.`, { exitCode: EXIT_VALIDATION });
|
|
19
|
+
}
|
|
20
|
+
chunks.push(buf);
|
|
21
|
+
}
|
|
22
|
+
return Buffer.concat(chunks).toString("utf8").trim();
|
|
23
|
+
}
|
|
24
|
+
function selectNsecSource(ctx) {
|
|
25
|
+
const fromStdin = ctx.flag("nsec-stdin");
|
|
26
|
+
const filePath = ctx.opt("nsec-file");
|
|
27
|
+
const flagValue = ctx.opt("nsec");
|
|
28
|
+
const explicitCount = (fromStdin ? 1 : 0) + (filePath !== undefined ? 1 : 0) + (flagValue !== undefined ? 1 : 0);
|
|
29
|
+
if (explicitCount > 1) {
|
|
30
|
+
throw new CliError("Pass only one of --nsec-stdin, --nsec-file, or --nsec.", { exitCode: EXIT_VALIDATION });
|
|
31
|
+
}
|
|
32
|
+
if (fromStdin) {
|
|
33
|
+
return { source: "nsec-stdin" };
|
|
34
|
+
}
|
|
35
|
+
if (filePath !== undefined) {
|
|
36
|
+
if (filePath.trim() === "") {
|
|
37
|
+
throw new CliError("--nsec-file requires a file path.", { exitCode: EXIT_VALIDATION });
|
|
38
|
+
}
|
|
39
|
+
return { source: "nsec-file", filePath };
|
|
40
|
+
}
|
|
41
|
+
if (flagValue !== undefined) {
|
|
42
|
+
return { source: "nsec-flag", flagValue };
|
|
43
|
+
}
|
|
44
|
+
const envValue = nonEmptyString(ctx.io.env[ENV_NSEC]);
|
|
45
|
+
if (envValue !== undefined) {
|
|
46
|
+
return { source: "env", envValue };
|
|
47
|
+
}
|
|
48
|
+
throw new CliError(`Provide a secret key via --nsec-stdin, --nsec-file PATH, --nsec VALUE, or ${ENV_NSEC}.`, { exitCode: EXIT_VALIDATION });
|
|
49
|
+
}
|
|
50
|
+
function validateMeResponse(me) {
|
|
51
|
+
if (typeof me.pubkey !== "string" || me.pubkey.trim() === "" || !Array.isArray(me.communities)) {
|
|
52
|
+
throw new CliError('Unexpected /api/me response: "pubkey" or "communities" is missing.', { exitCode: EXIT_RUNTIME });
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/** `login` / `auth login`: sends `nsec` once to `POST /api/session` and stores only the issued session cookie + unlock key. */
|
|
56
|
+
export async function cmdLogin(ctx) {
|
|
57
|
+
const { global: g, io } = ctx;
|
|
58
|
+
const cfg = ctx.config();
|
|
59
|
+
const serverUrl = resolveServer(g.server, cfg, io.env);
|
|
60
|
+
if (isNonLoopbackHttp(serverUrl)) {
|
|
61
|
+
errOut(`Warning: logging in over unencrypted HTTP (${serverUrl}); secret keys and session credentials travel in cleartext. Prefer HTTPS.`);
|
|
62
|
+
}
|
|
63
|
+
const chosen = selectNsecSource(ctx);
|
|
64
|
+
if (g.dryRun) {
|
|
65
|
+
if (chosen.source === "nsec-file" && chosen.filePath !== undefined) {
|
|
66
|
+
const content = readUtf8File(chosen.filePath, MAX_SECRET_BYTES).trim();
|
|
67
|
+
if (content === "") {
|
|
68
|
+
throw new CliError(`Secret key file ${chosen.filePath} is empty.`, { exitCode: EXIT_VALIDATION });
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
else if (chosen.source === "nsec-flag" && (chosen.flagValue ?? "").trim() === "") {
|
|
72
|
+
throw new CliError("--nsec cannot be empty.", { exitCode: EXIT_VALIDATION });
|
|
73
|
+
}
|
|
74
|
+
const plan = {
|
|
75
|
+
ok: true,
|
|
76
|
+
dry_run: true,
|
|
77
|
+
action: "login",
|
|
78
|
+
server: serverUrl,
|
|
79
|
+
nsec_source: chosen.source,
|
|
80
|
+
config_path: ctx.configPath,
|
|
81
|
+
};
|
|
82
|
+
if (g.json) {
|
|
83
|
+
emitJson(plan);
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
out(`Dry run: would log in to ${displayText(serverUrl)} using ${chosen.source}.`);
|
|
87
|
+
out(`Dry run: would save the session in ${ctx.configPath}; nothing was sent.`);
|
|
88
|
+
}
|
|
89
|
+
return EXIT_OK;
|
|
90
|
+
}
|
|
91
|
+
let nsec;
|
|
92
|
+
switch (chosen.source) {
|
|
93
|
+
case "nsec-stdin":
|
|
94
|
+
nsec = await readSecretFromStdin(io.stdin);
|
|
95
|
+
break;
|
|
96
|
+
case "nsec-file":
|
|
97
|
+
nsec = readUtf8File(chosen.filePath ?? "", MAX_SECRET_BYTES).trim();
|
|
98
|
+
break;
|
|
99
|
+
case "nsec-flag":
|
|
100
|
+
nsec = (chosen.flagValue ?? "").trim();
|
|
101
|
+
break;
|
|
102
|
+
case "env":
|
|
103
|
+
nsec = chosen.envValue ?? "";
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
if (nsec === "") {
|
|
107
|
+
throw new CliError("Secret key (nsec) cannot be empty.", { exitCode: EXIT_VALIDATION });
|
|
108
|
+
}
|
|
109
|
+
const anonymous = new ApiClient(serverUrl, null, io.clientOptions);
|
|
110
|
+
const { data, cookie } = await anonymous.signIn(nsec);
|
|
111
|
+
if (cookie === null || cookie.cleared || cookie.name === "" || cookie.value === "") {
|
|
112
|
+
throw new CliError("Unexpected login response: session Set-Cookie header is missing.", { exitCode: EXIT_RUNTIME });
|
|
113
|
+
}
|
|
114
|
+
const unlock = nonEmptyString(data.unlock);
|
|
115
|
+
const pubkey = nonEmptyString(data.pubkey);
|
|
116
|
+
if (unlock === undefined) {
|
|
117
|
+
throw new CliError('Unexpected login response: "unlock" is missing.', { exitCode: EXIT_RUNTIME });
|
|
118
|
+
}
|
|
119
|
+
if (pubkey === undefined) {
|
|
120
|
+
throw new CliError('Unexpected login response: "pubkey" is missing.', { exitCode: EXIT_RUNTIME });
|
|
121
|
+
}
|
|
122
|
+
const saved = {
|
|
123
|
+
...cfg,
|
|
124
|
+
server: serverUrl,
|
|
125
|
+
cookie_name: cookie.name,
|
|
126
|
+
cookie_value: cookie.value,
|
|
127
|
+
unlock,
|
|
128
|
+
pubkey,
|
|
129
|
+
profile: data.profile,
|
|
130
|
+
communities: data.communities,
|
|
131
|
+
saved_at: utcNowIso(),
|
|
132
|
+
};
|
|
133
|
+
delete saved["token"];
|
|
134
|
+
delete saved["nsec"];
|
|
135
|
+
try {
|
|
136
|
+
saveConfig(ctx.configPath, saved);
|
|
137
|
+
}
|
|
138
|
+
catch (err) {
|
|
139
|
+
const rollbackSession = {
|
|
140
|
+
mode: "cookie",
|
|
141
|
+
source: "config",
|
|
142
|
+
cookieName: cookie.name,
|
|
143
|
+
cookieValue: cookie.value,
|
|
144
|
+
unlock,
|
|
145
|
+
};
|
|
146
|
+
await new ApiClient(serverUrl, rollbackSession, io.clientOptions).signOut().catch(() => undefined);
|
|
147
|
+
throw err;
|
|
148
|
+
}
|
|
149
|
+
const summary = {
|
|
150
|
+
ok: true,
|
|
151
|
+
server: serverUrl,
|
|
152
|
+
pubkey,
|
|
153
|
+
profile: data.profile,
|
|
154
|
+
communities: data.communities,
|
|
155
|
+
config_path: ctx.configPath,
|
|
156
|
+
};
|
|
157
|
+
if (g.json) {
|
|
158
|
+
emitJson(summary);
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
const displayName = nonEmptyString(data.profile.name) ?? pubkey.slice(0, 12);
|
|
162
|
+
out(`Logged in as ${displayText(displayName)} (${pubkey.slice(0, 12)}…) on ${serverUrl}`);
|
|
163
|
+
if (data.communities.length > 0) {
|
|
164
|
+
const list = data.communities.map((c) => `${displayText(c.id)} (${c.role})`).join(", ");
|
|
165
|
+
out(`Communities: ${list}`);
|
|
166
|
+
}
|
|
167
|
+
out(`Saved config: ${ctx.configPath}`);
|
|
168
|
+
const warning = configPermWarning(ctx.configPath, io.platform);
|
|
169
|
+
if (warning !== null) {
|
|
170
|
+
errOut(`Note: ${warning}`);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return EXIT_OK;
|
|
174
|
+
}
|
|
175
|
+
/** `logout` / `auth logout`: signs out on the server (`DELETE /api/session`) and clears saved session fields. */
|
|
176
|
+
export async function cmdLogout(ctx) {
|
|
177
|
+
const { global: g, io } = ctx;
|
|
178
|
+
const localOnly = ctx.flag("local");
|
|
179
|
+
const cfg = ctx.config();
|
|
180
|
+
const rawServer = nonEmptyString(g.server) ?? nonEmptyString(io.env[ENV_SERVER]) ?? nonEmptyString(cfg["server"]);
|
|
181
|
+
const serverUrl = rawServer === undefined ? null : normalizeServerUrl(rawServer);
|
|
182
|
+
const envCreds = hasEnvCredentials(io.env);
|
|
183
|
+
const session = serverUrl === null ? null : resolveSession(cfg, serverUrl, io.env);
|
|
184
|
+
const savedServer = nonEmptyString(cfg["server"]);
|
|
185
|
+
const clearsSavedLogin = !envCreds && (serverUrl === null || (savedServer !== undefined && sameServerOrigin(serverUrl, savedServer)));
|
|
186
|
+
const hasSavedSessionFields = SESSION_CONFIG_KEYS.some((key) => key in cfg);
|
|
187
|
+
if (g.dryRun) {
|
|
188
|
+
const info = {
|
|
189
|
+
ok: true,
|
|
190
|
+
dry_run: true,
|
|
191
|
+
server: serverUrl,
|
|
192
|
+
would_revoke_server: !localOnly && session !== null && serverUrl !== null,
|
|
193
|
+
would_clear_config: clearsSavedLogin && hasSavedSessionFields,
|
|
194
|
+
};
|
|
195
|
+
if (g.json) {
|
|
196
|
+
emitJson(info);
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
out(`Dry run: would logout on ${serverUrl ?? "local config"} ` +
|
|
200
|
+
`(revoke_server=${String(info.would_revoke_server)}, clear_config=${String(info.would_clear_config)}).`);
|
|
201
|
+
}
|
|
202
|
+
return EXIT_OK;
|
|
203
|
+
}
|
|
204
|
+
let serverRevoked = false;
|
|
205
|
+
let note = "";
|
|
206
|
+
if (localOnly) {
|
|
207
|
+
note =
|
|
208
|
+
session !== null || (clearsSavedLogin && hasSavedSessionFields)
|
|
209
|
+
? "Skipped server sign-out (--local); the session may remain active on the server until expiry."
|
|
210
|
+
: "No active session configured.";
|
|
211
|
+
}
|
|
212
|
+
else if (session !== null && serverUrl !== null) {
|
|
213
|
+
const client = new ApiClient(serverUrl, session, io.clientOptions);
|
|
214
|
+
try {
|
|
215
|
+
const { revoked } = await client.signOut();
|
|
216
|
+
if (revoked) {
|
|
217
|
+
serverRevoked = true;
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
note = "Session was already expired or signed out on the server.";
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
catch (err) {
|
|
224
|
+
if (err instanceof CliError && clearsSavedLogin && hasSavedSessionFields) {
|
|
225
|
+
throw new CliError(`${err.message} Pass --local to clear the saved login locally without contacting the server.`, {
|
|
226
|
+
exitCode: err.exitCode,
|
|
227
|
+
payload: err.payload,
|
|
228
|
+
retryable: err.retryable,
|
|
229
|
+
retryAfter: err.retryAfter,
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
throw err;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
else {
|
|
236
|
+
note = "No active session configured.";
|
|
237
|
+
}
|
|
238
|
+
let configCleared = false;
|
|
239
|
+
if (clearsSavedLogin) {
|
|
240
|
+
const { config, removed } = withoutSessionFields(cfg);
|
|
241
|
+
if (removed) {
|
|
242
|
+
saveConfig(ctx.configPath, config);
|
|
243
|
+
configCleared = true;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
const result = { ok: true, server_revoked: serverRevoked, config_cleared: configCleared };
|
|
247
|
+
if (note !== "") {
|
|
248
|
+
result["note"] = note;
|
|
249
|
+
}
|
|
250
|
+
if (g.json) {
|
|
251
|
+
emitJson(result);
|
|
252
|
+
}
|
|
253
|
+
else if (serverRevoked && configCleared) {
|
|
254
|
+
out("Logged out; server session ended and local config cleared.");
|
|
255
|
+
}
|
|
256
|
+
else if (serverRevoked) {
|
|
257
|
+
out("Logged out; server session ended (local config left intact).");
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
out(`Logged out locally. ${note}`.trim());
|
|
261
|
+
}
|
|
262
|
+
return EXIT_OK;
|
|
263
|
+
}
|
|
264
|
+
/** `whoami` / `auth whoami`: displays the signed-in profile, pubkey, and communities from `GET /api/me`. */
|
|
265
|
+
export async function cmdWhoami(ctx) {
|
|
266
|
+
const client = ctx.client();
|
|
267
|
+
const me = await client.me();
|
|
268
|
+
validateMeResponse(me);
|
|
269
|
+
if (ctx.global.json) {
|
|
270
|
+
emitJson({ server: client.serverUrl, ...me });
|
|
271
|
+
return EXIT_OK;
|
|
272
|
+
}
|
|
273
|
+
out(`Name: ${displayText(nonEmptyString(me.profile.name) ?? "(unnamed)")}`);
|
|
274
|
+
out(`Pubkey: ${displayText(me.pubkey)}`);
|
|
275
|
+
out(`Server: ${displayText(client.serverUrl)}`);
|
|
276
|
+
if (me.communities.length === 0) {
|
|
277
|
+
out("Communities: (none)");
|
|
278
|
+
}
|
|
279
|
+
else {
|
|
280
|
+
out("");
|
|
281
|
+
out(formatTable(["COMMUNITY", "ROLE", "RELAY"], me.communities.map((community) => [community.id, community.role, community.relay])));
|
|
282
|
+
}
|
|
283
|
+
return EXIT_OK;
|
|
284
|
+
}
|
|
285
|
+
function printDoctorChecks(checks) {
|
|
286
|
+
for (const check of checks) {
|
|
287
|
+
out(`[${check.ok ? "PASS" : "FAIL"}] ${check.name}: ${displayText(check.detail)}`);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
/** `doctor` / `auth doctor`: checks Node version, config permissions, server URL, session, and community sync status. */
|
|
291
|
+
export async function cmdDoctor(ctx) {
|
|
292
|
+
const { global: g, io } = ctx;
|
|
293
|
+
const checks = [];
|
|
294
|
+
let exitCode = EXIT_OK;
|
|
295
|
+
const major = Number(io.nodeVersion.split(".")[0]);
|
|
296
|
+
const nodeOk = Number.isFinite(major) && major >= MIN_NODE_MAJOR;
|
|
297
|
+
checks.push({
|
|
298
|
+
name: "node",
|
|
299
|
+
ok: nodeOk,
|
|
300
|
+
detail: nodeOk ? io.nodeVersion : `${io.nodeVersion} (${PROGRAM} needs Node ${MIN_NODE_MAJOR} or newer)`,
|
|
301
|
+
});
|
|
302
|
+
if (!nodeOk) {
|
|
303
|
+
exitCode = EXIT_RUNTIME;
|
|
304
|
+
}
|
|
305
|
+
const perms = inspectConfigFile(ctx.configPath, io.platform);
|
|
306
|
+
checks.push({ name: "config_perms", ok: perms.ok, detail: perms.detail });
|
|
307
|
+
if (!perms.ok) {
|
|
308
|
+
exitCode = EXIT_RUNTIME;
|
|
309
|
+
}
|
|
310
|
+
let cfg = {};
|
|
311
|
+
try {
|
|
312
|
+
cfg = ctx.config();
|
|
313
|
+
}
|
|
314
|
+
catch (err) {
|
|
315
|
+
checks.push({ name: "config", ok: false, detail: err instanceof CliError ? err.message : errorMessage(err) });
|
|
316
|
+
exitCode = EXIT_RUNTIME;
|
|
317
|
+
}
|
|
318
|
+
const rawServer = nonEmptyString(g.server) ?? nonEmptyString(io.env[ENV_SERVER]) ?? nonEmptyString(cfg["server"]);
|
|
319
|
+
if (rawServer === undefined) {
|
|
320
|
+
checks.push({ name: "server_url", ok: false, detail: "Not configured" });
|
|
321
|
+
if (g.json) {
|
|
322
|
+
emitJson({ ok: false, checks });
|
|
323
|
+
}
|
|
324
|
+
else {
|
|
325
|
+
printDoctorChecks(checks);
|
|
326
|
+
}
|
|
327
|
+
return EXIT_AUTH;
|
|
328
|
+
}
|
|
329
|
+
let serverUrl;
|
|
330
|
+
try {
|
|
331
|
+
serverUrl = normalizeServerUrl(rawServer);
|
|
332
|
+
}
|
|
333
|
+
catch (err) {
|
|
334
|
+
checks.push({ name: "server_url", ok: false, detail: err instanceof CliError ? err.message : errorMessage(err) });
|
|
335
|
+
if (g.json) {
|
|
336
|
+
emitJson({ ok: false, checks });
|
|
337
|
+
}
|
|
338
|
+
else {
|
|
339
|
+
printDoctorChecks(checks);
|
|
340
|
+
}
|
|
341
|
+
return err instanceof CliError ? err.exitCode : EXIT_RUNTIME;
|
|
342
|
+
}
|
|
343
|
+
const cleartext = isNonLoopbackHttp(serverUrl);
|
|
344
|
+
checks.push({
|
|
345
|
+
name: "server_url",
|
|
346
|
+
ok: true,
|
|
347
|
+
detail: cleartext ? `${serverUrl} (warning: cleartext HTTP)` : serverUrl,
|
|
348
|
+
});
|
|
349
|
+
const session = resolveSession(cfg, serverUrl, io.env);
|
|
350
|
+
if (cleartext && session === null) {
|
|
351
|
+
errOut(`Warning: server URL uses unencrypted HTTP (${serverUrl}); credentials travel in cleartext. Prefer HTTPS.`);
|
|
352
|
+
}
|
|
353
|
+
const clientOpts = {
|
|
354
|
+
...(io.sleepFn === undefined ? {} : { sleepFn: io.sleepFn }),
|
|
355
|
+
...io.clientOptions,
|
|
356
|
+
};
|
|
357
|
+
const client = new ApiClient(serverUrl, session, clientOpts);
|
|
358
|
+
let communitiesResult;
|
|
359
|
+
if (session === null) {
|
|
360
|
+
const hint = buildMissingAuthHint(cfg, serverUrl, io.env);
|
|
361
|
+
checks.push({ name: "auth", ok: false, detail: `No session configured${hint === undefined ? "" : ` ${hint}`}` });
|
|
362
|
+
exitCode = EXIT_AUTH;
|
|
363
|
+
}
|
|
364
|
+
else {
|
|
365
|
+
let authOk = false;
|
|
366
|
+
try {
|
|
367
|
+
const me = await client.me();
|
|
368
|
+
validateMeResponse(me);
|
|
369
|
+
const who = nonEmptyString(me.profile.name) ?? me.pubkey.slice(0, 12);
|
|
370
|
+
checks.push({ name: "auth", ok: true, detail: `${who} (${me.pubkey.slice(0, 12)}…, mode=${session.mode})` });
|
|
371
|
+
authOk = true;
|
|
372
|
+
}
|
|
373
|
+
catch (err) {
|
|
374
|
+
if (!(err instanceof CliError)) {
|
|
375
|
+
throw err;
|
|
376
|
+
}
|
|
377
|
+
checks.push({ name: "auth", ok: false, detail: err.message });
|
|
378
|
+
exitCode = err.exitCode === EXIT_AUTH ? EXIT_AUTH : Math.max(exitCode, err.exitCode);
|
|
379
|
+
}
|
|
380
|
+
if (authOk) {
|
|
381
|
+
try {
|
|
382
|
+
if (g.waitLive) {
|
|
383
|
+
const waited = await client.waitUntilLive();
|
|
384
|
+
communitiesResult = waited.communities;
|
|
385
|
+
}
|
|
386
|
+
else {
|
|
387
|
+
communitiesResult = await client.communities();
|
|
388
|
+
}
|
|
389
|
+
const summary = communitiesResult.length === 0
|
|
390
|
+
? "none"
|
|
391
|
+
: communitiesResult.map((c) => `${c.id}=${c.status} (lag ${c.lagSeconds}s)`).join(", ");
|
|
392
|
+
const allLive = communitiesResult.every((c) => c.status === "live");
|
|
393
|
+
checks.push({ name: "communities", ok: allLive, detail: summary });
|
|
394
|
+
if (!allLive) {
|
|
395
|
+
exitCode = Math.max(exitCode, EXIT_RUNTIME);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
catch (err) {
|
|
399
|
+
if (!(err instanceof CliError)) {
|
|
400
|
+
throw err;
|
|
401
|
+
}
|
|
402
|
+
checks.push({ name: "communities", ok: false, detail: err.message });
|
|
403
|
+
exitCode = Math.max(exitCode, err.exitCode);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
if (client.requiredProtocolVersion !== null) {
|
|
408
|
+
checks.push({
|
|
409
|
+
name: "protocol",
|
|
410
|
+
ok: false,
|
|
411
|
+
detail: `server requires CLI protocol v${client.requiredProtocolVersion} (this ${PROGRAM} implements v${PROTOCOL_VERSION}); upgrade ${PROGRAM}`,
|
|
412
|
+
});
|
|
413
|
+
exitCode = Math.max(exitCode, EXIT_RUNTIME);
|
|
414
|
+
}
|
|
415
|
+
const overallOk = checks.every((check) => check.ok) && exitCode === EXIT_OK;
|
|
416
|
+
if (g.json) {
|
|
417
|
+
emitJson({
|
|
418
|
+
ok: overallOk,
|
|
419
|
+
checks,
|
|
420
|
+
...(communitiesResult === undefined ? {} : { communities: communitiesResult }),
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
else {
|
|
424
|
+
printDoctorChecks(checks);
|
|
425
|
+
}
|
|
426
|
+
return exitCode;
|
|
427
|
+
}
|
|
428
|
+
const LOGIN_OPTIONS = {
|
|
429
|
+
"nsec-stdin": { type: "boolean", description: "Read the Nostr secret key (nsec) from stdin" },
|
|
430
|
+
"nsec-file": { type: "string", placeholder: "PATH", description: "Read the Nostr secret key (nsec) from a file" },
|
|
431
|
+
nsec: { type: "string", placeholder: "VALUE", description: "Nostr secret key (prefer --nsec-stdin or --nsec-file)" },
|
|
432
|
+
};
|
|
433
|
+
const LOGOUT_OPTIONS = {
|
|
434
|
+
local: { type: "boolean", description: "Skip server sign-out and clear only the saved local login" },
|
|
435
|
+
};
|
|
436
|
+
const loginSubcommand = {
|
|
437
|
+
name: "login",
|
|
438
|
+
summary: "Sign in with an nsec and store the issued session cookie and unlock key",
|
|
439
|
+
usage: "auth login [--nsec-stdin | --nsec-file PATH | --nsec VALUE]",
|
|
440
|
+
options: LOGIN_OPTIONS,
|
|
441
|
+
positionals: { min: 0, max: 0 },
|
|
442
|
+
handler: cmdLogin,
|
|
443
|
+
};
|
|
444
|
+
const logoutSubcommand = {
|
|
445
|
+
name: "logout",
|
|
446
|
+
summary: "End the current session on the server and clear the saved login",
|
|
447
|
+
usage: "auth logout [--local]",
|
|
448
|
+
options: LOGOUT_OPTIONS,
|
|
449
|
+
positionals: { min: 0, max: 0 },
|
|
450
|
+
handler: cmdLogout,
|
|
451
|
+
};
|
|
452
|
+
const whoamiSubcommand = {
|
|
453
|
+
name: "whoami",
|
|
454
|
+
summary: "Show the signed-in profile, pubkey, and communities",
|
|
455
|
+
usage: "auth whoami",
|
|
456
|
+
positionals: { min: 0, max: 0 },
|
|
457
|
+
handler: cmdWhoami,
|
|
458
|
+
};
|
|
459
|
+
const doctorSubcommand = {
|
|
460
|
+
name: "doctor",
|
|
461
|
+
summary: "Check Node, config permissions, server URL, sign-in, and community sync health",
|
|
462
|
+
usage: "auth doctor",
|
|
463
|
+
positionals: { min: 0, max: 0 },
|
|
464
|
+
handler: cmdDoctor,
|
|
465
|
+
};
|
|
466
|
+
export const authCommands = [
|
|
467
|
+
{
|
|
468
|
+
name: "auth",
|
|
469
|
+
summary: "Manage authentication and diagnostics (login, logout, whoami, doctor)",
|
|
470
|
+
usage: "auth <login|logout|whoami|doctor>",
|
|
471
|
+
subcommands: [loginSubcommand, logoutSubcommand, whoamiSubcommand, doctorSubcommand],
|
|
472
|
+
},
|
|
473
|
+
{
|
|
474
|
+
...loginSubcommand,
|
|
475
|
+
usage: "login [--nsec-stdin | --nsec-file PATH | --nsec VALUE]",
|
|
476
|
+
},
|
|
477
|
+
{
|
|
478
|
+
...logoutSubcommand,
|
|
479
|
+
usage: "logout [--local]",
|
|
480
|
+
},
|
|
481
|
+
{
|
|
482
|
+
...whoamiSubcommand,
|
|
483
|
+
usage: "whoami",
|
|
484
|
+
},
|
|
485
|
+
{
|
|
486
|
+
...doctorSubcommand,
|
|
487
|
+
usage: "doctor",
|
|
488
|
+
},
|
|
489
|
+
];
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { CommandContext, CommandSpec } from "../context.js";
|
|
2
|
+
export declare const MAX_DRAFT_BYTES = 65536;
|
|
3
|
+
export declare const MAX_TITLE_CHARS = 200;
|
|
4
|
+
export declare const MAX_BODY_CHARS = 60000;
|
|
5
|
+
export declare const DRAFT_ID_RE: RegExp;
|
|
6
|
+
export declare const MAX_LABELS = 10;
|
|
7
|
+
export declare const MAX_LABEL_LEN = 40;
|
|
8
|
+
/** `draft list`: lists backlog drafts with optional community and text filters. */
|
|
9
|
+
export declare function cmdDraftList(ctx: CommandContext): Promise<number>;
|
|
10
|
+
/** `draft show <DRAFT_ID>`: displays metadata and sanitized body for a single draft. */
|
|
11
|
+
export declare function cmdDraftShow(ctx: CommandContext): Promise<number>;
|
|
12
|
+
/** `draft create`: creates a new backlog draft in the target community. */
|
|
13
|
+
export declare function cmdDraftCreate(ctx: CommandContext): Promise<number>;
|
|
14
|
+
/** `draft update <DRAFT_ID>`: updates an existing backlog draft's title and/or body. */
|
|
15
|
+
export declare function cmdDraftUpdate(ctx: CommandContext): Promise<number>;
|
|
16
|
+
/** `draft delete <DRAFT_ID>`: deletes a backlog draft directly without prompting. */
|
|
17
|
+
export declare function cmdDraftDelete(ctx: CommandContext): Promise<number>;
|
|
18
|
+
/** `draft send <DRAFT_ID>`: preflights and sends a draft as a new task, applying optional priority and labels. */
|
|
19
|
+
export declare function cmdDraftSend(ctx: CommandContext): Promise<number>;
|
|
20
|
+
/** `draft retry <DRAFT_ID>`: retries sending a draft that is waiting for delivery confirmation. */
|
|
21
|
+
export declare function cmdDraftRetry(ctx: CommandContext): Promise<number>;
|
|
22
|
+
export declare const draftCommands: readonly CommandSpec[];
|