fix: cap memory wiki filenames for safe writes

This commit is contained in:
Peter Steinberger
2026-05-06 04:43:56 +01:00
parent 777c539daf
commit ebb8bed78f
2 changed files with 4 additions and 2 deletions

View File

@@ -41,7 +41,7 @@ describe("slugifyWikiSegment", () => {
const fileName = createWikiPageFilename(stem);
expect(fileName.endsWith(".md")).toBe(true);
expect(Buffer.byteLength(fileName)).toBeLessThanOrEqual(255);
expect(Buffer.byteLength(`.${fileName}.fallback.tmp`)).toBeLessThanOrEqual(255);
expect(createWikiPageFilename(stem)).toBe(fileName);
});
});

View File

@@ -109,6 +109,8 @@ const RELATED_BLOCK_PATTERN = new RegExp(
);
const MAX_WIKI_SEGMENT_BYTES = 240;
const MAX_WIKI_FILENAME_COMPONENT_BYTES = 255;
const MAX_WIKI_SAFE_WRITE_FILENAME_COMPONENT_BYTES =
MAX_WIKI_FILENAME_COMPONENT_BYTES - Buffer.byteLength(".fallback.tmp") - 1;
const WIKI_SEGMENT_HASH_BYTES = 12;
function truncateUtf8CodePointSafe(value: string, maxBytes: number): string {
@@ -152,7 +154,7 @@ export function createWikiPageFilename(stem: string, extension = ".md"): string
const normalizedExtension = extension.startsWith(".") ? extension : `.${extension}`;
const maxStemBytes = Math.max(
1,
MAX_WIKI_FILENAME_COMPONENT_BYTES - Buffer.byteLength(normalizedExtension),
MAX_WIKI_SAFE_WRITE_FILENAME_COMPONENT_BYTES - Buffer.byteLength(normalizedExtension),
);
return `${capWikiValueWithHash(stem, maxStemBytes, "page")}${normalizedExtension}`;
}