@peer2peer/cli 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 +201 -0
- package/README.md +370 -2
- package/dist/ancestry.d.ts +13 -0
- package/dist/ancestry.js +71 -0
- package/dist/app.d.ts +368 -0
- package/dist/app.js +1181 -0
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +56 -0
- package/dist/circle-ops.d.ts +402 -0
- package/dist/circle-ops.js +1725 -0
- package/dist/cli.d.ts +660 -0
- package/dist/cli.js +1787 -0
- package/dist/codes.d.ts +231 -0
- package/dist/codes.js +998 -0
- package/dist/config.d.ts +159 -0
- package/dist/config.js +294 -0
- package/dist/doctor.d.ts +113 -0
- package/dist/doctor.js +930 -0
- package/dist/errors.d.ts +8 -0
- package/dist/errors.js +29 -0
- package/dist/ext-host.d.ts +38 -0
- package/dist/ext-host.js +293 -0
- package/dist/extensions.d.ts +487 -0
- package/dist/extensions.js +406 -0
- package/dist/features.d.ts +91 -0
- package/dist/features.js +164 -0
- package/dist/format.d.ts +57 -0
- package/dist/format.js +248 -0
- package/dist/graph-memory.d.ts +107 -0
- package/dist/graph-memory.js +393 -0
- package/dist/history.d.ts +155 -0
- package/dist/history.js +697 -0
- package/dist/identity.d.ts +239 -0
- package/dist/identity.js +759 -0
- package/dist/inbound.d.ts +32 -0
- package/dist/inbound.js +261 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +14 -0
- package/dist/ix-local.d.ts +76 -0
- package/dist/ix-local.js +203 -0
- package/dist/join-answers.d.ts +49 -0
- package/dist/join-answers.js +149 -0
- package/dist/outbox.d.ts +109 -0
- package/dist/outbox.js +337 -0
- package/dist/pretty.d.ts +100 -0
- package/dist/pretty.js +256 -0
- package/dist/procstart.d.ts +1 -0
- package/dist/procstart.js +66 -0
- package/dist/progress.d.ts +30 -0
- package/dist/progress.js +78 -0
- package/dist/projects.d.ts +491 -0
- package/dist/projects.js +1383 -0
- package/dist/prompt/picker.d.ts +64 -0
- package/dist/prompt/picker.js +152 -0
- package/dist/prompt/tty.d.ts +29 -0
- package/dist/prompt/tty.js +122 -0
- package/dist/readonly.d.ts +20 -0
- package/dist/readonly.js +50 -0
- package/dist/receipts.d.ts +99 -0
- package/dist/receipts.js +182 -0
- package/dist/relay-client.d.ts +277 -0
- package/dist/relay-client.js +873 -0
- package/dist/relay-info.d.ts +24 -0
- package/dist/relay-info.js +53 -0
- package/dist/rotation.d.ts +402 -0
- package/dist/rotation.js +2054 -0
- package/dist/running.d.ts +5 -0
- package/dist/running.js +74 -0
- package/dist/sessions.d.ts +355 -0
- package/dist/sessions.js +1247 -0
- package/dist/shortcode.d.ts +149 -0
- package/dist/shortcode.js +465 -0
- package/dist/social.d.ts +139 -0
- package/dist/social.js +485 -0
- package/dist/state.d.ts +509 -0
- package/dist/state.js +220 -0
- package/dist/stdin.d.ts +17 -0
- package/dist/stdin.js +46 -0
- package/dist/sync.d.ts +77 -0
- package/dist/sync.js +375 -0
- package/dist/transient.d.ts +1 -0
- package/dist/transient.js +28 -0
- package/dist/trust.d.ts +37 -0
- package/dist/trust.js +134 -0
- package/dist/tty-confirm.d.ts +2 -0
- package/dist/tty-confirm.js +53 -0
- package/dist/tui/command.d.ts +20 -0
- package/dist/tui/command.js +70 -0
- package/dist/tui/frame.d.ts +43 -0
- package/dist/tui/frame.js +111 -0
- package/dist/tui/model.d.ts +221 -0
- package/dist/tui/model.js +259 -0
- package/dist/tui/sanitize.d.ts +13 -0
- package/dist/tui/sanitize.js +151 -0
- package/dist/tui/terminal.d.ts +88 -0
- package/dist/tui/terminal.js +354 -0
- package/dist/tui/tui.d.ts +78 -0
- package/dist/tui/tui.js +612 -0
- package/dist/tui/view.d.ts +4 -0
- package/dist/tui/view.js +358 -0
- package/dist/unknown-kinds.d.ts +12 -0
- package/dist/unknown-kinds.js +66 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +5 -0
- package/dist/want-pow.d.ts +12 -0
- package/dist/want-pow.js +46 -0
- package/dist/watch.d.ts +35 -0
- package/dist/watch.js +331 -0
- package/dist/xstate.d.ts +125 -0
- package/dist/xstate.js +422 -0
- package/package.json +60 -4
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export { isTransient } from "./transient.js";
|
|
2
|
+
import { type AcceptedMessageV3 } from "@peer2peer/core/v3";
|
|
3
|
+
import type { App } from "./app.js";
|
|
4
|
+
import type { EventRec } from "./state.js";
|
|
5
|
+
/**
|
|
6
|
+
* The command is exiting with these attempts still running (its close waited CLOSE_GRACE_MS):
|
|
7
|
+
* their claims are released now, without counting an attempt, and they apply nothing more. Left
|
|
8
|
+
* as they were, each exit counted an attempt against the envelope, and three short commands cut
|
|
9
|
+
* off in a row gave a legitimate envelope up unapplied (pre-release review M-A). If an attempt had written
|
|
10
|
+
* some effects already, the redelivery applies the envelope again: events are deduplicated by id,
|
|
11
|
+
* as after a crash between applying and committing (review M2).
|
|
12
|
+
*/
|
|
13
|
+
export declare function abandonInFlight(app: App): Promise<number>;
|
|
14
|
+
export type NewEvent = Omit<EventRec, "n">;
|
|
15
|
+
export interface EventFrom {
|
|
16
|
+
userId: string;
|
|
17
|
+
did: string;
|
|
18
|
+
address: string;
|
|
19
|
+
self: boolean;
|
|
20
|
+
}
|
|
21
|
+
export declare function fromOf(app: App, r: AcceptedMessageV3): Promise<EventFrom>;
|
|
22
|
+
/** Builds an event; `id` is derived from the envelope so every process logs it once. */
|
|
23
|
+
export declare function makeEvent(env: string, kind: string, at: number, data: Record<string, unknown>, extra?: Partial<NewEvent>, salt?: string): NewEvent;
|
|
24
|
+
/**
|
|
25
|
+
* How long one envelope may take before the command goes on without it (review M-A): the attempt
|
|
26
|
+
* keeps running (its claim stays alive) and counts toward the envelope's attempt limit, but a
|
|
27
|
+
* short command's drain isn't spent on it. P2P_ENVELOPE_TIMEOUT_MS overrides it.
|
|
28
|
+
*/
|
|
29
|
+
export declare const ENVELOPE_TIMEOUT_MS = 8000;
|
|
30
|
+
export declare function processEnvelope(app: App, env: string, relay: string): Promise<"ack" | "keep">;
|
|
31
|
+
/** Installs the handler and returns the app, for commands that connect. */
|
|
32
|
+
export declare function receiving(app: App): App;
|
package/dist/inbound.js
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
// Copyright 2026 Ix Infrastructure Inc.
|
|
2
|
+
//
|
|
3
|
+
// Every envelope a relay delivers goes through here, whichever command is
|
|
4
|
+
// connected: verifyIncomingV3 (the §4 receive chain, including the replay
|
|
5
|
+
// window that drops the copies other relays deliver), then the effect of the
|
|
6
|
+
// accepted kind, and finally zero or more events appended to the event log
|
|
7
|
+
// that `p2p watch` prints from. Keeping processing apart from printing means
|
|
8
|
+
// a message picked up by `p2p ls` still reaches the session's watcher.
|
|
9
|
+
import { randomBytes } from "node:crypto";
|
|
10
|
+
import { hostname } from "node:os";
|
|
11
|
+
import { processStartMs } from "./procstart.js";
|
|
12
|
+
import { isTransient } from "./transient.js";
|
|
13
|
+
export { isTransient } from "./transient.js";
|
|
14
|
+
import { decodeEnvelopeV3, jwsHash, P2PError, sanitizePeerText, verifyIncomingV3 } from "@peer2peer/core/v3";
|
|
15
|
+
import { answerDeferredWants, onCircleEnvelope } from "./circle-ops.js";
|
|
16
|
+
import { onSessionEnvelope } from "./sessions.js";
|
|
17
|
+
import { recordEvents } from "./history.js";
|
|
18
|
+
import { onReceipt } from "./receipts.js";
|
|
19
|
+
import { onUnknownKind } from "./unknown-kinds.js";
|
|
20
|
+
import { onContact, onUnfriendKind } from "./social.js";
|
|
21
|
+
import { importSync } from "./sync.js";
|
|
22
|
+
import { finishCodeJoins } from "./codes.js";
|
|
23
|
+
import { alertForeignRotations, retryRotationJoins } from "./rotation.js";
|
|
24
|
+
/** Rejections that only mean "already handled" (another relay's copy). */
|
|
25
|
+
const DUPLICATE = new Set(["replayed-seq", "replayed-nonce"]);
|
|
26
|
+
// Replay claims (core IncomingContextV3.replayClaim): an envelope enters the replay window only
|
|
27
|
+
// once it was applied, so a crash in between can't make its redelivery a "replayed" drop.
|
|
28
|
+
// A claim is alive while the attempt holding it runs: in this process, while its token is
|
|
29
|
+
// in flight; in another process on this machine, while that process lives; from another
|
|
30
|
+
// machine (a shared home), until the claim's TTL.
|
|
31
|
+
const HOST = hostname();
|
|
32
|
+
/** When this process started (ms). */
|
|
33
|
+
const STARTED = Math.round(Date.now() - process.uptime() * 1000);
|
|
34
|
+
/** Start times are compared to the second (ps prints seconds), with room for clock rounding. */
|
|
35
|
+
const START_SLACK_MS = 2500;
|
|
36
|
+
const PROCESS_TOKEN = randomBytes(9).toString("base64url");
|
|
37
|
+
let attempts = 0;
|
|
38
|
+
const inFlight = new Set();
|
|
39
|
+
/** Attempts running now, by token, with their envelope (abandonInFlight). */
|
|
40
|
+
const running = new Map();
|
|
41
|
+
/** Attempts given up because this process is exiting with them still running. */
|
|
42
|
+
const abandoned = new Set();
|
|
43
|
+
/**
|
|
44
|
+
* The command is exiting with these attempts still running (its close waited CLOSE_GRACE_MS):
|
|
45
|
+
* their claims are released now, without counting an attempt, and they apply nothing more. Left
|
|
46
|
+
* as they were, each exit counted an attempt against the envelope, and three short commands cut
|
|
47
|
+
* off in a row gave a legitimate envelope up unapplied (pre-release review M-A). If an attempt had written
|
|
48
|
+
* some effects already, the redelivery applies the envelope again: events are deduplicated by id,
|
|
49
|
+
* as after a crash between applying and committing (review M2).
|
|
50
|
+
*/
|
|
51
|
+
export async function abandonInFlight(app) {
|
|
52
|
+
let n = 0;
|
|
53
|
+
for (const [token, a] of running) {
|
|
54
|
+
if (a.app !== app || abandoned.has(token))
|
|
55
|
+
continue;
|
|
56
|
+
abandoned.add(token);
|
|
57
|
+
n++;
|
|
58
|
+
try {
|
|
59
|
+
const p = decodeEnvelopeV3(a.env).payload;
|
|
60
|
+
await app.store.releaseReplay(p.fdid, p, token, { uncount: true });
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
// Malformed (it would never have been claimed), or the store is unwritable: the claim's
|
|
64
|
+
// holder is gone in a moment either way, and the next attempt takes it over.
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return n;
|
|
68
|
+
}
|
|
69
|
+
function pidAlive(pid) {
|
|
70
|
+
try {
|
|
71
|
+
process.kill(pid, 0);
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
catch (e) {
|
|
75
|
+
return e.code === "EPERM";
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/** Liveness only on this machine (never age: pre-release re-review M-1); another host's claim: the TTL decides. */
|
|
79
|
+
function claimAlive(c) {
|
|
80
|
+
if (c.host !== HOST)
|
|
81
|
+
return undefined;
|
|
82
|
+
if (c.pid === process.pid && (c.started === undefined || Math.abs(c.started - STARTED) < START_SLACK_MS))
|
|
83
|
+
return inFlight.has(c.token);
|
|
84
|
+
if (!pidAlive(c.pid))
|
|
85
|
+
return false;
|
|
86
|
+
// review L-C: a pid can be reused; it is the holder only if it started when the holder did.
|
|
87
|
+
if (c.started === undefined)
|
|
88
|
+
return true;
|
|
89
|
+
const started = processStartMs(c.pid);
|
|
90
|
+
return started === undefined ? true : Math.abs(started - c.started) < START_SLACK_MS;
|
|
91
|
+
}
|
|
92
|
+
export async function fromOf(app, r) {
|
|
93
|
+
return { userId: r.envelope.from, did: r.envelope.fdid, address: await app.addressOf(r.envelope.from), self: r.envelope.from === app.userId };
|
|
94
|
+
}
|
|
95
|
+
/** Builds an event; `id` is derived from the envelope so every process logs it once. */
|
|
96
|
+
export function makeEvent(env, kind, at, data, extra = {}, salt = "") {
|
|
97
|
+
return { id: `${kind}:${jwsHash(env + salt).slice(0, 22)}`, at, kind, data, ...extra };
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* How long one envelope may take before the command goes on without it (review M-A): the attempt
|
|
101
|
+
* keeps running (its claim stays alive) and counts toward the envelope's attempt limit, but a
|
|
102
|
+
* short command's drain isn't spent on it. P2P_ENVELOPE_TIMEOUT_MS overrides it.
|
|
103
|
+
*/
|
|
104
|
+
export const ENVELOPE_TIMEOUT_MS = 8000;
|
|
105
|
+
export async function processEnvelope(app, env, relay) {
|
|
106
|
+
const token = `${PROCESS_TOKEN}.${++attempts}`;
|
|
107
|
+
inFlight.add(token);
|
|
108
|
+
running.set(token, { app, env });
|
|
109
|
+
const work = processClaimed(app, env, relay, token).finally(() => {
|
|
110
|
+
inFlight.delete(token);
|
|
111
|
+
running.delete(token);
|
|
112
|
+
abandoned.delete(token);
|
|
113
|
+
});
|
|
114
|
+
const limit = Number(app.env.P2P_ENVELOPE_TIMEOUT_MS ?? "") || ENVELOPE_TIMEOUT_MS;
|
|
115
|
+
let timer;
|
|
116
|
+
const late = new Promise((r) => {
|
|
117
|
+
timer = setTimeout(() => r("timeout"), limit);
|
|
118
|
+
timer.unref();
|
|
119
|
+
});
|
|
120
|
+
try {
|
|
121
|
+
const v = await Promise.race([work, late]);
|
|
122
|
+
if (v !== "timeout")
|
|
123
|
+
return v;
|
|
124
|
+
// Not acked: the relay redelivers it, and the attempt still running may finish it first.
|
|
125
|
+
work.catch(() => undefined);
|
|
126
|
+
app.io.err(`handling an envelope from ${sanitizePeerText(relay, 200)} is taking over ${Math.round(limit / 1000)}s; going on without it`);
|
|
127
|
+
return "keep";
|
|
128
|
+
}
|
|
129
|
+
finally {
|
|
130
|
+
clearTimeout(timer);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/** Envelopes this device dropped unapplied, for `p2p doctor` (a count only, never content). */
|
|
134
|
+
async function noteGaveUp(app) {
|
|
135
|
+
await app.state.update("inbound", (cur = { gaveUp: 0 }) => ({ next: { gaveUp: cur.gaveUp + 1, at: Date.now() }, result: undefined })).catch(() => undefined);
|
|
136
|
+
}
|
|
137
|
+
async function processClaimed(app, env, relay, token) {
|
|
138
|
+
const r = await verifyIncomingV3(env, {
|
|
139
|
+
self: { userId: app.userId, did: app.did, agreementSecretKey: app.device.agreementSecretKey, cert: app.keys.cert },
|
|
140
|
+
store: app.store,
|
|
141
|
+
resolveIdentity: (u) => app.discover(u, [relay]),
|
|
142
|
+
onWarning: (m) => app.io.err(`warning: ${sanitizePeerText(m, 300)}`),
|
|
143
|
+
replayClaim: { claimant: { token, host: HOST, pid: process.pid, started: STARTED }, isAlive: claimAlive, retryable: isTransient },
|
|
144
|
+
});
|
|
145
|
+
// Another process (or another relay's copy here) is applying it right now: not acked, so the
|
|
146
|
+
// relay keeps it until that one acks (or redelivers it, if that one never finishes).
|
|
147
|
+
if (!r.ok && r.reason === "replay-in-progress")
|
|
148
|
+
return "keep";
|
|
149
|
+
if (!r.ok && r.reason === "gave-up") {
|
|
150
|
+
app.io.err(`gave-up: an envelope from ${sanitizePeerText(relay, 200)} failed every attempt; dropped unapplied (see \`p2p doctor\`)`);
|
|
151
|
+
await noteGaveUp(app);
|
|
152
|
+
return "ack";
|
|
153
|
+
}
|
|
154
|
+
if (!r.ok && r.reason === "seq-too-old") {
|
|
155
|
+
// Below the replay window: a duplicate, or an envelope that arrived after more than 512 later
|
|
156
|
+
// ones from the same session. Either way it can't be told apart from a replay, so it is acked
|
|
157
|
+
// (it would only come back), but counted for `p2p doctor` (review M1): never silently.
|
|
158
|
+
await app.state.update("inbound", (cur = { gaveUp: 0 }) => ({ next: { ...cur, tooOld: (cur.tooOld ?? 0) + 1, at: Date.now() }, result: undefined })).catch(() => undefined);
|
|
159
|
+
return "ack";
|
|
160
|
+
}
|
|
161
|
+
if (!r.ok) {
|
|
162
|
+
// Core messages can quote raw envelope fields: one line, sanitized (review M2).
|
|
163
|
+
if (!DUPLICATE.has(r.reason))
|
|
164
|
+
app.io.err(`rejected an envelope from ${sanitizePeerText(relay, 200)}: ${sanitizePeerText(r.reason, 64)}: ${sanitizePeerText(r.message, 300)}`);
|
|
165
|
+
return "ack";
|
|
166
|
+
}
|
|
167
|
+
// Abandoned while it was being verified (and claimed): give the claim back, uncounted.
|
|
168
|
+
if (abandoned.has(token)) {
|
|
169
|
+
await r.releaseReplay?.({ uncount: true }).catch(() => undefined);
|
|
170
|
+
return "keep";
|
|
171
|
+
}
|
|
172
|
+
try {
|
|
173
|
+
const events = await dispatch(app, env, r, relay);
|
|
174
|
+
if (abandoned.has(token)) {
|
|
175
|
+
await r.releaseReplay?.({ uncount: true }).catch(() => undefined);
|
|
176
|
+
return "keep";
|
|
177
|
+
}
|
|
178
|
+
const added = await app.state.addEvents(events);
|
|
179
|
+
// Message history (history.ts): the messages just logged, once each (a redelivery adds none).
|
|
180
|
+
await recordEvents(app, added);
|
|
181
|
+
}
|
|
182
|
+
catch (e) {
|
|
183
|
+
const what = `applying ${sanitizePeerText(r.envelope.kind, 40)} from ${r.envelope.from} failed: ${sanitizePeerText(e.message, 300)}`;
|
|
184
|
+
if (!(e instanceof P2PError) && isTransient(e)) {
|
|
185
|
+
// This command closing, a lock timeout, a full disk: give the claim up unrecorded and don't
|
|
186
|
+
// ack, so it is applied in full on a redelivery (review L-2), within the attempt limit.
|
|
187
|
+
app.io.err(`${what} (attempt ${r.attempt ?? 1}; kept for a retry)`);
|
|
188
|
+
// Cut off by this process exiting: not an attempt that failed (pre-release review M-A).
|
|
189
|
+
await r.releaseReplay?.({ uncount: abandoned.has(token) }).catch(() => undefined);
|
|
190
|
+
return "keep";
|
|
191
|
+
}
|
|
192
|
+
// A protocol rejection, or anything else (a bug, the network): final, as retrying would only
|
|
193
|
+
// repeat it, and its side effects.
|
|
194
|
+
app.io.err(what);
|
|
195
|
+
if (!(e instanceof P2PError))
|
|
196
|
+
await noteGaveUp(app);
|
|
197
|
+
}
|
|
198
|
+
// Abandoned after its effects were written: not recorded; the redelivery applies it again
|
|
199
|
+
// (events deduplicated by id), as after a crash here.
|
|
200
|
+
if (abandoned.has(token)) {
|
|
201
|
+
await r.releaseReplay?.({ uncount: true }).catch(() => undefined);
|
|
202
|
+
return "keep";
|
|
203
|
+
}
|
|
204
|
+
// Applied (its effects and events written), or failed for good: only now is it a replay.
|
|
205
|
+
const lost = await r.commitReplay?.();
|
|
206
|
+
// Another host's attempt recorded it first (its claim was taken over after the TTL): this one
|
|
207
|
+
// was a duplicate. Its events were deduplicated by id; say so, and go on.
|
|
208
|
+
if (lost)
|
|
209
|
+
app.io.err(`an envelope from ${r.envelope.from} was also handled elsewhere meanwhile (${sanitizePeerText(lost.reason, 64)})`);
|
|
210
|
+
return "ack";
|
|
211
|
+
}
|
|
212
|
+
async function dispatch(app, env, r, relay) {
|
|
213
|
+
switch (r.rule) {
|
|
214
|
+
case "contact":
|
|
215
|
+
return onContact(app, env, r);
|
|
216
|
+
case "self-sync": {
|
|
217
|
+
// review L3: a device revoked moments ago could still sync. Refresh our own document
|
|
218
|
+
// (rate-limited by P2P_DOC_REFRESH_SEC) and require the sender to still be listed.
|
|
219
|
+
await app.refreshIdentity(app.userId, app.relays, { quiet: true });
|
|
220
|
+
const own = await app.view(app.userId);
|
|
221
|
+
if (!own?.devices.includes(r.envelope.fdid)) {
|
|
222
|
+
app.io.err(`ignored a sync from ${r.envelope.fdid}: no longer one of your devices`);
|
|
223
|
+
return [];
|
|
224
|
+
}
|
|
225
|
+
await importSync(app, r.plaintext.body, { fdid: r.envelope.fdid });
|
|
226
|
+
// §2.8: a rotation of our identity this device didn't publish is raised loudly.
|
|
227
|
+
await alertForeignRotations(app);
|
|
228
|
+
return [];
|
|
229
|
+
}
|
|
230
|
+
case "unfriend":
|
|
231
|
+
return onUnfriendKind(app, env, r);
|
|
232
|
+
case "receipt":
|
|
233
|
+
return onReceipt(app, env, r);
|
|
234
|
+
case "unknown-kind":
|
|
235
|
+
return onUnknownKind(app, env, r);
|
|
236
|
+
case "circle":
|
|
237
|
+
// "circle" names both the circle kind's rule and an exchange rule (§4.2: a circle-mate writing
|
|
238
|
+
// into a session shared with the circle). Only the kind tells them apart: a message routed to
|
|
239
|
+
// the circle handler failed there and was dropped (found by the doctor tests).
|
|
240
|
+
return r.plaintext.kind === "circle" ? onCircleEnvelope(app, env, r, relay) : onSessionEnvelope(app, env, r);
|
|
241
|
+
default:
|
|
242
|
+
return onSessionEnvelope(app, env, r);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
/** Installs the handler and returns the app, for commands that connect. */
|
|
246
|
+
export function receiving(app) {
|
|
247
|
+
app.onEnvelope = (env, relay) => processEnvelope(app, env, relay);
|
|
248
|
+
app.afterDrain = async () => {
|
|
249
|
+
// §2.8 (pre-release review H2): nothing here ever follows a rotation of our own identity; that is the
|
|
250
|
+
// user's own `p2p identity follow`. A rotation this device didn't publish is raised, and rotated
|
|
251
|
+
// members whose old identity's final version is now out are swapped in.
|
|
252
|
+
await alertForeignRotations(app);
|
|
253
|
+
await retryRotationJoins(app);
|
|
254
|
+
// Re-review N6: a want the first device in line didn't answer in time, answered here.
|
|
255
|
+
await answerDeferredWants(app).catch((e) => app.io.err(`p2p: ${e.message}`));
|
|
256
|
+
return finishCodeJoins(app);
|
|
257
|
+
};
|
|
258
|
+
app.onIdentityRotated = () => alertForeignRotations(app);
|
|
259
|
+
app.onAbandon = () => abandonInFlight(app);
|
|
260
|
+
return app;
|
|
261
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from "./relay-client.js";
|
|
2
|
+
export * from "./config.js";
|
|
3
|
+
export * from "./errors.js";
|
|
4
|
+
export { App, type Io, type AppOptions, type Delivery } from "./app.js";
|
|
5
|
+
export { buildProgram, main, realOps, VERSION } from "./cli.js";
|
|
6
|
+
export { processEnvelope, receiving } from "./inbound.js";
|
|
7
|
+
export * from "./extensions.js";
|
|
8
|
+
export { sendExtension, MAX_EXT_PLAINTEXT_BYTES, type SendExtOpts } from "./sessions.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Copyright 2026 Ix Infrastructure Inc.
|
|
2
|
+
//
|
|
3
|
+
// @peer2peer/cli: the standalone `p2p` CLI for Peer2Peer v3. The binary is
|
|
4
|
+
// `p2p` (./bin.ts); this entry point exposes the pieces for embedding and
|
|
5
|
+
// tests: the relay client, the App context and the command functions.
|
|
6
|
+
export * from "./relay-client.js";
|
|
7
|
+
export * from "./config.js";
|
|
8
|
+
export * from "./errors.js";
|
|
9
|
+
export { App } from "./app.js";
|
|
10
|
+
export { buildProgram, main, realOps, VERSION } from "./cli.js";
|
|
11
|
+
export { processEnvelope, receiving } from "./inbound.js";
|
|
12
|
+
// The extension seam (extensions.ts): the contract an @peer2peer/ext-* package implements.
|
|
13
|
+
export * from "./extensions.js";
|
|
14
|
+
export { sendExtension, MAX_EXT_PLAINTEXT_BYTES } from "./sessions.js";
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { App } from "./app.js";
|
|
2
|
+
import type { Env } from "./config.js";
|
|
3
|
+
import { type GraphTarget } from "./projects.js";
|
|
4
|
+
/** Where to get Ix (and how p2p says it): never installed for the person. */
|
|
5
|
+
export declare const IX_INSTALL_URL = "https://github.com/ix-infrastructure/Ix";
|
|
6
|
+
/**
|
|
7
|
+
* `name` resolved to an absolute path through PATH's absolute entries (the same rule as ext-ix's
|
|
8
|
+
* which.ts: on Windows the current directory is searched before PATH, so a cloned repository's
|
|
9
|
+
* `ix.exe` would otherwise run). An absolute name is used as given when it runs.
|
|
10
|
+
*/
|
|
11
|
+
export declare function whichAbsolute(name: string, env?: Record<string, string | undefined>, platform?: NodeJS.Platform): string | undefined;
|
|
12
|
+
/** The Ix CLI: IX_BIN when it is an absolute path that runs (or a bare name found on PATH), else `ix` on PATH. */
|
|
13
|
+
export declare function ixBinary(env: Env): string | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* The environment `ix` runs with: IX_CHILD_ALLOW from `env`, then IX_ENDPOINT from the target
|
|
16
|
+
* (never the caller's), no background `ix map` from a reader (Ix Pro routes a map to the cloud
|
|
17
|
+
* whenever an org session is active), no update check. Names compare case-insensitively, as on
|
|
18
|
+
* Windows. The same rule as ext-ix's ixEnv (ix.ts).
|
|
19
|
+
*/
|
|
20
|
+
export declare function ixChildEnv(env: Record<string, string | undefined>, target: Pick<GraphTarget, "endpoint">): NodeJS.ProcessEnv;
|
|
21
|
+
/** PATH with its relative and empty entries dropped (they mean the current directory: a repository's own `node`). */
|
|
22
|
+
export declare function absolutePath(v: string, platform?: NodeJS.Platform): string;
|
|
23
|
+
export interface IxRun {
|
|
24
|
+
code: number;
|
|
25
|
+
stdout: string;
|
|
26
|
+
stderr: string;
|
|
27
|
+
timedOut?: true;
|
|
28
|
+
}
|
|
29
|
+
/** One bounded `ix` call (argv, no shell, SIGKILL on timeout, output capped). Read-only commands only. */
|
|
30
|
+
export declare function runIx(bin: string, argv: readonly string[], o: {
|
|
31
|
+
env: Record<string, string | undefined>;
|
|
32
|
+
target: Pick<GraphTarget, "endpoint">;
|
|
33
|
+
cwd?: string;
|
|
34
|
+
timeoutMs: number;
|
|
35
|
+
}): Promise<IxRun>;
|
|
36
|
+
/** `ix --version`'s first line, sanitized; undefined when it fails. */
|
|
37
|
+
export declare function ixVersion(bin: string, env: Env, target: Pick<GraphTarget, "endpoint">, timeoutMs: number): Promise<string | undefined>;
|
|
38
|
+
/** Whether @peer2peer/ext-ix is where this p2p loads extensions from (resolved, never imported). */
|
|
39
|
+
export declare function extIxInstalled(env: Env): boolean;
|
|
40
|
+
/** What `p2p setup` did about Ix (SetupResult.ix). */
|
|
41
|
+
export interface SetupIx {
|
|
42
|
+
/** The Ix CLI was found (its absolute path is local only, never sent anywhere). */
|
|
43
|
+
found: boolean;
|
|
44
|
+
/** The `ix` extension is enabled now (by this setup, or already). */
|
|
45
|
+
enabled: boolean;
|
|
46
|
+
/** This setup turned it on (a new install, Ix present, ext-ix installed, the person running it). */
|
|
47
|
+
enabledNow?: true;
|
|
48
|
+
/** What the person can do, when it isn't on. */
|
|
49
|
+
hint?: string;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Ix on by default, for `p2p setup`. A new install (`created`) gets the `ix` extension enabled when
|
|
53
|
+
* the Ix CLI and ext-ix are both there and the person agrees: their front end's token (the Claude
|
|
54
|
+
* mod's typed `/peers setup`), or, outside any agent's session (its environment or a Claude Code or
|
|
55
|
+
* Codex ancestor), their "yes" typed on the controlling terminal, as for every other person-only
|
|
56
|
+
* action (projects.ts). Otherwise nothing changes: an existing install is never switched silently
|
|
57
|
+
* (it gets the one-time notice, ixNotice), an agent never enables an extension, and
|
|
58
|
+
* P2P_EXTENSIONS, when set, decides alone.
|
|
59
|
+
*/
|
|
60
|
+
export declare function setupIx(app: App, created: boolean): Promise<SetupIx>;
|
|
61
|
+
/**
|
|
62
|
+
* The one-time notice for an existing install (shown once per device, to the person at a terminal):
|
|
63
|
+
* new installs turn the Ix extension on when Ix is present; this one wasn't changed. Undefined when
|
|
64
|
+
* it was shown already, Ix isn't installed, or the extension is on (or P2P_EXTENSIONS decides).
|
|
65
|
+
*/
|
|
66
|
+
export declare function ixNotice(app: App): Promise<string | undefined>;
|
|
67
|
+
/**
|
|
68
|
+
* Which `ix note` / `ix session` this Ix has, from `ix --help` (graph memory, graph-memory.ts):
|
|
69
|
+
* "oss" (the local backend: Ix after 0.13.0, ix-infrastructure/Ix#916, with Ix Pro not loaded: its
|
|
70
|
+
* help then lists them under "Memory:"), "pro" (Ix Pro loaded: its "Pro:" section; its note and
|
|
71
|
+
* session go to the cloud org), "none" (neither). The same reading as ext-ix's parseMemorySupport
|
|
72
|
+
* (ix.ts), which decides; this one only reports (`p2p doctor`).
|
|
73
|
+
*/
|
|
74
|
+
export declare function parseIxMemorySupport(help: string): "oss" | "pro" | "none";
|
|
75
|
+
/** `ix --help` read as parseIxMemorySupport; undefined when it doesn't answer. */
|
|
76
|
+
export declare function ixMemorySupport(bin: string, env: Env, target: Pick<GraphTarget, "endpoint">, timeoutMs: number): Promise<"oss" | "pro" | "none" | undefined>;
|
package/dist/ix-local.js
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
// Copyright 2026 Ix Infrastructure Inc.
|
|
2
|
+
//
|
|
3
|
+
// Ix on by default (epic #41 piece 1): finding the local Ix CLI, asking it about this machine
|
|
4
|
+
// (version, backend, whether a checkout is mapped), and turning the `ix` extension on for a new
|
|
5
|
+
// install when Ix is there. The CLI never imports an extension (scripts/check-boundaries.mjs), so
|
|
6
|
+
// it runs `ix` itself here, the same way ext-ix does: by absolute path from PATH's absolute
|
|
7
|
+
// entries (never a binary planted in the repository), by argv with no shell, bounded, with an
|
|
8
|
+
// allowlisted environment pinned to the graph target (projects.ts), never the caller's IX_*
|
|
9
|
+
// cloud settings. Nothing is ever installed: the person is told how.
|
|
10
|
+
import { execFile } from "node:child_process";
|
|
11
|
+
import { accessSync, constants, statSync } from "node:fs";
|
|
12
|
+
import { delimiter, isAbsolute, join } from "node:path";
|
|
13
|
+
import { sanitizePeerText } from "@peer2peer/core/v3";
|
|
14
|
+
import { enabledNamespaces, extensionDirs, extensionPackageName, resolveIn, setExtensionEnabled } from "./extensions.js";
|
|
15
|
+
import { personHere } from "./projects.js";
|
|
16
|
+
/** Where to get Ix (and how p2p says it): never installed for the person. */
|
|
17
|
+
export const IX_INSTALL_URL = "https://github.com/ix-infrastructure/Ix";
|
|
18
|
+
/**
|
|
19
|
+
* `name` resolved to an absolute path through PATH's absolute entries (the same rule as ext-ix's
|
|
20
|
+
* which.ts: on Windows the current directory is searched before PATH, so a cloned repository's
|
|
21
|
+
* `ix.exe` would otherwise run). An absolute name is used as given when it runs.
|
|
22
|
+
*/
|
|
23
|
+
export function whichAbsolute(name, env = process.env, platform = process.platform) {
|
|
24
|
+
const runnable = (p) => {
|
|
25
|
+
try {
|
|
26
|
+
if (!statSync(p).isFile())
|
|
27
|
+
return false;
|
|
28
|
+
if (platform !== "win32")
|
|
29
|
+
accessSync(p, constants.X_OK);
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
if (isAbsolute(name))
|
|
37
|
+
return runnable(name) ? name : undefined;
|
|
38
|
+
if (/[\\/]/.test(name))
|
|
39
|
+
return undefined;
|
|
40
|
+
const win = platform === "win32";
|
|
41
|
+
const sep = win ? ";" : delimiter;
|
|
42
|
+
const path = env.PATH ?? env.Path ?? "";
|
|
43
|
+
const exts = win ? (env.PATHEXT ?? ".COM;.EXE;.BAT;.CMD").split(";").filter(Boolean) : [""];
|
|
44
|
+
const hasExt = win && exts.some((e) => name.toLowerCase().endsWith(e.toLowerCase()));
|
|
45
|
+
for (const dir of path.split(sep)) {
|
|
46
|
+
const d = dir.trim().replace(/^"|"$/g, "");
|
|
47
|
+
if (d === "" || !isAbsolute(d))
|
|
48
|
+
continue;
|
|
49
|
+
for (const ext of hasExt ? [""] : exts) {
|
|
50
|
+
const p = join(d, name + ext);
|
|
51
|
+
if (runnable(p))
|
|
52
|
+
return p;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
/** The Ix CLI: IX_BIN when it is an absolute path that runs (or a bare name found on PATH), else `ix` on PATH. */
|
|
58
|
+
export function ixBinary(env) {
|
|
59
|
+
const name = env.IX_BIN?.trim() || "ix";
|
|
60
|
+
return whichAbsolute(name, env);
|
|
61
|
+
}
|
|
62
|
+
/** What an `ix` child may see of the environment (besides the pinned IX_ENDPOINT): no IX_* cloud setting, no P2P_*, no loader or proxy variables. */
|
|
63
|
+
const IX_CHILD_ALLOW = ["PATH", "PATHEXT", "HOME", "USERPROFILE", "HOMEDRIVE", "HOMEPATH", "APPDATA", "LOCALAPPDATA", "SYSTEMROOT", "WINDIR", "TMPDIR", "TEMP", "TMP", "LANG", "LC_ALL", "LC_CTYPE", "LC_MESSAGES", "TZ", "TERM", "NO_COLOR", "IX_HOME", "IX_TOKEN"];
|
|
64
|
+
/**
|
|
65
|
+
* The environment `ix` runs with: IX_CHILD_ALLOW from `env`, then IX_ENDPOINT from the target
|
|
66
|
+
* (never the caller's), no background `ix map` from a reader (Ix Pro routes a map to the cloud
|
|
67
|
+
* whenever an org session is active), no update check. Names compare case-insensitively, as on
|
|
68
|
+
* Windows. The same rule as ext-ix's ixEnv (ix.ts).
|
|
69
|
+
*/
|
|
70
|
+
export function ixChildEnv(env, target) {
|
|
71
|
+
const allow = new Set(IX_CHILD_ALLOW);
|
|
72
|
+
const out = {};
|
|
73
|
+
for (const [k, v] of Object.entries(env))
|
|
74
|
+
if (typeof v === "string" && allow.has(k.toUpperCase()))
|
|
75
|
+
out[k] = k.toUpperCase() === "PATH" ? absolutePath(v) : v;
|
|
76
|
+
out.IX_ENDPOINT = target.endpoint;
|
|
77
|
+
out.IX_MAP_BACKGROUND_REFRESH = "0";
|
|
78
|
+
out.IX_NO_UPDATE_CHECK = "1";
|
|
79
|
+
return out;
|
|
80
|
+
}
|
|
81
|
+
/** PATH with its relative and empty entries dropped (they mean the current directory: a repository's own `node`). */
|
|
82
|
+
export function absolutePath(v, platform = process.platform) {
|
|
83
|
+
const sep = platform === "win32" ? ";" : delimiter;
|
|
84
|
+
return v
|
|
85
|
+
.split(sep)
|
|
86
|
+
.filter((d) => {
|
|
87
|
+
const x = d.trim().replace(/^"|"$/g, "");
|
|
88
|
+
return x !== "" && (platform === "win32" ? /^(?:[A-Za-z]:[\\/]|\\\\)/.test(x) : isAbsolute(x));
|
|
89
|
+
})
|
|
90
|
+
.join(sep);
|
|
91
|
+
}
|
|
92
|
+
/** One bounded `ix` call (argv, no shell, SIGKILL on timeout, output capped). Read-only commands only. */
|
|
93
|
+
export function runIx(bin, argv, o) {
|
|
94
|
+
return new Promise((resolve) => {
|
|
95
|
+
execFile(bin, [...argv], { cwd: o.cwd, env: ixChildEnv(o.env, o.target), timeout: o.timeoutMs, killSignal: "SIGKILL", maxBuffer: 1 << 20, windowsHide: true }, (err, stdout, stderr) => {
|
|
96
|
+
const e = err;
|
|
97
|
+
resolve({ code: e ? (typeof e.code === "number" ? e.code : 1) : 0, stdout: String(stdout ?? ""), stderr: String(stderr ?? ""), ...(e?.killed ? { timedOut: true } : {}) });
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
/** `ix --version`'s first line, sanitized; undefined when it fails. */
|
|
102
|
+
export async function ixVersion(bin, env, target, timeoutMs) {
|
|
103
|
+
const r = await runIx(bin, ["--version"], { env, target, timeoutMs });
|
|
104
|
+
if (r.code !== 0)
|
|
105
|
+
return undefined;
|
|
106
|
+
const v = sanitizePeerText(r.stdout.split("\n")[0]?.trim() ?? "", 64);
|
|
107
|
+
return v || undefined;
|
|
108
|
+
}
|
|
109
|
+
/** Whether @peer2peer/ext-ix is where this p2p loads extensions from (resolved, never imported). */
|
|
110
|
+
export function extIxInstalled(env) {
|
|
111
|
+
for (const dir of extensionDirs(env, import.meta.url)) {
|
|
112
|
+
try {
|
|
113
|
+
if (resolveIn(dir, extensionPackageName("ix")))
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
// a malformed copy is not an installed one
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
/** P2P_EXTENSIONS decides which extensions load (anything but unset or `config`, which defers to config.json). */
|
|
123
|
+
const envDecides = (env) => env.P2P_EXTENSIONS !== undefined && env.P2P_EXTENSIONS.trim() !== "config";
|
|
124
|
+
const INSTALL_IX = `Ix (the local code graph peers' agents point into) isn't installed: get it from ${IX_INSTALL_URL}, then run \`p2p extensions enable ix\` (Peer2Peer works without it)`;
|
|
125
|
+
const INSTALL_EXT = "Ix is installed, but the Ix extension isn't: `npm i -g @peer2peer/ext-ix`, then `p2p extensions enable ix` (the Claude Code mod ships it: `/peers setup` there turns it on)";
|
|
126
|
+
/**
|
|
127
|
+
* Ix on by default, for `p2p setup`. A new install (`created`) gets the `ix` extension enabled when
|
|
128
|
+
* the Ix CLI and ext-ix are both there and the person agrees: their front end's token (the Claude
|
|
129
|
+
* mod's typed `/peers setup`), or, outside any agent's session (its environment or a Claude Code or
|
|
130
|
+
* Codex ancestor), their "yes" typed on the controlling terminal, as for every other person-only
|
|
131
|
+
* action (projects.ts). Otherwise nothing changes: an existing install is never switched silently
|
|
132
|
+
* (it gets the one-time notice, ixNotice), an agent never enables an extension, and
|
|
133
|
+
* P2P_EXTENSIONS, when set, decides alone.
|
|
134
|
+
*/
|
|
135
|
+
export async function setupIx(app, created) {
|
|
136
|
+
const found = ixBinary(app.env) !== undefined;
|
|
137
|
+
const { enabled } = enabledNamespaces(app.env, app.file);
|
|
138
|
+
if (enabled.includes("ix"))
|
|
139
|
+
return { found, enabled: true };
|
|
140
|
+
if (!found)
|
|
141
|
+
return { found, enabled: false, hint: INSTALL_IX };
|
|
142
|
+
if (!extIxInstalled(app.env))
|
|
143
|
+
return { found, enabled: false, hint: INSTALL_EXT };
|
|
144
|
+
const chose = Array.isArray(app.file.extensions);
|
|
145
|
+
if (!created || chose || envDecides(app.env) || !personHere(app)) {
|
|
146
|
+
return { found, enabled: false, hint: "Ix is installed: `p2p extensions enable ix` lets your sessions share Ix context (notes, subgraph references, blast radius, footprints)" };
|
|
147
|
+
}
|
|
148
|
+
if (app.front !== true) {
|
|
149
|
+
const yes = app.confirmTty ? await app.confirmTty("Ix is installed. Turn on the Ix extension, so your sessions can share Ix context with the people you share a project with? (Ix calls stay on this machine.)").catch(() => false) : false;
|
|
150
|
+
if (!yes)
|
|
151
|
+
return { found, enabled: false, hint: "Ix is installed: `p2p extensions enable ix` lets your sessions share Ix context (notes, subgraph references, blast radius, footprints)" };
|
|
152
|
+
}
|
|
153
|
+
setExtensionEnabled(app.home, "ix", true);
|
|
154
|
+
return { found, enabled: true, enabledNow: true };
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* The one-time notice for an existing install (shown once per device, to the person at a terminal):
|
|
158
|
+
* new installs turn the Ix extension on when Ix is present; this one wasn't changed. Undefined when
|
|
159
|
+
* it was shown already, Ix isn't installed, or the extension is on (or P2P_EXTENSIONS decides).
|
|
160
|
+
*/
|
|
161
|
+
export async function ixNotice(app) {
|
|
162
|
+
if (envDecides(app.env))
|
|
163
|
+
return undefined;
|
|
164
|
+
if (enabledNamespaces(app.env, app.file).enabled.includes("ix"))
|
|
165
|
+
return undefined;
|
|
166
|
+
if ((await app.state.get("ixNotice"))?.at !== undefined)
|
|
167
|
+
return undefined;
|
|
168
|
+
if (!ixBinary(app.env))
|
|
169
|
+
return undefined;
|
|
170
|
+
const shown = await app.state.update("ixNotice", (cur) => (cur?.at !== undefined ? { result: false } : { next: { at: Date.now() }, result: true }));
|
|
171
|
+
if (!shown)
|
|
172
|
+
return undefined;
|
|
173
|
+
const how = extIxInstalled(app.env) ? "`p2p extensions enable ix`" : "`npm i -g @peer2peer/ext-ix`, then `p2p extensions enable ix`";
|
|
174
|
+
return `Ix: new installs now turn the Ix extension on when Ix is installed, so sessions can share Ix context. Yours wasn't changed; to turn it on, run ${how}. Ix calls stay on this machine (\`p2p doctor\` shows each project's graph: local only)`;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Which `ix note` / `ix session` this Ix has, from `ix --help` (graph memory, graph-memory.ts):
|
|
178
|
+
* "oss" (the local backend: Ix after 0.13.0, ix-infrastructure/Ix#916, with Ix Pro not loaded: its
|
|
179
|
+
* help then lists them under "Memory:"), "pro" (Ix Pro loaded: its "Pro:" section; its note and
|
|
180
|
+
* session go to the cloud org), "none" (neither). The same reading as ext-ix's parseMemorySupport
|
|
181
|
+
* (ix.ts), which decides; this one only reports (`p2p doctor`).
|
|
182
|
+
*/
|
|
183
|
+
export function parseIxMemorySupport(help) {
|
|
184
|
+
const lines = help.replace(/\u001b\[[0-9;]*m/g, "").split(/\r?\n/);
|
|
185
|
+
if (lines.some((l) => /^Pro:\s*$/.test(l)))
|
|
186
|
+
return "pro";
|
|
187
|
+
const at = lines.findIndex((l) => /^Memory:\s*$/.test(l));
|
|
188
|
+
if (at < 0)
|
|
189
|
+
return "none";
|
|
190
|
+
const section = [];
|
|
191
|
+
for (const l of lines.slice(at + 1)) {
|
|
192
|
+
if (!/^\s+\S/.test(l))
|
|
193
|
+
break;
|
|
194
|
+
section.push(l);
|
|
195
|
+
}
|
|
196
|
+
const has = (name) => section.some((l) => new RegExp(`^\\s+${name}\\s`).test(l));
|
|
197
|
+
return has("note") && has("session") ? "oss" : "none";
|
|
198
|
+
}
|
|
199
|
+
/** `ix --help` read as parseIxMemorySupport; undefined when it doesn't answer. */
|
|
200
|
+
export async function ixMemorySupport(bin, env, target, timeoutMs) {
|
|
201
|
+
const r = await runIx(bin, ["--help"], { env, target, timeoutMs });
|
|
202
|
+
return r.code === 0 ? parseIxMemorySupport(r.stdout) : undefined;
|
|
203
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { type AnswerPart } from "@peer2peer/core/v3";
|
|
2
|
+
import type { App } from "./app.js";
|
|
3
|
+
import type { AnswerPartsRec } from "./state.js";
|
|
4
|
+
export interface JoinAnswerDoc {
|
|
5
|
+
cid: string;
|
|
6
|
+
from: string;
|
|
7
|
+
versions: string[];
|
|
8
|
+
joins: string[];
|
|
9
|
+
parts: Record<string, AnswerPartsRec>;
|
|
10
|
+
bytes: number;
|
|
11
|
+
}
|
|
12
|
+
/** Distinct versions kept per `ver` (a sibling, at most: more is someone else's noise). */
|
|
13
|
+
export declare const MAX_VERSIONS_PER_VER = 2;
|
|
14
|
+
/** Versions above the offered head kept per confirmer: a link can be older than the circle's head. */
|
|
15
|
+
export declare const MAX_NEWER_VERSIONS = 256;
|
|
16
|
+
/**
|
|
17
|
+
* Answers a member sends one asker per rolling hour, and so the new answers (part ids) a joiner
|
|
18
|
+
* takes from one confirmer per rolling hour: one limit on both sides (review N-D; the joiner took 4
|
|
19
|
+
* while a member may send 10, so a legitimate fifth answer was dropped). Parts of answers already
|
|
20
|
+
* tracked always count.
|
|
21
|
+
*/
|
|
22
|
+
export declare const ANSWERS_PER_HOUR = 10;
|
|
23
|
+
export declare const MAX_NEW_ANSWERS_PER_HOUR = 10;
|
|
24
|
+
/** Answers whose parts are tracked per confirmer (the newest): more than a confirmer may start in an hour. */
|
|
25
|
+
export declare const MAX_PART_RECORDS: number;
|
|
26
|
+
/**
|
|
27
|
+
* The fixed ceiling on what one confirmer's answer may take, whatever the offered head claims
|
|
28
|
+
* (re-review N1: the head comes from a link, unverified, so whoever writes the link must not set
|
|
29
|
+
* the ceiling).
|
|
30
|
+
*/
|
|
31
|
+
export declare const ANSWER_MAX_BYTES: number;
|
|
32
|
+
/** The highest `ver` kept: no chain longer than a bundle may carry is bootstrapped (MAX_CHAIN_IN_BUNDLE). */
|
|
33
|
+
export declare function maxVerFor(offeredVer: number): number;
|
|
34
|
+
/**
|
|
35
|
+
* A confirmer's byte ceiling: two full versions per `ver` up to the (clamped) offered head, plus 4
|
|
36
|
+
* statements per member of 16 KiB, never more than ANSWER_MAX_BYTES.
|
|
37
|
+
*/
|
|
38
|
+
export declare function answerByteCap(offeredVer: number, members: number): number;
|
|
39
|
+
export declare const answerDocName: (cid: string, from: string) => string;
|
|
40
|
+
/**
|
|
41
|
+
* Adds what one envelope brought to `from`'s answer for `cid`, within the bounds above. Returns
|
|
42
|
+
* whether anything was kept.
|
|
43
|
+
*/
|
|
44
|
+
export declare function addToAnswer(app: App, cid: string, from: string, bound: {
|
|
45
|
+
offeredVer: number;
|
|
46
|
+
members: number;
|
|
47
|
+
}, versions: readonly string[], joins: readonly string[], part?: AnswerPart): Promise<void>;
|
|
48
|
+
export declare function readAnswer(app: App, cid: string, from: string): Promise<JoinAnswerDoc | undefined>;
|
|
49
|
+
export declare function dropAnswers(app: App, cid: string, froms: readonly string[]): Promise<void>;
|