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 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 { match } from 'next/dist/compiled/path-to-regexp'
import { import {
matchHas, matchHas,
...@@ -20,6 +20,7 @@ import type { BaseNextRequest } from '../../../server/base-http' ...@@ -20,6 +20,7 @@ import type { BaseNextRequest } from '../../../server/base-http'
import type { Params } from '../../../server/request/params' import type { Params } from '../../../server/request/params'
import { constructRequest } from './utils' import { constructRequest } from './utils'
import { parsedUrlQueryToParams } from '../../../server/route-modules/app-route/helpers/parsed-url-query-to-params' 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 * 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/ ...@@ -29,13 +30,10 @@ import { parsedUrlQueryToParams } from '../../../server/route-modules/app-route/
function matchRoute( function matchRoute(
route: ManifestHeaderRoute | ManifestRedirectRoute | ManifestRewriteRoute, route: ManifestHeaderRoute | ManifestRedirectRoute | ManifestRewriteRoute,
request: BaseNextRequest, request: BaseNextRequest,
parsedUrl: UrlWithParsedQuery pathname: string,
query: ParsedUrlQuery
): Params | undefined { ): Params | undefined {
const pathname = parsedUrl.pathname const regexMatches = pathname.match(route.regex)
if (!pathname) {
return
}
const regexMatches = pathname?.match(route.regex)
if (regexMatches) { if (regexMatches) {
const pathMatch = match<Params>(route.source)(pathname) const pathMatch = match<Params>(route.source)(pathname)
...@@ -45,7 +43,7 @@ function matchRoute( ...@@ -45,7 +43,7 @@ function matchRoute(
) )
} }
if (route.has || route.missing) { if (route.has || route.missing) {
if (!matchHas(request, parsedUrl.query, route.has, route.missing)) { if (!matchHas(request, query, route.has, route.missing)) {
return return
} }
} }
...@@ -88,7 +86,9 @@ export async function unstable_getResponseFromNextConfig({ ...@@ -88,7 +86,9 @@ export async function unstable_getResponseFromNextConfig({
headers?: IncomingHttpHeaders headers?: IncomingHttpHeaders
cookies?: Record<string, string> cookies?: Record<string, string>
}): Promise<NextResponse> { }): 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 request = constructRequest({ url, headers, cookies })
const resolvedConfig = await normalizeConfig( const resolvedConfig = await normalizeConfig(
PHASE_PRODUCTION_BUILD, PHASE_PRODUCTION_BUILD,
...@@ -110,7 +110,7 @@ export async function unstable_getResponseFromNextConfig({ ...@@ -110,7 +110,7 @@ export async function unstable_getResponseFromNextConfig({
const respHeaders: Record<string, string> = {} const respHeaders: Record<string, string> = {}
for (const route of headerRoutes) { for (const route of headerRoutes) {
const matched = matchRoute(route, request, parsedUrl) const matched = matchRoute(route, request, pathname, query)
if (matched) { if (matched) {
for (const header of route.headers) { for (const header of route.headers) {
respHeaders[header.key] = header.value respHeaders[header.key] = header.value
...@@ -120,7 +120,7 @@ export async function unstable_getResponseFromNextConfig({ ...@@ -120,7 +120,7 @@ export async function unstable_getResponseFromNextConfig({
function matchRouteAndGetDestination( function matchRouteAndGetDestination(
route: ManifestRedirectRoute | ManifestRewriteRoute route: ManifestRedirectRoute | ManifestRewriteRoute
): URL | undefined { ): URL | undefined {
const params = matchRoute(route, request, parsedUrl) const params = matchRoute(route, request, pathname, query)
if (!params) { if (!params) {
return return
} }
...@@ -128,7 +128,7 @@ export async function unstable_getResponseFromNextConfig({ ...@@ -128,7 +128,7 @@ export async function unstable_getResponseFromNextConfig({
appendParamsToQuery: false, appendParamsToQuery: false,
destination: route.destination, destination: route.destination,
params, params,
query: parsedUrl.query, query,
}) })
const searchParams = new URLSearchParams( const searchParams = new URLSearchParams(
parsedUrlQueryToParams(parsedDestination.query) as Record<string, string> parsedUrlQueryToParams(parsedDestination.query) as Record<string, string>
...@@ -137,9 +137,7 @@ export async function unstable_getResponseFromNextConfig({ ...@@ -137,9 +137,7 @@ export async function unstable_getResponseFromNextConfig({
searchParams.size > 0 ? `${newUrl}?${searchParams.toString()}` : newUrl, searchParams.size > 0 ? `${newUrl}?${searchParams.toString()}` : newUrl,
parsedDestination.hostname parsedDestination.hostname
? `${parsedDestination.protocol}//${parsedDestination.hostname}` ? `${parsedDestination.protocol}//${parsedDestination.hostname}`
: parsedUrl.host : parsed.origin
? `${parsedUrl.protocol}//${parsedUrl.host}`
: 'https://example.com'
) )
} }
for (const route of redirectRoutes) { 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