@myrialabs/clopen 0.4.33 → 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 +10 -0
- package/backend/auth/rate-limiter.test.ts +115 -0
- package/backend/auth/rate-limiter.ts +25 -12
- 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/db-client/connection-manager.ts +13 -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/host/runner.ts +204 -0
- package/backend/index.ts +20 -2
- package/backend/middleware/security.test.ts +95 -0
- package/backend/middleware/security.ts +114 -0
- package/backend/ports/attribute.test.ts +231 -0
- package/backend/ports/attribute.ts +444 -0
- package/backend/ports/kill.ts +203 -0
- package/backend/ports/monitor.ts +365 -0
- package/backend/ports/processes.test.ts +98 -0
- package/backend/ports/processes.ts +374 -0
- package/backend/ports/registry.ts +165 -0
- package/backend/ports/scan-host.ts +329 -0
- package/backend/ports/scan.test.ts +341 -0
- package/backend/ports/scan.ts +687 -0
- package/backend/ports/ssh-lineage.ts +318 -0
- package/backend/preview/browser/browser-video-capture.ts +84 -31
- package/backend/ssh/forwards.ts +31 -0
- package/backend/ws/auth/device.ts +1 -1
- package/backend/ws/auth/login.ts +8 -4
- 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 +4 -0
- package/backend/ws/message-rate-limiter.test.ts +45 -0
- package/backend/ws/message-rate-limiter.ts +34 -3
- package/backend/ws/ports/index.ts +19 -0
- package/backend/ws/ports/kill.ts +80 -0
- package/backend/ws/ports/list.ts +60 -0
- package/backend/ws/ssh/connections.ts +8 -0
- package/backend/ws/system/device-info.ts +99 -43
- package/dist/assets/{index-BF4buYmI.js → index-B0iIhSO9.js} +1 -1
- package/dist/assets/{index-BW0DA8NV.js → index-BRJqss6F.js} +1 -1
- package/dist/assets/main-Buko4D0t.css +1 -0
- package/dist/assets/{main-BbwWLJRN.js → main-C_iGUZyL.js} +468 -447
- 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/index.ts +5 -0
- package/shared/types/ports/port.ts +230 -0
- package/shared/utils/logger.ts +3 -0
- package/shared/utils/ws-client.ts +67 -12
- package/shared/utils/ws-server.ts +64 -13
- package/dist/assets/main-CCvRMXlP.css +0 -1
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Port manager — keeping the table live.
|
|
3
|
+
*
|
|
4
|
+
* No operating system offers a portable way to be *told* that a port opened.
|
|
5
|
+
* macOS exposes no socket-table event API at all, Windows' change
|
|
6
|
+
* notifications do not cover the TCP listener table, and Linux's `sock_diag`
|
|
7
|
+
* netlink is a query interface — `ss --events` only reports destroyed sockets
|
|
8
|
+
* and needs privileges besides. An event-driven implementation would therefore
|
|
9
|
+
* exist on one platform and be partial even there.
|
|
10
|
+
*
|
|
11
|
+
* So this polls, and earns the right to by being cheap and quiet:
|
|
12
|
+
*
|
|
13
|
+
* - A warm local scan is one `lsof` plus a cached process table — tens of
|
|
14
|
+
* milliseconds — so a watched panel ticks every second and reads as live. An
|
|
15
|
+
* SSH host ticks every three: each scan is a round trip on the connection the
|
|
16
|
+
* terminal and file browser are also using.
|
|
17
|
+
* - Results are diffed server-side and only pushed when something actually
|
|
18
|
+
* changed, so a static machine costs no traffic at all.
|
|
19
|
+
* - With no panel open, the only work is the sidebar count, refreshed every
|
|
20
|
+
* fifteen seconds at roughly fifteen milliseconds a time.
|
|
21
|
+
* - A remote host is scanned only while its tab is open, on a lease borrowed
|
|
22
|
+
* from the pool the terminal and file browser already keep warm.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import type { PortScanResult } from '$shared/types/ports';
|
|
26
|
+
import { LOCAL_PORT_HOST } from '$shared/types/ports';
|
|
27
|
+
import { HostPortScanner, localPrincipal, type ScanPrincipal } from './scan-host';
|
|
28
|
+
import {
|
|
29
|
+
LocalCommandRunner,
|
|
30
|
+
SshCommandRunner,
|
|
31
|
+
detectRemotePlatform,
|
|
32
|
+
forgetRemotePlatform,
|
|
33
|
+
localPlatform,
|
|
34
|
+
type CommandRunner,
|
|
35
|
+
type ProbePlatform
|
|
36
|
+
} from '../host/runner';
|
|
37
|
+
import { sshClientPool, type SshLease } from '../ssh/client-pool';
|
|
38
|
+
import { forgetClopenRootPid } from './ssh-lineage';
|
|
39
|
+
import { sshConnectionQueries } from '../database/queries';
|
|
40
|
+
import { ws } from '../utils/ws';
|
|
41
|
+
import { debug } from '$shared/utils/logger';
|
|
42
|
+
|
|
43
|
+
/** Panel open on this machine: a scan is milliseconds, so it can be brisk. */
|
|
44
|
+
const WATCH_INTERVAL_MS = 1_000;
|
|
45
|
+
/**
|
|
46
|
+
* Panel open on an SSH host. Every scan there is a round trip over the same
|
|
47
|
+
* connection the terminal and file browser are using, so it is paced to stay
|
|
48
|
+
* out of their way while still reading as live.
|
|
49
|
+
*/
|
|
50
|
+
const REMOTE_WATCH_INTERVAL_MS = 3_000;
|
|
51
|
+
/** Panel closed: only the sidebar count, and only when a terminal is running. */
|
|
52
|
+
const BADGE_INTERVAL_MS = 15_000;
|
|
53
|
+
|
|
54
|
+
interface HostState {
|
|
55
|
+
scanner: HostPortScanner;
|
|
56
|
+
runner: CommandRunner;
|
|
57
|
+
platform: ProbePlatform;
|
|
58
|
+
lease: SshLease | null;
|
|
59
|
+
/** User ids currently looking at this host. */
|
|
60
|
+
watchers: Set<string>;
|
|
61
|
+
timer: ReturnType<typeof setInterval> | null;
|
|
62
|
+
last: PortScanResult | null;
|
|
63
|
+
signature: string;
|
|
64
|
+
scanning: boolean;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* What a client would notice changing. Deliberately excludes the timestamp and
|
|
69
|
+
* live counters like cpu, so an idle machine produces an identical signature
|
|
70
|
+
* tick after tick and nothing is sent.
|
|
71
|
+
*/
|
|
72
|
+
function signatureOf(result: PortScanResult): string {
|
|
73
|
+
if (result.error) return `error:${result.error}`;
|
|
74
|
+
return result.entries
|
|
75
|
+
.map((entry) => `${entry.key}|${entry.origin.label}|${entry.peerCount}|${entry.publicUrl ?? ''}|${entry.workerPids.length}`)
|
|
76
|
+
.join('\n');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
class PortMonitor {
|
|
80
|
+
private hosts = new Map<string, HostState>();
|
|
81
|
+
/**
|
|
82
|
+
* Hosts being set up right now. Creating a host acquires an SSH lease, so two
|
|
83
|
+
* concurrent callers would each take one and only the last would be kept —
|
|
84
|
+
* the other lease then leaks for the life of the process, holding a
|
|
85
|
+
* connection open that nothing will ever release.
|
|
86
|
+
*/
|
|
87
|
+
private opening = new Map<string, Promise<HostState>>();
|
|
88
|
+
private badgeTimer: ReturnType<typeof setInterval> | null = null;
|
|
89
|
+
private badgeCount = 0;
|
|
90
|
+
|
|
91
|
+
// -- host lifecycle ------------------------------------------------------
|
|
92
|
+
|
|
93
|
+
/** The host's state, created once however many callers ask at the same time. */
|
|
94
|
+
private async ensureHost(hostId: string): Promise<HostState> {
|
|
95
|
+
const existing = this.hosts.get(hostId);
|
|
96
|
+
if (existing) return existing;
|
|
97
|
+
|
|
98
|
+
const opening = this.opening.get(hostId);
|
|
99
|
+
if (opening) return opening;
|
|
100
|
+
|
|
101
|
+
const created = this.createHost(hostId)
|
|
102
|
+
.then((state) => {
|
|
103
|
+
// Another caller may have finished first; keep theirs and let this
|
|
104
|
+
// one's lease go rather than overwriting it.
|
|
105
|
+
const settled = this.hosts.get(hostId);
|
|
106
|
+
if (settled) {
|
|
107
|
+
state.lease?.release();
|
|
108
|
+
return settled;
|
|
109
|
+
}
|
|
110
|
+
this.hosts.set(hostId, state);
|
|
111
|
+
return state;
|
|
112
|
+
})
|
|
113
|
+
.finally(() => {
|
|
114
|
+
this.opening.delete(hostId);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
this.opening.set(hostId, created);
|
|
118
|
+
return created;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
private async createHost(hostId: string): Promise<HostState> {
|
|
122
|
+
if (hostId === LOCAL_PORT_HOST) {
|
|
123
|
+
const runner = new LocalCommandRunner();
|
|
124
|
+
const platform = localPlatform();
|
|
125
|
+
return {
|
|
126
|
+
scanner: new HostPortScanner(hostId, 'this machine', runner, platform, localPrincipal()),
|
|
127
|
+
runner,
|
|
128
|
+
platform,
|
|
129
|
+
lease: null,
|
|
130
|
+
watchers: new Set(),
|
|
131
|
+
timer: null,
|
|
132
|
+
last: null,
|
|
133
|
+
signature: '',
|
|
134
|
+
scanning: false
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const connection = sshConnectionQueries.get(hostId);
|
|
139
|
+
if (!connection) throw new Error('That SSH host no longer exists.');
|
|
140
|
+
|
|
141
|
+
const lease = await sshClientPool.acquire(hostId);
|
|
142
|
+
try {
|
|
143
|
+
const runner = new SshCommandRunner(connection.name, lease.client);
|
|
144
|
+
const platform = await detectRemotePlatform(hostId, runner);
|
|
145
|
+
const principal: ScanPrincipal = {
|
|
146
|
+
user: connection.username,
|
|
147
|
+
isRoot: connection.username === 'root'
|
|
148
|
+
};
|
|
149
|
+
return {
|
|
150
|
+
scanner: new HostPortScanner(hostId, connection.name, runner, platform, principal),
|
|
151
|
+
runner,
|
|
152
|
+
platform,
|
|
153
|
+
lease,
|
|
154
|
+
watchers: new Set(),
|
|
155
|
+
timer: null,
|
|
156
|
+
last: null,
|
|
157
|
+
signature: '',
|
|
158
|
+
scanning: false
|
|
159
|
+
};
|
|
160
|
+
} catch (error) {
|
|
161
|
+
lease.release();
|
|
162
|
+
throw error;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
private disposeHost(hostId: string): void {
|
|
167
|
+
const state = this.hosts.get(hostId);
|
|
168
|
+
if (!state) return;
|
|
169
|
+
if (state.timer) clearInterval(state.timer);
|
|
170
|
+
state.lease?.release();
|
|
171
|
+
this.hosts.delete(hostId);
|
|
172
|
+
if (hostId !== LOCAL_PORT_HOST) {
|
|
173
|
+
forgetRemotePlatform(hostId);
|
|
174
|
+
forgetClopenRootPid(hostId);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// -- scanning ------------------------------------------------------------
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Run one scan and push it to watchers if anything changed. Overlapping
|
|
182
|
+
* ticks are dropped rather than queued: a slow host would otherwise build a
|
|
183
|
+
* backlog of scans that are all stale by the time they run.
|
|
184
|
+
*/
|
|
185
|
+
private async tick(hostId: string): Promise<void> {
|
|
186
|
+
const state = this.hosts.get(hostId);
|
|
187
|
+
if (!state || state.scanning) return;
|
|
188
|
+
|
|
189
|
+
state.scanning = true;
|
|
190
|
+
try {
|
|
191
|
+
const result = await state.scanner.scan();
|
|
192
|
+
state.last = result;
|
|
193
|
+
|
|
194
|
+
if (hostId === LOCAL_PORT_HOST) this.publishBadge(countSessionPorts(result));
|
|
195
|
+
|
|
196
|
+
const signature = signatureOf(result);
|
|
197
|
+
if (signature === state.signature) return;
|
|
198
|
+
state.signature = signature;
|
|
199
|
+
|
|
200
|
+
for (const userId of state.watchers) {
|
|
201
|
+
ws.emit.user(userId, 'ports:changed', { result });
|
|
202
|
+
}
|
|
203
|
+
} catch (error) {
|
|
204
|
+
debug.warn('ports', `scan failed for ${hostId}:`, error);
|
|
205
|
+
} finally {
|
|
206
|
+
state.scanning = false;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
private ensureTimer(hostId: string): void {
|
|
211
|
+
const state = this.hosts.get(hostId);
|
|
212
|
+
if (!state || state.timer) return;
|
|
213
|
+
state.timer = setInterval(
|
|
214
|
+
() => {
|
|
215
|
+
void this.tick(hostId);
|
|
216
|
+
},
|
|
217
|
+
hostId === LOCAL_PORT_HOST ? WATCH_INTERVAL_MS : REMOTE_WATCH_INTERVAL_MS
|
|
218
|
+
);
|
|
219
|
+
// A poll for a panel nobody is looking at must not hold the process open.
|
|
220
|
+
state.timer.unref?.();
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// -- public API ----------------------------------------------------------
|
|
224
|
+
|
|
225
|
+
/** Start watching a host for a user, and return the first scan immediately. */
|
|
226
|
+
async watch(hostId: string, userId: string): Promise<PortScanResult> {
|
|
227
|
+
const state = await this.ensureHost(hostId);
|
|
228
|
+
|
|
229
|
+
state.watchers.add(userId);
|
|
230
|
+
|
|
231
|
+
// Scan before starting the timer, and through the scanner's own guard, so
|
|
232
|
+
// this first read cannot run alongside a tick on the same connection.
|
|
233
|
+
const result = state.last ?? (await state.scanner.scan());
|
|
234
|
+
state.last = result;
|
|
235
|
+
state.signature = signatureOf(result);
|
|
236
|
+
if (hostId === LOCAL_PORT_HOST) this.publishBadge(countSessionPorts(result));
|
|
237
|
+
|
|
238
|
+
this.ensureTimer(hostId);
|
|
239
|
+
return result;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** Stop watching. The host is torn down once nobody is left looking. */
|
|
243
|
+
unwatch(hostId: string, userId: string): void {
|
|
244
|
+
const state = this.hosts.get(hostId);
|
|
245
|
+
if (!state) return;
|
|
246
|
+
|
|
247
|
+
state.watchers.delete(userId);
|
|
248
|
+
if (state.watchers.size > 0) return;
|
|
249
|
+
|
|
250
|
+
// The local host keeps its scanner: the badge ticker reuses the process
|
|
251
|
+
// cache, and rebuilding it costs a full sweep.
|
|
252
|
+
if (hostId === LOCAL_PORT_HOST) {
|
|
253
|
+
if (state.timer) clearInterval(state.timer);
|
|
254
|
+
state.timer = null;
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
this.disposeHost(hostId);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** A one-shot scan, for a host the caller is not watching. */
|
|
261
|
+
async scanOnce(hostId: string): Promise<PortScanResult> {
|
|
262
|
+
const existing = this.hosts.get(hostId);
|
|
263
|
+
if (existing) return existing.scanner.scan();
|
|
264
|
+
|
|
265
|
+
const state = await this.createHost(hostId);
|
|
266
|
+
try {
|
|
267
|
+
return await state.scanner.scan();
|
|
268
|
+
} finally {
|
|
269
|
+
state.lease?.release();
|
|
270
|
+
if (hostId !== LOCAL_PORT_HOST) {
|
|
271
|
+
forgetRemotePlatform(hostId);
|
|
272
|
+
forgetClopenRootPid(hostId);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** The runner and platform for a host, so an action can reuse the transport. */
|
|
278
|
+
async withHost<T>(hostId: string, fn: (runner: CommandRunner, platform: ProbePlatform) => Promise<T>): Promise<T> {
|
|
279
|
+
const existing = this.hosts.get(hostId);
|
|
280
|
+
if (existing) return fn(existing.runner, existing.platform);
|
|
281
|
+
|
|
282
|
+
const state = await this.createHost(hostId);
|
|
283
|
+
try {
|
|
284
|
+
return await fn(state.runner, state.platform);
|
|
285
|
+
} finally {
|
|
286
|
+
state.lease?.release();
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** Force the next tick to push, after an action that changed the table. */
|
|
291
|
+
invalidate(hostId: string): void {
|
|
292
|
+
const state = this.hosts.get(hostId);
|
|
293
|
+
if (state) state.signature = '';
|
|
294
|
+
void this.tick(hostId);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// -- sidebar count -------------------------------------------------------
|
|
298
|
+
|
|
299
|
+
private publishBadge(count: number): void {
|
|
300
|
+
if (count === this.badgeCount) return;
|
|
301
|
+
this.badgeCount = count;
|
|
302
|
+
ws.emit.global('ports:badge', { count });
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* The count behind the sidebar dot: ports Clopen started on this machine.
|
|
307
|
+
*
|
|
308
|
+
* Returned as well as published, because a client that has just loaded needs
|
|
309
|
+
* the current number and pushes only carry changes — a page refresh would
|
|
310
|
+
* otherwise sit at zero until something happened to move it.
|
|
311
|
+
*/
|
|
312
|
+
async refreshBadge(): Promise<number> {
|
|
313
|
+
const local = this.hosts.get(LOCAL_PORT_HOST);
|
|
314
|
+
// A watched panel is already scanning; reuse what it last saw.
|
|
315
|
+
if (local?.timer && local.last) return countSessionPorts(local.last);
|
|
316
|
+
|
|
317
|
+
try {
|
|
318
|
+
const state = await this.ensureHost(LOCAL_PORT_HOST);
|
|
319
|
+
const result = await state.scanner.scan();
|
|
320
|
+
state.last = result;
|
|
321
|
+
const count = countSessionPorts(result);
|
|
322
|
+
this.publishBadge(count);
|
|
323
|
+
return count;
|
|
324
|
+
} catch (error) {
|
|
325
|
+
debug.warn('ports', 'badge scan failed:', error);
|
|
326
|
+
return this.badgeCount;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* The periodic count.
|
|
332
|
+
*
|
|
333
|
+
* This used to skip the scan when no terminal session existed, on the
|
|
334
|
+
* grounds that nothing could then have been started from Clopen. That stops
|
|
335
|
+
* being true once the count includes everything Clopen spawns — an engine,
|
|
336
|
+
* an MCP server, the preview browser — none of which need a terminal. The
|
|
337
|
+
* guard would have reported a confident zero over a running engine, so it
|
|
338
|
+
* is gone: a scan every fifteen seconds costs about fifteen milliseconds.
|
|
339
|
+
*/
|
|
340
|
+
private async badgeTick(): Promise<void> {
|
|
341
|
+
const local = this.hosts.get(LOCAL_PORT_HOST);
|
|
342
|
+
if (local?.timer) return; // A watched panel already refreshes the count.
|
|
343
|
+
await this.refreshBadge();
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
start(): void {
|
|
347
|
+
if (this.badgeTimer) return;
|
|
348
|
+
this.badgeTimer = setInterval(() => {
|
|
349
|
+
void this.badgeTick();
|
|
350
|
+
}, BADGE_INTERVAL_MS);
|
|
351
|
+
this.badgeTimer.unref?.();
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
stop(): void {
|
|
355
|
+
if (this.badgeTimer) clearInterval(this.badgeTimer);
|
|
356
|
+
this.badgeTimer = null;
|
|
357
|
+
for (const hostId of [...this.hosts.keys()]) this.disposeHost(hostId);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
function countSessionPorts(result: PortScanResult): number {
|
|
362
|
+
return result.entries.filter((entry) => entry.origin.kind === 'session').length;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
export const portMonitor = new PortMonitor();
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test';
|
|
2
|
+
import { parseLsofCwd, parseProcCwd, parsePsOutput, parseWindowsProcesses } from './processes';
|
|
3
|
+
|
|
4
|
+
describe('parsePsOutput', () => {
|
|
5
|
+
// `ps -o pid=,ppid=,user=,lstart=,args=` — lstart is always five tokens, so
|
|
6
|
+
// the row is read positionally and the argv keeps its own spaces.
|
|
7
|
+
const output = [
|
|
8
|
+
' 1 0 root Tue Aug 25 08:24:48 2026 /sbin/launchd',
|
|
9
|
+
' 1310 1200 deploy Tue Aug 25 09:02:11 2026 node /srv/app/node_modules/.bin/vite --port 5173',
|
|
10
|
+
' 640 1 root Tue Aug 25 08:24:52 2026 nginx: master process /usr/sbin/nginx -g daemon off;',
|
|
11
|
+
''
|
|
12
|
+
].join('\n');
|
|
13
|
+
|
|
14
|
+
const processes = parsePsOutput(output);
|
|
15
|
+
|
|
16
|
+
test('reads every row', () => {
|
|
17
|
+
expect(processes).toHaveLength(3);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test('keeps the full command line including its arguments', () => {
|
|
21
|
+
expect(processes[1].command).toBe('node /srv/app/node_modules/.bin/vite --port 5173');
|
|
22
|
+
expect(processes[2].command).toBe('nginx: master process /usr/sbin/nginx -g daemon off;');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test('reads pid, parent and user', () => {
|
|
26
|
+
expect(processes[1]).toMatchObject({ pid: 1310, parentPid: 1200, user: 'deploy' });
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('turns the five lstart tokens into a timestamp', () => {
|
|
30
|
+
expect(processes[0].startedAt).toBe(new Date('Tue Aug 25 08:24:48 2026').toISOString());
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test('reports no start time rather than a wrong one when the date is unreadable', () => {
|
|
34
|
+
// A non-English locale prints month names `Date` cannot parse.
|
|
35
|
+
const localised = ' 900 1 root mar. ao\u00fbt 25 08:24:48 2026 /usr/sbin/cron';
|
|
36
|
+
expect(parsePsOutput(localised)[0]).toMatchObject({ pid: 900, startedAt: null });
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('skips rows too short to be a process', () => {
|
|
40
|
+
expect(parsePsOutput('garbage\n\n 1 0 root\n')).toHaveLength(0);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
describe('parseWindowsProcesses', () => {
|
|
45
|
+
const output = [
|
|
46
|
+
'ProcessId : 1044',
|
|
47
|
+
'ParentProcessId : 640',
|
|
48
|
+
'Name : node.exe',
|
|
49
|
+
'CommandLine : "C:\\Program Files\\nodejs\\node.exe" server.js',
|
|
50
|
+
'CreationDate : 2026-08-25T09:02:11',
|
|
51
|
+
'',
|
|
52
|
+
'ProcessId : 4',
|
|
53
|
+
'ParentProcessId : 0',
|
|
54
|
+
'Name : System',
|
|
55
|
+
'CommandLine :',
|
|
56
|
+
'CreationDate :',
|
|
57
|
+
''
|
|
58
|
+
].join('\n');
|
|
59
|
+
|
|
60
|
+
const processes = parseWindowsProcesses(output);
|
|
61
|
+
|
|
62
|
+
test('reads one process per blank-line-separated block', () => {
|
|
63
|
+
expect(processes).toHaveLength(2);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test('prefers the command line over the bare name', () => {
|
|
67
|
+
expect(processes[0].command).toBe('"C:\\Program Files\\nodejs\\node.exe" server.js');
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test('falls back to the name when there is no command line', () => {
|
|
71
|
+
expect(processes[1].command).toBe('System');
|
|
72
|
+
expect(processes[1].startedAt).toBeNull();
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe('cwd probes', () => {
|
|
77
|
+
test('parses lsof cwd blocks', () => {
|
|
78
|
+
const output = ['p1310', 'fcwd', 'n/srv/app', 'p1400', 'fcwd', 'n/srv/api', ''].join('\n');
|
|
79
|
+
expect([...parseLsofCwd(output).entries()]).toEqual([
|
|
80
|
+
[1310, '/srv/app'],
|
|
81
|
+
[1400, '/srv/api']
|
|
82
|
+
]);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test('parses /proc symlink listings and ignores rows it could not read', () => {
|
|
86
|
+
const output = [
|
|
87
|
+
'lrwxrwxrwx 1 deploy deploy 0 Aug 25 09:02 /proc/1310/cwd -> /srv/app',
|
|
88
|
+
"ls: cannot read symbolic link '/proc/800/cwd': Permission denied",
|
|
89
|
+
'lrwxrwxrwx 1 deploy deploy 0 Aug 25 09:02 /proc/1400/cwd -> /srv/api',
|
|
90
|
+
''
|
|
91
|
+
].join('\n');
|
|
92
|
+
|
|
93
|
+
const cwds = parseProcCwd(output);
|
|
94
|
+
expect(cwds.get(1310)).toBe('/srv/app');
|
|
95
|
+
expect(cwds.get(1400)).toBe('/srv/api');
|
|
96
|
+
expect(cwds.has(800)).toBe(false);
|
|
97
|
+
});
|
|
98
|
+
});
|