Files
outline/server/models/helpers/HTMLHelper.test.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

116 lines
3.0 KiB
TypeScript

import HTMLHelper from "./HTMLHelper";
describe("HTMLHelper", () => {
const document = `<html>
<head>
<style>h1 { color:blue; }</style>
</head>
<body>
<h1>Big Text</h1>
</body>
</html>`;
describe("inlineCSS", () => {
it("should inline CSS from style tags", async () => {
const result = await HTMLHelper.inlineCSS(document);
expect(result).toBe(`<html><head>
</head>
<body>
<h1 style="color: blue;">Big Text</h1>
</body></html>`);
});
it("should initialize once", async () => {
const first = await HTMLHelper.inlineCSS(document);
const second = await HTMLHelper.inlineCSS(document);
expect(first).toBe(second);
});
});
describe("inlineImage", () => {
const redirectUrl = "/api/attachments.redirect?id=123";
const buffer = Buffer.from("small image");
it("should inline a small single-referenced image as a data URI", () => {
const html = `<p><img src="${redirectUrl}"></p>`;
const result = HTMLHelper.inlineImage(
html,
redirectUrl,
"image/png",
buffer
);
expect(result).toBe(
`<p><img src="data:image/png;base64,${buffer.toString("base64")}"></p>`
);
});
it("should not inline non-image attachments", () => {
const html = `<a href="${redirectUrl}">file</a>`;
const result = HTMLHelper.inlineImage(
html,
redirectUrl,
"application/pdf",
buffer
);
expect(result).toBeNull();
});
it("should not inline images larger than the maximum size", () => {
const html = `<p><img src="${redirectUrl}"></p>`;
const large = Buffer.alloc(HTMLHelper.maxInlineImageSize + 1);
const result = HTMLHelper.inlineImage(
html,
redirectUrl,
"image/png",
large
);
expect(result).toBeNull();
});
it("should not inline images referenced more than once", () => {
const html = `<p><img src="${redirectUrl}"><img src="${redirectUrl}"></p>`;
const result = HTMLHelper.inlineImage(
html,
redirectUrl,
"image/png",
buffer
);
expect(result).toBeNull();
});
it("should not inline images past the total size limit", () => {
const html = `<p><img src="${redirectUrl}"></p>`;
expect(
HTMLHelper.inlineImage(
html,
redirectUrl,
"image/png",
buffer,
HTMLHelper.maxTotalInlineImageSize - buffer.length
)
).not.toBeNull();
expect(
HTMLHelper.inlineImage(
html,
redirectUrl,
"image/png",
buffer,
HTMLHelper.maxTotalInlineImageSize - buffer.length + 1
)
).toBeNull();
});
it("should not inline an empty buffer", () => {
const html = `<p><img src="${redirectUrl}"></p>`;
const result = HTMLHelper.inlineImage(
html,
redirectUrl,
"image/png",
Buffer.from("")
);
expect(result).toBeNull();
});
});
});