add support to request rev via HEAD

This commit is contained in:
Don 2022-12-11 21:18:43 -07:00
parent 862ce00c62
commit df2d376c18
2 changed files with 49 additions and 35 deletions

View file

@ -95,6 +95,16 @@ Doc.prototype.get = function (dbName, docId, params) {
});
};
Doc.prototype.rev = function (dbName, docId, params) {
return this._slouch._req({
uri: this._slouch._url + '/' + encodeURIComponent(dbName) + '/' + encodeURIComponent(
docId),
method: 'HEAD',
qs: params,
parseBody: false
}).then(function (headers) { return headers.etag; });
};
Doc.prototype.getIgnoreMissing = function (dbName, id) {
var self = this;
return self.ignoreMissing(function () {

View file

@ -49,10 +49,10 @@ EnhancedRequest.MAX_RETRIES = 10;
// Preserve some compatibility with nano
EnhancedRequest.prototype._getStatusCode = function (body) {
switch (body.error) {
case 'conflict':
return 409;
case 'not_found':
return 404;
case 'conflict':
return 409;
case 'not_found':
return 404;
}
if (body.reason && body.reason.indexOf('Could not open source database') !== -1) {
@ -125,6 +125,10 @@ EnhancedRequest.prototype._request = function (opts) {
// Sometimes CouchDB just returns an malformed error
if (!response || !response.body) {
if (response && opts.method === 'HEAD') {
// or we only want the response headers
return response.headers;
}
err = new Error('malformed body');
err.error = 'malformed body';
self._log('Slouch Request:', {
@ -181,42 +185,42 @@ EnhancedRequest.prototype._throttledEnhancedRequest = function () {
EnhancedRequest.prototype._shouldReconnect = function (err) {
switch (err.message) {
case 'all_dbs_active': // No more connections
return true;
case 'all_dbs_active': // No more connections
return true;
default:
return new RegExp([
// Connection refused, e.g. because the CouchDB server is being restarted.
'ECONNREFUSED',
default:
return new RegExp([
// Connection refused, e.g. because the CouchDB server is being restarted.
'ECONNREFUSED',
'ENETUNREACH', // can occur when box sleeps/wakes-up
'ENETUNREACH', // can occur when box sleeps/wakes-up
// Occurs randomly when many simultaenous connections:
'emfile',
'socket hang up',
'ECONNRESET',
'ETIMEDOUT',
'function_clause',
'unknown_error',
'internal_server_error',
// Occurs randomly when many simultaenous connections:
'emfile',
'socket hang up',
'ECONNRESET',
'ETIMEDOUT',
'function_clause',
'unknown_error',
'internal_server_error',
'Failed to fetch', // ECONNREFUSED/ENOTFOUND in Chrome
'Type error', // ECONNREFUSED/ENOTFOUND in Safari
'XHR error', // ECONNREFUSED/ENOTFOUND in Firefox
'EAI_AGAIN' // Transient DNS error
'Failed to fetch', // ECONNREFUSED/ENOTFOUND in Chrome
'Type error', // ECONNREFUSED/ENOTFOUND in Safari
'XHR error', // ECONNREFUSED/ENOTFOUND in Firefox
'EAI_AGAIN' // Transient DNS error
// TODO: It is not clear why CouchDB responds with these timeout errors and immediately
// retrying the request often leads to deadlocks when you are continuously listening. In
// practice, it is better that your app throws a fatal error and then is restarted, e.g. via
// Docker, as this seems to reliably recover from this state. At some point we should analyze
// this better and determine where the bug lies and fix it at the root cause:
// https://github.com/redgeoff/spiegel/issues/100
//
// Occurs randomly even when there is a relatively small amount of data, e.g. "The request
// could not be processed in a reasonable amount of time"
//
// 'timeout'
].join('|'), 'i').test(err.message);
// TODO: It is not clear why CouchDB responds with these timeout errors and immediately
// retrying the request often leads to deadlocks when you are continuously listening. In
// practice, it is better that your app throws a fatal error and then is restarted, e.g. via
// Docker, as this seems to reliably recover from this state. At some point we should analyze
// this better and determine where the bug lies and fix it at the root cause:
// https://github.com/redgeoff/spiegel/issues/100
//
// Occurs randomly even when there is a relatively small amount of data, e.g. "The request
// could not be processed in a reasonable amount of time"
//
// 'timeout'
].join('|'), 'i').test(err.message);
}
};