Registry.require(["helper","convert"],function(){var p=Registry.get("helper"),q=Registry.get("convert"),t,v,A=function(a){return a&&"http"==a.substr(0,4)},B={internal:!0,cookie:!0,"user-agent":!0,referer:!0,origin:!0,host:!0,"proxy-connection":!0,"accept-encoding":!0,"accept-charset":!0},E={"cache-control":"no-cache",pragma:"no-cache"},F={"cache-control":"max-age=0, must-revalidate"},u=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 m,b=0;null!==(m=c(b));b++)l[m]=w(a.value[m]);return l}return a.value},G=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||A(a.url)){var m=window.fetch&&a.url&&"http"==a.url.substr(0,4),b=!v&&a.anonymous,k=a.fetch;return m&&(b||k)?C(a,c,l,d,h):y(a,c,l,d,h)}console.warn("xhr: invalid scheme of url:",
a.url);a=u("Invalid scheme");d("onerror",a);d("ondone",a)},z="tm-finalurl"+rea.runtime.short_id.toLowerCase(),D="tm-setcookie"+rea.runtime.short_id.toLowerCase(),C=function(a,d,l,c,h){var m=function(a){var b=[],c,n;a.headers&&(c=a.headers.get(z)||a.url,a.headers.forEach(function(a,n){var c=n.toLowerCase();-1==[z,D].indexOf(c)&&b.push(c+":"+a)}),(n=a.headers.get(D))&&b.push("set-cookie:"+n));return{readyState:4,responseHeaders:b.join("\n"),finalUrl:c,status:a.status,statusText:a.statusText}},b=function(b){(function(a,
b){for(var n=[],f=0,c=a.length;f<c;f+=b)n.push(a.slice(f,b+f));return n})(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)B[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=m(e);(0!==g.status||200>g.status||300<=g.status)&&0<a.retries?(a.retries--,C(a,d,l,c,h)):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=m({status:408,statusText:a.message||"Request Timeout"});c("onerror",a);c("ondone",a)})}catch(e){console.error(e.stack),k=u(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 m,b=new XMLHttpRequest(a.anonymous?{mozAnon:!0}:{}),k=function(n){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}/i,""),f=f.replace(/tm-setcookie[0-9a-zA-Z]*\:/i,"set-cookie:"));var d;if(d=b.getResponseHeader(z))c=d}f={readyState:b.readyState,responseHeaders:f,finalUrl:c,status:4==b.readyState?b.status:0,statusText:4==b.readyState?b.statusText:""};n&&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},v=function(b){(function(a,b){for(var n=[],c=0,d=a.length;c<d;c+=b)n.push(a.slice(c,b+c));return n})(b,parseInt(a.partialSize)).forEach(function(a){h("onpartial",{partial:a})})},r={onload:function(){var b=k(!0);(0!==b.status||200>b.status||300<=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?v(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=b.responseType&&-1==["","text"].indexOf(b.responseType)?null:b.responseText,
l=Number(p.getStringBetweenTags(c.responseHeaders.toLowerCase(),"content-length:","\n").trim()),m=2<c.readyState&&h?h.length:0;0==l&&(l=-1);e=a.loaded||m;g=a.total||l}d.lengthComputable=a.lengthComputable;d.loaded=e;d.done=e;d.position=e;d.total=g;d.totalSize=g}catch(q){}return d})},ontimeout:function(){var a=k();c("ontimeout");c("ondone",a)},onabort:function(){var a=u("aborted");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","onabort"].indexOf(a))b[a]=r[a]});try{if(!l.internal&&!A(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,E):a.revalidate&&p.assign(g,F);Object.keys(g).forEach(function(a){var c=a,d=g[a];if(t)B[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(n){console.error(n.stack);var x=u(n.message);c("onerror",x);c("ondone",x);e&&(m=x)}m=m||(e?k():{});return p.copy({abort:function(){b.abort()}},
m)};Registry.register("xmlhttprequest","5770",function(a,d){v=d;t=a;return{run:G,makeErrorResponse:u}})});
