@teyik0/furin 0.2.0-alpha.4 → 0.2.0-alpha.5

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 (67) hide show
  1. package/dist/adapter/bun.d.ts +17 -1
  2. package/dist/adapter/package.d.ts +26 -0
  3. package/dist/build/client.d.ts +1 -1
  4. package/dist/build/compile-entry.d.ts +3 -3
  5. package/dist/build/compile-entry.ts +88 -83
  6. package/dist/build/entry-template.d.ts +35 -9
  7. package/dist/build/entry-template.ts +106 -56
  8. package/dist/build/hydrate.d.ts +1 -1
  9. package/dist/build/index.js +643 -247
  10. package/dist/build/scan-server.d.ts +10 -4
  11. package/dist/build/server-routes-entry.d.ts +3 -3
  12. package/dist/build/server-routes-entry.ts +5 -11
  13. package/dist/build/ssg-cache.d.ts +1 -1
  14. package/dist/build/types.d.ts +36 -1
  15. package/dist/cli/config.d.ts +6 -1
  16. package/dist/cli/index.js +662 -260
  17. package/dist/config.d.ts +11 -2
  18. package/dist/config.js +5 -1
  19. package/dist/furin.d.ts +36 -18
  20. package/dist/furin.js +630 -310
  21. package/dist/server/auto-invalidate/registry.d.ts +8 -1
  22. package/dist/server/cache/dev-loader.d.ts +3 -2
  23. package/dist/server/cache/invalidation.d.ts +19 -0
  24. package/dist/server/cache/isr.d.ts +6 -2
  25. package/dist/server/cache/registry.d.ts +3 -3
  26. package/dist/server/cache/ssg.d.ts +6 -2
  27. package/dist/server/cache/store-view.d.ts +11 -0
  28. package/dist/server/instance.d.ts +86 -0
  29. package/dist/server/internal.d.ts +16 -1
  30. package/dist/server/internal.js +72 -5
  31. package/dist/server/render/template.d.ts +3 -2
  32. package/dist/server/sync/config.d.ts +11 -0
  33. package/dist/server/sync/stream.d.ts +8 -1
  34. package/dist/shared/prefix.d.ts +15 -0
  35. package/package.json +6 -1
  36. package/src/adapter/bun.ts +113 -50
  37. package/src/adapter/package.ts +206 -0
  38. package/src/build/client.ts +9 -3
  39. package/src/build/compile-entry.ts +88 -83
  40. package/src/build/entry-template.ts +106 -56
  41. package/src/build/hydrate.ts +4 -2
  42. package/src/build/index.ts +99 -28
  43. package/src/build/scan-server.ts +23 -7
  44. package/src/build/server-routes-entry.ts +5 -11
  45. package/src/build/ssg-cache.ts +6 -2
  46. package/src/build/types.ts +37 -1
  47. package/src/cli/config.ts +9 -3
  48. package/src/cli/index.ts +21 -3
  49. package/src/config.ts +14 -1
  50. package/src/furin.ts +297 -143
  51. package/src/server/auto-invalidate/registry.ts +23 -1
  52. package/src/server/auto-invalidate/runtime.ts +29 -4
  53. package/src/server/cache/dev-loader.ts +77 -38
  54. package/src/server/cache/invalidation.ts +71 -30
  55. package/src/server/cache/isr.ts +20 -5
  56. package/src/server/cache/registry.ts +17 -20
  57. package/src/server/cache/ssg.ts +19 -4
  58. package/src/server/cache/store-view.ts +30 -0
  59. package/src/server/instance.ts +252 -0
  60. package/src/server/internal.ts +100 -5
  61. package/src/server/render/not-found.ts +9 -3
  62. package/src/server/render/ssr.ts +16 -4
  63. package/src/server/render/template.ts +57 -24
  64. package/src/server/sync/config.ts +32 -1
  65. package/src/server/sync/plugin.ts +4 -5
  66. package/src/server/sync/stream.ts +19 -9
  67. package/src/shared/prefix.ts +38 -0
@@ -1,6 +1,12 @@
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 { allInstances } from "../instance.ts";
9
+ import { getAutoInvalidateRegistry } from "./registry.ts";
4
10
  import type { InvalidationInput, InvalidationRule } from "./types.ts";
5
11
 
6
12
  function toRules(input: InvalidationInput): readonly InvalidationRule[] {
@@ -113,10 +119,29 @@ export function appendPendingInvalidationHeader(set: Context["set"]): string[] {
113
119
 
114
120
  export function revalidateTag(tags: string | readonly string[]): boolean {
115
121
  const tagList = typeof tags === "string" ? [tags] : [...tags];
122
+ // Tags are cross-app by design: with several mounted furin instances a
123
+ // shared mutation must be able to invalidate pages rendered by any of them.
124
+ // But each instance's tag-registered paths are evicted from THAT instance's
125
+ // caches only — the cross-app fan-out of `revalidatePath` would also evict
126
+ // a sibling app's unrelated page that merely shares the pathname.
116
127
  let deleted = false;
117
- for (const path of autoInvalidateRegistry.pathsForTags(tagList)) {
118
- deleted = revalidatePath(path, "page") || deleted;
128
+ const purgedPaths = new Set<string>();
129
+ for (const instance of allInstances()) {
130
+ // Registry paths are LOGICAL (unprefixed); the CDN caches the PHYSICAL
131
+ // request URL, so prefix each with the instance's mount prefix before
132
+ // queueing it for purge — otherwise a mounted app's `/admin/x` stays stale.
133
+ for (const path of getAutoInvalidateRegistry(instance).pathsForTags(tagList)) {
134
+ const result = revalidatePathForInstance(instance, path, "page");
135
+ deleted = result.deleted || deleted;
136
+ purgedPaths.add(`${instance.prefix}${path}`);
137
+ for (const purged of result.purgedPaths) {
138
+ purgedPaths.add(`${instance.prefix}${purged}`);
139
+ }
140
+ }
119
141
  }
142
+ // One batched CDN purge — the CDN sits in front of every mounted app, so
143
+ // physical paths from all instances go out together, deduped.
144
+ callCachePurger([...purgedPaths]);
120
145
  return deleted;
121
146
  }
122
147
 
@@ -1,5 +1,12 @@
1
1
  import { statSync } from "node:fs";
2
2
  import { autoInvalidateRegistry } from "../auto-invalidate/registry";
3
+ import {
4
+ allStateBuckets,
5
+ currentInstance,
6
+ type FurinInstance,
7
+ instanceSlot,
8
+ withInstance,
9
+ } from "../instance.ts";
3
10
  import { registerCacheInvalidator } from "./registry";
4
11
  import { createRouteCache, type RevalidateType } from "./route-cache";
5
12
 
@@ -18,42 +25,54 @@ export interface InvalidateOutcome {
18
25
  ssg: number;
19
26
  }
20
27
 
21
- const sourceFileToCacheKeys = new Map<string, Set<string>>();
22
-
23
28
  interface CacheKindHandle {
24
29
  cache: ReturnType<typeof createDevLoaderCache>;
25
30
  kind: "isr" | "ssg";
26
31
  }
27
32
 
28
- function indexEntryDependencies(cacheKey: string, deps: string[]): void {
33
+ interface DevLoaderState {
34
+ isr: CacheKindHandle;
35
+ sourceFileToCacheKeys: Map<string, Set<string>>;
36
+ ssg: CacheKindHandle;
37
+ }
38
+
39
+ function indexEntryDependencies(
40
+ index: Map<string, Set<string>>,
41
+ cacheKey: string,
42
+ deps: string[]
43
+ ): void {
29
44
  for (const dep of deps) {
30
- let bucket = sourceFileToCacheKeys.get(dep);
45
+ let bucket = index.get(dep);
31
46
  if (!bucket) {
32
47
  bucket = new Set<string>();
33
- sourceFileToCacheKeys.set(dep, bucket);
48
+ index.set(dep, bucket);
34
49
  }
35
50
  bucket.add(cacheKey);
36
51
  }
37
52
  }
38
53
 
39
- function unindexEntryDependencies(cacheKey: string, deps: string[]): void {
54
+ function unindexEntryDependencies(
55
+ index: Map<string, Set<string>>,
56
+ cacheKey: string,
57
+ deps: string[]
58
+ ): void {
40
59
  for (const dep of deps) {
41
- const bucket = sourceFileToCacheKeys.get(dep);
60
+ const bucket = index.get(dep);
42
61
  if (!bucket) {
43
62
  continue;
44
63
  }
45
64
  bucket.delete(cacheKey);
46
65
  if (bucket.size === 0) {
47
- sourceFileToCacheKeys.delete(dep);
66
+ index.delete(dep);
48
67
  }
49
68
  }
50
69
  }
51
70
 
52
- function createDevLoaderCache(name: string) {
71
+ function createDevLoaderCache(name: string, index: Map<string, Set<string>>) {
53
72
  return createRouteCache<DevLoaderCacheEntry>({
54
73
  name,
55
74
  onDelete: (key, entry) => {
56
- unindexEntryDependencies(key, entry.dependencies);
75
+ unindexEntryDependencies(index, key, entry.dependencies);
57
76
  const urlPath = urlPathFromCacheKey(key);
58
77
  if (urlPath) {
59
78
  autoInvalidateRegistry.unregisterPath(urlPath);
@@ -61,38 +80,43 @@ function createDevLoaderCache(name: string) {
61
80
  },
62
81
  onSet: (key, entry, previous) => {
63
82
  if (previous) {
64
- unindexEntryDependencies(key, previous.dependencies);
83
+ unindexEntryDependencies(index, key, previous.dependencies);
65
84
  }
66
- indexEntryDependencies(key, entry.dependencies);
85
+ indexEntryDependencies(index, key, entry.dependencies);
67
86
  },
68
87
  pathFromKey: urlPathFromCacheKey,
69
88
  });
70
89
  }
71
90
 
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" };
77
-
78
- function setEntry(handle: CacheKindHandle, key: string, entry: DevLoaderCacheEntry): void {
79
- handle.cache.set(key, entry);
80
- }
91
+ // Per-instance dev loader caches + source-dependency index — two furin
92
+ // instances in one dev process must not invalidate each other's loader data.
93
+ const instanceDevLoaderState = instanceSlot((instance): DevLoaderState => {
94
+ const sourceFileToCacheKeys = new Map<string, Set<string>>();
95
+ const isrCache = createDevLoaderCache("render:dev-isr-loader", sourceFileToCacheKeys);
96
+ const ssgCache = createDevLoaderCache("render:dev-ssg-loader", sourceFileToCacheKeys);
97
+ registerCacheInvalidator(isrCache, instance);
98
+ registerCacheInvalidator(ssgCache, instance);
99
+ return {
100
+ isr: { cache: isrCache, kind: "isr" },
101
+ sourceFileToCacheKeys,
102
+ ssg: { cache: ssgCache, kind: "ssg" },
103
+ };
104
+ });
81
105
 
82
106
  export function getDevISRLoaderCache(key: string): DevLoaderCacheEntry | undefined {
83
- return devISRLoaderCache.get(key);
107
+ return instanceDevLoaderState().isr.cache.get(key);
84
108
  }
85
109
 
86
110
  export function setDevISRLoaderCache(key: string, entry: DevLoaderCacheEntry): void {
87
- setEntry(isrHandle, key, entry);
111
+ instanceDevLoaderState().isr.cache.set(key, entry);
88
112
  }
89
113
 
90
114
  export function getDevSSGLoaderCache(key: string): DevLoaderCacheEntry | undefined {
91
- return devSSGLoaderCache.get(key);
115
+ return instanceDevLoaderState().ssg.cache.get(key);
92
116
  }
93
117
 
94
118
  export function setDevSSGLoaderCache(key: string, entry: DevLoaderCacheEntry): void {
95
- setEntry(ssgHandle, key, entry);
119
+ instanceDevLoaderState().ssg.cache.set(key, entry);
96
120
  }
97
121
 
98
122
  export function urlPathFromCacheKey(key: string): string | null {
@@ -107,6 +131,7 @@ export function invalidateDevLoaderCacheByPath(
107
131
  path: string,
108
132
  type: RevalidateType
109
133
  ): InvalidateOutcome {
134
+ const state = instanceDevLoaderState();
110
135
  const cleared: string[] = [];
111
136
  let isr = 0;
112
137
  let ssg = 0;
@@ -119,7 +144,7 @@ export function invalidateDevLoaderCacheByPath(
119
144
  return urlPath === path || urlPath.startsWith(prefix);
120
145
  };
121
146
 
122
- for (const handle of [isrHandle, ssgHandle]) {
147
+ for (const handle of [state.isr, state.ssg]) {
123
148
  for (const key of [...handle.cache.keys()]) {
124
149
  const urlPath = urlPathFromCacheKey(key);
125
150
  if (urlPath === null || !matches(urlPath)) {
@@ -143,7 +168,8 @@ export function invalidateDevLoaderCacheByPath(
143
168
  }
144
169
 
145
170
  export function invalidateDevLoaderCacheBySource(filePath: string): InvalidateOutcome {
146
- const keys = sourceFileToCacheKeys.get(filePath);
171
+ const state = instanceDevLoaderState();
172
+ const keys = state.sourceFileToCacheKeys.get(filePath);
147
173
  if (!keys || keys.size === 0) {
148
174
  return { cleared: [], isr: 0, ssg: 0 };
149
175
  }
@@ -154,16 +180,16 @@ export function invalidateDevLoaderCacheBySource(filePath: string): InvalidateOu
154
180
 
155
181
  const snapshot = [...keys];
156
182
  for (const key of snapshot) {
157
- const isrEntry = devISRLoaderCache.get(key);
183
+ const isrEntry = state.isr.cache.get(key);
158
184
  if (isrEntry) {
159
- devISRLoaderCache.delete(key);
185
+ state.isr.cache.delete(key);
160
186
  cleared.push(key);
161
187
  isr++;
162
188
  continue;
163
189
  }
164
- const ssgEntry = devSSGLoaderCache.get(key);
190
+ const ssgEntry = state.ssg.cache.get(key);
165
191
  if (ssgEntry) {
166
- devSSGLoaderCache.delete(key);
192
+ state.ssg.cache.delete(key);
167
193
  cleared.push(key);
168
194
  ssg++;
169
195
  }
@@ -195,17 +221,30 @@ export function isDevLoaderCacheValid(entry: DevLoaderCacheEntry): boolean {
195
221
  }
196
222
 
197
223
  export function getAllDevISRLoaderEntries(): [string, DevLoaderCacheEntry][] {
198
- return [...devISRLoaderCache.entries()];
224
+ return [...instanceDevLoaderState().isr.cache.entries()];
199
225
  }
200
226
 
201
227
  export function getAllDevSSGLoaderEntries(): [string, DevLoaderCacheEntry][] {
202
- return [...devSSGLoaderCache.entries()];
228
+ return [...instanceDevLoaderState().ssg.cache.entries()];
229
+ }
230
+
231
+ /** @internal — clears dev loader caches for `instance` (default: current). */
232
+ export function clearDevLoaderCaches(instance?: FurinInstance): void {
233
+ const target = instance ?? currentInstance();
234
+ const state = instanceDevLoaderState(target);
235
+ // `.clear()` fires onDelete per entry, which unregisters the path from the
236
+ // auto-invalidate registry via the `currentInstance()`-scoped facade — bind
237
+ // the scope here so the unregistration hits the cleared instance's registry
238
+ // (re-entering an already-active identical scope is harmless).
239
+ withInstance(target, () => {
240
+ state.isr.cache.clear();
241
+ state.ssg.cache.clear();
242
+ });
243
+ state.sourceFileToCacheKeys.clear();
203
244
  }
204
245
 
205
246
  export function __resetDevLoaderCacheState(): void {
206
- devISRLoaderCache.clear();
207
- devSSGLoaderCache.clear();
208
- sourceFileToCacheKeys.clear();
209
- registerCacheInvalidator(devISRLoaderCache);
210
- registerCacheInvalidator(devSSGLoaderCache);
247
+ for (const instance of allStateBuckets()) {
248
+ clearDevLoaderCaches(instance);
249
+ }
211
250
  }
@@ -1,40 +1,43 @@
1
- import { AsyncLocalStorage } from "node:async_hooks";
2
1
  import { createLogger } from "../context-logger";
3
- import { devISRLoaderCache, devSSGLoaderCache } from "./dev-loader";
2
+ import {
3
+ __clearInstanceRegistry,
4
+ allInstances,
5
+ allStateBuckets,
6
+ currentInstance,
7
+ type FurinInstance,
8
+ requestPendingInvalidations,
9
+ runWithInstanceScope,
10
+ withInstance,
11
+ } from "../instance.ts";
12
+ import { clearDevLoaderCaches } from "./dev-loader";
4
13
  import { isrRouteCache } from "./isr";
5
- import { clearCacheInvalidators, getCacheInvalidators, registerCacheInvalidator } from "./registry";
14
+ import { getCacheInvalidators } from "./registry";
6
15
  import type { RevalidateType } from "./route-cache";
7
16
  import { ssgRouteCache } from "./ssg";
8
17
 
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
18
  // ── Build ID ─────────────────────────────────────────────────────────────────
16
-
17
- let _buildId = "";
19
+ // Lives on the furin instance — each mounted app reports its own build ID.
18
20
 
19
21
  export function setBuildId(id: string): void {
20
- _buildId = id;
22
+ currentInstance().buildId = id;
21
23
  }
22
24
 
23
25
  export function getBuildId(): string {
24
- return _buildId;
26
+ return currentInstance().buildId;
25
27
  }
26
28
 
27
29
  // ── Pending invalidations (server → client bridge) ───────────────────────────
30
+ // The per-request set lives in the instance request scope (see instance.ts);
31
+ // outside a request scope invalidations accumulate in a process-global set.
28
32
 
29
- const _requestInvalidationScope = new AsyncLocalStorage<Set<string>>();
30
33
  const _globalPendingInvalidations = new Set<string>();
31
34
 
32
35
  function _activeInvalidationSet(): Set<string> {
33
- return _requestInvalidationScope.getStore() ?? _globalPendingInvalidations;
36
+ return requestPendingInvalidations() ?? _globalPendingInvalidations;
34
37
  }
35
38
 
36
39
  export function _runWithRequestInvalidationScope<T>(fn: () => T): T {
37
- return _requestInvalidationScope.run(new Set<string>(), fn);
40
+ return runWithInstanceScope(currentInstance(), fn);
38
41
  }
39
42
 
40
43
  export function consumePendingInvalidations(): string[] {
@@ -48,6 +51,7 @@ export function consumePendingInvalidations(): string[] {
48
51
  }
49
52
 
50
53
  // ── CDN purger hook ───────────────────────────────────────────────────────────
54
+ // Process-global on purpose: the CDN sits in front of every mounted app.
51
55
 
52
56
  type CachePurger = (paths: string[]) => Promise<void>;
53
57
  let _cachePurger: CachePurger | null = null;
@@ -75,13 +79,18 @@ export function callCachePurger(paths: string[]): void {
75
79
 
76
80
  // ── revalidatePath ───────────────────────────────────────────────────────────
77
81
 
82
+ /**
83
+ * Invalidates `path` across EVERY mounted furin instance — paths are logical
84
+ * (unprefixed), and with shared data two apps can legitimately render the same
85
+ * logical path. This mirrors the historical shared-cache behaviour. Each
86
+ * instance's invalidation runs inside its own scope so cache `onDelete` hooks
87
+ * (auto-invalidate unregistration) hit the owning instance's registry.
88
+ */
78
89
  export function revalidatePath(path: string, type: RevalidateType): boolean {
79
- _activeInvalidationSet().add(type === "layout" ? `${path}:layout` : path);
80
-
81
90
  let deleted = false;
82
91
  const purgedPaths: string[] = [];
83
- for (const invalidator of getCacheInvalidators()) {
84
- const result = invalidator.invalidatePath(path, type);
92
+ for (const instance of allInstances()) {
93
+ const result = revalidatePathForInstance(instance, path, type);
85
94
  deleted = result.deleted || deleted;
86
95
  purgedPaths.push(...result.purgedPaths);
87
96
  }
@@ -90,22 +99,54 @@ export function revalidatePath(path: string, type: RevalidateType): boolean {
90
99
  return deleted;
91
100
  }
92
101
 
102
+ /**
103
+ * Invalidates `path` against a SINGLE instance's caches. `revalidateTag` uses
104
+ * this because it knows which instance registered each path — the cross-app
105
+ * fan-out of `revalidatePath` would also evict a sibling app's unrelated page
106
+ * that merely shares the pathname. Does NOT call the CDN purger; callers
107
+ * batch the returned `purgedPaths` themselves.
108
+ */
109
+ export function revalidatePathForInstance(
110
+ instance: FurinInstance,
111
+ path: string,
112
+ type: RevalidateType
113
+ ): { deleted: boolean; purgedPaths: string[] } {
114
+ _activeInvalidationSet().add(type === "layout" ? `${path}:layout` : path);
115
+
116
+ let deleted = false;
117
+ const purgedPaths: string[] = [];
118
+ withInstance(instance, () => {
119
+ for (const invalidator of getCacheInvalidators(instance)) {
120
+ const result = invalidator.invalidatePath(path, type);
121
+ deleted = result.deleted || deleted;
122
+ purgedPaths.push(...result.purgedPaths);
123
+ }
124
+ });
125
+ return { deleted, purgedPaths };
126
+ }
127
+
93
128
  function dedupePaths(paths: string[]): string[] {
94
129
  return [...new Set(paths)];
95
130
  }
96
131
 
97
132
  /** @internal — resets all module state between tests */
98
133
  export function __resetCacheState(): void {
99
- isrRouteCache.clear();
100
- ssgRouteCache.clear();
101
- devISRLoaderCache.clear();
102
- devSSGLoaderCache.clear();
103
- _buildId = "";
134
+ for (const instance of allStateBuckets()) {
135
+ // `.clear()` fires each entry's onDelete hook, and those hooks resolve
136
+ // the auto-invalidate registry via `currentInstance()` — scope to the
137
+ // bucket being cleared so unregistrations hit ITS registry instead of
138
+ // the ambient (default) one.
139
+ withInstance(instance, () => {
140
+ isrRouteCache(instance).clear();
141
+ ssgRouteCache(instance).clear();
142
+ clearDevLoaderCaches(instance);
143
+ });
144
+ instance.buildId = "";
145
+ }
146
+ // Forget mounted instances (fresh furin() calls between tests must not hit
147
+ // prefix collisions) but keep the default bucket — cache slots above are
148
+ // cleared, while process-wide defaults like a beforeAll template survive.
149
+ __clearInstanceRegistry();
104
150
  _globalPendingInvalidations.clear();
105
151
  _cachePurger = null;
106
- clearCacheInvalidators();
107
- registerCacheInvalidator(isrRouteCache);
108
- registerCacheInvalidator(ssgRouteCache);
109
- registerCacheInvalidator(devISRLoaderCache);
110
- registerCacheInvalidator(devSSGLoaderCache);
111
152
  }
@@ -1,17 +1,32 @@
1
+ import { type FurinInstance, instanceSlot } from "../instance.ts";
1
2
  import { createHtmlRouteCache, type ISRCacheEntry } from "./isr-ssg";
3
+ import { registerCacheInvalidator } from "./registry";
4
+ import type { Cache } from "./route-cache";
5
+ import { createStoreView, type StoreView } from "./store-view";
2
6
 
3
- export const isrRouteCache = createHtmlRouteCache<ISRCacheEntry>("isr");
7
+ // Per-instance ISR HTML cache — registered against the owning instance's
8
+ // invalidator map on first access.
9
+ const instanceIsrCache = instanceSlot((instance) => {
10
+ const cache = createHtmlRouteCache<ISRCacheEntry>("isr");
11
+ registerCacheInvalidator(cache, instance);
12
+ return cache;
13
+ });
4
14
 
5
- export const isrCache = isrRouteCache.store;
15
+ export function isrRouteCache(instance?: FurinInstance): Cache<ISRCacheEntry> {
16
+ return instanceIsrCache(instance);
17
+ }
18
+
19
+ /** Raw store view over the current instance's ISR cache. */
20
+ export const isrCache: StoreView<ISRCacheEntry> = createStoreView(() => instanceIsrCache().store);
6
21
 
7
22
  export function getISRCache(key: string): ISRCacheEntry | undefined {
8
- return isrRouteCache.get(key);
23
+ return instanceIsrCache().get(key);
9
24
  }
10
25
 
11
26
  export function deleteISRCache(key: string): boolean {
12
- return isrRouteCache.delete(key);
27
+ return instanceIsrCache().delete(key);
13
28
  }
14
29
 
15
30
  export function setISRCache(key: string, entry: ISRCacheEntry): void {
16
- isrRouteCache.set(key, entry);
31
+ instanceIsrCache().set(key, entry);
17
32
  }
@@ -1,35 +1,32 @@
1
+ import { type FurinInstance, instanceSlot } from "../instance.ts";
1
2
  import type { Cache } from "./route-cache";
2
3
 
3
4
  export type CacheInvalidator = Pick<Cache<unknown>, "invalidatePath" | "name">;
4
5
 
5
- const _cacheInvalidators = new Map<string, CacheInvalidator>();
6
+ // One invalidator map per furin instance — each instance owns its SSG/ISR/dev
7
+ // caches, so `revalidatePath` never reaches into a sibling app's entries by
8
+ // accident (cross-instance invalidation is explicit, see invalidation.ts).
9
+ const instanceInvalidators = instanceSlot(() => new Map<string, CacheInvalidator>());
6
10
 
7
- export function registerCacheInvalidator(invalidator: CacheInvalidator): () => void {
8
- const existing = _cacheInvalidators.get(invalidator.name);
11
+ export function registerCacheInvalidator(
12
+ invalidator: CacheInvalidator,
13
+ instance?: FurinInstance
14
+ ): () => void {
15
+ const invalidators = instanceInvalidators(instance);
16
+ const existing = invalidators.get(invalidator.name);
9
17
  if (existing !== undefined && existing !== invalidator) {
10
18
  throw new Error(
11
- `Cache invalidator "${invalidator.name}" is already registered with a different instance.`
19
+ `Cache invalidator "${invalidator.name}" is already registered for this furin instance with a different invalidator object.`
12
20
  );
13
21
  }
14
- if (existing === invalidator) {
15
- return () => {
16
- if (_cacheInvalidators.get(invalidator.name) === invalidator) {
17
- _cacheInvalidators.delete(invalidator.name);
18
- }
19
- };
20
- }
21
- _cacheInvalidators.set(invalidator.name, invalidator);
22
+ invalidators.set(invalidator.name, invalidator);
22
23
  return () => {
23
- if (_cacheInvalidators.get(invalidator.name) === invalidator) {
24
- _cacheInvalidators.delete(invalidator.name);
24
+ if (invalidators.get(invalidator.name) === invalidator) {
25
+ invalidators.delete(invalidator.name);
25
26
  }
26
27
  };
27
28
  }
28
29
 
29
- export function getCacheInvalidators(): IterableIterator<CacheInvalidator> {
30
- return _cacheInvalidators.values();
31
- }
32
-
33
- export function clearCacheInvalidators(): void {
34
- _cacheInvalidators.clear();
30
+ export function getCacheInvalidators(instance?: FurinInstance): IterableIterator<CacheInvalidator> {
31
+ return instanceInvalidators(instance).values();
35
32
  }
@@ -1,15 +1,30 @@
1
1
  import { autoInvalidateRegistry } from "../auto-invalidate/registry";
2
+ import { type FurinInstance, instanceSlot } from "../instance.ts";
2
3
  import { createHtmlRouteCache, type SsgCacheEntry } from "./isr-ssg";
4
+ import { registerCacheInvalidator } from "./registry";
5
+ import type { Cache } from "./route-cache";
6
+ import { createStoreView, type StoreView } from "./store-view";
3
7
 
4
- export const ssgRouteCache = createHtmlRouteCache<SsgCacheEntry>("ssg");
8
+ // Per-instance SSG HTML cache — registered against the owning instance's
9
+ // invalidator map on first access.
10
+ const instanceSsgCache = instanceSlot((instance) => {
11
+ const cache = createHtmlRouteCache<SsgCacheEntry>("ssg");
12
+ registerCacheInvalidator(cache, instance);
13
+ return cache;
14
+ });
5
15
 
6
- export const ssgCache = ssgRouteCache.store;
16
+ export function ssgRouteCache(instance?: FurinInstance): Cache<SsgCacheEntry> {
17
+ return instanceSsgCache(instance);
18
+ }
19
+
20
+ /** Raw store view over the current instance's SSG cache. */
21
+ export const ssgCache: StoreView<SsgCacheEntry> = createStoreView(() => instanceSsgCache().store);
7
22
 
8
23
  export function getSSGCache(key: string): SsgCacheEntry | undefined {
9
- return ssgRouteCache.get(key);
24
+ return instanceSsgCache().get(key);
10
25
  }
11
26
 
12
27
  export function setSSGCache(key: string, entry: SsgCacheEntry): void {
13
- ssgRouteCache.set(key, entry);
28
+ instanceSsgCache().set(key, entry);
14
29
  autoInvalidateRegistry.registerLoaderTags(key, entry.tags);
15
30
  }
@@ -0,0 +1,30 @@
1
+ // Map-like facade over a lazily-resolved backing store. Keeps the historical
2
+ // `ssgCache` / `isrCache` module exports working now that the real Map lives
3
+ // on the current furin instance (see server/instance.ts).
4
+ export interface StoreView<Entry> {
5
+ clear(): void;
6
+ delete(key: string): boolean;
7
+ entries(): IterableIterator<[string, Entry]>;
8
+ get(key: string): Entry | undefined;
9
+ has(key: string): boolean;
10
+ keys(): IterableIterator<string>;
11
+ set(key: string, entry: Entry): void;
12
+ readonly size: number;
13
+ }
14
+
15
+ export function createStoreView<Entry>(resolve: () => Map<string, Entry>): StoreView<Entry> {
16
+ return {
17
+ clear: () => resolve().clear(),
18
+ delete: (key) => resolve().delete(key),
19
+ entries: () => resolve().entries(),
20
+ get: (key) => resolve().get(key),
21
+ has: (key) => resolve().has(key),
22
+ keys: () => resolve().keys(),
23
+ set: (key, entry) => {
24
+ resolve().set(key, entry);
25
+ },
26
+ get size() {
27
+ return resolve().size;
28
+ },
29
+ };
30
+ }