ovr 1.3.0 → 1.4.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/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/router/get-context.d.ts +6 -0
- package/dist/router/get-context.js +9 -0
- package/dist/router/index.js +29 -26
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -2,5 +2,6 @@ export { Suspense } from "./components/suspense.js";
|
|
|
2
2
|
export { escape } from "./escape/index.js";
|
|
3
3
|
export { jsx, jsx as jsxs, jsx as jsxDEV, Fragment, toGenerator, toString, type FC, type JSX, } from "./jsx/index.js";
|
|
4
4
|
export { Context } from "./router/context.js";
|
|
5
|
+
export { context } from "./router/get-context.js";
|
|
5
6
|
export { Router, type Middleware, type Params } from "./router/index.js";
|
|
6
7
|
export { Trie, Route } from "./trie/index.js";
|
package/dist/index.js
CHANGED
|
@@ -2,5 +2,6 @@ export { Suspense } from "./components/suspense.js";
|
|
|
2
2
|
export { escape } from "./escape/index.js";
|
|
3
3
|
export { jsx, jsx as jsxs, jsx as jsxDEV, Fragment, toGenerator, toString, } from "./jsx/index.js";
|
|
4
4
|
export { Context } from "./router/context.js";
|
|
5
|
+
export { context } from "./router/get-context.js";
|
|
5
6
|
export { Router } from "./router/index.js";
|
|
6
7
|
export { Trie, Route } from "./trie/index.js";
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Context } from "./context.js";
|
|
2
|
+
import type { Params } from "./index.js";
|
|
3
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
4
|
+
export declare const asyncLocalContext: AsyncLocalStorage<Context<unknown, Params>>;
|
|
5
|
+
/** Helper to get the current context within a handler. */
|
|
6
|
+
export declare const context: () => Context<unknown, Params>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
export const asyncLocalContext = new AsyncLocalStorage();
|
|
3
|
+
/** Helper to get the current context within a handler. */
|
|
4
|
+
export const context = () => {
|
|
5
|
+
const c = asyncLocalContext.getStore();
|
|
6
|
+
if (!c)
|
|
7
|
+
throw new Error("Context not set: Context can only be obtained within a handler.");
|
|
8
|
+
return c;
|
|
9
|
+
};
|
package/dist/router/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Trie, Route } from "../trie/index.js";
|
|
2
2
|
import { Context } from "./context.js";
|
|
3
|
+
import { asyncLocalContext } from "./get-context.js";
|
|
3
4
|
export class Router {
|
|
4
5
|
/** Built tries per HTTP method */
|
|
5
6
|
#trieMap = new Map();
|
|
@@ -76,35 +77,37 @@ export class Router {
|
|
|
76
77
|
*/
|
|
77
78
|
async fetch(req) {
|
|
78
79
|
const c = new Context(req, new URL(req.url), this.#trailingSlash);
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
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
|
+
}
|
|
91
94
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
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
|
+
}
|
|
98
101
|
}
|
|
99
102
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
103
|
+
catch (error) {
|
|
104
|
+
if (c.error)
|
|
105
|
+
c.error(c, error);
|
|
106
|
+
else
|
|
107
|
+
throw error;
|
|
108
|
+
}
|
|
109
|
+
return c.build();
|
|
110
|
+
});
|
|
108
111
|
}
|
|
109
112
|
/**
|
|
110
113
|
* Combines all middleware into a single function.
|