burger-api 0.9.7 → 1.0.0-beta

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 +201 -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 +47 -0
  61. package/dist/src/errors/http-error.js +146 -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 +263 -50
  69. package/dist/src/index.js +919 -332
  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 +817 -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 +201 -0
  99. package/dist/src/router/router.js +600 -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 +172 -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 +2 -9
  130. package/dist/src/utils/wildcard.js +20 -58
  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 +79 -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 +219 -0
  154. package/dist/src/ws/adapter.js +623 -0
  155. package/dist/src/ws/compiler.d.ts +37 -0
  156. package/dist/src/ws/compiler.js +156 -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 +81 -0
  160. package/dist/src/ws/platform.js +113 -0
  161. package/dist/src/ws/router.d.ts +48 -0
  162. package/dist/src/ws/router.js +174 -0
  163. package/dist/src/ws/scanner.d.ts +97 -0
  164. package/dist/src/ws/scanner.js +193 -0
  165. package/dist/src/ws/types.d.ts +380 -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,219 @@
1
+ /**
2
+ * Matches upgrade requests to routes and wires sockets to their handlers.
3
+ */
4
+ import type { WebSocketRouter } from './router.js';
5
+ import type { WebSocketConfig } from './types.js';
6
+ import { BurgerContext } from '../context/context.js';
7
+ import type { BurgerEnv, BurgerExecutionContext, RequestIPHolder } from '../context/context.js';
8
+ import type { Hook, TransformMap } from '../lifecycle/types.js';
9
+ import { type NodeWsBridge, type NodeWsBridgeOptions, type WsUpgradeOutcome } from './platform.js';
10
+ import { type RuntimeTarget } from '../runtime/capabilities.js';
11
+ /**
12
+ * WebSocket adapter options
13
+ */
14
+ export interface WebSocketAdapterOptions {
15
+ /**
16
+ * WebSocket router
17
+ */
18
+ router: WebSocketRouter;
19
+ /**
20
+ * Global WebSocket config
21
+ */
22
+ config?: WebSocketConfig;
23
+ /**
24
+ * Debug mode
25
+ */
26
+ debug?: boolean;
27
+ /**
28
+ * Application services (providers) to inject into WebSocket handlers
29
+ */
30
+ providers?: Map<string, unknown>;
31
+ /**
32
+ * Resolved plugin hooks for auth execution during upgrade
33
+ */
34
+ pluginTransform?: TransformMap;
35
+ /**
36
+ * Resolved plugin beforeRoute hooks for auth execution during upgrade
37
+ */
38
+ pluginBeforeRoute?: ((ctx: BurgerContext) => unknown | Promise<unknown>)[];
39
+ /**
40
+ * Resolved framework-scope beforeRoute hooks (run before plugin hooks).
41
+ */
42
+ frameworkBeforeRoute?: Hook[];
43
+ /**
44
+ * Pre-routing onRequest hooks, in the same Framework → Plugin → Global
45
+ * order the HTTP path uses. Always run on upgrade — auth must not fail open.
46
+ */
47
+ onRequestHooks?: Hook[];
48
+ /**
49
+ * App-level (`src/hooks.ts`) transform factories, applied after plugin ones.
50
+ */
51
+ globalTransform?: TransformMap;
52
+ /**
53
+ * App-level (`src/hooks.ts`) beforeRoute hooks — run after plugin hooks.
54
+ */
55
+ globalBeforeRoute?: Hook[];
56
+ /**
57
+ * Per-app `ctx.ip` holder, so upgrade hooks resolve the socket peer like
58
+ * HTTP hooks do.
59
+ */
60
+ ipHolder?: RequestIPHolder;
61
+ /**
62
+ * The deployment target declared by `burger-api build --target`, if any.
63
+ * When present, resolves the handoff via `RUNTIME_CAPABILITIES` instead of
64
+ * live probing (Node and Vercel look identical to `globalThis` detection).
65
+ */
66
+ runtimeTarget?: RuntimeTarget;
67
+ }
68
+ /**
69
+ * The WebSocket option object handed to `Bun.serve`'s `websocket` key.
70
+ * Framework handlers plus forwarded connection limits from `WebSocketConfig`.
71
+ */
72
+ export interface WebSocketServeOption {
73
+ open: (ws: any) => void | Promise<void>;
74
+ message: (ws: any, message: string | Buffer) => void | Promise<void>;
75
+ close: (ws: any, code: number, reason: string) => void | Promise<void>;
76
+ drain: (ws: any) => void | Promise<void>;
77
+ ping: (ws: any) => void | Promise<void>;
78
+ pong: (ws: any) => void | Promise<void>;
79
+ maxPayloadLength?: number;
80
+ idleTimeout?: number;
81
+ backpressureLimit?: number;
82
+ closeOnBackpressureLimit?: boolean;
83
+ compression?: boolean;
84
+ }
85
+ /**
86
+ * WebSocket adapter: upgrade handoff, auth gate and per-connection contexts.
87
+ */
88
+ export declare class WebSocketAdapter {
89
+ private router;
90
+ private config;
91
+ private debug;
92
+ private providers?;
93
+ private pluginTransform?;
94
+ private pluginBeforeRoute?;
95
+ private frameworkBeforeRoute;
96
+ private onRequestHooks;
97
+ private globalTransform?;
98
+ private globalBeforeRoute;
99
+ private ipHolder?;
100
+ private runtimeTarget?;
101
+ /** One context per connection, so `ws.data` mutations persist. */
102
+ private wsContexts;
103
+ /** Matched route per connection (see `getMatch`). */
104
+ private wsMatches;
105
+ /**
106
+ * Per-socket event chain: messages (and close/drain/ping/pong) are
107
+ * delivered in order and only after `open` finished. The first event runs
108
+ * synchronously; only queued events wait on the chain.
109
+ */
110
+ private wsChains;
111
+ constructor(options: WebSocketAdapterOptions);
112
+ /**
113
+ * Create the websocket option for Bun.serve()
114
+ */
115
+ createWebSocketOption(): WebSocketServeOption;
116
+ /**
117
+ * Serializes events per socket. The first event starts synchronously (a
118
+ * sync handler keeps its immediate behavior); queued events run in order
119
+ * after it. Handlers catch their own errors, so the chain never rejects.
120
+ */
121
+ private enqueue;
122
+ private handleOpen;
123
+ private runOpen;
124
+ private handleMessage;
125
+ private runMessage;
126
+ private handleClose;
127
+ private runClose;
128
+ private handleDrain;
129
+ private runDrain;
130
+ private handlePing;
131
+ private runPing;
132
+ private handlePong;
133
+ private runPong;
134
+ /**
135
+ * Entry point for WebSocket handling on every runtime: detects the
136
+ * platform, matches the route, runs the auth gate and performs the
137
+ * protocol handoff. Returns an explicit outcome so callers can never
138
+ * confuse "not an upgrade" with "socket taken over".
139
+ *
140
+ * @param request incoming request (upgrade or normal)
141
+ * @param server Bun serve handle when running under `Bun.serve`
142
+ * @param env platform bindings (bound onto the temporary auth context)
143
+ */
144
+ handleUpgrade(request: Request, server?: unknown, env?: BurgerEnv, executionCtx?: BurgerExecutionContext): Promise<WsUpgradeOutcome>;
145
+ /**
146
+ * Route-match + auth, shared by every platform's upgrade handoff. The Node
147
+ * bridge cannot reuse {@link handleUpgrade} because that always routes
148
+ * through {@link detectWsPlatform}, which falls back to `'node'` there.
149
+ */
150
+ private matchAndAuthorize;
151
+ /**
152
+ * Node cannot complete a WebSocket handshake inside a fetch handler —
153
+ * surface an explicit 501 with remediation instead of a thrown error.
154
+ */
155
+ private nodeFetchUpgradeUnsupported;
156
+ /**
157
+ * Declared-target counterpart to {@link nodeFetchUpgradeUnsupported}: a
158
+ * target with `websocket: false` (only `vercel` today) gets a "not
159
+ * supported here" response — the Node bridge would be wrong advice, since
160
+ * that target has no persistent process.
161
+ */
162
+ private capabilityUnsupportedResponse;
163
+ /** Shared event sink pushed into by non-Bun platforms. */
164
+ private eventSink;
165
+ /**
166
+ * Fetch-shaped wrapper around {@link handleUpgrade}, kept for backward
167
+ * compatibility. Returns `undefined` for BOTH "not an upgrade" and "socket
168
+ * taken over" — prefer {@link handleUpgrade} when the distinction matters.
169
+ */
170
+ createFetchHandler(): (request: Request, server?: unknown) => Promise<Response | undefined>;
171
+ /**
172
+ * Node integration: bridges node:http's `'upgrade'` event into the framework
173
+ * pipeline using a framing library's `WebSocketServer` (e.g. the `ws`
174
+ * package). `fetchHandler()` must run at least once first — WS routes are
175
+ * processed there.
176
+ *
177
+ * ```ts
178
+ * import http from 'node:http';
179
+ * import { WebSocketServer } from 'ws';
180
+ *
181
+ * const fetchHandler = await burger.fetchHandler();
182
+ * const bridge = burger.createNodeWsBridge({ WebSocketServer });
183
+ * http.createServer((req, res) => { ... }) // bridge req/res <-> Request/Response
184
+ * .on('upgrade', (req, socket, head) => bridge.handleUpgrade(req, socket, head))
185
+ * .listen(3000);
186
+ * ```
187
+ */
188
+ createNodeWsBridge(options: NodeWsBridgeOptions): NodeWsBridge;
189
+ /**
190
+ * Get route from WebSocket data
191
+ */
192
+ private getRouteFromWs;
193
+ /**
194
+ * The route + params matched at upgrade, cached per socket so a handler
195
+ * replacing `ws.data` cannot orphan the connection from its route.
196
+ */
197
+ private getMatch;
198
+ /**
199
+ * Creates (or reuses) the BurgerWS context for a connection: one per
200
+ * connection, so `ws.data` mutations in one handler persist into the next.
201
+ */
202
+ private createBurgerWS;
203
+ /**
204
+ * Runs the upgrade request through the same pre-handler chain HTTP uses —
205
+ * onRequest → transform → beforeRoute (Framework → Plugin → Global) —
206
+ * then enforces `config.auth.required` / `roles`. Always runs: auth
207
+ * check must never depend on a plugin being registered. `env` /
208
+ * `executionCtx` are bound onto the context so hooks can read bindings.
209
+ */
210
+ private runAuthHooks;
211
+ /**
212
+ * Renders a throw from the upgrade auth chain. Only framework
213
+ * `HTTPError`s keep their status; anything else is a server bug and
214
+ * renders a generic 500 (never a fake 401 echoing the thrown message).
215
+ */
216
+ private renderAuthError;
217
+ /** Applies collected mappers onion-style (last registered runs first). */
218
+ private applyMappers;
219
+ }