@tschk/moonshine-astro 0.4.49 → 0.4.80
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +63 -18
- package/package.json +17 -5
- package/src/actions.ts +556 -0
- package/src/assets.ts +710 -0
- package/src/config.ts +269 -0
- package/src/container.ts +560 -0
- package/src/content.ts +1523 -0
- package/src/context.ts +756 -0
- package/src/env.ts +361 -0
- package/src/i18n.ts +420 -0
- package/src/index.ts +264 -0
- package/src/loaders.ts +888 -0
- package/src/middleware.ts +206 -0
- package/src/routing.ts +239 -0
- package/src/ssr.ts +208 -2
- package/src/transitions.ts +496 -0
package/src/container.ts
ADDED
|
@@ -0,0 +1,560 @@
|
|
|
1
|
+
/** Independent host-free Astro Container API (`astro/container`): `AstroContainer` and `experimental_AstroContainer`. */
|
|
2
|
+
import {
|
|
3
|
+
AstroCookies,
|
|
4
|
+
createAstroGlobal,
|
|
5
|
+
withAstroContext,
|
|
6
|
+
type AstroGlobal,
|
|
7
|
+
type AstroSlotValue,
|
|
8
|
+
} from "./context";
|
|
9
|
+
import { createContext, type APIContext } from "./middleware";
|
|
10
|
+
import type { APIRoute } from "./routing";
|
|
11
|
+
|
|
12
|
+
export interface AstroContainerRenderer {
|
|
13
|
+
name?: string;
|
|
14
|
+
check: (
|
|
15
|
+
component: unknown,
|
|
16
|
+
props: Record<string, any>,
|
|
17
|
+
slots: Record<string, string>,
|
|
18
|
+
) => boolean | Promise<boolean>;
|
|
19
|
+
renderToStaticMarkup: (
|
|
20
|
+
component: unknown,
|
|
21
|
+
props: Record<string, any>,
|
|
22
|
+
slots: Record<string, string>,
|
|
23
|
+
metadata?: Record<string, any>,
|
|
24
|
+
) => { html: string } | string | Promise<{ html: string } | string>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface AstroContainerClientRenderer {
|
|
28
|
+
name: string;
|
|
29
|
+
entrypoint: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface AstroContainerOptions {
|
|
33
|
+
streaming?: boolean;
|
|
34
|
+
renderers?: AstroContainerRenderer[];
|
|
35
|
+
resolve?: (specifier: string) => string | Promise<string>;
|
|
36
|
+
site?: string | URL;
|
|
37
|
+
locals?: Record<string, any>;
|
|
38
|
+
clientAddress?: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface ContainerRenderOptions<
|
|
42
|
+
Props extends Record<string, any> = Record<string, any>,
|
|
43
|
+
> {
|
|
44
|
+
props?: Props;
|
|
45
|
+
params?: Record<string, string | undefined>;
|
|
46
|
+
slots?: Record<string, AstroSlotValue>;
|
|
47
|
+
request?: Request;
|
|
48
|
+
url?: string | URL;
|
|
49
|
+
routePattern?: string;
|
|
50
|
+
locals?: Record<string, any>;
|
|
51
|
+
cookies?: AstroCookies | Record<string, string> | string;
|
|
52
|
+
site?: string | URL;
|
|
53
|
+
clientAddress?: string;
|
|
54
|
+
routeType?: "page" | "endpoint";
|
|
55
|
+
partial?: boolean;
|
|
56
|
+
status?: number;
|
|
57
|
+
statusText?: string;
|
|
58
|
+
headers?: HeadersInit;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export type EndpointModule = Partial<
|
|
62
|
+
Record<
|
|
63
|
+
"GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS" | "ALL",
|
|
64
|
+
APIRoute
|
|
65
|
+
>
|
|
66
|
+
>;
|
|
67
|
+
|
|
68
|
+
export type AstroContainerComponent = (
|
|
69
|
+
props: any,
|
|
70
|
+
slotsOrContext?: any,
|
|
71
|
+
) => unknown;
|
|
72
|
+
|
|
73
|
+
export type AstroContainerRenderable =
|
|
74
|
+
| AstroContainerComponent
|
|
75
|
+
| EndpointModule
|
|
76
|
+
| { default: AstroContainerComponent | EndpointModule }
|
|
77
|
+
| string;
|
|
78
|
+
|
|
79
|
+
interface RegisteredPageRoute {
|
|
80
|
+
pattern: string;
|
|
81
|
+
component: AstroContainerRenderable;
|
|
82
|
+
defaultParams?: Record<string, string | undefined>;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
let cachedSsrMod: typeof import("./ssr") | undefined;
|
|
86
|
+
|
|
87
|
+
async function getSsrModule(): Promise<typeof import("./ssr")> {
|
|
88
|
+
if (!cachedSsrMod) {
|
|
89
|
+
const spec = "./ssr";
|
|
90
|
+
cachedSsrMod = (await import(
|
|
91
|
+
new URL(`${spec}.ts`, import.meta.url).href
|
|
92
|
+
)) as typeof import("./ssr");
|
|
93
|
+
}
|
|
94
|
+
return cachedSsrMod;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function isSsrSymbolObject(value: unknown): boolean {
|
|
98
|
+
if (typeof value !== "object" || value === null) return false;
|
|
99
|
+
return Object.getOwnPropertySymbols(value).length > 0;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function isEndpointModule(value: unknown): value is EndpointModule {
|
|
103
|
+
if (typeof value !== "object" || value === null) return false;
|
|
104
|
+
const methods = [
|
|
105
|
+
"GET",
|
|
106
|
+
"POST",
|
|
107
|
+
"PUT",
|
|
108
|
+
"PATCH",
|
|
109
|
+
"DELETE",
|
|
110
|
+
"HEAD",
|
|
111
|
+
"OPTIONS",
|
|
112
|
+
"ALL",
|
|
113
|
+
];
|
|
114
|
+
return methods.some(
|
|
115
|
+
(m) => typeof (value as Record<string, unknown>)[m] === "function",
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function unwrapRenderable(
|
|
120
|
+
input: AstroContainerRenderable,
|
|
121
|
+
): AstroContainerComponent | EndpointModule | string {
|
|
122
|
+
if (
|
|
123
|
+
typeof input === "object" &&
|
|
124
|
+
input !== null &&
|
|
125
|
+
"default" in input &&
|
|
126
|
+
input.default !== undefined
|
|
127
|
+
) {
|
|
128
|
+
return input.default;
|
|
129
|
+
}
|
|
130
|
+
return input as AstroContainerComponent | EndpointModule | string;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function matchRoutePattern(
|
|
134
|
+
pattern: string,
|
|
135
|
+
pathname: string,
|
|
136
|
+
): Record<string, string | undefined> | null {
|
|
137
|
+
const normPattern = pattern.startsWith("/") ? pattern : `/${pattern}`;
|
|
138
|
+
const normPath = pathname.startsWith("/") ? pathname : `/${pathname}`;
|
|
139
|
+
|
|
140
|
+
const patSegments = normPattern.split("/").filter(Boolean);
|
|
141
|
+
const pathSegments = normPath.split("/").filter(Boolean);
|
|
142
|
+
const params: Record<string, string | undefined> = {};
|
|
143
|
+
|
|
144
|
+
for (let i = 0; i < patSegments.length; i++) {
|
|
145
|
+
const pat = patSegments[i]!;
|
|
146
|
+
if (pat.startsWith("[...") && pat.endsWith("]")) {
|
|
147
|
+
const name = pat.slice(4, -1);
|
|
148
|
+
const rest = pathSegments.slice(i).join("/");
|
|
149
|
+
params[name] = rest || undefined;
|
|
150
|
+
return params;
|
|
151
|
+
}
|
|
152
|
+
if (i >= pathSegments.length) {
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
const seg = pathSegments[i]!;
|
|
156
|
+
if (pat.startsWith("[") && pat.endsWith("]")) {
|
|
157
|
+
const name = pat.slice(1, -1);
|
|
158
|
+
params[name] = decodeURIComponent(seg);
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
if (pat !== seg) {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (pathSegments.length !== patSegments.length) {
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
return params;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
interface InternalRenderResult {
|
|
173
|
+
html: string;
|
|
174
|
+
response?: Response;
|
|
175
|
+
astro: AstroGlobal;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Host-free Astro Container (`astro/container`) for rendering Astro components,
|
|
180
|
+
* Moonshine SSR components, and API endpoints to strings or HTTP `Response`s in isolation.
|
|
181
|
+
*/
|
|
182
|
+
export class AstroContainer {
|
|
183
|
+
readonly options: AstroContainerOptions;
|
|
184
|
+
readonly #serverRenderers: AstroContainerRenderer[] = [];
|
|
185
|
+
readonly #clientRenderers: AstroContainerClientRenderer[] = [];
|
|
186
|
+
readonly #routes: RegisteredPageRoute[] = [];
|
|
187
|
+
|
|
188
|
+
constructor(options: AstroContainerOptions = {}) {
|
|
189
|
+
this.options = { ...options };
|
|
190
|
+
if (options.renderers) {
|
|
191
|
+
this.#serverRenderers.push(...options.renderers);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
static async create(
|
|
196
|
+
options: AstroContainerOptions = {},
|
|
197
|
+
): Promise<AstroContainer> {
|
|
198
|
+
return new AstroContainer(options);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
addServerRenderer(
|
|
202
|
+
input:
|
|
203
|
+
| { name?: string; renderer: AstroContainerRenderer }
|
|
204
|
+
| AstroContainerRenderer,
|
|
205
|
+
): void {
|
|
206
|
+
const renderer = "renderer" in input ? input.renderer : input;
|
|
207
|
+
this.#serverRenderers.push(renderer);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
addClientRenderer(renderer: AstroContainerClientRenderer): void {
|
|
211
|
+
this.#clientRenderers.push(renderer);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
getServerRenderers(): readonly AstroContainerRenderer[] {
|
|
215
|
+
return this.#serverRenderers;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
getClientRenderers(): readonly AstroContainerClientRenderer[] {
|
|
219
|
+
return this.#clientRenderers;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
insertPageRoute(
|
|
223
|
+
route: string,
|
|
224
|
+
component: AstroContainerRenderable,
|
|
225
|
+
defaultParams?: Record<string, string | undefined>,
|
|
226
|
+
): void {
|
|
227
|
+
this.#routes.push({
|
|
228
|
+
pattern: route,
|
|
229
|
+
component,
|
|
230
|
+
defaultParams,
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
async resolve(specifier: string): Promise<string> {
|
|
235
|
+
if (this.options.resolve) {
|
|
236
|
+
return await this.options.resolve(specifier);
|
|
237
|
+
}
|
|
238
|
+
return specifier;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
private async evaluateSlotsMap(
|
|
242
|
+
rawSlots: Record<string, AstroSlotValue> = {},
|
|
243
|
+
): Promise<Record<string, string>> {
|
|
244
|
+
const out: Record<string, string> = {};
|
|
245
|
+
for (const [name, slotVal] of Object.entries(rawSlots)) {
|
|
246
|
+
if (slotVal === undefined || slotVal === null || slotVal === false) {
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
let resolved: unknown;
|
|
250
|
+
if (typeof slotVal === "function") {
|
|
251
|
+
try {
|
|
252
|
+
resolved = await slotVal();
|
|
253
|
+
} catch {
|
|
254
|
+
resolved = "";
|
|
255
|
+
}
|
|
256
|
+
} else {
|
|
257
|
+
resolved = await slotVal;
|
|
258
|
+
}
|
|
259
|
+
if (resolved === undefined || resolved === null || resolved === false) {
|
|
260
|
+
out[name] = "";
|
|
261
|
+
} else if (isSsrSymbolObject(resolved)) {
|
|
262
|
+
const ssr = await getSsrModule();
|
|
263
|
+
out[name] = ssr.renderToString(() => resolved as any);
|
|
264
|
+
} else {
|
|
265
|
+
out[name] = String(resolved);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return out;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
private async renderInternal(
|
|
272
|
+
componentInput: AstroContainerRenderable,
|
|
273
|
+
options: ContainerRenderOptions = {},
|
|
274
|
+
): Promise<InternalRenderResult> {
|
|
275
|
+
const target = unwrapRenderable(componentInput);
|
|
276
|
+
const props = { ...options.props };
|
|
277
|
+
const params = { ...options.params };
|
|
278
|
+
const locals = {
|
|
279
|
+
...this.options.locals,
|
|
280
|
+
...options.locals,
|
|
281
|
+
};
|
|
282
|
+
const rawSlots = options.slots ?? {};
|
|
283
|
+
|
|
284
|
+
const resolvedCookies =
|
|
285
|
+
options.cookies instanceof AstroCookies
|
|
286
|
+
? options.cookies
|
|
287
|
+
: options.cookies !== undefined
|
|
288
|
+
? new AstroCookies(options.cookies)
|
|
289
|
+
: undefined;
|
|
290
|
+
|
|
291
|
+
const responseHeaders = new Headers(options.headers);
|
|
292
|
+
const rewriteHandler = async (
|
|
293
|
+
targetUrl: URL,
|
|
294
|
+
nextRequest: Request,
|
|
295
|
+
): Promise<Response> => {
|
|
296
|
+
for (const route of this.#routes) {
|
|
297
|
+
const matchedParams = matchRoutePattern(
|
|
298
|
+
route.pattern,
|
|
299
|
+
targetUrl.pathname,
|
|
300
|
+
);
|
|
301
|
+
if (matchedParams) {
|
|
302
|
+
return await this.renderToResponse(route.component, {
|
|
303
|
+
...options,
|
|
304
|
+
request: nextRequest,
|
|
305
|
+
url: targetUrl,
|
|
306
|
+
routePattern: route.pattern,
|
|
307
|
+
params: {
|
|
308
|
+
...route.defaultParams,
|
|
309
|
+
...matchedParams,
|
|
310
|
+
},
|
|
311
|
+
locals,
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
const fallbackHeaders = new Headers(responseHeaders);
|
|
316
|
+
fallbackHeaders.set(
|
|
317
|
+
"x-astro-rewrite",
|
|
318
|
+
targetUrl.pathname + targetUrl.search,
|
|
319
|
+
);
|
|
320
|
+
return new Response(null, {
|
|
321
|
+
status: options.status ?? 200,
|
|
322
|
+
statusText: options.statusText ?? "OK",
|
|
323
|
+
headers: fallbackHeaders,
|
|
324
|
+
});
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
const astro = createAstroGlobal({
|
|
328
|
+
props,
|
|
329
|
+
params,
|
|
330
|
+
locals,
|
|
331
|
+
slots: rawSlots,
|
|
332
|
+
request: options.request,
|
|
333
|
+
url: options.url,
|
|
334
|
+
routePattern: options.routePattern,
|
|
335
|
+
cookies: resolvedCookies,
|
|
336
|
+
site: options.site ?? this.options.site,
|
|
337
|
+
clientAddress: options.clientAddress ?? this.options.clientAddress,
|
|
338
|
+
response: {
|
|
339
|
+
status: options.status ?? 200,
|
|
340
|
+
statusText: options.statusText ?? "OK",
|
|
341
|
+
headers: responseHeaders,
|
|
342
|
+
},
|
|
343
|
+
rewriteHandler,
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
if (options.routeType === "endpoint" || isEndpointModule(target)) {
|
|
347
|
+
const apiCtx: APIContext = createContext({
|
|
348
|
+
request: astro.request,
|
|
349
|
+
url: astro.url,
|
|
350
|
+
params: astro.params,
|
|
351
|
+
props: astro.props,
|
|
352
|
+
locals: astro.locals,
|
|
353
|
+
cookies: astro.cookies,
|
|
354
|
+
site: astro.site,
|
|
355
|
+
clientAddress: astro.clientAddress,
|
|
356
|
+
rewriteHandler,
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
let handler: APIRoute | undefined;
|
|
360
|
+
if (typeof target === "function") {
|
|
361
|
+
handler = target as APIRoute;
|
|
362
|
+
} else if (isEndpointModule(target)) {
|
|
363
|
+
const method =
|
|
364
|
+
astro.request.method.toUpperCase() as keyof EndpointModule;
|
|
365
|
+
handler = target[method] ?? target.ALL;
|
|
366
|
+
}
|
|
367
|
+
if (!handler) {
|
|
368
|
+
const notAllowed = new Response(null, {
|
|
369
|
+
status: 405,
|
|
370
|
+
statusText: "Method Not Allowed",
|
|
371
|
+
});
|
|
372
|
+
return { html: "", response: notAllowed, astro };
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
const endpointRes = await withAstroContext(astro, () => handler!(apiCtx));
|
|
376
|
+
const clonedText = await endpointRes.clone().text();
|
|
377
|
+
return {
|
|
378
|
+
html: clonedText,
|
|
379
|
+
response: endpointRes,
|
|
380
|
+
astro,
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
if (typeof target === "string") {
|
|
385
|
+
return {
|
|
386
|
+
html: this.applyDoctypeIfNeeded(target, options.partial),
|
|
387
|
+
astro,
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
const evaluatedSlots = await this.evaluateSlotsMap(rawSlots);
|
|
392
|
+
|
|
393
|
+
for (const renderer of this.#serverRenderers) {
|
|
394
|
+
if (await renderer.check(target, props, evaluatedSlots)) {
|
|
395
|
+
const rendered = await withAstroContext(astro, () =>
|
|
396
|
+
renderer.renderToStaticMarkup(target, props, evaluatedSlots),
|
|
397
|
+
);
|
|
398
|
+
const htmlStr = typeof rendered === "string" ? rendered : rendered.html;
|
|
399
|
+
return {
|
|
400
|
+
html: this.applyDoctypeIfNeeded(htmlStr, options.partial),
|
|
401
|
+
astro,
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
if (typeof target === "function") {
|
|
407
|
+
const ssr = await getSsrModule();
|
|
408
|
+
const slotThunks: Record<string, (...args: unknown[]) => any> = {};
|
|
409
|
+
for (const [slotName, slotHtml] of Object.entries(evaluatedSlots)) {
|
|
410
|
+
const rawSlot = rawSlots[slotName];
|
|
411
|
+
if (typeof rawSlot === "function") {
|
|
412
|
+
slotThunks[slotName] = (...args: unknown[]) => {
|
|
413
|
+
if (args.length > 0) {
|
|
414
|
+
const out = rawSlot(...args);
|
|
415
|
+
if (out === undefined || out === null || out === false) {
|
|
416
|
+
return "";
|
|
417
|
+
}
|
|
418
|
+
if (isSsrSymbolObject(out)) {
|
|
419
|
+
return out as any;
|
|
420
|
+
}
|
|
421
|
+
return ssr.rawHTML(String(out));
|
|
422
|
+
}
|
|
423
|
+
return ssr.rawHTML(slotHtml);
|
|
424
|
+
};
|
|
425
|
+
} else {
|
|
426
|
+
slotThunks[slotName] = () => ssr.rawHTML(slotHtml);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
let directResult: unknown;
|
|
431
|
+
let renderedHtml: string | undefined;
|
|
432
|
+
|
|
433
|
+
await withAstroContext(astro, async () => {
|
|
434
|
+
const candidate = ssr.renderToString(() => {
|
|
435
|
+
const out = target(props, slotThunks);
|
|
436
|
+
if (
|
|
437
|
+
out instanceof Response ||
|
|
438
|
+
(typeof out === "object" &&
|
|
439
|
+
out !== null &&
|
|
440
|
+
"then" in out &&
|
|
441
|
+
typeof (out as PromiseLike<unknown>).then === "function")
|
|
442
|
+
) {
|
|
443
|
+
directResult = out;
|
|
444
|
+
return "";
|
|
445
|
+
}
|
|
446
|
+
return ssr.sourceChild(out);
|
|
447
|
+
});
|
|
448
|
+
if (directResult === undefined) {
|
|
449
|
+
renderedHtml = candidate;
|
|
450
|
+
}
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
if (directResult !== undefined) {
|
|
454
|
+
const resolved = await withAstroContext(
|
|
455
|
+
astro,
|
|
456
|
+
async () => await directResult,
|
|
457
|
+
);
|
|
458
|
+
if (resolved instanceof Response) {
|
|
459
|
+
const text = await resolved.clone().text();
|
|
460
|
+
return {
|
|
461
|
+
html: text,
|
|
462
|
+
response: resolved,
|
|
463
|
+
astro,
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
if (isSsrSymbolObject(resolved)) {
|
|
467
|
+
const html = withAstroContext(astro, () =>
|
|
468
|
+
ssr.renderToString(() => resolved as any),
|
|
469
|
+
);
|
|
470
|
+
return {
|
|
471
|
+
html: this.applyDoctypeIfNeeded(html, options.partial),
|
|
472
|
+
astro,
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
const str =
|
|
476
|
+
resolved === null || resolved === undefined || resolved === false
|
|
477
|
+
? ""
|
|
478
|
+
: String(resolved);
|
|
479
|
+
return {
|
|
480
|
+
html: this.applyDoctypeIfNeeded(str, options.partial),
|
|
481
|
+
astro,
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
return {
|
|
486
|
+
html: this.applyDoctypeIfNeeded(renderedHtml ?? "", options.partial),
|
|
487
|
+
astro,
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
return {
|
|
492
|
+
html: "",
|
|
493
|
+
astro,
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
private applyDoctypeIfNeeded(html: string, partial = true): string {
|
|
498
|
+
if (partial) return html;
|
|
499
|
+
const trimmed = html.trimStart();
|
|
500
|
+
if (/^<!doctype\s+html/i.test(trimmed)) {
|
|
501
|
+
return html;
|
|
502
|
+
}
|
|
503
|
+
return `<!DOCTYPE html>${html}`;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
async renderToString(
|
|
507
|
+
component: AstroContainerRenderable,
|
|
508
|
+
options: ContainerRenderOptions = {},
|
|
509
|
+
): Promise<string> {
|
|
510
|
+
const result = await this.renderInternal(component, {
|
|
511
|
+
...options,
|
|
512
|
+
partial: options.partial ?? true,
|
|
513
|
+
});
|
|
514
|
+
return result.html;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
async renderToResponse(
|
|
518
|
+
component: AstroContainerRenderable,
|
|
519
|
+
options: ContainerRenderOptions = {},
|
|
520
|
+
): Promise<Response> {
|
|
521
|
+
const result = await this.renderInternal(component, {
|
|
522
|
+
...options,
|
|
523
|
+
partial: options.partial ?? true,
|
|
524
|
+
});
|
|
525
|
+
|
|
526
|
+
if (result.response) {
|
|
527
|
+
const headers = new Headers(result.response.headers);
|
|
528
|
+
for (const [k, v] of result.astro.response.headers.entries()) {
|
|
529
|
+
if (!headers.has(k)) {
|
|
530
|
+
headers.set(k, v);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
for (const cookieHeader of result.astro.cookies.headers()) {
|
|
534
|
+
headers.append("Set-Cookie", cookieHeader);
|
|
535
|
+
}
|
|
536
|
+
return new Response(result.response.body, {
|
|
537
|
+
status: result.response.status,
|
|
538
|
+
statusText: result.response.statusText,
|
|
539
|
+
headers,
|
|
540
|
+
});
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
const headers = new Headers(result.astro.response.headers);
|
|
544
|
+
if (!headers.has("Content-Type")) {
|
|
545
|
+
headers.set("Content-Type", "text/html; charset=utf-8");
|
|
546
|
+
}
|
|
547
|
+
for (const cookieHeader of result.astro.cookies.headers()) {
|
|
548
|
+
headers.append("Set-Cookie", cookieHeader);
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
return new Response(result.html, {
|
|
552
|
+
status: result.astro.response.status,
|
|
553
|
+
statusText: result.astro.response.statusText,
|
|
554
|
+
headers,
|
|
555
|
+
});
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
export const experimental_AstroContainer = AstroContainer;
|
|
560
|
+
export type experimental_AstroContainer = AstroContainer;
|