import parse from "url-parse" ; const url = require ( "url" ) ; function isClient ( ) { return eval ( 'typeof window !== "undefined"' ) ; } function relativeURLToWindow ( relativeURL ) { const currentURL = new URL ( window . location . href ) ; return url . resolve ( currentURL , relativeURL ) ; } const EXPOSED_FRONTEND_PORT = process . env . PORT ? parseInt ( process . env . PORT , 10 ) : 3000 ; const FRONTEND_URL = process . env . FRONTEND_URL ? process . env . FRONTEND_URL : "" ; const LOCAL_BACKEND_URL = process . env . BACKEND_URL ? process . env . BACKEND_URL : isClient ( ) ? relativeURLToWindow ( "/server/" ) : `http://localhost:${ EXPOSED_FRONTEND_PORT }/server/` ; const clean = ( url ) => String ( url ) . replace ( /^\/|\/$/g , "" ) ; export const isLocal = ( url ) => String ( url ) . startsWith ( "chrome://" ) || String ( url ) . startsWith ( "chrome-extension://" ) ; export const isOfCrusherExtension = ( url ) => Boolean ( url ) && clean ( url ) . startsWith ( clean ( chrome . runtime . getURL ( "/" ) ) ) ; export const origins = ( url ) => { const { hostname } = parse ( url ) ; return [ `https://${ hostname }` , `http://${ hostname }` ] ; } ; export const addHttpToURLIfNotThere = ( uri ) => { if ( ! uri . startsWith ( "http://" ) && ! uri . startsWith ( "https://" ) ) { return `http://${ uri }` ; } return uri ; } ; export const getQueryStringParams = function getParameterByName ( name , url ) { if ( ! url ) url = window . location . href ; name = name . replace ( /[\[\]]/g , "\\$&" ) ; const regex = new RegExp ( `[?&]${ name }(=([^&#]*)|&|#|$)` ) ; const results = regex . exec ( url ) ; if ( ! results ) return null ; if ( ! results [ 2 ] ) return "" ; return decodeURIComponent ( results [ 2 ] . replace ( /\+/g , " " ) ) ; } ; export const resolveToBackendPath = ( relativePath , customBasePath = null ) => { const basePath = customBasePath ? customBasePath : LOCAL_BACKEND_URL ; return url . resolve ( basePath , relativePath ) ; } ; export const resolveToFrontEndPath = ( relativePath , customBasePath = null ) => { const backendURL = new URL ( customBasePath ? customBasePath : FRONTEND_URL ) ; return url . resolve ( backendURL . origin , relativePath ) ; } ;