Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2244x 2244x 2244x 2244x 2244x 2244x 2244x 2238x 7x 2244x 2238x 2238x 6x 6x 6x 6x 6x 6x 6x 29x 6x 6x 6x 6x 2x 2x 2254x 2254x 2254x 2254x 2254x 2254x 2254x 2254x 2254x 2254x 2254x 2254x 2254x 2254x 2254x 2254x 2254x 2254x 2254x 2254x 2254x 2254x 2254x 2x 2x 2x 2254x 2254x 2254x 2254x 2254x 2254x 2254x 2254x 2254x 2254x 2254x 2254x 2254x 2254x 2254x 2254x 2229x 2254x 2228x 2228x 3379x 78x 78x 3301x 2228x 2228x 2254x 2228x 2228x 2228x 1315x 1315x 1x 1x 1315x 1314x 1314x 1314x 1315x 2228x 2254x 2254x 2228x 26x 2254x 2254x 2254x 2254x 2254x 2254x 6x 2248x 2254x 2254x 2109x 145x 2254x 2254x 2254x 118x 118x 2254x 2254x 177x 177x 177x 2254x 2254x 17x 17x 2254x 22540x 22540x 2254x 2x 73x 73x 2x 2391x 2391x 2x 783x 783x 2x 11x 11x 2x 766x 766x 2x 789x 7x 7x 7x 782x 782x 776x 776x 776x 776x 776x 2x 2x 2x 2x 774x 774x 776x 789x 2x 709x 709x 2x 6x 6x 60x 60x 60x 60x 6x 2x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 2x | import type { IncomingMessage, ServerResponse } from 'node:http';
import { parseLocation, resolveRouteLocationInput } from './location';
import { parsedOptions } from './options';
import type { Router } from './router';
import {
type RouteConfirmHook,
type RouteHandleHook,
type RouteHandleResult,
type RouteLayerOptions,
type RouteLocationInput,
type RouteMatchResult,
type RouteMeta,
type RouteOptions,
type RouteParsedConfig,
type RouterParsedOptions,
type RouteState,
RouteType
} from './types';
import { decodeParams, isNonEmptyPlainObject, isPlainObject } from './util';
/**
* Configuration for non-enumerable properties in Route class
* These properties will be hidden during object traversal and serialization
*/
export const NON_ENUMERABLE_PROPERTIES = [
// Private fields - internal implementation details
'_handled',
'_handle',
'_handleResult',
'_options',
// SSR-specific properties - meaningless in client environment
'req',
'res',
// Internal context - used by framework internally
'context',
// Status code - internal status information
'statusCode',
// Route behavior overrides - framework internal logic
'confirm',
// Layer configuration - used for layer routes
'layer'
] satisfies string[];
/**
* Append user-provided parameters to URL path
* @param match Route matching result
* @param toInput User-provided route location object
* @param base Base URL
* @param to Current parsed URL object
*/
export function applyRouteParams(
match: RouteMatchResult,
toInput: RouteLocationInput,
base: URL,
to: URL
): void {
if (
!isPlainObject(toInput) ||
!isNonEmptyPlainObject(toInput.params) ||
!match.matches.length
) {
return;
}
// Get the last matched route configuration
const lastMatch = match.matches[match.matches.length - 1];
// Split current path
const current = to.pathname.split('/');
// Compile new path with user parameters and split
const next = new URL(
lastMatch.compile(toInput.params).substring(1),
base
).pathname.split('/');
// Replace current path segments with new path segments
next.forEach((item, index) => {
current[index] = item || current[index];
});
// Update URL path
to.pathname = current.join('/');
// Merge parameters to match result, user parameters take precedence
Object.assign(match.params, toInput.params);
}
/**
* Route class provides complete route object functionality
*/
export class Route {
// Private fields for handle validation
private _handled = false;
private _handle: RouteHandleHook | null = null;
private _handleResult: RouteHandleResult | null = null;
private readonly _options: RouterParsedOptions;
// Public properties
public readonly statusCode: number | null = null;
public readonly state: RouteState;
public readonly keepScrollPosition: boolean;
/** Custom confirm handler that overrides default route-transition confirm logic */
public readonly confirm: RouteConfirmHook | null;
/** Layer configuration for layer routes */
public readonly layer: RouteLayerOptions | null;
// Read-only properties
public readonly type: RouteType;
public readonly req: IncomingMessage | null;
public readonly res: ServerResponse | null;
public readonly context: Record<string | symbol, any>;
public readonly url: URL;
public readonly path: string;
public readonly fullPath: string;
public readonly hash: string;
public readonly params: Record<string, string> = {};
public readonly paramsArray: Record<string, string[]> = {};
public readonly query: Record<string, string | undefined> = {};
public readonly queryArray: Record<string, string[] | undefined> = {};
public readonly meta: RouteMeta;
public readonly matched: readonly RouteParsedConfig[];
public readonly config: RouteParsedConfig | null;
/** @deprecated Use `url.pathname` instead. */
public get pathname(): string {
return this.url.pathname;
}
/** @deprecated Use `url.href` instead. */
public get href(): string {
return this.url.href;
}
constructor(routeOptions: Partial<RouteOptions> = {}) {
const {
toType = RouteType.push,
from = null,
options = parsedOptions()
} = routeOptions;
this._options = options;
this.type = toType;
this.req = options.req;
this.res = options.res;
this.context = options.context;
const base = options.base;
const toInput = resolveRouteLocationInput(routeOptions.toInput, from);
const to = options.normalizeURL(parseLocation(toInput, base), from);
let match: RouteMatchResult | null = null;
// Check if URL origin matches base origin (protocol + hostname + port)
// If origins don't match, treat as external URL and don't attempt route matching
if (
to.origin === base.origin &&
to.pathname.startsWith(base.pathname)
) {
const isLayer = toType === RouteType.pushLayer || options.layer;
match = options.matcher(to, base, (config) => {
if (isLayer) {
return config.layer !== false;
}
return config.layer !== true;
});
}
if (match) {
applyRouteParams(match, toInput, base, to);
const decodedParams = decodeParams(match.params);
for (const key in decodedParams) {
const value = decodedParams[key];
if (Array.isArray(value)) {
this.params[key] = value[0] || '';
this.paramsArray[key] = value;
} else {
this.params[key] = value;
this.paramsArray[key] = [value];
}
}
}
this.url = to;
this.path = match
? to.pathname.substring(base.pathname.length - 1)
: to.pathname;
this.fullPath = (match ? this.path : to.pathname) + to.search + to.hash;
this.matched = match ? match.matches : Object.freeze([]);
this.keepScrollPosition = Boolean(toInput.keepScrollPosition);
this.confirm = toInput.confirm || null;
this.layer =
toType === RouteType.pushLayer && toInput.layer
? toInput.layer
: null;
this.config =
this.matched.length > 0
? this.matched[this.matched.length - 1]
: null;
this.meta = this.config?.meta || {};
const state: RouteState = {};
if (toInput.state) {
Object.assign(state, toInput.state);
}
this.state = state;
for (const key of new Set(to.searchParams.keys())) {
this.query[key] = to.searchParams.get(key)!;
this.queryArray[key] = to.searchParams.getAll(key);
}
this.hash = to.hash;
// Set status code
// Prioritize user-provided statusCode
if (typeof toInput.statusCode === 'number') {
this.statusCode = toInput.statusCode;
}
// If statusCode is not provided, keep default null value
// Configure property enumerability
// Set internal implementation details as non-enumerable, keep user-common properties enumerable
// Set specified properties as non-enumerable according to configuration
for (const property of NON_ENUMERABLE_PROPERTIES) {
Object.defineProperty(this, property, { enumerable: false });
}
}
get isPush(): boolean {
return this.type.startsWith('push');
}
// handle related getter/setter
get handle(): RouteHandleHook | null {
return this._handle;
}
set handle(val: RouteHandleHook | null) {
this.setHandle(val);
}
get handleResult(): RouteHandleResult | null {
return this._handleResult;
}
set handleResult(val: RouteHandleResult | null) {
this._handleResult = val;
}
/**
* Set handle function with validation logic wrapper
*/
setHandle(val: RouteHandleHook | null): void {
if (typeof val !== 'function') {
this._handle = null;
return;
}
const self = this;
this._handle = function handle(
this: Route,
to: Route,
from: Route | null,
router: Router
) {
if (self._handled) {
throw new Error(
'Route handle hook can only be called once per navigation'
);
}
self._handled = true;
return val.call(this, to, from, router);
};
}
/**
* Apply navigation-generated state to current route
* Used by route handlers to add system state like pageId
* @param navigationState Navigation-generated state to apply
*/
applyNavigationState(navigationState: Partial<RouteState>): void {
Object.assign(this.state, navigationState);
}
/**
* Sync all properties of current route to target route object
* Used for route object updates in reactive systems
* @param targetRoute Target route object
*/
syncTo(targetRoute: Route): void {
// Copy enumerable properties
Object.assign(targetRoute, this);
// Copy non-enumerable properties - type-safe property copying
for (const property of NON_ENUMERABLE_PROPERTIES) {
if (!(property in this && property in targetRoute)) continue;
// Use Reflect.set for type-safe property setting
const value = Reflect.get(this, property);
Reflect.set(targetRoute, property, value);
}
}
/**
* Clone current route instance
* Returns a new Route instance with same configuration and state
*/
clone(): Route {
// Reconstruct route object, passing current state and confirm handler
const toInput: RouteLocationInput = {
path: this.fullPath,
state: { ...this.state },
...(this.confirm && { confirm: this.confirm }),
...(this.layer && { layer: this.layer }),
...(this.statusCode !== null && { statusCode: this.statusCode })
};
// Get original options from constructor's finalOptions
const options = this._options;
const clonedRoute = new Route({
options,
toType: this.type,
toInput
});
return clonedRoute;
}
}
|