The best way to control Claude Code, Codex and more from any computer or phone
| 1 | // @effect-diagnostics globalFetch:off - This browser transport runs without an Effect runtime, like the live stream. |
| 2 | import { msg } from "gt-i18n"; |
| 3 | import { type MarkedText, markedTextEnglish } from "../errors/translatable.ts"; |
| 4 | import * as Schema from "effect/Schema"; |
| 5 | import { extractPng } from "@t3tools/shared/png"; |
| 6 | import { withDeviceHubQuery } from "./hubAccess.ts"; |
| 7 | import type { DeviceStreamTarget } from "./stream.ts"; |
| 8 | |
| 9 | export 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 [ |
| 19 | msg("Screenshot capture failed ({status}). Try again.", { status: String(this.status) }), |
| 20 | ]; |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | export async function captureDeviceScreenshot(target: DeviceStreamTarget, signal: AbortSignal) { |
| 25 | const vendor = target.platform === "ios" ? "serve-sim" : "serve-emu"; |
| 26 | const url = withDeviceHubQuery( |
| 27 | `${target.access.httpBase}/vendor/${vendor}/api/screenshot?device=${encodeURIComponent(target.deviceId)}`, |
| 28 | target.access, |
| 29 | ); |
| 30 | const response = await fetch(url, { |
| 31 | method: "POST", |
| 32 | credentials: target.access.credentials ? "include" : "omit", |
| 33 | signal: AbortSignal.any([signal, AbortSignal.timeout(20_000)]), |
| 34 | }); |
| 35 | if (!response.ok) throw new DeviceScreenshotError({ status: response.status }); |
| 36 | const png = extractPng(new Uint8Array(await response.arrayBuffer())); |
| 37 | if (!png) throw new Error("The device returned an incomplete screenshot. Try again."); |
| 38 | return new Blob([png], { type: "image/png" }); |
| 39 | } |