@b9g/router 0.1.8 → 0.1.9

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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/index.js +12 -106
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@b9g/router",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "description": "Universal request router for ServiceWorker applications. Built on web standards with cache-aware routing and generator-based middleware.",
5
5
  "keywords": [
6
6
  "router",
@@ -17,7 +17,7 @@
17
17
  "@b9g/match-pattern": "^0.1.7"
18
18
  },
19
19
  "devDependencies": {
20
- "@b9g/libuild": "^0.1.11",
20
+ "@b9g/libuild": "^0.1.18",
21
21
  "bun-types": "latest"
22
22
  },
23
23
  "type": "module",
package/src/index.js CHANGED
@@ -269,27 +269,21 @@ var Router = class {
269
269
  }
270
270
  const matchResult = this.#executor.match(request);
271
271
  if (matchResult) {
272
- const mutableRequest = this.#createMutableRequest(request);
273
272
  return await this.#executeMiddlewareStack(
274
273
  this.#middlewares,
275
- mutableRequest,
274
+ request,
276
275
  matchResult.context,
277
- matchResult.handler,
278
- request.url,
279
- this.#executor
276
+ matchResult.handler
280
277
  );
281
278
  } else {
282
279
  const notFoundHandler = async () => {
283
280
  throw new NotFound();
284
281
  };
285
- const mutableRequest = this.#createMutableRequest(request);
286
282
  return await this.#executeMiddlewareStack(
287
283
  this.#middlewares,
288
- mutableRequest,
284
+ request,
289
285
  { params: {} },
290
- notFoundHandler,
291
- request.url,
292
- this.#executor
286
+ notFoundHandler
293
287
  );
294
288
  }
295
289
  } catch (error) {
@@ -356,8 +350,6 @@ var Router = class {
356
350
  this.#executor = new RadixTreeExecutor(this.#routes);
357
351
  this.#dirty = false;
358
352
  }
359
- const mutableRequest = this.#createMutableRequest(request);
360
- const originalURL = mutableRequest.url;
361
353
  let matchResult = this.#executor.match(request);
362
354
  let handler;
363
355
  let context;
@@ -374,12 +366,9 @@ var Router = class {
374
366
  try {
375
367
  response = await this.#executeMiddlewareStack(
376
368
  this.#middlewares,
377
- mutableRequest,
369
+ request,
378
370
  context,
379
- handler,
380
- originalURL,
381
- this.#executor
382
- // Pass executor for re-routing
371
+ handler
383
372
  );
384
373
  } catch (error) {
385
374
  if (!matchResult && isHTTPError(error) && error.status === 404) {
@@ -512,7 +501,7 @@ var Router = class {
512
501
  /**
513
502
  * Execute middleware stack with guaranteed execution using Rack-style LIFO order
514
503
  */
515
- async #executeMiddlewareStack(middlewares, request, context, handler, originalURL, executor) {
504
+ async #executeMiddlewareStack(middlewares, request, context, handler) {
516
505
  const runningGenerators = [];
517
506
  let currentResponse = null;
518
507
  const requestPathname = new URL(request.url).pathname;
@@ -545,49 +534,19 @@ var Router = class {
545
534
  }
546
535
  }
547
536
  if (!currentResponse) {
548
- let finalHandler = handler;
549
- let finalContext = context;
550
- if (request.url !== originalURL && executor) {
551
- const newMatchResult = executor.match(
552
- new Request(request.url, {
553
- method: request.method,
554
- headers: request.headers,
555
- body: request.body
556
- })
557
- );
558
- if (newMatchResult) {
559
- finalHandler = newMatchResult.handler;
560
- finalContext = newMatchResult.context;
561
- }
562
- }
563
537
  let handlerError = null;
564
538
  try {
565
- currentResponse = await finalHandler(request, finalContext);
539
+ currentResponse = await handler(request, context);
566
540
  } catch (error) {
567
541
  handlerError = error;
568
542
  }
569
543
  if (handlerError) {
570
- if (request.url !== originalURL) {
571
- currentResponse = this.#handleAutomaticRedirect(
572
- originalURL,
573
- request.url,
574
- request.method
575
- );
576
- } else {
577
- currentResponse = await this.#handleErrorThroughGenerators(
578
- handlerError,
579
- runningGenerators
580
- );
581
- }
544
+ currentResponse = await this.#handleErrorThroughGenerators(
545
+ handlerError,
546
+ runningGenerators
547
+ );
582
548
  }
583
549
  }
584
- if (request.url !== originalURL && currentResponse) {
585
- currentResponse = this.#handleAutomaticRedirect(
586
- originalURL,
587
- request.url,
588
- request.method
589
- );
590
- }
591
550
  for (let i = runningGenerators.length - 1; i >= 0; i--) {
592
551
  const { generator } = runningGenerators[i];
593
552
  const result = await generator.next(currentResponse);
@@ -616,59 +575,6 @@ var Router = class {
616
575
  }
617
576
  throw error;
618
577
  }
619
- /**
620
- * Create a mutable request wrapper that allows URL modification
621
- */
622
- #createMutableRequest(request) {
623
- return {
624
- url: request.url,
625
- method: request.method,
626
- headers: new Headers(request.headers),
627
- body: request.body,
628
- bodyUsed: request.bodyUsed,
629
- cache: request.cache,
630
- credentials: request.credentials,
631
- destination: request.destination,
632
- integrity: request.integrity,
633
- keepalive: request.keepalive,
634
- mode: request.mode,
635
- redirect: request.redirect,
636
- referrer: request.referrer,
637
- referrerPolicy: request.referrerPolicy,
638
- signal: request.signal,
639
- // Add all other Request methods
640
- arrayBuffer: () => request.arrayBuffer(),
641
- blob: () => request.blob(),
642
- clone: () => request.clone(),
643
- formData: () => request.formData(),
644
- json: () => request.json(),
645
- text: () => request.text()
646
- };
647
- }
648
- /**
649
- * Handle automatic redirects when URL is modified
650
- */
651
- #handleAutomaticRedirect(originalURL, newURL, method) {
652
- const originalURLObj = new URL(originalURL);
653
- const newURLObj = new URL(newURL);
654
- if (originalURLObj.hostname !== newURLObj.hostname || originalURLObj.port !== newURLObj.port && originalURLObj.port !== "" && newURLObj.port !== "") {
655
- throw new Error(
656
- `Cross-origin redirect not allowed: ${originalURL} -> ${newURL}`
657
- );
658
- }
659
- let status = 302;
660
- if (originalURLObj.protocol !== newURLObj.protocol) {
661
- status = 301;
662
- } else if (method.toUpperCase() !== "GET") {
663
- status = 307;
664
- }
665
- return new Response(null, {
666
- status,
667
- headers: {
668
- Location: newURL
669
- }
670
- });
671
- }
672
578
  /**
673
579
  * Get route statistics
674
580
  */