Skip to content

Commit b0bc101

Browse files
Refactor process inspector control flow
- Reformat terminal process inspection branching - Simplify web port probing and error handling
1 parent 037a771 commit b0bc101

2 files changed

Lines changed: 69 additions & 69 deletions

File tree

‎apps/server/src/process/Layers/TerminalProcessInspector.ts‎

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -175,51 +175,51 @@ const makeTerminalProcessInspector = Effect.gen(function* () {
175175
);
176176
});
177177

178-
const inspect: TerminalProcessInspectorShape["inspect"] = Effect.fn("process.inspect")(
179-
function* (terminalPid: number) {
180-
if (!Number.isInteger(terminalPid) || terminalPid <= 0) {
181-
return {
182-
hasRunningSubprocess: false,
183-
runningPorts: [],
184-
} satisfies TerminalSubprocessActivity;
185-
}
186-
187-
if (process.platform === "win32") {
188-
const childPids = yield* collectWindowsChildPids(terminalPid, runInspectorCommand);
189-
const processPidsForPortScan = [terminalPid, ...childPids];
190-
const runningPorts = yield* checkWindowsListeningPorts(processPidsForPortScan, {
191-
terminalPid,
192-
runCommand: runInspectorCommand,
193-
});
194-
return {
195-
hasRunningSubprocess: childPids.length > 0 || runningPorts.length > 0,
196-
runningPorts,
197-
} satisfies TerminalSubprocessActivity;
198-
}
199-
200-
const processFamilyPids = yield* collectPosixProcessFamilyPids(
201-
terminalPid,
202-
runInspectorCommand,
203-
);
204-
if (processFamilyPids.length === 0) {
205-
return {
206-
hasRunningSubprocess: false,
207-
runningPorts: [],
208-
} satisfies TerminalSubprocessActivity;
209-
}
178+
const inspect: TerminalProcessInspectorShape["inspect"] = Effect.fn("process.inspect")(function* (
179+
terminalPid: number,
180+
) {
181+
if (!Number.isInteger(terminalPid) || terminalPid <= 0) {
182+
return {
183+
hasRunningSubprocess: false,
184+
runningPorts: [],
185+
} satisfies TerminalSubprocessActivity;
186+
}
210187

211-
const subprocessPids = processFamilyPids.filter((pid: number) => pid !== terminalPid);
212-
const runningPorts = yield* checkPosixListeningPorts(processFamilyPids, {
188+
if (process.platform === "win32") {
189+
const childPids = yield* collectWindowsChildPids(terminalPid, runInspectorCommand);
190+
const processPidsForPortScan = [terminalPid, ...childPids];
191+
const runningPorts = yield* checkWindowsListeningPorts(processPidsForPortScan, {
213192
terminalPid,
214193
runCommand: runInspectorCommand,
215-
platform: process.platform,
216194
});
217195
return {
218-
hasRunningSubprocess: subprocessPids.length > 0 || runningPorts.length > 0,
196+
hasRunningSubprocess: childPids.length > 0 || runningPorts.length > 0,
219197
runningPorts,
220198
} satisfies TerminalSubprocessActivity;
221-
},
222-
);
199+
}
200+
201+
const processFamilyPids = yield* collectPosixProcessFamilyPids(
202+
terminalPid,
203+
runInspectorCommand,
204+
);
205+
if (processFamilyPids.length === 0) {
206+
return {
207+
hasRunningSubprocess: false,
208+
runningPorts: [],
209+
} satisfies TerminalSubprocessActivity;
210+
}
211+
212+
const subprocessPids = processFamilyPids.filter((pid: number) => pid !== terminalPid);
213+
const runningPorts = yield* checkPosixListeningPorts(processFamilyPids, {
214+
terminalPid,
215+
runCommand: runInspectorCommand,
216+
platform: process.platform,
217+
});
218+
return {
219+
hasRunningSubprocess: subprocessPids.length > 0 || runningPorts.length > 0,
220+
runningPorts,
221+
} satisfies TerminalSubprocessActivity;
222+
});
223223

224224
return {
225225
inspect,

‎apps/server/src/process/Layers/WebPortInspector.ts‎

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -139,37 +139,37 @@ const makeWebPortInspector = Effect.gen(function* () {
139139
);
140140
});
141141

142-
const inspect: WebPortInspectorShape["inspect"] = Effect.fn("process.inspectWebPort")(
143-
function* (port: number) {
144-
if (!Number.isInteger(port) || port <= 0 || port > 65_535) {
145-
return yield* new WebPortInspectionError({
146-
port,
147-
host: "127.0.0.1",
148-
detail: "Port must be an integer between 1 and 65535.",
149-
});
150-
}
151-
152-
const ipv4Result = yield* probeWebPortOnHost(port, "127.0.0.1").pipe(Effect.exit);
153-
if (ipv4Result._tag === "Success" && isLikelyWebProbe(ipv4Result.value)) {
154-
return true;
155-
}
156-
157-
const ipv6Result = yield* probeWebPortOnHost(port, "::1").pipe(Effect.exit);
158-
if (ipv6Result._tag === "Success" && isLikelyWebProbe(ipv6Result.value)) {
159-
return true;
160-
}
161-
162-
if (ipv4Result._tag === "Success" || ipv6Result._tag === "Success") {
163-
return false;
164-
}
165-
166-
if (ipv6Result._tag === "Failure") {
167-
return yield* Effect.failCause(ipv6Result.cause);
168-
}
169-
170-
return yield* Effect.failCause(ipv4Result.cause);
171-
},
172-
);
142+
const inspect: WebPortInspectorShape["inspect"] = Effect.fn("process.inspectWebPort")(function* (
143+
port: number,
144+
) {
145+
if (!Number.isInteger(port) || port <= 0 || port > 65_535) {
146+
return yield* new WebPortInspectionError({
147+
port,
148+
host: "127.0.0.1",
149+
detail: "Port must be an integer between 1 and 65535.",
150+
});
151+
}
152+
153+
const ipv4Result = yield* probeWebPortOnHost(port, "127.0.0.1").pipe(Effect.exit);
154+
if (ipv4Result._tag === "Success" && isLikelyWebProbe(ipv4Result.value)) {
155+
return true;
156+
}
157+
158+
const ipv6Result = yield* probeWebPortOnHost(port, "::1").pipe(Effect.exit);
159+
if (ipv6Result._tag === "Success" && isLikelyWebProbe(ipv6Result.value)) {
160+
return true;
161+
}
162+
163+
if (ipv4Result._tag === "Success" || ipv6Result._tag === "Success") {
164+
return false;
165+
}
166+
167+
if (ipv6Result._tag === "Failure") {
168+
return yield* Effect.failCause(ipv6Result.cause);
169+
}
170+
171+
return yield* Effect.failCause(ipv4Result.cause);
172+
});
173173

174174
return {
175175
inspect,

0 commit comments

Comments
 (0)