ovr 6.0.1 → 6.2.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 (50) hide show
  1. package/dist/LICENSE.md +2 -0
  2. package/dist/app/index.d.ts +1 -1
  3. package/dist/app/index.d.ts.map +1 -1
  4. package/dist/app/index.js +17 -11
  5. package/dist/context/index.d.ts +37 -13
  6. package/dist/context/index.d.ts.map +1 -1
  7. package/dist/context/index.js +70 -29
  8. package/dist/cookie/index.d.ts +1 -1
  9. package/dist/cookie/index.d.ts.map +1 -1
  10. package/dist/cookie/index.js +2 -2
  11. package/dist/index.d.ts +4 -3
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +3 -2
  14. package/dist/jsx/elements.d.ts +194 -2
  15. package/dist/jsx/elements.d.ts.map +1 -1
  16. package/dist/jsx/index.d.ts.map +1 -1
  17. package/dist/jsx/index.js +18 -8
  18. package/dist/middleware/index.d.ts +12 -3
  19. package/dist/middleware/index.d.ts.map +1 -1
  20. package/dist/multipart/index.d.ts +5 -0
  21. package/dist/multipart/index.d.ts.map +1 -1
  22. package/dist/multipart/index.js +22 -24
  23. package/dist/render/index.d.ts +1 -0
  24. package/dist/render/index.d.ts.map +1 -1
  25. package/dist/render/index.js +6 -4
  26. package/dist/route/index.d.ts +143 -42
  27. package/dist/route/index.d.ts.map +1 -1
  28. package/dist/route/index.js +95 -43
  29. package/dist/schema/index.d.ts +1286 -0
  30. package/dist/schema/index.d.ts.map +1 -0
  31. package/dist/schema/index.js +1321 -0
  32. package/dist/trie/index.d.ts.map +1 -1
  33. package/dist/trie/index.js +8 -2
  34. package/dist/util/index.d.ts +138 -9
  35. package/dist/util/index.d.ts.map +1 -1
  36. package/dist/util/index.js +152 -19
  37. package/package.json +4 -1
  38. package/src/app/index.ts +17 -15
  39. package/src/context/index.ts +116 -40
  40. package/src/cookie/index.ts +5 -5
  41. package/src/index.ts +4 -3
  42. package/src/jsx/elements.ts +320 -64
  43. package/src/jsx/index.ts +16 -8
  44. package/src/middleware/index.ts +19 -8
  45. package/src/multipart/index.ts +25 -27
  46. package/src/render/index.ts +7 -4
  47. package/src/route/index.ts +338 -94
  48. package/src/schema/index.ts +2794 -0
  49. package/src/trie/index.ts +12 -2
  50. package/src/util/index.ts +193 -19
package/src/trie/index.ts CHANGED
@@ -54,7 +54,7 @@ export class Trie {
54
54
  this.seg = segment;
55
55
 
56
56
  if (children?.length) {
57
- this.map ??= new Map();
57
+ this.map = new Map();
58
58
 
59
59
  for (const child of children) {
60
60
  this.map.set(child.seg.charCodeAt(0), child);
@@ -120,7 +120,17 @@ export class Trie {
120
120
  * @returns the existing child with the same name, or creates a new
121
121
  */
122
122
  set(name: string) {
123
- return (this.param ??= new ParamNode(name));
123
+ if (this.param) {
124
+ if (this.param.name !== name) {
125
+ throw new TypeError(
126
+ `Conflicting param names "${this.param.name}" and "${name}"`,
127
+ );
128
+ }
129
+
130
+ return this.param;
131
+ }
132
+
133
+ return (this.param = new ParamNode(name));
124
134
  }
125
135
 
126
136
  /**
package/src/util/index.ts CHANGED
@@ -1,25 +1,112 @@
1
1
  /** General utility types */
2
2
  export namespace Util {
3
+ /**
4
+ * Function type wrapper that opts into bivariant parameter checking.
5
+ *
6
+ * Useful for public structural types that need to accept handlers from
7
+ * compatible but differently-parameterized generic instances.
8
+ *
9
+ * @template Fn Function type
10
+ */
11
+ export type Bivariant<Fn extends (...args: any[]) => any> = {
12
+ bivariant(...args: Parameters<Fn>): ReturnType<Fn>;
13
+ }["bivariant"];
14
+
15
+ /**
16
+ * Recursive array type that allows a value or nested arrays of that value.
17
+ *
18
+ * @template T Item type
19
+ */
3
20
  export type DeepArray<T> = T | DeepArray<T>[];
21
+
22
+ /**
23
+ * Flattens an inferred object type for improved readability in tooling.
24
+ *
25
+ * @template T Object type to normalize
26
+ */
27
+ export type Prettify<T> = { [K in keyof T]: T[K] } & {};
28
+ }
29
+
30
+ /** Media type utils */
31
+ export class Mime {
32
+ /** Mime type constants */
33
+ static type = {
34
+ html: Mime.#text("html"),
35
+ text: Mime.#text("plain"),
36
+ json: Mime.#application("json"),
37
+ stream: Mime.#application("octet-stream"),
38
+ mp: "multipart/form-data",
39
+ } as const;
40
+
41
+ static #text<T extends string>(type: T) {
42
+ return `text/${type}` as const;
43
+ }
44
+
45
+ static #application<T extends string>(type: T) {
46
+ return `application/${type}` as const;
47
+ }
48
+
49
+ static readonly #markup = new Set<string>([
50
+ Mime.type.html,
51
+ Mime.#text("xml"),
52
+ Mime.#application("xml"),
53
+ ]);
54
+
55
+ /**
56
+ * @param mime Media type
57
+ * @returns `true` if the mime is markup
58
+ */
59
+ static markup(mime: string) {
60
+ return (
61
+ Mime.#markup.has(mime) ||
62
+ // covers other xml types like svg
63
+ mime.includes("+xml")
64
+ );
65
+ }
66
+
67
+ /**
68
+ * @param mime Media type
69
+ * @returns `true` if the mime is multipart
70
+ */
71
+ static multipart(mime: string | null) {
72
+ return mime?.startsWith("multipart/");
73
+ }
4
74
  }
5
75
 
6
76
  /** Header parsing utils */
7
77
  export class Header {
8
- static readonly contentType = "content-type";
78
+ /** Header name constants */
79
+ static readonly name = {
80
+ type: "content-type",
81
+ disp: "content-disposition",
82
+ etag: "etag",
83
+ cache: "cache-control",
84
+ none: "if-none-match",
85
+ cookie: "cookie",
86
+ set: "set-cookie",
87
+ loc: "location",
88
+ origin: "origin",
89
+ fetch: "sec-fetch-site",
90
+ } as const;
91
+
92
+ /**
93
+ * @param mime
94
+ * @returns mime; charset=utf-8
95
+ */
96
+ static utf8<M extends string>(mime: M) {
97
+ return `${mime}; charset=utf-8` as const;
98
+ }
9
99
 
10
100
  /**
11
101
  * @param header header value to parse
12
102
  * @returns Base/first param
13
103
  */
14
104
  static shift(header: string | null): [string | null, string | null] {
15
- if (header) {
16
- const semi = header.indexOf(";");
17
- if (semi !== -1) {
18
- return [header.slice(0, semi), header.slice(semi)];
19
- }
20
- }
105
+ const semi = header?.indexOf(";") ?? -1;
21
106
 
22
- return [header, null];
107
+ return semi === -1
108
+ ? [header, null]
109
+ : [header!.slice(0, semi), header!.slice(semi)];
23
110
  }
24
111
 
25
112
  /**
@@ -79,21 +166,21 @@ export class Header {
79
166
  }
80
167
  }
81
168
 
82
- /** Hash util */
83
- export class Hash {
169
+ /** Checksum util */
170
+ export class Checksum {
84
171
  /**
85
- * Fast hashing algorithm - [djb2](http://www.cse.yorku.ca/~oz/hash.html)
172
+ * Fast checksum algorithm - [djb2](http://www.cse.yorku.ca/~oz/hash.html)
86
173
  *
87
- * @param s String to hash
88
- * @returns Hashed string
174
+ * @param s String to check
175
+ * @returns Checksum
89
176
  */
90
177
  static djb2(s: string) {
91
- let hash = 5381;
178
+ let c = 5381;
92
179
  let i = s.length;
93
180
 
94
- while (i) hash = (hash * 33) ^ s.charCodeAt(--i);
181
+ while (i) c = (c * 33) ^ s.charCodeAt(--i);
95
182
 
96
- return (hash >>> 0).toString(36);
183
+ return (c >>> 0).toString(36);
97
184
  }
98
185
  }
99
186
 
@@ -102,8 +189,95 @@ export class Codec {
102
189
  static #encoder = new TextEncoder();
103
190
  static #decoder = new TextDecoder("utf-8", { fatal: true });
104
191
 
105
- static encode = (s: string) => Codec.#encoder.encode(s);
192
+ /**
193
+ * Encodes a string into UTF-8 bytes.
194
+ *
195
+ * @param s String to encode
196
+ * @returns UTF-8 bytes
197
+ */
198
+ static encode(s: string) {
199
+ return Codec.#encoder.encode(s);
200
+ }
201
+
202
+ /**
203
+ * Decodes UTF-8 bytes into a string.
204
+ *
205
+ * Note: do not use this for incremental/stream decoding.
206
+ *
207
+ * @param bytes UTF-8 bytes
208
+ * @returns Decoded string
209
+ */
210
+ static decode(bytes?: Uint8Array) {
211
+ return Codec.#decoder.decode(bytes);
212
+ }
213
+
214
+ static readonly Base64 = class Base64 {
215
+ /**
216
+ * Encodes bytes into a Base64 string.
217
+ *
218
+ * @param bytes Bytes to encode
219
+ * @returns Base64 string
220
+ */
221
+ static encode(bytes: Uint8Array) {
222
+ return btoa(String.fromCharCode(...bytes));
223
+ }
224
+
225
+ /**
226
+ * Decodes a Base64 string into bytes.
227
+ *
228
+ * @param s Base64 string
229
+ * @returns Decoded bytes
230
+ */
231
+ static decode(s: string) {
232
+ return Uint8Array.from(atob(s), (c) => c.charCodeAt(0));
233
+ }
234
+ };
235
+
236
+ static readonly Base64Url = class Base64Url {
237
+ /**
238
+ * Encodes bytes into a Base64Url string.
239
+ *
240
+ * @param bytes Bytes to encode
241
+ * @returns Base64Url string
242
+ */
243
+ static encode(bytes: Uint8Array) {
244
+ return btoa(String.fromCharCode(...bytes))
245
+ .replace(/[+/]/g, (c) => (c === "+" ? "-" : "_"))
246
+ .replace(/=+$/, "");
247
+ }
248
+
249
+ /**
250
+ * Decodes a Base64Url string into bytes.
251
+ *
252
+ * @param s Base64Url string
253
+ * @returns Decoded bytes
254
+ */
255
+ static decode(s: string) {
256
+ const b64 = s.replace(/[-_]/g, (c) => (c === "-" ? "+" : "/"));
257
+
258
+ return Codec.Base64.decode(b64 + "=".repeat((4 - (b64.length % 4)) % 4));
259
+ }
260
+ };
261
+ }
262
+
263
+ /** HTTP methods */
264
+ export class Method {
265
+ static readonly get = "GET";
266
+ static readonly post = "POST";
267
+ static readonly head = "HEAD";
268
+ }
269
+
270
+ /** Byte size constants */
271
+ export class Size {
272
+ static readonly kb = 1024;
273
+ static readonly mb = 1024 * Size.kb;
274
+ }
106
275
 
107
- /** DO NOT USE FOR STREAMS */
108
- static decode = (input?: Uint8Array) => Codec.#decoder.decode(input);
276
+ /** Times in milliseconds */
277
+ export class Time {
278
+ static readonly second = 1000;
279
+ static readonly minute = 60 * Time.second;
280
+ static readonly hour = 60 * Time.minute;
281
+ static readonly day = 24 * Time.hour;
282
+ static readonly week = 7 * Time.day;
109
283
  }