Registry.require(["helper","convert"],function(){var p=Registry.get("helper"),q=Registry.get("convert"),t,u,z=function(a){var d=p.isLocalImage(a);return a&&"http"==a.substr(0,4)||d},A={internal:!0,cookie:!0,"user-agent":!0,referer:!0,origin:!0,host:!0,"proxy-connection":!0,"accept-encoding":!0,"accept-charset":!0},D={"Cache-Control":"no-cache",Pragma:"no-cache"},E={"Cache-Control":"max-age=0, must-revalidate"},v=function(a){return{responseXML:"",responseText:"",response:null,readyState:4,responseHeaders:"",
status:0,statusText:"",error:a||"Forbidden"}},w=function(a){if("Blob"===a.type)return new Blob([q.str2arrbuf(a.value)]);if("File"===a.type)return new File([q.str2arrbuf(a.value)],a.name,{type:a.meta,lastModified:a.lastModified||Date.now()});if("FormData"==a.type){var d=new FormData;Object.keys(a.value).forEach(function(b){var c="Array"===a.value[b].type,h=w(a.value[b]),e=c?h:[h];e.forEach(function(a,c){d.append(b,e[c])})});return d}if("Array"===a.type||"Object"===a.type){var l,c,h;"Object"===a?(h=
Object.keys(a.value),c=function(a){return a<h.length?h[a]:null},l={}):(c=function(b){return b<a.value.length?b:null},l=[]);for(var n,b=0;null!==(n=c(b));b++)l[n]=w(a.value[n]);return l}return a.value},F=function(a,d,l){void 0===l&&(l={});var c=p.assign({},d||{}),h=function(a,b){if(c[a])c[a]("function"==typeof b?b():b)};d=function(a,b){c[a]&&(h(a,b),c[a]=null)};if(l.internal||z(a.url)){var n=window.fetch&&a.url&&"http"==a.url.substr(0,4),b=!u&&a.anonymous,k=a.fetch;return n&&(b||k)?B(a,c,l,d,h):y(a,
c,l,d,h)}console.warn("xhr: invalid scheme of url:",a.url);a=v("Invalid scheme");d("onerror",a);d("ondone",a)},C="TM-finalURL"+rea.runtime.short_id,G="TM-setCookie"+rea.runtime.short_id,B=function(a,d,l,c,h){var n=function(a){var b=[],c,m;a.headers&&(c=a.headers.get(C)||a.url,a.headers.forEach(function(a,m){m.toLowerCase()!=C.toLowerCase()&&b.push(m+":"+a)}),(m=a.headers.get(G))&&b.push("Set-Cookie:"+m));return{readyState:4,responseHeaders:b.join("\n"),finalUrl:c,status:a.status,statusText:a.statusText}},
b=function(b){(function(a,b){for(var m=[],f=0,c=a.length;f<c;f+=b)m.push(a.slice(f,b+f));return m})(b,parseInt(a.partialSize)).forEach(function(a){h("onpartial",{partial:a})})};try{var k={};k.method=a.method||"GET";k.redirect="follow";if(a.nocache||a.revalidate)k.cache="no-store";k.credentials=a.anonymous?"omit":"include";if(a.headers){var p={};Object.keys(a.headers).forEach(function(b){var c=b,d=a.headers[b];if(t)A[b.toLowerCase()]&&(c=t+b,d=null===d?"":q.encodeS(d));else if(null===d)return;p[c]=
d});k.headers=new window.Headers(p)}void 0!==a.data&&(k.body="typified"===a.data_type?w(a.data):"string"===typeof a.data?a.data:JSON.stringify(a.data));void 0!==a.timeout&&(k.timeout=a.timeout);var r=window.fetch(a.url,k).then(function(e){var g=n(e);200!=g.status&&0!=g.status?0<a.retries?(a.retries--,B(a,d,l,c,h)):(c("onerror",g),c("ondone",g)):function(){var b;if(e.ok)void 0!==a.responseType?function(){if(!a.convertBinary||"arraybuffer"!=a.responseType&&"blob"!=a.responseType){if("arraybuffer"==
a.responseType)return e.arrayBuffer();if("blob"==a.responseType)return e.blob();console.warn("xhr: responseType",a.responseType," is not implemented!");return e.text()}return e.arrayBuffer().then(function(a){return q.arrbuf2str(a)})}().then(function(a){g.response=a;b()}):e.text().then(function(a){g.responseText=a;b()});else return g.responseXML=null,g.responseText="",g.response=null,{done:function(a){a()}};return{done:function(a){b=a}}}().done(function(){if(a.partialSize&&d.onpartial){var e=a.convertBinary&&
g.response?g.response:g.responseText;["response","responseText","responseXML"].forEach(function(a){delete g[a]});e&&(e.length>a.partialSize?b(e):g.response_data=e)}c("onload",g);c("ondone",g)})}).catch(function(a){a=n({status:408,statusText:a.message||"Request Timeout"});c("onerror",a);c("ondone",a)})}catch(e){console.error(e.stack),k=v(e.message),c("onerror",k),c("ondone",k)}return{abort:function(){try{r.abort&&r.abort()}catch(a){}}}},y=function(a,d,l,c,h){var n,b=new XMLHttpRequest(a.anonymous?
{mozAnon:!0}:{}),k=function(m){var f="",c=a.url;if(2<=b.readyState&&(f=b.getAllResponseHeaders(),4==b.readyState)){f&&(f=f.replace(/TM-finalURL[0-9a-zA-Z]*\: .*[\r\n]{1,2}/,""),f=f.replace(/TM-setCookie[0-9a-zA-Z]*\:/,"Set-Cookie:"));var d;if(d=b.getResponseHeader("TM-finalURL"+rea.runtime.short_id))c=d}f={readyState:b.readyState,responseHeaders:f,finalUrl:c,status:4==b.readyState?b.status:0,statusText:4==b.readyState?b.statusText:""};m&&4==b.readyState?(b.responseType?(f.responseXML=null,f.responseText=
null,f.responseType=b.responseType):(f.responseXML=b.responseXML,f.responseText=b.responseText),f.response=b.response):(f.responseXML=null,f.responseText="",f.response=null);return f},u=function(b){(function(a,b){for(var m=[],c=0,d=a.length;c<d;c+=b)m.push(a.slice(c,b+c));return m})(b,parseInt(a.partialSize)).forEach(function(a){h("onpartial",{partial:a})})},r={onload:function(){var b=k(!0);4==b.readyState&&200!=b.status&&0!=b.status&&0<a.retries?(a.retries--,y(a,d,l,c,h)):function(){if(a.convertBinary&&
b.response){var c=b.responseType?b.responseType.toLowerCase():"";if("blob"==c){var d,e=new FileReader;e.onload=function(){b.response=q.arrbuf2str(e.result);d()};e.readAsArrayBuffer(b.response);return{done:function(a){d=a}}}"arraybuffer"==c?b.response=q.arrbuf2str(b.response):"json"==c&&(b.response=JSON.stringify(b.response))}return{done:function(a){a()}}}().done(function(){if(a.partialSize&&d.onpartial){var f=a.convertBinary&&b.response?b.response:b.responseText;["response","responseText","responseXML"].forEach(function(a){delete b[a]});
f&&(f.length>a.partialSize?u(f):b.response_data=f)}c("onload",b);4==b.readyState&&c("ondone",b)})},onerror:function(){var b=k();4==b.readyState&&200!=b.status&&0!=b.status&&0<a.retries?(a.retries--,y(a,d,l,c,h)):(c("onerror",b),c("ondone",b))},onloadstart:function(){h("onloadstart",function(){return k()})},onreadystatechange:function(){h("onreadystatechange",function(){return k()})},onprogress:function(a){h("onprogress",function(){var c=k(),d=c;void 0===d&&(d={});try{var e=null,g=null;if(a.lengthComputable||
0<a.total)e=a.loaded,g=a.total;else{var h=Number(p.getStringBetweenTags(c.responseHeaders.toLowerCase(),"content-length:","\n").trim()),l=2<c.readyState&&b.responseText?b.responseText.length:0;0==h&&(h=-1);e=a.loaded||l;g=a.total||h}d.lengthComputable=a.lengthComputable;d.loaded=e;d.done=e;d.position=e;d.total=g;d.totalSize=g}catch(n){}return d})},ontimeout:function(){var a=k();c("ontimeout");c("ondone",a)}},e=0==Object.keys(r).concat(["ondone"]).filter(function(a){return!!d[a]}).length;e||Object.keys(r).forEach(function(a){if(d[a]||
-1!=["ontimeout","onload","onerror"].indexOf(a))b[a]=r[a]});try{if(!l.internal&&!z(a.url))throw Error("Invalid scheme of url: "+a.url);b.open(a.method,a.url,!e,a.user,a.password);if(a.headers||a.nocache||a.revalidate){var g={};a.headers&&p.assign(g,a.headers);a.nocache?p.assign(g,D):a.revalidate&&p.assign(g,E);Object.keys(g).forEach(function(a){var c=a,d=g[a];if(t)A[a.toLowerCase()]&&(c=t+a,d=null===d?"":q.encodeS(d));else if(null===d)return;try{b.setRequestHeader(c,d)}catch(e){console.warn("xhr: rejected header",
c,g[a])}})}void 0!==a.overrideMimeType&&b.overrideMimeType(a.overrideMimeType);void 0!==a.responseType&&(b.responseType=a.responseType);void 0!==a.timeout&&(b.timeout=a.timeout);void 0!==a.data?("typified"===a.data_type?b.send(w(a.data)):"string"===typeof a.data?b.send(a.data):b.send(JSON.stringify(a.data)),d.onprogress&&b.upload&&(b.upload.onprogress=r.onprogress)):b.send()}catch(m){console.error(m.stack);var x=v(m.message);c("onerror",x);c("ondone",x);e&&(n=x)}n=n||(e?k():{});return p.copy({abort:function(){b.abort()}},
n)};Registry.register("xmlhttprequest","5454",function(a,d){u=d;t=a;return{run:F,makeErrorResponse:v}})});
