Skip to content

Instantly share code, notes, and snippets.

@rjcorwin
Last active February 10, 2022 14:32
Show Gist options
  • Save rjcorwin/ce28e0273b70b6a0f2b7f491a22ceed0 to your computer and use it in GitHub Desktop.
Save rjcorwin/ce28e0273b70b6a0f2b7f491a22ceed0 to your computer and use it in GitHub Desktop.

How PouchDB Determines that Changes were successfully pushed to CouchDB

  1. During a replication, after PouchDB determines which docs should be pushed up and their contents, replicate's writeDocs function is called which in turn calls to bulkDocs function.
  2. When the target database is a CouchDB, the bulkDocs function is aliased to the pouchdb-adapter-http's _bulkDocs function.
  3. _bulkDocs then passes the request to fetchJSON function which then passes it to ourFetch. If response.ok is not truthy, an error is thrown here. Note the requirement that response body is valid JSON here. I'm assuming if a proxy returned a 200 status message but returned a body that just said something like "hello", this would throw an error and we would not have success.
  4. The ourFetch function passes the request to fetchFun. fetchFun looks like it's probably the fetch method imported from the pouchdb-fetch package.
  5. pouchdb-fetch's fetch export comes from the node-fetch package. The node-fetch dependency in PouchDB is listed as being set to version 2.6.7 in package.json.
  6. The documentation for node-fetch 2.x.x is here. node-fetch advertises itself as a "window.fetch compatible API on Node.js runtime".

With all this considered, the criteria for successfully pushing up changes to CouchDB from PouchDB are as follows.

  1. The HTTP request returns as response.ok == true according to how window.fetch would determine a response as ok. (A truthy Response.ok is a response with status code of 2xx)
  2. The HTTP request returns a body that can be parsed using window.fetch's response.json() method.

The contents of the response are not inspected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment