dsh-remote-go 0.3.0 → 0.3.2

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.
Files changed (86) hide show
  1. package/README.md +1 -0
  2. package/dist/client.js +1348 -2095
  3. package/dist/src/admission.js +3 -28
  4. package/dist/src/api.js +37 -99
  5. package/dist/src/atomic-json.js +32 -66
  6. package/dist/src/bootstrap.js +17 -27
  7. package/dist/src/connect/attach.js +3 -25
  8. package/dist/src/connect/bootstrap.js +2 -8
  9. package/dist/src/connect/failures.js +3 -18
  10. package/dist/src/connect/guards.js +13 -8
  11. package/dist/src/connect/launch.js +11 -21
  12. package/dist/src/connect/links.js +11 -27
  13. package/dist/src/connect/ownership.js +33 -114
  14. package/dist/src/connect/pipeline.js +62 -159
  15. package/dist/src/connect/reattach.js +26 -44
  16. package/dist/src/connect/registry.js +0 -1
  17. package/dist/src/connect/repair.js +8 -37
  18. package/dist/src/connect/restart.js +36 -97
  19. package/dist/src/connect/stop.js +16 -50
  20. package/dist/src/connect/transport.js +10 -18
  21. package/dist/src/connect/verify.js +3 -12
  22. package/dist/src/connect/watchdog.js +6 -38
  23. package/dist/src/connect-log.js +16 -41
  24. package/dist/src/connect-progress.js +13 -45
  25. package/dist/src/constants.js +18 -43
  26. package/dist/src/context.js +13 -32
  27. package/dist/src/diagnostics.js +56 -25
  28. package/dist/src/download-jobs.js +11 -20
  29. package/dist/src/errors.js +175 -190
  30. package/dist/src/events.js +20 -84
  31. package/dist/src/host-lifecycle.js +69 -177
  32. package/dist/src/host-registry.js +27 -78
  33. package/dist/src/host-runtime.js +0 -9
  34. package/dist/src/host-view.js +6 -29
  35. package/dist/src/index.js +15 -35
  36. package/dist/src/kernel-adapter.js +34 -80
  37. package/dist/src/key-files.js +10 -23
  38. package/dist/src/known-hosts.js +13 -34
  39. package/dist/src/layout.js +3 -20
  40. package/dist/src/local-download.js +94 -163
  41. package/dist/src/local-ssh.js +12 -19
  42. package/dist/src/local-version.js +35 -49
  43. package/dist/src/node/download.js +1 -4
  44. package/dist/src/node-runtime.js +19 -38
  45. package/dist/src/probe.js +6 -7
  46. package/dist/src/prompts.js +56 -128
  47. package/dist/src/proxy/cookies.js +26 -60
  48. package/dist/src/proxy/headers.js +30 -125
  49. package/dist/src/proxy/http.js +33 -41
  50. package/dist/src/proxy/index.js +20 -34
  51. package/dist/src/proxy/limits.js +4 -6
  52. package/dist/src/proxy/routes.js +10 -17
  53. package/dist/src/proxy/upgrade.js +27 -42
  54. package/dist/src/redact.js +14 -33
  55. package/dist/src/remote-files.js +9 -21
  56. package/dist/src/remote-path.js +21 -62
  57. package/dist/src/remote-proc.js +35 -103
  58. package/dist/src/remote-web/registry.js +10 -50
  59. package/dist/src/remote-web/resolve.js +23 -104
  60. package/dist/src/remote-web/start.js +27 -64
  61. package/dist/src/remote-web/stop.js +6 -25
  62. package/dist/src/request-trust.js +15 -32
  63. package/dist/src/runtime/dispatch.js +33 -143
  64. package/dist/src/runtime/effects.js +7 -17
  65. package/dist/src/runtime/events.js +1 -1
  66. package/dist/src/runtime/fsm.js +11 -61
  67. package/dist/src/runtime/rules.js +121 -199
  68. package/dist/src/secrets.js +15 -38
  69. package/dist/src/service.js +22 -128
  70. package/dist/src/settings.js +41 -42
  71. package/dist/src/ssh-config.js +22 -41
  72. package/dist/src/ssh-failures.js +23 -74
  73. package/dist/src/ssh-sftp.js +47 -86
  74. package/dist/src/ssh.js +99 -179
  75. package/dist/src/store.js +31 -48
  76. package/dist/src/tool-shared.js +17 -52
  77. package/dist/src/tools-exec-policy.js +33 -79
  78. package/dist/src/tools-exec.js +87 -151
  79. package/dist/src/tools-report.js +100 -167
  80. package/dist/src/tools.js +59 -133
  81. package/dist/src/tunnel.js +9 -27
  82. package/dist/src/util.js +19 -56
  83. package/dist/src/version.js +6 -12
  84. package/dist/src/watchdog-bus.js +0 -8
  85. package/dist/src/watchdog.js +7 -47
  86. package/package.json +3 -3
@@ -1,33 +1,15 @@
1
- /**
2
- * At most this many live state streams are served; a new one evicts the oldest, so a page that
3
- * leaks EventSources cannot grow this set without bound. The log channel has its own cap
4
- * (`MAX_SSE_LOG_CLIENTS`) because one page now opens two streams: sharing a counter would let
5
- * four tabs evict each other's state stream.
6
- */
1
+ /** At most this many live state streams; a new one evicts the oldest, so a leaking page cannot grow the set. */
7
2
  export const MAX_SSE_CLIENTS = 8;
8
- /** At most this many live `/events/logs` streams, counted independently of the state channel. */
9
3
  export const MAX_SSE_LOG_CLIENTS = 8;
10
- /** One SSE frame may not exceed this many bytes (a runaway snapshot is dropped, not buffered). */
11
4
  const MAX_SSE_FRAME_BYTES = 1 * 1024 * 1024;
12
- /** A subscriber whose unsent backlog passes this is gone: drop it instead of buffering forever. */
13
5
  const MAX_SSE_BUFFERED_BYTES = 1 * 1024 * 1024;
14
- /** One log frame may not exceed this many bytes; a frame is split before it would cross the cap. */
15
6
  export const MAX_LOG_FRAME_BYTES = 64 * 1024;
16
- /** Rows per log frame: the same bound as the `/state` tail, so one frame can rebuild the panel. */
17
7
  export const MAX_LOG_FRAME_ENTRIES = 120;
18
- /** Coalescing window for log frames (the console can print dozens of lines a second). */
19
8
  export const LOG_FLUSH_MS = 150;
20
- /** Liveness ping on both channels; a buffering middlebox then cannot look like a quiet stream. */
21
9
  const HEARTBEAT_MS = 15_000;
22
- /** `readyState` of an `EventSource`-style client that will not be retried; kept for the Client. */
23
10
  const SSE_COLON = ':';
24
- /**
25
- * Parse a `Last-Event-ID` header back into a cursor. Anything that is not exactly
26
- * `instance:hostId:attempt:seq` with two safe integers is "no cursor": the Client then gets a
27
- * `reset` frame, which is always a correct (if larger) answer.
28
- * @param value - the raw header value, when the request carried one.
29
- * @returns the cursor, or null.
30
- */
11
+ /** Parse a `Last-Event-ID` header into a cursor: anything but exactly `instance:hostId:attempt:seq` with two safe
12
+ * integers is "no cursor", and the Client then gets a `reset` frame — always correct, if larger. */
31
13
  export function parseEventId(value) {
32
14
  if (typeof value !== 'string')
33
15
  return null;
@@ -43,26 +25,18 @@ export function parseEventId(value) {
43
25
  return null;
44
26
  return { instance, hostId, attempt: parsedAttempt, seq: parsedSeq };
45
27
  }
46
- /** The `id:` of one log frame; the inverse of `parseEventId`. */
47
28
  export function formatEventId(cursor) {
48
29
  return [cursor.instance, cursor.hostId, cursor.attempt, cursor.seq].join(SSE_COLON);
49
30
  }
50
- /** One host's pending rows for one subscriber: what a reset must carry, or only the new tail. */
51
31
  function planFor(view, cursor, instance) {
52
- // Every "cannot continue" case answers with the whole tail: the Client does not need to know
53
- // what it missed, and a reset of at most one frame is a cheaper contract than a gap protocol.
54
- if (cursor === null)
55
- return { entries: view.entries, reset: true, seq: view.seq };
56
- if (cursor.instance !== instance || cursor.hostId !== view.hostId) {
57
- return { entries: view.entries, reset: true, seq: view.seq };
58
- }
59
- // A new attempt starts its own `seq` from 1, so a cursor from the previous attempt would either
60
- // drop every new row (seq went backwards) or mix two attempts; reset either way.
61
- if (cursor.attempt !== view.attempt)
62
- return { entries: view.entries, reset: true, seq: view.seq };
63
- // The ring only keeps 200 rows: a cursor older than the oldest retained row has a gap, and a
64
- // partial replay would hide it.
65
- if (cursor.seq < view.firstSeq - 1)
32
+ // Every "cannot continue" case answers with the whole tail rather than a gap protocol: no cursor, another
33
+ // generation/host, another attempt (seq restarts at 1), or a cursor older than the oldest retained row (ring: 200).
34
+ const stale = cursor === null ||
35
+ cursor.instance !== instance ||
36
+ cursor.hostId !== view.hostId ||
37
+ cursor.attempt !== view.attempt ||
38
+ cursor.seq < view.firstSeq - 1;
39
+ if (stale)
66
40
  return { entries: view.entries, reset: true, seq: view.seq };
67
41
  const entries = view.entries.filter((entry) => typeof entry.seq === 'number' && entry.seq > cursor.seq);
68
42
  if (entries.length === 0)
@@ -70,14 +44,12 @@ function planFor(view, cursor, instance) {
70
44
  return { entries, reset: false, seq: entries[entries.length - 1].seq };
71
45
  }
72
46
  export function createEventHub(options) {
73
- const { service } = options;
74
- const logger = options.logger ?? console;
47
+ const { service, logger = console } = options;
75
48
  const stateClients = new Set();
76
49
  const logClients = new Set();
77
50
  const dirtyHosts = new Set();
78
51
  let flushTimer = null;
79
52
  let disposed = false;
80
- /** The documented SSE response head; both channels answer with exactly these headers. */
81
53
  function openStream(res) {
82
54
  res.writeHead(200, {
83
55
  'content-type': 'text/event-stream; charset=utf-8',
@@ -85,16 +57,8 @@ export function createEventHub(options) {
85
57
  connection: 'keep-alive',
86
58
  'x-accel-buffering': 'no',
87
59
  });
88
- // Flush the head now: the state channel writes its first frame immediately, but the log
89
- // channel can legitimately stay silent (no host is connecting), and Node would otherwise hold
90
- // the response head until the first write — leaving the browser's EventSource in CONNECTING.
91
60
  res.flushHeaders?.();
92
61
  }
93
- /**
94
- * Write one frame, or report that the subscriber is gone (a slow consumer must not grow the
95
- * process heap: once its queue is too deep it is dropped, and the browser reconnects).
96
- * @returns false when the caller should remove the subscriber from its set.
97
- */
98
62
  function writeTo(res, frame) {
99
63
  if (res.writableEnded === true || res.destroyed === true)
100
64
  return false;
@@ -115,7 +79,6 @@ export function createEventHub(options) {
115
79
  return false;
116
80
  }
117
81
  }
118
- /** Broadcast one state-channel frame; a frame no browser could render is dropped whole. */
119
82
  function broadcastStateChannel(frame) {
120
83
  const bytes = Buffer.byteLength(frame);
121
84
  if (bytes > MAX_SSE_FRAME_BYTES) {
@@ -127,12 +90,11 @@ export function createEventHub(options) {
127
90
  stateClients.delete(res);
128
91
  }
129
92
  }
130
- /** Send one log frame to one subscriber, honoring the per-frame byte cap. */
131
93
  function sendLog(client, frame) {
132
94
  const bytes = Buffer.byteLength(frame);
133
95
  if (bytes > MAX_LOG_FRAME_BYTES) {
134
- // The packer splits by size, so this is a defect rather than a load: log it and skip the
135
- // frame instead of letting it grow the 1 MiB global gate.
96
+ // The packer splits by size, so this is a defect rather than a load: log it and skip the frame
97
+ // instead of letting it grow the 1 MiB global gate.
136
98
  logger.warn?.(`[dsh-remote-go] dropped an oversized ${bytes}-byte log frame`);
137
99
  return true;
138
100
  }
@@ -143,7 +105,6 @@ export function createEventHub(options) {
143
105
  return true;
144
106
  }
145
107
  const logEndFrame = (hostId, attempt) => `event: log-end\ndata: ${JSON.stringify({ hostId, attempt })}\n\n`;
146
- /** One `log` frame; `id:` is what a reconnecting EventSource sends back as `Last-Event-ID`. */
147
108
  function logFrame(view, instance, seq, reset, entries) {
148
109
  const frame = {
149
110
  hostId: view.hostId,
@@ -157,11 +118,8 @@ export function createEventHub(options) {
157
118
  const id = formatEventId({ instance, hostId: view.hostId, attempt: view.attempt, seq });
158
119
  return `event: log\nid: ${id}\ndata: ${JSON.stringify(frame)}\n\n`;
159
120
  }
160
- /**
161
- * Split one host's rows into frames that respect both log caps. Rows are already bounded by the
162
- * ring (each message is redacted and head-tail truncated at write time), so the count cap alone
163
- * is usually enough; the byte cap keeps a run of long lines from crossing the global gate.
164
- */
121
+ /** Split one host's rows into frames that respect both log caps (rows are already bounded by the ring, so the count
122
+ * cap is usually enough; the byte cap keeps long lines from crossing the global gate). */
165
123
  function packFrames(view, plan) {
166
124
  const instance = service.stateInstance;
167
125
  if (plan.entries.length === 0)
@@ -175,7 +133,6 @@ export function createEventHub(options) {
175
133
  while (index < plan.entries.length && chunk.length < MAX_LOG_FRAME_ENTRIES) {
176
134
  const entry = plan.entries[index];
177
135
  const size = Buffer.byteLength(JSON.stringify(entry)) + 1;
178
- // Envelope: the fixed field names plus an id whose tail is the last row's `seq`.
179
136
  const envelope = 320 + 2 * String(entry.seq).length;
180
137
  if (chunk.length > 0 && bytes + size + envelope > MAX_LOG_FRAME_BYTES)
181
138
  break;
@@ -190,7 +147,6 @@ export function createEventHub(options) {
190
147
  }
191
148
  return frames;
192
149
  }
193
- /** Send one host's pending rows to one subscriber, advancing (or dropping) its cursor. */
194
150
  function publishTo(client, view) {
195
151
  const cursor = client.cursors.get(view.hostId) ?? null;
196
152
  const instance = service.stateInstance;
@@ -204,20 +160,16 @@ export function createEventHub(options) {
204
160
  client.cursors.set(view.hostId, { instance, hostId: view.hostId, attempt: view.attempt, seq: plan.seq });
205
161
  return;
206
162
  }
207
- // The host left the log-visible set: the frame said `visible: false`, now close this host.
208
163
  client.cursors.delete(view.hostId);
209
164
  sendLog(client, logEndFrame(view.hostId, view.attempt));
210
165
  return;
211
166
  }
212
167
  if (!view.visible && cursor !== null) {
213
- // Nothing new to send, but the host left the set: it still gets one closing frame (the
214
- // explicit `visible: false` the contract promises) and then its `log-end`.
215
168
  sendLog(client, logFrame(view, instance, view.seq, false, []));
216
169
  client.cursors.delete(view.hostId);
217
170
  sendLog(client, logEndFrame(view.hostId, view.attempt));
218
171
  }
219
172
  }
220
- /** Send one host's pending rows to every log subscriber (each sliced by its own cursor). */
221
173
  function publishHost(hostId) {
222
174
  const visible = service.logView(hostId);
223
175
  if (visible === null)
@@ -225,11 +177,7 @@ export function createEventHub(options) {
225
177
  for (const client of [...logClients])
226
178
  publishTo(client, visible);
227
179
  }
228
- /**
229
- * A state change can move hosts in and out of the log-visible set, and a host that just started
230
- * connecting has no cursor anywhere yet: both are discovered here, from the same event that
231
- * paints the state frame.
232
- */
180
+ /** A state change can move hosts in and out of the log-visible set: both directions are discovered here. */
233
181
  function reconcileLogs() {
234
182
  const visible = new Set(service.logHostIds());
235
183
  const ids = new Set(visible);
@@ -247,7 +195,6 @@ export function createEventHub(options) {
247
195
  }
248
196
  continue;
249
197
  }
250
- // The host was deleted: close every cursor that still names it.
251
198
  for (const client of [...logClients]) {
252
199
  const cursor = client.cursors.get(hostId);
253
200
  if (cursor === undefined)
@@ -257,15 +204,10 @@ export function createEventHub(options) {
257
204
  }
258
205
  }
259
206
  }
260
- /** The state model changed once: one frame carrying no log rows (docs/api.md v2.12). */
261
207
  function broadcastState() {
262
- const frame = `event: state\ndata: ${JSON.stringify(service.snapshot({ logs: false }))}\n\n`;
263
- broadcastStateChannel(frame);
208
+ broadcastStateChannel(`event: state\ndata: ${JSON.stringify(service.snapshot({ logs: false }))}\n\n`);
264
209
  reconcileLogs();
265
210
  }
266
- /** Both channels carry the download job and liveness-episode events of the state contract. */
267
- const broadcastDownload = (job) => broadcastStateChannel(`event: download\ndata: ${JSON.stringify(job)}\n\n`);
268
- const broadcastRemoteUnreachable = (event) => broadcastStateChannel(`event: remote-unreachable\ndata: ${JSON.stringify(event)}\n\n`);
269
211
  function markDirty(hostId) {
270
212
  if (disposed)
271
213
  return;
@@ -279,13 +221,12 @@ export function createEventHub(options) {
279
221
  for (const hostId of hosts)
280
222
  publishHost(hostId);
281
223
  }, LOG_FLUSH_MS);
282
- // A pending log frame must never keep the process alive on its own.
283
224
  flushTimer.unref?.();
284
225
  }
285
226
  const onState = () => broadcastState();
286
227
  const onLogs = (payload) => markDirty(payload.hostId);
287
- const onDownload = (job) => broadcastDownload(job);
288
- const onRemoteUnreachable = (event) => broadcastRemoteUnreachable(event);
228
+ const onDownload = (job) => broadcastStateChannel(`event: download\ndata: ${JSON.stringify(job)}\n\n`);
229
+ const onRemoteUnreachable = (event) => broadcastStateChannel(`event: remote-unreachable\ndata: ${JSON.stringify(event)}\n\n`);
289
230
  service.on('state', onState);
290
231
  service.on('logs', onLogs);
291
232
  service.on('download', onDownload);
@@ -298,7 +239,6 @@ export function createEventHub(options) {
298
239
  }
299
240
  }, HEARTBEAT_MS);
300
241
  heartbeat.unref?.();
301
- /** Close the oldest stream until one more fits; the page reconnects to a fresh one. */
302
242
  function evictOldest(clients, cap, close) {
303
243
  while (clients.size >= cap) {
304
244
  const oldest = clients.values().next().value;
@@ -319,8 +259,6 @@ export function createEventHub(options) {
319
259
  }
320
260
  });
321
261
  openStream(res);
322
- // The first frame is the current snapshot, exactly as a fresh `GET /state` would read it;
323
- // the Client drops it when it already painted that revision.
324
262
  res.write(`event: state\ndata: ${JSON.stringify(service.snapshot({ logs: false }))}\n\n`);
325
263
  stateClients.add(res);
326
264
  req.on('close', () => {
@@ -338,8 +276,6 @@ export function createEventHub(options) {
338
276
  });
339
277
  openStream(res);
340
278
  const client = { res, cursors: new Map() };
341
- // `Last-Event-ID` can only name one host (an EventSource remembers one `id`): the host it
342
- // names continues from its cursor, every other host gets a reset frame.
343
279
  const resume = parseEventId(req.headers['last-event-id']);
344
280
  if (resume !== null)
345
281
  client.cursors.set(resume.hostId, resume);