@peer2peer/cli 0.0.0-stage → 0.1.0

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.
Files changed (105) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +366 -2
  3. package/dist/ancestry.d.ts +13 -0
  4. package/dist/ancestry.js +71 -0
  5. package/dist/app.d.ts +363 -0
  6. package/dist/app.js +1177 -0
  7. package/dist/bin.d.ts +2 -0
  8. package/dist/bin.js +56 -0
  9. package/dist/circle-ops.d.ts +402 -0
  10. package/dist/circle-ops.js +1725 -0
  11. package/dist/cli.d.ts +611 -0
  12. package/dist/cli.js +1650 -0
  13. package/dist/codes.d.ts +231 -0
  14. package/dist/codes.js +998 -0
  15. package/dist/config.d.ts +112 -0
  16. package/dist/config.js +243 -0
  17. package/dist/doctor.d.ts +99 -0
  18. package/dist/doctor.js +817 -0
  19. package/dist/errors.d.ts +8 -0
  20. package/dist/errors.js +29 -0
  21. package/dist/ext-host.d.ts +32 -0
  22. package/dist/ext-host.js +259 -0
  23. package/dist/extensions.d.ts +386 -0
  24. package/dist/extensions.js +395 -0
  25. package/dist/features.d.ts +91 -0
  26. package/dist/features.js +164 -0
  27. package/dist/format.d.ts +57 -0
  28. package/dist/format.js +248 -0
  29. package/dist/identity.d.ts +236 -0
  30. package/dist/identity.js +751 -0
  31. package/dist/inbound.d.ts +32 -0
  32. package/dist/inbound.js +258 -0
  33. package/dist/index.d.ts +8 -0
  34. package/dist/index.js +14 -0
  35. package/dist/join-answers.d.ts +49 -0
  36. package/dist/join-answers.js +149 -0
  37. package/dist/outbox.d.ts +109 -0
  38. package/dist/outbox.js +337 -0
  39. package/dist/pretty.d.ts +100 -0
  40. package/dist/pretty.js +256 -0
  41. package/dist/procstart.d.ts +1 -0
  42. package/dist/procstart.js +66 -0
  43. package/dist/progress.d.ts +30 -0
  44. package/dist/progress.js +78 -0
  45. package/dist/projects.d.ts +240 -0
  46. package/dist/projects.js +699 -0
  47. package/dist/prompt/picker.d.ts +64 -0
  48. package/dist/prompt/picker.js +152 -0
  49. package/dist/prompt/tty.d.ts +29 -0
  50. package/dist/prompt/tty.js +122 -0
  51. package/dist/readonly.d.ts +20 -0
  52. package/dist/readonly.js +50 -0
  53. package/dist/receipts.d.ts +99 -0
  54. package/dist/receipts.js +180 -0
  55. package/dist/relay-client.d.ts +277 -0
  56. package/dist/relay-client.js +873 -0
  57. package/dist/relay-info.d.ts +24 -0
  58. package/dist/relay-info.js +53 -0
  59. package/dist/rotation.d.ts +402 -0
  60. package/dist/rotation.js +2054 -0
  61. package/dist/running.d.ts +5 -0
  62. package/dist/running.js +74 -0
  63. package/dist/sessions.d.ts +340 -0
  64. package/dist/sessions.js +1152 -0
  65. package/dist/shortcode.d.ts +149 -0
  66. package/dist/shortcode.js +465 -0
  67. package/dist/social.d.ts +139 -0
  68. package/dist/social.js +485 -0
  69. package/dist/state.d.ts +483 -0
  70. package/dist/state.js +217 -0
  71. package/dist/stdin.d.ts +17 -0
  72. package/dist/stdin.js +46 -0
  73. package/dist/sync.d.ts +77 -0
  74. package/dist/sync.js +375 -0
  75. package/dist/transient.d.ts +1 -0
  76. package/dist/transient.js +28 -0
  77. package/dist/trust.d.ts +37 -0
  78. package/dist/trust.js +134 -0
  79. package/dist/tty-confirm.d.ts +2 -0
  80. package/dist/tty-confirm.js +53 -0
  81. package/dist/tui/command.d.ts +20 -0
  82. package/dist/tui/command.js +70 -0
  83. package/dist/tui/frame.d.ts +43 -0
  84. package/dist/tui/frame.js +111 -0
  85. package/dist/tui/model.d.ts +215 -0
  86. package/dist/tui/model.js +255 -0
  87. package/dist/tui/sanitize.d.ts +13 -0
  88. package/dist/tui/sanitize.js +151 -0
  89. package/dist/tui/terminal.d.ts +88 -0
  90. package/dist/tui/terminal.js +354 -0
  91. package/dist/tui/tui.d.ts +71 -0
  92. package/dist/tui/tui.js +571 -0
  93. package/dist/tui/view.d.ts +4 -0
  94. package/dist/tui/view.js +356 -0
  95. package/dist/unknown-kinds.d.ts +12 -0
  96. package/dist/unknown-kinds.js +66 -0
  97. package/dist/version.d.ts +1 -0
  98. package/dist/version.js +5 -0
  99. package/dist/want-pow.d.ts +12 -0
  100. package/dist/want-pow.js +46 -0
  101. package/dist/watch.d.ts +35 -0
  102. package/dist/watch.js +325 -0
  103. package/dist/xstate.d.ts +125 -0
  104. package/dist/xstate.js +422 -0
  105. package/package.json +60 -4
@@ -0,0 +1,112 @@
1
+ export type Env = Record<string, string | undefined>;
2
+ export interface FileConfig {
3
+ defaultRelays?: string[];
4
+ /** Enabled extension namespaces (`p2p extensions enable ix`); see extensions.ts. */
5
+ extensions?: string[];
6
+ transport?: Record<string, string>;
7
+ /** Send `caps` in statuses (§4.3 "Status caps"); on by default since P1, `false` opts out. P2P_STATUS_CAPS wins. */
8
+ statusCaps?: boolean;
9
+ /** Send delivery receipts (§4.3 "Delivery receipts"); on by default, `false` turns them off. P2P_RECEIPTS wins. */
10
+ receipts?: boolean;
11
+ /** Who this user trusts to drive a session that approves its own tool calls (trust.ts): full userIds and circle ids. */
12
+ trust?: {
13
+ users?: string[];
14
+ circles?: string[];
15
+ };
16
+ /** Publish this device's caps and version in the identity document (`dinfo`); off until step 2 (docs/compatibility.md). P2P_DEVICE_INFO wins. */
17
+ deviceInfo?: boolean;
18
+ }
19
+ /**
20
+ * Whether this device puts its caps and client version in the identity document (`dinfo`).
21
+ * Off by default in this release: pre-release clients from before protocol 3.1 refuse
22
+ * an identity document with a field they don't know, so the user would become unreachable to them.
23
+ * Step 2 (once the two previous releases are 3.1) turns it on. `P2P_DEVICE_INFO` (`1`/`true`,
24
+ * `0`/`false`) wins over `"deviceInfo": true` in config.json. Even on, it is published only to
25
+ * relays that list `identity.lenient` (relay-info.ts).
26
+ */
27
+ export declare function deviceInfoEnabled(env: Env, home: string): boolean;
28
+ /**
29
+ * Whether this user sends delivery receipts, and advertises `receipts.v1` (§4.3): on by default.
30
+ * `P2P_RECEIPTS` (`1`/`true` on, `0`/`false` off) wins over `"receipts": false` in config.json.
31
+ * Off, no receipt goes out and no status advertises the cap, so senders see what an older client
32
+ * looks like ("no receipt yet"): turning them off is not itself revealed.
33
+ */
34
+ export declare function receiptsEnabled(env: Env, home: string): boolean;
35
+ /**
36
+ * Whether statuses carry `caps` (two-step rollout, §4.3, step 2): on by
37
+ * default. `P2P_STATUS_CAPS` (`1`/`true` on, `0`/`false` off) wins over
38
+ * `"statusCaps": false` in config.json, the opt-out for a user whose peers
39
+ * still run a client older than the amendment (it rejects a status carrying
40
+ * `caps`). A broken config.json counts as the default (the command reports
41
+ * it elsewhere).
42
+ */
43
+ export declare function statusCapsEnabled(env: Env, home: string): boolean;
44
+ export declare function resolveHome(env: Env): string;
45
+ export declare function readFileConfig(home: string): FileConfig;
46
+ /** `p2p receipts on|off`: sets `receipts` in <home>/config.json (atomically, 0600). */
47
+ export declare function setReceiptsEnabled(home: string, on: boolean): void;
48
+ /**
49
+ * Read-modify-write of one top-level field of <home>/config.json under the state lock, so two
50
+ * commands changing it at once (`p2p receipts`, `p2p trust`) never lose an update (pre-release review N3).
51
+ * `fn` gets the field as stored and returns its next value and a result.
52
+ */
53
+ export declare function updateFileConfigField<R>(app: {
54
+ home: string;
55
+ state: {
56
+ withLock<T>(fn: () => Promise<T>): Promise<T>;
57
+ };
58
+ }, key: string, fn: (cur: unknown) => {
59
+ next: unknown;
60
+ result: R;
61
+ }): Promise<R>;
62
+ /** Sets one top-level field of <home>/config.json, keeping the rest (atomically, 0600). Callers hold the state lock. */
63
+ export declare function setFileConfigField(home: string, key: string, value: unknown): void;
64
+ /** Canonicalises, de-duplicates and bounds a relay list (1–5, §5.8). */
65
+ export declare function normalizeRelays(list: readonly string[]): string[];
66
+ /** `defaultRelays` from the `defaults.json` beside the CLI (or `P2P_DEFAULTS_FILE`); [] when there is none. */
67
+ export declare function shippedDefaultRelays(env: Env): string[];
68
+ /** The relays a new identity uses when `--relay` is not given. */
69
+ export declare function defaultRelays(env: Env, file: FileConfig): string[];
70
+ export interface Transport {
71
+ /** What the WebSocket dials. */
72
+ ws: string;
73
+ /** The document API base (§5.6): `<http>/identity/<userId>`. */
74
+ http: string;
75
+ }
76
+ export type TransportResolver = (canonical: string) => Transport;
77
+ export declare function transportResolver(env: Env, file: FileConfig): TransportResolver;
78
+ /** Tunables, mostly so tests run fast. */
79
+ export interface Tunables {
80
+ /** How long a short command listens for queued envelopes after connecting. */
81
+ drainQuietMs: number;
82
+ drainMaxMs: number;
83
+ /** Re-fetch a contact's identity document when the copy is older than this. */
84
+ docRefreshSec: number;
85
+ /** Link mailbox polling. */
86
+ linkPollMs: number;
87
+ /** Per-relay connect timeout: each of dial, hello and auth (and an HTTP document fetch). */
88
+ connectTimeoutMs: number;
89
+ /** Per relay request (send, identity, invite-*, …). */
90
+ requestTimeoutMs: number;
91
+ /** A whole short command (not `watch`; `--wait` adds to it; 0 = none). */
92
+ commandTimeoutSec: number;
93
+ /** How long a failed identity lookup is remembered before trying again. */
94
+ discoverRetrySec: number;
95
+ /** A relay refusing this device triggers at most one own-document refresh per this long. */
96
+ refusedRefreshSec: number;
97
+ /** A relay that refused a publish outright is retried automatically at most this often. */
98
+ refusedPublishRetrySec: number;
99
+ /** Bytes of circle history this client sends, per rolling hour, in answer to non-members' `want`s. */
100
+ strangerAnswerBytesPerHour: number;
101
+ /** Bytes of circle history sent per rolling hour to non-members whose want names no version they were offered. */
102
+ unlinkedAnswerBytesPerHour: number;
103
+ /** How long each device after the first in answering order waits before answering a want nobody answered. */
104
+ wantFallbackSec: number;
105
+ /** Bytes of one circle's history sent per rolling hour to all its members together. */
106
+ memberAnswerBytesPerCirclePerHour: number;
107
+ /** Bytes of circle history this client sends one asker (member or not) per rolling hour. */
108
+ answerBytesPerAskerPerHour: number;
109
+ /** Tests only: a smaller cap on a code's sealed bundle than the relay's (never a larger one). */
110
+ inviteBlobMax?: number;
111
+ }
112
+ export declare function tunables(env: Env): Tunables;
package/dist/config.js ADDED
@@ -0,0 +1,243 @@
1
+ // Copyright 2026 Ix Infrastructure Inc.
2
+ //
3
+ // Where the CLI keeps its state, which relays a new identity uses, and how a
4
+ // canonical relay URL is actually reached.
5
+ //
6
+ // - Home: `P2P_HOME`, else `~/.p2p`. The v3 trust store lives in
7
+ // `<home>/v3` (FileTrustStoreV3), the CLI's own documents in `<home>/cli`,
8
+ // both 0700 with 0600 files written atomically under a cross-process lock.
9
+ // - Default relays: none are hardcoded. `P2P_DEFAULT_RELAYS` (comma or
10
+ // space separated) wins over `defaultRelays` in `<home>/config.json`,
11
+ // which wins over `defaultRelays` in a `defaults.json` shipped next to the
12
+ // CLI (the bundled single-file build; `P2P_DEFAULTS_FILE` names another).
13
+ // - Transport overrides: a canonical relay URL (always `wss://…`, §5.1) is
14
+ // dialled at itself, and its document API at `https://<host>/<path>`.
15
+ // `P2P_RELAY_TRANSPORT` (JSON) or `transport` in config.json can map a
16
+ // canonical URL to another dial URL, e.g. a local relay without TLS:
17
+ // `{"wss://relay.test/v3": "ws://127.0.0.1:8080/v3"}`. It is a hosts file
18
+ // for relays: the hello check still compares against the canonical URL.
19
+ import { mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
20
+ import { homedir } from "node:os";
21
+ import { join } from "node:path";
22
+ import { fileURLToPath } from "node:url";
23
+ import { canonicalRelayUrl, MAX_RELAYS, relayHttpsBase } from "@peer2peer/core/v3";
24
+ import { CliError } from "./errors.js";
25
+ /**
26
+ * Whether this device puts its caps and client version in the identity document (`dinfo`).
27
+ * Off by default in this release: pre-release clients from before protocol 3.1 refuse
28
+ * an identity document with a field they don't know, so the user would become unreachable to them.
29
+ * Step 2 (once the two previous releases are 3.1) turns it on. `P2P_DEVICE_INFO` (`1`/`true`,
30
+ * `0`/`false`) wins over `"deviceInfo": true` in config.json. Even on, it is published only to
31
+ * relays that list `identity.lenient` (relay-info.ts).
32
+ */
33
+ export function deviceInfoEnabled(env, home) {
34
+ const v = env.P2P_DEVICE_INFO?.trim().toLowerCase();
35
+ if (v === "1" || v === "true")
36
+ return true;
37
+ if (v === "0" || v === "false")
38
+ return false;
39
+ try {
40
+ return readFileConfig(home).deviceInfo === true;
41
+ }
42
+ catch {
43
+ return false;
44
+ }
45
+ }
46
+ /**
47
+ * Whether this user sends delivery receipts, and advertises `receipts.v1` (§4.3): on by default.
48
+ * `P2P_RECEIPTS` (`1`/`true` on, `0`/`false` off) wins over `"receipts": false` in config.json.
49
+ * Off, no receipt goes out and no status advertises the cap, so senders see what an older client
50
+ * looks like ("no receipt yet"): turning them off is not itself revealed.
51
+ */
52
+ export function receiptsEnabled(env, home) {
53
+ const v = env.P2P_RECEIPTS?.trim().toLowerCase();
54
+ if (v === "1" || v === "true")
55
+ return true;
56
+ if (v === "0" || v === "false")
57
+ return false;
58
+ try {
59
+ return readFileConfig(home).receipts !== false;
60
+ }
61
+ catch {
62
+ return true;
63
+ }
64
+ }
65
+ /**
66
+ * Whether statuses carry `caps` (two-step rollout, §4.3, step 2): on by
67
+ * default. `P2P_STATUS_CAPS` (`1`/`true` on, `0`/`false` off) wins over
68
+ * `"statusCaps": false` in config.json, the opt-out for a user whose peers
69
+ * still run a client older than the amendment (it rejects a status carrying
70
+ * `caps`). A broken config.json counts as the default (the command reports
71
+ * it elsewhere).
72
+ */
73
+ export function statusCapsEnabled(env, home) {
74
+ const v = env.P2P_STATUS_CAPS?.trim().toLowerCase();
75
+ if (v === "1" || v === "true")
76
+ return true;
77
+ if (v === "0" || v === "false")
78
+ return false;
79
+ try {
80
+ return readFileConfig(home).statusCaps !== false;
81
+ }
82
+ catch {
83
+ return true;
84
+ }
85
+ }
86
+ export function resolveHome(env) {
87
+ const h = env.P2P_HOME?.trim();
88
+ return h ? h : join(homedir(), ".p2p");
89
+ }
90
+ export function readFileConfig(home) {
91
+ let raw;
92
+ try {
93
+ raw = readFileSync(join(home, "config.json"), "utf8");
94
+ }
95
+ catch {
96
+ return {};
97
+ }
98
+ let v;
99
+ try {
100
+ v = JSON.parse(raw);
101
+ }
102
+ catch {
103
+ throw new CliError("config", `${join(home, "config.json")} is not valid JSON`);
104
+ }
105
+ if (typeof v !== "object" || v === null || Array.isArray(v))
106
+ throw new CliError("config", "config.json must hold a JSON object");
107
+ return v;
108
+ }
109
+ /** `p2p receipts on|off`: sets `receipts` in <home>/config.json (atomically, 0600). */
110
+ export function setReceiptsEnabled(home, on) {
111
+ setFileConfigField(home, "receipts", on);
112
+ }
113
+ /**
114
+ * Read-modify-write of one top-level field of <home>/config.json under the state lock, so two
115
+ * commands changing it at once (`p2p receipts`, `p2p trust`) never lose an update (pre-release review N3).
116
+ * `fn` gets the field as stored and returns its next value and a result.
117
+ */
118
+ export async function updateFileConfigField(app, key, fn) {
119
+ return app.state.withLock(async () => {
120
+ const r = fn(readFileConfig(app.home)[key]);
121
+ setFileConfigField(app.home, key, r.next);
122
+ return r.result;
123
+ });
124
+ }
125
+ /** Sets one top-level field of <home>/config.json, keeping the rest (atomically, 0600). Callers hold the state lock. */
126
+ export function setFileConfigField(home, key, value) {
127
+ const p = join(home, "config.json");
128
+ const cfg = readFileConfig(home);
129
+ mkdirSync(home, { recursive: true, mode: 0o700 });
130
+ const tmp = `${p}.${process.pid}.tmp`;
131
+ writeFileSync(tmp, JSON.stringify({ ...cfg, [key]: value }, null, 2) + "\n", { mode: 0o600 });
132
+ renameSync(tmp, p);
133
+ }
134
+ /** Canonicalises, de-duplicates and bounds a relay list (1–5, §5.8). */
135
+ export function normalizeRelays(list) {
136
+ const out = [];
137
+ for (const r of list) {
138
+ let c;
139
+ try {
140
+ c = canonicalRelayUrl(r.trim());
141
+ }
142
+ catch (e) {
143
+ throw new CliError("bad_relay", `${r} is not a relay URL: ${e.message}`);
144
+ }
145
+ if (!out.includes(c))
146
+ out.push(c);
147
+ }
148
+ if (out.length > MAX_RELAYS)
149
+ throw new CliError("bad_relay", `at most ${MAX_RELAYS} relays (got ${out.length})`);
150
+ return out;
151
+ }
152
+ /** `defaultRelays` from the `defaults.json` beside the CLI (or `P2P_DEFAULTS_FILE`); [] when there is none. */
153
+ export function shippedDefaultRelays(env) {
154
+ let path;
155
+ try {
156
+ path = env.P2P_DEFAULTS_FILE?.trim() || fileURLToPath(new URL("./defaults.json", import.meta.url));
157
+ }
158
+ catch {
159
+ return [];
160
+ }
161
+ let raw;
162
+ try {
163
+ raw = readFileSync(path, "utf8");
164
+ }
165
+ catch {
166
+ return [];
167
+ }
168
+ try {
169
+ const v = JSON.parse(raw);
170
+ if (v.defaultRelays === undefined)
171
+ return [];
172
+ if (!Array.isArray(v.defaultRelays) || v.defaultRelays.some((x) => typeof x !== "string"))
173
+ throw new Error("defaultRelays must be a list of relay URLs");
174
+ return v.defaultRelays;
175
+ }
176
+ catch (e) {
177
+ throw new CliError("config", `${path}: ${e.message}`);
178
+ }
179
+ }
180
+ /** The relays a new identity uses when `--relay` is not given. */
181
+ export function defaultRelays(env, file) {
182
+ const fromEnv = env.P2P_DEFAULT_RELAYS?.split(/[\s,]+/).filter((s) => s !== "");
183
+ const list = fromEnv && fromEnv.length > 0 ? fromEnv : file.defaultRelays && file.defaultRelays.length > 0 ? file.defaultRelays : shippedDefaultRelays(env);
184
+ return normalizeRelays(list);
185
+ }
186
+ function httpFor(ws) {
187
+ return ws.replace(/^wss:/, "https:").replace(/^ws:/, "http:");
188
+ }
189
+ export function transportResolver(env, file) {
190
+ let map = { ...(file.transport ?? {}) };
191
+ if (env.P2P_RELAY_TRANSPORT) {
192
+ try {
193
+ const v = JSON.parse(env.P2P_RELAY_TRANSPORT);
194
+ if (typeof v !== "object" || v === null || Array.isArray(v))
195
+ throw new Error("not an object");
196
+ map = { ...map, ...v };
197
+ }
198
+ catch (e) {
199
+ throw new CliError("config", `P2P_RELAY_TRANSPORT must be a JSON object: ${e.message}`);
200
+ }
201
+ }
202
+ const canon = {};
203
+ for (const [k, v] of Object.entries(map)) {
204
+ if (typeof v !== "string" || !/^wss?:\/\//.test(v))
205
+ throw new CliError("config", `transport for ${k} must be a ws:// or wss:// URL`);
206
+ canon[canonicalRelayUrl(k)] = v.replace(/\/+$/, "");
207
+ }
208
+ return (relay) => {
209
+ const c = canonicalRelayUrl(relay);
210
+ const over = canon[c];
211
+ if (over)
212
+ return { ws: over, http: httpFor(over) };
213
+ return { ws: c, http: relayHttpsBase(c) };
214
+ };
215
+ }
216
+ export function tunables(env) {
217
+ const n = (k, d) => {
218
+ const v = env[k];
219
+ if (v === undefined || v.trim() === "")
220
+ return d;
221
+ const x = Number(v);
222
+ return Number.isFinite(x) && x >= 0 ? x : d;
223
+ };
224
+ return {
225
+ drainQuietMs: n("P2P_DRAIN_QUIET_MS", 400),
226
+ drainMaxMs: n("P2P_DRAIN_MAX_MS", 5000),
227
+ docRefreshSec: n("P2P_DOC_REFRESH_SEC", 60),
228
+ linkPollMs: n("P2P_LINK_POLL_MS", 2000),
229
+ connectTimeoutMs: n("P2P_CONNECT_TIMEOUT_MS", 10_000),
230
+ requestTimeoutMs: n("P2P_REQUEST_TIMEOUT_MS", 15_000),
231
+ commandTimeoutSec: n("P2P_COMMAND_TIMEOUT_SEC", 60),
232
+ discoverRetrySec: n("P2P_DISCOVER_RETRY_SEC", 60),
233
+ refusedRefreshSec: n("P2P_REFUSED_REFRESH_SEC", 60),
234
+ refusedPublishRetrySec: n("P2P_REFUSED_PUBLISH_RETRY_SEC", 3600),
235
+ strangerAnswerBytesPerHour: n("P2P_STRANGER_ANSWER_BYTES_PER_HOUR", 8 * 1024 * 1024),
236
+ unlinkedAnswerBytesPerHour: n("P2P_UNLINKED_ANSWER_BYTES_PER_HOUR", 1024 * 1024),
237
+ wantFallbackSec: n("P2P_WANT_FALLBACK_SEC", 120),
238
+ memberAnswerBytesPerCirclePerHour: n("P2P_MEMBER_ANSWER_BYTES_PER_CIRCLE_PER_HOUR", 16 * 1024 * 1024),
239
+ answerBytesPerAskerPerHour: n("P2P_ANSWER_BYTES_PER_ASKER_PER_HOUR", 4 * 1024 * 1024),
240
+ // Tests only (the "bundle too big" case): it can only lower the cap (codes.ts takes the smaller of the two).
241
+ ...(env.P2P_INVITE_BLOB_MAX ? { inviteBlobMax: n("P2P_INVITE_BLOB_MAX", 32 * 1024) } : {}),
242
+ };
243
+ }
@@ -0,0 +1,99 @@
1
+ import { App } from "./app.js";
2
+ import { type PeerDevice } from "./features.js";
3
+ export type CheckStatus = "ok" | "fail" | "unknown";
4
+ export interface Check {
5
+ id: string;
6
+ status: CheckStatus;
7
+ title: string;
8
+ detail: string;
9
+ /** One line: what to do (✗), or what would tell (?). */
10
+ fix?: string;
11
+ }
12
+ export interface DoctorResult {
13
+ scope: "device" | "user";
14
+ version: string;
15
+ user?: {
16
+ input: string;
17
+ userId?: string;
18
+ address?: string;
19
+ };
20
+ checks: Check[];
21
+ summary: {
22
+ ok: number;
23
+ fail: number;
24
+ unknown: number;
25
+ };
26
+ /** About a user: each of their devices with its client version and caps (docs/compatibility.md). */
27
+ devices?: PeerDevice[];
28
+ /** About a user: the known features one of you has and the other lacks. */
29
+ unavailable?: {
30
+ theyLack: string[];
31
+ weLack: string[];
32
+ };
33
+ }
34
+ export interface DoctorOptions {
35
+ /** The CLI's own version (cli.ts VERSION). */
36
+ version: string;
37
+ user?: string;
38
+ /** Overrides P2P_DOCTOR_TIMEOUT_MS (tests). */
39
+ timeoutMs?: number;
40
+ /** The mod's folder, when known (P2P_MOD_ROOT; else the folder around a bundled p2p.mjs). */
41
+ modRoot?: string;
42
+ }
43
+ /**
44
+ * Clock skew. Signed objects (device certificates, documents, caps, invites, statements) are
45
+ * refused when their `iat` is more than DEFAULT_SKEW_SEC (300 s) ahead of the verifier's clock,
46
+ * in core and at relays (relay auth too). Envelope `ts` alone tolerates 10 minutes
47
+ * (REPLAY_FUTURE_SKEW_MS), so 300 s is the real limit. Under SKEW_OK_SEC is fine (the Date
48
+ * header has 1 s resolution, plus half a round trip); up to the limit still works, so it is a
49
+ * warning; only past it does anything break.
50
+ */
51
+ export declare const SKEW_OK_SEC = 30;
52
+ export declare const SKEW_FAIL_SEC = 300;
53
+ export interface HttpProbe {
54
+ ok: boolean;
55
+ status?: number;
56
+ ms: number;
57
+ /** The server's clock (ms) from its Date header, and our midpoint of the request. */
58
+ serverMs?: number;
59
+ localMs?: number;
60
+ body?: Record<string, unknown>;
61
+ error?: string;
62
+ code?: "unreachable" | "timeout" | "network";
63
+ }
64
+ /** One bounded GET (the relay's document API), with its latency and the server's Date header. */
65
+ export declare function httpProbe(url: string, timeoutMs: number, opts?: {
66
+ maxBody?: number;
67
+ }): Promise<HttpProbe>;
68
+ export interface WsProbe {
69
+ /** Dial + hello (+ auth) took this long. */
70
+ connectMs?: number;
71
+ /** An authenticated round trip. */
72
+ rttMs?: number;
73
+ authed: boolean;
74
+ restricted: boolean;
75
+ /** Envelopes the relay pushed while we were connected (queued for this device; not acked). */
76
+ queued: number;
77
+ error?: string;
78
+ code?: string;
79
+ }
80
+ /** Dials one relay as this device (or anonymously), checks hello and auth, and makes one authenticated round trip. */
81
+ export declare function wsProbe(app: App, relay: string, timeoutMs: number, auth: boolean): Promise<WsProbe>;
82
+ export declare function modVersions(root: string): Promise<{
83
+ plugin?: string;
84
+ bundled?: string;
85
+ }>;
86
+ /**
87
+ * The clock check from each relay's skew (seconds, + when this device is ahead): ✓ under
88
+ * SKEW_OK_SEC, ? (a warning) up to the protocol's limit (SKEW_FAIL_SEC, 300 s), ✗ only past it.
89
+ */
90
+ export declare function clockVerdict(skews: readonly {
91
+ relay: string;
92
+ sec: number;
93
+ }[]): Check;
94
+ export declare function summarize(checks: readonly Check[]): DoctorResult["summary"];
95
+ /** The doctor's per-step bound: P2P_DOCTOR_TIMEOUT_MS (default 5 s), never more than the connect timeout. */
96
+ export declare function doctorTimeoutMs(app: App, override?: number): number;
97
+ export declare function doctor(app: App, o: DoctorOptions): Promise<DoctorResult>;
98
+ /** Human output: one line per check (✓ ✗ ?), with its fix indented below. */
99
+ export declare function formatDoctor(r: DoctorResult): string;