@hyperframes/studio 0.8.212 → 0.8.214
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-DHfuW7Lt.js → hyperframes-player-H1DIJsJZ.js} +1 -1
- package/dist/assets/{index-CWgIsKzV.js → index-BftVE_0y.js} +119 -119
- package/dist/assets/{index-CkjUY33q.js → index-v_hWj6Kv.js} +1 -1
- 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 +1079 -846
- 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/player/hooks/previewMessageRouter.test.ts +40 -0
- package/src/player/hooks/previewMessageRouter.ts +12 -0
- package/src/player/lib/mediaFetch.ts +7 -2
- package/src/player/lib/thumbnailDiskCache.test.ts +185 -0
- package/src/player/lib/thumbnailDiskCache.ts +223 -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.214",
|
|
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/
|
|
64
|
-
"@hyperframes/
|
|
65
|
-
"@hyperframes/player": "0.8.
|
|
66
|
-
"@hyperframes/sdk": "0.8.
|
|
67
|
-
"@hyperframes/
|
|
68
|
-
"@hyperframes/
|
|
63
|
+
"@hyperframes/core": "0.8.214",
|
|
64
|
+
"@hyperframes/parsers": "0.8.214",
|
|
65
|
+
"@hyperframes/player": "0.8.214",
|
|
66
|
+
"@hyperframes/sdk": "0.8.214",
|
|
67
|
+
"@hyperframes/studio-server": "0.8.214",
|
|
68
|
+
"@hyperframes/shader-transitions": "0.8.214"
|
|
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.214"
|
|
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
|
}
|
|
@@ -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
|
}
|
|
@@ -177,6 +177,9 @@ export function mediaUrlSourceOptions(reader: MediaReader) {
|
|
|
177
177
|
const failuresByStart = new Map<number | null, number>();
|
|
178
178
|
const inFlightReads = new AbortController();
|
|
179
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);
|
|
180
183
|
const fetchFn = async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
|
|
181
184
|
const sent = ranges === "readable" ? boundedRange(init) : init;
|
|
182
185
|
const asked = Number(rangeStart(sent, true) ?? 0);
|
|
@@ -187,6 +190,7 @@ export function mediaUrlSourceOptions(reader: MediaReader) {
|
|
|
187
190
|
);
|
|
188
191
|
const response = await typedFetch(reader, input, sent ?? {}, asked, request);
|
|
189
192
|
if (ranges !== "hidden") ranges = rangesReadable(sent, response) ? "readable" : "hidden";
|
|
193
|
+
noteEtag(response);
|
|
190
194
|
return typedResponse(response, asked, request);
|
|
191
195
|
};
|
|
192
196
|
const getRetryDelay = (_previousAttempts: number, error: unknown): number | null => {
|
|
@@ -201,8 +205,9 @@ export function mediaUrlSourceOptions(reader: MediaReader) {
|
|
|
201
205
|
};
|
|
202
206
|
const cancelReads = () =>
|
|
203
207
|
inFlightReads.abort(new DOMException("Media reads cancelled", "AbortError"));
|
|
204
|
-
|
|
205
|
-
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 };
|
|
206
211
|
}
|
|
207
212
|
|
|
208
213
|
export function reportMediaFetchFailure(error: unknown): void {
|
|
@@ -0,0 +1,185 @@
|
|
|
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
|
+
expect(Object.values(saved.etags)).toEqual(['"two"']);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("keep a newer version another tab saw of a source this tab only read at start", async () => {
|
|
161
|
+
await rememberVersion(v1);
|
|
162
|
+
await storeFrame(v1, "cover 1x1 0", 0, jpeg());
|
|
163
|
+
await vi.waitFor(() => expect(localStorage.getItem("hf-thumbnails-index")).toContain("bytes"), {
|
|
164
|
+
timeout: 3000,
|
|
165
|
+
});
|
|
166
|
+
resetStoredThumbnailsForTests();
|
|
167
|
+
// a new session reads the index, then another tab moves the source on to v2
|
|
168
|
+
await storeFrame({ source: "/other.mp4", etag: '"1"' }, "cover 1x1 0", 0, jpeg());
|
|
169
|
+
const other = JSON.parse(localStorage.getItem("hf-thumbnails-index")!);
|
|
170
|
+
const key = Object.keys(other.etags).find((k) => other.etags[k] === '"one"')!;
|
|
171
|
+
other.etags[key] = '"two"';
|
|
172
|
+
localStorage.setItem("hf-thumbnails-index", JSON.stringify(other));
|
|
173
|
+
await storeFrame({ source: "/other.mp4", etag: '"1"' }, "cover 1x1 1", 1, jpeg());
|
|
174
|
+
|
|
175
|
+
// this tab's save is the one that adds /other.mp4's cache to the index
|
|
176
|
+
await vi.waitFor(
|
|
177
|
+
() =>
|
|
178
|
+
expect(
|
|
179
|
+
Object.keys(JSON.parse(localStorage.getItem("hf-thumbnails-index")!).caches),
|
|
180
|
+
).toHaveLength(2),
|
|
181
|
+
{ timeout: 3000 },
|
|
182
|
+
);
|
|
183
|
+
expect(JSON.parse(localStorage.getItem("hf-thumbnails-index")!).etags[key]).toBe('"two"');
|
|
184
|
+
});
|
|
185
|
+
});
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import type { SourceInfo } from "./thumbnailSourcePool";
|
|
2
|
+
|
|
3
|
+
/** A video as the server serves it now: its URL and the ETag of this version of its file. */
|
|
4
|
+
export interface StoredSource {
|
|
5
|
+
source: string;
|
|
6
|
+
etag: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
interface StoredFrame {
|
|
10
|
+
blob: Blob;
|
|
11
|
+
decodedAt: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const CACHE_PREFIX = "hf-thumbnails:";
|
|
15
|
+
const INDEX_KEY = "hf-thumbnails-index";
|
|
16
|
+
const ENTRY_ORIGIN = "https://hf-thumbnail.invalid/";
|
|
17
|
+
export const STORED_THUMBNAIL_BYTES = 256 * 1024 * 1024;
|
|
18
|
+
|
|
19
|
+
interface Index {
|
|
20
|
+
etags: Record<string, string>;
|
|
21
|
+
caches: Record<string, { usedAt: number; bytes: number }>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
let index: Index | undefined;
|
|
25
|
+
// caches this tab dropped since its last save, which another tab's older snapshot must not bring back
|
|
26
|
+
const dropped = new Set<string>();
|
|
27
|
+
const sourcesThisTabMovedOn = new Set<string>();
|
|
28
|
+
let saveTimer: ReturnType<typeof setTimeout> | undefined;
|
|
29
|
+
let warned = false;
|
|
30
|
+
|
|
31
|
+
const available = () => typeof caches !== "undefined" && typeof crypto?.subtle !== "undefined";
|
|
32
|
+
|
|
33
|
+
/** Disk is a shortcut, never a reason a thumbnail fails: a broken store says so once and is skipped. */
|
|
34
|
+
async function quietly<T>(work: () => Promise<T>): Promise<T | undefined> {
|
|
35
|
+
if (!available()) return undefined;
|
|
36
|
+
try {
|
|
37
|
+
return await work();
|
|
38
|
+
} catch (error) {
|
|
39
|
+
if (!warned) console.warn("[Studio] timeline thumbnails stay in memory:", error);
|
|
40
|
+
warned = true;
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async function digest(text: string): Promise<string> {
|
|
46
|
+
const bytes = new Uint8Array(
|
|
47
|
+
await crypto.subtle.digest("SHA-256", new TextEncoder().encode(text)),
|
|
48
|
+
);
|
|
49
|
+
return Array.from(bytes.subarray(0, 12), (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const cacheNameOf = async ({ source, etag }: StoredSource) =>
|
|
53
|
+
`${CACHE_PREFIX}${await digest(source)}:${await digest(etag)}`;
|
|
54
|
+
|
|
55
|
+
function savedIndex(): Index | undefined {
|
|
56
|
+
try {
|
|
57
|
+
const saved = JSON.parse(localStorage.getItem(INDEX_KEY) ?? "null") as Index | null;
|
|
58
|
+
return saved?.etags && saved.caches ? saved : undefined;
|
|
59
|
+
} catch {
|
|
60
|
+
return undefined; // expected: no storage (a private window) or a value this version can't read
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function loadIndex(): Index {
|
|
65
|
+
return (index ??= savedIndex() ?? { etags: {}, caches: {} });
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
type CacheEntry = Index["caches"][string];
|
|
69
|
+
|
|
70
|
+
/** Another tab's entry when it was used later than this tab's, with the larger count of bytes. */
|
|
71
|
+
function laterOf(own: CacheEntry | undefined, theirs: CacheEntry): CacheEntry | undefined {
|
|
72
|
+
if (own && own.usedAt >= theirs.usedAt) return undefined;
|
|
73
|
+
return { usedAt: theirs.usedAt, bytes: Math.max(theirs.bytes, own?.bytes ?? 0) };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function savedEtagsButThisTabsMoves(mine: Index["etags"], theirs: Index["etags"]): Index["etags"] {
|
|
77
|
+
const etags = { ...mine, ...theirs };
|
|
78
|
+
for (const key of sourcesThisTabMovedOn) etags[key] = mine[key]!;
|
|
79
|
+
sourcesThisTabMovedOn.clear();
|
|
80
|
+
return etags;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** This tab's index with what other tabs saved since: each cache at its latest use, none this tab dropped. */
|
|
84
|
+
function mergedIndex(): Index {
|
|
85
|
+
const mine = loadIndex();
|
|
86
|
+
const theirs = savedIndex() ?? { etags: {}, caches: {} };
|
|
87
|
+
for (const [name, entry] of Object.entries(theirs.caches)) {
|
|
88
|
+
const later = dropped.has(name) ? undefined : laterOf(mine.caches[name], entry);
|
|
89
|
+
if (later) mine.caches[name] = later;
|
|
90
|
+
}
|
|
91
|
+
mine.etags = savedEtagsButThisTabsMoves(mine.etags, theirs.etags);
|
|
92
|
+
dropped.clear();
|
|
93
|
+
return mine;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function saveIndexSoon(): void {
|
|
97
|
+
saveTimer ??= setTimeout(() => {
|
|
98
|
+
saveTimer = undefined;
|
|
99
|
+
try {
|
|
100
|
+
localStorage.setItem(INDEX_KEY, JSON.stringify(mergedIndex()));
|
|
101
|
+
} catch {
|
|
102
|
+
// expected: storage refused; the caches still hold the frames, only their order of use is lost
|
|
103
|
+
}
|
|
104
|
+
}, 1000);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function used(name: string, addedBytes = 0): void {
|
|
108
|
+
const caches = loadIndex().caches;
|
|
109
|
+
caches[name] = { usedAt: Date.now(), bytes: (caches[name]?.bytes ?? 0) + addedBytes };
|
|
110
|
+
saveIndexSoon();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** The version stored for `source`, when one conditional one-byte read says the server still has it (304). */
|
|
114
|
+
export const storedVersion = (source: string) =>
|
|
115
|
+
quietly(async (): Promise<StoredSource | undefined> => {
|
|
116
|
+
const etag = loadIndex().etags[await digest(source)];
|
|
117
|
+
if (!etag) return undefined;
|
|
118
|
+
// oxlint-disable-next-line no-restricted-globals -- one validation read, on the media's own route like mediabunny's
|
|
119
|
+
const response = await fetch(source, {
|
|
120
|
+
headers: { Range: "bytes=0-0", "If-None-Match": etag },
|
|
121
|
+
});
|
|
122
|
+
await response.body?.cancel();
|
|
123
|
+
return response.status === 304 ? { source, etag } : undefined;
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
/** Records the version the server now has, dropping what was stored for the source's earlier versions. */
|
|
127
|
+
export const rememberVersion = (stored: StoredSource) =>
|
|
128
|
+
quietly(async () => {
|
|
129
|
+
const key = await digest(stored.source);
|
|
130
|
+
const saved = loadIndex();
|
|
131
|
+
if (saved.etags[key] === stored.etag) return;
|
|
132
|
+
saved.etags[key] = stored.etag;
|
|
133
|
+
sourcesThisTabMovedOn.add(key);
|
|
134
|
+
saveIndexSoon();
|
|
135
|
+
const keep = await cacheNameOf(stored);
|
|
136
|
+
for (const name of await caches.keys()) {
|
|
137
|
+
if (!name.startsWith(`${CACHE_PREFIX}${key}:`) || name === keep) continue;
|
|
138
|
+
await caches.delete(name);
|
|
139
|
+
delete saved.caches[name];
|
|
140
|
+
dropped.add(name);
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
async function entry(stored: StoredSource, path: string) {
|
|
145
|
+
const name = await cacheNameOf(stored);
|
|
146
|
+
return { name, cache: await caches.open(name), url: `${ENTRY_ORIGIN}${path}` };
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** A read opens nothing: opening a cache creates it, and an empty one for a gone version would never be dropped. */
|
|
150
|
+
async function storedEntry(stored: StoredSource, path: string) {
|
|
151
|
+
const name = await cacheNameOf(stored);
|
|
152
|
+
return (await caches.has(name)) ? entry(stored, path) : undefined;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export const readStoredInfo = (stored: StoredSource) =>
|
|
156
|
+
quietly(async (): Promise<SourceInfo | undefined> => {
|
|
157
|
+
const found = await storedEntry(stored, "info");
|
|
158
|
+
if (!found) return undefined;
|
|
159
|
+
const { name, cache, url } = found;
|
|
160
|
+
const info = (await (await cache.match(url))?.json()) as SourceInfo | undefined;
|
|
161
|
+
if (!(info && info.aspect > 0)) return undefined;
|
|
162
|
+
used(name);
|
|
163
|
+
return info;
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
export const storeInfo = (stored: StoredSource, info: SourceInfo) =>
|
|
167
|
+
quietly(async () => {
|
|
168
|
+
const { name, cache, url } = await entry(stored, "info");
|
|
169
|
+
await cache.put(url, new Response(JSON.stringify(info)));
|
|
170
|
+
used(name);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
/** `frame` names one picture of the source: its fit, size and the time it was asked for. */
|
|
174
|
+
export const readStoredFrame = (stored: StoredSource, frame: string) =>
|
|
175
|
+
quietly(async (): Promise<StoredFrame | undefined> => {
|
|
176
|
+
const found = await storedEntry(stored, `frame?${encodeURIComponent(frame)}`);
|
|
177
|
+
if (!found) return undefined;
|
|
178
|
+
const { name, cache, url } = found;
|
|
179
|
+
const response = await cache.match(url);
|
|
180
|
+
const decodedAt = Number(response?.headers.get("x-decoded-at"));
|
|
181
|
+
if (!response || !Number.isFinite(decodedAt)) return undefined;
|
|
182
|
+
used(name);
|
|
183
|
+
return { blob: await response.blob(), decodedAt };
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
export const storeFrame = (stored: StoredSource, frame: string, decodedAt: number, blob: Blob) =>
|
|
187
|
+
quietly(async () => {
|
|
188
|
+
const { name, cache, url } = await entry(stored, `frame?${encodeURIComponent(frame)}`);
|
|
189
|
+
const replaced = Number((await cache.match(url))?.headers.get("x-bytes")) || 0;
|
|
190
|
+
const headers = {
|
|
191
|
+
"content-type": blob.type,
|
|
192
|
+
"x-decoded-at": String(decodedAt),
|
|
193
|
+
"x-bytes": String(blob.size),
|
|
194
|
+
};
|
|
195
|
+
await cache.put(url, new Response(blob, { headers }));
|
|
196
|
+
used(name, blob.size - replaced);
|
|
197
|
+
await dropLeastUsed(STORED_THUMBNAIL_BYTES);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
/** Whole sources go, least recently used first, until what is stored fits `budgetBytes`. */
|
|
201
|
+
async function dropLeastUsed(budgetBytes: number): Promise<void> {
|
|
202
|
+
const saved = loadIndex();
|
|
203
|
+
let total = Object.values(saved.caches).reduce((sum, { bytes }) => sum + bytes, 0);
|
|
204
|
+
const oldestFirst = Object.entries(saved.caches).sort(([, a], [, b]) => a.usedAt - b.usedAt);
|
|
205
|
+
for (const [name, { bytes }] of oldestFirst) {
|
|
206
|
+
if (total <= budgetBytes) break;
|
|
207
|
+
await caches.delete(name);
|
|
208
|
+
delete saved.caches[name];
|
|
209
|
+
dropped.add(name);
|
|
210
|
+
total -= bytes;
|
|
211
|
+
}
|
|
212
|
+
saveIndexSoon();
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** Tests only: forget the in-memory index and any pending save. */
|
|
216
|
+
export function resetStoredThumbnailsForTests(): void {
|
|
217
|
+
clearTimeout(saveTimer);
|
|
218
|
+
saveTimer = undefined;
|
|
219
|
+
index = undefined;
|
|
220
|
+
warned = false;
|
|
221
|
+
dropped.clear();
|
|
222
|
+
sourcesThisTabMovedOn.clear();
|
|
223
|
+
}
|
|
@@ -12,6 +12,7 @@ interface OpenedSource {
|
|
|
12
12
|
mediabunny: Mediabunny;
|
|
13
13
|
track: InputVideoTrack;
|
|
14
14
|
info: SourceInfo;
|
|
15
|
+
etag: string | undefined;
|
|
15
16
|
}
|
|
16
17
|
|
|
17
18
|
export interface ThumbnailSource extends OpenedSource {
|
|
@@ -88,7 +89,8 @@ async function open(source: string, pooled: () => PooledSource): Promise<OpenedS
|
|
|
88
89
|
}
|
|
89
90
|
const metadataDuration = await track.getDurationFromMetadata({ skipLiveWait: true });
|
|
90
91
|
live();
|
|
91
|
-
|
|
92
|
+
const info = { aspect: displayWidth / displayHeight, metadataDuration };
|
|
93
|
+
return { mediabunny, track, info, etag: options.etagOf() };
|
|
92
94
|
}
|
|
93
95
|
|
|
94
96
|
function pooledSource(source: string, version: string | undefined): PooledSource {
|