@adonix.org/cloud-spark 0.0.93 → 0.0.96

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 CHANGED
@@ -2,113 +2,6 @@ import CacheLib from 'cache-control-parser';
2
2
  import { StatusCodes } from 'http-status-codes';
3
3
  export { StatusCodes } from 'http-status-codes';
4
4
 
5
- /**
6
- * Represents the constructor of a Worker or a subclass of Worker.
7
- *
8
- * @template T - The specific type of Worker being constructed. Defaults to `Worker`.
9
- * @param req - The `Request` object to be handled by the worker instance.
10
- * @param env - The environment bindings available to the worker.
11
- * @param ctx - The `ExecutionContext` for the worker invocation.
12
- * @returns An instance of the worker type `T`.
13
- */
14
- type WorkerConstructor<T extends Worker = Worker> = new (req: Request, env: Env, ctx: ExecutionContext) => T;
15
- /**
16
- * Defines the contract for a Cloudflare-compatible Worker.
17
- *
18
- * Implementations are responsible for handling incoming requests,
19
- * providing access to the request, environment bindings, and
20
- * execution context.
21
- */
22
- interface Worker {
23
- /**
24
- * Processes the incoming {@link Request} and produces a {@link Response}.
25
- *
26
- * @returns A Promise that resolves to the HTTP {@link Response}.
27
- */
28
- fetch(): Promise<Response>;
29
- /**
30
- * The original {@link Request} being processed by this worker instance.
31
- */
32
- get request(): Request;
33
- /**
34
- * The environment bindings provided at runtime (e.g., KV, R2, secrets).
35
- */
36
- get env(): Env;
37
- /**
38
- * The {@link ExecutionContext} associated with the current request,
39
- * used to manage background tasks and request lifecycle.
40
- */
41
- get ctx(): ExecutionContext;
42
- }
43
-
44
- /**
45
- * A type-safe Cloudflare Worker handler where `fetch` is required.
46
- *
47
- * Extends `ExportedHandler` but guarantees that the `fetch` method exists
48
- * and has the correct signature for Cloudflare Worker invocation.
49
- *
50
- * @template E - The type of environment bindings passed to the worker. Defaults to `Env`.
51
- */
52
- interface FetchHandler<E = Env> extends ExportedHandler<E> {
53
- /**
54
- * Handles an incoming request and produces a response.
55
- *
56
- * @param request - The incoming `Request` object.
57
- * @param env - Environment bindings (e.g., KV namespaces, secrets, Durable Objects).
58
- * @param ctx - Optional execution context for background tasks (`waitUntil`).
59
- * @returns A `Promise` that resolves to the response.
60
- */
61
- fetch: (request: Request, env: E, ctx: ExecutionContext) => Promise<Response>;
62
- }
63
- /**
64
- * Provides the foundational structure for handling requests, environment bindings,
65
- * and the worker execution context. Subclasses are expected to implement the
66
- * `fetch` method to handle the request and return a Response.
67
- *
68
- * Features:
69
- * - Holds the current `Request` object (`request` getter).
70
- * - Provides access to environment bindings (`env` getter).
71
- * - Provides access to the worker execution context (`ctx` getter), if available.
72
- * - Subclasses must implement `fetch()` to process the request.
73
- */
74
- declare abstract class BaseWorker implements Worker {
75
- private readonly _request;
76
- private readonly _env;
77
- private readonly _ctx;
78
- constructor(_request: Request, _env: Env, _ctx: ExecutionContext);
79
- /** The Request object associated with this worker invocation */
80
- get request(): Request;
81
- /** Environment bindings (e.g., KV, secrets, or other globals) */
82
- get env(): Env;
83
- /** Optional execution context for background tasks or `waitUntil` */
84
- get ctx(): ExecutionContext;
85
- /**
86
- * Creates a new instance of the current Worker subclass.
87
- *
88
- * @param request - The request to pass to the new worker instance.
89
- * @returns A new worker instance of the same subclass as `this`.
90
- */
91
- protected createWorker(request: Request): this;
92
- /**
93
- * Process the request and produce a Response.
94
- * Subclasses must implement this method.
95
- *
96
- * @returns A Promise resolving to the Response for the request
97
- */
98
- abstract fetch(): Promise<Response>;
99
- /**
100
- * **Ignite** your `Worker` implementation into a Cloudflare-compatible handler.
101
- *
102
- * @returns A `FetchHandler` that launches a new worker instance for each request.
103
- *
104
- * @example
105
- * ```ts
106
- * export default MyWorker.ignite();
107
- * ```
108
- */
109
- static ignite<W extends Worker>(this: WorkerConstructor<W>): FetchHandler;
110
- }
111
-
112
5
  /**
113
6
  * @see {@link https://github.com/etienne-martin/cache-control-parser | cache-control-parser}
114
7
  */
@@ -116,6 +9,13 @@ type CacheControl = CacheLib.CacheControl;
116
9
  declare const CacheControl: {
117
10
  parse: (cacheControlHeader: string) => CacheLib.CacheControl;
118
11
  stringify: (cacheControl: CacheLib.CacheControl) => string;
12
+ /** A Cache-Control directive that disables all caching. */
13
+ DISABLE: Readonly<{
14
+ "no-cache": true;
15
+ "no-store": true;
16
+ "must-revalidate": true;
17
+ "max-age": 0;
18
+ }>;
119
19
  };
120
20
 
121
21
  /**
@@ -123,14 +23,15 @@ declare const CacheControl: {
123
23
  */
124
24
  declare namespace HttpHeader {
125
25
  const VARY = "Vary";
26
+ const ALLOW = "Allow";
126
27
  const CONTENT_TYPE = "Content-Type";
127
28
  const CACHE_CONTROL = "Cache-Control";
128
- const X_CONTENT_TYPE_OPTIONS = "X-Content-Type-Options";
129
29
  const X_FRAME_OPTIONS = "X-Frame-Options";
130
- const STRICT_TRANSPORT_SECURITY = "Strict-Transport-Security";
131
- const CONTENT_SECURITY_POLICY = "Content-Security-Policy";
30
+ const X_CONTENT_TYPE_OPTIONS = "X-Content-Type-Options";
132
31
  const REFERRER_POLICY = "Referrer-Policy";
133
32
  const PERMISSIONS_POLICY = "Permissions-Policy";
33
+ const CONTENT_SECURITY_POLICY = "Content-Security-Policy";
34
+ const STRICT_TRANSPORT_SECURITY = "Strict-Transport-Security";
134
35
  const NOSNIFF = "nosniff";
135
36
  const ORIGIN = "Origin";
136
37
  }
@@ -152,10 +53,10 @@ declare const Time: {
152
53
  declare enum Method {
153
54
  GET = "GET",
154
55
  PUT = "PUT",
56
+ HEAD = "HEAD",
155
57
  POST = "POST",
156
58
  PATCH = "PATCH",
157
59
  DELETE = "DELETE",
158
- HEAD = "HEAD",
159
60
  OPTIONS = "OPTIONS"
160
61
  }
161
62
  /**
@@ -288,12 +189,12 @@ interface CorsProvider {
288
189
  * Constants for common CORS headers.
289
190
  */
290
191
  declare namespace Cors {
192
+ const MAX_AGE = "Access-Control-Max-Age";
291
193
  const ALLOW_ORIGIN = "Access-Control-Allow-Origin";
292
- const ALLOW_CREDENTIALS = "Access-Control-Allow-Credentials";
293
- const EXPOSE_HEADERS = "Access-Control-Expose-Headers";
294
194
  const ALLOW_HEADERS = "Access-Control-Allow-Headers";
295
195
  const ALLOW_METHODS = "Access-Control-Allow-Methods";
296
- const MAX_AGE = "Access-Control-Max-Age";
196
+ const EXPOSE_HEADERS = "Access-Control-Expose-Headers";
197
+ const ALLOW_CREDENTIALS = "Access-Control-Allow-Credentials";
297
198
  const ALLOW_ALL_ORIGINS = "*";
298
199
  }
299
200
  /**
@@ -312,105 +213,92 @@ declare namespace Cors {
312
213
  declare function addCorsHeaders(origin: string | null, cors: CorsProvider, headers: Headers): void;
313
214
 
314
215
  /**
315
- * Abstract base class for Workers to provide a default CORS policy.
216
+ * A callback function for a route.
316
217
  *
317
- * Implements the `CorsProvider` interface and provides a standard policy:
318
- * - Allows all origins (`*`) by default.
319
- * - Allows GET, OPTIONS, and HEAD methods.
320
- * - Allows the `Content-Type` header.
321
- * - Exposes no additional headers.
322
- * - Sets CORS preflight max-age to one week.
218
+ * @param matches - Captured groups from the route RegExp, or the full match at index 0
219
+ * @returns A Response object or a Promise that resolves to a Response
220
+ */
221
+ type RouteCallback = (...matches: string[]) => Response | Promise<Response>;
222
+ /**
223
+ * Tuple used to initialize a route.
323
224
  *
324
- * Subclasses can override any of the methods to customize the CORS behavior.
225
+ * [HTTP method, path pattern (string or RegExp), callback function]
325
226
  */
326
- declare abstract class CorsWorker$1 extends BaseWorker implements CorsProvider {
327
- getAllowOrigins(): string[];
328
- allowAnyOrigin(): boolean;
329
- getAllowMethods(): Method[];
330
- getAllowHeaders(): string[];
331
- getExposeHeaders(): string[];
332
- getMaxAge(): number;
227
+ type RouteInit = [Method, RegExp | string, RouteCallback];
228
+ /**
229
+ * Represents a single route with a pattern and a callback.
230
+ */
231
+ declare class Route {
232
+ readonly callback: RouteCallback;
233
+ readonly pattern: RegExp;
234
+ /**
235
+ * @param pattern - A RegExp or string used to match the request path
236
+ * @param callback - Function to handle requests matching the pattern
237
+ */
238
+ constructor(pattern: RegExp | string, callback: RouteCallback);
333
239
  }
334
-
335
240
  /**
336
- * Abstract worker class that adds caching support for GET requests.
337
- *
338
- * Behavior:
339
- * - Caches successful GET responses (`response.ok === true`) in the selected cache.
340
- * - Strips CORS headers from cached responses; all other origin headers are preserved.
341
- * - Dynamically adds CORS headers to cached responses when returned to the client.
241
+ * A collection of routes grouped by HTTP method.
342
242
  *
343
- * Subclasses should override `getCacheKey()` to customize cache key generation if needed.
243
+ * Supports adding routes and retrieving the first matching route for a given method and URL.
344
244
  */
345
- declare abstract class CacheWorker extends CorsWorker$1 {
245
+ declare class Routes {
246
+ private readonly map;
346
247
  /**
347
- * Returns the cache key for the current request.
348
- *
349
- * Behavior:
350
- * - By default, returns the normalized request URL.
351
- * - Query parameters are normalized so that the order does not affect the cache key.
352
- * For example, `?a=1&b=2` and `?b=2&a=1` produce the same cache key.
353
- *
354
- * Subclasses may override this method to implement custom cache key strategies.
248
+ * Adds a route to the collection under the given HTTP method.
355
249
  *
356
- * @returns {URL | RequestInfo} The URL or RequestInfo used as the cache key.
250
+ * @param method - HTTP method (GET, POST, etc.)
251
+ * @param route - Route instance to add
252
+ * @returns The Routes instance (for chaining)
357
253
  */
358
- protected getCacheKey(): URL | RequestInfo;
254
+ add(method: Method, route: Route): this;
359
255
  /**
360
- * Retrieves a cached Response for the current request, if one exists.
361
- *
362
- * Behavior:
363
- * - Only GET requests are considered.
364
- * - Returns a new Response with dynamic CORS headers applied via `addCacheHeaders`.
365
- * - Returns `undefined` if no cached response is found.
366
- * - Cloudflare dynamic headers (`CF-Cache-Status`, `Age`, `Connection`, etc.) will
367
- * always be present on the returned response, even though they are not stored in the cache.
256
+ * Finds the first route that matches the given method and URL.
368
257
  *
369
- * @param {string} [cacheName] Optional named cache; defaults to `caches.default`.
370
- * @returns {Promise<Response | undefined>} A Response with CORS headers, or undefined.
371
- * @see {@link setCachedResponse}
372
- * @see {@link getCacheKey}
258
+ * @param method - HTTP method of the request
259
+ * @param url - Full URL string of the request
260
+ * @returns The first matching Route, or undefined if none match
373
261
  */
374
- protected getCachedResponse(cacheName?: string): Promise<Response | undefined>;
262
+ get(method: Method, url: string): Route | undefined;
263
+ }
264
+
265
+ /**
266
+ * Represents the constructor of a Worker or a subclass of Worker.
267
+ *
268
+ * @template T - The specific type of Worker being constructed. Defaults to `Worker`.
269
+ * @param req - The `Request` object to be handled by the worker instance.
270
+ * @param env - The environment bindings available to the worker.
271
+ * @param ctx - The `ExecutionContext` for the worker invocation.
272
+ * @returns An instance of the worker type `T`.
273
+ */
274
+ type WorkerConstructor<T extends Worker = Worker> = new (req: Request, env: Env, ctx: ExecutionContext) => T;
275
+ /**
276
+ * Defines the contract for a Cloudflare-compatible Worker.
277
+ *
278
+ * Implementations are responsible for handling incoming requests,
279
+ * providing access to the request, environment bindings, and
280
+ * execution context.
281
+ */
282
+ interface Worker {
375
283
  /**
376
- * Stores a Response in the cache for the current request.
377
- *
378
- * Behavior:
379
- * - Only caches successful GET responses (`response.ok === true`).
380
- * - Strips headers via `removeCacheHeaders` before storing.
381
- * - Uses `ctx.waitUntil` to perform caching asynchronously without blocking the response.
382
- * - All other origin headers (e.g., Cache-Control, Expires) are preserved.
284
+ * Processes the incoming {@link Request} and produces a {@link Response}.
383
285
  *
384
- * @param {Response} response The Response to cache.
385
- * @param {string} [cacheName] Optional named cache; defaults to `caches.default`.
386
- * @see {@link getCachedResponse}
387
- * @see {@link getCacheKey}
286
+ * @returns A Promise that resolves to the HTTP {@link Response}.
388
287
  */
389
- protected setCachedResponse(response: Response, cacheName?: string): Promise<void>;
288
+ fetch(): Promise<Response>;
390
289
  /**
391
- * Adds headers to a cached response.
392
- *
393
- * @param {Response} cached The cached Response.
394
- * @returns {Response} A new Response with dynamic CORS headers applied.
395
- * @see {@link removeCacheHeaders}
290
+ * The original {@link Request} being processed by this worker instance.
396
291
  */
397
- private addCacheHeaders;
292
+ get request(): Request;
398
293
  /**
399
- * Removes headers that should not be stored in the cache (currently only CORS headers).
400
- *
401
- * @param {Response} response The Response to clean before caching.
402
- * @returns {Response} A new Response with excluded headers removed.
403
- * @see {@link addCacheHeaders}
294
+ * The environment bindings provided at runtime (e.g., KV, R2, secrets).
404
295
  */
405
- private removeCacheHeaders;
296
+ get env(): Env;
406
297
  /**
407
- * Returns the list of headers to exclude from the cached response.
408
- * By default, excludes only dynamic CORS headers.
409
- *
410
- * @returns {string[]} Array of header names to exclude.
411
- * @see {@link removeCacheHeaders}
298
+ * The {@link ExecutionContext} associated with the current request,
299
+ * used to manage background tasks and request lifecycle.
412
300
  */
413
- protected excludeCacheHeaders(): string[];
301
+ get ctx(): ExecutionContext;
414
302
  }
415
303
 
416
304
  interface ErrorJson {
@@ -424,28 +312,28 @@ interface ErrorJson {
424
312
  * Used by response builders that require both Worker
425
313
  * and CORS functionality.
426
314
  */
427
- type CorsWorker = Worker & CorsProvider;
315
+ type CorsWorker$1 = Worker & CorsProvider;
428
316
  declare abstract class BaseResponse {
429
- readonly worker: CorsWorker;
317
+ readonly worker: CorsWorker$1;
430
318
  headers: Headers;
431
319
  body: BodyInit | null;
432
320
  status: StatusCodes;
433
321
  statusText?: string;
434
322
  mediaType?: MediaType;
435
- constructor(worker: CorsWorker, content?: BodyInit | null);
323
+ constructor(worker: CorsWorker$1, content?: BodyInit | null);
436
324
  protected get responseInit(): ResponseInit;
437
325
  setHeader(key: string, value: string | string[]): void;
438
326
  mergeHeader(key: string, value: string | string[]): void;
439
327
  addContentType(): void;
440
328
  }
441
329
  declare abstract class CorsResponse extends BaseResponse {
442
- constructor(worker: CorsWorker, content?: BodyInit | null);
330
+ constructor(worker: CorsWorker$1, content?: BodyInit | null);
443
331
  protected addCorsHeaders(): void;
444
332
  protected getOrigin(): string | null;
445
333
  }
446
334
  declare abstract class CacheResponse extends CorsResponse {
447
335
  cache?: CacheControl | undefined;
448
- constructor(worker: CorsWorker, body?: BodyInit | null, cache?: CacheControl | undefined);
336
+ constructor(worker: CorsWorker$1, body?: BodyInit | null, cache?: CacheControl | undefined);
449
337
  protected addCacheHeaders(): void;
450
338
  }
451
339
  declare abstract class WorkerResponse extends CacheResponse {
@@ -453,131 +341,249 @@ declare abstract class WorkerResponse extends CacheResponse {
453
341
  protected addSecurityHeaders(): void;
454
342
  }
455
343
  declare class ClonedResponse extends WorkerResponse {
456
- constructor(worker: CorsWorker, response: Response, cache?: CacheControl);
344
+ constructor(worker: CorsWorker$1, response: Response, cache?: CacheControl);
457
345
  }
458
346
  declare class SuccessResponse extends WorkerResponse {
459
- constructor(worker: CorsWorker, body?: BodyInit | null, cache?: CacheControl, status?: StatusCodes);
347
+ constructor(worker: CorsWorker$1, body?: BodyInit | null, cache?: CacheControl, status?: StatusCodes);
460
348
  }
461
349
  declare class JsonResponse extends SuccessResponse {
462
- constructor(worker: CorsWorker, json?: unknown, cache?: CacheControl, status?: StatusCodes);
350
+ constructor(worker: CorsWorker$1, json?: unknown, cache?: CacheControl, status?: StatusCodes);
463
351
  }
464
352
  declare class HtmlResponse extends SuccessResponse {
465
- constructor(worker: CorsWorker, body: string, cache?: CacheControl, status?: StatusCodes);
353
+ constructor(worker: CorsWorker$1, body: string, cache?: CacheControl, status?: StatusCodes);
466
354
  }
467
355
  declare class TextResponse extends SuccessResponse {
468
- constructor(worker: CorsWorker, content: string, cache?: CacheControl, status?: StatusCodes);
356
+ constructor(worker: CorsWorker$1, content: string, cache?: CacheControl, status?: StatusCodes);
469
357
  }
470
358
  /**
471
359
  * Removes the body from a GET response.
472
360
  */
473
361
  declare class Head extends WorkerResponse {
474
- constructor(worker: CorsWorker, get: Response);
362
+ constructor(worker: CorsWorker$1, get: Response);
475
363
  }
476
364
  declare class Options extends SuccessResponse {
477
- constructor(worker: CorsWorker);
365
+ constructor(worker: CorsWorker$1);
478
366
  }
479
367
  declare class HttpError extends JsonResponse {
480
368
  protected readonly details?: string | undefined;
481
- constructor(worker: CorsWorker, status: StatusCodes, details?: string | undefined);
369
+ constructor(worker: CorsWorker$1, status: StatusCodes, details?: string | undefined);
482
370
  get json(): ErrorJson;
483
371
  createResponse(): Response;
484
372
  }
485
373
  declare class BadRequest extends HttpError {
486
- constructor(worker: CorsWorker, details?: string);
374
+ constructor(worker: CorsWorker$1, details?: string);
487
375
  }
488
376
  declare class Unauthorized extends HttpError {
489
- constructor(worker: CorsWorker, details?: string);
377
+ constructor(worker: CorsWorker$1, details?: string);
490
378
  }
491
379
  declare class Forbidden extends HttpError {
492
- constructor(worker: CorsWorker, details?: string);
380
+ constructor(worker: CorsWorker$1, details?: string);
493
381
  }
494
382
  declare class NotFound extends HttpError {
495
- constructor(worker: CorsWorker, details?: string);
383
+ constructor(worker: CorsWorker$1, details?: string);
496
384
  }
497
385
  declare class MethodNotAllowed extends HttpError {
498
- constructor(worker: CorsWorker, method: string);
386
+ constructor(worker: CorsWorker$1);
499
387
  get json(): ErrorJson & {
500
388
  allowed: Method[];
501
389
  };
502
390
  }
503
391
  declare class InternalServerError extends HttpError {
504
- constructor(worker: CorsWorker, details?: string);
392
+ constructor(worker: CorsWorker$1, details?: string);
505
393
  }
506
394
  declare class NotImplemented extends HttpError {
507
- constructor(worker: CorsWorker, details?: string);
395
+ constructor(worker: CorsWorker$1, details?: string);
508
396
  }
509
397
  declare class MethodNotImplemented extends NotImplemented {
510
- constructor(worker: CorsWorker, method: Method);
398
+ constructor(worker: CorsWorker$1);
511
399
  }
512
400
  declare class ServiceUnavailable extends HttpError {
513
- constructor(worker: CorsWorker, details?: string);
514
- }
515
-
516
- declare abstract class BasicWorker extends CacheWorker {
517
- fetch(): Promise<Response>;
518
- protected dispatch(): Promise<Response>;
519
- protected get(): Promise<Response>;
520
- protected put(): Promise<Response>;
521
- protected post(): Promise<Response>;
522
- protected patch(): Promise<Response>;
523
- protected delete(): Promise<Response>;
524
- protected options(): Promise<Response>;
525
- protected head(): Promise<Response>;
526
- protected getResponse<T extends WorkerResponse, Ctor extends new (worker: CorsWorker, ...args: any[]) => T>(ResponseClass: Ctor, ...args: ConstructorParameters<Ctor> extends [CorsWorker, ...infer R] ? R : never): Promise<Response>;
527
- isAllowed(method: string): boolean;
401
+ constructor(worker: CorsWorker$1, details?: string);
528
402
  }
529
403
 
530
404
  /**
531
- * A callback function for a route.
405
+ * A type-safe Cloudflare Worker handler.
532
406
  *
533
- * @param matches - Captured groups from the route RegExp, or the full match at index 0
534
- * @returns A Response object or a Promise that resolves to a Response
535
- */
536
- type RouteCallback = (...matches: string[]) => Response | Promise<Response>;
537
- /**
538
- * Tuple used to initialize a route.
407
+ * Extends `ExportedHandler` but guarantees that the `fetch` method exists
408
+ * and has the correct signature for Cloudflare Worker invocation.
539
409
  *
540
- * [HTTP method, path pattern (string or RegExp), callback function]
410
+ * @template E - The type of environment bindings passed to the worker. Defaults to `Env`.
541
411
  */
542
- type RouteInit = [Method, RegExp | string, RouteCallback];
412
+ interface FetchHandler<E = Env> extends ExportedHandler<E> {
413
+ /**
414
+ * Handles an incoming request and produces a response.
415
+ *
416
+ * @param request - The incoming `Request` object.
417
+ * @param env - Environment bindings (e.g., KV namespaces, secrets, Durable Objects).
418
+ * @param ctx - Execution context for background tasks (`waitUntil`).
419
+ * @returns A `Promise` that resolves to the response.
420
+ */
421
+ fetch: (request: Request, env: E, ctx: ExecutionContext) => Promise<Response>;
422
+ }
543
423
  /**
544
- * Represents a single route with a pattern and a callback.
424
+ * Provides the foundational structure for handling requests,
425
+ * environment bindings, and the worker execution context.
426
+ *
427
+ * Features:
428
+ * - Holds the current `Request` object (`request` getter).
429
+ * - Provides access to environment bindings (`env` getter).
430
+ * - Provides access to the worker execution context (`ctx` getter).
431
+ * - Subclasses must implement `fetch()` to process the request.
545
432
  */
546
- declare class Route {
547
- readonly callback: RouteCallback;
548
- readonly pattern: RegExp;
433
+ declare abstract class BaseWorker implements Worker {
434
+ private readonly _request;
435
+ private readonly _env;
436
+ private readonly _ctx;
437
+ constructor(_request: Request, _env: Env, _ctx: ExecutionContext);
438
+ /** The Request object associated with this worker invocation */
439
+ get request(): Request;
440
+ /** Environment bindings (e.g., KV, secrets, or other globals) */
441
+ get env(): Env;
442
+ /** Execution context for background tasks or `waitUntil` */
443
+ get ctx(): ExecutionContext;
549
444
  /**
550
- * @param pattern - A RegExp or string used to match the request path
551
- * @param callback - Function to handle requests matching the pattern
445
+ * Creates a new instance of the current Worker subclass.
446
+ *
447
+ * @param request - The {@link Request} to pass to the new worker instance.
448
+ * @returns A new worker instance of the same subclass as `this`.
552
449
  */
553
- constructor(pattern: RegExp | string, callback: RouteCallback);
450
+ protected createWorker(request: Request): this;
451
+ /**
452
+ * Process the {@link Request} and produce a {@link Response}.
453
+ *
454
+ * @returns A {@link Response} promise for the {@link Request}.
455
+ */
456
+ abstract fetch(): Promise<Response>;
457
+ /**
458
+ * **Ignite** your `Worker` implementation into a Cloudflare-compatible handler.
459
+ *
460
+ * @returns A `FetchHandler` that launches a new worker instance for each request.
461
+ *
462
+ * @example
463
+ * ```ts
464
+ * export default MyWorker.ignite();
465
+ * ```
466
+ */
467
+ static ignite<W extends Worker>(this: WorkerConstructor<W>): FetchHandler;
554
468
  }
469
+
555
470
  /**
556
- * A collection of routes grouped by HTTP method.
471
+ * Abstract base class for Workers to provide a default CORS policy.
557
472
  *
558
- * Supports adding routes and retrieving the first matching route for a given method and URL.
473
+ * Implements the `CorsProvider` interface and provides a standard policy:
474
+ * - Allows all origins (`*`) by default.
475
+ * - Allows GET, OPTIONS, and HEAD methods.
476
+ * - Allows the `Content-Type` header.
477
+ * - Exposes no additional headers.
478
+ * - Sets CORS preflight max-age to one week.
479
+ *
480
+ * Subclasses can override any of the methods to customize the CORS behavior.
559
481
  */
560
- declare class Routes {
561
- private readonly map;
482
+ declare abstract class CorsWorker extends BaseWorker implements CorsProvider {
483
+ getAllowOrigins(): string[];
484
+ allowAnyOrigin(): boolean;
485
+ getAllowMethods(): Method[];
486
+ getAllowHeaders(): string[];
487
+ getExposeHeaders(): string[];
488
+ getMaxAge(): number;
489
+ }
490
+
491
+ /**
492
+ * Abstract worker class that adds caching support for GET requests.
493
+ *
494
+ * Behavior:
495
+ * - Caches successful GET responses (`response.ok === true`) in the selected cache.
496
+ * - Strips CORS headers from cached responses; all other origin headers are preserved.
497
+ * - Dynamically adds CORS headers to cached responses when returned to the client.
498
+ *
499
+ * Subclasses should override `getCacheKey()` to customize cache key generation if needed.
500
+ */
501
+ declare abstract class CacheWorker extends CorsWorker {
562
502
  /**
563
- * Adds a route to the collection under the given HTTP method.
503
+ * Returns the cache key for the current request.
564
504
  *
565
- * @param method - HTTP method (GET, POST, etc.)
566
- * @param route - Route instance to add
567
- * @returns The Routes instance (for chaining)
505
+ * Behavior:
506
+ * - By default, returns the normalized request URL.
507
+ * - Query parameters are normalized so that the order does not affect the cache key.
508
+ * For example, `?a=1&b=2` and `?b=2&a=1` produce the same cache key.
509
+ *
510
+ * Subclasses may override this method to implement custom cache key strategies.
511
+ *
512
+ * @returns {URL | RequestInfo} The URL or RequestInfo used as the cache key.
568
513
  */
569
- add(method: Method, route: Route): this;
514
+ protected getCacheKey(): URL | RequestInfo;
570
515
  /**
571
- * Finds the first route that matches the given method and URL.
516
+ * Retrieves a cached Response for the current request, if one exists.
572
517
  *
573
- * @param method - HTTP method of the request
574
- * @param url - Full URL string of the request
575
- * @returns The first matching Route, or undefined if none match
518
+ * Behavior:
519
+ * - Only GET requests are considered.
520
+ * - Returns a new Response with dynamic CORS headers applied via `addCacheHeaders`.
521
+ * - Returns `undefined` if no cached response is found.
522
+ * - Cloudflare dynamic headers (`CF-Cache-Status`, `Age`, `Connection`, etc.) will
523
+ * always be present on the returned response, even though they are not stored in the cache.
524
+ *
525
+ * @param {string} [cacheName] Optional named cache; defaults to `caches.default`.
526
+ * @returns {Promise<Response | undefined>} A Response with CORS headers, or undefined.
527
+ * @see {@link setCachedResponse}
528
+ * @see {@link getCacheKey}
576
529
  */
577
- get(method: Method, url: string): Route | undefined;
530
+ protected getCachedResponse(cacheName?: string): Promise<Response | undefined>;
531
+ /**
532
+ * Stores a Response in the cache for the current request.
533
+ *
534
+ * Behavior:
535
+ * - Only caches successful GET responses (`response.ok === true`).
536
+ * - Strips headers via `removeCacheHeaders` before storing.
537
+ * - Uses `ctx.waitUntil` to perform caching asynchronously without blocking the response.
538
+ * - All other origin headers (e.g., Cache-Control, Expires) are preserved.
539
+ *
540
+ * @param {Response} response The Response to cache.
541
+ * @param {string} [cacheName] Optional named cache; defaults to `caches.default`.
542
+ * @see {@link getCachedResponse}
543
+ * @see {@link getCacheKey}
544
+ */
545
+ protected setCachedResponse(response: Response, cacheName?: string): Promise<void>;
546
+ /**
547
+ * Adds headers to a cached response.
548
+ *
549
+ * @param {Response} cached The cached Response.
550
+ * @returns {Response} A new Response with dynamic CORS headers applied.
551
+ * @see {@link removeCacheHeaders}
552
+ */
553
+ private addCacheHeaders;
554
+ /**
555
+ * Removes headers that should not be stored in the cache (currently only CORS headers).
556
+ *
557
+ * @param {Response} response The Response to clean before caching.
558
+ * @returns {Response} A new Response with excluded headers removed.
559
+ * @see {@link addCacheHeaders}
560
+ */
561
+ private removeCacheHeaders;
562
+ /**
563
+ * Returns the list of headers to exclude from the cached response.
564
+ * By default, excludes only dynamic CORS headers.
565
+ *
566
+ * @returns {string[]} Array of header names to exclude.
567
+ * @see {@link removeCacheHeaders}
568
+ */
569
+ protected excludeCacheHeaders(): string[];
570
+ }
571
+
572
+ declare abstract class BasicWorker extends CacheWorker {
573
+ fetch(): Promise<Response>;
574
+ protected dispatch(): Promise<Response>;
575
+ isAllowed(method: string): boolean;
576
+ protected get(): Promise<Response>;
577
+ protected put(): Promise<Response>;
578
+ protected post(): Promise<Response>;
579
+ protected patch(): Promise<Response>;
580
+ protected delete(): Promise<Response>;
581
+ protected options(): Promise<Response>;
582
+ protected head(): Promise<Response>;
583
+ protected getResponse<T extends WorkerResponse, Ctor extends new (worker: CorsWorker$1, ...args: any[]) => T>(ResponseClass: Ctor, ...args: ConstructorParameters<Ctor> extends [CorsWorker$1, ...infer R] ? R : never): Promise<Response>;
578
584
  }
579
585
 
580
- declare abstract class RoutedWorker extends BasicWorker {
586
+ declare abstract class RouteWorker extends BasicWorker {
581
587
  private readonly routes;
582
588
  protected initialize(routes: RouteInit[]): void;
583
589
  protected add(method: Method, pattern: RegExp | string, callback: RouteCallback): this;
@@ -589,4 +595,4 @@ declare abstract class RoutedWorker extends BasicWorker {
589
595
  protected delete(): Promise<Response>;
590
596
  }
591
597
 
592
- export { BadRequest, BasicWorker, CacheControl, ClonedResponse, Cors, type CorsProvider, type CorsWorker, type ErrorJson, Forbidden, Head, HtmlResponse, HttpError, HttpHeader, InternalServerError, JsonResponse, MediaType, Method, MethodNotAllowed, MethodNotImplemented, NotFound, NotImplemented, Options, Route, type RouteCallback, type RouteInit, RoutedWorker, Routes, ServiceUnavailable, SuccessResponse, TextResponse, Time, Unauthorized, type Worker, type WorkerConstructor, WorkerResponse, addCorsHeaders, getContentType, getOrigin, isMethod, mergeHeader, normalizeUrl, setHeader };
598
+ export { BadRequest, BasicWorker, CacheControl, ClonedResponse, Cors, type CorsProvider, type CorsWorker$1 as CorsWorker, type ErrorJson, Forbidden, Head, HtmlResponse, HttpError, HttpHeader, InternalServerError, JsonResponse, MediaType, Method, MethodNotAllowed, MethodNotImplemented, NotFound, NotImplemented, Options, Route, type RouteCallback, type RouteInit, RouteWorker, Routes, ServiceUnavailable, SuccessResponse, TextResponse, Time, Unauthorized, type Worker, type WorkerConstructor, WorkerResponse, addCorsHeaders, getContentType, getOrigin, isMethod, mergeHeader, normalizeUrl, setHeader };