@adonix.org/cloud-spark 0.0.141 → 0.0.143
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/dist/index.d.ts +323 -281
- package/dist/index.js +274 -263
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import CacheLib from 'cache-control-parser';
|
|
2
|
+
import { MatchFunction } from 'path-to-regexp';
|
|
2
3
|
import { StatusCodes } from 'http-status-codes';
|
|
3
4
|
export { StatusCodes } from 'http-status-codes';
|
|
4
|
-
import { MatchFunction } from 'path-to-regexp';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* @see {@link https://github.com/etienne-martin/cache-control-parser | cache-control-parser}
|
|
@@ -10,7 +10,7 @@ type CacheControl = CacheLib.CacheControl;
|
|
|
10
10
|
declare const CacheControl: {
|
|
11
11
|
parse: (cacheControlHeader: string) => CacheLib.CacheControl;
|
|
12
12
|
stringify: (cacheControl: CacheLib.CacheControl) => string;
|
|
13
|
-
/** A
|
|
13
|
+
/** A CacheControl directive that disables all caching. */
|
|
14
14
|
DISABLE: Readonly<{
|
|
15
15
|
"no-cache": true;
|
|
16
16
|
"no-store": true;
|
|
@@ -19,16 +19,20 @@ declare const CacheControl: {
|
|
|
19
19
|
}>;
|
|
20
20
|
};
|
|
21
21
|
|
|
22
|
+
/**
|
|
23
|
+
* https://github.com/prettymuchbryce/http-status-codes
|
|
24
|
+
*/
|
|
25
|
+
|
|
22
26
|
/**
|
|
23
27
|
* Standard HTTP header names and common values.
|
|
24
28
|
*/
|
|
25
29
|
declare namespace HttpHeader {
|
|
26
30
|
const VARY = "Vary";
|
|
27
31
|
const ALLOW = "Allow";
|
|
32
|
+
const ORIGIN = "Origin";
|
|
28
33
|
const USER_AGENT = "User-Agent";
|
|
29
34
|
const CONTENT_TYPE = "Content-Type";
|
|
30
35
|
const CACHE_CONTROL = "Cache-Control";
|
|
31
|
-
const SEC_FETCH_SITE = "Sec-Fetch-Site";
|
|
32
36
|
const X_FRAME_OPTIONS = "X-Frame-Options";
|
|
33
37
|
const X_CONTENT_TYPE_OPTIONS = "X-Content-Type-Options";
|
|
34
38
|
const REFERRER_POLICY = "Referrer-Policy";
|
|
@@ -41,22 +45,9 @@ declare namespace HttpHeader {
|
|
|
41
45
|
const ALLOW_METHODS = "Access-Control-Allow-Methods";
|
|
42
46
|
const EXPOSE_HEADERS = "Access-Control-Expose-Headers";
|
|
43
47
|
const ALLOW_CREDENTIALS = "Access-Control-Allow-Credentials";
|
|
44
|
-
const ORIGIN = "Origin";
|
|
45
48
|
const ALLOW_ALL_ORIGINS = "*";
|
|
46
49
|
const CROSS_SITE = "cross-site";
|
|
47
50
|
}
|
|
48
|
-
/**
|
|
49
|
-
* Time constants in seconds. Month is approximated as 30 days.
|
|
50
|
-
*/
|
|
51
|
-
declare const Time: {
|
|
52
|
-
readonly Second: 1;
|
|
53
|
-
readonly Minute: 60;
|
|
54
|
-
readonly Hour: 3600;
|
|
55
|
-
readonly Day: 86400;
|
|
56
|
-
readonly Week: 604800;
|
|
57
|
-
readonly Month: 2592000;
|
|
58
|
-
readonly Year: 31536000;
|
|
59
|
-
};
|
|
60
51
|
/**
|
|
61
52
|
* Standard HTTP request methods.
|
|
62
53
|
*/
|
|
@@ -82,21 +73,7 @@ declare const POST: Method;
|
|
|
82
73
|
declare const PATCH: Method;
|
|
83
74
|
declare const DELETE: Method;
|
|
84
75
|
declare const OPTIONS: Method;
|
|
85
|
-
|
|
86
|
-
* Type guard that checks if a string is a valid HTTP method.
|
|
87
|
-
*
|
|
88
|
-
* @param value - The string to test.
|
|
89
|
-
* @returns True if `value` is a recognized HTTP method.
|
|
90
|
-
*/
|
|
91
|
-
declare function isMethod(value: string): value is Method;
|
|
92
|
-
/**
|
|
93
|
-
* Returns the proper Content-Type string for a given media type.
|
|
94
|
-
* Appends `charset=utf-8` for text-based types that require it.
|
|
95
|
-
*
|
|
96
|
-
* @param type - The media type.
|
|
97
|
-
* @returns A string suitable for the `Content-Type` header.
|
|
98
|
-
*/
|
|
99
|
-
declare function getContentType(type: MediaType): string;
|
|
76
|
+
|
|
100
77
|
/**
|
|
101
78
|
* Common media types used for HTTP headers.
|
|
102
79
|
*/
|
|
@@ -145,241 +122,19 @@ declare enum MediaType {
|
|
|
145
122
|
TAR = "application/x-tar",
|
|
146
123
|
BZIP2 = "application/x-bzip2"
|
|
147
124
|
}
|
|
148
|
-
/**
|
|
149
|
-
* Sets a header on the given Headers object.
|
|
150
|
-
*
|
|
151
|
-
* - If `value` is an array, any duplicates and empty strings are removed.
|
|
152
|
-
* - If the resulting value array is empty, the header is deleted.
|
|
153
|
-
* - Otherwise, values are joined with `", "` and set as the header value.
|
|
154
|
-
*
|
|
155
|
-
* @param headers - The Headers object to modify.
|
|
156
|
-
* @param key - The header name to set.
|
|
157
|
-
* @param value - The header value(s) to set. Can be a string or array of strings.
|
|
158
|
-
*/
|
|
159
|
-
declare function setHeader(headers: Headers, key: string, value: string | string[]): void;
|
|
160
|
-
/**
|
|
161
|
-
* Merges new value(s) into an existing header on the given Headers object.
|
|
162
|
-
*
|
|
163
|
-
* - Preserves any existing values and adds new ones.
|
|
164
|
-
* - Removes duplicates and trims all values.
|
|
165
|
-
* - If the header does not exist, it is created.
|
|
166
|
-
*
|
|
167
|
-
* @param headers - The Headers object to modify.
|
|
168
|
-
* @param key - The header name to merge into.
|
|
169
|
-
* @param value - The new header value(s) to add. Can be a string or array of strings.
|
|
170
|
-
*/
|
|
171
|
-
declare function mergeHeader(headers: Headers, key: string, value: string | string[]): void;
|
|
172
|
-
/**
|
|
173
|
-
* Normalizes a URL string for use as a consistent cache key.
|
|
174
|
-
*
|
|
175
|
-
* - Sorts query parameters alphabetically so `?b=2&a=1` and `?a=1&b=2` are treated the same.
|
|
176
|
-
* - Strips fragment identifiers (`#...`) since they are not sent in HTTP requests.
|
|
177
|
-
* - Leaves protocol, host, path, and query values intact.
|
|
178
|
-
*
|
|
179
|
-
* @param url The original URL string to normalize.
|
|
180
|
-
* @returns A normalized URL string suitable for hashing or direct cache key use.
|
|
181
|
-
*/
|
|
182
|
-
declare function normalizeUrl(url: string): URL;
|
|
183
|
-
/**
|
|
184
|
-
* Lexicographically compares two strings.
|
|
185
|
-
*
|
|
186
|
-
* This comparator can be used in `Array.prototype.sort()` to produce a
|
|
187
|
-
* consistent, stable ordering of string arrays.
|
|
188
|
-
*
|
|
189
|
-
* @param a - The first string to compare.
|
|
190
|
-
* @param b - The second string to compare.
|
|
191
|
-
* @returns A number indicating the relative order of `a` and `b`.
|
|
192
|
-
*/
|
|
193
|
-
declare function lexCompare(a: string, b: string): number;
|
|
194
|
-
/**
|
|
195
|
-
* Extracts the `Origin` header value from a request.
|
|
196
|
-
*
|
|
197
|
-
* The `Origin` header identifies the origin (scheme, host, and port)
|
|
198
|
-
* of the request initiator. It is commonly used for CORS checks.
|
|
199
|
-
*
|
|
200
|
-
* @param request - The incoming {@link Request} object.
|
|
201
|
-
* @returns The origin string if present, otherwise `null`.
|
|
202
|
-
*/
|
|
203
|
-
declare function getOrigin(request: Request): string | null;
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* Represents the constructor of a Worker subclass.
|
|
207
|
-
*
|
|
208
|
-
* @template T - The specific type of Worker being constructed. Defaults to `Worker`.
|
|
209
|
-
* @param req - The `Request` object to be handled by the worker instance.
|
|
210
|
-
* @param env - The environment bindings available to the worker.
|
|
211
|
-
* @param ctx - The `ExecutionContext` for the worker invocation.
|
|
212
|
-
* @returns An instance of the worker type `T`.
|
|
213
|
-
*/
|
|
214
|
-
type WorkerClass<T extends Worker = Worker> = new (request: Request, env: Env, ctx: ExecutionContext) => T;
|
|
215
|
-
/**
|
|
216
|
-
* Defines the contract for a Cloudflare-compatible Worker.
|
|
217
|
-
*
|
|
218
|
-
* Implementations are responsible for handling incoming requests,
|
|
219
|
-
* providing access to the request, environment bindings, and
|
|
220
|
-
* execution context.
|
|
221
|
-
*/
|
|
222
|
-
interface Worker {
|
|
223
|
-
/**
|
|
224
|
-
* Processes the incoming {@link Request} and produces a {@link Response}.
|
|
225
|
-
*
|
|
226
|
-
* @returns A Promise that resolves to the HTTP {@link Response}.
|
|
227
|
-
*/
|
|
228
|
-
fetch(): Promise<Response>;
|
|
229
|
-
/**
|
|
230
|
-
* The original {@link Request} being processed by this worker instance.
|
|
231
|
-
*/
|
|
232
|
-
get request(): Request;
|
|
233
|
-
/**
|
|
234
|
-
* The environment bindings provided at runtime (e.g., KV, R2, secrets).
|
|
235
|
-
*/
|
|
236
|
-
get env(): Env;
|
|
237
|
-
/**
|
|
238
|
-
* The {@link ExecutionContext} associated with the current request,
|
|
239
|
-
* used to manage background tasks and request lifecycle.
|
|
240
|
-
*/
|
|
241
|
-
get ctx(): ExecutionContext;
|
|
242
|
-
/**
|
|
243
|
-
* The HTTP methods supported by this worker.
|
|
244
|
-
*/
|
|
245
|
-
getAllowedMethods(): Method[];
|
|
246
|
-
}
|
|
247
125
|
|
|
248
126
|
/**
|
|
249
|
-
*
|
|
250
|
-
* Manages headers, status, and media type.
|
|
251
|
-
*/
|
|
252
|
-
declare abstract class BaseResponse {
|
|
253
|
-
constructor();
|
|
254
|
-
/** HTTP headers for the response. */
|
|
255
|
-
headers: Headers;
|
|
256
|
-
/** HTTP status code (default 200 OK). */
|
|
257
|
-
status: StatusCodes;
|
|
258
|
-
/** Optional status text. Defaults to standard reason phrase. */
|
|
259
|
-
statusText?: string;
|
|
260
|
-
/** Default media type of the response body. */
|
|
261
|
-
mediaType: MediaType;
|
|
262
|
-
/** Converts current state to ResponseInit for constructing a Response. */
|
|
263
|
-
protected get responseInit(): ResponseInit;
|
|
264
|
-
/** Sets a header, overwriting any existing value. */
|
|
265
|
-
setHeader(key: string, value: string | string[]): void;
|
|
266
|
-
/** Merges a header with existing values (does not overwrite). */
|
|
267
|
-
mergeHeader(key: string, value: string | string[]): void;
|
|
268
|
-
/** Adds a Content-Type header if not already existing (does not overwrite). */
|
|
269
|
-
addContentType(): void;
|
|
270
|
-
}
|
|
271
|
-
/**
|
|
272
|
-
* Base response class that adds caching headers.
|
|
273
|
-
*/
|
|
274
|
-
declare abstract class CacheResponse extends BaseResponse {
|
|
275
|
-
cache?: CacheControl | undefined;
|
|
276
|
-
constructor(cache?: CacheControl | undefined);
|
|
277
|
-
/** Adds Cache-Control header if caching is configured. */
|
|
278
|
-
protected addCacheHeader(): void;
|
|
279
|
-
}
|
|
280
|
-
/**
|
|
281
|
-
* Core worker response. Combines caching, and security headers.
|
|
282
|
-
*/
|
|
283
|
-
declare abstract class WorkerResponse extends CacheResponse {
|
|
284
|
-
readonly worker: Worker;
|
|
285
|
-
private readonly body;
|
|
286
|
-
constructor(worker: Worker, body?: BodyInit | null, cache?: CacheControl);
|
|
287
|
-
/** Builds the Response object with body, headers, and status. */
|
|
288
|
-
getResponse(): Promise<Response>;
|
|
289
|
-
}
|
|
290
|
-
/**
|
|
291
|
-
* Wraps an existing Response and clones its body, headers, and status.
|
|
292
|
-
*/
|
|
293
|
-
declare class ClonedResponse extends WorkerResponse {
|
|
294
|
-
constructor(worker: Worker, response: Response, cache?: CacheControl);
|
|
295
|
-
}
|
|
296
|
-
/**
|
|
297
|
-
* Represents a successful response with customizable body and status.
|
|
298
|
-
*/
|
|
299
|
-
declare class SuccessResponse extends WorkerResponse {
|
|
300
|
-
constructor(worker: Worker, body?: BodyInit | null, cache?: CacheControl, status?: StatusCodes);
|
|
301
|
-
}
|
|
302
|
-
/**
|
|
303
|
-
* JSON response. Automatically sets Content-Type to application/json.
|
|
304
|
-
*/
|
|
305
|
-
declare class JsonResponse extends SuccessResponse {
|
|
306
|
-
constructor(worker: Worker, json?: unknown, cache?: CacheControl, status?: StatusCodes);
|
|
307
|
-
}
|
|
308
|
-
/**
|
|
309
|
-
* HTML response. Automatically sets Content-Type to text/html.
|
|
310
|
-
*/
|
|
311
|
-
declare class HtmlResponse extends SuccessResponse {
|
|
312
|
-
constructor(worker: Worker, body: string, cache?: CacheControl, status?: StatusCodes);
|
|
313
|
-
}
|
|
314
|
-
/**
|
|
315
|
-
* Plain text response. Automatically sets Content-Type to text/plain.
|
|
316
|
-
*/
|
|
317
|
-
declare class TextResponse extends SuccessResponse {
|
|
318
|
-
constructor(worker: Worker, content: string, cache?: CacheControl, status?: StatusCodes);
|
|
319
|
-
}
|
|
320
|
-
/**
|
|
321
|
-
* Response for HEAD requests. Clones headers but has no body.
|
|
322
|
-
*/
|
|
323
|
-
declare class Head extends WorkerResponse {
|
|
324
|
-
constructor(worker: Worker, get: Response);
|
|
325
|
-
}
|
|
326
|
-
/**
|
|
327
|
-
* Response for OPTIONS preflight requests.
|
|
328
|
-
* Sets CORS headers and returns 204 No Content.
|
|
329
|
-
*/
|
|
330
|
-
declare class Options extends SuccessResponse {
|
|
331
|
-
constructor(worker: Worker);
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
/**
|
|
335
|
-
* Generic HTTP error response.
|
|
336
|
-
* Sends a JSON body with status, error message, and details.
|
|
127
|
+
* Time constants in seconds. Month is approximated as 30 days.
|
|
337
128
|
*/
|
|
338
|
-
declare
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
}
|
|
347
|
-
/** 400 Bad Request error response. */
|
|
348
|
-
declare class BadRequest extends HttpError {
|
|
349
|
-
constructor(worker: Worker, details?: string);
|
|
350
|
-
}
|
|
351
|
-
/** 401 Unauthorized error response. */
|
|
352
|
-
declare class Unauthorized extends HttpError {
|
|
353
|
-
constructor(worker: Worker, details?: string);
|
|
354
|
-
}
|
|
355
|
-
/** 403 Forbidden error response. */
|
|
356
|
-
declare class Forbidden extends HttpError {
|
|
357
|
-
constructor(worker: Worker, details?: string);
|
|
358
|
-
}
|
|
359
|
-
/** 404 Not Found error response. */
|
|
360
|
-
declare class NotFound extends HttpError {
|
|
361
|
-
constructor(worker: Worker, details?: string);
|
|
362
|
-
}
|
|
363
|
-
/** 405 Method Not Allowed error response. */
|
|
364
|
-
declare class MethodNotAllowed extends HttpError {
|
|
365
|
-
constructor(worker: Worker);
|
|
366
|
-
}
|
|
367
|
-
/** 500 Internal Server Error response. */
|
|
368
|
-
declare class InternalServerError extends HttpError {
|
|
369
|
-
constructor(worker: Worker, details?: string);
|
|
370
|
-
}
|
|
371
|
-
/** 501 Not Implemented error response. */
|
|
372
|
-
declare class NotImplemented extends HttpError {
|
|
373
|
-
constructor(worker: Worker, details?: string);
|
|
374
|
-
}
|
|
375
|
-
/** 501 Method Not Implemented error response for unsupported HTTP methods. */
|
|
376
|
-
declare class MethodNotImplemented extends NotImplemented {
|
|
377
|
-
constructor(worker: Worker);
|
|
378
|
-
}
|
|
379
|
-
/** 503 Service Unavailable error response. */
|
|
380
|
-
declare class ServiceUnavailable extends HttpError {
|
|
381
|
-
constructor(worker: Worker, details?: string);
|
|
382
|
-
}
|
|
129
|
+
declare const Time: {
|
|
130
|
+
readonly Second: 1;
|
|
131
|
+
readonly Minute: 60;
|
|
132
|
+
readonly Hour: 3600;
|
|
133
|
+
readonly Day: 86400;
|
|
134
|
+
readonly Week: 604800;
|
|
135
|
+
readonly Month: 2592000;
|
|
136
|
+
readonly Year: 31536000;
|
|
137
|
+
};
|
|
383
138
|
|
|
384
139
|
/**
|
|
385
140
|
* Override only what is needed from the default CORS configuration.
|
|
@@ -427,8 +182,8 @@ interface CorsConfig {
|
|
|
427
182
|
* Structure for JSON-formatted error responses.
|
|
428
183
|
*
|
|
429
184
|
* This interface defines the standard shape of error responses returned
|
|
430
|
-
*
|
|
431
|
-
*
|
|
185
|
+
* when an operation fails. It ensures consistent error reporting and easy
|
|
186
|
+
* parsing by clients.
|
|
432
187
|
*/
|
|
433
188
|
interface ErrorJson {
|
|
434
189
|
/**
|
|
@@ -447,12 +202,57 @@ interface ErrorJson {
|
|
|
447
202
|
* Optional detailed message describing the error.
|
|
448
203
|
*
|
|
449
204
|
* This can include additional context, debugging hints, or
|
|
450
|
-
* information useful to the client.
|
|
205
|
+
* information useful to the client. Will be an empty string
|
|
451
206
|
* if no details are provided.
|
|
452
207
|
*/
|
|
453
208
|
details: string;
|
|
454
209
|
}
|
|
455
210
|
|
|
211
|
+
/**
|
|
212
|
+
* Represents the constructor of a Worker subclass.
|
|
213
|
+
*
|
|
214
|
+
* @template T - The specific type of Worker being constructed. Defaults to `Worker`.
|
|
215
|
+
* @param req - The `Request` object to be handled by the worker instance.
|
|
216
|
+
* @param env - The environment bindings available to the worker.
|
|
217
|
+
* @param ctx - The `ExecutionContext` for the worker invocation.
|
|
218
|
+
* @returns An instance of the worker type `T`.
|
|
219
|
+
*/
|
|
220
|
+
type WorkerClass<T extends Worker = Worker> = new (request: Request, env: Env, ctx: ExecutionContext) => T;
|
|
221
|
+
/**
|
|
222
|
+
* Defines the contract for a Cloudflare-compatible Worker.
|
|
223
|
+
*
|
|
224
|
+
* Implementations are responsible for handling incoming requests,
|
|
225
|
+
* providing access to the request, environment bindings, and
|
|
226
|
+
* execution context.
|
|
227
|
+
*/
|
|
228
|
+
interface Worker {
|
|
229
|
+
/**
|
|
230
|
+
* Processes the incoming {@link Request} and produces a {@link Response}.
|
|
231
|
+
*
|
|
232
|
+
* @returns A Promise that resolves to the HTTP {@link Response}.
|
|
233
|
+
*/
|
|
234
|
+
fetch(): Promise<Response>;
|
|
235
|
+
/**
|
|
236
|
+
* The original {@link Request} being processed by this worker instance.
|
|
237
|
+
*/
|
|
238
|
+
get request(): Request;
|
|
239
|
+
/**
|
|
240
|
+
* The environment bindings provided at runtime (e.g., KV, R2, secrets).
|
|
241
|
+
*/
|
|
242
|
+
get env(): Env;
|
|
243
|
+
/**
|
|
244
|
+
* The {@link ExecutionContext} associated with the current request,
|
|
245
|
+
* used to manage background tasks and request lifecycle.
|
|
246
|
+
*/
|
|
247
|
+
get ctx(): ExecutionContext;
|
|
248
|
+
/**
|
|
249
|
+
* Returns the list of HTTP methods that are allowed for this worker.
|
|
250
|
+
*
|
|
251
|
+
* @returns An array of allowed HTTP methods.
|
|
252
|
+
*/
|
|
253
|
+
getAllowedMethods(): Method[];
|
|
254
|
+
}
|
|
255
|
+
|
|
456
256
|
/**
|
|
457
257
|
* Parameters extracted from a matched route.
|
|
458
258
|
*
|
|
@@ -562,26 +362,131 @@ declare abstract class Middleware {
|
|
|
562
362
|
abstract handle(worker: Worker, next: () => Promise<Response>): Promise<Response>;
|
|
563
363
|
}
|
|
564
364
|
|
|
365
|
+
/**
|
|
366
|
+
* Middleware for caching GET requests.
|
|
367
|
+
*
|
|
368
|
+
* This middleware checks a cache (either a named cache or the default)
|
|
369
|
+
* before passing the request down the middleware chain. Responses for
|
|
370
|
+
* successful GET requests are automatically stored in the cache.
|
|
371
|
+
*
|
|
372
|
+
* Non-GET requests are never cached. The cache key can be customized
|
|
373
|
+
* via the `getKey` function; otherwise, the URL is normalized and used.
|
|
374
|
+
*
|
|
375
|
+
* @param cacheName - Optional name of the cache to use. Defaults to `caches.default`.
|
|
376
|
+
* @param getKey - Optional function to generate a cache key from a request.
|
|
377
|
+
* @returns A {@link Middleware} instance that can be added to your middleware chain.
|
|
378
|
+
*/
|
|
379
|
+
declare function cache(cacheName?: string, getKey?: (request: Request) => URL | RequestInfo): Middleware;
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Creates a CORS middleware instance.
|
|
383
|
+
*
|
|
384
|
+
* This middleware automatically handles Cross-Origin Resource Sharing (CORS)
|
|
385
|
+
* for incoming requests, including preflight OPTIONS requests, and adds
|
|
386
|
+
* appropriate headers to responses.
|
|
387
|
+
*
|
|
388
|
+
* @param init - Optional configuration for CORS behavior. See {@link CorsConfig}.
|
|
389
|
+
* @returns A {@link Middleware} instance that can be used in your middleware chain.
|
|
390
|
+
*/
|
|
565
391
|
declare function cors(init?: CorsInit): Middleware;
|
|
566
392
|
|
|
567
|
-
|
|
393
|
+
/**
|
|
394
|
+
* Lexicographically compares two strings.
|
|
395
|
+
*
|
|
396
|
+
* This comparator can be used in `Array.prototype.sort()` to produce a
|
|
397
|
+
* consistent, stable ordering of string arrays.
|
|
398
|
+
*
|
|
399
|
+
* @param a - The first string to compare.
|
|
400
|
+
* @param b - The second string to compare.
|
|
401
|
+
* @returns A number indicating the relative order of `a` and `b`.
|
|
402
|
+
*/
|
|
403
|
+
declare function lexCompare(a: string, b: string): number;
|
|
568
404
|
|
|
569
405
|
/**
|
|
570
|
-
*
|
|
406
|
+
* Sets a header on the given Headers object.
|
|
407
|
+
*
|
|
408
|
+
* - If `value` is an array, any duplicates and empty strings are removed.
|
|
409
|
+
* - If the resulting value array is empty, the header is deleted.
|
|
410
|
+
* - Otherwise, values are joined with `", "` and set as the header value.
|
|
411
|
+
*
|
|
412
|
+
* @param headers - The Headers object to modify.
|
|
413
|
+
* @param key - The header name to set.
|
|
414
|
+
* @param value - The header value(s) to set. Can be a string or array of strings.
|
|
415
|
+
*/
|
|
416
|
+
declare function setHeader(headers: Headers, key: string, value: string | string[]): void;
|
|
417
|
+
/**
|
|
418
|
+
* Merges new value(s) into an existing header on the given Headers object.
|
|
419
|
+
*
|
|
420
|
+
* - Preserves any existing values and adds new ones.
|
|
421
|
+
* - Removes duplicates and trims all values.
|
|
422
|
+
* - If the header does not exist, it is created.
|
|
423
|
+
* - If the resulting value array is empty, the header is deleted.
|
|
571
424
|
*
|
|
572
|
-
*
|
|
573
|
-
*
|
|
425
|
+
* @param headers - The Headers object to modify.
|
|
426
|
+
* @param key - The header name to merge into.
|
|
427
|
+
* @param value - The new header value(s) to add. Can be a string or array of strings.
|
|
428
|
+
*/
|
|
429
|
+
declare function mergeHeader(headers: Headers, key: string, value: string | string[]): void;
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Type guard that checks if a string is a valid HTTP method.
|
|
574
433
|
*
|
|
575
|
-
* @
|
|
434
|
+
* @param value - The string to test.
|
|
435
|
+
* @returns True if `value` is a recognized HTTP method.
|
|
436
|
+
*/
|
|
437
|
+
declare function isMethod(value: string): value is Method;
|
|
438
|
+
/**
|
|
439
|
+
* Returns the proper Content-Type string for a given media type.
|
|
440
|
+
* Appends `charset=utf-8` for text-based types that require it.
|
|
441
|
+
*
|
|
442
|
+
* @param type - The media type.
|
|
443
|
+
* @returns A string suitable for the `Content-Type` header.
|
|
444
|
+
*/
|
|
445
|
+
declare function getContentType(type: MediaType): string;
|
|
446
|
+
/**
|
|
447
|
+
* Extracts and normalizes the `Origin` header from a request.
|
|
448
|
+
*
|
|
449
|
+
* Returns the origin (scheme + host + port) as a string if present and valid.
|
|
450
|
+
* Returns `null` if:
|
|
451
|
+
* - The `Origin` header is missing
|
|
452
|
+
* - The `Origin` header is `"null"` (opaque origin)
|
|
453
|
+
* - The `Origin` header is malformed
|
|
454
|
+
*
|
|
455
|
+
* @param request - The incoming {@link Request} object.
|
|
456
|
+
* @returns The normalized origin string, or `null` if not present or invalid.
|
|
457
|
+
*/
|
|
458
|
+
declare function getOrigin(request: Request): string | null;
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* Normalizes a URL string for use as a consistent cache key.
|
|
462
|
+
*
|
|
463
|
+
* - Sorts query parameters alphabetically so `?b=2&a=1` and `?a=1&b=2` are treated the same.
|
|
464
|
+
* - Strips fragment identifiers (`#...`) since they are not sent in HTTP requests.
|
|
465
|
+
* - Leaves protocol, host, path, and query values intact.
|
|
466
|
+
*
|
|
467
|
+
* @param url The original URL string to normalize.
|
|
468
|
+
* @returns A normalized URL string suitable for hashing or direct cache key use.
|
|
469
|
+
*/
|
|
470
|
+
declare function normalizeUrl(url: string): URL;
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* A type-safe Cloudflare Worker handler with a guaranteed `fetch` method.
|
|
474
|
+
*
|
|
475
|
+
* Extends `ExportedHandler` but ensures that `fetch` exists and has the
|
|
476
|
+
* correct signature for Cloudflare Worker invocation.
|
|
477
|
+
*
|
|
478
|
+
* `Env` is a placeholder type for the environment bindings provided by
|
|
479
|
+
* the user (e.g., KV namespaces, secrets, Durable Objects). This interface
|
|
480
|
+
* does not constrain the actual type; it only ensures `fetch` is present.
|
|
576
481
|
*/
|
|
577
482
|
interface FetchHandler extends ExportedHandler<Env> {
|
|
578
483
|
/**
|
|
579
484
|
* Handles an incoming request and produces a response.
|
|
580
485
|
*
|
|
581
486
|
* @param request - The incoming `Request` object.
|
|
582
|
-
* @param env -
|
|
487
|
+
* @param env - User-provided environment bindings (placeholder type).
|
|
583
488
|
* @param ctx - Execution context for background tasks (`waitUntil`).
|
|
584
|
-
* @returns A `Promise` that resolves to
|
|
489
|
+
* @returns A `Promise` that resolves to a `Response`.
|
|
585
490
|
*/
|
|
586
491
|
fetch: (request: Request, env: Env, ctx: ExecutionContext) => Promise<Response>;
|
|
587
492
|
}
|
|
@@ -616,13 +521,7 @@ declare abstract class BaseWorker implements Worker {
|
|
|
616
521
|
* @returns A Promise that resolves to the `Response` for the request.
|
|
617
522
|
*/
|
|
618
523
|
protected abstract dispatch(): Promise<Response>;
|
|
619
|
-
|
|
620
|
-
* DEFAULT allowed HTTP methods for subclasses.
|
|
621
|
-
*
|
|
622
|
-
* These defaults were selected for getting started quickly and should be
|
|
623
|
-
* overridden for the specific worker.
|
|
624
|
-
*/
|
|
625
|
-
getAllowedMethods(): Method[];
|
|
524
|
+
abstract getAllowedMethods(): Method[];
|
|
626
525
|
/**
|
|
627
526
|
* Creates a new instance of the current Worker subclass.
|
|
628
527
|
*
|
|
@@ -712,6 +611,13 @@ declare abstract class BasicWorker extends MiddlewareWorker {
|
|
|
712
611
|
* standard HEAD requirements.
|
|
713
612
|
*/
|
|
714
613
|
protected head(): Promise<Response>;
|
|
614
|
+
/**
|
|
615
|
+
* DEFAULT allowed HTTP methods for subclasses.
|
|
616
|
+
*
|
|
617
|
+
* These defaults were selected for getting started quickly and should be
|
|
618
|
+
* overridden for each specific worker.
|
|
619
|
+
*/
|
|
620
|
+
getAllowedMethods(): Method[];
|
|
715
621
|
/**
|
|
716
622
|
* Simplify and standardize {@link Response} creation by extending {@link WorkerResponse}
|
|
717
623
|
* or any of its subclasses and passing to this method.
|
|
@@ -726,7 +632,9 @@ declare abstract class BasicWorker extends MiddlewareWorker {
|
|
|
726
632
|
* @param args Additional constructor arguments
|
|
727
633
|
* @returns A Promise resolving to the {@link Response} object
|
|
728
634
|
*/
|
|
729
|
-
protected getResponse<
|
|
635
|
+
protected getResponse<Ctor extends new (...args: any[]) => {
|
|
636
|
+
getResponse(): Promise<Response>;
|
|
637
|
+
}>(ResponseClass: Ctor, ...args: ConstructorParameters<Ctor>): Promise<Response>;
|
|
730
638
|
}
|
|
731
639
|
|
|
732
640
|
/**
|
|
@@ -795,4 +703,138 @@ declare abstract class RouteWorker extends BasicWorker {
|
|
|
795
703
|
protected options(): Promise<Response>;
|
|
796
704
|
}
|
|
797
705
|
|
|
706
|
+
/**
|
|
707
|
+
* Base class for building HTTP responses.
|
|
708
|
+
* Manages headers, status, and media type.
|
|
709
|
+
*/
|
|
710
|
+
declare abstract class BaseResponse {
|
|
711
|
+
/** HTTP headers for the response. */
|
|
712
|
+
headers: Headers;
|
|
713
|
+
/** HTTP status code (default 200 OK). */
|
|
714
|
+
status: StatusCodes;
|
|
715
|
+
/** Optional status text. Defaults to standard reason phrase. */
|
|
716
|
+
statusText?: string;
|
|
717
|
+
/** Default media type of the response body. */
|
|
718
|
+
mediaType: MediaType;
|
|
719
|
+
/** Converts current state to ResponseInit for constructing a Response. */
|
|
720
|
+
protected get responseInit(): ResponseInit;
|
|
721
|
+
/** Sets a header, overwriting any existing value. */
|
|
722
|
+
setHeader(key: string, value: string | string[]): void;
|
|
723
|
+
/** Merges a header with existing values (does not overwrite). */
|
|
724
|
+
mergeHeader(key: string, value: string | string[]): void;
|
|
725
|
+
/** Adds a Content-Type header if not already existing (does not overwrite). */
|
|
726
|
+
addContentType(): void;
|
|
727
|
+
}
|
|
728
|
+
/**
|
|
729
|
+
* Base response class that adds caching headers.
|
|
730
|
+
*/
|
|
731
|
+
declare abstract class CacheResponse extends BaseResponse {
|
|
732
|
+
cache?: CacheControl | undefined;
|
|
733
|
+
constructor(cache?: CacheControl | undefined);
|
|
734
|
+
/** Adds Cache-Control header if caching is configured. */
|
|
735
|
+
protected addCacheHeader(): void;
|
|
736
|
+
}
|
|
737
|
+
/**
|
|
738
|
+
* Core worker response. Combines caching, and security headers.
|
|
739
|
+
*/
|
|
740
|
+
declare abstract class WorkerResponse extends CacheResponse {
|
|
741
|
+
private readonly body;
|
|
742
|
+
constructor(body?: BodyInit | null, cache?: CacheControl);
|
|
743
|
+
/** Builds the Response object with body, headers, and status. */
|
|
744
|
+
getResponse(): Promise<Response>;
|
|
745
|
+
}
|
|
746
|
+
/**
|
|
747
|
+
* Wraps an existing Response and clones its body, headers, and status.
|
|
748
|
+
*/
|
|
749
|
+
declare class ClonedResponse extends WorkerResponse {
|
|
750
|
+
constructor(response: Response, cache?: CacheControl);
|
|
751
|
+
}
|
|
752
|
+
/**
|
|
753
|
+
* Represents a successful response with customizable body, cache and status.
|
|
754
|
+
*/
|
|
755
|
+
declare class SuccessResponse extends WorkerResponse {
|
|
756
|
+
constructor(body?: BodyInit | null, cache?: CacheControl, status?: StatusCodes);
|
|
757
|
+
}
|
|
758
|
+
/**
|
|
759
|
+
* JSON response. Automatically sets Content-Type to application/json.
|
|
760
|
+
*/
|
|
761
|
+
declare class JsonResponse extends SuccessResponse {
|
|
762
|
+
constructor(json?: unknown, cache?: CacheControl, status?: StatusCodes);
|
|
763
|
+
}
|
|
764
|
+
/**
|
|
765
|
+
* HTML response. Automatically sets Content-Type to text/html.
|
|
766
|
+
*/
|
|
767
|
+
declare class HtmlResponse extends SuccessResponse {
|
|
768
|
+
constructor(body: string, cache?: CacheControl, status?: StatusCodes);
|
|
769
|
+
}
|
|
770
|
+
/**
|
|
771
|
+
* Plain text response. Automatically sets Content-Type to text/plain.
|
|
772
|
+
*/
|
|
773
|
+
declare class TextResponse extends SuccessResponse {
|
|
774
|
+
constructor(content: string, cache?: CacheControl, status?: StatusCodes);
|
|
775
|
+
}
|
|
776
|
+
/**
|
|
777
|
+
* Response for HEAD requests. Copy headers and status from a GET response
|
|
778
|
+
* without the body.
|
|
779
|
+
*/
|
|
780
|
+
declare class Head extends WorkerResponse {
|
|
781
|
+
constructor(get: Response);
|
|
782
|
+
}
|
|
783
|
+
/**
|
|
784
|
+
* Response for OPTIONS preflight requests.
|
|
785
|
+
*/
|
|
786
|
+
declare class Options extends SuccessResponse {
|
|
787
|
+
constructor();
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
/**
|
|
791
|
+
* Generic HTTP error response.
|
|
792
|
+
* Sends a JSON body with status, error message, and details.
|
|
793
|
+
*/
|
|
794
|
+
declare class HttpError extends JsonResponse {
|
|
795
|
+
protected readonly details?: string | undefined;
|
|
796
|
+
/**
|
|
797
|
+
* @param worker The worker handling the request.
|
|
798
|
+
* @param status HTTP status code.
|
|
799
|
+
* @param details Optional detailed error message.
|
|
800
|
+
*/
|
|
801
|
+
constructor(status: StatusCodes, details?: string | undefined);
|
|
802
|
+
}
|
|
803
|
+
/** 400 Bad Request error response. */
|
|
804
|
+
declare class BadRequest extends HttpError {
|
|
805
|
+
constructor(details?: string);
|
|
806
|
+
}
|
|
807
|
+
/** 401 Unauthorized error response. */
|
|
808
|
+
declare class Unauthorized extends HttpError {
|
|
809
|
+
constructor(details?: string);
|
|
810
|
+
}
|
|
811
|
+
/** 403 Forbidden error response. */
|
|
812
|
+
declare class Forbidden extends HttpError {
|
|
813
|
+
constructor(details?: string);
|
|
814
|
+
}
|
|
815
|
+
/** 404 Not Found error response. */
|
|
816
|
+
declare class NotFound extends HttpError {
|
|
817
|
+
constructor(details?: string);
|
|
818
|
+
}
|
|
819
|
+
/** 405 Method Not Allowed error response. */
|
|
820
|
+
declare class MethodNotAllowed extends HttpError {
|
|
821
|
+
constructor(worker: Worker);
|
|
822
|
+
}
|
|
823
|
+
/** 500 Internal Server Error response. */
|
|
824
|
+
declare class InternalServerError extends HttpError {
|
|
825
|
+
constructor(details?: string);
|
|
826
|
+
}
|
|
827
|
+
/** 501 Not Implemented error response. */
|
|
828
|
+
declare class NotImplemented extends HttpError {
|
|
829
|
+
constructor(details?: string);
|
|
830
|
+
}
|
|
831
|
+
/** 501 Method Not Implemented error response for unsupported HTTP methods. */
|
|
832
|
+
declare class MethodNotImplemented extends NotImplemented {
|
|
833
|
+
constructor(worker: Worker);
|
|
834
|
+
}
|
|
835
|
+
/** 503 Service Unavailable error response. */
|
|
836
|
+
declare class ServiceUnavailable extends HttpError {
|
|
837
|
+
constructor(details?: string);
|
|
838
|
+
}
|
|
839
|
+
|
|
798
840
|
export { BadRequest, BasicWorker, CacheControl, ClonedResponse, type CorsConfig, type CorsInit, DELETE, type ErrorJson, Forbidden, GET, HEAD, Head, HtmlResponse, HttpError, HttpHeader, InternalServerError, JsonResponse, type MatchedRoute, MediaType, Method, MethodNotAllowed, MethodNotImplemented, Middleware, NotFound, NotImplemented, OPTIONS, Options, PATCH, POST, PUT, type PathParams, type Route, type RouteCallback, type RouteHandler, type RouteTable, type RouteTuple, RouteWorker, ServiceUnavailable, SuccessResponse, TextResponse, Time, Unauthorized, type Worker, type WorkerClass, WorkerResponse, cache, cors, getContentType, getOrigin, isMethod, lexCompare, mergeHeader, normalizeUrl, setHeader };
|