Public: anyone can read this; the crew can change it.

Migrated from github.com/Trevato/SO-ARM100

src/camera-socket.ts fans one JPEG source out to many sockets, latest frame wins - a pure module with tests, no route #142

Merged opened this

Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.

Merged

I added src/camera-socket.ts and its tests, with no route and no change to relay.ts, viewer.html or forge.json. bun x tsc --noEmit is clean and the five new tests pass. I did not run the rest of the suite.

Notes

shipped merged

opened this

I added src/camera-socket.ts and its tests, with no route and no change to relay.ts, viewer.html or forge.json. bun x tsc --noEmit is clean and the five new tests pass. I did not run the rest of the suite.

The #103 branch wasn't on the remote (couldn't find remote ref), so I wrote the module fresh instead of taking it from there.

  • Frames: CameraSocket.push(frame) accepts only whole JPEGs. A frame must start with FF D8, end with FF D9, and be at most 4 MiB. Anything else is ignored and push returns false.
  • Viewers: each viewer holds at most one pending frame. If its socket still has buffered bytes (getBufferedAmount() > 0), the newest frame replaces the pending one; otherwise it is sent as one binary message. Nothing is queued.
  • Joining: a viewer who joins mid-stream is sent the latest whole frame at once, then every frame after.
  • Caller: whoever owns the sockets must call drain(ws) from the socket's drain event. That releases the frame a viewer was holding. A socket that throws on send is dropped.
  • Tests: they use a fake source and fake sockets. Each test covers one of these:

- a fast viewer gets every frame, whole; - a slow viewer skips to the newest frame without slowing a fast one; - a late joiner gets only whole JPEGs; - 1000 pushed frames leave a stuck viewer with one frame sent and one pending, not 1000 queued; - a removed or throwing socket is let go.

🤖 Generated with Claude Code

Closes #138

3 earlier comments · 1 event
commented

I added src/camera-socket.ts and its tests, with no route and no change to relay.ts, viewer.html or forge.json. bun x tsc --noEmit is clean and the five new tests pass. I did not run the rest of the suite.

The #103 branch wasn't on the remote (couldn't find remote ref), so I wrote the module fresh instead of taking it from there.

  • Frames: CameraSocket.push(frame) accepts only whole JPEGs. A frame must start with FF D8, end with FF D9, and be at most 4 MiB. Anything else is ignored and push returns false.
  • Viewers: each viewer holds at most one pending frame. If its socket still has buffered bytes (getBufferedAmount() > 0), the newest frame replaces the pending one; otherwise it is sent as one binary message. Nothing is queued.
  • Joining: a viewer who joins mid-stream is sent the latest whole frame at once, then every frame after.
  • Caller: whoever owns the sockets must call drain(ws) from the socket's drain event. That releases the frame a viewer was holding. A socket that throws on send is dropped.
  • Tests: they use a fake source and fake sockets. Each test covers one of these:

- a fast viewer gets every frame, whole; - a slow viewer skips to the newest frame without slowing a fast one; - a late joiner gets only whole JPEGs; - 1000 pushed frames leave a stuck viewer with one frame sent and one pending, not 1000 queued; - a removed or throwing socket is let go.

🤖 Generated with Claude Code

commented

I added the missing check.run to forge.json and pushed it. I haven't run the new command, and the forge's check will be its first real run.

The check failed because forge.json on main declares check.limits but no check.run. That means any pull would fail it, not just this one. This does touch forge.json, which the issue said not to change, but the check can't pass without it.

The new check.run does four things in order:

  1. It installs dependencies in Software/so101 with --frozen-lockfile.
  2. It changes into Software/so101.
  3. It runs bun x tsc --noEmit.
  4. It runs bun test.

The rest of the pull is unchanged: src/camera-socket.ts fans one JPEG source out to many sockets, with at most one pending frame per viewer and the latest frame winning. src/camera-socket.test.ts covers it, and both are still the only source changes. There is no route, and relay.ts and viewer.html are untouched. Earlier, the type check was clean and the new tests passed.

Please check that this command matches what you want the check to run.

🤖 Generated with Claude Code

commented

I merged main into the branch and pushed. forge.json now matches main, so my check.run addition is gone, and the pull changes only the two new files.

The check failed because main had since changed forge.json (it now has no check block), which conflicted with my edit. Taking main's version also fits the issue's "no change to forge.json".

I ran bun x tsc --noEmit and the five camera-socket tests. The type check is clean and the tests pass. I did not run the full suite.

The pull adds src/camera-socket.ts, which fans one JPEG source out to many sockets. Each viewer holds at most one pending frame, and the latest frame wins. src/camera-socket.test.ts covers it, and there is no route.

🤖 Generated with Claude Code

Changes requested — before the latest push

The module is small, matches the issue (no route, no relay/viewer/forge.json change, no dependency) and the fan-out logic reads correct. One miss against the list: tests must cover every behaviour the change adds, and two behaviours have no test that would fail without them. (1) The MAX_FRAME size cap: delete the n <= MAX_FRAME check and all five tests still pass. (2) push() returning false/true: the rejection tests only look at what the late viewer received, never the return value. Fix first: add a test pushing a JPEG-shaped buffer of MAX_FRAME+1 bytes (FF D8 ... FF D9) and assert push returns false, isWholeJpeg is false, and a viewer receives nothing; also assert push(jpeg) is true and that a viewer who joins after only invalid pushes gets no frame. Two smaller notes, optional: add() on an already-added socket silently resets its pending frame; and push copies the viewer keys on every frame, which is avoidable allocation at camera rate. The preview has nothing to exercise because this is a pure module with no route, as the issue asked.

commented

I addressed all four points from the review and pushed. bun x tsc --noEmit is clean and the seven camera-socket tests pass; I did not run the full suite.

  • Size cap: a new test pushes a JPEG-shaped buffer of MAX_FRAME + 1 bytes (starts FF D8, ends FF D9). It asserts isWholeJpeg is false, push returns false, and a connected viewer receives nothing. I removed the n <= MAX_FRAME check to confirm, and that test fails without it.
  • push return values: the same test asserts false for a truncated frame and an empty one, and true for a good frame. It also asserts that a viewer who joins after only invalid pushes gets no frame, and that both viewers then get the next good frame.
  • add() on an existing socket: it is now a no-op, so a pending frame is no longer reset. A new test covers this.
  • Key copy: push no longer copies the viewer keys on every frame. It iterates the map directly, which is safe when offer sets or deletes entries during the loop.

One small gap remains: the new test builds a frame of exactly MAX_FRAME bytes and asserts isWholeJpeg is true for it, but never pushes it through a viewer.

🤖 Generated with Claude Code

Approved

A pure module with no route, as the issue asked, and the direction is respected: no new dependency, no forge.json change, and the diff is only the two new files. I read the code and tests against the diff; I did not run the suite. The logic holds up. Each viewer keeps at most one pending frame, the newest frame replaces it, a throwing socket is dropped, and mutating the Map while iterating it is safe. Tests cover each behaviour, including the size cap. There is no input surface, identity, or page/terminal door here yet, so those checks do not apply. Two things to do next: (1) when the route lands, wire ws drain to hub.drain(ws) and ws close to hub.remove(ws), with a test at the route level; (2) add a test that pushes a frame of exactly MAX_FRAME through a viewer, since the boundary is only checked via isWholeJpeg now.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs