Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
perf(export): flush headers so desktop streams the zip instead of buffering
Elian Doran233926a08545
diff --git a/packages/trilium-core/src/services/export/zip.spec.ts b/packages/trilium-core/src/services/export/zip.spec.tsindex 96aa4c218b..d97eb76392 100644--- a/packages/trilium-core/src/services/export/zip.spec.ts+++ b/packages/trilium-core/src/services/export/zip.spec.ts@@ -102,6 +102,33 @@ function parseMeta(entries: Record<string, Buffer>): NoteMetaFile {return JSON.parse(entries["!!!meta.json"].toString("utf-8"));}+/** A ZipProvider that delegates to `original` but records every archive operation into `events`. */+function makeRecordingProvider(original: ZipProvider, events: string[]): ZipProvider {+ return {+ detectFilenameEncoding: (b) => original.detectFilenameEncoding(b),+ readZipFile: (b, fn, enc) => original.readZipFile(b, fn, enc),+ createFileStream: (p) => original.createFileStream(p),+ createZipArchive() {+ const real = original.createZipArchive();+ const wrapper: ZipArchive = {+ append(content: string | Uint8Array, options: ZipArchiveEntryOptions) {+ events.push(`append:${options.name}`);+ real.append(content, options);+ },+ pipe(dest: unknown) {+ events.push("pipe");+ real.pipe(dest);+ },+ finalize() {+ events.push("finalize");+ return real.finalize();+ }+ };+ return wrapper;+ }+ };+}+// Gated to Node: these real-DB export tests rely on Node stream semantics.// The in-memory exportToZip tests pipe into a PassThrough and await its `end`// event, but the BrowserZipProvider.finalize() writes synchronously via@@ -315,31 +342,7 @@ describe.skipIf(isBrowserRuntime)("zip export (real DB)", () => {const events: string[] = [];const original = getZipProvider();- const spy: ZipProvider = {- detectFilenameEncoding: (b) => original.detectFilenameEncoding(b),- readZipFile: (b, fn, enc) => original.readZipFile(b, fn, enc),- createFileStream: (p) => original.createFileStream(p),- createZipArchive() {- const real = original.createZipArchive();- const wrapper: ZipArchive = {- append(content: string | Uint8Array, options: ZipArchiveEntryOptions) {- events.push(`append:${options.name}`);- real.append(content, options);- },- pipe(dest: unknown) {- events.push("pipe");- real.pipe(dest);- },- finalize() {- events.push("finalize");- return real.finalize();- }- };- return wrapper;- }- };-- initZipProvider(spy);+ initZipProvider(makeRecordingProvider(original, events));try {const taskContext = (await import("../task_context.js")).default;const ctx = new taskContext("no-progress-reporting", "export", null);@@ -361,6 +364,42 @@ describe.skipIf(isBrowserRuntime)("zip export (real DB)", () => {expect(events[events.length - 1]).toBe("finalize");});+ it("flushes response headers before streaming so the desktop dispatcher streams instead of buffering", async () => {+ // On desktop, exportToZip's `res` is a node-mocks-http mock that buffers+ // the entire body unless the route calls res.flushHeaders() to commit a+ // streaming response. The export must flush *before* piping any content.+ const { note } = createNote("root", { title: "FlushFirst", content: "<p>x</p>" });+ const branch = note.getParentBranches()[0];++ const events: string[] = [];+ const original = getZipProvider();++ // A response that records flushHeaders, mirroring http.ServerResponse+ // (and the desktop mock) which both expose the method.+ class FlushRecordingResponse extends FakeResponse {+ flushHeaders() {+ events.push("flushHeaders");+ }+ }++ initZipProvider(makeRecordingProvider(original, events));+ try {+ const taskContext = (await import("../task_context.js")).default;+ const ctx = new taskContext("no-progress-reporting", "export", null);+ const res = new FlushRecordingResponse();+ const done = collect(res);+ await zip.exportToZip(ctx, branch, "html", res as unknown as Record<string, unknown>);+ await done;+ } finally {+ initZipProvider(original);+ }++ // flushHeaders is called, and precedes the pipe so the streaming bridge+ // commits before any body bytes are written.+ expect(events).toContain("flushHeaders");+ expect(events.indexOf("flushHeaders")).toBeLessThan(events.indexOf("pipe"));+ });+it("keeps the extension on very long multi-byte titles within the 255-byte limit", async () => {// A title of 3-byte CJK characters long enough that, once the upstream// 255-byte sanitize cap fills the base, appending the extension woulddiff --git a/packages/trilium-core/src/services/export/zip.ts b/packages/trilium-core/src/services/export/zip.tsindex 5402e0a17a..745fd47e24 100644--- a/packages/trilium-core/src/services/export/zip.ts+++ b/packages/trilium-core/src/services/export/zip.ts@@ -441,6 +441,16 @@ async function exportToZip(taskContext: TaskContext<"export">, branch: BBranch,res.setHeader("Content-Type", "application/zip");}+ // Commit the response headers up front. On the desktop build, `res` is a+ // node-mocks-http mock behind the trilium-app:// protocol dispatcher, which+ // buffers the entire body (and copies it again) unless flushHeaders() is+ // called to switch it to a streaming ReadableStream response. On the plain+ // HTTP server this is a harmless early header flush. Either way it must+ // happen before any body bytes are written below.+ if (setHeaders && typeof res.flushHeaders === "function") {+ res.flushHeaders();+ }+// Start streaming to the destination *before* appending content. The archiver// drains each appended blob as it is added, so memory stays bounded instead of// buffering the whole export. Trade-off: a failure while reading content mid-