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

@@ -45,15 +45,29 @@ export interface LivePin {
export interface LivePinStore {
get(channelId: string): Promise<LivePin | null>;
/**
* Create or refresh a pin. Must throw `LivePinTakenError` if the channel is
* pinned to another signing key — atomically, so two relays sharing the
* store cannot both pin one channel to different keys.
*/
put(pin: LivePin): Promise<void>;
}
export class LivePinTakenError extends Error {
override readonly name = 'LivePinTakenError';
constructor(channelId: string) {
super(`channel ${channelId} is pinned to another key`);
}
}
export class MemoryLivePinStore implements LivePinStore {
private readonly pins = new Map<string, LivePin>();
async get(id: string): Promise<LivePin | null> {
return this.pins.get(id) ?? null;
}
async put(pin: LivePin): Promise<void> {
const have = this.pins.get(pin.channelId);
if (have !== undefined && have.signingKey !== pin.signingKey) throw new LivePinTakenError(pin.channelId);
this.pins.set(pin.channelId, pin);
}
}
@@ -90,6 +104,8 @@ export class FileLivePinStore implements LivePinStore {
async put(pin: LivePin): Promise<void> {
const pins = await this.load();
const have = pins.get(pin.channelId);
if (have !== undefined && have.signingKey !== pin.signingKey) throw new LivePinTakenError(pin.channelId);
pins.set(pin.channelId, pin);
const cutoff = Date.now() - this.ttlMs;
for (const [k, p] of pins) if (p.lastSeen < cutoff) pins.delete(k);
@@ -369,7 +385,13 @@ export function createLiveRelay(opts: LiveRelayOptions): LiveRelay {
pinnedAt: existing?.pinnedAt ?? now(),
lastSeen: now(),
};
await opts.pins.put(pin);
try {
await opts.pins.put(pin);
} catch (e) {
// Another relay sharing the pin store won the race for this channel.
if (e instanceof LivePinTakenError) return fail(ws, 'channel is pinned to another key');
throw e;
}
const ch = (await channelFor(channelId))!;
ch.pin = pin;
// A reconnecting publisher replaces the stale socket.