@k8ordo/framework 1.0.0 → 1.2.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 +13 -9
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +33 -0
- package/dist/generated.mjs +1 -2
- package/dist/index.mjs +1 -2
- package/dist/runtime/app-router.d.mts +7 -2
- package/dist/runtime/app-router.mjs +32 -6
- package/dist/runtime/browser-payloads.mjs +3 -2
- package/dist/runtime/client-ready.d.mts +27 -0
- package/dist/runtime/client-ready.mjs +89 -0
- package/dist/runtime/entry.browser.mjs +17 -12
- package/dist/runtime/entry.rsc.mjs +102 -43
- package/dist/runtime/fallback-boundary.d.mts +2 -1
- package/dist/runtime/guard.d.mts +2 -1
- package/dist/runtime/page-boundary.d.mts +2 -1
- package/dist/runtime/params.d.mts +2 -1
- package/dist/runtime/redirect-file.d.mts +2 -1
- package/dist/runtime/redirect.d.mts +2 -1
- package/dist/runtime/route.d.mts +2 -1
- package/dist/static-BaGdAVF1.d.mts +66 -0
- package/dist/vercel.d.mts +49 -3
- package/dist/vercel.mjs +34 -15
- package/dist/vite.d.mts +2 -59
- package/dist/vite.mjs +251 -176
- package/docs/GUIDE.md +26 -7
- package/docs/llms.txt +31 -7
- package/docs/references/csp.md +12 -3
- package/docs/references/deploy.md +157 -0
- package/docs/references/routing.md +79 -30
- package/package.json +19 -9
package/README.md
CHANGED
|
@@ -33,12 +33,13 @@ dependencies in either mode: the build bundles what the application runs.
|
|
|
33
33
|
|
|
34
34
|
<!-- peers -->
|
|
35
35
|
|
|
36
|
-
| Package
|
|
37
|
-
|
|
|
38
|
-
| `@k8ordo/router`
|
|
39
|
-
| `react`
|
|
40
|
-
| `react-dom`
|
|
41
|
-
| `vite`
|
|
36
|
+
| Package | Version | Required | Needed for |
|
|
37
|
+
| --------------------- | -------- | -------- | ------------------------------------------ |
|
|
38
|
+
| `@k8ordo/router` | ^1.2.0 | yes | routing, re-exported by the framework |
|
|
39
|
+
| `react` | ≥19.3.0 | yes | rendering |
|
|
40
|
+
| `react-dom` | ≥19.3.0 | yes | rendering |
|
|
41
|
+
| `vite` | ≥8.0.0 | yes | the build (`framework()` is a Vite plugin) |
|
|
42
|
+
| `oxc-transform-react` | ^0.152.0 | optional | `reactCompiler: true` (React Compiler) |
|
|
42
43
|
|
|
43
44
|
<!-- /peers -->
|
|
44
45
|
|
|
@@ -90,8 +91,9 @@ export default async function ProductPage({
|
|
|
90
91
|
```
|
|
91
92
|
|
|
92
93
|
```bash
|
|
93
|
-
vite dev
|
|
94
|
-
vite build
|
|
94
|
+
vite dev # renders per request, with Fast Refresh
|
|
95
|
+
vite build # static: dist/client/ is the site
|
|
96
|
+
k8ordo generate # only .k8ordo/, the route table and its types, for tsc in CI
|
|
95
97
|
```
|
|
96
98
|
|
|
97
99
|
Under `mode: 'server'` the build is a `dist/` that runs on its own — every
|
|
@@ -134,7 +136,9 @@ A value `paths` does not list — a product added after the build — is
|
|
|
134
136
|
answered by a `fallback.tsx` beside the page: the build writes it once as a
|
|
135
137
|
shell, the layouts in HTML, and the browser draws the page from the URL.
|
|
136
138
|
The build also writes the `_redirects` through which Netlify and Cloudflare
|
|
137
|
-
reach the shell, and `vercel()` the same rewrites for Vercel
|
|
139
|
+
reach the shell, and `vercel()` the same rewrites for Vercel — around them,
|
|
140
|
+
routes of your own: headers on every page, redirects, a rewrite decided by
|
|
141
|
+
`Accept`, a proxy to another origin.
|
|
138
142
|
|
|
139
143
|
## AI Agent Documentation
|
|
140
144
|
|
package/dist/cli.d.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|
package/dist/cli.mjs
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { resolveConfig } from "vite";
|
|
3
|
+
//#region src/cli.ts
|
|
4
|
+
const USAGE = `usage: k8ordo generate
|
|
5
|
+
|
|
6
|
+
Writes .k8ordo/ from the Vite config in this directory, as vite build does
|
|
7
|
+
when it begins, and builds nothing.
|
|
8
|
+
`;
|
|
9
|
+
/**
|
|
10
|
+
* `.k8ordo/` without a build: the config resolved as `vite build` resolves
|
|
11
|
+
* it, and the engine's plugin, which knows the routes directory and the
|
|
12
|
+
* mode, asked to generate. No environment is built and no page rendered, so
|
|
13
|
+
* `paths` and whatever data a page reads are never touched.
|
|
14
|
+
*/
|
|
15
|
+
const generate = async () => {
|
|
16
|
+
const config = await resolveConfig({}, "build", "production", "production");
|
|
17
|
+
const engine = config.plugins.find(({ name }) => name === "k8ordo:engine");
|
|
18
|
+
if (engine === void 0) throw new Error(`the Vite config in ${config.root} does not use framework() from @k8ordo/framework/vite, so there is nothing to generate`);
|
|
19
|
+
await engine.api.generate();
|
|
20
|
+
config.logger.info("k8ordo: wrote .k8ordo/");
|
|
21
|
+
};
|
|
22
|
+
const args = process.argv.slice(2);
|
|
23
|
+
if (args.length !== 1 || args[0] !== "generate") {
|
|
24
|
+
process.stderr.write(USAGE);
|
|
25
|
+
process.exitCode = 1;
|
|
26
|
+
} else try {
|
|
27
|
+
await generate();
|
|
28
|
+
} catch (error) {
|
|
29
|
+
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
|
|
30
|
+
process.exitCode = 1;
|
|
31
|
+
}
|
|
32
|
+
//#endregion
|
|
33
|
+
export {};
|
package/dist/generated.mjs
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export { defineRoutes };
|
|
1
|
+
export { defineRoutes } from "@k8ordo/router";
|
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export { bindParams, href, isNotFound, matchPath, navigateTo, normalizePathname, notFound, useMatch, usePathname, usePendingPathname, withBase };
|
|
1
|
+
export { bindParams, href, isNotFound, matchPath, navigateTo, normalizePathname, notFound, useMatch, usePathname, usePendingPathname, withBase } from "@k8ordo/router";
|
|
@@ -2,8 +2,13 @@ import { Payload } from "./payload.mjs";
|
|
|
2
2
|
import { ReactNode } from "react";
|
|
3
3
|
//#region src/runtime/app-router.d.ts
|
|
4
4
|
export declare const setDocumentClient: (client: string) => void;
|
|
5
|
-
/**
|
|
6
|
-
export declare const
|
|
5
|
+
/** Taken before a Server Action is sent, and handed to `answerAction`. */
|
|
6
|
+
export declare const navigationsApplied: () => number;
|
|
7
|
+
/**
|
|
8
|
+
* What a Server Action's answer does to the document, once it is read.
|
|
9
|
+
* `sent` is `navigationsApplied()` as the action was sent.
|
|
10
|
+
*/
|
|
11
|
+
export declare const answerAction: (payload: Payload, sent: number) => Promise<unknown>;
|
|
7
12
|
export declare const setPayloadReader: (read: (stream: ReadableStream<Uint8Array>) => Promise<Payload>) => void;
|
|
8
13
|
/**
|
|
9
14
|
* The client half under the framework: the tree comes from the server, so
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { payloadPathFor } from "./payload-path.mjs";
|
|
3
|
+
import { whenClientReady } from "./client-ready.mjs";
|
|
3
4
|
import { ShowNotFound } from "./fallback-boundary.mjs";
|
|
4
5
|
import { isPayload } from "./is-payload.mjs";
|
|
5
6
|
import { createPrefetchCache, prefetchTargetOf } from "./prefetch.mjs";
|
|
@@ -34,15 +35,36 @@ const setDocumentClient = (client) => {
|
|
|
34
35
|
* document load brings the script that can.
|
|
35
36
|
*/
|
|
36
37
|
const canRender = (payload) => payload.client === documentClient;
|
|
37
|
-
/**
|
|
38
|
-
|
|
38
|
+
/**
|
|
39
|
+
* How a payload is waited for before its tree is applied: until the client
|
|
40
|
+
* components it renders have loaded — one still loading suspends where it
|
|
41
|
+
* renders, and a `<Suspense>` the page just brought shows its fallback in a
|
|
42
|
+
* page already on screen — and what the page sends once its data is in,
|
|
43
|
+
* except under a `<Suspense>`, whose fallback stands in for it as a
|
|
44
|
+
* transition leaves it.
|
|
45
|
+
*/
|
|
46
|
+
const WHEN_APPLIED = { inSuspense: "leave" };
|
|
47
|
+
/**
|
|
48
|
+
* How many trees navigations have applied — the router's, and `vite dev`'s
|
|
49
|
+
* reload of the page on screen. A Server Action's answer is not counted:
|
|
50
|
+
* answers to actions called in a row apply one after another.
|
|
51
|
+
*/
|
|
52
|
+
let navigations = 0;
|
|
53
|
+
/** Taken before a Server Action is sent, and handed to `answerAction`. */
|
|
54
|
+
const navigationsApplied = () => navigations;
|
|
55
|
+
/**
|
|
56
|
+
* What a Server Action's answer does to the document, once it is read.
|
|
57
|
+
* `sent` is `navigationsApplied()` as the action was sent.
|
|
58
|
+
*/
|
|
59
|
+
const answerAction = async (payload, sent) => {
|
|
39
60
|
mounted?.forgetPrefetched();
|
|
40
61
|
if (payload.redirect !== void 0) {
|
|
41
62
|
await navigation.navigate(payload.redirect).finished;
|
|
42
63
|
return;
|
|
43
64
|
}
|
|
44
65
|
if (!canRender(payload)) return reloadInstead();
|
|
45
|
-
|
|
66
|
+
await whenClientReady([payload.tree, payload.notFound], WHEN_APPLIED);
|
|
67
|
+
if (navigations === sent) mounted?.apply(payload);
|
|
46
68
|
return payload.returnValue;
|
|
47
69
|
};
|
|
48
70
|
let readPayload = () => Promise.reject(/* @__PURE__ */ new Error("k8ordo: payloads are read in the browser only"));
|
|
@@ -62,8 +84,9 @@ const setPayloadReader = (read) => {
|
|
|
62
84
|
const streams = /* @__PURE__ */ new WeakMap();
|
|
63
85
|
/**
|
|
64
86
|
* A page's payload, or `null` when what came back is not one — a file the
|
|
65
|
-
* host serves, or a URL nothing answers.
|
|
66
|
-
* components
|
|
87
|
+
* host serves, or a URL nothing answers. It resolves once the client
|
|
88
|
+
* components the page renders have loaded, so a prefetch has those in hand
|
|
89
|
+
* too.
|
|
67
90
|
*/
|
|
68
91
|
const fetchPage = async (payloadPath, signal) => {
|
|
69
92
|
signal.throwIfAborted();
|
|
@@ -75,6 +98,7 @@ const fetchPage = async (payloadPath, signal) => {
|
|
|
75
98
|
const response = await fetch(payloadPath, { signal: request.signal });
|
|
76
99
|
if (!isPayload(response)) return null;
|
|
77
100
|
const payload = await readPayload(response.body);
|
|
101
|
+
await whenClientReady([payload.tree, payload.notFound], WHEN_APPLIED);
|
|
78
102
|
streams.set(payload, {
|
|
79
103
|
cancel: () => {
|
|
80
104
|
request.abort();
|
|
@@ -191,6 +215,7 @@ function AppRouter({ pathname, search, tree, notFound }) {
|
|
|
191
215
|
if (payload === null || location.href !== url) return;
|
|
192
216
|
if (!canRender(payload)) return reloadInstead();
|
|
193
217
|
markNavigated();
|
|
218
|
+
navigations += 1;
|
|
194
219
|
const next = hold(streaming.current, payload);
|
|
195
220
|
searchApplied.current = payload.search;
|
|
196
221
|
startTransition(() => {
|
|
@@ -242,6 +267,7 @@ function AppRouter({ pathname, search, tree, notFound }) {
|
|
|
242
267
|
},
|
|
243
268
|
apply: (next) => {
|
|
244
269
|
markNavigated();
|
|
270
|
+
navigations += 1;
|
|
245
271
|
searchApplied.current = next.search;
|
|
246
272
|
setLatest(hold(streaming.current, next));
|
|
247
273
|
},
|
|
@@ -269,4 +295,4 @@ function AppRouter({ pathname, search, tree, notFound }) {
|
|
|
269
295
|
});
|
|
270
296
|
}
|
|
271
297
|
//#endregion
|
|
272
|
-
export { AppRouter, answerAction, setDocumentClient, setPayloadReader };
|
|
298
|
+
export { AppRouter, answerAction, navigationsApplied, setDocumentClient, setPayloadReader };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ACTION_ID_HEADER } from "./payload.mjs";
|
|
2
|
-
import { answerAction, setPayloadReader } from "./app-router.mjs";
|
|
2
|
+
import { answerAction, navigationsApplied, setPayloadReader } from "./app-router.mjs";
|
|
3
3
|
import { createFromFetch, createFromReadableStream, createTemporaryReferenceSet, encodeReply, setServerCallback } from "@vitejs/plugin-rsc/browser";
|
|
4
4
|
//#region src/runtime/browser-payloads.ts
|
|
5
5
|
/**
|
|
@@ -10,13 +10,14 @@ import { createFromFetch, createFromReadableStream, createTemporaryReferenceSet,
|
|
|
10
10
|
const connectAppRouter = () => {
|
|
11
11
|
setPayloadReader((stream) => createFromReadableStream(stream));
|
|
12
12
|
setServerCallback(async (id, args) => {
|
|
13
|
+
const sent = navigationsApplied();
|
|
13
14
|
const temporaryReferences = createTemporaryReferenceSet();
|
|
14
15
|
const payload = await createFromFetch(fetch(location.href, {
|
|
15
16
|
method: "POST",
|
|
16
17
|
headers: { [ACTION_ID_HEADER]: id },
|
|
17
18
|
body: await encodeReply(args, { temporaryReferences })
|
|
18
19
|
}), { temporaryReferences });
|
|
19
|
-
return answerAction(payload);
|
|
20
|
+
return answerAction(payload, sent);
|
|
20
21
|
});
|
|
21
22
|
};
|
|
22
23
|
//#endregion
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
//#region src/runtime/client-ready.d.ts
|
|
2
|
+
export type ClientReadyOptions = {
|
|
3
|
+
/**
|
|
4
|
+
* What to do with a row still on its way inside a `<Suspense>`: `'wait'`
|
|
5
|
+
* for it, or `'leave'` it to that boundary's fallback, as a transition
|
|
6
|
+
* leaves it — a page streaming what it put under a boundary of its own.
|
|
7
|
+
*/
|
|
8
|
+
readonly inSuspense: 'wait' | 'leave';
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Settles once the client components a payload's trees name have loaded.
|
|
12
|
+
* The Flight client hands each one over as a lazy element type and starts
|
|
13
|
+
* importing it as the payload is read; a render that meets one still loading
|
|
14
|
+
* suspends there, and the nearest `<Suspense>` shows its fallback — in place
|
|
15
|
+
* of the server's HTML on a first render, and in place of a page a
|
|
16
|
+
* navigation has just put on screen.
|
|
17
|
+
*
|
|
18
|
+
* Walks every element's `type` and `props` (`children` and any other prop
|
|
19
|
+
* holding elements), arrays and plain objects, each once, since a payload
|
|
20
|
+
* shares and nests them. A row still on its way (a lazy node: what a page
|
|
21
|
+
* sends once its data is in) is waited for and walked too, except as
|
|
22
|
+
* `inSuspense` says. What fails — a row the server could not render, a
|
|
23
|
+
* module that would not load — ends the wait there, for the render to meet
|
|
24
|
+
* where an error boundary takes it.
|
|
25
|
+
*/
|
|
26
|
+
export declare const whenClientReady: (trees: readonly unknown[], { inSuspense }: ClientReadyOptions) => Promise<void>;
|
|
27
|
+
//#endregion
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
//#region src/runtime/client-ready.ts
|
|
2
|
+
/**
|
|
3
|
+
* The shapes React shares between `React.lazy`, the Flight client and the
|
|
4
|
+
* reconciler, and the only ones read here: an element's `$$typeof`, `type`
|
|
5
|
+
* and `props`, and a lazy's `$$typeof`, `_payload` and `_init` — which
|
|
6
|
+
* returns what it stands for, or throws a thenable while that is on its way.
|
|
7
|
+
*/
|
|
8
|
+
const ELEMENT = Symbol.for("react.transitional.element");
|
|
9
|
+
const LAZY = Symbol.for("react.lazy");
|
|
10
|
+
const SUSPENSE = Symbol.for("react.suspense");
|
|
11
|
+
const isObject = (value) => typeof value === "object" && value !== null;
|
|
12
|
+
const isLazy = (value) => isObject(value) && value.$$typeof === LAZY;
|
|
13
|
+
const isElement = (value) => value.$$typeof === ELEMENT;
|
|
14
|
+
const isThenable = (value) => isObject(value) && typeof value.then === "function";
|
|
15
|
+
const isPlain = (value) => {
|
|
16
|
+
const prototype = Object.getPrototypeOf(value);
|
|
17
|
+
return prototype === Object.prototype || prototype === null;
|
|
18
|
+
};
|
|
19
|
+
const whenSettled = (thenable) => new Promise((resolve) => {
|
|
20
|
+
thenable.then(() => {
|
|
21
|
+
resolve();
|
|
22
|
+
}, () => {
|
|
23
|
+
resolve();
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
/**
|
|
27
|
+
* Settles once the client components a payload's trees name have loaded.
|
|
28
|
+
* The Flight client hands each one over as a lazy element type and starts
|
|
29
|
+
* importing it as the payload is read; a render that meets one still loading
|
|
30
|
+
* suspends there, and the nearest `<Suspense>` shows its fallback — in place
|
|
31
|
+
* of the server's HTML on a first render, and in place of a page a
|
|
32
|
+
* navigation has just put on screen.
|
|
33
|
+
*
|
|
34
|
+
* Walks every element's `type` and `props` (`children` and any other prop
|
|
35
|
+
* holding elements), arrays and plain objects, each once, since a payload
|
|
36
|
+
* shares and nests them. A row still on its way (a lazy node: what a page
|
|
37
|
+
* sends once its data is in) is waited for and walked too, except as
|
|
38
|
+
* `inSuspense` says. What fails — a row the server could not render, a
|
|
39
|
+
* module that would not load — ends the wait there, for the render to meet
|
|
40
|
+
* where an error boundary takes it.
|
|
41
|
+
*/
|
|
42
|
+
const whenClientReady = async (trees, { inSuspense }) => {
|
|
43
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
44
|
+
let waiting = [];
|
|
45
|
+
const read = (lazy, inside, isType) => {
|
|
46
|
+
let value;
|
|
47
|
+
try {
|
|
48
|
+
value = lazy._init(lazy._payload);
|
|
49
|
+
} catch (thrown) {
|
|
50
|
+
if (isThenable(thrown) && (isType || !inside || inSuspense === "wait")) waiting.push({
|
|
51
|
+
until: thrown,
|
|
52
|
+
again: () => {
|
|
53
|
+
read(lazy, inside, isType);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (!isType) visit(value, inside);
|
|
59
|
+
};
|
|
60
|
+
const visit = (value, inside) => {
|
|
61
|
+
if (!isObject(value)) return;
|
|
62
|
+
if (isLazy(value)) {
|
|
63
|
+
read(value, inside, false);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (seen.has(value)) return;
|
|
67
|
+
seen.add(value);
|
|
68
|
+
if (Array.isArray(value)) {
|
|
69
|
+
for (const item of value) visit(item, inside);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (isElement(value)) {
|
|
73
|
+
if (isLazy(value.type)) read(value.type, inside, true);
|
|
74
|
+
const below = inside || value.type === SUSPENSE;
|
|
75
|
+
for (const [key, prop] of Object.entries(value.props)) visit(prop, key === "fallback" ? inside : below);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
if (isPlain(value)) for (const prop of Object.values(value)) visit(prop, inside);
|
|
79
|
+
};
|
|
80
|
+
for (const tree of trees) visit(tree, false);
|
|
81
|
+
while (waiting.length > 0) {
|
|
82
|
+
const round = waiting;
|
|
83
|
+
waiting = [];
|
|
84
|
+
await Promise.all(round.map(({ until }) => whenSettled(until)));
|
|
85
|
+
for (const { again } of round) again();
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
//#endregion
|
|
89
|
+
export { whenClientReady };
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { whenClientReady } from "./client-ready.mjs";
|
|
1
2
|
import { AppRouter, setDocumentClient } from "./app-router.mjs";
|
|
2
3
|
import { connectAppRouter } from "./browser-payloads.mjs";
|
|
3
4
|
import { mount } from "./mount.mjs";
|
|
@@ -8,17 +9,21 @@ import { createFromReadableStream } from "@vitejs/plugin-rsc/browser";
|
|
|
8
9
|
import { rscStream } from "rsc-html-stream/client";
|
|
9
10
|
//#region src/runtime/entry.browser.tsx
|
|
10
11
|
connectAppRouter();
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
12
|
+
const start = async () => {
|
|
13
|
+
const payload = await createFromReadableStream(rscStream);
|
|
14
|
+
setDocumentClient(payload.client);
|
|
15
|
+
await whenRevealed();
|
|
16
|
+
await whenClientReady([payload.tree, payload.notFound], { inSuspense: "wait" });
|
|
17
|
+
mount(document, /* @__PURE__ */ jsx(AppRouter, {
|
|
18
|
+
notFound: payload.notFound,
|
|
19
|
+
pathname: payload.pathname,
|
|
20
|
+
search: payload.search,
|
|
21
|
+
tree: payload.tree
|
|
22
|
+
}), payload.pathname, {
|
|
23
|
+
formState: payload.formState,
|
|
24
|
+
onCaughtError: reportCaught
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
start();
|
|
23
28
|
//#endregion
|
|
24
29
|
export {};
|
|
@@ -16,7 +16,7 @@ import { shellParams } from "./shell.mjs";
|
|
|
16
16
|
import { isNotFound, normalizePathname, withBase, withoutBase } from "@k8ordo/router";
|
|
17
17
|
import { jsx } from "react/jsx-runtime";
|
|
18
18
|
import { createTemporaryReferenceSet, decodeAction, decodeFormState, decodeReply, loadServerAction, renderToReadableStream } from "@vitejs/plugin-rsc/rsc/server";
|
|
19
|
-
import { catchAllSchemas, fallbacks, guards, paramSchemas, redirects, routeModules, routes, searchReaders } from "virtual:k8ordo/routes";
|
|
19
|
+
import { catchAllSchemas, fallbacks, guards, pageModules, paramSchemas, redirects, routeModules, routes, searchReaders } from "virtual:k8ordo/routes";
|
|
20
20
|
//#region src/runtime/entry.rsc.tsx
|
|
21
21
|
const redirectFor = resolveRedirects(redirects);
|
|
22
22
|
const redirectResponse = (to, status) => new Response(null, {
|
|
@@ -34,6 +34,92 @@ const base = import.meta.env.BASE_URL;
|
|
|
34
34
|
* serves a build into files — as a static host would — or runs the handler.
|
|
35
35
|
*/
|
|
36
36
|
const mode = import.meta.env.K8ORDO_MODE === "static" ? "static" : "server";
|
|
37
|
+
const NO_PARAMS = {
|
|
38
|
+
params: {},
|
|
39
|
+
enter: (fn) => fn()
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* The pattern that answers a pathname. A param a schema refuses is a
|
|
43
|
+
* pathname the pattern does not answer, so the walk goes on to whatever the
|
|
44
|
+
* table declares next — the catch-all in the end. A catch-all answers
|
|
45
|
+
* whatever its params hold: a 404 is already what a refused param means. The
|
|
46
|
+
* layouts' schemas above it still run, for what they write — `/en/missing`
|
|
47
|
+
* renders in the locale its URL names. What answers runs inside
|
|
48
|
+
* `parsed.enter`, so it sees what the schemas of the pattern that answered
|
|
49
|
+
* wrote to the async context, and nothing a refused pattern's did.
|
|
50
|
+
*
|
|
51
|
+
* A page's schema is an export of the page, and the table reads a page only
|
|
52
|
+
* when a request reaches it; `routes.match` decides synchronously, so it is
|
|
53
|
+
* asked for every pattern that fits, and the walk reads each file before
|
|
54
|
+
* running the schemas along its stack, in the table's order.
|
|
55
|
+
*/
|
|
56
|
+
const find = async (pathname) => {
|
|
57
|
+
const fits = [];
|
|
58
|
+
routes.match(pathname, (fit) => {
|
|
59
|
+
fits.push(fit);
|
|
60
|
+
return false;
|
|
61
|
+
});
|
|
62
|
+
for (const fit of fits) {
|
|
63
|
+
if (fit.pattern.endsWith("/*")) return {
|
|
64
|
+
match: fit,
|
|
65
|
+
parsed: parseCatchAllParams(catchAllSchemas[fit.pattern] ?? [], fit.params)
|
|
66
|
+
};
|
|
67
|
+
const fallback = fallbacks[fit.pattern];
|
|
68
|
+
const known = fallback === void 0 ? null : shellParams(fit.params, fallback.open);
|
|
69
|
+
if (fallback !== void 0 && known !== null) {
|
|
70
|
+
const accepted = parseParams(fallback.schemas, known);
|
|
71
|
+
if (accepted === null) continue;
|
|
72
|
+
return {
|
|
73
|
+
match: fit,
|
|
74
|
+
parsed: accepted,
|
|
75
|
+
shell: {
|
|
76
|
+
pattern: fit.pattern,
|
|
77
|
+
load: fallback.load,
|
|
78
|
+
known
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
const leaf = await (routeModules[fit.pattern] ?? pageModules[fit.pattern])?.();
|
|
83
|
+
const accepted = parseParams(leaf === void 0 ? [] : paramSchemas[fit.pattern]?.(leaf) ?? [], fit.params);
|
|
84
|
+
if (accepted === null) continue;
|
|
85
|
+
return {
|
|
86
|
+
match: fit,
|
|
87
|
+
parsed: accepted,
|
|
88
|
+
leaf
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
match: null,
|
|
93
|
+
parsed: NO_PARAMS
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
/** What a route file renders: its default export. */
|
|
97
|
+
const componentOf = (leaf) => leaf.default;
|
|
98
|
+
/**
|
|
99
|
+
* A catch-all's match with its not-found in the leaf's place, read now. The
|
|
100
|
+
* table holds `lazy()` there, which the render would read by itself; read
|
|
101
|
+
* here, a not-found that fails to load fails the request, as a page does,
|
|
102
|
+
* rather than a response already under way.
|
|
103
|
+
*/
|
|
104
|
+
const withNotFound = async (match) => {
|
|
105
|
+
const leaf = await pageModules[match.pattern]?.();
|
|
106
|
+
return leaf === void 0 ? match : {
|
|
107
|
+
...match,
|
|
108
|
+
stack: [...match.stack.slice(0, -1), componentOf(leaf)]
|
|
109
|
+
};
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* Per page module, what reads its search: `urlReader` builds the codec each
|
|
113
|
+
* time it is called, and a page's is the same for every request.
|
|
114
|
+
*/
|
|
115
|
+
const searchReads = /* @__PURE__ */ new WeakMap();
|
|
116
|
+
const searchReaderOf = (pattern, leaf) => {
|
|
117
|
+
const make = searchReaders[pattern];
|
|
118
|
+
if (make === void 0) return void 0;
|
|
119
|
+
const made = searchReads.get(leaf) ?? make(leaf);
|
|
120
|
+
searchReads.set(leaf, made);
|
|
121
|
+
return made;
|
|
122
|
+
};
|
|
37
123
|
/**
|
|
38
124
|
* A `redirect.ts` target is written the way the route table is: a pathname
|
|
39
125
|
* from the application's root, which the URL spells with the base in front.
|
|
@@ -119,7 +205,10 @@ const renderFailed = (error) => new Response(error instanceof Error ? error.mess
|
|
|
119
205
|
* gets the request, and whether a failed render may still stream.
|
|
120
206
|
*/
|
|
121
207
|
function handler(request) {
|
|
122
|
-
return withRequest(request, async () => answer(await (import.meta.env.K8ORDO_MODE === "static" ? writingFile(() => respond(request))
|
|
208
|
+
return withRequest(request, async () => answer(await (import.meta.env.K8ORDO_MODE === "static" ? writingFile(() => respond(request).catch((error) => {
|
|
209
|
+
console.error("k8ordo: answering %s failed", new URL(request.url).pathname, error);
|
|
210
|
+
return renderFailed(error);
|
|
211
|
+
})) : respond(request))));
|
|
123
212
|
}
|
|
124
213
|
const respond = async (request) => {
|
|
125
214
|
const url = new URL(request.url);
|
|
@@ -130,35 +219,7 @@ const respond = async (request) => {
|
|
|
130
219
|
});
|
|
131
220
|
const wantsPayload = isPayloadPath(own);
|
|
132
221
|
const pathname = wantsPayload ? pagePathFor(own) : own;
|
|
133
|
-
|
|
134
|
-
params: {},
|
|
135
|
-
enter: (fn) => fn()
|
|
136
|
-
};
|
|
137
|
-
let shell = void 0;
|
|
138
|
-
const match = routes.match(pathname, (found) => {
|
|
139
|
-
if (found.pattern.endsWith("/*")) {
|
|
140
|
-
parsed = parseCatchAllParams(catchAllSchemas[found.pattern] ?? [], found.params);
|
|
141
|
-
return true;
|
|
142
|
-
}
|
|
143
|
-
const fallback = fallbacks[found.pattern];
|
|
144
|
-
const known = fallback === void 0 ? null : shellParams(found.params, fallback.open);
|
|
145
|
-
if (fallback !== void 0 && known !== null) {
|
|
146
|
-
const accepted = parseParams(fallback.schemas, known);
|
|
147
|
-
if (accepted === null) return false;
|
|
148
|
-
parsed = accepted;
|
|
149
|
-
shell = {
|
|
150
|
-
pattern: found.pattern,
|
|
151
|
-
component: fallback.component,
|
|
152
|
-
known
|
|
153
|
-
};
|
|
154
|
-
return true;
|
|
155
|
-
}
|
|
156
|
-
shell = void 0;
|
|
157
|
-
const accepted = parseParams(paramSchemas[found.pattern] ?? [], found.params);
|
|
158
|
-
if (accepted === null) return false;
|
|
159
|
-
parsed = accepted;
|
|
160
|
-
return true;
|
|
161
|
-
});
|
|
222
|
+
const { match, parsed, leaf, shell } = await find(pathname);
|
|
162
223
|
const declared = match === null ? null : redirectFor(match.pattern, pathname);
|
|
163
224
|
if (declared !== null) {
|
|
164
225
|
if (request.method !== "GET" && request.method !== "HEAD") return new Response("method not allowed", {
|
|
@@ -167,7 +228,7 @@ const respond = async (request) => {
|
|
|
167
228
|
});
|
|
168
229
|
return redirectResponse(locationOf(declared.to), declared.permanent ? 308 : 307);
|
|
169
230
|
}
|
|
170
|
-
const module = match === null ? void 0 :
|
|
231
|
+
const module = match === null || routeModules[match.pattern] === void 0 ? void 0 : leaf;
|
|
171
232
|
if (match !== null && module !== void 0) {
|
|
172
233
|
if (wantsPayload) return new Response("not a page", {
|
|
173
234
|
status: 404,
|
|
@@ -208,12 +269,12 @@ const respond = async (request) => {
|
|
|
208
269
|
});
|
|
209
270
|
const routeRequest = import.meta.env.K8ORDO_MODE === "server" ? routeRequestOf(request) : fileRequest;
|
|
210
271
|
const ssr = await import.meta.viteRsc.loadModule("ssr", "index");
|
|
211
|
-
const readSearch = match === null || missing ||
|
|
272
|
+
const readSearch = match === null || missing || leaf === void 0 ? void 0 : searchReaderOf(match.pattern, leaf);
|
|
212
273
|
const search = readSearch?.(url.searchParams);
|
|
213
|
-
const nearestNotFound = () => {
|
|
274
|
+
const nearestNotFound = async () => {
|
|
214
275
|
const nearest = matchNotFound(pathname, shell !== void 0);
|
|
215
276
|
return {
|
|
216
|
-
tree: nearest.match === null ? renderNotFound(routes, pathname, routeRequest) : renderMatch(nearest.match, {
|
|
277
|
+
tree: nearest.match === null ? renderNotFound(routes, pathname, routeRequest) : renderMatch(await withNotFound(nearest.match), {
|
|
217
278
|
pathname,
|
|
218
279
|
params: nearest.parsed.params,
|
|
219
280
|
request: routeRequest
|
|
@@ -231,11 +292,12 @@ const respond = async (request) => {
|
|
|
231
292
|
formState: action.formState,
|
|
232
293
|
redirect: action.redirect
|
|
233
294
|
}, enter, isAction ? temporaryReferences : void 0);
|
|
234
|
-
const
|
|
295
|
+
const shown = shell === void 0 ? leaf : await shell.load();
|
|
296
|
+
const page = match === null || missing || action.redirect !== void 0 || shown === void 0 ? null : watchPage(componentOf(shown), /* @__PURE__ */ jsx(PageShown, {}));
|
|
235
297
|
let showsPage = page?.followed === true;
|
|
236
298
|
let status = missing ? 404 : 200;
|
|
237
299
|
let { enter } = parsed;
|
|
238
|
-
let rendered = render(action.redirect === void 0 ? match === null ? renderNotFound(routes, pathname, routeRequest) : renderMatch(shell === void 0 ? match : {
|
|
300
|
+
let rendered = render(action.redirect === void 0 ? match === null ? renderNotFound(routes, pathname, routeRequest) : renderMatch(missing ? await withNotFound(match) : shell === void 0 ? match : {
|
|
239
301
|
...match,
|
|
240
302
|
params: shell.known
|
|
241
303
|
}, {
|
|
@@ -245,7 +307,7 @@ const respond = async (request) => {
|
|
|
245
307
|
page: page?.Page,
|
|
246
308
|
search,
|
|
247
309
|
shell: shell !== void 0
|
|
248
|
-
}) : null, enter, readSearch === void 0 ? void 0 : url.search, shell === void 0 ? void 0 : nearestNotFound().tree);
|
|
310
|
+
}) : null, enter, readSearch === void 0 ? void 0 : url.search, shell === void 0 ? void 0 : (await nearestNotFound()).tree);
|
|
249
311
|
if (page !== null && (!answersPayload || request.method === "HEAD")) {
|
|
250
312
|
const [forAnswer, forWatch] = rendered.stream.tee();
|
|
251
313
|
rendered = {
|
|
@@ -260,7 +322,7 @@ const respond = async (request) => {
|
|
|
260
322
|
]));
|
|
261
323
|
if (rendered.notFound() || isNotFound(threw)) {
|
|
262
324
|
rendered.abort();
|
|
263
|
-
const nearest = nearestNotFound();
|
|
325
|
+
const nearest = await nearestNotFound();
|
|
264
326
|
status = 404;
|
|
265
327
|
showsPage = false;
|
|
266
328
|
({enter} = nearest);
|
|
@@ -321,10 +383,7 @@ const respond = async (request) => {
|
|
|
321
383
|
* value the shell answers.
|
|
322
384
|
*/
|
|
323
385
|
const matchNotFound = (pathname, forShell) => {
|
|
324
|
-
let parsed =
|
|
325
|
-
params: {},
|
|
326
|
-
enter: (fn) => fn()
|
|
327
|
-
};
|
|
386
|
+
let parsed = NO_PARAMS;
|
|
328
387
|
const page = normalizePathname(pathname);
|
|
329
388
|
return {
|
|
330
389
|
match: routes.match(`${page === "/" ? "" : page}/${NOT_FOUND_SEGMENT}`, (found) => {
|
package/dist/runtime/guard.d.mts
CHANGED
|
@@ -46,4 +46,5 @@ export declare const parseParams: (schemas: readonly ParamsSchema[], raw: Readon
|
|
|
46
46
|
* says.
|
|
47
47
|
*/
|
|
48
48
|
export declare const parseCatchAllParams: (schemas: readonly ParamsSchema[], raw: Readonly<Record<string, string>>) => ParsedParams;
|
|
49
|
-
//#endregion
|
|
49
|
+
//#endregion
|
|
50
|
+
export {};
|
|
@@ -18,4 +18,5 @@ type ResolvedRedirect = {
|
|
|
18
18
|
* the table normalizes it, so `/old/` is `/old` here too.
|
|
19
19
|
*/
|
|
20
20
|
export declare const resolveRedirects: (declared: Readonly<Record<string, RedirectTarget>>) => ((pattern: string, pathname: string) => ResolvedRedirect | null);
|
|
21
|
-
//#endregion
|
|
21
|
+
//#endregion
|
|
22
|
+
export {};
|
package/dist/runtime/route.d.mts
CHANGED
|
@@ -32,4 +32,5 @@ export declare const methodNotAllowed: (module: AnyRouteModule) => Response;
|
|
|
32
32
|
* answers the request as much as a guard does.
|
|
33
33
|
*/
|
|
34
34
|
export declare const runRoute: ({ handler, headOnly }: RouteAnswer, context: RouteContext) => Promise<Response>;
|
|
35
|
-
//#endregion
|
|
35
|
+
//#endregion
|
|
36
|
+
export {};
|