@teyik0/furin 0.2.0-alpha.5 → 0.4.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 (282) hide show
  1. package/README.md +2 -3
  2. package/dist/adapter/bun.d.ts +1 -1
  3. package/dist/adapter/static.d.ts +1 -1
  4. package/dist/build/bun-build.d.ts +3 -0
  5. package/dist/build/client.d.ts +2 -2
  6. package/dist/build/entry-template.d.ts +6 -0
  7. package/dist/build/entry-template.ts +20 -3
  8. package/dist/build/hydrate.d.ts +1 -1
  9. package/dist/build/index.d.ts +1 -1
  10. package/dist/build/index.js +9363 -5827
  11. package/dist/build/production-instrumentation.d.ts +1 -0
  12. package/dist/build/route-types.d.ts +3 -23
  13. package/dist/build/shared.d.ts +1 -1
  14. package/dist/build/ssg-cache.d.ts +1 -1
  15. package/dist/build/types.d.ts +2 -1
  16. package/dist/cli/index.js +9467 -5870
  17. package/dist/client/boundaries.d.ts +6 -4
  18. package/dist/client/define-route.d.ts +35 -0
  19. package/dist/client/document.d.ts +26 -0
  20. package/dist/client/hmr.d.ts +10 -0
  21. package/dist/client/internal/runtime-types.d.ts +32 -0
  22. package/dist/client/link.d.ts +2 -1
  23. package/dist/client/link.js +1884 -1227
  24. package/dist/client/router/boundary-tree.d.ts +1 -1
  25. package/dist/client/router/index.d.ts +2 -2
  26. package/dist/client/router/link-utils.d.ts +14 -1
  27. package/dist/client/router/search/index.d.ts +3 -7
  28. package/dist/client/router/sync-catch-up.d.ts +4 -4
  29. package/dist/client/router/types.d.ts +34 -26
  30. package/dist/client-only.d.ts +1 -0
  31. package/dist/client-only.js +1 -0
  32. package/dist/client.d.ts +11 -169
  33. package/dist/client.js +197 -36
  34. package/dist/config.d.ts +8 -8
  35. package/dist/config.js +10 -10
  36. package/dist/define-route.d.ts +1355 -0
  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 +24 -39
  40. package/dist/furin.js +14432 -7929
  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 +1708 -489
  45. package/dist/plugin/route-config-autofix.d.ts +31 -0
  46. package/dist/plugin/routes.d.ts +27 -0
  47. package/dist/plugin/transform-client.d.ts +0 -2
  48. package/dist/plugin/transform-isomorphic.d.ts +9 -0
  49. package/dist/routes.d.ts +6 -0
  50. package/dist/routes.js +1 -0
  51. package/dist/rsc/build/environment.d.ts +2 -0
  52. package/dist/rsc/build/index.d.ts +15 -0
  53. package/dist/rsc/client-codec.d.ts +1 -0
  54. package/dist/rsc/codec.d.ts +4 -0
  55. package/dist/rsc/flight-drain.d.ts +11 -0
  56. package/dist/rsc/render-error.d.ts +17 -0
  57. package/dist/rsc/server-codec.d.ts +2 -0
  58. package/dist/rsc/server-codec.js +24 -0
  59. package/dist/rsc/shared.d.ts +30 -0
  60. package/dist/rsc/symbols.d.ts +2 -0
  61. package/dist/rsc/transport-benchmark.d.ts +13 -0
  62. package/dist/rsc/version.d.ts +7 -0
  63. package/dist/rsc-client.d.ts +4 -0
  64. package/dist/rsc-client.js +166 -0
  65. package/dist/rsc-server.d.ts +11 -0
  66. package/dist/rsc.d.ts +11 -0
  67. package/dist/rsc.js +353 -0
  68. package/dist/server/cache/index.d.ts +2 -2
  69. package/dist/server/cache/invalidation.d.ts +2 -3
  70. package/dist/server/cache/isr-ssg.d.ts +5 -1
  71. package/dist/server/cache/isr.d.ts +9 -0
  72. package/dist/server/cache/route-cache.d.ts +10 -8
  73. package/dist/server/cache/store-view.d.ts +7 -7
  74. package/dist/server/dev-page-plugin.d.ts +11 -5
  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/internal.d.ts +5 -0
  81. package/dist/server/internal.js +3 -3
  82. package/dist/server/lang-detect.d.ts +1 -0
  83. package/dist/server/logger.d.ts +4 -0
  84. package/dist/server/render/assemble.d.ts +4 -9
  85. package/dist/server/render/document.d.ts +4 -0
  86. package/dist/server/render/element.d.ts +3 -2
  87. package/dist/server/render/index.d.ts +2 -2
  88. package/dist/server/render/index.ts +6 -3
  89. package/dist/server/render/isr.d.ts +1 -1
  90. package/dist/server/render/loaders.d.ts +9 -1
  91. package/dist/server/render/not-found.d.ts +2 -2
  92. package/dist/server/render/ppr-route.d.ts +8 -0
  93. package/dist/server/render/route-frame-transport.d.ts +2 -0
  94. package/dist/server/render/shell.ts +0 -1
  95. package/dist/server/render/ssg.d.ts +1 -1
  96. package/dist/server/render/ssr.d.ts +9 -9
  97. package/dist/server/render/template.d.ts +4 -0
  98. package/dist/server/router/defined-route.d.ts +24 -0
  99. package/dist/server/router/discovery.d.ts +1 -1
  100. package/dist/server/router/hmr.d.ts +17 -9
  101. package/dist/server/router/patterns.d.ts +7 -1
  102. package/dist/server/router/plugin.d.ts +8 -10
  103. package/dist/server/router/schema-merge.d.ts +3 -0
  104. package/dist/server/router/schemas.d.ts +14 -9
  105. package/dist/server/router/source-version.d.ts +2 -0
  106. package/dist/server/router/types.d.ts +2 -1
  107. package/dist/server/runtime-env.js +2 -2
  108. package/dist/server/sync/adapter.d.ts +45 -26
  109. package/dist/server/sync/config.d.ts +5 -9
  110. package/dist/server/sync/fingerprint.d.ts +2 -1
  111. package/dist/server/sync/index.d.ts +15 -4
  112. package/dist/server/sync/index.js +3023 -0
  113. package/dist/server/sync/notifier.d.ts +18 -0
  114. package/dist/server/sync/plugin.d.ts +53 -54
  115. package/dist/server/sync/postgres/index.d.ts +19 -0
  116. package/dist/server/sync/postgres/index.js +252 -0
  117. package/dist/server/sync/postgres/migrate.d.ts +2 -0
  118. package/dist/server/sync/postgres/migrate.js +17 -0
  119. package/dist/server/sync/postgres/migration.sql +66 -0
  120. package/dist/server/sync/redis/index.d.ts +30 -0
  121. package/dist/server/sync/redis/index.js +336 -0
  122. package/dist/server/sync/redis/scripts.d.ts +4 -0
  123. package/dist/server/sync/response.d.ts +10 -1
  124. package/dist/server/sync/runtime.d.ts +6 -0
  125. package/dist/server/sync/sqlite/index.d.ts +20 -0
  126. package/dist/server/sync/sqlite/index.js +250 -0
  127. package/dist/server/sync/stream.d.ts +12 -11
  128. package/dist/server-only.d.ts +1 -0
  129. package/dist/server-only.js +1 -0
  130. package/dist/shared/await.d.ts +6 -7
  131. package/dist/shared/elysia-contract.d.ts +41 -0
  132. package/dist/shared/parser.d.ts +2 -0
  133. package/dist/shared/route-frame.d.ts +49 -0
  134. package/dist/shared/route-map.d.ts +5 -0
  135. package/dist/shared/utils/index.d.ts +1 -3
  136. package/package.json +86 -41
  137. package/src/adapter/bun.ts +113 -36
  138. package/src/adapter/package.ts +23 -15
  139. package/src/adapter/static.ts +97 -50
  140. package/src/build/bun-build.ts +11 -0
  141. package/src/build/client.ts +41 -26
  142. package/src/build/entry-template.ts +20 -3
  143. package/src/build/hydrate.ts +128 -33
  144. package/src/build/index.ts +14 -2
  145. package/src/build/production-instrumentation.ts +14 -0
  146. package/src/build/route-types.ts +20 -116
  147. package/src/build/scan-server.ts +3 -2
  148. package/src/build/shared.ts +1 -1
  149. package/src/build/ssg-cache.ts +4 -2
  150. package/src/build/types.ts +2 -1
  151. package/src/cli/config.ts +3 -3
  152. package/src/cli/index.ts +58 -27
  153. package/src/client/boundaries.tsx +51 -19
  154. package/src/client/default-screens.tsx +4 -4
  155. package/src/client/define-route.ts +48 -0
  156. package/src/client/document.tsx +165 -0
  157. package/src/client/hmr.ts +40 -0
  158. package/src/client/internal/runtime-types.ts +33 -0
  159. package/src/client/link.tsx +51 -38
  160. package/src/client/router/boundary-tree.tsx +2 -2
  161. package/src/client/router/context.ts +13 -9
  162. package/src/client/router/index.ts +4 -0
  163. package/src/client/router/link-utils.ts +92 -3
  164. package/src/client/router/navigation.ts +9 -15
  165. package/src/client/router/provider.tsx +98 -53
  166. package/src/client/router/search/index.ts +9 -19
  167. package/src/client/router/search-store.ts +5 -1
  168. package/src/client/router/spa-response.ts +3 -4
  169. package/src/client/router/sync-catch-up.ts +9 -8
  170. package/src/client/router/types.ts +52 -26
  171. package/src/client/sync.ts +3 -2
  172. package/src/client-only.ts +1 -0
  173. package/src/client.ts +28 -283
  174. package/src/config.ts +10 -10
  175. package/src/define-route.ts +959 -0
  176. package/src/devtools/devtools-element.js +885 -0
  177. package/src/devtools/protocol.ts +139 -0
  178. package/src/furin.ts +475 -136
  179. package/src/isomorphic.ts +66 -0
  180. package/src/plugin/binding-scope.ts +158 -0
  181. package/src/plugin/dead-code-elimination.ts +202 -0
  182. package/src/plugin/index.ts +14 -18
  183. package/src/plugin/route-config-autofix.ts +1140 -0
  184. package/src/plugin/routes.ts +891 -0
  185. package/src/plugin/transform-client.ts +160 -330
  186. package/src/plugin/transform-isomorphic.ts +464 -0
  187. package/src/routes.ts +6 -0
  188. package/src/rsc/build/environment.ts +30 -0
  189. package/src/rsc/build/index.ts +94 -0
  190. package/src/rsc/client-codec.ts +11 -0
  191. package/src/rsc/codec.ts +85 -0
  192. package/src/rsc/flight-drain.ts +68 -0
  193. package/src/rsc/react-server-dom-webpack.d.ts +23 -0
  194. package/src/rsc/render-error.ts +120 -0
  195. package/src/rsc/server-codec.ts +25 -0
  196. package/src/rsc/shared.tsx +113 -0
  197. package/src/rsc/symbols.ts +2 -0
  198. package/src/rsc/transport-benchmark.ts +73 -0
  199. package/src/rsc/version.ts +57 -0
  200. package/src/rsc-client.ts +12 -0
  201. package/src/rsc-server.tsx +91 -0
  202. package/src/rsc.tsx +75 -0
  203. package/src/server/auto-invalidate/registry.ts +2 -2
  204. package/src/server/auto-invalidate/runtime.ts +32 -6
  205. package/src/server/cache/dev-loader.ts +70 -10
  206. package/src/server/cache/index.ts +7 -3
  207. package/src/server/cache/invalidation.ts +33 -15
  208. package/src/server/cache/isr-ssg.ts +12 -3
  209. package/src/server/cache/isr.ts +84 -9
  210. package/src/server/cache/route-cache.ts +49 -36
  211. package/src/server/cache/store-view.ts +7 -7
  212. package/src/server/context-logger.ts +7 -7
  213. package/src/server/dev-page-plugin.ts +38 -17
  214. package/src/server/devtools/hub.ts +64 -0
  215. package/src/server/devtools/instrumentation.production.ts +80 -0
  216. package/src/server/devtools/instrumentation.ts +72 -0
  217. package/src/server/devtools/plugin.ts +269 -0
  218. package/src/server/devtools/request-context.ts +76 -0
  219. package/src/server/instance.ts +4 -4
  220. package/src/server/internal.ts +5 -0
  221. package/src/server/lang-detect.ts +14 -10
  222. package/src/server/logger.ts +21 -0
  223. package/src/server/render/assemble.ts +58 -36
  224. package/src/server/render/document.tsx +25 -0
  225. package/src/server/render/element.tsx +24 -9
  226. package/src/server/render/index.ts +6 -3
  227. package/src/server/render/isr.ts +182 -117
  228. package/src/server/render/loaders.ts +289 -50
  229. package/src/server/render/not-found.ts +57 -38
  230. package/src/server/render/ppr-route.ts +146 -0
  231. package/src/server/render/route-frame-transport.ts +62 -0
  232. package/src/server/render/shell.ts +0 -1
  233. package/src/server/render/ssg.ts +7 -6
  234. package/src/server/render/ssr.ts +412 -166
  235. package/src/server/render/template.ts +93 -2
  236. package/src/server/router/defined-route.ts +110 -0
  237. package/src/server/router/discovery.ts +165 -48
  238. package/src/server/router/hmr.ts +126 -53
  239. package/src/server/router/patterns.ts +56 -27
  240. package/src/server/router/plugin.ts +210 -200
  241. package/src/server/router/schema-merge.ts +42 -0
  242. package/src/server/router/schemas.ts +61 -76
  243. package/src/server/router/source-version.ts +16 -0
  244. package/src/server/router/types.ts +3 -1
  245. package/src/server/sync/adapter.ts +44 -28
  246. package/src/server/sync/config.ts +9 -27
  247. package/src/server/sync/fingerprint.ts +8 -3
  248. package/src/server/sync/index.ts +37 -13
  249. package/src/server/sync/notifier.ts +109 -0
  250. package/src/server/sync/plugin.ts +184 -89
  251. package/src/server/sync/postgres/index.ts +301 -0
  252. package/src/server/sync/postgres/migrate.ts +16 -0
  253. package/src/server/sync/postgres/migration.sql +66 -0
  254. package/src/server/sync/redis/index.ts +342 -0
  255. package/src/server/sync/redis/scripts.ts +74 -0
  256. package/src/server/sync/response.ts +145 -16
  257. package/src/server/sync/runtime.ts +27 -0
  258. package/src/server/sync/sql.d.ts +4 -0
  259. package/src/server/sync/sqlite/index.ts +337 -0
  260. package/src/server/sync/sqlite/migration.sql +70 -0
  261. package/src/server/sync/stream.ts +137 -81
  262. package/src/server-only.ts +1 -0
  263. package/src/shared/await.tsx +10 -11
  264. package/src/shared/deferred-ndjson.ts +34 -6
  265. package/src/shared/digest.ts +2 -2
  266. package/src/shared/elysia-contract.ts +63 -0
  267. package/src/shared/parser.ts +5 -0
  268. package/src/shared/route-frame.ts +429 -0
  269. package/src/shared/route-map.ts +37 -0
  270. package/src/shared/search-params.ts +7 -7
  271. package/src/shared/utils/index.ts +6 -22
  272. package/dist/build/server-routes-entry.d.ts +0 -13
  273. package/dist/build/server-routes-entry.ts +0 -30
  274. package/dist/server/dev-inspector.d.ts +0 -89
  275. package/dist/server/router/index.d.ts +0 -6
  276. package/dist/server/router/index.js +0 -27
  277. package/dist/server/router/index.ts +0 -28
  278. package/dist/server/sync/memory-adapter.d.ts +0 -17
  279. package/src/build/server-routes-entry.ts +0 -30
  280. package/src/server/dev-inspector.ts +0 -60
  281. package/src/server/router/index.ts +0 -28
  282. package/src/server/sync/memory-adapter.ts +0 -196
@@ -1,8 +1,10 @@
1
- import { t } from "elysia";
2
- import { parseQueryFromURL, parseQueryStandardSchema } from "elysia/parse-query";
3
- import { getSchemaValidator } from "elysia/schema";
4
- import type { AnySchema } from "elysia/types";
5
- import type { RuntimeRoute } from "../../client.ts";
1
+ import type { RuntimeRoute } from "../../client/internal/runtime-types.ts";
2
+ import {
3
+ type FurinSchema,
4
+ getSchemaValidator,
5
+ parseQueryFromURL,
6
+ parseQueryStandardSchema,
7
+ } from "../../shared/elysia-contract.ts";
6
8
  import {
7
9
  collectSearchDefaults,
8
10
  type SearchParamsInput,
@@ -10,6 +12,8 @@ import {
10
12
  } from "../../shared/search-params.ts";
11
13
  import { buildRouteRegex } from "./patterns.ts";
12
14
 
15
+ import { mergeRouteSchemas } from "./schema-merge.ts";
16
+
13
17
  interface UnknownObject {
14
18
  [key: string]: unknown;
15
19
  }
@@ -44,7 +48,7 @@ export function parseDataEndpointPath(rawPath: string): { url: URL; pathname: st
44
48
  if (url.origin !== "http://localhost") {
45
49
  return;
46
50
  }
47
- return { url, pathname: url.pathname };
51
+ return { pathname: url.pathname, url };
48
52
  }
49
53
 
50
54
  function isObjectSchema(schema: unknown): schema is UnknownObject {
@@ -129,103 +133,84 @@ export type ParseRouteQueryResult =
129
133
  | { ok: true; query: SearchParamsInput }
130
134
  | { errors: unknown; ok: false };
131
135
 
132
- /**
133
- * Parses and validates a logical route URL's search string for the synthetic
134
- * `/_furin/data` request path. This keeps SPA navigations aligned with the
135
- * Elysia guard used by the full SSR route.
136
- *
137
- * @internal Exported for unit testing.
138
- */
139
- export async function parseRouteQuery(
140
- url: URL,
141
- schema: AnySchema | undefined
142
- ): Promise<ParseRouteQueryResult> {
143
- if (!schema) {
144
- return { ok: true, query: parseQueryFromURL(url.search, 1) as SearchParamsInput };
145
- }
136
+ export type ParseRouteParamsResult =
137
+ | { ok: true; params: UnknownObject }
138
+ | { errors: unknown; ok: false };
139
+
140
+ type RouteInputValidationResult =
141
+ | { ok: true; value: UnknownObject }
142
+ | { errors: unknown; ok: false };
146
143
 
144
+ async function validateRouteInput(
145
+ input: UnknownObject,
146
+ schema: FurinSchema
147
+ ): Promise<RouteInputValidationResult> {
147
148
  if (isStandardSchema(schema)) {
148
- const rawQuery = parseQueryStandardSchema(url.search, 1) as UnknownObject;
149
149
  const validator = getSchemaValidator(schema, { dynamic: true });
150
- const checked = await validator?.Check(rawQuery);
150
+ const checked = await validator?.Check(input);
151
151
  if (checked && typeof checked === "object" && "issues" in checked) {
152
152
  return { errors: checked.issues, ok: false };
153
153
  }
154
154
  if (checked && typeof checked === "object" && "value" in checked) {
155
- return { ok: true, query: checked.value as SearchParamsInput };
155
+ return { ok: true, value: checked.value as UnknownObject };
156
156
  }
157
- return { ok: true, query: rawQuery as SearchParamsInput };
157
+ return { ok: true, value: input };
158
158
  }
159
159
 
160
- const rawQuery = parseJsonQueryObjects(
161
- parseQueryFromURL(url.search, 1, collectQueryArrayKeys(schema)) as UnknownObject,
162
- collectQueryObjectKeys(schema)
163
- );
164
- const queryWithDefaults = applySchemaDefaults(schema as UnknownObject, rawQuery);
160
+ const inputWithDefaults = applySchemaDefaults(schema as UnknownObject, input);
165
161
  const validator = getSchemaValidator(schema, { coerce: true, dynamic: true });
166
- if (validator?.Check(queryWithDefaults) === false) {
167
- return { errors: [...(validator?.Errors(queryWithDefaults) ?? [])], ok: false };
162
+ if (validator?.Check(inputWithDefaults) === false) {
163
+ return { errors: [...(validator?.Errors(inputWithDefaults) ?? [])], ok: false };
168
164
  }
169
165
 
170
166
  return {
171
167
  ok: true,
172
- query: (validator?.parse(queryWithDefaults) ?? queryWithDefaults) as SearchParamsInput,
168
+ value: (validator?.parse(inputWithDefaults) ?? inputWithDefaults) as UnknownObject,
173
169
  };
174
170
  }
175
171
 
176
- // Standard structural keys on a TObject — everything else is a user-supplied option
177
- // (e.g. additionalProperties, $id, description, title) and must be preserved.
178
- const TOBJECT_STRUCTURAL_KEYS = new Set(["type", "properties", "required"]);
179
- const TYPEBOX_KIND = Symbol.for("TypeBox.Kind");
172
+ /**
173
+ * Parses and validates a logical route URL's search string for the synthetic
174
+ * `/_furin/data` request path. This keeps SPA navigations aligned with the
175
+ * Elysia guard used by the full SSR route.
176
+ *
177
+ * @internal Exported for unit testing.
178
+ */
179
+ export async function parseRouteQuery(
180
+ url: URL,
181
+ schema: FurinSchema | undefined
182
+ ): Promise<ParseRouteQueryResult> {
183
+ if (!schema) {
184
+ return { ok: true, query: parseQueryFromURL(url.search, 1) as SearchParamsInput };
185
+ }
180
186
 
181
- function isTypeBoxObjectSchema(schema: unknown): schema is UnknownObject {
182
- return (
183
- isObjectSchema(schema) &&
184
- (schema as UnknownObject & { [key: symbol]: unknown })[TYPEBOX_KIND] === "Object"
185
- );
187
+ const rawQuery = isStandardSchema(schema)
188
+ ? (parseQueryStandardSchema(url.search, 1) as UnknownObject)
189
+ : parseJsonQueryObjects(
190
+ parseQueryFromURL(url.search, 1, collectQueryArrayKeys(schema)) as UnknownObject,
191
+ collectQueryObjectKeys(schema)
192
+ );
193
+ const result = await validateRouteInput(rawQuery, schema);
194
+ return result.ok ? { ok: true, query: result.value as SearchParamsInput } : result;
186
195
  }
187
196
 
188
197
  /**
189
- * Merges TObject schemas from all routeChain entries for a given key.
190
- * Properties are spread left-to-right (leaf wins on key conflict).
191
- * Object-level options (additionalProperties, $id, description, …) are also
192
- * merged with the same leaf-wins semantics so they are not silently dropped.
193
- * Returns undefined when no entry in the chain defines the key.
198
+ * Validates and coerces path params for the synthetic `/_furin/data` request
199
+ * path. This mirrors the composed Elysia route schema so SPA loaders receive
200
+ * the same values as full SSR loaders.
194
201
  *
195
202
  * @internal Exported for unit testing.
196
203
  */
197
- export function mergeRouteSchemas(
198
- routeChain: RuntimeRoute[],
199
- key: "params" | "query"
200
- ): AnySchema | undefined {
201
- const schemas = routeChain.flatMap((r) => (r[key] ? [r[key]] : [])) as Record<string, unknown>[];
202
-
203
- if (schemas.length === 0) {
204
- return;
205
- }
206
- if (schemas.length === 1) {
207
- return schemas[0] as AnySchema;
208
- }
209
-
210
- if (schemas.some((s) => !isTypeBoxObjectSchema(s))) {
211
- throw new Error(
212
- `[furin] Merging ${key} schemas across the route chain requires TypeBox in V1. Use TypeBox for parent/child ${key}, or define ${key} only on leaf routes.`
213
- );
204
+ export async function parseRouteParams(
205
+ params: UnknownObject,
206
+ schema: FurinSchema | undefined
207
+ ): Promise<ParseRouteParamsResult> {
208
+ if (!schema) {
209
+ return { ok: true, params };
214
210
  }
215
211
 
216
- const mergedProperties = Object.assign(
217
- {},
218
- ...schemas.map((s) => (s.properties as Record<string, unknown>) ?? {})
219
- );
220
-
221
- const mergedOptions = Object.assign(
222
- {},
223
- ...schemas.map((s) =>
224
- Object.fromEntries(Object.entries(s).filter(([k]) => !TOBJECT_STRUCTURAL_KEYS.has(k)))
225
- )
226
- );
227
-
228
- return t.Object(mergedProperties, mergedOptions) as AnySchema;
212
+ const result = await validateRouteInput(params, schema);
213
+ return result.ok ? { ok: true, params: result.value } : result;
229
214
  }
230
215
 
231
216
  export function createSearchRouteMetadata(
@@ -0,0 +1,16 @@
1
+ import { statSync } from "node:fs";
2
+
3
+ let routeModuleGeneration = 0;
4
+
5
+ export function routeModuleSourceVersion(path: string): string {
6
+ try {
7
+ const stats = statSync(path, { bigint: true });
8
+ return Bun.hash(`${stats.mtimeNs}:${stats.size}:${routeModuleGeneration}`).toString();
9
+ } catch {
10
+ return Bun.hash(`missing:${routeModuleGeneration}`).toString();
11
+ }
12
+ }
13
+
14
+ export function invalidateRouteModuleSourceVersions(): void {
15
+ routeModuleGeneration += 1;
16
+ }
@@ -1,4 +1,4 @@
1
- import type { RuntimePage, RuntimeRoute } from "../../client.ts";
1
+ import type { RuntimePage, RuntimeRoute } from "../../client/internal/runtime-types.ts";
2
2
  import type { ErrorComponent } from "../../shared/error.ts";
3
3
  import type { NotFoundComponent } from "../../shared/not-found.ts";
4
4
 
@@ -49,6 +49,8 @@ export interface ResolvedRoute {
49
49
  tags?: string[];
50
50
  }
51
51
 
52
+ export type ResolvedRoutesSource = ResolvedRoute[] | (() => ResolvedRoute[]);
53
+
52
54
  export interface RootLayout {
53
55
  error?: ErrorComponent;
54
56
  errorPath?: string;
@@ -10,39 +10,40 @@ export interface BeginMutationInput {
10
10
  principal: string;
11
11
  }
12
12
 
13
+ export interface MutationLease {
14
+ id: string;
15
+ key: string;
16
+ leaseMs: number;
17
+ principal: string;
18
+ }
19
+
13
20
  export type BeginMutationResult =
14
- | { kind: "execute"; mutationId: string }
21
+ | { kind: "execute"; lease: MutationLease }
15
22
  | { kind: "replay"; response: StoredResponse }
16
- | { kind: "conflict"; reason: "in-progress" | "payload-mismatch" }
17
- | { kind: "unavailable" };
23
+ | { kind: "conflict"; reason: "in-progress" | "payload-mismatch" };
24
+
25
+ export type SyncInvalidation =
26
+ | { kind: "path"; path: string; type: "layout" | "page" }
27
+ | { kind: "tags"; tags: readonly string[] };
18
28
 
19
- export interface CommitMutationInput {
20
- mutationId: string;
29
+ export interface CompleteMutationInput {
30
+ invalidations: readonly SyncInvalidation[];
31
+ lease: MutationLease;
21
32
  response: StoredResponse;
22
33
  }
23
34
 
24
- export interface AbortMutationInput {
25
- mutationId: string;
26
- }
35
+ export type CompleteMutationResult =
36
+ | { cursor: string | undefined; kind: "committed" }
37
+ | { kind: "lost" };
27
38
 
28
39
  export interface SyncChange {
29
40
  cursor: string;
30
- invalidations: readonly string[];
31
- }
32
-
33
- export interface AppendChangesInput {
34
- invalidations: readonly string[];
35
- path: string;
36
- }
37
-
38
- export interface AppendChangesResult {
39
- change: SyncChange;
41
+ invalidations: readonly SyncInvalidation[];
40
42
  }
41
43
 
42
44
  export interface ReadChangesInput {
43
45
  after: string | undefined;
44
46
  limit: number;
45
- path: string;
46
47
  }
47
48
 
48
49
  export interface ChangePage {
@@ -52,14 +53,29 @@ export interface ChangePage {
52
53
  reset: boolean;
53
54
  }
54
55
 
55
- export type SyncChangeListener = (change: SyncChange) => void;
56
- type MaybePromise<T> = Promise<T> | T;
57
-
58
56
  export interface SyncAdapter {
59
- abortMutation(input: AbortMutationInput): MaybePromise<void>;
60
- appendChanges(input: AppendChangesInput): MaybePromise<AppendChangesResult>;
61
- beginMutation(input: BeginMutationInput): MaybePromise<BeginMutationResult>;
62
- commitMutation(input: CommitMutationInput): MaybePromise<void>;
63
- readChanges(input: ReadChangesInput): MaybePromise<ChangePage>;
64
- subscribe(path: string, listener: SyncChangeListener): () => void;
57
+ abortMutation: (lease: MutationLease) => Promise<void>;
58
+ beginMutation: (input: BeginMutationInput) => Promise<BeginMutationResult>;
59
+ completeMutation: (input: CompleteMutationInput) => Promise<CompleteMutationResult>;
60
+ currentCursor: () => Promise<string>;
61
+ readChanges: (input: ReadChangesInput) => Promise<ChangePage>;
62
+ renewMutation: (lease: MutationLease) => Promise<"lost" | "renewed">;
63
+ readonly scope: "distributed" | "host-local" | "process-local";
65
64
  }
65
+
66
+ export interface SyncSubscription {
67
+ unsubscribe: () => Promise<void>;
68
+ }
69
+
70
+ export interface SyncNotifier {
71
+ publish: (cursor: string) => Promise<void>;
72
+ subscribe: (listener: (cursor: string) => void) => Promise<SyncSubscription>;
73
+ }
74
+
75
+ export interface SyncRuntimeOptions {
76
+ adapter: SyncAdapter;
77
+ notifier?: SyncNotifier;
78
+ principal: (context: Context) => Promise<string> | string;
79
+ }
80
+
81
+ import type { Context } from "elysia";
@@ -1,5 +1,6 @@
1
1
  import { AsyncLocalStorage } from "node:async_hooks";
2
- import { allInstances, currentInstance } from "../instance.ts";
2
+ import { currentInstance } from "../instance.ts";
3
+ import type { SyncRuntimeOptions } from "./adapter.ts";
3
4
 
4
5
  const SYNC_STREAM_DEFAULT_PATH = "/_furin/sync";
5
6
 
@@ -7,19 +8,16 @@ const SYNC_STREAM_DEFAULT_PATH = "/_furin/sync";
7
8
  // stream path comes from the current furin instance.
8
9
  const requestSyncStreamPath = new AsyncLocalStorage<string | undefined>();
9
10
 
10
- export type FurinSyncOption =
11
- | boolean
12
- | {
13
- streamPath?: string;
14
- };
11
+ export interface FurinSyncOptions extends SyncRuntimeOptions {
12
+ streamPath?: string;
13
+ }
14
+
15
+ export type FurinSyncOption = FurinSyncOptions | false;
15
16
 
16
17
  export function resolveSyncStreamPath(sync: FurinSyncOption | undefined): string | undefined {
17
18
  if (!sync) {
18
19
  return;
19
20
  }
20
- if (sync === true) {
21
- return SYNC_STREAM_DEFAULT_PATH;
22
- }
23
21
  return sync.streamPath ?? SYNC_STREAM_DEFAULT_PATH;
24
22
  }
25
23
 
@@ -39,22 +37,6 @@ export function runWithSyncStreamPath<T>(path: string | undefined, fn: () => T):
39
37
  return requestSyncStreamPath.run(path, fn);
40
38
  }
41
39
 
42
- /**
43
- * Physical adapter channels a sync publication must reach. An explicit
44
- * override wins (historical single-channel behaviour); otherwise every
45
- * sync-enabled instance gets notified — a mutation on a shared API must wake
46
- * the SSE clients of every mounted app.
47
- */
48
- export function syncPublishChannels(): string[] {
49
- const override = requestSyncStreamPath.getStore();
50
- if (override !== undefined) {
51
- return [override];
52
- }
53
- const channels = allInstances()
54
- .filter((instance) => instance.syncStreamPath !== undefined)
55
- .map((instance) => `${instance.prefix}${instance.syncStreamPath}`);
56
- if (channels.length > 0) {
57
- return channels;
58
- }
59
- return [SYNC_STREAM_DEFAULT_PATH];
40
+ export function syncRuntimeOptions(sync: FurinSyncOptions): SyncRuntimeOptions {
41
+ return { adapter: sync.adapter, notifier: sync.notifier, principal: sync.principal };
60
42
  }
@@ -24,7 +24,13 @@ function canonicalize(value: unknown): unknown {
24
24
  return value;
25
25
  }
26
26
 
27
- export async function createMutationFingerprint(input: FingerprintInput): Promise<string> {
27
+ export function sha256Hex(source: string): string {
28
+ const hasher = new Bun.CryptoHasher("sha256");
29
+ hasher.update(source);
30
+ return hasher.digest("hex");
31
+ }
32
+
33
+ export function createMutationFingerprint(input: FingerprintInput): string {
28
34
  const url = new URL(input.request.url);
29
35
  const query = [...url.searchParams.entries()].sort(
30
36
  ([leftKey, leftValue], [rightKey, rightValue]) =>
@@ -39,6 +45,5 @@ export async function createMutationFingerprint(input: FingerprintInput): Promis
39
45
  pathname: url.pathname,
40
46
  query,
41
47
  });
42
- const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(source));
43
- return [...new Uint8Array(digest)].map((byte) => byte.toString(16).padStart(2, "0")).join("");
48
+ return sha256Hex(source);
44
49
  }
@@ -1,15 +1,39 @@
1
1
  // biome-ignore-all lint/performance/noBarrelFile: sync has a small public/internal surface
2
- export type { FurinSyncOption } from "./config.ts";
3
- export {
4
- getSyncStreamPath,
5
- resolveSyncStreamPath,
6
- runWithSyncStreamPath,
2
+
3
+ import {
4
+ getSyncStreamPath as getSyncStreamPathImplementation,
5
+ resolveSyncStreamPath as resolveSyncStreamPathImplementation,
6
+ runWithSyncStreamPath as runWithSyncStreamPathImplementation,
7
7
  } from "./config.ts";
8
- export { furinSync, type SyncInput, type SyncRouteOption } from "./plugin.ts";
9
- export {
10
- __resetSyncState,
11
- createSyncStreamPlugin,
12
- publishSyncInvalidation,
13
- type SyncChange,
14
- type SyncChangePage,
15
- } from "./stream.ts";
8
+ import { PollingSyncNotifier as PollingSyncNotifierImplementation } from "./notifier.ts";
9
+ import { furinSync as furinSyncImplementation } from "./plugin.ts";
10
+ import { createSyncStreamPlugin as createSyncStreamPluginImplementation } from "./stream.ts";
11
+
12
+ export const createSyncStreamPlugin = createSyncStreamPluginImplementation;
13
+ export const furinSync = furinSyncImplementation;
14
+ export const getSyncStreamPath = getSyncStreamPathImplementation;
15
+ export const PollingSyncNotifier = PollingSyncNotifierImplementation;
16
+ export const resolveSyncStreamPath = resolveSyncStreamPathImplementation;
17
+ export const runWithSyncStreamPath = runWithSyncStreamPathImplementation;
18
+
19
+ export type PollingSyncNotifier = PollingSyncNotifierImplementation;
20
+
21
+ export type {
22
+ BeginMutationInput,
23
+ BeginMutationResult,
24
+ ChangePage,
25
+ CompleteMutationInput,
26
+ CompleteMutationResult,
27
+ MutationLease,
28
+ ReadChangesInput,
29
+ StoredResponse,
30
+ SyncAdapter,
31
+ SyncChange,
32
+ SyncInvalidation,
33
+ SyncNotifier,
34
+ SyncRuntimeOptions,
35
+ SyncSubscription,
36
+ } from "./adapter.ts";
37
+ export type { FurinSyncOption, FurinSyncOptions } from "./config.ts";
38
+ export type { SyncInput, SyncRouteOption } from "./plugin.ts";
39
+ export type { SyncChangePage } from "./stream.ts";
@@ -0,0 +1,109 @@
1
+ import type { SyncAdapter, SyncNotifier, SyncSubscription } from "./adapter.ts";
2
+
3
+ interface ListenerSubscription {
4
+ listener: (cursor: string) => void;
5
+ }
6
+
7
+ export class PollingSyncNotifier implements SyncNotifier {
8
+ private readonly adapter: SyncAdapter;
9
+ private readonly intervalMs: number;
10
+ private readonly listeners = new Set<ListenerSubscription>();
11
+ private cursor: string | undefined;
12
+ private initialization: Promise<void> | undefined;
13
+ private polling: Promise<void> | undefined;
14
+ private revision = 0;
15
+ private timer: ReturnType<typeof setTimeout> | undefined;
16
+
17
+ constructor(adapter: SyncAdapter, intervalMs: number) {
18
+ this.adapter = adapter;
19
+ this.intervalMs = intervalMs;
20
+ }
21
+
22
+ publish(cursor: string): Promise<void> {
23
+ this.revision += 1;
24
+ this.emit(cursor);
25
+ return Promise.resolve();
26
+ }
27
+
28
+ async subscribe(listener: (cursor: string) => void): Promise<SyncSubscription> {
29
+ const subscription = { listener };
30
+ this.listeners.add(subscription);
31
+ try {
32
+ await this.start();
33
+ } catch (error) {
34
+ this.listeners.delete(subscription);
35
+ throw error;
36
+ }
37
+ return {
38
+ unsubscribe: () => {
39
+ this.listeners.delete(subscription);
40
+ if (this.listeners.size === 0 && this.timer) {
41
+ clearTimeout(this.timer);
42
+ this.timer = undefined;
43
+ }
44
+ return Promise.resolve();
45
+ },
46
+ };
47
+ }
48
+
49
+ private emit(cursor: string): void {
50
+ if (cursor === this.cursor) {
51
+ return;
52
+ }
53
+ this.cursor = cursor;
54
+ for (const subscription of [...this.listeners]) {
55
+ try {
56
+ subscription.listener(cursor);
57
+ } catch {
58
+ // Notifications are best-effort wake-ups; durable recovery reads the change log.
59
+ }
60
+ }
61
+ }
62
+
63
+ private async poll(): Promise<void> {
64
+ const { revision } = this;
65
+ try {
66
+ const cursor = await this.adapter.currentCursor();
67
+ if (revision === this.revision && this.listeners.size > 0) {
68
+ this.emit(cursor);
69
+ }
70
+ } catch {
71
+ // A later poll retries transient adapter failures.
72
+ }
73
+ }
74
+
75
+ private async start(): Promise<void> {
76
+ if (this.timer || this.polling) {
77
+ return;
78
+ }
79
+ if (!this.initialization) {
80
+ const { revision } = this;
81
+ this.initialization = this.adapter
82
+ .currentCursor()
83
+ .then((cursor) => {
84
+ if (revision === this.revision) {
85
+ this.cursor = cursor;
86
+ }
87
+ })
88
+ .finally(() => {
89
+ this.initialization = undefined;
90
+ });
91
+ }
92
+ await this.initialization;
93
+ this.schedulePoll();
94
+ }
95
+
96
+ private schedulePoll(): void {
97
+ if (this.listeners.size === 0 || this.timer || this.polling) {
98
+ return;
99
+ }
100
+ this.timer = setTimeout(() => {
101
+ this.timer = undefined;
102
+ this.polling = this.poll().finally(() => {
103
+ this.polling = undefined;
104
+ this.schedulePoll();
105
+ });
106
+ }, this.intervalMs);
107
+ this.timer.unref?.();
108
+ }
109
+ }