@ecopages/core 0.8.0 → 0.10.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 +2 -2
- package/package.json +14 -4
- package/src/adapters/node/create-app.d.ts +5 -5
- package/src/adapters/node/create-app.js +1 -1
- package/src/adapters/node/server-adapter.d.ts +1 -0
- package/src/adapters/node/server-adapter.js +4 -1
- package/src/adapters/shared/hmr/hmr-manager.d.ts +2 -0
- package/src/adapters/shared/hmr/hmr-manager.js +8 -4
- package/src/adapters/shared/http/api-request-pipeline.d.ts +16 -3
- package/src/adapters/shared/http/api-request-pipeline.js +23 -16
- package/src/adapters/shared/http/define-api-handler.d.ts +8 -8
- package/src/adapters/shared/http/explicit-static-route-matcher.js +8 -4
- package/src/adapters/shared/http/file-route-middleware-pipeline.d.ts +10 -11
- package/src/adapters/shared/http/file-route-middleware-pipeline.js +5 -6
- package/src/adapters/shared/http/fs-server-response-matcher.js +12 -4
- package/src/adapters/shared/http/run-middleware-chain.d.ts +29 -2
- package/src/adapters/shared/http/run-middleware-chain.js +108 -10
- package/src/adapters/shared/runtime/render-context.d.ts +2 -0
- package/src/adapters/shared/runtime/render-context.js +6 -21
- package/src/adapters/shared/runtime/server-request-pipeline.d.ts +2 -2
- package/src/adapters/shared/runtime/server-request-pipeline.js +4 -3
- package/src/adapters/shared/runtime/server-static-builder.js +4 -1
- package/src/adapters/shared/runtime/unattached-scripts.d.ts +17 -0
- package/src/adapters/shared/runtime/unattached-scripts.js +57 -0
- package/src/adapters/shared/ws/node-http-websocket-upgrades.js +3 -2
- package/src/adapters/shared/ws/ws-pattern-matcher.d.ts +14 -1
- package/src/adapters/shared/ws/ws-pattern-matcher.js +14 -13
- package/src/assets/app-browser-client-build.js +3 -0
- package/src/assets/asset-processing-service/asset-processing.service.d.ts +9 -0
- package/src/assets/asset-processing-service/asset-processing.service.js +19 -0
- package/src/assets/asset-processing-service/processed-asset-ledger.d.ts +16 -2
- package/src/assets/asset-processing-service/processed-asset-ledger.js +31 -3
- package/src/build/build-graph.d.ts +19 -2
- package/src/build/build-graph.js +19 -18
- package/src/build/cache/pages-unified-graph-build.d.ts +4 -6
- package/src/build/cache/pages-unified-graph-build.js +14 -51
- package/src/build/cache/server-module-entries.d.ts +5 -9
- package/src/build/cache/server-module-entries.js +5 -58
- package/src/build/contracts/build-contracts.d.ts +15 -0
- package/src/build/contracts.d.ts +1 -1
- package/src/build/contracts.js +1 -1
- package/src/build/production-asset-ledger.d.ts +1 -1
- package/src/build/production-asset-ledger.js +4 -2
- package/src/build/rolldown/file-role-grammar-plugin.d.ts +11 -0
- package/src/build/rolldown/file-role-grammar-plugin.js +32 -0
- package/src/build/rolldown/rolldown-adapter-helpers.d.ts +11 -0
- package/src/build/rolldown/rolldown-adapter-helpers.js +79 -10
- package/src/build/rolldown/ssr-script-entries-plugin.d.ts +31 -0
- package/src/build/rolldown/ssr-script-entries-plugin.js +104 -0
- package/src/build/runtime/build-request-policy.js +4 -1
- package/src/build/server/server-modules-manifest.d.ts +6 -1
- package/src/build/server/server-modules-manifest.js +10 -1
- package/src/build/server/view-reference.d.ts +9 -0
- package/src/build/server/view-reference.js +11 -0
- package/src/build.d.ts +3 -1
- package/src/build.js +3 -1
- package/src/cli.d.ts +14 -0
- package/src/cli.js +14 -0
- package/src/config/assembly/app-runtime.d.ts +13 -0
- package/src/config/assembly/app-runtime.js +2 -0
- package/src/config/assembly/finalize-config.js +13 -8
- package/src/config/internal-types.d.ts +0 -12
- package/src/config/resolve-eco-config-path.d.ts +2 -0
- package/src/config/resolve-eco-config-path.js +2 -1
- package/src/config/server-config-bundle.d.ts +0 -1
- package/src/config/server-config-bundle.js +1 -1
- package/src/config/user-config-types.d.ts +0 -12
- package/src/declarations.d.ts +2 -0
- package/src/dev/invalidation/development-invalidation.service.d.ts +0 -2
- package/src/dev/invalidation/development-invalidation.service.js +1 -11
- package/src/dev/transform-server/dev-transform-vendor-registry.js +33 -19
- package/src/eco/discovered-dependencies.d.ts +5 -0
- package/src/eco/discovered-dependencies.js +9 -0
- package/src/eco/eco.browser.js +6 -26
- package/src/eco/eco.js +24 -16
- package/src/eco/migration-docs.d.ts +7 -0
- package/src/eco/migration-docs.js +7 -0
- package/src/eco/page-data-hooks.d.ts +27 -0
- package/src/eco/page-data-hooks.js +64 -0
- package/src/eco/page-layout-normalization.d.ts +2 -2
- package/src/hmr/client/hmr-runtime.js +1 -1
- package/src/hmr/strategies/js-hmr-strategy.d.ts +0 -2
- package/src/hmr/strategies/server-rendered-template-hmr-strategy.d.ts +2 -1
- package/src/hmr/strategies/server-rendered-template-hmr-strategy.js +4 -3
- package/src/html/html-rewriter.d.ts +9 -7
- package/src/html/html-rewriter.js +26 -41
- package/src/html/html-transformer.service.d.ts +2 -2
- package/src/html/html-transformer.service.js +21 -15
- package/src/html-pages/html-page-module-scripts.js +2 -1
- package/src/html-pages/html-pages.plugin.d.ts +1 -1
- package/src/html-pages/html-pages.plugin.js +1 -1
- package/src/index.d.ts +1 -0
- package/src/index.js +1 -0
- package/src/integration.d.ts +2 -2
- package/src/integration.js +2 -2
- package/src/islands/island-host.d.ts +16 -5
- package/src/islands/island-host.js +8 -1
- package/src/middleware/create-middleware.d.ts +41 -0
- package/src/middleware/create-middleware.js +72 -0
- package/src/plugins/component-import-discovery.d.ts +14 -1
- package/src/plugins/component-import-discovery.js +36 -16
- package/src/plugins/eco-component-meta-plugin.d.ts +38 -0
- package/src/plugins/eco-component-meta-plugin.js +208 -44
- package/src/plugins/ecopages-virtual-imports.d.ts +15 -0
- package/src/plugins/ecopages-virtual-imports.js +25 -0
- package/src/plugins/integration-plugin.d.ts +1 -1
- package/src/plugins/integration-plugin.js +1 -1
- package/src/plugins/tsconfig-import-resolver.d.ts +2 -0
- package/src/plugins/tsconfig-import-resolver.js +7 -5
- package/src/route-renderer/error-pages/error-page-renderer.js +32 -3
- package/src/route-renderer/explicit-view-render-preparation.d.ts +28 -3
- package/src/route-renderer/explicit-view-render-preparation.js +63 -11
- package/src/route-renderer/orchestration/document-shell/layout-shell-props.service.d.ts +15 -5
- package/src/route-renderer/orchestration/document-shell/layout-shell-props.service.js +27 -15
- package/src/route-renderer/orchestration/foreign-child/foreign-subtree-execution.service.d.ts +24 -9
- package/src/route-renderer/orchestration/foreign-child/foreign-subtree-execution.service.js +73 -50
- package/src/route-renderer/orchestration/integration-renderer.d.ts +42 -11
- package/src/route-renderer/orchestration/integration-renderer.js +83 -43
- package/src/route-renderer/orchestration/route-pipeline/route-prepared-options.builder.js +0 -3
- package/src/route-renderer/orchestration/route-pipeline/route-render-orchestrator.d.ts +36 -11
- package/src/route-renderer/orchestration/route-pipeline/route-render-orchestrator.js +74 -62
- package/src/route-renderer/page-declaration.d.ts +25 -0
- package/src/route-renderer/page-declaration.js +39 -0
- package/src/route-renderer/page-loading/component-declarations.d.ts +65 -0
- package/src/route-renderer/page-loading/component-declarations.js +77 -0
- package/src/route-renderer/page-loading/component-dependency-collection.d.ts +4 -16
- package/src/route-renderer/page-loading/component-dependency-collection.js +36 -105
- package/src/route-renderer/page-loading/declared-asset-collection.d.ts +19 -14
- package/src/route-renderer/page-loading/declared-asset-collection.js +66 -67
- package/src/route-renderer/page-loading/dependency-resolver.d.ts +18 -10
- package/src/route-renderer/page-loading/dependency-resolver.js +58 -99
- package/src/route-renderer/page-loading/lazy-entry-collection.d.ts +19 -26
- package/src/route-renderer/page-loading/lazy-entry-collection.js +53 -64
- package/src/route-renderer/page-loading/module-declaration-aggregation.d.ts +17 -3
- package/src/route-renderer/page-loading/module-declaration-aggregation.js +14 -13
- package/src/route-renderer/page-loading/page-module-loader.d.ts +31 -12
- package/src/route-renderer/page-loading/page-module-loader.js +66 -35
- package/src/route-renderer/page-loading/resolved-page-dependencies.js +14 -9
- package/src/router/server/pages-dir.d.ts +7 -0
- package/src/router/server/pages-dir.js +7 -0
- package/src/router/server/route-registry.d.ts +22 -10
- package/src/router/server/route-registry.js +54 -23
- package/src/router/server/segment-path-matcher.d.ts +29 -5
- package/src/router/server/segment-path-matcher.js +22 -19
- package/src/static-site-generator/production-page-browser-graph-prebuild.d.ts +2 -2
- package/src/static-site-generator/production-page-browser-graph-prebuild.js +5 -5
- package/src/static-site-generator/static-site-generator.d.ts +17 -2
- package/src/static-site-generator/static-site-generator.js +52 -18
- package/src/types/contracts.d.ts +216 -160
- package/src/types/eco-api-types.d.ts +86 -51
- package/src/types/locals-brand.d.ts +9 -0
- package/src/types/locals-brand.js +9 -0
- package/src/types/middleware-types.d.ts +143 -0
- package/src/types/middleware-types.js +1 -0
- package/src/types/public-types.d.ts +3 -2
- package/src/utils/file-roles.d.ts +48 -0
- package/src/utils/file-roles.js +85 -0
- package/src/utils/node-builtin.d.ts +10 -0
- package/src/utils/node-builtin.js +13 -0
- package/src/watchers/project-watcher.js +2 -2
- package/src/build/contracts/server-only-specifier.d.ts +0 -8
- package/src/build/contracts/server-only-specifier.js +0 -11
- package/src/route-renderer/orchestration/route-pipeline/marker-artifact.utils.d.ts +0 -5
- package/src/route-renderer/orchestration/route-pipeline/marker-artifact.utils.js +0 -11
- package/src/route-renderer/orchestration/route-pipeline/route-prepared-options.utils.d.ts +0 -4
- package/src/route-renderer/orchestration/route-pipeline/route-prepared-options.utils.js +0 -30
- package/src/route-renderer/page-loading/ecopages-virtual-imports.d.ts +0 -12
- package/src/route-renderer/page-loading/ecopages-virtual-imports.js +0 -100
- package/src/utils/endpoint-file-extensions.d.ts +0 -11
- package/src/utils/endpoint-file-extensions.js +0 -13
- package/src/utils/locals-utils.d.ts +0 -15
- package/src/utils/locals-utils.js +0 -34
package/README.md
CHANGED
|
@@ -59,7 +59,7 @@ await app.start();
|
|
|
59
59
|
|
|
60
60
|
### 3. Creating Pages
|
|
61
61
|
|
|
62
|
-
Use the `eco.page()` factory to define
|
|
62
|
+
Use the `eco.page()` factory to define a page. A file in `src/pages/` default-exports one:
|
|
63
63
|
|
|
64
64
|
```tsx
|
|
65
65
|
import { eco } from '@ecopages/core';
|
|
@@ -239,6 +239,6 @@ Use `@ecopages/core/client` for browser navigation and client runtime helpers.
|
|
|
239
239
|
|
|
240
240
|
### Runtime-Loaded Entries
|
|
241
241
|
|
|
242
|
-
`@ecopages/core/config/write-types`, `@ecopages/core/hmr/client/hmr-runtime`, and `@ecopages/core/view-reference` remain exported because Ecopages loads them by path at runtime. They are implementation entries, not general authoring surfaces. App authors import the view file, not
|
|
242
|
+
`@ecopages/core/config/write-types`, `@ecopages/core/hmr/client/hmr-runtime`, and `@ecopages/core/view-reference` remain exported because Ecopages loads them by path at runtime. `@ecopages/core/cli` holds what the `ecopages` CLI reads before it starts the app, so the CLI starts without loading the bundler. They are implementation entries, not general authoring surfaces. App authors import the view file, not the `view-reference` subpath.
|
|
243
243
|
|
|
244
244
|
`@ecopages/core/errors` remains an alias for the root error exports.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ecopages/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "Core package for Ecopages",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ecopages",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"@clack/prompts": "^1.7.0",
|
|
27
|
-
"@ecopages/file-system": "0.
|
|
27
|
+
"@ecopages/file-system": "0.10.0",
|
|
28
28
|
"@ecopages/logger": "^0.2.3",
|
|
29
29
|
"@ecopages/scripts-injector": "^0.1.5",
|
|
30
30
|
"@jridgewell/remapping": "^2.3.5",
|
|
@@ -33,11 +33,11 @@
|
|
|
33
33
|
"magic-string": "^1.4.3",
|
|
34
34
|
"oxc-parser": "^0.141.0",
|
|
35
35
|
"oxc-resolver": "^11.24.2",
|
|
36
|
-
"rolldown": "^1.
|
|
36
|
+
"rolldown": "^1.3.2",
|
|
37
37
|
"ws": "^8.20.1"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|
|
40
|
-
"@ecopages/dev-toolbar": "0.
|
|
40
|
+
"@ecopages/dev-toolbar": "0.10.0"
|
|
41
41
|
},
|
|
42
42
|
"peerDependenciesMeta": {
|
|
43
43
|
"@ecopages/dev-toolbar": {
|
|
@@ -71,6 +71,11 @@
|
|
|
71
71
|
"types": "./src/processor-contract.d.ts",
|
|
72
72
|
"default": "./src/processor-contract.js"
|
|
73
73
|
},
|
|
74
|
+
"./cli": {
|
|
75
|
+
"import": "./src/cli.js",
|
|
76
|
+
"types": "./src/cli.d.ts",
|
|
77
|
+
"default": "./src/cli.js"
|
|
78
|
+
},
|
|
74
79
|
"./build": {
|
|
75
80
|
"import": "./src/build.js",
|
|
76
81
|
"types": "./src/build.d.ts",
|
|
@@ -133,6 +138,11 @@
|
|
|
133
138
|
"types": "./src/processor-contract.d.ts",
|
|
134
139
|
"default": "./src/processor-contract.js"
|
|
135
140
|
},
|
|
141
|
+
"./cli.ts": {
|
|
142
|
+
"import": "./src/cli.js",
|
|
143
|
+
"types": "./src/cli.d.ts",
|
|
144
|
+
"default": "./src/cli.js"
|
|
145
|
+
},
|
|
136
146
|
"./build.ts": {
|
|
137
147
|
"import": "./src/build.js",
|
|
138
148
|
"types": "./src/build.d.ts",
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { EcoPagesAppConfig } from '../../config/internal-types.js';
|
|
2
|
-
import type { ApiHandler, ApiHandlerContext, ErrorHandler,
|
|
2
|
+
import type { ApiHandler, ApiHandlerContext, ErrorHandler, WithMiddlewareLocals, MiddlewareList, NoMiddlewareList, RouteGroupBuilder, RouteMethod, RouteOptions, RouteSchema, RouteHandlerContext, ErrorPageLoaders, ErrorPageTemplateProps, EcopagesRouteInfo, EcopagesWebSocketHandler } from '../../types/contracts.js';
|
|
3
3
|
import { type HttpErrorPageStatus } from '../../errors/http-error-page-contract.js';
|
|
4
4
|
import type { ReturnParseCliArgs } from '../../utils/parse-cli-args.js';
|
|
5
5
|
import { type RuntimeHost } from './runtime-host.js';
|
|
@@ -34,7 +34,7 @@ export type EcopagesAppDependencies = {
|
|
|
34
34
|
};
|
|
35
35
|
export type RouteGroupDefinition<TRequest extends Request = Request, TServer = unknown> = {
|
|
36
36
|
prefix: string;
|
|
37
|
-
middleware?: MiddlewareList
|
|
37
|
+
middleware?: MiddlewareList;
|
|
38
38
|
routes: readonly ApiHandler<string, TRequest, TServer>[];
|
|
39
39
|
};
|
|
40
40
|
export declare class EcopagesApp implements AsyncDisposable {
|
|
@@ -77,7 +77,7 @@ export declare class EcopagesApp implements AsyncDisposable {
|
|
|
77
77
|
readonly head: RouteMethod<this, Request, unknown>;
|
|
78
78
|
/** Registers a handler for the QUERY method, not for `ctx.query`. */
|
|
79
79
|
readonly query: RouteMethod<this, Request, unknown>;
|
|
80
|
-
route<TSchema extends RouteSchema = RouteSchema, TRouteContext extends ApiHandlerContext<Request, unknown> = ApiHandlerContext<Request, unknown>, const TMiddleware extends MiddlewareList
|
|
80
|
+
route<TSchema extends RouteSchema = RouteSchema, TRouteContext extends ApiHandlerContext<Request, unknown> = ApiHandlerContext<Request, unknown>, const TMiddleware extends MiddlewareList = NoMiddlewareList>(path: string, method: ApiHandler['method'], handler: (context: RouteHandlerContext<TSchema, WithMiddlewareLocals<TRouteContext, TMiddleware>>) => Promise<Response> | Response, options?: RouteOptions<TSchema, TMiddleware>): this;
|
|
81
81
|
add(handler: ApiHandler<string, Request, unknown>): this;
|
|
82
82
|
/**
|
|
83
83
|
* @remarks
|
|
@@ -85,13 +85,13 @@ export declare class EcopagesApp implements AsyncDisposable {
|
|
|
85
85
|
* and fills its schema fields before the handler runs.
|
|
86
86
|
*/
|
|
87
87
|
private addRouteHandler;
|
|
88
|
-
group<TContext extends ApiHandlerContext<Request, unknown> = ApiHandlerContext<Request, unknown>, const TMiddleware extends MiddlewareList
|
|
88
|
+
group<TContext extends ApiHandlerContext<Request, unknown> = ApiHandlerContext<Request, unknown>, const TMiddleware extends MiddlewareList = NoMiddlewareList>(prefixOrGroup: string | RouteGroupDefinition<Request, unknown>, callback?: (builder: RouteGroupBuilder<Request, unknown, WithMiddlewareLocals<TContext, TMiddleware>>) => void, options?: {
|
|
89
89
|
middleware?: TMiddleware;
|
|
90
90
|
}): this;
|
|
91
91
|
private registerGroup;
|
|
92
92
|
/**
|
|
93
93
|
* Register a view for static generation at build time.
|
|
94
|
-
* The view must have
|
|
94
|
+
* The view must have paths defined for dynamic routes.
|
|
95
95
|
*
|
|
96
96
|
* @param path - URL path pattern (e.g., '/posts/:slug')
|
|
97
97
|
* @param view - An imported view, an `eco.page` component, or a view loader
|
|
@@ -147,7 +147,7 @@ export class EcopagesApp {
|
|
|
147
147
|
}
|
|
148
148
|
/**
|
|
149
149
|
* Register a view for static generation at build time.
|
|
150
|
-
* The view must have
|
|
150
|
+
* The view must have paths defined for dynamic routes.
|
|
151
151
|
*
|
|
152
152
|
* @param path - URL path pattern (e.g., '/posts/:slug')
|
|
153
153
|
* @param view - An imported view, an `eco.page` component, or a view loader
|
|
@@ -11,6 +11,7 @@ import { DEFAULT_ECOPAGES_HOSTNAME, DEFAULT_ECOPAGES_PORT } from '../../constant
|
|
|
11
11
|
import { disposeDevResources, prepareRuntimePublicDir } from '../shared/runtime/runtime-server-lifecycle.js';
|
|
12
12
|
import { resolveServeRuntimeOrigin } from '../shared/runtime/runtime-app-bootstrap.js';
|
|
13
13
|
import { bindRuntimeServer } from '../shared/runtime/bind-runtime-server.js';
|
|
14
|
+
import { compileApiRoutes } from '../shared/http/api-request-pipeline.js';
|
|
14
15
|
import { serveDistFile } from '../shared/http/dist-file-handler.js';
|
|
15
16
|
import { applyResponseHeaders } from '../shared/http/response-headers.js';
|
|
16
17
|
import { DevSession } from './dev-session.js';
|
|
@@ -45,6 +46,7 @@ export class NodeServerAdapter {
|
|
|
45
46
|
phase = 'idle';
|
|
46
47
|
ready;
|
|
47
48
|
apiHandlers;
|
|
49
|
+
apiRoutes;
|
|
48
50
|
staticRoutes;
|
|
49
51
|
errorPageLoaders;
|
|
50
52
|
errorHandler;
|
|
@@ -91,6 +93,7 @@ export class NodeServerAdapter {
|
|
|
91
93
|
this.allowPortFallback = options.allowPortFallback !== false;
|
|
92
94
|
this.onDevelopmentRestart = options.onDevelopmentRestart;
|
|
93
95
|
this.apiHandlers = options.apiHandlers || [];
|
|
96
|
+
this.apiRoutes = compileApiRoutes(this.apiHandlers);
|
|
94
97
|
this.staticRoutes = options.staticRoutes || [];
|
|
95
98
|
this.errorPageLoaders = options.errorPageLoaders ?? {};
|
|
96
99
|
this.errorHandler = options.errorHandler;
|
|
@@ -343,7 +346,7 @@ export class NodeServerAdapter {
|
|
|
343
346
|
async handleRequestInternal(request, waitForReadiness) {
|
|
344
347
|
await this.initialize();
|
|
345
348
|
const context = {
|
|
346
|
-
|
|
349
|
+
apiRoutes: this.apiRoutes,
|
|
347
350
|
errorHandler: this.errorHandler,
|
|
348
351
|
serverInstance: this.serverInstance,
|
|
349
352
|
};
|
|
@@ -109,6 +109,8 @@ export declare class HmrManager implements IHmrManager {
|
|
|
109
109
|
* still register the entrypoint so file watchers can invalidate it on change.
|
|
110
110
|
*/
|
|
111
111
|
getResolvedScriptOutput(entrypointPath: string): ResolvedHmrEntrypoint | undefined;
|
|
112
|
+
private isPageFile;
|
|
113
|
+
private isLayoutFile;
|
|
112
114
|
getWatchedFiles(): Map<string, string>;
|
|
113
115
|
getRegisteredEntrypoints(): ReadonlyMap<string, ResolvedHmrEntrypoint>;
|
|
114
116
|
/**
|
|
@@ -136,8 +136,6 @@ export class HmrManager {
|
|
|
136
136
|
getWatchedFiles: () => this.entrypointRegistry.getWatchedOutputUrls(),
|
|
137
137
|
getRegisteredEntrypoints: () => this.entrypointRegistry.getRegisteredEntrypoints(),
|
|
138
138
|
getSrcDir: () => this.appConfig.absolutePaths.srcDir,
|
|
139
|
-
getPagesDir: () => this.appConfig.absolutePaths.pagesDir,
|
|
140
|
-
getLayoutsDir: () => this.appConfig.absolutePaths.layoutsDir,
|
|
141
139
|
getTemplateExtensions: () => this.appConfig.templatesExt,
|
|
142
140
|
isStylesheet: (filePath) => (this.stylesheetKinds ??= getStylesheetKinds(this.appConfig)).isStylesheet(filePath),
|
|
143
141
|
getEntrypointDependencyGraph: () => this.entrypointDependencyGraph,
|
|
@@ -332,6 +330,12 @@ export class HmrManager {
|
|
|
332
330
|
}
|
|
333
331
|
return registered;
|
|
334
332
|
}
|
|
333
|
+
isPageFile(filePath) {
|
|
334
|
+
return requireAppRuntime(this.appConfig).router?.isPageFile(filePath) ?? false;
|
|
335
|
+
}
|
|
336
|
+
isLayoutFile(filePath) {
|
|
337
|
+
return requireAppRuntime(this.appConfig).layoutFiles.has(path.resolve(filePath));
|
|
338
|
+
}
|
|
335
339
|
getWatchedFiles() {
|
|
336
340
|
return this.entrypointRegistry.getWatchedOutputUrls();
|
|
337
341
|
}
|
|
@@ -370,8 +374,8 @@ export class HmrManager {
|
|
|
370
374
|
getWatchedFiles: () => this.entrypointRegistry.getWatchedOutputUrls(),
|
|
371
375
|
getRegisteredEntrypoints: () => this.entrypointRegistry.getRegisteredEntrypoints(),
|
|
372
376
|
getSrcDir: () => this.appConfig.absolutePaths.srcDir,
|
|
373
|
-
|
|
374
|
-
|
|
377
|
+
isPageFile: (filePath) => this.isPageFile(filePath),
|
|
378
|
+
isLayoutFile: (filePath) => this.isLayoutFile(filePath),
|
|
375
379
|
getEntrypointDependencyGraph: () => this.entrypointDependencyGraph,
|
|
376
380
|
importServerModule: async (filePath) => await requireAppRuntime(this.appConfig).moduleLoader.importModule({
|
|
377
381
|
filePath,
|
|
@@ -1,5 +1,18 @@
|
|
|
1
|
-
import { type ParsedSegmentPath } from '../../../router/server/segment-path-matcher.js';
|
|
1
|
+
import { type SegmentPattern, type ParsedSegmentPath } from '../../../router/server/segment-path-matcher.js';
|
|
2
2
|
import type { ApiHandler, ApiHandlerContext, CacheInvalidator, ErrorHandler, PageParams, RenderContext } from '../../../types/contracts.js';
|
|
3
|
+
/**
|
|
4
|
+
* API handlers with their patterns parsed and ordered from most to least specific.
|
|
5
|
+
*
|
|
6
|
+
* @remarks Build it with {@link compileApiRoutes} when the handlers are known, so a request only walks the
|
|
7
|
+
* list. Equal scores keep registration order.
|
|
8
|
+
*/
|
|
9
|
+
export type ApiRoutes<TRequest extends Request = Request, TServer = unknown> = ReadonlyArray<{
|
|
10
|
+
handler: ApiHandler<string, TRequest, TServer>;
|
|
11
|
+
/** The handler's method, upper-cased; `GET` when the handler names none. */
|
|
12
|
+
method: string;
|
|
13
|
+
pattern: SegmentPattern<'api'>;
|
|
14
|
+
}>;
|
|
15
|
+
export declare function compileApiRoutes<TRequest extends Request = Request, TServer = unknown>(handlers: readonly ApiHandler<string, TRequest, TServer>[]): ApiRoutes<TRequest, TServer>;
|
|
3
16
|
export type ApiRequestPipelineOptions = {
|
|
4
17
|
getRenderContext: () => RenderContext;
|
|
5
18
|
getCacheService: () => CacheInvalidator | null;
|
|
@@ -8,11 +21,11 @@ export declare class ApiRequestPipeline<TRequest extends Request = Request, TSer
|
|
|
8
21
|
private readonly getRenderContext;
|
|
9
22
|
private readonly getCacheService;
|
|
10
23
|
constructor(options: ApiRequestPipelineOptions);
|
|
11
|
-
match(request: TRequest,
|
|
24
|
+
match(request: TRequest, apiRoutes: ApiRoutes<TRequest, TServer>, parsedPathSegments?: ParsedSegmentPath): {
|
|
12
25
|
routeConfig: ApiHandler<string, TRequest, TServer>;
|
|
13
26
|
params: PageParams;
|
|
14
27
|
} | null;
|
|
15
|
-
tryHandle(request: TRequest,
|
|
28
|
+
tryHandle(request: TRequest, apiRoutes: ApiRoutes<TRequest, TServer>, serverInstance: TServer | undefined, errorHandler?: ErrorHandler<TRequest, TServer>, parsedPathSegments?: ParsedSegmentPath): Promise<Response | null>;
|
|
16
29
|
execute(request: TRequest, params: PageParams, routeConfig: ApiHandler<string, TRequest, TServer>, serverInstance: TServer | undefined, errorHandler?: ErrorHandler<TRequest, TServer>): Promise<Response>;
|
|
17
30
|
/**
|
|
18
31
|
* Resolves an error that escaped a handler into a response.
|
|
@@ -1,12 +1,25 @@
|
|
|
1
|
-
import { createRequire } from '../../../utils/locals-utils.js';
|
|
2
1
|
import { HttpError } from '../../../errors/http-error.js';
|
|
3
2
|
import { ApiResponseBuilder } from './api-response.js';
|
|
3
|
+
import { flattenMiddleware } from '../../../middleware/create-middleware.js';
|
|
4
4
|
import { runMiddlewareChain } from './run-middleware-chain.js';
|
|
5
|
-
import { matchParsedSegmentPath, parseSegmentPath,
|
|
5
|
+
import { matchParsedSegmentPath, parseSegmentPath, compileSegmentPattern, } from '../../../router/server/segment-path-matcher.js';
|
|
6
6
|
import { appLogger } from '../../../global/app-logger.js';
|
|
7
7
|
import { isNodeClientAbortError } from '../../node/http-request-bridge.js';
|
|
8
8
|
import { validateRequest } from '../../../validation/validate-request.js';
|
|
9
9
|
import { parseSearchParams } from '../../../router/server/parse-search-params.js';
|
|
10
|
+
function validatedMiddleware(handler) {
|
|
11
|
+
flattenMiddleware(handler.middleware, `route ${handler.method} ${handler.path}`);
|
|
12
|
+
return handler;
|
|
13
|
+
}
|
|
14
|
+
export function compileApiRoutes(handlers) {
|
|
15
|
+
return handlers
|
|
16
|
+
.map((handler) => ({
|
|
17
|
+
handler: validatedMiddleware(handler),
|
|
18
|
+
method: (handler.method || 'GET').toUpperCase(),
|
|
19
|
+
pattern: compileSegmentPattern(handler.path, 'api'),
|
|
20
|
+
}))
|
|
21
|
+
.sort((a, b) => b.pattern.score - a.pattern.score);
|
|
22
|
+
}
|
|
10
23
|
export class ApiRequestPipeline {
|
|
11
24
|
getRenderContext;
|
|
12
25
|
getCacheService;
|
|
@@ -14,29 +27,25 @@ export class ApiRequestPipeline {
|
|
|
14
27
|
this.getRenderContext = options.getRenderContext;
|
|
15
28
|
this.getCacheService = options.getCacheService;
|
|
16
29
|
}
|
|
17
|
-
match(request,
|
|
30
|
+
match(request, apiRoutes, parsedPathSegments) {
|
|
18
31
|
const pathname = new URL(request.url).pathname;
|
|
19
32
|
const pathSegments = parsedPathSegments ?? parseSegmentPath(pathname);
|
|
20
33
|
const method = request.method.toUpperCase();
|
|
21
|
-
const
|
|
22
|
-
return scoreSegmentPathPattern(b.path, 'api') - scoreSegmentPathPattern(a.path, 'api');
|
|
23
|
-
});
|
|
24
|
-
for (const routeConfig of sortedHandlers) {
|
|
25
|
-
const routeMethod = (routeConfig.method || 'GET').toUpperCase();
|
|
34
|
+
for (const { handler, method: routeMethod, pattern } of apiRoutes) {
|
|
26
35
|
if (routeMethod !== method) {
|
|
27
36
|
continue;
|
|
28
37
|
}
|
|
29
|
-
const params = matchParsedSegmentPath(
|
|
38
|
+
const params = matchParsedSegmentPath(pattern, pathSegments);
|
|
30
39
|
if (params) {
|
|
31
|
-
return { routeConfig, params };
|
|
40
|
+
return { routeConfig: handler, params };
|
|
32
41
|
}
|
|
33
42
|
}
|
|
34
43
|
return null;
|
|
35
44
|
}
|
|
36
|
-
async tryHandle(request,
|
|
45
|
+
async tryHandle(request, apiRoutes, serverInstance, errorHandler, parsedPathSegments) {
|
|
37
46
|
let apiMatch;
|
|
38
47
|
try {
|
|
39
|
-
apiMatch = this.match(request,
|
|
48
|
+
apiMatch = this.match(request, apiRoutes, parsedPathSegments);
|
|
40
49
|
}
|
|
41
50
|
catch (error) {
|
|
42
51
|
return await this.handleError(error, { request, serverInstance, errorHandler });
|
|
@@ -91,15 +100,13 @@ export class ApiRequestPipeline {
|
|
|
91
100
|
return new Response('Internal Server Error', { status: 500 });
|
|
92
101
|
}
|
|
93
102
|
createContext(request, params, serverInstance) {
|
|
94
|
-
const locals = {};
|
|
95
103
|
return {
|
|
96
104
|
request,
|
|
97
105
|
params,
|
|
98
106
|
query: parseSearchParams(new URL(request.url).searchParams),
|
|
99
107
|
response: new ApiResponseBuilder(),
|
|
100
108
|
server: serverInstance,
|
|
101
|
-
locals,
|
|
102
|
-
require: createRequire(() => locals),
|
|
109
|
+
locals: {},
|
|
103
110
|
services: {
|
|
104
111
|
cache: this.getCacheService(),
|
|
105
112
|
},
|
|
@@ -198,7 +205,7 @@ export class ApiRequestPipeline {
|
|
|
198
205
|
}
|
|
199
206
|
}
|
|
200
207
|
async runMiddlewareChain(context, routeConfig) {
|
|
201
|
-
const middleware = routeConfig.middleware
|
|
208
|
+
const middleware = flattenMiddleware(routeConfig.middleware, `route ${routeConfig.method} ${routeConfig.path}`);
|
|
202
209
|
return runMiddlewareChain(middleware, context, async () => await routeConfig.handler(context));
|
|
203
210
|
}
|
|
204
211
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { ApiHandler, ApiHandlerContext,
|
|
1
|
+
import type { ApiHandler, ApiHandlerContext, WithMiddlewareLocals, MiddlewareList, NoMiddlewareList, RenderOptions, RouteHandlerContext, RouteSchema } from '../../../types/contracts.js';
|
|
2
2
|
type UniversalContext = ApiHandlerContext<Request, unknown>;
|
|
3
|
-
type DefineApiHandlerInput<TPath extends string, TSchema extends RouteSchema, TContext extends UniversalContext, TMiddleware extends MiddlewareList
|
|
4
|
-
handler: (context: RouteHandlerContext<TSchema, TContext
|
|
3
|
+
type DefineApiHandlerInput<TPath extends string, TSchema extends RouteSchema, TContext extends UniversalContext, TMiddleware extends MiddlewareList> = Omit<ApiHandler<TPath, Request, unknown>, 'handler' | 'middleware' | 'schema' | 'method' | 'path'> & {
|
|
4
|
+
handler: (context: RouteHandlerContext<TSchema, WithMiddlewareLocals<TContext, TMiddleware>>) => Promise<Response> | Response;
|
|
5
5
|
middleware?: TMiddleware;
|
|
6
6
|
schema?: TSchema;
|
|
7
7
|
};
|
|
@@ -14,7 +14,7 @@ type DefineApiHandlerInput<TPath extends string, TSchema extends RouteSchema, TC
|
|
|
14
14
|
* @remarks
|
|
15
15
|
* Without `path` the returned `path` is `''`, which `app.add` and `app.group` reject.
|
|
16
16
|
*/
|
|
17
|
-
type DefineApiMethod<TContext extends UniversalContext, TPathOptional extends boolean = false> = <const TPath extends string = '', TSchema extends RouteSchema = RouteSchema, THandlerContext extends TContext = TContext, const TMiddleware extends MiddlewareList
|
|
17
|
+
type DefineApiMethod<TContext extends UniversalContext, TPathOptional extends boolean = false> = <const TPath extends string = '', TSchema extends RouteSchema = RouteSchema, THandlerContext extends TContext = TContext, const TMiddleware extends MiddlewareList = NoMiddlewareList>(handler: DefineApiHandlerInput<TPath, TSchema, THandlerContext, TMiddleware> & (TPathOptional extends true ? {
|
|
18
18
|
path?: TPath;
|
|
19
19
|
} : {
|
|
20
20
|
path: TPath;
|
|
@@ -24,7 +24,7 @@ type DefineApiMethod<TContext extends UniversalContext, TPathOptional extends bo
|
|
|
24
24
|
* The returned handler's context is the base context: the typed one, narrowed by the schema and the
|
|
25
25
|
* middleware, is erased by the cast, as `app.add` would erase it. The middleware list needs no cast.
|
|
26
26
|
*/
|
|
27
|
-
export declare function defineApiHandler<TPath extends string, TSchema extends RouteSchema = RouteSchema, TContext extends UniversalContext = UniversalContext, const TMiddleware extends MiddlewareList
|
|
27
|
+
export declare function defineApiHandler<TPath extends string, TSchema extends RouteSchema = RouteSchema, TContext extends UniversalContext = UniversalContext, const TMiddleware extends MiddlewareList = NoMiddlewareList>(handler: DefineApiHandlerInput<TPath, TSchema, TContext, TMiddleware> & {
|
|
28
28
|
path: TPath;
|
|
29
29
|
method: ApiHandler<TPath, Request, unknown>['method'];
|
|
30
30
|
}): ApiHandler<TPath, Request, unknown>;
|
|
@@ -44,18 +44,18 @@ export declare function html(content: string, options?: RenderOptions): Response
|
|
|
44
44
|
export declare function redirect(url: string, status?: number): Response;
|
|
45
45
|
export interface GroupHandler<TPrefix extends string = string> {
|
|
46
46
|
prefix: TPrefix;
|
|
47
|
-
middleware?: MiddlewareList
|
|
47
|
+
middleware?: MiddlewareList;
|
|
48
48
|
routes: readonly ApiHandler<string, Request, unknown>[];
|
|
49
49
|
}
|
|
50
50
|
type GroupDefineHandler<TContext extends UniversalContext> = {
|
|
51
|
-
<const TPath extends string, TSchema extends RouteSchema = RouteSchema, const TMiddleware extends MiddlewareList
|
|
51
|
+
<const TPath extends string, TSchema extends RouteSchema = RouteSchema, const TMiddleware extends MiddlewareList = NoMiddlewareList>(handler: DefineApiHandlerInput<TPath, TSchema, TContext, TMiddleware> & {
|
|
52
52
|
path: TPath;
|
|
53
53
|
method: ApiHandler<TPath, Request, unknown>['method'];
|
|
54
54
|
}): ApiHandler<TPath, Request, unknown>;
|
|
55
55
|
} & {
|
|
56
56
|
[M in ApiHandler['method'] as Lowercase<M>]: DefineApiMethod<TContext>;
|
|
57
57
|
};
|
|
58
|
-
export declare function defineGroupHandler<TPrefix extends string, const TMiddleware extends MiddlewareList
|
|
58
|
+
export declare function defineGroupHandler<TPrefix extends string, const TMiddleware extends MiddlewareList = NoMiddlewareList, TContext extends UniversalContext = WithMiddlewareLocals<UniversalContext, TMiddleware>>(config: {
|
|
59
59
|
prefix: TPrefix;
|
|
60
60
|
middleware?: TMiddleware;
|
|
61
61
|
routes: (api: GroupDefineHandler<TContext>) => readonly ApiHandler<string, Request, unknown>[];
|
|
@@ -3,7 +3,7 @@ import { requireAppRuntime } from '../../../utils/app-runtime.js';
|
|
|
3
3
|
import { formatErrorChain } from '../../../utils/format-error-chain.js';
|
|
4
4
|
import { shouldLogPageFailure } from '../../../errors/http-error-page-contract.js';
|
|
5
5
|
import { prepareExplicitStaticRender } from '../../../route-renderer/explicit-view-render-preparation.js';
|
|
6
|
-
import { matchParsedSegmentPath, parseSegmentPath, } from '../../../router/server/segment-path-matcher.js';
|
|
6
|
+
import { compileSegmentPattern, matchParsedSegmentPath, parseSegmentPath, } from '../../../router/server/segment-path-matcher.js';
|
|
7
7
|
export const EXPLICIT_STATIC_ROUTE_MATCHER_ERRORS = {
|
|
8
8
|
missingIntegration: (routePath) => `View at ${routePath} is missing component identity integration. Ensure it's defined with eco.page() and exported as default.`,
|
|
9
9
|
noRendererForIntegration: (integrationName) => `No renderer found for integration: ${integrationName}`,
|
|
@@ -18,7 +18,10 @@ export class ExplicitStaticRouteMatcher {
|
|
|
18
18
|
constructor({ appConfig, routeRendererFactory, staticRoutes }) {
|
|
19
19
|
this.appConfig = appConfig;
|
|
20
20
|
this.routeRendererFactory = routeRendererFactory;
|
|
21
|
-
this.staticRoutes = staticRoutes
|
|
21
|
+
this.staticRoutes = staticRoutes.map((route) => ({
|
|
22
|
+
route,
|
|
23
|
+
pattern: compileSegmentPattern(route.path, 'explicit'),
|
|
24
|
+
}));
|
|
22
25
|
}
|
|
23
26
|
/**
|
|
24
27
|
* Match a request URL against explicit static routes.
|
|
@@ -26,8 +29,8 @@ export class ExplicitStaticRouteMatcher {
|
|
|
26
29
|
*/
|
|
27
30
|
match(url, parsedPathSegments) {
|
|
28
31
|
const pathSegments = parsedPathSegments ?? parseSegmentPath(new URL(url).pathname);
|
|
29
|
-
for (const route of this.staticRoutes) {
|
|
30
|
-
const params = matchParsedSegmentPath(
|
|
32
|
+
for (const { route, pattern } of this.staticRoutes) {
|
|
33
|
+
const params = matchParsedSegmentPath(pattern, pathSegments);
|
|
31
34
|
if (params !== null) {
|
|
32
35
|
return { route, params };
|
|
33
36
|
}
|
|
@@ -46,6 +49,7 @@ export class ExplicitStaticRouteMatcher {
|
|
|
46
49
|
const { renderer, props, view: renderableView, } = await prepareExplicitStaticRender({
|
|
47
50
|
routePath: route.path,
|
|
48
51
|
view,
|
|
52
|
+
viewFile: route.loader.filePath,
|
|
49
53
|
params,
|
|
50
54
|
appConfig: this.appConfig,
|
|
51
55
|
runtimeOrigin: requireAppRuntime(this.appConfig).origin,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { FileRouteMiddlewareContext } from '../../../types/contracts.js';
|
|
2
2
|
import type { PageCacheService } from '../../../page-cache/page-cache-service.js';
|
|
3
|
+
import { type MiddlewareHandler } from './run-middleware-chain.js';
|
|
3
4
|
export declare const FILE_ROUTE_MIDDLEWARE_PIPELINE_ERRORS: {
|
|
4
5
|
readonly middlewareRequiresDynamic: (filePath: string) => string;
|
|
5
6
|
};
|
|
@@ -18,17 +19,15 @@ export declare class FileRouteMiddlewarePipeline {
|
|
|
18
19
|
* Enforces the current middleware contract for file-routed pages.
|
|
19
20
|
*
|
|
20
21
|
* Middleware depends on request-scoped locals and therefore must not run when
|
|
21
|
-
* the page is treated as statically cacheable.
|
|
22
|
+
* the page is treated as statically cacheable. The Page itself must declare
|
|
23
|
+
* `cache: 'dynamic'`: an app default of `'dynamic'` does not give its `props`
|
|
24
|
+
* hook or layout factories the request.
|
|
22
25
|
*
|
|
23
|
-
* @
|
|
24
|
-
* @throws LocalsAccessError When middleware is configured for a non-dynamic page.
|
|
26
|
+
* @throws LocalsAccessError When middleware is configured for a Page that is not a Dynamic Page.
|
|
25
27
|
*/
|
|
26
28
|
assertValidConfiguration(input: {
|
|
27
|
-
middleware:
|
|
28
|
-
|
|
29
|
-
revalidate: number;
|
|
30
|
-
tags?: string[];
|
|
31
|
-
};
|
|
29
|
+
middleware: readonly MiddlewareHandler<FileRouteMiddlewareContext>[];
|
|
30
|
+
isDynamicPage: boolean;
|
|
32
31
|
filePath: string;
|
|
33
32
|
}): void;
|
|
34
33
|
/**
|
|
@@ -43,7 +42,7 @@ export declare class FileRouteMiddlewarePipeline {
|
|
|
43
42
|
createContext(input: {
|
|
44
43
|
request: Request;
|
|
45
44
|
params: Record<string, string>;
|
|
46
|
-
locals:
|
|
45
|
+
locals: Record<string, unknown>;
|
|
47
46
|
}): FileRouteMiddlewareContext;
|
|
48
47
|
/**
|
|
49
48
|
* Runs the middleware chain and eventually delegates to the render callback.
|
|
@@ -55,7 +54,7 @@ export declare class FileRouteMiddlewarePipeline {
|
|
|
55
54
|
* @returns Response from middleware or final render stage.
|
|
56
55
|
*/
|
|
57
56
|
run(input: {
|
|
58
|
-
middleware:
|
|
57
|
+
middleware: readonly MiddlewareHandler<FileRouteMiddlewareContext>[];
|
|
59
58
|
context: FileRouteMiddlewareContext;
|
|
60
59
|
renderResponse: () => Promise<Response>;
|
|
61
60
|
}): Promise<Response>;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { createRequire } from '../../../utils/locals-utils.js';
|
|
2
1
|
import { ApiResponseBuilder } from './api-response.js';
|
|
3
2
|
import { runMiddlewareChain } from './run-middleware-chain.js';
|
|
4
3
|
import { LocalsAccessError } from '../../../errors/locals-access-error.js';
|
|
@@ -22,13 +21,14 @@ export class FileRouteMiddlewarePipeline {
|
|
|
22
21
|
* Enforces the current middleware contract for file-routed pages.
|
|
23
22
|
*
|
|
24
23
|
* Middleware depends on request-scoped locals and therefore must not run when
|
|
25
|
-
* the page is treated as statically cacheable.
|
|
24
|
+
* the page is treated as statically cacheable. The Page itself must declare
|
|
25
|
+
* `cache: 'dynamic'`: an app default of `'dynamic'` does not give its `props`
|
|
26
|
+
* hook or layout factories the request.
|
|
26
27
|
*
|
|
27
|
-
* @
|
|
28
|
-
* @throws LocalsAccessError When middleware is configured for a non-dynamic page.
|
|
28
|
+
* @throws LocalsAccessError When middleware is configured for a Page that is not a Dynamic Page.
|
|
29
29
|
*/
|
|
30
30
|
assertValidConfiguration(input) {
|
|
31
|
-
if (input.middleware.length > 0 && input.
|
|
31
|
+
if (input.middleware.length > 0 && !input.isDynamicPage) {
|
|
32
32
|
throw new LocalsAccessError(FILE_ROUTE_MIDDLEWARE_PIPELINE_ERRORS.middlewareRequiresDynamic(input.filePath));
|
|
33
33
|
}
|
|
34
34
|
}
|
|
@@ -51,7 +51,6 @@ export class FileRouteMiddlewarePipeline {
|
|
|
51
51
|
cache: this.cacheService,
|
|
52
52
|
},
|
|
53
53
|
locals: input.locals,
|
|
54
|
-
require: createRequire(() => context.locals),
|
|
55
54
|
json: (data, options) => {
|
|
56
55
|
if (options?.status)
|
|
57
56
|
context.response.status(options.status);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { appLogger } from '../../../global/app-logger.js';
|
|
2
2
|
import { PageRequestCacheCoordinator } from '../../../page-cache/page-request-cache-coordinator.service.js';
|
|
3
|
+
import { flattenMiddleware } from '../../../middleware/create-middleware.js';
|
|
3
4
|
import { FileRouteMiddlewarePipeline } from './file-route-middleware-pipeline.js';
|
|
4
5
|
import { classifyPageFailure } from '../../../errors/http-error-page-contract.js';
|
|
5
6
|
import { isDevelopmentRuntime } from '../../../utils/runtime.js';
|
|
@@ -9,6 +10,7 @@ import { serveDistFile } from './dist-file-handler.js';
|
|
|
9
10
|
import { requireAppRuntime } from '../../../utils/app-runtime.js';
|
|
10
11
|
import { ensureIntegrationRuntimeReady } from '../../../build/app-build-manifest-runtime.js';
|
|
11
12
|
import { findIntegrationForFile } from '../../../plugins/find-integration-for-file.js';
|
|
13
|
+
import { isDynamicPage } from '../../../eco/page-data-hooks.js';
|
|
12
14
|
/**
|
|
13
15
|
* Matches file-system routes to rendered HTML responses.
|
|
14
16
|
*
|
|
@@ -72,7 +74,7 @@ export class FileSystemResponseMatcher {
|
|
|
72
74
|
const executionPlan = await this.createExecutionPlan(match, request);
|
|
73
75
|
this.fileRouteMiddlewarePipeline.assertValidConfiguration({
|
|
74
76
|
middleware: executionPlan.pageMiddleware,
|
|
75
|
-
|
|
77
|
+
isDynamicPage: executionPlan.isDynamic,
|
|
76
78
|
filePath: executionPlan.pageFilePath,
|
|
77
79
|
});
|
|
78
80
|
const routeRenderer = this.routeRendererFactory.getPageRenderer(executionPlan.pageFilePath);
|
|
@@ -88,6 +90,7 @@ export class FileSystemResponseMatcher {
|
|
|
88
90
|
params: match.params,
|
|
89
91
|
query: match.query,
|
|
90
92
|
locals: executionPlan.localsForRender,
|
|
93
|
+
request: executionPlan.requestForRender,
|
|
91
94
|
});
|
|
92
95
|
const html = await this.pageRequestCacheCoordinator.bodyToString(result.body);
|
|
93
96
|
const strategy = result.cacheStrategy ?? this.pageRequestCacheCoordinator.getDefaultCacheStrategy();
|
|
@@ -201,17 +204,22 @@ export class FileSystemResponseMatcher {
|
|
|
201
204
|
const pageFilePath = match.templateRoute.filePath;
|
|
202
205
|
const pageModule = await this.importPageModule(pageFilePath);
|
|
203
206
|
const pageComponent = pageModule.default;
|
|
204
|
-
const pageMiddleware = pageComponent.middleware
|
|
205
|
-
const
|
|
207
|
+
const pageMiddleware = flattenMiddleware(pageComponent.middleware, `page ${pageFilePath}`);
|
|
208
|
+
const isDynamic = isDynamicPage(pageComponent);
|
|
209
|
+
const pageCacheStrategy = isDynamic
|
|
210
|
+
? 'dynamic'
|
|
211
|
+
: (pageComponent.cache ?? this.pageRequestCacheCoordinator.getDefaultCacheStrategy());
|
|
206
212
|
return {
|
|
207
213
|
cacheKey,
|
|
208
214
|
request: resolvedRequest,
|
|
209
215
|
pageFilePath,
|
|
210
216
|
pageModule,
|
|
211
217
|
pageMiddleware,
|
|
218
|
+
isDynamic,
|
|
212
219
|
pageCacheStrategy,
|
|
213
220
|
localsStore,
|
|
214
|
-
localsForRender:
|
|
221
|
+
localsForRender: isDynamic ? localsStore : undefined,
|
|
222
|
+
requestForRender: isDynamic ? resolvedRequest : undefined,
|
|
215
223
|
};
|
|
216
224
|
}
|
|
217
225
|
/**
|
|
@@ -1,5 +1,32 @@
|
|
|
1
|
-
export type MiddlewareHandler<TContext> = (context: TContext, next: () => Promise<Response>) => Response | Promise<Response>;
|
|
1
|
+
export type MiddlewareHandler<TContext> = (context: TContext, next: (locals?: Record<string, unknown>) => Promise<Response>) => Response | Promise<Response>;
|
|
2
2
|
/**
|
|
3
3
|
* Runs an onion middleware chain and invokes the terminal handler when the chain completes.
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* Each stage receives its own `next`, which continues the chain exactly once. A second call throws before the
|
|
7
|
+
* chain advances, even from inside a later stage: a shared cursor would let a stage that calls `next` twice resume
|
|
8
|
+
* the chain after a guard that had already answered, running everything behind the guard, including the terminal
|
|
9
|
+
* handler, a second time. `next` also throws once its stage has returned or thrown, so a stage that answered cannot
|
|
10
|
+
* start the rest of the chain later, from a timer or a callback.
|
|
11
|
+
*
|
|
12
|
+
* A stage continues with `next(locals)`. The chain defines each own key it provides on `context.locals` before the
|
|
13
|
+
* next stage runs, so a later stage and the handler read it, and a later key overrides an earlier one. Keys are
|
|
14
|
+
* defined, not assigned, so a `__proto__` key is a plain local and cannot replace the prototype of the store. A
|
|
15
|
+
* provided key is typed for everything after it, so when a stage continues, every key an earlier stage provided
|
|
16
|
+
* must still hold its value: a plain function that wrote or deleted one would leave readers with a value their
|
|
17
|
+
* types do not describe, so its `next` throws instead.
|
|
18
|
+
*
|
|
19
|
+
* A stage answers with whatever it returns: the response `next` resolved with, edited, or another response that
|
|
20
|
+
* wraps or replaces it, as an outer stage adding headers to an immutable redirect must. A stage that returns before
|
|
21
|
+
* the rest of the chain it started settles, or throws, waits for that rest before its own result or error
|
|
22
|
+
* propagates, so the handler never runs after the request has been answered.
|
|
23
|
+
*
|
|
24
|
+
* When the rest of the chain fails, the stage decides only if it looked at the promise `next` returned: awaited it,
|
|
25
|
+
* returned it, or called `then`, `catch` or `finally` on it. Then its own result stands, so
|
|
26
|
+
* `try { await next() } catch { return fallback }` answers with the fallback. A stage that never looked at that
|
|
27
|
+
* promise, as in `next(); return new Response('ok')`, did not handle the failure, so the chain rethrows it once the
|
|
28
|
+
* stage has returned, even when the failure comes after. A stage that throws propagates its own error instead.
|
|
4
29
|
*/
|
|
5
|
-
export declare function runMiddlewareChain<TContext
|
|
30
|
+
export declare function runMiddlewareChain<TContext extends {
|
|
31
|
+
readonly locals: object;
|
|
32
|
+
}>(middleware: readonly MiddlewareHandler<TContext>[], context: TContext, terminal: () => Promise<Response>): Promise<Response>;
|