mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-16 14:27:26 +00:00
* refactor: extract @hyperframes/studio-server package from core Moves all studio-api routes, helpers, and Hono server wiring from packages/core/src/studio-api/ into a new standalone packages/studio-server package (@hyperframes/studio-server). Core keeps thin re-export stubs at @hyperframes/core/studio-api and the subpath helpers (screenshot-clip, draft-markers, etc.) for backward compatibility. Consumer imports (cli studioServer, vite adapter/config, producer htmlCompiler, studio manualEditsTypes) are updated to import from @hyperframes/studio-server directly. Also exports rewriteInlineStyleAssetUrls from @hyperframes/core root (was in compiler/rewriteSubCompPaths.ts but not re-exported), required by @hyperframes/studio-server/helpers/subComposition. Removes postcss-selector-parser from @hyperframes/core dependencies (moved to @hyperframes/studio-server which owns the routes that used it). Depends on @hyperframes/parsers (PR #1755). * fix(ci): add parsers+studio-server to Dockerfile and build before preview tests * fix(ci): build @hyperframes/studio-server before Test and studio load smoke Studio's vite.config.ts imports @hyperframes/studio-server, which resolves via its "node" export condition to built dist. The Test and studio-load-smoke jobs only built parsers + core, so esbuild's config load failed to resolve the package entry. Build studio-server too. * fix(studio): repoint sdkCutoverParity test import to studio-server sourceMutation moved from core's studio-api to @hyperframes/studio-server; the test still imported the deleted core path. This was masked while studio's vite.config failed to load (couldn't resolve studio-server); now that the config loads, the test runs and the stale import surfaced.
100 lines
2.9 KiB
TypeScript
100 lines
2.9 KiB
TypeScript
import { mkdirSync, readdirSync, readFileSync, unlinkSync, writeFileSync } from "node:fs";
|
|
import { Buffer } from "node:buffer";
|
|
import { join, relative } from "node:path";
|
|
import { isSafePath } from "./safePath.js";
|
|
|
|
const DEFAULT_KEEP_PER_FILE = 10;
|
|
|
|
export interface BackupJournalResult {
|
|
backupPath: string | null;
|
|
error?: string;
|
|
}
|
|
|
|
function backupKeyForPath(path: string): string {
|
|
return Buffer.from(path, "utf-8").toString("base64url");
|
|
}
|
|
|
|
function timestampPrefix(): string {
|
|
return new Date().toISOString().replace(/[:.]/g, "-");
|
|
}
|
|
|
|
export function backupPathForResponse(
|
|
projectDir: string,
|
|
backupPath: string | null,
|
|
): string | null {
|
|
if (!backupPath) return null;
|
|
const rel = relative(projectDir, backupPath);
|
|
if (!rel || rel.startsWith("..")) return null;
|
|
return rel.split("\\").join("/");
|
|
}
|
|
|
|
export function snapshotBeforeWrite(
|
|
projectDir: string,
|
|
absPath: string,
|
|
options: { keepPerFile?: number } = {},
|
|
): BackupJournalResult {
|
|
if (!isSafePath(projectDir, absPath)) return { backupPath: null };
|
|
|
|
try {
|
|
const content = readFileSync(absPath);
|
|
|
|
const relativePath = relative(projectDir, absPath);
|
|
const backupDir = join(projectDir, ".hyperframes", "backup");
|
|
mkdirSync(backupDir, { recursive: true });
|
|
|
|
const backupKey = backupKeyForPath(relativePath);
|
|
const backupPath = nextBackupPath(backupDir, backupKey);
|
|
writeFileSync(backupPath, content);
|
|
pruneBackups(backupDir, backupKey, options.keepPerFile ?? DEFAULT_KEEP_PER_FILE);
|
|
return { backupPath };
|
|
} catch (error) {
|
|
if (
|
|
error &&
|
|
typeof error === "object" &&
|
|
"code" in error &&
|
|
(error.code === "ENOENT" || error.code === "EISDIR")
|
|
) {
|
|
return { backupPath: null };
|
|
}
|
|
return { backupPath: null, error: error instanceof Error ? error.message : String(error) };
|
|
}
|
|
}
|
|
|
|
function nextBackupPath(backupDir: string, backupKey: string): string {
|
|
const base = `${timestampPrefix()}-${backupKey}`;
|
|
let candidate = join(backupDir, base);
|
|
let counter = 2;
|
|
while (true) {
|
|
try {
|
|
readFileSync(candidate);
|
|
} catch (error) {
|
|
if (error && typeof error === "object" && "code" in error && error.code === "ENOENT") {
|
|
return candidate;
|
|
}
|
|
throw error;
|
|
}
|
|
candidate = join(backupDir, `${base}-${counter}`);
|
|
counter += 1;
|
|
}
|
|
}
|
|
|
|
function pruneBackups(backupDir: string, backupKey: string, keepPerFile: number): void {
|
|
const keep = Math.max(1, Math.floor(keepPerFile));
|
|
const suffix = `-${backupKey}`;
|
|
const numberedSuffix = new RegExp(`-${backupKey}-\\d+$`);
|
|
const matches = readdirSync(backupDir)
|
|
.filter((name) => name.endsWith(suffix) || numberedSuffix.test(name))
|
|
.map((name) => join(backupDir, name))
|
|
.sort((a, b) => {
|
|
return b.localeCompare(a);
|
|
});
|
|
|
|
for (const file of matches.slice(keep)) {
|
|
try {
|
|
unlinkSync(file);
|
|
} catch {
|
|
// Backup pruning is best-effort and must not block the user's write.
|
|
}
|
|
}
|
|
}
|