ovr 1.3.0 → 2.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/app/action.d.ts +49 -0
- package/dist/app/action.js +31 -0
- package/dist/{router → app}/context.d.ts +42 -41
- package/dist/{router → app}/context.js +62 -54
- package/dist/app/index.d.ts +115 -0
- package/dist/app/index.js +168 -0
- package/dist/app/page.d.ts +41 -0
- package/dist/app/page.js +42 -0
- package/dist/escape/index.d.ts +4 -4
- package/dist/escape/index.js +6 -6
- package/dist/hash/index.d.ts +1 -0
- package/dist/hash/index.js +9 -0
- package/dist/index.d.ts +7 -5
- package/dist/index.js +6 -5
- package/dist/jsx/elements.d.ts +2 -2
- package/dist/jsx/index.d.ts +6 -7
- package/dist/jsx/index.js +8 -5
- package/dist/jsx-dev-runtime.d.ts +1 -1
- package/dist/jsx-dev-runtime.js +1 -1
- package/dist/jsx-runtime.d.ts +1 -1
- package/dist/jsx-runtime.js +1 -1
- package/dist/memo/index.d.ts +11 -0
- package/dist/memo/index.js +34 -0
- package/dist/trie/index.d.ts +2 -1
- package/dist/trie/insert-params.d.ts +7 -0
- package/dist/trie/insert-params.js +22 -0
- package/dist/types/index.d.ts +14 -0
- package/dist/types/index.js +1 -0
- package/package.json +5 -2
- package/dist/components/index.d.ts +0 -2
- package/dist/components/index.js +0 -3
- package/dist/components/suspense.d.ts +0 -33
- package/dist/components/suspense.js +0 -31
- package/dist/router/index.d.ts +0 -99
- package/dist/router/index.js +0 -131
package/dist/router/index.js
DELETED
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
import { Trie, Route } from "../trie/index.js";
|
|
2
|
-
import { Context } from "./context.js";
|
|
3
|
-
export class Router {
|
|
4
|
-
/** Built tries per HTTP method */
|
|
5
|
-
#trieMap = new Map();
|
|
6
|
-
/** Added routes per HTTP method */
|
|
7
|
-
#routesMap = new Map();
|
|
8
|
-
/** Global middleware */
|
|
9
|
-
#use = [];
|
|
10
|
-
#start;
|
|
11
|
-
#trailingSlash;
|
|
12
|
-
constructor(config = {}) {
|
|
13
|
-
this.#trailingSlash = config.trailingSlash ?? "never";
|
|
14
|
-
this.#start = config.start;
|
|
15
|
-
this.fetch = this.fetch.bind(this);
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Add global middleware.
|
|
19
|
-
*
|
|
20
|
-
* @param middleware
|
|
21
|
-
* @returns the router instance
|
|
22
|
-
*/
|
|
23
|
-
use(...middleware) {
|
|
24
|
-
this.#use.push(...middleware);
|
|
25
|
-
return this;
|
|
26
|
-
}
|
|
27
|
-
on(method, pattern, ...middleware) {
|
|
28
|
-
if (!Array.isArray(method))
|
|
29
|
-
method = [method];
|
|
30
|
-
let patterns;
|
|
31
|
-
if (!Array.isArray(pattern))
|
|
32
|
-
patterns = [pattern];
|
|
33
|
-
else
|
|
34
|
-
patterns = pattern;
|
|
35
|
-
for (const p of patterns) {
|
|
36
|
-
const route = new Route(p, middleware);
|
|
37
|
-
for (const m of method) {
|
|
38
|
-
const routes = this.#routesMap.get(m);
|
|
39
|
-
if (routes)
|
|
40
|
-
routes.push(route);
|
|
41
|
-
else
|
|
42
|
-
this.#routesMap.set(m, [route]);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
return this;
|
|
46
|
-
}
|
|
47
|
-
get(patternOrPatterns, ...middleware) {
|
|
48
|
-
return this.on("GET", patternOrPatterns, ...middleware);
|
|
49
|
-
}
|
|
50
|
-
post(patternOrPatterns, ...middleware) {
|
|
51
|
-
return this.on("POST", patternOrPatterns, ...middleware);
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* @param basePattern pattern to mount the router to, each route will begin with this base
|
|
55
|
-
* @param router sub-router to mount
|
|
56
|
-
* @returns the base router instance
|
|
57
|
-
*/
|
|
58
|
-
mount(basePattern, router) {
|
|
59
|
-
if (basePattern.at(-1) === "/")
|
|
60
|
-
basePattern = basePattern.slice(0, -1);
|
|
61
|
-
router.#routesMap.forEach((routes, method) => {
|
|
62
|
-
for (const route of routes) {
|
|
63
|
-
if (this.#trailingSlash !== "always" &&
|
|
64
|
-
route.pattern === "/" &&
|
|
65
|
-
basePattern !== "") {
|
|
66
|
-
route.pattern = "";
|
|
67
|
-
}
|
|
68
|
-
this.on(method, basePattern + route.pattern, ...route.store);
|
|
69
|
-
}
|
|
70
|
-
});
|
|
71
|
-
return this;
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* @param req [`Request` Reference](https://developer.mozilla.org/en-US/docs/Web/API/Request)
|
|
75
|
-
* @returns [`Response` Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response)
|
|
76
|
-
*/
|
|
77
|
-
async fetch(req) {
|
|
78
|
-
const c = new Context(req, new URL(req.url), this.#trailingSlash);
|
|
79
|
-
try {
|
|
80
|
-
if (this.#start)
|
|
81
|
-
c.state = this.#start(c);
|
|
82
|
-
let trie = this.#trieMap.get(req.method);
|
|
83
|
-
if (!trie) {
|
|
84
|
-
const routes = this.#routesMap.get(req.method);
|
|
85
|
-
if (routes) {
|
|
86
|
-
// build trie
|
|
87
|
-
trie = new Trie();
|
|
88
|
-
for (const route of routes)
|
|
89
|
-
trie.add(route);
|
|
90
|
-
this.#trieMap.set(req.method, trie);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
if (trie) {
|
|
94
|
-
const match = trie.find(c.url.pathname);
|
|
95
|
-
if (match) {
|
|
96
|
-
Object.assign(c, match);
|
|
97
|
-
await this.#compose([...this.#use, ...match.route.store])(c, () => Promise.resolve());
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
catch (error) {
|
|
102
|
-
if (c.error)
|
|
103
|
-
c.error(c, error);
|
|
104
|
-
else
|
|
105
|
-
throw error;
|
|
106
|
-
}
|
|
107
|
-
return c.build();
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Combines all middleware into a single function.
|
|
111
|
-
* Adapted from [koa-compose](https://github.com/koajs/compose/blob/master/index.js)
|
|
112
|
-
*
|
|
113
|
-
* @param middleware
|
|
114
|
-
* @returns single function middleware function
|
|
115
|
-
*/
|
|
116
|
-
#compose(middleware) {
|
|
117
|
-
return (c, next) => {
|
|
118
|
-
let index = -1;
|
|
119
|
-
const dispatch = async (i) => {
|
|
120
|
-
if (i <= index)
|
|
121
|
-
throw new Error("next() called multiple times");
|
|
122
|
-
index = i;
|
|
123
|
-
const mw = i === middleware.length ? next : middleware[i];
|
|
124
|
-
if (!mw)
|
|
125
|
-
return;
|
|
126
|
-
return mw(c, dispatch.bind(null, i + 1));
|
|
127
|
-
};
|
|
128
|
-
return dispatch(0);
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
}
|