Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(notes): preserve editing when browser storage is unavailable

dajiaohuangd982cfe1bf07
diff --git a/web/lib/chat-markdown-note.ts b/web/lib/chat-markdown-note.ts
index d651a631c..51858628f 100644
--- a/web/lib/chat-markdown-note.ts
+++ b/web/lib/chat-markdown-note.ts
@@ -70,43 +70,46 @@ function parseDraft(raw: string | null): ChatMarkdownNoteDraft | null {
export function loadChatMarkdownNoteDraft(
ownerId: string,
sessionId: string | null,
- storage: ChatMarkdownNoteStorage | null = typeof window === "undefined"
- ? null
- : window.localStorage,
+ storage?: ChatMarkdownNoteStorage | null,
): ChatMarkdownNoteDraft {
- if (!storage) return EMPTY_CHAT_MARKDOWN_NOTE_DRAFT;
-
- const key = storageKey(ownerId, sessionId);
- const draft = parseDraft(storage.getItem(key));
- if (!sessionId) return draft ?? EMPTY_CHAT_MARKDOWN_NOTE_DRAFT;
-
- // A note started before a new chat receives its session id is adopted by
- // that session instead of leaking into the next new chat.
- const pending = parseDraft(storage.getItem(storageKey(ownerId, null)));
- if (!draft && pending) {
- try {
- storage.setItem(key, JSON.stringify(pending));
- storage.removeItem(storageKey(ownerId, null));
- } catch {
- // If adoption cannot be persisted, return the pending draft so this
- // session still gets the content in memory.
+ try {
+ const noteStorage = storage === undefined
+ ? typeof window === "undefined" ? null : window.localStorage
+ : storage;
+ if (!noteStorage) return EMPTY_CHAT_MARKDOWN_NOTE_DRAFT;
+
+ const key = storageKey(ownerId, sessionId);
+ const draft = parseDraft(noteStorage.getItem(key));
+ if (draft || !sessionId) return draft ?? EMPTY_CHAT_MARKDOWN_NOTE_DRAFT;
+
+ // Adopt a note started before the server assigned this chat its ID.
+ const pending = parseDraft(noteStorage.getItem(storageKey(ownerId, null)));
+ if (pending) {
+ try {
+ noteStorage.setItem(key, JSON.stringify(pending));
+ noteStorage.removeItem(storageKey(ownerId, null));
+ } catch {
+ // Keep the pending content in memory if adoption cannot be persisted.
+ }
+ return pending;
}
- return pending;
+ } catch {
+ // Browser policy may deny the storage getter or reads. Editing still works.
}
- return draft ?? EMPTY_CHAT_MARKDOWN_NOTE_DRAFT;
+ return EMPTY_CHAT_MARKDOWN_NOTE_DRAFT;
}
export function saveChatMarkdownNoteDraft(
ownerId: string,
sessionId: string | null,
draft: ChatMarkdownNoteDraft,
- storage: ChatMarkdownNoteStorage | null = typeof window === "undefined"
- ? null
- : window.localStorage,
+ storage?: ChatMarkdownNoteStorage | null,
): void {
- if (!storage) return;
try {
- storage.setItem(storageKey(ownerId, sessionId), JSON.stringify(draft));
+ const noteStorage = storage === undefined
+ ? typeof window === "undefined" ? null : window.localStorage
+ : storage;
+ noteStorage?.setItem(storageKey(ownerId, sessionId), JSON.stringify(draft));
} catch {
// Storage can be unavailable or full. The in-memory draft remains usable.
}
diff --git a/web/tests/chat-markdown-note.test.ts b/web/tests/chat-markdown-note.test.ts
index 89d933a9e..78cafc1b7 100644
--- a/web/tests/chat-markdown-note.test.ts
+++ b/web/tests/chat-markdown-note.test.ts
@@ -246,3 +246,41 @@ function createStorage(
},
};
}
+
+for (const operation of ["load", "save"] as const) {
+ test(`markdown-note ${operation} tolerates browser storage denied for an opaque origin`, () => {
+ const { JSDOM } = require("jsdom");
+ const dom = new JSDOM();
+ const original = Object.getOwnPropertyDescriptor(globalThis, "window");
+ Object.defineProperty(globalThis, "window", { configurable: true, value: dom.window });
+ try {
+ assert.throws(() => dom.window.localStorage, { name: "SecurityError" });
+ if (operation === "load") {
+ assert.deepEqual(loadChatMarkdownNoteDraft("local", "session-1"), {
+ title: "", content: "", saved: null,
+ });
+ } else {
+ assert.doesNotThrow(() => saveChatMarkdownNoteDraft(
+ "local", "session-1", { title: "Note", content: "# Keep editing", saved: null },
+ ));
+ }
+ } finally {
+ if (original) Object.defineProperty(globalThis, "window", original);
+ else Reflect.deleteProperty(globalThis, "window");
+ dom.window.close();
+ }
+ });
+}
+
+test("markdown-note loading tolerates a denied read without deleting the draft", () => {
+ let writes = 0;
+ const denied = {
+ getItem() { throw new DOMException("Storage access denied", "SecurityError"); },
+ setItem() { writes++; },
+ removeItem() { writes++; },
+ };
+ assert.deepEqual(loadChatMarkdownNoteDraft("local", "session-1", denied), {
+ title: "", content: "", saved: null,
+ });
+ assert.equal(writes, 0);
+});