mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-23 11:29:54 +00:00
feat(studio-server): history names the bytes its next restore point would copy in (#8120)
* feat(studio-server): history names the bytes its next restore point would copy in * fix(studio-server): count each unsaved blob once, and name the method for what it counts * refactor(studio-server): a file the next restore point copies nothing of is its own check
This commit is contained in:
@@ -70,7 +70,7 @@ async function readChecked(path: string, hash: string): Promise<Buffer> {
|
||||
|
||||
export const readBlob = (dir: string, hash: string) => readChecked(blobPath(dir, hash), hash);
|
||||
|
||||
async function hashFile(path: string): Promise<string> {
|
||||
export async function hashFile(path: string): Promise<string> {
|
||||
const hash = createHash("sha256");
|
||||
for await (const chunk of createReadStream(path)) hash.update(chunk);
|
||||
return hash.digest("hex");
|
||||
|
||||
@@ -30,7 +30,14 @@ import {
|
||||
listProjectFilesAsync,
|
||||
type ListedProjectFile,
|
||||
} from "../helpers/projectSignature.js";
|
||||
import { isSettling, openBlobStore, readBlob, SETTLE_MS, type BlobStore } from "./blobStore.js";
|
||||
import {
|
||||
hashFile,
|
||||
isSettling,
|
||||
openBlobStore,
|
||||
readBlob,
|
||||
SETTLE_MS,
|
||||
type BlobStore,
|
||||
} from "./blobStore.js";
|
||||
import { isUncopied, uncopiedOnlyHashes } from "./uncopied.js";
|
||||
import {
|
||||
assertStillWithin,
|
||||
@@ -155,6 +162,7 @@ export interface ProjectHistory {
|
||||
label: string,
|
||||
options?: { idleMs?: number; exclusive?: boolean },
|
||||
): Promise<HistoryWindow>;
|
||||
unsavedBlobBytes(): Promise<number>;
|
||||
/**
|
||||
* For a writer that records after writing (Studio): moves the uncommitted changes to `paths` into one entry of
|
||||
* `who`'s, each cut at the `overwrote` version so earlier parts stay their writer's. Same-key claims merge until
|
||||
@@ -289,6 +297,12 @@ function withoutHiddenPaths(log: HistoryLog, ignores?: (path: string) => boolean
|
||||
return log;
|
||||
}
|
||||
|
||||
const hashIfPresent = (path: string) =>
|
||||
hashFile(path).catch((error: NodeJS.ErrnoException) => {
|
||||
if (error.code === "ENOENT") return null;
|
||||
throw error;
|
||||
});
|
||||
|
||||
function dropKeptOutside(entry: HistoryEntry, ignores?: (path: string) => boolean): boolean {
|
||||
const kept = Object.entries(entry.keptInPlace ?? {});
|
||||
const left = kept.filter(([path]) => isHistoryPath(path, ignores));
|
||||
@@ -830,6 +844,24 @@ class Engine {
|
||||
replaceFileAtomically(join(this.home, "stat.json"), files, 0o644);
|
||||
}
|
||||
|
||||
copiesNothing(file: ListedFile, now: number): boolean {
|
||||
if (this.adopting.has(file.path) || isUncopied(file.path, file.size)) return true;
|
||||
const stat = statKey(file, now);
|
||||
return stat !== "" && this.tracked.get(file.path)?.stat === stat;
|
||||
}
|
||||
|
||||
async unsavedBlobBytes(): Promise<number> {
|
||||
if (this.whereFolder() !== "here") return 0;
|
||||
const now = this.now();
|
||||
const missing = new Map<string, number>();
|
||||
for (const file of await this.historyFiles()) {
|
||||
if (this.copiesNothing(file, now)) continue;
|
||||
const hash = await hashIfPresent(join(this.dir, file.path));
|
||||
if (hash && !this.blobs.has(hash)) missing.set(hash, file.size);
|
||||
}
|
||||
return [...missing.values()].reduce((sum, size) => sum + size, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes in every write since the last sweep and files each change to its writer. ponytail: always the whole
|
||||
* project (a stat per file, a hash only when the stat moved); per-path sweeps if projects reach tens of thousands.
|
||||
@@ -1611,6 +1643,7 @@ class Engine {
|
||||
options.idleMs ?? this.options.maxGroupMs ?? 30_000,
|
||||
options.exclusive === true,
|
||||
),
|
||||
unsavedBlobBytes: () => this.unsavedBlobBytes(),
|
||||
claim: async (who, label, paths, options = {}) => {
|
||||
await Promise.all(paths.map((path) => this.adopting.get(this.logPath(path))?.retryNow()));
|
||||
return this.queue(() => this.claimNow(who, label, paths, options));
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
// @vitest-environment node
|
||||
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { afterEach, expect, it } from "vitest";
|
||||
import { openProjectHistory } from "./projectHistory";
|
||||
|
||||
const cleanup: Array<() => unknown> = [];
|
||||
afterEach(async () => {
|
||||
for (const step of cleanup.splice(0).reverse()) await step();
|
||||
});
|
||||
|
||||
function tempDir(prefix: string): string {
|
||||
const dir = mkdtempSync(join(tmpdir(), prefix));
|
||||
cleanup.push(() => rmSync(dir, { recursive: true, force: true }));
|
||||
return dir;
|
||||
}
|
||||
|
||||
it("names the bytes the next restore point would copy in: changed files the store lacks, no media", async () => {
|
||||
const projectDir = tempDir("hf-history-project-");
|
||||
const historyRoot = tempDir("hf-history-root-");
|
||||
writeFileSync(join(projectDir, "index.html"), "<h1>Hello</h1>");
|
||||
const history = await openProjectHistory({
|
||||
projectDir,
|
||||
historyRoot,
|
||||
ignores: (path) => path.startsWith("private/"),
|
||||
});
|
||||
cleanup.push(() => history.close());
|
||||
await (await history.beginWindow({ kind: "person", name: "You" }, "first")).close();
|
||||
expect(await history.unsavedBlobBytes()).toBe(0);
|
||||
|
||||
writeFileSync(join(projectDir, "scene.html"), "x".repeat(1000));
|
||||
// The same bytes as a stored file are already in the store.
|
||||
writeFileSync(join(projectDir, "copy.html"), "<h1>Hello</h1>");
|
||||
writeFileSync(join(projectDir, "clip.mp4"), "v".repeat(5000));
|
||||
expect(await history.unsavedBlobBytes()).toBe(1000);
|
||||
|
||||
// Two new files with the same bytes are one blob.
|
||||
writeFileSync(join(projectDir, "b1.html"), "y".repeat(2000));
|
||||
writeFileSync(join(projectDir, "b2.html"), "y".repeat(2000));
|
||||
expect(await history.unsavedBlobBytes()).toBe(3000);
|
||||
|
||||
// A path history ignores is never copied in.
|
||||
mkdirSync(join(projectDir, "private"));
|
||||
writeFileSync(join(projectDir, "private", "notes.html"), "z".repeat(4000));
|
||||
expect(await history.unsavedBlobBytes()).toBe(3000);
|
||||
});
|
||||
Reference in New Issue
Block a user