dsh-remote-vs 0.13.0 → 0.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +455 -443
- package/README.md +408 -396
- package/lib/client.js +8062 -2651
- package/lib/index.js +1058 -288
- package/lib/p2-router-fs.js +1 -1
- package/lib/p2-router-refs.js +89 -10
- package/lib/p2-router-spill.js +73 -1
- package/lib/p2-router-subprocess.js +18 -6
- package/lib/spill-retention-BwKEWrl5.js +115 -0
- package/lib/{ssh-proc-arI5lm4W.js → ssh-proc-VKWn_LZC.js} +17 -0
- package/lib/types/archive.d.ts +1 -1
- package/lib/types/changes/recorder.d.ts +34 -2
- package/lib/types/client/CodeEditor.d.ts +12 -0
- package/lib/types/client/ConnectionStatus.d.ts +17 -0
- package/lib/types/client/DirectoryFlow.d.ts +2 -0
- package/lib/types/client/DoctorPanel.d.ts +25 -0
- package/lib/types/client/QuickOpen.d.ts +17 -0
- package/lib/types/client/RemoteExplorer.d.ts +192 -1
- package/lib/types/client/RemoteRoot.d.ts +17 -0
- package/lib/types/client/SearchView.d.ts +55 -0
- package/lib/types/client/TerminalView.d.ts +38 -0
- package/lib/types/client/WorkspaceWizard.d.ts +17 -0
- package/lib/types/client/api.d.ts +54 -4
- package/lib/types/client/changes/undo-affordance.d.ts +16 -0
- package/lib/types/client/changes/undo-context.d.ts +14 -18
- package/lib/types/client/changes/undo-target.d.ts +78 -0
- package/lib/types/client/connection.d.ts +111 -0
- package/lib/types/client/doctor.d.ts +122 -0
- package/lib/types/client/download.d.ts +35 -5
- package/lib/types/client/downloads.d.ts +18 -1
- package/lib/types/client/file-open-probe.d.ts +64 -0
- package/lib/types/client/i18n.d.ts +1 -1
- package/lib/types/client/quickopen-model.d.ts +332 -0
- package/lib/types/client/scm/DiffView.d.ts +34 -0
- package/lib/types/client/scm/actions.d.ts +18 -0
- package/lib/types/client/scm/diff-model.d.ts +17 -1
- package/lib/types/client/scm/git-cache.d.ts +10 -2
- package/lib/types/client/scm/read-queue.d.ts +2 -1
- package/lib/types/client/scm/stage-queue.d.ts +13 -0
- package/lib/types/client/scm/status-map.d.ts +3 -1
- package/lib/types/client/store.d.ts +114 -5
- package/lib/types/client/vsc/FindWidget.d.ts +7 -0
- package/lib/types/client/vsc/save-shortcut.d.ts +57 -0
- package/lib/types/deploy.d.ts +44 -2
- package/lib/types/git-format.d.ts +15 -0
- package/lib/types/http.d.ts +44 -0
- package/lib/types/p2/file-refs.d.ts +39 -2
- package/lib/types/p2/helper-rpc.d.ts +26 -0
- package/lib/types/p2/router-spill.d.ts +19 -0
- package/lib/types/p2/router-subprocess.d.ts +2 -0
- package/lib/types/p2/spill-retention.d.ts +74 -0
- package/lib/types/p2/worlds.d.ts +11 -0
- package/lib/types/pool.d.ts +73 -11
- package/lib/types/protocol.d.ts +63 -0
- package/lib/types/routes.d.ts +18 -0
- package/lib/types/sftp.d.ts +114 -8
- package/lib/types/store.d.ts +34 -0
- package/lib/types/timeouts.d.ts +14 -0
- package/lib/{worlds-C6Xha1mQ.js → worlds-Be1YE2Ot.js} +727 -78
- package/package.json +1 -1
- package/src/archive.ts +41 -9
- package/src/changes/recorder.ts +227 -31
- package/src/changes/status.ts +37 -1
- package/src/client/CodeEditor.tsx +84 -2
- package/src/client/ConnectionStatus.tsx +130 -0
- package/src/client/DirectoryFlow.tsx +40 -5
- package/src/client/DoctorPanel.tsx +202 -0
- package/src/client/DownloadDock.tsx +13 -25
- package/src/client/EditorPane.tsx +291 -7
- package/src/client/HostsView.tsx +17 -8
- package/src/client/QuickOpen.tsx +371 -0
- package/src/client/RemoteDirectoryPicker.tsx +54 -3
- package/src/client/RemoteExplorer.tsx +879 -160
- package/src/client/RemoteRoot.tsx +178 -1
- package/src/client/SearchView.tsx +180 -6
- package/src/client/SettingsHosts.tsx +1 -1
- package/src/client/TerminalView.tsx +174 -9
- package/src/client/WorkspaceWizard.tsx +110 -16
- package/src/client/WorkspacesView.tsx +22 -6
- package/src/client/api.ts +197 -11
- package/src/client/changes/ChangesDock.tsx +35 -8
- package/src/client/changes/undo-affordance.ts +33 -0
- package/src/client/changes/undo-context.ts +14 -18
- package/src/client/changes/undo-target.ts +139 -0
- package/src/client/connection.ts +264 -0
- package/src/client/doctor.ts +441 -0
- package/src/client/download.ts +148 -12
- package/src/client/downloads.ts +43 -2
- package/src/client/file-open-probe.ts +83 -0
- package/src/client/i18n.ts +244 -0
- package/src/client/index.tsx +5 -2
- package/src/client/quickopen-model.ts +778 -0
- package/src/client/remote-file-open.ts +21 -33
- package/src/client/scm/DiffView.tsx +116 -6
- package/src/client/scm/ScmPanel.tsx +19 -0
- package/src/client/scm/actions.ts +34 -2
- package/src/client/scm/diff-model.ts +33 -37
- package/src/client/scm/git-cache.ts +61 -4
- package/src/client/scm/read-queue.ts +33 -2
- package/src/client/scm/stage-queue.ts +72 -1
- package/src/client/scm/status-map.ts +22 -1
- package/src/client/store.tsx +704 -21
- package/src/client/vsc/FindWidget.tsx +20 -0
- package/src/client/vsc/kit.tsx +115 -6
- package/src/client/vsc/reveal-line.ts +33 -14
- package/src/client/vsc/save-shortcut.ts +131 -0
- package/src/client/vsc/vsc.css +419 -0
- package/src/deploy.ts +185 -33
- package/src/git-format.ts +136 -22
- package/src/git-ops.ts +14 -6
- package/src/http.ts +80 -12
- package/src/importers.ts +80 -21
- package/src/index.ts +21 -1
- package/src/p2/file-refs.ts +112 -10
- package/src/p2/helper-rpc.ts +73 -36
- package/src/p2/router-spill.ts +96 -1
- package/src/p2/router-subprocess.ts +18 -5
- package/src/p2/spill-retention.ts +134 -0
- package/src/p2/ssh-proc.ts +22 -0
- package/src/p2/worlds.ts +24 -0
- package/src/pool.ts +329 -47
- package/src/protocol.ts +66 -0
- package/src/routes.ts +251 -43
- package/src/sftp.ts +486 -26
- package/src/store.ts +207 -85
- package/src/terminal.ts +38 -2
- package/src/timeouts.ts +14 -0
- package/src/tools.ts +14 -5
package/src/routes.ts
CHANGED
|
@@ -10,13 +10,13 @@ import { fileURLToPath } from 'node:url'
|
|
|
10
10
|
import { WebSocketServer } from 'ws'
|
|
11
11
|
import type { Context } from '@deepseek-ai/cordis'
|
|
12
12
|
import type { WebRoute, WebUpgradeRoute } from '@deepseek-ai/dsh-host-webserver'
|
|
13
|
-
import {
|
|
13
|
+
import { readJsonBodyResult, writeJson, writeRouteError } from './http.ts'
|
|
14
14
|
import { isLoopbackRequest } from './loopback.ts'
|
|
15
15
|
import { REMOTE_API, MAX_SELECTION_DOWNLOAD, selectionDownloadName, type ExecStreamEvent, type ImportReport, type RemoteTimeouts } from './protocol.ts'
|
|
16
16
|
import type { HostPayload, RemoteStore } from './store.ts'
|
|
17
17
|
import type { RemotePool } from './pool.ts'
|
|
18
18
|
import type { RemoteWorlds } from './p2/worlds.ts'
|
|
19
|
-
import { RemoteFiles, attachmentHeader } from './sftp.ts'
|
|
19
|
+
import { RemoteFiles, WriteConflictError, attachmentHeader } from './sftp.ts'
|
|
20
20
|
import { directoryArchiveCommand, directoryArchiveName, selectionArchiveCommand } from './archive.ts'
|
|
21
21
|
import { bootstrap } from './keygen.ts'
|
|
22
22
|
import { helperAvailability, probe, pushPipeline } from './deploy.ts'
|
|
@@ -36,6 +36,109 @@ function queryParam(url: URL, name: string): string | undefined {
|
|
|
36
36
|
return value === null ? undefined : value
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
+
/**
|
|
40
|
+
* A caller mistake the route can name: well-formed HTTP, unusable payload.
|
|
41
|
+
*
|
|
42
|
+
* `/files` used to throw plain Errors for missing fields and map EVERY failure
|
|
43
|
+
* to 502, so "your request had no path" was reported as "the remote host
|
|
44
|
+
* failed". The class keeps that distinction at the HTTP status.
|
|
45
|
+
*/
|
|
46
|
+
class BadRequestError extends Error {
|
|
47
|
+
constructor(message: string) {
|
|
48
|
+
super(message)
|
|
49
|
+
this.name = 'BadRequestError'
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Reject an oversized JSON body BEFORE reading it.
|
|
55
|
+
*
|
|
56
|
+
* A known Content-Length lets the route answer 413 without draining the body at
|
|
57
|
+
* all (the GUI always sends one). A chunked body without the header is caught by
|
|
58
|
+
* `readJsonBodyResult` after the read and answered with the same 413, so this
|
|
59
|
+
* preflight is only the cheap path — not the only one.
|
|
60
|
+
*/
|
|
61
|
+
function bodyTooLarge(req: IncomingMessage, maxBytes: number): boolean {
|
|
62
|
+
const raw = req.headers['content-length']
|
|
63
|
+
if (typeof raw !== 'string') return false
|
|
64
|
+
const length = Number(raw)
|
|
65
|
+
return Number.isFinite(length) && length > maxBytes
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Answer 413 and drain the rest of the body so the socket is not reset mid-response. */
|
|
69
|
+
function writePayloadTooLarge(req: IncomingMessage, res: ServerResponse, maxBytes: number): void {
|
|
70
|
+
writeJson(res, 413, { error: `request body exceeds ${maxBytes} bytes` }, { connection: 'close' })
|
|
71
|
+
req.resume()
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Read one JSON request body, answering the outcomes every route shares the same
|
|
76
|
+
* way: an oversized body is a 413 (the connection closes), malformed JSON is a
|
|
77
|
+
* 400. `{ ok: false }` means a response was already written — the handler returns.
|
|
78
|
+
*
|
|
79
|
+
* An EMPTY body still arrives as `null`, so each route keeps its own "missing
|
|
80
|
+
* object" answer, and `objectOnly` is passed through unchanged.
|
|
81
|
+
*/
|
|
82
|
+
async function readBodyOrError<T>(
|
|
83
|
+
req: IncomingMessage,
|
|
84
|
+
res: ServerResponse,
|
|
85
|
+
opts: { maxBytes?: number; objectOnly?: boolean } = {},
|
|
86
|
+
): Promise<{ ok: true; body: T | null } | { ok: false }> {
|
|
87
|
+
const result = await readJsonBodyResult(req, opts)
|
|
88
|
+
if (result.kind === 'too-large') {
|
|
89
|
+
writeJson(res, 413, { error: `request body exceeds ${result.maxBytes} bytes` }, { connection: 'close' })
|
|
90
|
+
return { ok: false }
|
|
91
|
+
}
|
|
92
|
+
if (result.kind === 'invalid') {
|
|
93
|
+
writeJson(res, 400, { error: 'invalid JSON body' })
|
|
94
|
+
return { ok: false }
|
|
95
|
+
}
|
|
96
|
+
return { ok: true, body: result.kind === 'ok' ? (result.value as T) : null }
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Body cap of the /files route (base64/UTF-8 file content). */
|
|
100
|
+
const FILES_BODY_LIMIT = 10 * 1024 * 1024
|
|
101
|
+
|
|
102
|
+
/** Body cap of the ssh_config import route (pasted config text). */
|
|
103
|
+
const SSH_CONFIG_BODY_LIMIT = 1024 * 1024
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Total budget for one archive stream (directory or multi-selection download).
|
|
107
|
+
*
|
|
108
|
+
* A hung remote `tar` (a stalled mount, a failing disk, a huge tree) otherwise
|
|
109
|
+
* pins the Host channel until the CLIENT disconnects, and `channel.close()`
|
|
110
|
+
* alone cannot stop a tar that is blocked on a read. Enforced on the remote side
|
|
111
|
+
* so it survives a dead HTTP socket too.
|
|
112
|
+
*/
|
|
113
|
+
const ARCHIVE_TIMEOUT_SECONDS = 600
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Wrap one archive command in a remote-side deadline.
|
|
117
|
+
*
|
|
118
|
+
* The command is a single `tar` invocation with already-quoted operands
|
|
119
|
+
* (`archive.ts` builders), so prefixing is syntactically safe and makes tar the
|
|
120
|
+
* direct child of `timeout` — TERM (then KILL after 3 s) reaches tar itself, no
|
|
121
|
+
* process-group id is needed (2026-10 audit, A16-1). Hosts without GNU `timeout`
|
|
122
|
+
* (BSD/macOS) fall back to today's behaviour instead of failing the download.
|
|
123
|
+
*/
|
|
124
|
+
export function archiveCommandWithDeadline(command: string): string {
|
|
125
|
+
return `if command -v timeout >/dev/null 2>&1; then timeout -k 3 ${ARCHIVE_TIMEOUT_SECONDS} ${command}; else ${command}; fi`
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Error for a failed archive stream.
|
|
130
|
+
*
|
|
131
|
+
* GNU `timeout` reports 124 when TERM sufficed and 137 when KILL was needed;
|
|
132
|
+
* both mean "the remote budget ran out", which is more actionable than
|
|
133
|
+
* `tar failed (124): no stderr`.
|
|
134
|
+
*/
|
|
135
|
+
export function archiveFailureMessage(exitCode: number | null | undefined, stderr: string): Error {
|
|
136
|
+
if (exitCode === 124 || exitCode === 137) return new Error(`archive timed out after ${ARCHIVE_TIMEOUT_SECONDS}s`)
|
|
137
|
+
const detail = stderr.trim() === '' ? 'no stderr' : stderr.trim().slice(0, 400)
|
|
138
|
+
const code = exitCode === null || exitCode === undefined ? 'signal' : exitCode
|
|
139
|
+
return new Error(`tar failed (${code}): ${detail}`)
|
|
140
|
+
}
|
|
141
|
+
|
|
39
142
|
/** This plugin's version from the shipped package.json (Settings display). */
|
|
40
143
|
let cachedVersion: string | undefined
|
|
41
144
|
|
|
@@ -156,9 +259,9 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
156
259
|
const streamArchive = async (alias: string, command: string, res: ServerResponse, downloadName: string): Promise<void> => {
|
|
157
260
|
let channel: Awaited<ReturnType<typeof pool.execChannel>>
|
|
158
261
|
try {
|
|
159
|
-
channel = await pool.execChannel(alias, command)
|
|
262
|
+
channel = await pool.execChannel(alias, archiveCommandWithDeadline(command))
|
|
160
263
|
} catch (error) {
|
|
161
|
-
|
|
264
|
+
writeRouteError(res, error)
|
|
162
265
|
return
|
|
163
266
|
}
|
|
164
267
|
res.writeHead(200, {
|
|
@@ -167,6 +270,9 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
167
270
|
'cache-control': 'no-store',
|
|
168
271
|
'x-content-type-options': 'nosniff',
|
|
169
272
|
})
|
|
273
|
+
// Send the headers now so the browser's save/stream starts immediately instead
|
|
274
|
+
// of waiting for the remote tar to produce its first byte (A16-2).
|
|
275
|
+
res.flushHeaders()
|
|
170
276
|
let closed = false
|
|
171
277
|
/** `undefined` until the remote process reports one. */
|
|
172
278
|
let exitCode: number | null | undefined
|
|
@@ -220,8 +326,7 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
220
326
|
channel.on('close', () => {
|
|
221
327
|
if (closed) return
|
|
222
328
|
if (exitCode !== 0) {
|
|
223
|
-
|
|
224
|
-
stop(new Error(`tar failed (${exitCode === null ? 'signal' : exitCode}): ${detail}`))
|
|
329
|
+
stop(archiveFailureMessage(exitCode, stderr))
|
|
225
330
|
return
|
|
226
331
|
}
|
|
227
332
|
stop()
|
|
@@ -265,7 +370,9 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
265
370
|
return
|
|
266
371
|
}
|
|
267
372
|
if (method === 'POST') {
|
|
268
|
-
const
|
|
373
|
+
const read = await readBodyOrError<Record<string, unknown>>(req, res, { objectOnly: true })
|
|
374
|
+
if (!read.ok) return
|
|
375
|
+
const body = read.body
|
|
269
376
|
if (body === null) {
|
|
270
377
|
writeJson(res, 400, { error: 'invalid JSON body' })
|
|
271
378
|
return
|
|
@@ -288,7 +395,7 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
288
395
|
return
|
|
289
396
|
}
|
|
290
397
|
if (method === 'DELETE') {
|
|
291
|
-
pool.
|
|
398
|
+
pool.forget(alias)
|
|
292
399
|
void worlds.dropHost(alias).catch(() => undefined)
|
|
293
400
|
if (!store.remove(alias)) {
|
|
294
401
|
writeJson(res, 404, { error: `unknown host alias: ${alias}` })
|
|
@@ -298,13 +405,15 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
298
405
|
writeJson(res, 200, { ok: true })
|
|
299
406
|
return
|
|
300
407
|
}
|
|
301
|
-
const
|
|
408
|
+
const read = await readBodyOrError<Record<string, unknown>>(req, res, { objectOnly: true })
|
|
409
|
+
if (!read.ok) return
|
|
410
|
+
const body = read.body
|
|
302
411
|
if (body === null) {
|
|
303
412
|
writeJson(res, 400, { error: 'invalid JSON body' })
|
|
304
413
|
return
|
|
305
414
|
}
|
|
306
415
|
try {
|
|
307
|
-
pool.
|
|
416
|
+
pool.forget(alias)
|
|
308
417
|
void worlds.dropHost(alias).catch(() => undefined)
|
|
309
418
|
const entry = store.update(alias, body as Partial<HostPayload>)
|
|
310
419
|
writeJson(res, 200, { host: store.summarize(entry) })
|
|
@@ -314,6 +423,25 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
314
423
|
}
|
|
315
424
|
},
|
|
316
425
|
},
|
|
426
|
+
// ----------------------------------------------------------- status
|
|
427
|
+
// Connection health for the browser's status chip. Read-only and dial-free:
|
|
428
|
+
// it reports what the pool already knows, so polling it never opens a
|
|
429
|
+
// connection or wakes a sleeping host.
|
|
430
|
+
{
|
|
431
|
+
kind: 'exact',
|
|
432
|
+
path: REMOTE_API.status,
|
|
433
|
+
handler: (req, res) => {
|
|
434
|
+
if (!isLoopbackRequest(req)) {
|
|
435
|
+
writeJson(res, 403, { error: 'forbidden: loopback-only' })
|
|
436
|
+
return
|
|
437
|
+
}
|
|
438
|
+
if (req.method !== 'GET') {
|
|
439
|
+
writeJson(res, 405, { error: `method not allowed: ${req.method}` })
|
|
440
|
+
return
|
|
441
|
+
}
|
|
442
|
+
writeJson(res, 200, { connections: pool.status() })
|
|
443
|
+
},
|
|
444
|
+
},
|
|
317
445
|
// ------------------------------------------------------------- test
|
|
318
446
|
{
|
|
319
447
|
kind: 'exact',
|
|
@@ -327,7 +455,9 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
327
455
|
writeJson(res, 405, { error: `method not allowed: ${req.method}` })
|
|
328
456
|
return
|
|
329
457
|
}
|
|
330
|
-
const
|
|
458
|
+
const read = await readBodyOrError<{ alias?: unknown }>(req, res, { objectOnly: true })
|
|
459
|
+
if (!read.ok) return
|
|
460
|
+
const body = read.body
|
|
331
461
|
if (body === null || typeof body.alias !== 'string') {
|
|
332
462
|
writeJson(res, 400, { error: 'alias is required' })
|
|
333
463
|
return
|
|
@@ -348,7 +478,9 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
348
478
|
writeJson(res, 405, { error: `method not allowed: ${req.method}` })
|
|
349
479
|
return
|
|
350
480
|
}
|
|
351
|
-
const
|
|
481
|
+
const read = await readBodyOrError<{ alias?: unknown }>(req, res, { objectOnly: true })
|
|
482
|
+
if (!read.ok) return
|
|
483
|
+
const body = read.body
|
|
352
484
|
if (body === null || typeof body.alias !== 'string') {
|
|
353
485
|
writeJson(res, 400, { error: 'alias is required' })
|
|
354
486
|
return
|
|
@@ -357,7 +489,10 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
357
489
|
const result = await bootstrap(pool, store, body.alias)
|
|
358
490
|
writeJson(res, 200, result)
|
|
359
491
|
} catch (error) {
|
|
360
|
-
|
|
492
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
493
|
+
// An unknown alias is the caller's mistake; everything else failed while
|
|
494
|
+
// generating the keypair or pushing it to the host.
|
|
495
|
+
writeJson(res, message.startsWith('unknown host alias') ? 404 : 502, { error: message })
|
|
361
496
|
}
|
|
362
497
|
},
|
|
363
498
|
},
|
|
@@ -374,11 +509,13 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
374
509
|
writeJson(res, 405, { error: `method not allowed: ${req.method}` })
|
|
375
510
|
return
|
|
376
511
|
}
|
|
377
|
-
const
|
|
512
|
+
const read = await readBodyOrError<{
|
|
378
513
|
alias?: unknown
|
|
379
514
|
command?: unknown
|
|
380
515
|
timeoutMs?: unknown
|
|
381
|
-
}
|
|
516
|
+
}>(req, res, { objectOnly: true })
|
|
517
|
+
if (!read.ok) return
|
|
518
|
+
const body = read.body
|
|
382
519
|
if (body === null || typeof body.alias !== 'string' || typeof body.command !== 'string' || body.command === '') {
|
|
383
520
|
writeJson(res, 400, { error: 'alias and a non-empty command are required' })
|
|
384
521
|
return
|
|
@@ -400,11 +537,13 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
400
537
|
writeJson(res, 405, { error: `method not allowed: ${req.method}` })
|
|
401
538
|
return
|
|
402
539
|
}
|
|
403
|
-
const
|
|
540
|
+
const read = await readBodyOrError<{
|
|
404
541
|
alias?: unknown
|
|
405
542
|
command?: unknown
|
|
406
543
|
timeoutMs?: unknown
|
|
407
|
-
}
|
|
544
|
+
}>(req, res, { objectOnly: true })
|
|
545
|
+
if (!read.ok) return
|
|
546
|
+
const body = read.body
|
|
408
547
|
if (body === null || typeof body.alias !== 'string' || typeof body.command !== 'string' || body.command === '') {
|
|
409
548
|
writeJson(res, 400, { error: 'alias and a non-empty command are required' })
|
|
410
549
|
return
|
|
@@ -417,6 +556,11 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
417
556
|
// A reverse proxy must not buffer an event stream into uselessness.
|
|
418
557
|
'X-Accel-Buffering': 'no',
|
|
419
558
|
})
|
|
559
|
+
// Headers must leave NOW: the client `fetch()` only resolves once they
|
|
560
|
+
// arrive, and a silent command (a push waiting on the far end) would
|
|
561
|
+
// otherwise keep the request unresolved until the first event or the
|
|
562
|
+
// 15 s heartbeat (A16-2).
|
|
563
|
+
res.flushHeaders()
|
|
420
564
|
/** No more events to the client (finished, or the socket broke). */
|
|
421
565
|
let outputClosed = false
|
|
422
566
|
/** `res.end` already called. */
|
|
@@ -534,7 +678,13 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
534
678
|
writeJson(res, 405, { error: `method not allowed: ${req.method}` })
|
|
535
679
|
return
|
|
536
680
|
}
|
|
537
|
-
|
|
681
|
+
if (bodyTooLarge(req, FILES_BODY_LIMIT)) {
|
|
682
|
+
writePayloadTooLarge(req, res, FILES_BODY_LIMIT)
|
|
683
|
+
return
|
|
684
|
+
}
|
|
685
|
+
const read = await readBodyOrError<Record<string, unknown>>(req, res, { objectOnly: true, maxBytes: FILES_BODY_LIMIT })
|
|
686
|
+
if (!read.ok) return
|
|
687
|
+
const body = read.body
|
|
538
688
|
if (body === null || typeof body.op !== 'string' || typeof body.alias !== 'string') {
|
|
539
689
|
writeJson(res, 400, { error: 'op and alias are required' })
|
|
540
690
|
return
|
|
@@ -543,40 +693,66 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
543
693
|
try {
|
|
544
694
|
switch (body.op) {
|
|
545
695
|
case 'list': {
|
|
546
|
-
if (typeof body.path !== 'string') throw new
|
|
696
|
+
if (typeof body.path !== 'string') throw new BadRequestError('path is required')
|
|
547
697
|
writeJson(res, 200, { entries: await files.list(alias, body.path) })
|
|
548
698
|
return
|
|
549
699
|
}
|
|
550
700
|
case 'stat': {
|
|
551
|
-
if (typeof body.path !== 'string') throw new
|
|
701
|
+
if (typeof body.path !== 'string') throw new BadRequestError('path is required')
|
|
552
702
|
writeJson(res, 200, { entry: await files.stat(alias, body.path) })
|
|
553
703
|
return
|
|
554
704
|
}
|
|
555
705
|
case 'read': {
|
|
556
|
-
if (typeof body.path !== 'string') throw new
|
|
706
|
+
if (typeof body.path !== 'string') throw new BadRequestError('path is required')
|
|
557
707
|
writeJson(res, 200, await files.read(alias, body.path))
|
|
558
708
|
return
|
|
559
709
|
}
|
|
560
710
|
case 'write': {
|
|
561
|
-
if (typeof body.path !== 'string' || typeof body.content !== 'string') throw new
|
|
711
|
+
if (typeof body.path !== 'string' || typeof body.content !== 'string') throw new BadRequestError('path and content are required')
|
|
562
712
|
const encoding = body.encoding === 'base64' ? 'base64' : 'utf8'
|
|
563
|
-
|
|
713
|
+
/**
|
|
714
|
+
* Optional optimistic lock. `expectMtime` is the version the client
|
|
715
|
+
* loaded; when present the Host refuses a file that changed since.
|
|
716
|
+
* Requests without it (helpers, deploy, the agent's write tool)
|
|
717
|
+
* keep the old unconditional semantics — the guard protects a
|
|
718
|
+
* human's buffer, it must not make programmatic writes fail.
|
|
719
|
+
*/
|
|
720
|
+
const expectMtime = body.expectMtime
|
|
721
|
+
const expectSize = body.expectSize
|
|
722
|
+
/**
|
|
723
|
+
* A malformed baseline is a CALLER mistake, not "no baseline".
|
|
724
|
+
*
|
|
725
|
+
* Silently dropping it would turn `{expectMtime: "123"}` (a client
|
|
726
|
+
* bug, a serializer change) into an UNGUARDED write — the exact
|
|
727
|
+
* overwrite the caller was asking to be protected from, reported as
|
|
728
|
+
* a success.
|
|
729
|
+
*/
|
|
730
|
+
if (expectMtime !== undefined && (typeof expectMtime !== 'number' || !Number.isFinite(expectMtime))) {
|
|
731
|
+
throw new BadRequestError('expectMtime must be a finite number')
|
|
732
|
+
}
|
|
733
|
+
if (expectSize !== undefined && (typeof expectSize !== 'number' || !Number.isFinite(expectSize))) {
|
|
734
|
+
throw new BadRequestError('expectSize must be a finite number')
|
|
735
|
+
}
|
|
736
|
+
const guard = typeof expectMtime === 'number'
|
|
737
|
+
? { expectMtime, ...(typeof expectSize === 'number' ? { expectSize } : {}) }
|
|
738
|
+
: undefined
|
|
739
|
+
writeJson(res, 200, await files.write(alias, body.path, body.content, encoding, guard))
|
|
564
740
|
return
|
|
565
741
|
}
|
|
566
742
|
case 'mkdir': {
|
|
567
|
-
if (typeof body.path !== 'string') throw new
|
|
743
|
+
if (typeof body.path !== 'string') throw new BadRequestError('path is required')
|
|
568
744
|
await files.mkdir(alias, body.path)
|
|
569
745
|
writeJson(res, 200, { ok: true })
|
|
570
746
|
return
|
|
571
747
|
}
|
|
572
748
|
case 'rm': {
|
|
573
|
-
if (typeof body.path !== 'string') throw new
|
|
749
|
+
if (typeof body.path !== 'string') throw new BadRequestError('path is required')
|
|
574
750
|
await files.remove(alias, body.path, body.recursive === true)
|
|
575
751
|
writeJson(res, 200, { ok: true })
|
|
576
752
|
return
|
|
577
753
|
}
|
|
578
754
|
case 'rename': {
|
|
579
|
-
if (typeof body.from !== 'string' || typeof body.to !== 'string') throw new
|
|
755
|
+
if (typeof body.from !== 'string' || typeof body.to !== 'string') throw new BadRequestError('from and to are required')
|
|
580
756
|
await files.rename(alias, body.from, body.to)
|
|
581
757
|
writeJson(res, 200, { ok: true })
|
|
582
758
|
return
|
|
@@ -585,7 +761,21 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
585
761
|
writeJson(res, 400, { error: `unknown op: ${body.op}` })
|
|
586
762
|
}
|
|
587
763
|
} catch (error) {
|
|
588
|
-
|
|
764
|
+
/**
|
|
765
|
+
* A save the remote side refused because the file moved on.
|
|
766
|
+
*
|
|
767
|
+
* 409, not 502: the operation did not fail, it was DECLINED — and the
|
|
768
|
+
* client needs the current version to offer overwrite / reload / see
|
|
769
|
+
* the difference. Collapsing this into the generic remote-error path
|
|
770
|
+
* is how a silent overwrite becomes a red toast nobody can act on.
|
|
771
|
+
*/
|
|
772
|
+
if (error instanceof WriteConflictError) {
|
|
773
|
+
writeJson(res, 409, { error: error.message, code: 'conflict', conflict: error.conflict })
|
|
774
|
+
return
|
|
775
|
+
}
|
|
776
|
+
// Caller mistakes are 400; only the remote side deserves 502.
|
|
777
|
+
const status = error instanceof BadRequestError ? 400 : 502
|
|
778
|
+
writeJson(res, status, { error: error instanceof Error ? error.message : String(error) })
|
|
589
779
|
}
|
|
590
780
|
},
|
|
591
781
|
},
|
|
@@ -634,7 +824,7 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
634
824
|
const written = await files.upload(alias, path, req, undefined, overwrite)
|
|
635
825
|
writeJson(res, 200, { ok: true, size: written.size })
|
|
636
826
|
} catch (error) {
|
|
637
|
-
|
|
827
|
+
writeRouteError(res, error)
|
|
638
828
|
}
|
|
639
829
|
},
|
|
640
830
|
},
|
|
@@ -671,12 +861,11 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
671
861
|
try {
|
|
672
862
|
await files.download(alias, path, res)
|
|
673
863
|
} catch (error) {
|
|
674
|
-
const message = error instanceof Error ? error.message : String(error)
|
|
675
864
|
// The stat/stream start can still fail before any byte goes out, and
|
|
676
865
|
// then the client expects its JSON error; once the body started (or
|
|
677
866
|
// the connection is already gone) the only honest answer is closing
|
|
678
867
|
// the connection.
|
|
679
|
-
if (!res.headersSent && !res.destroyed)
|
|
868
|
+
if (!res.headersSent && !res.destroyed) writeRouteError(res, error)
|
|
680
869
|
else res.destroy(error instanceof Error ? error : undefined)
|
|
681
870
|
}
|
|
682
871
|
},
|
|
@@ -719,7 +908,7 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
719
908
|
}
|
|
720
909
|
command = directoryArchiveCommand(path)
|
|
721
910
|
} catch (error) {
|
|
722
|
-
|
|
911
|
+
writeRouteError(res, error)
|
|
723
912
|
return
|
|
724
913
|
}
|
|
725
914
|
await streamArchive(alias, command, res, directoryArchiveName(path))
|
|
@@ -766,7 +955,7 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
766
955
|
for (const path of paths) await files.stat(alias, path)
|
|
767
956
|
command = selectionArchiveCommand(paths)
|
|
768
957
|
} catch (error) {
|
|
769
|
-
|
|
958
|
+
writeRouteError(res, error)
|
|
770
959
|
return
|
|
771
960
|
}
|
|
772
961
|
await streamArchive(alias, command, res, selectionDownloadName(paths.length))
|
|
@@ -805,7 +994,9 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
805
994
|
return
|
|
806
995
|
}
|
|
807
996
|
if (method === 'POST') {
|
|
808
|
-
const
|
|
997
|
+
const read = await readBodyOrError<Record<string, unknown>>(req, res, { objectOnly: true })
|
|
998
|
+
if (!read.ok) return
|
|
999
|
+
const body = read.body
|
|
809
1000
|
if (body === null) {
|
|
810
1001
|
writeJson(res, 400, { error: 'invalid JSON body' })
|
|
811
1002
|
return
|
|
@@ -871,7 +1062,9 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
871
1062
|
writeJson(res, 405, { error: `method not allowed: ${req.method ?? 'GET'}` })
|
|
872
1063
|
return
|
|
873
1064
|
}
|
|
874
|
-
const
|
|
1065
|
+
const read = await readBodyOrError<{ sessionId?: unknown }>(req, res, { objectOnly: true })
|
|
1066
|
+
if (!read.ok) return
|
|
1067
|
+
const body = read.body
|
|
875
1068
|
const sessionId = typeof body?.sessionId === 'string' ? body.sessionId : ''
|
|
876
1069
|
if (sessionId === '') {
|
|
877
1070
|
writeJson(res, 400, { error: 'sessionId must be a non-empty string' })
|
|
@@ -898,7 +1091,9 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
898
1091
|
writeJson(res, 405, { error: `method not allowed: ${req.method ?? 'GET'}` })
|
|
899
1092
|
return
|
|
900
1093
|
}
|
|
901
|
-
const
|
|
1094
|
+
const read = await readBodyOrError<Record<string, unknown>>(req, res, { objectOnly: true })
|
|
1095
|
+
if (!read.ok) return
|
|
1096
|
+
const body = read.body
|
|
902
1097
|
const sessionId = typeof body?.sessionId === 'string' ? body.sessionId : ''
|
|
903
1098
|
const op = typeof body?.op === 'string' ? body.op : ''
|
|
904
1099
|
if (sessionId === '' || (op !== 'list' && op !== 'diff')) {
|
|
@@ -942,7 +1137,8 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
942
1137
|
try {
|
|
943
1138
|
writeJson(res, 200, { diff: await ledger.diff(sessionId, path) })
|
|
944
1139
|
} catch (error) {
|
|
945
|
-
|
|
1140
|
+
// Reading the diff is a remote (git) round trip: it failed upstream.
|
|
1141
|
+
writeRouteError(res, error)
|
|
946
1142
|
}
|
|
947
1143
|
},
|
|
948
1144
|
},
|
|
@@ -964,7 +1160,9 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
964
1160
|
writeJson(res, 405, { error: `method not allowed: ${req.method ?? 'GET'}` })
|
|
965
1161
|
return
|
|
966
1162
|
}
|
|
967
|
-
const
|
|
1163
|
+
const read = await readBodyOrError<Record<string, unknown>>(req, res, { objectOnly: true })
|
|
1164
|
+
if (!read.ok) return
|
|
1165
|
+
const body = read.body
|
|
968
1166
|
const mode = body?.mode
|
|
969
1167
|
if (mode !== 'off' && mode !== 'shadow' && mode !== 'enforce') {
|
|
970
1168
|
writeJson(res, 400, { error: `mode must be one of off/shadow/enforce (got ${JSON.stringify(mode) ?? 'missing'})` })
|
|
@@ -982,7 +1180,7 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
982
1180
|
await settings.update(ns, { routingMode: mode })
|
|
983
1181
|
writeJson(res, 200, { mode: worlds.mode })
|
|
984
1182
|
} catch (error) {
|
|
985
|
-
|
|
1183
|
+
writeRouteError(res, error)
|
|
986
1184
|
}
|
|
987
1185
|
},
|
|
988
1186
|
},
|
|
@@ -1008,7 +1206,7 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
1008
1206
|
try {
|
|
1009
1207
|
writeJson(res, 200, await probe(pool, alias))
|
|
1010
1208
|
} catch (error) {
|
|
1011
|
-
|
|
1209
|
+
writeRouteError(res, error)
|
|
1012
1210
|
}
|
|
1013
1211
|
return
|
|
1014
1212
|
}
|
|
@@ -1028,7 +1226,9 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
1028
1226
|
writeJson(res, 405, { error: `method not allowed: ${method}` })
|
|
1029
1227
|
return
|
|
1030
1228
|
}
|
|
1031
|
-
const
|
|
1229
|
+
const read = await readBodyOrError<{ alias?: unknown; version?: unknown }>(req, res, { objectOnly: true })
|
|
1230
|
+
if (!read.ok) return
|
|
1231
|
+
const body = read.body
|
|
1032
1232
|
if (body === null || typeof body.alias !== 'string') {
|
|
1033
1233
|
writeJson(res, 400, { error: 'alias is required' })
|
|
1034
1234
|
return
|
|
@@ -1036,7 +1236,7 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
1036
1236
|
try {
|
|
1037
1237
|
writeJson(res, 200, await pushPipeline(pool, store, body.alias, typeof body.version === 'string' ? { version: body.version } : {}))
|
|
1038
1238
|
} catch (error) {
|
|
1039
|
-
|
|
1239
|
+
writeRouteError(res, error)
|
|
1040
1240
|
}
|
|
1041
1241
|
},
|
|
1042
1242
|
},
|
|
@@ -1053,7 +1253,13 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
1053
1253
|
writeJson(res, 405, { error: `method not allowed: ${req.method}` })
|
|
1054
1254
|
return
|
|
1055
1255
|
}
|
|
1056
|
-
|
|
1256
|
+
if (bodyTooLarge(req, SSH_CONFIG_BODY_LIMIT)) {
|
|
1257
|
+
writePayloadTooLarge(req, res, SSH_CONFIG_BODY_LIMIT)
|
|
1258
|
+
return
|
|
1259
|
+
}
|
|
1260
|
+
const read = await readBodyOrError<{ text?: unknown }>(req, res, { maxBytes: SSH_CONFIG_BODY_LIMIT })
|
|
1261
|
+
if (!read.ok) return
|
|
1262
|
+
const body = read.body
|
|
1057
1263
|
try {
|
|
1058
1264
|
const text = body !== null && typeof body.text === 'string' ? body.text : readDefaultSshConfig()
|
|
1059
1265
|
writeJson(res, 200, applyImport(parseSshConfig(text)))
|
|
@@ -1095,7 +1301,9 @@ export function makeRoutes(deps: RemoteRoutesDeps): { routes: WebRoute[]; upgrad
|
|
|
1095
1301
|
writeJson(res, 405, { error: `method not allowed: ${req.method}` })
|
|
1096
1302
|
return
|
|
1097
1303
|
}
|
|
1098
|
-
const
|
|
1304
|
+
const read = await readBodyOrError<{ id?: unknown }>(req, res, { objectOnly: true })
|
|
1305
|
+
if (!read.ok) return
|
|
1306
|
+
const body = read.body
|
|
1099
1307
|
if (body === null || typeof body.id !== 'string' || body.id === '') {
|
|
1100
1308
|
writeJson(res, 400, { error: 'id is required' })
|
|
1101
1309
|
return
|