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