one
Version:
One is a new React Framework that makes Vite serve both native and web.
91 lines (90 loc) • 2.73 kB
JavaScript
function parsePathAndParamsFromExpoGoLink(url) {
const href = parsePathFromExpoGoLink(url);
const results = href.match(/([^?]*)(\?.*)?/);
return {
pathname: results?.[1] ?? "",
queryString: results?.[2] ?? ""
};
}
function parsePathFromExpoGoLink(url) {
return url.match(/exps?:\/\/.*?\/--\/(.*)/)?.[1] ?? "";
}
function extractExactPathFromURL(url) {
if (
// If a universal link / app link / web URL is used, we should use the path
// from the URL, while stripping the origin.
url.match(/^https?:\/\//)) {
const {
origin,
href,
hostname
} = new URL(url);
if (hostname === "exp.host" || hostname === "u.expo.dev") {
return "";
}
return href.replace(origin, "");
}
const isExpoGo = typeof globalThis.expo !== "undefined" && globalThis.expo?.modules?.ExpoGo;
if (isExpoGo &&
// while not exhaustive, `exp` and `exps` are the only two schemes which
// are passed through to other apps in Expo Go.
url.match(/^exp(s)?:\/\//)) {
const pathname = parsePathFromExpoGoLink(url);
if (pathname) {
return fromDeepLink("a://" + pathname);
}
const queryParams = url.match(/exps?:\/\/.*\?(.*)/)?.[1];
if (queryParams) {
return fromDeepLink("a://?" + queryParams);
}
return "";
}
return fromDeepLink(url);
}
function isExpoDevelopmentClient(url) {
return url.hostname === "expo-development-client";
}
function fromDeepLink(url) {
let res;
try {
res = new URL(url);
} catch {
if (url.startsWith("/")) {
return url;
}
return url.replace(/^[^:]+:\/\//, "");
}
if (isExpoDevelopmentClient(res)) {
if (!res.searchParams.get("url")) {
return "";
}
const incomingUrl = res.searchParams.get("url");
return extractExactPathFromURL(decodeURI(incomingUrl));
}
let results = "";
if (res.host) {
results += res.host;
}
if (res.pathname) {
results += res.pathname;
}
const qs = !res.search ? "" :
// @ts-ignore: `entries` is not on `URLSearchParams` in some typechecks.
[...res.searchParams.entries()].map(([k, v]) => `${k}=${decodeURIComponent(v)}`).join("&");
if (qs) {
results += "?" + qs;
}
return results;
}
function extractExpoPathFromURL(_prefixes, url = "") {
return extractExactPathFromURL(url).replace(/^\//, "");
}
function adjustPathname(url) {
if (url.hostname === "exp.host" || url.hostname === "u.expo.dev") {
return url.pathname.split("/").slice(2).join("/");
}
return url.pathname;
}
const extractPathFromURL = extractExpoPathFromURL;
export { adjustPathname, extractExpoPathFromURL, extractPathFromURL, parsePathAndParamsFromExpoGoLink, parsePathFromExpoGoLink };
//# sourceMappingURL=extractPathFromURL.mjs.map