Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Fix query duplication #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ module.exports.retries = retries;
function retry (retries) {

var self = this
, oldPath = this.url
, timeout = this._timeout
, oldEnd = this.end;

retries = retries || 1;

this.end = function (fn) {
var timeout = this._timeout;

function attemptRetry () {
return oldEnd.call(self, function (err, res) {
if (!retries || !shouldRetry(err, res)) return fn && fn(err, res);

reset(self, timeout);
reset(self, oldPath, timeout);

retries--;
return attemptRetry();
Expand All @@ -56,23 +56,29 @@ function retry (retries) {
* HACK: Resets the internal state of a request.
*/

function reset (request, timeout) {
var headers = request.req._headers;
var path = request.req.path;
function reset (request, url, timeout) {
if (request.req) {
var headers = request.req._headers;
var path = request.url;

request.req.abort();
request.called = false;
request.timeout(timeout);
delete request.req;
delete request._timer;
request.req.abort();
request.called = false;
delete request.req;

for (var k in headers) {
request.set(k, headers[k]);
}
for (var k in headers) {
request.set(k, headers[k]);
}

if (!request.qs) {
request.req.path = path;
if (!request.qs) {
request.req.path = path;
}
} else {
request.abort();
request.clearTimeout();
}

delete request._timer;
request.timeout(timeout);
}


Expand Down