@teyik0/furin 0.4.0-alpha.2 → 0.5.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/dist/build/compile-entry.d.ts +2 -1
- package/dist/build/compile-entry.ts +17 -13
- package/dist/build/elysia-aot.d.ts +9 -0
- package/dist/build/entry-template.d.ts +5 -1
- package/dist/build/entry-template.ts +24 -5
- package/dist/build/index.js +753 -453
- package/dist/build/server-app-entry.d.ts +12 -0
- package/dist/cache.d.ts +1 -0
- package/dist/cache.js +185 -0
- package/dist/cli/index.js +766 -466
- package/dist/client/internal/runtime-types.d.ts +8 -1
- package/dist/client/link.js +221 -11
- package/dist/client/sync.d.ts +1 -0
- package/dist/config.d.ts +22 -21
- package/dist/config.js +1 -1
- package/dist/define-route.d.ts +273 -1009
- package/dist/furin.d.ts +20 -5
- package/dist/furin.js +6222 -5383
- package/dist/plugin/routes.d.ts +6 -0
- package/dist/server/assets/production.d.ts +3 -0
- package/dist/server/auto-invalidate/plugin.d.ts +8 -24
- package/dist/server/auto-invalidate/runtime.d.ts +2 -2
- package/dist/server/cache/invalidation.d.ts +1 -1
- package/dist/server/cache/page-cache-state.d.ts +5 -0
- package/dist/server/cache/page-cache.d.ts +53 -0
- package/dist/server/cache/redis/index.d.ts +39 -0
- package/dist/server/cache/redis/index.js +396 -0
- package/dist/server/cache/redis/scripts.d.ts +6 -0
- package/dist/server/context-logger.d.ts +2 -2
- package/dist/server/dev/runtime.production.d.ts +1 -0
- package/dist/server/dev-page-plugin.d.ts +7 -0
- package/dist/server/evlog.d.ts +14 -0
- package/dist/server/internal.d.ts +6 -0
- package/dist/server/internal.js +2537 -0
- package/dist/server/render/not-found.d.ts +1 -1
- package/dist/server/render/ssg.d.ts +5 -0
- package/dist/server/render/static-params.d.ts +4 -0
- package/dist/server/router/defined-route.d.ts +1 -1
- package/dist/server/router/schemas.d.ts +1 -0
- package/dist/server/sync/index.js +794 -116
- package/dist/server/sync/plugin.d.ts +31 -158
- package/dist/server/sync/stream.d.ts +34 -49
- package/dist/shared/elysia-contract.d.ts +8 -14
- package/package.json +13 -7
- package/src/adapter/README.md +9 -2
- package/src/adapter/bun.ts +73 -27
- package/src/adapter/package.ts +6 -2
- package/src/adapter/runtime-build.ts +11 -4
- package/src/adapter/static.ts +8 -6
- package/src/adapter/vercel.ts +49 -30
- package/src/build/client.ts +18 -1
- package/src/build/compile-entry.ts +17 -13
- package/src/build/elysia-aot.ts +102 -0
- package/src/build/entry-template.ts +24 -5
- package/src/build/index.ts +0 -5
- package/src/build/production-instrumentation.ts +2 -2
- package/src/build/server-app-entry.ts +60 -0
- package/src/build/shared.ts +3 -1
- package/src/build/ssg-cache.ts +5 -10
- package/src/build/virtual-entry.ts +5 -4
- package/src/cache.ts +12 -0
- package/src/cli/config.ts +3 -3
- package/src/cli/index.ts +5 -2
- package/src/client/internal/runtime-types.ts +10 -1
- package/src/client/sync.ts +1 -0
- package/src/config.ts +3 -2
- package/src/define-route.ts +266 -128
- package/src/furin.ts +138 -149
- package/src/plugin/route-config-autofix.ts +11 -2
- package/src/plugin/routes.ts +85 -29
- package/src/server/assets/production.ts +99 -0
- package/src/server/auto-invalidate/plugin.ts +4 -4
- package/src/server/auto-invalidate/runtime.ts +74 -42
- package/src/server/browser-events/plugin.ts +6 -1
- package/src/server/cache/invalidation.ts +61 -20
- package/src/server/cache/page-cache-state.ts +22 -0
- package/src/server/cache/page-cache.ts +300 -0
- package/src/server/cache/redis/index.ts +350 -0
- package/src/server/cache/redis/scripts.ts +160 -0
- package/src/server/context-logger.ts +5 -5
- package/src/server/dev/runtime.production.ts +4 -0
- package/src/server/dev-page-plugin.ts +54 -10
- package/src/server/evlog.ts +99 -0
- package/src/server/internal.ts +7 -0
- package/src/server/render/isr.ts +480 -132
- package/src/server/render/loaders.ts +18 -12
- package/src/server/render/not-found.ts +4 -4
- package/src/server/render/ppr-document.ts +2 -2
- package/src/server/render/ppr-route.ts +193 -7
- package/src/server/render/ssg.ts +148 -4
- package/src/server/render/ssr.ts +4 -6
- package/src/server/render/static-params.ts +174 -0
- package/src/server/router/defined-route.ts +2 -1
- package/src/server/router/hmr.ts +2 -0
- package/src/server/router/plugin.ts +44 -35
- package/src/server/router/schema-merge.ts +1 -1
- package/src/server/router/schemas.ts +174 -61
- package/src/server/sync/plugin.ts +14 -10
- package/src/server/sync/response.ts +3 -3
- package/src/shared/elysia-contract.ts +23 -25
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { dirname, relative } from "node:path";
|
|
2
|
+
import type { Context } from "elysia";
|
|
3
|
+
import type {
|
|
4
|
+
RuntimeData,
|
|
5
|
+
RuntimeParams,
|
|
6
|
+
RuntimeRoute,
|
|
7
|
+
RuntimeStaticParams,
|
|
8
|
+
} from "../../client/internal/runtime-types.ts";
|
|
9
|
+
import { mapWithConcurrency } from "../../shared/utils/index.ts";
|
|
10
|
+
import { filePathToPattern } from "../router/patterns.ts";
|
|
11
|
+
import type { ResolvedRoute } from "../router/types.ts";
|
|
12
|
+
import { resolvePath } from "./assemble.ts";
|
|
13
|
+
import { runPublicLoaders } from "./loaders.ts";
|
|
14
|
+
|
|
15
|
+
interface StaticParamsStage {
|
|
16
|
+
ancestors: RuntimeRoute[];
|
|
17
|
+
run: RuntimeStaticParams;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const STATIC_PARAMS_CONCURRENCY = 4;
|
|
21
|
+
|
|
22
|
+
function collectStaticParamsStages(route: ResolvedRoute): StaticParamsStage[] {
|
|
23
|
+
const stages: StaticParamsStage[] = [];
|
|
24
|
+
for (const [index, entry] of route.routeChain.entries()) {
|
|
25
|
+
if (entry.staticParams) {
|
|
26
|
+
stages.push({ ancestors: route.routeChain.slice(0, index), run: entry.staticParams });
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (route.page.staticParams) {
|
|
30
|
+
stages.push({ ancestors: route.routeChain, run: route.page.staticParams });
|
|
31
|
+
}
|
|
32
|
+
return stages;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function ancestorPattern(route: ResolvedRoute, ancestors: RuntimeRoute[]): string {
|
|
36
|
+
const rootSourcePath = route.routeChain[0]?.sourcePath;
|
|
37
|
+
const ancestorSourcePath = ancestors.at(-1)?.sourcePath;
|
|
38
|
+
if (rootSourcePath === undefined || ancestorSourcePath === undefined) {
|
|
39
|
+
return route.pattern;
|
|
40
|
+
}
|
|
41
|
+
const pagesDir = dirname(rootSourcePath);
|
|
42
|
+
const ancestorDir = dirname(ancestorSourcePath);
|
|
43
|
+
if (ancestorDir === pagesDir) {
|
|
44
|
+
return "/";
|
|
45
|
+
}
|
|
46
|
+
return filePathToPattern(`${relative(pagesDir, ancestorDir)}/index.tsx`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function createBuildContext(pattern: string, params: RuntimeParams, origin: string): Context {
|
|
50
|
+
const path = resolvePath(pattern, params);
|
|
51
|
+
return {
|
|
52
|
+
cookie: {},
|
|
53
|
+
headers: {},
|
|
54
|
+
params,
|
|
55
|
+
path,
|
|
56
|
+
query: {},
|
|
57
|
+
redirect: (url: string, status: number | undefined) =>
|
|
58
|
+
new Response(null, { headers: { Location: url }, status: status ?? 302 }),
|
|
59
|
+
request: new Request(new URL(path, origin)),
|
|
60
|
+
set: { headers: {} },
|
|
61
|
+
} as Context;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async function runAncestorLoaders(
|
|
65
|
+
route: ResolvedRoute,
|
|
66
|
+
ancestors: RuntimeRoute[],
|
|
67
|
+
params: RuntimeParams,
|
|
68
|
+
origin: string
|
|
69
|
+
): Promise<RuntimeData> {
|
|
70
|
+
const pattern = ancestorPattern(route, ancestors);
|
|
71
|
+
const result = await runPublicLoaders(
|
|
72
|
+
{
|
|
73
|
+
...route,
|
|
74
|
+
page: { ...route.page, loader: undefined },
|
|
75
|
+
pattern,
|
|
76
|
+
routeChain: ancestors,
|
|
77
|
+
},
|
|
78
|
+
createBuildContext(pattern, params, origin)
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
if (result.type === "redirect") {
|
|
82
|
+
throw result.response;
|
|
83
|
+
}
|
|
84
|
+
if (result.type === "not-found") {
|
|
85
|
+
throw result.error;
|
|
86
|
+
}
|
|
87
|
+
if (result.type === "error") {
|
|
88
|
+
throw result.error;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const data: RuntimeData = {};
|
|
92
|
+
for (const [key, value] of Object.entries(result.syncData)) {
|
|
93
|
+
if (key !== "params" && key !== "path" && key !== "query") {
|
|
94
|
+
data[key] = value;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
Object.assign(data, result.deferredPromises);
|
|
98
|
+
return data;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function createStaticParamsContext(
|
|
102
|
+
route: ResolvedRoute,
|
|
103
|
+
ancestors: RuntimeRoute[],
|
|
104
|
+
params: RuntimeParams,
|
|
105
|
+
origin: string
|
|
106
|
+
): RuntimeData & { params: RuntimeParams } {
|
|
107
|
+
const target: RuntimeData & { params: RuntimeParams } = { params };
|
|
108
|
+
const fields = new Map<string, Promise<unknown>>();
|
|
109
|
+
let parentData: Promise<RuntimeData> | undefined;
|
|
110
|
+
|
|
111
|
+
return new Proxy(target, {
|
|
112
|
+
get(context, property: string | symbol) {
|
|
113
|
+
if (typeof property !== "string" || Object.hasOwn(context, property)) {
|
|
114
|
+
return Reflect.get(context, property);
|
|
115
|
+
}
|
|
116
|
+
if (
|
|
117
|
+
property === "then" ||
|
|
118
|
+
property === "catch" ||
|
|
119
|
+
property === "finally" ||
|
|
120
|
+
property === "toJSON"
|
|
121
|
+
) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
let field = fields.get(property);
|
|
125
|
+
if (!field) {
|
|
126
|
+
parentData ??= runAncestorLoaders(route, ancestors, params, origin);
|
|
127
|
+
field = parentData.then((data) => data[property]);
|
|
128
|
+
fields.set(property, field);
|
|
129
|
+
}
|
|
130
|
+
return field;
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function hasStaticParams(route: ResolvedRoute): boolean {
|
|
136
|
+
return (
|
|
137
|
+
route.page.staticParams !== undefined ||
|
|
138
|
+
route.routeChain.some((entry) => entry.staticParams !== undefined)
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export async function resolveStaticParams(
|
|
143
|
+
route: ResolvedRoute,
|
|
144
|
+
origin: string
|
|
145
|
+
): Promise<RuntimeParams[] | undefined> {
|
|
146
|
+
const stages = collectStaticParamsStages(route);
|
|
147
|
+
if (stages.length === 0) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
let branches: RuntimeParams[] = [{}];
|
|
152
|
+
for (const stage of stages) {
|
|
153
|
+
// Stages are dependent (parent params feed the next stage), while sibling
|
|
154
|
+
// branches are independent and can run through a bounded worker pool.
|
|
155
|
+
// biome-ignore lint/performance/noAwaitInLoops: each stage consumes the previous stage's params
|
|
156
|
+
const branchValues = await mapWithConcurrency(
|
|
157
|
+
branches,
|
|
158
|
+
STATIC_PARAMS_CONCURRENCY,
|
|
159
|
+
async (params) => {
|
|
160
|
+
const values = await stage.run(
|
|
161
|
+
createStaticParamsContext(route, stage.ancestors, params, origin)
|
|
162
|
+
);
|
|
163
|
+
if (!Array.isArray(values)) {
|
|
164
|
+
throw new TypeError(
|
|
165
|
+
`[furin] staticParams() for "${route.pattern}" must return an array.`
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
return values.map((value) => ({ ...params, ...value }));
|
|
169
|
+
}
|
|
170
|
+
);
|
|
171
|
+
branches = branchValues.flat();
|
|
172
|
+
}
|
|
173
|
+
return branches;
|
|
174
|
+
}
|
|
@@ -13,7 +13,7 @@ interface DefinedRouteTerminal {
|
|
|
13
13
|
requestLoader?: (...args: never[]) => unknown;
|
|
14
14
|
revalidate?: number;
|
|
15
15
|
schemas?: { params?: unknown; query?: unknown };
|
|
16
|
-
staticParams?: () => Promise<readonly unknown[]> | readonly unknown[];
|
|
16
|
+
staticParams?: (context: never) => Promise<readonly unknown[]> | readonly unknown[];
|
|
17
17
|
tags?: readonly string[];
|
|
18
18
|
}
|
|
19
19
|
|
|
@@ -51,6 +51,7 @@ export function adaptDefinedLayout(
|
|
|
51
51
|
requestLoader: route.requestLoader as RuntimeRoute["requestLoader"],
|
|
52
52
|
revalidate: route.revalidate,
|
|
53
53
|
sourcePath,
|
|
54
|
+
staticParams: route.staticParams as RuntimeRoute["staticParams"],
|
|
54
55
|
tags: copyTags(route.tags),
|
|
55
56
|
};
|
|
56
57
|
}
|
package/src/server/router/hmr.ts
CHANGED
|
@@ -185,6 +185,7 @@ function patchRouteEntryFromFreshModule(
|
|
|
185
185
|
entry.query = adapted.query;
|
|
186
186
|
entry.requestLoader = adapted.requestLoader;
|
|
187
187
|
entry.revalidate = adapted.revalidate;
|
|
188
|
+
entry.staticParams = adapted.staticParams;
|
|
188
189
|
entry.tags = adapted.tags;
|
|
189
190
|
}
|
|
190
191
|
}
|
|
@@ -320,6 +321,7 @@ export async function handleDevRequest(
|
|
|
320
321
|
if (chain[0] && currentRoot.route) {
|
|
321
322
|
chain[0].layout = currentRoot.route.layout;
|
|
322
323
|
chain[0].loader = currentRoot.route.loader;
|
|
324
|
+
chain[0].staticParams = currentRoot.route.staticParams;
|
|
323
325
|
}
|
|
324
326
|
|
|
325
327
|
const refreshedRoute = rebuildDevRoute(route, page, chain);
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { type AnyElysia, type Context, Elysia, t } from "elysia";
|
|
1
|
+
import { type AnyElysia, type Context, Elysia, problem, t } from "elysia";
|
|
2
2
|
import { toCrossJSONAsync } from "seroval";
|
|
3
3
|
import type { HeadOptions } from "../../client.ts";
|
|
4
4
|
import { computeErrorDigest } from "../../shared/digest.ts";
|
|
5
5
|
import type { FurinSchema } from "../../shared/elysia-contract.ts";
|
|
6
6
|
import { containsRscSource } from "../../shared/route-frame.ts";
|
|
7
7
|
import type { SearchParamsInput, SearchRouteMetadata } from "../../shared/search-params.ts";
|
|
8
|
-
import {
|
|
8
|
+
import { getLogger } from "../context-logger.ts";
|
|
9
9
|
import {
|
|
10
10
|
currentInstrumentationRequest,
|
|
11
11
|
emitPayloadSerialized,
|
|
@@ -24,7 +24,7 @@ import {
|
|
|
24
24
|
import { renderPprRoute, runPprPublicLoaders } from "../render/ppr-route.ts";
|
|
25
25
|
import { createDeferredRouteFrameStream } from "../render/route-frame-transport.ts";
|
|
26
26
|
import { extractTitle } from "../render/shell.ts";
|
|
27
|
-
import { prerenderRoute,
|
|
27
|
+
import { prerenderRoute, prerenderRuntimeSSG } from "../render/ssg.ts";
|
|
28
28
|
import { renderSSR, serializeLoaderDataNdjson } from "../render/ssr.ts";
|
|
29
29
|
import { IS_DEV } from "../runtime-env.ts";
|
|
30
30
|
import { handleDevRequest } from "./hmr.ts";
|
|
@@ -217,9 +217,23 @@ async function handleSSGRequest(
|
|
|
217
217
|
): Promise<unknown> {
|
|
218
218
|
const { origin } = new URL(ctx.request.url);
|
|
219
219
|
const params = ctx.params ?? {};
|
|
220
|
-
const
|
|
221
|
-
|
|
222
|
-
|
|
220
|
+
const externalPrerender = isExternalPrerenderRequest(ctx.request);
|
|
221
|
+
const runtimeResult = externalPrerender
|
|
222
|
+
? {
|
|
223
|
+
cacheStored: true,
|
|
224
|
+
entry: await prerenderRoute(
|
|
225
|
+
route,
|
|
226
|
+
params,
|
|
227
|
+
root,
|
|
228
|
+
origin,
|
|
229
|
+
"ssg",
|
|
230
|
+
undefined,
|
|
231
|
+
searchRoutes,
|
|
232
|
+
ctx
|
|
233
|
+
),
|
|
234
|
+
}
|
|
235
|
+
: await prerenderRuntimeSSG(route, params, root, origin, buildId, searchRoutes);
|
|
236
|
+
const { cacheStored, entry } = runtimeResult;
|
|
223
237
|
|
|
224
238
|
// Loader issued a redirect — forward it directly to the client.
|
|
225
239
|
if (entry instanceof Response) {
|
|
@@ -238,7 +252,9 @@ async function handleSSGRequest(
|
|
|
238
252
|
ctx.set.headers["content-type"] = "text/html; charset=utf-8";
|
|
239
253
|
// Browser: max-age=0 + must-revalidate → always validates via ETag (304 = free)
|
|
240
254
|
// CDN: s-maxage=31536000 → cache for 1 year, purge via revalidatePath + purger
|
|
241
|
-
ctx.set.headers["cache-control"] =
|
|
255
|
+
ctx.set.headers["cache-control"] = cacheStored
|
|
256
|
+
? "public, max-age=0, must-revalidate, s-maxage=31536000"
|
|
257
|
+
: "no-store";
|
|
242
258
|
if (etag) {
|
|
243
259
|
ctx.set.headers.etag = etag;
|
|
244
260
|
}
|
|
@@ -255,21 +271,20 @@ export function createRoutePlugin(
|
|
|
255
271
|
const resolvedBuildId = buildId ?? "";
|
|
256
272
|
const { pattern, routeChain } = route;
|
|
257
273
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
query: allQuery as FurinSchema,
|
|
274
|
+
// Keep every route-chain schema in Elysia's native merge pipeline so its
|
|
275
|
+
// coercion and default semantics remain authoritative for document requests.
|
|
276
|
+
const plugin = routeChain.reduce<AnyElysia>((app, entry) => {
|
|
277
|
+
if (!(entry.params || entry.query)) {
|
|
278
|
+
return app;
|
|
279
|
+
}
|
|
280
|
+
return app.guard({
|
|
281
|
+
params: entry.params as FurinSchema,
|
|
282
|
+
query: entry.query as FurinSchema,
|
|
283
|
+
schema: "merge",
|
|
269
284
|
});
|
|
270
|
-
}
|
|
285
|
+
}, new Elysia());
|
|
271
286
|
|
|
272
|
-
plugin.get(pattern, (ctx) =>
|
|
287
|
+
plugin.get(pattern, (ctx: Context) =>
|
|
273
288
|
renderResolvedRoute(route, ctx, root, resolvedBuildId, searchRoutes)
|
|
274
289
|
);
|
|
275
290
|
|
|
@@ -330,15 +345,18 @@ export function createDataEndpoint(
|
|
|
330
345
|
|
|
331
346
|
plugin.get(
|
|
332
347
|
"/_furin/data",
|
|
348
|
+
{
|
|
349
|
+
query: t.Object({ path: t.Optional(t.String()) }),
|
|
350
|
+
},
|
|
333
351
|
async (ctx) => {
|
|
334
352
|
const rawPath = ctx.query.path;
|
|
335
353
|
if (!rawPath || typeof rawPath !== "string") {
|
|
336
|
-
return
|
|
354
|
+
return problem("Bad Request", { detail: "Missing required query param: path" });
|
|
337
355
|
}
|
|
338
356
|
|
|
339
357
|
const parsed = parseDataEndpointPath(rawPath);
|
|
340
358
|
if (!parsed) {
|
|
341
|
-
return
|
|
359
|
+
return problem("Bad Request", { detail: "Invalid path" });
|
|
342
360
|
}
|
|
343
361
|
const { url, pathname } = parsed;
|
|
344
362
|
|
|
@@ -347,7 +365,7 @@ export function createDataEndpoint(
|
|
|
347
365
|
// instead of the technical "/_furin/data" transport URL. We set this
|
|
348
366
|
// before the route-match check so 404s also surface the attempted path
|
|
349
367
|
// — otherwise monitoring just sees "GET /_furin/data 404" with no clue.
|
|
350
|
-
const wideEventLog =
|
|
368
|
+
const wideEventLog = getLogger();
|
|
351
369
|
wideEventLog.set({ path: rawPath });
|
|
352
370
|
|
|
353
371
|
let currentRoutes: ResolvedRoute[];
|
|
@@ -366,7 +384,7 @@ export function createDataEndpoint(
|
|
|
366
384
|
const matched = matchRoute(pathname);
|
|
367
385
|
|
|
368
386
|
if (!matched) {
|
|
369
|
-
return
|
|
387
|
+
return problem("Not Found", { detail: "Route not found" });
|
|
370
388
|
}
|
|
371
389
|
|
|
372
390
|
// Now that we know the matched pattern, add it as a stable aggregation
|
|
@@ -410,17 +428,11 @@ export function createDataEndpoint(
|
|
|
410
428
|
const mergedQuery = mergeRouteSchemas(matched.route.routeChain, "query");
|
|
411
429
|
const parsedParams = await parseRouteParams(matched.params, mergedParams);
|
|
412
430
|
if (!parsedParams.ok) {
|
|
413
|
-
return
|
|
414
|
-
{ errors: parsedParams.errors, message: "Invalid params", type: "validation" },
|
|
415
|
-
{ status: 422 }
|
|
416
|
-
);
|
|
431
|
+
return problem(422, { detail: "Invalid params", errors: parsedParams.errors });
|
|
417
432
|
}
|
|
418
433
|
const parsedQuery = await parseRouteQuery(url, mergedQuery);
|
|
419
434
|
if (!parsedQuery.ok) {
|
|
420
|
-
return
|
|
421
|
-
{ errors: parsedQuery.errors, message: "Invalid query", type: "validation" },
|
|
422
|
-
{ status: 422 }
|
|
423
|
-
);
|
|
435
|
+
return problem(422, { detail: "Invalid query", errors: parsedQuery.errors });
|
|
424
436
|
}
|
|
425
437
|
syntheticCtx.params = parsedParams.params;
|
|
426
438
|
syntheticCtx.query = parsedQuery.query as SearchParamsInput;
|
|
@@ -433,9 +445,6 @@ export function createDataEndpoint(
|
|
|
433
445
|
);
|
|
434
446
|
|
|
435
447
|
return createLoaderDataResponse(result, matched.route, syntheticRequest.url, syntheticCtx);
|
|
436
|
-
},
|
|
437
|
-
{
|
|
438
|
-
query: t.Object({ path: t.Optional(t.String()) }),
|
|
439
448
|
}
|
|
440
449
|
);
|
|
441
450
|
|
|
@@ -21,7 +21,7 @@ export function mergeRouteSchemas(
|
|
|
21
21
|
}
|
|
22
22
|
if (schemas.some((schema) => !isTypeBoxObjectSchema(schema))) {
|
|
23
23
|
throw new Error(
|
|
24
|
-
`[furin] Merging ${key} schemas across the route chain requires TypeBox
|
|
24
|
+
`[furin] Merging ${key} schemas across the route chain requires TypeBox object schemas. Use TypeBox for parent/child ${key}, or define ${key} only on leaf routes.`
|
|
25
25
|
);
|
|
26
26
|
}
|
|
27
27
|
|
|
@@ -1,10 +1,5 @@
|
|
|
1
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";
|
|
2
|
+
import { type FurinSchema, getSchemaValidator } from "../../shared/elysia-contract.ts";
|
|
8
3
|
import {
|
|
9
4
|
collectSearchDefaults,
|
|
10
5
|
type SearchParamsInput,
|
|
@@ -22,6 +17,59 @@ interface QueryKeyMap {
|
|
|
22
17
|
[key: string]: 1;
|
|
23
18
|
}
|
|
24
19
|
|
|
20
|
+
function decodeQueryPart(value: string): string {
|
|
21
|
+
return new URLSearchParams(`value=${value}`).get("value") ?? "";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function parseArrayQueryValue(raw: string, decoded: string, repeated: boolean): string[] {
|
|
25
|
+
const rawBracketed = raw.startsWith("[") && raw.endsWith("]");
|
|
26
|
+
const decodedBracketed = decoded.startsWith("[") && decoded.endsWith("]");
|
|
27
|
+
if (repeated && !rawBracketed && !decodedBracketed) {
|
|
28
|
+
return [decoded];
|
|
29
|
+
}
|
|
30
|
+
if (decoded === "[]") {
|
|
31
|
+
return [];
|
|
32
|
+
}
|
|
33
|
+
if (rawBracketed) {
|
|
34
|
+
return raw.slice(1, -1).split(",").map(decodeQueryPart);
|
|
35
|
+
}
|
|
36
|
+
if (decodedBracketed) {
|
|
37
|
+
return decoded.slice(1, -1).split(",");
|
|
38
|
+
}
|
|
39
|
+
return raw.includes(",") ? raw.split(",").map(decodeQueryPart) : [decoded];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function parseQueryFromURL(search: string, arrayKeys?: QueryKeyMap): UnknownObject {
|
|
43
|
+
const query: UnknownObject = Object.create(null);
|
|
44
|
+
if (!arrayKeys) {
|
|
45
|
+
for (const [key, value] of new URLSearchParams(search)) {
|
|
46
|
+
query[key] = value;
|
|
47
|
+
}
|
|
48
|
+
return query;
|
|
49
|
+
}
|
|
50
|
+
for (const pair of search.slice(1).split("&")) {
|
|
51
|
+
const entry = new URLSearchParams(pair).entries().next().value;
|
|
52
|
+
if (!entry) {
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
const [key, value] = entry;
|
|
56
|
+
if (!arrayKeys[key]) {
|
|
57
|
+
query[key] = value;
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
const equalIndex = pair.indexOf("=");
|
|
61
|
+
const rawValue = equalIndex === -1 ? "" : pair.slice(equalIndex + 1);
|
|
62
|
+
const previous = query[key];
|
|
63
|
+
const values = parseArrayQueryValue(rawValue, value, Array.isArray(previous));
|
|
64
|
+
if (Array.isArray(previous)) {
|
|
65
|
+
previous.push(...values);
|
|
66
|
+
} else {
|
|
67
|
+
query[key] = values;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return query;
|
|
71
|
+
}
|
|
72
|
+
|
|
25
73
|
/**
|
|
26
74
|
* Parses the `?path=` argument of `/_furin/data`, rejecting absolute /
|
|
27
75
|
* protocol-relative inputs that would let a caller smuggle a foreign origin
|
|
@@ -60,50 +108,54 @@ function isStandardSchema(schema: unknown): boolean {
|
|
|
60
108
|
}
|
|
61
109
|
|
|
62
110
|
function collectQueryArrayKeys(schema: unknown): QueryKeyMap | undefined {
|
|
63
|
-
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const keys: QueryKeyMap = {};
|
|
68
|
-
for (const [key, value] of Object.entries(schema.properties)) {
|
|
69
|
-
const effectiveSchema = findEffectiveAnyOfMember(value, "array") ?? value;
|
|
70
|
-
if (isObjectSchema(effectiveSchema) && effectiveSchema.type === "array") {
|
|
71
|
-
keys[key] = 1;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return Object.keys(keys).length > 0 ? keys : undefined;
|
|
111
|
+
return collectQueryKeys(schema, "array");
|
|
76
112
|
}
|
|
77
113
|
|
|
78
114
|
function collectQueryObjectKeys(schema: unknown): QueryKeyMap | undefined {
|
|
79
|
-
|
|
115
|
+
return collectQueryKeys(schema, "object");
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function collectQueryKeys(schema: unknown, type: "array" | "object"): QueryKeyMap | undefined {
|
|
119
|
+
if (!isObjectSchema(schema)) {
|
|
80
120
|
return;
|
|
81
121
|
}
|
|
82
122
|
|
|
83
123
|
const keys: QueryKeyMap = {};
|
|
84
|
-
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
124
|
+
if (isObjectSchema(schema.properties)) {
|
|
125
|
+
for (const [key, value] of Object.entries(schema.properties)) {
|
|
126
|
+
if (hasSchemaType(value, type)) {
|
|
127
|
+
keys[key] = 1;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
for (const keyword of ["allOf", "anyOf"] as const) {
|
|
133
|
+
const members = schema[keyword];
|
|
134
|
+
if (!Array.isArray(members)) {
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
for (const member of members) {
|
|
138
|
+
Object.assign(keys, collectQueryKeys(member, type));
|
|
88
139
|
}
|
|
89
140
|
}
|
|
90
141
|
|
|
91
142
|
return Object.keys(keys).length > 0 ? keys : undefined;
|
|
92
143
|
}
|
|
93
144
|
|
|
94
|
-
function
|
|
95
|
-
schema
|
|
96
|
-
|
|
97
|
-
): UnknownObject | undefined {
|
|
98
|
-
if (!(isObjectSchema(schema) && Array.isArray(schema.anyOf))) {
|
|
99
|
-
return;
|
|
145
|
+
function hasSchemaType(schema: unknown, type: "array" | "object"): boolean {
|
|
146
|
+
if (!isObjectSchema(schema)) {
|
|
147
|
+
return false;
|
|
100
148
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
149
|
+
if (schema.type === type) {
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
for (const keyword of ["allOf", "anyOf"] as const) {
|
|
153
|
+
const members = schema[keyword];
|
|
154
|
+
if (Array.isArray(members) && members.some((member) => hasSchemaType(member, type))) {
|
|
155
|
+
return true;
|
|
105
156
|
}
|
|
106
157
|
}
|
|
158
|
+
return false;
|
|
107
159
|
}
|
|
108
160
|
|
|
109
161
|
function parseJsonQueryObjects(
|
|
@@ -129,6 +181,72 @@ function parseJsonQueryObjects(
|
|
|
129
181
|
return parsed;
|
|
130
182
|
}
|
|
131
183
|
|
|
184
|
+
function coerceUnionValue(members: unknown[], value: unknown): unknown {
|
|
185
|
+
for (const member of members) {
|
|
186
|
+
const candidate = coerceSchemaValue(member, value);
|
|
187
|
+
const validator = getSchemaValidator(member as FurinSchema);
|
|
188
|
+
if (validator?.Check(candidate)) {
|
|
189
|
+
return candidate;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return value;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function coerceObjectValue(properties: UnknownObject, value: UnknownObject): UnknownObject {
|
|
196
|
+
const coerced = { ...value };
|
|
197
|
+
for (const [key, propertySchema] of Object.entries(properties)) {
|
|
198
|
+
if (key in coerced) {
|
|
199
|
+
coerced[key] = coerceSchemaValue(propertySchema, coerced[key]);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return coerced;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function coerceStringValue(schema: UnknownObject, value: string): unknown {
|
|
206
|
+
if (value.length === 0) {
|
|
207
|
+
return value;
|
|
208
|
+
}
|
|
209
|
+
if (schema.type === "boolean") {
|
|
210
|
+
if (value === "true") {
|
|
211
|
+
return true;
|
|
212
|
+
}
|
|
213
|
+
if (value === "false") {
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
216
|
+
return value;
|
|
217
|
+
}
|
|
218
|
+
if (schema.type === "number" || schema.type === "integer") {
|
|
219
|
+
const number = Number(value);
|
|
220
|
+
if (Number.isFinite(number) && (schema.type !== "integer" || Number.isInteger(number))) {
|
|
221
|
+
return number;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return value;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function coerceSchemaValue(schema: unknown, value: unknown): unknown {
|
|
228
|
+
if (!isObjectSchema(schema)) {
|
|
229
|
+
return value;
|
|
230
|
+
}
|
|
231
|
+
if (Array.isArray(schema.anyOf)) {
|
|
232
|
+
return coerceUnionValue(schema.anyOf, value);
|
|
233
|
+
}
|
|
234
|
+
if (Array.isArray(schema.allOf)) {
|
|
235
|
+
return schema.allOf.reduce((candidate, member) => coerceSchemaValue(member, candidate), value);
|
|
236
|
+
}
|
|
237
|
+
if (schema.type === "object" && isObjectSchema(schema.properties) && isObjectSchema(value)) {
|
|
238
|
+
return coerceObjectValue(schema.properties, value);
|
|
239
|
+
}
|
|
240
|
+
if (schema.type === "array" && Array.isArray(value)) {
|
|
241
|
+
return value.map((entry) => coerceSchemaValue(schema.items, entry));
|
|
242
|
+
}
|
|
243
|
+
return typeof value === "string" ? coerceStringValue(schema, value) : value;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export function coerceRouteInput(schema: FurinSchema, value: UnknownObject): UnknownObject {
|
|
247
|
+
return coerceSchemaValue(schema, value) as UnknownObject;
|
|
248
|
+
}
|
|
249
|
+
|
|
132
250
|
export type ParseRouteQueryResult =
|
|
133
251
|
| { ok: true; query: SearchParamsInput }
|
|
134
252
|
| { errors: unknown; ok: false };
|
|
@@ -143,30 +261,25 @@ type RouteInputValidationResult =
|
|
|
143
261
|
|
|
144
262
|
async function validateRouteInput(
|
|
145
263
|
input: UnknownObject,
|
|
146
|
-
schema: FurinSchema
|
|
264
|
+
schema: FurinSchema,
|
|
265
|
+
type: "params" | "query"
|
|
147
266
|
): Promise<RouteInputValidationResult> {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
return {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
return { errors: [...(validator?.Errors(inputWithDefaults) ?? [])], ok: false };
|
|
267
|
+
const inputWithDefaults = isStandardSchema(schema)
|
|
268
|
+
? input
|
|
269
|
+
: applySchemaDefaults(schema as UnknownObject, input);
|
|
270
|
+
const valueToValidate = isStandardSchema(schema)
|
|
271
|
+
? inputWithDefaults
|
|
272
|
+
: coerceRouteInput(schema, inputWithDefaults);
|
|
273
|
+
const validator = getSchemaValidator(schema);
|
|
274
|
+
try {
|
|
275
|
+
const parsed = await validator?.parse(valueToValidate, type);
|
|
276
|
+
return {
|
|
277
|
+
ok: true,
|
|
278
|
+
value: (parsed ?? valueToValidate) as UnknownObject,
|
|
279
|
+
};
|
|
280
|
+
} catch {
|
|
281
|
+
return { errors: [...(validator?.Errors(valueToValidate) ?? [])], ok: false };
|
|
164
282
|
}
|
|
165
|
-
|
|
166
|
-
return {
|
|
167
|
-
ok: true,
|
|
168
|
-
value: (validator?.parse(inputWithDefaults) ?? inputWithDefaults) as UnknownObject,
|
|
169
|
-
};
|
|
170
283
|
}
|
|
171
284
|
|
|
172
285
|
/**
|
|
@@ -181,16 +294,16 @@ export async function parseRouteQuery(
|
|
|
181
294
|
schema: FurinSchema | undefined
|
|
182
295
|
): Promise<ParseRouteQueryResult> {
|
|
183
296
|
if (!schema) {
|
|
184
|
-
return { ok: true, query: parseQueryFromURL(url.search
|
|
297
|
+
return { ok: true, query: parseQueryFromURL(url.search) as SearchParamsInput };
|
|
185
298
|
}
|
|
186
299
|
|
|
187
300
|
const rawQuery = isStandardSchema(schema)
|
|
188
|
-
? (
|
|
301
|
+
? parseQueryFromURL(url.search)
|
|
189
302
|
: parseJsonQueryObjects(
|
|
190
|
-
parseQueryFromURL(url.search,
|
|
303
|
+
parseQueryFromURL(url.search, collectQueryArrayKeys(schema)),
|
|
191
304
|
collectQueryObjectKeys(schema)
|
|
192
305
|
);
|
|
193
|
-
const result = await validateRouteInput(rawQuery, schema);
|
|
306
|
+
const result = await validateRouteInput(rawQuery, schema, "query");
|
|
194
307
|
return result.ok ? { ok: true, query: result.value as SearchParamsInput } : result;
|
|
195
308
|
}
|
|
196
309
|
|
|
@@ -209,7 +322,7 @@ export async function parseRouteParams(
|
|
|
209
322
|
return { ok: true, params };
|
|
210
323
|
}
|
|
211
324
|
|
|
212
|
-
const result = await validateRouteInput(params, schema);
|
|
325
|
+
const result = await validateRouteInput(params, schema, "params");
|
|
213
326
|
return result.ok ? { ok: true, params: result.value } : result;
|
|
214
327
|
}
|
|
215
328
|
|