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
fix(pdf): stop an uncaught TypeError when adding a signature
Elian Doran1fac210f7153
diff --git a/packages/pdfjs-viewer/src/bootstrap.spec.ts b/packages/pdfjs-viewer/src/bootstrap.spec.tsindex c6c3a7490d..0a825bd89b 100644--- a/packages/pdfjs-viewer/src/bootstrap.spec.ts+++ b/packages/pdfjs-viewer/src/bootstrap.spec.ts@@ -253,6 +253,31 @@ describe("reporting the document as modified", () => {expect(viewer.messagesOfType("pdfjs-viewer-document-modified")).toHaveLength(1);});+ it("survives an editor pdf.js has stored before finishing it", async () => {+ viewer = await installViewerApp(allFeaturesPdf());+ const storage = viewer.pdfDocument.annotationStorage as any;+ manageSave();++ // Pressing "Add signature" adds a SignatureEditor to annotationStorage — its own+ // isEmpty() returns false unconditionally — while its outlines are still null, and only+ // fills them in when the signature dialog resolves. Serializing the document in between+ // throws out of the storage hook, from where it reaches the console uncaught.+ Object.defineProperty(storage, "serializable", {+ configurable: true,+ get() {+ throw new TypeError("can't access property \"serialize\", this[#drawOutlines] is null");+ }+ });+ expect(() => storage.onSetModified()).not.toThrow();+ expect(viewer.messagesOfType("pdfjs-viewer-document-modified")).toHaveLength(0);++ // The hash the failed read fell back to has to be the one from before, or the finished+ // signature would look unchanged and never be saved.+ delete storage.serializable;+ storage.setValue("field-1", { value: "typed" });+ expect(viewer.messagesOfType("pdfjs-viewer-document-modified")).toHaveLength(1);+ });+it("stays quiet when the editing state changes with nothing to undo", async () => {viewer = await installViewerApp(allFeaturesPdf());manageSave();diff --git a/packages/pdfjs-viewer/src/bootstrap.ts b/packages/pdfjs-viewer/src/bootstrap.tsindex 46f3dda6ff..ae57d90996 100644--- a/packages/pdfjs-viewer/src/bootstrap.ts+++ b/packages/pdfjs-viewer/src/bootstrap.ts@@ -127,15 +127,25 @@ export function manageSave() {// What the document held when the parent was last told about it. Compared against, rather// than trusted, because pdf.js reports far more than actual changes — see reportIfChanged.- let reportedHash = serializedHash();+ let reportedHash = serializedHash() ?? "";/**- * A digest of everything `saveDocument()` would write out. pdf.js serializes an annotation it- * has not modified to nothing at all, so this stays put while the ones already stored in the- * document are registered, and moves as soon as one is genuinely edited.+ * A digest of everything `saveDocument()` would write out, or `null` while the document+ * cannot be serialized at all. pdf.js serializes an annotation it has not modified to nothing+ * at all, so this stays put while the ones already stored in the document are registered, and+ * moves as soon as one is genuinely edited.+ *+ * Unserializable is a state pdf.js passes through rather than an error: it stores an editor+ * before finishing it, so pressing "Add signature" leaves one with no outlines in+ * `annotationStorage` until the signature dialog resolves. Callers substitute the hash they+ * already hold, which counts the document as unchanged until the editor is complete.*/- function serializedHash(): string {- return (storage as any)?.serializable?.hash ?? "";+ function serializedHash(): string | null {+ try {+ return (storage as any)?.serializable?.hash ?? "";+ } catch {+ return null;+ }}/**@@ -146,7 +156,7 @@ export function manageSave() {*/function onChange() {if (!storage) return;- reportedHash = serializedHash();+ reportedHash = serializedHash() ?? reportedHash;storage.resetModified();announceModified();}@@ -171,7 +181,7 @@ export function manageSave() {// once per dirtying, so a flag left set would swallow the next real change.storage.resetModified();- const hash = serializedHash();+ const hash = serializedHash() ?? reportedHash;if (hash === reportedHash) return;reportedHash = hash;announceModified();