From 907a980b62d3d530f1353b643256cba0076fd949 Mon Sep 17 00:00:00 2001 From: Michael Naumov Date: Sun, 27 Mar 2016 15:02:37 +0300 Subject: [PATCH 1/2] Add failing test for IE9 --- test/tests.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/tests.js b/test/tests.js index 7b9cbe2..4fab5b8 100644 --- a/test/tests.js +++ b/test/tests.js @@ -59,4 +59,12 @@ describe('xhook', function() { }; xhr.send(); }); + + it('should not fail on aborted request in IE9', function(done) { + var xhr = new XMLHttpRequest(); + xhr.open('GET', '../example/example1.txt'); + xhr.send(); + xhr.abort(); + done(); + }); }); From 071d26da7a863910c2f910219e6eef89643dc066 Mon Sep 17 00:00:00 2001 From: Michael Naumov Date: Sun, 27 Mar 2016 15:09:33 +0300 Subject: [PATCH 2/2] Don't call readBody for aborted request in IE9 --- dist/xhook.js | 3 +++ dist/xhook.min.js | 2 +- src/xhook.coffee | 2 ++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/dist/xhook.js b/dist/xhook.js index 64c73e0..b4ac624 100644 --- a/dist/xhook.js +++ b/dist/xhook.js @@ -305,6 +305,9 @@ XHookHttpRequest = window[XMLHTTP] = function() { } }; readBody = function() { + if (status === ABORTED && msie < 10) { + return; + } if (!xhr.responseType || xhr.responseType === "text") { response.text = xhr.responseText; response.data = xhr.responseText; diff --git a/dist/xhook.min.js b/dist/xhook.min.js index 714e0e7..6437e87 100644 --- a/dist/xhook.min.js +++ b/dist/xhook.min.js @@ -1,3 +1,3 @@ // XHook - v1.3.5 - https://github.com/jpillora/xhook // Jaime Pillora - MIT Copyright 2016 -(function(a,b){var c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B=[].indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1};t=a.document,d="before",c="after",m="readyState",l="addEventListener",k="removeEventListener",g="dispatchEvent",q="XMLHttpRequest",h="FormData",n=["load","loadend","loadstart"],e=["progress","abort","error","timeout"],w=parseInt((/msie (\d+)/.exec(navigator.userAgent.toLowerCase())||[])[1]),isNaN(w)&&(w=parseInt((/trident\/.*; rv:(\d+)/.exec(navigator.userAgent.toLowerCase())||[])[1])),(A=Array.prototype).indexOf||(A.indexOf=function(a){var b,c,d,e;for(b=d=0,e=this.length;e>d;b=++d)if(c=this[b],c===a)return b;return-1}),y=function(a,b){return Array.prototype.slice.call(a,b)},s=function(a){return"returnValue"===a||"totalSize"===a||"position"===a},v=function(a,b){var c,d;for(c in a)if(d=a[c],!s(c))try{b[c]=a[c]}catch(e){}return b},x=function(a,b,c){var d,e,f,h;for(e=function(a){return function(d){var e,f,h;e={};for(f in d)s(f)||(h=d[f],e[f]=h===b?c:h);return c[g](a,e)}},f=0,h=a.length;h>f;f++)d=a[f],c._has(d)&&(b["on"+d]=e(d))},u=function(a){var b;if(null!=t.createEventObject)return b=t.createEventObject(),b.type=a,b;try{return new Event(a)}catch(c){return{type:a}}},f=function(a){var c,d,e;return d={},e=function(a){return d[a]||[]},c={},c[l]=function(a,c,f){d[a]=e(a),d[a].indexOf(c)>=0||(f=f===b?d[a].length:f,d[a].splice(f,0,c))},c[k]=function(a,c){var f;return a===b?void(d={}):(c===b&&(d[a]=[]),f=e(a).indexOf(c),void(-1!==f&&e(a).splice(f,1)))},c[g]=function(){var b,d,f,g,h,i,j,k;for(b=y(arguments),d=b.shift(),a||(b[0]=v(b[0],u(d))),g=c["on"+d],g&&g.apply(c,b),k=e(d).concat(e("*")),f=i=0,j=k.length;j>i;f=++i)h=k[f],h.apply(c,b)},c._has=function(a){return!(!d[a]&&!c["on"+a])},a&&(c.listeners=function(a){return y(e(a))},c.on=c[l],c.off=c[k],c.fire=c[g],c.once=function(a,b){var d;return d=function(){return c.off(a,d),b.apply(null,arguments)},c.on(a,d)},c.destroy=function(){return d={}}),c},z=f(!0),z.EventEmitter=f,z[d]=function(a,b){if(a.length<1||a.length>2)throw"invalid hook";return z[l](d,a,b)},z[c]=function(a,b){if(a.length<2||a.length>3)throw"invalid hook";return z[l](c,a,b)},z.enable=function(){a[q]=p,i&&(a[h]=o)},z.disable=function(){a[q]=z[q],i&&(a[h]=i)},r=z.headers=function(a,b){var c,d,e,f,g,h,i,j,k;switch(null==b&&(b={}),typeof a){case"object":d=[];for(e in a)g=a[e],f=e.toLowerCase(),d.push(""+f+": "+g);return d.join("\n");case"string":for(d=a.split("\n"),i=0,j=d.length;j>i;i++)c=d[i],/([^:]+):\s*(.+)/.test(c)&&(f=null!=(k=RegExp.$1)?k.toLowerCase():void 0,h=RegExp.$2,null==b[f]&&(b[f]=h));return b}},i=a[h],o=function(a){var b;this.fd=a?new i(a):new i,this.form=a,b=[],Object.defineProperty(this,"entries",{get:function(){var c;return c=a?y(a.querySelectorAll("input,select")).filter(function(a){var b;return"checkbox"!==(b=a.type)&&"radio"!==b||a.checked}).map(function(a){return[a.name,"file"===a.type?a.files:a.value]}):[],c.concat(b)}}),this.append=function(a){return function(){var c;return c=y(arguments),b.push(c),a.fd.append.apply(a.fd,c)}}(this)},i&&(z[h]=i,a[h]=o),j=a[q],z[q]=j,p=a[q]=function(){var a,b,h,i,j,k,p,s,t,u,y,A,C,D,E,F,G,H,I,J,K;a=-1,H=new z[q],y={},D=null,p=void 0,E=void 0,A=void 0,u=function(){var b,c,d,e;if(A.status=D||H.status,D===a&&10>w||(A.statusText=H.statusText),D!==a){e=r(H.getAllResponseHeaders());for(b in e)d=e[b],A.headers[b]||(c=b.toLowerCase(),A.headers[c]=d)}},t=function(){H.responseType&&"text"!==H.responseType?"document"===H.responseType?(A.xml=H.responseXML,A.data=H.responseXML):A.data=H.response:(A.text=H.responseText,A.data=H.responseText),"responseURL"in H&&(A.finalUrl=H.responseURL)},G=function(){k.status=A.status,k.statusText=A.statusText},F=function(){"text"in A&&(k.responseText=A.text),"xml"in A&&(k.responseXML=A.xml),"data"in A&&(k.response=A.data),"finalUrl"in A&&(k.responseURL=A.finalUrl)},i=function(a){for(;a>b&&4>b;)k[m]=++b,1===b&&k[g]("loadstart",{}),2===b&&G(),4===b&&(G(),F()),k[g]("readystatechange",{}),4===b&&setTimeout(h,0)},h=function(){p||k[g]("load",{}),k[g]("loadend",{}),p&&(k[m]=0)},b=0,C=function(a){var b,d;return 4!==a?void i(a):(b=z.listeners(c),d=function(){var a;return b.length?(a=b.shift(),2===a.length?(a(y,A),d()):3===a.length&&y.async?a(y,A,d):d()):i(4)},void d())},k=y.xhr=f(),H.onreadystatechange=function(){try{2===H[m]&&u()}catch(a){}4===H[m]&&(E=!1,u(),t()),C(H[m])},s=function(){p=!0},k[l]("error",s),k[l]("timeout",s),k[l]("abort",s),k[l]("progress",function(){3>b?C(3):k[g]("readystatechange",{})}),("withCredentials"in H||z.addWithCredentials)&&(k.withCredentials=!1),k.status=0,K=e.concat(n);for(I=0,J=K.length;J>I;I++)j=K[I],k["on"+j]=null;return k.open=function(a,c,d,e,f){b=0,p=!1,E=!1,y.headers={},y.headerNames={},y.status=0,A={},A.headers={},y.method=a,y.url=c,y.async=d!==!1,y.user=e,y.pass=f,C(1)},k.send=function(a){var b,c,f,g,h,i,j,l;for(l=["type","timeout","withCredentials"],i=0,j=l.length;j>i;i++)c=l[i],f="type"===c?"responseType":c,f in k&&(y[c]=k[f]);y.body=a,h=function(){var a,b,d,g,h,i;for(x(e,H,k),k.upload&&x(e.concat(n),H.upload,k.upload),E=!0,H.open(y.method,y.url,y.async,y.user,y.pass),h=["type","timeout","withCredentials"],d=0,g=h.length;g>d;d++)c=h[d],f="type"===c?"responseType":c,c in y&&(H[f]=y[c]);i=y.headers;for(a in i)b=i[a],a&&H.setRequestHeader(a,b);y.body instanceof o&&(y.body=y.body.fd),H.send(y.body)},b=z.listeners(d),(g=function(){var a,c;return b.length?(a=function(a){return"object"!=typeof a||"number"!=typeof a.status&&"number"!=typeof A.status?void g():(v(a,A),B.call(a,"data")<0&&(a.data=a.response||a.text),void C(4))},a.head=function(a){return v(a,A),C(2)},a.progress=function(a){return v(a,A),C(3)},c=b.shift(),1===c.length?a(c(y)):2===c.length&&y.async?c(y,a):a()):h()})()},k.abort=function(){D=a,E?H.abort():k[g]("abort",{})},k.setRequestHeader=function(a,b){var c,d;c=null!=a?a.toLowerCase():void 0,d=y.headerNames[c]=y.headerNames[c]||a,y.headers[d]&&(b=y.headers[d]+", "+b),y.headers[d]=b},k.getResponseHeader=function(a){var b;return b=null!=a?a.toLowerCase():void 0,A.headers[b]},k.getAllResponseHeaders=function(){return r(A.headers)},H.overrideMimeType&&(k.overrideMimeType=function(){return H.overrideMimeType.apply(H,arguments)}),H.upload&&(k.upload=y.upload=f()),k},"function"==typeof define&&define.amd?define("xhook",[],function(){return z}):(this.exports||this).xhook=z}).call(this,window); \ No newline at end of file +(function(a,b){var c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B=[].indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1};t=a.document,d="before",c="after",m="readyState",l="addEventListener",k="removeEventListener",g="dispatchEvent",q="XMLHttpRequest",h="FormData",n=["load","loadend","loadstart"],e=["progress","abort","error","timeout"],w=parseInt((/msie (\d+)/.exec(navigator.userAgent.toLowerCase())||[])[1]),isNaN(w)&&(w=parseInt((/trident\/.*; rv:(\d+)/.exec(navigator.userAgent.toLowerCase())||[])[1])),(A=Array.prototype).indexOf||(A.indexOf=function(a){var b,c,d,e;for(b=d=0,e=this.length;e>d;b=++d)if(c=this[b],c===a)return b;return-1}),y=function(a,b){return Array.prototype.slice.call(a,b)},s=function(a){return"returnValue"===a||"totalSize"===a||"position"===a},v=function(a,b){var c,d;for(c in a)if(d=a[c],!s(c))try{b[c]=a[c]}catch(e){}return b},x=function(a,b,c){var d,e,f,h;for(e=function(a){return function(d){var e,f,h;e={};for(f in d)s(f)||(h=d[f],e[f]=h===b?c:h);return c[g](a,e)}},f=0,h=a.length;h>f;f++)d=a[f],c._has(d)&&(b["on"+d]=e(d))},u=function(a){var b;if(null!=t.createEventObject)return b=t.createEventObject(),b.type=a,b;try{return new Event(a)}catch(c){return{type:a}}},f=function(a){var c,d,e;return d={},e=function(a){return d[a]||[]},c={},c[l]=function(a,c,f){d[a]=e(a),d[a].indexOf(c)>=0||(f=f===b?d[a].length:f,d[a].splice(f,0,c))},c[k]=function(a,c){var f;return a===b?void(d={}):(c===b&&(d[a]=[]),f=e(a).indexOf(c),void(-1!==f&&e(a).splice(f,1)))},c[g]=function(){var b,d,f,g,h,i,j,k;for(b=y(arguments),d=b.shift(),a||(b[0]=v(b[0],u(d))),g=c["on"+d],g&&g.apply(c,b),k=e(d).concat(e("*")),f=i=0,j=k.length;j>i;f=++i)h=k[f],h.apply(c,b)},c._has=function(a){return!(!d[a]&&!c["on"+a])},a&&(c.listeners=function(a){return y(e(a))},c.on=c[l],c.off=c[k],c.fire=c[g],c.once=function(a,b){var d;return d=function(){return c.off(a,d),b.apply(null,arguments)},c.on(a,d)},c.destroy=function(){return d={}}),c},z=f(!0),z.EventEmitter=f,z[d]=function(a,b){if(a.length<1||a.length>2)throw"invalid hook";return z[l](d,a,b)},z[c]=function(a,b){if(a.length<2||a.length>3)throw"invalid hook";return z[l](c,a,b)},z.enable=function(){a[q]=p,i&&(a[h]=o)},z.disable=function(){a[q]=z[q],i&&(a[h]=i)},r=z.headers=function(a,b){var c,d,e,f,g,h,i,j,k;switch(null==b&&(b={}),typeof a){case"object":d=[];for(e in a)g=a[e],f=e.toLowerCase(),d.push(""+f+": "+g);return d.join("\n");case"string":for(d=a.split("\n"),i=0,j=d.length;j>i;i++)c=d[i],/([^:]+):\s*(.+)/.test(c)&&(f=null!=(k=RegExp.$1)?k.toLowerCase():void 0,h=RegExp.$2,null==b[f]&&(b[f]=h));return b}},i=a[h],o=function(a){var b;this.fd=a?new i(a):new i,this.form=a,b=[],Object.defineProperty(this,"entries",{get:function(){var c;return c=a?y(a.querySelectorAll("input,select")).filter(function(a){var b;return"checkbox"!==(b=a.type)&&"radio"!==b||a.checked}).map(function(a){return[a.name,"file"===a.type?a.files:a.value]}):[],c.concat(b)}}),this.append=function(a){return function(){var c;return c=y(arguments),b.push(c),a.fd.append.apply(a.fd,c)}}(this)},i&&(z[h]=i,a[h]=o),j=a[q],z[q]=j,p=a[q]=function(){var a,b,h,i,j,k,p,s,t,u,y,A,C,D,E,F,G,H,I,J,K;a=-1,H=new z[q],y={},D=null,p=void 0,E=void 0,A=void 0,u=function(){var b,c,d,e;if(A.status=D||H.status,D===a&&10>w||(A.statusText=H.statusText),D!==a){e=r(H.getAllResponseHeaders());for(b in e)d=e[b],A.headers[b]||(c=b.toLowerCase(),A.headers[c]=d)}},t=function(){D===a&&10>w||(H.responseType&&"text"!==H.responseType?"document"===H.responseType?(A.xml=H.responseXML,A.data=H.responseXML):A.data=H.response:(A.text=H.responseText,A.data=H.responseText),"responseURL"in H&&(A.finalUrl=H.responseURL))},G=function(){k.status=A.status,k.statusText=A.statusText},F=function(){"text"in A&&(k.responseText=A.text),"xml"in A&&(k.responseXML=A.xml),"data"in A&&(k.response=A.data),"finalUrl"in A&&(k.responseURL=A.finalUrl)},i=function(a){for(;a>b&&4>b;)k[m]=++b,1===b&&k[g]("loadstart",{}),2===b&&G(),4===b&&(G(),F()),k[g]("readystatechange",{}),4===b&&setTimeout(h,0)},h=function(){p||k[g]("load",{}),k[g]("loadend",{}),p&&(k[m]=0)},b=0,C=function(a){var b,d;return 4!==a?void i(a):(b=z.listeners(c),d=function(){var a;return b.length?(a=b.shift(),2===a.length?(a(y,A),d()):3===a.length&&y.async?a(y,A,d):d()):i(4)},void d())},k=y.xhr=f(),H.onreadystatechange=function(a){try{2===H[m]&&u()}catch(b){}4===H[m]&&(E=!1,u(),t()),C(H[m])},s=function(){p=!0},k[l]("error",s),k[l]("timeout",s),k[l]("abort",s),k[l]("progress",function(){3>b?C(3):k[g]("readystatechange",{})}),("withCredentials"in H||z.addWithCredentials)&&(k.withCredentials=!1),k.status=0,K=e.concat(n);for(I=0,J=K.length;J>I;I++)j=K[I],k["on"+j]=null;return k.open=function(a,c,d,e,f){b=0,p=!1,E=!1,y.headers={},y.headerNames={},y.status=0,A={},A.headers={},y.method=a,y.url=c,y.async=d!==!1,y.user=e,y.pass=f,C(1)},k.send=function(a){var b,c,f,g,h,i,j,l;for(l=["type","timeout","withCredentials"],i=0,j=l.length;j>i;i++)c=l[i],f="type"===c?"responseType":c,f in k&&(y[c]=k[f]);y.body=a,h=function(){var a,b,d,g,h,i;for(x(e,H,k),k.upload&&x(e.concat(n),H.upload,k.upload),E=!0,H.open(y.method,y.url,y.async,y.user,y.pass),h=["type","timeout","withCredentials"],d=0,g=h.length;g>d;d++)c=h[d],f="type"===c?"responseType":c,c in y&&(H[f]=y[c]);i=y.headers;for(a in i)b=i[a],a&&H.setRequestHeader(a,b);y.body instanceof o&&(y.body=y.body.fd),H.send(y.body)},b=z.listeners(d),(g=function(){var a,c;return b.length?(a=function(a){return"object"!=typeof a||"number"!=typeof a.status&&"number"!=typeof A.status?void g():(v(a,A),B.call(a,"data")<0&&(a.data=a.response||a.text),void C(4))},a.head=function(a){return v(a,A),C(2)},a.progress=function(a){return v(a,A),C(3)},c=b.shift(),1===c.length?a(c(y)):2===c.length&&y.async?c(y,a):a()):h()})()},k.abort=function(){D=a,E?H.abort():k[g]("abort",{})},k.setRequestHeader=function(a,b){var c,d;c=null!=a?a.toLowerCase():void 0,d=y.headerNames[c]=y.headerNames[c]||a,y.headers[d]&&(b=y.headers[d]+", "+b),y.headers[d]=b},k.getResponseHeader=function(a){var b;return b=null!=a?a.toLowerCase():void 0,A.headers[b]},k.getAllResponseHeaders=function(){return r(A.headers)},H.overrideMimeType&&(k.overrideMimeType=function(){return H.overrideMimeType.apply(H,arguments)}),H.upload&&(k.upload=y.upload=f()),k},"function"==typeof define&&define.amd?define("xhook",[],function(){return z}):(this.exports||this).xhook=z}).call(this,window); \ No newline at end of file diff --git a/src/xhook.coffee b/src/xhook.coffee index ed460bc..ffd53f8 100644 --- a/src/xhook.coffee +++ b/src/xhook.coffee @@ -220,6 +220,8 @@ XHookHttpRequest = window[XMLHTTP] = -> return readBody = -> + if status is ABORTED and msie < 10 + return #https://xhr.spec.whatwg.org/ if !xhr.responseType or xhr.responseType is "text" response.text = xhr.responseText