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,l=w(a.value[b]),f=c?l:[l];f.forEach(function(a,c){d.append(b,f[c])})});return d}if("Array"===a.type||"Object"===a.type){var m,c,l;"Object"===a?(l=Object.keys(a.value),c=
function(a){return a<l.length?l[a]:null},m={}):(c=function(b){return b<a.value.length?b:null},m=[]);for(var n,b=0;null!==(n=c(b));b++)m[n]=w(a.value[n]);return m}return a.value},G=function(a,d,m){void 0===m&&(m={});var c=p.assign({},d||{}),l=function(a,b){if(c[a])c[a]("function"==typeof b?b():b)};d=function(a,b){c[a]&&(l(a,b),c[a]=null)};if(m.internal||A(a.url)){var n=window.fetch&&a.url&&"http"==a.url.substr(0,4),b=!v&&a.anonymous,h=a.fetch;return n&&(b||h)?C(a,c,m,d,l):y(a,c,m,d,l)}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,m,c,l){var n=function(a){var b=[],c,e;a.headers&&(c=a.headers.get(z)||a.url,a.headers.forEach(function(a,e){var c=e.toLowerCase();-1==[z,D].indexOf(c)&&b.push(c+":"+a)}),(e=a.headers.get(D))&&b.push("set-cookie:"+e));return{readyState:4,responseHeaders:b.join("\n"),finalUrl:c,status:a.status,statusText:a.statusText}},b=function(b){(function(a,
b){for(var e=[],k=0,c=a.length;k<c;k+=b)e.push(a.slice(k,b+k));return e})(b,parseInt(a.partialSize)).forEach(function(a){l("onpartial",{partial:a})})};try{var h={};h.method=a.method||"GET";h.redirect="follow";if(a.nocache||a.revalidate)h.cache="no-store";h.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});h.headers=new window.Headers(p)}void 0!==
a.data&&(h.body="typified"===a.data_type?w(a.data):"string"===typeof a.data?a.data:JSON.stringify(a.data));void 0!==a.timeout&&(h.timeout=a.timeout);var r=window.fetch(a.url,h).then(function(f){var g=n(f);(0!==g.status||200>g.status||300<=g.status)&&0<a.retries?(a.retries--,C(a,d,m,c,l)):function(){var b;if(f.ok)void 0!==a.responseType?function(){if(!a.convertBinary||"arraybuffer"!=a.responseType&&"blob"!=a.responseType){if("arraybuffer"==a.responseType)return f.arrayBuffer();if("blob"==a.responseType)return f.blob();
console.warn("xhr: responseType",a.responseType," is not implemented!");return f.text()}return f.arrayBuffer().then(function(a){return q.arrbuf2str(a)})}().then(function(a){g.response=a;b()}):f.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 f=a.convertBinary&&g.response?g.response:g.responseText;["response","responseText","responseXML"].forEach(function(a){delete g[a]});
f&&(f.length>a.partialSize?b(f):g.response_data=f)}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(f){console.error(f.stack),h=u(f.message),c("onerror",h),c("ondone",h)}return{abort:function(){try{r.abort&&r.abort()}catch(a){}}}},y=function(a,d,m,c,l){var n,b=new XMLHttpRequest(a.anonymous?{mozAnon:!0}:{}),h=function(e){var k="",c=a.url;if(2<=b.readyState&&(k=b.getAllResponseHeaders(),4==b.readyState)){k&&
(k=k.replace(/tm-finalurl[0-9a-zA-Z]*\: .*[\r\n]{1,2}/i,""),k=k.replace(/tm-setcookie[0-9a-zA-Z]*\:/i,"set-cookie:"));var d;if(d=b.getResponseHeader(z))c=d}k={readyState:b.readyState,responseHeaders:k,finalUrl:c,status:4==b.readyState?b.status:0,statusText:4==b.readyState?b.statusText:""};e&&4==b.readyState?(b.responseType?(k.responseXML=null,k.responseText=null,k.responseType=b.responseType):(k.responseXML=b.responseXML,k.responseText=b.responseText),k.response=b.response):(k.responseXML=null,k.responseText=
"",k.response=null);return k},v=function(e){(function(a,e){for(var b=[],c=0,d=a.length;c<d;c+=e)b.push(a.slice(c,e+c));return b})(e,parseInt(a.partialSize)).forEach(function(a){l("onpartial",{partial:a})})},r={onload:function(){var e=h(!0);(0!==e.status||200>e.status||300<=e.status)&&0<a.retries?(a.retries--,y(a,d,m,c,l)):function(){if(a.convertBinary&&e.response){var b=e.responseType?e.responseType.toLowerCase():"";if("blob"==b){var c,d=new FileReader;d.onload=function(){e.response=q.arrbuf2str(d.result);
c()};d.readAsArrayBuffer(e.response);return{done:function(a){c=a}}}if("arraybuffer"==b)e.response=q.arrbuf2str(e.response);else if("json"==b)e.response=JSON.stringify(e.response);else if("document"==b)if("string"==typeof e.response)e.response={data:e.response,contentType:"text/html"};else{var b=e.response,f=b.contentType||"text/html";try{e.response={data:(new XMLSerializer).serializeToString(b.documentElement),contentType:f}}catch(g){var h="unable to serialize content type "+f;console.warn("xhr: ",
h,b);e.response={error:h,contentType:f}}}}return{done:function(a){a()}}}().done(function(){if(a.partialSize&&d.onpartial){var b=a.convertBinary&&e.response?e.response:e.responseText;["response","responseText","responseXML"].forEach(function(a){delete e[a]});b&&(b.length>a.partialSize?v(b):e.response_data=b)}c("onload",e);4==e.readyState&&c("ondone",e)})},onerror:function(){var b=h();4==b.readyState&&200!=b.status&&0!=b.status&&0<a.retries?(a.retries--,y(a,d,m,c,l)):(c("onerror",b),c("ondone",b))},
onloadstart:function(){l("onloadstart",function(){return h()})},onreadystatechange:function(){l("onreadystatechange",function(){return h()})},onprogress:function(a){l("onprogress",function(){var c=h(),d=c;void 0===d&&(d={});try{var f=null,g=null;if(a.lengthComputable||0<a.total)f=a.loaded,g=a.total;else{var l=b.responseType&&-1==["","text"].indexOf(b.responseType)?null:b.responseText,m=Number(p.getStringBetweenTags(c.responseHeaders.toLowerCase(),"content-length:","\n").trim()),n=2<c.readyState&&
l?l.length:0;0==m&&(m=-1);f=a.loaded||n;g=a.total||m}d.lengthComputable=a.lengthComputable;d.loaded=f;d.done=f;d.position=f;d.total=g;d.totalSize=g}catch(q){}return d})},ontimeout:function(){var a=h();c("ontimeout");c("ondone",a)},onabort:function(){var a=u("aborted");c("ondone",a)}},f=0==Object.keys(r).concat(["ondone"]).filter(function(a){return!!d[a]}).length;f||Object.keys(r).forEach(function(a){if(d[a]||-1!=["ontimeout","onload","onerror","onabort"].indexOf(a))b[a]=r[a]});try{if(!m.internal&&
!A(a.url))throw Error("Invalid scheme of url: "+a.url);b.open(a.method,a.url,!f,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(f){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(e){console.error(e.stack);var x=u(e.message);c("onerror",x);c("ondone",x);f&&(n=x)}n=n||(f?h():{});return p.copy({abort:function(){b.abort()}},n)};Registry.register("xmlhttprequest","5828",
function(a,d){v=d;t=a;return{run:G,makeErrorResponse:u}})});
