@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
|
@@ -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
|
/**
|
package/dist/serve.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { n as
|
|
2
|
-
import {
|
|
1
|
+
import { f as ENCODINGS, g as streamingCompressor, h as negotiateEncoding, m as isCompressible, n as resolveStatic, p as SUFFIX, t as readRules } from "./static-host-tIDLqV8c.mjs";
|
|
2
|
+
import { n as safeJoin, t as isFile } from "./static-file-BS-WEiwu.mjs";
|
|
3
3
|
import { withoutBase } from "@k8ordo/router";
|
|
4
4
|
import { pathToFileURL } from "node:url";
|
|
5
5
|
import { stat } from "node:fs/promises";
|
|
@@ -11,7 +11,7 @@ import { createServer } from "node:http";
|
|
|
11
11
|
import { Readable } from "node:stream";
|
|
12
12
|
import { pipeline } from "node:stream/promises";
|
|
13
13
|
import fresh from "fresh";
|
|
14
|
-
import
|
|
14
|
+
import { combineRanges, parse } from "range-parser";
|
|
15
15
|
// src/serve.ts
|
|
16
16
|
/**
|
|
17
17
|
* Vite writes the hash of the contents into the name of everything under
|
|
@@ -81,6 +81,8 @@ const digest = async (file) => {
|
|
|
81
81
|
for await (const chunk of createReadStream(file)) hash.update(chunk);
|
|
82
82
|
return hash.digest("base64url");
|
|
83
83
|
};
|
|
84
|
+
const UNREADABLE = 1;
|
|
85
|
+
const PAST_THE_END = 2;
|
|
84
86
|
/**
|
|
85
87
|
* The part of a file a GET asked for: the whole, one byte range, or a range
|
|
86
88
|
* that starts past the end. Only the single range a media element asks for
|
|
@@ -92,10 +94,11 @@ const rangeOf = (headers, size, etag) => {
|
|
|
92
94
|
const { range, "if-range": ifRange } = headers;
|
|
93
95
|
if (range === void 0) return "whole";
|
|
94
96
|
if (ifRange !== void 0 && ifRange !== etag) return "whole";
|
|
95
|
-
const ranges =
|
|
96
|
-
if (
|
|
97
|
-
|
|
98
|
-
|
|
97
|
+
const { code, type, ranges } = parse(size, range);
|
|
98
|
+
if (type !== "bytes" || (code & UNREADABLE) !== 0) return "whole";
|
|
99
|
+
const [only, ...more] = combineRanges(ranges);
|
|
100
|
+
if (only === void 0) return code === PAST_THE_END ? "unsatisfiable" : "whole";
|
|
101
|
+
return more.length === 0 ? only : "whole";
|
|
99
102
|
};
|
|
100
103
|
/**
|
|
101
104
|
* The handler's answer, written out — compressed on the way when its type is
|
|
@@ -225,6 +228,13 @@ const serve = async (options = {}) => {
|
|
|
225
228
|
}
|
|
226
229
|
await sendFile(incoming, response, answer.file, answer.status);
|
|
227
230
|
};
|
|
231
|
+
const pending = /* @__PURE__ */ new Set();
|
|
232
|
+
const keeping = { waitUntil: (promise) => {
|
|
233
|
+
const settled = promise.finally(() => {
|
|
234
|
+
pending.delete(settled);
|
|
235
|
+
});
|
|
236
|
+
pending.add(settled);
|
|
237
|
+
} };
|
|
228
238
|
const server = createServer((incoming, response) => {
|
|
229
239
|
for (const [name, value] of everywhere) response.setHeader(name, value);
|
|
230
240
|
(async () => {
|
|
@@ -245,7 +255,7 @@ const serve = async (options = {}) => {
|
|
|
245
255
|
await sendFile(incoming, response, file);
|
|
246
256
|
return;
|
|
247
257
|
}
|
|
248
|
-
await sendAnswer(incoming, response, await handler(asRequest(incoming, url)));
|
|
258
|
+
await sendAnswer(incoming, response, await handler(asRequest(incoming, url), keeping));
|
|
249
259
|
})().catch((error) => {
|
|
250
260
|
console.error("k8ordo: %s %s failed", incoming.method ?? "GET", incoming.url ?? "/", error);
|
|
251
261
|
response.writeHead(500, { "content-type": "text/plain;charset=utf-8" });
|
|
@@ -262,12 +272,15 @@ const serve = async (options = {}) => {
|
|
|
262
272
|
return {
|
|
263
273
|
port,
|
|
264
274
|
url,
|
|
265
|
-
close:
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
275
|
+
close: async () => {
|
|
276
|
+
await new Promise((resolve, reject) => {
|
|
277
|
+
server.close((error) => {
|
|
278
|
+
if (error === void 0) resolve();
|
|
279
|
+
else reject(error);
|
|
280
|
+
});
|
|
269
281
|
});
|
|
270
|
-
|
|
282
|
+
while (pending.size > 0) await Promise.all(pending);
|
|
283
|
+
}
|
|
271
284
|
};
|
|
272
285
|
};
|
|
273
286
|
export { serve };
|
|
@@ -2,8 +2,6 @@ import { Plugin, PluginOption } from "vite";
|
|
|
2
2
|
import "react";
|
|
3
3
|
/** What `framework()` takes whatever the mode. */
|
|
4
4
|
type EngineOptions = {
|
|
5
|
-
/** Where the route files live, relative to the project root. */
|
|
6
|
-
readonly routesDir?: string;
|
|
7
5
|
/**
|
|
8
6
|
* Compiles the client components with React Compiler — the Rust one,
|
|
9
7
|
* through `@vitejs/plugin-react`'s `compiler`, which needs
|
|
@@ -26,6 +24,13 @@ type EngineOptions = {
|
|
|
26
24
|
* `<meta>`, with the hashes of that page's signed inline scripts added.
|
|
27
25
|
*/
|
|
28
26
|
type ContentSecurityPolicy = Readonly<Record<string, readonly string[]>>;
|
|
27
|
+
/**
|
|
28
|
+
* An inline script of the application's as a CSP source, `'sha256-…'`: the
|
|
29
|
+
* hash of `text`, the string its `<script>` is given, for static mode's
|
|
30
|
+
* `csp` option, which adds the hashes of the framework's own scripts alone.
|
|
31
|
+
* A line break counts as the browser parses the page, a CR LF as one LF.
|
|
32
|
+
*/
|
|
33
|
+
declare const scriptHash: (text: string) => Promise<string>;
|
|
29
34
|
type StaticOptions = EngineOptions & {
|
|
30
35
|
/**
|
|
31
36
|
* Every route rendered at build time and written to files; no server at
|
|
@@ -34,22 +39,24 @@ type StaticOptions = EngineOptions & {
|
|
|
34
39
|
*/
|
|
35
40
|
readonly mode: 'static';
|
|
36
41
|
/**
|
|
37
|
-
* The origin the site is served from — `https://example.com
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
+
* The origin the site is served from — `https://example.com`, with no
|
|
43
|
+
* path. The build asks for every page and `route.ts` under it, so it is
|
|
44
|
+
* the origin of the `request.url` a `route.ts` reads, and it writes
|
|
45
|
+
* `sitemap.xml`, listing every page it rendered at its URL, Vite's `base`
|
|
46
|
+
* included. A `route.ts` that writes `sitemap.xml` is the application's
|
|
47
|
+
* own list, and the build leaves the file to it.
|
|
42
48
|
*/
|
|
43
|
-
readonly site
|
|
49
|
+
readonly site: string;
|
|
44
50
|
/**
|
|
45
51
|
* The `Content-Security-Policy` every page carries, as directives and
|
|
46
52
|
* their sources. The framework decides no policy: it writes this one into
|
|
47
53
|
* each page's `<meta>`, adding to `script-src` the hashes of its own inline
|
|
48
54
|
* scripts on that page — the payload and React's. An inline script of the
|
|
49
|
-
* application's is allowed by its hash here
|
|
50
|
-
*
|
|
51
|
-
*
|
|
55
|
+
* application's is allowed by its hash here: `scriptHash()` from
|
|
56
|
+
* `@k8ordo/framework/vite` gives the hash of one the application writes,
|
|
57
|
+
* as `colorSchemeScriptHash()` gives `@k8ordo/color-scheme`'s. Without it,
|
|
58
|
+
* no policy is written.
|
|
52
59
|
*/
|
|
53
60
|
readonly csp?: ContentSecurityPolicy;
|
|
54
61
|
};
|
|
55
|
-
export { ContentSecurityPolicy as n,
|
|
62
|
+
export { EngineOptions as i, ContentSecurityPolicy as n, scriptHash as r, StaticOptions as t };
|