@k8ordo/framework 2.3.1 → 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 +28 -13
- package/dist/cli.mjs +3 -3
- package/dist/generated.d.mts +13 -13
- package/dist/headers-BxB_jJVk.mjs +73 -0
- package/dist/index.d.mts +3 -2
- package/dist/index.mjs +1 -1
- package/dist/paths-type-DXpmjaOr.d.mts +43 -0
- package/dist/runtime/app-router.mjs +21 -8
- package/dist/runtime/browser-payloads.mjs +1 -1
- package/dist/runtime/client-files.d.mts +6 -5
- package/dist/runtime/client-files.mjs +17 -9
- package/dist/runtime/client-ready.d.mts +7 -1
- package/dist/runtime/client-ready.mjs +2 -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 +38 -2
- package/dist/runtime/entry.rsc.mjs +97 -31
- package/dist/runtime/entry.ssr.d.mts +7 -1
- package/dist/runtime/entry.ssr.mjs +12 -6
- 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/payload.d.mts +19 -4
- package/dist/runtime/payload.mjs +20 -5
- package/dist/runtime/prefetch.d.mts +29 -8
- package/dist/runtime/prefetch.mjs +95 -24
- 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 +29 -14
- package/dist/static-Be3xIqd8.d.mts +62 -0
- package/dist/{static-file-BAjwBMMl.mjs → static-file-BS-WEiwu.mjs} +311 -134
- package/dist/{static-host-B-1fAfAw.mjs → static-host-tIDLqV8c.mjs} +1 -1
- package/dist/vercel.d.mts +18 -1
- package/dist/vercel.mjs +131 -19
- package/dist/vite.d.mts +6 -3
- package/dist/vite.mjs +366 -156
- package/docs/GUIDE.md +173 -46
- package/docs/llms.txt +129 -65
- 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 +154 -25
- package/docs/references/errors.md +57 -12
- package/docs/references/guards.md +100 -29
- package/docs/references/params.md +234 -126
- package/docs/references/routing.md +358 -75
- package/package.json +18 -13
- package/dist/shared-dir-CNXFRZgP.mjs +0 -9
- package/dist/static-DnbgGIoy.d.mts +0 -61
|
@@ -64,55 +64,100 @@ const prefetchOnIntent = (prefetch) => {
|
|
|
64
64
|
};
|
|
65
65
|
};
|
|
66
66
|
/**
|
|
67
|
+
* How long a link stays on screen before it is fetched ahead, in
|
|
68
|
+
* milliseconds. A link scrolled past is not one the visitor stopped at, and a
|
|
69
|
+
* link that just came into view may not be the one they tap.
|
|
70
|
+
*/
|
|
71
|
+
const IN_VIEW_DWELL = 1e3;
|
|
72
|
+
/**
|
|
67
73
|
* Prefetches the links on screen (`prefetchInView`) while the primary pointer
|
|
68
|
-
* is coarse
|
|
74
|
+
* is coarse.
|
|
69
75
|
*/
|
|
70
76
|
const prefetchInViewUnderTouch = (prefetch) => {
|
|
71
77
|
const coarse = matchMedia("(pointer: coarse)");
|
|
72
|
-
let
|
|
78
|
+
let inView;
|
|
79
|
+
let pending = null;
|
|
73
80
|
const follow = () => {
|
|
74
|
-
stop
|
|
75
|
-
|
|
81
|
+
inView?.stop();
|
|
82
|
+
inView = coarse.matches ? prefetchInView(prefetch) : void 0;
|
|
83
|
+
inView?.pending(pending);
|
|
76
84
|
};
|
|
77
85
|
follow();
|
|
78
86
|
coarse.addEventListener("change", follow);
|
|
79
|
-
return
|
|
80
|
-
|
|
81
|
-
|
|
87
|
+
return {
|
|
88
|
+
stop: () => {
|
|
89
|
+
coarse.removeEventListener("change", follow);
|
|
90
|
+
inView?.stop();
|
|
91
|
+
},
|
|
92
|
+
pending: (pathname) => {
|
|
93
|
+
pending = pathname;
|
|
94
|
+
inView?.pending(pathname);
|
|
95
|
+
}
|
|
82
96
|
};
|
|
83
97
|
};
|
|
98
|
+
/** Resolves once the document has loaded, at once if it has. */
|
|
99
|
+
const documentLoaded = () => new Promise((resolve) => {
|
|
100
|
+
if (document.readyState === "complete") resolve();
|
|
101
|
+
else addEventListener("load", () => resolve(), { once: true });
|
|
102
|
+
});
|
|
84
103
|
/**
|
|
85
104
|
* Prefetches the links on screen, one at a time, in the order they came into
|
|
86
|
-
* view,
|
|
87
|
-
*
|
|
88
|
-
*
|
|
105
|
+
* view, once each has stayed there `IN_VIEW_DWELL` and the document has
|
|
106
|
+
* loaded. A link that leaves before its turn is passed over, so a fast
|
|
107
|
+
* scroll fetches little; one at a time, so a page visited while they load
|
|
108
|
+
* shares the connection with one prefetch at most, and with none once the
|
|
109
|
+
* page change is known (`pending`).
|
|
89
110
|
*/
|
|
90
111
|
const prefetchInView = (prefetch) => {
|
|
112
|
+
const onScreen = /* @__PURE__ */ new Map();
|
|
91
113
|
const waiting = /* @__PURE__ */ new Set();
|
|
114
|
+
const loaded = documentLoaded();
|
|
115
|
+
let fetching;
|
|
116
|
+
let pending = null;
|
|
92
117
|
let draining = false;
|
|
93
118
|
let stopped = false;
|
|
94
119
|
const drain = async () => {
|
|
95
120
|
if (draining) return;
|
|
96
121
|
draining = true;
|
|
122
|
+
await loaded;
|
|
97
123
|
for (const link of waiting) {
|
|
124
|
+
if (stopped || pending !== null) break;
|
|
98
125
|
waiting.delete(link);
|
|
99
126
|
const url = prefetchTargetOf(link);
|
|
100
|
-
if (url
|
|
101
|
-
|
|
127
|
+
if (url === null) continue;
|
|
128
|
+
const controller = new AbortController();
|
|
129
|
+
fetching = {
|
|
130
|
+
link,
|
|
131
|
+
url,
|
|
132
|
+
controller
|
|
133
|
+
};
|
|
134
|
+
await prefetch(url, controller.signal).catch(() => void 0);
|
|
135
|
+
fetching = void 0;
|
|
102
136
|
}
|
|
103
137
|
draining = false;
|
|
104
138
|
};
|
|
139
|
+
const leave = (link) => {
|
|
140
|
+
clearTimeout(onScreen.get(link));
|
|
141
|
+
onScreen.delete(link);
|
|
142
|
+
waiting.delete(link);
|
|
143
|
+
};
|
|
105
144
|
const view = new IntersectionObserver((entries) => {
|
|
106
|
-
for (const { isIntersecting, target } of entries) if (isIntersecting)
|
|
107
|
-
else
|
|
108
|
-
|
|
145
|
+
for (const { isIntersecting, target } of entries) if (!isIntersecting) leave(target);
|
|
146
|
+
else if (!onScreen.has(target)) {
|
|
147
|
+
const stayed = setTimeout(() => {
|
|
148
|
+
onScreen.set(target, void 0);
|
|
149
|
+
waiting.add(target);
|
|
150
|
+
drain();
|
|
151
|
+
}, IN_VIEW_DWELL);
|
|
152
|
+
onScreen.set(target, stayed);
|
|
153
|
+
}
|
|
109
154
|
});
|
|
110
155
|
const linksIn = (node) => node instanceof Element ? [...node.matches("a[href]") ? [node] : [], ...node.querySelectorAll("a[href]")] : [];
|
|
111
156
|
const tree = new MutationObserver((records) => {
|
|
112
157
|
for (const { addedNodes, removedNodes } of records) {
|
|
113
158
|
for (const node of removedNodes) for (const link of linksIn(node)) {
|
|
114
159
|
view.unobserve(link);
|
|
115
|
-
|
|
160
|
+
leave(link);
|
|
116
161
|
}
|
|
117
162
|
for (const node of addedNodes) for (const link of linksIn(node)) view.observe(link);
|
|
118
163
|
}
|
|
@@ -122,11 +167,29 @@ const prefetchInView = (prefetch) => {
|
|
|
122
167
|
subtree: true
|
|
123
168
|
});
|
|
124
169
|
for (const link of document.querySelectorAll("a[href]")) view.observe(link);
|
|
125
|
-
return
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
170
|
+
return {
|
|
171
|
+
stop: () => {
|
|
172
|
+
stopped = true;
|
|
173
|
+
tree.disconnect();
|
|
174
|
+
view.disconnect();
|
|
175
|
+
for (const link of onScreen.keys()) leave(link);
|
|
176
|
+
},
|
|
177
|
+
pending: (pathname) => {
|
|
178
|
+
pending = pathname;
|
|
179
|
+
if (pathname === null) {
|
|
180
|
+
drain();
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
if (fetching === void 0) return;
|
|
184
|
+
const { pathname: fetched } = fetching.url;
|
|
185
|
+
if (normalizePathname(withoutBase(fetched) ?? fetched) === pathname) return;
|
|
186
|
+
if (onScreen.has(fetching.link)) {
|
|
187
|
+
const behind = [...waiting];
|
|
188
|
+
waiting.clear();
|
|
189
|
+
for (const link of [fetching.link, ...behind]) waiting.add(link);
|
|
190
|
+
}
|
|
191
|
+
fetching.controller.abort();
|
|
192
|
+
}
|
|
130
193
|
};
|
|
131
194
|
};
|
|
132
195
|
const createPageCache = (load, { files }, now = () => performance.now()) => {
|
|
@@ -171,9 +234,17 @@ const createPageCache = (load, { files }, now = () => performance.now()) => {
|
|
|
171
234
|
for (const [key, entry] of entries) if (!usable(entry)) forget(key, entry);
|
|
172
235
|
};
|
|
173
236
|
return {
|
|
174
|
-
prefetch: (key, priority) => {
|
|
237
|
+
prefetch: (key, priority, signal) => {
|
|
175
238
|
sweep();
|
|
176
|
-
|
|
239
|
+
let entry = held(key);
|
|
240
|
+
if (entry === void 0) {
|
|
241
|
+
const started = start(key, priority);
|
|
242
|
+
signal?.addEventListener("abort", () => {
|
|
243
|
+
if (started.settled || started.waiting > 0) return;
|
|
244
|
+
if (entries.get(key) === started) forget(key, started);
|
|
245
|
+
}, { once: true });
|
|
246
|
+
entry = started;
|
|
247
|
+
}
|
|
177
248
|
hold(key, entry);
|
|
178
249
|
return entry.value;
|
|
179
250
|
},
|
|
@@ -212,4 +283,4 @@ const createPageCache = (load, { files }, now = () => performance.now()) => {
|
|
|
212
283
|
}
|
|
213
284
|
};
|
|
214
285
|
};
|
|
215
|
-
export { KEPT_PAGES, PREFETCH_LIFETIME, createPageCache, prefetchInView, prefetchInViewUnderTouch, prefetchOnIntent, prefetchTargetOf };
|
|
286
|
+
export { IN_VIEW_DWELL, KEPT_PAGES, PREFETCH_LIFETIME, createPageCache, prefetchInView, prefetchInViewUnderTouch, prefetchOnIntent, prefetchTargetOf };
|
|
@@ -1,16 +1,21 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/** What `redirect()` throws, for whatever answers the request to catch. */
|
|
2
2
|
export declare class Redirect extends Error {
|
|
3
3
|
readonly to: string;
|
|
4
4
|
constructor(to: string);
|
|
5
5
|
}
|
|
6
6
|
/**
|
|
7
|
-
* Ends
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
7
|
+
* Ends what answers the request — a `guard.ts`, a `route.ts` or a Server
|
|
8
|
+
* Action — by sending the visitor to `to`, a URL: `href()`'s, with Vite's
|
|
9
|
+
* `base` in it. Thrown, so the lines after it never run, and the cookies
|
|
10
|
+
* and headers written before it go out with the redirect. A guard, a
|
|
11
|
+
* route.ts and a form posted without JavaScript are answered with a `303`
|
|
12
|
+
* to `to`; an action the client runtime posted, with a payload that tells
|
|
13
|
+
* the browser to navigate there. A page is a render, not an answer, so
|
|
14
|
+
* calling it while one renders throws by name.
|
|
12
15
|
*/
|
|
13
16
|
export declare const redirect: (to: string) => never;
|
|
17
|
+
/** A redirect's answer: `to` in `location`, under `status`. */
|
|
18
|
+
export declare const redirectResponse: (to: string, status: number) => Response;
|
|
14
19
|
/** What a `redirect.ts` route file default-exports. */
|
|
15
20
|
export type RedirectTarget = string | {
|
|
16
21
|
readonly to: string;
|
|
@@ -23,7 +28,8 @@ type ResolvedRedirect = {
|
|
|
23
28
|
/**
|
|
24
29
|
* Where a `redirect.ts` sends a pathname the table matched to its pattern,
|
|
25
30
|
* with the matched params filled into the target, so `/:locale/legacy` can
|
|
26
|
-
* send to `/:locale/new
|
|
31
|
+
* send to `/:locale/new`, and `/old/:path+` to `/new/:path+`, every segment
|
|
32
|
+
* the rest param took moved as one; `null` for a pattern no redirect declares. Which
|
|
27
33
|
* pattern answers is the table's to say — the redirect holds its place there
|
|
28
34
|
* — so this only reads the params again: the router hands them decoded, and a
|
|
29
35
|
* target moves a segment as the URL spelled it. The pathname is normalized as
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { currentPhase } from "./request-scope.mjs";
|
|
1
2
|
import { normalizePathname } from "@k8ordo/router";
|
|
2
3
|
// src/runtime/redirect.ts
|
|
3
|
-
/**
|
|
4
|
+
/** What `redirect()` throws, for whatever answers the request to catch. */
|
|
4
5
|
var Redirect = class extends Error {
|
|
5
6
|
to;
|
|
6
7
|
constructor(to) {
|
|
@@ -10,15 +11,24 @@ var Redirect = class extends Error {
|
|
|
10
11
|
}
|
|
11
12
|
};
|
|
12
13
|
/**
|
|
13
|
-
* Ends
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
14
|
+
* Ends what answers the request — a `guard.ts`, a `route.ts` or a Server
|
|
15
|
+
* Action — by sending the visitor to `to`, a URL: `href()`'s, with Vite's
|
|
16
|
+
* `base` in it. Thrown, so the lines after it never run, and the cookies
|
|
17
|
+
* and headers written before it go out with the redirect. A guard, a
|
|
18
|
+
* route.ts and a form posted without JavaScript are answered with a `303`
|
|
19
|
+
* to `to`; an action the client runtime posted, with a payload that tells
|
|
20
|
+
* the browser to navigate there. A page is a render, not an answer, so
|
|
21
|
+
* calling it while one renders throws by name.
|
|
18
22
|
*/
|
|
19
23
|
const redirect = (to) => {
|
|
24
|
+
if (currentPhase() === "render") throw new Error(`redirect() ends what answers the request, and a page is a render — redirect from a guard.ts, a route.ts or a Server Action, or answer the URL with a redirect.ts (it was asked for ${to})`);
|
|
20
25
|
throw new Redirect(to);
|
|
21
26
|
};
|
|
27
|
+
/** A redirect's answer: `to` in `location`, under `status`. */
|
|
28
|
+
const redirectResponse = (to, status) => new Response(null, {
|
|
29
|
+
status,
|
|
30
|
+
headers: { location: to }
|
|
31
|
+
});
|
|
22
32
|
const resolveTarget = (target, segments) => {
|
|
23
33
|
const { to, permanent } = typeof target === "string" ? {
|
|
24
34
|
to: target,
|
|
@@ -37,7 +47,8 @@ const resolveTarget = (target, segments) => {
|
|
|
37
47
|
/**
|
|
38
48
|
* Where a `redirect.ts` sends a pathname the table matched to its pattern,
|
|
39
49
|
* with the matched params filled into the target, so `/:locale/legacy` can
|
|
40
|
-
* send to `/:locale/new
|
|
50
|
+
* send to `/:locale/new`, and `/old/:path+` to `/new/:path+`, every segment
|
|
51
|
+
* the rest param took moved as one; `null` for a pattern no redirect declares. Which
|
|
41
52
|
* pattern answers is the table's to say — the redirect holds its place there
|
|
42
53
|
* — so this only reads the params again: the router hands them decoded, and a
|
|
43
54
|
* target moves a segment as the URL spelled it. The pathname is normalized as
|
|
@@ -46,16 +57,21 @@ const resolveTarget = (target, segments) => {
|
|
|
46
57
|
const resolveRedirects = (declared) => {
|
|
47
58
|
const matchers = new Map(Object.entries(declared).map(([pattern, target]) => [pattern, {
|
|
48
59
|
matcher: new URLPattern({ pathname: pattern }),
|
|
60
|
+
rest: new Set(pattern.split("/").filter((segment) => segment.startsWith(":") && segment.endsWith("+")).map((segment) => segment.slice(1, -1))),
|
|
49
61
|
target
|
|
50
62
|
}]));
|
|
51
63
|
return (pattern, pathname) => {
|
|
52
64
|
const declaredHere = matchers.get(pattern);
|
|
53
65
|
if (declaredHere === void 0) return null;
|
|
66
|
+
const { rest } = declaredHere;
|
|
54
67
|
const result = declaredHere.matcher.exec({ pathname: normalizePathname(pathname) });
|
|
55
68
|
if (result === null) return null;
|
|
56
69
|
const segments = {};
|
|
57
|
-
for (const [name, value] of Object.entries(result.pathname.groups))
|
|
70
|
+
for (const [name, value] of Object.entries(result.pathname.groups)) {
|
|
71
|
+
if (/^\d+$/u.test(name) || value === void 0) continue;
|
|
72
|
+
segments[rest.has(name) ? `${name}+` : name] = value;
|
|
73
|
+
}
|
|
58
74
|
return resolveTarget(declaredHere.target, segments);
|
|
59
75
|
};
|
|
60
76
|
};
|
|
61
|
-
export { Redirect, redirect, resolveRedirects };
|
|
77
|
+
export { Redirect, redirect, redirectResponse, resolveRedirects };
|
|
@@ -6,8 +6,27 @@ import { Cookies } from "./cookies.mjs";
|
|
|
6
6
|
* response would be a second handler.
|
|
7
7
|
*/
|
|
8
8
|
export type Phase = 'guard' | 'route' | 'action' | 'render';
|
|
9
|
-
/**
|
|
10
|
-
|
|
9
|
+
/**
|
|
10
|
+
* What whoever calls the handler hands it beside the request: the host's own
|
|
11
|
+
* `waitUntil`, where it has one. A Worker's `ctx` fits as it is, and so does
|
|
12
|
+
* `{ waitUntil }` around a function a host exports.
|
|
13
|
+
*/
|
|
14
|
+
export type HandlerContext = {
|
|
15
|
+
/** Keeps the host running until `promise` settles. */
|
|
16
|
+
readonly waitUntil?: (promise: Promise<unknown>) => void;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Runs `fn` as the handling of `request`, starting in the render, with what
|
|
20
|
+
* the handler's caller handed it as `host`.
|
|
21
|
+
*/
|
|
22
|
+
export declare const withRequest: <T>(request: Request, fn: () => T, host?: HandlerContext) => T;
|
|
23
|
+
/**
|
|
24
|
+
* The cookies of the request in progress as the render reads them: what it
|
|
25
|
+
* carried, with what a guard or a Server Action set or deleted before.
|
|
26
|
+
*/
|
|
27
|
+
export declare const currentCookies: () => ReadonlyMap<string, string>;
|
|
28
|
+
/** The phase of the request in progress; `undefined` outside one. */
|
|
29
|
+
export declare const currentPhase: () => Phase | undefined;
|
|
11
30
|
/** Runs `fn` as `phase` of the request in progress. */
|
|
12
31
|
export declare const inPhase: <T>(phase: Phase, fn: () => T) => T;
|
|
13
32
|
/**
|
|
@@ -55,6 +74,22 @@ export declare const requestHeaders: () => Headers;
|
|
|
55
74
|
* carries it into hashes.
|
|
56
75
|
*/
|
|
57
76
|
export declare const nonce: () => string;
|
|
77
|
+
/**
|
|
78
|
+
* `body`, read as part of the request in progress. A stream's `pull` runs in
|
|
79
|
+
* the async context of whoever reads it — the host writing the answer out,
|
|
80
|
+
* which is in no request — so what a route.ts calls from inside the stream
|
|
81
|
+
* it answered with would find none.
|
|
82
|
+
*/
|
|
83
|
+
export declare const readInRequest: (body: ReadableStream<Uint8Array>) => ReadableStream<Uint8Array>;
|
|
84
|
+
/**
|
|
85
|
+
* Keeps the host from stopping this request's work before `promise` settles:
|
|
86
|
+
* what goes on after the answer has left — a generation read to its end and
|
|
87
|
+
* saved once the visitor is gone. As a Service Worker's `waitUntil`: the
|
|
88
|
+
* work has already started, and nothing here runs it again. Callable
|
|
89
|
+
* wherever the request is in progress, the render included, since it writes
|
|
90
|
+
* nothing to the response. A rejection is logged, once, and goes no further.
|
|
91
|
+
*/
|
|
92
|
+
export declare const waitUntil: (promise: Promise<unknown>) => void;
|
|
58
93
|
/**
|
|
59
94
|
* The nonce the handler signs its own inline scripts with, in either mode: a
|
|
60
95
|
* build into files turns what it signed into hashes.
|
|
@@ -4,13 +4,18 @@ import { AsyncLocalStorage } from "node:async_hooks";
|
|
|
4
4
|
// src/runtime/request-scope.ts
|
|
5
5
|
const storage = new AsyncLocalStorage();
|
|
6
6
|
const freshNonce = () => btoa(String.fromCodePoint(...crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(16))));
|
|
7
|
-
/**
|
|
8
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Runs `fn` as the handling of `request`, starting in the render, with what
|
|
9
|
+
* the handler's caller handed it as `host`.
|
|
10
|
+
*/
|
|
11
|
+
const withRequest = (request, fn, host = {}) => {
|
|
9
12
|
const jar = createCookies(parseCookies(request.headers.get("cookie")), new URL(request.url));
|
|
10
13
|
return storage.run({
|
|
11
14
|
request,
|
|
15
|
+
host,
|
|
12
16
|
phase: "render",
|
|
13
17
|
cookies: jar.cookies,
|
|
18
|
+
currentCookies: jar.current,
|
|
14
19
|
outgoing: {
|
|
15
20
|
headers: new Headers(),
|
|
16
21
|
cookies: jar.lines
|
|
@@ -18,6 +23,17 @@ const withRequest = (request, fn) => {
|
|
|
18
23
|
nonce: freshNonce()
|
|
19
24
|
}, fn);
|
|
20
25
|
};
|
|
26
|
+
/**
|
|
27
|
+
* The cookies of the request in progress as the render reads them: what it
|
|
28
|
+
* carried, with what a guard or a Server Action set or deleted before.
|
|
29
|
+
*/
|
|
30
|
+
const currentCookies = () => {
|
|
31
|
+
const scope = storage.getStore();
|
|
32
|
+
if (scope === void 0) throw new Error("currentCookies() needs a request — the handler reads it while one is answered, before the render");
|
|
33
|
+
return scope.currentCookies();
|
|
34
|
+
};
|
|
35
|
+
/** The phase of the request in progress; `undefined` outside one. */
|
|
36
|
+
const currentPhase = () => storage.getStore()?.phase;
|
|
21
37
|
/** Runs `fn` as `phase` of the request in progress. */
|
|
22
38
|
const inPhase = (phase, fn) => {
|
|
23
39
|
const current = storage.getStore();
|
|
@@ -110,6 +126,47 @@ const nonce = () => {
|
|
|
110
126
|
return scope.nonce;
|
|
111
127
|
};
|
|
112
128
|
/**
|
|
129
|
+
* `body`, read as part of the request in progress. A stream's `pull` runs in
|
|
130
|
+
* the async context of whoever reads it — the host writing the answer out,
|
|
131
|
+
* which is in no request — so what a route.ts calls from inside the stream
|
|
132
|
+
* it answered with would find none.
|
|
133
|
+
*/
|
|
134
|
+
const readInRequest = (body) => {
|
|
135
|
+
const scope = storage.getStore();
|
|
136
|
+
if (scope === void 0) return body;
|
|
137
|
+
const reader = body.getReader();
|
|
138
|
+
return new ReadableStream({
|
|
139
|
+
pull: (controller) => storage.run(scope, async () => {
|
|
140
|
+
const { done, value } = await reader.read();
|
|
141
|
+
if (done) controller.close();
|
|
142
|
+
else controller.enqueue(value);
|
|
143
|
+
}),
|
|
144
|
+
cancel: (reason) => storage.run(scope, () => reader.cancel(reason))
|
|
145
|
+
}, { highWaterMark: 0 });
|
|
146
|
+
};
|
|
147
|
+
/**
|
|
148
|
+
* Keeps the host from stopping this request's work before `promise` settles:
|
|
149
|
+
* what goes on after the answer has left — a generation read to its end and
|
|
150
|
+
* saved once the visitor is gone. As a Service Worker's `waitUntil`: the
|
|
151
|
+
* work has already started, and nothing here runs it again. Callable
|
|
152
|
+
* wherever the request is in progress, the render included, since it writes
|
|
153
|
+
* nothing to the response. A rejection is logged, once, and goes no further.
|
|
154
|
+
*/
|
|
155
|
+
const waitUntil = (promise) => {
|
|
156
|
+
const scope = storage.getStore();
|
|
157
|
+
if (scope === void 0 || scope.file === true) {
|
|
158
|
+
promise.catch((error) => {
|
|
159
|
+
console.error("k8ordo: what a refused waitUntil() was handed failed", error);
|
|
160
|
+
});
|
|
161
|
+
throw scope === void 0 ? /* @__PURE__ */ new Error("waitUntil() needs a request — call it while one is answered: from a guard.ts, a route.ts, a Server Action, or a layout or page") : fileRefusal("waitUntil()");
|
|
162
|
+
}
|
|
163
|
+
const { request, host } = scope;
|
|
164
|
+
const reported = promise.then(() => void 0, (error) => {
|
|
165
|
+
console.error("k8ordo: waitUntil() of %s %s failed", request.method, new URL(request.url).pathname, error);
|
|
166
|
+
});
|
|
167
|
+
host.waitUntil?.(reported);
|
|
168
|
+
};
|
|
169
|
+
/**
|
|
113
170
|
* The nonce the handler signs its own inline scripts with, in either mode: a
|
|
114
171
|
* build into files turns what it signed into hashes.
|
|
115
172
|
*/
|
|
@@ -118,4 +175,4 @@ const signingNonce = () => {
|
|
|
118
175
|
if (scope === void 0) throw new Error("the handler signs its scripts while a request is answered");
|
|
119
176
|
return scope.nonce;
|
|
120
177
|
};
|
|
121
|
-
export { answer, cookies, inPhase, nonce, requestHeaders, responseHeaders, signingNonce, withRequest, writingFile };
|
|
178
|
+
export { answer, cookies, currentCookies, currentPhase, inPhase, nonce, readInRequest, requestHeaders, responseHeaders, signingNonce, waitUntil, withRequest, writingFile };
|
|
@@ -9,7 +9,12 @@ export type RouteRequest = {
|
|
|
9
9
|
readonly cookies: ReadonlyMap<string, string>;
|
|
10
10
|
};
|
|
11
11
|
export declare const parseCookies: (header: string | null) => ReadonlyMap<string, string>;
|
|
12
|
-
|
|
12
|
+
/**
|
|
13
|
+
* The request a page receives: the headers as they arrived, and the cookies
|
|
14
|
+
* as the guards and the Server Action left them, so the answer to an action
|
|
15
|
+
* renders what the action wrote.
|
|
16
|
+
*/
|
|
17
|
+
export declare const routeRequestOf: (request: Request, cookies: ReadonlyMap<string, string>) => RouteRequest;
|
|
13
18
|
/**
|
|
14
19
|
* The `request` a page is handed under mode: 'static', whose generated types
|
|
15
20
|
* have none. An object rather than `undefined`, so that reading it names the
|
package/dist/runtime/request.mjs
CHANGED
|
@@ -16,9 +16,14 @@ const parseCookies = (header) => {
|
|
|
16
16
|
}
|
|
17
17
|
return cookies;
|
|
18
18
|
};
|
|
19
|
-
|
|
19
|
+
/**
|
|
20
|
+
* The request a page receives: the headers as they arrived, and the cookies
|
|
21
|
+
* as the guards and the Server Action left them, so the answer to an action
|
|
22
|
+
* renders what the action wrote.
|
|
23
|
+
*/
|
|
24
|
+
const routeRequestOf = (request, cookies) => ({
|
|
20
25
|
headers: request.headers,
|
|
21
|
-
cookies
|
|
26
|
+
cookies
|
|
22
27
|
});
|
|
23
28
|
const unreadable = (name) => ({ get: () => {
|
|
24
29
|
throw new Error(`a page read request.${name}, and under mode: 'static' a page is a file written once for every visitor, with no request to read\nthis application wants mode: 'server'`);
|
package/dist/runtime/route.d.mts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { SaidNotFound } from "./ended.mjs";
|
|
1
2
|
/** The request methods a `route.ts` answers by exporting a function named so. */
|
|
2
3
|
export declare const ROUTE_METHODS: readonly ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'];
|
|
3
4
|
export type RouteMethod = (typeof ROUTE_METHODS)[number];
|
|
@@ -28,7 +29,8 @@ export declare const routeAnswerFor: (module: AnyRouteModule, method: string) =>
|
|
|
28
29
|
export declare const methodNotAllowed: (module: AnyRouteModule) => Response;
|
|
29
30
|
/**
|
|
30
31
|
* Runs what answers, in the phase that may write the response: a route
|
|
31
|
-
* answers the request as much as a guard does.
|
|
32
|
+
* answers the request as much as a guard does. A body it answered with is
|
|
33
|
+
* read inside the request too, so a stream's later `pull` still has one.
|
|
32
34
|
*/
|
|
33
|
-
export declare const runRoute: ({ handler, headOnly }: RouteAnswer, context: RouteContext) => Promise<Response>;
|
|
35
|
+
export declare const runRoute: ({ handler, headOnly }: RouteAnswer, context: RouteContext) => Promise<Response | SaidNotFound>;
|
|
34
36
|
export {};
|
package/dist/runtime/route.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { endedBy } from "./ended.mjs";
|
|
2
|
+
import { inPhase, readInRequest } from "./request-scope.mjs";
|
|
2
3
|
// src/runtime/route.ts
|
|
3
4
|
/** The request methods a `route.ts` answers by exporting a function named so. */
|
|
4
5
|
const ROUTE_METHODS = [
|
|
@@ -43,13 +44,20 @@ const methodNotAllowed = (module) => new Response("method not allowed", {
|
|
|
43
44
|
});
|
|
44
45
|
/**
|
|
45
46
|
* Runs what answers, in the phase that may write the response: a route
|
|
46
|
-
* answers the request as much as a guard does.
|
|
47
|
+
* answers the request as much as a guard does. A body it answered with is
|
|
48
|
+
* read inside the request too, so a stream's later `pull` still has one.
|
|
47
49
|
*/
|
|
48
50
|
const runRoute = async ({ handler, headOnly }, context) => {
|
|
49
|
-
|
|
51
|
+
let response;
|
|
52
|
+
try {
|
|
53
|
+
response = await inPhase("route", () => handler(context));
|
|
54
|
+
} catch (error) {
|
|
55
|
+
return endedBy(error);
|
|
56
|
+
}
|
|
50
57
|
if (!(response instanceof Response)) throw new TypeError(`a route.ts ${context.request.method} answers with a Response, and this one returned something else`);
|
|
51
|
-
|
|
52
|
-
|
|
58
|
+
const body = headOnly || response.body === null ? null : readInRequest(response.body);
|
|
59
|
+
if (body === response.body) return response;
|
|
60
|
+
return new Response(body, {
|
|
53
61
|
status: response.status,
|
|
54
62
|
statusText: response.statusText,
|
|
55
63
|
headers: response.headers
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Guard, GuardContext } from "./guard.mjs";
|
|
3
|
-
import { RedirectTarget, redirect } from "./redirect.mjs";
|
|
1
|
+
import { redirect } from "./redirect.mjs";
|
|
4
2
|
import { RouteRequest } from "./request.mjs";
|
|
5
|
-
import {
|
|
6
|
-
|
|
3
|
+
import { Guard, GuardContext } from "./guard.mjs";
|
|
4
|
+
import { HandlerContext, cookies, nonce, requestHeaders, responseHeaders, waitUntil } from "./request-scope.mjs";
|
|
5
|
+
import { CookieOptions, CookieScope, Cookies } from "./cookies.mjs";
|
|
6
|
+
export { type CookieOptions, type CookieScope, type Cookies, type Guard, type GuardContext, type HandlerContext, type RouteRequest, cookies, nonce, redirect, requestHeaders, responseHeaders, waitUntil };
|
package/dist/runtime/server.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { cookies, nonce, requestHeaders, responseHeaders } from "./request-scope.mjs";
|
|
2
1
|
import { redirect } from "./redirect.mjs";
|
|
3
|
-
|
|
2
|
+
import { cookies, nonce, requestHeaders, responseHeaders, waitUntil } from "./request-scope.mjs";
|
|
3
|
+
export { cookies, nonce, redirect, requestHeaders, responseHeaders, waitUntil };
|
package/dist/runtime/shell.d.mts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { Match } from "@k8ordo/router";
|
|
1
2
|
/**
|
|
2
3
|
* The params a shell request fills — `null` when this is not one: no param
|
|
3
4
|
* holds the reserved segment, or one that does is not open.
|
|
4
5
|
*/
|
|
5
|
-
export declare const shellParams: (params:
|
|
6
|
+
export declare const shellParams: (params: Match['params'], open: readonly string[]) => Match['params'] | null;
|
package/dist/serve.d.mts
CHANGED
|
@@ -10,6 +10,10 @@ export type Server = {
|
|
|
10
10
|
readonly port: number;
|
|
11
11
|
/** The address it listens on, which `localhost` may have made `[::1]`. */
|
|
12
12
|
readonly url: string;
|
|
13
|
+
/**
|
|
14
|
+
* Stops listening, and resolves once every answer under way is written
|
|
15
|
+
* and everything handed to `waitUntil()` has settled.
|
|
16
|
+
*/
|
|
13
17
|
readonly close: () => Promise<void>;
|
|
14
18
|
};
|
|
15
19
|
/**
|