ovr 4.2.4 → 4.2.5
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/dist/app/context.d.ts +9 -6
- package/dist/app/context.d.ts.map +1 -1
- package/dist/app/context.js +49 -31
- package/package.json +1 -1
- package/src/app/context.ts +51 -36
package/dist/app/context.d.ts
CHANGED
|
@@ -21,20 +21,23 @@ export declare class Context<P extends Params = Params> {
|
|
|
21
21
|
params: P;
|
|
22
22
|
/** The matched `Route` instance. */
|
|
23
23
|
route: Route<Middleware<P>[]>;
|
|
24
|
-
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response#body) */
|
|
25
|
-
body: BodyInit | null;
|
|
26
|
-
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status) */
|
|
27
|
-
status?: number;
|
|
28
24
|
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Headers) */
|
|
29
25
|
headers: Headers;
|
|
30
|
-
/** Passed from `app.base` */
|
|
31
|
-
base: string;
|
|
32
26
|
/** Passed from `app.error` */
|
|
33
27
|
error: ErrorHandler<P>;
|
|
34
28
|
/** Passed from `app.notFound` */
|
|
35
29
|
notFound: NotFoundHandler<P>;
|
|
36
30
|
memo: <A extends any[], R>(fn: (...args: A) => R) => (...args: A) => R;
|
|
37
31
|
constructor(req: Request, url: URL, trailingSlash: TrailingSlash, base: string, error: ErrorHandler<P>, notFound: NotFoundHandler<P>);
|
|
32
|
+
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status) */
|
|
33
|
+
get status(): number | undefined;
|
|
34
|
+
set status(status: number | undefined);
|
|
35
|
+
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response#body) */
|
|
36
|
+
get body(): BodyInit | null;
|
|
37
|
+
set body(body: BodyInit | null);
|
|
38
|
+
/** Passed from `app.base` */
|
|
39
|
+
get base(): string;
|
|
40
|
+
set base(base: string);
|
|
38
41
|
/**
|
|
39
42
|
* Mirrors `new Response()` constructor, set values with one function.
|
|
40
43
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/app/context.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,GAAG,EAAe,MAAM,iBAAiB,CAAC;AACxD,OAAO,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAEtD,OAAO,EAEN,KAAK,YAAY,EACjB,KAAK,UAAU,EACf,KAAK,eAAe,EACpB,KAAK,aAAa,EAClB,MAAM,YAAY,CAAC;AAGpB,KAAK,MAAM,GAAG,CAAC,KAAK,EAAE;IAAE,QAAQ,EAAE,GAAG,CAAC,OAAO,CAAA;CAAE,KAAK,GAAG,CAAC,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/app/context.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,GAAG,EAAe,MAAM,iBAAiB,CAAC;AACxD,OAAO,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAEtD,OAAO,EAEN,KAAK,YAAY,EACjB,KAAK,UAAU,EACf,KAAK,eAAe,EACpB,KAAK,aAAa,EAClB,MAAM,YAAY,CAAC;AAGpB,KAAK,MAAM,GAAG,CAAC,KAAK,EAAE;IAAE,QAAQ,EAAE,GAAG,CAAC,OAAO,CAAA;CAAE,KAAK,GAAG,CAAC,OAAO,CAAC;AAoBhE,qBAAa,OAAO,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM;;IAC7C,gFAAgF;IAChF,GAAG,EAAE,OAAO,CAAC;IAEb,4EAA4E;IAC5E,GAAG,EAAE,GAAG,CAAC;IAET;;;;;;;OAOG;IACH,MAAM,EAAG,CAAC,CAAC;IAEX,oCAAoC;IACpC,KAAK,EAAG,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC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G;IACH,IAAI,CAAC,GAAG,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE;IAK9B;;OAEG;IACH,MAAM,CAAC,GAAG,MAAM,EAAE,MAAM,EAAE;IAK1B;;;;;OAKG;IACH,IAAI,CAAC,IAAI,EAAE,GAAG,CAAC,OAAO,EAAE,MAAM,CAAC,EAAE,MAAM;IAmEvC;;;;;;;;OAQG;IACH,IAAI,CAAC,CAAC,EAAE,MAAM;IAed;;OAEG;IACH,KAAK;IA2BL;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,MAAM,CAAC,GAAG;CAUV"}
|
package/dist/app/context.js
CHANGED
|
@@ -4,9 +4,17 @@ import { hash } from "../util/hash.js";
|
|
|
4
4
|
import { App, } from "./index.js";
|
|
5
5
|
import { Memo } from "./memo/index.js";
|
|
6
6
|
class TagNotFound extends Error {
|
|
7
|
+
name = "TagNotFound";
|
|
7
8
|
constructor(tag) {
|
|
8
9
|
super(`No closing ${tag} tag found`);
|
|
9
|
-
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
class ResponseFinalized extends Error {
|
|
13
|
+
name = "ResponseFinalized";
|
|
14
|
+
constructor(property) {
|
|
15
|
+
super(`Cannot set \`${property}\` after the \`Response\` has been finalized. ` +
|
|
16
|
+
`This can occur if \`${property}\` is set within a component since ` +
|
|
17
|
+
"components are evaluated during streaming.");
|
|
10
18
|
}
|
|
11
19
|
}
|
|
12
20
|
export class Context {
|
|
@@ -25,17 +33,14 @@ export class Context {
|
|
|
25
33
|
params; // set after match
|
|
26
34
|
/** The matched `Route` instance. */
|
|
27
35
|
route; // set after match
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status) */
|
|
31
|
-
status;
|
|
36
|
+
#body = null;
|
|
37
|
+
#status;
|
|
32
38
|
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Headers) */
|
|
33
39
|
headers = new Headers();
|
|
34
40
|
#layouts = [];
|
|
35
41
|
#headElements = [];
|
|
36
42
|
#trailingSlash;
|
|
37
|
-
|
|
38
|
-
base;
|
|
43
|
+
#base;
|
|
39
44
|
/** Passed from `app.error` */
|
|
40
45
|
error;
|
|
41
46
|
/** Passed from `app.notFound` */
|
|
@@ -43,18 +48,45 @@ export class Context {
|
|
|
43
48
|
/** `Memo` unique to the current `Request` context */
|
|
44
49
|
#memo = new Memo();
|
|
45
50
|
memo = this.#memo.use;
|
|
46
|
-
/**
|
|
47
|
-
#
|
|
51
|
+
/** Check if the `Response` has been built already */
|
|
52
|
+
#finalized = false;
|
|
48
53
|
/** Used across requests */
|
|
49
54
|
static #encoder = new TextEncoder();
|
|
50
55
|
constructor(req, url, trailingSlash, base, error, notFound) {
|
|
51
56
|
this.req = req;
|
|
52
57
|
this.url = url;
|
|
53
58
|
this.#trailingSlash = trailingSlash;
|
|
54
|
-
this
|
|
59
|
+
this.#base = base;
|
|
55
60
|
this.error = error;
|
|
56
61
|
this.notFound = notFound;
|
|
57
62
|
}
|
|
63
|
+
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status) */
|
|
64
|
+
get status() {
|
|
65
|
+
return this.#status;
|
|
66
|
+
}
|
|
67
|
+
set status(status) {
|
|
68
|
+
if (this.#finalized)
|
|
69
|
+
throw new ResponseFinalized("status");
|
|
70
|
+
this.#status = status;
|
|
71
|
+
}
|
|
72
|
+
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response#body) */
|
|
73
|
+
get body() {
|
|
74
|
+
return this.#body;
|
|
75
|
+
}
|
|
76
|
+
set body(body) {
|
|
77
|
+
if (this.#finalized)
|
|
78
|
+
throw new ResponseFinalized("body");
|
|
79
|
+
this.#body = body;
|
|
80
|
+
}
|
|
81
|
+
/** Passed from `app.base` */
|
|
82
|
+
get base() {
|
|
83
|
+
return this.#base;
|
|
84
|
+
}
|
|
85
|
+
set base(base) {
|
|
86
|
+
if (this.#finalized)
|
|
87
|
+
throw new ResponseFinalized("base");
|
|
88
|
+
this.#base = base;
|
|
89
|
+
}
|
|
58
90
|
/**
|
|
59
91
|
* Mirrors `new Response()` constructor, set values with one function.
|
|
60
92
|
*
|
|
@@ -126,18 +158,16 @@ export class Context {
|
|
|
126
158
|
* @param Element `JSX.Element` to add to the head
|
|
127
159
|
*/
|
|
128
160
|
head(...Element) {
|
|
129
|
-
if (this.#
|
|
130
|
-
throw new
|
|
131
|
-
"This can occur if `head` is called within a component since " +
|
|
132
|
-
"components are evaluated during the stream, often times after " +
|
|
133
|
-
"the <head> element has already been sent.");
|
|
134
|
-
}
|
|
161
|
+
if (this.#finalized)
|
|
162
|
+
throw new ResponseFinalized("head");
|
|
135
163
|
this.#headElements.push(...Element);
|
|
136
164
|
}
|
|
137
165
|
/**
|
|
138
166
|
* @param Layout `Layout`(s) for the `Page` to be passed into before being injected into the `<body>`
|
|
139
167
|
*/
|
|
140
168
|
layout(...Layout) {
|
|
169
|
+
if (this.#finalized)
|
|
170
|
+
throw new ResponseFinalized("layout");
|
|
141
171
|
this.#layouts.push(...Layout);
|
|
142
172
|
}
|
|
143
173
|
/**
|
|
@@ -183,7 +213,7 @@ export class Context {
|
|
|
183
213
|
}
|
|
184
214
|
let result;
|
|
185
215
|
this.html(new ReadableStream({
|
|
186
|
-
|
|
216
|
+
async pull(c) {
|
|
187
217
|
result = await gen.next();
|
|
188
218
|
if (result.done) {
|
|
189
219
|
c.close();
|
|
@@ -221,11 +251,6 @@ export class Context {
|
|
|
221
251
|
* @returns the constructed `Response`
|
|
222
252
|
*/
|
|
223
253
|
build() {
|
|
224
|
-
if (this.req.signal.aborted) {
|
|
225
|
-
// user cancelled the request
|
|
226
|
-
this.body = null;
|
|
227
|
-
this.status = 408;
|
|
228
|
-
}
|
|
229
254
|
if ((!this.status || this.status === 404) &&
|
|
230
255
|
this.#trailingSlash !== "ignore") {
|
|
231
256
|
const last = this.url.pathname.at(-1);
|
|
@@ -242,15 +267,8 @@ export class Context {
|
|
|
242
267
|
}
|
|
243
268
|
if (!this.body && !this.status)
|
|
244
269
|
this.notFound(this);
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
/**
|
|
248
|
-
* Check if the `Response` has been set and the body has been read.
|
|
249
|
-
*
|
|
250
|
-
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response/bodyUsed)
|
|
251
|
-
*/
|
|
252
|
-
get #bodyUsed() {
|
|
253
|
-
return Boolean(this.#response?.bodyUsed);
|
|
270
|
+
this.#finalized = true;
|
|
271
|
+
return new Response(this.body, this);
|
|
254
272
|
}
|
|
255
273
|
/**
|
|
256
274
|
* Call within the scope of a handler to get the current context.
|
package/package.json
CHANGED
package/src/app/context.ts
CHANGED
|
@@ -14,9 +14,20 @@ import { Memo } from "./memo/index.js";
|
|
|
14
14
|
type Layout = (props: { children: JSX.Element }) => JSX.Element;
|
|
15
15
|
|
|
16
16
|
class TagNotFound extends Error {
|
|
17
|
+
override name = "TagNotFound";
|
|
17
18
|
constructor(tag: string) {
|
|
18
19
|
super(`No closing ${tag} tag found`);
|
|
19
|
-
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
class ResponseFinalized extends Error {
|
|
24
|
+
override name = "ResponseFinalized";
|
|
25
|
+
constructor(property: string) {
|
|
26
|
+
super(
|
|
27
|
+
`Cannot set \`${property}\` after the \`Response\` has been finalized. ` +
|
|
28
|
+
`This can occur if \`${property}\` is set within a component since ` +
|
|
29
|
+
"components are evaluated during streaming.",
|
|
30
|
+
);
|
|
20
31
|
}
|
|
21
32
|
}
|
|
22
33
|
|
|
@@ -40,11 +51,9 @@ export class Context<P extends Params = Params> {
|
|
|
40
51
|
/** The matched `Route` instance. */
|
|
41
52
|
route!: Route<Middleware<P>[]>; // set after match
|
|
42
53
|
|
|
43
|
-
|
|
44
|
-
body: BodyInit | null = null;
|
|
54
|
+
#body: BodyInit | null = null;
|
|
45
55
|
|
|
46
|
-
|
|
47
|
-
status?: number;
|
|
56
|
+
#status?: number;
|
|
48
57
|
|
|
49
58
|
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Headers) */
|
|
50
59
|
headers = new Headers();
|
|
@@ -55,8 +64,7 @@ export class Context<P extends Params = Params> {
|
|
|
55
64
|
|
|
56
65
|
#trailingSlash: TrailingSlash;
|
|
57
66
|
|
|
58
|
-
|
|
59
|
-
base: string;
|
|
67
|
+
#base: string;
|
|
60
68
|
|
|
61
69
|
/** Passed from `app.error` */
|
|
62
70
|
error: ErrorHandler<P>;
|
|
@@ -69,11 +77,11 @@ export class Context<P extends Params = Params> {
|
|
|
69
77
|
|
|
70
78
|
memo = this.#memo.use;
|
|
71
79
|
|
|
72
|
-
/**
|
|
73
|
-
#
|
|
80
|
+
/** Check if the `Response` has been built already */
|
|
81
|
+
#finalized = false;
|
|
74
82
|
|
|
75
83
|
/** Used across requests */
|
|
76
|
-
static #encoder = new TextEncoder();
|
|
84
|
+
static readonly #encoder = new TextEncoder();
|
|
77
85
|
|
|
78
86
|
constructor(
|
|
79
87
|
req: Request,
|
|
@@ -86,11 +94,38 @@ export class Context<P extends Params = Params> {
|
|
|
86
94
|
this.req = req;
|
|
87
95
|
this.url = url;
|
|
88
96
|
this.#trailingSlash = trailingSlash;
|
|
89
|
-
this
|
|
97
|
+
this.#base = base;
|
|
90
98
|
this.error = error;
|
|
91
99
|
this.notFound = notFound;
|
|
92
100
|
}
|
|
93
101
|
|
|
102
|
+
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status) */
|
|
103
|
+
get status() {
|
|
104
|
+
return this.#status;
|
|
105
|
+
}
|
|
106
|
+
set status(status) {
|
|
107
|
+
if (this.#finalized) throw new ResponseFinalized("status");
|
|
108
|
+
this.#status = status;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response#body) */
|
|
112
|
+
get body() {
|
|
113
|
+
return this.#body;
|
|
114
|
+
}
|
|
115
|
+
set body(body) {
|
|
116
|
+
if (this.#finalized) throw new ResponseFinalized("body");
|
|
117
|
+
this.#body = body;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Passed from `app.base` */
|
|
121
|
+
get base() {
|
|
122
|
+
return this.#base;
|
|
123
|
+
}
|
|
124
|
+
set base(base) {
|
|
125
|
+
if (this.#finalized) throw new ResponseFinalized("base");
|
|
126
|
+
this.#base = base;
|
|
127
|
+
}
|
|
128
|
+
|
|
94
129
|
/**
|
|
95
130
|
* Mirrors `new Response()` constructor, set values with one function.
|
|
96
131
|
*
|
|
@@ -181,15 +216,7 @@ export class Context<P extends Params = Params> {
|
|
|
181
216
|
* @param Element `JSX.Element` to add to the head
|
|
182
217
|
*/
|
|
183
218
|
head(...Element: JSX.Element[]) {
|
|
184
|
-
if (this.#
|
|
185
|
-
throw new Error(
|
|
186
|
-
"Cannot call `head` after `Response.body` has been read. " +
|
|
187
|
-
"This can occur if `head` is called within a component since " +
|
|
188
|
-
"components are evaluated during the stream, often times after " +
|
|
189
|
-
"the <head> element has already been sent.",
|
|
190
|
-
);
|
|
191
|
-
}
|
|
192
|
-
|
|
219
|
+
if (this.#finalized) throw new ResponseFinalized("head");
|
|
193
220
|
this.#headElements.push(...Element);
|
|
194
221
|
}
|
|
195
222
|
|
|
@@ -197,6 +224,7 @@ export class Context<P extends Params = Params> {
|
|
|
197
224
|
* @param Layout `Layout`(s) for the `Page` to be passed into before being injected into the `<body>`
|
|
198
225
|
*/
|
|
199
226
|
layout(...Layout: Layout[]) {
|
|
227
|
+
if (this.#finalized) throw new ResponseFinalized("layout");
|
|
200
228
|
this.#layouts.push(...Layout);
|
|
201
229
|
}
|
|
202
230
|
|
|
@@ -252,7 +280,7 @@ export class Context<P extends Params = Params> {
|
|
|
252
280
|
|
|
253
281
|
this.html(
|
|
254
282
|
new ReadableStream<Uint8Array>({
|
|
255
|
-
|
|
283
|
+
async pull(c) {
|
|
256
284
|
result = await gen.next();
|
|
257
285
|
|
|
258
286
|
if (result.done) {
|
|
@@ -301,12 +329,6 @@ export class Context<P extends Params = Params> {
|
|
|
301
329
|
* @returns the constructed `Response`
|
|
302
330
|
*/
|
|
303
331
|
build() {
|
|
304
|
-
if (this.req.signal.aborted) {
|
|
305
|
-
// user cancelled the request
|
|
306
|
-
this.body = null;
|
|
307
|
-
this.status = 408;
|
|
308
|
-
}
|
|
309
|
-
|
|
310
332
|
if (
|
|
311
333
|
(!this.status || this.status === 404) &&
|
|
312
334
|
this.#trailingSlash !== "ignore"
|
|
@@ -328,16 +350,9 @@ export class Context<P extends Params = Params> {
|
|
|
328
350
|
|
|
329
351
|
if (!this.body && !this.status) this.notFound(this);
|
|
330
352
|
|
|
331
|
-
|
|
332
|
-
}
|
|
353
|
+
this.#finalized = true;
|
|
333
354
|
|
|
334
|
-
|
|
335
|
-
* Check if the `Response` has been set and the body has been read.
|
|
336
|
-
*
|
|
337
|
-
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response/bodyUsed)
|
|
338
|
-
*/
|
|
339
|
-
get #bodyUsed() {
|
|
340
|
-
return Boolean(this.#response?.bodyUsed);
|
|
355
|
+
return new Response(this.body, this);
|
|
341
356
|
}
|
|
342
357
|
|
|
343
358
|
/**
|