UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

112 lines (111 loc) 3.66 kB
function createRouteMask(options) { const { from, to, params = !0, unmaskOnReload = !1, useSearchParam = !1 } = options, fromParams = [], fromRegexStr = from.split("/").map((segment) => { if (segment.startsWith("[...") && segment.endsWith("]")) { const paramName = segment.slice(4, -1); return fromParams.push(paramName), "(.+)"; } if (segment.startsWith("$...")) { const paramName = segment.slice(4); return fromParams.push(paramName), "(.+)"; } if (segment.startsWith("[") && segment.endsWith("]")) { const paramName = segment.slice(1, -1); return fromParams.push(paramName), "([^/]+)"; } if (segment.startsWith("$")) { const paramName = segment.slice(1); return fromParams.push(paramName), "([^/]+)"; } return segment.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); }).join("/"); return { from, to, params, unmaskOnReload, useSearchParam, _fromRegex: new RegExp(`^${fromRegexStr}$`), _fromParams: fromParams }; } function matchRouteMask(pathname, mask) { const match = pathname.match(mask._fromRegex); if (!match) return null; const params = {}; return mask._fromParams.forEach((paramName, index) => { params[paramName] = match[index + 1]; }), params; } function buildMaskedPath(mask, matchedParams) { let params; return mask.params === !1 ? params = {} : typeof mask.params == "function" ? params = mask.params(matchedParams) : params = matchedParams, mask.to.split("/").map((segment) => { if (segment.startsWith("[...") && segment.endsWith("]")) { const paramName = segment.slice(4, -1); return params[paramName] ?? ""; } if (segment.startsWith("$...")) { const paramName = segment.slice(4); return params[paramName] ?? ""; } if (segment.startsWith("[") && segment.endsWith("]")) { const paramName = segment.slice(1, -1); return params[paramName] ?? ""; } if (segment.startsWith("$")) { const paramName = segment.slice(1); return params[paramName] ?? ""; } return segment; }).join("/"); } function encodeUnmask(path) { return btoa(path).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""); } function decodeUnmask(encoded) { const base64 = encoded.replace(/-/g, "+").replace(/_/g, "/"); return atob(base64); } const UNMASK_SEPARATOR = "__"; function parseUnmaskFromPath(pathname) { const lastSlash = pathname.lastIndexOf("/"), lastSegment = pathname.slice(lastSlash + 1), separatorIndex = lastSegment.indexOf(UNMASK_SEPARATOR); if (separatorIndex === -1) return null; const encoded = lastSegment.slice(separatorIndex + UNMASK_SEPARATOR.length); if (!encoded) return null; try { return decodeUnmask(encoded); } catch { return null; } } function findMatchingMask(pathname, routeMasks) { for (const mask of routeMasks) { const matchedParams = matchRouteMask(pathname, mask); if (matchedParams) { const maskedPath = buildMaskedPath(mask, matchedParams), useSearchParam = mask.useSearchParam ?? !1; return { // If useSearchParam is true, append base64-encoded actual path as pathname postfix // e.g. /photos/3__L3Bob3Rvcy8zL21vZGFs maskedPath: useSearchParam ? `${maskedPath}${UNMASK_SEPARATOR}${encodeUnmask(pathname)}` : maskedPath, unmaskOnReload: mask.unmaskOnReload ?? !1, useSearchParam, actualPath: pathname }; } } } export { buildMaskedPath, createRouteMask, decodeUnmask, encodeUnmask, findMatchingMask, matchRouteMask, parseUnmaskFromPath }; //# sourceMappingURL=routeMask.js.map