@stacksjs/rpx
Version:
A modern and smart reverse proxy.
47 lines (46 loc) • 2.14 kB
TypeScript
import type { PathRewriteStyle, StaticRouteConfig } from './types';
export declare function resolveStaticRoute(cfg: string | StaticRouteConfig, cleanUrls: boolean): ResolvedStaticRoute;
export declare function contentTypeFor(filePath: string): string;
/**
* Decode + normalize a URL pathname into a safe relative path.
*
* Traversal safety: normalizing against a leading `/` collapses every `..`
* segment and clamps at the root, so the returned relative path never contains
* `..` and `path.join(root, rel)` can't escape `root`. Backslash, NUL and
* malformed percent-encoding are rejected outright (return `null`); the
* residual `..` guard is belt-and-suspenders.
*/
export declare function safeRelativePath(pathname: string): string | null;
/**
* Pure resolution of an incoming request pathname to a candidate file path on
* disk. Does no I/O; the caller checks existence and may fall back (SPA).
*
* Rules:
* - A trailing `/` (or root) resolves to `index.html` in that directory.
* - `cleanUrls` + a `.html` request → 301 to the extensionless URL.
* - Extensionless paths resolve per `pathRewriteStyle`:
* - `directory`: `/about` → `about/index.html`
* - `flat`: `/about` → `about.html`
* - Paths with a real extension (`.css`, `.png`, …) map straight through.
*
* Returns `null` when the path is unsafe (traversal attempt).
*/
export declare function resolveStaticFile(pathname: string, route: ResolvedStaticRoute): StaticResolution | null;
/**
* Serve a static file for the matched route. Returns a 301 for clean-URL
* redirects, the file with the right `Content-Type`/`Cache-Control` when it
* exists, the SPA `index.html` fallback when configured, or 404.
*/
export declare function serveStaticFile(pathname: string, route: ResolvedStaticRoute): Promise<Response>;
/** Normalized static-route config (shorthand string already expanded). */
export declare interface ResolvedStaticRoute {
dir: string
spa: boolean
pathRewriteStyle: PathRewriteStyle
maxAge: number
cleanUrls: boolean
}
export declare interface StaticResolution {
filePath: string
redirectTo?: string
}