ovr 6.2.4 → 7.0.0-next.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 +3 -1
- package/dist/app/index.d.ts +51 -7
- package/dist/app/index.d.ts.map +1 -1
- package/dist/app/index.js +163 -28
- package/dist/auth/auth-data.d.ts +26 -0
- package/dist/auth/auth-data.d.ts.map +1 -0
- package/dist/auth/auth-data.js +42 -0
- package/dist/auth/cbor.d.ts +50 -0
- package/dist/auth/cbor.d.ts.map +1 -0
- package/dist/auth/cbor.js +200 -0
- package/dist/auth/client-source.d.ts +3 -0
- package/dist/auth/client-source.d.ts.map +1 -0
- package/dist/auth/client-source.js +3 -0
- package/dist/auth/client.d.ts +3 -0
- package/dist/auth/client.d.ts.map +1 -0
- package/dist/auth/client.js +186 -0
- package/dist/auth/configure.d.ts +28 -0
- package/dist/auth/configure.d.ts.map +1 -0
- package/dist/auth/configure.js +228 -0
- package/dist/auth/der.d.ts +6 -0
- package/dist/auth/der.d.ts.map +1 -0
- package/dist/auth/der.js +36 -0
- package/dist/auth/index.d.ts +242 -0
- package/dist/auth/index.d.ts.map +1 -0
- package/dist/auth/index.js +152 -0
- package/dist/auth/issue.d.ts +36 -0
- package/dist/auth/issue.d.ts.map +1 -0
- package/dist/auth/issue.js +47 -0
- package/dist/auth/metadata.d.ts +22 -0
- package/dist/auth/metadata.d.ts.map +1 -0
- package/dist/auth/metadata.js +134 -0
- package/dist/auth/oauth.d.ts +29 -0
- package/dist/auth/oauth.d.ts.map +1 -0
- package/dist/auth/oauth.js +724 -0
- package/dist/auth/passkey.d.ts +101 -0
- package/dist/auth/passkey.d.ts.map +1 -0
- package/dist/auth/passkey.js +393 -0
- package/dist/auth/state.d.ts +49 -0
- package/dist/auth/state.d.ts.map +1 -0
- package/dist/auth/state.js +184 -0
- package/dist/auth/storage.d.ts +193 -0
- package/dist/auth/storage.d.ts.map +1 -0
- package/dist/auth/storage.js +410 -0
- package/dist/auth/token.d.ts +12 -0
- package/dist/auth/token.d.ts.map +1 -0
- package/dist/auth/token.js +39 -0
- package/dist/context/index.d.ts +86 -16
- package/dist/context/index.d.ts.map +1 -1
- package/dist/context/index.js +166 -42
- package/dist/cookie/index.d.ts +3 -0
- package/dist/cookie/index.d.ts.map +1 -1
- package/dist/cookie/index.js +1 -0
- package/dist/database/index.d.ts +262 -0
- package/dist/database/index.d.ts.map +1 -0
- package/dist/database/index.js +807 -0
- package/dist/form/index.d.ts +699 -0
- package/dist/form/index.d.ts.map +1 -0
- package/dist/form/index.js +901 -0
- package/dist/index.d.ts +10 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -3
- package/dist/jsx/elements.d.ts +28 -2
- package/dist/jsx/elements.d.ts.map +1 -1
- package/dist/jsx/index.d.ts +20 -4
- package/dist/jsx/index.d.ts.map +1 -1
- package/dist/jsx/index.js +6 -65
- package/dist/limiter/index.d.ts +65 -0
- package/dist/limiter/index.d.ts.map +1 -0
- package/dist/limiter/index.js +157 -0
- package/dist/markdown/index.d.ts +61 -0
- package/dist/markdown/index.d.ts.map +1 -0
- package/dist/markdown/index.js +1160 -0
- package/dist/markup/index.d.ts +188 -0
- package/dist/markup/index.d.ts.map +1 -0
- package/dist/markup/index.js +718 -0
- package/dist/mcp/index.d.ts +73 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +988 -0
- package/dist/middleware/index.d.ts +3 -4
- package/dist/middleware/index.d.ts.map +1 -1
- package/dist/multipart/index.d.ts +53 -7
- package/dist/multipart/index.d.ts.map +1 -1
- package/dist/multipart/index.js +332 -124
- package/dist/route/index.d.ts +226 -80
- package/dist/route/index.d.ts.map +1 -1
- package/dist/route/index.js +239 -99
- package/dist/schema/index.d.ts +307 -734
- package/dist/schema/index.d.ts.map +1 -1
- package/dist/schema/index.js +783 -1050
- package/dist/table/index.d.ts +132 -0
- package/dist/table/index.d.ts.map +1 -0
- package/dist/table/index.js +188 -0
- package/dist/trie/index.js +4 -4
- package/dist/util/index.d.ts +37 -5
- package/dist/util/index.d.ts.map +1 -1
- package/dist/util/index.js +156 -41
- package/package.json +11 -4
- package/src/app/index.ts +230 -42
- package/src/auth/auth-data.ts +56 -0
- package/src/auth/cbor.ts +231 -0
- package/src/auth/client-source.ts +3 -0
- package/src/auth/client.ts +205 -0
- package/src/auth/configure.ts +279 -0
- package/src/auth/der.ts +35 -0
- package/src/auth/index.ts +387 -0
- package/src/auth/issue.ts +52 -0
- package/src/auth/metadata.ts +149 -0
- package/src/auth/oauth.ts +882 -0
- package/src/auth/passkey.ts +556 -0
- package/src/auth/state.ts +213 -0
- package/src/auth/storage.ts +572 -0
- package/src/auth/token.ts +67 -0
- package/src/context/index.ts +240 -65
- package/src/cookie/index.ts +3 -0
- package/src/database/index.ts +1193 -0
- package/src/form/index.ts +1720 -0
- package/src/index.ts +10 -3
- package/src/jsx/elements.ts +45 -25
- package/src/jsx/index.ts +24 -69
- package/src/limiter/index.ts +221 -0
- package/src/markdown/index.ts +1322 -0
- package/src/markup/index.ts +853 -0
- package/src/mcp/conformance.2025-11-25.yml +39 -0
- package/src/mcp/conformance.2026-07-28.yml +56 -0
- package/src/mcp/index.ts +1262 -0
- package/src/middleware/index.ts +5 -6
- package/src/multipart/index.ts +410 -158
- package/src/route/index.ts +559 -278
- package/src/schema/index.ts +1542 -2256
- package/src/table/index.ts +387 -0
- package/src/trie/index.ts +4 -4
- package/src/util/index.ts +163 -49
- package/dist/render/index.d.ts +0 -79
- package/dist/render/index.d.ts.map +0 -1
- package/dist/render/index.js +0 -257
- package/src/render/index.ts +0 -314
package/src/context/index.ts
CHANGED
|
@@ -1,13 +1,31 @@
|
|
|
1
1
|
import type { App } from "../app/index.js";
|
|
2
2
|
import { Cookie } from "../cookie/index.js";
|
|
3
|
+
import { type Field, Form as FormSchema, forms } from "../form/index.js";
|
|
4
|
+
import { Markdown, tools } from "../markdown/index.js";
|
|
5
|
+
import { Markup } from "../markup/index.js";
|
|
3
6
|
import type { Middleware } from "../middleware/index.js";
|
|
4
7
|
import { Multipart } from "../multipart/index.js";
|
|
5
|
-
import { Render } from "../render/index.js";
|
|
6
8
|
import { Route } from "../route/index.js";
|
|
7
|
-
import { Form as FormSchema } from "../schema/index.js";
|
|
8
|
-
import type { Trie } from "../trie/index.js";
|
|
9
9
|
import { Checksum, Header, Method, Mime, type Util } from "../util/index.js";
|
|
10
10
|
|
|
11
|
+
/**
|
|
12
|
+
* Requests an agent makes through an MCP tool, with the session its token
|
|
13
|
+
* authorizes. `c.data()` adds the form's issues, which the MCP endpoint
|
|
14
|
+
* returns to the agent instead of following the handler's redirect.
|
|
15
|
+
*/
|
|
16
|
+
export const agent = new WeakMap<
|
|
17
|
+
Request,
|
|
18
|
+
{
|
|
19
|
+
readonly session?: unknown;
|
|
20
|
+
issues?: readonly { readonly path: string; readonly message: string }[];
|
|
21
|
+
/** The tool a form submits to and its arguments, so a page points to it */
|
|
22
|
+
readonly tool?: (
|
|
23
|
+
method: string,
|
|
24
|
+
action: URL,
|
|
25
|
+
) => { name: string; args: Record<string, string> } | undefined;
|
|
26
|
+
}
|
|
27
|
+
>();
|
|
28
|
+
|
|
11
29
|
/** Properties to build the final `Response` with once middleware has run. */
|
|
12
30
|
class PreparedResponse {
|
|
13
31
|
/**
|
|
@@ -32,27 +50,49 @@ class PreparedResponse {
|
|
|
32
50
|
headers = new Headers();
|
|
33
51
|
}
|
|
34
52
|
|
|
53
|
+
/** Reads a request's session, set by a mounted `Auth` */
|
|
54
|
+
export const sessions = Symbol("sessions");
|
|
55
|
+
|
|
35
56
|
export namespace Context {
|
|
57
|
+
/** A signed-in user's revocable session. */
|
|
58
|
+
export interface Session {
|
|
59
|
+
/**
|
|
60
|
+
* Opaque session ID: a browser session's row ID in `auth_sessions`, or
|
|
61
|
+
* for an agent's tool call, a hash of its access token.
|
|
62
|
+
*/
|
|
63
|
+
readonly id: string;
|
|
64
|
+
/** User ID. */
|
|
65
|
+
readonly user: string;
|
|
66
|
+
/** Time in milliseconds the session ends unless it is used, which renews it. */
|
|
67
|
+
readonly expiration: number;
|
|
68
|
+
}
|
|
69
|
+
|
|
36
70
|
/**
|
|
37
|
-
* Context.data return type
|
|
71
|
+
* Context.data return type: `null` for a route without fields, and either
|
|
72
|
+
* where the fields are not known, such as in middleware written on its own.
|
|
38
73
|
*
|
|
39
|
-
*
|
|
74
|
+
* @template S Form shape, `any` where it is not known
|
|
75
|
+
*/
|
|
76
|
+
export type Result<S extends Field.Shape> = 0 extends 1 & S
|
|
77
|
+
? Parsed<Field.Shape> | null
|
|
78
|
+
: [Field.Shape] extends [S]
|
|
79
|
+
? null
|
|
80
|
+
: Parsed<S>;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Parsed fields, with the URL to return to when they are invalid.
|
|
40
84
|
*
|
|
41
85
|
* @template S Form shape
|
|
42
86
|
*/
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
readonly url: URL;
|
|
53
|
-
}
|
|
54
|
-
>
|
|
55
|
-
>;
|
|
87
|
+
type Parsed<S extends Field.Shape> = Util.Prettify<
|
|
88
|
+
Field.Parse.Result<
|
|
89
|
+
S,
|
|
90
|
+
{
|
|
91
|
+
/** URL with invalid field values encoded into a search parameter. */
|
|
92
|
+
readonly url: URL;
|
|
93
|
+
}
|
|
94
|
+
>
|
|
95
|
+
>;
|
|
56
96
|
}
|
|
57
97
|
|
|
58
98
|
/**
|
|
@@ -62,8 +102,9 @@ export namespace Context {
|
|
|
62
102
|
* @template Shape Parsed form data shape
|
|
63
103
|
*/
|
|
64
104
|
export class Context<
|
|
65
|
-
Params extends
|
|
66
|
-
|
|
105
|
+
Params extends Record<string, unknown> = Record<string, unknown>,
|
|
106
|
+
// any, so middleware written on its own fits a route with fields
|
|
107
|
+
Shape extends Field.Shape = any,
|
|
67
108
|
> {
|
|
68
109
|
/**
|
|
69
110
|
* Incoming `Request` to the server.
|
|
@@ -71,6 +112,8 @@ export class Context<
|
|
|
71
112
|
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Request)
|
|
72
113
|
*/
|
|
73
114
|
readonly req: Request;
|
|
115
|
+
/** Trusted client IP supplied by the server through App.fetch; never inferred from request headers. */
|
|
116
|
+
readonly ip: string | undefined;
|
|
74
117
|
|
|
75
118
|
/**
|
|
76
119
|
* Parsed `URL` created from `req.url`.
|
|
@@ -88,22 +131,29 @@ export class Context<
|
|
|
88
131
|
/** Contains the arguments to used create the final `Response` */
|
|
89
132
|
readonly res = new PreparedResponse();
|
|
90
133
|
|
|
91
|
-
/** Get
|
|
134
|
+
/** Get and set cookies, a cookie set with `maxAge: 0` is deleted. */
|
|
92
135
|
readonly cookie = new Cookie(this);
|
|
93
136
|
|
|
94
137
|
/** Forwarded app options */
|
|
95
138
|
readonly #options: App.Options;
|
|
96
139
|
|
|
140
|
+
/** `Vary: Accept` is set */
|
|
141
|
+
#varied = false;
|
|
142
|
+
|
|
97
143
|
/** Cached result from `data()` to avoid re-reading request bodies. */
|
|
98
144
|
#parsed?: Promise<Context.Result<Shape>>;
|
|
99
145
|
|
|
146
|
+
/** Session reader, set by `app.use(auth)` */
|
|
147
|
+
[sessions]?: () => Promise<Context.Session | null>;
|
|
148
|
+
|
|
100
149
|
/**
|
|
101
150
|
* Creates a new `Context` with the current `Request`.
|
|
102
151
|
*
|
|
103
152
|
* @param req Request
|
|
104
153
|
*/
|
|
105
|
-
constructor(req: Request, options: App.Options) {
|
|
154
|
+
constructor(req: Request, options: App.Options, ip?: string) {
|
|
106
155
|
this.req = req;
|
|
156
|
+
this.ip = ip;
|
|
107
157
|
this.url = new URL(req.url);
|
|
108
158
|
this.#options = options;
|
|
109
159
|
}
|
|
@@ -115,21 +165,18 @@ export class Context<
|
|
|
115
165
|
* @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
|
|
116
166
|
*/
|
|
117
167
|
html(body: BodyInit | null, status?: number) {
|
|
118
|
-
this
|
|
119
|
-
this.res.status = status;
|
|
120
|
-
this.res.headers.set(Header.name.type, Header.utf8(Mime.type.html));
|
|
168
|
+
this.#respond(body, status, Header.utf8(Mime.type.html));
|
|
121
169
|
}
|
|
122
170
|
|
|
123
171
|
/**
|
|
124
172
|
* Creates a JSON response.
|
|
125
173
|
*
|
|
174
|
+
* @template D Data, not a `bigint`, which JSON cannot represent
|
|
126
175
|
* @param data passed into JSON.stringify to create the body
|
|
127
176
|
* @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
|
|
128
177
|
*/
|
|
129
178
|
json<D>(data: D extends bigint ? never : D, status?: number) {
|
|
130
|
-
this
|
|
131
|
-
this.res.status = status;
|
|
132
|
-
this.res.headers.set(Header.name.type, Mime.type.json);
|
|
179
|
+
this.#respond(JSON.stringify(data), status, Mime.type.json);
|
|
133
180
|
}
|
|
134
181
|
|
|
135
182
|
/**
|
|
@@ -139,14 +186,47 @@ export class Context<
|
|
|
139
186
|
* @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
|
|
140
187
|
*/
|
|
141
188
|
text(body: BodyInit | null, status?: number) {
|
|
189
|
+
this.#respond(body, status, Header.utf8(Mime.type.text));
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Responds with Markdown when the request asks for Markdown, such as an
|
|
194
|
+
* agent's, instead of the Markdown written from the page. Use it for a page
|
|
195
|
+
* that is a Markdown document, so agents read its source.
|
|
196
|
+
*
|
|
197
|
+
* ```tsx
|
|
198
|
+
* if (c.md(post.body)) return;
|
|
199
|
+
*
|
|
200
|
+
* return <Post post={post} />;
|
|
201
|
+
* ```
|
|
202
|
+
*
|
|
203
|
+
* @param body Markdown body
|
|
204
|
+
* @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
|
|
205
|
+
* @returns `true` if it responded, otherwise the page renders for the browser
|
|
206
|
+
*/
|
|
207
|
+
md(body: BodyInit | null, status?: number) {
|
|
208
|
+
if (!this.#markdown()) return false;
|
|
209
|
+
|
|
210
|
+
this.#respond(body, status, Header.utf8(Mime.type.md));
|
|
211
|
+
|
|
212
|
+
return true;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* @param body Response body
|
|
217
|
+
* @param status HTTP response status code
|
|
218
|
+
* @param type `Content-Type` of the body
|
|
219
|
+
*/
|
|
220
|
+
#respond(body: BodyInit | null, status: number | undefined, type: string) {
|
|
142
221
|
this.res.body = body;
|
|
143
222
|
this.res.status = status;
|
|
144
|
-
this.res.headers.set(Header.name.type,
|
|
223
|
+
this.res.headers.set(Header.name.type, type);
|
|
145
224
|
}
|
|
146
225
|
|
|
147
226
|
/**
|
|
148
227
|
* Creates a redirect response.
|
|
149
228
|
*
|
|
229
|
+
* @template L Location: a URL, or a route without parameters
|
|
150
230
|
* @param location redirect `Location` header
|
|
151
231
|
* @param status HTTP status code
|
|
152
232
|
*
|
|
@@ -156,15 +236,20 @@ export class Context<
|
|
|
156
236
|
* - [307 Temporary Redirect](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/307)
|
|
157
237
|
* - [308 Permanent Redirect](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/308)
|
|
158
238
|
*/
|
|
159
|
-
redirect(
|
|
160
|
-
|
|
239
|
+
redirect<L extends string | URL | Route>(
|
|
240
|
+
// a route with parameters needs values, which only route.url() is given
|
|
241
|
+
location: L extends Route<infer P>
|
|
242
|
+
? P extends `${string}${":" | "*"}${string}`
|
|
243
|
+
? never
|
|
244
|
+
: L
|
|
245
|
+
: L,
|
|
161
246
|
status: 301 | 302 | 303 | 307 | 308 = 302,
|
|
162
247
|
) {
|
|
163
248
|
this.res.body = null;
|
|
164
249
|
this.res.status = status;
|
|
165
250
|
this.res.headers.set(
|
|
166
251
|
Header.name.loc,
|
|
167
|
-
location instanceof Route ? location.
|
|
252
|
+
location instanceof Route ? location.url() : String(location),
|
|
168
253
|
);
|
|
169
254
|
}
|
|
170
255
|
|
|
@@ -204,8 +289,8 @@ export class Context<
|
|
|
204
289
|
* ```ts
|
|
205
290
|
* import { Route } from "ovr";
|
|
206
291
|
*
|
|
207
|
-
* const post = Route.post(async (c) => {
|
|
208
|
-
* for await (const part of c.
|
|
292
|
+
* const post = Route.post("/posts").use(async (c) => {
|
|
293
|
+
* for await (const part of c.multipart()) {
|
|
209
294
|
* if (part.name === "email") {
|
|
210
295
|
* // ...
|
|
211
296
|
* }
|
|
@@ -213,31 +298,59 @@ export class Context<
|
|
|
213
298
|
* })
|
|
214
299
|
* ```
|
|
215
300
|
*/
|
|
216
|
-
|
|
217
|
-
return new Multipart(this.req, { ...this.#options.
|
|
301
|
+
multipart(options?: Multipart.Options) {
|
|
302
|
+
return new Multipart(this.req, { ...this.#options.multipart, ...options });
|
|
218
303
|
}
|
|
219
304
|
|
|
220
305
|
/**
|
|
221
|
-
*
|
|
306
|
+
* Read the signed-in user's session before returning JSX, or `null` when
|
|
307
|
+
* nobody is signed in. In an agent's tool call, it is the session of the
|
|
308
|
+
* user who approved the agent. Requires `app.use(auth)`.
|
|
222
309
|
*
|
|
223
|
-
*
|
|
310
|
+
* ```ts
|
|
311
|
+
* const session = await c.session();
|
|
312
|
+
* if (!session) return c.redirect("/login", 303);
|
|
313
|
+
* ```
|
|
314
|
+
*
|
|
315
|
+
* @returns The session, or `null`
|
|
316
|
+
*/
|
|
317
|
+
session() {
|
|
318
|
+
if (!this[sessions])
|
|
319
|
+
throw new TypeError("Mount Auth with app.use(auth) to read sessions");
|
|
320
|
+
return this[sessions]();
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Parse route form data: from the search params of a GET or HEAD request,
|
|
325
|
+
* and from the multipart body of any other.
|
|
326
|
+
*
|
|
327
|
+
* @param options Multipart options for requests with a body
|
|
224
328
|
* @returns Parsed form data with possible invalid redirect URL state
|
|
225
329
|
*/
|
|
226
330
|
async data(options?: Multipart.Options): Promise<Context.Result<Shape>> {
|
|
227
331
|
return (this.#parsed ??= (async () => {
|
|
228
|
-
|
|
229
|
-
const
|
|
332
|
+
// GET and HEAD carry their fields in the URL, every other method in its body
|
|
333
|
+
const post =
|
|
334
|
+
this.req.method !== Method.get && this.req.method !== Method.head;
|
|
335
|
+
const form = this.route && forms.get(this.route);
|
|
230
336
|
|
|
231
|
-
if (!form
|
|
337
|
+
if (!form) return null as Context.Result<Shape>;
|
|
232
338
|
|
|
339
|
+
// a body too large or not form data throws, for the handler to answer
|
|
233
340
|
const result = await form.parse(
|
|
234
341
|
post
|
|
235
|
-
? this.
|
|
342
|
+
? this.multipart({ parts: form.parts, ...options })
|
|
236
343
|
: this.url.searchParams,
|
|
237
344
|
);
|
|
238
345
|
|
|
239
|
-
|
|
240
|
-
|
|
346
|
+
/**
|
|
347
|
+
* Add the URL to return to with the encoded invalid state.
|
|
348
|
+
*
|
|
349
|
+
* @template R Parse result, with its search parameters
|
|
350
|
+
*/
|
|
351
|
+
const invalid = <R extends { search: Field.Parse.Result.Search }>(
|
|
352
|
+
result: R,
|
|
353
|
+
) => {
|
|
241
354
|
let url = new URL(this.url);
|
|
242
355
|
|
|
243
356
|
if (post) {
|
|
@@ -258,13 +371,43 @@ export class Context<
|
|
|
258
371
|
|
|
259
372
|
if (result.search) url.searchParams.set(...result.search[0]);
|
|
260
373
|
|
|
261
|
-
return Object.assign(result, { url })
|
|
374
|
+
return Object.assign(result, { url });
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
if (result.issues) {
|
|
378
|
+
this.#agentIssues(result.issues);
|
|
379
|
+
return invalid(result) as Context.Result<Shape>;
|
|
262
380
|
}
|
|
263
381
|
|
|
264
|
-
|
|
382
|
+
const create = result.invalid;
|
|
383
|
+
|
|
384
|
+
return Object.assign(result, {
|
|
385
|
+
invalid: (messages: Field.Parse.Messages<Field.Shape>) => {
|
|
386
|
+
const result = create(messages);
|
|
387
|
+
this.#agentIssues(result.issues);
|
|
388
|
+
return invalid(result);
|
|
389
|
+
},
|
|
390
|
+
}) as Context.Result<Shape>;
|
|
265
391
|
})());
|
|
266
392
|
}
|
|
267
393
|
|
|
394
|
+
/**
|
|
395
|
+
* Agents get the issues directly instead of a redirect to the form.
|
|
396
|
+
*
|
|
397
|
+
* @param issues Parse issues
|
|
398
|
+
*/
|
|
399
|
+
#agentIssues(
|
|
400
|
+
issues: readonly { path: readonly PropertyKey[]; message: string }[],
|
|
401
|
+
) {
|
|
402
|
+
const call = agent.get(this.req);
|
|
403
|
+
if (!call) return;
|
|
404
|
+
|
|
405
|
+
call.issues = issues.map((issue) => ({
|
|
406
|
+
path: issue.path.map(String).join("."),
|
|
407
|
+
message: issue.message,
|
|
408
|
+
}));
|
|
409
|
+
}
|
|
410
|
+
|
|
268
411
|
/**
|
|
269
412
|
* Dispatches the stack of `middleware` provided.
|
|
270
413
|
*
|
|
@@ -272,44 +415,74 @@ export class Context<
|
|
|
272
415
|
* @param i current middleware index (default `0`)
|
|
273
416
|
* @returns return value of `middleware[i]`
|
|
274
417
|
*/
|
|
275
|
-
async #run(middleware: Middleware<
|
|
418
|
+
async #run(middleware: Middleware<any, any>[], i = 0) {
|
|
276
419
|
const mw = middleware[i];
|
|
277
420
|
|
|
278
421
|
if (!mw) return; // end of stack
|
|
279
422
|
|
|
280
|
-
const r = await mw(
|
|
281
|
-
|
|
282
|
-
() => this.#run(middleware, i + 1), // next
|
|
283
|
-
);
|
|
423
|
+
const r: unknown = await mw(this, () => this.#run(middleware, i + 1));
|
|
424
|
+
const { res } = this;
|
|
284
425
|
|
|
285
426
|
// resolve the return value
|
|
286
427
|
if (r instanceof Response) {
|
|
287
428
|
// overwrite
|
|
288
|
-
|
|
289
|
-
|
|
429
|
+
res.body = r.body;
|
|
430
|
+
res.status = r.status;
|
|
290
431
|
|
|
291
|
-
// merge
|
|
432
|
+
// merge, each cookie is its own header, added to ones already set
|
|
292
433
|
for (const [name, header] of r.headers) {
|
|
293
|
-
|
|
434
|
+
if (name !== Header.name.set) res.headers.set(name, header);
|
|
294
435
|
}
|
|
436
|
+
for (const cookie of r.headers.getSetCookie())
|
|
437
|
+
res.headers.append(Header.name.set, cookie);
|
|
295
438
|
} else if (r !== undefined) {
|
|
296
439
|
// something to stream
|
|
297
|
-
const [mime] = Header.shift(
|
|
440
|
+
const [mime] = Header.shift(res.headers.get(Header.name.type));
|
|
441
|
+
|
|
442
|
+
if (!mime || mime === Mime.type.html) {
|
|
443
|
+
// pages are HTML for browsers and Markdown for agents that ask for it
|
|
444
|
+
if (this.#markdown()) {
|
|
445
|
+
res.body = Markdown.stream(r, {
|
|
446
|
+
base: this.url,
|
|
447
|
+
[tools]: agent.get(this.req)?.tool,
|
|
448
|
+
});
|
|
449
|
+
res.headers.set(Header.name.type, Header.utf8(Mime.type.md));
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
298
453
|
|
|
299
|
-
|
|
300
|
-
// other defined types are
|
|
301
|
-
|
|
454
|
+
res.body = Markup.stream(r, {
|
|
455
|
+
// other defined types are not markup, so they are not escaped
|
|
456
|
+
raw: Boolean(mime && !Mime.markup(mime)),
|
|
302
457
|
});
|
|
303
458
|
|
|
304
459
|
if (!mime) {
|
|
305
460
|
// default to HTML
|
|
306
|
-
|
|
461
|
+
res.headers.set(Header.name.type, Header.utf8(Mime.type.html));
|
|
307
462
|
}
|
|
308
463
|
|
|
309
464
|
// do not overwrite/remove status - that way user can set it before returning
|
|
310
465
|
}
|
|
311
466
|
}
|
|
312
467
|
|
|
468
|
+
/**
|
|
469
|
+
* The response depends on `Accept` once this is asked, so it varies by it.
|
|
470
|
+
*
|
|
471
|
+
* @returns `true` if the request asks for Markdown over HTML
|
|
472
|
+
*/
|
|
473
|
+
#markdown() {
|
|
474
|
+
if (!this.#varied) {
|
|
475
|
+
this.#varied = true;
|
|
476
|
+
this.res.headers.append(Header.name.vary, "Accept");
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
const accept = this.req.headers.get(Header.name.accept);
|
|
480
|
+
// only an explicit request, browsers accept */*
|
|
481
|
+
const markdown = Header.quality(accept, Mime.type.md, true);
|
|
482
|
+
|
|
483
|
+
return markdown > 0 && markdown >= Header.quality(accept, Mime.type.html);
|
|
484
|
+
}
|
|
485
|
+
|
|
313
486
|
/**
|
|
314
487
|
* Composes a stack of `middleware` into a `Response`.
|
|
315
488
|
*
|
|
@@ -320,20 +493,22 @@ export class Context<
|
|
|
320
493
|
static async compose(c: Context, middleware: Middleware[]) {
|
|
321
494
|
await c.#run(middleware);
|
|
322
495
|
|
|
496
|
+
const { res } = c;
|
|
497
|
+
|
|
323
498
|
if (c.req.method === Method.head) {
|
|
324
|
-
if (
|
|
499
|
+
if (res.body instanceof ReadableStream) {
|
|
325
500
|
// cancel unused stream to prevent leaks
|
|
326
|
-
await
|
|
501
|
+
await res.body.cancel(Method.head);
|
|
327
502
|
}
|
|
328
503
|
|
|
329
|
-
|
|
504
|
+
res.body = null;
|
|
330
505
|
}
|
|
331
506
|
|
|
332
507
|
return new Response(
|
|
333
|
-
|
|
508
|
+
res.body,
|
|
334
509
|
// prevents users from setting the response during the stream
|
|
335
510
|
// `Response` treats an explicitly undefined status as omitted
|
|
336
|
-
Object.freeze(
|
|
511
|
+
Object.freeze(res) as ResponseInit,
|
|
337
512
|
);
|
|
338
513
|
}
|
|
339
514
|
}
|
package/src/cookie/index.ts
CHANGED
|
@@ -111,6 +111,8 @@ export namespace Cookie {
|
|
|
111
111
|
* Configuration options for setting a cookie.
|
|
112
112
|
*
|
|
113
113
|
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie)
|
|
114
|
+
*
|
|
115
|
+
* @template Name Cookie name, whose `__Host-` or `__Secure-` prefix requires options
|
|
114
116
|
*/
|
|
115
117
|
export type Options<Name extends string = string> =
|
|
116
118
|
Name extends `__${"host" | "secure"}-${string}`
|
|
@@ -154,6 +156,7 @@ export class Cookie {
|
|
|
154
156
|
/**
|
|
155
157
|
* Sets a cookie on the response using the `Set-Cookie` header.
|
|
156
158
|
*
|
|
159
|
+
* @template Name Cookie name, whose prefix selects the options it requires
|
|
157
160
|
* @param name Name of the cookie
|
|
158
161
|
* @param value Value to store
|
|
159
162
|
* @param options Options configuration
|