Files
Tom Moor 2c84b2100f fix: Stream attachments in document exports and stop on disconnect (#14697)
* fix: Stream attachments in single document exports and stop on disconnect

Single document and revision exports read every attachment into memory
before building the zip, and kept running after the client had gone.
Repeated exports of a document with large attachments ran web dynos out
of memory.

- Stream attachments into the zip with a shared AttachmentHelper.addToArchive
- Abort export work when the client connection closes
- Read only images that will be inlined, with a 1MB total inline limit

* fix: Destroy streams opened after abort, treat missing local files as absent
2026-11-24 19:51:15 -05:00

86 lines
2.7 KiB
TypeScript

import JWT from "jsonwebtoken";
import { Hour } from "@shared/utils/time";
import env from "@server/env";
import LocalStorage from "./LocalStorage";
describe("LocalStorage", () => {
describe("getSignedUrl", () => {
// A time deliberately offset from a window boundary, so that rounding is
// exercised and two calls cannot straddle a boundary.
beforeEach(() => {
vi.useFakeTimers();
vi.setSystemTime(new Date("2026-04-16T12:07:31.500Z"));
});
afterEach(() => {
vi.useRealTimers();
});
it("should return an identical url for repeated calls", async () => {
const storage = new LocalStorage();
const first = await storage.getSignedUrl(
"uploads/test.png",
Hour.seconds
);
// Move on within the same window, which is what separates the server
// rendered page from the API request that follows it.
vi.setSystemTime(new Date("2026-04-16T12:08:01.500Z"));
const second = await storage.getSignedUrl(
"uploads/test.png",
Hour.seconds
);
expect(second).toBe(first);
});
it("should return a different url for a different key", async () => {
const storage = new LocalStorage();
const first = await storage.getSignedUrl("uploads/one.png", Hour.seconds);
const second = await storage.getSignedUrl(
"uploads/two.png",
Hour.seconds
);
expect(second).not.toBe(first);
});
it("should return a different url once the window has passed", async () => {
const storage = new LocalStorage();
const first = await storage.getSignedUrl(
"uploads/test.png",
Hour.seconds
);
vi.setSystemTime(new Date("2026-04-16T12:22:31.500Z"));
const second = await storage.getSignedUrl(
"uploads/test.png",
Hour.seconds
);
expect(second).not.toBe(first);
});
it("should remain valid for at least half of the requested lifetime", async () => {
const storage = new LocalStorage();
const url = await storage.getSignedUrl("uploads/test.png", Hour.seconds);
const sig = new URL(url).searchParams.get("sig");
const payload = JWT.verify(sig!, env.SECRET_KEY) as { exp: number };
const remaining = payload.exp - Math.floor(Date.now() / 1000);
expect(remaining).toBeGreaterThanOrEqual(Hour.seconds / 2);
expect(remaining).toBeLessThanOrEqual(Hour.seconds);
});
});
describe("getFileStream", () => {
it("should return null for a file that does not exist", async () => {
const storage = new LocalStorage();
await expect(
storage.getFileStream("uploads/missing/file.png")
).resolves.toBeNull();
});
});
});