@teyik0/furin 0.7.0-alpha.3 → 0.8.0-alpha.0
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/build.d.ts +9 -0
- package/dist/adapter/bun/server.d.ts +1 -0
- package/dist/adapter/bun.d.ts +7 -3
- package/dist/adapter/package.d.ts +1 -0
- package/dist/adapter/prerender.d.ts +21 -0
- package/dist/adapter/runtime-build.d.ts +24 -10
- package/dist/adapter/server-bundle.d.ts +20 -0
- package/dist/adapter/static.d.ts +2 -2
- package/dist/adapter/types.d.ts +81 -0
- package/dist/adapter/vercel/bootstrap.d.ts +5 -0
- package/dist/adapter/vercel/data.d.ts +23 -0
- package/dist/adapter/vercel/handler.d.ts +10 -0
- package/dist/adapter/vercel.d.ts +13 -3
- package/dist/adapter/vercel.js +516 -0
- package/dist/build/bun-build.d.ts +4 -0
- package/dist/build/compile-entry.ts +1 -0
- package/dist/build/discover-mounts-process.d.ts +1 -0
- package/dist/build/discover-mounts.d.ts +6 -0
- package/dist/build/elysia-aot-worker.d.ts +4 -0
- package/dist/build/elysia-aot.d.ts +7 -1
- package/dist/build/entry-template.d.ts +14 -7
- package/dist/build/entry-template.ts +33 -18
- package/dist/build/hydrate.d.ts +15 -2
- package/dist/build/index.d.ts +7 -3
- package/dist/build/index.js +8842 -15395
- package/dist/build/private-server-sourcemaps.d.ts +2 -0
- package/dist/build/route-types.d.ts +8 -2
- package/dist/build/server-app-entry.d.ts +2 -2
- package/dist/build/shared.d.ts +3 -5
- package/dist/build/types.d.ts +16 -16
- package/dist/cache.js +117 -32
- package/dist/cli/config.d.ts +2 -2
- package/dist/cli/index.js +8953 -15269
- package/dist/client/chunks/client-brdfd773.js +852 -0
- package/dist/client/chunks/client-k29262a4.js +59 -0
- package/dist/client/chunks/client-k88668k7.js +159 -0
- package/dist/client/chunks/client-xy6g38kx.js +317 -0
- package/dist/client/default-screens.d.ts +0 -11
- package/dist/client/define-route.d.ts +2 -1
- package/dist/client/hmr.d.ts +1 -1
- package/dist/client/internal/runtime-types.d.ts +7 -1
- package/dist/client/link.d.ts +1 -0
- package/dist/client/link.js +1790 -4190
- package/dist/client/mutation.d.ts +5 -0
- package/dist/client/query-store.d.ts +17 -7
- package/dist/client/query.d.ts +6 -8
- package/dist/client/request-id.d.ts +1 -0
- package/dist/client/route-api.d.ts +4 -4
- package/dist/client/route-element.d.ts +51 -0
- package/dist/client/router/boundary-tree.d.ts +1 -9
- package/dist/client/router/cache-generation.d.ts +15 -0
- package/dist/client/router/context.d.ts +13 -2
- package/dist/client/router/fetch-observers.d.ts +9 -0
- package/dist/client/router/hmr-refresh.d.ts +12 -0
- package/dist/client/router/index.d.ts +5 -7
- package/dist/client/router/link-utils.d.ts +0 -2
- package/dist/client/router/navigation-controller.d.ts +76 -0
- package/dist/client/router/navigation.d.ts +1 -0
- package/dist/client/router/page-fetch.d.ts +38 -0
- package/dist/client/router/provider.d.ts +2 -3
- package/dist/client/router/search/index.d.ts +11 -4
- package/dist/client/router/search/index.js +60 -0
- package/dist/client/router/spa-response.d.ts +9 -26
- package/dist/client/router/sync-catch-up.d.ts +3 -2
- package/dist/client/router/transport.d.ts +4 -0
- package/dist/client/router/types.d.ts +120 -46
- package/dist/client/router/use-fetch-interception.d.ts +12 -0
- package/dist/client/router/use-history-sync.d.ts +7 -0
- package/dist/client/router/use-hmr-refresh.d.ts +8 -0
- package/dist/client/router/use-sync-stream.d.ts +16 -0
- package/dist/client/server-error.d.ts +4 -7
- package/dist/client/sync.d.ts +43 -30
- package/dist/client.d.ts +2 -1
- package/dist/client.js +279 -829
- package/dist/config.d.ts +11 -12
- package/dist/config.js +0 -30
- package/dist/define-route.d.ts +262 -517
- package/dist/devtools/protocol.d.ts +4 -3
- package/dist/furin.d.ts +41 -31
- package/dist/furin.js +20560 -18970
- package/dist/plugin/binding-scope.d.ts +15 -2
- package/dist/plugin/client-plugin.d.ts +19 -0
- package/dist/plugin/dead-code-elimination.d.ts +14 -3
- package/dist/plugin/dev-routes.d.ts +22 -0
- package/dist/plugin/hmr-data-signature.d.ts +3 -0
- package/dist/plugin/hmr-hook-signature.d.ts +9 -0
- package/dist/plugin/index.d.ts +1 -6
- package/dist/plugin/index.js +1753 -1214
- package/dist/plugin/module-bindings.d.ts +11 -0
- package/dist/plugin/module-transform.d.ts +29 -0
- package/dist/plugin/preflight.d.ts +8 -0
- package/dist/plugin/route-chains.d.ts +35 -0
- package/dist/plugin/route-config.d.ts +28 -0
- package/dist/plugin/route-modules.d.ts +35 -0
- package/dist/plugin/route-registry.d.ts +12 -0
- package/dist/plugin/route-tree.d.ts +43 -0
- package/dist/plugin/strip-server-stages.d.ts +10 -0
- package/dist/plugin/strip-server.d.ts +10 -0
- package/dist/plugin/transform-client-module.d.ts +3 -0
- package/dist/plugin/transform-client.d.ts +0 -2
- package/dist/plugin/transform-isomorphic.d.ts +3 -2
- package/dist/routes.d.ts +15 -5
- package/dist/rsc/build/client-references.d.ts +8 -0
- package/dist/rsc/build/discover.d.ts +7 -0
- package/dist/rsc/build/flight-loader.d.ts +1 -0
- package/dist/rsc/build/index.d.ts +4 -1
- package/dist/rsc/build/paths.d.ts +2 -0
- package/dist/rsc/bundled-client-codec.d.ts +1 -0
- package/dist/rsc/client-codec.d.ts +3 -0
- package/dist/rsc/client-references.d.ts +22 -0
- package/dist/rsc/server-client-codec.d.ts +1 -0
- package/dist/rsc/server-codec.js +3889 -2
- package/dist/rsc/slot-proxy.d.ts +11 -0
- package/dist/rsc/transport-benchmark.d.ts +2 -1
- package/dist/rsc-client.js +16 -164
- package/dist/rsc-server.d.ts +1 -5
- package/dist/rsc.d.ts +1 -5
- package/dist/rsc.js +233 -73
- package/dist/server/assets/client-dir.d.ts +1 -0
- package/dist/server/auto-invalidate/index.d.ts +2 -2
- package/dist/server/auto-invalidate/plugin.d.ts +21 -15
- package/dist/server/auto-invalidate/types.d.ts +0 -2
- package/dist/server/bounded-body.d.ts +6 -0
- package/dist/server/browser-events/plugin.d.ts +4 -1
- package/dist/server/browser-events/shutdown.d.ts +1 -1
- package/dist/server/cache/dev-loader.d.ts +6 -1
- package/dist/server/cache/index.d.ts +4 -9
- package/dist/server/cache/invalidation.d.ts +7 -2
- package/dist/server/cache/isr-ssg.d.ts +6 -1
- package/dist/server/cache/page-cache.d.ts +5 -0
- package/dist/server/cache/purger.d.ts +14 -4
- package/dist/server/cache/redis/index.d.ts +1 -34
- package/dist/server/cache/redis/index.js +51 -25
- package/dist/server/cache/redis/scripts.d.ts +1 -1
- package/dist/server/cache/route-cache.d.ts +18 -2
- package/dist/server/cache/runtime-cache.d.ts +7 -0
- package/dist/server/context-logger.d.ts +6 -1
- package/dist/server/dev/graph.d.ts +4 -3
- package/dist/server/dev/runtime.d.ts +4 -0
- package/dist/server/dev/runtime.production.d.ts +1 -0
- package/dist/server/dev/stack-location.d.ts +9 -0
- package/dist/server/dev-page-imports.d.ts +42 -0
- package/dist/server/dev-page-plugin.d.ts +2 -49
- package/dist/server/devtools/instrumentation.d.ts +1 -3
- package/dist/server/devtools/instrumentation.production.d.ts +0 -2
- package/dist/server/devtools/request-context.d.ts +7 -0
- package/dist/server/evlog.d.ts +18 -6
- package/dist/server/instance.d.ts +52 -44
- package/dist/server/internal.d.ts +51 -14
- package/dist/server/internal.js +9749 -2095
- package/dist/server/lang-detect.d.ts +4 -4
- package/dist/server/logger.d.ts +6 -0
- package/dist/server/logging/ingest-plugin.d.ts +32 -0
- package/dist/server/mode.d.ts +17 -0
- package/dist/server/prerender.d.ts +32 -0
- package/dist/server/prod/runtime.d.ts +5 -0
- package/dist/server/production-server.js +992 -726
- package/dist/server/registrations.d.ts +14 -0
- package/dist/server/render/assemble.d.ts +0 -22
- package/dist/server/render/buffered-render.d.ts +20 -0
- package/dist/server/render/document-stream.d.ts +45 -0
- package/dist/server/render/document.d.ts +1 -1
- package/dist/server/render/element.d.ts +2 -5
- package/dist/server/render/index.d.ts +4 -5
- package/dist/server/render/index.ts +4 -21
- package/dist/server/render/isr-revalidation.d.ts +5 -0
- package/dist/server/render/isr-shared-cache.d.ts +23 -0
- package/dist/server/render/isr.d.ts +19 -1
- package/dist/server/render/loader-errors.d.ts +33 -0
- package/dist/server/render/loader-segments.d.ts +40 -0
- package/dist/server/render/loaders.d.ts +16 -32
- package/dist/server/render/mixed-cache.d.ts +2 -2
- package/dist/server/render/navigation-head.d.ts +13 -0
- package/dist/server/render/not-found-plugin.d.ts +18 -0
- package/dist/server/render/observe-loader.d.ts +1 -0
- package/dist/server/render/ppr-document.d.ts +1 -1
- package/dist/server/render/ppr-request.d.ts +0 -2
- package/dist/server/render/ppr-route.d.ts +13 -2
- package/dist/server/render/prepare-render.d.ts +57 -0
- package/dist/server/render/request-loaders.d.ts +19 -0
- package/dist/server/render/route-frame-transport.d.ts +7 -1
- package/dist/server/render/shell.d.ts +1 -1
- package/dist/server/render/shell.ts +1 -1
- package/dist/server/render/ssr-transport.d.ts +11 -0
- package/dist/server/render/ssr.d.ts +1 -116
- package/dist/server/render/static-params.d.ts +15 -0
- package/dist/server/router/discovery.d.ts +1 -0
- package/dist/server/router/dispatch.d.ts +96 -0
- package/dist/server/router/hmr.d.ts +14 -24
- package/dist/server/router/patterns.d.ts +15 -20
- package/dist/server/router/plugin.d.ts +26 -24
- package/dist/server/router/schemas.d.ts +6 -24
- package/dist/server/router/types.d.ts +0 -2
- package/dist/server/runtime-env.d.ts +6 -1
- package/dist/server/static-export.d.ts +38 -0
- package/dist/server/static-export.js +10667 -0
- package/dist/server/sync/adapter.d.ts +11 -0
- package/dist/server/sync/atomic.d.ts +0 -2
- package/dist/server/sync/config.d.ts +0 -1
- package/dist/server/sync/drizzle/index.js +112 -68
- package/dist/server/sync/fingerprint.d.ts +0 -1
- package/dist/server/sync/index.d.ts +2 -6
- package/dist/server/sync/index.js +1689 -1364
- package/dist/server/sync/invalidate-sync.d.ts +10 -0
- package/dist/server/sync/plugin.d.ts +18 -55
- package/dist/server/sync/postgres/execute.d.ts +2 -2
- package/dist/server/sync/postgres/index.d.ts +3 -1
- package/dist/server/sync/postgres/index.js +68 -37
- package/dist/server/sync/postgres/migration.sql +2 -0
- package/dist/server/sync/principal-journal.d.ts +11 -0
- package/dist/server/sync/prisma/index.js +74 -39
- package/dist/server/sync/queries.d.ts +57 -16
- package/dist/server/sync/redis/index.d.ts +2 -1
- package/dist/server/sync/redis/index.js +49 -5
- package/dist/server/sync/redis/scripts.d.ts +2 -1
- package/dist/server/sync/response.d.ts +3 -0
- package/dist/server/sync/runtime.d.ts +5 -1
- package/dist/server/sync/sqlite/index.d.ts +3 -1
- package/dist/server/sync/sqlite/index.js +58 -27
- package/dist/server/sync/stream.d.ts +32 -7
- package/dist/server/weak-registry.d.ts +9 -0
- package/dist/shared/await.d.ts +2 -1
- package/dist/shared/compact-json.d.ts +1 -0
- package/dist/shared/defer.d.ts +1 -1
- package/dist/shared/deferred-ndjson.d.ts +3 -2
- package/dist/shared/elysia-contract.d.ts +2 -0
- package/dist/shared/in-memory-apps.d.ts +7 -0
- package/dist/shared/invalidation-header.d.ts +13 -0
- package/dist/shared/navigation-data.d.ts +7 -0
- package/dist/shared/prefix.d.ts +11 -5
- package/dist/shared/route-frame.d.ts +3 -0
- package/dist/shared/route-map.d.ts +7 -0
- package/dist/shared/route-matches.d.ts +41 -0
- package/dist/shared/route-specificity.d.ts +2 -0
- package/dist/shared/search-params.d.ts +2 -3
- package/dist/shared/sync-query.d.ts +6 -13
- package/dist/shared/utils/ast-walk.d.ts +3 -11
- package/package.json +32 -21
- package/src/adapter/README.md +37 -16
- package/src/adapter/build.ts +269 -0
- package/src/adapter/bun/server.ts +14 -0
- package/src/adapter/bun.ts +89 -265
- package/src/adapter/furin-app.d.ts +14 -0
- package/src/adapter/package.ts +31 -27
- package/src/adapter/prerender.ts +97 -0
- package/src/adapter/runtime-build.ts +83 -93
- package/src/adapter/server-bundle.ts +110 -0
- package/src/adapter/static.ts +259 -247
- package/src/adapter/types.ts +89 -0
- package/src/adapter/vercel/bootstrap.ts +85 -0
- package/src/adapter/vercel/data.ts +21 -0
- package/src/adapter/vercel/handler.ts +132 -0
- package/src/adapter/vercel.ts +329 -659
- package/src/build/bun-build.ts +3 -0
- package/src/build/client.ts +22 -60
- package/src/build/compile-entry.ts +1 -0
- package/src/build/discover-mounts-process.ts +13 -0
- package/src/build/discover-mounts.ts +40 -0
- package/src/build/elysia-aot-worker.ts +84 -2
- package/src/build/elysia-aot.ts +61 -2
- package/src/build/entry-template.ts +33 -18
- package/src/build/hydrate.ts +122 -44
- package/src/build/index.ts +156 -108
- package/src/build/private-server-sourcemaps.ts +5 -0
- package/src/build/production-instrumentation.ts +22 -19
- package/src/build/route-types.ts +93 -11
- package/src/build/server-app-entry.ts +4 -21
- package/src/build/shared.ts +9 -41
- package/src/build/types.ts +14 -20
- package/src/build/virtual-entry.ts +2 -4
- package/src/cli/config.ts +133 -6
- package/src/cli/index.ts +59 -21
- package/src/cli/preview.ts +2 -1
- package/src/client/boundaries.tsx +5 -14
- package/src/client/default-screens.tsx +2 -2
- package/src/client/define-route.ts +3 -2
- package/src/client/hmr.ts +1 -1
- package/src/client/internal/runtime-types.ts +7 -1
- package/src/client/link.tsx +80 -43
- package/src/client/mutation.ts +98 -3
- package/src/client/query-store.ts +160 -29
- package/src/client/query.tsx +12 -29
- package/src/client/request-id.ts +10 -0
- package/src/client/route-api.tsx +6 -6
- package/src/client/route-element.tsx +155 -0
- package/src/client/router/boundary-tree.tsx +2 -93
- package/src/client/router/cache-generation.ts +37 -0
- package/src/client/router/context.ts +40 -4
- package/src/client/router/fetch-observers.ts +46 -0
- package/src/client/router/hmr-refresh.ts +50 -0
- package/src/client/router/index.ts +7 -29
- package/src/client/router/link-utils.ts +19 -31
- package/src/client/router/navigation-controller.ts +547 -0
- package/src/client/router/navigation.ts +3 -2
- package/src/client/router/optimistic.ts +2 -3
- package/src/client/router/page-fetch.ts +272 -0
- package/src/client/router/provider.tsx +223 -1079
- package/src/client/router/search/index.ts +38 -14
- package/src/client/router/spa-response.ts +19 -70
- package/src/client/router/sync-catch-up.ts +65 -7
- package/src/client/router/transport.ts +23 -0
- package/src/client/router/types.ts +203 -61
- package/src/client/router/use-fetch-interception.ts +52 -0
- package/src/client/router/use-history-sync.ts +56 -0
- package/src/client/router/use-hmr-refresh.ts +51 -0
- package/src/client/router/use-sync-stream.ts +129 -0
- package/src/client/server-error.ts +2 -40
- package/src/client/sync.ts +268 -378
- package/src/client.ts +2 -7
- package/src/config.ts +23 -45
- package/src/define-route.ts +487 -882
- package/src/devtools/devtools-element.js +10 -3
- package/src/devtools/protocol.ts +3 -3
- package/src/furin.ts +253 -1086
- package/src/plugin/binding-scope.ts +219 -94
- package/src/plugin/client-plugin.ts +145 -0
- package/src/plugin/dead-code-elimination.ts +390 -111
- package/src/plugin/dev-routes.ts +453 -0
- package/src/plugin/hmr-data-signature.ts +161 -0
- package/src/plugin/hmr-dependencies.ts +2 -4
- package/src/plugin/hmr-hook-signature.ts +252 -0
- package/src/plugin/index.ts +3 -96
- package/src/plugin/module-bindings.ts +216 -0
- package/src/plugin/module-transform.ts +63 -0
- package/src/plugin/preflight.ts +66 -0
- package/src/plugin/route-chains.ts +309 -0
- package/src/plugin/route-config.ts +451 -0
- package/src/plugin/route-modules.ts +209 -0
- package/src/plugin/route-registry.ts +316 -0
- package/src/plugin/route-tree.ts +248 -0
- package/src/plugin/strip-server-stages.ts +197 -0
- package/src/plugin/strip-server.ts +22 -0
- package/src/plugin/transform-client-module.ts +36 -31
- package/src/plugin/transform-client.ts +33 -1114
- package/src/plugin/transform-dev-page.ts +84 -92
- package/src/plugin/transform-isomorphic.ts +396 -185
- package/src/routes.ts +15 -5
- package/src/rsc/build/client-references.ts +267 -0
- package/src/rsc/build/discover.ts +226 -0
- package/src/rsc/build/flight-loader.ts +69 -0
- package/src/rsc/build/index.ts +53 -31
- package/src/rsc/build/paths.ts +7 -0
- package/src/rsc/bundled-client-codec.ts +4 -0
- package/src/rsc/client-codec.ts +12 -3
- package/src/rsc/client-references.ts +138 -0
- package/src/rsc/codec.ts +30 -21
- package/src/rsc/flight-drain.ts +6 -29
- package/src/rsc/react-server-dom-webpack.d.ts +1 -1
- package/src/rsc/server-client-codec.ts +14 -0
- package/src/rsc/server-codec.ts +5 -8
- package/src/rsc/slot-proxy.ts +34 -0
- package/src/rsc/transport-benchmark.ts +1 -1
- package/src/rsc-server.tsx +5 -23
- package/src/rsc.tsx +5 -29
- package/src/server/assets/client-dir.ts +110 -0
- package/src/server/assets/production.ts +1 -1
- package/src/server/auto-invalidate/index.ts +2 -7
- package/src/server/auto-invalidate/plugin.ts +43 -15
- package/src/server/auto-invalidate/registry.ts +6 -6
- package/src/server/auto-invalidate/runtime.ts +16 -98
- package/src/server/auto-invalidate/types.ts +0 -6
- package/src/server/bounded-body.ts +38 -0
- package/src/server/browser-events/plugin.ts +109 -22
- package/src/server/browser-events/shutdown.ts +28 -9
- package/src/server/cache/dev-loader.ts +30 -16
- package/src/server/cache/index.ts +2 -15
- package/src/server/cache/invalidation.ts +27 -46
- package/src/server/cache/isr-ssg.ts +6 -1
- package/src/server/cache/isr.ts +11 -11
- package/src/server/cache/page-cache-state.ts +2 -2
- package/src/server/cache/page-cache.ts +13 -9
- package/src/server/cache/purger.ts +64 -8
- package/src/server/cache/redis/index.ts +17 -27
- package/src/server/cache/redis/scripts.ts +21 -4
- package/src/server/cache/registry.ts +3 -3
- package/src/server/cache/route-cache.ts +47 -13
- package/src/server/cache/runtime-cache.ts +144 -24
- package/src/server/cache/ssg.ts +7 -5
- package/src/server/context-logger.ts +46 -8
- package/src/server/dev/diagnostics.ts +6 -56
- package/src/server/dev/error.ts +3 -34
- package/src/server/dev/graph.ts +35 -22
- package/src/server/dev/rewrite-module-specifiers.ts +15 -19
- package/src/server/dev/runtime.production.ts +4 -0
- package/src/server/dev/runtime.ts +404 -0
- package/src/server/dev/stack-location.ts +46 -0
- package/src/server/dev-page-imports.ts +183 -0
- package/src/server/dev-page-plugin.ts +39 -302
- package/src/server/devtools/hub.ts +3 -3
- package/src/server/devtools/instrumentation.production.ts +0 -8
- package/src/server/devtools/instrumentation.ts +6 -8
- package/src/server/devtools/plugin.ts +2 -1
- package/src/server/devtools/request-context.ts +67 -16
- package/src/server/evlog.ts +119 -55
- package/src/server/instance.ts +187 -138
- package/src/server/internal.ts +66 -62
- package/src/server/lang-detect.ts +31 -22
- package/src/server/logger.ts +20 -7
- package/src/server/logging/ingest-plugin.ts +123 -0
- package/src/server/mode.ts +19 -0
- package/src/server/prerender.ts +122 -0
- package/src/server/prod/runtime.ts +181 -0
- package/src/server/production-server.ts +43 -17
- package/src/server/registrations.ts +38 -0
- package/src/server/render/assemble.ts +5 -8
- package/src/server/render/buffered-render.ts +171 -0
- package/src/server/render/document-stream.ts +188 -0
- package/src/server/render/document.tsx +5 -3
- package/src/server/render/element.tsx +6 -73
- package/src/server/render/index.ts +4 -21
- package/src/server/render/isr-revalidation.ts +246 -0
- package/src/server/render/isr-shared-cache.ts +141 -0
- package/src/server/render/isr.ts +212 -516
- package/src/server/render/loader-errors.ts +183 -0
- package/src/server/render/loader-segments.ts +149 -0
- package/src/server/render/loaders.ts +126 -547
- package/src/server/render/mixed-cache.ts +28 -27
- package/src/server/render/navigation-head.ts +33 -0
- package/src/server/render/not-found-plugin.ts +31 -0
- package/src/server/render/not-found.ts +32 -8
- package/src/server/render/observe-loader.ts +46 -0
- package/src/server/render/ppr-document.ts +36 -14
- package/src/server/render/ppr-request.ts +2 -2
- package/src/server/render/ppr-route.ts +79 -18
- package/src/server/render/prepare-render.ts +251 -0
- package/src/server/render/request-loaders.ts +216 -0
- package/src/server/render/route-frame-transport.ts +41 -4
- package/src/server/render/shell.ts +1 -1
- package/src/server/render/ssg.ts +9 -20
- package/src/server/render/ssr-transport.ts +221 -0
- package/src/server/render/ssr.ts +95 -956
- package/src/server/render/static-params.ts +32 -0
- package/src/server/render/template.ts +7 -7
- package/src/server/router/discovery.ts +82 -159
- package/src/server/router/dispatch.ts +337 -0
- package/src/server/router/hmr.ts +177 -190
- package/src/server/router/patterns.ts +39 -65
- package/src/server/router/plugin.ts +234 -359
- package/src/server/router/schemas.ts +6 -39
- package/src/server/router/source-version.ts +5 -4
- package/src/server/router/types.ts +0 -2
- package/src/server/runtime-env.ts +6 -1
- package/src/server/static-export.ts +143 -0
- package/src/server/sync/adapter.ts +11 -0
- package/src/server/sync/atomic.ts +16 -36
- package/src/server/sync/config.ts +6 -14
- package/src/server/sync/drizzle/index.ts +12 -20
- package/src/server/sync/fingerprint.ts +1 -1
- package/src/server/sync/index.ts +2 -10
- package/src/server/sync/invalidate-sync.ts +31 -0
- package/src/server/sync/mutation-handler.ts +6 -0
- package/src/server/sync/plugin.ts +171 -129
- package/src/server/sync/postgres/execute.ts +9 -1
- package/src/server/sync/postgres/index.ts +71 -39
- package/src/server/sync/postgres/migration.sql +2 -0
- package/src/server/sync/principal-journal.ts +78 -0
- package/src/server/sync/prisma/index.ts +1 -0
- package/src/server/sync/queries.ts +133 -40
- package/src/server/sync/query-context.ts +38 -5
- package/src/server/sync/redis/index.ts +34 -6
- package/src/server/sync/redis/scripts.ts +10 -1
- package/src/server/sync/response.ts +47 -67
- package/src/server/sync/runtime.ts +24 -7
- package/src/server/sync/sqlite/index.ts +92 -61
- package/src/server/sync/sqlite/migration.sql +1 -0
- package/src/server/sync/stream.ts +47 -26
- package/src/server/sync/validation.ts +1 -1
- package/src/server/weak-registry.ts +37 -0
- package/src/shared/await.tsx +2 -1
- package/src/shared/compact-json.ts +1 -1
- package/src/shared/defer.ts +1 -1
- package/src/shared/deferred-ndjson.ts +142 -120
- package/src/shared/elysia-contract.ts +7 -0
- package/src/shared/error.ts +1 -4
- package/src/shared/in-memory-apps.ts +24 -0
- package/src/shared/invalidation-header.ts +38 -0
- package/src/shared/navigation-data.ts +29 -0
- package/src/shared/prefix.ts +42 -6
- package/src/shared/route-frame.ts +82 -26
- package/src/shared/route-map.ts +23 -39
- package/src/shared/route-matches.ts +59 -0
- package/src/shared/route-specificity.ts +28 -0
- package/src/shared/search-params.ts +6 -35
- package/src/shared/sync-query.ts +30 -23
- package/src/shared/utils/ast-walk.ts +9 -25
- package/src/shared/utils/index.ts +1 -1
- package/dist/build/scan-server.d.ts +0 -14
- package/dist/build/ssg-cache.d.ts +0 -18
- package/dist/plugin/route-config-autofix.d.ts +0 -31
- package/dist/plugin/routes.d.ts +0 -34
- package/dist/server/router/hmr.production.d.ts +0 -7
- package/src/build/scan-server.ts +0 -112
- package/src/build/ssg-cache.ts +0 -126
- package/src/plugin/route-config-autofix.ts +0 -1149
- package/src/plugin/routes.ts +0 -1140
- package/src/server/router/hmr.production.ts +0 -18
package/src/furin.ts
CHANGED
|
@@ -1,112 +1,43 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { type AnyElysia, Elysia, file, NotFound, problem } from "elysia";
|
|
6
|
-
import type { DrainContext, LoggerConfig } from "evlog";
|
|
7
|
-
import { FURIN_RENDER_DECORATOR, type FurinRouteDispatcher } from "./define-route.ts";
|
|
8
|
-
import { createProductionAssetsPlugin } from "./server/assets/production.ts";
|
|
1
|
+
import { dirname, resolve } from "node:path";
|
|
2
|
+
import { type AnyElysia, Elysia, NotFound } from "elysia";
|
|
3
|
+
import type { RequestLogger } from "evlog";
|
|
4
|
+
import { FURIN_RENDER_DECORATOR, type FurinNativeRouteContext } from "./define-route.ts";
|
|
9
5
|
import { consumePendingInvalidations } from "./server/cache/invalidation.ts";
|
|
10
6
|
import type { PageCacheAdapter } from "./server/cache/page-cache.ts";
|
|
11
7
|
import { setPageCacheAdapter } from "./server/cache/page-cache-state.ts";
|
|
12
|
-
import { setSSGCache } from "./server/cache/ssg.ts";
|
|
13
|
-
import { getLogger } from "./server/context-logger.ts";
|
|
14
|
-
import type { DevelopmentRouteSnapshot, DevGraph } from "./server/dev/graph.ts";
|
|
15
8
|
import {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
} from "./server/devtools/instrumentation.ts";
|
|
21
|
-
import { createFurinEvlog, type FurinEvlogOptions } from "./server/evlog.ts";
|
|
9
|
+
type CachePurger,
|
|
10
|
+
setInstanceCachePurger,
|
|
11
|
+
setInstanceCacheTagPurger,
|
|
12
|
+
} from "./server/cache/purger.ts";
|
|
22
13
|
import {
|
|
23
|
-
assertPrefixAvailable,
|
|
24
14
|
createInstance,
|
|
25
15
|
type FurinInstance,
|
|
26
|
-
hasRequestScope,
|
|
27
|
-
markTraffic,
|
|
28
16
|
normalizePrefix,
|
|
29
17
|
registerInstance,
|
|
30
|
-
|
|
31
|
-
runWithInstanceScope,
|
|
32
|
-
withInstance,
|
|
18
|
+
unregisterInstance,
|
|
33
19
|
} from "./server/instance.ts";
|
|
34
|
-
import type
|
|
35
|
-
import {
|
|
36
|
-
import {
|
|
37
|
-
import {
|
|
38
|
-
import {
|
|
20
|
+
import { type CompileContext, getCompileContext } from "./server/internal.ts";
|
|
21
|
+
import { createLoggerPlugin, type FurinLoggerOptions } from "./server/logging/ingest-plugin.ts";
|
|
22
|
+
import type { ModeOptions, ModeRuntime } from "./server/mode.ts";
|
|
23
|
+
import { createProdRuntime } from "./server/prod/runtime.ts";
|
|
24
|
+
import { recordFurinRegistration } from "./server/registrations.ts";
|
|
25
|
+
import { createNotFoundHandling } from "./server/render/not-found-plugin.ts";
|
|
39
26
|
import {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
} from "./server/
|
|
44
|
-
import { loadProdRoutes } from "./server/router/discovery.ts";
|
|
45
|
-
import { invalidateStampedRouteModules, resolveCurrentDevRoute } from "./server/router/hmr.ts";
|
|
46
|
-
import { buildRouteMatcher } from "./server/router/patterns.ts";
|
|
47
|
-
import {
|
|
48
|
-
renderResolvedRoute,
|
|
49
|
-
renderRouteData,
|
|
50
|
-
serializeGuardRedirect,
|
|
51
|
-
} from "./server/router/plugin.ts";
|
|
52
|
-
import { mergeRouteSchemas } from "./server/router/schema-merge.ts";
|
|
53
|
-
import {
|
|
54
|
-
createSearchRouteMetadata,
|
|
55
|
-
parseDataEndpointPath,
|
|
56
|
-
parseRouteParams,
|
|
57
|
-
parseRouteQuery,
|
|
58
|
-
} from "./server/router/schemas.ts";
|
|
59
|
-
import type { ResolvedRoute, RootLayout } from "./server/router/types.ts";
|
|
60
|
-
import { IS_DEV } from "./server/runtime-env.ts";
|
|
27
|
+
createRequestScope,
|
|
28
|
+
dispatchNativeRoute,
|
|
29
|
+
renderNotFound,
|
|
30
|
+
} from "./server/router/dispatch.ts";
|
|
61
31
|
import { type FurinSyncOption, resolveSyncPath } from "./server/sync/config.ts";
|
|
62
32
|
import { bindSyncValidation } from "./server/sync/validation.ts";
|
|
63
|
-
import {
|
|
33
|
+
import { serializeInvalidationPaths } from "./shared/invalidation-header.ts";
|
|
34
|
+
import { assertOnePagesDirPerMount } from "./shared/prefix.ts";
|
|
64
35
|
|
|
65
36
|
// biome-ignore lint/suspicious/noEmptyInterface: intentionally augmentable via furin-env.d.ts
|
|
66
37
|
export interface FurinCacheTags {}
|
|
67
38
|
|
|
68
39
|
export type CacheTag = keyof FurinCacheTags extends never ? string : keyof FurinCacheTags;
|
|
69
40
|
|
|
70
|
-
async function createProductionBrowserEventsPlugin(
|
|
71
|
-
sync: FurinSyncOption | undefined,
|
|
72
|
-
deploymentTarget: "vercel" | undefined
|
|
73
|
-
): Promise<AnyElysia> {
|
|
74
|
-
if (!sync) {
|
|
75
|
-
return new Elysia();
|
|
76
|
-
}
|
|
77
|
-
const browserEvents = await import("./server/browser-events/plugin.ts");
|
|
78
|
-
// Vercel invokes app.handle(Request), so Elysia's Bun.Server WebSocket upgrade is unavailable.
|
|
79
|
-
return deploymentTarget === "vercel"
|
|
80
|
-
? browserEvents.createSseBrowserEventsPlugin({ sync })
|
|
81
|
-
: browserEvents.createBrowserEventsPlugin({ sync });
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function repairedDevelopmentRoutes(
|
|
85
|
-
snapshot: DevelopmentRouteSnapshot,
|
|
86
|
-
changedSources: readonly string[],
|
|
87
|
-
graph: DevGraph<DevelopmentRouteSnapshot | null>
|
|
88
|
-
): { patterns: ReadonlySet<string>; root: boolean } {
|
|
89
|
-
const patterns = new Set<string>();
|
|
90
|
-
for (const route of snapshot.routes) {
|
|
91
|
-
if (
|
|
92
|
-
changedSources.some(
|
|
93
|
-
(sourcePath) => route.path === sourcePath || graph.dependsOn(route.path, sourcePath)
|
|
94
|
-
)
|
|
95
|
-
) {
|
|
96
|
-
patterns.add(route.pattern);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
return {
|
|
100
|
-
patterns,
|
|
101
|
-
root: changedSources.some(
|
|
102
|
-
(sourcePath) =>
|
|
103
|
-
snapshot.root.path === sourcePath || graph.dependsOn(snapshot.root.path, sourcePath)
|
|
104
|
-
),
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
import { clientDirNameForPrefix } from "./shared/prefix.ts";
|
|
109
|
-
|
|
110
41
|
// biome-ignore lint/performance/noBarrelFile: furin.ts is the public package entry
|
|
111
42
|
export {
|
|
112
43
|
type DefineRouteConfig,
|
|
@@ -123,553 +54,55 @@ export {
|
|
|
123
54
|
type IsomorphicFnBuilder,
|
|
124
55
|
type ServerIsomorphicFn,
|
|
125
56
|
} from "./isomorphic.ts";
|
|
57
|
+
export type { FurinLoggerOptions } from "./server/logging/ingest-plugin.ts";
|
|
58
|
+
export { isFurinPageRequest } from "./server/router/dispatch.ts";
|
|
126
59
|
export { type FurinCspOptions, furinCsp } from "./server/security/csp.ts";
|
|
127
|
-
export { clientDirNameForPrefix } from "./shared/prefix.ts";
|
|
128
|
-
|
|
129
|
-
const MAX_BROWSER_INGEST_BYTES = 64 * 1024;
|
|
130
|
-
const MAX_BROWSER_INGEST_EVENTS = 100;
|
|
131
|
-
function resolveClientDirFromArgv(prefix: string): string {
|
|
132
|
-
const dirName = clientDirNameForPrefix(prefix);
|
|
133
|
-
return (
|
|
134
|
-
resolveClientDirFromEnv(dirName) ??
|
|
135
|
-
resolveClientDirFromModuleUrl(dirName) ??
|
|
136
|
-
resolveClientDirFromProcessArgs(dirName) ??
|
|
137
|
-
resolveFallbackClientDir(dirName)
|
|
138
|
-
);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
function resolveClientDirFromEnv(dirName: string): string | null {
|
|
142
|
-
const envClientDir = process.env.FURIN_CLIENT_DIR;
|
|
143
|
-
if (!envClientDir) {
|
|
144
|
-
return null;
|
|
145
|
-
}
|
|
146
|
-
const base = envClientDir.startsWith("/") ? envClientDir : resolve(process.cwd(), envClientDir);
|
|
147
|
-
// FURIN_CLIENT_DIR points at the ROOT instance's client dir; sibling
|
|
148
|
-
// instances live next to it under their own dir name.
|
|
149
|
-
return dirName === "client" ? base : join(dirname(base), dirName);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
function resolveClientDirFromModuleUrl(dirName: string): string | null {
|
|
153
|
-
try {
|
|
154
|
-
const moduleUrl = new URL(import.meta.url);
|
|
155
|
-
if (moduleUrl.protocol !== "file:") {
|
|
156
|
-
return null;
|
|
157
|
-
}
|
|
158
|
-
const modulePath = fileURLToPath(moduleUrl);
|
|
159
|
-
if (modulePath.includes("/$bunfs/")) {
|
|
160
|
-
return null;
|
|
161
|
-
}
|
|
162
|
-
const moduleClientDir = join(dirname(modulePath), dirName);
|
|
163
|
-
if (existsSync(join(moduleClientDir, "index.html"))) {
|
|
164
|
-
return moduleClientDir;
|
|
165
|
-
}
|
|
166
|
-
} catch {
|
|
167
|
-
// ignore, fallback to argv-based resolution
|
|
168
|
-
}
|
|
169
|
-
return null;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
function resolveClientDirFromProcessArgs(dirName: string): string | null {
|
|
173
|
-
const candidates = [
|
|
174
|
-
process.argv[1],
|
|
175
|
-
process.argv[0],
|
|
176
|
-
(process as { argv0?: string }).argv0,
|
|
177
|
-
process.execPath,
|
|
178
|
-
].filter((value): value is string => typeof value === "string" && value.length > 0);
|
|
179
|
-
|
|
180
|
-
for (const candidate of candidates) {
|
|
181
|
-
const resolved = resolveClientDirFromCandidate(candidate, dirName);
|
|
182
|
-
if (resolved) {
|
|
183
|
-
return resolved;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
return null;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
function resolveClientDirFromCandidate(candidate: string, dirName: string): string | null {
|
|
191
|
-
const name = basename(candidate);
|
|
192
|
-
if (name === "bun" || name === "node") {
|
|
193
|
-
return null;
|
|
194
|
-
}
|
|
195
|
-
if (candidate.includes("/$bunfs/") || candidate.startsWith("bunfs:")) {
|
|
196
|
-
return null;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
const absolute = candidate.startsWith("/") ? candidate : resolve(process.cwd(), candidate);
|
|
200
|
-
if (existsSync(absolute)) {
|
|
201
|
-
const clientDir = join(dirname(absolute), dirName);
|
|
202
|
-
if (existsSync(join(clientDir, "index.html"))) {
|
|
203
|
-
return clientDir;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
if (!candidate.includes("/")) {
|
|
208
|
-
return resolveClientDirFromPath(candidate, dirName);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
return null;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
function resolveClientDirFromPath(candidate: string, dirName: string): string | null {
|
|
215
|
-
const pathEntries = process.env.PATH?.split(delimiter) ?? [];
|
|
216
|
-
for (const dir of pathEntries) {
|
|
217
|
-
const fullPath = join(dir, candidate);
|
|
218
|
-
if (existsSync(fullPath)) {
|
|
219
|
-
const clientDir = join(dirname(fullPath), dirName);
|
|
220
|
-
if (existsSync(join(clientDir, "index.html"))) {
|
|
221
|
-
return clientDir;
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
return null;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
function resolveFallbackClientDir(dirName: string): string {
|
|
229
|
-
const defaultClientDir = resolve(process.cwd(), ".furin/build/bun", dirName);
|
|
230
|
-
if (existsSync(join(defaultClientDir, "index.html"))) {
|
|
231
|
-
return defaultClientDir;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
return join(process.cwd(), dirName);
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
async function setupProdTemplate(
|
|
238
|
-
embedded: EmbeddedAppData | undefined,
|
|
239
|
-
clientDir: string,
|
|
240
|
-
instance: FurinInstance
|
|
241
|
-
): Promise<void> {
|
|
242
|
-
if (embedded) {
|
|
243
|
-
const templatePath = join(embedded.clientDir, "index.html");
|
|
244
|
-
if (!existsSync(templatePath)) {
|
|
245
|
-
throw new Error("[furin] Embedded app is missing its HTML template (index.html).");
|
|
246
|
-
}
|
|
247
|
-
const html = await Bun.file(templatePath).text();
|
|
248
|
-
setProductionTemplateContent(html, instance);
|
|
249
|
-
return;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
const templatePath = join(clientDir, "index.html");
|
|
253
|
-
if (!existsSync(templatePath)) {
|
|
254
|
-
throw new Error("[furin] No pre-built assets found. Run `bun run build` first.");
|
|
255
|
-
}
|
|
256
|
-
setProductionTemplatePath(templatePath, instance);
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
async function setupCompiledTemplate(
|
|
260
|
-
ctx: CompileContext,
|
|
261
|
-
embedded: EmbeddedAppData | undefined,
|
|
262
|
-
clientDir: string,
|
|
263
|
-
instance: FurinInstance
|
|
264
|
-
): Promise<void> {
|
|
265
|
-
if (ctx.preloadManifest) {
|
|
266
|
-
setProductionPreloadManifest(ctx.preloadManifest, instance);
|
|
267
|
-
}
|
|
268
|
-
if (ctx.templateHtml === undefined) {
|
|
269
|
-
await setupProdTemplate(embedded, clientDir, instance);
|
|
270
|
-
return;
|
|
271
|
-
}
|
|
272
|
-
setProductionTemplateContent(ctx.templateHtml, instance);
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* Shape of one browser-submitted log event. The named keys are the dangerous
|
|
277
|
-
* ones stripped before `log.set` (prototype-pollution vectors, plus the
|
|
278
|
-
* browser `environment` which must not overwrite the server's); everything
|
|
279
|
-
* else is forwarded as-is.
|
|
280
|
-
*/
|
|
281
|
-
interface FurinBrowserEvent {
|
|
282
|
-
__proto__?: unknown;
|
|
283
|
-
constructor?: unknown;
|
|
284
|
-
environment?: unknown;
|
|
285
|
-
prototype?: unknown;
|
|
286
|
-
[key: string]: unknown;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
type BrowserIngestRead =
|
|
290
|
-
| { body: unknown; kind: "ok" }
|
|
291
|
-
| { kind: "invalid" }
|
|
292
|
-
| { kind: "oversized" };
|
|
293
|
-
|
|
294
|
-
async function readBrowserIngest(request: Request): Promise<BrowserIngestRead> {
|
|
295
|
-
const contentLength = request.headers.get("content-length");
|
|
296
|
-
if (contentLength) {
|
|
297
|
-
const declaredBytes = Number(contentLength);
|
|
298
|
-
if (Number.isFinite(declaredBytes) && declaredBytes > MAX_BROWSER_INGEST_BYTES) {
|
|
299
|
-
await request.body?.cancel();
|
|
300
|
-
return { kind: "oversized" };
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
if (request.body === null) {
|
|
304
|
-
return { kind: "invalid" };
|
|
305
|
-
}
|
|
306
|
-
const reader = request.body.getReader();
|
|
307
|
-
const chunks: Uint8Array[] = [];
|
|
308
|
-
let total = 0;
|
|
309
|
-
try {
|
|
310
|
-
for (;;) {
|
|
311
|
-
// biome-ignore lint/performance/noAwaitInLoops: request body chunks must be read sequentially.
|
|
312
|
-
const { done, value } = await reader.read();
|
|
313
|
-
if (done) {
|
|
314
|
-
break;
|
|
315
|
-
}
|
|
316
|
-
total += value.byteLength;
|
|
317
|
-
if (total > MAX_BROWSER_INGEST_BYTES) {
|
|
318
|
-
await reader.cancel();
|
|
319
|
-
return { kind: "oversized" };
|
|
320
|
-
}
|
|
321
|
-
chunks.push(value);
|
|
322
|
-
}
|
|
323
|
-
} finally {
|
|
324
|
-
reader.releaseLock();
|
|
325
|
-
}
|
|
326
|
-
const bytes = new Uint8Array(total);
|
|
327
|
-
let offset = 0;
|
|
328
|
-
for (const chunk of chunks) {
|
|
329
|
-
bytes.set(chunk, offset);
|
|
330
|
-
offset += chunk.byteLength;
|
|
331
|
-
}
|
|
332
|
-
try {
|
|
333
|
-
return { body: JSON.parse(new TextDecoder().decode(bytes)), kind: "ok" };
|
|
334
|
-
} catch {
|
|
335
|
-
return { kind: "invalid" };
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
/** Evlog wide-event plugin + browser log ingest endpoint for one instance. */
|
|
340
|
-
function createLoggerPlugin(
|
|
341
|
-
prefix: string,
|
|
342
|
-
syncPath: string | undefined,
|
|
343
|
-
logger: FurinEvlogOptions | undefined,
|
|
344
|
-
clientLogging: boolean
|
|
345
|
-
) {
|
|
346
|
-
const { exclude: userExclude, ...evlogOptions } = logger ?? {};
|
|
347
|
-
const app = new Elysia().use(
|
|
348
|
-
createFurinEvlog({
|
|
349
|
-
...evlogOptions,
|
|
350
|
-
// Exclude patterns match the PHYSICAL request path — prefix them.
|
|
351
|
-
exclude: [
|
|
352
|
-
`${prefix}/_client/**`,
|
|
353
|
-
`${prefix}/public/**`,
|
|
354
|
-
`${prefix}/favicon.ico`,
|
|
355
|
-
`${prefix}/_bun_hmr_entry/**`,
|
|
356
|
-
...instrumentationLoggerExclusions(prefix),
|
|
357
|
-
...(syncPath ? [`${prefix}${syncPath}/**`] : []),
|
|
358
|
-
// Note: /_furin/data is logged with the logical page path, so SPA navigations
|
|
359
|
-
// appear as "GET /board/123 200" — same shape as a normal SSR nav.
|
|
360
|
-
// /_furin/ingest remains loggable when browser logging is explicitly
|
|
361
|
-
// enabled so browser-side events show up.
|
|
362
|
-
// evlog's `matchesPattern` only supports `*`, `**`, `?` — extglob
|
|
363
|
-
// like `!(...)` matches nothing, so don't add patterns relying on it.
|
|
364
|
-
...(userExclude ?? []),
|
|
365
|
-
],
|
|
366
|
-
})
|
|
367
|
-
);
|
|
368
|
-
|
|
369
|
-
if (!clientLogging) {
|
|
370
|
-
return app;
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
return app.post("/_furin/ingest", { parse: "none" }, async ({ log, request, status }) => {
|
|
374
|
-
const parsed = await readBrowserIngest(request);
|
|
375
|
-
if (parsed.kind === "oversized") {
|
|
376
|
-
return problem(413, { detail: "Browser event payload exceeds 64 KiB." });
|
|
377
|
-
}
|
|
378
|
-
if (parsed.kind === "invalid") {
|
|
379
|
-
return problem("Bad Request", { detail: "Browser event payload must be valid JSON." });
|
|
380
|
-
}
|
|
381
|
-
const { body } = parsed;
|
|
382
|
-
if (!Array.isArray(body)) {
|
|
383
|
-
return problem("Bad Request", { detail: "Browser event payload must be a JSON array." });
|
|
384
|
-
}
|
|
385
|
-
const batch = (body as DrainContext[]).slice(0, MAX_BROWSER_INGEST_EVENTS);
|
|
386
|
-
for (const entry of batch) {
|
|
387
|
-
if (!entry || typeof entry !== "object" || !("event" in entry)) {
|
|
388
|
-
log.set({ msg: "[furin] ingest: skipping malformed entry" });
|
|
389
|
-
continue;
|
|
390
|
-
}
|
|
391
|
-
// Pick only safe, known fields from the event to prevent prototype pollution
|
|
392
|
-
const event = entry.event as FurinBrowserEvent | undefined;
|
|
393
|
-
if (!event || typeof event !== "object") {
|
|
394
|
-
continue;
|
|
395
|
-
}
|
|
396
|
-
const {
|
|
397
|
-
__proto__,
|
|
398
|
-
constructor: _ctor,
|
|
399
|
-
prototype,
|
|
400
|
-
environment: _browserEnv,
|
|
401
|
-
...safeEvent
|
|
402
|
-
} = event;
|
|
403
|
-
log.set({ ...safeEvent, service: "furin:browser" });
|
|
404
|
-
}
|
|
405
|
-
return status("No Content");
|
|
406
|
-
}) as unknown as Elysia;
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
function initializeLogger(logger: FurinLoggerOptions | undefined): FurinEvlogOptions {
|
|
410
|
-
const { sampling, ...elysiaLoggerOptions } = logger ?? {};
|
|
411
|
-
initializeFurinLogger({
|
|
412
|
-
env: { service: "furin" },
|
|
413
|
-
...(sampling ? { sampling } : {}),
|
|
414
|
-
});
|
|
415
|
-
return elysiaLoggerOptions;
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
/**
|
|
419
|
-
* Registers a HigherOrderFunction on the Elysia instance so that every request
|
|
420
|
-
* runs inside a fresh instance-bound `AsyncLocalStorage` scope. This isolates
|
|
421
|
-
* `pendingInvalidations` per request AND binds the request to the furin
|
|
422
|
-
* instance that owns its path, so render/cache code resolves the right
|
|
423
|
-
* per-instance state (build ID, template, caches, sync path).
|
|
424
|
-
*
|
|
425
|
-
* Uses `app.wrap()` (Elysia HigherOrderFunction) instead of mutating
|
|
426
|
-
* `app.handle`, because handle mutations are lost when the Furin plugin is
|
|
427
|
-
* `.use()`-d by a parent Elysia instance. HigherOrderFunctions survive the
|
|
428
|
-
* plugin merge and wrap the entire composed `map` handler.
|
|
429
|
-
*
|
|
430
|
-
* IMPORTANT — multi-instance: HigherOrderFunctions apply to the WHOLE parent
|
|
431
|
-
* app, so with N mounted furin instances all N wraps run stacked on every
|
|
432
|
-
* request. The scope guard makes the wrap idempotent (first one wins) and the
|
|
433
|
-
* owning instance is resolved from the request PATH, never from this
|
|
434
|
-
* closure — which wrap executes first is therefore irrelevant.
|
|
435
|
-
*/
|
|
436
|
-
const navigationDataRequests = new WeakSet<Request>();
|
|
437
|
-
const navigationDataRefreshers = new WeakMap<FurinInstance, () => Promise<void>>();
|
|
438
|
-
const navigationDataMatchers = new WeakMap<FurinInstance, (path: string) => boolean>();
|
|
439
|
-
|
|
440
|
-
/** Whether a request targets a registered page in the specified Furin mount. */
|
|
441
|
-
export function isFurinPageRequest(request: Request, prefix: string): boolean {
|
|
442
|
-
if (request.method !== "GET") {
|
|
443
|
-
return false;
|
|
444
|
-
}
|
|
445
|
-
const url = new URL(request.url);
|
|
446
|
-
const { pathname } = url;
|
|
447
|
-
const mountPrefix = normalizePrefix(prefix);
|
|
448
|
-
const instance = resolveInstanceByPath(pathname);
|
|
449
|
-
if (instance.prefix !== mountPrefix) {
|
|
450
|
-
return false;
|
|
451
|
-
}
|
|
452
|
-
const path = pathname.slice(mountPrefix.length) || "/";
|
|
453
|
-
if (path === "/_furin/data") {
|
|
454
|
-
const dataPath = parseDataEndpointPath(url.searchParams.get("path") ?? "");
|
|
455
|
-
return (
|
|
456
|
-
dataPath !== undefined && (navigationDataMatchers.get(instance)?.(dataPath.pathname) ?? false)
|
|
457
|
-
);
|
|
458
|
-
}
|
|
459
|
-
if (
|
|
460
|
-
path.startsWith("/_client/") ||
|
|
461
|
-
path.startsWith("/_furin/") ||
|
|
462
|
-
path.startsWith("/_bun_hmr_entry") ||
|
|
463
|
-
path.startsWith("/public/") ||
|
|
464
|
-
path === "/favicon.ico"
|
|
465
|
-
) {
|
|
466
|
-
return false;
|
|
467
|
-
}
|
|
468
|
-
return navigationDataMatchers.get(instance)?.(path) ?? false;
|
|
469
|
-
}
|
|
470
60
|
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
return fetch(request, ...rest);
|
|
501
|
-
}
|
|
502
|
-
markTraffic();
|
|
503
|
-
const { pathname } = new URL(request.url);
|
|
504
|
-
const instance = resolveInstanceByPath(pathname);
|
|
505
|
-
return runWithInstanceScope(instance, () => {
|
|
506
|
-
const dispatch = () => {
|
|
507
|
-
const rewritten = rewriteNavigationDataRequest(request, instance);
|
|
508
|
-
if (rewritten instanceof Response) {
|
|
509
|
-
return rewritten;
|
|
510
|
-
}
|
|
511
|
-
const response = shouldInstrumentRequest(pathname, instance.prefix)
|
|
512
|
-
? runWithRequestInstrumentation(request, () => fetch(rewritten, ...rest))
|
|
513
|
-
: fetch(rewritten, ...rest);
|
|
514
|
-
if (rewritten === request) {
|
|
515
|
-
return response;
|
|
516
|
-
}
|
|
517
|
-
return Promise.resolve(response).then((resolved) =>
|
|
518
|
-
resolved.status >= 300 && resolved.status < 400
|
|
519
|
-
? serializeGuardRedirect(resolved, request)
|
|
520
|
-
: resolved
|
|
521
|
-
);
|
|
522
|
-
};
|
|
523
|
-
const dataPath =
|
|
524
|
-
pathname === `${instance.prefix}/_furin/data`
|
|
525
|
-
? parseDataEndpointPath(new URL(request.url).searchParams.get("path") ?? "")
|
|
526
|
-
: undefined;
|
|
527
|
-
const refresh =
|
|
528
|
-
pathname === `${instance.prefix}/_furin/data` &&
|
|
529
|
-
request.method === "GET" &&
|
|
530
|
-
dataPath !== undefined &&
|
|
531
|
-
navigationDataMatchers.get(instance)?.(dataPath.pathname) === true &&
|
|
532
|
-
request.headers.get("x-furin-hmr-refresh") === "1"
|
|
533
|
-
? navigationDataRefreshers.get(instance)
|
|
534
|
-
: undefined;
|
|
535
|
-
return refresh ? refresh().then(dispatch) : dispatch();
|
|
536
|
-
});
|
|
537
|
-
});
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
function createFurinPlugin(app: AnyElysia, hmrPrefix: string | undefined, hasSync: boolean) {
|
|
541
|
-
const scopedApp = wrapWithRequestScope(app);
|
|
542
|
-
return <ParentApp extends AnyElysia>(parentApp: ParentApp) => {
|
|
543
|
-
if (hasSync) {
|
|
544
|
-
bindSyncValidation(parentApp);
|
|
545
|
-
}
|
|
546
|
-
const mounted = parentApp.use(scopedApp);
|
|
547
|
-
if (hmrPrefix !== undefined) {
|
|
548
|
-
const parentConfig = Reflect.get(parentApp, "~config") as { prefix?: string } | undefined;
|
|
549
|
-
const entryPath = `${parentConfig?.prefix ?? ""}${hmrPrefix}/_bun_hmr_entry`;
|
|
550
|
-
const routes = Reflect.get(mounted, "~routes") as
|
|
551
|
-
| [string, string, unknown, unknown, unknown, unknown, unknown?][]
|
|
552
|
-
| undefined;
|
|
553
|
-
for (const route of routes ?? []) {
|
|
554
|
-
if (
|
|
555
|
-
route[0] === "GET" &&
|
|
556
|
-
(route[1] === entryPath || route[1] === `${entryPath}/index.html`)
|
|
557
|
-
) {
|
|
558
|
-
// Bun serves these HTML bundles natively; inherited Elysia hooks cannot run.
|
|
559
|
-
route[6] = undefined;
|
|
560
|
-
}
|
|
561
|
-
}
|
|
562
|
-
}
|
|
563
|
-
return mounted;
|
|
564
|
-
};
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
async function loadDevelopmentRoutes(resolvedPagesDir: string) {
|
|
568
|
-
const { scanPages } = await import("./server/router/discovery.ts");
|
|
569
|
-
return scanPages(resolvedPagesDir);
|
|
570
|
-
}
|
|
571
|
-
|
|
572
|
-
function createNativeRouteRenderer(
|
|
573
|
-
prefix: string,
|
|
574
|
-
routes: ResolvedRoute[],
|
|
575
|
-
root: RootLayout,
|
|
576
|
-
buildId: string,
|
|
577
|
-
searchRoutes: ReturnType<typeof createSearchRouteMetadata>
|
|
578
|
-
): FurinRouteDispatcher {
|
|
579
|
-
const matchNativeRoute = buildRouteMatcher(routes);
|
|
580
|
-
return async (context) => {
|
|
581
|
-
const { request } = context;
|
|
582
|
-
const requestUrl = new URL(request.url);
|
|
583
|
-
const { pathname } = requestUrl;
|
|
584
|
-
const hasPrefix = prefix !== "" && (pathname === prefix || pathname.startsWith(`${prefix}/`));
|
|
585
|
-
const logicalPath = hasPrefix ? pathname.slice(prefix.length) : pathname;
|
|
586
|
-
const matched = matchNativeRoute(logicalPath || "/");
|
|
587
|
-
if (!matched || (IS_DEV && !existsSync(matched.route.path))) {
|
|
588
|
-
// Dev topology swap: the mounted Elysia route can outlive its source
|
|
589
|
-
// file (hot-remove). Render the root not-found page instead of failing.
|
|
590
|
-
const listenerOrigin = (context.server as { url?: { origin: string } } | undefined)?.url
|
|
591
|
-
?.origin;
|
|
592
|
-
return renderRootNotFound(root, request, listenerOrigin);
|
|
593
|
-
}
|
|
594
|
-
const parsedParams = await parseRouteParams(
|
|
595
|
-
matched.params,
|
|
596
|
-
mergeRouteSchemas(matched.route.routeChain, "params")
|
|
597
|
-
);
|
|
598
|
-
if (!parsedParams.ok) {
|
|
599
|
-
return problem(422, { detail: "Invalid params", errors: parsedParams.errors });
|
|
600
|
-
}
|
|
601
|
-
const parsedQuery = await parseRouteQuery(
|
|
602
|
-
requestUrl,
|
|
603
|
-
mergeRouteSchemas(matched.route.routeChain, "query")
|
|
604
|
-
);
|
|
605
|
-
if (!parsedQuery.ok) {
|
|
606
|
-
return problem(422, { detail: "Invalid query", errors: parsedQuery.errors });
|
|
607
|
-
}
|
|
608
|
-
context.params = parsedParams.params;
|
|
609
|
-
context.query = parsedQuery.query;
|
|
610
|
-
if (navigationDataRequests.has(request)) {
|
|
611
|
-
getLogger().set({
|
|
612
|
-
path: logicalPath + requestUrl.search,
|
|
613
|
-
routePattern: matched.route.pattern,
|
|
614
|
-
});
|
|
615
|
-
const current = IS_DEV
|
|
616
|
-
? await resolveCurrentDevRoute(matched.route, root)
|
|
617
|
-
: { root, route: matched.route };
|
|
618
|
-
return renderRouteData(
|
|
619
|
-
current.route,
|
|
620
|
-
context as unknown as Parameters<typeof renderRouteData>[1],
|
|
621
|
-
current.root,
|
|
622
|
-
searchRoutes,
|
|
623
|
-
logicalPath + requestUrl.search
|
|
624
|
-
);
|
|
625
|
-
}
|
|
626
|
-
return renderResolvedRoute(
|
|
627
|
-
matched.route,
|
|
628
|
-
context as unknown as Parameters<typeof renderResolvedRoute>[1],
|
|
629
|
-
root,
|
|
630
|
-
buildId,
|
|
631
|
-
searchRoutes
|
|
632
|
-
);
|
|
633
|
-
};
|
|
634
|
-
}
|
|
635
|
-
|
|
636
|
-
function createDevelopmentRouteSnapshot(
|
|
637
|
-
prefix: string,
|
|
638
|
-
root: RootLayout,
|
|
639
|
-
routes: ResolvedRoute[]
|
|
640
|
-
): DevelopmentRouteSnapshot {
|
|
641
|
-
const searchRoutes = createSearchRouteMetadata(routes);
|
|
642
|
-
return {
|
|
643
|
-
render: createNativeRouteRenderer(prefix, routes, root, "", searchRoutes),
|
|
644
|
-
root,
|
|
645
|
-
routes,
|
|
646
|
-
};
|
|
647
|
-
}
|
|
648
|
-
|
|
649
|
-
const nativeRouteRenderers = new WeakMap<FurinInstance, FurinRouteDispatcher>();
|
|
650
|
-
|
|
651
|
-
function dispatchNativeRoute(context: Parameters<FurinRouteDispatcher>[0]): unknown {
|
|
652
|
-
const { pathname } = new URL(context.request.url);
|
|
653
|
-
const renderer = nativeRouteRenderers.get(resolveInstanceByPath(pathname));
|
|
654
|
-
if (!renderer) {
|
|
655
|
-
throw new Error(`[furin] No route renderer is registered for ${JSON.stringify(pathname)}`);
|
|
656
|
-
}
|
|
657
|
-
return renderer(context);
|
|
658
|
-
}
|
|
659
|
-
|
|
660
|
-
function hydrateSSGCacheFromCompileContext(ctx: CompileContext): void {
|
|
661
|
-
if (!ctx.ssgCache) {
|
|
662
|
-
return;
|
|
663
|
-
}
|
|
664
|
-
for (const [path, entry] of Object.entries(ctx.ssgCache)) {
|
|
665
|
-
setSSGCache(path, entry);
|
|
666
|
-
}
|
|
667
|
-
}
|
|
61
|
+
// Read the parent's generics through Elysia's `~` carriers: inferring them from
|
|
62
|
+
// the invariant class collapses function-valued macros to `never`. Scope has
|
|
63
|
+
// no carrier, so it alone is inferred.
|
|
64
|
+
type FurinApplication<App extends AnyElysia> =
|
|
65
|
+
App extends Elysia<
|
|
66
|
+
App["~Prefix"],
|
|
67
|
+
infer Scope,
|
|
68
|
+
App["~Singleton"],
|
|
69
|
+
App["~Definitions"],
|
|
70
|
+
App["~Metadata"],
|
|
71
|
+
App["~Routes"],
|
|
72
|
+
App["~Ephemeral"],
|
|
73
|
+
App["~Volatile"]
|
|
74
|
+
>
|
|
75
|
+
? Elysia<
|
|
76
|
+
App["~Prefix"],
|
|
77
|
+
Scope,
|
|
78
|
+
{
|
|
79
|
+
decorator: App["~Singleton"]["decorator"];
|
|
80
|
+
derive: App["~Singleton"]["derive"] & { log: RequestLogger };
|
|
81
|
+
store: App["~Singleton"]["store"];
|
|
82
|
+
},
|
|
83
|
+
App["~Definitions"],
|
|
84
|
+
App["~Metadata"],
|
|
85
|
+
App["~Routes"],
|
|
86
|
+
App["~Ephemeral"],
|
|
87
|
+
App["~Volatile"]
|
|
88
|
+
>
|
|
89
|
+
: never;
|
|
668
90
|
|
|
669
91
|
/** Options for the {@link furin} plugin. */
|
|
670
|
-
export type FurinLoggerOptions = FurinEvlogOptions & Pick<LoggerConfig, "sampling">;
|
|
671
|
-
|
|
672
92
|
export interface FurinOptions {
|
|
93
|
+
/**
|
|
94
|
+
* CDN purge hook for this app. Receives the physical (prefixed) paths that
|
|
95
|
+
* `revalidatePath()` and `revalidateTag()` invalidate. Takes precedence over
|
|
96
|
+
* the process-wide `setCachePurger()`.
|
|
97
|
+
*/
|
|
98
|
+
cachePurger?: CachePurger;
|
|
99
|
+
/**
|
|
100
|
+
* CDN purge hook for this app's semantic cache tags. Receives the tags passed
|
|
101
|
+
* to `revalidateTag()`. Takes precedence over the tag purger installed by
|
|
102
|
+
* the deployment adapter (e.g. Vercel). Runtimes sharing one purger get a
|
|
103
|
+
* single call per `revalidateTag()`.
|
|
104
|
+
*/
|
|
105
|
+
cacheTagPurger?: CachePurger;
|
|
673
106
|
/**
|
|
674
107
|
* Production only: explicit directory holding this app's built client
|
|
675
108
|
* assets (chunks + index.html template). Packaged furin apps pass their own
|
|
@@ -693,10 +126,14 @@ export interface FurinOptions {
|
|
|
693
126
|
pageCache?: PageCacheAdapter;
|
|
694
127
|
pagesDir?: string;
|
|
695
128
|
/**
|
|
696
|
-
*
|
|
697
|
-
* endpoints (`/_furin/*`) and client assets (`/_client/*`) are
|
|
698
|
-
* it, and the client bundle is built with
|
|
699
|
-
* to `""` (root).
|
|
129
|
+
* Full physical mount path of this app, e.g. `"/admin"`. All pages,
|
|
130
|
+
* framework endpoints (`/_furin/*`) and client assets (`/_client/*`) are
|
|
131
|
+
* served under it, and the client bundle is built with it as basePath.
|
|
132
|
+
* Defaults to `""` (root). Mount Furin on an Elysia instance without a
|
|
133
|
+
* prefix: under `new Elysia({ prefix: "/outer" })`, declare
|
|
134
|
+
* `furin({ prefix: "/outer/admin" })` on an unprefixed child instead.
|
|
135
|
+
* Mounting two furin instances on the same prefix, or one pagesDir under
|
|
136
|
+
* two prefixes, throws.
|
|
700
137
|
*/
|
|
701
138
|
prefix?: string;
|
|
702
139
|
/**
|
|
@@ -712,9 +149,9 @@ function configurePageCache(
|
|
|
712
149
|
ctx: CompileContext | null,
|
|
713
150
|
pageCache: PageCacheAdapter | undefined
|
|
714
151
|
): void {
|
|
715
|
-
if (pageCache !== undefined && ctx?.
|
|
152
|
+
if (pageCache !== undefined && ctx?.adapter?.capabilities.publicCache === "platform") {
|
|
716
153
|
throw new Error(
|
|
717
|
-
|
|
154
|
+
`[furin] pageCache cannot be configured with the ${JSON.stringify(ctx.adapter.name)} adapter: its platform caches public SSG, ISR, and PPR pages.`
|
|
718
155
|
);
|
|
719
156
|
}
|
|
720
157
|
if (pageCache !== undefined) {
|
|
@@ -722,495 +159,225 @@ function configurePageCache(
|
|
|
722
159
|
}
|
|
723
160
|
}
|
|
724
161
|
|
|
725
|
-
/**
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
sync,
|
|
749
|
-
}: FurinOptions = {}) {
|
|
750
|
-
const prefix = normalizePrefix(rawPrefix);
|
|
751
|
-
const syncPath = resolveSyncPath(sync);
|
|
752
|
-
const elysiaLoggerOptions = initializeLogger(logger);
|
|
162
|
+
/** Runtimes started by each composed application, by prefix. */
|
|
163
|
+
const startedRuntimes = new WeakMap<AnyElysia, Map<string, FurinInstance>>();
|
|
164
|
+
|
|
165
|
+
function assertMountedPrefix(instance: FurinInstance, app: AnyElysia, owner: AnyElysia): void {
|
|
166
|
+
const anchor = app.routes.find((route) => typeof route.handler === "function");
|
|
167
|
+
const mounted =
|
|
168
|
+
anchor &&
|
|
169
|
+
owner.routes.find(
|
|
170
|
+
(route) =>
|
|
171
|
+
route.method === anchor.method &&
|
|
172
|
+
route.handler === anchor.handler &&
|
|
173
|
+
route.path.endsWith(anchor.path)
|
|
174
|
+
);
|
|
175
|
+
if (!(anchor && mounted) || mounted.path === anchor.path) {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
const found = normalizePrefix(
|
|
179
|
+
`${mounted.path.slice(0, mounted.path.length - anchor.path.length)}${instance.prefix}`
|
|
180
|
+
);
|
|
181
|
+
throw new Error(
|
|
182
|
+
`[furin] furin({ prefix: ${JSON.stringify(instance.prefix)} }) is served at ${JSON.stringify(found)}. The prefix is build-time configuration (the client bundle is built for it), so it must be the full physical path: when mounting under a prefixed parent, repeat the parent prefix in furin({ prefix: ${JSON.stringify(found)} }) and mount Furin on an Elysia instance without a prefix.`
|
|
183
|
+
);
|
|
184
|
+
}
|
|
753
185
|
|
|
186
|
+
/** The application currently serving each runtime. */
|
|
187
|
+
const servingOwners = new WeakMap<FurinInstance, AnyElysia>();
|
|
188
|
+
|
|
189
|
+
function trackStartedRuntime(instance: FurinInstance, owner: AnyElysia): void {
|
|
190
|
+
const serving = servingOwners.get(instance);
|
|
191
|
+
if (serving && serving !== owner) {
|
|
192
|
+
throw new Error(
|
|
193
|
+
"[furin] This furin() runtime is already served by another Elysia application. Each plugin owns one runtime (caches, build ID, watchers): call furin() again for each application."
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
servingOwners.set(instance, owner);
|
|
197
|
+
const runtimes = startedRuntimes.get(owner) ?? new Map<string, FurinInstance>();
|
|
198
|
+
startedRuntimes.set(owner, runtimes);
|
|
199
|
+
const existing = runtimes.get(instance.prefix);
|
|
200
|
+
if (existing && existing !== instance) {
|
|
201
|
+
throw new Error(
|
|
202
|
+
`[furin] prefix "${instance.prefix || "/"}" is already mounted by pagesDir "${existing.pagesDir}" (attempted to mount "${instance.pagesDir}"). Give each furin() instance a unique prefix.`
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
assertOnePagesDirPerMount([...runtimes.values(), instance]);
|
|
206
|
+
runtimes.set(instance.prefix, instance);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
async function createFurinApp(options: FurinOptions): Promise<{
|
|
210
|
+
app: AnyElysia;
|
|
211
|
+
hasSync: boolean;
|
|
212
|
+
instance: FurinInstance;
|
|
213
|
+
}> {
|
|
214
|
+
const {
|
|
215
|
+
cachePurger,
|
|
216
|
+
cacheTagPurger,
|
|
217
|
+
clientDir,
|
|
218
|
+
clientLogging,
|
|
219
|
+
logger,
|
|
220
|
+
pageCache,
|
|
221
|
+
pagesDir,
|
|
222
|
+
sync,
|
|
223
|
+
} = options;
|
|
224
|
+
const prefix = normalizePrefix(options.prefix);
|
|
754
225
|
const cwd = process.cwd();
|
|
755
|
-
//
|
|
756
|
-
//
|
|
757
|
-
// lookup then falls back to the (stable) prefix, then to the sole context.
|
|
226
|
+
// In a deployed binary the cwd-resolved pagesDir may differ from the build
|
|
227
|
+
// machine's; the compile context is found by the (stable) declared prefix.
|
|
758
228
|
const paramPagesDir = resolve(cwd, pagesDir ?? "src/pages");
|
|
759
229
|
const ctx = getCompileContext(paramPagesDir, prefix);
|
|
760
|
-
const loggerPlugin = createLoggerPlugin(
|
|
761
|
-
prefix,
|
|
762
|
-
syncPath,
|
|
763
|
-
elysiaLoggerOptions,
|
|
764
|
-
clientLogging === true || ctx?.clientLogging === true
|
|
765
|
-
);
|
|
766
230
|
const resolvedPagesDir = ctx?.rootPath ? dirname(ctx.rootPath) : paramPagesDir;
|
|
767
|
-
|
|
768
|
-
// Unique name per pagesDir to avoid Elysia's name-based plugin dedup.
|
|
769
|
-
const instanceName = `furin-${prefix}-${resolvedPagesDir.replaceAll("\\", "/")}`;
|
|
770
|
-
|
|
771
|
-
// Same prefix + different pagesDir is a mount collision — fail fast, but
|
|
772
|
-
// only REGISTER right before returning so a failed mount leaves no stale
|
|
773
|
-
// registration behind. All per-app runtime state (build ID, caches,
|
|
774
|
-
// template, sync path) hangs off this object; requests are bound to it by
|
|
775
|
-
// path in wrapWithRequestScope.
|
|
776
231
|
const normalizedPagesDir = resolvedPagesDir.replaceAll("\\", "/");
|
|
777
|
-
|
|
232
|
+
|
|
233
|
+
// All per-app runtime state (build ID, caches, template, sync path) hangs
|
|
234
|
+
// off this object; the plugin's closures bind requests to it.
|
|
778
235
|
const instance = createInstance(prefix, normalizedPagesDir);
|
|
779
|
-
instance.syncPath =
|
|
236
|
+
instance.syncPath = resolveSyncPath(sync);
|
|
237
|
+
const loggerPlugin = createLoggerPlugin(
|
|
238
|
+
instance,
|
|
239
|
+
logger,
|
|
240
|
+
clientLogging === true || ctx?.clientLogging === true
|
|
241
|
+
);
|
|
780
242
|
configurePageCache(instance, ctx, pageCache);
|
|
243
|
+
setInstanceCachePurger(instance, cachePurger);
|
|
244
|
+
setInstanceCacheTagPurger(instance, cacheTagPurger);
|
|
245
|
+
registerInstance(instance);
|
|
781
246
|
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
registerDevPagePlugin();
|
|
797
|
-
const {
|
|
798
|
-
registerDevRoutesPlugin,
|
|
799
|
-
registerDevRouteTopologyWatcher,
|
|
800
|
-
routeModuleSpecifier,
|
|
801
|
-
routeSourcePaths,
|
|
802
|
-
} = await import("./plugin/routes.ts");
|
|
803
|
-
const routeInstance = { pagesDir: resolvedPagesDir, prefix };
|
|
804
|
-
registerDevRoutesPlugin([routeInstance]);
|
|
805
|
-
|
|
806
|
-
// TanStack-style config auto-fix: verify each route file's `layout`
|
|
807
|
-
// reference against the file-system tree and rewrite missing/misplaced
|
|
808
|
-
// ones. Idempotent and content-diff safe — the watcher resynchronizes the
|
|
809
|
-
// route-file signature after a rewrite, so the auto-fix cannot loop.
|
|
810
|
-
const { fixRouteConfigLayout } = await import("./plugin/route-config-autofix.ts");
|
|
811
|
-
const applyRouteConfigAutofix = (): boolean => {
|
|
812
|
-
let changed = false;
|
|
813
|
-
const sourcePaths = [
|
|
814
|
-
join(resolvedPagesDir, "root.tsx"),
|
|
815
|
-
...routeSourcePaths(routeInstance),
|
|
816
|
-
].filter((sourcePath) => existsSync(sourcePath));
|
|
817
|
-
for (const sourcePath of sourcePaths) {
|
|
818
|
-
let source: string;
|
|
819
|
-
try {
|
|
820
|
-
source = readFileSync(sourcePath, "utf8");
|
|
821
|
-
} catch (error) {
|
|
822
|
-
console.warn(
|
|
823
|
-
`[furin] Could not read ${sourcePath} for route config autofix: ${error instanceof Error ? error.message : String(error)}`
|
|
824
|
-
);
|
|
825
|
-
continue;
|
|
826
|
-
}
|
|
827
|
-
const fixed = fixRouteConfigLayout(source, sourcePath, resolvedPagesDir);
|
|
828
|
-
if (fixed !== null && fixed !== source) {
|
|
829
|
-
writeFileSync(sourcePath, fixed);
|
|
830
|
-
changed = true;
|
|
831
|
-
}
|
|
832
|
-
}
|
|
833
|
-
return changed;
|
|
834
|
-
};
|
|
835
|
-
applyRouteConfigAutofix();
|
|
836
|
-
|
|
837
|
-
const graph = devGraph(instance);
|
|
838
|
-
const { nativeRoutesApp, root, routes } = await withInstance(instance, async () => {
|
|
839
|
-
const { furinShell } = (await import(routeModuleSpecifier(routeInstance))) as {
|
|
840
|
-
furinShell: AnyElysia;
|
|
841
|
-
};
|
|
842
|
-
const loaded = await loadDevelopmentRoutes(resolvedPagesDir);
|
|
843
|
-
return { nativeRoutesApp: furinShell, ...loaded };
|
|
844
|
-
});
|
|
845
|
-
const initialSnapshot = createDevelopmentRouteSnapshot(prefix, root, routes);
|
|
846
|
-
const currentSnapshot = (): DevelopmentRouteSnapshot => graph.snapshot ?? initialSnapshot;
|
|
847
|
-
nativeRouteRenderers.set(instance, (context) => currentSnapshot().render(context));
|
|
848
|
-
let matchNavigationData = buildRouteMatcher(initialSnapshot.routes);
|
|
849
|
-
navigationDataMatchers.set(instance, (path) => matchNavigationData(path) !== null);
|
|
850
|
-
|
|
851
|
-
const { writeDevFiles } = await import("./build/hydrate.ts");
|
|
852
|
-
const { getHmrDataSignature } = await import("./plugin/transform-client.ts");
|
|
853
|
-
let serverSourceVersion = 0;
|
|
854
|
-
let serverDataSignatures = new Map<string, string>();
|
|
855
|
-
let serverSourcePaths: string | undefined;
|
|
856
|
-
const writeCurrentDevFiles = (
|
|
857
|
-
snapshot: DevelopmentRouteSnapshot,
|
|
858
|
-
changedSources: readonly string[]
|
|
859
|
-
): void => {
|
|
860
|
-
const paths = [
|
|
861
|
-
...new Set([
|
|
862
|
-
snapshot.root.path,
|
|
863
|
-
...snapshot.routes.flatMap((route) => [
|
|
864
|
-
route.path,
|
|
865
|
-
...route.routeChain.flatMap((entry) => (entry.sourcePath ? [entry.sourcePath] : [])),
|
|
866
|
-
]),
|
|
867
|
-
]),
|
|
868
|
-
].toSorted();
|
|
869
|
-
const nextPaths = JSON.stringify(paths);
|
|
870
|
-
const nextSignatures = new Map(
|
|
871
|
-
paths.map((path) => {
|
|
872
|
-
let signature: string;
|
|
873
|
-
try {
|
|
874
|
-
signature = getHmrDataSignature(readFileSync(path, "utf8"), path);
|
|
875
|
-
if (signature.startsWith("external:")) {
|
|
876
|
-
signature = graph.sourceVersion(path);
|
|
877
|
-
}
|
|
878
|
-
} catch {
|
|
879
|
-
// A broken route still needs a client entry for its diagnostic.
|
|
880
|
-
signature = graph.sourceVersion(path);
|
|
247
|
+
const { development } = instance;
|
|
248
|
+
const modeOptions: ModeOptions = {
|
|
249
|
+
clientLogging,
|
|
250
|
+
requestHooks: (runtimeApp) =>
|
|
251
|
+
(runtimeApp as Elysia)
|
|
252
|
+
.use(loggerPlugin)
|
|
253
|
+
// Local scope keeps hooks isolated from sibling Furin mounts.
|
|
254
|
+
.onError(NotFound, (context) =>
|
|
255
|
+
renderNotFound(instance, context as unknown as FurinNativeRouteContext)
|
|
256
|
+
)
|
|
257
|
+
.onAfterHandle(({ set }) => {
|
|
258
|
+
const pending = consumePendingInvalidations();
|
|
259
|
+
if (pending.length > 0) {
|
|
260
|
+
set.headers["x-furin-revalidate"] = serializeInvalidationPaths(pending);
|
|
881
261
|
}
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
);
|
|
885
|
-
// Route additions/removals already update the native client manifest.
|
|
886
|
-
// Rebase their signature without issuing a second data invalidation.
|
|
887
|
-
const changedPaths = new Set(changedSources);
|
|
888
|
-
const importsChanged = paths.some(
|
|
889
|
-
(path) =>
|
|
890
|
-
!changedPaths.has(path) && serverDataSignatures.get(path) !== nextSignatures.get(path)
|
|
891
|
-
);
|
|
892
|
-
if (importsChanged && serverSourcePaths === nextPaths) {
|
|
893
|
-
serverSourceVersion += 1;
|
|
894
|
-
}
|
|
895
|
-
serverSourcePaths = nextPaths;
|
|
896
|
-
serverDataSignatures = nextSignatures;
|
|
897
|
-
writeDevFiles(
|
|
898
|
-
snapshot.routes,
|
|
899
|
-
{
|
|
900
|
-
basePath: prefix,
|
|
901
|
-
clientLogging: clientLogging ?? false,
|
|
902
|
-
outDir: furinDir,
|
|
903
|
-
publicPath: `${prefix}/_client/`,
|
|
904
|
-
rootLayout: snapshot.root.path,
|
|
905
|
-
// furin-env.d.ts is one file at the project root — only the root
|
|
906
|
-
// instance owns it, otherwise mounted apps clobber each other's types.
|
|
907
|
-
skipRouteTypes: prefix !== "",
|
|
908
|
-
},
|
|
909
|
-
cwd,
|
|
910
|
-
String(serverSourceVersion)
|
|
911
|
-
);
|
|
912
|
-
};
|
|
913
|
-
writeCurrentDevFiles(initialSnapshot, []);
|
|
914
|
-
graph.commit(initialSnapshot);
|
|
915
|
-
const hmrEntry = (await import(join(furinDir, "index.html"))).default;
|
|
916
|
-
const refreshDevelopmentRoutes = (changedSources: readonly string[]): Promise<void> =>
|
|
917
|
-
withInstance(instance, async () => {
|
|
918
|
-
invalidateStampedRouteModules();
|
|
919
|
-
try {
|
|
920
|
-
const previousSnapshot = currentSnapshot();
|
|
921
|
-
const repaired = repairedDevelopmentRoutes(previousSnapshot, changedSources, graph);
|
|
922
|
-
const next = await loadDevelopmentRoutes(resolvedPagesDir);
|
|
923
|
-
// Retain tags only for routes whose current module could not be loaded.
|
|
924
|
-
const nextRoutes = next.routes.map((route) =>
|
|
925
|
-
route.routeChain.length > 0
|
|
926
|
-
? route
|
|
927
|
-
: {
|
|
928
|
-
...route,
|
|
929
|
-
tags: previousSnapshot.routes.find((previous) => previous.path === route.path)
|
|
930
|
-
?.tags,
|
|
931
|
-
}
|
|
932
|
-
);
|
|
933
|
-
const nextSnapshot = createDevelopmentRouteSnapshot(prefix, next.root, nextRoutes);
|
|
934
|
-
writeCurrentDevFiles(nextSnapshot, changedSources);
|
|
935
|
-
graph.commit(nextSnapshot);
|
|
936
|
-
matchNavigationData = buildRouteMatcher(nextSnapshot.routes);
|
|
937
|
-
const diagnostics = devDiagnosticStore(instance);
|
|
938
|
-
if (repaired.root) {
|
|
939
|
-
diagnostics.markReady("*");
|
|
262
|
+
if (!development && instance.buildId) {
|
|
263
|
+
set.headers["x-furin-build-id"] = instance.buildId;
|
|
940
264
|
}
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
phase: "import",
|
|
950
|
-
route: "*",
|
|
951
|
-
});
|
|
952
|
-
throw error;
|
|
953
|
-
}
|
|
954
|
-
});
|
|
955
|
-
const publicDir = resolve(cwd, "public");
|
|
956
|
-
const publicExists = existsSync(publicDir);
|
|
957
|
-
let routeTopologyWatcher: ReturnType<typeof registerDevRouteTopologyWatcher> | undefined;
|
|
958
|
-
navigationDataRefreshers.set(instance, async () => {
|
|
959
|
-
await routeTopologyWatcher?.refresh();
|
|
960
|
-
});
|
|
961
|
-
|
|
962
|
-
// Routes registered below are LOGICAL — Elysia's `prefix` makes them
|
|
963
|
-
// physical when this plugin is merged into the parent app (child prefixes
|
|
964
|
-
// like staticPlugin's compose underneath).
|
|
965
|
-
const devApp = new Elysia({
|
|
966
|
-
name: instanceName,
|
|
967
|
-
prefix: prefix || undefined,
|
|
968
|
-
seed: resolvedPagesDir,
|
|
969
|
-
})
|
|
970
|
-
.setup(() => {
|
|
971
|
-
routeTopologyWatcher = registerDevRouteTopologyWatcher({
|
|
972
|
-
instance: routeInstance,
|
|
973
|
-
onRouteFilesTouched: async (sourcePaths) => {
|
|
974
|
-
applyRouteConfigAutofix();
|
|
975
|
-
await refreshDevelopmentRoutes(sourcePaths);
|
|
976
|
-
},
|
|
977
|
-
onSourceError: (error, sourcePath) => {
|
|
978
|
-
const route = currentSnapshot().routes.find((candidate) =>
|
|
979
|
-
graph.dependsOn(candidate.path, sourcePath)
|
|
980
|
-
);
|
|
981
|
-
withInstance(instance, () =>
|
|
982
|
-
publishDevDiagnostic(error, {
|
|
983
|
-
entryPath: route?.path ?? sourcePath,
|
|
984
|
-
phase: "transform",
|
|
985
|
-
route: route?.pattern ?? "*",
|
|
986
|
-
})
|
|
987
|
-
);
|
|
988
|
-
},
|
|
989
|
-
onTopologyChange: async (sourcePaths) => {
|
|
990
|
-
// Bun --hot cannot be triggered from generated artifacts (its
|
|
991
|
-
// watch graph is the entry's static imports), so topology changes
|
|
992
|
-
// are served by swapping the dispatcher's route matcher. Hot-added
|
|
993
|
-
// routes then resolve through the NOT_FOUND fallback below;
|
|
994
|
-
// removed routes 404 through the renderer's miss path.
|
|
995
|
-
applyRouteConfigAutofix();
|
|
996
|
-
await refreshDevelopmentRoutes(sourcePaths);
|
|
997
|
-
},
|
|
998
|
-
});
|
|
999
|
-
})
|
|
1000
|
-
.cleanup(() => {
|
|
1001
|
-
routeTopologyWatcher?.close();
|
|
1002
|
-
routeTopologyWatcher = undefined;
|
|
1003
|
-
navigationDataRefreshers.delete(instance);
|
|
1004
|
-
navigationDataMatchers.delete(instance);
|
|
1005
|
-
})
|
|
1006
|
-
.get("/_bun_hmr_entry/index.html", hmrEntry)
|
|
1007
|
-
.get("/_bun_hmr_entry", hmrEntry)
|
|
1008
|
-
.use(loggerPlugin)
|
|
1009
|
-
// Local scope (default) — a global hook would leak onto sibling furin
|
|
1010
|
-
// instances mounted on the same parent app.
|
|
1011
|
-
.error(NotFound, async ({ request, server }) =>
|
|
1012
|
-
renderRootNotFound(currentSnapshot().root, request, server?.url.origin)
|
|
1013
|
-
)
|
|
1014
|
-
.afterHandle(({ set }) => {
|
|
1015
|
-
// Forward pending revalidation paths so the client can bust its prefetch cache
|
|
1016
|
-
const pending = consumePendingInvalidations();
|
|
1017
|
-
if (pending.length > 0) {
|
|
1018
|
-
set.headers["x-furin-revalidate"] = pending.join(",");
|
|
1019
|
-
}
|
|
1020
|
-
})
|
|
1021
|
-
.use(
|
|
1022
|
-
publicExists ? await staticPlugin({ assets: publicDir, prefix: "/public" }) : new Elysia()
|
|
1023
|
-
)
|
|
1024
|
-
.get(
|
|
1025
|
-
"/favicon.ico",
|
|
1026
|
-
publicExists
|
|
1027
|
-
? file(join(publicDir, "favicon.ico"))
|
|
1028
|
-
: () => new Response(null, { status: 404 })
|
|
1029
|
-
)
|
|
1030
|
-
.use(
|
|
1031
|
-
(await import("./server/browser-events/plugin.ts")).createBrowserEventsPlugin({
|
|
1032
|
-
sources: createDevelopmentBrowserEventSources(instance, devDiagnosticStore(instance)),
|
|
1033
|
-
sync: sync || undefined,
|
|
1034
|
-
})
|
|
1035
|
-
)
|
|
1036
|
-
.use(
|
|
1037
|
-
createDevDiagnosticPlugin(devDiagnosticStore(instance), instance, async () => {
|
|
1038
|
-
await routeTopologyWatcher?.refresh();
|
|
1039
|
-
})
|
|
1040
|
-
)
|
|
1041
|
-
.use(createInstrumentationPlugin(() => currentSnapshot().routes, syncPath))
|
|
1042
|
-
.use(
|
|
1043
|
-
sync
|
|
1044
|
-
? (await import("./server/sync/stream.ts")).createSyncChangesPlugin(sync)
|
|
1045
|
-
: new Elysia()
|
|
265
|
+
}),
|
|
266
|
+
sync: sync || undefined,
|
|
267
|
+
};
|
|
268
|
+
const mode: ModeRuntime = development
|
|
269
|
+
? await (await import("./server/dev/runtime.ts")).createDevRuntime(
|
|
270
|
+
instance,
|
|
271
|
+
resolvedPagesDir,
|
|
272
|
+
modeOptions
|
|
1046
273
|
)
|
|
1047
|
-
|
|
1048
|
-
.use(nativeRoutesApp)
|
|
1049
|
-
.use(
|
|
1050
|
-
createNotFoundHandling(prefix, routes, root, async (notFoundContext) => {
|
|
1051
|
-
// Dev topology: try the (watcher-refreshed) native renderer before
|
|
1052
|
-
// the root not-found page, so hot-added routes are served without
|
|
1053
|
-
// a restart. Instances outside this pathname's prefix are skipped.
|
|
1054
|
-
const { pathname } = new URL(notFoundContext.request.url);
|
|
1055
|
-
if (!nativeRouteRenderers.has(resolveInstanceByPath(pathname))) {
|
|
1056
|
-
return;
|
|
1057
|
-
}
|
|
1058
|
-
return await dispatchNativeRoute(
|
|
1059
|
-
notFoundContext as unknown as Parameters<FurinRouteDispatcher>[0]
|
|
1060
|
-
);
|
|
1061
|
-
})
|
|
1062
|
-
);
|
|
1063
|
-
registerInstance(instance);
|
|
1064
|
-
return createFurinPlugin(devApp, prefix, Boolean(sync));
|
|
1065
|
-
}
|
|
274
|
+
: await createProdRuntime(instance, ctx, clientDir, modeOptions);
|
|
1066
275
|
|
|
1067
|
-
//
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
}
|
|
1071
|
-
const { root, routes } = loadProdRoutes(ctx);
|
|
1072
|
-
const searchRoutes = createSearchRouteMetadata(routes);
|
|
1073
|
-
const prodBuildId = ctx.buildId ?? "";
|
|
1074
|
-
if (!ctx.nativeRoutes) {
|
|
1075
|
-
throw new Error("[furin] Production build is missing the composed Elysia route app.");
|
|
1076
|
-
}
|
|
1077
|
-
const renderNativeRoute = createNativeRouteRenderer(
|
|
1078
|
-
prefix,
|
|
1079
|
-
routes,
|
|
1080
|
-
root,
|
|
1081
|
-
prodBuildId,
|
|
1082
|
-
searchRoutes
|
|
1083
|
-
);
|
|
1084
|
-
nativeRouteRenderers.set(instance, renderNativeRoute);
|
|
1085
|
-
const matchNavigationData = buildRouteMatcher(routes);
|
|
1086
|
-
navigationDataMatchers.set(instance, (path) => matchNavigationData(path) !== null);
|
|
1087
|
-
instance.buildId = prodBuildId;
|
|
1088
|
-
// Init-time writes target THIS instance explicitly — with several mounted
|
|
1089
|
-
// apps there is no ambient request scope to resolve it from.
|
|
1090
|
-
withInstance(instance, () => {
|
|
1091
|
-
hydrateSSGCacheFromCompileContext(ctx);
|
|
1092
|
-
});
|
|
1093
|
-
|
|
1094
|
-
const embedded = ctx?.embedded;
|
|
1095
|
-
const clientDir =
|
|
1096
|
-
embedded?.clientDir ?? explicitClientDir ?? ctx.clientDir ?? resolveClientDirFromArgv(prefix);
|
|
1097
|
-
await setupCompiledTemplate(ctx, embedded, clientDir, instance);
|
|
1098
|
-
|
|
1099
|
-
const prodApp = new Elysia({
|
|
1100
|
-
name: instanceName,
|
|
276
|
+
// Unique name per pagesDir to avoid Elysia's name-based plugin dedup.
|
|
277
|
+
const app: AnyElysia = new Elysia({
|
|
278
|
+
name: `furin-${prefix}-${normalizedPagesDir}`,
|
|
1101
279
|
prefix: prefix || undefined,
|
|
1102
280
|
seed: resolvedPagesDir,
|
|
1103
281
|
})
|
|
1104
|
-
.
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
renderRootNotFound(root, request, server?.url.origin)
|
|
1109
|
-
)
|
|
1110
|
-
.afterHandle(({ set }) => {
|
|
1111
|
-
// Forward pending revalidation paths so the client can bust its prefetch cache
|
|
1112
|
-
const pending = consumePendingInvalidations();
|
|
1113
|
-
if (pending.length > 0) {
|
|
1114
|
-
set.headers["x-furin-revalidate"] = pending.join(",");
|
|
1115
|
-
}
|
|
1116
|
-
// Tell the client the current build ID so it can detect stale deploys
|
|
1117
|
-
if (instance.buildId) {
|
|
1118
|
-
set.headers["x-furin-build-id"] = instance.buildId;
|
|
1119
|
-
}
|
|
282
|
+
.wrap(createRequestScope(instance))
|
|
283
|
+
.onStart((owner) => {
|
|
284
|
+
assertMountedPrefix(instance, app, owner);
|
|
285
|
+
trackStartedRuntime(instance, owner);
|
|
1120
286
|
})
|
|
1121
|
-
.
|
|
1122
|
-
|
|
1123
|
-
|
|
287
|
+
.onStop((owner) => {
|
|
288
|
+
startedRuntimes.get(owner)?.delete(instance.prefix);
|
|
289
|
+
if (servingOwners.get(instance) === owner) {
|
|
290
|
+
servingOwners.delete(instance);
|
|
291
|
+
unregisterInstance(instance);
|
|
1124
292
|
}
|
|
1125
|
-
const origin = server?.url?.origin ?? "http://localhost:3000";
|
|
1126
|
-
// Synthetic (non-request) renders — bind them to this instance so the
|
|
1127
|
-
// render pipeline resolves its template/caches, not a sibling's.
|
|
1128
|
-
await withInstance(instance, () => warmSSGCache(routes, root, origin, searchRoutes));
|
|
1129
293
|
})
|
|
1130
|
-
.use(
|
|
1131
|
-
.use(await createProductionBrowserEventsPlugin(sync, ctx?.deploymentTarget))
|
|
294
|
+
.use(mode.plugins)
|
|
1132
295
|
.use(
|
|
1133
|
-
sync
|
|
296
|
+
sync
|
|
297
|
+
? (await import("./server/sync/stream.ts")).createRuntimeSyncChangesPlugin(
|
|
298
|
+
sync,
|
|
299
|
+
development
|
|
300
|
+
)
|
|
301
|
+
: new Elysia()
|
|
1134
302
|
)
|
|
1135
303
|
.decorate(FURIN_RENDER_DECORATOR, dispatchNativeRoute)
|
|
1136
|
-
.use(
|
|
1137
|
-
.use(createNotFoundHandling(
|
|
1138
|
-
|
|
1139
|
-
return createFurinPlugin(prodApp, undefined, Boolean(sync));
|
|
304
|
+
.use(mode.nativeRoutes)
|
|
305
|
+
.use(createNotFoundHandling(instance));
|
|
306
|
+
return { app, hasSync: Boolean(sync), instance };
|
|
1140
307
|
}
|
|
1141
308
|
|
|
309
|
+
/** Parent applications and the prefixes their Furin plugins claim. */
|
|
310
|
+
const mountedPrefixes = new WeakMap<AnyElysia, Map<string, FurinInstance>>();
|
|
311
|
+
|
|
1142
312
|
/**
|
|
1143
|
-
*
|
|
313
|
+
* Main Furin plugin.
|
|
314
|
+
*
|
|
315
|
+
* Returns an Elysia plugin function for exactly one application. Applying the
|
|
316
|
+
* function to the parent app before `listen()` lets development register
|
|
317
|
+
* Bun's native HTML bundle in the initial server options while preserving
|
|
318
|
+
* Elysia's route composition.
|
|
319
|
+
*
|
|
320
|
+
* ## Usage
|
|
1144
321
|
*
|
|
1145
|
-
*
|
|
1146
|
-
*
|
|
1147
|
-
*
|
|
1148
|
-
*
|
|
1149
|
-
*
|
|
1150
|
-
*
|
|
1151
|
-
* the instance registers a lowest-priority catch-all under its own prefix;
|
|
1152
|
-
* the router prefers every more-specific route, so this only fires for
|
|
1153
|
-
* paths no page matched. Skipped when the app defines its own `[...rest]`
|
|
1154
|
-
* catch-all page.
|
|
322
|
+
* ```ts
|
|
323
|
+
* new Elysia()
|
|
324
|
+
* .use(await furin({ pagesDir: "./src/pages" }))
|
|
325
|
+
* .use(await furin({ pagesDir: "./src/admin", prefix: "/admin" }))
|
|
326
|
+
* .listen(3000)
|
|
327
|
+
* ```
|
|
1155
328
|
*/
|
|
1156
|
-
function
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
)
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
if (rendered !== undefined && rendered !== null) {
|
|
1181
|
-
return rendered;
|
|
1182
|
-
}
|
|
1183
|
-
}
|
|
1184
|
-
return await renderRootNotFound(root, context.request, context.server?.url.origin);
|
|
1185
|
-
});
|
|
1186
|
-
return app;
|
|
1187
|
-
}
|
|
1188
|
-
app.get("/*", async (context) => {
|
|
1189
|
-
if (dispatch) {
|
|
1190
|
-
const rendered = await dispatch(context);
|
|
1191
|
-
if (rendered !== undefined && rendered !== null) {
|
|
1192
|
-
return rendered;
|
|
1193
|
-
}
|
|
329
|
+
export async function furin(options?: FurinOptions) {
|
|
330
|
+
const registration = {
|
|
331
|
+
pagesDir: options?.pagesDir ?? "src/pages",
|
|
332
|
+
prefix: normalizePrefix(options?.prefix),
|
|
333
|
+
};
|
|
334
|
+
if (recordFurinRegistration(registration)) {
|
|
335
|
+
// The build only discovers mounts: no runtime, nothing to mount.
|
|
336
|
+
return <ParentApp extends AnyElysia>(parentApp: ParentApp) =>
|
|
337
|
+
parentApp as unknown as FurinApplication<ParentApp>;
|
|
338
|
+
}
|
|
339
|
+
const { app, hasSync, instance } = await createFurinApp(options ?? {});
|
|
340
|
+
let mounted = false;
|
|
341
|
+
return <ParentApp extends AnyElysia>(parentApp: ParentApp): FurinApplication<ParentApp> => {
|
|
342
|
+
if (mounted) {
|
|
343
|
+
throw new Error(
|
|
344
|
+
"[furin] This furin() plugin is already mounted. Each plugin owns one runtime (caches, build ID, watchers): call furin() again for each application."
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
const prefixes = mountedPrefixes.get(parentApp) ?? new Map<string, FurinInstance>();
|
|
348
|
+
const existing = prefixes.get(instance.prefix);
|
|
349
|
+
if (existing) {
|
|
350
|
+
throw new Error(
|
|
351
|
+
`[furin] prefix "${instance.prefix || "/"}" is already mounted by pagesDir "${existing.pagesDir}" (attempted to mount "${instance.pagesDir}"). Give each furin() instance a unique prefix.`
|
|
352
|
+
);
|
|
1194
353
|
}
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
354
|
+
assertOnePagesDirPerMount([...prefixes.values(), instance]);
|
|
355
|
+
mounted = true;
|
|
356
|
+
prefixes.set(instance.prefix, instance);
|
|
357
|
+
mountedPrefixes.set(parentApp, prefixes);
|
|
358
|
+
if (hasSync) {
|
|
359
|
+
bindSyncValidation(parentApp);
|
|
360
|
+
}
|
|
361
|
+
// The mounted evlog plugin contributes only log; preserve the parent's
|
|
362
|
+
// native route, schema and macro types instead of merging AnyElysia.
|
|
363
|
+
return parentApp.use(app) as unknown as FurinApplication<ParentApp>;
|
|
364
|
+
};
|
|
1198
365
|
}
|
|
1199
366
|
|
|
1200
367
|
export { FurinErrorBoundary, FurinNotFoundBoundary } from "./client/boundaries.tsx";
|
|
1201
368
|
export { HeadContent, Scripts } from "./client/document.tsx";
|
|
369
|
+
export type { Register } from "./routes.ts";
|
|
1202
370
|
export type { InvalidationInput, InvalidationRule } from "./server/auto-invalidate/index.ts";
|
|
1203
371
|
export { furinInvalidate, revalidateTag } from "./server/auto-invalidate/index.ts";
|
|
1204
372
|
export { revalidatePath, setCachePurger } from "./server/cache/invalidation.ts";
|
|
1205
|
-
export {
|
|
373
|
+
export { buildErrorElement, renderRootNotFound } from "./server/render/index.ts";
|
|
1206
374
|
export {
|
|
1207
375
|
type FurinSyncOption,
|
|
1208
376
|
type FurinSyncOptions,
|
|
1209
377
|
furinSync,
|
|
378
|
+
invalidateSync,
|
|
1210
379
|
type SyncAdapter,
|
|
1211
|
-
type SyncInput,
|
|
1212
380
|
type SyncNotifier,
|
|
1213
|
-
type SyncRouteOption,
|
|
1214
381
|
type SyncRuntimeOptions,
|
|
1215
382
|
} from "./server/sync/index.ts";
|
|
1216
383
|
export { Await, useAsyncError, useAsyncValue } from "./shared/await.tsx";
|