@teyik0/furin 0.3.0-alpha.1 → 0.4.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapter/bun.d.ts +1 -1
- package/dist/build/client.d.ts +2 -2
- package/dist/build/entry-template.d.ts +4 -0
- package/dist/build/entry-template.ts +17 -3
- package/dist/build/index.d.ts +1 -1
- package/dist/build/index.js +8288 -6629
- package/dist/build/route-types.d.ts +2 -22
- package/dist/build/shared.d.ts +1 -1
- package/dist/build/ssg-cache.d.ts +1 -1
- package/dist/build/types.d.ts +1 -1
- package/dist/cli/index.js +7862 -6168
- package/dist/client/boundaries.d.ts +2 -0
- package/dist/client/define-route.d.ts +35 -0
- package/dist/client/document.d.ts +26 -0
- package/dist/client/hmr.d.ts +10 -0
- package/dist/client/internal/runtime-types.d.ts +3 -1
- package/dist/client/link.js +1177 -894
- package/dist/client/router/index.d.ts +1 -1
- package/dist/client/router/link-utils.d.ts +9 -0
- package/dist/client/router/search/index.d.ts +3 -7
- package/dist/client/router/types.d.ts +32 -24
- package/dist/client.d.ts +4 -159
- package/dist/client.js +183 -21
- package/dist/config.d.ts +8 -8
- package/dist/config.js +8 -8
- package/dist/define-route.d.ts +1355 -0
- package/dist/furin.d.ts +17 -33
- package/dist/furin.js +13444 -10301
- package/dist/plugin/index.js +953 -244
- package/dist/plugin/route-config-autofix.d.ts +31 -0
- package/dist/plugin/routes.d.ts +27 -0
- package/dist/routes.d.ts +6 -0
- package/dist/routes.js +1 -0
- package/dist/rsc/build/index.d.ts +1 -1
- package/dist/rsc.js +3 -3
- package/dist/server/dev-page-plugin.d.ts +7 -5
- package/dist/server/devtools/instrumentation.d.ts +2 -2
- package/dist/server/devtools/instrumentation.production.d.ts +2 -2
- package/dist/server/devtools/plugin.d.ts +2 -2
- package/dist/server/internal.d.ts +3 -0
- package/dist/server/logger.d.ts +4 -0
- package/dist/server/render/assemble.d.ts +0 -9
- package/dist/server/render/document.d.ts +4 -0
- package/dist/server/render/element.d.ts +2 -1
- package/dist/server/render/index.d.ts +1 -1
- package/dist/server/render/index.ts +0 -1
- package/dist/server/render/isr.d.ts +1 -1
- package/dist/server/render/loaders.d.ts +1 -1
- package/dist/server/render/not-found.d.ts +1 -1
- package/dist/server/render/ppr-route.d.ts +1 -1
- package/dist/server/render/ssg.d.ts +1 -1
- package/dist/server/render/ssr.d.ts +7 -6
- package/dist/server/render/template.d.ts +4 -0
- package/dist/server/router/defined-route.d.ts +24 -0
- package/dist/server/router/hmr.d.ts +16 -8
- package/dist/server/router/patterns.d.ts +6 -0
- package/dist/server/router/plugin.d.ts +4 -3
- package/dist/server/router/schema-merge.d.ts +2 -2
- package/dist/server/router/schemas.d.ts +5 -5
- package/dist/server/router/source-version.d.ts +2 -0
- package/dist/server/router/types.d.ts +1 -0
- package/dist/server/sync/index.js +961 -806
- package/dist/server/sync/postgres/migrate.js +3 -2
- package/dist/shared/await.d.ts +6 -7
- package/dist/shared/elysia-contract.d.ts +41 -0
- package/dist/shared/route-map.d.ts +5 -0
- package/dist/shared/utils/index.d.ts +1 -3
- package/package.json +44 -41
- package/src/adapter/bun.ts +42 -22
- package/src/adapter/package.ts +20 -14
- package/src/adapter/static.ts +4 -3
- package/src/build/client.ts +20 -3
- package/src/build/entry-template.ts +17 -3
- package/src/build/hydrate.ts +98 -28
- package/src/build/index.ts +2 -2
- package/src/build/route-types.ts +19 -114
- package/src/build/shared.ts +1 -1
- package/src/build/ssg-cache.ts +2 -5
- package/src/build/types.ts +1 -1
- package/src/cli/config.ts +3 -3
- package/src/cli/index.ts +7 -7
- package/src/client/boundaries.tsx +9 -1
- package/src/client/define-route.ts +48 -0
- package/src/client/document.tsx +165 -0
- package/src/client/hmr.ts +40 -0
- package/src/client/internal/runtime-types.ts +3 -1
- package/src/client/link.tsx +21 -6
- package/src/client/router/index.ts +1 -0
- package/src/client/router/link-utils.ts +26 -0
- package/src/client/router/provider.tsx +25 -2
- package/src/client/router/search/index.ts +4 -12
- package/src/client/router/types.ts +50 -24
- package/src/client.ts +14 -316
- package/src/config.ts +10 -10
- package/src/define-route.ts +959 -0
- package/src/devtools/devtools-element.js +11 -10
- package/src/furin.ts +303 -53
- package/src/plugin/route-config-autofix.ts +1140 -0
- package/src/plugin/routes.ts +891 -0
- package/src/plugin/transform-client.ts +132 -266
- package/src/plugin/transform-isomorphic.ts +1 -1
- package/src/routes.ts +6 -0
- package/src/rsc/build/index.ts +1 -1
- package/src/server/auto-invalidate/registry.ts +2 -2
- package/src/server/dev-page-plugin.ts +31 -12
- package/src/server/devtools/instrumentation.production.ts +2 -2
- package/src/server/devtools/instrumentation.ts +2 -2
- package/src/server/devtools/plugin.ts +5 -3
- package/src/server/instance.ts +3 -3
- package/src/server/internal.ts +3 -0
- package/src/server/logger.ts +21 -0
- package/src/server/render/assemble.ts +0 -41
- package/src/server/render/document.tsx +25 -0
- package/src/server/render/element.tsx +13 -1
- package/src/server/render/index.ts +0 -1
- package/src/server/render/isr.ts +31 -10
- package/src/server/render/loaders.ts +31 -6
- package/src/server/render/not-found.ts +51 -27
- package/src/server/render/ppr-route.ts +1 -1
- package/src/server/render/ssg.ts +1 -1
- package/src/server/render/ssr.ts +271 -113
- package/src/server/render/template.ts +91 -1
- package/src/server/router/defined-route.ts +110 -0
- package/src/server/router/discovery.ts +129 -29
- package/src/server/router/hmr.ts +111 -40
- package/src/server/router/patterns.ts +40 -12
- package/src/server/router/plugin.ts +63 -46
- package/src/server/router/schema-merge.ts +4 -14
- package/src/server/router/schemas.ts +11 -8
- package/src/server/router/source-version.ts +16 -0
- package/src/server/router/types.ts +2 -0
- package/src/server/sync/postgres/migrate.ts +3 -2
- package/src/server/sync/response.ts +14 -10
- package/src/shared/await.tsx +6 -7
- package/src/shared/elysia-contract.ts +63 -0
- package/src/shared/route-map.ts +37 -0
- package/src/shared/utils/index.ts +2 -20
- package/dist/build/server-routes-entry.d.ts +0 -13
- package/dist/build/server-routes-entry.ts +0 -30
- package/dist/server/router/index.d.ts +0 -7
- package/dist/server/router/index.js +0 -27
- package/dist/server/router/index.ts +0 -28
- package/src/build/server-routes-entry.ts +0 -30
- package/src/server/router/index.ts +0 -28
|
@@ -293,21 +293,22 @@ function installSyncObserver(sync) {
|
|
|
293
293
|
}
|
|
294
294
|
|
|
295
295
|
function refreshBundleEntries() {
|
|
296
|
-
browserState.bundleEntries =
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
}
|
|
302
|
-
.
|
|
296
|
+
browserState.bundleEntries = [];
|
|
297
|
+
for (const entry of performance.getEntriesByType("resource")) {
|
|
298
|
+
const url = new URL(entry.name, window.location.href);
|
|
299
|
+
if (url.origin !== window.location.origin || url.pathname.includes("/_furin/devtools/")) {
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
browserState.bundleEntries.push({
|
|
303
303
|
decodedBytes: entry.decodedBodySize,
|
|
304
304
|
durationMs: entry.duration,
|
|
305
305
|
encodedBytes: entry.encodedBodySize,
|
|
306
|
-
name:
|
|
306
|
+
name: url.pathname,
|
|
307
307
|
transferredBytes: entry.transferSize,
|
|
308
308
|
type: entry.initiatorType || "resource",
|
|
309
|
-
})
|
|
310
|
-
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
browserState.bundleEntries.sort((left, right) => right.transferredBytes - left.transferredBytes);
|
|
311
312
|
}
|
|
312
313
|
|
|
313
314
|
function installResourceObserver() {
|
package/src/furin.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { existsSync } from "node:fs";
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
2
|
import { basename, dirname, join, resolve } from "node:path";
|
|
3
3
|
import { fileURLToPath } from "node:url";
|
|
4
4
|
import { staticPlugin } from "@elysiajs/static";
|
|
5
5
|
import { type AnyElysia, Elysia, file } from "elysia";
|
|
6
|
-
import {
|
|
6
|
+
import type { DrainContext, LoggerConfig } from "evlog";
|
|
7
7
|
import { type EvlogElysiaOptions, evlog } from "evlog/elysia";
|
|
8
|
+
import { FURIN_RENDER_DECORATOR, type FurinRouteDispatcher } from "./define-route.ts";
|
|
8
9
|
import { consumePendingInvalidations } from "./server/cache/invalidation.ts";
|
|
9
10
|
import { setSSGCache } from "./server/cache/ssg.ts";
|
|
10
11
|
import {
|
|
@@ -27,6 +28,7 @@ import {
|
|
|
27
28
|
} from "./server/instance.ts";
|
|
28
29
|
import type { CompileContext, EmbeddedAppData } from "./server/internal.ts";
|
|
29
30
|
import { getCompileContext } from "./server/internal.ts";
|
|
31
|
+
import { initializeFurinLogger } from "./server/logger.ts";
|
|
30
32
|
import { renderRootNotFound } from "./server/render/not-found.ts";
|
|
31
33
|
import { warmSSGCache } from "./server/render/ssg.ts";
|
|
32
34
|
import {
|
|
@@ -34,8 +36,16 @@ import {
|
|
|
34
36
|
setProductionTemplatePath,
|
|
35
37
|
} from "./server/render/template.ts";
|
|
36
38
|
import { loadProdRoutes } from "./server/router/discovery.ts";
|
|
37
|
-
import {
|
|
38
|
-
import {
|
|
39
|
+
import { invalidateStampedRouteModules } from "./server/router/hmr.ts";
|
|
40
|
+
import { buildRouteMatcher } from "./server/router/patterns.ts";
|
|
41
|
+
import { createDataEndpoint, renderResolvedRoute } from "./server/router/plugin.ts";
|
|
42
|
+
import { mergeRouteSchemas } from "./server/router/schema-merge.ts";
|
|
43
|
+
import {
|
|
44
|
+
createSearchRouteMetadata,
|
|
45
|
+
parseRouteParams,
|
|
46
|
+
parseRouteQuery,
|
|
47
|
+
} from "./server/router/schemas.ts";
|
|
48
|
+
import type { ResolvedRoute, RootLayout } from "./server/router/types.ts";
|
|
39
49
|
import { IS_DEV } from "./server/runtime-env.ts";
|
|
40
50
|
import { type FurinSyncOption, resolveSyncStreamPath } from "./server/sync/config.ts";
|
|
41
51
|
import { createSyncStreamPlugin } from "./server/sync/stream.ts";
|
|
@@ -48,6 +58,14 @@ export type CacheTag = keyof FurinCacheTags extends never ? string : keyof Furin
|
|
|
48
58
|
import { clientDirNameForPrefix } from "./shared/prefix.ts";
|
|
49
59
|
|
|
50
60
|
// biome-ignore lint/performance/noBarrelFile: furin.ts is the public package entry
|
|
61
|
+
export {
|
|
62
|
+
type DefineRouteConfig,
|
|
63
|
+
defineRootRoute,
|
|
64
|
+
defineRoute,
|
|
65
|
+
type RequestLoaderContext,
|
|
66
|
+
type RouteLoaderData,
|
|
67
|
+
type RouteParams,
|
|
68
|
+
} from "./define-route.ts";
|
|
51
69
|
export {
|
|
52
70
|
type ClientIsomorphicFn,
|
|
53
71
|
createIsomorphicFn,
|
|
@@ -59,6 +77,7 @@ export { clientDirNameForPrefix } from "./shared/prefix.ts";
|
|
|
59
77
|
|
|
60
78
|
const MAX_BROWSER_INGEST_BYTES = 64 * 1024;
|
|
61
79
|
const MAX_BROWSER_INGEST_EVENTS = 100;
|
|
80
|
+
const DEV_ROUTE_TOPOLOGY_POLL_INTERVAL_MS = 100;
|
|
62
81
|
|
|
63
82
|
function resolveClientDirFromArgv(prefix: string): string {
|
|
64
83
|
const dirName = clientDirNameForPrefix(prefix);
|
|
@@ -320,6 +339,15 @@ function createLoggerPlugin(
|
|
|
320
339
|
) as unknown as Elysia;
|
|
321
340
|
}
|
|
322
341
|
|
|
342
|
+
function initializeLogger(logger: FurinLoggerOptions | undefined): EvlogElysiaOptions {
|
|
343
|
+
const { sampling, ...elysiaLoggerOptions } = logger ?? {};
|
|
344
|
+
initializeFurinLogger({
|
|
345
|
+
env: { service: "furin" },
|
|
346
|
+
...(sampling ? { sampling } : {}),
|
|
347
|
+
});
|
|
348
|
+
return elysiaLoggerOptions;
|
|
349
|
+
}
|
|
350
|
+
|
|
323
351
|
/**
|
|
324
352
|
* Registers a HigherOrderFunction on the Elysia instance so that every request
|
|
325
353
|
* runs inside a fresh instance-bound `AsyncLocalStorage` scope. This isolates
|
|
@@ -356,11 +384,106 @@ function wrapWithRequestScope(app: AnyElysia): Elysia {
|
|
|
356
384
|
});
|
|
357
385
|
}
|
|
358
386
|
|
|
387
|
+
function createFurinPlugin(
|
|
388
|
+
app: AnyElysia,
|
|
389
|
+
prepareParent: ((parentApp: AnyElysia) => void) | undefined
|
|
390
|
+
) {
|
|
391
|
+
const scopedApp = wrapWithRequestScope(app);
|
|
392
|
+
return <ParentApp extends AnyElysia>(parentApp: ParentApp) => {
|
|
393
|
+
prepareParent?.(parentApp);
|
|
394
|
+
return parentApp.use(scopedApp);
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
|
|
359
398
|
async function loadDevelopmentRoutes(resolvedPagesDir: string) {
|
|
360
|
-
const { scanPages } = await import("./server/router/
|
|
399
|
+
const { scanPages } = await import("./server/router/discovery.ts");
|
|
361
400
|
return scanPages(resolvedPagesDir);
|
|
362
401
|
}
|
|
363
402
|
|
|
403
|
+
function createNativeRouteRenderer(
|
|
404
|
+
prefix: string,
|
|
405
|
+
routes: ResolvedRoute[],
|
|
406
|
+
root: RootLayout,
|
|
407
|
+
buildId: string,
|
|
408
|
+
searchRoutes: ReturnType<typeof createSearchRouteMetadata>
|
|
409
|
+
): FurinRouteDispatcher {
|
|
410
|
+
const matchNativeRoute = buildRouteMatcher(routes);
|
|
411
|
+
return async (context) => {
|
|
412
|
+
const { request } = context;
|
|
413
|
+
const requestUrl = new URL(request.url);
|
|
414
|
+
const { pathname } = requestUrl;
|
|
415
|
+
const hasPrefix = prefix !== "" && (pathname === prefix || pathname.startsWith(`${prefix}/`));
|
|
416
|
+
const logicalPath = hasPrefix ? pathname.slice(prefix.length) : pathname;
|
|
417
|
+
const matched = matchNativeRoute(logicalPath || "/");
|
|
418
|
+
if (!matched) {
|
|
419
|
+
// Dev topology swap: the mounted Elysia route can outlive its source
|
|
420
|
+
// file (hot-remove). Render the root not-found page instead of failing.
|
|
421
|
+
const listenerOrigin = (context.server as { url?: { origin: string } } | undefined)?.url
|
|
422
|
+
?.origin;
|
|
423
|
+
return renderRootNotFound(root, request, listenerOrigin);
|
|
424
|
+
}
|
|
425
|
+
const parsedParams = await parseRouteParams(
|
|
426
|
+
matched.params,
|
|
427
|
+
mergeRouteSchemas(matched.route.routeChain, "params")
|
|
428
|
+
);
|
|
429
|
+
if (!parsedParams.ok) {
|
|
430
|
+
return Response.json(
|
|
431
|
+
{ errors: parsedParams.errors, message: "Invalid params", type: "validation" },
|
|
432
|
+
{ status: 422 }
|
|
433
|
+
);
|
|
434
|
+
}
|
|
435
|
+
const parsedQuery = await parseRouteQuery(
|
|
436
|
+
requestUrl,
|
|
437
|
+
mergeRouteSchemas(matched.route.routeChain, "query")
|
|
438
|
+
);
|
|
439
|
+
if (!parsedQuery.ok) {
|
|
440
|
+
return Response.json(
|
|
441
|
+
{ errors: parsedQuery.errors, message: "Invalid query", type: "validation" },
|
|
442
|
+
{ status: 422 }
|
|
443
|
+
);
|
|
444
|
+
}
|
|
445
|
+
context.params = parsedParams.params;
|
|
446
|
+
context.query = parsedQuery.query;
|
|
447
|
+
return renderResolvedRoute(
|
|
448
|
+
matched.route,
|
|
449
|
+
context as unknown as Parameters<typeof renderResolvedRoute>[1],
|
|
450
|
+
root,
|
|
451
|
+
buildId,
|
|
452
|
+
searchRoutes
|
|
453
|
+
);
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
interface DevelopmentRouteSnapshot {
|
|
458
|
+
render: FurinRouteDispatcher;
|
|
459
|
+
root: RootLayout;
|
|
460
|
+
routes: ResolvedRoute[];
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
function createDevelopmentRouteSnapshot(
|
|
464
|
+
prefix: string,
|
|
465
|
+
root: RootLayout,
|
|
466
|
+
routes: ResolvedRoute[]
|
|
467
|
+
): DevelopmentRouteSnapshot {
|
|
468
|
+
const searchRoutes = createSearchRouteMetadata(routes);
|
|
469
|
+
return {
|
|
470
|
+
render: createNativeRouteRenderer(prefix, routes, root, "", searchRoutes),
|
|
471
|
+
root,
|
|
472
|
+
routes,
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
const nativeRouteRenderers = new WeakMap<FurinInstance, FurinRouteDispatcher>();
|
|
477
|
+
|
|
478
|
+
function dispatchNativeRoute(context: Parameters<FurinRouteDispatcher>[0]): unknown {
|
|
479
|
+
const { pathname } = new URL(context.request.url);
|
|
480
|
+
const renderer = nativeRouteRenderers.get(resolveInstanceByPath(pathname));
|
|
481
|
+
if (!renderer) {
|
|
482
|
+
throw new Error(`[furin] No route renderer is registered for ${JSON.stringify(pathname)}`);
|
|
483
|
+
}
|
|
484
|
+
return renderer(context);
|
|
485
|
+
}
|
|
486
|
+
|
|
364
487
|
function hydrateSSGCacheFromCompileContext(ctx: CompileContext): void {
|
|
365
488
|
if (!ctx.ssgCache) {
|
|
366
489
|
return;
|
|
@@ -371,6 +494,8 @@ function hydrateSSGCacheFromCompileContext(ctx: CompileContext): void {
|
|
|
371
494
|
}
|
|
372
495
|
|
|
373
496
|
/** Options for the {@link furin} plugin. */
|
|
497
|
+
export type FurinLoggerOptions = EvlogElysiaOptions & Pick<LoggerConfig, "sampling">;
|
|
498
|
+
|
|
374
499
|
export interface FurinOptions {
|
|
375
500
|
/**
|
|
376
501
|
* Production only: explicit directory holding this app's built client
|
|
@@ -386,7 +511,7 @@ export interface FurinOptions {
|
|
|
386
511
|
* and unaffected.
|
|
387
512
|
*/
|
|
388
513
|
clientLogging?: boolean;
|
|
389
|
-
logger?:
|
|
514
|
+
logger?: FurinLoggerOptions;
|
|
390
515
|
pagesDir?: string;
|
|
391
516
|
/**
|
|
392
517
|
* Mount prefix for this app, e.g. `"/admin"`. All pages, framework
|
|
@@ -406,8 +531,9 @@ export interface FurinOptions {
|
|
|
406
531
|
/**
|
|
407
532
|
* Main Furin plugin.
|
|
408
533
|
*
|
|
409
|
-
* Returns
|
|
410
|
-
*
|
|
534
|
+
* Returns an Elysia plugin function. Applying the function to the parent app
|
|
535
|
+
* before `listen()` lets development register Bun's native HTML bundle in the
|
|
536
|
+
* initial server options while preserving Elysia's route composition.
|
|
411
537
|
*
|
|
412
538
|
* ## Usage
|
|
413
539
|
*
|
|
@@ -428,7 +554,7 @@ export async function furin({
|
|
|
428
554
|
}: FurinOptions = {}) {
|
|
429
555
|
const prefix = normalizePrefix(rawPrefix);
|
|
430
556
|
const syncStreamPath = resolveSyncStreamPath(sync);
|
|
431
|
-
|
|
557
|
+
const elysiaLoggerOptions = initializeLogger(logger);
|
|
432
558
|
|
|
433
559
|
const cwd = process.cwd();
|
|
434
560
|
// The pagesDir param drives which compile context this instance loads. In a
|
|
@@ -439,7 +565,7 @@ export async function furin({
|
|
|
439
565
|
const loggerPlugin = createLoggerPlugin(
|
|
440
566
|
prefix,
|
|
441
567
|
syncStreamPath,
|
|
442
|
-
|
|
568
|
+
elysiaLoggerOptions,
|
|
443
569
|
clientLogging === true || ctx?.clientLogging === true
|
|
444
570
|
);
|
|
445
571
|
const resolvedPagesDir = ctx?.rootPath ? dirname(ctx.rootPath) : paramPagesDir;
|
|
@@ -466,42 +592,124 @@ export async function furin({
|
|
|
466
592
|
// Lazy import — build pipeline has native deps not available in compiled binaries
|
|
467
593
|
const { registerDevPagePlugin } = await import("./server/dev-page-plugin.ts");
|
|
468
594
|
registerDevPagePlugin();
|
|
595
|
+
const {
|
|
596
|
+
registerDevRoutesPlugin,
|
|
597
|
+
registerDevRouteTopologyWatcher,
|
|
598
|
+
routeModuleSpecifier,
|
|
599
|
+
routeSourcePaths,
|
|
600
|
+
} = await import("./plugin/routes.ts");
|
|
601
|
+
const routeInstance = { pagesDir: resolvedPagesDir, prefix };
|
|
602
|
+
registerDevRoutesPlugin([routeInstance]);
|
|
603
|
+
|
|
604
|
+
// TanStack-style config auto-fix: verify each route file's `layout`
|
|
605
|
+
// reference against the file-system tree and rewrite missing/misplaced
|
|
606
|
+
// ones. Idempotent and content-diff safe — the watcher resynchronizes the
|
|
607
|
+
// route-file signature after a rewrite, so the auto-fix cannot loop.
|
|
608
|
+
const { fixRouteConfigLayout } = await import("./plugin/route-config-autofix.ts");
|
|
609
|
+
const applyRouteConfigAutofix = (): boolean => {
|
|
610
|
+
let changed = false;
|
|
611
|
+
const sourcePaths = [
|
|
612
|
+
join(resolvedPagesDir, "root.tsx"),
|
|
613
|
+
...routeSourcePaths(routeInstance),
|
|
614
|
+
].filter((sourcePath) => existsSync(sourcePath));
|
|
615
|
+
for (const sourcePath of sourcePaths) {
|
|
616
|
+
let source: string;
|
|
617
|
+
try {
|
|
618
|
+
source = readFileSync(sourcePath, "utf8");
|
|
619
|
+
} catch (error) {
|
|
620
|
+
console.warn(
|
|
621
|
+
`[furin] Could not read ${sourcePath} for route config autofix: ${error instanceof Error ? error.message : String(error)}`
|
|
622
|
+
);
|
|
623
|
+
continue;
|
|
624
|
+
}
|
|
625
|
+
const fixed = fixRouteConfigLayout(source, sourcePath, resolvedPagesDir);
|
|
626
|
+
if (fixed !== null && fixed !== source) {
|
|
627
|
+
writeFileSync(sourcePath, fixed);
|
|
628
|
+
changed = true;
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
return changed;
|
|
632
|
+
};
|
|
633
|
+
applyRouteConfigAutofix();
|
|
634
|
+
|
|
635
|
+
const { furinShell: nativeRoutesApp } = (await import(routeModuleSpecifier(routeInstance))) as {
|
|
636
|
+
furinShell: AnyElysia;
|
|
637
|
+
};
|
|
469
638
|
const { root, routes } = await loadDevelopmentRoutes(resolvedPagesDir);
|
|
470
|
-
|
|
639
|
+
let currentSnapshot = createDevelopmentRouteSnapshot(prefix, root, routes);
|
|
640
|
+
nativeRouteRenderers.set(instance, (context) => currentSnapshot.render(context));
|
|
471
641
|
|
|
472
642
|
const { writeDevFiles } = await import("./build/hydrate.ts");
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
643
|
+
const writeCurrentDevFiles = (snapshot: DevelopmentRouteSnapshot): void => {
|
|
644
|
+
writeDevFiles(
|
|
645
|
+
snapshot.routes,
|
|
646
|
+
{
|
|
647
|
+
basePath: prefix,
|
|
648
|
+
clientLogging: clientLogging ?? false,
|
|
649
|
+
outDir: furinDir,
|
|
650
|
+
publicPath: `${prefix}/_client/`,
|
|
651
|
+
rootLayout: snapshot.root.path,
|
|
652
|
+
// furin-env.d.ts is one file at the project root — only the root
|
|
653
|
+
// instance owns it, otherwise mounted apps clobber each other's types.
|
|
654
|
+
skipRouteTypes: prefix !== "",
|
|
655
|
+
},
|
|
656
|
+
cwd
|
|
657
|
+
);
|
|
658
|
+
};
|
|
659
|
+
writeCurrentDevFiles(currentSnapshot);
|
|
660
|
+
const refreshDevelopmentRoutes = async (): Promise<void> => {
|
|
661
|
+
invalidateStampedRouteModules();
|
|
662
|
+
const next = await loadDevelopmentRoutes(resolvedPagesDir);
|
|
663
|
+
const nextSnapshot = createDevelopmentRouteSnapshot(prefix, next.root, next.routes);
|
|
664
|
+
writeCurrentDevFiles(nextSnapshot);
|
|
665
|
+
currentSnapshot = nextSnapshot;
|
|
666
|
+
};
|
|
667
|
+
const devHtmlBundle = (await import(join(furinDir, "index.html"))).default;
|
|
488
668
|
const publicDir = resolve(cwd, "public");
|
|
489
669
|
const publicExists = existsSync(publicDir);
|
|
670
|
+
const hmrEntryPath = `${prefix}/_bun_hmr_entry`;
|
|
671
|
+
let routeTopologyWatcher: ReturnType<typeof registerDevRouteTopologyWatcher> | undefined;
|
|
490
672
|
|
|
491
673
|
// Routes registered below are LOGICAL — Elysia's `prefix` makes them
|
|
492
674
|
// physical when this plugin is merged into the parent app (child prefixes
|
|
493
675
|
// like staticPlugin's compose underneath).
|
|
494
676
|
const devApp = new Elysia({
|
|
495
677
|
name: instanceName,
|
|
496
|
-
seed: resolvedPagesDir,
|
|
497
678
|
prefix: prefix || undefined,
|
|
679
|
+
seed: resolvedPagesDir,
|
|
498
680
|
})
|
|
681
|
+
.onStart(() => {
|
|
682
|
+
routeTopologyWatcher = registerDevRouteTopologyWatcher({
|
|
683
|
+
instance: routeInstance,
|
|
684
|
+
onRouteFilesTouched: async () => {
|
|
685
|
+
applyRouteConfigAutofix();
|
|
686
|
+
await refreshDevelopmentRoutes();
|
|
687
|
+
},
|
|
688
|
+
onTopologyChange: async () => {
|
|
689
|
+
// Bun --hot cannot be triggered from generated artifacts (its
|
|
690
|
+
// watch graph is the entry's static imports), so topology changes
|
|
691
|
+
// are served by swapping the dispatcher's route matcher. Hot-added
|
|
692
|
+
// routes then resolve through the NOT_FOUND fallback below;
|
|
693
|
+
// removed routes 404 through the renderer's miss path.
|
|
694
|
+
applyRouteConfigAutofix();
|
|
695
|
+
await refreshDevelopmentRoutes();
|
|
696
|
+
},
|
|
697
|
+
pollIntervalMs: DEV_ROUTE_TOPOLOGY_POLL_INTERVAL_MS,
|
|
698
|
+
});
|
|
699
|
+
})
|
|
700
|
+
.onStop(() => {
|
|
701
|
+
routeTopologyWatcher?.close();
|
|
702
|
+
routeTopologyWatcher = undefined;
|
|
703
|
+
})
|
|
704
|
+
// Elysia 1.4.30 only preserves Bun's native HTMLBundle handler when the
|
|
705
|
+
// bundle is also present in serve.routes once request hooks are installed.
|
|
706
|
+
.use(await staticPlugin({ assets: furinDir, bunFullstack: true, prefix: "/_bun_hmr_entry" }))
|
|
499
707
|
.use(loggerPlugin)
|
|
500
708
|
// Local scope (default) — a global hook would leak onto sibling furin
|
|
501
709
|
// instances mounted on the same parent app.
|
|
502
710
|
.onError(async ({ code, request, server }) => {
|
|
503
711
|
if (code === "NOT_FOUND") {
|
|
504
|
-
return await renderRootNotFound(root, request, server?.url.origin);
|
|
712
|
+
return await renderRootNotFound(currentSnapshot.root, request, server?.url.origin);
|
|
505
713
|
}
|
|
506
714
|
})
|
|
507
715
|
.onAfterHandle(({ set }) => {
|
|
@@ -511,7 +719,6 @@ export async function furin({
|
|
|
511
719
|
set.headers["x-furin-revalidate"] = pending.join(",");
|
|
512
720
|
}
|
|
513
721
|
})
|
|
514
|
-
.use(await staticPlugin({ assets: furinDir, prefix: "/_bun_hmr_entry", bunFullstack: true }))
|
|
515
722
|
.use(
|
|
516
723
|
publicExists ? await staticPlugin({ assets: publicDir, prefix: "/public" }) : new Elysia()
|
|
517
724
|
)
|
|
@@ -521,18 +728,35 @@ export async function furin({
|
|
|
521
728
|
? file(join(publicDir, "favicon.ico"))
|
|
522
729
|
: () => new Response(null, { status: 404 })
|
|
523
730
|
)
|
|
524
|
-
.use(createInstrumentationPlugin(routes, syncStreamPath))
|
|
731
|
+
.use(createInstrumentationPlugin(() => currentSnapshot.routes, syncStreamPath))
|
|
525
732
|
.use(sync ? createSyncStreamPlugin(sync) : new Elysia())
|
|
526
|
-
.use(createDataEndpoint(routes))
|
|
527
|
-
.
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
733
|
+
.use(createDataEndpoint(() => currentSnapshot.routes))
|
|
734
|
+
.decorate(FURIN_RENDER_DECORATOR, dispatchNativeRoute)
|
|
735
|
+
.use(nativeRoutesApp)
|
|
736
|
+
.use(
|
|
737
|
+
createNotFoundHandling(prefix, routes, root, async (notFoundContext) => {
|
|
738
|
+
// Dev topology: try the (watcher-refreshed) native renderer before
|
|
739
|
+
// the root not-found page, so hot-added routes are served without
|
|
740
|
+
// a restart. Instances outside this pathname's prefix are skipped.
|
|
741
|
+
const { pathname } = new URL(notFoundContext.request.url);
|
|
742
|
+
if (!nativeRouteRenderers.has(resolveInstanceByPath(pathname))) {
|
|
743
|
+
return;
|
|
744
|
+
}
|
|
745
|
+
return await dispatchNativeRoute(
|
|
746
|
+
notFoundContext as unknown as Parameters<FurinRouteDispatcher>[0]
|
|
747
|
+
);
|
|
748
|
+
})
|
|
749
|
+
);
|
|
534
750
|
registerInstance(instance);
|
|
535
|
-
return
|
|
751
|
+
return createFurinPlugin(devApp, (parentApp) => {
|
|
752
|
+
parentApp.config.serve = {
|
|
753
|
+
...parentApp.config.serve,
|
|
754
|
+
routes: {
|
|
755
|
+
...parentApp.config.serve?.routes,
|
|
756
|
+
[hmrEntryPath]: devHtmlBundle,
|
|
757
|
+
},
|
|
758
|
+
};
|
|
759
|
+
});
|
|
536
760
|
}
|
|
537
761
|
|
|
538
762
|
// ── Production ──────────────────────────────────────────────────────────
|
|
@@ -542,6 +766,17 @@ export async function furin({
|
|
|
542
766
|
const { root, routes } = loadProdRoutes(ctx);
|
|
543
767
|
const searchRoutes = createSearchRouteMetadata(routes);
|
|
544
768
|
const prodBuildId = ctx.buildId ?? "";
|
|
769
|
+
if (!ctx.nativeRoutes) {
|
|
770
|
+
throw new Error("[furin] Production build is missing the composed Elysia route app.");
|
|
771
|
+
}
|
|
772
|
+
const renderNativeRoute = createNativeRouteRenderer(
|
|
773
|
+
prefix,
|
|
774
|
+
routes,
|
|
775
|
+
root,
|
|
776
|
+
prodBuildId,
|
|
777
|
+
searchRoutes
|
|
778
|
+
);
|
|
779
|
+
nativeRouteRenderers.set(instance, renderNativeRoute);
|
|
545
780
|
instance.buildId = prodBuildId;
|
|
546
781
|
// Init-time writes target THIS instance explicitly — with several mounted
|
|
547
782
|
// apps there is no ambient request scope to resolve it from.
|
|
@@ -556,8 +791,8 @@ export async function furin({
|
|
|
556
791
|
const clientAssetPrefix = `${prefix}/_client/`;
|
|
557
792
|
const prodApp = new Elysia({
|
|
558
793
|
name: instanceName,
|
|
559
|
-
seed: resolvedPagesDir,
|
|
560
794
|
prefix: prefix || undefined,
|
|
795
|
+
seed: resolvedPagesDir,
|
|
561
796
|
})
|
|
562
797
|
.use(loggerPlugin)
|
|
563
798
|
// Local scope (default) — a global hook would leak onto sibling furin
|
|
@@ -631,15 +866,11 @@ export async function furin({
|
|
|
631
866
|
)
|
|
632
867
|
.use(sync ? createSyncStreamPlugin(sync) : new Elysia())
|
|
633
868
|
.use(createDataEndpoint(routes))
|
|
634
|
-
.
|
|
635
|
-
|
|
636
|
-
(chain, route) => chain.use(createRoutePlugin(route, root, prodBuildId, searchRoutes)),
|
|
637
|
-
app
|
|
638
|
-
)
|
|
639
|
-
)
|
|
869
|
+
.decorate(FURIN_RENDER_DECORATOR, dispatchNativeRoute)
|
|
870
|
+
.use(ctx.nativeRoutes)
|
|
640
871
|
.use(createNotFoundHandling(prefix, routes, root));
|
|
641
872
|
registerInstance(instance);
|
|
642
|
-
return
|
|
873
|
+
return createFurinPlugin(prodApp, undefined);
|
|
643
874
|
}
|
|
644
875
|
|
|
645
876
|
/**
|
|
@@ -659,25 +890,45 @@ export async function furin({
|
|
|
659
890
|
function createNotFoundHandling(
|
|
660
891
|
prefix: string,
|
|
661
892
|
routes: Array<{ pattern: string }>,
|
|
662
|
-
root: Parameters<typeof renderRootNotFound>[0]
|
|
893
|
+
root: Parameters<typeof renderRootNotFound>[0],
|
|
894
|
+
tryNativeDispatch?: (context: { request: Request }) => Promise<unknown>
|
|
663
895
|
): Elysia {
|
|
664
896
|
const app = new Elysia();
|
|
665
897
|
if (prefix === "") {
|
|
666
|
-
app.onError({ as: "global" }, async (
|
|
667
|
-
if (code
|
|
668
|
-
return
|
|
898
|
+
app.onError({ as: "global" }, async (context) => {
|
|
899
|
+
if (context.code !== "NOT_FOUND") {
|
|
900
|
+
return;
|
|
901
|
+
}
|
|
902
|
+
// Dev topology: the native renderer is rebuilt by the watcher on route
|
|
903
|
+
// add/remove, so a hot-added route (no mounted Elysia route yet) can
|
|
904
|
+
// still be served here. `tryNativeDispatch` is undefined in production.
|
|
905
|
+
if (tryNativeDispatch) {
|
|
906
|
+
const rendered = await tryNativeDispatch(context);
|
|
907
|
+
if (rendered !== undefined && rendered !== null) {
|
|
908
|
+
return rendered;
|
|
909
|
+
}
|
|
669
910
|
}
|
|
911
|
+
return await renderRootNotFound(root, context.request, context.server?.url.origin);
|
|
670
912
|
});
|
|
671
913
|
return app;
|
|
672
914
|
}
|
|
673
915
|
if (routes.some((route) => route.pattern === "/*")) {
|
|
674
916
|
return app;
|
|
675
917
|
}
|
|
676
|
-
app.get("/*", ({ request, server }) =>
|
|
918
|
+
app.get("/*", async ({ request, server }) => {
|
|
919
|
+
if (tryNativeDispatch) {
|
|
920
|
+
const rendered = await tryNativeDispatch({ request });
|
|
921
|
+
if (rendered !== undefined && rendered !== null) {
|
|
922
|
+
return rendered;
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
return renderRootNotFound(root, request, server?.url.origin);
|
|
926
|
+
});
|
|
677
927
|
return app;
|
|
678
928
|
}
|
|
679
929
|
|
|
680
930
|
export { FurinErrorBoundary, FurinNotFoundBoundary } from "./client/boundaries.tsx";
|
|
931
|
+
export { HeadContent, Scripts } from "./client/document.tsx";
|
|
681
932
|
// ── Public API re-export ──────────────────────────────────────────────────────
|
|
682
933
|
// biome-ignore-start lint/performance/noBarrelFile: intentional — furin.ts is the public package entry
|
|
683
934
|
export type { DeferredData } from "./client.ts";
|
|
@@ -686,7 +937,6 @@ export type { InvalidationInput, InvalidationRule } from "./server/auto-invalida
|
|
|
686
937
|
export { furinInvalidate, revalidateTag } from "./server/auto-invalidate/index.ts";
|
|
687
938
|
export { revalidatePath, setCachePurger } from "./server/cache/invalidation.ts";
|
|
688
939
|
export { buildElement, buildErrorElement, renderRootNotFound } from "./server/render/index.ts";
|
|
689
|
-
export type { ResolvedRoute, SegmentBoundary } from "./server/router/index.ts";
|
|
690
940
|
export {
|
|
691
941
|
type FurinSyncOption,
|
|
692
942
|
type FurinSyncOptions,
|