Skip to content

Commit 4a6cd8d

Browse files
saphidcodexclaudejuliusmarminge
authored
fix(preview): release original browser downloads after saving copies (pingdotgg#18435)
Co-authored-by: SAPHID <[email protected]> Co-authored-by: GPT-6.1 Sol <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent b494887 commit 4a6cd8d

2 files changed

Lines changed: 133 additions & 55 deletions

File tree

‎apps/server/src/preview/ServerBrowser.test.ts‎

Lines changed: 131 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import * as NodeEvents from "node:events";
2+
// @effect-diagnostics-next-line nodeBuiltinImport:off - Playwright download mocks perform real file operations outside the Effect runtime.
3+
import * as NodeFSP from "node:fs/promises";
24

35
import * as NodeServices from "@effect/platform-node/NodeServices";
46
import { it } from "@effect/vitest";
@@ -1034,6 +1036,15 @@ it.live("a tab opened to a file the browser cannot show reports the file to down
10341036
const written = Promise.withResolvers<void>();
10351037
page.emit("download", {
10361038
failure: async () => null,
1039+
delete: async () => {
1040+
// A new navigation during cleanup must not erase the settled download status.
1041+
page.emit("request", {
1042+
url: () => "https://example.com/next",
1043+
method: () => "GET",
1044+
isNavigationRequest: () => true,
1045+
frame: () => page,
1046+
});
1047+
},
10371048
saveAs: (path: string) => {
10381049
Queue.offerUnsafe(saved, path);
10391050
return written.promise;
@@ -1044,9 +1055,23 @@ it.live("a tab opened to a file the browser cannot show reports the file to down
10441055
const fs = yield* FileSystem.FileSystem;
10451056
yield* fs.writeFileString(yield* Queue.take(saved), "%PDF");
10461057
written.resolve();
1047-
let status = yield* PubSub.take(events);
1048-
while (status.type !== "failed") status = yield* PubSub.take(events);
1049-
expect(status).toMatchObject({ url: pdf, download: { fileName: "paper.pdf" } });
1058+
const received = [];
1059+
let status;
1060+
do {
1061+
status = yield* PubSub.take(events);
1062+
received.push(status);
1063+
} while (
1064+
status.type !== "navigated" ||
1065+
status.snapshot.navStatus._tag !== "Loading" ||
1066+
status.snapshot.navStatus.url !== "https://example.com/next"
1067+
);
1068+
expect(received).toContainEqual(
1069+
expect.objectContaining({
1070+
type: "failed",
1071+
url: pdf,
1072+
download: { id: expect.any(String), fileName: "paper.pdf" },
1073+
}),
1074+
);
10501075
expect((yield* Queue.clear(viewer.output)).some((item) => item._tag === "download")).toBe(
10511076
false,
10521077
);
@@ -1078,6 +1103,7 @@ it.live("a file a blank tab opened is offered to download if the tab moves on wh
10781103
const written = Promise.withResolvers<void>();
10791104
page.emit("download", {
10801105
failure: async () => null,
1106+
delete: async () => {},
10811107
saveAs: (path: string) => {
10821108
Queue.offerUnsafe(saved, path);
10831109
return written.promise;
@@ -1120,6 +1146,7 @@ it.live("a download from a superseded navigation leaves the newer navigation loa
11201146
const written = Promise.withResolvers<void>();
11211147
page.emit("download", {
11221148
failure: async () => null,
1149+
delete: async () => {},
11231150
saveAs: (path: string) => {
11241151
Queue.offerUnsafe(saved, path);
11251152
return written.promise;
@@ -1321,58 +1348,107 @@ it.live("copy keys reach the viewer from a frame in its own process", () =>
13211348
).pipe(Effect.provide(layer)),
13221349
);
13231350

1324-
it.live("a page download is saved, offered to the controller, and listed for the agent", () =>
1325-
Effect.scoped(
1326-
Effect.gen(function* () {
1327-
const { browser, broker, tabId } = yield* ready;
1328-
const page = contexts[0]!.page;
1329-
const viewer = yield* browser.attachViewer(viewerInput(tabId, true));
1330-
yield* viewer.input({ type: "takeControl" });
1331-
yield* Queue.clear(viewer.output);
1332-
const saved = yield* Queue.unbounded<string>();
1333-
const written = Promise.withResolvers<void>();
1334-
page.emit("download", {
1335-
failure: async () => null,
1336-
saveAs: (path: string) => {
1337-
Queue.offerUnsafe(saved, path);
1338-
return written.promise;
1339-
},
1340-
suggestedFilename: () => "report.csv",
1341-
url: () => "blob:http://localhost:5173/1",
1342-
});
1343-
const fs = yield* FileSystem.FileSystem;
1344-
const path = yield* Queue.take(saved);
1345-
yield* fs.writeFileString(path, "a,b");
1346-
written.resolve();
1347-
let offered = yield* Queue.take(viewer.output);
1348-
while (offered._tag !== "download") offered = yield* Queue.take(viewer.output);
1349-
expect(offered).toMatchObject({ _tag: "download", fileName: "report.csv", sizeBytes: 3 });
1350-
const file = yield* browser.openDownload({
1351-
threadId: scope.thread.threadId,
1352-
tabId,
1353-
downloadId: offered.id,
1354-
});
1355-
expect(Option.isSome(file) && file.value.fileName).toBe("report.csv");
1356-
const status = yield* broker.invoke<PreviewAutomationStatus>({
1357-
scope,
1358-
tabId,
1359-
operation: "status",
1360-
input: {},
1361-
});
1362-
expect(status.downloads).toEqual([
1363-
expect.objectContaining({ fileName: "report.csv", sizeBytes: 3 }),
1364-
]);
1365-
expect(
1366-
Option.isNone(
1367-
yield* browser.openDownload({
1368-
threadId: scope.thread.threadId,
1369-
tabId,
1370-
downloadId: "guess",
1371-
}),
1372-
),
1373-
).toBe(true);
1374-
}),
1375-
).pipe(Effect.provide(layer)),
1351+
it.live.each([false, true])(
1352+
"a page download is saved, offered to the controller, and listed for the agent (delete fails: %s)",
1353+
(deleteFails) =>
1354+
Effect.scoped(
1355+
Effect.gen(function* () {
1356+
const { browser, broker, tabId } = yield* ready;
1357+
const page = contexts[0]!.page;
1358+
const viewer = yield* browser.attachViewer(viewerInput(tabId, true));
1359+
yield* viewer.input({ type: "takeControl" });
1360+
yield* Queue.clear(viewer.output);
1361+
const fs = yield* FileSystem.FileSystem;
1362+
const original = `${yield* fs.makeTempDirectoryScoped()}/chromium-download`;
1363+
yield* fs.writeFileString(original, "a,b");
1364+
const released = Promise.withResolvers<void>();
1365+
page.emit("download", {
1366+
failure: async () => null,
1367+
delete: async () => {
1368+
try {
1369+
if (deleteFails) throw new Error("context closed");
1370+
await NodeFSP.rm(original);
1371+
} finally {
1372+
released.resolve();
1373+
}
1374+
},
1375+
saveAs: (path: string) => NodeFSP.copyFile(original, path),
1376+
suggestedFilename: () => "report.csv",
1377+
url: () => "blob:http://localhost:5173/1",
1378+
});
1379+
let offered = yield* Queue.take(viewer.output);
1380+
while (offered._tag !== "download") offered = yield* Queue.take(viewer.output);
1381+
expect(offered).toMatchObject({ _tag: "download", fileName: "report.csv", sizeBytes: 3 });
1382+
yield* Effect.promise(() => released.promise);
1383+
expect(yield* fs.exists(original)).toBe(deleteFails);
1384+
const file = yield* browser.openDownload({
1385+
threadId: scope.thread.threadId,
1386+
tabId,
1387+
downloadId: offered.id,
1388+
});
1389+
expect(Option.isSome(file) && file.value.fileName).toBe("report.csv");
1390+
expect(yield* fs.readFileString(Option.getOrThrow(file).path)).toBe("a,b");
1391+
const status = yield* broker.invoke<PreviewAutomationStatus>({
1392+
scope,
1393+
tabId,
1394+
operation: "status",
1395+
input: {},
1396+
});
1397+
expect(status.downloads).toEqual([
1398+
expect.objectContaining({ fileName: "report.csv", sizeBytes: 3 }),
1399+
]);
1400+
expect(
1401+
Option.isNone(
1402+
yield* browser.openDownload({
1403+
threadId: scope.thread.threadId,
1404+
tabId,
1405+
downloadId: "guess",
1406+
}),
1407+
),
1408+
).toBe(true);
1409+
}),
1410+
).pipe(Effect.provide(layer)),
1411+
);
1412+
1413+
it.live.each(["save failed", "oversized"])(
1414+
"a rejected download releases the original and keeps no saved copy (%s)",
1415+
(outcome) =>
1416+
Effect.scoped(
1417+
Effect.gen(function* () {
1418+
const { broker, tabId } = yield* ready;
1419+
const fs = yield* FileSystem.FileSystem;
1420+
const original = `${yield* fs.makeTempDirectoryScoped()}/chromium-download`;
1421+
yield* fs.writeFileString(original, "a,b");
1422+
let savedPath: string | undefined;
1423+
const released = Promise.withResolvers<void>();
1424+
contexts[0]!.page.emit("download", {
1425+
failure: async () => null,
1426+
delete: async () => {
1427+
await NodeFSP.rm(original);
1428+
released.resolve();
1429+
},
1430+
saveAs: async (path: string) => {
1431+
savedPath = path;
1432+
await NodeFSP.copyFile(original, path);
1433+
if (outcome === "save failed") throw new Error("save interrupted");
1434+
// A sparse file exercises the size limit without writing a GiB of data.
1435+
await NodeFSP.truncate(path, 1024 ** 3 + 1);
1436+
},
1437+
suggestedFilename: () => "report.csv",
1438+
url: () => "blob:http://localhost:5173/1",
1439+
});
1440+
yield* Effect.promise(() => released.promise);
1441+
expect(yield* fs.exists(original)).toBe(false);
1442+
if (savedPath !== undefined) expect(yield* fs.exists(savedPath)).toBe(false);
1443+
const status = yield* broker.invoke<PreviewAutomationStatus>({
1444+
scope,
1445+
tabId,
1446+
operation: "status",
1447+
input: {},
1448+
});
1449+
expect(status.downloads).toEqual([]);
1450+
}),
1451+
).pipe(Effect.provide(layer)),
13761452
);
13771453

13781454
it.live("a page's file picker goes to the controller and takes its uploaded files", () =>

‎apps/server/src/preview/ServerBrowser.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,8 @@ const make = Effect.gen(function* () {
985985
if (navigation !== null && !tab.closing) {
986986
settleAbortedNavigation(tab, navigation, download.url(), offered);
987987
}
988+
// saveAs copies the artifact; cached profile contexts otherwise retain the original.
989+
await download.delete().catch(constVoid);
988990
}
989991
};
990992

0 commit comments

Comments
 (0)