@teyik0/furin 0.8.0-alpha.0 → 0.9.0-alpha.0

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 (143) 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 +123 -0
  23. package/dist/cli/dev.d.ts +1 -0
  24. package/dist/cli/index.js +7811 -8477
  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 +110 -156
  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 +1119 -346
  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 +201 -91
  48. package/dist/server/logging/ingest-plugin.d.ts +1 -2
  49. package/dist/server/production-server.js +36 -33
  50. package/dist/server/server-entry.d.ts +5 -0
  51. package/dist/server/server-entry.js +36 -0
  52. package/dist/server/static-export.js +201 -91
  53. package/dist/server/sync/adapter.d.ts +4 -3
  54. package/dist/server/sync/config.d.ts +7 -0
  55. package/dist/server/sync/connection.d.ts +13 -0
  56. package/dist/server/sync/drizzle/index.js +5 -4
  57. package/dist/server/sync/index.d.ts +3 -0
  58. package/dist/server/sync/index.js +1361 -243
  59. package/dist/server/sync/invalidate-sync.d.ts +6 -2
  60. package/dist/server/sync/local-client.d.ts +9 -0
  61. package/dist/server/sync/postgres/index.d.ts +1 -1
  62. package/dist/server/sync/postgres/index.js +2 -2
  63. package/dist/server/sync/prisma/index.js +2 -2
  64. package/dist/server/sync/redis/index.d.ts +1 -1
  65. package/dist/server/sync/redis/index.js +4 -3
  66. package/dist/server/sync/redis/scripts.d.ts +1 -1
  67. package/dist/server/sync/runtime.d.ts +3 -0
  68. package/dist/server/sync/sqlite/index.d.ts +1 -1
  69. package/dist/server/sync/sqlite/index.js +3 -2
  70. package/dist/shared/browser-events.d.ts +4 -0
  71. package/dist/sync-client.d.ts +3 -0
  72. package/dist/sync-client.js +1600 -0
  73. package/package.json +12 -7
  74. package/src/adapter/README.md +7 -0
  75. package/src/adapter/build.ts +34 -33
  76. package/src/adapter/bun/app.ts +5 -0
  77. package/src/adapter/bun/server.ts +2 -2
  78. package/src/adapter/bun.ts +18 -2
  79. package/src/adapter/furin-app.d.ts +1 -0
  80. package/src/adapter/package.ts +1 -0
  81. package/src/adapter/runtime-build.ts +18 -12
  82. package/src/adapter/static.ts +17 -13
  83. package/src/build/client.ts +7 -5
  84. package/src/build/discover-mounts-process.ts +5 -1
  85. package/src/build/discover-pages-process.ts +151 -0
  86. package/src/build/discover-pages.ts +69 -0
  87. package/src/build/discovery-module.ts +70 -0
  88. package/src/build/discovery-plugins.ts +101 -0
  89. package/src/build/elysia-aot-worker.ts +9 -8
  90. package/src/build/elysia-aot.ts +110 -75
  91. package/src/build/entry-template.ts +6 -1
  92. package/src/build/hydrate.ts +11 -8
  93. package/src/build/index.ts +6 -50
  94. package/src/build/preload-manifest.ts +3 -3
  95. package/src/build/route-metadata.ts +90 -0
  96. package/src/build/route-types.ts +1 -1
  97. package/src/build/shared.ts +6 -8
  98. package/src/build/types.ts +9 -0
  99. package/src/cli/config.ts +4 -0
  100. package/src/cli/dev-worker.ts +123 -0
  101. package/src/cli/dev.ts +91 -0
  102. package/src/cli/index.ts +24 -1
  103. package/src/client/browser-events-runtime.ts +97 -23
  104. package/src/client/browser-events.ts +8 -0
  105. package/src/client/dev-error-overlay.ts +18 -1
  106. package/src/client/query-store.ts +54 -3
  107. package/src/client/router/navigation-controller.ts +45 -12
  108. package/src/client/router/provider.tsx +6 -2
  109. package/src/client/router/use-fetch-interception.ts +1 -1
  110. package/src/client/router/use-sync-stream.ts +96 -44
  111. package/src/client/{router/sync-catch-up.ts → sync-catch-up.ts} +28 -12
  112. package/src/client/sync-client.ts +528 -0
  113. package/src/client/sync.ts +11 -4
  114. package/src/client.ts +10 -0
  115. package/src/config.ts +17 -10
  116. package/src/furin.ts +5 -0
  117. package/src/plugin/route-registry.ts +53 -7
  118. package/src/rsc/build/index.ts +6 -3
  119. package/src/server/browser-events/plugin.ts +79 -25
  120. package/src/server/dev/rewrite-module-specifiers.ts +2 -1
  121. package/src/server/dev/server-only-module.ts +23 -0
  122. package/src/server/dev/symbolicate.ts +4 -3
  123. package/src/server/dev-page-imports.ts +3 -1
  124. package/src/server/dev-page-plugin.ts +18 -1
  125. package/src/server/logging/ingest-plugin.ts +3 -9
  126. package/src/server/server-entry.ts +39 -0
  127. package/src/server/sync/adapter.ts +7 -3
  128. package/src/server/sync/config.ts +12 -6
  129. package/src/server/sync/connection.ts +71 -0
  130. package/src/server/sync/index.ts +3 -0
  131. package/src/server/sync/invalidate-sync.ts +30 -5
  132. package/src/server/sync/local-client.ts +84 -0
  133. package/src/server/sync/plugin.ts +27 -20
  134. package/src/server/sync/postgres/index.ts +9 -2
  135. package/src/server/sync/redis/index.ts +5 -1
  136. package/src/server/sync/redis/scripts.ts +1 -1
  137. package/src/server/sync/runtime.ts +56 -18
  138. package/src/server/sync/sqlite/index.ts +6 -2
  139. package/src/server/sync/stream.ts +17 -5
  140. package/src/shared/browser-events.ts +1 -1
  141. package/src/sync-client.ts +14 -0
  142. package/dist/server/logger.d.ts +0 -10
  143. package/src/server/logger.ts +0 -34
@@ -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";