@hyperframes/studio 0.8.211 → 0.8.213
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-CzrqgMUX.js → hyperframes-player-Bb6n05RB.js} +1 -1
- package/dist/assets/{index-B3SPHPi1.js → index-DOtf09XR.js} +1 -1
- package/dist/assets/{index-k4nVfS2D.js → index-pq574E5O.js} +119 -119
- package/dist/{chunk-HD6NXWGI.js → chunk-UNZTUFD7.js} +2 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.html +1 -1
- package/dist/index.js +1078 -849
- package/dist/player.js +1 -1
- package/package.json +8 -8
- package/src/hooks/timelineEditingHelpers.test.ts +25 -0
- package/src/hooks/timelineEditingHelpers.ts +17 -1
- package/src/hooks/usePersistentEditHistory.test.ts +7 -3
- package/src/player/hooks/previewMessageRouter.test.ts +40 -0
- package/src/player/hooks/previewMessageRouter.ts +12 -0
- package/src/player/lib/mediaFetch.test.ts +106 -0
- package/src/player/lib/mediaFetch.ts +15 -4
- package/src/player/lib/thumbnailDiskCache.test.ts +158 -0
- package/src/player/lib/thumbnailDiskCache.ts +212 -0
- package/src/player/lib/thumbnailSourcePool.ts +3 -1
- package/src/player/lib/thumbnailVideoDecoder.test.ts +79 -0
- package/src/player/lib/thumbnailVideoDecoder.ts +97 -3
- package/src/player/store/previewQualitySlice.ts +5 -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.213",
|
|
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/
|
|
65
|
-
"@hyperframes/
|
|
66
|
-
"@hyperframes/
|
|
67
|
-
"@hyperframes/
|
|
68
|
-
"@hyperframes/studio-server": "0.8.
|
|
63
|
+
"@hyperframes/core": "0.8.213",
|
|
64
|
+
"@hyperframes/player": "0.8.213",
|
|
65
|
+
"@hyperframes/parsers": "0.8.213",
|
|
66
|
+
"@hyperframes/sdk": "0.8.213",
|
|
67
|
+
"@hyperframes/shader-transitions": "0.8.213",
|
|
68
|
+
"@hyperframes/studio-server": "0.8.213"
|
|
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.213"
|
|
86
86
|
},
|
|
87
87
|
"peerDependencies": {
|
|
88
88
|
"react": "19",
|
|
@@ -414,6 +414,31 @@ describe("persistTimelineBatchEdit", () => {
|
|
|
414
414
|
).rejects.toThrow("Unable to patch timeline element ghost in index.html");
|
|
415
415
|
expect(writes).toHaveLength(0);
|
|
416
416
|
});
|
|
417
|
+
|
|
418
|
+
it("names which lookups a missing clip had, never their values", async () => {
|
|
419
|
+
stubReadFileContent(SOURCE);
|
|
420
|
+
// As the timeline builds them: a clip with an id also carries `#id` as its selector.
|
|
421
|
+
const missing = (element: Partial<TimelineElement>) => {
|
|
422
|
+
const change = moveMember("ghost", 3, 0, 2);
|
|
423
|
+
change.element = { ...change.element, ...element };
|
|
424
|
+
return persistTimelineBatchEdit(batchInput([change], []));
|
|
425
|
+
};
|
|
426
|
+
await expect(
|
|
427
|
+
missing({
|
|
428
|
+
domId: "intro__hf2",
|
|
429
|
+
hfId: "hf-zz9q",
|
|
430
|
+
selector: "#intro__hf2",
|
|
431
|
+
sourceFile: "compositions/intro.html",
|
|
432
|
+
}),
|
|
433
|
+
).rejects.toThrow("(by hf-id, runtime-renamed id; file from the clip)");
|
|
434
|
+
await expect(missing({ selector: "#ghost" })).rejects.toThrow("(by id; no file on the clip)");
|
|
435
|
+
await expect(
|
|
436
|
+
missing({ domId: undefined, selector: ".caption", selectorIndex: 4 }),
|
|
437
|
+
).rejects.toThrow("(by class; no file on the clip)");
|
|
438
|
+
await expect(
|
|
439
|
+
missing({ domId: undefined, selector: '[data-composition-id="intro__hf2"]' }),
|
|
440
|
+
).rejects.toThrow("(by runtime-renamed composition id; no file on the clip)");
|
|
441
|
+
});
|
|
417
442
|
});
|
|
418
443
|
|
|
419
444
|
describe("persistTimelineEdit", () => {
|
|
@@ -387,6 +387,20 @@ export function operationChanges(
|
|
|
387
387
|
}));
|
|
388
388
|
}
|
|
389
389
|
|
|
390
|
+
const RENAMED_MOUNT = /__hf\d+"?\]?$/;
|
|
391
|
+
const lookup = (value: string, kind: string) =>
|
|
392
|
+
RENAMED_MOUNT.test(value) ? `runtime-renamed ${kind}` : kind;
|
|
393
|
+
const selectorLookup = (selector: string) =>
|
|
394
|
+
selector.startsWith("[data-composition-id=")
|
|
395
|
+
? lookup(selector, "composition id")
|
|
396
|
+
: /^\.[\w-]+$/.test(selector) && "class";
|
|
397
|
+
|
|
398
|
+
function missedLookups(element: Pick<TimelineElement, "sourceFile">, target: PatchTarget): string {
|
|
399
|
+
const kinds = [target.hfId && "hf-id", target.id && lookup(target.id, "id")];
|
|
400
|
+
const by = [...kinds, target.selector && selectorLookup(target.selector)].filter(Boolean);
|
|
401
|
+
return `by ${by.join(", ") || "nothing"}; ${element.sourceFile ? "file from the clip" : "no file on the clip"}`;
|
|
402
|
+
}
|
|
403
|
+
|
|
390
404
|
/** Patches each change into `source`, failing loudly on a target the file does not hold. */
|
|
391
405
|
export function patchTimelineChangesInSource(
|
|
392
406
|
source: string,
|
|
@@ -399,7 +413,9 @@ export function patchTimelineChangesInSource(
|
|
|
399
413
|
if (!target) throw new Error(`Timeline element ${element.id} is missing a patchable target`);
|
|
400
414
|
// Resolve first: a member already at its target values patches to the same string, a missing one must throw.
|
|
401
415
|
if (!findTagByTarget(current, target)) {
|
|
402
|
-
throw new Error(
|
|
416
|
+
throw new Error(
|
|
417
|
+
`Unable to patch timeline element ${element.id} in ${targetPath} (${missedLookups(element, target)})`,
|
|
418
|
+
);
|
|
403
419
|
}
|
|
404
420
|
current = buildPatches(current, target);
|
|
405
421
|
}
|
|
@@ -317,8 +317,12 @@ it("refuses a second press whose edit is undone once an edit was made after the
|
|
|
317
317
|
return run;
|
|
318
318
|
};
|
|
319
319
|
const press = () => hook().undo({ readFile, claimedAfter: atKey, serialize });
|
|
320
|
-
const
|
|
321
|
-
|
|
320
|
+
const firstPress = press();
|
|
321
|
+
const secondPress = press();
|
|
322
|
+
// The first press must be over, not only its file written: the undo's closing sweep would take card.html
|
|
323
|
+
// into the undo, and the claim below would then hold nothing.
|
|
324
|
+
const first = await act(() => firstPress);
|
|
325
|
+
expect(file()).toBe("B");
|
|
322
326
|
writeFileSync(join(dir, "card.html"), "Y");
|
|
323
327
|
await act(() =>
|
|
324
328
|
hook().recordEdit({
|
|
@@ -328,7 +332,7 @@ it("refuses a second press whose edit is undone once an edit was made after the
|
|
|
328
332
|
);
|
|
329
333
|
|
|
330
334
|
open();
|
|
331
|
-
const
|
|
335
|
+
const second = await act(() => secondPress);
|
|
332
336
|
|
|
333
337
|
expect(first).toMatchObject({ ok: true, label: "Undid: Moved Card" });
|
|
334
338
|
expect(second).toMatchObject({ ok: false, reason: "content-mismatch" });
|
|
@@ -128,6 +128,46 @@ describe("preview quality messages", () => {
|
|
|
128
128
|
expect(usePlayerStore.getState().previewFacts).toBeNull();
|
|
129
129
|
});
|
|
130
130
|
|
|
131
|
+
it("keeps each failed media load the preview reports, and ignores other diagnostics", () => {
|
|
132
|
+
const details = {
|
|
133
|
+
asset: "assets/x.mp3",
|
|
134
|
+
elementId: "music",
|
|
135
|
+
kind: "audio",
|
|
136
|
+
stage: "decode",
|
|
137
|
+
code: null,
|
|
138
|
+
bytes: 8,
|
|
139
|
+
extension: "mp3",
|
|
140
|
+
};
|
|
141
|
+
routeMessage({
|
|
142
|
+
source: "hf-preview",
|
|
143
|
+
type: "diagnostic",
|
|
144
|
+
code: "runtime_media_load_failed",
|
|
145
|
+
details,
|
|
146
|
+
});
|
|
147
|
+
expect(usePlayerStore.getState().mediaLoadFailure).toEqual({
|
|
148
|
+
elementId: "music",
|
|
149
|
+
kind: "audio",
|
|
150
|
+
stage: "decode",
|
|
151
|
+
code: null,
|
|
152
|
+
bytes: 8,
|
|
153
|
+
extension: "mp3",
|
|
154
|
+
});
|
|
155
|
+
const kept = usePlayerStore.getState().mediaLoadFailure;
|
|
156
|
+
routeMessage({
|
|
157
|
+
source: "hf-preview",
|
|
158
|
+
type: "diagnostic",
|
|
159
|
+
code: "runtime_web_audio_bypass",
|
|
160
|
+
details,
|
|
161
|
+
});
|
|
162
|
+
routeMessage({
|
|
163
|
+
source: "hf-preview",
|
|
164
|
+
type: "diagnostic",
|
|
165
|
+
code: "runtime_media_load_failed",
|
|
166
|
+
details: { ...details, stage: "?" },
|
|
167
|
+
});
|
|
168
|
+
expect(usePlayerStore.getState().mediaLoadFailure).toBe(kept);
|
|
169
|
+
});
|
|
170
|
+
|
|
131
171
|
it("sends the chosen quality to a preview that has just loaded", () => {
|
|
132
172
|
usePlayerStore.getState().setPreviewQuality(0.25);
|
|
133
173
|
const postMessage = vi.fn();
|
|
@@ -13,6 +13,10 @@ import { usePlayerStore, type TimelineElement } from "../store/playerStore";
|
|
|
13
13
|
import type { ClipManifestClip, IframeWindow, PlaybackAdapter } from "../lib/playbackTypes";
|
|
14
14
|
import { hasTimelinePerformanceFixtureLease } from "../lib/timelinePerformanceFixture";
|
|
15
15
|
import { acceptStudioRuntimeMessage, postRuntimeControlMessage } from "../lib/runtimeProtocol";
|
|
16
|
+
import {
|
|
17
|
+
parseMediaLoadFailure,
|
|
18
|
+
RUNTIME_MEDIA_LOAD_FAILED,
|
|
19
|
+
} from "@hyperframes/core/runtime/protocol";
|
|
16
20
|
import {
|
|
17
21
|
parsePlaybackHealth,
|
|
18
22
|
parsePreviewCopies,
|
|
@@ -38,6 +42,8 @@ interface PreviewMessage {
|
|
|
38
42
|
copies?: unknown;
|
|
39
43
|
health?: unknown;
|
|
40
44
|
facts?: unknown;
|
|
45
|
+
code?: unknown;
|
|
46
|
+
details?: unknown;
|
|
41
47
|
}
|
|
42
48
|
|
|
43
49
|
export interface PreviewMessageRouterDeps {
|
|
@@ -136,6 +142,12 @@ function applyStoreReport(data: PreviewMessage): boolean {
|
|
|
136
142
|
case "preview-facts":
|
|
137
143
|
store.setPreviewFacts(parsePreviewFacts(data.facts));
|
|
138
144
|
return true;
|
|
145
|
+
case "diagnostic": {
|
|
146
|
+
if (data.code !== RUNTIME_MEDIA_LOAD_FAILED) return false;
|
|
147
|
+
const failure = parseMediaLoadFailure(data.details);
|
|
148
|
+
if (failure) store.setMediaLoadFailure(failure);
|
|
149
|
+
return true;
|
|
150
|
+
}
|
|
139
151
|
default:
|
|
140
152
|
return false;
|
|
141
153
|
}
|
|
@@ -498,6 +498,112 @@ describe("mediaUrlSourceOptions", () => {
|
|
|
498
498
|
expect(repeated).toEqual([0.5, 1, null]);
|
|
499
499
|
});
|
|
500
500
|
|
|
501
|
+
it("forgets a start's failures once a read from it ends cleanly, so a long-lived reader meeting it again retries", async () => {
|
|
502
|
+
const headers = { "Content-Range": "bytes 5000-6023/1000000" };
|
|
503
|
+
vi.stubGlobal(
|
|
504
|
+
"fetch",
|
|
505
|
+
vi
|
|
506
|
+
.fn()
|
|
507
|
+
.mockRejectedValueOnce(new TypeError("Failed to fetch"))
|
|
508
|
+
.mockRejectedValueOnce(new TypeError("Failed to fetch"))
|
|
509
|
+
.mockResolvedValueOnce(new Response(new Uint8Array(1024), { status: 206, headers }))
|
|
510
|
+
.mockRejectedValue(new TypeError("Failed to fetch")),
|
|
511
|
+
);
|
|
512
|
+
const { fetchFn, getRetryDelay } = mediaUrlSourceOptions("thumbnail");
|
|
513
|
+
const read5000 = () => fetchFn(URL_WITH_PATH, { headers: { Range: "bytes=5000-" } });
|
|
514
|
+
const failAt5000 = async () => getRetryDelay(1, await read5000().catch((e: unknown) => e));
|
|
515
|
+
const early = [await failAt5000(), await failAt5000()];
|
|
516
|
+
await (await read5000()).arrayBuffer();
|
|
517
|
+
const later = [await failAt5000(), await failAt5000(), await failAt5000()];
|
|
518
|
+
|
|
519
|
+
expect(early).toEqual([0.5, 1]);
|
|
520
|
+
expect(later).toEqual([0.5, 1, null]);
|
|
521
|
+
});
|
|
522
|
+
|
|
523
|
+
it("forgets a start's failures when mediabunny stops a read from it at its target", async () => {
|
|
524
|
+
const headers = { "Content-Range": "bytes 5000-999999/1000000" };
|
|
525
|
+
const endless = () =>
|
|
526
|
+
new ReadableStream<Uint8Array>({ pull: (c) => c.enqueue(new Uint8Array(4096)) });
|
|
527
|
+
vi.stubGlobal(
|
|
528
|
+
"fetch",
|
|
529
|
+
vi
|
|
530
|
+
.fn()
|
|
531
|
+
.mockRejectedValueOnce(new TypeError("Failed to fetch"))
|
|
532
|
+
.mockRejectedValueOnce(new TypeError("Failed to fetch"))
|
|
533
|
+
.mockResolvedValueOnce(new Response(endless(), { status: 206, headers }))
|
|
534
|
+
.mockRejectedValue(new TypeError("Failed to fetch")),
|
|
535
|
+
);
|
|
536
|
+
const { fetchFn, getRetryDelay } = mediaUrlSourceOptions("thumbnail");
|
|
537
|
+
const read5000 = (signal?: AbortSignal) =>
|
|
538
|
+
fetchFn(URL_WITH_PATH, { headers: { Range: "bytes=5000-" }, signal });
|
|
539
|
+
const failAt5000 = async () => getRetryDelay(1, await read5000().catch((e: unknown) => e));
|
|
540
|
+
const early = [await failAt5000(), await failAt5000()];
|
|
541
|
+
// what mediabunny's _runWorker does once a read reaches its target: abort the signal it passed
|
|
542
|
+
const target = new AbortController();
|
|
543
|
+
const reader = (await read5000(target.signal)).body!.getReader();
|
|
544
|
+
await reader.read();
|
|
545
|
+
target.abort();
|
|
546
|
+
await reader.read().catch(() => {});
|
|
547
|
+
const later = [await failAt5000(), await failAt5000(), await failAt5000()];
|
|
548
|
+
|
|
549
|
+
expect(early).toEqual([0.5, 1]);
|
|
550
|
+
expect(later).toEqual([0.5, 1, null]);
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
it("keeps a start's failures when its read broke before mediabunny stopped it", async () => {
|
|
554
|
+
const headers = { "Content-Range": "bytes 5000-999999/1000000" };
|
|
555
|
+
let sent = false;
|
|
556
|
+
const breaking = new ReadableStream<Uint8Array>({
|
|
557
|
+
pull(controller) {
|
|
558
|
+
if (sent) return controller.error(new TypeError("network error"));
|
|
559
|
+
sent = true;
|
|
560
|
+
controller.enqueue(new Uint8Array(4096));
|
|
561
|
+
},
|
|
562
|
+
});
|
|
563
|
+
vi.stubGlobal(
|
|
564
|
+
"fetch",
|
|
565
|
+
vi
|
|
566
|
+
.fn()
|
|
567
|
+
.mockResolvedValueOnce(new Response(breaking, { status: 206, headers }))
|
|
568
|
+
.mockRejectedValue(new TypeError("Failed to fetch")),
|
|
569
|
+
);
|
|
570
|
+
const { fetchFn, getRetryDelay } = mediaUrlSourceOptions("thumbnail");
|
|
571
|
+
const target = new AbortController();
|
|
572
|
+
const reader = (
|
|
573
|
+
await fetchFn(URL_WITH_PATH, { headers: { Range: "bytes=5000-" }, signal: target.signal })
|
|
574
|
+
).body!.getReader();
|
|
575
|
+
await reader.read();
|
|
576
|
+
const broken = await reader.read().catch((e: unknown) => e);
|
|
577
|
+
const delays = [getRetryDelay(1, broken)];
|
|
578
|
+
target.abort();
|
|
579
|
+
for (let i = 0; i < 2; i++) {
|
|
580
|
+
const failure = await fetchFn(URL_WITH_PATH, { headers: { Range: "bytes=5000-" } }).catch(
|
|
581
|
+
(e: unknown) => e,
|
|
582
|
+
);
|
|
583
|
+
delays.push(getRetryDelay(1, failure));
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
expect(delays).toEqual([0.5, 1, null]);
|
|
587
|
+
});
|
|
588
|
+
|
|
589
|
+
it("keeps counting a start's failures however far apart they come, while no read from it ends", async () => {
|
|
590
|
+
vi.useFakeTimers({ now: Date.UTC(2026, 9, 28, 9, 0, 0) });
|
|
591
|
+
vi.stubGlobal("fetch", vi.fn().mockRejectedValue(new TypeError("Failed to fetch")));
|
|
592
|
+
const { fetchFn, getRetryDelay } = mediaUrlSourceOptions("thumbnail");
|
|
593
|
+
const delays = [];
|
|
594
|
+
for (let i = 0; i < 3; i++) {
|
|
595
|
+
const failure = await fetchFn(URL_WITH_PATH, { headers: { Range: "bytes=5000-" } }).catch(
|
|
596
|
+
(e: unknown) => e,
|
|
597
|
+
);
|
|
598
|
+
delays.push(getRetryDelay(1, failure));
|
|
599
|
+
// a slow server streaming for minutes before it breaks
|
|
600
|
+
vi.setSystemTime(Date.now() + 20 * 60_000);
|
|
601
|
+
}
|
|
602
|
+
vi.useRealTimers();
|
|
603
|
+
|
|
604
|
+
expect(delays).toEqual([0.5, 1, null]);
|
|
605
|
+
});
|
|
606
|
+
|
|
501
607
|
it("does not retry a client error status", () => {
|
|
502
608
|
const failure = new MediaFetchError("probe", 404, null);
|
|
503
609
|
|
|
@@ -71,7 +71,9 @@ const typedFailure = (reader: MediaReader, error: unknown, start: number) =>
|
|
|
71
71
|
* A stalled body ignores every signal, so a silent one is aborted. The wait for the answer is not
|
|
72
72
|
* timed: it includes Chrome's queue for a connection, which streaming previews can hold for long.
|
|
73
73
|
*/
|
|
74
|
-
|
|
74
|
+
type CleanEnd = { ended: (start: number) => void; atTarget: AbortSignal | null | undefined };
|
|
75
|
+
|
|
76
|
+
function watchedRequest(reader: MediaReader, parents: readonly AbortSignal[], end?: CleanEnd) {
|
|
75
77
|
const controller = new AbortController();
|
|
76
78
|
let stalled = false;
|
|
77
79
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
@@ -102,7 +104,7 @@ function watchedRequest(reader: MediaReader, parents: readonly AbortSignal[]) {
|
|
|
102
104
|
stalled
|
|
103
105
|
? new MediaFetchError(reader, null, "stalled", start)
|
|
104
106
|
: typedFailure(reader, error, start);
|
|
105
|
-
return { signal: controller.signal, wait, done, failure };
|
|
107
|
+
return { signal: controller.signal, wait, done, failure, end };
|
|
106
108
|
}
|
|
107
109
|
|
|
108
110
|
type WatchedRequest = ReturnType<typeof watchedRequest>;
|
|
@@ -114,12 +116,15 @@ function typedBody(
|
|
|
114
116
|
request: WatchedRequest,
|
|
115
117
|
): ReadableStream<Uint8Array> {
|
|
116
118
|
const source = body.getReader();
|
|
119
|
+
const stopped = () => request.end?.atTarget?.aborted && request.end.ended(start);
|
|
120
|
+
request.signal.addEventListener("abort", stopped, { once: true });
|
|
117
121
|
return new ReadableStream<Uint8Array>({
|
|
118
122
|
async pull(controller) {
|
|
119
123
|
try {
|
|
120
124
|
const { done, value } = await request.wait(source.read());
|
|
121
125
|
if (!done) return controller.enqueue(value);
|
|
122
126
|
request.done();
|
|
127
|
+
request.end?.ended(start);
|
|
123
128
|
controller.close();
|
|
124
129
|
} catch (error) {
|
|
125
130
|
request.done();
|
|
@@ -172,15 +177,20 @@ export function mediaUrlSourceOptions(reader: MediaReader) {
|
|
|
172
177
|
const failuresByStart = new Map<number | null, number>();
|
|
173
178
|
const inFlightReads = new AbortController();
|
|
174
179
|
let ranges: "unknown" | "readable" | "hidden" = reader === "thumbnail" ? "unknown" : "hidden";
|
|
180
|
+
let etag: string | undefined;
|
|
181
|
+
const noteEtag = (response: Response) =>
|
|
182
|
+
void (etag ??= response.headers.get("ETag") ?? undefined);
|
|
175
183
|
const fetchFn = async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
|
|
176
184
|
const sent = ranges === "readable" ? boundedRange(init) : init;
|
|
177
185
|
const asked = Number(rangeStart(sent, true) ?? 0);
|
|
178
186
|
const request = watchedRequest(
|
|
179
187
|
reader,
|
|
180
188
|
init?.signal ? [inFlightReads.signal, init.signal] : [inFlightReads.signal],
|
|
189
|
+
{ ended: (start) => failuresByStart.delete(start), atTarget: init?.signal },
|
|
181
190
|
);
|
|
182
191
|
const response = await typedFetch(reader, input, sent ?? {}, asked, request);
|
|
183
192
|
if (ranges !== "hidden") ranges = rangesReadable(sent, response) ? "readable" : "hidden";
|
|
193
|
+
noteEtag(response);
|
|
184
194
|
return typedResponse(response, asked, request);
|
|
185
195
|
};
|
|
186
196
|
const getRetryDelay = (_previousAttempts: number, error: unknown): number | null => {
|
|
@@ -195,8 +205,9 @@ export function mediaUrlSourceOptions(reader: MediaReader) {
|
|
|
195
205
|
};
|
|
196
206
|
const cancelReads = () =>
|
|
197
207
|
inFlightReads.abort(new DOMException("Media reads cancelled", "AbortError"));
|
|
198
|
-
|
|
199
|
-
return { fetchFn, getRetryDelay, cancelReads,
|
|
208
|
+
const etagOf = () => etag;
|
|
209
|
+
if (reader === "probe") return { fetchFn, getRetryDelay, cancelReads, etagOf };
|
|
210
|
+
return { fetchFn, getRetryDelay, cancelReads, etagOf, maxCacheSize: POOLED_READER_CACHE_BYTES };
|
|
200
211
|
}
|
|
201
212
|
|
|
202
213
|
export function reportMediaFetchFailure(error: unknown): void {
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
|
|
3
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
4
|
+
import {
|
|
5
|
+
readStoredFrame,
|
|
6
|
+
readStoredInfo,
|
|
7
|
+
rememberVersion,
|
|
8
|
+
resetStoredThumbnailsForTests,
|
|
9
|
+
STORED_THUMBNAIL_BYTES,
|
|
10
|
+
storedVersion,
|
|
11
|
+
storeFrame,
|
|
12
|
+
storeInfo,
|
|
13
|
+
} from "./thumbnailDiskCache";
|
|
14
|
+
|
|
15
|
+
/** Cache Storage as a Map of Maps: what a browser keeps across sessions. */
|
|
16
|
+
function fakeCacheStorage() {
|
|
17
|
+
const stores = new Map<string, Map<string, Response>>();
|
|
18
|
+
const storage = {
|
|
19
|
+
open: async (name: string) => {
|
|
20
|
+
const store = stores.get(name) ?? new Map<string, Response>();
|
|
21
|
+
stores.set(name, store);
|
|
22
|
+
return {
|
|
23
|
+
match: async (url: string) => store.get(String(url))?.clone(),
|
|
24
|
+
put: async (url: string, response: Response) => void store.set(String(url), response),
|
|
25
|
+
};
|
|
26
|
+
},
|
|
27
|
+
has: async (name: string) => stores.has(name),
|
|
28
|
+
keys: async () => [...stores.keys()],
|
|
29
|
+
delete: async (name: string) => stores.delete(name),
|
|
30
|
+
};
|
|
31
|
+
return { storage, stores };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const v1 = { source: "/media/take.mp4", etag: '"one"' };
|
|
35
|
+
const v2 = { source: "/media/take.mp4", etag: '"two"' };
|
|
36
|
+
// a picture that reports `bytes` without holding them, so the budget test needs no real 128 MB
|
|
37
|
+
function jpeg(bytes = 5): Blob {
|
|
38
|
+
const blob = new Blob([new Uint8Array(Math.min(bytes, 5))], { type: "image/jpeg" });
|
|
39
|
+
return Object.defineProperty(blob, "size", { value: bytes });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let stores: Map<string, Map<string, Response>>;
|
|
43
|
+
|
|
44
|
+
beforeEach(() => {
|
|
45
|
+
resetStoredThumbnailsForTests();
|
|
46
|
+
localStorage.clear();
|
|
47
|
+
const fake = fakeCacheStorage();
|
|
48
|
+
stores = fake.stores;
|
|
49
|
+
vi.stubGlobal("caches", fake.storage);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
afterEach(() => {
|
|
53
|
+
vi.unstubAllGlobals();
|
|
54
|
+
vi.restoreAllMocks();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe("stored thumbnails", () => {
|
|
58
|
+
it("keep a source's facts and each frame with the time it was decoded at", async () => {
|
|
59
|
+
await storeInfo(v1, { aspect: 16 / 9, metadataDuration: 30 });
|
|
60
|
+
await storeFrame(v1, "cover 320x180 2.5", 2.4, jpeg());
|
|
61
|
+
|
|
62
|
+
expect(await readStoredInfo(v1)).toEqual({ aspect: 16 / 9, metadataDuration: 30 });
|
|
63
|
+
const frame = await readStoredFrame(v1, "cover 320x180 2.5");
|
|
64
|
+
expect(frame?.decodedAt).toBe(2.4);
|
|
65
|
+
expect(frame?.blob.size).toBe(5);
|
|
66
|
+
expect(await readStoredFrame(v1, "cover 320x180 5")).toBeUndefined();
|
|
67
|
+
expect(await readStoredFrame(v2, "cover 320x180 2.5")).toBeUndefined();
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("are trusted in a new session only when the server answers the stored ETag with a 304", async () => {
|
|
71
|
+
const fetch = vi.fn(async () => new Response(null, { status: 304 }));
|
|
72
|
+
vi.stubGlobal("fetch", fetch);
|
|
73
|
+
expect(await storedVersion(v1.source)).toBeUndefined();
|
|
74
|
+
expect(fetch).not.toHaveBeenCalled();
|
|
75
|
+
|
|
76
|
+
await rememberVersion(v1);
|
|
77
|
+
expect(await storedVersion(v1.source)).toEqual(v1);
|
|
78
|
+
expect(fetch).toHaveBeenCalledWith(v1.source, {
|
|
79
|
+
headers: { Range: "bytes=0-0", "If-None-Match": '"one"' },
|
|
80
|
+
});
|
|
81
|
+
fetch.mockResolvedValueOnce(new Response("x", { status: 206 }));
|
|
82
|
+
expect(await storedVersion(v1.source)).toBeUndefined();
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("drop what was stored for a file's earlier version once a new one is read", async () => {
|
|
86
|
+
await rememberVersion(v1);
|
|
87
|
+
await storeFrame(v1, "cover 320x180 1", 1, jpeg());
|
|
88
|
+
await rememberVersion(v2);
|
|
89
|
+
|
|
90
|
+
expect(await readStoredFrame(v1, "cover 320x180 1")).toBeUndefined();
|
|
91
|
+
expect(stores.size).toBe(0);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("drop the least recently used sources once they pass the budget", async () => {
|
|
95
|
+
const big = Math.floor(STORED_THUMBNAIL_BYTES / 2) + 1;
|
|
96
|
+
const others = ["/a.mp4", "/b.mp4", "/c.mp4"].map((source) => ({ source, etag: '"1"' }));
|
|
97
|
+
for (const version of others) {
|
|
98
|
+
await storeFrame(version, "cover 1x1 0", 0, jpeg(big));
|
|
99
|
+
vi.spyOn(Date, "now").mockReturnValue(Date.now() + 1000);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
expect(await readStoredFrame(others[0]!, "cover 1x1 0")).toBeUndefined();
|
|
103
|
+
expect(await readStoredFrame(others[2]!, "cover 1x1 0")).toBeDefined();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("never fail a thumbnail: a broken store warns once and reads as empty", async () => {
|
|
107
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
108
|
+
vi.stubGlobal("caches", {
|
|
109
|
+
open: async () => {
|
|
110
|
+
throw new DOMException("quota", "QuotaExceededError");
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
expect(await readStoredInfo(v1)).toBeUndefined();
|
|
115
|
+
expect(await storeFrame(v1, "cover 1x1 0", 0, jpeg())).toBeUndefined();
|
|
116
|
+
expect(warn).toHaveBeenCalledTimes(1);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("are skipped where the browser offers no Cache Storage", async () => {
|
|
120
|
+
vi.stubGlobal("caches", undefined);
|
|
121
|
+
expect(await readStoredInfo(v1)).toBeUndefined();
|
|
122
|
+
expect(await storeInfo(v1, { aspect: 1, metadataDuration: 1 })).toBeUndefined();
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("count a frame stored again under its name once, so the budget holds", async () => {
|
|
126
|
+
await storeFrame(v1, "cover 1x1 0", 0, jpeg(100));
|
|
127
|
+
await storeFrame(v1, "cover 1x1 0", 0, jpeg(100));
|
|
128
|
+
|
|
129
|
+
await vi.waitFor(() => expect(localStorage.getItem("hf-thumbnails-index")).toContain("bytes"), {
|
|
130
|
+
timeout: 3000,
|
|
131
|
+
});
|
|
132
|
+
const saved = JSON.parse(localStorage.getItem("hf-thumbnails-index")!);
|
|
133
|
+
expect(Object.values(saved.caches)).toMatchObject([{ bytes: 100 }]);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("keep what another tab stored when saving, but not what this tab dropped", async () => {
|
|
137
|
+
await rememberVersion(v1);
|
|
138
|
+
await storeFrame(v1, "cover 1x1 0", 0, jpeg());
|
|
139
|
+
await vi.waitFor(() => expect(localStorage.getItem("hf-thumbnails-index")).toContain("bytes"), {
|
|
140
|
+
timeout: 3000,
|
|
141
|
+
});
|
|
142
|
+
// another tab saves its snapshot: a source of its own, and v1 as it was before this tab moved on
|
|
143
|
+
const other = JSON.parse(localStorage.getItem("hf-thumbnails-index")!);
|
|
144
|
+
other.caches["hf-thumbnails:other:tab"] = { usedAt: Date.now(), bytes: 7 };
|
|
145
|
+
localStorage.setItem("hf-thumbnails-index", JSON.stringify(other));
|
|
146
|
+
const v1Cache = Object.keys(other.caches).find((name) => name !== "hf-thumbnails:other:tab")!;
|
|
147
|
+
await rememberVersion(v2);
|
|
148
|
+
await storeFrame(v2, "cover 1x1 0", 0, jpeg());
|
|
149
|
+
|
|
150
|
+
await vi.waitFor(
|
|
151
|
+
() => expect(localStorage.getItem("hf-thumbnails-index")).not.toContain(v1Cache),
|
|
152
|
+
{ timeout: 3000 },
|
|
153
|
+
);
|
|
154
|
+
const saved = JSON.parse(localStorage.getItem("hf-thumbnails-index")!);
|
|
155
|
+
expect(Object.keys(saved.caches)).toContain("hf-thumbnails:other:tab");
|
|
156
|
+
expect(Object.keys(saved.caches)).toHaveLength(2);
|
|
157
|
+
});
|
|
158
|
+
});
|