Files
outline/server/utils/koa.ts
T
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

102 lines
2.7 KiB
TypeScript

import type { ServerResponse } from "node:http";
import contentDisposition from "content-disposition";
import type formidable from "formidable";
import type { Request } from "koa";
import { isArray } from "es-toolkit/compat";
import { ZipFile } from "yazl";
import type { AppContext } from "@server/types";
/**
* Get the first file from an incoming koa request
*
* @param request The incoming request
* @returns The first file or undefined
*/
export const getFileFromRequest = (
request: Request
): formidable.File | undefined => {
const { files } = request;
if (!files) {
return undefined;
}
const file = Object.values(files)[0];
if (!file) {
return undefined;
}
return isArray(file) ? file[0] : file;
};
/**
* Get a signal that aborts when the client connection closes before the
* response is sent in full, so that work for an abandoned request can stop.
*
* @param res The response of the request.
* @returns A signal that aborts when the client disconnects.
*/
export const getClientAbortSignal = (res: ServerResponse): AbortSignal => {
const controller = new AbortController();
if (res.destroyed && !res.writableFinished) {
controller.abort();
return controller.signal;
}
res.once("close", () => {
if (!res.writableFinished) {
controller.abort();
}
});
return controller.signal;
};
/**
* Stream a freshly-built zip archive as the response body. The supplied
* `build` callback receives a yazl ZipFile to populate with entries; the
* helper handles response headers, error forwarding, and finalizing the
* archive once `build` resolves.
*
* @param ctx The koa context to write the response to.
* @param fileName The filename to advertise in the Content-Disposition header.
* @param build Callback that adds entries to the provided ZipFile, and
* receives a signal that aborts when the client disconnects.
*/
export const streamZipResponse = (
ctx: AppContext,
fileName: string,
build: (zip: ZipFile, signal: AbortSignal) => void | Promise<void>
): void => {
const zip = new ZipFile();
const signal = getClientAbortSignal(ctx.res);
ctx.set("Content-Type", "application/zip");
ctx.set(
"Content-Disposition",
contentDisposition(fileName, { type: "attachment" })
);
let failed = false;
const handleError = (err: Error) => {
if (failed) {
return;
}
failed = true;
ctx.app.emit("error", err, ctx);
ctx.res.destroy(err);
};
zip.on("error", handleError);
zip.outputStream.on("error", handleError);
ctx.body = zip.outputStream;
void (async () => {
try {
await build(zip, signal);
zip.end();
} catch (err) {
handleError(err instanceof Error ? err : new Error(String(err)));
}
})();
};