12345 |
- /*
- All material copyright ESRI, All Rights Reserved, unless otherwise specified.
- See https://js.arcgis.com/4.25/esri/copyright.txt for details.
- */
- import t from"../config.js";import n from"./Error.js";import e from"./Logger.js";import{assumeNonNull as r,isSome as o,isNone as i}from"./maybe.js";const s=e.getLogger("esri.core.urlUtils"),u=t.request,l="esri/config: esriConfig.request.proxyUrl is not set.",c=/^\s*[a-z][a-z0-9-+.]*:(?![0-9])/i,f=/^\s*http:/i,a=/^\s*https:/i,h=/^\s*file:/i,p=/:\d+$/,d=/^https?:\/\/[^/]+\.arcgis.com\/sharing(\/|$)/i,g=new RegExp("^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?$"),m=new RegExp("^((([^\\[:]+):)?([^@]+)@)?(\\[([^\\]]+)\\]|([^\\[:]*))(:([0-9]+))?$");class y{constructor(t=""){this.uri=t,this.scheme=null,this.authority=null,this.path=null,this.query=null,this.fragment=null,this.user=null,this.password=null,this.host=null,this.port=null;let n=r(this.uri.match(g));this.scheme=n[2]||(n[1]?"":null),this.authority=n[4]||(n[3]?"":null),this.path=n[5],this.query=n[7]||(n[6]?"":null),this.fragment=n[9]||(n[8]?"":null),null!=this.authority&&(n=r(this.authority.match(m)),this.user=n[3]||null,this.password=n[4]||null,this.host=n[6]||n[7],this.port=n[9]||null)}toString(){return this.uri}}const $={},x=new y(t.applicationUrl);let w=x;const O=q();let U=O;const b=()=>w,C=()=>U;function q(){const t=r(w.path),n=t.substring(0,t.lastIndexOf(t.split("/")[t.split("/").length-1]));return`${`${w.scheme}://${w.host}${null!=w.port?`:${w.port}`:""}`}${n}`}const R={setAppUrl:t=>w=t,setAppBaseUrl:t=>U=t,restoreUrls:()=>{w=x,U=O}};function j(t){if(!t)return null;const n={path:null,query:null},e=new y(t),r=t.indexOf("?");return null===e.query?n.path=t:(n.path=t.substring(0,r),n.query=L(e.query)),e.fragment&&(n.hash=e.fragment,null===e.query&&(n.path=n.path.substring(0,n.path.length-(e.fragment.length+1)))),n}function L(t){const n=t.split("&"),e={};for(const r of n){if(!r)continue;const t=r.indexOf("=");let n,o;t<0?(n=decodeURIComponent(r),o=""):(n=decodeURIComponent(r.slice(0,t)),o=decodeURIComponent(r.slice(t+1)));let i=e[n];"string"==typeof i&&(i=e[n]=[i]),Array.isArray(i)?i.push(o):e[n]=o}return e}function v(t){return t&&"object"==typeof t&&"toJSON"in t&&"function"==typeof t.toJSON}function I(t,n){return t?n&&"function"==typeof n?Object.keys(t).map((e=>encodeURIComponent(e)+"="+encodeURIComponent(n(e,t[e])))).join("&"):Object.keys(t).map((e=>{const r=t[e];if(null==r)return"";const o=encodeURIComponent(e)+"=",i=n&&n[e];return i?o+encodeURIComponent(i(r)):Array.isArray(r)?r.map((t=>v(t)?o+encodeURIComponent(JSON.stringify(t)):o+encodeURIComponent(t))).join("&"):v(r)?o+encodeURIComponent(JSON.stringify(r)):o+encodeURIComponent(r)})).filter((t=>t)).join("&"):""}function A(t=!1){let e,r=u.proxyUrl;if("string"==typeof t){e=ht(t);const n=J(t);n&&(r=n.proxyUrl)}else e=!!t;if(!r)throw s.warn(l),new n("urlutils:proxy-not-set",l);e&&$t()&&(r=mt(r));return j(r)}function S(t){const n=J(t);let e,r;if(n){const t=P(n.proxyUrl);e=t.path,r=t.query?L(t.query):null}if(e){const n=j(t);t=e+"?"+n.path;const o=I({...r,...n.query});o&&(t=`${t}?${o}`)}return t}const B={path:"",query:""};function P(t){const n=t.indexOf("?");return-1!==n?(B.path=t.slice(0,n),B.query=t.slice(n+1)):(B.path=t,B.query=null),B}function k(t){return t=(t=xt(t=bt(t=P(t).path),!0)).toLowerCase()}function E(t){const n={proxyUrl:t.proxyUrl,urlPrefix:k(t.urlPrefix)},e=u.proxyRules,r=n.urlPrefix;let o=e.length;for(let i=0;i<e.length;i++){const t=e[i].urlPrefix;if(0===r.indexOf(t)){if(r.length===t.length)return-1;o=i;break}0===t.indexOf(r)&&(o=i+1)}return e.splice(o,0,n),o}function J(t){const n=u.proxyRules,e=k(t);for(let r=0;r<n.length;r++)if(0===e.indexOf(n[r].urlPrefix))return n[r]}function N(t,n){return t=T(t),n=T(n),xt(t)===xt(n)}function T(t){const n=(t=F(t)).indexOf("/sharing");return n>0?t.substring(0,n):t.replace(/\/+$/,"")}function W(t){const n=n=>null==n||n instanceof RegExp&&n.test(t)||"string"==typeof n&&t.startsWith(n),e=u.interceptors;if(e)for(const r of e)if(Array.isArray(r.urls)){if(r.urls.some(n))return r}else if(n(r.urls))return r;return null}function z(t,n,e=!1){if(!t||!n)return!1;const r=vt(t),o=vt(n);return!(!e&&r.scheme!==o.scheme)&&(null!=r.host&&null!=o.host&&(r.host.toLowerCase()===o.host.toLowerCase()&&r.port===o.port))}function D(t){if("string"==typeof t){if(!K(t))return!0;t=vt(t)}if(z(t,w))return!0;const n=u.trustedServers||[];for(let e=0;e<n.length;e++){const r=M(n[e]);for(let n=0;n<r.length;n++)if(z(t,r[n]))return!0}return!1}function M(t){return $[t]||(at(t)||ft(t)?$[t]=[new y(Q(t))]:$[t]=[new y(`http://${t}`),new y(`https://${t}`)]),$[t]}function Q(t,n=U,e){return ft(t)?e&&e.preserveProtocolRelative?t:"http"===w.scheme&&w.authority===H(t).slice(2)?`http:${t}`:`https:${t}`:at(t)?t:r(G("/"===t[0]?wt(n):n,t))}function _(t,n=U,e){if(null==t||!K(t))return t;const r=F(t),o=r.toLowerCase(),i=F(n).toLowerCase().replace(/\/+$/,""),s=e?F(e).toLowerCase().replace(/\/+$/,""):null;if(s&&0!==i.indexOf(s))return t;const u=(t,n,e)=>-1===(e=t.indexOf(n,e))?t.length:e;let l=u(o,"/",o.indexOf("//")+2),c=-1;for(;o.slice(0,l+1)===i.slice(0,l)+"/"&&(c=l+1,l!==o.length);)l=u(o,"/",l+1);if(-1===c)return t;if(s&&c<s.length)return t;t=r.slice(c);const f=i.slice(c-1).replace(/[^/]+/g,"").length;if(f>0)for(let a=0;a<f;a++)t=`../${t}`;else t=`./${t}`;return t}function F(t){return t=jt(t=Rt(t=qt(t=Q(t=t.trim()))))}function G(...t){const n=t.filter(o);if(!n||!n.length)return;const e=[];if(K(n[0])){const t=n[0],r=t.indexOf("//");-1!==r&&(e.push(t.slice(0,r+1)),dt(n[0])&&(e[0]+="/"),n[0]=t.slice(r+2))}else"/"===n[0][0]&&e.push("");const r=n.reduce(((t,n)=>n?t.concat(n.split("/")):t),[]);for(let o=0;o<r.length;o++){const t=r[o];".."===t&&e.length>0&&".."!==e[e.length-1]?e.pop():(!t&&o===r.length-1||t&&("."!==t||0===e.length))&&e.push(t)}return e.join("/")}function H(t,n=!1){if(null==t||V(t)||X(t))return null;let e=t.indexOf("://");if(-1===e&&ft(t))e=2;else{if(-1===e)return null;e+=3}const r=t.indexOf("/",e);return-1!==r&&(t=t.slice(0,r)),n&&(t=xt(t,!0)),t}function K(t){return ft(t)||at(t)}function V(t){return null!=t&&"blob:"===t.slice(0,5)}function X(t){return null!=t&&"data:"===t.slice(0,5)}function Y(t){const n=nt(t);if(!n||!n.isBase64)return null;const e=atob(n.data),r=new Uint8Array(e.length);for(let o=0;o<e.length;o++)r[o]=e.charCodeAt(o);return r.buffer}function Z(t){return btoa(String.fromCharCode.apply(null,t)).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}const tt=/^data:(.*?)(;base64)?,(.*)$/;function nt(t){const n=t.match(tt);if(!n)return null;const[,e,r,o]=n;return{mediaType:e,isBase64:!!r,data:o}}function et(t){return t.isBase64?`data:${t.mediaType};base64,${t.data}`:`data:${t.mediaType},${t.data}`}function rt(t){const n=Y(t);if(!n)return null;const e=nt(t);return new Blob([n],{type:e.mediaType})}function ot(t,n){st(t,n)||lt(t,n)}function it(t,n){ut(t,n)||ct(t,n)}function st(t,n){const e=rt(t);return!!e&&ut(e,n)}function ut(t,n){if(!t)return!1;const e=document.createElement("a");if(!("download"in e))return!1;const r=URL.createObjectURL(t);return e.download=n,e.href=r,e.style.display="none",document.body.appendChild(e),e.click(),document.body.removeChild(e),URL.revokeObjectURL(r),!0}function lt(t,n){const e=rt(t);return!!e&&ct(e,n)}function ct(t,n){return!!window.navigator.msSaveOrOpenBlob&&window.navigator.msSaveOrOpenBlob(t,n)}function ft(t){return null!=t&&"/"===t[0]&&"/"===t[1]}function at(t){return null!=t&&c.test(t)}function ht(t){return null!=t&&a.test(t)||"https"===w.scheme&&ft(t)}function pt(t){return null!=t&&f.test(t)||"http"===w.scheme&&ft(t)}function dt(t){return null!=t&&h.test(t)}function gt(t){return ft(t)?`http:${t}`:t.replace(a,"http:")}function mt(t){return ft(t)?`https:${t}`:t.replace(f,"https:")}function yt(){return"http"===w.scheme}function $t(){return"https"===w.scheme}function xt(t,n=!1){return ft(t)?t.slice(2):(t=t.replace(c,""),n&&t.length>1&&"/"===t[0]&&"/"===t[1]&&(t=t.slice(2)),t)}function wt(t){const n=t.indexOf("//"),e=t.indexOf("/",n+2);return-1===e?t:t.slice(0,e)}function Ot(t){let n=0;if(K(t)){const e=t.indexOf("//");-1!==e&&(n=e+2)}const e=t.lastIndexOf("/");return e<n?t:t.slice(0,e+1)}function Ut(t,n){if(!t)return"";const e=j(t).path.replace(/\/+$/,""),r=e.substring(e.lastIndexOf("/")+1);if(!n?.length)return r;const o=new RegExp(`.(${n.join("|")})$`,"ig");return r.replace(o,"")}function bt(t){return t&&"/"===t[t.length-1]?t:`${t}/`}function Ct(t){return t.replace(/\/+$/,"")}function qt(t){if(/^https?:\/\//i.test(t)){const n=P(t);t=(t=n.path.replace(/\/{2,}/g,"/")).replace("/","//"),n.query&&(t+=`?${n.query}`)}return t}function Rt(t){return t.replace(/^(https?:\/\/)(arcgis\.com)/i,"$1www.$2")}function jt(t){const n=u.httpsDomains;if(!pt(t))return t;const e=t.indexOf("/",7);let r;if(r=-1===e?t:t.slice(0,e),r=r.toLowerCase().slice(7),p.test(r)){if(!r.endsWith(":80"))return t;r=r.slice(0,-3),t=t.replace(":80","")}return yt()&&r===w.authority&&!d.test(t)||($t()&&r===w.authority||n&&n.some((t=>r===t||r.endsWith(`.${t}`)))||$t()&&!J(t))&&(t=mt(t)),t}function Lt(t,n,e){if(!(n&&e&&t&&K(t)))return t;const r=t.indexOf("//"),o=t.indexOf("/",r+2),i=t.indexOf(":",r+2),s=Math.min(o<0?t.length:o,i<0?t.length:i);if(t.slice(r+2,s).toLowerCase()!==n.toLowerCase())return t;return`${t.slice(0,r+2)}${e}${t.slice(s)}`}function vt(t){return"string"==typeof t?new y(Q(t)):(t.scheme||(t.scheme=w.scheme),t)}function It(t){return Nt.test(t)}function At(t,n){const e=j(t),r=Object.keys(e.query||{});return r.length>0&&n&&n.warn("removeQueryParameters()",`Url query parameters are not supported, the following parameters have been removed: ${r.join(", ")}.`),e.path}function St(t,n,e){const r=j(t),o=r.query||{};return o[n]=String(e),`${r.path}?${I(o)}`}function Bt(t,n){const e=j(t),r=e.query||{};for(const i in n)r[i]=n[i];const o=I(r);return o?`${e.path}?${o}`:e.path}function Pt(t,n){const{path:e,query:r}=j(t);if(!r)return t;delete r[n];const o=I(r);return o?`${e}?${o}`:e}function kt(t){if(i(t))return null;const n=t.match(Jt);return n?n[2]:null}function Et(t){if(i(t))return null;const n=t.match(Jt);return n?{path:n[1],extension:n[2]}:{path:t,extension:null}}const Jt=/([^.]*)\.([^\/]*)$/,Nt=/(^data:image\/svg|\.svg$)/i;export{y as Url,S as addProxy,E as addProxyRule,St as addQueryParameter,Bt as addQueryParameters,Z as base64UrlEncode,Lt as changeDomain,nt as dataComponents,Y as dataToArrayBuffer,rt as dataToBlob,it as downloadBlobAsFile,ot as downloadDataAsFile,C as getAppBaseUrl,b as getAppUrl,Ut as getFilename,W as getInterceptor,H as getOrigin,kt as getPathExtension,J as getProxyRule,A as getProxyUrl,at as hasProtocol,z as hasSameOrigin,N as hasSamePortal,K as isAbsolute,$t as isAppHTTPS,V as isBlobProtocol,X as isDataProtocol,ht as isHTTPSProtocol,ft as isProtocolRelative,It as isSVG,D as isTrustedServer,G as join,Q as makeAbsolute,et as makeData,_ as makeRelative,F as normalize,I as objectToQuery,L as queryToObject,Ot as removeFile,Pt as removeQueryParameter,At as removeQueryParameters,Ct as removeTrailingSlash,Et as splitPathExtension,R as test,gt as toHTTP,mt as toHTTPS,$ as trustedServersUrlCache,j as urlToObject};
|