feat(media): uploads in pieces — resumable, and a relay's memory no longer grows with the film

Bun.serve reads a request body as fast as the client sends it, whether the
handler takes it or not. Whenever the store was slower than the client, one
PUT of a film put the whole film in the relay's memory: a 512 MiB upload
peaked at 762 MiB (disk) and 1011 MiB (S3).

A sealed file over 16 MiB now goes up as POST /v1/media/:id/upload plus
16 MiB PATCH pieces, each signed over its offset and length, up to 4 in
flight and landing in any order. Uploads under way are durable: on disk
uploads/<id>.part + .json + .log (the log names a piece only after fsync),
and in S3 a row in shade_media_uploads beside the multipart upload, where
piece n is part n+1. A dropped connection, a relay restart or the next piece
landing on another instance costs only the pieces in flight. The client asks
how far the upload got and sends the rest. Uploads count against quota and
are swept after 24 h without a piece. An upload still receiving pieces is
never swept, however old its multipart upload is.

The sweep is now part of every MediaStore and runs from createMediaService
(onSweep) instead of two copies of the timer. No route takes more than
64 MiB, so Bun's 128 MiB cap stays and the container's body limit covers
every route. `media put` shows progress.

bench/media-store.ts: the relay's peak RSS is 330–369 MiB at both 512 MiB
and 1.5 GiB (worst of 3 runs, both stores).

Also found:
- Bun 1.3.5 fetch never finishes sending a Bun.file().slice() body, so the
  client reads each piece itself.
- postgres.js reads a NULL element of a TEXT[] as the string "NULL", so etags
  are selected as JSON.

Tests:
- tests/upload.test.ts (11).
- S3 across two instances (+2).
- PG index (+1).
- media 114, storage-postgres 21, server 55, cli 15.
- All 27 packages type-check.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-10-18 23:39:25 +02:00
parent 5f7c6ff4b4
commit bd4882e1b8
22 changed files with 2076 additions and 101 deletions

View File

@@ -66,6 +66,15 @@ New package **`@shade/media`** — see `docs/media.md`.
`PostgresLivePinStore`, `createSharedMediaBackend` in
`@shade/storage-postgres`. A live pin can no longer change key, even between
instances (`LivePinTakenError`).
- **Uploads in pieces, resumable.** A sealed file over 16 MiB goes up as
`POST /v1/media/:id/upload` + 16 MiB `PATCH` pieces (each signed over its
offset), up to 4 in flight and landing in any order. A dropped connection,
a relay restart or the next piece landing on another instance costs only the
pieces in flight: the client asks how far the upload got and sends the rest.
Uploads under way survive restarts (`uploads/` on disk, `shade_media_uploads`
beside the S3 multipart upload), count against quota and are swept after
24 h without a piece. `media put` shows progress. Above all, a relay's
memory no longer grows with the film (see Fixed).
- **`shade live video --input`** a film, `/dev/video0` or `rtsp://…`: ffmpeg is
run for you, H.264/AAC copied (a camera's own encoder) and anything else
encoded to H.264 (keyframe every 2 s) and AAC, as low-latency fMP4.
@@ -116,8 +125,17 @@ New package **`@shade/media`** — see `docs/media.md`.
handler (an unhandled rejection). Now every call fails with the reason.
- The container refused every request body over 128 MiB (Bun's default),
which would have made every film a 413. The cap is raised only when media
is mounted, and every other route now has an explicit 128 MiB body limit.
which would have made every film a 413. Films now go up in pieces of at
most 64 MiB, so the cap stays, and every route has an explicit 128 MiB
body limit.
- An upload put the whole film in the relay's memory whenever the store was
slower than the client: Bun.serve reads a request body as fast as it
arrives, whether the handler takes it or not. One `PUT` of a 512 MiB film
peaked at 762 MiB (disk) and 1011 MiB (S3). In pieces it stays the same at
512 MiB and 1.5 GiB (`bench/media-store.ts`).
- The media sweep ran only for the S3 store, from two copies of the timer
(container and `media serve`). It is now part of every store and of
`createMediaService` (`onSweep` reports it), hourly.
## [4.14.1] — 2026-10-16 — Vault: `edit` for vaults two devices write