sevok 0.0.2
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 +21 -0
- package/README.md +342 -0
- package/README.zh-CN.md +342 -0
- package/bin/sevok.mjs +13 -0
- package/dist/_color-B42q-MpL.mjs +34 -0
- package/dist/_node_like-BRaAGeNM.mjs +112 -0
- package/dist/_shared-38k_JIsU.mjs +92 -0
- package/dist/bun.d.mts +31 -0
- package/dist/bun.mjs +58 -0
- package/dist/cli.d.mts +142 -0
- package/dist/cli.mjs +461 -0
- package/dist/core-BGp2ZR_k.d.mts +665 -0
- package/dist/core-CmUugTW7.mjs +732 -0
- package/dist/deno.d.mts +32 -0
- package/dist/deno.mjs +65 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +2 -0
- package/dist/log.d.mts +27 -0
- package/dist/log.mjs +26 -0
- package/dist/node.d.mts +32 -0
- package/dist/node.mjs +139 -0
- package/dist/static.d.mts +35 -0
- package/dist/static.mjs +100 -0
- package/dist/stream.d.mts +56 -0
- package/dist/stream.mjs +68 -0
- package/package.json +68 -0
|
@@ -0,0 +1,665 @@
|
|
|
1
|
+
import { EventDispatcher } from "@hornjs/evt";
|
|
2
|
+
import nodeHTTP from "node:http";
|
|
3
|
+
import nodeHTTPS from "node:https";
|
|
4
|
+
import nodeHTTP2 from "node:http2";
|
|
5
|
+
import * as NodeNet from "node:net";
|
|
6
|
+
|
|
7
|
+
//#region src/core.d.ts
|
|
8
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
9
|
+
/**
|
|
10
|
+
* HTTP methods supported by the route table shorthand.
|
|
11
|
+
*/
|
|
12
|
+
type HTTPMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD" | "OPTIONS";
|
|
13
|
+
/**
|
|
14
|
+
* Core request handler function.
|
|
15
|
+
*
|
|
16
|
+
* Receives an `InvocationContext` containing the request and invocation state.
|
|
17
|
+
* Runtime adapters, middleware, and higher-level helpers all eventually resolve down to a
|
|
18
|
+
* `ServerHandler`.
|
|
19
|
+
*/
|
|
20
|
+
type ServerHandler = (context: InvocationContext) => MaybePromise<Response>;
|
|
21
|
+
/**
|
|
22
|
+
* Error handler for failures raised during request handling.
|
|
23
|
+
*
|
|
24
|
+
* This is used by `wrapFetch()` to turn thrown exceptions or rejected promises
|
|
25
|
+
* into a fallback `Response`.
|
|
26
|
+
*/
|
|
27
|
+
type ErrorHandler = (error: unknown) => MaybePromise<Response>;
|
|
28
|
+
/**
|
|
29
|
+
* Request middleware.
|
|
30
|
+
*
|
|
31
|
+
* `next` keeps the same handler signature so middleware can replace the request
|
|
32
|
+
* object before passing control downstream.
|
|
33
|
+
*/
|
|
34
|
+
type ServerMiddleware = (context: InvocationContext, next: ServerHandler) => MaybePromise<Response>;
|
|
35
|
+
/**
|
|
36
|
+
* Object form of a handler.
|
|
37
|
+
*
|
|
38
|
+
* This makes it possible to attach middleware at the leaf of a middleware
|
|
39
|
+
* chain, which is useful for composing route handlers or feature modules.
|
|
40
|
+
*/
|
|
41
|
+
type ServerHandlerObject = {
|
|
42
|
+
/**
|
|
43
|
+
* Additional middleware to apply immediately before `handleRequest`.
|
|
44
|
+
*/
|
|
45
|
+
middleware?: ServerMiddleware[];
|
|
46
|
+
/**
|
|
47
|
+
* Final request handler invoked after middleware has run.
|
|
48
|
+
*/
|
|
49
|
+
handleRequest: ServerHandler;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Route-table shorthand keyed by pathname.
|
|
53
|
+
*
|
|
54
|
+
* Each path can resolve to a single handler, a handler object with per-route
|
|
55
|
+
* middleware, or a method map when the same path needs different handlers for
|
|
56
|
+
* different HTTP verbs.
|
|
57
|
+
*/
|
|
58
|
+
type ServerRoutes<TPath extends string = string> = { [Path in TPath]: ServerHandler | ServerHandlerObject | Partial<Record<HTTPMethod, ServerHandler | ServerHandlerObject>> };
|
|
59
|
+
/**
|
|
60
|
+
* A type-safe key for storing and retrieving values from
|
|
61
|
+
* {@link InvocationContext}.
|
|
62
|
+
*
|
|
63
|
+
* Keys are objects instead of strings so packages can create collision-free
|
|
64
|
+
* context channels without coordinating global names.
|
|
65
|
+
*/
|
|
66
|
+
interface InvocationContextKey<TValue> {
|
|
67
|
+
/**
|
|
68
|
+
* The default value for this key if no value has been set.
|
|
69
|
+
*/
|
|
70
|
+
defaultValue?: TValue;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Resolve the runtime value type associated with a context key.
|
|
74
|
+
*
|
|
75
|
+
* Keys created with `createContextKey()` resolve to their declared value type,
|
|
76
|
+
* while constructor keys resolve to the instance type they construct.
|
|
77
|
+
*/
|
|
78
|
+
type InvocationContextValue<TKey> = TKey extends InvocationContextKey<infer Value> ? Value : TKey extends (abstract new (...args: any[]) => infer Instance) ? Instance : never;
|
|
79
|
+
/**
|
|
80
|
+
* Create an invocation context key with an optional default value.
|
|
81
|
+
*
|
|
82
|
+
* When a default value is provided, `InvocationContext#get()` can always
|
|
83
|
+
* return a value for that key even if nothing has been explicitly written yet.
|
|
84
|
+
*
|
|
85
|
+
* @param defaultValue The default value for the context key
|
|
86
|
+
* @returns The new context key
|
|
87
|
+
*/
|
|
88
|
+
declare function createContextKey<TValue>(defaultValue?: TValue): InvocationContextKey<TValue>;
|
|
89
|
+
/**
|
|
90
|
+
* Function to register background work that should complete before the server
|
|
91
|
+
* shuts down. Mirrors the service-worker `waitUntil()` semantics.
|
|
92
|
+
*
|
|
93
|
+
* @param promise The background work to track
|
|
94
|
+
*/
|
|
95
|
+
type WaitUntilFunction = (promise: Promise<unknown> | PromiseLike<unknown>) => MaybePromise<void>;
|
|
96
|
+
/**
|
|
97
|
+
* Initialization options for creating an `InvocationContext`.
|
|
98
|
+
*/
|
|
99
|
+
type InvocationContextInit = {
|
|
100
|
+
/** The HTTP request being handled. */request: Request; /** Runtime capabilities for file system, compression, etc. */
|
|
101
|
+
capabilities: RuntimeCapabilities; /** Optional function to register background work for shutdown tracking. */
|
|
102
|
+
waitUntil?: WaitUntilFunction; /** Route parameters extracted from the matched route pattern. */
|
|
103
|
+
params?: Readonly<Record<string, string>>;
|
|
104
|
+
};
|
|
105
|
+
/**
|
|
106
|
+
* A context object that contains information about the current invocation.
|
|
107
|
+
* Each handler and middleware receives its own context instance, which may be
|
|
108
|
+
* derived from a parent context via `with()`.
|
|
109
|
+
*
|
|
110
|
+
* Context values are immutable - use `with()` to create a modified copy.
|
|
111
|
+
*/
|
|
112
|
+
declare class InvocationContext {
|
|
113
|
+
#private;
|
|
114
|
+
/**
|
|
115
|
+
* The original request that was dispatched to the router.
|
|
116
|
+
*
|
|
117
|
+
* Note: The request body may already have been consumed by middleware
|
|
118
|
+
* (available as `context.get(FormData)`). Use `context.with({ request })`
|
|
119
|
+
* if you need to pass a modified request downstream.
|
|
120
|
+
*/
|
|
121
|
+
readonly request: Request;
|
|
122
|
+
/**
|
|
123
|
+
* Cached parsed URL for middleware and handlers that need repeated URL access.
|
|
124
|
+
*/
|
|
125
|
+
url: URL | undefined;
|
|
126
|
+
/**
|
|
127
|
+
* Runtime specific invocation context.
|
|
128
|
+
*/
|
|
129
|
+
readonly capabilities: RuntimeCapabilities;
|
|
130
|
+
/**
|
|
131
|
+
* The current route parameters for this request.
|
|
132
|
+
*
|
|
133
|
+
* This reflects the active matched route and returns an empty object before
|
|
134
|
+
* routing has resolved a match.
|
|
135
|
+
*/
|
|
136
|
+
readonly params: Readonly<Record<string, string>>;
|
|
137
|
+
/**
|
|
138
|
+
* Tell the runtime about an ongoing operation that shouldn't close until the
|
|
139
|
+
* promise resolves.
|
|
140
|
+
*
|
|
141
|
+
* This mirrors service-worker-style `waitUntil()` semantics and is wired into
|
|
142
|
+
* `Server.close()`.
|
|
143
|
+
*/
|
|
144
|
+
readonly waitUntil: WaitUntilFunction | undefined;
|
|
145
|
+
constructor(init: InvocationContextInit);
|
|
146
|
+
/**
|
|
147
|
+
* Create a derived context with optional overrides.
|
|
148
|
+
* Copies all context values to the new instance.
|
|
149
|
+
*/
|
|
150
|
+
with(overrides: Partial<InvocationContextInit>): InvocationContext;
|
|
151
|
+
/**
|
|
152
|
+
* Get a value from invocation context.
|
|
153
|
+
*
|
|
154
|
+
* @param TKey The key to read
|
|
155
|
+
* @returns The value for the given key
|
|
156
|
+
*/
|
|
157
|
+
get<TKey extends object>(key: TKey): InvocationContextValue<TKey>;
|
|
158
|
+
/**
|
|
159
|
+
* Check whether a value exists in invocation context.
|
|
160
|
+
*
|
|
161
|
+
* @param TKey The key to check
|
|
162
|
+
* @returns `true` if a value has been set for the key
|
|
163
|
+
*/
|
|
164
|
+
has<TKey extends object>(key: TKey): boolean;
|
|
165
|
+
/**
|
|
166
|
+
* Set a value in invocation context.
|
|
167
|
+
*
|
|
168
|
+
* @param key The key to write
|
|
169
|
+
* @param value The value to write
|
|
170
|
+
*/
|
|
171
|
+
set<TKey extends object>(key: TKey, value: InvocationContextValue<TKey>): void;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Reject when the request abort signal fires before the promise settles.
|
|
175
|
+
*
|
|
176
|
+
* This keeps long-running async work aligned with fetch semantics: once the
|
|
177
|
+
* request has been aborted, downstream work should stop surfacing successful
|
|
178
|
+
* results for it.
|
|
179
|
+
*/
|
|
180
|
+
declare function raceRequestAbort<T>(promise: Promise<T>, request: Request): Promise<T>;
|
|
181
|
+
/**
|
|
182
|
+
* Internal helper used to track background tasks registered through
|
|
183
|
+
* `waitUntil()`.
|
|
184
|
+
*/
|
|
185
|
+
interface WaitUntil {
|
|
186
|
+
waitUntil(promise: Promise<any> | PromiseLike<any>): void;
|
|
187
|
+
wait(): Promise<any>;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Create a `waitUntil()` registry that keeps track of pending background work.
|
|
191
|
+
*
|
|
192
|
+
* Rejected tasks are logged and removed from the registry so shutdown can
|
|
193
|
+
* continue cleanly.
|
|
194
|
+
*/
|
|
195
|
+
declare function createWaitUntil(): WaitUntil;
|
|
196
|
+
/**
|
|
197
|
+
* Normalize handlers so middleware runners can treat function and object forms
|
|
198
|
+
* uniformly.
|
|
199
|
+
*
|
|
200
|
+
* A bare handler becomes `{ handleRequest }`, while object handlers are returned
|
|
201
|
+
* unchanged.
|
|
202
|
+
*/
|
|
203
|
+
declare function toServerHandlerObject(handler: ServerHandler | ServerHandlerObject): ServerHandlerObject;
|
|
204
|
+
/**
|
|
205
|
+
* Type guard to check if a value conforms to the `ServerHandlerObject` shape.
|
|
206
|
+
*
|
|
207
|
+
* This is used to distinguish between a bare `ServerHandler` function and an
|
|
208
|
+
* object wrapper that may also carry per-route middleware.
|
|
209
|
+
*/
|
|
210
|
+
declare function isServerHandlerObject(value: unknown): value is ServerHandlerObject;
|
|
211
|
+
/**
|
|
212
|
+
* Execute middleware in sequence and then hand off to the terminal handler.
|
|
213
|
+
*
|
|
214
|
+
* Middleware may:
|
|
215
|
+
* - return a `Response` to short-circuit downstream execution
|
|
216
|
+
* - call `next()` and return its result
|
|
217
|
+
* - call `next()` without returning it, in which case the downstream response
|
|
218
|
+
* is still used
|
|
219
|
+
*
|
|
220
|
+
* If the terminal handler is a `ServerHandlerObject`, its own `middleware`
|
|
221
|
+
* array is executed after the outer middleware chain completes.
|
|
222
|
+
*/
|
|
223
|
+
declare function runMiddleware(context: InvocationContext, middleware: ServerMiddleware[], terminal: ServerHandler | ServerHandlerObject): Promise<Response>;
|
|
224
|
+
type RouteInput = Request | URL | string | {
|
|
225
|
+
url: string | URL;
|
|
226
|
+
method?: HTTPMethod;
|
|
227
|
+
};
|
|
228
|
+
type RouteValue = ServerHandler | ServerHandlerObject | Partial<Record<HTTPMethod, ServerHandler | ServerHandlerObject>>;
|
|
229
|
+
type CompiledRoute = {
|
|
230
|
+
path: string;
|
|
231
|
+
route: RouteValue;
|
|
232
|
+
regexp: RegExp;
|
|
233
|
+
keys: string[];
|
|
234
|
+
};
|
|
235
|
+
/**
|
|
236
|
+
* Compiled route table grouped by precedence buckets.
|
|
237
|
+
*
|
|
238
|
+
* Matching order follows Bun-style routing precedence:
|
|
239
|
+
* exact -> param -> wildcard -> catch-all.
|
|
240
|
+
*/
|
|
241
|
+
type RouteTree = {
|
|
242
|
+
exact: Map<string, RouteValue>;
|
|
243
|
+
param: CompiledRoute[];
|
|
244
|
+
wildcard: CompiledRoute[];
|
|
245
|
+
catchAll?: CompiledRoute;
|
|
246
|
+
};
|
|
247
|
+
/**
|
|
248
|
+
* A pathname match before HTTP method filtering has been applied.
|
|
249
|
+
*/
|
|
250
|
+
type RouteCandidate = {
|
|
251
|
+
path: string;
|
|
252
|
+
route: RouteValue;
|
|
253
|
+
params: Record<string, string>;
|
|
254
|
+
};
|
|
255
|
+
/**
|
|
256
|
+
* A fully resolved route match including the selected handler for the request
|
|
257
|
+
* method.
|
|
258
|
+
*/
|
|
259
|
+
type UnstableRouteMatch = RouteCandidate & {
|
|
260
|
+
handler: ServerHandler | ServerHandlerObject;
|
|
261
|
+
method?: HTTPMethod;
|
|
262
|
+
};
|
|
263
|
+
/**
|
|
264
|
+
* Route lookup result containing all pathname candidates and the subset that
|
|
265
|
+
* also match the request method.
|
|
266
|
+
*/
|
|
267
|
+
type UnstableRouteMatchResult = {
|
|
268
|
+
all: RouteCandidate[];
|
|
269
|
+
matched: UnstableRouteMatch[];
|
|
270
|
+
};
|
|
271
|
+
/**
|
|
272
|
+
* Compile a declarative `ServerRoutes` table into precedence buckets for fast
|
|
273
|
+
* request matching.
|
|
274
|
+
*
|
|
275
|
+
* The build step also rejects conflicting route shapes such as duplicate exact
|
|
276
|
+
* routes or parameter routes that collapse to the same canonical pattern.
|
|
277
|
+
*/
|
|
278
|
+
declare function unstable_buildRouteTree(routes: ServerRoutes): RouteTree;
|
|
279
|
+
/**
|
|
280
|
+
* Match a request-like input against a compiled route tree.
|
|
281
|
+
*
|
|
282
|
+
* `all` contains every pathname candidate in precedence order, while
|
|
283
|
+
* `matched` further filters that list by HTTP method and resolves the concrete
|
|
284
|
+
* handler that would run.
|
|
285
|
+
*/
|
|
286
|
+
declare function unstable_match(tree: RouteTree, input: RouteInput): UnstableRouteMatchResult;
|
|
287
|
+
type UnstableConvertRoutesToHandlerOptions = {
|
|
288
|
+
input: RouteTree;
|
|
289
|
+
fallback?: ServerHandler;
|
|
290
|
+
runRouteMiddleware?: (context: InvocationContext, middleware: ServerMiddleware[], terminal: ServerHandlerObject) => Promise<Response>;
|
|
291
|
+
};
|
|
292
|
+
/**
|
|
293
|
+
* Turn a precompiled route tree into a `ServerHandler`.
|
|
294
|
+
*
|
|
295
|
+
* Pathname matches are resolved using Bun-style precedence. When a pathname
|
|
296
|
+
* matches but the method does not, the returned handler responds with `405`
|
|
297
|
+
* and an `Allow` header. If no route matches, `fallback` is used when
|
|
298
|
+
* provided; otherwise a `404` response is returned.
|
|
299
|
+
*/
|
|
300
|
+
declare function unstable_convertRoutesToHandler({
|
|
301
|
+
input,
|
|
302
|
+
fallback,
|
|
303
|
+
runRouteMiddleware
|
|
304
|
+
}: UnstableConvertRoutesToHandlerOptions): ServerHandler;
|
|
305
|
+
/**
|
|
306
|
+
* Host capabilities required by middleware and helpers that need filesystem or
|
|
307
|
+
* compression support.
|
|
308
|
+
*/
|
|
309
|
+
interface RuntimeCapabilities {
|
|
310
|
+
/**
|
|
311
|
+
* Resolve a path relative to `root`.
|
|
312
|
+
*
|
|
313
|
+
* Returns `null` when the resolved path would escape the root directory.
|
|
314
|
+
*/
|
|
315
|
+
resolve(root: string, ...components: string[]): Promise<string | null>;
|
|
316
|
+
/**
|
|
317
|
+
* Open a path for static file serving.
|
|
318
|
+
*
|
|
319
|
+
* Returns `null` when the path does not exist.
|
|
320
|
+
*/
|
|
321
|
+
open(path: string): Promise<{
|
|
322
|
+
readonly isFile: false;
|
|
323
|
+
} | {
|
|
324
|
+
readonly isFile: true;
|
|
325
|
+
readonly size: number;
|
|
326
|
+
stream: (start?: number, end?: number) => ReadableStream;
|
|
327
|
+
} | null>;
|
|
328
|
+
/**
|
|
329
|
+
* Create a gzip compression transform.
|
|
330
|
+
*/
|
|
331
|
+
createGzip(): MaybePromise<TransformStream>;
|
|
332
|
+
/**
|
|
333
|
+
* Create a Brotli compression transform.
|
|
334
|
+
*/
|
|
335
|
+
createBrotliCompress(): MaybePromise<TransformStream>;
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Lifecycle hooks required to host a `Server` in a specific runtime.
|
|
339
|
+
*
|
|
340
|
+
* `setup()` prepares runtime-specific state, `serve()` starts listening,
|
|
341
|
+
* and `close()` shuts the runtime down.
|
|
342
|
+
*/
|
|
343
|
+
interface RuntimeAdapter {
|
|
344
|
+
/**
|
|
345
|
+
* Runtime-specific helpers exposed to middleware and request handlers.
|
|
346
|
+
*/
|
|
347
|
+
readonly capabilities: RuntimeCapabilities;
|
|
348
|
+
/**
|
|
349
|
+
* Whether the adapter/runtime should be treated as supporting graceful
|
|
350
|
+
* process shutdown integration.
|
|
351
|
+
*/
|
|
352
|
+
readonly graceful?: boolean;
|
|
353
|
+
/**
|
|
354
|
+
* Prepare runtime-specific state before the server starts listening.
|
|
355
|
+
*/
|
|
356
|
+
setup: (server: Server) => void;
|
|
357
|
+
/**
|
|
358
|
+
* Start serving requests and resolve once the adapter knows the public URL.
|
|
359
|
+
*/
|
|
360
|
+
serve: (server: Server) => Promise<{
|
|
361
|
+
url: string | undefined;
|
|
362
|
+
} | undefined>;
|
|
363
|
+
/**
|
|
364
|
+
* Stop accepting new work and optionally terminate active connections.
|
|
365
|
+
*/
|
|
366
|
+
close: (closeActiveConnections: boolean) => Promise<void>;
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Bun's native server options, excluding the fetch handler owned by `Server`.
|
|
370
|
+
*/
|
|
371
|
+
type BunServerOptions = Omit<Bun.Serve.Options<any>, "fetch" | "routes" | "unix">;
|
|
372
|
+
/**
|
|
373
|
+
* Deno's native serve options.
|
|
374
|
+
*/
|
|
375
|
+
type DenoServerOptions = Deno.ServeOptions;
|
|
376
|
+
/**
|
|
377
|
+
* Node.js server options.
|
|
378
|
+
*/
|
|
379
|
+
type NodeServerOptions = (nodeHTTP.ServerOptions | nodeHTTPS.ServerOptions | nodeHTTP2.ServerOptions) & NodeNet.ListenOptions & {
|
|
380
|
+
http2?: boolean;
|
|
381
|
+
};
|
|
382
|
+
/**
|
|
383
|
+
* Resolve the runtime adapter for the current process.
|
|
384
|
+
*
|
|
385
|
+
* The adapter is chosen lazily so the package can stay portable across Bun,
|
|
386
|
+
* Deno, and Node without importing runtime-specific code up front.
|
|
387
|
+
*/
|
|
388
|
+
declare function loadServerAdapter(): Promise<RuntimeAdapter>;
|
|
389
|
+
/**
|
|
390
|
+
* Emitted after the server starts accepting requests.
|
|
391
|
+
*/
|
|
392
|
+
declare class ServerServeEvent extends Event {
|
|
393
|
+
constructor();
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* Emitted after the server has fully closed.
|
|
397
|
+
*/
|
|
398
|
+
declare class ServerCloseEvent extends Event {
|
|
399
|
+
constructor();
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Emitted when server startup or runtime handling raises an error.
|
|
403
|
+
*/
|
|
404
|
+
declare class ServerErrorEvent extends Event {
|
|
405
|
+
readonly error: any;
|
|
406
|
+
constructor(error?: any);
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Extension point that can mutate a `Server` instance during construction.
|
|
410
|
+
*/
|
|
411
|
+
type ServerPlugin = (server: Server) => void;
|
|
412
|
+
/**
|
|
413
|
+
* TLS server options.
|
|
414
|
+
*
|
|
415
|
+
* These are normalized by individual runtime adapters before being handed to
|
|
416
|
+
* Bun, Deno, or Node.
|
|
417
|
+
*/
|
|
418
|
+
type TLSOptions = {
|
|
419
|
+
/**
|
|
420
|
+
* File path or inlined TLS certificate in PEM format (required).
|
|
421
|
+
*/
|
|
422
|
+
cert?: string;
|
|
423
|
+
/**
|
|
424
|
+
* File path or inlined TLS private key in PEM format (required).
|
|
425
|
+
*/
|
|
426
|
+
key?: string;
|
|
427
|
+
/**
|
|
428
|
+
* Passphrase for the private key (optional).
|
|
429
|
+
*/
|
|
430
|
+
passphrase?: string;
|
|
431
|
+
};
|
|
432
|
+
/**
|
|
433
|
+
* Core server configuration shared by all runtime adapters.
|
|
434
|
+
*
|
|
435
|
+
* These options describe request handling behavior, listener defaults, and
|
|
436
|
+
* process-level features such as graceful shutdown.
|
|
437
|
+
*/
|
|
438
|
+
interface ServerOptions {
|
|
439
|
+
/**
|
|
440
|
+
* Declarative route table matched before the fallback `fetch` handler.
|
|
441
|
+
*
|
|
442
|
+
* If this table does not define `/*`, `fetch` must be provided to handle
|
|
443
|
+
* unmatched requests.
|
|
444
|
+
*/
|
|
445
|
+
routes?: ServerRoutes;
|
|
446
|
+
/**
|
|
447
|
+
* Fallback request handler.
|
|
448
|
+
*
|
|
449
|
+
* When `routes` is provided, this handles requests that do not match the
|
|
450
|
+
* route table. When `routes` is omitted, this acts as the primary request
|
|
451
|
+
* handler.
|
|
452
|
+
*/
|
|
453
|
+
fetch?: ServerHandler;
|
|
454
|
+
/**
|
|
455
|
+
* Handle errors raised while processing requests.
|
|
456
|
+
*
|
|
457
|
+
* @note This handler will set built-in Bun and Deno error handler.
|
|
458
|
+
*/
|
|
459
|
+
error?: ErrorHandler;
|
|
460
|
+
/**
|
|
461
|
+
* Server middleware handlers to run before the main fetch handler.
|
|
462
|
+
*
|
|
463
|
+
* Middleware is executed in the order provided.
|
|
464
|
+
*/
|
|
465
|
+
middleware: ServerMiddleware[];
|
|
466
|
+
/**
|
|
467
|
+
* If set to `true`, server will not start listening automatically.
|
|
468
|
+
*/
|
|
469
|
+
manual?: boolean;
|
|
470
|
+
/**
|
|
471
|
+
* The port server should be listening to.
|
|
472
|
+
*
|
|
473
|
+
* Default is read from `PORT` environment variable or will be `3000`.
|
|
474
|
+
*
|
|
475
|
+
* **Tip:** You can set the port to `0` to use a random port.
|
|
476
|
+
*/
|
|
477
|
+
port?: string | number;
|
|
478
|
+
/**
|
|
479
|
+
* The hostname (IP or resolvable host) server listener should bound to.
|
|
480
|
+
*
|
|
481
|
+
* When not provided, server with listen to all network interfaces by default.
|
|
482
|
+
*
|
|
483
|
+
* **Important:** If you are running a server that is not expected to be exposed to the network, use `hostname: "localhost"`.
|
|
484
|
+
*/
|
|
485
|
+
hostname?: string;
|
|
486
|
+
/**
|
|
487
|
+
* Enabling this option allows multiple processes to bind to the same port, which is useful for load balancing.
|
|
488
|
+
*
|
|
489
|
+
* **Note:** Despite Node.js built-in behavior that has `exclusive` flag (opposite of `reusePort`) enabled by default, sevok uses non-exclusive mode for consistency.
|
|
490
|
+
*/
|
|
491
|
+
reusePort?: boolean;
|
|
492
|
+
/**
|
|
493
|
+
* The protocol to use for the server.
|
|
494
|
+
*
|
|
495
|
+
* Possible values are `http` and `https`.
|
|
496
|
+
*
|
|
497
|
+
* If `protocol` is not set, Server will use `http` as the default protocol or `https` if both `tls.cert` and `tls.key` options are provided.
|
|
498
|
+
*/
|
|
499
|
+
protocol?: "http" | "https";
|
|
500
|
+
/**
|
|
501
|
+
* TLS server options.
|
|
502
|
+
*/
|
|
503
|
+
tls?: TLSOptions;
|
|
504
|
+
/**
|
|
505
|
+
* If set to `true`, server will not print the listening address.
|
|
506
|
+
*/
|
|
507
|
+
silent?: boolean;
|
|
508
|
+
/**
|
|
509
|
+
* Graceful shutdown on SIGINT and SIGTERM signals.
|
|
510
|
+
*
|
|
511
|
+
* Supported for Node.js, Deno and Bun runtimes.
|
|
512
|
+
*
|
|
513
|
+
* @default true (disabled in test and ci environments)
|
|
514
|
+
*/
|
|
515
|
+
gracefulShutdown?: boolean | {
|
|
516
|
+
gracefulTimeout?: number;
|
|
517
|
+
forceTimeout?: number;
|
|
518
|
+
};
|
|
519
|
+
/**
|
|
520
|
+
* Deno-specific adapter options.
|
|
521
|
+
*/
|
|
522
|
+
deno?: DenoServerOptions;
|
|
523
|
+
/**
|
|
524
|
+
* Bun-specific adapter options.
|
|
525
|
+
*/
|
|
526
|
+
bun?: BunServerOptions;
|
|
527
|
+
/**
|
|
528
|
+
* Node.js-specific adapter options.
|
|
529
|
+
*/
|
|
530
|
+
node?: NodeServerOptions;
|
|
531
|
+
}
|
|
532
|
+
/**
|
|
533
|
+
* Constructor input for creating a `Server`.
|
|
534
|
+
*
|
|
535
|
+
* Extends the base server options with the runtime adapter and optional
|
|
536
|
+
* plugins that can mutate server behavior before the adapter is set up.
|
|
537
|
+
*/
|
|
538
|
+
interface ServerInit extends Omit<ServerOptions, "middleware"> {
|
|
539
|
+
/**
|
|
540
|
+
* Server plugins.
|
|
541
|
+
*
|
|
542
|
+
* Plugins run before the runtime adapter is set up, so they can adjust
|
|
543
|
+
* `server.options` or register server-level behavior.
|
|
544
|
+
*/
|
|
545
|
+
plugins?: ServerPlugin[];
|
|
546
|
+
/**
|
|
547
|
+
* Global middleware applied before route-level or handler-level middleware.
|
|
548
|
+
*/
|
|
549
|
+
middleware?: ServerMiddleware[];
|
|
550
|
+
/**
|
|
551
|
+
* Runtime adapter responsible for integrating with the host environment.
|
|
552
|
+
*/
|
|
553
|
+
adapter?: RuntimeAdapter;
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* Convenience factory for creating a `Server` without `new`.
|
|
557
|
+
*/
|
|
558
|
+
declare function serve(init: ServerInit): Server;
|
|
559
|
+
/**
|
|
560
|
+
* Augmentation hook for adding application-specific `Server` events.
|
|
561
|
+
*
|
|
562
|
+
* Consumers can extend this interface with module augmentation so custom event
|
|
563
|
+
* names become part of `ServerEventMap`.
|
|
564
|
+
*
|
|
565
|
+
* @example
|
|
566
|
+
* ```ts
|
|
567
|
+
* declare module "sevok" {
|
|
568
|
+
* interface ServerEventMapCustom {
|
|
569
|
+
* reload: CustomEvent<{ full: boolean }>;
|
|
570
|
+
* }
|
|
571
|
+
* }
|
|
572
|
+
* ```
|
|
573
|
+
*/
|
|
574
|
+
interface ServerEventMapCustom {}
|
|
575
|
+
/**
|
|
576
|
+
* Complete typed event map for `Server`.
|
|
577
|
+
*
|
|
578
|
+
* Combines built-in lifecycle events with any consumer-defined events added
|
|
579
|
+
* through `ServerEventMapCustom` augmentation.
|
|
580
|
+
*/
|
|
581
|
+
interface ServerEventMap extends ServerEventMapCustom {
|
|
582
|
+
/** Fired after the runtime adapter reports that the server is serving. */
|
|
583
|
+
serve: ServerServeEvent;
|
|
584
|
+
/** Fired after shutdown has completed and background work has settled. */
|
|
585
|
+
close: ServerCloseEvent;
|
|
586
|
+
/** Fired when adapter initialization fails with a non-abort error. */
|
|
587
|
+
error: ServerErrorEvent;
|
|
588
|
+
}
|
|
589
|
+
/**
|
|
590
|
+
* Runtime-agnostic fetch server.
|
|
591
|
+
*
|
|
592
|
+
* `Server` owns middleware composition, per-invocation context setup, background
|
|
593
|
+
* task tracking, and runtime lifecycle coordination. Concrete adapters handle
|
|
594
|
+
* the host-specific details of listening for requests.
|
|
595
|
+
*/
|
|
596
|
+
declare class Server extends EventDispatcher<ServerEventMap> {
|
|
597
|
+
#private;
|
|
598
|
+
/**
|
|
599
|
+
* Server options.
|
|
600
|
+
*/
|
|
601
|
+
readonly options: ServerOptions;
|
|
602
|
+
/**
|
|
603
|
+
* Register a background task that the server should await before closing.
|
|
604
|
+
*
|
|
605
|
+
* Same as `request.waitUntil` but available at the server level for use
|
|
606
|
+
* outside of request handlers.
|
|
607
|
+
*/
|
|
608
|
+
readonly waitUntil?: (promise: Promise<unknown>) => void;
|
|
609
|
+
/**
|
|
610
|
+
* Create a server, apply plugins, prepare the runtime adapter, and optionally
|
|
611
|
+
* start listening immediately.
|
|
612
|
+
*/
|
|
613
|
+
constructor({
|
|
614
|
+
middleware,
|
|
615
|
+
plugins,
|
|
616
|
+
adapter,
|
|
617
|
+
...options
|
|
618
|
+
}: ServerInit);
|
|
619
|
+
/**
|
|
620
|
+
* Listener URL reported by the runtime adapter after `serve()` succeeds.
|
|
621
|
+
*/
|
|
622
|
+
get url(): string | undefined;
|
|
623
|
+
/**
|
|
624
|
+
* Create a `InvocationContext` view over an arbitrary `Request`.
|
|
625
|
+
*
|
|
626
|
+
* This preserves the upstream request object and pairs it with sevok
|
|
627
|
+
* invocation state such as runtime capabilities, `waitUntil`, and `params`.
|
|
628
|
+
*/
|
|
629
|
+
createContext(request: Request, params?: Readonly<Record<string, string>>): InvocationContext;
|
|
630
|
+
/**
|
|
631
|
+
* Invoke the composed request pipeline directly.
|
|
632
|
+
*/
|
|
633
|
+
handle(context: InvocationContext): MaybePromise<Response>;
|
|
634
|
+
/**
|
|
635
|
+
* Adapt an arbitrary `Request` into an `InvocationContext` and invoke the handler pipeline.
|
|
636
|
+
*/
|
|
637
|
+
fetch(request: Request): Promise<Response>;
|
|
638
|
+
/**
|
|
639
|
+
* Start the runtime adapter if it has not been started already.
|
|
640
|
+
*
|
|
641
|
+
* The returned promise resolves once the adapter reports the final listener
|
|
642
|
+
* URL. Repeated calls reuse the same in-flight startup.
|
|
643
|
+
*/
|
|
644
|
+
serve(): Promise<void>;
|
|
645
|
+
/**
|
|
646
|
+
* Wait until the server has completed startup and then return the instance.
|
|
647
|
+
*
|
|
648
|
+
* This is mainly a convenience for fluent startup code.
|
|
649
|
+
*/
|
|
650
|
+
ready(): Promise<Server>;
|
|
651
|
+
/**
|
|
652
|
+
* Close the runtime adapter and wait for outstanding `waitUntil()` tasks.
|
|
653
|
+
*
|
|
654
|
+
* If startup was still in progress, `ready()` is rejected with an
|
|
655
|
+
* `AbortError`.
|
|
656
|
+
*/
|
|
657
|
+
close(closeActiveConnections?: boolean): Promise<void>;
|
|
658
|
+
}
|
|
659
|
+
/**
|
|
660
|
+
* Compose fetch handler, middleware, and optional error handling into the
|
|
661
|
+
* single request kernel used by `Server`.
|
|
662
|
+
*/
|
|
663
|
+
declare function wrapFetch(server: Server): ServerHandler;
|
|
664
|
+
//#endregion
|
|
665
|
+
export { WaitUntil as A, unstable_buildRouteTree as B, ServerPlugin as C, UnstableConvertRoutesToHandlerOptions as D, TLSOptions as E, loadServerAdapter as F, unstable_match as H, raceRequestAbort as I, runMiddleware as L, createContextKey as M, createWaitUntil as N, UnstableRouteMatch as O, isServerHandlerObject as P, serve as R, ServerOptions as S, ServerServeEvent as T, wrapFetch as U, unstable_convertRoutesToHandler as V, ServerEventMapCustom as _, InvocationContext as a, ServerInit as b, InvocationContextValue as c, RuntimeAdapter as d, RuntimeCapabilities as f, ServerEventMap as g, ServerErrorEvent as h, HTTPMethod as i, WaitUntilFunction as j, UnstableRouteMatchResult as k, MaybePromise as l, ServerCloseEvent as m, DenoServerOptions as n, InvocationContextInit as o, Server as p, ErrorHandler as r, InvocationContextKey as s, BunServerOptions as t, NodeServerOptions as u, ServerHandler as v, ServerRoutes as w, ServerMiddleware as x, ServerHandlerObject as y, toServerHandlerObject as z };
|