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.
Files changed (90) hide show
  1. package/dist/LICENSE.md +26 -0
  2. package/dist/app/index.d.ts +55 -103
  3. package/dist/app/index.d.ts.map +1 -1
  4. package/dist/app/index.js +67 -145
  5. package/dist/context/index.d.ts +138 -0
  6. package/dist/context/index.d.ts.map +1 -0
  7. package/dist/context/index.js +218 -0
  8. package/dist/cookie/index.d.ts +121 -0
  9. package/dist/cookie/index.d.ts.map +1 -0
  10. package/dist/cookie/index.js +56 -0
  11. package/dist/index.d.ts +6 -8
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +4 -7
  14. package/dist/jsx/{intrinsic-elements.d.ts → elements.d.ts} +14 -7
  15. package/dist/jsx/elements.d.ts.map +1 -0
  16. package/dist/jsx/index.d.ts +37 -52
  17. package/dist/jsx/index.d.ts.map +1 -1
  18. package/dist/jsx/index.js +71 -197
  19. package/dist/middleware/index.d.ts +22 -0
  20. package/dist/middleware/index.d.ts.map +1 -0
  21. package/dist/multipart/index.d.ts +122 -0
  22. package/dist/multipart/index.d.ts.map +1 -0
  23. package/dist/multipart/index.js +341 -0
  24. package/dist/render/index.d.ts +78 -0
  25. package/dist/render/index.d.ts.map +1 -0
  26. package/dist/render/index.js +248 -0
  27. package/dist/route/index.d.ts +147 -0
  28. package/dist/route/index.d.ts.map +1 -0
  29. package/dist/route/index.js +126 -0
  30. package/dist/trie/index.d.ts +31 -26
  31. package/dist/trie/index.d.ts.map +1 -1
  32. package/dist/trie/index.js +71 -80
  33. package/dist/util/index.d.ts +38 -0
  34. package/dist/util/index.d.ts.map +1 -0
  35. package/dist/util/index.js +88 -0
  36. package/package.json +4 -4
  37. package/src/app/index.ts +128 -265
  38. package/src/context/index.ts +262 -0
  39. package/src/cookie/index.ts +189 -0
  40. package/src/index.ts +6 -8
  41. package/src/jsx/{intrinsic-elements.ts → elements.ts} +14 -7
  42. package/src/jsx/index.ts +81 -222
  43. package/src/middleware/index.ts +29 -0
  44. package/src/multipart/index.ts +486 -0
  45. package/src/render/index.ts +300 -0
  46. package/src/route/index.ts +332 -0
  47. package/src/trie/index.ts +90 -107
  48. package/src/util/index.ts +112 -0
  49. package/dist/app/context.d.ts +0 -147
  50. package/dist/app/context.d.ts.map +0 -1
  51. package/dist/app/context.js +0 -283
  52. package/dist/app/helper/get.d.ts +0 -30
  53. package/dist/app/helper/get.d.ts.map +0 -1
  54. package/dist/app/helper/get.js +0 -33
  55. package/dist/app/helper/index.d.ts +0 -55
  56. package/dist/app/helper/index.d.ts.map +0 -1
  57. package/dist/app/helper/index.js +0 -96
  58. package/dist/app/helper/post.d.ts +0 -35
  59. package/dist/app/helper/post.d.ts.map +0 -1
  60. package/dist/app/helper/post.js +0 -15
  61. package/dist/app/memo/index.d.ts +0 -12
  62. package/dist/app/memo/index.d.ts.map +0 -1
  63. package/dist/app/memo/index.js +0 -34
  64. package/dist/app/mw/csrf.d.ts +0 -23
  65. package/dist/app/mw/csrf.d.ts.map +0 -1
  66. package/dist/app/mw/csrf.js +0 -30
  67. package/dist/jsx/chunk/index.d.ts +0 -38
  68. package/dist/jsx/chunk/index.d.ts.map +0 -1
  69. package/dist/jsx/chunk/index.js +0 -69
  70. package/dist/jsx/intrinsic-elements.d.ts.map +0 -1
  71. package/dist/jsx/merge-async-generators.d.ts +0 -14
  72. package/dist/jsx/merge-async-generators.d.ts.map +0 -1
  73. package/dist/jsx/merge-async-generators.js +0 -42
  74. package/dist/types/index.d.ts +0 -17
  75. package/dist/types/index.d.ts.map +0 -1
  76. package/dist/util/hash.d.ts +0 -11
  77. package/dist/util/hash.d.ts.map +0 -1
  78. package/dist/util/hash.js +0 -16
  79. package/src/app/context.ts +0 -365
  80. package/src/app/helper/get.ts +0 -46
  81. package/src/app/helper/index.ts +0 -174
  82. package/src/app/helper/post.ts +0 -54
  83. package/src/app/memo/index.ts +0 -40
  84. package/src/app/mw/csrf.ts +0 -32
  85. package/src/jsx/chunk/index.ts +0 -79
  86. package/src/jsx/merge-async-generators.ts +0 -47
  87. package/src/types/index.ts +0 -42
  88. package/src/util/hash.ts +0 -17
  89. /package/dist/jsx/{intrinsic-elements.js → elements.js} +0 -0
  90. /package/dist/{types → middleware}/index.js +0 -0
@@ -1,283 +0,0 @@
1
- import { Chunk } from "../jsx/chunk/index.js";
2
- import { toStream } from "../jsx/index.js";
3
- import { hash } from "../util/hash.js";
4
- import { App, } from "./index.js";
5
- import { Memo } from "./memo/index.js";
6
- class TagNotFound extends Error {
7
- name = "TagNotFound";
8
- constructor(tag) {
9
- super(`No closing ${tag} tag found`);
10
- }
11
- }
12
- class ResponseFinalized extends Error {
13
- name = "ResponseFinalized";
14
- constructor(property) {
15
- super(`Cannot set \`${property}\` after the \`Response\` has been finalized. ` +
16
- `This can occur if \`${property}\` is set within a component since ` +
17
- "components are evaluated during streaming.");
18
- }
19
- }
20
- export class Context {
21
- /** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Request) */
22
- req;
23
- /** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/URL) */
24
- url;
25
- /**
26
- * Route pattern parameters
27
- *
28
- * Given the route pattern `/posts/:slug` is added, a request made to
29
- * `/posts/my-post` creates a `params` object `{ slug: "my-post" }`.
30
- *
31
- * @example { slug: "my-post" }
32
- */
33
- params; // set after match
34
- /** The matched `Route` instance. */
35
- route; // set after match
36
- #body = null;
37
- #status;
38
- /** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Headers) */
39
- headers = new Headers();
40
- #layouts = [];
41
- #headElements = [];
42
- #trailingSlash;
43
- #base;
44
- /** Passed from `app.error` */
45
- error;
46
- /** Passed from `app.notFound` */
47
- notFound;
48
- /** `Memo` unique to the current `Request` context */
49
- #memo = new Memo();
50
- memo = this.#memo.use;
51
- /** Check if the `Response` has been built already */
52
- #finalized = false;
53
- static #contentType = "content-type";
54
- static #headClose = "</head>";
55
- static #bodyClose = "</body>";
56
- constructor(req, url, trailingSlash, base, error, notFound) {
57
- this.req = req;
58
- this.url = url;
59
- this.#trailingSlash = trailingSlash;
60
- this.#base = base;
61
- this.error = error;
62
- this.notFound = notFound;
63
- }
64
- /** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status) */
65
- get status() {
66
- return this.#status;
67
- }
68
- set status(status) {
69
- if (this.#finalized)
70
- throw new ResponseFinalized("status");
71
- this.#status = status;
72
- }
73
- /** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response#body) */
74
- get body() {
75
- return this.#body;
76
- }
77
- set body(body) {
78
- if (this.#finalized)
79
- throw new ResponseFinalized("body");
80
- this.#body = body;
81
- }
82
- /** Passed from `app.base` */
83
- get base() {
84
- return this.#base;
85
- }
86
- set base(base) {
87
- if (this.#finalized)
88
- throw new ResponseFinalized("base");
89
- this.#base = base;
90
- }
91
- /**
92
- * Mirrors `new Response()` constructor, set values with one function.
93
- *
94
- * @param body Response BodyInit
95
- * @param init Enhanced ResponseInit
96
- */
97
- res(body, init) {
98
- this.body = body;
99
- this.status = init?.status ?? 200;
100
- if (init?.headers) {
101
- for (const [name, value] of new Headers(init.headers)) {
102
- this.headers.set(name, value);
103
- }
104
- }
105
- }
106
- /**
107
- * Creates an HTML response.
108
- *
109
- * @param body HTML body
110
- * @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
111
- */
112
- html(body, status) {
113
- this.res(body, {
114
- status,
115
- headers: { [Context.#contentType]: "text/html; charset=utf-8" },
116
- });
117
- }
118
- /**
119
- * Creates a JSON response.
120
- *
121
- * @param data passed into JSON.stringify to create the body
122
- * @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
123
- */
124
- json(data, status) {
125
- this.res(JSON.stringify(data), {
126
- status,
127
- headers: { [Context.#contentType]: "application/json" },
128
- });
129
- }
130
- /**
131
- * Creates a plain text response.
132
- *
133
- * @param body response body
134
- * @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
135
- */
136
- text(body, status) {
137
- this.res(body, {
138
- status,
139
- headers: { [Context.#contentType]: "text/plain; charset=utf-8" },
140
- });
141
- }
142
- /**
143
- * Creates a redirect response.
144
- *
145
- * @param location redirect `Location` header
146
- * @param status HTTP status code
147
- *
148
- * - [301 Moved Permanently](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/301)
149
- * - [302 Found](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/302) (default)
150
- * - [303 See Other](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/303)
151
- * - [307 Temporary Redirect](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/307)
152
- * - [308 Permanent Redirect](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/308)
153
- */
154
- redirect(location, status = 302) {
155
- this.headers.set("location", location.toString());
156
- this.status = status;
157
- }
158
- /**
159
- * @param Element `JSX.Element` to add to the head
160
- */
161
- head(...Element) {
162
- if (this.#finalized)
163
- throw new ResponseFinalized("head");
164
- this.#headElements.push(...Element);
165
- }
166
- /**
167
- * @param Layout `Layout`(s) for the `Page` to be passed into before being injected into the `<body>`
168
- */
169
- layout(...Layout) {
170
- if (this.#finalized)
171
- throw new ResponseFinalized("layout");
172
- this.#layouts.push(...Layout);
173
- }
174
- /**
175
- * Creates an HTML response based on the `head` elements, `Layout`(s), and `Page` provided.
176
- *
177
- * @param Page `JSX.Element` to inject into the `<body>`
178
- * @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
179
- */
180
- page(Page, status) {
181
- for (let i = this.#layouts.length - 1; i >= 0; i--) {
182
- Page = this.#layouts[i]({ children: Page });
183
- }
184
- let stream;
185
- if (this.base) {
186
- // inject into base
187
- const elements = this.base.split(Context.#headClose);
188
- if (!elements[1])
189
- throw new TagNotFound(Context.#headClose);
190
- elements.splice(1, 0, this.#headElements, Context.#headClose);
191
- const bodyParts = elements[3].split(Context.#bodyClose);
192
- if (!bodyParts[1])
193
- throw new TagNotFound(Context.#bodyClose);
194
- const userAgent = this.req.headers.get("user-agent");
195
- if (userAgent?.includes("Safari") && !userAgent.includes("Chrome")) {
196
- // https://bugs.webkit.org/show_bug.cgi?id=252413
197
- // https://github.com/sveltejs/kit/issues/10315
198
- // https://github.com/remix-run/remix/issues/5804
199
- 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>`;
200
- }
201
- elements[3] = bodyParts[0];
202
- elements.push(Page, Context.#bodyClose + bodyParts[1]);
203
- stream = toStream(elements.map((el) => (typeof el === "string" ? Chunk.safe(el) : el)));
204
- }
205
- else {
206
- // HTML partial - just use the layouts + page
207
- stream = toStream(Page);
208
- }
209
- this.html(stream, status);
210
- }
211
- /**
212
- * Generates an etag from a hash of the string provided.
213
- * If the etag matches, sets the response to not modified.
214
- *
215
- * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag)
216
- *
217
- * @param s string to hash
218
- * @returns `true` if the etag matches, `false` otherwise
219
- */
220
- etag(s) {
221
- const etag = `"${hash(s)}"`;
222
- this.headers.set("etag", etag);
223
- if (this.req.headers.get("if-none-match") === etag) {
224
- this.body = null;
225
- this.status = 304;
226
- return true;
227
- }
228
- return false;
229
- }
230
- /**
231
- * @returns the constructed `Response`
232
- */
233
- build() {
234
- if ((!this.status || this.status === 404) &&
235
- this.#trailingSlash !== "ignore") {
236
- const last = this.url.pathname.at(-1);
237
- if (this.#trailingSlash === "always" && last !== "/") {
238
- this.url.pathname += "/";
239
- this.redirect(this.url, 308);
240
- }
241
- else if (this.#trailingSlash === "never" &&
242
- this.url.pathname !== "/" &&
243
- last === "/") {
244
- this.url.pathname = this.url.pathname.slice(0, -1);
245
- this.redirect(this.url, 308);
246
- }
247
- }
248
- if (!this.body && !this.status)
249
- this.notFound(this);
250
- this.#finalized = true;
251
- return new Response(this.body, this);
252
- }
253
- /**
254
- * Call within the scope of a handler to get the current context.
255
- *
256
- * @returns Request context
257
- *
258
- * @example
259
- *
260
- * ```ts
261
- * import { Context } from "ovr";
262
- *
263
- * const app = new Router();
264
- *
265
- * const fn = () => {
266
- * const c = Context.get();
267
- * // ...
268
- * }
269
- *
270
- * app.get("/", () => {
271
- * fn(); // OK
272
- * });
273
- *
274
- * fn() // ReferenceError - outside AsyncLocalStorage scope
275
- * ```
276
- */
277
- static get() {
278
- const c = App.storage.getStore();
279
- if (!c)
280
- throw new ReferenceError("Context can only be obtained within a handler.");
281
- return c;
282
- }
283
- }
@@ -1,30 +0,0 @@
1
- import { type JSX } from "../../jsx/index.js";
2
- import type { ExtractParams } from "../../types/index.js";
3
- import type { Middleware } from "../index.js";
4
- import { Helper, type UrlOptions } from "./index.js";
5
- export declare class Get<Pattern extends string = string> extends Helper<Pattern> {
6
- /** `<a>` component with preset `href` attribute. */
7
- Anchor: (props: JSX.IntrinsicElements["a"] & UrlOptions<ExtractParams<Pattern>>) => JSX.Element;
8
- /**
9
- * @param pattern Route pattern
10
- * @param middleware GET middleware
11
- *
12
- * @example
13
- *
14
- * ```tsx
15
- * import { Get } from "ovr";
16
- *
17
- * const page = new Get("/", () => {
18
- * return <p>Hello world</p>
19
- * });
20
- *
21
- * const Nav = () => {
22
- * return <page.Anchor>Home</page.Anchor>
23
- * }
24
- *
25
- * app.add(page); // register
26
- * ```
27
- */
28
- constructor(pattern: Pattern, ...middleware: Middleware<ExtractParams<Pattern>>[]);
29
- }
30
- //# sourceMappingURL=get.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"get.d.ts","sourceRoot":"","sources":["../../../src/app/helper/get.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,GAAG,EAAO,MAAM,oBAAoB,CAAC;AACnD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAE,KAAK,UAAU,EAAE,MAAM,YAAY,CAAC;AAErD,qBAAa,GAAG,CAAC,OAAO,SAAS,MAAM,GAAG,MAAM,CAAE,SAAQ,MAAM,CAAC,OAAO,CAAC;IACxE,oDAAoD;IACpD,MAAM,EAAE,CACP,KAAK,EAAE,GAAG,CAAC,iBAAiB,CAAC,GAAG,CAAC,GAAG,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,KAClE,GAAG,CAAC,OAAO,CAAC;IAEjB;;;;;;;;;;;;;;;;;;;OAmBG;gBAEF,OAAO,EAAE,OAAO,EAChB,GAAG,UAAU,EAAE,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,EAAE;CAYpD"}
@@ -1,33 +0,0 @@
1
- import { jsx } from "../../jsx/index.js";
2
- import { Helper } from "./index.js";
3
- export class Get extends Helper {
4
- /** `<a>` component with preset `href` attribute. */
5
- Anchor;
6
- /**
7
- * @param pattern Route pattern
8
- * @param middleware GET middleware
9
- *
10
- * @example
11
- *
12
- * ```tsx
13
- * import { Get } from "ovr";
14
- *
15
- * const page = new Get("/", () => {
16
- * return <p>Hello world</p>
17
- * });
18
- *
19
- * const Nav = () => {
20
- * return <page.Anchor>Home</page.Anchor>
21
- * }
22
- *
23
- * app.add(page); // register
24
- * ```
25
- */
26
- constructor(pattern, ...middleware) {
27
- super("GET", pattern, ...middleware);
28
- this.Anchor = ({ params, search, hash, ...rest }) => jsx("a", {
29
- href: this.url({ params, search, hash }),
30
- ...rest,
31
- });
32
- }
33
- }
@@ -1,55 +0,0 @@
1
- import { type JSX } from "../../jsx/index.js";
2
- import type { Params } from "../../trie/index.js";
3
- import type { ExtractParams, InsertParams } from "../../types/index.js";
4
- import type { Middleware } from "../index.js";
5
- export type UrlOptions<P extends Params> = {
6
- /**
7
- * If `true` the current request's `url.search` is forwarded, this option
8
- * can only be used in the context of a request.
9
- *
10
- * Otherwise the value is passed into `URLSearchParams` constructor to
11
- * create new params.
12
- *
13
- * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams)
14
- */
15
- search?: boolean | string | Iterable<[string, string]> | Record<string, string>;
16
- /**
17
- * Hash (fragment) of the URL. `"#"` prefix is added if not present.
18
- *
19
- * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/URL/hash)
20
- */
21
- hash?: string;
22
- } & (keyof P extends never ? {
23
- /** Route pattern does not contain parameters */
24
- params?: never;
25
- } : {
26
- /** Route pattern parameters */
27
- params: P;
28
- });
29
- export declare class Helper<Pattern extends string> {
30
- #private;
31
- /** Extracted parameters type for the pattern */
32
- readonly Params: ExtractParams<Pattern>;
33
- /** Route pattern */
34
- pattern: Pattern;
35
- /** GET middleware */
36
- middleware: Middleware<any>[];
37
- /** `<button>` component with preset `formaction` and `formmethod` attributes. */
38
- Button: (props: JSX.IntrinsicElements["button"] & UrlOptions<ExtractParams<Pattern>>) => JSX.Element;
39
- /** `<form>` component with preset `method` and `action` attributes. */
40
- Form: (props: JSX.IntrinsicElements["form"] & UrlOptions<ExtractParams<Pattern>>) => JSX.Element;
41
- constructor(method: string, pattern: Pattern, ...middleware: Middleware<ExtractParams<Pattern>>[]);
42
- /**
43
- * Constructs a _relative_ URL for the route.
44
- *
45
- * @param [options] Options with type safe parameters
46
- * @returns `pathname` + `search` + `hash`
47
- */
48
- url(...[options]: keyof ExtractParams<Pattern> extends never ? [UrlOptions<ExtractParams<Pattern>>] | [] : [UrlOptions<ExtractParams<Pattern>>]): string;
49
- /**
50
- * @param [params] Parameters to insert
51
- * @returns Resolved `pathname` with params
52
- */
53
- pathname<Params extends ExtractParams<Pattern>>(...[params]: keyof Params extends never ? [] : [Params]): InsertParams<Pattern, Params>;
54
- }
55
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/app/helper/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,GAAG,EAAO,MAAM,oBAAoB,CAAC;AACnD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAClD,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAExE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAE9C,MAAM,MAAM,UAAU,CAAC,CAAC,SAAS,MAAM,IAAI;IAC1C;;;;;;;;OAQG;IACH,MAAM,CAAC,EACJ,OAAO,GACP,MAAM,GAGN,QAAQ,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,GAC1B,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAE1B;;;;OAIG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;CACd,GAAG,CAAC,MAAM,CAAC,SAAS,KAAK,GACvB;IACA,gDAAgD;IAChD,MAAM,CAAC,EAAE,KAAK,CAAC;CACf,GACA;IACA,+BAA+B;IAC/B,MAAM,EAAE,CAAC,CAAC;CACV,CAAC,CAAC;AAEL,qBAAa,MAAM,CAAC,OAAO,SAAS,MAAM;;IACzC,gDAAgD;IAChD,SAAiB,MAAM,EAAE,aAAa,CAAC,OAAO,CAAC,CAAC;IAEhD,oBAAoB;IACpB,OAAO,EAAE,OAAO,CAAC;IAEjB,qBAAqB;IACrB,UAAU,EAAE,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;IAE9B,iFAAiF;IACjF,MAAM,EAAE,CACP,KAAK,EAAE,GAAG,CAAC,iBAAiB,CAAC,QAAQ,CAAC,GAAG,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,KACvE,GAAG,CAAC,OAAO,CAAC;IAEjB,uEAAuE;IACvE,IAAI,EAAE,CACL,KAAK,EAAE,GAAG,CAAC,iBAAiB,CAAC,MAAM,CAAC,GAAG,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,KACrE,GAAG,CAAC,OAAO,CAAC;gBAMhB,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,OAAO,EAChB,GAAG,UAAU,EAAE,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,EAAE;IAyBpD;;;;;OAKG;IACH,GAAG,CACF,GAAG,CAAC,OAAO,CAAC,EAAE,MAAM,aAAa,CAAC,OAAO,CAAC,SAAS,KAAK,GACrD,CAAC,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,GACzC,CAAC,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,CAAC;IAwCxC;;;OAGG;IACH,QAAQ,CAAC,MAAM,SAAS,aAAa,CAAC,OAAO,CAAC,EAC7C,GAAG,CAAC,MAAM,CAAC,EAAE,MAAM,MAAM,SAAS,KAAK,GAAG,EAAE,GAAG,CAAC,MAAM,CAAC,GACrD,YAAY,CAAC,OAAO,EAAE,MAAM,CAAC;CA2BhC"}
@@ -1,96 +0,0 @@
1
- import { jsx } from "../../jsx/index.js";
2
- import { Context } from "../context.js";
3
- export class Helper {
4
- /** Route pattern */
5
- pattern;
6
- /** GET middleware */
7
- middleware; // any so you can use other middleware
8
- /** `<button>` component with preset `formaction` and `formmethod` attributes. */
9
- Button;
10
- /** `<form>` component with preset `method` and `action` attributes. */
11
- Form;
12
- /** Pattern parts */
13
- #parts;
14
- constructor(method, pattern, ...middleware) {
15
- this.pattern = pattern;
16
- this.middleware = middleware;
17
- this.#parts = pattern.split("/");
18
- this.Button = ({ params, search, hash, ...rest }) => jsx("button", {
19
- formaction: this.url({ params, search, hash }),
20
- formmethod: method,
21
- ...rest,
22
- });
23
- this.Form = ({ params, search, hash, ...rest }) => jsx("form", {
24
- action: this.url({ params, search, hash }),
25
- method,
26
- ...rest,
27
- });
28
- }
29
- /**
30
- * Constructs a _relative_ URL for the route.
31
- *
32
- * @param [options] Options with type safe parameters
33
- * @returns `pathname` + `search` + `hash`
34
- */
35
- url(...[options]) {
36
- const pathname = this.pathname(
37
- // @ts-expect-error - do not have to pass in {} if no params
38
- options?.params);
39
- let search;
40
- if (options?.search) {
41
- if (options.search === true) {
42
- // forward current request's search
43
- search = Context.get().url.search;
44
- }
45
- else {
46
- // use the value as the init
47
- search =
48
- "?" +
49
- new URLSearchParams(options.search);
50
- }
51
- }
52
- else {
53
- search = "";
54
- }
55
- let hash;
56
- if (options?.hash) {
57
- // adding # prefix if not present matches the URL setter:
58
- // https://developer.mozilla.org/en-US/docs/Web/API/URL/hash
59
- if (options.hash.startsWith("#")) {
60
- hash = options.hash;
61
- }
62
- else {
63
- hash = "#" + options.hash;
64
- }
65
- }
66
- else {
67
- hash = "";
68
- }
69
- return pathname + search + hash;
70
- }
71
- /**
72
- * @param [params] Parameters to insert
73
- * @returns Resolved `pathname` with params
74
- */
75
- pathname(...[params]) {
76
- if (!params)
77
- return this.pattern;
78
- const wild = "*";
79
- return this.#parts
80
- .map((part) => {
81
- if (part.startsWith(":")) {
82
- const param = part.slice(1);
83
- if (!(param in params))
84
- throw new Error(`Parameter "${param}" did not match pattern.`);
85
- return params[param];
86
- }
87
- if (part === wild) {
88
- if (!(wild in params))
89
- throw new Error("No wildcard parameter found.");
90
- return params[wild];
91
- }
92
- return part;
93
- })
94
- .join("/");
95
- }
96
- }
@@ -1,35 +0,0 @@
1
- import type { ExtractParams } from "../../types/index.js";
2
- import type { Middleware } from "../index.js";
3
- import { Helper } from "./index.js";
4
- export declare class Post<Pattern extends string = string> extends Helper<Pattern> {
5
- /**
6
- * @param middleware POST middleware
7
- *
8
- * @example
9
- *
10
- * ```tsx
11
- * import { Post } from "ovr";
12
- *
13
- * const post = new Post((c) => {
14
- * console.log("posted");
15
- * c.redirect("/", 303);
16
- * });
17
- *
18
- * const page = new Get("/", () => (
19
- * <post.Form>
20
- * <input type="text" name="name" />
21
- * <button>Submit</button>
22
- * </post.Form>
23
- * ));
24
- *
25
- * app.add(page, post); // register
26
- * ```
27
- */
28
- constructor(...middleware: Middleware<{}>[]);
29
- /**
30
- * @param pattern Route pattern
31
- * @param middleware POST middleware
32
- */
33
- constructor(pattern: Pattern, ...middleware: Middleware<ExtractParams<Pattern>>[]);
34
- }
35
- //# sourceMappingURL=post.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"post.d.ts","sourceRoot":"","sources":["../../../src/app/helper/post.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAE1D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAEpC,qBAAa,IAAI,CAAC,OAAO,SAAS,MAAM,GAAG,MAAM,CAAE,SAAQ,MAAM,CAAC,OAAO,CAAC;IACzE;;;;;;;;;;;;;;;;;;;;;;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;CAiBpD"}
@@ -1,15 +0,0 @@
1
- import { hash } from "../../util/hash.js";
2
- import { Helper } from "./index.js";
3
- export class Post extends Helper {
4
- constructor(patternOrMiddleware, ...middleware) {
5
- let pattern;
6
- if (typeof patternOrMiddleware === "string") {
7
- pattern = patternOrMiddleware;
8
- }
9
- else {
10
- middleware.unshift(patternOrMiddleware);
11
- pattern = `/_p/${hash(middleware.join())}`;
12
- }
13
- super("POST", pattern, ...middleware);
14
- }
15
- }
@@ -1,12 +0,0 @@
1
- export declare class Memo {
2
- #private;
3
- /**
4
- * Calls to the returned function with the same arguments will
5
- * return a cached result.
6
- *
7
- * @param fn Function to memoize.
8
- * @returns The memoized function.
9
- */
10
- use: <A extends any[], R>(fn: (...args: A) => R) => (...args: A) => R;
11
- }
12
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/app/memo/index.ts"],"names":[],"mappings":"AAAA,qBAAa,IAAI;;IAOhB;;;;;;OAMG;IACH,GAAG,GAAI,CAAC,SAAS,GAAG,EAAE,EAAE,CAAC,EAAE,IAAI,CAAC,GAAG,IAAI,EAAE,CAAC,KAAK,CAAC,eAAP,CAAC,KAAK,CAAC,CAwB9C;CACF"}
@@ -1,34 +0,0 @@
1
- export class Memo {
2
- /** Already memoized functions. */
3
- #memoized = new WeakMap();
4
- /** Symbol to store the result of each memoized function. */
5
- static #RESULT = Symbol();
6
- /**
7
- * Calls to the returned function with the same arguments will
8
- * return a cached result.
9
- *
10
- * @param fn Function to memoize.
11
- * @returns The memoized function.
12
- */
13
- use = (fn) => {
14
- if (this.#memoized.has(fn))
15
- return this.#memoized.get(fn);
16
- // closure will GC automatically
17
- const trie = new Map();
18
- const memo = (...args) => {
19
- let node = trie;
20
- for (const arg of args) {
21
- // get/create a node for each arg
22
- if (!node.has(arg))
23
- node.set(arg, new Map());
24
- node = node.get(arg);
25
- }
26
- // set the final result if not there
27
- if (!node.has(Memo.#RESULT))
28
- node.set(Memo.#RESULT, fn(...args));
29
- return node.get(Memo.#RESULT);
30
- };
31
- this.#memoized.set(fn, memo);
32
- return memo;
33
- };
34
- }
@@ -1,23 +0,0 @@
1
- import type { Middleware } from "../index.js";
2
- /**
3
- * Basic
4
- * [cross-site request forgery (CSRF)](https://developer.mozilla.org/en-US/docs/Web/Security/Attacks/CSRF)
5
- * protection that checks the request's `method` and `Origin` header.
6
- * For more robust protection you'll need a stateful server or a database to store
7
- * [CSRF tokens](https://developer.mozilla.org/en-US/docs/Web/Security/Attacks/CSRF#csrf_tokens).
8
- *
9
- * @param options
10
- * @returns CSRF middleware
11
- *
12
- * @example
13
- *
14
- * ```ts
15
- * import { csrf } from "ovr";
16
- *
17
- * app.use(csrf({ origin: "https://example.com" }));
18
- * ```
19
- */
20
- export declare const csrf: (options: {
21
- origin: string | string[];
22
- }) => Middleware;
23
- //# sourceMappingURL=csrf.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"csrf.d.ts","sourceRoot":"","sources":["../../../src/app/mw/csrf.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAE9C;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,IAAI,GAAI,SAAS;IAAE,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;CAAE,KAAG,UAW7D,CAAC"}
@@ -1,30 +0,0 @@
1
- /**
2
- * Basic
3
- * [cross-site request forgery (CSRF)](https://developer.mozilla.org/en-US/docs/Web/Security/Attacks/CSRF)
4
- * protection that checks the request's `method` and `Origin` header.
5
- * For more robust protection you'll need a stateful server or a database to store
6
- * [CSRF tokens](https://developer.mozilla.org/en-US/docs/Web/Security/Attacks/CSRF#csrf_tokens).
7
- *
8
- * @param options
9
- * @returns CSRF middleware
10
- *
11
- * @example
12
- *
13
- * ```ts
14
- * import { csrf } from "ovr";
15
- *
16
- * app.use(csrf({ origin: "https://example.com" }));
17
- * ```
18
- */
19
- export const csrf = (options) => {
20
- if (typeof options.origin === "string")
21
- options.origin = [options.origin];
22
- return (c, next) => {
23
- if (["GET", "HEAD"].includes(c.req.method))
24
- return next();
25
- const origin = c.req.headers.get("Origin");
26
- if (origin && options.origin.includes(origin))
27
- return next();
28
- return c.text("Forbidden", 403);
29
- };
30
- };