From 7990068cf6c933f5dcdb64a7ecee5a3a5ef05721 Mon Sep 17 00:00:00 2001 From: Yacine Petitprez Date: Mon, 21 Mar 2016 15:55:57 +0800 Subject: [PATCH 1/6] Handling onprogress event (upload only) --- src/reqwest.js | 54 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/src/reqwest.js b/src/reqwest.js index fbc85e0..f193842 100644 --- a/src/reqwest.js +++ b/src/reqwest.js @@ -1,3 +1,9 @@ +/*! + * Reqwest! A general purpose XHR connection manager + * license MIT (c) Dustin Diaz 2015 + * https://github.com/ded/reqwest + */ + !function (name, context, definition) { if (typeof module != 'undefined' && module.exports) module.exports = definition() else if (typeof define == 'function' && define.amd) define(definition) @@ -6,7 +12,7 @@ var context = this - if ('document' in context) { + if ('window' in context) { var doc = document , byTag = 'getElementsByTagName' , head = doc[byTag]('head')[0] @@ -19,6 +25,7 @@ } } + var httpsRe = /^http/ , protocolRe = /(^\w+):\/\// , twoHundo = /^(20\d|1223)$/ //http://stackoverflow.com/questions/10046972/msie-returns-status-code-of-1223-for-ajax-request @@ -58,11 +65,6 @@ if (xhr && 'withCredentials' in xhr) { return xhr } else if (context[xDomainRequest]) { - var protocolRegExp = /^https?/; - if (window.location.href.match(protocolRegExp)[0] !== o.url.match(protocolRegExp)[0]) { - throw new Error('XDomainRequest: requests must be targeted to the same scheme as the hosting page.') - // As per: http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx - } return new XDomainRequest() } else { throw new Error('Browser does not support cross-origin requests') @@ -192,7 +194,7 @@ } } - function getRequest(fn, err) { + function getRequest(fn, err, progress) { var o = this.o , method = (o['method'] || 'GET').toUpperCase() , url = typeof o === 'string' ? o : o['url'] @@ -224,10 +226,12 @@ http.onerror = err // NOTE: see // http://social.msdn.microsoft.com/Forums/en-US/iewebdevelopment/thread/30ef3add-767c-4436-b8a9-f1ca19b4812e - http.onprogress = function() {} + http.onprogress = (progress || function(){}) sendWait = true } else { http.onreadystatechange = handleReadyState(this, fn, err) + + http.upload && (http.upload.onprogress = progress) } o['before'] && o['before'](http) if (sendWait) { @@ -267,6 +271,8 @@ // success handlers this._successHandler = function(){} this._fulfillmentHandlers = [] + // progress handlers + this._progressHandlers = [] // error handlers this._errorHandlers = [] // complete (both success and fail) handlers @@ -302,6 +308,13 @@ }) } + if (o['progress']) { + this._progressHandlers.push(function () { + o['progress'].apply(o, arguments) + }) + } + + function complete (resp) { o['timeout'] && clearTimeout(self.timeout) self.timeout = null @@ -375,7 +388,24 @@ complete(resp) } - this.request = getRequest.call(this, success, error) + function progress(evt) { + var percentCompleted; + + if (evt.lengthComputable) { + percentCompleted = evt.loaded / evt.total; + + } else { + //Length is not computable, we trigger the event with percentCompleted==0 + percentCompleted = 0; + } + + + for(var i = 0; i < self._progressHandlers.length; i++) { + self._progressHandlers[i](evt, percentCompleted); + } + } + + this.request = getRequest.call(this, success, error, progress) } Reqwest.prototype = { @@ -436,6 +466,10 @@ , 'catch': function (fn) { return this.fail(fn) } + , progress: function(fn) { + this._progressHandlers.push(fn) + return this; + } } function reqwest(o, fn) { @@ -625,4 +659,4 @@ } return reqwest -}); +}); \ No newline at end of file From 6021a15cdf6e02ea5cd1ea8fe867bbcf31b3b775 Mon Sep 17 00:00:00 2001 From: Yacine Petitprez Date: Mon, 21 Mar 2016 16:03:25 +0800 Subject: [PATCH 2/6] Fix readme adding progress doc --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index ca75f9e..72362e6 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,21 @@ reqwest({ }) ``` +``` js +reqwest({ + url: 'path/to/upload/large/file' + , method: 'POST' + , data: { image: 'somelargebase64encoding' } +}) + .progress(function(evt, progressPercent){ + console.log("Uploaded at " + (progressPercent*100) + "%" ) + }) + .then(function(){ + console.log("Upload completed") + }) +``` + + ``` js var r = reqwest({ url: 'path/to/data.jsonp?foo=bar' @@ -152,6 +167,7 @@ var r = reqwest({ }) ``` + ## Options * `url` a fully qualified uri @@ -165,6 +181,7 @@ var r = reqwest({ * `error` A function called when the request fails. * `complete` A function called whether the request is a success or failure. Always called when complete. * `jsonpCallback` Specify the callback function name for a `JSONP` request. This value will be used instead of the random (but recommended) name automatically generated by reqwest. + * `progress` A function called during upload, to handle the progression of the request. Usable also as promise-like method `progress` ## Security From 823eb93541c0b1398a824b411423cc4d460a23ac Mon Sep 17 00:00:00 2001 From: Yacine Petitprez Date: Mon, 21 Mar 2016 16:06:12 +0800 Subject: [PATCH 3/6] reverting removing license from source for submitting pull request --- src/reqwest.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/reqwest.js b/src/reqwest.js index f193842..0b7d2b3 100644 --- a/src/reqwest.js +++ b/src/reqwest.js @@ -1,8 +1,3 @@ -/*! - * Reqwest! A general purpose XHR connection manager - * license MIT (c) Dustin Diaz 2015 - * https://github.com/ded/reqwest - */ !function (name, context, definition) { if (typeof module != 'undefined' && module.exports) module.exports = definition() From 3f632528fd285d90eaab6f408a50621cf453af97 Mon Sep 17 00:00:00 2001 From: Yacine Petitprez Date: Mon, 21 Mar 2016 16:09:50 +0800 Subject: [PATCH 4/6] fix --- src/reqwest.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/reqwest.js b/src/reqwest.js index 0b7d2b3..321ec68 100644 --- a/src/reqwest.js +++ b/src/reqwest.js @@ -7,7 +7,7 @@ var context = this - if ('window' in context) { + if ('document' in context) { var doc = document , byTag = 'getElementsByTagName' , head = doc[byTag]('head')[0] @@ -60,6 +60,13 @@ if (xhr && 'withCredentials' in xhr) { return xhr } else if (context[xDomainRequest]) { + var protocolRegExp = /^https?/; + + if (window.location.href.match(protocolRegExp)[0] !== o.url.match(protocolRegExp)[0]) { + throw new Error('XDomainRequest: requests must be targeted to the same scheme as the hosting page.') + // As per: http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx + } + return new XDomainRequest() } else { throw new Error('Browser does not support cross-origin requests') From 5892f627dd5eb4ed8d611c79b9a5af9f22fc64e4 Mon Sep 17 00:00:00 2001 From: Yacine Petitprez Date: Mon, 21 Mar 2016 16:09:50 +0800 Subject: [PATCH 5/6] Reverting code to the last version --- src/reqwest.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/reqwest.js b/src/reqwest.js index 0b7d2b3..321ec68 100644 --- a/src/reqwest.js +++ b/src/reqwest.js @@ -7,7 +7,7 @@ var context = this - if ('window' in context) { + if ('document' in context) { var doc = document , byTag = 'getElementsByTagName' , head = doc[byTag]('head')[0] @@ -60,6 +60,13 @@ if (xhr && 'withCredentials' in xhr) { return xhr } else if (context[xDomainRequest]) { + var protocolRegExp = /^https?/; + + if (window.location.href.match(protocolRegExp)[0] !== o.url.match(protocolRegExp)[0]) { + throw new Error('XDomainRequest: requests must be targeted to the same scheme as the hosting page.') + // As per: http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx + } + return new XDomainRequest() } else { throw new Error('Browser does not support cross-origin requests') From cca6fee10aa2d6465ccde982503309d9c7682969 Mon Sep 17 00:00:00 2001 From: Yacine Petitprez Date: Mon, 21 Mar 2016 16:19:33 +0800 Subject: [PATCH 6/6] Fixing the README progress topic --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 72362e6..b8e3df9 100644 --- a/README.md +++ b/README.md @@ -181,7 +181,7 @@ var r = reqwest({ * `error` A function called when the request fails. * `complete` A function called whether the request is a success or failure. Always called when complete. * `jsonpCallback` Specify the callback function name for a `JSONP` request. This value will be used instead of the random (but recommended) name automatically generated by reqwest. - * `progress` A function called during upload, to handle the progression of the request. Usable also as promise-like method `progress` + * `progress` A function called during upload, to handle the progression of the request. ## Security