@oxog/spark 1.1.1 → 1.2.0
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/CHANGELOG.md +632 -315
- package/LICENSE +20 -20
- package/README.md +288 -241
- package/package.json +83 -89
- package/src/core/application.js +1068 -956
- package/src/core/context.js +1098 -830
- package/src/core/middleware.js +303 -217
- package/src/index.js +183 -280
- package/src/middleware/body-parser.js +482 -526
- package/src/middleware/cache.js +177 -170
- package/src/middleware/compression.js +177 -182
- package/src/middleware/cors.js +176 -95
- package/src/middleware/health.js +219 -218
- package/src/middleware/logger.js +195 -194
- package/src/middleware/metrics.js +339 -327
- package/src/middleware/rate-limit.js +367 -319
- package/src/middleware/security.js +442 -332
- package/src/middleware/session.js +647 -529
- package/src/middleware/static.js +404 -299
- package/src/router/layer.js +197 -117
- package/src/router/route.js +149 -123
- package/src/router/router.js +703 -755
- package/src/utils/async-handler.js +145 -147
- package/src/utils/context-pool.js +83 -82
- package/src/utils/regex-validator.js +187 -163
- package/src/utils/safe-json.js +110 -0
- package/types/index.d.ts +536 -291
- package/dist/LICENSE +0 -21
- package/dist/README.md +0 -242
- package/dist/core/application.js +0 -959
- package/dist/core/context.js +0 -833
- package/dist/core/middleware.js +0 -220
- package/dist/core/request.js +0 -335
- package/dist/core/response.js +0 -377
- package/dist/index.js +0 -280
- package/dist/middleware/body-parser.js +0 -528
- package/dist/middleware/cache.js +0 -173
- package/dist/middleware/compression.js +0 -185
- package/dist/middleware/cors.js +0 -97
- package/dist/middleware/health.js +0 -221
- package/dist/middleware/index.js +0 -23
- package/dist/middleware/logger.js +0 -197
- package/dist/middleware/metrics.js +0 -330
- package/dist/middleware/rate-limit.js +0 -322
- package/dist/middleware/security.js +0 -335
- package/dist/middleware/session.js +0 -532
- package/dist/middleware/static.js +0 -301
- package/dist/package.json +0 -62
- package/dist/router/layer.js +0 -119
- package/dist/router/route.js +0 -125
- package/dist/router/router.js +0 -758
- package/dist/types/index.d.ts +0 -291
- package/dist/utils/async-handler.js +0 -150
- package/dist/utils/cache.js +0 -443
- package/dist/utils/context-pool.js +0 -85
- package/dist/utils/error-types.js +0 -134
- package/dist/utils/http.js +0 -415
- package/dist/utils/regex-validator.js +0 -166
- package/src/core/request.js +0 -335
- package/src/core/response.js +0 -377
- package/src/middleware/index.js +0 -23
- package/src/utils/cache.js +0 -443
- package/src/utils/error-types.js +0 -134
- package/src/utils/http.js +0 -415
package/types/index.d.ts
CHANGED
|
@@ -1,291 +1,536 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
1
|
+
// Type definitions for @oxog/spark.
|
|
2
|
+
// The runtime is a single CommonJS module that also serves ESM `import`.
|
|
3
|
+
// Node's own types are not required: request/response objects are typed
|
|
4
|
+
// structurally below.
|
|
5
|
+
|
|
6
|
+
/** Incoming HTTP request (structural subset of http.IncomingMessage). */
|
|
7
|
+
export interface IncomingMessage {
|
|
8
|
+
url?: string;
|
|
9
|
+
method?: string;
|
|
10
|
+
httpVersion?: string;
|
|
11
|
+
headers: Record<string, string | string[] | undefined>;
|
|
12
|
+
[key: string]: any;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** HTTP response (structural subset of http.ServerResponse). */
|
|
16
|
+
export interface ServerResponse {
|
|
17
|
+
statusCode: number;
|
|
18
|
+
headersSent: boolean;
|
|
19
|
+
setHeader(name: string, value: number | string | readonly string[]): any;
|
|
20
|
+
getHeader(name: string): number | string | string[] | undefined;
|
|
21
|
+
end(chunk?: any): any;
|
|
22
|
+
[key: string]: any;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** HTTP server (structural subset of http.Server). */
|
|
26
|
+
export interface Server {
|
|
27
|
+
listening: boolean;
|
|
28
|
+
close(callback?: (error?: Error) => void): any;
|
|
29
|
+
address(): { address: string; family: string; port: number } | string | null;
|
|
30
|
+
[key: string]: any;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Parsed request URL (a WHATWG URL instance). */
|
|
34
|
+
export interface RequestURL {
|
|
35
|
+
href: string;
|
|
36
|
+
origin: string;
|
|
37
|
+
protocol: string;
|
|
38
|
+
host: string;
|
|
39
|
+
hostname: string;
|
|
40
|
+
port: string;
|
|
41
|
+
pathname: string;
|
|
42
|
+
search: string;
|
|
43
|
+
hash: string;
|
|
44
|
+
searchParams: { get(name: string): string | null; getAll(name: string): string[]; has(name: string): boolean };
|
|
45
|
+
toString(): string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** A readable stream that can be piped to the response. */
|
|
49
|
+
export interface ReadableLike {
|
|
50
|
+
pipe(destination: any, options?: { end?: boolean }): any;
|
|
51
|
+
destroy(error?: Error): any;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface AppSettings {
|
|
55
|
+
/** Trust X-Forwarded-For / X-Real-IP in ctx.ip() and ctx.ips(). */
|
|
56
|
+
trustProxy?: boolean;
|
|
57
|
+
/** Hosts ctx.redirect() may send clients to (subdomains included). */
|
|
58
|
+
allowedRedirectDomains?: Array<string | RegExp>;
|
|
59
|
+
/** Allow redirects to any external host (not recommended). */
|
|
60
|
+
allowOpenRedirects?: boolean;
|
|
61
|
+
[key: string]: unknown;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface AppOptions {
|
|
65
|
+
port?: number | string;
|
|
66
|
+
host?: string;
|
|
67
|
+
cluster?: boolean;
|
|
68
|
+
https?: Record<string, unknown>;
|
|
69
|
+
settings?: AppSettings;
|
|
70
|
+
/** Milliseconds close() waits for in-flight requests (default 10000). */
|
|
71
|
+
shutdownTimeout?: number;
|
|
72
|
+
/** Close gracefully on SIGTERM/SIGINT while listening (default true). */
|
|
73
|
+
handleSignals?: boolean;
|
|
74
|
+
/** Exit (code 1) on uncaught exceptions while listening (default true). */
|
|
75
|
+
exitOnUncaughtException?: boolean;
|
|
76
|
+
/** Exit (code 1) on unhandled rejections while listening (default false). */
|
|
77
|
+
exitOnUnhandledRejection?: boolean;
|
|
78
|
+
[key: string]: unknown;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export interface CookieOptions {
|
|
82
|
+
maxAge?: number;
|
|
83
|
+
expires?: Date;
|
|
84
|
+
path?: string;
|
|
85
|
+
domain?: string;
|
|
86
|
+
secure?: boolean;
|
|
87
|
+
httpOnly?: boolean;
|
|
88
|
+
sameSite?: 'strict' | 'lax' | 'none' | 'Strict' | 'Lax' | 'None';
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export interface UploadedFile {
|
|
92
|
+
filename: string;
|
|
93
|
+
contentType: string;
|
|
94
|
+
size: number;
|
|
95
|
+
data: Uint8Array;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export interface SessionData {
|
|
99
|
+
readonly id: string;
|
|
100
|
+
readonly isNew: boolean;
|
|
101
|
+
readonly destroyed: boolean | undefined;
|
|
102
|
+
modified: boolean | undefined;
|
|
103
|
+
destroy(): Promise<void>;
|
|
104
|
+
regenerate(): Promise<void>;
|
|
105
|
+
reload(): Promise<void>;
|
|
106
|
+
save(): Promise<void>;
|
|
107
|
+
touch(): Promise<void>;
|
|
108
|
+
[key: string]: any;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export declare class Context<TState = any> {
|
|
112
|
+
constructor(req: IncomingMessage, res: ServerResponse, app?: Spark);
|
|
113
|
+
req: IncomingMessage;
|
|
114
|
+
res: ServerResponse;
|
|
115
|
+
app: Spark;
|
|
116
|
+
method: string;
|
|
117
|
+
url: RequestURL | null;
|
|
118
|
+
originalUrl: string;
|
|
119
|
+
path: string;
|
|
120
|
+
mountpath?: string;
|
|
121
|
+
query: Record<string, string | string[]>;
|
|
122
|
+
params: Record<string, string>;
|
|
123
|
+
headers: Record<string, string | string[] | undefined>;
|
|
124
|
+
responseHeaders: Record<string, string | string[]>;
|
|
125
|
+
cookies: Record<string, string | true>;
|
|
126
|
+
/** Parsed request body (set by bodyParser). */
|
|
127
|
+
body: any;
|
|
128
|
+
files: Record<string, UploadedFile> | null;
|
|
129
|
+
session: SessionData | null;
|
|
130
|
+
state: TState;
|
|
131
|
+
responded: boolean;
|
|
132
|
+
statusCode: number;
|
|
133
|
+
/** Set by the csrf() middleware. */
|
|
134
|
+
csrfToken?: () => string;
|
|
135
|
+
[key: string]: any;
|
|
136
|
+
|
|
137
|
+
/** Read a request header (case-insensitive). */
|
|
138
|
+
get(headerName: string): string | undefined;
|
|
139
|
+
/** Set a response header. Rejects CR/LF/NUL in values. */
|
|
140
|
+
set(headerName: string, value: string | number): this;
|
|
141
|
+
setHeader(name: string, value: string | number): this;
|
|
142
|
+
getHeader(name: string): string | string[] | undefined;
|
|
143
|
+
removeHeader(name: string): this;
|
|
144
|
+
status(code: number | string): this;
|
|
145
|
+
redirect(url: string, status?: number): this;
|
|
146
|
+
json(data: unknown): this;
|
|
147
|
+
text(data: string): this;
|
|
148
|
+
html(data: string): this;
|
|
149
|
+
send(data?: unknown): this;
|
|
150
|
+
end(data?: unknown): this;
|
|
151
|
+
/** Pipe a readable stream as the response body (with backpressure). */
|
|
152
|
+
stream(readable: ReadableLike): Promise<void>;
|
|
153
|
+
setCookie(name: string, value: string | number | boolean | null | undefined, options?: CookieOptions): this;
|
|
154
|
+
clearCookie(name: string, options?: CookieOptions): this;
|
|
155
|
+
is(type: string): boolean;
|
|
156
|
+
accepts(types: string | string[]): string | false;
|
|
157
|
+
fresh(): boolean;
|
|
158
|
+
stale(): boolean;
|
|
159
|
+
secure(): boolean;
|
|
160
|
+
xhr(): boolean;
|
|
161
|
+
ip(): string;
|
|
162
|
+
ips(): string[];
|
|
163
|
+
protocol(): 'http' | 'https';
|
|
164
|
+
host(): string;
|
|
165
|
+
hostname(): string;
|
|
166
|
+
port(): number;
|
|
167
|
+
subdomains(): string[];
|
|
168
|
+
type(): string;
|
|
169
|
+
charset(): string;
|
|
170
|
+
length(): number;
|
|
171
|
+
toJSON(): Record<string, unknown>;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export type Next = (error?: unknown) => Promise<void>;
|
|
175
|
+
|
|
176
|
+
/** Middleware or route handler. The return value is ignored (it may be awaited). */
|
|
177
|
+
export type Middleware<TState = any> = (ctx: Context<TState>, next: Next) => unknown;
|
|
178
|
+
|
|
179
|
+
export interface RouterOptions {
|
|
180
|
+
caseSensitive?: boolean;
|
|
181
|
+
/** Alias of caseSensitive. */
|
|
182
|
+
sensitive?: boolean;
|
|
183
|
+
/** Trailing slash is significant. */
|
|
184
|
+
strict?: boolean;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export declare class Route {
|
|
188
|
+
constructor(path: string);
|
|
189
|
+
path: string;
|
|
190
|
+
get(...handlers: Middleware[]): this;
|
|
191
|
+
post(...handlers: Middleware[]): this;
|
|
192
|
+
put(...handlers: Middleware[]): this;
|
|
193
|
+
delete(...handlers: Middleware[]): this;
|
|
194
|
+
patch(...handlers: Middleware[]): this;
|
|
195
|
+
head(...handlers: Middleware[]): this;
|
|
196
|
+
options(...handlers: Middleware[]): this;
|
|
197
|
+
all(...handlers: Middleware[]): this;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
type RouteRegistrar<T> = (path: string | RegExp | Array<string | RegExp>, ...handlers: Middleware[]) => T;
|
|
201
|
+
|
|
202
|
+
export declare class Router {
|
|
203
|
+
constructor(options?: RouterOptions);
|
|
204
|
+
/** Router configuration; also callable to register OPTIONS routes. */
|
|
205
|
+
options: RouterOptions & RouteRegistrar<Router>;
|
|
206
|
+
use(...middleware: Array<Middleware | Router>): this;
|
|
207
|
+
use(path: string, ...middleware: Array<Middleware | Router>): this;
|
|
208
|
+
get: RouteRegistrar<this>;
|
|
209
|
+
post: RouteRegistrar<this>;
|
|
210
|
+
put: RouteRegistrar<this>;
|
|
211
|
+
patch: RouteRegistrar<this>;
|
|
212
|
+
delete: RouteRegistrar<this>;
|
|
213
|
+
head: RouteRegistrar<this>;
|
|
214
|
+
all: RouteRegistrar<this>;
|
|
215
|
+
route(path: string, method: string | null, ...handlers: Middleware[]): Route;
|
|
216
|
+
param(name: string, handler: (ctx: Context, next: () => void, value: string, name: string) => unknown): this;
|
|
217
|
+
group(prefix: string, fn: (router: Router) => void): this;
|
|
218
|
+
version(version: string | number, fn: (router: Router) => void): this;
|
|
219
|
+
mount(path: string, router: Router): this;
|
|
220
|
+
routes(): Middleware;
|
|
221
|
+
middleware(prefix?: string): Middleware;
|
|
222
|
+
handle(ctx: Context, next: Next): Promise<void>;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export declare class Spark {
|
|
226
|
+
constructor(options?: AppOptions);
|
|
227
|
+
/** Application configuration; also callable to register OPTIONS routes. */
|
|
228
|
+
options: AppOptions & RouteRegistrar<Spark>;
|
|
229
|
+
settings: AppSettings;
|
|
230
|
+
router: Router;
|
|
231
|
+
server: Server | null;
|
|
232
|
+
listening: boolean;
|
|
233
|
+
|
|
234
|
+
use(middleware: Middleware | Router): this;
|
|
235
|
+
use(path: string, middleware: Middleware | Router): this;
|
|
236
|
+
get: RouteRegistrar<this>;
|
|
237
|
+
post: RouteRegistrar<this>;
|
|
238
|
+
put: RouteRegistrar<this>;
|
|
239
|
+
patch: RouteRegistrar<this>;
|
|
240
|
+
delete: RouteRegistrar<this>;
|
|
241
|
+
head: RouteRegistrar<this>;
|
|
242
|
+
all: RouteRegistrar<this>;
|
|
243
|
+
|
|
244
|
+
/** Start listening; the callback runs once the server is bound. */
|
|
245
|
+
listen(callback?: () => void): this;
|
|
246
|
+
listen(port: number | string, callback?: () => void): this;
|
|
247
|
+
listen(port: number | string, host: string, callback?: () => void): this;
|
|
248
|
+
/** Stop accepting connections and resolve once the server is closed. */
|
|
249
|
+
close(): Promise<void>;
|
|
250
|
+
handleError(error: unknown, ctx: Context): Promise<void>;
|
|
251
|
+
onShutdown(handler: () => unknown): this;
|
|
252
|
+
gracefulShutdown(exitCode?: number): Promise<void>;
|
|
253
|
+
|
|
254
|
+
on(event: 'error', listener: (error: Error, ctx?: Context) => void): this;
|
|
255
|
+
on(event: 'listening' | 'close', listener: () => void): this;
|
|
256
|
+
on(event: string, listener: (...args: any[]) => void): this;
|
|
257
|
+
once(event: string, listener: (...args: any[]) => void): this;
|
|
258
|
+
off(event: string, listener: (...args: any[]) => void): this;
|
|
259
|
+
emit(event: string, ...args: any[]): boolean;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** @deprecated Use Spark. */
|
|
263
|
+
export declare const App: typeof Spark;
|
|
264
|
+
export declare const Application: typeof Spark;
|
|
265
|
+
|
|
266
|
+
// Middleware option types
|
|
267
|
+
|
|
268
|
+
export interface CorsOptions {
|
|
269
|
+
origin?: boolean | string | Array<string | RegExp> | ((ctx: Context) => string | boolean | Promise<string | boolean>);
|
|
270
|
+
credentials?: boolean;
|
|
271
|
+
methods?: string[];
|
|
272
|
+
allowedHeaders?: string[] | string | ((ctx: Context) => string[] | string);
|
|
273
|
+
exposedHeaders?: string[];
|
|
274
|
+
maxAge?: number;
|
|
275
|
+
preflightContinue?: boolean;
|
|
276
|
+
optionsSuccessStatus?: number;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
export interface RateLimitStore {
|
|
280
|
+
incr(key: string, windowMs: number): Promise<{ totalHits: number; resetTime: number }>;
|
|
281
|
+
decrement(key: string): Promise<void>;
|
|
282
|
+
reset(key: string): Promise<void>;
|
|
283
|
+
cleanup?(): void;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
export interface RateLimitOptions {
|
|
287
|
+
max?: number;
|
|
288
|
+
windowMs?: number;
|
|
289
|
+
message?: string;
|
|
290
|
+
statusCode?: number;
|
|
291
|
+
/** Send X-RateLimit-* headers (default true). */
|
|
292
|
+
headers?: boolean;
|
|
293
|
+
keyGenerator?: (ctx: Context) => string;
|
|
294
|
+
skip?: (ctx: Context) => boolean | Promise<boolean>;
|
|
295
|
+
handler?: (ctx: Context, options: RateLimitOptions, resetTime?: number) => unknown;
|
|
296
|
+
onLimitReached?: (ctx: Context, options: RateLimitOptions) => void;
|
|
297
|
+
skipSuccessfulRequests?: boolean;
|
|
298
|
+
skipFailedRequests?: boolean;
|
|
299
|
+
store?: RateLimitStore;
|
|
300
|
+
maxKeys?: number;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
export interface SessionStore {
|
|
304
|
+
get(id: string): Promise<Record<string, any> | null>;
|
|
305
|
+
set(id: string, data: Record<string, any>, maxAge: number): Promise<void>;
|
|
306
|
+
destroy(id: string): Promise<void>;
|
|
307
|
+
touch(id: string, data: Record<string, any>, maxAge: number): Promise<void>;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
export interface SessionOptions {
|
|
311
|
+
secret: string;
|
|
312
|
+
key?: string;
|
|
313
|
+
store?: SessionStore;
|
|
314
|
+
cookie?: CookieOptions;
|
|
315
|
+
genid?: () => string;
|
|
316
|
+
rolling?: boolean;
|
|
317
|
+
resave?: boolean;
|
|
318
|
+
saveUninitialized?: boolean;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
export interface StaticOptions {
|
|
322
|
+
maxAge?: number;
|
|
323
|
+
index?: string | string[] | false;
|
|
324
|
+
dotfiles?: 'ignore' | 'deny' | 'allow';
|
|
325
|
+
/** Legacy alias for dotfiles: 'allow'. */
|
|
326
|
+
hidden?: boolean;
|
|
327
|
+
etag?: boolean;
|
|
328
|
+
lastModified?: boolean;
|
|
329
|
+
extensions?: string[];
|
|
330
|
+
fallthrough?: boolean;
|
|
331
|
+
redirect?: boolean;
|
|
332
|
+
setHeaders?: (ctx: Context, fileName: string, stats: unknown) => void;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export interface CompressionOptions {
|
|
336
|
+
threshold?: number;
|
|
337
|
+
level?: number;
|
|
338
|
+
encodings?: Array<'br' | 'gzip' | 'deflate'>;
|
|
339
|
+
filter?: (ctx: Context, body: unknown) => boolean;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
export interface BodyParserOptions {
|
|
343
|
+
/** Maximum body size in bytes, or a string such as '100kb' / '5mb' (default 1mb). */
|
|
344
|
+
limit?: number | string;
|
|
345
|
+
type?: 'auto' | 'json' | 'urlencoded' | 'multipart' | 'text' | 'raw';
|
|
346
|
+
encoding?: string;
|
|
347
|
+
/** Decompress gzip/deflate/br request bodies (default true). */
|
|
348
|
+
inflate?: boolean;
|
|
349
|
+
verify?: (req: IncomingMessage, res: ServerResponse, body: unknown) => unknown;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
export interface CsrfOptions {
|
|
353
|
+
secret: string;
|
|
354
|
+
cookieName?: string;
|
|
355
|
+
cookieOptions?: CookieOptions;
|
|
356
|
+
headerName?: string;
|
|
357
|
+
bodyName?: string;
|
|
358
|
+
sessionKey?: string;
|
|
359
|
+
ignoreMethods?: string[];
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
export interface SecurityOptions {
|
|
363
|
+
contentSecurityPolicy?: boolean | Record<string, string | string[] | false>;
|
|
364
|
+
crossOriginEmbedderPolicy?: boolean;
|
|
365
|
+
crossOriginOpenerPolicy?: boolean;
|
|
366
|
+
crossOriginResourcePolicy?: boolean;
|
|
367
|
+
dnsPrefetchControl?: boolean;
|
|
368
|
+
frameguard?: boolean | string | { action?: string; domain?: string };
|
|
369
|
+
hidePoweredBy?: boolean;
|
|
370
|
+
hsts?: boolean | { maxAge?: number; includeSubDomains?: boolean; preload?: boolean };
|
|
371
|
+
ieNoOpen?: boolean;
|
|
372
|
+
noSniff?: boolean;
|
|
373
|
+
originAgentCluster?: boolean;
|
|
374
|
+
permittedCrossDomainPolicies?: boolean;
|
|
375
|
+
referrerPolicy?: boolean | string;
|
|
376
|
+
xssFilter?: boolean;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
export interface CacheOptions {
|
|
380
|
+
maxAge?: number;
|
|
381
|
+
maxEntries?: number;
|
|
382
|
+
key?: (ctx: Context) => string;
|
|
383
|
+
condition?: (ctx: Context) => boolean;
|
|
384
|
+
store?: { get(key: string): any; set(key: string, value: any): void };
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// Middleware factories
|
|
388
|
+
|
|
389
|
+
export interface BodyParser {
|
|
390
|
+
(options?: BodyParserOptions): Middleware;
|
|
391
|
+
json(options?: BodyParserOptions): Middleware;
|
|
392
|
+
urlencoded(options?: BodyParserOptions): Middleware;
|
|
393
|
+
text(options?: BodyParserOptions): Middleware;
|
|
394
|
+
raw(options?: BodyParserOptions): Middleware;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
export interface Security {
|
|
398
|
+
(options?: SecurityOptions): Middleware;
|
|
399
|
+
csrf(options: CsrfOptions): Middleware;
|
|
400
|
+
csrfToken(ctx: Context, options?: Partial<CsrfOptions>): string;
|
|
401
|
+
xssProtection(options?: { escapeHtml?: boolean; escapeJson?: boolean }): Middleware;
|
|
402
|
+
requestSizeLimit(options?: { limit?: number; message?: string; statusCode?: number }): Middleware;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
export interface RateLimit {
|
|
406
|
+
(options?: RateLimitOptions): Middleware & { cleanup(): void; store: RateLimitStore };
|
|
407
|
+
slowDown(options?: Record<string, unknown>): Middleware;
|
|
408
|
+
tokenBucket(options?: Record<string, unknown>): Middleware;
|
|
409
|
+
MemoryStore: new (options?: { maxKeys?: number }) => RateLimitStore;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
export interface SessionFactory {
|
|
413
|
+
(options: SessionOptions): Middleware;
|
|
414
|
+
MemoryStore: new () => SessionStore;
|
|
415
|
+
FileStore: new (options?: { path?: string; ttl?: number }) => SessionStore;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
export interface StaticFactory {
|
|
419
|
+
(root: string, options?: StaticOptions): Middleware;
|
|
420
|
+
serveStatic(root: string, options?: StaticOptions): Middleware;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
export interface CompressionFactory {
|
|
424
|
+
(options?: CompressionOptions): Middleware;
|
|
425
|
+
gzip(options?: CompressionOptions): Middleware;
|
|
426
|
+
deflate(options?: CompressionOptions): Middleware;
|
|
427
|
+
brotli(options?: CompressionOptions): Middleware;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
export declare const bodyParser: BodyParser;
|
|
431
|
+
export declare const cors: (options?: CorsOptions) => Middleware;
|
|
432
|
+
export declare const compression: CompressionFactory;
|
|
433
|
+
export declare const rateLimit: RateLimit;
|
|
434
|
+
export declare const session: SessionFactory;
|
|
435
|
+
export declare const security: Security;
|
|
436
|
+
export declare const csrf: Security['csrf'];
|
|
437
|
+
export declare const csrfToken: Security['csrfToken'];
|
|
438
|
+
export declare const serveStatic: StaticFactory;
|
|
439
|
+
export declare const healthCheck: (options?: Record<string, unknown>) => Middleware;
|
|
440
|
+
export declare const metrics: (options?: Record<string, unknown>) => Middleware;
|
|
441
|
+
export declare const logger: (options?: Record<string, unknown>) => Middleware;
|
|
442
|
+
export declare const cache: (options?: CacheOptions) => Middleware;
|
|
443
|
+
declare const staticMiddleware: StaticFactory;
|
|
444
|
+
export { staticMiddleware as static };
|
|
445
|
+
|
|
446
|
+
// Error handling
|
|
447
|
+
|
|
448
|
+
export interface HttpError extends Error {
|
|
449
|
+
status: number;
|
|
450
|
+
code?: string;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
export declare function asyncHandler(fn: Middleware): Middleware;
|
|
454
|
+
export declare function createError(message: string, status?: number, code?: string | null): HttpError;
|
|
455
|
+
export declare const errors: {
|
|
456
|
+
badRequest(message?: string): HttpError;
|
|
457
|
+
unauthorized(message?: string): HttpError;
|
|
458
|
+
forbidden(message?: string): HttpError;
|
|
459
|
+
notFound(message?: string): HttpError;
|
|
460
|
+
methodNotAllowed(message?: string): HttpError;
|
|
461
|
+
conflict(message?: string): HttpError;
|
|
462
|
+
gone(message?: string): HttpError;
|
|
463
|
+
unprocessableEntity(message?: string): HttpError;
|
|
464
|
+
tooManyRequests(message?: string): HttpError;
|
|
465
|
+
internalServerError(message?: string): HttpError;
|
|
466
|
+
notImplemented(message?: string): HttpError;
|
|
467
|
+
badGateway(message?: string): HttpError;
|
|
468
|
+
serviceUnavailable(message?: string): HttpError;
|
|
469
|
+
gatewayTimeout(message?: string): HttpError;
|
|
470
|
+
};
|
|
471
|
+
|
|
472
|
+
export declare const errorHandling: {
|
|
473
|
+
asyncHandler: typeof asyncHandler;
|
|
474
|
+
errorHandler: (...args: any[]) => unknown;
|
|
475
|
+
createError: typeof createError;
|
|
476
|
+
errors: typeof errors;
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
export declare const middleware: {
|
|
480
|
+
bodyParser: BodyParser;
|
|
481
|
+
cors: typeof cors;
|
|
482
|
+
compression: CompressionFactory;
|
|
483
|
+
compress: CompressionFactory;
|
|
484
|
+
static: StaticFactory;
|
|
485
|
+
session: SessionFactory;
|
|
486
|
+
rateLimit: RateLimit;
|
|
487
|
+
security: Security;
|
|
488
|
+
helmet: Security;
|
|
489
|
+
healthCheck: typeof healthCheck;
|
|
490
|
+
health: typeof healthCheck;
|
|
491
|
+
metrics: typeof metrics;
|
|
492
|
+
logger: typeof logger;
|
|
493
|
+
cache: typeof cache;
|
|
494
|
+
};
|
|
495
|
+
|
|
496
|
+
export declare const utils: {
|
|
497
|
+
ContextPool: new (maxSize?: number) => {
|
|
498
|
+
acquire(req: IncomingMessage, res: ServerResponse, app: Spark): Context;
|
|
499
|
+
release(ctx: Context): void;
|
|
500
|
+
};
|
|
501
|
+
RegexValidator: any;
|
|
502
|
+
SafeRegexCache: any;
|
|
503
|
+
};
|
|
504
|
+
|
|
505
|
+
/** Package version. */
|
|
506
|
+
export declare const version: string;
|
|
507
|
+
|
|
508
|
+
declare const spark: {
|
|
509
|
+
Spark: typeof Spark;
|
|
510
|
+
App: typeof Spark;
|
|
511
|
+
Application: typeof Spark;
|
|
512
|
+
Router: typeof Router;
|
|
513
|
+
Context: typeof Context;
|
|
514
|
+
middleware: typeof middleware;
|
|
515
|
+
bodyParser: BodyParser;
|
|
516
|
+
cors: typeof cors;
|
|
517
|
+
compression: CompressionFactory;
|
|
518
|
+
static: StaticFactory;
|
|
519
|
+
serveStatic: StaticFactory;
|
|
520
|
+
session: SessionFactory;
|
|
521
|
+
rateLimit: RateLimit;
|
|
522
|
+
security: Security;
|
|
523
|
+
csrf: typeof csrf;
|
|
524
|
+
csrfToken: typeof csrfToken;
|
|
525
|
+
healthCheck: typeof healthCheck;
|
|
526
|
+
metrics: typeof metrics;
|
|
527
|
+
logger: typeof logger;
|
|
528
|
+
cache: typeof cache;
|
|
529
|
+
errorHandling: typeof errorHandling;
|
|
530
|
+
asyncHandler: typeof asyncHandler;
|
|
531
|
+
createError: typeof createError;
|
|
532
|
+
errors: typeof errors;
|
|
533
|
+
utils: typeof utils;
|
|
534
|
+
version: string;
|
|
535
|
+
};
|
|
536
|
+
export default spark;
|