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 | 2x 2x 2x 665x 665x 665x 412x 665x 157x 253x 13x 13x 13x 7x 7x 1x 6x 13x 13x 12x 2x 1x 13x 13x 13x 96x 83x 83x 665x 665x 665x 665x 665x 665x 665x 2x 665x 665x 665x 665x 665x 665x 665x 665x 665x 665x 665x 665x 665x 665x 665x 665x 199x 466x 665x 665x 665x 1x 664x 665x 665x 665x 665x 665x 665x 665x 665x 665x 2x 74x 74x 15x 15x 15x 10x 15x 1x 1x 1x 1x 15x 15x 15x 15x 15x 74x 13x 10x 10x 10x 7x 10x 2x 2x 7x 10x 10x 6x 6x 74x | import { createMatcher, createRouteMatches } from './matcher';
import type { Router } from './router';
import type { Route, RouterOptions, RouterParsedOptions } from './types';
import { RouterMode } from './types';
import { isBrowser } from './util';
/**
* Gets the base URL object for the router.
* @param options - Router options.
* @returns The processed URL object.
*/
function getBaseUrl(options: RouterOptions): URL {
// Determine the URL source
let sourceUrl: string | URL;
if (options.base) {
sourceUrl = options.base;
} else if (isBrowser) {
sourceUrl = location.origin;
} else if (options.req) {
// Server-side: try to get it from the req object
const { req } = options;
const protocol =
req.headers['x-forwarded-proto'] ||
req.headers['x-forwarded-protocol'] ||
(req.socket && 'encrypted' in req.socket && req.socket.encrypted
? 'https'
: 'http');
const host =
req.headers['x-forwarded-host'] ||
req.headers.host ||
req.headers['x-real-ip'] ||
'localhost';
const port = req.headers['x-forwarded-port'];
const path = req.url || '';
sourceUrl = `${protocol}://${host}${port ? `:${port}` : ''}${path}`;
} else {
sourceUrl = 'https://esmx.dev/';
}
// Parse the URL, falling back to a default on failure.
// Use a try-catch block with the standard URL constructor for robustness.
let base: URL;
try {
base = new URL('.', sourceUrl);
} catch (e) {
console.warn(
`Failed to parse base URL '${sourceUrl}', using default: https://esmx.dev/`
);
base = new URL('https://esmx.dev/');
}
// Clean up and return
base.search = base.hash = '';
return base;
}
export function parsedOptions(
options: RouterOptions = {}
): RouterParsedOptions {
const base = getBaseUrl(options);
const routes = options.routes ?? [];
const compiledRoutes = createRouteMatches(routes);
return Object.freeze<RouterParsedOptions>({
rootStyle: options.rootStyle || false,
root: options.root || '',
context: options.context || {},
data: options.data || {},
req: options.req || null,
res: options.res || null,
layer: options.layer || false,
zIndex: options.zIndex || 10000,
base,
mode: isBrowser
? (options.mode ?? RouterMode.history)
: RouterMode.memory,
routes,
apps:
typeof options.apps === 'function'
? options.apps
: Object.assign({}, options.apps),
compiledRoutes,
matcher: createMatcher(routes, compiledRoutes),
normalizeURL: options.normalizeURL ?? ((url) => url),
fallback: options.fallback ?? fallback,
nextTick: options.nextTick ?? (() => {}),
handleBackBoundary: options.handleBackBoundary ?? (() => {}),
handleLayerClose: options.handleLayerClose ?? (() => {})
});
}
export function fallback(to: Route, from: Route | null, router: Router) {
const href = to.url.href;
// Server-side environment: handle application-level redirects and status codes
if (!isBrowser && router?.res) {
// Determine status code: prioritize route-specified code, default to 302 temporary redirect
let statusCode = 302;
// Validate redirect status code (3xx series)
const validRedirectCodes = [300, 301, 302, 303, 304, 307, 308];
if (to.statusCode && validRedirectCodes.includes(to.statusCode)) {
statusCode = to.statusCode;
} else if (to.statusCode) {
console.warn(
`Invalid redirect status code ${to.statusCode}, using default 302`
);
}
// Set redirect response
router.res.statusCode = statusCode;
router.res.setHeader('Location', href);
router.res.end();
return;
}
// Client-side environment: handle browser navigation
if (isBrowser) {
if (to.isPush) {
try {
const newWindow = window.open(href);
if (!newWindow) {
location.href = href;
} else {
newWindow.opener = null; // Sever the relationship between the new window and the current one
}
return newWindow;
} catch {}
}
location.href = href;
}
// Do nothing in a server environment without a res context
}
|