Open History to see the last commit
1// @effect-diagnostics globalFetch:off - This browser transport runs without an Effect runtime, like the live stream.
2import { msg } from "gt-i18n";
3import { type MarkedText, markedTextEnglish } from "../errors/translatable.ts";
4import * as Schema from "effect/Schema";
5import { extractPng } from "@t3tools/shared/png";
6import { withDeviceHubQuery } from "./hubAccess.ts";
7import type { DeviceStreamTarget } from "./stream.ts";
8
9export class DeviceScreenshotError extends Schema.TaggedError<DeviceScreenshotError>()(
10 "DeviceScreenshotError",
11 { status: Schema.Number },
12) {
13 override get message(): string {
14 return markedTextEnglish(this.i18nMessage);
15 }
16
17 get i18nMessage(): MarkedText {
18 return [msg`Screenshot capture failed (${{ status: String(this.status) }}). Try again.`];
19 }
20}
21
22export async function captureDeviceScreenshot(target: DeviceStreamTarget, signal: AbortSignal) {
23 const vendor = target.platform === "ios" ? "serve-sim" : "serve-emu";
24 const url = withDeviceHubQuery(
25 `${target.access.httpBase}/vendor/${vendor}/api/screenshot?device=${encodeURIComponent(target.deviceId)}`,
26 target.access,
27 );
28 const response = await fetch(url, {
29 method: "POST",
30 credentials: target.access.credentials ? "include" : "omit",
31 signal: AbortSignal.any([signal, AbortSignal.timeout(20_000)]),
32 });
33 if (!response.ok) throw new DeviceScreenshotError({ status: response.status });
34 const png = extractPng(new Uint8Array(await response.arrayBuffer()));
35 if (!png) throw new Error("The device returned an incomplete screenshot. Try again.");
36 return new Blob([png], { type: "image/png" });
37}