{"version":3,"file":"group-routes-by-phase.cjs","sources":["../../../src/router/utils/group-routes-by-phase.ts"],"sourcesContent":["import type { Phase, Route, RoutesGroupedByPhase, SourceRoute } from '../types';\nimport { isHandlerRoute } from './is-handler-route';\n\nconst append = (str: string, suffix: string) => (str.endsWith(suffix) ? str : `${str}${suffix}`);\nconst prepend = (str: string, prefix: string) => (str.startsWith(prefix) ? str : `${prefix}${str}`);\n\n/**\n * Given a source route it normalizes its src value if needed.\n *\n * (In this context normalization means tweaking the src value so that it follows\n * a format which Vercel expects).\n *\n * Note: this function applies the change side-effectfully to the route object.\n *\n * @param route Route which src we want to potentially normalize.\n */\nconst normalizeRouteSrc = (route: SourceRoute): SourceRoute => {\n\tif (!route.src) return route;\n\n\t// we rely on locale root routes pointing to '/' to perform runtime checks\n\t// so we cannot normalize such src values as that would break things later on\n\t// see: https://github.com/cloudflare/next-on-pages/blob/654545/packages/next-on-pages/templates/_worker.js/routes-matcher.ts#L353-L358\n\tif (route.locale && route.src === '/') return route;\n\n\treturn {\n\t\t...route,\n\t\t// route src should always end start with `^` and end with `$`.\n\t\t// see: https://github.com/vercel/vercel/blob/ea5bc88/packages/routing-utils/src/index.ts#L77\n\t\t// see: https://github.com/vercel/vercel/blob/ea5bc88/packages/routing-utils/src/index.ts#L82\n\t\tsrc: append(prepend(route.src, '^'), '$'),\n\t};\n};\n\n/**\n * Groups the routing rules from the build output config by the phases that each route falls under.\n *\n * Defaults to `none` for rules that are not under a phase.\n *\n * @param routes Routing rules from the build output config.\n * @returns Grouped routing rules.\n */\nexport const groupRoutesByPhase = (routes: Route[]) => {\n\tconst grouped: RoutesGroupedByPhase = {\n\t\tnone: [],\n\t\tfilesystem: [],\n\t\tmiss: [],\n\t\trewrite: [],\n\t\tresource: [],\n\t\thit: [],\n\t\terror: [],\n\t};\n\n\tlet currentPhase: Phase = 'none';\n\n\tfor (const route of routes) {\n\t\tif (isHandlerRoute(route)) {\n\t\t\tcurrentPhase = route.handle;\n\t\t} else {\n\t\t\tgrouped[currentPhase].push(normalizeRouteSrc(route));\n\t\t}\n\t}\n\n\treturn grouped;\n};\n"],"names":["append","str","suffix","prepend","prefix","normalizeRouteSrc","route","groupRoutesByPhase","routes","grouped","currentPhase","isHandlerRoute"],"mappings":"0HAGMA,EAAS,CAACC,EAAaC,IAAoBD,EAAI,SAASC,CAAM,EAAID,EAAM,GAAGA,CAAG,GAAGC,CAAM,GACvFC,EAAU,CAACF,EAAaG,IAAoBH,EAAI,WAAWG,CAAM,EAAIH,EAAM,GAAGG,CAAM,GAAGH,CAAG,GAY1FI,EAAqBC,GACtB,CAACA,EAAM,KAKPA,EAAM,QAAUA,EAAM,MAAQ,IAAYA,EAEvC,CACN,GAAGA,EAIH,IAAKN,EAAOG,EAAQG,EAAM,IAAK,GAAG,EAAG,GAAG,CAAA,EAY7BC,EAAsBC,GAAoB,CACtD,MAAMC,EAAgC,CACrC,KAAM,CAAC,EACP,WAAY,CAAC,EACb,KAAM,CAAC,EACP,QAAS,CAAC,EACV,SAAU,CAAC,EACX,IAAK,CAAC,EACN,MAAO,CAAC,CAAA,EAGT,IAAIC,EAAsB,OAE1B,UAAWJ,KAASE,EACfG,EAAAA,eAAeL,CAAK,EACvBI,EAAeJ,EAAM,OAErBG,EAAQC,CAAY,EAAE,KAAKL,EAAkBC,CAAK,CAAC,EAI9C,OAAAG,CACR"}