dsh-remote-vs 0.24.0 → 0.26.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +50 -16
- package/README.md +40 -15
- package/THIRD-PARTY-NOTICES.md +188 -0
- package/lib/client.js +6512 -77887
- package/lib/index.js +1461 -1024
- package/lib/p2-router-fs.js +1 -1
- package/lib/types/client/CodeEditor.d.ts +25 -0
- package/lib/types/client/EditorPane.d.ts +16 -5
- package/lib/types/client/FloatingPlaceholder.d.ts +16 -5
- package/lib/types/client/RemoteEditorView.d.ts +13 -10
- package/lib/types/client/RemoteExplorer.d.ts +53 -0
- package/lib/types/client/RemoteRoot.d.ts +5 -5
- package/lib/types/client/TerminalSnippets.d.ts +36 -0
- package/lib/types/client/TerminalView.d.ts +1 -1
- package/lib/types/client/XTerm.d.ts +33 -0
- package/lib/types/client/api.d.ts +37 -2
- package/lib/types/client/changes/review-state.d.ts +19 -0
- package/lib/types/client/connection.d.ts +16 -0
- package/lib/types/client/editor-capability.d.ts +27 -0
- package/lib/types/client/i18n.d.ts +1 -1
- package/lib/types/client/request-gate.d.ts +110 -0
- package/lib/types/client/scm/DiffView.d.ts +45 -7
- package/lib/types/client/scm/actions.d.ts +31 -3
- package/lib/types/client/store.d.ts +112 -4
- package/lib/types/client/terminal-input.d.ts +23 -0
- package/lib/types/client/terminal-snippets.d.ts +86 -0
- package/lib/types/client/vsc/FindWidget.d.ts +6 -0
- package/lib/types/client/vsc/breadcrumb.d.ts +30 -0
- package/lib/types/client/vsc/change-marks.d.ts +16 -6
- package/lib/types/client/vsc/editor-caret.d.ts +32 -0
- package/lib/types/client/vsc/editor-keymap.d.ts +35 -1
- package/lib/types/client/vsc/editor-langs.d.ts +9 -0
- package/lib/types/client/vsc/editor-settings.d.ts +55 -0
- package/lib/types/client/vsc/image-preview.d.ts +36 -0
- package/lib/types/client/vsc/kit.d.ts +13 -0
- package/lib/types/client/vsc/save-shortcut.d.ts +21 -0
- package/lib/types/client/vsc/terminal-clipboard.d.ts +15 -0
- package/lib/types/client/vsc/terminal-line.d.ts +25 -0
- package/lib/types/client/vsc/terminal-links.d.ts +62 -0
- package/lib/types/client/vsc/terminal-theme.d.ts +48 -0
- package/lib/types/client/vsc/terminal-writer.d.ts +52 -0
- package/lib/types/forward.d.ts +12 -0
- package/lib/types/fs-ops.d.ts +42 -0
- package/lib/types/http.d.ts +2 -0
- package/lib/types/keygen.d.ts +9 -0
- package/lib/types/monitor.d.ts +8 -0
- package/lib/types/notices.d.ts +16 -1
- package/lib/types/p2/helper-rpc.d.ts +45 -0
- package/lib/types/pool.d.ts +25 -0
- package/lib/types/routes.d.ts +2 -0
- package/lib/types/store.d.ts +7 -0
- package/lib/types/terminal.d.ts +33 -1
- package/lib/types/view-settings.d.ts +5 -0
- package/lib/{worlds-B7gEDBzC.js → worlds-RG8K5eom.js} +113 -15
- package/package.json +2 -1
- package/src/client/CodeEditor.tsx +205 -10
- package/src/client/DirectoryFlow.tsx +16 -0
- package/src/client/DoctorPanel.tsx +70 -7
- package/src/client/EditorPane.tsx +545 -118
- package/src/client/FloatingPlaceholder.tsx +34 -5
- package/src/client/HostsView.tsx +12 -3
- package/src/client/RemoteDirectoryPicker.tsx +12 -0
- package/src/client/RemoteEditorView.tsx +14 -11
- package/src/client/RemoteExplorer.tsx +929 -40
- package/src/client/RemoteRoot.tsx +167 -147
- package/src/client/SearchView.tsx +8 -0
- package/src/client/TerminalSnippets.tsx +432 -0
- package/src/client/TerminalView.tsx +50 -1
- package/src/client/XTerm.tsx +228 -58
- package/src/client/api.ts +106 -13
- package/src/client/auto-open-remote.ts +11 -9
- package/src/client/changes/ChangesDock.tsx +188 -6
- package/src/client/changes/changes.css +10 -3
- package/src/client/changes/review-state.ts +34 -0
- package/src/client/connection.ts +29 -0
- package/src/client/downloads.ts +49 -4
- package/src/client/editor-capability.ts +42 -0
- package/src/client/i18n.ts +228 -12
- package/src/client/index.tsx +22 -6
- package/src/client/request-gate.ts +221 -0
- package/src/client/scm/CommitPane.tsx +20 -2
- package/src/client/scm/DiffView.tsx +343 -117
- package/src/client/scm/HistoryPanel.tsx +23 -2
- package/src/client/scm/RepoPicker.tsx +1 -1
- package/src/client/scm/ScmPanel.tsx +391 -87
- package/src/client/scm/actions.ts +95 -12
- package/src/client/store.tsx +537 -22
- package/src/client/terminal-input.ts +44 -0
- package/src/client/terminal-snippets.ts +216 -0
- package/src/client/ui.tsx +4 -0
- package/src/client/vsc/FindWidget.tsx +24 -9
- package/src/client/vsc/breadcrumb.tsx +339 -0
- package/src/client/vsc/change-marks.ts +93 -44
- package/src/client/vsc/editor-caret.ts +30 -0
- package/src/client/vsc/editor-keymap.ts +59 -1
- package/src/client/vsc/editor-langs.ts +20 -0
- package/src/client/vsc/editor-settings.ts +146 -0
- package/src/client/vsc/editor-theme.ts +4 -5
- package/src/client/vsc/file-icons.tsx +17 -3
- package/src/client/vsc/image-preview.tsx +110 -0
- package/src/client/vsc/kit.tsx +46 -10
- package/src/client/vsc/save-shortcut.ts +47 -1
- package/src/client/vsc/terminal-clipboard.ts +19 -0
- package/src/client/vsc/terminal-line.ts +101 -0
- package/src/client/vsc/terminal-links.ts +166 -0
- package/src/client/vsc/terminal-theme.ts +97 -0
- package/src/client/vsc/terminal-writer.ts +112 -0
- package/src/client/vsc/vsc.css +1078 -181
- package/src/forward.ts +47 -9
- package/src/fs-ops.ts +124 -4
- package/src/git-ops.ts +20 -3
- package/src/http.ts +35 -4
- package/src/keygen.ts +55 -4
- package/src/monitor.ts +13 -0
- package/src/notices.ts +23 -1
- package/src/p2/helper-rpc.ts +113 -7
- package/src/p2/worlds.ts +14 -1
- package/src/pool.ts +99 -27
- package/src/routes.ts +212 -43
- package/src/sftp.ts +51 -14
- package/src/store.ts +10 -0
- package/src/terminal.ts +225 -24
- package/src/view-settings.ts +50 -10
- package/lib/types/client/Dialog.d.ts +0 -14
- package/src/client/Dialog.tsx +0 -76
package/lib/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { C as
|
|
1
|
+
import { C as dshHome, D as viewSettingsPath, E as storePath, S as aliasRoot, T as keysDir, _ as MAX_TIMEOUT_MS, a as takeHostNotices, b as resolveTimeouts, c as pushPipeline, d as WriteConflictError, f as attachmentHeader, g as DeadlineError, h as DEFAULT_TIMEOUTS, i as drainHostNotices, l as seedHelperCache, m as DEFAULT_READ_TIMEOUT_MS, n as TombstonedError, o as helperAvailability, p as DEFAULT_GIT_TIMEOUT_MS, s as probe, t as RemoteWorlds, u as RemoteFiles, v as MIN_TIMEOUT_MS, w as ensurePrivateDir, x as require_lib, y as clampExecTimeout } from "./worlds-RG8K5eom.js";
|
|
2
2
|
import z from "@deepseek-ai/schemastery";
|
|
3
3
|
import { chmodSync, closeSync, existsSync, fsyncSync, mkdirSync, openSync, readFileSync, renameSync, statSync, writeSync } from "node:fs";
|
|
4
4
|
import { createHash, randomUUID } from "node:crypto";
|
|
@@ -26,7 +26,7 @@ function emptyDoc() {
|
|
|
26
26
|
};
|
|
27
27
|
}
|
|
28
28
|
/** Same file identity (both absent counts as same). */
|
|
29
|
-
function sameStamp(left, right) {
|
|
29
|
+
function sameStamp$1(left, right) {
|
|
30
30
|
if (left === null || right === null) return left === right;
|
|
31
31
|
return left.mtimeMs === right.mtimeMs && left.size === right.size;
|
|
32
32
|
}
|
|
@@ -66,6 +66,15 @@ var RemoteStore = class {
|
|
|
66
66
|
this.reload();
|
|
67
67
|
}
|
|
68
68
|
/**
|
|
69
|
+
* Absolute path of the JSON document.
|
|
70
|
+
*
|
|
71
|
+
* Diagnostics only: a corrupt file must name itself in the error a surface
|
|
72
|
+
* shows, otherwise the user only sees "the request failed".
|
|
73
|
+
*/
|
|
74
|
+
get filePath() {
|
|
75
|
+
return this.file;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
69
78
|
* Reload from disk (picks up external edits).
|
|
70
79
|
*
|
|
71
80
|
* Re-reading on every access is deliberate: the root fs router asks this store
|
|
@@ -80,7 +89,7 @@ var RemoteStore = class {
|
|
|
80
89
|
*/
|
|
81
90
|
reload() {
|
|
82
91
|
const stamp = this.stampOf();
|
|
83
|
-
if (stamp !== void 0 && this.stamp !== void 0 && sameStamp(stamp, this.stamp)) return;
|
|
92
|
+
if (stamp !== void 0 && this.stamp !== void 0 && sameStamp$1(stamp, this.stamp)) return;
|
|
84
93
|
this.doc = this.loadDoc();
|
|
85
94
|
this.stamp = stamp;
|
|
86
95
|
}
|
|
@@ -168,7 +177,7 @@ var RemoteStore = class {
|
|
|
168
177
|
const before = JSON.stringify(this.doc);
|
|
169
178
|
const result = apply(this.doc);
|
|
170
179
|
if (JSON.stringify(this.doc) === before) return result;
|
|
171
|
-
if (!sameStamp(base, this.stampOf() ?? null)) continue;
|
|
180
|
+
if (!sameStamp$1(base, this.stampOf() ?? null)) continue;
|
|
172
181
|
this.save();
|
|
173
182
|
return result;
|
|
174
183
|
}
|
|
@@ -458,6 +467,21 @@ function clampViewWidthRatio(value) {
|
|
|
458
467
|
function defaults() {
|
|
459
468
|
return { ...REMOTE_VIEW_SETTINGS_DEFAULTS };
|
|
460
469
|
}
|
|
470
|
+
/** Same file identity (both absent counts as same). */
|
|
471
|
+
function sameStamp(left, right) {
|
|
472
|
+
if (left === null || right === null) return left === right;
|
|
473
|
+
return left.mtimeMs === right.mtimeMs && left.size === right.size;
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* Replays one read-modify-write before giving up.
|
|
477
|
+
*
|
|
478
|
+
* Desktop and `dsh web` share `$DSH_HOME/dsh-remote-view.json` by design, and
|
|
479
|
+
* every patch rewrites the WHOLE document: a plain read-merge-write let the last
|
|
480
|
+
* window win and silently dropped the other one's preference. A stamp re-check
|
|
481
|
+
* right before the write turns that into a replay on fresh content; a third
|
|
482
|
+
* failed attempt fails loud instead of losing a setting.
|
|
483
|
+
*/
|
|
484
|
+
const PATCH_ATTEMPTS = 3;
|
|
461
485
|
/** A finite width share, clamped into the allowed band; anything else is a miss. */
|
|
462
486
|
function readWidthRatio(value) {
|
|
463
487
|
if (typeof value !== "number" || !Number.isFinite(value)) return void 0;
|
|
@@ -502,13 +526,33 @@ var RemoteViewSettingsStore = class {
|
|
|
502
526
|
* @returns the settings as stored.
|
|
503
527
|
*/
|
|
504
528
|
patch(patch) {
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
529
|
+
for (let attempt = 0; attempt < PATCH_ATTEMPTS; attempt += 1) {
|
|
530
|
+
const before = this.stampOf();
|
|
531
|
+
const current = this.get();
|
|
532
|
+
const next = {
|
|
533
|
+
autoOpenRemoteWorkspace: typeof patch.autoOpenRemoteWorkspace === "boolean" ? patch.autoOpenRemoteWorkspace : current.autoOpenRemoteWorkspace,
|
|
534
|
+
remoteWorkspaceWidthRatio: readWidthRatio(patch.remoteWorkspaceWidthRatio) ?? current.remoteWorkspaceWidthRatio
|
|
535
|
+
};
|
|
536
|
+
if (!sameStamp(before, this.stampOf())) continue;
|
|
537
|
+
this.save(next);
|
|
538
|
+
return next;
|
|
539
|
+
}
|
|
540
|
+
throw new Error(`${this.file}: another process keeps editing the view settings; retry the operation`);
|
|
541
|
+
}
|
|
542
|
+
/**
|
|
543
|
+
* File identity right now; `null` when the file is absent (or cannot be
|
|
544
|
+
* statted — `get()` fails loud on that case anyway).
|
|
545
|
+
*/
|
|
546
|
+
stampOf() {
|
|
547
|
+
try {
|
|
548
|
+
const stat = statSync(this.file, { throwIfNoEntry: false });
|
|
549
|
+
return stat === void 0 ? null : {
|
|
550
|
+
mtimeMs: stat.mtimeMs,
|
|
551
|
+
size: stat.size
|
|
552
|
+
};
|
|
553
|
+
} catch {
|
|
554
|
+
return null;
|
|
555
|
+
}
|
|
512
556
|
}
|
|
513
557
|
/** Write one document atomically: a crash never leaves a half-written file. */
|
|
514
558
|
save(settings) {
|
|
@@ -657,6 +701,24 @@ function isConnectionFailure(error) {
|
|
|
657
701
|
var RemotePool = class {
|
|
658
702
|
store;
|
|
659
703
|
conns = /* @__PURE__ */ new Map();
|
|
704
|
+
/**
|
|
705
|
+
* Dedicated connections for PTY shells (one per alias).
|
|
706
|
+
*
|
|
707
|
+
* Why a SECOND connection instead of a channel on the shared one: ssh2
|
|
708
|
+
* multiplexes channels over ONE TCP connection, and SSH has no per-channel
|
|
709
|
+
* priority or flow control. A large SFTP transfer / git output on that
|
|
710
|
+
* connection therefore delays everything else on it — including the echo of a
|
|
711
|
+
* keystroke — which is exactly the "typing stutters when something else is
|
|
712
|
+
* moving data" report. A terminal gets its own connection, so its bytes never
|
|
713
|
+
* queue behind browse/exec traffic, and shells stop consuming the shared
|
|
714
|
+
* connection's channel budget as well.
|
|
715
|
+
*
|
|
716
|
+
* Cost: one extra SSH handshake per alias the first time a terminal opens,
|
|
717
|
+
* plus one held TCP connection per host with a terminal. `shell()` falls back
|
|
718
|
+
* to the shared connection if this one cannot be established at all, so a host
|
|
719
|
+
* that refuses extra connections still gets a working terminal.
|
|
720
|
+
*/
|
|
721
|
+
shellConns = /* @__PURE__ */ new Map();
|
|
660
722
|
/** TOFU fingerprints observed this process (alias -> sha256). */
|
|
661
723
|
hostKeys = /* @__PURE__ */ new Map();
|
|
662
724
|
/** Per-alias `setsid --wait` capability, probed once (see supportsSetsid). */
|
|
@@ -673,18 +735,13 @@ var RemotePool = class {
|
|
|
673
735
|
/** Drop one pooled connection (call after host update/delete). */
|
|
674
736
|
drop(alias) {
|
|
675
737
|
this.evictBrowse(alias);
|
|
676
|
-
const pooled = this.conns.get(alias);
|
|
677
738
|
this.setsidProbes.delete(alias);
|
|
678
|
-
|
|
679
|
-
this.
|
|
680
|
-
if (pooled.timer !== void 0) clearTimeout(pooled.timer);
|
|
681
|
-
try {
|
|
682
|
-
pooled.conn.end();
|
|
683
|
-
} catch {}
|
|
739
|
+
this.evictFrom(this.conns, alias);
|
|
740
|
+
this.evictFrom(this.shellConns, alias);
|
|
684
741
|
}
|
|
685
742
|
/** Close every pooled connection. */
|
|
686
743
|
dispose() {
|
|
687
|
-
for (const alias of [...this.conns.keys()]) this.drop(alias);
|
|
744
|
+
for (const alias of /* @__PURE__ */ new Set([...this.conns.keys(), ...this.shellConns.keys()])) this.drop(alias);
|
|
688
745
|
}
|
|
689
746
|
/**
|
|
690
747
|
* Forget one alias's connection HEALTH (host edited/removed), not just its
|
|
@@ -715,7 +772,7 @@ var RemotePool = class {
|
|
|
715
772
|
return {
|
|
716
773
|
alias,
|
|
717
774
|
state: entry?.state ?? "idle",
|
|
718
|
-
open: pooled !== void 0 && pooled.connecting === void 0,
|
|
775
|
+
open: pooled !== void 0 && pooled.connecting === void 0 || this.shellConns.get(alias)?.connecting === void 0 && this.shellConns.has(alias),
|
|
719
776
|
...entry?.okAt === void 0 ? {} : { okAt: entry.okAt },
|
|
720
777
|
...entry?.errorAt === void 0 ? {} : { errorAt: entry.errorAt },
|
|
721
778
|
...entry?.error === void 0 ? {} : { error: entry.error }
|
|
@@ -1047,8 +1104,23 @@ var RemotePool = class {
|
|
|
1047
1104
|
}
|
|
1048
1105
|
/** Open an interactive PTY shell. The caller owns the channel lifecycle. */
|
|
1049
1106
|
async shell(alias, opts = {}) {
|
|
1050
|
-
|
|
1051
|
-
|
|
1107
|
+
/**
|
|
1108
|
+
* Prefer the DEDICATED PTY connection so terminal bytes never queue behind
|
|
1109
|
+
* SFTP/git/exec traffic on the shared one (SSH has no channel priority).
|
|
1110
|
+
* A host that refuses the extra connection still gets a terminal: the fallback
|
|
1111
|
+
* is the shared connection, exactly the old behaviour.
|
|
1112
|
+
*/
|
|
1113
|
+
let conn;
|
|
1114
|
+
let dedicated = true;
|
|
1115
|
+
try {
|
|
1116
|
+
conn = await this.acquireShell(alias);
|
|
1117
|
+
this.holdIn(this.shellConns, alias);
|
|
1118
|
+
} catch (error) {
|
|
1119
|
+
dedicated = false;
|
|
1120
|
+
conn = await this.acquire(alias);
|
|
1121
|
+
this.hold(alias);
|
|
1122
|
+
if (!(error instanceof Error) || !isConnectionFailure(error)) console.warn("[dsh-remote-vs] dedicated PTY connection failed; using the shared one:", error);
|
|
1123
|
+
}
|
|
1052
1124
|
try {
|
|
1053
1125
|
const channel = await new Promise((resolve, reject) => {
|
|
1054
1126
|
conn.shell({
|
|
@@ -1060,11 +1132,15 @@ var RemotePool = class {
|
|
|
1060
1132
|
else resolve(stream);
|
|
1061
1133
|
});
|
|
1062
1134
|
});
|
|
1063
|
-
channel.once("close", () =>
|
|
1135
|
+
channel.once("close", () => {
|
|
1136
|
+
if (dedicated) this.releaseIn(this.shellConns, alias);
|
|
1137
|
+
else this.release(alias);
|
|
1138
|
+
});
|
|
1064
1139
|
this.markOk(alias);
|
|
1065
1140
|
return channel;
|
|
1066
1141
|
} catch (error) {
|
|
1067
|
-
this.
|
|
1142
|
+
if (dedicated) this.releaseIn(this.shellConns, alias);
|
|
1143
|
+
else this.release(alias);
|
|
1068
1144
|
if (isConnectionFailure(error)) this.markError(alias, error);
|
|
1069
1145
|
throw error;
|
|
1070
1146
|
}
|
|
@@ -1604,7 +1680,10 @@ var RemotePool = class {
|
|
|
1604
1680
|
}
|
|
1605
1681
|
/** Hold the connection against idle eviction (balanced by release). */
|
|
1606
1682
|
hold(alias) {
|
|
1607
|
-
|
|
1683
|
+
this.holdIn(this.conns, alias);
|
|
1684
|
+
}
|
|
1685
|
+
holdIn(map, alias) {
|
|
1686
|
+
const pooled = map.get(alias);
|
|
1608
1687
|
if (pooled === void 0) return;
|
|
1609
1688
|
pooled.held += 1;
|
|
1610
1689
|
pooled.lastUse = Date.now();
|
|
@@ -1614,17 +1693,27 @@ var RemotePool = class {
|
|
|
1614
1693
|
}
|
|
1615
1694
|
}
|
|
1616
1695
|
release(alias) {
|
|
1617
|
-
|
|
1696
|
+
this.releaseIn(this.conns, alias);
|
|
1697
|
+
}
|
|
1698
|
+
releaseIn(map, alias) {
|
|
1699
|
+
const pooled = map.get(alias);
|
|
1618
1700
|
if (pooled === void 0) return;
|
|
1619
1701
|
if (pooled.held > 0) pooled.held -= 1;
|
|
1620
1702
|
pooled.lastUse = Date.now();
|
|
1621
1703
|
if (pooled.held > 0) return;
|
|
1622
1704
|
if (pooled.timer !== void 0) clearTimeout(pooled.timer);
|
|
1623
|
-
pooled.timer = setTimeout(() => this.
|
|
1705
|
+
pooled.timer = setTimeout(() => this.evictFrom(map, alias), IDLE_MS);
|
|
1624
1706
|
if (typeof pooled.timer === "object" && "unref" in pooled.timer) pooled.timer.unref();
|
|
1625
1707
|
}
|
|
1626
1708
|
async acquire(alias) {
|
|
1627
|
-
|
|
1709
|
+
return this.acquireIn(this.conns, alias);
|
|
1710
|
+
}
|
|
1711
|
+
/** `acquire` for the dedicated PTY connection map. */
|
|
1712
|
+
async acquireShell(alias) {
|
|
1713
|
+
return this.acquireIn(this.shellConns, alias);
|
|
1714
|
+
}
|
|
1715
|
+
async acquireIn(map, alias) {
|
|
1716
|
+
const pooled = map.get(alias);
|
|
1628
1717
|
if (pooled !== void 0 && pooled.connecting !== void 0) return pooled.connecting;
|
|
1629
1718
|
if (pooled !== void 0) {
|
|
1630
1719
|
pooled.lastUse = Date.now();
|
|
@@ -1637,7 +1726,7 @@ var RemotePool = class {
|
|
|
1637
1726
|
connecting: void 0,
|
|
1638
1727
|
held: 0
|
|
1639
1728
|
};
|
|
1640
|
-
|
|
1729
|
+
map.set(alias, entry);
|
|
1641
1730
|
/**
|
|
1642
1731
|
* The entry object IS the ownership token: `drop()` deletes it and a later
|
|
1643
1732
|
* `acquire()` creates a NEW one, so comparing identity answers "does this
|
|
@@ -1650,7 +1739,7 @@ var RemotePool = class {
|
|
|
1650
1739
|
* failure check, a failed OLD dial deletes the NEW entry by alias.
|
|
1651
1740
|
*/
|
|
1652
1741
|
entry.connecting = this.dial(alias).then((conn) => {
|
|
1653
|
-
if (
|
|
1742
|
+
if (map.get(alias) !== entry) {
|
|
1654
1743
|
try {
|
|
1655
1744
|
conn.end();
|
|
1656
1745
|
} catch {}
|
|
@@ -1660,24 +1749,34 @@ var RemotePool = class {
|
|
|
1660
1749
|
entry.connecting = void 0;
|
|
1661
1750
|
this.markOk(alias);
|
|
1662
1751
|
conn.on("close", () => {
|
|
1663
|
-
if (
|
|
1752
|
+
if (map.get(alias) !== entry) return;
|
|
1664
1753
|
this.markError(alias, /* @__PURE__ */ new Error("connection closed"));
|
|
1665
|
-
this.
|
|
1754
|
+
this.evictFrom(map, alias);
|
|
1666
1755
|
});
|
|
1667
1756
|
conn.on("error", (error) => {
|
|
1668
|
-
if (
|
|
1757
|
+
if (map.get(alias) !== entry) return;
|
|
1669
1758
|
this.markError(alias, error);
|
|
1670
|
-
this.
|
|
1759
|
+
this.evictFrom(map, alias);
|
|
1671
1760
|
});
|
|
1672
|
-
this.
|
|
1761
|
+
this.releaseIn(map, alias);
|
|
1673
1762
|
return conn;
|
|
1674
1763
|
}, (error) => {
|
|
1675
|
-
if (
|
|
1764
|
+
if (map.get(alias) === entry) map.delete(alias);
|
|
1676
1765
|
this.markError(alias, error);
|
|
1677
1766
|
throw error;
|
|
1678
1767
|
});
|
|
1679
1768
|
return entry.connecting;
|
|
1680
1769
|
}
|
|
1770
|
+
/** Evict ONE alias from ONE map (host removal drops both maps explicitly). */
|
|
1771
|
+
evictFrom(map, alias) {
|
|
1772
|
+
const pooled = map.get(alias);
|
|
1773
|
+
if (pooled === void 0) return;
|
|
1774
|
+
map.delete(alias);
|
|
1775
|
+
if (pooled.timer !== void 0) clearTimeout(pooled.timer);
|
|
1776
|
+
try {
|
|
1777
|
+
pooled.conn.end();
|
|
1778
|
+
} catch {}
|
|
1779
|
+
}
|
|
1681
1780
|
/**
|
|
1682
1781
|
* The ssh2 client factory.
|
|
1683
1782
|
*
|
|
@@ -1952,6 +2051,18 @@ var ForwardManager = class {
|
|
|
1952
2051
|
* first listener orphaned — unreachable from the panel and never closed.
|
|
1953
2052
|
*/
|
|
1954
2053
|
starting = /* @__PURE__ */ new Map();
|
|
2054
|
+
/**
|
|
2055
|
+
* Rules stopped while their listener was still BINDING.
|
|
2056
|
+
*
|
|
2057
|
+
* A pending bind is not in `forwards` yet, so `stop()`, `stopAlias()`,
|
|
2058
|
+
* `stopAll()` and `dispose()` used to see nothing to close: the listener the
|
|
2059
|
+
* OS was about to hand out stayed bound with no panel row left to stop it
|
|
2060
|
+
* (click a forward, immediately disable the plugin or delete the host, and
|
|
2061
|
+
* `127.0.0.1:<port>` stayed taken until the Host restarted). The id sits here
|
|
2062
|
+
* until the bind settles; a settling bind that finds its id records itself as
|
|
2063
|
+
* stopped and closes the fresh listener instead of entering `forwards`.
|
|
2064
|
+
*/
|
|
2065
|
+
stoppedWhileBinding = /* @__PURE__ */ new Set();
|
|
1955
2066
|
/** Set on unload so a bind that settles afterwards closes itself. */
|
|
1956
2067
|
disposed = false;
|
|
1957
2068
|
constructor(deps) {
|
|
@@ -1977,15 +2088,16 @@ var ForwardManager = class {
|
|
|
1977
2088
|
const maxPerAlias = this.deps.maxPerAlias ?? 16;
|
|
1978
2089
|
if (perAlias >= maxPerAlias) throw new Error(`host ${request.alias} already has ${maxPerAlias} local forwards; stop one first`);
|
|
1979
2090
|
const pending = this.bindForward(id, request, requestedLocalPort).then((forward) => {
|
|
1980
|
-
if (this.disposed) {
|
|
2091
|
+
if (this.disposed || this.stoppedWhileBinding.has(id)) {
|
|
1981
2092
|
this.closeForward(forward);
|
|
1982
|
-
throw new Error("the port-forward manager was unloaded while the rule was binding");
|
|
2093
|
+
throw new Error(this.disposed ? "the port-forward manager was unloaded while the rule was binding" : "the rule was stopped while its local listener was binding");
|
|
1983
2094
|
}
|
|
1984
2095
|
this.forwards.set(id, forward);
|
|
1985
2096
|
this.log("info", `forward ${id} listening on 127.0.0.1:${forward.localPort}`);
|
|
1986
2097
|
return this.stateOf(forward);
|
|
1987
2098
|
}).finally(() => {
|
|
1988
2099
|
this.starting.delete(id);
|
|
2100
|
+
this.stoppedWhileBinding.delete(id);
|
|
1989
2101
|
});
|
|
1990
2102
|
this.starting.set(id, pending);
|
|
1991
2103
|
return pending;
|
|
@@ -1993,10 +2105,15 @@ var ForwardManager = class {
|
|
|
1993
2105
|
/** Stop one rule. Returns false when the id is unknown (already stopped). */
|
|
1994
2106
|
stop(id) {
|
|
1995
2107
|
const forward = this.forwards.get(id);
|
|
1996
|
-
if (forward
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2108
|
+
if (forward !== void 0) {
|
|
2109
|
+
this.forwards.delete(id);
|
|
2110
|
+
this.closeForward(forward);
|
|
2111
|
+
this.log("info", `forward ${id} stopped`);
|
|
2112
|
+
return true;
|
|
2113
|
+
}
|
|
2114
|
+
if (!this.starting.has(id)) return false;
|
|
2115
|
+
this.stoppedWhileBinding.add(id);
|
|
2116
|
+
this.log("info", `forward ${id} stopped while binding; its listener will close on settle`);
|
|
2000
2117
|
return true;
|
|
2001
2118
|
}
|
|
2002
2119
|
/**
|
|
@@ -2014,6 +2131,11 @@ var ForwardManager = class {
|
|
|
2014
2131
|
this.closeForward(forward);
|
|
2015
2132
|
stopped += 1;
|
|
2016
2133
|
}
|
|
2134
|
+
for (const id of [...this.starting.keys()]) {
|
|
2135
|
+
if (this.forwards.has(id) || !id.startsWith(`${alias}|`)) continue;
|
|
2136
|
+
this.stoppedWhileBinding.add(id);
|
|
2137
|
+
stopped += 1;
|
|
2138
|
+
}
|
|
2017
2139
|
if (stopped > 0) this.log("info", `stopped ${stopped} forward(s) for deleted host ${alias}`);
|
|
2018
2140
|
return stopped;
|
|
2019
2141
|
}
|
|
@@ -2027,6 +2149,11 @@ var ForwardManager = class {
|
|
|
2027
2149
|
stopAll() {
|
|
2028
2150
|
let stopped = 0;
|
|
2029
2151
|
for (const id of [...this.forwards.keys()]) if (this.stop(id)) stopped += 1;
|
|
2152
|
+
for (const id of [...this.starting.keys()]) {
|
|
2153
|
+
if (this.forwards.has(id)) continue;
|
|
2154
|
+
this.stoppedWhileBinding.add(id);
|
|
2155
|
+
stopped += 1;
|
|
2156
|
+
}
|
|
2030
2157
|
return stopped;
|
|
2031
2158
|
}
|
|
2032
2159
|
/** Every rule, optionally narrowed to one host alias. */
|
|
@@ -2036,7 +2163,7 @@ var ForwardManager = class {
|
|
|
2036
2163
|
/** Close every listener and every proxied connection (plugin unload). */
|
|
2037
2164
|
dispose() {
|
|
2038
2165
|
this.disposed = true;
|
|
2039
|
-
|
|
2166
|
+
this.stopAll();
|
|
2040
2167
|
}
|
|
2041
2168
|
/** Bind the loopback listener. Split out so the error path is one place. */
|
|
2042
2169
|
async bindForward(id, request, requestedLocalPort) {
|
|
@@ -2259,9 +2386,31 @@ async function readJsonBodyResult(req, opts = {}) {
|
|
|
2259
2386
|
function isJsonObject(value) {
|
|
2260
2387
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
2261
2388
|
}
|
|
2389
|
+
/**
|
|
2390
|
+
* The host alias one response is about (consumed by {@link writeJson}).
|
|
2391
|
+
*
|
|
2392
|
+
* A response may only carry news about the host it concerns, otherwise one
|
|
2393
|
+
* window shows another window's silent helper push and the queue loses it for
|
|
2394
|
+
* the window that caused it. `null` is the explicit "about no single host"
|
|
2395
|
+
* answer (a hosts/workspaces listing): it leaves the queue alone. An untouched
|
|
2396
|
+
* response keeps the historical drain-all answer — only this plugin's route
|
|
2397
|
+
* table marks its responses, and every route states what it is about.
|
|
2398
|
+
*/
|
|
2399
|
+
const noticeAliasOf = /* @__PURE__ */ new WeakMap();
|
|
2400
|
+
/** Declare which host the response being written concerns (`null` = no host). */
|
|
2401
|
+
function scopeHostNotices(res, alias) {
|
|
2402
|
+
noticeAliasOf.set(res, alias === "" ? null : alias);
|
|
2403
|
+
}
|
|
2404
|
+
/** The notices this response may carry and consume. */
|
|
2405
|
+
function noticesFor(res, attachable, status) {
|
|
2406
|
+
if (!attachable || status >= 400) return [];
|
|
2407
|
+
if (!noticeAliasOf.has(res)) return drainHostNotices();
|
|
2408
|
+
const alias = noticeAliasOf.get(res);
|
|
2409
|
+
return typeof alias === "string" && alias !== "" ? takeHostNotices(alias) : [];
|
|
2410
|
+
}
|
|
2262
2411
|
/** Write one JSON response. */
|
|
2263
2412
|
function writeJson(res, status, body, headers = {}) {
|
|
2264
|
-
const notices = typeof body === "object" && body !== null && !Array.isArray(body)
|
|
2413
|
+
const notices = noticesFor(res, typeof body === "object" && body !== null && !Array.isArray(body), status);
|
|
2265
2414
|
const payload = notices.length > 0 ? {
|
|
2266
2415
|
...body,
|
|
2267
2416
|
notices
|
|
@@ -2422,6 +2571,40 @@ function selectionArchiveCommand(paths) {
|
|
|
2422
2571
|
* store entry flips to key auth with the managed keyPath.
|
|
2423
2572
|
*/
|
|
2424
2573
|
const execFileAsync = promisify(execFile);
|
|
2574
|
+
/**
|
|
2575
|
+
* `type base64` — the only two fields of a `.pub` file that are key material.
|
|
2576
|
+
*
|
|
2577
|
+
* ssh-keygen appends a free-text comment, and that comment is NOT part of the
|
|
2578
|
+
* key. The old code only checked the line's prefix and then spliced the WHOLE
|
|
2579
|
+
* line into a single-quoted remote shell command; a garbled or tampered `.pub`
|
|
2580
|
+
* therefore reached `sh -c` as code. The strict grammar below admits nothing
|
|
2581
|
+
* but the two safe fields, and the comment is regenerated from the alias.
|
|
2582
|
+
*/
|
|
2583
|
+
const PUBLIC_KEY_FIELDS_RE = /^(ssh-ed25519) ([A-Za-z0-9+/]+={0,2})$/;
|
|
2584
|
+
/** Ed25519 wire blob: 4-byte length + "ssh-ed25519", 4-byte length + 32 bytes. */
|
|
2585
|
+
function isEd25519Blob(blob) {
|
|
2586
|
+
const decoded = Buffer.from(blob, "base64");
|
|
2587
|
+
if (decoded.length !== 51) return false;
|
|
2588
|
+
if (decoded.readUInt32BE(0) !== 11) return false;
|
|
2589
|
+
if (decoded.subarray(4, 15).toString("latin1") !== "ssh-ed25519") return false;
|
|
2590
|
+
return decoded.readUInt32BE(15) === 32;
|
|
2591
|
+
}
|
|
2592
|
+
/**
|
|
2593
|
+
* Normalize one `.pub` line to `ssh-ed25519 <base64> dsh-remote-vs/<alias>`.
|
|
2594
|
+
*
|
|
2595
|
+
* Throws when the line is not a well-formed ed25519 public key, so the caller
|
|
2596
|
+
* sees a clear "this file is not a key" error instead of a remote login that
|
|
2597
|
+
* fails later. The returned text is the only form that may enter the shell
|
|
2598
|
+
* command: its comment is ours, and both remaining fields are shell-inert.
|
|
2599
|
+
*/
|
|
2600
|
+
function formatPublicKey(raw, alias) {
|
|
2601
|
+
const fields = raw.trim().split(/\s+/);
|
|
2602
|
+
const type = fields[0];
|
|
2603
|
+
const blob = fields[1];
|
|
2604
|
+
if (fields.length < 2 || type === void 0 || blob === void 0 || !PUBLIC_KEY_FIELDS_RE.test(`${type} ${blob}`)) throw new Error("expected an \"ssh-ed25519 <base64>\" public key line");
|
|
2605
|
+
if (!isEd25519Blob(blob)) throw new Error("the base64 blob is not a valid ed25519 public key");
|
|
2606
|
+
return `${type} ${blob} dsh-remote-vs/${alias.replace(/[^A-Za-z0-9._-]/g, "_")}`;
|
|
2607
|
+
}
|
|
2425
2608
|
/** Ensure the managed keypair exists; return the private key path. */
|
|
2426
2609
|
async function ensureKeypair(alias, dir = keysDir()) {
|
|
2427
2610
|
ensurePrivateDir(dir);
|
|
@@ -2446,8 +2629,12 @@ async function ensureKeypair(alias, dir = keysDir()) {
|
|
|
2446
2629
|
chmodSync(privatePath, 384);
|
|
2447
2630
|
} catch {}
|
|
2448
2631
|
}
|
|
2449
|
-
|
|
2450
|
-
|
|
2632
|
+
let publicKey;
|
|
2633
|
+
try {
|
|
2634
|
+
publicKey = formatPublicKey(readFileSync(publicPath, "utf8"), alias);
|
|
2635
|
+
} catch (error) {
|
|
2636
|
+
throw new Error(`unexpected public key format in ${publicPath}: ${error instanceof Error ? error.message : String(error)}`);
|
|
2637
|
+
}
|
|
2451
2638
|
return {
|
|
2452
2639
|
privatePath,
|
|
2453
2640
|
publicKey
|
|
@@ -2469,7 +2656,7 @@ async function bootstrap(pool, store, alias) {
|
|
|
2469
2656
|
const script = [
|
|
2470
2657
|
"mkdir -p ~/.ssh && chmod 700 ~/.ssh",
|
|
2471
2658
|
`touch ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys`,
|
|
2472
|
-
`grep -
|
|
2659
|
+
`grep -qF '${publicKey.split(" ").slice(0, 2).join(" ")}' ~/.ssh/authorized_keys 2>/dev/null || echo '${publicKey}' >> ~/.ssh/authorized_keys`
|
|
2473
2660
|
].join(" && ");
|
|
2474
2661
|
const result = await pool.exec(alias, script, { timeoutMs: 3e4 });
|
|
2475
2662
|
if (!result.success) throw new Error(`key install failed [exit code: ${result.exitCode}]: ${result.stderr || result.stdout || result.error || "unknown"}`);
|
|
@@ -2735,12 +2922,52 @@ const EMPTY_SOCKET_TTL_MS = 10 * 6e4;
|
|
|
2735
2922
|
* The retained ceiling is therefore this mark plus ssh2's window (2 MiB) plus
|
|
2736
2923
|
* whatever is in flight — bounded, but not exactly 4 MiB.
|
|
2737
2924
|
*
|
|
2925
|
+
* The mark is RESPONSIVENESS, not just memory: everything already handed to a
|
|
2926
|
+
* socket has to be parsed and painted by the browser before a later keystroke's
|
|
2927
|
+
* echo can appear, so a large mark means "type during a huge dump and wait
|
|
2928
|
+
* seconds". 2 MiB still absorbs a burst comfortably (a keystroke is bytes) while
|
|
2929
|
+
* keeping the worst-case echo delay down; ssh2's own 2 MiB window is the part we
|
|
2930
|
+
* cannot shrink from here.
|
|
2931
|
+
*
|
|
2738
2932
|
* The counter is per session, not per socket: one lagging socket pauses the
|
|
2739
2933
|
* channel for every socket attached to that shell. That is deliberate — the
|
|
2740
2934
|
* alternative is letting the slowest consumer grow the host process.
|
|
2741
2935
|
*/
|
|
2742
|
-
const OUT_HIGH_WATER =
|
|
2743
|
-
const OUT_LOW_WATER =
|
|
2936
|
+
const OUT_HIGH_WATER = 2 * 1024 * 1024;
|
|
2937
|
+
const OUT_LOW_WATER = 512 * 1024;
|
|
2938
|
+
/** Default window for coalescing output frames (0 = one frame per chunk). */
|
|
2939
|
+
const OUT_COALESCE_MS = 8;
|
|
2940
|
+
/**
|
|
2941
|
+
* Flush immediately past this much buffered text (measured in UTF-16 code
|
|
2942
|
+
* units, i.e. `String.length`): a producer faster than the window must not turn
|
|
2943
|
+
* into an unbounded string copy (the channel backpressure bounds the bytes
|
|
2944
|
+
* handed to the SOCKETS, not this buffer).
|
|
2945
|
+
*
|
|
2946
|
+
* Code units rather than bytes on purpose: converting to a byte count would
|
|
2947
|
+
* copy the whole buffer on every chunk just to compare a threshold. CJK/emoji
|
|
2948
|
+
* output therefore flushes at up to ~3x this many bytes, which is still far
|
|
2949
|
+
* below the 1 MiB client-frame cap this path never approaches (it is the
|
|
2950
|
+
* client→host direction).
|
|
2951
|
+
*/
|
|
2952
|
+
const OUT_COALESCE_CHARS = 64 * 1024;
|
|
2953
|
+
/**
|
|
2954
|
+
* How long the output must have been quiet for the NEXT chunk to be sent at once.
|
|
2955
|
+
*
|
|
2956
|
+
* A terminal echoes what the user types. Holding that echo for the coalescing
|
|
2957
|
+
* window is invisible at 8 ms — but under load the window itself is a timer in
|
|
2958
|
+
* an event loop shared with the agent, and the delay adds up. After a quiet
|
|
2959
|
+
* period the chunk is therefore flushed synchronously; only a running stream
|
|
2960
|
+
* (where `lastFlushAt` is fresh) is batched.
|
|
2961
|
+
*/
|
|
2962
|
+
const OUT_ECHO_IDLE_MS = 24;
|
|
2963
|
+
/**
|
|
2964
|
+
* How much recent output a reattaching socket receives (see `Session.replay`).
|
|
2965
|
+
*
|
|
2966
|
+
* 128 Ki UTF-16 units is a few screens of logs plus the prompt line; the buffer
|
|
2967
|
+
* may grow to twice this between trims, so one terminal costs at most ~512 KiB
|
|
2968
|
+
* of the host's memory in replay state.
|
|
2969
|
+
*/
|
|
2970
|
+
const REPLAY_MAX_CHARS = 128 * 1024;
|
|
2744
2971
|
/**
|
|
2745
2972
|
* The largest client frame accepted, in bytes.
|
|
2746
2973
|
*
|
|
@@ -2761,9 +2988,15 @@ const TERMINAL_MAX_FRAME_BYTES = 1024 * 1024;
|
|
|
2761
2988
|
const WS_OPEN = 1;
|
|
2762
2989
|
var TerminalSessionRegistry = class {
|
|
2763
2990
|
pool;
|
|
2991
|
+
options;
|
|
2764
2992
|
sessions = /* @__PURE__ */ new Map();
|
|
2765
|
-
constructor(pool) {
|
|
2993
|
+
constructor(pool, options = {}) {
|
|
2766
2994
|
this.pool = pool;
|
|
2995
|
+
this.options = options;
|
|
2996
|
+
}
|
|
2997
|
+
/** The window this registry coalesces within (`0` = every chunk is a frame). */
|
|
2998
|
+
get coalesceMs() {
|
|
2999
|
+
return this.options.coalesceMs ?? OUT_COALESCE_MS;
|
|
2767
3000
|
}
|
|
2768
3001
|
/** Open a shell and attach the first socket. */
|
|
2769
3002
|
async open(ws, alias, cols, rows) {
|
|
@@ -2793,6 +3026,11 @@ var TerminalSessionRegistry = class {
|
|
|
2793
3026
|
sockets: /* @__PURE__ */ new Set(),
|
|
2794
3027
|
pending: 0,
|
|
2795
3028
|
paused: false,
|
|
3029
|
+
outBuffer: "",
|
|
3030
|
+
outTimer: void 0,
|
|
3031
|
+
replay: "",
|
|
3032
|
+
lastFlushAt: 0,
|
|
3033
|
+
outstanding: /* @__PURE__ */ new Map(),
|
|
2796
3034
|
reap: void 0,
|
|
2797
3035
|
outDecoder: new StringDecoder("utf8"),
|
|
2798
3036
|
errDecoder: new StringDecoder("utf8")
|
|
@@ -2800,25 +3038,17 @@ var TerminalSessionRegistry = class {
|
|
|
2800
3038
|
this.sessions.set(id, session);
|
|
2801
3039
|
channel.on("data", (chunk) => {
|
|
2802
3040
|
const text = session.outDecoder.write(chunk);
|
|
2803
|
-
if (text !== "") this.
|
|
2804
|
-
t: "out",
|
|
2805
|
-
d: text
|
|
2806
|
-
});
|
|
3041
|
+
if (text !== "") this.queueOut(session, text);
|
|
2807
3042
|
});
|
|
2808
3043
|
channel.stderr.on("data", (chunk) => {
|
|
2809
3044
|
const text = session.errDecoder.write(chunk);
|
|
2810
|
-
if (text !== "") this.
|
|
2811
|
-
t: "out",
|
|
2812
|
-
d: text
|
|
2813
|
-
});
|
|
3045
|
+
if (text !== "") this.queueOut(session, text);
|
|
2814
3046
|
});
|
|
2815
3047
|
channel.on("close", (code) => {
|
|
2816
3048
|
session.alive = false;
|
|
2817
3049
|
session.exitCode = code;
|
|
2818
|
-
for (const tail of [session.outDecoder.end(), session.errDecoder.end()]) if (tail !== "") this.
|
|
2819
|
-
|
|
2820
|
-
d: tail
|
|
2821
|
-
});
|
|
3050
|
+
for (const tail of [session.outDecoder.end(), session.errDecoder.end()]) if (tail !== "") this.queueOut(session, tail);
|
|
3051
|
+
this.flushOut(session);
|
|
2822
3052
|
this.broadcast(session, {
|
|
2823
3053
|
t: "exit",
|
|
2824
3054
|
code
|
|
@@ -2852,6 +3082,11 @@ var TerminalSessionRegistry = class {
|
|
|
2852
3082
|
const session = this.sessions.get(id);
|
|
2853
3083
|
if (session === void 0 || !session.alive) return false;
|
|
2854
3084
|
this.hookSocket(session, ws);
|
|
3085
|
+
if (session.replay !== "") this.sendOutTo(session, ws, JSON.stringify({
|
|
3086
|
+
t: "out",
|
|
3087
|
+
d: session.replay
|
|
3088
|
+
}));
|
|
3089
|
+
this.flushOut(session);
|
|
2855
3090
|
this.send(ws, {
|
|
2856
3091
|
t: "ready",
|
|
2857
3092
|
id
|
|
@@ -2864,6 +3099,7 @@ var TerminalSessionRegistry = class {
|
|
|
2864
3099
|
if (session === void 0) return false;
|
|
2865
3100
|
this.sessions.delete(id);
|
|
2866
3101
|
this.cancelReap(session);
|
|
3102
|
+
this.flushOut(session);
|
|
2867
3103
|
try {
|
|
2868
3104
|
session.channel.close();
|
|
2869
3105
|
} catch {}
|
|
@@ -2871,6 +3107,8 @@ var TerminalSessionRegistry = class {
|
|
|
2871
3107
|
socket.close(1e3);
|
|
2872
3108
|
} catch {}
|
|
2873
3109
|
session.sockets.clear();
|
|
3110
|
+
session.outstanding.clear();
|
|
3111
|
+
session.pending = 0;
|
|
2874
3112
|
return true;
|
|
2875
3113
|
}
|
|
2876
3114
|
/** Close every session (plugin unload). */
|
|
@@ -2909,9 +3147,17 @@ var TerminalSessionRegistry = class {
|
|
|
2909
3147
|
} catch {}
|
|
2910
3148
|
});
|
|
2911
3149
|
const detach = () => {
|
|
3150
|
+
this.flushOut(session);
|
|
3151
|
+
const unsettled = session.outstanding.get(ws);
|
|
3152
|
+
if (unsettled !== void 0) {
|
|
3153
|
+
session.outstanding.delete(ws);
|
|
3154
|
+
session.pending = Math.max(0, session.pending - unsettled);
|
|
3155
|
+
if (session.pending <= OUT_LOW_WATER) this.setPaused(session, false);
|
|
3156
|
+
}
|
|
2912
3157
|
session.sockets.delete(ws);
|
|
2913
3158
|
if (session.sockets.size > 0) return;
|
|
2914
3159
|
session.pending = 0;
|
|
3160
|
+
session.outstanding.clear();
|
|
2915
3161
|
this.setPaused(session, false);
|
|
2916
3162
|
this.armReap(session);
|
|
2917
3163
|
};
|
|
@@ -2944,6 +3190,47 @@ var TerminalSessionRegistry = class {
|
|
|
2944
3190
|
clearTimeout(session.reap);
|
|
2945
3191
|
session.reap = void 0;
|
|
2946
3192
|
}
|
|
3193
|
+
/**
|
|
3194
|
+
* Queue one output chunk and schedule the coalesced frame.
|
|
3195
|
+
*
|
|
3196
|
+
* `coalesceMs === 0` broadcasts immediately (the checks rely on that
|
|
3197
|
+
* synchrony). A chunk that arrives after an idle gap is also sent at once —
|
|
3198
|
+
* that is the interactive echo path (a keystroke must not wait for the
|
|
3199
|
+
* window) — while a stream that keeps producing coalesces.
|
|
3200
|
+
*/
|
|
3201
|
+
queueOut(session, text) {
|
|
3202
|
+
if (text === "") return;
|
|
3203
|
+
const idle = Date.now() - session.lastFlushAt >= OUT_ECHO_IDLE_MS;
|
|
3204
|
+
session.outBuffer += text;
|
|
3205
|
+
if (session.outBuffer.length >= OUT_COALESCE_CHARS || this.coalesceMs <= 0 || idle) {
|
|
3206
|
+
this.flushOut(session);
|
|
3207
|
+
return;
|
|
3208
|
+
}
|
|
3209
|
+
if (session.outTimer !== void 0) return;
|
|
3210
|
+
const timer = setTimeout(() => {
|
|
3211
|
+
session.outTimer = void 0;
|
|
3212
|
+
this.flushOut(session);
|
|
3213
|
+
}, this.coalesceMs);
|
|
3214
|
+
timer.unref?.();
|
|
3215
|
+
session.outTimer = timer;
|
|
3216
|
+
}
|
|
3217
|
+
/** Send the buffered output as ONE `out` frame (no-op when empty). */
|
|
3218
|
+
flushOut(session) {
|
|
3219
|
+
if (session.outTimer !== void 0) {
|
|
3220
|
+
clearTimeout(session.outTimer);
|
|
3221
|
+
session.outTimer = void 0;
|
|
3222
|
+
}
|
|
3223
|
+
if (session.outBuffer === "") return;
|
|
3224
|
+
const text = session.outBuffer;
|
|
3225
|
+
session.outBuffer = "";
|
|
3226
|
+
session.replay += text;
|
|
3227
|
+
if (session.replay.length > REPLAY_MAX_CHARS * 2) session.replay = session.replay.slice(-131072);
|
|
3228
|
+
session.lastFlushAt = Date.now();
|
|
3229
|
+
this.broadcast(session, {
|
|
3230
|
+
t: "out",
|
|
3231
|
+
d: text
|
|
3232
|
+
});
|
|
3233
|
+
}
|
|
2947
3234
|
setPaused(session, paused) {
|
|
2948
3235
|
if (session.paused === paused) return;
|
|
2949
3236
|
session.paused = paused;
|
|
@@ -2959,23 +3246,37 @@ var TerminalSessionRegistry = class {
|
|
|
2959
3246
|
}
|
|
2960
3247
|
broadcast(session, frame) {
|
|
2961
3248
|
const text = JSON.stringify(frame);
|
|
3249
|
+
for (const socket of session.sockets) this.sendOutTo(session, socket, text);
|
|
3250
|
+
if (session.pending > OUT_HIGH_WATER) this.setPaused(session, true);
|
|
3251
|
+
}
|
|
3252
|
+
/**
|
|
3253
|
+
* Send one already-stringified `out` frame to ONE socket with accounting.
|
|
3254
|
+
*
|
|
3255
|
+
* Shared by `broadcast` and by the replay on `attach`; the byte count must be
|
|
3256
|
+
* counted once per socket so the session's brake reflects every socket's
|
|
3257
|
+
* backlog (see `Session.pending`).
|
|
3258
|
+
*/
|
|
3259
|
+
sendOutTo(session, socket, text) {
|
|
2962
3260
|
const size = Buffer.byteLength(text, "utf8");
|
|
2963
|
-
|
|
2964
|
-
|
|
2965
|
-
|
|
2966
|
-
|
|
2967
|
-
|
|
2968
|
-
|
|
2969
|
-
|
|
2970
|
-
|
|
2971
|
-
session.
|
|
2972
|
-
|
|
2973
|
-
|
|
2974
|
-
|
|
2975
|
-
|
|
2976
|
-
|
|
3261
|
+
let settled = false;
|
|
3262
|
+
const done = () => {
|
|
3263
|
+
if (settled) return;
|
|
3264
|
+
settled = true;
|
|
3265
|
+
const left = session.outstanding.get(socket);
|
|
3266
|
+
if (left === void 0) return;
|
|
3267
|
+
const remaining = Math.max(0, left - size);
|
|
3268
|
+
if (remaining === 0) session.outstanding.delete(socket);
|
|
3269
|
+
else session.outstanding.set(socket, remaining);
|
|
3270
|
+
session.pending = Math.max(0, session.pending - size);
|
|
3271
|
+
if (session.pending <= OUT_LOW_WATER) this.setPaused(session, false);
|
|
3272
|
+
};
|
|
3273
|
+
session.pending += size;
|
|
3274
|
+
session.outstanding.set(socket, (session.outstanding.get(socket) ?? 0) + size);
|
|
3275
|
+
try {
|
|
3276
|
+
socket.send(text, done);
|
|
3277
|
+
} catch {
|
|
3278
|
+
done();
|
|
2977
3279
|
}
|
|
2978
|
-
if (session.pending > OUT_HIGH_WATER) this.setPaused(session, true);
|
|
2979
3280
|
}
|
|
2980
3281
|
};
|
|
2981
3282
|
//#endregion
|
|
@@ -3441,6 +3742,16 @@ var HostMonitor = class {
|
|
|
3441
3742
|
this.inflight.delete(alias);
|
|
3442
3743
|
this.generations.set(alias, (this.generations.get(alias) ?? 0) + 1);
|
|
3443
3744
|
}
|
|
3745
|
+
/**
|
|
3746
|
+
* Snapshots already in the cache, without dialing anything.
|
|
3747
|
+
*
|
|
3748
|
+
* A GET must not have a dial side effect: listing what the Host knows is the
|
|
3749
|
+
* read-only shape, and only an explicit per-host request (or the opt-in batch
|
|
3750
|
+
* flag) may wake a sleeping host.
|
|
3751
|
+
*/
|
|
3752
|
+
cached() {
|
|
3753
|
+
return [...this.cache.values()].map((entry) => entry.snapshot).sort((left, right) => left.alias.localeCompare(right.alias));
|
|
3754
|
+
}
|
|
3444
3755
|
async probe(alias) {
|
|
3445
3756
|
const generation = this.generations.get(alias) ?? 0;
|
|
3446
3757
|
const startedAt = this.now();
|
|
@@ -3499,16 +3810,27 @@ var HostMonitor = class {
|
|
|
3499
3810
|
};
|
|
3500
3811
|
//#endregion
|
|
3501
3812
|
//#region src/routes.ts
|
|
3502
|
-
/** One noServer WebSocket server for terminal upgrades (RFC 6455). */
|
|
3503
|
-
const terminalWss = new WebSocketServer({
|
|
3504
|
-
noServer: true,
|
|
3505
|
-
maxPayload: TERMINAL_MAX_FRAME_BYTES
|
|
3506
|
-
});
|
|
3507
3813
|
function queryParam(url, name) {
|
|
3508
3814
|
const value = url.searchParams.get(name);
|
|
3509
3815
|
return value === null ? void 0 : value;
|
|
3510
3816
|
}
|
|
3511
3817
|
/**
|
|
3818
|
+
* Host alias a request URL names (`?alias=`), or `null` when it names none.
|
|
3819
|
+
*
|
|
3820
|
+
* Only the routes know what a request is about; the response writer needs that
|
|
3821
|
+
* in order to attach only THIS host's notices (see `scopeHostNotices`).
|
|
3822
|
+
*/
|
|
3823
|
+
function urlAliasOf(req) {
|
|
3824
|
+
const alias = new URL(req.url ?? "/", "http://localhost").searchParams.get("alias");
|
|
3825
|
+
return alias === null || alias === "" ? null : alias;
|
|
3826
|
+
}
|
|
3827
|
+
/** Host alias a parsed JSON body names, or `null` when it names none. */
|
|
3828
|
+
function bodyAliasOf(value) {
|
|
3829
|
+
if (typeof value !== "object" || value === null) return null;
|
|
3830
|
+
const alias = value.alias;
|
|
3831
|
+
return typeof alias === "string" && alias !== "" ? alias : null;
|
|
3832
|
+
}
|
|
3833
|
+
/**
|
|
3512
3834
|
* A caller mistake the route can name: well-formed HTTP, unusable payload.
|
|
3513
3835
|
*
|
|
3514
3836
|
* `/files` used to throw plain Errors for missing fields and map EVERY failure
|
|
@@ -3558,6 +3880,10 @@ async function readBodyOrError(req, res, opts = {}) {
|
|
|
3558
3880
|
writeJson(res, 400, { error: "invalid JSON body" });
|
|
3559
3881
|
return { ok: false };
|
|
3560
3882
|
}
|
|
3883
|
+
if (result.kind === "ok") {
|
|
3884
|
+
const alias = bodyAliasOf(result.value);
|
|
3885
|
+
if (alias !== null) scopeHostNotices(res, alias);
|
|
3886
|
+
}
|
|
3561
3887
|
return {
|
|
3562
3888
|
ok: true,
|
|
3563
3889
|
body: result.kind === "ok" ? result.value : null
|
|
@@ -3678,6 +4004,49 @@ function makeRoutes(deps) {
|
|
|
3678
4004
|
const forwards = deps.forwards;
|
|
3679
4005
|
const viewSettings = deps.viewSettings ?? new RemoteViewSettingsStore();
|
|
3680
4006
|
/**
|
|
4007
|
+
* One noServer WebSocket server for terminal upgrades (RFC 6455).
|
|
4008
|
+
*
|
|
4009
|
+
* `maxPayload` is the authoritative frame bound: without it ws buffers up to
|
|
4010
|
+
* its 100 MiB default before the handler ever sees the message, in the
|
|
4011
|
+
* process that also runs the agent. Terminal frames are small JSON control
|
|
4012
|
+
* messages.
|
|
4013
|
+
*
|
|
4014
|
+
* It belongs to the route set, not to the module: as a module-level singleton
|
|
4015
|
+
* a plugin unload (and every later reload with a fresh module instance) left
|
|
4016
|
+
* the old server object alive forever. It must NOT close per route rebuild —
|
|
4017
|
+
* live PTYs survive a settings edit, and ws answers an upgrade on a closed
|
|
4018
|
+
* server with 503 — so teardown is bound to the fiber (the unload boundary);
|
|
4019
|
+
* a later load builds a fresh instance.
|
|
4020
|
+
*/
|
|
4021
|
+
const terminalWss = new WebSocketServer({
|
|
4022
|
+
noServer: true,
|
|
4023
|
+
maxPayload: TERMINAL_MAX_FRAME_BYTES
|
|
4024
|
+
});
|
|
4025
|
+
/** Idempotent teardown of the terminal upgrade server (plugin unload). */
|
|
4026
|
+
const close = () => {
|
|
4027
|
+
try {
|
|
4028
|
+
terminalWss.close();
|
|
4029
|
+
} catch {}
|
|
4030
|
+
};
|
|
4031
|
+
if (typeof ctx.effect === "function") ctx.effect(() => close, "dsh-remote-vs: terminal websocket server");
|
|
4032
|
+
/**
|
|
4033
|
+
* A corrupt store is a service-side failure with a path the user can repair.
|
|
4034
|
+
*
|
|
4035
|
+
* `store.list()`/`listWorkspaces()` re-read the JSON file, and a bad file
|
|
4036
|
+
* threw straight out of the route: the host webserver answered a bare 400
|
|
4037
|
+
* with no body, so the GUI could only report "request failed" and the path
|
|
4038
|
+
* that needs fixing never reached the person who could fix it.
|
|
4039
|
+
*/
|
|
4040
|
+
const writeStoreError = (res, error) => {
|
|
4041
|
+
writeJson(res, 503, {
|
|
4042
|
+
code: "store-corrupt",
|
|
4043
|
+
error: error instanceof Error ? error.message : String(error),
|
|
4044
|
+
path: store.filePath
|
|
4045
|
+
});
|
|
4046
|
+
};
|
|
4047
|
+
/** Did the store's own JSON parse/validate fail? (`store.filePath` names it.) */
|
|
4048
|
+
const isStoreError = (error) => error instanceof Error && error.message.includes(store.filePath);
|
|
4049
|
+
/**
|
|
3681
4050
|
* Stream one remote `tar -czf -` command into a `.tar.gz` download response.
|
|
3682
4051
|
*
|
|
3683
4052
|
* Shared by the directory and multi-selection routes (they differ only in the
|
|
@@ -3776,60 +4145,26 @@ function makeRoutes(deps) {
|
|
|
3776
4145
|
skipped
|
|
3777
4146
|
};
|
|
3778
4147
|
};
|
|
3779
|
-
|
|
3780
|
-
|
|
3781
|
-
|
|
3782
|
-
|
|
3783
|
-
|
|
3784
|
-
|
|
3785
|
-
|
|
3786
|
-
|
|
3787
|
-
|
|
3788
|
-
|
|
3789
|
-
|
|
3790
|
-
|
|
3791
|
-
|
|
4148
|
+
const routes = [
|
|
4149
|
+
{
|
|
4150
|
+
kind: "exact",
|
|
4151
|
+
path: REMOTE_API.hosts,
|
|
4152
|
+
handler: async (req, res) => {
|
|
4153
|
+
if (!isLoopbackRequest(req)) {
|
|
4154
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4155
|
+
return;
|
|
4156
|
+
}
|
|
4157
|
+
const method = req.method ?? "GET";
|
|
4158
|
+
const url = new URL(req.url ?? "/", "http://localhost");
|
|
4159
|
+
if (method === "GET") {
|
|
4160
|
+
try {
|
|
3792
4161
|
writeJson(res, 200, { hosts: store.list().map((h) => store.summarize(h)) });
|
|
3793
|
-
|
|
3794
|
-
|
|
3795
|
-
if (method === "POST") {
|
|
3796
|
-
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
3797
|
-
if (!read.ok) return;
|
|
3798
|
-
const body = read.body;
|
|
3799
|
-
if (body === null) {
|
|
3800
|
-
writeJson(res, 400, { error: "invalid JSON body" });
|
|
3801
|
-
return;
|
|
3802
|
-
}
|
|
3803
|
-
try {
|
|
3804
|
-
const entry = store.create(body);
|
|
3805
|
-
writeJson(res, 201, { host: store.summarize(entry) });
|
|
3806
|
-
} catch (error) {
|
|
3807
|
-
writeJson(res, 400, { error: error instanceof Error ? error.message : String(error) });
|
|
3808
|
-
}
|
|
3809
|
-
return;
|
|
3810
|
-
}
|
|
3811
|
-
if (method !== "PATCH" && method !== "DELETE") {
|
|
3812
|
-
writeJson(res, 405, { error: `method not allowed: ${method}` });
|
|
3813
|
-
return;
|
|
3814
|
-
}
|
|
3815
|
-
const alias = queryParam(url, "alias");
|
|
3816
|
-
if (alias === void 0 || alias === "") {
|
|
3817
|
-
writeJson(res, 400, { error: "alias query parameter is required" });
|
|
3818
|
-
return;
|
|
3819
|
-
}
|
|
3820
|
-
if (method === "DELETE") {
|
|
3821
|
-
forwards?.stopAlias(alias);
|
|
3822
|
-
pool.forget(alias);
|
|
3823
|
-
monitor.forget(alias);
|
|
3824
|
-
worlds.dropHost(alias).catch(() => void 0);
|
|
3825
|
-
if (!store.remove(alias)) {
|
|
3826
|
-
writeJson(res, 404, { error: `unknown host alias: ${alias}` });
|
|
3827
|
-
return;
|
|
3828
|
-
}
|
|
3829
|
-
worlds.syncWorkspaces().catch(() => void 0);
|
|
3830
|
-
writeJson(res, 200, { ok: true });
|
|
3831
|
-
return;
|
|
4162
|
+
} catch (error) {
|
|
4163
|
+
writeStoreError(res, error);
|
|
3832
4164
|
}
|
|
4165
|
+
return;
|
|
4166
|
+
}
|
|
4167
|
+
if (method === "POST") {
|
|
3833
4168
|
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
3834
4169
|
if (!read.ok) return;
|
|
3835
4170
|
const body = read.body;
|
|
@@ -3838,981 +4173,1083 @@ function makeRoutes(deps) {
|
|
|
3838
4173
|
return;
|
|
3839
4174
|
}
|
|
3840
4175
|
try {
|
|
3841
|
-
|
|
3842
|
-
|
|
3843
|
-
worlds.dropHost(alias).catch(() => void 0);
|
|
3844
|
-
const entry = store.update(alias, body);
|
|
3845
|
-
writeJson(res, 200, { host: store.summarize(entry) });
|
|
4176
|
+
const entry = store.create(body);
|
|
4177
|
+
writeJson(res, 201, { host: store.summarize(entry) });
|
|
3846
4178
|
} catch (error) {
|
|
3847
|
-
writeJson(res,
|
|
4179
|
+
writeJson(res, 400, { error: error instanceof Error ? error.message : String(error) });
|
|
3848
4180
|
}
|
|
4181
|
+
return;
|
|
3849
4182
|
}
|
|
3850
|
-
|
|
3851
|
-
|
|
3852
|
-
|
|
3853
|
-
path: REMOTE_API.status,
|
|
3854
|
-
handler: (req, res) => {
|
|
3855
|
-
if (!isLoopbackRequest(req)) {
|
|
3856
|
-
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
3857
|
-
return;
|
|
3858
|
-
}
|
|
3859
|
-
if (req.method !== "GET") {
|
|
3860
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
3861
|
-
return;
|
|
3862
|
-
}
|
|
3863
|
-
writeJson(res, 200, { connections: pool.status() });
|
|
4183
|
+
if (method !== "PATCH" && method !== "DELETE") {
|
|
4184
|
+
writeJson(res, 405, { error: `method not allowed: ${method}` });
|
|
4185
|
+
return;
|
|
3864
4186
|
}
|
|
3865
|
-
|
|
3866
|
-
|
|
3867
|
-
|
|
3868
|
-
|
|
3869
|
-
handler: async (req, res) => {
|
|
3870
|
-
if (!isLoopbackRequest(req)) {
|
|
3871
|
-
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
3872
|
-
return;
|
|
3873
|
-
}
|
|
3874
|
-
if (req.method !== "POST") {
|
|
3875
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
3876
|
-
return;
|
|
3877
|
-
}
|
|
3878
|
-
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
3879
|
-
if (!read.ok) return;
|
|
3880
|
-
const body = read.body;
|
|
3881
|
-
if (body === null || typeof body.alias !== "string") {
|
|
3882
|
-
writeJson(res, 400, { error: "alias is required" });
|
|
3883
|
-
return;
|
|
3884
|
-
}
|
|
3885
|
-
writeJson(res, 200, await pool.test(body.alias));
|
|
4187
|
+
const alias = queryParam(url, "alias");
|
|
4188
|
+
if (alias === void 0 || alias === "") {
|
|
4189
|
+
writeJson(res, 400, { error: "alias query parameter is required" });
|
|
4190
|
+
return;
|
|
3886
4191
|
}
|
|
3887
|
-
|
|
3888
|
-
|
|
3889
|
-
|
|
3890
|
-
|
|
3891
|
-
|
|
3892
|
-
if (!
|
|
3893
|
-
writeJson(res,
|
|
3894
|
-
return;
|
|
3895
|
-
}
|
|
3896
|
-
if (req.method !== "POST") {
|
|
3897
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
3898
|
-
return;
|
|
3899
|
-
}
|
|
3900
|
-
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
3901
|
-
if (!read.ok) return;
|
|
3902
|
-
const body = read.body;
|
|
3903
|
-
if (body === null || typeof body.alias !== "string") {
|
|
3904
|
-
writeJson(res, 400, { error: "alias is required" });
|
|
4192
|
+
if (method === "DELETE") {
|
|
4193
|
+
forwards?.stopAlias(alias);
|
|
4194
|
+
pool.forget(alias);
|
|
4195
|
+
monitor.forget(alias);
|
|
4196
|
+
worlds.dropHost(alias).catch(() => void 0);
|
|
4197
|
+
if (!store.remove(alias)) {
|
|
4198
|
+
writeJson(res, 404, { error: `unknown host alias: ${alias}` });
|
|
3905
4199
|
return;
|
|
3906
4200
|
}
|
|
3907
|
-
|
|
3908
|
-
|
|
3909
|
-
|
|
3910
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
3911
|
-
writeJson(res, message.startsWith("unknown host alias") ? 404 : 502, { error: message });
|
|
3912
|
-
}
|
|
4201
|
+
worlds.syncWorkspaces().catch(() => void 0);
|
|
4202
|
+
writeJson(res, 200, { ok: true });
|
|
4203
|
+
return;
|
|
3913
4204
|
}
|
|
3914
|
-
|
|
3915
|
-
|
|
3916
|
-
|
|
3917
|
-
|
|
3918
|
-
|
|
3919
|
-
|
|
3920
|
-
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
3921
|
-
return;
|
|
3922
|
-
}
|
|
3923
|
-
if (req.method !== "POST") {
|
|
3924
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
3925
|
-
return;
|
|
3926
|
-
}
|
|
3927
|
-
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
3928
|
-
if (!read.ok) return;
|
|
3929
|
-
const body = read.body;
|
|
3930
|
-
if (body === null || typeof body.alias !== "string" || typeof body.command !== "string" || body.command === "") {
|
|
3931
|
-
writeJson(res, 400, { error: "alias and a non-empty command are required" });
|
|
3932
|
-
return;
|
|
3933
|
-
}
|
|
3934
|
-
const timeoutMs = clampExecTimeout(body.timeoutMs);
|
|
3935
|
-
writeJson(res, 200, await pool.exec(body.alias, body.command, timeoutMs === void 0 ? {} : { timeoutMs }));
|
|
4205
|
+
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4206
|
+
if (!read.ok) return;
|
|
4207
|
+
const body = read.body;
|
|
4208
|
+
if (body === null) {
|
|
4209
|
+
writeJson(res, 400, { error: "invalid JSON body" });
|
|
4210
|
+
return;
|
|
3936
4211
|
}
|
|
3937
|
-
|
|
3938
|
-
|
|
3939
|
-
|
|
3940
|
-
|
|
3941
|
-
|
|
3942
|
-
|
|
3943
|
-
|
|
3944
|
-
|
|
3945
|
-
}
|
|
3946
|
-
if (req.method !== "POST") {
|
|
3947
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
3948
|
-
return;
|
|
3949
|
-
}
|
|
3950
|
-
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
3951
|
-
if (!read.ok) return;
|
|
3952
|
-
const body = read.body;
|
|
3953
|
-
if (body === null || typeof body.alias !== "string" || typeof body.command !== "string" || body.command === "") {
|
|
3954
|
-
writeJson(res, 400, { error: "alias and a non-empty command are required" });
|
|
3955
|
-
return;
|
|
3956
|
-
}
|
|
3957
|
-
const timeoutMs = clampExecTimeout(body.timeoutMs);
|
|
3958
|
-
res.writeHead(200, {
|
|
3959
|
-
"Content-Type": "text/event-stream; charset=utf-8",
|
|
3960
|
-
"Cache-Control": "no-cache, no-transform",
|
|
3961
|
-
Connection: "keep-alive",
|
|
3962
|
-
"X-Accel-Buffering": "no"
|
|
3963
|
-
});
|
|
3964
|
-
res.flushHeaders();
|
|
3965
|
-
/** No more events to the client (finished, or the socket broke). */
|
|
3966
|
-
let outputClosed = false;
|
|
3967
|
-
/** `res.end` already called. */
|
|
3968
|
-
let ended = false;
|
|
3969
|
-
/** The client asked to stop: kill the remote even if the read is still opening. */
|
|
3970
|
-
let cancelRequested = false;
|
|
3971
|
-
/** The cancel handle was invoked (only once). */
|
|
3972
|
-
let cancelSent = false;
|
|
3973
|
-
let cancel;
|
|
3974
|
-
/** Server-side forward cap: the client shows a scrolling tail anyway. */
|
|
3975
|
-
let sent = 0;
|
|
3976
|
-
const FORWARD_CAP = 4 * 1024 * 1024;
|
|
3977
|
-
/**
|
|
3978
|
-
* Stop the remote process group exactly once.
|
|
3979
|
-
*
|
|
3980
|
-
* The disconnect may happen while `pool.exec` is still acquiring the
|
|
3981
|
-
* channel (slot queue, setsid probe, connect) — the handle does not exist
|
|
3982
|
-
* yet then, so the request is latched and fired the moment `onCancel`
|
|
3983
|
-
* arrives; otherwise a push would finish unseen and uncancellable.
|
|
3984
|
-
*/
|
|
3985
|
-
const abortRemote = () => {
|
|
3986
|
-
cancelRequested = true;
|
|
3987
|
-
if (cancel === void 0 || cancelSent) return;
|
|
3988
|
-
cancelSent = true;
|
|
3989
|
-
cancel();
|
|
3990
|
-
};
|
|
3991
|
-
const send = (event) => {
|
|
3992
|
-
if (outputClosed) return;
|
|
3993
|
-
try {
|
|
3994
|
-
res.write(`data: ${JSON.stringify(event)}\n\n`);
|
|
3995
|
-
} catch {
|
|
3996
|
-
outputClosed = true;
|
|
3997
|
-
abortRemote();
|
|
3998
|
-
}
|
|
3999
|
-
};
|
|
4000
|
-
const heartbeat = setInterval(() => {
|
|
4001
|
-
if (outputClosed) return;
|
|
4002
|
-
try {
|
|
4003
|
-
res.write(": ping\n\n");
|
|
4004
|
-
} catch {
|
|
4005
|
-
outputClosed = true;
|
|
4006
|
-
abortRemote();
|
|
4007
|
-
}
|
|
4008
|
-
}, 15e3);
|
|
4009
|
-
heartbeat.unref?.();
|
|
4010
|
-
const stop = () => {
|
|
4011
|
-
if (ended) return;
|
|
4012
|
-
ended = true;
|
|
4013
|
-
outputClosed = true;
|
|
4014
|
-
clearInterval(heartbeat);
|
|
4015
|
-
try {
|
|
4016
|
-
res.end();
|
|
4017
|
-
} catch {}
|
|
4018
|
-
};
|
|
4019
|
-
res.on("close", () => {
|
|
4020
|
-
abortRemote();
|
|
4021
|
-
stop();
|
|
4022
|
-
});
|
|
4023
|
-
try {
|
|
4024
|
-
const result = await pool.exec(body.alias, body.command, {
|
|
4025
|
-
...timeoutMs === void 0 ? {} : { timeoutMs },
|
|
4026
|
-
onData: (stream, text) => {
|
|
4027
|
-
if (outputClosed || sent >= FORWARD_CAP) return;
|
|
4028
|
-
sent += Buffer.byteLength(text, "utf8");
|
|
4029
|
-
send({
|
|
4030
|
-
t: stream,
|
|
4031
|
-
d: text
|
|
4032
|
-
});
|
|
4033
|
-
},
|
|
4034
|
-
onCancel: (handle) => {
|
|
4035
|
-
cancel = handle;
|
|
4036
|
-
if (cancelRequested && !cancelSent) {
|
|
4037
|
-
cancelSent = true;
|
|
4038
|
-
handle();
|
|
4039
|
-
}
|
|
4040
|
-
}
|
|
4041
|
-
});
|
|
4042
|
-
if (outputClosed) return;
|
|
4043
|
-
send({
|
|
4044
|
-
t: "exit",
|
|
4045
|
-
success: result.success,
|
|
4046
|
-
exitCode: result.exitCode,
|
|
4047
|
-
timedOut: result.timedOut,
|
|
4048
|
-
durationMs: result.durationMs,
|
|
4049
|
-
...result.error === void 0 ? {} : { error: result.error }
|
|
4050
|
-
});
|
|
4051
|
-
} catch (error) {
|
|
4052
|
-
send({
|
|
4053
|
-
t: "exit",
|
|
4054
|
-
success: false,
|
|
4055
|
-
exitCode: null,
|
|
4056
|
-
timedOut: false,
|
|
4057
|
-
durationMs: 0,
|
|
4058
|
-
error: error instanceof Error ? error.message : String(error)
|
|
4059
|
-
});
|
|
4060
|
-
} finally {
|
|
4061
|
-
stop();
|
|
4062
|
-
}
|
|
4212
|
+
try {
|
|
4213
|
+
pool.forget(alias);
|
|
4214
|
+
monitor.forget(alias);
|
|
4215
|
+
worlds.dropHost(alias).catch(() => void 0);
|
|
4216
|
+
const entry = store.update(alias, body);
|
|
4217
|
+
writeJson(res, 200, { host: store.summarize(entry) });
|
|
4218
|
+
} catch (error) {
|
|
4219
|
+
writeJson(res, (error instanceof Error ? error.message : "").startsWith("unknown host alias") ? 404 : 400, { error: error instanceof Error ? error.message : String(error) });
|
|
4063
4220
|
}
|
|
4064
|
-
}
|
|
4065
|
-
|
|
4066
|
-
|
|
4067
|
-
|
|
4068
|
-
|
|
4069
|
-
|
|
4070
|
-
|
|
4071
|
-
|
|
4072
|
-
|
|
4073
|
-
|
|
4074
|
-
|
|
4075
|
-
|
|
4076
|
-
|
|
4077
|
-
|
|
4078
|
-
|
|
4079
|
-
|
|
4080
|
-
|
|
4081
|
-
|
|
4082
|
-
|
|
4083
|
-
|
|
4084
|
-
|
|
4085
|
-
|
|
4086
|
-
|
|
4087
|
-
|
|
4088
|
-
|
|
4089
|
-
|
|
4090
|
-
}
|
|
4091
|
-
|
|
4092
|
-
|
|
4093
|
-
|
|
4094
|
-
|
|
4095
|
-
|
|
4096
|
-
|
|
4097
|
-
|
|
4098
|
-
|
|
4099
|
-
|
|
4100
|
-
|
|
4101
|
-
|
|
4102
|
-
|
|
4103
|
-
|
|
4104
|
-
|
|
4105
|
-
|
|
4106
|
-
|
|
4107
|
-
|
|
4108
|
-
|
|
4109
|
-
|
|
4110
|
-
|
|
4111
|
-
|
|
4112
|
-
|
|
4113
|
-
|
|
4114
|
-
|
|
4115
|
-
|
|
4116
|
-
|
|
4117
|
-
|
|
4118
|
-
|
|
4119
|
-
|
|
4120
|
-
|
|
4121
|
-
|
|
4122
|
-
|
|
4123
|
-
|
|
4124
|
-
|
|
4125
|
-
|
|
4126
|
-
|
|
4127
|
-
|
|
4128
|
-
|
|
4129
|
-
|
|
4130
|
-
|
|
4131
|
-
|
|
4132
|
-
|
|
4133
|
-
|
|
4221
|
+
}
|
|
4222
|
+
},
|
|
4223
|
+
{
|
|
4224
|
+
kind: "exact",
|
|
4225
|
+
path: REMOTE_API.status,
|
|
4226
|
+
handler: (req, res) => {
|
|
4227
|
+
if (!isLoopbackRequest(req)) {
|
|
4228
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4229
|
+
return;
|
|
4230
|
+
}
|
|
4231
|
+
if (req.method !== "GET") {
|
|
4232
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4233
|
+
return;
|
|
4234
|
+
}
|
|
4235
|
+
writeJson(res, 200, { connections: pool.status() });
|
|
4236
|
+
}
|
|
4237
|
+
},
|
|
4238
|
+
{
|
|
4239
|
+
kind: "exact",
|
|
4240
|
+
path: REMOTE_API.test,
|
|
4241
|
+
handler: async (req, res) => {
|
|
4242
|
+
if (!isLoopbackRequest(req)) {
|
|
4243
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4244
|
+
return;
|
|
4245
|
+
}
|
|
4246
|
+
if (req.method !== "POST") {
|
|
4247
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4248
|
+
return;
|
|
4249
|
+
}
|
|
4250
|
+
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4251
|
+
if (!read.ok) return;
|
|
4252
|
+
const body = read.body;
|
|
4253
|
+
if (body === null || typeof body.alias !== "string") {
|
|
4254
|
+
writeJson(res, 400, { error: "alias is required" });
|
|
4255
|
+
return;
|
|
4256
|
+
}
|
|
4257
|
+
writeJson(res, 200, await pool.test(body.alias));
|
|
4258
|
+
}
|
|
4259
|
+
},
|
|
4260
|
+
{
|
|
4261
|
+
kind: "exact",
|
|
4262
|
+
path: REMOTE_API.keygen,
|
|
4263
|
+
handler: async (req, res) => {
|
|
4264
|
+
if (!isLoopbackRequest(req)) {
|
|
4265
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4266
|
+
return;
|
|
4267
|
+
}
|
|
4268
|
+
if (req.method !== "POST") {
|
|
4269
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4270
|
+
return;
|
|
4271
|
+
}
|
|
4272
|
+
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4273
|
+
if (!read.ok) return;
|
|
4274
|
+
const body = read.body;
|
|
4275
|
+
if (body === null || typeof body.alias !== "string") {
|
|
4276
|
+
writeJson(res, 400, { error: "alias is required" });
|
|
4277
|
+
return;
|
|
4278
|
+
}
|
|
4279
|
+
try {
|
|
4280
|
+
writeJson(res, 200, await bootstrap(pool, store, body.alias));
|
|
4281
|
+
} catch (error) {
|
|
4282
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
4283
|
+
writeJson(res, message.startsWith("unknown host alias") ? 404 : 502, { error: message });
|
|
4284
|
+
}
|
|
4285
|
+
}
|
|
4286
|
+
},
|
|
4287
|
+
{
|
|
4288
|
+
kind: "exact",
|
|
4289
|
+
path: REMOTE_API.exec,
|
|
4290
|
+
handler: async (req, res) => {
|
|
4291
|
+
if (!isLoopbackRequest(req)) {
|
|
4292
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4293
|
+
return;
|
|
4294
|
+
}
|
|
4295
|
+
if (req.method !== "POST") {
|
|
4296
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4297
|
+
return;
|
|
4298
|
+
}
|
|
4299
|
+
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4300
|
+
if (!read.ok) return;
|
|
4301
|
+
const body = read.body;
|
|
4302
|
+
if (body === null || typeof body.alias !== "string" || typeof body.command !== "string" || body.command === "") {
|
|
4303
|
+
writeJson(res, 400, { error: "alias and a non-empty command are required" });
|
|
4304
|
+
return;
|
|
4305
|
+
}
|
|
4306
|
+
const timeoutMs = clampExecTimeout(body.timeoutMs);
|
|
4307
|
+
/** The client closed the request: its response is never read. */
|
|
4308
|
+
let clientGone = false;
|
|
4309
|
+
/** The client asked to stop: kill the remote even if the read is still opening. */
|
|
4310
|
+
let cancelRequested = false;
|
|
4311
|
+
/** The cancel handle was invoked (only once). */
|
|
4312
|
+
let cancelSent = false;
|
|
4313
|
+
let cancel;
|
|
4314
|
+
/**
|
|
4315
|
+
* Stop the remote process group exactly once.
|
|
4316
|
+
*
|
|
4317
|
+
* Same latch as the streaming route: the disconnect may land while
|
|
4318
|
+
* `pool.exec` is still acquiring the channel (slot queue, setsid probe,
|
|
4319
|
+
* connect), where no handle exists yet — the request is remembered and
|
|
4320
|
+
* fired the moment `onCancel` supplies one. Without this the command kept
|
|
4321
|
+
* running to its timeout after a page refresh, holding a channel slot and
|
|
4322
|
+
* remote CPU for nobody.
|
|
4323
|
+
*/
|
|
4324
|
+
const abortRemote = () => {
|
|
4325
|
+
cancelRequested = true;
|
|
4326
|
+
if (cancel === void 0 || cancelSent) return;
|
|
4327
|
+
cancelSent = true;
|
|
4328
|
+
cancel();
|
|
4329
|
+
};
|
|
4330
|
+
res.on("close", () => {
|
|
4331
|
+
clientGone = true;
|
|
4332
|
+
abortRemote();
|
|
4333
|
+
});
|
|
4334
|
+
try {
|
|
4335
|
+
const result = await pool.exec(body.alias, body.command, {
|
|
4336
|
+
...timeoutMs === void 0 ? {} : { timeoutMs },
|
|
4337
|
+
onCancel: (handle) => {
|
|
4338
|
+
cancel = handle;
|
|
4339
|
+
if (cancelRequested && !cancelSent) {
|
|
4340
|
+
cancelSent = true;
|
|
4341
|
+
handle();
|
|
4134
4342
|
}
|
|
4135
|
-
case "mkdir":
|
|
4136
|
-
if (typeof body.path !== "string") throw new BadRequestError("path is required");
|
|
4137
|
-
await files.mkdir(alias, body.path);
|
|
4138
|
-
writeJson(res, 200, { ok: true });
|
|
4139
|
-
return;
|
|
4140
|
-
case "rm":
|
|
4141
|
-
if (typeof body.path !== "string") throw new BadRequestError("path is required");
|
|
4142
|
-
await files.remove(alias, body.path, body.recursive === true);
|
|
4143
|
-
writeJson(res, 200, { ok: true });
|
|
4144
|
-
return;
|
|
4145
|
-
case "rename":
|
|
4146
|
-
if (typeof body.from !== "string" || typeof body.to !== "string") throw new BadRequestError("from and to are required");
|
|
4147
|
-
await files.rename(alias, body.from, body.to);
|
|
4148
|
-
writeJson(res, 200, { ok: true });
|
|
4149
|
-
return;
|
|
4150
|
-
default: writeJson(res, 400, { error: `unknown op: ${body.op}` });
|
|
4151
4343
|
}
|
|
4152
|
-
}
|
|
4153
|
-
|
|
4154
|
-
|
|
4155
|
-
|
|
4156
|
-
* 409, not 502: the operation did not fail, it was DECLINED — and the
|
|
4157
|
-
* client needs the current version to offer overwrite / reload / see
|
|
4158
|
-
* the difference. Collapsing this into the generic remote-error path
|
|
4159
|
-
* is how a silent overwrite becomes a red toast nobody can act on.
|
|
4160
|
-
*/
|
|
4161
|
-
if (error instanceof WriteConflictError) {
|
|
4162
|
-
writeJson(res, 409, {
|
|
4163
|
-
error: error.message,
|
|
4164
|
-
code: "conflict",
|
|
4165
|
-
conflict: error.conflict
|
|
4166
|
-
});
|
|
4167
|
-
return;
|
|
4168
|
-
}
|
|
4169
|
-
writeJson(res, error instanceof BadRequestError ? 400 : 502, { error: error instanceof Error ? error.message : String(error) });
|
|
4170
|
-
}
|
|
4344
|
+
});
|
|
4345
|
+
if (!clientGone) writeJson(res, 200, result);
|
|
4346
|
+
} catch (error) {
|
|
4347
|
+
if (!clientGone) writeJson(res, 502, { error: error instanceof Error ? error.message : String(error) });
|
|
4171
4348
|
}
|
|
4172
|
-
}
|
|
4173
|
-
|
|
4174
|
-
|
|
4175
|
-
|
|
4176
|
-
|
|
4177
|
-
|
|
4178
|
-
|
|
4179
|
-
|
|
4180
|
-
|
|
4181
|
-
|
|
4182
|
-
|
|
4183
|
-
|
|
4184
|
-
|
|
4185
|
-
|
|
4186
|
-
|
|
4187
|
-
|
|
4188
|
-
|
|
4189
|
-
|
|
4190
|
-
|
|
4191
|
-
|
|
4349
|
+
}
|
|
4350
|
+
},
|
|
4351
|
+
{
|
|
4352
|
+
kind: "exact",
|
|
4353
|
+
path: REMOTE_API.execStream,
|
|
4354
|
+
handler: async (req, res) => {
|
|
4355
|
+
if (!isLoopbackRequest(req)) {
|
|
4356
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4357
|
+
return;
|
|
4358
|
+
}
|
|
4359
|
+
if (req.method !== "POST") {
|
|
4360
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4361
|
+
return;
|
|
4362
|
+
}
|
|
4363
|
+
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4364
|
+
if (!read.ok) return;
|
|
4365
|
+
const body = read.body;
|
|
4366
|
+
if (body === null || typeof body.alias !== "string" || typeof body.command !== "string" || body.command === "") {
|
|
4367
|
+
writeJson(res, 400, { error: "alias and a non-empty command are required" });
|
|
4368
|
+
return;
|
|
4369
|
+
}
|
|
4370
|
+
const timeoutMs = clampExecTimeout(body.timeoutMs);
|
|
4371
|
+
res.writeHead(200, {
|
|
4372
|
+
"Content-Type": "text/event-stream; charset=utf-8",
|
|
4373
|
+
"Cache-Control": "no-cache, no-transform",
|
|
4374
|
+
Connection: "keep-alive",
|
|
4375
|
+
"X-Accel-Buffering": "no"
|
|
4376
|
+
});
|
|
4377
|
+
res.flushHeaders();
|
|
4378
|
+
/** No more events to the client (finished, or the socket broke). */
|
|
4379
|
+
let outputClosed = false;
|
|
4380
|
+
/** `res.end` already called. */
|
|
4381
|
+
let ended = false;
|
|
4382
|
+
/** The client asked to stop: kill the remote even if the read is still opening. */
|
|
4383
|
+
let cancelRequested = false;
|
|
4384
|
+
/** The cancel handle was invoked (only once). */
|
|
4385
|
+
let cancelSent = false;
|
|
4386
|
+
let cancel;
|
|
4387
|
+
/** Server-side forward cap: the client shows a scrolling tail anyway. */
|
|
4388
|
+
let sent = 0;
|
|
4389
|
+
const FORWARD_CAP = 4 * 1024 * 1024;
|
|
4390
|
+
/**
|
|
4391
|
+
* Stop the remote process group exactly once.
|
|
4392
|
+
*
|
|
4393
|
+
* The disconnect may happen while `pool.exec` is still acquiring the
|
|
4394
|
+
* channel (slot queue, setsid probe, connect) — the handle does not exist
|
|
4395
|
+
* yet then, so the request is latched and fired the moment `onCancel`
|
|
4396
|
+
* arrives; otherwise a push would finish unseen and uncancellable.
|
|
4397
|
+
*/
|
|
4398
|
+
const abortRemote = () => {
|
|
4399
|
+
cancelRequested = true;
|
|
4400
|
+
if (cancel === void 0 || cancelSent) return;
|
|
4401
|
+
cancelSent = true;
|
|
4402
|
+
cancel();
|
|
4403
|
+
};
|
|
4404
|
+
const send = (event) => {
|
|
4405
|
+
if (outputClosed) return;
|
|
4406
|
+
try {
|
|
4407
|
+
res.write(`data: ${JSON.stringify(event)}\n\n`);
|
|
4408
|
+
} catch {
|
|
4409
|
+
outputClosed = true;
|
|
4410
|
+
abortRemote();
|
|
4192
4411
|
}
|
|
4193
|
-
|
|
4194
|
-
|
|
4195
|
-
|
|
4412
|
+
};
|
|
4413
|
+
const heartbeat = setInterval(() => {
|
|
4414
|
+
if (outputClosed) return;
|
|
4415
|
+
try {
|
|
4416
|
+
res.write(": ping\n\n");
|
|
4417
|
+
} catch {
|
|
4418
|
+
outputClosed = true;
|
|
4419
|
+
abortRemote();
|
|
4196
4420
|
}
|
|
4421
|
+
}, 15e3);
|
|
4422
|
+
heartbeat.unref?.();
|
|
4423
|
+
const stop = () => {
|
|
4424
|
+
if (ended) return;
|
|
4425
|
+
ended = true;
|
|
4426
|
+
outputClosed = true;
|
|
4427
|
+
clearInterval(heartbeat);
|
|
4197
4428
|
try {
|
|
4198
|
-
|
|
4199
|
-
|
|
4200
|
-
|
|
4201
|
-
|
|
4202
|
-
|
|
4203
|
-
|
|
4204
|
-
|
|
4429
|
+
res.end();
|
|
4430
|
+
} catch {}
|
|
4431
|
+
};
|
|
4432
|
+
res.on("close", () => {
|
|
4433
|
+
abortRemote();
|
|
4434
|
+
stop();
|
|
4435
|
+
});
|
|
4436
|
+
try {
|
|
4437
|
+
const result = await pool.exec(body.alias, body.command, {
|
|
4438
|
+
...timeoutMs === void 0 ? {} : { timeoutMs },
|
|
4439
|
+
onData: (stream, text) => {
|
|
4440
|
+
if (outputClosed || sent >= FORWARD_CAP) return;
|
|
4441
|
+
sent += Buffer.byteLength(text, "utf8");
|
|
4442
|
+
send({
|
|
4443
|
+
t: stream,
|
|
4444
|
+
d: text
|
|
4445
|
+
});
|
|
4446
|
+
},
|
|
4447
|
+
onCancel: (handle) => {
|
|
4448
|
+
cancel = handle;
|
|
4449
|
+
if (cancelRequested && !cancelSent) {
|
|
4450
|
+
cancelSent = true;
|
|
4451
|
+
handle();
|
|
4205
4452
|
}
|
|
4206
4453
|
}
|
|
4207
|
-
|
|
4208
|
-
|
|
4209
|
-
|
|
4210
|
-
|
|
4211
|
-
|
|
4212
|
-
|
|
4213
|
-
|
|
4454
|
+
});
|
|
4455
|
+
if (outputClosed) return;
|
|
4456
|
+
send({
|
|
4457
|
+
t: "exit",
|
|
4458
|
+
success: result.success,
|
|
4459
|
+
exitCode: result.exitCode,
|
|
4460
|
+
timedOut: result.timedOut,
|
|
4461
|
+
durationMs: result.durationMs,
|
|
4462
|
+
...result.error === void 0 ? {} : { error: result.error }
|
|
4463
|
+
});
|
|
4464
|
+
} catch (error) {
|
|
4465
|
+
send({
|
|
4466
|
+
t: "exit",
|
|
4467
|
+
success: false,
|
|
4468
|
+
exitCode: null,
|
|
4469
|
+
timedOut: false,
|
|
4470
|
+
durationMs: 0,
|
|
4471
|
+
error: error instanceof Error ? error.message : String(error)
|
|
4472
|
+
});
|
|
4473
|
+
} finally {
|
|
4474
|
+
stop();
|
|
4214
4475
|
}
|
|
4215
|
-
}
|
|
4216
|
-
|
|
4217
|
-
|
|
4218
|
-
|
|
4219
|
-
|
|
4220
|
-
|
|
4221
|
-
|
|
4222
|
-
|
|
4223
|
-
|
|
4224
|
-
if (req.method !== "GET") {
|
|
4225
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4226
|
-
return;
|
|
4227
|
-
}
|
|
4228
|
-
const url = new URL(req.url ?? "/", "http://localhost");
|
|
4229
|
-
const alias = url.searchParams.get("alias") ?? "";
|
|
4230
|
-
const path = url.searchParams.get("path") ?? "";
|
|
4231
|
-
if (alias === "" || path === "") {
|
|
4232
|
-
writeJson(res, 400, { error: "alias and path query parameters are required" });
|
|
4233
|
-
return;
|
|
4234
|
-
}
|
|
4235
|
-
if (!path.startsWith("/")) {
|
|
4236
|
-
writeJson(res, 400, { error: `path must be absolute: ${path}` });
|
|
4237
|
-
return;
|
|
4238
|
-
}
|
|
4239
|
-
try {
|
|
4240
|
-
await files.download(alias, path, res);
|
|
4241
|
-
} catch (error) {
|
|
4242
|
-
if (!res.headersSent && !res.destroyed) writeRouteError(res, error);
|
|
4243
|
-
else res.destroy(error instanceof Error ? error : void 0);
|
|
4244
|
-
}
|
|
4476
|
+
}
|
|
4477
|
+
},
|
|
4478
|
+
{
|
|
4479
|
+
kind: "exact",
|
|
4480
|
+
path: REMOTE_API.files,
|
|
4481
|
+
handler: async (req, res) => {
|
|
4482
|
+
if (!isLoopbackRequest(req)) {
|
|
4483
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4484
|
+
return;
|
|
4245
4485
|
}
|
|
4246
|
-
|
|
4247
|
-
|
|
4248
|
-
|
|
4249
|
-
path: REMOTE_API.downloadDir,
|
|
4250
|
-
handler: async (req, res) => {
|
|
4251
|
-
if (!isLoopbackRequest(req)) {
|
|
4252
|
-
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4253
|
-
return;
|
|
4254
|
-
}
|
|
4255
|
-
if (req.method !== "GET") {
|
|
4256
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4257
|
-
return;
|
|
4258
|
-
}
|
|
4259
|
-
const url = new URL(req.url ?? "/", "http://localhost");
|
|
4260
|
-
const alias = url.searchParams.get("alias") ?? "";
|
|
4261
|
-
const path = url.searchParams.get("path") ?? "";
|
|
4262
|
-
if (alias === "" || path === "") {
|
|
4263
|
-
writeJson(res, 400, { error: "alias and path query parameters are required" });
|
|
4264
|
-
return;
|
|
4265
|
-
}
|
|
4266
|
-
if (!path.startsWith("/")) {
|
|
4267
|
-
writeJson(res, 400, { error: `path must be absolute: ${path}` });
|
|
4268
|
-
return;
|
|
4269
|
-
}
|
|
4270
|
-
let command;
|
|
4271
|
-
try {
|
|
4272
|
-
if (!(await files.stat(alias, path)).isDir) {
|
|
4273
|
-
writeJson(res, 400, { error: `not a directory: ${path}` });
|
|
4274
|
-
return;
|
|
4275
|
-
}
|
|
4276
|
-
command = directoryArchiveCommand(path);
|
|
4277
|
-
} catch (error) {
|
|
4278
|
-
writeRouteError(res, error);
|
|
4279
|
-
return;
|
|
4280
|
-
}
|
|
4281
|
-
await streamArchive(alias, command, res, directoryArchiveName(path));
|
|
4486
|
+
if (req.method !== "POST") {
|
|
4487
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4488
|
+
return;
|
|
4282
4489
|
}
|
|
4283
|
-
|
|
4284
|
-
|
|
4285
|
-
|
|
4286
|
-
path: REMOTE_API.downloadMany,
|
|
4287
|
-
handler: async (req, res) => {
|
|
4288
|
-
if (!isLoopbackRequest(req)) {
|
|
4289
|
-
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4290
|
-
return;
|
|
4291
|
-
}
|
|
4292
|
-
if (req.method !== "GET") {
|
|
4293
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4294
|
-
return;
|
|
4295
|
-
}
|
|
4296
|
-
const url = new URL(req.url ?? "/", "http://localhost");
|
|
4297
|
-
const alias = url.searchParams.get("alias") ?? "";
|
|
4298
|
-
const paths = url.searchParams.getAll("path");
|
|
4299
|
-
if (alias === "" || paths.length === 0) {
|
|
4300
|
-
writeJson(res, 400, { error: "alias and at least one path query parameter are required" });
|
|
4301
|
-
return;
|
|
4302
|
-
}
|
|
4303
|
-
if (paths.length > 100) {
|
|
4304
|
-
writeJson(res, 400, { error: `too many entries: ${paths.length} (max 100)` });
|
|
4305
|
-
return;
|
|
4306
|
-
}
|
|
4307
|
-
for (const path of paths) if (!path.startsWith("/")) {
|
|
4308
|
-
writeJson(res, 400, { error: `path must be absolute: ${path}` });
|
|
4309
|
-
return;
|
|
4310
|
-
}
|
|
4311
|
-
let command;
|
|
4312
|
-
try {
|
|
4313
|
-
for (const path of paths) await files.stat(alias, path);
|
|
4314
|
-
command = selectionArchiveCommand(paths);
|
|
4315
|
-
} catch (error) {
|
|
4316
|
-
writeRouteError(res, error);
|
|
4317
|
-
return;
|
|
4318
|
-
}
|
|
4319
|
-
await streamArchive(alias, command, res, selectionDownloadName(paths.length));
|
|
4490
|
+
if (bodyTooLarge(req, FILES_BODY_LIMIT)) {
|
|
4491
|
+
writePayloadTooLarge(req, res, FILES_BODY_LIMIT);
|
|
4492
|
+
return;
|
|
4320
4493
|
}
|
|
4321
|
-
|
|
4322
|
-
|
|
4323
|
-
|
|
4324
|
-
|
|
4325
|
-
|
|
4326
|
-
|
|
4327
|
-
|
|
4328
|
-
|
|
4329
|
-
|
|
4330
|
-
|
|
4331
|
-
|
|
4332
|
-
|
|
4333
|
-
|
|
4334
|
-
|
|
4335
|
-
|
|
4336
|
-
|
|
4337
|
-
|
|
4338
|
-
|
|
4339
|
-
|
|
4340
|
-
|
|
4341
|
-
|
|
4342
|
-
|
|
4343
|
-
|
|
4344
|
-
|
|
4345
|
-
|
|
4346
|
-
|
|
4347
|
-
|
|
4348
|
-
|
|
4349
|
-
|
|
4350
|
-
|
|
4351
|
-
|
|
4352
|
-
|
|
4494
|
+
const read = await readBodyOrError(req, res, {
|
|
4495
|
+
objectOnly: true,
|
|
4496
|
+
maxBytes: FILES_BODY_LIMIT
|
|
4497
|
+
});
|
|
4498
|
+
if (!read.ok) return;
|
|
4499
|
+
const body = read.body;
|
|
4500
|
+
if (body === null || typeof body.op !== "string" || typeof body.alias !== "string") {
|
|
4501
|
+
writeJson(res, 400, { error: "op and alias are required" });
|
|
4502
|
+
return;
|
|
4503
|
+
}
|
|
4504
|
+
const alias = body.alias;
|
|
4505
|
+
try {
|
|
4506
|
+
switch (body.op) {
|
|
4507
|
+
case "list":
|
|
4508
|
+
if (typeof body.path !== "string") throw new BadRequestError("path is required");
|
|
4509
|
+
writeJson(res, 200, { entries: await files.list(alias, body.path) });
|
|
4510
|
+
return;
|
|
4511
|
+
case "stat":
|
|
4512
|
+
if (typeof body.path !== "string") throw new BadRequestError("path is required");
|
|
4513
|
+
writeJson(res, 200, { entry: await files.stat(alias, body.path) });
|
|
4514
|
+
return;
|
|
4515
|
+
case "read":
|
|
4516
|
+
if (typeof body.path !== "string") throw new BadRequestError("path is required");
|
|
4517
|
+
writeJson(res, 200, await files.read(alias, body.path));
|
|
4518
|
+
return;
|
|
4519
|
+
case "write": {
|
|
4520
|
+
if (typeof body.path !== "string" || typeof body.content !== "string") throw new BadRequestError("path and content are required");
|
|
4521
|
+
const encoding = body.encoding === "base64" ? "base64" : "utf8";
|
|
4522
|
+
/**
|
|
4523
|
+
* Optional optimistic lock. `expectMtime` is the version the client
|
|
4524
|
+
* loaded; when present the Host refuses a file that changed since.
|
|
4525
|
+
* Requests without it (helpers, deploy, the agent's write tool)
|
|
4526
|
+
* keep the old unconditional semantics — the guard protects a
|
|
4527
|
+
* human's buffer, it must not make programmatic writes fail.
|
|
4528
|
+
*/
|
|
4529
|
+
const expectMtime = body.expectMtime;
|
|
4530
|
+
const expectSize = body.expectSize;
|
|
4531
|
+
/**
|
|
4532
|
+
* A malformed baseline is a CALLER mistake, not "no baseline".
|
|
4533
|
+
*
|
|
4534
|
+
* Silently dropping it would turn `{expectMtime: "123"}` (a client
|
|
4535
|
+
* bug, a serializer change) into an UNGUARDED write — the exact
|
|
4536
|
+
* overwrite the caller was asking to be protected from, reported as
|
|
4537
|
+
* a success.
|
|
4538
|
+
*/
|
|
4539
|
+
if (expectMtime !== void 0 && (typeof expectMtime !== "number" || !Number.isFinite(expectMtime))) throw new BadRequestError("expectMtime must be a finite number");
|
|
4540
|
+
if (expectSize !== void 0 && (typeof expectSize !== "number" || !Number.isFinite(expectSize))) throw new BadRequestError("expectSize must be a finite number");
|
|
4541
|
+
const guard = typeof expectMtime === "number" ? {
|
|
4542
|
+
expectMtime,
|
|
4543
|
+
...typeof expectSize === "number" ? { expectSize } : {}
|
|
4544
|
+
} : void 0;
|
|
4545
|
+
writeJson(res, 200, await files.write(alias, body.path, body.content, encoding, guard));
|
|
4353
4546
|
return;
|
|
4354
4547
|
}
|
|
4355
|
-
|
|
4356
|
-
|
|
4357
|
-
|
|
4358
|
-
|
|
4359
|
-
|
|
4360
|
-
|
|
4361
|
-
|
|
4362
|
-
|
|
4363
|
-
|
|
4364
|
-
|
|
4365
|
-
|
|
4366
|
-
|
|
4367
|
-
|
|
4368
|
-
|
|
4369
|
-
|
|
4370
|
-
}
|
|
4371
|
-
return;
|
|
4372
|
-
}
|
|
4373
|
-
if (method !== "DELETE") {
|
|
4374
|
-
writeJson(res, 405, { error: `method not allowed: ${method}` });
|
|
4375
|
-
return;
|
|
4376
|
-
}
|
|
4377
|
-
const id = queryParam(url, "id");
|
|
4378
|
-
if (id === void 0 || id === "") {
|
|
4379
|
-
writeJson(res, 400, { error: "id query parameter is required" });
|
|
4380
|
-
return;
|
|
4548
|
+
case "mkdir":
|
|
4549
|
+
if (typeof body.path !== "string") throw new BadRequestError("path is required");
|
|
4550
|
+
await files.mkdir(alias, body.path);
|
|
4551
|
+
writeJson(res, 200, { ok: true });
|
|
4552
|
+
return;
|
|
4553
|
+
case "rm":
|
|
4554
|
+
if (typeof body.path !== "string") throw new BadRequestError("path is required");
|
|
4555
|
+
await files.remove(alias, body.path, body.recursive === true);
|
|
4556
|
+
writeJson(res, 200, { ok: true });
|
|
4557
|
+
return;
|
|
4558
|
+
case "rename":
|
|
4559
|
+
if (typeof body.from !== "string" || typeof body.to !== "string") throw new BadRequestError("from and to are required");
|
|
4560
|
+
await files.rename(alias, body.from, body.to);
|
|
4561
|
+
writeJson(res, 200, { ok: true });
|
|
4562
|
+
return;
|
|
4563
|
+
default: writeJson(res, 400, { error: `unknown op: ${body.op}` });
|
|
4381
4564
|
}
|
|
4382
|
-
|
|
4383
|
-
|
|
4565
|
+
} catch (error) {
|
|
4566
|
+
/**
|
|
4567
|
+
* A save the remote side refused because the file moved on.
|
|
4568
|
+
*
|
|
4569
|
+
* 409, not 502: the operation did not fail, it was DECLINED — and the
|
|
4570
|
+
* client needs the current version to offer overwrite / reload / see
|
|
4571
|
+
* the difference. Collapsing this into the generic remote-error path
|
|
4572
|
+
* is how a silent overwrite becomes a red toast nobody can act on.
|
|
4573
|
+
*/
|
|
4574
|
+
if (error instanceof WriteConflictError) {
|
|
4575
|
+
writeJson(res, 409, {
|
|
4576
|
+
error: error.message,
|
|
4577
|
+
code: "conflict",
|
|
4578
|
+
conflict: error.conflict
|
|
4579
|
+
});
|
|
4384
4580
|
return;
|
|
4385
4581
|
}
|
|
4386
|
-
|
|
4387
|
-
writeJson(res, 200, { ok: true });
|
|
4582
|
+
writeJson(res, error instanceof BadRequestError ? 400 : 502, { error: error instanceof Error ? error.message : String(error) });
|
|
4388
4583
|
}
|
|
4389
|
-
}
|
|
4390
|
-
|
|
4391
|
-
|
|
4392
|
-
|
|
4393
|
-
|
|
4394
|
-
|
|
4395
|
-
|
|
4396
|
-
|
|
4397
|
-
|
|
4398
|
-
if ((req.method ?? "GET") !== "POST") {
|
|
4399
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method ?? "GET"}` });
|
|
4400
|
-
return;
|
|
4401
|
-
}
|
|
4402
|
-
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4403
|
-
if (!read.ok) return;
|
|
4404
|
-
const body = read.body;
|
|
4405
|
-
const sessionId = typeof body?.sessionId === "string" ? body.sessionId : "";
|
|
4406
|
-
if (sessionId === "") {
|
|
4407
|
-
writeJson(res, 400, { error: "sessionId must be a non-empty string" });
|
|
4408
|
-
return;
|
|
4409
|
-
}
|
|
4410
|
-
writeJson(res, 200, { cwd: liveSessionCwd(ctx, sessionId) });
|
|
4584
|
+
}
|
|
4585
|
+
},
|
|
4586
|
+
{
|
|
4587
|
+
kind: "exact",
|
|
4588
|
+
path: REMOTE_API.upload,
|
|
4589
|
+
handler: async (req, res) => {
|
|
4590
|
+
if (!isLoopbackRequest(req)) {
|
|
4591
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4592
|
+
return;
|
|
4411
4593
|
}
|
|
4412
|
-
|
|
4413
|
-
|
|
4414
|
-
|
|
4415
|
-
path: REMOTE_API.changes,
|
|
4416
|
-
handler: async (req, res) => {
|
|
4417
|
-
if (!isLoopbackRequest(req)) {
|
|
4418
|
-
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4419
|
-
return;
|
|
4420
|
-
}
|
|
4421
|
-
if ((req.method ?? "GET") !== "POST") {
|
|
4422
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method ?? "GET"}` });
|
|
4423
|
-
return;
|
|
4424
|
-
}
|
|
4425
|
-
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4426
|
-
if (!read.ok) return;
|
|
4427
|
-
const body = read.body;
|
|
4428
|
-
const sessionId = typeof body?.sessionId === "string" ? body.sessionId : "";
|
|
4429
|
-
const op = typeof body?.op === "string" ? body.op : "";
|
|
4430
|
-
if (sessionId === "" || op !== "list" && op !== "diff") {
|
|
4431
|
-
writeJson(res, 400, { error: "op (list|diff) and a non-empty sessionId are required" });
|
|
4432
|
-
return;
|
|
4433
|
-
}
|
|
4434
|
-
const ledger = deps.changes;
|
|
4435
|
-
if (ledger === void 0) {
|
|
4436
|
-
writeJson(res, 200, {
|
|
4437
|
-
summary: null,
|
|
4438
|
-
diff: null
|
|
4439
|
-
});
|
|
4440
|
-
return;
|
|
4441
|
-
}
|
|
4442
|
-
if (typeof body?.repoSubdir === "string" || body?.repoSubdir === null) ledger.useRepoSubdir(sessionId, typeof body.repoSubdir === "string" ? body.repoSubdir : void 0);
|
|
4443
|
-
if (op === "list") {
|
|
4444
|
-
if (ledger.summary(sessionId) === null) {
|
|
4445
|
-
const cwd = liveSessionCwd(ctx, sessionId);
|
|
4446
|
-
if (cwd !== void 0) ledger.beginTurn({
|
|
4447
|
-
sessionId,
|
|
4448
|
-
cwd
|
|
4449
|
-
});
|
|
4450
|
-
}
|
|
4451
|
-
if (body?.force === true) ledger.rescan(sessionId);
|
|
4452
|
-
else ledger.touch(sessionId);
|
|
4453
|
-
writeJson(res, 200, { summary: ledger.summary(sessionId) });
|
|
4454
|
-
return;
|
|
4455
|
-
}
|
|
4456
|
-
const path = typeof body?.path === "string" ? body.path : "";
|
|
4457
|
-
if (path === "") {
|
|
4458
|
-
writeJson(res, 400, { error: "path is required for op=diff" });
|
|
4459
|
-
return;
|
|
4460
|
-
}
|
|
4461
|
-
try {
|
|
4462
|
-
writeJson(res, 200, { diff: await ledger.diff(sessionId, path) });
|
|
4463
|
-
} catch (error) {
|
|
4464
|
-
writeRouteError(res, error);
|
|
4465
|
-
}
|
|
4594
|
+
if (req.method !== "POST") {
|
|
4595
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4596
|
+
return;
|
|
4466
4597
|
}
|
|
4467
|
-
|
|
4468
|
-
|
|
4469
|
-
|
|
4470
|
-
|
|
4471
|
-
|
|
4472
|
-
|
|
4473
|
-
|
|
4474
|
-
return;
|
|
4475
|
-
}
|
|
4476
|
-
if ((req.method ?? "GET") !== "POST") {
|
|
4477
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method ?? "GET"}` });
|
|
4478
|
-
return;
|
|
4479
|
-
}
|
|
4480
|
-
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4481
|
-
if (!read.ok) return;
|
|
4482
|
-
const mode = read.body?.mode;
|
|
4483
|
-
if (mode !== "off" && mode !== "shadow" && mode !== "enforce") {
|
|
4484
|
-
writeJson(res, 400, { error: `mode must be one of off/shadow/enforce (got ${JSON.stringify(mode) ?? "missing"})` });
|
|
4485
|
-
return;
|
|
4486
|
-
}
|
|
4487
|
-
try {
|
|
4488
|
-
const settings = ctx.get("settings");
|
|
4489
|
-
if (typeof settings?.update !== "function") {
|
|
4490
|
-
writeJson(res, 503, { error: "settings service unavailable (cannot persist routing mode)" });
|
|
4491
|
-
return;
|
|
4492
|
-
}
|
|
4493
|
-
const ns = ownSettingsNs(ctx) ?? "remote-vs";
|
|
4494
|
-
await settings.update(ns, { routingMode: mode });
|
|
4495
|
-
writeJson(res, 200, { mode: worlds.mode });
|
|
4496
|
-
} catch (error) {
|
|
4497
|
-
writeRouteError(res, error);
|
|
4498
|
-
}
|
|
4598
|
+
const url = new URL(req.url ?? "/", "http://localhost");
|
|
4599
|
+
const alias = url.searchParams.get("alias") ?? "";
|
|
4600
|
+
const path = url.searchParams.get("path") ?? "";
|
|
4601
|
+
const overwrite = url.searchParams.get("overwrite") === "1";
|
|
4602
|
+
if (alias === "" || path === "") {
|
|
4603
|
+
writeJson(res, 400, { error: "alias and path query parameters are required" });
|
|
4604
|
+
return;
|
|
4499
4605
|
}
|
|
4500
|
-
|
|
4501
|
-
|
|
4502
|
-
|
|
4503
|
-
path: REMOTE_API.viewSettings,
|
|
4504
|
-
handler: async (req, res) => {
|
|
4505
|
-
if (!isLoopbackRequest(req)) {
|
|
4506
|
-
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4507
|
-
return;
|
|
4508
|
-
}
|
|
4509
|
-
const method = req.method ?? "GET";
|
|
4510
|
-
if (method === "GET") {
|
|
4511
|
-
try {
|
|
4512
|
-
writeJson(res, 200, { settings: viewSettings.get() });
|
|
4513
|
-
} catch (error) {
|
|
4514
|
-
writeRouteError(res, error);
|
|
4515
|
-
}
|
|
4516
|
-
return;
|
|
4517
|
-
}
|
|
4518
|
-
if (method !== "POST") {
|
|
4519
|
-
writeJson(res, 405, { error: `method not allowed: ${method}` });
|
|
4520
|
-
return;
|
|
4521
|
-
}
|
|
4522
|
-
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4523
|
-
if (!read.ok) return;
|
|
4524
|
-
try {
|
|
4525
|
-
writeJson(res, 200, { settings: viewSettings.patch(read.body ?? {}) });
|
|
4526
|
-
} catch (error) {
|
|
4527
|
-
writeRouteError(res, error);
|
|
4528
|
-
}
|
|
4606
|
+
if (!path.startsWith("/")) {
|
|
4607
|
+
writeJson(res, 400, { error: `path must be absolute: ${path}` });
|
|
4608
|
+
return;
|
|
4529
4609
|
}
|
|
4530
|
-
|
|
4531
|
-
|
|
4532
|
-
|
|
4533
|
-
|
|
4534
|
-
|
|
4535
|
-
|
|
4536
|
-
|
|
4537
|
-
return;
|
|
4538
|
-
}
|
|
4539
|
-
const method = req.method ?? "GET";
|
|
4540
|
-
const url = new URL(req.url ?? "/", "http://localhost");
|
|
4541
|
-
if (method === "GET") {
|
|
4542
|
-
const alias = queryParam(url, "alias");
|
|
4543
|
-
const view = queryParam(url, "view") ?? "status";
|
|
4544
|
-
if (alias === void 0 || alias === "") {
|
|
4545
|
-
writeJson(res, 400, { error: "alias query parameter is required" });
|
|
4546
|
-
return;
|
|
4547
|
-
}
|
|
4548
|
-
if (view === "probe") {
|
|
4549
|
-
try {
|
|
4550
|
-
writeJson(res, 200, await probe(pool, alias));
|
|
4551
|
-
} catch (error) {
|
|
4552
|
-
writeRouteError(res, error);
|
|
4553
|
-
}
|
|
4554
|
-
return;
|
|
4555
|
-
}
|
|
4556
|
-
if (view === "status") {
|
|
4557
|
-
try {
|
|
4558
|
-
writeJson(res, 200, {
|
|
4559
|
-
deploy: store.require(alias).deploy ?? { status: "not-deployed" },
|
|
4560
|
-
helpers: helperAvailability()
|
|
4561
|
-
});
|
|
4562
|
-
} catch (error) {
|
|
4563
|
-
writeJson(res, 404, { error: error instanceof Error ? error.message : String(error) });
|
|
4564
|
-
}
|
|
4610
|
+
try {
|
|
4611
|
+
if (!overwrite) {
|
|
4612
|
+
if (await files.stat(alias, path).then(() => true, () => false)) {
|
|
4613
|
+
writeJson(res, 409, {
|
|
4614
|
+
error: `${path} already exists`,
|
|
4615
|
+
exists: true
|
|
4616
|
+
});
|
|
4565
4617
|
return;
|
|
4566
4618
|
}
|
|
4567
|
-
writeJson(res, 400, { error: `unknown view: ${view}` });
|
|
4568
|
-
return;
|
|
4569
|
-
}
|
|
4570
|
-
if (method !== "POST") {
|
|
4571
|
-
writeJson(res, 405, { error: `method not allowed: ${method}` });
|
|
4572
|
-
return;
|
|
4573
4619
|
}
|
|
4574
|
-
|
|
4575
|
-
|
|
4576
|
-
|
|
4577
|
-
|
|
4578
|
-
|
|
4620
|
+
writeJson(res, 200, {
|
|
4621
|
+
ok: true,
|
|
4622
|
+
size: (await files.upload(alias, path, req, void 0, overwrite)).size
|
|
4623
|
+
});
|
|
4624
|
+
} catch (error) {
|
|
4625
|
+
writeRouteError(res, error);
|
|
4626
|
+
}
|
|
4627
|
+
}
|
|
4628
|
+
},
|
|
4629
|
+
{
|
|
4630
|
+
kind: "exact",
|
|
4631
|
+
path: REMOTE_API.download,
|
|
4632
|
+
handler: async (req, res) => {
|
|
4633
|
+
if (!isLoopbackRequest(req)) {
|
|
4634
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4635
|
+
return;
|
|
4636
|
+
}
|
|
4637
|
+
if (req.method !== "GET") {
|
|
4638
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4639
|
+
return;
|
|
4640
|
+
}
|
|
4641
|
+
const url = new URL(req.url ?? "/", "http://localhost");
|
|
4642
|
+
const alias = url.searchParams.get("alias") ?? "";
|
|
4643
|
+
const path = url.searchParams.get("path") ?? "";
|
|
4644
|
+
if (alias === "" || path === "") {
|
|
4645
|
+
writeJson(res, 400, { error: "alias and path query parameters are required" });
|
|
4646
|
+
return;
|
|
4647
|
+
}
|
|
4648
|
+
if (!path.startsWith("/")) {
|
|
4649
|
+
writeJson(res, 400, { error: `path must be absolute: ${path}` });
|
|
4650
|
+
return;
|
|
4651
|
+
}
|
|
4652
|
+
try {
|
|
4653
|
+
await files.download(alias, path, res);
|
|
4654
|
+
} catch (error) {
|
|
4655
|
+
if (!res.headersSent && !res.destroyed) writeRouteError(res, error);
|
|
4656
|
+
else res.destroy(error instanceof Error ? error : void 0);
|
|
4657
|
+
}
|
|
4658
|
+
}
|
|
4659
|
+
},
|
|
4660
|
+
{
|
|
4661
|
+
kind: "exact",
|
|
4662
|
+
path: REMOTE_API.downloadDir,
|
|
4663
|
+
handler: async (req, res) => {
|
|
4664
|
+
if (!isLoopbackRequest(req)) {
|
|
4665
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4666
|
+
return;
|
|
4667
|
+
}
|
|
4668
|
+
if (req.method !== "GET") {
|
|
4669
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4670
|
+
return;
|
|
4671
|
+
}
|
|
4672
|
+
const url = new URL(req.url ?? "/", "http://localhost");
|
|
4673
|
+
const alias = url.searchParams.get("alias") ?? "";
|
|
4674
|
+
const path = url.searchParams.get("path") ?? "";
|
|
4675
|
+
if (alias === "" || path === "") {
|
|
4676
|
+
writeJson(res, 400, { error: "alias and path query parameters are required" });
|
|
4677
|
+
return;
|
|
4678
|
+
}
|
|
4679
|
+
if (!path.startsWith("/")) {
|
|
4680
|
+
writeJson(res, 400, { error: `path must be absolute: ${path}` });
|
|
4681
|
+
return;
|
|
4682
|
+
}
|
|
4683
|
+
let command;
|
|
4684
|
+
try {
|
|
4685
|
+
if (!(await files.stat(alias, path)).isDir) {
|
|
4686
|
+
writeJson(res, 400, { error: `not a directory: ${path}` });
|
|
4579
4687
|
return;
|
|
4580
4688
|
}
|
|
4689
|
+
command = directoryArchiveCommand(path);
|
|
4690
|
+
} catch (error) {
|
|
4691
|
+
writeRouteError(res, error);
|
|
4692
|
+
return;
|
|
4693
|
+
}
|
|
4694
|
+
await streamArchive(alias, command, res, directoryArchiveName(path));
|
|
4695
|
+
}
|
|
4696
|
+
},
|
|
4697
|
+
{
|
|
4698
|
+
kind: "exact",
|
|
4699
|
+
path: REMOTE_API.downloadMany,
|
|
4700
|
+
handler: async (req, res) => {
|
|
4701
|
+
if (!isLoopbackRequest(req)) {
|
|
4702
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4703
|
+
return;
|
|
4704
|
+
}
|
|
4705
|
+
if (req.method !== "GET") {
|
|
4706
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4707
|
+
return;
|
|
4708
|
+
}
|
|
4709
|
+
const url = new URL(req.url ?? "/", "http://localhost");
|
|
4710
|
+
const alias = url.searchParams.get("alias") ?? "";
|
|
4711
|
+
const paths = url.searchParams.getAll("path");
|
|
4712
|
+
if (alias === "" || paths.length === 0) {
|
|
4713
|
+
writeJson(res, 400, { error: "alias and at least one path query parameter are required" });
|
|
4714
|
+
return;
|
|
4715
|
+
}
|
|
4716
|
+
if (paths.length > 100) {
|
|
4717
|
+
writeJson(res, 400, { error: `too many entries: ${paths.length} (max 100)` });
|
|
4718
|
+
return;
|
|
4719
|
+
}
|
|
4720
|
+
for (const path of paths) if (!path.startsWith("/")) {
|
|
4721
|
+
writeJson(res, 400, { error: `path must be absolute: ${path}` });
|
|
4722
|
+
return;
|
|
4723
|
+
}
|
|
4724
|
+
let command;
|
|
4725
|
+
try {
|
|
4726
|
+
for (const path of paths) await files.stat(alias, path);
|
|
4727
|
+
command = selectionArchiveCommand(paths);
|
|
4728
|
+
} catch (error) {
|
|
4729
|
+
writeRouteError(res, error);
|
|
4730
|
+
return;
|
|
4731
|
+
}
|
|
4732
|
+
await streamArchive(alias, command, res, selectionDownloadName(paths.length));
|
|
4733
|
+
}
|
|
4734
|
+
},
|
|
4735
|
+
{
|
|
4736
|
+
kind: "exact",
|
|
4737
|
+
path: REMOTE_API.workspaces,
|
|
4738
|
+
handler: async (req, res) => {
|
|
4739
|
+
if (!isLoopbackRequest(req)) {
|
|
4740
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4741
|
+
return;
|
|
4742
|
+
}
|
|
4743
|
+
const method = req.method ?? "GET";
|
|
4744
|
+
const url = new URL(req.url ?? "/", "http://localhost");
|
|
4745
|
+
if (method === "GET") {
|
|
4581
4746
|
try {
|
|
4582
|
-
|
|
4747
|
+
const bindings = store.listWorkspaces();
|
|
4748
|
+
const aliasPaths = {};
|
|
4749
|
+
for (const b of bindings) aliasPaths[b.id] = worlds.aliasPathFor(b.id);
|
|
4750
|
+
worlds.syncWorkspaces().catch(() => void 0);
|
|
4751
|
+
writeJson(res, 200, {
|
|
4752
|
+
workspaces: bindings,
|
|
4753
|
+
mode: worlds.mode,
|
|
4754
|
+
aliasPaths,
|
|
4755
|
+
tombstones: store.listTombstones(),
|
|
4756
|
+
version: pluginVersion(),
|
|
4757
|
+
timeouts: timeouts()
|
|
4758
|
+
});
|
|
4583
4759
|
} catch (error) {
|
|
4584
|
-
|
|
4760
|
+
writeStoreError(res, error);
|
|
4585
4761
|
}
|
|
4762
|
+
return;
|
|
4586
4763
|
}
|
|
4587
|
-
|
|
4588
|
-
|
|
4589
|
-
kind: "exact",
|
|
4590
|
-
path: REMOTE_API.importSshConfig,
|
|
4591
|
-
handler: async (req, res) => {
|
|
4592
|
-
if (!isLoopbackRequest(req)) {
|
|
4593
|
-
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4594
|
-
return;
|
|
4595
|
-
}
|
|
4596
|
-
if (req.method !== "POST") {
|
|
4597
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4598
|
-
return;
|
|
4599
|
-
}
|
|
4600
|
-
if (bodyTooLarge(req, SSH_CONFIG_BODY_LIMIT)) {
|
|
4601
|
-
writePayloadTooLarge(req, res, SSH_CONFIG_BODY_LIMIT);
|
|
4602
|
-
return;
|
|
4603
|
-
}
|
|
4604
|
-
const read = await readBodyOrError(req, res, { maxBytes: SSH_CONFIG_BODY_LIMIT });
|
|
4764
|
+
if (method === "POST") {
|
|
4765
|
+
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4605
4766
|
if (!read.ok) return;
|
|
4606
4767
|
const body = read.body;
|
|
4607
|
-
|
|
4608
|
-
|
|
4609
|
-
writeJson(res, 200, applyImport(parseSshConfig(text)));
|
|
4610
|
-
} catch (error) {
|
|
4611
|
-
writeJson(res, 400, { error: error instanceof Error ? error.message : String(error) });
|
|
4612
|
-
}
|
|
4613
|
-
}
|
|
4614
|
-
},
|
|
4615
|
-
{
|
|
4616
|
-
kind: "exact",
|
|
4617
|
-
path: REMOTE_API.importDshSsh,
|
|
4618
|
-
handler: async (req, res) => {
|
|
4619
|
-
if (!isLoopbackRequest(req)) {
|
|
4620
|
-
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4621
|
-
return;
|
|
4622
|
-
}
|
|
4623
|
-
if (req.method !== "POST") {
|
|
4624
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4768
|
+
if (body === null) {
|
|
4769
|
+
writeJson(res, 400, { error: "invalid JSON body" });
|
|
4625
4770
|
return;
|
|
4626
4771
|
}
|
|
4627
4772
|
try {
|
|
4628
|
-
|
|
4773
|
+
const binding = store.createWorkspace(body);
|
|
4774
|
+
const aliasPath = worlds.aliasPathFor(binding.id);
|
|
4775
|
+
try {
|
|
4776
|
+
mkdirSync(aliasPath, { recursive: true });
|
|
4777
|
+
} catch (error) {
|
|
4778
|
+
throw new Error(`cannot materialize alias dir ${aliasPath}: ${error instanceof Error ? error.message : String(error)}`);
|
|
4779
|
+
}
|
|
4780
|
+
await worlds.syncWorkspaces().catch(() => void 0);
|
|
4781
|
+
writeJson(res, 201, {
|
|
4782
|
+
workspace: binding,
|
|
4783
|
+
aliasPath
|
|
4784
|
+
});
|
|
4629
4785
|
} catch (error) {
|
|
4630
4786
|
writeJson(res, 400, { error: error instanceof Error ? error.message : String(error) });
|
|
4631
4787
|
}
|
|
4788
|
+
return;
|
|
4632
4789
|
}
|
|
4633
|
-
|
|
4634
|
-
|
|
4635
|
-
|
|
4636
|
-
path: REMOTE_API.terminalClose,
|
|
4637
|
-
handler: async (req, res) => {
|
|
4638
|
-
if (!isLoopbackRequest(req)) {
|
|
4639
|
-
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4640
|
-
return;
|
|
4641
|
-
}
|
|
4642
|
-
if (req.method !== "POST") {
|
|
4643
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4644
|
-
return;
|
|
4645
|
-
}
|
|
4646
|
-
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4647
|
-
if (!read.ok) return;
|
|
4648
|
-
const body = read.body;
|
|
4649
|
-
if (body === null || typeof body.id !== "string" || body.id === "") {
|
|
4650
|
-
writeJson(res, 400, { error: "id is required" });
|
|
4651
|
-
return;
|
|
4652
|
-
}
|
|
4653
|
-
writeJson(res, 200, { ok: terminalSessions.close(body.id) });
|
|
4790
|
+
if (method !== "DELETE") {
|
|
4791
|
+
writeJson(res, 405, { error: `method not allowed: ${method}` });
|
|
4792
|
+
return;
|
|
4654
4793
|
}
|
|
4655
|
-
|
|
4656
|
-
|
|
4657
|
-
|
|
4658
|
-
|
|
4659
|
-
|
|
4660
|
-
|
|
4661
|
-
|
|
4662
|
-
|
|
4663
|
-
|
|
4664
|
-
|
|
4665
|
-
|
|
4666
|
-
|
|
4667
|
-
|
|
4668
|
-
|
|
4669
|
-
|
|
4670
|
-
|
|
4671
|
-
|
|
4672
|
-
|
|
4673
|
-
}
|
|
4674
|
-
|
|
4675
|
-
|
|
4676
|
-
|
|
4677
|
-
|
|
4678
|
-
|
|
4679
|
-
|
|
4680
|
-
|
|
4681
|
-
|
|
4682
|
-
|
|
4683
|
-
|
|
4684
|
-
|
|
4685
|
-
|
|
4794
|
+
const id = queryParam(url, "id");
|
|
4795
|
+
if (id === void 0 || id === "") {
|
|
4796
|
+
writeJson(res, 400, { error: "id query parameter is required" });
|
|
4797
|
+
return;
|
|
4798
|
+
}
|
|
4799
|
+
if (!store.removeWorkspace(id)) {
|
|
4800
|
+
writeJson(res, 404, { error: `unknown workspace id: ${id}` });
|
|
4801
|
+
return;
|
|
4802
|
+
}
|
|
4803
|
+
worlds.syncWorkspaces().catch(() => void 0);
|
|
4804
|
+
writeJson(res, 200, { ok: true });
|
|
4805
|
+
}
|
|
4806
|
+
},
|
|
4807
|
+
{
|
|
4808
|
+
kind: "exact",
|
|
4809
|
+
path: REMOTE_API.sessionCwd,
|
|
4810
|
+
handler: async (req, res) => {
|
|
4811
|
+
if (!isLoopbackRequest(req)) {
|
|
4812
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4813
|
+
return;
|
|
4814
|
+
}
|
|
4815
|
+
if ((req.method ?? "GET") !== "POST") {
|
|
4816
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method ?? "GET"}` });
|
|
4817
|
+
return;
|
|
4818
|
+
}
|
|
4819
|
+
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4820
|
+
if (!read.ok) return;
|
|
4821
|
+
const body = read.body;
|
|
4822
|
+
const sessionId = typeof body?.sessionId === "string" ? body.sessionId : "";
|
|
4823
|
+
if (sessionId === "") {
|
|
4824
|
+
writeJson(res, 400, { error: "sessionId must be a non-empty string" });
|
|
4825
|
+
return;
|
|
4826
|
+
}
|
|
4827
|
+
writeJson(res, 200, { cwd: liveSessionCwd(ctx, sessionId) });
|
|
4828
|
+
}
|
|
4829
|
+
},
|
|
4830
|
+
{
|
|
4831
|
+
kind: "exact",
|
|
4832
|
+
path: REMOTE_API.changes,
|
|
4833
|
+
handler: async (req, res) => {
|
|
4834
|
+
if (!isLoopbackRequest(req)) {
|
|
4835
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4836
|
+
return;
|
|
4837
|
+
}
|
|
4838
|
+
if ((req.method ?? "GET") !== "POST") {
|
|
4839
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method ?? "GET"}` });
|
|
4840
|
+
return;
|
|
4841
|
+
}
|
|
4842
|
+
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4843
|
+
if (!read.ok) return;
|
|
4844
|
+
const body = read.body;
|
|
4845
|
+
const sessionId = typeof body?.sessionId === "string" ? body.sessionId : "";
|
|
4846
|
+
const op = typeof body?.op === "string" ? body.op : "";
|
|
4847
|
+
if (sessionId === "" || op !== "list" && op !== "diff") {
|
|
4848
|
+
writeJson(res, 400, { error: "op (list|diff) and a non-empty sessionId are required" });
|
|
4849
|
+
return;
|
|
4850
|
+
}
|
|
4851
|
+
const ledger = deps.changes;
|
|
4852
|
+
if (ledger === void 0) {
|
|
4853
|
+
writeJson(res, 200, {
|
|
4854
|
+
summary: null,
|
|
4855
|
+
diff: null
|
|
4856
|
+
});
|
|
4857
|
+
return;
|
|
4858
|
+
}
|
|
4859
|
+
if (typeof body?.repoSubdir === "string" || body?.repoSubdir === null) ledger.useRepoSubdir(sessionId, typeof body.repoSubdir === "string" ? body.repoSubdir : void 0);
|
|
4860
|
+
if (op === "list") {
|
|
4861
|
+
if (ledger.summary(sessionId) === null) {
|
|
4862
|
+
const cwd = liveSessionCwd(ctx, sessionId);
|
|
4863
|
+
if (cwd !== void 0) ledger.beginTurn({
|
|
4864
|
+
sessionId,
|
|
4865
|
+
cwd
|
|
4866
|
+
});
|
|
4686
4867
|
}
|
|
4687
|
-
|
|
4688
|
-
|
|
4689
|
-
|
|
4690
|
-
|
|
4691
|
-
|
|
4868
|
+
if (body?.force === true) ledger.rescan(sessionId);
|
|
4869
|
+
else ledger.touch(sessionId);
|
|
4870
|
+
writeJson(res, 200, { summary: ledger.summary(sessionId) });
|
|
4871
|
+
return;
|
|
4872
|
+
}
|
|
4873
|
+
const path = typeof body?.path === "string" ? body.path : "";
|
|
4874
|
+
if (path === "") {
|
|
4875
|
+
writeJson(res, 400, { error: "path is required for op=diff" });
|
|
4876
|
+
return;
|
|
4877
|
+
}
|
|
4878
|
+
try {
|
|
4879
|
+
writeJson(res, 200, { diff: await ledger.diff(sessionId, path) });
|
|
4880
|
+
} catch (error) {
|
|
4881
|
+
writeRouteError(res, error);
|
|
4882
|
+
}
|
|
4883
|
+
}
|
|
4884
|
+
},
|
|
4885
|
+
{
|
|
4886
|
+
kind: "exact",
|
|
4887
|
+
path: REMOTE_API.routing,
|
|
4888
|
+
handler: async (req, res) => {
|
|
4889
|
+
if (!isLoopbackRequest(req)) {
|
|
4890
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4891
|
+
return;
|
|
4892
|
+
}
|
|
4893
|
+
if ((req.method ?? "GET") !== "POST") {
|
|
4894
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method ?? "GET"}` });
|
|
4895
|
+
return;
|
|
4896
|
+
}
|
|
4897
|
+
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4898
|
+
if (!read.ok) return;
|
|
4899
|
+
const mode = read.body?.mode;
|
|
4900
|
+
if (mode !== "off" && mode !== "shadow" && mode !== "enforce") {
|
|
4901
|
+
writeJson(res, 400, { error: `mode must be one of off/shadow/enforce (got ${JSON.stringify(mode) ?? "missing"})` });
|
|
4902
|
+
return;
|
|
4903
|
+
}
|
|
4904
|
+
try {
|
|
4905
|
+
const settings = ctx.get("settings");
|
|
4906
|
+
if (typeof settings?.update !== "function") {
|
|
4907
|
+
writeJson(res, 503, { error: "settings service unavailable (cannot persist routing mode)" });
|
|
4692
4908
|
return;
|
|
4693
4909
|
}
|
|
4910
|
+
const ns = ownSettingsNs(ctx) ?? "remote-vs";
|
|
4911
|
+
await settings.update(ns, { routingMode: mode });
|
|
4912
|
+
writeJson(res, 200, { mode: worlds.mode });
|
|
4913
|
+
} catch (error) {
|
|
4914
|
+
writeRouteError(res, error);
|
|
4915
|
+
}
|
|
4916
|
+
}
|
|
4917
|
+
},
|
|
4918
|
+
{
|
|
4919
|
+
kind: "exact",
|
|
4920
|
+
path: REMOTE_API.viewSettings,
|
|
4921
|
+
handler: async (req, res) => {
|
|
4922
|
+
if (!isLoopbackRequest(req)) {
|
|
4923
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4924
|
+
return;
|
|
4925
|
+
}
|
|
4926
|
+
const method = req.method ?? "GET";
|
|
4927
|
+
if (method === "GET") {
|
|
4694
4928
|
try {
|
|
4695
|
-
writeJson(res, 200, {
|
|
4929
|
+
writeJson(res, 200, { settings: viewSettings.get() });
|
|
4696
4930
|
} catch (error) {
|
|
4697
|
-
|
|
4698
|
-
writeJson(res, /EADDRINUSE|EACCES|already in use|already has \d+ local forwards|permission denied|not permitted/i.test(message) ? 409 : 502, { error: message });
|
|
4931
|
+
writeRouteError(res, error);
|
|
4699
4932
|
}
|
|
4933
|
+
return;
|
|
4700
4934
|
}
|
|
4701
|
-
|
|
4702
|
-
|
|
4703
|
-
|
|
4704
|
-
|
|
4705
|
-
|
|
4706
|
-
|
|
4707
|
-
|
|
4708
|
-
|
|
4709
|
-
|
|
4710
|
-
|
|
4711
|
-
|
|
4712
|
-
|
|
4713
|
-
|
|
4714
|
-
|
|
4935
|
+
if (method !== "POST") {
|
|
4936
|
+
writeJson(res, 405, { error: `method not allowed: ${method}` });
|
|
4937
|
+
return;
|
|
4938
|
+
}
|
|
4939
|
+
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4940
|
+
if (!read.ok) return;
|
|
4941
|
+
try {
|
|
4942
|
+
writeJson(res, 200, { settings: viewSettings.patch(read.body ?? {}) });
|
|
4943
|
+
} catch (error) {
|
|
4944
|
+
writeRouteError(res, error);
|
|
4945
|
+
}
|
|
4946
|
+
}
|
|
4947
|
+
},
|
|
4948
|
+
{
|
|
4949
|
+
kind: "exact",
|
|
4950
|
+
path: REMOTE_API.deploy,
|
|
4951
|
+
handler: async (req, res) => {
|
|
4952
|
+
if (!isLoopbackRequest(req)) {
|
|
4953
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4954
|
+
return;
|
|
4955
|
+
}
|
|
4956
|
+
const method = req.method ?? "GET";
|
|
4957
|
+
const url = new URL(req.url ?? "/", "http://localhost");
|
|
4958
|
+
if (method === "GET") {
|
|
4959
|
+
const alias = queryParam(url, "alias");
|
|
4960
|
+
const view = queryParam(url, "view") ?? "status";
|
|
4715
4961
|
if (alias === void 0 || alias === "") {
|
|
4716
4962
|
writeJson(res, 400, { error: "alias query parameter is required" });
|
|
4717
4963
|
return;
|
|
4718
4964
|
}
|
|
4719
|
-
|
|
4720
|
-
|
|
4721
|
-
|
|
4722
|
-
|
|
4723
|
-
|
|
4724
|
-
}
|
|
4725
|
-
if (result.timedOut) {
|
|
4726
|
-
writeJson(res, 200, {
|
|
4727
|
-
tool: "unknown",
|
|
4728
|
-
ports: [],
|
|
4729
|
-
error: `the remote probe did not finish within ${LISTEN_PORTS_TIMEOUT_MS / 1e3}s`
|
|
4730
|
-
});
|
|
4731
|
-
return;
|
|
4965
|
+
if (view === "probe") {
|
|
4966
|
+
try {
|
|
4967
|
+
writeJson(res, 200, await probe(pool, alias));
|
|
4968
|
+
} catch (error) {
|
|
4969
|
+
writeRouteError(res, error);
|
|
4732
4970
|
}
|
|
4733
|
-
|
|
4971
|
+
return;
|
|
4972
|
+
}
|
|
4973
|
+
if (view === "status") {
|
|
4974
|
+
try {
|
|
4734
4975
|
writeJson(res, 200, {
|
|
4735
|
-
|
|
4736
|
-
|
|
4737
|
-
error: "the remote probe ended without an exit status (the SSH channel closed)"
|
|
4976
|
+
deploy: store.require(alias).deploy ?? { status: "not-deployed" },
|
|
4977
|
+
helpers: helperAvailability()
|
|
4738
4978
|
});
|
|
4739
|
-
|
|
4979
|
+
} catch (error) {
|
|
4980
|
+
if (isStoreError(error)) writeStoreError(res, error);
|
|
4981
|
+
else writeJson(res, 404, { error: error instanceof Error ? error.message : String(error) });
|
|
4740
4982
|
}
|
|
4741
|
-
|
|
4742
|
-
} catch (error) {
|
|
4743
|
-
writeJson(res, 502, { error: error instanceof Error ? error.message : String(error) });
|
|
4983
|
+
return;
|
|
4744
4984
|
}
|
|
4985
|
+
writeJson(res, 400, { error: `unknown view: ${view}` });
|
|
4986
|
+
return;
|
|
4745
4987
|
}
|
|
4746
|
-
|
|
4747
|
-
|
|
4748
|
-
|
|
4749
|
-
|
|
4750
|
-
|
|
4751
|
-
|
|
4752
|
-
|
|
4988
|
+
if (method !== "POST") {
|
|
4989
|
+
writeJson(res, 405, { error: `method not allowed: ${method}` });
|
|
4990
|
+
return;
|
|
4991
|
+
}
|
|
4992
|
+
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4993
|
+
if (!read.ok) return;
|
|
4994
|
+
const body = read.body;
|
|
4995
|
+
if (body === null || typeof body.alias !== "string") {
|
|
4996
|
+
writeJson(res, 400, { error: "alias is required" });
|
|
4997
|
+
return;
|
|
4998
|
+
}
|
|
4999
|
+
try {
|
|
5000
|
+
writeJson(res, 200, await pushPipeline(pool, store, body.alias, typeof body.version === "string" ? { version: body.version } : {}));
|
|
5001
|
+
} catch (error) {
|
|
5002
|
+
writeRouteError(res, error);
|
|
5003
|
+
}
|
|
5004
|
+
}
|
|
5005
|
+
},
|
|
5006
|
+
{
|
|
5007
|
+
kind: "exact",
|
|
5008
|
+
path: REMOTE_API.importSshConfig,
|
|
5009
|
+
handler: async (req, res) => {
|
|
5010
|
+
if (!isLoopbackRequest(req)) {
|
|
5011
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
5012
|
+
return;
|
|
5013
|
+
}
|
|
5014
|
+
if (req.method !== "POST") {
|
|
5015
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
5016
|
+
return;
|
|
5017
|
+
}
|
|
5018
|
+
if (bodyTooLarge(req, SSH_CONFIG_BODY_LIMIT)) {
|
|
5019
|
+
writePayloadTooLarge(req, res, SSH_CONFIG_BODY_LIMIT);
|
|
5020
|
+
return;
|
|
5021
|
+
}
|
|
5022
|
+
const read = await readBodyOrError(req, res, { maxBytes: SSH_CONFIG_BODY_LIMIT });
|
|
5023
|
+
if (!read.ok) return;
|
|
5024
|
+
const body = read.body;
|
|
5025
|
+
try {
|
|
5026
|
+
const text = body !== null && typeof body.text === "string" ? body.text : readDefaultSshConfig();
|
|
5027
|
+
writeJson(res, 200, applyImport(parseSshConfig(text)));
|
|
5028
|
+
} catch (error) {
|
|
5029
|
+
writeJson(res, 400, { error: error instanceof Error ? error.message : String(error) });
|
|
5030
|
+
}
|
|
5031
|
+
}
|
|
5032
|
+
},
|
|
5033
|
+
{
|
|
5034
|
+
kind: "exact",
|
|
5035
|
+
path: REMOTE_API.importDshSsh,
|
|
5036
|
+
handler: async (req, res) => {
|
|
5037
|
+
if (!isLoopbackRequest(req)) {
|
|
5038
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
5039
|
+
return;
|
|
5040
|
+
}
|
|
5041
|
+
if (req.method !== "POST") {
|
|
5042
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
5043
|
+
return;
|
|
5044
|
+
}
|
|
5045
|
+
try {
|
|
5046
|
+
writeJson(res, 200, applyImport(migrateDshSsh()));
|
|
5047
|
+
} catch (error) {
|
|
5048
|
+
writeJson(res, 400, { error: error instanceof Error ? error.message : String(error) });
|
|
5049
|
+
}
|
|
5050
|
+
}
|
|
5051
|
+
},
|
|
5052
|
+
{
|
|
5053
|
+
kind: "exact",
|
|
5054
|
+
path: REMOTE_API.terminalClose,
|
|
5055
|
+
handler: async (req, res) => {
|
|
5056
|
+
if (!isLoopbackRequest(req)) {
|
|
5057
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
5058
|
+
return;
|
|
5059
|
+
}
|
|
5060
|
+
if (req.method !== "POST") {
|
|
5061
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
5062
|
+
return;
|
|
5063
|
+
}
|
|
5064
|
+
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
5065
|
+
if (!read.ok) return;
|
|
5066
|
+
const body = read.body;
|
|
5067
|
+
if (body === null || typeof body.id !== "string" || body.id === "") {
|
|
5068
|
+
writeJson(res, 400, { error: "id is required" });
|
|
5069
|
+
return;
|
|
5070
|
+
}
|
|
5071
|
+
writeJson(res, 200, { ok: terminalSessions.close(body.id) });
|
|
5072
|
+
}
|
|
5073
|
+
},
|
|
5074
|
+
{
|
|
5075
|
+
kind: "exact",
|
|
5076
|
+
path: REMOTE_API.forwards,
|
|
5077
|
+
handler: async (req, res) => {
|
|
5078
|
+
if (!isLoopbackRequest(req)) {
|
|
5079
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
5080
|
+
return;
|
|
5081
|
+
}
|
|
5082
|
+
if (forwards === void 0) {
|
|
5083
|
+
writeJson(res, 503, { error: "port forwarding is not available in this composition" });
|
|
5084
|
+
return;
|
|
5085
|
+
}
|
|
5086
|
+
const url = new URL(req.url ?? "/", "http://localhost");
|
|
5087
|
+
if (req.method === "GET") {
|
|
5088
|
+
const alias = queryParam(url, "alias");
|
|
5089
|
+
writeJson(res, 200, { forwards: forwards.list(alias === void 0 || alias === "" ? void 0 : alias) });
|
|
5090
|
+
return;
|
|
5091
|
+
}
|
|
5092
|
+
if (req.method === "DELETE") {
|
|
5093
|
+
const id = queryParam(url, "id");
|
|
5094
|
+
if (id === void 0 || id === "") {
|
|
5095
|
+
writeJson(res, 400, { error: "id query parameter is required" });
|
|
4753
5096
|
return;
|
|
4754
5097
|
}
|
|
4755
|
-
|
|
4756
|
-
|
|
5098
|
+
writeJson(res, 200, { ok: forwards.stop(id) });
|
|
5099
|
+
return;
|
|
5100
|
+
}
|
|
5101
|
+
if (req.method !== "POST") {
|
|
5102
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
5103
|
+
return;
|
|
5104
|
+
}
|
|
5105
|
+
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
5106
|
+
if (!read.ok) return;
|
|
5107
|
+
const validated = validateForwardRequest(read.body);
|
|
5108
|
+
if (!validated.ok) {
|
|
5109
|
+
writeJson(res, 400, { error: validated.error });
|
|
5110
|
+
return;
|
|
5111
|
+
}
|
|
5112
|
+
try {
|
|
5113
|
+
writeJson(res, 200, { forward: await forwards.start(validated.value) });
|
|
5114
|
+
} catch (error) {
|
|
5115
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
5116
|
+
writeJson(res, /EADDRINUSE|EACCES|already in use|already has \d+ local forwards|permission denied|not permitted/i.test(message) ? 409 : 502, { error: message });
|
|
5117
|
+
}
|
|
5118
|
+
}
|
|
5119
|
+
},
|
|
5120
|
+
{
|
|
5121
|
+
kind: "exact",
|
|
5122
|
+
path: REMOTE_API.listenPorts,
|
|
5123
|
+
handler: async (req, res) => {
|
|
5124
|
+
if (!isLoopbackRequest(req)) {
|
|
5125
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
5126
|
+
return;
|
|
5127
|
+
}
|
|
5128
|
+
if (req.method !== "GET") {
|
|
5129
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
5130
|
+
return;
|
|
5131
|
+
}
|
|
5132
|
+
const alias = queryParam(new URL(req.url ?? "/", "http://localhost"), "alias");
|
|
5133
|
+
if (alias === void 0 || alias === "") {
|
|
5134
|
+
writeJson(res, 400, { error: "alias query parameter is required" });
|
|
5135
|
+
return;
|
|
5136
|
+
}
|
|
5137
|
+
try {
|
|
5138
|
+
const result = await pool.exec(alias, listenPortsCommand(), { timeoutMs: LISTEN_PORTS_TIMEOUT_MS });
|
|
5139
|
+
if (result.error !== void 0) {
|
|
5140
|
+
writeJson(res, 502, { error: result.error });
|
|
4757
5141
|
return;
|
|
4758
5142
|
}
|
|
4759
|
-
|
|
4760
|
-
|
|
4761
|
-
|
|
4762
|
-
|
|
4763
|
-
|
|
5143
|
+
if (result.timedOut) {
|
|
5144
|
+
writeJson(res, 200, {
|
|
5145
|
+
tool: "unknown",
|
|
5146
|
+
ports: [],
|
|
5147
|
+
error: `the remote probe did not finish within ${LISTEN_PORTS_TIMEOUT_MS / 1e3}s`
|
|
5148
|
+
});
|
|
4764
5149
|
return;
|
|
4765
5150
|
}
|
|
4766
|
-
|
|
4767
|
-
|
|
4768
|
-
|
|
4769
|
-
|
|
4770
|
-
|
|
5151
|
+
if (result.exitCode === null) {
|
|
5152
|
+
writeJson(res, 200, {
|
|
5153
|
+
tool: "unknown",
|
|
5154
|
+
ports: [],
|
|
5155
|
+
error: "the remote probe ended without an exit status (the SSH channel closed)"
|
|
5156
|
+
});
|
|
5157
|
+
return;
|
|
4771
5158
|
}
|
|
5159
|
+
writeJson(res, 200, parseListenPorts(result.stdout, result.exitCode));
|
|
5160
|
+
} catch (error) {
|
|
5161
|
+
if (isStoreError(error)) writeStoreError(res, error);
|
|
5162
|
+
else writeJson(res, 502, { error: error instanceof Error ? error.message : String(error) });
|
|
4772
5163
|
}
|
|
4773
5164
|
}
|
|
4774
|
-
|
|
4775
|
-
|
|
4776
|
-
|
|
4777
|
-
|
|
5165
|
+
},
|
|
5166
|
+
{
|
|
5167
|
+
kind: "exact",
|
|
5168
|
+
path: REMOTE_API.metrics,
|
|
5169
|
+
handler: async (req, res) => {
|
|
4778
5170
|
if (!isLoopbackRequest(req)) {
|
|
4779
|
-
|
|
4780
|
-
|
|
5171
|
+
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
5172
|
+
return;
|
|
5173
|
+
}
|
|
5174
|
+
if (req.method !== "GET") {
|
|
5175
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4781
5176
|
return;
|
|
4782
5177
|
}
|
|
4783
5178
|
const url = new URL(req.url ?? "/", "http://localhost");
|
|
4784
5179
|
const alias = queryParam(url, "alias");
|
|
4785
|
-
const
|
|
4786
|
-
|
|
4787
|
-
|
|
4788
|
-
|
|
5180
|
+
const refresh = queryParam(url, "refresh") === "1";
|
|
5181
|
+
/** Explicit opt-in to sampling every configured host (tooling/debug). */
|
|
5182
|
+
const all = queryParam(url, "all") === "1";
|
|
5183
|
+
try {
|
|
5184
|
+
let hosts;
|
|
5185
|
+
if (alias !== void 0 && alias !== "") {
|
|
5186
|
+
if (store.get(alias) === void 0) {
|
|
5187
|
+
writeJson(res, 404, { error: `unknown host alias: ${alias}` });
|
|
5188
|
+
return;
|
|
5189
|
+
}
|
|
5190
|
+
hosts = [await monitor.sample(alias, refresh)];
|
|
5191
|
+
} else if (all) {
|
|
5192
|
+
const aliases = store.list().map((host) => host.alias);
|
|
5193
|
+
hosts = await Promise.all(aliases.map((host) => monitor.sample(host, refresh)));
|
|
5194
|
+
} else hosts = monitor.cached();
|
|
5195
|
+
writeJson(res, 200, { hosts });
|
|
5196
|
+
} catch (error) {
|
|
5197
|
+
writeJson(res, 502, { error: error instanceof Error ? error.message : String(error) });
|
|
5198
|
+
}
|
|
5199
|
+
}
|
|
5200
|
+
}
|
|
5201
|
+
];
|
|
5202
|
+
const upgrade = {
|
|
5203
|
+
path: REMOTE_API.terminal,
|
|
5204
|
+
handler: (req, socket, head) => {
|
|
5205
|
+
if (!isLoopbackRequest(req)) {
|
|
5206
|
+
socket.write("HTTP/1.1 403 Forbidden\r\nConnection: close\r\n\r\n");
|
|
5207
|
+
socket.destroy();
|
|
5208
|
+
return;
|
|
5209
|
+
}
|
|
5210
|
+
const url = new URL(req.url ?? "/", "http://localhost");
|
|
5211
|
+
const alias = queryParam(url, "alias");
|
|
5212
|
+
const sessionId = queryParam(url, "session");
|
|
5213
|
+
if (alias === void 0 && sessionId === void 0) {
|
|
5214
|
+
socket.write("HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n");
|
|
5215
|
+
socket.destroy();
|
|
5216
|
+
return;
|
|
5217
|
+
}
|
|
5218
|
+
const cols = Number.parseInt(queryParam(url, "cols") ?? "80", 10);
|
|
5219
|
+
const rows = Number.parseInt(queryParam(url, "rows") ?? "24", 10);
|
|
5220
|
+
const size = {
|
|
5221
|
+
cols: Number.isFinite(cols) && cols > 0 ? Math.min(cols, 500) : 80,
|
|
5222
|
+
rows: Number.isFinite(rows) && rows > 0 ? Math.min(rows, 200) : 24
|
|
5223
|
+
};
|
|
5224
|
+
terminalWss.handleUpgrade(req, socket, head, (ws) => {
|
|
5225
|
+
if (sessionId !== void 0) {
|
|
5226
|
+
if (terminalSessions.attach(ws, sessionId, size.cols, size.rows)) return;
|
|
5227
|
+
try {
|
|
5228
|
+
ws.send(JSON.stringify({
|
|
5229
|
+
t: "error",
|
|
5230
|
+
message: "terminal session expired"
|
|
5231
|
+
}));
|
|
5232
|
+
} catch {}
|
|
5233
|
+
try {
|
|
5234
|
+
ws.close(1e3);
|
|
5235
|
+
} catch {}
|
|
4789
5236
|
return;
|
|
4790
5237
|
}
|
|
4791
|
-
|
|
4792
|
-
|
|
4793
|
-
|
|
4794
|
-
|
|
4795
|
-
|
|
4796
|
-
|
|
4797
|
-
|
|
4798
|
-
|
|
4799
|
-
|
|
4800
|
-
|
|
4801
|
-
ws.send(JSON.stringify({
|
|
4802
|
-
t: "error",
|
|
4803
|
-
message: "terminal session expired"
|
|
4804
|
-
}));
|
|
4805
|
-
} catch {}
|
|
4806
|
-
try {
|
|
4807
|
-
ws.close(1e3);
|
|
4808
|
-
} catch {}
|
|
4809
|
-
return;
|
|
4810
|
-
}
|
|
4811
|
-
terminalSessions.open(ws, alias, size.cols, size.rows).then(() => void 0, () => void 0);
|
|
4812
|
-
});
|
|
5238
|
+
terminalSessions.open(ws, alias, size.cols, size.rows).then(() => void 0, () => void 0);
|
|
5239
|
+
});
|
|
5240
|
+
}
|
|
5241
|
+
};
|
|
5242
|
+
return {
|
|
5243
|
+
routes: routes.map((route) => ({
|
|
5244
|
+
...route,
|
|
5245
|
+
handler: (req, res) => {
|
|
5246
|
+
scopeHostNotices(res, urlAliasOf(req));
|
|
5247
|
+
return route.handler(req, res);
|
|
4813
5248
|
}
|
|
4814
|
-
},
|
|
4815
|
-
|
|
5249
|
+
})),
|
|
5250
|
+
upgrade,
|
|
5251
|
+
terminalSessions,
|
|
5252
|
+
close
|
|
4816
5253
|
};
|
|
4817
5254
|
}
|
|
4818
5255
|
//#endregion
|