@adonix.org/cloud-spark 0.0.94 → 0.0.97
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 +258 -259
- package/dist/index.js +264 -256
- package/dist/index.js.map +1 -1
- package/package.json +3 -1
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
|
*/
|
|
@@ -130,14 +23,15 @@ declare const CacheControl: {
|
|
|
130
23
|
*/
|
|
131
24
|
declare namespace HttpHeader {
|
|
132
25
|
const VARY = "Vary";
|
|
26
|
+
const ALLOW = "Allow";
|
|
133
27
|
const CONTENT_TYPE = "Content-Type";
|
|
134
28
|
const CACHE_CONTROL = "Cache-Control";
|
|
135
|
-
const X_CONTENT_TYPE_OPTIONS = "X-Content-Type-Options";
|
|
136
29
|
const X_FRAME_OPTIONS = "X-Frame-Options";
|
|
137
|
-
const
|
|
138
|
-
const CONTENT_SECURITY_POLICY = "Content-Security-Policy";
|
|
30
|
+
const X_CONTENT_TYPE_OPTIONS = "X-Content-Type-Options";
|
|
139
31
|
const REFERRER_POLICY = "Referrer-Policy";
|
|
140
32
|
const PERMISSIONS_POLICY = "Permissions-Policy";
|
|
33
|
+
const CONTENT_SECURITY_POLICY = "Content-Security-Policy";
|
|
34
|
+
const STRICT_TRANSPORT_SECURITY = "Strict-Transport-Security";
|
|
141
35
|
const NOSNIFF = "nosniff";
|
|
142
36
|
const ORIGIN = "Origin";
|
|
143
37
|
}
|
|
@@ -159,10 +53,10 @@ declare const Time: {
|
|
|
159
53
|
declare enum Method {
|
|
160
54
|
GET = "GET",
|
|
161
55
|
PUT = "PUT",
|
|
56
|
+
HEAD = "HEAD",
|
|
162
57
|
POST = "POST",
|
|
163
58
|
PATCH = "PATCH",
|
|
164
59
|
DELETE = "DELETE",
|
|
165
|
-
HEAD = "HEAD",
|
|
166
60
|
OPTIONS = "OPTIONS"
|
|
167
61
|
}
|
|
168
62
|
/**
|
|
@@ -295,12 +189,12 @@ interface CorsProvider {
|
|
|
295
189
|
* Constants for common CORS headers.
|
|
296
190
|
*/
|
|
297
191
|
declare namespace Cors {
|
|
192
|
+
const MAX_AGE = "Access-Control-Max-Age";
|
|
298
193
|
const ALLOW_ORIGIN = "Access-Control-Allow-Origin";
|
|
299
|
-
const ALLOW_CREDENTIALS = "Access-Control-Allow-Credentials";
|
|
300
|
-
const EXPOSE_HEADERS = "Access-Control-Expose-Headers";
|
|
301
194
|
const ALLOW_HEADERS = "Access-Control-Allow-Headers";
|
|
302
195
|
const ALLOW_METHODS = "Access-Control-Allow-Methods";
|
|
303
|
-
const
|
|
196
|
+
const EXPOSE_HEADERS = "Access-Control-Expose-Headers";
|
|
197
|
+
const ALLOW_CREDENTIALS = "Access-Control-Allow-Credentials";
|
|
304
198
|
const ALLOW_ALL_ORIGINS = "*";
|
|
305
199
|
}
|
|
306
200
|
/**
|
|
@@ -319,105 +213,92 @@ declare namespace Cors {
|
|
|
319
213
|
declare function addCorsHeaders(origin: string | null, cors: CorsProvider, headers: Headers): void;
|
|
320
214
|
|
|
321
215
|
/**
|
|
322
|
-
*
|
|
216
|
+
* A callback function for a route.
|
|
323
217
|
*
|
|
324
|
-
*
|
|
325
|
-
*
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
*
|
|
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.
|
|
330
224
|
*
|
|
331
|
-
*
|
|
225
|
+
* [HTTP method, path pattern (string or RegExp), callback function]
|
|
332
226
|
*/
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
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);
|
|
340
239
|
}
|
|
341
|
-
|
|
342
240
|
/**
|
|
343
|
-
*
|
|
344
|
-
*
|
|
345
|
-
* Behavior:
|
|
346
|
-
* - Caches successful GET responses (`response.ok === true`) in the selected cache.
|
|
347
|
-
* - Strips CORS headers from cached responses; all other origin headers are preserved.
|
|
348
|
-
* - Dynamically adds CORS headers to cached responses when returned to the client.
|
|
241
|
+
* A collection of routes grouped by HTTP method.
|
|
349
242
|
*
|
|
350
|
-
*
|
|
243
|
+
* Supports adding routes and retrieving the first matching route for a given method and URL.
|
|
351
244
|
*/
|
|
352
|
-
declare
|
|
245
|
+
declare class Routes {
|
|
246
|
+
private readonly map;
|
|
353
247
|
/**
|
|
354
|
-
*
|
|
355
|
-
*
|
|
356
|
-
* Behavior:
|
|
357
|
-
* - By default, returns the normalized request URL.
|
|
358
|
-
* - Query parameters are normalized so that the order does not affect the cache key.
|
|
359
|
-
* For example, `?a=1&b=2` and `?b=2&a=1` produce the same cache key.
|
|
360
|
-
*
|
|
361
|
-
* Subclasses may override this method to implement custom cache key strategies.
|
|
248
|
+
* Adds a route to the collection under the given HTTP method.
|
|
362
249
|
*
|
|
363
|
-
* @
|
|
250
|
+
* @param method - HTTP method (GET, POST, etc.)
|
|
251
|
+
* @param route - Route instance to add
|
|
252
|
+
* @returns The Routes instance (for chaining)
|
|
364
253
|
*/
|
|
365
|
-
|
|
254
|
+
add(method: Method, route: Route): this;
|
|
366
255
|
/**
|
|
367
|
-
*
|
|
368
|
-
*
|
|
369
|
-
* Behavior:
|
|
370
|
-
* - Only GET requests are considered.
|
|
371
|
-
* - Returns a new Response with dynamic CORS headers applied via `addCacheHeaders`.
|
|
372
|
-
* - Returns `undefined` if no cached response is found.
|
|
373
|
-
* - Cloudflare dynamic headers (`CF-Cache-Status`, `Age`, `Connection`, etc.) will
|
|
374
|
-
* 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.
|
|
375
257
|
*
|
|
376
|
-
* @param
|
|
377
|
-
* @
|
|
378
|
-
* @
|
|
379
|
-
* @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
|
|
380
261
|
*/
|
|
381
|
-
|
|
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 {
|
|
382
283
|
/**
|
|
383
|
-
*
|
|
384
|
-
*
|
|
385
|
-
* Behavior:
|
|
386
|
-
* - Only caches successful GET responses (`response.ok === true`).
|
|
387
|
-
* - Strips headers via `removeCacheHeaders` before storing.
|
|
388
|
-
* - Uses `ctx.waitUntil` to perform caching asynchronously without blocking the response.
|
|
389
|
-
* - All other origin headers (e.g., Cache-Control, Expires) are preserved.
|
|
284
|
+
* Processes the incoming {@link Request} and produces a {@link Response}.
|
|
390
285
|
*
|
|
391
|
-
* @
|
|
392
|
-
* @param {string} [cacheName] Optional named cache; defaults to `caches.default`.
|
|
393
|
-
* @see {@link getCachedResponse}
|
|
394
|
-
* @see {@link getCacheKey}
|
|
286
|
+
* @returns A Promise that resolves to the HTTP {@link Response}.
|
|
395
287
|
*/
|
|
396
|
-
|
|
288
|
+
fetch(): Promise<Response>;
|
|
397
289
|
/**
|
|
398
|
-
*
|
|
399
|
-
*
|
|
400
|
-
* @param {Response} cached The cached Response.
|
|
401
|
-
* @returns {Response} A new Response with dynamic CORS headers applied.
|
|
402
|
-
* @see {@link removeCacheHeaders}
|
|
290
|
+
* The original {@link Request} being processed by this worker instance.
|
|
403
291
|
*/
|
|
404
|
-
|
|
292
|
+
get request(): Request;
|
|
405
293
|
/**
|
|
406
|
-
*
|
|
407
|
-
*
|
|
408
|
-
* @param {Response} response The Response to clean before caching.
|
|
409
|
-
* @returns {Response} A new Response with excluded headers removed.
|
|
410
|
-
* @see {@link addCacheHeaders}
|
|
294
|
+
* The environment bindings provided at runtime (e.g., KV, R2, secrets).
|
|
411
295
|
*/
|
|
412
|
-
|
|
296
|
+
get env(): Env;
|
|
413
297
|
/**
|
|
414
|
-
*
|
|
415
|
-
*
|
|
416
|
-
*
|
|
417
|
-
* @returns {string[]} Array of header names to exclude.
|
|
418
|
-
* @see {@link removeCacheHeaders}
|
|
298
|
+
* The {@link ExecutionContext} associated with the current request,
|
|
299
|
+
* used to manage background tasks and request lifecycle.
|
|
419
300
|
*/
|
|
420
|
-
|
|
301
|
+
get ctx(): ExecutionContext;
|
|
421
302
|
}
|
|
422
303
|
|
|
423
304
|
interface ErrorJson {
|
|
@@ -431,28 +312,28 @@ interface ErrorJson {
|
|
|
431
312
|
* Used by response builders that require both Worker
|
|
432
313
|
* and CORS functionality.
|
|
433
314
|
*/
|
|
434
|
-
type CorsWorker = Worker & CorsProvider;
|
|
315
|
+
type CorsWorker$1 = Worker & CorsProvider;
|
|
435
316
|
declare abstract class BaseResponse {
|
|
436
|
-
readonly worker: CorsWorker;
|
|
317
|
+
readonly worker: CorsWorker$1;
|
|
437
318
|
headers: Headers;
|
|
438
319
|
body: BodyInit | null;
|
|
439
320
|
status: StatusCodes;
|
|
440
321
|
statusText?: string;
|
|
441
322
|
mediaType?: MediaType;
|
|
442
|
-
constructor(worker: CorsWorker, content?: BodyInit | null);
|
|
323
|
+
constructor(worker: CorsWorker$1, content?: BodyInit | null);
|
|
443
324
|
protected get responseInit(): ResponseInit;
|
|
444
325
|
setHeader(key: string, value: string | string[]): void;
|
|
445
326
|
mergeHeader(key: string, value: string | string[]): void;
|
|
446
327
|
addContentType(): void;
|
|
447
328
|
}
|
|
448
329
|
declare abstract class CorsResponse extends BaseResponse {
|
|
449
|
-
constructor(worker: CorsWorker, content?: BodyInit | null);
|
|
330
|
+
constructor(worker: CorsWorker$1, content?: BodyInit | null);
|
|
450
331
|
protected addCorsHeaders(): void;
|
|
451
332
|
protected getOrigin(): string | null;
|
|
452
333
|
}
|
|
453
334
|
declare abstract class CacheResponse extends CorsResponse {
|
|
454
335
|
cache?: CacheControl | undefined;
|
|
455
|
-
constructor(worker: CorsWorker, body?: BodyInit | null, cache?: CacheControl | undefined);
|
|
336
|
+
constructor(worker: CorsWorker$1, body?: BodyInit | null, cache?: CacheControl | undefined);
|
|
456
337
|
protected addCacheHeaders(): void;
|
|
457
338
|
}
|
|
458
339
|
declare abstract class WorkerResponse extends CacheResponse {
|
|
@@ -460,131 +341,249 @@ declare abstract class WorkerResponse extends CacheResponse {
|
|
|
460
341
|
protected addSecurityHeaders(): void;
|
|
461
342
|
}
|
|
462
343
|
declare class ClonedResponse extends WorkerResponse {
|
|
463
|
-
constructor(worker: CorsWorker, response: Response, cache?: CacheControl);
|
|
344
|
+
constructor(worker: CorsWorker$1, response: Response, cache?: CacheControl);
|
|
464
345
|
}
|
|
465
346
|
declare class SuccessResponse extends WorkerResponse {
|
|
466
|
-
constructor(worker: CorsWorker, body?: BodyInit | null, cache?: CacheControl, status?: StatusCodes);
|
|
347
|
+
constructor(worker: CorsWorker$1, body?: BodyInit | null, cache?: CacheControl, status?: StatusCodes);
|
|
467
348
|
}
|
|
468
349
|
declare class JsonResponse extends SuccessResponse {
|
|
469
|
-
constructor(worker: CorsWorker, json?: unknown, cache?: CacheControl, status?: StatusCodes);
|
|
350
|
+
constructor(worker: CorsWorker$1, json?: unknown, cache?: CacheControl, status?: StatusCodes);
|
|
470
351
|
}
|
|
471
352
|
declare class HtmlResponse extends SuccessResponse {
|
|
472
|
-
constructor(worker: CorsWorker, body: string, cache?: CacheControl, status?: StatusCodes);
|
|
353
|
+
constructor(worker: CorsWorker$1, body: string, cache?: CacheControl, status?: StatusCodes);
|
|
473
354
|
}
|
|
474
355
|
declare class TextResponse extends SuccessResponse {
|
|
475
|
-
constructor(worker: CorsWorker, content: string, cache?: CacheControl, status?: StatusCodes);
|
|
356
|
+
constructor(worker: CorsWorker$1, content: string, cache?: CacheControl, status?: StatusCodes);
|
|
476
357
|
}
|
|
477
358
|
/**
|
|
478
359
|
* Removes the body from a GET response.
|
|
479
360
|
*/
|
|
480
361
|
declare class Head extends WorkerResponse {
|
|
481
|
-
constructor(worker: CorsWorker, get: Response);
|
|
362
|
+
constructor(worker: CorsWorker$1, get: Response);
|
|
482
363
|
}
|
|
483
364
|
declare class Options extends SuccessResponse {
|
|
484
|
-
constructor(worker: CorsWorker);
|
|
365
|
+
constructor(worker: CorsWorker$1);
|
|
485
366
|
}
|
|
486
367
|
declare class HttpError extends JsonResponse {
|
|
487
368
|
protected readonly details?: string | undefined;
|
|
488
|
-
constructor(worker: CorsWorker, status: StatusCodes, details?: string | undefined);
|
|
369
|
+
constructor(worker: CorsWorker$1, status: StatusCodes, details?: string | undefined);
|
|
489
370
|
get json(): ErrorJson;
|
|
490
371
|
createResponse(): Response;
|
|
491
372
|
}
|
|
492
373
|
declare class BadRequest extends HttpError {
|
|
493
|
-
constructor(worker: CorsWorker, details?: string);
|
|
374
|
+
constructor(worker: CorsWorker$1, details?: string);
|
|
494
375
|
}
|
|
495
376
|
declare class Unauthorized extends HttpError {
|
|
496
|
-
constructor(worker: CorsWorker, details?: string);
|
|
377
|
+
constructor(worker: CorsWorker$1, details?: string);
|
|
497
378
|
}
|
|
498
379
|
declare class Forbidden extends HttpError {
|
|
499
|
-
constructor(worker: CorsWorker, details?: string);
|
|
380
|
+
constructor(worker: CorsWorker$1, details?: string);
|
|
500
381
|
}
|
|
501
382
|
declare class NotFound extends HttpError {
|
|
502
|
-
constructor(worker: CorsWorker, details?: string);
|
|
383
|
+
constructor(worker: CorsWorker$1, details?: string);
|
|
503
384
|
}
|
|
504
385
|
declare class MethodNotAllowed extends HttpError {
|
|
505
|
-
constructor(worker: CorsWorker
|
|
386
|
+
constructor(worker: CorsWorker$1);
|
|
506
387
|
get json(): ErrorJson & {
|
|
507
388
|
allowed: Method[];
|
|
508
389
|
};
|
|
509
390
|
}
|
|
510
391
|
declare class InternalServerError extends HttpError {
|
|
511
|
-
constructor(worker: CorsWorker, details?: string);
|
|
392
|
+
constructor(worker: CorsWorker$1, details?: string);
|
|
512
393
|
}
|
|
513
394
|
declare class NotImplemented extends HttpError {
|
|
514
|
-
constructor(worker: CorsWorker, details?: string);
|
|
395
|
+
constructor(worker: CorsWorker$1, details?: string);
|
|
515
396
|
}
|
|
516
397
|
declare class MethodNotImplemented extends NotImplemented {
|
|
517
|
-
constructor(worker: CorsWorker
|
|
398
|
+
constructor(worker: CorsWorker$1);
|
|
518
399
|
}
|
|
519
400
|
declare class ServiceUnavailable extends HttpError {
|
|
520
|
-
constructor(worker: CorsWorker, details?: string);
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
declare abstract class BasicWorker extends CacheWorker {
|
|
524
|
-
fetch(): Promise<Response>;
|
|
525
|
-
protected dispatch(): Promise<Response>;
|
|
526
|
-
protected get(): Promise<Response>;
|
|
527
|
-
protected put(): Promise<Response>;
|
|
528
|
-
protected post(): Promise<Response>;
|
|
529
|
-
protected patch(): Promise<Response>;
|
|
530
|
-
protected delete(): Promise<Response>;
|
|
531
|
-
protected options(): Promise<Response>;
|
|
532
|
-
protected head(): Promise<Response>;
|
|
533
|
-
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>;
|
|
534
|
-
isAllowed(method: string): boolean;
|
|
401
|
+
constructor(worker: CorsWorker$1, details?: string);
|
|
535
402
|
}
|
|
536
403
|
|
|
537
404
|
/**
|
|
538
|
-
* A
|
|
405
|
+
* A type-safe Cloudflare Worker handler.
|
|
539
406
|
*
|
|
540
|
-
*
|
|
541
|
-
*
|
|
542
|
-
*/
|
|
543
|
-
type RouteCallback = (...matches: string[]) => Response | Promise<Response>;
|
|
544
|
-
/**
|
|
545
|
-
* 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.
|
|
546
409
|
*
|
|
547
|
-
*
|
|
410
|
+
* @template E - The type of environment bindings passed to the worker. Defaults to `Env`.
|
|
548
411
|
*/
|
|
549
|
-
|
|
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
|
+
}
|
|
550
423
|
/**
|
|
551
|
-
*
|
|
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.
|
|
552
432
|
*/
|
|
553
|
-
declare class
|
|
554
|
-
readonly
|
|
555
|
-
readonly
|
|
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;
|
|
556
444
|
/**
|
|
557
|
-
*
|
|
558
|
-
*
|
|
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`.
|
|
559
449
|
*/
|
|
560
|
-
|
|
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;
|
|
561
468
|
}
|
|
469
|
+
|
|
562
470
|
/**
|
|
563
|
-
*
|
|
471
|
+
* Abstract base class for Workers to provide a default CORS policy.
|
|
564
472
|
*
|
|
565
|
-
*
|
|
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.
|
|
566
481
|
*/
|
|
567
|
-
declare class
|
|
568
|
-
|
|
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 {
|
|
569
502
|
/**
|
|
570
|
-
*
|
|
503
|
+
* Returns the cache key for the current request.
|
|
571
504
|
*
|
|
572
|
-
*
|
|
573
|
-
*
|
|
574
|
-
*
|
|
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.
|
|
575
513
|
*/
|
|
576
|
-
|
|
514
|
+
protected getCacheKey(): URL | RequestInfo;
|
|
577
515
|
/**
|
|
578
|
-
*
|
|
516
|
+
* Retrieves a cached Response for the current request, if one exists.
|
|
579
517
|
*
|
|
580
|
-
*
|
|
581
|
-
*
|
|
582
|
-
*
|
|
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}
|
|
583
529
|
*/
|
|
584
|
-
|
|
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>;
|
|
585
584
|
}
|
|
586
585
|
|
|
587
|
-
declare abstract class
|
|
586
|
+
declare abstract class RouteWorker extends BasicWorker {
|
|
588
587
|
private readonly routes;
|
|
589
588
|
protected initialize(routes: RouteInit[]): void;
|
|
590
589
|
protected add(method: Method, pattern: RegExp | string, callback: RouteCallback): this;
|
|
@@ -596,4 +595,4 @@ declare abstract class RoutedWorker extends BasicWorker {
|
|
|
596
595
|
protected delete(): Promise<Response>;
|
|
597
596
|
}
|
|
598
597
|
|
|
599
|
-
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,
|
|
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 };
|