vit 0.4.0 → 0.4.3
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/package.json +2 -2
- package/skills/vit/SKILL.md +3 -0
- package/src/cmd/adopt.js +2 -1
- package/src/cmd/beacon.js +5 -2
- package/src/cmd/config.js +2 -1
- package/src/cmd/doctor.js +25 -12
- package/src/cmd/explore.js +43 -70
- package/src/cmd/firehose.js +3 -2
- package/src/cmd/follow.js +7 -9
- package/src/cmd/hack.js +5 -2
- package/src/cmd/inbox.js +4 -4
- package/src/cmd/init.js +10 -6
- package/src/cmd/learn.js +8 -4
- package/src/cmd/link.js +2 -1
- package/src/cmd/login.js +71 -4
- package/src/cmd/remix.js +3 -3
- package/src/cmd/scan.js +9 -4
- package/src/cmd/ship.js +3 -3
- package/src/cmd/skim.js +7 -1
- package/src/cmd/vet.js +3 -3
- package/src/cmd/vouch.js +3 -3
- package/src/lib/config.js +7 -3
- package/src/lib/error-format.js +78 -0
- package/src/lib/json-output.js +28 -3
- package/src/lib/oauth.js +34 -17
- package/src/lib/pds.js +31 -10
- package/src/lib/trust-gate.js +4 -4
- package/src/lib/vit-dir.js +33 -22
package/src/cmd/link.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
import { existsSync, mkdirSync, symlinkSync, unlinkSync, readlinkSync, writeFileSync } from 'node:fs';
|
|
5
5
|
import { join, resolve } from 'node:path';
|
|
6
6
|
import { mark, name } from '../lib/brand.js';
|
|
7
|
+
import { formatError } from '../lib/error-format.js';
|
|
7
8
|
|
|
8
9
|
export default function register(program) {
|
|
9
10
|
program
|
|
@@ -68,7 +69,7 @@ export default function register(program) {
|
|
|
68
69
|
console.log(`${mark} PATH: ok`);
|
|
69
70
|
}
|
|
70
71
|
} catch (err) {
|
|
71
|
-
console.error(err
|
|
72
|
+
console.error(formatError(err, { verbose: false }));
|
|
72
73
|
process.exitCode = 1;
|
|
73
74
|
}
|
|
74
75
|
});
|
package/src/cmd/login.js
CHANGED
|
@@ -11,6 +11,14 @@ import { loadConfig, saveConfig } from '../lib/config.js';
|
|
|
11
11
|
import { createOAuthClient, createSessionStore, createStore, checkSession } from '../lib/oauth.js';
|
|
12
12
|
import { configDir, configPath } from '../lib/paths.js';
|
|
13
13
|
import { vitDir } from '../lib/vit-dir.js';
|
|
14
|
+
import { errorMessage, formatError } from '../lib/error-format.js';
|
|
15
|
+
|
|
16
|
+
export const LOGIN_COMMON_ISSUES_FOOTER = `Common issues:
|
|
17
|
+
- make sure the handle is correct and resolves on Bluesky
|
|
18
|
+
- open the printed URL in your browser, approve vit, and wait for the callback to finish
|
|
19
|
+
- if you're on a remote machine, rerun with 'vit login <handle> --remote' and paste the full callback URL
|
|
20
|
+
- if you used an app password, create a fresh Bluesky app password and try again
|
|
21
|
+
- check your DNS, firewall, or VPN settings if vit cannot reach Bluesky`;
|
|
14
22
|
|
|
15
23
|
function ensureGitignore(dir, entry) {
|
|
16
24
|
const gitignorePath = join(dir, '.gitignore');
|
|
@@ -21,6 +29,42 @@ function ensureGitignore(dir, entry) {
|
|
|
21
29
|
}
|
|
22
30
|
}
|
|
23
31
|
|
|
32
|
+
export function cancelLogin({
|
|
33
|
+
server,
|
|
34
|
+
rl,
|
|
35
|
+
timer,
|
|
36
|
+
clearTimer = clearTimeout,
|
|
37
|
+
stderr = console.error,
|
|
38
|
+
exit = process.exit,
|
|
39
|
+
footer = LOGIN_COMMON_ISSUES_FOOTER,
|
|
40
|
+
}) {
|
|
41
|
+
try {
|
|
42
|
+
if (server?.listening) server.close();
|
|
43
|
+
} catch {
|
|
44
|
+
// Ignore cleanup failures during cancellation.
|
|
45
|
+
}
|
|
46
|
+
try {
|
|
47
|
+
rl?.close();
|
|
48
|
+
} catch {
|
|
49
|
+
// Ignore cleanup failures during cancellation.
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
if (timer) clearTimer(timer);
|
|
53
|
+
} catch {
|
|
54
|
+
// Ignore cleanup failures during cancellation.
|
|
55
|
+
}
|
|
56
|
+
stderr('\nLogin cancelled.');
|
|
57
|
+
stderr(footer);
|
|
58
|
+
exit(130);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function printLoginFailure(err, { verbose = false, includeFooter = false } = {}) {
|
|
62
|
+
console.error(formatError(err, { verbose }));
|
|
63
|
+
if (includeFooter) {
|
|
64
|
+
console.error(LOGIN_COMMON_ISSUES_FOOTER);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
24
68
|
export default function register(program) {
|
|
25
69
|
program
|
|
26
70
|
.command('login')
|
|
@@ -60,7 +104,9 @@ export default function register(program) {
|
|
|
60
104
|
return;
|
|
61
105
|
}
|
|
62
106
|
}
|
|
63
|
-
} catch {
|
|
107
|
+
} catch (err) {
|
|
108
|
+
console.warn(`warning: failed to read ${localPath}: ${errorMessage(err)}`);
|
|
109
|
+
}
|
|
64
110
|
}
|
|
65
111
|
} else {
|
|
66
112
|
const existing = loadConfig();
|
|
@@ -91,7 +137,13 @@ export default function register(program) {
|
|
|
91
137
|
} else {
|
|
92
138
|
const sessionFile = configPath('session.json');
|
|
93
139
|
let data = {};
|
|
94
|
-
|
|
140
|
+
if (existsSync(sessionFile)) {
|
|
141
|
+
try {
|
|
142
|
+
data = JSON.parse(readFileSync(sessionFile, 'utf-8'));
|
|
143
|
+
} catch (err) {
|
|
144
|
+
console.warn(`warning: failed to read ${sessionFile}: ${errorMessage(err)}`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
95
147
|
data[did] = { type: 'app-password', service: 'https://bsky.social', session };
|
|
96
148
|
mkdirSync(configDir, { recursive: true });
|
|
97
149
|
writeFileSync(sessionFile, JSON.stringify(data, null, 2) + '\n');
|
|
@@ -102,7 +154,7 @@ export default function register(program) {
|
|
|
102
154
|
|
|
103
155
|
console.log(`Logged in as ${did}`);
|
|
104
156
|
} catch (err) {
|
|
105
|
-
|
|
157
|
+
printLoginFailure(err, { verbose: opts.verbose, includeFooter: true });
|
|
106
158
|
process.exitCode = 1;
|
|
107
159
|
}
|
|
108
160
|
return;
|
|
@@ -111,6 +163,8 @@ export default function register(program) {
|
|
|
111
163
|
let server;
|
|
112
164
|
let timeout;
|
|
113
165
|
let rl;
|
|
166
|
+
let loginStage = 'preflight';
|
|
167
|
+
let onSigint = () => {};
|
|
114
168
|
|
|
115
169
|
try {
|
|
116
170
|
let resolveCallback;
|
|
@@ -139,6 +193,12 @@ export default function register(program) {
|
|
|
139
193
|
res.end('Not found');
|
|
140
194
|
});
|
|
141
195
|
|
|
196
|
+
onSigint = () => {
|
|
197
|
+
process.off('SIGINT', onSigint);
|
|
198
|
+
cancelLogin({ server, rl, timer: timeout });
|
|
199
|
+
};
|
|
200
|
+
process.once('SIGINT', onSigint);
|
|
201
|
+
|
|
142
202
|
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));
|
|
143
203
|
const port = server.address().port;
|
|
144
204
|
|
|
@@ -156,6 +216,7 @@ export default function register(program) {
|
|
|
156
216
|
const sessionStore = createSessionStore();
|
|
157
217
|
const client = createOAuthClient({ stateStore, sessionStore, redirectUri });
|
|
158
218
|
|
|
219
|
+
loginStage = 'authorize';
|
|
159
220
|
const authUrl = await client.authorize(handle, {
|
|
160
221
|
scope: 'atproto transition:generic',
|
|
161
222
|
});
|
|
@@ -182,6 +243,7 @@ export default function register(program) {
|
|
|
182
243
|
console.log(`Open this URL in your browser:\n ${authUrl.toString()}\n`);
|
|
183
244
|
}
|
|
184
245
|
|
|
246
|
+
loginStage = 'callback';
|
|
185
247
|
if (isRemote) {
|
|
186
248
|
rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
187
249
|
rl.question('Paste the callback URL from your browser: ', (line) => {
|
|
@@ -229,6 +291,7 @@ export default function register(program) {
|
|
|
229
291
|
}
|
|
230
292
|
|
|
231
293
|
console.log('Exchanging token...');
|
|
294
|
+
loginStage = 'token';
|
|
232
295
|
const { session } = await client.callback(params);
|
|
233
296
|
|
|
234
297
|
if (verbose) {
|
|
@@ -247,9 +310,13 @@ export default function register(program) {
|
|
|
247
310
|
}
|
|
248
311
|
console.log(`Logged in as ${session.did}`);
|
|
249
312
|
} catch (err) {
|
|
250
|
-
|
|
313
|
+
printLoginFailure(err, {
|
|
314
|
+
verbose: opts.verbose,
|
|
315
|
+
includeFooter: loginStage !== 'preflight',
|
|
316
|
+
});
|
|
251
317
|
process.exitCode = 1;
|
|
252
318
|
} finally {
|
|
319
|
+
process.removeListener('SIGINT', onSigint);
|
|
253
320
|
if (timeout) {
|
|
254
321
|
clearTimeout(timeout);
|
|
255
322
|
}
|
package/src/cmd/remix.js
CHANGED
|
@@ -12,6 +12,7 @@ import { brand, name } from '../lib/brand.js';
|
|
|
12
12
|
import { resolvePds, listRecordsFromPds, batchQuery } from '../lib/pds.js';
|
|
13
13
|
import { loadConfig } from '../lib/config.js';
|
|
14
14
|
import { jsonOk, jsonError } from '../lib/json-output.js';
|
|
15
|
+
import { formatError } from '../lib/error-format.js';
|
|
15
16
|
|
|
16
17
|
export default function register(program) {
|
|
17
18
|
program
|
|
@@ -173,12 +174,11 @@ export default function register(program) {
|
|
|
173
174
|
console.log('');
|
|
174
175
|
console.log(text);
|
|
175
176
|
} catch (err) {
|
|
176
|
-
const msg = err instanceof Error ? err.message : String(err);
|
|
177
177
|
if (opts.json) {
|
|
178
|
-
jsonError(
|
|
178
|
+
jsonError(err);
|
|
179
179
|
return;
|
|
180
180
|
}
|
|
181
|
-
console.error(
|
|
181
|
+
console.error(formatError(err, { verbose: opts.verbose }));
|
|
182
182
|
process.exitCode = 1;
|
|
183
183
|
}
|
|
184
184
|
});
|
package/src/cmd/scan.js
CHANGED
|
@@ -7,6 +7,7 @@ import { resolveHandleFromDid } from '../lib/pds.js';
|
|
|
7
7
|
import { brand } from '../lib/brand.js';
|
|
8
8
|
import { jsonOk, jsonError } from '../lib/json-output.js';
|
|
9
9
|
import { readBeaconSet } from '../lib/vit-dir.js';
|
|
10
|
+
import { formatError } from '../lib/error-format.js';
|
|
10
11
|
|
|
11
12
|
export default function register(program) {
|
|
12
13
|
program
|
|
@@ -91,7 +92,12 @@ export default function register(program) {
|
|
|
91
92
|
|
|
92
93
|
ws.onmessage = (event) => {
|
|
93
94
|
let msg;
|
|
94
|
-
try {
|
|
95
|
+
try {
|
|
96
|
+
msg = JSON.parse(event.data);
|
|
97
|
+
} catch {
|
|
98
|
+
console.warn('warning: failed to parse Jetstream event as JSON; skipping');
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
95
101
|
|
|
96
102
|
if (msg.kind !== 'commit' || msg.commit?.operation !== 'create') return;
|
|
97
103
|
|
|
@@ -197,12 +203,11 @@ export default function register(program) {
|
|
|
197
203
|
console.log(` ${parts.join(' ')}`);
|
|
198
204
|
}
|
|
199
205
|
} catch (err) {
|
|
200
|
-
const msg = err instanceof Error ? err.message : String(err);
|
|
201
206
|
if (opts.json) {
|
|
202
|
-
jsonError(
|
|
207
|
+
jsonError(err);
|
|
203
208
|
return;
|
|
204
209
|
}
|
|
205
|
-
console.error(
|
|
210
|
+
console.error(formatError(err, { verbose: opts.verbose }));
|
|
206
211
|
process.exitCode = 1;
|
|
207
212
|
}
|
|
208
213
|
});
|
package/src/cmd/ship.js
CHANGED
|
@@ -16,6 +16,7 @@ import { resolvePds, listRecordsFromPds, batchQuery } from '../lib/pds.js';
|
|
|
16
16
|
import { jsonOk, jsonError } from '../lib/json-output.js';
|
|
17
17
|
import { toBeacon } from '../lib/beacon.js';
|
|
18
18
|
import { hashTo3Words } from '../lib/cap-ref.js';
|
|
19
|
+
import { formatError } from '../lib/error-format.js';
|
|
19
20
|
|
|
20
21
|
const STOP_WORDS = new Set([
|
|
21
22
|
'a', 'an', 'the', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for',
|
|
@@ -664,12 +665,11 @@ export default function register(program) {
|
|
|
664
665
|
await shipCap(opts);
|
|
665
666
|
}
|
|
666
667
|
} catch (err) {
|
|
667
|
-
const msg = err instanceof Error ? err.message : String(err);
|
|
668
668
|
if (opts.json) {
|
|
669
|
-
jsonError(
|
|
669
|
+
jsonError(err);
|
|
670
670
|
return;
|
|
671
671
|
}
|
|
672
|
-
console.error(
|
|
672
|
+
console.error(formatError(err, { verbose: opts.verbose }));
|
|
673
673
|
process.exitCode = 1;
|
|
674
674
|
}
|
|
675
675
|
})
|
package/src/cmd/skim.js
CHANGED
|
@@ -10,6 +10,8 @@ import { resolveRef } from '../lib/cap-ref.js';
|
|
|
10
10
|
import { skillRefFromName } from '../lib/skill-ref.js';
|
|
11
11
|
import { name } from '../lib/brand.js';
|
|
12
12
|
import { resolvePds, listRecordsFromPds, batchQuery } from '../lib/pds.js';
|
|
13
|
+
import { jsonError } from '../lib/json-output.js';
|
|
14
|
+
import { formatError } from '../lib/error-format.js';
|
|
13
15
|
|
|
14
16
|
export default function register(program) {
|
|
15
17
|
program
|
|
@@ -208,7 +210,11 @@ export default function register(program) {
|
|
|
208
210
|
}
|
|
209
211
|
}
|
|
210
212
|
} catch (err) {
|
|
211
|
-
|
|
213
|
+
if (opts.json) {
|
|
214
|
+
jsonError(err);
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
console.error(formatError(err, { verbose: opts.verbose }));
|
|
212
218
|
process.exitCode = 1;
|
|
213
219
|
}
|
|
214
220
|
});
|
package/src/cmd/vet.js
CHANGED
|
@@ -17,6 +17,7 @@ import { resolvePds, listRecordsFromPds, batchQuery } from '../lib/pds.js';
|
|
|
17
17
|
import { loadConfig } from '../lib/config.js';
|
|
18
18
|
import { jsonOk, jsonError } from '../lib/json-output.js';
|
|
19
19
|
import { sandboxArgs } from '../lib/sandbox.js';
|
|
20
|
+
import { formatError } from '../lib/error-format.js';
|
|
20
21
|
|
|
21
22
|
const execFileAsync = promisify(execFile);
|
|
22
23
|
|
|
@@ -629,12 +630,11 @@ export default function register(program) {
|
|
|
629
630
|
console.log(` vit vet ${ref} --trust`);
|
|
630
631
|
}
|
|
631
632
|
} catch (err) {
|
|
632
|
-
const msg = err instanceof Error ? err.message : String(err);
|
|
633
633
|
if (opts.json) {
|
|
634
|
-
jsonError(
|
|
634
|
+
jsonError(err);
|
|
635
635
|
return;
|
|
636
636
|
}
|
|
637
|
-
console.error(
|
|
637
|
+
console.error(formatError(err, { verbose: opts.verbose }));
|
|
638
638
|
process.exitCode = 1;
|
|
639
639
|
}
|
|
640
640
|
});
|
package/src/cmd/vouch.js
CHANGED
|
@@ -12,6 +12,7 @@ import { mark, name } from '../lib/brand.js';
|
|
|
12
12
|
import { resolvePds, listRecordsFromPds, batchQuery } from '../lib/pds.js';
|
|
13
13
|
import { loadConfig } from '../lib/config.js';
|
|
14
14
|
import { jsonOk, jsonError } from '../lib/json-output.js';
|
|
15
|
+
import { formatError } from '../lib/error-format.js';
|
|
15
16
|
|
|
16
17
|
export default function register(program) {
|
|
17
18
|
program
|
|
@@ -302,12 +303,11 @@ export default function register(program) {
|
|
|
302
303
|
}
|
|
303
304
|
}
|
|
304
305
|
} catch (err) {
|
|
305
|
-
const msg = err instanceof Error ? err.message : String(err);
|
|
306
306
|
if (opts.json) {
|
|
307
|
-
jsonError(
|
|
307
|
+
jsonError(err);
|
|
308
308
|
return;
|
|
309
309
|
}
|
|
310
|
-
console.error(
|
|
310
|
+
console.error(formatError(err, { verbose: opts.verbose }));
|
|
311
311
|
process.exitCode = 1;
|
|
312
312
|
}
|
|
313
313
|
});
|
package/src/lib/config.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'node:fs';
|
|
5
5
|
import { join } from 'node:path';
|
|
6
6
|
import { configDir, configPath } from './paths.js';
|
|
7
|
+
import { errorMessage } from './error-format.js';
|
|
7
8
|
|
|
8
9
|
const vitJsonPath = configPath('vit.json');
|
|
9
10
|
|
|
@@ -11,7 +12,8 @@ export function loadConfig() {
|
|
|
11
12
|
if (!existsSync(vitJsonPath)) return {};
|
|
12
13
|
try {
|
|
13
14
|
return JSON.parse(readFileSync(vitJsonPath, 'utf-8'));
|
|
14
|
-
} catch {
|
|
15
|
+
} catch (err) {
|
|
16
|
+
console.warn(`warning: failed to read ${vitJsonPath}: ${errorMessage(err)}`);
|
|
15
17
|
return {};
|
|
16
18
|
}
|
|
17
19
|
}
|
|
@@ -26,13 +28,15 @@ export function saveConfig(obj) {
|
|
|
26
28
|
|
|
27
29
|
export function requireDid(opts) {
|
|
28
30
|
if (opts?.did) return opts.did;
|
|
31
|
+
const localLogin = join(process.cwd(), '.vit', 'login.json');
|
|
29
32
|
try {
|
|
30
|
-
const localLogin = join(process.cwd(), '.vit', 'login.json');
|
|
31
33
|
if (existsSync(localLogin)) {
|
|
32
34
|
const local = JSON.parse(readFileSync(localLogin, 'utf-8'));
|
|
33
35
|
if (local.did) return local.did;
|
|
34
36
|
}
|
|
35
|
-
} catch {
|
|
37
|
+
} catch (err) {
|
|
38
|
+
console.warn(`warning: failed to read ${localLogin}: ${errorMessage(err)}`);
|
|
39
|
+
}
|
|
36
40
|
const did = loadConfig().did;
|
|
37
41
|
if (!did) {
|
|
38
42
|
console.error("no DID configured. run 'vit login <handle>' first or pass --did.");
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
// Copyright (c) 2026 sol pbc
|
|
3
|
+
|
|
4
|
+
const MAX_CAUSES = 10;
|
|
5
|
+
|
|
6
|
+
function isObjectLike(value) {
|
|
7
|
+
return (typeof value === 'object' && value !== null) || typeof value === 'function';
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function errorMessage(value) {
|
|
11
|
+
if (value instanceof Error) return value.message || String(value);
|
|
12
|
+
if (isObjectLike(value) && typeof value.message === 'string') return value.message;
|
|
13
|
+
return String(value);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function stackLinesOf(value) {
|
|
17
|
+
if (!(value instanceof Error) || typeof value.stack !== 'string' || value.stack === '') return [];
|
|
18
|
+
return value.stack.split('\n').map(line => ` ${line}`);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function walkChain(err) {
|
|
22
|
+
if (!(err instanceof Error)) {
|
|
23
|
+
return [{ value: err, message: String(err), stackLines: [] }];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const levels = [];
|
|
27
|
+
const seen = new Set();
|
|
28
|
+
let current = err;
|
|
29
|
+
let causeCount = 0;
|
|
30
|
+
|
|
31
|
+
while (current !== undefined) {
|
|
32
|
+
if (isObjectLike(current)) {
|
|
33
|
+
if (seen.has(current)) break;
|
|
34
|
+
seen.add(current);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
levels.push({
|
|
38
|
+
value: current,
|
|
39
|
+
message: errorMessage(current),
|
|
40
|
+
stackLines: stackLinesOf(current),
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
if (!isObjectLike(current) || !('cause' in current)) break;
|
|
44
|
+
|
|
45
|
+
const next = current.cause;
|
|
46
|
+
if (next === undefined) break;
|
|
47
|
+
if (causeCount >= MAX_CAUSES) break;
|
|
48
|
+
|
|
49
|
+
current = next;
|
|
50
|
+
causeCount += 1;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return levels;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function collectCauses(err) {
|
|
57
|
+
if (!(err instanceof Error)) return [];
|
|
58
|
+
return walkChain(err).slice(1).map(level => level.message);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function formatError(err, { hint, verbose = false } = {}) {
|
|
62
|
+
const levels = walkChain(err);
|
|
63
|
+
const lines = [];
|
|
64
|
+
|
|
65
|
+
for (let i = 0; i < levels.length; i += 1) {
|
|
66
|
+
const level = levels[i];
|
|
67
|
+
lines.push(i === 0 ? level.message : ` caused by: ${level.message}`);
|
|
68
|
+
if (verbose && level.stackLines.length > 0) {
|
|
69
|
+
lines.push(...level.stackLines);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (typeof hint === 'string' && hint) {
|
|
74
|
+
lines.push(`hint: ${hint}`);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return lines.join('\n');
|
|
78
|
+
}
|
package/src/lib/json-output.js
CHANGED
|
@@ -1,13 +1,38 @@
|
|
|
1
1
|
// SPDX-License-Identifier: MIT
|
|
2
2
|
// Copyright (c) 2026 sol pbc
|
|
3
3
|
|
|
4
|
+
import { collectCauses } from './error-format.js';
|
|
5
|
+
|
|
6
|
+
function errorMessage(input) {
|
|
7
|
+
if (input instanceof Error) return input.message || String(input);
|
|
8
|
+
return String(input);
|
|
9
|
+
}
|
|
10
|
+
|
|
4
11
|
export function jsonOk(data) {
|
|
5
12
|
console.log(JSON.stringify({ ok: true, ...data }, null, 2));
|
|
6
13
|
}
|
|
7
14
|
|
|
8
|
-
export function jsonError(
|
|
9
|
-
|
|
10
|
-
|
|
15
|
+
export function jsonError(input, hintArg) {
|
|
16
|
+
if (typeof input === 'string') {
|
|
17
|
+
const obj = { ok: false, error: input };
|
|
18
|
+
if (hintArg) obj.hint = hintArg;
|
|
19
|
+
console.log(JSON.stringify(obj, null, 2));
|
|
20
|
+
process.exitCode = 1;
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const obj = {
|
|
25
|
+
ok: false,
|
|
26
|
+
error: errorMessage(input),
|
|
27
|
+
};
|
|
28
|
+
const causes = collectCauses(input);
|
|
29
|
+
if (causes.length > 0) obj.causes = causes;
|
|
30
|
+
|
|
31
|
+
const resolvedHint = typeof hintArg === 'string'
|
|
32
|
+
? hintArg
|
|
33
|
+
: (typeof input?.hint === 'string' ? input.hint : undefined);
|
|
34
|
+
if (resolvedHint) obj.hint = resolvedHint;
|
|
35
|
+
|
|
11
36
|
console.log(JSON.stringify(obj, null, 2));
|
|
12
37
|
process.exitCode = 1;
|
|
13
38
|
}
|
package/src/lib/oauth.js
CHANGED
|
@@ -6,6 +6,7 @@ import { NodeOAuthClient } from '@atproto/oauth-client-node';
|
|
|
6
6
|
import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'node:fs';
|
|
7
7
|
import { join } from 'node:path';
|
|
8
8
|
import { configDir, configPath } from './paths.js';
|
|
9
|
+
import { errorMessage } from './error-format.js';
|
|
9
10
|
|
|
10
11
|
const requestLock = async (_name, fn) => await fn();
|
|
11
12
|
|
|
@@ -44,9 +45,13 @@ export function createStore() {
|
|
|
44
45
|
export function createSessionStore() {
|
|
45
46
|
const sessionFile = configPath('session.json');
|
|
46
47
|
let data = {};
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
if (existsSync(sessionFile)) {
|
|
49
|
+
try {
|
|
50
|
+
data = JSON.parse(readFileSync(sessionFile, 'utf-8'));
|
|
51
|
+
} catch (err) {
|
|
52
|
+
console.warn(`warning: failed to read ${sessionFile}: ${errorMessage(err)}`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
50
55
|
return {
|
|
51
56
|
set: async (key, value) => {
|
|
52
57
|
data[key] = value;
|
|
@@ -64,18 +69,22 @@ export function createSessionStore() {
|
|
|
64
69
|
|
|
65
70
|
export function checkSession(did) {
|
|
66
71
|
// Check project-local app-password session
|
|
72
|
+
const localPath = join(process.cwd(), '.vit', 'login.json');
|
|
67
73
|
try {
|
|
68
|
-
const localPath = join(process.cwd(), '.vit', 'login.json');
|
|
69
74
|
if (existsSync(localPath)) {
|
|
70
75
|
const local = JSON.parse(readFileSync(localPath, 'utf-8'));
|
|
71
76
|
if (local.did === did && local.type === 'app-password' && local.session?.accessJwt) {
|
|
72
77
|
return did;
|
|
73
78
|
}
|
|
74
79
|
}
|
|
75
|
-
} catch {
|
|
80
|
+
} catch (err) {
|
|
81
|
+
console.warn(`warning: failed to read ${localPath}: ${errorMessage(err)}`);
|
|
82
|
+
}
|
|
76
83
|
|
|
84
|
+
const sessionFile = configPath('session.json');
|
|
85
|
+
if (!existsSync(sessionFile)) return null;
|
|
77
86
|
try {
|
|
78
|
-
const raw = readFileSync(
|
|
87
|
+
const raw = readFileSync(sessionFile, 'utf-8');
|
|
79
88
|
const data = JSON.parse(raw);
|
|
80
89
|
const entry = data[did];
|
|
81
90
|
if (!entry) return null;
|
|
@@ -89,7 +98,8 @@ export function checkSession(did) {
|
|
|
89
98
|
const accessValid = tokenSet.expires_at && new Date(tokenSet.expires_at) > new Date();
|
|
90
99
|
if (accessValid || tokenSet.refresh_token) return did;
|
|
91
100
|
return null;
|
|
92
|
-
} catch {
|
|
101
|
+
} catch (err) {
|
|
102
|
+
console.warn(`warning: failed to read ${sessionFile}: ${errorMessage(err)}`);
|
|
93
103
|
return null;
|
|
94
104
|
}
|
|
95
105
|
}
|
|
@@ -108,8 +118,8 @@ export function createOAuthClient({ stateStore, sessionStore, redirectUri }) {
|
|
|
108
118
|
|
|
109
119
|
export async function restoreAgent(did) {
|
|
110
120
|
// Check project-local app-password session
|
|
121
|
+
const localPath = join(process.cwd(), '.vit', 'login.json');
|
|
111
122
|
try {
|
|
112
|
-
const localPath = join(process.cwd(), '.vit', 'login.json');
|
|
113
123
|
if (existsSync(localPath)) {
|
|
114
124
|
const local = JSON.parse(readFileSync(localPath, 'utf-8'));
|
|
115
125
|
if (local.did === did && local.type === 'app-password' && local.session) {
|
|
@@ -118,19 +128,26 @@ export async function restoreAgent(did) {
|
|
|
118
128
|
return { agent, session: { did: local.did, handle: local.handle } };
|
|
119
129
|
}
|
|
120
130
|
}
|
|
121
|
-
} catch {
|
|
131
|
+
} catch (err) {
|
|
132
|
+
console.warn(`warning: failed to read ${localPath}: ${errorMessage(err)}`);
|
|
133
|
+
}
|
|
122
134
|
|
|
123
135
|
// Check global app-password session
|
|
136
|
+
const sessionFile = configPath('session.json');
|
|
124
137
|
try {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
138
|
+
if (existsSync(sessionFile)) {
|
|
139
|
+
const raw = readFileSync(sessionFile, 'utf-8');
|
|
140
|
+
const data = JSON.parse(raw);
|
|
141
|
+
const entry = data[did];
|
|
142
|
+
if (entry?.type === 'app-password' && entry.session) {
|
|
143
|
+
const agent = new AtpAgent({ service: entry.service || 'https://bsky.social' });
|
|
144
|
+
await agent.resumeSession(entry.session);
|
|
145
|
+
return { agent, session: { did, handle: entry.session.handle } };
|
|
146
|
+
}
|
|
132
147
|
}
|
|
133
|
-
} catch {
|
|
148
|
+
} catch (err) {
|
|
149
|
+
console.warn(`warning: failed to read ${sessionFile}: ${errorMessage(err)}`);
|
|
150
|
+
}
|
|
134
151
|
|
|
135
152
|
// Existing OAuth restore path
|
|
136
153
|
const sessionStore = createSessionStore();
|
package/src/lib/pds.js
CHANGED
|
@@ -1,9 +1,35 @@
|
|
|
1
1
|
// SPDX-License-Identifier: MIT
|
|
2
2
|
// Copyright (c) 2026 sol pbc
|
|
3
3
|
|
|
4
|
+
import { errorMessage } from './error-format.js';
|
|
5
|
+
|
|
4
6
|
const PLC_DIRECTORY = 'https://plc.directory';
|
|
5
7
|
const pdsCache = new Map();
|
|
6
8
|
|
|
9
|
+
function requestErrorMessage(method, url, err) {
|
|
10
|
+
const code = err?.cause?.code || err?.code;
|
|
11
|
+
if (code === 'ECONNREFUSED') return `could not connect to ${url} (refused)`;
|
|
12
|
+
if (code === 'ENOTFOUND') return `could not resolve ${url}`;
|
|
13
|
+
if (code === 'ETIMEDOUT' || code === 'UND_ERR_CONNECT_TIMEOUT') {
|
|
14
|
+
return `timed out connecting to ${url}`;
|
|
15
|
+
}
|
|
16
|
+
return `request to ${url} failed: ${errorMessage(err)}`;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async function fetchJson(method, url) {
|
|
20
|
+
const requestUrl = url.toString();
|
|
21
|
+
try {
|
|
22
|
+
const res = await fetch(url);
|
|
23
|
+
if (!res.ok) throw new Error(`${method} ${requestUrl} returned ${res.status}`);
|
|
24
|
+
return await res.json();
|
|
25
|
+
} catch (err) {
|
|
26
|
+
if (err instanceof Error && err.message.startsWith(`${method} ${requestUrl} returned `)) {
|
|
27
|
+
throw err;
|
|
28
|
+
}
|
|
29
|
+
throw new Error(requestErrorMessage(method, requestUrl, err), { cause: err });
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
7
33
|
async function fetchDidDocument(did) {
|
|
8
34
|
let url;
|
|
9
35
|
if (did.startsWith('did:web:')) {
|
|
@@ -19,9 +45,7 @@ async function fetchDidDocument(did) {
|
|
|
19
45
|
url = `${PLC_DIRECTORY}/${did}`;
|
|
20
46
|
}
|
|
21
47
|
|
|
22
|
-
|
|
23
|
-
if (!res.ok) throw new Error(`failed to resolve DID document for ${did}: ${res.status} ${res.statusText}`);
|
|
24
|
-
return res.json();
|
|
48
|
+
return fetchJson('GET', url);
|
|
25
49
|
}
|
|
26
50
|
|
|
27
51
|
export async function resolvePds(did) {
|
|
@@ -42,9 +66,7 @@ export async function listRecordsFromPds(pdsUrl, repo, collection, limit) {
|
|
|
42
66
|
url.searchParams.set('collection', collection);
|
|
43
67
|
if (limit) url.searchParams.set('limit', String(limit));
|
|
44
68
|
if (cursor) url.searchParams.set('cursor', cursor);
|
|
45
|
-
const
|
|
46
|
-
if (!res.ok) throw new Error(`listRecords failed for ${repo}: ${res.status} ${res.statusText}`);
|
|
47
|
-
const data = await res.json();
|
|
69
|
+
const data = await fetchJson('GET', url);
|
|
48
70
|
records.push(...data.records);
|
|
49
71
|
cursor = data.cursor;
|
|
50
72
|
} while (cursor);
|
|
@@ -56,16 +78,15 @@ export async function resolveHandleFromDid(did) {
|
|
|
56
78
|
const doc = await fetchDidDocument(did);
|
|
57
79
|
const aka = doc.alsoKnownAs?.find(a => a.startsWith('at://'));
|
|
58
80
|
return aka ? aka.replace('at://', '') : did;
|
|
59
|
-
} catch {
|
|
81
|
+
} catch (err) {
|
|
82
|
+
console.warn(`warning: failed to resolve handle for ${did}: ${errorMessage(err)}`);
|
|
60
83
|
return did;
|
|
61
84
|
}
|
|
62
85
|
}
|
|
63
86
|
|
|
64
87
|
export async function resolveHandle(handle) {
|
|
65
88
|
const url = `https://public.api.bsky.app/xrpc/com.atproto.identity.resolveHandle?handle=${encodeURIComponent(handle)}`;
|
|
66
|
-
const
|
|
67
|
-
if (!res.ok) throw new Error(`could not resolve handle: ${handle}`);
|
|
68
|
-
const data = await res.json();
|
|
89
|
+
const data = await fetchJson('GET', url);
|
|
69
90
|
return data.did;
|
|
70
91
|
}
|
|
71
92
|
|