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 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
+ };
@@ -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
- 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);
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
- 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());
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
- catch (error) {
102
- if (c.error)
103
- c.error(c, error);
104
- else
105
- throw error;
106
- }
107
- return c.build();
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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "ovr",
3
3
  "description": "Asynchronous streaming server framework",
4
- "version": "1.3.0",
4
+ "version": "1.4.0",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",