UNPKG

one

Version:

One is a React framework for web, iOS, and Android apps.

1 lines • 14 kB
{"version":3,"file":"getServerManifest.js","names":[],"sources":["esm/server/getServerManifest.js"],"sourcesContent":["import { getContextKey, matchGroupName } from \"../router/matchers\";\nfunction isNotFoundRoute(route) {\n return Boolean(route.dynamic && route.dynamic[route.dynamic.length - 1].notFound);\n}\nfunction sortByPathSpecificity([keyA, nodeA], [keyB, nodeB]) {\n const partsA = keyA.split(\"/\").filter(Boolean);\n const partsB = keyB.split(\"/\").filter(Boolean);\n const minLen = Math.min(partsA.length, partsB.length);\n for (let i = 0; i < minLen; i++) {\n const aIsDynamic = partsA[i].startsWith(\"[\") || partsA[i] === \"+not-found\";\n const bIsDynamic = partsB[i].startsWith(\"[\") || partsB[i] === \"+not-found\";\n if (!aIsDynamic && bIsDynamic) return -1;\n if (aIsDynamic && !bIsDynamic) return 1;\n if (aIsDynamic && bIsDynamic) {\n const aIsCatchAll = partsA[i].startsWith(\"[...\") || partsA[i] === \"+not-found\";\n const bIsCatchAll = partsB[i].startsWith(\"[...\") || partsB[i] === \"+not-found\";\n if (!aIsCatchAll && bIsCatchAll) return -1;\n if (aIsCatchAll && !bIsCatchAll) return 1;\n if (aIsCatchAll && bIsCatchAll) {\n const aNotFound2 = partsA[i] === \"+not-found\";\n const bNotFound2 = partsB[i] === \"+not-found\";\n if (aNotFound2 && !bNotFound2) return 1;\n if (!aNotFound2 && bNotFound2) return -1;\n }\n }\n }\n const aNotFound = isNotFoundRoute(nodeA);\n const bNotFound = isNotFoundRoute(nodeB);\n if (aNotFound && !bNotFound) return 1;\n if (!aNotFound && bNotFound) return -1;\n if (partsA.length !== partsB.length) {\n return partsB.length - partsA.length;\n }\n return 0;\n}\nfunction getServerManifest(route) {\n function getFlatNodes(route2, layouts) {\n if (route2.children.length) {\n return route2.children.flatMap((child) => {\n return getFlatNodes(child, [...layouts || [], route2]);\n });\n }\n let key;\n if (route2.type === \"api\") {\n key = getContextKey(route2.contextKey).replace(/\\/index$/, \"\") || \"/\";\n } else {\n const parentSegments = layouts?.flatMap((route3) => {\n const key2 = getContextKey(route3.route).replace(/\\/index$/, \"\") || \"/\";\n if (key2 === \"/\" || key2.startsWith(\"/(\")) {\n return [];\n }\n return [key2];\n });\n key = (parentSegments ? parentSegments.join(\"\") : \"\") + getContextKey(route2.route).replace(/\\/index$/, \"\") || \"/\";\n }\n return [[key, { ...route2, layouts }]];\n }\n const flat = getFlatNodes(route).sort(sortByPathSpecificity);\n const pathToRoute = {};\n for (const [path, route2] of flat) {\n if (pathToRoute[path]) {\n console.warn(`\n[one] \\u274C Duplicate routes error`);\n console.warn(\n ` Multiple routes at the same path! One route will always win over the other.`\n );\n console.warn(` path: ${path}`);\n console.warn(` first route: ${pathToRoute[path].contextKey}`);\n console.warn(` second route: ${route2.contextKey}\n`);\n }\n pathToRoute[path] = route2;\n }\n const apiRoutes = [];\n const middlewareRoutes = [];\n const pageRoutes = [];\n const allRoutes = [];\n const addedMiddlewares = {};\n for (const [path, node] of flat) {\n if (node.middlewares?.length) {\n for (const middleware of node.middlewares) {\n if (!addedMiddlewares[middleware.contextKey]) {\n addedMiddlewares[middleware.contextKey] = true;\n middlewareRoutes.push(getGeneratedNamedRouteRegex(path, middleware));\n }\n }\n }\n if (node.type === \"api\") {\n const route3 = getGeneratedNamedRouteRegex(path, node);\n apiRoutes.push(route3);\n allRoutes.push(route3);\n continue;\n }\n const route2 = getGeneratedNamedRouteRegex(path, node);\n pageRoutes.push(route2);\n allRoutes.push(route2);\n }\n return {\n apiRoutes,\n middlewareRoutes,\n pageRoutes,\n allRoutes\n };\n}\nfunction getGeneratedNamedRouteRegex(normalizedRoute, node) {\n return {\n ...getRouteEntry(normalizedRoute, node),\n generated: true,\n isNotFound: isNotFoundRoute(node)\n };\n}\nfunction getRouteEntry(normalizedRoute, node) {\n const result = getPathMeta(normalizedRoute);\n return {\n file: node.contextKey,\n page: getContextKey(node.route),\n type: node.type,\n namedRegex: result.namedRegex,\n urlPath: result.urlPath,\n urlCleanPath: result.urlCleanPath,\n routeKeys: result.routeKeys,\n layouts: node.layouts,\n middlewares: node.middlewares\n };\n}\nfunction buildGetSafeRouteKey() {\n let currentCharCode = 96;\n let currentLength = 1;\n return () => {\n let result = \"\";\n let incrementNext = true;\n for (let i = 0; i < currentLength; i++) {\n if (incrementNext) {\n currentCharCode++;\n if (currentCharCode > 122) {\n currentCharCode = 97;\n incrementNext = true;\n } else {\n incrementNext = false;\n }\n }\n result = String.fromCharCode(currentCharCode) + result;\n }\n if (incrementNext) {\n currentLength++;\n currentCharCode = 96;\n }\n return result;\n };\n}\nfunction removeTrailingSlash(route) {\n return route.replace(/\\/$/, \"\") || \"/\";\n}\nfunction getPathMeta(route) {\n const segments = removeTrailingSlash(route).slice(1).split(\"/\");\n const getSafeRouteKey = buildGetSafeRouteKey();\n const routeKeys = {};\n const urlPathParts = [];\n const routeSegments = segments.map((segment, index) => {\n if (segment === \"+not-found\" && index === segments.length - 1) {\n segment = \"[...not-found]\";\n }\n if (/^\\[.*\\]$/.test(segment)) {\n const { name, optional, repeat } = parseParam(segment);\n let cleanedKey = name.replace(/\\W/g, \"\");\n let invalidKey = false;\n if (cleanedKey.length === 0 || cleanedKey.length > 30) {\n invalidKey = true;\n }\n if (!Number.isNaN(Number.parseInt(cleanedKey.slice(0, 1), 10))) {\n invalidKey = true;\n }\n if (cleanedKey in routeKeys) {\n invalidKey = true;\n }\n if (invalidKey) {\n cleanedKey = getSafeRouteKey();\n }\n urlPathParts.push({\n content: repeat ? \"/*\" : `/:${name}${optional ? \"?\" : \"\"}`\n });\n routeKeys[cleanedKey] = name;\n return repeat ? optional ? `(?:/(?<${cleanedKey}>.+?))?` : `/(?<${cleanedKey}>.+?)` : `/(?<${cleanedKey}>(?!\\\\+not-found$)[^/]+?)`;\n }\n if (insideParensRegex.test(segment)) {\n const groupName = matchGroupName(segment).split(\",\").map((group) => group.trim()).filter(Boolean);\n urlPathParts.push({ content: `/:${groupName}?`, type: \"group\" });\n if (groupName.length > 1) {\n const optionalSegment = `\\\\((?:${groupName.map(escapeStringRegexp).join(\"|\")})\\\\)`;\n return `(?:/${optionalSegment})?`;\n }\n return `(?:/${escapeStringRegexp(segment)})?`;\n }\n urlPathParts.push({ content: `/${segment}` });\n return `/${escapeStringRegexp(segment)}`;\n }).join(\"\");\n let urlPath = urlPathParts.map((p) => p.content).join(\"\");\n let urlCleanPath = urlPathParts.filter((p) => p.type !== \"group\").map((p) => p.content).join(\"\");\n if (urlPath.endsWith(\"/*\")) {\n urlPath = urlPath.replace(/\\?(?=\\/|\\/\\*)/g, \"\");\n }\n if (urlCleanPath.endsWith(\"/*\")) {\n urlCleanPath = urlCleanPath.replace(/\\?(?=\\/|\\/\\*)/g, \"\");\n }\n return {\n namedRegex: `^${routeSegments}(?:/)?$`,\n urlPath: urlPath === \"\" ? \"/\" : urlPath,\n urlCleanPath: urlCleanPath === \"\" ? \"/\" : urlCleanPath,\n routeKeys\n };\n}\nconst insideBracketsRegex = /^\\[.*\\]$/;\nconst insideParensRegex = /^\\(.*\\)$/;\nconst tripleDotRegex = /^\\.\\.\\./;\nconst replaceRegex = /[|\\\\{}()[\\]^$+*?.-]/g;\nconst hasRegExpRegex = /[|\\\\{}()[\\]^$+*?.-]/;\nfunction escapeStringRegexp(str) {\n if (hasRegExpRegex.test(str)) {\n return str.replace(replaceRegex, \"\\\\$&\");\n }\n return str;\n}\nfunction parseParam(param) {\n let repeat = false;\n let optional = false;\n let name = param;\n if (/^\\[\\[.*\\]\\]$/.test(name)) {\n optional = true;\n name = name.slice(2, -2);\n } else if (insideBracketsRegex.test(name)) {\n name = name.slice(1, -1);\n }\n if (tripleDotRegex.test(name)) {\n repeat = true;\n name = name.slice(3);\n }\n return { name, repeat, optional };\n}\nexport {\n getServerManifest,\n parseParam\n};\n//# sourceMappingURL=getServerManifest.js.map\n"],"mappings":";;;AACA,SAAS,gBAAgB,OAAO;CAC9B,OAAO,QAAQ,MAAM,WAAW,MAAM,QAAQ,MAAM,QAAQ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