-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_astro-internal_middleware.mjs
More file actions
156 lines (145 loc) · 5.31 KB
/
_astro-internal_middleware.mjs
File metadata and controls
156 lines (145 loc) · 5.31 KB
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
import './chunks/index_DcPCChI5.mjs';
import { o as originPathnameSymbol, A as AstroError, F as ForbiddenRewrite, a as AstroUserError } from './chunks/astro/server_DEq4NUVT.mjs';
import { a as appendForwardSlash, r as removeTrailingForwardSlash, u as useTranslations } from './chunks/translations_iGNhBq7M.mjs';
function shouldAppendForwardSlash(trailingSlash, buildFormat) {
switch (trailingSlash) {
case "always":
return true;
case "never":
return false;
case "ignore": {
switch (buildFormat) {
case "directory":
return true;
case "preserve":
case "file":
return false;
}
}
}
}
function setOriginPathname(request, pathname, trailingSlash, buildFormat) {
if (!pathname) {
pathname = "/";
}
const shouldAppendSlash = shouldAppendForwardSlash(trailingSlash, buildFormat);
let finalPathname;
if (pathname === "/") {
finalPathname = "/";
} else if (shouldAppendSlash) {
finalPathname = appendForwardSlash(pathname);
} else {
finalPathname = removeTrailingForwardSlash(pathname);
}
Reflect.set(request, originPathnameSymbol, encodeURIComponent(finalPathname));
}
function getParams(route, pathname) {
if (!route.params.length) return {};
const paramsMatch = route.pattern.exec(pathname) || route.fallbackRoutes.map((fallbackRoute) => fallbackRoute.pattern.exec(pathname)).find((x) => x);
if (!paramsMatch) return {};
const params = {};
route.params.forEach((key, i) => {
if (key.startsWith("...")) {
params[key.slice(3)] = paramsMatch[i + 1] ? paramsMatch[i + 1] : void 0;
} else {
params[key] = paramsMatch[i + 1];
}
});
return params;
}
const apiContextRoutesSymbol = Symbol.for("context.routes");
function sequence(...handlers) {
const filtered = handlers.filter((h) => !!h);
const length = filtered.length;
if (!length) {
return defineMiddleware((_context, next) => {
return next();
});
}
return defineMiddleware((context, next) => {
let carriedPayload = void 0;
return applyHandle(0, context);
function applyHandle(i, handleContext) {
const handle = filtered[i];
const result = handle(handleContext, async (payload) => {
if (i < length - 1) {
if (payload) {
let newRequest;
if (payload instanceof Request) {
newRequest = payload;
} else if (payload instanceof URL) {
newRequest = new Request(payload, handleContext.request.clone());
} else {
newRequest = new Request(
new URL(payload, handleContext.url.origin),
handleContext.request.clone()
);
}
const oldPathname = handleContext.url.pathname;
const pipeline = Reflect.get(handleContext, apiContextRoutesSymbol);
const { routeData, pathname } = await pipeline.tryRewrite(
payload,
handleContext.request
);
if (pipeline.serverLike === true && handleContext.isPrerendered === false && routeData.prerender === true) {
throw new AstroError({
...ForbiddenRewrite,
message: ForbiddenRewrite.message(
handleContext.url.pathname,
pathname,
routeData.component
),
hint: ForbiddenRewrite.hint(routeData.component)
});
}
carriedPayload = payload;
handleContext.request = newRequest;
handleContext.url = new URL(newRequest.url);
handleContext.params = getParams(routeData, pathname);
handleContext.routePattern = routeData.route;
setOriginPathname(
handleContext.request,
oldPathname,
pipeline.manifest.trailingSlash,
pipeline.manifest.buildFormat
);
}
return applyHandle(i + 1, handleContext);
} else {
return next(payload ?? carriedPayload);
}
});
return result;
}
});
}
function defineMiddleware(fn) {
return fn;
}
const onRequest$1 = defineMiddleware(async (context, next) => {
context.locals.t = useTranslations(context.currentLocale);
initializeStarlightRoute(context);
return next();
});
function initializeStarlightRoute(context) {
if ("starlightRoute" in context.locals) return;
const state = { routeData: void 0 };
Object.defineProperty(context.locals, "starlightRoute", {
get() {
if (!state.routeData) {
throw new AstroUserError(
"`locals.starlightRoute` is not defined",
"This usually means a component that accesses `locals.starlightRoute` is being rendered outside of a Starlight page, which is not supported.\n\nIf this is a component you authored, you can do one of the following:\n\n1. Avoid using this component in non-Starlight pages.\n2. Wrap the code that reads `locals.starlightRoute` in a `try/catch` block and handle the cases where `starlightRoute` is not available.\n\nIf this is a Starlight built-in or third-party component, you may need to report a bug or avoid this use of the component."
);
}
return state.routeData;
},
set(routeData) {
state.routeData = routeData;
}
});
}
const onRequest = sequence(
onRequest$1,
);
export { onRequest };