ovr 4.6.0 → 6.0.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/dist/LICENSE.md +26 -0
- package/dist/app/index.d.ts +55 -103
- package/dist/app/index.d.ts.map +1 -1
- package/dist/app/index.js +67 -145
- package/dist/context/index.d.ts +138 -0
- package/dist/context/index.d.ts.map +1 -0
- package/dist/context/index.js +218 -0
- package/dist/cookie/index.d.ts +121 -0
- package/dist/cookie/index.d.ts.map +1 -0
- package/dist/cookie/index.js +56 -0
- package/dist/index.d.ts +6 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -7
- package/dist/jsx/{intrinsic-elements.d.ts → elements.d.ts} +14 -7
- package/dist/jsx/elements.d.ts.map +1 -0
- package/dist/jsx/index.d.ts +37 -52
- package/dist/jsx/index.d.ts.map +1 -1
- package/dist/jsx/index.js +71 -197
- package/dist/middleware/index.d.ts +22 -0
- package/dist/middleware/index.d.ts.map +1 -0
- package/dist/multipart/index.d.ts +122 -0
- package/dist/multipart/index.d.ts.map +1 -0
- package/dist/multipart/index.js +341 -0
- package/dist/render/index.d.ts +78 -0
- package/dist/render/index.d.ts.map +1 -0
- package/dist/render/index.js +248 -0
- package/dist/route/index.d.ts +147 -0
- package/dist/route/index.d.ts.map +1 -0
- package/dist/route/index.js +126 -0
- package/dist/trie/index.d.ts +31 -26
- package/dist/trie/index.d.ts.map +1 -1
- package/dist/trie/index.js +71 -80
- package/dist/util/index.d.ts +38 -0
- package/dist/util/index.d.ts.map +1 -0
- package/dist/util/index.js +88 -0
- package/package.json +4 -4
- package/src/app/index.ts +128 -265
- package/src/context/index.ts +262 -0
- package/src/cookie/index.ts +189 -0
- package/src/index.ts +6 -8
- package/src/jsx/{intrinsic-elements.ts → elements.ts} +14 -7
- package/src/jsx/index.ts +81 -222
- package/src/middleware/index.ts +29 -0
- package/src/multipart/index.ts +486 -0
- package/src/render/index.ts +300 -0
- package/src/route/index.ts +332 -0
- package/src/trie/index.ts +90 -107
- package/src/util/index.ts +112 -0
- package/dist/app/context.d.ts +0 -147
- package/dist/app/context.d.ts.map +0 -1
- package/dist/app/context.js +0 -283
- package/dist/app/helper/get.d.ts +0 -30
- package/dist/app/helper/get.d.ts.map +0 -1
- package/dist/app/helper/get.js +0 -33
- package/dist/app/helper/index.d.ts +0 -55
- package/dist/app/helper/index.d.ts.map +0 -1
- package/dist/app/helper/index.js +0 -96
- package/dist/app/helper/post.d.ts +0 -35
- package/dist/app/helper/post.d.ts.map +0 -1
- package/dist/app/helper/post.js +0 -15
- package/dist/app/memo/index.d.ts +0 -12
- package/dist/app/memo/index.d.ts.map +0 -1
- package/dist/app/memo/index.js +0 -34
- package/dist/app/mw/csrf.d.ts +0 -23
- package/dist/app/mw/csrf.d.ts.map +0 -1
- package/dist/app/mw/csrf.js +0 -30
- package/dist/jsx/chunk/index.d.ts +0 -38
- package/dist/jsx/chunk/index.d.ts.map +0 -1
- package/dist/jsx/chunk/index.js +0 -69
- package/dist/jsx/intrinsic-elements.d.ts.map +0 -1
- package/dist/jsx/merge-async-generators.d.ts +0 -14
- package/dist/jsx/merge-async-generators.d.ts.map +0 -1
- package/dist/jsx/merge-async-generators.js +0 -42
- package/dist/types/index.d.ts +0 -17
- package/dist/types/index.d.ts.map +0 -1
- package/dist/util/hash.d.ts +0 -11
- package/dist/util/hash.d.ts.map +0 -1
- package/dist/util/hash.js +0 -16
- package/src/app/context.ts +0 -365
- package/src/app/helper/get.ts +0 -46
- package/src/app/helper/index.ts +0 -174
- package/src/app/helper/post.ts +0 -54
- package/src/app/memo/index.ts +0 -40
- package/src/app/mw/csrf.ts +0 -32
- package/src/jsx/chunk/index.ts +0 -79
- package/src/jsx/merge-async-generators.ts +0 -47
- package/src/types/index.ts +0 -42
- package/src/util/hash.ts +0 -17
- /package/dist/jsx/{intrinsic-elements.js → elements.js} +0 -0
- /package/dist/{types → middleware}/index.js +0 -0
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
import type { JSX } from "../jsx/index.js";
|
|
2
|
+
import { Codec } from "../util/index.js";
|
|
3
|
+
|
|
4
|
+
type Next = { i: number; result: IteratorResult<Chunk, void> };
|
|
5
|
+
|
|
6
|
+
/** Chunk containing the HTML from a rendered element */
|
|
7
|
+
class Chunk {
|
|
8
|
+
/** Safe value to render */
|
|
9
|
+
#value: string;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Chunk containing the HTML from a rendered element.
|
|
13
|
+
*
|
|
14
|
+
* @param html string of HTML to escape
|
|
15
|
+
* @param safe Set to `true` if the HTML is safe and should not be escaped
|
|
16
|
+
*/
|
|
17
|
+
constructor(html: unknown, safe?: boolean) {
|
|
18
|
+
const value = String(html ?? "");
|
|
19
|
+
this.#value = safe ? value : Render.escape(value);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @returns Safe value to render
|
|
24
|
+
*/
|
|
25
|
+
toString() {
|
|
26
|
+
return this.#value;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @param chunk Chunk to append to the end of the chunk
|
|
31
|
+
*/
|
|
32
|
+
concat(chunk: Chunk) {
|
|
33
|
+
this.#value += chunk;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export namespace Render {
|
|
38
|
+
/** Render options */
|
|
39
|
+
export type Options = {
|
|
40
|
+
/** Set to `true` to disable escaping for non-HTML use */
|
|
41
|
+
safe?: boolean;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/** Rendered `Chunk` instance */
|
|
45
|
+
export type Chunk = InstanceType<typeof Chunk>;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export class Render {
|
|
49
|
+
/** Regex to find escape character for HTML attributes */
|
|
50
|
+
static readonly #attr = /[&"<]/g;
|
|
51
|
+
|
|
52
|
+
/** Regex to find escape character for HTML content */
|
|
53
|
+
static readonly #content = /[&<]/g;
|
|
54
|
+
|
|
55
|
+
/** Escape character map */
|
|
56
|
+
static readonly #map = { "&": "&", '"': """, "<": "<" };
|
|
57
|
+
|
|
58
|
+
/** Element to render */
|
|
59
|
+
readonly #element: JSX.Element;
|
|
60
|
+
|
|
61
|
+
/** Render options */
|
|
62
|
+
readonly #options?: Render.Options;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Creates an `AsyncIterable` that renders the `Element`.
|
|
66
|
+
*
|
|
67
|
+
* @param element Element to render
|
|
68
|
+
* @param options Render options
|
|
69
|
+
*/
|
|
70
|
+
constructor(element: JSX.Element, options?: Render.Options) {
|
|
71
|
+
this.#element = element;
|
|
72
|
+
this.#options = options;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** @yields `Chunk`s as the `Element` resolves */
|
|
76
|
+
async *[Symbol.asyncIterator](): AsyncGenerator<Chunk, void, unknown> {
|
|
77
|
+
let element = this.#element;
|
|
78
|
+
|
|
79
|
+
// modifications
|
|
80
|
+
// these are required to allow functions to be used as children
|
|
81
|
+
// instead of creating a separate component to use them
|
|
82
|
+
if (typeof element === "function") element = element();
|
|
83
|
+
if (element instanceof Promise) element = await element;
|
|
84
|
+
|
|
85
|
+
// resolve based on type
|
|
86
|
+
// should not render
|
|
87
|
+
if (element == null || typeof element === "boolean" || element === "")
|
|
88
|
+
return;
|
|
89
|
+
|
|
90
|
+
if (element instanceof Chunk) {
|
|
91
|
+
// already escaped or safe
|
|
92
|
+
yield element;
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (typeof element === "object") {
|
|
97
|
+
if (Symbol.asyncIterator in element) {
|
|
98
|
+
// any async iterable - lazily resolve
|
|
99
|
+
for await (const children of element)
|
|
100
|
+
yield* new Render(children, this.#options);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (Symbol.iterator in element) {
|
|
105
|
+
// sync iterable
|
|
106
|
+
if ("next" in element) {
|
|
107
|
+
// sync generator - lazily resolve, avoids loading all in memory
|
|
108
|
+
for (const children of element)
|
|
109
|
+
yield* new Render(children, this.#options);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// other iterable - array, set, etc.
|
|
114
|
+
// process children in parallel
|
|
115
|
+
const renders = Array.from(
|
|
116
|
+
element,
|
|
117
|
+
(el) => new Render(el, this.#options),
|
|
118
|
+
);
|
|
119
|
+
const n = renders.length;
|
|
120
|
+
const queue = new Array<Chunk | null>(n);
|
|
121
|
+
const complete = new Uint8Array(n);
|
|
122
|
+
let current = 0;
|
|
123
|
+
|
|
124
|
+
for await (const m of Render.#merge(renders)) {
|
|
125
|
+
if (m.result.done) {
|
|
126
|
+
complete[m.i] = 1;
|
|
127
|
+
|
|
128
|
+
if (m.i === current) {
|
|
129
|
+
while (++current < n) {
|
|
130
|
+
if (queue[current]) {
|
|
131
|
+
// yield whatever is in the next queue even if it hasn't completed yet
|
|
132
|
+
yield queue[current]!;
|
|
133
|
+
queue[current] = null;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// if it hasn't completed, stop iterating to the next
|
|
137
|
+
if (!complete[current]) break;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
} else if (m.i === current) {
|
|
141
|
+
yield m.result.value; // stream the current value directly
|
|
142
|
+
} else {
|
|
143
|
+
// queue the value for later
|
|
144
|
+
if (queue[m.i]) queue[m.i]!.concat(m.result.value);
|
|
145
|
+
else queue[m.i] = m.result.value;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// clear the queue
|
|
150
|
+
yield* queue.filter((chunk) => chunk !== null);
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// primitive or other object
|
|
156
|
+
yield new Chunk(element, this.#options?.safe);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* `render` piped into a `ReadableStream`.
|
|
161
|
+
* Use `render` when possible to avoid the overhead of the stream.
|
|
162
|
+
*
|
|
163
|
+
* @param element Element to render
|
|
164
|
+
* @param options Render options
|
|
165
|
+
* @returns `ReadableStream` of HTML
|
|
166
|
+
*/
|
|
167
|
+
static stream(element: JSX.Element, options?: Render.Options) {
|
|
168
|
+
const gen = new Render(element, options)[Symbol.asyncIterator]();
|
|
169
|
+
|
|
170
|
+
return new ReadableStream<Uint8Array>(
|
|
171
|
+
{
|
|
172
|
+
// enables zero-copy transfer from underlying source when queue is empty
|
|
173
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/Streams_API/Using_readable_byte_streams#overview
|
|
174
|
+
type: "bytes",
|
|
175
|
+
// `pull` ensures backpressure and cancelled requests stop the generator
|
|
176
|
+
async pull(c) {
|
|
177
|
+
const result = await gen.next();
|
|
178
|
+
|
|
179
|
+
if (result.done) {
|
|
180
|
+
c.close();
|
|
181
|
+
gen.return();
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
c.enqueue(
|
|
186
|
+
// need to encode for Node (ex: during prerendering) or it will error
|
|
187
|
+
// doesn't seem to be needed for browsers
|
|
188
|
+
// faster than piping through a `TextEncoderStream`
|
|
189
|
+
Codec.encode(String(result.value)),
|
|
190
|
+
);
|
|
191
|
+
},
|
|
192
|
+
cancel() {
|
|
193
|
+
gen.return();
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
// `highWaterMark` defaults to 1
|
|
198
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/ReadableStream#highwatermark
|
|
199
|
+
// setting this ensures at least a small buffer is maintained if the
|
|
200
|
+
// underlying server does not have its own high water mark set
|
|
201
|
+
// https://blog.cloudflare.com/unpacking-cloudflare-workers-cpu-performance-benchmarks/#inefficient-streams-adapters
|
|
202
|
+
// in Node, the default is 16kb, so this stacks another 2kb in front
|
|
203
|
+
// https://nodejs.org/api/http.html#outgoingmessagewritablehighwatermark
|
|
204
|
+
highWaterMark: 2048,
|
|
205
|
+
},
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Converts a render into a fully concatenated string of HTML.
|
|
211
|
+
*
|
|
212
|
+
* ### WARNING
|
|
213
|
+
*
|
|
214
|
+
* This negates streaming benefits and buffers the result into a string.
|
|
215
|
+
* @param element Element to render
|
|
216
|
+
* @param options Render options
|
|
217
|
+
* @returns Concatenated HTML
|
|
218
|
+
*/
|
|
219
|
+
async string(element: JSX.Element, options?: Render.Options) {
|
|
220
|
+
return (await Array.fromAsync(new Render(element, options))).join("");
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Render raw HTML and dangerously bypass escaping.
|
|
225
|
+
*
|
|
226
|
+
* @param html Safe html to render
|
|
227
|
+
* @returns New _safe_ `Chunk`
|
|
228
|
+
*/
|
|
229
|
+
static html(html: unknown) {
|
|
230
|
+
return new Chunk(html, true);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Escapes strings of HTML.
|
|
235
|
+
*
|
|
236
|
+
* @param html String to escape
|
|
237
|
+
* @param attr Set to `true` if the value is an attribute, otherwise it's a string of HTML content
|
|
238
|
+
* @returns Escaped string of HTML
|
|
239
|
+
*/
|
|
240
|
+
static escape(html: string, attr?: boolean) {
|
|
241
|
+
return html.replace(
|
|
242
|
+
attr ? Render.#attr : Render.#content,
|
|
243
|
+
(c) =>
|
|
244
|
+
// @ts-expect-error - private method type error
|
|
245
|
+
Render.#map[c],
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* @param gen
|
|
251
|
+
* @param i index of the generator within the list
|
|
252
|
+
* @returns promise containing the index and the next result of the iteration
|
|
253
|
+
*/
|
|
254
|
+
static async #next(
|
|
255
|
+
gen: AsyncGenerator<Chunk, void, unknown>,
|
|
256
|
+
i: number,
|
|
257
|
+
): Promise<Next> {
|
|
258
|
+
return { i, result: await gen.next() };
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Merges `Render[]` into a single `AsyncGenerator`, resolving all in parallel.
|
|
263
|
+
* The return of each `Render` is yielded from the generator with `done: true`.
|
|
264
|
+
*
|
|
265
|
+
* Adapted from [stack overflow answers](https://stackoverflow.com/questions/50585456).
|
|
266
|
+
*
|
|
267
|
+
* @param renders Resolved in parallel.
|
|
268
|
+
* @yields `NextResult` and index of the resolved generator.
|
|
269
|
+
*/
|
|
270
|
+
static async *#merge(renders: Render[]) {
|
|
271
|
+
const generators = renders.map((render) => render[Symbol.asyncIterator]());
|
|
272
|
+
const promises = new Map<number, Promise<Next>>();
|
|
273
|
+
|
|
274
|
+
for (let i = 0; i < generators.length; i++) {
|
|
275
|
+
promises.set(i, Render.#next(generators[i]!, i));
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
let next: Next;
|
|
279
|
+
|
|
280
|
+
try {
|
|
281
|
+
while (promises.size > 0) {
|
|
282
|
+
yield (next = await Promise.race(promises.values()));
|
|
283
|
+
|
|
284
|
+
if (next.result.done) {
|
|
285
|
+
promises.delete(next.i);
|
|
286
|
+
} else {
|
|
287
|
+
promises.set(next.i, Render.#next(generators[next.i]!, next.i));
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
} finally {
|
|
291
|
+
for (const gen of generators) {
|
|
292
|
+
try {
|
|
293
|
+
gen.return();
|
|
294
|
+
} catch {
|
|
295
|
+
// could have already returned
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import { type JSX, jsx } from "../jsx/index.js";
|
|
2
|
+
import type { Middleware } from "../middleware/index.js";
|
|
3
|
+
import type { Trie } from "../trie/index.js";
|
|
4
|
+
import { Hash } from "../util/index.js";
|
|
5
|
+
|
|
6
|
+
/** Helper type to extract the route params (`:slug`) into a record */
|
|
7
|
+
export type ExtractParams<Pattern extends string = string> =
|
|
8
|
+
Pattern extends `${infer _Start}:${infer Param}/${infer Rest}`
|
|
9
|
+
? { [k in Param | keyof ExtractParams<Rest>]: string }
|
|
10
|
+
: Pattern extends `${infer _Start}:${infer Param}`
|
|
11
|
+
? { [k in Param]: string }
|
|
12
|
+
: Pattern extends `${infer _Rest}*`
|
|
13
|
+
? { "*": string }
|
|
14
|
+
: {};
|
|
15
|
+
|
|
16
|
+
/** Helper type to insert a record of params into a resolved string */
|
|
17
|
+
export type InsertParams<
|
|
18
|
+
Pattern extends string,
|
|
19
|
+
Params extends Trie.Params,
|
|
20
|
+
> = Pattern extends `${infer Start}:${infer Param}/${infer Rest}`
|
|
21
|
+
? Param extends keyof Params
|
|
22
|
+
? `${Start}${Params[Param]}/${InsertParams<Rest, Params>}`
|
|
23
|
+
: Pattern
|
|
24
|
+
: Pattern extends `${infer Start}:${infer Param}`
|
|
25
|
+
? Param extends keyof Params
|
|
26
|
+
? `${Start}${Params[Param]}`
|
|
27
|
+
: Pattern
|
|
28
|
+
: Pattern extends `${infer Start}*`
|
|
29
|
+
? "*" extends keyof Params
|
|
30
|
+
? `${Start}${Params["*"]}`
|
|
31
|
+
: Pattern
|
|
32
|
+
: Pattern;
|
|
33
|
+
|
|
34
|
+
export namespace Route {
|
|
35
|
+
/** HTTP Method */
|
|
36
|
+
export type Method =
|
|
37
|
+
| "GET"
|
|
38
|
+
| "HEAD"
|
|
39
|
+
| "POST"
|
|
40
|
+
| "PUT"
|
|
41
|
+
| "DELETE"
|
|
42
|
+
| "CONNECT"
|
|
43
|
+
| "OPTIONS"
|
|
44
|
+
| "TRACE"
|
|
45
|
+
| "PATCH"
|
|
46
|
+
| (string & {});
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Options to construct a relative URL from the route.
|
|
50
|
+
*
|
|
51
|
+
* @template Params Parameters created from a route match
|
|
52
|
+
*/
|
|
53
|
+
export type URLOptions<Params extends Trie.Params> = {
|
|
54
|
+
/**
|
|
55
|
+
* Passed into `URLSearchParams` constructor to create new params.
|
|
56
|
+
*
|
|
57
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams)
|
|
58
|
+
*/
|
|
59
|
+
readonly search?:
|
|
60
|
+
| string
|
|
61
|
+
// Iterable is more accurate than the built in string[][] + URLSearchParams
|
|
62
|
+
// https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/2070
|
|
63
|
+
| Iterable<[string, string]>
|
|
64
|
+
| Record<string, string>;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Hash (fragment) of the URL. `"#"` prefix is added if not present.
|
|
68
|
+
*
|
|
69
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/URL/hash)
|
|
70
|
+
*/
|
|
71
|
+
readonly hash?: string;
|
|
72
|
+
} & (keyof Params extends never
|
|
73
|
+
? {
|
|
74
|
+
/** Route pattern does not contain parameters */
|
|
75
|
+
readonly params?: never;
|
|
76
|
+
}
|
|
77
|
+
: {
|
|
78
|
+
/** Route pattern parameters */
|
|
79
|
+
readonly params: Params;
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* `<Anchor>` route component type
|
|
84
|
+
*
|
|
85
|
+
* @template Pattern Route pattern
|
|
86
|
+
*/
|
|
87
|
+
export type Anchor<Pattern extends string> = (
|
|
88
|
+
props: JSX.IntrinsicElements["a"] & URLOptions<ExtractParams<Pattern>>,
|
|
89
|
+
) => JSX.Element;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* `<Button>` route component type
|
|
93
|
+
*
|
|
94
|
+
* @template Pattern Route pattern
|
|
95
|
+
*/
|
|
96
|
+
export type Button<Pattern extends string> = (
|
|
97
|
+
props: JSX.IntrinsicElements["button"] & URLOptions<ExtractParams<Pattern>>,
|
|
98
|
+
) => JSX.Element;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* `<Form>` route component type
|
|
102
|
+
*
|
|
103
|
+
* @template Pattern Route pattern
|
|
104
|
+
*/
|
|
105
|
+
export type Form<Pattern extends string> = (
|
|
106
|
+
props: JSX.IntrinsicElements["form"] & URLOptions<ExtractParams<Pattern>>,
|
|
107
|
+
) => JSX.Element;
|
|
108
|
+
|
|
109
|
+
/** Extract params from a Route instance. */
|
|
110
|
+
export type Params<R> = R extends { pattern: infer P }
|
|
111
|
+
? P extends string
|
|
112
|
+
? ExtractParams<P>
|
|
113
|
+
: never
|
|
114
|
+
: never;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// these types are needed for proper JSDoc on `get` and `post` return types
|
|
118
|
+
/**
|
|
119
|
+
* Helper type for a route with a `<Button>` component.
|
|
120
|
+
*
|
|
121
|
+
* @template Pattern Route pattern
|
|
122
|
+
*/
|
|
123
|
+
type WithButton<Pattern extends string = string> = {
|
|
124
|
+
/** `<button>` component with preset `formaction` and `formmethod` attributes */
|
|
125
|
+
readonly Button: Route.Button<Pattern>;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Helper type for a route with a `<Form>` component.
|
|
130
|
+
*
|
|
131
|
+
* @template Pattern Route pattern
|
|
132
|
+
*/
|
|
133
|
+
type WithForm<Pattern extends string = string> = {
|
|
134
|
+
/** `<form>` component with preset `method` and `action` attributes */
|
|
135
|
+
readonly Form: Route.Form<Pattern>;
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Helper type for a route with a `<Anchor>` component.
|
|
140
|
+
*
|
|
141
|
+
* @template Pattern Route pattern
|
|
142
|
+
*/
|
|
143
|
+
type WithAnchor<Pattern extends string = string> = {
|
|
144
|
+
/** `<a>` component with preset `href` attribute */
|
|
145
|
+
readonly Anchor: Route.Anchor<Pattern>;
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Route to use in the application.
|
|
150
|
+
*
|
|
151
|
+
* @template Pattern Route pattern
|
|
152
|
+
*/
|
|
153
|
+
export class Route<Pattern extends string = string> {
|
|
154
|
+
/** Route pattern */
|
|
155
|
+
readonly pattern: Pattern;
|
|
156
|
+
|
|
157
|
+
/** HTTP method */
|
|
158
|
+
readonly method: Route.Method;
|
|
159
|
+
|
|
160
|
+
/** Route middleware stack, runs after global middleware */
|
|
161
|
+
readonly middleware: Middleware<any>[]; // any so you can use other middleware
|
|
162
|
+
|
|
163
|
+
/** Pattern parts */
|
|
164
|
+
#parts: string[];
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Create a new route.
|
|
168
|
+
*
|
|
169
|
+
* @param method HTTP Method
|
|
170
|
+
* @param pattern Route pattern
|
|
171
|
+
* @param middleware Route middleware
|
|
172
|
+
*/
|
|
173
|
+
constructor(
|
|
174
|
+
method: Route.Method,
|
|
175
|
+
pattern: Pattern,
|
|
176
|
+
...middleware: Middleware<ExtractParams<Pattern>>[]
|
|
177
|
+
) {
|
|
178
|
+
this.method = method;
|
|
179
|
+
this.pattern = pattern;
|
|
180
|
+
this.middleware = middleware;
|
|
181
|
+
this.#parts = pattern.split("/");
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Constructs a _relative_ URL for the route.
|
|
186
|
+
*
|
|
187
|
+
* @param [options] Options with type safe pathname parameters
|
|
188
|
+
* @returns `pathname` + `search` + `hash`
|
|
189
|
+
*/
|
|
190
|
+
url(
|
|
191
|
+
...[options]: keyof ExtractParams<Pattern> extends never
|
|
192
|
+
? [Route.URLOptions<ExtractParams<Pattern>>] | []
|
|
193
|
+
: [Route.URLOptions<ExtractParams<Pattern>>]
|
|
194
|
+
) {
|
|
195
|
+
const pathname = this.pathname(
|
|
196
|
+
// @ts-expect-error - do not have to pass in {} if no params
|
|
197
|
+
options?.params,
|
|
198
|
+
);
|
|
199
|
+
let search = "";
|
|
200
|
+
let hash = "";
|
|
201
|
+
|
|
202
|
+
if (options?.search) {
|
|
203
|
+
// use the value as the init
|
|
204
|
+
// @ts-expect-error - see above
|
|
205
|
+
search = "?" + new URLSearchParams(options.search);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (options?.hash) {
|
|
209
|
+
// adding # prefix if not present matches the URL setter:
|
|
210
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/URL/hash
|
|
211
|
+
if (options.hash.startsWith("#")) {
|
|
212
|
+
hash = options.hash;
|
|
213
|
+
} else {
|
|
214
|
+
hash = "#" + options.hash;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return pathname + search + hash;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* @template Params Parameters to create the pathname with
|
|
223
|
+
* @param [params] Parameters to insert
|
|
224
|
+
* @returns Resolved `pathname` with params
|
|
225
|
+
*/
|
|
226
|
+
pathname<Params extends ExtractParams<Pattern>>(
|
|
227
|
+
...[params]: keyof Params extends never ? [] : [Params]
|
|
228
|
+
): InsertParams<Pattern, Params> {
|
|
229
|
+
if (!params) return this.pattern as InsertParams<Pattern, Params>;
|
|
230
|
+
|
|
231
|
+
return this.#parts
|
|
232
|
+
.map((part) => {
|
|
233
|
+
if (part.startsWith(":")) {
|
|
234
|
+
return params[part.slice(1) as keyof typeof params];
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (part === "*") return params[part as keyof typeof params];
|
|
238
|
+
|
|
239
|
+
return part;
|
|
240
|
+
})
|
|
241
|
+
.join("/") as InsertParams<Pattern, Params>;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* @template Pattern Route pattern
|
|
246
|
+
* @param route Route to add components to
|
|
247
|
+
* @returns Route with added components
|
|
248
|
+
*/
|
|
249
|
+
static #withComponents<Pattern extends string>(route: Route<Pattern>) {
|
|
250
|
+
const enctype = route.method === "POST" ? "multipart/form-data" : undefined;
|
|
251
|
+
|
|
252
|
+
return Object.assign(route, {
|
|
253
|
+
Button: (({ params, search, hash, ...rest }) =>
|
|
254
|
+
jsx("button", {
|
|
255
|
+
formaction: route.url({ params, search, hash } as Route.URLOptions<
|
|
256
|
+
ExtractParams<Pattern>
|
|
257
|
+
>),
|
|
258
|
+
formmethod: route.method,
|
|
259
|
+
formenctype: enctype,
|
|
260
|
+
...rest,
|
|
261
|
+
})) as Route.Button<Pattern>,
|
|
262
|
+
Form: (({ params, search, hash, ...rest }) =>
|
|
263
|
+
jsx("form", {
|
|
264
|
+
action: route.url({ params, search, hash } as Route.URLOptions<
|
|
265
|
+
ExtractParams<Pattern>
|
|
266
|
+
>),
|
|
267
|
+
method: route.method,
|
|
268
|
+
enctype,
|
|
269
|
+
...rest,
|
|
270
|
+
})) as Route.Form<Pattern>,
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* @template Pattern Route pattern
|
|
276
|
+
* @param pattern Route pattern
|
|
277
|
+
* @param middleware GET middleware
|
|
278
|
+
* @returns GET `Route` with added components
|
|
279
|
+
*/
|
|
280
|
+
static get<Pattern extends string>(
|
|
281
|
+
pattern: Pattern,
|
|
282
|
+
...middleware: Middleware<ExtractParams<Pattern>>[]
|
|
283
|
+
): Route<Pattern> &
|
|
284
|
+
WithButton<Pattern> &
|
|
285
|
+
WithForm<Pattern> &
|
|
286
|
+
WithAnchor<Pattern> {
|
|
287
|
+
const route = Route.#withComponents(
|
|
288
|
+
new Route("GET", pattern, ...middleware),
|
|
289
|
+
);
|
|
290
|
+
|
|
291
|
+
return Object.assign(route, {
|
|
292
|
+
Anchor: (({ params, search, hash, ...rest }) =>
|
|
293
|
+
jsx("a", {
|
|
294
|
+
href: route.url({ params, search, hash } as Route.URLOptions<
|
|
295
|
+
ExtractParams<Pattern>
|
|
296
|
+
>),
|
|
297
|
+
...rest,
|
|
298
|
+
})) as Route.Anchor<Pattern>,
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* @param middleware POST middleware
|
|
304
|
+
* @returns POST `Route` with added components
|
|
305
|
+
*/
|
|
306
|
+
static post(middleware: Middleware<{}>): Route & WithButton & WithForm;
|
|
307
|
+
/**
|
|
308
|
+
* @template Pattern Route pattern
|
|
309
|
+
* @param pattern Route pattern
|
|
310
|
+
* @param middleware POST middleware
|
|
311
|
+
* @returns POST `Route` with added components
|
|
312
|
+
*/
|
|
313
|
+
static post<Pattern extends string>(
|
|
314
|
+
pattern: Pattern,
|
|
315
|
+
...middleware: Middleware<ExtractParams<Pattern>>[]
|
|
316
|
+
): Route<Pattern> & WithButton<Pattern> & WithForm<Pattern>;
|
|
317
|
+
static post<Pattern extends string>(
|
|
318
|
+
patternOrMiddleware: Pattern | Middleware<ExtractParams<Pattern>>,
|
|
319
|
+
...middleware: Middleware<ExtractParams<Pattern>>[]
|
|
320
|
+
) {
|
|
321
|
+
let pattern: Pattern;
|
|
322
|
+
|
|
323
|
+
if (typeof patternOrMiddleware === "string") {
|
|
324
|
+
pattern = patternOrMiddleware;
|
|
325
|
+
} else {
|
|
326
|
+
middleware.unshift(patternOrMiddleware);
|
|
327
|
+
pattern = `/_p/${Hash.djb2(middleware.join())}` as Pattern;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
return Route.#withComponents(new Route("POST", pattern, ...middleware));
|
|
331
|
+
}
|
|
332
|
+
}
|