dsh-remote-vs 0.25.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 +11 -7
- package/README.md +10 -7
- package/THIRD-PARTY-NOTICES.md +188 -0
- package/lib/client.js +6512 -79961
- package/lib/index.js +1283 -992
- package/lib/p2-router-fs.js +1 -1
- package/lib/types/client/CodeEditor.d.ts +8 -0
- package/lib/types/client/RemoteExplorer.d.ts +53 -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/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 +53 -0
- package/lib/types/client/vsc/FindWidget.d.ts +6 -0
- package/lib/types/client/vsc/change-marks.d.ts +16 -6
- package/lib/types/client/vsc/editor-keymap.d.ts +35 -1
- package/lib/types/client/vsc/kit.d.ts +11 -0
- package/lib/types/client/vsc/save-shortcut.d.ts +21 -0
- package/lib/types/client/vsc/terminal-theme.d.ts +28 -2
- 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/routes.d.ts +2 -0
- package/lib/types/store.d.ts +7 -0
- package/lib/types/terminal.d.ts +8 -0
- 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 +151 -8
- package/src/client/DirectoryFlow.tsx +16 -0
- package/src/client/DoctorPanel.tsx +70 -7
- package/src/client/EditorPane.tsx +63 -14
- package/src/client/HostsView.tsx +12 -3
- package/src/client/RemoteDirectoryPicker.tsx +12 -0
- package/src/client/RemoteExplorer.tsx +929 -40
- package/src/client/RemoteRoot.tsx +147 -22
- package/src/client/SearchView.tsx +8 -0
- package/src/client/TerminalSnippets.tsx +83 -8
- package/src/client/api.ts +106 -13
- 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/i18n.ts +82 -8
- 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 +372 -9
- package/src/client/ui.tsx +4 -0
- package/src/client/vsc/FindWidget.tsx +24 -9
- package/src/client/vsc/breadcrumb.tsx +91 -3
- package/src/client/vsc/change-marks.ts +93 -44
- package/src/client/vsc/editor-keymap.ts +59 -1
- package/src/client/vsc/file-icons.tsx +17 -3
- package/src/client/vsc/kit.tsx +26 -9
- package/src/client/vsc/save-shortcut.ts +47 -1
- package/src/client/vsc/terminal-theme.ts +44 -18
- package/src/client/vsc/vsc.css +411 -153
- 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/routes.ts +212 -43
- package/src/sftp.ts +51 -14
- package/src/store.ts +10 -0
- package/src/terminal.ts +73 -26
- 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) {
|
|
@@ -2007,6 +2051,18 @@ var ForwardManager = class {
|
|
|
2007
2051
|
* first listener orphaned — unreachable from the panel and never closed.
|
|
2008
2052
|
*/
|
|
2009
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();
|
|
2010
2066
|
/** Set on unload so a bind that settles afterwards closes itself. */
|
|
2011
2067
|
disposed = false;
|
|
2012
2068
|
constructor(deps) {
|
|
@@ -2032,15 +2088,16 @@ var ForwardManager = class {
|
|
|
2032
2088
|
const maxPerAlias = this.deps.maxPerAlias ?? 16;
|
|
2033
2089
|
if (perAlias >= maxPerAlias) throw new Error(`host ${request.alias} already has ${maxPerAlias} local forwards; stop one first`);
|
|
2034
2090
|
const pending = this.bindForward(id, request, requestedLocalPort).then((forward) => {
|
|
2035
|
-
if (this.disposed) {
|
|
2091
|
+
if (this.disposed || this.stoppedWhileBinding.has(id)) {
|
|
2036
2092
|
this.closeForward(forward);
|
|
2037
|
-
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");
|
|
2038
2094
|
}
|
|
2039
2095
|
this.forwards.set(id, forward);
|
|
2040
2096
|
this.log("info", `forward ${id} listening on 127.0.0.1:${forward.localPort}`);
|
|
2041
2097
|
return this.stateOf(forward);
|
|
2042
2098
|
}).finally(() => {
|
|
2043
2099
|
this.starting.delete(id);
|
|
2100
|
+
this.stoppedWhileBinding.delete(id);
|
|
2044
2101
|
});
|
|
2045
2102
|
this.starting.set(id, pending);
|
|
2046
2103
|
return pending;
|
|
@@ -2048,10 +2105,15 @@ var ForwardManager = class {
|
|
|
2048
2105
|
/** Stop one rule. Returns false when the id is unknown (already stopped). */
|
|
2049
2106
|
stop(id) {
|
|
2050
2107
|
const forward = this.forwards.get(id);
|
|
2051
|
-
if (forward
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
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`);
|
|
2055
2117
|
return true;
|
|
2056
2118
|
}
|
|
2057
2119
|
/**
|
|
@@ -2069,6 +2131,11 @@ var ForwardManager = class {
|
|
|
2069
2131
|
this.closeForward(forward);
|
|
2070
2132
|
stopped += 1;
|
|
2071
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
|
+
}
|
|
2072
2139
|
if (stopped > 0) this.log("info", `stopped ${stopped} forward(s) for deleted host ${alias}`);
|
|
2073
2140
|
return stopped;
|
|
2074
2141
|
}
|
|
@@ -2082,6 +2149,11 @@ var ForwardManager = class {
|
|
|
2082
2149
|
stopAll() {
|
|
2083
2150
|
let stopped = 0;
|
|
2084
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
|
+
}
|
|
2085
2157
|
return stopped;
|
|
2086
2158
|
}
|
|
2087
2159
|
/** Every rule, optionally narrowed to one host alias. */
|
|
@@ -2091,7 +2163,7 @@ var ForwardManager = class {
|
|
|
2091
2163
|
/** Close every listener and every proxied connection (plugin unload). */
|
|
2092
2164
|
dispose() {
|
|
2093
2165
|
this.disposed = true;
|
|
2094
|
-
|
|
2166
|
+
this.stopAll();
|
|
2095
2167
|
}
|
|
2096
2168
|
/** Bind the loopback listener. Split out so the error path is one place. */
|
|
2097
2169
|
async bindForward(id, request, requestedLocalPort) {
|
|
@@ -2314,9 +2386,31 @@ async function readJsonBodyResult(req, opts = {}) {
|
|
|
2314
2386
|
function isJsonObject(value) {
|
|
2315
2387
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
2316
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
|
+
}
|
|
2317
2411
|
/** Write one JSON response. */
|
|
2318
2412
|
function writeJson(res, status, body, headers = {}) {
|
|
2319
|
-
const notices = typeof body === "object" && body !== null && !Array.isArray(body)
|
|
2413
|
+
const notices = noticesFor(res, typeof body === "object" && body !== null && !Array.isArray(body), status);
|
|
2320
2414
|
const payload = notices.length > 0 ? {
|
|
2321
2415
|
...body,
|
|
2322
2416
|
notices
|
|
@@ -2477,6 +2571,40 @@ function selectionArchiveCommand(paths) {
|
|
|
2477
2571
|
* store entry flips to key auth with the managed keyPath.
|
|
2478
2572
|
*/
|
|
2479
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
|
+
}
|
|
2480
2608
|
/** Ensure the managed keypair exists; return the private key path. */
|
|
2481
2609
|
async function ensureKeypair(alias, dir = keysDir()) {
|
|
2482
2610
|
ensurePrivateDir(dir);
|
|
@@ -2501,8 +2629,12 @@ async function ensureKeypair(alias, dir = keysDir()) {
|
|
|
2501
2629
|
chmodSync(privatePath, 384);
|
|
2502
2630
|
} catch {}
|
|
2503
2631
|
}
|
|
2504
|
-
|
|
2505
|
-
|
|
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
|
+
}
|
|
2506
2638
|
return {
|
|
2507
2639
|
privatePath,
|
|
2508
2640
|
publicKey
|
|
@@ -2524,7 +2656,7 @@ async function bootstrap(pool, store, alias) {
|
|
|
2524
2656
|
const script = [
|
|
2525
2657
|
"mkdir -p ~/.ssh && chmod 700 ~/.ssh",
|
|
2526
2658
|
`touch ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys`,
|
|
2527
|
-
`grep -
|
|
2659
|
+
`grep -qF '${publicKey.split(" ").slice(0, 2).join(" ")}' ~/.ssh/authorized_keys 2>/dev/null || echo '${publicKey}' >> ~/.ssh/authorized_keys`
|
|
2528
2660
|
].join(" && ");
|
|
2529
2661
|
const result = await pool.exec(alias, script, { timeoutMs: 3e4 });
|
|
2530
2662
|
if (!result.success) throw new Error(`key install failed [exit code: ${result.exitCode}]: ${result.stderr || result.stdout || result.error || "unknown"}`);
|
|
@@ -2829,6 +2961,14 @@ const OUT_COALESCE_CHARS = 64 * 1024;
|
|
|
2829
2961
|
*/
|
|
2830
2962
|
const OUT_ECHO_IDLE_MS = 24;
|
|
2831
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;
|
|
2971
|
+
/**
|
|
2832
2972
|
* The largest client frame accepted, in bytes.
|
|
2833
2973
|
*
|
|
2834
2974
|
* The only frame with a payload is `in` — keystrokes, and pastes, which the
|
|
@@ -2888,6 +3028,7 @@ var TerminalSessionRegistry = class {
|
|
|
2888
3028
|
paused: false,
|
|
2889
3029
|
outBuffer: "",
|
|
2890
3030
|
outTimer: void 0,
|
|
3031
|
+
replay: "",
|
|
2891
3032
|
lastFlushAt: 0,
|
|
2892
3033
|
outstanding: /* @__PURE__ */ new Map(),
|
|
2893
3034
|
reap: void 0,
|
|
@@ -2941,6 +3082,10 @@ var TerminalSessionRegistry = class {
|
|
|
2941
3082
|
const session = this.sessions.get(id);
|
|
2942
3083
|
if (session === void 0 || !session.alive) return false;
|
|
2943
3084
|
this.hookSocket(session, ws);
|
|
3085
|
+
if (session.replay !== "") this.sendOutTo(session, ws, JSON.stringify({
|
|
3086
|
+
t: "out",
|
|
3087
|
+
d: session.replay
|
|
3088
|
+
}));
|
|
2944
3089
|
this.flushOut(session);
|
|
2945
3090
|
this.send(ws, {
|
|
2946
3091
|
t: "ready",
|
|
@@ -3078,6 +3223,8 @@ var TerminalSessionRegistry = class {
|
|
|
3078
3223
|
if (session.outBuffer === "") return;
|
|
3079
3224
|
const text = session.outBuffer;
|
|
3080
3225
|
session.outBuffer = "";
|
|
3226
|
+
session.replay += text;
|
|
3227
|
+
if (session.replay.length > REPLAY_MAX_CHARS * 2) session.replay = session.replay.slice(-131072);
|
|
3081
3228
|
session.lastFlushAt = Date.now();
|
|
3082
3229
|
this.broadcast(session, {
|
|
3083
3230
|
t: "out",
|
|
@@ -3099,29 +3246,37 @@ var TerminalSessionRegistry = class {
|
|
|
3099
3246
|
}
|
|
3100
3247
|
broadcast(session, frame) {
|
|
3101
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) {
|
|
3102
3260
|
const size = Buffer.byteLength(text, "utf8");
|
|
3103
|
-
|
|
3104
|
-
|
|
3105
|
-
|
|
3106
|
-
|
|
3107
|
-
|
|
3108
|
-
|
|
3109
|
-
|
|
3110
|
-
|
|
3111
|
-
|
|
3112
|
-
|
|
3113
|
-
|
|
3114
|
-
|
|
3115
|
-
|
|
3116
|
-
|
|
3117
|
-
|
|
3118
|
-
|
|
3119
|
-
|
|
3120
|
-
|
|
3121
|
-
done();
|
|
3122
|
-
}
|
|
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();
|
|
3123
3279
|
}
|
|
3124
|
-
if (session.pending > OUT_HIGH_WATER) this.setPaused(session, true);
|
|
3125
3280
|
}
|
|
3126
3281
|
};
|
|
3127
3282
|
//#endregion
|
|
@@ -3587,6 +3742,16 @@ var HostMonitor = class {
|
|
|
3587
3742
|
this.inflight.delete(alias);
|
|
3588
3743
|
this.generations.set(alias, (this.generations.get(alias) ?? 0) + 1);
|
|
3589
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
|
+
}
|
|
3590
3755
|
async probe(alias) {
|
|
3591
3756
|
const generation = this.generations.get(alias) ?? 0;
|
|
3592
3757
|
const startedAt = this.now();
|
|
@@ -3645,16 +3810,27 @@ var HostMonitor = class {
|
|
|
3645
3810
|
};
|
|
3646
3811
|
//#endregion
|
|
3647
3812
|
//#region src/routes.ts
|
|
3648
|
-
/** One noServer WebSocket server for terminal upgrades (RFC 6455). */
|
|
3649
|
-
const terminalWss = new WebSocketServer({
|
|
3650
|
-
noServer: true,
|
|
3651
|
-
maxPayload: TERMINAL_MAX_FRAME_BYTES
|
|
3652
|
-
});
|
|
3653
3813
|
function queryParam(url, name) {
|
|
3654
3814
|
const value = url.searchParams.get(name);
|
|
3655
3815
|
return value === null ? void 0 : value;
|
|
3656
3816
|
}
|
|
3657
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
|
+
/**
|
|
3658
3834
|
* A caller mistake the route can name: well-formed HTTP, unusable payload.
|
|
3659
3835
|
*
|
|
3660
3836
|
* `/files` used to throw plain Errors for missing fields and map EVERY failure
|
|
@@ -3704,6 +3880,10 @@ async function readBodyOrError(req, res, opts = {}) {
|
|
|
3704
3880
|
writeJson(res, 400, { error: "invalid JSON body" });
|
|
3705
3881
|
return { ok: false };
|
|
3706
3882
|
}
|
|
3883
|
+
if (result.kind === "ok") {
|
|
3884
|
+
const alias = bodyAliasOf(result.value);
|
|
3885
|
+
if (alias !== null) scopeHostNotices(res, alias);
|
|
3886
|
+
}
|
|
3707
3887
|
return {
|
|
3708
3888
|
ok: true,
|
|
3709
3889
|
body: result.kind === "ok" ? result.value : null
|
|
@@ -3824,6 +4004,49 @@ function makeRoutes(deps) {
|
|
|
3824
4004
|
const forwards = deps.forwards;
|
|
3825
4005
|
const viewSettings = deps.viewSettings ?? new RemoteViewSettingsStore();
|
|
3826
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
|
+
/**
|
|
3827
4050
|
* Stream one remote `tar -czf -` command into a `.tar.gz` download response.
|
|
3828
4051
|
*
|
|
3829
4052
|
* Shared by the directory and multi-selection routes (they differ only in the
|
|
@@ -3922,60 +4145,26 @@ function makeRoutes(deps) {
|
|
|
3922
4145
|
skipped
|
|
3923
4146
|
};
|
|
3924
4147
|
};
|
|
3925
|
-
|
|
3926
|
-
|
|
3927
|
-
|
|
3928
|
-
|
|
3929
|
-
|
|
3930
|
-
|
|
3931
|
-
|
|
3932
|
-
|
|
3933
|
-
|
|
3934
|
-
|
|
3935
|
-
|
|
3936
|
-
|
|
3937
|
-
|
|
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 {
|
|
3938
4161
|
writeJson(res, 200, { hosts: store.list().map((h) => store.summarize(h)) });
|
|
3939
|
-
|
|
3940
|
-
|
|
3941
|
-
if (method === "POST") {
|
|
3942
|
-
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
3943
|
-
if (!read.ok) return;
|
|
3944
|
-
const body = read.body;
|
|
3945
|
-
if (body === null) {
|
|
3946
|
-
writeJson(res, 400, { error: "invalid JSON body" });
|
|
3947
|
-
return;
|
|
3948
|
-
}
|
|
3949
|
-
try {
|
|
3950
|
-
const entry = store.create(body);
|
|
3951
|
-
writeJson(res, 201, { host: store.summarize(entry) });
|
|
3952
|
-
} catch (error) {
|
|
3953
|
-
writeJson(res, 400, { error: error instanceof Error ? error.message : String(error) });
|
|
3954
|
-
}
|
|
3955
|
-
return;
|
|
3956
|
-
}
|
|
3957
|
-
if (method !== "PATCH" && method !== "DELETE") {
|
|
3958
|
-
writeJson(res, 405, { error: `method not allowed: ${method}` });
|
|
3959
|
-
return;
|
|
3960
|
-
}
|
|
3961
|
-
const alias = queryParam(url, "alias");
|
|
3962
|
-
if (alias === void 0 || alias === "") {
|
|
3963
|
-
writeJson(res, 400, { error: "alias query parameter is required" });
|
|
3964
|
-
return;
|
|
3965
|
-
}
|
|
3966
|
-
if (method === "DELETE") {
|
|
3967
|
-
forwards?.stopAlias(alias);
|
|
3968
|
-
pool.forget(alias);
|
|
3969
|
-
monitor.forget(alias);
|
|
3970
|
-
worlds.dropHost(alias).catch(() => void 0);
|
|
3971
|
-
if (!store.remove(alias)) {
|
|
3972
|
-
writeJson(res, 404, { error: `unknown host alias: ${alias}` });
|
|
3973
|
-
return;
|
|
3974
|
-
}
|
|
3975
|
-
worlds.syncWorkspaces().catch(() => void 0);
|
|
3976
|
-
writeJson(res, 200, { ok: true });
|
|
3977
|
-
return;
|
|
4162
|
+
} catch (error) {
|
|
4163
|
+
writeStoreError(res, error);
|
|
3978
4164
|
}
|
|
4165
|
+
return;
|
|
4166
|
+
}
|
|
4167
|
+
if (method === "POST") {
|
|
3979
4168
|
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
3980
4169
|
if (!read.ok) return;
|
|
3981
4170
|
const body = read.body;
|
|
@@ -3984,981 +4173,1083 @@ function makeRoutes(deps) {
|
|
|
3984
4173
|
return;
|
|
3985
4174
|
}
|
|
3986
4175
|
try {
|
|
3987
|
-
|
|
3988
|
-
|
|
3989
|
-
worlds.dropHost(alias).catch(() => void 0);
|
|
3990
|
-
const entry = store.update(alias, body);
|
|
3991
|
-
writeJson(res, 200, { host: store.summarize(entry) });
|
|
4176
|
+
const entry = store.create(body);
|
|
4177
|
+
writeJson(res, 201, { host: store.summarize(entry) });
|
|
3992
4178
|
} catch (error) {
|
|
3993
|
-
writeJson(res,
|
|
4179
|
+
writeJson(res, 400, { error: error instanceof Error ? error.message : String(error) });
|
|
3994
4180
|
}
|
|
4181
|
+
return;
|
|
3995
4182
|
}
|
|
3996
|
-
|
|
3997
|
-
|
|
3998
|
-
|
|
3999
|
-
path: REMOTE_API.status,
|
|
4000
|
-
handler: (req, res) => {
|
|
4001
|
-
if (!isLoopbackRequest(req)) {
|
|
4002
|
-
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4003
|
-
return;
|
|
4004
|
-
}
|
|
4005
|
-
if (req.method !== "GET") {
|
|
4006
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4007
|
-
return;
|
|
4008
|
-
}
|
|
4009
|
-
writeJson(res, 200, { connections: pool.status() });
|
|
4183
|
+
if (method !== "PATCH" && method !== "DELETE") {
|
|
4184
|
+
writeJson(res, 405, { error: `method not allowed: ${method}` });
|
|
4185
|
+
return;
|
|
4010
4186
|
}
|
|
4011
|
-
|
|
4012
|
-
|
|
4013
|
-
|
|
4014
|
-
|
|
4015
|
-
handler: async (req, res) => {
|
|
4016
|
-
if (!isLoopbackRequest(req)) {
|
|
4017
|
-
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4018
|
-
return;
|
|
4019
|
-
}
|
|
4020
|
-
if (req.method !== "POST") {
|
|
4021
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4022
|
-
return;
|
|
4023
|
-
}
|
|
4024
|
-
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4025
|
-
if (!read.ok) return;
|
|
4026
|
-
const body = read.body;
|
|
4027
|
-
if (body === null || typeof body.alias !== "string") {
|
|
4028
|
-
writeJson(res, 400, { error: "alias is required" });
|
|
4029
|
-
return;
|
|
4030
|
-
}
|
|
4031
|
-
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;
|
|
4032
4191
|
}
|
|
4033
|
-
|
|
4034
|
-
|
|
4035
|
-
|
|
4036
|
-
|
|
4037
|
-
|
|
4038
|
-
if (!
|
|
4039
|
-
writeJson(res,
|
|
4040
|
-
return;
|
|
4041
|
-
}
|
|
4042
|
-
if (req.method !== "POST") {
|
|
4043
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4044
|
-
return;
|
|
4045
|
-
}
|
|
4046
|
-
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4047
|
-
if (!read.ok) return;
|
|
4048
|
-
const body = read.body;
|
|
4049
|
-
if (body === null || typeof body.alias !== "string") {
|
|
4050
|
-
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}` });
|
|
4051
4199
|
return;
|
|
4052
4200
|
}
|
|
4053
|
-
|
|
4054
|
-
|
|
4055
|
-
|
|
4056
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
4057
|
-
writeJson(res, message.startsWith("unknown host alias") ? 404 : 502, { error: message });
|
|
4058
|
-
}
|
|
4201
|
+
worlds.syncWorkspaces().catch(() => void 0);
|
|
4202
|
+
writeJson(res, 200, { ok: true });
|
|
4203
|
+
return;
|
|
4059
4204
|
}
|
|
4060
|
-
|
|
4061
|
-
|
|
4062
|
-
|
|
4063
|
-
|
|
4064
|
-
|
|
4065
|
-
|
|
4066
|
-
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4067
|
-
return;
|
|
4068
|
-
}
|
|
4069
|
-
if (req.method !== "POST") {
|
|
4070
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4071
|
-
return;
|
|
4072
|
-
}
|
|
4073
|
-
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4074
|
-
if (!read.ok) return;
|
|
4075
|
-
const body = read.body;
|
|
4076
|
-
if (body === null || typeof body.alias !== "string" || typeof body.command !== "string" || body.command === "") {
|
|
4077
|
-
writeJson(res, 400, { error: "alias and a non-empty command are required" });
|
|
4078
|
-
return;
|
|
4079
|
-
}
|
|
4080
|
-
const timeoutMs = clampExecTimeout(body.timeoutMs);
|
|
4081
|
-
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;
|
|
4082
4211
|
}
|
|
4083
|
-
|
|
4084
|
-
|
|
4085
|
-
|
|
4086
|
-
|
|
4087
|
-
|
|
4088
|
-
|
|
4089
|
-
|
|
4090
|
-
|
|
4091
|
-
}
|
|
4092
|
-
if (req.method !== "POST") {
|
|
4093
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4094
|
-
return;
|
|
4095
|
-
}
|
|
4096
|
-
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4097
|
-
if (!read.ok) return;
|
|
4098
|
-
const body = read.body;
|
|
4099
|
-
if (body === null || typeof body.alias !== "string" || typeof body.command !== "string" || body.command === "") {
|
|
4100
|
-
writeJson(res, 400, { error: "alias and a non-empty command are required" });
|
|
4101
|
-
return;
|
|
4102
|
-
}
|
|
4103
|
-
const timeoutMs = clampExecTimeout(body.timeoutMs);
|
|
4104
|
-
res.writeHead(200, {
|
|
4105
|
-
"Content-Type": "text/event-stream; charset=utf-8",
|
|
4106
|
-
"Cache-Control": "no-cache, no-transform",
|
|
4107
|
-
Connection: "keep-alive",
|
|
4108
|
-
"X-Accel-Buffering": "no"
|
|
4109
|
-
});
|
|
4110
|
-
res.flushHeaders();
|
|
4111
|
-
/** No more events to the client (finished, or the socket broke). */
|
|
4112
|
-
let outputClosed = false;
|
|
4113
|
-
/** `res.end` already called. */
|
|
4114
|
-
let ended = false;
|
|
4115
|
-
/** The client asked to stop: kill the remote even if the read is still opening. */
|
|
4116
|
-
let cancelRequested = false;
|
|
4117
|
-
/** The cancel handle was invoked (only once). */
|
|
4118
|
-
let cancelSent = false;
|
|
4119
|
-
let cancel;
|
|
4120
|
-
/** Server-side forward cap: the client shows a scrolling tail anyway. */
|
|
4121
|
-
let sent = 0;
|
|
4122
|
-
const FORWARD_CAP = 4 * 1024 * 1024;
|
|
4123
|
-
/**
|
|
4124
|
-
* Stop the remote process group exactly once.
|
|
4125
|
-
*
|
|
4126
|
-
* The disconnect may happen while `pool.exec` is still acquiring the
|
|
4127
|
-
* channel (slot queue, setsid probe, connect) — the handle does not exist
|
|
4128
|
-
* yet then, so the request is latched and fired the moment `onCancel`
|
|
4129
|
-
* arrives; otherwise a push would finish unseen and uncancellable.
|
|
4130
|
-
*/
|
|
4131
|
-
const abortRemote = () => {
|
|
4132
|
-
cancelRequested = true;
|
|
4133
|
-
if (cancel === void 0 || cancelSent) return;
|
|
4134
|
-
cancelSent = true;
|
|
4135
|
-
cancel();
|
|
4136
|
-
};
|
|
4137
|
-
const send = (event) => {
|
|
4138
|
-
if (outputClosed) return;
|
|
4139
|
-
try {
|
|
4140
|
-
res.write(`data: ${JSON.stringify(event)}\n\n`);
|
|
4141
|
-
} catch {
|
|
4142
|
-
outputClosed = true;
|
|
4143
|
-
abortRemote();
|
|
4144
|
-
}
|
|
4145
|
-
};
|
|
4146
|
-
const heartbeat = setInterval(() => {
|
|
4147
|
-
if (outputClosed) return;
|
|
4148
|
-
try {
|
|
4149
|
-
res.write(": ping\n\n");
|
|
4150
|
-
} catch {
|
|
4151
|
-
outputClosed = true;
|
|
4152
|
-
abortRemote();
|
|
4153
|
-
}
|
|
4154
|
-
}, 15e3);
|
|
4155
|
-
heartbeat.unref?.();
|
|
4156
|
-
const stop = () => {
|
|
4157
|
-
if (ended) return;
|
|
4158
|
-
ended = true;
|
|
4159
|
-
outputClosed = true;
|
|
4160
|
-
clearInterval(heartbeat);
|
|
4161
|
-
try {
|
|
4162
|
-
res.end();
|
|
4163
|
-
} catch {}
|
|
4164
|
-
};
|
|
4165
|
-
res.on("close", () => {
|
|
4166
|
-
abortRemote();
|
|
4167
|
-
stop();
|
|
4168
|
-
});
|
|
4169
|
-
try {
|
|
4170
|
-
const result = await pool.exec(body.alias, body.command, {
|
|
4171
|
-
...timeoutMs === void 0 ? {} : { timeoutMs },
|
|
4172
|
-
onData: (stream, text) => {
|
|
4173
|
-
if (outputClosed || sent >= FORWARD_CAP) return;
|
|
4174
|
-
sent += Buffer.byteLength(text, "utf8");
|
|
4175
|
-
send({
|
|
4176
|
-
t: stream,
|
|
4177
|
-
d: text
|
|
4178
|
-
});
|
|
4179
|
-
},
|
|
4180
|
-
onCancel: (handle) => {
|
|
4181
|
-
cancel = handle;
|
|
4182
|
-
if (cancelRequested && !cancelSent) {
|
|
4183
|
-
cancelSent = true;
|
|
4184
|
-
handle();
|
|
4185
|
-
}
|
|
4186
|
-
}
|
|
4187
|
-
});
|
|
4188
|
-
if (outputClosed) return;
|
|
4189
|
-
send({
|
|
4190
|
-
t: "exit",
|
|
4191
|
-
success: result.success,
|
|
4192
|
-
exitCode: result.exitCode,
|
|
4193
|
-
timedOut: result.timedOut,
|
|
4194
|
-
durationMs: result.durationMs,
|
|
4195
|
-
...result.error === void 0 ? {} : { error: result.error }
|
|
4196
|
-
});
|
|
4197
|
-
} catch (error) {
|
|
4198
|
-
send({
|
|
4199
|
-
t: "exit",
|
|
4200
|
-
success: false,
|
|
4201
|
-
exitCode: null,
|
|
4202
|
-
timedOut: false,
|
|
4203
|
-
durationMs: 0,
|
|
4204
|
-
error: error instanceof Error ? error.message : String(error)
|
|
4205
|
-
});
|
|
4206
|
-
} finally {
|
|
4207
|
-
stop();
|
|
4208
|
-
}
|
|
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) });
|
|
4209
4220
|
}
|
|
4210
|
-
}
|
|
4211
|
-
|
|
4212
|
-
|
|
4213
|
-
|
|
4214
|
-
|
|
4215
|
-
|
|
4216
|
-
|
|
4217
|
-
|
|
4218
|
-
|
|
4219
|
-
if (req.method !== "POST") {
|
|
4220
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4221
|
-
return;
|
|
4222
|
-
}
|
|
4223
|
-
if (bodyTooLarge(req, FILES_BODY_LIMIT)) {
|
|
4224
|
-
writePayloadTooLarge(req, res, FILES_BODY_LIMIT);
|
|
4225
|
-
return;
|
|
4226
|
-
}
|
|
4227
|
-
const read = await readBodyOrError(req, res, {
|
|
4228
|
-
objectOnly: true,
|
|
4229
|
-
maxBytes: FILES_BODY_LIMIT
|
|
4230
|
-
});
|
|
4231
|
-
if (!read.ok) return;
|
|
4232
|
-
const body = read.body;
|
|
4233
|
-
if (body === null || typeof body.op !== "string" || typeof body.alias !== "string") {
|
|
4234
|
-
writeJson(res, 400, { error: "op and alias are required" });
|
|
4235
|
-
return;
|
|
4236
|
-
}
|
|
4237
|
-
const alias = body.alias;
|
|
4238
|
-
try {
|
|
4239
|
-
switch (body.op) {
|
|
4240
|
-
case "list":
|
|
4241
|
-
if (typeof body.path !== "string") throw new BadRequestError("path is required");
|
|
4242
|
-
writeJson(res, 200, { entries: await files.list(alias, body.path) });
|
|
4243
|
-
return;
|
|
4244
|
-
case "stat":
|
|
4245
|
-
if (typeof body.path !== "string") throw new BadRequestError("path is required");
|
|
4246
|
-
writeJson(res, 200, { entry: await files.stat(alias, body.path) });
|
|
4247
|
-
return;
|
|
4248
|
-
case "read":
|
|
4249
|
-
if (typeof body.path !== "string") throw new BadRequestError("path is required");
|
|
4250
|
-
writeJson(res, 200, await files.read(alias, body.path));
|
|
4251
|
-
return;
|
|
4252
|
-
case "write": {
|
|
4253
|
-
if (typeof body.path !== "string" || typeof body.content !== "string") throw new BadRequestError("path and content are required");
|
|
4254
|
-
const encoding = body.encoding === "base64" ? "base64" : "utf8";
|
|
4255
|
-
/**
|
|
4256
|
-
* Optional optimistic lock. `expectMtime` is the version the client
|
|
4257
|
-
* loaded; when present the Host refuses a file that changed since.
|
|
4258
|
-
* Requests without it (helpers, deploy, the agent's write tool)
|
|
4259
|
-
* keep the old unconditional semantics — the guard protects a
|
|
4260
|
-
* human's buffer, it must not make programmatic writes fail.
|
|
4261
|
-
*/
|
|
4262
|
-
const expectMtime = body.expectMtime;
|
|
4263
|
-
const expectSize = body.expectSize;
|
|
4264
|
-
/**
|
|
4265
|
-
* A malformed baseline is a CALLER mistake, not "no baseline".
|
|
4266
|
-
*
|
|
4267
|
-
* Silently dropping it would turn `{expectMtime: "123"}` (a client
|
|
4268
|
-
* bug, a serializer change) into an UNGUARDED write — the exact
|
|
4269
|
-
* overwrite the caller was asking to be protected from, reported as
|
|
4270
|
-
* a success.
|
|
4271
|
-
*/
|
|
4272
|
-
if (expectMtime !== void 0 && (typeof expectMtime !== "number" || !Number.isFinite(expectMtime))) throw new BadRequestError("expectMtime must be a finite number");
|
|
4273
|
-
if (expectSize !== void 0 && (typeof expectSize !== "number" || !Number.isFinite(expectSize))) throw new BadRequestError("expectSize must be a finite number");
|
|
4274
|
-
const guard = typeof expectMtime === "number" ? {
|
|
4275
|
-
expectMtime,
|
|
4276
|
-
...typeof expectSize === "number" ? { expectSize } : {}
|
|
4277
|
-
} : void 0;
|
|
4278
|
-
writeJson(res, 200, await files.write(alias, body.path, body.content, encoding, guard));
|
|
4279
|
-
return;
|
|
4280
|
-
}
|
|
4281
|
-
case "mkdir":
|
|
4282
|
-
if (typeof body.path !== "string") throw new BadRequestError("path is required");
|
|
4283
|
-
await files.mkdir(alias, body.path);
|
|
4284
|
-
writeJson(res, 200, { ok: true });
|
|
4285
|
-
return;
|
|
4286
|
-
case "rm":
|
|
4287
|
-
if (typeof body.path !== "string") throw new BadRequestError("path is required");
|
|
4288
|
-
await files.remove(alias, body.path, body.recursive === true);
|
|
4289
|
-
writeJson(res, 200, { ok: true });
|
|
4290
|
-
return;
|
|
4291
|
-
case "rename":
|
|
4292
|
-
if (typeof body.from !== "string" || typeof body.to !== "string") throw new BadRequestError("from and to are required");
|
|
4293
|
-
await files.rename(alias, body.from, body.to);
|
|
4294
|
-
writeJson(res, 200, { ok: true });
|
|
4295
|
-
return;
|
|
4296
|
-
default: writeJson(res, 400, { error: `unknown op: ${body.op}` });
|
|
4297
|
-
}
|
|
4298
|
-
} catch (error) {
|
|
4299
|
-
/**
|
|
4300
|
-
* A save the remote side refused because the file moved on.
|
|
4301
|
-
*
|
|
4302
|
-
* 409, not 502: the operation did not fail, it was DECLINED — and the
|
|
4303
|
-
* client needs the current version to offer overwrite / reload / see
|
|
4304
|
-
* the difference. Collapsing this into the generic remote-error path
|
|
4305
|
-
* is how a silent overwrite becomes a red toast nobody can act on.
|
|
4306
|
-
*/
|
|
4307
|
-
if (error instanceof WriteConflictError) {
|
|
4308
|
-
writeJson(res, 409, {
|
|
4309
|
-
error: error.message,
|
|
4310
|
-
code: "conflict",
|
|
4311
|
-
conflict: error.conflict
|
|
4312
|
-
});
|
|
4313
|
-
return;
|
|
4314
|
-
}
|
|
4315
|
-
writeJson(res, error instanceof BadRequestError ? 400 : 502, { error: error instanceof Error ? error.message : String(error) });
|
|
4316
|
-
}
|
|
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;
|
|
4317
4230
|
}
|
|
4318
|
-
|
|
4319
|
-
|
|
4320
|
-
|
|
4321
|
-
path: REMOTE_API.upload,
|
|
4322
|
-
handler: async (req, res) => {
|
|
4323
|
-
if (!isLoopbackRequest(req)) {
|
|
4324
|
-
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4325
|
-
return;
|
|
4326
|
-
}
|
|
4327
|
-
if (req.method !== "POST") {
|
|
4328
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4329
|
-
return;
|
|
4330
|
-
}
|
|
4331
|
-
const url = new URL(req.url ?? "/", "http://localhost");
|
|
4332
|
-
const alias = url.searchParams.get("alias") ?? "";
|
|
4333
|
-
const path = url.searchParams.get("path") ?? "";
|
|
4334
|
-
const overwrite = url.searchParams.get("overwrite") === "1";
|
|
4335
|
-
if (alias === "" || path === "") {
|
|
4336
|
-
writeJson(res, 400, { error: "alias and path query parameters are required" });
|
|
4337
|
-
return;
|
|
4338
|
-
}
|
|
4339
|
-
if (!path.startsWith("/")) {
|
|
4340
|
-
writeJson(res, 400, { error: `path must be absolute: ${path}` });
|
|
4341
|
-
return;
|
|
4342
|
-
}
|
|
4343
|
-
try {
|
|
4344
|
-
if (!overwrite) {
|
|
4345
|
-
if (await files.stat(alias, path).then(() => true, () => false)) {
|
|
4346
|
-
writeJson(res, 409, {
|
|
4347
|
-
error: `${path} already exists`,
|
|
4348
|
-
exists: true
|
|
4349
|
-
});
|
|
4350
|
-
return;
|
|
4351
|
-
}
|
|
4352
|
-
}
|
|
4353
|
-
writeJson(res, 200, {
|
|
4354
|
-
ok: true,
|
|
4355
|
-
size: (await files.upload(alias, path, req, void 0, overwrite)).size
|
|
4356
|
-
});
|
|
4357
|
-
} catch (error) {
|
|
4358
|
-
writeRouteError(res, error);
|
|
4359
|
-
}
|
|
4231
|
+
if (req.method !== "GET") {
|
|
4232
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4233
|
+
return;
|
|
4360
4234
|
}
|
|
4361
|
-
|
|
4362
|
-
|
|
4363
|
-
|
|
4364
|
-
|
|
4365
|
-
|
|
4366
|
-
|
|
4367
|
-
|
|
4368
|
-
|
|
4369
|
-
}
|
|
4370
|
-
|
|
4371
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4372
|
-
return;
|
|
4373
|
-
}
|
|
4374
|
-
const url = new URL(req.url ?? "/", "http://localhost");
|
|
4375
|
-
const alias = url.searchParams.get("alias") ?? "";
|
|
4376
|
-
const path = url.searchParams.get("path") ?? "";
|
|
4377
|
-
if (alias === "" || path === "") {
|
|
4378
|
-
writeJson(res, 400, { error: "alias and path query parameters are required" });
|
|
4379
|
-
return;
|
|
4380
|
-
}
|
|
4381
|
-
if (!path.startsWith("/")) {
|
|
4382
|
-
writeJson(res, 400, { error: `path must be absolute: ${path}` });
|
|
4383
|
-
return;
|
|
4384
|
-
}
|
|
4385
|
-
try {
|
|
4386
|
-
await files.download(alias, path, res);
|
|
4387
|
-
} catch (error) {
|
|
4388
|
-
if (!res.headersSent && !res.destroyed) writeRouteError(res, error);
|
|
4389
|
-
else res.destroy(error instanceof Error ? error : void 0);
|
|
4390
|
-
}
|
|
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;
|
|
4391
4245
|
}
|
|
4392
|
-
|
|
4393
|
-
|
|
4394
|
-
|
|
4395
|
-
path: REMOTE_API.downloadDir,
|
|
4396
|
-
handler: async (req, res) => {
|
|
4397
|
-
if (!isLoopbackRequest(req)) {
|
|
4398
|
-
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4399
|
-
return;
|
|
4400
|
-
}
|
|
4401
|
-
if (req.method !== "GET") {
|
|
4402
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4403
|
-
return;
|
|
4404
|
-
}
|
|
4405
|
-
const url = new URL(req.url ?? "/", "http://localhost");
|
|
4406
|
-
const alias = url.searchParams.get("alias") ?? "";
|
|
4407
|
-
const path = url.searchParams.get("path") ?? "";
|
|
4408
|
-
if (alias === "" || path === "") {
|
|
4409
|
-
writeJson(res, 400, { error: "alias and path query parameters are required" });
|
|
4410
|
-
return;
|
|
4411
|
-
}
|
|
4412
|
-
if (!path.startsWith("/")) {
|
|
4413
|
-
writeJson(res, 400, { error: `path must be absolute: ${path}` });
|
|
4414
|
-
return;
|
|
4415
|
-
}
|
|
4416
|
-
let command;
|
|
4417
|
-
try {
|
|
4418
|
-
if (!(await files.stat(alias, path)).isDir) {
|
|
4419
|
-
writeJson(res, 400, { error: `not a directory: ${path}` });
|
|
4420
|
-
return;
|
|
4421
|
-
}
|
|
4422
|
-
command = directoryArchiveCommand(path);
|
|
4423
|
-
} catch (error) {
|
|
4424
|
-
writeRouteError(res, error);
|
|
4425
|
-
return;
|
|
4426
|
-
}
|
|
4427
|
-
await streamArchive(alias, command, res, directoryArchiveName(path));
|
|
4246
|
+
if (req.method !== "POST") {
|
|
4247
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4248
|
+
return;
|
|
4428
4249
|
}
|
|
4429
|
-
|
|
4430
|
-
|
|
4431
|
-
|
|
4432
|
-
|
|
4433
|
-
|
|
4434
|
-
|
|
4435
|
-
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4436
|
-
return;
|
|
4437
|
-
}
|
|
4438
|
-
if (req.method !== "GET") {
|
|
4439
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4440
|
-
return;
|
|
4441
|
-
}
|
|
4442
|
-
const url = new URL(req.url ?? "/", "http://localhost");
|
|
4443
|
-
const alias = url.searchParams.get("alias") ?? "";
|
|
4444
|
-
const paths = url.searchParams.getAll("path");
|
|
4445
|
-
if (alias === "" || paths.length === 0) {
|
|
4446
|
-
writeJson(res, 400, { error: "alias and at least one path query parameter are required" });
|
|
4447
|
-
return;
|
|
4448
|
-
}
|
|
4449
|
-
if (paths.length > 100) {
|
|
4450
|
-
writeJson(res, 400, { error: `too many entries: ${paths.length} (max 100)` });
|
|
4451
|
-
return;
|
|
4452
|
-
}
|
|
4453
|
-
for (const path of paths) if (!path.startsWith("/")) {
|
|
4454
|
-
writeJson(res, 400, { error: `path must be absolute: ${path}` });
|
|
4455
|
-
return;
|
|
4456
|
-
}
|
|
4457
|
-
let command;
|
|
4458
|
-
try {
|
|
4459
|
-
for (const path of paths) await files.stat(alias, path);
|
|
4460
|
-
command = selectionArchiveCommand(paths);
|
|
4461
|
-
} catch (error) {
|
|
4462
|
-
writeRouteError(res, error);
|
|
4463
|
-
return;
|
|
4464
|
-
}
|
|
4465
|
-
await streamArchive(alias, command, res, selectionDownloadName(paths.length));
|
|
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;
|
|
4466
4256
|
}
|
|
4467
|
-
|
|
4468
|
-
|
|
4469
|
-
|
|
4470
|
-
|
|
4471
|
-
|
|
4472
|
-
|
|
4473
|
-
|
|
4474
|
-
|
|
4475
|
-
}
|
|
4476
|
-
|
|
4477
|
-
|
|
4478
|
-
|
|
4479
|
-
|
|
4480
|
-
|
|
4481
|
-
|
|
4482
|
-
|
|
4483
|
-
|
|
4484
|
-
|
|
4485
|
-
|
|
4486
|
-
|
|
4487
|
-
|
|
4488
|
-
|
|
4489
|
-
|
|
4490
|
-
|
|
4491
|
-
|
|
4492
|
-
|
|
4493
|
-
|
|
4494
|
-
|
|
4495
|
-
|
|
4496
|
-
|
|
4497
|
-
|
|
4498
|
-
|
|
4499
|
-
|
|
4500
|
-
|
|
4501
|
-
|
|
4502
|
-
|
|
4503
|
-
|
|
4504
|
-
|
|
4505
|
-
|
|
4506
|
-
|
|
4507
|
-
|
|
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();
|
|
4508
4342
|
}
|
|
4509
|
-
await worlds.syncWorkspaces().catch(() => void 0);
|
|
4510
|
-
writeJson(res, 201, {
|
|
4511
|
-
workspace: binding,
|
|
4512
|
-
aliasPath
|
|
4513
|
-
});
|
|
4514
|
-
} catch (error) {
|
|
4515
|
-
writeJson(res, 400, { error: error instanceof Error ? error.message : String(error) });
|
|
4516
4343
|
}
|
|
4517
|
-
|
|
4518
|
-
|
|
4519
|
-
|
|
4520
|
-
|
|
4521
|
-
return;
|
|
4522
|
-
}
|
|
4523
|
-
const id = queryParam(url, "id");
|
|
4524
|
-
if (id === void 0 || id === "") {
|
|
4525
|
-
writeJson(res, 400, { error: "id query parameter is required" });
|
|
4526
|
-
return;
|
|
4527
|
-
}
|
|
4528
|
-
if (!store.removeWorkspace(id)) {
|
|
4529
|
-
writeJson(res, 404, { error: `unknown workspace id: ${id}` });
|
|
4530
|
-
return;
|
|
4531
|
-
}
|
|
4532
|
-
worlds.syncWorkspaces().catch(() => void 0);
|
|
4533
|
-
writeJson(res, 200, { ok: true });
|
|
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) });
|
|
4534
4348
|
}
|
|
4535
|
-
}
|
|
4536
|
-
|
|
4537
|
-
|
|
4538
|
-
|
|
4539
|
-
|
|
4540
|
-
|
|
4541
|
-
|
|
4542
|
-
|
|
4543
|
-
|
|
4544
|
-
if ((req.method ?? "GET") !== "POST") {
|
|
4545
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method ?? "GET"}` });
|
|
4546
|
-
return;
|
|
4547
|
-
}
|
|
4548
|
-
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4549
|
-
if (!read.ok) return;
|
|
4550
|
-
const body = read.body;
|
|
4551
|
-
const sessionId = typeof body?.sessionId === "string" ? body.sessionId : "";
|
|
4552
|
-
if (sessionId === "") {
|
|
4553
|
-
writeJson(res, 400, { error: "sessionId must be a non-empty string" });
|
|
4554
|
-
return;
|
|
4555
|
-
}
|
|
4556
|
-
writeJson(res, 200, { cwd: liveSessionCwd(ctx, sessionId) });
|
|
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;
|
|
4557
4358
|
}
|
|
4558
|
-
|
|
4559
|
-
|
|
4560
|
-
|
|
4561
|
-
path: REMOTE_API.changes,
|
|
4562
|
-
handler: async (req, res) => {
|
|
4563
|
-
if (!isLoopbackRequest(req)) {
|
|
4564
|
-
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4565
|
-
return;
|
|
4566
|
-
}
|
|
4567
|
-
if ((req.method ?? "GET") !== "POST") {
|
|
4568
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method ?? "GET"}` });
|
|
4569
|
-
return;
|
|
4570
|
-
}
|
|
4571
|
-
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4572
|
-
if (!read.ok) return;
|
|
4573
|
-
const body = read.body;
|
|
4574
|
-
const sessionId = typeof body?.sessionId === "string" ? body.sessionId : "";
|
|
4575
|
-
const op = typeof body?.op === "string" ? body.op : "";
|
|
4576
|
-
if (sessionId === "" || op !== "list" && op !== "diff") {
|
|
4577
|
-
writeJson(res, 400, { error: "op (list|diff) and a non-empty sessionId are required" });
|
|
4578
|
-
return;
|
|
4579
|
-
}
|
|
4580
|
-
const ledger = deps.changes;
|
|
4581
|
-
if (ledger === void 0) {
|
|
4582
|
-
writeJson(res, 200, {
|
|
4583
|
-
summary: null,
|
|
4584
|
-
diff: null
|
|
4585
|
-
});
|
|
4586
|
-
return;
|
|
4587
|
-
}
|
|
4588
|
-
if (typeof body?.repoSubdir === "string" || body?.repoSubdir === null) ledger.useRepoSubdir(sessionId, typeof body.repoSubdir === "string" ? body.repoSubdir : void 0);
|
|
4589
|
-
if (op === "list") {
|
|
4590
|
-
if (ledger.summary(sessionId) === null) {
|
|
4591
|
-
const cwd = liveSessionCwd(ctx, sessionId);
|
|
4592
|
-
if (cwd !== void 0) ledger.beginTurn({
|
|
4593
|
-
sessionId,
|
|
4594
|
-
cwd
|
|
4595
|
-
});
|
|
4596
|
-
}
|
|
4597
|
-
if (body?.force === true) ledger.rescan(sessionId);
|
|
4598
|
-
else ledger.touch(sessionId);
|
|
4599
|
-
writeJson(res, 200, { summary: ledger.summary(sessionId) });
|
|
4600
|
-
return;
|
|
4601
|
-
}
|
|
4602
|
-
const path = typeof body?.path === "string" ? body.path : "";
|
|
4603
|
-
if (path === "") {
|
|
4604
|
-
writeJson(res, 400, { error: "path is required for op=diff" });
|
|
4605
|
-
return;
|
|
4606
|
-
}
|
|
4607
|
-
try {
|
|
4608
|
-
writeJson(res, 200, { diff: await ledger.diff(sessionId, path) });
|
|
4609
|
-
} catch (error) {
|
|
4610
|
-
writeRouteError(res, error);
|
|
4611
|
-
}
|
|
4359
|
+
if (req.method !== "POST") {
|
|
4360
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4361
|
+
return;
|
|
4612
4362
|
}
|
|
4613
|
-
|
|
4614
|
-
|
|
4615
|
-
|
|
4616
|
-
|
|
4617
|
-
|
|
4618
|
-
|
|
4619
|
-
|
|
4620
|
-
|
|
4621
|
-
|
|
4622
|
-
|
|
4623
|
-
|
|
4624
|
-
|
|
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();
|
|
4625
4411
|
}
|
|
4626
|
-
|
|
4627
|
-
|
|
4628
|
-
|
|
4629
|
-
|
|
4630
|
-
|
|
4631
|
-
|
|
4412
|
+
};
|
|
4413
|
+
const heartbeat = setInterval(() => {
|
|
4414
|
+
if (outputClosed) return;
|
|
4415
|
+
try {
|
|
4416
|
+
res.write(": ping\n\n");
|
|
4417
|
+
} catch {
|
|
4418
|
+
outputClosed = true;
|
|
4419
|
+
abortRemote();
|
|
4632
4420
|
}
|
|
4421
|
+
}, 15e3);
|
|
4422
|
+
heartbeat.unref?.();
|
|
4423
|
+
const stop = () => {
|
|
4424
|
+
if (ended) return;
|
|
4425
|
+
ended = true;
|
|
4426
|
+
outputClosed = true;
|
|
4427
|
+
clearInterval(heartbeat);
|
|
4633
4428
|
try {
|
|
4634
|
-
|
|
4635
|
-
|
|
4636
|
-
|
|
4637
|
-
|
|
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();
|
|
4452
|
+
}
|
|
4638
4453
|
}
|
|
4639
|
-
|
|
4640
|
-
|
|
4641
|
-
|
|
4642
|
-
|
|
4643
|
-
|
|
4644
|
-
|
|
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();
|
|
4645
4475
|
}
|
|
4646
|
-
}
|
|
4647
|
-
|
|
4648
|
-
|
|
4649
|
-
|
|
4650
|
-
|
|
4651
|
-
|
|
4652
|
-
|
|
4653
|
-
|
|
4654
|
-
|
|
4655
|
-
const method = req.method ?? "GET";
|
|
4656
|
-
if (method === "GET") {
|
|
4657
|
-
try {
|
|
4658
|
-
writeJson(res, 200, { settings: viewSettings.get() });
|
|
4659
|
-
} catch (error) {
|
|
4660
|
-
writeRouteError(res, error);
|
|
4661
|
-
}
|
|
4662
|
-
return;
|
|
4663
|
-
}
|
|
4664
|
-
if (method !== "POST") {
|
|
4665
|
-
writeJson(res, 405, { error: `method not allowed: ${method}` });
|
|
4666
|
-
return;
|
|
4667
|
-
}
|
|
4668
|
-
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4669
|
-
if (!read.ok) return;
|
|
4670
|
-
try {
|
|
4671
|
-
writeJson(res, 200, { settings: viewSettings.patch(read.body ?? {}) });
|
|
4672
|
-
} catch (error) {
|
|
4673
|
-
writeRouteError(res, error);
|
|
4674
|
-
}
|
|
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;
|
|
4675
4485
|
}
|
|
4676
|
-
|
|
4677
|
-
|
|
4678
|
-
|
|
4679
|
-
|
|
4680
|
-
|
|
4681
|
-
|
|
4682
|
-
|
|
4683
|
-
|
|
4684
|
-
|
|
4685
|
-
|
|
4686
|
-
|
|
4687
|
-
|
|
4688
|
-
|
|
4689
|
-
|
|
4690
|
-
|
|
4691
|
-
|
|
4486
|
+
if (req.method !== "POST") {
|
|
4487
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4488
|
+
return;
|
|
4489
|
+
}
|
|
4490
|
+
if (bodyTooLarge(req, FILES_BODY_LIMIT)) {
|
|
4491
|
+
writePayloadTooLarge(req, res, FILES_BODY_LIMIT);
|
|
4492
|
+
return;
|
|
4493
|
+
}
|
|
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) });
|
|
4692
4510
|
return;
|
|
4693
|
-
|
|
4694
|
-
|
|
4695
|
-
|
|
4696
|
-
writeJson(res, 200, await probe(pool, alias));
|
|
4697
|
-
} catch (error) {
|
|
4698
|
-
writeRouteError(res, error);
|
|
4699
|
-
}
|
|
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) });
|
|
4700
4514
|
return;
|
|
4701
|
-
|
|
4702
|
-
|
|
4703
|
-
|
|
4704
|
-
|
|
4705
|
-
|
|
4706
|
-
|
|
4707
|
-
|
|
4708
|
-
|
|
4709
|
-
|
|
4710
|
-
|
|
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));
|
|
4711
4546
|
return;
|
|
4712
4547
|
}
|
|
4713
|
-
|
|
4714
|
-
|
|
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}` });
|
|
4715
4564
|
}
|
|
4716
|
-
|
|
4717
|
-
|
|
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
|
+
});
|
|
4718
4580
|
return;
|
|
4719
4581
|
}
|
|
4720
|
-
|
|
4721
|
-
|
|
4722
|
-
|
|
4723
|
-
|
|
4724
|
-
|
|
4582
|
+
writeJson(res, error instanceof BadRequestError ? 400 : 502, { error: error instanceof Error ? error.message : String(error) });
|
|
4583
|
+
}
|
|
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;
|
|
4593
|
+
}
|
|
4594
|
+
if (req.method !== "POST") {
|
|
4595
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4596
|
+
return;
|
|
4597
|
+
}
|
|
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;
|
|
4605
|
+
}
|
|
4606
|
+
if (!path.startsWith("/")) {
|
|
4607
|
+
writeJson(res, 400, { error: `path must be absolute: ${path}` });
|
|
4608
|
+
return;
|
|
4609
|
+
}
|
|
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
|
+
});
|
|
4617
|
+
return;
|
|
4618
|
+
}
|
|
4619
|
+
}
|
|
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}` });
|
|
4725
4687
|
return;
|
|
4726
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") {
|
|
4727
4746
|
try {
|
|
4728
|
-
|
|
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
|
+
});
|
|
4729
4759
|
} catch (error) {
|
|
4730
|
-
|
|
4760
|
+
writeStoreError(res, error);
|
|
4731
4761
|
}
|
|
4762
|
+
return;
|
|
4732
4763
|
}
|
|
4733
|
-
|
|
4734
|
-
|
|
4735
|
-
kind: "exact",
|
|
4736
|
-
path: REMOTE_API.importSshConfig,
|
|
4737
|
-
handler: async (req, res) => {
|
|
4738
|
-
if (!isLoopbackRequest(req)) {
|
|
4739
|
-
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4740
|
-
return;
|
|
4741
|
-
}
|
|
4742
|
-
if (req.method !== "POST") {
|
|
4743
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4744
|
-
return;
|
|
4745
|
-
}
|
|
4746
|
-
if (bodyTooLarge(req, SSH_CONFIG_BODY_LIMIT)) {
|
|
4747
|
-
writePayloadTooLarge(req, res, SSH_CONFIG_BODY_LIMIT);
|
|
4748
|
-
return;
|
|
4749
|
-
}
|
|
4750
|
-
const read = await readBodyOrError(req, res, { maxBytes: SSH_CONFIG_BODY_LIMIT });
|
|
4764
|
+
if (method === "POST") {
|
|
4765
|
+
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4751
4766
|
if (!read.ok) return;
|
|
4752
4767
|
const body = read.body;
|
|
4753
|
-
|
|
4754
|
-
|
|
4755
|
-
writeJson(res, 200, applyImport(parseSshConfig(text)));
|
|
4756
|
-
} catch (error) {
|
|
4757
|
-
writeJson(res, 400, { error: error instanceof Error ? error.message : String(error) });
|
|
4758
|
-
}
|
|
4759
|
-
}
|
|
4760
|
-
},
|
|
4761
|
-
{
|
|
4762
|
-
kind: "exact",
|
|
4763
|
-
path: REMOTE_API.importDshSsh,
|
|
4764
|
-
handler: async (req, res) => {
|
|
4765
|
-
if (!isLoopbackRequest(req)) {
|
|
4766
|
-
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4767
|
-
return;
|
|
4768
|
-
}
|
|
4769
|
-
if (req.method !== "POST") {
|
|
4770
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4768
|
+
if (body === null) {
|
|
4769
|
+
writeJson(res, 400, { error: "invalid JSON body" });
|
|
4771
4770
|
return;
|
|
4772
4771
|
}
|
|
4773
4772
|
try {
|
|
4774
|
-
|
|
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
|
+
});
|
|
4775
4785
|
} catch (error) {
|
|
4776
4786
|
writeJson(res, 400, { error: error instanceof Error ? error.message : String(error) });
|
|
4777
4787
|
}
|
|
4788
|
+
return;
|
|
4778
4789
|
}
|
|
4779
|
-
|
|
4780
|
-
|
|
4781
|
-
|
|
4782
|
-
path: REMOTE_API.terminalClose,
|
|
4783
|
-
handler: async (req, res) => {
|
|
4784
|
-
if (!isLoopbackRequest(req)) {
|
|
4785
|
-
writeJson(res, 403, { error: "forbidden: loopback-only" });
|
|
4786
|
-
return;
|
|
4787
|
-
}
|
|
4788
|
-
if (req.method !== "POST") {
|
|
4789
|
-
writeJson(res, 405, { error: `method not allowed: ${req.method}` });
|
|
4790
|
-
return;
|
|
4791
|
-
}
|
|
4792
|
-
const read = await readBodyOrError(req, res, { objectOnly: true });
|
|
4793
|
-
if (!read.ok) return;
|
|
4794
|
-
const body = read.body;
|
|
4795
|
-
if (body === null || typeof body.id !== "string" || body.id === "") {
|
|
4796
|
-
writeJson(res, 400, { error: "id is required" });
|
|
4797
|
-
return;
|
|
4798
|
-
}
|
|
4799
|
-
writeJson(res, 200, { ok: terminalSessions.close(body.id) });
|
|
4790
|
+
if (method !== "DELETE") {
|
|
4791
|
+
writeJson(res, 405, { error: `method not allowed: ${method}` });
|
|
4792
|
+
return;
|
|
4800
4793
|
}
|
|
4801
|
-
|
|
4802
|
-
|
|
4803
|
-
|
|
4804
|
-
|
|
4805
|
-
|
|
4806
|
-
|
|
4807
|
-
|
|
4808
|
-
|
|
4809
|
-
|
|
4810
|
-
|
|
4811
|
-
|
|
4812
|
-
|
|
4813
|
-
|
|
4814
|
-
|
|
4815
|
-
|
|
4816
|
-
|
|
4817
|
-
|
|
4818
|
-
|
|
4819
|
-
}
|
|
4820
|
-
|
|
4821
|
-
|
|
4822
|
-
|
|
4823
|
-
|
|
4824
|
-
|
|
4825
|
-
|
|
4826
|
-
|
|
4827
|
-
|
|
4828
|
-
|
|
4829
|
-
|
|
4830
|
-
|
|
4831
|
-
|
|
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
|
+
});
|
|
4832
4867
|
}
|
|
4833
|
-
|
|
4834
|
-
|
|
4835
|
-
|
|
4836
|
-
|
|
4837
|
-
|
|
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)" });
|
|
4838
4908
|
return;
|
|
4839
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") {
|
|
4840
4928
|
try {
|
|
4841
|
-
writeJson(res, 200, {
|
|
4929
|
+
writeJson(res, 200, { settings: viewSettings.get() });
|
|
4842
4930
|
} catch (error) {
|
|
4843
|
-
|
|
4844
|
-
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);
|
|
4845
4932
|
}
|
|
4933
|
+
return;
|
|
4846
4934
|
}
|
|
4847
|
-
|
|
4848
|
-
|
|
4849
|
-
|
|
4850
|
-
|
|
4851
|
-
|
|
4852
|
-
|
|
4853
|
-
|
|
4854
|
-
|
|
4855
|
-
|
|
4856
|
-
|
|
4857
|
-
|
|
4858
|
-
|
|
4859
|
-
|
|
4860
|
-
|
|
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";
|
|
4861
4961
|
if (alias === void 0 || alias === "") {
|
|
4862
4962
|
writeJson(res, 400, { error: "alias query parameter is required" });
|
|
4863
4963
|
return;
|
|
4864
4964
|
}
|
|
4865
|
-
|
|
4866
|
-
|
|
4867
|
-
|
|
4868
|
-
|
|
4869
|
-
|
|
4870
|
-
}
|
|
4871
|
-
if (result.timedOut) {
|
|
4872
|
-
writeJson(res, 200, {
|
|
4873
|
-
tool: "unknown",
|
|
4874
|
-
ports: [],
|
|
4875
|
-
error: `the remote probe did not finish within ${LISTEN_PORTS_TIMEOUT_MS / 1e3}s`
|
|
4876
|
-
});
|
|
4877
|
-
return;
|
|
4965
|
+
if (view === "probe") {
|
|
4966
|
+
try {
|
|
4967
|
+
writeJson(res, 200, await probe(pool, alias));
|
|
4968
|
+
} catch (error) {
|
|
4969
|
+
writeRouteError(res, error);
|
|
4878
4970
|
}
|
|
4879
|
-
|
|
4971
|
+
return;
|
|
4972
|
+
}
|
|
4973
|
+
if (view === "status") {
|
|
4974
|
+
try {
|
|
4880
4975
|
writeJson(res, 200, {
|
|
4881
|
-
|
|
4882
|
-
|
|
4883
|
-
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()
|
|
4884
4978
|
});
|
|
4885
|
-
|
|
4979
|
+
} catch (error) {
|
|
4980
|
+
if (isStoreError(error)) writeStoreError(res, error);
|
|
4981
|
+
else writeJson(res, 404, { error: error instanceof Error ? error.message : String(error) });
|
|
4886
4982
|
}
|
|
4887
|
-
|
|
4888
|
-
} catch (error) {
|
|
4889
|
-
writeJson(res, 502, { error: error instanceof Error ? error.message : String(error) });
|
|
4983
|
+
return;
|
|
4890
4984
|
}
|
|
4985
|
+
writeJson(res, 400, { error: `unknown view: ${view}` });
|
|
4986
|
+
return;
|
|
4891
4987
|
}
|
|
4892
|
-
|
|
4893
|
-
|
|
4894
|
-
|
|
4895
|
-
|
|
4896
|
-
|
|
4897
|
-
|
|
4898
|
-
|
|
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" });
|
|
4899
5096
|
return;
|
|
4900
5097
|
}
|
|
4901
|
-
|
|
4902
|
-
|
|
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 });
|
|
4903
5141
|
return;
|
|
4904
5142
|
}
|
|
4905
|
-
|
|
4906
|
-
|
|
4907
|
-
|
|
4908
|
-
|
|
4909
|
-
|
|
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
|
+
});
|
|
4910
5149
|
return;
|
|
4911
5150
|
}
|
|
4912
|
-
|
|
4913
|
-
|
|
4914
|
-
|
|
4915
|
-
|
|
4916
|
-
|
|
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;
|
|
4917
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) });
|
|
4918
5163
|
}
|
|
4919
5164
|
}
|
|
4920
|
-
|
|
4921
|
-
|
|
4922
|
-
|
|
4923
|
-
|
|
5165
|
+
},
|
|
5166
|
+
{
|
|
5167
|
+
kind: "exact",
|
|
5168
|
+
path: REMOTE_API.metrics,
|
|
5169
|
+
handler: async (req, res) => {
|
|
4924
5170
|
if (!isLoopbackRequest(req)) {
|
|
4925
|
-
|
|
4926
|
-
|
|
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}` });
|
|
4927
5176
|
return;
|
|
4928
5177
|
}
|
|
4929
5178
|
const url = new URL(req.url ?? "/", "http://localhost");
|
|
4930
5179
|
const alias = queryParam(url, "alias");
|
|
4931
|
-
const
|
|
4932
|
-
|
|
4933
|
-
|
|
4934
|
-
|
|
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 {}
|
|
4935
5236
|
return;
|
|
4936
5237
|
}
|
|
4937
|
-
|
|
4938
|
-
|
|
4939
|
-
|
|
4940
|
-
|
|
4941
|
-
|
|
4942
|
-
|
|
4943
|
-
|
|
4944
|
-
|
|
4945
|
-
|
|
4946
|
-
|
|
4947
|
-
ws.send(JSON.stringify({
|
|
4948
|
-
t: "error",
|
|
4949
|
-
message: "terminal session expired"
|
|
4950
|
-
}));
|
|
4951
|
-
} catch {}
|
|
4952
|
-
try {
|
|
4953
|
-
ws.close(1e3);
|
|
4954
|
-
} catch {}
|
|
4955
|
-
return;
|
|
4956
|
-
}
|
|
4957
|
-
terminalSessions.open(ws, alias, size.cols, size.rows).then(() => void 0, () => void 0);
|
|
4958
|
-
});
|
|
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);
|
|
4959
5248
|
}
|
|
4960
|
-
},
|
|
4961
|
-
|
|
5249
|
+
})),
|
|
5250
|
+
upgrade,
|
|
5251
|
+
terminalSessions,
|
|
5252
|
+
close
|
|
4962
5253
|
};
|
|
4963
5254
|
}
|
|
4964
5255
|
//#endregion
|