@teyik0/furin 0.9.0-alpha.0 → 0.9.0-alpha.2
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 +1 -1
- package/dist/adapter/runtime-build.d.ts +7 -3
- package/dist/adapter/vercel.js +31 -6
- package/dist/build/client.d.ts +1 -1
- package/dist/build/entry-template.d.ts +3 -12
- package/dist/build/entry-template.ts +3 -15
- package/dist/build/hydrate.d.ts +6 -1
- package/dist/build/index.d.ts +1 -1
- package/dist/build/index.js +565 -401
- package/dist/build/route-metadata.d.ts +2 -2
- package/dist/build/route-types.d.ts +1 -1
- package/dist/build/types.d.ts +10 -4
- package/dist/cli/dev-worker.ts +10 -4
- package/dist/cli/index.js +624 -445
- package/dist/client/chunks/{client-xy6g38kx.js → client-a7spxfnn.js} +24 -5
- package/dist/client/chunks/{client-rjhxgt64.js → client-d9v49jwz.js} +1 -1
- package/dist/client/chunks/{client-k29262a4.js → client-rdxyrw11.js} +1 -1
- package/dist/client/define-route.d.ts +14 -0
- package/dist/client/internal/runtime-types.d.ts +8 -0
- package/dist/client/link.js +20 -17
- package/dist/client/router/link-utils.d.ts +3 -2
- package/dist/client/router/search/index.js +2 -2
- package/dist/client/router/types.d.ts +9 -3
- package/dist/client.js +22 -6
- package/dist/config.d.ts +4 -0
- package/dist/config.js +4 -1
- package/dist/define-route.d.ts +53 -12
- package/dist/furin.d.ts +13 -3
- package/dist/furin.js +815 -661
- package/dist/plugin/route-config.d.ts +3 -3
- package/dist/plugin/route-manifest.d.ts +46 -0
- package/dist/plugin/route-tree.d.ts +13 -29
- package/dist/routes.d.ts +2 -2
- package/dist/server/dev/runtime.d.ts +2 -1
- package/dist/server/internal.d.ts +6 -0
- package/dist/server/internal.js +97 -114
- package/dist/server/registrations.d.ts +4 -2
- package/dist/server/render/index.d.ts +0 -1
- package/dist/server/render/index.ts +0 -1
- package/dist/server/render/shell.d.ts +1 -13
- package/dist/server/render/shell.ts +1 -90
- package/dist/server/router/defined-route.d.ts +4 -0
- package/dist/server/router/discovery.d.ts +14 -4
- package/dist/server/router/hmr.d.ts +12 -12
- package/dist/server/router/patterns.d.ts +4 -12
- package/dist/server/router/types.d.ts +16 -1
- package/dist/server/static-export.js +128 -158
- package/dist/server/sync/drizzle/index.d.ts +8 -10
- package/dist/shared/not-found.d.ts +1 -1
- package/dist/shared/route-specificity.d.ts +10 -1
- package/package.json +8 -11
- package/src/adapter/package.ts +11 -37
- package/src/adapter/runtime-build.ts +5 -4
- package/src/adapter/static.ts +1 -1
- package/src/adapter/vercel.ts +2 -1
- package/src/build/client.ts +3 -3
- package/src/build/discover-pages-process.ts +1 -1
- package/src/build/elysia-aot.ts +2 -2
- package/src/build/entry-template.ts +3 -15
- package/src/build/hydrate.ts +12 -6
- package/src/build/index.ts +19 -9
- package/src/build/route-metadata.ts +15 -8
- package/src/build/route-types.ts +18 -43
- package/src/build/types.ts +12 -5
- package/src/cli/config.ts +6 -0
- package/src/cli/dev-worker.ts +10 -4
- package/src/cli/dev.ts +41 -26
- package/src/cli/index.ts +30 -14
- package/src/client/define-route.ts +29 -4
- package/src/client/internal/runtime-types.ts +8 -0
- package/src/client/link.tsx +21 -22
- package/src/client/router/context.ts +8 -7
- package/src/client/router/link-utils.ts +16 -8
- package/src/client/router/provider.tsx +5 -0
- package/src/client/router/types.ts +40 -14
- package/src/config.ts +5 -0
- package/src/define-route.ts +85 -16
- package/src/furin.ts +31 -7
- package/src/plugin/dev-routes.ts +8 -17
- package/src/plugin/route-config.ts +77 -62
- package/src/plugin/route-manifest.ts +437 -0
- package/src/plugin/route-modules.ts +6 -1
- package/src/plugin/route-registry.ts +7 -21
- package/src/plugin/route-tree.ts +71 -181
- package/src/routes.ts +2 -2
- package/src/server/dev/runtime.ts +16 -22
- package/src/server/internal.ts +4 -1
- package/src/server/registrations.ts +5 -2
- package/src/server/render/index.ts +0 -1
- package/src/server/render/shell.ts +1 -90
- package/src/server/render/static-params.ts +2 -10
- package/src/server/router/defined-route.ts +8 -0
- package/src/server/router/discovery.ts +237 -196
- package/src/server/router/hmr.ts +45 -63
- package/src/server/router/patterns.ts +41 -117
- package/src/server/router/types.ts +17 -1
- package/src/server/sync/drizzle/index.ts +18 -19
- package/src/shared/not-found.ts +1 -1
- package/src/shared/route-specificity.ts +34 -2
- package/src/shared/utils/index.ts +1 -1
package/src/server/router/hmr.ts
CHANGED
|
@@ -32,12 +32,11 @@ import { adaptDefinedLayout, adaptDefinedPage, isDefinedRouteTerminal } from "./
|
|
|
32
32
|
import {
|
|
33
33
|
annotateScannedRequestKeys,
|
|
34
34
|
collectRouteTags,
|
|
35
|
-
getSourceModuleCandidates,
|
|
36
35
|
invalidNestedLayoutError,
|
|
37
36
|
isModuleNotFoundError,
|
|
38
37
|
} from "./discovery.ts";
|
|
39
38
|
import type { CurrentRoute, PagePorts } from "./dispatch.ts";
|
|
40
|
-
import {
|
|
39
|
+
import { resolveMode, resolveRouteRevalidate } from "./patterns.ts";
|
|
41
40
|
import { createRouteDataErrorResponse, isPprRoute } from "./plugin.ts";
|
|
42
41
|
import { invalidateRouteModuleSourceVersions, routeModuleSourceVersion } from "./source-version.ts";
|
|
43
42
|
import type { ResolvedRoute, RootLayout } from "./types.ts";
|
|
@@ -203,21 +202,19 @@ async function importFreshRouteModuleCandidate(
|
|
|
203
202
|
}
|
|
204
203
|
|
|
205
204
|
async function importFreshLayoutRouteModule(
|
|
206
|
-
|
|
205
|
+
layoutPath: string,
|
|
207
206
|
resolveImport: RouteModuleImport,
|
|
208
207
|
ctx: CompileContext | null
|
|
209
208
|
): Promise<CompiledModule | undefined> {
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
continue;
|
|
214
|
-
}
|
|
215
|
-
// Same contract as an invalid page export: the overlay names the file.
|
|
216
|
-
if (!(isDefinedRouteTerminal(freshMod.route) && typeof freshMod.route.layout === "function")) {
|
|
217
|
-
throw invalidExportFailure(invalidNestedLayoutError(layoutPath), layoutPath);
|
|
218
|
-
}
|
|
219
|
-
return freshMod;
|
|
209
|
+
const freshMod = await importFreshRouteModuleCandidate(layoutPath, resolveImport, ctx);
|
|
210
|
+
if (!freshMod) {
|
|
211
|
+
return;
|
|
220
212
|
}
|
|
213
|
+
// Same contract as an invalid page export: the overlay names the file.
|
|
214
|
+
if (!(isDefinedRouteTerminal(freshMod.route) && typeof freshMod.route.layout === "function")) {
|
|
215
|
+
throw invalidExportFailure(invalidNestedLayoutError(layoutPath), layoutPath);
|
|
216
|
+
}
|
|
217
|
+
return freshMod;
|
|
221
218
|
}
|
|
222
219
|
|
|
223
220
|
function invalidExportFailure(error: Error, file: string): DevTransformFailure {
|
|
@@ -236,9 +233,11 @@ function patchRouteEntryFromFreshModule(
|
|
|
236
233
|
|
|
237
234
|
if (isDefinedRouteTerminal(freshRoute) && typeof freshRoute.layout === "function") {
|
|
238
235
|
const adapted = adaptDefinedLayout(freshRoute, entry.parent);
|
|
236
|
+
entry.errorComponent = adapted.errorComponent;
|
|
239
237
|
entry.layout = adapted.layout;
|
|
240
238
|
entry.loader = adapted.loader;
|
|
241
239
|
entry.mode = adapted.mode;
|
|
240
|
+
entry.notFoundComponent = adapted.notFoundComponent;
|
|
242
241
|
entry.params = adapted.params;
|
|
243
242
|
entry.query = adapted.query;
|
|
244
243
|
entry.requestLoader = adapted.requestLoader;
|
|
@@ -249,50 +248,29 @@ function patchRouteEntryFromFreshModule(
|
|
|
249
248
|
}
|
|
250
249
|
|
|
251
250
|
/**
|
|
252
|
-
* Re-imports
|
|
253
|
-
* module identity, then patches the
|
|
254
|
-
* route-chain objects.
|
|
251
|
+
* Re-imports the page's layouts (`layoutPaths`, shallow → deep, as the route
|
|
252
|
+
* manifest lists them) with a versioned module identity, then patches the
|
|
253
|
+
* layout/loader references on the existing route-chain objects. `chain[0]`
|
|
254
|
+
* is the root, so layout `i` patches `chain[i + 1]`.
|
|
255
255
|
*
|
|
256
256
|
* The optional `importFn` parameter exists only for unit testing. In production
|
|
257
257
|
* it defaults to the real `import()`.
|
|
258
258
|
*/
|
|
259
259
|
export async function refreshLayoutChain(
|
|
260
260
|
chain: RuntimeRoute[],
|
|
261
|
-
|
|
262
|
-
rootPath: string,
|
|
261
|
+
layoutPaths: readonly string[],
|
|
263
262
|
importFn: ((specifier: string) => Promise<CompiledModule>) | undefined
|
|
264
263
|
): Promise<void> {
|
|
265
264
|
const resolveImport = importFn ?? routeModuleImport;
|
|
266
265
|
const ctx = getCompileContext();
|
|
267
|
-
const layoutDirs = collectIntermediateLayoutDirs(pagePath, rootPath);
|
|
268
|
-
|
|
269
|
-
// Track chainIdx independently rather than deriving it from layoutPaths
|
|
270
|
-
// index. Directories without a _route module produce import errors that are
|
|
271
|
-
// silently skipped, but those directories have no corresponding chain entry —
|
|
272
|
-
// so we must only advance chainIdx for directories whose _route module actually
|
|
273
|
-
// exists. A positional assumption (i = chainIdx - 1) drifts whenever
|
|
274
|
-
// isModuleNotFoundError is swallowed for a gap directory.
|
|
275
|
-
//
|
|
276
|
-
// Imports are parallelised for speed; patching stays sequential so the
|
|
277
|
-
// chainIdx-to-layoutDir positional mapping remains deterministic.
|
|
278
266
|
const freshMods = await Promise.all(
|
|
279
|
-
|
|
267
|
+
layoutPaths.map((layoutPath) => importFreshLayoutRouteModule(layoutPath, resolveImport, ctx))
|
|
280
268
|
);
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
if (
|
|
284
|
-
|
|
269
|
+
for (const [index, freshMod] of freshMods.entries()) {
|
|
270
|
+
// A layout deleted since the scan keeps its entry until the topology rescan.
|
|
271
|
+
if (freshMod) {
|
|
272
|
+
patchRouteEntryFromFreshModule(chain[index + 1], freshMod);
|
|
285
273
|
}
|
|
286
|
-
if (!freshMod) {
|
|
287
|
-
// No _route module in this directory — no chain entry to match, so
|
|
288
|
-
// do NOT advance chainIdx. The next deeper layoutDir may correspond
|
|
289
|
-
// to the current chainIdx.
|
|
290
|
-
continue;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
patchRouteEntryFromFreshModule(chain[chainIdx], freshMod);
|
|
294
|
-
// A valid _route module exists at this depth (invalid exports throw on import).
|
|
295
|
-
chainIdx += 1;
|
|
296
274
|
}
|
|
297
275
|
}
|
|
298
276
|
|
|
@@ -352,7 +330,11 @@ export async function resolveCurrentDevRoute(
|
|
|
352
330
|
let chain: RuntimeRoute[] | undefined;
|
|
353
331
|
if (isDefinedRouteTerminal(pageMod.route) && typeof pageMod.route.page === "function") {
|
|
354
332
|
const currentChain = route.routeChain;
|
|
355
|
-
await refreshLayoutChain(
|
|
333
|
+
await refreshLayoutChain(
|
|
334
|
+
currentChain,
|
|
335
|
+
route.layouts.map(({ path }) => path),
|
|
336
|
+
undefined
|
|
337
|
+
);
|
|
356
338
|
const parent = currentChain.at(-2) ?? currentRoot.route;
|
|
357
339
|
const adaptedPage = adaptDefinedPage(pageMod.route, parent);
|
|
358
340
|
page = adaptedPage;
|
|
@@ -363,7 +345,7 @@ export async function resolveCurrentDevRoute(
|
|
|
363
345
|
// and layout. `refreshLayoutChain` deliberately starts at chainIdx=1
|
|
364
346
|
// because the root is already loaded separately above; without this
|
|
365
347
|
// patch, chain[0] is the root adapted by the last route scan — a
|
|
366
|
-
// STALE reference for a nested page between a root
|
|
348
|
+
// STALE reference for a nested page between a root layout edit and the
|
|
367
349
|
// topology watcher's rescan. Mirroring the pattern used by
|
|
368
350
|
// patchRouteEntryFromFreshModule.
|
|
369
351
|
if (chain[0] && currentRoot.route) {
|
|
@@ -463,7 +445,11 @@ async function loadCachedDevLoaderData(
|
|
|
463
445
|
: await runPublicLoaders(route, loaderCtx);
|
|
464
446
|
if (result.type === "data") {
|
|
465
447
|
const entry: DevLoaderCacheEntry = {
|
|
466
|
-
dependencies: computeRouteDependencies(
|
|
448
|
+
dependencies: computeRouteDependencies(
|
|
449
|
+
route.path,
|
|
450
|
+
root.path,
|
|
451
|
+
route.layouts.map(({ path }) => path)
|
|
452
|
+
),
|
|
467
453
|
generatedAt: Date.now(),
|
|
468
454
|
headers: result.headers,
|
|
469
455
|
loaderData: result.syncData,
|
|
@@ -550,26 +536,22 @@ export const developmentPagePorts: PagePorts = {
|
|
|
550
536
|
|
|
551
537
|
/**
|
|
552
538
|
* @internal Lists every source file whose contents can affect the render
|
|
553
|
-
* output for a given page: the page itself,
|
|
554
|
-
*
|
|
539
|
+
* output for a given page: the page itself, its layouts, the root layout,
|
|
540
|
+
* and their local imports.
|
|
555
541
|
*
|
|
556
542
|
* Only paths that EXIST on disk are returned. `isDevLoaderCacheValid` treats
|
|
557
543
|
* a `statSync` throw as "invalid" (conservative on missing files), so listing
|
|
558
|
-
*
|
|
559
|
-
*
|
|
560
|
-
*
|
|
561
|
-
*
|
|
562
|
-
* the next read and yield the same conservative miss.
|
|
544
|
+
* missing files here would force a permanent cache MISS. Renames and
|
|
545
|
+
* deletions of TRACKED deps are still detected: the path that previously
|
|
546
|
+
* existed will then `statSync`-throw on the next read and yield the same
|
|
547
|
+
* conservative miss.
|
|
563
548
|
*/
|
|
564
|
-
export function computeRouteDependencies(
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
}
|
|
571
|
-
}
|
|
572
|
-
}
|
|
549
|
+
export function computeRouteDependencies(
|
|
550
|
+
pagePath: string,
|
|
551
|
+
rootPath: string,
|
|
552
|
+
layoutPaths: readonly string[]
|
|
553
|
+
): string[] {
|
|
554
|
+
const deps = [pagePath, rootPath, ...layoutPaths.filter((path) => existsSync(path))];
|
|
573
555
|
const graph = devGraph(undefined);
|
|
574
556
|
return [...new Set(deps.flatMap((path) => graph.sourceDependencies(path)))].filter(existsSync);
|
|
575
557
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { parse } from "node:path";
|
|
2
1
|
import type { RuntimePage, RuntimeRoute } from "../../client/internal/runtime-types.ts";
|
|
3
2
|
import { compareRouteSpecificity } from "../../shared/route-specificity.ts";
|
|
4
3
|
import type { ResolvedRoute } from "./types.ts";
|
|
@@ -6,20 +5,6 @@ import type { ResolvedRoute } from "./types.ts";
|
|
|
6
5
|
// biome-ignore lint/performance/noBarrelFile: preserve the existing comparator import while sharing its implementation with the client.
|
|
7
6
|
export { compareRouteSpecificity } from "../../shared/route-specificity.ts";
|
|
8
7
|
|
|
9
|
-
export function collectIntermediateLayoutDirs(pagePath: string, rootPath: string): string[] {
|
|
10
|
-
const pageDir = pagePath.slice(0, pagePath.lastIndexOf("/"));
|
|
11
|
-
const pagesDir = rootPath.slice(0, rootPath.lastIndexOf("/"));
|
|
12
|
-
const layoutDirs: string[] = [];
|
|
13
|
-
let dir = pageDir;
|
|
14
|
-
|
|
15
|
-
while (dir.length > pagesDir.length) {
|
|
16
|
-
layoutDirs.unshift(dir);
|
|
17
|
-
dir = dir.slice(0, dir.lastIndexOf("/"));
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return layoutDirs;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
8
|
export function resolveRouteRevalidate(page: RuntimePage): number | undefined {
|
|
24
9
|
return page.revalidate ?? page._route.revalidate;
|
|
25
10
|
}
|
|
@@ -77,90 +62,8 @@ export function resolveMode(page: RuntimePage, routeChain: RuntimeRoute[]): "ssr
|
|
|
77
62
|
return "ssr";
|
|
78
63
|
}
|
|
79
64
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
export interface DynamicRouteSegment {
|
|
83
|
-
catchAll: boolean;
|
|
84
|
-
name: string;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export function parseDynamicRouteSegment(
|
|
88
|
-
segment: string,
|
|
89
|
-
sourcePath: string | undefined
|
|
90
|
-
): DynamicRouteSegment | undefined {
|
|
91
|
-
if (!(segment.startsWith("[") && segment.endsWith("]"))) {
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
const inner = segment.slice(1, -1);
|
|
95
|
-
const catchAll = inner.startsWith("...");
|
|
96
|
-
const name = catchAll ? inner.slice(3) : inner;
|
|
97
|
-
if (!DYNAMIC_PARAMETER_NAME_RE.test(name)) {
|
|
98
|
-
throw new Error(
|
|
99
|
-
`[furin] Invalid dynamic parameter ${JSON.stringify(name)} in ${JSON.stringify(
|
|
100
|
-
sourcePath ?? segment
|
|
101
|
-
)}. Parameter names must match ${DYNAMIC_PARAMETER_NAME_RE}.`
|
|
102
|
-
);
|
|
103
|
-
}
|
|
104
|
-
return { catchAll, name };
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
export function routeSegmentToPattern(segment: string): string {
|
|
108
|
-
const dynamic = parseDynamicRouteSegment(segment, undefined);
|
|
109
|
-
if (!dynamic) {
|
|
110
|
-
return segment;
|
|
111
|
-
}
|
|
112
|
-
return dynamic.catchAll ? "*" : `:${dynamic.name}`;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const ROUTE_GROUP_RE = /^\([^()]+\)$/;
|
|
116
|
-
|
|
117
|
-
export function isRouteGroup(segment: string): boolean {
|
|
118
|
-
return ROUTE_GROUP_RE.test(segment);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
const DYNAMIC_ROUTE_SEGMENT_RE = /(^|\/):[^/]+/g;
|
|
122
|
-
|
|
123
|
-
export function routePatternKey(pattern: string): string {
|
|
124
|
-
return pattern.replace(DYNAMIC_ROUTE_SEGMENT_RE, "$1:param");
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
export function filePathToPattern(path: string): string {
|
|
128
|
-
const parts = path.replaceAll("\\", "/").split("/");
|
|
129
|
-
const segments: string[] = [];
|
|
130
|
-
const lastIndex = parts.length - 1;
|
|
131
|
-
|
|
132
|
-
for (let idx = 0; idx < parts.length; idx += 1) {
|
|
133
|
-
const part = parts[idx];
|
|
134
|
-
if (part === undefined || part.length === 0) {
|
|
135
|
-
continue;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
// Only the leaf (file) segment carries an extension to strip. Directory
|
|
139
|
-
// segments are kept verbatim so valid folder names with dots — e.g.
|
|
140
|
-
// `v1.0` — are not truncated into `v1` by `parse().ext` handling.
|
|
141
|
-
const isFile = idx === lastIndex;
|
|
142
|
-
const name = isFile ? parse(part).name : part;
|
|
143
|
-
|
|
144
|
-
if (!isFile && isRouteGroup(name)) {
|
|
145
|
-
continue;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// `index` collapses to its parent ONLY as a leaf filename. A directory
|
|
149
|
-
// literally named `index` is a real route segment and must be preserved.
|
|
150
|
-
if (isFile && name === "index") {
|
|
151
|
-
continue;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
const dynamic = parseDynamicRouteSegment(name, path);
|
|
155
|
-
if (dynamic) {
|
|
156
|
-
segments.push(dynamic.catchAll ? "*" : `:${dynamic.name}`);
|
|
157
|
-
} else {
|
|
158
|
-
segments.push(name);
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
return `/${segments.join("/")}`;
|
|
163
|
-
}
|
|
65
|
+
/** Param name of a catch-all segment (`*` in patterns), as in TanStack Start. */
|
|
66
|
+
export const SPLAT_PARAM = "_splat";
|
|
164
67
|
|
|
165
68
|
const REGEX_META_CHARS_RE = /[.*+?^${}()|[\]\\]/g;
|
|
166
69
|
|
|
@@ -171,8 +74,8 @@ export function escapeRegExp(value: string): string {
|
|
|
171
74
|
/**
|
|
172
75
|
* Unanchored regex source for a route pattern: literal characters (e.g. dots
|
|
173
76
|
* in `v1.0`) are escaped, each `:param` becomes `sources.param` and `*`
|
|
174
|
-
* becomes `sources.catchAll`. `paramNames` lists them in order (
|
|
175
|
-
* catch-all).
|
|
77
|
+
* becomes `sources.catchAll`. `paramNames` lists them in order (`_splat` for
|
|
78
|
+
* the catch-all).
|
|
176
79
|
*/
|
|
177
80
|
export function routePatternRegexSource(
|
|
178
81
|
pattern: string,
|
|
@@ -188,11 +91,21 @@ export function routePatternRegexSource(
|
|
|
188
91
|
while (i < pattern.length && pattern[i] !== "/") {
|
|
189
92
|
i += 1;
|
|
190
93
|
}
|
|
191
|
-
|
|
192
|
-
|
|
94
|
+
const name = pattern.slice(start, i);
|
|
95
|
+
if (name.endsWith("?") && source.endsWith("/")) {
|
|
96
|
+
// `/:lang?` matches with or without its segment, slash included.
|
|
97
|
+
paramNames.push(name.slice(0, -1));
|
|
98
|
+
source = `${source.slice(0, -1)}(?:/${sources.param})?`;
|
|
99
|
+
} else {
|
|
100
|
+
paramNames.push(name);
|
|
101
|
+
source += sources.param;
|
|
102
|
+
}
|
|
193
103
|
} else if (pattern[i] === "*") {
|
|
194
|
-
paramNames.push(
|
|
195
|
-
|
|
104
|
+
paramNames.push(SPLAT_PARAM);
|
|
105
|
+
// Elysia serves `/docs` from `/docs/*` too, with an empty splat.
|
|
106
|
+
source = source.endsWith("/")
|
|
107
|
+
? `${source.slice(0, -1)}(?:/${sources.catchAll})?`
|
|
108
|
+
: source + sources.catchAll;
|
|
196
109
|
i += 1;
|
|
197
110
|
} else {
|
|
198
111
|
const ch = pattern[i];
|
|
@@ -235,6 +148,28 @@ interface CompiledRoutePattern<TRoute extends RoutePatternLike> {
|
|
|
235
148
|
route: TRoute;
|
|
236
149
|
}
|
|
237
150
|
|
|
151
|
+
function decodeParam(value: string): string {
|
|
152
|
+
try {
|
|
153
|
+
return decodeURIComponent(value);
|
|
154
|
+
} catch {
|
|
155
|
+
return value;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** An empty catch-all is `""`; an optional segment that did not match stays unset. */
|
|
160
|
+
function matchedParams(paramNames: string[], match: RegExpExecArray): Record<string, string> {
|
|
161
|
+
const params: Record<string, string> = {};
|
|
162
|
+
for (const [index, name] of paramNames.entries()) {
|
|
163
|
+
const value = match[index + 1];
|
|
164
|
+
if (value !== undefined) {
|
|
165
|
+
params[name] = decodeParam(value);
|
|
166
|
+
} else if (name === SPLAT_PARAM) {
|
|
167
|
+
params[name] = "";
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return params;
|
|
171
|
+
}
|
|
172
|
+
|
|
238
173
|
export function buildRouteMatcher<TRoute extends RoutePatternLike>(
|
|
239
174
|
routes: TRoute[]
|
|
240
175
|
): (pathname: string) => RouteMatch<TRoute> | null {
|
|
@@ -251,18 +186,7 @@ export function buildRouteMatcher<TRoute extends RoutePatternLike>(
|
|
|
251
186
|
if (!match) {
|
|
252
187
|
continue;
|
|
253
188
|
}
|
|
254
|
-
|
|
255
|
-
for (let i = 0; i < candidate.paramNames.length; i += 1) {
|
|
256
|
-
const name = candidate.paramNames[i];
|
|
257
|
-
if (name !== undefined) {
|
|
258
|
-
try {
|
|
259
|
-
params[name] = decodeURIComponent(match[i + 1] ?? "");
|
|
260
|
-
} catch {
|
|
261
|
-
params[name] = match[i + 1] ?? "";
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
return { params, route: candidate.route };
|
|
189
|
+
return { params: matchedParams(candidate.paramNames, match), route: candidate.route };
|
|
266
190
|
}
|
|
267
191
|
return null;
|
|
268
192
|
};
|
|
@@ -22,15 +22,31 @@ export interface SegmentBoundary {
|
|
|
22
22
|
* can't survive the server→client boundary.
|
|
23
23
|
*/
|
|
24
24
|
errorPath?: string;
|
|
25
|
+
/**
|
|
26
|
+
* TanStack style: the components are a route's `.error()` / `.notFound()`,
|
|
27
|
+
* read from `errorPath`'s `route` export instead of its default export.
|
|
28
|
+
*/
|
|
29
|
+
fromRoute?: true;
|
|
25
30
|
notFound?: NotFoundComponent;
|
|
26
31
|
/** Absolute path to the `not-found.tsx` module, when present. */
|
|
27
32
|
notFoundPath?: string;
|
|
28
|
-
/**
|
|
33
|
+
/**
|
|
34
|
+
* Absolute directory path; the route file for a `fromRoute` boundary,
|
|
35
|
+
* except the root's, which is the pages directory.
|
|
36
|
+
*/
|
|
29
37
|
path: string;
|
|
30
38
|
}
|
|
31
39
|
|
|
40
|
+
/** A layout wrapping a page, from the route manifest. */
|
|
41
|
+
export interface ResolvedLayout {
|
|
42
|
+
path: string;
|
|
43
|
+
pattern: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
32
46
|
export interface ResolvedRoute {
|
|
33
47
|
isrCache?: { html: string; generatedAt: number; revalidate: number };
|
|
48
|
+
/** Layouts wrapping the page, shallow → deep, the root excluded. */
|
|
49
|
+
layouts: ResolvedLayout[];
|
|
34
50
|
mode: "ssr" | "ssg" | "isr";
|
|
35
51
|
page: RuntimePage;
|
|
36
52
|
path: string;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Database } from "bun:sqlite";
|
|
2
|
-
import { sql as drizzleSql } from "drizzle-orm";
|
|
3
|
-
import type { BunSQLDatabase } from "drizzle-orm/bun-sql";
|
|
4
|
-
import type {
|
|
2
|
+
import { type AnyRelations, sql as drizzleSql } from "drizzle-orm";
|
|
3
|
+
import type { BunSQLDatabase } from "drizzle-orm/bun-sql/postgres";
|
|
4
|
+
import type { SQLiteBunDatabase } from "drizzle-orm/bun-sqlite";
|
|
5
5
|
import type {
|
|
6
6
|
AtomicMutationResult,
|
|
7
7
|
AtomicMutationValue,
|
|
@@ -14,14 +14,10 @@ import { PostgresSyncAdapter } from "../postgres/index.ts";
|
|
|
14
14
|
import { transactionSql } from "../postgres/sql.ts";
|
|
15
15
|
import { SqliteSyncAdapter } from "../sqlite/index.ts";
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
[
|
|
19
|
-
}
|
|
20
|
-
type Transaction<Db extends BunSQLiteDatabase<Schema> | BunSQLDatabase<Schema>> = Parameters<
|
|
21
|
-
Parameters<Db["transaction"]>[0]
|
|
22
|
-
>[0];
|
|
17
|
+
type Transaction<Db extends SQLiteBunDatabase<AnyRelations> | BunSQLDatabase<AnyRelations>> =
|
|
18
|
+
Parameters<Parameters<Db["transaction"]>[0]>[0];
|
|
23
19
|
|
|
24
|
-
function drizzleTransactionSql(db: Pick<BunSQLDatabase<
|
|
20
|
+
function drizzleTransactionSql(db: Pick<BunSQLDatabase<AnyRelations>, "execute">) {
|
|
25
21
|
return transactionSql(<Rows>(strings: TemplateStringsArray, values: unknown[]) =>
|
|
26
22
|
Promise.resolve(db.execute(drizzleSql(strings, ...values))).then(
|
|
27
23
|
(rows) => rows as unknown as Rows
|
|
@@ -29,7 +25,7 @@ function drizzleTransactionSql(db: Pick<BunSQLDatabase<Schema>, "execute">) {
|
|
|
29
25
|
);
|
|
30
26
|
}
|
|
31
27
|
|
|
32
|
-
export class DrizzleSqliteSyncAdapter<Db extends
|
|
28
|
+
export class DrizzleSqliteSyncAdapter<Db extends SQLiteBunDatabase<AnyRelations>>
|
|
33
29
|
extends SqliteSyncAdapter
|
|
34
30
|
implements TransactionalSyncAdapter<Transaction<Db>, "sync">
|
|
35
31
|
{
|
|
@@ -70,7 +66,7 @@ export class DrizzleSqliteSyncAdapter<Db extends BunSQLiteDatabase<Schema>>
|
|
|
70
66
|
}
|
|
71
67
|
}
|
|
72
68
|
|
|
73
|
-
export class DrizzlePostgresSyncAdapter<Db extends BunSQLDatabase<
|
|
69
|
+
export class DrizzlePostgresSyncAdapter<Db extends BunSQLDatabase<AnyRelations>>
|
|
74
70
|
extends PostgresSyncAdapter
|
|
75
71
|
implements TransactionalSyncAdapter<Transaction<Db>, "async">
|
|
76
72
|
{
|
|
@@ -107,30 +103,33 @@ export class DrizzlePostgresSyncAdapter<Db extends BunSQLDatabase<Schema>>
|
|
|
107
103
|
}
|
|
108
104
|
}
|
|
109
105
|
|
|
110
|
-
export function drizzleSyncAdapter<Db extends
|
|
106
|
+
export function drizzleSyncAdapter<Db extends SQLiteBunDatabase<AnyRelations>>(options: {
|
|
111
107
|
db: Db & { $client: import("bun:sqlite").Database };
|
|
112
108
|
namespace: string;
|
|
113
109
|
}): DrizzleSqliteSyncAdapter<Db>;
|
|
114
|
-
export function drizzleSyncAdapter<Db extends BunSQLDatabase<
|
|
110
|
+
export function drizzleSyncAdapter<Db extends BunSQLDatabase<AnyRelations>>(options: {
|
|
115
111
|
db: Db & { $client: import("bun").SQL };
|
|
116
112
|
namespace: string;
|
|
117
113
|
}): DrizzlePostgresSyncAdapter<Db>;
|
|
118
114
|
export function drizzleSyncAdapter(options: {
|
|
119
|
-
db: (
|
|
115
|
+
db: (SQLiteBunDatabase<AnyRelations> | BunSQLDatabase<AnyRelations>) & {
|
|
120
116
|
$client: import("bun:sqlite").Database | import("bun").SQL;
|
|
121
117
|
};
|
|
122
118
|
namespace: string;
|
|
123
119
|
}):
|
|
124
|
-
| DrizzleSqliteSyncAdapter<
|
|
125
|
-
| DrizzlePostgresSyncAdapter<BunSQLDatabase<
|
|
120
|
+
| DrizzleSqliteSyncAdapter<SQLiteBunDatabase<AnyRelations>>
|
|
121
|
+
| DrizzlePostgresSyncAdapter<BunSQLDatabase<AnyRelations>> {
|
|
126
122
|
if (!(options.db.$client instanceof Database)) {
|
|
127
123
|
return new DrizzlePostgresSyncAdapter(
|
|
128
|
-
options as {
|
|
124
|
+
options as {
|
|
125
|
+
db: BunSQLDatabase<AnyRelations> & { $client: import("bun").SQL };
|
|
126
|
+
namespace: string;
|
|
127
|
+
}
|
|
129
128
|
);
|
|
130
129
|
}
|
|
131
130
|
return new DrizzleSqliteSyncAdapter(
|
|
132
131
|
options as {
|
|
133
|
-
db:
|
|
132
|
+
db: SQLiteBunDatabase<AnyRelations> & { $client: import("bun:sqlite").Database };
|
|
134
133
|
namespace: string;
|
|
135
134
|
}
|
|
136
135
|
);
|
package/src/shared/not-found.ts
CHANGED
|
@@ -5,8 +5,8 @@ function segmentSpecificity(segment: string): number {
|
|
|
5
5
|
return segment.startsWith(":") ? 2 : 3;
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
/**
|
|
9
|
-
|
|
8
|
+
/** Literal > parameter > catch-all, on patterns without optional segments. */
|
|
9
|
+
function compareConcreteSpecificity(a: string, b: string): number {
|
|
10
10
|
const aSegments = a.split("/").filter(Boolean);
|
|
11
11
|
const bSegments = b.split("/").filter(Boolean);
|
|
12
12
|
const length = Math.max(aSegments.length, bSegments.length);
|
|
@@ -26,3 +26,35 @@ export function compareRouteSpecificity(a: string, b: string): number {
|
|
|
26
26
|
}
|
|
27
27
|
return 0;
|
|
28
28
|
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The concrete patterns an optional `:x?` segment stands for, as Elysia
|
|
32
|
+
* registers them: `/:lang?/about` → `/about` and `/:lang/about`.
|
|
33
|
+
*/
|
|
34
|
+
export function expandOptionalSegments(pattern: string): string[] {
|
|
35
|
+
let variants: string[][] = [[]];
|
|
36
|
+
for (const segment of pattern.split("/").filter(Boolean)) {
|
|
37
|
+
if (segment.startsWith(":") && segment.endsWith("?")) {
|
|
38
|
+
const required = segment.slice(0, -1);
|
|
39
|
+
variants = variants.flatMap((variant) => [variant, [...variant, required]]);
|
|
40
|
+
} else {
|
|
41
|
+
variants = variants.map((variant) => [...variant, segment]);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return variants.map((variant) => `/${variant.join("/")}`);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function mostSpecificVariant(pattern: string): string {
|
|
48
|
+
return expandOptionalSegments(pattern).reduce((best, variant) =>
|
|
49
|
+
compareConcreteSpecificity(variant, best) > 0 ? variant : best
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Positive when a is more specific: literal > parameter > catch-all. A
|
|
55
|
+
* pattern with optional segments ranks as its most specific variant, so
|
|
56
|
+
* `/:lang?/about` serves `/about` before `/:id` does, as on the server.
|
|
57
|
+
*/
|
|
58
|
+
export function compareRouteSpecificity(a: string, b: string): number {
|
|
59
|
+
return compareConcreteSpecificity(mostSpecificVariant(a), mostSpecificVariant(b));
|
|
60
|
+
}
|
|
@@ -71,7 +71,7 @@ export function validateRouteChain(
|
|
|
71
71
|
const location = pagePath ? `in ${pagePath}` : "";
|
|
72
72
|
throw new Error(
|
|
73
73
|
`[furin] Page ${location} must inherit from root route. ` +
|
|
74
|
-
"Its route chain must end at the root
|
|
74
|
+
"Its route chain must end at the root layout."
|
|
75
75
|
);
|
|
76
76
|
}
|
|
77
77
|
|