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
@@ -1,54 +0,0 @@
1
- import type { ExtractParams } from "../../types/index.js";
2
- import { hash } from "../../util/hash.js";
3
- import type { Middleware } from "../index.js";
4
- import { Helper } from "./index.js";
5
-
6
- export class Post<Pattern extends string = string> extends Helper<Pattern> {
7
- /**
8
- * @param middleware POST middleware
9
- *
10
- * @example
11
- *
12
- * ```tsx
13
- * import { Post } from "ovr";
14
- *
15
- * const post = new Post((c) => {
16
- * console.log("posted");
17
- * c.redirect("/", 303);
18
- * });
19
- *
20
- * const page = new Get("/", () => (
21
- * <post.Form>
22
- * <input type="text" name="name" />
23
- * <button>Submit</button>
24
- * </post.Form>
25
- * ));
26
- *
27
- * app.add(page, post); // register
28
- * ```
29
- */
30
- constructor(...middleware: Middleware<{}>[]);
31
- /**
32
- * @param pattern Route pattern
33
- * @param middleware POST middleware
34
- */
35
- constructor(
36
- pattern: Pattern,
37
- ...middleware: Middleware<ExtractParams<Pattern>>[]
38
- );
39
- constructor(
40
- patternOrMiddleware: Pattern | Middleware<ExtractParams<Pattern>>,
41
- ...middleware: Middleware<ExtractParams<Pattern>>[]
42
- ) {
43
- let pattern: Pattern;
44
-
45
- if (typeof patternOrMiddleware === "string") {
46
- pattern = patternOrMiddleware;
47
- } else {
48
- middleware.unshift(patternOrMiddleware);
49
- pattern = `/_p/${hash(middleware.join())}` as Pattern;
50
- }
51
-
52
- super("POST", pattern, ...middleware);
53
- }
54
- }
@@ -1,40 +0,0 @@
1
- export class Memo {
2
- /** Already memoized functions. */
3
- #memoized = new WeakMap<Function, Function>();
4
-
5
- /** Symbol to store the result of each memoized function. */
6
- static #RESULT = Symbol();
7
-
8
- /**
9
- * Calls to the returned function with the same arguments will
10
- * return a cached result.
11
- *
12
- * @param fn Function to memoize.
13
- * @returns The memoized function.
14
- */
15
- use<A extends any[], R>(fn: (...args: A) => R) {
16
- if (this.#memoized.has(fn)) return this.#memoized.get(fn) as typeof fn;
17
-
18
- // closure will GC automatically
19
- const trie = new Map();
20
-
21
- const memo = (...args: A): R => {
22
- let node = trie;
23
-
24
- for (const arg of args) {
25
- // get/create a node for each arg
26
- if (!node.has(arg)) node.set(arg, new Map());
27
- node = node.get(arg);
28
- }
29
-
30
- // set the final result if not there
31
- if (!node.has(Memo.#RESULT)) node.set(Memo.#RESULT, fn(...args));
32
-
33
- return node.get(Memo.#RESULT);
34
- };
35
-
36
- this.#memoized.set(fn, memo);
37
-
38
- return memo;
39
- }
40
- }
@@ -1,79 +0,0 @@
1
- export class Chunk {
2
- static #attr = /[&"<]/g;
3
- static #content = /[&<]/g;
4
-
5
- /** Safe value to render */
6
- value: string;
7
-
8
- /**
9
- * `Chunk` to send in an HTML stream.
10
- *
11
- * @param html string of HTML to escape
12
- * @param safe Set to `true` if the HTML is safe and should not be escaped
13
- */
14
- constructor(html: unknown, safe?: boolean) {
15
- const value = String(html ?? "");
16
- this.value = safe ? value : Chunk.escape(value);
17
- }
18
-
19
- /**
20
- * @returns `value`
21
- */
22
- toString() {
23
- return this.value;
24
- }
25
-
26
- /**
27
- * @param chunks Chunks to append to the end of the chunk
28
- */
29
- concat(...chunks: Chunk[]) {
30
- this.value += chunks.join("");
31
- }
32
-
33
- /**
34
- * Wraps `new Chunk("html", true)`.
35
- *
36
- * Use to create a new `Chunk` and bypass escaping.
37
- *
38
- * @param html Safe string of HTML
39
- * @returns New _safe_ `Chunk`
40
- */
41
- static safe(html: unknown) {
42
- return new Chunk(html, true);
43
- }
44
-
45
- /**
46
- * Escapes strings of HTML.
47
- *
48
- * @param html String to escape
49
- * @param attr Set to `true` if the value is an attribute, otherwise it's a string of HTML content
50
- * @returns Escaped string of HTML
51
- */
52
- static escape(html: string, attr?: boolean) {
53
- // adapted from https://github.com/sveltejs/svelte/blob/main/packages/svelte/src/escaping.js
54
- const regex = attr ? Chunk.#attr : Chunk.#content;
55
-
56
- // search starts at beginning of the string
57
- regex.lastIndex = 0;
58
- // tracks the position of the last successful match in the input string
59
- let start = 0;
60
- let result = "";
61
-
62
- // since `g` flag is used, regex maintains state with each loop,
63
- // checking from the lastIndex onward each time
64
- while (regex.test(html)) {
65
- // index of the match
66
- const i = regex.lastIndex - 1;
67
- const match = html[i];
68
-
69
- result +=
70
- // everything that didn't match during this test
71
- html.slice(start, i) +
72
- // replacement
73
- (match === "&" ? "&amp;" : match === '"' ? "&quot;" : "&lt;");
74
- start = regex.lastIndex;
75
- }
76
-
77
- return result + html.slice(start);
78
- }
79
- }
@@ -1,47 +0,0 @@
1
- const next = async <T, R>(iterator: AsyncIterator<T, R>, index: number) => ({
2
- index,
3
- result: await iterator.next(),
4
- });
5
-
6
- /**
7
- * Merges `AsyncGenerator[]` into a single `AsyncGenerator`, resolving all in parallel.
8
- * The return of each `AsyncGenerator` is yielded from the generator with `done: true`.
9
- *
10
- * Adapted from [stack overflow answers](https://stackoverflow.com/questions/50585456).
11
- *
12
- * @param generators Resolved in parallel.
13
- * @yields `IteratorResult` and `index` of the resolved generator.
14
- */
15
- export async function* merge<T>(generators: AsyncGenerator<T, void>[]) {
16
- const iterators = generators.map((gen) => gen[Symbol.asyncIterator]());
17
- const promises = new Map<number, ReturnType<typeof next<T, void>>>();
18
-
19
- for (let i = 0; i < iterators.length; i++) {
20
- promises.set(i, next(iterators[i]!, i));
21
- }
22
-
23
- let current: Awaited<ReturnType<typeof next>>;
24
-
25
- try {
26
- while (promises.size > 0) {
27
- yield (current = await Promise.race(promises.values()));
28
-
29
- if (current.result.done) {
30
- promises.delete(current.index);
31
- } else {
32
- promises.set(
33
- current.index,
34
- next(iterators[current.index]!, current.index),
35
- );
36
- }
37
- }
38
- } finally {
39
- for (const iterator of iterators) {
40
- try {
41
- iterator.return();
42
- } catch {
43
- // could have already returned
44
- }
45
- }
46
- }
47
- }
@@ -1,42 +0,0 @@
1
- import type { Params } from "../trie/index.js";
2
-
3
- export type MaybePromise<T> = T | Promise<T>;
4
-
5
- export type MaybeFunction<T> = T | (() => T);
6
-
7
- export type DeepArray<T> = T | DeepArray<T>[];
8
-
9
- export type ExtractParams<Pattern extends string = string> =
10
- Pattern extends `${infer _Start}:${infer Param}/${infer Rest}`
11
- ? { [k in Param | keyof ExtractParams<Rest>]: string }
12
- : Pattern extends `${infer _Start}:${infer Param}`
13
- ? { [k in Param]: string }
14
- : Pattern extends `${infer _Rest}*`
15
- ? { "*": string }
16
- : {};
17
-
18
- export type ExtractMultiParams<Patterns extends string[]> = Patterns extends [
19
- infer First extends string,
20
- ...infer Rest extends string[],
21
- ]
22
- ? Rest["length"] extends 0
23
- ? ExtractParams<First>
24
- : ExtractParams<First> | ExtractMultiParams<Rest>
25
- : never;
26
-
27
- export type InsertParams<
28
- Pattern extends string,
29
- P extends Params,
30
- > = Pattern extends `${infer Start}:${infer Param}/${infer Rest}`
31
- ? Param extends keyof P
32
- ? `${Start}${P[Param]}/${InsertParams<Rest, P>}`
33
- : Pattern
34
- : Pattern extends `${infer Start}:${infer Param}`
35
- ? Param extends keyof P
36
- ? `${Start}${P[Param]}`
37
- : Pattern
38
- : Pattern extends `${infer Start}*`
39
- ? "*" extends keyof P
40
- ? `${Start}${P["*"]}`
41
- : Pattern
42
- : Pattern;
package/src/util/hash.ts DELETED
@@ -1,17 +0,0 @@
1
- /**
2
- * Fast hashing algorithm - [djb2](http://www.cse.yorku.ca/~oz/hash.html)
3
- *
4
- * Adapted from
5
- * [SvelteKit](https://github.com/sveltejs/kit/blob/25d459104814b0c2dc6b4cf73b680378a29d8200/packages/kit/src/runtime/hash.js)
6
- *
7
- * @param s String to hash
8
- * @returns Hashed string
9
- */
10
- export const hash = (s: string) => {
11
- let hash = 5381;
12
- let i = s.length;
13
-
14
- while (i) hash = (hash * 33) ^ s.charCodeAt(--i);
15
-
16
- return (hash >>> 0).toString(36);
17
- };
File without changes
File without changes