4d3b2ea426
Part of #31506, this PR moves the code of middleware handling from the base server to the node server. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `yarn lint`
33 lines
799 B
TypeScript
33 lines
799 B
TypeScript
import type { ParsedUrlQuery } from 'querystring'
|
|
|
|
import { searchParamsToUrlQuery } from './querystring'
|
|
import { parseRelativeUrl } from './parse-relative-url'
|
|
|
|
export interface ParsedUrl {
|
|
hash: string
|
|
hostname?: string | null
|
|
href: string
|
|
pathname: string
|
|
port?: string | null
|
|
protocol?: string | null
|
|
query: ParsedUrlQuery
|
|
search: string
|
|
}
|
|
|
|
export function parseUrl(url: string): ParsedUrl {
|
|
if (url.startsWith('/')) {
|
|
return parseRelativeUrl(url)
|
|
}
|
|
|
|
const parsedURL = new URL(url)
|
|
return {
|
|
hash: parsedURL.hash,
|
|
hostname: parsedURL.hostname,
|
|
href: parsedURL.href,
|
|
pathname: parsedURL.pathname,
|
|
port: parsedURL.port,
|
|
protocol: parsedURL.protocol,
|
|
query: searchParamsToUrlQuery(parsedURL.searchParams),
|
|
search: parsedURL.search,
|
|
}
|
|
}
|