@rangojs/router 0.0.0-experimental.91 → 0.0.0-experimental.93

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.
@@ -0,0 +1,171 @@
1
+ import type { Debugger } from "../debug.js";
2
+
3
+ /**
4
+ * Manifest-readiness gate + rediscovery scheduler.
5
+ *
6
+ * Owns the four pieces of state that cooperate to keep
7
+ * `s.discoveryDone` (the promise the manifest virtual module's `load()`
8
+ * hook awaits) consistent across HMR fan-out:
9
+ *
10
+ * - **gatePending**: a Promise has been issued and not yet resolved.
11
+ * Workerd's manifest virtual module load() is blocked on it.
12
+ * - **inProgress**: a refresh's work callback is currently executing.
13
+ * - **queued**: a refresh was attempted while one was already in
14
+ * flight; the active run consumes this in its `finally` and
15
+ * recurses.
16
+ * - **pendingEvents**: a route-file event has been received (gate
17
+ * already reset) but the corresponding refresh's work hasn't started
18
+ * yet — i.e. the debounce hasn't fired. Set in `noteRouteEvent`,
19
+ * cleared at the start of each refresh cycle. Refresh's finally MUST
20
+ * hold the gate if this is true even when `queued` is false,
21
+ * otherwise an event whose debounce fires AFTER the active refresh
22
+ * completes (the "tail-race" window) would observe a resolved gate.
23
+ *
24
+ * The HMR-event flow (cloudflare-stress repro):
25
+ *
26
+ * t=0 Touch 1 → noteRouteEvent → pendingEvents=true, beginGate
27
+ * (gate1 pending)
28
+ * → debounce 100ms
29
+ * t=100 runRefreshCycle(work) → clear pendingEvents, work starts
30
+ * t=750 Touch 2 → noteRouteEvent → pendingEvents=true (no-op gate)
31
+ * → debounce fires at t=850
32
+ * t=800 refresh A's finally → queued=false, pendingEvents=true
33
+ * → HOLD gate (don't resolve)
34
+ * t=850 runRefreshCycle (debounce) → clear pendingEvents, work starts
35
+ * t=1500 refresh B's finally → queued=false, pendingEvents=false
36
+ * → resolveGate (gate1 resolves)
37
+ *
38
+ * @internal Exported only for unit tests.
39
+ */
40
+ export interface DiscoveryGate {
41
+ /**
42
+ * Reset the gate to a fresh pending Promise via `s.discoveryDone`.
43
+ * No-op when a gate is already pending — file watchers can fire
44
+ * multiple events for one save, and replacing the resolver would
45
+ * orphan the original promise (workerd's manifest load() would hang).
46
+ */
47
+ beginGate(): void;
48
+ /**
49
+ * Resolve the current pending gate. No-op when no gate is pending.
50
+ * Called at the tail of the last refresh cycle in a burst.
51
+ */
52
+ resolveGate(): void;
53
+ /**
54
+ * Record that a route-file event has arrived. Sets `pendingEvents`
55
+ * and begins the gate. Idempotent for both flags.
56
+ */
57
+ noteRouteEvent(): void;
58
+ /**
59
+ * Run one refresh cycle, managing queue + pending state around it.
60
+ * If a cycle is already in flight, sets `queued=true` and returns.
61
+ * Otherwise clears `pendingEvents`, runs `work`, and in `finally`:
62
+ *
63
+ * - queued → recurse, gate stays pending
64
+ * - pendingEvents → hold gate (next debounced cycle resolves)
65
+ * - neither → resolveGate
66
+ */
67
+ runRefreshCycle(work: () => Promise<void>): Promise<void>;
68
+ /** Snapshot of internal state. Test-only. */
69
+ readonly state: () => Readonly<{
70
+ gatePending: boolean;
71
+ inProgress: boolean;
72
+ queued: boolean;
73
+ pendingEvents: boolean;
74
+ }>;
75
+ }
76
+
77
+ /** State container the gate writes `discoveryDone` into. */
78
+ export interface GateOwner {
79
+ discoveryDone: Promise<void> | null | undefined;
80
+ }
81
+
82
+ export function createDiscoveryGate(
83
+ s: GateOwner,
84
+ debug?: Debugger,
85
+ ): DiscoveryGate {
86
+ let gatePending = false;
87
+ let gateResolver: () => void = () => {};
88
+ let inProgress = false;
89
+ let queued = false;
90
+ let pendingEvents = false;
91
+
92
+ const beginGate = (): void => {
93
+ if (gatePending) return;
94
+ s.discoveryDone = new Promise<void>((resolve) => {
95
+ gateResolver = resolve;
96
+ });
97
+ gatePending = true;
98
+ };
99
+
100
+ const resolveGate = (): void => {
101
+ if (!gatePending) return;
102
+ // Defer resolution while a refresh cycle is in flight or queued, or
103
+ // while an unprocessed route-file event is pending its debounce.
104
+ // Without this guard, cold-start's `discover().then(resolveGate)`
105
+ // could fire while an HMR-triggered runRefreshCycle is mid-flight,
106
+ // prematurely unblocking workerd's manifest load() against the
107
+ // stale cold-start gen. The active cycle's `finally` calls
108
+ // resolveGate again at the tail and finishes the resolution then.
109
+ if (inProgress || queued || pendingEvents) {
110
+ debug?.(
111
+ "hmr: resolveGate deferred — work in flight (inProgress=%s queued=%s pendingEvents=%s)",
112
+ inProgress,
113
+ queued,
114
+ pendingEvents,
115
+ );
116
+ return;
117
+ }
118
+ gatePending = false;
119
+ debug?.("hmr: discoveryDone resolved");
120
+ gateResolver();
121
+ };
122
+
123
+ const noteRouteEvent = (): void => {
124
+ pendingEvents = true;
125
+ beginGate();
126
+ };
127
+
128
+ const runRefreshCycle = async (work: () => Promise<void>): Promise<void> => {
129
+ if (inProgress) {
130
+ queued = true;
131
+ debug?.("hmr: rediscovery in flight — queued for a follow-up cycle");
132
+ return;
133
+ }
134
+ // Snapshot the current pendingEvents into "we're about to process";
135
+ // events arriving from now on re-set it.
136
+ pendingEvents = false;
137
+ inProgress = true;
138
+ try {
139
+ await work();
140
+ } finally {
141
+ inProgress = false;
142
+ if (queued) {
143
+ queued = false;
144
+ debug?.("hmr: consuming queued rediscovery");
145
+ runRefreshCycle(work).catch((err: unknown) => {
146
+ debug?.(
147
+ "hmr: queued cycle rejected — releasing gate (%s)",
148
+ err instanceof Error ? err.message : String(err),
149
+ );
150
+ // Belt-and-suspenders: even if the queued cycle's own try/catch
151
+ // missed something, ensure workerd doesn't hang.
152
+ resolveGate();
153
+ });
154
+ } else if (pendingEvents) {
155
+ debug?.(
156
+ "hmr: holding gate for pending events (debounce not yet fired)",
157
+ );
158
+ } else {
159
+ resolveGate();
160
+ }
161
+ }
162
+ };
163
+
164
+ return {
165
+ beginGate,
166
+ resolveGate,
167
+ noteRouteEvent,
168
+ runRefreshCycle,
169
+ state: () => ({ gatePending, inProgress, queued, pendingEvents }),
170
+ };
171
+ }
@@ -22,6 +22,32 @@ export function markSelfGenWrite(
22
22
  export function consumeSelfGenWrite(
23
23
  state: DiscoveryState,
24
24
  filePath: string,
25
+ ): boolean {
26
+ return checkSelfGenWrite(state, filePath, true);
27
+ }
28
+
29
+ /**
30
+ * Non-consuming variant. Used by the `handleHotUpdate` plugin hook to
31
+ * suppress vite's HMR cascade for our own gen-file writes WITHOUT
32
+ * consuming the entry — `consumeSelfGenWrite` (called later from the
33
+ * chokidar `change` handler in `handleRouteFileChange`) still needs to
34
+ * see and consume the same entry to short-circuit our regen path.
35
+ *
36
+ * Both hooks fire for the same file change event:
37
+ * - `handleHotUpdate` runs first (vite's HMR pipeline).
38
+ * - chokidar `change` callback runs after (filesystem watcher).
39
+ */
40
+ export function peekSelfGenWrite(
41
+ state: DiscoveryState,
42
+ filePath: string,
43
+ ): boolean {
44
+ return checkSelfGenWrite(state, filePath, false);
45
+ }
46
+
47
+ function checkSelfGenWrite(
48
+ state: DiscoveryState,
49
+ filePath: string,
50
+ consume: boolean,
25
51
  ): boolean {
26
52
  const info = state.selfWrittenGenFiles.get(filePath);
27
53
  if (!info) return false;
@@ -33,7 +59,7 @@ export function consumeSelfGenWrite(
33
59
  const current = readFileSync(filePath, "utf-8");
34
60
  const currentHash = createHash("sha256").update(current).digest("hex");
35
61
  if (currentHash === info.hash) {
36
- state.selfWrittenGenFiles.delete(filePath);
62
+ if (consume) state.selfWrittenGenFiles.delete(filePath);
37
63
  return true;
38
64
  }
39
65
  // Hash mismatch: file was changed externally. Keep the entry so a
@@ -47,16 +47,26 @@ export function createVersionInjectorPlugin(
47
47
  return null;
48
48
  }
49
49
 
50
- // Prepend imports at the top of the file. ES imports are hoisted
51
- // by the module system, so source position is irrelevant.
52
- const prepend: string[] = [];
53
- let newCode = code;
54
-
55
- if (!code.includes("virtual:rsc-router/routes-manifest")) {
56
- prepend.push(`import "virtual:rsc-router/routes-manifest";`);
57
- }
50
+ // Always prepend `import "virtual:rsc-router/routes-manifest"` as the
51
+ // first side-effect import. The manifest virtual module's `load()` hook
52
+ // awaits `s.discoveryDone` so that, by the time the rest of the entry
53
+ // including any module-level `router.reverse()` calls under `./router.js`
54
+ // evaluates, runtime discovery has rewritten `router.named-routes.gen.ts`
55
+ // with the full route table.
56
+ //
57
+ // ES module evaluation order matters here: while imports are *parsed*
58
+ // hoisted, side-effect imports are evaluated in source order in the
59
+ // dependency graph. A user-authored `import "virtual:rsc-router/..."`
60
+ // placed after `import "./router.js"` runs too late: the manifest
61
+ // gate fires after router.tsx has already crashed on a stale gen file.
62
+ // We always prepend; ESM dedups any user-written duplicate, so module
63
+ // initialization still runs once.
64
+ const prepend: string[] = [
65
+ `import "virtual:rsc-router/routes-manifest";`,
66
+ ];
58
67
 
59
68
  // Auto-inject VERSION if file uses createRSCHandler without version
69
+ let newCode = code;
60
70
  const needsVersion =
61
71
  code.includes("createRSCHandler") &&
62
72
  !code.includes("@rangojs/router:version") &&
@@ -70,9 +80,25 @@ export function createVersionInjectorPlugin(
70
80
  );
71
81
  }
72
82
 
73
- if (prepend.length === 0 && newCode === code) return null;
74
-
75
- newCode = prepend.join("\n") + (prepend.length > 0 ? "\n" : "") + newCode;
83
+ // Insert after any leading `/// <reference ... />` triple-slash
84
+ // directives (and surrounding blank lines). TypeScript requires those
85
+ // directives to precede all other code; putting our imports above
86
+ // them silently demotes the directives to plain comments.
87
+ const lines = newCode.split("\n");
88
+ let insertAt = 0;
89
+ while (insertAt < lines.length) {
90
+ const trimmed = lines[insertAt]!.trim();
91
+ if (trimmed === "" || /^\/\/\/\s*<reference\b/.test(trimmed)) {
92
+ insertAt++;
93
+ } else {
94
+ break;
95
+ }
96
+ }
97
+ newCode = [
98
+ ...lines.slice(0, insertAt),
99
+ ...prepend,
100
+ ...lines.slice(insertAt),
101
+ ].join("\n");
76
102
 
77
103
  return {
78
104
  code: newCode,