UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 4.26 kB
import{extend as e}from"../../core/core.js";import{now as t}from"../../core/time.js";import{path as s}from"../../core/path.js";import{URI as n}from"../../core/uri.js";import{math as o}from"../../core/math/math.js";class r{get(e,t,s){return"function"==typeof t&&(s=t,t={}),this.request("GET",e,t,s)}post(e,t,s,n){return"function"==typeof s&&(n=s,s={}),s.postdata=t,this.request("POST",e,s,n)}put(e,t,s,n){return"function"==typeof s&&(n=s,s={}),s.postdata=t,this.request("PUT",e,s,n)}del(e,t,s){return"function"==typeof t&&(s=t,t={}),this.request("DELETE",e,t,s)}request(s,o,a,p){let i,y,c,u=!1;if("function"==typeof a&&(p=a,a={}),a.retry&&(a=Object.assign({retries:0,maxRetries:5},a)),a.callback=p,null==a.async&&(a.async=!0),null==a.headers&&(a.headers={}),null!=a.postdata)if(a.postdata instanceof Document)c=a.postdata;else if(a.postdata instanceof FormData)c=a.postdata;else if(a.postdata instanceof Object){let e=a.headers["Content-Type"];switch(void 0===e&&(a.headers["Content-Type"]=r.ContentType.FORM_URLENCODED,e=a.headers["Content-Type"]),e){case r.ContentType.FORM_URLENCODED:{c="";let e=!0;for(const t in a.postdata)if(a.postdata.hasOwnProperty(t)){e?e=!1:c+="&";c+=`${encodeURIComponent(t)}=${encodeURIComponent(a.postdata[t])}`}break}default:case r.ContentType.JSON:null==e&&(a.headers["Content-Type"]=r.ContentType.JSON),c=JSON.stringify(a.postdata)}}else c=a.postdata;if(!1===a.cache){const e=t();i=new n(o),i.query?i.query=`${i.query}&ts=${e}`:i.query=`ts=${e}`,o=i.toString()}a.query&&(i=new n(o),y=e(i.getQuery(),a.query),i.setQuery(y),o=i.toString());const l=new XMLHttpRequest;l.open(s,o,a.async),l.withCredentials=void 0!==a.withCredentials&&a.withCredentials,l.responseType=a.responseType||this._guessResponseType(o);for(const e in a.headers)a.headers.hasOwnProperty(e)&&l.setRequestHeader(e,a.headers[e]);l.onreadystatechange=()=>{this._onReadyStateChange(s,o,a,l)},l.onerror=()=>{this._onError(s,o,a,l),u=!0};try{l.send(c)}catch(e){u||a.error(l.status,l,e)}return l}_guessResponseType(e){const t=new n(e),o=s.getExtension(t.path).toLowerCase();return r.binaryExtensions.indexOf(o)>=0?r.ResponseType.ARRAY_BUFFER:".json"===o?r.ResponseType.JSON:".xml"===o?r.ResponseType.DOCUMENT:r.ResponseType.TEXT}_isBinaryContentType(e){return[r.ContentType.BASIS,r.ContentType.BIN,r.ContentType.DDS,r.ContentType.GLB,r.ContentType.MP3,r.ContentType.MP4,r.ContentType.OGG,r.ContentType.OPUS,r.ContentType.WAV].indexOf(e)>=0}_isBinaryResponseType(e){return e===r.ResponseType.ARRAY_BUFFER||e===r.ResponseType.BLOB||e===r.ResponseType.JSON}_onReadyStateChange(e,t,s,n){if(4===n.readyState)switch(n.status){case 0:n.responseURL&&n.responseURL.startsWith("file:///")?this._onSuccess(e,t,s,n):this._onError(e,t,s,n);break;case 200:case 201:case 206:case 304:this._onSuccess(e,t,s,n);break;default:this._onError(e,t,s,n)}}_onSuccess(e,t,s,n){let o,a;const p=n.getResponseHeader("Content-Type");if(p){a=p.split(";")[0].trim()}try{o=this._isBinaryContentType(a)||this._isBinaryResponseType(n.responseType)?n.response:a===r.ContentType.JSON||t.split("?")[0].endsWith(".json")?JSON.parse(n.responseText):n.responseType===r.ResponseType.DOCUMENT||a===r.ContentType.XML?n.responseXML:n.responseText,s.callback(null,o)}catch(e){s.callback(e)}}_onError(e,t,s,n){if(!s.retrying)if(s.retry&&s.retries<s.maxRetries){s.retries++,s.retrying=!0;const a=o.clamp(Math.pow(2,s.retries)*r.retryDelay,0,s.maxRetryDelay||5e3);console.log(`${e}: ${t} - Error ${n.status}. Retrying in ${a} ms`),setTimeout((()=>{s.retrying=!1,this.request(e,t,s,s.callback)}),a)}else s.callback(0===n.status?"Network error":n.status,null)}}r.ContentType={AAC:"audio/aac",BASIS:"image/basis",BIN:"application/octet-stream",DDS:"image/dds",FORM_URLENCODED:"application/x-www-form-urlencoded",GIF:"image/gif",GLB:"model/gltf-binary",JPEG:"image/jpeg",JSON:"application/json",MP3:"audio/mpeg",MP4:"audio/mp4",OGG:"audio/ogg",OPUS:'audio/ogg; codecs="opus"',PNG:"image/png",TEXT:"text/plain",WAV:"audio/x-wav",XML:"application/xml"},r.ResponseType={TEXT:"text",ARRAY_BUFFER:"arraybuffer",BLOB:"blob",DOCUMENT:"document",JSON:"json"},r.binaryExtensions=[".model",".wav",".ogg",".mp3",".mp4",".m4a",".aac",".dds",".basis",".glb",".opus"],r.retryDelay=100;const a=new r;export{r as Http,a as http};