@adonix.org/cloud-spark 0.0.187 → 0.0.188

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 CHANGED
@@ -56,7 +56,7 @@ And it's ready on http://localhost:8787
56
56
  As shown in the [Quickstart](#rocket-quickstart), BasicWorker is the base class for building Cloudflare Workers with CloudSpark. It handles common tasks, including:
57
57
 
58
58
  - Dispatching incoming HTTP requests to the corresponding handler (GET, POST, PUT, etc.).
59
- - Providing defaults for standard HTTP behavior, such as HEAD requests and OPTIONS responses.
59
+ - Providing defaults for standard HTTP behavior, such as HEAD and OPTIONS requests.
60
60
  - Ensuring type safety and consistent response formatting.
61
61
  - Support for built-in and custom middleware.
62
62
  - Catching unhandled errors.
@@ -90,7 +90,7 @@ export class MyWorker extends BasicWorker {
90
90
  * If a requested method is not listed, the response is:
91
91
  * 405 Method Not Allowed
92
92
  *
93
- * If an allowed method isn’t implemented, the response is:
93
+ * If an allowed method is not implemented, the response is:
94
94
  * GET or HEAD: 404 Not Found
95
95
  * All other methods: 501 Not Implemented
96
96
  *
@@ -380,7 +380,7 @@ class ChatWorker extends RouteWorker {
380
380
  /**
381
381
  * Handles WebSocket upgrade requests.
382
382
  *
383
- * Expects a DurableObject binding named CHAT
383
+ * Expects a DurableObject binding named CHAT
384
384
  * in wrangler.jsonc
385
385
  */
386
386
  protected upgrade(params: PathParams): Promise<Response> {
@@ -388,7 +388,7 @@ class ChatWorker extends RouteWorker {
388
388
  const chat = this.env.CHAT;
389
389
 
390
390
  /**
391
- * Request has already been validated by the
391
+ * Request has already been validated by the
392
392
  * WebSocket middleware.
393
393
  */
394
394
  return chat.get(chat.idFromName(room)).fetch(this.request);
@@ -403,6 +403,80 @@ export default ChatWorker.ignite();
403
403
 
404
404
  ### Custom
405
405
 
406
+ Create custom middleware by implementing the [Middleware](https://github.com/adonix-org/cloud-spark/blob/main/src/interfaces/middleware.ts) interface and its single _handle_ method, then register it with your worker. Within your middleware, you can inspect requests and modify responses or short-circuit processing entirely.
407
+
408
+ Here is a simple example:
409
+
410
+ ```ts
411
+ import { BadRequest, CopyResponse, Middleware, Worker } from "@adonix.org/cloud-spark";
412
+
413
+ /**
414
+ * Custom middleware example.
415
+ *
416
+ * Demonstrates several key middleware capabilities:
417
+ * • Options via constructor parameters.
418
+ * • Inspection of the incoming request.
419
+ * • Short-circuiting by returning a response directly.
420
+ * • Modifying outgoing responses dispatched by the worker.
421
+ */
422
+ class PoweredBy implements Middleware {
423
+ /**
424
+ * Optional constructor parameter to customize the "X-Powered-By" header.
425
+ */
426
+ constructor(private readonly name = "CloudSpark") {}
427
+
428
+ public async handle(worker: Worker, next: () => Promise<Response>): Promise<Response> {
429
+ /**
430
+ * Extract the User-Agent header from the request.
431
+ */
432
+ const userAgent = worker.request.headers.get("User-Agent")?.trim();
433
+
434
+ /**
435
+ * If the User-Agent is missing, short-circuit by directly
436
+ * returning 400 Bad Request.
437
+ */
438
+ if (!userAgent) {
439
+ return new BadRequest(`Missing User-Agent`).response();
440
+ }
441
+
442
+ /**
443
+ * Calls the next middleware or worker dispatch method.
444
+ */
445
+ const response = await next();
446
+
447
+ /**
448
+ * Wrap the response in a mutable copy.
449
+ */
450
+ const copy = new CopyResponse(response);
451
+
452
+ /**
453
+ * Append custom headers to the response.
454
+ */
455
+ copy.setHeader("X-User-Agent", userAgent);
456
+ copy.setHeader("X-Powered-By", this.name);
457
+ copy.setHeader("X-Processed-At", new Date().toUTCString());
458
+
459
+ /**
460
+ * Return the modified response.
461
+ */
462
+ return copy.response();
463
+ }
464
+ }
465
+
466
+ /**
467
+ * Factory function for registering the middleware, with an optional
468
+ * name parameter. Workers interact with the `Middleware` interface,
469
+ * and not the concrete implementation.
470
+ *
471
+ * Example:
472
+ * this.use(poweredby());
473
+ * this.use(poweredby("My Project Name"));
474
+ */
475
+ export function poweredby(name?: string): Middleware {
476
+ return new PoweredBy(name);
477
+ }
478
+ ```
479
+
406
480
  <br>
407
481
 
408
482
  ## :left_right_arrow: Web Sockets
package/dist/index.d.ts CHANGED
@@ -1036,9 +1036,9 @@ declare class R2ObjectStream extends OctetStream {
1036
1036
  * This function normalizes a Cloudflare R2 `R2Range` into the shape expected
1037
1037
  * by `OctetStream`. It handles the following cases:
1038
1038
  *
1039
- * - No range provided returns `{ size }` (full content).
1040
- * - `suffix` range calculates the offset and length from the end of the file.
1041
- * - Explicit `offset` and/or `length` passed through as-is.
1039
+ * - No range provided: returns `{ size }` (full content).
1040
+ * - `suffix` range: calculates the offset and length from the end of the file.
1041
+ * - Explicit `offset` and/or `length`: passed through as-is.
1042
1042
  *
1043
1043
  * @param size - The total size of the file/object.
1044
1044
  * @param range - Optional range to extract (from R2). Can be: