feat(media): one library for several relays — films in S3, records and live pins in Postgres

FileMediaStore ties a library to one disk on one machine. SHADE_MEDIA_S3_URL
+ SHADE_MEDIA_PG_URL (or `media serve --s3 --pg`) put sealed bytes in any
S3 bucket and the records every instance must agree on in Postgres.

- ObjectMediaStore: each upload streams as multipart to its own object
  key; the commit is an INSERT whose primary key admits one record per id,
  so racing uploads on any instances are decided once and the loser
  deletes its object. Delete drops the record first. sweep() reclaims
  unreferenced objects and dead multipart uploads past a 24 h grace.
- S3Bucket: Shade's own SigV4 client. Bun 1.3.5's S3Client buffers a
  whole upload (512 MiB in, 543 MiB RSS), end(error) does not abort, and
  slice(0, n).stream() returned the whole object. Here: at most three parts
  in memory (512 MiB through, 11 MiB RSS), payload-hashed signatures,
  AbortMultipartUpload, ranged reads checked for length.
- PostgresMediaIndex, PostgresLivePinStore, createSharedMediaBackend in
  @shade/storage-postgres. LivePinStore.put now refuses a key change
  (LivePinTakenError) in every store, atomically in Postgres.
- The container logs a backend it cannot use and runs on without media.

Verified: SigV4 reproduces AWS's published example signature; SeaweedFS
with signature checking accepts it and refuses a wrong secret (403);
Postgres 16: racing inserts over 8 pools (1 of 24 wins), racing pins,
BIGINT sizes; two relays over one backend; CLI relays behind a
round-robin proxy: upload, bit-identical download, Chromium playback
seeked to 24 s with ranges served by both; the bucket holds no
plaintext, name or MP4 marker. Suites: media 87, storage-postgres 18,
server 55, cli 15, all green.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-10-18 22:28:58 +02:00
parent 01f8471fb5
commit aaec92429d
24 changed files with 1464 additions and 63 deletions

View File

@@ -85,6 +85,41 @@ failing. With `SHADE_MEDIA_DIR` unset the routes are not mounted, and the log sa
With `SHADE_MEDIA_OWNERS` empty, the relay serves what it has and accepts nothing new.
`*` admits every key, which is only safe on a private network.
### Several instances, one library (S3 + Postgres)
```
SHADE_MEDIA_S3_URL=https://<key>:<secret>@s3.eu-north-1.amazonaws.com/<bucket>[/<prefix>]
SHADE_MEDIA_PG_URL=postgres://… # falls back to SHADE_PREKEY_PG_URL
```
(`shade media serve --s3 … --pg …` does the same outside the container.) Sealed
bytes go to the bucket, records and live-channel pins to Postgres, and any number of
instances behind one hostname serve one library. Any S3 API works (AWS, R2, MinIO,
SeaweedFS, Garage, B2); credentials left out of the URL come from
`AWS_ACCESS_KEY_ID`/`AWS_SECRET_ACCESS_KEY`, the region from an `amazonaws.com` host
or `?region=`. A bucket the credentials cannot use is reported at start; media stays
off and the rest of the server runs.
- **Uploads stream.** A film goes up as an S3 multipart upload, part by part, with at
most three 16 MiB parts in memory (512 MiB through: 11 MiB of RSS). Every part's
payload is hashed into its signature, so a part damaged in transit is refused.
- **Racing uploads of one id** (on any instances) each go to their own object key;
the primary key in Postgres admits one record, and the loser deletes its object.
Delete removes the record first, so a crash never brings a film back.
- **Sweeping.** Objects no record points at and multipart uploads never completed
(a crash mid-upload) are reclaimed after 24 hours, a minute after start and every
6 hours. Every instance may sweep at once.
- **Live channels** are pinned in Postgres, so a channel cannot be pinned to two keys
by two instances. Frames still fan out in the memory of the instance the publisher
is connected to: route `/v1/live/*` to one instance (or sticky by channel id).
- The bucket holds only sealed SMF1 bytes. No key, name or plaintext is in it or in
Postgres.
`S3Bucket` is Shade's own small S3 client (SigV4, checked against AWS's published
signature example and a server that validates signatures), not `Bun.S3Client`: in
Bun 1.3.5 its writer buffers everything written to it (512 MiB in, 543 MiB of RSS),
`end(error)` does not abort, and `slice(0, n).stream()` returned the whole object.
## In code
```ts
@@ -371,6 +406,10 @@ done in v1. A deployment that needs it can pad above this layer.
mode across restarts), the HTTP routes and relay over real sockets, and the WebM
and fMP4 splitters on real ffmpeg output (keyframes inside a GOP, codec strings
for H.264, HEVC, AV1, VP9, AAC and Opus, CMAF `styp`/`prft` prefixes).
- `tests/object-store.test.ts` and `shade-storage-postgres/tests/postgres-media.test.ts`
run against a real S3 API and Postgres when `SHADE_TEST_S3_URL` /
`SHADE_TEST_PG_URL` are set (commands in the files): two relays over one bucket,
racing uploads and pins, sweeping, bounded memory.
- `test-vectors/media.json` (`bun run scripts/generate-vectors.ts media.json`): the
Kotlin port reads every vector and reproduces every sealed file and signed frame
byte for byte (`MediaVectorTest`).