From 2b9bb0c6c1ac8fc4be2f5337ad25a1156cb0bae2 Mon Sep 17 00:00:00 2001 From: Alba Mendez Date: Sun, 31 Mar 2019 20:37:49 +0200 Subject: [PATCH 1/3] Little refactor - Initialize options at the beginning of init - Extract the code that resolves / rejects the promise into separate functions --- lib/plumbing.js | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/lib/plumbing.js b/lib/plumbing.js index 979a0f2..712ffcc 100644 --- a/lib/plumbing.js +++ b/lib/plumbing.js @@ -33,6 +33,11 @@ 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_promise = new PromiseImpl(function (resolve, reject) { self._rp_resolve = resolve; self._rp_reject = reject; @@ -52,11 +57,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,13 +80,33 @@ 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); + } + + if (origCallbackThrewException) { + throw thrownException; + } + + }; + + plumbing.handleError = function (err, response) { + + var self = this; + + self._rp_reject(new errors.RequestError(err, self._rp_options, response)); + + }; + + plumbing.handleResponse = function (response, body) { + + var self = this; - self._rp_reject(new errors.RequestError(err, self._rp_options, response)); + var is2xx = /^2/.test('' + response.statusCode); - } else if (self._rp_options.simple && !is2xx) { + if (self._rp_options.simple && !is2xx) { if (isFunction(self._rp_options.transform) && self._rp_options.transform2xxOnly === false) { @@ -126,10 +146,6 @@ module.exports = function (options) { } - if (origCallbackThrewException) { - throw thrownException; - } - }; plumbing.exposePromiseMethod = function (exposeTo, bindTo, promisePropertyKey, methodToExpose, exposeAs) { From d0f8b13d7ce4edc521a13d5127daf31e064e54ce Mon Sep 17 00:00:00 2001 From: Alba Mendez Date: Sun, 31 Mar 2019 23:32:35 +0200 Subject: [PATCH 2/3] Implement `pause` option --- lib/plumbing.js | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/lib/plumbing.js b/lib/plumbing.js index 712ffcc..89c506b 100644 --- a/lib/plumbing.js +++ b/lib/plumbing.js @@ -37,6 +37,7 @@ module.exports = function (options) { 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; @@ -46,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(); @@ -95,6 +101,7 @@ module.exports = function (options) { plumbing.handleError = function (err, response) { var self = this; + plumbing.removeListeners.call(self); self._rp_reject(new errors.RequestError(err, self._rp_options, response)); @@ -103,12 +110,13 @@ module.exports = function (options) { 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 (isFunction(self._rp_options.transform) && self._rp_options.transform2xxOnly === false) { + 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 @@ -124,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)) { @@ -148,6 +161,14 @@ module.exports = function (options) { }; + 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) { exposeAs = exposeAs || methodToExpose; From dab29c7f6c3d65a50ad3f79eb44dfa5c1bed9187 Mon Sep 17 00:00:00 2001 From: Alba Mendez Date: Mon, 1 Apr 2019 00:41:37 +0200 Subject: [PATCH 3/3] Fix tests --- lib/plumbing.js | 8 ++++---- test/spec/plumbing.js | 9 ++++++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/plumbing.js b/lib/plumbing.js index 89c506b..182d8b9 100644 --- a/lib/plumbing.js +++ b/lib/plumbing.js @@ -48,8 +48,8 @@ module.exports = function (options) { }); if (self._rp_options.pause) { - self.on("error", plumbing.handleError); - self.on("response", plumbing.handleResponse); + 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) { @@ -164,8 +164,8 @@ module.exports = function (options) { plumbing.removeListeners = function () { var self = this; if (self._rp_options.pause) { - self.removeListener("error", plumbing.handleError); - self.removeListener("response", plumbing.handleResponse); + self.removeListener('error', plumbing.handleError); + self.removeListener('response', plumbing.handleResponse); } }; 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 }); });