ovr 6.1.0 → 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 (43) hide show
  1. package/dist/LICENSE.md +2 -0
  2. package/dist/app/index.d.ts.map +1 -1
  3. package/dist/app/index.js +13 -9
  4. package/dist/context/index.d.ts +33 -9
  5. package/dist/context/index.d.ts.map +1 -1
  6. package/dist/context/index.js +61 -17
  7. package/dist/cookie/index.d.ts +1 -1
  8. package/dist/cookie/index.d.ts.map +1 -1
  9. package/dist/cookie/index.js +2 -2
  10. package/dist/index.d.ts +4 -3
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +3 -2
  13. package/dist/middleware/index.d.ts +12 -3
  14. package/dist/middleware/index.d.ts.map +1 -1
  15. package/dist/multipart/index.d.ts +5 -0
  16. package/dist/multipart/index.d.ts.map +1 -1
  17. package/dist/multipart/index.js +20 -22
  18. package/dist/render/index.d.ts +1 -0
  19. package/dist/render/index.d.ts.map +1 -1
  20. package/dist/render/index.js +6 -4
  21. package/dist/route/index.d.ts +143 -42
  22. package/dist/route/index.d.ts.map +1 -1
  23. package/dist/route/index.js +94 -42
  24. package/dist/schema/index.d.ts +1286 -0
  25. package/dist/schema/index.d.ts.map +1 -0
  26. package/dist/schema/index.js +1321 -0
  27. package/dist/trie/index.d.ts.map +1 -1
  28. package/dist/trie/index.js +8 -2
  29. package/dist/util/index.d.ts +113 -19
  30. package/dist/util/index.d.ts.map +1 -1
  31. package/dist/util/index.js +111 -35
  32. package/package.json +4 -1
  33. package/src/app/index.ts +13 -12
  34. package/src/context/index.ts +104 -25
  35. package/src/cookie/index.ts +3 -3
  36. package/src/index.ts +4 -3
  37. package/src/middleware/index.ts +19 -8
  38. package/src/multipart/index.ts +23 -24
  39. package/src/render/index.ts +7 -4
  40. package/src/route/index.ts +336 -95
  41. package/src/schema/index.ts +2794 -0
  42. package/src/trie/index.ts +12 -2
  43. package/src/util/index.ts +142 -36
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,16 +1,42 @@
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] } & {};
4
28
  }
5
29
 
6
30
  /** Media type utils */
7
31
  export class Mime {
8
- static readonly html = Mime.#text("html");
9
- static readonly text = Mime.#text("plain");
10
- static readonly json = Mime.#application("json");
11
- static readonly stream = Mime.#application("octet-stream");
12
- static readonly #multipartBase = "multipart/";
13
- static readonly multipartFormData = Mime.#multipart("form-data");
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;
14
40
 
15
41
  static #text<T extends string>(type: T) {
16
42
  return `text/${type}` as const;
@@ -20,12 +46,8 @@ export class Mime {
20
46
  return `application/${type}` as const;
21
47
  }
22
48
 
23
- static #multipart<T extends string>(type: T) {
24
- return `${Mime.#multipartBase}${type}` as const;
25
- }
26
-
27
49
  static readonly #markup = new Set<string>([
28
- Mime.html,
50
+ Mime.type.html,
29
51
  Mime.#text("xml"),
30
52
  Mime.#application("xml"),
31
53
  ]);
@@ -47,18 +69,25 @@ export class Mime {
47
69
  * @returns `true` if the mime is multipart
48
70
  */
49
71
  static multipart(mime: string | null) {
50
- return mime?.startsWith(Mime.#multipartBase);
72
+ return mime?.startsWith("multipart/");
51
73
  }
52
74
  }
53
75
 
54
76
  /** Header parsing utils */
55
77
  export class Header {
56
- static readonly type = "content-type";
57
- static readonly disposition = "content-disposition";
58
- static readonly etag = "etag";
59
- static readonly ifNoneMatch = "if-none-match";
60
- static readonly cookie = "cookie";
61
- static readonly setCookie = "set-cookie";
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;
62
91
 
63
92
  /**
64
93
  * @param mime
@@ -73,14 +102,11 @@ export class Header {
73
102
  * @returns Base/first param
74
103
  */
75
104
  static shift(header: string | null): [string | null, string | null] {
76
- if (header) {
77
- const semi = header.indexOf(";");
78
- if (semi !== -1) {
79
- return [header.slice(0, semi), header.slice(semi)];
80
- }
81
- }
105
+ const semi = header?.indexOf(";") ?? -1;
82
106
 
83
- return [header, null];
107
+ return semi === -1
108
+ ? [header, null]
109
+ : [header!.slice(0, semi), header!.slice(semi)];
84
110
  }
85
111
 
86
112
  /**
@@ -140,21 +166,21 @@ export class Header {
140
166
  }
141
167
  }
142
168
 
143
- /** Hash util */
144
- export class Hash {
169
+ /** Checksum util */
170
+ export class Checksum {
145
171
  /**
146
- * 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)
147
173
  *
148
- * @param s String to hash
149
- * @returns Hashed string
174
+ * @param s String to check
175
+ * @returns Checksum
150
176
  */
151
177
  static djb2(s: string) {
152
- let hash = 5381;
178
+ let c = 5381;
153
179
  let i = s.length;
154
180
 
155
- while (i) hash = (hash * 33) ^ s.charCodeAt(--i);
181
+ while (i) c = (c * 33) ^ s.charCodeAt(--i);
156
182
 
157
- return (hash >>> 0).toString(36);
183
+ return (c >>> 0).toString(36);
158
184
  }
159
185
  }
160
186
 
@@ -163,10 +189,75 @@ export class Codec {
163
189
  static #encoder = new TextEncoder();
164
190
  static #decoder = new TextDecoder("utf-8", { fatal: true });
165
191
 
166
- 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
+ }
167
213
 
168
- /** DO NOT USE FOR STREAMS */
169
- static decode = (input?: Uint8Array) => Codec.#decoder.decode(input);
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
+ };
170
261
  }
171
262
 
172
263
  /** HTTP methods */
@@ -175,3 +266,18 @@ export class Method {
175
266
  static readonly post = "POST";
176
267
  static readonly head = "HEAD";
177
268
  }
269
+
270
+ /** Byte size constants */
271
+ export class Size {
272
+ static readonly kb = 1024;
273
+ static readonly mb = 1024 * Size.kb;
274
+ }
275
+
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;
283
+ }