dsh-remote-go 0.2.0 → 0.3.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.md +11 -3
- package/{client.js → dist/client.js} +7710 -8182
- package/dist/index.js +4 -0
- package/dist/src/admission.js +46 -0
- package/dist/src/api.js +442 -0
- package/dist/src/atomic-json.js +235 -0
- package/dist/src/bootstrap.js +106 -0
- package/dist/src/connect/attach.js +43 -0
- package/dist/src/connect/bootstrap.js +14 -0
- package/dist/src/connect/failures.js +44 -0
- package/dist/src/connect/guards.js +31 -0
- package/dist/src/connect/launch.js +80 -0
- package/dist/src/connect/links.js +70 -0
- package/dist/src/connect/ownership.js +220 -0
- package/dist/src/connect/pipeline.js +473 -0
- package/dist/src/connect/reattach.js +215 -0
- package/dist/src/connect/registry.js +11 -0
- package/dist/src/connect/repair.js +126 -0
- package/dist/src/connect/restart.js +316 -0
- package/dist/src/connect/stop.js +198 -0
- package/dist/src/connect/transport.js +109 -0
- package/dist/src/connect/verify.js +39 -0
- package/dist/src/connect/watchdog.js +76 -0
- package/dist/src/connect-log.js +162 -0
- package/dist/src/connect-progress.js +142 -0
- package/{src → dist/src}/constants.js +30 -56
- package/dist/src/context.js +117 -0
- package/dist/src/diagnostics.js +102 -0
- package/dist/src/download-jobs.js +230 -0
- package/dist/src/errors.js +243 -0
- package/dist/src/events.js +390 -0
- package/dist/src/host-lifecycle.js +504 -0
- package/dist/src/host-registry.js +232 -0
- package/dist/src/host-runtime.js +86 -0
- package/dist/src/host-view.js +75 -0
- package/dist/src/index.js +216 -0
- package/dist/src/kernel-adapter.js +434 -0
- package/dist/src/key-files.js +52 -0
- package/dist/src/known-hosts.js +125 -0
- package/dist/src/layout.js +43 -0
- package/dist/src/local-download.js +380 -0
- package/dist/src/local-ssh.js +30 -0
- package/dist/src/local-version.js +101 -0
- package/dist/src/node/download.js +160 -0
- package/dist/src/node-runtime.js +346 -0
- package/dist/src/probe.js +96 -0
- package/dist/src/prompts.js +270 -0
- package/dist/src/proxy/cookies.js +136 -0
- package/dist/src/proxy/headers.js +262 -0
- package/dist/src/proxy/http.js +142 -0
- package/dist/src/proxy/index.js +135 -0
- package/dist/src/proxy/limits.js +8 -0
- package/dist/src/proxy/routes.js +39 -0
- package/dist/src/proxy/upgrade.js +175 -0
- package/dist/src/redact.js +104 -0
- package/dist/src/remote-files.js +72 -0
- package/dist/src/remote-path.js +123 -0
- package/dist/src/remote-proc.js +339 -0
- package/dist/src/remote-web/registry.js +147 -0
- package/dist/src/remote-web/resolve.js +310 -0
- package/dist/src/remote-web/start.js +227 -0
- package/dist/src/remote-web/stop.js +93 -0
- package/dist/src/request-trust.js +87 -0
- package/dist/src/runtime/dispatch.js +310 -0
- package/dist/src/runtime/effects.js +91 -0
- package/dist/src/runtime/events.js +35 -0
- package/dist/src/runtime/fsm.js +120 -0
- package/dist/src/runtime/rules.js +565 -0
- package/dist/src/secrets.js +142 -0
- package/dist/src/service.js +406 -0
- package/dist/src/settings.js +144 -0
- package/dist/src/ssh-config.js +276 -0
- package/dist/src/ssh-failures.js +122 -0
- package/dist/src/ssh-port.js +4 -0
- package/dist/src/ssh-sftp.js +297 -0
- package/dist/src/ssh.js +477 -0
- package/dist/src/store.js +191 -0
- package/dist/src/tool-shared.js +181 -0
- package/dist/src/tools-exec-policy.js +167 -0
- package/dist/src/tools-exec.js +371 -0
- package/dist/src/tools-report.js +424 -0
- package/dist/src/tools.js +411 -0
- package/dist/src/tunnel.js +129 -0
- package/dist/src/util.js +149 -0
- package/dist/src/version.js +114 -0
- package/dist/src/watchdog-bus.js +29 -0
- package/dist/src/watchdog.js +156 -0
- package/index.js +5 -4
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +37 -19
- package/src/admission.js +0 -43
- package/src/api.js +0 -536
- package/src/atomic-json.js +0 -225
- package/src/bootstrap.js +0 -113
- package/src/connect-log.js +0 -176
- package/src/connect-pipeline.js +0 -1900
- package/src/connect-progress.js +0 -155
- package/src/diagnostics.js +0 -160
- package/src/download-jobs.js +0 -234
- package/src/errors.js +0 -412
- package/src/host-lifecycle.js +0 -604
- package/src/host-registry.js +0 -217
- package/src/host-runtime.js +0 -127
- package/src/host-view.js +0 -75
- package/src/index.js +0 -206
- package/src/key-files.js +0 -54
- package/src/known-hosts.js +0 -133
- package/src/layout.js +0 -53
- package/src/local-download.js +0 -381
- package/src/local-ssh.js +0 -27
- package/src/local-version.js +0 -88
- package/src/node-runtime.js +0 -521
- package/src/probe.js +0 -101
- package/src/prompts.js +0 -229
- package/src/redact.js +0 -68
- package/src/remote-files.js +0 -71
- package/src/remote-path.js +0 -137
- package/src/remote-proc.js +0 -343
- package/src/remote-proxy.js +0 -815
- package/src/remote-web.js +0 -751
- package/src/request-trust.js +0 -89
- package/src/secrets.js +0 -109
- package/src/service.js +0 -459
- package/src/settings.js +0 -163
- package/src/ssh-config.js +0 -271
- package/src/ssh.js +0 -731
- package/src/store.js +0 -193
- package/src/tools.js +0 -386
- package/src/tunnel.js +0 -132
- package/src/util.js +0 -153
- package/src/version.js +0 -103
- package/src/webapp.js +0 -43
- /package/{vendor → dist/vendor}/ssh2.bundle.cjs +0 -0
|
@@ -0,0 +1,390 @@
|
|
|
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
|
+
*/
|
|
7
|
+
export const MAX_SSE_CLIENTS = 8;
|
|
8
|
+
/** At most this many live `/events/logs` streams, counted independently of the state channel. */
|
|
9
|
+
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
|
+
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
|
+
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
|
+
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
|
+
export const MAX_LOG_FRAME_ENTRIES = 120;
|
|
18
|
+
/** Coalescing window for log frames (the console can print dozens of lines a second). */
|
|
19
|
+
export const LOG_FLUSH_MS = 150;
|
|
20
|
+
/** Liveness ping on both channels; a buffering middlebox then cannot look like a quiet stream. */
|
|
21
|
+
const HEARTBEAT_MS = 15_000;
|
|
22
|
+
/** `readyState` of an `EventSource`-style client that will not be retried; kept for the Client. */
|
|
23
|
+
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
|
+
*/
|
|
31
|
+
export function parseEventId(value) {
|
|
32
|
+
if (typeof value !== 'string')
|
|
33
|
+
return null;
|
|
34
|
+
const parts = value.split(SSE_COLON);
|
|
35
|
+
if (parts.length !== 4)
|
|
36
|
+
return null;
|
|
37
|
+
const [instance, hostId, attempt, seq] = parts;
|
|
38
|
+
const parsedAttempt = Number(attempt);
|
|
39
|
+
const parsedSeq = Number(seq);
|
|
40
|
+
if (instance === '' || hostId === '')
|
|
41
|
+
return null;
|
|
42
|
+
if (!Number.isSafeInteger(parsedAttempt) || !Number.isSafeInteger(parsedSeq))
|
|
43
|
+
return null;
|
|
44
|
+
return { instance, hostId, attempt: parsedAttempt, seq: parsedSeq };
|
|
45
|
+
}
|
|
46
|
+
/** The `id:` of one log frame; the inverse of `parseEventId`. */
|
|
47
|
+
export function formatEventId(cursor) {
|
|
48
|
+
return [cursor.instance, cursor.hostId, cursor.attempt, cursor.seq].join(SSE_COLON);
|
|
49
|
+
}
|
|
50
|
+
/** One host's pending rows for one subscriber: what a reset must carry, or only the new tail. */
|
|
51
|
+
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)
|
|
66
|
+
return { entries: view.entries, reset: true, seq: view.seq };
|
|
67
|
+
const entries = view.entries.filter((entry) => typeof entry.seq === 'number' && entry.seq > cursor.seq);
|
|
68
|
+
if (entries.length === 0)
|
|
69
|
+
return null;
|
|
70
|
+
return { entries, reset: false, seq: entries[entries.length - 1].seq };
|
|
71
|
+
}
|
|
72
|
+
export function createEventHub(options) {
|
|
73
|
+
const { service } = options;
|
|
74
|
+
const logger = options.logger ?? console;
|
|
75
|
+
const stateClients = new Set();
|
|
76
|
+
const logClients = new Set();
|
|
77
|
+
const dirtyHosts = new Set();
|
|
78
|
+
let flushTimer = null;
|
|
79
|
+
let disposed = false;
|
|
80
|
+
/** The documented SSE response head; both channels answer with exactly these headers. */
|
|
81
|
+
function openStream(res) {
|
|
82
|
+
res.writeHead(200, {
|
|
83
|
+
'content-type': 'text/event-stream; charset=utf-8',
|
|
84
|
+
'cache-control': 'no-cache, no-transform',
|
|
85
|
+
connection: 'keep-alive',
|
|
86
|
+
'x-accel-buffering': 'no',
|
|
87
|
+
});
|
|
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
|
+
res.flushHeaders?.();
|
|
92
|
+
}
|
|
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
|
+
function writeTo(res, frame) {
|
|
99
|
+
if (res.writableEnded === true || res.destroyed === true)
|
|
100
|
+
return false;
|
|
101
|
+
if (res.writableLength > MAX_SSE_BUFFERED_BYTES) {
|
|
102
|
+
try {
|
|
103
|
+
res.end();
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
// swallow-ok: the response is already gone; the subscriber is dropped either way
|
|
107
|
+
}
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
res.write(frame);
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/** Broadcast one state-channel frame; a frame no browser could render is dropped whole. */
|
|
119
|
+
function broadcastStateChannel(frame) {
|
|
120
|
+
const bytes = Buffer.byteLength(frame);
|
|
121
|
+
if (bytes > MAX_SSE_FRAME_BYTES) {
|
|
122
|
+
logger.warn?.(`[dsh-remote-go] dropped an ${bytes}-byte SSE frame`);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
for (const res of [...stateClients]) {
|
|
126
|
+
if (!writeTo(res, frame))
|
|
127
|
+
stateClients.delete(res);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/** Send one log frame to one subscriber, honoring the per-frame byte cap. */
|
|
131
|
+
function sendLog(client, frame) {
|
|
132
|
+
const bytes = Buffer.byteLength(frame);
|
|
133
|
+
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.
|
|
136
|
+
logger.warn?.(`[dsh-remote-go] dropped an oversized ${bytes}-byte log frame`);
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
if (!writeTo(client.res, frame)) {
|
|
140
|
+
logClients.delete(client);
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
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
|
+
function logFrame(view, instance, seq, reset, entries) {
|
|
148
|
+
const frame = {
|
|
149
|
+
hostId: view.hostId,
|
|
150
|
+
instance,
|
|
151
|
+
attempt: view.attempt,
|
|
152
|
+
seq,
|
|
153
|
+
...(reset ? { reset: true } : {}),
|
|
154
|
+
visible: view.visible,
|
|
155
|
+
entries,
|
|
156
|
+
};
|
|
157
|
+
const id = formatEventId({ instance, hostId: view.hostId, attempt: view.attempt, seq });
|
|
158
|
+
return `event: log\nid: ${id}\ndata: ${JSON.stringify(frame)}\n\n`;
|
|
159
|
+
}
|
|
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
|
+
*/
|
|
165
|
+
function packFrames(view, plan) {
|
|
166
|
+
const instance = service.stateInstance;
|
|
167
|
+
if (plan.entries.length === 0)
|
|
168
|
+
return [logFrame(view, instance, plan.seq, plan.reset, [])];
|
|
169
|
+
const frames = [];
|
|
170
|
+
let index = 0;
|
|
171
|
+
let first = true;
|
|
172
|
+
while (index < plan.entries.length) {
|
|
173
|
+
const chunk = [];
|
|
174
|
+
let bytes = 0;
|
|
175
|
+
while (index < plan.entries.length && chunk.length < MAX_LOG_FRAME_ENTRIES) {
|
|
176
|
+
const entry = plan.entries[index];
|
|
177
|
+
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
|
+
const envelope = 320 + 2 * String(entry.seq).length;
|
|
180
|
+
if (chunk.length > 0 && bytes + size + envelope > MAX_LOG_FRAME_BYTES)
|
|
181
|
+
break;
|
|
182
|
+
chunk.push(entry);
|
|
183
|
+
bytes += size;
|
|
184
|
+
index += 1;
|
|
185
|
+
}
|
|
186
|
+
const last = chunk[chunk.length - 1];
|
|
187
|
+
const seq = last !== undefined ? last.seq : view.seq;
|
|
188
|
+
frames.push(logFrame(view, instance, seq, plan.reset && first, chunk));
|
|
189
|
+
first = false;
|
|
190
|
+
}
|
|
191
|
+
return frames;
|
|
192
|
+
}
|
|
193
|
+
/** Send one host's pending rows to one subscriber, advancing (or dropping) its cursor. */
|
|
194
|
+
function publishTo(client, view) {
|
|
195
|
+
const cursor = client.cursors.get(view.hostId) ?? null;
|
|
196
|
+
const instance = service.stateInstance;
|
|
197
|
+
const plan = planFor(view, cursor, instance);
|
|
198
|
+
if (plan !== null) {
|
|
199
|
+
for (const frame of packFrames(view, plan)) {
|
|
200
|
+
if (!sendLog(client, frame))
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
if (view.visible) {
|
|
204
|
+
client.cursors.set(view.hostId, { instance, hostId: view.hostId, attempt: view.attempt, seq: plan.seq });
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
// The host left the log-visible set: the frame said `visible: false`, now close this host.
|
|
208
|
+
client.cursors.delete(view.hostId);
|
|
209
|
+
sendLog(client, logEndFrame(view.hostId, view.attempt));
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
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
|
+
sendLog(client, logFrame(view, instance, view.seq, false, []));
|
|
216
|
+
client.cursors.delete(view.hostId);
|
|
217
|
+
sendLog(client, logEndFrame(view.hostId, view.attempt));
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
/** Send one host's pending rows to every log subscriber (each sliced by its own cursor). */
|
|
221
|
+
function publishHost(hostId) {
|
|
222
|
+
const visible = service.logView(hostId);
|
|
223
|
+
if (visible === null)
|
|
224
|
+
return;
|
|
225
|
+
for (const client of [...logClients])
|
|
226
|
+
publishTo(client, visible);
|
|
227
|
+
}
|
|
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
|
+
*/
|
|
233
|
+
function reconcileLogs() {
|
|
234
|
+
const visible = new Set(service.logHostIds());
|
|
235
|
+
const ids = new Set(visible);
|
|
236
|
+
for (const client of logClients) {
|
|
237
|
+
for (const hostId of client.cursors.keys())
|
|
238
|
+
ids.add(hostId);
|
|
239
|
+
}
|
|
240
|
+
for (const hostId of ids) {
|
|
241
|
+
const view = service.logView(hostId);
|
|
242
|
+
if (view !== null) {
|
|
243
|
+
for (const client of [...logClients]) {
|
|
244
|
+
if (!visible.has(hostId) && !client.cursors.has(hostId))
|
|
245
|
+
continue;
|
|
246
|
+
publishTo(client, view);
|
|
247
|
+
}
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
// The host was deleted: close every cursor that still names it.
|
|
251
|
+
for (const client of [...logClients]) {
|
|
252
|
+
const cursor = client.cursors.get(hostId);
|
|
253
|
+
if (cursor === undefined)
|
|
254
|
+
continue;
|
|
255
|
+
client.cursors.delete(hostId);
|
|
256
|
+
sendLog(client, logEndFrame(hostId, cursor.attempt));
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
/** The state model changed once: one frame carrying no log rows (docs/api.md v2.12). */
|
|
261
|
+
function broadcastState() {
|
|
262
|
+
const frame = `event: state\ndata: ${JSON.stringify(service.snapshot({ logs: false }))}\n\n`;
|
|
263
|
+
broadcastStateChannel(frame);
|
|
264
|
+
reconcileLogs();
|
|
265
|
+
}
|
|
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
|
+
function markDirty(hostId) {
|
|
270
|
+
if (disposed)
|
|
271
|
+
return;
|
|
272
|
+
dirtyHosts.add(hostId);
|
|
273
|
+
if (flushTimer !== null)
|
|
274
|
+
return;
|
|
275
|
+
flushTimer = setTimeout(() => {
|
|
276
|
+
flushTimer = null;
|
|
277
|
+
const hosts = [...dirtyHosts];
|
|
278
|
+
dirtyHosts.clear();
|
|
279
|
+
for (const hostId of hosts)
|
|
280
|
+
publishHost(hostId);
|
|
281
|
+
}, LOG_FLUSH_MS);
|
|
282
|
+
// A pending log frame must never keep the process alive on its own.
|
|
283
|
+
flushTimer.unref?.();
|
|
284
|
+
}
|
|
285
|
+
const onState = () => broadcastState();
|
|
286
|
+
const onLogs = (payload) => markDirty(payload.hostId);
|
|
287
|
+
const onDownload = (job) => broadcastDownload(job);
|
|
288
|
+
const onRemoteUnreachable = (event) => broadcastRemoteUnreachable(event);
|
|
289
|
+
service.on('state', onState);
|
|
290
|
+
service.on('logs', onLogs);
|
|
291
|
+
service.on('download', onDownload);
|
|
292
|
+
service.on('remote-unreachable', onRemoteUnreachable);
|
|
293
|
+
const heartbeat = setInterval(() => {
|
|
294
|
+
broadcastStateChannel('event: ping\ndata: {}\n\n');
|
|
295
|
+
for (const client of [...logClients]) {
|
|
296
|
+
if (!writeTo(client.res, 'event: ping\ndata: {}\n\n'))
|
|
297
|
+
logClients.delete(client);
|
|
298
|
+
}
|
|
299
|
+
}, HEARTBEAT_MS);
|
|
300
|
+
heartbeat.unref?.();
|
|
301
|
+
/** Close the oldest stream until one more fits; the page reconnects to a fresh one. */
|
|
302
|
+
function evictOldest(clients, cap, close) {
|
|
303
|
+
while (clients.size >= cap) {
|
|
304
|
+
const oldest = clients.values().next().value;
|
|
305
|
+
if (oldest === undefined)
|
|
306
|
+
break;
|
|
307
|
+
clients.delete(oldest);
|
|
308
|
+
close(oldest);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
return {
|
|
312
|
+
handleState(req, res) {
|
|
313
|
+
evictOldest(stateClients, MAX_SSE_CLIENTS, (client) => {
|
|
314
|
+
try {
|
|
315
|
+
client.end();
|
|
316
|
+
}
|
|
317
|
+
catch {
|
|
318
|
+
// swallow-ok: eviction closes a stream the browser already dropped
|
|
319
|
+
}
|
|
320
|
+
});
|
|
321
|
+
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
|
+
res.write(`event: state\ndata: ${JSON.stringify(service.snapshot({ logs: false }))}\n\n`);
|
|
325
|
+
stateClients.add(res);
|
|
326
|
+
req.on('close', () => {
|
|
327
|
+
stateClients.delete(res);
|
|
328
|
+
});
|
|
329
|
+
},
|
|
330
|
+
handleLogs(req, res) {
|
|
331
|
+
evictOldest(logClients, MAX_SSE_LOG_CLIENTS, (client) => {
|
|
332
|
+
try {
|
|
333
|
+
client.res.end();
|
|
334
|
+
}
|
|
335
|
+
catch {
|
|
336
|
+
// swallow-ok: same eviction as the state channel; the stream is already gone
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
openStream(res);
|
|
340
|
+
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
|
+
const resume = parseEventId(req.headers['last-event-id']);
|
|
344
|
+
if (resume !== null)
|
|
345
|
+
client.cursors.set(resume.hostId, resume);
|
|
346
|
+
logClients.add(client);
|
|
347
|
+
for (const hostId of service.logHostIds()) {
|
|
348
|
+
const view = service.logView(hostId);
|
|
349
|
+
if (view !== null)
|
|
350
|
+
publishTo(client, view);
|
|
351
|
+
}
|
|
352
|
+
req.on('close', () => {
|
|
353
|
+
logClients.delete(client);
|
|
354
|
+
});
|
|
355
|
+
},
|
|
356
|
+
dispose() {
|
|
357
|
+
if (disposed)
|
|
358
|
+
return;
|
|
359
|
+
disposed = true;
|
|
360
|
+
service.removeListener('state', onState);
|
|
361
|
+
service.removeListener('logs', onLogs);
|
|
362
|
+
service.removeListener('download', onDownload);
|
|
363
|
+
service.removeListener('remote-unreachable', onRemoteUnreachable);
|
|
364
|
+
clearInterval(heartbeat);
|
|
365
|
+
if (flushTimer !== null) {
|
|
366
|
+
clearTimeout(flushTimer);
|
|
367
|
+
flushTimer = null;
|
|
368
|
+
}
|
|
369
|
+
dirtyHosts.clear();
|
|
370
|
+
for (const res of stateClients) {
|
|
371
|
+
try {
|
|
372
|
+
res.end();
|
|
373
|
+
}
|
|
374
|
+
catch {
|
|
375
|
+
// swallow-ok: dispose ends streams the browser may already have closed
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
stateClients.clear();
|
|
379
|
+
for (const client of logClients) {
|
|
380
|
+
try {
|
|
381
|
+
client.res.end();
|
|
382
|
+
}
|
|
383
|
+
catch {
|
|
384
|
+
// swallow-ok: the same dispose sweep on the log channel
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
logClients.clear();
|
|
388
|
+
},
|
|
389
|
+
};
|
|
390
|
+
}
|