ovr 2.1.0 → 3.0.1

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.
Files changed (67) hide show
  1. package/dist/app/action.d.ts +2 -1
  2. package/dist/app/action.d.ts.map +1 -0
  3. package/dist/app/action.js +1 -1
  4. package/dist/app/context.d.ts +1 -0
  5. package/dist/app/context.d.ts.map +1 -0
  6. package/dist/app/context.js +9 -4
  7. package/dist/app/index.d.ts +1 -0
  8. package/dist/app/index.d.ts.map +1 -0
  9. package/dist/app/index.js +1 -1
  10. package/dist/{memo → app/memo}/index.d.ts +1 -0
  11. package/dist/app/memo/index.d.ts.map +1 -0
  12. package/dist/{mw → app/mw}/csrf.d.ts +2 -1
  13. package/dist/app/mw/csrf.d.ts.map +1 -0
  14. package/dist/app/page.d.ts +2 -1
  15. package/dist/app/page.d.ts.map +1 -0
  16. package/dist/index.d.ts +3 -2
  17. package/dist/index.d.ts.map +1 -0
  18. package/dist/index.js +2 -2
  19. package/dist/jsx/chunk/index.d.ts +29 -0
  20. package/dist/jsx/chunk/index.d.ts.map +1 -0
  21. package/dist/jsx/chunk/index.js +61 -0
  22. package/dist/jsx/index.d.ts +17 -12
  23. package/dist/jsx/index.d.ts.map +1 -0
  24. package/dist/jsx/index.js +95 -79
  25. package/dist/jsx/{elements.d.ts → intrinsic-elements.d.ts} +2 -1
  26. package/dist/jsx/intrinsic-elements.d.ts.map +1 -0
  27. package/dist/jsx/{merge-async-iterables.d.ts → merge-async-generators.d.ts} +1 -0
  28. package/dist/jsx/merge-async-generators.d.ts.map +1 -0
  29. package/dist/jsx-dev-runtime.d.ts +1 -0
  30. package/dist/jsx-dev-runtime.d.ts.map +1 -0
  31. package/dist/jsx-runtime.d.ts +1 -0
  32. package/dist/jsx-runtime.d.ts.map +1 -0
  33. package/dist/trie/index.d.ts +1 -0
  34. package/dist/trie/index.d.ts.map +1 -0
  35. package/dist/trie/insert-params.d.ts +1 -0
  36. package/dist/trie/insert-params.d.ts.map +1 -0
  37. package/dist/types/index.d.ts +1 -0
  38. package/dist/types/index.d.ts.map +1 -0
  39. package/dist/util/hash.d.ts +11 -0
  40. package/dist/util/hash.d.ts.map +1 -0
  41. package/dist/util/hash.js +16 -0
  42. package/package.json +4 -2
  43. package/src/app/action.ts +81 -0
  44. package/src/app/context.ts +329 -0
  45. package/src/app/index.ts +302 -0
  46. package/src/app/memo/index.ts +40 -0
  47. package/src/app/mw/csrf.ts +28 -0
  48. package/src/app/page.ts +56 -0
  49. package/src/index.ts +9 -0
  50. package/src/jsx/chunk/index.ts +70 -0
  51. package/src/jsx/index.ts +235 -0
  52. package/src/jsx/intrinsic-elements.ts +812 -0
  53. package/src/jsx/merge-async-generators.ts +42 -0
  54. package/src/jsx-dev-runtime.ts +1 -0
  55. package/src/jsx-runtime.ts +1 -0
  56. package/src/trie/index.ts +335 -0
  57. package/src/trie/insert-params.ts +31 -0
  58. package/src/types/index.ts +23 -0
  59. package/src/util/hash.ts +17 -0
  60. package/dist/escape/index.d.ts +0 -8
  61. package/dist/escape/index.js +0 -33
  62. package/dist/hash/index.d.ts +0 -1
  63. package/dist/hash/index.js +0 -9
  64. /package/dist/{memo → app/memo}/index.js +0 -0
  65. /package/dist/{mw → app/mw}/csrf.js +0 -0
  66. /package/dist/jsx/{elements.js → intrinsic-elements.js} +0 -0
  67. /package/dist/jsx/{merge-async-iterables.js → merge-async-generators.js} +0 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"merge-async-generators.d.ts","sourceRoot":"","sources":["../../src/jsx/merge-async-generators.ts"],"names":[],"mappings":"AAOA;;;;;;;;;GASG;AACH,wBAAuB,KAAK,CAAC,CAAC,EAAE,UAAU,EAAE,cAAc,CAAC,CAAC,EAAE,IAAI,CAAC,EAAE;;;kBAwBpE"}
@@ -1 +1,2 @@
1
1
  export { jsx as jsxDEV, Fragment, type JSX } from "./jsx/index.js";
2
+ //# sourceMappingURL=jsx-dev-runtime.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jsx-dev-runtime.d.ts","sourceRoot":"","sources":["../src/jsx-dev-runtime.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,IAAI,MAAM,EAAE,QAAQ,EAAE,KAAK,GAAG,EAAE,MAAM,gBAAgB,CAAC"}
@@ -1 +1,2 @@
1
1
  export { jsx, jsx as jsxs, Fragment, type JSX } from "./jsx/index.js";
2
+ //# sourceMappingURL=jsx-runtime.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jsx-runtime.d.ts","sourceRoot":"","sources":["../src/jsx-runtime.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,GAAG,IAAI,IAAI,EAAE,QAAQ,EAAE,KAAK,GAAG,EAAE,MAAM,gBAAgB,CAAC"}
@@ -74,3 +74,4 @@ export declare class Trie<Store> {
74
74
  } | null;
75
75
  }
76
76
  export {};
77
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/trie/index.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAE5C,qBAAa,KAAK,CAAC,KAAK;IACvB,oBAAoB;IACpB,OAAO,EAAE,MAAM,CAAC;IAEhB,yCAAyC;IACzC,KAAK,EAAE,KAAK,CAAC;gBAED,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK;CAUzC;AAED,cAAM,SAAS,CAAC,KAAK;IACpB,oDAAoD;IACpD,IAAI,EAAE,MAAM,CAAC;IAEb,oBAAoB;IACpB,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,GAAG,IAAI,CAAQ;IAElC,wBAAwB;IACxB,WAAW,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,IAAI,CAAQ;gBAE3B,IAAI,EAAE,MAAM;CAGxB;AAED,qBAAa,IAAI,CAAC,KAAK;IACtB,yCAAyC;IACzC,OAAO,EAAE,MAAM,CAAC;IAEhB,uEAAuE;IACvE,SAAS,EAAE,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,IAAI,CAAQ;IAElD,4BAA4B;IAC5B,UAAU,EAAE,SAAS,CAAC,KAAK,CAAC,GAAG,IAAI,CAAQ;IAE3C,oBAAoB;IACpB,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,GAAG,IAAI,CAAQ;IAElC,6BAA6B;IAC7B,aAAa,EAAE,KAAK,CAAC,KAAK,CAAC,GAAG,IAAI,CAAQ;IAE1C;;;OAGG;gBACS,OAAO,SAAM,EAAE,cAAc,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,EAAE;IAYzD;;;OAGG;IACH,KAAK,CAAC,OAAO,EAAE,MAAM;IAWrB;;;;;;;;OAQG;IACH,IAAI,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;IAavC;;;;;;OAMG;IACH,KAAK,CAAC,OAAO,EAAE,MAAM;IAMrB;;;OAGG;IACH,aAAa,CAAC,IAAI,EAAE,MAAM;IAW1B;;;OAGG;IACH,GAAG,CAAC,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC;IAiHvB;;;OAGG;IACH,IAAI,CAAC,QAAQ,EAAE,MAAM,GAAG;QAAE,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI;CAgFtE"}
@@ -5,3 +5,4 @@ import type { Params } from "./index.js";
5
5
  * @returns Resolved pathname with params
6
6
  */
7
7
  export declare const insertParams: (parts: string[], params: Params) => string;
8
+ //# sourceMappingURL=insert-params.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"insert-params.d.ts","sourceRoot":"","sources":["../../src/trie/insert-params.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAEzC;;;;GAIG;AACH,eAAO,MAAM,YAAY,GAAI,OAAO,MAAM,EAAE,EAAE,QAAQ,MAAM,WAuB3D,CAAC"}
@@ -12,3 +12,4 @@ export type ExtractMultiParams<Patterns extends string[]> = Patterns extends [
12
12
  infer First extends string,
13
13
  ...infer Rest extends string[]
14
14
  ] ? Rest["length"] extends 0 ? ExtractParams<First> : ExtractParams<First> | ExtractMultiParams<Rest> : never;
15
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;AAE7C,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;AAE7C,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC;AAE9C,MAAM,MAAM,aAAa,CAAC,OAAO,SAAS,MAAM,GAAG,MAAM,IACxD,OAAO,SAAS,GAAG,MAAM,MAAM,IAAI,MAAM,KAAK,IAAI,MAAM,IAAI,EAAE,GAC3D;KAAG,CAAC,IAAI,KAAK,GAAG,MAAM,aAAa,CAAC,IAAI,CAAC,GAAG,MAAM;CAAE,GACpD,OAAO,SAAS,GAAG,MAAM,MAAM,IAAI,MAAM,KAAK,EAAE,GAC/C;KAAG,CAAC,IAAI,KAAK,GAAG,MAAM;CAAE,GACxB,OAAO,SAAS,GAAG,MAAM,KAAK,GAAG,GAChC;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,GACf,EAAE,CAAC;AAET,MAAM,MAAM,kBAAkB,CAAC,QAAQ,SAAS,MAAM,EAAE,IAAI,QAAQ,SAAS;IAC5E,MAAM,KAAK,SAAS,MAAM;IAC1B,GAAG,MAAM,IAAI,SAAS,MAAM,EAAE;CAC9B,GACE,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,GACvB,aAAa,CAAC,KAAK,CAAC,GACpB,aAAa,CAAC,KAAK,CAAC,GAAG,kBAAkB,CAAC,IAAI,CAAC,GAChD,KAAK,CAAC"}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Fast hashing algorithm - [djb2](http://www.cse.yorku.ca/~oz/hash.html)
3
+ *
4
+ * Adapted from
5
+ * [SvelteKit](https://github.com/sveltejs/kit/blob/25d459104814b0c2dc6b4cf73b680378a29d8200/packages/kit/src/runtime/hash.js)
6
+ *
7
+ * @param s String to hash
8
+ * @returns Hashed string
9
+ */
10
+ export declare const hash: (s: string) => string;
11
+ //# sourceMappingURL=hash.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["../../src/util/hash.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,eAAO,MAAM,IAAI,GAAI,GAAG,MAAM,WAO7B,CAAC"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Fast hashing algorithm - [djb2](http://www.cse.yorku.ca/~oz/hash.html)
3
+ *
4
+ * Adapted from
5
+ * [SvelteKit](https://github.com/sveltejs/kit/blob/25d459104814b0c2dc6b4cf73b680378a29d8200/packages/kit/src/runtime/hash.js)
6
+ *
7
+ * @param s String to hash
8
+ * @returns Hashed string
9
+ */
10
+ export const hash = (s) => {
11
+ let hash = 5381;
12
+ let i = s.length;
13
+ while (i)
14
+ hash = (hash * 33) ^ s.charCodeAt(--i);
15
+ return (hash >>> 0).toString(36);
16
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "ovr",
3
3
  "description": "Progressive HTML Rendering",
4
- "version": "2.1.0",
4
+ "version": "3.0.1",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
@@ -33,7 +33,9 @@
33
33
  }
34
34
  },
35
35
  "files": [
36
- "dist"
36
+ "dist",
37
+ "src",
38
+ "!src/**/*.test.ts"
37
39
  ],
38
40
  "scripts": {
39
41
  "check": "tsc --noEmit",
@@ -0,0 +1,81 @@
1
+ import { type JSX, jsx } from "../jsx/index.js";
2
+ import { insertParams } from "../trie/insert-params.js";
3
+ import type { ExtractParams } from "../types/index.js";
4
+ import { hash } from "../util/hash.js";
5
+ import type { Middleware, Params } from "./index.js";
6
+
7
+ type FormProps<P extends Params> = JSX.IntrinsicElements["form"] &
8
+ (keyof P extends never ? { params?: never } : { params: P });
9
+
10
+ export class Action<Pattern extends string = string> {
11
+ /** Route pattern */
12
+ pattern: Pattern;
13
+
14
+ /** POST middleware */
15
+ middleware: Middleware<any>[];
16
+
17
+ /** `<form>` component with preset `method` and `action` attributes. */
18
+ Form: (props: FormProps<ExtractParams<Pattern>>) => JSX.Element;
19
+
20
+ #parts: string[] = [];
21
+
22
+ /**
23
+ * @param middleware POST middleware
24
+ *
25
+ * @example
26
+ *
27
+ * ```tsx
28
+ * // action.tsx
29
+ * import { Action } from "ovr";
30
+ *
31
+ * export const action = new Action((c) => {
32
+ * console.log("posted");
33
+ * c.redirect("/", 303);
34
+ * });
35
+ *
36
+ * export const page = new Page("/", () => (
37
+ * <action.Form>
38
+ * <input />
39
+ * <button>Submit</button>
40
+ * </action.Form>
41
+ * ));
42
+ *
43
+ * // app.tsx
44
+ * app.add(page, action); registers the action
45
+ * ```
46
+ */
47
+ constructor(...middleware: Middleware<{}>[]);
48
+ /**
49
+ * @param pattern Route pattern
50
+ * @param middleware POST middleware
51
+ */
52
+ constructor(
53
+ pattern: Pattern,
54
+ ...middleware: Middleware<ExtractParams<Pattern>>[]
55
+ );
56
+ constructor(
57
+ patternOrMiddleware: Pattern | Middleware<ExtractParams<Pattern>>,
58
+ ...middleware: Middleware<ExtractParams<Pattern>>[]
59
+ ) {
60
+ this.middleware = middleware;
61
+
62
+ if (typeof patternOrMiddleware === "string") {
63
+ this.pattern = patternOrMiddleware;
64
+ } else {
65
+ this.middleware.unshift(patternOrMiddleware);
66
+ this.pattern = `/_action/${hash(this.middleware.join())}` as Pattern;
67
+ }
68
+
69
+ this.#parts = this.pattern.split("/");
70
+
71
+ this.Form = (props) => {
72
+ const { params = {}, ...rest } = props;
73
+
74
+ return jsx("form", {
75
+ action: insertParams(this.#parts, params),
76
+ method: "post",
77
+ ...rest,
78
+ });
79
+ };
80
+ }
81
+ }
@@ -0,0 +1,329 @@
1
+ import { Chunk } from "../jsx/chunk/index.js";
2
+ import { type JSX, toGenerator } from "../jsx/index.js";
3
+ import type { Route } from "../trie/index.js";
4
+ import { hash } from "../util/hash.js";
5
+ import {
6
+ App,
7
+ type ErrorHandler,
8
+ type Middleware,
9
+ type NotFoundHandler,
10
+ type Params,
11
+ type TrailingSlash,
12
+ } from "./index.js";
13
+ import { Memo } from "./memo/index.js";
14
+
15
+ type Layout = (props: { children: JSX.Element }) => JSX.Element;
16
+
17
+ class TagNotFound extends Error {
18
+ constructor(tag: string) {
19
+ super(`No closing ${tag} tag found`);
20
+ this.name = "TagNotFound";
21
+ }
22
+ }
23
+
24
+ export class Context<P extends Params = Params> {
25
+ /** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Request) */
26
+ req: Request;
27
+
28
+ /** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/URL) */
29
+ url: URL;
30
+
31
+ /**
32
+ * Route pattern parameters
33
+ *
34
+ * Given the route pattern `/posts/:slug` is added, a request made to
35
+ * `/posts/my-post` creates a `params` object `{ slug: "my-post" }`.
36
+ *
37
+ * @example { slug: "my-post" }
38
+ */
39
+ params!: P; // set after match
40
+
41
+ /** The matched `Route` instance. */
42
+ route!: Route<Middleware<P>[]>; // set after match
43
+
44
+ /** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response#body) */
45
+ body: BodyInit | null = null;
46
+
47
+ /** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status) */
48
+ status?: number;
49
+
50
+ /** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Headers) */
51
+ headers = new Headers();
52
+
53
+ #layouts: Layout[] = [];
54
+
55
+ #headElements: JSX.Element[] = [];
56
+
57
+ #trailingSlash: TrailingSlash;
58
+
59
+ /** Passed from `app.base` */
60
+ base: string;
61
+
62
+ /** Passed from `app.error` */
63
+ error: ErrorHandler<P>;
64
+
65
+ /** Passed from `app.notFound` */
66
+ notFound: NotFoundHandler<P>;
67
+
68
+ #memo = new Memo();
69
+
70
+ memo = this.#memo.use;
71
+
72
+ constructor(
73
+ req: Request,
74
+ url: URL,
75
+ trailingSlash: TrailingSlash,
76
+ base: string,
77
+ error: ErrorHandler<P>,
78
+ notFound: NotFoundHandler<P>,
79
+ ) {
80
+ this.req = req;
81
+ this.url = url;
82
+ this.#trailingSlash = trailingSlash;
83
+ this.base = base;
84
+ this.error = error;
85
+ this.notFound = notFound;
86
+ }
87
+
88
+ /**
89
+ * Mirrors `new Response()` constructor, set values with one function.
90
+ *
91
+ * @param body Response BodyInit
92
+ * @param init Enhanced ResponseInit
93
+ */
94
+ res(
95
+ body: BodyInit | null,
96
+ init?: {
97
+ /**
98
+ * [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
99
+ *
100
+ * @default 200
101
+ */
102
+ status?: number;
103
+
104
+ /** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Headers) */
105
+ headers?: HeadersInit;
106
+ },
107
+ ) {
108
+ this.body = body;
109
+ this.status = init?.status ?? 200;
110
+
111
+ if (init?.headers) {
112
+ for (const [name, value] of new Headers(init.headers)) {
113
+ this.headers.set(name, value);
114
+ }
115
+ }
116
+ }
117
+
118
+ /**
119
+ * Creates an HTML response.
120
+ *
121
+ * @param body HTML body
122
+ * @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
123
+ */
124
+ html(body: BodyInit | null, status?: number) {
125
+ this.res(body, {
126
+ status,
127
+ headers: { "content-type": "text/html; charset=utf-8" },
128
+ });
129
+ }
130
+
131
+ /**
132
+ * Creates a JSON response.
133
+ *
134
+ * @param data passed into JSON.stringify to create the body
135
+ * @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
136
+ */
137
+ json(data: any, status?: number) {
138
+ this.res(JSON.stringify(data), {
139
+ status,
140
+ headers: { "content-type": "application/json" },
141
+ });
142
+ }
143
+
144
+ /**
145
+ * Creates a plain text response.
146
+ *
147
+ * @param body response body
148
+ * @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
149
+ */
150
+ text(body: BodyInit, status?: number) {
151
+ this.res(body, {
152
+ status,
153
+ headers: { "content-type": "text/plain; charset=utf-8" },
154
+ });
155
+ }
156
+
157
+ /**
158
+ * Creates a redirect response.
159
+ *
160
+ * @param location redirect `Location` header
161
+ * @param status HTTP status code
162
+ *
163
+ * - [301 Moved Permanently](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/301)
164
+ * - [302 Found](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/302) (default)
165
+ * - [303 See Other](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/303)
166
+ * - [307 Temporary Redirect](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/307)
167
+ * - [308 Permanent Redirect](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/308)
168
+ */
169
+ redirect(location: string | URL, status: 301 | 302 | 303 | 307 | 308 = 302) {
170
+ this.headers.set("location", location.toString());
171
+ this.status = status;
172
+ }
173
+
174
+ /**
175
+ * @param Element `JSX.Element` to add to the head
176
+ */
177
+ head(...Element: JSX.Element[]) {
178
+ this.#headElements.push(...Element);
179
+ }
180
+
181
+ /**
182
+ * @param Layout `Layout`(s) for the `Page` to be passed into before being injected into the `<body>`
183
+ */
184
+ layout(...Layout: Layout[]) {
185
+ this.#layouts.push(...Layout);
186
+ }
187
+
188
+ /**
189
+ * Creates an HTML response based on the `head` elements, `Layout`(s), and `Page` provided.
190
+ *
191
+ * @param Page `JSX.Element` to inject into the `<body>`
192
+ * @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
193
+ */
194
+ page(Page: JSX.Element, status?: number) {
195
+ for (let i = this.#layouts.length - 1; i >= 0; i--)
196
+ Page = this.#layouts[i]!({ children: Page });
197
+
198
+ const headClose = "</head>";
199
+ const bodyClose = "</body>";
200
+
201
+ const elements: JSX.Element[] = this.base.split(headClose);
202
+ if (!elements[1]) throw new TagNotFound(headClose);
203
+
204
+ elements.splice(1, 0, this.#headElements, headClose);
205
+
206
+ const bodyParts = (elements[3] as string).split(bodyClose);
207
+ if (!bodyParts[1]) throw new TagNotFound(bodyClose);
208
+
209
+ const userAgent = this.req.headers.get("user-agent");
210
+ if (userAgent?.includes("Safari") && !userAgent.includes("Chrome")) {
211
+ // https://bugs.webkit.org/show_bug.cgi?id=252413
212
+ // https://github.com/sveltejs/kit/issues/10315
213
+ // https://github.com/remix-run/remix/issues/5804
214
+ bodyParts[0] += `<div aria-hidden=true style=position:absolute;width:0;height:0;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap;border-width:0>${"\u200b".repeat(200)}</div>`;
215
+ }
216
+
217
+ elements[3] = bodyParts[0];
218
+ elements.push(Page, bodyClose + bodyParts[1]);
219
+
220
+ const gen = toGenerator(
221
+ elements.map((el) => {
222
+ if (typeof el === "string") return new Chunk(el, true);
223
+ return el;
224
+ }),
225
+ );
226
+
227
+ this.html(
228
+ new ReadableStream<string>({
229
+ async pull(c) {
230
+ const { value, done } = await gen.next();
231
+ if (done) c.close();
232
+ else c.enqueue(value?.value);
233
+ },
234
+
235
+ cancel() {
236
+ gen.return();
237
+ },
238
+ }).pipeThrough(new TextEncoderStream()),
239
+ status,
240
+ );
241
+ }
242
+
243
+ /**
244
+ * Generates an etag from a hash of the string provided.
245
+ * If the etag matches, sets the response to not modified.
246
+ *
247
+ * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag)
248
+ *
249
+ * @param s string to hash
250
+ * @returns `true` if the etag matches, `false` otherwise
251
+ */
252
+ etag(s: string) {
253
+ const etag = `"${hash(s)}"`;
254
+
255
+ this.headers.set("etag", etag);
256
+
257
+ if (this.req.headers.get("if-none-match") === etag) {
258
+ this.body = null;
259
+ this.status = 304;
260
+
261
+ return true;
262
+ }
263
+
264
+ return false;
265
+ }
266
+
267
+ /**
268
+ * @returns the constructed `Response`
269
+ */
270
+ build() {
271
+ if (
272
+ (!this.status || this.status === 404) &&
273
+ this.#trailingSlash !== "ignore"
274
+ ) {
275
+ const last = this.url.pathname.at(-1);
276
+
277
+ if (this.#trailingSlash === "always" && last !== "/") {
278
+ this.url.pathname += "/";
279
+ this.redirect(this.url, 308);
280
+ } else if (
281
+ this.#trailingSlash === "never" &&
282
+ this.url.pathname !== "/" &&
283
+ last === "/"
284
+ ) {
285
+ this.url.pathname = this.url.pathname.slice(0, -1);
286
+ this.redirect(this.url, 308);
287
+ }
288
+ }
289
+
290
+ if (!this.body && !this.status) this.notFound(this);
291
+
292
+ return new Response(this.body, this);
293
+ }
294
+
295
+ /**
296
+ * Call within the scope of a handler to get the current context.
297
+ *
298
+ * @returns Request context
299
+ *
300
+ * @example
301
+ *
302
+ * ```ts
303
+ * import { Context } from "ovr";
304
+ *
305
+ * const app = new Router();
306
+ *
307
+ * const fn = () => {
308
+ * const c = Context.get();
309
+ * // ...
310
+ * }
311
+ *
312
+ * app.get("/", () => {
313
+ * fn(); // OK
314
+ * });
315
+ *
316
+ * fn() // ReferenceError - outside AsyncLocalStorage scope
317
+ * ```
318
+ */
319
+ static get() {
320
+ const c = App.storage.getStore();
321
+
322
+ if (!c)
323
+ throw new ReferenceError(
324
+ "Context can only be obtained within a handler.",
325
+ );
326
+
327
+ return c;
328
+ }
329
+ }