ovr 3.0.1 → 4.1.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/app/context.d.ts.map +1 -1
- package/dist/app/context.js +46 -27
- package/dist/app/{page.d.ts → get.d.ts} +11 -15
- package/dist/app/get.d.ts.map +1 -0
- package/dist/app/{page.js → get.js} +23 -6
- package/dist/app/index.d.ts +4 -4
- package/dist/app/index.d.ts.map +1 -1
- package/dist/app/index.js +6 -6
- package/dist/app/{action.d.ts → post.d.ts} +13 -19
- package/dist/app/post.d.ts.map +1 -0
- package/dist/app/{action.js → post.js} +12 -2
- package/dist/index.d.ts +2 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/jsx/chunk/index.d.ts.map +1 -1
- package/dist/jsx/chunk/index.js +1 -4
- package/dist/jsx/index.d.ts.map +1 -1
- package/dist/jsx/index.js +9 -0
- package/dist/jsx/merge-async-generators.d.ts +1 -5
- package/dist/jsx/merge-async-generators.d.ts.map +1 -1
- package/dist/jsx/merge-async-generators.js +10 -11
- package/dist/types/index.d.ts +17 -0
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/app/context.ts +48 -27
- package/src/app/get.ts +82 -0
- package/src/app/index.ts +7 -7
- package/src/app/{action.ts → post.ts} +24 -16
- package/src/index.ts +2 -3
- package/src/jsx/chunk/index.ts +1 -3
- package/src/jsx/index.ts +12 -0
- package/src/jsx/merge-async-generators.ts +14 -12
- package/src/types/index.ts +12 -0
- package/dist/app/action.d.ts.map +0 -1
- package/dist/app/page.d.ts.map +0 -1
- package/src/app/page.ts +0 -56
|
@@ -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,KAAK,EAAE,MAAM,kBAAkB,CAAC;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G,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAS;IAK1E;;OAEG;IACH,IAAI,CAAC,GAAG,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE;IAI9B;;OAEG;IACH,MAAM,CAAC,GAAG,MAAM,EAAE,MAAM,EAAE;IAI1B;;;;;OAKG;IACH,IAAI,CAAC,IAAI,EAAE,GAAG,CAAC,OAAO,EAAE,MAAM,CAAC,EAAE,MAAM;IAgEvC;;;;;;;;OAQG;IACH,IAAI,CAAC,CAAC,EAAE,MAAM;IAed;;OAEG;IACH,KAAK;IA+BL;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,MAAM,CAAC,GAAG;CAUV"}
|
package/dist/app/context.js
CHANGED
|
@@ -136,38 +136,52 @@ export class Context {
|
|
|
136
136
|
* @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
|
|
137
137
|
*/
|
|
138
138
|
page(Page, status) {
|
|
139
|
-
for (let i = this.#layouts.length - 1; i >= 0; i--)
|
|
139
|
+
for (let i = this.#layouts.length - 1; i >= 0; i--) {
|
|
140
140
|
Page = this.#layouts[i]({ children: Page });
|
|
141
|
-
const headClose = "</head>";
|
|
142
|
-
const bodyClose = "</body>";
|
|
143
|
-
const elements = this.base.split(headClose);
|
|
144
|
-
if (!elements[1])
|
|
145
|
-
throw new TagNotFound(headClose);
|
|
146
|
-
elements.splice(1, 0, this.#headElements, headClose);
|
|
147
|
-
const bodyParts = elements[3].split(bodyClose);
|
|
148
|
-
if (!bodyParts[1])
|
|
149
|
-
throw new TagNotFound(bodyClose);
|
|
150
|
-
const userAgent = this.req.headers.get("user-agent");
|
|
151
|
-
if (userAgent?.includes("Safari") && !userAgent.includes("Chrome")) {
|
|
152
|
-
// https://bugs.webkit.org/show_bug.cgi?id=252413
|
|
153
|
-
// https://github.com/sveltejs/kit/issues/10315
|
|
154
|
-
// https://github.com/remix-run/remix/issues/5804
|
|
155
|
-
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>`;
|
|
156
141
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
142
|
+
let gen;
|
|
143
|
+
if (this.base) {
|
|
144
|
+
// inject into base
|
|
145
|
+
const headClose = "</head>";
|
|
146
|
+
const bodyClose = "</body>";
|
|
147
|
+
const elements = this.base.split(headClose);
|
|
148
|
+
if (!elements[1])
|
|
149
|
+
throw new TagNotFound(headClose);
|
|
150
|
+
elements.splice(1, 0, this.#headElements, headClose);
|
|
151
|
+
const bodyParts = elements[3].split(bodyClose);
|
|
152
|
+
if (!bodyParts[1])
|
|
153
|
+
throw new TagNotFound(bodyClose);
|
|
154
|
+
const userAgent = this.req.headers.get("user-agent");
|
|
155
|
+
if (userAgent?.includes("Safari") && !userAgent.includes("Chrome")) {
|
|
156
|
+
// https://bugs.webkit.org/show_bug.cgi?id=252413
|
|
157
|
+
// https://github.com/sveltejs/kit/issues/10315
|
|
158
|
+
// https://github.com/remix-run/remix/issues/5804
|
|
159
|
+
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>`;
|
|
160
|
+
}
|
|
161
|
+
elements[3] = bodyParts[0];
|
|
162
|
+
elements.push(Page, bodyClose + bodyParts[1]);
|
|
163
|
+
gen = toGenerator(elements.map((el) => {
|
|
164
|
+
if (typeof el === "string")
|
|
165
|
+
return new Chunk(el, true);
|
|
166
|
+
return el;
|
|
167
|
+
}));
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
// HTML partial - just use the layouts + page
|
|
171
|
+
gen = toGenerator(Page);
|
|
172
|
+
}
|
|
164
173
|
this.html(new ReadableStream({
|
|
165
|
-
async
|
|
166
|
-
|
|
167
|
-
|
|
174
|
+
pull: async (c) => {
|
|
175
|
+
if (this.req.signal.aborted) {
|
|
176
|
+
c.close();
|
|
177
|
+
gen.return();
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
const result = await gen.next();
|
|
181
|
+
if (result.done)
|
|
168
182
|
c.close();
|
|
169
183
|
else
|
|
170
|
-
c.enqueue(value
|
|
184
|
+
c.enqueue(result.value.value);
|
|
171
185
|
},
|
|
172
186
|
cancel() {
|
|
173
187
|
gen.return();
|
|
@@ -197,6 +211,11 @@ export class Context {
|
|
|
197
211
|
* @returns the constructed `Response`
|
|
198
212
|
*/
|
|
199
213
|
build() {
|
|
214
|
+
if (this.req.signal.aborted) {
|
|
215
|
+
// user cancelled the request
|
|
216
|
+
this.body = null;
|
|
217
|
+
this.status = 408;
|
|
218
|
+
}
|
|
200
219
|
if ((!this.status || this.status === 404) &&
|
|
201
220
|
this.#trailingSlash !== "ignore") {
|
|
202
221
|
const last = this.url.pathname.at(-1);
|
|
@@ -1,12 +1,7 @@
|
|
|
1
1
|
import { type JSX } from "../jsx/index.js";
|
|
2
|
-
import type { ExtractParams } from "../types/index.js";
|
|
3
|
-
import type { Middleware
|
|
4
|
-
|
|
5
|
-
params?: never;
|
|
6
|
-
} : {
|
|
7
|
-
params: P;
|
|
8
|
-
});
|
|
9
|
-
export declare class Page<Pattern extends string = string> {
|
|
2
|
+
import type { AnchorProps, ButtonProps, ExtractParams, FormProps } from "../types/index.js";
|
|
3
|
+
import type { Middleware } from "./index.js";
|
|
4
|
+
export declare class Get<Pattern extends string = string> {
|
|
10
5
|
#private;
|
|
11
6
|
/** Route pattern */
|
|
12
7
|
pattern: Pattern;
|
|
@@ -14,6 +9,10 @@ export declare class Page<Pattern extends string = string> {
|
|
|
14
9
|
middleware: Middleware<any>[];
|
|
15
10
|
/** `<a>` component with preset `href` attribute. */
|
|
16
11
|
Anchor: (props: AnchorProps<ExtractParams<Pattern>>) => JSX.Element;
|
|
12
|
+
/** `<button>` component with preset `formaction` and `formmethod` attributes. */
|
|
13
|
+
Button: (props: ButtonProps<ExtractParams<Pattern>>) => JSX.Element;
|
|
14
|
+
/** `<form>` component with preset `method` and `action` attributes. */
|
|
15
|
+
Form: (props: FormProps<ExtractParams<Pattern>>) => JSX.Element;
|
|
17
16
|
/**
|
|
18
17
|
* @param pattern Route pattern
|
|
19
18
|
* @param middleware GET middleware
|
|
@@ -21,10 +20,9 @@ export declare class Page<Pattern extends string = string> {
|
|
|
21
20
|
* @example
|
|
22
21
|
*
|
|
23
22
|
* ```tsx
|
|
24
|
-
*
|
|
25
|
-
* import { Page } from "ovr";
|
|
23
|
+
* import { Get } from "ovr";
|
|
26
24
|
*
|
|
27
|
-
*
|
|
25
|
+
* const page = new Get("/", () => {
|
|
28
26
|
* return <p>Hello world</p>
|
|
29
27
|
* });
|
|
30
28
|
*
|
|
@@ -32,11 +30,9 @@ export declare class Page<Pattern extends string = string> {
|
|
|
32
30
|
* return <page.Anchor>Home</page.Anchor>
|
|
33
31
|
* }
|
|
34
32
|
*
|
|
35
|
-
*
|
|
36
|
-
* app.add(page); registers the page
|
|
33
|
+
* app.add(page); register
|
|
37
34
|
* ```
|
|
38
35
|
*/
|
|
39
36
|
constructor(pattern: Pattern, ...middleware: Middleware<ExtractParams<Pattern>>[]);
|
|
40
37
|
}
|
|
41
|
-
|
|
42
|
-
//# sourceMappingURL=page.d.ts.map
|
|
38
|
+
//# sourceMappingURL=get.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get.d.ts","sourceRoot":"","sources":["../../src/app/get.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,GAAG,EAAO,MAAM,iBAAiB,CAAC;AAEhD,OAAO,KAAK,EACX,WAAW,EACX,WAAW,EACX,aAAa,EACb,SAAS,EACT,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE7C,qBAAa,GAAG,CAAC,OAAO,SAAS,MAAM,GAAG,MAAM;;IAC/C,oBAAoB;IACpB,OAAO,EAAE,OAAO,CAAC;IAEjB,qBAAqB;IACrB,UAAU,EAAE,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;IAE9B,oDAAoD;IACpD,MAAM,EAAE,CAAC,KAAK,EAAE,WAAW,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,KAAK,GAAG,CAAC,OAAO,CAAC;IAEpE,iFAAiF;IACjF,MAAM,EAAE,CAAC,KAAK,EAAE,WAAW,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,KAAK,GAAG,CAAC,OAAO,CAAC;IAEpE,uEAAuE;IACvE,IAAI,EAAE,CAAC,KAAK,EAAE,SAAS,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,KAAK,GAAG,CAAC,OAAO,CAAC;IAIhE;;;;;;;;;;;;;;;;;;;OAmBG;gBAEF,OAAO,EAAE,OAAO,EAChB,GAAG,UAAU,EAAE,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,EAAE;CA+BpD"}
|
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import { jsx } from "../jsx/index.js";
|
|
2
2
|
import { insertParams } from "../trie/insert-params.js";
|
|
3
|
-
export class
|
|
3
|
+
export class Get {
|
|
4
4
|
/** Route pattern */
|
|
5
5
|
pattern;
|
|
6
6
|
/** GET middleware */
|
|
7
7
|
middleware;
|
|
8
8
|
/** `<a>` component with preset `href` attribute. */
|
|
9
9
|
Anchor;
|
|
10
|
+
/** `<button>` component with preset `formaction` and `formmethod` attributes. */
|
|
11
|
+
Button;
|
|
12
|
+
/** `<form>` component with preset `method` and `action` attributes. */
|
|
13
|
+
Form;
|
|
10
14
|
#parts;
|
|
11
15
|
/**
|
|
12
16
|
* @param pattern Route pattern
|
|
@@ -15,10 +19,9 @@ export class Page {
|
|
|
15
19
|
* @example
|
|
16
20
|
*
|
|
17
21
|
* ```tsx
|
|
18
|
-
*
|
|
19
|
-
* import { Page } from "ovr";
|
|
22
|
+
* import { Get } from "ovr";
|
|
20
23
|
*
|
|
21
|
-
*
|
|
24
|
+
* const page = new Get("/", () => {
|
|
22
25
|
* return <p>Hello world</p>
|
|
23
26
|
* });
|
|
24
27
|
*
|
|
@@ -26,8 +29,7 @@ export class Page {
|
|
|
26
29
|
* return <page.Anchor>Home</page.Anchor>
|
|
27
30
|
* }
|
|
28
31
|
*
|
|
29
|
-
*
|
|
30
|
-
* app.add(page); registers the page
|
|
32
|
+
* app.add(page); register
|
|
31
33
|
* ```
|
|
32
34
|
*/
|
|
33
35
|
constructor(pattern, ...middleware) {
|
|
@@ -38,5 +40,20 @@ export class Page {
|
|
|
38
40
|
const { params = {}, ...rest } = props;
|
|
39
41
|
return jsx("a", { href: insertParams(this.#parts, params), ...rest });
|
|
40
42
|
};
|
|
43
|
+
this.Button = (props) => {
|
|
44
|
+
const { params = {}, ...rest } = props;
|
|
45
|
+
return jsx("button", {
|
|
46
|
+
formaction: insertParams(this.#parts, params),
|
|
47
|
+
formmethod: "get",
|
|
48
|
+
...rest,
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
this.Form = (props) => {
|
|
52
|
+
const { params = {}, ...rest } = props;
|
|
53
|
+
return jsx("form", {
|
|
54
|
+
action: insertParams(this.#parts, params),
|
|
55
|
+
...rest,
|
|
56
|
+
});
|
|
57
|
+
};
|
|
41
58
|
}
|
|
42
59
|
}
|
package/dist/app/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { DeepArray, ExtractMultiParams, ExtractParams } from "../types/index.js";
|
|
2
|
-
import { Action } from "./action.js";
|
|
3
2
|
import { Context } from "./context.js";
|
|
4
|
-
import {
|
|
3
|
+
import { Get } from "./get.js";
|
|
4
|
+
import { Post } from "./post.js";
|
|
5
5
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
6
6
|
export type Params = Record<string, string>;
|
|
7
7
|
export type Middleware<P extends Params = Params> = (context: Context<P>, next: () => Promise<void>) => any;
|
|
@@ -57,10 +57,10 @@ export declare class App {
|
|
|
57
57
|
/** Stores context per request. */
|
|
58
58
|
static storage: AsyncLocalStorage<Context<Params>>;
|
|
59
59
|
/**
|
|
60
|
-
* @param routes `
|
|
60
|
+
* @param routes `Get` or `Post` to add to `App`
|
|
61
61
|
* @returns `App` instance
|
|
62
62
|
*/
|
|
63
|
-
add(...routes: DeepArray<
|
|
63
|
+
add(...routes: DeepArray<Get | Post | Record<string, Get | Post>>[]): this;
|
|
64
64
|
/**
|
|
65
65
|
* Add global middleware.
|
|
66
66
|
*
|
package/dist/app/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/app/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACX,SAAS,EACT,kBAAkB,EAClB,aAAa,EACb,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/app/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACX,SAAS,EACT,kBAAkB,EAClB,aAAa,EACb,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAErD,MAAM,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAE5C,MAAM,MAAM,UAAU,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,IAAI,CACnD,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,EACnB,IAAI,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,KACrB,GAAG,CAAC;AAET,MAAM,MAAM,aAAa,GAAG,QAAQ,G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C;CACF"}
|
package/dist/app/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Route, Trie } from "../trie/index.js";
|
|
2
|
-
import { Action } from "./action.js";
|
|
3
2
|
import { Context } from "./context.js";
|
|
4
|
-
import {
|
|
3
|
+
import { Get } from "./get.js";
|
|
4
|
+
import { Post } from "./post.js";
|
|
5
5
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
6
6
|
export class App {
|
|
7
7
|
/** Built tries per HTTP method. */
|
|
@@ -54,7 +54,7 @@ export class App {
|
|
|
54
54
|
/** Stores context per request. */
|
|
55
55
|
static storage = new AsyncLocalStorage();
|
|
56
56
|
/**
|
|
57
|
-
* @param routes `
|
|
57
|
+
* @param routes `Get` or `Post` to add to `App`
|
|
58
58
|
* @returns `App` instance
|
|
59
59
|
*/
|
|
60
60
|
add(...routes) {
|
|
@@ -62,10 +62,10 @@ export class App {
|
|
|
62
62
|
if (route instanceof Array) {
|
|
63
63
|
this.add(...route);
|
|
64
64
|
}
|
|
65
|
-
else if (route instanceof
|
|
65
|
+
else if (route instanceof Get) {
|
|
66
66
|
this.get(route.pattern, ...route.middleware);
|
|
67
67
|
}
|
|
68
|
-
else if (route instanceof
|
|
68
|
+
else if (route instanceof Post) {
|
|
69
69
|
this.post(route.pattern, ...route.middleware);
|
|
70
70
|
}
|
|
71
71
|
else {
|
|
@@ -148,7 +148,7 @@ export class App {
|
|
|
148
148
|
c.res(result.body, result);
|
|
149
149
|
else if (result instanceof ReadableStream)
|
|
150
150
|
c.body = result;
|
|
151
|
-
else if (result)
|
|
151
|
+
else if (result != null)
|
|
152
152
|
c.page(result);
|
|
153
153
|
}
|
|
154
154
|
};
|
|
@@ -1,17 +1,14 @@
|
|
|
1
1
|
import { type JSX } from "../jsx/index.js";
|
|
2
|
-
import type { ExtractParams } from "../types/index.js";
|
|
3
|
-
import type { Middleware
|
|
4
|
-
|
|
5
|
-
params?: never;
|
|
6
|
-
} : {
|
|
7
|
-
params: P;
|
|
8
|
-
});
|
|
9
|
-
export declare class Action<Pattern extends string = string> {
|
|
2
|
+
import type { ButtonProps, ExtractParams, FormProps } from "../types/index.js";
|
|
3
|
+
import type { Middleware } from "./index.js";
|
|
4
|
+
export declare class Post<Pattern extends string = string> {
|
|
10
5
|
#private;
|
|
11
6
|
/** Route pattern */
|
|
12
7
|
pattern: Pattern;
|
|
13
8
|
/** POST middleware */
|
|
14
9
|
middleware: Middleware<any>[];
|
|
10
|
+
/** `<button>` component with preset `formaction` and `formmethod` attributes. */
|
|
11
|
+
Button: (props: ButtonProps<ExtractParams<Pattern>>) => JSX.Element;
|
|
15
12
|
/** `<form>` component with preset `method` and `action` attributes. */
|
|
16
13
|
Form: (props: FormProps<ExtractParams<Pattern>>) => JSX.Element;
|
|
17
14
|
/**
|
|
@@ -20,23 +17,21 @@ export declare class Action<Pattern extends string = string> {
|
|
|
20
17
|
* @example
|
|
21
18
|
*
|
|
22
19
|
* ```tsx
|
|
23
|
-
*
|
|
24
|
-
* import { Action } from "ovr";
|
|
20
|
+
* import { Post } from "ovr";
|
|
25
21
|
*
|
|
26
|
-
*
|
|
22
|
+
* const post = new Post((c) => {
|
|
27
23
|
* console.log("posted");
|
|
28
24
|
* c.redirect("/", 303);
|
|
29
25
|
* });
|
|
30
26
|
*
|
|
31
|
-
*
|
|
32
|
-
* <
|
|
33
|
-
* <input />
|
|
27
|
+
* const page = new Get("/", () => (
|
|
28
|
+
* <post.Form>
|
|
29
|
+
* <input type="text" name="name" />
|
|
34
30
|
* <button>Submit</button>
|
|
35
|
-
* </
|
|
31
|
+
* </post.Form>
|
|
36
32
|
* ));
|
|
37
33
|
*
|
|
38
|
-
*
|
|
39
|
-
* app.add(page, action); registers the action
|
|
34
|
+
* app.add(page, post); register
|
|
40
35
|
* ```
|
|
41
36
|
*/
|
|
42
37
|
constructor(...middleware: Middleware<{}>[]);
|
|
@@ -46,5 +41,4 @@ export declare class Action<Pattern extends string = string> {
|
|
|
46
41
|
*/
|
|
47
42
|
constructor(pattern: Pattern, ...middleware: Middleware<ExtractParams<Pattern>>[]);
|
|
48
43
|
}
|
|
49
|
-
|
|
50
|
-
//# sourceMappingURL=action.d.ts.map
|
|
44
|
+
//# sourceMappingURL=post.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"post.d.ts","sourceRoot":"","sources":["../../src/app/post.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,GAAG,EAAO,MAAM,iBAAiB,CAAC;AAEhD,OAAO,KAAK,EAAE,WAAW,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAE/E,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE7C,qBAAa,IAAI,CAAC,OAAO,SAAS,MAAM,GAAG,MAAM;;IAChD,oBAAoB;IACpB,OAAO,EAAE,OAAO,CAAC;IAEjB,sBAAsB;IACtB,UAAU,EAAE,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;IAE9B,iFAAiF;IACjF,MAAM,EAAE,CAAC,KAAK,EAAE,WAAW,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,KAAK,GAAG,CAAC,OAAO,CAAC;IAEpE,uEAAuE;IACvE,IAAI,EAAE,CAAC,KAAK,EAAE,SAAS,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,KAAK,GAAG,CAAC,OAAO,CAAC;IAIhE;;;;;;;;;;;;;;;;;;;;;;OAsBG;gBACS,GAAG,UAAU,EAAE,UAAU,CAAC,EAAE,CAAC,EAAE;IAC3C;;;OAGG;gBAEF,OAAO,EAAE,OAAO,EAChB,GAAG,UAAU,EAAE,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,EAAE;CAqCpD"}
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { jsx } from "../jsx/index.js";
|
|
2
2
|
import { insertParams } from "../trie/insert-params.js";
|
|
3
3
|
import { hash } from "../util/hash.js";
|
|
4
|
-
export class
|
|
4
|
+
export class Post {
|
|
5
5
|
/** Route pattern */
|
|
6
6
|
pattern;
|
|
7
7
|
/** POST middleware */
|
|
8
8
|
middleware;
|
|
9
|
+
/** `<button>` component with preset `formaction` and `formmethod` attributes. */
|
|
10
|
+
Button;
|
|
9
11
|
/** `<form>` component with preset `method` and `action` attributes. */
|
|
10
12
|
Form;
|
|
11
13
|
#parts = [];
|
|
@@ -16,9 +18,17 @@ export class Action {
|
|
|
16
18
|
}
|
|
17
19
|
else {
|
|
18
20
|
this.middleware.unshift(patternOrMiddleware);
|
|
19
|
-
this.pattern = `/
|
|
21
|
+
this.pattern = `/_p/${hash(this.middleware.join())}`;
|
|
20
22
|
}
|
|
21
23
|
this.#parts = this.pattern.split("/");
|
|
24
|
+
this.Button = (props) => {
|
|
25
|
+
const { params = {}, ...rest } = props;
|
|
26
|
+
return jsx("button", {
|
|
27
|
+
formaction: insertParams(this.#parts, params),
|
|
28
|
+
formmethod: "post",
|
|
29
|
+
...rest,
|
|
30
|
+
});
|
|
31
|
+
};
|
|
22
32
|
this.Form = (props) => {
|
|
23
33
|
const { params = {}, ...rest } = props;
|
|
24
34
|
return jsx("form", {
|
package/dist/index.d.ts
CHANGED
|
@@ -3,8 +3,7 @@ export { Chunk } from "./jsx/chunk/index.js";
|
|
|
3
3
|
export { Trie, Route } from "./trie/index.js";
|
|
4
4
|
export { App, type Middleware, type Params } from "./app/index.js";
|
|
5
5
|
export { Context } from "./app/context.js";
|
|
6
|
-
export {
|
|
7
|
-
export {
|
|
6
|
+
export { Get } from "./app/get.js";
|
|
7
|
+
export { Post } from "./app/post.js";
|
|
8
8
|
export { csrf } from "./app/mw/csrf.js";
|
|
9
|
-
export type * from "./types/index.js";
|
|
10
9
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,WAAW,EAAE,QAAQ,EAAE,KAAK,GAAG,EAAE,MAAM,gBAAgB,CAAC;AACtE,OAAO,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAC7C,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAE,GAAG,EAAE,KAAK,UAAU,EAAE,KAAK,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACnE,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAC3C,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,WAAW,EAAE,QAAQ,EAAE,KAAK,GAAG,EAAE,MAAM,gBAAgB,CAAC;AACtE,OAAO,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAC7C,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAE,GAAG,EAAE,KAAK,UAAU,EAAE,KAAK,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACnE,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAC3C,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AACnC,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AACrC,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -3,6 +3,6 @@ export { Chunk } from "./jsx/chunk/index.js";
|
|
|
3
3
|
export { Trie, Route } from "./trie/index.js";
|
|
4
4
|
export { App } from "./app/index.js";
|
|
5
5
|
export { Context } from "./app/context.js";
|
|
6
|
-
export {
|
|
7
|
-
export {
|
|
6
|
+
export { Get } from "./app/get.js";
|
|
7
|
+
export { Post } from "./app/post.js";
|
|
8
8
|
export { csrf } from "./app/mw/csrf.js";
|
|
@@ -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,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,OAAO;
|
|
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,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,OAAO;IAKtC;;OAEG;IACH,QAAQ;IAIR;;OAEG;IACH,MAAM,CAAC,GAAG,MAAM,EAAE,KAAK,EAAE;IAIzB;;;;;;OAMG;IACH,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO;CA4BvC"}
|
package/dist/jsx/chunk/index.js
CHANGED
|
@@ -11,10 +11,7 @@ export class Chunk {
|
|
|
11
11
|
*/
|
|
12
12
|
constructor(s, safe) {
|
|
13
13
|
const value = String(s ?? "");
|
|
14
|
-
|
|
15
|
-
this.value = value;
|
|
16
|
-
else
|
|
17
|
-
this.value = Chunk.escape(value);
|
|
14
|
+
this.value = safe ? value : Chunk.escape(value);
|
|
18
15
|
}
|
|
19
16
|
/**
|
|
20
17
|
* @returns `value`
|
package/dist/jsx/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/jsx/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACrE,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACzC,OAAO,KAAK,EAAE,iBAAiB,IAAI,EAAE,EAAE,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OAAO,oBAIlD,CAAC"}
|
package/dist/jsx/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Chunk } from "./chunk/index.js";
|
|
2
2
|
import { merge } from "./merge-async-generators.js";
|
|
3
|
+
import { setImmediate } from "node:timers/promises";
|
|
3
4
|
/**
|
|
4
5
|
* These are the HTML tags that do not require a closing tag.
|
|
5
6
|
*
|
|
@@ -105,11 +106,19 @@ export async function* toGenerator(element) {
|
|
|
105
106
|
typeof iterator.return === "function") {
|
|
106
107
|
// sync generator
|
|
107
108
|
// process lazily - avoids loading all in memory
|
|
109
|
+
const yieldIterations = 50;
|
|
110
|
+
let yieldCounter = yieldIterations;
|
|
108
111
|
while (true) {
|
|
109
112
|
const result = iterator.next();
|
|
110
113
|
if (result.done)
|
|
111
114
|
break;
|
|
112
115
|
yield* toGenerator(result.value);
|
|
116
|
+
if (--yieldCounter === 0) {
|
|
117
|
+
// yields back to the event loop to send chunks
|
|
118
|
+
// or check if the request has been cancelled
|
|
119
|
+
await setImmediate();
|
|
120
|
+
yieldCounter = yieldIterations;
|
|
121
|
+
}
|
|
113
122
|
}
|
|
114
123
|
return;
|
|
115
124
|
}
|
|
@@ -5,11 +5,7 @@
|
|
|
5
5
|
* Adapted from [stack overflow answers](https://stackoverflow.com/questions/50585456).
|
|
6
6
|
*
|
|
7
7
|
* @param generators Resolved in parallel.
|
|
8
|
-
* @param returnValue Passed into each `iterator.return()`
|
|
9
8
|
* @yields `IteratorResult` and `index` of the resolved generator.
|
|
10
9
|
*/
|
|
11
|
-
export declare function merge<T>(generators: AsyncGenerator<T, void>[]): AsyncGenerator<
|
|
12
|
-
index: number;
|
|
13
|
-
result: IteratorResult<T, void>;
|
|
14
|
-
}, void, unknown>;
|
|
10
|
+
export declare function merge<T>(generators: AsyncGenerator<T, void>[]): AsyncGenerator<any, void, unknown>;
|
|
15
11
|
//# sourceMappingURL=merge-async-generators.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"merge-async-generators.d.ts","sourceRoot":"","sources":["../../src/jsx/merge-async-generators.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"merge-async-generators.d.ts","sourceRoot":"","sources":["../../src/jsx/merge-async-generators.ts"],"names":[],"mappings":"AAKA;;;;;;;;GAQG;AACH,wBAAuB,KAAK,CAAC,CAAC,EAAE,UAAU,EAAE,cAAc,CAAC,CAAC,EAAE,IAAI,CAAC,EAAE,sCA6BpE"}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
const never = new Promise(() => { });
|
|
2
1
|
const next = async (iterator, index) => ({
|
|
3
2
|
index,
|
|
4
3
|
result: await iterator.next(),
|
|
@@ -10,28 +9,28 @@ const next = async (iterator, index) => ({
|
|
|
10
9
|
* Adapted from [stack overflow answers](https://stackoverflow.com/questions/50585456).
|
|
11
10
|
*
|
|
12
11
|
* @param generators Resolved in parallel.
|
|
13
|
-
* @param returnValue Passed into each `iterator.return()`
|
|
14
12
|
* @yields `IteratorResult` and `index` of the resolved generator.
|
|
15
13
|
*/
|
|
16
14
|
export async function* merge(generators) {
|
|
17
15
|
const iterators = generators.map((gen) => gen[Symbol.asyncIterator]());
|
|
18
|
-
const promises =
|
|
19
|
-
|
|
16
|
+
const promises = new Map();
|
|
17
|
+
iterators.forEach((iterator, index) => promises.set(index, next(iterator, index)));
|
|
20
18
|
let current;
|
|
21
19
|
try {
|
|
22
|
-
while (
|
|
23
|
-
yield (current = await Promise.race(promises));
|
|
20
|
+
while (promises.size > 0) {
|
|
21
|
+
yield (current = await Promise.race(promises.values()));
|
|
24
22
|
if (current.result.done) {
|
|
25
|
-
promises
|
|
26
|
-
remaining--;
|
|
23
|
+
promises.delete(current.index);
|
|
27
24
|
}
|
|
28
25
|
else {
|
|
29
|
-
promises
|
|
26
|
+
promises.set(current.index, next(iterators[current.index], current.index));
|
|
30
27
|
}
|
|
31
28
|
}
|
|
32
29
|
}
|
|
33
30
|
finally {
|
|
34
|
-
for (const
|
|
35
|
-
|
|
31
|
+
for (const iterator of iterators) {
|
|
32
|
+
// catch - could have already returned
|
|
33
|
+
iterator.return().catch(() => { });
|
|
34
|
+
}
|
|
36
35
|
}
|
|
37
36
|
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { Params } from "../app/index.js";
|
|
2
|
+
import type { JSX } from "../jsx/index.js";
|
|
1
3
|
export type MaybePromise<T> = T | Promise<T>;
|
|
2
4
|
export type MaybeFunction<T> = T | (() => T);
|
|
3
5
|
export type DeepArray<T> = T | DeepArray<T>[];
|
|
@@ -12,4 +14,19 @@ export type ExtractMultiParams<Patterns extends string[]> = Patterns extends [
|
|
|
12
14
|
infer First extends string,
|
|
13
15
|
...infer Rest extends string[]
|
|
14
16
|
] ? Rest["length"] extends 0 ? ExtractParams<First> : ExtractParams<First> | ExtractMultiParams<Rest> : never;
|
|
17
|
+
export type AnchorProps<P extends Params> = JSX.IntrinsicElements["a"] & (keyof P extends never ? {
|
|
18
|
+
params?: never;
|
|
19
|
+
} : {
|
|
20
|
+
params: P;
|
|
21
|
+
});
|
|
22
|
+
export type FormProps<P extends Params> = JSX.IntrinsicElements["form"] & (keyof P extends never ? {
|
|
23
|
+
params?: never;
|
|
24
|
+
} : {
|
|
25
|
+
params: P;
|
|
26
|
+
});
|
|
27
|
+
export type ButtonProps<P extends Params> = JSX.IntrinsicElements["button"] & (keyof P extends never ? {
|
|
28
|
+
params?: never;
|
|
29
|
+
} : {
|
|
30
|
+
params: P;
|
|
31
|
+
});
|
|
15
32
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +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,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CAAE,GAAG;IAAE,MAAM,EAAE,CAAC,CAAA;CAAE,CAAC,CAAC;AAE9D,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,MAAM,IAAI,GAAG,CAAC,iBAAiB,CAAC,QAAQ,CAAC,GAC1E,CAAC,MAAM,CAAC,SAAS,KAAK,GAAG;IAAE,MAAM,CAAC,EAAE,KAAK,CAAA;CAAE,GAAG;IAAE,MAAM,EAAE,CAAC,CAAA;CAAE,CAAC,CAAC"}
|
package/package.json
CHANGED
package/src/app/context.ts
CHANGED
|
@@ -192,44 +192,59 @@ export class Context<P extends Params = Params> {
|
|
|
192
192
|
* @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
|
|
193
193
|
*/
|
|
194
194
|
page(Page: JSX.Element, status?: number) {
|
|
195
|
-
for (let i = this.#layouts.length - 1; i >= 0; i--)
|
|
195
|
+
for (let i = this.#layouts.length - 1; i >= 0; i--) {
|
|
196
196
|
Page = this.#layouts[i]!({ children: Page });
|
|
197
|
+
}
|
|
197
198
|
|
|
198
|
-
|
|
199
|
-
const bodyClose = "</body>";
|
|
199
|
+
let gen: AsyncGenerator<Chunk, void>;
|
|
200
200
|
|
|
201
|
-
|
|
202
|
-
|
|
201
|
+
if (this.base) {
|
|
202
|
+
// inject into base
|
|
203
|
+
const headClose = "</head>";
|
|
204
|
+
const bodyClose = "</body>";
|
|
203
205
|
|
|
204
|
-
|
|
206
|
+
const elements: JSX.Element[] = this.base.split(headClose);
|
|
207
|
+
if (!elements[1]) throw new TagNotFound(headClose);
|
|
205
208
|
|
|
206
|
-
|
|
207
|
-
if (!bodyParts[1]) throw new TagNotFound(bodyClose);
|
|
209
|
+
elements.splice(1, 0, this.#headElements, headClose);
|
|
208
210
|
|
|
209
|
-
|
|
210
|
-
|
|
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
|
-
}
|
|
211
|
+
const bodyParts = (elements[3] as string).split(bodyClose);
|
|
212
|
+
if (!bodyParts[1]) throw new TagNotFound(bodyClose);
|
|
216
213
|
|
|
217
|
-
|
|
218
|
-
|
|
214
|
+
const userAgent = this.req.headers.get("user-agent");
|
|
215
|
+
if (userAgent?.includes("Safari") && !userAgent.includes("Chrome")) {
|
|
216
|
+
// https://bugs.webkit.org/show_bug.cgi?id=252413
|
|
217
|
+
// https://github.com/sveltejs/kit/issues/10315
|
|
218
|
+
// https://github.com/remix-run/remix/issues/5804
|
|
219
|
+
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>`;
|
|
220
|
+
}
|
|
219
221
|
|
|
220
|
-
|
|
221
|
-
elements.
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
222
|
+
elements[3] = bodyParts[0];
|
|
223
|
+
elements.push(Page, bodyClose + bodyParts[1]);
|
|
224
|
+
|
|
225
|
+
gen = toGenerator(
|
|
226
|
+
elements.map((el) => {
|
|
227
|
+
if (typeof el === "string") return new Chunk(el, true);
|
|
228
|
+
return el;
|
|
229
|
+
}),
|
|
230
|
+
);
|
|
231
|
+
} else {
|
|
232
|
+
// HTML partial - just use the layouts + page
|
|
233
|
+
gen = toGenerator(Page);
|
|
234
|
+
}
|
|
226
235
|
|
|
227
236
|
this.html(
|
|
228
237
|
new ReadableStream<string>({
|
|
229
|
-
async
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
238
|
+
pull: async (c) => {
|
|
239
|
+
if (this.req.signal.aborted) {
|
|
240
|
+
c.close();
|
|
241
|
+
gen.return();
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const result = await gen.next();
|
|
246
|
+
if (result.done) c.close();
|
|
247
|
+
else c.enqueue(result.value.value);
|
|
233
248
|
},
|
|
234
249
|
|
|
235
250
|
cancel() {
|
|
@@ -268,6 +283,12 @@ export class Context<P extends Params = Params> {
|
|
|
268
283
|
* @returns the constructed `Response`
|
|
269
284
|
*/
|
|
270
285
|
build() {
|
|
286
|
+
if (this.req.signal.aborted) {
|
|
287
|
+
// user cancelled the request
|
|
288
|
+
this.body = null;
|
|
289
|
+
this.status = 408;
|
|
290
|
+
}
|
|
291
|
+
|
|
271
292
|
if (
|
|
272
293
|
(!this.status || this.status === 404) &&
|
|
273
294
|
this.#trailingSlash !== "ignore"
|
package/src/app/get.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { type JSX, jsx } from "../jsx/index.js";
|
|
2
|
+
import { insertParams } from "../trie/insert-params.js";
|
|
3
|
+
import type {
|
|
4
|
+
AnchorProps,
|
|
5
|
+
ButtonProps,
|
|
6
|
+
ExtractParams,
|
|
7
|
+
FormProps,
|
|
8
|
+
} from "../types/index.js";
|
|
9
|
+
import type { Middleware } from "./index.js";
|
|
10
|
+
|
|
11
|
+
export class Get<Pattern extends string = string> {
|
|
12
|
+
/** Route pattern */
|
|
13
|
+
pattern: Pattern;
|
|
14
|
+
|
|
15
|
+
/** GET middleware */
|
|
16
|
+
middleware: Middleware<any>[];
|
|
17
|
+
|
|
18
|
+
/** `<a>` component with preset `href` attribute. */
|
|
19
|
+
Anchor: (props: AnchorProps<ExtractParams<Pattern>>) => JSX.Element;
|
|
20
|
+
|
|
21
|
+
/** `<button>` component with preset `formaction` and `formmethod` attributes. */
|
|
22
|
+
Button: (props: ButtonProps<ExtractParams<Pattern>>) => JSX.Element;
|
|
23
|
+
|
|
24
|
+
/** `<form>` component with preset `method` and `action` attributes. */
|
|
25
|
+
Form: (props: FormProps<ExtractParams<Pattern>>) => JSX.Element;
|
|
26
|
+
|
|
27
|
+
#parts: string[];
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @param pattern Route pattern
|
|
31
|
+
* @param middleware GET middleware
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
*
|
|
35
|
+
* ```tsx
|
|
36
|
+
* import { Get } from "ovr";
|
|
37
|
+
*
|
|
38
|
+
* const page = new Get("/", () => {
|
|
39
|
+
* return <p>Hello world</p>
|
|
40
|
+
* });
|
|
41
|
+
*
|
|
42
|
+
* const Nav = () => {
|
|
43
|
+
* return <page.Anchor>Home</page.Anchor>
|
|
44
|
+
* }
|
|
45
|
+
*
|
|
46
|
+
* app.add(page); register
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
constructor(
|
|
50
|
+
pattern: Pattern,
|
|
51
|
+
...middleware: Middleware<ExtractParams<Pattern>>[]
|
|
52
|
+
) {
|
|
53
|
+
this.pattern = pattern;
|
|
54
|
+
this.middleware = middleware;
|
|
55
|
+
this.#parts = pattern.split("/");
|
|
56
|
+
|
|
57
|
+
this.Anchor = (props) => {
|
|
58
|
+
const { params = {}, ...rest } = props;
|
|
59
|
+
|
|
60
|
+
return jsx("a", { href: insertParams(this.#parts, params), ...rest });
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
this.Button = (props) => {
|
|
64
|
+
const { params = {}, ...rest } = props;
|
|
65
|
+
|
|
66
|
+
return jsx("button", {
|
|
67
|
+
formaction: insertParams(this.#parts, params),
|
|
68
|
+
formmethod: "get",
|
|
69
|
+
...rest,
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
this.Form = (props) => {
|
|
74
|
+
const { params = {}, ...rest } = props;
|
|
75
|
+
|
|
76
|
+
return jsx("form", {
|
|
77
|
+
action: insertParams(this.#parts, params),
|
|
78
|
+
...rest,
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
}
|
package/src/app/index.ts
CHANGED
|
@@ -4,9 +4,9 @@ import type {
|
|
|
4
4
|
ExtractMultiParams,
|
|
5
5
|
ExtractParams,
|
|
6
6
|
} from "../types/index.js";
|
|
7
|
-
import { Action } from "./action.js";
|
|
8
7
|
import { Context } from "./context.js";
|
|
9
|
-
import {
|
|
8
|
+
import { Get } from "./get.js";
|
|
9
|
+
import { Post } from "./post.js";
|
|
10
10
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
11
11
|
|
|
12
12
|
export type Params = Record<string, string>;
|
|
@@ -105,16 +105,16 @@ export class App {
|
|
|
105
105
|
static storage = new AsyncLocalStorage<Context>();
|
|
106
106
|
|
|
107
107
|
/**
|
|
108
|
-
* @param routes `
|
|
108
|
+
* @param routes `Get` or `Post` to add to `App`
|
|
109
109
|
* @returns `App` instance
|
|
110
110
|
*/
|
|
111
|
-
add(...routes: DeepArray<
|
|
111
|
+
add(...routes: DeepArray<Get | Post | Record<string, Get | Post>>[]) {
|
|
112
112
|
for (const route of routes) {
|
|
113
113
|
if (route instanceof Array) {
|
|
114
114
|
this.add(...route);
|
|
115
|
-
} else if (route instanceof
|
|
115
|
+
} else if (route instanceof Get) {
|
|
116
116
|
this.get(route.pattern, ...route.middleware);
|
|
117
|
-
} else if (route instanceof
|
|
117
|
+
} else if (route instanceof Post) {
|
|
118
118
|
this.post(route.pattern, ...route.middleware);
|
|
119
119
|
} else {
|
|
120
120
|
this.add(...Object.values(route));
|
|
@@ -284,7 +284,7 @@ export class App {
|
|
|
284
284
|
|
|
285
285
|
if (result instanceof Response) c.res(result.body, result);
|
|
286
286
|
else if (result instanceof ReadableStream) c.body = result;
|
|
287
|
-
else if (result) c.page(result);
|
|
287
|
+
else if (result != null) c.page(result);
|
|
288
288
|
}
|
|
289
289
|
};
|
|
290
290
|
|
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import { type JSX, jsx } from "../jsx/index.js";
|
|
2
2
|
import { insertParams } from "../trie/insert-params.js";
|
|
3
|
-
import type { ExtractParams } from "../types/index.js";
|
|
3
|
+
import type { ButtonProps, ExtractParams, FormProps } from "../types/index.js";
|
|
4
4
|
import { hash } from "../util/hash.js";
|
|
5
|
-
import type { Middleware
|
|
5
|
+
import type { Middleware } from "./index.js";
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
(keyof P extends never ? { params?: never } : { params: P });
|
|
9
|
-
|
|
10
|
-
export class Action<Pattern extends string = string> {
|
|
7
|
+
export class Post<Pattern extends string = string> {
|
|
11
8
|
/** Route pattern */
|
|
12
9
|
pattern: Pattern;
|
|
13
10
|
|
|
14
11
|
/** POST middleware */
|
|
15
12
|
middleware: Middleware<any>[];
|
|
16
13
|
|
|
14
|
+
/** `<button>` component with preset `formaction` and `formmethod` attributes. */
|
|
15
|
+
Button: (props: ButtonProps<ExtractParams<Pattern>>) => JSX.Element;
|
|
16
|
+
|
|
17
17
|
/** `<form>` component with preset `method` and `action` attributes. */
|
|
18
18
|
Form: (props: FormProps<ExtractParams<Pattern>>) => JSX.Element;
|
|
19
19
|
|
|
@@ -25,23 +25,21 @@ export class Action<Pattern extends string = string> {
|
|
|
25
25
|
* @example
|
|
26
26
|
*
|
|
27
27
|
* ```tsx
|
|
28
|
-
*
|
|
29
|
-
* import { Action } from "ovr";
|
|
28
|
+
* import { Post } from "ovr";
|
|
30
29
|
*
|
|
31
|
-
*
|
|
30
|
+
* const post = new Post((c) => {
|
|
32
31
|
* console.log("posted");
|
|
33
32
|
* c.redirect("/", 303);
|
|
34
33
|
* });
|
|
35
34
|
*
|
|
36
|
-
*
|
|
37
|
-
* <
|
|
38
|
-
* <input />
|
|
35
|
+
* const page = new Get("/", () => (
|
|
36
|
+
* <post.Form>
|
|
37
|
+
* <input type="text" name="name" />
|
|
39
38
|
* <button>Submit</button>
|
|
40
|
-
* </
|
|
39
|
+
* </post.Form>
|
|
41
40
|
* ));
|
|
42
41
|
*
|
|
43
|
-
*
|
|
44
|
-
* app.add(page, action); registers the action
|
|
42
|
+
* app.add(page, post); register
|
|
45
43
|
* ```
|
|
46
44
|
*/
|
|
47
45
|
constructor(...middleware: Middleware<{}>[]);
|
|
@@ -63,11 +61,21 @@ export class Action<Pattern extends string = string> {
|
|
|
63
61
|
this.pattern = patternOrMiddleware;
|
|
64
62
|
} else {
|
|
65
63
|
this.middleware.unshift(patternOrMiddleware);
|
|
66
|
-
this.pattern = `/
|
|
64
|
+
this.pattern = `/_p/${hash(this.middleware.join())}` as Pattern;
|
|
67
65
|
}
|
|
68
66
|
|
|
69
67
|
this.#parts = this.pattern.split("/");
|
|
70
68
|
|
|
69
|
+
this.Button = (props) => {
|
|
70
|
+
const { params = {}, ...rest } = props;
|
|
71
|
+
|
|
72
|
+
return jsx("button", {
|
|
73
|
+
formaction: insertParams(this.#parts, params),
|
|
74
|
+
formmethod: "post",
|
|
75
|
+
...rest,
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
|
|
71
79
|
this.Form = (props) => {
|
|
72
80
|
const { params = {}, ...rest } = props;
|
|
73
81
|
|
package/src/index.ts
CHANGED
|
@@ -3,7 +3,6 @@ export { Chunk } from "./jsx/chunk/index.js";
|
|
|
3
3
|
export { Trie, Route } from "./trie/index.js";
|
|
4
4
|
export { App, type Middleware, type Params } from "./app/index.js";
|
|
5
5
|
export { Context } from "./app/context.js";
|
|
6
|
-
export {
|
|
7
|
-
export {
|
|
6
|
+
export { Get } from "./app/get.js";
|
|
7
|
+
export { Post } from "./app/post.js";
|
|
8
8
|
export { csrf } from "./app/mw/csrf.js";
|
|
9
|
-
export type * from "./types/index.js";
|
package/src/jsx/chunk/index.ts
CHANGED
package/src/jsx/index.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { MaybeFunction, MaybePromise } from "../types/index.js";
|
|
|
2
2
|
import { Chunk } from "./chunk/index.js";
|
|
3
3
|
import type { IntrinsicElements as IE } from "./intrinsic-elements.js";
|
|
4
4
|
import { merge } from "./merge-async-generators.js";
|
|
5
|
+
import { setImmediate } from "node:timers/promises";
|
|
5
6
|
|
|
6
7
|
/** ovr JSX namespace */
|
|
7
8
|
export namespace JSX {
|
|
@@ -154,10 +155,21 @@ export async function* toGenerator(
|
|
|
154
155
|
) {
|
|
155
156
|
// sync generator
|
|
156
157
|
// process lazily - avoids loading all in memory
|
|
158
|
+
const yieldIterations = 50;
|
|
159
|
+
let yieldCounter = yieldIterations;
|
|
160
|
+
|
|
157
161
|
while (true) {
|
|
158
162
|
const result = iterator.next();
|
|
163
|
+
|
|
159
164
|
if (result.done) break;
|
|
160
165
|
yield* toGenerator(result.value);
|
|
166
|
+
|
|
167
|
+
if (--yieldCounter === 0) {
|
|
168
|
+
// yields back to the event loop to send chunks
|
|
169
|
+
// or check if the request has been cancelled
|
|
170
|
+
await setImmediate();
|
|
171
|
+
yieldCounter = yieldIterations;
|
|
172
|
+
}
|
|
161
173
|
}
|
|
162
174
|
|
|
163
175
|
return;
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
const never = new Promise(() => {}) as Promise<any>;
|
|
2
|
-
|
|
3
1
|
const next = async <T, R>(iterator: AsyncIterator<T, R>, index: number) => ({
|
|
4
2
|
index,
|
|
5
3
|
result: await iterator.next(),
|
|
@@ -12,31 +10,35 @@ const next = async <T, R>(iterator: AsyncIterator<T, R>, index: number) => ({
|
|
|
12
10
|
* Adapted from [stack overflow answers](https://stackoverflow.com/questions/50585456).
|
|
13
11
|
*
|
|
14
12
|
* @param generators Resolved in parallel.
|
|
15
|
-
* @param returnValue Passed into each `iterator.return()`
|
|
16
13
|
* @yields `IteratorResult` and `index` of the resolved generator.
|
|
17
14
|
*/
|
|
18
15
|
export async function* merge<T>(generators: AsyncGenerator<T, void>[]) {
|
|
19
16
|
const iterators = generators.map((gen) => gen[Symbol.asyncIterator]());
|
|
20
|
-
const promises =
|
|
17
|
+
const promises = new Map<number, Promise<any>>();
|
|
18
|
+
|
|
19
|
+
iterators.forEach((iterator, index) =>
|
|
20
|
+
promises.set(index, next(iterator, index)),
|
|
21
|
+
);
|
|
21
22
|
|
|
22
|
-
let remaining = promises.length;
|
|
23
23
|
let current: Awaited<ReturnType<typeof next>>;
|
|
24
24
|
|
|
25
25
|
try {
|
|
26
|
-
while (
|
|
27
|
-
yield (current = await Promise.race(promises));
|
|
26
|
+
while (promises.size > 0) {
|
|
27
|
+
yield (current = await Promise.race(promises.values()));
|
|
28
28
|
|
|
29
29
|
if (current.result.done) {
|
|
30
|
-
promises
|
|
31
|
-
remaining--;
|
|
30
|
+
promises.delete(current.index);
|
|
32
31
|
} else {
|
|
33
|
-
promises
|
|
34
|
-
iterators[current.index]!,
|
|
32
|
+
promises.set(
|
|
35
33
|
current.index,
|
|
34
|
+
next(iterators[current.index]!, current.index),
|
|
36
35
|
);
|
|
37
36
|
}
|
|
38
37
|
}
|
|
39
38
|
} finally {
|
|
40
|
-
for (const
|
|
39
|
+
for (const iterator of iterators) {
|
|
40
|
+
// catch - could have already returned
|
|
41
|
+
iterator.return().catch(() => {});
|
|
42
|
+
}
|
|
41
43
|
}
|
|
42
44
|
}
|
package/src/types/index.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import type { Params } from "../app/index.js";
|
|
2
|
+
import type { JSX } from "../jsx/index.js";
|
|
3
|
+
|
|
1
4
|
export type MaybePromise<T> = T | Promise<T>;
|
|
2
5
|
|
|
3
6
|
export type MaybeFunction<T> = T | (() => T);
|
|
@@ -21,3 +24,12 @@ export type ExtractMultiParams<Patterns extends string[]> = Patterns extends [
|
|
|
21
24
|
? ExtractParams<First>
|
|
22
25
|
: ExtractParams<First> | ExtractMultiParams<Rest>
|
|
23
26
|
: never;
|
|
27
|
+
|
|
28
|
+
export type AnchorProps<P extends Params> = JSX.IntrinsicElements["a"] &
|
|
29
|
+
(keyof P extends never ? { params?: never } : { params: P });
|
|
30
|
+
|
|
31
|
+
export type FormProps<P extends Params> = JSX.IntrinsicElements["form"] &
|
|
32
|
+
(keyof P extends never ? { params?: never } : { params: P });
|
|
33
|
+
|
|
34
|
+
export type ButtonProps<P extends Params> = JSX.IntrinsicElements["button"] &
|
|
35
|
+
(keyof P extends never ? { params?: never } : { params: P });
|
package/dist/app/action.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"action.d.ts","sourceRoot":"","sources":["../../src/app/action.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,GAAG,EAAO,MAAM,iBAAiB,CAAC;AAEhD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAEvD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAErD,KAAK,SAAS,CAAC,CAAC,SAAS,MAAM,IAAI,GAAG,CAAC,iBAAiB,CAAC,MAAM,CAAC,GAC/D,CAAC,MAAM,CAAC,SAAS,KAAK,GAAG;IAAE,MAAM,CAAC,EAAE,KAAK,CAAA;CAAE,GAAG;IAAE,MAAM,EAAE,CAAC,CAAA;CAAE,CAAC,CAAC;AAE9D,qBAAa,MAAM,CAAC,OAAO,SAAS,MAAM,GAAG,MAAM;;IAClD,oBAAoB;IACpB,OAAO,EAAE,OAAO,CAAC;IAEjB,sBAAsB;IACtB,UAAU,EAAE,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;IAE9B,uEAAuE;IACvE,IAAI,EAAE,CAAC,KAAK,EAAE,SAAS,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,KAAK,GAAG,CAAC,OAAO,CAAC;IAIhE;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;gBACS,GAAG,UAAU,EAAE,UAAU,CAAC,EAAE,CAAC,EAAE;IAC3C;;;OAGG;gBAEF,OAAO,EAAE,OAAO,EAChB,GAAG,UAAU,EAAE,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,EAAE;CA2BpD"}
|
package/dist/app/page.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"page.d.ts","sourceRoot":"","sources":["../../src/app/page.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,GAAG,EAAO,MAAM,iBAAiB,CAAC;AAEhD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AACvD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAErD,KAAK,WAAW,CAAC,CAAC,SAAS,MAAM,IAAI,GAAG,CAAC,iBAAiB,CAAC,GAAG,CAAC,GAC9D,CAAC,MAAM,CAAC,SAAS,KAAK,GAAG;IAAE,MAAM,CAAC,EAAE,KAAK,CAAA;CAAE,GAAG;IAAE,MAAM,EAAE,CAAC,CAAA;CAAE,CAAC,CAAC;AAE9D,qBAAa,IAAI,CAAC,OAAO,SAAS,MAAM,GAAG,MAAM;;IAChD,oBAAoB;IACpB,OAAO,EAAE,OAAO,CAAC;IAEjB,qBAAqB;IACrB,UAAU,EAAE,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;IAE9B,oDAAoD;IACpD,MAAM,EAAE,CAAC,KAAK,EAAE,WAAW,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,KAAK,GAAG,CAAC,OAAO,CAAC;IAIpE;;;;;;;;;;;;;;;;;;;;;OAqBG;gBAEF,OAAO,EAAE,OAAO,EAChB,GAAG,UAAU,EAAE,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,EAAE;CAWpD"}
|
package/src/app/page.ts
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
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 type { Middleware, Params } from "./index.js";
|
|
5
|
-
|
|
6
|
-
type AnchorProps<P extends Params> = JSX.IntrinsicElements["a"] &
|
|
7
|
-
(keyof P extends never ? { params?: never } : { params: P });
|
|
8
|
-
|
|
9
|
-
export class Page<Pattern extends string = string> {
|
|
10
|
-
/** Route pattern */
|
|
11
|
-
pattern: Pattern;
|
|
12
|
-
|
|
13
|
-
/** GET middleware */
|
|
14
|
-
middleware: Middleware<any>[];
|
|
15
|
-
|
|
16
|
-
/** `<a>` component with preset `href` attribute. */
|
|
17
|
-
Anchor: (props: AnchorProps<ExtractParams<Pattern>>) => JSX.Element;
|
|
18
|
-
|
|
19
|
-
#parts: string[];
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* @param pattern Route pattern
|
|
23
|
-
* @param middleware GET middleware
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
*
|
|
27
|
-
* ```tsx
|
|
28
|
-
* // page.tsx
|
|
29
|
-
* import { Page } from "ovr";
|
|
30
|
-
*
|
|
31
|
-
* export const page = new Page("/", () => {
|
|
32
|
-
* return <p>Hello world</p>
|
|
33
|
-
* });
|
|
34
|
-
*
|
|
35
|
-
* const Nav = () => {
|
|
36
|
-
* return <page.Anchor>Home</page.Anchor>
|
|
37
|
-
* }
|
|
38
|
-
*
|
|
39
|
-
* // app.tsx
|
|
40
|
-
* app.add(page); registers the page
|
|
41
|
-
* ```
|
|
42
|
-
*/
|
|
43
|
-
constructor(
|
|
44
|
-
pattern: Pattern,
|
|
45
|
-
...middleware: Middleware<ExtractParams<Pattern>>[]
|
|
46
|
-
) {
|
|
47
|
-
this.pattern = pattern;
|
|
48
|
-
this.middleware = middleware;
|
|
49
|
-
this.#parts = pattern.split("/");
|
|
50
|
-
this.Anchor = (props) => {
|
|
51
|
-
const { params = {}, ...rest } = props;
|
|
52
|
-
|
|
53
|
-
return jsx("a", { href: insertParams(this.#parts, params), ...rest });
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
}
|