2026-04-19 14:18:20 +08:00
|
|
|
import { contextBridge, ipcRenderer } from "electron/renderer";
|
2026-04-20 17:16:15 +08:00
|
|
|
import type { IpcRendererEvent } from "electron";
|
2026-04-20 09:52:48 +08:00
|
|
|
import type {
|
|
|
|
|
CreatePublishJobResponse,
|
|
|
|
|
DesktopAccountInfo,
|
|
|
|
|
DesktopPublishTaskListResponse,
|
|
|
|
|
DesktopRuntimeSessionSyncRequest,
|
|
|
|
|
ListDesktopPublishTasksParams,
|
|
|
|
|
} from "@geo/shared-types";
|
2026-04-19 14:18:20 +08:00
|
|
|
import type { DesktopRuntimeSnapshot } from "../renderer/types";
|
2026-04-22 00:24:21 +08:00
|
|
|
import type { DesktopObservedRequest } from "../shared/network-debug";
|
2026-04-19 14:18:20 +08:00
|
|
|
|
2026-04-20 11:59:35 +08:00
|
|
|
interface DesktopDeviceInfo {
|
|
|
|
|
device_name: string;
|
|
|
|
|
os: string;
|
|
|
|
|
cpu_arch: string;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-27 20:06:37 +08:00
|
|
|
interface DesktopClientIDScope {
|
|
|
|
|
tenant_id: number;
|
|
|
|
|
workspace_id: number;
|
|
|
|
|
user_id: number;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-22 00:24:21 +08:00
|
|
|
const rendererProxyRequestChannel = "desktop:renderer-devtools-proxy:request";
|
|
|
|
|
const rendererProxyResponseChannel = "desktop:renderer-devtools-proxy:response";
|
|
|
|
|
|
|
|
|
|
ipcRenderer.on(
|
|
|
|
|
rendererProxyRequestChannel,
|
|
|
|
|
async (
|
|
|
|
|
_event,
|
|
|
|
|
payload: {
|
|
|
|
|
requestId?: unknown;
|
|
|
|
|
request?: {
|
|
|
|
|
url?: unknown;
|
|
|
|
|
method?: unknown;
|
|
|
|
|
headers?: unknown;
|
|
|
|
|
body?: unknown;
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
) => {
|
|
|
|
|
const requestId = typeof payload?.requestId === "string" ? payload.requestId : "";
|
|
|
|
|
const request = payload?.request;
|
|
|
|
|
if (!requestId || !request || typeof request !== "object") {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const url = typeof request.url === "string" ? request.url : "";
|
|
|
|
|
const method = typeof request.method === "string" ? request.method : "GET";
|
|
|
|
|
const headers = isStringRecord(request.headers) ? request.headers : {};
|
|
|
|
|
const body = typeof request.body === "string" ? request.body : undefined;
|
|
|
|
|
|
|
|
|
|
const response = await (async () => {
|
|
|
|
|
try {
|
|
|
|
|
const fetchResponse = await fetch(url, {
|
|
|
|
|
method,
|
|
|
|
|
headers,
|
|
|
|
|
body,
|
|
|
|
|
});
|
|
|
|
|
const bodyText = await fetchResponse.text();
|
|
|
|
|
return {
|
|
|
|
|
ok: fetchResponse.ok,
|
|
|
|
|
status: fetchResponse.status,
|
|
|
|
|
statusText: fetchResponse.statusText,
|
|
|
|
|
headers: Object.fromEntries(fetchResponse.headers.entries()),
|
|
|
|
|
bodyText,
|
|
|
|
|
};
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return {
|
|
|
|
|
ok: false,
|
|
|
|
|
status: 0,
|
|
|
|
|
statusText: "",
|
|
|
|
|
headers: {},
|
|
|
|
|
bodyText: "",
|
|
|
|
|
error: String(error && ((error as Error).message || error)),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
ipcRenderer.send(rendererProxyResponseChannel, {
|
|
|
|
|
requestId,
|
|
|
|
|
response,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2026-04-19 14:18:20 +08:00
|
|
|
const desktopBridge = {
|
|
|
|
|
app: {
|
|
|
|
|
ping: () => ipcRenderer.invoke("desktop:ping") as Promise<string>,
|
2026-04-20 11:59:35 +08:00
|
|
|
deviceInfo: () => ipcRenderer.invoke("desktop:device-info") as Promise<DesktopDeviceInfo>,
|
2026-04-27 20:06:37 +08:00
|
|
|
clientId: (scope: DesktopClientIDScope) =>
|
|
|
|
|
ipcRenderer.invoke("desktop:client-id", scope) as Promise<string>,
|
2026-04-19 14:18:20 +08:00
|
|
|
runtimeSnapshot: () =>
|
|
|
|
|
ipcRenderer.invoke("desktop:runtime-snapshot") as Promise<DesktopRuntimeSnapshot>,
|
2026-04-27 21:33:55 +08:00
|
|
|
runtimeAccountSnapshot: (accountId: string) =>
|
|
|
|
|
ipcRenderer.invoke("desktop:runtime-account-snapshot", accountId) as Promise<DesktopRuntimeSnapshot["accounts"][number] | null>,
|
2026-04-19 14:18:20 +08:00
|
|
|
bindPublishAccount: (platformId: string) =>
|
|
|
|
|
ipcRenderer.invoke("desktop:bind-publish-account", platformId) as Promise<DesktopAccountInfo>,
|
2026-04-20 09:52:48 +08:00
|
|
|
refreshRuntimeAccounts: () =>
|
|
|
|
|
ipcRenderer.invoke("desktop:refresh-runtime-accounts") as Promise<null>,
|
2026-04-27 21:33:55 +08:00
|
|
|
probeRuntimeAccount: (accountId: string) =>
|
|
|
|
|
ipcRenderer.invoke("desktop:probe-runtime-account", accountId) as Promise<DesktopRuntimeSnapshot["accounts"][number] | null>,
|
2026-04-19 14:18:20 +08:00
|
|
|
openPublishAccountConsole: (account: {
|
|
|
|
|
id: string;
|
|
|
|
|
platform: string;
|
|
|
|
|
platformUid: string;
|
|
|
|
|
displayName: string;
|
|
|
|
|
}) =>
|
|
|
|
|
ipcRenderer.invoke("desktop:open-publish-account-console", account) as Promise<null>,
|
2026-04-20 09:52:48 +08:00
|
|
|
unbindPublishAccount: (accountId: string, syncVersion: number) =>
|
|
|
|
|
ipcRenderer.invoke("desktop:unbind-publish-account", accountId, syncVersion) as Promise<null>,
|
|
|
|
|
listPublishTasks: (params?: ListDesktopPublishTasksParams) =>
|
|
|
|
|
ipcRenderer.invoke("desktop:list-publish-tasks", params) as Promise<DesktopPublishTaskListResponse>,
|
|
|
|
|
retryPublishTask: (taskId: string) =>
|
|
|
|
|
ipcRenderer.invoke("desktop:retry-publish-task", taskId) as Promise<CreatePublishJobResponse>,
|
2026-04-26 19:47:35 +08:00
|
|
|
openExternalUrl: (url: string) =>
|
|
|
|
|
ipcRenderer.invoke("desktop:open-external-url", url) as Promise<null>,
|
2026-04-19 14:18:20 +08:00
|
|
|
syncRuntimeSession: (session: DesktopRuntimeSessionSyncRequest | null) =>
|
|
|
|
|
ipcRenderer.invoke("desktop:runtime-session-sync", session) as Promise<null>,
|
2026-04-20 09:52:48 +08:00
|
|
|
releaseRuntimeSession: (revoke?: boolean) =>
|
|
|
|
|
ipcRenderer.invoke("desktop:runtime-session-release", revoke) as Promise<null>,
|
2026-04-22 00:24:21 +08:00
|
|
|
setWindowMode: (mode: "login" | "main") =>
|
|
|
|
|
ipcRenderer.invoke("desktop:set-window-mode", mode) as Promise<null>,
|
2026-04-20 17:16:15 +08:00
|
|
|
onRuntimeInvalidated: (
|
2026-04-27 21:33:55 +08:00
|
|
|
listener: (event: { reason: "account-health"; at: number; accountId?: string }) => void,
|
2026-04-20 17:16:15 +08:00
|
|
|
) => {
|
2026-04-27 21:33:55 +08:00
|
|
|
const wrapped = (_event: IpcRendererEvent, payload: { reason: "account-health"; at: number; accountId?: string }) => {
|
2026-04-20 17:16:15 +08:00
|
|
|
listener(payload);
|
|
|
|
|
};
|
|
|
|
|
ipcRenderer.on("desktop:runtime-invalidated", wrapped);
|
|
|
|
|
return () => {
|
|
|
|
|
ipcRenderer.off("desktop:runtime-invalidated", wrapped);
|
|
|
|
|
};
|
|
|
|
|
},
|
2026-04-22 00:24:21 +08:00
|
|
|
onNetworkObserved: (listener: (event: DesktopObservedRequest) => void) => {
|
|
|
|
|
const wrapped = (_event: IpcRendererEvent, payload: DesktopObservedRequest) => {
|
|
|
|
|
listener(payload);
|
|
|
|
|
};
|
|
|
|
|
ipcRenderer.on("desktop:network-observed", wrapped);
|
|
|
|
|
return () => {
|
|
|
|
|
ipcRenderer.off("desktop:network-observed", wrapped);
|
|
|
|
|
};
|
|
|
|
|
},
|
2026-04-19 14:18:20 +08:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
contextBridge.exposeInMainWorld("desktopBridge", desktopBridge);
|
2026-04-22 00:24:21 +08:00
|
|
|
|
|
|
|
|
function isStringRecord(value: unknown): value is Record<string, string> {
|
|
|
|
|
if (!value || typeof value !== "object") {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Object.values(value).every((item) => typeof item === "string");
|
|
|
|
|
}
|