ovr 0.0.1 → 0.0.2
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 +1 -1
- package/dist/escape/index.d.ts +8 -0
- package/dist/escape/index.js +33 -0
- package/dist/index.d.ts +5 -1
- package/dist/index.js +5 -1
- package/dist/jsx/elements.d.ts +562 -0
- package/dist/jsx/elements.js +1 -0
- package/dist/jsx/index.d.ts +46 -0
- package/dist/jsx/index.js +138 -0
- package/dist/jsx-dev-runtime.d.ts +1 -0
- package/dist/jsx-dev-runtime.js +1 -0
- package/dist/jsx-runtime.d.ts +1 -0
- package/dist/jsx-runtime.js +1 -0
- package/dist/router/context.d.ts +143 -0
- package/dist/router/context.js +222 -0
- package/dist/router/index.d.ts +92 -0
- package/dist/router/index.js +115 -0
- package/dist/trie/index.d.ts +75 -0
- package/dist/trie/index.js +265 -0
- package/dist/types.d.ts +1 -0
- package/dist/types.js +1 -0
- package/dist/util/merge-async-iterables.d.ts +12 -0
- package/dist/util/merge-async-iterables.js +39 -0
- package/package.json +18 -4
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { mergeAsyncIterables } from "../util/merge-async-iterables.js";
|
|
2
|
+
// https://developer.mozilla.org/en-US/docs/Glossary/Void_element#self-closing_tags
|
|
3
|
+
const voidElements = new Set([
|
|
4
|
+
"area",
|
|
5
|
+
"base",
|
|
6
|
+
"br",
|
|
7
|
+
"col",
|
|
8
|
+
"embed",
|
|
9
|
+
"hr",
|
|
10
|
+
"img",
|
|
11
|
+
"input",
|
|
12
|
+
"link",
|
|
13
|
+
"meta",
|
|
14
|
+
"source",
|
|
15
|
+
"track",
|
|
16
|
+
"wbr",
|
|
17
|
+
]);
|
|
18
|
+
/**
|
|
19
|
+
* The main function of the jsx transform cycle, each time jsx is encountered
|
|
20
|
+
* it is passed into this function to be resolved.
|
|
21
|
+
*
|
|
22
|
+
* @param tag string or function that refers to the component or element type
|
|
23
|
+
* @param props object containing all the properties and attributes passed to the element or component
|
|
24
|
+
* @returns an async generator that yields parts of HTML
|
|
25
|
+
*/
|
|
26
|
+
export const jsx = async function* (tag, props) {
|
|
27
|
+
if (typeof tag === "function") {
|
|
28
|
+
yield* toGenerator(tag(props));
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
// element
|
|
32
|
+
const { children, ...attrs } = props;
|
|
33
|
+
let attrStr = "";
|
|
34
|
+
for (let key in attrs) {
|
|
35
|
+
const value = attrs[key];
|
|
36
|
+
if (key === "className")
|
|
37
|
+
key = "class";
|
|
38
|
+
else if (key === "htmlFor")
|
|
39
|
+
key = "for";
|
|
40
|
+
if (value === true) {
|
|
41
|
+
// just put the key without the value
|
|
42
|
+
attrStr += ` ${key}`;
|
|
43
|
+
}
|
|
44
|
+
else if (typeof value === "string" ||
|
|
45
|
+
typeof value === "number" ||
|
|
46
|
+
typeof value === "bigint") {
|
|
47
|
+
attrStr += ` ${key}=${JSON.stringify(value)}`;
|
|
48
|
+
}
|
|
49
|
+
// otherwise, don't include the attribute
|
|
50
|
+
}
|
|
51
|
+
yield `<${tag}${attrStr}>`;
|
|
52
|
+
if (voidElements.has(tag))
|
|
53
|
+
return;
|
|
54
|
+
if (children)
|
|
55
|
+
yield* toGenerator(children);
|
|
56
|
+
yield `</${tag}>`;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* jsx requires a `Fragment` export to resolve <></>
|
|
61
|
+
*
|
|
62
|
+
* @param props containing `children` to render
|
|
63
|
+
* @returns async generator that yields concatenated children
|
|
64
|
+
*/
|
|
65
|
+
export async function* Fragment(props = {}) {
|
|
66
|
+
yield* toGenerator(props.children);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* @param element
|
|
70
|
+
* @returns async generator that yields concatenated children
|
|
71
|
+
*/
|
|
72
|
+
export async function* toGenerator(element) {
|
|
73
|
+
if (typeof element === "function")
|
|
74
|
+
element = element();
|
|
75
|
+
if (element instanceof Promise)
|
|
76
|
+
element = await element;
|
|
77
|
+
// undefined, null, or false should render ""
|
|
78
|
+
if (element == null || element === false)
|
|
79
|
+
return;
|
|
80
|
+
if (typeof element === "object") {
|
|
81
|
+
if (Symbol.asyncIterator in element) {
|
|
82
|
+
for await (const children of element)
|
|
83
|
+
yield* toGenerator(children);
|
|
84
|
+
}
|
|
85
|
+
else if (Symbol.iterator in element) {
|
|
86
|
+
const generators = [];
|
|
87
|
+
for (const children of element)
|
|
88
|
+
generators.push(toGenerator(children));
|
|
89
|
+
const queue = new Array(generators.length).fill("");
|
|
90
|
+
const completed = new Set();
|
|
91
|
+
let current = 0;
|
|
92
|
+
for await (const { index, value, done } of mergeAsyncIterables(generators)) {
|
|
93
|
+
if (done) {
|
|
94
|
+
completed.add(index);
|
|
95
|
+
if (index === current) {
|
|
96
|
+
while (++current < generators.length) {
|
|
97
|
+
if (queue[current]) {
|
|
98
|
+
// yield whatever is in the next queue even if it hasn't completed yet
|
|
99
|
+
yield queue[current];
|
|
100
|
+
queue[current] = "";
|
|
101
|
+
}
|
|
102
|
+
// if it hasn't completed, stop iterating to the next
|
|
103
|
+
if (!completed.has(current))
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
else if (index === current) {
|
|
109
|
+
yield value; // stream the current value directly
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
queue[index] += value; // queue the value for later
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
yield queue.join(""); // clear the queue
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
yield JSON.stringify(element); // avoids things like [object Object]
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
yield String(element); // primitive
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Converts a `JSX.Element` into its fully concatenated string representation.
|
|
127
|
+
*
|
|
128
|
+
* WARNING - this negates streaming benefits and buffers the result into a string.
|
|
129
|
+
*
|
|
130
|
+
* @param element
|
|
131
|
+
* @returns A promise that resolves to the concatenated string.
|
|
132
|
+
*/
|
|
133
|
+
export const toString = async (element) => {
|
|
134
|
+
let buffer = "";
|
|
135
|
+
for await (const value of toGenerator(element))
|
|
136
|
+
buffer += value;
|
|
137
|
+
return buffer;
|
|
138
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { jsx, jsx as jsxs, jsx as jsxDEV, Fragment, toGenerator, toString, type FC, type JSX, } from "./jsx/index.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { jsx, jsx as jsxs, jsx as jsxDEV, Fragment, toGenerator, toString, } from "./jsx/index.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { jsx, jsx as jsxs, jsx as jsxDEV, Fragment, toGenerator, toString, type FC, type JSX, } from "./jsx/index.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { jsx, jsx as jsxs, jsx as jsxDEV, Fragment, toGenerator, toString, } from "./jsx/index.js";
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { type JSX } from "../jsx/index.js";
|
|
2
|
+
import type { Route } from "../trie/index.js";
|
|
3
|
+
import type { MaybePromise } from "../types.js";
|
|
4
|
+
import type { Middleware, Params, TrailingSlash, UnmatchedContext } from "./index.js";
|
|
5
|
+
type Layout = (props: {
|
|
6
|
+
children: JSX.Element;
|
|
7
|
+
}) => JSX.Element;
|
|
8
|
+
export declare class Context<State, P extends Params> {
|
|
9
|
+
#private;
|
|
10
|
+
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Request) */
|
|
11
|
+
req: Request;
|
|
12
|
+
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/URL) */
|
|
13
|
+
url: URL;
|
|
14
|
+
/**
|
|
15
|
+
* `state` returned from `config.start`, possibly modified by previous middleware.
|
|
16
|
+
*
|
|
17
|
+
* @default null
|
|
18
|
+
*/
|
|
19
|
+
state: State;
|
|
20
|
+
/**
|
|
21
|
+
* Route pattern parameters
|
|
22
|
+
*
|
|
23
|
+
* Given the route pattern `/posts/:slug` is added, a request made to
|
|
24
|
+
* `/posts/my-post` creates a `params` object `{ slug: "my-post" }`.
|
|
25
|
+
*
|
|
26
|
+
* @example { slug: "my-post" }
|
|
27
|
+
*/
|
|
28
|
+
params: P;
|
|
29
|
+
/** The matched `Route` instance. */
|
|
30
|
+
route: Route<Middleware<State, P>[]>;
|
|
31
|
+
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response#body) */
|
|
32
|
+
body: BodyInit | null;
|
|
33
|
+
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status) */
|
|
34
|
+
status?: number;
|
|
35
|
+
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Headers) */
|
|
36
|
+
headers: Headers;
|
|
37
|
+
/**
|
|
38
|
+
* Base HTML to inject the `head` and `page` elements into.
|
|
39
|
+
*
|
|
40
|
+
* @default
|
|
41
|
+
*
|
|
42
|
+
* ```html
|
|
43
|
+
* <!doctype html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"></head><body></body></html>
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
base: string;
|
|
47
|
+
/**
|
|
48
|
+
* Assign a handler to run when an `Error` is thrown.
|
|
49
|
+
*
|
|
50
|
+
* If not set, `Error` will be thrown. This might be desired
|
|
51
|
+
* if your server already includes error handling. Set in `config.start`
|
|
52
|
+
* to handle errors globally.
|
|
53
|
+
*
|
|
54
|
+
* @default null
|
|
55
|
+
*/
|
|
56
|
+
error: ((context: UnmatchedContext<State, Params>, error: unknown) => MaybePromise<void>) | null;
|
|
57
|
+
constructor(req: Request, url: URL, trailingSlash: TrailingSlash);
|
|
58
|
+
/**
|
|
59
|
+
* Middleware to run when no `body` or `status` has been set on the `context`.
|
|
60
|
+
* Set to a new function to override the default.
|
|
61
|
+
*
|
|
62
|
+
* @default
|
|
63
|
+
*
|
|
64
|
+
* ```ts
|
|
65
|
+
* () => this.html("Not found", 404)
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
notFound(_context: UnmatchedContext<State, P>): MaybePromise<void>;
|
|
69
|
+
/**
|
|
70
|
+
* Mirrors `new Response()` constructor, set values with one function.
|
|
71
|
+
*
|
|
72
|
+
* @param body Response BodyInit
|
|
73
|
+
* @param init Enhanced ResponseInit
|
|
74
|
+
*/
|
|
75
|
+
res(body: BodyInit | null, init?: {
|
|
76
|
+
/**
|
|
77
|
+
* [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
|
|
78
|
+
*
|
|
79
|
+
* @default 200
|
|
80
|
+
*/
|
|
81
|
+
status?: number;
|
|
82
|
+
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Headers) */
|
|
83
|
+
headers?: HeadersInit;
|
|
84
|
+
}): void;
|
|
85
|
+
/**
|
|
86
|
+
* Creates an HTML response.
|
|
87
|
+
*
|
|
88
|
+
* @param body HTML body
|
|
89
|
+
* @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
|
|
90
|
+
*/
|
|
91
|
+
html(body: BodyInit | null, status?: number): void;
|
|
92
|
+
/**
|
|
93
|
+
* Creates a JSON response.
|
|
94
|
+
*
|
|
95
|
+
* @param data passed into JSON.stringify to create the body
|
|
96
|
+
* @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
|
|
97
|
+
*/
|
|
98
|
+
json(data: any, status?: number): void;
|
|
99
|
+
/**
|
|
100
|
+
* Creates a plain text response.
|
|
101
|
+
*
|
|
102
|
+
* @param body response body
|
|
103
|
+
* @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
|
|
104
|
+
*/
|
|
105
|
+
text(body: BodyInit, status?: number): void;
|
|
106
|
+
/**
|
|
107
|
+
* Creates a redirect response.
|
|
108
|
+
*
|
|
109
|
+
* @param location redirect `Location` header
|
|
110
|
+
* @param status defaults to [`302`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/302)
|
|
111
|
+
*/
|
|
112
|
+
redirect(location: string | URL, status?: 301 | 302 | 303 | 307 | 308): void;
|
|
113
|
+
/**
|
|
114
|
+
* @param Element `JSX.Element` to add to the head
|
|
115
|
+
*/
|
|
116
|
+
head(...Element: JSX.Element[]): void;
|
|
117
|
+
/**
|
|
118
|
+
* @param Layout `Layout`(s) for the `Page` to be passed into before being injected into the `<body>`
|
|
119
|
+
*/
|
|
120
|
+
layout(...Layout: Layout[]): void;
|
|
121
|
+
/**
|
|
122
|
+
* Creates an HTML response based on the `head` elements, `Layout`(s), and `Page` provided.
|
|
123
|
+
*
|
|
124
|
+
* @param Page `JSX.Element` to inject into the `<body>`
|
|
125
|
+
* @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
|
|
126
|
+
*/
|
|
127
|
+
page(Page: JSX.Element, status?: number): void;
|
|
128
|
+
/**
|
|
129
|
+
* Generates an etag from a hash of the string provided.
|
|
130
|
+
* If the etag matches, sets the response to not modified.
|
|
131
|
+
*
|
|
132
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag)
|
|
133
|
+
*
|
|
134
|
+
* @param s string to hash
|
|
135
|
+
* @returns `true` if the etag matches, `false` otherwise
|
|
136
|
+
*/
|
|
137
|
+
etag(s: string): boolean;
|
|
138
|
+
/**
|
|
139
|
+
* @returns the constructed `Response`
|
|
140
|
+
*/
|
|
141
|
+
build(): Response;
|
|
142
|
+
}
|
|
143
|
+
export {};
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { toGenerator } from "../jsx/index.js";
|
|
2
|
+
class TagNotFound extends Error {
|
|
3
|
+
constructor(tag) {
|
|
4
|
+
super(`No closing ${tag} tag found`);
|
|
5
|
+
this.name = "TagNotFound";
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export class Context {
|
|
9
|
+
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Request) */
|
|
10
|
+
req;
|
|
11
|
+
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/URL) */
|
|
12
|
+
url;
|
|
13
|
+
/**
|
|
14
|
+
* `state` returned from `config.start`, possibly modified by previous middleware.
|
|
15
|
+
*
|
|
16
|
+
* @default null
|
|
17
|
+
*/
|
|
18
|
+
state = null;
|
|
19
|
+
/**
|
|
20
|
+
* Route pattern parameters
|
|
21
|
+
*
|
|
22
|
+
* Given the route pattern `/posts/:slug` is added, a request made to
|
|
23
|
+
* `/posts/my-post` creates a `params` object `{ slug: "my-post" }`.
|
|
24
|
+
*
|
|
25
|
+
* @example { slug: "my-post" }
|
|
26
|
+
*/
|
|
27
|
+
params = {}; // set after match
|
|
28
|
+
/** The matched `Route` instance. */
|
|
29
|
+
route = undefined; // set after match
|
|
30
|
+
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response#body) */
|
|
31
|
+
body = null;
|
|
32
|
+
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status) */
|
|
33
|
+
status;
|
|
34
|
+
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Headers) */
|
|
35
|
+
headers = new Headers();
|
|
36
|
+
/**
|
|
37
|
+
* Base HTML to inject the `head` and `page` elements into.
|
|
38
|
+
*
|
|
39
|
+
* @default
|
|
40
|
+
*
|
|
41
|
+
* ```html
|
|
42
|
+
* <!doctype html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"></head><body></body></html>
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
base = '<!doctype html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"></head><body></body></html>';
|
|
46
|
+
/**
|
|
47
|
+
* Assign a handler to run when an `Error` is thrown.
|
|
48
|
+
*
|
|
49
|
+
* If not set, `Error` will be thrown. This might be desired
|
|
50
|
+
* if your server already includes error handling. Set in `config.start`
|
|
51
|
+
* to handle errors globally.
|
|
52
|
+
*
|
|
53
|
+
* @default null
|
|
54
|
+
*/
|
|
55
|
+
error = null;
|
|
56
|
+
#layouts = [];
|
|
57
|
+
#headElements = [];
|
|
58
|
+
#trailingSlash;
|
|
59
|
+
constructor(req, url, trailingSlash) {
|
|
60
|
+
this.req = req;
|
|
61
|
+
this.url = url;
|
|
62
|
+
this.#trailingSlash = trailingSlash;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Middleware to run when no `body` or `status` has been set on the `context`.
|
|
66
|
+
* Set to a new function to override the default.
|
|
67
|
+
*
|
|
68
|
+
* @default
|
|
69
|
+
*
|
|
70
|
+
* ```ts
|
|
71
|
+
* () => this.html("Not found", 404)
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
notFound(_context) {
|
|
75
|
+
this.html("Not found", 404);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Mirrors `new Response()` constructor, set values with one function.
|
|
79
|
+
*
|
|
80
|
+
* @param body Response BodyInit
|
|
81
|
+
* @param init Enhanced ResponseInit
|
|
82
|
+
*/
|
|
83
|
+
res(body, init) {
|
|
84
|
+
this.body = body;
|
|
85
|
+
this.status = init?.status ?? 200;
|
|
86
|
+
if (init?.headers) {
|
|
87
|
+
for (const [name, value] of new Headers(init.headers)) {
|
|
88
|
+
this.headers.append(name, value);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Creates an HTML response.
|
|
94
|
+
*
|
|
95
|
+
* @param body HTML body
|
|
96
|
+
* @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
|
|
97
|
+
*/
|
|
98
|
+
html(body, status) {
|
|
99
|
+
this.res(body, {
|
|
100
|
+
status,
|
|
101
|
+
headers: { "content-type": "text/html; charset=utf-8" },
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Creates a JSON response.
|
|
106
|
+
*
|
|
107
|
+
* @param data passed into JSON.stringify to create the body
|
|
108
|
+
* @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
|
|
109
|
+
*/
|
|
110
|
+
json(data, status) {
|
|
111
|
+
this.res(JSON.stringify(data), {
|
|
112
|
+
status,
|
|
113
|
+
headers: { "content-type": "application/json" },
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Creates a plain text response.
|
|
118
|
+
*
|
|
119
|
+
* @param body response body
|
|
120
|
+
* @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
|
|
121
|
+
*/
|
|
122
|
+
text(body, status) {
|
|
123
|
+
this.res(body, { status, headers: { "content-type": "text/html" } });
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Creates a redirect response.
|
|
127
|
+
*
|
|
128
|
+
* @param location redirect `Location` header
|
|
129
|
+
* @param status defaults to [`302`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/302)
|
|
130
|
+
*/
|
|
131
|
+
redirect(location, status = 302) {
|
|
132
|
+
this.headers.set("location", location.toString());
|
|
133
|
+
this.status = status;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* @param Element `JSX.Element` to add to the head
|
|
137
|
+
*/
|
|
138
|
+
head(...Element) {
|
|
139
|
+
this.#headElements.push(...Element);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* @param Layout `Layout`(s) for the `Page` to be passed into before being injected into the `<body>`
|
|
143
|
+
*/
|
|
144
|
+
layout(...Layout) {
|
|
145
|
+
this.#layouts.push(...Layout);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Creates an HTML response based on the `head` elements, `Layout`(s), and `Page` provided.
|
|
149
|
+
*
|
|
150
|
+
* @param Page `JSX.Element` to inject into the `<body>`
|
|
151
|
+
* @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
|
|
152
|
+
*/
|
|
153
|
+
page(Page, status) {
|
|
154
|
+
for (let i = this.#layouts.length - 1; i >= 0; i--)
|
|
155
|
+
Page = this.#layouts[i]({ children: Page });
|
|
156
|
+
const headClose = "</head>";
|
|
157
|
+
const bodyClose = "</body>";
|
|
158
|
+
const elements = this.base.split(headClose);
|
|
159
|
+
if (!elements[1])
|
|
160
|
+
throw new TagNotFound(headClose);
|
|
161
|
+
elements.splice(1, 0, this.#headElements, headClose);
|
|
162
|
+
const bodyParts = elements[3].split(bodyClose);
|
|
163
|
+
if (!bodyParts[1])
|
|
164
|
+
throw new TagNotFound(bodyClose);
|
|
165
|
+
elements[3] = bodyParts[0];
|
|
166
|
+
elements.push(Page, bodyClose + bodyParts[1]);
|
|
167
|
+
this.html(new ReadableStream({
|
|
168
|
+
start: async (c) => {
|
|
169
|
+
for await (const value of toGenerator(elements))
|
|
170
|
+
c.enqueue(value);
|
|
171
|
+
c.close();
|
|
172
|
+
},
|
|
173
|
+
}).pipeThrough(new TextEncoderStream()), status);
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Generates an etag from a hash of the string provided.
|
|
177
|
+
* If the etag matches, sets the response to not modified.
|
|
178
|
+
*
|
|
179
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag)
|
|
180
|
+
*
|
|
181
|
+
* @param s string to hash
|
|
182
|
+
* @returns `true` if the etag matches, `false` otherwise
|
|
183
|
+
*/
|
|
184
|
+
etag(s) {
|
|
185
|
+
// Fast hashing algorithm http://www.cse.yorku.ca/~oz/hash.html
|
|
186
|
+
// Adapted from SvelteKit https://github.com/sveltejs/kit/blob/25d459104814b0c2dc6b4cf73b680378a29d8200/packages/kit/src/runtime/hash.js
|
|
187
|
+
let hash = 5381;
|
|
188
|
+
let i = s.length;
|
|
189
|
+
while (i)
|
|
190
|
+
hash = (hash * 33) ^ s.charCodeAt(--i);
|
|
191
|
+
const etag = `"${(hash >>> 0).toString(36)}"`;
|
|
192
|
+
this.headers.set("etag", etag);
|
|
193
|
+
if (this.req.headers.get("if-none-match") === etag) {
|
|
194
|
+
this.body = null;
|
|
195
|
+
this.status = 304;
|
|
196
|
+
return true;
|
|
197
|
+
}
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* @returns the constructed `Response`
|
|
202
|
+
*/
|
|
203
|
+
build() {
|
|
204
|
+
if ((!this.status || this.status === 404) &&
|
|
205
|
+
this.#trailingSlash !== "ignore") {
|
|
206
|
+
const last = this.url.pathname.at(-1);
|
|
207
|
+
if (this.#trailingSlash === "always" && last !== "/") {
|
|
208
|
+
this.url.pathname += "/";
|
|
209
|
+
this.redirect(this.url, 308);
|
|
210
|
+
}
|
|
211
|
+
else if (this.#trailingSlash === "never" &&
|
|
212
|
+
this.url.pathname !== "/" &&
|
|
213
|
+
last === "/") {
|
|
214
|
+
this.url.pathname = this.url.pathname.slice(0, -1);
|
|
215
|
+
this.redirect(this.url, 308);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
if (!this.body && !this.status)
|
|
219
|
+
this.notFound(this);
|
|
220
|
+
return new Response(this.body, this);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { MaybePromise } from "../types.js";
|
|
2
|
+
import { Context } from "./context.js";
|
|
3
|
+
export type Params = Record<string, string>;
|
|
4
|
+
export type UnmatchedContext<State, P extends Params> = Omit<Context<State, P>, "route"> & Partial<Pick<Context<State, P>, "route">>;
|
|
5
|
+
export type Start<State> = (context: Omit<UnmatchedContext<any, Params>, "state" | "route" | "params">) => State;
|
|
6
|
+
export type Middleware<State = null, P extends Params = Params> = (context: Context<State, P>, next: () => Promise<void>) => MaybePromise<void>;
|
|
7
|
+
export type TrailingSlash = "always" | "never" | "ignore";
|
|
8
|
+
type Method = "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "CONNECT" | "OPTIONS" | "TRACE" | "PATCH" | (string & {});
|
|
9
|
+
type ExtractParams<Pattern extends string = string> = Pattern extends `${infer _Start}:${infer Param}/${infer Rest}` ? {
|
|
10
|
+
[k in Param | keyof ExtractParams<Rest>]: string;
|
|
11
|
+
} : Pattern extends `${infer _Start}:${infer Param}` ? {
|
|
12
|
+
[k in Param]: string;
|
|
13
|
+
} : Pattern extends `${infer _Rest}*` ? {
|
|
14
|
+
"*": string;
|
|
15
|
+
} : {};
|
|
16
|
+
type ExtractMultiParams<Patterns extends string[]> = Patterns extends [
|
|
17
|
+
infer First extends string,
|
|
18
|
+
...infer Rest extends string[]
|
|
19
|
+
] ? Rest["length"] extends 0 ? ExtractParams<First> : ExtractParams<First> | ExtractMultiParams<Rest> : never;
|
|
20
|
+
export declare class Router<State = null> {
|
|
21
|
+
#private;
|
|
22
|
+
constructor(config?: {
|
|
23
|
+
/**
|
|
24
|
+
* - `"never"` - Not found requests with a trailing slash will be redirected to the same path without a trailing slash
|
|
25
|
+
* - `"always"` - Not found requests without a trailing slash will be redirected to the same path with a trailing slash
|
|
26
|
+
* - `"ignore"` - no redirects (not recommended, bad for SEO)
|
|
27
|
+
*
|
|
28
|
+
* [Trailing Slash for Frameworks by Bjorn Lu](https://bjornlu.com/blog/trailing-slash-for-frameworks)
|
|
29
|
+
*
|
|
30
|
+
* @default "never"
|
|
31
|
+
*/
|
|
32
|
+
trailingSlash?: TrailingSlash;
|
|
33
|
+
/**
|
|
34
|
+
* Runs before middleware, return a value to set the initial state.
|
|
35
|
+
*
|
|
36
|
+
* @param context request context
|
|
37
|
+
* @returns any state to access in middleware
|
|
38
|
+
* @default null
|
|
39
|
+
*/
|
|
40
|
+
start?: Start<State>;
|
|
41
|
+
});
|
|
42
|
+
/**
|
|
43
|
+
* @param method HTTP method
|
|
44
|
+
* @param pattern route pattern
|
|
45
|
+
* @param middleware
|
|
46
|
+
* @returns the router instance
|
|
47
|
+
*/
|
|
48
|
+
on<Pattern extends string>(method: Method, pattern: Pattern, ...middleware: Middleware<State, ExtractParams<Pattern>>[]): this;
|
|
49
|
+
/**
|
|
50
|
+
* @param method HTTP method
|
|
51
|
+
* @param patterns array of route patterns
|
|
52
|
+
* @param middleware
|
|
53
|
+
* @returns the router instance
|
|
54
|
+
*/
|
|
55
|
+
on<Patterns extends string[]>(method: Method, patterns: [...Patterns], ...middleware: Middleware<State, ExtractMultiParams<Patterns>>[]): this;
|
|
56
|
+
/**
|
|
57
|
+
* @param pattern route pattern
|
|
58
|
+
* @param middleware
|
|
59
|
+
* @returns the router instance
|
|
60
|
+
*/
|
|
61
|
+
get<Pattern extends string>(pattern: Pattern, ...middleware: Middleware<State, ExtractParams<Pattern>>[]): this;
|
|
62
|
+
/**
|
|
63
|
+
* @param patterns array of route patterns
|
|
64
|
+
* @param middleware
|
|
65
|
+
* @returns the router instance
|
|
66
|
+
*/
|
|
67
|
+
get<Patterns extends string[]>(patterns: [...Patterns], ...middleware: Middleware<State, ExtractMultiParams<Patterns>>[]): this;
|
|
68
|
+
/**
|
|
69
|
+
* @param pattern route pattern
|
|
70
|
+
* @param middleware
|
|
71
|
+
* @returns the router instance
|
|
72
|
+
*/
|
|
73
|
+
post<Pattern extends string>(pattern: Pattern, ...middleware: Middleware<State, ExtractParams<Pattern>>[]): this;
|
|
74
|
+
/**
|
|
75
|
+
* @param patterns array of route patterns
|
|
76
|
+
* @param middleware
|
|
77
|
+
* @returns the router instance
|
|
78
|
+
*/
|
|
79
|
+
post<Patterns extends string[]>(patterns: [...Patterns], ...middleware: Middleware<State, ExtractMultiParams<Patterns>>[]): this;
|
|
80
|
+
/**
|
|
81
|
+
* @param basePattern pattern to mount the router to, each route will begin with this base
|
|
82
|
+
* @param router sub-router to mount
|
|
83
|
+
* @returns the base router instance
|
|
84
|
+
*/
|
|
85
|
+
mount(basePattern: string, router: Router<State>): this;
|
|
86
|
+
/**
|
|
87
|
+
* @param req [`Request` Reference](https://developer.mozilla.org/en-US/docs/Web/API/Request)
|
|
88
|
+
* @returns [`Response` Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response)
|
|
89
|
+
*/
|
|
90
|
+
fetch(req: Request): Promise<Response>;
|
|
91
|
+
}
|
|
92
|
+
export {};
|