@eloxiv/daemon 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +6 -2
- package/dist/Acp.d.ts +8 -0
- package/dist/Acp.d.ts.map +1 -0
- package/dist/Acp.js +268 -0
- package/dist/Acp.js.map +1 -0
- package/dist/Canonical.d.ts +3 -0
- package/dist/Canonical.d.ts.map +1 -0
- package/dist/Canonical.js +12 -0
- package/dist/Canonical.js.map +1 -0
- package/dist/Client.d.ts +102 -0
- package/dist/Client.d.ts.map +1 -0
- package/dist/Client.js +482 -0
- package/dist/Client.js.map +1 -0
- package/dist/Commands.d.ts +85 -0
- package/dist/Commands.d.ts.map +1 -0
- package/dist/Commands.js +370 -0
- package/dist/Commands.js.map +1 -0
- package/dist/Config.d.ts +74 -0
- package/dist/Config.d.ts.map +1 -0
- package/dist/Config.js +126 -0
- package/dist/Config.js.map +1 -0
- package/dist/Daemon.d.ts +243 -0
- package/dist/Daemon.d.ts.map +1 -0
- package/dist/Daemon.js +965 -0
- package/dist/Daemon.js.map +1 -0
- package/dist/Harness.d.ts +133 -0
- package/dist/Harness.d.ts.map +1 -0
- package/dist/Harness.js +734 -0
- package/dist/Harness.js.map +1 -0
- package/dist/Http.d.ts +15 -0
- package/dist/Http.d.ts.map +1 -0
- package/dist/Http.js +548 -0
- package/dist/Http.js.map +1 -0
- package/dist/KernelLink.d.ts +145 -0
- package/dist/KernelLink.d.ts.map +1 -0
- package/dist/KernelLink.js +109 -0
- package/dist/KernelLink.js.map +1 -0
- package/dist/LocalAuth.d.ts +99 -0
- package/dist/LocalAuth.d.ts.map +1 -0
- package/dist/LocalAuth.js +225 -0
- package/dist/LocalAuth.js.map +1 -0
- package/dist/Main.d.ts +51 -0
- package/dist/Main.d.ts.map +1 -0
- package/dist/Main.js +219 -0
- package/dist/Main.js.map +1 -0
- package/dist/PersonSession.d.ts +49 -0
- package/dist/PersonSession.d.ts.map +1 -0
- package/dist/PersonSession.js +173 -0
- package/dist/PersonSession.js.map +1 -0
- package/dist/Secrets.d.ts +21 -0
- package/dist/Secrets.d.ts.map +1 -0
- package/dist/Secrets.js +204 -0
- package/dist/Secrets.js.map +1 -0
- package/dist/SecureUrl.d.ts +11 -0
- package/dist/SecureUrl.d.ts.map +1 -0
- package/dist/SecureUrl.js +23 -0
- package/dist/SecureUrl.js.map +1 -0
- package/dist/State.d.ts +77 -0
- package/dist/State.d.ts.map +1 -0
- package/dist/State.js +38 -0
- package/dist/State.js.map +1 -0
- package/dist/Terminals.d.ts +49 -0
- package/dist/Terminals.d.ts.map +1 -0
- package/dist/Terminals.js +203 -0
- package/dist/Terminals.js.map +1 -0
- package/dist/bin-node.d.ts +2 -0
- package/dist/bin-node.d.ts.map +1 -0
- package/dist/bin-node.js +4 -0
- package/dist/bin-node.js.map +1 -0
- package/dist/bin.d.ts +2 -0
- package/dist/bin.d.ts.map +1 -0
- package/dist/bin.js +4 -0
- package/dist/bin.js.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/runtime/bun.d.ts +11 -0
- package/dist/runtime/bun.d.ts.map +1 -0
- package/dist/runtime/bun.js +20 -0
- package/dist/runtime/bun.js.map +1 -0
- package/dist/runtime/node.d.ts +11 -0
- package/dist/runtime/node.d.ts.map +1 -0
- package/dist/runtime/node.js +21 -0
- package/dist/runtime/node.js.map +1 -0
- package/dist/testing.d.ts +25 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +27 -0
- package/dist/testing.js.map +1 -0
- package/fixtures/claude-code-env-vars.txt +409 -0
- package/fixtures/fake-acp-agent.mjs +222 -0
- package/fixtures/fake-harness.mjs +299 -0
- package/fixtures/recorded/claude-code.jsonl +7 -0
- package/fixtures/recorded/codex.jsonl +6 -0
- package/package.json +64 -3
- package/scripts/build-sidecar.ts +86 -0
- package/src/Acp.ts +292 -0
- package/src/Canonical.ts +9 -0
- package/src/Client.ts +590 -0
- package/src/Commands.ts +427 -0
- package/src/Config.ts +179 -0
- package/src/Daemon.ts +1092 -0
- package/src/Harness.ts +839 -0
- package/src/Http.ts +563 -0
- package/src/KernelLink.ts +197 -0
- package/src/LocalAuth.ts +301 -0
- package/src/Main.ts +281 -0
- package/src/PersonSession.ts +203 -0
- package/src/Secrets.ts +226 -0
- package/src/SecureUrl.ts +21 -0
- package/src/State.ts +109 -0
- package/src/Terminals.ts +227 -0
- package/src/bin-node.ts +4 -0
- package/src/bin.ts +4 -0
- package/src/index.ts +17 -0
- package/src/runtime/bun.ts +29 -0
- package/src/runtime/node.ts +30 -0
- package/src/testing.ts +43 -0
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Who may talk to the daemon over loopback, and how each is authenticated (per client):
|
|
3
|
+
*
|
|
4
|
+
* - the **host** (the Tauri app that started the daemon as its sidecar) holds a per-launch secret it handed the
|
|
5
|
+
* daemon in `ELOXIV_DAEMON_HOST_SECRET` and gives its webview; the person at the daemon's machine acts as the
|
|
6
|
+
* host through `eloxiv-daemon approve-client`, which reads the daemon's control secret from its private state
|
|
7
|
+
* directory;
|
|
8
|
+
* - **local clients** (the shell in a browser, a browser extension, a CLI) pair once. A client asks to pair; the
|
|
9
|
+
* host **approves** the request (the desktop app's prompt, or `eloxiv-daemon approve-client`), which shows the
|
|
10
|
+
* person a ten-character code; the client exchanges the approved pairing and its code for a token (`elxl_...`,
|
|
11
|
+
* stored by the daemon only as a SHA-256 hash). A code alone is never enough, and the code is never logged;
|
|
12
|
+
* - **harnesses** get a token for one turn, valid only for the relay and MCP while that turn runs, optionally
|
|
13
|
+
* limited to some Eloxiv actions.
|
|
14
|
+
*
|
|
15
|
+
* Against guessing: codes carry 50 bits (ten symbols of 32), each pairing allows a few wrong codes, every wrong
|
|
16
|
+
* code anywhere counts against one budget whose exhaustion locks all confirmations out for exponentially longer,
|
|
17
|
+
* and pairings started per window are capped.
|
|
18
|
+
*
|
|
19
|
+
* Every request except `GET /health` is also checked for its `Host` (a loopback name with the daemon's port,
|
|
20
|
+
* against DNS rebinding) and, when it carries one, its `Origin` (one of the trusted origins).
|
|
21
|
+
*/
|
|
22
|
+
import { DEVICE_CREDENTIAL_PREFIX } from "@eloxiv/contracts";
|
|
23
|
+
import { randomBytes, randomUUID, timingSafeEqual } from "node:crypto";
|
|
24
|
+
import { sha256Hex } from "./State.js";
|
|
25
|
+
export class LocalAuthError extends Error {
|
|
26
|
+
status;
|
|
27
|
+
tag;
|
|
28
|
+
detail;
|
|
29
|
+
constructor(status, tag, message, detail = {}) {
|
|
30
|
+
super(message);
|
|
31
|
+
this.status = status;
|
|
32
|
+
this.tag = tag;
|
|
33
|
+
this.detail = detail;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/** Crockford's base 32: no I, L, O or U, so a code read aloud or retyped is unambiguous. */
|
|
37
|
+
const ALPHABET = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
|
38
|
+
const CODE_LENGTH = 10;
|
|
39
|
+
const newCode = () => {
|
|
40
|
+
// 256 is a multiple of 32: taking each byte mod 32 is uniform.
|
|
41
|
+
const symbols = [...randomBytes(CODE_LENGTH)].map((byte) => ALPHABET[byte % 32]);
|
|
42
|
+
return `${symbols.slice(0, 5).join("")}-${symbols.slice(5).join("")}`;
|
|
43
|
+
};
|
|
44
|
+
/** What a person typed, normalised: upper case, separators dropped, look-alikes mapped as Crockford does. */
|
|
45
|
+
export const normaliseCode = (code) => code.toUpperCase().replaceAll(/[\s-]/gu, "").replaceAll("O", "0").replaceAll(/[IL]/gu, "1");
|
|
46
|
+
const token = () => `${DEVICE_CREDENTIAL_PREFIX.local}${randomBytes(32).toString("base64url")}`;
|
|
47
|
+
const same = (a, b) => {
|
|
48
|
+
const left = Buffer.from(sha256Hex(a), "hex");
|
|
49
|
+
const right = Buffer.from(sha256Hex(b), "hex");
|
|
50
|
+
return timingSafeEqual(left, right);
|
|
51
|
+
};
|
|
52
|
+
const MAX_LOCKOUT_MS = 60 * 60 * 1000;
|
|
53
|
+
export class LocalAuth {
|
|
54
|
+
#pending = new Map();
|
|
55
|
+
#harness = new Map();
|
|
56
|
+
#starts = [];
|
|
57
|
+
#failures = 0;
|
|
58
|
+
#lockedUntil = 0;
|
|
59
|
+
#port = 0;
|
|
60
|
+
state;
|
|
61
|
+
options;
|
|
62
|
+
constructor(state, options) {
|
|
63
|
+
this.state = state;
|
|
64
|
+
this.options = options;
|
|
65
|
+
}
|
|
66
|
+
/** The port the daemon listens on (Host checks need it). */
|
|
67
|
+
set port(port) {
|
|
68
|
+
this.#port = port;
|
|
69
|
+
}
|
|
70
|
+
/** Requests must name the daemon by a loopback host and its own port: a rebound DNS name is refused. */
|
|
71
|
+
checkHost(host) {
|
|
72
|
+
const allowed = [`127.0.0.1:${this.#port}`, `localhost:${this.#port}`, `[::1]:${this.#port}`];
|
|
73
|
+
if (host === undefined || !allowed.includes(host.toLowerCase())) {
|
|
74
|
+
throw new LocalAuthError(403, "ForbiddenHost", "Requests must address the daemon on loopback");
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/** A browser request from a page that is not one of the trusted origins is refused outright (not just by CORS). */
|
|
78
|
+
checkOrigin(origin) {
|
|
79
|
+
if (origin === undefined)
|
|
80
|
+
return;
|
|
81
|
+
if (!this.options.trustedOrigins.includes(origin)) {
|
|
82
|
+
throw new LocalAuthError(403, "ForbiddenOrigin", `Origin ${origin} may not use the daemon`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
authenticate(authorization) {
|
|
86
|
+
const match = authorization === undefined ? null : /^Bearer\s+(\S+)\s*$/iu.exec(authorization);
|
|
87
|
+
const presented = match?.[1];
|
|
88
|
+
if (presented === undefined)
|
|
89
|
+
throw new LocalAuthError(401, "Unauthenticated", "A local client token is required");
|
|
90
|
+
if (this.options.hostSecret !== undefined && same(presented, this.options.hostSecret)) {
|
|
91
|
+
return { id: "host", kind: "host", name: "host" };
|
|
92
|
+
}
|
|
93
|
+
if (this.options.controlSecret !== undefined && same(presented, this.options.controlSecret)) {
|
|
94
|
+
return { id: "control", kind: "host", name: "control" };
|
|
95
|
+
}
|
|
96
|
+
const hash = sha256Hex(presented);
|
|
97
|
+
const harness = this.#harness.get(hash);
|
|
98
|
+
if (harness !== undefined)
|
|
99
|
+
return harness;
|
|
100
|
+
const client = this.state.current.localClients.find((c) => c.tokenHash === hash);
|
|
101
|
+
if (client === undefined)
|
|
102
|
+
throw new LocalAuthError(401, "Unauthenticated", "Unknown or revoked local client token");
|
|
103
|
+
return { id: client.id, kind: client.kind, name: client.name };
|
|
104
|
+
}
|
|
105
|
+
// --- local pairing -------------------------------------------------------------------------------------------
|
|
106
|
+
startPairing(request = {}) {
|
|
107
|
+
const now = Date.now();
|
|
108
|
+
for (const [id, entry] of this.#pending)
|
|
109
|
+
if (entry.expiresAt <= now)
|
|
110
|
+
this.#pending.delete(id);
|
|
111
|
+
const window = this.options.startWindowMs ?? 10 * 60 * 1000;
|
|
112
|
+
while (this.#starts.length > 0 && this.#starts[0] <= now - window)
|
|
113
|
+
this.#starts.shift();
|
|
114
|
+
if (this.#starts.length >= (this.options.startLimit ?? 5) || this.#pending.size >= 20) {
|
|
115
|
+
throw new LocalAuthError(429, "PairingRateLimited", "Too many pairings were started; try again later", {
|
|
116
|
+
retryAfterMs: Math.max(1, (this.#starts[0] ?? now) + window - now),
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
this.#starts.push(now);
|
|
120
|
+
const pairingId = randomUUID();
|
|
121
|
+
const clientName = (request.clientName ?? "a local client").slice(0, 100);
|
|
122
|
+
this.#pending.set(pairingId, {
|
|
123
|
+
code: newCode(),
|
|
124
|
+
expiresAt: now + (this.options.codeTtlMs ?? 5 * 60 * 1000),
|
|
125
|
+
clientName,
|
|
126
|
+
kind: request.kind ?? "cli",
|
|
127
|
+
approved: false,
|
|
128
|
+
attempts: 0,
|
|
129
|
+
});
|
|
130
|
+
// The code is never logged: the host (or `eloxiv-daemon approve-client`) shows it when it approves.
|
|
131
|
+
this.options.log(`${JSON.stringify(clientName)} asks to pair (${pairingId}): approve it in the desktop app or with \`eloxiv-daemon approve-client\``);
|
|
132
|
+
return { pairingId, expiresAt: new Date(now + (this.options.codeTtlMs ?? 5 * 60 * 1000)).toISOString() };
|
|
133
|
+
}
|
|
134
|
+
/** Pending local pairings with their codes, for the host to show (never to other clients). */
|
|
135
|
+
pendingPairings() {
|
|
136
|
+
const now = Date.now();
|
|
137
|
+
return [...this.#pending.entries()]
|
|
138
|
+
.filter(([, entry]) => entry.expiresAt > now)
|
|
139
|
+
.map(([pairingId, entry]) => ({
|
|
140
|
+
pairingId,
|
|
141
|
+
code: entry.code,
|
|
142
|
+
clientName: entry.clientName,
|
|
143
|
+
kind: entry.kind,
|
|
144
|
+
approved: entry.approved,
|
|
145
|
+
expiresAt: new Date(entry.expiresAt).toISOString(),
|
|
146
|
+
}));
|
|
147
|
+
}
|
|
148
|
+
/** The host approves a pending pairing: only then can its code be exchanged for a token. */
|
|
149
|
+
approvePairing(pairingId) {
|
|
150
|
+
const entry = this.#pending.get(pairingId);
|
|
151
|
+
if (entry === undefined || entry.expiresAt <= Date.now()) {
|
|
152
|
+
throw new LocalAuthError(404, "PairingNotFound", "No pending pairing with this id");
|
|
153
|
+
}
|
|
154
|
+
entry.approved = true;
|
|
155
|
+
return { pairingId, code: entry.code, clientName: entry.clientName };
|
|
156
|
+
}
|
|
157
|
+
/** A wrong guess anywhere: it counts against the global budget, and past it every confirmation waits. */
|
|
158
|
+
#fail() {
|
|
159
|
+
this.#failures += 1;
|
|
160
|
+
const budget = this.options.failureBudget ?? 3;
|
|
161
|
+
if (this.#failures >= budget) {
|
|
162
|
+
const base = this.options.lockoutBaseMs ?? 2_000;
|
|
163
|
+
this.#lockedUntil = Date.now() + Math.min(base * 2 ** (this.#failures - budget), MAX_LOCKOUT_MS);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
confirmPairing(input) {
|
|
167
|
+
const now = Date.now();
|
|
168
|
+
if (now < this.#lockedUntil) {
|
|
169
|
+
throw new LocalAuthError(429, "PairingLocked", "Too many wrong pairing codes; wait before trying again", {
|
|
170
|
+
retryAfterMs: this.#lockedUntil - now,
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
const entry = this.#pending.get(input.pairingId);
|
|
174
|
+
if (entry === undefined) {
|
|
175
|
+
this.#fail();
|
|
176
|
+
throw new LocalAuthError(404, "PairingNotFound", "No pending pairing with this id");
|
|
177
|
+
}
|
|
178
|
+
if (entry.expiresAt <= now) {
|
|
179
|
+
this.#pending.delete(input.pairingId);
|
|
180
|
+
throw new LocalAuthError(410, "PairingExpired", "The pairing code has expired");
|
|
181
|
+
}
|
|
182
|
+
if (!entry.approved) {
|
|
183
|
+
this.#fail();
|
|
184
|
+
throw new LocalAuthError(403, "PairingNotApproved", "The pairing has not been approved on this machine yet");
|
|
185
|
+
}
|
|
186
|
+
if (!same(entry.code, normaliseCode(input.code).replace(/^(.{5})(.{5})$/u, "$1-$2"))) {
|
|
187
|
+
entry.attempts += 1;
|
|
188
|
+
this.#fail();
|
|
189
|
+
const attemptsLeft = Math.max(0, (this.options.maxAttempts ?? 3) - entry.attempts);
|
|
190
|
+
if (attemptsLeft === 0)
|
|
191
|
+
this.#pending.delete(input.pairingId);
|
|
192
|
+
throw new LocalAuthError(403, "PairingCodeMismatch", "Wrong code", { attemptsLeft });
|
|
193
|
+
}
|
|
194
|
+
if (input.kind !== undefined && input.kind !== entry.kind) {
|
|
195
|
+
// The host approved this kind; a confirmation may not change it (a companion must not become a desktop-app).
|
|
196
|
+
this.#pending.delete(input.pairingId);
|
|
197
|
+
this.#fail();
|
|
198
|
+
throw new LocalAuthError(403, "PairingKindMismatch", "The kind differs from the pairing the host approved");
|
|
199
|
+
}
|
|
200
|
+
this.#pending.delete(input.pairingId);
|
|
201
|
+
this.#failures = 0;
|
|
202
|
+
const secret = token();
|
|
203
|
+
const client = {
|
|
204
|
+
id: randomUUID(),
|
|
205
|
+
name: input.clientName,
|
|
206
|
+
kind: entry.kind,
|
|
207
|
+
tokenHash: sha256Hex(secret),
|
|
208
|
+
pairedAt: new Date().toISOString(),
|
|
209
|
+
};
|
|
210
|
+
this.state.update((state) => ({ ...state, localClients: [...state.localClients, client] }));
|
|
211
|
+
return { clientId: client.id, token: secret, kind: client.kind };
|
|
212
|
+
}
|
|
213
|
+
unpair(clientId) {
|
|
214
|
+
this.state.update((state) => ({ ...state, localClients: state.localClients.filter((c) => c.id !== clientId) }));
|
|
215
|
+
}
|
|
216
|
+
// --- harness grants ------------------------------------------------------------------------------------------
|
|
217
|
+
/** A token for a harness running one turn; revoked when the turn ends. */
|
|
218
|
+
grantHarness(grant) {
|
|
219
|
+
const secret = token();
|
|
220
|
+
const hash = sha256Hex(secret);
|
|
221
|
+
this.#harness.set(hash, { id: `harness:${grant.turnId}`, kind: "harness", name: grant.turnId, turn: grant });
|
|
222
|
+
return { token: secret, revoke: () => this.#harness.delete(hash) };
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
//# sourceMappingURL=LocalAuth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LocalAuth.js","sourceRoot":"","sources":["../src/LocalAuth.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,OAAO,EAAE,wBAAwB,EAAE,MAAM,mBAAmB,CAAA;AAC5D,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AACtE,OAAO,EAAE,SAAS,EAAoC,MAAM,YAAY,CAAA;AAwBxE,MAAM,OAAO,cAAe,SAAQ,KAAK;IAC9B,MAAM,CAAiB;IACvB,GAAG,CAAQ;IACX,MAAM,CAAyB;IACxC,YAAY,MAAuB,EAAE,GAAW,EAAE,OAAe,EAAE,MAAM,GAA4B,EAAE;QACrG,KAAK,CAAC,OAAO,CAAC,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EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,CAAA;IAClE,CAAC;IAED,MAAM,CAAC,QAAgB;QACrB,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,KAAK,EAAE,YAAY,EAAE,KAAK,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAA;IACjH,CAAC;IAED,gHAAgH;IAEhH,0EAA0E;IAC1E,YAAY,CAAC,KAAmB;QAC9B,MAAM,MAAM,GAAG,KAAK,EAAE,CAAA;QACtB,MAAM,IAAI,GAAG,SAAS,CAAC,MAAM,CAAC,CAAA;QAC9B,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,EAAE,EAAE,WAAW,KAAK,CAAC,MAAM,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,CAAC,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAA;QAC5G,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAA;IACpE,CAAC;CACF"}
|
package/dist/Main.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Layer } from "effect";
|
|
2
|
+
import { HttpServer } from "effect/http";
|
|
3
|
+
import type { Teardown } from "effect/Runtime";
|
|
4
|
+
import type { SyncSqliteDatabase } from "@eloxiv/client/storage";
|
|
5
|
+
import type { Effect as EffectType } from "effect/Effect";
|
|
6
|
+
import { type DaemonConfig } from "./Config.ts";
|
|
7
|
+
import type { ClientApp } from "./Client.ts";
|
|
8
|
+
import { Daemon, type DaemonOptions } from "./Daemon.ts";
|
|
9
|
+
export declare const DAEMON_BUILD: string;
|
|
10
|
+
/** The port the platform server actually bound (differs from the config when it asked for 0). */
|
|
11
|
+
export declare const boundPort: EffectType<number, never, HttpServer.HttpServer>;
|
|
12
|
+
/** The daemon's core for a config: its state directory, secret store and harnesses. */
|
|
13
|
+
export declare const createDaemon: (config: Pick<DaemonConfig, "stateDir" | "secretStore" | "headless" | "trustedOrigins" | "workspace" | "hostSecret"> & Partial<Pick<DaemonConfig, "commands" | "toolsOnly">>, overrides?: Partial<DaemonOptions>) => Promise<Daemon>;
|
|
14
|
+
/** The runner settings of a config, as `daemonLayer` takes them. */
|
|
15
|
+
export declare const runnerOf: (config: Partial<Pick<DaemonConfig, "headless" | "kernelUrl" | "name">>) => {
|
|
16
|
+
headless: boolean;
|
|
17
|
+
kernelUrl: string | undefined;
|
|
18
|
+
name: string | undefined;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* The daemon application over some platform `HttpServer` layer, which must already be bound to the configured
|
|
22
|
+
* host and port. The server stays in the output so callers can read the bound address.
|
|
23
|
+
*/
|
|
24
|
+
export declare const daemonLayer: <E, R>(server: Layer.Layer<HttpServer.HttpServer, E, R>, daemon: Daemon, config?: Pick<DaemonConfig, "headless" | "kernelUrl" | "name">) => Layer.Layer<HttpServer.HttpServer, E, R>;
|
|
25
|
+
/** Completes when stdin reaches end of file (the parent closed its end or died). */
|
|
26
|
+
export declare const stdinClosed: EffectType<void, never, never>;
|
|
27
|
+
/** Run the daemon until interrupted (SIGTERM/SIGINT) or, with the lifeline, until stdin closes. */
|
|
28
|
+
export declare const program: <E, R>(app: Layer.Layer<never, E, R>, config: Pick<DaemonConfig, "stdinLifeline">) => EffectType<void, E, R>;
|
|
29
|
+
/** Shutting down on a signal or on the lifeline is a clean exit (0); anything else is 1. */
|
|
30
|
+
export declare const teardown: Teardown;
|
|
31
|
+
/**
|
|
32
|
+
* `pair`, `status`, `unpair`, `approve-client` and `pairing-code`: one-shot commands, printing to stdout. Resolves to
|
|
33
|
+
* the exit code.
|
|
34
|
+
*/
|
|
35
|
+
export declare const runCommand: (config: DaemonConfig) => Promise<number>;
|
|
36
|
+
/** What a runtime entry (`runtime/bun`, `runtime/node`) gives `mainFor`: its server layer, run-main and SQLite. */
|
|
37
|
+
export interface RuntimeEntry {
|
|
38
|
+
readonly serverLayer: (config: DaemonConfig, daemon: Daemon) => Layer.Layer<never, unknown>;
|
|
39
|
+
readonly runMain: (effect: EffectType<unknown, unknown>, options: {
|
|
40
|
+
readonly teardown: Teardown;
|
|
41
|
+
}) => void;
|
|
42
|
+
readonly openSqlite: (path: string) => SyncSqliteDatabase;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The daemon's main for a runtime: the generic sidecar (`bin.ts`), or an app's own with `clientApp` (what it serves
|
|
46
|
+
* LocalClient for, decision 14). One-shot commands run and exit; `serve` runs until interrupted or the lifeline ends.
|
|
47
|
+
*/
|
|
48
|
+
export declare const mainFor: (entry: RuntimeEntry) => (argv?: ReadonlyArray<string>, env?: Record<string, string | undefined>, options?: {
|
|
49
|
+
readonly clientApp?: ClientApp | undefined;
|
|
50
|
+
}) => void;
|
|
51
|
+
//# sourceMappingURL=Main.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Main.d.ts","sourceRoot":"","sources":["../src/Main.ts"],"names":[],"mappings":"AAQA,OAAO,EAAuB,KAAK,EAAE,MAAM,QAAQ,CAAA;AACnD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA;AAC9C,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAA;AAChE,OAAO,KAAK,EAAE,MAAM,IAAI,UAAU,EAAE,MAAM,eAAe,CAAA;AACzD,OAAO,EAA4C,KAAK,YAAY,EAAE,MAAM,aAAa,CAAA;AACzF,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAC5C,OAA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AED,mHAAmH;AACnH,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,WAAW,EAAE,CAAC,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,KAAK,KAAK,CAAC,KAAK,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;IAC3F,QAAQ,CAAC,OAAO,EAAE,CAAC,MAAM,EAAE,UAAU,CAAC,OAAO,EAAE,OAAO,CAAC,EAAE,OAAO,EAAE;QAAE,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAA;KAAE,KAAK,IAAI,CAAA;IAC1G,QAAQ,CAAC,UAAU,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,kBAAkB,CAAA;CAC1D;AAED;;;GAGG;AACH,eAAO,MAAM,OAAO,UACV,YAAY,aAEZ,aAAa,CAAC,MAAM,CAAC,QACtB,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,YAC9B;IAAE,QAAQ,CAAC,SAAS,CAAC,EAAE,SAAS,GAAG,SAAS,CAAA;CAAE,SAcxD,CAAA"}
|
package/dist/Main.js
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { mkdirSync, readFileSync } from "node:fs";
|
|
2
|
+
import { resolve, sep } from "node:path";
|
|
3
|
+
import { Cause, Effect, Exit, Layer } from "effect";
|
|
4
|
+
import { HttpServer } from "effect/http";
|
|
5
|
+
import { readHostSecret, readyLine, resolveConfig } from "./Config.js";
|
|
6
|
+
import { controlSecretPath, Daemon } from "./Daemon.js";
|
|
7
|
+
import { DaemonHttp, trustedOriginsFor } from "./Http.js";
|
|
8
|
+
import { selectSecretStore } from "./Secrets.js";
|
|
9
|
+
export const DAEMON_BUILD = typeof ELOXIV_DAEMON_BUILD === "string" ? ELOXIV_DAEMON_BUILD : "development";
|
|
10
|
+
/** The port the platform server actually bound (differs from the config when it asked for 0). */
|
|
11
|
+
export const boundPort = Effect.gen(function* () {
|
|
12
|
+
const server = yield* HttpServer.HttpServer;
|
|
13
|
+
return server.address._tag === "UnixPathAddress" ? 0 : server.address.port;
|
|
14
|
+
});
|
|
15
|
+
/** The daemon's core for a config: its state directory, secret store and harnesses. */
|
|
16
|
+
export const createDaemon = async (config, overrides = {}) => {
|
|
17
|
+
mkdirSync(config.stateDir, { recursive: true, mode: 0o700 });
|
|
18
|
+
const stateDir = resolve(config.stateDir);
|
|
19
|
+
const workspace = config.workspace === undefined ? undefined : resolve(config.workspace);
|
|
20
|
+
// A harness works in its workspace: it must never be the state directory (the credential's file store, the
|
|
21
|
+
// control secret, state.json) or inside it. The state directory may be inside the workspace (a home directory as
|
|
22
|
+
// the workspace, the default state under it): file commands exclude it (`Commands.ts`).
|
|
23
|
+
if (workspace !== undefined && (workspace === stateDir || workspace.startsWith(`${stateDir}${sep}`))) {
|
|
24
|
+
throw new Error(`The workspace (${workspace}) must be outside the state directory (${stateDir})`);
|
|
25
|
+
}
|
|
26
|
+
if (workspace !== undefined)
|
|
27
|
+
mkdirSync(workspace, { recursive: true });
|
|
28
|
+
return new Daemon({
|
|
29
|
+
mode: config.headless ? "headless" : "desktop",
|
|
30
|
+
stateDir: config.stateDir,
|
|
31
|
+
secrets: await selectSecretStore(config.secretStore, config.stateDir),
|
|
32
|
+
hostSecret: config.hostSecret,
|
|
33
|
+
trustedOrigins: config.trustedOrigins ?? trustedOriginsFor(DAEMON_BUILD),
|
|
34
|
+
workspace,
|
|
35
|
+
commands: config.commands,
|
|
36
|
+
toolsOnly: config.toolsOnly,
|
|
37
|
+
...overrides,
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Once listening: tell the core its port, start the runner (headless; pairing first when `--kernel` is given
|
|
42
|
+
* and the device is not paired), and print the ready line.
|
|
43
|
+
*/
|
|
44
|
+
const started = (daemon, config) => Effect.gen(function* () {
|
|
45
|
+
daemon.port = yield* boundPort;
|
|
46
|
+
// Device commands, in either mode, once paired (a pairing completed later starts them too).
|
|
47
|
+
if (daemon.paired)
|
|
48
|
+
daemon.startCommands();
|
|
49
|
+
// LocalClient: the stored person session is checked in the background (the kernel may not be up yet).
|
|
50
|
+
if (daemon.client !== undefined)
|
|
51
|
+
void daemon.client.start();
|
|
52
|
+
if (config.headless) {
|
|
53
|
+
if (daemon.paired)
|
|
54
|
+
daemon.startRunner();
|
|
55
|
+
else if (config.kernelUrl !== undefined) {
|
|
56
|
+
const pairing = yield* Effect.promise(() => daemon.startKernelPairing(config.kernelUrl, { name: config.name, kind: "runner" }));
|
|
57
|
+
// The code goes to a person at a terminal only, never to a log (`eloxiv-daemon pairing-code` reads it).
|
|
58
|
+
if (process.stderr.isTTY) {
|
|
59
|
+
process.stderr.write(`To pair this runner, confirm the code ${pairing.started.userCode} at ${pairing.started.verificationUri}\n`);
|
|
60
|
+
}
|
|
61
|
+
pairing.done.then(() => daemon.startRunner(), (error) => daemon.log(`pairing failed: ${String(error)}`));
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
daemon.log("headless, but not paired: run `eloxiv-daemon pair --kernel <url> --runner` or pass --kernel");
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (daemon.options.announce !== false)
|
|
68
|
+
yield* Effect.sync(() => process.stdout.write(`${readyLine(daemon.port)}\n`));
|
|
69
|
+
});
|
|
70
|
+
/** The runner settings of a config, as `daemonLayer` takes them. */
|
|
71
|
+
export const runnerOf = (config) => ({
|
|
72
|
+
headless: config.headless ?? false,
|
|
73
|
+
kernelUrl: config.kernelUrl,
|
|
74
|
+
name: config.name,
|
|
75
|
+
});
|
|
76
|
+
/**
|
|
77
|
+
* The daemon application over some platform `HttpServer` layer, which must already be bound to the configured
|
|
78
|
+
* host and port. The server stays in the output so callers can read the bound address.
|
|
79
|
+
*/
|
|
80
|
+
export const daemonLayer = (server, daemon, config = {
|
|
81
|
+
headless: false,
|
|
82
|
+
kernelUrl: undefined,
|
|
83
|
+
name: undefined,
|
|
84
|
+
}) => DaemonHttp(daemon).pipe(Layer.tap(() => started(daemon, config)), Layer.provideMerge(server));
|
|
85
|
+
/** Completes when stdin reaches end of file (the parent closed its end or died). */
|
|
86
|
+
export const stdinClosed = Effect.callback((resume) => {
|
|
87
|
+
const done = () => resume(Effect.void);
|
|
88
|
+
process.stdin.once("end", done);
|
|
89
|
+
process.stdin.once("close", done);
|
|
90
|
+
process.stdin.resume();
|
|
91
|
+
return Effect.sync(() => {
|
|
92
|
+
process.stdin.off("end", done);
|
|
93
|
+
process.stdin.off("close", done);
|
|
94
|
+
process.stdin.pause();
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
/** Run the daemon until interrupted (SIGTERM/SIGINT) or, with the lifeline, until stdin closes. */
|
|
98
|
+
export const program = (app, config) => {
|
|
99
|
+
const serve = Layer.launch(app);
|
|
100
|
+
return config.stdinLifeline ? Effect.raceFirst(serve, stdinClosed) : serve;
|
|
101
|
+
};
|
|
102
|
+
/** Shutting down on a signal or on the lifeline is a clean exit (0); anything else is 1. */
|
|
103
|
+
export const teardown = (exit, onExit) => {
|
|
104
|
+
const clean = Exit.isSuccess(exit) || Cause.hasInterruptsOnly(exit.cause);
|
|
105
|
+
onExit(clean ? 0 : 1);
|
|
106
|
+
// runMain only exits by itself after a signal or a failure; the lifeline path ends here.
|
|
107
|
+
if (clean)
|
|
108
|
+
process.exit(0);
|
|
109
|
+
};
|
|
110
|
+
/**
|
|
111
|
+
* `approve-client`: the person at this machine approves a local client's pairing with the running daemon (the
|
|
112
|
+
* port from `ELOXIV_DAEMON_PORT` or --port), acting as the host through the control secret in the state
|
|
113
|
+
* directory. Without an id it lists the pending requests; with one it approves it and prints the code to type
|
|
114
|
+
* into the client.
|
|
115
|
+
*/
|
|
116
|
+
const approveClient = async (config) => {
|
|
117
|
+
const secret = readFileSync(controlSecretPath(config.stateDir), "utf8").trim();
|
|
118
|
+
const base = `http://127.0.0.1:${config.port}`;
|
|
119
|
+
const headers = { authorization: `Bearer ${secret}`, "content-type": "application/json" };
|
|
120
|
+
if (config.pairingId === undefined) {
|
|
121
|
+
const pending = (await (await fetch(`${base}/local/pairings`, { headers })).json());
|
|
122
|
+
if (pending.length === 0)
|
|
123
|
+
process.stdout.write("No local client is waiting to pair.\n");
|
|
124
|
+
for (const entry of pending) {
|
|
125
|
+
process.stdout.write(`${entry.pairingId} ${entry.kind} ${JSON.stringify(entry.clientName)}${entry.approved ? " (approved)" : ""}\n`);
|
|
126
|
+
}
|
|
127
|
+
return 0;
|
|
128
|
+
}
|
|
129
|
+
const answer = await fetch(`${base}/local/pairings/${encodeURIComponent(config.pairingId)}/approve`, {
|
|
130
|
+
method: "POST",
|
|
131
|
+
headers,
|
|
132
|
+
body: "{}",
|
|
133
|
+
});
|
|
134
|
+
if (!answer.ok) {
|
|
135
|
+
process.stdout.write(`Could not approve ${config.pairingId}: ${await answer.text()}\n`);
|
|
136
|
+
return 1;
|
|
137
|
+
}
|
|
138
|
+
const approved = (await answer.json());
|
|
139
|
+
process.stdout.write(`Approved ${JSON.stringify(approved.clientName)}. Type this code into it: ${approved.code}\n`);
|
|
140
|
+
return 0;
|
|
141
|
+
};
|
|
142
|
+
/**
|
|
143
|
+
* `pairing-code`: the kernel pairing code of the running daemon (the port from `ELOXIV_DAEMON_PORT` or --port),
|
|
144
|
+
* read through the control secret in the state directory, for the person at this machine. The daemon never logs it.
|
|
145
|
+
*/
|
|
146
|
+
const pairingCode = async (config) => {
|
|
147
|
+
const secret = readFileSync(controlSecretPath(config.stateDir), "utf8").trim();
|
|
148
|
+
const answer = await fetch(`http://127.0.0.1:${config.port}/status`, {
|
|
149
|
+
headers: { authorization: `Bearer ${secret}` },
|
|
150
|
+
});
|
|
151
|
+
if (!answer.ok) {
|
|
152
|
+
process.stdout.write(`Could not read the daemon's status: ${await answer.text()}\n`);
|
|
153
|
+
return 1;
|
|
154
|
+
}
|
|
155
|
+
const status = (await answer.json());
|
|
156
|
+
if (status.kernel.userCode === null) {
|
|
157
|
+
process.stdout.write(`No kernel pairing is in progress (this device is ${status.kernel.state}).\n`);
|
|
158
|
+
return 1;
|
|
159
|
+
}
|
|
160
|
+
process.stdout.write(`Confirm the code ${status.kernel.userCode} at ${status.kernel.verificationUri ?? status.kernel.url ?? "the kernel"}\n`);
|
|
161
|
+
return 0;
|
|
162
|
+
};
|
|
163
|
+
/**
|
|
164
|
+
* `pair`, `status`, `unpair`, `approve-client` and `pairing-code`: one-shot commands, printing to stdout. Resolves to
|
|
165
|
+
* the exit code.
|
|
166
|
+
*/
|
|
167
|
+
export const runCommand = async (config) => {
|
|
168
|
+
// They talk to the running daemon; starting a second core here would replace that daemon's control secret.
|
|
169
|
+
if (config.command === "approve-client")
|
|
170
|
+
return approveClient(config);
|
|
171
|
+
if (config.command === "pairing-code")
|
|
172
|
+
return pairingCode(config);
|
|
173
|
+
const daemon = await createDaemon(config);
|
|
174
|
+
switch (config.command) {
|
|
175
|
+
case "pair": {
|
|
176
|
+
const pairing = await daemon.startKernelPairing(config.kernelUrl, {
|
|
177
|
+
name: config.name,
|
|
178
|
+
kind: config.runner ? "runner" : "desktop",
|
|
179
|
+
});
|
|
180
|
+
process.stdout.write(`To pair this device, sign in at ${pairing.started.verificationUri} and confirm the code ${pairing.started.userCode}\n` +
|
|
181
|
+
`(${pairing.started.verificationUriComplete}). Waiting...\n`);
|
|
182
|
+
try {
|
|
183
|
+
await pairing.done;
|
|
184
|
+
}
|
|
185
|
+
catch (error) {
|
|
186
|
+
process.stdout.write(`Pairing failed: ${String(error instanceof Error ? error.message : error)}\n`);
|
|
187
|
+
return 1;
|
|
188
|
+
}
|
|
189
|
+
const kernel = daemon.state.current.kernel;
|
|
190
|
+
process.stdout.write(`Paired as device ${kernel.deviceId} in ${kernel.orgId}; the credential is in the ${daemon.options.secrets.kind} store.\n`);
|
|
191
|
+
return 0;
|
|
192
|
+
}
|
|
193
|
+
case "status":
|
|
194
|
+
process.stdout.write(`${JSON.stringify(await daemon.status(), null, 2)}\n`);
|
|
195
|
+
return 0;
|
|
196
|
+
case "unpair":
|
|
197
|
+
await daemon.unpair();
|
|
198
|
+
process.stdout.write("Unpaired: the credential was deleted (revoke the device on the kernel too).\n");
|
|
199
|
+
return 0;
|
|
200
|
+
case "serve":
|
|
201
|
+
return 0;
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
/**
|
|
205
|
+
* The daemon's main for a runtime: the generic sidecar (`bin.ts`), or an app's own with `clientApp` (what it serves
|
|
206
|
+
* LocalClient for, decision 14). One-shot commands run and exit; `serve` runs until interrupted or the lifeline ends.
|
|
207
|
+
*/
|
|
208
|
+
export const mainFor = (entry) => (argv = process.argv.slice(2), env = process.env, options = {}) => {
|
|
209
|
+
// The host's secret leaves the environment at once: nothing the daemon starts inherits it.
|
|
210
|
+
const config = { ...resolveConfig(argv, env), hostSecret: readHostSecret(env) };
|
|
211
|
+
if (config.command !== "serve") {
|
|
212
|
+
void runCommand(config).then((code) => process.exit(code));
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
const overrides = options.clientApp === undefined ? {} : { clientApp: options.clientApp, sqlite: entry.openSqlite, env };
|
|
216
|
+
const app = Layer.unwrap(Effect.promise(() => createDaemon(config, overrides)).pipe(Effect.map((d) => entry.serverLayer(config, d))));
|
|
217
|
+
entry.runMain(program(app, config), { teardown });
|
|
218
|
+
};
|
|
219
|
+
//# sourceMappingURL=Main.js.map
|
package/dist/Main.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Main.js","sourceRoot":"","sources":["../src/Main.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AACjD,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,WAAW,CAAA;AACxC,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAA;AACnD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAIxC,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,aAAa,EAAqB,MAA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session for LocalClient (the client core's `SessionStore`): the token in the OS secret store
|
|
3
|
+
* (or its file fallback) under its own key (`personSessionKey`), as before, and the context last open with it
|
|
4
|
+
* remembered for the client core's offline start (`recall`, `remember`, `forgetContext`), so a daemon started without
|
|
5
|
+
* the network serves the person's replica of that organisation from disk at once.
|
|
6
|
+
*
|
|
7
|
+
* - **The remembered context is not a secret, but it is bound to the token.** It is kept in the state directory
|
|
8
|
+
* (`person-context.json`, 0600, written atomically) with a SHA-256 digest of the token it was open with, never the
|
|
9
|
+
* token, and recalled only while the secret store still holds that token.
|
|
10
|
+
* - **It goes with its session.** Forgotten whenever the session is (`clear`, `clearIf`: the context first, so a failure
|
|
11
|
+
* between the two leaves a session that reopens nothing offline), when another token is saved, and alone
|
|
12
|
+
* (`forgetContext`, that token's only).
|
|
13
|
+
* - **Failures are errors.** A read or write the secret store or the file system refuses rejects, never an empty
|
|
14
|
+
* answer for a read of the session, so the core retries a forgetting or forgets the context instead.
|
|
15
|
+
* - **One writer.** Every write and every recall goes through one queue, so `clearIf` compares and clears atomically
|
|
16
|
+
* with the others; a recall reads the context only after the secret store answered, so a context another instance
|
|
17
|
+
* forgot meanwhile is not returned. One daemon per canonical state directory (`lockPersonState`): a second refuses.
|
|
18
|
+
* - **Forgetting has two protections.** The context file is unlinked first and the token deleted from the secret store
|
|
19
|
+
* regardless; a context file that cannot be unlinked (a state directory not writable now) is still harmless once
|
|
20
|
+
* the secret store no longer holds its token (read back), since nothing is recalled without it. Only when both stay
|
|
21
|
+
* does the forgetting reject.
|
|
22
|
+
*/
|
|
23
|
+
import type { SessionStore } from "@eloxiv/client";
|
|
24
|
+
import type { SecretStore } from "./Secrets.ts";
|
|
25
|
+
/** Where the context remembered with the person session is kept (beside `state.json`). */
|
|
26
|
+
export declare const personContextPath: (stateDir: string) => string;
|
|
27
|
+
/** The lock a daemon serving LocalClient holds on its state directory (beside `state.json`). */
|
|
28
|
+
export declare const personStateLockPath: (stateDir: string) => string;
|
|
29
|
+
/**
|
|
30
|
+
* Take the state directory for one daemon's person session (its canonical path: a symlink or `dir/.` is the same
|
|
31
|
+
* one): a lock file holding this process's id, created exclusively. Another daemon's lock (in this process, or a
|
|
32
|
+
* process still running) refuses with an error; one a process that is gone left behind is taken over. Best effort
|
|
33
|
+
* against two daemons taking over the same stale lock at the same instant. A state directory that does not exist holds
|
|
34
|
+
* no person session to share (the daemon creates it before it serves one): nothing to lock.
|
|
35
|
+
*/
|
|
36
|
+
export declare const lockPersonState: (stateDir: string) => {
|
|
37
|
+
readonly release: () => void;
|
|
38
|
+
};
|
|
39
|
+
export interface PersonSessionStoreOptions {
|
|
40
|
+
readonly secrets: SecretStore;
|
|
41
|
+
/** The secret-store key (`personSessionKey(stateDir)`). */
|
|
42
|
+
readonly key: string;
|
|
43
|
+
/** The remembered context's file (`personContextPath(stateDir)`). */
|
|
44
|
+
readonly contextPath: string;
|
|
45
|
+
/** Told each token read or written (`undefined` once forgotten): the service's scrub keeps the last one. */
|
|
46
|
+
readonly onChange?: ((token: string | undefined) => void) | undefined;
|
|
47
|
+
}
|
|
48
|
+
export declare const personSessionStore: (options: PersonSessionStoreOptions) => SessionStore;
|
|
49
|
+
//# sourceMappingURL=PersonSession.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PersonSession.d.ts","sourceRoot":"","sources":["../src/PersonSession.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,OAAO,KAAK,EAA8B,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAI9E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AAE/C,0FAA0F;AAC1F,eAAO,MAAM,iBAAiB,aAAc,MAAM,WAA0C,CAAA;AAE5F,gGAAgG;AAChG,eAAO,MAAM,mBAAmB,aAAc,MAAM,WAA0C,CAAA;AAc9F;;;;;;GAMG;AACH,eAAO,MAAM,eAAe,aAAc,MAAM,KAAG;IAAE,QAAQ,CAAC,OAAO,EAAE,MAAM,IAAI,CAAA;CAiChF,CAAA;AAID,MAAM,WAAW,yBAAyB;IACxC,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAA;IAC7B,2DAA2D;IAC3D,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAA;IACpB,qEAAqE;IACrE,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAA;IAC5B,4GAA4G;IAC5G,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,KAAK,IAAI,CAAC,GAAG,SAAS,CAAA;CACtE;AAED,eAAO,MAAM,kBAAkB,YAAa,yBAAyB,KAAG,YAsGvE,CAAA"}
|