@teyik0/furin 0.2.0-alpha.3 → 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 (98) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +37 -33
  3. package/dist/adapter/bun.d.ts +17 -1
  4. package/dist/adapter/package.d.ts +26 -0
  5. package/dist/build/client.d.ts +1 -1
  6. package/dist/build/compile-entry.d.ts +3 -3
  7. package/dist/build/compile-entry.ts +114 -109
  8. package/dist/build/entry-template.d.ts +35 -9
  9. package/dist/build/entry-template.ts +206 -156
  10. package/dist/build/hydrate.d.ts +1 -1
  11. package/dist/build/index.js +643 -247
  12. package/dist/build/scan-server.d.ts +10 -4
  13. package/dist/build/server-routes-entry.d.ts +3 -3
  14. package/dist/build/server-routes-entry.ts +30 -36
  15. package/dist/build/ssg-cache.d.ts +1 -1
  16. package/dist/build/types.d.ts +36 -1
  17. package/dist/cli/config.d.ts +6 -1
  18. package/dist/cli/index.js +662 -260
  19. package/dist/client/link.js +163 -16
  20. package/dist/client/router/abort.d.ts +1 -0
  21. package/dist/client/router/sync-catch-up.d.ts +29 -0
  22. package/dist/config.d.ts +11 -2
  23. package/dist/config.js +5 -1
  24. package/dist/furin.d.ts +37 -19
  25. package/dist/furin.js +982 -378
  26. package/dist/server/auto-invalidate/registry.d.ts +8 -1
  27. package/dist/server/cache/dev-loader.d.ts +3 -2
  28. package/dist/server/cache/invalidation.d.ts +19 -0
  29. package/dist/server/cache/isr.d.ts +6 -2
  30. package/dist/server/cache/registry.d.ts +3 -3
  31. package/dist/server/cache/ssg.d.ts +6 -2
  32. package/dist/server/cache/store-view.d.ts +11 -0
  33. package/dist/server/instance.d.ts +86 -0
  34. package/dist/server/internal.d.ts +16 -1
  35. package/dist/server/internal.js +72 -5
  36. package/dist/server/render/template.d.ts +3 -2
  37. package/dist/server/sync/adapter.d.ts +62 -0
  38. package/dist/server/sync/config.d.ts +11 -0
  39. package/dist/server/sync/fingerprint.d.ts +6 -0
  40. package/dist/server/sync/index.d.ts +2 -2
  41. package/dist/server/sync/memory-adapter.d.ts +17 -0
  42. package/dist/server/sync/plugin.d.ts +12 -195
  43. package/dist/server/sync/response.d.ts +4 -0
  44. package/dist/server/sync/stream.d.ts +24 -5
  45. package/dist/shared/prefix.d.ts +15 -0
  46. package/package.json +6 -1
  47. package/src/adapter/README.md +13 -13
  48. package/src/adapter/bun.ts +113 -50
  49. package/src/adapter/package.ts +206 -0
  50. package/src/build/client.ts +158 -152
  51. package/src/build/compile-entry.ts +114 -109
  52. package/src/build/entry-template.ts +206 -156
  53. package/src/build/hydrate.ts +342 -340
  54. package/src/build/index.ts +223 -152
  55. package/src/build/route-types.ts +145 -145
  56. package/src/build/scan-server.ts +111 -95
  57. package/src/build/server-routes-entry.ts +30 -36
  58. package/src/build/shared.ts +80 -80
  59. package/src/build/ssg-cache.ts +36 -32
  60. package/src/build/types.ts +125 -89
  61. package/src/cli/config.ts +9 -3
  62. package/src/cli/index.ts +21 -3
  63. package/src/client/router/abort.ts +5 -0
  64. package/src/client/router/link-utils.ts +184 -184
  65. package/src/client/router/provider.tsx +883 -804
  66. package/src/client/router/sync-catch-up.ts +113 -0
  67. package/src/client/router/types.ts +276 -276
  68. package/src/client/sync.ts +98 -98
  69. package/src/client.ts +369 -369
  70. package/src/config.ts +14 -1
  71. package/src/furin.ts +619 -465
  72. package/src/server/auto-invalidate/registry.ts +23 -1
  73. package/src/server/auto-invalidate/runtime.ts +159 -134
  74. package/src/server/cache/dev-loader.ts +77 -38
  75. package/src/server/cache/invalidation.ts +71 -30
  76. package/src/server/cache/isr.ts +20 -5
  77. package/src/server/cache/registry.ts +17 -20
  78. package/src/server/cache/ssg.ts +19 -4
  79. package/src/server/cache/store-view.ts +30 -0
  80. package/src/server/dev-page-plugin.ts +454 -454
  81. package/src/server/instance.ts +252 -0
  82. package/src/server/internal.ts +100 -5
  83. package/src/server/render/assemble.ts +160 -160
  84. package/src/server/render/isr.ts +323 -323
  85. package/src/server/render/not-found.ts +9 -3
  86. package/src/server/render/ssr.ts +603 -585
  87. package/src/server/render/template.ts +57 -24
  88. package/src/server/router/plugin.ts +384 -384
  89. package/src/server/sync/adapter.ts +65 -0
  90. package/src/server/sync/config.ts +60 -29
  91. package/src/server/sync/fingerprint.ts +44 -0
  92. package/src/server/sync/index.ts +15 -14
  93. package/src/server/sync/memory-adapter.ts +196 -0
  94. package/src/server/sync/plugin.ts +199 -108
  95. package/src/server/sync/response.ts +77 -0
  96. package/src/server/sync/stream.ts +162 -144
  97. package/src/shared/deferred-ndjson.ts +7 -0
  98. package/src/shared/prefix.ts +38 -0
@@ -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
+ }