Files
piik/scripts/browser-gate-harness.ts
AAA 066e218d84 fix(media): improve connection recovery and capture compatibility (#498)
## Problem and outcome

Stale ICE and transport callbacks, interrupted media ownership, and optional encoder capabilities could break otherwise recoverable sharing sessions. This maintenance phase corrects those owners across Web, Native and embedded SFU, improves current-state diagnostics, and finishes the affected UI and documentation animation paths.

Android, QR invitations and App/Server headline presentation stay on their feature branch. This PR contains the accepted maintenance scope only.

## Release notes

**Play together. Your turn!**

Approved friends can play together with shared keyboard and mouse controls or optional gamepads.

**远程同乐,轮到你啦!**

获准的朋友可以共用键鼠,或使用可选手柄,一起玩本地多人游戏。

### English

This maintenance update improves connection recovery, capture compatibility and troubleshooting for existing Piik rooms.

- Keep ICE candidates and gathering completion tied to their current connection. Preserve Viewer route preferences through reconnection and retry newly available Peer opportunities after SFU fallback.
- Correct picture and voice ownership during restart, source replacement and connection retirement. Report failed picture dependencies while keeping room voice available.
- Separate capture and forwarding statistics and isolate pooled sender attachment failures.
- Improve Windows H.264 compatibility with optional encoder properties, retain safe capture failure codes, and keep shared sound volume usable without microphone support.
- Improve remote input continuity and mouse responsiveness after idle.
- Fix fullscreen handling in some mobile browsers, source-picker refresh, delayed chat confirmations, starred-site startup and local App requests. Refine control motion and reduced-motion behavior.
- Restore the mascot's face and shared animation lifecycle in the documentation header.
- Include current connection and playback state in diagnostic exports, update Zod to 4.6.6 and simplify shared lifecycle code.

Existing 2.x protocols, configuration and default quality settings are preserved.

### 中文

本次维护更新改善现有房间的连接恢复、采集兼容性和故障诊断。

- ICE 候选和收集结束事件跟随当前连接;重连保留观众线路偏好,SFU 兜底后出现新的可用 P2P 机会仍可尝试。
- 修正重启、换源和连接结束时的画面与语音生命周期;语音继续时,画面失败会明确反馈。
- 区分采集与转发统计,隔离编码池中单个输出连接的失败。
- 改善 Windows H.264 可选属性兼容性,保留有效的采集失败原因;没有麦克风能力时也能调整共享声音音量。
- 修复部分移动浏览器全屏、选源刷新、迟到聊天确认、收藏站点启动和本机 App 请求问题,打磨控件动效及减少动态效果行为。
- 修复文档导航 mascot 面部一直隐藏的问题,接回共用动画生命周期。
- 诊断导出补取当前连接与播放状态,升级 Zod 至 4.6.6,精简共用生命周期代码。

沿用现有 2.x 协议、配置和画质默认值。Android、二维码邀请及 App/Server 新增的 HEADLINE 展示留待后续版本。

## Verification

- `npm run check`: type-check, 98 test files / 1,765 tests, production Web build passed.
- `npm run check:go-core`: Go formatting, tests, vet, Windows/Linux builds passed; Darwin builds require the native CI runners.
- `npm run check:native`: current Windows capture compilation, fixtures and capability probe passed.
- `npm run build:website`, TypeScript and repository hygiene checks passed after the documentation-brand fix.
- Existing controlled media lifecycle and P2P/SFU voice acceptance covered the retained route/transport changes. Final local browser acceptance covered actual sharing and pause/resume, fullscreen, source refresh, voice/keyboard/settings, 320px to desktop layouts, both themes and all presentation modes. Documentation brand was reproduced broken, then verified on load, replay and reduced motion.
- Independent scoped reviews found no integration blocker.

## Impact

Patch release v2.4.1. Existing 2.x wire, configuration, authority, capacity and default quality contracts remain intact; no data migration. Source volume no longer depends on microphone availability. Zod 4.6.6 fixes GHSA-mrhx-c7xc-m3w5.

The device-specific Windows reports #495 and #452 remain under investigation; this PR improves compatibility and diagnostics without claiming those reports fully resolved. Physical macOS acceptance remains with its native runner/device checks.
2026-10-24 12:30:39 -04:00

582 lines
17 KiB
TypeScript

import { spawn, type ChildProcessWithoutNullStreams } from "node:child_process";
import { once } from "node:events";
import { lstat, readdir, rm } from "node:fs/promises";
import { createConnection, createServer } from "node:net";
import { tmpdir } from "node:os";
import { basename, dirname, join, resolve } from "node:path";
import WebSocket from "ws";
export interface PageHandle {
sessionId: string;
}
interface CdpResult<T> {
result: { value?: T };
exceptionDetails?: unknown;
}
export interface CleanupResult {
browserExited: boolean;
nativeExited: boolean;
serverClosed: boolean;
portsClosed: boolean;
profileRemoved: boolean;
}
interface AsyncCloseable {
close(): Promise<void>;
}
// A gate's server is either an in-process page server it closes directly or a
// spawned Piik server it stops like any other child.
export type CleanupServer = AsyncCloseable | ChildProcessWithoutNullStreams;
export interface CleanupResources {
cdp: CdpConnection | null;
native: ChildProcessWithoutNullStreams | null;
chrome: ChildProcessWithoutNullStreams | null;
server: CleanupServer | null;
profile: string | null;
ports: number[];
}
export class CdpConnection {
private nextId = 1;
private readonly pending = new Map<number, {
resolve: (value: unknown) => void;
reject: (error: Error) => void;
timeout: NodeJS.Timeout;
}>();
private closed = false;
private constructor(private readonly socket: WebSocket) {
socket.on("message", (raw) => this.onMessage(raw.toString()));
socket.on("close", () => this.closePending());
socket.on("error", () => this.closePending());
}
static async connect(url: string, deadline: number): Promise<CdpConnection> {
const socket = new WebSocket(url, { maxPayload: 16 * 1024 * 1024 });
try {
await withDeadline(() => new Promise<void>((resolveOpen, rejectOpen) => {
socket.once("open", resolveOpen);
socket.once("error", rejectOpen);
}), deadline);
} catch (error) {
socket.terminate();
throw error;
}
return new CdpConnection(socket);
}
call<T>(
method: string,
params: Record<string, unknown>,
sessionId: string | undefined,
deadline: number,
): Promise<T> {
if (this.closed || this.socket.readyState !== WebSocket.OPEN) {
return Promise.reject(new Error(`Cannot call ${method}: CDP is closed`));
}
const id = this.nextId++;
return new Promise<T>((resolveCall, rejectCall) => {
const timeout = setTimeout(() => {
this.pending.delete(id);
rejectCall(new Error(`CDP command timed out: ${method}`));
}, Math.max(0, deadline - Date.now()));
this.pending.set(id, {
resolve: (value) => {
clearTimeout(timeout);
resolveCall(value as T);
},
reject: (error) => {
clearTimeout(timeout);
rejectCall(error);
},
timeout,
});
try {
this.socket.send(JSON.stringify({ id, method, params, sessionId }));
} catch (error) {
clearTimeout(timeout);
this.pending.delete(id);
rejectCall(error instanceof Error ? error : new Error("CDP send failed"));
}
});
}
close(): void {
if (!this.closed) this.socket.close();
}
private onMessage(raw: string): void {
let message: { id?: number; result?: unknown; error?: unknown };
try {
message = JSON.parse(raw) as typeof message;
} catch {
return;
}
if (message.id === undefined) return;
const pending = this.pending.get(message.id);
if (!pending) return;
this.pending.delete(message.id);
if (message.error) {
const detail = typeof message.error === "object" && "message" in message.error
? String(message.error.message)
: "unknown protocol error";
pending.reject(new Error(`CDP command failed: ${detail}`));
} else {
pending.resolve(message.result ?? {});
}
}
private closePending(): void {
if (this.closed) return;
this.closed = true;
for (const pending of this.pending.values()) {
clearTimeout(pending.timeout);
pending.reject(new Error("CDP connection closed"));
}
this.pending.clear();
}
}
// launchChrome starts an isolated Chrome process with the shared CDP flags.
export async function launchChrome(
executable: string,
debugPort: number,
profile: string,
flags: string[] = [],
options: { cwd?: string; env?: NodeJS.ProcessEnv } = {},
): Promise<ChildProcessWithoutNullStreams> {
const child = spawn(executable, [
`--remote-debugging-port=${debugPort}`,
`--user-data-dir=${profile}`,
...flags,
"about:blank",
], {
cwd: options.cwd,
env: options.env,
stdio: "pipe",
windowsHide: true,
});
await once(child, "spawn");
return child;
}
export function withDeadline<T>(
operation: () => Promise<T>,
deadline: number,
now: () => number = Date.now,
): Promise<T> {
const remaining = Math.max(0, deadline - now());
return new Promise<T>((resolveValue, rejectValue) => {
const timeout = setTimeout(
() => rejectValue(new Error("Operation deadline exceeded")),
remaining,
);
Promise.resolve().then(operation).then(
(value) => {
clearTimeout(timeout);
resolveValue(value);
},
(error: unknown) => {
clearTimeout(timeout);
rejectValue(error);
},
);
});
}
type JsonFetch = (
input: string,
init: { signal: AbortSignal },
) => Promise<{ ok: boolean; json(): Promise<unknown> }>;
export async function fetchJsonBefore<T>(
url: string,
deadline: number,
fetcher: JsonFetch = fetch,
): Promise<T> {
const controller = new AbortController();
const abort = setTimeout(
() => controller.abort(),
Math.max(0, deadline - Date.now()),
);
try {
const response = await withDeadline(
() => fetcher(url, { signal: controller.signal }),
deadline,
);
if (!response.ok) throw new Error("HTTP response was not successful");
return await withDeadline(() => response.json() as Promise<T>, deadline);
} finally {
clearTimeout(abort);
controller.abort();
}
}
export async function waitForSample<T>(
sample: (deadline: number) => Promise<T>,
retainAndAccept: (value: T) => boolean,
timeoutMs: number,
pollMs = 100,
): Promise<void> {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
let value: T | undefined;
try {
value = await withDeadline(() => sample(deadline), deadline);
} catch {}
if (value !== undefined && retainAndAccept(value)) return;
await delay(Math.min(pollMs, Math.max(0, deadline - Date.now())));
}
throw new Error("Bounded browser stage timed out");
}
export async function createPage(
cdp: CdpConnection,
url: string,
probe?: string,
newWindow = false,
browserContextId?: string,
): Promise<PageHandle> {
const deadline = Date.now() + 15_000;
const created = await cdp.call<{ targetId: string }>(
"Target.createTarget",
{
url: "about:blank",
background: false,
newWindow,
...(browserContextId ? { browserContextId } : {}),
},
undefined,
deadline,
);
const attached = await cdp.call<{ sessionId: string }>(
"Target.attachToTarget",
{ targetId: created.targetId, flatten: true },
undefined,
deadline,
);
const page = { sessionId: attached.sessionId };
await Promise.all([
cdp.call("Page.enable", {}, page.sessionId, deadline),
cdp.call("Runtime.enable", {}, page.sessionId, deadline),
]);
if (probe) {
await cdp.call(
"Page.addScriptToEvaluateOnNewDocument",
{ source: probe },
page.sessionId,
deadline,
);
}
await cdp.call("Page.navigate", { url }, page.sessionId, deadline);
await waitForSample(
(sampleDeadline) => evaluate(
cdp,
page,
"document.readyState === 'complete'",
sampleDeadline,
),
Boolean,
Math.max(1, deadline - Date.now()),
);
return page;
}
export async function evaluate<T>(
cdp: CdpConnection,
page: PageHandle,
expression: string,
deadline: number,
): Promise<T> {
const value = await cdp.call<CdpResult<T>>(
"Runtime.evaluate",
{
expression,
awaitPromise: true,
returnByValue: true,
userGesture: true,
},
page.sessionId,
deadline,
);
if (value.exceptionDetails) throw new Error("Browser evaluation failed");
return value.result.value as T;
}
export async function reservePort(): Promise<number> {
const server = createServer();
return new Promise((resolvePort, rejectPort) => {
server.once("error", rejectPort);
server.listen(0, "127.0.0.1", () => {
const address = server.address();
if (!address || typeof address === "string") {
rejectPort(new Error("Port reservation failed"));
return;
}
server.close((error) =>
error ? rejectPort(error) : resolvePort(address.port),
);
});
});
}
// Readiness is the debugging endpoint, never the spawned process: Chrome's
// Windows launcher exits as soon as the browser process takes over the profile
// (measured: exit at ~140 ms, endpoint ready at ~440 ms). The deadline bounds
// the wait, and the launcher's exit code is reported when it runs out.
export async function waitForVersion(
port: number,
child: ChildProcessWithoutNullStreams,
): Promise<{ Browser: string; webSocketDebuggerUrl: string }> {
const deadline = Date.now() + 20_000;
while (Date.now() < deadline) {
try {
return await fetchJsonBefore<{ Browser: string; webSocketDebuggerUrl: string }>(
`http://127.0.0.1:${port}/json/version`,
deadline,
);
} catch {}
await delay(Math.min(100, Math.max(0, deadline - Date.now())));
}
throw new Error(`Chrome startup timed out (exit ${child.exitCode ?? "running"})`);
}
export async function cleanupRun(
resources: CleanupResources,
): Promise<CleanupResult> {
if (resources.cdp) {
try {
await resources.cdp.call(
"Browser.close",
{},
undefined,
Date.now() + 2_000,
);
} catch {}
resources.cdp.close();
}
const browserExited = resources.chrome
? await stopProcessTree(resources.chrome)
: true;
if (resources.native && processRunning(resources.native)) {
resources.native.stdin.once("error", () => {}); // Exit may race the stop request (EPIPE).
try {
resources.native.stdin.write("\n");
} catch {}
}
const nativeExited = resources.native
? await stopProcessTree(resources.native)
: true;
const server = resources.server;
let serverClosed = true;
if (server && "close" in server) {
try {
await withDeadline(() => server.close(), Date.now() + 5_000);
} catch {
serverClosed = false;
}
} else if (server) {
serverClosed = await stopProcessTree(server);
}
const portsClosed = await waitForPortsClosed(
[...new Set(resources.ports)],
5_000,
);
const profileRemoved = resources.profile
? await removeGateProfile(resources.profile)
: true;
return {
browserExited,
nativeExited,
serverClosed,
portsClosed,
profileRemoved,
};
}
function processRunning(child: ChildProcessWithoutNullStreams): boolean {
return child.pid !== undefined && child.exitCode === null && child.signalCode === null;
}
async function stopProcessTree(
child: ChildProcessWithoutNullStreams,
): Promise<boolean> {
if (!processRunning(child)) return true;
if (await waitForExit(child, 2_000)) return true;
if (process.platform === "win32" && child.pid !== undefined) {
await runBounded(
"taskkill.exe",
["/PID", String(child.pid), "/T", "/F"],
5_000,
);
} else {
child.kill("SIGTERM");
if (await waitForExit(child, 2_000)) return true;
child.kill("SIGKILL");
}
return waitForExit(child, 3_000);
}
async function waitForExit(
child: ChildProcessWithoutNullStreams,
timeoutMs: number,
): Promise<boolean> {
if (!processRunning(child)) return true;
return new Promise<boolean>((resolveExit) => {
const onExit = () => {
clearTimeout(timeout);
resolveExit(true);
};
const timeout = setTimeout(() => {
child.off("exit", onExit);
resolveExit(!processRunning(child));
}, timeoutMs);
child.once("exit", onExit);
});
}
async function runBounded(
command: string,
args: string[],
timeoutMs: number,
environment: NodeJS.ProcessEnv = process.env,
): Promise<boolean> {
const child = spawn(command, args, {
stdio: "pipe",
windowsHide: true,
env: environment,
});
child.stdout.resume();
child.stderr.resume();
try {
const code = await withDeadline(
() => new Promise<number | null>((resolveExit, rejectExit) => {
child.once("error", rejectExit);
child.once("exit", resolveExit);
}),
Date.now() + timeoutMs,
);
return code === 0;
} catch {
child.kill();
return false;
}
}
async function waitForPortsClosed(
ports: number[],
timeoutMs: number,
): Promise<boolean> {
if (ports.length === 0) return true;
try {
await waitForSample(
(deadline) => Promise.all(ports.map((port) => portClosed(port, deadline))),
(closed) => closed.every(Boolean),
timeoutMs,
);
return true;
} catch {
return false;
}
}
function portClosed(port: number, deadline: number): Promise<boolean> {
return withDeadline(() => new Promise<boolean>((resolveClosed) => {
const socket = createConnection({ host: "127.0.0.1", port });
const finish = (closed: boolean) => {
socket.destroy();
resolveClosed(closed);
};
socket.once("connect", () => finish(false));
socket.once("error", () => finish(true));
}), deadline);
}
export const profileCleanupScript = String.raw`
$ErrorActionPreference = 'Stop'
$root = [IO.Path]::GetFullPath($env:PIIK_GATE_PROFILE_TO_REMOVE)
$temp = [IO.Path]::GetFullPath([IO.Path]::GetTempPath()).TrimEnd([IO.Path]::DirectorySeparatorChar)
$parent = [IO.Path]::GetDirectoryName($root).TrimEnd([IO.Path]::DirectorySeparatorChar)
$name = [IO.Path]::GetFileName($root)
if ($parent -ine $temp -or $name -notmatch '^piik-(access-privacy|client-loopback|client-media)-[A-Za-z0-9_-]{6}$') { exit 31 }
$rootEntries = @([IO.Directory]::EnumerateFileSystemEntries($parent, $name, [IO.SearchOption]::TopDirectoryOnly))
if ($rootEntries.Count -eq 0) { exit 0 }
if ($rootEntries.Count -ne 1) { exit 34 }
$root = $rootEntries[0]
$rootAttributes = [IO.File]::GetAttributes($root)
if (($rootAttributes -band [IO.FileAttributes]::ReparsePoint) -ne 0) { exit 32 }
if (($rootAttributes -band [IO.FileAttributes]::Directory) -eq 0) { exit 34 }
$pending = [Collections.Generic.Stack[string]]::new()
$pending.Push($root)
while ($pending.Count -gt 0) {
$directory = $pending.Pop()
$attributes = [IO.File]::GetAttributes($directory)
if (($attributes -band [IO.FileAttributes]::ReparsePoint) -ne 0) { exit 32 }
foreach ($entry in [IO.Directory]::EnumerateFileSystemEntries($directory)) {
$attributes = [IO.File]::GetAttributes($entry)
if (($attributes -band [IO.FileAttributes]::ReparsePoint) -ne 0) { exit 32 }
if (($attributes -band [IO.FileAttributes]::Directory) -ne 0) { $pending.Push($entry) }
}
}
Remove-Item -LiteralPath $root -Recurse -Force
$rootEntries = @([IO.Directory]::EnumerateFileSystemEntries($parent, $name, [IO.SearchOption]::TopDirectoryOnly))
if ($rootEntries.Count -ne 0) { exit 33 }
`;
export function isExactGateProfile(
profile: string,
systemTemp: string,
): boolean {
const candidate = resolve(profile);
const temp = resolve(systemTemp);
const same = process.platform === "win32"
? dirname(candidate).toLowerCase() === temp.toLowerCase()
: dirname(candidate) === temp;
return same &&
/^piik-(?:access-privacy|client-loopback|client-media)-[A-Za-z0-9_-]{6}$/.test(
basename(candidate),
);
}
async function removeGateProfile(profile: string): Promise<boolean> {
if (!isExactGateProfile(profile, tmpdir())) return false;
if (process.platform !== "win32") {
const pending = [profile];
try {
while (pending.length > 0) {
const current = pending.pop()!;
const stat = await lstat(current).catch(error => {
if (current === profile && (error as NodeJS.ErrnoException).code === "ENOENT") return null;
throw error;
});
if (!stat) return true;
if (stat.isSymbolicLink()) return false;
if (stat.isDirectory()) {
pending.push(
...(await readdir(current)).map((name) => join(current, name)),
);
}
}
await rm(profile, { recursive: true });
return true;
} catch {
return false;
}
}
return runBounded(
"powershell.exe",
["-NoProfile", "-NonInteractive", "-Command", profileCleanupScript],
10_000,
{ ...process.env, PIIK_GATE_PROFILE_TO_REMOVE: profile },
);
}
function delay(ms: number): Promise<void> {
return new Promise((resolveDelay) => setTimeout(resolveDelay, ms));
}