ovr 6.1.0 → 6.2.1
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.
- package/dist/LICENSE.md +2 -0
- package/dist/app/index.d.ts.map +1 -1
- package/dist/app/index.js +13 -9
- package/dist/context/index.d.ts +33 -9
- package/dist/context/index.d.ts.map +1 -1
- package/dist/context/index.js +61 -17
- package/dist/cookie/index.d.ts +1 -1
- package/dist/cookie/index.d.ts.map +1 -1
- package/dist/cookie/index.js +2 -2
- package/dist/index.d.ts +4 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/middleware/index.d.ts +12 -3
- package/dist/middleware/index.d.ts.map +1 -1
- package/dist/multipart/index.d.ts +5 -0
- package/dist/multipart/index.d.ts.map +1 -1
- package/dist/multipart/index.js +20 -22
- package/dist/render/index.d.ts +1 -0
- package/dist/render/index.d.ts.map +1 -1
- package/dist/render/index.js +6 -4
- package/dist/route/index.d.ts +143 -42
- package/dist/route/index.d.ts.map +1 -1
- package/dist/route/index.js +94 -42
- package/dist/schema/index.d.ts +1286 -0
- package/dist/schema/index.d.ts.map +1 -0
- package/dist/schema/index.js +1321 -0
- package/dist/trie/index.d.ts.map +1 -1
- package/dist/trie/index.js +8 -2
- package/dist/util/index.d.ts +113 -19
- package/dist/util/index.d.ts.map +1 -1
- package/dist/util/index.js +111 -35
- package/package.json +4 -1
- package/src/app/index.ts +13 -12
- package/src/context/index.ts +104 -25
- package/src/cookie/index.ts +3 -3
- package/src/index.ts +4 -3
- package/src/middleware/index.ts +19 -8
- package/src/multipart/index.ts +23 -24
- package/src/render/index.ts +7 -4
- package/src/route/index.ts +336 -95
- package/src/schema/index.ts +2798 -0
- package/src/trie/index.ts +12 -2
- 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
|
|
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
|
-
|
|
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
|
-
|
|
9
|
-
static
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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(
|
|
72
|
+
return mime?.startsWith("multipart/");
|
|
51
73
|
}
|
|
52
74
|
}
|
|
53
75
|
|
|
54
76
|
/** Header parsing utils */
|
|
55
77
|
export class Header {
|
|
56
|
-
|
|
57
|
-
static readonly
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
/**
|
|
144
|
-
export class
|
|
169
|
+
/** Checksum util */
|
|
170
|
+
export class Checksum {
|
|
145
171
|
/**
|
|
146
|
-
* Fast
|
|
172
|
+
* Fast checksum algorithm - [djb2](http://www.cse.yorku.ca/~oz/hash.html)
|
|
147
173
|
*
|
|
148
|
-
* @param s String to
|
|
149
|
-
* @returns
|
|
174
|
+
* @param s String to check
|
|
175
|
+
* @returns Checksum
|
|
150
176
|
*/
|
|
151
177
|
static djb2(s: string) {
|
|
152
|
-
let
|
|
178
|
+
let c = 5381;
|
|
153
179
|
let i = s.length;
|
|
154
180
|
|
|
155
|
-
while (i)
|
|
181
|
+
while (i) c = (c * 33) ^ s.charCodeAt(--i);
|
|
156
182
|
|
|
157
|
-
return (
|
|
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
|
-
|
|
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
|
-
|
|
169
|
-
|
|
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
|
+
}
|