ovr 6.2.4 → 7.0.0-next.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.
Files changed (136) hide show
  1. package/README.md +3 -1
  2. package/dist/app/index.d.ts +51 -7
  3. package/dist/app/index.d.ts.map +1 -1
  4. package/dist/app/index.js +163 -28
  5. package/dist/auth/auth-data.d.ts +26 -0
  6. package/dist/auth/auth-data.d.ts.map +1 -0
  7. package/dist/auth/auth-data.js +42 -0
  8. package/dist/auth/cbor.d.ts +50 -0
  9. package/dist/auth/cbor.d.ts.map +1 -0
  10. package/dist/auth/cbor.js +200 -0
  11. package/dist/auth/client-source.d.ts +3 -0
  12. package/dist/auth/client-source.d.ts.map +1 -0
  13. package/dist/auth/client-source.js +3 -0
  14. package/dist/auth/client.d.ts +3 -0
  15. package/dist/auth/client.d.ts.map +1 -0
  16. package/dist/auth/client.js +186 -0
  17. package/dist/auth/configure.d.ts +28 -0
  18. package/dist/auth/configure.d.ts.map +1 -0
  19. package/dist/auth/configure.js +228 -0
  20. package/dist/auth/der.d.ts +6 -0
  21. package/dist/auth/der.d.ts.map +1 -0
  22. package/dist/auth/der.js +36 -0
  23. package/dist/auth/index.d.ts +242 -0
  24. package/dist/auth/index.d.ts.map +1 -0
  25. package/dist/auth/index.js +152 -0
  26. package/dist/auth/issue.d.ts +36 -0
  27. package/dist/auth/issue.d.ts.map +1 -0
  28. package/dist/auth/issue.js +47 -0
  29. package/dist/auth/metadata.d.ts +22 -0
  30. package/dist/auth/metadata.d.ts.map +1 -0
  31. package/dist/auth/metadata.js +134 -0
  32. package/dist/auth/oauth.d.ts +29 -0
  33. package/dist/auth/oauth.d.ts.map +1 -0
  34. package/dist/auth/oauth.js +724 -0
  35. package/dist/auth/passkey.d.ts +101 -0
  36. package/dist/auth/passkey.d.ts.map +1 -0
  37. package/dist/auth/passkey.js +393 -0
  38. package/dist/auth/state.d.ts +49 -0
  39. package/dist/auth/state.d.ts.map +1 -0
  40. package/dist/auth/state.js +184 -0
  41. package/dist/auth/storage.d.ts +193 -0
  42. package/dist/auth/storage.d.ts.map +1 -0
  43. package/dist/auth/storage.js +410 -0
  44. package/dist/auth/token.d.ts +12 -0
  45. package/dist/auth/token.d.ts.map +1 -0
  46. package/dist/auth/token.js +39 -0
  47. package/dist/context/index.d.ts +86 -16
  48. package/dist/context/index.d.ts.map +1 -1
  49. package/dist/context/index.js +166 -42
  50. package/dist/cookie/index.d.ts +3 -0
  51. package/dist/cookie/index.d.ts.map +1 -1
  52. package/dist/cookie/index.js +1 -0
  53. package/dist/database/index.d.ts +262 -0
  54. package/dist/database/index.d.ts.map +1 -0
  55. package/dist/database/index.js +807 -0
  56. package/dist/form/index.d.ts +699 -0
  57. package/dist/form/index.d.ts.map +1 -0
  58. package/dist/form/index.js +901 -0
  59. package/dist/index.d.ts +10 -3
  60. package/dist/index.d.ts.map +1 -1
  61. package/dist/index.js +9 -3
  62. package/dist/jsx/elements.d.ts +28 -2
  63. package/dist/jsx/elements.d.ts.map +1 -1
  64. package/dist/jsx/index.d.ts +20 -4
  65. package/dist/jsx/index.d.ts.map +1 -1
  66. package/dist/jsx/index.js +6 -65
  67. package/dist/limiter/index.d.ts +65 -0
  68. package/dist/limiter/index.d.ts.map +1 -0
  69. package/dist/limiter/index.js +157 -0
  70. package/dist/markdown/index.d.ts +61 -0
  71. package/dist/markdown/index.d.ts.map +1 -0
  72. package/dist/markdown/index.js +1160 -0
  73. package/dist/markup/index.d.ts +188 -0
  74. package/dist/markup/index.d.ts.map +1 -0
  75. package/dist/markup/index.js +718 -0
  76. package/dist/mcp/index.d.ts +73 -0
  77. package/dist/mcp/index.d.ts.map +1 -0
  78. package/dist/mcp/index.js +988 -0
  79. package/dist/middleware/index.d.ts +3 -4
  80. package/dist/middleware/index.d.ts.map +1 -1
  81. package/dist/multipart/index.d.ts +53 -7
  82. package/dist/multipart/index.d.ts.map +1 -1
  83. package/dist/multipart/index.js +332 -124
  84. package/dist/route/index.d.ts +226 -80
  85. package/dist/route/index.d.ts.map +1 -1
  86. package/dist/route/index.js +239 -99
  87. package/dist/schema/index.d.ts +307 -734
  88. package/dist/schema/index.d.ts.map +1 -1
  89. package/dist/schema/index.js +783 -1050
  90. package/dist/table/index.d.ts +132 -0
  91. package/dist/table/index.d.ts.map +1 -0
  92. package/dist/table/index.js +188 -0
  93. package/dist/trie/index.js +4 -4
  94. package/dist/util/index.d.ts +37 -5
  95. package/dist/util/index.d.ts.map +1 -1
  96. package/dist/util/index.js +156 -41
  97. package/package.json +11 -4
  98. package/src/app/index.ts +230 -42
  99. package/src/auth/auth-data.ts +56 -0
  100. package/src/auth/cbor.ts +231 -0
  101. package/src/auth/client-source.ts +3 -0
  102. package/src/auth/client.ts +205 -0
  103. package/src/auth/configure.ts +279 -0
  104. package/src/auth/der.ts +35 -0
  105. package/src/auth/index.ts +387 -0
  106. package/src/auth/issue.ts +52 -0
  107. package/src/auth/metadata.ts +149 -0
  108. package/src/auth/oauth.ts +882 -0
  109. package/src/auth/passkey.ts +556 -0
  110. package/src/auth/state.ts +213 -0
  111. package/src/auth/storage.ts +572 -0
  112. package/src/auth/token.ts +67 -0
  113. package/src/context/index.ts +240 -65
  114. package/src/cookie/index.ts +3 -0
  115. package/src/database/index.ts +1193 -0
  116. package/src/form/index.ts +1720 -0
  117. package/src/index.ts +10 -3
  118. package/src/jsx/elements.ts +45 -25
  119. package/src/jsx/index.ts +24 -69
  120. package/src/limiter/index.ts +221 -0
  121. package/src/markdown/index.ts +1322 -0
  122. package/src/markup/index.ts +853 -0
  123. package/src/mcp/conformance.2025-11-25.yml +39 -0
  124. package/src/mcp/conformance.2026-07-28.yml +56 -0
  125. package/src/mcp/index.ts +1262 -0
  126. package/src/middleware/index.ts +5 -6
  127. package/src/multipart/index.ts +410 -158
  128. package/src/route/index.ts +559 -278
  129. package/src/schema/index.ts +1542 -2256
  130. package/src/table/index.ts +387 -0
  131. package/src/trie/index.ts +4 -4
  132. package/src/util/index.ts +163 -49
  133. package/dist/render/index.d.ts +0 -79
  134. package/dist/render/index.d.ts.map +0 -1
  135. package/dist/render/index.js +0 -257
  136. package/src/render/index.ts +0 -314
package/README.md CHANGED
@@ -1,3 +1,5 @@
1
1
  # ovr
2
2
 
3
- Repository for the core `ovr` package.
3
+ Web apps anyone can use. Write each route once: people get pages and forms, agents get Markdown and MCP tools, through the same handler.
4
+
5
+ ovr supports only the latest Node.js LTS release and current browsers.
@@ -1,8 +1,51 @@
1
1
  import type { Middleware } from "../middleware/index.js";
2
- import type { Multipart } from "../multipart/index.js";
2
+ import { type Multipart } from "../multipart/index.js";
3
3
  import { Route } from "../route/index.js";
4
4
  import { type Util } from "../util/index.js";
5
+ /** Internal protocol for services that `app.use` mounts, such as `Auth`. */
6
+ export declare const mount: unique symbol;
7
+ /** Asks an authorization service for the routes agents sign in with, see `Mcp` */
8
+ export declare const agents: unique symbol;
9
+ /** Routes that accept cross-origin requests without cookies, such as OAuth endpoints. */
10
+ export declare const crossOrigin: WeakSet<Route<string, any, any, any>>;
11
+ /**
12
+ * Routes that run without the app's global middleware, such as an MCP
13
+ * endpoint, which makes its own request to the app for each call.
14
+ */
15
+ export declare const direct: WeakSet<Route<string, any, any, any>>;
5
16
  export declare namespace App {
17
+ /** What a service can use from the app it is mounted on. */
18
+ interface Mount {
19
+ /** Every route the app has, including ones added later */
20
+ readonly routes: readonly Route[];
21
+ /** The app's request handler */
22
+ readonly fetch: App["fetch"];
23
+ /** Most memory a request body may take, the app's `multipart.memory` */
24
+ readonly memory: number;
25
+ /**
26
+ * @param method Request method
27
+ * @param pathname URL pathname
28
+ * @returns The route a request would run and its parameters, before
29
+ * running it
30
+ */
31
+ find(method: string, pathname: string): {
32
+ route: Route;
33
+ params: Record<string, string>;
34
+ } | undefined;
35
+ }
36
+ /** A service that adds its routes and middleware to the app. */
37
+ interface Service {
38
+ /**
39
+ * @param app The app the service is mounted on
40
+ * @returns Routes and global middleware to use
41
+ */
42
+ [mount](app: Mount): Util.DeepArray<Route | Middleware>;
43
+ }
44
+ /** Fetch options plus server-supplied connection metadata. */
45
+ interface Fetch extends RequestInit {
46
+ /** Trusted socket peer or verified edge client IP. Never copy an arbitrary forwarded header. */
47
+ readonly ip?: string | undefined;
48
+ }
6
49
  namespace Options {
7
50
  /** Trailing slash preference */
8
51
  type TrailingSlash = "always" | "never" | "ignore";
@@ -15,7 +58,8 @@ export declare namespace App {
15
58
  * protection that checks the request's `method`, and its
16
59
  * [`Sec-Fetch-Site`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Sec-Fetch-Site)
17
60
  * and [`Origin`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Origin)
18
- * headers.
61
+ * headers. Requests with neither header nor a `Cookie` are not from a
62
+ * browser and are allowed.
19
63
  *
20
64
  * More robust protection requires a stateful server or a database to store
21
65
  * [CSRF tokens](https://developer.mozilla.org/en-US/docs/Web/Security/Attacks/CSRF#csrf_tokens).
@@ -35,9 +79,9 @@ export declare namespace App {
35
79
  readonly trailingSlash?: Options.TrailingSlash;
36
80
  /**
37
81
  * Multipart form data options
38
- * (overridden by options passed into `Context.form(options)`)
82
+ * (overridden by options passed into `Context.multipart(options)`)
39
83
  */
40
- readonly form?: Multipart.Options;
84
+ readonly multipart?: Multipart.Options;
41
85
  };
42
86
  }
43
87
  /** Web server application */
@@ -50,10 +94,10 @@ export declare class App {
50
94
  */
51
95
  constructor(options?: App.Options);
52
96
  /**
53
- * @param routes Route or middleware to use
97
+ * @param routes Routes, middleware, or services such as `Auth` to use
54
98
  * @returns `App` instance
55
99
  */
56
- use(...routes: Util.DeepArray<Route | Middleware | Record<string, Route | Middleware>>[]): this;
100
+ use(...routes: Util.DeepArray<Route | Middleware | App.Service | Record<string, Route | Middleware>>[]): this;
57
101
  /**
58
102
  * Request a resource from the application.
59
103
  *
@@ -63,6 +107,6 @@ export declare class App {
63
107
  * @param options [Options](https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch#options) to apply to the request
64
108
  * @returns Promise that resolves to the [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) to the request
65
109
  */
66
- readonly fetch: (resource: RequestInfo | URL, options?: RequestInit) => Promise<Response>;
110
+ readonly fetch: (resource: RequestInfo | URL, options?: App.Fetch) => Promise<Response>;
67
111
  }
68
112
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/app/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAE1C,OAAO,EAAkB,KAAK,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAE7D,yBAAiB,GAAG,CAAC;IACpB,UAAiB,OAAO,CAAC;QACxB,gCAAgC;QAChC,KAAY,aAAa,GAAG,QAAQ,GAAG,OAAO,GAAG,QAAQ,CAAC;KAC1D;IAED,4BAA4B;IAC5B,KAAY,OAAO,GAAG;QACrB;;;;;;;;;;;;WAYG;QACH,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC;QAExB;;;;;;;;WAQG;QACH,QAAQ,CAAC,aAAa,CAAC,EAAE,OAAO,CAAC,aAAa,CAAC;QAE/C;;;WAGG;QACH,QAAQ,CAAC,IAAI,CAAC,EAAE,SAAS,CAAC,OAAO,CAAC;KAClC,CAAC;CACF;AAED,6BAA6B;AAC7B,qBAAa,GAAG;;IAUf;;;;OAIG;IACH,YAAY,OAAO,CAAC,EAAE,GAAG,CAAC,OAAO,EAQhC;IAED;;;OAGG;IACH,GAAG,CACF,GAAG,MAAM,EAAE,IAAI,CAAC,SAAS,CACxB,KAAK,GAAG,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,GAAG,UAAU,CAAC,CACvD,EAAE,QAeH;IAED;;;;;;;;OAQG;IACH,QAAQ,CAAC,KAAK,aACH,WAAW,GAAG,GAAG,YACjB,WAAW,uBAqBpB;CAkCF"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/app/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAGN,KAAK,SAAS,EAGd,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,KAAK,EAAU,MAAM,mBAAmB,CAAC;AAElD,OAAO,EAAwB,KAAK,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAEnE,4EAA4E;AAC5E,eAAO,MAAM,KAAK,eAAkB,CAAC;AAErC,kFAAkF;AAClF,eAAO,MAAM,MAAM,eAAmB,CAAC;AAEvC,yFAAyF;AACzF,eAAO,MAAM,WAAW,uCAAuB,CAAC;AAEhD;;;GAGG;AACH,eAAO,MAAM,MAAM,uCAAuB,CAAC;AAE3C,yBAAiB,GAAG,CAAC;IACpB,4DAA4D;IAC5D,UAAiB,KAAK;QACrB,0DAA0D;QAC1D,QAAQ,CAAC,MAAM,EAAE,SAAS,KAAK,EAAE,CAAC;QAElC,gCAAgC;QAChC,QAAQ,CAAC,KAAK,EAAE,GAAG,CAAC,OAAO,CAAC,CAAC;QAE7B,wEAAwE;QACxE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;QAExB;;;;;WAKG;QACH,IAAI,CACH,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,GACd;YAAE,KAAK,EAAE,KAAK,CAAC;YAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;SAAE,GAAG,SAAS,CAAC;KAChE;IAED,gEAAgE;IAChE,UAAiB,OAAO;QACvB;;;WAGG;QACH,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,KAAK,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,GAAG,UAAU,CAAC,CAAC;KACxD;IAED,8DAA8D;IAC9D,UAAiB,KAAM,SAAQ,WAAW;QACzC,gGAAgG;QAChG,QAAQ,CAAC,EAAE,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;KACjC;IACD,UAAiB,OAAO,CAAC;QACxB,gCAAgC;QAChC,KAAY,aAAa,GAAG,QAAQ,GAAG,OAAO,GAAG,QAAQ,CAAC;KAC1D;IAED,4BAA4B;IAC5B,KAAY,OAAO,GAAG;QACrB;;;;;;;;;;;;;WAaG;QACH,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC;QAExB;;;;;;;;WAQG;QACH,QAAQ,CAAC,aAAa,CAAC,EAAE,OAAO,CAAC,aAAa,CAAC;QAE/C;;;WAGG;QACH,QAAQ,CAAC,SAAS,CAAC,EAAE,SAAS,CAAC,OAAO,CAAC;KACvC,CAAC;CACF;AAED,6BAA6B;AAC7B,qBAAa,GAAG;;IAgBf;;;;OAIG;IACH,YAAY,OAAO,CAAC,EAAE,GAAG,CAAC,OAAO,EAQhC;IAED;;;OAGG;IACH,GAAG,CACF,GAAG,MAAM,EAAE,IAAI,CAAC,SAAS,CACxB,KAAK,GAAG,UAAU,GAAG,GAAG,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,GAAG,UAAU,CAAC,CACrE,EAAE,QAsCH;IAED;;;;;;;;OAQG;IACH,QAAQ,CAAC,KAAK,aAAoB,WAAW,GAAG,GAAG,YAAY,GAAG,CAAC,KAAK,uBAuCtE;CAsHF"}
package/dist/app/index.js CHANGED
@@ -1,13 +1,29 @@
1
1
  import { Context } from "../context/index.js";
2
- import { Route } from "../route/index.js";
2
+ import { Limit, Malformed, Unsupported, memory, } from "../multipart/index.js";
3
+ import { Route, freeze } from "../route/index.js";
3
4
  import { Trie } from "../trie/index.js";
4
- import { Header, Method } from "../util/index.js";
5
+ import { Header, Method, Mime } from "../util/index.js";
6
+ /** Internal protocol for services that `app.use` mounts, such as `Auth`. */
7
+ export const mount = Symbol("mount");
8
+ /** Asks an authorization service for the routes agents sign in with, see `Mcp` */
9
+ export const agents = Symbol("agents");
10
+ /** Routes that accept cross-origin requests without cookies, such as OAuth endpoints. */
11
+ export const crossOrigin = new WeakSet();
12
+ /**
13
+ * Routes that run without the app's global middleware, such as an MCP
14
+ * endpoint, which makes its own request to the app for each call.
15
+ */
16
+ export const direct = new WeakSet();
5
17
  /** Web server application */
6
18
  export class App {
7
19
  /** Route trie */
8
20
  #trie = new Trie();
21
+ /** Every route added, in order */
22
+ #routes = [];
9
23
  /** Global middleware */
10
24
  #global = [];
25
+ /** Routes by tool name, agents call each by its own */
26
+ #tools = new Map();
11
27
  /** App options */
12
28
  #options;
13
29
  /**
@@ -20,17 +36,35 @@ export class App {
20
36
  if (this.#options.csrf)
21
37
  this.#global.push(App.#csrf);
22
38
  if (this.#options.trailingSlash !== "ignore") {
23
- this.#global.push(App.#createTrailingSlash(this.#options.trailingSlash));
39
+ this.#global.push(this.#createTrailingSlash(this.#options.trailingSlash));
24
40
  }
25
41
  }
26
42
  /**
27
- * @param routes Route or middleware to use
43
+ * @param routes Routes, middleware, or services such as `Auth` to use
28
44
  * @returns `App` instance
29
45
  */
30
46
  use(...routes) {
31
47
  for (const route of routes) {
32
48
  if (route instanceof Route) {
49
+ // final from here, so its tool name and middleware stay as checked
50
+ route[freeze]();
51
+ const { tool } = route;
52
+ if (tool) {
53
+ const other = this.#tools.get(tool.name);
54
+ if (other && other !== route)
55
+ throw new TypeError(`Tool name "${tool.name}" is used by ${other.method} ${other.pattern} and ${route.method} ${route.pattern}, change one of their patterns.`);
56
+ this.#tools.set(tool.name, route);
57
+ }
33
58
  this.#trie.add(route);
59
+ this.#routes.push(route);
60
+ }
61
+ else if (mount in route) {
62
+ this.use(route[mount]({
63
+ routes: this.#routes,
64
+ fetch: this.fetch,
65
+ memory: this.#options.multipart?.memory ?? memory,
66
+ find: (method, pathname) => this.#find(method, pathname) ?? undefined,
67
+ }));
34
68
  }
35
69
  else if (Array.isArray(route)) {
36
70
  this.use(...route);
@@ -54,41 +88,142 @@ export class App {
54
88
  * @returns Promise that resolves to the [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) to the request
55
89
  */
56
90
  fetch = async (resource, options) => {
57
- const c = new Context(resource instanceof Request && !options
91
+ let init;
92
+ if (options)
93
+ for (const key in options) {
94
+ if (key === "ip")
95
+ continue;
96
+ const { ip, ...rest } = options;
97
+ init = rest;
98
+ break;
99
+ }
100
+ const c = new Context(resource instanceof Request && !init
58
101
  ? resource
59
- : new Request(resource, options), this.#options);
60
- return Context.compose(Object.assign(c,
61
- // match
62
- this.#trie.find(
63
- // use GET for HEAD requests to find the correct route
64
- (c.req.method === Method.head ? Method.get : c.req.method) +
65
- c.url.pathname)), this.#global.concat(c.route?.middleware ?? []));
102
+ : new Request(resource, init), this.#options, options?.ip);
103
+ try {
104
+ return await Context.compose(Object.assign(c, this.#find(c.req.method, c.url.pathname)),
105
+ // a route's body errors are answered before global middleware
106
+ // sees them, so middleware that catches errors gets only the app's
107
+ c.route
108
+ ? direct.has(c.route)
109
+ ? c.route.middleware
110
+ : [...this.#global, App.#body, ...c.route.middleware]
111
+ : this.#global);
112
+ }
113
+ catch (error) {
114
+ // from global middleware that read the body itself
115
+ const answer = App.#mistake(error);
116
+ if (!answer)
117
+ throw error;
118
+ // with the headers middleware set before it, such as for CORS
119
+ const headers = new Headers(c.res.headers);
120
+ headers.set(Header.name.type, Header.utf8(Mime.type.text));
121
+ return new Response(answer[1], { status: answer[0], headers });
122
+ }
66
123
  };
124
+ /**
125
+ * @param method Request method, GET finds the route for HEAD
126
+ * @param pathname URL pathname
127
+ * @returns The matched route and its decoded parameters
128
+ */
129
+ #find(method, pathname) {
130
+ const match = this.#trie.find((method === Method.head ? Method.get : method) + pathname);
131
+ if (match)
132
+ for (const name in match.params)
133
+ match.params[name] = App.#decode(match.params[name], name === "*");
134
+ return match;
135
+ }
136
+ /**
137
+ * @param error Error from reading a request body
138
+ * @returns The status and message for a body the client got wrong,
139
+ * `undefined` for any other error, which is the app's
140
+ */
141
+ static #mistake(error) {
142
+ if (error instanceof Limit)
143
+ return [413, error.message];
144
+ if (error instanceof Unsupported)
145
+ return [415, "Unsupported Media Type: send multipart/form-data"];
146
+ if (error instanceof Malformed)
147
+ return [400, `Bad Request: ${error.message}`];
148
+ }
149
+ /** Answers a body the client got wrong, where the route read it */
150
+ static async #body(c, next) {
151
+ try {
152
+ await next();
153
+ }
154
+ catch (error) {
155
+ const answer = App.#mistake(error);
156
+ if (!answer)
157
+ throw error;
158
+ c.text(answer[1], answer[0]);
159
+ }
160
+ }
161
+ /**
162
+ * @param value Matched parameter, as the URL encodes it
163
+ * @param wildcard The value is a wildcard's path
164
+ * @returns The decoded value, or the value as sent when it is malformed
165
+ */
166
+ static #decode(value, wildcard) {
167
+ // a wildcard is relative to its route, so `/files//etc/passwd` cannot
168
+ // make it an absolute path
169
+ if (wildcard)
170
+ value = value.replace(/^\/+/, "");
171
+ if (!value.includes("%"))
172
+ return value;
173
+ try {
174
+ if (!wildcard)
175
+ return decodeURIComponent(value);
176
+ // a wildcard is a path, so encoded separators stay encoded and a
177
+ // request cannot add segments to it, such as `..%2Fsecret`
178
+ return value
179
+ .split(/(%2F|%5C)/i)
180
+ .map((part, i) => (i % 2 ? part : decodeURIComponent(part)))
181
+ .join("");
182
+ }
183
+ catch {
184
+ return value;
185
+ }
186
+ }
67
187
  /** Basic CSRF middleware */
68
188
  static #csrf(c, next) {
69
- if (c.req.method === Method.get ||
70
- c.req.method === Method.head ||
71
- c.req.headers.get(Header.name.fetch) === "same-origin" ||
72
- c.req.headers.get(Header.name.origin) === c.url.origin) {
189
+ const { headers, method } = c.req;
190
+ const site = headers.get(Header.name.fetch);
191
+ const origin = headers.get(Header.name.origin);
192
+ if (method === Method.get ||
193
+ method === Method.head ||
194
+ site === "same-origin" ||
195
+ origin === c.url.origin ||
196
+ (c.route && crossOrigin.has(c.route)) ||
197
+ // browsers send at least one, so without cookies this is not a browser,
198
+ // for example an agent submitting a form
199
+ (site === null && origin === null && !headers.has(Header.name.cookie))) {
73
200
  return next();
74
201
  }
75
202
  c.text("Forbidden", 403);
76
203
  }
77
- /** @returns Trailing slash middleware */
78
- static #createTrailingSlash(mode) {
204
+ /**
205
+ * Redirect a request no route matched to the other spelling of its path,
206
+ * with or without the trailing slash, when a route matches that.
207
+ *
208
+ * @returns Trailing slash middleware
209
+ */
210
+ #createTrailingSlash(mode) {
79
211
  return async (c, next) => {
80
212
  await next();
81
- if (c.res.status && c.res.status !== 404)
213
+ // a route answered, its handler has already run
214
+ if (c.route || (c.res.status && c.res.status !== 404))
82
215
  return;
83
- const slash = c.url.pathname.endsWith("/");
84
- if (mode === "always" && !slash) {
85
- c.url.pathname += "/";
86
- c.redirect(c.url, 308);
87
- }
88
- else if (mode === "never" && c.url.pathname !== "/" && slash) {
89
- c.url.pathname = c.url.pathname.slice(0, -1);
90
- c.redirect(c.url, 308);
91
- }
216
+ const { pathname } = c.url;
217
+ const slash = pathname.endsWith("/");
218
+ const other = mode === "always" && !slash
219
+ ? pathname + "/"
220
+ : mode === "never" && pathname !== "/" && slash
221
+ ? pathname.slice(0, -1)
222
+ : undefined;
223
+ if (!other || !this.#find(c.req.method, other))
224
+ return;
225
+ c.url.pathname = other;
226
+ c.redirect(c.url, 308);
92
227
  };
93
228
  }
94
229
  }
@@ -0,0 +1,26 @@
1
+ export declare namespace AuthData {
2
+ /** Parsed authenticator data and credential state. */
3
+ interface Data {
4
+ rpIdHash: Uint8Array;
5
+ flags: number;
6
+ counter: number;
7
+ attestedCredentialData: AttestedCredentialData | null;
8
+ }
9
+ /** Attested ES256 credential. */
10
+ interface AttestedCredentialData {
11
+ aaguid: Uint8Array;
12
+ credentialId: Uint8Array;
13
+ publicKey: Map<number, number | Uint8Array>;
14
+ }
15
+ }
16
+ /** Bounded parser for WebAuthn authenticator data. */
17
+ export declare class AuthData {
18
+ /** Parse the fixed header, optional attested credential, and optional extensions. */
19
+ static parse(data: Uint8Array): {
20
+ rpIdHash: Uint8Array<ArrayBufferLike>;
21
+ flags: number;
22
+ counter: number;
23
+ attestedCredentialData: AuthData.AttestedCredentialData | null;
24
+ };
25
+ }
26
+ //# sourceMappingURL=auth-data.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth-data.d.ts","sourceRoot":"","sources":["../../src/auth/auth-data.ts"],"names":[],"mappings":"AAGA,yBAAiB,QAAQ,CAAC;IACzB,sDAAsD;IACtD,UAAiB,IAAI;QACpB,QAAQ,EAAE,UAAU,CAAC;QACrB,KAAK,EAAE,MAAM,CAAC;QACd,OAAO,EAAE,MAAM,CAAC;QAChB,sBAAsB,EAAE,sBAAsB,GAAG,IAAI,CAAC;KACtD;IACD,iCAAiC;IACjC,UAAiB,sBAAsB;QACtC,MAAM,EAAE,UAAU,CAAC;QACnB,YAAY,EAAE,UAAU,CAAC;QACzB,SAAS,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,CAAC,CAAC;KAC5C;CACD;AAED,sDAAsD;AACtD,qBAAa,QAAQ;IACpB,qFAAqF;IACrF,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,UAAU;;;;;MAgC5B;CACD"}
@@ -0,0 +1,42 @@
1
+ import { CBOR } from "./cbor.js";
2
+ import { Issue } from "./issue.js";
3
+ /** Bounded parser for WebAuthn authenticator data. */
4
+ export class AuthData {
5
+ /** Parse the fixed header, optional attested credential, and optional extensions. */
6
+ static parse(data) {
7
+ if (data.length < 37)
8
+ throw new Issue("credential");
9
+ const view = new DataView(data.buffer, data.byteOffset, data.length);
10
+ const flags = data[32];
11
+ let end = 37;
12
+ let attestedCredentialData = null;
13
+ if (flags & 64) {
14
+ if (data.length < 55)
15
+ throw new Issue("credential");
16
+ const length = view.getUint16(53);
17
+ end = 55 + length;
18
+ if (!length || length > 1023 || end >= data.length)
19
+ throw new Issue("credential");
20
+ const cbor = new CBOR(data.subarray(end));
21
+ attestedCredentialData = {
22
+ aaguid: data.subarray(37, 53),
23
+ credentialId: data.subarray(55, end),
24
+ publicKey: cbor.decodeCOSEKey(),
25
+ };
26
+ end += cbor.offset;
27
+ }
28
+ if (flags & 128) {
29
+ const cbor = new CBOR(data.subarray(end));
30
+ cbor.decodeExtensions();
31
+ end += cbor.offset;
32
+ }
33
+ if (end !== data.length)
34
+ throw new Issue("authenticator data length");
35
+ return {
36
+ rpIdHash: data.subarray(0, 32),
37
+ flags,
38
+ counter: view.getUint32(33),
39
+ attestedCredentialData,
40
+ };
41
+ }
42
+ }
@@ -0,0 +1,50 @@
1
+ /** COSE (CBOR Object Signing and Encryption) key format utilities */
2
+ export declare class COSE {
3
+ #private;
4
+ /**
5
+ * Convert COSE public key to SPKI format.
6
+ *
7
+ * Only EC2 keys with P-256 curve (ES256, alg -7) are supported. This is the standard modern algorithm
8
+ * supported by virtually all platform authenticators (https://www.rfc-editor.org/rfc/rfc8152#section-8.1).
9
+ *
10
+ * @param cose - COSE key map with type, curve, and coordinates
11
+ * @returns SPKI encoded public key as bytes
12
+ * @throws Issue if key type, curve, or coordinates are invalid
13
+ */
14
+ static toSPKI(cose: Map<number, number | Uint8Array>): Uint8Array<ArrayBuffer>;
15
+ }
16
+ export declare namespace CBOR {
17
+ type Key = number | string;
18
+ type Value = number | string | boolean | null | Uint8Array | Map<Key, Value> | Value[];
19
+ }
20
+ /** CBOR (Concise Binary Object Representation) decoder for WebAuthn data */
21
+ export declare class CBOR {
22
+ #private;
23
+ /**
24
+ * Create a new CBOR instance
25
+ *
26
+ * @param data WebAuthn data
27
+ */
28
+ constructor(data: Uint8Array);
29
+ /**
30
+ * Decode attestation object and extract authenticator data.
31
+ *
32
+ * Only supports fmt === "none", other formats may throw earlier depending on CBOR contents
33
+ *
34
+ * @returns Authenticator data bytes
35
+ * @throws Issue if CBOR structure is invalid
36
+ */
37
+ decodeAttestation(): Uint8Array<ArrayBufferLike>;
38
+ /**
39
+ * Decode COSE key from CBOR format.
40
+ *
41
+ * @returns COSE key map with numeric labels
42
+ * @throws Issue if CBOR structure is invalid
43
+ */
44
+ decodeCOSEKey(): Map<number, number | Uint8Array<ArrayBufferLike>>;
45
+ /** Bytes consumed by the last decoded value. */
46
+ get offset(): number;
47
+ /** Decode authenticator extensions as a CBOR map. */
48
+ decodeExtensions(): Map<CBOR.Key, CBOR.Value>;
49
+ }
50
+ //# sourceMappingURL=cbor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cbor.d.ts","sourceRoot":"","sources":["../../src/auth/cbor.ts"],"names":[],"mappings":"AAGA,qEAAqE;AACrE,qBAAa,IAAI;;IAQhB;;;;;;;;;OASG;IACH,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,CAAC,2BAiBnD;CACD;AAED,yBAAiB,IAAI,CAAC;IACrB,KAAY,GAAG,GAAG,MAAM,GAAG,MAAM,CAAC;IAClC,KAAY,KAAK,GAChB,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,UAAU,GAAG,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,GAAG,KAAK,EAAE,CAAC;CAC3E;AAED,4EAA4E;AAC5E,qBAAa,IAAI;;IAOhB;;;;OAIG;IACH,YAAY,IAAI,EAAE,UAAU,EAG3B;IAED;;;;;;;OAOG;IACH,iBAAiB,gCAmBhB;IAED;;;;;OAKG;IACH,aAAa,sDAsBZ;IAED,gDAAgD;IAChD,IAAI,MAAM,WAET;IAED,qDAAqD;IACrD,gBAAgB,8BAQf;CA2FD"}