@teyik0/furin 0.8.0-alpha.0 → 0.9.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 (153) hide show
  1. package/README.md +37 -0
  2. package/dist/adapter/bun/app.d.ts +1 -0
  3. package/dist/adapter/bun.d.ts +3 -1
  4. package/dist/adapter/runtime-build.d.ts +8 -5
  5. package/dist/adapter/vercel.js +8 -8
  6. package/dist/build/client.d.ts +2 -2
  7. package/dist/build/discover-mounts-process.d.ts +1 -1
  8. package/dist/build/discover-pages-process.d.ts +1 -0
  9. package/dist/build/discover-pages.d.ts +11 -0
  10. package/dist/build/discovery-module.d.ts +3 -0
  11. package/dist/build/discovery-plugins.d.ts +34 -0
  12. package/dist/build/elysia-aot.d.ts +1 -1
  13. package/dist/build/entry-template.ts +6 -1
  14. package/dist/build/hydrate.d.ts +3 -2
  15. package/dist/build/index.js +7688 -8458
  16. package/dist/build/preload-manifest.d.ts +2 -2
  17. package/dist/build/route-metadata.d.ts +31 -0
  18. package/dist/build/route-types.d.ts +1 -1
  19. package/dist/build/shared.d.ts +2 -2
  20. package/dist/build/types.d.ts +11 -0
  21. package/dist/cli/dev-worker.d.ts +1 -0
  22. package/dist/cli/dev-worker.ts +129 -0
  23. package/dist/cli/dev.d.ts +1 -0
  24. package/dist/cli/index.js +7814 -8473
  25. package/dist/client/browser-events-runtime.d.ts +2 -2
  26. package/dist/client/browser-events.d.ts +2 -0
  27. package/dist/client/chunks/{client-brdfd773.js → client-rjhxgt64.js} +187 -4
  28. package/dist/client/dev-error-overlay.ts +18 -1
  29. package/dist/client/link.js +127 -170
  30. package/dist/client/query-store.d.ts +5 -0
  31. package/dist/client/router/navigation-controller.d.ts +2 -0
  32. package/dist/client/router/use-fetch-interception.d.ts +1 -1
  33. package/dist/client/router/use-sync-stream.d.ts +1 -1
  34. package/dist/client/{router/sync-catch-up.d.ts → sync-catch-up.d.ts} +5 -3
  35. package/dist/client/sync-client.d.ts +74 -0
  36. package/dist/client.d.ts +1 -0
  37. package/dist/client.js +533 -145
  38. package/dist/config.d.ts +6 -1
  39. package/dist/config.js +3 -1
  40. package/dist/define-route.d.ts +3 -3
  41. package/dist/furin.d.ts +2 -1
  42. package/dist/furin.js +1168 -393
  43. package/dist/plugin/index.js +5 -5
  44. package/dist/plugin/route-registry.d.ts +22 -2
  45. package/dist/rsc/build/index.d.ts +4 -3
  46. package/dist/server/dev/server-only-module.d.ts +5 -0
  47. package/dist/server/internal.js +241 -129
  48. package/dist/server/logging/ingest-plugin.d.ts +1 -2
  49. package/dist/server/production-server.js +36 -33
  50. package/dist/server/render/index.d.ts +0 -1
  51. package/dist/server/render/index.ts +0 -1
  52. package/dist/server/render/shell.d.ts +1 -13
  53. package/dist/server/render/shell.ts +1 -90
  54. package/dist/server/server-entry.d.ts +5 -0
  55. package/dist/server/server-entry.js +36 -0
  56. package/dist/server/static-export.js +241 -129
  57. package/dist/server/sync/adapter.d.ts +4 -3
  58. package/dist/server/sync/config.d.ts +7 -0
  59. package/dist/server/sync/connection.d.ts +13 -0
  60. package/dist/server/sync/drizzle/index.js +5 -4
  61. package/dist/server/sync/index.d.ts +3 -0
  62. package/dist/server/sync/index.js +1361 -243
  63. package/dist/server/sync/invalidate-sync.d.ts +6 -2
  64. package/dist/server/sync/local-client.d.ts +9 -0
  65. package/dist/server/sync/postgres/index.d.ts +1 -1
  66. package/dist/server/sync/postgres/index.js +2 -2
  67. package/dist/server/sync/prisma/index.js +2 -2
  68. package/dist/server/sync/redis/index.d.ts +1 -1
  69. package/dist/server/sync/redis/index.js +4 -3
  70. package/dist/server/sync/redis/scripts.d.ts +1 -1
  71. package/dist/server/sync/runtime.d.ts +3 -0
  72. package/dist/server/sync/sqlite/index.d.ts +1 -1
  73. package/dist/server/sync/sqlite/index.js +3 -2
  74. package/dist/shared/browser-events.d.ts +4 -0
  75. package/dist/shared/not-found.d.ts +1 -1
  76. package/dist/sync-client.d.ts +3 -0
  77. package/dist/sync-client.js +1600 -0
  78. package/package.json +14 -9
  79. package/src/adapter/README.md +7 -0
  80. package/src/adapter/build.ts +34 -33
  81. package/src/adapter/bun/app.ts +5 -0
  82. package/src/adapter/bun/server.ts +2 -2
  83. package/src/adapter/bun.ts +18 -2
  84. package/src/adapter/furin-app.d.ts +1 -0
  85. package/src/adapter/package.ts +1 -0
  86. package/src/adapter/runtime-build.ts +18 -12
  87. package/src/adapter/static.ts +17 -13
  88. package/src/build/client.ts +7 -5
  89. package/src/build/discover-mounts-process.ts +5 -1
  90. package/src/build/discover-pages-process.ts +151 -0
  91. package/src/build/discover-pages.ts +69 -0
  92. package/src/build/discovery-module.ts +70 -0
  93. package/src/build/discovery-plugins.ts +101 -0
  94. package/src/build/elysia-aot-worker.ts +9 -8
  95. package/src/build/elysia-aot.ts +110 -75
  96. package/src/build/entry-template.ts +6 -1
  97. package/src/build/hydrate.ts +11 -8
  98. package/src/build/index.ts +6 -50
  99. package/src/build/preload-manifest.ts +3 -3
  100. package/src/build/route-metadata.ts +90 -0
  101. package/src/build/route-types.ts +1 -1
  102. package/src/build/shared.ts +6 -8
  103. package/src/build/types.ts +9 -0
  104. package/src/cli/config.ts +4 -0
  105. package/src/cli/dev-worker.ts +129 -0
  106. package/src/cli/dev.ts +106 -0
  107. package/src/cli/index.ts +24 -1
  108. package/src/client/browser-events-runtime.ts +97 -23
  109. package/src/client/browser-events.ts +8 -0
  110. package/src/client/dev-error-overlay.ts +18 -1
  111. package/src/client/link.tsx +21 -22
  112. package/src/client/query-store.ts +54 -3
  113. package/src/client/router/context.ts +8 -7
  114. package/src/client/router/navigation-controller.ts +45 -12
  115. package/src/client/router/provider.tsx +11 -2
  116. package/src/client/router/use-fetch-interception.ts +1 -1
  117. package/src/client/router/use-sync-stream.ts +96 -44
  118. package/src/client/{router/sync-catch-up.ts → sync-catch-up.ts} +28 -12
  119. package/src/client/sync-client.ts +528 -0
  120. package/src/client/sync.ts +11 -4
  121. package/src/client.ts +10 -0
  122. package/src/config.ts +17 -10
  123. package/src/furin.ts +5 -0
  124. package/src/plugin/route-registry.ts +53 -7
  125. package/src/rsc/build/index.ts +6 -3
  126. package/src/server/browser-events/plugin.ts +79 -25
  127. package/src/server/dev/rewrite-module-specifiers.ts +2 -1
  128. package/src/server/dev/server-only-module.ts +23 -0
  129. package/src/server/dev/symbolicate.ts +4 -3
  130. package/src/server/dev-page-imports.ts +3 -1
  131. package/src/server/dev-page-plugin.ts +18 -1
  132. package/src/server/logging/ingest-plugin.ts +3 -9
  133. package/src/server/render/index.ts +0 -1
  134. package/src/server/render/shell.ts +1 -90
  135. package/src/server/server-entry.ts +39 -0
  136. package/src/server/sync/adapter.ts +7 -3
  137. package/src/server/sync/config.ts +12 -6
  138. package/src/server/sync/connection.ts +71 -0
  139. package/src/server/sync/index.ts +3 -0
  140. package/src/server/sync/invalidate-sync.ts +30 -5
  141. package/src/server/sync/local-client.ts +84 -0
  142. package/src/server/sync/plugin.ts +27 -20
  143. package/src/server/sync/postgres/index.ts +9 -2
  144. package/src/server/sync/redis/index.ts +5 -1
  145. package/src/server/sync/redis/scripts.ts +1 -1
  146. package/src/server/sync/runtime.ts +56 -18
  147. package/src/server/sync/sqlite/index.ts +6 -2
  148. package/src/server/sync/stream.ts +17 -5
  149. package/src/shared/browser-events.ts +1 -1
  150. package/src/shared/not-found.ts +1 -1
  151. package/src/sync-client.ts +14 -0
  152. package/dist/server/logger.d.ts +0 -10
  153. package/src/server/logger.ts +0 -34
@@ -14,10 +14,12 @@ import { findRouteFailure } from "../../shared/route-matches.ts";
14
14
  import type { SearchParamsInput } from "../../shared/search-params.ts";
15
15
  import type { QuerySeed } from "../../shared/sync-query.ts";
16
16
  import { collectRouteChainFromRoute } from "../../shared/utils/index.ts";
17
+ import { setBrowserSyncAuthentication } from "../browser-events.ts";
17
18
  import { DocumentProvider, useDocumentState } from "../document.tsx";
18
19
  import { QueryStoreContext } from "../query.tsx";
19
20
  import { QueryStore } from "../query-store.ts";
20
21
  import { resolveRouteElement } from "../route-element.tsx";
22
+ import { createInvalidationRefresh } from "../sync-catch-up.ts";
21
23
  import { buildRouterTree } from "./boundary-tree.tsx";
22
24
  import { RouterStoreContext } from "./context.ts";
23
25
  import {
@@ -36,7 +38,6 @@ import { createOptimisticRuntime, registerOptimisticRuntime } from "./optimistic
36
38
  import { fetchPageState } from "./page-fetch.ts";
37
39
  import { createSearchStore, SearchStoreContext } from "./search-store.ts";
38
40
  import { detectStaticMode } from "./spa-response.ts";
39
- import { createInvalidationRefresh } from "./sync-catch-up.ts";
40
41
  import type {
41
42
  ClientRoute,
42
43
  ClientSegmentBoundary,
@@ -126,6 +127,7 @@ export function RouterProvider({
126
127
  const syncResponse = useRef<
127
128
  ((response: Response | undefined, optimistic: boolean) => void) | undefined
128
129
  >(undefined);
130
+ // biome-ignore lint/nursery/useReactCompiler: the runtime reads these refs only from its callbacks, after render.
129
131
  const [optimisticRuntime] = useState(() =>
130
132
  createOptimisticRuntime({
131
133
  basePath,
@@ -365,6 +367,8 @@ export function RouterProvider({
365
367
  });
366
368
  useSyncStream({ autoRefresh, basePath, controller, invalidationRefresh, queries, syncPath });
367
369
 
370
+ // The query store and resolved deferred promises are mutable sources outside
371
+ // React; queryRevision and deferredRevision are the keys that invalidate them.
368
372
  const queryData = useMemo(
369
373
  () =>
370
374
  projectDeferredQueryData(
@@ -379,8 +383,10 @@ export function RouterProvider({
379
383
  }
380
384
  return queries.snapshot(seed.url).data;
381
385
  },
386
+ // biome-ignore lint/nursery/useReactCompiler: deferredRevision invalidates this read.
382
387
  resolvedQueries.current
383
388
  ),
389
+ // biome-ignore lint/nursery/useReactCompiler: revisions intentionally key reads of external mutable sources.
384
390
  [queries, state.data, state.querySeeds, queryRevision, deferredRevision]
385
391
  );
386
392
  const renderedData = optimisticRuntime.project(queryData, currentHref);
@@ -417,7 +423,10 @@ export function RouterProvider({
417
423
  invalidatePrefetch,
418
424
  navigate,
419
425
  prefetch,
420
- refresh,
426
+ refresh: (options) => {
427
+ setBrowserSyncAuthentication(true);
428
+ return refresh(options);
429
+ },
421
430
  searchRoutes: routes,
422
431
  }),
423
432
  [
@@ -1,4 +1,5 @@
1
1
  import { useEffect } from "react";
2
+ import type { InvalidationRefresh } from "../sync-catch-up.ts";
2
3
  import { observeWindowFetch } from "./fetch-observers.ts";
3
4
  import {
4
5
  applyRevalidateHeader,
@@ -8,7 +9,6 @@ import {
8
9
  toLogical,
9
10
  } from "./link-utils.ts";
10
11
  import { SYNC_REQUEST } from "./optimistic.ts";
11
- import type { InvalidationRefresh } from "./sync-catch-up.ts";
12
12
 
13
13
  /**
14
14
  * Processes `X-Furin-Revalidate` headers on every same-origin `window.fetch`
@@ -1,8 +1,14 @@
1
1
  import { log } from "evlog";
2
2
  import { useEffect } from "react";
3
+ import type { BrowserEventFor } from "../../shared/browser-events.ts";
3
4
  import { queryTag, queryTags } from "../../shared/sync-query.ts";
4
- import { subscribeBrowserEvent } from "../browser-events.ts";
5
+ import { setBrowserSyncAuthentication, subscribeBrowserEvent } from "../browser-events.ts";
5
6
  import type { QueryStore } from "../query-store.ts";
7
+ import {
8
+ createSyncCatchUp,
9
+ type InvalidationRefresh,
10
+ type SyncChangePagePayload,
11
+ } from "../sync-catch-up.ts";
6
12
  import {
7
13
  applyRevalidateEntries,
8
14
  normalizeHref,
@@ -10,11 +16,6 @@ import {
10
16
  toLogical,
11
17
  } from "./link-utils.ts";
12
18
  import type { NavigationController } from "./navigation-controller.ts";
13
- import {
14
- createSyncCatchUp,
15
- type InvalidationRefresh,
16
- type SyncChangePagePayload,
17
- } from "./sync-catch-up.ts";
18
19
  import type { RouterState } from "./types.ts";
19
20
 
20
21
  /**
@@ -42,6 +43,24 @@ export function useSyncStream(options: {
42
43
  : `${basePath}/${syncPath}`;
43
44
  let unsubscribe: (() => void) | undefined;
44
45
  let disposed = false;
46
+ let requestController = new AbortController();
47
+ let contextChanged = false;
48
+ let session: string | undefined;
49
+ const clearContext = () => {
50
+ contextChanged = true;
51
+ queries.clear();
52
+ controller.invalidateContext();
53
+ };
54
+ const observeSession = (response: Response) => {
55
+ const identity = response.headers.get("x-furin-sync-session");
56
+ if (identity === null) {
57
+ return;
58
+ }
59
+ if (session !== undefined && identity !== session) {
60
+ clearContext();
61
+ }
62
+ session = identity;
63
+ };
45
64
  const applyInvalidations = (entries: readonly string[]) => {
46
65
  if (disposed) {
47
66
  return;
@@ -61,53 +80,84 @@ export function useSyncStream(options: {
61
80
  }
62
81
  }
63
82
  };
64
- const catchUp = createSyncCatchUp({
65
- fetchPage: async (after) => {
66
- const url = new URL(`${changesUrl}/changes`, window.location.origin);
67
- if (after !== undefined) {
68
- url.searchParams.set("after", after);
69
- }
70
- const response = await window.fetch(url);
71
- if (!response.ok) {
72
- throw new Error(`Sync catch-up failed with status ${response.status}`);
73
- }
74
- return (await response.json()) as SyncChangePagePayload;
75
- },
76
- onInvalidations: applyInvalidations,
77
- onReset: () => {
78
- if (!disposed) {
79
- queries.invalidateAll();
80
- }
81
- },
82
- onQueries: (identities) => {
83
- queries.invalidate(identities);
84
- const tags = new Set(identities.map(queryTag));
85
- const affected = (snapshot: RouterState) =>
86
- snapshot.querySeeds?.some((seed) =>
87
- queryTags(seed.identity).some((tag) => tags.has(tag))
88
- ) === true;
89
- controller.invalidateQueries(affected);
90
- if (autoRefresh && affected(controller.getSnapshot().state)) {
91
- invalidationRefresh.run();
92
- }
93
- },
94
- });
83
+ const createRecovery = () =>
84
+ createSyncCatchUp({
85
+ fetchPage: async (after) => {
86
+ const url = new URL(`${changesUrl}/changes`, window.location.origin);
87
+ if (after !== undefined) {
88
+ url.searchParams.set("after", after);
89
+ }
90
+ const { signal } = requestController;
91
+ const response = await window.fetch(url, { signal });
92
+ signal.throwIfAborted();
93
+ if (response.status === 401 || response.status === 403) {
94
+ clearContext();
95
+ setBrowserSyncAuthentication(false);
96
+ }
97
+ if (!response.ok) {
98
+ throw new Error(`Sync catch-up failed with status ${response.status}`);
99
+ }
100
+ const page = (await response.json()) as SyncChangePagePayload;
101
+ signal.throwIfAborted();
102
+ observeSession(response);
103
+ return page;
104
+ },
105
+ onInvalidations: applyInvalidations,
106
+ onReset: () => {
107
+ if (!disposed) {
108
+ queries.invalidateAll();
109
+ }
110
+ },
111
+ onQueries: (identities) => {
112
+ queries.invalidate(identities);
113
+ const tags = new Set(identities.map(queryTag));
114
+ const affected = (snapshot: RouterState) =>
115
+ snapshot.querySeeds?.some((seed) =>
116
+ queryTags(seed.identity).some((tag) => tags.has(tag))
117
+ ) === true;
118
+ controller.invalidateQueries(affected);
119
+ if (autoRefresh && affected(controller.getSnapshot().state)) {
120
+ invalidationRefresh.run();
121
+ }
122
+ },
123
+ });
124
+ let catchUp = createRecovery();
95
125
  const recover = () => {
96
126
  if (disposed) {
97
127
  return;
98
128
  }
99
- catchUp.catchUp().catch((error: unknown) => {
100
- if (!disposed) {
101
- log.warn({ action: "sync_catch_up_failed", error: String(error) });
102
- }
103
- });
129
+ const current = catchUp;
130
+ current
131
+ .catchUp()
132
+ .then(() => {
133
+ if (!disposed && catchUp === current && contextChanged) {
134
+ contextChanged = false;
135
+ queries.invalidateAll();
136
+ applyInvalidations(["/:layout"]);
137
+ }
138
+ })
139
+ .catch((error: unknown) => {
140
+ if (!disposed) {
141
+ log.warn({ action: "sync_catch_up_failed", error: String(error) });
142
+ }
143
+ });
104
144
  };
105
- const onSync = (event: { data: { cursor: string } }) => {
145
+ const onSync = (event: BrowserEventFor<"sync">) => {
146
+ if (event.data.context === "changed") {
147
+ requestController.abort();
148
+ requestController = new AbortController();
149
+ catchUp.close();
150
+ catchUp = createRecovery();
151
+ clearContext();
152
+ recover();
153
+ return;
154
+ }
106
155
  if (typeof event.data.cursor !== "string") {
107
156
  log.warn({ action: "sync_invalid_event", event: "browser-events.sync" });
108
157
  return;
109
158
  }
110
- catchUp.seed(event.data.cursor);
159
+ // Notifications do not prove the rendered snapshot includes earlier writes.
160
+ // Recover from zero first; only successful HTTP recovery advances the cursor.
111
161
  recover();
112
162
  };
113
163
  const connect = () => {
@@ -123,6 +173,8 @@ export function useSyncStream(options: {
123
173
  connect();
124
174
  return () => {
125
175
  disposed = true;
176
+ requestController.abort();
177
+ catchUp.close();
126
178
  unsubscribe?.();
127
179
  };
128
180
  }, [syncPath, basePath, invalidationRefresh, autoRefresh, queries, controller]);
@@ -1,12 +1,12 @@
1
1
  // biome-ignore-all lint/performance/noAwaitInLoops: sync catch-up pages must be fetched sequentially by cursor
2
- import type { SyncInvalidation } from "../../server/sync/adapter.ts";
3
- import { encodeInvalidationEntry } from "../../shared/invalidation-header.ts";
4
- import { type QueryIdentity, queryFromTag } from "../../shared/sync-query.ts";
2
+ import type { SyncInvalidation } from "../server/sync/adapter.ts";
3
+ import { encodeInvalidationEntry } from "../shared/invalidation-header.ts";
4
+ import { type QueryIdentity, queryFromTag } from "../shared/sync-query.ts";
5
5
 
6
6
  interface SyncChangePayload {
7
7
  cursor: string;
8
8
  invalidations: readonly (string | SyncInvalidation)[];
9
- queries?: readonly import("../../shared/sync-query.ts").QueryIdentity[];
9
+ queries?: readonly QueryIdentity[];
10
10
  }
11
11
 
12
12
  export interface SyncChangePagePayload {
@@ -19,7 +19,7 @@ export interface SyncChangePagePayload {
19
19
  interface SyncCatchUpOptions {
20
20
  fetchPage: (after: string | undefined) => Promise<SyncChangePagePayload>;
21
21
  onInvalidations: (invalidations: readonly string[]) => void;
22
- onQueries?: (identities: readonly import("../../shared/sync-query.ts").QueryIdentity[]) => void;
22
+ onQueries?: (identities: readonly QueryIdentity[]) => void;
23
23
  onReset?: () => void;
24
24
  }
25
25
 
@@ -57,6 +57,7 @@ function changeInvalidations(change: SyncChangePayload): {
57
57
 
58
58
  export interface SyncCatchUp {
59
59
  catchUp: () => Promise<void>;
60
+ close: () => void;
60
61
  cursor: () => string | undefined;
61
62
  seed: (cursor: string) => void;
62
63
  }
@@ -114,6 +115,7 @@ export function createSyncCatchUp(options: SyncCatchUpOptions): SyncCatchUp {
114
115
  let requested = false;
115
116
  let lastReset = Number.NEGATIVE_INFINITY;
116
117
  let pendingReset: ReturnType<typeof setTimeout> | undefined;
118
+ let closed = false;
117
119
 
118
120
  const refreshAfterReset = () => {
119
121
  pendingReset = undefined;
@@ -141,25 +143,35 @@ export function createSyncCatchUp(options: SyncCatchUpOptions): SyncCatchUp {
141
143
  let page: SyncChangePagePayload;
142
144
  do {
143
145
  page = await options.fetchPage(currentCursor ?? "0");
146
+ if (closed) {
147
+ return;
148
+ }
144
149
  if (page.reset) {
145
150
  currentCursor = page.cursor;
146
151
  scheduleResetRefresh();
147
152
  break;
148
153
  }
149
154
  for (const change of page.changes) {
150
- const { paths, queries } = changeInvalidations(change);
151
- options.onInvalidations(paths);
152
- if (queries.length > 0) {
153
- options.onQueries?.(queries);
154
- }
155
+ applyChange(change);
155
156
  }
156
157
  currentCursor = page.cursor;
157
158
  } while (page.hasMore);
158
- } while (requested);
159
+ } while (requested && !closed);
160
+ };
161
+
162
+ const applyChange = (change: SyncChangePayload) => {
163
+ const { paths, queries } = changeInvalidations(change);
164
+ options.onInvalidations(paths);
165
+ if (queries.length > 0) {
166
+ options.onQueries?.(queries);
167
+ }
159
168
  };
160
169
 
161
170
  return {
162
171
  catchUp() {
172
+ if (closed) {
173
+ return Promise.resolve();
174
+ }
163
175
  requested = true;
164
176
  if (!running) {
165
177
  running = readUntilCurrent().finally(() => {
@@ -169,10 +181,14 @@ export function createSyncCatchUp(options: SyncCatchUpOptions): SyncCatchUp {
169
181
  return running;
170
182
  },
171
183
  cursor: () => currentCursor,
184
+ close() {
185
+ closed = true;
186
+ clearTimeout(pendingReset);
187
+ },
172
188
  seed(cursor) {
173
189
  currentCursor ??= cursor;
174
190
  },
175
191
  };
176
192
  }
177
193
 
178
- import { isAbortError } from "./abort.ts";
194
+ import { isAbortError } from "./router/abort.ts";