@stacksjs/router 0.70.255 → 0.70.257

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.
@@ -64,6 +64,22 @@ export declare function clearMiddlewareCache(): void;
64
64
  * old version forever.
65
65
  */
66
66
  export declare function installMiddlewareHotReload(): () => void;
67
+ /**
68
+ * Drop every registered route → middleware mapping.
69
+ *
70
+ * The registry is module-scoped and lives for the life of the process, which
71
+ * is what `listRegisteredRoutes()` and the boot-time validators read. That is
72
+ * correct at runtime — routes are registered once at boot — but it means a
73
+ * test that registers a route keeps it visible to every later caller in the
74
+ * same process, including `assertRouteMiddlewareResolvable()`. A suite that
75
+ * deliberately registers an unresolvable alias therefore made an unrelated
76
+ * file's `serverResponse()` throw on routes it never declared.
77
+ *
78
+ * `clearMiddlewareCache()` deliberately does NOT do this: it flushes resolved
79
+ * middleware so a hot-reloaded file is re-read, while the routes themselves
80
+ * must survive. Clearing the route table is a separate, coarser action.
81
+ */
82
+ export declare function clearRouteMiddlewareRegistry(): void;
67
83
  /**
68
84
  * Resolve every middleware alias referenced by a registered route and
69
85
  * report the ones that don't load. `csrf` is always checked too — it's
@@ -190,6 +206,24 @@ export declare interface StacksRouterInstance {
190
206
  declare type RouteHandlerFn = (_req: EnhancedRequest) => Response | Promise<Response>;
191
207
  declare type StacksHandler = string | RouteHandlerFn;
192
208
  declare type ResourceAction = 'index' | 'store' | 'show' | 'update' | 'destroy';
209
+ /**
210
+ * Per-request read-routing context.
211
+ *
212
+ * The database package tracks writes per async context so that a request
213
+ * which reads back what it just wrote is never served from a read replica
214
+ * (replication is asynchronous, so the row may not be there yet). That
215
+ * tracking only works if something establishes the context at the request
216
+ * boundary — without this, `contextHasWritten()` has no store to consult,
217
+ * always reports false, and every read routes to a replica including the
218
+ * one immediately following a write. The safety rule would be dead code.
219
+ *
220
+ * Loaded lazily rather than imported: `@stacksjs/database` already depends
221
+ * on `@stacksjs/router`, so a static import here would close a package
222
+ * cycle. The dynamic import is resolved once and cached, and the fallback
223
+ * is a plain pass-through so a build without the database package (or an
224
+ * older one) still serves requests.
225
+ */
226
+ declare type ContextRunner = <T>(fn: () => T) => T;
193
227
  /**
194
228
  * FIFO-bounded Map. Wraps `Map` with a hard size cap; on overflow,
195
229
  * the oldest entry (Map insertion order) is evicted. Used for the
@@ -330,7 +330,11 @@ export function installMiddlewareHotReload() {
330
330
  fsWatchers = [];
331
331
  };
332
332
  }
333
- const routeMiddlewareRegistry = new Map, routeApiResponseRegistry = new Set;
333
+ const routeMiddlewareRegistry = new Map;
334
+ export function clearRouteMiddlewareRegistry() {
335
+ routeMiddlewareRegistry.clear();
336
+ }
337
+ const routeApiResponseRegistry = new Set;
334
338
  function parseMiddlewareName(middleware) {
335
339
  const colonIndex = middleware.indexOf(":");
336
340
  if (colonIndex === -1)
@@ -1489,8 +1493,21 @@ export function createStacksRouter(config = {}) {
1489
1493
  }
1490
1494
  const ROUTE_SINGLETON_KEY = Symbol.for("@stacksjs/router:route-singleton");
1491
1495
  export const route = globalThis[ROUTE_SINGLETON_KEY] ??= createStacksRouter();
1492
- let routesLoadPromise = null;
1496
+ let routesLoadPromise = null, routingContextRunner = null;
1497
+ async function getRoutingContextRunner() {
1498
+ if (!routingContextRunner)
1499
+ try {
1500
+ const { withRoutingContext } = await import("@stacksjs/database");
1501
+ routingContextRunner = typeof withRoutingContext === "function" ? withRoutingContext : (fn) => fn();
1502
+ } catch {
1503
+ routingContextRunner = (fn) => fn();
1504
+ }
1505
+ return routingContextRunner;
1506
+ }
1493
1507
  export async function serverResponse(request, _body) {
1508
+ return (await getRoutingContextRunner())(() => handleServerRequest(request));
1509
+ }
1510
+ async function handleServerRequest(request) {
1494
1511
  if (!routesLoadPromise) {
1495
1512
  log.debug("[router] Loading routes for first time...");
1496
1513
  routesLoadPromise = route.importRoutes().catch((err) => {
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@stacksjs/router",
3
3
  "type": "module",
4
4
  "sideEffects": false,
5
- "version": "0.70.255",
5
+ "version": "0.70.257",
6
6
  "description": "The Stacks framework router.",
7
7
  "author": "Chris Breuer",
8
8
  "contributors": [
@@ -53,19 +53,19 @@
53
53
  },
54
54
  "dependencies": {
55
55
  "@stacksjs/bun-router": "^0.0.21",
56
- "@stacksjs/collections": "0.70.255",
56
+ "@stacksjs/collections": "0.70.257",
57
57
  "ts-rate-limiter": "^0.4.2"
58
58
  },
59
59
  "devDependencies": {
60
- "@stacksjs/actions": "0.70.255",
61
- "@stacksjs/config": "0.70.255",
60
+ "@stacksjs/actions": "0.70.257",
61
+ "@stacksjs/config": "0.70.257",
62
62
  "better-dx": "^0.2.17",
63
- "@stacksjs/error-handling": "0.70.255",
64
- "@stacksjs/logging": "0.70.255",
65
- "@stacksjs/orm": "0.70.255",
66
- "@stacksjs/path": "0.70.255",
67
- "@stacksjs/storage": "0.70.255",
68
- "@stacksjs/types": "0.70.255",
69
- "@stacksjs/validation": "0.70.255"
63
+ "@stacksjs/error-handling": "0.70.257",
64
+ "@stacksjs/logging": "0.70.257",
65
+ "@stacksjs/orm": "0.70.257",
66
+ "@stacksjs/path": "0.70.257",
67
+ "@stacksjs/storage": "0.70.257",
68
+ "@stacksjs/types": "0.70.257",
69
+ "@stacksjs/validation": "0.70.257"
70
70
  }
71
71
  }