-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
WIP: refactor router tests #15510
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
matthewp
wants to merge
3
commits into
main
Choose a base branch
from
refactor-analysis
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,391
−2,203
Draft
WIP: refactor router tests #15510
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| import type { AstroConfig } from '../../types/public/config.js'; | ||
| import type { Params } from '../../types/public/common.js'; | ||
| import type { RouteData } from '../../types/public/internal.js'; | ||
| import { getParams } from '../render/params-and-props.js'; | ||
| import { routeComparator } from './priority.js'; | ||
|
|
||
| export interface RouterOptions { | ||
| base: AstroConfig['base']; | ||
| trailingSlash: AstroConfig['trailingSlash']; | ||
| buildFormat: 'directory' | 'file' | 'preserve'; | ||
| } | ||
|
|
||
| export interface RouterMatchRoute { | ||
| type: 'match'; | ||
| route: RouteData; | ||
| params: Params; | ||
| pathname: string; | ||
| } | ||
|
|
||
| export interface RouterMatchRedirect { | ||
| type: 'redirect'; | ||
| location: string; | ||
| status: 301 | 308; | ||
| } | ||
|
|
||
| export interface RouterMatchNone { | ||
| type: 'none'; | ||
| reason: 'no-match' | 'outside-base'; | ||
| } | ||
|
|
||
| export type RouterMatch = RouterMatchRoute | RouterMatchRedirect | RouterMatchNone; | ||
|
|
||
| export class Router { | ||
| #routes: RouteData[]; | ||
| #base: string; | ||
| #baseWithoutTrailingSlash: string; | ||
| #buildFormat: RouterOptions['buildFormat']; | ||
| #trailingSlash: RouterOptions['trailingSlash']; | ||
|
|
||
| constructor(routes: RouteData[], options: RouterOptions) { | ||
| this.#routes = [...routes].sort(routeComparator); | ||
| this.#base = normalizeBase(options.base); | ||
| this.#baseWithoutTrailingSlash = removeTrailingSlash(this.#base); | ||
| this.#buildFormat = options.buildFormat; | ||
| this.#trailingSlash = options.trailingSlash; | ||
| } | ||
|
|
||
| public match(inputPathname: string): RouterMatch { | ||
| const normalized = normalizePathname(inputPathname); | ||
| if (normalized.redirect) { | ||
| return { type: 'redirect', location: normalized.redirect, status: 301 }; | ||
| } | ||
|
|
||
| const baseResult = stripBase( | ||
| normalized.pathname, | ||
| this.#base, | ||
| this.#baseWithoutTrailingSlash, | ||
| this.#trailingSlash, | ||
| ); | ||
| if (!baseResult) { | ||
| return { type: 'none', reason: 'outside-base' }; | ||
| } | ||
|
|
||
| let pathname = baseResult; | ||
| if (this.#buildFormat === 'file') { | ||
| pathname = normalizeFileFormatPathname(pathname); | ||
| } | ||
|
|
||
| const route = this.#routes.find((candidate) => { | ||
| if (candidate.pattern.test(pathname)) return true; | ||
| return candidate.fallbackRoutes.some((fallbackRoute) => fallbackRoute.pattern.test(pathname)); | ||
| }); | ||
|
|
||
| if (!route) { | ||
| return { type: 'none', reason: 'no-match' }; | ||
| } | ||
|
|
||
| const params = getParams(route, pathname); | ||
| return { type: 'match', route, params, pathname }; | ||
| } | ||
| } | ||
|
|
||
| function normalizeBase(base: string): string { | ||
| if (!base) return '/'; | ||
| if (base === '/') return '/'; | ||
| if (!base.startsWith('/')) return `/${base}`; | ||
| return base; | ||
| } | ||
|
|
||
| function removeTrailingSlash(value: string): string { | ||
| if (value === '/') return '/'; | ||
| return value.endsWith('/') ? value.slice(0, -1) : value; | ||
| } | ||
|
|
||
| function normalizePathname(pathname: string): { pathname: string; redirect?: string } { | ||
| let value = pathname; | ||
| if (!value.startsWith('/')) { | ||
| value = `/${value}`; | ||
| } | ||
|
|
||
| if (value.startsWith('//')) { | ||
| return { pathname: value, redirect: '/' }; | ||
| } | ||
|
|
||
| return { pathname: value }; | ||
| } | ||
|
|
||
| function stripBase( | ||
| pathname: string, | ||
| base: string, | ||
| baseWithoutTrailingSlash: string, | ||
| trailingSlash: RouterOptions['trailingSlash'], | ||
| ): string | null { | ||
| if (base === '/') return pathname; | ||
| const baseWithSlash = `${baseWithoutTrailingSlash}/`; | ||
|
|
||
| if (pathname === baseWithoutTrailingSlash || pathname === base) { | ||
| return trailingSlash === 'always' ? null : '/'; | ||
| } | ||
| if (pathname === baseWithSlash) { | ||
| return trailingSlash === 'never' ? null : '/'; | ||
| } | ||
| if (pathname.startsWith(baseWithSlash)) { | ||
| return pathname.slice(baseWithoutTrailingSlash.length); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| function normalizeFileFormatPathname(pathname: string): string { | ||
| if (pathname.endsWith('/index.html')) { | ||
| const trimmed = pathname.slice(0, -'/index.html'.length); | ||
| return trimmed === '' ? '/' : trimmed; | ||
| } | ||
|
|
||
| if (pathname.endsWith('.html')) { | ||
| const trimmed = pathname.slice(0, -'.html'.length); | ||
| return trimmed === '' ? '/' : trimmed; | ||
| } | ||
|
|
||
| return pathname; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not correct
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be in the manifest I think.