Skip to content

Commit

Permalink
Merge pull request #125 from emmansun/master
Browse files Browse the repository at this point in the history
Avoid agent clone
  • Loading branch information
FGRibreau authored Nov 11, 2020
2 parents a6e72dd + f8cac13 commit bb2f12b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ function defaultPromiseFactory(resolver) {
return when.promise(resolver);
}

function _cloneOptions(options) {
const cloned = {};
for (let key in options) {
if (options.hasOwnProperty(key)) {
cloned[key] = key === 'agent' ? options[key] : _.cloneDeep(options[key]);
}
}
return cloned;
}

/**
* It calls the promiseFactory function passing it the resolver for the promise
*
Expand Down Expand Up @@ -130,7 +140,7 @@ Request.prototype._tryUntilFail = function () {
err.attempts = this.attempts;
}

var mustRetry = await Promise.resolve(this.retryStrategy(err, response, body, _.cloneDeep(this.options)));
var mustRetry = await Promise.resolve(this.retryStrategy(err, response, body, _cloneOptions(this.options)));
if (_.isObject(mustRetry) && _.has(mustRetry, 'mustRetry')) {
if (_.isObject(mustRetry.options)) {
this.options = mustRetry.options; //if retryStrategy supposes different request options for retry
Expand Down
58 changes: 58 additions & 0 deletions test/clone.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict';

var request = require('../');
var t = require('chai').assert;
const http = require('http');

describe('Clone option function', function () {
it('should not clone `options.agent`', function (done) {
const agent = new http.Agent({ keepAlive: true });
const strategy = function (err, response, body, options) {
options.url = 'http://www.filltext.com/?rows=1&err=200'; //overwrite url to return 200
t.strictEqual(agent, options.agent);
return {
mustRetry: true,
options: options,
};
};

request({
url: 'http://www.filltext.com/?rows=1&err=500', // returns a 500 status
maxAttempts: 3,
agent: agent,
retryDelay: 100,
retryStrategy: strategy
}, function (err, response, body) {
if (err) done(err);
t.strictEqual(200, response.statusCode);
done();
});
});

it('should not clone `non-own` property', function (done) {
const originOptions = Object.create({ parent: true });
originOptions.url = 'http://www.filltext.com/?rows=1&err=500'; // returns a 500 status
originOptions.maxAttempts = 3;
originOptions.retryDelay = 100;
originOptions.cloneable = { a: 1 };

const strategy = function (err, response, body, options) {
t.isUndefined(options.parent);
t.notStrictEqual(originOptions.cloneable, options.cloneable);
t.equal(originOptions.cloneable.a, options.cloneable.a);
options.url = 'http://www.filltext.com/?rows=1&err=200'; //overwrite url to return 200
return {
mustRetry: true,
options: options,
};
};

originOptions.retryStrategy = strategy;

request(originOptions, function (err, response, body) {
if (err) done(err);
t.strictEqual(200, response.statusCode);
done();
});
});
});

0 comments on commit bb2f12b

Please sign in to comment.