@teyik0/furin 0.2.0-alpha.4 → 0.3.0-alpha.1

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 (263) hide show
  1. package/README.md +2 -3
  2. package/dist/adapter/bun.d.ts +17 -1
  3. package/dist/adapter/package.d.ts +26 -0
  4. package/dist/adapter/static.d.ts +1 -1
  5. package/dist/build/bun-build.d.ts +3 -0
  6. package/dist/build/client.d.ts +1 -1
  7. package/dist/build/compile-entry.d.ts +3 -3
  8. package/dist/build/compile-entry.ts +88 -83
  9. package/dist/build/entry-template.d.ts +37 -9
  10. package/dist/build/entry-template.ts +109 -56
  11. package/dist/build/hydrate.d.ts +2 -2
  12. package/dist/build/index.js +6817 -4544
  13. package/dist/build/production-instrumentation.d.ts +1 -0
  14. package/dist/build/route-types.d.ts +1 -1
  15. package/dist/build/scan-server.d.ts +10 -4
  16. package/dist/build/server-routes-entry.d.ts +3 -3
  17. package/dist/build/server-routes-entry.ts +5 -11
  18. package/dist/build/ssg-cache.d.ts +1 -1
  19. package/dist/build/types.d.ts +37 -1
  20. package/dist/cli/config.d.ts +6 -1
  21. package/dist/cli/index.js +6854 -4549
  22. package/dist/client/boundaries.d.ts +4 -4
  23. package/dist/client/internal/runtime-types.d.ts +30 -0
  24. package/dist/client/link.d.ts +2 -1
  25. package/dist/client/link.js +712 -338
  26. package/dist/client/router/boundary-tree.d.ts +1 -1
  27. package/dist/client/router/index.d.ts +1 -1
  28. package/dist/client/router/link-utils.d.ts +5 -1
  29. package/dist/client/router/sync-catch-up.d.ts +4 -4
  30. package/dist/client/router/types.d.ts +2 -2
  31. package/dist/client-only.d.ts +1 -0
  32. package/dist/client-only.js +1 -0
  33. package/dist/client.d.ts +108 -111
  34. package/dist/client.js +16 -17
  35. package/dist/config.d.ts +11 -2
  36. package/dist/config.js +7 -3
  37. package/dist/devtools/devtools-element.d.ts +1 -0
  38. package/dist/devtools/protocol.d.ts +119 -0
  39. package/dist/furin.d.ts +40 -21
  40. package/dist/furin.js +7198 -3518
  41. package/dist/isomorphic.d.ts +18 -0
  42. package/dist/plugin/binding-scope.d.ts +2 -0
  43. package/dist/plugin/dead-code-elimination.d.ts +3 -0
  44. package/dist/plugin/index.js +933 -423
  45. package/dist/plugin/transform-client.d.ts +0 -2
  46. package/dist/plugin/transform-isomorphic.d.ts +9 -0
  47. package/dist/rsc/build/environment.d.ts +2 -0
  48. package/dist/rsc/build/index.d.ts +15 -0
  49. package/dist/rsc/client-codec.d.ts +1 -0
  50. package/dist/rsc/codec.d.ts +4 -0
  51. package/dist/rsc/flight-drain.d.ts +11 -0
  52. package/dist/rsc/render-error.d.ts +17 -0
  53. package/dist/rsc/server-codec.d.ts +2 -0
  54. package/dist/rsc/server-codec.js +24 -0
  55. package/dist/rsc/shared.d.ts +30 -0
  56. package/dist/rsc/symbols.d.ts +2 -0
  57. package/dist/rsc/transport-benchmark.d.ts +13 -0
  58. package/dist/rsc/version.d.ts +7 -0
  59. package/dist/rsc-client.d.ts +4 -0
  60. package/dist/rsc-client.js +166 -0
  61. package/dist/rsc-server.d.ts +11 -0
  62. package/dist/rsc.d.ts +11 -0
  63. package/dist/rsc.js +353 -0
  64. package/dist/server/auto-invalidate/registry.d.ts +8 -1
  65. package/dist/server/cache/dev-loader.d.ts +3 -2
  66. package/dist/server/cache/index.d.ts +2 -2
  67. package/dist/server/cache/invalidation.d.ts +20 -2
  68. package/dist/server/cache/isr-ssg.d.ts +5 -1
  69. package/dist/server/cache/isr.d.ts +15 -2
  70. package/dist/server/cache/registry.d.ts +3 -3
  71. package/dist/server/cache/route-cache.d.ts +10 -8
  72. package/dist/server/cache/ssg.d.ts +6 -2
  73. package/dist/server/cache/store-view.d.ts +11 -0
  74. package/dist/server/dev-page-plugin.d.ts +4 -0
  75. package/dist/server/devtools/hub.d.ts +11 -0
  76. package/dist/server/devtools/instrumentation.d.ts +22 -0
  77. package/dist/server/devtools/instrumentation.production.d.ts +44 -0
  78. package/dist/server/devtools/plugin.d.ts +3 -0
  79. package/dist/server/devtools/request-context.d.ts +6 -0
  80. package/dist/server/instance.d.ts +86 -0
  81. package/dist/server/internal.d.ts +18 -1
  82. package/dist/server/internal.js +74 -7
  83. package/dist/server/lang-detect.d.ts +1 -0
  84. package/dist/server/render/assemble.d.ts +4 -0
  85. package/dist/server/render/element.d.ts +1 -1
  86. package/dist/server/render/index.d.ts +2 -2
  87. package/dist/server/render/index.ts +6 -2
  88. package/dist/server/render/loaders.d.ts +8 -0
  89. package/dist/server/render/not-found.d.ts +1 -1
  90. package/dist/server/render/ppr-route.d.ts +8 -0
  91. package/dist/server/render/route-frame-transport.d.ts +2 -0
  92. package/dist/server/render/shell.ts +0 -1
  93. package/dist/server/render/ssr.d.ts +2 -3
  94. package/dist/server/render/template.d.ts +3 -2
  95. package/dist/server/router/discovery.d.ts +1 -1
  96. package/dist/server/router/hmr.d.ts +1 -1
  97. package/dist/server/router/index.d.ts +2 -1
  98. package/dist/server/router/index.js +23 -23
  99. package/dist/server/router/index.ts +1 -1
  100. package/dist/server/router/patterns.d.ts +1 -1
  101. package/dist/server/router/plugin.d.ts +4 -7
  102. package/dist/server/router/schema-merge.d.ts +3 -0
  103. package/dist/server/router/schemas.d.ts +12 -7
  104. package/dist/server/router/types.d.ts +1 -1
  105. package/dist/server/runtime-env.js +2 -2
  106. package/dist/server/sync/adapter.d.ts +45 -26
  107. package/dist/server/sync/config.d.ts +9 -2
  108. package/dist/server/sync/fingerprint.d.ts +2 -1
  109. package/dist/server/sync/index.d.ts +15 -4
  110. package/dist/server/sync/index.js +2868 -0
  111. package/dist/server/sync/notifier.d.ts +18 -0
  112. package/dist/server/sync/plugin.d.ts +53 -54
  113. package/dist/server/sync/postgres/index.d.ts +19 -0
  114. package/dist/server/sync/postgres/index.js +252 -0
  115. package/dist/server/sync/postgres/migrate.d.ts +2 -0
  116. package/dist/server/sync/postgres/migrate.js +16 -0
  117. package/dist/server/sync/postgres/migration.sql +66 -0
  118. package/dist/server/sync/redis/index.d.ts +30 -0
  119. package/dist/server/sync/redis/index.js +336 -0
  120. package/dist/server/sync/redis/scripts.d.ts +4 -0
  121. package/dist/server/sync/response.d.ts +10 -1
  122. package/dist/server/sync/runtime.d.ts +6 -0
  123. package/dist/server/sync/sqlite/index.d.ts +20 -0
  124. package/dist/server/sync/sqlite/index.js +250 -0
  125. package/dist/server/sync/stream.d.ts +12 -4
  126. package/dist/server-only.d.ts +1 -0
  127. package/dist/server-only.js +1 -0
  128. package/dist/shared/parser.d.ts +2 -0
  129. package/dist/shared/prefix.d.ts +15 -0
  130. package/dist/shared/route-frame.d.ts +49 -0
  131. package/dist/shared/utils/index.d.ts +1 -1
  132. package/package.json +59 -12
  133. package/src/adapter/bun.ts +175 -55
  134. package/src/adapter/package.ts +208 -0
  135. package/src/adapter/static.ts +94 -48
  136. package/src/build/bun-build.ts +11 -0
  137. package/src/build/client.ts +31 -27
  138. package/src/build/compile-entry.ts +88 -83
  139. package/src/build/entry-template.ts +109 -56
  140. package/src/build/hydrate.ts +37 -10
  141. package/src/build/index.ts +111 -28
  142. package/src/build/production-instrumentation.ts +14 -0
  143. package/src/build/route-types.ts +7 -8
  144. package/src/build/scan-server.ts +26 -9
  145. package/src/build/server-routes-entry.ts +5 -11
  146. package/src/build/ssg-cache.ts +13 -4
  147. package/src/build/types.ts +38 -1
  148. package/src/cli/config.ts +9 -3
  149. package/src/cli/index.ts +73 -24
  150. package/src/client/boundaries.tsx +42 -18
  151. package/src/client/default-screens.tsx +4 -4
  152. package/src/client/internal/runtime-types.ts +31 -0
  153. package/src/client/link.tsx +35 -37
  154. package/src/client/router/boundary-tree.tsx +2 -2
  155. package/src/client/router/context.ts +13 -9
  156. package/src/client/router/index.ts +3 -0
  157. package/src/client/router/link-utils.ts +66 -3
  158. package/src/client/router/navigation.ts +9 -15
  159. package/src/client/router/provider.tsx +74 -52
  160. package/src/client/router/search/index.ts +5 -7
  161. package/src/client/router/search-store.ts +5 -1
  162. package/src/client/router/spa-response.ts +3 -4
  163. package/src/client/router/sync-catch-up.ts +9 -8
  164. package/src/client/router/types.ts +2 -2
  165. package/src/client/sync.ts +3 -2
  166. package/src/client-only.ts +1 -0
  167. package/src/client.ts +221 -174
  168. package/src/config.ts +14 -1
  169. package/src/devtools/devtools-element.js +884 -0
  170. package/src/devtools/protocol.ts +139 -0
  171. package/src/furin.ts +414 -171
  172. package/src/isomorphic.ts +66 -0
  173. package/src/plugin/binding-scope.ts +158 -0
  174. package/src/plugin/dead-code-elimination.ts +202 -0
  175. package/src/plugin/index.ts +14 -18
  176. package/src/plugin/transform-client.ts +237 -273
  177. package/src/plugin/transform-isomorphic.ts +464 -0
  178. package/src/rsc/build/environment.ts +30 -0
  179. package/src/rsc/build/index.ts +94 -0
  180. package/src/rsc/client-codec.ts +11 -0
  181. package/src/rsc/codec.ts +85 -0
  182. package/src/rsc/flight-drain.ts +68 -0
  183. package/src/rsc/react-server-dom-webpack.d.ts +23 -0
  184. package/src/rsc/render-error.ts +120 -0
  185. package/src/rsc/server-codec.ts +25 -0
  186. package/src/rsc/shared.tsx +113 -0
  187. package/src/rsc/symbols.ts +2 -0
  188. package/src/rsc/transport-benchmark.ts +73 -0
  189. package/src/rsc/version.ts +57 -0
  190. package/src/rsc-client.ts +12 -0
  191. package/src/rsc-server.tsx +91 -0
  192. package/src/rsc.tsx +75 -0
  193. package/src/server/auto-invalidate/registry.ts +23 -1
  194. package/src/server/auto-invalidate/runtime.ts +58 -7
  195. package/src/server/cache/dev-loader.ts +143 -44
  196. package/src/server/cache/index.ts +7 -3
  197. package/src/server/cache/invalidation.ts +100 -41
  198. package/src/server/cache/isr-ssg.ts +12 -3
  199. package/src/server/cache/isr.ts +95 -5
  200. package/src/server/cache/registry.ts +17 -20
  201. package/src/server/cache/route-cache.ts +49 -36
  202. package/src/server/cache/ssg.ts +19 -4
  203. package/src/server/cache/store-view.ts +30 -0
  204. package/src/server/context-logger.ts +7 -7
  205. package/src/server/dev-page-plugin.ts +8 -6
  206. package/src/server/devtools/hub.ts +64 -0
  207. package/src/server/devtools/instrumentation.production.ts +80 -0
  208. package/src/server/devtools/instrumentation.ts +72 -0
  209. package/src/server/devtools/plugin.ts +267 -0
  210. package/src/server/devtools/request-context.ts +76 -0
  211. package/src/server/instance.ts +252 -0
  212. package/src/server/internal.ts +102 -5
  213. package/src/server/lang-detect.ts +14 -10
  214. package/src/server/render/assemble.ts +73 -10
  215. package/src/server/render/element.tsx +11 -8
  216. package/src/server/render/index.ts +6 -2
  217. package/src/server/render/isr.ts +154 -110
  218. package/src/server/render/loaders.ts +259 -45
  219. package/src/server/render/not-found.ts +18 -17
  220. package/src/server/render/ppr-route.ts +146 -0
  221. package/src/server/render/route-frame-transport.ts +62 -0
  222. package/src/server/render/shell.ts +0 -1
  223. package/src/server/render/ssg.ts +6 -5
  224. package/src/server/render/ssr.ts +170 -70
  225. package/src/server/render/template.ts +59 -25
  226. package/src/server/router/discovery.ts +39 -22
  227. package/src/server/router/hmr.ts +15 -13
  228. package/src/server/router/index.ts +1 -1
  229. package/src/server/router/patterns.ts +16 -15
  230. package/src/server/router/plugin.ts +151 -158
  231. package/src/server/router/schema-merge.ts +52 -0
  232. package/src/server/router/schemas.ts +55 -73
  233. package/src/server/router/types.ts +1 -1
  234. package/src/server/sync/adapter.ts +44 -28
  235. package/src/server/sync/config.ts +22 -9
  236. package/src/server/sync/fingerprint.ts +8 -3
  237. package/src/server/sync/index.ts +37 -13
  238. package/src/server/sync/notifier.ts +109 -0
  239. package/src/server/sync/plugin.ts +184 -90
  240. package/src/server/sync/postgres/index.ts +301 -0
  241. package/src/server/sync/postgres/migrate.ts +15 -0
  242. package/src/server/sync/postgres/migration.sql +66 -0
  243. package/src/server/sync/redis/index.ts +342 -0
  244. package/src/server/sync/redis/scripts.ts +74 -0
  245. package/src/server/sync/response.ts +141 -16
  246. package/src/server/sync/runtime.ts +27 -0
  247. package/src/server/sync/sql.d.ts +4 -0
  248. package/src/server/sync/sqlite/index.ts +337 -0
  249. package/src/server/sync/sqlite/migration.sql +70 -0
  250. package/src/server/sync/stream.ts +136 -70
  251. package/src/server-only.ts +1 -0
  252. package/src/shared/await.tsx +4 -4
  253. package/src/shared/deferred-ndjson.ts +34 -6
  254. package/src/shared/digest.ts +2 -2
  255. package/src/shared/parser.ts +5 -0
  256. package/src/shared/prefix.ts +38 -0
  257. package/src/shared/route-frame.ts +429 -0
  258. package/src/shared/search-params.ts +7 -7
  259. package/src/shared/utils/index.ts +5 -3
  260. package/dist/server/dev-inspector.d.ts +0 -89
  261. package/dist/server/sync/memory-adapter.d.ts +0 -17
  262. package/src/server/dev-inspector.ts +0 -60
  263. package/src/server/sync/memory-adapter.ts +0 -196
@@ -1,3 +1,5 @@
1
+ import { type FurinInstance, instanceSlot } from "../instance.ts";
2
+
1
3
  export class AutoInvalidateRegistry {
2
4
  private readonly pathToTags = new Map<string, Set<string>>();
3
5
  private readonly tagToPaths = new Map<string, Set<string>>();
@@ -57,4 +59,24 @@ export class AutoInvalidateRegistry {
57
59
  }
58
60
  }
59
61
 
60
- export const autoInvalidateRegistry = new AutoInvalidateRegistry();
62
+ const instanceAutoInvalidateRegistry = instanceSlot(() => new AutoInvalidateRegistry());
63
+
64
+ /** The current furin instance's registry (see server/instance.ts). */
65
+ export function getAutoInvalidateRegistry(instance?: FurinInstance): AutoInvalidateRegistry {
66
+ return instanceAutoInvalidateRegistry(instance);
67
+ }
68
+
69
+ /**
70
+ * Instance-scoped facade kept under the historical singleton name so call
71
+ * sites read the same — every method resolves the current instance's registry.
72
+ */
73
+ export const autoInvalidateRegistry: Pick<
74
+ AutoInvalidateRegistry,
75
+ "registerLoaderTags" | "pathsForTags" | "unregisterPath" | "reset"
76
+ > = {
77
+ registerLoaderTags: (urlPath, tags) =>
78
+ instanceAutoInvalidateRegistry().registerLoaderTags(urlPath, tags),
79
+ pathsForTags: (tags) => instanceAutoInvalidateRegistry().pathsForTags(tags),
80
+ unregisterPath: (urlPath) => instanceAutoInvalidateRegistry().unregisterPath(urlPath),
81
+ reset: () => instanceAutoInvalidateRegistry().reset(),
82
+ };
@@ -1,6 +1,18 @@
1
1
  import type { Context } from "elysia";
2
- import { consumePendingInvalidations, revalidatePath } from "../cache/invalidation.ts";
3
- import { autoInvalidateRegistry } from "./registry.ts";
2
+ import {
3
+ callCachePurger,
4
+ consumePendingInvalidations,
5
+ revalidatePath,
6
+ revalidatePathForInstance,
7
+ } from "../cache/invalidation.ts";
8
+ import { pathWithoutSearch } from "../cache/route-cache.ts";
9
+ import {
10
+ currentInstrumentationRequest,
11
+ emitCacheInvalidated,
12
+ } from "../devtools/instrumentation.ts";
13
+ import { allInstances, withInstance } from "../instance.ts";
14
+ import { IS_DEV } from "../runtime-env.ts";
15
+ import { getAutoInvalidateRegistry } from "./registry.ts";
4
16
  import type { InvalidationInput, InvalidationRule } from "./types.ts";
5
17
 
6
18
  function toRules(input: InvalidationInput): readonly InvalidationRule[] {
@@ -12,18 +24,17 @@ function statusFromResponseValue(responseValue: unknown): number | undefined {
12
24
  return responseValue.status;
13
25
  }
14
26
  if (responseValue && typeof responseValue === "object" && "status" in responseValue) {
15
- const status = (responseValue as { status?: unknown }).status;
27
+ const { status } = responseValue as { status?: unknown };
16
28
  if (typeof status === "number") {
17
29
  return status;
18
30
  }
19
31
  }
20
32
  if (responseValue && typeof responseValue === "object" && "code" in responseValue) {
21
- const code = (responseValue as { code?: unknown }).code;
33
+ const { code } = responseValue as { code?: unknown };
22
34
  if (typeof code === "number") {
23
35
  return code;
24
36
  }
25
37
  }
26
- return;
27
38
  }
28
39
 
29
40
  export function isSuccessfulMutationResponse(
@@ -113,10 +124,50 @@ export function appendPendingInvalidationHeader(set: Context["set"]): string[] {
113
124
 
114
125
  export function revalidateTag(tags: string | readonly string[]): boolean {
115
126
  const tagList = typeof tags === "string" ? [tags] : [...tags];
127
+ // Tags are cross-app by design: with several mounted furin instances a
128
+ // shared mutation must be able to invalidate pages rendered by any of them.
129
+ // But each instance's tag-registered paths are evicted from THAT instance's
130
+ // caches only — the cross-app fan-out of `revalidatePath` would also evict
131
+ // a sibling app's unrelated page that merely shares the pathname.
116
132
  let deleted = false;
117
- for (const path of autoInvalidateRegistry.pathsForTags(tagList)) {
118
- deleted = revalidatePath(path, "page") || deleted;
133
+ const purgedPaths = new Set<string>();
134
+ for (const instance of allInstances()) {
135
+ let instanceDeleted = false;
136
+ const instancePurgedPaths = new Set<string>();
137
+ // Registry paths are LOGICAL (unprefixed); the CDN caches the PHYSICAL
138
+ // request URL, so prefix each with the instance's mount prefix before
139
+ // queueing it for purge — otherwise a mounted app's `/admin/x` stays stale.
140
+ for (const path of getAutoInvalidateRegistry(instance).pathsForTags(tagList)) {
141
+ const logicalPath = pathWithoutSearch(path);
142
+ const result = revalidatePathForInstance(instance, logicalPath, "page", false);
143
+ deleted = result.deleted || deleted;
144
+ instanceDeleted = result.deleted || instanceDeleted;
145
+ purgedPaths.add(`${instance.prefix}${logicalPath}`);
146
+ instancePurgedPaths.add(logicalPath);
147
+ for (const purged of result.purgedPaths) {
148
+ purgedPaths.add(`${instance.prefix}${purged}`);
149
+ instancePurgedPaths.add(purged);
150
+ }
151
+ }
152
+ if (IS_DEV) {
153
+ withInstance(instance, () => {
154
+ const request = currentInstrumentationRequest();
155
+ const operationId = request === undefined ? null : request.operationId;
156
+ const requestId = request === undefined ? null : request.requestId;
157
+ emitCacheInvalidated({
158
+ deleted: instanceDeleted,
159
+ operationId,
160
+ purgedPaths: instancePurgedPaths.size,
161
+ reason: "tag",
162
+ requestId,
163
+ target: tagList.join(","),
164
+ });
165
+ });
166
+ }
119
167
  }
168
+ // One batched CDN purge — the CDN sits in front of every mounted app, so
169
+ // physical paths from all instances go out together, deduped.
170
+ callCachePurger([...purgedPaths]);
120
171
  return deleted;
121
172
  }
122
173
 
@@ -1,5 +1,18 @@
1
1
  import { statSync } from "node:fs";
2
+ import { relative } from "node:path";
2
3
  import { autoInvalidateRegistry } from "../auto-invalidate/registry";
4
+ import {
5
+ currentInstrumentationRequest,
6
+ emitCacheInvalidated,
7
+ emitCacheAccess as emitInstrumentationCacheAccess,
8
+ } from "../devtools/instrumentation.ts";
9
+ import {
10
+ allStateBuckets,
11
+ currentInstance,
12
+ type FurinInstance,
13
+ instanceSlot,
14
+ withInstance,
15
+ } from "../instance.ts";
3
16
  import { registerCacheInvalidator } from "./registry";
4
17
  import { createRouteCache, type RevalidateType } from "./route-cache";
5
18
 
@@ -18,95 +31,156 @@ export interface InvalidateOutcome {
18
31
  ssg: number;
19
32
  }
20
33
 
21
- const sourceFileToCacheKeys = new Map<string, Set<string>>();
22
-
23
34
  interface CacheKindHandle {
24
35
  cache: ReturnType<typeof createDevLoaderCache>;
25
36
  kind: "isr" | "ssg";
26
37
  }
27
38
 
28
- function indexEntryDependencies(cacheKey: string, deps: string[]): void {
39
+ interface DevLoaderState {
40
+ isr: CacheKindHandle;
41
+ sourceFileToCacheKeys: Map<string, Set<string>>;
42
+ ssg: CacheKindHandle;
43
+ }
44
+
45
+ function indexEntryDependencies(
46
+ index: Map<string, Set<string>>,
47
+ cacheKey: string,
48
+ deps: string[]
49
+ ): void {
29
50
  for (const dep of deps) {
30
- let bucket = sourceFileToCacheKeys.get(dep);
51
+ let bucket = index.get(dep);
31
52
  if (!bucket) {
32
53
  bucket = new Set<string>();
33
- sourceFileToCacheKeys.set(dep, bucket);
54
+ index.set(dep, bucket);
34
55
  }
35
56
  bucket.add(cacheKey);
36
57
  }
37
58
  }
38
59
 
39
- function unindexEntryDependencies(cacheKey: string, deps: string[]): void {
60
+ function unindexEntryDependencies(
61
+ index: Map<string, Set<string>>,
62
+ cacheKey: string,
63
+ deps: string[]
64
+ ): void {
40
65
  for (const dep of deps) {
41
- const bucket = sourceFileToCacheKeys.get(dep);
66
+ const bucket = index.get(dep);
42
67
  if (!bucket) {
43
68
  continue;
44
69
  }
45
70
  bucket.delete(cacheKey);
46
71
  if (bucket.size === 0) {
47
- sourceFileToCacheKeys.delete(dep);
72
+ index.delete(dep);
48
73
  }
49
74
  }
50
75
  }
51
76
 
52
- function createDevLoaderCache(name: string) {
77
+ function createDevLoaderCache(name: string, index: Map<string, Set<string>>) {
53
78
  return createRouteCache<DevLoaderCacheEntry>({
54
79
  name,
55
80
  onDelete: (key, entry) => {
56
- unindexEntryDependencies(key, entry.dependencies);
57
- const urlPath = urlPathFromCacheKey(key);
81
+ unindexEntryDependencies(index, key, entry.dependencies);
82
+ const urlPath = urlPathWithSearchFromCacheKey(key);
58
83
  if (urlPath) {
59
84
  autoInvalidateRegistry.unregisterPath(urlPath);
60
85
  }
61
86
  },
62
87
  onSet: (key, entry, previous) => {
63
88
  if (previous) {
64
- unindexEntryDependencies(key, previous.dependencies);
89
+ unindexEntryDependencies(index, key, previous.dependencies);
65
90
  }
66
- indexEntryDependencies(key, entry.dependencies);
91
+ indexEntryDependencies(index, key, entry.dependencies);
67
92
  },
68
93
  pathFromKey: urlPathFromCacheKey,
69
94
  });
70
95
  }
71
96
 
72
- export const devISRLoaderCache = createDevLoaderCache("render:dev-isr-loader");
73
- export const devSSGLoaderCache = createDevLoaderCache("render:dev-ssg-loader");
74
-
75
- const isrHandle: CacheKindHandle = { cache: devISRLoaderCache, kind: "isr" };
76
- const ssgHandle: CacheKindHandle = { cache: devSSGLoaderCache, kind: "ssg" };
97
+ // Per-instance dev loader caches + source-dependency index — two furin
98
+ // instances in one dev process must not invalidate each other's loader data.
99
+ const instanceDevLoaderState = instanceSlot((instance): DevLoaderState => {
100
+ const sourceFileToCacheKeys = new Map<string, Set<string>>();
101
+ const isrCache = createDevLoaderCache("render:dev-isr-loader", sourceFileToCacheKeys);
102
+ const ssgCache = createDevLoaderCache("render:dev-ssg-loader", sourceFileToCacheKeys);
103
+ registerCacheInvalidator(isrCache, instance);
104
+ registerCacheInvalidator(ssgCache, instance);
105
+ return {
106
+ isr: { cache: isrCache, kind: "isr" },
107
+ sourceFileToCacheKeys,
108
+ ssg: { cache: ssgCache, kind: "ssg" },
109
+ };
110
+ });
77
111
 
78
- function setEntry(handle: CacheKindHandle, key: string, entry: DevLoaderCacheEntry): void {
79
- handle.cache.set(key, entry);
112
+ function emitCacheAccess(
113
+ cache: "isr-loader" | "ssg-loader",
114
+ key: string,
115
+ entry: DevLoaderCacheEntry | undefined
116
+ ): void {
117
+ const request = currentInstrumentationRequest();
118
+ const path = urlPathFromCacheKey(key);
119
+ if (request === undefined || path === null) {
120
+ return;
121
+ }
122
+ let outcome: "hit" | "miss" | "stale";
123
+ if (entry === undefined) {
124
+ outcome = "miss";
125
+ } else {
126
+ outcome = isDevLoaderCacheValid(entry) ? "hit" : "stale";
127
+ }
128
+ emitInstrumentationCacheAccess({
129
+ cache,
130
+ operationId: request.operationId,
131
+ outcome,
132
+ path,
133
+ requestId: request.requestId,
134
+ });
80
135
  }
81
136
 
82
137
  export function getDevISRLoaderCache(key: string): DevLoaderCacheEntry | undefined {
83
- return devISRLoaderCache.get(key);
138
+ const entry = instanceDevLoaderState().isr.cache.get(key);
139
+ emitCacheAccess("isr-loader", key, entry);
140
+ return entry;
84
141
  }
85
142
 
86
143
  export function setDevISRLoaderCache(key: string, entry: DevLoaderCacheEntry): void {
87
- setEntry(isrHandle, key, entry);
144
+ instanceDevLoaderState().isr.cache.set(key, entry);
88
145
  }
89
146
 
90
147
  export function getDevSSGLoaderCache(key: string): DevLoaderCacheEntry | undefined {
91
- return devSSGLoaderCache.get(key);
148
+ const entry = instanceDevLoaderState().ssg.cache.get(key);
149
+ emitCacheAccess("ssg-loader", key, entry);
150
+ return entry;
92
151
  }
93
152
 
94
153
  export function setDevSSGLoaderCache(key: string, entry: DevLoaderCacheEntry): void {
95
- setEntry(ssgHandle, key, entry);
154
+ instanceDevLoaderState().ssg.cache.set(key, entry);
96
155
  }
97
156
 
98
157
  export function urlPathFromCacheKey(key: string): string | null {
99
- const sep = key.lastIndexOf(":/");
158
+ const searchStart = key.indexOf("?");
159
+ const keyWithoutSearch = searchStart === -1 ? key : key.slice(0, searchStart);
160
+ const sep = keyWithoutSearch.lastIndexOf(":/");
100
161
  if (sep === -1) {
101
162
  return null;
102
163
  }
103
- return key.slice(sep + 1);
164
+ return keyWithoutSearch.slice(sep + 1);
165
+ }
166
+
167
+ function urlPathWithSearchFromCacheKey(key: string): string | null {
168
+ const urlPath = urlPathFromCacheKey(key);
169
+ if (urlPath === null) {
170
+ return null;
171
+ }
172
+ const searchStart = key.indexOf("?");
173
+ if (searchStart === -1) {
174
+ return urlPath;
175
+ }
176
+ return `${urlPath}${key.slice(searchStart)}`;
104
177
  }
105
178
 
106
179
  export function invalidateDevLoaderCacheByPath(
107
180
  path: string,
108
181
  type: RevalidateType
109
182
  ): InvalidateOutcome {
183
+ const state = instanceDevLoaderState();
110
184
  const cleared: string[] = [];
111
185
  let isr = 0;
112
186
  let ssg = 0;
@@ -119,7 +193,7 @@ export function invalidateDevLoaderCacheByPath(
119
193
  return urlPath === path || urlPath.startsWith(prefix);
120
194
  };
121
195
 
122
- for (const handle of [isrHandle, ssgHandle]) {
196
+ for (const handle of [state.isr, state.ssg]) {
123
197
  for (const key of [...handle.cache.keys()]) {
124
198
  const urlPath = urlPathFromCacheKey(key);
125
199
  if (urlPath === null || !matches(urlPath)) {
@@ -132,9 +206,9 @@ export function invalidateDevLoaderCacheByPath(
132
206
  handle.cache.delete(key);
133
207
  cleared.push(key);
134
208
  if (handle.kind === "isr") {
135
- isr++;
209
+ isr += 1;
136
210
  } else {
137
- ssg++;
211
+ ssg += 1;
138
212
  }
139
213
  }
140
214
  }
@@ -143,7 +217,8 @@ export function invalidateDevLoaderCacheByPath(
143
217
  }
144
218
 
145
219
  export function invalidateDevLoaderCacheBySource(filePath: string): InvalidateOutcome {
146
- const keys = sourceFileToCacheKeys.get(filePath);
220
+ const state = instanceDevLoaderState();
221
+ const keys = state.sourceFileToCacheKeys.get(filePath);
147
222
  if (!keys || keys.size === 0) {
148
223
  return { cleared: [], isr: 0, ssg: 0 };
149
224
  }
@@ -154,21 +229,32 @@ export function invalidateDevLoaderCacheBySource(filePath: string): InvalidateOu
154
229
 
155
230
  const snapshot = [...keys];
156
231
  for (const key of snapshot) {
157
- const isrEntry = devISRLoaderCache.get(key);
232
+ const isrEntry = state.isr.cache.get(key);
158
233
  if (isrEntry) {
159
- devISRLoaderCache.delete(key);
234
+ state.isr.cache.delete(key);
160
235
  cleared.push(key);
161
- isr++;
236
+ isr += 1;
162
237
  continue;
163
238
  }
164
- const ssgEntry = devSSGLoaderCache.get(key);
239
+ const ssgEntry = state.ssg.cache.get(key);
165
240
  if (ssgEntry) {
166
- devSSGLoaderCache.delete(key);
241
+ state.ssg.cache.delete(key);
167
242
  cleared.push(key);
168
- ssg++;
243
+ ssg += 1;
169
244
  }
170
245
  }
171
246
 
247
+ const request = currentInstrumentationRequest();
248
+ const operationId = request === undefined ? null : request.operationId;
249
+ const requestId = request === undefined ? null : request.requestId;
250
+ emitCacheInvalidated({
251
+ deleted: cleared.length > 0,
252
+ operationId,
253
+ purgedPaths: cleared.length,
254
+ reason: "source",
255
+ requestId,
256
+ target: relative(process.cwd(), filePath).replaceAll("\\", "/"),
257
+ });
172
258
  return { cleared, isr, ssg };
173
259
  }
174
260
 
@@ -183,7 +269,7 @@ export function isDevLoaderCacheValid(entry: DevLoaderCacheEntry): boolean {
183
269
  for (const dep of entry.dependencies) {
184
270
  let mtimeMs: number;
185
271
  try {
186
- mtimeMs = statSync(dep).mtimeMs;
272
+ ({ mtimeMs } = statSync(dep));
187
273
  } catch {
188
274
  return false;
189
275
  }
@@ -195,17 +281,30 @@ export function isDevLoaderCacheValid(entry: DevLoaderCacheEntry): boolean {
195
281
  }
196
282
 
197
283
  export function getAllDevISRLoaderEntries(): [string, DevLoaderCacheEntry][] {
198
- return [...devISRLoaderCache.entries()];
284
+ return [...instanceDevLoaderState().isr.cache.entries()];
199
285
  }
200
286
 
201
287
  export function getAllDevSSGLoaderEntries(): [string, DevLoaderCacheEntry][] {
202
- return [...devSSGLoaderCache.entries()];
288
+ return [...instanceDevLoaderState().ssg.cache.entries()];
289
+ }
290
+
291
+ /** @internal — clears dev loader caches for `instance` (default: current). */
292
+ export function clearDevLoaderCaches(instance?: FurinInstance): void {
293
+ const target = instance ?? currentInstance();
294
+ const state = instanceDevLoaderState(target);
295
+ // `.clear()` fires onDelete per entry, which unregisters the path from the
296
+ // auto-invalidate registry via the `currentInstance()`-scoped facade — bind
297
+ // the scope here so the unregistration hits the cleared instance's registry
298
+ // (re-entering an already-active identical scope is harmless).
299
+ withInstance(target, () => {
300
+ state.isr.cache.clear();
301
+ state.ssg.cache.clear();
302
+ });
303
+ state.sourceFileToCacheKeys.clear();
203
304
  }
204
305
 
205
306
  export function __resetDevLoaderCacheState(): void {
206
- devISRLoaderCache.clear();
207
- devSSGLoaderCache.clear();
208
- sourceFileToCacheKeys.clear();
209
- registerCacheInvalidator(devISRLoaderCache);
210
- registerCacheInvalidator(devSSGLoaderCache);
307
+ for (const instance of allStateBuckets()) {
308
+ clearDevLoaderCaches(instance);
309
+ }
211
310
  }
@@ -20,12 +20,16 @@ export {
20
20
  _runWithRequestInvalidationScope,
21
21
  callCachePurger,
22
22
  consumePendingInvalidations,
23
- getBuildId,
24
23
  revalidatePath,
25
- setBuildId,
26
24
  setCachePurger,
27
25
  } from "./invalidation";
28
- export { deleteISRCache, getISRCache, isrCache, setISRCache } from "./isr";
26
+ export {
27
+ deleteISRCache,
28
+ getISRCache,
29
+ isrCache,
30
+ setISRCache,
31
+ waitForPendingISRRevalidations,
32
+ } from "./isr";
29
33
  export type { ISRCacheEntry, SsgCacheEntry } from "./isr-ssg";
30
34
  export type { CacheInvalidator } from "./registry";
31
35
  export { registerCacheInvalidator } from "./registry";
@@ -1,40 +1,38 @@
1
- import { AsyncLocalStorage } from "node:async_hooks";
2
1
  import { createLogger } from "../context-logger";
3
- import { devISRLoaderCache, devSSGLoaderCache } from "./dev-loader";
4
- import { isrRouteCache } from "./isr";
5
- import { clearCacheInvalidators, getCacheInvalidators, registerCacheInvalidator } from "./registry";
2
+ import {
3
+ currentInstrumentationRequest,
4
+ emitCacheInvalidated,
5
+ } from "../devtools/instrumentation.ts";
6
+ import {
7
+ __clearInstanceRegistry,
8
+ allInstances,
9
+ allStateBuckets,
10
+ currentInstance,
11
+ type FurinInstance,
12
+ requestPendingInvalidations,
13
+ runWithInstanceScope,
14
+ withInstance,
15
+ } from "../instance.ts";
16
+ import { clearPprRouteCache } from "../render/ppr-route.ts";
17
+ import { IS_DEV } from "../runtime-env.ts";
18
+ import { clearDevLoaderCaches } from "./dev-loader";
19
+ import { clearPendingISRRevalidations, isrRouteCache } from "./isr";
20
+ import { getCacheInvalidators } from "./registry";
6
21
  import type { RevalidateType } from "./route-cache";
7
22
  import { ssgRouteCache } from "./ssg";
8
23
 
9
- // Register core caches at module load time so revalidatePath works out of the box.
10
- registerCacheInvalidator(isrRouteCache);
11
- registerCacheInvalidator(ssgRouteCache);
12
- registerCacheInvalidator(devISRLoaderCache);
13
- registerCacheInvalidator(devSSGLoaderCache);
14
-
15
- // ── Build ID ─────────────────────────────────────────────────────────────────
16
-
17
- let _buildId = "";
18
-
19
- export function setBuildId(id: string): void {
20
- _buildId = id;
21
- }
22
-
23
- export function getBuildId(): string {
24
- return _buildId;
25
- }
26
-
27
24
  // ── Pending invalidations (server → client bridge) ───────────────────────────
25
+ // The per-request set lives in the instance request scope (see instance.ts);
26
+ // outside a request scope invalidations accumulate in a process-global set.
28
27
 
29
- const _requestInvalidationScope = new AsyncLocalStorage<Set<string>>();
30
28
  const _globalPendingInvalidations = new Set<string>();
31
29
 
32
30
  function _activeInvalidationSet(): Set<string> {
33
- return _requestInvalidationScope.getStore() ?? _globalPendingInvalidations;
31
+ return requestPendingInvalidations() ?? _globalPendingInvalidations;
34
32
  }
35
33
 
36
34
  export function _runWithRequestInvalidationScope<T>(fn: () => T): T {
37
- return _requestInvalidationScope.run(new Set<string>(), fn);
35
+ return runWithInstanceScope(currentInstance(), fn);
38
36
  }
39
37
 
40
38
  export function consumePendingInvalidations(): string[] {
@@ -47,7 +45,12 @@ export function consumePendingInvalidations(): string[] {
47
45
  return paths;
48
46
  }
49
47
 
48
+ export function peekPendingInvalidations(): string[] {
49
+ return [..._activeInvalidationSet()];
50
+ }
51
+
50
52
  // ── CDN purger hook ───────────────────────────────────────────────────────────
53
+ // Process-global on purpose: the CDN sits in front of every mounted app.
51
54
 
52
55
  type CachePurger = (paths: string[]) => Promise<void>;
53
56
  let _cachePurger: CachePurger | null = null;
@@ -75,37 +78,93 @@ export function callCachePurger(paths: string[]): void {
75
78
 
76
79
  // ── revalidatePath ───────────────────────────────────────────────────────────
77
80
 
81
+ /**
82
+ * Invalidates `path` across EVERY mounted furin instance — paths are logical
83
+ * (unprefixed), and with shared data two apps can legitimately render the same
84
+ * logical path. This mirrors the historical shared-cache behaviour. Each
85
+ * instance's invalidation runs inside its own scope so cache `onDelete` hooks
86
+ * (auto-invalidate unregistration) hit the owning instance's registry.
87
+ */
78
88
  export function revalidatePath(path: string, type: RevalidateType): boolean {
79
- _activeInvalidationSet().add(type === "layout" ? `${path}:layout` : path);
80
-
81
89
  let deleted = false;
82
90
  const purgedPaths: string[] = [];
83
- for (const invalidator of getCacheInvalidators()) {
84
- const result = invalidator.invalidatePath(path, type);
91
+ for (const instance of allInstances()) {
92
+ const result = revalidatePathForInstance(instance, path, type);
85
93
  deleted = result.deleted || deleted;
86
- purgedPaths.push(...result.purgedPaths);
94
+ purgedPaths.push(`${instance.prefix}${path}`);
95
+ for (const purged of result.purgedPaths) {
96
+ purgedPaths.push(`${instance.prefix}${purged}`);
97
+ }
87
98
  }
88
99
 
89
- callCachePurger(dedupePaths([...purgedPaths, path]));
100
+ callCachePurger(dedupePaths(purgedPaths));
90
101
  return deleted;
91
102
  }
92
103
 
104
+ /**
105
+ * Invalidates `path` against a SINGLE instance's caches. `revalidateTag` uses
106
+ * this because it knows which instance registered each path — the cross-app
107
+ * fan-out of `revalidatePath` would also evict a sibling app's unrelated page
108
+ * that merely shares the pathname. Does NOT call the CDN purger; callers
109
+ * batch the returned `purgedPaths` themselves.
110
+ */
111
+ export function revalidatePathForInstance(
112
+ instance: FurinInstance,
113
+ path: string,
114
+ type: RevalidateType,
115
+ emitPathEvent?: boolean
116
+ ): { deleted: boolean; purgedPaths: string[] } {
117
+ _activeInvalidationSet().add(type === "layout" ? `${path}:layout` : path);
118
+
119
+ let deleted = false;
120
+ const purgedPaths: string[] = [];
121
+ withInstance(instance, () => {
122
+ for (const invalidator of getCacheInvalidators(instance)) {
123
+ const result = invalidator.invalidatePath(path, type);
124
+ deleted = result.deleted || deleted;
125
+ purgedPaths.push(...result.purgedPaths);
126
+ }
127
+ if (IS_DEV && emitPathEvent !== false) {
128
+ const request = currentInstrumentationRequest();
129
+ const operationId = request === undefined ? null : request.operationId;
130
+ const requestId = request === undefined ? null : request.requestId;
131
+ emitCacheInvalidated({
132
+ deleted,
133
+ operationId,
134
+ purgedPaths: new Set(purgedPaths).size,
135
+ reason: "path",
136
+ requestId,
137
+ target: path,
138
+ });
139
+ }
140
+ });
141
+ return { deleted, purgedPaths };
142
+ }
143
+
93
144
  function dedupePaths(paths: string[]): string[] {
94
145
  return [...new Set(paths)];
95
146
  }
96
147
 
97
148
  /** @internal — resets all module state between tests */
98
149
  export function __resetCacheState(): void {
99
- isrRouteCache.clear();
100
- ssgRouteCache.clear();
101
- devISRLoaderCache.clear();
102
- devSSGLoaderCache.clear();
103
- _buildId = "";
150
+ for (const instance of allStateBuckets()) {
151
+ // `.clear()` fires each entry's onDelete hook, and those hooks resolve
152
+ // the auto-invalidate registry via `currentInstance()` — scope to the
153
+ // bucket being cleared so unregistrations hit ITS registry instead of
154
+ // the ambient (default) one.
155
+ withInstance(instance, () => {
156
+ isrRouteCache(instance).clear();
157
+ ssgRouteCache(instance).clear();
158
+ clearDevLoaderCaches(instance);
159
+ clearPprRouteCache(instance);
160
+ clearPendingISRRevalidations(instance);
161
+ });
162
+ instance.buildId = "";
163
+ }
164
+ // Forget mounted instances (fresh furin() calls between tests must not hit
165
+ // prefix collisions) but keep the default bucket — cache slots above are
166
+ // cleared, while process-wide defaults like a beforeAll template survive.
167
+ __clearInstanceRegistry();
104
168
  _globalPendingInvalidations.clear();
105
169
  _cachePurger = null;
106
- clearCacheInvalidators();
107
- registerCacheInvalidator(isrRouteCache);
108
- registerCacheInvalidator(ssgRouteCache);
109
- registerCacheInvalidator(devISRLoaderCache);
110
- registerCacheInvalidator(devSSGLoaderCache);
111
170
  }