Skip to content

Commit 50b2b25

Browse files
committed
refactor: extract getAppProxyBasePathFromRequestValue helper
The orpc server's public-base-path detection in `src/node/orpc/server.ts` repeated the pattern `parsePathnameFromRequestValue(value) → getAppProxyBasePathFromPathname(...)` across four callsites (forwarded headers, originalUrl/url loop, referer header, and the direct app-proxy handler-prefix calculator). Extract a single `getAppProxyBasePathFromRequestValue` helper that performs the two-step normalize-then-classify operation, then call it from every site. Behavior-preserving: each callsite still produces null when the value is absent or yields an invalid pathname, and otherwise returns the same parsed app-proxy base path. All 52 tests in `src/node/orpc/server.test.ts` continue to pass.
1 parent 8efd50d commit 50b2b25

1 file changed

Lines changed: 15 additions & 14 deletions

File tree

src/node/orpc/server.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,13 @@ function getPathnameFromRequestUrl(requestUrl: string | undefined): string | nul
509509
return parsePathnameFromRequestValue(requestUrl);
510510
}
511511

512+
// Combines pathname parsing + app-proxy prefix detection so request-value
513+
// shaped inputs (raw URLs, header values) can be classified in a single call.
514+
function getAppProxyBasePathFromRequestValue(value: string | null | undefined): string | null {
515+
const pathname = parsePathnameFromRequestValue(value);
516+
return pathname ? getAppProxyBasePathFromPathname(pathname) : null;
517+
}
518+
512519
function getRequestPublicBasePath(
513520
req: PublicBasePathRequest,
514521
options: { allowReferer?: boolean } = {}
@@ -519,18 +526,16 @@ function getRequestPublicBasePath(
519526
}
520527

521528
for (const headerName of ["x-forwarded-uri", "x-original-uri", "x-original-url"] as const) {
522-
const headerPathname = parsePathnameFromRequestValue(getFirstHeaderValue(req, headerName));
523-
const headerBasePath = headerPathname ? getAppProxyBasePathFromPathname(headerPathname) : null;
529+
const headerBasePath = getAppProxyBasePathFromRequestValue(
530+
getFirstHeaderValue(req, headerName)
531+
);
524532
if (headerBasePath) {
525533
return headerBasePath;
526534
}
527535
}
528536

529537
for (const requestValue of [req.originalUrl, req.url]) {
530-
const requestPathname = parsePathnameFromRequestValue(requestValue);
531-
const requestBasePath = requestPathname
532-
? getAppProxyBasePathFromPathname(requestPathname)
533-
: null;
538+
const requestBasePath = getAppProxyBasePathFromRequestValue(requestValue);
534539
if (requestBasePath) {
535540
return requestBasePath;
536541
}
@@ -539,10 +544,9 @@ function getRequestPublicBasePath(
539544
// Browser mode requests include Referer by default, so this keeps cookie scope
540545
// and generated app links aligned when a proxy strips the public prefix.
541546
if (options.allowReferer) {
542-
const refererPathname = parsePathnameFromRequestValue(getFirstHeaderValue(req, "referer"));
543-
const refererBasePath = refererPathname
544-
? getAppProxyBasePathFromPathname(refererPathname)
545-
: null;
547+
const refererBasePath = getAppProxyBasePathFromRequestValue(
548+
getFirstHeaderValue(req, "referer")
549+
);
546550
if (refererBasePath) {
547551
return refererBasePath;
548552
}
@@ -583,10 +587,7 @@ function getDirectAppProxyHandlerPrefix(
583587
req: express.Request,
584588
routePrefix: `/${string}`
585589
): `/${string}` {
586-
const originalPathname = parsePathnameFromRequestValue(req.originalUrl);
587-
const directBasePath = originalPathname
588-
? getAppProxyBasePathFromPathname(originalPathname)
589-
: null;
590+
const directBasePath = getAppProxyBasePathFromRequestValue(req.originalUrl);
590591
return directBasePath
591592
? (joinPublicBasePath(directBasePath, routePrefix) as `/${string}`)
592593
: routePrefix;

0 commit comments

Comments
 (0)