@ozanarslan/corpus 0.0.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.
- package/LICENSE.txt +20 -0
- package/README.md +337 -0
- package/dist/App/index.d.ts +377 -0
- package/dist/App/index.d.ts.map +1 -0
- package/dist/C.namespace.d.ts +27 -0
- package/dist/C.namespace.d.ts.map +1 -0
- package/dist/Config/index.d.ts +163 -0
- package/dist/Config/index.d.ts.map +1 -0
- package/dist/Context/ContextAccess/index.d.ts +50 -0
- package/dist/Context/ContextAccess/index.d.ts.map +1 -0
- package/dist/Context/index.d.ts +162 -0
- package/dist/Context/index.d.ts.map +1 -0
- package/dist/Controller/index.d.ts +164 -0
- package/dist/Controller/index.d.ts.map +1 -0
- package/dist/Cookies/index.d.ts +81 -0
- package/dist/Cookies/index.d.ts.map +1 -0
- package/dist/Cors/index.d.ts +135 -0
- package/dist/Cors/index.d.ts.map +1 -0
- package/dist/Exception/index.d.ts +85 -0
- package/dist/Exception/index.d.ts.map +1 -0
- package/dist/Globals/AppsRegistry/index.d.ts +48 -0
- package/dist/Globals/AppsRegistry/index.d.ts.map +1 -0
- package/dist/Globals/ParsersRegistry/index.d.ts +93 -0
- package/dist/Globals/ParsersRegistry/index.d.ts.map +1 -0
- package/dist/Globals/index.d.ts +109 -0
- package/dist/Globals/index.d.ts.map +1 -0
- package/dist/Headers/index.d.ts +208 -0
- package/dist/Headers/index.d.ts.map +1 -0
- package/dist/Middleware/index.d.ts +125 -0
- package/dist/Middleware/index.d.ts.map +1 -0
- package/dist/ParserBase/BodyParser/index.d.ts +138 -0
- package/dist/ParserBase/BodyParser/index.d.ts.map +1 -0
- package/dist/ParserBase/FormDataParser/index.d.ts +57 -0
- package/dist/ParserBase/FormDataParser/index.d.ts.map +1 -0
- package/dist/ParserBase/SchemaParser/index.d.ts +143 -0
- package/dist/ParserBase/SchemaParser/index.d.ts.map +1 -0
- package/dist/ParserBase/SearchParamsParser/index.d.ts +61 -0
- package/dist/ParserBase/SearchParamsParser/index.d.ts.map +1 -0
- package/dist/ParserBase/URLParamsParser/index.d.ts +32 -0
- package/dist/ParserBase/URLParamsParser/index.d.ts.map +1 -0
- package/dist/ParserBase/index.d.ts +92 -0
- package/dist/ParserBase/index.d.ts.map +1 -0
- package/dist/RateLimiter/index.d.ts +321 -0
- package/dist/RateLimiter/index.d.ts.map +1 -0
- package/dist/Request/index.d.ts +81 -0
- package/dist/Request/index.d.ts.map +1 -0
- package/dist/Res/index.d.ts +353 -0
- package/dist/Res/index.d.ts.map +1 -0
- package/dist/RouteBase/BundleRoute/index.d.ts +268 -0
- package/dist/RouteBase/BundleRoute/index.d.ts.map +1 -0
- package/dist/RouteBase/FileRoute/index.d.ts +121 -0
- package/dist/RouteBase/FileRoute/index.d.ts.map +1 -0
- package/dist/RouteBase/Route/index.d.ts +83 -0
- package/dist/RouteBase/Route/index.d.ts.map +1 -0
- package/dist/RouteBase/StaticRoute/index.d.ts +149 -0
- package/dist/RouteBase/StaticRoute/index.d.ts.map +1 -0
- package/dist/RouteBase/WebSocketRoute/index.d.ts +117 -0
- package/dist/RouteBase/WebSocketRoute/index.d.ts.map +1 -0
- package/dist/RouteBase/index.d.ts +206 -0
- package/dist/RouteBase/index.d.ts.map +1 -0
- package/dist/Server/index.d.ts +44 -0
- package/dist/Server/index.d.ts.map +1 -0
- package/dist/XFile/index.d.ts +202 -0
- package/dist/XFile/index.d.ts.map +1 -0
- package/dist/exports.d.ts +11 -0
- package/dist/exports.d.ts.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +47 -0
- package/dist/initialize.d.ts +22 -0
- package/dist/initialize.d.ts.map +1 -0
- package/dist/utils/assert.d.ts +7 -0
- package/dist/utils/assert.d.ts.map +1 -0
- package/dist/utils/is.d.ts +17 -0
- package/dist/utils/is.d.ts.map +1 -0
- package/dist/utils/lazy.d.ts +27 -0
- package/dist/utils/lazy.d.ts.map +1 -0
- package/dist/utils/logger.d.ts +31 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/object.d.ts +20 -0
- package/dist/utils/object.d.ts.map +1 -0
- package/dist/utils/path.d.ts +6 -0
- package/dist/utils/path.d.ts.map +1 -0
- package/dist/utils/tuple.d.ts +3 -0
- package/dist/utils/tuple.d.ts.map +1 -0
- package/package.json +57 -0
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The application root: owns the {@link Server}, the route table, the
|
|
3
|
+
* {@link Middleware} registry, and the request lifecycle that ties them
|
|
4
|
+
* together.
|
|
5
|
+
*
|
|
6
|
+
* An {@link App} is the only thing that talks to Bun's HTTP server.
|
|
7
|
+
* {@link RouteBase} implementations and {@link Middleware} instances register
|
|
8
|
+
* themselves against an app, and at {@link App.listen} time the app compiles
|
|
9
|
+
* them into a single {@link ServerRouteMap} plus a fallback {@link ServerHandler}.
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* const app = new App({ port: 3000, prefix: "/api" });
|
|
13
|
+
* await app.listen();
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @module App
|
|
17
|
+
*/
|
|
18
|
+
import { type ContextFactory, type ContextHandler, Context } from "../Context";
|
|
19
|
+
import type { CorsInterface } from "../Cors";
|
|
20
|
+
import type { Middleware } from "../Middleware";
|
|
21
|
+
import { type RouteBase } from "../RouteBase";
|
|
22
|
+
import type { Server, ServerHandler, ServerRouteMap } from "../Server";
|
|
23
|
+
import { type MaybePromise, type Nullable, type Optional, type OrString } from "../utils/is";
|
|
24
|
+
/**
|
|
25
|
+
* TLS material used to serve an {@link App} over HTTPS.
|
|
26
|
+
*
|
|
27
|
+
* Passing this to {@link App} switches {@link App.baseUrl} to the `https`
|
|
28
|
+
* scheme and hands the certificate straight to Bun's server.
|
|
29
|
+
*/
|
|
30
|
+
interface TlsOptions {
|
|
31
|
+
/** PEM-encoded certificate chain, as a string or a file buffer. */
|
|
32
|
+
cert: string | Buffer;
|
|
33
|
+
/** PEM-encoded private key matching {@link TlsOptions.cert}. */
|
|
34
|
+
key: string | Buffer;
|
|
35
|
+
/** Optional PEM-encoded certificate authority bundle for client verification. */
|
|
36
|
+
ca?: string | Buffer;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Construction options for {@link App}. Every field is optional; omitted fields
|
|
40
|
+
* keep the defaults declared on {@link App}.
|
|
41
|
+
*/
|
|
42
|
+
interface AppOptions {
|
|
43
|
+
/** TCP port to bind. See {@link App.port}. */
|
|
44
|
+
port?: number;
|
|
45
|
+
/**
|
|
46
|
+
* Path prefix prepended to the endpoint of every {@link RouteBase} registered
|
|
47
|
+
* on the app. See {@link App.prefix}.
|
|
48
|
+
*/
|
|
49
|
+
prefix?: string;
|
|
50
|
+
/** Interface to bind. See {@link App.hostname}. */
|
|
51
|
+
hostname?: OrString<"0.0.0.0" | "127.0.0.1" | "localhost">;
|
|
52
|
+
/** Seconds a connection may stay idle before Bun closes it. See {@link App.idleTimeout}. */
|
|
53
|
+
idleTimeout?: number;
|
|
54
|
+
/** {@link TlsOptions} to serve the app over HTTPS. See {@link App.tls}. */
|
|
55
|
+
tls?: TlsOptions;
|
|
56
|
+
/**
|
|
57
|
+
* App-wide request body ceiling in bytes, handed to Bun's server. An
|
|
58
|
+
* individual {@link RouteBase} can tighten this through its own
|
|
59
|
+
* {@link Config}, which {@link enforceBodyLimit} applies per request.
|
|
60
|
+
*/
|
|
61
|
+
maxRequestBodySize?: number;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Handles an {@link Error} raised anywhere in the request lifecycle. Assigned to
|
|
65
|
+
* {@link App.handleError}.
|
|
66
|
+
*
|
|
67
|
+
* @param error - The thrown error, typically an {@link Exception}.
|
|
68
|
+
* @param context - The {@link Context} the error was raised in. Absent only when
|
|
69
|
+
* the failure happened before a {@link Context} could be built.
|
|
70
|
+
* @returns The value to respond with — a {@link Res}, a plain body that
|
|
71
|
+
* {@link App.respond} will assign to {@link Res.body}, or a promise of either.
|
|
72
|
+
*/
|
|
73
|
+
type ErrorHandler<R = unknown> = (error: Error, context?: Context) => MaybePromise<R>;
|
|
74
|
+
/**
|
|
75
|
+
* The public shape of an application instance, implemented by {@link App}.
|
|
76
|
+
*
|
|
77
|
+
* Depend on this type rather than the {@link App} class when you need to accept
|
|
78
|
+
* an app without pinning the implementation.
|
|
79
|
+
*/
|
|
80
|
+
interface AppInterface {
|
|
81
|
+
/** The running {@link Server}, or `null` before {@link AppInterface.listen} and after {@link AppInterface.close}. */
|
|
82
|
+
server: Nullable<Server>;
|
|
83
|
+
/** {@link CorsInterface} policy applied to every response and to preflight requests. */
|
|
84
|
+
cors: Optional<CorsInterface>;
|
|
85
|
+
/** {@link RouteBase} instances registered on this app, in registration order. */
|
|
86
|
+
routes: Array<RouteBase>;
|
|
87
|
+
/** {@link Middleware} instances keyed by the {@link RouteBase.id} they target; `"*"` holds the global ones. */
|
|
88
|
+
middlewares: Map<string, Array<Middleware>>;
|
|
89
|
+
/** TCP port to bind. */
|
|
90
|
+
port: number;
|
|
91
|
+
/** Path prefix prepended to every {@link RouteBase} endpoint on this app. */
|
|
92
|
+
prefix: string;
|
|
93
|
+
/** Interface to bind. */
|
|
94
|
+
hostname: OrString<"0.0.0.0" | "127.0.0.1" | "localhost">;
|
|
95
|
+
/** Seconds a connection may stay idle before Bun closes it. */
|
|
96
|
+
idleTimeout?: number;
|
|
97
|
+
/** {@link TlsOptions} material; when set, the app is served over HTTPS. */
|
|
98
|
+
tls?: TlsOptions;
|
|
99
|
+
/** Origin the app is reachable at. */
|
|
100
|
+
get baseUrl(): string;
|
|
101
|
+
/** Compiles {@link RouteBase} and {@link Middleware} registrations and starts the {@link Server}. */
|
|
102
|
+
listen(): Promise<void>;
|
|
103
|
+
/** Stops the {@link Server} and releases the port. */
|
|
104
|
+
close(closeActiveConnections?: boolean): Promise<void>;
|
|
105
|
+
/** Hook run just before the {@link Server} starts. */
|
|
106
|
+
handleBeforeListen: Optional<() => MaybePromise<void>>;
|
|
107
|
+
/** Hook run just before the {@link Server} stops. */
|
|
108
|
+
handleBeforeClose: Optional<() => MaybePromise<void>>;
|
|
109
|
+
/** {@link ErrorHandler} that converts a thrown {@link Error} into a response value. */
|
|
110
|
+
handleError: ErrorHandler;
|
|
111
|
+
/** {@link ContextHandler} that produces the response for requests matching no {@link RouteBase}. */
|
|
112
|
+
handleNotFound: ContextHandler;
|
|
113
|
+
/** {@link ContextHandler} that produces the response for CORS preflight requests. */
|
|
114
|
+
handlePreflight: ContextHandler;
|
|
115
|
+
/** {@link ContextFactory} that builds the {@link Context} for each incoming request. */
|
|
116
|
+
contextFactory: ContextFactory;
|
|
117
|
+
/** Registers a {@link Middleware} against each {@link RouteBase.id} it targets. */
|
|
118
|
+
addMiddleware(middleware: Middleware): void;
|
|
119
|
+
/** Resolves the {@link Middleware} instances that apply to a {@link RouteBase.id}. */
|
|
120
|
+
findMiddlewares(routeId: string): Array<Middleware>;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* An HTTP application.
|
|
124
|
+
*
|
|
125
|
+
* The app is a container first: {@link RouteBase} implementations and
|
|
126
|
+
* {@link Middleware} instances attach to it as they are constructed. Nothing is
|
|
127
|
+
* compiled until {@link App.listen}, at which point {@link App.composeRoutes}
|
|
128
|
+
* turns routes into a {@link ServerRouteMap} and folds the matching
|
|
129
|
+
* {@link Middleware} handlers into each route's chain.
|
|
130
|
+
*
|
|
131
|
+
* Every request follows the same path — build a {@link Context}, resolve
|
|
132
|
+
* {@link Context.params}, {@link Context.search} and {@link Context.body}
|
|
133
|
+
* through the parsers registry, run the {@link composeHandlerChain} chain, apply
|
|
134
|
+
* {@link CorsInterface}, and serialise the {@link Res}. Anything thrown along
|
|
135
|
+
* the way is routed to {@link App.handleError}.
|
|
136
|
+
*
|
|
137
|
+
* Constructing an app calls {@link registerApp}, so it is discoverable without
|
|
138
|
+
* being passed around.
|
|
139
|
+
*/
|
|
140
|
+
declare class App implements AppInterface {
|
|
141
|
+
/**
|
|
142
|
+
* Creates an app and registers it globally with {@link registerApp}.
|
|
143
|
+
*
|
|
144
|
+
* @param opts - {@link AppOptions} overriding port, hostname, prefix, idle
|
|
145
|
+
* timeout, {@link TlsOptions} and body size. Any field left out keeps the
|
|
146
|
+
* default declared on the corresponding {@link App} property.
|
|
147
|
+
*/
|
|
148
|
+
constructor(opts?: AppOptions);
|
|
149
|
+
/**
|
|
150
|
+
* The live {@link Server}. `null` until {@link App.listen} is called and again
|
|
151
|
+
* after {@link App.close}.
|
|
152
|
+
*/
|
|
153
|
+
server: Nullable<Server>;
|
|
154
|
+
/**
|
|
155
|
+
* {@link CorsInterface} policy for this app. When set,
|
|
156
|
+
* {@link CorsInterface.handler} runs after every handler chain in
|
|
157
|
+
* {@link App.respond} and {@link CorsInterface.handlePreflight} answers
|
|
158
|
+
* preflight requests. Left unset, {@link App.handlePreflight} replies with
|
|
159
|
+
* {@link Status.NO_CONTENT} and no CORS headers are added.
|
|
160
|
+
*/
|
|
161
|
+
cors: Optional<CorsInterface>;
|
|
162
|
+
/** {@link RouteBase} instances attached to this app, in registration order. */
|
|
163
|
+
routes: Array<RouteBase>;
|
|
164
|
+
/**
|
|
165
|
+
* {@link Middleware} instances indexed by the {@link RouteBase.id} they
|
|
166
|
+
* target. The `"*"` key holds middlewares that run on every route as well as
|
|
167
|
+
* on the {@link App.handleNotFound} path.
|
|
168
|
+
*/
|
|
169
|
+
middlewares: Map<string, Array<Middleware>>;
|
|
170
|
+
/** Port the {@link Server} binds to. Defaults to `3000`. */
|
|
171
|
+
port: number;
|
|
172
|
+
/** Prefix prepended to every {@link RouteBase} endpoint on this app. Defaults to `""`. */
|
|
173
|
+
prefix: string;
|
|
174
|
+
/** Interface the {@link Server} binds to. Defaults to `"0.0.0.0"`. */
|
|
175
|
+
hostname: OrString<"0.0.0.0" | "127.0.0.1" | "localhost">;
|
|
176
|
+
/** Seconds an idle connection is kept open before Bun closes it. */
|
|
177
|
+
idleTimeout?: number;
|
|
178
|
+
/** {@link TlsOptions} material. When present the app is served over HTTPS. */
|
|
179
|
+
tls?: TlsOptions;
|
|
180
|
+
/**
|
|
181
|
+
* App-wide body ceiling in bytes passed to Bun. A {@link RouteBase} may
|
|
182
|
+
* declare a tighter limit in its {@link Config}, enforced per request by
|
|
183
|
+
* {@link enforceBodyLimit}.
|
|
184
|
+
*/
|
|
185
|
+
maxRequestBodySize?: number;
|
|
186
|
+
/**
|
|
187
|
+
* Origin the app is reachable at, for example `http://0.0.0.0:3000`.
|
|
188
|
+
*
|
|
189
|
+
* @returns The {@link Server} URL once listening; otherwise a URL derived from
|
|
190
|
+
* {@link App.tls}, {@link App.hostname} and {@link App.port}.
|
|
191
|
+
*/
|
|
192
|
+
get baseUrl(): string;
|
|
193
|
+
/**
|
|
194
|
+
* Compiles {@link App.composeRoutes} and {@link App.composeFetch} and hands
|
|
195
|
+
* them to `Bun.serve`, wiring the WebSocket callbacks through to the handlers
|
|
196
|
+
* carried by each {@link WebSocketRoute}. Unroutable middlewares are reported
|
|
197
|
+
* first by {@link App.warnUnmatchedMiddlewares}.
|
|
198
|
+
*
|
|
199
|
+
* Calling this when {@link App.server} already exists is a no-op that returns
|
|
200
|
+
* the existing one, so it is safe to reach for lazily.
|
|
201
|
+
*
|
|
202
|
+
* @returns The running {@link Server}.
|
|
203
|
+
*/
|
|
204
|
+
protected createServer(): Server;
|
|
205
|
+
/**
|
|
206
|
+
* Starts the app.
|
|
207
|
+
*
|
|
208
|
+
* Installs `SIGINT` and `SIGTERM` handlers that call {@link App.close}, runs
|
|
209
|
+
* {@link App.handleBeforeListen}, then compiles and starts the {@link Server}
|
|
210
|
+
* via {@link App.createServer}. A failure at any of these steps is logged and
|
|
211
|
+
* the app is closed rather than left half-started.
|
|
212
|
+
*
|
|
213
|
+
* @returns A promise that resolves once the {@link Server} is listening.
|
|
214
|
+
*/
|
|
215
|
+
listen(): Promise<void>;
|
|
216
|
+
/**
|
|
217
|
+
* Stops the app.
|
|
218
|
+
*
|
|
219
|
+
* Runs {@link App.handleBeforeClose}, stops the {@link Server}, and clears
|
|
220
|
+
* {@link App.server}. Outside the `test` value of {@link Config.nodeEnv} this
|
|
221
|
+
* also exits the process, so tests can close apps without tearing down the
|
|
222
|
+
* runner.
|
|
223
|
+
*
|
|
224
|
+
* @param closeActiveConnections - Whether in-flight connections are severed
|
|
225
|
+
* immediately rather than allowed to drain. Defaults to `true`.
|
|
226
|
+
* @returns A promise that resolves once the {@link Server} has stopped.
|
|
227
|
+
*/
|
|
228
|
+
close(closeActiveConnections?: boolean): Promise<void>;
|
|
229
|
+
/**
|
|
230
|
+
* Compiles {@link App.routes} into the {@link ServerRouteMap} Bun expects,
|
|
231
|
+
* keyed by endpoint and then by {@link Method}.
|
|
232
|
+
*
|
|
233
|
+
* Each entry is a full request pipeline wrapped in {@link App.finalize}:
|
|
234
|
+
* wildcard segments are lifted into {@link Req.params} (Bun does not treat
|
|
235
|
+
* them as params), then params, search and body are parsed and validated
|
|
236
|
+
* against the {@link RouteBase} {@link Config} — but only the ones the handler
|
|
237
|
+
* chain actually reads, as reported by {@link getContextAccess}. Routes with
|
|
238
|
+
* {@link RouteVariant.websocket} upgrade the connection into a
|
|
239
|
+
* {@link WebSocketRoute} instead of responding, and {@link Method.GET} and
|
|
240
|
+
* {@link Method.HEAD} skip body work entirely.
|
|
241
|
+
*
|
|
242
|
+
* @returns A {@link ServerRouteMap} ready to hand to `Bun.serve`.
|
|
243
|
+
* @throws {@link Exception} with {@link Status.UPGRADE_REQUIRED} when a
|
|
244
|
+
* WebSocket upgrade is rejected.
|
|
245
|
+
*/
|
|
246
|
+
protected composeRoutes(): ServerRouteMap;
|
|
247
|
+
/**
|
|
248
|
+
* Builds the {@link ServerHandler} Bun uses for requests that matched no
|
|
249
|
+
* {@link RouteBase}.
|
|
250
|
+
*
|
|
251
|
+
* Preflight requests — {@link Method.OPTIONS} carrying
|
|
252
|
+
* {@link HeaderKey.AccessControlRequestMethod} — go to
|
|
253
|
+
* {@link App.handlePreflight}. Everything else runs the global (`"*"`)
|
|
254
|
+
* {@link Middleware} chain followed by {@link App.handleNotFound}, so global
|
|
255
|
+
* middlewares still observe traffic to unknown endpoints.
|
|
256
|
+
*
|
|
257
|
+
* @returns The `fetch` handler for `Bun.serve`, wrapped by
|
|
258
|
+
* {@link App.finalize}.
|
|
259
|
+
*/
|
|
260
|
+
protected composeFetch(): ServerHandler;
|
|
261
|
+
/**
|
|
262
|
+
* Wraps a {@link ContextHandler} into the {@link ServerHandler} Bun calls,
|
|
263
|
+
* giving it a {@link Context} from {@link App.contextFactory} and guaranteeing
|
|
264
|
+
* that every outcome — value or throw — leaves as a `Response`.
|
|
265
|
+
*
|
|
266
|
+
* This is the single boundary where errors are caught, so every throw reaches
|
|
267
|
+
* {@link App.handleError} through {@link App.respondWithError}.
|
|
268
|
+
*
|
|
269
|
+
* @param handler - The {@link ContextHandler} to run for the request.
|
|
270
|
+
* @returns A {@link ServerHandler} suitable for a {@link ServerRouteMap} entry
|
|
271
|
+
* or for `Bun.serve`'s `fetch`.
|
|
272
|
+
*/
|
|
273
|
+
protected finalize(handler: ContextHandler): ServerHandler;
|
|
274
|
+
/**
|
|
275
|
+
* Turns a handler's return value into the response sent over the wire.
|
|
276
|
+
*
|
|
277
|
+
* A returned {@link Res} replaces {@link Context.res} wholesale; any other
|
|
278
|
+
* defined value becomes {@link Res.body}; `undefined` leaves the existing
|
|
279
|
+
* {@link Res} untouched, which is how handlers that mutate
|
|
280
|
+
* {@link Context.res} directly are supported. {@link CorsInterface.handler}
|
|
281
|
+
* runs last and separately from the {@link Middleware} chain, so CORS headers
|
|
282
|
+
* cannot be clobbered by a short-circuiting middleware.
|
|
283
|
+
*
|
|
284
|
+
* @param context - The {@link Context} for the request.
|
|
285
|
+
* @param result - Whatever the handler chain returned.
|
|
286
|
+
* @returns The native `Response` produced by {@link Res.toNativeResponse}.
|
|
287
|
+
*/
|
|
288
|
+
protected respond(context: Context, result: unknown): Promise<Response>;
|
|
289
|
+
/**
|
|
290
|
+
* Runs {@link App.handleError} and responds with its result.
|
|
291
|
+
*
|
|
292
|
+
* If the error handler itself throws, that second failure is logged and a bare
|
|
293
|
+
* {@link Status.INTERNAL_SERVER_ERROR} is returned — the request never escapes
|
|
294
|
+
* without a response.
|
|
295
|
+
*
|
|
296
|
+
* @param context - The {@link Context} the failure occurred in.
|
|
297
|
+
* @param err - The {@link Error} thrown by the handler chain.
|
|
298
|
+
* @returns The error response.
|
|
299
|
+
*/
|
|
300
|
+
protected respondWithError(context: Context, err: Error): Promise<Response>;
|
|
301
|
+
/**
|
|
302
|
+
* Hook run inside {@link App.listen}, before the {@link Server} is created.
|
|
303
|
+
* Use it for setup that must complete before traffic is accepted; throwing
|
|
304
|
+
* here aborts startup and closes the app.
|
|
305
|
+
*/
|
|
306
|
+
handleBeforeListen: Optional<() => MaybePromise<void>>;
|
|
307
|
+
/**
|
|
308
|
+
* Hook run inside {@link App.close}, before the {@link Server} is stopped. Use
|
|
309
|
+
* it to release resources the app owns.
|
|
310
|
+
*/
|
|
311
|
+
handleBeforeClose: Optional<() => MaybePromise<void>>;
|
|
312
|
+
/**
|
|
313
|
+
* Default {@link ErrorHandler}. An {@link Exception} is rendered through
|
|
314
|
+
* {@link Exception.toRes}; anything else becomes an opaque
|
|
315
|
+
* {@link Status.INTERNAL_SERVER_ERROR} {@link Res}, so internal failures never
|
|
316
|
+
* leak their message. Replace it to customise error output.
|
|
317
|
+
*
|
|
318
|
+
* @param err - The thrown {@link Error}.
|
|
319
|
+
* @returns The {@link Res} to send.
|
|
320
|
+
*/
|
|
321
|
+
handleError: ErrorHandler;
|
|
322
|
+
/**
|
|
323
|
+
* Default {@link ContextHandler} for unmatched requests. Replace it to
|
|
324
|
+
* customise the 404 body.
|
|
325
|
+
*
|
|
326
|
+
* @param c - The {@link Context} for the unmatched request.
|
|
327
|
+
* @returns A {@link Status.NOT_FOUND} {@link Res} naming the method and URL
|
|
328
|
+
* that did not resolve.
|
|
329
|
+
*/
|
|
330
|
+
handleNotFound: ContextHandler;
|
|
331
|
+
/**
|
|
332
|
+
* Default {@link ContextHandler} for CORS preflight requests. Delegates to
|
|
333
|
+
* {@link CorsInterface.handlePreflight} when {@link App.cors} is configured.
|
|
334
|
+
*
|
|
335
|
+
* @param c - The {@link Context} for the preflight request.
|
|
336
|
+
* @returns The CORS preflight response, or an empty
|
|
337
|
+
* {@link Status.NO_CONTENT} {@link Res} when no {@link CorsInterface} is set.
|
|
338
|
+
*/
|
|
339
|
+
handlePreflight: ContextHandler;
|
|
340
|
+
/**
|
|
341
|
+
* Default {@link ContextFactory}. Replace it to have the app build a
|
|
342
|
+
* {@link Context} subclass carrying your own per-request state.
|
|
343
|
+
*
|
|
344
|
+
* @param request - The incoming request.
|
|
345
|
+
* @param server - The {@link Server} that accepted it.
|
|
346
|
+
* @returns A new {@link Context}.
|
|
347
|
+
*/
|
|
348
|
+
contextFactory: ContextFactory;
|
|
349
|
+
/**
|
|
350
|
+
* Registers a {@link Middleware} under every {@link RouteBase.id} in
|
|
351
|
+
* {@link Middleware.routeIds}, so one instance can serve several routes.
|
|
352
|
+
*
|
|
353
|
+
* @param middleware - The {@link Middleware} to register.
|
|
354
|
+
*/
|
|
355
|
+
addMiddleware(middleware: Middleware): void;
|
|
356
|
+
/**
|
|
357
|
+
* Resolves the {@link Middleware} instances that apply to a route, global ones
|
|
358
|
+
* first so they wrap the route-specific ones.
|
|
359
|
+
*
|
|
360
|
+
* @param routeId - The {@link RouteBase.id} to resolve for, or `"*"` to get
|
|
361
|
+
* only the global middlewares without duplicating them.
|
|
362
|
+
* @returns The middlewares in execution order.
|
|
363
|
+
*/
|
|
364
|
+
findMiddlewares(routeId: string): Array<Middleware>;
|
|
365
|
+
/**
|
|
366
|
+
* Logs a warning for every {@link Middleware} whose target
|
|
367
|
+
* {@link RouteBase.id} is not registered on this app and which therefore can
|
|
368
|
+
* never run — usually a typo or a route that was never attached.
|
|
369
|
+
*
|
|
370
|
+
* Runs from {@link App.createServer} rather than {@link App.addMiddleware},
|
|
371
|
+
* because registration order is not guaranteed and a middleware may legally be
|
|
372
|
+
* added before its route.
|
|
373
|
+
*/
|
|
374
|
+
protected warnUnmatchedMiddlewares(): void;
|
|
375
|
+
}
|
|
376
|
+
export { App, type AppInterface };
|
|
377
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/App/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAGH,OAAO,EAAE,KAAK,cAAc,EAAE,KAAK,cAAc,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAE9E,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAK5C,OAAO,KAAK,EAAqB,UAAU,EAAE,MAAM,cAAc,CAAC;AAGlE,OAAO,EAAgB,KAAK,SAAS,EAAE,MAAM,aAAa,CAAC;AAE3D,OAAO,KAAK,EAAE,MAAM,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AACtE,OAAO,EAIN,KAAK,YAAY,EACjB,KAAK,QAAQ,EACb,KAAK,QAAQ,EAEb,KAAK,QAAQ,EACb,MAAM,YAAY,CAAC;AAMpB;;;;;GAKG;AACH,UAAU,UAAU;IACnB,mEAAmE;IACnE,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC;IACtB,gEAAgE;IAChE,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC;IACrB,iFAAiF;IACjF,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;CACrB;AAED;;;GAGG;AACH,UAAU,UAAU;IACnB,8CAA8C;IAC9C,IAAI,CAAC,EAAE,MAAM,CAAC;IACd;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,mDAAmD;IACnD,QAAQ,CAAC,EAAE,QAAQ,CAAC,SAAS,GAAG,WAAW,GAAG,W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AAC,wBAAwB,IAAI,IAAI;CAS1C;AAED,OAAO,EAAE,GAAG,EAAE,KAAK,YAAY,EAAE,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export { App, type AppInterface } from "./App";
|
|
2
|
+
export { BodyParser, type BodyParserInterface } from "./ParserBase/BodyParser";
|
|
3
|
+
export { BundleRoute } from "./RouteBase/BundleRoute";
|
|
4
|
+
export { Config } from "./Config";
|
|
5
|
+
export { Context, type ContextFactory } from "./Context";
|
|
6
|
+
export { Controller } from "./Controller";
|
|
7
|
+
export { Cookies } from "./Cookies";
|
|
8
|
+
export { Cors, type CorsInterface } from "./Cors";
|
|
9
|
+
export { Exception } from "./Exception";
|
|
10
|
+
export { FileRoute } from "./RouteBase/FileRoute";
|
|
11
|
+
export { FormDataParser } from "./ParserBase/FormDataParser";
|
|
12
|
+
export { HeaderKey, type CacheControlDefinition, type ContentDispositionDefinition } from "./Headers";
|
|
13
|
+
export { Middleware, type MiddlewareDefinition, type MiddlewareUseOn, type MiddlewareHandler } from "./Middleware";
|
|
14
|
+
export { ParserBase, type ParserBaseInterface } from "./ParserBase";
|
|
15
|
+
export { RateLimiter, RateLimiterMemoryStore, type RateLimiterStoreInterface, type RateLimiterConfig } from "./RateLimiter";
|
|
16
|
+
export { Method } from "./Request";
|
|
17
|
+
export { Res, Status } from "./Res";
|
|
18
|
+
export { Route } from "./RouteBase/Route";
|
|
19
|
+
export { RouteBase, RouteVariant, type RouteAddress, type RouteConfig } from "./RouteBase";
|
|
20
|
+
export { SchemaParser, type SchemaParserInterface, type InferModel } from "./ParserBase/SchemaParser";
|
|
21
|
+
export { SearchParamsParser } from "./ParserBase/SearchParamsParser";
|
|
22
|
+
export type { Server, ServerWebSocket, ServerHandler, ServerRouteMap } from "./Server";
|
|
23
|
+
export { StaticRoute } from "./RouteBase/StaticRoute";
|
|
24
|
+
export { URLParamsParser } from "./ParserBase/URLParamsParser";
|
|
25
|
+
export { WebSocketRoute, type WebSocketRouteDefinition, type WebSocketOnOpen, type WebSocketOnClose, type WebSocketOnMessage } from "./RouteBase/WebSocketRoute";
|
|
26
|
+
export { XFile } from "./XFile";
|
|
27
|
+
//# sourceMappingURL=C.namespace.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"C.namespace.d.ts","sourceRoot":"","sources":["../src/C.namespace.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,GAAG,EAAE,KAAK,YAAY,EAAE,MAAM,OAAO,CAAC;AAE/C,OAAO,EAAE,UAAU,EAAE,KAAK,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAE/E,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAEtD,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC,OAAO,EAAE,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,WAAW,CAAC;AAEzD,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,OAAO,EAAE,IAAI,EAAE,KAAK,aAAa,EAAE,MAAM,QAAQ,CAAC;AAElD,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAElD,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAQ7D,OAAO,EAAE,SAAS,EAAE,KAAK,sBAAsB,EAAE,KAAK,4BAA4B,EAAE,MAAM,WAAW,CAAC;AAEtG,OAAO,EAAE,UAAU,EAAE,KAAK,oBAAoB,EAAE,KAAK,eAAe,EAAE,KAAK,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAEnH,OAAO,EAAE,UAAU,EAAE,KAAK,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAEpE,OAAO,EAAE,WAAW,EAAE,sBAAsB,EAAE,KAAK,yBAAyB,EAAE,KAAK,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAE5H,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAEnC,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAEpC,OAAO,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAE1C,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,KAAK,YAAY,EAAE,KAAK,WAAW,EAAE,MAAM,aAAa,CAAC;AAE3F,OAAO,EAAE,YAAY,EAAE,KAAK,qBAAqB,EAAE,KAAK,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAEtG,OAAO,EAAE,kBAAkB,EAAE,MAAM,iCAAiC,CAAC;AAErE,YAAY,EAAE,MAAM,EAAE,eAAe,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAEvF,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAEtD,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAE/D,OAAO,EAAE,cAAc,EAAE,KAAK,wBAAwB,EAAE,KAAK,eAAe,EAAE,KAAK,gBAAgB,EAAE,KAAK,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAEjK,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC"}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed access to environment variables and the current runtime environment.
|
|
3
|
+
*
|
|
4
|
+
* {@link Config} is a static façade over `process.env` — nothing is cached, so a
|
|
5
|
+
* read always reflects the current value. Beyond convenience, it is what the
|
|
6
|
+
* framework itself consults for environment-dependent behaviour: {@link App}
|
|
7
|
+
* checks {@link Config.nodeEnv} before exiting the process on
|
|
8
|
+
* {@link App.close}.
|
|
9
|
+
*
|
|
10
|
+
* Declare your variables on {@link Env} in your own code to get autocompletion
|
|
11
|
+
* and a checked key for every lookup.
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* const port = Config.get("PORT", { parser: Number, fallback: 3000 });
|
|
15
|
+
* const secret = Config.require("JWT_SECRET");
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @module Config
|
|
19
|
+
*/
|
|
20
|
+
import { type OrString } from "../utils/is";
|
|
21
|
+
/**
|
|
22
|
+
* Declaration target for an application's environment variables.
|
|
23
|
+
*
|
|
24
|
+
* Empty by design. Augment it from your own code and every key you add becomes a
|
|
25
|
+
* suggested {@link EnvKey}:
|
|
26
|
+
*
|
|
27
|
+
* ```ts
|
|
28
|
+
* declare module "corpus" {
|
|
29
|
+
* interface Env {
|
|
30
|
+
* DATABASE_URL: string;
|
|
31
|
+
* JWT_SECRET: string;
|
|
32
|
+
* }
|
|
33
|
+
* }
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
interface Env {
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* The value of `NODE_ENV`. The three known environments are suggested, but any
|
|
40
|
+
* string is accepted — {@link OrString} keeps the literals as hints rather than
|
|
41
|
+
* as a closed set.
|
|
42
|
+
*/
|
|
43
|
+
type NodeEnv = OrString<"development" | "production" | "test">;
|
|
44
|
+
/**
|
|
45
|
+
* A variable name accepted by the {@link Config} lookups. Keys declared on
|
|
46
|
+
* {@link Env} are suggested; any other string still resolves.
|
|
47
|
+
*/
|
|
48
|
+
type EnvKey = OrString<keyof Env>;
|
|
49
|
+
/**
|
|
50
|
+
* Static accessor for environment variables.
|
|
51
|
+
*
|
|
52
|
+
* Every read goes straight to `process.env`, so a variable set after startup —
|
|
53
|
+
* by a test, or by {@link Config.set} — is visible immediately.
|
|
54
|
+
*
|
|
55
|
+
* The three lookups differ in how they treat a missing value:
|
|
56
|
+
* {@link Config.get} returns `undefined` or a fallback, {@link Config.require}
|
|
57
|
+
* throws, and {@link Config.has} only reports presence.
|
|
58
|
+
*/
|
|
59
|
+
declare class Config {
|
|
60
|
+
/**
|
|
61
|
+
* The live `process.env` object.
|
|
62
|
+
*
|
|
63
|
+
* @returns The environment, unwrapped. Reading it directly bypasses the
|
|
64
|
+
* parsing and fallback handling of {@link Config.get}.
|
|
65
|
+
*/
|
|
66
|
+
static get env(): NodeJS.ProcessEnv;
|
|
67
|
+
/**
|
|
68
|
+
* The current environment name.
|
|
69
|
+
*
|
|
70
|
+
* @returns The value of `NODE_ENV`, or `"development"` when it is unset — an
|
|
71
|
+
* unconfigured process is treated as a development one.
|
|
72
|
+
*/
|
|
73
|
+
static get nodeEnv(): NodeEnv;
|
|
74
|
+
/**
|
|
75
|
+
* @returns `true` when {@link Config.nodeEnv} is `"production"`.
|
|
76
|
+
*/
|
|
77
|
+
static get isProd(): boolean;
|
|
78
|
+
/**
|
|
79
|
+
* @returns `true` when {@link Config.nodeEnv} is `"development"`, including
|
|
80
|
+
* when `NODE_ENV` is unset.
|
|
81
|
+
*/
|
|
82
|
+
static get isDev(): boolean;
|
|
83
|
+
/**
|
|
84
|
+
* @returns `true` when {@link Config.nodeEnv} is `"test"`. {@link App.close}
|
|
85
|
+
* checks this to avoid exiting the process out from under a test runner.
|
|
86
|
+
*/
|
|
87
|
+
static get isTest(): boolean;
|
|
88
|
+
/**
|
|
89
|
+
* Reports whether a variable is set, without reading its value.
|
|
90
|
+
*
|
|
91
|
+
* @param key - The variable name to check.
|
|
92
|
+
* @returns `true` when the variable is defined. An empty string counts as
|
|
93
|
+
* defined.
|
|
94
|
+
*/
|
|
95
|
+
static has(key: EnvKey): boolean;
|
|
96
|
+
/**
|
|
97
|
+
* Reads a variable as a raw string.
|
|
98
|
+
*
|
|
99
|
+
* @param key - The variable name to read.
|
|
100
|
+
* @returns The value, or `undefined` when unset.
|
|
101
|
+
*/
|
|
102
|
+
static get(key: EnvKey): string | undefined;
|
|
103
|
+
/**
|
|
104
|
+
* Reads a variable with a guaranteed result, since a fallback covers the unset
|
|
105
|
+
* case.
|
|
106
|
+
*
|
|
107
|
+
* @param key - The variable name to read.
|
|
108
|
+
* @param opts - Lookup options.
|
|
109
|
+
* @param opts.parser - Converts the raw string into the value you want. Not
|
|
110
|
+
* applied to the fallback, which is used as given.
|
|
111
|
+
* @param opts.fallback - Returned when the variable is unset.
|
|
112
|
+
* @returns The parsed value, or the fallback.
|
|
113
|
+
*/
|
|
114
|
+
static get<T = string>(key: EnvKey, opts: {
|
|
115
|
+
parser?: (raw: string) => T;
|
|
116
|
+
fallback: T;
|
|
117
|
+
}): T;
|
|
118
|
+
/**
|
|
119
|
+
* Reads and converts a variable with no fallback.
|
|
120
|
+
*
|
|
121
|
+
* @param key - The variable name to read.
|
|
122
|
+
* @param opts - Lookup options.
|
|
123
|
+
* @param opts.parser - Converts the raw string into the value you want.
|
|
124
|
+
* @param opts.fallback - Returned when the variable is unset.
|
|
125
|
+
* @returns The parsed value, or `undefined` when the variable is unset.
|
|
126
|
+
*/
|
|
127
|
+
static get<T = string>(key: EnvKey, opts: {
|
|
128
|
+
parser: (raw: string) => T;
|
|
129
|
+
fallback?: T;
|
|
130
|
+
}): T | undefined;
|
|
131
|
+
/**
|
|
132
|
+
* Reads a variable that the application cannot run without.
|
|
133
|
+
*
|
|
134
|
+
* @param key - The variable name to read.
|
|
135
|
+
* @returns The raw value.
|
|
136
|
+
* @throws {@link Error} when the variable is unset.
|
|
137
|
+
*/
|
|
138
|
+
static require(key: EnvKey): string;
|
|
139
|
+
/**
|
|
140
|
+
* Reads and converts a variable that the application cannot run without.
|
|
141
|
+
*
|
|
142
|
+
* @param key - The variable name to read.
|
|
143
|
+
* @param parser - Converts the raw string into the value you want.
|
|
144
|
+
* @returns The parsed value.
|
|
145
|
+
* @throws {@link Error} when the variable is unset. The parser is never called
|
|
146
|
+
* in that case, so it can assume a real value.
|
|
147
|
+
*/
|
|
148
|
+
static require<T = string>(key: EnvKey, parser: (raw: string) => T): T;
|
|
149
|
+
/**
|
|
150
|
+
* Writes a variable into the environment, stringifying the value the way the
|
|
151
|
+
* environment stores everything.
|
|
152
|
+
*
|
|
153
|
+
* Mutates the real `process.env`, so the change is visible to every reader in
|
|
154
|
+
* the process, not only to {@link Config}.
|
|
155
|
+
*
|
|
156
|
+
* @param key - The variable name to write.
|
|
157
|
+
* @param value - The value to store; numbers and booleans are converted to
|
|
158
|
+
* their string form.
|
|
159
|
+
*/
|
|
160
|
+
static set(key: string, value: string | number | boolean): void;
|
|
161
|
+
}
|
|
162
|
+
export { Config, type Env };
|
|
163
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/Config/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAAuB,KAAK,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEhE;;;;;;;;;;;;;;GAcG;AACH,UAAU,GAAG;CAAG;AAEhB;;;;GAIG;AACH,KAAK,OAAO,GAAG,QAAQ,CAAC,aAAa,GAAG,YAAY,GAAG,MAAM,CAAC,CAAC;AAE/D;;;GAGG;AACH,KAAK,MAAM,GAAG,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC;AAElC;;;;;;;;;GASG;AACH,cAAM,MAAM;IACX;;;;;OAKG;IACH,MAAM,KAAK,GAAG,IAAI,MAAM,CAAC,UAAU,CAElC;IAED;;;;;OAKG;IACH,MAAM,KAAK,OAAO,IAAI,OAAO,CAE5B;IAED;;OAEG;IACH,MAAM,KAAK,MAAM,IAAI,OAAO,CAE3B;IAED;;;OAGG;IACH,MAAM,KAAK,KAAK,IAAI,OAAO,CAE1B;IAED;;;OAGG;IACH,MAAM,KAAK,MAAM,IAAI,OAAO,CAE3B;IAED;;;;;;OAMG;IACH,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAIhC;;;;;OAKG;IACH,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS;IAC3C;;;;;;;;;;OAUG;IACH,MAAM,CAAC,GAAG,CAAC,CAAC,GAAG,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE;QAAE,MAAM,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,CAAC,CAAC;QAAC,QAAQ,EAAE,CAAC,CAAA;KAAE,GAAG,CAAC;IAC1F;;;;;;;;OAQG;IACH,MAAM,CAAC,GAAG,CAAC,CAAC,GAAG,MAAM,EACpB,GAAG,EAAE,MAAM,EACX,IAAI,EAAE;QAAE,MAAM,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,CAAC,CAAC;QAAC,QAAQ,CAAC,EAAE,CAAC,CAAA;KAAE,GAChD,CAAC,GAAG,SAAS;IAehB;;;;;;OAMG;IACH,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;IACnC;;;;;;;;OAQG;IACH,MAAM,CAAC,OAAO,CAAC,CAAC,GAAG,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,CAAC,GAAG,CAAC;IAStE;;;;;;;;;;OAUG;IACH,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI;CAG/D;AAED,OAAO,EAAE,MAAM,EAAE,KAAK,GAAG,EAAE,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Works out which parts of a request a route actually uses, so the rest can be
|
|
3
|
+
* skipped.
|
|
4
|
+
*
|
|
5
|
+
* Parsing a body, a query string and path parameters costs work, and most
|
|
6
|
+
* handlers only use one of them. Before serving a route, {@link App} asks this
|
|
7
|
+
* module what the handler chain reads and then parses only that.
|
|
8
|
+
*
|
|
9
|
+
* The answer comes from reading the handler's own source: `fn.toString()` gives
|
|
10
|
+
* back the function's text, and every mention of the context parameter is
|
|
11
|
+
* checked. `c.body` is a read of the body. `({ params }) => …` is a read of the
|
|
12
|
+
* params. Anything less obvious — passing `c` to another function, returning it,
|
|
13
|
+
* indexing it with a variable — means the handler could reach anything, so
|
|
14
|
+
* everything gets parsed.
|
|
15
|
+
*
|
|
16
|
+
* That fallback is the rule the whole module follows: when in doubt, parse it
|
|
17
|
+
* all. Guessing "unused" wrongly would hand a handler an empty body it was
|
|
18
|
+
* counting on. Guessing "used" wrongly just does some work nobody needed.
|
|
19
|
+
*
|
|
20
|
+
* @module ContextAccess
|
|
21
|
+
*/
|
|
22
|
+
import type { RouteConfig } from "../../RouteBase";
|
|
23
|
+
import { type Optional } from "../../utils/is";
|
|
24
|
+
/**
|
|
25
|
+
* The context properties whose population can be skipped. Order is irrelevant;
|
|
26
|
+
* this exists so the key list, the record type, and the runtime loops can never
|
|
27
|
+
* drift apart.
|
|
28
|
+
*/
|
|
29
|
+
declare const KEYS: readonly ["body", "search", "params"];
|
|
30
|
+
/** One of the skippable {@link Context} properties: `body`, `search` or `params`. */
|
|
31
|
+
type ContextAccessKey = (typeof KEYS)[number];
|
|
32
|
+
/** Which properties to parse. `true` means the handler chain might read it. */
|
|
33
|
+
type ContextAccess = Record<ContextAccessKey, boolean>;
|
|
34
|
+
/**
|
|
35
|
+
* Combines every handler in a route's chain into one answer.
|
|
36
|
+
*
|
|
37
|
+
* A property is parsed if any handler reads it, so a {@link Middleware} that
|
|
38
|
+
* needs the body still gets one even when the route handler ignores it. A key
|
|
39
|
+
* with a schema in the route's {@link RouteConfig} is always parsed — skipping
|
|
40
|
+
* it would let an invalid payload through unvalidated.
|
|
41
|
+
*
|
|
42
|
+
* @param handlers - Every function in the chain, middleware and route handler
|
|
43
|
+
* alike.
|
|
44
|
+
* @param config - The route's config, read for its schemas.
|
|
45
|
+
* @returns Which properties {@link App} should populate on the
|
|
46
|
+
* {@link Context}.
|
|
47
|
+
*/
|
|
48
|
+
declare function getContextAccess(handlers: ReadonlyArray<Function>, config: Optional<RouteConfig>): ContextAccess;
|
|
49
|
+
export { getContextAccess };
|
|
50
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/Context/ContextAccess/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,EAA4B,KAAK,QAAQ,EAAE,MAAM,YAAY,CAAC;AAErE;;;;GAIG;AACH,QAAA,MAAM,IAAI,uCAAwC,CAAC;AAEnD,qFAAqF;AACrF,KAAK,gBAAgB,GAAG,CAAC,OAAO,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC;AAE9C,+EAA+E;AAC/E,KAAK,aAAa,GAAG,MAAM,CAAC,gBAAgB,EAAE,OAAO,CAAC,CAAC;AAiQvD;;;;;;;;;;;;;GAaG;AACH,iBAAS,gBAAgB,CACxB,QAAQ,EAAE,aAAa,CAAC,QAAQ,CAAC,EACjC,MAAM,EAAE,QAAQ,CAAC,WAAW,CAAC,GAC3B,aAAa,CAiBf;AAED,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
|