Skip to content

Commit d2741b3

Browse files
authored
fix(studio-server): preserve binary file writes and versions (#3653)
* fix(studio-server): preserve binary file writes and versions * fix(studio-server): rely on exclusive file creation for POST * test(studio-server): create race fixture atomically
1 parent 924b98a commit d2741b3

4 files changed

Lines changed: 108 additions & 20 deletions

File tree

‎packages/cli/src/server/studioServer.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ export function createStudioServer(options: StudioServerOptions): StudioServer {
779779
const absPath = resolve(projectDir, path);
780780
let version: string | null = null;
781781
try {
782-
version = fileContentVersion(readFileSync(absPath, "utf-8"));
782+
version = fileContentVersion(readFileSync(absPath));
783783
} catch {
784784
// A deletion has no current bytes to match against an API write receipt.
785785
}

‎packages/studio-server/src/helpers/fileVersion.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ const RECEIPT_TTL_MS = 10_000;
1414
const receipts = new Map<string, StoredReceipt[]>();
1515

1616
/** Strong content version used as both the JSON version and HTTP ETag. */
17-
export function fileContentVersion(content: string): string {
18-
return `"sha256:${createHash("sha256").update(content, "utf8").digest("hex")}"`;
17+
export function fileContentVersion(content: string | Uint8Array): string {
18+
return `"sha256:${createHash("sha256").update(content).digest("hex")}"`;
1919
}
2020

2121
export function createWriteToken(requestToken?: string): string {

‎packages/studio-server/src/routes/files.test.ts‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,91 @@ describe("registerFileRoutes", () => {
284284
expect(response.headers.get("etag")).toBe(payload.version);
285285
});
286286

287+
it.each(["POST", "PUT"])("preserves arbitrary bytes when creating through %s", async (method) => {
288+
const projectDir = createProjectDir();
289+
const app = new Hono();
290+
registerFileRoutes(app, createAdapter(projectDir));
291+
const bytes = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0, 0xff, 0xc0, 0x80]);
292+
const url = "http://localhost/projects/demo/files/assets/image.png";
293+
const response = await app.request(url, {
294+
method,
295+
headers: { "Content-Type": "application/octet-stream", "If-None-Match": "*" },
296+
body: bytes,
297+
});
298+
expect(response.status).toBe(method === "POST" ? 201 : 200);
299+
expect(readFileSync(join(projectDir, "assets/image.png"))).toEqual(bytes);
300+
const read = await app.request(url);
301+
const payload = await read.json();
302+
expect(payload.content).toBe(bytes.toString("utf-8"));
303+
expect(payload.version).toBe(fileContentVersion(bytes));
304+
expect(read.headers.get("etag")).toBe(payload.version);
305+
const duplicate = await app.request(url, {
306+
method,
307+
headers: { "If-None-Match": "*" },
308+
body: "overwrite",
309+
});
310+
expect(duplicate.status).toBe(409);
311+
expect(readFileSync(join(projectDir, "assets/image.png"))).toEqual(bytes);
312+
});
313+
314+
it("does not overwrite a file created while a POST body is being read", async () => {
315+
const projectDir = createProjectDir();
316+
const app = new Hono();
317+
registerFileRoutes(app, createAdapter(projectDir));
318+
const path = join(projectDir, "raced.bin");
319+
const existing = Buffer.from([0xff, 0, 0x80]);
320+
const request = new Request("http://localhost/projects/demo/files/raced.bin", {
321+
method: "POST",
322+
body: "upload",
323+
});
324+
const readBody = request.arrayBuffer.bind(request);
325+
vi.spyOn(request, "arrayBuffer").mockImplementation(async () => {
326+
writeFileSync(path, existing, { flag: "wx" });
327+
return readBody();
328+
});
329+
const response = await app.request(request);
330+
expect(response.status).toBe(409);
331+
expect(readFileSync(path)).toEqual(existing);
332+
});
333+
334+
it("versions binary bytes exactly while preserving conflicts, backups, and write receipts", async () => {
335+
const projectDir = createProjectDir();
336+
const app = new Hono();
337+
registerFileRoutes(app, createAdapter(projectDir));
338+
const path = join(projectDir, "image.png");
339+
const before = Buffer.from([0xff, 0, 0x80, 0x81]);
340+
const after = Buffer.from([0xfe, 0, 0x80]);
341+
writeFileSync(path, before);
342+
const url = "http://localhost/projects/demo/files/image.png";
343+
const read = await app.request(url);
344+
const version = read.headers.get("etag")!;
345+
// Invalid UTF-8 bytes may decode to the same string, but must not share a version.
346+
const staleBytes = Buffer.from([0xfe, 0, 0x80, 0x81]);
347+
expect(staleBytes.toString("utf-8")).toBe(before.toString("utf-8"));
348+
for (const headers of [{}, { "If-Match": fileContentVersion(staleBytes) }]) {
349+
const rejected = await app.request(url, { method: "PUT", headers, body: after });
350+
expect([428, 409]).toContain(rejected.status);
351+
expect((await rejected.json()).currentVersion).toBe(version);
352+
expect(readFileSync(path)).toEqual(before);
353+
}
354+
const response = await app.request(url, {
355+
method: "PUT",
356+
headers: { "If-Match": version, "X-Hyperframes-Write-Token": "binary-write" },
357+
body: after,
358+
});
359+
const payload = await response.json();
360+
expect(response.status).toBe(200);
361+
expect(readFileSync(path)).toEqual(after);
362+
expect(readFileSync(join(projectDir, payload.backupPath))).toEqual(before);
363+
expect(payload.version).toBe(fileContentVersion(after));
364+
expect(response.headers.get("etag")).toBe(payload.version);
365+
expect(consumeFileWriteReceipt(path, payload.version)).toEqual({
366+
path: "image.png",
367+
version: payload.version,
368+
writeToken: "binary-write",
369+
});
370+
});
371+
287372
it("requires If-Match for updates and preserves the current bytes", async () => {
288373
const projectDir = createProjectDir();
289374
const app = new Hono();

‎packages/studio-server/src/routes/files.ts‎

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2253,10 +2253,10 @@ export function registerFileRoutes(api: Hono, adapter: StudioApiAdapter): void {
22532253
return c.json({ error: "not found" }, 404);
22542254
}
22552255

2256-
const content = readFileSync(res.absPath, "utf-8");
2256+
const content = readFileSync(res.absPath);
22572257
const version = fileContentVersion(content);
22582258
c.header("ETag", version);
2259-
return c.json({ filename: res.filePath, content, version });
2259+
return c.json({ filename: res.filePath, content: content.toString("utf-8"), version });
22602260
});
22612261

22622262
// ── Write (overwrite) ──
@@ -2265,13 +2265,13 @@ export function registerFileRoutes(api: Hono, adapter: StudioApiAdapter): void {
22652265
const res = await resolveProjectFile(c, adapter);
22662266
if ("error" in res) return res.error;
22672267

2268-
const body = await c.req.text();
2268+
const body = Buffer.from(await c.req.arrayBuffer());
22692269
const expectedVersion = c.req.header("If-Match")?.trim() ?? null;
22702270
const createOnly = c.req.header("If-None-Match")?.trim() === "*";
22712271
if (expectedVersion === null && !createOnly) {
2272-
let currentContent: string | null = null;
2272+
let currentContent: Buffer | null = null;
22732273
try {
2274-
currentContent = readFileSync(res.absPath, "utf-8");
2274+
currentContent = readFileSync(res.absPath);
22752275
} catch (error) {
22762276
if (!error || typeof error !== "object" || !("code" in error) || error.code !== "ENOENT") {
22772277
throw error;
@@ -2282,7 +2282,7 @@ export function registerFileRoutes(api: Hono, adapter: StudioApiAdapter): void {
22822282
error: "precondition required",
22832283
path: res.filePath,
22842284
currentVersion: currentContent === null ? null : fileContentVersion(currentContent),
2285-
currentContent,
2285+
currentContent: currentContent?.toString("utf-8") ?? null,
22862286
},
22872287
428,
22882288
);
@@ -2298,19 +2298,19 @@ export function registerFileRoutes(api: Hono, adapter: StudioApiAdapter): void {
22982298
if (!error || typeof error !== "object" || !("code" in error) || error.code !== "EEXIST") {
22992299
throw error;
23002300
}
2301-
const currentContent = readFileSync(res.absPath, "utf-8");
2301+
const currentContent = readFileSync(res.absPath);
23022302
return c.json(
23032303
{
23042304
error: "file conflict",
23052305
path: res.filePath,
23062306
currentVersion: fileContentVersion(currentContent),
2307-
currentContent,
2307+
currentContent: currentContent.toString("utf-8"),
23082308
},
23092309
409,
23102310
);
23112311
}
23122312
try {
2313-
writeSync(fd, body, 0, "utf-8");
2313+
writeSync(fd, body, 0, body.length, 0);
23142314
} finally {
23152315
closeSync(fd);
23162316
}
@@ -2333,15 +2333,15 @@ export function registerFileRoutes(api: Hono, adapter: StudioApiAdapter): void {
23332333
);
23342334
}
23352335
try {
2336-
const currentContent = readFileSync(fd, "utf-8");
2336+
const currentContent = readFileSync(fd);
23372337
const currentVersion = fileContentVersion(currentContent);
23382338
if (expectedVersion !== currentVersion) {
23392339
return c.json(
23402340
{
23412341
error: "file conflict",
23422342
path: res.filePath,
23432343
currentVersion,
2344-
currentContent,
2344+
currentContent: currentContent.toString("utf-8"),
23452345
},
23462346
409,
23472347
);
@@ -2350,7 +2350,7 @@ export function registerFileRoutes(api: Hono, adapter: StudioApiAdapter): void {
23502350
if (backup.error)
23512351
console.warn(`Failed to create backup for ${res.filePath}: ${backup.error}`);
23522352
ftruncateSync(fd, 0);
2353-
writeSync(fd, body, 0, "utf-8");
2353+
writeSync(fd, body, 0, body.length, 0);
23542354
} finally {
23552355
closeSync(fd);
23562356
}
@@ -2375,14 +2375,17 @@ export function registerFileRoutes(api: Hono, adapter: StudioApiAdapter): void {
23752375
const res = await resolveProjectFile(c, adapter);
23762376
if ("error" in res) return res.error;
23772377

2378-
if (existsSync(res.absPath)) {
2378+
ensureDir(res.absPath);
2379+
const body = Buffer.from(await c.req.arrayBuffer());
2380+
try {
2381+
writeFileSync(res.absPath, body, { flag: "wx" });
2382+
} catch (error) {
2383+
if (!error || typeof error !== "object" || !("code" in error) || error.code !== "EEXIST") {
2384+
throw error;
2385+
}
23792386
return c.json({ error: "already exists" }, 409);
23802387
}
23812388

2382-
ensureDir(res.absPath);
2383-
const body = await c.req.text().catch(() => "");
2384-
writeFileSync(res.absPath, body, "utf-8");
2385-
23862389
return c.json({ ok: true, path: res.filePath }, 201);
23872390
});
23882391

0 commit comments

Comments
 (0)