fix(media): an uploader checks the relay's upload status before trusting it

A chunk of 0 looped forever, a huge chunk held the film in memory and a huge
window opened a request (a thread on Kotlin) per piece. statusProblem checks
chunk, window, received and landed; the client window is capped at 16. Same
in MediaUploader, with tests on both sides.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-10-19 00:31:06 +02:00
parent 0a706cdb47
commit 7d45be8712
7 changed files with 133 additions and 9 deletions

View File

@@ -14,7 +14,7 @@ import type { OwnerKey } from './owner.js';
import { MediaReader, sealMediaBytes } from './sealed.js';
import type { MediaMeta, ReaderOptions, SealOptions, SealResult } from './sealed.js';
import { HttpRangeSource } from './source.js';
import { DEFAULT_CHUNK_BYTES, OFFSET_HEADER } from './upload.js';
import { DEFAULT_CHUNK_BYTES, MAX_CLIENT_WINDOW, OFFSET_HEADER, statusProblem } from './upload.js';
export function mediaUrl(server: string, mediaId: Uint8Array): string {
return `${server.replace(/\/$/, '')}/v1/media/${toHex(mediaId)}`;
@@ -162,7 +162,13 @@ export class MediaClient {
},
});
if (res.status !== 200 && res.status !== 201) return fail(res, 'start');
return (await res.json()) as Status;
const st = (await res.json().catch(() => {
throw new Transient("start: the relay's answer is not JSON");
})) as Status;
if (st.complete === true) return st;
const problem = statusProblem(st, size);
if (problem !== null) throw new Error(`upload failed: the relay's upload status is unusable: ${problem}`);
return st;
};
const has = (st: Status, offset: number) => offset < st.received || (st.landed ?? []).includes(offset / st.chunk);
@@ -172,7 +178,7 @@ export class MediaClient {
return;
}
const { chunk } = opened;
const window = Math.max(1, opened.window ?? 1);
const window = Math.min(MAX_CLIENT_WINDOW, Math.max(1, opened.window ?? 1));
let acked = 0;
let complete = false;
const todo: number[] = [];