burger-api 0.9.7 → 1.0.0-beta.1

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 (175) hide show
  1. package/README.md +84 -241
  2. package/dist/src/adapter/bun/index.d.ts +14 -0
  3. package/dist/src/adapter/bun/index.js +73 -0
  4. package/dist/src/adapter/bun/types.d.ts +15 -0
  5. package/dist/src/adapter/bun/types.js +1 -0
  6. package/dist/src/adapter/index.d.ts +1 -0
  7. package/dist/src/adapter/index.js +1 -0
  8. package/dist/src/adapter/types.d.ts +49 -0
  9. package/dist/src/adapter/types.js +1 -0
  10. package/dist/src/adapter/web-standard/fetch-handler.d.ts +39 -0
  11. package/dist/src/adapter/web-standard/fetch-handler.js +54 -0
  12. package/dist/src/adapter/web-standard/index.d.ts +8 -0
  13. package/dist/src/adapter/web-standard/index.js +7 -0
  14. package/dist/src/analysis/route-access-analyzer.d.ts +10 -0
  15. package/dist/src/analysis/route-access-analyzer.js +287 -0
  16. package/dist/src/chain/chain.d.ts +16 -0
  17. package/dist/src/chain/chain.js +17 -0
  18. package/dist/src/chain/flattener.d.ts +3 -0
  19. package/dist/src/chain/flattener.js +78 -0
  20. package/dist/src/chain/node.d.ts +25 -0
  21. package/dist/src/chain/node.js +1 -0
  22. package/dist/src/compiler/conventions.d.ts +67 -0
  23. package/dist/src/compiler/conventions.js +129 -0
  24. package/dist/src/compiler/index.d.ts +13 -0
  25. package/dist/src/compiler/index.js +11 -0
  26. package/dist/src/compiler/module-loader.d.ts +75 -0
  27. package/dist/src/compiler/module-loader.js +276 -0
  28. package/dist/src/compiler/route-module.d.ts +97 -0
  29. package/dist/src/compiler/route-module.js +1 -0
  30. package/dist/src/compiler/route-tree.d.ts +19 -0
  31. package/dist/src/compiler/route-tree.js +28 -0
  32. package/dist/src/compiler/scanner.d.ts +42 -0
  33. package/dist/src/compiler/scanner.js +240 -0
  34. package/dist/src/context/context-set.d.ts +26 -0
  35. package/dist/src/context/context-set.js +45 -0
  36. package/dist/src/context/context.d.ts +357 -0
  37. package/dist/src/context/context.js +401 -0
  38. package/dist/src/context/query-parser.d.ts +23 -0
  39. package/dist/src/context/query-parser.js +132 -0
  40. package/dist/src/context/route-access.d.ts +7 -0
  41. package/dist/src/context/route-access.js +18 -0
  42. package/dist/src/context/types.d.ts +62 -0
  43. package/dist/src/context/types.js +5 -0
  44. package/dist/src/core/asset-mime.d.ts +9 -0
  45. package/dist/src/core/asset-mime.js +35 -0
  46. package/dist/src/core/assets.d.ts +30 -0
  47. package/dist/src/core/assets.js +67 -0
  48. package/dist/src/core/docs-providers.d.ts +13 -0
  49. package/dist/src/core/docs-providers.js +89 -0
  50. package/dist/src/core/embedded-assets.d.ts +18 -0
  51. package/dist/src/core/embedded-assets.js +13 -0
  52. package/dist/src/core/openapi.d.ts +9 -10
  53. package/dist/src/core/openapi.js +333 -127
  54. package/dist/src/core/page-router.d.ts +13 -17
  55. package/dist/src/core/page-router.js +86 -55
  56. package/dist/src/core/server.d.ts +20 -11
  57. package/dist/src/core/server.js +41 -45
  58. package/dist/src/errors/forbidden.d.ts +14 -0
  59. package/dist/src/errors/forbidden.js +16 -0
  60. package/dist/src/errors/http-error.d.ts +49 -0
  61. package/dist/src/errors/http-error.js +155 -0
  62. package/dist/src/errors/method-not-allowed.d.ts +13 -0
  63. package/dist/src/errors/method-not-allowed.js +15 -0
  64. package/dist/src/errors/not-found.d.ts +14 -0
  65. package/dist/src/errors/not-found.js +16 -0
  66. package/dist/src/errors/unauthorized.d.ts +14 -0
  67. package/dist/src/errors/unauthorized.js +16 -0
  68. package/dist/src/index.d.ts +275 -50
  69. package/dist/src/index.js +974 -333
  70. package/dist/src/lifecycle/executor.d.ts +64 -0
  71. package/dist/src/lifecycle/executor.js +201 -0
  72. package/dist/src/lifecycle/hook-runner.d.ts +11 -0
  73. package/dist/src/lifecycle/hook-runner.js +156 -0
  74. package/dist/src/lifecycle/jit.d.ts +50 -0
  75. package/dist/src/lifecycle/jit.js +258 -0
  76. package/dist/src/lifecycle/transform.d.ts +26 -0
  77. package/dist/src/lifecycle/transform.js +99 -0
  78. package/dist/src/lifecycle/types.d.ts +135 -0
  79. package/dist/src/lifecycle/types.js +1 -0
  80. package/dist/src/plugin/composer.d.ts +3 -0
  81. package/dist/src/plugin/composer.js +28 -0
  82. package/dist/src/plugin/registry.d.ts +25 -0
  83. package/dist/src/plugin/registry.js +107 -0
  84. package/dist/src/plugin/types.d.ts +20 -0
  85. package/dist/src/plugin/types.js +1 -0
  86. package/dist/src/router/allow-cache.d.ts +20 -0
  87. package/dist/src/router/allow-cache.js +26 -0
  88. package/dist/src/router/compiler.d.ts +39 -0
  89. package/dist/src/router/compiler.js +867 -0
  90. package/dist/src/router/define.d.ts +67 -0
  91. package/dist/src/router/define.js +34 -0
  92. package/dist/src/router/index.d.ts +6 -0
  93. package/dist/src/router/index.js +5 -0
  94. package/dist/src/router/param-extract.d.ts +33 -0
  95. package/dist/src/router/param-extract.js +87 -0
  96. package/dist/src/router/regex-matcher.d.ts +42 -0
  97. package/dist/src/router/regex-matcher.js +216 -0
  98. package/dist/src/router/router.d.ts +214 -0
  99. package/dist/src/router/router.js +670 -0
  100. package/dist/src/router/static-map.d.ts +39 -0
  101. package/dist/src/router/static-map.js +48 -0
  102. package/dist/src/router/trie.d.ts +69 -0
  103. package/dist/src/router/trie.js +572 -0
  104. package/dist/src/router/types.d.ts +104 -0
  105. package/dist/src/router/types.js +1 -0
  106. package/dist/src/runtime/capabilities.d.ts +29 -0
  107. package/dist/src/runtime/capabilities.js +48 -0
  108. package/dist/src/types/index.d.ts +260 -109
  109. package/dist/src/types/index.js +1 -1
  110. package/dist/src/types/inference.d.ts +78 -0
  111. package/dist/src/types/inference.js +20 -0
  112. package/dist/src/types/openapi-config.d.ts +164 -0
  113. package/dist/src/types/openapi-config.js +8 -0
  114. package/dist/src/utils/env.d.ts +13 -0
  115. package/dist/src/utils/env.js +24 -0
  116. package/dist/src/utils/fs.d.ts +29 -0
  117. package/dist/src/utils/fs.js +75 -0
  118. package/dist/src/utils/index.d.ts +1 -1
  119. package/dist/src/utils/index.js +2 -2
  120. package/dist/src/utils/pathConversion.js +13 -7
  121. package/dist/src/utils/response.d.ts +42 -6
  122. package/dist/src/utils/response.js +189 -7
  123. package/dist/src/utils/routing.d.ts +32 -13
  124. package/dist/src/utils/routing.js +41 -18
  125. package/dist/src/utils/thenable.d.ts +8 -0
  126. package/dist/src/utils/thenable.js +12 -0
  127. package/dist/src/utils/timing-safe.d.ts +9 -0
  128. package/dist/src/utils/timing-safe.js +20 -0
  129. package/dist/src/utils/wildcard.d.ts +6 -7
  130. package/dist/src/utils/wildcard.js +38 -55
  131. package/dist/src/validation/adapter.d.ts +54 -0
  132. package/dist/src/validation/adapter.js +75 -0
  133. package/dist/src/validation/adapters/standard.d.ts +11 -0
  134. package/dist/src/validation/adapters/standard.js +110 -0
  135. package/dist/src/validation/adapters/zod.d.ts +9 -0
  136. package/dist/src/validation/adapters/zod.js +128 -0
  137. package/dist/src/validation/cache.d.ts +26 -0
  138. package/dist/src/validation/cache.js +35 -0
  139. package/dist/src/validation/coerce.d.ts +28 -0
  140. package/dist/src/validation/coerce.js +176 -0
  141. package/dist/src/validation/compiler.d.ts +27 -0
  142. package/dist/src/validation/compiler.js +191 -0
  143. package/dist/src/validation/error.d.ts +69 -0
  144. package/dist/src/validation/error.js +137 -0
  145. package/dist/src/validation/index.d.ts +16 -0
  146. package/dist/src/validation/index.js +12 -0
  147. package/dist/src/validation/response.d.ts +25 -0
  148. package/dist/src/validation/response.js +85 -0
  149. package/dist/src/validation/types.d.ts +148 -0
  150. package/dist/src/validation/types.js +6 -0
  151. package/dist/src/validation/validator.d.ts +39 -0
  152. package/dist/src/validation/validator.js +441 -0
  153. package/dist/src/ws/adapter.d.ts +230 -0
  154. package/dist/src/ws/adapter.js +660 -0
  155. package/dist/src/ws/compiler.d.ts +37 -0
  156. package/dist/src/ws/compiler.js +178 -0
  157. package/dist/src/ws/index.d.ts +11 -0
  158. package/dist/src/ws/index.js +8 -0
  159. package/dist/src/ws/platform.d.ts +82 -0
  160. package/dist/src/ws/platform.js +122 -0
  161. package/dist/src/ws/router.d.ts +47 -0
  162. package/dist/src/ws/router.js +183 -0
  163. package/dist/src/ws/scanner.d.ts +97 -0
  164. package/dist/src/ws/scanner.js +253 -0
  165. package/dist/src/ws/types.d.ts +381 -0
  166. package/dist/src/ws/types.js +213 -0
  167. package/package.json +72 -60
  168. package/dist/src/core/api-router.d.ts +0 -65
  169. package/dist/src/core/api-router.js +0 -264
  170. package/dist/src/core/swagger-ui.d.ts +0 -1
  171. package/dist/src/core/swagger-ui.js +0 -33
  172. package/dist/src/middleware/validator.d.ts +0 -14
  173. package/dist/src/middleware/validator.js +0 -133
  174. package/dist/src/utils/error.d.ts +0 -17
  175. package/dist/src/utils/error.js +0 -63
@@ -0,0 +1,214 @@
1
+ import type { CompiledHandler, CompiledRoute, NativeMethodHandlers, RouterConfig } from './types.js';
2
+ import type { ResolvedPlugin } from '../plugin/types.js';
3
+ import type { Hook } from '../lifecycle/types.js';
4
+ import type { BurgerEnv, BurgerExecutionContext, BurgerServices, RequestIPHolder } from '../context/context.js';
5
+ import { BurgerContext } from '../context/context.js';
6
+ /**
7
+ * Public router: owns the compiled dispatch state and orchestrates lookup
8
+ * and execution.
9
+ *
10
+ * Static, dynamic (`:param`), and wildcard (`*`) routes are all registered
11
+ * on Bun's native `routes` map; compiled handlers self-extract `params` /
12
+ * `wildcardParams`. The `fetch` fallback still runs for unmatched and
13
+ * trailing-slash requests (`/foo/` ≡ `/foo`), consulting the internal trie.
14
+ *
15
+ * Both paths execute the same compiled handler, so method dispatch,
16
+ * 405+Allow, auto-HEAD, and lifecycle behavior are identical. Non-Bun
17
+ * (WinterCG) adapters dispatch every route through `fetch` + trie.
18
+ */
19
+ export declare class Router {
20
+ private staticMap;
21
+ private trie;
22
+ private allowCache;
23
+ private compiler;
24
+ /** Dev mode: controls error-rendering detail (stack/cause in problem bodies). */
25
+ private debug;
26
+ /** Dynamic-dispatch engine preference ('auto' default). */
27
+ private engine?;
28
+ /** Native dispatch table for `:param` / `*` routes (Bun `routes` map keys). */
29
+ private nativeRoutesMap;
30
+ /** Per-path per-method specialized executors (fed to Bun method objects). */
31
+ private methodCores;
32
+ /** Retained compiled-route metadata (RouteAccessInfo + RouteMeta). */
33
+ private compiledRoutes?;
34
+ /** Memoized `staticRoutes()` result; rebuilt on `compile()`. */
35
+ private cachedStaticRoutes?;
36
+ /** Memoized `nativeRoutes()` result; rebuilt on `compile()`. */
37
+ private cachedNativeRoutes?;
38
+ /** Pre-routing hooks (Plugin + Global scope). Run before routing in `fetch()`. */
39
+ private onRequestHooks;
40
+ /** True when pre-routing `onRequest` hooks exist (hot-path flag). */
41
+ private hasOnRequest;
42
+ /**
43
+ * Error hooks available before a route is matched (Global → Plugin →
44
+ * Framework, nearest-first). Let pre-routing `onRequest` throws go
45
+ * through the same `onError` pipeline as request errors.
46
+ */
47
+ private onErrorHooks;
48
+ /**
49
+ * Global/plugin response hooks (afterRoute + mapResponse) for responses
50
+ * with no matched route plan: 404/405/auto-OPTIONS, pages/assets/docs,
51
+ * and onError-rendered errors. Compiled once at startup.
52
+ */
53
+ private globalResponsePlan;
54
+ /** Hot-path flag: true when the global response plan has hooks. */
55
+ private globalResponseActive;
56
+ /** App services resolved ONCE; shared (frozen) by every request context. */
57
+ private appServices;
58
+ /** Per-app server reference for lazy `ctx.ip` (written once at startup). */
59
+ private ipHolder;
60
+ /**
61
+ * RegExp matcher for dynamic/wildcard routes (WinterCG fast path).
62
+ * Built when the engine setting allows and compilation succeeds;
63
+ * `null` means dispatch stays on the trie.
64
+ */
65
+ private regexMatcher;
66
+ constructor(config?: RouterConfig);
67
+ /**
68
+ * Compiles a `RouteDefinition[]` into the dispatch structures.
69
+ * Replaces all tables wholesale (no incremental merge) so hot reload
70
+ * cannot leak stale routes.
71
+ */
72
+ compile(defs: import('../types/index.js').RouteDefinition[], plugins?: ResolvedPlugin[], providers?: Map<string, unknown>, onRequestHooks?: Hook[], globalHooks?: import('../lifecycle/types.js').RouteHooks): void;
73
+ /**
74
+ * Records the serving runtime's handle (Bun's `Server`) once so `ctx.ip`
75
+ * can resolve the socket peer lazily — no per-request WeakMap write.
76
+ * Adapter-facing; called at startup by the Bun serve path.
77
+ */
78
+ setRequestIPSource(server: unknown): void;
79
+ /**
80
+ * The per-app `ctx.ip` holder. Non-API contexts (dynamic pages) receive it
81
+ * so their `ctx.ip` resolves exactly like API routes.
82
+ */
83
+ getRequestIPHolder(): RequestIPHolder;
84
+ /**
85
+ * The app services resolved ONCE at compile time. Non-API contexts
86
+ * (dynamic pages) receive the same shared object so `ctx.services` is
87
+ * populated there too.
88
+ */
89
+ getAppServices(): BurgerServices;
90
+ /**
91
+ * Builds the RegExp dispatch matcher for dynamic/wildcard routes when
92
+ * the configured engine asks for it ('regex'). 'auto' stays on the trie,
93
+ * so the matcher is an explicit opt-in.
94
+ */
95
+ private buildMatcher;
96
+ /**
97
+ * Read-only access to the retained compiled-route metadata
98
+ * (`RouteAccessInfo` + `RouteMeta`). Populated by `compile()`; not used on
99
+ * the request hot path, so it has no runtime performance impact.
100
+ */
101
+ getCompiledRoutes(): Map<string, CompiledRoute> | undefined;
102
+ /**
103
+ * Returns the static routes as a `Bun.serve` `routes` map: one **method
104
+ * object** per path (`{ GET: fnGet, POST: fnPost, HEAD: fnHead, ... }`).
105
+ * Each method is specialized at compile time; a method the route does not
106
+ * declare answers 405 + Allow here, so Bun never falls through to a less
107
+ * specific route.
108
+ */
109
+ staticRoutes(): Record<string, NativeMethodHandlers>;
110
+ /**
111
+ * Same method-object shape for `:param` / `*` routes: Bun matches the
112
+ * pattern natively. Wildcard requests with a trailing slash are handed to
113
+ * the fetch dispatch (which strips the slash before matching); ordinary
114
+ * wildcard requests keep their native dispatch. Every HTTP method key is
115
+ * present, matching the fetch path's 405 + Allow instead of falling
116
+ * through.
117
+ */
118
+ nativeRoutes(): Record<string, NativeMethodHandlers>;
119
+ /**
120
+ * Exact static API paths as direct dispatchers, keyed by path.
121
+ * `fetchHandler()` merges these into its page/asset lookup so a static
122
+ * API request resolves in one `Map.get` instead of a page-map miss plus
123
+ * a `StaticMap` hit. Each dispatcher mirrors the `fetch` fallback for its
124
+ * own path: known method → specialized core, otherwise the compiled
125
+ * method-dispatch handler (405 + Allow), with global response hooks and
126
+ * the `onRequest` pipeline applied exactly as `fetchWithPath` does.
127
+ */
128
+ staticDispatchMap(): Map<string, CompiledHandler>;
129
+ private buildStaticDispatch;
130
+ /**
131
+ * Builds the Bun method object for one compiled path. Every HTTP method
132
+ * key is present: a declared method runs its compiled core (wrapped by
133
+ * onRequest when hooks exist), an undeclared one answers the same
134
+ * 405 + Allow as the fetch path.
135
+ */
136
+ private buildMethods;
137
+ /**
138
+ * Wraps a non-API handler registered on the native routes map (pages,
139
+ * static assets, `/openapi.json`, `/docs`) so global/plugin `onRequest`
140
+ * hooks (CORS, logging, auth, rate limiting) apply to it exactly like
141
+ * API routes. Also catches its errors for the global/plugin `onError`
142
+ * chain. Returned unchanged when no hooks exist.
143
+ */
144
+ wrapWithOnRequest<T extends (request: Request) => unknown>(handler: T): T;
145
+ /**
146
+ * Runs the pre-routing `onError` chain (plugin + global) for a response
147
+ * source that has no matched route plan (page, asset, docs), then the
148
+ * global response hooks and `ctx.set` — the non-route equivalent of a
149
+ * route's error exit.
150
+ */
151
+ renderNonRouteError(error: unknown, request: Request, prebuilt?: BurgerContext, env?: BurgerEnv, executionCtx?: BurgerExecutionContext): Promise<Response>;
152
+ /** Renders an error that escaped the pipeline; logs it when 5xx. */
153
+ private renderUnhandled;
154
+ /**
155
+ * Applies the global/plugin response plan and `ctx.set` to a response
156
+ * that did not run a route plan (404/405/OPTIONS, pages/assets/docs,
157
+ * onError renders). Returns the response unchanged — zero cost — when
158
+ * neither applies.
159
+ */
160
+ private finishNonRouteResponse;
161
+ /** Runs the global response plan, then `ctx.set`, for one response. */
162
+ private applyGlobalResponseHooks;
163
+ /**
164
+ * Compatibility alias for `staticRoutes()`.
165
+ */
166
+ get routes(): Record<string, NativeMethodHandlers>;
167
+ /**
168
+ * Executes pre-routing `onRequest` hooks on a minimal BurgerContext.
169
+ * Returns a Response if any hook short-circuits, or undefined to continue.
170
+ * Platform `env` / `executionCtx` are bound onto the context here so they
171
+ * survive into the dispatched handler via re-binding.
172
+ */
173
+ private runOnRequest;
174
+ /**
175
+ * Dynamic / wildcard lookup: the RegExp matcher first (when compiled),
176
+ * then the radix trie. Both produce identical match shapes. A match that
177
+ * binds a `:param` to an empty segment is rejected. Apps with no dynamic
178
+ * routes skip both matchers.
179
+ */
180
+ private matchDynamic;
181
+ /**
182
+ * Applies collected after-mappers in onion order (last registered runs
183
+ * first, the first hook's mapper wraps outermost) — identical to
184
+ * `runHooks` and the JIT for beforeRoute.
185
+ */
186
+ private applyMappers;
187
+ /**
188
+ * The `fetch` fallback handed to `Bun.serve`: dynamic/wildcard routes
189
+ * via the trie, plus loose-trailing-slash static variants Bun did not
190
+ * match directly.
191
+ *
192
+ * `env` / `executionCtx` are optional platform bindings (WinterCG
193
+ * `fetch(request, env, ctx)`). The signature is its own shape — NOT the
194
+ * server-oriented `FetchHandler` — so the platform slots stay unambiguous.
195
+ * Sync-first: a synchronously resolved executor returns a `Response`
196
+ * directly; only hook plans and async handlers produce a Promise.
197
+ */
198
+ fetch: (request: Request, env?: BurgerEnv, executionCtx?: BurgerExecutionContext) => Response | Promise<Response>;
199
+ /**
200
+ * `fetch` with a pathname the caller already extracted (e.g.
201
+ * `fetchHandler()` resolved page/asset routes first), avoiding a second
202
+ * URL parse per request. The pathname must come from
203
+ * `extractPathnameFromUrl`.
204
+ */
205
+ fetchWithPath: (request: Request, pathname: string, env?: BurgerEnv, executionCtx?: BurgerExecutionContext) => Response | Promise<Response>;
206
+ /** Pre-routing hook path: context + mappers wrap the matched dispatch. */
207
+ private dispatchWithOnRequest;
208
+ /**
209
+ * Routing + execution against the compiled tables. `prebuilt` is the
210
+ * pre-routing context (onRequest path) or `undefined`; `apply` wraps the
211
+ * response in collected onRequest mappers when any exist.
212
+ */
213
+ private dispatchMatched;
214
+ }