@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.
- package/README.md +2 -3
- package/dist/adapter/bun.d.ts +1 -1
- package/dist/adapter/static.d.ts +1 -1
- package/dist/build/bun-build.d.ts +3 -0
- package/dist/build/client.d.ts +2 -2
- package/dist/build/entry-template.d.ts +6 -0
- package/dist/build/entry-template.ts +20 -3
- package/dist/build/hydrate.d.ts +1 -1
- package/dist/build/index.d.ts +1 -1
- package/dist/build/index.js +9363 -5827
- package/dist/build/production-instrumentation.d.ts +1 -0
- package/dist/build/route-types.d.ts +3 -23
- package/dist/build/shared.d.ts +1 -1
- package/dist/build/ssg-cache.d.ts +1 -1
- package/dist/build/types.d.ts +2 -1
- package/dist/cli/index.js +9467 -5870
- package/dist/client/boundaries.d.ts +6 -4
- package/dist/client/define-route.d.ts +35 -0
- package/dist/client/document.d.ts +26 -0
- package/dist/client/hmr.d.ts +10 -0
- package/dist/client/internal/runtime-types.d.ts +32 -0
- package/dist/client/link.d.ts +2 -1
- package/dist/client/link.js +1884 -1227
- package/dist/client/router/boundary-tree.d.ts +1 -1
- package/dist/client/router/index.d.ts +2 -2
- package/dist/client/router/link-utils.d.ts +14 -1
- package/dist/client/router/search/index.d.ts +3 -7
- package/dist/client/router/sync-catch-up.d.ts +4 -4
- package/dist/client/router/types.d.ts +34 -26
- package/dist/client-only.d.ts +1 -0
- package/dist/client-only.js +1 -0
- package/dist/client.d.ts +11 -169
- package/dist/client.js +197 -36
- package/dist/config.d.ts +8 -8
- package/dist/config.js +10 -10
- package/dist/define-route.d.ts +1355 -0
- package/dist/devtools/devtools-element.d.ts +1 -0
- package/dist/devtools/protocol.d.ts +119 -0
- package/dist/furin.d.ts +24 -39
- package/dist/furin.js +14432 -7929
- package/dist/isomorphic.d.ts +18 -0
- package/dist/plugin/binding-scope.d.ts +2 -0
- package/dist/plugin/dead-code-elimination.d.ts +3 -0
- package/dist/plugin/index.js +1708 -489
- package/dist/plugin/route-config-autofix.d.ts +31 -0
- package/dist/plugin/routes.d.ts +27 -0
- package/dist/plugin/transform-client.d.ts +0 -2
- package/dist/plugin/transform-isomorphic.d.ts +9 -0
- package/dist/routes.d.ts +6 -0
- package/dist/routes.js +1 -0
- package/dist/rsc/build/environment.d.ts +2 -0
- package/dist/rsc/build/index.d.ts +15 -0
- package/dist/rsc/client-codec.d.ts +1 -0
- package/dist/rsc/codec.d.ts +4 -0
- package/dist/rsc/flight-drain.d.ts +11 -0
- package/dist/rsc/render-error.d.ts +17 -0
- package/dist/rsc/server-codec.d.ts +2 -0
- package/dist/rsc/server-codec.js +24 -0
- package/dist/rsc/shared.d.ts +30 -0
- package/dist/rsc/symbols.d.ts +2 -0
- package/dist/rsc/transport-benchmark.d.ts +13 -0
- package/dist/rsc/version.d.ts +7 -0
- package/dist/rsc-client.d.ts +4 -0
- package/dist/rsc-client.js +166 -0
- package/dist/rsc-server.d.ts +11 -0
- package/dist/rsc.d.ts +11 -0
- package/dist/rsc.js +353 -0
- package/dist/server/cache/index.d.ts +2 -2
- package/dist/server/cache/invalidation.d.ts +2 -3
- package/dist/server/cache/isr-ssg.d.ts +5 -1
- package/dist/server/cache/isr.d.ts +9 -0
- package/dist/server/cache/route-cache.d.ts +10 -8
- package/dist/server/cache/store-view.d.ts +7 -7
- package/dist/server/dev-page-plugin.d.ts +11 -5
- package/dist/server/devtools/hub.d.ts +11 -0
- package/dist/server/devtools/instrumentation.d.ts +22 -0
- package/dist/server/devtools/instrumentation.production.d.ts +44 -0
- package/dist/server/devtools/plugin.d.ts +3 -0
- package/dist/server/devtools/request-context.d.ts +6 -0
- package/dist/server/internal.d.ts +5 -0
- package/dist/server/internal.js +3 -3
- package/dist/server/lang-detect.d.ts +1 -0
- package/dist/server/logger.d.ts +4 -0
- package/dist/server/render/assemble.d.ts +4 -9
- package/dist/server/render/document.d.ts +4 -0
- package/dist/server/render/element.d.ts +3 -2
- package/dist/server/render/index.d.ts +2 -2
- package/dist/server/render/index.ts +6 -3
- package/dist/server/render/isr.d.ts +1 -1
- package/dist/server/render/loaders.d.ts +9 -1
- package/dist/server/render/not-found.d.ts +2 -2
- package/dist/server/render/ppr-route.d.ts +8 -0
- package/dist/server/render/route-frame-transport.d.ts +2 -0
- package/dist/server/render/shell.ts +0 -1
- package/dist/server/render/ssg.d.ts +1 -1
- package/dist/server/render/ssr.d.ts +9 -9
- package/dist/server/render/template.d.ts +4 -0
- package/dist/server/router/defined-route.d.ts +24 -0
- package/dist/server/router/discovery.d.ts +1 -1
- package/dist/server/router/hmr.d.ts +17 -9
- package/dist/server/router/patterns.d.ts +7 -1
- package/dist/server/router/plugin.d.ts +8 -10
- package/dist/server/router/schema-merge.d.ts +3 -0
- package/dist/server/router/schemas.d.ts +14 -9
- package/dist/server/router/source-version.d.ts +2 -0
- package/dist/server/router/types.d.ts +2 -1
- package/dist/server/runtime-env.js +2 -2
- package/dist/server/sync/adapter.d.ts +45 -26
- package/dist/server/sync/config.d.ts +5 -9
- package/dist/server/sync/fingerprint.d.ts +2 -1
- package/dist/server/sync/index.d.ts +15 -4
- package/dist/server/sync/index.js +3023 -0
- package/dist/server/sync/notifier.d.ts +18 -0
- package/dist/server/sync/plugin.d.ts +53 -54
- package/dist/server/sync/postgres/index.d.ts +19 -0
- package/dist/server/sync/postgres/index.js +252 -0
- package/dist/server/sync/postgres/migrate.d.ts +2 -0
- package/dist/server/sync/postgres/migrate.js +17 -0
- package/dist/server/sync/postgres/migration.sql +66 -0
- package/dist/server/sync/redis/index.d.ts +30 -0
- package/dist/server/sync/redis/index.js +336 -0
- package/dist/server/sync/redis/scripts.d.ts +4 -0
- package/dist/server/sync/response.d.ts +10 -1
- package/dist/server/sync/runtime.d.ts +6 -0
- package/dist/server/sync/sqlite/index.d.ts +20 -0
- package/dist/server/sync/sqlite/index.js +250 -0
- package/dist/server/sync/stream.d.ts +12 -11
- package/dist/server-only.d.ts +1 -0
- package/dist/server-only.js +1 -0
- package/dist/shared/await.d.ts +6 -7
- package/dist/shared/elysia-contract.d.ts +41 -0
- package/dist/shared/parser.d.ts +2 -0
- package/dist/shared/route-frame.d.ts +49 -0
- package/dist/shared/route-map.d.ts +5 -0
- package/dist/shared/utils/index.d.ts +1 -3
- package/package.json +86 -41
- package/src/adapter/bun.ts +113 -36
- package/src/adapter/package.ts +23 -15
- package/src/adapter/static.ts +97 -50
- package/src/build/bun-build.ts +11 -0
- package/src/build/client.ts +41 -26
- package/src/build/entry-template.ts +20 -3
- package/src/build/hydrate.ts +128 -33
- package/src/build/index.ts +14 -2
- package/src/build/production-instrumentation.ts +14 -0
- package/src/build/route-types.ts +20 -116
- package/src/build/scan-server.ts +3 -2
- package/src/build/shared.ts +1 -1
- package/src/build/ssg-cache.ts +4 -2
- package/src/build/types.ts +2 -1
- package/src/cli/config.ts +3 -3
- package/src/cli/index.ts +58 -27
- package/src/client/boundaries.tsx +51 -19
- package/src/client/default-screens.tsx +4 -4
- package/src/client/define-route.ts +48 -0
- package/src/client/document.tsx +165 -0
- package/src/client/hmr.ts +40 -0
- package/src/client/internal/runtime-types.ts +33 -0
- package/src/client/link.tsx +51 -38
- package/src/client/router/boundary-tree.tsx +2 -2
- package/src/client/router/context.ts +13 -9
- package/src/client/router/index.ts +4 -0
- package/src/client/router/link-utils.ts +92 -3
- package/src/client/router/navigation.ts +9 -15
- package/src/client/router/provider.tsx +98 -53
- package/src/client/router/search/index.ts +9 -19
- package/src/client/router/search-store.ts +5 -1
- package/src/client/router/spa-response.ts +3 -4
- package/src/client/router/sync-catch-up.ts +9 -8
- package/src/client/router/types.ts +52 -26
- package/src/client/sync.ts +3 -2
- package/src/client-only.ts +1 -0
- package/src/client.ts +28 -283
- package/src/config.ts +10 -10
- package/src/define-route.ts +959 -0
- package/src/devtools/devtools-element.js +885 -0
- package/src/devtools/protocol.ts +139 -0
- package/src/furin.ts +475 -136
- package/src/isomorphic.ts +66 -0
- package/src/plugin/binding-scope.ts +158 -0
- package/src/plugin/dead-code-elimination.ts +202 -0
- package/src/plugin/index.ts +14 -18
- package/src/plugin/route-config-autofix.ts +1140 -0
- package/src/plugin/routes.ts +891 -0
- package/src/plugin/transform-client.ts +160 -330
- package/src/plugin/transform-isomorphic.ts +464 -0
- package/src/routes.ts +6 -0
- package/src/rsc/build/environment.ts +30 -0
- package/src/rsc/build/index.ts +94 -0
- package/src/rsc/client-codec.ts +11 -0
- package/src/rsc/codec.ts +85 -0
- package/src/rsc/flight-drain.ts +68 -0
- package/src/rsc/react-server-dom-webpack.d.ts +23 -0
- package/src/rsc/render-error.ts +120 -0
- package/src/rsc/server-codec.ts +25 -0
- package/src/rsc/shared.tsx +113 -0
- package/src/rsc/symbols.ts +2 -0
- package/src/rsc/transport-benchmark.ts +73 -0
- package/src/rsc/version.ts +57 -0
- package/src/rsc-client.ts +12 -0
- package/src/rsc-server.tsx +91 -0
- package/src/rsc.tsx +75 -0
- package/src/server/auto-invalidate/registry.ts +2 -2
- package/src/server/auto-invalidate/runtime.ts +32 -6
- package/src/server/cache/dev-loader.ts +70 -10
- package/src/server/cache/index.ts +7 -3
- package/src/server/cache/invalidation.ts +33 -15
- package/src/server/cache/isr-ssg.ts +12 -3
- package/src/server/cache/isr.ts +84 -9
- package/src/server/cache/route-cache.ts +49 -36
- package/src/server/cache/store-view.ts +7 -7
- package/src/server/context-logger.ts +7 -7
- package/src/server/dev-page-plugin.ts +38 -17
- package/src/server/devtools/hub.ts +64 -0
- package/src/server/devtools/instrumentation.production.ts +80 -0
- package/src/server/devtools/instrumentation.ts +72 -0
- package/src/server/devtools/plugin.ts +269 -0
- package/src/server/devtools/request-context.ts +76 -0
- package/src/server/instance.ts +4 -4
- package/src/server/internal.ts +5 -0
- package/src/server/lang-detect.ts +14 -10
- package/src/server/logger.ts +21 -0
- package/src/server/render/assemble.ts +58 -36
- package/src/server/render/document.tsx +25 -0
- package/src/server/render/element.tsx +24 -9
- package/src/server/render/index.ts +6 -3
- package/src/server/render/isr.ts +182 -117
- package/src/server/render/loaders.ts +289 -50
- package/src/server/render/not-found.ts +57 -38
- package/src/server/render/ppr-route.ts +146 -0
- package/src/server/render/route-frame-transport.ts +62 -0
- package/src/server/render/shell.ts +0 -1
- package/src/server/render/ssg.ts +7 -6
- package/src/server/render/ssr.ts +412 -166
- package/src/server/render/template.ts +93 -2
- package/src/server/router/defined-route.ts +110 -0
- package/src/server/router/discovery.ts +165 -48
- package/src/server/router/hmr.ts +126 -53
- package/src/server/router/patterns.ts +56 -27
- package/src/server/router/plugin.ts +210 -200
- package/src/server/router/schema-merge.ts +42 -0
- package/src/server/router/schemas.ts +61 -76
- package/src/server/router/source-version.ts +16 -0
- package/src/server/router/types.ts +3 -1
- package/src/server/sync/adapter.ts +44 -28
- package/src/server/sync/config.ts +9 -27
- package/src/server/sync/fingerprint.ts +8 -3
- package/src/server/sync/index.ts +37 -13
- package/src/server/sync/notifier.ts +109 -0
- package/src/server/sync/plugin.ts +184 -89
- package/src/server/sync/postgres/index.ts +301 -0
- package/src/server/sync/postgres/migrate.ts +16 -0
- package/src/server/sync/postgres/migration.sql +66 -0
- package/src/server/sync/redis/index.ts +342 -0
- package/src/server/sync/redis/scripts.ts +74 -0
- package/src/server/sync/response.ts +145 -16
- package/src/server/sync/runtime.ts +27 -0
- package/src/server/sync/sql.d.ts +4 -0
- package/src/server/sync/sqlite/index.ts +337 -0
- package/src/server/sync/sqlite/migration.sql +70 -0
- package/src/server/sync/stream.ts +137 -81
- package/src/server-only.ts +1 -0
- package/src/shared/await.tsx +10 -11
- package/src/shared/deferred-ndjson.ts +34 -6
- package/src/shared/digest.ts +2 -2
- package/src/shared/elysia-contract.ts +63 -0
- package/src/shared/parser.ts +5 -0
- package/src/shared/route-frame.ts +429 -0
- package/src/shared/route-map.ts +37 -0
- package/src/shared/search-params.ts +7 -7
- package/src/shared/utils/index.ts +6 -22
- package/dist/build/server-routes-entry.d.ts +0 -13
- package/dist/build/server-routes-entry.ts +0 -30
- package/dist/server/dev-inspector.d.ts +0 -89
- package/dist/server/router/index.d.ts +0 -6
- package/dist/server/router/index.js +0 -27
- package/dist/server/router/index.ts +0 -28
- package/dist/server/sync/memory-adapter.d.ts +0 -17
- package/src/build/server-routes-entry.ts +0 -30
- package/src/server/dev-inspector.ts +0 -60
- package/src/server/router/index.ts +0 -28
- package/src/server/sync/memory-adapter.ts +0 -196
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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 {
|
|
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
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
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(
|
|
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,
|
|
155
|
+
return { ok: true, value: checked.value as UnknownObject };
|
|
156
156
|
}
|
|
157
|
-
return { ok: true,
|
|
157
|
+
return { ok: true, value: input };
|
|
158
158
|
}
|
|
159
159
|
|
|
160
|
-
const
|
|
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(
|
|
167
|
-
return { errors: [...(validator?.Errors(
|
|
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
|
-
|
|
168
|
+
value: (validator?.parse(inputWithDefaults) ?? inputWithDefaults) as UnknownObject,
|
|
173
169
|
};
|
|
174
170
|
}
|
|
175
171
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
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
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
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
|
-
*
|
|
190
|
-
*
|
|
191
|
-
*
|
|
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
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
):
|
|
201
|
-
|
|
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
|
|
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";
|
|
21
|
+
| { kind: "execute"; lease: MutationLease }
|
|
15
22
|
| { kind: "replay"; response: StoredResponse }
|
|
16
|
-
| { kind: "conflict"; reason: "in-progress" | "payload-mismatch" }
|
|
17
|
-
|
|
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
|
|
20
|
-
|
|
29
|
+
export interface CompleteMutationInput {
|
|
30
|
+
invalidations: readonly SyncInvalidation[];
|
|
31
|
+
lease: MutationLease;
|
|
21
32
|
response: StoredResponse;
|
|
22
33
|
}
|
|
23
34
|
|
|
24
|
-
export
|
|
25
|
-
|
|
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
|
|
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(
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
readChanges(input: ReadChangesInput)
|
|
64
|
-
|
|
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 {
|
|
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
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
43
|
-
return [...new Uint8Array(digest)].map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
48
|
+
return sha256Hex(source);
|
|
44
49
|
}
|
package/src/server/sync/index.ts
CHANGED
|
@@ -1,15 +1,39 @@
|
|
|
1
1
|
// biome-ignore-all lint/performance/noBarrelFile: sync has a small public/internal surface
|
|
2
|
-
|
|
3
|
-
|
|
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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
+
}
|