UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 11.4 kB
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";import{parseKnownArcGISOnlineDomain as s}from"../portal/support/urlUtils.js";const u=e.getLogger("esri.core.urlUtils"),l=t.request,c="esri/config: esriConfig.request.proxyUrl is not set.",f=/^\s*[a-z][a-z0-9-+.]*:(?![0-9])/i,a=/^\s*http:/i,p=/^\s*https:/i,h=/^\s*file:/i,d=/:\d+$/,m=/^https?:\/\/[^/]+\.arcgis.com\/sharing(\/|$)/i,g=new RegExp("^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?$"),y=new RegExp("^((([^\\[:]+):)?([^@]+)@)?(\\[([^\\]]+)\\]|([^\\[:]*))(:([0-9]+))?$");class ${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(y)),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={},w=new $(t.applicationUrl);let O=w;const U=R();let b=U;const C=()=>O,q=()=>b;function R(){const t=r(O.path),n=t.substring(0,t.lastIndexOf(t.split("/")[t.split("/").length-1]));return`${`${O.scheme}://${O.host}${null!=O.port?`:${O.port}`:""}`}${n}`}const j={setAppUrl:t=>O=t,setAppBaseUrl:t=>b=t,restoreUrls:()=>{O=w,b=U}};function L(t){if(!t)return null;const n={path:null,query:null},e=new $(t),r=t.indexOf("?");return null===e.query?n.path=t:(n.path=t.substring(0,r),n.query=v(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 v(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 I(t){return t&&"object"==typeof t&&"toJSON"in t&&"function"==typeof t.toJSON}function A(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=>I(t)?o+encodeURIComponent(JSON.stringify(t)):o+encodeURIComponent(t))).join("&"):I(r)?o+encodeURIComponent(JSON.stringify(r)):o+encodeURIComponent(r)})).filter((t=>t)).join("&"):""}function S(t=!1){let e,r=l.proxyUrl;if("string"==typeof t){e=mt(t);const n=J(t);n&&(r=n.proxyUrl)}else e=!!t;if(!r)throw u.warn(c),new n("urlutils:proxy-not-set",c);e&&Ot()&&(r=xt(r));return L(r)}function B(t){const n=J(t);let e,r;if(n){const t=k(n.proxyUrl);e=t.path,r=t.query?v(t.query):null}if(e){const n=L(t);t=e+"?"+n.path;const o=A({...r,...n.query});o&&(t=`${t}?${o}`)}return t}const P={path:"",query:""};function k(t){const n=t.indexOf("?");return-1!==n?(P.path=t.slice(0,n),P.query=t.slice(n+1)):(P.path=t,P.query=null),P}function E(t){return t=(t=Ut(t=Rt(t=k(t).path),!0)).toLowerCase()}function H(t){const n={proxyUrl:t.proxyUrl,urlPrefix:E(t.urlPrefix)},e=l.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=l.proxyRules,e=E(t);for(let r=0;r<n.length;r++)if(0===e.indexOf(n[r].urlPrefix))return n[r]}function N(t,n){if(!t||!n)return!1;t=z(t),n=z(n);const e=s(t),r=s(n);return o(e)&&o(r)?e.portalHostname===r.portalHostname:!o(e)&&!o(r)&&M(t,n,!0)}function T(t,n){if(!t||!n)return!1;t=z(t),n=z(n);const e=s(t),r=s(n);return!(!o(e)||!o(r))&&e.portalHostname===r.portalHostname}function W(t,n){return t=z(t),n=z(n),Ut(t)===Ut(n)}function z(t){const n=(t=K(t)).indexOf("/sharing");return n>0?t.substring(0,n):t.replace(/\/+$/,"")}function D(t){const n=n=>null==n||n instanceof RegExp&&n.test(t)||"string"==typeof n&&t.startsWith(n),e=l.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 M(t,n,e=!1){if(!t||!n)return!1;const r=St(t),o=St(n);return!(!e&&r.scheme!==o.scheme)&&(null!=r.host&&null!=o.host&&(r.host.toLowerCase()===o.host.toLowerCase()&&r.port===o.port))}function Q(t){if("string"==typeof t){if(!Y(t))return!0;t=St(t)}if(M(t,O))return!0;const n=l.trustedServers||[];for(let e=0;e<n.length;e++){const r=_(n[e]);for(let n=0;n<r.length;n++)if(M(t,r[n]))return!0}return!1}function _(t){return x[t]||(dt(t)||ht(t)?x[t]=[new $(F(t))]:x[t]=[new $(`http://${t}`),new $(`https://${t}`)]),x[t]}function F(t,n=b,e){return ht(t)?e&&e.preserveProtocolRelative?t:"http"===O.scheme&&O.authority===X(t).slice(2)?`http:${t}`:`https:${t}`:dt(t)?t:r(V("/"===t[0]?bt(n):n,t))}function G(t,n=b,e){if(null==t||!Y(t))return t;const r=K(t),o=r.toLowerCase(),i=K(n).toLowerCase().replace(/\/+$/,""),s=e?K(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 K(t){return t=It(t=vt(t=Lt(t=F(t=t.trim()))))}function V(...t){const n=t.filter(o);if(!n||!n.length)return;const e=[];if(Y(n[0])){const t=n[0],r=t.indexOf("//");-1!==r&&(e.push(t.slice(0,r+1)),yt(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 X(t,n=!1){if(null==t||Z(t)||tt(t))return null;let e=t.indexOf("://");if(-1===e&&ht(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=Ut(t,!0)),t}function Y(t){return ht(t)||dt(t)}function Z(t){return null!=t&&"blob:"===t.slice(0,5)}function tt(t){return null!=t&&"data:"===t.slice(0,5)}function nt(t){const n=ot(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 et(t){return btoa(String.fromCharCode.apply(null,t)).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}const rt=/^data:(.*?)(;base64)?,(.*)$/;function ot(t){const n=t.match(rt);if(!n)return null;const[,e,r,o]=n;return{mediaType:e,isBase64:!!r,data:o}}function it(t){return t.isBase64?`data:${t.mediaType};base64,${t.data}`:`data:${t.mediaType},${t.data}`}function st(t){const n=nt(t);if(!n)return null;const e=ot(t);return new Blob([n],{type:e.mediaType})}function ut(t,n){ct(t,n)||at(t,n)}function lt(t,n){ft(t,n)||pt(t,n)}function ct(t,n){const e=st(t);return!!e&&ft(e,n)}function ft(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 at(t,n){const e=st(t);return!!e&&pt(e,n)}function pt(t,n){return!!window.navigator.msSaveOrOpenBlob&&window.navigator.msSaveOrOpenBlob(t,n)}function ht(t){return null!=t&&"/"===t[0]&&"/"===t[1]}function dt(t){return null!=t&&f.test(t)}function mt(t){return null!=t&&p.test(t)||"https"===O.scheme&&ht(t)}function gt(t){return null!=t&&a.test(t)||"http"===O.scheme&&ht(t)}function yt(t){return null!=t&&h.test(t)}function $t(t){return ht(t)?`http:${t}`:t.replace(p,"http:")}function xt(t){return ht(t)?`https:${t}`:t.replace(a,"https:")}function wt(){return"http"===O.scheme}function Ot(){return"https"===O.scheme}function Ut(t,n=!1){return ht(t)?t.slice(2):(t=t.replace(f,""),n&&t.length>1&&"/"===t[0]&&"/"===t[1]&&(t=t.slice(2)),t)}function bt(t){const n=t.indexOf("//"),e=t.indexOf("/",n+2);return-1===e?t:t.slice(0,e)}function Ct(t){let n=0;if(Y(t)){const e=t.indexOf("//");-1!==e&&(n=e+2)}const e=t.lastIndexOf("/");return e<n?t:t.slice(0,e+1)}function qt(t,n){if(!t)return"";const e=L(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 Rt(t){return t&&"/"===t[t.length-1]?t:`${t}/`}function jt(t){return t.replace(/\/+$/,"")}function Lt(t){if(/^https?:\/\//i.test(t)){const n=k(t);t=(t=n.path.replace(/\/{2,}/g,"/")).replace("/","//"),n.query&&(t+=`?${n.query}`)}return t}function vt(t){return t.replace(/^(https?:\/\/)(arcgis\.com)/i,"$1www.$2")}function It(t){const n=l.httpsDomains;if(!gt(t))return t;const e=t.indexOf("/",7);let r;if(r=-1===e?t:t.slice(0,e),r=r.toLowerCase().slice(7),d.test(r)){if(!r.endsWith(":80"))return t;r=r.slice(0,-3),t=t.replace(":80","")}return wt()&&r===O.authority&&!m.test(t)||(Ot()&&r===O.authority||n&&n.some((t=>r===t||r.endsWith(`.${t}`)))||Ot()&&!J(t))&&(t=xt(t)),t}function At(t,n,e){if(!(n&&e&&t&&Y(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 St(t){return"string"==typeof t?new $(F(t)):(t.scheme||(t.scheme=O.scheme),t)}function Bt(t){return Wt.test(t)}function Pt(t,n){const e=L(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 kt(t,n,e){const r=L(t),o=r.query||{};return o[n]=String(e),`${r.path}?${A(o)}`}function Et(t,n){const e=L(t),r=e.query||{};for(const i in n)r[i]=n[i];const o=A(r);return o?`${e.path}?${o}`:e.path}function Ht(t,n){const{path:e,query:r}=L(t);if(!r)return t;delete r[n];const o=A(r);return o?`${e}?${o}`:e}function Jt(t){if(i(t))return null;const n=t.match(Tt);return n?n[2]:null}function Nt(t){if(i(t))return null;const n=t.match(Tt);return n?{path:n[1],extension:n[2]}:{path:t,extension:null}}const Tt=/([^.]*)\.([^\/]*)$/,Wt=/(^data:image\/svg|\.svg$)/i;export{$ as Url,B as addProxy,H as addProxyRule,kt as addQueryParameter,Et as addQueryParameters,et as base64UrlEncode,At as changeDomain,ot as dataComponents,nt as dataToArrayBuffer,st as dataToBlob,lt as downloadBlobAsFile,ut as downloadDataAsFile,q as getAppBaseUrl,C as getAppUrl,qt as getFilename,D as getInterceptor,X as getOrigin,Jt as getPathExtension,J as getProxyRule,S as getProxyUrl,dt as hasProtocol,T as hasSameCanonicalArcGISOnlinePortal,N as hasSameCanonicalPortal,M as hasSameOrigin,W as hasSamePortal,Y as isAbsolute,Ot as isAppHTTPS,Z as isBlobProtocol,tt as isDataProtocol,mt as isHTTPSProtocol,ht as isProtocolRelative,Bt as isSVG,Q as isTrustedServer,V as join,F as makeAbsolute,it as makeData,G as makeRelative,K as normalize,A as objectToQuery,v as queryToObject,Ct as removeFile,Ht as removeQueryParameter,Pt as removeQueryParameters,jt as removeTrailingSlash,Nt as splitPathExtension,j as test,$t as toHTTP,xt as toHTTPS,x as trustedServersUrlCache,L as urlToObject};