Skip to content

Commit 3024429

Browse files
refactor(desktop): read Windows interfaces via DesktopNetworkInterfaces
The distro-IP probe called node:os networkInterfaces() imperatively inside the service implementation, hiding the dependency from the layer's requirements and making it unsubstitutable in tests. Acquire the existing DesktopNetworkInterfaces service in the layer, pass its read effect into the probe, and keep the IPv4 flattening as a pure helper over the returned NetworkInterfaces map.
1 parent 3fb1ece commit 3024429

3 files changed

Lines changed: 44 additions & 11 deletions

File tree

‎apps/desktop/src/backend/DesktopBackendConfiguration.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { ChildProcessSpawner } from "effect/unstable/process";
1414
import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
1515
import * as DesktopBackendConfiguration from "./DesktopBackendConfiguration.ts";
1616
import * as DesktopConfig from "../app/DesktopConfig.ts";
17+
import * as DesktopNetworkInterfaces from "./DesktopNetworkInterfaces.ts";
1718
import * as DesktopServerExposure from "./DesktopServerExposure.ts";
1819
import * as DesktopAppSettings from "../settings/DesktopAppSettings.ts";
1920
import * as DesktopWslEnvironment from "../wsl/DesktopWslEnvironment.ts";
@@ -965,6 +966,11 @@ describe("DesktopBackendConfiguration", () => {
965966
Layer.provideMerge(DesktopAppSettings.layerTest()),
966967
Layer.provideMerge(DesktopWslServerTree.layerTest()),
967968
Layer.provideMerge(DesktopWslEnvironment.layer),
969+
Layer.provideMerge(
970+
Layer.succeed(DesktopNetworkInterfaces.DesktopNetworkInterfaces, {
971+
read: Effect.succeed({}),
972+
}),
973+
),
968974
// isAvailable on win32 only touches the filesystem, never the spawner,
969975
// so a die-stub is enough to satisfy the layer's deps.
970976
Layer.provideMerge(

‎apps/desktop/src/wsl/DesktopWslEnvironment.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
parseToolchainReport,
2323
pickDistroIp,
2424
probeWslDistros,
25+
windowsIpv4Interfaces,
2526
} from "./DesktopWslEnvironment.ts";
2627

2728
const encoder = new TextEncoder();
@@ -152,6 +153,24 @@ describe("pickDistroIp", () => {
152153
});
153154
});
154155

156+
describe("windowsIpv4Interfaces", () => {
157+
it("flattens IPv4 entries across adapters, accepting string and numeric family", () => {
158+
expect(
159+
windowsIpv4Interfaces({
160+
"vEthernet (WSL)": [
161+
{ address: "172.27.0.1", family: "IPv4", internal: false, netmask: "255.255.240.0" },
162+
{ address: "fe80::1", family: "IPv6", internal: false, netmask: "ffff:ffff:ffff:ffff::" },
163+
],
164+
"Wi-Fi": [{ address: "192.168.1.219", family: 4, internal: false }],
165+
Disconnected: undefined,
166+
}),
167+
).toEqual([
168+
{ address: "172.27.0.1", netmask: "255.255.240.0" },
169+
{ address: "192.168.1.219", netmask: undefined },
170+
]);
171+
});
172+
});
173+
155174
describe("formatNodePtyProbeFailureReason", () => {
156175
it("identifies a packaged build that omitted the Linux node-pty prebuild", () => {
157176
const reason = formatNodePtyProbeFailureReason(4);

‎apps/desktop/src/wsl/DesktopWslEnvironment.ts‎

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import * as NodeOS from "node:os";
2-
31
import * as Context from "effect/Context";
42
import * as Duration from "effect/Duration";
53
import * as Effect from "effect/Effect";
@@ -15,6 +13,7 @@ import { buildRemoteNodeEnvScript } from "@t3tools/ssh/tunnel";
1513
import { satisfiesSemverRange } from "@t3tools/shared/semver";
1614

1715
import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
16+
import * as DesktopNetworkInterfaces from "../backend/DesktopNetworkInterfaces.ts";
1817
import { parseWslDistroList, type WslDistro } from "./wslPathParsing.ts";
1918

2019
const PROCESS_TERMINATE_GRACE = Duration.seconds(1);
@@ -723,7 +722,7 @@ export const parseDistroIpCandidates = (stdout: string): ReadonlyArray<string> =
723722

724723
export interface WindowsIpv4Interface {
725724
readonly address: string;
726-
readonly netmask: string;
725+
readonly netmask: string | undefined;
727726
}
728727

729728
const ipv4ToInt = (ip: string): number | null => {
@@ -759,31 +758,36 @@ export const pickDistroIp = (
759758
for (const candidate of candidates) {
760759
for (const iface of windowsInterfaces) {
761760
if (candidate === iface.address) return candidate;
762-
if (inSameSubnet(candidate, iface.address, iface.netmask)) return candidate;
761+
if (iface.netmask !== undefined && inSameSubnet(candidate, iface.address, iface.netmask)) {
762+
return candidate;
763+
}
763764
}
764765
}
765766
return candidates[0] ?? null;
766767
};
767768

768-
const windowsIpv4Interfaces = (): ReadonlyArray<WindowsIpv4Interface> => {
769-
const interfaces: WindowsIpv4Interface[] = [];
770-
for (const list of Object.values(NodeOS.networkInterfaces())) {
769+
export const windowsIpv4Interfaces = (
770+
interfaces: DesktopNetworkInterfaces.NetworkInterfaces,
771+
): ReadonlyArray<WindowsIpv4Interface> => {
772+
const flattened: WindowsIpv4Interface[] = [];
773+
for (const list of Object.values(interfaces)) {
771774
if (!list) continue;
772775
for (const entry of list) {
773776
// Same family normalization as isLocalHostIpv4 in
774777
// DesktopBackendConfiguration: Electron's Node reports the string
775778
// "IPv4", some Node builds report the numeric 4.
776779
const family = String(entry.family);
777780
if (family === "IPv4" || family === "4") {
778-
interfaces.push({ address: entry.address, netmask: entry.netmask });
781+
flattened.push({ address: entry.address, netmask: entry.netmask });
779782
}
780783
}
781784
}
782-
return interfaces;
785+
return flattened;
783786
};
784787

785788
const getDistroIpImpl = (
786789
distro: string | null,
790+
readNetworkInterfaces: Effect.Effect<DesktopNetworkInterfaces.NetworkInterfaces>,
787791
): Effect.Effect<Option.Option<string>, never, ChildProcessSpawner.ChildProcessSpawner> =>
788792
Effect.scoped(
789793
Effect.gen(function* () {
@@ -805,7 +809,8 @@ const getDistroIpImpl = (
805809
if ((exitCode as unknown as number) !== 0) return Option.none<string>();
806810
const raw = decodeUtf8(concatChunks(stdoutBytes)).trim();
807811
const candidates = parseDistroIpCandidates(raw);
808-
const chosen = pickDistroIp(candidates, windowsIpv4Interfaces());
812+
const interfaces = yield* readNetworkInterfaces;
813+
const chosen = pickDistroIp(candidates, windowsIpv4Interfaces(interfaces));
809814
yield* Effect.log(
810815
`[wsl] distro IP probe chose ${chosen ?? "none"} from candidates [${candidates.join(", ")}]`,
811816
);
@@ -914,6 +919,7 @@ export const layer = Layer.effect(
914919
const environment = yield* DesktopEnvironment.DesktopEnvironment;
915920
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
916921
const fileSystem = yield* FileSystem.FileSystem;
922+
const networkInterfaces = yield* DesktopNetworkInterfaces.DesktopNetworkInterfaces;
917923
const windir = process.env.WINDIR ?? "C:\\Windows";
918924

919925
const provideSpawner = <A, E>(
@@ -960,7 +966,9 @@ export const layer = Layer.effect(
960966
}).pipe(Effect.withSpan("desktop.wsl.getUserHome"));
961967

962968
const getDistroIp = (distro: string | null) =>
963-
provideSpawner(getDistroIpImpl(distro)).pipe(Effect.withSpan("desktop.wsl.getDistroIp"));
969+
provideSpawner(getDistroIpImpl(distro, networkInterfaces.read)).pipe(
970+
Effect.withSpan("desktop.wsl.getDistroIp"),
971+
);
964972

965973
const probeDistros = provideSpawner(probeWslDistros).pipe(
966974
Effect.withSpan("desktop.wsl.probeDistros"),

0 commit comments

Comments
 (0)