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
package/dist/jsx/index.js CHANGED
@@ -1,204 +1,78 @@
1
- import { Chunk } from "./chunk/index.js";
2
- import { merge } from "./merge-async-generators.js";
3
- import { performance } from "node:perf_hooks";
4
- import { setImmediate } from "node:timers/promises";
5
- import { types } from "node:util";
6
- /**
7
- * These are the HTML tags that do not require a closing tag.
8
- *
9
- * [MDN Reference](https://developer.mozilla.org/en-US/docs/Glossary/Void_element#self-closing_tags)
10
- */
11
- const voidElements = new Set([
12
- "area",
13
- "base",
14
- "br",
15
- "col",
16
- "embed",
17
- "hr",
18
- "img",
19
- "input",
20
- "link",
21
- "meta",
22
- "source",
23
- "track",
24
- "wbr",
25
- ]);
26
- /**
27
- * The main function of the JSX transform cycle, each time JSX is encountered
28
- * it is passed into this function to be resolved. This function doesn't need to be
29
- * called recursively, JSX will be transformed into these `jsx()` function calls.
30
- *
31
- * @param tag string or function component
32
- * @param props object containing all the properties and attributes passed to the element or component
33
- * @returns `AsyncGenerator` that yields `Chunk`s of HTML
34
- */
35
- export async function* jsx(tag, props) {
36
- if (typeof tag === "function") {
37
- // component or fragment
38
- yield* toGenerator(tag(props));
39
- return;
40
- }
41
- // intrinsic element
42
- // faster to concatenate attributes than to yield them as separate chunks
43
- let attributes = "";
44
- for (const key in props) {
45
- // more memory efficient to skip children instead of destructuring and using ...rest
46
- if (key === "children")
47
- continue;
48
- const value = props[key];
49
- if (value === true) {
50
- // just put the key without the value
51
- attributes += ` ${key}`;
52
- }
53
- else if (typeof value === "string") {
54
- attributes += ` ${key}="${Chunk.escape(value, true)}"`;
55
- }
56
- else if (typeof value === "number" || typeof value === "bigint") {
57
- attributes += ` ${key}="${value}"`;
58
- }
59
- // otherwise, don't include the attribute
60
- }
61
- yield Chunk.safe(`<${tag}${attributes}>`);
62
- if (voidElements.has(tag))
63
- return;
64
- yield* toGenerator(props.children);
65
- yield Chunk.safe(`</${tag}>`);
66
- }
67
- /**
68
- * JSX requires a `Fragment` export to resolve `<></>`
69
- *
70
- * @param props containing `children` to render
71
- * @returns async generator that yields concatenated children
72
- */
73
- export async function* Fragment(props = {}) {
74
- yield* toGenerator(props.children);
75
- }
76
- /**
77
- * @param element
78
- * @yields `Chunk`s of HTML as the `Element` resolves
79
- */
80
- export async function* toGenerator(element) {
81
- // modifications
82
- // these are required to allow functions to be used as children
83
- // instead of creating a separate component to use them
84
- if (typeof element === "function")
85
- element = element();
86
- if (element instanceof Promise)
87
- element = await element;
88
- // resolve based on type
89
- // should not render
90
- if (element == null || typeof element === "boolean" || element === "")
91
- return;
92
- if (element instanceof Chunk) {
93
- // already escaped or safe
94
- yield element;
95
- return;
96
- }
97
- if (typeof element === "object") {
98
- if (Symbol.asyncIterator in element) {
99
- // any async iterable - lazily resolve
100
- for await (const children of element)
101
- yield* toGenerator(children);
1
+ import { Render } from "../render/index.js";
2
+ /** ovr JSX */
3
+ export class JSX {
4
+ /**
5
+ * These are the HTML tags that do not require a closing tag.
6
+ *
7
+ * [MDN Reference](https://developer.mozilla.org/en-US/docs/Glossary/Void_element#self-closing_tags)
8
+ */
9
+ static #void = new Set([
10
+ "area",
11
+ "base",
12
+ "br",
13
+ "col",
14
+ "embed",
15
+ "hr",
16
+ "img",
17
+ "input",
18
+ "link",
19
+ "meta",
20
+ "source",
21
+ "track",
22
+ "wbr",
23
+ ]);
24
+ /**
25
+ * The main function of the JSX transform cycle, each time JSX is encountered
26
+ * it is passed into `jsx` to be resolved.
27
+ *
28
+ * @param tag tag name or function component
29
+ * @param props object containing all the properties and attributes passed to the element or component
30
+ * @yields `Chunk`s of HTML
31
+ */
32
+ static async *jsx(tag, props) {
33
+ // this function doesn't need to be called recursively
34
+ // JSX will be transformed into `jsx()` function calls automatically
35
+ if (typeof tag === "function") {
36
+ // component or fragment
37
+ yield* new Render(tag(props));
102
38
  return;
103
39
  }
104
- if (Symbol.iterator in element) {
105
- // sync iterable
106
- if (types.isGeneratorObject(element)) {
107
- // sync generator - lazily resolve, avoids loading all in memory
108
- const ms = 8;
109
- let deadline = performance.now() + ms;
110
- for (const children of element) {
111
- yield* toGenerator(children);
112
- if (performance.now() > deadline) {
113
- // yields back to the event loop if deadline is reached
114
- // to send chunks or check if the request has been cancelled
115
- await setImmediate();
116
- deadline = performance.now() + ms;
117
- }
118
- }
119
- return;
40
+ if (tag === "html")
41
+ yield Render.html("<!doctype html>");
42
+ // intrinsic element
43
+ // faster to concatenate attributes than to yield them as separate chunks
44
+ let attributes = "";
45
+ for (const key in props) {
46
+ // more memory efficient to skip children instead of destructuring and using ...rest
47
+ if (key === "children")
48
+ continue;
49
+ const value = props[key];
50
+ if (value === true) {
51
+ // just put the key without the value
52
+ attributes += ` ${key}`;
120
53
  }
121
- // other iterable - array, set, etc.
122
- // process children in parallel
123
- const generators = Array.from(element, toGenerator);
124
- const n = generators.length;
125
- const queue = new Array(n);
126
- const complete = new Uint8Array(n);
127
- let current = 0;
128
- for await (const m of merge(generators)) {
129
- if (m.result.done) {
130
- complete[m.index] = 1;
131
- if (m.index === current) {
132
- while (++current < n) {
133
- if (queue[current]) {
134
- // yield whatever is in the next queue even if it hasn't completed yet
135
- yield queue[current];
136
- queue[current] = null;
137
- }
138
- // if it hasn't completed, stop iterating to the next
139
- if (!complete[current])
140
- break;
141
- }
142
- }
143
- }
144
- else if (m.index === current) {
145
- yield m.result.value; // stream the current value directly
146
- }
147
- else {
148
- // queue the value for later
149
- if (queue[m.index])
150
- queue[m.index].concat(m.result.value);
151
- else
152
- queue[m.index] = m.result.value;
153
- }
54
+ else if (typeof value === "string") {
55
+ attributes += ` ${key}="${Render.escape(value, true)}"`;
154
56
  }
155
- // clear the queue
156
- yield* queue.filter((chunk) => chunk !== null);
157
- return;
57
+ else if (typeof value === "number" || typeof value === "bigint") {
58
+ attributes += ` ${key}="${value}"`;
59
+ }
60
+ // otherwise, don't include the attribute
158
61
  }
62
+ yield Render.html(`<${tag}${attributes}>`);
63
+ if (JSX.#void.has(tag))
64
+ return;
65
+ yield* new Render(props.children);
66
+ yield Render.html(`</${tag}>`);
67
+ }
68
+ /**
69
+ * JSX requires a `Fragment` export to resolve `<></>`
70
+ *
71
+ * @param props containing `children` to render
72
+ * @yields concatenated children
73
+ */
74
+ static async *Fragment(props) {
75
+ yield* new Render(props.children);
159
76
  }
160
- // primitive or other object
161
- yield new Chunk(element);
162
77
  }
163
- /**
164
- * Converts a `JSX.Element` into a fully concatenated string of HTML.
165
- *
166
- * ### WARNING
167
- *
168
- * This negates streaming benefits and buffers the result into a string.
169
- * Use `toGenerator` whenever possible.
170
- *
171
- * @param element
172
- * @returns Concatenated HTML
173
- */
174
- export const toString = async (element) => (await Array.fromAsync(toGenerator(element))).join("");
175
- /** Single encoder to use across requests. */
176
- const encoder = new TextEncoder();
177
- /**
178
- * `toGenerator` piped into a `ReadableStream`.
179
- * Use `toGenerator` when possible to avoid the overhead of the stream.
180
- *
181
- * @param element
182
- * @returns `ReadableStream` of HTML
183
- */
184
- export const toStream = (element) => {
185
- const gen = toGenerator(element);
186
- return new ReadableStream({
187
- async pull(c) {
188
- const result = await gen.next();
189
- if (result.done) {
190
- c.close();
191
- gen.return();
192
- return;
193
- }
194
- c.enqueue(
195
- // need to encode for Node JS (ex: during prerendering) or it will error
196
- // doesn't seem to be needed for browsers
197
- // faster than piping through a `TextEncoderStream`
198
- encoder.encode(result.value.value));
199
- },
200
- cancel() {
201
- gen.return();
202
- },
203
- });
204
- };
78
+ export const { jsx, Fragment } = JSX;
@@ -0,0 +1,22 @@
1
+ import type { Context } from "../context/index.js";
2
+ import type { Trie } from "../trie/index.js";
3
+ export declare namespace Middleware {
4
+ /**
5
+ * Middleware context.
6
+ *
7
+ * @template Params Parameters created from a route match
8
+ */
9
+ type Context<Params extends Trie.Params = Trie.Params> = InstanceType<typeof Context<Params>>;
10
+ /** Dispatches the next middleware in the stack */
11
+ type Next = () => Promise<void>;
12
+ }
13
+ /**
14
+ * App middleware.
15
+ *
16
+ * @template Params Parameters created from a route match
17
+ * @param context Request context
18
+ * @param next Dispatches the next middleware in the stack
19
+ * @returns `Response` or element(s) to stream as HTML
20
+ */
21
+ export type Middleware<Params extends Trie.Params = Trie.Params> = (context: Middleware.Context<Params>, next: Middleware.Next) => any;
22
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/middleware/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAE7C,yBAAiB,UAAU,CAAC;IAC3B;;;;OAIG;IACH,KAAY,OAAO,CAAC,MAAM,SAAS,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,IAAI,YAAY,CAC3E,OAAO,OAAO,CAAC,MAAM,CAAC,CACtB,CAAC;IAEF,kDAAkD;IAClD,KAAY,IAAI,GAAG,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CACvC;AAED;;;;;;;GAOG;AACH,MAAM,MAAM,UAAU,CAAC,MAAM,SAAS,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,IAAI,CAClE,OAAO,EAAE,UAAU,CAAC,OAAO,CAAC,MAAM,CAAC,EACnC,IAAI,EAAE,UAAU,CAAC,IAAI,KACjB,GAAG,CAAC"}
@@ -0,0 +1,122 @@
1
+ /** Multipart part */
2
+ declare class Part extends Request {
3
+ /**
4
+ * Form input `name` attribute
5
+ *
6
+ * @example
7
+ *
8
+ * ```html
9
+ * <input type="file" name="photo">
10
+ * ```
11
+ */
12
+ readonly name: string | null;
13
+ /**
14
+ * Filename from Content-Disposition header if file
15
+ *
16
+ * @example "my-image.png"
17
+ */
18
+ readonly filename: string | null;
19
+ /**
20
+ * Media type of the part
21
+ *
22
+ * @example "image/png"
23
+ */
24
+ readonly type: string | null;
25
+ /** Part body */
26
+ readonly body: ReadableStream<Uint8Array<ArrayBuffer>>;
27
+ /**
28
+ * Create a new multipart part
29
+ *
30
+ * @param req Original request
31
+ * @param body Part body
32
+ * @param rawHeaders Raw buffer of HTTP headers for the part
33
+ */
34
+ constructor(req: Request, body: ReadableStream, rawHeaders: Uint8Array);
35
+ }
36
+ export declare namespace Multipart {
37
+ /** Multipart options */
38
+ type Options = {
39
+ /**
40
+ * Maximum memory allocation for request body processing (default 4MB)
41
+ *
42
+ * Even for large files, the parser does not hold the entire file in memory,
43
+ * it processes each chunk of the request body as it arrives.
44
+ * Set to a larger number if for example clients are sending all the
45
+ * data in a massive chunk instead of broken into smaller packets.
46
+ *
47
+ * @default 4 * 1024 * 1024
48
+ * @example
49
+ *
50
+ * ```ts
51
+ * const memory = 8 * 1024 * 1024; // increase to 8MB
52
+ * new Multipart(request, { memory });
53
+ * ```
54
+ */
55
+ memory?: number;
56
+ /**
57
+ * Maximum `Request.body` size in bytes (default 16MB)
58
+ *
59
+ * Prevents attackers from creating massive requests.
60
+ *
61
+ * Since the parser doesn't buffer all chunks in memory, it's possible
62
+ * to handle very large requests. Use this option to adjust the
63
+ * maximum total request body size that will be processed.
64
+ *
65
+ * @default 16 * 1024 * 1024
66
+ * @example
67
+ *
68
+ * ```ts
69
+ * const payload = 1024 ** 3; // increase to 1GB
70
+ * new Multipart(request, { payload });
71
+ * ```
72
+ */
73
+ payload?: number;
74
+ /**
75
+ * Maximum number of parts.
76
+ *
77
+ * @default Infinity
78
+ * @example
79
+ *
80
+ * ```ts
81
+ * const parts = 4; // only allow 4 parts
82
+ * new Multipart(request, { parts });
83
+ * ```
84
+ */
85
+ parts?: number;
86
+ };
87
+ /** Type for a `Part` of the multipart body */
88
+ type Part = InstanceType<typeof Part>;
89
+ }
90
+ /** Multipart request */
91
+ export declare class Multipart extends Request {
92
+ #private;
93
+ /**
94
+ * Split a multipart request into parts.
95
+ *
96
+ * @param req Multipart request
97
+ * @param options Options
98
+ * @yields Multipart form data `Part`(s)
99
+ * @throws {TypeError} If request headers are invalid
100
+ * @example
101
+ *
102
+ * ```ts
103
+ * for await (const part of new Multipart(request)) {
104
+ * // ...
105
+ * }
106
+ * ```
107
+ */
108
+ constructor(req: Request, options?: Multipart.Options);
109
+ /** @yields Multipart form data `Part`(s) */
110
+ [Symbol.asyncIterator](): AsyncGenerator<Part, void, unknown>;
111
+ /**
112
+ * Drop in replacement for `Request.formData` enhanced with size thresholds.
113
+ *
114
+ * Only use `data` when buffering all content is needed, otherwise iterate through
115
+ * the multipart using `for await...of` instead.
116
+ *
117
+ * @returns Buffered `FormData`
118
+ */
119
+ data(): Promise<FormData>;
120
+ }
121
+ export {};
122
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/multipart/index.ts"],"names":[],"mappings":"AAgCA,qBAAqB;AACrB,cAAM,IAAK,SAAQ,OAAO;IACzB;;;;;;;;OAQG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAE7B;;;;OAIG;IACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IAEjC;;;;OAIG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAG7B,gBAAgB;IAChB,SAAiB,IAAI,EAAE,cAAc,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC;IAE/D;;;;;;OAMG;gBACS,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,UAAU,EAAE,UAAU;CA6BtE;AAED,yBAAiB,SAAS,CAAC;IAC1B,wBAAwB;IACxB,KAAY,OAAO,GAAG;QACrB;;;;;;;;;;;;;;;WAeG;QACH,MAAM,CAAC,EAAE,MAAM,CAAC;QAEhB;;;;;;;;;;;;;;;;WAgBG;QACH,OAAO,CAAC,EAAE,MAAM,CAAC;QAEjB;;;;;;;;;;WAUG;QACH,KAAK,CAAC,EAAE,MAAM,CAAC;KACf,CAAC;IAEF,8CAA8C;IAC9C,KAAY,IAAI,GAAG,YAAY,CAAC,OAAO,IAAI,CAAC,CAAC;CAC7C;AAED,wBAAwB;AACxB,qBAAa,SAAU,SAAQ,OAAO;;IAuCrC;;;;;;;;;;;;;;OAcG;gBACS,GAAG,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,SAAS,CAAC,OAAO;IAmMrD,4CAA4C;IACrC,CAAC,MAAM,CAAC,aAAa,CAAC;IA6C7B;;;;;;;OAOG;IACG,IAAI;CAsBV"}