ovr 1.2.4 → 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
+ };
@@ -52,14 +52,14 @@ export declare class Router<State = null> {
52
52
  * @param middleware
53
53
  * @returns the router instance
54
54
  */
55
- on<Pattern extends string>(method: Method, pattern: Pattern, ...middleware: Middleware<State, ExtractParams<Pattern>>[]): this;
55
+ on<Pattern extends string>(method: Method | Method[], pattern: Pattern, ...middleware: Middleware<State, ExtractParams<Pattern>>[]): this;
56
56
  /**
57
57
  * @param method HTTP method
58
58
  * @param patterns array of route patterns
59
59
  * @param middleware
60
60
  * @returns the router instance
61
61
  */
62
- on<Patterns extends string[]>(method: Method, patterns: [...Patterns], ...middleware: Middleware<State, ExtractMultiParams<Patterns>>[]): this;
62
+ on<Patterns extends string[]>(method: Method | Method[], patterns: [...Patterns], ...middleware: Middleware<State, ExtractMultiParams<Patterns>>[]): this;
63
63
  /**
64
64
  * @param pattern route pattern
65
65
  * @param middleware
@@ -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();
@@ -25,6 +26,8 @@ export class Router {
25
26
  return this;
26
27
  }
27
28
  on(method, pattern, ...middleware) {
29
+ if (!Array.isArray(method))
30
+ method = [method];
28
31
  let patterns;
29
32
  if (!Array.isArray(pattern))
30
33
  patterns = [pattern];
@@ -32,11 +35,13 @@ export class Router {
32
35
  patterns = pattern;
33
36
  for (const p of patterns) {
34
37
  const route = new Route(p, middleware);
35
- const routes = this.#routesMap.get(method);
36
- if (routes)
37
- routes.push(route);
38
- else
39
- this.#routesMap.set(method, [route]);
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
+ }
40
45
  }
41
46
  return this;
42
47
  }
@@ -72,35 +77,37 @@ export class Router {
72
77
  */
73
78
  async fetch(req) {
74
79
  const c = new Context(req, new URL(req.url), this.#trailingSlash);
75
- try {
76
- if (this.#start)
77
- c.state = this.#start(c);
78
- let trie = this.#trieMap.get(req.method);
79
- if (!trie) {
80
- const routes = this.#routesMap.get(req.method);
81
- if (routes) {
82
- // build trie
83
- trie = new Trie();
84
- for (const route of routes)
85
- trie.add(route);
86
- 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
+ }
87
94
  }
88
- }
89
- if (trie) {
90
- const match = trie.find(c.url.pathname);
91
- if (match) {
92
- Object.assign(c, match);
93
- 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
+ }
94
101
  }
95
102
  }
96
- }
97
- catch (error) {
98
- if (c.error)
99
- c.error(c, error);
100
- else
101
- throw error;
102
- }
103
- 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
+ });
104
111
  }
105
112
  /**
106
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.2.4",
4
+ "version": "1.4.0",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",