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
core/backup: fix some issues
Adorian Dorand6ae6b9e7e6b
diff --git a/packages/commons/src/lib/filesystem_name.spec.ts b/packages/commons/src/lib/filesystem_name.spec.tsindex 1378e39df8..0821786798 100644--- a/packages/commons/src/lib/filesystem_name.spec.ts+++ b/packages/commons/src/lib/filesystem_name.spec.ts@@ -1,6 +1,10 @@import { describe, expect, it } from "vitest";-import { asFileName, tidyFilesystemFriendlyName, toFilesystemFriendlyName } from "./filesystem_name.js";+import {+ asFileName,+ tidyFilesystemFriendlyName,+ toFilesystemFriendlyName+} from "./filesystem_name.js";describe("what a file name may hold", () => {it("keeps everything a file name can carry, other scripts included", () => {@@ -14,8 +18,8 @@ describe("what a file name may hold", () => {it("drops what no filesystem would take", () => {expect(toFilesystemFriendlyName('a<b>c:d"e/f\\g|h?i*j')).toBe("abcdefghij");// Control characters, which cannot be written down as themselves here.- expect(toFilesystemFriendlyName(`before${String.fromCharCode(1)}after${String.fromCharCode(31)}end`))- .toBe("beforeafterend");+ const control = `before${String.fromCharCode(1)}after${String.fromCharCode(31)}end`;+ expect(toFilesystemFriendlyName(control)).toBe("beforeafterend");});it("leaves a half-typed name alone, since it runs on every keystroke", () => {@@ -31,11 +35,23 @@ describe("what a file name may hold", () => {expect(tidyFilesystemFriendlyName("My backup...")).toBe("My backup");expect(tidyFilesystemFriendlyName("mixed. . ")).toBe("mixed");});++ it("stays linear on a long run of the characters it strips", () => {+ // These names arrive over a request, so their length is the caller's to choose. Matching+ // the trailing run with `/[. ]+$/` is quadratic on exactly this shape — a long run that+ // does not reach the end — and took 31 seconds on this input where the scan takes none.+ const name = `${". ".repeat(100_000)}x`;++ const started = Date.now();+ expect(tidyFilesystemFriendlyName(name)).toBe(name);+ expect(Date.now() - started).toBeLessThan(1000);+ });});describe("asFileName", () => {it("hands back the tidied name where there is one to be had", () => {- expect(asFileName(" Trilium data (2026-08-08 16-30-32) ")).toBe("Trilium data (2026-08-08 16-30-32)");+ expect(asFileName(" Trilium data (2026-08-08 16-30-32) "))+ .toBe("Trilium data (2026-08-08 16-30-32)");});it("refuses a name that nothing usable is left of", () => {@@ -53,7 +69,7 @@ describe("asFileName", () => {expect(asFileName("nullify")).toBe("nullify");});- it("never answers with something that reaches outside the directory it is resolved against", () => {+ it("never answers with something that reaches outside the directory it is used in", () => {// A name arriving over a request is not a name any field has vetted, and the side that// opens the file resolves it against a directory of its own choosing.expect(asFileName("../../etc/passwd")).toBe("....etcpasswd");diff --git a/packages/commons/src/lib/filesystem_name.ts b/packages/commons/src/lib/filesystem_name.tsindex 4f5a349d2a..16469b4978 100644--- a/packages/commons/src/lib/filesystem_name.ts+++ b/packages/commons/src/lib/filesystem_name.ts@@ -19,9 +19,6 @@*/export const FORBIDDEN_FILE_NAME_CHARACTERS = '<>:"/\\|?*';-/** Trailing dots and spaces are legal to type and impossible to save on Windows. */-const FORBIDDEN_AT_THE_END = /[. ]+$/;-/** Reserved device names on Windows, which cannot be a file name there whatever the extension. */const RESERVED_FILE_NAMES = /^(con|prn|aux|nul|com[1-9]|lpt[1-9])$/i;@@ -38,9 +35,20 @@ export function toFilesystemFriendlyName(value: string): string {/*** The same, plus what can only be settled once the name is finished: surrounding whitespace and a* trailing dot, which are legal mid-edit and refused at save time.+ *+ * The trailing run is walked off by hand rather than matched. The obvious `/[. ]+$/` is quadratic+ * on a name that is one long run of dots and does not end in one, and this runs on a name arriving+ * over a request, where the length is the caller's to choose.*/export function tidyFilesystemFriendlyName(value: string): string {- return toFilesystemFriendlyName(value).trim().replace(FORBIDDEN_AT_THE_END, "");+ const name = toFilesystemFriendlyName(value).trim();++ let end = name.length;+ while (end > 0 && isForbiddenAtTheEnd(name[end - 1])) {+ end--;+ }++ return name.slice(0, end);}/**@@ -62,5 +70,14 @@ export function asFileName(value: string): string | null {/** Whether a single character is one no file name may contain. */function isForbidden(character: string): boolean {return FORBIDDEN_FILE_NAME_CHARACTERS.includes(character)- || (character.codePointAt(0) ?? 0) < 0x20;+ // `charCodeAt` rather than `codePointAt`, which is typed as possibly absent and would+ // leave a "no character there" case that this cannot reach and so cannot be tested. A+ // character outside the basic plane reports the first half of its pair, far above the+ // control characters this is looking for, which is the same answer either way.+ || character.charCodeAt(0) < 0x20;+}++/** Whether a character is one no name may end on: legal to type, impossible to save on Windows. */+function isForbiddenAtTheEnd(character: string): boolean {+ return character === "." || character === " ";}diff --git a/packages/commons/src/lib/setup_marker.spec.ts b/packages/commons/src/lib/setup_marker.spec.tsnew file mode 100644index 0000000000..d51612a57a--- /dev/null+++ b/packages/commons/src/lib/setup_marker.spec.ts@@ -0,0 +1,13 @@+import { describe, expect, it } from "vitest";++import { SETUP_MARKER_FILE_NAME } from "./setup_marker.js";++describe("the setup marker", () => {+ it("is named the same for every build that has to find it", () => {+ // One build writes this file and the next start reads it, and those two need not be the+ // same version: renaming it strands the marker an older instance left behind, and the+ // wizard the user asked for silently never opens. It is a name on disk, not an internal+ // one, so it is pinned here rather than left to whoever edits the module next.+ expect(SETUP_MARKER_FILE_NAME).toBe("setup.json");+ });+});