diff --git a/lib/plumbing.js b/lib/plumbing.js index 979a0f2..182d8b9 100644 --- a/lib/plumbing.js +++ b/lib/plumbing.js @@ -33,6 +33,12 @@ module.exports = function (options) { var self = this; + self._rp_options = requestOptions; + self._rp_options.simple = requestOptions.simple !== false; + self._rp_options.resolveWithFullResponse = requestOptions.resolveWithFullResponse === true; + self._rp_options.transform2xxOnly = requestOptions.transform2xxOnly === true; + self._rp_options.pause = requestOptions.pause === true; + self._rp_promise = new PromiseImpl(function (resolve, reject) { self._rp_resolve = resolve; self._rp_reject = reject; @@ -41,10 +47,15 @@ module.exports = function (options) { } }); - self._rp_callbackOrig = requestOptions.callback; - requestOptions.callback = self.callback = function RP$callback(err, response, body) { - plumbing.callback.call(self, err, response, body); - }; + if (self._rp_options.pause) { + self.on('error', plumbing.handleError); + self.on('response', plumbing.handleResponse); + } else { + self._rp_callbackOrig = requestOptions.callback; + requestOptions.callback = self.callback = function RP$callback(err, response, body) { + plumbing.callback.call(self, err, response, body); + }; + } if (isString(requestOptions.method)) { requestOptions.method = requestOptions.method.toUpperCase(); @@ -52,11 +63,6 @@ module.exports = function (options) { requestOptions.transform = requestOptions.transform || plumbing.defaultTransformations[requestOptions.method]; - self._rp_options = requestOptions; - self._rp_options.simple = requestOptions.simple !== false; - self._rp_options.resolveWithFullResponse = requestOptions.resolveWithFullResponse === true; - self._rp_options.transform2xxOnly = requestOptions.transform2xxOnly === true; - }; plumbing.defaultTransformations = { @@ -80,15 +86,37 @@ module.exports = function (options) { } } - var is2xx = !err && /^2/.test('' + response.statusCode); - if (err) { + plumbing.handleError.call(self, err, response); + } else { + plumbing.handleResponse.call(self, response, body); + } - self._rp_reject(new errors.RequestError(err, self._rp_options, response)); + if (origCallbackThrewException) { + throw thrownException; + } - } else if (self._rp_options.simple && !is2xx) { + }; - if (isFunction(self._rp_options.transform) && self._rp_options.transform2xxOnly === false) { + plumbing.handleError = function (err, response) { + + var self = this; + plumbing.removeListeners.call(self); + + self._rp_reject(new errors.RequestError(err, self._rp_options, response)); + + }; + + plumbing.handleResponse = function (response, body) { + + var self = this; + plumbing.removeListeners.call(self); + + var is2xx = /^2/.test('' + response.statusCode); + + if (self._rp_options.simple && !is2xx) { + + if (!self._rp_options.pause && isFunction(self._rp_options.transform) && self._rp_options.transform2xxOnly === false) { (new PromiseImpl(function (resolve) { resolve(self._rp_options.transform(body, response, self._rp_options.resolveWithFullResponse)); // transform may return a Promise @@ -104,6 +132,11 @@ module.exports = function (options) { self._rp_reject(new errors.StatusCodeError(response.statusCode, body, self._rp_options, response)); } + } else if (self._rp_options.pause) { + + response.pause(); + self._rp_resolve(response); + } else { if (isFunction(self._rp_options.transform) && (is2xx || self._rp_options.transform2xxOnly === false)) { @@ -126,10 +159,14 @@ module.exports = function (options) { } - if (origCallbackThrewException) { - throw thrownException; - } + }; + plumbing.removeListeners = function () { + var self = this; + if (self._rp_options.pause) { + self.removeListener('error', plumbing.handleError); + self.removeListener('response', plumbing.handleResponse); + } }; plumbing.exposePromiseMethod = function (exposeTo, bindTo, promisePropertyKey, methodToExpose, exposeAs) { diff --git a/test/spec/plumbing.js b/test/spec/plumbing.js index d6f9e7e..f757d06 100644 --- a/test/spec/plumbing.js +++ b/test/spec/plumbing.js @@ -141,7 +141,8 @@ describe('Promise-Core\'s Plumbing', function () { simple: true, resolveWithFullResponse: false, transform: undefined, - transform2xxOnly: false + transform2xxOnly: false, + pause: false }); }); @@ -160,7 +161,8 @@ describe('Promise-Core\'s Plumbing', function () { simple: true, resolveWithFullResponse: false, transform: undefined, - transform2xxOnly: false + transform2xxOnly: false, + pause: false }); }); @@ -183,7 +185,8 @@ describe('Promise-Core\'s Plumbing', function () { simple: false, resolveWithFullResponse: true, transform: customTransform, - transform2xxOnly: true + transform2xxOnly: true, + pause: false }); });