@adonix.org/cloud-spark 0.0.108 → 0.0.110
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/dist/index.d.ts +311 -177
- package/dist/index.js +215 -170
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
[](https://www.npmjs.com/package/@adonix.org/cloud-spark)
|
|
4
4
|
[](https://github.com/adonix-org/cloud-spark/blob/main/LICENSE)
|
|
5
5
|
[](https://github.com/adonix-org/postrise/actions/workflows/build.yml)
|
|
6
|
-
[](https://sonarcloud.io/summary/
|
|
7
|
-
[](https://sonarcloud.io/summary/
|
|
8
|
-
[](https://sonarcloud.io/summary/
|
|
6
|
+
[](https://sonarcloud.io/summary/overall?id=adonix-org_cloud-spark&branch=main)
|
|
7
|
+
[](https://sonarcloud.io/summary/overall?id=adonix-org_cloud-spark&branch=main)
|
|
8
|
+
[](https://sonarcloud.io/summary/overall?id=adonix-org_cloud-spark&branch=main)
|
|
9
9
|
|
|
10
10
|
Ignite your Cloudflare Workers with a type-safe library for rapid development.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import CacheLib from 'cache-control-parser';
|
|
1
2
|
import { StatusCodes } from 'http-status-codes';
|
|
2
3
|
export { StatusCodes } from 'http-status-codes';
|
|
3
|
-
import CacheLib from 'cache-control-parser';
|
|
4
4
|
import { MatchFunction } from 'path-to-regexp';
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -223,136 +223,231 @@ interface Worker {
|
|
|
223
223
|
getAllowedMethods(): Method[];
|
|
224
224
|
}
|
|
225
225
|
|
|
226
|
-
|
|
226
|
+
/**
|
|
227
|
+
* Default CORS configuration used by `CorsProvider`.
|
|
228
|
+
*
|
|
229
|
+
* By default, all origins are allowed, only `Content-Type` is allowed as a header,
|
|
230
|
+
* no headers are exposed, and preflight caching is 1 week.
|
|
231
|
+
*
|
|
232
|
+
* @see {@link CorsConfig}
|
|
233
|
+
*/
|
|
234
|
+
declare const DEFAULT_CORS_CONFIG: Required<CorsConfig>;
|
|
235
|
+
/**
|
|
236
|
+
* Configuration options for `CorsProvider`.
|
|
237
|
+
*
|
|
238
|
+
* @see {@link DEFAULT_CORS_CONFIG}
|
|
239
|
+
*/
|
|
240
|
+
interface CorsConfig {
|
|
241
|
+
/** Origins allowed for CORS requests. */
|
|
242
|
+
allowedOrigins?: string[];
|
|
243
|
+
/** Allowed HTTP headers for CORS requests. */
|
|
244
|
+
allowedHeaders?: string[];
|
|
245
|
+
/** HTTP headers exposed to the client. */
|
|
246
|
+
exposedHeaders?: string[];
|
|
247
|
+
/** Max age in seconds for CORS preflight caching. */
|
|
248
|
+
maxAge?: number;
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Provides CORS settings for a worker.
|
|
252
|
+
*
|
|
253
|
+
* Combines a user-supplied `CorsConfig` with defaults. Used by
|
|
254
|
+
* `CorsHandler` to automatically set the appropriate headers.
|
|
255
|
+
*/
|
|
256
|
+
declare class CorsProvider {
|
|
257
|
+
private readonly config;
|
|
258
|
+
/**
|
|
259
|
+
* Create a new `CorsProvider`.
|
|
260
|
+
*
|
|
261
|
+
* @param config - Optional configuration to override defaults.
|
|
262
|
+
*/
|
|
263
|
+
constructor(config?: CorsConfig);
|
|
264
|
+
/** Returns the allowed origins. Default: all (`*`). */
|
|
265
|
+
getAllowedOrigins(): string[];
|
|
266
|
+
/** Returns the allowed HTTP headers. Default: `["Content-Type"]`. */
|
|
267
|
+
getAllowedHeaders(): string[];
|
|
268
|
+
/** Returns headers exposed to the client. Default: `[]`. */
|
|
269
|
+
getExposedHeaders(): string[];
|
|
270
|
+
/** Returns the max age in seconds for preflight requests. Default: 1 week. */
|
|
271
|
+
getMaxAge(): number;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Constants for common CORS headers.
|
|
275
|
+
*/
|
|
276
|
+
declare namespace Cors {
|
|
277
|
+
const MAX_AGE = "Access-Control-Max-Age";
|
|
278
|
+
const ALLOW_ORIGIN = "Access-Control-Allow-Origin";
|
|
279
|
+
const ALLOW_HEADERS = "Access-Control-Allow-Headers";
|
|
280
|
+
const ALLOW_METHODS = "Access-Control-Allow-Methods";
|
|
281
|
+
const EXPOSE_HEADERS = "Access-Control-Expose-Headers";
|
|
282
|
+
const ALLOW_CREDENTIALS = "Access-Control-Allow-Credentials";
|
|
283
|
+
const ALLOW_ALL_ORIGINS = "*";
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Adds or updates CORS headers on a Headers object according to the provided policy.
|
|
287
|
+
*
|
|
288
|
+
* Behavior:
|
|
289
|
+
* - Removes any existing CORS headers to avoid stale values.
|
|
290
|
+
* - If the request has no origin, the function exits early.
|
|
291
|
+
* - If wildcard `*` is allowed, sets Access-Control-Allow-Origin to `*`.
|
|
292
|
+
* - If the origin is explicitly allowed, sets the correct headers including credentials.
|
|
293
|
+
* - Optional headers (Expose-Headers, Allow-Headers, Allow-Methods, Max-Age) are always applied.
|
|
294
|
+
*
|
|
295
|
+
* @param cors The CorsProvider instance that determines allowed origins and headers
|
|
296
|
+
* @param headers The Headers object to update
|
|
297
|
+
*/
|
|
298
|
+
declare function addCorsHeaders(worker: Worker, cors: CorsProvider, headers: Headers): void;
|
|
299
|
+
/**
|
|
300
|
+
* Determines if a CORS policy allows any origin (`*`).
|
|
301
|
+
*
|
|
302
|
+
* @param cors - The `CorsProvider` instance to check.
|
|
303
|
+
* @returns `true` if the allowed origins include `"*"`, otherwise `false`.
|
|
304
|
+
*/
|
|
305
|
+
declare function allowAnyOrigin(cors: CorsProvider): boolean;
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Base class for building HTTP responses.
|
|
309
|
+
* Manages headers, status, and media type.
|
|
310
|
+
*/
|
|
227
311
|
declare abstract class BaseResponse {
|
|
312
|
+
readonly worker: Worker;
|
|
313
|
+
constructor(worker: Worker);
|
|
314
|
+
/** HTTP headers for the response. */
|
|
228
315
|
headers: Headers;
|
|
316
|
+
/** HTTP status code (default 200 OK). */
|
|
229
317
|
status: StatusCodes;
|
|
318
|
+
/** Optional status text. Defaults to standard reason phrase. */
|
|
230
319
|
statusText?: string;
|
|
320
|
+
/** Optional media type of the response body. */
|
|
231
321
|
mediaType?: MediaType;
|
|
322
|
+
/** Converts current state to ResponseInit for constructing a Response. */
|
|
232
323
|
protected get responseInit(): ResponseInit;
|
|
324
|
+
/** Sets a header, overwriting any existing value. */
|
|
233
325
|
setHeader(key: string, value: string | string[]): void;
|
|
326
|
+
/** Merges a header with existing values (does not overwrite). */
|
|
234
327
|
mergeHeader(key: string, value: string | string[]): void;
|
|
328
|
+
/** Adds a Content-Type header based on the media type if set. */
|
|
235
329
|
addContentType(): void;
|
|
236
330
|
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
protected getOrigin(): string | null;
|
|
242
|
-
}
|
|
243
|
-
declare abstract class CacheResponse extends CorsResponse {
|
|
331
|
+
/**
|
|
332
|
+
* Base response class that adds caching headers.
|
|
333
|
+
*/
|
|
334
|
+
declare abstract class CacheResponse extends BaseResponse {
|
|
244
335
|
cache?: CacheControl | undefined;
|
|
245
|
-
constructor(worker:
|
|
336
|
+
constructor(worker: Worker, cache?: CacheControl | undefined);
|
|
337
|
+
/** Adds Cache-Control header if caching is configured. */
|
|
246
338
|
protected addCacheHeader(): void;
|
|
247
339
|
}
|
|
340
|
+
/**
|
|
341
|
+
* Core worker response. Combines caching, and security headers.
|
|
342
|
+
*/
|
|
248
343
|
declare abstract class WorkerResponse extends CacheResponse {
|
|
249
344
|
private readonly body;
|
|
250
|
-
constructor(worker:
|
|
345
|
+
constructor(worker: Worker, body?: BodyInit | null, cache?: CacheControl);
|
|
346
|
+
/** Builds the Response object with body, headers, and status. */
|
|
251
347
|
getResponse(): Promise<Response>;
|
|
348
|
+
/** Adds default security headers. */
|
|
252
349
|
protected addSecurityHeaders(): void;
|
|
253
350
|
}
|
|
351
|
+
/**
|
|
352
|
+
* Wraps an existing Response and clones its body, headers, and status.
|
|
353
|
+
*/
|
|
254
354
|
declare class ClonedResponse extends WorkerResponse {
|
|
255
|
-
constructor(worker:
|
|
355
|
+
constructor(worker: Worker, response: Response, cache?: CacheControl);
|
|
256
356
|
}
|
|
357
|
+
/**
|
|
358
|
+
* Represents a successful response with customizable body and status.
|
|
359
|
+
*/
|
|
257
360
|
declare class SuccessResponse extends WorkerResponse {
|
|
258
|
-
constructor(worker:
|
|
361
|
+
constructor(worker: Worker, body?: BodyInit | null, cache?: CacheControl, status?: StatusCodes);
|
|
259
362
|
}
|
|
363
|
+
/**
|
|
364
|
+
* JSON response. Automatically sets Content-Type to application/json.
|
|
365
|
+
*/
|
|
260
366
|
declare class JsonResponse extends SuccessResponse {
|
|
261
|
-
constructor(worker:
|
|
262
|
-
}
|
|
263
|
-
declare class HtmlResponse extends SuccessResponse {
|
|
264
|
-
constructor(worker: CorsWorker, body: string, cache?: CacheControl, status?: StatusCodes);
|
|
265
|
-
}
|
|
266
|
-
declare class TextResponse extends SuccessResponse {
|
|
267
|
-
constructor(worker: CorsWorker, content: string, cache?: CacheControl, status?: StatusCodes);
|
|
367
|
+
constructor(worker: Worker, json?: unknown, cache?: CacheControl, status?: StatusCodes);
|
|
268
368
|
}
|
|
269
369
|
/**
|
|
270
|
-
*
|
|
370
|
+
* HTML response. Automatically sets Content-Type to text/html.
|
|
271
371
|
*/
|
|
272
|
-
declare class
|
|
273
|
-
constructor(worker:
|
|
274
|
-
}
|
|
275
|
-
declare class Options extends SuccessResponse {
|
|
276
|
-
constructor(worker: CorsWorker);
|
|
372
|
+
declare class HtmlResponse extends SuccessResponse {
|
|
373
|
+
constructor(worker: Worker, body: string, cache?: CacheControl, status?: StatusCodes);
|
|
277
374
|
}
|
|
278
|
-
|
|
279
375
|
/**
|
|
280
|
-
*
|
|
376
|
+
* Plain text response. Automatically sets Content-Type to text/plain.
|
|
281
377
|
*/
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
getAllowedOrigins(): string[];
|
|
285
|
-
/** Returns true if any origin is allowed (`*`). */
|
|
286
|
-
allowAnyOrigin(): boolean;
|
|
287
|
-
/** Returns the HTTP headers allowed by CORS. */
|
|
288
|
-
getAllowedHeaders(): string[];
|
|
289
|
-
/** Returns the HTTP headers that should be exposed to the browser. */
|
|
290
|
-
getExposedHeaders(): string[];
|
|
291
|
-
/** Returns the max age (in seconds) for CORS preflight caching. */
|
|
292
|
-
getMaxAge(): number;
|
|
378
|
+
declare class TextResponse extends SuccessResponse {
|
|
379
|
+
constructor(worker: Worker, content: string, cache?: CacheControl, status?: StatusCodes);
|
|
293
380
|
}
|
|
294
381
|
/**
|
|
295
|
-
*
|
|
382
|
+
* Response for HEAD requests. Clones headers but has no body.
|
|
296
383
|
*/
|
|
297
|
-
declare
|
|
298
|
-
|
|
299
|
-
const ALLOW_ORIGIN = "Access-Control-Allow-Origin";
|
|
300
|
-
const ALLOW_HEADERS = "Access-Control-Allow-Headers";
|
|
301
|
-
const ALLOW_METHODS = "Access-Control-Allow-Methods";
|
|
302
|
-
const EXPOSE_HEADERS = "Access-Control-Expose-Headers";
|
|
303
|
-
const ALLOW_CREDENTIALS = "Access-Control-Allow-Credentials";
|
|
304
|
-
const ALLOW_ALL_ORIGINS = "*";
|
|
384
|
+
declare class Head extends WorkerResponse {
|
|
385
|
+
constructor(worker: Worker, get: Response);
|
|
305
386
|
}
|
|
306
387
|
/**
|
|
307
|
-
*
|
|
308
|
-
*
|
|
309
|
-
* Behavior:
|
|
310
|
-
* - Removes any existing CORS headers to avoid stale values.
|
|
311
|
-
* - If the request has no origin, the function exits early.
|
|
312
|
-
* - If wildcard `*` is allowed, sets Access-Control-Allow-Origin to `*`.
|
|
313
|
-
* - If the origin is explicitly allowed, sets the correct headers including credentials and Vary: Origin.
|
|
314
|
-
* - Optional headers (Expose-Headers, Allow-Headers, Allow-Methods, Max-Age) are always applied.
|
|
315
|
-
*
|
|
316
|
-
* @param cors The CorsProvider instance that determines allowed origins and headers
|
|
317
|
-
* @param headers The Headers object to update
|
|
388
|
+
* Response for OPTIONS requests. Sets allowed methods and returns 204 No Content.
|
|
318
389
|
*/
|
|
319
|
-
declare
|
|
390
|
+
declare class Options extends SuccessResponse {
|
|
391
|
+
constructor(worker: Worker);
|
|
392
|
+
}
|
|
320
393
|
|
|
394
|
+
/** Structure for JSON error responses. */
|
|
321
395
|
interface ErrorJson {
|
|
396
|
+
/** HTTP status code. */
|
|
322
397
|
status: number;
|
|
398
|
+
/** Standard HTTP reason phrase. */
|
|
323
399
|
error: string;
|
|
400
|
+
/** Optional detailed message about the error. */
|
|
324
401
|
details: string;
|
|
325
402
|
}
|
|
403
|
+
/**
|
|
404
|
+
* Generic HTTP error response.
|
|
405
|
+
* Sends a JSON body with status, error message, and details.
|
|
406
|
+
*/
|
|
326
407
|
declare class HttpError extends JsonResponse {
|
|
327
408
|
protected readonly details?: string | undefined;
|
|
328
|
-
|
|
409
|
+
/**
|
|
410
|
+
* @param worker The worker handling the request.
|
|
411
|
+
* @param status HTTP status code.
|
|
412
|
+
* @param details Optional detailed error message.
|
|
413
|
+
*/
|
|
414
|
+
constructor(worker: Worker, status: StatusCodes, details?: string | undefined);
|
|
329
415
|
}
|
|
416
|
+
/** 400 Bad Request error response. */
|
|
330
417
|
declare class BadRequest extends HttpError {
|
|
331
|
-
constructor(worker:
|
|
418
|
+
constructor(worker: Worker, details?: string);
|
|
332
419
|
}
|
|
420
|
+
/** 401 Unauthorized error response. */
|
|
333
421
|
declare class Unauthorized extends HttpError {
|
|
334
|
-
constructor(worker:
|
|
422
|
+
constructor(worker: Worker, details?: string);
|
|
335
423
|
}
|
|
424
|
+
/** 403 Forbidden error response. */
|
|
336
425
|
declare class Forbidden extends HttpError {
|
|
337
|
-
constructor(worker:
|
|
426
|
+
constructor(worker: Worker, details?: string);
|
|
338
427
|
}
|
|
428
|
+
/** 404 Not Found error response. */
|
|
339
429
|
declare class NotFound extends HttpError {
|
|
340
|
-
constructor(worker:
|
|
430
|
+
constructor(worker: Worker, details?: string);
|
|
341
431
|
}
|
|
432
|
+
/** 405 Method Not Allowed error response. */
|
|
342
433
|
declare class MethodNotAllowed extends HttpError {
|
|
343
|
-
constructor(worker:
|
|
434
|
+
constructor(worker: Worker);
|
|
344
435
|
}
|
|
436
|
+
/** 500 Internal Server Error response. */
|
|
345
437
|
declare class InternalServerError extends HttpError {
|
|
346
|
-
constructor(worker:
|
|
438
|
+
constructor(worker: Worker, details?: string);
|
|
347
439
|
}
|
|
440
|
+
/** 501 Not Implemented error response. */
|
|
348
441
|
declare class NotImplemented extends HttpError {
|
|
349
|
-
constructor(worker:
|
|
442
|
+
constructor(worker: Worker, details?: string);
|
|
350
443
|
}
|
|
444
|
+
/** 501 Method Not Implemented error response for unsupported HTTP methods. */
|
|
351
445
|
declare class MethodNotImplemented extends NotImplemented {
|
|
352
|
-
constructor(worker:
|
|
446
|
+
constructor(worker: Worker);
|
|
353
447
|
}
|
|
448
|
+
/** 503 Service Unavailable error response. */
|
|
354
449
|
declare class ServiceUnavailable extends HttpError {
|
|
355
|
-
constructor(worker:
|
|
450
|
+
constructor(worker: Worker, details?: string);
|
|
356
451
|
}
|
|
357
452
|
|
|
358
453
|
/** Parameters extracted from a matched route */
|
|
@@ -420,6 +515,61 @@ declare class Routes implements Iterable<Route> {
|
|
|
420
515
|
[Symbol.iterator](): Iterator<Route>;
|
|
421
516
|
}
|
|
422
517
|
|
|
518
|
+
/**
|
|
519
|
+
* Abstract base class for Worker middleware.
|
|
520
|
+
* Provides a structured pre/post pattern around the next middleware or final handler,
|
|
521
|
+
* with optional short-circuit support.
|
|
522
|
+
*/
|
|
523
|
+
declare abstract class Middleware {
|
|
524
|
+
/**
|
|
525
|
+
* Implement this method to perform logic **before** the next middleware.
|
|
526
|
+
* Can inspect or modify the request via the worker instance.
|
|
527
|
+
* Return a Response to short-circuit the chain, or `undefined` to continue.
|
|
528
|
+
* @param worker The worker handling the request
|
|
529
|
+
*/
|
|
530
|
+
protected pre(_worker: Worker): void | Response | Promise<void | Response>;
|
|
531
|
+
/**
|
|
532
|
+
* Implement this method to perform logic **after** the next middleware.
|
|
533
|
+
* Can inspect or modify the response before it is returned.
|
|
534
|
+
* @param worker The worker handling the request
|
|
535
|
+
* @param response The Response returned from the next middleware or final handler
|
|
536
|
+
*/
|
|
537
|
+
protected post(_worker: Worker, _response: Response): void | Promise<void>;
|
|
538
|
+
/**
|
|
539
|
+
* Executes this middleware around the next middleware or final handler.
|
|
540
|
+
* Calls `pre`, then `next()` if not short-circuited, then `post`.
|
|
541
|
+
*/
|
|
542
|
+
handle(worker: Worker, next: () => Promise<Response>): Promise<Response>;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* Middleware that automatically applies CORS headers to responses.
|
|
547
|
+
*
|
|
548
|
+
* Uses a `CorsProvider` to determine the allowed origins, allowed headers,
|
|
549
|
+
* exposed headers, and max age for preflight caching.
|
|
550
|
+
*
|
|
551
|
+
* Can be registered with a `MiddlewareWorker` or any worker that supports middleware.
|
|
552
|
+
*/
|
|
553
|
+
declare class CorsHandler extends Middleware {
|
|
554
|
+
private readonly provider;
|
|
555
|
+
/**
|
|
556
|
+
* Create a new CorsHandler.
|
|
557
|
+
*
|
|
558
|
+
* @param provider - The `CorsProvider` that defines the CORS policy.
|
|
559
|
+
*/
|
|
560
|
+
constructor(provider?: CorsProvider);
|
|
561
|
+
/**
|
|
562
|
+
* Apply CORS headers to the outgoing response.
|
|
563
|
+
*
|
|
564
|
+
* Modifies the response in place according to the `CorsProvider`.
|
|
565
|
+
* Adds `Vary: Origin` if not allowing all origins.
|
|
566
|
+
*
|
|
567
|
+
* @param worker - Worker handling the request.
|
|
568
|
+
* @param response - Response returned from downstream middleware or final handler.
|
|
569
|
+
*/
|
|
570
|
+
protected post(worker: Worker, response: Response): void;
|
|
571
|
+
}
|
|
572
|
+
|
|
423
573
|
/**
|
|
424
574
|
* A type-safe Cloudflare Worker handler.
|
|
425
575
|
*
|
|
@@ -460,6 +610,16 @@ declare abstract class BaseWorker implements Worker {
|
|
|
460
610
|
get env(): Env;
|
|
461
611
|
/** Execution context for background tasks or `waitUntil` */
|
|
462
612
|
get ctx(): ExecutionContext;
|
|
613
|
+
/**
|
|
614
|
+
* Dispatches the incoming request to the appropriate handler and produces a response.
|
|
615
|
+
*
|
|
616
|
+
* Subclasses must implement this method to define how the worker generates a `Response`
|
|
617
|
+
* for the current request. This is the central point where request processing occurs,
|
|
618
|
+
* and where middleware chains, routing, or other custom behavior can be applied.
|
|
619
|
+
*
|
|
620
|
+
* @returns A Promise that resolves to the `Response` for the request.
|
|
621
|
+
*/
|
|
622
|
+
protected abstract dispatch(): Promise<Response>;
|
|
463
623
|
/**
|
|
464
624
|
* The DEFAULT allowed HTTP methods for subclasses.
|
|
465
625
|
*/
|
|
@@ -491,141 +651,115 @@ declare abstract class BaseWorker implements Worker {
|
|
|
491
651
|
}
|
|
492
652
|
|
|
493
653
|
/**
|
|
494
|
-
*
|
|
495
|
-
*
|
|
496
|
-
* Implements the `CorsProvider` interface and provides a standard policy:
|
|
497
|
-
* - Allows all origins (`*`) by default.
|
|
498
|
-
* - Allows the `Content-Type` header.
|
|
499
|
-
* - Exposes no additional headers.
|
|
500
|
-
* - Sets CORS preflight max-age to one week.
|
|
501
|
-
*
|
|
502
|
-
* Subclasses can override any of the methods to customize the CORS behavior.
|
|
654
|
+
* Worker that supports class-based middleware with optional always-run middleware.
|
|
655
|
+
* Extends CacheWorker to include caching logic automatically.
|
|
503
656
|
*/
|
|
504
|
-
declare abstract class
|
|
505
|
-
|
|
506
|
-
allowAnyOrigin(): boolean;
|
|
507
|
-
getAllowedHeaders(): string[];
|
|
508
|
-
getExposedHeaders(): string[];
|
|
509
|
-
getMaxAge(): number;
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
/**
|
|
513
|
-
* Abstract worker class that adds caching support for GET requests.
|
|
514
|
-
*
|
|
515
|
-
* Behavior:
|
|
516
|
-
* - Caches successful GET responses (`response.ok === true`) in the selected cache.
|
|
517
|
-
* - Strips CORS headers from cached responses; all other origin headers are preserved.
|
|
518
|
-
* - Dynamically adds CORS headers to cached responses when returned to the client.
|
|
519
|
-
*
|
|
520
|
-
* Subclasses should override `getCacheKey()` to customize cache key generation if needed.
|
|
521
|
-
*/
|
|
522
|
-
declare abstract class CacheWorker extends CorsDefaults {
|
|
657
|
+
declare abstract class MiddlewareWorker extends BaseWorker {
|
|
658
|
+
private readonly middlewares;
|
|
523
659
|
/**
|
|
524
|
-
*
|
|
525
|
-
*
|
|
526
|
-
* Behavior:
|
|
527
|
-
* - By default, returns the normalized request URL.
|
|
528
|
-
* - Query parameters are normalized so that the order does not affect the cache key.
|
|
529
|
-
* For example, `?a=1&b=2` and `?b=2&a=1` produce the same cache key.
|
|
530
|
-
*
|
|
531
|
-
* Subclasses may override this method to implement custom cache key strategies.
|
|
532
|
-
*
|
|
533
|
-
* @returns {URL | RequestInfo} The URL or RequestInfo used as the cache key.
|
|
660
|
+
* Register a middleware instance.
|
|
661
|
+
* @param mw Middleware to register
|
|
534
662
|
*/
|
|
535
|
-
|
|
663
|
+
use(mw: Middleware): this;
|
|
536
664
|
/**
|
|
537
|
-
*
|
|
665
|
+
* Fetch the current request through the registered middleware chain
|
|
666
|
+
* and ultimately to the final worker handler.
|
|
538
667
|
*
|
|
539
|
-
*
|
|
540
|
-
*
|
|
541
|
-
*
|
|
542
|
-
* - Returns `undefined` if no cached response is found.
|
|
543
|
-
* - Cloudflare dynamic headers (`CF-Cache-Status`, `Age`, `Connection`, etc.) will
|
|
544
|
-
* always be present on the returned response, even though they are not stored in the cache.
|
|
668
|
+
* Middleware are executed in **last-registered-first-called** order (via reduceRight).
|
|
669
|
+
* Each middleware receives the worker instance and a `next` function to call the
|
|
670
|
+
* next middleware or final handler.
|
|
545
671
|
*
|
|
546
|
-
* @
|
|
547
|
-
*
|
|
548
|
-
* @see {@link setCachedResponse}
|
|
549
|
-
* @see {@link getCacheKey}
|
|
672
|
+
* @returns A Promise resolving to the Response returned by the middleware chain
|
|
673
|
+
* or the final handler.
|
|
550
674
|
*/
|
|
551
|
-
|
|
675
|
+
fetch(): Promise<Response>;
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
/**
|
|
679
|
+
* Base worker class providing HTTP method dispatching, caching, and error handling.
|
|
680
|
+
* Extends `CacheWorker` and defines default implementations for HTTP methods.
|
|
681
|
+
*/
|
|
682
|
+
declare abstract class BasicWorker extends MiddlewareWorker {
|
|
552
683
|
/**
|
|
553
|
-
*
|
|
554
|
-
*
|
|
555
|
-
* Behavior:
|
|
556
|
-
* - Only caches successful GET responses (`response.ok === true`).
|
|
557
|
-
* - Strips headers via `removeCacheHeaders` before storing.
|
|
558
|
-
* - Uses `ctx.waitUntil` to perform caching asynchronously without blocking the response.
|
|
559
|
-
* - All other origin headers (e.g., Cache-Control, Expires) are preserved.
|
|
560
|
-
*
|
|
561
|
-
* @param {Response} response The Response to cache.
|
|
562
|
-
* @param {string} [cacheName] Optional named cache; defaults to `caches.default`.
|
|
563
|
-
* @see {@link getCachedResponse}
|
|
564
|
-
* @see {@link getCacheKey}
|
|
684
|
+
* Entry point to handle a fetch request.
|
|
685
|
+
* Checks allowed methods, serves cached responses, or dispatches to the appropriate handler.
|
|
565
686
|
*/
|
|
566
|
-
|
|
687
|
+
fetch(): Promise<Response>;
|
|
567
688
|
/**
|
|
568
|
-
*
|
|
569
|
-
*
|
|
570
|
-
* This method only affects the caching behavior provided by the library’s
|
|
571
|
-
* `getCachedResponse()` and `setCachedResponse()` methods. It does **not**
|
|
572
|
-
* prevent users from manually reading from or writing to `caches.default`
|
|
573
|
-
* or any other Cache API. By default, this returns `true`, enabling the
|
|
574
|
-
* library’s default caching behavior.
|
|
575
|
-
*
|
|
576
|
-
* Subclasses can override this method to disable automatic caching in
|
|
577
|
-
* development environments, for certain pathnames, or based on
|
|
578
|
-
* environment variables.
|
|
579
|
-
*
|
|
580
|
-
* @returns {boolean} `true` if the library should use its default caching,
|
|
581
|
-
* `false` to disable the library cache.
|
|
689
|
+
* Dispatches the request to the method-specific handler.
|
|
690
|
+
* Defaults to MethodNotAllowed if the HTTP method is not recognized.
|
|
582
691
|
*/
|
|
583
|
-
protected
|
|
692
|
+
protected dispatch(): Promise<Response>;
|
|
584
693
|
/**
|
|
585
|
-
*
|
|
586
|
-
*
|
|
587
|
-
* @param {Response} cached The cached Response.
|
|
588
|
-
* @returns {Response} A new Response with dynamic CORS headers applied.
|
|
589
|
-
* @see {@link removeCacheHeaders}
|
|
694
|
+
* Hook for subclasses to perform any setup, e.g., registering middleware,
|
|
695
|
+
* overriding defaults, or configuring state.
|
|
590
696
|
*/
|
|
591
|
-
protected
|
|
697
|
+
protected setup(): void;
|
|
592
698
|
/**
|
|
593
|
-
*
|
|
594
|
-
*
|
|
595
|
-
* @
|
|
596
|
-
* @returns {Response} A new Response with excluded headers removed.
|
|
597
|
-
* @see {@link addCacheHeaders}
|
|
699
|
+
* Checks if the given HTTP method is allowed for this worker.
|
|
700
|
+
* @param method HTTP method string
|
|
701
|
+
* @returns true if the method is allowed
|
|
598
702
|
*/
|
|
599
|
-
protected removeCacheHeaders(response: Response): Response;
|
|
600
|
-
/**
|
|
601
|
-
* Returns the list of headers to exclude from the cached response.
|
|
602
|
-
* By default, excludes only dynamic CORS headers.
|
|
603
|
-
*
|
|
604
|
-
* @returns {string[]} Array of header names to exclude.
|
|
605
|
-
* @see {@link removeCacheHeaders}
|
|
606
|
-
*/
|
|
607
|
-
protected excludeCacheHeaders(): string[];
|
|
608
|
-
}
|
|
609
|
-
|
|
610
|
-
declare abstract class BasicWorker extends CacheWorker {
|
|
611
|
-
fetch(): Promise<Response>;
|
|
612
|
-
protected dispatch(): Promise<Response>;
|
|
613
703
|
isAllowed(method: string): boolean;
|
|
704
|
+
/** Default handler for GET requests. Returns MethodNotImplemented unless overridden. */
|
|
614
705
|
protected get(): Promise<Response>;
|
|
706
|
+
/** Default handler for PUT requests. Returns MethodNotImplemented unless overridden. */
|
|
615
707
|
protected put(): Promise<Response>;
|
|
708
|
+
/** Default handler for POST requests. Returns MethodNotImplemented unless overridden. */
|
|
616
709
|
protected post(): Promise<Response>;
|
|
710
|
+
/** Default handler for PATCH requests. Returns MethodNotImplemented unless overridden. */
|
|
617
711
|
protected patch(): Promise<Response>;
|
|
712
|
+
/** Default handler for DELETE requests. Returns MethodNotImplemented unless overridden. */
|
|
618
713
|
protected delete(): Promise<Response>;
|
|
714
|
+
/**
|
|
715
|
+
* Default handler for OPTIONS requests.
|
|
716
|
+
* Returns an Options response.
|
|
717
|
+
*
|
|
718
|
+
* Typically does not need to be overridden.
|
|
719
|
+
*/
|
|
619
720
|
protected options(): Promise<Response>;
|
|
721
|
+
/**
|
|
722
|
+
* Default handler for HEAD requests.
|
|
723
|
+
* Performs a GET request internally and removes the body for HEAD semantics.
|
|
724
|
+
*
|
|
725
|
+
* Usually does not need to be overridden, as this behavior covers standard HEAD requirements.
|
|
726
|
+
*/
|
|
620
727
|
protected head(): Promise<Response>;
|
|
621
|
-
|
|
728
|
+
/**
|
|
729
|
+
* Helper to construct a WorkerResponse of the given class with arguments.
|
|
730
|
+
* @param ResponseClass The response class to instantiate
|
|
731
|
+
* @param args Additional constructor arguments
|
|
732
|
+
* @returns The final Response object
|
|
733
|
+
*/
|
|
734
|
+
protected getResponse<T extends WorkerResponse, Ctor extends new (worker: Worker, ...args: any[]) => T>(ResponseClass: Ctor, ...args: ConstructorParameters<Ctor> extends [Worker, ...infer R] ? R : never): Promise<Response>;
|
|
622
735
|
}
|
|
623
736
|
|
|
737
|
+
/**
|
|
738
|
+
* Abstract worker that provides routing capabilities.
|
|
739
|
+
* Extends `BasicWorker` and uses a `Routes` table to map HTTP methods and paths
|
|
740
|
+
* to handler callbacks.
|
|
741
|
+
*/
|
|
624
742
|
declare abstract class RouteWorker extends BasicWorker {
|
|
743
|
+
/** Routing table used for registering and matching routes. */
|
|
625
744
|
protected readonly routes: Routes;
|
|
626
|
-
|
|
745
|
+
/**
|
|
746
|
+
* Loads routes from a `RouteTable` into this worker's route table.
|
|
747
|
+
* @param table The table of routes to load.
|
|
748
|
+
*/
|
|
627
749
|
protected load(table: RouteTable): void;
|
|
750
|
+
/**
|
|
751
|
+
* Adds a single route to this worker.
|
|
752
|
+
* @param method HTTP method (GET, POST, etc.)
|
|
753
|
+
* @param path Route path
|
|
754
|
+
* @param callback Function to handle requests matching this route
|
|
755
|
+
* @returns The worker instance (for chaining)
|
|
756
|
+
*/
|
|
628
757
|
protected add(method: Method, path: string, callback: RouteCallback): this;
|
|
758
|
+
/**
|
|
759
|
+
* Matches the incoming request against registered routes and executes
|
|
760
|
+
* the corresponding callback. Falls back to `BasicWorker.dispatch()` if no match.
|
|
761
|
+
* @returns The response from the matched route or the default handler.
|
|
762
|
+
*/
|
|
629
763
|
protected dispatch(): Promise<Response>;
|
|
630
764
|
protected get(): Promise<Response>;
|
|
631
765
|
protected put(): Promise<Response>;
|
|
@@ -634,4 +768,4 @@ declare abstract class RouteWorker extends BasicWorker {
|
|
|
634
768
|
protected delete(): Promise<Response>;
|
|
635
769
|
}
|
|
636
770
|
|
|
637
|
-
export { BadRequest, BasicWorker, CacheControl, ClonedResponse, Cors, type
|
|
771
|
+
export { BadRequest, BasicWorker, CacheControl, ClonedResponse, Cors, type CorsConfig, CorsHandler, CorsProvider, DEFAULT_CORS_CONFIG, type ErrorJson, Forbidden, Head, HtmlResponse, HttpError, HttpHeader, InternalServerError, JsonResponse, type MatchedRoute, MediaType, Method, MethodNotAllowed, MethodNotImplemented, Middleware, NotFound, NotImplemented, Options, type Route, type RouteCallback, type RouteParams, type RouteTable, type RouteTuple, RouteWorker, Routes, ServiceUnavailable, SuccessResponse, TextResponse, Time, Unauthorized, type Worker, type WorkerConstructor, WorkerResponse, addCorsHeaders, allowAnyOrigin, getContentType, getOrigin, isMethod, lexCompare, mergeHeader, normalizeUrl, setHeader };
|