Files
piik/src/client/remote-control/protocol.ts
T
AAA aa4e6ee45c fix(input): validate escaped text against the actual command budget
The editor could clear 2048 quote characters although escaping exceeded the Native 4096-byte command budget. Measure the complete command before sending and retain oversized text. Core, Native and Browser use the 8192-byte UTF-8 carrier bound; independent queue budgets keep their own names. Static Browser values are compared with compiled Core, with no new WASM hot-path dependency. Real WebCrypto boundary probes, Native compilation and all 2521 Web tests pass.
2026-11-27 00:59:56 -05:00

66 lines
2.9 KiB
TypeScript

import { z } from "zod";
import { isComposingOrAltGraph } from "../ui/shortcut-key";
export const INPUT_FRAME_BYTES = 8192;
export const REMOTE_INPUT_COMMAND_BYTES = 4096;
export const REMOTE_TEXT_BYTES = 2048;
const encoder = new TextEncoder();
export function inputFrameFits(frame: string): boolean {
return frame.length <= INPUT_FRAME_BYTES && encoder.encode(frame).byteLength <= INPUT_FRAME_BYTES;
}
// The editor can measure the command before sending. Every grant has the same hex length.
const exampleGrant = "0".repeat(64);
export function remoteTextFits(text: string, grant = exampleGrant): boolean {
return text.length <= REMOTE_TEXT_BYTES && encoder.encode(text).byteLength <= REMOTE_TEXT_BYTES
&& encoder.encode(JSON.stringify({ type: "text", text, grant })).byteLength <= REMOTE_INPUT_COMMAND_BYTES;
}
// F12 is reserved by Windows debuggers, including for global modified chords.
export const remoteShortcutKeySchema = z.number().int().min(1).max(11);
export const REMOTE_SHORTCUT_KEYS = Array.from({ length: 11 }, (_, index) => index + 1);
export function matchesRemoteShortcut(event: Pick<KeyboardEvent, "code" | "ctrlKey" | "altKey" | "shiftKey" | "metaKey"
| "isComposing" | "keyCode" | "getModifierState">, key: number): boolean {
return !isComposingOrAltGraph(event) && event.ctrlKey && event.altKey && !event.shiftKey && !event.metaKey && event.code === `F${key}`;
}
export const remoteControlModeSchema = z.enum(["off", "ask", "room", "password"]);
export type RemoteControlMode = z.infer<typeof remoteControlModeSchema>;
export const remoteInputPlatformSchema = z.enum(["windows", "darwin", "linux"]);
export type RemoteInputPlatform = z.infer<typeof remoteInputPlatformSchema>;
export const remoteInputStateSchema = z.object({
state: z.enum(["ready", "unavailable", "pending", "active", "suspended", "failed"]),
mode: remoteControlModeSchema,
platform: remoteInputPlatformSchema,
grant: z.string().regex(/^[0-9a-f]{64}$/).optional(),
intent: z.string().regex(/^[0-9a-f]{32}$/).optional(),
reason: z.enum(["password", "retry", "busy", "gamepad"]).optional(),
capabilities: z.array(z.string().min(1).max(32)).max(16).optional(),
coPlay: z.boolean().optional(),
inputFailed: z.boolean().optional(),
}).strict();
export type RemoteInputState = z.infer<typeof remoteInputStateSchema>;
export function remoteTextAvailable(state: RemoteInputState): boolean {
return !state.coPlay || state.capabilities?.includes("text") === true;
}
export function remoteGamepadAvailable(state: RemoteInputState): boolean {
return state.coPlay === true && state.capabilities?.includes("gamepad") === true;
}
// Button bits follow W3C standard indices; sticks are signed 16-bit, Y down,
// and triggers are unsigned 8-bit. Native SDK layouts stay in Host adapters.
export interface RemoteGamepadReport {
buttons: number;
leftTrigger: number;
rightTrigger: number;
leftX: number;
leftY: number;
rightX: number;
rightY: number;
}