-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcontrols.ts
More file actions
193 lines (177 loc) · 5.06 KB
/
Copy pathcontrols.ts
File metadata and controls
193 lines (177 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { accessTokenFromLocation, apiHeaders, apiRequest } from "./client";
import { apiUrl } from "./config";
import type {
ButtonPayload,
CrownPayload,
EdgeTouchPayload,
InstallUploadResponse,
KeyPayload,
LaunchPayload,
MultiTouchPayload,
OpenUrlPayload,
ScrollPayload,
SimulatorMetadata,
SimulatorResponse,
TouchPayload,
} from "./types";
export type ControlMessage =
| ({ type: "touch" } & TouchPayload)
| ({ type: "edgeTouch" } & EdgeTouchPayload)
| ({ type: "multiTouch" } & MultiTouchPayload)
| ({ type: "key" } & KeyPayload)
| ({ type: "button" } & ButtonPayload)
| ({ type: "crown" } & CrownPayload)
| ({ type: "scroll" } & ScrollPayload)
| { type: "dismissKeyboard" }
| { type: "toggleSoftwareKeyboard" }
| { type: "home" }
| { type: "appSwitcher" }
| { type: "rotateLeft" }
| { type: "rotateRight" }
| { type: "toggleAppearance" };
export interface ScreenRecordingStartResponse {
ok: boolean;
recordingId: string;
}
async function postSimulatorAction(
udid: string,
action: string,
payload?: LaunchPayload | OpenUrlPayload,
): Promise<SimulatorMetadata | null> {
if (action === "launch" || action === "open-url") {
const response = await apiRequest<{
ok: boolean;
simulator?: SimulatorMetadata | null;
}>(`/api/simulators/${encodeURIComponent(udid)}/action`, {
method: "POST",
body: JSON.stringify({
action: action === "open-url" ? "openUrl" : "launch",
...payload,
}),
});
return response.simulator ?? null;
}
const response = await apiRequest<SimulatorResponse | { ok: boolean }>(
`/api/simulators/${udid}/${action}`,
{
method: "POST",
body: payload ? JSON.stringify(payload) : undefined,
},
);
return "simulator" in response ? response.simulator : null;
}
export function bootSimulator(udid: string) {
return postSimulatorAction(udid, "boot");
}
export function shutdownSimulator(udid: string) {
return postSimulatorAction(udid, "shutdown");
}
export function openSimulatorUrl(udid: string, payload: OpenUrlPayload) {
return postSimulatorAction(udid, "open-url", payload);
}
export function launchSimulatorBundle(udid: string, payload: LaunchPayload) {
return postSimulatorAction(udid, "launch", payload);
}
export function toggleSimulatorAppearance(udid: string) {
return apiRequest<{ ok: boolean }>(
`/api/simulators/${encodeURIComponent(udid)}/action`,
{
method: "POST",
body: JSON.stringify({ action: "toggleAppearance" }),
},
);
}
export function uploadSimulatorApp(
udid: string,
file: File,
): Promise<InstallUploadResponse> {
return apiRequest<InstallUploadResponse>(
`/api/simulators/${encodeURIComponent(udid)}/install-upload`,
{
body: file,
headers: {
"Content-Type": "application/octet-stream",
"X-SimDeck-Filename": encodeURIComponent(file.name || "app-upload"),
},
method: "POST",
},
);
}
export function simulatorControlSocketUrl(udid: string) {
const url = new URL(
apiUrl(`/api/simulators/${encodeURIComponent(udid)}/control`),
window.location.href,
);
const token = accessTokenFromLocation();
if (token) {
url.searchParams.set("simdeckToken", token);
}
url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
return url.toString();
}
async function fetchSimulatorBlob(
path: string,
options: RequestInit = {},
): Promise<Blob> {
const { headers, ...rest } = options;
const response = await fetch(apiUrl(path), {
...rest,
headers: apiHeaders(headers),
});
if (!response.ok) {
const contentType = response.headers.get("content-type") ?? "";
if (contentType.includes("application/json")) {
const body = (await response.json()) as { error?: string };
throw new Error(
body.error ?? `Request failed with status ${response.status}`,
);
}
throw new Error(
(await response.text()) ||
`Request failed with status ${response.status}`,
);
}
return response.blob();
}
export function captureSimulatorScreenshot(
udid: string,
options: { withBezel?: boolean } = {},
): Promise<Blob> {
const params = options.withBezel ? "?bezel=true" : "";
return fetchSimulatorBlob(
`/api/simulators/${encodeURIComponent(udid)}/screenshot.png${params}`,
);
}
export function recordSimulatorScreen(
udid: string,
seconds = 5,
): Promise<Blob> {
return fetchSimulatorBlob(
`/api/simulators/${encodeURIComponent(udid)}/screen-recording`,
{
body: JSON.stringify({ seconds }),
method: "POST",
},
);
}
export function startSimulatorScreenRecording(
udid: string,
): Promise<ScreenRecordingStartResponse> {
return apiRequest<ScreenRecordingStartResponse>(
`/api/simulators/${encodeURIComponent(udid)}/screen-recording/start`,
{
method: "POST",
},
);
}
export function stopSimulatorScreenRecording(
udid: string,
recordingId: string,
): Promise<Blob> {
return fetchSimulatorBlob(
`/api/simulators/${encodeURIComponent(udid)}/screen-recording/${encodeURIComponent(recordingId)}/stop`,
{
method: "POST",
},
);
}