1import Constants, { ExecutionEnvironment } from 'expo-constants'; 2import * as Linking from 'expo-linking'; 3import URL from 'url-parse'; 4// This is only run on native. 5function extractExactPathFromURL(url) { 6 if ( 7 // If a universal link / app link / web URL is used, we should use the path 8 // from the URL, while stripping the origin. 9 url.match(/^https?:\/\//)) { 10 const { origin, href } = new URL(url); 11 return href.replace(origin, ''); 12 } 13 // Handle special URLs used in Expo Go: `/--/pathname` -> `pathname` 14 if (Constants.executionEnvironment === ExecutionEnvironment.StoreClient && 15 // while not exhaustive, `exp` and `exps` are the only two schemes which 16 // are passed through to other apps in Expo Go. 17 url.match(/^exp(s)?:\/\//)) { 18 const pathname = url.match(/exps?:\/\/.*?\/--\/(.*)/)?.[1]; 19 if (pathname) { 20 return fromDeepLink('a://' + pathname); 21 } 22 const res = Linking.parse(url); 23 const qs = !res.queryParams 24 ? '' 25 : Object.entries(res.queryParams) 26 .map(([k, v]) => `${k}=${v}`) 27 .join('&'); 28 return (adjustPathname({ hostname: res.hostname, pathname: res.path || '' }) + (qs ? '?' + qs : '')); 29 } 30 // TODO: Support dev client URLs 31 return fromDeepLink(url); 32} 33/** Major hack to support the makeshift expo-development-client system. */ 34function isExpoDevelopmentClient(url) { 35 return !!url.hostname.match(/^expo-development-client$/); 36} 37function fromDeepLink(url) { 38 // This is for all standard deep links, e.g. `foobar://` where everything 39 // after the `://` is the path. 40 const res = new URL(url, true); 41 if (isExpoDevelopmentClient(res)) { 42 if (!res.query || !res.query.url) { 43 return ''; 44 } 45 const incomingUrl = res.query.url; 46 return extractExactPathFromURL(decodeURI(incomingUrl)); 47 } 48 const qs = !res.query 49 ? '' 50 : Object.entries(res.query) 51 .map(([k, v]) => `${k}=${decodeURIComponent(v)}`) 52 .join('&'); 53 let results = ''; 54 if (res.host) { 55 results += res.host; 56 } 57 if (res.pathname) { 58 results += res.pathname; 59 } 60 if (qs) { 61 results += '?' + qs; 62 } 63 return results; 64} 65export function extractExpoPathFromURL(url = '') { 66 // TODO: We should get rid of this, dropping specificities is not good 67 return extractExactPathFromURL(url).replace(/^\//, ''); 68} 69export function adjustPathname(url) { 70 if (url.hostname === 'exp.host' || url.hostname === 'u.expo.dev') { 71 // drop the first two segments from pathname: 72 return url.pathname.split('/').slice(2).join('/'); 73 } 74 return url.pathname; 75} 76//# sourceMappingURL=extractPathFromURL.js.map