1import {
2 AdvertisedEndpoint,
3 DesktopServerExposureModeSchema,
4 DesktopServerExposureStateSchema,
5} from "@t3tools/contracts";
6import * as Effect from "effect/Effect";
7import * as Schema from "effect/Schema";
8
9import * as DesktopLifecycle from "../../app/DesktopLifecycle.ts";
10import * as DesktopServerExposure from "../../backend/DesktopServerExposure.ts";
11import * as IpcChannels from "../channels.ts";
12import * as DesktopIpc from "../DesktopIpc.ts";
13
14const SetTailscaleServeEnabledInput = Schema.Struct({
15 enabled: Schema.Boolean,
16 port: Schema.optionalKey(Schema.Number),
17});
18
19export const getServerExposureState = DesktopIpc.makeIpcMethod({
20 channel: IpcChannels.GET_SERVER_EXPOSURE_STATE_CHANNEL,
21 payload: Schema.Void,
22 result: DesktopServerExposureStateSchema,
23 handler: Effect.fn("desktop.ipc.serverExposure.getState")(function* () {
24 const serverExposure = yield* DesktopServerExposure.DesktopServerExposure;
25 return yield* serverExposure.getState;
26 }),
27});
28
29export const setServerExposureMode = DesktopIpc.makeIpcMethod({
30 channel: IpcChannels.SET_SERVER_EXPOSURE_MODE_CHANNEL,
31 payload: DesktopServerExposureModeSchema,
32 result: DesktopServerExposureStateSchema,
33 handler: Effect.fn("desktop.ipc.serverExposure.setMode")(function* (mode) {
34 const lifecycle = yield* DesktopLifecycle.DesktopLifecycle;
35 const serverExposure = yield* DesktopServerExposure.DesktopServerExposure;
36 const change = yield* serverExposure.setMode(mode);
37 if (change.requiresRelaunch) {
38 yield* lifecycle.relaunch(`serverExposureMode=${mode}`);
39 }
40 return change.state;
41 }),
42});
43
44export const setTailscaleServeEnabled = DesktopIpc.makeIpcMethod({
45 channel: IpcChannels.SET_TAILSCALE_SERVE_ENABLED_CHANNEL,
46 payload: SetTailscaleServeEnabledInput,
47 result: DesktopServerExposureStateSchema,
48 handler: Effect.fn("desktop.ipc.serverExposure.setTailscaleServeEnabled")(function* (input) {
49 const lifecycle = yield* DesktopLifecycle.DesktopLifecycle;
50 const serverExposure = yield* DesktopServerExposure.DesktopServerExposure;
51 const change = yield* serverExposure.setTailscaleServeEnabled(input);
52 if (change.requiresRelaunch) {
53 yield* lifecycle.relaunch(
54 change.state.tailscaleServeEnabled ? "tailscale-serve-enabled" : "tailscale-serve-disabled",
55 );
56 }
57 return change.state;
58 }),
59});
60
61export const getAdvertisedEndpoints = DesktopIpc.makeIpcMethod({
62 channel: IpcChannels.GET_ADVERTISED_ENDPOINTS_CHANNEL,
63 payload: Schema.Void,
64 result: Schema.Array(AdvertisedEndpoint),
65 handler: Effect.fn("desktop.ipc.serverExposure.getAdvertisedEndpoints")(function* () {
66 const serverExposure = yield* DesktopServerExposure.DesktopServerExposure;
67 return yield* serverExposure.getAdvertisedEndpoints;
68 }),
69});