mirror of
https://github.com/outline/outline.git
synced 2026-11-26 07:33:19 +03:00
* 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
116 lines
3.0 KiB
TypeScript
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();
|
|
});
|
|
});
|
|
});
|