@cloudant/couchbackup
Advanced tools
Comparing version 2.11.1-SNAPSHOT-263 to 2.11.1-SNAPSHOT-264
@@ -19,3 +19,2 @@ // Copyright © 2017, 2021 IBM Corp. All rights reserved. | ||
const { IamAuthenticator, NoAuthAuthenticator } = require('ibm-cloud-sdk-core'); | ||
const retryPlugin = require('retry-axios'); | ||
const debug = require('debug')('couchbackup:request'); | ||
@@ -29,3 +28,3 @@ | ||
// after the ugprade to @ibm-cloud/cloudant | ||
const errorHelper = async function(err) { | ||
function errorHelper(err) { | ||
debug('Entering error helper interceptor'); | ||
@@ -80,4 +79,15 @@ let method; | ||
return Promise.reject(err); | ||
}; | ||
} | ||
// Interceptor function to add the User-Agent header. | ||
// An interceptor is used because setting UA in headers | ||
// option during client initialization means it gets overwritten | ||
// by the default value during a request. | ||
// This interceptor is further along the chain and able to | ||
// replace the default value. | ||
function userAgentHelper(requestConfig) { | ||
requestConfig.headers['User-Agent'] = userAgent; | ||
return requestConfig; | ||
} | ||
function newSimpleClient(rawUrl, opts) { | ||
@@ -126,39 +136,19 @@ const url = new URL(rawUrl); | ||
const authenticator = service.getAuthenticator(); | ||
// Configure retries | ||
const maxRetries = 2; // for 3 total attempts | ||
service.getHttpClient().defaults.raxConfig = { | ||
// retries for status codes | ||
retry: maxRetries, | ||
// retries for non-response e.g. ETIMEDOUT | ||
noResponseRetries: maxRetries, | ||
backoffType: 'exponential', | ||
httpMethodsToRetry: ['GET', 'HEAD', 'POST'], | ||
statusCodesToRetry: [ | ||
[429, 429], | ||
[500, 599] | ||
], | ||
shouldRetry: err => { | ||
const cfg = retryPlugin.getConfig(err); | ||
// cap at max retries regardless of response/non-response type | ||
if (cfg.currentRetryAttempt >= maxRetries) { | ||
return false; | ||
} else { | ||
return retryPlugin.shouldRetryRequest(err); | ||
} | ||
}, | ||
instance: service.getHttpClient() | ||
}; | ||
retryPlugin.attach(service.getHttpClient()); | ||
// Add interceptors | ||
// Request interceptor to set the User-Agent header | ||
// Response interceptor to put URLs in error messages | ||
// Add for the token manager if present | ||
if (authenticator.tokenManager && authenticator.tokenManager.requestWrapperInstance) { | ||
authenticator.tokenManager.requestWrapperInstance.axiosInstance.interceptors.request.use(userAgentHelper, null); | ||
authenticator.tokenManager.requestWrapperInstance.axiosInstance.interceptors.response.use(null, errorHelper); | ||
} | ||
// Add error interceptors to put URLs in error messages | ||
// and add for the client | ||
service.getHttpClient().interceptors.request.use(userAgentHelper, null); | ||
service.getHttpClient().interceptors.response.use(null, errorHelper); | ||
// Add request interceptor to add user-agent (adding it with custom request headers gets overwritten) | ||
service.getHttpClient().interceptors.request.use(function(requestConfig) { | ||
requestConfig.headers['User-Agent'] = userAgent; | ||
return requestConfig; | ||
}, null); | ||
// Configure retries | ||
// Note: this MUST happen last after all other interceptors have been registered | ||
const maxRetries = 2; // for 3 total attempts | ||
service.enableRetries({ maxRetries }); | ||
@@ -165,0 +155,0 @@ return { service, dbName, url: actUrl.toString() }; |
{ | ||
"name": "@cloudant/couchbackup", | ||
"version": "2.11.1-SNAPSHOT-263", | ||
"version": "2.11.1-SNAPSHOT-264", | ||
"description": "CouchBackup - command-line backup utility for Cloudant/CouchDB", | ||
@@ -32,3 +32,2 @@ "homepage": "https://github.com/IBM/couchbackup", | ||
"ibm-cloud-sdk-core": "^5.0.2", | ||
"retry-axios": "^2.6.0", | ||
"axios": "^1.7.4" | ||
@@ -35,0 +34,0 @@ }, |
5
128214
2439