Unverified Commit da6e8b65 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[testmode] Stop using deprecated `url.parse` (#98194)

`unstable_getResponseFromNextConfig` parsed the request URL with the
deprecated `url.parse()` (DEP0169)

This change constructs the same pathname/query/protocol/host shape from
the WHATWG `URL` API instead, keeping `protocol` and `host` null for
relative URLs so the existing rewrite base fallback keeps working. Query
parsing goes through the existing `searchParamsToUrlQuery` helper, which
decodes `+` as a space where the legacy parser left it untouched; we
accept that edge-case difference in this testing utility.
parent dcbfff77
import type { IncomingHttpHeaders } from 'node:http'
import { parse, type UrlWithParsedQuery } from 'node:url'
import type { ParsedUrlQuery } from 'querystring'
import { match } from 'next/dist/compiled/path-to-regexp'
import {
matchHas,
......@@ -20,6 +20,7 @@ import type { BaseNextRequest } from '../../../server/base-http'
import type { Params } from '../../../server/request/params'
import { constructRequest } from './utils'
import { parsedUrlQueryToParams } from '../../../server/route-modules/app-route/helpers/parsed-url-query-to-params'
import { searchParamsToUrlQuery } from '../../../shared/lib/router/utils/querystring'
/**
* Tries to match the current request against the provided route. If there is
......@@ -29,13 +30,10 @@ import { parsedUrlQueryToParams } from '../../../server/route-modules/app-route/
function matchRoute(
route: ManifestHeaderRoute | ManifestRedirectRoute | ManifestRewriteRoute,
request: BaseNextRequest,
parsedUrl: UrlWithParsedQuery
pathname: string,
query: ParsedUrlQuery
): Params | undefined {
const pathname = parsedUrl.pathname
if (!pathname) {
return
}
const regexMatches = pathname?.match(route.regex)
const regexMatches = pathname.match(route.regex)
if (regexMatches) {
const pathMatch = match<Params>(route.source)(pathname)
......@@ -45,7 +43,7 @@ function matchRoute(
)
}
if (route.has || route.missing) {
if (!matchHas(request, parsedUrl.query, route.has, route.missing)) {
if (!matchHas(request, query, route.has, route.missing)) {
return
}
}
......@@ -88,7 +86,9 @@ export async function unstable_getResponseFromNextConfig({
headers?: IncomingHttpHeaders
cookies?: Record<string, string>
}): Promise<NextResponse> {
const parsedUrl = parse(url, true)
const parsed = new URL(url, 'https://example.com')
const pathname = parsed.pathname
const query = searchParamsToUrlQuery(parsed.searchParams)
const request = constructRequest({ url, headers, cookies })
const resolvedConfig = await normalizeConfig(
PHASE_PRODUCTION_BUILD,
......@@ -110,7 +110,7 @@ export async function unstable_getResponseFromNextConfig({
const respHeaders: Record<string, string> = {}
for (const route of headerRoutes) {
const matched = matchRoute(route, request, parsedUrl)
const matched = matchRoute(route, request, pathname, query)
if (matched) {
for (const header of route.headers) {
respHeaders[header.key] = header.value
......@@ -120,7 +120,7 @@ export async function unstable_getResponseFromNextConfig({
function matchRouteAndGetDestination(
route: ManifestRedirectRoute | ManifestRewriteRoute
): URL | undefined {
const params = matchRoute(route, request, parsedUrl)
const params = matchRoute(route, request, pathname, query)
if (!params) {
return
}
......@@ -128,7 +128,7 @@ export async function unstable_getResponseFromNextConfig({
appendParamsToQuery: false,
destination: route.destination,
params,
query: parsedUrl.query,
query,
})
const searchParams = new URLSearchParams(
parsedUrlQueryToParams(parsedDestination.query) as Record<string, string>
......@@ -137,9 +137,7 @@ export async function unstable_getResponseFromNextConfig({
searchParams.size > 0 ? `${newUrl}?${searchParams.toString()}` : newUrl,
parsedDestination.hostname
? `${parsedDestination.protocol}//${parsedDestination.hostname}`
: parsedUrl.host
? `${parsedUrl.protocol}//${parsedUrl.host}`
: 'https://example.com'
: parsed.origin
)
}
for (const route of redirectRoutes) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment