@stacksjs/rpx
Version:
A modern and smart reverse proxy.
44 lines (43 loc) • 2.22 kB
TypeScript
/**
* Normalize a path prefix to a leading-slash, no-trailing-slash form so prefix
* comparisons are predictable. `''`/`undefined`/`'/'` all normalize to `'/'`
* (the host default). `'/api/'` → `'/api'`, `'docs'` → `'/docs'`.
*/
export declare function normalizePathPrefix(path: string | undefined): string;
/**
* True if `pathname` is matched by the prefix `prefix`. The root prefix `'/'`
* matches everything. A non-root prefix matches when the pathname equals it
* (`/api`), or continues with a `/` (`/api/x`) — so `/api` does NOT match
* `/apifoo`, only a real path-segment boundary.
*/
export declare function pathPrefixMatches(pathname: string, prefix: string): boolean;
/**
* Build a {@link HostRoutes} table from a flat list of entries. Entries are
* grouped by host; within each host the path-routes are sorted longest-prefix
* first so {@link matchHostRoute} can take the first match. If two entries
* collide on the same (host, path) the later one wins (matching `Map.set`).
*/
export declare function buildHostRoutes<T>(entries: Array<{ host: string, path?: string, route: T }>): HostRoutes<T>;
/**
* Find the path-route list for `hostname` in a {@link HostRoutes} table. Exact
* host match wins; otherwise the most-specific (deepest-suffix) wildcard wins —
* mirroring {@link import('./host-match').matchHost}.
*/
export declare function matchHostList<T>(table: HostRoutes<T>, hostname: string): Array<PathRoute<T>> | undefined;
/**
* Resolve a (hostname, pathname) pair to a single route value. First the host
* is resolved ({@link matchHostList}); then the longest matching path prefix
* within that host wins. Returns `undefined` when no host matches, or a host
* matches but no path prefix (including the `'/'` default) covers the request.
*/
export declare function matchHostRoute<T>(table: HostRoutes<T>, hostname: string, pathname: string): T | undefined;
/** One path-scoped route under a host. */
export declare interface PathRoute<T> {
path: string
route: T
}
/**
* A host-keyed routing table where each host owns an ordered set of
* path-scoped routes. Build it with {@link buildHostRoutes}.
*/
export type HostRoutes<T> = Map<string, Array<PathRoute<T>>>;