ovr 4.6.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.
- package/dist/LICENSE.md +26 -0
- package/dist/app/index.d.ts +55 -103
- package/dist/app/index.d.ts.map +1 -1
- package/dist/app/index.js +67 -145
- package/dist/context/index.d.ts +138 -0
- package/dist/context/index.d.ts.map +1 -0
- package/dist/context/index.js +218 -0
- package/dist/cookie/index.d.ts +121 -0
- package/dist/cookie/index.d.ts.map +1 -0
- package/dist/cookie/index.js +56 -0
- package/dist/index.d.ts +6 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -7
- package/dist/jsx/{intrinsic-elements.d.ts → elements.d.ts} +14 -7
- package/dist/jsx/elements.d.ts.map +1 -0
- package/dist/jsx/index.d.ts +37 -52
- package/dist/jsx/index.d.ts.map +1 -1
- package/dist/jsx/index.js +71 -197
- package/dist/middleware/index.d.ts +22 -0
- package/dist/middleware/index.d.ts.map +1 -0
- package/dist/multipart/index.d.ts +122 -0
- package/dist/multipart/index.d.ts.map +1 -0
- package/dist/multipart/index.js +341 -0
- package/dist/render/index.d.ts +78 -0
- package/dist/render/index.d.ts.map +1 -0
- package/dist/render/index.js +248 -0
- package/dist/route/index.d.ts +147 -0
- package/dist/route/index.d.ts.map +1 -0
- package/dist/route/index.js +126 -0
- package/dist/trie/index.d.ts +31 -26
- package/dist/trie/index.d.ts.map +1 -1
- package/dist/trie/index.js +71 -80
- package/dist/util/index.d.ts +38 -0
- package/dist/util/index.d.ts.map +1 -0
- package/dist/util/index.js +88 -0
- package/package.json +4 -4
- package/src/app/index.ts +128 -265
- package/src/context/index.ts +262 -0
- package/src/cookie/index.ts +189 -0
- package/src/index.ts +6 -8
- package/src/jsx/{intrinsic-elements.ts → elements.ts} +14 -7
- package/src/jsx/index.ts +81 -222
- package/src/middleware/index.ts +29 -0
- package/src/multipart/index.ts +486 -0
- package/src/render/index.ts +300 -0
- package/src/route/index.ts +332 -0
- package/src/trie/index.ts +90 -107
- package/src/util/index.ts +112 -0
- package/dist/app/context.d.ts +0 -147
- package/dist/app/context.d.ts.map +0 -1
- package/dist/app/context.js +0 -283
- package/dist/app/helper/get.d.ts +0 -30
- package/dist/app/helper/get.d.ts.map +0 -1
- package/dist/app/helper/get.js +0 -33
- package/dist/app/helper/index.d.ts +0 -55
- package/dist/app/helper/index.d.ts.map +0 -1
- package/dist/app/helper/index.js +0 -96
- package/dist/app/helper/post.d.ts +0 -35
- package/dist/app/helper/post.d.ts.map +0 -1
- package/dist/app/helper/post.js +0 -15
- package/dist/app/memo/index.d.ts +0 -12
- package/dist/app/memo/index.d.ts.map +0 -1
- package/dist/app/memo/index.js +0 -34
- package/dist/app/mw/csrf.d.ts +0 -23
- package/dist/app/mw/csrf.d.ts.map +0 -1
- package/dist/app/mw/csrf.js +0 -30
- package/dist/jsx/chunk/index.d.ts +0 -38
- package/dist/jsx/chunk/index.d.ts.map +0 -1
- package/dist/jsx/chunk/index.js +0 -69
- package/dist/jsx/intrinsic-elements.d.ts.map +0 -1
- package/dist/jsx/merge-async-generators.d.ts +0 -14
- package/dist/jsx/merge-async-generators.d.ts.map +0 -1
- package/dist/jsx/merge-async-generators.js +0 -42
- package/dist/types/index.d.ts +0 -17
- package/dist/types/index.d.ts.map +0 -1
- package/dist/util/hash.d.ts +0 -11
- package/dist/util/hash.d.ts.map +0 -1
- package/dist/util/hash.js +0 -16
- package/src/app/context.ts +0 -365
- package/src/app/helper/get.ts +0 -46
- package/src/app/helper/index.ts +0 -174
- package/src/app/helper/post.ts +0 -54
- package/src/app/memo/index.ts +0 -40
- package/src/app/mw/csrf.ts +0 -32
- package/src/jsx/chunk/index.ts +0 -79
- package/src/jsx/merge-async-generators.ts +0 -47
- package/src/types/index.ts +0 -42
- package/src/util/hash.ts +0 -17
- /package/dist/jsx/{intrinsic-elements.js → elements.js} +0 -0
- /package/dist/{types → middleware}/index.js +0 -0
package/src/trie/index.ts
CHANGED
|
@@ -1,67 +1,63 @@
|
|
|
1
|
-
|
|
1
|
+
import type { Route } from "../route/index.js";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
/** Route pattern */
|
|
5
|
-
pattern: string;
|
|
6
|
-
|
|
7
|
-
/** Store returned when route is found */
|
|
8
|
-
store: Store;
|
|
9
|
-
|
|
10
|
-
constructor(pattern: string, store: Store) {
|
|
11
|
-
if (pattern[0] !== "/") {
|
|
12
|
-
throw new Error(
|
|
13
|
-
`Invalid route: ${pattern} - route pattern must begin with "/"`,
|
|
14
|
-
);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
this.pattern = pattern;
|
|
18
|
-
this.store = store;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
class ParamNode<Store> {
|
|
3
|
+
class ParamNode {
|
|
23
4
|
/** Name of the parameter (without the colon ":") */
|
|
24
|
-
name: string;
|
|
5
|
+
readonly name: string;
|
|
25
6
|
|
|
26
7
|
/** Matched route */
|
|
27
|
-
route
|
|
8
|
+
route?: Route;
|
|
28
9
|
|
|
29
10
|
/** Static child node */
|
|
30
|
-
|
|
11
|
+
child?: Trie;
|
|
31
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Create a new parameter node.
|
|
15
|
+
*
|
|
16
|
+
* @param name Name of the parameter.
|
|
17
|
+
*/
|
|
32
18
|
constructor(name: string) {
|
|
33
19
|
this.name = name;
|
|
34
20
|
}
|
|
35
21
|
}
|
|
36
22
|
|
|
37
|
-
export
|
|
23
|
+
export namespace Trie {
|
|
24
|
+
/** Params created from the route match */
|
|
25
|
+
export type Params = Record<string, string>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export class Trie {
|
|
38
29
|
/** Unique segment of the pattern trie */
|
|
39
|
-
|
|
30
|
+
readonly seg: string;
|
|
40
31
|
|
|
41
32
|
/** Static child node map, key is the first character in the segment */
|
|
42
|
-
|
|
33
|
+
map?: Map<number, Trie>;
|
|
43
34
|
|
|
44
35
|
/** Parametric child node */
|
|
45
|
-
|
|
36
|
+
param?: ParamNode;
|
|
46
37
|
|
|
47
38
|
/** Matched route */
|
|
48
|
-
route
|
|
39
|
+
route?: Route;
|
|
49
40
|
|
|
50
41
|
/** Matched wildcard route */
|
|
51
|
-
|
|
42
|
+
wild?: Route;
|
|
43
|
+
|
|
44
|
+
static readonly #paramMatch = /:.+?(?=\/|$)/g;
|
|
45
|
+
static readonly #paramSplit = /:.+?(?=\/|$)/;
|
|
52
46
|
|
|
53
47
|
/**
|
|
48
|
+
* Create a new trie.
|
|
49
|
+
*
|
|
54
50
|
* @param segment pattern segment
|
|
55
|
-
* @param
|
|
51
|
+
* @param children static children nodes to add to staticMap
|
|
56
52
|
*/
|
|
57
|
-
constructor(segment = "/",
|
|
58
|
-
this.
|
|
53
|
+
constructor(segment = "/", children?: Trie[]) {
|
|
54
|
+
this.seg = segment;
|
|
59
55
|
|
|
60
|
-
if (
|
|
61
|
-
this.
|
|
56
|
+
if (children?.length) {
|
|
57
|
+
this.map ??= new Map();
|
|
62
58
|
|
|
63
|
-
for (const child of
|
|
64
|
-
this.
|
|
59
|
+
for (const child of children) {
|
|
60
|
+
this.map.set(child.seg.charCodeAt(0), child);
|
|
65
61
|
}
|
|
66
62
|
}
|
|
67
63
|
}
|
|
@@ -73,10 +69,10 @@ export class Trie<Store> {
|
|
|
73
69
|
clone(segment: string) {
|
|
74
70
|
const clone = new Trie(segment);
|
|
75
71
|
|
|
76
|
-
clone.
|
|
77
|
-
clone.
|
|
72
|
+
clone.map = this.map;
|
|
73
|
+
clone.param = this.param;
|
|
78
74
|
clone.route = this.route;
|
|
79
|
-
clone.
|
|
75
|
+
clone.wild = this.wild;
|
|
80
76
|
|
|
81
77
|
return clone;
|
|
82
78
|
}
|
|
@@ -91,13 +87,15 @@ export class Trie<Store> {
|
|
|
91
87
|
* @returns the new child produced from the new segment
|
|
92
88
|
*/
|
|
93
89
|
fork(charIndex: number, segment: string) {
|
|
94
|
-
const
|
|
95
|
-
const newChild = new Trie<Store>(segment.slice(charIndex)); // "movies/"
|
|
90
|
+
const newChild = new Trie(segment.slice(charIndex)); // "movies/"
|
|
96
91
|
|
|
97
92
|
Object.assign(
|
|
98
93
|
this,
|
|
99
94
|
// "api/" with the above as children
|
|
100
|
-
new Trie(this.
|
|
95
|
+
new Trie(this.seg.slice(0, charIndex), [
|
|
96
|
+
this.clone(this.seg.slice(charIndex)), // "posts/"
|
|
97
|
+
newChild,
|
|
98
|
+
]),
|
|
101
99
|
);
|
|
102
100
|
|
|
103
101
|
return newChild;
|
|
@@ -111,39 +109,33 @@ export class Trie<Store> {
|
|
|
111
109
|
* @param segment
|
|
112
110
|
*/
|
|
113
111
|
split(segment: string) {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
112
|
+
Object.assign(
|
|
113
|
+
this,
|
|
114
|
+
new Trie(segment, [this.clone(this.seg.slice(segment.length))]),
|
|
115
|
+
);
|
|
117
116
|
}
|
|
118
117
|
|
|
119
118
|
/**
|
|
120
119
|
* @param name name of the param
|
|
121
120
|
* @returns the existing child with the same name, or creates a new
|
|
122
121
|
*/
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
throw new Error(
|
|
126
|
-
`Cannot create parameter "${name}" because a different parameter ` +
|
|
127
|
-
`("${this.paramChild.name}") already exists in this location.\n\n${this}`,
|
|
128
|
-
);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
return (this.paramChild ??= new ParamNode<Store>(name));
|
|
122
|
+
set(name: string) {
|
|
123
|
+
return (this.param ??= new ParamNode(name));
|
|
132
124
|
}
|
|
133
125
|
|
|
134
126
|
/**
|
|
135
127
|
* @param route route return when pattern is matched
|
|
136
128
|
* @returns this - the Node
|
|
137
129
|
*/
|
|
138
|
-
add(route: Route
|
|
139
|
-
let current: Trie
|
|
140
|
-
let pattern = route.pattern; // created to not modify the original
|
|
130
|
+
add(route: Route) {
|
|
131
|
+
let current: Trie = this;
|
|
132
|
+
let pattern = route.method + route.pattern; // created to not modify the original
|
|
141
133
|
|
|
142
134
|
const endsWithWildcard = pattern.endsWith("*");
|
|
143
135
|
if (endsWithWildcard) pattern = pattern.slice(0, -1);
|
|
144
136
|
|
|
145
|
-
const paramSegments = pattern.match(
|
|
146
|
-
const staticSegments = pattern.split(
|
|
137
|
+
const paramSegments = pattern.match(Trie.#paramMatch) ?? [];
|
|
138
|
+
const staticSegments = pattern.split(Trie.#paramSplit);
|
|
147
139
|
|
|
148
140
|
// if the last segment is a param without a trailing slash
|
|
149
141
|
// then there will be an empty string, remove
|
|
@@ -162,26 +154,26 @@ export class Trie<Store> {
|
|
|
162
154
|
// there is only a second static segment (could just be "/")
|
|
163
155
|
// if there is a param to split them, so there must be a param here
|
|
164
156
|
|
|
165
|
-
const paramChild = current.
|
|
157
|
+
const paramChild = current.set(
|
|
166
158
|
// param without the ":" (only increment when this is reached)
|
|
167
159
|
paramSegments[paramIndex++]!.slice(1),
|
|
168
160
|
);
|
|
169
161
|
|
|
170
|
-
if (!paramChild.
|
|
162
|
+
if (!paramChild.child) {
|
|
171
163
|
// new - create node with the next static segment
|
|
172
|
-
current = paramChild.
|
|
164
|
+
current = paramChild.child = new Trie(staticSegment);
|
|
173
165
|
continue; // next segment - no need to check since it's new
|
|
174
166
|
}
|
|
175
167
|
|
|
176
168
|
// there's already a static child - need to check if it's a match
|
|
177
|
-
current = paramChild.
|
|
169
|
+
current = paramChild.child;
|
|
178
170
|
}
|
|
179
171
|
|
|
180
172
|
// check if the staticSegment matches the current node
|
|
181
173
|
for (let charIndex = 0; ; ) {
|
|
182
174
|
if (charIndex === staticSegment.length) {
|
|
183
175
|
// finished iterating through the staticSegment
|
|
184
|
-
if (charIndex < current.
|
|
176
|
+
if (charIndex < current.seg.length) {
|
|
185
177
|
// too short
|
|
186
178
|
current.split(staticSegment);
|
|
187
179
|
}
|
|
@@ -189,15 +181,15 @@ export class Trie<Store> {
|
|
|
189
181
|
break; // next segment
|
|
190
182
|
}
|
|
191
183
|
|
|
192
|
-
if (charIndex === current.
|
|
184
|
+
if (charIndex === current.seg.length) {
|
|
193
185
|
// passed the end of the current node
|
|
194
|
-
if (!current.
|
|
186
|
+
if (!current.map) {
|
|
195
187
|
// new pattern, create new leaf
|
|
196
|
-
current.
|
|
188
|
+
current.map = new Map();
|
|
197
189
|
} else {
|
|
198
190
|
// there's already static children,
|
|
199
191
|
// check to see if there's a leaf that starts with the char
|
|
200
|
-
const staticChild = current.
|
|
192
|
+
const staticChild = current.map.get(
|
|
201
193
|
staticSegment.charCodeAt(charIndex),
|
|
202
194
|
);
|
|
203
195
|
|
|
@@ -211,17 +203,14 @@ export class Trie<Store> {
|
|
|
211
203
|
}
|
|
212
204
|
|
|
213
205
|
// otherwise, add new staticChild
|
|
214
|
-
const staticChild = new Trie
|
|
215
|
-
current.
|
|
216
|
-
staticSegment.charCodeAt(charIndex),
|
|
217
|
-
staticChild,
|
|
218
|
-
);
|
|
206
|
+
const staticChild = new Trie(staticSegment.slice(charIndex));
|
|
207
|
+
current.map.set(staticSegment.charCodeAt(charIndex), staticChild);
|
|
219
208
|
current = staticChild;
|
|
220
209
|
|
|
221
210
|
break; // next segment
|
|
222
211
|
}
|
|
223
212
|
|
|
224
|
-
if (staticSegment[charIndex] !== current.
|
|
213
|
+
if (staticSegment[charIndex] !== current.seg[charIndex]) {
|
|
225
214
|
// different than the node - fork
|
|
226
215
|
current = current.fork(charIndex, staticSegment);
|
|
227
216
|
|
|
@@ -235,14 +224,13 @@ export class Trie<Store> {
|
|
|
235
224
|
|
|
236
225
|
if (paramIndex < paramSegments.length) {
|
|
237
226
|
// final segment is a param
|
|
238
|
-
current.
|
|
239
|
-
route;
|
|
227
|
+
current.set(paramSegments[paramIndex]!.slice(1)).route = route;
|
|
240
228
|
} else if (endsWithWildcard) {
|
|
241
229
|
// final segment is a wildcard
|
|
242
|
-
current.
|
|
230
|
+
current.wild = route;
|
|
243
231
|
} else {
|
|
244
232
|
// final segment is static
|
|
245
|
-
current.route
|
|
233
|
+
current.route = route;
|
|
246
234
|
}
|
|
247
235
|
|
|
248
236
|
return this;
|
|
@@ -252,67 +240,62 @@ export class Trie<Store> {
|
|
|
252
240
|
* @param pathname Path to find
|
|
253
241
|
* @returns `Route` and the matched `params` if found, otherwise `null`
|
|
254
242
|
*/
|
|
255
|
-
find(pathname: string): { route: Route
|
|
243
|
+
find(pathname: string): { route: Route; params: Trie.Params } | null {
|
|
244
|
+
const segmentLength = this.seg.length;
|
|
245
|
+
|
|
256
246
|
if (
|
|
257
247
|
// too short
|
|
258
|
-
pathname.length <
|
|
248
|
+
pathname.length < segmentLength ||
|
|
259
249
|
// segment does not match current node segment
|
|
260
|
-
!pathname.startsWith(this.
|
|
250
|
+
!pathname.startsWith(this.seg)
|
|
261
251
|
) {
|
|
262
252
|
return null;
|
|
263
253
|
}
|
|
264
254
|
|
|
265
|
-
if (pathname === this.
|
|
255
|
+
if (pathname === this.seg) {
|
|
266
256
|
// reached the end of the path
|
|
267
257
|
if (this.route) return { route: this.route, params: {} };
|
|
268
258
|
|
|
269
|
-
if (this.
|
|
270
|
-
return { route: this.wildcardRoute, params: { "*": "" } };
|
|
259
|
+
if (this.wild) return { route: this.wild, params: { "*": "" } };
|
|
271
260
|
|
|
272
261
|
return null;
|
|
273
262
|
}
|
|
274
263
|
|
|
275
|
-
if (this.
|
|
264
|
+
if (this.map) {
|
|
276
265
|
// check for a static leaf that starts with the first character
|
|
277
|
-
const staticChild = this.
|
|
278
|
-
pathname.charCodeAt(this.segment.length),
|
|
279
|
-
);
|
|
266
|
+
const staticChild = this.map.get(pathname.charCodeAt(segmentLength));
|
|
280
267
|
|
|
281
268
|
if (staticChild) {
|
|
282
|
-
const result = staticChild.find(pathname.slice(
|
|
269
|
+
const result = staticChild.find(pathname.slice(segmentLength));
|
|
283
270
|
if (result) return result;
|
|
284
271
|
}
|
|
285
272
|
}
|
|
286
273
|
|
|
287
274
|
// check for param leaf
|
|
288
|
-
if (this.
|
|
289
|
-
const slashIndex = pathname.indexOf("/",
|
|
275
|
+
if (this.param) {
|
|
276
|
+
const slashIndex = pathname.indexOf("/", segmentLength);
|
|
290
277
|
|
|
291
278
|
// if there is not a slash immediately following this.segment
|
|
292
|
-
if (slashIndex !==
|
|
279
|
+
if (slashIndex !== segmentLength) {
|
|
293
280
|
// there is a valid parameter
|
|
294
281
|
if (
|
|
295
282
|
// param is the end of the pathname
|
|
296
283
|
slashIndex === -1 &&
|
|
297
|
-
this.
|
|
284
|
+
this.param.route
|
|
298
285
|
) {
|
|
299
286
|
return {
|
|
300
|
-
route: this.
|
|
301
|
-
params: {
|
|
302
|
-
[this.paramChild.name]: pathname.slice(this.segment.length),
|
|
303
|
-
},
|
|
287
|
+
route: this.param.route,
|
|
288
|
+
params: { [this.param.name]: pathname.slice(segmentLength) },
|
|
304
289
|
};
|
|
305
|
-
} else if (this.
|
|
290
|
+
} else if (this.param.child) {
|
|
306
291
|
// there's a static node after the param
|
|
307
292
|
// this is how there can be multiple params, "/" in between
|
|
308
|
-
const result = this.
|
|
309
|
-
pathname.slice(slashIndex),
|
|
310
|
-
);
|
|
293
|
+
const result = this.param.child.find(pathname.slice(slashIndex));
|
|
311
294
|
|
|
312
295
|
if (result) {
|
|
313
296
|
// add original params to the result
|
|
314
|
-
result.params[this.
|
|
315
|
-
|
|
297
|
+
result.params[this.param.name] = pathname.slice(
|
|
298
|
+
segmentLength,
|
|
316
299
|
slashIndex,
|
|
317
300
|
);
|
|
318
301
|
|
|
@@ -323,10 +306,10 @@ export class Trie<Store> {
|
|
|
323
306
|
}
|
|
324
307
|
|
|
325
308
|
// check for wildcard leaf
|
|
326
|
-
if (this.
|
|
309
|
+
if (this.wild) {
|
|
327
310
|
return {
|
|
328
|
-
route: this.
|
|
329
|
-
params: { "*": pathname.slice(
|
|
311
|
+
route: this.wild,
|
|
312
|
+
params: { "*": pathname.slice(segmentLength) },
|
|
330
313
|
};
|
|
331
314
|
}
|
|
332
315
|
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/** General utility types */
|
|
2
|
+
export namespace Util {
|
|
3
|
+
export type MaybePromise<T> = T | Promise<T>;
|
|
4
|
+
export type MaybeFunction<T> = T | (() => T);
|
|
5
|
+
export type DeepArray<T> = T | DeepArray<T>[];
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/** Header parsing utils */
|
|
9
|
+
export class Header {
|
|
10
|
+
// shared content type header to minify better
|
|
11
|
+
static readonly contentType = "content-type";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param header header value to parse
|
|
15
|
+
* @returns Base/first param
|
|
16
|
+
*/
|
|
17
|
+
static shift(header: string | null): [string | null, string | null] {
|
|
18
|
+
if (header) {
|
|
19
|
+
const semi = header.indexOf(";");
|
|
20
|
+
if (semi !== -1) {
|
|
21
|
+
return [header.slice(0, semi), header.slice(semi)];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return [header, null];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @param header header value to parse
|
|
30
|
+
* @returns Map containing each key=value pair
|
|
31
|
+
*/
|
|
32
|
+
static params(header: string | null) {
|
|
33
|
+
const parsed: Record<string, string> = {};
|
|
34
|
+
|
|
35
|
+
if (header) {
|
|
36
|
+
const headerLength = header.length;
|
|
37
|
+
|
|
38
|
+
for (let cursor = 0; cursor < headerLength; ) {
|
|
39
|
+
const equal = header.indexOf("=", cursor);
|
|
40
|
+
if (equal === -1) break; // done
|
|
41
|
+
|
|
42
|
+
let semi = header.indexOf(";", cursor);
|
|
43
|
+
if (semi === -1) semi = headerLength;
|
|
44
|
+
|
|
45
|
+
if (equal > semi) {
|
|
46
|
+
// equal is in the next pair, current is malformed
|
|
47
|
+
// move the cursor to the start of the next and ignore
|
|
48
|
+
cursor = header.lastIndexOf(";", equal - 1) + 1;
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const key = header.slice(cursor, equal).trim();
|
|
53
|
+
|
|
54
|
+
if (!(key in parsed)) {
|
|
55
|
+
// first value should take precedence
|
|
56
|
+
|
|
57
|
+
let value = header.slice(equal + 1, semi).trim();
|
|
58
|
+
|
|
59
|
+
if (value[0] === '"' && value[value.length - 1] === '"') {
|
|
60
|
+
// remove quotes
|
|
61
|
+
value = value.slice(1, -1);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (value.includes("%")) {
|
|
65
|
+
// most values don't need to be decoded
|
|
66
|
+
try {
|
|
67
|
+
// cookies must be a simple string
|
|
68
|
+
value = decodeURIComponent(value);
|
|
69
|
+
} catch {
|
|
70
|
+
// ignore
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
parsed[key] = value;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
cursor = semi + 1;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return parsed;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Hash util */
|
|
86
|
+
export class Hash {
|
|
87
|
+
/**
|
|
88
|
+
* Fast hashing algorithm - [djb2](http://www.cse.yorku.ca/~oz/hash.html)
|
|
89
|
+
*
|
|
90
|
+
* @param s String to hash
|
|
91
|
+
* @returns Hashed string
|
|
92
|
+
*/
|
|
93
|
+
static djb2(s: string) {
|
|
94
|
+
let hash = 5381;
|
|
95
|
+
let i = s.length;
|
|
96
|
+
|
|
97
|
+
while (i) hash = (hash * 33) ^ s.charCodeAt(--i);
|
|
98
|
+
|
|
99
|
+
return (hash >>> 0).toString(36);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Shared encoding to use across features */
|
|
104
|
+
export class Codec {
|
|
105
|
+
static #encoder = new TextEncoder();
|
|
106
|
+
static #decoder = new TextDecoder("utf-8", { fatal: true });
|
|
107
|
+
|
|
108
|
+
static encode = (s: string) => Codec.#encoder.encode(s);
|
|
109
|
+
|
|
110
|
+
/** DO NOT USE FOR STREAMS */
|
|
111
|
+
static decode = (input?: Uint8Array) => Codec.#decoder.decode(input);
|
|
112
|
+
}
|
package/dist/app/context.d.ts
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import { type JSX } from "../jsx/index.js";
|
|
2
|
-
import type { Params, Route } from "../trie/index.js";
|
|
3
|
-
import { type ErrorHandler, type Middleware, type NotFoundHandler, type TrailingSlash } from "./index.js";
|
|
4
|
-
type Layout = (props: {
|
|
5
|
-
children: JSX.Element;
|
|
6
|
-
}) => JSX.Element;
|
|
7
|
-
export declare class Context<P extends Params = Params> {
|
|
8
|
-
#private;
|
|
9
|
-
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Request) */
|
|
10
|
-
req: Request;
|
|
11
|
-
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/URL) */
|
|
12
|
-
url: URL;
|
|
13
|
-
/**
|
|
14
|
-
* Route pattern parameters
|
|
15
|
-
*
|
|
16
|
-
* Given the route pattern `/posts/:slug` is added, a request made to
|
|
17
|
-
* `/posts/my-post` creates a `params` object `{ slug: "my-post" }`.
|
|
18
|
-
*
|
|
19
|
-
* @example { slug: "my-post" }
|
|
20
|
-
*/
|
|
21
|
-
params: P;
|
|
22
|
-
/** The matched `Route` instance. */
|
|
23
|
-
route: Route<Middleware<P>[]>;
|
|
24
|
-
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Headers) */
|
|
25
|
-
headers: Headers;
|
|
26
|
-
/** Passed from `app.error` */
|
|
27
|
-
error: ErrorHandler<P>;
|
|
28
|
-
/** Passed from `app.notFound` */
|
|
29
|
-
notFound: NotFoundHandler<P>;
|
|
30
|
-
memo: <A extends any[], R>(fn: (...args: A) => R) => (...args: A) => R;
|
|
31
|
-
constructor(req: Request, url: URL, trailingSlash: TrailingSlash, base: string, error: ErrorHandler<P>, notFound: NotFoundHandler<P>);
|
|
32
|
-
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status) */
|
|
33
|
-
get status(): number | undefined;
|
|
34
|
-
set status(status: number | undefined);
|
|
35
|
-
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response#body) */
|
|
36
|
-
get body(): BodyInit | null;
|
|
37
|
-
set body(body: BodyInit | null);
|
|
38
|
-
/** Passed from `app.base` */
|
|
39
|
-
get base(): string;
|
|
40
|
-
set base(base: string);
|
|
41
|
-
/**
|
|
42
|
-
* Mirrors `new Response()` constructor, set values with one function.
|
|
43
|
-
*
|
|
44
|
-
* @param body Response BodyInit
|
|
45
|
-
* @param init Enhanced ResponseInit
|
|
46
|
-
*/
|
|
47
|
-
res(body: BodyInit | null, init?: {
|
|
48
|
-
/**
|
|
49
|
-
* [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
|
|
50
|
-
*
|
|
51
|
-
* @default 200
|
|
52
|
-
*/
|
|
53
|
-
status?: number;
|
|
54
|
-
/** [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Headers) */
|
|
55
|
-
headers?: HeadersInit;
|
|
56
|
-
}): void;
|
|
57
|
-
/**
|
|
58
|
-
* Creates an HTML response.
|
|
59
|
-
*
|
|
60
|
-
* @param body HTML body
|
|
61
|
-
* @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
|
|
62
|
-
*/
|
|
63
|
-
html(body: BodyInit | null, status?: number): void;
|
|
64
|
-
/**
|
|
65
|
-
* Creates a JSON response.
|
|
66
|
-
*
|
|
67
|
-
* @param data passed into JSON.stringify to create the body
|
|
68
|
-
* @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
|
|
69
|
-
*/
|
|
70
|
-
json(data: any, status?: number): void;
|
|
71
|
-
/**
|
|
72
|
-
* Creates a plain text response.
|
|
73
|
-
*
|
|
74
|
-
* @param body response body
|
|
75
|
-
* @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
|
|
76
|
-
*/
|
|
77
|
-
text(body: BodyInit, status?: number): void;
|
|
78
|
-
/**
|
|
79
|
-
* Creates a redirect response.
|
|
80
|
-
*
|
|
81
|
-
* @param location redirect `Location` header
|
|
82
|
-
* @param status HTTP status code
|
|
83
|
-
*
|
|
84
|
-
* - [301 Moved Permanently](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/301)
|
|
85
|
-
* - [302 Found](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/302) (default)
|
|
86
|
-
* - [303 See Other](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/303)
|
|
87
|
-
* - [307 Temporary Redirect](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/307)
|
|
88
|
-
* - [308 Permanent Redirect](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/308)
|
|
89
|
-
*/
|
|
90
|
-
redirect(location: string | URL, status?: 301 | 302 | 303 | 307 | 308): void;
|
|
91
|
-
/**
|
|
92
|
-
* @param Element `JSX.Element` to add to the head
|
|
93
|
-
*/
|
|
94
|
-
head(...Element: JSX.Element[]): void;
|
|
95
|
-
/**
|
|
96
|
-
* @param Layout `Layout`(s) for the `Page` to be passed into before being injected into the `<body>`
|
|
97
|
-
*/
|
|
98
|
-
layout(...Layout: Layout[]): void;
|
|
99
|
-
/**
|
|
100
|
-
* Creates an HTML response based on the `head` elements, `Layout`(s), and `Page` provided.
|
|
101
|
-
*
|
|
102
|
-
* @param Page `JSX.Element` to inject into the `<body>`
|
|
103
|
-
* @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
|
|
104
|
-
*/
|
|
105
|
-
page(Page: JSX.Element, status?: number): void;
|
|
106
|
-
/**
|
|
107
|
-
* Generates an etag from a hash of the string provided.
|
|
108
|
-
* If the etag matches, sets the response to not modified.
|
|
109
|
-
*
|
|
110
|
-
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag)
|
|
111
|
-
*
|
|
112
|
-
* @param s string to hash
|
|
113
|
-
* @returns `true` if the etag matches, `false` otherwise
|
|
114
|
-
*/
|
|
115
|
-
etag(s: string): boolean;
|
|
116
|
-
/**
|
|
117
|
-
* @returns the constructed `Response`
|
|
118
|
-
*/
|
|
119
|
-
build(): Response;
|
|
120
|
-
/**
|
|
121
|
-
* Call within the scope of a handler to get the current context.
|
|
122
|
-
*
|
|
123
|
-
* @returns Request context
|
|
124
|
-
*
|
|
125
|
-
* @example
|
|
126
|
-
*
|
|
127
|
-
* ```ts
|
|
128
|
-
* import { Context } from "ovr";
|
|
129
|
-
*
|
|
130
|
-
* const app = new Router();
|
|
131
|
-
*
|
|
132
|
-
* const fn = () => {
|
|
133
|
-
* const c = Context.get();
|
|
134
|
-
* // ...
|
|
135
|
-
* }
|
|
136
|
-
*
|
|
137
|
-
* app.get("/", () => {
|
|
138
|
-
* fn(); // OK
|
|
139
|
-
* });
|
|
140
|
-
*
|
|
141
|
-
* fn() // ReferenceError - outside AsyncLocalStorage scope
|
|
142
|
-
* ```
|
|
143
|
-
*/
|
|
144
|
-
static get(): Context<Params>;
|
|
145
|
-
}
|
|
146
|
-
export {};
|
|
147
|
-
//# sourceMappingURL=context.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/app/context.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,GAAG,EAAY,MAAM,iBAAiB,CAAC;AACrD,OAAO,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAEtD,OAAO,EAEN,KAAK,YAAY,EACjB,KAAK,UAAU,EACf,KAAK,eAAe,EACpB,KAAK,aAAa,EAClB,MAAM,YAAY,CAAC;AAGpB,KAAK,MAAM,GAAG,CAAC,KAAK,EAAE;IAAE,QAAQ,EAAE,GAAG,CAAC,OAAO,CAAA;CAAE,KAAK,GAAG,CAAC,OAAO,CAAC;AAoBhE,qBAAa,OAAO,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM;;IAC7C,gFAAgF;IAChF,GAAG,EAAE,OAAO,CAAC;IAEb,4EAA4E;IAC5E,GAAG,EAAE,GAAG,CAAC;IAET;;;;;;;OAOG;IACH,MAAM,EAAG,CAAC,CAAC;IAEX,oCAAoC;IACpC,KAAK,EAAG,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;IAM/B,gFAAgF;IAChF,OAAO,UAAiB;IAUxB,8BAA8B;IAC9B,KAAK,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC;IAEvB,iCAAiC;IACjC,QAAQ,EAAE,eAAe,CAAC,CAAC,CAAC,CAAC;IAK7B,IAAI,mEAAkB;gBAUrB,GAAG,EAAE,OAAO,EACZ,GAAG,EAAE,GAAG,EACR,aAAa,EAAE,aAAa,EAC5B,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,YAAY,CAAC,CAAC,CAAC,EACtB,QAAQ,EAAE,eAAe,CAAC,CAAC,CAAC;IAU7B,gFAAgF;IAChF,IAAI,MAAM,uBAET;IACD,IAAI,MAAM,CAAC,MAAM,oBAAA,EAGhB;IAED,+FAA+F;IAC/F,IAAI,IAAI,oBAEP;IACD,IAAI,IAAI,CAAC,IAAI,iBAAA,EAGZ;IAED,6BAA6B;IAC7B,IAAI,IAAI,WAEP;IACD,IAAI,IAAI,CAAC,IAAI,QAAA,EAGZ;IAED;;;;;OAKG;IACH,GAAG,CACF,IAAI,EAAE,QAAQ,GAAG,IAAI,EACrB,IAAI,CAAC,EAAE;QACN;;;;WAIG;QACH,MAAM,CAAC,EAAE,MAAM,CAAC;QAEhB,gFAAgF;QAChF,OAAO,CAAC,EAAE,WAAW,CAAC;KACtB;IAYF;;;;;OAKG;IACH,IAAI,CAAC,IAAI,EAAE,QAAQ,GAAG,IAAI,EAAE,MAAM,CAAC,EAAE,MAAM;IAO3C;;;;;OAKG;IACH,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,MAAM;IAO/B;;;;;OAKG;IACH,IAAI,CAAC,IAAI,EAAE,QAAQ,EAAE,MAAM,CAAC,EAAE,MAAM;IAOpC;;;;;;;;;;;OAWG;IACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,GAAG,EAAE,MAAM,GAAE,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAS;IAK1E;;OAEG;IACH,IAAI,CAAC,GAAG,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE;IAK9B;;OAEG;IACH,MAAM,CAAC,GAAG,MAAM,EAAE,MAAM,EAAE;IAK1B;;;;;OAKG;IACH,IAAI,CAAC,IAAI,EAAE,GAAG,CAAC,OAAO,EAAE,MAAM,CAAC,EAAE,MAAM;IAuCvC;;;;;;;;OAQG;IACH,IAAI,CAAC,CAAC,EAAE,MAAM;IAed;;OAEG;IACH,KAAK;IA2BL;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,MAAM,CAAC,GAAG;CAUV"}
|