Open History to see the last commit
1import type { DeviceHubAccess } from "@t3tools/client-runtime/device/hub-access";
2import { isMarked } from "@t3tools/client-runtime/errors";
3import type { DevicePlatform } from "@t3tools/contracts";
4import type { PlatformOSType } from "react-native";
5
6export interface DeviceStreamConfiguration {
7 readonly access: DeviceHubAccess;
8 readonly platform: DevicePlatform;
9 readonly deviceId: string;
10 /** See `DeviceStreamTarget.serveSimOnly`. */
11 readonly serveSimOnly?: boolean;
12 readonly colors: {
13 readonly background: string;
14 readonly foreground: string;
15 readonly muted: string;
16 readonly buttonBackground: string;
17 readonly buttonForeground: string;
18 readonly buttonBorder: string;
19 };
20 /** Translated in React Native: the WebView cannot load the catalogs. */
21 readonly labels: {
22 readonly screen: string;
23 readonly reconnectingControls: string;
24 };
25}
26
27export function deviceStreamBaseUrl(
28 viewerPlatform: PlatformOSType,
29 devicePlatform: DevicePlatform,
30 httpBase: string,
31) {
32 if (viewerPlatform !== "android") return "file:///";
33 // Android targets stream through WebCodecs, which needs a secure context. iOS targets
34 // stream an MJPEG image from the environment, which a synthetic https document blocks as
35 // mixed content on plain-http environments even with mixedContentMode="always".
36 if (devicePlatform === "android") return "https://localhost/";
37 return `${new URL(httpBase).origin}/`;
38}
39
40export function deviceStreamDocument(
41 configuration: string,
42 script: string,
43 stoppedDetail = "Device viewer stopped unexpectedly.",
44) {
45 const safeConfiguration = configuration.replace(/</g, "\\u003c");
46 const safeScript = script.replace(/<\/script/gi, "<\\/script");
47 const safeStoppedDetail = JSON.stringify(stoppedDetail).replace(/</g, "\\u003c");
48 const failure = `window.ReactNativeWebView.postMessage(JSON.stringify({type:"status",status:"error",detail:${safeStoppedDetail}}));`;
49 return `<!doctype html><html><head><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"></head><body><script>window.addEventListener("error",function(){${failure}});window.addEventListener("unhandledrejection",function(){${failure}});\n${safeScript}\ntry{T3DeviceStream.start(${safeConfiguration});}catch{${failure}}</script></body></html>`;
50}
51
52export function deviceStreamMessage(data: string) {
53 try {
54 const message: unknown = JSON.parse(data);
55 if (typeof message !== "object" || message === null || !("type" in message)) return null;
56 if (message.type === "unauthorized" || message.type === "retry") {
57 return { type: message.type } as const;
58 }
59 if (
60 message.type === "input" &&
61 "connected" in message &&
62 typeof message.connected === "boolean"
63 ) {
64 return { type: message.type, connected: message.connected } as const;
65 }
66 if (
67 message.type === "status" &&
68 "status" in message &&
69 (message.status === "connecting" ||
70 message.status === "streaming" ||
71 message.status === "error") &&
72 (!("detail" in message) || isMarked(message.detail))
73 ) {
74 return {
75 type: message.type,
76 status: message.status,
77 detail: "detail" in message && isMarked(message.detail) ? message.detail : undefined,
78 } as const;
79 }
80 } catch {}
81 return null;
82}