This is an automated email from the ASF dual-hosted git repository.

jan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pouchdb.git

commit a22960a7aec890372b5f9dc09057f86035515777
Author: ems <[email protected]>
AuthorDate: Thu Jan 22 00:41:10 2026 +0100

    rewrite methods using async/await and extract initCheckpointDoc helper fn
---
 .../node_modules/pouchdb-checkpointer/src/index.js | 200 +++++++++++----------
 1 file changed, 102 insertions(+), 98 deletions(-)

diff --git a/packages/node_modules/pouchdb-checkpointer/src/index.js 
b/packages/node_modules/pouchdb-checkpointer/src/index.js
index e455d814a..74253c731 100644
--- a/packages/node_modules/pouchdb-checkpointer/src/index.js
+++ b/packages/node_modules/pouchdb-checkpointer/src/index.js
@@ -33,70 +33,50 @@ class CheckpointerInternal {
     }
   }
 
-  writeCheckpoint(checkpoint, session) {
-    const self = this;
-    return this._updateTarget(checkpoint, session).then(function () {
-      return self._updateSource(checkpoint, session);
-    });
+  async writeCheckpoint(checkpoint, session) {
+    // update target before source every time
+    // because otherwise, compareHistory will pick a too new seq from source
+    // after an error writing a checkpoint to the target
+    await this._updateTarget(checkpoint, session);
+    return this._updateSource(checkpoint, session);
   }
 
-  _updateTarget(checkpoint, session) {
+  async _updateTarget(checkpoint, session) {
     if (this.opts.writeTargetCheckpoint) {
-      return this._updateCheckpoint(this.target, this.id, checkpoint,
+      return await this._updateCheckpoint(this.target, this.id, checkpoint,
         session, this.returnValue);
-    } else {
-      return Promise.resolve(true);
-    }
+    } return true;
   }
 
-  _updateSource(checkpoint, session) {
+  async _updateSource(checkpoint, session) {
     if (this.opts.writeSourceCheckpoint) {
-      const self = this;
-      return this._updateCheckpoint(this.src, this.id, checkpoint,
-        session, this.returnValue)
-        .catch(function (err) {
-          if (isForbiddenError(err)) {
-            self.opts.writeSourceCheckpoint = false;
-            return true;
-          }
-          throw err;
-        });
-    } else {
-      return Promise.resolve(true);
+      try {
+        return await this._updateCheckpoint(this.src, this.id, checkpoint,
+          session, this.returnValue);
+      } catch (err) {
+        if (isForbiddenError(err)) {
+          this.opts.writeSourceCheckpoint = false;
+          return true;
+        }
+        throw err;
+      }
     }
   }
 
-  _updateCheckpoint(db, id, checkpoint, session, returnValue) {
-    return db.get(id).catch(function (err) {
-      if (err.status === 404) {
-        if (db.adapter === 'http' || db.adapter === 'https') {
-          explainError(
-            404, 'PouchDB is just checking if a remote checkpoint exists.'
-          );
-        }
-        return {
-          session_id: session,
-          _id: id,
-          history: [],
-          replicator: REPLICATOR,
-          version: CHECKPOINT_VERSION
-        };
-      }
-      throw err;
-    }).then(function (doc) {
-      if (returnValue.cancelled) {
+  async _updateCheckpoint(db, id, checkpoint, session, returnValue) {
+    //retrieve checkpoint doc from db or create a new one
+    const doc = await this._initCheckpointDoc(db, id, session);
+
+    if (returnValue.cancelled) {
         return;
       }
-
       // if the checkpoint has not changed, do not update
       if (doc.last_seq === checkpoint) {
         return;
       }
 
       // Filter out current entry for this replication
-      doc.history = (doc.history || []).filter(function (item) {
-        return item.session_id !== session;
-      });
+      doc.history = (doc.history || []).filter(item => item.session_id !== 
session);
 
       // Add the latest checkpoint to history
       doc.history.unshift({
@@ -115,90 +95,114 @@ class CheckpointerInternal {
       doc.session_id = session;
       doc.last_seq = checkpoint;
 
-      return db.put(doc).catch(function (err) {
+      try {
+        return await db.put(doc);
+      } catch (err) {
         if (err.status === 409) {
           // retry; someone is trying to write a checkpoint simultaneously
           return this._updateCheckpoint(db, id, checkpoint, session, 
returnValue);
         }
-        throw err;
-      });
-    });
+          throw err;
+      }
   }
 
-  getCheckpoint() {
-    const self = this;
+  async _initCheckpointDoc(db, id, session) {
+    try {
+      return await db.get(id);
+    } catch (err) {
+      if (err.status === 404) {
+        if (db.adapter === 'http' || db.adapter === 'https') {
+          explainError(
+            404, 'PouchDB is just checking if a remote checkpoint exists.'
+          );
+        }
+        return  {
+          session_id: session,
+          _id: id,
+          history: [],
+          replicator: REPLICATOR,
+          version: CHECKPOINT_VERSION
+        };
+      } else {
+        throw err;
+      }
+    }
+  }
 
-    if (!self.opts.writeSourceCheckpoint && !self.opts.writeTargetCheckpoint) {
-      return Promise.resolve(LOWEST_SEQ);
+  async getCheckpoint() {
+    if (!this.opts.writeSourceCheckpoint && !this.opts.writeTargetCheckpoint) {
+      return LOWEST_SEQ;
     }
 
-    if (self.opts && self.opts.writeSourceCheckpoint && 
!self.opts.writeTargetCheckpoint) {
-      return self.src.get(self.id).then(function (sourceDoc) {
+    if (this.opts && this.opts.writeSourceCheckpoint && 
!this.opts.writeTargetCheckpoint) {
+      try {
+        const sourceDoc = await this.src.get(this.id);
         return sourceDoc.last_seq || LOWEST_SEQ;
-      }).catch(function (err) {
+      } catch (err) {
         /* istanbul ignore if */
         if (err.status !== 404) {
           throw err;
         }
         return LOWEST_SEQ;
-      });
-    }
-
-    return self.target.get(self.id).then(function (targetDoc) {
-      if (self.opts && self.opts.writeTargetCheckpoint && 
!self.opts.writeSourceCheckpoint) {
-        return targetDoc.last_seq || LOWEST_SEQ;
       }
+    }
 
-      return self.src.get(self.id).then(function (sourceDoc) {
-        // Since we can't migrate an old version doc to a new one
-        // (no session id), we just go with the lowest seq in this case
-        /* istanbul ignore if */
-        if (targetDoc.version !== sourceDoc.version) {
+    let targetDoc;
+    try {
+      targetDoc = await this.target.get(this.id);
+    } catch (err) {
+      if (err.status !== 404) {
+            throw err;
+          }
           return LOWEST_SEQ;
-        }
+    }
 
-        const version = targetDoc.version ? targetDoc.version.toString() : 
"undefined";
+    if (this.opts && this.opts.writeTargetCheckpoint && 
!this.opts.writeSourceCheckpoint) {
+      return targetDoc.last_seq || LOWEST_SEQ;
+    }
 
-        if (version in comparisons) {
-          return comparisons[version](targetDoc, sourceDoc);
-        }
-        /* istanbul ignore next */
+    try {
+      const sourceDoc = await this.src.get(this.id);
+      // Since we can't migrate an old version doc to a new one
+        // (no session id), we just go with the lowest seq in this case
+        /* istanbul ignore if */
+      if (targetDoc.version !== sourceDoc.version) {
         return LOWEST_SEQ;
-      }, function (err) {
-        if (err.status === 404 && targetDoc.last_seq) {
-          return self.src.put({
-            _id: self.id,
+      }
+      const version = targetDoc.version ? targetDoc.version.toString() : 
"undefined";
+
+      if (version in comparisons) {
+        return comparisons[version](targetDoc, sourceDoc);
+      }
+      /* istanbul ignore next */
+      return LOWEST_SEQ;
+    } catch (err) {
+      if (err.status === 404 && targetDoc.last_seq) {
+        try {
+          await this.src.put({
+            _id: this.id,
             last_seq: LOWEST_SEQ
-          }).then(function () {
-            return LOWEST_SEQ;
-          }, function (err) {
-            if (isForbiddenError(err)) {
-              self.opts.writeSourceCheckpoint = false;
-              return targetDoc.last_seq;
-            }
-            /* istanbul ignore next */
-            return LOWEST_SEQ;
           });
+          return LOWEST_SEQ;
+        } catch (err) {
+          if (isForbiddenError(err)) {
+            this.opts.writeSourceCheckpoint = false;
+            return targetDoc.last_seq;
+          }
+          /* istanbul ignore next */
+          return LOWEST_SEQ;
         }
-        throw err;
-      });
-    }).catch(function (err) {
-      if (err.status !== 404) {
-        throw err;
       }
-      return LOWEST_SEQ;
-    });
+      throw err;
+    }
   }
 }
 
 const comparisons = {
   "undefined": (targetDoc, sourceDoc) => {
     // This is the previous comparison function
-    if (collate(targetDoc.last_seq, sourceDoc.last_seq) === 0) {
-      return sourceDoc.last_seq;
-    }
     /* istanbul ignore next */
-    return 0;
+    return collate(targetDoc.last_seq, sourceDoc.last_seq) === 0 ? 
sourceDoc.last_seq : 0;
   },
   "1": (targetDoc, sourceDoc) => {
     // This is the comparison function ported from CouchDB

Reply via email to