From deba62e670aef72f1068c55bf9070ce64ecf5ef4 Mon Sep 17 00:00:00 2001 From: Shannon Wynter Date: Mon, 17 Nov 2014 15:28:55 +1000 Subject: [PATCH] Fixes #3 Redirects break write --- lib/client.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/client.js b/lib/client.js index d66ca96..ba0fd07 100644 --- a/lib/client.js +++ b/lib/client.js @@ -50,6 +50,30 @@ Client.prototype.configure = function (settings) { return this; }; +/** +* Internal method to wrap the generate a wrapped callback +* +* Fixes issues with etcd nodes redirecting +* +* @param {Object} options +* @param {Function} cb +* @return {Function} +* @private +* +*/ +Client.prototype._wrap = function(options, cb) { + retries = 0; + wrapped = function (err, resp, body) { + if (!body && resp.headers.location && retries < 3) { + retries ++; + options.uri = resp.headers.location; + request(resp.headers.location, options, wrapped); + return; + } + cb.apply(this, arguments); + }; + return wrapped; +} /** * Internal method for calling the server. @@ -74,7 +98,7 @@ Client.prototype._call = function (options, callback) { if (this.agent) { options.agent = this.agent } - request(url, options, cb) + request(url, options, this._wrap(options, cb)) return this; };