@teyik0/furin 0.7.0-alpha.1 → 0.7.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/dist/build/elysia-aot-worker.d.ts +22 -0
- package/dist/build/elysia-aot.d.ts +1 -1
- package/dist/build/index.js +226 -142
- package/dist/cli/index.js +227 -143
- package/dist/client/link.js +10 -4
- package/dist/client/router/types.d.ts +2 -0
- package/dist/furin.js +56 -18
- package/dist/server/router/patterns.d.ts +2 -0
- package/dist/server/router/types.d.ts +1 -1
- package/package.json +1 -1
- package/src/build/elysia-aot-worker.ts +51 -0
- package/src/build/elysia-aot.ts +83 -19
- package/src/build/hydrate.ts +4 -0
- package/src/client/router/boundary-tree.tsx +12 -10
- package/src/client/router/provider.tsx +2 -2
- package/src/client/router/types.ts +2 -0
- package/src/plugin/routes.ts +25 -13
- package/src/server/render/element.tsx +14 -9
- package/src/server/router/discovery.ts +25 -9
- package/src/server/router/patterns.ts +16 -0
- package/src/server/router/types.ts +1 -1
package/dist/client/link.js
CHANGED
|
@@ -1083,18 +1083,24 @@ function buildPageElement(match, root, data, options, error) {
|
|
|
1083
1083
|
}
|
|
1084
1084
|
const byDepth = new Map;
|
|
1085
1085
|
for (const segment of match.segmentBoundaries ?? []) {
|
|
1086
|
-
byDepth.
|
|
1086
|
+
const boundaries = byDepth.get(segment.depth) ?? [];
|
|
1087
|
+
boundaries.unshift(segment);
|
|
1088
|
+
byDepth.set(segment.depth, boundaries);
|
|
1087
1089
|
}
|
|
1088
1090
|
const rootOffset = root ? 1 : 0;
|
|
1089
1091
|
for (let i = chain.length - 1;i >= rootOffset; i -= 1) {
|
|
1090
|
-
|
|
1092
|
+
for (const segment of byDepth.get(i) ?? []) {
|
|
1093
|
+
element = wrapSegmentBoundaries(element, segment, options);
|
|
1094
|
+
}
|
|
1091
1095
|
const Layout = chain[i]?.layout;
|
|
1092
1096
|
if (Layout) {
|
|
1093
1097
|
element = createElement3(Layout, data, element);
|
|
1094
1098
|
}
|
|
1095
1099
|
}
|
|
1096
1100
|
if (root) {
|
|
1097
|
-
|
|
1101
|
+
for (const segment of byDepth.get(0) ?? []) {
|
|
1102
|
+
element = wrapSegmentBoundaries(element, segment, options);
|
|
1103
|
+
}
|
|
1098
1104
|
if (root.layout) {
|
|
1099
1105
|
element = createElement3(root.layout, data, element);
|
|
1100
1106
|
}
|
|
@@ -5297,7 +5303,7 @@ function RouterProvider({
|
|
|
5297
5303
|
const resolvedSearch = state.data.query ?? EMPTY_SEARCH;
|
|
5298
5304
|
const rootBoundaries = useMemo2(() => {
|
|
5299
5305
|
for (const route of routes) {
|
|
5300
|
-
const depth0 = route.segmentBoundaries?.find((b) => b.depth === 0);
|
|
5306
|
+
const depth0 = route.segmentBoundaries?.find((b) => b.depth === 0 && b.isRoot !== false);
|
|
5301
5307
|
if (depth0) {
|
|
5302
5308
|
return [depth0];
|
|
5303
5309
|
}
|
|
@@ -174,6 +174,8 @@ export interface RouterContextValue {
|
|
|
174
174
|
export interface ClientSegmentBoundary {
|
|
175
175
|
depth: number;
|
|
176
176
|
error?: ErrorComponent;
|
|
177
|
+
/** False when a pathless directory shares index 0 but does not own the root fallback. */
|
|
178
|
+
isRoot?: boolean;
|
|
177
179
|
notFound?: NotFoundComponent;
|
|
178
180
|
}
|
|
179
181
|
export interface ClientRoute {
|
package/dist/furin.js
CHANGED
|
@@ -258,6 +258,12 @@ function routeSegmentToPattern(segment) {
|
|
|
258
258
|
}
|
|
259
259
|
return dynamic.catchAll ? "*" : `:${dynamic.name}`;
|
|
260
260
|
}
|
|
261
|
+
function isRouteGroup(segment) {
|
|
262
|
+
return ROUTE_GROUP_RE.test(segment);
|
|
263
|
+
}
|
|
264
|
+
function routePatternKey(pattern) {
|
|
265
|
+
return pattern.replace(DYNAMIC_ROUTE_SEGMENT_RE, "$1:param");
|
|
266
|
+
}
|
|
261
267
|
function filePathToPattern(path) {
|
|
262
268
|
const parts = path.replaceAll("\\", "/").split("/");
|
|
263
269
|
const segments = [];
|
|
@@ -269,6 +275,9 @@ function filePathToPattern(path) {
|
|
|
269
275
|
}
|
|
270
276
|
const isFile = idx === lastIndex;
|
|
271
277
|
const name = isFile ? parse(part).name : part;
|
|
278
|
+
if (!isFile && isRouteGroup(name)) {
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
272
281
|
if (isFile && name === "index") {
|
|
273
282
|
continue;
|
|
274
283
|
}
|
|
@@ -363,9 +372,11 @@ function buildRouteMatcher(routes) {
|
|
|
363
372
|
return null;
|
|
364
373
|
};
|
|
365
374
|
}
|
|
366
|
-
var DYNAMIC_PARAMETER_NAME_RE, REGEX_META_CHARS_RE;
|
|
375
|
+
var DYNAMIC_PARAMETER_NAME_RE, ROUTE_GROUP_RE, DYNAMIC_ROUTE_SEGMENT_RE, REGEX_META_CHARS_RE;
|
|
367
376
|
var init_patterns = __esm(() => {
|
|
368
377
|
DYNAMIC_PARAMETER_NAME_RE = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
378
|
+
ROUTE_GROUP_RE = /^\([^()]+\)$/;
|
|
379
|
+
DYNAMIC_ROUTE_SEGMENT_RE = /(^|\/):[^/]+/g;
|
|
369
380
|
REGEX_META_CHARS_RE = /[.*+?^${}()|[\]\\]/;
|
|
370
381
|
});
|
|
371
382
|
|
|
@@ -7162,9 +7173,6 @@ async function scanRootLayout(pagesDir) {
|
|
|
7162
7173
|
route: rootRoute
|
|
7163
7174
|
};
|
|
7164
7175
|
}
|
|
7165
|
-
function routePatternKey(pattern) {
|
|
7166
|
-
return pattern.replace(DYNAMIC_ROUTE_SEGMENT_RE, "$1:param");
|
|
7167
|
-
}
|
|
7168
7176
|
function isConventionFileName(name) {
|
|
7169
7177
|
return CONVENTION_FILE_NAMES.includes(name);
|
|
7170
7178
|
}
|
|
@@ -7213,7 +7221,7 @@ async function scanPageFiles(pagesDir, root) {
|
|
|
7213
7221
|
const devRoute = await buildDevRoute(absolutePath, relativePath, pattern, root, pagesDir);
|
|
7214
7222
|
devRoute.notFound = notFound;
|
|
7215
7223
|
devRoute.error = errorComponent;
|
|
7216
|
-
devRoute.segmentBoundaries = segmentBoundaries;
|
|
7224
|
+
devRoute.segmentBoundaries = alignSegmentBoundaryDepths(segmentBoundaries, devRoute.routeChain);
|
|
7217
7225
|
routes.push(devRoute);
|
|
7218
7226
|
continue;
|
|
7219
7227
|
}
|
|
@@ -7233,7 +7241,7 @@ async function scanPageFiles(pagesDir, root) {
|
|
|
7233
7241
|
path: absolutePath,
|
|
7234
7242
|
pattern,
|
|
7235
7243
|
routeChain,
|
|
7236
|
-
segmentBoundaries,
|
|
7244
|
+
segmentBoundaries: alignSegmentBoundaryDepths(segmentBoundaries, routeChain),
|
|
7237
7245
|
tags: collectRouteTags(routeChain, page)
|
|
7238
7246
|
});
|
|
7239
7247
|
}
|
|
@@ -7271,6 +7279,21 @@ async function resolveDefinedLayoutParent(pagePath, pagesDir, rootRoute) {
|
|
|
7271
7279
|
}
|
|
7272
7280
|
return parent;
|
|
7273
7281
|
}
|
|
7282
|
+
function alignSegmentBoundaryDepths(boundaries, routeChain) {
|
|
7283
|
+
return boundaries.map((boundary) => {
|
|
7284
|
+
let depth = 0;
|
|
7285
|
+
for (const [index, entry] of routeChain.entries()) {
|
|
7286
|
+
if (!entry.sourcePath) {
|
|
7287
|
+
continue;
|
|
7288
|
+
}
|
|
7289
|
+
const directory = entry.sourcePath.slice(0, entry.sourcePath.lastIndexOf("/"));
|
|
7290
|
+
if (boundary.path === directory || boundary.path.startsWith(`${directory}/`)) {
|
|
7291
|
+
depth = index;
|
|
7292
|
+
}
|
|
7293
|
+
}
|
|
7294
|
+
return { ...boundary, depth };
|
|
7295
|
+
});
|
|
7296
|
+
}
|
|
7274
7297
|
async function collectSegmentBoundaries(pageAbsolutePath, pagesDir, notFoundCache, errorCache) {
|
|
7275
7298
|
const pageDir = pageAbsolutePath.slice(0, pageAbsolutePath.lastIndexOf("/"));
|
|
7276
7299
|
const dirs = [pagesDir];
|
|
@@ -7371,14 +7394,13 @@ async function collectPageFilePaths(dir) {
|
|
|
7371
7394
|
}
|
|
7372
7395
|
return files;
|
|
7373
7396
|
}
|
|
7374
|
-
var CONVENTION_FILE_NAMES, SOURCE_MODULE_EXTENSIONS,
|
|
7397
|
+
var CONVENTION_FILE_NAMES, SOURCE_MODULE_EXTENSIONS, DIR_SCAN_CONCURRENCY = 8;
|
|
7375
7398
|
var init_discovery = __esm(() => {
|
|
7376
7399
|
init_internal();
|
|
7377
7400
|
init_patterns();
|
|
7378
7401
|
init_source_version();
|
|
7379
7402
|
CONVENTION_FILE_NAMES = ["not-found", "error"];
|
|
7380
7403
|
SOURCE_MODULE_EXTENSIONS = [".tsx", ".ts", ".jsx", ".js"];
|
|
7381
|
-
DYNAMIC_ROUTE_SEGMENT_RE = /(^|\/):[^/]+/g;
|
|
7382
7404
|
});
|
|
7383
7405
|
|
|
7384
7406
|
// src/server/dev/transform-failure.ts
|
|
@@ -11111,7 +11133,8 @@ function routeModuleSpecifier(instance) {
|
|
|
11111
11133
|
return `@teyik0/furin/routes?instance=${Bun.hash(instanceKey(instance)).toString(16)}`;
|
|
11112
11134
|
}
|
|
11113
11135
|
function scanRouteFiles(directory, directorySegments, files) {
|
|
11114
|
-
|
|
11136
|
+
const entries = readdirSync(directory, { withFileTypes: true }).sort((left, right) => left.name.localeCompare(right.name));
|
|
11137
|
+
for (const entry of entries) {
|
|
11115
11138
|
if (entry.isDirectory()) {
|
|
11116
11139
|
if (entry.name.startsWith("_")) {
|
|
11117
11140
|
continue;
|
|
@@ -11144,9 +11167,6 @@ function scanRouteFiles(directory, directorySegments, files) {
|
|
|
11144
11167
|
function segmentPath(segment) {
|
|
11145
11168
|
return routeSegmentToPattern(segment);
|
|
11146
11169
|
}
|
|
11147
|
-
function routePath(segments) {
|
|
11148
|
-
return segments.length === 0 ? "/" : `/${segments.map(segmentPath).join("/")}`;
|
|
11149
|
-
}
|
|
11150
11170
|
function routeId(instanceId, path) {
|
|
11151
11171
|
const encodedPath = Array.from(new TextEncoder().encode(path), (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
11152
11172
|
return `_route_${instanceId}_${encodedPath}`;
|
|
@@ -11175,14 +11195,22 @@ function buildRouteTree(pagesDir, instanceId) {
|
|
|
11175
11195
|
return node;
|
|
11176
11196
|
};
|
|
11177
11197
|
const root = ensureNode([]);
|
|
11198
|
+
const seenPatterns = new Map;
|
|
11178
11199
|
for (const file of scannedFiles) {
|
|
11179
11200
|
const node = ensureNode(file.directorySegments);
|
|
11180
11201
|
if (file.base === "_route") {
|
|
11181
|
-
const path =
|
|
11202
|
+
const path = `/${file.directorySegments.map(segmentPath).join("/")}#layout`;
|
|
11182
11203
|
node.layout = { id: routeId(instanceId, path), path: "", sourcePath: file.sourcePath };
|
|
11183
11204
|
continue;
|
|
11184
11205
|
}
|
|
11185
|
-
const path =
|
|
11206
|
+
const path = filePathToPattern(relative7(pagesDir, file.sourcePath));
|
|
11207
|
+
const patternKey = routePatternKey(path);
|
|
11208
|
+
const relativePath = relative7(pagesDir, file.sourcePath).replaceAll("\\", "/");
|
|
11209
|
+
const previousPath = seenPatterns.get(patternKey);
|
|
11210
|
+
if (previousPath !== undefined) {
|
|
11211
|
+
throw new Error(`[furin] Duplicate route pattern "${patternKey}" from "${previousPath}" and "${relativePath}".`);
|
|
11212
|
+
}
|
|
11213
|
+
seenPatterns.set(patternKey, relativePath);
|
|
11186
11214
|
const route = { id: routeId(instanceId, path), path, sourcePath: file.sourcePath };
|
|
11187
11215
|
if (file.base === "index") {
|
|
11188
11216
|
node.indexRoute = route;
|
|
@@ -11581,7 +11609,7 @@ async function composableRouteApps(route) {
|
|
|
11581
11609
|
};
|
|
11582
11610
|
}
|
|
11583
11611
|
async function composeRuntimeNode(node) {
|
|
11584
|
-
const prefix = node.name ? `/${segmentPath(node.name)}` : "";
|
|
11612
|
+
const prefix = node.name && !isRouteGroup(node.name) ? `/${segmentPath(node.name)}` : "";
|
|
11585
11613
|
const { fileRoutes } = node;
|
|
11586
11614
|
const [layoutApps, indexRouteApps, fileRouteApps, childApps] = await Promise.all([
|
|
11587
11615
|
node.layout ? composableRouteApps(node.layout) : undefined,
|
|
@@ -12592,6 +12620,7 @@ var init_route_types = () => {};
|
|
|
12592
12620
|
import { existsSync as existsSync7, mkdirSync, readFileSync as readFileSync7, writeFileSync as writeFileSync2 } from "fs";
|
|
12593
12621
|
import { join as join6 } from "path";
|
|
12594
12622
|
function generateHydrateEntry(routes, rootLayout, basePath, clientLogging) {
|
|
12623
|
+
const rootDirectory = rootLayout.replaceAll("\\", "/").split("/").slice(0, -1).join("/");
|
|
12595
12624
|
const getBoundaryIdent = (idents, filePath) => {
|
|
12596
12625
|
if (!filePath) {
|
|
12597
12626
|
return;
|
|
@@ -12632,6 +12661,9 @@ function generateHydrateEntry(routes, rootLayout, basePath, clientLogging) {
|
|
|
12632
12661
|
if (notFoundIdent) {
|
|
12633
12662
|
parts.push(`notFound: ${notFoundIdent}.default`);
|
|
12634
12663
|
}
|
|
12664
|
+
if (seg.depth === 0 && seg.path.replaceAll("\\", "/") !== rootDirectory) {
|
|
12665
|
+
parts.push("isRoot: false");
|
|
12666
|
+
}
|
|
12635
12667
|
boundaryLiterals.push(`{ ${parts.join(", ")} }`);
|
|
12636
12668
|
}
|
|
12637
12669
|
const lazyImports = [
|
|
@@ -18584,17 +18616,23 @@ function buildElement(route, data, rootLayout) {
|
|
|
18584
18616
|
const byDepth = new Map;
|
|
18585
18617
|
const legacyRoute = route;
|
|
18586
18618
|
for (const segment of legacyRoute.segmentBoundaries ?? []) {
|
|
18587
|
-
byDepth.
|
|
18619
|
+
const boundaries = byDepth.get(segment.depth) ?? [];
|
|
18620
|
+
boundaries.unshift(segment);
|
|
18621
|
+
byDepth.set(segment.depth, boundaries);
|
|
18588
18622
|
}
|
|
18589
18623
|
for (let i = route.routeChain.length - 1;i >= 1; i -= 1) {
|
|
18590
|
-
|
|
18624
|
+
for (const segment of byDepth.get(i) ?? []) {
|
|
18625
|
+
element = wrapSegmentBoundaries(element, segment, undefined);
|
|
18626
|
+
}
|
|
18591
18627
|
const routeEntry = route.routeChain[i];
|
|
18592
18628
|
if (routeEntry?.layout) {
|
|
18593
18629
|
const Layout = routeEntry.layout;
|
|
18594
18630
|
element = createElement6(Layout, data, element);
|
|
18595
18631
|
}
|
|
18596
18632
|
}
|
|
18597
|
-
|
|
18633
|
+
for (const segment of byDepth.get(0) ?? []) {
|
|
18634
|
+
element = wrapSegmentBoundaries(element, segment, undefined);
|
|
18635
|
+
}
|
|
18598
18636
|
if (rootLayout.layout) {
|
|
18599
18637
|
const RootLayoutComponent = rootLayout.layout;
|
|
18600
18638
|
element = createElement6(RootLayoutComponent, data, element);
|
|
@@ -11,6 +11,8 @@ export interface DynamicRouteSegment {
|
|
|
11
11
|
}
|
|
12
12
|
export declare function parseDynamicRouteSegment(segment: string, sourcePath: string | undefined): DynamicRouteSegment | undefined;
|
|
13
13
|
export declare function routeSegmentToPattern(segment: string): string;
|
|
14
|
+
export declare function isRouteGroup(segment: string): boolean;
|
|
15
|
+
export declare function routePatternKey(pattern: string): string;
|
|
14
16
|
export declare function filePathToPattern(path: string): string;
|
|
15
17
|
export declare function escapeRegExpChar(ch: string): string;
|
|
16
18
|
/**
|
|
@@ -11,7 +11,7 @@ import type { NotFoundComponent } from "../../shared/not-found.ts";
|
|
|
11
11
|
* exact nesting level where the user authored them (Next.js app-router model).
|
|
12
12
|
*/
|
|
13
13
|
export interface SegmentBoundary {
|
|
14
|
-
/**
|
|
14
|
+
/** Index of the enclosing layout in routeChain; 0 = the root layout. */
|
|
15
15
|
depth: number;
|
|
16
16
|
error?: ErrorComponent;
|
|
17
17
|
/**
|
package/package.json
CHANGED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { parentPort, workerData } from "node:worker_threads";
|
|
2
|
+
import { aotFactory } from "elysia/plugin/aot/unplugin";
|
|
3
|
+
import { isomorphicTransformPlugin } from "../plugin/transform-isomorphic.ts";
|
|
4
|
+
|
|
5
|
+
export type AotWorkerOperation =
|
|
6
|
+
| { method: "start" | "end" }
|
|
7
|
+
| { code: string; method: "transform"; path: string };
|
|
8
|
+
|
|
9
|
+
export type AotWorkerRequest = AotWorkerOperation & { id: number };
|
|
10
|
+
|
|
11
|
+
export interface AotWorkerResult {
|
|
12
|
+
code?: string;
|
|
13
|
+
virtualType?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type AotWorkerResponse =
|
|
17
|
+
| (AotWorkerResult & { id: number; ok: true })
|
|
18
|
+
| { error: Error; id: number; ok: false };
|
|
19
|
+
|
|
20
|
+
const port = parentPort;
|
|
21
|
+
if (!port) {
|
|
22
|
+
throw new Error("[furin] Elysia AOT worker requires a parent port.");
|
|
23
|
+
}
|
|
24
|
+
const { entry } = workerData as { entry: string };
|
|
25
|
+
const hooks = aotFactory({ entry, strip: false, target: "bun" });
|
|
26
|
+
Bun.plugin(isomorphicTransformPlugin("server"));
|
|
27
|
+
|
|
28
|
+
port.on("message", async (request: AotWorkerRequest) => {
|
|
29
|
+
try {
|
|
30
|
+
let code: string | undefined;
|
|
31
|
+
let virtualType: string | undefined;
|
|
32
|
+
if (request.method === "start") {
|
|
33
|
+
await hooks.buildStart?.();
|
|
34
|
+
code = hooks.load?.("\0elysia/compiled");
|
|
35
|
+
virtualType = hooks.load?.("\0elysia/type");
|
|
36
|
+
} else if (request.method === "transform") {
|
|
37
|
+
if (hooks.transformInclude?.(request.path) !== false) {
|
|
38
|
+
code = hooks.transform?.(request.code, request.path);
|
|
39
|
+
}
|
|
40
|
+
} else {
|
|
41
|
+
hooks.buildEnd?.();
|
|
42
|
+
}
|
|
43
|
+
port.postMessage({ code, id: request.id, ok: true, virtualType } satisfies AotWorkerResponse);
|
|
44
|
+
} catch (error) {
|
|
45
|
+
port.postMessage({
|
|
46
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
47
|
+
id: request.id,
|
|
48
|
+
ok: false,
|
|
49
|
+
} satisfies AotWorkerResponse);
|
|
50
|
+
}
|
|
51
|
+
});
|
package/src/build/elysia-aot.ts
CHANGED
|
@@ -1,9 +1,25 @@
|
|
|
1
|
-
import { dirname, resolve } from "node:path";
|
|
2
|
-
import {
|
|
1
|
+
import { dirname, join, resolve } from "node:path";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import { Worker } from "node:worker_threads";
|
|
4
|
+
import type {
|
|
5
|
+
AotWorkerOperation,
|
|
6
|
+
AotWorkerRequest,
|
|
7
|
+
AotWorkerResponse,
|
|
8
|
+
AotWorkerResult,
|
|
9
|
+
} from "./elysia-aot-worker.ts";
|
|
3
10
|
|
|
4
11
|
const AOT_NAMESPACE = "furin-elysia-aot";
|
|
5
12
|
const ELYSIA_RESOLVE_DIR = dirname(Bun.resolveSync("elysia", import.meta.dir));
|
|
13
|
+
// The published package ships src/ too; resolving from the package entry also
|
|
14
|
+
// works when this adapter is bundled into dist/build/index.js or dist/cli/index.js.
|
|
15
|
+
const AOT_WORKER_PATH = join(
|
|
16
|
+
dirname(dirname(fileURLToPath(import.meta.resolve("@teyik0/furin")))),
|
|
17
|
+
"src/build/elysia-aot-worker.ts"
|
|
18
|
+
);
|
|
6
19
|
const SOURCE_FILTER = /\.[cm]?[jt]sx?$/;
|
|
20
|
+
const ELYSIA_IMPORT_FILTER = /^elysia(?:\/(?!compiled$|type$).*)?$/;
|
|
21
|
+
const ELYSIA_VIRTUAL_IMPORT_FILTER = /^elysia\/(?:compiled|type)$/;
|
|
22
|
+
const ANY_MODULE_FILTER = /.*/;
|
|
7
23
|
const WEBSOCKET_STUB_MARKER =
|
|
8
24
|
"[elysia-aot] WebSocket route builder was stripped (strip mode) but a WS route was used.";
|
|
9
25
|
|
|
@@ -39,16 +55,57 @@ export function handleWSResponse(){return e()}
|
|
|
39
55
|
`;
|
|
40
56
|
}
|
|
41
57
|
|
|
42
|
-
/** Elysia AOT
|
|
58
|
+
/** Elysia AOT evaluation has its own module cache, independent of prerender loaders. */
|
|
43
59
|
export function elysiaAot(entry: string): Bun.BunPlugin {
|
|
44
60
|
const entryPath = resolve(entry);
|
|
45
|
-
const hooks = aotFactory({ entry, strip: false, target: "bun" });
|
|
46
61
|
return {
|
|
47
62
|
name: "elysia-aot",
|
|
48
63
|
async setup(build) {
|
|
64
|
+
const worker = new Worker(AOT_WORKER_PATH, { workerData: { entry: entryPath } });
|
|
65
|
+
const pending = new Map<
|
|
66
|
+
number,
|
|
67
|
+
{
|
|
68
|
+
reject: (error: Error) => void;
|
|
69
|
+
resolve: (response: AotWorkerResult) => void;
|
|
70
|
+
}
|
|
71
|
+
>();
|
|
72
|
+
let nextId = 0;
|
|
73
|
+
let workerFailure: Error | undefined;
|
|
74
|
+
const fail = (error: Error): void => {
|
|
75
|
+
workerFailure = error;
|
|
76
|
+
for (const request of pending.values()) {
|
|
77
|
+
request.reject(error);
|
|
78
|
+
}
|
|
79
|
+
pending.clear();
|
|
80
|
+
};
|
|
81
|
+
worker.on("message", (response: AotWorkerResponse) => {
|
|
82
|
+
const request = pending.get(response.id);
|
|
83
|
+
pending.delete(response.id);
|
|
84
|
+
if (response.ok) {
|
|
85
|
+
request?.resolve(response);
|
|
86
|
+
} else {
|
|
87
|
+
request?.reject(response.error);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
worker.on("error", fail);
|
|
91
|
+
worker.on("exit", (code) => fail(new Error(`[furin] Elysia AOT worker exited (${code}).`)));
|
|
92
|
+
const request = (payload: AotWorkerOperation) =>
|
|
93
|
+
new Promise<AotWorkerResult>((resolveResponse, reject) => {
|
|
94
|
+
if (workerFailure) {
|
|
95
|
+
reject(workerFailure);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
const id = nextId;
|
|
99
|
+
nextId += 1;
|
|
100
|
+
pending.set(id, { reject, resolve: resolveResponse });
|
|
101
|
+
worker.postMessage({ ...payload, id } satisfies AotWorkerRequest);
|
|
102
|
+
});
|
|
103
|
+
let compiled: string | undefined;
|
|
104
|
+
let virtualType: string | undefined;
|
|
49
105
|
try {
|
|
50
|
-
await
|
|
106
|
+
({ code: compiled, virtualType } = await request({ method: "start" }));
|
|
51
107
|
} catch (error) {
|
|
108
|
+
await worker.terminate();
|
|
52
109
|
if (
|
|
53
110
|
error instanceof Error &&
|
|
54
111
|
error.message.startsWith("[elysia-aot]") &&
|
|
@@ -59,20 +116,33 @@ export function elysiaAot(entry: string): Bun.BunPlugin {
|
|
|
59
116
|
}
|
|
60
117
|
throw error;
|
|
61
118
|
}
|
|
119
|
+
build.onEnd(async () => {
|
|
120
|
+
try {
|
|
121
|
+
await request({ method: "end" });
|
|
122
|
+
} finally {
|
|
123
|
+
await worker.terminate();
|
|
124
|
+
}
|
|
125
|
+
});
|
|
62
126
|
const entrySource = await Bun.file(entryPath).text();
|
|
63
|
-
const transformedEntry = await
|
|
127
|
+
const { code: transformedEntry } = await request({
|
|
128
|
+
code: entrySource,
|
|
129
|
+
method: "transform",
|
|
130
|
+
path: entryPath,
|
|
131
|
+
});
|
|
64
132
|
if (transformedEntry !== undefined) {
|
|
65
133
|
await Bun.write(entryPath, transformedEntry);
|
|
66
134
|
}
|
|
67
|
-
build.onResolve({ filter:
|
|
135
|
+
build.onResolve({ filter: ELYSIA_IMPORT_FILTER }, ({ path }) => ({
|
|
68
136
|
path: Bun.resolveSync(path, ELYSIA_RESOLVE_DIR),
|
|
69
137
|
}));
|
|
70
|
-
build.onResolve({ filter:
|
|
71
|
-
|
|
72
|
-
|
|
138
|
+
build.onResolve({ filter: ELYSIA_VIRTUAL_IMPORT_FILTER }, ({ path }) => {
|
|
139
|
+
if (path === "elysia/type" && virtualType === undefined) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
return { namespace: AOT_NAMESPACE, path };
|
|
73
143
|
});
|
|
74
|
-
build.onLoad({ filter:
|
|
75
|
-
const contents =
|
|
144
|
+
build.onLoad({ filter: ANY_MODULE_FILTER, namespace: AOT_NAMESPACE }, ({ path }) => {
|
|
145
|
+
const contents = path === "elysia/compiled" ? compiled : virtualType;
|
|
76
146
|
return contents === undefined
|
|
77
147
|
? undefined
|
|
78
148
|
: { contents, loader: "js", resolveDir: ELYSIA_RESOLVE_DIR };
|
|
@@ -81,11 +151,8 @@ export function elysiaAot(entry: string): Bun.BunPlugin {
|
|
|
81
151
|
if (resolve(path) === entryPath) {
|
|
82
152
|
return;
|
|
83
153
|
}
|
|
84
|
-
if (hooks.transformInclude?.(path) === false) {
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
154
|
const original = await Bun.file(path).text();
|
|
88
|
-
const transformed = await
|
|
155
|
+
const { code: transformed } = await request({ code: original, method: "transform", path });
|
|
89
156
|
if (transformed === undefined) {
|
|
90
157
|
return;
|
|
91
158
|
}
|
|
@@ -94,9 +161,6 @@ export function elysiaAot(entry: string): Bun.BunPlugin {
|
|
|
94
161
|
loader: loaderFor(path),
|
|
95
162
|
};
|
|
96
163
|
});
|
|
97
|
-
build.onEnd(() => {
|
|
98
|
-
hooks.buildEnd?.();
|
|
99
|
-
});
|
|
100
164
|
},
|
|
101
165
|
};
|
|
102
166
|
}
|
package/src/build/hydrate.ts
CHANGED
|
@@ -31,6 +31,7 @@ export function generateHydrateEntry(
|
|
|
31
31
|
basePath: string,
|
|
32
32
|
clientLogging: boolean
|
|
33
33
|
): string {
|
|
34
|
+
const rootDirectory = rootLayout.replaceAll("\\", "/").split("/").slice(0, -1).join("/");
|
|
34
35
|
const getBoundaryIdent = (idents: Map<string, string>, filePath: string | undefined) => {
|
|
35
36
|
if (!filePath) {
|
|
36
37
|
return;
|
|
@@ -85,6 +86,9 @@ export function generateHydrateEntry(
|
|
|
85
86
|
if (notFoundIdent) {
|
|
86
87
|
parts.push(`notFound: ${notFoundIdent}.default`);
|
|
87
88
|
}
|
|
89
|
+
if (seg.depth === 0 && seg.path.replaceAll("\\", "/") !== rootDirectory) {
|
|
90
|
+
parts.push("isRoot: false");
|
|
91
|
+
}
|
|
88
92
|
boundaryLiterals.push(`{ ${parts.join(", ")} }`);
|
|
89
93
|
}
|
|
90
94
|
|
|
@@ -70,12 +70,8 @@ export function buildPageElement(
|
|
|
70
70
|
});
|
|
71
71
|
|
|
72
72
|
// Reconstruct the FULL route chain (shallow→deep, index 0 = root) by walking
|
|
73
|
-
// parents.
|
|
74
|
-
//
|
|
75
|
-
// `buildElement` exactly, which is what guarantees the per-segment boundaries
|
|
76
|
-
// and layouts attach at the same depths on both sides (hydration parity).
|
|
77
|
-
// Compacting to layouts-only (the previous approach) misaligned boundaries
|
|
78
|
-
// and dropped the first nested layout whenever an ancestor lacked a layout.
|
|
73
|
+
// parents. Boundary depths refer to these indices, including routes without
|
|
74
|
+
// layouts, so server and client attach boundaries at the same positions.
|
|
79
75
|
const chain: RuntimeRoute[] = [];
|
|
80
76
|
let current: RuntimeRoute | undefined = match.pageRoute;
|
|
81
77
|
while (current) {
|
|
@@ -85,9 +81,11 @@ export function buildPageElement(
|
|
|
85
81
|
|
|
86
82
|
// Index boundaries by depth for O(1) lookup. A boundary's `depth` maps 1:1 to
|
|
87
83
|
// the route-chain index (depth 0 = root layout, handled separately below).
|
|
88
|
-
const byDepth = new Map<number, ClientSegmentBoundary>();
|
|
84
|
+
const byDepth = new Map<number, ClientSegmentBoundary[]>();
|
|
89
85
|
for (const segment of match.segmentBoundaries ?? []) {
|
|
90
|
-
byDepth.
|
|
86
|
+
const boundaries = byDepth.get(segment.depth) ?? [];
|
|
87
|
+
boundaries.unshift(segment);
|
|
88
|
+
byDepth.set(segment.depth, boundaries);
|
|
91
89
|
}
|
|
92
90
|
|
|
93
91
|
// When a root route is present it occupies chain index 0 and is wrapped
|
|
@@ -101,7 +99,9 @@ export function buildPageElement(
|
|
|
101
99
|
// boundary (so the boundary sits INSIDE the layout), then wrap with the
|
|
102
100
|
// layout itself when this route declares one.
|
|
103
101
|
for (let i = chain.length - 1; i >= rootOffset; i -= 1) {
|
|
104
|
-
|
|
102
|
+
for (const segment of byDepth.get(i) ?? []) {
|
|
103
|
+
element = wrapSegmentBoundaries(element, segment, options);
|
|
104
|
+
}
|
|
105
105
|
const Layout = chain[i]?.layout;
|
|
106
106
|
if (Layout) {
|
|
107
107
|
element = createElement(Layout as ElementType, data, element);
|
|
@@ -110,7 +110,9 @@ export function buildPageElement(
|
|
|
110
110
|
|
|
111
111
|
if (root) {
|
|
112
112
|
// Depth 0 boundary wraps EVERYTHING below the root layout.
|
|
113
|
-
|
|
113
|
+
for (const segment of byDepth.get(0) ?? []) {
|
|
114
|
+
element = wrapSegmentBoundaries(element, segment, options);
|
|
115
|
+
}
|
|
114
116
|
if (root.layout) {
|
|
115
117
|
element = createElement(root.layout as ElementType, data, element);
|
|
116
118
|
}
|
|
@@ -281,14 +281,14 @@ export function RouterProvider({
|
|
|
281
281
|
const resolvedSearch = (state.data.query as SearchParamsInput | undefined) ?? EMPTY_SEARCH;
|
|
282
282
|
|
|
283
283
|
/**
|
|
284
|
-
*
|
|
284
|
+
* Root-owned boundary for the "no client route matched" 404
|
|
285
285
|
* path. When an unknown URL is navigated to, the server's `renderRootNotFound`
|
|
286
286
|
* handles it — the root `not-found.tsx` (depth 0) is the correct component to
|
|
287
287
|
* render inline instead of doing a full-page reload.
|
|
288
288
|
*/
|
|
289
289
|
const rootBoundaries = useMemo<ClientSegmentBoundary[] | undefined>(() => {
|
|
290
290
|
for (const route of routes) {
|
|
291
|
-
const depth0 = route.segmentBoundaries?.find((b) => b.depth === 0);
|
|
291
|
+
const depth0 = route.segmentBoundaries?.find((b) => b.depth === 0 && b.isRoot !== false);
|
|
292
292
|
if (depth0) {
|
|
293
293
|
return [depth0];
|
|
294
294
|
}
|
|
@@ -216,6 +216,8 @@ export interface RouterContextValue {
|
|
|
216
216
|
export interface ClientSegmentBoundary {
|
|
217
217
|
depth: number;
|
|
218
218
|
error?: ErrorComponent;
|
|
219
|
+
/** False when a pathless directory shares index 0 but does not own the root fallback. */
|
|
220
|
+
isRoot?: boolean;
|
|
219
221
|
notFound?: NotFoundComponent;
|
|
220
222
|
}
|
|
221
223
|
|
package/src/plugin/routes.ts
CHANGED
|
@@ -12,7 +12,13 @@ import { pathToFileURL } from "node:url";
|
|
|
12
12
|
import { type AnyElysia, Elysia } from "elysia";
|
|
13
13
|
import { type FurinNativeRouteContext, getFurinRenderer } from "../define-route.ts";
|
|
14
14
|
import { detectLoaderFromPath } from "../server/lang-detect.ts";
|
|
15
|
-
import {
|
|
15
|
+
import {
|
|
16
|
+
filePathToPattern,
|
|
17
|
+
isRouteGroup,
|
|
18
|
+
parseDynamicRouteSegment,
|
|
19
|
+
routePatternKey,
|
|
20
|
+
routeSegmentToPattern,
|
|
21
|
+
} from "../server/router/patterns.ts";
|
|
16
22
|
import { routeModuleSourceVersion } from "../server/router/source-version.ts";
|
|
17
23
|
|
|
18
24
|
const ROUTES_NAMESPACE_PREFIX = "furin-routes";
|
|
@@ -118,7 +124,10 @@ function scanRouteFiles(
|
|
|
118
124
|
directorySegments: string[],
|
|
119
125
|
files: ScannedRouteFile[]
|
|
120
126
|
): void {
|
|
121
|
-
|
|
127
|
+
const entries = readdirSync(directory, { withFileTypes: true }).sort((left, right) =>
|
|
128
|
+
left.name.localeCompare(right.name)
|
|
129
|
+
);
|
|
130
|
+
for (const entry of entries) {
|
|
122
131
|
if (entry.isDirectory()) {
|
|
123
132
|
// Underscore-prefixed directories are co-located private folders
|
|
124
133
|
// (components, libs) — never route segments.
|
|
@@ -157,10 +166,6 @@ function segmentPath(segment: string): string {
|
|
|
157
166
|
return routeSegmentToPattern(segment);
|
|
158
167
|
}
|
|
159
168
|
|
|
160
|
-
function routePath(segments: string[]): string {
|
|
161
|
-
return segments.length === 0 ? "/" : `/${segments.map(segmentPath).join("/")}`;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
169
|
function routeId(instanceId: string, path: string): string {
|
|
165
170
|
const encodedPath = Array.from(new TextEncoder().encode(path), (byte) =>
|
|
166
171
|
byte.toString(16).padStart(2, "0")
|
|
@@ -193,18 +198,25 @@ function buildRouteTree(pagesDir: string, instanceId: string): RouteTreeNode {
|
|
|
193
198
|
return node;
|
|
194
199
|
};
|
|
195
200
|
const root = ensureNode([]);
|
|
201
|
+
const seenPatterns = new Map<string, string>();
|
|
196
202
|
|
|
197
203
|
for (const file of scannedFiles) {
|
|
198
204
|
const node = ensureNode(file.directorySegments);
|
|
199
205
|
if (file.base === "_route") {
|
|
200
|
-
const path =
|
|
206
|
+
const path = `/${file.directorySegments.map(segmentPath).join("/")}#layout`;
|
|
201
207
|
node.layout = { id: routeId(instanceId, path), path: "", sourcePath: file.sourcePath };
|
|
202
208
|
continue;
|
|
203
209
|
}
|
|
204
|
-
const path =
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
210
|
+
const path = filePathToPattern(relative(pagesDir, file.sourcePath));
|
|
211
|
+
const patternKey = routePatternKey(path);
|
|
212
|
+
const relativePath = relative(pagesDir, file.sourcePath).replaceAll("\\", "/");
|
|
213
|
+
const previousPath = seenPatterns.get(patternKey);
|
|
214
|
+
if (previousPath !== undefined) {
|
|
215
|
+
throw new Error(
|
|
216
|
+
`[furin] Duplicate route pattern "${patternKey}" from "${previousPath}" and "${relativePath}".`
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
seenPatterns.set(patternKey, relativePath);
|
|
208
220
|
const route = { id: routeId(instanceId, path), path, sourcePath: file.sourcePath };
|
|
209
221
|
if (file.base === "index") {
|
|
210
222
|
node.indexRoute = route;
|
|
@@ -547,7 +559,7 @@ function emitRouteNode(
|
|
|
547
559
|
includeRootCatchAll: boolean
|
|
548
560
|
): string {
|
|
549
561
|
const childIndentation = `${indentation} `;
|
|
550
|
-
const prefix = node.name ? `/${segmentPath(node.name)}` : "";
|
|
562
|
+
const prefix = node.name && !isRouteGroup(node.name) ? `/${segmentPath(node.name)}` : "";
|
|
551
563
|
const head = `new Elysia({ prefix: ${JSON.stringify(prefix)} })`;
|
|
552
564
|
const content: string[] = [];
|
|
553
565
|
if (node.indexRoute) {
|
|
@@ -905,7 +917,7 @@ async function composableRouteApps(route: RouteFile): Promise<DevRoutesApps | un
|
|
|
905
917
|
}
|
|
906
918
|
|
|
907
919
|
async function composeRuntimeNode(node: RouteTreeNode): Promise<DevRoutesApps> {
|
|
908
|
-
const prefix = node.name ? `/${segmentPath(node.name)}` : "";
|
|
920
|
+
const prefix = node.name && !isRouteGroup(node.name) ? `/${segmentPath(node.name)}` : "";
|
|
909
921
|
const { fileRoutes } = node;
|
|
910
922
|
const [layoutApps, indexRouteApps, fileRouteApps, childApps] = await Promise.all([
|
|
911
923
|
node.layout ? composableRouteApps(node.layout) : undefined,
|