@hyperframes/studio 0.8.210 → 0.8.211
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assets/{hyperframes-player-DUpxahTX.js → hyperframes-player-CzrqgMUX.js} +1 -1
- package/dist/assets/{index-B4dcZZAm.js → index-B3SPHPi1.js} +1 -1
- package/dist/assets/{index-CtrcjpbM.js → index-k4nVfS2D.js} +123 -123
- package/dist/{chunk-7SREYKIC.js → chunk-HD6NXWGI.js} +6 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.html +1 -1
- package/dist/index.js +484 -240
- package/dist/player.js +1 -1
- package/package.json +8 -8
- package/src/components/DesktopGuideCard.tsx +1 -1
- package/src/player/components/VideoThumbnail.tsx +6 -0
- package/src/player/hooks/previewMessageRouter.test.ts +4 -0
- package/src/player/lib/mediaFetch.test.ts +466 -0
- package/src/player/lib/mediaFetch.ts +133 -26
- package/src/player/lib/thumbnailFrameCache.ts +69 -0
- package/src/player/lib/thumbnailPlaybackHold.test.ts +86 -0
- package/src/player/lib/thumbnailPlaybackHold.ts +15 -0
- package/src/player/lib/thumbnailScheduler.test.ts +49 -0
- package/src/player/lib/thumbnailScheduler.ts +22 -6
- package/src/player/lib/thumbnailSourcePool.ts +163 -0
- package/src/player/lib/thumbnailVideoDecoder.test.ts +492 -8
- package/src/player/lib/thumbnailVideoDecoder.ts +67 -75
- package/src/player/lib/timelineViewportBudgets.test.ts +1 -0
- package/src/player/lib/timelineViewportBudgets.ts +4 -0
- package/src/player/store/previewQualitySlice.ts +11 -0
package/dist/player.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyperframes/studio",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.211",
|
|
4
4
|
"description": "",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -60,12 +60,12 @@
|
|
|
60
60
|
"dockview-react": "8.3.1",
|
|
61
61
|
"gsap": "^3.13.0",
|
|
62
62
|
"mediabunny": "^1.45.3",
|
|
63
|
-
"@hyperframes/core": "0.8.
|
|
64
|
-
"@hyperframes/parsers": "0.8.
|
|
65
|
-
"@hyperframes/
|
|
66
|
-
"@hyperframes/shader-transitions": "0.8.
|
|
67
|
-
"@hyperframes/
|
|
68
|
-
"@hyperframes/studio-server": "0.8.
|
|
63
|
+
"@hyperframes/core": "0.8.211",
|
|
64
|
+
"@hyperframes/parsers": "0.8.211",
|
|
65
|
+
"@hyperframes/sdk": "0.8.211",
|
|
66
|
+
"@hyperframes/shader-transitions": "0.8.211",
|
|
67
|
+
"@hyperframes/player": "0.8.211",
|
|
68
|
+
"@hyperframes/studio-server": "0.8.211"
|
|
69
69
|
},
|
|
70
70
|
"devDependencies": {
|
|
71
71
|
"@tailwindcss/vite": "^4.3.3",
|
|
@@ -82,7 +82,7 @@
|
|
|
82
82
|
"vite": "^6.4.2",
|
|
83
83
|
"vitest": "^4.1.11",
|
|
84
84
|
"zustand": "^5.0.0",
|
|
85
|
-
"@hyperframes/producer": "0.8.
|
|
85
|
+
"@hyperframes/producer": "0.8.211"
|
|
86
86
|
},
|
|
87
87
|
"peerDependencies": {
|
|
88
88
|
"react": "19",
|
|
@@ -168,7 +168,7 @@ function DownloadStep({ progress, onDownload }: CardProps) {
|
|
|
168
168
|
if (!progress.downloaded && !installed)
|
|
169
169
|
return (
|
|
170
170
|
<Step n={1} state={NOW} title={`Download HyperFrames for ${where}`}>
|
|
171
|
-
<p className={NOTE}>Free. Opens hyperframes.
|
|
171
|
+
<p className={NOTE}>Free. Opens hyperframes.studio in a new tab.</p>
|
|
172
172
|
<Button
|
|
173
173
|
variant="primary"
|
|
174
174
|
className="mt-2"
|
|
@@ -3,10 +3,13 @@ import { useThumbnailLease } from "../../hooks/useThumbnailLease";
|
|
|
3
3
|
import { useThumbnailStripSize } from "../../hooks/useThumbnailStripSize";
|
|
4
4
|
import {
|
|
5
5
|
createThumbnailKey,
|
|
6
|
+
thumbnailScheduler,
|
|
6
7
|
type ThumbnailPriority,
|
|
7
8
|
type ThumbnailSnapshot,
|
|
8
9
|
} from "../lib/thumbnailScheduler";
|
|
10
|
+
import { bindThumbnailPlaybackHold } from "../lib/thumbnailPlaybackHold";
|
|
9
11
|
import { decodeVideoThumbnail } from "../lib/thumbnailVideoDecoder";
|
|
12
|
+
import { usePlayerStore } from "../store/playerStore";
|
|
10
13
|
import { ThumbnailTiles } from "./ThumbnailTiles";
|
|
11
14
|
import {
|
|
12
15
|
computeThumbnailStrip,
|
|
@@ -15,6 +18,9 @@ import {
|
|
|
15
18
|
} from "./thumbnailUtils";
|
|
16
19
|
import { useValueAtRest } from "./timelineMotion";
|
|
17
20
|
|
|
21
|
+
const unbindPlaybackHold = bindThumbnailPlaybackHold(usePlayerStore, thumbnailScheduler);
|
|
22
|
+
import.meta.hot?.dispose(unbindPlaybackHold);
|
|
23
|
+
|
|
18
24
|
interface VideoThumbnailProps {
|
|
19
25
|
videoSrc: string;
|
|
20
26
|
label: string;
|
|
@@ -95,6 +95,10 @@ describe("preview quality messages", () => {
|
|
|
95
95
|
choppy: true,
|
|
96
96
|
fps: 18,
|
|
97
97
|
worstFrameMs: 90,
|
|
98
|
+
skippedShare: 0.1,
|
|
99
|
+
waitingShare: 0.4,
|
|
100
|
+
gapP50Ms: 17,
|
|
101
|
+
gapP95Ms: 66,
|
|
98
102
|
mode: "play",
|
|
99
103
|
};
|
|
100
104
|
routeMessage({ source: "hf-preview", type: "preview-health", health });
|
|
@@ -106,6 +106,313 @@ describe("mediaUrlSourceOptions", () => {
|
|
|
106
106
|
}
|
|
107
107
|
});
|
|
108
108
|
|
|
109
|
+
it("fails a read stalled mid-body once its reader is disposed and its reads cancelled", async () => {
|
|
110
|
+
// Like fetch, the stub ends its body when the request's signal aborts, and not before.
|
|
111
|
+
vi.stubGlobal(
|
|
112
|
+
"fetch",
|
|
113
|
+
vi.fn(async (_input: RequestInfo | URL, init?: RequestInit) => {
|
|
114
|
+
let sent = false;
|
|
115
|
+
const body = new ReadableStream<Uint8Array>({
|
|
116
|
+
pull(controller) {
|
|
117
|
+
if (!sent) {
|
|
118
|
+
sent = true;
|
|
119
|
+
return controller.enqueue(new Uint8Array(16));
|
|
120
|
+
}
|
|
121
|
+
return new Promise<void>((resolve) => {
|
|
122
|
+
const end = () => {
|
|
123
|
+
controller.error(init?.signal?.reason);
|
|
124
|
+
resolve();
|
|
125
|
+
};
|
|
126
|
+
init?.signal?.addEventListener("abort", end, { once: true });
|
|
127
|
+
});
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
const headers = { "Content-Range": "bytes 0-999999/1000000", "Content-Length": "1000000" };
|
|
131
|
+
return new Response(body, { status: 206, headers });
|
|
132
|
+
}),
|
|
133
|
+
);
|
|
134
|
+
const mb = await import("mediabunny");
|
|
135
|
+
const runnerListeners = process.listeners("unhandledRejection");
|
|
136
|
+
process.removeAllListeners("unhandledRejection");
|
|
137
|
+
process.on("unhandledRejection", () => {});
|
|
138
|
+
try {
|
|
139
|
+
const options = mediaUrlSourceOptions("thumbnail");
|
|
140
|
+
const input = new mb.Input({
|
|
141
|
+
source: new mb.UrlSource(URL_WITH_PATH, options),
|
|
142
|
+
formats: mb.ALL_FORMATS,
|
|
143
|
+
});
|
|
144
|
+
let settled = false;
|
|
145
|
+
const read = input.getDurationFromMetadata().finally(() => (settled = true));
|
|
146
|
+
read.catch(() => {});
|
|
147
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
148
|
+
expect(settled).toBe(false);
|
|
149
|
+
|
|
150
|
+
input.dispose();
|
|
151
|
+
options.cancelReads();
|
|
152
|
+
await expect(read).rejects.toBeDefined();
|
|
153
|
+
// The cancelled read-ahead's own rejection lands a turn later; keep it out of the next test.
|
|
154
|
+
await new Promise(setImmediate);
|
|
155
|
+
await new Promise(setImmediate);
|
|
156
|
+
} finally {
|
|
157
|
+
process.removeAllListeners("unhandledRejection");
|
|
158
|
+
for (const listener of runnerListeners) process.on("unhandledRejection", listener);
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
// Like fetch, a stub whose waits end when the request's signal aborts, and not before.
|
|
163
|
+
const endsOnAbort = (signal: AbortSignal | null | undefined) =>
|
|
164
|
+
new Promise<never>((_, reject) =>
|
|
165
|
+
signal?.addEventListener("abort", () => reject(signal.reason), { once: true }),
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
it("gives up on a response that sends no bytes, as a stall, inside the load timeout", async () => {
|
|
169
|
+
vi.useFakeTimers();
|
|
170
|
+
const requests: (string | null)[] = [];
|
|
171
|
+
vi.stubGlobal(
|
|
172
|
+
"fetch",
|
|
173
|
+
vi.fn(async (_input: RequestInfo | URL, init?: RequestInit) => {
|
|
174
|
+
requests.push(new Headers(init?.headers).get("Range"));
|
|
175
|
+
const body = new ReadableStream<Uint8Array>({ pull: () => endsOnAbort(init?.signal) });
|
|
176
|
+
const headers = { "Content-Range": "bytes 0-999999/1000000", "Content-Length": "1000000" };
|
|
177
|
+
return new Response(body, { status: 206, headers });
|
|
178
|
+
}),
|
|
179
|
+
);
|
|
180
|
+
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
181
|
+
const mb = await import("mediabunny");
|
|
182
|
+
const runnerListeners = process.listeners("unhandledRejection");
|
|
183
|
+
process.removeAllListeners("unhandledRejection");
|
|
184
|
+
process.on("unhandledRejection", () => {});
|
|
185
|
+
try {
|
|
186
|
+
const input = new mb.Input({
|
|
187
|
+
source: new mb.UrlSource(URL_WITH_PATH, mediaUrlSourceOptions("thumbnail")),
|
|
188
|
+
formats: mb.ALL_FORMATS,
|
|
189
|
+
});
|
|
190
|
+
let outcome: unknown = "pending";
|
|
191
|
+
void input.getDurationFromMetadata().then(
|
|
192
|
+
() => (outcome = "resolved"),
|
|
193
|
+
(error: unknown) => (outcome = error),
|
|
194
|
+
);
|
|
195
|
+
// Three silent waits of 8 s, with mediabunny's 0.5 s and 1 s retry delays between them.
|
|
196
|
+
await vi.advanceTimersByTimeAsync(25_400);
|
|
197
|
+
expect(outcome).toBe("pending");
|
|
198
|
+
await vi.advanceTimersByTimeAsync(200);
|
|
199
|
+
expect(outcome).toMatchObject({
|
|
200
|
+
name: "MediaFetchError",
|
|
201
|
+
message: "thumbnail range fetch failed: network stalled after 2 retries",
|
|
202
|
+
});
|
|
203
|
+
expect(requests).toHaveLength(3);
|
|
204
|
+
input.dispose();
|
|
205
|
+
} finally {
|
|
206
|
+
vi.useRealTimers();
|
|
207
|
+
// mediabunny rethrows the failed read-ahead a turn later; keep it out of the next test.
|
|
208
|
+
await new Promise(setImmediate);
|
|
209
|
+
await new Promise(setImmediate);
|
|
210
|
+
process.removeAllListeners("unhandledRejection");
|
|
211
|
+
for (const listener of runnerListeners) process.on("unhandledRejection", listener);
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it("never cuts off a request still waiting for its answer, however long it queues", async () => {
|
|
216
|
+
// Chrome queues a request for one of six connections per host, which streaming previews may hold.
|
|
217
|
+
vi.useFakeTimers();
|
|
218
|
+
vi.stubGlobal(
|
|
219
|
+
"fetch",
|
|
220
|
+
vi.fn(
|
|
221
|
+
() =>
|
|
222
|
+
new Promise<Response>((resolve) =>
|
|
223
|
+
setTimeout(() => resolve(new Response("", { status: 206 })), 20_000),
|
|
224
|
+
),
|
|
225
|
+
),
|
|
226
|
+
);
|
|
227
|
+
try {
|
|
228
|
+
const answer = mediaUrlSourceOptions("thumbnail").fetchFn(URL_WITH_PATH, {
|
|
229
|
+
headers: { Range: "bytes=0-" },
|
|
230
|
+
});
|
|
231
|
+
await vi.advanceTimersByTimeAsync(20_000);
|
|
232
|
+
expect((await answer).status).toBe(206);
|
|
233
|
+
} finally {
|
|
234
|
+
vi.useRealTimers();
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it("fails a body that sends nothing for 8 s as stalled, and aborts its fetch", async () => {
|
|
239
|
+
vi.useFakeTimers();
|
|
240
|
+
let sentSignal: AbortSignal | undefined;
|
|
241
|
+
vi.stubGlobal(
|
|
242
|
+
"fetch",
|
|
243
|
+
vi.fn(async (_input: RequestInfo | URL, init?: RequestInit) => {
|
|
244
|
+
sentSignal = init?.signal ?? undefined;
|
|
245
|
+
// Deaf to its signal: only the watchdog can end this wait.
|
|
246
|
+
const body = new ReadableStream<Uint8Array>({ pull: () => new Promise<void>(() => {}) });
|
|
247
|
+
return new Response(body, { status: 206 });
|
|
248
|
+
}),
|
|
249
|
+
);
|
|
250
|
+
try {
|
|
251
|
+
const response = await mediaUrlSourceOptions("thumbnail").fetchFn(URL_WITH_PATH, {
|
|
252
|
+
headers: { Range: "bytes=4096-" },
|
|
253
|
+
});
|
|
254
|
+
const read = response
|
|
255
|
+
.body!.getReader()
|
|
256
|
+
.read()
|
|
257
|
+
.catch((e: unknown) => e);
|
|
258
|
+
await vi.advanceTimersByTimeAsync(8_000);
|
|
259
|
+
expect(await read).toMatchObject({ name: "MediaFetchError", code: "stalled", start: 4096 });
|
|
260
|
+
expect(sentSignal?.aborted).toBe(true);
|
|
261
|
+
} finally {
|
|
262
|
+
vi.useRealTimers();
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it("never times out a probe's body, however slow", async () => {
|
|
267
|
+
vi.useFakeTimers();
|
|
268
|
+
vi.stubGlobal(
|
|
269
|
+
"fetch",
|
|
270
|
+
vi.fn(async () => {
|
|
271
|
+
const body = new ReadableStream<Uint8Array>({
|
|
272
|
+
pull: (controller) =>
|
|
273
|
+
new Promise<void>((resolve) =>
|
|
274
|
+
setTimeout(() => {
|
|
275
|
+
controller.enqueue(new Uint8Array(8));
|
|
276
|
+
controller.close();
|
|
277
|
+
resolve();
|
|
278
|
+
}, 20_000),
|
|
279
|
+
),
|
|
280
|
+
});
|
|
281
|
+
return new Response(body, { status: 206 });
|
|
282
|
+
}),
|
|
283
|
+
);
|
|
284
|
+
try {
|
|
285
|
+
const response = await mediaUrlSourceOptions("probe").fetchFn(URL_WITH_PATH, {
|
|
286
|
+
headers: { Range: "bytes=0-" },
|
|
287
|
+
});
|
|
288
|
+
const read = response.arrayBuffer();
|
|
289
|
+
await vi.advanceTimersByTimeAsync(20_000);
|
|
290
|
+
expect((await read).byteLength).toBe(8);
|
|
291
|
+
} finally {
|
|
292
|
+
vi.useRealTimers();
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
it("starts every request made after its reader's reads were cancelled already aborted", async () => {
|
|
297
|
+
const seen: AbortSignal[] = [];
|
|
298
|
+
vi.stubGlobal(
|
|
299
|
+
"fetch",
|
|
300
|
+
vi.fn(async (_input: RequestInfo | URL, init?: RequestInit) => {
|
|
301
|
+
if (init?.signal) seen.push(init.signal);
|
|
302
|
+
return new Response("", { status: 206 });
|
|
303
|
+
}),
|
|
304
|
+
);
|
|
305
|
+
const { fetchFn, cancelReads } = mediaUrlSourceOptions("thumbnail");
|
|
306
|
+
cancelReads();
|
|
307
|
+
await fetchFn(URL_WITH_PATH, { headers: { Range: "bytes=0-" } }).catch(() => {});
|
|
308
|
+
await Promise.resolve();
|
|
309
|
+
expect(seen[0]?.aborted).toBe(true);
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
it("unlinks every request from mediabunny's signal however it ends", async () => {
|
|
313
|
+
const answers: (() => Promise<Response>)[] = [
|
|
314
|
+
async () => new Response(new Uint8Array(4), { status: 206 }),
|
|
315
|
+
async () => Promise.reject(new TypeError("Failed to fetch")),
|
|
316
|
+
async () => new Response("", { status: 503 }),
|
|
317
|
+
async () => new Response(null, { status: 206 }),
|
|
318
|
+
async () =>
|
|
319
|
+
new Response(
|
|
320
|
+
new ReadableStream({ pull: (controller) => controller.error(new TypeError("network")) }),
|
|
321
|
+
{ status: 206 },
|
|
322
|
+
),
|
|
323
|
+
async () =>
|
|
324
|
+
new Response(
|
|
325
|
+
new ReadableStream({ pull: (controller) => controller.enqueue(new Uint8Array(4)) }),
|
|
326
|
+
{ status: 206 },
|
|
327
|
+
),
|
|
328
|
+
];
|
|
329
|
+
let call = 0;
|
|
330
|
+
vi.stubGlobal(
|
|
331
|
+
"fetch",
|
|
332
|
+
vi.fn(() => answers[call++]!()),
|
|
333
|
+
);
|
|
334
|
+
const { fetchFn } = mediaUrlSourceOptions("thumbnail");
|
|
335
|
+
let added = 0;
|
|
336
|
+
let removed = 0;
|
|
337
|
+
for (const exit of ["ok", "rejected", "503", "no body", "body error", "cancel"]) {
|
|
338
|
+
const signal = new AbortController().signal;
|
|
339
|
+
const add = signal.addEventListener.bind(signal);
|
|
340
|
+
const remove = signal.removeEventListener.bind(signal);
|
|
341
|
+
signal.addEventListener = (...args: Parameters<typeof add>) => (added++, add(...args));
|
|
342
|
+
signal.removeEventListener = (...args: Parameters<typeof remove>) => (
|
|
343
|
+
removed++, remove(...args)
|
|
344
|
+
);
|
|
345
|
+
const response = await fetchFn(URL_WITH_PATH, {
|
|
346
|
+
headers: { Range: "bytes=0-" },
|
|
347
|
+
signal,
|
|
348
|
+
}).catch(() => null);
|
|
349
|
+
const reader = response?.body?.getReader();
|
|
350
|
+
if (exit === "cancel") {
|
|
351
|
+
await reader!.read();
|
|
352
|
+
await reader!.cancel();
|
|
353
|
+
} else if (reader) {
|
|
354
|
+
await (async () => {
|
|
355
|
+
for (;;) if ((await reader.read()).done) return;
|
|
356
|
+
})().catch(() => {});
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
expect({ added, removed }).toEqual({ added: 6, removed: 6 });
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
it("never cuts off a slow response that keeps sending", async () => {
|
|
364
|
+
vi.useFakeTimers();
|
|
365
|
+
vi.stubGlobal(
|
|
366
|
+
"fetch",
|
|
367
|
+
vi.fn(async (_input: RequestInfo | URL, init?: RequestInit) => {
|
|
368
|
+
let chunks = 0;
|
|
369
|
+
const body = new ReadableStream<Uint8Array>({
|
|
370
|
+
// Like fetch, the body fails if the request is aborted.
|
|
371
|
+
start: (controller) =>
|
|
372
|
+
init?.signal?.addEventListener("abort", () => controller.error(init.signal?.reason)),
|
|
373
|
+
pull: (controller) =>
|
|
374
|
+
new Promise<void>((resolve) =>
|
|
375
|
+
setTimeout(() => {
|
|
376
|
+
if (++chunks > 10) controller.close();
|
|
377
|
+
else controller.enqueue(new Uint8Array(1024));
|
|
378
|
+
resolve();
|
|
379
|
+
}, 2_000),
|
|
380
|
+
),
|
|
381
|
+
});
|
|
382
|
+
return new Response(body, { status: 206 });
|
|
383
|
+
}),
|
|
384
|
+
);
|
|
385
|
+
try {
|
|
386
|
+
const response = await mediaUrlSourceOptions("thumbnail").fetchFn(URL_WITH_PATH, {
|
|
387
|
+
headers: { Range: "bytes=0-" },
|
|
388
|
+
});
|
|
389
|
+
const read = response.arrayBuffer();
|
|
390
|
+
await vi.advanceTimersByTimeAsync(24_000);
|
|
391
|
+
expect((await read).byteLength).toBe(10 * 1024);
|
|
392
|
+
} finally {
|
|
393
|
+
vi.useRealTimers();
|
|
394
|
+
}
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
it("aborts a request when mediabunny aborts its own signal", async () => {
|
|
398
|
+
const seen: AbortSignal[] = [];
|
|
399
|
+
vi.stubGlobal(
|
|
400
|
+
"fetch",
|
|
401
|
+
vi.fn(async (_input: RequestInfo | URL, init?: RequestInit) => {
|
|
402
|
+
if (init?.signal) seen.push(init.signal);
|
|
403
|
+
return new Response("", { status: 206 });
|
|
404
|
+
}),
|
|
405
|
+
);
|
|
406
|
+
const stop = new AbortController();
|
|
407
|
+
await mediaUrlSourceOptions("thumbnail").fetchFn(URL_WITH_PATH, {
|
|
408
|
+
headers: { Range: "bytes=0-" },
|
|
409
|
+
signal: stop.signal,
|
|
410
|
+
});
|
|
411
|
+
expect(seen[0]?.aborted).toBe(false);
|
|
412
|
+
stop.abort();
|
|
413
|
+
expect(seen[0]?.aborted).toBe(true);
|
|
414
|
+
});
|
|
415
|
+
|
|
109
416
|
it("counts mid-stream read failures, which always arrive as attempt 1", () => {
|
|
110
417
|
const { getRetryDelay } = mediaUrlSourceOptions("thumbnail");
|
|
111
418
|
const readError = new TypeError("network error");
|
|
@@ -113,6 +420,84 @@ describe("mediaUrlSourceOptions", () => {
|
|
|
113
420
|
expect([1, 1, 1].map((attempt) => getRetryDelay(attempt, readError))).toEqual([0.5, 1, null]);
|
|
114
421
|
});
|
|
115
422
|
|
|
423
|
+
// What mediabunny does with a body that breaks: feed the failure to getRetryDelay, then ask again.
|
|
424
|
+
async function readUntilBroken(
|
|
425
|
+
fetchFn: ReturnType<typeof mediaUrlSourceOptions>["fetchFn"],
|
|
426
|
+
range: string,
|
|
427
|
+
) {
|
|
428
|
+
const reader = (await fetchFn(URL_WITH_PATH, { headers: { Range: range } })).body!.getReader();
|
|
429
|
+
for (;;) await reader.read();
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
it("gives up on a server that ignores Range and breaks at the same byte, whatever it sent first", async () => {
|
|
433
|
+
vi.stubGlobal(
|
|
434
|
+
"fetch",
|
|
435
|
+
vi.fn(async () => {
|
|
436
|
+
let sent = false;
|
|
437
|
+
const body = new ReadableStream<Uint8Array>({
|
|
438
|
+
pull(controller) {
|
|
439
|
+
if (sent) return controller.error(new TypeError("network error"));
|
|
440
|
+
sent = true;
|
|
441
|
+
controller.enqueue(new Uint8Array(64 * 1024));
|
|
442
|
+
},
|
|
443
|
+
});
|
|
444
|
+
return new Response(body, { status: 200 });
|
|
445
|
+
}),
|
|
446
|
+
);
|
|
447
|
+
const { fetchFn, getRetryDelay } = mediaUrlSourceOptions("thumbnail");
|
|
448
|
+
const delays: (number | null)[] = [];
|
|
449
|
+
// A 200 answers from byte 0, so mediabunny resumes at 64 KiB and is sent byte 0 again.
|
|
450
|
+
for (const range of ["bytes=0-", "bytes=65536-", "bytes=65536-"]) {
|
|
451
|
+
const failure = await readUntilBroken(fetchFn, range).catch((e: unknown) => e);
|
|
452
|
+
delays.push(getRetryDelay(1, failure));
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
expect(delays).toEqual([0.5, 1, null]);
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
it("gives up on a region that keeps failing while another region of the same reader streams", async () => {
|
|
459
|
+
vi.stubGlobal(
|
|
460
|
+
"fetch",
|
|
461
|
+
vi.fn(async (_input: RequestInfo | URL, init?: RequestInit) => {
|
|
462
|
+
const start = Number(/\d+/.exec(new Headers(init?.headers).get("Range") ?? "")?.[0]);
|
|
463
|
+
if (start >= 500_000) return new Response("", { status: 503 });
|
|
464
|
+
const headers = { "Content-Range": `bytes ${start}-999999/1000000` };
|
|
465
|
+
return new Response(new Uint8Array(1024), { status: 206, headers });
|
|
466
|
+
}),
|
|
467
|
+
);
|
|
468
|
+
const { fetchFn, getRetryDelay } = mediaUrlSourceOptions("thumbnail");
|
|
469
|
+
const delays: (number | null)[] = [];
|
|
470
|
+
for (const healthy of [0, 1024, 2048]) {
|
|
471
|
+
await (
|
|
472
|
+
await fetchFn(URL_WITH_PATH, { headers: { Range: `bytes=${healthy}-` } })
|
|
473
|
+
).arrayBuffer();
|
|
474
|
+
const failure = await fetchFn(URL_WITH_PATH, { headers: { Range: "bytes=600000-" } }).catch(
|
|
475
|
+
(e: unknown) => e,
|
|
476
|
+
);
|
|
477
|
+
delays.push(getRetryDelay(1, failure));
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
expect(delays).toEqual([0.5, 1, null]);
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
it("keeps a budget for each start, so failures on many clips of one reader never add up", async () => {
|
|
484
|
+
vi.stubGlobal("fetch", vi.fn().mockRejectedValue(new TypeError("Failed to fetch")));
|
|
485
|
+
const { fetchFn, getRetryDelay } = mediaUrlSourceOptions("thumbnail");
|
|
486
|
+
const failAt = async (start: number) =>
|
|
487
|
+
getRetryDelay(
|
|
488
|
+
1,
|
|
489
|
+
await fetchFn(URL_WITH_PATH, { headers: { Range: `bytes=${start}-` } }).catch(
|
|
490
|
+
(e: unknown) => e,
|
|
491
|
+
),
|
|
492
|
+
);
|
|
493
|
+
const spread = [];
|
|
494
|
+
for (const start of [0, 1000, 2000, 3000, 4000]) spread.push(await failAt(start));
|
|
495
|
+
const repeated = [await failAt(5000), await failAt(5000), await failAt(5000)];
|
|
496
|
+
|
|
497
|
+
expect(spread).toEqual([0.5, 0.5, 0.5, 0.5, 0.5]);
|
|
498
|
+
expect(repeated).toEqual([0.5, 1, null]);
|
|
499
|
+
});
|
|
500
|
+
|
|
116
501
|
it("does not retry a client error status", () => {
|
|
117
502
|
const failure = new MediaFetchError("probe", 404, null);
|
|
118
503
|
|
|
@@ -147,6 +532,87 @@ describe("mediaUrlSourceOptions", () => {
|
|
|
147
532
|
});
|
|
148
533
|
});
|
|
149
534
|
|
|
535
|
+
describe("thumbnail reads", () => {
|
|
536
|
+
// A 206 answering each request, with its Content-Range readable or (cross-origin, unexposed) hidden.
|
|
537
|
+
function rangeServer(answers: ("readable" | "hidden" | "whole" | { start: number })[]) {
|
|
538
|
+
const ranges: (string | null)[] = [];
|
|
539
|
+
vi.stubGlobal(
|
|
540
|
+
"fetch",
|
|
541
|
+
vi.fn(async (_input: RequestInfo | URL, init?: RequestInit) => {
|
|
542
|
+
const range = new Headers(init?.headers).get("Range");
|
|
543
|
+
ranges.push(range);
|
|
544
|
+
const answer = answers[ranges.length - 1] ?? "readable";
|
|
545
|
+
const start = typeof answer === "object" ? answer.start : Number(/\d+/.exec(range ?? "0"));
|
|
546
|
+
const headers =
|
|
547
|
+
answer === "hidden" ? {} : { "Content-Range": `bytes ${start}-9999999/10000000` };
|
|
548
|
+
return new Response("", { status: answer === "whole" ? 200 : 206, headers });
|
|
549
|
+
}),
|
|
550
|
+
);
|
|
551
|
+
return ranges;
|
|
552
|
+
}
|
|
553
|
+
const BOUND = 512 * 1024;
|
|
554
|
+
|
|
555
|
+
it("bounds an open-ended thumbnail range once this reader has read a Content-Range", async () => {
|
|
556
|
+
const ranges = rangeServer(["readable"]);
|
|
557
|
+
const { fetchFn } = mediaUrlSourceOptions("thumbnail");
|
|
558
|
+
await fetchFn(URL_WITH_PATH, { headers: { Range: "bytes=0-" } });
|
|
559
|
+
await fetchFn(URL_WITH_PATH, { headers: { Range: "bytes=1000-" } });
|
|
560
|
+
await fetchFn(URL_WITH_PATH, { headers: new Headers({ Range: "bytes=0-99" }) });
|
|
561
|
+
// A bounded answer's Content-Range is read too, so the reader stays bounded.
|
|
562
|
+
await fetchFn(URL_WITH_PATH, { headers: { Range: "bytes=2000-" } });
|
|
563
|
+
|
|
564
|
+
// The first stays open-ended: without a readable Content-Range mediabunny sizes the file from it.
|
|
565
|
+
expect(ranges).toEqual([
|
|
566
|
+
"bytes=0-",
|
|
567
|
+
`bytes=1000-${1000 + BOUND - 1}`,
|
|
568
|
+
"bytes=0-99",
|
|
569
|
+
`bytes=2000-${2000 + BOUND - 1}`,
|
|
570
|
+
]);
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
it("never bounds a reader once an answer hid its Content-Range", async () => {
|
|
574
|
+
const ranges = rangeServer(["hidden", "readable"]);
|
|
575
|
+
const { fetchFn } = mediaUrlSourceOptions("thumbnail");
|
|
576
|
+
for (const start of [0, 1000, 2000]) {
|
|
577
|
+
await fetchFn(URL_WITH_PATH, { headers: { Range: `bytes=${start}-` } });
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
expect(ranges).toEqual(["bytes=0-", "bytes=1000-", "bytes=2000-"]);
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
it("does not trust a Content-Range for a range it did not ask for", async () => {
|
|
584
|
+
const ranges = rangeServer([{ start: 4096 }]);
|
|
585
|
+
const { fetchFn } = mediaUrlSourceOptions("thumbnail");
|
|
586
|
+
await fetchFn(URL_WITH_PATH, { headers: { Range: "bytes=0-" } });
|
|
587
|
+
await fetchFn(URL_WITH_PATH, { headers: { Range: "bytes=1000-" } });
|
|
588
|
+
|
|
589
|
+
expect(ranges).toEqual(["bytes=0-", "bytes=1000-"]);
|
|
590
|
+
});
|
|
591
|
+
|
|
592
|
+
it("does not trust a whole-file answer, whatever its headers say", async () => {
|
|
593
|
+
const ranges = rangeServer(["whole"]);
|
|
594
|
+
const { fetchFn } = mediaUrlSourceOptions("thumbnail");
|
|
595
|
+
await fetchFn(URL_WITH_PATH, { headers: { Range: "bytes=0-" } });
|
|
596
|
+
await fetchFn(URL_WITH_PATH, { headers: { Range: "bytes=1000-" } });
|
|
597
|
+
|
|
598
|
+
expect(ranges).toEqual(["bytes=0-", "bytes=1000-"]);
|
|
599
|
+
});
|
|
600
|
+
|
|
601
|
+
it("never bounds a probe's range", async () => {
|
|
602
|
+
const ranges = rangeServer(["readable"]);
|
|
603
|
+
const { fetchFn } = mediaUrlSourceOptions("probe");
|
|
604
|
+
await fetchFn(URL_WITH_PATH, { headers: { Range: "bytes=0-" } });
|
|
605
|
+
await fetchFn(URL_WITH_PATH, { headers: { Range: "bytes=1000-" } });
|
|
606
|
+
|
|
607
|
+
expect(ranges).toEqual(["bytes=0-", "bytes=1000-"]);
|
|
608
|
+
});
|
|
609
|
+
|
|
610
|
+
it("keeps a thumbnail reader's cache at 16 MiB, and a probe's at mediabunny's default", () => {
|
|
611
|
+
expect(mediaUrlSourceOptions("thumbnail")).toMatchObject({ maxCacheSize: 16 * 1024 * 1024 });
|
|
612
|
+
expect(mediaUrlSourceOptions("probe")).not.toHaveProperty("maxCacheSize");
|
|
613
|
+
});
|
|
614
|
+
});
|
|
615
|
+
|
|
150
616
|
describe("reportMediaFetchFailure", () => {
|
|
151
617
|
it("writes one scrubbed line for a typed failure and nothing for anything else", () => {
|
|
152
618
|
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|