@myrialabs/clopen 0.4.34 → 0.4.35
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/backend/auth/permissions.ts +7 -0
- package/backend/containers/actions.ts +316 -0
- package/backend/containers/disk-usage.test.ts +68 -0
- package/backend/containers/disk-usage.ts +170 -0
- package/backend/containers/logs.ts +335 -0
- package/backend/containers/monitor.ts +410 -0
- package/backend/containers/parse.test.ts +599 -0
- package/backend/containers/parse.ts +905 -0
- package/backend/containers/port-index.test.ts +91 -0
- package/backend/containers/port-index.ts +198 -0
- package/backend/containers/prune-job.test.ts +148 -0
- package/backend/containers/prune-job.ts +129 -0
- package/backend/containers/pty-backend.ts +337 -0
- package/backend/containers/pty.ts +114 -0
- package/backend/containers/runtime.ts +218 -0
- package/backend/containers/scan.ts +293 -0
- package/backend/files/file-watcher.test.ts +105 -0
- package/backend/files/file-watcher.ts +45 -1
- package/backend/git/git-service.test.ts +33 -0
- package/backend/git/git-service.ts +7 -3
- package/backend/git/nested-repos.test.ts +292 -1
- package/backend/git/nested-repos.ts +330 -17
- package/backend/{ports → host}/runner.ts +19 -10
- package/backend/index.ts +11 -2
- package/backend/ports/attribute.ts +1 -1
- package/backend/ports/kill.ts +1 -1
- package/backend/ports/monitor.ts +1 -1
- package/backend/ports/processes.ts +1 -1
- package/backend/ports/scan-host.ts +77 -8
- package/backend/ports/scan.test.ts +1 -1
- package/backend/ports/scan.ts +1 -1
- package/backend/ports/ssh-lineage.ts +1 -1
- package/backend/preview/browser/browser-video-capture.ts +84 -31
- package/backend/ws/containers/actions.ts +156 -0
- package/backend/ws/containers/index.ts +43 -0
- package/backend/ws/containers/list.ts +146 -0
- package/backend/ws/containers/logs.ts +43 -0
- package/backend/ws/containers/tunnel.ts +101 -0
- package/backend/ws/git/log.ts +3 -1
- package/backend/ws/git/status.ts +37 -27
- package/backend/ws/index.ts +2 -0
- package/backend/ws/message-rate-limiter.test.ts +45 -0
- package/backend/ws/message-rate-limiter.ts +34 -3
- package/backend/ws/ssh/connections.ts +8 -0
- package/backend/ws/system/device-info.ts +99 -43
- package/dist/assets/{index-vW2RpdHu.js → index-B0iIhSO9.js} +1 -1
- package/dist/assets/{index-gGTMFUa8.js → index-BRJqss6F.js} +1 -1
- package/dist/assets/main-Buko4D0t.css +1 -0
- package/dist/assets/{main-ZCojSzvW.js → main-C_iGUZyL.js} +467 -449
- package/dist/index.html +2 -2
- package/dist/manifest.json +9 -2
- package/package.json +1 -1
- package/shared/types/containers/container.ts +366 -0
- package/shared/types/containers/index.ts +5 -0
- package/shared/types/host/index.ts +18 -0
- package/shared/types/ports/port.ts +47 -9
- package/shared/utils/logger.ts +2 -0
- package/shared/utils/ws-client.ts +67 -12
- package/shared/utils/ws-server.ts +64 -13
- package/dist/assets/main-CYCMEbXB.css +0 -1
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Containers — following a container's output.
|
|
3
|
+
*
|
|
4
|
+
* `docker logs -f` never returns, which is the one thing the shared
|
|
5
|
+
* `CommandRunner` cannot carry: locally it buffers a command to completion, and
|
|
6
|
+
* remotely it queues commands one at a time on a connection the terminal and
|
|
7
|
+
* file browser share — a follow would sit at the head of that queue forever. So
|
|
8
|
+
* a stream gets its own channel on both transports, and pays for it with hard
|
|
9
|
+
* limits: a bounded number of streams per host, and a bounded ring buffer per
|
|
10
|
+
* stream so a container writing a megabyte a second cannot grow this process's
|
|
11
|
+
* memory without limit.
|
|
12
|
+
*
|
|
13
|
+
* Output is coalesced and flushed on a short interval rather than pushed per
|
|
14
|
+
* chunk. A build log arrives in hundreds of tiny writes, and one WebSocket
|
|
15
|
+
* frame each would cost more than the log is worth.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import type { Client as SshClient, ClientChannel } from 'ssh2';
|
|
19
|
+
import { CONTAINER_LOG_BUFFER_LINES } from '$shared/types/containers';
|
|
20
|
+
import { LOCAL_HOST_ID } from '$shared/types/host';
|
|
21
|
+
import { containerMonitor } from './monitor';
|
|
22
|
+
import { assertContainerId } from './actions';
|
|
23
|
+
import { containerArgv, detectRuntime } from './runtime';
|
|
24
|
+
import { localPlatform, type ProbePlatform } from '../host/runner';
|
|
25
|
+
import { detectRemotePlatform } from '../host/runner';
|
|
26
|
+
import { shellQuote } from '../ssh/connect';
|
|
27
|
+
import { sshClientPool, type SshLease } from '../ssh/client-pool';
|
|
28
|
+
import { sshConnectionQueries } from '../database/queries';
|
|
29
|
+
import { ws } from '../utils/ws';
|
|
30
|
+
import { debug } from '$shared/utils/logger';
|
|
31
|
+
|
|
32
|
+
/** Lines asked for when a stream opens, so the view is not blank. */
|
|
33
|
+
const TAIL_LINES = 500;
|
|
34
|
+
/**
|
|
35
|
+
* Streams a single host may have open at once.
|
|
36
|
+
*
|
|
37
|
+
* Bounded for SSH's sake: every follow holds an exec channel, and sshd's
|
|
38
|
+
* `MaxSessions` is ten by default — shared with the terminal, the file browser
|
|
39
|
+
* and the port scan. Three leaves room for all of them.
|
|
40
|
+
*/
|
|
41
|
+
const MAX_STREAMS_PER_HOST = 3;
|
|
42
|
+
/** How often buffered output is pushed to the client. */
|
|
43
|
+
const FLUSH_INTERVAL_MS = 120;
|
|
44
|
+
|
|
45
|
+
interface LogStream {
|
|
46
|
+
id: string;
|
|
47
|
+
hostId: string;
|
|
48
|
+
containerId: string;
|
|
49
|
+
userId: string;
|
|
50
|
+
/** The last N lines, so a reopened view is not empty while it waits. */
|
|
51
|
+
buffer: string[];
|
|
52
|
+
pending: string;
|
|
53
|
+
flushTimer: ReturnType<typeof setInterval> | null;
|
|
54
|
+
stop: () => void;
|
|
55
|
+
stopped: boolean;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const streams = new Map<string, LogStream>();
|
|
59
|
+
|
|
60
|
+
/** One stream per user per container: a second tab joins the first. */
|
|
61
|
+
function streamIdFor(userId: string, hostId: string, containerId: string): string {
|
|
62
|
+
return `${userId}:${hostId}:${containerId}`;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function countForHost(hostId: string): number {
|
|
66
|
+
let count = 0;
|
|
67
|
+
for (const stream of streams.values()) if (stream.hostId === hostId) count++;
|
|
68
|
+
return count;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Keep the ring bounded, counting lines rather than bytes so a trim never cuts
|
|
73
|
+
* a line in half. The buffer holds lines without their newlines, so a chunk's
|
|
74
|
+
* first piece continues whatever line the previous chunk left open — output
|
|
75
|
+
* arrives in arbitrary slices, not conveniently at line boundaries.
|
|
76
|
+
*/
|
|
77
|
+
function appendToBuffer(stream: LogStream, text: string): void {
|
|
78
|
+
const lines = text.split('\n');
|
|
79
|
+
if (stream.buffer.length > 0) {
|
|
80
|
+
stream.buffer[stream.buffer.length - 1] += lines.shift() ?? '';
|
|
81
|
+
}
|
|
82
|
+
for (const line of lines) stream.buffer.push(line);
|
|
83
|
+
if (stream.buffer.length > CONTAINER_LOG_BUFFER_LINES) {
|
|
84
|
+
stream.buffer.splice(0, stream.buffer.length - CONTAINER_LOG_BUFFER_LINES);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function push(stream: LogStream, text: string): void {
|
|
89
|
+
if (!text) return;
|
|
90
|
+
appendToBuffer(stream, text);
|
|
91
|
+
stream.pending += text;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function flush(stream: LogStream): void {
|
|
95
|
+
if (!stream.pending) return;
|
|
96
|
+
const data = stream.pending;
|
|
97
|
+
stream.pending = '';
|
|
98
|
+
ws.emit.user(stream.userId, 'containers:log-chunk', {
|
|
99
|
+
streamId: stream.id,
|
|
100
|
+
hostId: stream.hostId,
|
|
101
|
+
containerId: stream.containerId,
|
|
102
|
+
data
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function finish(stream: LogStream, error: string | null): void {
|
|
107
|
+
if (stream.stopped) return;
|
|
108
|
+
stream.stopped = true;
|
|
109
|
+
flush(stream);
|
|
110
|
+
if (stream.flushTimer) clearInterval(stream.flushTimer);
|
|
111
|
+
streams.delete(stream.id);
|
|
112
|
+
ws.emit.user(stream.userId, 'containers:log-chunk', {
|
|
113
|
+
streamId: stream.id,
|
|
114
|
+
hostId: stream.hostId,
|
|
115
|
+
containerId: stream.containerId,
|
|
116
|
+
data: '',
|
|
117
|
+
done: true,
|
|
118
|
+
error
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export interface StartedLogStream {
|
|
123
|
+
streamId: string;
|
|
124
|
+
/** Whatever the ring already holds, for a view that is joining late. */
|
|
125
|
+
backlog: string;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Start following a container, or join the stream already following it.
|
|
130
|
+
*
|
|
131
|
+
* The container is verified against a fresh listing first: the id arrived from
|
|
132
|
+
* a client, and a stream is a process held open on someone's machine.
|
|
133
|
+
*/
|
|
134
|
+
export async function startLogStream(
|
|
135
|
+
hostId: string,
|
|
136
|
+
containerId: string,
|
|
137
|
+
userId: string
|
|
138
|
+
): Promise<StartedLogStream> {
|
|
139
|
+
assertContainerId(containerId);
|
|
140
|
+
|
|
141
|
+
const id = streamIdFor(userId, hostId, containerId);
|
|
142
|
+
const existing = streams.get(id);
|
|
143
|
+
// Rejoined with the newlines the buffer strips, or the whole backlog would
|
|
144
|
+
// arrive as one enormous line.
|
|
145
|
+
if (existing) return { streamId: id, backlog: existing.buffer.join('\n') };
|
|
146
|
+
|
|
147
|
+
const container = await containerMonitor.findContainer(hostId, containerId);
|
|
148
|
+
if (!container) throw new Error('That container no longer exists on this host.');
|
|
149
|
+
|
|
150
|
+
if (countForHost(hostId) >= MAX_STREAMS_PER_HOST) {
|
|
151
|
+
throw new Error(
|
|
152
|
+
`Already following ${MAX_STREAMS_PER_HOST} logs on this host. Close one before opening another.`
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const stream: LogStream = {
|
|
157
|
+
id,
|
|
158
|
+
hostId,
|
|
159
|
+
containerId,
|
|
160
|
+
userId,
|
|
161
|
+
buffer: [],
|
|
162
|
+
pending: '',
|
|
163
|
+
flushTimer: null,
|
|
164
|
+
stop: () => undefined,
|
|
165
|
+
stopped: false
|
|
166
|
+
};
|
|
167
|
+
streams.set(id, stream);
|
|
168
|
+
|
|
169
|
+
stream.flushTimer = setInterval(() => flush(stream), FLUSH_INTERVAL_MS);
|
|
170
|
+
stream.flushTimer.unref?.();
|
|
171
|
+
|
|
172
|
+
try {
|
|
173
|
+
if (hostId === LOCAL_HOST_ID) await followLocal(stream);
|
|
174
|
+
else await followRemote(stream);
|
|
175
|
+
} catch (error) {
|
|
176
|
+
finish(stream, error instanceof Error ? error.message : String(error));
|
|
177
|
+
throw error;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
debug.log('containers', `following logs for ${container.name} on ${hostId}`);
|
|
181
|
+
return { streamId: id, backlog: '' };
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** The argv both transports run, once the runtime is known. */
|
|
185
|
+
async function logArgv(
|
|
186
|
+
hostId: string,
|
|
187
|
+
containerId: string,
|
|
188
|
+
platform: ProbePlatform
|
|
189
|
+
): Promise<string[]> {
|
|
190
|
+
const info = await containerMonitor.withHost(hostId, (runner) =>
|
|
191
|
+
detectRuntime(hostId, runner, platform)
|
|
192
|
+
);
|
|
193
|
+
if (info.problem !== 'none' || !info.runtime) {
|
|
194
|
+
throw new Error('This host has no container runtime available right now.');
|
|
195
|
+
}
|
|
196
|
+
return containerArgv(info.runtime, platform, [
|
|
197
|
+
'logs',
|
|
198
|
+
'--follow',
|
|
199
|
+
'--tail',
|
|
200
|
+
String(TAIL_LINES),
|
|
201
|
+
containerId
|
|
202
|
+
]);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async function followLocal(stream: LogStream): Promise<void> {
|
|
206
|
+
const platform = localPlatform();
|
|
207
|
+
const argv = await logArgv(stream.hostId, stream.containerId, platform);
|
|
208
|
+
|
|
209
|
+
const child = Bun.spawn(argv, { stdout: 'pipe', stderr: 'pipe', stdin: 'ignore' });
|
|
210
|
+
stream.stop = () => {
|
|
211
|
+
try {
|
|
212
|
+
child.kill();
|
|
213
|
+
} catch {
|
|
214
|
+
// Already gone.
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
// `docker logs` writes a container's stdout and stderr to its own two
|
|
219
|
+
// streams; a reader wants them interleaved exactly as the container wrote
|
|
220
|
+
// them, so both are pumped into the same buffer.
|
|
221
|
+
void pump(child.stdout, stream);
|
|
222
|
+
void pump(child.stderr, stream);
|
|
223
|
+
void child.exited.then((code) => {
|
|
224
|
+
finish(stream, code === 0 || stream.stopped ? null : `The log command exited with status ${code}.`);
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
async function pump(readable: ReadableStream<Uint8Array> | null, stream: LogStream): Promise<void> {
|
|
229
|
+
if (!readable) return;
|
|
230
|
+
const decoder = new TextDecoder();
|
|
231
|
+
const reader = readable.getReader();
|
|
232
|
+
try {
|
|
233
|
+
for (;;) {
|
|
234
|
+
const { done, value } = await reader.read();
|
|
235
|
+
if (done || stream.stopped) return;
|
|
236
|
+
if (value) push(stream, decoder.decode(value, { stream: true }));
|
|
237
|
+
}
|
|
238
|
+
} catch (error) {
|
|
239
|
+
debug.log('containers', 'log stream ended:', error);
|
|
240
|
+
} finally {
|
|
241
|
+
reader.releaseLock();
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
async function followRemote(stream: LogStream): Promise<void> {
|
|
246
|
+
const connection = sshConnectionQueries.get(stream.hostId);
|
|
247
|
+
if (!connection) throw new Error('That SSH host no longer exists.');
|
|
248
|
+
|
|
249
|
+
const platform = await containerMonitor.withHost(stream.hostId, (runner) =>
|
|
250
|
+
detectRemotePlatform(stream.hostId, runner)
|
|
251
|
+
);
|
|
252
|
+
const argv = await logArgv(stream.hostId, stream.containerId, platform);
|
|
253
|
+
// Its own lease, not the shared queued runner: this channel never closes on
|
|
254
|
+
// its own, and everything behind it in that queue would wait forever.
|
|
255
|
+
const lease = await sshClientPool.acquire(stream.hostId);
|
|
256
|
+
|
|
257
|
+
try {
|
|
258
|
+
const channel = await openExec(lease.client, argv.map(shellQuote).join(' '));
|
|
259
|
+
let released = false;
|
|
260
|
+
const release = (): void => {
|
|
261
|
+
if (released) return;
|
|
262
|
+
released = true;
|
|
263
|
+
lease.release();
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
stream.stop = () => {
|
|
267
|
+
try {
|
|
268
|
+
channel.close();
|
|
269
|
+
} catch {
|
|
270
|
+
// Already gone.
|
|
271
|
+
}
|
|
272
|
+
release();
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
const decoder = new TextDecoder();
|
|
276
|
+
channel.on('data', (chunk: Buffer) => push(stream, decoder.decode(chunk, { stream: true })));
|
|
277
|
+
channel.stderr.on('data', (chunk: Buffer) => push(stream, decoder.decode(chunk, { stream: true })));
|
|
278
|
+
channel.on('close', () => {
|
|
279
|
+
release();
|
|
280
|
+
finish(stream, null);
|
|
281
|
+
});
|
|
282
|
+
channel.on('error', (error: Error) => {
|
|
283
|
+
release();
|
|
284
|
+
finish(stream, error.message);
|
|
285
|
+
});
|
|
286
|
+
} catch (error) {
|
|
287
|
+
lease.release();
|
|
288
|
+
throw error;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function openExec(client: SshClient, command: string): Promise<ClientChannel> {
|
|
293
|
+
return new Promise((resolve, reject) => {
|
|
294
|
+
client.exec(command, (error, channel) => {
|
|
295
|
+
if (error) reject(error);
|
|
296
|
+
else resolve(channel);
|
|
297
|
+
});
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** Stop one stream, if it belongs to this user. */
|
|
302
|
+
export function stopLogStream(streamId: string, userId: string): void {
|
|
303
|
+
const stream = streams.get(streamId);
|
|
304
|
+
if (!stream || stream.userId !== userId) return;
|
|
305
|
+
stream.stop();
|
|
306
|
+
finish(stream, null);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/** Stop every stream a user holds, when their socket goes away. */
|
|
310
|
+
export function stopLogStreamsForUser(userId: string): void {
|
|
311
|
+
for (const stream of [...streams.values()]) {
|
|
312
|
+
if (stream.userId === userId) {
|
|
313
|
+
stream.stop();
|
|
314
|
+
finish(stream, null);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/** Stop everything, when the process is shutting down. */
|
|
320
|
+
export function stopAllLogStreams(): void {
|
|
321
|
+
for (const stream of [...streams.values()]) {
|
|
322
|
+
stream.stop();
|
|
323
|
+
finish(stream, null);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/** Stop every stream on a host, when its connection is edited or deleted. */
|
|
328
|
+
export function stopLogStreamsForHost(hostId: string): void {
|
|
329
|
+
for (const stream of [...streams.values()]) {
|
|
330
|
+
if (stream.hostId === hostId) {
|
|
331
|
+
stream.stop();
|
|
332
|
+
finish(stream, null);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|