ovr 4.2.3 → 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 +50 -14
- package/dist/jsx/chunk/index.d.ts +4 -4
- package/dist/jsx/chunk/index.d.ts.map +1 -1
- package/dist/jsx/chunk/index.js +11 -13
- package/package.json +1 -1
- package/src/app/context.ts +54 -16
- package/src/jsx/chunk/index.ts +12 -13
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;IAM/B,gFAAgF;IAChF,OAAO,UAAiB;IAUxB,8BAA8B;IAC9B,KAAK,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC;IAEvB,iCAAiC;IACjC,QAAQ,EAAE,eAAe,CAAC,CAAC,CAAC,CAAC;IAK7B,IAAI,mEAAkB;gBASrB,GAAG,EAAE,OAAO,EACZ,GAAG,EAAE,GAAG,EACR,aAAa,EAAE,aAAa,EAC5B,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,YAAY,CAAC,CAAC,CAAC,EACtB,QAAQ,EAAE,eAAe,CAAC,CAAC,CAAC;IAU7B,gFAAgF;IAChF,IAAI,MAAM,uBAET;IACD,IAAI,MAAM,CAAC,MAAM,oBAAA,EAGhB;IAED,+FAA+F;IAC/F,IAAI,IAAI,oBAEP;IACD,IAAI,IAAI,CAAC,IAAI,iBAAA,EAGZ;IAED,6BAA6B;IAC7B,IAAI,IAAI,WAEP;IACD,IAAI,IAAI,CAAC,IAAI,QAAA,EAGZ;IAED;;;;;OAKG;IACH,GAAG,CACF,IAAI,EAAE,QAAQ,GAAG,IAAI,EACrB,IAAI,CAAC,EAAE;QACN;;;;WAIG;QACH,MAAM,CAAC,EAAE,MAAM,CAAC;QAEhB,gFAAgF;QAChF,OAAO,CAAC,EAAE,WAAW,CAAC;KACtB;IAYF;;;;;OAKG;IACH,IAAI,CAAC,IAAI,EAAE,QAAQ,GAAG,IAAI,EAAE,MAAM,CAAC,EAAE,MAAM;IAO3C;;;;;OAKG;IACH,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,MAAM;IAO/B;;;;;OAKG;IACH,IAAI,CAAC,IAAI,EAAE,QAAQ,EAAE,MAAM,CAAC,EAAE,MAAM;IAOpC;;;;;;;;;;;OAWG;IACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,GAAG,EAAE,MAAM,GAAE,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAS;IAK1E;;OAEG;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,32 +33,60 @@ 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` */
|
|
42
47
|
notFound;
|
|
48
|
+
/** `Memo` unique to the current `Request` context */
|
|
43
49
|
#memo = new Memo();
|
|
44
50
|
memo = this.#memo.use;
|
|
51
|
+
/** Check if the `Response` has been built already */
|
|
52
|
+
#finalized = false;
|
|
53
|
+
/** Used across requests */
|
|
45
54
|
static #encoder = new TextEncoder();
|
|
46
55
|
constructor(req, url, trailingSlash, base, error, notFound) {
|
|
47
56
|
this.req = req;
|
|
48
57
|
this.url = url;
|
|
49
58
|
this.#trailingSlash = trailingSlash;
|
|
50
|
-
this
|
|
59
|
+
this.#base = base;
|
|
51
60
|
this.error = error;
|
|
52
61
|
this.notFound = notFound;
|
|
53
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
|
+
}
|
|
54
90
|
/**
|
|
55
91
|
* Mirrors `new Response()` constructor, set values with one function.
|
|
56
92
|
*
|
|
@@ -122,12 +158,16 @@ export class Context {
|
|
|
122
158
|
* @param Element `JSX.Element` to add to the head
|
|
123
159
|
*/
|
|
124
160
|
head(...Element) {
|
|
161
|
+
if (this.#finalized)
|
|
162
|
+
throw new ResponseFinalized("head");
|
|
125
163
|
this.#headElements.push(...Element);
|
|
126
164
|
}
|
|
127
165
|
/**
|
|
128
166
|
* @param Layout `Layout`(s) for the `Page` to be passed into before being injected into the `<body>`
|
|
129
167
|
*/
|
|
130
168
|
layout(...Layout) {
|
|
169
|
+
if (this.#finalized)
|
|
170
|
+
throw new ResponseFinalized("layout");
|
|
131
171
|
this.#layouts.push(...Layout);
|
|
132
172
|
}
|
|
133
173
|
/**
|
|
@@ -173,7 +213,7 @@ export class Context {
|
|
|
173
213
|
}
|
|
174
214
|
let result;
|
|
175
215
|
this.html(new ReadableStream({
|
|
176
|
-
|
|
216
|
+
async pull(c) {
|
|
177
217
|
result = await gen.next();
|
|
178
218
|
if (result.done) {
|
|
179
219
|
c.close();
|
|
@@ -211,11 +251,6 @@ export class Context {
|
|
|
211
251
|
* @returns the constructed `Response`
|
|
212
252
|
*/
|
|
213
253
|
build() {
|
|
214
|
-
if (this.req.signal.aborted) {
|
|
215
|
-
// user cancelled the request
|
|
216
|
-
this.body = null;
|
|
217
|
-
this.status = 408;
|
|
218
|
-
}
|
|
219
254
|
if ((!this.status || this.status === 404) &&
|
|
220
255
|
this.#trailingSlash !== "ignore") {
|
|
221
256
|
const last = this.url.pathname.at(-1);
|
|
@@ -232,6 +267,7 @@ export class Context {
|
|
|
232
267
|
}
|
|
233
268
|
if (!this.body && !this.status)
|
|
234
269
|
this.notFound(this);
|
|
270
|
+
this.#finalized = true;
|
|
235
271
|
return new Response(this.body, this);
|
|
236
272
|
}
|
|
237
273
|
/**
|
|
@@ -5,10 +5,10 @@ export declare class Chunk {
|
|
|
5
5
|
/**
|
|
6
6
|
* `Chunk` to send in an HTML stream.
|
|
7
7
|
*
|
|
8
|
-
* @param
|
|
8
|
+
* @param html string of HTML to escape
|
|
9
9
|
* @param safe Set to `true` if the HTML is safe and should not be escaped
|
|
10
10
|
*/
|
|
11
|
-
constructor(
|
|
11
|
+
constructor(html: unknown, safe?: boolean);
|
|
12
12
|
/**
|
|
13
13
|
* @returns `value`
|
|
14
14
|
*/
|
|
@@ -20,10 +20,10 @@ export declare class Chunk {
|
|
|
20
20
|
/**
|
|
21
21
|
* Escapes strings of HTML.
|
|
22
22
|
*
|
|
23
|
-
* @param
|
|
23
|
+
* @param html String to escape
|
|
24
24
|
* @param attr Set to `true` if the value is an attribute, otherwise it's a string of HTML content
|
|
25
25
|
* @returns Escaped string of HTML
|
|
26
26
|
*/
|
|
27
|
-
static escape(
|
|
27
|
+
static escape(html: string, attr?: boolean): string;
|
|
28
28
|
}
|
|
29
29
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/jsx/chunk/index.ts"],"names":[],"mappings":"AAAA,qBAAa,KAAK;;IAIjB,2BAA2B;IAC3B,KAAK,EAAE,MAAM,CAAC;IAEd;;;;;OAKG;gBACS,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/jsx/chunk/index.ts"],"names":[],"mappings":"AAAA,qBAAa,KAAK;;IAIjB,2BAA2B;IAC3B,KAAK,EAAE,MAAM,CAAC;IAEd;;;;;OAKG;gBACS,IAAI,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,OAAO;IAKzC;;OAEG;IACH,QAAQ;IAIR;;OAEG;IACH,MAAM,CAAC,GAAG,MAAM,EAAE,KAAK,EAAE;IAIzB;;;;;;OAMG;IACH,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO;CA2B1C"}
|
package/dist/jsx/chunk/index.js
CHANGED
|
@@ -6,11 +6,11 @@ export class Chunk {
|
|
|
6
6
|
/**
|
|
7
7
|
* `Chunk` to send in an HTML stream.
|
|
8
8
|
*
|
|
9
|
-
* @param
|
|
9
|
+
* @param html string of HTML to escape
|
|
10
10
|
* @param safe Set to `true` if the HTML is safe and should not be escaped
|
|
11
11
|
*/
|
|
12
|
-
constructor(
|
|
13
|
-
const value = String(
|
|
12
|
+
constructor(html, safe) {
|
|
13
|
+
const value = String(html ?? "");
|
|
14
14
|
this.value = safe ? value : Chunk.escape(value);
|
|
15
15
|
}
|
|
16
16
|
/**
|
|
@@ -28,11 +28,11 @@ export class Chunk {
|
|
|
28
28
|
/**
|
|
29
29
|
* Escapes strings of HTML.
|
|
30
30
|
*
|
|
31
|
-
* @param
|
|
31
|
+
* @param html String to escape
|
|
32
32
|
* @param attr Set to `true` if the value is an attribute, otherwise it's a string of HTML content
|
|
33
33
|
* @returns Escaped string of HTML
|
|
34
34
|
*/
|
|
35
|
-
static escape(
|
|
35
|
+
static escape(html, attr) {
|
|
36
36
|
// adapted from https://github.com/sveltejs/svelte/blob/main/packages/svelte/src/escaping.js
|
|
37
37
|
const regex = attr ? Chunk.#attr : Chunk.#content;
|
|
38
38
|
// search starts at beginning of the string
|
|
@@ -40,21 +40,19 @@ export class Chunk {
|
|
|
40
40
|
// tracks the position of the last successful match in the input string
|
|
41
41
|
let start = 0;
|
|
42
42
|
let result = "";
|
|
43
|
-
// index of the match
|
|
44
|
-
let i;
|
|
45
|
-
let match;
|
|
46
43
|
// since `g` flag is used, regex maintains state with each loop,
|
|
47
44
|
// checking from the lastIndex onward each time
|
|
48
|
-
while (regex.test(
|
|
49
|
-
|
|
50
|
-
|
|
45
|
+
while (regex.test(html)) {
|
|
46
|
+
// index of the match
|
|
47
|
+
const i = regex.lastIndex - 1;
|
|
48
|
+
const match = html[i];
|
|
51
49
|
result +=
|
|
52
50
|
// everything that didn't match during this test
|
|
53
|
-
|
|
51
|
+
html.slice(start, i) +
|
|
54
52
|
// replacement
|
|
55
53
|
(match === "&" ? "&" : match === '"' ? """ : "<");
|
|
56
54
|
start = regex.lastIndex;
|
|
57
55
|
}
|
|
58
|
-
return result +
|
|
56
|
+
return result + html.slice(start);
|
|
59
57
|
}
|
|
60
58
|
}
|
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>;
|
|
@@ -64,11 +72,16 @@ export class Context<P extends Params = Params> {
|
|
|
64
72
|
/** Passed from `app.notFound` */
|
|
65
73
|
notFound: NotFoundHandler<P>;
|
|
66
74
|
|
|
75
|
+
/** `Memo` unique to the current `Request` context */
|
|
67
76
|
#memo = new Memo();
|
|
68
77
|
|
|
69
78
|
memo = this.#memo.use;
|
|
70
79
|
|
|
71
|
-
|
|
80
|
+
/** Check if the `Response` has been built already */
|
|
81
|
+
#finalized = false;
|
|
82
|
+
|
|
83
|
+
/** Used across requests */
|
|
84
|
+
static readonly #encoder = new TextEncoder();
|
|
72
85
|
|
|
73
86
|
constructor(
|
|
74
87
|
req: Request,
|
|
@@ -81,11 +94,38 @@ export class Context<P extends Params = Params> {
|
|
|
81
94
|
this.req = req;
|
|
82
95
|
this.url = url;
|
|
83
96
|
this.#trailingSlash = trailingSlash;
|
|
84
|
-
this
|
|
97
|
+
this.#base = base;
|
|
85
98
|
this.error = error;
|
|
86
99
|
this.notFound = notFound;
|
|
87
100
|
}
|
|
88
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
|
+
|
|
89
129
|
/**
|
|
90
130
|
* Mirrors `new Response()` constructor, set values with one function.
|
|
91
131
|
*
|
|
@@ -176,6 +216,7 @@ export class Context<P extends Params = Params> {
|
|
|
176
216
|
* @param Element `JSX.Element` to add to the head
|
|
177
217
|
*/
|
|
178
218
|
head(...Element: JSX.Element[]) {
|
|
219
|
+
if (this.#finalized) throw new ResponseFinalized("head");
|
|
179
220
|
this.#headElements.push(...Element);
|
|
180
221
|
}
|
|
181
222
|
|
|
@@ -183,6 +224,7 @@ export class Context<P extends Params = Params> {
|
|
|
183
224
|
* @param Layout `Layout`(s) for the `Page` to be passed into before being injected into the `<body>`
|
|
184
225
|
*/
|
|
185
226
|
layout(...Layout: Layout[]) {
|
|
227
|
+
if (this.#finalized) throw new ResponseFinalized("layout");
|
|
186
228
|
this.#layouts.push(...Layout);
|
|
187
229
|
}
|
|
188
230
|
|
|
@@ -238,7 +280,7 @@ export class Context<P extends Params = Params> {
|
|
|
238
280
|
|
|
239
281
|
this.html(
|
|
240
282
|
new ReadableStream<Uint8Array>({
|
|
241
|
-
|
|
283
|
+
async pull(c) {
|
|
242
284
|
result = await gen.next();
|
|
243
285
|
|
|
244
286
|
if (result.done) {
|
|
@@ -287,12 +329,6 @@ export class Context<P extends Params = Params> {
|
|
|
287
329
|
* @returns the constructed `Response`
|
|
288
330
|
*/
|
|
289
331
|
build() {
|
|
290
|
-
if (this.req.signal.aborted) {
|
|
291
|
-
// user cancelled the request
|
|
292
|
-
this.body = null;
|
|
293
|
-
this.status = 408;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
332
|
if (
|
|
297
333
|
(!this.status || this.status === 404) &&
|
|
298
334
|
this.#trailingSlash !== "ignore"
|
|
@@ -314,6 +350,8 @@ export class Context<P extends Params = Params> {
|
|
|
314
350
|
|
|
315
351
|
if (!this.body && !this.status) this.notFound(this);
|
|
316
352
|
|
|
353
|
+
this.#finalized = true;
|
|
354
|
+
|
|
317
355
|
return new Response(this.body, this);
|
|
318
356
|
}
|
|
319
357
|
|
package/src/jsx/chunk/index.ts
CHANGED
|
@@ -8,11 +8,11 @@ export class Chunk {
|
|
|
8
8
|
/**
|
|
9
9
|
* `Chunk` to send in an HTML stream.
|
|
10
10
|
*
|
|
11
|
-
* @param
|
|
11
|
+
* @param html string of HTML to escape
|
|
12
12
|
* @param safe Set to `true` if the HTML is safe and should not be escaped
|
|
13
13
|
*/
|
|
14
|
-
constructor(
|
|
15
|
-
const value = String(
|
|
14
|
+
constructor(html: unknown, safe?: boolean) {
|
|
15
|
+
const value = String(html ?? "");
|
|
16
16
|
this.value = safe ? value : Chunk.escape(value);
|
|
17
17
|
}
|
|
18
18
|
|
|
@@ -33,11 +33,11 @@ export class Chunk {
|
|
|
33
33
|
/**
|
|
34
34
|
* Escapes strings of HTML.
|
|
35
35
|
*
|
|
36
|
-
* @param
|
|
36
|
+
* @param html String to escape
|
|
37
37
|
* @param attr Set to `true` if the value is an attribute, otherwise it's a string of HTML content
|
|
38
38
|
* @returns Escaped string of HTML
|
|
39
39
|
*/
|
|
40
|
-
static escape(
|
|
40
|
+
static escape(html: string, attr?: boolean) {
|
|
41
41
|
// adapted from https://github.com/sveltejs/svelte/blob/main/packages/svelte/src/escaping.js
|
|
42
42
|
const regex = attr ? Chunk.#attr : Chunk.#content;
|
|
43
43
|
|
|
@@ -46,23 +46,22 @@ export class Chunk {
|
|
|
46
46
|
// tracks the position of the last successful match in the input string
|
|
47
47
|
let start = 0;
|
|
48
48
|
let result = "";
|
|
49
|
-
// index of the match
|
|
50
|
-
let i: number;
|
|
51
|
-
let match: string | undefined;
|
|
52
49
|
|
|
53
50
|
// since `g` flag is used, regex maintains state with each loop,
|
|
54
51
|
// checking from the lastIndex onward each time
|
|
55
|
-
while (regex.test(
|
|
56
|
-
|
|
57
|
-
|
|
52
|
+
while (regex.test(html)) {
|
|
53
|
+
// index of the match
|
|
54
|
+
const i = regex.lastIndex - 1;
|
|
55
|
+
const match = html[i];
|
|
56
|
+
|
|
58
57
|
result +=
|
|
59
58
|
// everything that didn't match during this test
|
|
60
|
-
|
|
59
|
+
html.slice(start, i) +
|
|
61
60
|
// replacement
|
|
62
61
|
(match === "&" ? "&" : match === '"' ? """ : "<");
|
|
63
62
|
start = regex.lastIndex;
|
|
64
63
|
}
|
|
65
64
|
|
|
66
|
-
return result +
|
|
65
|
+
return result + html.slice(start);
|
|
67
66
|
}
|
|
68
67
|
}
|