ovr 5.0.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 (86) hide show
  1. package/dist/LICENSE.md +26 -0
  2. package/dist/app/index.d.ts +49 -83
  3. package/dist/app/index.d.ts.map +1 -1
  4. package/dist/app/index.js +40 -92
  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 -7
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +4 -6
  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 -200
  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 +30 -30
  31. package/dist/trie/index.d.ts.map +1 -1
  32. package/dist/trie/index.js +67 -82
  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 +3 -3
  37. package/src/app/index.ts +99 -224
  38. package/src/context/index.ts +262 -0
  39. package/src/cookie/index.ts +189 -0
  40. package/src/index.ts +6 -7
  41. package/src/jsx/{intrinsic-elements.ts → elements.ts} +14 -7
  42. package/src/jsx/index.ts +81 -223
  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 +87 -111
  48. package/src/util/index.ts +112 -0
  49. package/dist/app/context.d.ts +0 -149
  50. package/dist/app/context.d.ts.map +0 -1
  51. package/dist/app/context.js +0 -246
  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 -51
  56. package/dist/app/helper/index.d.ts.map +0 -1
  57. package/dist/app/helper/index.js +0 -82
  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/jsx/chunk/index.d.ts +0 -38
  65. package/dist/jsx/chunk/index.d.ts.map +0 -1
  66. package/dist/jsx/chunk/index.js +0 -69
  67. package/dist/jsx/intrinsic-elements.d.ts.map +0 -1
  68. package/dist/jsx/merge-async-generators.d.ts +0 -14
  69. package/dist/jsx/merge-async-generators.d.ts.map +0 -1
  70. package/dist/jsx/merge-async-generators.js +0 -42
  71. package/dist/types/index.d.ts +0 -17
  72. package/dist/types/index.d.ts.map +0 -1
  73. package/dist/util/hash.d.ts +0 -11
  74. package/dist/util/hash.d.ts.map +0 -1
  75. package/dist/util/hash.js +0 -16
  76. package/src/app/context.ts +0 -303
  77. package/src/app/helper/get.ts +0 -46
  78. package/src/app/helper/index.ts +0 -156
  79. package/src/app/helper/post.ts +0 -54
  80. package/src/app/memo/index.ts +0 -40
  81. package/src/jsx/chunk/index.ts +0 -79
  82. package/src/jsx/merge-async-generators.ts +0 -47
  83. package/src/types/index.ts +0 -42
  84. package/src/util/hash.ts +0 -17
  85. /package/dist/jsx/{intrinsic-elements.js → elements.js} +0 -0
  86. /package/dist/{types → middleware}/index.js +0 -0
package/dist/jsx/index.js CHANGED
@@ -1,207 +1,78 @@
1
- import { Chunk } from "./chunk/index.js";
2
- import { merge } from "./merge-async-generators.js";
3
- /**
4
- * These are the HTML tags that do not require a closing tag.
5
- *
6
- * [MDN Reference](https://developer.mozilla.org/en-US/docs/Glossary/Void_element#self-closing_tags)
7
- */
8
- const voidElements = new Set([
9
- "area",
10
- "base",
11
- "br",
12
- "col",
13
- "embed",
14
- "hr",
15
- "img",
16
- "input",
17
- "link",
18
- "meta",
19
- "source",
20
- "track",
21
- "wbr",
22
- ]);
23
- /**
24
- * The main function of the JSX transform cycle, each time JSX is encountered
25
- * it is passed into this function to be resolved. This function doesn't need to be
26
- * called recursively, JSX will be transformed into these `jsx()` function calls.
27
- *
28
- * @param tag string or function component
29
- * @param props object containing all the properties and attributes passed to the element or component
30
- * @returns `AsyncGenerator` that yields `Chunk`s of HTML
31
- */
32
- export async function* jsx(tag, props) {
33
- if (typeof tag === "function") {
34
- // component or fragment
35
- yield* toGenerator(tag(props));
36
- return;
37
- }
38
- // intrinsic element
39
- // faster to concatenate attributes than to yield them as separate chunks
40
- let attributes = "";
41
- for (const key in props) {
42
- // more memory efficient to skip children instead of destructuring and using ...rest
43
- if (key === "children")
44
- continue;
45
- const value = props[key];
46
- if (value === true) {
47
- // just put the key without the value
48
- attributes += ` ${key}`;
49
- }
50
- else if (typeof value === "string") {
51
- attributes += ` ${key}="${Chunk.escape(value, true)}"`;
52
- }
53
- else if (typeof value === "number" || typeof value === "bigint") {
54
- attributes += ` ${key}="${value}"`;
55
- }
56
- // otherwise, don't include the attribute
57
- }
58
- if (tag === "html")
59
- yield Chunk.safe("<!doctype html>");
60
- yield Chunk.safe(`<${tag}${attributes}>`);
61
- if (voidElements.has(tag))
62
- return;
63
- yield* toGenerator(props.children);
64
- yield Chunk.safe(`</${tag}>`);
65
- }
66
- /**
67
- * JSX requires a `Fragment` export to resolve `<></>`
68
- *
69
- * @param props containing `children` to render
70
- * @returns async generator that yields concatenated children
71
- */
72
- export async function* Fragment(props = {}) {
73
- yield* toGenerator(props.children);
74
- }
75
- /**
76
- * @param element
77
- * @yields `Chunk`s of HTML as the `Element` resolves
78
- */
79
- export async function* toGenerator(element) {
80
- // modifications
81
- // these are required to allow functions to be used as children
82
- // instead of creating a separate component to use them
83
- if (typeof element === "function")
84
- element = element();
85
- if (element instanceof Promise)
86
- element = await element;
87
- // resolve based on type
88
- // should not render
89
- if (element == null || typeof element === "boolean" || element === "")
90
- return;
91
- if (element instanceof Chunk) {
92
- // already escaped or safe
93
- yield element;
94
- return;
95
- }
96
- if (typeof element === "object") {
97
- if (Symbol.asyncIterator in element) {
98
- // any async iterable - lazily resolve
99
- for await (const children of element)
100
- yield* 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));
101
38
  return;
102
39
  }
103
- if (Symbol.iterator in element) {
104
- // sync iterable
105
- if ("next" in element) {
106
- // sync generator - lazily resolve, avoids loading all in memory
107
- for (const children of element)
108
- yield* toGenerator(children);
109
- 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}`;
110
53
  }
111
- // other iterable - array, set, etc.
112
- // process children in parallel
113
- const generators = Array.from(element, toGenerator);
114
- const n = generators.length;
115
- const queue = new Array(n);
116
- const complete = new Uint8Array(n);
117
- let current = 0;
118
- for await (const m of merge(generators)) {
119
- if (m.result.done) {
120
- complete[m.index] = 1;
121
- if (m.index === current) {
122
- while (++current < n) {
123
- if (queue[current]) {
124
- // yield whatever is in the next queue even if it hasn't completed yet
125
- yield queue[current];
126
- queue[current] = null;
127
- }
128
- // if it hasn't completed, stop iterating to the next
129
- if (!complete[current])
130
- break;
131
- }
132
- }
133
- }
134
- else if (m.index === current) {
135
- yield m.result.value; // stream the current value directly
136
- }
137
- else {
138
- // queue the value for later
139
- if (queue[m.index])
140
- queue[m.index].concat(m.result.value);
141
- else
142
- queue[m.index] = m.result.value;
143
- }
54
+ else if (typeof value === "string") {
55
+ attributes += ` ${key}="${Render.escape(value, true)}"`;
144
56
  }
145
- // clear the queue
146
- yield* queue.filter((chunk) => chunk !== null);
147
- return;
57
+ else if (typeof value === "number" || typeof value === "bigint") {
58
+ attributes += ` ${key}="${value}"`;
59
+ }
60
+ // otherwise, don't include the attribute
148
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);
149
76
  }
150
- // primitive or other object
151
- yield new Chunk(element);
152
77
  }
153
- /**
154
- * Converts a `JSX.Element` into a fully concatenated string of HTML.
155
- *
156
- * ### WARNING
157
- *
158
- * This negates streaming benefits and buffers the result into a string.
159
- * Use `toGenerator` whenever possible.
160
- *
161
- * @param element
162
- * @returns Concatenated HTML
163
- */
164
- export const toString = async (element) => (await Array.fromAsync(toGenerator(element))).join("");
165
- /** Single encoder to use across requests. */
166
- const encoder = new TextEncoder();
167
- /**
168
- * `toGenerator` piped into a `ReadableStream`.
169
- * Use `toGenerator` when possible to avoid the overhead of the stream.
170
- *
171
- * @param element
172
- * @returns `ReadableStream` of HTML
173
- */
174
- export const toStream = (element) => {
175
- const gen = toGenerator(element);
176
- return new ReadableStream({
177
- // enables zero-copy transfer from underlying source when queue is empty
178
- // https://developer.mozilla.org/en-US/docs/Web/API/Streams_API/Using_readable_byte_streams#overview
179
- type: "bytes",
180
- // `pull` ensures backpressure and cancelled requests stop the generator
181
- async pull(c) {
182
- const result = await gen.next();
183
- if (result.done) {
184
- c.close();
185
- gen.return();
186
- return;
187
- }
188
- c.enqueue(
189
- // need to encode for Node (ex: during prerendering) or it will error
190
- // doesn't seem to be needed for browsers
191
- // faster than piping through a `TextEncoderStream`
192
- encoder.encode(result.value.value));
193
- },
194
- cancel() {
195
- gen.return();
196
- },
197
- }, {
198
- // `highWaterMark` defaults to 1
199
- // https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/ReadableStream#highwatermark
200
- // setting this ensures at least a small buffer is maintained if the
201
- // underlying server does not have its own high water mark set
202
- // https://blog.cloudflare.com/unpacking-cloudflare-workers-cpu-performance-benchmarks/#inefficient-streams-adapters
203
- // in Node, the default is 16kb, so this stacks another 2kb in front
204
- // https://nodejs.org/api/http.html#outgoingmessagewritablehighwatermark
205
- highWaterMark: 2048,
206
- });
207
- };
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"}