CH-J Toolsgestión de servidores a través de SSH
Menú
Fuente publicada

CH-J Tools

Busque directorios y archivos para obtener una versión de aplicación específica.

Descargar fuente ZIP
CH-J / Server Manager 1704 archivos
0.1.2-beta Añadido: · Construido:
transport.ts Añadido: Fecha no registrada 2,7 KB · 58 líneas
Código Descargar archivo
1/**
2 * Transport between the web UI and the Rust Core.
3 *
4 * Every call goes to one named Tauri command; the Rust Core identifies the calling
5 * window and checks its permissions. This wrapper is a convenience, NOT a security
6 * boundary.
7 *
8 * Errors are converted to the message format of the Electron 0.1.0 IPC layer
9 * ("Error invoking remote method '<channel>': Error: <message>"), because existing UI
10 * code and third-party modules parse error codes out of that text.
11 */
12import { invoke } from "@tauri-apps/api/core";
13import { listen, type UnlistenFn } from "@tauri-apps/api/event";
14import { getCurrentWebview } from "@tauri-apps/api/webview";
15import type { CoreErrorPayload } from "./types";
17export type Unsubscribe = () => void;
19function errorFrom(channel: string, reason: unknown): Error {
20 const payload = (reason && typeof reason === "object" ? reason : { message: String(reason ?? "Unknown error") }) as Partial<CoreErrorPayload>;
21 const message = typeof payload.message === "string" && payload.message ? payload.message : String(payload.code || "Operation failed.");
22 return new Error(`Error invoking remote method '${channel}': Error: ${message}`);
25/** Invokes a Core command. `channel` is the stable 0.1.0 channel name used in error texts. */
26export async function call<T>(command: string, channel: string, payload?: unknown): Promise<T> {
27 try {
28 return await invoke<T>(command, payload === undefined ? {} : { payload });
29 } catch (reason) {
30 throw errorFrom(channel, reason);
31 }
34/** Fire-and-forget command (Electron `ipcRenderer.send`): failures are logged by the Core only. */
35export function send(command: string, payload: unknown): void {
36 void invoke(command, { payload }).catch(() => {});
39/** Subscribes to a Core push event; returns a synchronous unsubscribe function like 0.1.0. */
40export function subscribe<T>(event: string, callback: (payload: T) => void): Unsubscribe {
41 if (typeof callback !== "function") return () => {};
42 let active = true;
43 let unlisten: UnlistenFn | null = null;
44 // Without a target a window hears events addressed to every other window as well.
45 void listen<T>(event, (message) => { if (active) callback(message.payload); }, { target: { kind: "AnyLabel", label: getCurrentWebview().label } })
46 .then((fn) => { if (active) unlisten = fn; else fn(); })
47 .catch(() => {});
48 return () => {
49 active = false;
50 if (unlisten) { unlisten(); unlisten = null; }
51 };
54/** Like `subscribe`, but throws for a non-function callback (module API contract of 0.1.0). */
55export function subscribeStrict<T>(event: string, callback: (payload: T) => void, what: string): Unsubscribe {
56 if (typeof callback !== "function") throw new TypeError(`${what} callback must be a function.`);
57 return subscribe(event, callback);
SHA-256fc30d588aa5656d2f3fe644ea7d54f3b949003506ae04d6ea5989ee91b53a236

CH-J Proprietary Software License 1.14 · La fuente se proporciona en el CH-J Proprietary Software License 1.14. Su disponibilidad no cambia los términos de la licencia ni otorga derechos adicionales.

Archivo SHA-256ac8fe790f4ba60fc098f8c985fc2656cfcc6cdc21fb34b30e308a2b54e62258f
Archivos