UNPKG

one

Version:

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

206 lines (205 loc) 8.14 kB
import { getContextKey, matchGroupName } from "../router/matchers.native.js"; import { sortRoutes } from "../router/sortRoutes.native.js"; function isNotFoundRoute(route) { return !!(route.dynamic && route.dynamic[route.dynamic.length - 1].notFound); } function getServerManifest(route) { function getFlatNodes(route2, layouts) { if (route2.children.length) return route2.children.flatMap(function (child) { return getFlatNodes(child, [...(layouts || []), route2]); }); var key; if (route2.type === "api") key = getContextKey(route2.contextKey).replace(/\/index$/, "") || "/";else { var parentSegments = layouts?.flatMap(function (route3) { var key2 = getContextKey(route3.route).replace(/\/index$/, "") || "/"; return key2 === "/" || key2.startsWith("/(") ? [] : [key2]; }); key = (parentSegments ? parentSegments.join("") : "") + getContextKey(route2.route).replace(/\/index$/, "") || "/"; } return [[key, { ...route2, layouts }]]; } var flat = getFlatNodes(route).sort(function (param, param1) { var [, a] = param, [, b] = param1; return sortRoutes(b, a); }).reverse(), pathToRoute = {}, _iteratorNormalCompletion = !0, _didIteratorError = !1, _iteratorError = void 0; try { for (var _iterator = flat[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = !0) { var [path, _$route] = _step.value; pathToRoute[path] && (console.warn(` [one] \u274C Duplicate routes error`), console.warn(" Multiple routes at the same path! One route will always win over the other."), console.warn(` path: ${path}`), console.warn(` first route: ${pathToRoute[path].contextKey}`), console.warn(` second route: ${_$route.contextKey} `)), pathToRoute[path] = _$route; } } catch (err) { _didIteratorError = !0, _iteratorError = err; } finally { try { !_iteratorNormalCompletion && _iterator.return != null && _iterator.return(); } finally { if (_didIteratorError) throw _iteratorError; } } var apiRoutes = [], middlewareRoutes = [], pageRoutes = [], allRoutes = [], addedMiddlewares = {}, _iteratorNormalCompletion1 = !0, _didIteratorError1 = !1, _iteratorError1 = void 0; try { for (var _iterator1 = flat[Symbol.iterator](), _step1; !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = !0) { var [path1, node] = _step1.value, _node_middlewares; if (node.type === "api") { var _$route1 = getGeneratedNamedRouteRegex(path1, node); apiRoutes.push(_$route1), allRoutes.push(_$route1); continue; } if (!((_node_middlewares = node.middlewares) === null || _node_middlewares === void 0) && _node_middlewares.length) { var _iteratorNormalCompletion2 = !0, _didIteratorError2 = !1, _iteratorError2 = void 0; try { for (var _iterator2 = node.middlewares[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = !0) { var middleware = _step2.value; addedMiddlewares[middleware.contextKey] || (addedMiddlewares[middleware.contextKey] = !0, middlewareRoutes.push(getGeneratedNamedRouteRegex(path1, middleware))); } } catch (err) { _didIteratorError2 = !0, _iteratorError2 = err; } finally { try { !_iteratorNormalCompletion2 && _iterator2.return != null && _iterator2.return(); } finally { if (_didIteratorError2) throw _iteratorError2; } } } var _$route2 = getGeneratedNamedRouteRegex(path1, node); pageRoutes.push(_$route2), allRoutes.push(_$route2); } } catch (err) { _didIteratorError1 = !0, _iteratorError1 = err; } finally { try { !_iteratorNormalCompletion1 && _iterator1.return != null && _iterator1.return(); } finally { if (_didIteratorError1) throw _iteratorError1; } } return { apiRoutes, middlewareRoutes, pageRoutes, allRoutes }; } function getGeneratedNamedRouteRegex(normalizedRoute, node) { return { ...getRouteEntry(normalizedRoute, node), generated: !0, isNotFound: isNotFoundRoute(node) }; } function getRouteEntry(normalizedRoute, node) { var result = getPathMeta(normalizedRoute); return { file: node.contextKey, page: getContextKey(node.route), type: node.type, namedRegex: result.namedRegex, urlPath: result.urlPath, urlCleanPath: result.urlCleanPath, routeKeys: result.routeKeys, layouts: node.layouts, middlewares: node.middlewares }; } function buildGetSafeRouteKey() { var currentCharCode = 96, currentLength = 1; return function () { for (var result = "", incrementNext = !0, i = 0; i < currentLength; i++) incrementNext && (currentCharCode++, currentCharCode > 122 ? (currentCharCode = 97, incrementNext = !0) : incrementNext = !1), result = String.fromCharCode(currentCharCode) + result; return incrementNext && (currentLength++, currentCharCode = 96), result; }; } function removeTrailingSlash(route) { return route.replace(/\/$/, "") || "/"; } function getPathMeta(route) { var segments = removeTrailingSlash(route).slice(1).split("/"), getSafeRouteKey = buildGetSafeRouteKey(), routeKeys = {}, urlPathParts = [], routeSegments = segments.map(function (segment, index) { if (segment === "+not-found" && index === segments.length - 1 && (segment = "[...not-found]"), /^\[.*\]$/.test(segment)) { var { name, optional, repeat } = parseParam(segment), cleanedKey = name.replace(/\W/g, ""), invalidKey = !1; return (cleanedKey.length === 0 || cleanedKey.length > 30) && (invalidKey = !0), Number.isNaN(Number.parseInt(cleanedKey.slice(0, 1), 10)) || (invalidKey = !0), cleanedKey in routeKeys && (invalidKey = !0), invalidKey && (cleanedKey = getSafeRouteKey()), urlPathParts.push({ content: repeat ? "/*" : `/:${name}${optional ? "?" : ""}` }), routeKeys[cleanedKey] = name, repeat ? optional ? `(?:/(?<${cleanedKey}>.+?))?` : `/(?<${cleanedKey}>.+?)` : `/(?<${cleanedKey}>[^/]+?)`; } if (insideParensRegex.test(segment)) { var groupName = matchGroupName(segment).split(",").map(function (group) { return group.trim(); }).filter(Boolean); if (urlPathParts.push({ content: `/:${groupName}?`, type: "group" }), groupName.length > 1) { var optionalSegment = `\\((?:${groupName.map(escapeStringRegexp).join("|")})\\)`; return `(?:/${optionalSegment})?`; } return `(?:/${escapeStringRegexp(segment)})?`; } return urlPathParts.push({ content: `/${segment}` }), `/${escapeStringRegexp(segment)}`; }).join(""), urlPath = urlPathParts.map(function (p) { return p.content; }).join(""), urlCleanPath = urlPathParts.filter(function (p) { return p.type !== "group"; }).map(function (p) { return p.content; }).join(""); return { namedRegex: `^${routeSegments}(?:/)?$`, urlPath: urlPath === "" ? "/" : urlPath, urlCleanPath: urlCleanPath === "" ? "/" : urlCleanPath, routeKeys }; } var insideBracketsRegex = /^\[.*\]$/, insideParensRegex = /^\(.*\)$/, tripleDotRegex = /^\.\.\./, replaceRegex = /[|\\{}()[\]^$+*?.-]/g, hasRegExpRegex = /[|\\{}()[\]^$+*?.-]/; function escapeStringRegexp(str) { return hasRegExpRegex.test(str) ? str.replace(replaceRegex, "\\$&") : str; } function parseParam(param) { var repeat = !1, optional = !1, name = param; return insideBracketsRegex.test(name) && (optional = !0, name = name.slice(1, -1)), tripleDotRegex.test(name) && (repeat = !0, name = name.slice(3)), { name, repeat, optional }; } export { getServerManifest, parseParam }; //# sourceMappingURL=getServerManifest.native.js.map