tmux-ide 2.9.20 → 2.9.22
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/bin/cli.js +16998 -4283
- package/package.json +3 -2
- package/packages/contracts/src/command-catalog.ts +116 -0
- package/packages/contracts/src/commands.ts +5 -4
- package/packages/contracts/src/daemon-events.ts +2 -1
- package/packages/contracts/src/index.ts +3 -0
- package/packages/contracts/src/pane-reader.ts +121 -0
- package/packages/contracts/src/pane-stream.ts +18 -0
- package/packages/contracts/src/plain-text.ts +61 -0
- package/packages/contracts/src/visual-theme-presets.ts +58 -2
- package/packages/daemon/dist/command-center/command-catalog-routes.js +78 -0
- package/packages/daemon/dist/command-center/plugin-routes.js +84 -0
- package/packages/daemon/dist/command-center/server.js +13 -0
- package/packages/daemon/dist/command-center/tmux-server-reader.js +41 -0
- package/packages/daemon/dist/command-center/tmux-servers.js +6 -0
- package/packages/daemon/dist/doctor.js +106 -43
- package/packages/daemon/dist/lib/cli-action-bridge.js +15 -0
- package/packages/daemon/dist/lib/cli-command.js +13 -0
- package/packages/daemon/dist/lib/daemon-embed.js +39 -1
- package/packages/daemon/dist/lib/headless-daemon.js +1 -0
- package/packages/daemon/dist/lib/pane-reader-files.js +226 -0
- package/packages/daemon/dist/lib/remote-tmux-command.js +8 -5
- package/packages/daemon/dist/lib/tmux-client-execution.js +73 -7
- package/packages/daemon/dist/lib/tmux-pane-reader.js +105 -0
- package/packages/daemon/dist/lib/tmux-server-features.js +38 -0
- package/packages/daemon/dist/lib/tmux-server-owner.js +9 -0
- package/packages/daemon/dist/lib/worktree.js +18 -0
- package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/Info.plist +2 -2
- package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/MacOS/tmux-ide-notifier +0 -0
- package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/Resources/Assets.car +0 -0
- package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/_CodeSignature/CodeResources +2 -2
- package/packages/daemon/dist/open-browser.js +26 -12
- package/packages/daemon/dist/open-reader.js +84 -0
- package/packages/daemon/dist/plugins/action-catalog.js +129 -0
- package/packages/daemon/dist/plugins/action-cli.js +212 -0
- package/packages/daemon/dist/plugins/api-table.js +303 -0
- package/packages/daemon/dist/plugins/cli.js +556 -0
- package/packages/daemon/dist/plugins/consent.js +107 -0
- package/packages/daemon/dist/plugins/daemon-plugins.js +124 -0
- package/packages/daemon/dist/plugins/discovery.js +142 -0
- package/packages/daemon/dist/plugins/examples/worktree-tasks.js +115 -0
- package/packages/daemon/dist/plugins/exec-sandbox.js +202 -0
- package/packages/daemon/dist/plugins/host/actions-prelude.js +279 -0
- package/packages/daemon/dist/plugins/host/entry.js +4 -0
- package/packages/daemon/dist/plugins/host/host-api-prelude.js +232 -0
- package/packages/daemon/dist/plugins/host/lua-state.js +493 -0
- package/packages/daemon/dist/plugins/host/main.js +322 -0
- package/packages/daemon/dist/plugins/host/sandbox.js +299 -0
- package/packages/daemon/dist/plugins/host-calls.js +279 -0
- package/packages/daemon/dist/plugins/manifest.js +69 -0
- package/packages/daemon/dist/plugins/plugin-exec.js +261 -0
- package/packages/daemon/dist/plugins/plugin-log.js +54 -0
- package/packages/daemon/dist/plugins/plugin-store.js +74 -0
- package/packages/daemon/dist/plugins/protocol.js +252 -0
- package/packages/daemon/dist/plugins/safe-files.js +97 -0
- package/packages/daemon/dist/plugins/supervisor.js +790 -0
- package/packages/daemon/dist/plugins/worktree-calls.js +154 -0
- package/packages/daemon/dist/restore.js +57 -40
- package/packages/daemon/dist/terminal/mirror/session-channel.js +4 -1
- package/packages/daemon/dist/terminal/pane-stream/pane-stream-websocket.js +12 -3
- package/packages/daemon/dist/terminal/protocol/reader-option.js +66 -0
- package/packages/daemon/dist/terminal/protocol/session-descriptor-discovery.js +7 -2
- package/packages/daemon/dist/tmux-upgrade/cli.js +106 -0
- package/packages/daemon/dist/tmux-upgrade/io.js +540 -0
- package/packages/daemon/dist/tmux-upgrade/plan.js +238 -0
- package/packages/daemon/dist/tmux-upgrade/run.js +199 -0
- package/packages/daemon/dist/tui/chrome/snapshot.js +3 -4
- package/packages/daemon/dist/tui/mirror/runtime/application-home-agent-navigation.js +64 -4
- package/packages/daemon/dist/tui/mirror/runtime/application-home-agents-owner.js +62 -5
- package/packages/daemon/dist/tui/mirror/runtime/application-home-experience.js +2 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-machine-agent-navigation.js +12 -8
- package/packages/daemon/dist/tui/mirror/runtime/application-palette-command-owner.js +30 -2
- package/packages/daemon/dist/tui/mirror/runtime/application-plugin-commands.js +152 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-plugin-form.js +251 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-terminal-interaction-controller.js +125 -111
- package/packages/daemon/dist/tui/mirror/workspace/application-command-description.js +14 -0
- package/packages/daemon/dist/tui/mirror/workspace/palette-list-layout.js +5 -0
- package/packages/daemon/src/command-center/command-catalog-routes.ts +116 -0
- package/packages/daemon/src/command-center/plugin-routes.ts +110 -0
- package/packages/daemon/src/command-center/server.ts +15 -0
- package/packages/daemon/src/command-center/tmux-server-reader.ts +62 -0
- package/packages/daemon/src/command-center/tmux-servers.ts +6 -0
- package/packages/daemon/src/doctor.ts +118 -45
- package/packages/daemon/src/lib/cli-action-bridge.ts +19 -0
- package/packages/daemon/src/lib/cli-command.ts +16 -0
- package/packages/daemon/src/lib/daemon-embed.ts +59 -0
- package/packages/daemon/src/lib/headless-daemon.ts +1 -0
- package/packages/daemon/src/lib/pane-reader-files.ts +294 -0
- package/packages/daemon/src/lib/remote-tmux-command.ts +13 -6
- package/packages/daemon/src/lib/tmux-client-execution.ts +79 -7
- package/packages/daemon/src/lib/tmux-pane-reader.ts +144 -0
- package/packages/daemon/src/lib/tmux-server-features.ts +43 -0
- package/packages/daemon/src/lib/tmux-server-owner.ts +9 -0
- package/packages/daemon/src/lib/worktree.ts +16 -0
- package/packages/daemon/src/open-browser.ts +59 -18
- package/packages/daemon/src/open-reader.ts +148 -0
- package/packages/daemon/src/plugins/action-catalog.ts +141 -0
- package/packages/daemon/src/plugins/action-cli.ts +248 -0
- package/packages/daemon/src/plugins/api-table.ts +346 -0
- package/packages/daemon/src/plugins/cli.ts +630 -0
- package/packages/daemon/src/plugins/consent.ts +125 -0
- package/packages/daemon/src/plugins/daemon-plugins.ts +172 -0
- package/packages/daemon/src/plugins/discovery.ts +176 -0
- package/packages/daemon/src/plugins/examples/worktree-tasks.ts +120 -0
- package/packages/daemon/src/plugins/exec-sandbox.ts +264 -0
- package/packages/daemon/src/plugins/host/actions-prelude.ts +280 -0
- package/packages/daemon/src/plugins/host/entry.ts +5 -0
- package/packages/daemon/src/plugins/host/host-api-prelude.ts +233 -0
- package/packages/daemon/src/plugins/host/lua-state.ts +557 -0
- package/packages/daemon/src/plugins/host/main.ts +359 -0
- package/packages/daemon/src/plugins/host/sandbox.ts +301 -0
- package/packages/daemon/src/plugins/host-calls.ts +343 -0
- package/packages/daemon/src/plugins/manifest.ts +74 -0
- package/packages/daemon/src/plugins/plugin-exec.ts +326 -0
- package/packages/daemon/src/plugins/plugin-log.ts +63 -0
- package/packages/daemon/src/plugins/plugin-store.ts +80 -0
- package/packages/daemon/src/plugins/protocol.ts +275 -0
- package/packages/daemon/src/plugins/safe-files.ts +105 -0
- package/packages/daemon/src/plugins/supervisor.ts +954 -0
- package/packages/daemon/src/plugins/worktree-calls.ts +183 -0
- package/packages/daemon/src/restore.ts +80 -44
- package/packages/daemon/src/terminal/mirror/events.ts +2 -0
- package/packages/daemon/src/terminal/mirror/session-channel.ts +6 -1
- package/packages/daemon/src/terminal/pane-stream/pane-stream-websocket.ts +15 -2
- package/packages/daemon/src/terminal/protocol/reader-option.ts +77 -0
- package/packages/daemon/src/terminal/protocol/session-descriptor-discovery.ts +10 -1
- package/packages/daemon/src/tmux-upgrade/cli.ts +118 -0
- package/packages/daemon/src/tmux-upgrade/io.ts +596 -0
- package/packages/daemon/src/tmux-upgrade/plan.ts +353 -0
- package/packages/daemon/src/tmux-upgrade/run.ts +325 -0
- package/packages/daemon/src/tui/chrome/snapshot.ts +3 -4
- package/packages/daemon/src/tui/mirror/runtime/application-fleet-switcher.tsx +3 -1
- package/packages/daemon/src/tui/mirror/runtime/application-home-agent-navigation.ts +110 -10
- package/packages/daemon/src/tui/mirror/runtime/application-home-agents-owner.ts +90 -6
- package/packages/daemon/src/tui/mirror/runtime/application-home-experience.ts +2 -0
- package/packages/daemon/src/tui/mirror/runtime/application-machine-agent-navigation.ts +20 -9
- package/packages/daemon/src/tui/mirror/runtime/application-palette-command-owner.ts +42 -2
- package/packages/daemon/src/tui/mirror/runtime/application-palette-preview.tsx +7 -1
- package/packages/daemon/src/tui/mirror/runtime/application-plugin-commands.ts +229 -0
- package/packages/daemon/src/tui/mirror/runtime/application-plugin-form.ts +312 -0
- package/packages/daemon/src/tui/mirror/runtime/application-root-v2.tsx +2 -2
- package/packages/daemon/src/tui/mirror/runtime/application-terminal-interaction-controller.ts +149 -128
- package/packages/daemon/src/tui/mirror/workspace/application-command-description.ts +44 -2
- package/packages/daemon/src/tui/mirror/workspace/palette-list-layout.ts +4 -0
- package/packages/tmux-bridge/src/runner.ts +3 -1
|
@@ -5,6 +5,7 @@ import { WindowSplitLayoutUnavailable } from "../terminal/mirror/window-split-au
|
|
|
5
5
|
import { streamTmuxInteractions } from "./tmux-server-interaction-events.ts";
|
|
6
6
|
import { mountTmuxServerNativeBackingRoute } from "./tmux-server-native-backing.ts";
|
|
7
7
|
import { mountTmuxServerEditorContextRoute } from "./tmux-server-editor-context.ts";
|
|
8
|
+
import { mountTmuxServerReaderRoutes } from "./tmux-server-reader.ts";
|
|
8
9
|
import { Hono, type Context } from "hono";
|
|
9
10
|
import { streamSSE } from "hono/streaming";
|
|
10
11
|
import { projectApplicationShellResource } from "./resources/application-shell.ts";
|
|
@@ -157,6 +158,11 @@ export function mountTmuxServerRoutes(app: Hono, options: TmuxServerRoutesOption
|
|
|
157
158
|
owners: options.owners,
|
|
158
159
|
readBody: (request) => boundedJson(request, 8 * 1024),
|
|
159
160
|
});
|
|
161
|
+
mountTmuxServerReaderRoutes(app, {
|
|
162
|
+
scopedPath: scoped,
|
|
163
|
+
owners: options.owners,
|
|
164
|
+
readBody: (request) => boundedJson(request, 8 * 1024),
|
|
165
|
+
});
|
|
160
166
|
app.post(
|
|
161
167
|
`${scoped}/native-browser`,
|
|
162
168
|
route(async (c) => {
|
|
@@ -2,10 +2,13 @@ import { requireSupportedTmuxVersion } from "./lib/tmux-version.ts";
|
|
|
2
2
|
import { resolveTmuxExecutable } from "./lib/tmux-client-execution.ts";
|
|
3
3
|
import { TMUX_SOCKET_PATH_ENV } from "./lib/runtime-namespace.ts";
|
|
4
4
|
import { execSync, execFileSync } from "node:child_process";
|
|
5
|
-
import { accessSync, constants, existsSync, mkdtempSync, rmSync } from "node:fs";
|
|
5
|
+
import { accessSync, constants, existsSync, mkdtempSync, realpathSync, rmSync } from "node:fs";
|
|
6
6
|
import { tmpdir } from "node:os";
|
|
7
|
-
import { resolve, dirname, join } from "node:path";
|
|
8
|
-
import {
|
|
7
|
+
import { delimiter, resolve, dirname, join } from "node:path";
|
|
8
|
+
import { resolveBundledTmux } from "./lib/bundled-tmux.ts";
|
|
9
|
+
import { readSnapshot } from "./tui/chrome/snapshot.ts";
|
|
10
|
+
import { isListableSession } from "./tui/team/sessions.ts";
|
|
11
|
+
import { nativeCommands, type NativeAnswer, type TmuxProbe } from "./lib/tmux-server-features.ts";
|
|
9
12
|
import { fileURLToPath } from "node:url";
|
|
10
13
|
import { getCurrentVersion, getUpdateStatus } from "./lib/update-check.ts";
|
|
11
14
|
import { installedSkillVersion } from "./lib/skill-sync.ts";
|
|
@@ -179,54 +182,20 @@ function check(
|
|
|
179
182
|
}
|
|
180
183
|
}
|
|
181
184
|
|
|
182
|
-
type TmuxProbe = (args: readonly string[]) => string;
|
|
183
|
-
const UNKNOWN = (command: string) => new RegExp(`^unknown command: ${command}$`, "mu");
|
|
184
|
-
|
|
185
|
-
type NativeAnswer = "full" | "stock" | "older" | "partial" | "no-server" | "unknown";
|
|
186
|
-
/** The same reading of the server as the mirror's: the stock resize when it refuses
|
|
187
|
-
* the split command by name, and full only with the split capability it requires. */
|
|
188
|
-
function nativeCommands(probe: TmuxProbe): NativeAnswer {
|
|
189
|
-
const ask = (command: string) => {
|
|
190
|
-
try {
|
|
191
|
-
return { output: probe([command, "-V"]) };
|
|
192
|
-
} catch (error) {
|
|
193
|
-
const failure = error as { stderr?: unknown; message?: string };
|
|
194
|
-
return { failure: String(failure.stderr ?? failure.message ?? "") };
|
|
195
|
-
}
|
|
196
|
-
};
|
|
197
|
-
const split = ask("tmux-ide-resize-split");
|
|
198
|
-
const events = ask("tmux-ide-events");
|
|
199
|
-
const failures = [split.failure, events.failure].filter((text) => text !== undefined);
|
|
200
|
-
if (failures.some((text) => /no server running|error connecting/u.test(text!)))
|
|
201
|
-
return "no-server";
|
|
202
|
-
const refused = (answer: { failure?: string }, command: string) =>
|
|
203
|
-
answer.failure !== undefined && UNKNOWN(command).test(answer.failure);
|
|
204
|
-
if (refused(split, "tmux-ide-resize-split")) {
|
|
205
|
-
if (refused(events, "tmux-ide-events")) return "stock";
|
|
206
|
-
// The journal answers: a tmux-ide tmux built before the split command.
|
|
207
|
-
if (events.output !== undefined) return "older";
|
|
208
|
-
return "unknown";
|
|
209
|
-
}
|
|
210
|
-
if (split.output !== undefined && events.output !== undefined) {
|
|
211
|
-
try {
|
|
212
|
-
return nativeSplitCapability(split.output) ? "full" : "partial";
|
|
213
|
-
} catch {
|
|
214
|
-
return "partial";
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
return failures.length === 2 ? "unknown" : "partial";
|
|
218
|
-
}
|
|
219
|
-
|
|
220
185
|
/** Reads the server a session would use. With none running, a throwaway private
|
|
221
186
|
* server shows what the resolved binary would start. */
|
|
222
187
|
export function tmuxFeaturesRow(probeServer: TmuxProbe, probeBinary: TmuxProbe): CheckResult {
|
|
223
188
|
let answer = nativeCommands(probeServer);
|
|
224
|
-
|
|
189
|
+
const running = answer !== "no-server";
|
|
190
|
+
if (!running) answer = nativeCommands(probeBinary);
|
|
191
|
+
// Only a running server can be moved; the command restarts it and restores its sessions.
|
|
192
|
+
const upgrade = running
|
|
193
|
+
? "; run `tmux-ide servers upgrade` to move it to tmux-ide's own tmux"
|
|
194
|
+
: "";
|
|
225
195
|
const details: Record<NativeAnswer, string> = {
|
|
226
196
|
full: "bundled tmux: full features",
|
|
227
|
-
stock:
|
|
228
|
-
older:
|
|
229
|
-
"older tmux-ide tmux: reduced features (restart it with tmux-ide's own tmux for every feature; that ends its sessions)",
|
|
197
|
+
stock: `stock tmux: reduced features${upgrade}`,
|
|
198
|
+
older: `older tmux-ide tmux: reduced features${upgrade}`,
|
|
230
199
|
partial: "this tmux has only some tmux-ide commands: no pane dividers",
|
|
231
200
|
"no-server": "could not read the tmux server's features",
|
|
232
201
|
unknown: "could not read the tmux server's features",
|
|
@@ -239,6 +208,108 @@ export function tmuxFeaturesRow(probeServer: TmuxProbe, probeBinary: TmuxProbe):
|
|
|
239
208
|
};
|
|
240
209
|
}
|
|
241
210
|
|
|
211
|
+
/**
|
|
212
|
+
* PURE — what a plain `tmux` typed in a shell runs. Informational: when it is
|
|
213
|
+
* not tmux-ide's own tmux and it starts the server, features drop (#766).
|
|
214
|
+
*/
|
|
215
|
+
export function plainTmuxRow(pathTmux: string | null, bundled: string | null): CheckResult {
|
|
216
|
+
const label = "plain `tmux` in your shell";
|
|
217
|
+
if (!pathTmux)
|
|
218
|
+
return {
|
|
219
|
+
label,
|
|
220
|
+
pass: true,
|
|
221
|
+
detail: "not on PATH (tmux-ide uses its own tmux)",
|
|
222
|
+
optional: true,
|
|
223
|
+
};
|
|
224
|
+
if (bundled && pathTmux === bundled)
|
|
225
|
+
return { label, pass: true, detail: `tmux-ide's own tmux (${pathTmux})`, optional: true };
|
|
226
|
+
return {
|
|
227
|
+
label,
|
|
228
|
+
pass: true,
|
|
229
|
+
detail: `${pathTmux}; if it starts the server, features drop (see docs: tmux upgrade)`,
|
|
230
|
+
optional: true,
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* PURE — is the crash snapshot fresh enough to restore from? It fails when it
|
|
238
|
+
* is missing a live session, or is over a day old while sessions run. Only the
|
|
239
|
+
* chrome updater writes it, and that runs only while a session is adopted.
|
|
240
|
+
*/
|
|
241
|
+
export function snapshotRow(
|
|
242
|
+
snapshot: { savedAt: string; sessions: readonly { name: string }[] } | null,
|
|
243
|
+
liveSessions: readonly string[] | null,
|
|
244
|
+
nowMs: number,
|
|
245
|
+
): CheckResult {
|
|
246
|
+
const label = "crash snapshot (restore)";
|
|
247
|
+
const hint =
|
|
248
|
+
"Only the chrome updater writes it, while a session is adopted: `tmux-ide adopt --all` starts it";
|
|
249
|
+
if (!snapshot) {
|
|
250
|
+
return liveSessions?.length
|
|
251
|
+
? { label, pass: false, detail: `none yet. ${hint}`, optional: true }
|
|
252
|
+
: { label, pass: true, detail: "none yet (no sessions running)", optional: true };
|
|
253
|
+
}
|
|
254
|
+
const ageMs = nowMs - Date.parse(snapshot.savedAt);
|
|
255
|
+
const age =
|
|
256
|
+
ageMs >= DAY_MS
|
|
257
|
+
? `${Math.floor(ageMs / DAY_MS)} days old`
|
|
258
|
+
: `${Math.max(0, Math.round(ageMs / 60_000))} min old`;
|
|
259
|
+
const saved = new Set(snapshot.sessions.map((session) => session.name));
|
|
260
|
+
const missing = (liveSessions ?? []).filter((name) => !saved.has(name));
|
|
261
|
+
if (missing.length)
|
|
262
|
+
return {
|
|
263
|
+
label,
|
|
264
|
+
pass: false,
|
|
265
|
+
detail: `${age}; misses ${missing.length} running session${missing.length === 1 ? "" : "s"} (${missing.slice(0, 5).join(", ")}${missing.length > 5 ? ", …" : ""}). ${hint}`,
|
|
266
|
+
optional: true,
|
|
267
|
+
};
|
|
268
|
+
return { label, pass: true, detail: `${age}, covers every running session`, optional: true };
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function firstOnPath(name: string): string | null {
|
|
272
|
+
for (const directory of (process.env.PATH ?? "").split(delimiter)) {
|
|
273
|
+
if (!directory) continue;
|
|
274
|
+
const candidate = join(directory, name);
|
|
275
|
+
try {
|
|
276
|
+
accessSync(candidate, constants.X_OK);
|
|
277
|
+
return realpathSync(candidate);
|
|
278
|
+
} catch {
|
|
279
|
+
// Try the next PATH entry.
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return null;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function safeBundledTmux(): string | null {
|
|
286
|
+
try {
|
|
287
|
+
const bundled = resolveBundledTmux();
|
|
288
|
+
return bundled ? realpathSync(bundled) : null;
|
|
289
|
+
} catch {
|
|
290
|
+
return null;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/** The sessions the snapshot should hold, on the server doctor reads; null when unreadable. */
|
|
295
|
+
function liveListableSessions(): string[] | null {
|
|
296
|
+
try {
|
|
297
|
+
const socketPath = process.env[TMUX_SOCKET_PATH_ENV];
|
|
298
|
+
const socketName = process.env.TMUX_IDE_TMUX_SOCKET_NAME;
|
|
299
|
+
const server = socketPath ? ["-S", socketPath] : socketName ? ["-L", socketName] : [];
|
|
300
|
+
return execFileSync(
|
|
301
|
+
resolveTmuxExecutable(),
|
|
302
|
+
[...server, "list-sessions", "-F", "#{session_name}"],
|
|
303
|
+
{ encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], timeout: 3000 },
|
|
304
|
+
)
|
|
305
|
+
.split("\n")
|
|
306
|
+
.filter((name) => name && isListableSession(name));
|
|
307
|
+
} catch (error) {
|
|
308
|
+
const text = String((error as { stderr?: unknown }).stderr ?? "");
|
|
309
|
+
return /no server running|error connecting/u.test(text) ? [] : null;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
242
313
|
function runTmuxFeatureProbes(executable: string): CheckResult {
|
|
243
314
|
const run =
|
|
244
315
|
(prefix: readonly string[]): TmuxProbe =>
|
|
@@ -335,6 +406,8 @@ export async function doctor({
|
|
|
335
406
|
} catch {
|
|
336
407
|
// The tmux version row above already reports a missing executable.
|
|
337
408
|
}
|
|
409
|
+
checks.push(plainTmuxRow(firstOnPath("tmux"), safeBundledTmux()));
|
|
410
|
+
checks.push(snapshotRow(readSnapshot(), liveListableSessions(), Date.now()));
|
|
338
411
|
|
|
339
412
|
checks.push(nodeVersionRow(process.versions.node));
|
|
340
413
|
|
|
@@ -254,3 +254,22 @@ export async function tryDispatchAction<Name extends ActionName>(
|
|
|
254
254
|
}
|
|
255
255
|
return null;
|
|
256
256
|
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* The running canonical daemon's base URL and owner token, for CLI commands
|
|
260
|
+
* that call daemon routes other than the action dispatcher.
|
|
261
|
+
*/
|
|
262
|
+
export async function resolveCanonicalDaemonEndpoint(
|
|
263
|
+
options: { cwd?: string; autostart?: boolean } = {},
|
|
264
|
+
): Promise<{ baseUrl: string; ownerToken: string | null } | null> {
|
|
265
|
+
const bridgeDeps = deps;
|
|
266
|
+
return resolveCanonicalDaemon(bridgeDeps, {
|
|
267
|
+
autostart: options.autostart ?? true,
|
|
268
|
+
cwd: options.cwd ?? bridgeDeps.cwd(),
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** `fetch`, replaceable in tests through {@link __setCliActionBridgeDepsForTests}. */
|
|
273
|
+
export function cliBridgeFetch(): typeof fetch {
|
|
274
|
+
return deps.fetch;
|
|
275
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { basename, dirname, extname, join } from "node:path";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The argv prefix that runs this tmux-ide's CLI: the bundled `bin/cli.js`,
|
|
6
|
+
* or `bin/cli.ts` under the runtime's loader flags when run from source.
|
|
7
|
+
* `here` must sit two directories below `packages/daemon/src`'s parent.
|
|
8
|
+
*/
|
|
9
|
+
export function resolveCliCommand(
|
|
10
|
+
here: string = fileURLToPath(import.meta.url),
|
|
11
|
+
runtime: { execPath: string; execArgv: readonly string[] } = process,
|
|
12
|
+
): string[] {
|
|
13
|
+
if (basename(here) === "cli.js") return [runtime.execPath, here];
|
|
14
|
+
const cli = join(dirname(here), "..", "..", "..", "..", "bin", `cli${extname(here)}`);
|
|
15
|
+
return [runtime.execPath, ...runtime.execArgv, cli];
|
|
16
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createPaneReaderResolver } from "./tmux-pane-reader.ts";
|
|
1
2
|
import { createNativeBrowserResolver } from "./tmux-native-browser.ts";
|
|
2
3
|
import { createNativeSurfaceContextResolver } from "./native-surface-context.ts";
|
|
3
4
|
import { NativeSurfaceProviderRegistry } from "../terminal/graphics/native-surface-provider.ts";
|
|
@@ -168,6 +169,8 @@ import { setActivationBackend, type ProjectActivationOptions } from "./active-pr
|
|
|
168
169
|
import { readOrMintEnvironmentId } from "./environment-identity.ts";
|
|
169
170
|
import { captureDaemonProvenance } from "./daemon-provenance.ts";
|
|
170
171
|
import { registerLogSecret, setLogIdentity } from "./log.ts";
|
|
172
|
+
import { createDaemonPluginSupervisor, pluginsEnabled } from "../plugins/daemon-plugins.ts";
|
|
173
|
+
import type { PluginSupervisor } from "../plugins/supervisor.ts";
|
|
171
174
|
import {
|
|
172
175
|
canonicalDaemonUrl,
|
|
173
176
|
getCanonicalDaemonInfoPath,
|
|
@@ -249,6 +252,11 @@ export interface EmbeddedDaemonOptions {
|
|
|
249
252
|
silent?: boolean;
|
|
250
253
|
/** Diagnostic product version advertised by daemon.json. */
|
|
251
254
|
productVersion?: string;
|
|
255
|
+
/**
|
|
256
|
+
* @internal Run the plugin host for `~/.tmux-ide/init.lua` and plugins.
|
|
257
|
+
* Only the canonical headless owner sets this; `TMUX_IDE_PLUGINS=0` overrides it.
|
|
258
|
+
*/
|
|
259
|
+
plugins?: boolean;
|
|
252
260
|
/** @deprecated Use bindHostname. */
|
|
253
261
|
hostname?: string;
|
|
254
262
|
}
|
|
@@ -834,8 +842,10 @@ async function startHttpServer({
|
|
|
834
842
|
fleetPreviewCapture,
|
|
835
843
|
sessionRuntimeRegistry,
|
|
836
844
|
serverOwners,
|
|
845
|
+
pluginBackend,
|
|
837
846
|
}: {
|
|
838
847
|
serverOwners: Awaited<ReturnType<typeof createEmbeddedTmuxServerOwners>>;
|
|
848
|
+
pluginBackend: PluginSupervisor | null;
|
|
839
849
|
sessionName: string;
|
|
840
850
|
requestedPort: number;
|
|
841
851
|
bindHostname: string;
|
|
@@ -939,6 +949,7 @@ async function startHttpServer({
|
|
|
939
949
|
catalogLiveSessions,
|
|
940
950
|
catalogFleet,
|
|
941
951
|
fleetPreviewCapture,
|
|
952
|
+
pluginBackend,
|
|
942
953
|
});
|
|
943
954
|
mountTerminalNativeBackingRoute(app, {
|
|
944
955
|
ownerToken: localBypassToken ?? null,
|
|
@@ -1455,6 +1466,29 @@ async function startEmbeddedDaemonGeneration(
|
|
|
1455
1466
|
setFleetFactsObserverDiagnostics(null);
|
|
1456
1467
|
await runtimeTraceWriter?.close(incomplete);
|
|
1457
1468
|
};
|
|
1469
|
+
// Plugins reach the daemon's own routes over loopback with the owner token.
|
|
1470
|
+
const pluginEndpoint = canonicalDaemonUrl(
|
|
1471
|
+
"http",
|
|
1472
|
+
bindHostname === "0.0.0.0" || bindHostname === "::" ? "127.0.0.1" : bindHostname,
|
|
1473
|
+
port,
|
|
1474
|
+
);
|
|
1475
|
+
// Created now so its routes mount with the server; it spawns nothing until
|
|
1476
|
+
// start(), which runs once this generation is published.
|
|
1477
|
+
const pluginSupervisor =
|
|
1478
|
+
opts.plugins && pluginsEnabled()
|
|
1479
|
+
? createDaemonPluginSupervisor({
|
|
1480
|
+
productVersion,
|
|
1481
|
+
secrets: [localBypassToken, authToken ?? null],
|
|
1482
|
+
endpoint: () =>
|
|
1483
|
+
pluginEndpoint ? { baseUrl: pluginEndpoint, ownerToken: localBypassToken } : null,
|
|
1484
|
+
tmux: (args) => fleetFactsTmuxRunner(args, AbortSignal.timeout(5000)),
|
|
1485
|
+
onCatalogChange: () =>
|
|
1486
|
+
broadcastResourceChanged(
|
|
1487
|
+
{ workspaceName: null, resource: "command-catalog" },
|
|
1488
|
+
instanceId,
|
|
1489
|
+
),
|
|
1490
|
+
})
|
|
1491
|
+
: null;
|
|
1458
1492
|
let startedServer: Awaited<ReturnType<typeof startHttpServer>>;
|
|
1459
1493
|
try {
|
|
1460
1494
|
const selector = tmuxAuthority.socketSelector;
|
|
@@ -1963,6 +1997,22 @@ async function startEmbeddedDaemonGeneration(
|
|
|
1963
1997
|
throw new Error("Default native browser owner is unavailable");
|
|
1964
1998
|
},
|
|
1965
1999
|
}),
|
|
2000
|
+
paneReader: createPaneReaderResolver({
|
|
2001
|
+
generation: instanceId,
|
|
2002
|
+
run: fleetFactsTmuxRunner,
|
|
2003
|
+
registry: workspaceRegistry,
|
|
2004
|
+
catalog: createNativeTmuxServerCatalog(workspaceRegistry, fleetFactsTmuxRunner),
|
|
2005
|
+
inventory: terminalInventoryRuntime,
|
|
2006
|
+
assertOpen: () => {
|
|
2007
|
+
if (
|
|
2008
|
+
defaultEditorContextRetired ||
|
|
2009
|
+
serverOwners?.defaultRetired ||
|
|
2010
|
+
!initialNativeServerIdentity ||
|
|
2011
|
+
terminalInventoryRuntime?.lifecycleState() !== "ready"
|
|
2012
|
+
)
|
|
2013
|
+
throw new Error("Default reader owner is unavailable");
|
|
2014
|
+
},
|
|
2015
|
+
}),
|
|
1966
2016
|
catalog: createNativeTmuxServerCatalog(workspaceRegistry, fleetFactsTmuxRunner),
|
|
1967
2017
|
readWindowSplitLayout: async (workspaceName, input) => {
|
|
1968
2018
|
const target = WindowLinkTargetSchemaZ.parse(input);
|
|
@@ -2080,6 +2130,7 @@ async function startEmbeddedDaemonGeneration(
|
|
|
2080
2130
|
setFleetFactsTmuxRunner(fleetFactsTmuxRunner);
|
|
2081
2131
|
startedServer = await startHttpServer({
|
|
2082
2132
|
serverOwners,
|
|
2133
|
+
pluginBackend: pluginSupervisor,
|
|
2083
2134
|
sessionName,
|
|
2084
2135
|
requestedPort: port,
|
|
2085
2136
|
bindHostname,
|
|
@@ -2211,6 +2262,7 @@ async function startEmbeddedDaemonGeneration(
|
|
|
2211
2262
|
);
|
|
2212
2263
|
};
|
|
2213
2264
|
const abortStartedServer = async (): Promise<void> => {
|
|
2265
|
+
await pluginSupervisor?.stop().catch(() => {});
|
|
2214
2266
|
await serverOwners?.dispose();
|
|
2215
2267
|
setFleetFactsTmuxRunner(null);
|
|
2216
2268
|
const terminalFailures = [
|
|
@@ -2315,6 +2367,12 @@ async function startEmbeddedDaemonGeneration(
|
|
|
2315
2367
|
throw error;
|
|
2316
2368
|
}
|
|
2317
2369
|
|
|
2370
|
+
// Plugins load after publication, in the background: a slow or broken
|
|
2371
|
+
// plugin never delays or fails daemon startup.
|
|
2372
|
+
void pluginSupervisor?.start().catch((error: unknown) => {
|
|
2373
|
+
console.warn(`[daemon] plugins failed to start: ${(error as Error).message}`);
|
|
2374
|
+
});
|
|
2375
|
+
|
|
2318
2376
|
let stopped = false;
|
|
2319
2377
|
let stopping: Promise<void> | null = null;
|
|
2320
2378
|
let stopSelf: (() => void) | null = null;
|
|
@@ -2474,6 +2532,7 @@ async function startEmbeddedDaemonGeneration(
|
|
|
2474
2532
|
};
|
|
2475
2533
|
try {
|
|
2476
2534
|
stopped = true;
|
|
2535
|
+
await capture(() => pluginSupervisor?.stop());
|
|
2477
2536
|
await capture(() => setActivationBackend(null));
|
|
2478
2537
|
await capture(() => sessionMonitor?.stop());
|
|
2479
2538
|
paneSourceCredentials.dispose();
|
|
@@ -366,6 +366,7 @@ async function runHeadlessDaemonGeneration(
|
|
|
366
366
|
try {
|
|
367
367
|
handle = await deps.startEmbeddedDaemon({
|
|
368
368
|
launcher: "headless",
|
|
369
|
+
plugins: true,
|
|
369
370
|
...(options.supervisionId
|
|
370
371
|
? { supervisionId: options.supervisionId, predecessor: lifecycle.predecessor }
|
|
371
372
|
: {}),
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import {
|
|
3
|
+
closeSync,
|
|
4
|
+
constants,
|
|
5
|
+
fstatSync,
|
|
6
|
+
lstatSync,
|
|
7
|
+
openSync,
|
|
8
|
+
readSync,
|
|
9
|
+
realpathSync,
|
|
10
|
+
statSync,
|
|
11
|
+
type BigIntStats,
|
|
12
|
+
} from "node:fs";
|
|
13
|
+
import { dirname, extname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
14
|
+
import {
|
|
15
|
+
PANE_READER_ASSET_MAX_BYTES,
|
|
16
|
+
PANE_READER_MAX_BYTES,
|
|
17
|
+
PANE_READER_PAGE_BYTES,
|
|
18
|
+
hasControlCharacter,
|
|
19
|
+
type PaneReaderPage,
|
|
20
|
+
} from "@tmux-ide/contracts";
|
|
21
|
+
import { isMarkdownPath } from "../terminal/protocol/reader-option.ts";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* File access for the native Markdown reader (card #756). Pure of tmux: the
|
|
25
|
+
* caller has already proven which pane and which file. Nothing here touches
|
|
26
|
+
* the network; a URL is always refused.
|
|
27
|
+
*/
|
|
28
|
+
export type ReaderPageResult =
|
|
29
|
+
| { state: "unchanged"; revision: string }
|
|
30
|
+
| { state: "ok"; revision: string; size: number; offset: number; chunk: string }
|
|
31
|
+
| { state: "changed" }
|
|
32
|
+
| { state: "too_large"; revision: string; size: number }
|
|
33
|
+
| { state: "missing" };
|
|
34
|
+
|
|
35
|
+
export type ReaderAssetMime =
|
|
36
|
+
| "image/png"
|
|
37
|
+
| "image/jpeg"
|
|
38
|
+
| "image/gif"
|
|
39
|
+
| "image/webp"
|
|
40
|
+
| "image/svg+xml";
|
|
41
|
+
export type ReaderAssetResult =
|
|
42
|
+
| {
|
|
43
|
+
state: "ok";
|
|
44
|
+
mime: ReaderAssetMime;
|
|
45
|
+
revision: string;
|
|
46
|
+
size: number;
|
|
47
|
+
offset: number;
|
|
48
|
+
chunk: string;
|
|
49
|
+
}
|
|
50
|
+
| { state: "changed" | "refused" | "missing" | "too_large" };
|
|
51
|
+
|
|
52
|
+
const IMAGE_MIME: Readonly<Record<string, ReaderAssetMime>> = {
|
|
53
|
+
".png": "image/png",
|
|
54
|
+
".jpg": "image/jpeg",
|
|
55
|
+
".jpeg": "image/jpeg",
|
|
56
|
+
".gif": "image/gif",
|
|
57
|
+
".webp": "image/webp",
|
|
58
|
+
".svg": "image/svg+xml",
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
function revisionOf(stat: BigIntStats): string {
|
|
62
|
+
return createHash("sha256")
|
|
63
|
+
.update(`${stat.dev}:${stat.ino}:${stat.size}:${stat.mtimeNs}:${stat.ctimeNs}`)
|
|
64
|
+
.digest("hex")
|
|
65
|
+
.slice(0, 32);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function missingError(error: unknown): boolean {
|
|
69
|
+
const code = (error as NodeJS.ErrnoException | null)?.code;
|
|
70
|
+
return code === "ENOENT" || code === "ENOTDIR" || code === "ELOOP" || code === "EISDIR";
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function realpathOrNull(path: string): string | null {
|
|
74
|
+
try {
|
|
75
|
+
return realpathSync(path);
|
|
76
|
+
} catch (error) {
|
|
77
|
+
if (missingError(error)) return null;
|
|
78
|
+
throw error;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Equal to or below `root`, compared by path segments rather than by string prefix. */
|
|
83
|
+
export function isInside(root: string, path: string): boolean {
|
|
84
|
+
const rel = relative(root, path);
|
|
85
|
+
return rel === "" || (!isAbsolute(rel) && rel !== ".." && !rel.startsWith(`..${sep}`));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The nearest ancestor of `directory` holding a `.git` entry; no git process is run. */
|
|
89
|
+
export function gitRoot(directory: string): string | null {
|
|
90
|
+
for (let current = directory; ; ) {
|
|
91
|
+
try {
|
|
92
|
+
lstatSync(join(current, ".git"));
|
|
93
|
+
return current;
|
|
94
|
+
} catch {
|
|
95
|
+
/* keep walking up */
|
|
96
|
+
}
|
|
97
|
+
const parent = dirname(current);
|
|
98
|
+
if (parent === current) return null;
|
|
99
|
+
current = parent;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
type Opened = { fd: number; stat: BigIntStats };
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Steps a test can wrap, to change a file between the steps of one read.
|
|
107
|
+
* Production passes none.
|
|
108
|
+
*/
|
|
109
|
+
export type ReaderFileSeams = {
|
|
110
|
+
readonly afterOpen?: () => void;
|
|
111
|
+
readonly read?: (fd: number, offset: number, size: number, real: typeof readChunk) => Buffer;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const NO_SEAMS: ReaderFileSeams = Object.freeze({});
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Opens `real` (already resolved) without following a swapped-in final
|
|
118
|
+
* symlink, and only as a regular file. Then proves the descriptor is still the
|
|
119
|
+
* file at that real path: a fresh stat of the path, following every symlink on
|
|
120
|
+
* it, names the same device and inode as the open descriptor. A path or a
|
|
121
|
+
* folder on it swapped between resolving and opening is refused ("swapped").
|
|
122
|
+
* No separate realpath check is needed: whatever the path now resolves to,
|
|
123
|
+
* the stat proves the open file is the one it leads to.
|
|
124
|
+
*/
|
|
125
|
+
function openRegular(real: string, seams: ReaderFileSeams): Opened | "swapped" | null {
|
|
126
|
+
let fd: number;
|
|
127
|
+
try {
|
|
128
|
+
fd = openSync(real, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
129
|
+
} catch (error) {
|
|
130
|
+
if (missingError(error)) return null;
|
|
131
|
+
throw error;
|
|
132
|
+
}
|
|
133
|
+
const stat = fstatSync(fd, { bigint: true });
|
|
134
|
+
if (!stat.isFile()) {
|
|
135
|
+
closeSync(fd);
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
seams.afterOpen?.();
|
|
139
|
+
let now: BigIntStats | null = null;
|
|
140
|
+
try {
|
|
141
|
+
now = statSync(real, { bigint: true });
|
|
142
|
+
} catch (error) {
|
|
143
|
+
if (!missingError(error)) {
|
|
144
|
+
closeSync(fd);
|
|
145
|
+
throw error;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
if (!now || now.dev !== stat.dev || now.ino !== stat.ino) {
|
|
149
|
+
closeSync(fd);
|
|
150
|
+
return "swapped";
|
|
151
|
+
}
|
|
152
|
+
return { fd, stat };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function readChunk(fd: number, offset: number, size: number): Buffer {
|
|
156
|
+
const length = Math.max(0, Math.min(PANE_READER_PAGE_BYTES, size - offset));
|
|
157
|
+
const buffer = Buffer.alloc(length);
|
|
158
|
+
let done = 0;
|
|
159
|
+
while (done < length) {
|
|
160
|
+
const read = readSync(fd, buffer, done, length - done, offset + done);
|
|
161
|
+
if (read === 0) break;
|
|
162
|
+
done += read;
|
|
163
|
+
}
|
|
164
|
+
return buffer.subarray(0, done);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
type Paged =
|
|
168
|
+
| { state: "ok"; revision: string; size: number; offset: number; chunk: string }
|
|
169
|
+
| { state: "changed" }
|
|
170
|
+
| { state: "too_large"; revision: string; size: number }
|
|
171
|
+
| { state: "unchanged"; revision: string };
|
|
172
|
+
|
|
173
|
+
/** One page of `opened`, re-checking after the read that the file did not change. */
|
|
174
|
+
function page(
|
|
175
|
+
opened: Opened,
|
|
176
|
+
limit: number,
|
|
177
|
+
have: string | null,
|
|
178
|
+
request: PaneReaderPage | null,
|
|
179
|
+
seams: ReaderFileSeams,
|
|
180
|
+
): Paged {
|
|
181
|
+
const revision = revisionOf(opened.stat);
|
|
182
|
+
const size = Number(opened.stat.size);
|
|
183
|
+
if (size > limit) return { state: "too_large", revision, size };
|
|
184
|
+
if (request) {
|
|
185
|
+
if (request.revision !== revision || request.offset > size) return { state: "changed" };
|
|
186
|
+
} else if (have === revision) return { state: "unchanged", revision };
|
|
187
|
+
const offset = request?.offset ?? 0;
|
|
188
|
+
const bytes = seams.read
|
|
189
|
+
? seams.read(opened.fd, offset, size, readChunk)
|
|
190
|
+
: readChunk(opened.fd, offset, size);
|
|
191
|
+
const after = fstatSync(opened.fd, { bigint: true });
|
|
192
|
+
if (
|
|
193
|
+
revisionOf(after) !== revision ||
|
|
194
|
+
bytes.length !== Math.min(PANE_READER_PAGE_BYTES, size - offset)
|
|
195
|
+
)
|
|
196
|
+
return { state: "changed" };
|
|
197
|
+
return { state: "ok", revision, size, offset, chunk: bytes.toString("base64") };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** A page of the Markdown file at `path`; its real path must still be Markdown. */
|
|
201
|
+
export function readReaderPage(
|
|
202
|
+
path: string,
|
|
203
|
+
have: string | null,
|
|
204
|
+
request: PaneReaderPage | null,
|
|
205
|
+
seams: ReaderFileSeams = NO_SEAMS,
|
|
206
|
+
): ReaderPageResult {
|
|
207
|
+
// A whole-file read retries once if the file changed underneath it.
|
|
208
|
+
for (let attempt = 0; ; attempt++) {
|
|
209
|
+
const real = realpathOrNull(path);
|
|
210
|
+
if (!real || !isMarkdownPath(real)) return { state: "missing" };
|
|
211
|
+
const opened = openRegular(real, seams);
|
|
212
|
+
if (!opened || opened === "swapped") {
|
|
213
|
+
// A document swapped mid-open reads again once, then reports changed.
|
|
214
|
+
if (opened === "swapped" && !request && attempt === 0) continue;
|
|
215
|
+
return opened === "swapped" ? { state: "changed" } : { state: "missing" };
|
|
216
|
+
}
|
|
217
|
+
try {
|
|
218
|
+
const result = page(opened, PANE_READER_MAX_BYTES, have, request, seams);
|
|
219
|
+
if (result.state !== "changed" || request || attempt > 0) return result;
|
|
220
|
+
} finally {
|
|
221
|
+
closeSync(opened.fd);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const SCHEME = /^[a-z][a-z0-9+.-]*:/iu;
|
|
227
|
+
|
|
228
|
+
/** The decoded relative path `src` names, or null when the reader must not follow it. */
|
|
229
|
+
export function localImageReference(src: string): string | null {
|
|
230
|
+
if (src.includes("\0") || SCHEME.test(src) || src.startsWith("//") || src.startsWith("\\"))
|
|
231
|
+
return null;
|
|
232
|
+
const bare = src.split(/[?#]/u, 1)[0] ?? "";
|
|
233
|
+
let decoded: string;
|
|
234
|
+
try {
|
|
235
|
+
decoded = decodeURIComponent(bare);
|
|
236
|
+
} catch {
|
|
237
|
+
return null;
|
|
238
|
+
}
|
|
239
|
+
if (
|
|
240
|
+
!decoded ||
|
|
241
|
+
hasControlCharacter(decoded) ||
|
|
242
|
+
decoded.startsWith("/") ||
|
|
243
|
+
decoded.startsWith("~") ||
|
|
244
|
+
decoded.startsWith("\\") ||
|
|
245
|
+
isAbsolute(decoded)
|
|
246
|
+
)
|
|
247
|
+
return null;
|
|
248
|
+
return decoded;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function imageMime(path: string): ReaderAssetMime | null {
|
|
252
|
+
return IMAGE_MIME[extname(path).toLowerCase()] ?? null;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* A page of the image `src` names, relative to the Markdown file at `markdown`.
|
|
257
|
+
* Allowed only inside the file's git repository, or its folder when there is
|
|
258
|
+
* none, after every symlink is resolved.
|
|
259
|
+
*/
|
|
260
|
+
export function readReaderAssetPage(
|
|
261
|
+
markdown: string,
|
|
262
|
+
src: string,
|
|
263
|
+
request: PaneReaderPage | null,
|
|
264
|
+
seams: ReaderFileSeams = NO_SEAMS,
|
|
265
|
+
): ReaderAssetResult {
|
|
266
|
+
const reference = localImageReference(src);
|
|
267
|
+
if (reference === null) return { state: "refused" };
|
|
268
|
+
const document = realpathOrNull(markdown);
|
|
269
|
+
if (!document || !isMarkdownPath(document)) return { state: "missing" };
|
|
270
|
+
const base = dirname(document);
|
|
271
|
+
const root = gitRoot(base) ?? base;
|
|
272
|
+
const candidate = resolve(base, reference);
|
|
273
|
+
// Refuse outside paths before touching the file system, so the answer never
|
|
274
|
+
// says whether a file outside the root exists.
|
|
275
|
+
if (!isInside(root, candidate) || !imageMime(candidate)) return { state: "refused" };
|
|
276
|
+
const real = realpathOrNull(candidate);
|
|
277
|
+
if (!real) return { state: "missing" };
|
|
278
|
+
const mime = imageMime(real);
|
|
279
|
+
if (!isInside(root, real) || !mime) return { state: "refused" };
|
|
280
|
+
const opened = openRegular(real, seams);
|
|
281
|
+
if (opened === "swapped") return { state: "refused" };
|
|
282
|
+
if (!opened) return { state: "missing" };
|
|
283
|
+
try {
|
|
284
|
+
// The name as written must still lead to the file that was opened.
|
|
285
|
+
if (realpathOrNull(candidate) !== real) return { state: "refused" };
|
|
286
|
+
const result = page(opened, PANE_READER_ASSET_MAX_BYTES, null, request, seams);
|
|
287
|
+
if (result.state === "unchanged") return { state: "changed" };
|
|
288
|
+
if (result.state === "too_large") return { state: "too_large" };
|
|
289
|
+
if (result.state === "changed") return result;
|
|
290
|
+
return { ...result, mime };
|
|
291
|
+
} finally {
|
|
292
|
+
closeSync(opened.fd);
|
|
293
|
+
}
|
|
294
|
+
}
|