@k8ordo/framework 3.0.0 → 4.0.0
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 +6 -4
- package/dist/cli.mjs +3 -3
- package/dist/generated.d.mts +12 -13
- package/dist/index.d.mts +3 -3
- package/dist/index.mjs +1 -1
- package/dist/paths-type-DXpmjaOr.d.mts +43 -0
- package/dist/runtime/app-router.mjs +1 -1
- package/dist/runtime/browser-payloads.mjs +1 -1
- package/dist/runtime/cookies.d.mts +2 -0
- package/dist/runtime/cookies.mjs +2 -1
- package/dist/runtime/ended.d.mts +7 -0
- package/dist/runtime/ended.mjs +14 -0
- package/dist/runtime/entry.browser.mjs +1 -1
- package/dist/runtime/entry.rsc.d.mts +17 -7
- package/dist/runtime/entry.rsc.mjs +42 -33
- package/dist/runtime/entry.ssr.d.mts +1 -0
- package/dist/runtime/entry.ssr.mjs +4 -3
- package/dist/runtime/guard.d.mts +15 -10
- package/dist/runtime/guard.mjs +8 -2
- package/dist/runtime/mount.mjs +1 -1
- package/dist/runtime/page-boundary.mjs +2 -2
- package/dist/runtime/params.d.mts +5 -4
- package/dist/runtime/params.mjs +2 -2
- package/dist/runtime/redirect.d.mts +13 -7
- package/dist/runtime/redirect.mjs +25 -9
- package/dist/runtime/request-scope.d.mts +37 -2
- package/dist/runtime/request-scope.mjs +60 -3
- package/dist/runtime/request.d.mts +6 -1
- package/dist/runtime/request.mjs +7 -2
- package/dist/runtime/route.d.mts +4 -2
- package/dist/runtime/route.mjs +13 -5
- package/dist/runtime/server.d.mts +5 -5
- package/dist/runtime/server.mjs +2 -2
- package/dist/runtime/shell.d.mts +2 -1
- package/dist/serve.d.mts +4 -0
- package/dist/serve.mjs +26 -13
- package/dist/{static-c-nX9Juk.d.mts → static-Be3xIqd8.d.mts} +19 -12
- package/dist/{static-file-BEk_xO5y.mjs → static-file-BS-WEiwu.mjs} +180 -136
- package/dist/{static-host-CyAmsL_7.mjs → static-host-tIDLqV8c.mjs} +1 -1
- package/dist/vercel.d.mts +18 -1
- package/dist/vercel.mjs +104 -7
- package/dist/vite.d.mts +6 -3
- package/dist/vite.mjs +155 -65
- package/docs/GUIDE.md +149 -21
- package/docs/llms.txt +102 -47
- package/docs/references/actions.md +14 -5
- package/docs/references/boundaries.md +23 -2
- package/docs/references/csp.md +91 -2
- package/docs/references/deploy.md +85 -16
- package/docs/references/errors.md +18 -10
- package/docs/references/guards.md +100 -29
- package/docs/references/params.md +91 -22
- package/docs/references/routing.md +171 -31
- package/package.json +15 -10
- package/dist/paths-type-DtsL8JuC.d.mts +0 -19
package/README.md
CHANGED
|
@@ -35,11 +35,12 @@ dependencies in either mode: the build bundles what the application runs.
|
|
|
35
35
|
|
|
36
36
|
| Package | Version | Required | Needed for |
|
|
37
37
|
| --------------------- | ----------------- | -------- | ------------------------------------------ |
|
|
38
|
-
| `@k8ordo/router` | ^
|
|
38
|
+
| `@k8ordo/router` | ^4.0.0 | yes | routing, re-exported by the framework |
|
|
39
39
|
| `react` | ≥19.3.0 | yes | rendering |
|
|
40
40
|
| `react-dom` | ≥19.3.0 | yes | rendering |
|
|
41
41
|
| `vite` | ≥8.1.0 | yes | the build (`framework()` is a Vite plugin) |
|
|
42
42
|
| `oxc-transform-react` | ≥0.152.0 <0.157.0 | optional | `reactCompiler: true` (React Compiler) |
|
|
43
|
+
| `@vercel/functions` | ^3.0.0 | optional | `waitUntil()` on Vercel, under `vercel()` |
|
|
43
44
|
|
|
44
45
|
<!-- /peers -->
|
|
45
46
|
|
|
@@ -54,7 +55,7 @@ export default defineConfig({
|
|
|
54
55
|
plugins: [
|
|
55
56
|
framework({
|
|
56
57
|
mode: 'static',
|
|
57
|
-
site: 'https://example.com', //
|
|
58
|
+
site: 'https://example.com', // the origin it is served from: its URLs, sitemap.xml
|
|
58
59
|
}),
|
|
59
60
|
],
|
|
60
61
|
});
|
|
@@ -95,7 +96,7 @@ export default async function ProductPage({
|
|
|
95
96
|
import type { Paths } from '@k8ordo/framework';
|
|
96
97
|
|
|
97
98
|
const paths: Paths<'/products/:id'> = async () =>
|
|
98
|
-
(await listProducts()).map((product) =>
|
|
99
|
+
(await listProducts()).map((product) => product.id);
|
|
99
100
|
|
|
100
101
|
export default paths;
|
|
101
102
|
```
|
|
@@ -132,7 +133,8 @@ export async function createTalk(_previous: FormState, formData: FormData) {
|
|
|
132
133
|
```
|
|
133
134
|
|
|
134
135
|
The built handler, `dist/rsc/index.js`, is a plain
|
|
135
|
-
`(request: Request) => Promise<Response>`
|
|
136
|
+
`(request: Request, context?: HandlerContext) => Promise<Response>`
|
|
137
|
+
(`context.waitUntil` is the host's own) that needs nothing from its runtime
|
|
136
138
|
but `AsyncLocalStorage`, so Node.js, Bun, Deno and Cloudflare Workers
|
|
137
139
|
(`nodejs_compat`) all run it, and `vercel()` from `@k8ordo/framework/vercel`
|
|
138
140
|
writes a build for Vercel.
|
package/dist/cli.mjs
CHANGED
|
@@ -8,9 +8,9 @@ when it begins, and builds nothing.
|
|
|
8
8
|
`;
|
|
9
9
|
/**
|
|
10
10
|
* `.k8ordo/` without a build: the config resolved as `vite build` resolves
|
|
11
|
-
* it, and the engine's plugin, which knows the
|
|
12
|
-
*
|
|
13
|
-
*
|
|
11
|
+
* it, and the engine's plugin, which knows the mode, asked to generate. No
|
|
12
|
+
* environment is built and no page rendered, so a `paths.ts` and whatever
|
|
13
|
+
* data a page reads are never touched.
|
|
14
14
|
*/
|
|
15
15
|
const generate = async () => {
|
|
16
16
|
const config = await resolveConfig({}, "build", "production", "production");
|
package/dist/generated.d.mts
CHANGED
|
@@ -1,24 +1,23 @@
|
|
|
1
|
-
import { t as Paths } from "./paths-type-
|
|
1
|
+
import { n as RedirectTarget, t as Paths } from "./paths-type-DXpmjaOr.mjs";
|
|
2
2
|
import { ErrorComponent, ParamsOf, ParamsOf as ParamsOf$1, ParamsSchemaFor, ParsedParams, ParsedParamsMap, Register, defineRoutes } from "@k8ordo/router";
|
|
3
3
|
/**
|
|
4
|
-
* What a `guard.ts` receives: the request
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* What a `guard.ts` receives: the request for the page — a client
|
|
5
|
+
* navigation's payload request too, under the page's URL — and the params of
|
|
6
|
+
* the pattern its directory puts it under, as the pathname carried them — a
|
|
7
|
+
* string each, a rest param its segments — since a guard runs above the page
|
|
8
|
+
* and every layout, where no schema has typed them.
|
|
7
9
|
*/
|
|
8
10
|
type GuardContext<P extends string = string> = {
|
|
9
11
|
readonly request: Request;
|
|
10
12
|
readonly params: ParamsOf$1<P>;
|
|
11
13
|
};
|
|
12
14
|
/**
|
|
13
|
-
* What a `guard.ts` default-exports.
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
15
|
+
* What a `guard.ts` default-exports. `redirect(url)` ends the request there
|
|
16
|
+
* with a `303`, `notFound()` with the nearest not-found.tsx under 404, and a
|
|
17
|
+
* `Response` returned — a `401`, a `403` — is the answer itself; returning
|
|
18
|
+
* nothing lets it through to what answers below. There is no `next()` to run
|
|
19
|
+
* the page and rewrite its answer: a page streams, and its headers leave
|
|
20
|
+
* before its body is written.
|
|
17
21
|
*/
|
|
18
22
|
type Guard<P extends string = string> = (context: GuardContext<P>) => Response | void | Promise<Response | void>;
|
|
19
|
-
/** What a `redirect.ts` route file default-exports. */
|
|
20
|
-
type RedirectTarget = string | {
|
|
21
|
-
readonly to: string;
|
|
22
|
-
readonly permanent?: boolean;
|
|
23
|
-
};
|
|
24
23
|
export { type ErrorComponent, type Guard, type ParamsOf, type ParamsSchemaFor, type ParsedParams, type ParsedParamsMap, type Paths, type RedirectTarget, type Register, defineRoutes };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { t as Paths } from "./paths-type-
|
|
2
|
-
import {
|
|
3
|
-
export { type
|
|
1
|
+
import { n as RedirectTarget, t as Paths } from "./paths-type-DXpmjaOr.mjs";
|
|
2
|
+
import { BoundParams, ErrorProps, LayoutProps, MatchOptions, MatchablePattern, PageProps, RegisteredNavigablePattern, RegisteredPageParams, RegisteredParams, RegisteredPattern, RouteContext, bindParams, href, isNotFound, matchPath, normalizePathname, notFound, useMatch, usePathname, usePendingPathname, withBase } from "@k8ordo/router";
|
|
3
|
+
export { type BoundParams, type ErrorProps, type LayoutProps, type MatchOptions, type MatchablePattern, type PageProps, type Paths, type RedirectTarget, type RegisteredNavigablePattern, type RegisteredPageParams, type RegisteredParams, type RegisteredPattern, type RouteContext, bindParams, href, isNotFound, matchPath, normalizePathname, notFound, useMatch, usePathname, usePendingPathname, withBase };
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { bindParams, href, isNotFound, matchPath,
|
|
1
|
+
export { bindParams, href, isNotFound, matchPath, normalizePathname, notFound, useMatch, usePathname, usePendingPathname, withBase } from "@k8ordo/router";
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { ParamsOf, Register, RegisteredParams } from "@k8ordo/router";
|
|
2
|
+
/** What a `redirect.ts` route file default-exports. */
|
|
3
|
+
type RedirectTarget = string | {
|
|
4
|
+
readonly to: string;
|
|
5
|
+
readonly permanent?: boolean;
|
|
6
|
+
};
|
|
7
|
+
/** The parameter a pattern ends in: `id` of `/:locale/posts/:id`. */
|
|
8
|
+
type LastParam<Pattern extends string> = Pattern extends `${string}/:${infer Rest}` ? Rest extends `${string}/:${string}` ? LastParam<`/${Rest}`> : Rest : never;
|
|
9
|
+
type Values<Pattern extends string> = ReadonlyArray<RegisteredParams<Pattern>[LastParam<Pattern> & keyof RegisteredParams<Pattern>]>;
|
|
10
|
+
type RegisteredAbove = Register extends {
|
|
11
|
+
pathsParams: infer M;
|
|
12
|
+
} ? M : object;
|
|
13
|
+
type Above<Pattern extends string> = Omit<Pattern extends keyof RegisteredAbove ? RegisteredAbove[Pattern] : ParamsOf<Pattern>, LastParam<Pattern>>;
|
|
14
|
+
/**
|
|
15
|
+
* What a `paths.ts` exports by default, under `mode: 'static'`: the values
|
|
16
|
+
* its directory's parameter takes, given the params of the directories above
|
|
17
|
+
* it — one call per combination of theirs. `Pattern` is the directory's own,
|
|
18
|
+
* ending in that parameter. The values are typed as a link takes them: what
|
|
19
|
+
* the page's `paramsSchema` produces (a number for a numeric id), or any
|
|
20
|
+
* value with one spelling where no schema says. The params above arrive as
|
|
21
|
+
* the layouts above the directory read them: spelled as the build writes
|
|
22
|
+
* them, then run through the `paramsSchema` exports of the layouts in the
|
|
23
|
+
* directories above, outer first, and typed as exactly what those produce —
|
|
24
|
+
* the string for a parameter none of them reads. When two `paths.ts` share a
|
|
25
|
+
* pattern under different layouts, the type is what both of them receive: a
|
|
26
|
+
* `year` one reads as a number and the other leaves alone is
|
|
27
|
+
* `number | string`. The page beside the file
|
|
28
|
+
* and a layout in its own directory take no part, since they may read the
|
|
29
|
+
* value the file is asked for. A `[year]` a layout above reads as a number
|
|
30
|
+
* arrives as one, whatever the `paths.ts` above returned, and values one of
|
|
31
|
+
* those schemas refuses are never handed over: the build names them instead.
|
|
32
|
+
* The build spells each value as `href` does (`café`, not `caf%C3%A9`, is
|
|
33
|
+
* the value).
|
|
34
|
+
*
|
|
35
|
+
* ```ts
|
|
36
|
+
* // src/routes/[locale]/posts/[id]/paths.ts
|
|
37
|
+
* const paths: Paths<'/:locale/posts/:id'> = async ({ locale }) =>
|
|
38
|
+
* (await postsIn(locale)).map((post) => post.id);
|
|
39
|
+
* export default paths;
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
type Paths<Pattern extends string> = (params: Above<Pattern>) => Values<Pattern> | Promise<Values<Pattern>>;
|
|
43
|
+
export { RedirectTarget as n, Paths as t };
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { payloadPathFor } from "./payload-path.mjs";
|
|
3
2
|
import { preloadClientFiles, readClientFiles } from "./client-files.mjs";
|
|
4
3
|
import { whenClientReady } from "./client-ready.mjs";
|
|
5
4
|
import { ShowNotFound } from "./fallback-boundary.mjs";
|
|
6
5
|
import { doneHydrating, whenHydrated } from "./hydrating-pathname.mjs";
|
|
7
6
|
import { isPayload } from "./is-payload.mjs";
|
|
7
|
+
import { payloadPathFor } from "./payload-path.mjs";
|
|
8
8
|
import { createPageCache, prefetchInViewUnderTouch, prefetchOnIntent } from "./prefetch.mjs";
|
|
9
9
|
import { Recover, markNavigated, navigationsApplied, reloadInstead } from "./recover.mjs";
|
|
10
10
|
import { NavigationGeneration, PathnameProvider, TreePathname, normalizePathname, useInterceptedNavigation, usePendingPathname, withoutBase } from "@k8ordo/router";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { answerAction, setPayloadReader } from "./app-router.mjs";
|
|
1
2
|
import { ACTION_ID_HEADER } from "./payload.mjs";
|
|
2
3
|
import { navigationsApplied } from "./recover.mjs";
|
|
3
|
-
import { answerAction, setPayloadReader } from "./app-router.mjs";
|
|
4
4
|
import { createFromFetch, createFromReadableStream, createTemporaryReferenceSet, encodeReply, setServerCallback } from "@vitejs/plugin-rsc/browser";
|
|
5
5
|
// src/runtime/browser-payloads.ts
|
|
6
6
|
/**
|
|
@@ -45,4 +45,6 @@ export type Cookies = {
|
|
|
45
45
|
export declare const createCookies: (incoming: ReadonlyMap<string, string>, url: URL) => {
|
|
46
46
|
readonly cookies: Cookies;
|
|
47
47
|
readonly lines: () => string[];
|
|
48
|
+
/** The cookies as they stand: what the request carried, written over. */
|
|
49
|
+
readonly current: () => ReadonlyMap<string, string>;
|
|
48
50
|
};
|
package/dist/runtime/cookies.mjs
CHANGED
|
@@ -79,7 +79,8 @@ const createCookies = (incoming, url) => {
|
|
|
79
79
|
say(name, scope, [`${name}=`, ...attributes(expired, secureByDefault)].join("; "));
|
|
80
80
|
}
|
|
81
81
|
},
|
|
82
|
-
lines: () => [...lines.values()]
|
|
82
|
+
lines: () => [...lines.values()],
|
|
83
|
+
current: () => new Map(values)
|
|
83
84
|
};
|
|
84
85
|
};
|
|
85
86
|
export { createCookies };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A guard or a route.ts that said `notFound()`: the handler answers with the
|
|
3
|
+
* nearest not-found.tsx, under 404, as for a URL nothing answers.
|
|
4
|
+
*/
|
|
5
|
+
export declare const SAID_NOT_FOUND: unique symbol;
|
|
6
|
+
export type SaidNotFound = typeof SAID_NOT_FOUND;
|
|
7
|
+
export declare const endedBy: (error: unknown) => Response | SaidNotFound;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Redirect, redirectResponse } from "./redirect.mjs";
|
|
2
|
+
import { isNotFound } from "@k8ordo/router";
|
|
3
|
+
// src/runtime/ended.ts
|
|
4
|
+
/**
|
|
5
|
+
* A guard or a route.ts that said `notFound()`: the handler answers with the
|
|
6
|
+
* nearest not-found.tsx, under 404, as for a URL nothing answers.
|
|
7
|
+
*/
|
|
8
|
+
const SAID_NOT_FOUND = Symbol("notFound()");
|
|
9
|
+
const endedBy = (error) => {
|
|
10
|
+
if (error instanceof Redirect) return redirectResponse(error.to, 303);
|
|
11
|
+
if (isNotFound(error)) return SAID_NOT_FOUND;
|
|
12
|
+
throw error;
|
|
13
|
+
};
|
|
14
|
+
export { SAID_NOT_FOUND, endedBy };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { whenClientReady } from "./client-ready.mjs";
|
|
2
1
|
import { connectAppRouter } from "./browser-payloads.mjs";
|
|
2
|
+
import { whenClientReady } from "./client-ready.mjs";
|
|
3
3
|
import { mount, renderedHere } from "./mount.mjs";
|
|
4
4
|
import { reportCaught } from "./page-boundary.mjs";
|
|
5
5
|
import { whenRevealed } from "./revealed.mjs";
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { Mode } from "../host.mjs";
|
|
2
|
+
import { HandlerContext } from "./request-scope.mjs";
|
|
3
|
+
import "virtual:k8ordo/locales";
|
|
2
4
|
/**
|
|
3
5
|
* Where Vite serves the application, as the build was told (`base`). A host
|
|
4
6
|
* serving the client build's files needs it to find them — `serve` reads it
|
|
@@ -29,18 +31,26 @@ export declare const payloadOf: (response: Response) => ArrayBuffer | undefined;
|
|
|
29
31
|
declare const everyAnswer: Readonly<Record<string, string>>;
|
|
30
32
|
/**
|
|
31
33
|
* What a `paths.ts` answers when asked for its parameter's values, given the
|
|
32
|
-
* params above it. The build into files
|
|
33
|
-
*
|
|
34
|
-
* the
|
|
35
|
-
*
|
|
34
|
+
* params above it as the build spelled them. The build into files, and
|
|
35
|
+
* `vite dev`, ask through here, from the RSC environment, so the file runs
|
|
36
|
+
* as the pages do — the application's aliases, and the module instances the
|
|
37
|
+
* pages read their data through. The params above are first read by the
|
|
38
|
+
* schemas of the layouts in the directories above the file's, and the file
|
|
39
|
+
* receives what they produced; when one of them refuses, the file is not
|
|
40
|
+
* asked and the answer is `null`. Otherwise whatever it returned is handed
|
|
41
|
+
* back as it is, as `values`: the build is what says a value is not one.
|
|
36
42
|
*/
|
|
37
|
-
export declare const pathValues: (file: string,
|
|
43
|
+
export declare const pathValues: (file: string, above: Readonly<Record<string, string>>) => Promise<{
|
|
44
|
+
readonly values: unknown;
|
|
45
|
+
} | null>;
|
|
38
46
|
/**
|
|
39
47
|
* The one entry both modes share: a request in, a page out. mode: 'server'
|
|
40
48
|
* calls it per request; mode: 'static' calls it at build time, for each
|
|
41
49
|
* route's HTML and again for its payload, and writes the answers to files.
|
|
42
50
|
* What it knows of the mode is compiled in (`K8ORDO_MODE`): whether a page
|
|
43
|
-
* gets the request, and whether a failed render may still stream.
|
|
51
|
+
* gets the request, and whether a failed render may still stream. `host` is
|
|
52
|
+
* what the caller offers beside the request — its `waitUntil` — and may be
|
|
53
|
+
* left out: the build, `vite dev` and `vite preview` hand none.
|
|
44
54
|
*/
|
|
45
|
-
export default function handler(request: Request): Promise<Response>;
|
|
55
|
+
export default function handler(request: Request, host?: HandlerContext): Promise<Response>;
|
|
46
56
|
export { everyAnswer as headers };
|
|
@@ -1,27 +1,25 @@
|
|
|
1
|
-
import { fileRequest, routeRequestOf } from "./request.mjs";
|
|
2
|
-
import { answer, inPhase, signingNonce, withRequest, writingFile } from "./request-scope.mjs";
|
|
3
|
-
import { methodNotAllowed, routeAnswerFor, runRoute } from "./route.mjs";
|
|
4
|
-
import { NOT_FOUND_SEGMENT } from "./pathname.mjs";
|
|
5
|
-
import { ACTION_ID_HEADER, CLIENT_DEPS_HEADER, NONCE_HEADER, NOT_FOUND_DIGEST, NOT_FOUND_HEADER, SERVER_STYLESHEETS_HEADER, SHELL_HEADER, clientDepsHeader, serverStylesheetsHeader } from "./payload.mjs";
|
|
6
|
-
import { isPayloadPath, pagePathFor } from "./payload-path.mjs";
|
|
7
1
|
import { noticeCancel } from "./cancel.mjs";
|
|
2
|
+
import { SAID_NOT_FOUND } from "./ended.mjs";
|
|
8
3
|
import { runGuards } from "./guard.mjs";
|
|
9
4
|
import { PageShown } from "./page-shown.mjs";
|
|
10
5
|
import { watchPage } from "./page-watch.mjs";
|
|
11
6
|
import { parseCatchAllParams, parseParams } from "./params.mjs";
|
|
12
|
-
import {
|
|
7
|
+
import { NOT_FOUND_SEGMENT } from "./pathname.mjs";
|
|
8
|
+
import { ACTION_ID_HEADER, CLIENT_DEPS_HEADER, NONCE_HEADER, NOT_FOUND_DIGEST, NOT_FOUND_HEADER, SERVER_STYLESHEETS_HEADER, SHELL_HEADER, clientDepsHeader, serverStylesheetsHeader } from "./payload.mjs";
|
|
9
|
+
import { isPayloadPath, pagePathFor } from "./payload-path.mjs";
|
|
10
|
+
import { Redirect, redirectResponse, resolveRedirects } from "./redirect.mjs";
|
|
13
11
|
import { renderMatch, renderNotFound } from "./render.mjs";
|
|
12
|
+
import { fileRequest, routeRequestOf } from "./request.mjs";
|
|
13
|
+
import { answer, currentCookies, inPhase, signingNonce, withRequest, writingFile } from "./request-scope.mjs";
|
|
14
|
+
import { methodNotAllowed, routeAnswerFor, runRoute } from "./route.mjs";
|
|
14
15
|
import { shellParams } from "./shell.mjs";
|
|
15
16
|
import { isNotFound, normalizePathname, withBase, withoutBase } from "@k8ordo/router";
|
|
16
17
|
import { jsx } from "react/jsx-runtime";
|
|
18
|
+
import "virtual:k8ordo/locales";
|
|
17
19
|
import { createTemporaryReferenceSet, decodeAction, decodeFormState, decodeReply, loadServerAction, renderToReadableStream } from "@vitejs/plugin-rsc/rsc/server";
|
|
18
20
|
import { catchAllSchemas, fallbacks, guards, pageModules, paramSchemas, paths, redirects, routeModules, routes, searchReaders } from "virtual:k8ordo/routes";
|
|
19
21
|
// src/runtime/entry.rsc.tsx
|
|
20
22
|
const redirectFor = resolveRedirects(redirects);
|
|
21
|
-
const redirectResponse = (to, status) => new Response(null, {
|
|
22
|
-
status,
|
|
23
|
-
headers: { location: to }
|
|
24
|
-
});
|
|
25
23
|
/**
|
|
26
24
|
* Where Vite serves the application, as the build was told (`base`). A host
|
|
27
25
|
* serving the client build's files needs it to find them — `serve` reads it
|
|
@@ -71,17 +69,23 @@ const withDeclared = (response) => {
|
|
|
71
69
|
};
|
|
72
70
|
/**
|
|
73
71
|
* What a `paths.ts` answers when asked for its parameter's values, given the
|
|
74
|
-
* params above it. The build into files
|
|
75
|
-
*
|
|
76
|
-
* the
|
|
77
|
-
*
|
|
72
|
+
* params above it as the build spelled them. The build into files, and
|
|
73
|
+
* `vite dev`, ask through here, from the RSC environment, so the file runs
|
|
74
|
+
* as the pages do — the application's aliases, and the module instances the
|
|
75
|
+
* pages read their data through. The params above are first read by the
|
|
76
|
+
* schemas of the layouts in the directories above the file's, and the file
|
|
77
|
+
* receives what they produced; when one of them refuses, the file is not
|
|
78
|
+
* asked and the answer is `null`. Otherwise whatever it returned is handed
|
|
79
|
+
* back as it is, as `values`: the build is what says a value is not one.
|
|
78
80
|
*/
|
|
79
|
-
const pathValues = async (file,
|
|
80
|
-
const
|
|
81
|
-
if (
|
|
82
|
-
const
|
|
81
|
+
const pathValues = async (file, above) => {
|
|
82
|
+
const supply = paths[file];
|
|
83
|
+
if (supply === void 0) throw new Error(`${file} is not a paths.ts of this build`);
|
|
84
|
+
const accepted = parseParams(supply.schemas, above);
|
|
85
|
+
if (accepted === null) return null;
|
|
86
|
+
const { default: values } = await supply.load();
|
|
83
87
|
if (typeof values !== "function") throw new TypeError(`${file} has to export default a function returning its parameter's values`);
|
|
84
|
-
return values(params);
|
|
88
|
+
return { values: await values(accepted.params) };
|
|
85
89
|
};
|
|
86
90
|
const NO_PARAMS = {
|
|
87
91
|
params: {},
|
|
@@ -245,13 +249,15 @@ const renderFailed = (error) => new Response(error instanceof Error ? error.mess
|
|
|
245
249
|
* calls it per request; mode: 'static' calls it at build time, for each
|
|
246
250
|
* route's HTML and again for its payload, and writes the answers to files.
|
|
247
251
|
* What it knows of the mode is compiled in (`K8ORDO_MODE`): whether a page
|
|
248
|
-
* gets the request, and whether a failed render may still stream.
|
|
252
|
+
* gets the request, and whether a failed render may still stream. `host` is
|
|
253
|
+
* what the caller offers beside the request — its `waitUntil` — and may be
|
|
254
|
+
* left out: the build, `vite dev` and `vite preview` hand none.
|
|
249
255
|
*/
|
|
250
|
-
function handler(request) {
|
|
256
|
+
function handler(request, host) {
|
|
251
257
|
return withRequest(request, async () => withDeclared(answer(await (import.meta.env.K8ORDO_MODE === "static" ? writingFile(() => respond(request).catch((error) => {
|
|
252
258
|
reportFailed("answering", new URL(request.url).pathname, error);
|
|
253
259
|
return renderFailed(error);
|
|
254
|
-
})) : respond(request)))));
|
|
260
|
+
})) : respond(request)))), host);
|
|
255
261
|
}
|
|
256
262
|
const reportFailed = (doing, pathname, error) => {
|
|
257
263
|
console.error(`k8ordo: ${doing} %s failed`, pathname, error);
|
|
@@ -270,22 +276,22 @@ const respond = async (request) => {
|
|
|
270
276
|
const { match, parsed, leaf, shell } = found;
|
|
271
277
|
const declared = match === null ? null : redirectFor(match.pattern, pathname);
|
|
272
278
|
if (declared !== null) return answerRedirect(request, declared);
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
const isAction = request.method === "POST";
|
|
279
|
+
const ended = match !== null && leaf !== void 0 && routeModules[match.pattern] !== void 0 ? await answerRoute(request, match, parsed, leaf, wantsPayload) : await refusedBeforePage(wantsPayload ? pageRequestOf(request, url, pathname) : request, found);
|
|
280
|
+
if (ended instanceof Response) return ended;
|
|
281
|
+
const disowned = ended === SAID_NOT_FOUND;
|
|
282
|
+
const isAction = request.method === "POST" && !disowned;
|
|
277
283
|
const addressed = request.headers.get(ACTION_ID_HEADER) !== null;
|
|
278
284
|
const temporaryReferences = createTemporaryReferenceSet();
|
|
279
285
|
const action = isAction ? await parsed.enter(() => inPhase("action", () => runAction(request, temporaryReferences))) : {};
|
|
280
286
|
if (action.redirect !== void 0 && !addressed) return redirectResponse(action.redirect, 303);
|
|
281
287
|
const answersPayload = wantsPayload || addressed;
|
|
282
288
|
const contentType = answersPayload ? PAYLOAD_TYPE : HTML_TYPE;
|
|
283
|
-
const missing = match === null || match.pattern.endsWith("/*");
|
|
289
|
+
const missing = disowned || match === null || match.pattern.endsWith("/*");
|
|
284
290
|
if (request.method === "HEAD" && missing) return new Response(null, {
|
|
285
291
|
status: 404,
|
|
286
292
|
headers: { "content-type": contentType }
|
|
287
293
|
});
|
|
288
|
-
const routeRequest = import.meta.env.K8ORDO_MODE === "server" ? routeRequestOf(request) : fileRequest;
|
|
294
|
+
const routeRequest = import.meta.env.K8ORDO_MODE === "server" ? routeRequestOf(request, currentCookies()) : fileRequest;
|
|
289
295
|
const ssr = await import.meta.viteRsc.loadModule("ssr", "index");
|
|
290
296
|
const readSearch = match === null || missing || leaf === void 0 ? void 0 : searchReaderOf(match.pattern, leaf);
|
|
291
297
|
const search = readSearch?.(url.searchParams);
|
|
@@ -318,8 +324,9 @@ const respond = async (request) => {
|
|
|
318
324
|
const page = match === null || missing || action.redirect !== void 0 || shown === void 0 ? null : watchPage(componentOf(shown), /* @__PURE__ */ jsx(PageShown, {}));
|
|
319
325
|
let showsPage = page?.followed === true;
|
|
320
326
|
let status = missing ? 404 : 200;
|
|
321
|
-
|
|
322
|
-
let
|
|
327
|
+
const disownedBy = disowned ? await nearestNotFound() : null;
|
|
328
|
+
let { enter } = disownedBy ?? parsed;
|
|
329
|
+
let rendered = render(disownedBy === null ? action.redirect === void 0 ? match === null ? renderNotFound(routes, pathname, routeRequest) : renderMatch(missing ? await withNotFound(match) : shell === void 0 ? match : {
|
|
323
330
|
...match,
|
|
324
331
|
params: shell.known
|
|
325
332
|
}, {
|
|
@@ -329,7 +336,7 @@ const respond = async (request) => {
|
|
|
329
336
|
page: page?.Page,
|
|
330
337
|
search,
|
|
331
338
|
shell: shell !== void 0
|
|
332
|
-
}) : null, enter, readSearch === void 0 ? void 0 : url.search, shell === void 0 ? void 0 : (await nearestNotFound()).tree);
|
|
339
|
+
}) : null : disownedBy.tree, enter, readSearch === void 0 ? void 0 : url.search, shell === void 0 ? void 0 : (await nearestNotFound()).tree);
|
|
333
340
|
if (page !== null && (!answersPayload || request.method === "HEAD")) {
|
|
334
341
|
const answered = await pageAnswer(rendered, page);
|
|
335
342
|
({rendered} = answered);
|
|
@@ -398,11 +405,13 @@ const answerRoute = async (request, match, parsed, module, wantsPayload) => {
|
|
|
398
405
|
params: parsed.params
|
|
399
406
|
}));
|
|
400
407
|
};
|
|
408
|
+
const pageRequestOf = (request, url, pathname) => request.method === "GET" || request.method === "HEAD" ? new Request(new URL(`${withBase(pathname)}${url.search}`, url), request) : request;
|
|
401
409
|
/**
|
|
402
410
|
* What answers a page's request before anything renders, if anything does: a
|
|
403
411
|
* method a page does not take, a Server Action from another origin, a guard.
|
|
404
412
|
*/
|
|
405
|
-
const refusedBeforePage = async (request,
|
|
413
|
+
const refusedBeforePage = async (request, { match, parsed }) => {
|
|
414
|
+
const url = new URL(request.url);
|
|
406
415
|
if (!METHODS.includes(request.method)) return new Response("method not allowed", {
|
|
407
416
|
status: 405,
|
|
408
417
|
headers: { allow: METHODS.join(", ") }
|
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import { CLIENT_STYLESHEETS } from "./stylesheets.mjs";
|
|
2
|
-
import { whenClientReady } from "./client-ready.mjs";
|
|
3
1
|
import { AppRouter } from "./app-router.mjs";
|
|
4
2
|
import { noticeCancel } from "./cancel.mjs";
|
|
3
|
+
import { whenClientReady } from "./client-ready.mjs";
|
|
5
4
|
import { DocumentRoot } from "./document-root.mjs";
|
|
6
|
-
import { PageShownContext } from "./page-shown.mjs";
|
|
7
5
|
import { scriptsBeforeStylesheetsStream } from "./head-scripts.mjs";
|
|
6
|
+
import { PageShownContext } from "./page-shown.mjs";
|
|
7
|
+
import { CLIENT_STYLESHEETS } from "./stylesheets.mjs";
|
|
8
8
|
import { use } from "react";
|
|
9
9
|
import { preinit, preloadModule } from "react-dom";
|
|
10
10
|
import { jsx } from "react/jsx-runtime";
|
|
11
|
+
import "virtual:k8ordo/locales";
|
|
11
12
|
import { createFromReadableStream, getClientEntryUrl } from "@vitejs/plugin-rsc/ssr";
|
|
12
13
|
import { renderToReadableStream } from "react-dom/server.edge";
|
|
13
14
|
import { injectRSCPayload } from "rsc-html-stream/server";
|
package/dist/runtime/guard.d.mts
CHANGED
|
@@ -1,28 +1,33 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SaidNotFound } from "./ended.mjs";
|
|
2
|
+
import { Match, ParamsOf } from "@k8ordo/router";
|
|
2
3
|
/**
|
|
3
|
-
* What a `guard.ts` receives: the request
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* What a `guard.ts` receives: the request for the page — a client
|
|
5
|
+
* navigation's payload request too, under the page's URL — and the params of
|
|
6
|
+
* the pattern its directory puts it under, as the pathname carried them — a
|
|
7
|
+
* string each, a rest param its segments — since a guard runs above the page
|
|
8
|
+
* and every layout, where no schema has typed them.
|
|
6
9
|
*/
|
|
7
10
|
export type GuardContext<P extends string = string> = {
|
|
8
11
|
readonly request: Request;
|
|
9
12
|
readonly params: ParamsOf<P>;
|
|
10
13
|
};
|
|
11
14
|
/**
|
|
12
|
-
* What a `guard.ts` default-exports.
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
15
|
+
* What a `guard.ts` default-exports. `redirect(url)` ends the request there
|
|
16
|
+
* with a `303`, `notFound()` with the nearest not-found.tsx under 404, and a
|
|
17
|
+
* `Response` returned — a `401`, a `403` — is the answer itself; returning
|
|
18
|
+
* nothing lets it through to what answers below. There is no `next()` to run
|
|
19
|
+
* the page and rewrite its answer: a page streams, and its headers leave
|
|
20
|
+
* before its body is written.
|
|
16
21
|
*/
|
|
17
22
|
export type Guard<P extends string = string> = (context: GuardContext<P>) => Response | void | Promise<Response | void>;
|
|
18
23
|
type AnyGuard = (context: {
|
|
19
24
|
readonly request: Request;
|
|
20
|
-
readonly params:
|
|
25
|
+
readonly params: Match['params'];
|
|
21
26
|
}) => unknown;
|
|
22
27
|
/**
|
|
23
28
|
* Runs the guards along a pattern, outer first, one at a time: an outer guard
|
|
24
29
|
* that ends the request leaves the inner ones unrun, which is what lets an
|
|
25
30
|
* inner guard assume what an outer one checked.
|
|
26
31
|
*/
|
|
27
|
-
export declare const runGuards: (guards: readonly AnyGuard[], context: Parameters<AnyGuard>[0]) => Promise<Response | null>;
|
|
32
|
+
export declare const runGuards: (guards: readonly AnyGuard[], context: Parameters<AnyGuard>[0]) => Promise<Response | SaidNotFound | null>;
|
|
28
33
|
export {};
|
package/dist/runtime/guard.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { endedBy } from "./ended.mjs";
|
|
1
2
|
import { inPhase } from "./request-scope.mjs";
|
|
2
3
|
// src/runtime/guard.ts
|
|
3
4
|
/**
|
|
@@ -7,9 +8,14 @@ import { inPhase } from "./request-scope.mjs";
|
|
|
7
8
|
*/
|
|
8
9
|
const runGuards = async (guards, context) => {
|
|
9
10
|
for (const guard of guards) {
|
|
10
|
-
|
|
11
|
+
let answer;
|
|
12
|
+
try {
|
|
13
|
+
answer = await inPhase("guard", () => guard(context));
|
|
14
|
+
} catch (error) {
|
|
15
|
+
return endedBy(error);
|
|
16
|
+
}
|
|
11
17
|
if (answer === void 0) continue;
|
|
12
|
-
if (!(answer instanceof Response)) throw new TypeError("a guard.ts
|
|
18
|
+
if (!(answer instanceof Response)) throw new TypeError("a guard.ts ends the request with redirect() or notFound(), or by returning a Response, and returns nothing to let it through");
|
|
13
19
|
return answer;
|
|
14
20
|
}
|
|
15
21
|
return null;
|
package/dist/runtime/mount.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { hydrateAs } from "./hydrating-pathname.mjs";
|
|
2
1
|
import { AppRouter } from "./app-router.mjs";
|
|
3
2
|
import { DocumentRoot } from "./document-root.mjs";
|
|
3
|
+
import { hydrateAs } from "./hydrating-pathname.mjs";
|
|
4
4
|
import { normalizePathname, withoutBase } from "@k8ordo/router";
|
|
5
5
|
import { jsx } from "react/jsx-runtime";
|
|
6
6
|
import { hydrateRoot } from "react-dom/client";
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { saysNotFound } from "./says-not-found.mjs";
|
|
3
2
|
import { FallbackCatch } from "./fallback-boundary.mjs";
|
|
4
|
-
import { reloadDocument } from "./reload.mjs";
|
|
5
3
|
import { isNavigated } from "./recover.mjs";
|
|
4
|
+
import { reloadDocument } from "./reload.mjs";
|
|
5
|
+
import { saysNotFound } from "./says-not-found.mjs";
|
|
6
6
|
import { Component } from "react";
|
|
7
7
|
// src/runtime/page-boundary.tsx
|
|
8
8
|
/** Whether the boundary answers `error` with a document load. */
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Match } from "@k8ordo/router";
|
|
1
2
|
/**
|
|
2
3
|
* A route file's `paramsSchema` export, in the one shape every validation library
|
|
3
4
|
* agrees on (Standard Schema). The framework runs it; the router only types
|
|
@@ -23,8 +24,8 @@ export type ParsedParams = {
|
|
|
23
24
|
};
|
|
24
25
|
/**
|
|
25
26
|
* Runs the schemas along a matched stack — outer layouts first, the page
|
|
26
|
-
* last — over the raw params
|
|
27
|
-
* it produced. `null` means some schema refused, which is the pattern not
|
|
27
|
+
* last — over the raw params (a string each, a rest param its segments), each
|
|
28
|
+
* replacing the ones it names with what it produced. `null` means some schema refused, which is the pattern not
|
|
28
29
|
* answering this pathname at all.
|
|
29
30
|
*
|
|
30
31
|
* A schema may write to the async context it runs in — `@k8ordo/i18n`
|
|
@@ -34,7 +35,7 @@ export type ParsedParams = {
|
|
|
34
35
|
* never to the caller: the static build calls the handler for every page
|
|
35
36
|
* from one context.
|
|
36
37
|
*/
|
|
37
|
-
export declare const parseParams: (schemas: readonly ParamsSchema[], raw:
|
|
38
|
+
export declare const parseParams: (schemas: readonly ParamsSchema[], raw: Match['params']) => ParsedParams | null;
|
|
38
39
|
/**
|
|
39
40
|
* The layouts' schemas above a not-found, run over the catch-all's params for
|
|
40
41
|
* what they write — never to refuse it: a catch-all answers what nothing else
|
|
@@ -44,5 +45,5 @@ export declare const parseParams: (schemas: readonly ParamsSchema[], raw: Readon
|
|
|
44
45
|
* strings the pathname carried either way, which is what a not-found's type
|
|
45
46
|
* says.
|
|
46
47
|
*/
|
|
47
|
-
export declare const parseCatchAllParams: (schemas: readonly ParamsSchema[], raw:
|
|
48
|
+
export declare const parseCatchAllParams: (schemas: readonly ParamsSchema[], raw: Match['params']) => ParsedParams;
|
|
48
49
|
export {};
|
package/dist/runtime/params.mjs
CHANGED
|
@@ -2,8 +2,8 @@ import { AsyncLocalStorage, AsyncResource } from "node:async_hooks";
|
|
|
2
2
|
// src/runtime/params.ts
|
|
3
3
|
/**
|
|
4
4
|
* Runs the schemas along a matched stack — outer layouts first, the page
|
|
5
|
-
* last — over the raw params
|
|
6
|
-
* it produced. `null` means some schema refused, which is the pattern not
|
|
5
|
+
* last — over the raw params (a string each, a rest param its segments), each
|
|
6
|
+
* replacing the ones it names with what it produced. `null` means some schema refused, which is the pattern not
|
|
7
7
|
* answering this pathname at all.
|
|
8
8
|
*
|
|
9
9
|
* A schema may write to the async context it runs in — `@k8ordo/i18n`
|