@tschk/moonshine-server 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/package.json +3 -3
- package/src/pipeline.ts +67 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tschk/moonshine-server",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.80",
|
|
4
4
|
"license": "ISC",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
"test": "bun test"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@tschk/moonshine-framework": "^0.4.
|
|
20
|
-
"@tschk/moonshine-router": "^0.4.
|
|
19
|
+
"@tschk/moonshine-framework": "^0.4.80",
|
|
20
|
+
"@tschk/moonshine-router": "^0.4.80"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"@types/bun": "1.3.14",
|
package/src/pipeline.ts
CHANGED
|
@@ -442,6 +442,22 @@ async function runBefores(
|
|
|
442
442
|
}
|
|
443
443
|
}
|
|
444
444
|
|
|
445
|
+
const NOT_FOUND_PATH = "/404";
|
|
446
|
+
|
|
447
|
+
/** A page's loader answered 404 without a body of its own. */
|
|
448
|
+
function isBareNotFound(
|
|
449
|
+
response: Response,
|
|
450
|
+
route: RouteDefinition,
|
|
451
|
+
dataRequest: unknown,
|
|
452
|
+
): boolean {
|
|
453
|
+
return (
|
|
454
|
+
response.status === 404 &&
|
|
455
|
+
response.body === null &&
|
|
456
|
+
!dataRequest &&
|
|
457
|
+
(route as Partial<RouteArtifact>).mode !== "api"
|
|
458
|
+
);
|
|
459
|
+
}
|
|
460
|
+
|
|
445
461
|
export function createPipelineHandler(
|
|
446
462
|
options: RequestHandlerOptions,
|
|
447
463
|
policy?: DataRequestPolicy,
|
|
@@ -458,6 +474,47 @@ export function createPipelineHandler(
|
|
|
458
474
|
// per handler instead of once per request.
|
|
459
475
|
let cachedGraph = options.graph;
|
|
460
476
|
|
|
477
|
+
/**
|
|
478
|
+
* Like Waku's `404.tsx` and Next.js's `pages/404`, a page route at /404
|
|
479
|
+
* renders unmatched GET and HEAD requests, and page requests whose loader
|
|
480
|
+
* answers a bodiless 404 (Next.js's `notFound()`), with status 404 and the
|
|
481
|
+
* page's own headers. An explicit `notFound` option takes precedence.
|
|
482
|
+
*/
|
|
483
|
+
const notFoundPage = async (
|
|
484
|
+
request: Request,
|
|
485
|
+
): Promise<Response | undefined> => {
|
|
486
|
+
const { method } = request;
|
|
487
|
+
const url = new URL(request.url);
|
|
488
|
+
if (
|
|
489
|
+
options.notFound ||
|
|
490
|
+
(method !== "GET" && method !== "HEAD") ||
|
|
491
|
+
(url.pathname.replace(/\/+$/, "") || "/") === NOT_FOUND_PATH
|
|
492
|
+
)
|
|
493
|
+
return undefined;
|
|
494
|
+
const graph = cachedGraph;
|
|
495
|
+
const page = graph ? matchRoutes(graph, NOT_FOUND_PATH) : undefined;
|
|
496
|
+
if (
|
|
497
|
+
!page ||
|
|
498
|
+
page.route.path !== NOT_FOUND_PATH ||
|
|
499
|
+
(page.route as Partial<RouteArtifact>).mode === "api"
|
|
500
|
+
)
|
|
501
|
+
return undefined;
|
|
502
|
+
const response = await handler(
|
|
503
|
+
new Request(new URL(NOT_FOUND_PATH, url), {
|
|
504
|
+
method,
|
|
505
|
+
headers: request.headers,
|
|
506
|
+
signal: request.signal,
|
|
507
|
+
}),
|
|
508
|
+
);
|
|
509
|
+
if (response.status === 200)
|
|
510
|
+
return new Response(response.body, {
|
|
511
|
+
status: 404,
|
|
512
|
+
headers: response.headers,
|
|
513
|
+
});
|
|
514
|
+
await response.body?.cancel();
|
|
515
|
+
return undefined;
|
|
516
|
+
};
|
|
517
|
+
|
|
461
518
|
const handler = async (request: Request): Promise<Response> => {
|
|
462
519
|
const url = new URL(request.url);
|
|
463
520
|
const pathname = url.pathname.replace(/\/+$/, "") || "/";
|
|
@@ -494,6 +551,8 @@ export function createPipelineHandler(
|
|
|
494
551
|
|
|
495
552
|
const match = matchRoutes(graph, pathname);
|
|
496
553
|
if (!match) {
|
|
554
|
+
const page = dataRequest ? undefined : await notFoundPage(request);
|
|
555
|
+
if (page) return page;
|
|
497
556
|
const ctx = createRouteContext(request, {});
|
|
498
557
|
return finalizeResponse(
|
|
499
558
|
options.notFound
|
|
@@ -521,6 +580,10 @@ export function createPipelineHandler(
|
|
|
521
580
|
route,
|
|
522
581
|
dataRequest,
|
|
523
582
|
);
|
|
583
|
+
const page = isBareNotFound(response, route, dataRequest)
|
|
584
|
+
? await notFoundPage(request)
|
|
585
|
+
: undefined;
|
|
586
|
+
if (page) return page;
|
|
524
587
|
return finalizeResponse(response, baseHeaders, method);
|
|
525
588
|
} catch (error) {
|
|
526
589
|
if (error instanceof Redirect) {
|
|
@@ -557,6 +620,10 @@ export function createPipelineHandler(
|
|
|
557
620
|
}
|
|
558
621
|
|
|
559
622
|
if (error instanceof Response) {
|
|
623
|
+
const page = isBareNotFound(error, route, dataRequest)
|
|
624
|
+
? await notFoundPage(request)
|
|
625
|
+
: undefined;
|
|
626
|
+
if (page) return page;
|
|
560
627
|
return finalizeResponse(error, baseHeaders, method);
|
|
561
628
|
}
|
|
562
629
|
|