The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 23:15 UTC
fix(server): iOS device viewer no longer times out on a still simulator screen (#20999)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Joaquin Alfian <joaquinalfian@Joaquins-MacBook-Pro.local> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Joaquin Navarro committed
Oct 20, 2026 at 01:05 UTC
6aa83dbbe40f6a87f36bfa63a956ae3fddfba3fb
4 files changed
+221
-3
apps/server/src/device/DeviceHubProxy.ts
+5
-1
@@ -19,6 +19,7 @@ import * as NodeSocket from "@effect/platform-node/NodeSocket";
19
20
import { authenticateMediaRequest } from "../auth/http.ts";
21
import * as DeviceService from "./DeviceService.ts";
22
+import { repeatStillFrame } from "./mjpegStillFrame.ts";
23
24
const ALLOWED_PATHS: ReadonlyArray<RegExp> = [
25
/^\/api\/devices$/,
@@ -131,7 +132,10 @@ const proxyHttp = Effect.fn("DeviceHubProxy.proxyHttp")(function* (
132
}
133
// Long-lived MJPEG and AVCC responses must not be buffered by compression.
134
headers["cache-control"] = "no-store, no-transform";
134
- return HttpServerResponse.stream(response.stream, {
135
+ const body = headers["content-type"]?.startsWith("multipart/x-mixed-replace")
136
+ ? repeatStillFrame(response.stream)
137
+ : response.stream;
138
+ return HttpServerResponse.stream(body, {
139
status: response.status,
140
headers,
141
...(headers["content-type"] ? { contentType: headers["content-type"] } : {}),
apps/server/src/device/mjpegStillFrame.test.ts
new
+89
@@ -0,0 +1,89 @@
1
+import { describe, expect, it } from "@effect/vitest";
2
+import * as Cause from "effect/Cause";
3
+import * as Effect from "effect/Effect";
4
+import * as Fiber from "effect/Fiber";
5
+import * as Queue from "effect/Queue";
6
+import * as Stream from "effect/Stream";
7
+import { TestClock } from "effect/testing";
8
+
9
+import { STILL_FRAME_DELAY, repeatStillFrame } from "./mjpegStillFrame.ts";
10
+
11
+const part = (body: string) =>
12
+ Buffer.from(
13
+ `--frame\r\nContent-Type: image/jpeg\r\nContent-Length: ${body.length}\r\n\r\n${body}\r\n`,
14
+ );
15
+
16
+const viewer = Effect.gen(function* () {
17
+ const upstream = yield* Queue.unbounded<Uint8Array, Cause.Done>();
18
+ let received = "";
19
+ const fiber = yield* repeatStillFrame(Stream.fromQueue(upstream)).pipe(
20
+ Stream.runForEach((chunk) =>
21
+ Effect.sync(() => {
22
+ received += Buffer.from(chunk).toString();
23
+ }),
24
+ ),
25
+ Effect.forkChild,
26
+ );
27
+ const send = (...chunks: ReadonlyArray<Uint8Array>) =>
28
+ Effect.gen(function* () {
29
+ for (const chunk of chunks) yield* Queue.offer(upstream, chunk);
30
+ yield* Effect.yieldNow;
31
+ });
32
+ const finish = Effect.gen(function* () {
33
+ yield* Queue.end(upstream);
34
+ yield* Fiber.join(fiber);
35
+ return received;
36
+ });
37
+ return { send, finish };
38
+});
39
+
40
+describe("repeatStillFrame", () => {
41
+ it.effect("re-sends only the newest complete frame once when the screen stays still", () =>
42
+ Effect.gen(function* () {
43
+ const { send, finish } = yield* viewer;
44
+ yield* send(part("first"));
45
+ yield* TestClock.adjust(STILL_FRAME_DELAY);
46
+ yield* send(Buffer.concat([part("second"), part("third")]));
47
+ yield* TestClock.adjust(STILL_FRAME_DELAY);
48
+ yield* TestClock.adjust(STILL_FRAME_DELAY);
49
+ expect(yield* finish).toBe(
50
+ `${part("first")}${part("first")}${part("second")}${part("third")}${part("third")}`,
51
+ );
52
+ }),
53
+ );
54
+
55
+ it.effect("never repeats into a frame that is still arriving", () =>
56
+ Effect.gen(function* () {
57
+ const { send, finish } = yield* viewer;
58
+ const second = part("second");
59
+ // The pause lands inside the second part's headers.
60
+ yield* send(part("first"), second.subarray(0, 20));
61
+ yield* TestClock.adjust(STILL_FRAME_DELAY);
62
+ yield* send(second.subarray(20));
63
+ yield* TestClock.adjust(STILL_FRAME_DELAY);
64
+ expect(yield* finish).toBe(`${part("first")}${second}${second}`);
65
+ }),
66
+ );
67
+
68
+ it.effect("recognizes a frame whose final CRLF arrives in a later chunk", () =>
69
+ Effect.gen(function* () {
70
+ const { send, finish } = yield* viewer;
71
+ const first = part("first");
72
+ yield* send(first.subarray(0, -1), first.subarray(-1));
73
+ yield* TestClock.adjust(STILL_FRAME_DELAY);
74
+ expect(yield* finish).toBe(`${first}${first}`);
75
+ }),
76
+ );
77
+
78
+ it.effect("passes through bodies without the hub's part framing", () =>
79
+ Effect.gen(function* () {
80
+ const { send, finish } = yield* viewer;
81
+ const unframed = Buffer.from("--frame\r\nContent-Type: image/jpeg\r\n\r\nbody\r\n");
82
+ yield* send(unframed);
83
+ yield* TestClock.adjust(STILL_FRAME_DELAY);
84
+ yield* send(part("later"));
85
+ yield* TestClock.adjust(STILL_FRAME_DELAY);
86
+ expect(yield* finish).toBe(`${unframed}${part("later")}`);
87
+ }),
88
+ );
89
+});
apps/server/src/device/mjpegStillFrame.ts
new
+121
@@ -0,0 +1,121 @@
1
+/**
2
+ * Browsers paint a multipart/x-mixed-replace part only once the next part begins, and the hub
3
+ * writes a part only when the screen changes. Left alone, an `<img>` viewer never shows a still
4
+ * screen (clients such as the iOS app then give up waiting for the first frame) and always hides
5
+ * the last frame of a change. Re-sending the last part after a short quiet period makes it paint.
6
+ */
7
+import * as Arr from "effect/Array";
8
+import * as Duration from "effect/Duration";
9
+import * as Effect from "effect/Effect";
10
+import * as Fiber from "effect/Fiber";
11
+import * as Option from "effect/Option";
12
+import * as Stream from "effect/Stream";
13
+
14
+export const STILL_FRAME_DELAY = Duration.millis(100);
15
+
16
+const HEADER_END = Buffer.from("\r\n\r\n");
17
+const MAX_HEADER_BYTES = 16 * 1024;
18
+// Simulator frames are far smaller; a larger claim is unknown framing, not something to retain.
19
+const MAX_PART_BYTES = 16 * 1024 * 1024;
20
+
21
+type Part = Arr.NonEmptyReadonlyArray<Uint8Array>;
22
+
23
+const partLength = (headers: string) => {
24
+ if (!headers.startsWith("--")) return undefined;
25
+ const length = Number(/^content-length:\s*(\d+)\s*$/im.exec(headers)?.[1]);
26
+ return length <= MAX_PART_BYTES ? length : undefined;
27
+};
28
+
29
+const endsWithCrlf = (part: Part) => {
30
+ const last = Arr.lastNonEmpty(part);
31
+ const cr = last.length > 1 ? last.at(-2) : part.at(-2)?.at(-1);
32
+ return cr === 0x0d && last.at(-1) === 0x0a;
33
+};
34
+
35
+/**
36
+ * Follows the hub's part framing (`--frame`, `Content-Length`, CRLF CRLF, body, CRLF; each part
37
+ * is one write) and keeps the last complete part as views into the forwarded chunks, so frame
38
+ * bytes are never copied. Any other framing turns tracking off and the rest passes through.
39
+ */
40
+const partTracker = () => {
41
+ let pieces: Array<Uint8Array> = [];
42
+ let size = 0;
43
+ let partEnd: number | null = null;
44
+ let tracking = true;
45
+ return {
46
+ /** The last complete part, while the stream sits at a part boundary. */
47
+ still: null as Part | null,
48
+ feed(chunk: Uint8Array) {
49
+ if (!tracking) return;
50
+ let rest = Buffer.from(chunk.buffer, chunk.byteOffset, chunk.byteLength);
51
+ let part: Part | undefined;
52
+ while (rest.length > 0) {
53
+ if (partEnd === null) {
54
+ // A header split across chunks is re-scanned per chunk. Only a header-sized
55
+ // prefix of the new chunk joins the scan, so frame bytes are never copied.
56
+ const head =
57
+ size === 0
58
+ ? rest
59
+ : Buffer.concat([...pieces, rest.subarray(0, MAX_HEADER_BYTES + HEADER_END.length)]);
60
+ const headerEnd = head.indexOf(HEADER_END);
61
+ if (headerEnd < 0 && head.length <= MAX_HEADER_BYTES) {
62
+ pieces.push(rest);
63
+ size += rest.length;
64
+ break;
65
+ }
66
+ const length =
67
+ headerEnd < 0 || headerEnd > MAX_HEADER_BYTES
68
+ ? undefined
69
+ : partLength(head.subarray(0, headerEnd).toString("latin1"));
70
+ if (length === undefined) {
71
+ tracking = false;
72
+ break;
73
+ }
74
+ partEnd = headerEnd + HEADER_END.length + length + 2;
75
+ }
76
+ const taken = rest.subarray(0, partEnd - size);
77
+ pieces.push(taken);
78
+ size += taken.length;
79
+ rest = rest.subarray(taken.length);
80
+ if (size < partEnd) break;
81
+ const complete = pieces;
82
+ if (!Arr.isArrayNonEmpty(complete) || !endsWithCrlf(complete)) {
83
+ tracking = false;
84
+ break;
85
+ }
86
+ part = complete;
87
+ pieces = [];
88
+ size = 0;
89
+ partEnd = null;
90
+ }
91
+ if (!tracking) pieces = [];
92
+ this.still = tracking && size === 0 ? (part ?? this.still) : null;
93
+ },
94
+ };
95
+};
96
+
97
+/** Re-sends the latest frame once when the stream goes quiet between frames. */
98
+export const repeatStillFrame = <E, R>(body: Stream.Stream<Uint8Array, E, R>) =>
99
+ Stream.transformPull(body, (pull, scope) =>
100
+ Effect.sync(() => {
101
+ const tracker = partTracker();
102
+ let pending: Fiber.Fiber<Effect.Success<typeof pull>, Effect.Error<typeof pull>> | null =
103
+ null;
104
+ const forward = (chunks: Effect.Success<typeof pull>) => {
105
+ pending = null;
106
+ for (const chunk of chunks) tracker.feed(chunk);
107
+ return chunks;
108
+ };
109
+ return Effect.gen(function* () {
110
+ const still = tracker.still;
111
+ if (still === null && pending === null) return forward(yield* pull);
112
+ pending ??= yield* Effect.forkIn(pull, scope);
113
+ if (still === null) return forward(yield* Fiber.join(pending));
114
+ // Timing out the join leaves the pull running, so a frame arriving mid-wait is kept.
115
+ const next = yield* Fiber.join(pending).pipe(Effect.timeoutOption(STILL_FRAME_DELAY));
116
+ if (Option.isSome(next)) return forward(next.value);
117
+ tracker.still = null;
118
+ return still;
119
+ });
120
+ }),
121
+ );
docs/internals/devices.md
+6
-2
@@ -36,8 +36,12 @@ bearer and DPoP clients mint over authenticated HTTP. The ticket is stripped
36
before the request reaches the hub.
37
38
Stream responses carry `Cache-Control: no-transform`; the compression
39
-middleware would otherwise buffer an MJPEG body that never ends. In browser dev,
40
-the Vite proxy must forward WebSocket upgrades for `/api`, not only `/ws`.
39
+middleware would otherwise buffer an MJPEG body that never ends. Browsers paint an
40
+MJPEG part only once the next part begins and the hub writes one only when the
41
+screen changes, so the proxy
42
+[re-sends the last part](../../apps/server/src/device/mjpegStillFrame.ts) after a
43
+short quiet period. In browser dev, the Vite proxy must forward WebSocket
44
+upgrades for `/api`, not only `/ws`.
45
46
## Device settings never go through the hub
47