forked from heygen-com/hyperframes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.adapter.ts
More file actions
444 lines (410 loc) · 15.4 KB
/
Copy pathvite.adapter.ts
File metadata and controls
444 lines (410 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// Vite adapter that wires the shared Studio API to the local filesystem and build tools.
import {
readFileSync,
readdirSync,
existsSync,
writeFileSync,
realpathSync,
mkdirSync,
copyFileSync,
unlinkSync,
} from "node:fs";
import { join, relative, resolve, isAbsolute, dirname } from "node:path";
import type { ViteDevServer } from "vite";
import {
type ResolvedProject,
type RenderJobState,
type StudioApiAdapter,
type BackgroundRemovalRender,
createBackgroundRemovalJob,
createProjectSignature,
affectsProjectSignature,
} from "@hyperframes/studio-server";
import type { RegistryItem } from "@hyperframes/core/registry";
import { createRetryingModuleLoader, ensureProducerDist } from "./vite.producer";
import { createStudioDevRenderBodyScripts } from "./vite.studioMotion";
import { generateThumbnail, findSystemChrome } from "./vite.browser";
function isPathWithin(parentDir: string, childPath: string): boolean {
const childRelativePath = relative(resolve(parentDir), resolve(childPath));
return (
childRelativePath === "" ||
(!childRelativePath.startsWith("..") && !isAbsolute(childRelativePath))
);
}
export function resolveViteAutoProxy(value: string | undefined): boolean {
return value !== "false";
}
/**
* The preview ETag's cache, and the one thing allowed to clear it.
*
* The signature walks the whole project directory, so it is memoised per project
* directory. (The content hash underneath is already gated behind a stat-only
* fingerprint, so what this memo saves is the walk, not the hashing — worth
* knowing before deciding how aggressive invalidation is allowed to be.)
* Getting the invalidation wrong is not a
* performance bug: the preview answers a revalidation with 304 and the browser
* keeps serving the pre-edit composition, which is how a thumbnail regenerated
* after an edit can still show the old frame.
*
* `watch` is called the first time a project dir is seen, so whoever owns the
* watcher can start following it. It must be a watcher that actually sees
* project writes: Vite's own is configured to ignore them.
*/
export interface ProjectSignatureCache {
get(projectDir: string): string;
/** Drop the signature of whichever project contains `changedPath`. */
invalidate(changedPath: string): void;
}
export function createProjectSignatureCache({
compute = createProjectSignature,
watch,
}: {
compute?: (projectDir: string) => string;
watch?: (projectDir: string) => void;
} = {}): ProjectSignatureCache {
const signatures = new Map<string, string>();
const watched = new Set<string>();
return {
get(projectDir) {
const key = resolve(projectDir);
const cached = signatures.get(key);
if (cached !== undefined) return cached;
if (!watched.has(key)) {
watched.add(key);
watch?.(key);
}
const signature = compute(key);
signatures.set(key, signature);
return signature;
},
invalidate(changedPath) {
// Filtered here rather than at the watcher so no caller can wire up a
// subscription that forgets to: the cache owns what can change its value.
for (const projectDir of signatures.keys()) {
if (affectsProjectSignature(projectDir, changedPath)) signatures.delete(projectDir);
}
},
};
}
export function createViteAdapter(
dataDir: string,
server: ViteDevServer,
signatureCache: ProjectSignatureCache,
): StudioApiAdapter {
let _bundler:
| ((
dir: string,
options?: { runtime?: "inline" | "placeholder"; inlineColorGradingLuts?: boolean },
) => Promise<string>)
| null = null;
let _producerModuleLoader:
| (() => Promise<{
createRenderJob: (config: {
fps: 24 | 30 | 60;
quality: "draft" | "standard" | "high";
format: string;
renderBodyScripts?: string[];
outputResolution?: "landscape" | "portrait" | "landscape-4k" | "portrait-4k";
variables?: Record<string, unknown>;
}) => unknown;
executeRenderJob: (
job: unknown,
projectDir: string,
outputPath: string,
onProgress?: (job: { progress: number; currentStage?: string }) => void,
) => Promise<void>;
}>)
| null = null;
const getBundler = async () => {
if (!_bundler) {
try {
const mod = await server.ssrLoadModule("@hyperframes/core/compiler");
_bundler = (dir, options) => mod.bundleToSingleHtml(dir, options);
} catch (err) {
console.warn("[Studio] Failed to load compiler, previews will use raw HTML:", err);
_bundler = null as never;
}
}
return _bundler;
};
const getProducerModule = async () => {
if (!_producerModuleLoader) {
_producerModuleLoader = createRetryingModuleLoader(async () => {
const { built } = ensureProducerDist({
studioDir: __dirname,
env: process.env,
});
if (built) {
console.warn(
"[Studio] @hyperframes/producer dist missing; building producer package for local renders...",
);
}
const producerPkg = "@hyperframes/producer";
return await import(/* @vite-ignore */ producerPkg);
});
}
return _producerModuleLoader();
};
return {
// The CLI resolves --proxy/--no-proxy against hyperframes.json before it
// launches Vite. Direct `bun run dev` keeps the historical default-on
// behavior when the child environment is absent.
autoProxy: resolveViteAutoProxy(process.env.HYPERFRAMES_AUTO_PROXY),
// fallow-ignore-next-line complexity
listProjects() {
if (!existsSync(dataDir)) return [];
const sessionsDir = resolve(dataDir, "../sessions");
const sessionMap = new Map<string, { sessionId: string; title: string }>();
if (existsSync(sessionsDir)) {
for (const file of readdirSync(sessionsDir).filter((f) => f.endsWith(".json"))) {
try {
const raw = JSON.parse(readFileSync(join(sessionsDir, file), "utf-8"));
if (raw.projectId) {
sessionMap.set(raw.projectId, {
sessionId: file.replace(".json", ""),
title: raw.title || "Untitled",
});
}
} catch {
/* skip corrupt */
}
}
}
return readdirSync(dataDir, { withFileTypes: true })
.filter(
(d) =>
(d.isDirectory() || d.isSymbolicLink()) &&
(existsSync(join(dataDir, d.name, "index.html")) ||
existsSync(join(dataDir, d.name, `${d.name}.html`))),
)
.map((d) => {
const session = sessionMap.get(d.name);
return {
id: d.name,
dir: join(dataDir, d.name),
title: session?.title ?? d.name,
sessionId: session?.sessionId,
} satisfies ResolvedProject;
})
.sort((a, b) => (a.title ?? "").localeCompare(b.title ?? ""));
},
// fallow-ignore-next-line complexity
resolveProject(id: string) {
let projectDir = join(dataDir, id);
if (!existsSync(projectDir)) {
const sessionsDir = resolve(dataDir, "../sessions");
const sessionFile = join(sessionsDir, `${id}.json`);
if (existsSync(sessionFile)) {
try {
const session = JSON.parse(readFileSync(sessionFile, "utf-8"));
if (session.projectId) {
projectDir = join(dataDir, session.projectId);
if (existsSync(projectDir)) {
return {
id: session.projectId,
dir: realpathSync(projectDir),
title: session.title,
};
}
}
} catch {
/* ignore */
}
}
return null;
}
return { id, dir: realpathSync(projectDir) };
},
async bundle(dir: string) {
const bundler = await getBundler();
if (!bundler) return null;
let html = await bundler(dir, { runtime: "placeholder", inlineColorGradingLuts: false });
html = html.replace(
'data-hyperframes-preview-runtime="1" src=""',
`data-hyperframes-preview-runtime="1" src="${this.runtimeUrl}"`,
);
return html;
},
async transformPreviewHtml({ html }) {
const producer = await import("../producer/src/services/deterministicFonts.js");
return producer.injectDeterministicFontFaces(html);
},
getProjectSignature(projectDir: string): string {
return signatureCache.get(projectDir);
},
async lint(html: string, opts?: { filePath?: string }) {
const mod = await server.ssrLoadModule("@hyperframes/core/lint");
return await mod.lintHyperframeHtml(html, opts);
},
async lintProject(projectDir: string) {
const mod = await server.ssrLoadModule("@hyperframes/core/lint");
return await mod.lintProject(projectDir);
},
runtimeUrl: "/api/runtime.js",
rendersDir: () => resolve(dataDir, "../renders"),
startRender(opts): RenderJobState {
const abortController = new AbortController();
const state: RenderJobState = {
id: opts.jobId,
status: "rendering",
progress: 0,
outputPath: opts.outputPath,
cancel: () => abortController.abort(),
};
const startTime = Date.now();
const removeCancelledOutput = () => {
// User-initiated cancel: not a failure. Remove any output so the
// cancelled job doesn't resurrect in the render history.
state.status = "cancelled";
for (const fp of [
opts.outputPath,
opts.outputPath.replace(/\.(mp4|webm|mov)$/, ".meta.json"),
]) {
try {
if (existsSync(fp)) unlinkSync(fp);
} catch {
/* ignore */
}
}
};
// fallow-ignore-next-line complexity
(async () => {
try {
if (!process.env.PRODUCER_HEADLESS_SHELL_PATH) {
const systemChrome = findSystemChrome();
if (systemChrome) process.env.PRODUCER_HEADLESS_SHELL_PATH = systemChrome;
}
const { createRenderJob, executeRenderJob } = await getProducerModule();
const renderBodyScripts = createStudioDevRenderBodyScripts(opts.project.dir);
const job = createRenderJob({
fps: opts.fps,
quality: opts.quality as "draft" | "standard" | "high",
format: opts.format,
...(renderBodyScripts.length > 0 ? { renderBodyScripts } : {}),
outputResolution: opts.outputResolution,
...(opts.composition ? { entryFile: opts.composition } : {}),
...(opts.variables ? { variables: opts.variables } : {}),
});
const onProgress = (j: { progress: number; currentStage?: string }) => {
state.progress = j.progress;
if (j.currentStage) state.stage = j.currentStage;
};
await executeRenderJob(
job,
opts.project.dir,
opts.outputPath,
onProgress,
abortController.signal,
);
if (abortController.signal.aborted) {
// Cancel landed just as the render finished: honor the cancel the
// route already reported instead of resurrecting a completed job.
removeCancelledOutput();
return;
}
state.status = "complete";
state.progress = 100;
const metaPath = opts.outputPath.replace(/\.(mp4|webm|mov)$/, ".meta.json");
writeFileSync(
metaPath,
JSON.stringify({ status: "complete", durationMs: Date.now() - startTime }),
);
} catch (err) {
if (abortController.signal.aborted) {
removeCancelledOutput();
return;
}
state.status = "failed";
state.error = err instanceof Error ? err.message : String(err);
try {
const metaPath = opts.outputPath.replace(/\.(mp4|webm|mov)$/, ".meta.json");
writeFileSync(metaPath, JSON.stringify({ status: "failed" }));
} catch {
/* ignore */
}
}
})();
return state;
},
startBackgroundRemoval(opts) {
return createBackgroundRemovalJob(opts, async (renderOpts) => {
const mod = await server.ssrLoadModule(
resolve(__dirname, "../cli/src/background-removal/pipeline.ts"),
);
const render = mod.render as BackgroundRemovalRender;
return render(renderOpts);
});
},
async generateThumbnail(opts) {
return generateThumbnail(opts);
},
async resolveSession(sessionId: string) {
const sessionsDir = resolve(dataDir, "../sessions");
const sessionFile = join(sessionsDir, `${sessionId}.json`);
if (!existsSync(sessionFile)) return null;
try {
const raw = JSON.parse(readFileSync(sessionFile, "utf-8"));
if (raw.projectId) return { projectId: raw.projectId, title: raw.title };
} catch {
/* ignore */
}
return null;
},
// fallow-ignore-next-line complexity
async listRegistryCatalog(): Promise<RegistryItem[]> {
const registryRoot = resolve(__dirname, "../../registry");
const items: RegistryItem[] = [];
for (const subdir of ["blocks", "components"]) {
const dir = join(registryRoot, subdir);
if (!existsSync(dir)) continue;
for (const entry of readdirSync(dir, { withFileTypes: true })) {
if (!entry.isDirectory()) continue;
const manifestPath = join(dir, entry.name, "registry-item.json");
if (!existsSync(manifestPath)) continue;
try {
const manifest = JSON.parse(readFileSync(manifestPath, "utf-8")) as RegistryItem;
if (manifest.type === "hyperframes:block" || manifest.type === "hyperframes:component")
items.push(manifest);
} catch {
/* skip malformed manifests */
}
}
}
return items;
},
// fallow-ignore-next-line complexity
async installRegistryBlock(opts: {
project: ResolvedProject;
blockName: string;
}): Promise<{ written: string[]; block: RegistryItem }> {
const registryRoot = resolve(__dirname, "../../registry");
let itemDir = join(registryRoot, "blocks", opts.blockName);
if (!existsSync(join(itemDir, "registry-item.json"))) {
itemDir = join(registryRoot, "components", opts.blockName);
}
const manifestPath = join(itemDir, "registry-item.json");
if (!existsSync(manifestPath)) {
throw new Error(`Item "${opts.blockName}" not found in registry`);
}
const block = JSON.parse(readFileSync(manifestPath, "utf-8")) as RegistryItem;
const written: string[] = [];
for (const file of block.files) {
const sourcePath = join(itemDir, file.path);
const targetPath = resolve(opts.project.dir, file.target);
if (!isPathWithin(opts.project.dir, targetPath)) {
throw new Error(`Target path escapes project directory: ${file.target}`);
}
mkdirSync(dirname(targetPath), { recursive: true });
if (file.type === "hyperframes:composition") {
let content = readFileSync(sourcePath, "utf-8");
content = `<!-- hyperframes-registry-item: ${block.name} -->\n${content}`;
writeFileSync(targetPath, content, "utf-8");
} else {
copyFileSync(sourcePath, targetPath);
}
written.push(file.target);
}
return { written, block };
},
};
}