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