UNPKG

one

Version:

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

368 lines (367 loc) 12.7 kB
import escape from "escape-string-regexp"; import { getParamValue, isDynamicPart, replacePart } from "./_shared.mjs"; import { findFocusedRoute } from "./findFocusedRoute.mjs"; import { appendIsInitial, createConfigItemAdditionalProperties, decodeURIComponentSafe, formatRegexPattern, getRouteConfigSorter, getUrlWithReactNavigationConcessions, matchForEmptyPath, parseQueryParamsExtended, populateParams } from "./getStateFromPath-mods.mjs"; import { validatePathConfig } from "./validatePathConfig.mjs"; function getStateFromPath(path, options) { resetRouteKeyCounter(); if (process.env.ONE_DEBUG_ROUTER) { console.info(`[one] getStateFromPath called with path=${path}, isServer=${typeof window === "undefined"}`); } const { initialRoutes, configs, configWithRegexes } = getConfigResources(options); const screens = options?.screens; const pathData = getUrlWithReactNavigationConcessions(path); let remaining = pathData.nonstandardPathname.replace(/\/+/g, "/").replace(/^\//, "").replace(/\?.*$/, ""); remaining = remaining.endsWith("/") ? remaining : `${remaining}/`; const prefix = options?.path?.replace(/^\//, ""); if (prefix) { const normalizedPrefix = prefix.endsWith("/") ? prefix : `${prefix}/`; if (!remaining.startsWith(normalizedPrefix)) { return void 0; } remaining = remaining.replace(normalizedPrefix, ""); } if (screens === void 0) { const routes2 = remaining.split("/").filter(Boolean).map(segment => { const name = decodeURIComponent(segment); return { name }; }); if (routes2.length) { return createNestedStateObject(pathData, routes2, initialRoutes, []); } return void 0; } if (remaining === "/") { const match = matchForEmptyPath(configWithRegexes); if (match) { return createNestedStateObject(pathData, // @modified: pass pathData instead of path match.routeNames.map(name => ({ name })), initialRoutes, configs); } return void 0; } let result; let current; const { routes, remainingPath } = matchAgainstConfigs(remaining, configWithRegexes); if (routes !== void 0) { current = createNestedStateObject(pathData, routes, initialRoutes, configs); remaining = remainingPath; result = current; } if (current == null || result == null) { return void 0; } return result; } const cachedConfigResources = /* @__PURE__ */new WeakMap(); function getConfigResources(options) { if (!options) return prepareConfigResources(); const cached = cachedConfigResources.get(options); if (cached) return cached; const resources = prepareConfigResources(options); cachedConfigResources.set(options, resources); return resources; } function prepareConfigResources(options, previousSegments) { if (options) { validatePathConfig(options); } const initialRoutes = getInitialRoutes(options); const configs = getNormalizedConfigs(initialRoutes, options?.screens, previousSegments); checkForDuplicatedConfigs(configs); const configWithRegexes = getConfigsWithRegexes(configs); return { initialRoutes, configs, configWithRegexes }; } function getInitialRoutes(options) { const initialRoutes = []; if (options?.initialRouteName) { initialRoutes.push({ initialRouteName: options.initialRouteName, parentScreens: [] }); } return initialRoutes; } function getNormalizedConfigs(initialRoutes, screens = {}, previousSegments) { return [].concat(...Object.keys(screens).map(key => createNormalizedConfigs(key, screens, [], initialRoutes, []))).map(appendIsInitial(initialRoutes)).sort(getRouteConfigSorter(previousSegments)); } function checkForDuplicatedConfigs(configs) { configs.reduce((acc, config) => { if (acc[config.pattern]) { const a = acc[config.pattern].routeNames; const b = config.routeNames; const intersects = a.length > b.length ? b.every((it, i) => a[i] === it) : a.every((it, i) => b[i] === it); if (!intersects) { throw new Error(`Found conflicting screens with the same pattern. The pattern '${config.pattern}' resolves to both '${a.join(" > ")}' and '${b.join(" > ")}'. Patterns must be unique and cannot resolve to more than one screen.`); } } return Object.assign(acc, { [config.pattern]: config }); }, {}); } function getConfigsWithRegexes(configs) { return configs.map(c => ({ ...c, // Add `$` to the regex to make sure it matches till end of the path and not just beginning // @modified - start // regex: c.regex ? new RegExp(c.regex.source + '$') : undefined, regex: c.pattern ? new RegExp(`^(${c.pattern.split("/").map(formatRegexPattern).join("")})$`) : void 0 // @modified - end })); } const joinPaths = (...paths) => [].concat(...paths.map(p => p.split("/"))).filter(Boolean).join("/"); const matchAgainstConfigs = (remaining, configs) => { let routes; let remainingPath = remaining; const allParams = /* @__PURE__ */Object.create(null); for (const config of configs) { if (!config.regex) { continue; } const match = remainingPath.match(config.regex); if (match) { const matchResult = config.pattern?.split("/").reduce((acc, p, index) => { if (!isDynamicPart(p)) { return acc; } acc.pos += 1; const decodedParamSegment = decodeURIComponentSafe( // @modified: use decodeURIComponent**Safe** // The param segments appear every second item starting from 2 in the regex match result match[(acc.pos + 1) * 2].replace(/\/$/, "")); Object.assign(acc.matchedParams, { [p]: Object.assign(acc.matchedParams[p] || {}, { [index]: decodedParamSegment }) }); return acc; }, { pos: -1, matchedParams: {} }); const matchedParams = matchResult.matchedParams || {}; routes = config.routeNames.map(name => { const routeConfig = configs.find(c => { return c.screen === name && config.pattern.startsWith(c.pattern); }); const normalizedPath = routeConfig?.path.split("/").filter(Boolean).join("/"); const numInitialSegments = routeConfig?.pattern.replace(new RegExp(`${escape(normalizedPath)}$`), "")?.split("/").length; const params = normalizedPath?.split("/").reduce((acc, p, index) => { if (!isDynamicPart(p)) { return acc; } const offset = numInitialSegments ? numInitialSegments - 1 : 0; const value = getParamValue(p, matchedParams[p]?.[index + offset]); if (value) { const key = replacePart(p); acc[key] = routeConfig?.parse?.[key] ? routeConfig.parse[key](value) : value; } return acc; }, {}); if (params && Object.keys(params).length) { Object.assign(allParams, params); return { name, params }; } return { name }; }); remainingPath = remainingPath.replace(match[1], ""); break; } } populateParams(routes, allParams); return { routes, remainingPath }; }; const createNormalizedConfigs = (screen, routeConfig, routeNames = [], initials, parentScreens, parentPattern) => { const configs = []; routeNames.push(screen); parentScreens.push(screen); const config = routeConfig[screen]; if (typeof config === "string") { const pattern = parentPattern ? joinPaths(parentPattern, config) : config; configs.push(createConfigItem(screen, routeNames, pattern, config)); } else if (typeof config === "object") { let pattern; if (typeof config.path === "string") { if (config.exact && config.path === void 0) { throw new Error("A 'path' needs to be specified when specifying 'exact: true'. If you don't want this screen in the URL, specify it as empty string, e.g. `path: ''`."); } pattern = config.exact !== true ? joinPaths(parentPattern || "", config.path || "") : config.path || ""; configs.push(createConfigItem(screen, routeNames, pattern, config.path, config.parse, config)); } if (config.screens) { if (config.initialRouteName) { initials.push({ initialRouteName: config.initialRouteName, parentScreens }); } Object.keys(config.screens).forEach(nestedConfig => { const result = createNormalizedConfigs(nestedConfig, config.screens, routeNames, initials, [...parentScreens], pattern ?? parentPattern); configs.push(...result); }); } } routeNames.pop(); return configs; }; const createConfigItem = (screen, routeNames, pattern, path, parse = void 0, config = {}) => { pattern = pattern.split("/").filter(Boolean).join("/"); const regex = pattern ? new RegExp(`^(${pattern.split("/").map(it => { if (it.startsWith(":")) { return `(([^/]+\\/)${it.endsWith("?") ? "?" : ""})`; } return `${it === "*" ? ".*" : escape(it)}\\/`; }).join("")})`) : void 0; return { screen, regex, pattern, path, // The routeNames array is mutated, so copy it to keep the current state routeNames: [...routeNames], parse, // @modified - start ...createConfigItemAdditionalProperties(screen, pattern, routeNames, config) // @modified - end }; }; const findParseConfigForRoute = (routeName, flatConfig) => { for (const config of flatConfig) { if (routeName === config.routeNames[config.routeNames.length - 1]) { return config.parse; } } return void 0; }; const findInitialRoute = (routeName, parentScreens, initialRoutes) => { for (const config of initialRoutes) { if (parentScreens.length === config.parentScreens.length) { let sameParents = true; for (let i = 0; i < parentScreens.length; i++) { if (parentScreens[i].localeCompare(config.parentScreens[i]) !== 0) { sameParents = false; break; } } if (sameParents) { return routeName !== config.initialRouteName ? config.initialRouteName : void 0; } } } return void 0; }; let routeKeyCounter = 0; function resetRouteKeyCounter() { routeKeyCounter = 0; } function getRouteWithKey(route) { const key = `${route.name}-${routeKeyCounter++}`; if (process.env.ONE_DEBUG_ROUTER) { console.info(`[one] getRouteWithKey: ${route.name} -> key=${key}`); } return { ...route, key }; } const createStateObject = (initialRoute, route, isEmpty) => { if (isEmpty) { if (initialRoute) { return { index: 1, // @modified: add deterministic keys routes: [getRouteWithKey({ name: initialRoute }), getRouteWithKey(route)] }; } else { return { // @modified: add deterministic keys routes: [getRouteWithKey(route)] }; } } else { if (initialRoute) { return { index: 1, // @modified: add deterministic keys routes: [getRouteWithKey({ name: initialRoute }), getRouteWithKey({ ...route, state: { routes: [] } })] }; } else { return { // @modified: add deterministic keys routes: [getRouteWithKey({ ...route, state: { routes: [] } })] }; } } }; const createNestedStateObject = ({ path, ...restPathData }, routes, initialRoutes, flatConfig) => { let route = routes.shift(); const parentScreens = []; let initialRoute = findInitialRoute(route.name, parentScreens, initialRoutes); parentScreens.push(route.name); const state = createStateObject(initialRoute, route, routes.length === 0); if (routes.length > 0) { let nestedState = state; while (route = routes.shift()) { initialRoute = findInitialRoute(route.name, parentScreens, initialRoutes); const nestedStateIndex = nestedState.index || nestedState.routes.length - 1; nestedState.routes[nestedStateIndex].state = createStateObject(initialRoute, route, routes.length === 0); if (routes.length > 0) { nestedState = nestedState.routes[nestedStateIndex].state; } parentScreens.push(route.name); } } route = findFocusedRoute(state); route.path = restPathData.pathWithoutGroups; const params = parseQueryParamsExtended(path, route, flatConfig ? findParseConfigForRoute(route.name, flatConfig) : void 0, restPathData.hash); if (params) { route.params = { ...route.params, ...params }; } return state; }; export { getStateFromPath }; //# sourceMappingURL=getStateFromPath.mjs.map