Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

screenshot.ts

29 lines1.2 KB

1// @effect-diagnostics globalFetch:off - This browser transport runs without an Effect runtime, like the live stream.
2import { withDeviceHubQuery } from "./hubAccess.ts";
3import type { DeviceStreamTarget } from "./stream.ts";
4​
5export class DeviceScreenshotError extends Error {
6 readonly status: number;
7​
8 constructor(status: number) {
9 super(`Screenshot capture failed (${status}). Try again.`);
10 this.status = status;
11 }
13​
14export async function captureDeviceScreenshot(target: DeviceStreamTarget, signal: AbortSignal) {
15 const vendor = target.platform === "ios" ? "serve-sim" : "serve-emu";
16 const url = withDeviceHubQuery(
17 `${target.access.httpBase}/vendor/${vendor}/api/screenshot?device=${encodeURIComponent(target.deviceId)}`,
18 target.access,
19 );
20 const response = await fetch(url, {
21 method: "POST",
22 credentials: target.access.credentials ? "include" : "omit",
23 signal: AbortSignal.any([signal, AbortSignal.timeout(20_000)]),
24 });
25 if (!response.ok) throw new DeviceScreenshotError(response.status);
26 const image = await response.blob();
27 if (!image.size) throw new Error("The device returned an empty screenshot. Try again.");
28 return image;